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