blob: 6c81430cb105781626891007f1c4853a08b5f77a [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001#include <console/console.h>
Eric Biederman7003ba42004-10-16 06:20:29 +00002#include <device/path.h>
Yinghai Luf327d9f2008-02-20 17:41:38 +00003#include <device/pci_ids.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00004#include <cpu/cpu.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00005#include <arch/smp/mpspec.h>
6#include <string.h>
Eric Biedermanb78c1972004-10-14 20:54:17 +00007#include <arch/cpu.h>
8#include <cpu/x86/lapic.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +00009
Uwe Hermann55dc2232010-10-25 15:32:07 +000010/* Initialize the specified "mc" struct with initial values. */
Uwe Hermannc36d5062010-12-16 19:51:38 +000011void mptable_init(struct mp_config_table *mc, u32 lapic_addr)
Uwe Hermann55dc2232010-10-25 15:32:07 +000012{
Uwe Hermannc36d5062010-12-16 19:51:38 +000013 int i;
Uwe Hermann55dc2232010-10-25 15:32:07 +000014
15 memset(mc, 0, sizeof(*mc));
Uwe Hermannc36d5062010-12-16 19:51:38 +000016
Uwe Hermann55dc2232010-10-25 15:32:07 +000017 memcpy(mc->mpc_signature, MPC_SIGNATURE, 4);
Uwe Hermannc36d5062010-12-16 19:51:38 +000018
Uwe Hermann55dc2232010-10-25 15:32:07 +000019 mc->mpc_length = sizeof(*mc); /* Initially just the header size. */
20 mc->mpc_spec = 0x04; /* MultiProcessor specification 1.4 */
21 mc->mpc_checksum = 0; /* Not yet computed. */
Uwe Hermann55dc2232010-10-25 15:32:07 +000022 mc->mpc_oemptr = 0;
23 mc->mpc_oemsize = 0;
24 mc->mpc_entry_count = 0; /* No entries yet... */
25 mc->mpc_lapic = lapic_addr;
26 mc->mpe_length = 0;
27 mc->mpe_checksum = 0;
28 mc->reserved = 0;
Uwe Hermannc36d5062010-12-16 19:51:38 +000029
30 strncpy(mc->mpc_oem, CONFIG_MAINBOARD_VENDOR, 8);
31 strncpy(mc->mpc_productid, CONFIG_MAINBOARD_PART_NUMBER, 12);
32
33 /*
34 * The oem/productid fields are exactly 8/12 bytes long. If the resp.
35 * entry is shorter, the remaining bytes are filled with spaces.
36 */
37 for (i = MIN(strlen(CONFIG_MAINBOARD_VENDOR), 8); i < 8; i++)
38 mc->mpc_oem[i] = ' ';
39 for (i = MIN(strlen(CONFIG_MAINBOARD_PART_NUMBER), 12); i < 12; i++)
40 mc->mpc_productid[i] = ' ';
Uwe Hermann55dc2232010-10-25 15:32:07 +000041}
42
Eric Biederman8ca8d762003-04-22 19:02:15 +000043unsigned char smp_compute_checksum(void *v, int len)
44{
45 unsigned char *bytes;
46 unsigned char checksum;
47 int i;
48 bytes = v;
49 checksum = 0;
50 for(i = 0; i < len; i++) {
51 checksum -= bytes[i];
52 }
53 return checksum;
54}
55
56void *smp_write_floating_table(unsigned long addr)
57{
Eric Biederman8ca8d762003-04-22 19:02:15 +000058 /* 16 byte align the table address */
Yinghai Luf327d9f2008-02-20 17:41:38 +000059 addr = (addr + 0xf) & (~0xf);
Patrick Georgie9149032009-04-24 06:27:31 +000060 return smp_write_floating_table_physaddr(addr, addr + SMP_FLOATING_TABLE_LEN);
Eric Biederman8ca8d762003-04-22 19:02:15 +000061}
62
Stefan Reinauer4f1cb232006-07-19 15:32:49 +000063void *smp_write_floating_table_physaddr(unsigned long addr, unsigned long mpf_physptr)
64{
65 struct intel_mp_floating *mf;
66 void *v;
Stefan Reinauer14e22772010-04-27 06:56:47 +000067
Yinghai Luf327d9f2008-02-20 17:41:38 +000068 v = (void *)addr;
Stefan Reinauer4f1cb232006-07-19 15:32:49 +000069 mf = v;
70 mf->mpf_signature[0] = '_';
71 mf->mpf_signature[1] = 'M';
72 mf->mpf_signature[2] = 'P';
73 mf->mpf_signature[3] = '_';
74 mf->mpf_physptr = mpf_physptr;
75 mf->mpf_length = 1;
76 mf->mpf_specification = 4;
77 mf->mpf_checksum = 0;
78 mf->mpf_feature1 = 0;
79 mf->mpf_feature2 = 0;
80 mf->mpf_feature3 = 0;
81 mf->mpf_feature4 = 0;
82 mf->mpf_feature5 = 0;
83 mf->mpf_checksum = smp_compute_checksum(mf, mf->mpf_length*16);
84 return v;
85}
86
Eric Biederman8ca8d762003-04-22 19:02:15 +000087void *smp_next_mpc_entry(struct mp_config_table *mc)
88{
89 void *v;
90 v = (void *)(((char *)mc) + mc->mpc_length);
91 return v;
92}
93static void smp_add_mpc_entry(struct mp_config_table *mc, unsigned length)
94{
95 mc->mpc_length += length;
96 mc->mpc_entry_count++;
97}
98
99void *smp_next_mpe_entry(struct mp_config_table *mc)
100{
101 void *v;
102 v = (void *)(((char *)mc) + mc->mpc_length + mc->mpe_length);
103 return v;
104}
105static void smp_add_mpe_entry(struct mp_config_table *mc, mpe_t mpe)
106{
107 mc->mpe_length += mpe->mpe_length;
108}
109
110void smp_write_processor(struct mp_config_table *mc,
111 unsigned char apicid, unsigned char apicver,
112 unsigned char cpuflag, unsigned int cpufeature,
113 unsigned int featureflag)
114{
115 struct mpc_config_processor *mpc;
116 mpc = smp_next_mpc_entry(mc);
117 memset(mpc, '\0', sizeof(*mpc));
118 mpc->mpc_type = MP_PROCESSOR;
119 mpc->mpc_apicid = apicid;
120 mpc->mpc_apicver = apicver;
121 mpc->mpc_cpuflag = cpuflag;
122 mpc->mpc_cpufeature = cpufeature;
123 mpc->mpc_featureflag = featureflag;
124 smp_add_mpc_entry(mc, sizeof(*mpc));
125}
126
127/* If we assume a symmetric processor configuration we can
128 * get all of the information we need to write the processor
129 * entry from the bootstrap processor.
130 * Plus I don't think linux really even cares.
131 * Having the proper apicid's in the table so the non-bootstrap
132 * processors can be woken up should be enough.
133 */
Eric Biedermanb78c1972004-10-14 20:54:17 +0000134void smp_write_processors(struct mp_config_table *mc)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000135{
Eric Biedermanb78c1972004-10-14 20:54:17 +0000136 int boot_apic_id;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000137 unsigned apic_version;
138 unsigned cpu_features;
139 unsigned cpu_feature_flags;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000140 struct cpuid_result result;
141 device_t cpu;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000142
Eric Biedermanb78c1972004-10-14 20:54:17 +0000143 boot_apic_id = lapicid();
144 apic_version = lapic_read(LAPIC_LVR) & 0xff;
145 result = cpuid(1);
146 cpu_features = result.eax;
147 cpu_feature_flags = result.edx;
Eric Biederman7003ba42004-10-16 06:20:29 +0000148 for(cpu = all_devices; cpu; cpu = cpu->next) {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000149 unsigned long cpu_flag;
Stefan Reinauer14e22772010-04-27 06:56:47 +0000150 if ((cpu->path.type != DEVICE_PATH_APIC) ||
Eric Biederman7003ba42004-10-16 06:20:29 +0000151 (cpu->bus->dev->path.type != DEVICE_PATH_APIC_CLUSTER))
152 {
Eric Biederman8ca8d762003-04-22 19:02:15 +0000153 continue;
Eric Biederman8ca8d762003-04-22 19:02:15 +0000154 }
Eric Biedermanb78c1972004-10-14 20:54:17 +0000155 if (!cpu->enabled) {
156 continue;
157 }
158 cpu_flag = MPC_CPU_ENABLED;
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000159 if (boot_apic_id == cpu->path.apic.apic_id) {
Eric Biedermanf3ed1cf2004-10-16 08:38:58 +0000160 cpu_flag = MPC_CPU_ENABLED | MPC_CPU_BOOTPROCESSOR;
Eric Biedermanb78c1972004-10-14 20:54:17 +0000161 }
Stefan Reinauer14e22772010-04-27 06:56:47 +0000162 smp_write_processor(mc,
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000163 cpu->path.apic.apic_id, apic_version,
Eric Biederman8ca8d762003-04-22 19:02:15 +0000164 cpu_flag, cpu_features, cpu_feature_flags
165 );
Eric Biederman8ca8d762003-04-22 19:02:15 +0000166 }
167}
168
Patrick Georgi patrick612fcc32010-11-23 07:19:54 +0000169static void smp_write_bus(struct mp_config_table *mc,
Myles Watson6e235762009-09-29 14:56:15 +0000170 unsigned char id, const char *bustype)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000171{
172 struct mpc_config_bus *mpc;
173 mpc = smp_next_mpc_entry(mc);
174 memset(mpc, '\0', sizeof(*mpc));
175 mpc->mpc_type = MP_BUS;
176 mpc->mpc_busid = id;
177 memcpy(mpc->mpc_bustype, bustype, sizeof(mpc->mpc_bustype));
178 smp_add_mpc_entry(mc, sizeof(*mpc));
179}
180
181void smp_write_ioapic(struct mp_config_table *mc,
Stefan Reinauer14e22772010-04-27 06:56:47 +0000182 unsigned char id, unsigned char ver,
Eric Biedermanb78c1972004-10-14 20:54:17 +0000183 unsigned long apicaddr)
Eric Biederman8ca8d762003-04-22 19:02:15 +0000184{
185 struct mpc_config_ioapic *mpc;
186 mpc = smp_next_mpc_entry(mc);
187 memset(mpc, '\0', sizeof(*mpc));
188 mpc->mpc_type = MP_IOAPIC;
189 mpc->mpc_apicid = id;
190 mpc->mpc_apicver = ver;
191 mpc->mpc_flags = MPC_APIC_USABLE;
192 mpc->mpc_apicaddr = apicaddr;
193 smp_add_mpc_entry(mc, sizeof(*mpc));
194}
195
196void smp_write_intsrc(struct mp_config_table *mc,
197 unsigned char irqtype, unsigned short irqflag,
198 unsigned char srcbus, unsigned char srcbusirq,
199 unsigned char dstapic, unsigned char dstirq)
200{
201 struct mpc_config_intsrc *mpc;
202 mpc = smp_next_mpc_entry(mc);
203 memset(mpc, '\0', sizeof(*mpc));
204 mpc->mpc_type = MP_INTSRC;
205 mpc->mpc_irqtype = irqtype;
206 mpc->mpc_irqflag = irqflag;
207 mpc->mpc_srcbus = srcbus;
208 mpc->mpc_srcbusirq = srcbusirq;
209 mpc->mpc_dstapic = dstapic;
210 mpc->mpc_dstirq = dstirq;
211 smp_add_mpc_entry(mc, sizeof(*mpc));
Stefan Reinauer5bba7522009-04-21 23:00:14 +0000212#ifdef DEBUG_MPTABLE
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000213 printk(BIOS_DEBUG, "add intsrc srcbus 0x%x srcbusirq 0x%x, dstapic 0x%x, dstirq 0x%x\n",
Eric Biederman8ca8d762003-04-22 19:02:15 +0000214 srcbus, srcbusirq, dstapic, dstirq);
Myles Watson552b3272009-02-12 21:30:06 +0000215 hexdump(__func__, mpc, sizeof(*mpc));
Eric Biederman8ca8d762003-04-22 19:02:15 +0000216#endif
217}
218
Yinghai Luf327d9f2008-02-20 17:41:38 +0000219void smp_write_intsrc_pci_bridge(struct mp_config_table *mc,
220 unsigned char irqtype, unsigned short irqflag,
221 struct device *dev,
222 unsigned char dstapic, unsigned char *dstirq)
223{
224 struct device *child;
225
Yinghai Luf327d9f2008-02-20 17:41:38 +0000226 int i;
227 int srcbus;
228 int slot;
229
230 struct bus *link;
231 unsigned char dstirq_x[4];
232
Myles Watson894a3472010-06-09 22:41:35 +0000233 for (link = dev->link_list; link; link = link->next) {
Yinghai Luf327d9f2008-02-20 17:41:38 +0000234
Yinghai Luf327d9f2008-02-20 17:41:38 +0000235 child = link->children;
236 srcbus = link->secondary;
237
238 while (child) {
239 if (child->path.type != DEVICE_PATH_PCI)
240 goto next;
241
Stefan Reinauer2b34db82009-02-28 20:10:20 +0000242 slot = (child->path.pci.devfn >> 3);
Yinghai Luf327d9f2008-02-20 17:41:38 +0000243 /* round pins */
244 for (i = 0; i < 4; i++)
245 dstirq_x[i] = dstirq[(i + slot) % 4];
246
247 if ((child->class >> 16) != PCI_BASE_CLASS_BRIDGE) {
248 /* pci device */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000249 printk(BIOS_DEBUG, "route irq: %s\n", dev_path(child));
Yinghai Luf327d9f2008-02-20 17:41:38 +0000250 for (i = 0; i < 4; i++)
251 smp_write_intsrc(mc, irqtype, irqflag, srcbus, (slot<<2)|i, dstapic, dstirq_x[i]);
252 goto next;
253 }
254
255 switch (child->class>>8) {
256 case PCI_CLASS_BRIDGE_PCI:
257 case PCI_CLASS_BRIDGE_PCMCIA:
258 case PCI_CLASS_BRIDGE_CARDBUS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000259 printk(BIOS_DEBUG, "route irq bridge: %s\n", dev_path(child));
Yinghai Luf327d9f2008-02-20 17:41:38 +0000260 smp_write_intsrc_pci_bridge(mc, irqtype, irqflag, child, dstapic, dstirq_x);
261 }
262
263 next:
264 child = child->sibling;
265 }
266
267 }
268}
Eric Biederman8ca8d762003-04-22 19:02:15 +0000269
270void smp_write_lintsrc(struct mp_config_table *mc,
271 unsigned char irqtype, unsigned short irqflag,
272 unsigned char srcbusid, unsigned char srcbusirq,
273 unsigned char destapic, unsigned char destapiclint)
274{
275 struct mpc_config_lintsrc *mpc;
276 mpc = smp_next_mpc_entry(mc);
277 memset(mpc, '\0', sizeof(*mpc));
278 mpc->mpc_type = MP_LINTSRC;
279 mpc->mpc_irqtype = irqtype;
280 mpc->mpc_irqflag = irqflag;
281 mpc->mpc_srcbusid = srcbusid;
282 mpc->mpc_srcbusirq = srcbusirq;
283 mpc->mpc_destapic = destapic;
284 mpc->mpc_destapiclint = destapiclint;
285 smp_add_mpc_entry(mc, sizeof(*mpc));
286}
287
288void smp_write_address_space(struct mp_config_table *mc,
289 unsigned char busid, unsigned char address_type,
290 unsigned int address_base_low, unsigned int address_base_high,
291 unsigned int address_length_low, unsigned int address_length_high)
292{
293 struct mp_exten_system_address_space *mpe;
294 mpe = smp_next_mpe_entry(mc);
295 memset(mpe, '\0', sizeof(*mpe));
296 mpe->mpe_type = MPE_SYSTEM_ADDRESS_SPACE;
297 mpe->mpe_length = sizeof(*mpe);
298 mpe->mpe_busid = busid;
299 mpe->mpe_address_type = address_type;
300 mpe->mpe_address_base_low = address_base_low;
301 mpe->mpe_address_base_high = address_base_high;
302 mpe->mpe_address_length_low = address_length_low;
303 mpe->mpe_address_length_high = address_length_high;
304 smp_add_mpe_entry(mc, (mpe_t)mpe);
305}
306
307
308void smp_write_bus_hierarchy(struct mp_config_table *mc,
309 unsigned char busid, unsigned char bus_info,
310 unsigned char parent_busid)
311{
312 struct mp_exten_bus_hierarchy *mpe;
313 mpe = smp_next_mpe_entry(mc);
314 memset(mpe, '\0', sizeof(*mpe));
315 mpe->mpe_type = MPE_BUS_HIERARCHY;
316 mpe->mpe_length = sizeof(*mpe);
317 mpe->mpe_busid = busid;
318 mpe->mpe_bus_info = bus_info;
319 mpe->mpe_parent_busid = parent_busid;
320 smp_add_mpe_entry(mc, (mpe_t)mpe);
321}
322
323void smp_write_compatibility_address_space(struct mp_config_table *mc,
324 unsigned char busid, unsigned char address_modifier,
325 unsigned int range_list)
326{
327 struct mp_exten_compatibility_address_space *mpe;
328 mpe = smp_next_mpe_entry(mc);
329 memset(mpe, '\0', sizeof(*mpe));
330 mpe->mpe_type = MPE_COMPATIBILITY_ADDRESS_SPACE;
331 mpe->mpe_length = sizeof(*mpe);
332 mpe->mpe_busid = busid;
333 mpe->mpe_address_modifier = address_modifier;
334 mpe->mpe_range_list = range_list;
335 smp_add_mpe_entry(mc, (mpe_t)mpe);
336}
337
Patrick Georgi6eb7a532011-10-07 21:42:52 +0200338void mptable_lintsrc(struct mp_config_table *mc, unsigned long bus_isa)
339{
340 smp_write_lintsrc(mc, mp_ExtINT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, MP_APIC_ALL, 0x0);
341 smp_write_lintsrc(mc, mp_NMI, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, MP_APIC_ALL, 0x1);
342}
343
Patrick Georgic5b87c82010-05-20 15:28:19 +0000344void mptable_add_isa_interrupts(struct mp_config_table *mc, unsigned long bus_isa, unsigned long apicid, int external_int2)
345{
346/*I/O Ints: Type Trigger Polarity Bus ID IRQ APIC ID PIN# */
347 smp_write_intsrc(mc, external_int2?mp_INT:mp_ExtINT,
348 MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, apicid, 0x0);
349 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x1, apicid, 0x1);
350 smp_write_intsrc(mc, external_int2?mp_ExtINT:mp_INT,
351 MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x0, apicid, 0x2);
352 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x3, apicid, 0x3);
353 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x4, apicid, 0x4);
354 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x6, apicid, 0x6);
355 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x7, apicid, 0x7);
356 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x8, apicid, 0x8);
357 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0x9, apicid, 0x9);
358 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xa, apicid, 0xa);
359 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xb, apicid, 0xb);
360 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xc, apicid, 0xc);
361 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xd, apicid, 0xd);
362 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xe, apicid, 0xe);
363 smp_write_intsrc(mc, mp_INT, MP_IRQ_TRIGGER_EDGE|MP_IRQ_POLARITY_HIGH, bus_isa, 0xf, apicid, 0xf);
364}
Patrick Georgiab272662010-06-25 13:43:22 +0000365
366void mptable_write_buses(struct mp_config_table *mc, int *max_pci_bus, int *isa_bus) {
367 int dummy, i, highest;
368 char buses[256];
369 struct device *dev;
370
371 if (!max_pci_bus) max_pci_bus = &dummy;
372 if (!isa_bus) isa_bus = &dummy;
373
374 *max_pci_bus = 0;
375 highest = 0;
376 memset(buses, 0, sizeof(buses));
377
378 for (dev = all_devices; dev; dev = dev->next) {
379 struct bus *bus;
380 for (bus = dev->link_list; bus; bus = bus->next) {
381 if (bus->secondary > 255) {
382 printk(BIOS_ERR, "A bus claims to have a bus ID > 255?!? Aborting");
383 return;
384 }
385 buses[bus->secondary] = 1;
386 if (highest < bus->secondary) highest = bus->secondary;
387 }
388 }
389 for (i=0; i <= highest; i++) {
390 if (buses[i]) {
391 smp_write_bus(mc, i, "PCI ");
392 *max_pci_bus = i;
393 }
394 }
395 *isa_bus = *max_pci_bus + 1;
396 smp_write_bus(mc, *isa_bus, "ISA ");
397}
398