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