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