Add Post Memory Manager (PMM) support.
[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 static inline u32 __ffs(u32 word)
84 {
85     asm("bsf %1,%0"
86         : "=r" (word)
87         : "rm" (word));
88     return word;
89 }
90
91 #define call16_simpint(nr, peax, pflags) do {                           \
92         ASSERT16();                                                     \
93         asm volatile(                                                   \
94             "stc\n"                                                     \
95             "int %2\n"                                                  \
96             "pushfl\n"                                                  \
97             "popl %1\n"                                                 \
98             "cli\n"                                                     \
99             "cld"                                                       \
100             : "+a"(*peax), "=r"(*pflags)                                \
101             : "i"(nr)                                                   \
102             : "cc", "memory");                                          \
103     } while (0)
104
105 // util.c
106 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
107 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
108 u8 checksum(void *buf, u32 len);
109 int memcmp(const void *s1, const void *s2, size_t n);
110 size_t strlen(const char *s);
111 int strcmp(const char *s1, const char *s2);
112 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
113 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
114 void *memset(void *s, int c, size_t n);
115 void *memcpy(void *d1, const void *s1, size_t len);
116 #if MODE16 == 0
117 #define memcpy __builtin_memcpy
118 #endif
119 inline void memcpy_far(u16 d_seg, void *d_far
120                        , u16 s_seg, const void *s_far, size_t len);
121 void *memmove(void *d, const void *s, size_t len);
122 char *strtcpy(char *dest, const char *src, size_t len);
123 struct bregs;
124 inline void call16(struct bregs *callregs);
125 inline void call16big(struct bregs *callregs);
126 inline void __call16_int(struct bregs *callregs, u16 offset);
127 #define call16_int(nr, callregs) do {                           \
128         extern void irq_trampoline_ ##nr ();                    \
129         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
130     } while (0)
131 void usleep(u32 usec);
132 int get_keystroke(int msec);
133
134 // output.c
135 void debug_serial_setup();
136 void panic(const char *fmt, ...)
137     __attribute__ ((format (printf, 1, 2)))
138     __attribute__ ((noreturn));
139 void printf(const char *fmt, ...)
140     __attribute__ ((format (printf, 1, 2)));
141 void __dprintf(const char *fmt, ...)
142     __attribute__ ((format (printf, 1, 2)));
143 #define dprintf(lvl, fmt, args...) do {                         \
144         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
145             __dprintf((fmt) , ##args );                         \
146     } while (0)
147 void __debug_enter(struct bregs *regs, const char *fname);
148 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
149 void __debug_isr(const char *fname);
150 #define debug_enter(regs, lvl) do {                     \
151         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
152             __debug_enter((regs), __func__);            \
153     } while (0)
154 #define debug_isr(lvl) do {                             \
155         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
156             __debug_isr(__func__);                      \
157     } while (0)
158 #define debug_stub(regs)                        \
159     __debug_stub((regs), __LINE__, __func__)
160 void hexdump(void *d, int len);
161
162 // kbd.c
163 void kbd_setup();
164 void handle_15c2(struct bregs *regs);
165
166 // mouse.c
167 void mouse_setup();
168
169 // system.c
170 extern u32 RamSize;
171 extern u64 RamSizeOver4G;
172 void mathcp_setup();
173
174 // serial.c
175 void serial_setup();
176 void lpt_setup();
177
178 // clock.c
179 void timer_setup();
180 void ndelay(u32 count);
181 void udelay(u32 count);
182 void mdelay(u32 count);
183 u64 calc_future_tsc(u32 msecs);
184 void handle_1583(struct bregs *regs);
185 void handle_1586(struct bregs *regs);
186
187 // apm.c
188 void VISIBLE16 handle_1553(struct bregs *regs);
189
190 // pcibios.c
191 void handle_1ab1(struct bregs *regs);
192
193 // shadow.c
194 void make_bios_writable();
195 void make_bios_readonly();
196
197 // pciinit.c
198 void pci_bios_setup(void);
199
200 // smm.c
201 void smm_init();
202
203 // smp.c
204 extern u32 CountCPUs;
205 void wrmsr_smp(u32 index, u64 val);
206 void smp_probe(void);
207 void smp_probe_setup(void);
208
209 // smbios.c
210 void smbios_init(void);
211
212 // coreboot.c
213 const char *cbfs_findNprefix(const char *prefix, int n);
214 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
215 void cbfs_run_payload(const char *filename);
216 struct cbfs_file;
217 struct cbfs_file *cbfs_copyfile_prefix(void *dst, u32 maxlen, const char *prefix
218                                        , struct cbfs_file *last);
219 void coreboot_copy_biostable();
220 void coreboot_setup();
221
222 // vgahooks.c
223 extern int VGAbdf;
224 void handle_155f();
225 void vgahook_setup(const char *vendor, const char *part);
226
227 // optionroms.c
228 void call_bcv(u16 seg, u16 ip);
229 void optionrom_setup();
230 void vga_setup();
231 void s3_resume_vga_init();
232 extern u32 RomEnd;
233
234 // resume.c
235 void init_dma();
236
237 // pnpbios.c
238 #define PNP_SIGNATURE 0x506e5024 // $PnP
239 u16 get_pnp_offset();
240 void pnp_setup();
241
242 // pmm.c
243 void *malloc_high(u32 size);
244 void *malloc_fseg(u32 size);
245 void malloc_setup();
246 void malloc_finalize();
247 void pmm_setup();
248 void pmm_finalize();
249 // Minimum alignment of malloc'd memory
250 #define MALLOC_MIN_ALIGN 16
251
252 // mtrr.c
253 void mtrr_setup(void);
254
255 // romlayout.S
256 void reset_vector() __attribute__ ((noreturn));
257
258 // misc.c
259 extern u8 BiosChecksum;
260
261 // mptable.c
262 extern int irq0override;
263
264 // version (auto generated file out/version.c)
265 extern const char VERSION[];
266
267 #endif // util.h