blob: 0cb9e575877aedb455b5f52607b2f738a8d0ba4f [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 32bit code to Power On Self Test (POST) a machine.
2//
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -04003// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
8#include "ioport.h" // PORT_*
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05009#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'Connor3bbcc142008-04-13 17:07:33 -040013#include "disk.h" // floppy_drive_setup
Kevin O'Connorc892b132009-08-11 21:59:37 -040014#include "ata.h" // ata_setup
Kevin O'Connorc7812932008-06-08 23:08:12 -040015#include "memmap.h" // add_e820
Kevin O'Connorf54c1502008-06-14 15:56:16 -040016#include "pic.h" // pic_setup
Kevin O'Connor0525d292008-07-04 06:18:30 -040017#include "pci.h" // create_pirtable
18#include "acpi.h" // acpi_bios_init
Kevin O'Connor9521e262008-07-04 13:04:29 -040019#include "bregs.h" // struct bregs
Kevin O'Connor35746442009-02-27 20:54:51 -050020#include "mptable.h" // mptable_init
Kevin O'Connorc659fde2008-12-28 23:43:20 -050021#include "boot.h" // IPL
Kevin O'Connor114592f2009-09-28 21:32:08 -040022#include "usb.h" // usb_setup
Kevin O'Connor01a85202009-10-18 09:49:59 -040023#include "smbios.h" // smbios_init
24#include "paravirt.h" // qemu_cfg_port_probe
Kevin O'Connor57877482009-12-09 21:00:41 -050025#include "ps2port.h" // ps2port_setup
Gleb Natapov89acfa32010-05-10 11:36:37 +030026#include "virtio-blk.h" // virtio_blk_setup
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050027
Kevin O'Connord21c0892008-11-26 17:02:43 -050028void
29__set_irq(int vector, void *loc)
Kevin O'Connore3677b12008-07-04 15:29:23 -040030{
Kevin O'Connor9f985422009-09-09 11:34:39 -040031 SET_IVT(vector, SEGOFF(SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR));
Kevin O'Connore3677b12008-07-04 15:29:23 -040032}
33
Kevin O'Connord21c0892008-11-26 17:02:43 -050034#define set_irq(vector, func) do { \
35 extern void func (void); \
36 __set_irq(vector, func); \
37 } while (0)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040038
Kevin O'Connore3677b12008-07-04 15:29:23 -040039static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050040init_ivt(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050041{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050042 dprintf(3, "init ivt\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050043
Kevin O'Connord67a7032009-01-17 19:37:26 -050044 // Initialize all vectors to the default handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050045 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040046 for (i=0; i<256; i++)
Kevin O'Connord67a7032009-01-17 19:37:26 -050047 set_irq(i, entry_iret_official);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050048
Kevin O'Connord21c0892008-11-26 17:02:43 -050049 // Initialize all hw vectors to a default hw handler.
50 for (i=0x08; i<=0x0f; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050051 set_irq(i, entry_hwpic1);
Kevin O'Connord21c0892008-11-26 17:02:43 -050052 for (i=0x70; i<=0x77; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050053 set_irq(i, entry_hwpic2);
Kevin O'Connord21c0892008-11-26 17:02:43 -050054
55 // Initialize software handlers.
Kevin O'Connor75f49b32009-03-07 00:07:24 -050056 set_irq(0x02, entry_02);
Kevin O'Connord21c0892008-11-26 17:02:43 -050057 set_irq(0x10, entry_10);
Kevin O'Connor9f193b92009-05-16 23:31:27 -040058 set_irq(0x11, entry_11);
59 set_irq(0x12, entry_12);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050060 set_irq(0x13, entry_13_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050061 set_irq(0x14, entry_14);
62 set_irq(0x15, entry_15);
63 set_irq(0x16, entry_16);
64 set_irq(0x17, entry_17);
65 set_irq(0x18, entry_18);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050066 set_irq(0x19, entry_19_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050067 set_irq(0x1a, entry_1a);
Kevin O'Connord21c0892008-11-26 17:02:43 -050068 set_irq(0x40, entry_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050069
Kevin O'Connorb164d2c2009-12-26 23:24:52 -050070 // INT 60h-66h reserved for user interrupt
71 for (i=0x60; i<=0x66; i++)
72 SET_IVT(i, SEGOFF(0, 0));
73
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050074 // set vector 0x79 to zero
75 // this is used by 'gardian angel' protection system
Kevin O'Connor9f985422009-09-09 11:34:39 -040076 SET_IVT(0x79, SEGOFF(0, 0));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050077
Kevin O'Connord21c0892008-11-26 17:02:43 -050078 __set_irq(0x1E, &diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079}
80
81static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050082init_bda(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050083{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050084 dprintf(3, "init bda\n");
85
Kevin O'Connor35ae7262009-01-19 15:44:44 -050086 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050087 memset(bda, 0, sizeof(*bda));
88
Kevin O'Connor49553a42009-05-17 10:31:34 -040089 int esize = EBDA_SIZE_START;
Kevin O'Connorf416fe92009-09-24 20:51:55 -040090 SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
Kevin O'Connor49553a42009-05-17 10:31:34 -040091 u16 eseg = EBDA_SEGMENT_START;
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050092 SET_BDA(ebda_seg, eseg);
93
Kevin O'Connor9becbf12009-07-12 09:29:46 -040094 // Init ebda
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050095 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050096 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050097 ebda->size = esize;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050098}
99
100static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400101ram_probe(void)
102{
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400103 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400104 if (CONFIG_COREBOOT) {
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -0400105 coreboot_setup();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400106 } else {
107 // On emulators, get memory size from nvram.
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400108 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
109 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400110 if (rs)
111 rs += 16 * 1024 * 1024;
112 else
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400113 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
114 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400115 + 1 * 1024 * 1024);
Kevin O'Connore7916362008-12-28 22:03:17 -0500116 RamSize = rs;
Kevin O'Connorc7812932008-06-08 23:08:12 -0400117 add_e820(0, rs, E820_RAM);
118
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400119 // Check for memory over 4Gig
120 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
Kevin O'Connor669c9912010-01-05 19:50:04 -0500121 | ((u32)inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400122 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
Kevin O'Connore7916362008-12-28 22:03:17 -0500123 RamSizeOver4G = high;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400124 add_e820(0x100000000ull, high, E820_RAM);
125
Kevin O'Connorc7812932008-06-08 23:08:12 -0400126 /* reserve 256KB BIOS area at the end of 4 GB */
127 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400128 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400129
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400130 // Don't declare any memory between 0xa0000 and 0x100000
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400131 add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400132
Kevin O'Connorc7812932008-06-08 23:08:12 -0400133 // Mark known areas as reserved.
Kevin O'Connor08815372008-12-29 21:16:31 -0500134 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500135 add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -0500136 , E820_RESERVED);
Kevin O'Connore3677b12008-07-04 15:29:23 -0400137 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400138
Jes Sorensen0360e8e2010-02-16 09:46:08 +0100139 u32 count = qemu_cfg_e820_entries();
140 if (count) {
141 struct e820_reservation entry;
142 int i;
143
144 for (i = 0; i < count; i++) {
145 qemu_cfg_e820_load_next(&entry);
146 add_e820(entry.address, entry.length, entry.type);
147 }
148 } else if (kvm_para_available()) {
149 // Backwards compatibility - provide hard coded range.
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500150 // 4 pages before the bios, 3 pages for vmx tss pages, the
151 // other page for EPT real mode pagetable
152 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
Jes Sorensen0360e8e2010-02-16 09:46:08 +0100153 }
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500154
Kevin O'Connor979862e2009-12-24 11:09:30 -0500155 dprintf(1, "Ram Size=0x%08x (0x%08x%08x high)\n"
156 , RamSize, (u32)(RamSizeOver4G >> 32), (u32)RamSizeOver4G);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400157}
158
159static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400160init_bios_tables(void)
161{
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400162 if (CONFIG_COREBOOT) {
163 coreboot_copy_biostable();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400164 return;
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400165 }
Kevin O'Connor0525d292008-07-04 06:18:30 -0400166
Kevin O'Connor0525d292008-07-04 06:18:30 -0400167 create_pirtable();
168
169 mptable_init();
170
171 smbios_init();
172
173 acpi_bios_init();
174}
175
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400176// Initialize hardware devices
177static void
178init_hw(void)
179{
180 usb_setup();
181 ps2port_setup();
182 lpt_setup();
183 serial_setup();
184
185 floppy_setup();
186 ata_setup();
187 ramdisk_setup();
Gleb Natapov89acfa32010-05-10 11:36:37 +0300188 virtio_blk_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400189}
190
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400191// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500192static void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500193post(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500194{
Kevin O'Connor969df602009-11-25 19:05:04 -0500195 // Detect and init ram.
Kevin O'Connor8c0e3722009-01-02 14:19:43 -0500196 init_ivt();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500197 init_bda();
Kevin O'Connor969df602009-11-25 19:05:04 -0500198 memmap_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400199 qemu_cfg_port_probe();
Kevin O'Connor969df602009-11-25 19:05:04 -0500200 ram_probe();
201 malloc_setup();
Kevin O'Connore9a67bf2009-12-09 20:04:54 -0500202 thread_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500203
Kevin O'Connor969df602009-11-25 19:05:04 -0500204 // Init base pc hardware.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400205 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400206 timer_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400207 mathcp_setup();
208
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400209 // Initialize mtrr
Kevin O'Connoracf13742008-11-29 11:19:19 -0500210 smp_probe_setup();
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500211 mtrr_setup();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500212
Kevin O'Connor969df602009-11-25 19:05:04 -0500213 // Initialize pci
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400214 pci_setup();
215 smm_init();
216
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400217 // Initialize internal tables
218 boot_setup();
219 drive_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400220
221 // Start hardware initialization (if optionrom threading)
222 if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS)
223 init_hw();
224
225 // Find and initialize other cpus
226 smp_probe();
227
Kevin O'Connor98032802009-12-09 20:18:31 -0500228 // Setup interfaces that option roms may need
Kevin O'Connor871e0a02009-12-30 12:14:53 -0500229 bios32_setup();
Kevin O'Connor969df602009-11-25 19:05:04 -0500230 pmm_setup();
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500231 pnp_setup();
Kevin O'Connor57877482009-12-09 21:00:41 -0500232 kbd_setup();
233 mouse_setup();
Kevin O'Connor98032802009-12-09 20:18:31 -0500234 init_bios_tables();
235
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400236 // Run vga option rom
237 vga_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400238
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400239 // Do hardware initialization (if running synchronously)
240 if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS) {
241 init_hw();
242 wait_threads();
243 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500244
Kevin O'Connor98032802009-12-09 20:18:31 -0500245 // Run option roms
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400246 optionrom_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400247
Kevin O'Connor969df602009-11-25 19:05:04 -0500248 // Run BCVs and show optional boot menu
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400249 boot_prep();
250
Kevin O'Connor969df602009-11-25 19:05:04 -0500251 // Finalize data structures before boot
Kevin O'Connord5d02b62010-06-06 16:18:03 -0400252 cdemu_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400253 pmm_finalize();
254 malloc_finalize();
255 memmap_finalize();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500256}
257
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400258// 32-bit entry point.
Kevin O'Connor52a300f2009-12-26 23:32:57 -0500259void VISIBLE32FLAT
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500260_start(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500261{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500262 init_dma();
Kevin O'Connor95576e72008-03-01 09:57:51 -0500263
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400264 debug_serial_setup();
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400265 dprintf(1, "Start bios (version %s)\n", VERSION);
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400266
Kevin O'Connoracf13742008-11-29 11:19:19 -0500267 // Allow writes to modify bios area (0xf0000)
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400268 make_bios_writable();
269
270 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500271 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400272
Kevin O'Connore90f2642008-06-28 12:18:39 -0400273 // Setup bios checksum.
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400274 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
Kevin O'Connore90f2642008-06-28 12:18:39 -0400275
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400276 // Write protect bios memory.
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400277 make_bios_readonly();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400278
Kevin O'Connorcadaf0e2010-07-26 23:47:26 -0400279 // Disable bootsplash if something has hooked int19.
280 extern void entry_19_official(void);
281 if (GET_IVT(0x19).segoff
282 != SEGOFF(SEG_BIOS, (u32)entry_19_official - BUILD_BIOS_ADDR).segoff)
283 disable_bootsplash();
284
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400285 // Invoke int 19 to start boot process.
286 dprintf(3, "Jump to int19\n");
287 struct bregs br;
288 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400289 br.flags = F_IF;
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400290 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500291}