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