blob: 2409fcfc6be47762f3e9af72bd85992846f76f3a [file] [log] [blame]
Kevin O'Connor84ad59a2008-07-04 05:47:26 -04001// MPTable generation (on emulators)
2//
Kevin O'Connor2929c352009-07-25 13:48:27 -04003// Copyright (C) 2008,2009 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'Connor86916ce2009-11-14 13:34:27 -050023 // Allocate memory
Kevin O'Connor8c8a8802009-07-29 19:44:42 -040024 int length = (sizeof(struct mptable_config_s)
Kevin O'Connora26df9b2009-10-09 09:42:11 -040025 + sizeof(struct mpt_cpu) * MaxCountCPUs
Gleb Natapov928d4df2009-12-24 14:29:43 +020026 + sizeof(struct mpt_bus) * 2
Kevin O'Connor35746442009-02-27 20:54:51 -050027 + sizeof(struct mpt_ioapic)
Gleb Natapov928d4df2009-12-24 14:29:43 +020028 + sizeof(struct mpt_intsrc) * 34);
Kevin O'Connor8c8a8802009-07-29 19:44:42 -040029 struct mptable_config_s *config = malloc_fseg(length);
30 struct mptable_floating_s *floating = malloc_fseg(sizeof(*floating));
31 if (!config || !floating) {
Kevin O'Connor35746442009-02-27 20:54:51 -050032 dprintf(1, "No room for MPTABLE!\n");
Kevin O'Connor86916ce2009-11-14 13:34:27 -050033 free(config);
34 free(floating);
Kevin O'Connor35746442009-02-27 20:54:51 -050035 return;
36 }
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040037
Kevin O'Connor35746442009-02-27 20:54:51 -050038 /* floating pointer structure */
Kevin O'Connor35746442009-02-27 20:54:51 -050039 memset(floating, 0, sizeof(*floating));
Kevin O'Connor35746442009-02-27 20:54:51 -050040 floating->signature = MPTABLE_SIGNATURE;
41 floating->physaddr = (u32)config;
42 floating->length = 1;
43 floating->spec_rev = 4;
Kevin O'Connor523e5a92009-07-04 13:46:33 -040044 floating->checksum -= checksum(floating, sizeof(*floating));
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040045
Kevin O'Connor35746442009-02-27 20:54:51 -050046 // Config structure.
47 memset(config, 0, sizeof(*config));
48 config->signature = MPCONFIG_SIGNATURE;
Kevin O'Connor35746442009-02-27 20:54:51 -050049 config->spec = 4;
50 memcpy(config->oemid, CONFIG_CPUNAME8, sizeof(config->oemid));
51 memcpy(config->productid, "0.1 ", sizeof(config->productid));
Kevin O'Connor35746442009-02-27 20:54:51 -050052 config->lapic = BUILD_APIC_ADDR;
53
Kevin O'Connor86916ce2009-11-14 13:34:27 -050054 // Detect cpu info
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040055 u32 cpuid_signature, ebx, ecx, cpuid_features;
56 cpuid(1, &cpuid_signature, &ebx, &ecx, &cpuid_features);
Kevin O'Connor86916ce2009-11-14 13:34:27 -050057 int pkgcpus = 1;
58 if (cpuid_features & (1 << 28)) {
59 /* Only populate the MPS tables with the first logical CPU in
60 each package */
61 pkgcpus = (ebx >> 16) & 0xff;
62 pkgcpus = 1 << (__fls(pkgcpus - 1) + 1); /* round up to power of 2 */
63 }
Kevin O'Connorc0ad0e82009-11-09 19:20:21 -050064
Kevin O'Connor86916ce2009-11-14 13:34:27 -050065 // CPU definitions.
66 struct mpt_cpu *cpus = (void*)&config[1], *cpu = cpus;
67 int i;
68 for (i = 0; i < MaxCountCPUs; i+=pkgcpus) {
Kevin O'Connor35746442009-02-27 20:54:51 -050069 memset(cpu, 0, sizeof(*cpu));
70 cpu->type = MPT_TYPE_CPU;
71 cpu->apicid = i;
72 cpu->apicver = 0x11;
73 /* cpu flags: enabled, bootstrap cpu */
Kevin O'Connor86916ce2009-11-14 13:34:27 -050074 cpu->cpuflag = (i < CountCPUs) | ((i == 0) << 1);
Kevin O'Connore97ca7b2009-06-21 09:10:28 -040075 if (cpuid_signature) {
76 cpu->cpusignature = cpuid_signature;
77 cpu->featureflag = cpuid_features;
78 } else {
79 cpu->cpusignature = 0x600;
80 cpu->featureflag = 0x201;
81 }
Kevin O'Connor86916ce2009-11-14 13:34:27 -050082 cpu++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040083 }
Kevin O'Connor86916ce2009-11-14 13:34:27 -050084 int entrycount = cpu - cpus;
Kevin O'Connorc0ad0e82009-11-09 19:20:21 -050085
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040086 /* isa bus */
Kevin O'Connor86916ce2009-11-14 13:34:27 -050087 struct mpt_bus *bus = (void*)cpu;
Kevin O'Connor35746442009-02-27 20:54:51 -050088 memset(bus, 0, sizeof(*bus));
89 bus->type = MPT_TYPE_BUS;
Gleb Natapov928d4df2009-12-24 14:29:43 +020090 bus->busid = 1;
Kevin O'Connor35746442009-02-27 20:54:51 -050091 memcpy(bus->bustype, "ISA ", sizeof(bus->bustype));
Kevin O'Connor86916ce2009-11-14 13:34:27 -050092 entrycount++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -040093
Gleb Natapov928d4df2009-12-24 14:29:43 +020094 bus++;
95 memset(bus, 0, sizeof(*bus));
96 bus->type = MPT_TYPE_BUS;
97 bus->busid = 0;
98 memcpy(bus->bustype, "PCI ", sizeof(bus->bustype));
99 entrycount++;
100
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400101 /* ioapic */
Kevin O'Connora26df9b2009-10-09 09:42:11 -0400102 u8 ioapic_id = CountCPUs;
Kevin O'Connor35746442009-02-27 20:54:51 -0500103 struct mpt_ioapic *ioapic = (void*)&bus[1];
104 memset(ioapic, 0, sizeof(*ioapic));
105 ioapic->type = MPT_TYPE_IOAPIC;
106 ioapic->apicid = ioapic_id;
107 ioapic->apicver = 0x11;
108 ioapic->flags = 1; // enable
109 ioapic->apicaddr = BUILD_IOAPIC_ADDR;
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500110 entrycount++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400111
112 /* irqs */
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500113 struct mpt_intsrc *intsrcs = (void*)&ioapic[1], *intsrc = intsrcs;
Gleb Natapov928d4df2009-12-24 14:29:43 +0200114 int bdf, max, dev = -1;
115 unsigned short mask = 0, pinmask;
116
117 foreachpci(bdf, max) {
118 int pin = pci_config_readb(bdf, PCI_INTERRUPT_PIN);
119 int irq = pci_config_readb(bdf, PCI_INTERRUPT_LINE);
120 if (pin == 0)
121 continue;
122 if (dev != pci_bdf_to_dev(bdf)) {
123 dev = pci_bdf_to_dev(bdf);
124 pinmask = 0;
125 }
126 if (pinmask & (1 << pin)) /* pin was seen already */
127 continue;
128 pinmask |= (1 << pin);
129 mask |= (1 << irq);
Kevin O'Connorb64db302009-07-29 19:20:03 -0400130 memset(intsrc, 0, sizeof(*intsrc));
131 intsrc->type = MPT_TYPE_INTSRC;
Gleb Natapov928d4df2009-12-24 14:29:43 +0200132 intsrc->irqtype = 0; /* INT */
133 intsrc->irqflag = 1; /* active high */
134 intsrc->srcbus = 0; /* PCI bus */
135 intsrc->srcbusirq = (dev << 2) | (pin - 1);
136 intsrc->dstapic = ioapic_id;
137 intsrc->dstirq = irq;
138 intsrc++;
139 }
140
141 for (i = 0; i < 16; i++) {
142 memset(intsrc, 0, sizeof(*intsrc));
143 if (mask & (1 << i))
144 continue;
145 intsrc->type = MPT_TYPE_INTSRC;
146 intsrc->irqtype = 0; /* INT */
147 intsrc->irqflag = 0; /* conform to bus spec */
148 intsrc->srcbus = 1; /* ISA bus */
Kevin O'Connorb64db302009-07-29 19:20:03 -0400149 intsrc->srcbusirq = i;
150 intsrc->dstapic = ioapic_id;
151 intsrc->dstirq = i;
Kevin O'Connor4d2b6192009-10-08 21:37:21 -0400152 if (qemu_cfg_irq0_override()) {
Kevin O'Connorb64db302009-07-29 19:20:03 -0400153 /* Destination 2 is covered by irq0->inti2 override (i ==
154 0). Source IRQ 2 is unused */
155 if (i == 0)
156 intsrc->dstirq = 2;
157 else if (i == 2)
158 intsrc--;
159 }
160 intsrc++;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400161 }
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500162 entrycount += intsrc - intsrcs;
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400163
Kevin O'Connorb021a572009-11-14 13:49:06 -0500164 /* Local interrupt assignment */
165 intsrc->type = MPT_TYPE_LOCAL_INT;
166 intsrc->irqtype = 3; /* ExtINT */
167 intsrc->irqflag = 0; /* PO, EL default */
Gleb Natapov928d4df2009-12-24 14:29:43 +0200168 intsrc->srcbus = 1; /* ISA */
Kevin O'Connorb021a572009-11-14 13:49:06 -0500169 intsrc->srcbusirq = 0;
170 intsrc->dstapic = 0; /* BSP == APIC #0 */
171 intsrc->dstirq = 0; /* LINTIN0 */
172 intsrc++;
173 entrycount++;
174
175 intsrc->type = MPT_TYPE_LOCAL_INT;
176 intsrc->irqtype = 1; /* NMI */
177 intsrc->irqflag = 0; /* PO, EL default */
Gleb Natapov928d4df2009-12-24 14:29:43 +0200178 intsrc->srcbus = 1; /* ISA */
Kevin O'Connorb021a572009-11-14 13:49:06 -0500179 intsrc->srcbusirq = 0;
180 intsrc->dstapic = 0; /* BSP == APIC #0 */
181 intsrc->dstirq = 1; /* LINTIN1 */
182 intsrc++;
183 entrycount++;
184
Kevin O'Connor86916ce2009-11-14 13:34:27 -0500185 // Finalize config structure.
186 config->entrycount = entrycount;
Kevin O'Connorb64db302009-07-29 19:20:03 -0400187 config->length = (void*)intsrc - (void*)config;
Kevin O'Connor523e5a92009-07-04 13:46:33 -0400188 config->checksum -= checksum(config, config->length);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400189
Kevin O'Connor2929c352009-07-25 13:48:27 -0400190 dprintf(1, "MP table addr=%p MPC table addr=%p size=%d\n",
Kevin O'Connorb64db302009-07-29 19:20:03 -0400191 floating, config, config->length);
Kevin O'Connor84ad59a2008-07-04 05:47:26 -0400192}