1
// MPTable generation (on emulators)
2
// DO NOT ADD NEW FEATURES HERE. (See paravirt.c / biostables.c instead.)
4
// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
5
// Copyright (C) 2006 Fabrice Bellard
7
// This file may be distributed under the terms of the GNU LGPLv3 license.
9
#include "config.h" // CONFIG_*
10
#include "hw/pci.h" // pci_bdf_to_bus
11
#include "hw/pcidevice.h" // foreachpci
12
#include "hw/pci_regs.h" // PCI_INTERRUPT_PIN
13
#include "malloc.h" // free
14
#include "output.h" // dprintf
15
#include "romfile.h" // romfile_loadint
16
#include "std/mptable.h" // MPTABLE_SIGNATURE
17
#include "string.h" // memset
18
#include "util.h" // MaxCountCPUs
19
#include "x86.h" // cpuid
27
dprintf(3, "init MPTable\n");
29
// Config structure in temp area.
30
struct mptable_config_s *config = malloc_tmp(32*1024);
35
memset(config, 0, sizeof(*config));
36
config->signature = MPCONFIG_SIGNATURE;
38
memcpy(config->oemid, BUILD_CPUNAME8, sizeof(config->oemid));
39
memcpy(config->productid, "0.1 ", sizeof(config->productid));
40
config->lapic = BUILD_APIC_ADDR;
43
u32 cpuid_signature, ebx, ecx, cpuid_features;
44
cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
45
if (! cpuid_signature) {
46
// Use default values.
47
cpuid_signature = 0x600;
48
cpuid_features = 0x201;
51
if (cpuid_features & (1 << 28)) {
52
/* Only populate the MPS tables with the first logical CPU in
54
pkgcpus = (ebx >> 16) & 0xff;
55
pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
57
u8 apic_version = readl((u8*)BUILD_APIC_ADDR + 0x30) & 0xff;
60
struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
62
for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
63
memset(cpu, 0, sizeof(*cpu));
64
cpu->type = MPT_TYPE_CPU;
66
cpu->apicver = apic_version;
67
/* cpu flags: enabled, bootstrap cpu */
68
cpu->cpuflag = (apic_id_is_present(i) ? 0x01 : 0x00) | ((i==0) ? 0x02 : 0x00);
69
cpu->cpusignature = cpuid_signature;
70
cpu->featureflag = cpuid_features;
73
int entrycount = cpu - cpus;
76
struct mpt_bus *buses = (void*)cpu, *bus = buses;
77
if (!hlist_empty(&PCIDevices)) {
78
memset(bus, 0, sizeof(*bus));
79
bus->type = MPT_TYPE_BUS;
81
memcpy(bus->bustype, "PCI ", sizeof(bus->bustype));
87
int isabusid = bus - buses;
88
memset(bus, 0, sizeof(*bus));
89
bus->type = MPT_TYPE_BUS;
90
bus->busid = isabusid;
91
memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
96
u8 ioapic_id = BUILD_IOAPIC_ID;
97
struct mpt_ioapic *ioapic = (void*)bus;
98
memset(ioapic, 0, sizeof(*ioapic));
99
ioapic->type = MPT_TYPE_IOAPIC;
100
ioapic->apicid = ioapic_id;
101
ioapic->apicver = 0x11;
102
ioapic->flags = 1; // enable
103
ioapic->apicaddr = BUILD_IOAPIC_ADDR;
107
struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
109
unsigned short pinmask = 0;
111
struct pci_device *pci;
114
if (pci_bdf_to_bus(bdf) != 0)
116
int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
117
int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
120
if (dev != pci_bdf_to_busdev(bdf)) {
121
dev = pci_bdf_to_busdev(bdf);
124
if (pinmask & (1 << pin)) /* pin was seen already */
126
pinmask |= (1 << pin);
127
memset(intsrc, 0, sizeof(*intsrc));
128
intsrc->type = MPT_TYPE_INTSRC;
129
intsrc->irqtype = 0; /* INT */
130
intsrc->irqflag = 1; /* active high */
131
intsrc->srcbus = pci_bdf_to_bus(bdf); /* PCI bus */
132
intsrc->srcbusirq = (pci_bdf_to_dev(bdf) << 2) | (pin - 1);
133
intsrc->dstapic = ioapic_id;
134
intsrc->dstirq = irq;
138
int irq0_override = romfile_loadint("etc/irq0-override", 0);
139
for (i = 0; i < 16; i++) {
140
memset(intsrc, 0, sizeof(*intsrc));
141
if (BUILD_PCI_IRQS & (1 << i))
143
intsrc->type = MPT_TYPE_INTSRC;
144
intsrc->irqtype = 0; /* INT */
145
intsrc->irqflag = 0; /* conform to bus spec */
146
intsrc->srcbus = isabusid; /* ISA bus */
147
intsrc->srcbusirq = i;
148
intsrc->dstapic = ioapic_id;
151
/* Destination 2 is covered by irq0->inti2 override (i ==
152
0). Source IRQ 2 is unused */
161
/* Local interrupt assignment */
162
intsrc->type = MPT_TYPE_LOCAL_INT;
163
intsrc->irqtype = 3; /* ExtINT */
164
intsrc->irqflag = 0; /* PO, EL default */
165
intsrc->srcbus = isabusid; /* ISA */
166
intsrc->srcbusirq = 0;
167
intsrc->dstapic = 0; /* BSP == APIC #0 */
168
intsrc->dstirq = 0; /* LINTIN0 */
171
intsrc->type = MPT_TYPE_LOCAL_INT;
172
intsrc->irqtype = 1; /* NMI */
173
intsrc->irqflag = 0; /* PO, EL default */
174
intsrc->srcbus = isabusid; /* ISA */
175
intsrc->srcbusirq = 0;
176
intsrc->dstapic = 0xff; /* to all local APICs */
177
intsrc->dstirq = 1; /* LINTIN1 */
179
entrycount += intsrc - intsrcs;
181
// Finalize config structure.
182
int length = (void*)intsrc - (void*)config;
183
config->entrycount = entrycount;
184
config->length = length;
185
config->checksum -= checksum(config, length);
187
// floating pointer structure
188
struct mptable_floating_s floating;
189
memset(&floating, 0, sizeof(floating));
190
floating.signature = MPTABLE_SIGNATURE;
191
floating.physaddr = (u32)config;
193
floating.spec_rev = 4;
194
floating.checksum -= checksum(&floating, sizeof(floating));
195
copy_mptable(&floating);