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
19 dprintf(3, "init MPTable\n");
22 int length = (sizeof(struct mptable_config_s)
23 + sizeof(struct mpt_cpu) * MaxCountCPUs
24 + sizeof(struct mpt_bus)
25 + sizeof(struct mpt_ioapic)
26 + sizeof(struct mpt_intsrc) * 18);
27 struct mptable_config_s *config = malloc_fseg(length);
28 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
29 if (!config || !floating) {
30 dprintf(1, "No room for MPTABLE!\n");
36 /* floating pointer structure */
37 memset(floating, 0, sizeof(*floating));
38 floating->signature = MPTABLE_SIGNATURE;
39 floating->physaddr = (u32)config;
41 floating->spec_rev = 4;
42 floating->checksum -= checksum(floating, sizeof(*floating));
45 memset(config, 0, sizeof(*config));
46 config->signature = MPCONFIG_SIGNATURE;
48 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
49 memcpy(config->productid, "0.1 ", sizeof(config->productid));
50 config->lapic = BUILD_APIC_ADDR;
53 u32 cpuid_signature, ebx, ecx, cpuid_features;
54 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
56 if (cpuid_features & (1 << 28)) {
57 /* Only populate the MPS tables with the first logical CPU in
59 pkgcpus = (ebx >> 16) & 0xff;
60 pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
64 struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
66 for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
67 memset(cpu, 0, sizeof(*cpu));
68 cpu->type = MPT_TYPE_CPU;
71 /* cpu flags: enabled, bootstrap cpu */
72 cpu->cpuflag = (i < CountCPUs) | ((i == 0) << 1);
73 if (cpuid_signature) {
74 cpu->cpusignature = cpuid_signature;
75 cpu->featureflag = cpuid_features;
77 cpu->cpusignature = 0x600;
78 cpu->featureflag = 0x201;
82 int entrycount = cpu - cpus;
85 struct mpt_bus *bus = (void*)cpu;
86 memset(bus, 0, sizeof(*bus));
87 bus->type = MPT_TYPE_BUS;
88 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
92 u8 ioapic_id = CountCPUs;
93 struct mpt_ioapic *ioapic = (void*)&bus[1];
94 memset(ioapic, 0, sizeof(*ioapic));
95 ioapic->type = MPT_TYPE_IOAPIC;
96 ioapic->apicid = ioapic_id;
97 ioapic->apicver = 0x11;
98 ioapic->flags = 1; // enable
99 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
103 struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
104 for (i = 0; i < 16; i++) {
105 memset(intsrc, 0, sizeof(*intsrc));
106 intsrc->type = MPT_TYPE_INTSRC;
107 intsrc->srcbusirq = i;
108 intsrc->dstapic = ioapic_id;
110 if (qemu_cfg_irq0_override()) {
111 /* Destination 2 is covered by irq0->inti2 override (i ==
112 0). Source IRQ 2 is unused */
120 entrycount += intsrc - intsrcs;
122 /* Local interrupt assignment */
123 intsrc->type = MPT_TYPE_LOCAL_INT;
124 intsrc->irqtype = 3; /* ExtINT */
125 intsrc->irqflag = 0; /* PO, EL default */
127 intsrc->srcbusirq = 0;
128 intsrc->dstapic = 0; /* BSP == APIC #0 */
129 intsrc->dstirq = 0; /* LINTIN0 */
133 intsrc->type = MPT_TYPE_LOCAL_INT;
134 intsrc->irqtype = 1; /* NMI */
135 intsrc->irqflag = 0; /* PO, EL default */
137 intsrc->srcbusirq = 0;
138 intsrc->dstapic = 0; /* BSP == APIC #0 */
139 intsrc->dstirq = 1; /* LINTIN1 */
143 // Finalize config structure.
144 config->entrycount = entrycount;
145 config->length = (void*)intsrc - (void*)config;
146 config->checksum -= checksum(config, config->length);
148 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
149 floating, config, config->length);