Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // Basic x86 asm functions and function defs. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 5 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 6 | #ifndef __UTIL_H |
| 7 | #define __UTIL_H |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 8 | |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 9 | #include "types.h" // u32 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 10 | |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 11 | static inline void irq_disable(void) |
| 12 | { |
| 13 | asm volatile("cli": : :"memory"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 14 | } |
| 15 | |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 16 | static inline void irq_enable(void) |
| 17 | { |
| 18 | asm volatile("sti": : :"memory"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 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 | |
Kevin O'Connor | 06ad44e | 2008-04-05 19:30:02 -0400 | [diff] [blame] | 34 | static inline void cpu_relax(void) |
| 35 | { |
| 36 | asm volatile("rep ; nop": : :"memory"); |
| 37 | } |
| 38 | |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 39 | static inline void nop(void) |
| 40 | { |
| 41 | asm volatile("nop"); |
| 42 | } |
| 43 | |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 44 | static inline void hlt(void) |
| 45 | { |
| 46 | asm volatile("hlt"); |
| 47 | } |
| 48 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 49 | static inline void wbinvd(void) |
| 50 | { |
| 51 | asm volatile("wbinvd"); |
| 52 | } |
| 53 | |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 54 | static inline void cpuid(u32 index, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx) |
| 55 | { |
| 56 | asm("cpuid" |
| 57 | : "=a" (*eax), "=b" (*ebx), "=c" (*ecx), "=d" (*edx) |
| 58 | : "0" (index)); |
| 59 | } |
| 60 | |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 61 | static inline u64 rdtscll(void) |
| 62 | { |
| 63 | u64 val; |
| 64 | asm volatile("rdtsc" : "=A" (val)); |
| 65 | return val; |
| 66 | } |
| 67 | |
Kevin O'Connor | a83ff55 | 2009-01-01 21:00:59 -0500 | [diff] [blame] | 68 | // util.c |
| 69 | inline u32 stack_hop(u32 eax, u32 edx, u32 ecx, void *func); |
Kevin O'Connor | 94fd47e | 2009-02-15 15:21:10 -0500 | [diff] [blame] | 70 | u8 checksum_far(u16 buf_seg, void *buf_far, u32 len); |
| 71 | u8 checksum(void *buf, u32 len); |
Kevin O'Connor | 38d1a34 | 2009-04-18 16:59:47 -0400 | [diff] [blame] | 72 | int memcmp(const void *s1, const void *s2, size_t n); |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 73 | size_t strlen(const char *s); |
Kevin O'Connor | 38d1a34 | 2009-04-18 16:59:47 -0400 | [diff] [blame] | 74 | int strcmp(const char *s1, const char *s2); |
Kevin O'Connor | 5e4235f | 2008-04-12 09:00:04 -0400 | [diff] [blame] | 75 | void *memset(void *s, int c, size_t n); |
Kevin O'Connor | d944114 | 2009-04-19 23:18:54 -0400 | [diff] [blame] | 76 | void memcpy4(void *d1, const void *s1, size_t len); |
| 77 | #define memcpy(d1, s1, len) __builtin_memcpy((d1), (s1), (len)) |
Kevin O'Connor | 8b267cb | 2009-01-19 19:25:21 -0500 | [diff] [blame] | 78 | inline void memcpy_far(u16 d_seg, void *d_far |
| 79 | , u16 s_seg, const void *s_far, size_t len); |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 80 | void *memmove(void *d, const void *s, size_t len); |
Kevin O'Connor | 71f036d | 2009-02-08 16:57:22 -0500 | [diff] [blame] | 81 | char *strtcpy(char *dest, const char *src, size_t len); |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 82 | struct bregs; |
| 83 | inline void call16(struct bregs *callregs); |
Kevin O'Connor | 6e5b4a4 | 2008-12-06 13:03:52 -0500 | [diff] [blame] | 84 | inline void call16big(struct bregs *callregs); |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 85 | inline void __call16_int(struct bregs *callregs, u16 offset); |
Kevin O'Connor | 3a47a31 | 2008-03-01 14:46:37 -0500 | [diff] [blame] | 86 | #define call16_int(nr, callregs) do { \ |
| 87 | extern void irq_trampoline_ ##nr (); \ |
Kevin O'Connor | 117fc21 | 2008-04-13 18:17:02 -0400 | [diff] [blame] | 88 | __call16_int((callregs), (u32)&irq_trampoline_ ##nr ); \ |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 89 | } while (0) |
Kevin O'Connor | a83ff55 | 2009-01-01 21:00:59 -0500 | [diff] [blame] | 90 | inline void call16_simpint(int nr, u32 *eax, u32 *flags); |
Kevin O'Connor | 5ca4b95 | 2009-02-15 13:45:48 -0500 | [diff] [blame] | 91 | void usleep(u32 usec); |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 92 | int get_keystroke(int msec); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 93 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 94 | // output.c |
Kevin O'Connor | 61d6b06 | 2008-06-21 12:15:10 -0400 | [diff] [blame] | 95 | void debug_serial_setup(); |
Kevin O'Connor | e07e18e | 2009-02-08 17:07:29 -0500 | [diff] [blame] | 96 | void panic(const char *fmt, ...) |
Kevin O'Connor | 567e4e3 | 2008-04-05 11:37:51 -0400 | [diff] [blame] | 97 | __attribute__ ((format (printf, 1, 2))) |
| 98 | __attribute__ ((noreturn)); |
Kevin O'Connor | 567e4e3 | 2008-04-05 11:37:51 -0400 | [diff] [blame] | 99 | void printf(const char *fmt, ...) |
| 100 | __attribute__ ((format (printf, 1, 2))); |
Kevin O'Connor | ac8df8c | 2008-05-24 23:46:33 -0400 | [diff] [blame] | 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) |
Kevin O'Connor | 0560034 | 2009-01-02 13:10:58 -0500 | [diff] [blame] | 107 | void __debug_enter(struct bregs *regs, const char *fname); |
| 108 | void __debug_stub(struct bregs *regs, int lineno, const char *fname); |
Kevin O'Connor | ed12849 | 2008-03-11 11:14:59 -0400 | [diff] [blame] | 109 | void __debug_isr(const char *fname); |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 110 | #define debug_enter(regs, lvl) do { \ |
| 111 | if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \ |
Kevin O'Connor | 0560034 | 2009-01-02 13:10:58 -0500 | [diff] [blame] | 112 | __debug_enter((regs), __func__); \ |
Kevin O'Connor | 15c1f22 | 2008-06-12 22:59:43 -0400 | [diff] [blame] | 113 | } while (0) |
| 114 | #define debug_isr(lvl) do { \ |
| 115 | if ((lvl) && (lvl) <= CONFIG_DEBUG_LEVEL) \ |
| 116 | __debug_isr(__func__); \ |
| 117 | } while (0) |
Kevin O'Connor | 0560034 | 2009-01-02 13:10:58 -0500 | [diff] [blame] | 118 | #define debug_stub(regs) \ |
| 119 | __debug_stub((regs), __LINE__, __func__) |
Kevin O'Connor | 1eba429 | 2009-02-17 23:14:25 -0500 | [diff] [blame] | 120 | void hexdump(void *d, int len); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 121 | |
| 122 | // kbd.c |
Kevin O'Connor | efda97d | 2008-07-04 13:10:12 -0400 | [diff] [blame] | 123 | void kbd_setup(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 124 | void handle_15c2(struct bregs *regs); |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 125 | |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 126 | // mouse.c |
| 127 | void mouse_setup(); |
| 128 | |
| 129 | // system.c |
Kevin O'Connor | e791636 | 2008-12-28 22:03:17 -0500 | [diff] [blame] | 130 | extern u32 RamSize; |
| 131 | extern u64 RamSizeOver4G; |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 132 | void mathcp_setup(); |
| 133 | |
Kevin O'Connor | 913cc2e | 2008-04-13 17:31:45 -0400 | [diff] [blame] | 134 | // serial.c |
| 135 | void serial_setup(); |
| 136 | void lpt_setup(); |
| 137 | |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 138 | // clock.c |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 139 | void timer_setup(); |
Kevin O'Connor | bc2aecd | 2008-11-28 16:40:06 -0500 | [diff] [blame] | 140 | void ndelay(u32 count); |
| 141 | void udelay(u32 count); |
| 142 | void mdelay(u32 count); |
Kevin O'Connor | 4e6c970 | 2008-12-13 10:45:50 -0500 | [diff] [blame] | 143 | u64 calc_future_tsc(u32 msecs); |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 144 | void handle_1583(struct bregs *regs); |
Kevin O'Connor | 5be0490 | 2008-05-18 17:12:06 -0400 | [diff] [blame] | 145 | void handle_1586(struct bregs *regs); |
Kevin O'Connor | bdce35f | 2008-02-26 21:33:14 -0500 | [diff] [blame] | 146 | |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 147 | // apm.c |
Kevin O'Connor | 44d6530 | 2008-03-08 11:34:46 -0500 | [diff] [blame] | 148 | void VISIBLE16 handle_1553(struct bregs *regs); |
Kevin O'Connor | 95b2f78 | 2008-03-05 19:52:06 -0500 | [diff] [blame] | 149 | |
Kevin O'Connor | a0dc296 | 2008-03-16 14:29:32 -0400 | [diff] [blame] | 150 | // pcibios.c |
| 151 | void handle_1ab1(struct bregs *regs); |
| 152 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 153 | // shadow.c |
| 154 | void make_bios_writable(); |
| 155 | void make_bios_readonly(); |
| 156 | |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 157 | // pciinit.c |
| 158 | void pci_bios_setup(void); |
Kevin O'Connor | a4d3576 | 2008-03-08 15:43:03 -0500 | [diff] [blame] | 159 | |
Kevin O'Connor | f7ba6d7 | 2008-07-04 05:05:54 -0400 | [diff] [blame] | 160 | // smm.c |
| 161 | void smm_init(); |
| 162 | |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 163 | // smpdetect.c |
| 164 | int smp_probe(void); |
Kevin O'Connor | acf1374 | 2008-11-29 11:19:19 -0500 | [diff] [blame] | 165 | void smp_probe_setup(void); |
Kevin O'Connor | 84ad59a | 2008-07-04 05:47:26 -0400 | [diff] [blame] | 166 | |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 167 | // smbios.c |
| 168 | void smbios_init(void); |
| 169 | |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 170 | // coreboot.c |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 171 | const char *cbfs_findNprefix(const char *prefix, int n); |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 172 | int cbfs_copy_optionrom(void *dst, u32 maxlen, u32 vendev); |
Kevin O'Connor | 6782344 | 2009-04-13 14:14:51 -0400 | [diff] [blame] | 173 | void cbfs_run_payload(const char *filename); |
Kevin O'Connor | 1edc89d | 2009-04-30 21:50:35 -0400 | [diff] [blame] | 174 | struct cbfs_file; |
| 175 | struct cbfs_file *cbfs_copy_gen_optionrom(void *dst, u32 maxlen |
| 176 | , struct cbfs_file *file); |
Kevin O'Connor | 4c0c85a | 2009-04-11 23:31:29 -0400 | [diff] [blame] | 177 | void coreboot_setup(); |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 178 | |
Kevin O'Connor | cbffa8e | 2008-08-17 11:11:07 -0400 | [diff] [blame] | 179 | // vgahooks.c |
| 180 | void handle_155f(); |
| 181 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 182 | // optionroms.c |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 183 | void call_bcv(u16 seg, u16 ip); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 184 | void vga_setup(); |
| 185 | void optionrom_setup(); |
| 186 | |
Kevin O'Connor | 18e38b2 | 2008-12-10 20:40:13 -0500 | [diff] [blame] | 187 | // resume.c |
| 188 | void init_dma(); |
| 189 | |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 190 | // pnpbios.c |
| 191 | #define PNP_SIGNATURE 0x506e5024 // $PnP |
| 192 | u16 get_pnp_offset(); |
| 193 | void pnp_setup(); |
| 194 | |
Kevin O'Connor | 7061eb6 | 2009-01-04 21:48:22 -0500 | [diff] [blame] | 195 | // mtrr.c |
| 196 | void mtrr_setup(void); |
| 197 | |
Kevin O'Connor | 18e38b2 | 2008-12-10 20:40:13 -0500 | [diff] [blame] | 198 | // romlayout.S |
| 199 | void reset_vector() __attribute__ ((noreturn)); |
| 200 | |
Kevin O'Connor | 3085376 | 2009-01-17 18:49:20 -0500 | [diff] [blame] | 201 | // misc.c |
| 202 | extern u8 BiosChecksum; |
| 203 | |
Kevin O'Connor | 786502d | 2008-02-27 10:41:41 -0500 | [diff] [blame] | 204 | #endif // util.h |