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