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