blob: e5952c3ad706a2e6867dcf749338ca809e71fe8e [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'Connor86916ce2009-11-14 13:34:27 -050011#include "paravirt.h" // qemu_cfg_irq0_override
Gleb Natapov928d4df2009-12-24 14:29:43 +020012#include "pci.h"
13#include "pci_regs.h"
Kevin O'Connorc0ad0e82009-11-09 19:20:21 -050014
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040015void
16mptable_init(void)
17{
Kevin O'Connord3817022008-07-12 12:46:07 -040018 if (! CONFIG_MPTABLE)
19 return;
20
Kevin O'Connor7b49cd92008-11-08 10:35:26 -050021 dprintf(3, "init MPTable\n");
22
Kevin O'Connor83626992009-12-26 11:15:31 -050023 // Config structure in temp area.
Kevin O'Connor34e9cc52010-06-06 11:07:08 -040024 struct mptable_config_s *config = malloc_tmp(32*1024);
Kevin O'Connor83626992009-12-26 11:15:31 -050025 if (!config) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050026 warn_noalloc();
Kevin O'Connor35746442009-02-27 20:54:51 -050027 return;
28 }
Kevin O'Connor35746442009-02-27 20:54:51 -050029 memset(config, 0, sizeof(*config));
30 config->signature = MPCONFIG_SIGNATURE;
Kevin O'Connor35746442009-02-27 20:54:51 -050031 config->spec = 4;
32 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
33 memcpy(config->productid, "0.1 ", sizeof(config->productid));
Kevin O'Connor35746442009-02-27 20:54:51 -050034 config->lapic = BUILD_APIC_ADDR;
35
Kevin O'Connor86916ce2009-11-14 13:34:27 -050036 // Detect cpu info
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040037 u32 cpuid_signature, ebx, ecx, cpuid_features;
38 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
Kevin O'Connorbf7f1f32010-02-11 22:19:14 -050039 if (! cpuid_signature) {
40 // Use default values.
41 cpuid_signature = 0x600;
42 cpuid_features = 0x201;
43 }
Kevin O'Connor86916ce2009-11-14 13:34:27 -050044 int pkgcpus = 1;
45 if (cpuid_features & (1 << 28)) {
46 /* Only populate the MPS tables with the first logical CPU in
47 each package */
48 pkgcpus = (ebx >> 16) & 0xff;
49 pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
50 }
Kevin O'Connorbf7f1f32010-02-11 22:19:14 -050051 u8 apic_version = readl((u8*)BUILD_APIC_ADDR + 0x30) & 0xff;
Kevin O'Connorc0ad0e82009-11-09 19:20:21 -050052
Kevin O'Connor86916ce2009-11-14 13:34:27 -050053 // CPU definitions.
54 struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
55 int i;
56 for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
Kevin O'Connor35746442009-02-27 20:54:51 -050057 memset(cpu, 0, sizeof(*cpu));
58 cpu->type = MPT_TYPE_CPU;
59 cpu->apicid = i;
Magnus Christensson41c09572009-11-23 14:01:19 +010060 cpu->apicver = apic_version;
Kevin O'Connor35746442009-02-27 20:54:51 -050061 /* cpu flags: enabled, bootstrap cpu */
Kevin O'Connor9fd48512010-07-26 22:34:37 -040062 cpu->cpuflag = ((i<CountCPUs) ? 0x01 : 0x00) | ((i==0) ? 0x02 : 0x00);
Kevin O'Connorbf7f1f32010-02-11 22:19:14 -050063 cpu->cpusignature = cpuid_signature;
64 cpu->featureflag = cpuid_features;
Kevin O'Connor86916ce2009-11-14 13:34:27 -050065 cpu++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040066 }
Kevin O'Connor86916ce2009-11-14 13:34:27 -050067 int entrycount = cpu - cpus;
Kevin O'Connorc0ad0e82009-11-09 19:20:21 -050068
Kevin O'Connor5c99b6c2009-12-30 12:36:22 -050069 // PCI buses
Kevin O'Connor34e9cc52010-06-06 11:07:08 -040070 struct mpt_bus *buses = (void*)cpu, *bus = buses;
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020071 int bdf, max, lastbus = -1;
72 foreachpci(bdf, max) {
73 int curbus = pci_bdf_to_bus(bdf);
74 if (curbus == lastbus)
75 continue;
76 lastbus = curbus;
77 memset(bus, 0, sizeof(*bus));
78 bus->type = MPT_TYPE_BUS;
79 bus->busid = curbus;
80 memcpy(bus->bustype, "PCI ", sizeof(bus->bustype));
81 bus++;
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020082 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040083
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020084 /* isa bus */
85 int isabusid;
Gleb Natapov928d4df2009-12-24 14:29:43 +020086 memset(bus, 0, sizeof(*bus));
87 bus->type = MPT_TYPE_BUS;
Gleb Natapov4c94b7e2009-12-28 11:04:46 +020088 isabusid = bus->busid = lastbus + 1;
89 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
Kevin O'Connor34e9cc52010-06-06 11:07:08 -040090 bus++;
91 entrycount += bus - buses;
Gleb Natapov928d4df2009-12-24 14:29:43 +020092
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040093 /* ioapic */
Kevin O'Connora26df9b2009-10-09 09:42:11 -040094 u8 ioapic_id = CountCPUs;
Kevin O'Connor34e9cc52010-06-06 11:07:08 -040095 struct mpt_ioapic *ioapic = (void*)bus;
Kevin O'Connor35746442009-02-27 20:54:51 -050096 memset(ioapic, 0, sizeof(*ioapic));
97 ioapic->type = MPT_TYPE_IOAPIC;
98 ioapic->apicid = ioapic_id;
99 ioapic->apicver = 0x11;
100 ioapic->flags = 1; // enable
101 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500102 entrycount++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400103
104 /* irqs */
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500105 struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200106 int dev = -1;
Kevin O'Connor5c99b6c2009-12-30 12:36:22 -0500107 unsigned short mask = 0, pinmask = 0;
Gleb Natapov928d4df2009-12-24 14:29:43 +0200108
109 foreachpci(bdf, max) {
110 int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
111 int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
112 if (pin == 0)
113 continue;
Kevin O'Connorb5bb9db2010-01-03 15:14:11 -0500114 if (dev != pci_bdf_to_busdev(bdf)) {
115 dev = pci_bdf_to_busdev(bdf);
Gleb Natapov928d4df2009-12-24 14:29:43 +0200116 pinmask = 0;
117 }
118 if (pinmask & (1 << pin)) /* pin was seen already */
119 continue;
120 pinmask |= (1 << pin);
121 mask |= (1 << irq);
Kevin O'Connorb64db302009-07-29 19:20:03 -0400122 memset(intsrc, 0, sizeof(*intsrc));
123 intsrc->type = MPT_TYPE_INTSRC;
Gleb Natapov928d4df2009-12-24 14:29:43 +0200124 intsrc->irqtype = 0; /* INT */
125 intsrc->irqflag = 1; /* active high */
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200126 intsrc->srcbus = pci_bdf_to_bus(bdf); /* PCI bus */
Kevin O'Connorb5bb9db2010-01-03 15:14:11 -0500127 intsrc->srcbusirq = (pci_bdf_to_dev(bdf) << 2) | (pin - 1);
Gleb Natapov928d4df2009-12-24 14:29:43 +0200128 intsrc->dstapic = ioapic_id;
129 intsrc->dstirq = irq;
130 intsrc++;
131 }
132
133 for (i = 0; i < 16; i++) {
134 memset(intsrc, 0, sizeof(*intsrc));
135 if (mask & (1 << i))
136 continue;
137 intsrc->type = MPT_TYPE_INTSRC;
138 intsrc->irqtype = 0; /* INT */
139 intsrc->irqflag = 0; /* conform to bus spec */
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200140 intsrc->srcbus = isabusid; /* ISA bus */
Kevin O'Connorb64db302009-07-29 19:20:03 -0400141 intsrc->srcbusirq = i;
142 intsrc->dstapic = ioapic_id;
143 intsrc->dstirq = i;
Kevin O'Connor4d2b6192009-10-08 21:37:21 -0400144 if (qemu_cfg_irq0_override()) {
Kevin O'Connorb64db302009-07-29 19:20:03 -0400145 /* Destination 2 is covered by irq0->inti2 override (i ==
146 0). Source IRQ 2 is unused */
147 if (i == 0)
148 intsrc->dstirq = 2;
149 else if (i == 2)
150 intsrc--;
151 }
152 intsrc++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400153 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400154
Kevin O'Connorb021a572009-11-14 13:49:06 -0500155 /* Local interrupt assignment */
156 intsrc->type = MPT_TYPE_LOCAL_INT;
157 intsrc->irqtype = 3; /* ExtINT */
158 intsrc->irqflag = 0; /* PO, EL default */
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200159 intsrc->srcbus = isabusid; /* ISA */
Kevin O'Connorb021a572009-11-14 13:49:06 -0500160 intsrc->srcbusirq = 0;
161 intsrc->dstapic = 0; /* BSP == APIC #0 */
162 intsrc->dstirq = 0; /* LINTIN0 */
163 intsrc++;
Kevin O'Connorb021a572009-11-14 13:49:06 -0500164
165 intsrc->type = MPT_TYPE_LOCAL_INT;
166 intsrc->irqtype = 1; /* NMI */
167 intsrc->irqflag = 0; /* PO, EL default */
Gleb Natapov4c94b7e2009-12-28 11:04:46 +0200168 intsrc->srcbus = isabusid; /* ISA */
Kevin O'Connorb021a572009-11-14 13:49:06 -0500169 intsrc->srcbusirq = 0;
170 intsrc->dstapic = 0; /* BSP == APIC #0 */
171 intsrc->dstirq = 1; /* LINTIN1 */
172 intsrc++;
Kevin O'Connor34e9cc52010-06-06 11:07:08 -0400173 entrycount += intsrc - intsrcs;
Kevin O'Connorb021a572009-11-14 13:49:06 -0500174
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500175 // Finalize config structure.
Kevin O'Connor83626992009-12-26 11:15:31 -0500176 int length = (void*)intsrc - (void*)config;
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500177 config->entrycount = entrycount;
Kevin O'Connor83626992009-12-26 11:15:31 -0500178 config->length = length;
179 config->checksum -= checksum(config, length);
180
Kevin O'Connor48cf2322010-01-04 20:49:45 -0500181 // Allocate final memory locations. (In theory the config
182 // structure can go in high memory, but Linux kernels before
183 // v2.6.30 crash with that.)
Kevin O'Connor83626992009-12-26 11:15:31 -0500184 struct mptable_config_s *finalconfig = malloc_fseg(length);
185 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
186 if (!finalconfig || !floating) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500187 warn_noalloc();
Kevin O'Connor83626992009-12-26 11:15:31 -0500188 free(config);
189 free(finalconfig);
190 free(floating);
191 return;
192 }
193 memcpy(finalconfig, config, length);
194 free(config);
195
196 /* floating pointer structure */
197 memset(floating, 0, sizeof(*floating));
198 floating->signature = MPTABLE_SIGNATURE;
199 floating->physaddr = (u32)finalconfig;
200 floating->length = 1;
201 floating->spec_rev = 4;
202 floating->checksum -= checksum(floating, sizeof(*floating));
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400203
Kevin O'Connor2929c352009-07-25 13:48:27 -0400204 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
Kevin O'Connor83626992009-12-26 11:15:31 -0500205 floating, finalconfig, length);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400206}