blob: d75b868e0cb3515a92283b3889b203befe739eb6 [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'Connor8a0a9722013-01-21 01:53:31 -05003// Copyright (C) 2008-2013 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
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05008#include "config.h" // CONFIG_*
9#include "cmos.h" // CMOS_*
10#include "util.h" // memset
11#include "biosvar.h" // struct bios_data_area_s
Kevin O'Connord83c87b2013-01-21 01:14:12 -050012#include "disk.h" // floppy_setup
Kevin O'Connorc892b132009-08-11 21:59:37 -040013#include "ata.h" // ata_setup
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +010014#include "ahci.h" // ahci_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'Connor9521e262008-07-04 13:04:29 -040017#include "bregs.h" // struct bregs
Kevin O'Connord83c87b2013-01-21 01:14:12 -050018#include "boot.h" // boot_init
Kevin O'Connor114592f2009-09-28 21:32:08 -040019#include "usb.h" // usb_setup
Kevin O'Connord83c87b2013-01-21 01:14:12 -050020#include "paravirt.h" // qemu_cfg_preinit
21#include "xen.h" // xen_preinit
Kevin O'Connor57877482009-12-09 21:00:41 -050022#include "ps2port.h" // ps2port_setup
Gleb Natapov89acfa32010-05-10 11:36:37 +030023#include "virtio-blk.h" // virtio_blk_setup
Paolo Bonzinic5c488f2012-02-27 17:22:23 +010024#include "virtio-scsi.h" // virtio_scsi_setup
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +020025#include "lsi-scsi.h" // lsi_scsi_setup
Paolo Bonzini7a39e722012-08-06 13:15:06 +020026#include "esp-scsi.h" // esp_scsi_setup
Hannes Reinecke2df70bf2012-11-13 15:03:31 +010027#include "megasas.h" // megasas_setup
David Woodhousea2164512013-01-25 19:26:48 -060028#include "post.h" // interface_init
Kevin O'Connor025cabd2010-09-15 21:59:11 -040029
Kevin O'Connor8a0a9722013-01-21 01:53:31 -050030
Kevin O'Connor025cabd2010-09-15 21:59:11 -040031/****************************************************************
Kevin O'Connor6ca04602013-01-21 11:38:49 -050032 * BIOS initialization and hardware setup
Kevin O'Connor025cabd2010-09-15 21:59:11 -040033 ****************************************************************/
34
Kevin O'Connore3677b12008-07-04 15:29:23 -040035static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050036ivt_init(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050038 dprintf(3, "init ivt\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050039
Kevin O'Connor9e4d41c2013-01-21 12:14:29 -050040 // Setup reset-vector entry point (controls legacy reboots).
41 HaveRunPost = 1;
42 outb_cmos(0, CMOS_RESET_CODE);
43
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'Connorcc9e1bf2010-07-28 21:31:38 -040047 SET_IVT(i, FUNC16(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'Connorcc9e1bf2010-07-28 21:31:38 -040051 SET_IVT(i, FUNC16(entry_hwpic1));
Kevin O'Connord21c0892008-11-26 17:02:43 -050052 for (i=0x70; i<=0x77; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040053 SET_IVT(i, FUNC16(entry_hwpic2));
Kevin O'Connord21c0892008-11-26 17:02:43 -050054
55 // Initialize software handlers.
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040056 SET_IVT(0x02, FUNC16(entry_02));
57 SET_IVT(0x10, FUNC16(entry_10));
58 SET_IVT(0x11, FUNC16(entry_11));
59 SET_IVT(0x12, FUNC16(entry_12));
60 SET_IVT(0x13, FUNC16(entry_13_official));
61 SET_IVT(0x14, FUNC16(entry_14));
62 SET_IVT(0x15, FUNC16(entry_15));
63 SET_IVT(0x16, FUNC16(entry_16));
64 SET_IVT(0x17, FUNC16(entry_17));
65 SET_IVT(0x18, FUNC16(entry_18));
66 SET_IVT(0x19, FUNC16(entry_19_official));
67 SET_IVT(0x1a, FUNC16(entry_1a));
68 SET_IVT(0x40, FUNC16(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}
78
79static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050080bda_init(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050081{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050082 dprintf(3, "init bda\n");
83
Kevin O'Connor35ae7262009-01-19 15:44:44 -050084 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050085 memset(bda, 0, sizeof(*bda));
86
Kevin O'Connor49553a42009-05-17 10:31:34 -040087 int esize = EBDA_SIZE_START;
Kevin O'Connorf416fe92009-09-24 20:51:55 -040088 SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
Kevin O'Connor025cabd2010-09-15 21:59:11 -040089 u16 ebda_seg = EBDA_SEGMENT_START;
90 SET_BDA(ebda_seg, ebda_seg);
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050091
Kevin O'Connor9becbf12009-07-12 09:29:46 -040092 // Init ebda
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050093 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050094 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050095 ebda->size = esize;
Kevin O'Connor025cabd2010-09-15 21:59:11 -040096
Kevin O'Connor4bc49972012-05-13 22:58:08 -040097 add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA(ebda_seg, size) * 1024
Kevin O'Connor025cabd2010-09-15 21:59:11 -040098 , E820_RESERVED);
Kevin O'Connorbf2e8c22012-05-28 12:59:58 -040099
100 // Init extra stack
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400101 StackPos = (void*)(&ExtraStack[BUILD_EXTRA_STACK_SIZE] - datalow_base);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500102}
103
David Woodhousea2164512013-01-25 19:26:48 -0600104void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500105interface_init(void)
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400106{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500107 // Running at new code address - do code relocation fixups
108 malloc_fixupreloc_init();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400109
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500110 // Setup romfile items.
111 qemu_romfile_init();
112 coreboot_cbfs_init();
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400113
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500114 // Setup ivt/bda/ebda
115 ivt_init();
116 bda_init();
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400117
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500118 // Other interfaces
119 mathcp_init();
120 boot_init();
121 bios32_init();
122 pmm_init();
123 pnp_init();
124 kbd_init();
125 mouse_init();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400126}
127
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400128// Initialize hardware devices
David Woodhousea2164512013-01-25 19:26:48 -0600129void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500130device_hardware_setup(void)
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400131{
132 usb_setup();
133 ps2port_setup();
134 lpt_setup();
135 serial_setup();
136
137 floppy_setup();
138 ata_setup();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100139 ahci_setup();
Kevin O'Connor89a1efd2011-01-08 12:24:39 -0500140 cbfs_payload_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400141 ramdisk_setup();
Gleb Natapov89acfa32010-05-10 11:36:37 +0300142 virtio_blk_setup();
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100143 virtio_scsi_setup();
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200144 lsi_scsi_setup();
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200145 esp_scsi_setup();
Hannes Reinecke2df70bf2012-11-13 15:03:31 +0100146 megasas_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400147}
148
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500149static void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500150platform_hardware_setup(void)
151{
Kevin O'Connor9e4d41c2013-01-21 12:14:29 -0500152 // Enable CPU caching
153 setcr0(getcr0() & ~(CR0_CD|CR0_NW));
154
155 // Make sure legacy DMA isn't running.
156 dma_setup();
157
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500158 // Init base pc hardware.
159 pic_setup();
160 timer_setup();
161
162 // Initialize pci
163 pci_setup();
164 smm_setup();
165
Kevin O'Connor9f08c902013-02-10 00:25:53 -0500166 // Initialize mtrr and smp
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500167 mtrr_setup();
Kevin O'Connor9f08c902013-02-10 00:25:53 -0500168 smp_setup();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500169
170 // Setup Xen hypercalls
171 xen_hypercall_setup();
172
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500173 // Setup external BIOS interface tables
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500174 if (CONFIG_COREBOOT)
175 coreboot_biostable_setup();
Kevin O'Connor897fb112013-02-07 23:32:48 -0500176 else if (runningOnXen())
Kevin O'Connor8ed2e532013-01-21 02:32:48 -0500177 xen_biostable_setup();
178 else
179 qemu_biostable_setup();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500180}
181
David Woodhousea2164512013-01-25 19:26:48 -0600182void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500183prepareboot(void)
184{
185 // Run BCVs
186 bcv_prepboot();
187
188 // Finalize data structures before boot
189 cdrom_prepboot();
190 pmm_prepboot();
191 malloc_prepboot();
192 memmap_prepboot();
193
194 // Setup bios checksum.
195 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
196}
197
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400198// Begin the boot process by invoking an int0x19 in 16bit mode.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400199void VISIBLE32FLAT
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400200startBoot(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500201{
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400202 // Clear low-memory allocations (required by PMM spec).
203 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
204
205 dprintf(3, "Jump to int19\n");
206 struct bregs br;
207 memset(&br, 0, sizeof(br));
208 br.flags = F_IF;
209 call16_int(0x19, &br);
210}
211
212// Main setup code.
213static void
214maininit(void)
215{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500216 // Initialize internal interfaces.
217 interface_init();
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400218
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500219 // Setup platform devices.
220 platform_hardware_setup();
Kevin O'Connor98032802009-12-09 20:18:31 -0500221
Kevin O'Connor9f08c902013-02-10 00:25:53 -0500222 // Start hardware initialization (if optionrom threading)
223 if (CONFIG_THREAD_OPTIONROMS)
224 device_hardware_setup();
225
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400226 // Run vga option rom
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500227 vgarom_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400228
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400229 // Do hardware initialization (if running synchronously)
Kevin O'Connorb5c7da62013-02-08 00:45:44 -0500230 if (!CONFIG_THREAD_OPTIONROMS) {
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500231 device_hardware_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400232 wait_threads();
233 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500234
Kevin O'Connor98032802009-12-09 20:18:31 -0500235 // Run option roms
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400236 optionrom_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400237
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500238 // Allow user to modify overall boot order.
239 interactive_bootmenu();
240 wait_threads();
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400241
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500242 // Prepare for boot.
243 prepareboot();
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400244
245 // Write protect bios memory.
246 make_bios_readonly();
247
248 // Invoke int 19 to start boot process.
249 startBoot();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500250}
251
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400252
253/****************************************************************
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400254 * POST entry and code relocation
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400255 ****************************************************************/
256
257// Update given relocs for the code at 'dest' with a given 'delta'
258static void
259updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
260{
261 u32 *reloc;
262 for (reloc = rstart; reloc < rend; reloc++)
263 *((u32*)(dest + *reloc)) += delta;
264}
265
Kevin O'Connorbc628092013-02-05 20:57:10 -0500266// Relocate init code and then call a function at its new address.
267// The passed function should be in the "init" section and must not
268// return.
David Woodhousea2164512013-01-25 19:26:48 -0600269void __noreturn
Kevin O'Connorbc628092013-02-05 20:57:10 -0500270reloc_preinit(void *f, void *arg)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400271{
Kevin O'Connorbc628092013-02-05 20:57:10 -0500272 void (*func)(void *) __noreturn = f;
273 if (!CONFIG_RELOCATE_INIT)
274 func(arg);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400275 // Symbols populated by the build.
276 extern u8 code32flat_start[];
Kevin O'Connor46b82622012-05-13 12:10:30 -0400277 extern u8 _reloc_min_align;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400278 extern u32 _reloc_abs_start[], _reloc_abs_end[];
279 extern u32 _reloc_rel_start[], _reloc_rel_end[];
280 extern u32 _reloc_init_start[], _reloc_init_end[];
281 extern u8 code32init_start[], code32init_end[];
Kevin O'Connor46b82622012-05-13 12:10:30 -0400282 extern u32 _reloc_datalow_start[], _reloc_datalow_end[];
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400283 extern u8 datalow_start[], datalow_end[], final_datalow_start[];
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400284
285 // Allocate space for init code.
286 u32 initsize = code32init_end - code32init_start;
Kevin O'Connor46b82622012-05-13 12:10:30 -0400287 u32 codealign = (u32)&_reloc_min_align;
288 void *codedest = memalign_tmp(codealign, initsize);
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400289 if (!codedest)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400290 panic("No space for init relocation.\n");
291
292 // Copy code and update relocs (init absolute, init relative, and runtime)
Kevin O'Connor46b82622012-05-13 12:10:30 -0400293 dprintf(1, "Relocating low data from %p to %p (size %d)\n"
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400294 , datalow_start, final_datalow_start, datalow_end - datalow_start);
Kevin O'Connor46b82622012-05-13 12:10:30 -0400295 updateRelocs(code32flat_start, _reloc_datalow_start, _reloc_datalow_end
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400296 , final_datalow_start - datalow_start);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400297 dprintf(1, "Relocating init from %p to %p (size %d)\n"
Kevin O'Connor46b82622012-05-13 12:10:30 -0400298 , code32init_start, codedest, initsize);
299 s32 delta = codedest - (void*)code32init_start;
300 memcpy(codedest, code32init_start, initsize);
301 updateRelocs(codedest, _reloc_abs_start, _reloc_abs_end, delta);
302 updateRelocs(codedest, _reloc_rel_start, _reloc_rel_end, -delta);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400303 updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
Kevin O'Connorbc628092013-02-05 20:57:10 -0500304 if (f >= (void*)code32init_start && f < (void*)code32init_end)
305 func = f + delta;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400306
Kevin O'Connorbc628092013-02-05 20:57:10 -0500307 // Call function in relocated code.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400308 barrier();
Kevin O'Connorbc628092013-02-05 20:57:10 -0500309 func(arg);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400310}
311
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500312// Setup for code relocation and then relocate.
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400313void VISIBLE32INIT
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400314dopost(void)
315{
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500316 // Detect ram and setup internal malloc.
317 qemu_cfg_preinit();
318 if (CONFIG_COREBOOT)
319 coreboot_preinit();
Kevin O'Connor897fb112013-02-07 23:32:48 -0500320 else if (runningOnXen())
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500321 xen_ramsize_preinit();
322 else
323 qemu_ramsize_preinit();
324 malloc_preinit();
325
326 // Relocate initialization code and call maininit().
327 reloc_preinit(maininit, NULL);
328}
329
Kevin O'Connorb1885fc2013-02-08 21:00:46 -0500330// Entry point for Power On Self Test (POST) - the BIOS initilization
331// phase. This function makes the memory at 0xc0000-0xfffff
332// read/writable and then calls dopost().
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500333void VISIBLE32FLAT
334handle_post(void)
335{
Kevin O'Connorb1885fc2013-02-08 21:00:46 -0500336 debug_serial_preinit();
337 dprintf(1, "Start bios (version %s)\n", VERSION);
338
339 // Check if we are running under Xen.
340 xen_preinit();
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500341
Kevin O'Connora8999452010-09-25 12:48:43 -0400342 // Allow writes to modify bios area (0xf0000)
343 make_bios_writable();
Kevin O'Connora8999452010-09-25 12:48:43 -0400344
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400345 // Now that memory is read/writable - start post process.
346 dopost();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500347}