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 "memmap.h" // malloc_fseg
10 #include "config.h" // CONFIG_*
11 #include "mptable.h" // MPTABLE_SIGNATURE
25 dprintf(3, "init MPTable\n");
27 int smp_cpus = CountCPUs;
29 // Building an mptable on uniprocessor machines confuses some OSes.
32 int length = (sizeof(struct mptable_config_s)
33 + sizeof(struct mpt_cpu) * smp_cpus
34 + sizeof(struct mpt_bus)
35 + sizeof(struct mpt_ioapic)
36 + sizeof(struct mpt_intsrc) * 16);
37 struct mptable_config_s *config = malloc_fseg(length);
38 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
39 if (!config || !floating) {
40 dprintf(1, "No room for MPTABLE!\n");
44 /* floating pointer structure */
45 memset(floating, 0, sizeof(*floating));
46 floating->signature = MPTABLE_SIGNATURE;
47 floating->physaddr = (u32)config;
49 floating->spec_rev = 4;
50 floating->checksum -= checksum(floating, sizeof(*floating));
53 memset(config, 0, sizeof(*config));
54 config->signature = MPCONFIG_SIGNATURE;
56 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
57 memcpy(config->productid, "0.1 ", sizeof(config->productid));
58 config->entrycount = smp_cpus + 2 + 16;
59 config->lapic = BUILD_APIC_ADDR;
62 u32 cpuid_signature, ebx, ecx, cpuid_features;
63 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
64 struct mpt_cpu *cpus = (void*)&config[1];
66 for (i = 0; i < smp_cpus; i++) {
67 struct mpt_cpu *cpu = &cpus[i];
68 memset(cpu, 0, sizeof(*cpu));
69 cpu->type = MPT_TYPE_CPU;
72 /* cpu flags: enabled, bootstrap cpu */
73 cpu->cpuflag = (i == 0 ? 3 : 1);
74 if (cpuid_signature) {
75 cpu->cpusignature = cpuid_signature;
76 cpu->featureflag = cpuid_features;
78 cpu->cpusignature = 0x600;
79 cpu->featureflag = 0x201;
84 struct mpt_bus *bus = (void*)&cpus[smp_cpus];
85 memset(bus, 0, sizeof(*bus));
86 bus->type = MPT_TYPE_BUS;
87 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
90 u8 ioapic_id = smp_cpus;
91 struct mpt_ioapic *ioapic = (void*)&bus[1];
92 memset(ioapic, 0, sizeof(*ioapic));
93 ioapic->type = MPT_TYPE_IOAPIC;
94 ioapic->apicid = ioapic_id;
95 ioapic->apicver = 0x11;
96 ioapic->flags = 1; // enable
97 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
100 struct mpt_intsrc *intsrc = (void*)&ioapic[1];
101 for (i = 0; i < 16; i++) {
102 memset(intsrc, 0, sizeof(*intsrc));
103 intsrc->type = MPT_TYPE_INTSRC;
104 intsrc->srcbusirq = i;
105 intsrc->dstapic = ioapic_id;
108 /* Destination 2 is covered by irq0->inti2 override (i ==
109 0). Source IRQ 2 is unused */
119 config->length = (void*)intsrc - (void*)config;
120 config->checksum -= checksum(config, config->length);
122 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
123 floating, config, config->length);