Implement usleep using real time clock.
[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 void *memset(void *s, int c, size_t n);
51 void *memcpy(void *d1, const void *s1, size_t len);
52
53 static inline void
54 eoi_master_pic()
55 {
56     outb(PIC1_IRQ5, PORT_PIC1);
57 }
58
59 static inline void
60 eoi_both_pics()
61 {
62     outb(PIC2_IRQ13, PORT_PIC2);
63     eoi_master_pic();
64 }
65
66 // Call a function with a specified register state.  Note that on
67 // return, the interrupt enable/disable flag may be altered.
68 static inline
69 void call16(struct bregs *callregs)
70 {
71     asm volatile(
72 #ifdef MODE16
73         "calll __call16\n"
74 #else
75         "calll __call16_from32\n"
76 #endif
77         : "+a" (callregs), "+m" (*callregs)
78         :
79         : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc");
80 }
81
82 static inline
83 void __call16_int(struct bregs *callregs, u16 offset)
84 {
85     callregs->cs = SEG_BIOS;
86     callregs->ip = offset;
87     call16(callregs);
88 }
89
90 #ifdef MODE16
91 #define call16_int(nr, callregs) do {                           \
92         extern void irq_trampoline_ ##nr ();                    \
93         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
94     } while (0)
95 #else
96 #include "../out/rom16.offset.auto.h"
97 #define call16_int(nr, callregs)                                \
98     __call16_int((callregs), OFFSET_irq_trampoline_ ##nr )
99 #endif
100
101 // output.c
102 void BX_PANIC(const char *fmt, ...)
103     __attribute__ ((format (printf, 1, 2)))
104     __attribute__ ((noreturn));
105 void BX_INFO(const char *fmt, ...)
106     __attribute__ ((format (printf, 1, 2)));
107 void printf(const char *fmt, ...)
108     __attribute__ ((format (printf, 1, 2)));
109 void __debug_enter(const char *fname, struct bregs *regs);
110 void __debug_fail(const char *fname, struct bregs *regs);
111 void __debug_stub(const char *fname, struct bregs *regs);
112 void __debug_isr(const char *fname);
113 #define debug_enter(regs) \
114     __debug_enter(__func__, regs)
115 #define debug_stub(regs) \
116     __debug_stub(__func__, regs)
117 #define debug_isr(regs) \
118     __debug_isr(__func__)
119
120 // Frequently used return codes
121 #define RET_EUNSUPPORTED 0x86
122 static inline void
123 set_success(struct bregs *regs)
124 {
125     set_cf(regs, 0);
126 }
127
128 static inline void
129 set_code_success(struct bregs *regs)
130 {
131     regs->ah = 0;
132     set_cf(regs, 0);
133 }
134
135 void __set_fail(const char *fname, struct bregs *regs);
136 void __set_code_fail(const char *fname, struct bregs *regs, u8 code);
137
138 #define set_fail(regs) \
139     __set_fail(__func__, (regs))
140 #define set_code_fail(regs, code)               \
141     __set_code_fail(__func__, (regs), (code))
142
143 // kbd.c
144 void handle_15c2(struct bregs *regs);
145
146 // serial.c
147 void serial_setup();
148 void debug_serial_setup();
149 void debug_serial(char c);
150 void lpt_setup();
151
152 // clock.c
153 void timer_setup();
154 int usleep(u32 count);
155 void handle_1583(struct bregs *regs);
156 void handle_1586(struct bregs *regs);
157
158 // apm.c
159 void VISIBLE16 handle_1553(struct bregs *regs);
160
161 // pcibios.c
162 void handle_1ab1(struct bregs *regs);
163
164 // util.c
165 u8 checksum(u8 *far_data, u32 len);
166
167 // rombios32.c
168 void rombios32_init(void);
169
170 // boot.c
171 void printf_bootdev(u16 bootdev);
172
173 // post_menu.c
174 void interactive_bootmenu();
175
176 #endif // util.h