blob: 5dbbca22fe3140b40d2080527e06377c0d4a2ae3 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Basic x86 asm functions and function defs.
2//
3// Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor786502d2008-02-27 10:41:41 -05006#ifndef __UTIL_H
7#define __UTIL_H
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "types.h" // u32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050010
Kevin O'Connor786502d2008-02-27 10:41:41 -050011static inline void irq_disable(void)
12{
13 asm volatile("cli": : :"memory");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050014}
15
Kevin O'Connor786502d2008-02-27 10:41:41 -050016static inline void irq_enable(void)
17{
18 asm volatile("sti": : :"memory");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050019}
20
21static inline unsigned long irq_save(void)
22{
23 unsigned long flags;
24 asm volatile("pushfl ; popl %0" : "=g" (flags));
25 irq_disable();
26 return flags;
27}
28
29static inline void irq_restore(unsigned long flags)
30{
31 asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
32}
33
Kevin O'Connor06ad44e2008-04-05 19:30:02 -040034static inline void cpu_relax(void)
35{
36 asm volatile("rep ; nop": : :"memory");
37}
38
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050039static inline void nop(void)
40{
41 asm volatile("nop");
42}
43
Kevin O'Connor95b2f782008-03-05 19:52:06 -050044static inline void hlt(void)
45{
46 asm volatile("hlt");
47}
48
Kevin O'Connorda4a6482008-06-08 13:48:06 -040049static inline void wbinvd(void)
50{
51 asm volatile("wbinvd");
52}
53
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040054static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
55{
56 asm("cpuid"
57 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
58 : "0" (index));
59}
60
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050061static inline u64 rdtscll(void)
62{
63 u64 val;
64 asm volatile("rdtsc" : "=A" (val));
65 return val;
66}
67
Kevin O'Connora83ff552009-01-01 21:00:59 -050068// util.c
69inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
Kevin O'Connor94fd47e2009-02-15 15:21:10 -050070u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
71u8 checksum(void *buf, u32 len);
Kevin O'Connor38d1a342009-04-18 16:59:47 -040072int memcmp(const void *s1, const void *s2, size_t n);
Kevin O'Connor67823442009-04-13 14:14:51 -040073size_t strlen(const char *s);
Kevin O'Connor38d1a342009-04-18 16:59:47 -040074int strcmp(const char *s1, const char *s2);
Kevin O'Connor5e4235f2008-04-12 09:00:04 -040075void *memset(void *s, int c, size_t n);
Kevin O'Connord9441142009-04-19 23:18:54 -040076void memcpy4(void *d1, const void *s1, size_t len);
77#define memcpy(d1, s1, len) __builtin_memcpy((d1), (s1), (len))
Kevin O'Connor8b267cb2009-01-19 19:25:21 -050078inline void memcpy_far(u16 d_seg, void *d_far
79 , u16 s_seg, const void *s_far, size_t len);
Kevin O'Connorc7812932008-06-08 23:08:12 -040080void *memmove(void *d, const void *s, size_t len);
Kevin O'Connor71f036d2009-02-08 16:57:22 -050081char *strtcpy(char *dest, const char *src, size_t len);
Kevin O'Connor9521e262008-07-04 13:04:29 -040082struct bregs;
83inline void call16(struct bregs *callregs);
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -050084inline void call16big(struct bregs *callregs);
Kevin O'Connor9521e262008-07-04 13:04:29 -040085inline void __call16_int(struct bregs *callregs, u16 offset);
Kevin O'Connor3a47a312008-03-01 14:46:37 -050086#define call16_int(nr, callregs) do { \
87 extern void irq_trampoline_ ##nr (); \
Kevin O'Connor117fc212008-04-13 18:17:02 -040088 __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050089 } while (0)
Kevin O'Connora83ff552009-01-01 21:00:59 -050090inline void call16_simpint(int nr, u32 *eax, u32 *flags);
Kevin O'Connor5ca4b952009-02-15 13:45:48 -050091void usleep(u32 usec);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -050092int get_keystroke(int msec);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050093
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050094// output.c
Kevin O'Connor61d6b062008-06-21 12:15:10 -040095void debug_serial_setup();
Kevin O'Connore07e18e2009-02-08 17:07:29 -050096void panic(const char *fmt, ...)
Kevin O'Connor567e4e32008-04-05 11:37:51 -040097 __attribute__ ((format (printf, 1, 2)))
98 __attribute__ ((noreturn));
Kevin O'Connor567e4e32008-04-05 11:37:51 -040099void printf(const char *fmt, ...)
100 __attribute__ ((format (printf, 1, 2)));
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400101void __dprintf(const char *fmt, ...)
102 __attribute__ ((format (printf, 1, 2)));
103#define dprintf(lvl, fmt, args...) do { \
104 if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \
105 __dprintf((fmt) , ##args ); \
106 } while (0)
Kevin O'Connor05600342009-01-02 13:10:58 -0500107void __debug_enter(struct bregs *regs, const char *fname);
108void __debug_stub(struct bregs *regs, int lineno, const char *fname);
Kevin O'Connored128492008-03-11 11:14:59 -0400109void __debug_isr(const char *fname);
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400110#define debug_enter(regs, lvl) do { \
111 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
Kevin O'Connor05600342009-01-02 13:10:58 -0500112 __debug_enter((regs), __func__); \
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400113 } while (0)
114#define debug_isr(lvl) do { \
115 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
116 __debug_isr(__func__); \
117 } while (0)
Kevin O'Connor05600342009-01-02 13:10:58 -0500118#define debug_stub(regs) \
119 __debug_stub((regs), __LINE__, __func__)
Kevin O'Connor1eba4292009-02-17 23:14:25 -0500120void hexdump(void *d, int len);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500121
122// kbd.c
Kevin O'Connorefda97d2008-07-04 13:10:12 -0400123void kbd_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500124void handle_15c2(struct bregs *regs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500125
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400126// mouse.c
127void mouse_setup();
128
129// system.c
Kevin O'Connore7916362008-12-28 22:03:17 -0500130extern u32 RamSize;
131extern u64 RamSizeOver4G;
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400132void mathcp_setup();
133
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400134// serial.c
135void serial_setup();
136void lpt_setup();
137
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500138// clock.c
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400139void timer_setup();
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500140void ndelay(u32 count);
141void udelay(u32 count);
142void mdelay(u32 count);
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500143u64 calc_future_tsc(u32 msecs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500144void handle_1583(struct bregs *regs);
Kevin O'Connor5be04902008-05-18 17:12:06 -0400145void handle_1586(struct bregs *regs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500146
Kevin O'Connor95b2f782008-03-05 19:52:06 -0500147// apm.c
Kevin O'Connor44d65302008-03-08 11:34:46 -0500148void VISIBLE16 handle_1553(struct bregs *regs);
Kevin O'Connor95b2f782008-03-05 19:52:06 -0500149
Kevin O'Connora0dc2962008-03-16 14:29:32 -0400150// pcibios.c
151void handle_1ab1(struct bregs *regs);
152
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400153// shadow.c
154void make_bios_writable();
155void make_bios_readonly();
156
Kevin O'Connor0525d292008-07-04 06:18:30 -0400157// pciinit.c
158void pci_bios_setup(void);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500159
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400160// smm.c
161void smm_init();
162
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400163// smpdetect.c
164int smp_probe(void);
Kevin O'Connoracf13742008-11-29 11:19:19 -0500165void smp_probe_setup(void);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400166
Kevin O'Connor0525d292008-07-04 06:18:30 -0400167// smbios.c
168void smbios_init(void);
169
Kevin O'Connorc7812932008-06-08 23:08:12 -0400170// coreboot.c
Kevin O'Connor67823442009-04-13 14:14:51 -0400171const char *cbfs_findNprefix(const char *prefix, int n);
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400172int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
Kevin O'Connor67823442009-04-13 14:14:51 -0400173void cbfs_run_payload(const char *filename);
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400174struct cbfs_file;
175struct cbfs_file *cbfs_copy_gen_optionrom(void *dst, u32 maxlen
176 , struct cbfs_file *file);
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400177void coreboot_setup();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400178
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400179// vgahooks.c
180void handle_155f();
181
Kevin O'Connor714325c2008-11-01 20:32:27 -0400182// optionroms.c
Kevin O'Connor0a924122009-02-08 19:43:47 -0500183void call_bcv(u16 seg, u16 ip);
Kevin O'Connor714325c2008-11-01 20:32:27 -0400184void vga_setup();
185void optionrom_setup();
186
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500187// resume.c
188void init_dma();
189
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500190// pnpbios.c
191#define PNP_SIGNATURE 0x506e5024 // $PnP
192u16 get_pnp_offset();
193void pnp_setup();
194
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500195// mtrr.c
196void mtrr_setup(void);
197
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500198// romlayout.S
199void reset_vector() __attribute__ ((noreturn));
200
Kevin O'Connor30853762009-01-17 18:49:20 -0500201// misc.c
202extern u8 BiosChecksum;
203
Kevin O'Connor786502d2008-02-27 10:41:41 -0500204#endif // util.h