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
13 static inline unsigned long
14 fls(unsigned long word)
28 dprintf(3, "init MPTable\n");
30 int length = (sizeof(struct mptable_config_s)
31 + sizeof(struct mpt_cpu) * MaxCountCPUs
32 + sizeof(struct mpt_bus)
33 + sizeof(struct mpt_ioapic)
34 + sizeof(struct mpt_intsrc) * 16);
35 struct mptable_config_s *config = malloc_fseg(length);
36 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
37 if (!config || !floating) {
38 dprintf(1, "No room for MPTABLE!\n");
42 /* floating pointer structure */
43 memset(floating, 0, sizeof(*floating));
44 floating->signature = MPTABLE_SIGNATURE;
45 floating->physaddr = (u32)config;
47 floating->spec_rev = 4;
48 floating->checksum -= checksum(floating, sizeof(*floating));
51 memset(config, 0, sizeof(*config));
52 config->signature = MPCONFIG_SIGNATURE;
54 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
55 memcpy(config->productid, "0.1 ", sizeof(config->productid));
56 config->lapic = BUILD_APIC_ADDR;
59 u32 cpuid_signature, ebx, ecx, cpuid_features;
60 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
61 struct mpt_cpu *cpus = (void*)&config[1];
62 int i, actual_cpu_count;
63 for (i = 0, actual_cpu_count = 0; i < MaxCountCPUs; i++) {
64 struct mpt_cpu *cpu = &cpus[i];
65 int log_cpus = (ebx >> 16) & 0xff;
66 log_cpus = 1UL << fls(log_cpus - 1); /* round up to power of 2 */
68 /* Only populate the MPS tables with the first logical CPU in each
70 if ((cpuid_features & (1 << 28)) && (i & (log_cpus - 1)) != 0)
75 memset(cpu, 0, sizeof(*cpu));
76 cpu->type = MPT_TYPE_CPU;
79 /* cpu flags: enabled, bootstrap cpu */
81 cpu->cpuflag = 1 | ((i == 0) ? 2 : 0);
84 if (cpuid_signature) {
85 cpu->cpusignature = cpuid_signature;
86 cpu->featureflag = cpuid_features;
88 cpu->cpusignature = 0x600;
89 cpu->featureflag = 0x201;
93 config->entrycount = actual_cpu_count + 2 + 16;
96 struct mpt_bus *bus = (void*)&cpus[actual_cpu_count];
97 memset(bus, 0, sizeof(*bus));
98 bus->type = MPT_TYPE_BUS;
99 memcpy(bus->bustype, "ISA ", 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;
112 struct mpt_intsrc *intsrc = (void*)&ioapic[1];
113 for (i = 0; i < 16; i++) {
114 memset(intsrc, 0, sizeof(*intsrc));
115 intsrc->type = MPT_TYPE_INTSRC;
116 intsrc->srcbusirq = i;
117 intsrc->dstapic = ioapic_id;
119 if (qemu_cfg_irq0_override()) {
120 /* Destination 2 is covered by irq0->inti2 override (i ==
121 0). Source IRQ 2 is unused */
131 config->length = (void*)intsrc - (void*)config;
132 config->checksum -= checksum(config, config->length);
134 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
135 floating, config, config->length);