Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // 32bit code to Power On Self Test (POST) a machine. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
Kevin O'Connor | b1b7c2a | 2009-01-15 20:52:58 -0500 | [diff] [blame] | 6 | // This file may be distributed under the terms of the GNU LGPLv3 license. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 7 | |
| 8 | #include "ioport.h" // PORT_* |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 9 | #include "config.h" // CONFIG_* |
| 10 | #include "cmos.h" // CMOS_* |
| 11 | #include "util.h" // memset |
| 12 | #include "biosvar.h" // struct bios_data_area_s |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 13 | #include "disk.h" // floppy_drive_setup |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 14 | #include "memmap.h" // add_e820 |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 15 | #include "pic.h" // pic_setup |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 16 | #include "pci.h" // create_pirtable |
| 17 | #include "acpi.h" // acpi_bios_init |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 18 | #include "bregs.h" // struct bregs |
Kevin O'Connor | 3574644 | 2009-02-27 20:54:51 -0500 | [diff] [blame] | 19 | #include "mptable.h" // mptable_init |
Kevin O'Connor | c659fde | 2008-12-28 23:43:20 -0500 | [diff] [blame] | 20 | #include "boot.h" // IPL |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 21 | |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 22 | void |
| 23 | __set_irq(int vector, void *loc) |
Kevin O'Connor | e3677b1 | 2008-07-04 15:29:23 -0400 | [diff] [blame] | 24 | { |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 25 | SET_IVT(vector, SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR); |
Kevin O'Connor | e3677b1 | 2008-07-04 15:29:23 -0400 | [diff] [blame] | 26 | } |
| 27 | |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 28 | #define set_irq(vector, func) do { \ |
| 29 | extern void func (void); \ |
| 30 | __set_irq(vector, func); \ |
| 31 | } while (0) |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 32 | |
Kevin O'Connor | e3677b1 | 2008-07-04 15:29:23 -0400 | [diff] [blame] | 33 | static void |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 34 | init_ivt() |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 35 | { |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 36 | dprintf(3, "init ivt\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 37 | |
Kevin O'Connor | d67a703 | 2009-01-17 19:37:26 -0500 | [diff] [blame] | 38 | // Initialize all vectors to the default handler. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 39 | int i; |
Kevin O'Connor | e3677b1 | 2008-07-04 15:29:23 -0400 | [diff] [blame] | 40 | for (i=0; i<256; i++) |
Kevin O'Connor | d67a703 | 2009-01-17 19:37:26 -0500 | [diff] [blame] | 41 | set_irq(i, entry_iret_official); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 42 | |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 43 | // Initialize all hw vectors to a default hw handler. |
| 44 | for (i=0x08; i<=0x0f; i++) |
Kevin O'Connor | ffdc9ee | 2008-12-20 13:10:00 -0500 | [diff] [blame] | 45 | set_irq(i, entry_hwpic1); |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 46 | for (i=0x70; i<=0x77; i++) |
Kevin O'Connor | ffdc9ee | 2008-12-20 13:10:00 -0500 | [diff] [blame] | 47 | set_irq(i, entry_hwpic2); |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 48 | |
| 49 | // Initialize software handlers. |
| 50 | set_irq(0x10, entry_10); |
Kevin O'Connor | b4f0e89 | 2008-12-13 18:33:05 -0500 | [diff] [blame] | 51 | set_irq(0x11, entry_11_official); |
| 52 | set_irq(0x12, entry_12_official); |
| 53 | set_irq(0x13, entry_13_official); |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 54 | set_irq(0x14, entry_14); |
| 55 | set_irq(0x15, entry_15); |
| 56 | set_irq(0x16, entry_16); |
| 57 | set_irq(0x17, entry_17); |
| 58 | set_irq(0x18, entry_18); |
Kevin O'Connor | b4f0e89 | 2008-12-13 18:33:05 -0500 | [diff] [blame] | 59 | set_irq(0x19, entry_19_official); |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 60 | set_irq(0x1a, entry_1a); |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 61 | set_irq(0x40, entry_40); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 62 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 63 | // set vector 0x79 to zero |
| 64 | // this is used by 'gardian angel' protection system |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 65 | SET_IVT(0x79, 0, 0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 66 | |
Kevin O'Connor | d21c089 | 2008-11-26 17:02:43 -0500 | [diff] [blame] | 67 | __set_irq(0x1E, &diskette_param_table2); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 71 | init_bda() |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 72 | { |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 73 | dprintf(3, "init bda\n"); |
| 74 | |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 75 | struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0); |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 76 | memset(bda, 0, sizeof(*bda)); |
| 77 | |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 78 | int esize = DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024); |
| 79 | SET_BDA(mem_size_kb, 640 - esize); |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 80 | u16 eseg = FLATPTR_TO_SEG((640 - esize) * 1024); |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 81 | SET_BDA(ebda_seg, eseg); |
| 82 | |
| 83 | struct extended_bios_data_area_s *ebda = get_ebda_ptr(); |
Kevin O'Connor | 38fcbfe | 2008-02-25 22:30:47 -0500 | [diff] [blame] | 84 | memset(ebda, 0, sizeof(*ebda)); |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 85 | ebda->size = esize; |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 86 | SET_IVT(0x41, eseg, offsetof(struct extended_bios_data_area_s, fdpt[0])); |
| 87 | SET_IVT(0x46, eseg, offsetof(struct extended_bios_data_area_s, fdpt[1])); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 91 | ram_probe(void) |
| 92 | { |
Kevin O'Connor | 35192dd | 2008-06-08 19:18:33 -0400 | [diff] [blame] | 93 | dprintf(3, "Find memory size\n"); |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 94 | if (CONFIG_COREBOOT) { |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 95 | coreboot_fill_map(); |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 96 | } else { |
| 97 | // On emulators, get memory size from nvram. |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 98 | u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16) |
| 99 | | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24)); |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 100 | if (rs) |
| 101 | rs += 16 * 1024 * 1024; |
| 102 | else |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 103 | rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10) |
| 104 | | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18)) |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 105 | + 1 * 1024 * 1024); |
Kevin O'Connor | e791636 | 2008-12-28 22:03:17 -0500 | [diff] [blame] | 106 | RamSize = rs; |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 107 | add_e820(0, rs, E820_RAM); |
| 108 | |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 109 | // Check for memory over 4Gig |
| 110 | u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16) |
| 111 | | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24) |
| 112 | | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32)); |
Kevin O'Connor | e791636 | 2008-12-28 22:03:17 -0500 | [diff] [blame] | 113 | RamSizeOver4G = high; |
Kevin O'Connor | 59c35f2 | 2008-10-25 23:05:42 -0400 | [diff] [blame] | 114 | add_e820(0x100000000ull, high, E820_RAM); |
| 115 | |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 116 | /* reserve 256KB BIOS area at the end of 4 GB */ |
| 117 | add_e820(0xfffc0000, 256*1024, E820_RESERVED); |
Kevin O'Connor | f64f0db | 2008-05-18 02:42:58 -0400 | [diff] [blame] | 118 | } |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 119 | |
Kevin O'Connor | b8d7a47 | 2008-06-21 11:43:32 -0400 | [diff] [blame] | 120 | // Don't declare any memory between 0xa0000 and 0x100000 |
| 121 | add_e820(0xa0000, 0x50000, E820_HOLE); |
| 122 | |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 123 | // Mark known areas as reserved. |
Kevin O'Connor | 0881537 | 2008-12-29 21:16:31 -0500 | [diff] [blame] | 124 | u16 ebda_seg = get_ebda_seg(); |
Kevin O'Connor | 35ae726 | 2009-01-19 15:44:44 -0500 | [diff] [blame] | 125 | add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024 |
Kevin O'Connor | 4d7c37e | 2008-12-26 23:50:17 -0500 | [diff] [blame] | 126 | , E820_RESERVED); |
Kevin O'Connor | e3677b1 | 2008-07-04 15:29:23 -0400 | [diff] [blame] | 127 | add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED); |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 128 | |
Kevin O'Connor | 7061eb6 | 2009-01-04 21:48:22 -0500 | [diff] [blame] | 129 | if (CONFIG_KVM) |
| 130 | // 4 pages before the bios, 3 pages for vmx tss pages, the |
| 131 | // other page for EPT real mode pagetable |
| 132 | add_e820(0xfffbc000, 4*4096, E820_RESERVED); |
| 133 | |
Kevin O'Connor | e791636 | 2008-12-28 22:03:17 -0500 | [diff] [blame] | 134 | dprintf(1, "Ram Size=0x%08x\n", RamSize); |
Kevin O'Connor | 9571ac2 | 2008-05-17 22:20:27 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 138 | init_bios_tables(void) |
| 139 | { |
| 140 | if (CONFIG_COREBOOT) |
| 141 | // XXX - not supported on coreboot yet. |
| 142 | return; |
| 143 | |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 144 | create_pirtable(); |
| 145 | |
| 146 | mptable_init(); |
| 147 | |
| 148 | smbios_init(); |
| 149 | |
| 150 | acpi_bios_init(); |
| 151 | } |
| 152 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 153 | // Main setup code. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 154 | static void |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 155 | post() |
| 156 | { |
Kevin O'Connor | 8c0e372 | 2009-01-02 14:19:43 -0500 | [diff] [blame] | 157 | init_ivt(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 158 | init_bda(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 159 | |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 160 | pic_setup(); |
Kevin O'Connor | e6eb3f5 | 2008-04-13 17:37:41 -0400 | [diff] [blame] | 161 | timer_setup(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 162 | mathcp_setup(); |
| 163 | |
Kevin O'Connor | acf1374 | 2008-11-29 11:19:19 -0500 | [diff] [blame] | 164 | smp_probe_setup(); |
Kevin O'Connor | cf89e66 | 2008-11-28 11:56:37 -0500 | [diff] [blame] | 165 | |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 166 | memmap_setup(); |
| 167 | ram_probe(); |
Kevin O'Connor | 7061eb6 | 2009-01-04 21:48:22 -0500 | [diff] [blame] | 168 | mtrr_setup(); |
Kevin O'Connor | ceea03c | 2008-11-08 21:36:35 -0500 | [diff] [blame] | 169 | |
Kevin O'Connor | 0c3068d | 2008-12-21 17:51:36 -0500 | [diff] [blame] | 170 | pnp_setup(); |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 171 | vga_setup(); |
| 172 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 173 | kbd_setup(); |
| 174 | lpt_setup(); |
| 175 | serial_setup(); |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 176 | mouse_setup(); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 177 | |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 178 | pci_bios_setup(); |
Kevin O'Connor | 4bbd3b3 | 2008-12-10 21:01:00 -0500 | [diff] [blame] | 179 | smm_init(); |
| 180 | |
Kevin O'Connor | 0525d29 | 2008-07-04 06:18:30 -0400 | [diff] [blame] | 181 | init_bios_tables(); |
Kevin O'Connor | c781293 | 2008-06-08 23:08:12 -0400 | [diff] [blame] | 182 | memmap_finalize(); |
| 183 | |
Kevin O'Connor | 9f4e1d9 | 2009-02-08 15:44:08 -0500 | [diff] [blame] | 184 | boot_setup(); |
| 185 | |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 186 | floppy_drive_setup(); |
Kevin O'Connor | 9f0d94d | 2008-04-13 17:25:30 -0400 | [diff] [blame] | 187 | hard_drive_setup(); |
Kevin O'Connor | a2e7380 | 2008-02-27 10:27:00 -0500 | [diff] [blame] | 188 | |
Kevin O'Connor | 714325c | 2008-11-01 20:32:27 -0400 | [diff] [blame] | 189 | optionrom_setup(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 190 | } |
| 191 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 192 | // 32-bit entry point. |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 193 | void VISIBLE32 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 194 | _start() |
| 195 | { |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 196 | init_dma(); |
Kevin O'Connor | 95576e7 | 2008-03-01 09:57:51 -0500 | [diff] [blame] | 197 | |
Kevin O'Connor | 61d6b06 | 2008-06-21 12:15:10 -0400 | [diff] [blame] | 198 | debug_serial_setup(); |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 199 | dprintf(1, "Start bios\n"); |
| 200 | |
Kevin O'Connor | acf1374 | 2008-11-29 11:19:19 -0500 | [diff] [blame] | 201 | // Allow writes to modify bios area (0xf0000) |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 202 | make_bios_writable(); |
| 203 | |
| 204 | // Perform main setup code. |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 205 | post(); |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 206 | |
Kevin O'Connor | 0a92412 | 2009-02-08 19:43:47 -0500 | [diff] [blame] | 207 | // Run BCVs |
| 208 | boot_prep(); |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 209 | |
Kevin O'Connor | e90f264 | 2008-06-28 12:18:39 -0400 | [diff] [blame] | 210 | // Setup bios checksum. |
Kevin O'Connor | 3085376 | 2009-01-17 18:49:20 -0500 | [diff] [blame] | 211 | BiosChecksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1); |
Kevin O'Connor | e90f264 | 2008-06-28 12:18:39 -0400 | [diff] [blame] | 212 | |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 213 | // Prep for boot process. |
| 214 | make_bios_readonly(); |
Kevin O'Connor | da4a648 | 2008-06-08 13:48:06 -0400 | [diff] [blame] | 215 | |
| 216 | // Invoke int 19 to start boot process. |
| 217 | dprintf(3, "Jump to int19\n"); |
| 218 | struct bregs br; |
| 219 | memset(&br, 0, sizeof(br)); |
| 220 | call16_int(0x19, &br); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 221 | } |
Kevin O'Connor | 4b39b82 | 2008-05-08 21:47:33 -0400 | [diff] [blame] | 222 | |
Kevin O'Connor | 2fda7cb | 2008-07-05 20:41:53 -0400 | [diff] [blame] | 223 | // Ughh - some older gcc compilers have a bug which causes VISIBLE32 |
| 224 | // functions to not be exported as a global variable - force _start |
| 225 | // to be global here. |
| 226 | asm(".global _start"); |