5db1ba0ec0e12273ec4b752b18135ee9317cde89
[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 GPLv3 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 void *memset(void *s, int c, size_t n);
69 void *memcpy(void *d1, const void *s1, size_t len);
70 void *memcpy_far(void *far_d1, const void *far_s1, size_t len);
71 void *memmove(void *d, const void *s, size_t len);
72
73 struct bregs;
74 inline void call16(struct bregs *callregs);
75 inline void call16big(struct bregs *callregs);
76 inline void __call16_int(struct bregs *callregs, u16 offset);
77 #define call16_int(nr, callregs) do {                           \
78         extern void irq_trampoline_ ##nr ();                    \
79         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
80     } while (0)
81
82 // output.c
83 void debug_serial_setup();
84 void BX_PANIC(const char *fmt, ...)
85     __attribute__ ((format (printf, 1, 2)))
86     __attribute__ ((noreturn));
87 void printf(const char *fmt, ...)
88     __attribute__ ((format (printf, 1, 2)));
89 void __dprintf(const char *fmt, ...)
90     __attribute__ ((format (printf, 1, 2)));
91 #define dprintf(lvl, fmt, args...) do {                         \
92         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
93             __dprintf((fmt) , ##args );                         \
94     } while (0)
95 void __debug_enter(const char *fname, struct bregs *regs);
96 void __debug_stub(const char *fname, int lineno, struct bregs *regs);
97 void __debug_isr(const char *fname);
98 #define debug_enter(regs, lvl) do {                     \
99         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
100             __debug_enter(__func__, (regs));            \
101     } while (0)
102 #define debug_isr(lvl) do {                             \
103         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
104             __debug_isr(__func__);                      \
105     } while (0)
106 #define debug_stub(regs) \
107     __debug_stub(__func__, __LINE__, (regs))
108
109 // kbd.c
110 void kbd_setup();
111 void handle_15c2(struct bregs *regs);
112
113 // mouse.c
114 void mouse_setup();
115
116 // system.c
117 extern u32 RamSize;
118 extern u64 RamSizeOver4G;
119 void mathcp_setup();
120
121 // serial.c
122 void serial_setup();
123 void lpt_setup();
124
125 // clock.c
126 void timer_setup();
127 void ndelay(u32 count);
128 void udelay(u32 count);
129 void mdelay(u32 count);
130 u64 calc_future_tsc(u32 msecs);
131 void handle_1583(struct bregs *regs);
132 void handle_1586(struct bregs *regs);
133
134 // apm.c
135 void VISIBLE16 handle_1553(struct bregs *regs);
136
137 // pcibios.c
138 void handle_1ab1(struct bregs *regs);
139
140 // util.c
141 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
142 u8 checksum(u8 *far_data, u32 len);
143
144 // shadow.c
145 void make_bios_writable();
146 void make_bios_readonly();
147
148 // pciinit.c
149 void pci_bios_setup(void);
150
151 // smm.c
152 void smm_init();
153
154 // smpdetect.c
155 int smp_probe(void);
156 void smp_probe_setup(void);
157
158 // mptable.c
159 void mptable_init(void);
160
161 // smbios.c
162 void smbios_init(void);
163
164 // coreboot.c
165 void coreboot_fill_map();
166
167 // vgahooks.c
168 void handle_155f();
169
170 // optionroms.c
171 void vga_setup();
172 void optionrom_setup();
173
174 // resume.c
175 void init_dma();
176
177 // pnpbios.c
178 #define PNP_SIGNATURE 0x506e5024 // $PnP
179 u16 get_pnp_offset();
180 void pnp_setup();
181
182 // romlayout.S
183 void reset_vector() __attribute__ ((noreturn));
184
185 #endif // util.h