Init serial port before using it for debug - also reinit after option rom.
[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 debug_serial_setup();
96 void BX_PANIC(const char *fmt, ...)
97     __attribute__ ((format (printf, 1, 2)))
98     __attribute__ ((noreturn));
99 void printf(const char *fmt, ...)
100     __attribute__ ((format (printf, 1, 2)));
101 void __dprintf(const char *fmt, ...)
102     __attribute__ ((format (printf, 1, 2)));
103 #define dprintf(lvl, fmt, args...) do {                         \
104         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
105             __dprintf((fmt) , ##args );                         \
106     } while (0)
107 void __debug_enter(const char *fname, struct bregs *regs);
108 void __debug_fail(const char *fname, struct bregs *regs);
109 void __debug_stub(const char *fname, struct bregs *regs);
110 void __debug_isr(const char *fname);
111 #define debug_enter(regs, lvl) do {                     \
112         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
113             __debug_enter(__func__, regs);              \
114     } while (0)
115 #define debug_isr(lvl) do {                             \
116         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
117             __debug_isr(__func__);                      \
118     } while (0)
119 #define debug_stub(regs) \
120     __debug_stub(__func__, regs)
121
122 // Frequently used return codes
123 #define RET_EUNSUPPORTED 0x86
124 static inline void
125 set_success(struct bregs *regs)
126 {
127     set_cf(regs, 0);
128 }
129
130 static inline void
131 set_code_success(struct bregs *regs)
132 {
133     regs->ah = 0;
134     set_cf(regs, 0);
135 }
136
137 static inline void
138 set_fail_silent(struct bregs *regs)
139 {
140     set_cf(regs, 1);
141 }
142
143 static inline void
144 set_code_fail_silent(struct bregs *regs, u8 code)
145 {
146     regs->ah = code;
147     set_cf(regs, 1);
148 }
149
150 void __set_fail(const char *fname, struct bregs *regs);
151 void __set_code_fail(const char *fname, struct bregs *regs, u8 code);
152
153 #define set_fail(regs) \
154     __set_fail(__func__, (regs))
155 #define set_code_fail(regs, code)               \
156     __set_code_fail(__func__, (regs), (code))
157
158 // kbd.c
159 void handle_15c2(struct bregs *regs);
160
161 // mouse.c
162 void mouse_setup();
163
164 // system.c
165 void mathcp_setup();
166
167 // serial.c
168 void serial_setup();
169 void lpt_setup();
170
171 // clock.c
172 void timer_setup();
173 int usleep(u32 count);
174 void handle_1583(struct bregs *regs);
175 void handle_1586(struct bregs *regs);
176
177 // apm.c
178 void VISIBLE16 handle_1553(struct bregs *regs);
179
180 // pcibios.c
181 void handle_1ab1(struct bregs *regs);
182
183 // util.c
184 u8 checksum(u8 *far_data, u32 len);
185
186 // shadow.c
187 void make_bios_writable();
188 void make_bios_readonly();
189
190 // rombios32.c
191 void rombios32_init(void);
192
193 // boot.c
194 void printf_bootdev(u16 bootdev);
195
196 // post_menu.c
197 void interactive_bootmenu();
198
199 // coreboot.c
200 void coreboot_fill_map();
201
202 #endif // util.h