blob: 0fd41f981510e58702409516bcfe2e3ffdd9064e [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//
6// This file may be distributed under the terms of the GNU GPLv3 license.
7
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'Connorc659fde2008-12-28 23:43:20 -050019#include "boot.h" // IPL
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050020
Kevin O'Connord21c0892008-11-26 17:02:43 -050021void
22__set_irq(int vector, void *loc)
Kevin O'Connore3677b12008-07-04 15:29:23 -040023{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050024 SET_IVT(vector, SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR);
Kevin O'Connore3677b12008-07-04 15:29:23 -040025}
26
Kevin O'Connord21c0892008-11-26 17:02:43 -050027#define set_irq(vector, func) do { \
28 extern void func (void); \
29 __set_irq(vector, func); \
30 } while (0)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040031
Kevin O'Connore3677b12008-07-04 15:29:23 -040032static void
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050033init_ivt()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050034{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050035 dprintf(3, "init ivt\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050036
Kevin O'Connord21c0892008-11-26 17:02:43 -050037 // Initialize all vectors to a dummy handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050038 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040039 for (i=0; i<256; i++)
Kevin O'Connord21c0892008-11-26 17:02:43 -050040 set_irq(i, dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050041
Kevin O'Connord21c0892008-11-26 17:02:43 -050042 // Initialize all hw vectors to a default hw handler.
43 for (i=0x08; i<=0x0f; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050044 set_irq(i, entry_hwpic1);
Kevin O'Connord21c0892008-11-26 17:02:43 -050045 for (i=0x70; i<=0x77; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050046 set_irq(i, entry_hwpic2);
Kevin O'Connord21c0892008-11-26 17:02:43 -050047
48 // Initialize software handlers.
49 set_irq(0x10, entry_10);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050050 set_irq(0x11, entry_11_official);
51 set_irq(0x12, entry_12_official);
52 set_irq(0x13, entry_13_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050053 set_irq(0x14, entry_14);
54 set_irq(0x15, entry_15);
55 set_irq(0x16, entry_16);
56 set_irq(0x17, entry_17);
57 set_irq(0x18, entry_18);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050058 set_irq(0x19, entry_19_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050059 set_irq(0x1a, entry_1a);
60 set_irq(0x1c, entry_1c);
61 set_irq(0x40, entry_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050062
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050063 // set vector 0x79 to zero
64 // this is used by 'gardian angel' protection system
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050065 SET_IVT(0x79, 0, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050066
Kevin O'Connord21c0892008-11-26 17:02:43 -050067 __set_irq(0x1E, &diskette_param_table2);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050068}
69
70static void
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050071init_bda()
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050072{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050073 dprintf(3, "init bda\n");
74
75 struct bios_data_area_s *bda = MAKE_FARPTR(SEG_BDA, 0);
76 memset(bda, 0, sizeof(*bda));
77
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050078 int esize = DIV_ROUND_UP(sizeof(struct extended_bios_data_area_s), 1024);
79 SET_BDA(mem_size_kb, 640 - esize);
80 u16 eseg = FARPTR_TO_SEG((640 - esize) * 1024);
81 SET_BDA(ebda_seg, eseg);
82
83 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -050084 memset(ebda, 0, sizeof(*ebda));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050085 ebda->size = esize;
Kevin O'Connor8c0e3722009-01-02 14:19:43 -050086 SET_IVT(0x41, eseg, offsetof(struct extended_bios_data_area_s, fdpt[0]));
87 SET_IVT(0x46, eseg, offsetof(struct extended_bios_data_area_s, fdpt[1]));
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050088}
89
90static void
Kevin O'Connor9571ac22008-05-17 22:20:27 -040091ram_probe(void)
92{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040093 dprintf(3, "Find memory size\n");
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040094 if (CONFIG_COREBOOT) {
Kevin O'Connorc7812932008-06-08 23:08:12 -040095 coreboot_fill_map();
Kevin O'Connorf64f0db2008-05-18 02:42:58 -040096 } else {
97 // On emulators, get memory size from nvram.
Kevin O'Connor59c35f22008-10-25 23:05:42 -040098 u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
99 | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400100 if (rs)
101 rs += 16 * 1024 * 1024;
102 else
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400103 rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
104 | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400105 + 1 * 1024 * 1024);
Kevin O'Connore7916362008-12-28 22:03:17 -0500106 RamSize = rs;
Kevin O'Connorc7812932008-06-08 23:08:12 -0400107 add_e820(0, rs, E820_RAM);
108
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400109 // Check for memory over 4Gig
110 u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
111 | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
112 | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
Kevin O'Connore7916362008-12-28 22:03:17 -0500113 RamSizeOver4G = high;
Kevin O'Connor59c35f22008-10-25 23:05:42 -0400114 add_e820(0x100000000ull, high, E820_RAM);
115
Kevin O'Connorc7812932008-06-08 23:08:12 -0400116 /* reserve 256KB BIOS area at the end of 4 GB */
117 add_e820(0xfffc0000, 256*1024, E820_RESERVED);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400118 }
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400119
Kevin O'Connorb8d7a472008-06-21 11:43:32 -0400120 // Don't declare any memory between 0xa0000 and 0x100000
121 add_e820(0xa0000, 0x50000, E820_HOLE);
122
Kevin O'Connorc7812932008-06-08 23:08:12 -0400123 // Mark known areas as reserved.
Kevin O'Connor08815372008-12-29 21:16:31 -0500124 u16 ebda_seg = get_ebda_seg();
125 add_e820((u32)MAKE_FARPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -0500126 , E820_RESERVED);
Kevin O'Connore3677b12008-07-04 15:29:23 -0400127 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400128
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500129 if (CONFIG_KVM)
130 // 4 pages before the bios, 3 pages for vmx tss pages, the
131 // other page for EPT real mode pagetable
132 add_e820(0xfffbc000, 4*4096, E820_RESERVED);
133
Kevin O'Connore7916362008-12-28 22:03:17 -0500134 dprintf(1, "Ram Size=0x%08x\n", RamSize);
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400135}
136
137static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400138init_bios_tables(void)
139{
140 if (CONFIG_COREBOOT)
141 // XXX - not supported on coreboot yet.
142 return;
143
Kevin O'Connor0525d292008-07-04 06:18:30 -0400144 create_pirtable();
145
146 mptable_init();
147
148 smbios_init();
149
150 acpi_bios_init();
151}
152
153static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500154init_boot_vectors()
155{
Kevin O'Connor40967022008-07-21 22:23:05 -0400156 if (! CONFIG_BOOT)
157 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400158 dprintf(3, "init boot device ordering\n");
159
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500160 // Floppy drive
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500161 struct ipl_entry_s *ip = &IPL.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500162 ip->type = IPL_TYPE_FLOPPY;
163 ip++;
164
165 // First HDD
166 ip->type = IPL_TYPE_HARDDISK;
167 ip++;
168
169 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500170 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500171 ip->type = IPL_TYPE_CDROM;
172 ip++;
173 }
174
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500175 IPL.count = ip - IPL.table;
176 SET_EBDA(boot_sequence, 0xffff);
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400177 if (CONFIG_COREBOOT) {
178 // XXX - hardcode defaults for coreboot.
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500179 IPL.bootorder = 0x00000231;
180 IPL.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400181 } else {
182 // On emulators, get boot order from nvram.
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500183 IPL.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
184 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400185 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
Kevin O'Connorc659fde2008-12-28 23:43:20 -0500186 IPL.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400187 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500188}
189
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400190// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500191static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500192post()
193{
Kevin O'Connor8c0e3722009-01-02 14:19:43 -0500194 init_ivt();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500195 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500196
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400197 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400198 timer_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400199 mathcp_setup();
200
Kevin O'Connoracf13742008-11-29 11:19:19 -0500201 smp_probe_setup();
Kevin O'Connorcf89e662008-11-28 11:56:37 -0500202
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500203 memmap_setup();
204 ram_probe();
Kevin O'Connor7061eb62009-01-04 21:48:22 -0500205 mtrr_setup();
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500206
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500207 pnp_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400208 vga_setup();
209
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500210 kbd_setup();
211 lpt_setup();
212 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400213 mouse_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500214
Kevin O'Connor0525d292008-07-04 06:18:30 -0400215 pci_bios_setup();
Kevin O'Connor4bbd3b32008-12-10 21:01:00 -0500216 smm_init();
217
Kevin O'Connor0525d292008-07-04 06:18:30 -0400218 init_bios_tables();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400219 memmap_finalize();
220
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400221 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400222 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500223
224 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500225
Kevin O'Connor714325c2008-11-01 20:32:27 -0400226 optionrom_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500227}
228
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400229// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500230void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500231_start()
232{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500233 init_dma();
Kevin O'Connor95576e72008-03-01 09:57:51 -0500234
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400235 debug_serial_setup();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400236 dprintf(1, "Start bios\n");
237
Kevin O'Connoracf13742008-11-29 11:19:19 -0500238 // Allow writes to modify bios area (0xf0000)
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400239 make_bios_writable();
240
241 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500242 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400243
244 // Present the user with a bootup menu.
245 interactive_bootmenu();
246
Kevin O'Connore90f2642008-06-28 12:18:39 -0400247 // Setup bios checksum.
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400248 extern char bios_checksum;
249 bios_checksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
Kevin O'Connore90f2642008-06-28 12:18:39 -0400250
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400251 // Prep for boot process.
252 make_bios_readonly();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400253
254 // Invoke int 19 to start boot process.
255 dprintf(3, "Jump to int19\n");
256 struct bregs br;
257 memset(&br, 0, sizeof(br));
258 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500259}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400260
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400261// Ughh - some older gcc compilers have a bug which causes VISIBLE32
262// functions to not be exported as a global variable - force _start
263// to be global here.
264asm(".global _start");