Add Post Memory Manager (PMM) support.
[seabios.git] / src / mptable.c
1 // MPTable generation (on emulators)
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2006 Fabrice Bellard
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "util.h" // dprintf
9 #include "config.h" // CONFIG_*
10 #include "mptable.h" // MPTABLE_SIGNATURE
11
12 #if CONFIG_KVM
13 int irq0override = 1;
14 #else
15 int irq0override = 0;
16 #endif
17
18 void
19 mptable_init(void)
20 {
21     if (! CONFIG_MPTABLE)
22         return;
23
24     dprintf(3, "init MPTable\n");
25
26     int smp_cpus = CountCPUs;
27     if (smp_cpus <= 1)
28         // Building an mptable on uniprocessor machines confuses some OSes.
29         return;
30
31     int length = (sizeof(struct mptable_config_s)
32                   + sizeof(struct mpt_cpu) * smp_cpus
33                   + sizeof(struct mpt_bus)
34                   + sizeof(struct mpt_ioapic)
35                   + sizeof(struct mpt_intsrc) * 16);
36     struct mptable_config_s *config = malloc_fseg(length);
37     struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
38     if (!config || !floating) {
39         dprintf(1, "No room for MPTABLE!\n");
40         return;
41     }
42
43     /* floating pointer structure */
44     memset(floating, 0, sizeof(*floating));
45     floating->signature = MPTABLE_SIGNATURE;
46     floating->physaddr = (u32)config;
47     floating->length = 1;
48     floating->spec_rev = 4;
49     floating->checksum -= checksum(floating, sizeof(*floating));
50
51     // Config structure.
52     memset(config, 0, sizeof(*config));
53     config->signature = MPCONFIG_SIGNATURE;
54     config->spec = 4;
55     memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
56     memcpy(config->productid, "0.1         ", sizeof(config->productid));
57     config->entrycount = smp_cpus + 2 + 16;
58     config->lapic = BUILD_APIC_ADDR;
59
60     // CPU definitions.
61     u32 cpuid_signature, ebx, ecx, cpuid_features;
62     cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
63     struct mpt_cpu *cpus = (void*)&config[1];
64     int i;
65     for (i = 0; i < smp_cpus; i++) {
66         struct mpt_cpu *cpu = &cpus[i];
67         memset(cpu, 0, sizeof(*cpu));
68         cpu->type = MPT_TYPE_CPU;
69         cpu->apicid = i;
70         cpu->apicver = 0x11;
71         /* cpu flags: enabled, bootstrap cpu */
72         cpu->cpuflag = (i == 0 ? 3 : 1);
73         if (cpuid_signature) {
74             cpu->cpusignature = cpuid_signature;
75             cpu->featureflag = cpuid_features;
76         } else {
77             cpu->cpusignature = 0x600;
78             cpu->featureflag = 0x201;
79         }
80     }
81
82     /* isa bus */
83     struct mpt_bus *bus = (void*)&cpus[smp_cpus];
84     memset(bus, 0, sizeof(*bus));
85     bus->type = MPT_TYPE_BUS;
86     memcpy(bus->bustype, "ISA   ", sizeof(bus->bustype));
87
88     /* ioapic */
89     u8 ioapic_id = smp_cpus;
90     struct mpt_ioapic *ioapic = (void*)&bus[1];
91     memset(ioapic, 0, sizeof(*ioapic));
92     ioapic->type = MPT_TYPE_IOAPIC;
93     ioapic->apicid = ioapic_id;
94     ioapic->apicver = 0x11;
95     ioapic->flags = 1; // enable
96     ioapic->apicaddr = BUILD_IOAPIC_ADDR;
97
98     /* irqs */
99     struct mpt_intsrc *intsrc = (void*)&ioapic[1];
100     for (i = 0; i < 16; i++) {
101         memset(intsrc, 0, sizeof(*intsrc));
102         intsrc->type = MPT_TYPE_INTSRC;
103         intsrc->srcbusirq = i;
104         intsrc->dstapic = ioapic_id;
105         intsrc->dstirq = i;
106         if (irq0override) {
107             /* Destination 2 is covered by irq0->inti2 override (i ==
108                0). Source IRQ 2 is unused */
109             if (i == 0)
110                 intsrc->dstirq = 2;
111             else if (i == 2)
112                 intsrc--;
113         }
114         intsrc++;
115     }
116
117     // Set checksum.
118     config->length = (void*)intsrc - (void*)config;
119     config->checksum -= checksum(config, config->length);
120
121     dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
122             floating, config, config->length);
123 }