Cleanup handling of interrupt controller (PIC).
[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 "ioport.h" // outb
10 #include "biosvar.h" // struct bregs
11
12 static inline void irq_disable(void)
13 {
14     asm volatile("cli": : :"memory");
15 }
16
17 static inline void irq_enable(void)
18 {
19     asm volatile("sti": : :"memory");
20 }
21
22 static inline unsigned long irq_save(void)
23 {
24     unsigned long flags;
25     asm volatile("pushfl ; popl %0" : "=g" (flags));
26     irq_disable();
27     return flags;
28 }
29
30 static inline void irq_restore(unsigned long flags)
31 {
32     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
33 }
34
35 static inline void cpu_relax(void)
36 {
37     asm volatile("rep ; nop": : :"memory");
38 }
39
40 static inline void nop(void)
41 {
42     asm volatile("nop");
43 }
44
45 static inline void hlt(void)
46 {
47     asm volatile("hlt");
48 }
49
50 static inline void wbinvd(void)
51 {
52     asm volatile("wbinvd");
53 }
54
55 void *memset(void *s, int c, size_t n);
56 void *memcpy(void *d1, const void *s1, size_t len);
57 void *memmove(void *d, const void *s, size_t len);
58
59 // Call a function with a specified register state.  Note that on
60 // return, the interrupt enable/disable flag may be altered.
61 static inline
62 void call16(struct bregs *callregs)
63 {
64     asm volatile(
65 #ifdef MODE16
66         "calll __call16\n"
67 #else
68         "calll __call16_from32\n"
69 #endif
70         : "+a" (callregs), "+m" (*callregs)
71         :
72         : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc");
73 }
74
75 static inline
76 void __call16_int(struct bregs *callregs, u16 offset)
77 {
78     callregs->cs = SEG_BIOS;
79     callregs->ip = offset;
80     call16(callregs);
81 }
82
83 #ifdef MODE16
84 #define call16_int(nr, callregs) do {                           \
85         extern void irq_trampoline_ ##nr ();                    \
86         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
87     } while (0)
88 #else
89 #include "../out/rom16.offset.auto.h"
90 #define call16_int(nr, callregs)                                \
91     __call16_int((callregs), OFFSET_irq_trampoline_ ##nr )
92 #endif
93
94 // output.c
95 void BX_PANIC(const char *fmt, ...)
96     __attribute__ ((format (printf, 1, 2)))
97     __attribute__ ((noreturn));
98 void printf(const char *fmt, ...)
99     __attribute__ ((format (printf, 1, 2)));
100 void __dprintf(const char *fmt, ...)
101     __attribute__ ((format (printf, 1, 2)));
102 #define dprintf(lvl, fmt, args...) do {                         \
103         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
104             __dprintf((fmt) , ##args );                         \
105     } while (0)
106 void __debug_enter(const char *fname, struct bregs *regs);
107 void __debug_fail(const char *fname, struct bregs *regs);
108 void __debug_stub(const char *fname, struct bregs *regs);
109 void __debug_isr(const char *fname);
110 #define debug_enter(regs, lvl) do {                     \
111         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
112             __debug_enter(__func__, regs);              \
113     } while (0)
114 #define debug_isr(lvl) do {                             \
115         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
116             __debug_isr(__func__);                      \
117     } while (0)
118 #define debug_stub(regs) \
119     __debug_stub(__func__, regs)
120
121 // Frequently used return codes
122 #define RET_EUNSUPPORTED 0x86
123 static inline void
124 set_success(struct bregs *regs)
125 {
126     set_cf(regs, 0);
127 }
128
129 static inline void
130 set_code_success(struct bregs *regs)
131 {
132     regs->ah = 0;
133     set_cf(regs, 0);
134 }
135
136 static inline void
137 set_fail_silent(struct bregs *regs)
138 {
139     set_cf(regs, 1);
140 }
141
142 static inline void
143 set_code_fail_silent(struct bregs *regs, u8 code)
144 {
145     regs->ah = code;
146     set_cf(regs, 1);
147 }
148
149 void __set_fail(const char *fname, struct bregs *regs);
150 void __set_code_fail(const char *fname, struct bregs *regs, u8 code);
151
152 #define set_fail(regs) \
153     __set_fail(__func__, (regs))
154 #define set_code_fail(regs, code)               \
155     __set_code_fail(__func__, (regs), (code))
156
157 // kbd.c
158 void handle_15c2(struct bregs *regs);
159
160 // mouse.c
161 void mouse_setup();
162
163 // system.c
164 void mathcp_setup();
165
166 // serial.c
167 void serial_setup();
168 void lpt_setup();
169
170 // clock.c
171 void timer_setup();
172 int usleep(u32 count);
173 void handle_1583(struct bregs *regs);
174 void handle_1586(struct bregs *regs);
175
176 // apm.c
177 void VISIBLE16 handle_1553(struct bregs *regs);
178
179 // pcibios.c
180 void handle_1ab1(struct bregs *regs);
181
182 // util.c
183 u8 checksum(u8 *far_data, u32 len);
184
185 // shadow.c
186 void make_bios_writable();
187 void make_bios_readonly();
188
189 // rombios32.c
190 void rombios32_init(void);
191
192 // boot.c
193 void printf_bootdev(u16 bootdev);
194
195 // post_menu.c
196 void interactive_bootmenu();
197
198 // coreboot.c
199 void coreboot_fill_map();
200
201 #endif // util.h