blob: 7902e83aede4b0db071882b63cea1d536044d21e [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// Basic x86 asm functions and function defs.
2//
Kevin O'Connor5b199ac2009-05-06 23:23:01 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05005// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor786502d2008-02-27 10:41:41 -05006#ifndef __UTIL_H
7#define __UTIL_H
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "types.h" // u32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050010
Kevin O'Connor786502d2008-02-27 10:41:41 -050011static inline void irq_disable(void)
12{
13 asm volatile("cli": : :"memory");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050014}
15
Kevin O'Connor786502d2008-02-27 10:41:41 -050016static inline void irq_enable(void)
17{
18 asm volatile("sti": : :"memory");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050019}
20
21static inline unsigned long irq_save(void)
22{
23 unsigned long flags;
Kevin O'Connor9c447c32010-05-23 10:24:22 -040024 asm volatile("pushfl ; popl %0" : "=g" (flags): :"memory");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050025 irq_disable();
26 return flags;
27}
28
29static inline void irq_restore(unsigned long flags)
30{
31 asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
32}
33
Kevin O'Connor06ad44e2008-04-05 19:30:02 -040034static inline void cpu_relax(void)
35{
36 asm volatile("rep ; nop": : :"memory");
37}
38
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050039static inline void nop(void)
40{
41 asm volatile("nop");
42}
43
Kevin O'Connor95b2f782008-03-05 19:52:06 -050044static inline void hlt(void)
45{
Kevin O'Connor9c447c32010-05-23 10:24:22 -040046 asm volatile("hlt": : :"memory");
Kevin O'Connor95b2f782008-03-05 19:52:06 -050047}
48
Kevin O'Connorda4a6482008-06-08 13:48:06 -040049static inline void wbinvd(void)
50{
Kevin O'Connor9c447c32010-05-23 10:24:22 -040051 asm volatile("wbinvd": : :"memory");
Kevin O'Connorda4a6482008-06-08 13:48:06 -040052}
53
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040054#define CPUID_MSR (1 << 5)
55#define CPUID_APIC (1 << 9)
56#define CPUID_MTRR (1 << 12)
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040057static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx)
58{
59 asm("cpuid"
60 : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx)
61 : "0" (index));
62}
63
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040064static inline u64 rdmsr(u32 index)
65{
66 u64 ret;
67 asm ("rdmsr" : "=A"(ret) : "c"(index));
68 return ret;
69}
70
71static inline void wrmsr(u32 index, u64 val)
72{
73 asm volatile ("wrmsr" : : "c"(index), "A"(val));
74}
75
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -050076static inline u64 rdtscll(void)
77{
78 u64 val;
79 asm volatile("rdtsc" : "=A" (val));
80 return val;
81}
82
Kevin O'Connor0bf92702009-08-01 11:45:37 -040083static inline u32 __ffs(u32 word)
84{
85 asm("bsf %1,%0"
86 : "=r" (word)
87 : "rm" (word));
88 return word;
89}
Kevin O'Connor86916ce2009-11-14 13:34:27 -050090static inline u32 __fls(u32 word)
91{
92 asm("bsr %1,%0"
93 : "=r" (word)
94 : "rm" (word));
95 return word;
96}
Kevin O'Connor0bf92702009-08-01 11:45:37 -040097
Kevin O'Connor68caaa72010-02-15 10:46:37 -050098static inline u16 __htons_constant(u16 val) {
99 return (val<<8) | (val>>8);
100}
101static inline u32 __htonl_constant(u32 val) {
102 return (val<<24) | ((val&0xff00)<<8) | ((val&0xff0000)>>8) | (val>>24);
103}
104static inline u32 __htonl(u32 val) {
105 asm("bswapl %0" : "+r"(val));
106 return val;
107}
108#define htonl(x) (__builtin_constant_p((u32)(x)) ? __htonl_constant(x) : __htonl(x))
109#define ntohl(x) htonl(x)
110#define htons(x) __htons_constant(x)
111#define ntohs(x) htons(x)
112
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500113static inline u32 getesp(void) {
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -0500114 u32 esp;
115 asm("movl %%esp, %0" : "=rm"(esp));
116 return esp;
117}
118
Kevin O'Connor91031ed2009-10-12 09:49:27 -0400119static inline void writel(void *addr, u32 val) {
120 *(volatile u32 *)addr = val;
121}
122static inline void writew(void *addr, u16 val) {
123 *(volatile u16 *)addr = val;
124}
125static inline void writeb(void *addr, u8 val) {
126 *(volatile u8 *)addr = val;
127}
128static inline u32 readl(const void *addr) {
129 return *(volatile const u32 *)addr;
130}
131static inline u16 readw(const void *addr) {
132 return *(volatile const u16 *)addr;
133}
134static inline u8 readb(const void *addr) {
135 return *(volatile const u8 *)addr;
136}
137
Kevin O'Connor942d4952009-06-10 22:44:06 -0400138#define call16_simpint(nr, peax, pflags) do { \
Kevin O'Connor0b60a062009-06-15 23:03:05 -0400139 ASSERT16(); \
Kevin O'Connor942d4952009-06-10 22:44:06 -0400140 asm volatile( \
Kevin O'Connor4ed378a2010-03-13 22:29:55 -0500141 "pushl %%ebp\n" \
142 "sti\n" \
Kevin O'Connor942d4952009-06-10 22:44:06 -0400143 "stc\n" \
144 "int %2\n" \
145 "pushfl\n" \
146 "popl %1\n" \
147 "cli\n" \
Kevin O'Connor4ed378a2010-03-13 22:29:55 -0500148 "cld\n" \
149 "popl %%ebp" \
150 : "+a"(*peax), "=c"(*pflags) \
Kevin O'Connor942d4952009-06-10 22:44:06 -0400151 : "i"(nr) \
Kevin O'Connor4ed378a2010-03-13 22:29:55 -0500152 : "ebx", "edx", "esi", "edi", "cc", "memory"); \
Kevin O'Connor942d4952009-06-10 22:44:06 -0400153 } while (0)
154
Kevin O'Connorad901592009-12-13 11:25:25 -0500155// GDT bit manipulation
156#define GDT_BASE(v) ((((u64)(v) & 0xff000000) << 32) \
157 | (((u64)(v) & 0x00ffffff) << 16))
158#define GDT_LIMIT(v) ((((u64)(v) & 0x000f0000) << 32) \
159 | (((u64)(v) & 0x0000ffff) << 0))
160#define GDT_CODE (0x9bULL << 40) // Code segment - P,R,A bits also set
161#define GDT_DATA (0x93ULL << 40) // Data segment - W,A bits also set
162#define GDT_B (0x1ULL << 54) // Big flag
163#define GDT_G (0x1ULL << 55) // Granularity flag
164
165struct descloc_s {
166 u16 length;
167 u32 addr;
168} PACKED;
169
Kevin O'Connora83ff552009-01-01 21:00:59 -0500170// util.c
Kevin O'Connor9521e262008-07-04 13:04:29 -0400171struct bregs;
172inline void call16(struct bregs *callregs);
Kevin O'Connor6e5b4a42008-12-06 13:03:52 -0500173inline void call16big(struct bregs *callregs);
Kevin O'Connor9521e262008-07-04 13:04:29 -0400174inline void __call16_int(struct bregs *callregs, u16 offset);
Kevin O'Connor3a47a312008-03-01 14:46:37 -0500175#define call16_int(nr, callregs) do { \
176 extern void irq_trampoline_ ##nr (); \
Kevin O'Connor117fc212008-04-13 18:17:02 -0400177 __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500178 } while (0)
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -0500179u8 checksum_far(u16 buf_seg, void *buf_far, u32 len);
180u8 checksum(void *buf, u32 len);
181size_t strlen(const char *s);
182int memcmp(const void *s1, const void *s2, size_t n);
183int strcmp(const char *s1, const char *s2);
184inline void memset_far(u16 d_seg, void *d_far, u8 c, size_t len);
185inline void memset16_far(u16 d_seg, void *d_far, u16 c, size_t len);
186void *memset(void *s, int c, size_t n);
187inline void memcpy_far(u16 d_seg, void *d_far
188 , u16 s_seg, const void *s_far, size_t len);
189void *memcpy(void *d1, const void *s1, size_t len);
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500190#if MODESEGMENT == 0
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -0500191#define memcpy __builtin_memcpy
192#endif
193void iomemcpy(void *d, const void *s, u32 len);
194void *memmove(void *d, const void *s, size_t len);
195char *strtcpy(char *dest, const char *src, size_t len);
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400196void biosusleep(u32 usec);
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500197int get_keystroke(int msec);
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500198
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -0500199// stacks.c
Kevin O'Connorbca07362010-03-20 20:41:38 -0400200inline u32 stack_hop(u32 eax, u32 edx, void *func);
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -0500201extern struct thread_info MainThread;
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500202void thread_setup(void);
203struct thread_info *getCurThread(void);
204void yield(void);
Kevin O'Connor9c447c32010-05-23 10:24:22 -0400205void wait_irq(void);
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -0500206void run_thread(void (*func)(void*), void *data);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500207void wait_threads(void);
Kevin O'Connore9086652010-02-27 13:49:47 -0500208struct mutex_s { u32 isLocked; };
209void mutex_lock(struct mutex_s *mutex);
210void mutex_unlock(struct mutex_s *mutex);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500211void start_preempt(void);
212void finish_preempt(void);
Kevin O'Connord7eb27e2010-03-20 18:17:19 -0400213int wait_preempt(void);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500214void check_preempt(void);
Kevin O'Connor7cefbfa2009-12-10 21:35:49 -0500215
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500216// output.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500217void debug_serial_setup(void);
Kevin O'Connore07e18e2009-02-08 17:07:29 -0500218void panic(const char *fmt, ...)
Kevin O'Connordad41d92010-01-01 03:04:19 -0500219 __attribute__ ((format (printf, 1, 2))) __noreturn;
Kevin O'Connor567e4e32008-04-05 11:37:51 -0400220void printf(const char *fmt, ...)
221 __attribute__ ((format (printf, 1, 2)));
Kevin O'Connor2be312c2009-11-24 09:37:53 -0500222int snprintf(char *str, size_t size, const char *fmt, ...)
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400223 __attribute__ ((format (printf, 3, 4)));
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500224void __dprintf(const char *fmt, ...)
225 __attribute__ ((format (printf, 1, 2)));
226void __debug_enter(struct bregs *regs, const char *fname);
227void __debug_isr(const char *fname);
228void __debug_stub(struct bregs *regs, int lineno, const char *fname);
229void __warn_invalid(struct bregs *regs, int lineno, const char *fname);
230void __warn_unimplemented(struct bregs *regs, int lineno, const char *fname);
Kevin O'Connor7fb8ba82010-02-26 08:45:00 -0500231void __warn_internalerror(int lineno, const char *fname);
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500232void __warn_noalloc(int lineno, const char *fname);
233void __warn_timeout(int lineno, const char *fname);
234void __set_invalid(struct bregs *regs, int lineno, const char *fname);
235void __set_unimplemented(struct bregs *regs, int lineno, const char *fname);
236void __set_code_invalid(struct bregs *regs, u32 linecode, const char *fname);
237void __set_code_unimplemented(struct bregs *regs, u32 linecode
238 , const char *fname);
239void hexdump(const void *d, int len);
240
Kevin O'Connorac8df8c2008-05-24 23:46:33 -0400241#define dprintf(lvl, fmt, args...) do { \
242 if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL) \
243 __dprintf((fmt) , ##args ); \
244 } while (0)
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400245#define debug_enter(regs, lvl) do { \
246 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
Kevin O'Connor05600342009-01-02 13:10:58 -0500247 __debug_enter((regs), __func__); \
Kevin O'Connor15c1f222008-06-12 22:59:43 -0400248 } while (0)
249#define debug_isr(lvl) do { \
250 if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \
251 __debug_isr(__func__); \
252 } while (0)
Kevin O'Connor05600342009-01-02 13:10:58 -0500253#define debug_stub(regs) \
254 __debug_stub((regs), __LINE__, __func__)
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500255#define warn_invalid(regs) \
256 __warn_invalid((regs), __LINE__, __func__)
257#define warn_unimplemented(regs) \
258 __warn_unimplemented((regs), __LINE__, __func__)
Kevin O'Connor7fb8ba82010-02-26 08:45:00 -0500259#define warn_internalerror() \
260 __warn_internalerror(__LINE__, __func__)
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500261#define warn_noalloc() \
262 __warn_noalloc(__LINE__, __func__)
263#define warn_timeout() \
264 __warn_timeout(__LINE__, __func__)
265#define set_invalid(regs) \
266 __set_invalid((regs), __LINE__, __func__)
267#define set_code_invalid(regs, code) \
268 __set_code_invalid((regs), (code) | (__LINE__ << 8), __func__)
269#define set_unimplemented(regs) \
270 __set_unimplemented((regs), __LINE__, __func__)
271#define set_code_unimplemented(regs, code) \
272 __set_code_unimplemented((regs), (code) | (__LINE__ << 8), __func__)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500273
274// kbd.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500275void kbd_setup(void);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500276void handle_15c2(struct bregs *regs);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400277void process_key(u8 key);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500278
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400279// mouse.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500280void mouse_setup(void);
Kevin O'Connor57877482009-12-09 21:00:41 -0500281void process_mouse(u8 data);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400282
283// system.c
Kevin O'Connore7916362008-12-28 22:03:17 -0500284extern u32 RamSize;
285extern u64 RamSizeOver4G;
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500286void mathcp_setup(void);
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400287
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400288// serial.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500289void serial_setup(void);
290void lpt_setup(void);
Kevin O'Connor913cc2e2008-04-13 17:31:45 -0400291
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500292// clock.c
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500293#define PIT_TICK_RATE 1193180 // Underlying HZ of PIT
294#define PIT_TICK_INTERVAL 65536 // Default interval for 18.2Hz timer
Kevin O'Connor144817b2010-05-23 10:46:49 -0400295static inline int check_tsc(u64 end) {
Kevin O'Connor89eb6242009-10-22 22:30:37 -0400296 return (s64)(rdtscll() - end) > 0;
297}
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500298void timer_setup(void);
Kevin O'Connorbc2aecd2008-11-28 16:40:06 -0500299void ndelay(u32 count);
300void udelay(u32 count);
301void mdelay(u32 count);
Kevin O'Connor10ad7992009-10-24 11:06:08 -0400302void nsleep(u32 count);
303void usleep(u32 count);
304void msleep(u32 count);
Kevin O'Connor4e6c9702008-12-13 10:45:50 -0500305u64 calc_future_tsc(u32 msecs);
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400306u64 calc_future_tsc_usec(u32 usecs);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500307void handle_1583(struct bregs *regs);
Kevin O'Connor5be04902008-05-18 17:12:06 -0400308void handle_1586(struct bregs *regs);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500309void useRTC(void);
310void releaseRTC(void);
Kevin O'Connorbdce35f2008-02-26 21:33:14 -0500311
Kevin O'Connor95b2f782008-03-05 19:52:06 -0500312// apm.c
Kevin O'Connorc0031482010-01-01 13:03:17 -0500313void handle_1553(struct bregs *regs);
Kevin O'Connor95b2f782008-03-05 19:52:06 -0500314
Kevin O'Connora0dc2962008-03-16 14:29:32 -0400315// pcibios.c
316void handle_1ab1(struct bregs *regs);
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500317void bios32_setup(void);
Kevin O'Connora0dc2962008-03-16 14:29:32 -0400318
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400319// shadow.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500320void make_bios_writable(void);
321void make_bios_readonly(void);
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400322
Kevin O'Connor0525d292008-07-04 06:18:30 -0400323// pciinit.c
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400324void pci_setup(void);
Kevin O'Connora4d35762008-03-08 15:43:03 -0500325
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400326// smm.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500327void smm_init(void);
Kevin O'Connorf7ba6d72008-07-04 05:05:54 -0400328
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400329// smp.c
Kevin O'Connor603bfc32009-06-22 20:04:56 -0400330extern u32 CountCPUs;
Kevin O'Connor84705852009-10-08 22:13:15 -0400331extern u32 MaxCountCPUs;
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400332void wrmsr_smp(u32 index, u64 val);
333void smp_probe(void);
Kevin O'Connoracf13742008-11-29 11:19:19 -0500334void smp_probe_setup(void);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400335
Kevin O'Connorc7812932008-06-08 23:08:12 -0400336// coreboot.c
Kevin O'Connor1edc89d2009-04-30 21:50:35 -0400337struct cbfs_file;
Kevin O'Connor1f836252009-08-16 20:17:35 -0400338struct cbfs_file *cbfs_findprefix(const char *prefix, struct cbfs_file *last);
Kevin O'Connor00882592009-08-18 22:21:10 -0400339u32 cbfs_datasize(struct cbfs_file *file);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400340const char *cbfs_filename(struct cbfs_file *file);
Kevin O'Connor00882592009-08-18 22:21:10 -0400341int cbfs_copyfile(struct cbfs_file *file, void *dst, u32 maxlen);
Kevin O'Connor1f836252009-08-16 20:17:35 -0400342int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev);
343void cbfs_run_payload(struct cbfs_file *file);
344
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500345void coreboot_copy_biostable(void);
346void coreboot_setup(void);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400347
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400348// vgahooks.c
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400349extern int VGAbdf;
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500350void handle_155f(struct bregs *regs);
Kevin O'Connor22e1b912009-07-19 18:52:46 -0400351void vgahook_setup(const char *vendor, const char *part);
Kevin O'Connorcbffa8e2008-08-17 11:11:07 -0400352
Kevin O'Connor714325c2008-11-01 20:32:27 -0400353// optionroms.c
Kevin O'Connor0a924122009-02-08 19:43:47 -0500354void call_bcv(u16 seg, u16 ip);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500355void optionrom_setup(void);
356void vga_setup(void);
357void s3_resume_vga_init(void);
Kevin O'Connore7739302009-07-26 19:16:09 -0400358extern u32 RomEnd;
Kevin O'Connor714325c2008-11-01 20:32:27 -0400359
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500360// resume.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500361void init_dma(void);
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500362
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500363// pnpbios.c
364#define PNP_SIGNATURE 0x506e5024 // $PnP
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500365u16 get_pnp_offset(void);
366void pnp_setup(void);
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500367
Kevin O'Connore54ee382009-07-26 19:33:13 -0400368// pmm.c
Kevin O'Connor415d4292009-08-30 19:19:31 -0400369extern struct zone_s ZoneLow, ZoneHigh, ZoneFSeg, ZoneTmpLow, ZoneTmpHigh;
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500370void malloc_setup(void);
371void malloc_finalize(void);
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400372void *pmm_malloc(struct zone_s *zone, u32 handle, u32 size, u32 align);
373int pmm_free(void *data);
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500374void pmm_setup(void);
375void pmm_finalize(void);
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400376#define PMM_DEFAULT_HANDLE 0xFFFFFFFF
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400377// Minimum alignment of malloc'd memory
378#define MALLOC_MIN_ALIGN 16
Kevin O'Connor415d4292009-08-30 19:19:31 -0400379// Helper functions for memory allocation.
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400380static inline void *malloc_low(u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400381 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400382}
Kevin O'Connor415d4292009-08-30 19:19:31 -0400383static inline void *malloc_high(u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400384 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
Kevin O'Connor415d4292009-08-30 19:19:31 -0400385}
386static inline void *malloc_fseg(u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400387 return pmm_malloc(&ZoneFSeg, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
Kevin O'Connor415d4292009-08-30 19:19:31 -0400388}
Kevin O'Connor114592f2009-09-28 21:32:08 -0400389static inline void *malloc_tmphigh(u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400390 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400391}
Kevin O'Connor575ffc82010-02-21 23:20:10 -0500392static inline void *malloc_tmp(u32 size) {
393 void *ret = malloc_tmphigh(size);
394 if (ret)
395 return ret;
396 return pmm_malloc(&ZoneTmpLow, PMM_DEFAULT_HANDLE, size, MALLOC_MIN_ALIGN);
397}
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400398static inline void *memalign_low(u32 align, u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400399 return pmm_malloc(&ZoneLow, PMM_DEFAULT_HANDLE, size, align);
Kevin O'Connor415d4292009-08-30 19:19:31 -0400400}
401static inline void *memalign_high(u32 align, u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400402 return pmm_malloc(&ZoneHigh, PMM_DEFAULT_HANDLE, size, align);
Kevin O'Connor415d4292009-08-30 19:19:31 -0400403}
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400404static inline void *memalign_tmphigh(u32 align, u32 size) {
Kevin O'Connord948e2b2009-10-12 12:54:56 -0400405 return pmm_malloc(&ZoneTmpHigh, PMM_DEFAULT_HANDLE, size, align);
406}
407static inline void free(void *data) {
408 pmm_free(data);
Kevin O'Connor9ed6b622009-10-07 21:41:08 -0400409}
Kevin O'Connore54ee382009-07-26 19:33:13 -0400410
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500411// mtrr.c
412void mtrr_setup(void);
413
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500414// romlayout.S
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500415void reset_vector(void) __noreturn;
Kevin O'Connor18e38b22008-12-10 20:40:13 -0500416
Kevin O'Connor30853762009-01-17 18:49:20 -0500417// misc.c
418extern u8 BiosChecksum;
419
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400420// version (auto generated file out/version.c)
421extern const char VERSION[];
422
Kevin O'Connor786502d2008-02-27 10:41:41 -0500423#endif // util.h