Add support for 32bit PCI BIOS entry.
[seabios.git] / src / post.c
1 // 32bit code to Power On Self Test (POST) a machine.
2 //
3 // Copyright (C) 2008,2009  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 LGPLv3 license.
7
8 #include "ioport.h" // PORT_*
9 #include "config.h" // CONFIG_*
10 #include "cmos.h" // CMOS_*
11 #include "util.h" // memset
12 #include "biosvar.h" // struct bios_data_area_s
13 #include "disk.h" // floppy_drive_setup
14 #include "ata.h" // ata_setup
15 #include "memmap.h" // add_e820
16 #include "pic.h" // pic_setup
17 #include "pci.h" // create_pirtable
18 #include "acpi.h" // acpi_bios_init
19 #include "bregs.h" // struct bregs
20 #include "mptable.h" // mptable_init
21 #include "boot.h" // IPL
22 #include "usb.h" // usb_setup
23 #include "smbios.h" // smbios_init
24 #include "paravirt.h" // qemu_cfg_port_probe
25 #include "ps2port.h" // ps2port_setup
26
27 void
28 __set_irq(int vector, void *loc)
29 {
30     SET_IVT(vector, SEGOFF(SEG_BIOS, (u32)loc - BUILD_BIOS_ADDR));
31 }
32
33 #define set_irq(vector, func) do {              \
34         extern void func (void);                \
35         __set_irq(vector, func);                \
36     } while (0)
37
38 static void
39 init_ivt()
40 {
41     dprintf(3, "init ivt\n");
42
43     // Initialize all vectors to the default handler.
44     int i;
45     for (i=0; i<256; i++)
46         set_irq(i, entry_iret_official);
47
48     // Initialize all hw vectors to a default hw handler.
49     for (i=0x08; i<=0x0f; i++)
50         set_irq(i, entry_hwpic1);
51     for (i=0x70; i<=0x77; i++)
52         set_irq(i, entry_hwpic2);
53
54     // Initialize software handlers.
55     set_irq(0x02, entry_02);
56     set_irq(0x10, entry_10);
57     set_irq(0x11, entry_11);
58     set_irq(0x12, entry_12);
59     set_irq(0x13, entry_13_official);
60     set_irq(0x14, entry_14);
61     set_irq(0x15, entry_15);
62     set_irq(0x16, entry_16);
63     set_irq(0x17, entry_17);
64     set_irq(0x18, entry_18);
65     set_irq(0x19, entry_19_official);
66     set_irq(0x1a, entry_1a);
67     set_irq(0x40, entry_40);
68
69     // INT 60h-66h reserved for user interrupt
70     for (i=0x60; i<=0x66; i++)
71         SET_IVT(i, SEGOFF(0, 0));
72
73     // set vector 0x79 to zero
74     // this is used by 'gardian angel' protection system
75     SET_IVT(0x79, SEGOFF(0, 0));
76
77     __set_irq(0x1E, &diskette_param_table2);
78 }
79
80 static void
81 init_bda()
82 {
83     dprintf(3, "init bda\n");
84
85     struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
86     memset(bda, 0, sizeof(*bda));
87
88     int esize = EBDA_SIZE_START;
89     SET_BDA(mem_size_kb, BUILD_LOWRAM_END/1024 - esize);
90     u16 eseg = EBDA_SEGMENT_START;
91     SET_BDA(ebda_seg, eseg);
92
93     // Init ebda
94     struct extended_bios_data_area_s *ebda = get_ebda_ptr();
95     memset(ebda, 0, sizeof(*ebda));
96     ebda->size = esize;
97 }
98
99 static void
100 ram_probe(void)
101 {
102     dprintf(3, "Find memory size\n");
103     if (CONFIG_COREBOOT) {
104         coreboot_setup();
105     } else {
106         // On emulators, get memory size from nvram.
107         u32 rs = ((inb_cmos(CMOS_MEM_EXTMEM2_LOW) << 16)
108                   | (inb_cmos(CMOS_MEM_EXTMEM2_HIGH) << 24));
109         if (rs)
110             rs += 16 * 1024 * 1024;
111         else
112             rs = (((inb_cmos(CMOS_MEM_EXTMEM_LOW) << 10)
113                    | (inb_cmos(CMOS_MEM_EXTMEM_HIGH) << 18))
114                   + 1 * 1024 * 1024);
115         RamSize = rs;
116         add_e820(0, rs, E820_RAM);
117
118         // Check for memory over 4Gig
119         u64 high = ((inb_cmos(CMOS_MEM_HIGHMEM_LOW) << 16)
120                     | (inb_cmos(CMOS_MEM_HIGHMEM_MID) << 24)
121                     | ((u64)inb_cmos(CMOS_MEM_HIGHMEM_HIGH) << 32));
122         RamSizeOver4G = high;
123         add_e820(0x100000000ull, high, E820_RAM);
124
125         /* reserve 256KB BIOS area at the end of 4 GB */
126         add_e820(0xfffc0000, 256*1024, E820_RESERVED);
127     }
128
129     // Don't declare any memory between 0xa0000 and 0x100000
130     add_e820(BUILD_LOWRAM_END, BUILD_BIOS_ADDR-BUILD_LOWRAM_END, E820_HOLE);
131
132     // Mark known areas as reserved.
133     u16 ebda_seg = get_ebda_seg();
134     add_e820((u32)MAKE_FLATPTR(ebda_seg, 0), GET_EBDA2(ebda_seg, size) * 1024
135              , E820_RESERVED);
136     add_e820(BUILD_BIOS_ADDR, BUILD_BIOS_SIZE, E820_RESERVED);
137
138     if (kvm_para_available())
139         // 4 pages before the bios, 3 pages for vmx tss pages, the
140         // other page for EPT real mode pagetable
141         add_e820(0xfffbc000, 4*4096, E820_RESERVED);
142
143     dprintf(1, "Ram Size=0x%08x (0x%08x%08x high)\n"
144             , RamSize, (u32)(RamSizeOver4G >> 32), (u32)RamSizeOver4G);
145 }
146
147 static void
148 init_bios_tables(void)
149 {
150     if (CONFIG_COREBOOT) {
151         coreboot_copy_biostable();
152         return;
153     }
154
155     create_pirtable();
156
157     mptable_init();
158
159     smbios_init();
160
161     acpi_bios_init();
162 }
163
164 // Main setup code.
165 static void
166 post()
167 {
168     // Detect and init ram.
169     init_ivt();
170     init_bda();
171     memmap_setup();
172     ram_probe();
173     malloc_setup();
174     thread_setup();
175
176     // Init base pc hardware.
177     pic_setup();
178     timer_setup();
179     mathcp_setup();
180
181     // Initialize smp
182     qemu_cfg_port_probe();
183     smp_probe_setup();
184     mtrr_setup();
185     smp_probe();
186
187     // Initialize pci
188     pci_setup();
189     smm_init();
190
191     // Setup interfaces that option roms may need
192     bios32_setup();
193     pmm_setup();
194     pnp_setup();
195     kbd_setup();
196     mouse_setup();
197     init_bios_tables();
198
199     // Run vga option rom (if running synchronously)
200     if (!CONFIG_THREADS || !CONFIG_THREAD_OPTIONROMS)
201         vga_setup();
202
203     // Initialize hardware devices
204     usb_setup();
205     ps2port_setup();
206     lpt_setup();
207     serial_setup();
208
209     boot_setup();
210     drive_setup();
211     cdemu_setup();
212     floppy_setup();
213     ata_setup();
214     ramdisk_setup();
215
216     // Run option roms
217     if (CONFIG_THREADS && CONFIG_THREAD_OPTIONROMS) {
218         // Run option roms while hw init still in progress.
219         vga_setup();
220         optionrom_setup();
221         wait_threads();
222     } else {
223         // Wait for hw init to finish and run non-vga option roms.
224         wait_threads();
225         optionrom_setup();
226     }
227
228     // Run BCVs and show optional boot menu
229     boot_prep();
230
231     // Finalize data structures before boot
232     pmm_finalize();
233     malloc_finalize();
234     memmap_finalize();
235 }
236
237 // 32-bit entry point.
238 void VISIBLE32FLAT
239 _start()
240 {
241     init_dma();
242
243     debug_serial_setup();
244     dprintf(1, "Start bios (version %s)\n", VERSION);
245
246     // Allow writes to modify bios area (0xf0000)
247     make_bios_writable();
248
249     // Perform main setup code.
250     post();
251
252     // Setup bios checksum.
253     BiosChecksum -= checksum((u8*)BUILD_BIOS_ADDR, BUILD_BIOS_SIZE);
254
255     // Write protect bios memory.
256     make_bios_readonly();
257
258     // Invoke int 19 to start boot process.
259     dprintf(3, "Jump to int19\n");
260     struct bregs br;
261     memset(&br, 0, sizeof(br));
262     br.flags = F_IF;
263     call16_int(0x19, &br);
264 }