blob: 73cd7988733d12ac0fa8cbc2398e97ab018713db [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 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'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'Connorc7812932008-06-08 23:08:12 -040014#include "memmap.h" // add_e820
Kevin O'Connorf54c1502008-06-14 15:56:16 -040015#include "pic.h" // pic_setup
Kevin O'Connor0525d292008-07-04 06:18:30 -040016#include "pci.h" // create_pirtable
17#include "acpi.h" // acpi_bios_init
Kevin O'Connor9521e262008-07-04 13:04:29 -040018#include "bregs.h" // struct bregs
Kevin O'Connor35746442009-02-27 20:54:51 -050019#include "mptable.h" // mptable_init
Kevin O'Connorc659fde2008-12-28 23:43:20 -050020#include "boot.h" // IPL
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050021
Kevin O'Connord21c0892008-11-26 17:02:43 -050022void
23__set_irq(int vector, void *loc)
Kevin O'Connore3677b12008-07-04 15:29:23 -040024{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050025 SET_IVT(vector, SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR);
Kevin O'Connore3677b12008-07-04 15:29:23 -040026}
27
Kevin O'Connord21c0892008-11-26 17:02:43 -050028#define set_irq(vector, func) do { \
29 extern void func (void); \
30 __set_irq(vector, func); \
31 } while (0)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040032
Kevin O'Connore3677b12008-07-04 15:29:23 -040033static void
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050034init_ivt()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050035{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050036 dprintf(3, "init ivt\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037
Kevin O'Connord67a7032009-01-17 19:37:26 -050038 // Initialize all vectors to the default handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050039 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040040 for (i=0; i<256; i++)
Kevin O'Connord67a7032009-01-17 19:37:26 -050041 set_irq(i, entry_iret_official);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050042
Kevin O'Connord21c0892008-11-26 17:02:43 -050043 // Initialize all hw vectors to a default hw handler.
44 for (i=0x08; i<=0x0f; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050045 set_irq(i, entry_hwpic1);
Kevin O'Connord21c0892008-11-26 17:02:43 -050046 for (i=0x70; i<=0x77; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050047 set_irq(i, entry_hwpic2);
Kevin O'Connord21c0892008-11-26 17:02:43 -050048
49 // Initialize software handlers.
Kevin O'Connor75f49b32009-03-07 00:07:24 -050050 set_irq(0x02, entry_02);
Kevin O'Connord21c0892008-11-26 17:02:43 -050051 set_irq(0x10, entry_10);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050052 set_irq(0x11, entry_11_official);
53 set_irq(0x12, entry_12_official);
54 set_irq(0x13, entry_13_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050055 set_irq(0x14, entry_14);
56 set_irq(0x15, entry_15);
57 set_irq(0x16, entry_16);
58 set_irq(0x17, entry_17);
59 set_irq(0x18, entry_18);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050060 set_irq(0x19, entry_19_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050061 set_irq(0x1a, entry_1a);
Kevin O'Connord21c0892008-11-26 17:02:43 -050062 set_irq(0x40, entry_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050063
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050064 // set vector 0x79 to zero
65 // this is used by 'gardian angel' protection system
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050066 SET_IVT(0x79, 0, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050067
Kevin O'Connord21c0892008-11-26 17:02:43 -050068 __set_irq(0x1E, &diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050069}
70
71static void
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050072init_bda()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050073{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050074 dprintf(3, "init bda\n");
75
Kevin O'Connor35ae7262009-01-19 15:44:44 -050076 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050077 memset(bda, 0, sizeof(*bda));
78
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050079 int esize = DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024);
80 SET_BDA(mem_size_kb, 640 - esize);
Kevin O'Connor35ae7262009-01-19 15:44:44 -050081 u16 eseg = FLATPTR_TO_SEG((640 - esize) * 1024);
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050082 SET_BDA(ebda_seg, eseg);
83
84 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050085 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050086 ebda->size = esize;
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050087 SET_IVT(0x41, eseg, offsetof(struct extended_bios_data_area_s, fdpt[0]));
88 SET_IVT(0x46, eseg, offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050089}
90
91static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -040092ram_probe(void)
93{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040094 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040095 if (CONFIG_COREBOOT) {
Kevin O'Connorc7812932008-06-08 23:08:12 -040096 coreboot_fill_map();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040097 } else {
98 // On emulators, get memory size from nvram.
Kevin O'Connor59c35f22008-10-25 23:05:42 -040099 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
100 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400101 if (rs)
102 rs += 16 * 1024 * 1024;
103 else
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400104 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
105 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400106 + 1 * 1024 * 1024);
Kevin O'Connore7916362008-12-28 22:03:17 -0500107 RamSize = rs;
Kevin O'Connorc7812932008-06-08 23:08:12 -0400108 add_e820(0, rs, E820_RAM);
109
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400110 // Check for memory over 4Gig
111 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
112 | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
113 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
Kevin O'Connore7916362008-12-28 22:03:17 -0500114 RamSizeOver4G = high;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400115 add_e820(0x100000000ull, high, E820_RAM);
116
Kevin O'Connorc7812932008-06-08 23:08:12 -0400117 /* reserve 256KB BIOS area at the end of 4 GB */
118 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400119 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400120
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400121 // Don't declare any memory between 0xa0000 and 0x100000
122 add_e820(0xa0000, 0x50000, E820_HOLE);
123
Kevin O'Connorc7812932008-06-08 23:08:12 -0400124 // Mark known areas as reserved.
Kevin O'Connor08815372008-12-29 21:16:31 -0500125 u16 ebda_seg = get_ebda_seg();
Kevin O'Connor35ae7262009-01-19 15:44:44 -0500126 add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -0500127 , E820_RESERVED);
Kevin O'Connore3677b12008-07-04 15:29:23 -0400128 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400129
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500130 if (CONFIG_KVM)
131 // 4 pages before the bios, 3 pages for vmx tss pages, the
132 // other page for EPT real mode pagetable
133 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
134
Kevin O'Connore7916362008-12-28 22:03:17 -0500135 dprintf(1, "Ram Size=0x%08x\n", RamSize);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400136}
137
138static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400139init_bios_tables(void)
140{
141 if (CONFIG_COREBOOT)
142 // XXX - not supported on coreboot yet.
143 return;
144
Kevin O'Connor0525d292008-07-04 06:18:30 -0400145 create_pirtable();
146
147 mptable_init();
148
149 smbios_init();
150
151 acpi_bios_init();
152}
153
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400154// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500155static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500156post()
157{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -0500158 init_ivt();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500159 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500160
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400161 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400162 timer_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400163 mathcp_setup();
164
Kevin O'Connoracf13742008-11-29 11:19:19 -0500165 smp_probe_setup();
Kevin O'Connorcf89e662008-11-28 11:56:37 -0500166
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500167 memmap_setup();
168 ram_probe();
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500169 mtrr_setup();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500170
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500171 pnp_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400172 vga_setup();
173
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500174 kbd_setup();
175 lpt_setup();
176 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400177 mouse_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500178
Kevin O'Connor0525d292008-07-04 06:18:30 -0400179 pci_bios_setup();
Kevin O'Connor4bbd3b32008-12-10 21:01:00 -0500180 smm_init();
181
Kevin O'Connor0525d292008-07-04 06:18:30 -0400182 init_bios_tables();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400183 memmap_finalize();
184
Kevin O'Connor9f4e1d92009-02-08 15:44:08 -0500185 boot_setup();
186
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400187 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400188 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500189
Kevin O'Connor714325c2008-11-01 20:32:27 -0400190 optionrom_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500191}
192
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400193// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500194void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500195_start()
196{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500197 init_dma();
Kevin O'Connor95576e72008-03-01 09:57:51 -0500198
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400199 debug_serial_setup();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400200 dprintf(1, "Start bios\n");
201
Kevin O'Connoracf13742008-11-29 11:19:19 -0500202 // Allow writes to modify bios area (0xf0000)
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400203 make_bios_writable();
204
205 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500206 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400207
Kevin O'Connor0a924122009-02-08 19:43:47 -0500208 // Run BCVs
209 boot_prep();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400210
Kevin O'Connore90f2642008-06-28 12:18:39 -0400211 // Setup bios checksum.
Kevin O'Connor30853762009-01-17 18:49:20 -0500212 BiosChecksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
Kevin O'Connore90f2642008-06-28 12:18:39 -0400213
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400214 // Prep for boot process.
215 make_bios_readonly();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400216
217 // Invoke int 19 to start boot process.
218 dprintf(3, "Jump to int19\n");
219 struct bregs br;
220 memset(&br, 0, sizeof(br));
221 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500222}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400223
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400224// Ughh - some older gcc compilers have a bug which causes VISIBLE32
225// functions to not be exported as a global variable - force _start
226// to be global here.
227asm(".global _start");