Do PCI initialization before vga init.
[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 // GDT bit manipulation
92 #define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
93                       | (((u64)(v) & 0x00ffffff) << 16))
94 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
95                       | (((u64)(v) & 0x0000ffff) << 0))
96 #define GDT_CODE     (0x9bULL << 40) // Code segment - P,R,A bits also set
97 #define GDT_DATA     (0x93ULL << 40) // Data segment - W,A bits also set
98 #define GDT_B        (0x1ULL << 54)  // Big flag
99 #define GDT_G        (0x1ULL << 55)  // Granularity flag
100
101 #define call16_simpint(nr, peax, pflags) do {                           \
102         ASSERT16();                                                     \
103         asm volatile(                                                   \
104             "stc\n"                                                     \
105             "int %2\n"                                                  \
106             "pushfl\n"                                                  \
107             "popl %1\n"                                                 \
108             "cli\n"                                                     \
109             "cld"                                                       \
110             : "+a"(*peax), "=r"(*pflags)                                \
111             : "i"(nr)                                                   \
112             : "cc", "memory");                                          \
113     } while (0)
114
115 // util.c
116 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
117 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
118 u8 checksum(void *buf, u32 len);
119 int memcmp(const void *s1, const void *s2, size_t n);
120 size_t strlen(const char *s);
121 int strcmp(const char *s1, const char *s2);
122 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
123 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
124 void *memset(void *s, int c, size_t n);
125 void *memcpy(void *d1, const void *s1, size_t len);
126 #if MODE16 == 0
127 #define memcpy __builtin_memcpy
128 #endif
129 inline void memcpy_far(u16 d_seg, void *d_far
130                        , u16 s_seg, const void *s_far, size_t len);
131 void *memmove(void *d, const void *s, size_t len);
132 char *strtcpy(char *dest, const char *src, size_t len);
133 struct bregs;
134 inline void call16(struct bregs *callregs);
135 inline void call16big(struct bregs *callregs);
136 inline void __call16_int(struct bregs *callregs, u16 offset);
137 #define call16_int(nr, callregs) do {                           \
138         extern void irq_trampoline_ ##nr ();                    \
139         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
140     } while (0)
141 void usleep(u32 usec);
142 int get_keystroke(int msec);
143
144 // output.c
145 void debug_serial_setup();
146 void panic(const char *fmt, ...)
147     __attribute__ ((format (printf, 1, 2)))
148     __attribute__ ((noreturn));
149 void printf(const char *fmt, ...)
150     __attribute__ ((format (printf, 1, 2)));
151 void __dprintf(const char *fmt, ...)
152     __attribute__ ((format (printf, 1, 2)));
153 #define dprintf(lvl, fmt, args...) do {                         \
154         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
155             __dprintf((fmt) , ##args );                         \
156     } while (0)
157 void __debug_enter(struct bregs *regs, const char *fname);
158 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
159 void __debug_isr(const char *fname);
160 #define debug_enter(regs, lvl) do {                     \
161         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
162             __debug_enter((regs), __func__);            \
163     } while (0)
164 #define debug_isr(lvl) do {                             \
165         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
166             __debug_isr(__func__);                      \
167     } while (0)
168 #define debug_stub(regs)                        \
169     __debug_stub((regs), __LINE__, __func__)
170 void hexdump(void *d, int len);
171
172 // kbd.c
173 void kbd_setup();
174 void handle_15c2(struct bregs *regs);
175
176 // mouse.c
177 void mouse_setup();
178
179 // system.c
180 extern u32 RamSize;
181 extern u64 RamSizeOver4G;
182 void mathcp_setup();
183
184 // serial.c
185 void serial_setup();
186 void lpt_setup();
187
188 // clock.c
189 void timer_setup();
190 void ndelay(u32 count);
191 void udelay(u32 count);
192 void mdelay(u32 count);
193 u64 calc_future_tsc(u32 msecs);
194 void handle_1583(struct bregs *regs);
195 void handle_1586(struct bregs *regs);
196
197 // apm.c
198 void VISIBLE16 handle_1553(struct bregs *regs);
199
200 // pcibios.c
201 void handle_1ab1(struct bregs *regs);
202
203 // shadow.c
204 void make_bios_writable();
205 void make_bios_readonly();
206
207 // pciinit.c
208 void pci_setup(void);
209
210 // smm.c
211 void smm_init();
212
213 // smp.c
214 extern u32 CountCPUs;
215 void wrmsr_smp(u32 index, u64 val);
216 void smp_probe(void);
217 void smp_probe_setup(void);
218
219 // smbios.c
220 void smbios_init(void);
221
222 // coreboot.c
223 struct cbfs_file;
224 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
225 u32 cbfs_datasize(struct cbfs_file *file);
226 const char *cbfs_filename(struct cbfs_file *file);
227 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
228 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
229 void cbfs_run_payload(struct cbfs_file *file);
230
231 void coreboot_copy_biostable();
232 void coreboot_setup();
233
234 // vgahooks.c
235 extern int VGAbdf;
236 void handle_155f();
237 void vgahook_setup(const char *vendor, const char *part);
238
239 // optionroms.c
240 void call_bcv(u16 seg, u16 ip);
241 void optionrom_setup();
242 void vga_setup();
243 void s3_resume_vga_init();
244 extern u32 RomEnd;
245
246 // resume.c
247 void init_dma();
248
249 // pnpbios.c
250 #define PNP_SIGNATURE 0x506e5024 // $PnP
251 u16 get_pnp_offset();
252 void pnp_setup();
253
254 // pmm.c
255 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
256 void *zone_malloc(struct zone_s *zone, u32 size, u32 align);
257 void malloc_setup();
258 void malloc_finalize();
259 void pmm_setup();
260 void pmm_finalize();
261 // Minimum alignment of malloc'd memory
262 #define MALLOC_MIN_ALIGN 16
263 // Helper functions for memory allocation.
264 static inline void *malloc_high(u32 size) {
265     return zone_malloc(&ZoneHigh, size, MALLOC_MIN_ALIGN);
266 }
267 static inline void *malloc_fseg(u32 size) {
268     return zone_malloc(&ZoneFSeg, size, MALLOC_MIN_ALIGN);
269 }
270 static inline void *memalign_tmphigh(u32 align, u32 size) {
271     return zone_malloc(&ZoneTmpHigh, size, align);
272 }
273 static inline void *memalign_high(u32 align, u32 size) {
274     return zone_malloc(&ZoneHigh, size, align);
275 }
276
277 // mtrr.c
278 void mtrr_setup(void);
279
280 // romlayout.S
281 void reset_vector() __attribute__ ((noreturn));
282
283 // misc.c
284 extern u8 BiosChecksum;
285
286 // mptable.c
287 extern int irq0override;
288
289 // version (auto generated file out/version.c)
290 extern const char VERSION[];
291
292 #endif // util.h