Minor - remove VAR16_32 from extern declaration in header.
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
2 //
3 // Copyright (C) 2008,2009  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 #define CPUID_MSR (1 << 5)
55 #define CPUID_APIC (1 << 9)
56 #define CPUID_MTRR (1 << 12)
57 static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
58 {
59     asm("cpuid"
60         : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
61         : "0" (index));
62 }
63
64 static inline u64 rdmsr(u32 index)
65 {
66     u64 ret;
67     asm ("rdmsr" : "=A"(ret) : "c"(index));
68     return ret;
69 }
70
71 static inline void wrmsr(u32 index, u64 val)
72 {
73     asm volatile ("wrmsr" : : "c"(index), "A"(val));
74 }
75
76 static inline u64 rdtscll(void)
77 {
78     u64 val;
79     asm volatile("rdtsc" : "=A" (val));
80     return val;
81 }
82
83 #define call16_simpint(nr, peax, pflags) do {                           \
84         ASSERT16();                                                     \
85         asm volatile(                                                   \
86             "stc\n"                                                     \
87             "int %2\n"                                                  \
88             "pushfl\n"                                                  \
89             "popl %1\n"                                                 \
90             "cli\n"                                                     \
91             "cld"                                                       \
92             : "+a"(*peax), "=r"(*pflags)                                \
93             : "i"(nr)                                                   \
94             : "cc", "memory");                                          \
95     } while (0)
96
97 // util.c
98 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
99 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
100 u8 checksum(void *buf, u32 len);
101 int memcmp(const void *s1, const void *s2, size_t n);
102 size_t strlen(const char *s);
103 int strcmp(const char *s1, const char *s2);
104 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
105 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
106 void *memset(void *s, int c, size_t n);
107 void *memcpy(void *d1, const void *s1, size_t len);
108 #if MODE16 == 0
109 #define memcpy __builtin_memcpy
110 #endif
111 inline void memcpy_far(u16 d_seg, void *d_far
112                        , u16 s_seg, const void *s_far, size_t len);
113 void *memmove(void *d, const void *s, size_t len);
114 char *strtcpy(char *dest, const char *src, size_t len);
115 struct bregs;
116 inline void call16(struct bregs *callregs);
117 inline void call16big(struct bregs *callregs);
118 inline void __call16_int(struct bregs *callregs, u16 offset);
119 #define call16_int(nr, callregs) do {                           \
120         extern void irq_trampoline_ ##nr ();                    \
121         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
122     } while (0)
123 void usleep(u32 usec);
124 int get_keystroke(int msec);
125
126 // output.c
127 void debug_serial_setup();
128 void panic(const char *fmt, ...)
129     __attribute__ ((format (printf, 1, 2)))
130     __attribute__ ((noreturn));
131 void printf(const char *fmt, ...)
132     __attribute__ ((format (printf, 1, 2)));
133 void __dprintf(const char *fmt, ...)
134     __attribute__ ((format (printf, 1, 2)));
135 #define dprintf(lvl, fmt, args...) do {                         \
136         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
137             __dprintf((fmt) , ##args );                         \
138     } while (0)
139 void __debug_enter(struct bregs *regs, const char *fname);
140 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
141 void __debug_isr(const char *fname);
142 #define debug_enter(regs, lvl) do {                     \
143         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
144             __debug_enter((regs), __func__);            \
145     } while (0)
146 #define debug_isr(lvl) do {                             \
147         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
148             __debug_isr(__func__);                      \
149     } while (0)
150 #define debug_stub(regs)                        \
151     __debug_stub((regs), __LINE__, __func__)
152 void hexdump(void *d, int len);
153
154 // kbd.c
155 void kbd_setup();
156 void handle_15c2(struct bregs *regs);
157
158 // mouse.c
159 void mouse_setup();
160
161 // system.c
162 extern u32 RamSize;
163 extern u64 RamSizeOver4G;
164 void mathcp_setup();
165
166 // serial.c
167 void serial_setup();
168 void lpt_setup();
169
170 // clock.c
171 void timer_setup();
172 void ndelay(u32 count);
173 void udelay(u32 count);
174 void mdelay(u32 count);
175 u64 calc_future_tsc(u32 msecs);
176 void handle_1583(struct bregs *regs);
177 void handle_1586(struct bregs *regs);
178
179 // apm.c
180 void VISIBLE16 handle_1553(struct bregs *regs);
181
182 // pcibios.c
183 void handle_1ab1(struct bregs *regs);
184
185 // shadow.c
186 void make_bios_writable();
187 void make_bios_readonly();
188
189 // pciinit.c
190 void pci_bios_setup(void);
191
192 // smm.c
193 void smm_init();
194
195 // smp.c
196 extern u32 CountCPUs;
197 void wrmsr_smp(u32 index, u64 val);
198 void smp_probe(void);
199 void smp_probe_setup(void);
200
201 // smbios.c
202 void smbios_init(void);
203
204 // coreboot.c
205 const char *cbfs_findNprefix(const char *prefix, int n);
206 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
207 void cbfs_run_payload(const char *filename);
208 struct cbfs_file;
209 struct cbfs_file *cbfs_copyfile_prefix(void *dst, u32 maxlen, char *prefix
210                                        , struct cbfs_file *last);
211 void coreboot_setup();
212
213 // vgahooks.c
214 void handle_155f();
215
216 // optionroms.c
217 void call_bcv(u16 seg, u16 ip);
218 void vga_setup();
219 void optionrom_setup();
220
221 // resume.c
222 void init_dma();
223
224 // pnpbios.c
225 #define PNP_SIGNATURE 0x506e5024 // $PnP
226 u16 get_pnp_offset();
227 void pnp_setup();
228
229 // mtrr.c
230 void mtrr_setup(void);
231
232 // romlayout.S
233 void reset_vector() __attribute__ ((noreturn));
234
235 // misc.c
236 extern u8 BiosChecksum;
237
238 #endif // util.h