blob: b37a37dfb600b4989bedba63d54d60dc1e0ef227 [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'Connor2929c352009-07-25 13:48:27 -04003// Copyright (C) 2008,2009 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'Connorf076a3e2008-02-25 22:25:15 -050025
Kevin O'Connord21c0892008-11-26 17:02:43 -050026void
27__set_irq(int vector, void *loc)
Kevin O'Connore3677b12008-07-04 15:29:23 -040028{
Kevin O'Connor9f985422009-09-09 11:34:39 -040029 SET_IVT(vector, SEGOFF(SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR));
Kevin O'Connore3677b12008-07-04 15:29:23 -040030}
31
Kevin O'Connord21c0892008-11-26 17:02:43 -050032#define set_irq(vector, func) do { \
33 extern void func (void); \
34 __set_irq(vector, func); \
35 } while (0)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040036
Kevin O'Connore3677b12008-07-04 15:29:23 -040037static void
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050038init_ivt()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050039{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050040 dprintf(3, "init ivt\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050041
Kevin O'Connord67a7032009-01-17 19:37:26 -050042 // Initialize all vectors to the default handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050043 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040044 for (i=0; i<256; i++)
Kevin O'Connord67a7032009-01-17 19:37:26 -050045 set_irq(i, entry_iret_official);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050046
Kevin O'Connord21c0892008-11-26 17:02:43 -050047 // Initialize all hw vectors to a default hw handler.
48 for (i=0x08; i<=0x0f; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050049 set_irq(i, entry_hwpic1);
Kevin O'Connord21c0892008-11-26 17:02:43 -050050 for (i=0x70; i<=0x77; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050051 set_irq(i, entry_hwpic2);
Kevin O'Connord21c0892008-11-26 17:02:43 -050052
53 // Initialize software handlers.
Kevin O'Connor75f49b32009-03-07 00:07:24 -050054 set_irq(0x02, entry_02);
Kevin O'Connord21c0892008-11-26 17:02:43 -050055 set_irq(0x10, entry_10);
Kevin O'Connor9f193b92009-05-16 23:31:27 -040056 set_irq(0x11, entry_11);
57 set_irq(0x12, entry_12);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050058 set_irq(0x13, entry_13_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050059 set_irq(0x14, entry_14);
60 set_irq(0x15, entry_15);
61 set_irq(0x16, entry_16);
62 set_irq(0x17, entry_17);
63 set_irq(0x18, entry_18);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050064 set_irq(0x19, entry_19_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050065 set_irq(0x1a, entry_1a);
Kevin O'Connord21c0892008-11-26 17:02:43 -050066 set_irq(0x40, entry_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050067
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050068 // set vector 0x79 to zero
69 // this is used by 'gardian angel' protection system
Kevin O'Connor9f985422009-09-09 11:34:39 -040070 SET_IVT(0x79, SEGOFF(0, 0));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050071
Kevin O'Connord21c0892008-11-26 17:02:43 -050072 __set_irq(0x1E, &diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050073}
74
75static void
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050076init_bda()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050077{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050078 dprintf(3, "init bda\n");
79
Kevin O'Connor35ae7262009-01-19 15:44:44 -050080 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050081 memset(bda, 0, sizeof(*bda));
82
Kevin O'Connor49553a42009-05-17 10:31:34 -040083 int esize = EBDA_SIZE_START;
Kevin O'Connorf416fe92009-09-24 20:51:55 -040084 SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
Kevin O'Connor49553a42009-05-17 10:31:34 -040085 u16 eseg = EBDA_SEGMENT_START;
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050086 SET_BDA(ebda_seg, eseg);
87
Kevin O'Connor9becbf12009-07-12 09:29:46 -040088 // Init ebda
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050089 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050090 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050091 ebda->size = esize;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050092}
93
94static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -040095ram_probe(void)
96{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040097 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040098 if (CONFIG_COREBOOT) {
Kevin O'Connor4c0c85a2009-04-11 23:31:29 -040099 coreboot_setup();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400100 } else {
101 // On emulators, get memory size from nvram.
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400102 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
103 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400104 if (rs)
105 rs += 16 * 1024 * 1024;
106 else
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400107 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
108 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400109 + 1 * 1024 * 1024);
Kevin O'Connore7916362008-12-28 22:03:17 -0500110 RamSize = rs;
Kevin O'Connorc7812932008-06-08 23:08:12 -0400111 add_e820(0, rs, E820_RAM);
112
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400113 // Check for memory over 4Gig
114 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
115 | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
116 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
Kevin O'Connore7916362008-12-28 22:03:17 -0500117 RamSizeOver4G = high;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400118 add_e820(0x100000000ull, high, E820_RAM);
119
Kevin O'Connorc7812932008-06-08 23:08:12 -0400120 /* reserve 256KB BIOS area at the end of 4 GB */
121 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400122 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400123
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400124 // Don't declare any memory between 0xa0000 and 0x100000
Kevin O'Connorf416fe92009-09-24 20:51:55 -0400125 add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400126
Kevin O'Connorc7812932008-06-08 23:08:12 -0400127 // Mark known areas as reserved.
Kevin O'Connor08815372008-12-29 21:16:31 -0500128 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500129 add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -0500130 , E820_RESERVED);
Kevin O'Connore3677b12008-07-04 15:29:23 -0400131 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400132
Kevin O'Connorb8bf14a2009-10-08 21:50:31 -0400133 if (kvm_para_available())
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500134 // 4 pages before the bios, 3 pages for vmx tss pages, the
135 // other page for EPT real mode pagetable
136 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
137
Kevin O'Connore7916362008-12-28 22:03:17 -0500138 dprintf(1, "Ram Size=0x%08x\n", RamSize);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400139}
140
141static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400142init_bios_tables(void)
143{
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400144 if (CONFIG_COREBOOT) {
145 coreboot_copy_biostable();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400146 return;
Kevin O'Connordf5a5fd2009-07-28 20:46:03 -0400147 }
Kevin O'Connor0525d292008-07-04 06:18:30 -0400148
Kevin O'Connor0525d292008-07-04 06:18:30 -0400149 create_pirtable();
150
151 mptable_init();
152
153 smbios_init();
154
155 acpi_bios_init();
156}
157
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400158// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500159static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500160post()
161{
Kevin O'Connor969df602009-11-25 19:05:04 -0500162 // Detect and init ram.
Kevin O'Connor8c0e3722009-01-02 14:19:43 -0500163 init_ivt();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500164 init_bda();
Kevin O'Connor969df602009-11-25 19:05:04 -0500165 memmap_setup();
166 ram_probe();
167 malloc_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500168
Kevin O'Connor969df602009-11-25 19:05:04 -0500169 // Init base pc hardware.
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400170 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400171 timer_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400172 mathcp_setup();
173
Kevin O'Connor969df602009-11-25 19:05:04 -0500174 // Initialize smp
175 qemu_cfg_port_probe();
Kevin O'Connoracf13742008-11-29 11:19:19 -0500176 smp_probe_setup();
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500177 mtrr_setup();
Kevin O'Connore97ca7b2009-06-21 09:10:28 -0400178 smp_probe();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500179
Kevin O'Connor969df602009-11-25 19:05:04 -0500180 // Initialize pci
Kevin O'Connor40f5b5a2009-09-13 10:46:57 -0400181 pci_setup();
182 smm_init();
183
Kevin O'Connor969df602009-11-25 19:05:04 -0500184 // Run vga option rom.
185 pmm_setup();
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500186 pnp_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400187 vga_setup();
188
Kevin O'Connor969df602009-11-25 19:05:04 -0500189 // Initialize hardware devices
Kevin O'Connor114592f2009-09-28 21:32:08 -0400190 usb_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500191 kbd_setup();
192 lpt_setup();
193 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400194 mouse_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500195
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500196 boot_setup();
Kevin O'Connorc892b132009-08-11 21:59:37 -0400197 drive_setup();
Kevin O'Connor36c93a52009-09-12 19:35:04 -0400198 cdemu_setup();
Kevin O'Connorc892b132009-08-11 21:59:37 -0400199 floppy_setup();
200 ata_setup();
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400201 ramdisk_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500202
Kevin O'Connor969df602009-11-25 19:05:04 -0500203 // Run option roms (non vga)
204 init_bios_tables();
Kevin O'Connora5826b52009-10-24 17:57:29 -0400205 wait_threads();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400206 optionrom_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400207
Kevin O'Connor969df602009-11-25 19:05:04 -0500208 // Run BCVs and show optional boot menu
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400209 boot_prep();
210
Kevin O'Connor969df602009-11-25 19:05:04 -0500211 // Finalize data structures before boot
Kevin O'Connore54ee382009-07-26 19:33:13 -0400212 pmm_finalize();
213 malloc_finalize();
214 memmap_finalize();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500215}
216
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400217// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500218void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500219_start()
220{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500221 init_dma();
Kevin O'Connor95576e72008-03-01 09:57:51 -0500222
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400223 debug_serial_setup();
Kevin O'Connorc95d2ce2009-07-29 20:41:39 -0400224 dprintf(1, "Start bios (version %s)\n", VERSION);
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400225
Kevin O'Connoracf13742008-11-29 11:19:19 -0500226 // Allow writes to modify bios area (0xf0000)
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400227 make_bios_writable();
228
229 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500230 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400231
Kevin O'Connore90f2642008-06-28 12:18:39 -0400232 // Setup bios checksum.
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400233 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
Kevin O'Connore90f2642008-06-28 12:18:39 -0400234
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400235 // Write protect bios memory.
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400236 make_bios_readonly();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400237
238 // Invoke int 19 to start boot process.
239 dprintf(3, "Jump to int19\n");
240 struct bregs br;
241 memset(&br, 0, sizeof(br));
Kevin O'Connorf8e800d2009-09-24 21:01:16 -0400242 br.flags = F_IF;
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400243 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500244}