blob: 595164466d4bc357bcc32e548557a65db5d97e1d [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'Connor2ad37442008-05-06 19:49:01 -040013#include "ata.h" // hard_drive_setup
Kevin O'Connor3bbcc142008-04-13 17:07:33 -040014#include "disk.h" // floppy_drive_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'Connorf076a3e2008-02-25 22:25:15 -050020
Kevin O'Connor438f6352008-03-30 21:46:53 -040021#define bda ((struct bios_data_area_s *)MAKE_FARPTR(SEG_BDA, 0))
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050022
Kevin O'Connord21c0892008-11-26 17:02:43 -050023void
24__set_irq(int vector, void *loc)
Kevin O'Connore3677b12008-07-04 15:29:23 -040025{
26 SET_BDA(ivecs[vector].seg, SEG_BIOS);
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040027 SET_BDA(ivecs[vector].offset, (u32)loc - BUILD_BIOS_ADDR);
Kevin O'Connore3677b12008-07-04 15:29:23 -040028}
29
Kevin O'Connord21c0892008-11-26 17:02:43 -050030#define set_irq(vector, func) do { \
31 extern void func (void); \
32 __set_irq(vector, func); \
33 } while (0)
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -040034
Kevin O'Connore3677b12008-07-04 15:29:23 -040035static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050036init_bda()
37{
Kevin O'Connor35192dd2008-06-08 19:18:33 -040038 dprintf(3, "init bda\n");
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050039 memset(bda, 0, sizeof(*bda));
40
Kevin O'Connord21c0892008-11-26 17:02:43 -050041 // Initialize all vectors to a dummy handler.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050042 int i;
Kevin O'Connore3677b12008-07-04 15:29:23 -040043 for (i=0; i<256; i++)
Kevin O'Connord21c0892008-11-26 17:02:43 -050044 set_irq(i, dummy_iret_handler);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050045
Kevin O'Connord21c0892008-11-26 17:02:43 -050046 // Initialize all hw vectors to a default hw handler.
47 for (i=0x08; i<=0x0f; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050048 set_irq(i, entry_hwpic1);
Kevin O'Connord21c0892008-11-26 17:02:43 -050049 for (i=0x70; i<=0x77; i++)
Kevin O'Connorffdc9ee2008-12-20 13:10:00 -050050 set_irq(i, entry_hwpic2);
Kevin O'Connord21c0892008-11-26 17:02:43 -050051
52 // Initialize software handlers.
53 set_irq(0x10, entry_10);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050054 set_irq(0x11, entry_11_official);
55 set_irq(0x12, entry_12_official);
56 set_irq(0x13, entry_13_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050057 set_irq(0x14, entry_14);
58 set_irq(0x15, entry_15);
59 set_irq(0x16, entry_16);
60 set_irq(0x17, entry_17);
61 set_irq(0x18, entry_18);
Kevin O'Connorb4f0e892008-12-13 18:33:05 -050062 set_irq(0x19, entry_19_official);
Kevin O'Connord21c0892008-11-26 17:02:43 -050063 set_irq(0x1a, entry_1a);
64 set_irq(0x1c, entry_1c);
65 set_irq(0x40, entry_40);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050066
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050067 // set vector 0x79 to zero
68 // this is used by 'gardian angel' protection system
Kevin O'Connore9061492008-03-11 21:54:18 -040069 SET_BDA(ivecs[0x79].seg, 0);
70 SET_BDA(ivecs[0x79].offset, 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
76init_ebda()
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;
86 SET_BDA(ivecs[0x41].seg, eseg);
Kevin O'Connore9061492008-03-11 21:54:18 -040087 SET_BDA(ivecs[0x41].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040088 , offsetof(struct extended_bios_data_area_s, fdpt[0]));
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -050089 SET_BDA(ivecs[0x46].seg, eseg);
90 SET_BDA(ivecs[0x46].offset
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -040091 , offsetof(struct extended_bios_data_area_s, fdpt[1]));
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'Connorc7812932008-06-08 23:08:12 -040099 coreboot_fill_map();
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'Connorc7812932008-06-08 23:08:12 -0400110 SET_EBDA(ram_size, rs);
111 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));
117 SET_EBDA(ram_size_over4G, high);
118 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
125 add_e820(0xa0000, 0x50000, E820_HOLE);
126
Kevin O'Connorc7812932008-06-08 23:08:12 -0400127 // Mark known areas as reserved.
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -0500128 add_e820((u32)MAKE_FARPTR(GET_BDA(ebda_seg), 0), GET_EBDA(size) * 1024
129 , E820_RESERVED);
Kevin O'Connore3677b12008-07-04 15:29:23 -0400130 add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
Kevin O'Connorc7812932008-06-08 23:08:12 -0400131
132 dprintf(1, "ram_size=0x%08x\n", GET_EBDA(ram_size));
Kevin O'Connor9571ac22008-05-17 22:20:27 -0400133}
134
135static void
Kevin O'Connor0525d292008-07-04 06:18:30 -0400136init_bios_tables(void)
137{
138 if (CONFIG_COREBOOT)
139 // XXX - not supported on coreboot yet.
140 return;
141
Kevin O'Connor0525d292008-07-04 06:18:30 -0400142 create_pirtable();
143
144 mptable_init();
145
146 smbios_init();
147
148 acpi_bios_init();
149}
150
151static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500152init_boot_vectors()
153{
Kevin O'Connor40967022008-07-21 22:23:05 -0400154 if (! CONFIG_BOOT)
155 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400156 dprintf(3, "init boot device ordering\n");
157
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500158 // Floppy drive
Kevin O'Connor4d7c37e2008-12-26 23:50:17 -0500159 struct extended_bios_data_area_s *ebda = get_ebda_ptr();
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400160 struct ipl_entry_s *ip = &ebda->ipl.table[0];
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500161 ip->type = IPL_TYPE_FLOPPY;
162 ip++;
163
164 // First HDD
165 ip->type = IPL_TYPE_HARDDISK;
166 ip++;
167
168 // CDROM
Kevin O'Connor180a9592008-03-04 22:50:53 -0500169 if (CONFIG_CDROM_BOOT) {
Kevin O'Connor38fcbfe2008-02-25 22:30:47 -0500170 ip->type = IPL_TYPE_CDROM;
171 ip++;
172 }
173
Kevin O'Connor56a506d2008-03-29 13:15:36 -0400174 ebda->ipl.count = ip - ebda->ipl.table;
175 ebda->ipl.sequence = 0xffff;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400176 if (CONFIG_COREBOOT) {
177 // XXX - hardcode defaults for coreboot.
178 ebda->ipl.bootorder = 0x00000231;
Kevin O'Connorabc75972008-05-18 00:20:53 -0400179 ebda->ipl.checkfloppysig = 1;
Kevin O'Connorf64f0db2008-05-18 02:42:58 -0400180 } else {
181 // On emulators, get boot order from nvram.
182 ebda->ipl.bootorder = (inb_cmos(CMOS_BIOS_BOOTFLAG2)
183 | ((inb_cmos(CMOS_BIOS_BOOTFLAG1) & 0xf0) << 4));
184 if (!(inb_cmos(CMOS_BIOS_BOOTFLAG1) & 1))
185 ebda->ipl.checkfloppysig = 1;
186 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500187}
188
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400189// Main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500190static void
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500191post()
192{
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500193 init_bda();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500194 init_ebda();
195
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400196 pic_setup();
Kevin O'Connore6eb3f52008-04-13 17:37:41 -0400197 timer_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400198 mathcp_setup();
199
Kevin O'Connoracf13742008-11-29 11:19:19 -0500200 smp_probe_setup();
Kevin O'Connorcf89e662008-11-28 11:56:37 -0500201
Kevin O'Connorceea03c2008-11-08 21:36:35 -0500202 memmap_setup();
203 ram_probe();
204
Kevin O'Connor0c3068d2008-12-21 17:51:36 -0500205 pnp_setup();
Kevin O'Connor714325c2008-11-01 20:32:27 -0400206 vga_setup();
207
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500208 kbd_setup();
209 lpt_setup();
210 serial_setup();
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400211 mouse_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500212
Kevin O'Connor0525d292008-07-04 06:18:30 -0400213 pci_bios_setup();
Kevin O'Connor4bbd3b32008-12-10 21:01:00 -0500214 smm_init();
215
Kevin O'Connor0525d292008-07-04 06:18:30 -0400216 init_bios_tables();
Kevin O'Connorc7812932008-06-08 23:08:12 -0400217 memmap_finalize();
218
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400219 floppy_drive_setup();
Kevin O'Connor9f0d94d2008-04-13 17:25:30 -0400220 hard_drive_setup();
Kevin O'Connora2e73802008-02-27 10:27:00 -0500221
222 init_boot_vectors();
Kevin O'Connora4d35762008-03-08 15:43:03 -0500223
Kevin O'Connor714325c2008-11-01 20:32:27 -0400224 optionrom_setup();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500225}
226
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400227// 32-bit entry point.
Kevin O'Connor19786762008-03-05 21:09:59 -0500228void VISIBLE32
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500229_start()
230{
Kevin O'Connor95576e72008-03-01 09:57:51 -0500231 init_dma();
Kevin O'Connor95576e72008-03-01 09:57:51 -0500232
Kevin O'Connor61d6b062008-06-21 12:15:10 -0400233 debug_serial_setup();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400234 dprintf(1, "Start bios\n");
235
Kevin O'Connoracf13742008-11-29 11:19:19 -0500236 // Allow writes to modify bios area (0xf0000)
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400237 make_bios_writable();
238
239 // Perform main setup code.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500240 post();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400241
242 // Present the user with a bootup menu.
243 interactive_bootmenu();
244
Kevin O'Connore90f2642008-06-28 12:18:39 -0400245 // Setup bios checksum.
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400246 extern char bios_checksum;
247 bios_checksum = -checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE - 1);
Kevin O'Connore90f2642008-06-28 12:18:39 -0400248
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400249 // Prep for boot process.
250 make_bios_readonly();
Kevin O'Connorda4a6482008-06-08 13:48:06 -0400251
252 // Invoke int 19 to start boot process.
253 dprintf(3, "Jump to int19\n");
254 struct bregs br;
255 memset(&br, 0, sizeof(br));
256 call16_int(0x19, &br);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500257}
Kevin O'Connor4b39b822008-05-08 21:47:33 -0400258
Kevin O'Connor2fda7cb2008-07-05 20:41:53 -0400259// Ughh - some older gcc compilers have a bug which causes VISIBLE32
260// functions to not be exported as a global variable - force _start
261// to be global here.
262asm(".global _start");