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");
24 int length = (sizeof(struct mptable_config_s)
25 + sizeof(struct mpt_cpu) * MaxCountCPUs
26 + sizeof(struct mpt_bus) * 2
27 + sizeof(struct mpt_ioapic)
28 + sizeof(struct mpt_intsrc) * 34);
29 struct mptable_config_s *config = malloc_fseg(length);
30 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
31 if (!config || !floating) {
32 dprintf(1, "No room for MPTABLE!\n");
38 /* floating pointer structure */
39 memset(floating, 0, sizeof(*floating));
40 floating->signature = MPTABLE_SIGNATURE;
41 floating->physaddr = (u32)config;
43 floating->spec_rev = 4;
44 floating->checksum -= checksum(floating, sizeof(*floating));
47 memset(config, 0, sizeof(*config));
48 config->signature = MPCONFIG_SIGNATURE;
50 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
51 memcpy(config->productid, "0.1 ", sizeof(config->productid));
52 config->lapic = BUILD_APIC_ADDR;
55 u32 cpuid_signature, ebx, ecx, cpuid_features;
56 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
58 if (cpuid_features & (1 << 28)) {
59 /* Only populate the MPS tables with the first logical CPU in
61 pkgcpus = (ebx >> 16) & 0xff;
62 pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
66 struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
68 for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
69 memset(cpu, 0, sizeof(*cpu));
70 cpu->type = MPT_TYPE_CPU;
73 /* cpu flags: enabled, bootstrap cpu */
74 cpu->cpuflag = (i < CountCPUs) | ((i == 0) << 1);
75 if (cpuid_signature) {
76 cpu->cpusignature = cpuid_signature;
77 cpu->featureflag = cpuid_features;
79 cpu->cpusignature = 0x600;
80 cpu->featureflag = 0x201;
84 int entrycount = cpu - cpus;
87 struct mpt_bus *bus = (void*)cpu;
88 memset(bus, 0, sizeof(*bus));
89 bus->type = MPT_TYPE_BUS;
91 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
95 memset(bus, 0, sizeof(*bus));
96 bus->type = MPT_TYPE_BUS;
98 memcpy(bus->bustype, "PCI ", sizeof(bus->bustype));
102 u8 ioapic_id = CountCPUs;
103 struct mpt_ioapic *ioapic = (void*)&bus[1];
104 memset(ioapic, 0, sizeof(*ioapic));
105 ioapic->type = MPT_TYPE_IOAPIC;
106 ioapic->apicid = ioapic_id;
107 ioapic->apicver = 0x11;
108 ioapic->flags = 1; // enable
109 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
113 struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
114 int bdf, max, dev = -1;
115 unsigned short mask = 0, pinmask;
117 foreachpci(bdf, max) {
118 int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
119 int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
122 if (dev != pci_bdf_to_dev(bdf)) {
123 dev = pci_bdf_to_dev(bdf);
126 if (pinmask & (1 << pin)) /* pin was seen already */
128 pinmask |= (1 << pin);
130 memset(intsrc, 0, sizeof(*intsrc));
131 intsrc->type = MPT_TYPE_INTSRC;
132 intsrc->irqtype = 0; /* INT */
133 intsrc->irqflag = 1; /* active high */
134 intsrc->srcbus = 0; /* PCI bus */
135 intsrc->srcbusirq = (dev << 2) | (pin - 1);
136 intsrc->dstapic = ioapic_id;
137 intsrc->dstirq = irq;
141 for (i = 0; i < 16; i++) {
142 memset(intsrc, 0, sizeof(*intsrc));
145 intsrc->type = MPT_TYPE_INTSRC;
146 intsrc->irqtype = 0; /* INT */
147 intsrc->irqflag = 0; /* conform to bus spec */
148 intsrc->srcbus = 1; /* ISA bus */
149 intsrc->srcbusirq = i;
150 intsrc->dstapic = ioapic_id;
152 if (qemu_cfg_irq0_override()) {
153 /* Destination 2 is covered by irq0->inti2 override (i ==
154 0). Source IRQ 2 is unused */
162 entrycount += intsrc - intsrcs;
164 /* Local interrupt assignment */
165 intsrc->type = MPT_TYPE_LOCAL_INT;
166 intsrc->irqtype = 3; /* ExtINT */
167 intsrc->irqflag = 0; /* PO, EL default */
168 intsrc->srcbus = 1; /* ISA */
169 intsrc->srcbusirq = 0;
170 intsrc->dstapic = 0; /* BSP == APIC #0 */
171 intsrc->dstirq = 0; /* LINTIN0 */
175 intsrc->type = MPT_TYPE_LOCAL_INT;
176 intsrc->irqtype = 1; /* NMI */
177 intsrc->irqflag = 0; /* PO, EL default */
178 intsrc->srcbus = 1; /* ISA */
179 intsrc->srcbusirq = 0;
180 intsrc->dstapic = 0; /* BSP == APIC #0 */
181 intsrc->dstirq = 1; /* LINTIN1 */
185 // Finalize config structure.
186 config->entrycount = entrycount;
187 config->length = (void*)intsrc - (void*)config;
188 config->checksum -= checksum(config, config->length);
190 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
191 floating, config, config->length);