1 // MPTable generation (on emulators)
2 // DO NOT ADD NEW FEATURES HERE. (See paravirt.c / biostables.c instead.)
4 // Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
5 // Copyright (C) 2006 Fabrice Bellard
7 // This file may be distributed under the terms of the GNU LGPLv3 license.
9 #include "config.h" // CONFIG_*
11 #include "hw/pci_regs.h"
12 #include "malloc.h" // free
13 #include "output.h" // dprintf
14 #include "romfile.h" // romfile_loadint
15 #include "std/mptable.h" // MPTABLE_SIGNATURE
16 #include "string.h" // memset
17 #include "util.h" // MaxCountCPUs
18 #include "x86.h" // cpuid
26 dprintf(3, "init MPTable\n");
28 // Config structure in temp area.
29 struct mptable_config_s *config = malloc_tmp(32*1024);
34 memset(config, 0, sizeof(*config));
35 config->signature = MPCONFIG_SIGNATURE;
37 memcpy(config->oemid, BUILD_CPUNAME8, sizeof(config->oemid));
38 memcpy(config->productid, "0.1 ", sizeof(config->productid));
39 config->lapic = BUILD_APIC_ADDR;
42 u32 cpuid_signature, ebx, ecx, cpuid_features;
43 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
44 if (! cpuid_signature) {
45 // Use default values.
46 cpuid_signature = 0x600;
47 cpuid_features = 0x201;
50 if (cpuid_features & (1 << 28)) {
51 /* Only populate the MPS tables with the first logical CPU in
53 pkgcpus = (ebx >> 16) & 0xff;
54 pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
56 u8 apic_version = readl((u8*)BUILD_APIC_ADDR + 0x30) & 0xff;
59 struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
61 for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
62 memset(cpu, 0, sizeof(*cpu));
63 cpu->type = MPT_TYPE_CPU;
65 cpu->apicver = apic_version;
66 /* cpu flags: enabled, bootstrap cpu */
67 cpu->cpuflag = (apic_id_is_present(i) ? 0x01 : 0x00) | ((i==0) ? 0x02 : 0x00);
68 cpu->cpusignature = cpuid_signature;
69 cpu->featureflag = cpuid_features;
72 int entrycount = cpu - cpus;
75 struct mpt_bus *buses = (void*)cpu, *bus = buses;
76 if (!hlist_empty(&PCIDevices)) {
77 memset(bus, 0, sizeof(*bus));
78 bus->type = MPT_TYPE_BUS;
80 memcpy(bus->bustype, "PCI ", sizeof(bus->bustype));
86 int isabusid = bus - buses;
87 memset(bus, 0, sizeof(*bus));
88 bus->type = MPT_TYPE_BUS;
89 bus->busid = isabusid;
90 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
95 u8 ioapic_id = BUILD_IOAPIC_ID;
96 struct mpt_ioapic *ioapic = (void*)bus;
97 memset(ioapic, 0, sizeof(*ioapic));
98 ioapic->type = MPT_TYPE_IOAPIC;
99 ioapic->apicid = ioapic_id;
100 ioapic->apicver = 0x11;
101 ioapic->flags = 1; // enable
102 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
106 struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
108 unsigned short pinmask = 0;
110 struct pci_device *pci;
113 if (pci_bdf_to_bus(bdf) != 0)
115 int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
116 int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
119 if (dev != pci_bdf_to_busdev(bdf)) {
120 dev = pci_bdf_to_busdev(bdf);
123 if (pinmask & (1 << pin)) /* pin was seen already */
125 pinmask |= (1 << pin);
126 memset(intsrc, 0, sizeof(*intsrc));
127 intsrc->type = MPT_TYPE_INTSRC;
128 intsrc->irqtype = 0; /* INT */
129 intsrc->irqflag = 1; /* active high */
130 intsrc->srcbus = pci_bdf_to_bus(bdf); /* PCI bus */
131 intsrc->srcbusirq = (pci_bdf_to_dev(bdf) << 2) | (pin - 1);
132 intsrc->dstapic = ioapic_id;
133 intsrc->dstirq = irq;
137 int irq0_override = romfile_loadint("etc/irq0-override", 0);
138 for (i = 0; i < 16; i++) {
139 memset(intsrc, 0, sizeof(*intsrc));
140 if (BUILD_PCI_IRQS & (1 << i))
142 intsrc->type = MPT_TYPE_INTSRC;
143 intsrc->irqtype = 0; /* INT */
144 intsrc->irqflag = 0; /* conform to bus spec */
145 intsrc->srcbus = isabusid; /* ISA bus */
146 intsrc->srcbusirq = i;
147 intsrc->dstapic = ioapic_id;
150 /* Destination 2 is covered by irq0->inti2 override (i ==
151 0). Source IRQ 2 is unused */
160 /* Local interrupt assignment */
161 intsrc->type = MPT_TYPE_LOCAL_INT;
162 intsrc->irqtype = 3; /* ExtINT */
163 intsrc->irqflag = 0; /* PO, EL default */
164 intsrc->srcbus = isabusid; /* ISA */
165 intsrc->srcbusirq = 0;
166 intsrc->dstapic = 0; /* BSP == APIC #0 */
167 intsrc->dstirq = 0; /* LINTIN0 */
170 intsrc->type = MPT_TYPE_LOCAL_INT;
171 intsrc->irqtype = 1; /* NMI */
172 intsrc->irqflag = 0; /* PO, EL default */
173 intsrc->srcbus = isabusid; /* ISA */
174 intsrc->srcbusirq = 0;
175 intsrc->dstapic = 0xff; /* to all local APICs */
176 intsrc->dstirq = 1; /* LINTIN1 */
178 entrycount += intsrc - intsrcs;
180 // Finalize config structure.
181 int length = (void*)intsrc - (void*)config;
182 config->entrycount = entrycount;
183 config->length = length;
184 config->checksum -= checksum(config, length);
186 // floating pointer structure
187 struct mptable_floating_s floating;
188 memset(&floating, 0, sizeof(floating));
189 floating.signature = MPTABLE_SIGNATURE;
190 floating.physaddr = (u32)config;
192 floating.spec_rev = 4;
193 floating.checksum -= checksum(&floating, sizeof(floating));
194 copy_mptable(&floating);