]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86_64/kernel/mpparse.c
[PATCH] x86-64: Use ACPI PXM to parse PCI<->node assignments
[linux-2.6-omap-h63xx.git] / arch / x86_64 / kernel / mpparse.c
1 /*
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>
17 #include <linux/irq.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <linux/config.h>
21 #include <linux/bootmem.h>
22 #include <linux/smp_lock.h>
23 #include <linux/kernel_stat.h>
24 #include <linux/mc146818rtc.h>
25 #include <linux/acpi.h>
26 #include <linux/module.h>
27
28 #include <asm/smp.h>
29 #include <asm/mtrr.h>
30 #include <asm/mpspec.h>
31 #include <asm/pgalloc.h>
32 #include <asm/io_apic.h>
33 #include <asm/proto.h>
34 #include <asm/acpi.h>
35
36 /* Have we found an MP table */
37 int smp_found_config;
38 unsigned int __initdata maxcpus = NR_CPUS;
39
40 int acpi_found_madt;
41
42 /*
43  * Various Linux-internal data structures created from the
44  * MP-table.
45  */
46 int apic_version [MAX_APICS];
47 unsigned char mp_bus_id_to_type [MAX_MP_BUSSES] = { [0 ... MAX_MP_BUSSES-1] = -1 };
48 int mp_bus_id_to_pci_bus [MAX_MP_BUSSES] = { [0 ... MAX_MP_BUSSES-1] = -1 };
49
50 static int mp_current_pci_id = 0;
51 /* I/O APIC entries */
52 struct mpc_config_ioapic mp_ioapics[MAX_IO_APICS];
53
54 /* # of MP IRQ source entries */
55 struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
56
57 /* MP IRQ source entries */
58 int mp_irq_entries;
59
60 int nr_ioapics;
61 int pic_mode;
62 unsigned long mp_lapic_addr = 0;
63
64
65
66 /* Processor that is doing the boot up */
67 unsigned int boot_cpu_id = -1U;
68 /* Internal processor count */
69 static unsigned int num_processors = 0;
70
71 /* Bitmask of physically existing CPUs */
72 physid_mask_t phys_cpu_present_map = PHYSID_MASK_NONE;
73
74 /* ACPI MADT entry parsing functions */
75 #ifdef CONFIG_ACPI
76 extern struct acpi_boot_flags acpi_boot;
77 #ifdef CONFIG_X86_LOCAL_APIC
78 extern int acpi_parse_lapic (acpi_table_entry_header *header);
79 extern int acpi_parse_lapic_addr_ovr (acpi_table_entry_header *header);
80 extern int acpi_parse_lapic_nmi (acpi_table_entry_header *header);
81 #endif /*CONFIG_X86_LOCAL_APIC*/
82 #ifdef CONFIG_X86_IO_APIC
83 extern int acpi_parse_ioapic (acpi_table_entry_header *header);
84 #endif /*CONFIG_X86_IO_APIC*/
85 #endif /*CONFIG_ACPI*/
86
87 u8 bios_cpu_apicid[NR_CPUS] = { [0 ... NR_CPUS-1] = BAD_APICID };
88
89
90 /*
91  * Intel MP BIOS table parsing routines:
92  */
93
94 /*
95  * Checksum an MP configuration block.
96  */
97
98 static int __init mpf_checksum(unsigned char *mp, int len)
99 {
100         int sum = 0;
101
102         while (len--)
103                 sum += *mp++;
104
105         return sum & 0xFF;
106 }
107
108 static void __init MP_processor_info (struct mpc_config_processor *m)
109 {
110         int ver, cpu;
111         static int found_bsp=0;
112
113         if (!(m->mpc_cpuflag & CPU_ENABLED))
114                 return;
115
116         printk(KERN_INFO "Processor #%d %d:%d APIC version %d\n",
117                 m->mpc_apicid,
118                (m->mpc_cpufeature & CPU_FAMILY_MASK)>>8,
119                (m->mpc_cpufeature & CPU_MODEL_MASK)>>4,
120                 m->mpc_apicver);
121
122         if (m->mpc_cpuflag & CPU_BOOTPROCESSOR) {
123                 Dprintk("    Bootup CPU\n");
124                 boot_cpu_id = m->mpc_apicid;
125         }
126         if (num_processors >= NR_CPUS) {
127                 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
128                         " Processor ignored.\n", NR_CPUS);
129                 return;
130         }
131
132         cpu = num_processors++;
133
134         if (m->mpc_apicid > MAX_APICS) {
135                 printk(KERN_ERR "Processor #%d INVALID. (Max ID: %d).\n",
136                         m->mpc_apicid, MAX_APICS);
137                 return;
138         }
139         ver = m->mpc_apicver;
140
141         physid_set(m->mpc_apicid, phys_cpu_present_map);
142         /*
143          * Validate version
144          */
145         if (ver == 0x0) {
146                 printk(KERN_ERR "BIOS bug, APIC version is 0 for CPU#%d! fixing up to 0x10. (tell your hw vendor)\n", m->mpc_apicid);
147                 ver = 0x10;
148         }
149         apic_version[m->mpc_apicid] = ver;
150         if (m->mpc_cpuflag & CPU_BOOTPROCESSOR) {
151                 /*
152                  * bios_cpu_apicid is required to have processors listed
153                  * in same order as logical cpu numbers. Hence the first
154                  * entry is BSP, and so on.
155                  */
156                 cpu = 0;
157
158                 bios_cpu_apicid[0] = m->mpc_apicid;
159                 x86_cpu_to_apicid[0] = m->mpc_apicid;
160                 found_bsp = 1;
161         } else
162                 cpu = num_processors - found_bsp;
163         bios_cpu_apicid[cpu] = m->mpc_apicid;
164         x86_cpu_to_apicid[cpu] = m->mpc_apicid;
165
166         cpu_set(cpu, cpu_possible_map);
167         cpu_set(cpu, cpu_present_map);
168 }
169
170 static void __init MP_bus_info (struct mpc_config_bus *m)
171 {
172         char str[7];
173
174         memcpy(str, m->mpc_bustype, 6);
175         str[6] = 0;
176         Dprintk("Bus #%d is %s\n", m->mpc_busid, str);
177
178         if (strncmp(str, "ISA", 3) == 0) {
179                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_ISA;
180         } else if (strncmp(str, "EISA", 4) == 0) {
181                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_EISA;
182         } else if (strncmp(str, "PCI", 3) == 0) {
183                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_PCI;
184                 mp_bus_id_to_pci_bus[m->mpc_busid] = mp_current_pci_id;
185                 mp_current_pci_id++;
186         } else if (strncmp(str, "MCA", 3) == 0) {
187                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_MCA;
188         } else {
189                 printk(KERN_ERR "Unknown bustype %s\n", str);
190         }
191 }
192
193 static void __init MP_ioapic_info (struct mpc_config_ioapic *m)
194 {
195         if (!(m->mpc_flags & MPC_APIC_USABLE))
196                 return;
197
198         printk("I/O APIC #%d Version %d at 0x%X.\n",
199                 m->mpc_apicid, m->mpc_apicver, m->mpc_apicaddr);
200         if (nr_ioapics >= MAX_IO_APICS) {
201                 printk(KERN_ERR "Max # of I/O APICs (%d) exceeded (found %d).\n",
202                         MAX_IO_APICS, nr_ioapics);
203                 panic("Recompile kernel with bigger MAX_IO_APICS!.\n");
204         }
205         if (!m->mpc_apicaddr) {
206                 printk(KERN_ERR "WARNING: bogus zero I/O APIC address"
207                         " found in MP table, skipping!\n");
208                 return;
209         }
210         mp_ioapics[nr_ioapics] = *m;
211         nr_ioapics++;
212 }
213
214 static void __init MP_intsrc_info (struct mpc_config_intsrc *m)
215 {
216         mp_irqs [mp_irq_entries] = *m;
217         Dprintk("Int: type %d, pol %d, trig %d, bus %d,"
218                 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
219                         m->mpc_irqtype, m->mpc_irqflag & 3,
220                         (m->mpc_irqflag >> 2) & 3, m->mpc_srcbus,
221                         m->mpc_srcbusirq, m->mpc_dstapic, m->mpc_dstirq);
222         if (++mp_irq_entries == MAX_IRQ_SOURCES)
223                 panic("Max # of irq sources exceeded!!\n");
224 }
225
226 static void __init MP_lintsrc_info (struct mpc_config_lintsrc *m)
227 {
228         Dprintk("Lint: type %d, pol %d, trig %d, bus %d,"
229                 " IRQ %02x, APIC ID %x, APIC LINT %02x\n",
230                         m->mpc_irqtype, m->mpc_irqflag & 3,
231                         (m->mpc_irqflag >> 2) &3, m->mpc_srcbusid,
232                         m->mpc_srcbusirq, m->mpc_destapic, m->mpc_destapiclint);
233         /*
234          * Well it seems all SMP boards in existence
235          * use ExtINT/LVT1 == LINT0 and
236          * NMI/LVT2 == LINT1 - the following check
237          * will show us if this assumptions is false.
238          * Until then we do not have to add baggage.
239          */
240         if ((m->mpc_irqtype == mp_ExtINT) &&
241                 (m->mpc_destapiclint != 0))
242                         BUG();
243         if ((m->mpc_irqtype == mp_NMI) &&
244                 (m->mpc_destapiclint != 1))
245                         BUG();
246 }
247
248 /*
249  * Read/parse the MPC
250  */
251
252 static int __init smp_read_mpc(struct mp_config_table *mpc)
253 {
254         char str[16];
255         int count=sizeof(*mpc);
256         unsigned char *mpt=((unsigned char *)mpc)+count;
257
258         if (memcmp(mpc->mpc_signature,MPC_SIGNATURE,4)) {
259                 printk("SMP mptable: bad signature [%c%c%c%c]!\n",
260                         mpc->mpc_signature[0],
261                         mpc->mpc_signature[1],
262                         mpc->mpc_signature[2],
263                         mpc->mpc_signature[3]);
264                 return 0;
265         }
266         if (mpf_checksum((unsigned char *)mpc,mpc->mpc_length)) {
267                 printk("SMP mptable: checksum error!\n");
268                 return 0;
269         }
270         if (mpc->mpc_spec!=0x01 && mpc->mpc_spec!=0x04) {
271                 printk(KERN_ERR "SMP mptable: bad table version (%d)!!\n",
272                         mpc->mpc_spec);
273                 return 0;
274         }
275         if (!mpc->mpc_lapic) {
276                 printk(KERN_ERR "SMP mptable: null local APIC address!\n");
277                 return 0;
278         }
279         memcpy(str,mpc->mpc_oem,8);
280         str[8]=0;
281         printk(KERN_INFO "OEM ID: %s ",str);
282
283         memcpy(str,mpc->mpc_productid,12);
284         str[12]=0;
285         printk(KERN_INFO "Product ID: %s ",str);
286
287         printk(KERN_INFO "APIC at: 0x%X\n",mpc->mpc_lapic);
288
289         /* save the local APIC address, it might be non-default */
290         if (!acpi_lapic)
291         mp_lapic_addr = mpc->mpc_lapic;
292
293         /*
294          *      Now process the configuration blocks.
295          */
296         while (count < mpc->mpc_length) {
297                 switch(*mpt) {
298                         case MP_PROCESSOR:
299                         {
300                                 struct mpc_config_processor *m=
301                                         (struct mpc_config_processor *)mpt;
302                                 if (!acpi_lapic)
303                                 MP_processor_info(m);
304                                 mpt += sizeof(*m);
305                                 count += sizeof(*m);
306                                 break;
307                         }
308                         case MP_BUS:
309                         {
310                                 struct mpc_config_bus *m=
311                                         (struct mpc_config_bus *)mpt;
312                                 MP_bus_info(m);
313                                 mpt += sizeof(*m);
314                                 count += sizeof(*m);
315                                 break;
316                         }
317                         case MP_IOAPIC:
318                         {
319                                 struct mpc_config_ioapic *m=
320                                         (struct mpc_config_ioapic *)mpt;
321                                 MP_ioapic_info(m);
322                                 mpt+=sizeof(*m);
323                                 count+=sizeof(*m);
324                                 break;
325                         }
326                         case MP_INTSRC:
327                         {
328                                 struct mpc_config_intsrc *m=
329                                         (struct mpc_config_intsrc *)mpt;
330
331                                 MP_intsrc_info(m);
332                                 mpt+=sizeof(*m);
333                                 count+=sizeof(*m);
334                                 break;
335                         }
336                         case MP_LINTSRC:
337                         {
338                                 struct mpc_config_lintsrc *m=
339                                         (struct mpc_config_lintsrc *)mpt;
340                                 MP_lintsrc_info(m);
341                                 mpt+=sizeof(*m);
342                                 count+=sizeof(*m);
343                                 break;
344                         }
345                 }
346         }
347         clustered_apic_check();
348         if (!num_processors)
349                 printk(KERN_ERR "SMP mptable: no processors registered!\n");
350         return num_processors;
351 }
352
353 static int __init ELCR_trigger(unsigned int irq)
354 {
355         unsigned int port;
356
357         port = 0x4d0 + (irq >> 3);
358         return (inb(port) >> (irq & 7)) & 1;
359 }
360
361 static void __init construct_default_ioirq_mptable(int mpc_default_type)
362 {
363         struct mpc_config_intsrc intsrc;
364         int i;
365         int ELCR_fallback = 0;
366
367         intsrc.mpc_type = MP_INTSRC;
368         intsrc.mpc_irqflag = 0;                 /* conforming */
369         intsrc.mpc_srcbus = 0;
370         intsrc.mpc_dstapic = mp_ioapics[0].mpc_apicid;
371
372         intsrc.mpc_irqtype = mp_INT;
373
374         /*
375          *  If true, we have an ISA/PCI system with no IRQ entries
376          *  in the MP table. To prevent the PCI interrupts from being set up
377          *  incorrectly, we try to use the ELCR. The sanity check to see if
378          *  there is good ELCR data is very simple - IRQ0, 1, 2 and 13 can
379          *  never be level sensitive, so we simply see if the ELCR agrees.
380          *  If it does, we assume it's valid.
381          */
382         if (mpc_default_type == 5) {
383                 printk(KERN_INFO "ISA/PCI bus type with no IRQ information... falling back to ELCR\n");
384
385                 if (ELCR_trigger(0) || ELCR_trigger(1) || ELCR_trigger(2) || ELCR_trigger(13))
386                         printk(KERN_ERR "ELCR contains invalid data... not using ELCR\n");
387                 else {
388                         printk(KERN_INFO "Using ELCR to identify PCI interrupts\n");
389                         ELCR_fallback = 1;
390                 }
391         }
392
393         for (i = 0; i < 16; i++) {
394                 switch (mpc_default_type) {
395                 case 2:
396                         if (i == 0 || i == 13)
397                                 continue;       /* IRQ0 & IRQ13 not connected */
398                         /* fall through */
399                 default:
400                         if (i == 2)
401                                 continue;       /* IRQ2 is never connected */
402                 }
403
404                 if (ELCR_fallback) {
405                         /*
406                          *  If the ELCR indicates a level-sensitive interrupt, we
407                          *  copy that information over to the MP table in the
408                          *  irqflag field (level sensitive, active high polarity).
409                          */
410                         if (ELCR_trigger(i))
411                                 intsrc.mpc_irqflag = 13;
412                         else
413                                 intsrc.mpc_irqflag = 0;
414                 }
415
416                 intsrc.mpc_srcbusirq = i;
417                 intsrc.mpc_dstirq = i ? i : 2;          /* IRQ0 to INTIN2 */
418                 MP_intsrc_info(&intsrc);
419         }
420
421         intsrc.mpc_irqtype = mp_ExtINT;
422         intsrc.mpc_srcbusirq = 0;
423         intsrc.mpc_dstirq = 0;                          /* 8259A to INTIN0 */
424         MP_intsrc_info(&intsrc);
425 }
426
427 static inline void __init construct_default_ISA_mptable(int mpc_default_type)
428 {
429         struct mpc_config_processor processor;
430         struct mpc_config_bus bus;
431         struct mpc_config_ioapic ioapic;
432         struct mpc_config_lintsrc lintsrc;
433         int linttypes[2] = { mp_ExtINT, mp_NMI };
434         int i;
435
436         /*
437          * local APIC has default address
438          */
439         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
440
441         /*
442          * 2 CPUs, numbered 0 & 1.
443          */
444         processor.mpc_type = MP_PROCESSOR;
445         /* Either an integrated APIC or a discrete 82489DX. */
446         processor.mpc_apicver = mpc_default_type > 4 ? 0x10 : 0x01;
447         processor.mpc_cpuflag = CPU_ENABLED;
448         processor.mpc_cpufeature = (boot_cpu_data.x86 << 8) |
449                                    (boot_cpu_data.x86_model << 4) |
450                                    boot_cpu_data.x86_mask;
451         processor.mpc_featureflag = boot_cpu_data.x86_capability[0];
452         processor.mpc_reserved[0] = 0;
453         processor.mpc_reserved[1] = 0;
454         for (i = 0; i < 2; i++) {
455                 processor.mpc_apicid = i;
456                 MP_processor_info(&processor);
457         }
458
459         bus.mpc_type = MP_BUS;
460         bus.mpc_busid = 0;
461         switch (mpc_default_type) {
462                 default:
463                         printk(KERN_ERR "???\nUnknown standard configuration %d\n",
464                                 mpc_default_type);
465                         /* fall through */
466                 case 1:
467                 case 5:
468                         memcpy(bus.mpc_bustype, "ISA   ", 6);
469                         break;
470                 case 2:
471                 case 6:
472                 case 3:
473                         memcpy(bus.mpc_bustype, "EISA  ", 6);
474                         break;
475                 case 4:
476                 case 7:
477                         memcpy(bus.mpc_bustype, "MCA   ", 6);
478         }
479         MP_bus_info(&bus);
480         if (mpc_default_type > 4) {
481                 bus.mpc_busid = 1;
482                 memcpy(bus.mpc_bustype, "PCI   ", 6);
483                 MP_bus_info(&bus);
484         }
485
486         ioapic.mpc_type = MP_IOAPIC;
487         ioapic.mpc_apicid = 2;
488         ioapic.mpc_apicver = mpc_default_type > 4 ? 0x10 : 0x01;
489         ioapic.mpc_flags = MPC_APIC_USABLE;
490         ioapic.mpc_apicaddr = 0xFEC00000;
491         MP_ioapic_info(&ioapic);
492
493         /*
494          * We set up most of the low 16 IO-APIC pins according to MPS rules.
495          */
496         construct_default_ioirq_mptable(mpc_default_type);
497
498         lintsrc.mpc_type = MP_LINTSRC;
499         lintsrc.mpc_irqflag = 0;                /* conforming */
500         lintsrc.mpc_srcbusid = 0;
501         lintsrc.mpc_srcbusirq = 0;
502         lintsrc.mpc_destapic = MP_APIC_ALL;
503         for (i = 0; i < 2; i++) {
504                 lintsrc.mpc_irqtype = linttypes[i];
505                 lintsrc.mpc_destapiclint = i;
506                 MP_lintsrc_info(&lintsrc);
507         }
508 }
509
510 static struct intel_mp_floating *mpf_found;
511
512 /*
513  * Scan the memory blocks for an SMP configuration block.
514  */
515 void __init get_smp_config (void)
516 {
517         struct intel_mp_floating *mpf = mpf_found;
518
519         /*
520          * ACPI supports both logical (e.g. Hyper-Threading) and physical 
521          * processors, where MPS only supports physical.
522          */
523         if (acpi_lapic && acpi_ioapic) {
524                 printk(KERN_INFO "Using ACPI (MADT) for SMP configuration information\n");
525                 return;
526         }
527         else if (acpi_lapic)
528                 printk(KERN_INFO "Using ACPI for processor (LAPIC) configuration information\n");
529
530         printk("Intel MultiProcessor Specification v1.%d\n", mpf->mpf_specification);
531         if (mpf->mpf_feature2 & (1<<7)) {
532                 printk(KERN_INFO "    IMCR and PIC compatibility mode.\n");
533                 pic_mode = 1;
534         } else {
535                 printk(KERN_INFO "    Virtual Wire compatibility mode.\n");
536                 pic_mode = 0;
537         }
538
539         /*
540          * Now see if we need to read further.
541          */
542         if (mpf->mpf_feature1 != 0) {
543
544                 printk(KERN_INFO "Default MP configuration #%d\n", mpf->mpf_feature1);
545                 construct_default_ISA_mptable(mpf->mpf_feature1);
546
547         } else if (mpf->mpf_physptr) {
548
549                 /*
550                  * Read the physical hardware table.  Anything here will
551                  * override the defaults.
552                  */
553                 if (!smp_read_mpc((void *)(unsigned long)mpf->mpf_physptr)) {
554                         smp_found_config = 0;
555                         printk(KERN_ERR "BIOS bug, MP table errors detected!...\n");
556                         printk(KERN_ERR "... disabling SMP support. (tell your hw vendor)\n");
557                         return;
558                 }
559                 /*
560                  * If there are no explicit MP IRQ entries, then we are
561                  * broken.  We set up most of the low 16 IO-APIC pins to
562                  * ISA defaults and hope it will work.
563                  */
564                 if (!mp_irq_entries) {
565                         struct mpc_config_bus bus;
566
567                         printk(KERN_ERR "BIOS bug, no explicit IRQ entries, using default mptable. (tell your hw vendor)\n");
568
569                         bus.mpc_type = MP_BUS;
570                         bus.mpc_busid = 0;
571                         memcpy(bus.mpc_bustype, "ISA   ", 6);
572                         MP_bus_info(&bus);
573
574                         construct_default_ioirq_mptable(0);
575                 }
576
577         } else
578                 BUG();
579
580         printk(KERN_INFO "Processors: %d\n", num_processors);
581         /*
582          * Only use the first configuration found.
583          */
584 }
585
586 static int __init smp_scan_config (unsigned long base, unsigned long length)
587 {
588         extern void __bad_mpf_size(void); 
589         unsigned int *bp = phys_to_virt(base);
590         struct intel_mp_floating *mpf;
591
592         Dprintk("Scan SMP from %p for %ld bytes.\n", bp,length);
593         if (sizeof(*mpf) != 16)
594                 __bad_mpf_size();
595
596         while (length > 0) {
597                 mpf = (struct intel_mp_floating *)bp;
598                 if ((*bp == SMP_MAGIC_IDENT) &&
599                         (mpf->mpf_length == 1) &&
600                         !mpf_checksum((unsigned char *)bp, 16) &&
601                         ((mpf->mpf_specification == 1)
602                                 || (mpf->mpf_specification == 4)) ) {
603
604                         smp_found_config = 1;
605                         reserve_bootmem_generic(virt_to_phys(mpf), PAGE_SIZE);
606                         if (mpf->mpf_physptr)
607                                 reserve_bootmem_generic(mpf->mpf_physptr, PAGE_SIZE);
608                         mpf_found = mpf;
609                         return 1;
610                 }
611                 bp += 4;
612                 length -= 16;
613         }
614         return 0;
615 }
616
617 void __init find_intel_smp (void)
618 {
619         unsigned int address;
620
621         /*
622          * FIXME: Linux assumes you have 640K of base ram..
623          * this continues the error...
624          *
625          * 1) Scan the bottom 1K for a signature
626          * 2) Scan the top 1K of base RAM
627          * 3) Scan the 64K of bios
628          */
629         if (smp_scan_config(0x0,0x400) ||
630                 smp_scan_config(639*0x400,0x400) ||
631                         smp_scan_config(0xF0000,0x10000))
632                 return;
633         /*
634          * If it is an SMP machine we should know now, unless the
635          * configuration is in an EISA/MCA bus machine with an
636          * extended bios data area.
637          *
638          * there is a real-mode segmented pointer pointing to the
639          * 4K EBDA area at 0x40E, calculate and scan it here.
640          *
641          * NOTE! There are Linux loaders that will corrupt the EBDA
642          * area, and as such this kind of SMP config may be less
643          * trustworthy, simply because the SMP table may have been
644          * stomped on during early boot. These loaders are buggy and
645          * should be fixed.
646          */
647
648         address = *(unsigned short *)phys_to_virt(0x40E);
649         address <<= 4;
650         if (smp_scan_config(address, 0x1000))
651                 return;
652
653         /* If we have come this far, we did not find an MP table  */
654          printk(KERN_INFO "No mptable found.\n");
655 }
656
657 /*
658  * - Intel MP Configuration Table
659  */
660 void __init find_smp_config (void)
661 {
662 #ifdef CONFIG_X86_LOCAL_APIC
663         find_intel_smp();
664 #endif
665 }
666
667
668 /* --------------------------------------------------------------------------
669                             ACPI-based MP Configuration
670    -------------------------------------------------------------------------- */
671
672 #ifdef CONFIG_ACPI
673
674 void __init mp_register_lapic_address (
675         u64                     address)
676 {
677         mp_lapic_addr = (unsigned long) address;
678
679         set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
680
681         if (boot_cpu_id == -1U)
682                 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
683
684         Dprintk("Boot CPU = %d\n", boot_cpu_physical_apicid);
685 }
686
687
688 void __init mp_register_lapic (
689         u8                      id, 
690         u8                      enabled)
691 {
692         struct mpc_config_processor processor;
693         int                     boot_cpu = 0;
694         
695         if (id >= MAX_APICS) {
696                 printk(KERN_WARNING "Processor #%d invalid (max %d)\n",
697                         id, MAX_APICS);
698                 return;
699         }
700
701         if (id == boot_cpu_physical_apicid)
702                 boot_cpu = 1;
703
704         processor.mpc_type = MP_PROCESSOR;
705         processor.mpc_apicid = id;
706         processor.mpc_apicver = GET_APIC_VERSION(apic_read(APIC_LVR));
707         processor.mpc_cpuflag = (enabled ? CPU_ENABLED : 0);
708         processor.mpc_cpuflag |= (boot_cpu ? CPU_BOOTPROCESSOR : 0);
709         processor.mpc_cpufeature = (boot_cpu_data.x86 << 8) | 
710                 (boot_cpu_data.x86_model << 4) | boot_cpu_data.x86_mask;
711         processor.mpc_featureflag = boot_cpu_data.x86_capability[0];
712         processor.mpc_reserved[0] = 0;
713         processor.mpc_reserved[1] = 0;
714
715         MP_processor_info(&processor);
716 }
717
718 #ifdef CONFIG_X86_IO_APIC
719
720 #define MP_ISA_BUS              0
721 #define MP_MAX_IOAPIC_PIN       127
722
723 static struct mp_ioapic_routing {
724         int                     apic_id;
725         int                     gsi_start;
726         int                     gsi_end;
727         u32                     pin_programmed[4];
728 } mp_ioapic_routing[MAX_IO_APICS];
729
730
731 static int mp_find_ioapic (
732         int                     gsi)
733 {
734         int                     i = 0;
735
736         /* Find the IOAPIC that manages this GSI. */
737         for (i = 0; i < nr_ioapics; i++) {
738                 if ((gsi >= mp_ioapic_routing[i].gsi_start)
739                         && (gsi <= mp_ioapic_routing[i].gsi_end))
740                         return i;
741         }
742
743         printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
744
745         return -1;
746 }
747         
748
749 void __init mp_register_ioapic (
750         u8                      id, 
751         u32                     address,
752         u32                     gsi_base)
753 {
754         int                     idx = 0;
755
756         if (nr_ioapics >= MAX_IO_APICS) {
757                 printk(KERN_ERR "ERROR: Max # of I/O APICs (%d) exceeded "
758                         "(found %d)\n", MAX_IO_APICS, nr_ioapics);
759                 panic("Recompile kernel with bigger MAX_IO_APICS!\n");
760         }
761         if (!address) {
762                 printk(KERN_ERR "WARNING: Bogus (zero) I/O APIC address"
763                         " found in MADT table, skipping!\n");
764                 return;
765         }
766
767         idx = nr_ioapics++;
768
769         mp_ioapics[idx].mpc_type = MP_IOAPIC;
770         mp_ioapics[idx].mpc_flags = MPC_APIC_USABLE;
771         mp_ioapics[idx].mpc_apicaddr = address;
772
773         set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
774         mp_ioapics[idx].mpc_apicid = id;
775         mp_ioapics[idx].mpc_apicver = io_apic_get_version(idx);
776         
777         /* 
778          * Build basic IRQ lookup table to facilitate gsi->io_apic lookups
779          * and to prevent reprogramming of IOAPIC pins (PCI IRQs).
780          */
781         mp_ioapic_routing[idx].apic_id = mp_ioapics[idx].mpc_apicid;
782         mp_ioapic_routing[idx].gsi_start = gsi_base;
783         mp_ioapic_routing[idx].gsi_end = gsi_base + 
784                 io_apic_get_redir_entries(idx);
785
786         printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
787                 "GSI %d-%d\n", idx, mp_ioapics[idx].mpc_apicid, 
788                 mp_ioapics[idx].mpc_apicver, mp_ioapics[idx].mpc_apicaddr,
789                 mp_ioapic_routing[idx].gsi_start,
790                 mp_ioapic_routing[idx].gsi_end);
791
792         return;
793 }
794
795
796 void __init mp_override_legacy_irq (
797         u8                      bus_irq,
798         u8                      polarity, 
799         u8                      trigger, 
800         u32                     gsi)
801 {
802         struct mpc_config_intsrc intsrc;
803         int                     ioapic = -1;
804         int                     pin = -1;
805
806         /* 
807          * Convert 'gsi' to 'ioapic.pin'.
808          */
809         ioapic = mp_find_ioapic(gsi);
810         if (ioapic < 0)
811                 return;
812         pin = gsi - mp_ioapic_routing[ioapic].gsi_start;
813
814         /*
815          * TBD: This check is for faulty timer entries, where the override
816          *      erroneously sets the trigger to level, resulting in a HUGE 
817          *      increase of timer interrupts!
818          */
819         if ((bus_irq == 0) && (trigger == 3))
820                 trigger = 1;
821
822         intsrc.mpc_type = MP_INTSRC;
823         intsrc.mpc_irqtype = mp_INT;
824         intsrc.mpc_irqflag = (trigger << 2) | polarity;
825         intsrc.mpc_srcbus = MP_ISA_BUS;
826         intsrc.mpc_srcbusirq = bus_irq;                                /* IRQ */
827         intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;        /* APIC ID */
828         intsrc.mpc_dstirq = pin;                                    /* INTIN# */
829
830         Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, %d-%d\n", 
831                 intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3, 
832                 (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus, 
833                 intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, intsrc.mpc_dstirq);
834
835         mp_irqs[mp_irq_entries] = intsrc;
836         if (++mp_irq_entries == MAX_IRQ_SOURCES)
837                 panic("Max # of irq sources exceeded!\n");
838
839         return;
840 }
841
842
843 void __init mp_config_acpi_legacy_irqs (void)
844 {
845         struct mpc_config_intsrc intsrc;
846         int                     i = 0;
847         int                     ioapic = -1;
848
849         /* 
850          * Fabricate the legacy ISA bus (bus #31).
851          */
852         mp_bus_id_to_type[MP_ISA_BUS] = MP_BUS_ISA;
853         Dprintk("Bus #%d is ISA\n", MP_ISA_BUS);
854
855         /* 
856          * Locate the IOAPIC that manages the ISA IRQs (0-15). 
857          */
858         ioapic = mp_find_ioapic(0);
859         if (ioapic < 0)
860                 return;
861
862         intsrc.mpc_type = MP_INTSRC;
863         intsrc.mpc_irqflag = 0;                                 /* Conforming */
864         intsrc.mpc_srcbus = MP_ISA_BUS;
865         intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;
866
867         /* 
868          * Use the default configuration for the IRQs 0-15.  Unless
869          * overridden by (MADT) interrupt source override entries.
870          */
871         for (i = 0; i < 16; i++) {
872                 int idx;
873
874                 for (idx = 0; idx < mp_irq_entries; idx++) {
875                         struct mpc_config_intsrc *irq = mp_irqs + idx;
876
877                         /* Do we already have a mapping for this ISA IRQ? */
878                         if (irq->mpc_srcbus == MP_ISA_BUS && irq->mpc_srcbusirq == i)
879                                 break;
880
881                         /* Do we already have a mapping for this IOAPIC pin */
882                         if ((irq->mpc_dstapic == intsrc.mpc_dstapic) &&
883                                 (irq->mpc_dstirq == i))
884                                 break;
885                 }
886
887                 if (idx != mp_irq_entries) {
888                         printk(KERN_DEBUG "ACPI: IRQ%d used by override.\n", i);
889                         continue;                       /* IRQ already used */
890                 }
891
892                 intsrc.mpc_irqtype = mp_INT;
893                 intsrc.mpc_srcbusirq = i;                  /* Identity mapped */
894                 intsrc.mpc_dstirq = i;
895
896                 Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, "
897                         "%d-%d\n", intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3, 
898                         (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus, 
899                         intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, 
900                         intsrc.mpc_dstirq);
901
902                 mp_irqs[mp_irq_entries] = intsrc;
903                 if (++mp_irq_entries == MAX_IRQ_SOURCES)
904                         panic("Max # of irq sources exceeded!\n");
905         }
906
907         return;
908 }
909
910 #define MAX_GSI_NUM     4096
911
912 int mp_register_gsi(u32 gsi, int edge_level, int active_high_low)
913 {
914         int                     ioapic = -1;
915         int                     ioapic_pin = 0;
916         int                     idx, bit = 0;
917         static int              pci_irq = 16;
918         /*
919          * Mapping between Global System Interrupts, which
920          * represent all possible interrupts, to the IRQs
921          * assigned to actual devices.
922          */
923         static int              gsi_to_irq[MAX_GSI_NUM];
924
925         if (acpi_irq_model != ACPI_IRQ_MODEL_IOAPIC)
926                 return gsi;
927
928         /* Don't set up the ACPI SCI because it's already set up */
929         if (acpi_fadt.sci_int == gsi)
930                 return gsi;
931
932         ioapic = mp_find_ioapic(gsi);
933         if (ioapic < 0) {
934                 printk(KERN_WARNING "No IOAPIC for GSI %u\n", gsi);
935                 return gsi;
936         }
937
938         ioapic_pin = gsi - mp_ioapic_routing[ioapic].gsi_start;
939
940         /* 
941          * Avoid pin reprogramming.  PRTs typically include entries  
942          * with redundant pin->gsi mappings (but unique PCI devices);
943          * we only program the IOAPIC on the first.
944          */
945         bit = ioapic_pin % 32;
946         idx = (ioapic_pin < 32) ? 0 : (ioapic_pin / 32);
947         if (idx > 3) {
948                 printk(KERN_ERR "Invalid reference to IOAPIC pin "
949                         "%d-%d\n", mp_ioapic_routing[ioapic].apic_id, 
950                         ioapic_pin);
951                 return gsi;
952         }
953         if ((1<<bit) & mp_ioapic_routing[ioapic].pin_programmed[idx]) {
954                 Dprintk(KERN_DEBUG "Pin %d-%d already programmed\n",
955                         mp_ioapic_routing[ioapic].apic_id, ioapic_pin);
956                 return gsi_to_irq[gsi];
957         }
958
959         mp_ioapic_routing[ioapic].pin_programmed[idx] |= (1<<bit);
960
961         if (edge_level) {
962                 /*
963                  * For PCI devices assign IRQs in order, avoiding gaps
964                  * due to unused I/O APIC pins.
965                  */
966                 int irq = gsi;
967                 if (gsi < MAX_GSI_NUM) {
968                         if (gsi > 15)
969                                 gsi = pci_irq++;
970                         /*
971                          * Don't assign IRQ used by ACPI SCI
972                          */
973                         if (gsi == acpi_fadt.sci_int)
974                                 gsi = pci_irq++;
975                         gsi_to_irq[irq] = gsi;
976                 } else {
977                         printk(KERN_ERR "GSI %u is too high\n", gsi);
978                         return gsi;
979                 }
980         }
981
982         io_apic_set_pci_routing(ioapic, ioapic_pin, gsi,
983                 edge_level == ACPI_EDGE_SENSITIVE ? 0 : 1,
984                 active_high_low == ACPI_ACTIVE_HIGH ? 0 : 1);
985         return gsi;
986 }
987
988 #endif /*CONFIG_X86_IO_APIC*/
989 #endif /*CONFIG_ACPI*/