blob: 9ea5620c977a4277b88dd4c8fd4d5c91e136762b [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'Connor392d2aa2013-09-15 00:14:28 -04008#include "biosvar.h" // SET_BDA
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04009#include "bregs.h" // struct bregs
10#include "config.h" // CONFIG_*
Kevin O'Connorccee6e82013-09-02 21:25:21 -040011#include "fw/paravirt.h" // qemu_cfg_preinit
12#include "fw/xen.h" // xen_preinit
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040013#include "hw/ahci.h" // ahci_setup
14#include "hw/ata.h" // ata_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040015#include "hw/esp-scsi.h" // esp_scsi_setup
16#include "hw/lsi-scsi.h" // lsi_scsi_setup
17#include "hw/megasas.h" // megasas_setup
Evgeny Budilovsky83d60b32013-10-14 18:03:36 +030018#include "hw/pvscsi.h" // pvscsi_setup
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040019#include "hw/pic.h" // pic_setup
Kevin O'Connor5d369d82013-09-02 20:48:46 -040020#include "hw/ps2port.h" // ps2port_setup
Kevin O'Connor8b7861c2013-09-15 02:29:06 -040021#include "hw/rtc.h" // rtc_write
Kevin O'Connor940fc1f2014-01-15 13:42:50 -050022#include "hw/serialio.h" // serial_debug_preinit
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040023#include "hw/usb.h" // usb_setup
Kevin O'Connor5d369d82013-09-02 20:48:46 -040024#include "hw/virtio-blk.h" // virtio_blk_setup
25#include "hw/virtio-scsi.h" // virtio_scsi_setup
Kevin O'Connor9dea5902013-09-14 20:23:54 -040026#include "malloc.h" // malloc_init
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040027#include "memmap.h" // add_e820
28#include "output.h" // dprintf
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040029#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040030#include "util.h" // kbd_init
Kevin O'Connor025cabd2010-09-15 21:59:11 -040031
Kevin O'Connor8a0a9722013-01-21 01:53:31 -050032
Kevin O'Connor025cabd2010-09-15 21:59:11 -040033/****************************************************************
Kevin O'Connor6ca04602013-01-21 11:38:49 -050034 * BIOS initialization and hardware setup
Kevin O'Connor025cabd2010-09-15 21:59:11 -040035 ****************************************************************/
36
Kevin O'Connore3677b12008-07-04 15:29:23 -040037static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050038ivt_init(void)
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'Connor9e4d41c2013-01-21 12:14:29 -050042 // Setup reset-vector entry point (controls legacy reboots).
43 HaveRunPost = 1;
Kevin O'Connor8b7861c2013-09-15 02:29:06 -040044 rtc_write(CMOS_RESET_CODE, 0);
Kevin O'Connor9e4d41c2013-01-21 12:14:29 -050045
Kevin O'Connord67a7032009-01-17 19:37:26 -050046 // Initialize all vectors to the default handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050047 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040048 for (i=0; i<256; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040049 SET_IVT(i, FUNC16(entry_iret_official));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050050
Kevin O'Connord21c0892008-11-26 17:02:43 -050051 // Initialize all hw vectors to a default hw handler.
Kevin O'Connorf465e1e2013-02-23 21:33:36 -050052 for (i=BIOS_HWIRQ0_VECTOR; i<BIOS_HWIRQ0_VECTOR+8; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040053 SET_IVT(i, FUNC16(entry_hwpic1));
Kevin O'Connorf465e1e2013-02-23 21:33:36 -050054 for (i=BIOS_HWIRQ8_VECTOR; i<BIOS_HWIRQ8_VECTOR+8; i++)
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040055 SET_IVT(i, FUNC16(entry_hwpic2));
Kevin O'Connord21c0892008-11-26 17:02:43 -050056
57 // Initialize software handlers.
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040058 SET_IVT(0x02, FUNC16(entry_02));
59 SET_IVT(0x10, FUNC16(entry_10));
60 SET_IVT(0x11, FUNC16(entry_11));
61 SET_IVT(0x12, FUNC16(entry_12));
62 SET_IVT(0x13, FUNC16(entry_13_official));
63 SET_IVT(0x14, FUNC16(entry_14));
Kevin O'Connor3fcabf02014-05-08 18:32:32 -040064 SET_IVT(0x15, FUNC16(entry_15_official));
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040065 SET_IVT(0x16, FUNC16(entry_16));
66 SET_IVT(0x17, FUNC16(entry_17));
67 SET_IVT(0x18, FUNC16(entry_18));
68 SET_IVT(0x19, FUNC16(entry_19_official));
Kevin O'Connor922aa1b2013-03-02 04:02:11 -050069 SET_IVT(0x1a, FUNC16(entry_1a_official));
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -040070 SET_IVT(0x40, FUNC16(entry_40));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050071
Kevin O'Connorb164d2c2009-12-26 23:24:52 -050072 // INT 60h-66h reserved for user interrupt
73 for (i=0x60; i<=0x66; i++)
74 SET_IVT(i, SEGOFF(0, 0));
75
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050076 // set vector 0x79 to zero
77 // this is used by 'gardian angel' protection system
Kevin O'Connor9f985422009-09-09 11:34:39 -040078 SET_IVT(0x79, SEGOFF(0, 0));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050079}
80
81static void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050082bda_init(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'Connor025cabd2010-09-15 21:59:11 -040090 u16 ebda_seg = EBDA_SEGMENT_START;
Kevin O'Connor2b0fb8c2013-08-07 23:03:47 -040091 extern u8 final_varlow_start[];
92 if (!CONFIG_MALLOC_UPPERMEMORY)
93 ebda_seg = FLATPTR_TO_SEG(ALIGN_DOWN((u32)final_varlow_start, 1024)
94 - EBDA_SIZE_START*1024);
Kevin O'Connor025cabd2010-09-15 21:59:11 -040095 SET_BDA(ebda_seg, ebda_seg);
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050096
Kevin O'Connor2b0fb8c2013-08-07 23:03:47 -040097 SET_BDA(mem_size_kb, ebda_seg / (1024/16));
98
Kevin O'Connor9becbf12009-07-12 09:29:46 -040099 // Init ebda
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -0500100 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500101 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -0500102 ebda->size = esize;
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400103
Kevin O'Connor2b0fb8c2013-08-07 23:03:47 -0400104 add_e820((u32)ebda, BUILD_LOWRAM_END-(u32)ebda, E820_RESERVED);
Kevin O'Connorbf2e8c22012-05-28 12:59:58 -0400105
106 // Init extra stack
Kevin O'Connorc9243442013-02-17 13:58:28 -0500107 StackPos = (void*)(&ExtraStack[BUILD_EXTRA_STACK_SIZE] - zonelow_base);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500108}
109
David Woodhousea2164512013-01-25 19:26:48 -0600110void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500111interface_init(void)
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400112{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500113 // Running at new code address - do code relocation fixups
Kevin O'Connor6afc6f82013-02-19 01:02:50 -0500114 malloc_init();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400115
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500116 // Setup romfile items.
Kevin O'Connorb840ba92013-02-09 20:09:22 -0500117 qemu_cfg_init();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500118 coreboot_cbfs_init();
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400119
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500120 // Setup ivt/bda/ebda
121 ivt_init();
122 bda_init();
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400123
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500124 // Other interfaces
Kevin O'Connord29ce622014-04-07 12:15:34 -0400125 thread_init();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500126 boot_init();
127 bios32_init();
128 pmm_init();
129 pnp_init();
130 kbd_init();
131 mouse_init();
Kevin O'Connor0525d292008-07-04 06:18:30 -0400132}
133
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400134// Initialize hardware devices
David Woodhousea2164512013-01-25 19:26:48 -0600135void
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500136device_hardware_setup(void)
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400137{
138 usb_setup();
139 ps2port_setup();
140 lpt_setup();
141 serial_setup();
142
143 floppy_setup();
144 ata_setup();
Gerd Hoffmannd52fdf62010-11-29 09:42:13 +0100145 ahci_setup();
Kevin O'Connor72691a52014-12-16 09:55:16 -0500146 sdcard_setup();
Kevin O'Connor89a1efd2011-01-08 12:24:39 -0500147 cbfs_payload_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400148 ramdisk_setup();
Gleb Natapov89acfa32010-05-10 11:36:37 +0300149 virtio_blk_setup();
Paolo Bonzinic5c488f2012-02-27 17:22:23 +0100150 virtio_scsi_setup();
Gerd Hoffmann9d6bac12012-07-20 10:59:25 +0200151 lsi_scsi_setup();
Paolo Bonzini7a39e722012-08-06 13:15:06 +0200152 esp_scsi_setup();
Hannes Reinecke2df70bf2012-11-13 15:03:31 +0100153 megasas_setup();
Evgeny Budilovsky83d60b32013-10-14 18:03:36 +0300154 pvscsi_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400155}
156
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500157static void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500158platform_hardware_setup(void)
159{
Kevin O'Connor9e4d41c2013-01-21 12:14:29 -0500160 // Enable CPU caching
161 setcr0(getcr0() & ~(CR0_CD|CR0_NW));
162
163 // Make sure legacy DMA isn't running.
164 dma_setup();
165
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500166 // Init base pc hardware.
167 pic_setup();
Kevin O'Connor3a735ba2013-02-10 00:35:01 -0500168 mathcp_setup();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500169 timer_setup();
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400170 clock_setup();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500171
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500172 // Platform specific setup
173 qemu_platform_setup();
174 coreboot_platform_setup();
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500175}
176
David Woodhousea2164512013-01-25 19:26:48 -0600177void
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500178prepareboot(void)
179{
180 // Run BCVs
181 bcv_prepboot();
182
183 // Finalize data structures before boot
184 cdrom_prepboot();
185 pmm_prepboot();
186 malloc_prepboot();
187 memmap_prepboot();
188
Kevin O'Connor6c68e7a2014-04-19 12:22:22 -0400189 HaveRunPost = 2;
190
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500191 // Setup bios checksum.
192 BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
193}
194
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400195// Begin the boot process by invoking an int0x19 in 16bit mode.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400196void VISIBLE32FLAT
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400197startBoot(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500198{
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400199 // Clear low-memory allocations (required by PMM spec).
200 memset((void*)BUILD_STACK_ADDR, 0, BUILD_EBDA_MINIMUM - BUILD_STACK_ADDR);
201
202 dprintf(3, "Jump to int19\n");
203 struct bregs br;
204 memset(&br, 0, sizeof(br));
205 br.flags = F_IF;
206 call16_int(0x19, &br);
207}
208
209// Main setup code.
210static void
211maininit(void)
212{
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500213 // Initialize internal interfaces.
214 interface_init();
Kevin O'Connor59d6ca52012-05-31 00:20:55 -0400215
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500216 // Setup platform devices.
217 platform_hardware_setup();
Kevin O'Connor98032802009-12-09 20:18:31 -0500218
Kevin O'Connord29ce622014-04-07 12:15:34 -0400219 // Start hardware initialization (if threads allowed during optionroms)
220 if (threads_during_optionroms())
Kevin O'Connor9f08c902013-02-10 00:25:53 -0500221 device_hardware_setup();
222
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400223 // Run vga option rom
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500224 vgarom_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400225
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400226 // Do hardware initialization (if running synchronously)
Kevin O'Connord29ce622014-04-07 12:15:34 -0400227 if (!threads_during_optionroms()) {
Kevin O'Connord83c87b2013-01-21 01:14:12 -0500228 device_hardware_setup();
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400229 wait_threads();
230 }
Kevin O'Connora2e73802008-02-27 10:27:00 -0500231
Kevin O'Connor98032802009-12-09 20:18:31 -0500232 // Run option roms
Kevin O'Connore438b0c2010-05-01 12:20:33 -0400233 optionrom_setup();
Kevin O'Connore54ee382009-07-26 19:33:13 -0400234
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500235 // Allow user to modify overall boot order.
236 interactive_bootmenu();
237 wait_threads();
Kevin O'Connor0bf92702009-08-01 11:45:37 -0400238
Kevin O'Connor8a0a9722013-01-21 01:53:31 -0500239 // Prepare for boot.
240 prepareboot();
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400241
242 // Write protect bios memory.
243 make_bios_readonly();
244
245 // Invoke int 19 to start boot process.
246 startBoot();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500247}
248
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400249
250/****************************************************************
Kevin O'Connor87b533b2011-07-10 22:35:07 -0400251 * POST entry and code relocation
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400252 ****************************************************************/
253
254// Update given relocs for the code at 'dest' with a given 'delta'
255static void
256updateRelocs(void *dest, u32 *rstart, u32 *rend, u32 delta)
257{
258 u32 *reloc;
259 for (reloc = rstart; reloc < rend; reloc++)
260 *((u32*)(dest + *reloc)) += delta;
261}
262
Kevin O'Connorbc628092013-02-05 20:57:10 -0500263// Relocate init code and then call a function at its new address.
264// The passed function should be in the "init" section and must not
265// return.
David Woodhousea2164512013-01-25 19:26:48 -0600266void __noreturn
Kevin O'Connorbc628092013-02-05 20:57:10 -0500267reloc_preinit(void *f, void *arg)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400268{
Kevin O'Connorbc628092013-02-05 20:57:10 -0500269 void (*func)(void *) __noreturn = f;
270 if (!CONFIG_RELOCATE_INIT)
271 func(arg);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400272 // Symbols populated by the build.
273 extern u8 code32flat_start[];
Kevin O'Connor46b82622012-05-13 12:10:30 -0400274 extern u8 _reloc_min_align;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400275 extern u32 _reloc_abs_start[], _reloc_abs_end[];
276 extern u32 _reloc_rel_start[], _reloc_rel_end[];
277 extern u32 _reloc_init_start[], _reloc_init_end[];
278 extern u8 code32init_start[], code32init_end[];
279
280 // Allocate space for init code.
281 u32 initsize = code32init_end - code32init_start;
Kevin O'Connor46b82622012-05-13 12:10:30 -0400282 u32 codealign = (u32)&_reloc_min_align;
283 void *codedest = memalign_tmp(codealign, initsize);
Kevin O'Connorc91da7a2012-06-08 21:14:19 -0400284 if (!codedest)
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400285 panic("No space for init relocation.\n");
286
287 // Copy code and update relocs (init absolute, init relative, and runtime)
288 dprintf(1, "Relocating init from %p to %p (size %d)\n"
Kevin O'Connor46b82622012-05-13 12:10:30 -0400289 , code32init_start, codedest, initsize);
290 s32 delta = codedest - (void*)code32init_start;
291 memcpy(codedest, code32init_start, initsize);
292 updateRelocs(codedest, _reloc_abs_start, _reloc_abs_end, delta);
293 updateRelocs(codedest, _reloc_rel_start, _reloc_rel_end, -delta);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400294 updateRelocs(code32flat_start, _reloc_init_start, _reloc_init_end, delta);
Kevin O'Connorbc628092013-02-05 20:57:10 -0500295 if (f >= (void*)code32init_start && f < (void*)code32init_end)
296 func = f + delta;
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400297
Kevin O'Connorbc628092013-02-05 20:57:10 -0500298 // Call function in relocated code.
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400299 barrier();
Kevin O'Connorbc628092013-02-05 20:57:10 -0500300 func(arg);
Kevin O'Connor7f55fd32010-09-25 11:34:06 -0400301}
302
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500303// Setup for code relocation and then relocate.
Kevin O'Connor025cabd2010-09-15 21:59:11 -0400304void VISIBLE32INIT
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400305dopost(void)
306{
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500307 // Detect ram and setup internal malloc.
Kevin O'Connora2a86e22013-02-13 19:35:12 -0500308 qemu_preinit();
309 coreboot_preinit();
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500310 malloc_preinit();
311
312 // Relocate initialization code and call maininit().
313 reloc_preinit(maininit, NULL);
314}
315
Kevin O'Connorb1885fc2013-02-08 21:00:46 -0500316// Entry point for Power On Self Test (POST) - the BIOS initilization
317// phase. This function makes the memory at 0xc0000-0xfffff
318// read/writable and then calls dopost().
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500319void VISIBLE32FLAT
320handle_post(void)
321{
Kevin O'Connor4cf20672013-02-18 11:49:53 -0500322 if (!CONFIG_QEMU && !CONFIG_COREBOOT)
323 return;
324
Kevin O'Connor940fc1f2014-01-15 13:42:50 -0500325 serial_debug_preinit();
Kevin O'Connorbb1fcb42014-01-15 13:52:14 -0500326 debug_banner();
Kevin O'Connorb1885fc2013-02-08 21:00:46 -0500327
328 // Check if we are running under Xen.
329 xen_preinit();
Kevin O'Connor6ca04602013-01-21 11:38:49 -0500330
Kevin O'Connora8999452010-09-25 12:48:43 -0400331 // Allow writes to modify bios area (0xf0000)
332 make_bios_writable();
Kevin O'Connora8999452010-09-25 12:48:43 -0400333
Kevin O'Connor9acdc282011-09-20 19:42:14 -0400334 // Now that memory is read/writable - start post process.
335 dopost();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500336}