]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/mpparse_32.c
4cc325edc70de357e26a66c41fed15824a2fe1a8
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / mpparse_32.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/acpi.h>
19 #include <linux/delay.h>
20 #include <linux/bootmem.h>
21 #include <linux/kernel_stat.h>
22 #include <linux/mc146818rtc.h>
23 #include <linux/bitops.h>
24
25 #include <asm/smp.h>
26 #include <asm/acpi.h>
27 #include <asm/mtrr.h>
28 #include <asm/mpspec.h>
29 #include <asm/io_apic.h>
30 #include <asm/bios_ebda.h>
31
32 #include <mach_apic.h>
33 #include <mach_apicdef.h>
34 #include <mach_mpparse.h>
35
36 /* Have we found an MP table */
37 int smp_found_config;
38 unsigned int __cpuinitdata maxcpus = NR_CPUS;
39
40 /*
41  * Various Linux-internal data structures created from the
42  * MP-table.
43  */
44 int apic_version [MAX_APICS];
45 #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
46 int mp_bus_id_to_type [MAX_MP_BUSSES];
47 #endif
48 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
49 int mp_bus_id_to_pci_bus [MAX_MP_BUSSES] = { [0 ... MAX_MP_BUSSES-1] = -1 };
50 static int mp_current_pci_id;
51
52 /* I/O APIC entries */
53 struct mpc_config_ioapic mp_ioapics[MAX_IO_APICS];
54
55 /* # of MP IRQ source entries */
56 struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
57
58 /* MP IRQ source entries */
59 int mp_irq_entries;
60
61 int nr_ioapics;
62
63 int pic_mode;
64 unsigned long mp_lapic_addr;
65
66 unsigned int def_to_bigsmp = 0;
67
68 /* Processor that is doing the boot up */
69 unsigned int boot_cpu_physical_apicid = -1U;
70 /* Internal processor count */
71 unsigned int num_processors;
72
73 unsigned disabled_cpus __cpuinitdata;
74
75 /* Bitmask of physically existing CPUs */
76 physid_mask_t phys_cpu_present_map;
77
78 #ifndef CONFIG_SMP
79 DEFINE_PER_CPU(u16, x86_bios_cpu_apicid) = BAD_APICID;
80 #endif
81
82 /*
83  * Intel MP BIOS table parsing routines:
84  */
85
86
87 /*
88  * Checksum an MP configuration block.
89  */
90
91 static int __init mpf_checksum(unsigned char *mp, int len)
92 {
93         int sum = 0;
94
95         while (len--)
96                 sum += *mp++;
97
98         return sum & 0xFF;
99 }
100
101 #ifdef CONFIG_X86_NUMAQ
102 /*
103  * Have to match translation table entries to main table entries by counter
104  * hence the mpc_record variable .... can't see a less disgusting way of
105  * doing this ....
106  */
107
108 static int mpc_record; 
109 static struct mpc_config_translation *translation_table[MAX_MPC_ENTRY] __cpuinitdata;
110 #endif
111
112 static void __cpuinit MP_processor_info (struct mpc_config_processor *m)
113 {
114         int ver, apicid, cpu;
115         cpumask_t tmp_map;
116         physid_mask_t phys_cpu;
117         
118         if (!(m->mpc_cpuflag & CPU_ENABLED)) {
119                 disabled_cpus++;
120                 return;
121         }
122
123 #ifdef CONFIG_X86_NUMAQ
124         apicid = mpc_apic_id(m, translation_table[mpc_record]);
125 #else
126         Dprintk("Processor #%d %u:%u APIC version %d\n",
127                 m->mpc_apicid,
128                 (m->mpc_cpufeature & CPU_FAMILY_MASK) >> 8,
129                 (m->mpc_cpufeature & CPU_MODEL_MASK) >> 4,
130                 m->mpc_apicver);
131         apicid = m->mpc_apicid;
132 #endif
133
134         if (m->mpc_featureflag&(1<<0))
135                 Dprintk("    Floating point unit present.\n");
136         if (m->mpc_featureflag&(1<<7))
137                 Dprintk("    Machine Exception supported.\n");
138         if (m->mpc_featureflag&(1<<8))
139                 Dprintk("    64 bit compare & exchange supported.\n");
140         if (m->mpc_featureflag&(1<<9))
141                 Dprintk("    Internal APIC present.\n");
142         if (m->mpc_featureflag&(1<<11))
143                 Dprintk("    SEP present.\n");
144         if (m->mpc_featureflag&(1<<12))
145                 Dprintk("    MTRR  present.\n");
146         if (m->mpc_featureflag&(1<<13))
147                 Dprintk("    PGE  present.\n");
148         if (m->mpc_featureflag&(1<<14))
149                 Dprintk("    MCA  present.\n");
150         if (m->mpc_featureflag&(1<<15))
151                 Dprintk("    CMOV  present.\n");
152         if (m->mpc_featureflag&(1<<16))
153                 Dprintk("    PAT  present.\n");
154         if (m->mpc_featureflag&(1<<17))
155                 Dprintk("    PSE  present.\n");
156         if (m->mpc_featureflag&(1<<18))
157                 Dprintk("    PSN  present.\n");
158         if (m->mpc_featureflag&(1<<19))
159                 Dprintk("    Cache Line Flush Instruction present.\n");
160         /* 20 Reserved */
161         if (m->mpc_featureflag&(1<<21))
162                 Dprintk("    Debug Trace and EMON Store present.\n");
163         if (m->mpc_featureflag&(1<<22))
164                 Dprintk("    ACPI Thermal Throttle Registers  present.\n");
165         if (m->mpc_featureflag&(1<<23))
166                 Dprintk("    MMX  present.\n");
167         if (m->mpc_featureflag&(1<<24))
168                 Dprintk("    FXSR  present.\n");
169         if (m->mpc_featureflag&(1<<25))
170                 Dprintk("    XMM  present.\n");
171         if (m->mpc_featureflag&(1<<26))
172                 Dprintk("    Willamette New Instructions  present.\n");
173         if (m->mpc_featureflag&(1<<27))
174                 Dprintk("    Self Snoop  present.\n");
175         if (m->mpc_featureflag&(1<<28))
176                 Dprintk("    HT  present.\n");
177         if (m->mpc_featureflag&(1<<29))
178                 Dprintk("    Thermal Monitor present.\n");
179         /* 30, 31 Reserved */
180
181
182         if (m->mpc_cpuflag & CPU_BOOTPROCESSOR) {
183                 Dprintk("    Bootup CPU\n");
184                 boot_cpu_physical_apicid = m->mpc_apicid;
185         }
186
187         ver = m->mpc_apicver;
188
189         /*
190          * Validate version
191          */
192         if (ver == 0x0) {
193                 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
194                                 "fixing up to 0x10. (tell your hw vendor)\n",
195                                 m->mpc_apicid);
196                 ver = 0x10;
197         }
198         apic_version[m->mpc_apicid] = ver;
199
200         phys_cpu = apicid_to_cpu_present(apicid);
201         physids_or(phys_cpu_present_map, phys_cpu_present_map, phys_cpu);
202
203         if (num_processors >= NR_CPUS) {
204                 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
205                         "  Processor ignored.\n", NR_CPUS);
206                 return;
207         }
208
209         if (num_processors >= maxcpus) {
210                 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
211                         " Processor ignored.\n", maxcpus);
212                 return;
213         }
214
215         num_processors++;
216         cpus_complement(tmp_map, cpu_present_map);
217         cpu = first_cpu(tmp_map);
218
219         if (m->mpc_cpuflag & CPU_BOOTPROCESSOR)
220                 /*
221                  * x86_bios_cpu_apicid is required to have processors listed
222                  * in same order as logical cpu numbers. Hence the first
223                  * entry is BSP, and so on.
224                  */
225                 cpu = 0;
226
227         /*
228          * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
229          * but we need to work other dependencies like SMP_SUSPEND etc
230          * before this can be done without some confusion.
231          * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
232          *       - Ashok Raj <ashok.raj@intel.com>
233          */
234         if (num_processors > 8) {
235                 switch (boot_cpu_data.x86_vendor) {
236                 case X86_VENDOR_INTEL:
237                         if (!APIC_XAPIC(ver)) {
238                                 def_to_bigsmp = 0;
239                                 break;
240                         }
241                         /* If P4 and above fall through */
242                 case X86_VENDOR_AMD:
243                         def_to_bigsmp = 1;
244                 }
245         }
246 #ifdef CONFIG_SMP
247         /* are we being called early in kernel startup? */
248         if (x86_cpu_to_apicid_early_ptr) {
249                 u16 *cpu_to_apicid = x86_cpu_to_apicid_early_ptr;
250                 u16 *bios_cpu_apicid = x86_bios_cpu_apicid_early_ptr;
251
252                 cpu_to_apicid[cpu] = m->mpc_apicid;
253                 bios_cpu_apicid[cpu] = m->mpc_apicid;
254         } else {
255                 per_cpu(x86_cpu_to_apicid, cpu) = m->mpc_apicid;
256                 per_cpu(x86_bios_cpu_apicid, cpu) = m->mpc_apicid;
257         }
258 #endif
259         cpu_set(cpu, cpu_possible_map);
260         cpu_set(cpu, cpu_present_map);
261 }
262
263 static void __init MP_bus_info (struct mpc_config_bus *m)
264 {
265         char str[7];
266
267         memcpy(str, m->mpc_bustype, 6);
268         str[6] = 0;
269
270 #ifdef CONFIG_X86_NUMAQ
271         mpc_oem_bus_info(m, str, translation_table[mpc_record]);
272 #else
273         Dprintk("Bus #%d is %s\n", m->mpc_busid, str);
274 #endif
275
276 #if MAX_MP_BUSSES < 256
277         if (m->mpc_busid >= MAX_MP_BUSSES) {
278                 printk(KERN_WARNING "MP table busid value (%d) for bustype %s "
279                         " is too large, max. supported is %d\n",
280                         m->mpc_busid, str, MAX_MP_BUSSES - 1);
281                 return;
282         }
283 #endif
284
285         set_bit(m->mpc_busid, mp_bus_not_pci);
286         if (strncmp(str, BUSTYPE_PCI, sizeof(BUSTYPE_PCI)-1) == 0) {
287 #ifdef CONFIG_X86_NUMAQ
288                 mpc_oem_pci_bus(m, translation_table[mpc_record]);
289 #endif
290                 clear_bit(m->mpc_busid, mp_bus_not_pci);
291                 mp_bus_id_to_pci_bus[m->mpc_busid] = mp_current_pci_id;
292                 mp_current_pci_id++;
293 #if defined(CONFIG_EISA) || defined (CONFIG_MCA)
294                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_PCI;
295         } else if (strncmp(str, BUSTYPE_ISA, sizeof(BUSTYPE_ISA)-1) == 0) {
296                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_ISA;
297         } else if (strncmp(str, BUSTYPE_EISA, sizeof(BUSTYPE_EISA)-1) == 0) {
298                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_EISA;
299         } else if (strncmp(str, BUSTYPE_MCA, sizeof(BUSTYPE_MCA)-1) == 0) {
300                 mp_bus_id_to_type[m->mpc_busid] = MP_BUS_MCA;
301         } else {
302                 printk(KERN_WARNING "Unknown bustype %s - ignoring\n", str);
303 #endif
304         }
305 }
306
307 static int bad_ioapic(unsigned long address)
308 {
309         if (nr_ioapics >= MAX_IO_APICS) {
310                 printk(KERN_ERR "ERROR: Max # of I/O APICs (%d) exceeded "
311                        "(found %d)\n", MAX_IO_APICS, nr_ioapics);
312                 panic("Recompile kernel with bigger MAX_IO_APICS!\n");
313         }
314         if (!address) {
315                 printk(KERN_ERR "WARNING: Bogus (zero) I/O APIC address"
316                        " found in table, skipping!\n");
317                 return 1;
318         }
319         return 0;
320 }
321
322 static void __init MP_ioapic_info (struct mpc_config_ioapic *m)
323 {
324         if (!(m->mpc_flags & MPC_APIC_USABLE))
325                 return;
326
327         printk(KERN_INFO "I/O APIC #%d Version %d at 0x%X.\n",
328                 m->mpc_apicid, m->mpc_apicver, m->mpc_apicaddr);
329
330         if (bad_ioapic(m->mpc_apicaddr))
331                 return;
332
333         mp_ioapics[nr_ioapics] = *m;
334         nr_ioapics++;
335 }
336
337 static void __init MP_intsrc_info (struct mpc_config_intsrc *m)
338 {
339         mp_irqs [mp_irq_entries] = *m;
340         Dprintk("Int: type %d, pol %d, trig %d, bus %d,"
341                 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
342                         m->mpc_irqtype, m->mpc_irqflag & 3,
343                         (m->mpc_irqflag >> 2) & 3, m->mpc_srcbus,
344                         m->mpc_srcbusirq, m->mpc_dstapic, m->mpc_dstirq);
345         if (++mp_irq_entries == MAX_IRQ_SOURCES)
346                 panic("Max # of irq sources exceeded!!\n");
347 }
348
349 static void __init MP_lintsrc_info (struct mpc_config_lintsrc *m)
350 {
351         Dprintk("Lint: type %d, pol %d, trig %d, bus %d,"
352                 " IRQ %02x, APIC ID %x, APIC LINT %02x\n",
353                         m->mpc_irqtype, m->mpc_irqflag & 3,
354                         (m->mpc_irqflag >> 2) &3, m->mpc_srcbusid,
355                         m->mpc_srcbusirq, m->mpc_destapic, m->mpc_destapiclint);
356 }
357
358 #ifdef CONFIG_X86_NUMAQ
359 static void __init MP_translation_info (struct mpc_config_translation *m)
360 {
361         printk(KERN_INFO "Translation: record %d, type %d, quad %d, global %d, local %d\n", mpc_record, m->trans_type, m->trans_quad, m->trans_global, m->trans_local);
362
363         if (mpc_record >= MAX_MPC_ENTRY) 
364                 printk(KERN_ERR "MAX_MPC_ENTRY exceeded!\n");
365         else
366                 translation_table[mpc_record] = m; /* stash this for later */
367         if (m->trans_quad < MAX_NUMNODES && !node_online(m->trans_quad))
368                 node_set_online(m->trans_quad);
369 }
370
371 /*
372  * Read/parse the MPC oem tables
373  */
374
375 static void __init smp_read_mpc_oem(struct mp_config_oemtable *oemtable, \
376         unsigned short oemsize)
377 {
378         int count = sizeof (*oemtable); /* the header size */
379         unsigned char *oemptr = ((unsigned char *)oemtable)+count;
380         
381         mpc_record = 0;
382         printk(KERN_INFO "Found an OEM MPC table at %8p - parsing it ... \n", oemtable);
383         if (memcmp(oemtable->oem_signature,MPC_OEM_SIGNATURE,4))
384         {
385                 printk(KERN_WARNING "SMP mpc oemtable: bad signature [%c%c%c%c]!\n",
386                         oemtable->oem_signature[0],
387                         oemtable->oem_signature[1],
388                         oemtable->oem_signature[2],
389                         oemtable->oem_signature[3]);
390                 return;
391         }
392         if (mpf_checksum((unsigned char *)oemtable,oemtable->oem_length))
393         {
394                 printk(KERN_WARNING "SMP oem mptable: checksum error!\n");
395                 return;
396         }
397         while (count < oemtable->oem_length) {
398                 switch (*oemptr) {
399                         case MP_TRANSLATION:
400                         {
401                                 struct mpc_config_translation *m=
402                                         (struct mpc_config_translation *)oemptr;
403                                 MP_translation_info(m);
404                                 oemptr += sizeof(*m);
405                                 count += sizeof(*m);
406                                 ++mpc_record;
407                                 break;
408                         }
409                         default:
410                         {
411                                 printk(KERN_WARNING "Unrecognised OEM table entry type! - %d\n", (int) *oemptr);
412                                 return;
413                         }
414                 }
415        }
416 }
417
418 static inline void mps_oem_check(struct mp_config_table *mpc, char *oem,
419                 char *productid)
420 {
421         if (strncmp(oem, "IBM NUMA", 8))
422                 printk("Warning!  May not be a NUMA-Q system!\n");
423         if (mpc->mpc_oemptr)
424                 smp_read_mpc_oem((struct mp_config_oemtable *) mpc->mpc_oemptr,
425                                 mpc->mpc_oemsize);
426 }
427 #endif  /* CONFIG_X86_NUMAQ */
428
429 /*
430  * Read/parse the MPC
431  */
432
433 static int __init smp_read_mpc(struct mp_config_table *mpc)
434 {
435         char str[16];
436         char oem[10];
437         int count=sizeof(*mpc);
438         unsigned char *mpt=((unsigned char *)mpc)+count;
439
440         if (memcmp(mpc->mpc_signature,MPC_SIGNATURE,4)) {
441                 printk(KERN_ERR "SMP mptable: bad signature [0x%x]!\n",
442                         *(u32 *)mpc->mpc_signature);
443                 return 0;
444         }
445         if (mpf_checksum((unsigned char *)mpc,mpc->mpc_length)) {
446                 printk(KERN_ERR "SMP mptable: checksum error!\n");
447                 return 0;
448         }
449         if (mpc->mpc_spec!=0x01 && mpc->mpc_spec!=0x04) {
450                 printk(KERN_ERR "SMP mptable: bad table version (%d)!!\n",
451                         mpc->mpc_spec);
452                 return 0;
453         }
454         if (!mpc->mpc_lapic) {
455                 printk(KERN_ERR "SMP mptable: null local APIC address!\n");
456                 return 0;
457         }
458         memcpy(oem,mpc->mpc_oem,8);
459         oem[8]=0;
460         printk(KERN_INFO "OEM ID: %s ",oem);
461
462         memcpy(str,mpc->mpc_productid,12);
463         str[12]=0;
464         printk("Product ID: %s ",str);
465
466         mps_oem_check(mpc, oem, str);
467
468         printk("APIC at: 0x%X\n", mpc->mpc_lapic);
469
470         /*
471          * Save the local APIC address (it might be non-default) -- but only
472          * if we're not using ACPI.
473          */
474         if (!acpi_lapic)
475                 mp_lapic_addr = mpc->mpc_lapic;
476
477         /*
478          *      Now process the configuration blocks.
479          */
480 #ifdef CONFIG_X86_NUMAQ
481         mpc_record = 0;
482 #endif
483         while (count < mpc->mpc_length) {
484                 switch(*mpt) {
485                         case MP_PROCESSOR:
486                         {
487                                 struct mpc_config_processor *m=
488                                         (struct mpc_config_processor *)mpt;
489                                 /* ACPI may have already provided this data */
490                                 if (!acpi_lapic)
491                                         MP_processor_info(m);
492                                 mpt += sizeof(*m);
493                                 count += sizeof(*m);
494                                 break;
495                         }
496                         case MP_BUS:
497                         {
498                                 struct mpc_config_bus *m=
499                                         (struct mpc_config_bus *)mpt;
500                                 MP_bus_info(m);
501                                 mpt += sizeof(*m);
502                                 count += sizeof(*m);
503                                 break;
504                         }
505                         case MP_IOAPIC:
506                         {
507                                 struct mpc_config_ioapic *m=
508                                         (struct mpc_config_ioapic *)mpt;
509                                 MP_ioapic_info(m);
510                                 mpt+=sizeof(*m);
511                                 count+=sizeof(*m);
512                                 break;
513                         }
514                         case MP_INTSRC:
515                         {
516                                 struct mpc_config_intsrc *m=
517                                         (struct mpc_config_intsrc *)mpt;
518
519                                 MP_intsrc_info(m);
520                                 mpt+=sizeof(*m);
521                                 count+=sizeof(*m);
522                                 break;
523                         }
524                         case MP_LINTSRC:
525                         {
526                                 struct mpc_config_lintsrc *m=
527                                         (struct mpc_config_lintsrc *)mpt;
528                                 MP_lintsrc_info(m);
529                                 mpt+=sizeof(*m);
530                                 count+=sizeof(*m);
531                                 break;
532                         }
533                         default:
534                         {
535                                 count = mpc->mpc_length;
536                                 break;
537                         }
538                 }
539 #ifdef CONFIG_X86_NUMAQ
540                 ++mpc_record;
541 #endif
542         }
543         setup_apic_routing();
544         if (!num_processors)
545                 printk(KERN_ERR "SMP mptable: no processors registered!\n");
546         return num_processors;
547 }
548
549 static int __init ELCR_trigger(unsigned int irq)
550 {
551         unsigned int port;
552
553         port = 0x4d0 + (irq >> 3);
554         return (inb(port) >> (irq & 7)) & 1;
555 }
556
557 static void __init construct_default_ioirq_mptable(int mpc_default_type)
558 {
559         struct mpc_config_intsrc intsrc;
560         int i;
561         int ELCR_fallback = 0;
562
563         intsrc.mpc_type = MP_INTSRC;
564         intsrc.mpc_irqflag = 0;                 /* conforming */
565         intsrc.mpc_srcbus = 0;
566         intsrc.mpc_dstapic = mp_ioapics[0].mpc_apicid;
567
568         intsrc.mpc_irqtype = mp_INT;
569
570         /*
571          *  If true, we have an ISA/PCI system with no IRQ entries
572          *  in the MP table. To prevent the PCI interrupts from being set up
573          *  incorrectly, we try to use the ELCR. The sanity check to see if
574          *  there is good ELCR data is very simple - IRQ0, 1, 2 and 13 can
575          *  never be level sensitive, so we simply see if the ELCR agrees.
576          *  If it does, we assume it's valid.
577          */
578         if (mpc_default_type == 5) {
579                 printk(KERN_INFO "ISA/PCI bus type with no IRQ information... falling back to ELCR\n");
580
581                 if (ELCR_trigger(0) || ELCR_trigger(1) || ELCR_trigger(2) || ELCR_trigger(13))
582                         printk(KERN_WARNING "ELCR contains invalid data... not using ELCR\n");
583                 else {
584                         printk(KERN_INFO "Using ELCR to identify PCI interrupts\n");
585                         ELCR_fallback = 1;
586                 }
587         }
588
589         for (i = 0; i < 16; i++) {
590                 switch (mpc_default_type) {
591                 case 2:
592                         if (i == 0 || i == 13)
593                                 continue;       /* IRQ0 & IRQ13 not connected */
594                         /* fall through */
595                 default:
596                         if (i == 2)
597                                 continue;       /* IRQ2 is never connected */
598                 }
599
600                 if (ELCR_fallback) {
601                         /*
602                          *  If the ELCR indicates a level-sensitive interrupt, we
603                          *  copy that information over to the MP table in the
604                          *  irqflag field (level sensitive, active high polarity).
605                          */
606                         if (ELCR_trigger(i))
607                                 intsrc.mpc_irqflag = 13;
608                         else
609                                 intsrc.mpc_irqflag = 0;
610                 }
611
612                 intsrc.mpc_srcbusirq = i;
613                 intsrc.mpc_dstirq = i ? i : 2;          /* IRQ0 to INTIN2 */
614                 MP_intsrc_info(&intsrc);
615         }
616
617         intsrc.mpc_irqtype = mp_ExtINT;
618         intsrc.mpc_srcbusirq = 0;
619         intsrc.mpc_dstirq = 0;                          /* 8259A to INTIN0 */
620         MP_intsrc_info(&intsrc);
621 }
622
623 static inline void __init construct_default_ISA_mptable(int mpc_default_type)
624 {
625         struct mpc_config_processor processor;
626         struct mpc_config_bus bus;
627         struct mpc_config_ioapic ioapic;
628         struct mpc_config_lintsrc lintsrc;
629         int linttypes[2] = { mp_ExtINT, mp_NMI };
630         int i;
631
632         /*
633          * local APIC has default address
634          */
635         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
636
637         /*
638          * 2 CPUs, numbered 0 & 1.
639          */
640         processor.mpc_type = MP_PROCESSOR;
641         /* Either an integrated APIC or a discrete 82489DX. */
642         processor.mpc_apicver = mpc_default_type > 4 ? 0x10 : 0x01;
643         processor.mpc_cpuflag = CPU_ENABLED;
644         processor.mpc_cpufeature = (boot_cpu_data.x86 << 8) |
645                                    (boot_cpu_data.x86_model << 4) |
646                                    boot_cpu_data.x86_mask;
647         processor.mpc_featureflag = boot_cpu_data.x86_capability[0];
648         processor.mpc_reserved[0] = 0;
649         processor.mpc_reserved[1] = 0;
650         for (i = 0; i < 2; i++) {
651                 processor.mpc_apicid = i;
652                 MP_processor_info(&processor);
653         }
654
655         bus.mpc_type = MP_BUS;
656         bus.mpc_busid = 0;
657         switch (mpc_default_type) {
658                 default:
659                         printk("???\n");
660                         printk(KERN_ERR "Unknown standard configuration %d\n",
661                                 mpc_default_type);
662                         /* fall through */
663                 case 1:
664                 case 5:
665                         memcpy(bus.mpc_bustype, "ISA   ", 6);
666                         break;
667                 case 2:
668                 case 6:
669                 case 3:
670                         memcpy(bus.mpc_bustype, "EISA  ", 6);
671                         break;
672                 case 4:
673                 case 7:
674                         memcpy(bus.mpc_bustype, "MCA   ", 6);
675         }
676         MP_bus_info(&bus);
677         if (mpc_default_type > 4) {
678                 bus.mpc_busid = 1;
679                 memcpy(bus.mpc_bustype, "PCI   ", 6);
680                 MP_bus_info(&bus);
681         }
682
683         ioapic.mpc_type = MP_IOAPIC;
684         ioapic.mpc_apicid = 2;
685         ioapic.mpc_apicver = mpc_default_type > 4 ? 0x10 : 0x01;
686         ioapic.mpc_flags = MPC_APIC_USABLE;
687         ioapic.mpc_apicaddr = 0xFEC00000;
688         MP_ioapic_info(&ioapic);
689
690         /*
691          * We set up most of the low 16 IO-APIC pins according to MPS rules.
692          */
693         construct_default_ioirq_mptable(mpc_default_type);
694
695         lintsrc.mpc_type = MP_LINTSRC;
696         lintsrc.mpc_irqflag = 0;                /* conforming */
697         lintsrc.mpc_srcbusid = 0;
698         lintsrc.mpc_srcbusirq = 0;
699         lintsrc.mpc_destapic = MP_APIC_ALL;
700         for (i = 0; i < 2; i++) {
701                 lintsrc.mpc_irqtype = linttypes[i];
702                 lintsrc.mpc_destapiclint = i;
703                 MP_lintsrc_info(&lintsrc);
704         }
705 }
706
707 static struct intel_mp_floating *mpf_found;
708
709 /*
710  * Scan the memory blocks for an SMP configuration block.
711  */
712 void __init get_smp_config (void)
713 {
714         struct intel_mp_floating *mpf = mpf_found;
715
716         /*
717          * ACPI supports both logical (e.g. Hyper-Threading) and physical 
718          * processors, where MPS only supports physical.
719          */
720         if (acpi_lapic && acpi_ioapic) {
721                 printk(KERN_INFO "Using ACPI (MADT) for SMP configuration information\n");
722                 return;
723         }
724         else if (acpi_lapic)
725                 printk(KERN_INFO "Using ACPI for processor (LAPIC) configuration information\n");
726
727         printk(KERN_INFO "Intel MultiProcessor Specification v1.%d\n", mpf->mpf_specification);
728         if (mpf->mpf_feature2 & (1<<7)) {
729                 printk(KERN_INFO "    IMCR and PIC compatibility mode.\n");
730                 pic_mode = 1;
731         } else {
732                 printk(KERN_INFO "    Virtual Wire compatibility mode.\n");
733                 pic_mode = 0;
734         }
735
736         /*
737          * Now see if we need to read further.
738          */
739         if (mpf->mpf_feature1 != 0) {
740
741                 printk(KERN_INFO "Default MP configuration #%d\n", mpf->mpf_feature1);
742                 construct_default_ISA_mptable(mpf->mpf_feature1);
743
744         } else if (mpf->mpf_physptr) {
745
746                 /*
747                  * Read the physical hardware table.  Anything here will
748                  * override the defaults.
749                  */
750                 if (!smp_read_mpc(phys_to_virt(mpf->mpf_physptr))) {
751                         smp_found_config = 0;
752                         printk(KERN_ERR "BIOS bug, MP table errors detected!...\n");
753                         printk(KERN_ERR "... disabling SMP support. (tell your hw vendor)\n");
754                         return;
755                 }
756                 /*
757                  * If there are no explicit MP IRQ entries, then we are
758                  * broken.  We set up most of the low 16 IO-APIC pins to
759                  * ISA defaults and hope it will work.
760                  */
761                 if (!mp_irq_entries) {
762                         struct mpc_config_bus bus;
763
764                         printk(KERN_ERR "BIOS bug, no explicit IRQ entries, using default mptable. (tell your hw vendor)\n");
765
766                         bus.mpc_type = MP_BUS;
767                         bus.mpc_busid = 0;
768                         memcpy(bus.mpc_bustype, "ISA   ", 6);
769                         MP_bus_info(&bus);
770
771                         construct_default_ioirq_mptable(0);
772                 }
773
774         } else
775                 BUG();
776
777         printk(KERN_INFO "Processors: %d\n", num_processors);
778         /*
779          * Only use the first configuration found.
780          */
781 }
782
783 static int __init smp_scan_config (unsigned long base, unsigned long length)
784 {
785         unsigned long *bp = phys_to_virt(base);
786         struct intel_mp_floating *mpf;
787
788         printk(KERN_INFO "Scan SMP from %p for %ld bytes.\n", bp,length);
789         if (sizeof(*mpf) != 16)
790                 printk("Error: MPF size\n");
791
792         while (length > 0) {
793                 mpf = (struct intel_mp_floating *)bp;
794                 if ((*bp == SMP_MAGIC_IDENT) &&
795                         (mpf->mpf_length == 1) &&
796                         !mpf_checksum((unsigned char *)bp, 16) &&
797                         ((mpf->mpf_specification == 1)
798                                 || (mpf->mpf_specification == 4)) ) {
799
800                         smp_found_config = 1;
801                         printk(KERN_INFO "found SMP MP-table at [%p] %08lx\n",
802                                 mpf, virt_to_phys(mpf));
803                         reserve_bootmem(virt_to_phys(mpf), PAGE_SIZE,
804                                         BOOTMEM_DEFAULT);
805                         if (mpf->mpf_physptr) {
806                                 /*
807                                  * We cannot access to MPC table to compute
808                                  * table size yet, as only few megabytes from
809                                  * the bottom is mapped now.
810                                  * PC-9800's MPC table places on the very last
811                                  * of physical memory; so that simply reserving
812                                  * PAGE_SIZE from mpg->mpf_physptr yields BUG()
813                                  * in reserve_bootmem.
814                                  */
815                                 unsigned long size = PAGE_SIZE;
816                                 unsigned long end = max_low_pfn * PAGE_SIZE;
817                                 if (mpf->mpf_physptr + size > end)
818                                         size = end - mpf->mpf_physptr;
819                                 reserve_bootmem(mpf->mpf_physptr, size,
820                                                 BOOTMEM_DEFAULT);
821                         }
822
823                         mpf_found = mpf;
824                         return 1;
825                 }
826                 bp += 4;
827                 length -= 16;
828         }
829         return 0;
830 }
831
832 void __init find_smp_config (void)
833 {
834         unsigned int address;
835
836         /*
837          * FIXME: Linux assumes you have 640K of base ram..
838          * this continues the error...
839          *
840          * 1) Scan the bottom 1K for a signature
841          * 2) Scan the top 1K of base RAM
842          * 3) Scan the 64K of bios
843          */
844         if (smp_scan_config(0x0,0x400) ||
845                 smp_scan_config(639*0x400,0x400) ||
846                         smp_scan_config(0xF0000,0x10000))
847                 return;
848         /*
849          * If it is an SMP machine we should know now, unless the
850          * configuration is in an EISA/MCA bus machine with an
851          * extended bios data area.
852          *
853          * there is a real-mode segmented pointer pointing to the
854          * 4K EBDA area at 0x40E, calculate and scan it here.
855          *
856          * NOTE! There are Linux loaders that will corrupt the EBDA
857          * area, and as such this kind of SMP config may be less
858          * trustworthy, simply because the SMP table may have been
859          * stomped on during early boot. These loaders are buggy and
860          * should be fixed.
861          *
862          * MP1.4 SPEC states to only scan first 1K of 4K EBDA.
863          */
864
865         address = get_bios_ebda();
866         if (address)
867                 smp_scan_config(address, 0x400);
868 }
869
870 /* --------------------------------------------------------------------------
871                             ACPI-based MP Configuration
872    -------------------------------------------------------------------------- */
873
874 #ifdef CONFIG_ACPI
875
876 void __init mp_register_lapic_address(u64 address)
877 {
878         mp_lapic_addr = (unsigned long) address;
879
880         set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
881
882         if (boot_cpu_physical_apicid == -1U)
883                 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
884
885         Dprintk("Boot CPU = %d\n", boot_cpu_physical_apicid);
886 }
887
888 void __cpuinit mp_register_lapic (u8 id, u8 enabled)
889 {
890         struct mpc_config_processor processor;
891         int boot_cpu = 0;
892         
893         if (MAX_APICS - id <= 0) {
894                 printk(KERN_WARNING "Processor #%d invalid (max %d)\n",
895                         id, MAX_APICS);
896                 return;
897         }
898
899         if (id == boot_cpu_physical_apicid)
900                 boot_cpu = 1;
901
902         processor.mpc_type = MP_PROCESSOR;
903         processor.mpc_apicid = id;
904         processor.mpc_apicver = GET_APIC_VERSION(apic_read(APIC_LVR));
905         processor.mpc_cpuflag = (enabled ? CPU_ENABLED : 0);
906         processor.mpc_cpuflag |= (boot_cpu ? CPU_BOOTPROCESSOR : 0);
907         processor.mpc_cpufeature = (boot_cpu_data.x86 << 8) | 
908                 (boot_cpu_data.x86_model << 4) | boot_cpu_data.x86_mask;
909         processor.mpc_featureflag = boot_cpu_data.x86_capability[0];
910         processor.mpc_reserved[0] = 0;
911         processor.mpc_reserved[1] = 0;
912
913         MP_processor_info(&processor);
914 }
915
916 #ifdef  CONFIG_X86_IO_APIC
917
918 #define MP_ISA_BUS              0
919 #define MP_MAX_IOAPIC_PIN       127
920
921 static struct mp_ioapic_routing {
922         int                     apic_id;
923         int                     gsi_base;
924         int                     gsi_end;
925         u32                     pin_programmed[4];
926 } mp_ioapic_routing[MAX_IO_APICS];
927
928 static int mp_find_ioapic (int gsi)
929 {
930         int i = 0;
931
932         /* Find the IOAPIC that manages this GSI. */
933         for (i = 0; i < nr_ioapics; i++) {
934                 if ((gsi >= mp_ioapic_routing[i].gsi_base)
935                         && (gsi <= mp_ioapic_routing[i].gsi_end))
936                         return i;
937         }
938
939         printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
940
941         return -1;
942 }
943
944 static u8 uniq_ioapic_id(u8 id)
945 {
946         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
947             !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
948                 return io_apic_get_unique_id(nr_ioapics, id);
949         else
950                 return id;
951 }
952
953 void __init mp_register_ioapic(u8 id, u32 address, u32 gsi_base)
954 {
955         int idx = 0;
956
957         if (bad_ioapic(address))
958                 return;
959
960         idx = nr_ioapics;
961
962         mp_ioapics[idx].mpc_type = MP_IOAPIC;
963         mp_ioapics[idx].mpc_flags = MPC_APIC_USABLE;
964         mp_ioapics[idx].mpc_apicaddr = address;
965
966         set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
967         mp_ioapics[idx].mpc_apicid = uniq_ioapic_id(id);
968         mp_ioapics[idx].mpc_apicver = io_apic_get_version(idx);
969         
970         /* 
971          * Build basic GSI lookup table to facilitate gsi->io_apic lookups
972          * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
973          */
974         mp_ioapic_routing[idx].apic_id = mp_ioapics[idx].mpc_apicid;
975         mp_ioapic_routing[idx].gsi_base = gsi_base;
976         mp_ioapic_routing[idx].gsi_end = gsi_base +
977                 io_apic_get_redir_entries(idx);
978
979         printk("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
980                "GSI %d-%d\n", idx, mp_ioapics[idx].mpc_apicid,
981                mp_ioapics[idx].mpc_apicver,
982                mp_ioapics[idx].mpc_apicaddr,
983                mp_ioapic_routing[idx].gsi_base,
984                mp_ioapic_routing[idx].gsi_end);
985
986         nr_ioapics++;
987 }
988
989 void __init
990 mp_override_legacy_irq(u8 bus_irq, u8 polarity, u8 trigger, u32 gsi)
991 {
992         struct mpc_config_intsrc intsrc;
993         int                     ioapic = -1;
994         int                     pin = -1;
995
996         /* 
997          * Convert 'gsi' to 'ioapic.pin'.
998          */
999         ioapic = mp_find_ioapic(gsi);
1000         if (ioapic < 0)
1001                 return;
1002         pin = gsi - mp_ioapic_routing[ioapic].gsi_base;
1003
1004         /*
1005          * TBD: This check is for faulty timer entries, where the override
1006          *      erroneously sets the trigger to level, resulting in a HUGE 
1007          *      increase of timer interrupts!
1008          */
1009         if ((bus_irq == 0) && (trigger == 3))
1010                 trigger = 1;
1011
1012         intsrc.mpc_type = MP_INTSRC;
1013         intsrc.mpc_irqtype = mp_INT;
1014         intsrc.mpc_irqflag = (trigger << 2) | polarity;
1015         intsrc.mpc_srcbus = MP_ISA_BUS;
1016         intsrc.mpc_srcbusirq = bus_irq;                                /* IRQ */
1017         intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;        /* APIC ID */
1018         intsrc.mpc_dstirq = pin;                                    /* INTIN# */
1019
1020         Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, %d-%d\n",
1021                 intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3, 
1022                 (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus, 
1023                 intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, intsrc.mpc_dstirq);
1024
1025         mp_irqs[mp_irq_entries] = intsrc;
1026         if (++mp_irq_entries == MAX_IRQ_SOURCES)
1027                 panic("Max # of irq sources exceeded!\n");
1028 }
1029
1030 int es7000_plat;
1031
1032 void __init mp_config_acpi_legacy_irqs (void)
1033 {
1034         struct mpc_config_intsrc intsrc;
1035         int i = 0;
1036         int ioapic = -1;
1037
1038 #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
1039         /* 
1040          * Fabricate the legacy ISA bus (bus #31).
1041          */
1042         mp_bus_id_to_type[MP_ISA_BUS] = MP_BUS_ISA;
1043 #endif
1044         set_bit(MP_ISA_BUS, mp_bus_not_pci);
1045         Dprintk("Bus #%d is ISA\n", MP_ISA_BUS);
1046
1047         /*
1048          * Older generations of ES7000 have no legacy identity mappings
1049          */
1050         if (es7000_plat == 1)
1051                 return;
1052
1053         /* 
1054          * Locate the IOAPIC that manages the ISA IRQs (0-15). 
1055          */
1056         ioapic = mp_find_ioapic(0);
1057         if (ioapic < 0)
1058                 return;
1059
1060         intsrc.mpc_type = MP_INTSRC;
1061         intsrc.mpc_irqflag = 0;                                 /* Conforming */
1062         intsrc.mpc_srcbus = MP_ISA_BUS;
1063         intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;
1064
1065         /* 
1066          * Use the default configuration for the IRQs 0-15.  Unless
1067          * overridden by (MADT) interrupt source override entries.
1068          */
1069         for (i = 0; i < 16; i++) {
1070                 int idx;
1071
1072                 for (idx = 0; idx < mp_irq_entries; idx++) {
1073                         struct mpc_config_intsrc *irq = mp_irqs + idx;
1074
1075                         /* Do we already have a mapping for this ISA IRQ? */
1076                         if (irq->mpc_srcbus == MP_ISA_BUS && irq->mpc_srcbusirq == i)
1077                                 break;
1078
1079                         /* Do we already have a mapping for this IOAPIC pin */
1080                         if ((irq->mpc_dstapic == intsrc.mpc_dstapic) &&
1081                                 (irq->mpc_dstirq == i))
1082                                 break;
1083                 }
1084
1085                 if (idx != mp_irq_entries) {
1086                         printk(KERN_DEBUG "ACPI: IRQ%d used by override.\n", i);
1087                         continue;                       /* IRQ already used */
1088                 }
1089
1090                 intsrc.mpc_irqtype = mp_INT;
1091                 intsrc.mpc_srcbusirq = i;                  /* Identity mapped */
1092                 intsrc.mpc_dstirq = i;
1093
1094                 Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, "
1095                         "%d-%d\n", intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3, 
1096                         (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus, 
1097                         intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, 
1098                         intsrc.mpc_dstirq);
1099
1100                 mp_irqs[mp_irq_entries] = intsrc;
1101                 if (++mp_irq_entries == MAX_IRQ_SOURCES)
1102                         panic("Max # of irq sources exceeded!\n");
1103         }
1104 }
1105
1106 #define MAX_GSI_NUM     4096
1107 #define IRQ_COMPRESSION_START   64
1108
1109 int mp_register_gsi(u32 gsi, int triggering, int polarity)
1110 {
1111         int ioapic = -1;
1112         int ioapic_pin = 0;
1113         int idx, bit = 0;
1114         static int pci_irq = IRQ_COMPRESSION_START;
1115         /*
1116          * Mapping between Global System Interrupts, which
1117          * represent all possible interrupts, and IRQs
1118          * assigned to actual devices.
1119          */
1120         static int              gsi_to_irq[MAX_GSI_NUM];
1121
1122         /* Don't set up the ACPI SCI because it's already set up */
1123         if (acpi_gbl_FADT.sci_interrupt == gsi)
1124                 return gsi;
1125
1126         ioapic = mp_find_ioapic(gsi);
1127         if (ioapic < 0) {
1128                 printk(KERN_WARNING "No IOAPIC for GSI %u\n", gsi);
1129                 return gsi;
1130         }
1131
1132         ioapic_pin = gsi - mp_ioapic_routing[ioapic].gsi_base;
1133
1134         if (ioapic_renumber_irq)
1135                 gsi = ioapic_renumber_irq(ioapic, gsi);
1136
1137         /* 
1138          * Avoid pin reprogramming.  PRTs typically include entries  
1139          * with redundant pin->gsi mappings (but unique PCI devices);
1140          * we only program the IOAPIC on the first.
1141          */
1142         bit = ioapic_pin % 32;
1143         idx = (ioapic_pin < 32) ? 0 : (ioapic_pin / 32);
1144         if (idx > 3) {
1145                 printk(KERN_ERR "Invalid reference to IOAPIC pin "
1146                         "%d-%d\n", mp_ioapic_routing[ioapic].apic_id, 
1147                         ioapic_pin);
1148                 return gsi;
1149         }
1150         if ((1<<bit) & mp_ioapic_routing[ioapic].pin_programmed[idx]) {
1151                 Dprintk(KERN_DEBUG "Pin %d-%d already programmed\n",
1152                         mp_ioapic_routing[ioapic].apic_id, ioapic_pin);
1153                 return (gsi < IRQ_COMPRESSION_START ? gsi : gsi_to_irq[gsi]);
1154         }
1155
1156         mp_ioapic_routing[ioapic].pin_programmed[idx] |= (1<<bit);
1157
1158         /*
1159          * For GSI >= 64, use IRQ compression
1160          */
1161         if ((gsi >= IRQ_COMPRESSION_START)
1162                 && (triggering == ACPI_LEVEL_SENSITIVE)) {
1163                 /*
1164                  * For PCI devices assign IRQs in order, avoiding gaps
1165                  * due to unused I/O APIC pins.
1166                  */
1167                 int irq = gsi;
1168                 if (gsi < MAX_GSI_NUM) {
1169                         /*
1170                          * Retain the VIA chipset work-around (gsi > 15), but
1171                          * avoid a problem where the 8254 timer (IRQ0) is setup
1172                          * via an override (so it's not on pin 0 of the ioapic),
1173                          * and at the same time, the pin 0 interrupt is a PCI
1174                          * type.  The gsi > 15 test could cause these two pins
1175                          * to be shared as IRQ0, and they are not shareable.
1176                          * So test for this condition, and if necessary, avoid
1177                          * the pin collision.
1178                          */
1179                         gsi = pci_irq++;
1180                         /*
1181                          * Don't assign IRQ used by ACPI SCI
1182                          */
1183                         if (gsi == acpi_gbl_FADT.sci_interrupt)
1184                                 gsi = pci_irq++;
1185                         gsi_to_irq[irq] = gsi;
1186                 } else {
1187                         printk(KERN_ERR "GSI %u is too high\n", gsi);
1188                         return gsi;
1189                 }
1190         }
1191
1192         io_apic_set_pci_routing(ioapic, ioapic_pin, gsi,
1193                     triggering == ACPI_EDGE_SENSITIVE ? 0 : 1,
1194                     polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
1195         return gsi;
1196 }
1197
1198 #endif /* CONFIG_X86_IO_APIC */
1199 #endif /* CONFIG_ACPI */