blob: 32b6977f80c00430ad38d48f9ddd0436cb138f5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel Multiprocessor Specification 1.1 and 1.4
3 * compliant MP-table parsing routines.
4 *
5 * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
6 * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
7 *
8 * Fixes
9 * Erich Boleyn : MP v1.4 and additional changes.
10 * Alan Cox : Added EBDA scanning
11 * Ingo Molnar : various cleanups and rewrites
12 * Maciej W. Rozycki: Bits for default MP configurations
13 * Paul Diefenbaugh: Added full ACPI support
14 */
15
16#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/bootmem.h>
20#include <linux/smp_lock.h>
21#include <linux/kernel_stat.h>
22#include <linux/mc146818rtc.h>
23#include <linux/acpi.h>
Christoph Lameter8c5a0902005-06-23 00:08:18 -070024#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/smp.h>
27#include <asm/mtrr.h>
28#include <asm/mpspec.h>
29#include <asm/pgalloc.h>
30#include <asm/io_apic.h>
31#include <asm/proto.h>
Andi Kleen8d916402005-05-31 14:39:26 -070032#include <asm/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34/* Have we found an MP table */
35int smp_found_config;
36unsigned int __initdata maxcpus = NR_CPUS;
37
38int acpi_found_madt;
39
40/*
41 * Various Linux-internal data structures created from the
42 * MP-table.
43 */
Andi Kleen55f05ff2006-09-26 10:52:30 +020044DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045int mp_bus_id_to_pci_bus [MAX_MP_BUSSES] = { [0 ... MAX_MP_BUSSES-1] = -1 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47static int mp_current_pci_id = 0;
48/* I/O APIC entries */
49struct mpc_config_ioapic mp_ioapics[MAX_IO_APICS];
50
51/* # of MP IRQ source entries */
52struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
53
54/* MP IRQ source entries */
55int mp_irq_entries;
56
57int nr_ioapics;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058unsigned long mp_lapic_addr = 0;
59
60
61
62/* Processor that is doing the boot up */
63unsigned int boot_cpu_id = -1U;
64/* Internal processor count */
Andi Kleen420f8f62005-11-05 17:25:54 +010065unsigned int num_processors __initdata = 0;
66
67unsigned disabled_cpus __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/* Bitmask of physically existing CPUs */
70physid_mask_t phys_cpu_present_map = PHYSID_MASK_NONE;
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072u8 bios_cpu_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
73
74
75/*
76 * Intel MP BIOS table parsing routines:
77 */
78
79/*
80 * Checksum an MP configuration block.
81 */
82
83static int __init mpf_checksum(unsigned char *mp, int len)
84{
85 int sum = 0;
86
87 while (len--)
88 sum += *mp++;
89
90 return sum & 0xFF;
91}
92
Ashok Raj51f62e12006-03-25 16:29:28 +010093static void __cpuinit MP_processor_info (struct mpc_config_processor *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Andi Kleen88931662005-11-05 17:25:54 +010095 int cpu;
Ashok Raj51f62e12006-03-25 16:29:28 +010096 cpumask_t tmp_map;
Andi Kleenf2c2cca2006-09-26 10:52:37 +020097 char *bootup_cpu = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andi Kleen420f8f62005-11-05 17:25:54 +010099 if (!(m->mpc_cpuflag & CPU_ENABLED)) {
100 disabled_cpus++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 return;
Andi Kleen420f8f62005-11-05 17:25:54 +0100102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (m->mpc_cpuflag & CPU_BOOTPROCESSOR) {
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200105 bootup_cpu = " (Bootup-CPU)";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 boot_cpu_id = m->mpc_apicid;
107 }
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200108
109 printk(KERN_INFO "Processor #%d%s\n", m->mpc_apicid, bootup_cpu);
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (num_processors >= NR_CPUS) {
112 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
113 " Processor ignored.\n", NR_CPUS);
114 return;
115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Ashok Raj51f62e12006-03-25 16:29:28 +0100117 num_processors++;
118 cpus_complement(tmp_map, cpu_present_map);
119 cpu = first_cpu(tmp_map);
120
Andi Kleen88931662005-11-05 17:25:54 +0100121#if MAX_APICS < 255
122 if ((int)m->mpc_apicid > MAX_APICS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 printk(KERN_ERR "Processor #%d INVALID. (Max ID: %d).\n",
124 m->mpc_apicid, MAX_APICS);
125 return;
126 }
Andi Kleen88931662005-11-05 17:25:54 +0100127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 physid_set(m->mpc_apicid, phys_cpu_present_map);
Andi Kleen18a2b642005-05-16 21:53:35 -0700129 if (m->mpc_cpuflag & CPU_BOOTPROCESSOR) {
130 /*
131 * bios_cpu_apicid is required to have processors listed
132 * in same order as logical cpu numbers. Hence the first
133 * entry is BSP, and so on.
134 */
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700135 cpu = 0;
Ashok Raj51f62e12006-03-25 16:29:28 +0100136 }
Andi Kleen61b1b2d2005-07-28 21:15:27 -0700137 bios_cpu_apicid[cpu] = m->mpc_apicid;
138 x86_cpu_to_apicid[cpu] = m->mpc_apicid;
139
140 cpu_set(cpu, cpu_possible_map);
141 cpu_set(cpu, cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
144static void __init MP_bus_info (struct mpc_config_bus *m)
145{
146 char str[7];
147
148 memcpy(str, m->mpc_bustype, 6);
149 str[6] = 0;
150 Dprintk("Bus #%d is %s\n", m->mpc_busid, str);
151
152 if (strncmp(str, "ISA", 3) == 0) {
Andi Kleen55f05ff2006-09-26 10:52:30 +0200153 set_bit(m->mpc_busid, mp_bus_not_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 } else if (strncmp(str, "PCI", 3) == 0) {
Andi Kleen55f05ff2006-09-26 10:52:30 +0200155 clear_bit(m->mpc_busid, mp_bus_not_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 mp_bus_id_to_pci_bus[m->mpc_busid] = mp_current_pci_id;
157 mp_current_pci_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 } else {
159 printk(KERN_ERR "Unknown bustype %s\n", str);
160 }
161}
162
163static void __init MP_ioapic_info (struct mpc_config_ioapic *m)
164{
165 if (!(m->mpc_flags & MPC_APIC_USABLE))
166 return;
167
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200168 printk("I/O APIC #%d at 0x%X.\n",
169 m->mpc_apicid, m->mpc_apicaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (nr_ioapics >= MAX_IO_APICS) {
171 printk(KERN_ERR "Max # of I/O APICs (%d) exceeded (found %d).\n",
172 MAX_IO_APICS, nr_ioapics);
173 panic("Recompile kernel with bigger MAX_IO_APICS!.\n");
174 }
175 if (!m->mpc_apicaddr) {
176 printk(KERN_ERR "WARNING: bogus zero I/O APIC address"
177 " found in MP table, skipping!\n");
178 return;
179 }
180 mp_ioapics[nr_ioapics] = *m;
181 nr_ioapics++;
182}
183
184static void __init MP_intsrc_info (struct mpc_config_intsrc *m)
185{
186 mp_irqs [mp_irq_entries] = *m;
187 Dprintk("Int: type %d, pol %d, trig %d, bus %d,"
188 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
189 m->mpc_irqtype, m->mpc_irqflag & 3,
190 (m->mpc_irqflag >> 2) & 3, m->mpc_srcbus,
191 m->mpc_srcbusirq, m->mpc_dstapic, m->mpc_dstirq);
James Cleverdon6004e1b2005-11-05 17:25:53 +0100192 if (++mp_irq_entries >= MAX_IRQ_SOURCES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 panic("Max # of irq sources exceeded!!\n");
194}
195
196static void __init MP_lintsrc_info (struct mpc_config_lintsrc *m)
197{
198 Dprintk("Lint: type %d, pol %d, trig %d, bus %d,"
199 " IRQ %02x, APIC ID %x, APIC LINT %02x\n",
200 m->mpc_irqtype, m->mpc_irqflag & 3,
201 (m->mpc_irqflag >> 2) &3, m->mpc_srcbusid,
202 m->mpc_srcbusirq, m->mpc_destapic, m->mpc_destapiclint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/*
206 * Read/parse the MPC
207 */
208
209static int __init smp_read_mpc(struct mp_config_table *mpc)
210{
211 char str[16];
212 int count=sizeof(*mpc);
213 unsigned char *mpt=((unsigned char *)mpc)+count;
214
215 if (memcmp(mpc->mpc_signature,MPC_SIGNATURE,4)) {
216 printk("SMP mptable: bad signature [%c%c%c%c]!\n",
217 mpc->mpc_signature[0],
218 mpc->mpc_signature[1],
219 mpc->mpc_signature[2],
220 mpc->mpc_signature[3]);
221 return 0;
222 }
223 if (mpf_checksum((unsigned char *)mpc,mpc->mpc_length)) {
224 printk("SMP mptable: checksum error!\n");
225 return 0;
226 }
227 if (mpc->mpc_spec!=0x01 && mpc->mpc_spec!=0x04) {
228 printk(KERN_ERR "SMP mptable: bad table version (%d)!!\n",
229 mpc->mpc_spec);
230 return 0;
231 }
232 if (!mpc->mpc_lapic) {
233 printk(KERN_ERR "SMP mptable: null local APIC address!\n");
234 return 0;
235 }
236 memcpy(str,mpc->mpc_oem,8);
237 str[8]=0;
238 printk(KERN_INFO "OEM ID: %s ",str);
239
240 memcpy(str,mpc->mpc_productid,12);
241 str[12]=0;
Tim Hockin9ff4ced2006-02-17 13:52:54 -0800242 printk("Product ID: %s ",str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Tim Hockin9ff4ced2006-02-17 13:52:54 -0800244 printk("APIC at: 0x%X\n",mpc->mpc_lapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* save the local APIC address, it might be non-default */
247 if (!acpi_lapic)
248 mp_lapic_addr = mpc->mpc_lapic;
249
250 /*
251 * Now process the configuration blocks.
252 */
253 while (count < mpc->mpc_length) {
254 switch(*mpt) {
255 case MP_PROCESSOR:
256 {
257 struct mpc_config_processor *m=
258 (struct mpc_config_processor *)mpt;
259 if (!acpi_lapic)
260 MP_processor_info(m);
261 mpt += sizeof(*m);
262 count += sizeof(*m);
263 break;
264 }
265 case MP_BUS:
266 {
267 struct mpc_config_bus *m=
268 (struct mpc_config_bus *)mpt;
269 MP_bus_info(m);
270 mpt += sizeof(*m);
271 count += sizeof(*m);
272 break;
273 }
274 case MP_IOAPIC:
275 {
276 struct mpc_config_ioapic *m=
277 (struct mpc_config_ioapic *)mpt;
278 MP_ioapic_info(m);
279 mpt+=sizeof(*m);
280 count+=sizeof(*m);
281 break;
282 }
283 case MP_INTSRC:
284 {
285 struct mpc_config_intsrc *m=
286 (struct mpc_config_intsrc *)mpt;
287
288 MP_intsrc_info(m);
289 mpt+=sizeof(*m);
290 count+=sizeof(*m);
291 break;
292 }
293 case MP_LINTSRC:
294 {
295 struct mpc_config_lintsrc *m=
296 (struct mpc_config_lintsrc *)mpt;
297 MP_lintsrc_info(m);
298 mpt+=sizeof(*m);
299 count+=sizeof(*m);
300 break;
301 }
302 }
303 }
304 clustered_apic_check();
305 if (!num_processors)
306 printk(KERN_ERR "SMP mptable: no processors registered!\n");
307 return num_processors;
308}
309
310static int __init ELCR_trigger(unsigned int irq)
311{
312 unsigned int port;
313
314 port = 0x4d0 + (irq >> 3);
315 return (inb(port) >> (irq & 7)) & 1;
316}
317
318static void __init construct_default_ioirq_mptable(int mpc_default_type)
319{
320 struct mpc_config_intsrc intsrc;
321 int i;
322 int ELCR_fallback = 0;
323
324 intsrc.mpc_type = MP_INTSRC;
325 intsrc.mpc_irqflag = 0; /* conforming */
326 intsrc.mpc_srcbus = 0;
327 intsrc.mpc_dstapic = mp_ioapics[0].mpc_apicid;
328
329 intsrc.mpc_irqtype = mp_INT;
330
331 /*
332 * If true, we have an ISA/PCI system with no IRQ entries
333 * in the MP table. To prevent the PCI interrupts from being set up
334 * incorrectly, we try to use the ELCR. The sanity check to see if
335 * there is good ELCR data is very simple - IRQ0, 1, 2 and 13 can
336 * never be level sensitive, so we simply see if the ELCR agrees.
337 * If it does, we assume it's valid.
338 */
339 if (mpc_default_type == 5) {
340 printk(KERN_INFO "ISA/PCI bus type with no IRQ information... falling back to ELCR\n");
341
342 if (ELCR_trigger(0) || ELCR_trigger(1) || ELCR_trigger(2) || ELCR_trigger(13))
343 printk(KERN_ERR "ELCR contains invalid data... not using ELCR\n");
344 else {
345 printk(KERN_INFO "Using ELCR to identify PCI interrupts\n");
346 ELCR_fallback = 1;
347 }
348 }
349
350 for (i = 0; i < 16; i++) {
351 switch (mpc_default_type) {
352 case 2:
353 if (i == 0 || i == 13)
354 continue; /* IRQ0 & IRQ13 not connected */
355 /* fall through */
356 default:
357 if (i == 2)
358 continue; /* IRQ2 is never connected */
359 }
360
361 if (ELCR_fallback) {
362 /*
363 * If the ELCR indicates a level-sensitive interrupt, we
364 * copy that information over to the MP table in the
365 * irqflag field (level sensitive, active high polarity).
366 */
367 if (ELCR_trigger(i))
368 intsrc.mpc_irqflag = 13;
369 else
370 intsrc.mpc_irqflag = 0;
371 }
372
373 intsrc.mpc_srcbusirq = i;
374 intsrc.mpc_dstirq = i ? i : 2; /* IRQ0 to INTIN2 */
375 MP_intsrc_info(&intsrc);
376 }
377
378 intsrc.mpc_irqtype = mp_ExtINT;
379 intsrc.mpc_srcbusirq = 0;
380 intsrc.mpc_dstirq = 0; /* 8259A to INTIN0 */
381 MP_intsrc_info(&intsrc);
382}
383
384static inline void __init construct_default_ISA_mptable(int mpc_default_type)
385{
386 struct mpc_config_processor processor;
387 struct mpc_config_bus bus;
388 struct mpc_config_ioapic ioapic;
389 struct mpc_config_lintsrc lintsrc;
390 int linttypes[2] = { mp_ExtINT, mp_NMI };
391 int i;
392
393 /*
394 * local APIC has default address
395 */
396 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
397
398 /*
399 * 2 CPUs, numbered 0 & 1.
400 */
401 processor.mpc_type = MP_PROCESSOR;
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200402 processor.mpc_apicver = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 processor.mpc_cpuflag = CPU_ENABLED;
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200404 processor.mpc_cpufeature = 0;
405 processor.mpc_featureflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 processor.mpc_reserved[0] = 0;
407 processor.mpc_reserved[1] = 0;
408 for (i = 0; i < 2; i++) {
409 processor.mpc_apicid = i;
410 MP_processor_info(&processor);
411 }
412
413 bus.mpc_type = MP_BUS;
414 bus.mpc_busid = 0;
415 switch (mpc_default_type) {
416 default:
417 printk(KERN_ERR "???\nUnknown standard configuration %d\n",
418 mpc_default_type);
419 /* fall through */
420 case 1:
421 case 5:
422 memcpy(bus.mpc_bustype, "ISA ", 6);
423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425 MP_bus_info(&bus);
426 if (mpc_default_type > 4) {
427 bus.mpc_busid = 1;
428 memcpy(bus.mpc_bustype, "PCI ", 6);
429 MP_bus_info(&bus);
430 }
431
432 ioapic.mpc_type = MP_IOAPIC;
433 ioapic.mpc_apicid = 2;
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200434 ioapic.mpc_apicver = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 ioapic.mpc_flags = MPC_APIC_USABLE;
436 ioapic.mpc_apicaddr = 0xFEC00000;
437 MP_ioapic_info(&ioapic);
438
439 /*
440 * We set up most of the low 16 IO-APIC pins according to MPS rules.
441 */
442 construct_default_ioirq_mptable(mpc_default_type);
443
444 lintsrc.mpc_type = MP_LINTSRC;
445 lintsrc.mpc_irqflag = 0; /* conforming */
446 lintsrc.mpc_srcbusid = 0;
447 lintsrc.mpc_srcbusirq = 0;
448 lintsrc.mpc_destapic = MP_APIC_ALL;
449 for (i = 0; i < 2; i++) {
450 lintsrc.mpc_irqtype = linttypes[i];
451 lintsrc.mpc_destapiclint = i;
452 MP_lintsrc_info(&lintsrc);
453 }
454}
455
456static struct intel_mp_floating *mpf_found;
457
458/*
459 * Scan the memory blocks for an SMP configuration block.
460 */
461void __init get_smp_config (void)
462{
463 struct intel_mp_floating *mpf = mpf_found;
464
465 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 * ACPI supports both logical (e.g. Hyper-Threading) and physical
467 * processors, where MPS only supports physical.
468 */
469 if (acpi_lapic && acpi_ioapic) {
470 printk(KERN_INFO "Using ACPI (MADT) for SMP configuration information\n");
471 return;
472 }
473 else if (acpi_lapic)
474 printk(KERN_INFO "Using ACPI for processor (LAPIC) configuration information\n");
475
476 printk("Intel MultiProcessor Specification v1.%d\n", mpf->mpf_specification);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 /*
479 * Now see if we need to read further.
480 */
481 if (mpf->mpf_feature1 != 0) {
482
483 printk(KERN_INFO "Default MP configuration #%d\n", mpf->mpf_feature1);
484 construct_default_ISA_mptable(mpf->mpf_feature1);
485
486 } else if (mpf->mpf_physptr) {
487
488 /*
489 * Read the physical hardware table. Anything here will
490 * override the defaults.
491 */
Siddha, Suresh Bf6c2e332005-11-05 17:25:53 +0100492 if (!smp_read_mpc(phys_to_virt(mpf->mpf_physptr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 smp_found_config = 0;
494 printk(KERN_ERR "BIOS bug, MP table errors detected!...\n");
495 printk(KERN_ERR "... disabling SMP support. (tell your hw vendor)\n");
496 return;
497 }
498 /*
499 * If there are no explicit MP IRQ entries, then we are
500 * broken. We set up most of the low 16 IO-APIC pins to
501 * ISA defaults and hope it will work.
502 */
503 if (!mp_irq_entries) {
504 struct mpc_config_bus bus;
505
506 printk(KERN_ERR "BIOS bug, no explicit IRQ entries, using default mptable. (tell your hw vendor)\n");
507
508 bus.mpc_type = MP_BUS;
509 bus.mpc_busid = 0;
510 memcpy(bus.mpc_bustype, "ISA ", 6);
511 MP_bus_info(&bus);
512
513 construct_default_ioirq_mptable(0);
514 }
515
516 } else
517 BUG();
518
519 printk(KERN_INFO "Processors: %d\n", num_processors);
520 /*
521 * Only use the first configuration found.
522 */
523}
524
525static int __init smp_scan_config (unsigned long base, unsigned long length)
526{
527 extern void __bad_mpf_size(void);
528 unsigned int *bp = phys_to_virt(base);
529 struct intel_mp_floating *mpf;
530
531 Dprintk("Scan SMP from %p for %ld bytes.\n", bp,length);
532 if (sizeof(*mpf) != 16)
533 __bad_mpf_size();
534
535 while (length > 0) {
536 mpf = (struct intel_mp_floating *)bp;
537 if ((*bp == SMP_MAGIC_IDENT) &&
538 (mpf->mpf_length == 1) &&
539 !mpf_checksum((unsigned char *)bp, 16) &&
540 ((mpf->mpf_specification == 1)
541 || (mpf->mpf_specification == 4)) ) {
542
543 smp_found_config = 1;
544 reserve_bootmem_generic(virt_to_phys(mpf), PAGE_SIZE);
545 if (mpf->mpf_physptr)
546 reserve_bootmem_generic(mpf->mpf_physptr, PAGE_SIZE);
547 mpf_found = mpf;
548 return 1;
549 }
550 bp += 4;
551 length -= 16;
552 }
553 return 0;
554}
555
Andi Kleena01fd3b2006-09-26 10:52:30 +0200556void __init find_smp_config(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 unsigned int address;
559
560 /*
561 * FIXME: Linux assumes you have 640K of base ram..
562 * this continues the error...
563 *
564 * 1) Scan the bottom 1K for a signature
565 * 2) Scan the top 1K of base RAM
566 * 3) Scan the 64K of bios
567 */
568 if (smp_scan_config(0x0,0x400) ||
569 smp_scan_config(639*0x400,0x400) ||
570 smp_scan_config(0xF0000,0x10000))
571 return;
572 /*
Andi Kleene5099132006-09-26 10:52:29 +0200573 * If it is an SMP machine we should know now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 *
575 * there is a real-mode segmented pointer pointing to the
576 * 4K EBDA area at 0x40E, calculate and scan it here.
577 *
578 * NOTE! There are Linux loaders that will corrupt the EBDA
579 * area, and as such this kind of SMP config may be less
580 * trustworthy, simply because the SMP table may have been
581 * stomped on during early boot. These loaders are buggy and
582 * should be fixed.
583 */
584
585 address = *(unsigned short *)phys_to_virt(0x40E);
586 address <<= 4;
587 if (smp_scan_config(address, 0x1000))
588 return;
589
590 /* If we have come this far, we did not find an MP table */
591 printk(KERN_INFO "No mptable found.\n");
592}
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/* --------------------------------------------------------------------------
595 ACPI-based MP Configuration
596 -------------------------------------------------------------------------- */
597
Len Brown888ba6c2005-08-24 12:07:20 -0400598#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Andi Kleenefec3b92006-09-26 10:52:30 +0200600void __init mp_register_lapic_address(u64 address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 mp_lapic_addr = (unsigned long) address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (boot_cpu_id == -1U)
605 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
606
607 Dprintk("Boot CPU = %d\n", boot_cpu_physical_apicid);
608}
609
Andi Kleenefec3b92006-09-26 10:52:30 +0200610void __cpuinit mp_register_lapic (u8 id, u8 enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct mpc_config_processor processor;
613 int boot_cpu = 0;
614
615 if (id >= MAX_APICS) {
616 printk(KERN_WARNING "Processor #%d invalid (max %d)\n",
617 id, MAX_APICS);
618 return;
619 }
620
621 if (id == boot_cpu_physical_apicid)
622 boot_cpu = 1;
623
624 processor.mpc_type = MP_PROCESSOR;
625 processor.mpc_apicid = id;
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200626 processor.mpc_apicver = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 processor.mpc_cpuflag = (enabled ? CPU_ENABLED : 0);
628 processor.mpc_cpuflag |= (boot_cpu ? CPU_BOOTPROCESSOR : 0);
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200629 processor.mpc_cpufeature = 0;
630 processor.mpc_featureflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 processor.mpc_reserved[0] = 0;
632 processor.mpc_reserved[1] = 0;
633
634 MP_processor_info(&processor);
635}
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637#define MP_ISA_BUS 0
638#define MP_MAX_IOAPIC_PIN 127
639
640static struct mp_ioapic_routing {
641 int apic_id;
642 int gsi_start;
643 int gsi_end;
644 u32 pin_programmed[4];
645} mp_ioapic_routing[MAX_IO_APICS];
646
Andi Kleenefec3b92006-09-26 10:52:30 +0200647static int mp_find_ioapic(int gsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Andi Kleenefec3b92006-09-26 10:52:30 +0200649 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 /* Find the IOAPIC that manages this GSI. */
652 for (i = 0; i < nr_ioapics; i++) {
653 if ((gsi >= mp_ioapic_routing[i].gsi_start)
654 && (gsi <= mp_ioapic_routing[i].gsi_end))
655 return i;
656 }
657
658 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -1;
660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Andi Kleenefec3b92006-09-26 10:52:30 +0200662void __init mp_register_ioapic(u8 id, u32 address, u32 gsi_base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Andi Kleenefec3b92006-09-26 10:52:30 +0200664 int idx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (nr_ioapics >= MAX_IO_APICS) {
667 printk(KERN_ERR "ERROR: Max # of I/O APICs (%d) exceeded "
668 "(found %d)\n", MAX_IO_APICS, nr_ioapics);
669 panic("Recompile kernel with bigger MAX_IO_APICS!\n");
670 }
671 if (!address) {
672 printk(KERN_ERR "WARNING: Bogus (zero) I/O APIC address"
673 " found in MADT table, skipping!\n");
674 return;
675 }
676
677 idx = nr_ioapics++;
678
679 mp_ioapics[idx].mpc_type = MP_IOAPIC;
680 mp_ioapics[idx].mpc_flags = MPC_APIC_USABLE;
681 mp_ioapics[idx].mpc_apicaddr = address;
682
683 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
Andi Kleen0af2be02005-05-16 21:53:27 -0700684 mp_ioapics[idx].mpc_apicid = id;
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200685 mp_ioapics[idx].mpc_apicver = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /*
688 * Build basic IRQ lookup table to facilitate gsi->io_apic lookups
689 * and to prevent reprogramming of IOAPIC pins (PCI IRQs).
690 */
691 mp_ioapic_routing[idx].apic_id = mp_ioapics[idx].mpc_apicid;
692 mp_ioapic_routing[idx].gsi_start = gsi_base;
693 mp_ioapic_routing[idx].gsi_end = gsi_base +
694 io_apic_get_redir_entries(idx);
695
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200696 printk(KERN_INFO "IOAPIC[%d]: apic_id %d, address 0x%x, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 "GSI %d-%d\n", idx, mp_ioapics[idx].mpc_apicid,
Andi Kleenf2c2cca2006-09-26 10:52:37 +0200698 mp_ioapics[idx].mpc_apicaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 mp_ioapic_routing[idx].gsi_start,
700 mp_ioapic_routing[idx].gsi_end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Andi Kleenefec3b92006-09-26 10:52:30 +0200703void __init
704mp_override_legacy_irq(u8 bus_irq, u8 polarity, u8 trigger, u32 gsi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 struct mpc_config_intsrc intsrc;
707 int ioapic = -1;
708 int pin = -1;
709
710 /*
711 * Convert 'gsi' to 'ioapic.pin'.
712 */
713 ioapic = mp_find_ioapic(gsi);
714 if (ioapic < 0)
715 return;
716 pin = gsi - mp_ioapic_routing[ioapic].gsi_start;
717
718 /*
719 * TBD: This check is for faulty timer entries, where the override
720 * erroneously sets the trigger to level, resulting in a HUGE
721 * increase of timer interrupts!
722 */
723 if ((bus_irq == 0) && (trigger == 3))
724 trigger = 1;
725
726 intsrc.mpc_type = MP_INTSRC;
727 intsrc.mpc_irqtype = mp_INT;
728 intsrc.mpc_irqflag = (trigger << 2) | polarity;
729 intsrc.mpc_srcbus = MP_ISA_BUS;
730 intsrc.mpc_srcbusirq = bus_irq; /* IRQ */
731 intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid; /* APIC ID */
732 intsrc.mpc_dstirq = pin; /* INTIN# */
733
734 Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, %d-%d\n",
735 intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3,
736 (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus,
737 intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, intsrc.mpc_dstirq);
738
739 mp_irqs[mp_irq_entries] = intsrc;
740 if (++mp_irq_entries == MAX_IRQ_SOURCES)
741 panic("Max # of irq sources exceeded!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Andi Kleenefec3b92006-09-26 10:52:30 +0200744void __init mp_config_acpi_legacy_irqs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
746 struct mpc_config_intsrc intsrc;
Andi Kleenefec3b92006-09-26 10:52:30 +0200747 int i = 0;
748 int ioapic = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /*
751 * Fabricate the legacy ISA bus (bus #31).
752 */
Andi Kleen55f05ff2006-09-26 10:52:30 +0200753 set_bit(MP_ISA_BUS, mp_bus_not_pci);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /*
756 * Locate the IOAPIC that manages the ISA IRQs (0-15).
757 */
758 ioapic = mp_find_ioapic(0);
759 if (ioapic < 0)
760 return;
761
762 intsrc.mpc_type = MP_INTSRC;
763 intsrc.mpc_irqflag = 0; /* Conforming */
764 intsrc.mpc_srcbus = MP_ISA_BUS;
765 intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;
766
767 /*
768 * Use the default configuration for the IRQs 0-15. Unless
769 * overridden by (MADT) interrupt source override entries.
770 */
771 for (i = 0; i < 16; i++) {
772 int idx;
773
774 for (idx = 0; idx < mp_irq_entries; idx++) {
775 struct mpc_config_intsrc *irq = mp_irqs + idx;
776
777 /* Do we already have a mapping for this ISA IRQ? */
778 if (irq->mpc_srcbus == MP_ISA_BUS && irq->mpc_srcbusirq == i)
779 break;
780
781 /* Do we already have a mapping for this IOAPIC pin */
782 if ((irq->mpc_dstapic == intsrc.mpc_dstapic) &&
783 (irq->mpc_dstirq == i))
784 break;
785 }
786
787 if (idx != mp_irq_entries) {
788 printk(KERN_DEBUG "ACPI: IRQ%d used by override.\n", i);
789 continue; /* IRQ already used */
790 }
791
792 intsrc.mpc_irqtype = mp_INT;
793 intsrc.mpc_srcbusirq = i; /* Identity mapped */
794 intsrc.mpc_dstirq = i;
795
796 Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, "
797 "%d-%d\n", intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3,
798 (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus,
799 intsrc.mpc_srcbusirq, intsrc.mpc_dstapic,
800 intsrc.mpc_dstirq);
801
802 mp_irqs[mp_irq_entries] = intsrc;
803 if (++mp_irq_entries == MAX_IRQ_SOURCES)
804 panic("Max # of irq sources exceeded!\n");
805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Natalie Protasevich701067c2005-06-23 00:08:41 -0700808#define MAX_GSI_NUM 4096
809
Bob Moore50eca3e2005-09-30 19:03:00 -0400810int mp_register_gsi(u32 gsi, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Andi Kleenefec3b92006-09-26 10:52:30 +0200812 int ioapic = -1;
813 int ioapic_pin = 0;
814 int idx, bit = 0;
815 static int pci_irq = 16;
Natalie Protasevich701067c2005-06-23 00:08:41 -0700816 /*
817 * Mapping between Global System Interrupts, which
818 * represent all possible interrupts, to the IRQs
819 * assigned to actual devices.
820 */
Andi Kleenefec3b92006-09-26 10:52:30 +0200821 static int gsi_to_irq[MAX_GSI_NUM];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 if (acpi_irq_model != ACPI_IRQ_MODEL_IOAPIC)
824 return gsi;
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* Don't set up the ACPI SCI because it's already set up */
827 if (acpi_fadt.sci_int == gsi)
828 return gsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 ioapic = mp_find_ioapic(gsi);
831 if (ioapic < 0) {
832 printk(KERN_WARNING "No IOAPIC for GSI %u\n", gsi);
833 return gsi;
834 }
835
836 ioapic_pin = gsi - mp_ioapic_routing[ioapic].gsi_start;
837
838 /*
839 * Avoid pin reprogramming. PRTs typically include entries
840 * with redundant pin->gsi mappings (but unique PCI devices);
841 * we only program the IOAPIC on the first.
842 */
843 bit = ioapic_pin % 32;
844 idx = (ioapic_pin < 32) ? 0 : (ioapic_pin / 32);
845 if (idx > 3) {
846 printk(KERN_ERR "Invalid reference to IOAPIC pin "
847 "%d-%d\n", mp_ioapic_routing[ioapic].apic_id,
848 ioapic_pin);
849 return gsi;
850 }
851 if ((1<<bit) & mp_ioapic_routing[ioapic].pin_programmed[idx]) {
852 Dprintk(KERN_DEBUG "Pin %d-%d already programmed\n",
853 mp_ioapic_routing[ioapic].apic_id, ioapic_pin);
Natalie Protasevich701067c2005-06-23 00:08:41 -0700854 return gsi_to_irq[gsi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 mp_ioapic_routing[ioapic].pin_programmed[idx] |= (1<<bit);
858
Len Browncb654692005-12-28 02:43:51 -0500859 if (triggering == ACPI_LEVEL_SENSITIVE) {
Natalie Protasevich701067c2005-06-23 00:08:41 -0700860 /*
861 * For PCI devices assign IRQs in order, avoiding gaps
862 * due to unused I/O APIC pins.
863 */
864 int irq = gsi;
Natalie.Protasevich@unisys.com6a1caa22005-07-30 11:25:32 -0700865 if (gsi < MAX_GSI_NUM) {
Kimball Murraye0c1e9b2006-05-08 15:17:16 +0200866 /*
867 * Retain the VIA chipset work-around (gsi > 15), but
868 * avoid a problem where the 8254 timer (IRQ0) is setup
869 * via an override (so it's not on pin 0 of the ioapic),
870 * and at the same time, the pin 0 interrupt is a PCI
871 * type. The gsi > 15 test could cause these two pins
872 * to be shared as IRQ0, and they are not shareable.
873 * So test for this condition, and if necessary, avoid
874 * the pin collision.
875 */
876 if (gsi > 15 || (gsi == 0 && !timer_uses_ioapic_pin_0))
Natalie.Protasevich@unisys.com6a1caa22005-07-30 11:25:32 -0700877 gsi = pci_irq++;
Natalie.Protasevich@unisys.com6a1caa22005-07-30 11:25:32 -0700878 /*
879 * Don't assign IRQ used by ACPI SCI
880 */
881 if (gsi == acpi_fadt.sci_int)
882 gsi = pci_irq++;
Natalie.Protasevich@unisys.com6a1caa22005-07-30 11:25:32 -0700883 gsi_to_irq[irq] = gsi;
884 } else {
885 printk(KERN_ERR "GSI %u is too high\n", gsi);
886 return gsi;
887 }
Natalie Protasevich701067c2005-06-23 00:08:41 -0700888 }
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 io_apic_set_pci_routing(ioapic, ioapic_pin, gsi,
Bob Moore50eca3e2005-09-30 19:03:00 -0400891 triggering == ACPI_EDGE_SENSITIVE ? 0 : 1,
892 polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return gsi;
894}
Len Brown888ba6c2005-08-24 12:07:20 -0400895#endif /*CONFIG_ACPI*/