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