blob: dda6af4a749c2e17399ae7d4bc22b531e9587116 [file] [log] [blame]
David Woodhouse118469a2013-01-25 19:46:25 -06001// Compatibility Support Module (CSM) for UEFI / EDK-II
2//
3// Copyright © 2013 Intel Corporation
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
Kevin O'Connor2d2fa312013-09-14 21:55:26 -04007#include "biosvar.h"
8#include "boot.h"
9#include "bregs.h"
David Woodhouse118469a2013-01-25 19:46:25 -060010#include "config.h" // CONFIG_*
Kevin O'Connor5d369d82013-09-02 20:48:46 -040011#include "hw/pci.h"
Kevin O'Connor5d369d82013-09-02 20:48:46 -040012#include "hw/pic.h"
Kevin O'Connor9dea5902013-09-14 20:23:54 -040013#include "malloc.h" // csm_malloc_preinit
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040014#include "memmap.h"
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040015#include "output.h" // dprintf
16#include "post.h"
Kevin O'Connor3df600b2013-09-14 19:28:55 -040017#include "stacks.h" // wait_threads
Kevin O'Connor5a7545c2013-09-14 22:54:44 -040018#include "std/acpi.h" // RSDP_SIGNATURE
Kevin O'Connor8fb3a5e2013-09-14 22:27:14 -040019#include "std/optionrom.h" // struct rom_header
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040020#include "util.h" // copy_smbios
David Woodhouse118469a2013-01-25 19:46:25 -060021
Kevin O'Connorf6d700d2013-09-14 22:38:00 -040022#define UINT8 u8
23#define UINT16 u16
24#define UINT32 u32
25#include "std/LegacyBios.h"
26
Kevin O'Connor89a2f962013-02-18 23:36:03 -050027struct rsdp_descriptor csm_rsdp VARFSEG __aligned(16);
David Woodhouse118469a2013-01-25 19:46:25 -060028
Kevin O'Connor89a2f962013-02-18 23:36:03 -050029EFI_COMPATIBILITY16_TABLE csm_compat_table VARFSEG __aligned(16) = {
David Woodhouse118469a2013-01-25 19:46:25 -060030 .Signature = 0x24454649,
31 .TableChecksum = 0 /* Filled in by checkrom.py */,
32 .TableLength = sizeof(csm_compat_table),
33 .Compatibility16CallSegment = SEG_BIOS,
34 .Compatibility16CallOffset = 0 /* Filled in by checkrom.py */,
35 .OemIdStringPointer = (u32)"SeaBIOS",
36 .AcpiRsdPtrPointer = (u32)&csm_rsdp,
37};
38
David Woodhouse118469a2013-01-25 19:46:25 -060039EFI_TO_COMPATIBILITY16_INIT_TABLE *csm_init_table;
40EFI_TO_COMPATIBILITY16_BOOT_TABLE *csm_boot_table;
41
Kevin O'Connoraa7c2342013-07-14 15:07:21 -040042static u16 PICMask = PIC_IRQMASK_DEFAULT;
43
David Woodhouse4b1d2be2013-02-10 00:51:56 +000044extern void __csm_return(struct bregs *regs) __noreturn;
45
46static void
47csm_return(struct bregs *regs)
48{
49 dprintf(3, "handle_csm returning AX=%04x\n", regs->ax);
50
Kevin O'Connoraa7c2342013-07-14 15:07:21 -040051 PICMask = pic_irqmask_read();
David Woodhouse4b1d2be2013-02-10 00:51:56 +000052 __csm_return(regs);
53}
David Woodhouse118469a2013-01-25 19:46:25 -060054
55static void
56csm_maininit(struct bregs *regs)
57{
58 interface_init();
59 pci_probe_devices();
60
61 csm_compat_table.PnPInstallationCheckSegment = SEG_BIOS;
62 csm_compat_table.PnPInstallationCheckOffset = get_pnp_offset();
63
64 regs->ax = 0;
65
David Woodhouse118469a2013-01-25 19:46:25 -060066 csm_return(regs);
67}
68
69/* Legacy16InitializeYourself */
70static void
71handle_csm_0000(struct bregs *regs)
72{
73 dprintf(3, "Legacy16InitializeYourself table %04x:%04x\n", regs->es,
74 regs->bx);
75
76 csm_init_table = MAKE_FLATPTR(regs->es, regs->bx);
77
78 dprintf(3, "BiosLessThan1MB %08x\n", csm_init_table->BiosLessThan1MB);
79 dprintf(3, "HiPmmMemory %08x\n", csm_init_table->HiPmmMemory);
80 dprintf(3, "HiPmmMemorySize %08x\n", csm_init_table->HiPmmMemorySizeInBytes);
81 dprintf(3, "ReverseThunk %04x:%04x\n", csm_init_table->ReverseThunkCallSegment,
82 csm_init_table->ReverseThunkCallOffset);
83 dprintf(3, "NumE820Entries %08x\n", csm_init_table->NumberE820Entries);
84 dprintf(3, "OsMemoryAbove1M %08x\n", csm_init_table->OsMemoryAbove1Mb);
85 dprintf(3, "ThunkStart %08x\n", csm_init_table->ThunkStart);
86 dprintf(3, "ThunkSize %08x\n", csm_init_table->ThunkSizeInBytes);
87 dprintf(3, "LoPmmMemory %08x\n", csm_init_table->LowPmmMemory);
88 dprintf(3, "LoPmmMemorySize %08x\n", csm_init_table->LowPmmMemorySizeInBytes);
89
90 csm_malloc_preinit(csm_init_table->LowPmmMemory,
91 csm_init_table->LowPmmMemorySizeInBytes,
92 csm_init_table->HiPmmMemory,
93 csm_init_table->HiPmmMemorySizeInBytes);
94 reloc_preinit(csm_maininit, regs);
95}
96
97/* Legacy16UpdateBbs */
98static void
99handle_csm_0001(struct bregs *regs)
100{
David Woodhouse84b7dcb2013-02-14 09:17:17 +0000101 if (!CONFIG_BOOT) {
102 regs->ax = 1;
103 return;
104 }
105
David Woodhouse118469a2013-01-25 19:46:25 -0600106 dprintf(3, "Legacy16UpdateBbs table %04x:%04x\n", regs->es, regs->bx);
107
108 csm_boot_table = MAKE_FLATPTR(regs->es, regs->bx);
109 dprintf(3, "MajorVersion %04x\n", csm_boot_table->MajorVersion);
110 dprintf(3, "MinorVersion %04x\n", csm_boot_table->MinorVersion);
111 dprintf(3, "AcpiTable %08x\n", csm_boot_table->AcpiTable);
112 dprintf(3, "SmbiosTable %08x\n", csm_boot_table->SmbiosTable);
113 dprintf(3, "SmbiosTableLength %08x\n", csm_boot_table->SmbiosTableLength);
114// dprintf(3, "SioData %08x\n", csm_boot_table->SioData);
115 dprintf(3, "DevicePathType %04x\n", csm_boot_table->DevicePathType);
116 dprintf(3, "PciIrqMask %04x\n", csm_boot_table->PciIrqMask);
117 dprintf(3, "NumberE820Entries %08x\n", csm_boot_table->NumberE820Entries);
118// dprintf(3, "HddInfo %08x\n", csm_boot_table->HddInfo);
119 dprintf(3, "NumberBbsEntries %08x\n", csm_boot_table->NumberBbsEntries);
120 dprintf(3, "BBsTable %08x\n", csm_boot_table->BbsTable);
121 dprintf(3, "SmmTable %08x\n", csm_boot_table->SmmTable);
122 dprintf(3, "OsMemoryAbove1Mb %08x\n", csm_boot_table->OsMemoryAbove1Mb);
123 dprintf(3, "UnconventionalDeviceTable %08x\n", csm_boot_table->UnconventionalDeviceTable);
124
125 regs->ax = 0;
126}
127
128/* PrepareToBoot */
129static void
130handle_csm_0002(struct bregs *regs)
131{
David Woodhouse84b7dcb2013-02-14 09:17:17 +0000132 if (!CONFIG_BOOT) {
133 regs->ax = 1;
134 return;
135 }
136
David Woodhouse118469a2013-01-25 19:46:25 -0600137 dprintf(3, "PrepareToBoot table %04x:%04x\n", regs->es, regs->bx);
138
139 struct e820entry *p = (void *)csm_compat_table.E820Pointer;
140 int i;
141 for (i=0; i < csm_compat_table.E820Length / sizeof(struct e820entry); i++)
142 add_e820(p[i].start, p[i].size, p[i].type);
143
Kevin O'Connore52ad392013-02-20 23:48:22 -0500144 if (csm_init_table->HiPmmMemorySizeInBytes > BUILD_MAX_HIGHTABLE) {
David Woodhouse118469a2013-01-25 19:46:25 -0600145 u32 hi_pmm_end = csm_init_table->HiPmmMemory + csm_init_table->HiPmmMemorySizeInBytes;
Kevin O'Connore52ad392013-02-20 23:48:22 -0500146 add_e820(hi_pmm_end - BUILD_MAX_HIGHTABLE, BUILD_MAX_HIGHTABLE, E820_RESERVED);
David Woodhouse118469a2013-01-25 19:46:25 -0600147 }
148
149 // For PCIBIOS 1ab10e
150 if (csm_compat_table.IrqRoutingTablePointer &&
151 csm_compat_table.IrqRoutingTableLength) {
152 PirAddr = (void *)csm_compat_table.IrqRoutingTablePointer;
153 dprintf(3, "CSM PIRQ table at %p\n", PirAddr);
154 }
155
David Woodhoused304fe42013-02-23 00:24:48 +0000156 // For find_resume_vector()... and find_acpi_features()
David Woodhouse118469a2013-01-25 19:46:25 -0600157 if (csm_rsdp.signature == RSDP_SIGNATURE) {
158 RsdpAddr = &csm_rsdp;
159 dprintf(3, "CSM ACPI RSDP at %p\n", RsdpAddr);
David Woodhouse31fe26e2013-02-10 01:15:01 +0000160
David Woodhoused304fe42013-02-23 00:24:48 +0000161 find_acpi_features();
David Woodhouse118469a2013-01-25 19:46:25 -0600162 }
163
164 // SMBIOS table needs to be copied into the f-seg
165 // XX: OVMF doesn't seem to set SmbiosTableLength so don't check it
166 if (csm_boot_table->SmbiosTable && !SMBiosAddr)
167 copy_smbios((void *)csm_boot_table->SmbiosTable);
168
169 // MPTABLE is just there; we don't care where.
170
171 // EFI may have reinitialised the video using its *own* driver.
172 enable_vga_console();
173
174 // EFI fills this in for us. Zero it for now...
175 struct bios_data_area_s *bda = MAKE_FLATPTR(SEG_BDA, 0);
176 bda->hdcount = 0;
177
Kevin O'Connor7093aa52013-07-21 20:01:07 -0400178 mathcp_setup();
David Woodhouse118469a2013-01-25 19:46:25 -0600179 timer_setup();
Kevin O'Connorc6e8c072013-07-20 10:51:58 -0400180 clock_setup();
David Woodhouse118469a2013-01-25 19:46:25 -0600181 device_hardware_setup();
182 wait_threads();
183 interactive_bootmenu();
184
185 prepareboot();
186
187 regs->ax = 0;
188}
189
190/* Boot */
191static void
192handle_csm_0003(struct bregs *regs)
193{
David Woodhouse84b7dcb2013-02-14 09:17:17 +0000194 if (!CONFIG_BOOT) {
195 regs->ax = 1;
196 return;
197 }
198
David Woodhouse118469a2013-01-25 19:46:25 -0600199 dprintf(3, "Boot\n");
200
201 startBoot();
202
203 regs->ax = 1;
204}
205
206/* Legacy16DispatchOprom */
207static void
208handle_csm_0005(struct bregs *regs)
209{
210 EFI_DISPATCH_OPROM_TABLE *table = MAKE_FLATPTR(regs->es, regs->bx);
211 struct rom_header *rom;
212 u16 bdf;
213
David Woodhouse0feab532013-02-14 09:07:35 +0000214 if (!CONFIG_OPTIONROMS) {
215 regs->ax = 1;
216 return;
217 }
218
David Woodhouse118469a2013-01-25 19:46:25 -0600219 dprintf(3, "Legacy16DispatchOprom rom %p\n", table);
220
221 dprintf(3, "OpromSegment %04x\n", table->OpromSegment);
222 dprintf(3, "RuntimeSegment %04x\n", table->RuntimeSegment);
223 dprintf(3, "PnPInstallationCheck %04x:%04x\n",
224 table->PnPInstallationCheckSegment,
225 table->PnPInstallationCheckOffset);
226 dprintf(3, "RuntimeSegment %04x\n", table->RuntimeSegment);
227
228 rom = MAKE_FLATPTR(table->OpromSegment, 0);
229 bdf = pci_bus_devfn_to_bdf(table->PciBus, table->PciDeviceFunction);
230
David Woodhouse38c2ebf2013-02-09 23:45:02 +0000231 rom_reserve(rom->size * 512);
David Woodhouse118469a2013-01-25 19:46:25 -0600232
233 // XX PnP seg/ofs should never be other than default
234 callrom(rom, bdf);
235
David Woodhouse38c2ebf2013-02-09 23:45:02 +0000236 rom_confirm(rom->size * 512);
237
David Woodhouse118469a2013-01-25 19:46:25 -0600238 regs->bx = 0; // FIXME
239 regs->ax = 0;
240}
241
242/* Legacy16GetTableAddress */
243static void
244handle_csm_0006(struct bregs *regs)
245{
246 u16 size = regs->cx;
247 u16 align = regs->dx;
248 u16 region = regs->bx; // (1 for F000 seg, 2 for E000 seg, 0 for either)
249 void *chunk = NULL;
250
251 if (!region)
252 region = 3;
253
254 dprintf(3, "Legacy16GetTableAddress size %x align %x region %d\n",
255 size, align, region);
256
257 if (region & 2)
Kevin O'Connor9dea5902013-09-14 20:23:54 -0400258 chunk = _malloc(&ZoneLow, MALLOC_DEFAULT_HANDLE, size, align);
David Woodhouse118469a2013-01-25 19:46:25 -0600259 if (!chunk && (region & 1))
Kevin O'Connor9dea5902013-09-14 20:23:54 -0400260 chunk = _malloc(&ZoneFSeg, MALLOC_DEFAULT_HANDLE, size, align);
David Woodhouse118469a2013-01-25 19:46:25 -0600261
262 dprintf(3, "Legacy16GetTableAddress size %x align %x region %d yields %p\n",
263 size, align, region, chunk);
264 if (chunk) {
265 regs->ds = FLATPTR_TO_SEG(chunk);
266 regs->bx = FLATPTR_TO_OFFSET(chunk);
267 regs->ax = 0;
268 } else {
269 regs->ax = 1;
270 }
271}
272
273void VISIBLE32INIT
274handle_csm(struct bregs *regs)
275{
276 ASSERT32FLAT();
277
278 if (!CONFIG_CSM)
279 return;
280
David Woodhouse4b1d2be2013-02-10 00:51:56 +0000281 dprintf(3, "handle_csm regs %p AX=%04x\n", regs, regs->ax);
David Woodhouse118469a2013-01-25 19:46:25 -0600282
Kevin O'Connoraa7c2342013-07-14 15:07:21 -0400283 pic_irqmask_write(PICMask);
David Woodhouse118469a2013-01-25 19:46:25 -0600284
285 switch(regs->ax) {
286 case 0000: handle_csm_0000(regs); break;
287 case 0001: handle_csm_0001(regs); break;
288 case 0002: handle_csm_0002(regs); break;
289 case 0003: handle_csm_0003(regs); break;
290// case 0004: handle_csm_0004(regs); break;
291 case 0005: handle_csm_0005(regs); break;
292 case 0006: handle_csm_0006(regs); break;
293// case 0007: handle_csm_0007(regs); break;
294// case 0008: hamdle_csm_0008(regs); break;
295 default: regs->al = 1;
296 }
297
David Woodhouse4b1d2be2013-02-10 00:51:56 +0000298 csm_return(regs);
David Woodhouse118469a2013-01-25 19:46:25 -0600299}
300
301int csm_bootprio_ata(struct pci_device *pci, int chanid, int slave)
302{
303 if (!csm_boot_table)
304 return -1;
305 BBS_TABLE *bbs = (void *)csm_boot_table->BbsTable;
306 int index = 1 + (chanid * 2) + slave;
307 dprintf(3, "CSM bootprio for ATA%d,%d (index %d) is %d\n", chanid, slave,
308 index, bbs[index].BootPriority);
309 return bbs[index].BootPriority;
310}
311
312int csm_bootprio_fdc(struct pci_device *pci, int port, int fdid)
313{
314 if (!csm_boot_table)
315 return -1;
316 BBS_TABLE *bbs = (void *)csm_boot_table->BbsTable;
317 dprintf(3, "CSM bootprio for FDC is %d\n", bbs[0].BootPriority);
318 return bbs[0].BootPriority;
319}
320
321int csm_bootprio_pci(struct pci_device *pci)
322{
323 if (!csm_boot_table)
324 return -1;
325 BBS_TABLE *bbs = (void *)csm_boot_table->BbsTable;
326 int i;
327
328 for (i = 5; i < csm_boot_table->NumberBbsEntries; i++) {
329 if (pci->bdf == pci_to_bdf(bbs[i].Bus, bbs[i].Device, bbs[i].Function)) {
330 dprintf(3, "CSM bootprio for PCI(%d,%d,%d) is %d\n", bbs[i].Bus,
331 bbs[i].Device, bbs[i].Function, bbs[i].BootPriority);
332 return bbs[i].BootPriority;
333 }
334 }
335 return -1;
336}