Introduce optimized iomemcpy function for copying from io memory.
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
2 //
3 // Copyright (C) 2008,2009  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 // Atomically enable irqs and sleep until an irq; then re-disable irqs.
40 static inline void wait_irq(void)
41 {
42     asm volatile("sti ; hlt ; cli ; cld": : :"memory");
43 }
44
45 static inline void nop(void)
46 {
47     asm volatile("nop");
48 }
49
50 static inline void hlt(void)
51 {
52     asm volatile("hlt");
53 }
54
55 static inline void wbinvd(void)
56 {
57     asm volatile("wbinvd");
58 }
59
60 #define CPUID_MSR (1 << 5)
61 #define CPUID_APIC (1 << 9)
62 #define CPUID_MTRR (1 << 12)
63 static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
64 {
65     asm("cpuid"
66         : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
67         : "0" (index));
68 }
69
70 static inline u64 rdmsr(u32 index)
71 {
72     u64 ret;
73     asm ("rdmsr" : "=A"(ret) : "c"(index));
74     return ret;
75 }
76
77 static inline void wrmsr(u32 index, u64 val)
78 {
79     asm volatile ("wrmsr" : : "c"(index), "A"(val));
80 }
81
82 static inline u64 rdtscll(void)
83 {
84     u64 val;
85     asm volatile("rdtsc" : "=A" (val));
86     return val;
87 }
88
89 static inline u32 __ffs(u32 word)
90 {
91     asm("bsf %1,%0"
92         : "=r" (word)
93         : "rm" (word));
94     return word;
95 }
96 static inline u32 __fls(u32 word)
97 {
98     asm("bsr %1,%0"
99         : "=r" (word)
100         : "rm" (word));
101     return word;
102 }
103
104 static inline void writel(void *addr, u32 val) {
105     *(volatile u32 *)addr = val;
106 }
107 static inline void writew(void *addr, u16 val) {
108     *(volatile u16 *)addr = val;
109 }
110 static inline void writeb(void *addr, u8 val) {
111     *(volatile u8 *)addr = val;
112 }
113 static inline u32 readl(const void *addr) {
114     return *(volatile const u32 *)addr;
115 }
116 static inline u16 readw(const void *addr) {
117     return *(volatile const u16 *)addr;
118 }
119 static inline u8 readb(const void *addr) {
120     return *(volatile const u8 *)addr;
121 }
122
123 // GDT bit manipulation
124 #define GDT_BASE(v)  ((((u64)(v) & 0xff000000) << 32)           \
125                       | (((u64)(v) & 0x00ffffff) << 16))
126 #define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32)   \
127                       | (((u64)(v) & 0x0000ffff) << 0))
128 #define GDT_CODE     (0x9bULL << 40) // Code segment - P,R,A bits also set
129 #define GDT_DATA     (0x93ULL << 40) // Data segment - W,A bits also set
130 #define GDT_B        (0x1ULL << 54)  // Big flag
131 #define GDT_G        (0x1ULL << 55)  // Granularity flag
132
133 #define call16_simpint(nr, peax, pflags) do {                           \
134         ASSERT16();                                                     \
135         asm volatile(                                                   \
136             "stc\n"                                                     \
137             "int %2\n"                                                  \
138             "pushfl\n"                                                  \
139             "popl %1\n"                                                 \
140             "cli\n"                                                     \
141             "cld"                                                       \
142             : "+a"(*peax), "=r"(*pflags)                                \
143             : "i"(nr)                                                   \
144             : "cc", "memory");                                          \
145     } while (0)
146
147 // util.c
148 inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func);
149 extern struct thread_info MainThread;
150 struct thread_info *getCurThread();
151 void run_thread(void (*func)(void*), void *data);
152 void wait_threads();
153 u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
154 u8 checksum(void *buf, u32 len);
155 int memcmp(const void *s1, const void *s2, size_t n);
156 size_t strlen(const char *s);
157 int strcmp(const char *s1, const char *s2);
158 inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
159 inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
160 void *memset(void *s, int c, size_t n);
161 void *memcpy(void *d1, const void *s1, size_t len);
162 #if MODE16 == 0
163 #define memcpy __builtin_memcpy
164 #endif
165 inline void memcpy_far(u16 d_seg, void *d_far
166                        , u16 s_seg, const void *s_far, size_t len);
167 void iomemcpy(void *d, const void *s, u32 len);
168 void *memmove(void *d, const void *s, size_t len);
169 char *strtcpy(char *dest, const char *src, size_t len);
170 struct bregs;
171 inline void call16(struct bregs *callregs);
172 inline void call16big(struct bregs *callregs);
173 inline void __call16_int(struct bregs *callregs, u16 offset);
174 #define call16_int(nr, callregs) do {                           \
175         extern void irq_trampoline_ ##nr ();                    \
176         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
177     } while (0)
178 void yield();
179 void biosusleep(u32 usec);
180 int get_keystroke(int msec);
181
182 // output.c
183 void debug_serial_setup();
184 void panic(const char *fmt, ...)
185     __attribute__ ((format (printf, 1, 2)))
186     __attribute__ ((noreturn));
187 void printf(const char *fmt, ...)
188     __attribute__ ((format (printf, 1, 2)));
189 void __dprintf(const char *fmt, ...)
190     __attribute__ ((format (printf, 1, 2)));
191 int snprintf(char *str, size_t size, const char *fmt, ...)
192     __attribute__ ((format (printf, 3, 4)));
193 #define dprintf(lvl, fmt, args...) do {                         \
194         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
195             __dprintf((fmt) , ##args );                         \
196     } while (0)
197 void __debug_enter(struct bregs *regs, const char *fname);
198 void __debug_stub(struct bregs *regs, int lineno, const char *fname);
199 void __debug_isr(const char *fname);
200 #define debug_enter(regs, lvl) do {                     \
201         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
202             __debug_enter((regs), __func__);            \
203     } while (0)
204 #define debug_isr(lvl) do {                             \
205         if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL)       \
206             __debug_isr(__func__);                      \
207     } while (0)
208 #define debug_stub(regs)                        \
209     __debug_stub((regs), __LINE__, __func__)
210 void hexdump(const void *d, int len);
211
212 // kbd.c
213 void kbd_setup();
214 void handle_15c2(struct bregs *regs);
215 void process_key(u8 key);
216
217 // mouse.c
218 void mouse_setup();
219
220 // system.c
221 extern u32 RamSize;
222 extern u64 RamSizeOver4G;
223 void mathcp_setup();
224
225 // serial.c
226 void serial_setup();
227 void lpt_setup();
228
229 // clock.c
230 static inline int check_time(u64 end) {
231     return (s64)(rdtscll() - end) > 0;
232 }
233 void timer_setup();
234 void ndelay(u32 count);
235 void udelay(u32 count);
236 void mdelay(u32 count);
237 void nsleep(u32 count);
238 void usleep(u32 count);
239 void msleep(u32 count);
240 u64 calc_future_tsc(u32 msecs);
241 u64 calc_future_tsc_usec(u32 usecs);
242 void handle_1583(struct bregs *regs);
243 void handle_1586(struct bregs *regs);
244
245 // apm.c
246 void VISIBLE16 handle_1553(struct bregs *regs);
247
248 // pcibios.c
249 void handle_1ab1(struct bregs *regs);
250
251 // shadow.c
252 void make_bios_writable();
253 void make_bios_readonly();
254
255 // pciinit.c
256 void pci_setup(void);
257
258 // smm.c
259 void smm_init();
260
261 // smp.c
262 extern u32 CountCPUs;
263 extern u32 MaxCountCPUs;
264 void wrmsr_smp(u32 index, u64 val);
265 void smp_probe(void);
266 void smp_probe_setup(void);
267
268 // coreboot.c
269 struct cbfs_file;
270 struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
271 u32 cbfs_datasize(struct cbfs_file *file);
272 const char *cbfs_filename(struct cbfs_file *file);
273 int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
274 int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
275 void cbfs_run_payload(struct cbfs_file *file);
276
277 void coreboot_copy_biostable();
278 void coreboot_setup();
279
280 // vgahooks.c
281 extern int VGAbdf;
282 void handle_155f();
283 void vgahook_setup(const char *vendor, const char *part);
284
285 // optionroms.c
286 void call_bcv(u16 seg, u16 ip);
287 void optionrom_setup();
288 void vga_setup();
289 void s3_resume_vga_init();
290 extern u32 RomEnd;
291
292 // resume.c
293 void init_dma();
294
295 // pnpbios.c
296 #define PNP_SIGNATURE 0x506e5024 // $PnP
297 u16 get_pnp_offset();
298 void pnp_setup();
299
300 // pmm.c
301 extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
302 void malloc_setup();
303 void malloc_finalize();
304 void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
305 int pmm_free(void *data);
306 void pmm_setup();
307 void pmm_finalize();
308 #define PMM_DEFAULT_HANDLE 0xFFFFFFFF
309 // Minimum alignment of malloc'd memory
310 #define MALLOC_MIN_ALIGN 16
311 // Helper functions for memory allocation.
312 static inline void *malloc_low(u32 size) {
313     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
314 }
315 static inline void *malloc_high(u32 size) {
316     return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
317 }
318 static inline void *malloc_fseg(u32 size) {
319     return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
320 }
321 static inline void *malloc_tmphigh(u32 size) {
322     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
323 }
324 static inline void *memalign_low(u32 align, u32 size) {
325     return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
326 }
327 static inline void *memalign_high(u32 align, u32 size) {
328     return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
329 }
330 static inline void *memalign_tmphigh(u32 align, u32 size) {
331     return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
332 }
333 static inline void free(void *data) {
334     pmm_free(data);
335 }
336
337 // mtrr.c
338 void mtrr_setup(void);
339
340 // romlayout.S
341 void reset_vector() __attribute__ ((noreturn));
342
343 // misc.c
344 extern u8 BiosChecksum;
345
346 // version (auto generated file out/version.c)
347 extern const char VERSION[];
348
349 #endif // util.h