blob: c79f54f52116bdd329f6b6a649e527f953f938bf [file] [log] [blame]
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04001// MPTable generation (on emulators)
2//
Kevin O'Connor9fd48512010-07-26 22:34:37 -04003// Copyright (C) 2008-2010 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04004// Copyright (C) 2006 Fabrice Bellard
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04007
8#include "util.h" // dprintf
Kevin O'Connor9521e262008-07-04 13:04:29 -04009#include "config.h" // CONFIG_*
Kevin O'Connor35746442009-02-27 20:54:51 -050010#include "mptable.h" // MPTABLE_SIGNATURE
Kevin O'Connor5d369d82013-09-02 20:48:46 -040011#include "hw/pci.h"
12#include "hw/pci_regs.h"
Kevin O'Connor41639f82013-09-14 19:37:36 -040013#include "romfile.h" // romfile_loadint
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040014#include "string.h" // memset
Kevin O'Connorb9c6a962013-09-14 13:01:30 -040015#include "x86.h" // cpuid
Kevin O'Connorc0ad0e82009-11-09 19:20:21 -050016
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040017void
Kevin O'Connord83c87b2013-01-21 01:14:12 -050018mptable_setup(void)
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040019{
Kevin O'Connord3817022008-07-12 12:46:07 -040020 if (! CONFIG_MPTABLE)
21 return;
22
Kevin O'Connor7b49cd92008-11-08 10:35:26 -050023 dprintf(3, "init MPTable\n");
24
Kevin O'Connor83626992009-12-26 11:15:31 -050025 // Config structure in temp area.
Kevin O'Connor34e9cc52010-06-06 11:07:08 -040026 struct mptable_config_s *config = malloc_tmp(32*1024);
Kevin O'Connor83626992009-12-26 11:15:31 -050027 if (!config) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050028 warn_noalloc();
Kevin O'Connor35746442009-02-27 20:54:51 -050029 return;
30 }
Kevin O'Connor35746442009-02-27 20:54:51 -050031 memset(config, 0, sizeof(*config));
32 config->signature = MPCONFIG_SIGNATURE;
Kevin O'Connor35746442009-02-27 20:54:51 -050033 config->spec = 4;
Kevin O'Connore52ad392013-02-20 23:48:22 -050034 memcpy(config->oemid, BUILD_CPUNAME8, sizeof(config->oemid));
Kevin O'Connor35746442009-02-27 20:54:51 -050035 memcpy(config->productid, "0.1 ", sizeof(config->productid));
Kevin O'Connor35746442009-02-27 20:54:51 -050036 config->lapic = BUILD_APIC_ADDR;
37
Kevin O'Connor86916ce2009-11-14 13:34:27 -050038 // Detect cpu info
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040039 u32 cpuid_signature, ebx, ecx, cpuid_features;
40 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
Kevin O'Connorbf7f1f32010-02-11 22:19:14 -050041 if (! cpuid_signature) {
42 // Use default values.
43 cpuid_signature = 0x600;
44 cpuid_features = 0x201;
45 }
Kevin O'Connor86916ce2009-11-14 13:34:27 -050046 int pkgcpus = 1;
47 if (cpuid_features & (1 << 28)) {
48 /* Only populate the MPS tables with the first logical CPU in
49 each package */
50 pkgcpus = (ebx >> 16) & 0xff;
51 pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
52 }
Kevin O'Connorbf7f1f32010-02-11 22:19:14 -050053 u8 apic_version = readl((u8*)BUILD_APIC_ADDR + 0x30) & 0xff;
Kevin O'Connorc0ad0e82009-11-09 19:20:21 -050054
Kevin O'Connor86916ce2009-11-14 13:34:27 -050055 // CPU definitions.
56 struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
57 int i;
58 for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
Kevin O'Connor35746442009-02-27 20:54:51 -050059 memset(cpu, 0, sizeof(*cpu));
60 cpu->type = MPT_TYPE_CPU;
61 cpu->apicid = i;
Magnus Christensson41c09572009-11-23 14:01:19 +010062 cpu->apicver = apic_version;
Kevin O'Connor35746442009-02-27 20:54:51 -050063 /* cpu flags: enabled, bootstrap cpu */
Eduardo Habkost008c1fc2012-07-25 15:45:30 -030064 cpu->cpuflag = (apic_id_is_present(i) ? 0x01 : 0x00) | ((i==0) ? 0x02 : 0x00);
Kevin O'Connorbf7f1f32010-02-11 22:19:14 -050065 cpu->cpusignature = cpuid_signature;
66 cpu->featureflag = cpuid_features;
Kevin O'Connor86916ce2009-11-14 13:34:27 -050067 cpu++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040068 }
Kevin O'Connor86916ce2009-11-14 13:34:27 -050069 int entrycount = cpu - cpus;
Kevin O'Connorc0ad0e82009-11-09 19:20:21 -050070
Kevin O'Connor3c3a3fa2013-03-18 20:14:21 -040071 // PCI bus
Kevin O'Connor34e9cc52010-06-06 11:07:08 -040072 struct mpt_bus *buses = (void*)cpu, *bus = buses;
Kevin O'Connorb98a4b12013-06-08 21:53:36 -040073 if (!hlist_empty(&PCIDevices)) {
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020074 memset(bus, 0, sizeof(*bus));
75 bus->type = MPT_TYPE_BUS;
Kevin O'Connor3c3a3fa2013-03-18 20:14:21 -040076 bus->busid = 0;
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020077 memcpy(bus->bustype, "PCI ", sizeof(bus->bustype));
78 bus++;
Kevin O'Connor3c3a3fa2013-03-18 20:14:21 -040079 entrycount++;
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020080 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040081
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020082 /* isa bus */
Kevin O'Connor3c3a3fa2013-03-18 20:14:21 -040083 int isabusid = bus - buses;
Gleb Natapov928d4df2009-12-24 14:29:43 +020084 memset(bus, 0, sizeof(*bus));
85 bus->type = MPT_TYPE_BUS;
Kevin O'Connor3c3a3fa2013-03-18 20:14:21 -040086 bus->busid = isabusid;
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020087 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
Kevin O'Connor34e9cc52010-06-06 11:07:08 -040088 bus++;
Kevin O'Connor3c3a3fa2013-03-18 20:14:21 -040089 entrycount++;
Gleb Natapov928d4df2009-12-24 14:29:43 +020090
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040091 /* ioapic */
Eduardo Habkoste39b9382012-07-25 15:45:29 -030092 u8 ioapic_id = BUILD_IOAPIC_ID;
Kevin O'Connor34e9cc52010-06-06 11:07:08 -040093 struct mpt_ioapic *ioapic = (void*)bus;
Kevin O'Connor35746442009-02-27 20:54:51 -050094 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;
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500100 entrycount++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400101
102 /* irqs */
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500103 struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200104 int dev = -1;
Kevin O'Connord8388152013-03-18 20:26:54 -0400105 unsigned short pinmask = 0;
Gleb Natapov928d4df2009-12-24 14:29:43 +0200106
Kevin O'Connor3c3a3fa2013-03-18 20:14:21 -0400107 struct pci_device *pci;
Kevin O'Connor49f5ce52011-06-20 22:21:11 -0400108 foreachpci(pci) {
109 u16 bdf = pci->bdf;
Kevin O'Connor3c3a3fa2013-03-18 20:14:21 -0400110 if (pci_bdf_to_bus(bdf) != 0)
111 break;
Gleb Natapov928d4df2009-12-24 14:29:43 +0200112 int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
113 int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
114 if (pin == 0)
115 continue;
Kevin O'Connorb5bb9db2010-01-03 15:14:11 -0500116 if (dev != pci_bdf_to_busdev(bdf)) {
117 dev = pci_bdf_to_busdev(bdf);
Gleb Natapov928d4df2009-12-24 14:29:43 +0200118 pinmask = 0;
119 }
120 if (pinmask & (1 << pin)) /* pin was seen already */
121 continue;
122 pinmask |= (1 << pin);
Kevin O'Connorb64db302009-07-29 19:20:03 -0400123 memset(intsrc, 0, sizeof(*intsrc));
124 intsrc->type = MPT_TYPE_INTSRC;
Gleb Natapov928d4df2009-12-24 14:29:43 +0200125 intsrc->irqtype = 0; /* INT */
126 intsrc->irqflag = 1; /* active high */
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200127 intsrc->srcbus = pci_bdf_to_bus(bdf); /* PCI bus */
Kevin O'Connorb5bb9db2010-01-03 15:14:11 -0500128 intsrc->srcbusirq = (pci_bdf_to_dev(bdf) << 2) | (pin - 1);
Gleb Natapov928d4df2009-12-24 14:29:43 +0200129 intsrc->dstapic = ioapic_id;
130 intsrc->dstirq = irq;
131 intsrc++;
132 }
133
Kevin O'Connord8388152013-03-18 20:26:54 -0400134 int irq0_override = romfile_loadint("etc/irq0-override", 0);
Gleb Natapov928d4df2009-12-24 14:29:43 +0200135 for (i = 0; i < 16; i++) {
136 memset(intsrc, 0, sizeof(*intsrc));
Kevin O'Connord8388152013-03-18 20:26:54 -0400137 if (BUILD_PCI_IRQS & (1 << i))
Gleb Natapov928d4df2009-12-24 14:29:43 +0200138 continue;
139 intsrc->type = MPT_TYPE_INTSRC;
140 intsrc->irqtype = 0; /* INT */
141 intsrc->irqflag = 0; /* conform to bus spec */
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200142 intsrc->srcbus = isabusid; /* ISA bus */
Kevin O'Connorb64db302009-07-29 19:20:03 -0400143 intsrc->srcbusirq = i;
144 intsrc->dstapic = ioapic_id;
145 intsrc->dstirq = i;
Kevin O'Connord8388152013-03-18 20:26:54 -0400146 if (irq0_override) {
Kevin O'Connorb64db302009-07-29 19:20:03 -0400147 /* Destination 2 is covered by irq0->inti2 override (i ==
148 0). Source IRQ 2 is unused */
149 if (i == 0)
150 intsrc->dstirq = 2;
151 else if (i == 2)
152 intsrc--;
153 }
154 intsrc++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400155 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400156
Kevin O'Connorb021a572009-11-14 13:49:06 -0500157 /* Local interrupt assignment */
158 intsrc->type = MPT_TYPE_LOCAL_INT;
159 intsrc->irqtype = 3; /* ExtINT */
160 intsrc->irqflag = 0; /* PO, EL default */
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200161 intsrc->srcbus = isabusid; /* ISA */
Kevin O'Connorb021a572009-11-14 13:49:06 -0500162 intsrc->srcbusirq = 0;
163 intsrc->dstapic = 0; /* BSP == APIC #0 */
164 intsrc->dstirq = 0; /* LINTIN0 */
165 intsrc++;
Kevin O'Connorb021a572009-11-14 13:49:06 -0500166
167 intsrc->type = MPT_TYPE_LOCAL_INT;
168 intsrc->irqtype = 1; /* NMI */
169 intsrc->irqflag = 0; /* PO, EL default */
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200170 intsrc->srcbus = isabusid; /* ISA */
Kevin O'Connorb021a572009-11-14 13:49:06 -0500171 intsrc->srcbusirq = 0;
Kenji Kaneshige55e28a42011-10-10 14:06:29 +0800172 intsrc->dstapic = 0xff; /* to all local APICs */
Kevin O'Connorb021a572009-11-14 13:49:06 -0500173 intsrc->dstirq = 1; /* LINTIN1 */
174 intsrc++;
Kevin O'Connor34e9cc52010-06-06 11:07:08 -0400175 entrycount += intsrc - intsrcs;
Kevin O'Connorb021a572009-11-14 13:49:06 -0500176
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500177 // Finalize config structure.
Kevin O'Connor83626992009-12-26 11:15:31 -0500178 int length = (void*)intsrc - (void*)config;
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500179 config->entrycount = entrycount;
Kevin O'Connor83626992009-12-26 11:15:31 -0500180 config->length = length;
181 config->checksum -= checksum(config, length);
182
Kevin O'Connor48cf2322010-01-04 20:49:45 -0500183 // Allocate final memory locations. (In theory the config
184 // structure can go in high memory, but Linux kernels before
185 // v2.6.30 crash with that.)
Kevin O'Connor83626992009-12-26 11:15:31 -0500186 struct mptable_config_s *finalconfig = malloc_fseg(length);
187 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
188 if (!finalconfig || !floating) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500189 warn_noalloc();
Kevin O'Connor83626992009-12-26 11:15:31 -0500190 free(config);
191 free(finalconfig);
192 free(floating);
193 return;
194 }
195 memcpy(finalconfig, config, length);
196 free(config);
197
198 /* floating pointer structure */
199 memset(floating, 0, sizeof(*floating));
200 floating->signature = MPTABLE_SIGNATURE;
201 floating->physaddr = (u32)finalconfig;
202 floating->length = 1;
203 floating->spec_rev = 4;
204 floating->checksum -= checksum(floating, sizeof(*floating));
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400205
Kevin O'Connor2929c352009-07-25 13:48:27 -0400206 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
Kevin O'Connor83626992009-12-26 11:15:31 -0500207 floating, finalconfig, length);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400208}