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