Initial support for finding option roms in coreboot flash layout.
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6 #ifndef __UTIL_H
7 #define __UTIL_H
8
9 #include "types.h" // u32
10
11 static inline void irq_disable(void)
12 {
13     asm volatile("cli": : :"memory");
14 }
15
16 static inline void irq_enable(void)
17 {
18     asm volatile("sti": : :"memory");
19 }
20
21 static 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
29 static inline void irq_restore(unsigned long flags)
30 {
31     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
32 }
33
34 static inline void cpu_relax(void)
35 {
36     asm volatile("rep ; nop": : :"memory");
37 }
38
39 static inline void nop(void)
40 {
41     asm volatile("nop");
42 }
43
44 static inline void hlt(void)
45 {
46     asm volatile("hlt");
47 }
48
49 static inline void wbinvd(void)
50 {
51     asm volatile("wbinvd");
52 }
53
54 static 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
61 static inline u64 rdtscll(void)
62 {
63     u64 val;
64     asm volatile("rdtsc" : "=A" (val));
65     return val;
66 }
67
68 // util.c
69 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
70 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
71 u8 checksum(void *buf, u32 len);
72 int streq(char *s1, char *s2);
73 void *memset(void *s, int c, size_t n);
74 void *memcpy(void *d1, const void *s1, size_t len);
75 inline void memcpy_far(u16 d_seg, void *d_far
76                        , u16 s_seg, const void *s_far, size_t len);
77 void *memmove(void *d, const void *s, size_t len);
78 char *strtcpy(char *dest, const char *src, size_t len);
79 struct bregs;
80 inline void call16(struct bregs *callregs);
81 inline void call16big(struct bregs *callregs);
82 inline void __call16_int(struct bregs *callregs, u16 offset);
83 #define call16_int(nr, callregs) do {                           \
84         extern void irq_trampoline_ ##nr ();                    \
85         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
86     } while (0)
87 inline void call16_simpint(int nr, u32 *eax, u32 *flags);
88 void usleep(u32 usec);
89 int get_keystroke(int msec);
90
91 // output.c
92 void debug_serial_setup();
93 void panic(const char *fmt, ...)
94     __attribute__ ((format (printf, 1, 2)))
95     __attribute__ ((noreturn));
96 void printf(const char *fmt, ...)
97     __attribute__ ((format (printf, 1, 2)));
98 void __dprintf(const char *fmt, ...)
99     __attribute__ ((format (printf, 1, 2)));
100 #define dprintf(lvl, fmt, args...) do {                         \
101         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
102             __dprintf((fmt) , ##args );                         \
103     } while (0)
104 void __debug_enter(struct bregs *regs, const char *fname);
105 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
106 void __debug_isr(const char *fname);
107 #define debug_enter(regs, lvl) do {                     \
108         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
109             __debug_enter((regs), __func__);            \
110     } while (0)
111 #define debug_isr(lvl) do {                             \
112         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
113             __debug_isr(__func__);                      \
114     } while (0)
115 #define debug_stub(regs)                        \
116     __debug_stub((regs), __LINE__, __func__)
117 void hexdump(void *d, int len);
118
119 // kbd.c
120 void kbd_setup();
121 void handle_15c2(struct bregs *regs);
122
123 // mouse.c
124 void mouse_setup();
125
126 // system.c
127 extern u32 RamSize;
128 extern u64 RamSizeOver4G;
129 void mathcp_setup();
130
131 // serial.c
132 void serial_setup();
133 void lpt_setup();
134
135 // clock.c
136 void timer_setup();
137 void ndelay(u32 count);
138 void udelay(u32 count);
139 void mdelay(u32 count);
140 u64 calc_future_tsc(u32 msecs);
141 void handle_1583(struct bregs *regs);
142 void handle_1586(struct bregs *regs);
143
144 // apm.c
145 void VISIBLE16 handle_1553(struct bregs *regs);
146
147 // pcibios.c
148 void handle_1ab1(struct bregs *regs);
149
150 // shadow.c
151 void make_bios_writable();
152 void make_bios_readonly();
153
154 // pciinit.c
155 void pci_bios_setup(void);
156
157 // smm.c
158 void smm_init();
159
160 // smpdetect.c
161 int smp_probe(void);
162 void smp_probe_setup(void);
163
164 // smbios.c
165 void smbios_init(void);
166
167 // coreboot.c
168 void *cb_find_optionrom(u32 vendev);
169 void coreboot_setup();
170
171 // vgahooks.c
172 void handle_155f();
173
174 // optionroms.c
175 void call_bcv(u16 seg, u16 ip);
176 void vga_setup();
177 void optionrom_setup();
178
179 // resume.c
180 void init_dma();
181
182 // pnpbios.c
183 #define PNP_SIGNATURE 0x506e5024 // $PnP
184 u16 get_pnp_offset();
185 void pnp_setup();
186
187 // mtrr.c
188 void mtrr_setup(void);
189
190 // romlayout.S
191 void reset_vector() __attribute__ ((noreturn));
192
193 // misc.c
194 extern u8 BiosChecksum;
195
196 #endif // util.h