patch-2_6_7-vs1_9_1_12
[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         static int printed __initdata; 
579
580         Dprintk("Scan SMP from %p for %ld bytes.\n", bp,length);
581         if (sizeof(*mpf) != 16)
582                 __bad_mpf_size();
583
584         while (length > 0) {
585                 mpf = (struct intel_mp_floating *)bp;
586                 if ((*bp == SMP_MAGIC_IDENT) &&
587                         (mpf->mpf_length == 1) &&
588                         !mpf_checksum((unsigned char *)bp, 16) &&
589                         ((mpf->mpf_specification == 1)
590                                 || (mpf->mpf_specification == 4)) ) {
591
592                         smp_found_config = 1;
593                         reserve_bootmem_generic(virt_to_phys(mpf), PAGE_SIZE);
594                         if (mpf->mpf_physptr)
595                                 reserve_bootmem_generic(mpf->mpf_physptr, PAGE_SIZE);
596                         mpf_found = mpf;
597                         return 1;
598                 }
599                 bp += 4;
600                 length -= 16;
601         }
602         if (!printed) {         
603                 printk(KERN_INFO "No mptable found.\n");
604                 printed = 1;
605         }
606         return 0;
607 }
608
609 void __init find_intel_smp (void)
610 {
611         unsigned int address;
612
613         /*
614          * FIXME: Linux assumes you have 640K of base ram..
615          * this continues the error...
616          *
617          * 1) Scan the bottom 1K for a signature
618          * 2) Scan the top 1K of base RAM
619          * 3) Scan the 64K of bios
620          */
621         if (smp_scan_config(0x0,0x400) ||
622                 smp_scan_config(639*0x400,0x400) ||
623                         smp_scan_config(0xF0000,0x10000))
624                 return;
625         /*
626          * If it is an SMP machine we should know now, unless the
627          * configuration is in an EISA/MCA bus machine with an
628          * extended bios data area.
629          *
630          * there is a real-mode segmented pointer pointing to the
631          * 4K EBDA area at 0x40E, calculate and scan it here.
632          *
633          * NOTE! There are Linux loaders that will corrupt the EBDA
634          * area, and as such this kind of SMP config may be less
635          * trustworthy, simply because the SMP table may have been
636          * stomped on during early boot. These loaders are buggy and
637          * should be fixed.
638          */
639
640         address = *(unsigned short *)phys_to_virt(0x40E);
641         address <<= 4;
642         smp_scan_config(address, 0x1000);
643 }
644
645 /*
646  * - Intel MP Configuration Table
647  */
648 void __init find_smp_config (void)
649 {
650 #ifdef CONFIG_X86_LOCAL_APIC
651         find_intel_smp();
652 #endif
653 }
654
655
656 /* --------------------------------------------------------------------------
657                             ACPI-based MP Configuration
658    -------------------------------------------------------------------------- */
659
660 #ifdef CONFIG_ACPI_BOOT
661
662 void __init mp_register_lapic_address (
663         u64                     address)
664 {
665         mp_lapic_addr = (unsigned long) address;
666
667         set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
668
669         if (boot_cpu_id == -1U)
670                 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
671
672         Dprintk("Boot CPU = %d\n", boot_cpu_physical_apicid);
673 }
674
675
676 void __init mp_register_lapic (
677         u8                      id, 
678         u8                      enabled)
679 {
680         struct mpc_config_processor processor;
681         int                     boot_cpu = 0;
682         
683         if (id >= MAX_APICS) {
684                 printk(KERN_WARNING "Processor #%d invalid (max %d)\n",
685                         id, MAX_APICS);
686                 return;
687         }
688
689         if (id == boot_cpu_physical_apicid)
690                 boot_cpu = 1;
691
692         processor.mpc_type = MP_PROCESSOR;
693         processor.mpc_apicid = id;
694         processor.mpc_apicver = 0x10; /* TBD: lapic version */
695         processor.mpc_cpuflag = (enabled ? CPU_ENABLED : 0);
696         processor.mpc_cpuflag |= (boot_cpu ? CPU_BOOTPROCESSOR : 0);
697         processor.mpc_cpufeature = (boot_cpu_data.x86 << 8) | 
698                 (boot_cpu_data.x86_model << 4) | boot_cpu_data.x86_mask;
699         processor.mpc_featureflag = boot_cpu_data.x86_capability[0];
700         processor.mpc_reserved[0] = 0;
701         processor.mpc_reserved[1] = 0;
702
703         MP_processor_info(&processor);
704 }
705
706 #ifdef CONFIG_X86_IO_APIC
707
708 #define MP_ISA_BUS              0
709 #define MP_MAX_IOAPIC_PIN       127
710
711 struct mp_ioapic_routing {
712         int                     apic_id;
713         int                     gsi_start;
714         int                     gsi_end;
715         u32                     pin_programmed[4];
716 } mp_ioapic_routing[MAX_IO_APICS];
717
718
719 static int __init mp_find_ioapic (
720         int                     gsi)
721 {
722         int                     i = 0;
723
724         /* Find the IOAPIC that manages this GSI. */
725         for (i = 0; i < nr_ioapics; i++) {
726                 if ((gsi >= mp_ioapic_routing[i].gsi_start)
727                         && (gsi <= mp_ioapic_routing[i].gsi_end))
728                         return i;
729         }
730
731         printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
732
733         return -1;
734 }
735         
736
737 void __init mp_register_ioapic (
738         u8                      id, 
739         u32                     address,
740         u32                     gsi_base)
741 {
742         int                     idx = 0;
743
744         if (nr_ioapics >= MAX_IO_APICS) {
745                 printk(KERN_ERR "ERROR: Max # of I/O APICs (%d) exceeded "
746                         "(found %d)\n", MAX_IO_APICS, nr_ioapics);
747                 panic("Recompile kernel with bigger MAX_IO_APICS!\n");
748         }
749         if (!address) {
750                 printk(KERN_ERR "WARNING: Bogus (zero) I/O APIC address"
751                         " found in MADT table, skipping!\n");
752                 return;
753         }
754
755         idx = nr_ioapics++;
756
757         mp_ioapics[idx].mpc_type = MP_IOAPIC;
758         mp_ioapics[idx].mpc_flags = MPC_APIC_USABLE;
759         mp_ioapics[idx].mpc_apicaddr = address;
760
761         set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
762         mp_ioapics[idx].mpc_apicid = io_apic_get_unique_id(idx, id);
763         mp_ioapics[idx].mpc_apicver = io_apic_get_version(idx);
764         
765         /* 
766          * Build basic IRQ lookup table to facilitate gsi->io_apic lookups
767          * and to prevent reprogramming of IOAPIC pins (PCI IRQs).
768          */
769         mp_ioapic_routing[idx].apic_id = mp_ioapics[idx].mpc_apicid;
770         mp_ioapic_routing[idx].gsi_start = gsi_base;
771         mp_ioapic_routing[idx].gsi_end = gsi_base + 
772                 io_apic_get_redir_entries(idx);
773
774         printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
775                 "GSI %d-%d\n", idx, mp_ioapics[idx].mpc_apicid, 
776                 mp_ioapics[idx].mpc_apicver, mp_ioapics[idx].mpc_apicaddr,
777                 mp_ioapic_routing[idx].gsi_start,
778                 mp_ioapic_routing[idx].gsi_end);
779
780         return;
781 }
782
783
784 void __init mp_override_legacy_irq (
785         u8                      bus_irq,
786         u8                      polarity, 
787         u8                      trigger, 
788         u32                     gsi)
789 {
790         struct mpc_config_intsrc intsrc;
791         int                     ioapic = -1;
792         int                     pin = -1;
793
794         /* 
795          * Convert 'gsi' to 'ioapic.pin'.
796          */
797         ioapic = mp_find_ioapic(gsi);
798         if (ioapic < 0)
799                 return;
800         pin = gsi - mp_ioapic_routing[ioapic].gsi_start;
801
802         /*
803          * TBD: This check is for faulty timer entries, where the override
804          *      erroneously sets the trigger to level, resulting in a HUGE 
805          *      increase of timer interrupts!
806          */
807         if ((bus_irq == 0) && (trigger == 3))
808                 trigger = 1;
809
810         intsrc.mpc_type = MP_INTSRC;
811         intsrc.mpc_irqtype = mp_INT;
812         intsrc.mpc_irqflag = (trigger << 2) | polarity;
813         intsrc.mpc_srcbus = MP_ISA_BUS;
814         intsrc.mpc_srcbusirq = bus_irq;                                /* IRQ */
815         intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;        /* APIC ID */
816         intsrc.mpc_dstirq = pin;                                    /* INTIN# */
817
818         Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, %d-%d\n", 
819                 intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3, 
820                 (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus, 
821                 intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, intsrc.mpc_dstirq);
822
823         mp_irqs[mp_irq_entries] = intsrc;
824         if (++mp_irq_entries == MAX_IRQ_SOURCES)
825                 panic("Max # of irq sources exceeded!\n");
826
827         return;
828 }
829
830
831 void __init mp_config_acpi_legacy_irqs (void)
832 {
833         struct mpc_config_intsrc intsrc;
834         int                     i = 0;
835         int                     ioapic = -1;
836
837         /* 
838          * Fabricate the legacy ISA bus (bus #31).
839          */
840         mp_bus_id_to_type[MP_ISA_BUS] = MP_BUS_ISA;
841         Dprintk("Bus #%d is ISA\n", MP_ISA_BUS);
842
843         /* 
844          * Locate the IOAPIC that manages the ISA IRQs (0-15). 
845          */
846         ioapic = mp_find_ioapic(0);
847         if (ioapic < 0)
848                 return;
849
850         intsrc.mpc_type = MP_INTSRC;
851         intsrc.mpc_irqflag = 0;                                 /* Conforming */
852         intsrc.mpc_srcbus = MP_ISA_BUS;
853         intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;
854
855         /* 
856          * Use the default configuration for the IRQs 0-15.  Unless
857          * overridden by (MADT) interrupt source override entries.
858          */
859         for (i = 0; i < 16; i++) {
860                 int idx;
861
862                 for (idx = 0; idx < mp_irq_entries; idx++)
863                         if (mp_irqs[idx].mpc_srcbus == MP_ISA_BUS &&
864                                 (mp_irqs[idx].mpc_srcbusirq == i ||
865                                 mp_irqs[idx].mpc_dstirq == i))
866                                         break;
867
868                 if (idx != mp_irq_entries) {
869                         printk(KERN_DEBUG "ACPI: IRQ%d used by override.\n", i);
870                         continue;                        /* IRQ already used */
871                 }
872
873                 intsrc.mpc_irqtype = mp_INT;
874                 intsrc.mpc_srcbusirq = i;                  /* Identity mapped */
875                 intsrc.mpc_dstirq = i;
876
877                 Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, "
878                         "%d-%d\n", intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3, 
879                         (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus, 
880                         intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, 
881                         intsrc.mpc_dstirq);
882
883                 mp_irqs[mp_irq_entries] = intsrc;
884                 if (++mp_irq_entries == MAX_IRQ_SOURCES)
885                         panic("Max # of irq sources exceeded!\n");
886         }
887
888         return;
889 }
890
891
892 extern FADT_DESCRIPTOR acpi_fadt;
893
894 #ifdef CONFIG_ACPI_PCI
895
896 void __init mp_parse_prt (void)
897 {
898         struct list_head        *node = NULL;
899         struct acpi_prt_entry   *entry = NULL;
900         int                     ioapic = -1;
901         int                     ioapic_pin = 0;
902         int                     gsi = 0;
903         int                     idx, bit = 0;
904         int                     edge_level = 0;
905         int                     active_high_low = 0;
906
907         /*
908          * Parsing through the PCI Interrupt Routing Table (PRT) and program
909          * routing for all static (IOAPIC-direct) entries.
910          */
911         list_for_each(node, &acpi_prt.entries) {
912                 entry = list_entry(node, struct acpi_prt_entry, node);
913
914                 /* Need to get gsi for dynamic entry */
915                 if (entry->link.handle) {
916                         gsi = acpi_pci_link_get_irq(entry->link.handle, entry->link.index, &edge_level, &active_high_low);
917                         if (!gsi)
918                                 continue;
919                 } else {
920                         /* Hardwired GSI. Assume PCI standard settings */
921                         gsi = entry->link.index;
922                         edge_level = 1;
923                         active_high_low = 1;
924                 }
925
926                 /* Don't set up the ACPI SCI because it's already set up */
927                 if (acpi_fadt.sci_int == gsi) {
928                         /* we still need to set up the entry's irq */
929                         acpi_gsi_to_irq(gsi, &entry->irq);
930                         continue;
931                 }
932
933                 ioapic = mp_find_ioapic(gsi);
934                 if (ioapic < 0)
935                         continue;
936                 ioapic_pin = gsi - mp_ioapic_routing[ioapic].gsi_start;
937
938                 /* 
939                  * Avoid pin reprogramming.  PRTs typically include entries  
940                  * with redundant pin->gsi mappings (but unique PCI devices);
941                  * we only only program the IOAPIC on the first.
942                  */
943                 bit = ioapic_pin % 32;
944                 idx = (ioapic_pin < 32) ? 0 : (ioapic_pin / 32);
945                 if (idx > 3) {
946                         printk(KERN_ERR "Invalid reference to IOAPIC pin "
947                                 "%d-%d\n", mp_ioapic_routing[ioapic].apic_id, 
948                                 ioapic_pin);
949                         continue;
950                 }
951                 if ((1<<bit) & mp_ioapic_routing[ioapic].pin_programmed[idx]) {
952                         Dprintk(KERN_DEBUG "Pin %d-%d already programmed\n",
953                                 mp_ioapic_routing[ioapic].apic_id, ioapic_pin);
954                         acpi_gsi_to_irq(gsi, &entry->irq);
955                         continue;
956                 }
957
958                 mp_ioapic_routing[ioapic].pin_programmed[idx] |= (1<<bit);
959                 if (!io_apic_set_pci_routing(ioapic, ioapic_pin, gsi, edge_level, active_high_low)) {
960                         acpi_gsi_to_irq(gsi, &entry->irq);
961                 }
962                 printk(KERN_DEBUG "%02x:%02x:%02x[%c] -> %d-%d -> IRQ %d\n",
963                         entry->id.segment, entry->id.bus,
964                         entry->id.device, ('A' + entry->pin), 
965                         mp_ioapic_routing[ioapic].apic_id, ioapic_pin,
966                         entry->irq);
967         }
968         
969         print_IO_APIC();
970
971         return;
972 }
973
974 #endif /*CONFIG_ACPI_PCI*/
975
976 #endif /*CONFIG_X86_IO_APIC*/
977
978 #endif /*CONFIG_ACPI_BOOT*/