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