1 // MPTable generation (on emulators)
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
8 #include "util.h" // dprintf
9 #include "config.h" // CONFIG_*
10 #include "mptable.h" // MPTABLE_SIGNATURE
11 #include "paravirt.h" // qemu_cfg_irq0_override
21 dprintf(3, "init MPTable\n");
23 // Config structure in temp area.
24 struct mptable_config_s *config = malloc_tmphigh(32*1024);
26 dprintf(1, "No space for temp mptable\n");
29 memset(config, 0, sizeof(*config));
30 config->signature = MPCONFIG_SIGNATURE;
32 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
33 memcpy(config->productid, "0.1 ", sizeof(config->productid));
34 config->lapic = BUILD_APIC_ADDR;
37 u32 cpuid_signature, ebx, ecx, cpuid_features;
38 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
40 if (cpuid_features & (1 << 28)) {
41 /* Only populate the MPS tables with the first logical CPU in
43 pkgcpus = (ebx >> 16) & 0xff;
44 pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
48 struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
50 for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
51 memset(cpu, 0, sizeof(*cpu));
52 cpu->type = MPT_TYPE_CPU;
55 /* cpu flags: enabled, bootstrap cpu */
56 cpu->cpuflag = (i < CountCPUs) | ((i == 0) << 1);
57 if (cpuid_signature) {
58 cpu->cpusignature = cpuid_signature;
59 cpu->featureflag = cpuid_features;
61 cpu->cpusignature = 0x600;
62 cpu->featureflag = 0x201;
66 int entrycount = cpu - cpus;
69 struct mpt_bus *bus = (void*)cpu;
70 int bdf, max, lastbus = -1;
71 foreachpci(bdf, max) {
72 int curbus = pci_bdf_to_bus(bdf);
73 if (curbus == lastbus)
76 memset(bus, 0, sizeof(*bus));
77 bus->type = MPT_TYPE_BUS;
79 memcpy(bus->bustype, "PCI ", sizeof(bus->bustype));
86 memset(bus, 0, sizeof(*bus));
87 bus->type = MPT_TYPE_BUS;
88 isabusid = bus->busid = lastbus + 1;
89 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
93 u8 ioapic_id = CountCPUs;
94 struct mpt_ioapic *ioapic = (void*)&bus[1];
95 memset(ioapic, 0, sizeof(*ioapic));
96 ioapic->type = MPT_TYPE_IOAPIC;
97 ioapic->apicid = ioapic_id;
98 ioapic->apicver = 0x11;
99 ioapic->flags = 1; // enable
100 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
104 struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
106 unsigned short mask = 0, pinmask = 0;
108 foreachpci(bdf, max) {
109 int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
110 int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
113 if (dev != pci_bdf_to_dev(bdf)) {
114 dev = pci_bdf_to_dev(bdf);
117 if (pinmask & (1 << pin)) /* pin was seen already */
119 pinmask |= (1 << pin);
121 memset(intsrc, 0, sizeof(*intsrc));
122 intsrc->type = MPT_TYPE_INTSRC;
123 intsrc->irqtype = 0; /* INT */
124 intsrc->irqflag = 1; /* active high */
125 intsrc->srcbus = pci_bdf_to_bus(bdf); /* PCI bus */
126 intsrc->srcbusirq = (dev << 2) | (pin - 1);
127 intsrc->dstapic = ioapic_id;
128 intsrc->dstirq = irq;
132 for (i = 0; i < 16; i++) {
133 memset(intsrc, 0, sizeof(*intsrc));
136 intsrc->type = MPT_TYPE_INTSRC;
137 intsrc->irqtype = 0; /* INT */
138 intsrc->irqflag = 0; /* conform to bus spec */
139 intsrc->srcbus = isabusid; /* ISA bus */
140 intsrc->srcbusirq = i;
141 intsrc->dstapic = ioapic_id;
143 if (qemu_cfg_irq0_override()) {
144 /* Destination 2 is covered by irq0->inti2 override (i ==
145 0). Source IRQ 2 is unused */
153 entrycount += intsrc - intsrcs;
155 /* Local interrupt assignment */
156 intsrc->type = MPT_TYPE_LOCAL_INT;
157 intsrc->irqtype = 3; /* ExtINT */
158 intsrc->irqflag = 0; /* PO, EL default */
159 intsrc->srcbus = isabusid; /* ISA */
160 intsrc->srcbusirq = 0;
161 intsrc->dstapic = 0; /* BSP == APIC #0 */
162 intsrc->dstirq = 0; /* LINTIN0 */
166 intsrc->type = MPT_TYPE_LOCAL_INT;
167 intsrc->irqtype = 1; /* NMI */
168 intsrc->irqflag = 0; /* PO, EL default */
169 intsrc->srcbus = isabusid; /* ISA */
170 intsrc->srcbusirq = 0;
171 intsrc->dstapic = 0; /* BSP == APIC #0 */
172 intsrc->dstirq = 1; /* LINTIN1 */
176 // Finalize config structure.
177 int length = (void*)intsrc - (void*)config;
178 config->entrycount = entrycount;
179 config->length = length;
180 config->checksum -= checksum(config, length);
182 // Allocate final memory locations
183 struct mptable_config_s *finalconfig = malloc_fseg(length);
184 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
185 if (!finalconfig || !floating) {
186 dprintf(1, "No room for MPTABLE!\n");
192 memcpy(finalconfig, config, length);
195 /* floating pointer structure */
196 memset(floating, 0, sizeof(*floating));
197 floating->signature = MPTABLE_SIGNATURE;
198 floating->physaddr = (u32)finalconfig;
199 floating->length = 1;
200 floating->spec_rev = 4;
201 floating->checksum -= checksum(floating, sizeof(*floating));
203 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
204 floating, finalconfig, length);