]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/mpparse_64.c
x86: move mp_irqs to io_apic_64.c
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / mpparse_64.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/kernel_stat.h>
21 #include <linux/mc146818rtc.h>
22 #include <linux/acpi.h>
23 #include <linux/module.h>
24
25 #include <asm/smp.h>
26 #include <asm/mtrr.h>
27 #include <asm/mpspec.h>
28 #include <asm/pgalloc.h>
29 #include <asm/io_apic.h>
30 #include <asm/proto.h>
31 #include <asm/acpi.h>
32 #include <asm/bios_ebda.h>
33
34 #include <mach_apic.h>
35
36 /* Have we found an MP table */
37 int smp_found_config;
38
39 /*
40  * Various Linux-internal data structures created from the
41  * MP-table.
42  */
43 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
44 int mp_bus_id_to_pci_bus[MAX_MP_BUSSES] = {[0 ... MAX_MP_BUSSES - 1] = -1 };
45
46 static int mp_current_pci_id = 0;
47
48 /* Make it easy to share the UP and SMP code: */
49 #ifndef CONFIG_X86_SMP
50 unsigned int num_processors;
51 unsigned disabled_cpus __cpuinitdata;
52 #ifndef CONFIG_X86_LOCAL_APIC
53 unsigned int boot_cpu_physical_apicid = -1U;
54 #endif
55 #endif
56
57 /*
58  * Intel MP BIOS table parsing routines:
59  */
60
61 /*
62  * Checksum an MP configuration block.
63  */
64
65 static int __init mpf_checksum(unsigned char *mp, int len)
66 {
67         int sum = 0;
68
69         while (len--)
70                 sum += *mp++;
71
72         return sum & 0xFF;
73 }
74
75 static void __cpuinit MP_processor_info(struct mpc_config_processor *m)
76 {
77         char *bootup_cpu = "";
78
79         if (!(m->mpc_cpuflag & CPU_ENABLED)) {
80                 disabled_cpus++;
81                 return;
82         }
83         if (m->mpc_cpuflag & CPU_BOOTPROCESSOR) {
84                 bootup_cpu = " (Bootup-CPU)";
85                 boot_cpu_physical_apicid = m->mpc_apicid;
86         }
87
88         printk(KERN_INFO "Processor #%d%s\n", m->mpc_apicid, bootup_cpu);
89         generic_processor_info(m->mpc_apicid, 0);
90 }
91
92 static void __init MP_bus_info(struct mpc_config_bus *m)
93 {
94         char str[7];
95
96         memcpy(str, m->mpc_bustype, 6);
97         str[6] = 0;
98         Dprintk("Bus #%d is %s\n", m->mpc_busid, str);
99
100         if (strncmp(str, "ISA", 3) == 0) {
101                 set_bit(m->mpc_busid, mp_bus_not_pci);
102         } else if (strncmp(str, "PCI", 3) == 0) {
103                 clear_bit(m->mpc_busid, mp_bus_not_pci);
104                 mp_bus_id_to_pci_bus[m->mpc_busid] = mp_current_pci_id;
105                 mp_current_pci_id++;
106         } else {
107                 printk(KERN_ERR "Unknown bustype %s\n", str);
108         }
109 }
110
111 static int bad_ioapic(unsigned long address)
112 {
113         if (nr_ioapics >= MAX_IO_APICS) {
114                 printk(KERN_ERR "ERROR: Max # of I/O APICs (%d) exceeded "
115                        "(found %d)\n", MAX_IO_APICS, nr_ioapics);
116                 panic("Recompile kernel with bigger MAX_IO_APICS!\n");
117         }
118         if (!address) {
119                 printk(KERN_ERR "WARNING: Bogus (zero) I/O APIC address"
120                        " found in table, skipping!\n");
121                 return 1;
122         }
123         return 0;
124 }
125
126 static void __init MP_ioapic_info(struct mpc_config_ioapic *m)
127 {
128         if (!(m->mpc_flags & MPC_APIC_USABLE))
129                 return;
130
131         printk(KERN_INFO "I/O APIC #%d at 0x%X.\n", m->mpc_apicid,
132                m->mpc_apicaddr);
133
134         if (bad_ioapic(m->mpc_apicaddr))
135                 return;
136
137         mp_ioapics[nr_ioapics] = *m;
138         nr_ioapics++;
139 }
140
141 static void __init MP_intsrc_info(struct mpc_config_intsrc *m)
142 {
143         mp_irqs[mp_irq_entries] = *m;
144         Dprintk("Int: type %d, pol %d, trig %d, bus %d,"
145                 " IRQ %02x, APIC ID %x, APIC INT %02x\n",
146                 m->mpc_irqtype, m->mpc_irqflag & 3,
147                 (m->mpc_irqflag >> 2) & 3, m->mpc_srcbus,
148                 m->mpc_srcbusirq, m->mpc_dstapic, m->mpc_dstirq);
149         if (++mp_irq_entries >= MAX_IRQ_SOURCES)
150                 panic("Max # of irq sources exceeded!!\n");
151 }
152
153 static void __init MP_lintsrc_info(struct mpc_config_lintsrc *m)
154 {
155         Dprintk("Lint: type %d, pol %d, trig %d, bus %d,"
156                 " IRQ %02x, APIC ID %x, APIC LINT %02x\n",
157                 m->mpc_irqtype, m->mpc_irqflag & 3,
158                 (m->mpc_irqflag >> 2) & 3, m->mpc_srcbusid,
159                 m->mpc_srcbusirq, m->mpc_destapic, m->mpc_destapiclint);
160 }
161
162 /*
163  * Read/parse the MPC
164  */
165 static int __init smp_read_mpc(struct mp_config_table *mpc, unsigned early)
166 {
167         char str[16];
168         int count = sizeof(*mpc);
169         unsigned char *mpt = ((unsigned char *)mpc) + count;
170
171         if (memcmp(mpc->mpc_signature, MPC_SIGNATURE, 4)) {
172                 printk(KERN_ERR "MPTABLE: bad signature [%c%c%c%c]!\n",
173                        mpc->mpc_signature[0],
174                        mpc->mpc_signature[1],
175                        mpc->mpc_signature[2], mpc->mpc_signature[3]);
176                 return 0;
177         }
178         if (mpf_checksum((unsigned char *)mpc, mpc->mpc_length)) {
179                 printk(KERN_ERR "MPTABLE: checksum error!\n");
180                 return 0;
181         }
182         if (mpc->mpc_spec != 0x01 && mpc->mpc_spec != 0x04) {
183                 printk(KERN_ERR "MPTABLE: bad table version (%d)!!\n",
184                        mpc->mpc_spec);
185                 return 0;
186         }
187         if (!mpc->mpc_lapic) {
188                 printk(KERN_ERR "MPTABLE: null local APIC address!\n");
189                 return 0;
190         }
191         memcpy(str, mpc->mpc_oem, 8);
192         str[8] = 0;
193         printk(KERN_INFO "MPTABLE: OEM ID: %s ", str);
194
195         memcpy(str, mpc->mpc_productid, 12);
196         str[12] = 0;
197         printk(KERN_INFO "MPTABLE: Product ID: %s ", str);
198
199         printk(KERN_INFO "MPTABLE: APIC at: 0x%X\n", mpc->mpc_lapic);
200
201         /* save the local APIC address, it might be non-default */
202         if (!acpi_lapic)
203                 mp_lapic_addr = mpc->mpc_lapic;
204
205         if (early)
206                 return 1;
207
208         /*
209          *      Now process the configuration blocks.
210          */
211         while (count < mpc->mpc_length) {
212                 switch (*mpt) {
213                 case MP_PROCESSOR:
214                         {
215                                 struct mpc_config_processor *m =
216                                     (struct mpc_config_processor *)mpt;
217                                 if (!acpi_lapic)
218                                         MP_processor_info(m);
219                                 mpt += sizeof(*m);
220                                 count += sizeof(*m);
221                                 break;
222                         }
223                 case MP_BUS:
224                         {
225                                 struct mpc_config_bus *m =
226                                     (struct mpc_config_bus *)mpt;
227                                 MP_bus_info(m);
228                                 mpt += sizeof(*m);
229                                 count += sizeof(*m);
230                                 break;
231                         }
232                 case MP_IOAPIC:
233                         {
234                                 struct mpc_config_ioapic *m =
235                                     (struct mpc_config_ioapic *)mpt;
236                                 MP_ioapic_info(m);
237                                 mpt += sizeof(*m);
238                                 count += sizeof(*m);
239                                 break;
240                         }
241                 case MP_INTSRC:
242                         {
243                                 struct mpc_config_intsrc *m =
244                                     (struct mpc_config_intsrc *)mpt;
245
246                                 MP_intsrc_info(m);
247                                 mpt += sizeof(*m);
248                                 count += sizeof(*m);
249                                 break;
250                         }
251                 case MP_LINTSRC:
252                         {
253                                 struct mpc_config_lintsrc *m =
254                                     (struct mpc_config_lintsrc *)mpt;
255                                 MP_lintsrc_info(m);
256                                 mpt += sizeof(*m);
257                                 count += sizeof(*m);
258                                 break;
259                         }
260                 }
261         }
262         setup_apic_routing();
263         if (!num_processors)
264                 printk(KERN_ERR "MPTABLE: no processors registered!\n");
265         return num_processors;
266 }
267
268 static int __init ELCR_trigger(unsigned int irq)
269 {
270         unsigned int port;
271
272         port = 0x4d0 + (irq >> 3);
273         return (inb(port) >> (irq & 7)) & 1;
274 }
275
276 static void __init construct_default_ioirq_mptable(int mpc_default_type)
277 {
278         struct mpc_config_intsrc intsrc;
279         int i;
280         int ELCR_fallback = 0;
281
282         intsrc.mpc_type = MP_INTSRC;
283         intsrc.mpc_irqflag = 0; /* conforming */
284         intsrc.mpc_srcbus = 0;
285         intsrc.mpc_dstapic = mp_ioapics[0].mpc_apicid;
286
287         intsrc.mpc_irqtype = mp_INT;
288
289         /*
290          *  If true, we have an ISA/PCI system with no IRQ entries
291          *  in the MP table. To prevent the PCI interrupts from being set up
292          *  incorrectly, we try to use the ELCR. The sanity check to see if
293          *  there is good ELCR data is very simple - IRQ0, 1, 2 and 13 can
294          *  never be level sensitive, so we simply see if the ELCR agrees.
295          *  If it does, we assume it's valid.
296          */
297         if (mpc_default_type == 5) {
298                 printk(KERN_INFO "ISA/PCI bus type with no IRQ information... "
299                        "falling back to ELCR\n");
300
301                 if (ELCR_trigger(0) || ELCR_trigger(1) || ELCR_trigger(2) ||
302                     ELCR_trigger(13))
303                         printk(KERN_ERR "ELCR contains invalid data... "
304                                "not using ELCR\n");
305                 else {
306                         printk(KERN_INFO
307                                "Using ELCR to identify PCI interrupts\n");
308                         ELCR_fallback = 1;
309                 }
310         }
311
312         for (i = 0; i < 16; i++) {
313                 switch (mpc_default_type) {
314                 case 2:
315                         if (i == 0 || i == 13)
316                                 continue;       /* IRQ0 & IRQ13 not connected */
317                         /* fall through */
318                 default:
319                         if (i == 2)
320                                 continue;       /* IRQ2 is never connected */
321                 }
322
323                 if (ELCR_fallback) {
324                         /*
325                          *  If the ELCR indicates a level-sensitive interrupt, we
326                          *  copy that information over to the MP table in the
327                          *  irqflag field (level sensitive, active high polarity).
328                          */
329                         if (ELCR_trigger(i))
330                                 intsrc.mpc_irqflag = 13;
331                         else
332                                 intsrc.mpc_irqflag = 0;
333                 }
334
335                 intsrc.mpc_srcbusirq = i;
336                 intsrc.mpc_dstirq = i ? i : 2;  /* IRQ0 to INTIN2 */
337                 MP_intsrc_info(&intsrc);
338         }
339
340         intsrc.mpc_irqtype = mp_ExtINT;
341         intsrc.mpc_srcbusirq = 0;
342         intsrc.mpc_dstirq = 0;  /* 8259A to INTIN0 */
343         MP_intsrc_info(&intsrc);
344 }
345
346 static inline void __init construct_default_ISA_mptable(int mpc_default_type)
347 {
348         struct mpc_config_processor processor;
349         struct mpc_config_bus bus;
350         struct mpc_config_ioapic ioapic;
351         struct mpc_config_lintsrc lintsrc;
352         int linttypes[2] = { mp_ExtINT, mp_NMI };
353         int i;
354
355         /*
356          * local APIC has default address
357          */
358         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
359
360         /*
361          * 2 CPUs, numbered 0 & 1.
362          */
363         processor.mpc_type = MP_PROCESSOR;
364         processor.mpc_apicver = 0;
365         processor.mpc_cpuflag = CPU_ENABLED;
366         processor.mpc_cpufeature = 0;
367         processor.mpc_featureflag = 0;
368         processor.mpc_reserved[0] = 0;
369         processor.mpc_reserved[1] = 0;
370         for (i = 0; i < 2; i++) {
371                 processor.mpc_apicid = i;
372                 MP_processor_info(&processor);
373         }
374
375         bus.mpc_type = MP_BUS;
376         bus.mpc_busid = 0;
377         switch (mpc_default_type) {
378         default:
379                 printk(KERN_ERR "???\nUnknown standard configuration %d\n",
380                        mpc_default_type);
381                 /* fall through */
382         case 1:
383         case 5:
384                 memcpy(bus.mpc_bustype, "ISA   ", 6);
385                 break;
386         }
387         MP_bus_info(&bus);
388         if (mpc_default_type > 4) {
389                 bus.mpc_busid = 1;
390                 memcpy(bus.mpc_bustype, "PCI   ", 6);
391                 MP_bus_info(&bus);
392         }
393
394         ioapic.mpc_type = MP_IOAPIC;
395         ioapic.mpc_apicid = 2;
396         ioapic.mpc_apicver = 0;
397         ioapic.mpc_flags = MPC_APIC_USABLE;
398         ioapic.mpc_apicaddr = 0xFEC00000;
399         MP_ioapic_info(&ioapic);
400
401         /*
402          * We set up most of the low 16 IO-APIC pins according to MPS rules.
403          */
404         construct_default_ioirq_mptable(mpc_default_type);
405
406         lintsrc.mpc_type = MP_LINTSRC;
407         lintsrc.mpc_irqflag = 0;        /* conforming */
408         lintsrc.mpc_srcbusid = 0;
409         lintsrc.mpc_srcbusirq = 0;
410         lintsrc.mpc_destapic = MP_APIC_ALL;
411         for (i = 0; i < 2; i++) {
412                 lintsrc.mpc_irqtype = linttypes[i];
413                 lintsrc.mpc_destapiclint = i;
414                 MP_lintsrc_info(&lintsrc);
415         }
416 }
417
418 static struct intel_mp_floating *mpf_found;
419
420 /*
421  * Scan the memory blocks for an SMP configuration block.
422  */
423 static void __init __get_smp_config(unsigned early)
424 {
425         struct intel_mp_floating *mpf = mpf_found;
426
427         if (acpi_lapic && early)
428                 return;
429         /*
430          * ACPI supports both logical (e.g. Hyper-Threading) and physical
431          * processors, where MPS only supports physical.
432          */
433         if (acpi_lapic && acpi_ioapic) {
434                 printk(KERN_INFO "Using ACPI (MADT) for SMP configuration "
435                        "information\n");
436                 return;
437         } else if (acpi_lapic)
438                 printk(KERN_INFO "Using ACPI for processor (LAPIC) "
439                        "configuration information\n");
440
441         printk(KERN_INFO "Intel MultiProcessor Specification v1.%d\n",
442                mpf->mpf_specification);
443
444         /*
445          * Now see if we need to read further.
446          */
447         if (mpf->mpf_feature1 != 0) {
448                 if (early) {
449                         /*
450                          * local APIC has default address
451                          */
452                         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
453                         return;
454                 }
455
456                 printk(KERN_INFO "Default MP configuration #%d\n",
457                        mpf->mpf_feature1);
458                 construct_default_ISA_mptable(mpf->mpf_feature1);
459
460         } else if (mpf->mpf_physptr) {
461
462                 /*
463                  * Read the physical hardware table.  Anything here will
464                  * override the defaults.
465                  */
466                 if (!smp_read_mpc(phys_to_virt(mpf->mpf_physptr), early)) {
467                         smp_found_config = 0;
468                         printk(KERN_ERR
469                                "BIOS bug, MP table errors detected!...\n");
470                         printk(KERN_ERR "... disabling SMP support. "
471                                "(tell your hw vendor)\n");
472                         return;
473                 }
474
475                 if (early)
476                         return;
477                 /*
478                  * If there are no explicit MP IRQ entries, then we are
479                  * broken.  We set up most of the low 16 IO-APIC pins to
480                  * ISA defaults and hope it will work.
481                  */
482                 if (!mp_irq_entries) {
483                         struct mpc_config_bus bus;
484
485                         printk(KERN_ERR "BIOS bug, no explicit IRQ entries, "
486                                "using default mptable. "
487                                "(tell your hw vendor)\n");
488
489                         bus.mpc_type = MP_BUS;
490                         bus.mpc_busid = 0;
491                         memcpy(bus.mpc_bustype, "ISA   ", 6);
492                         MP_bus_info(&bus);
493
494                         construct_default_ioirq_mptable(0);
495                 }
496
497         } else
498                 BUG();
499
500         if (!early)
501                 printk(KERN_INFO "Processors: %d\n", num_processors);
502         /*
503          * Only use the first configuration found.
504          */
505 }
506
507 void __init early_get_smp_config(void)
508 {
509         __get_smp_config(1);
510 }
511
512 void __init get_smp_config(void)
513 {
514         __get_smp_config(0);
515 }
516
517 static int __init smp_scan_config(unsigned long base, unsigned long length,
518                                   unsigned reserve)
519 {
520         extern void __bad_mpf_size(void);
521         unsigned int *bp = phys_to_virt(base);
522         struct intel_mp_floating *mpf;
523
524         Dprintk("Scan SMP from %p for %ld bytes.\n", bp, length);
525         if (sizeof(*mpf) != 16)
526                 __bad_mpf_size();
527
528         while (length > 0) {
529                 mpf = (struct intel_mp_floating *)bp;
530                 if ((*bp == SMP_MAGIC_IDENT) &&
531                     (mpf->mpf_length == 1) &&
532                     !mpf_checksum((unsigned char *)bp, 16) &&
533                     ((mpf->mpf_specification == 1)
534                      || (mpf->mpf_specification == 4))) {
535
536                         smp_found_config = 1;
537                         mpf_found = mpf;
538
539                         if (!reserve)
540                                 return 1;
541
542                         reserve_bootmem_generic(virt_to_phys(mpf), PAGE_SIZE);
543                         if (mpf->mpf_physptr)
544                                 reserve_bootmem_generic(mpf->mpf_physptr,
545                                                         PAGE_SIZE);
546                         return 1;
547                 }
548                 bp += 4;
549                 length -= 16;
550         }
551         return 0;
552 }
553
554 static void __init __find_smp_config(unsigned reserve)
555 {
556         unsigned int address;
557
558         /*
559          * FIXME: Linux assumes you have 640K of base ram..
560          * this continues the error...
561          *
562          * 1) Scan the bottom 1K for a signature
563          * 2) Scan the top 1K of base RAM
564          * 3) Scan the 64K of bios
565          */
566         if (smp_scan_config(0x0, 0x400, reserve) ||
567             smp_scan_config(639 * 0x400, 0x400, reserve) ||
568             smp_scan_config(0xF0000, 0x10000, reserve))
569                 return;
570         /*
571          * If it is an SMP machine we should know now.
572          *
573          * there is a real-mode segmented pointer pointing to the
574          * 4K EBDA area at 0x40E, calculate and scan it here.
575          *
576          * NOTE! There are Linux loaders that will corrupt the EBDA
577          * area, and as such this kind of SMP config may be less
578          * trustworthy, simply because the SMP table may have been
579          * stomped on during early boot. These loaders are buggy and
580          * should be fixed.
581          *
582          * MP1.4 SPEC states to only scan first 1K of 4K EBDA.
583          */
584
585         address = get_bios_ebda();
586         if (address)
587                 smp_scan_config(address, 0x400, reserve);
588 }
589
590 void __init early_find_smp_config(void)
591 {
592         __find_smp_config(0);
593 }
594
595 void __init find_smp_config(void)
596 {
597         __find_smp_config(1);
598 }
599
600 /* --------------------------------------------------------------------------
601                             ACPI-based MP Configuration
602    -------------------------------------------------------------------------- */
603
604 #ifdef CONFIG_ACPI
605
606 void __init mp_register_lapic_address(u64 address)
607 {
608         mp_lapic_addr = (unsigned long)address;
609         set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
610         if (boot_cpu_physical_apicid == -1U)
611                 boot_cpu_physical_apicid  = GET_APIC_ID(read_apic_id());
612 }
613 void __cpuinit mp_register_lapic(int id, u8 enabled)
614 {
615         if (!enabled) {
616                 ++disabled_cpus;
617                 return;
618         }
619
620         generic_processor_info(id, 0);
621 }
622
623
624 #define MP_ISA_BUS              0
625 #define MP_MAX_IOAPIC_PIN       127
626
627 extern struct mp_ioapic_routing mp_ioapic_routing[MAX_IO_APICS];
628
629 static int mp_find_ioapic(int gsi)
630 {
631         int i = 0;
632
633         /* Find the IOAPIC that manages this GSI. */
634         for (i = 0; i < nr_ioapics; i++) {
635                 if ((gsi >= mp_ioapic_routing[i].gsi_base)
636                     && (gsi <= mp_ioapic_routing[i].gsi_end))
637                         return i;
638         }
639
640         printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
641         return -1;
642 }
643
644 static u8 uniq_ioapic_id(u8 id)
645 {
646         int i;
647         DECLARE_BITMAP(used, 256);
648         bitmap_zero(used, 256);
649         for (i = 0; i < nr_ioapics; i++) {
650                 struct mpc_config_ioapic *ia = &mp_ioapics[i];
651                 __set_bit(ia->mpc_apicid, used);
652         }
653         if (!test_bit(id, used))
654                 return id;
655         return find_first_zero_bit(used, 256);
656 }
657
658 void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
659 {
660         int idx = 0;
661
662         if (bad_ioapic(address))
663                 return;
664
665         idx = nr_ioapics;
666
667         mp_ioapics[idx].mpc_type = MP_IOAPIC;
668         mp_ioapics[idx].mpc_flags = MPC_APIC_USABLE;
669         mp_ioapics[idx].mpc_apicaddr = address;
670
671         set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
672         mp_ioapics[idx].mpc_apicid = uniq_ioapic_id(id);
673         mp_ioapics[idx].mpc_apicver = 0;
674
675         /* 
676          * Build basic IRQ lookup table to facilitate gsi->io_apic lookups
677          * and to prevent reprogramming of IOAPIC pins (PCI IRQs).
678          */
679         mp_ioapic_routing[idx].apic_id = mp_ioapics[idx].mpc_apicid;
680         mp_ioapic_routing[idx].gsi_base = gsi_base;
681         mp_ioapic_routing[idx].gsi_end = gsi_base +
682             io_apic_get_redir_entries(idx);
683
684         printk(KERN_INFO "IOAPIC[%d]: apic_id %d, address 0x%x, "
685                "GSI %d-%d\n", idx, mp_ioapics[idx].mpc_apicid,
686                mp_ioapics[idx].mpc_apicaddr,
687                mp_ioapic_routing[idx].gsi_base,
688                mp_ioapic_routing[idx].gsi_end);
689
690         nr_ioapics++;
691 }
692
693 void __init mp_override_legacy_irq(u8 bus_irq, u8 polarity, u8 trigger, u32 gsi)
694 {
695         struct mpc_config_intsrc intsrc;
696         int ioapic = -1;
697         int pin = -1;
698
699         /* 
700          * Convert 'gsi' to 'ioapic.pin'.
701          */
702         ioapic = mp_find_ioapic(gsi);
703         if (ioapic < 0)
704                 return;
705         pin = gsi - mp_ioapic_routing[ioapic].gsi_base;
706
707         /*
708          * TBD: This check is for faulty timer entries, where the override
709          *      erroneously sets the trigger to level, resulting in a HUGE 
710          *      increase of timer interrupts!
711          */
712         if ((bus_irq == 0) && (trigger == 3))
713                 trigger = 1;
714
715         intsrc.mpc_type = MP_INTSRC;
716         intsrc.mpc_irqtype = mp_INT;
717         intsrc.mpc_irqflag = (trigger << 2) | polarity;
718         intsrc.mpc_srcbus = MP_ISA_BUS;
719         intsrc.mpc_srcbusirq = bus_irq; /* IRQ */
720         intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;     /* APIC ID */
721         intsrc.mpc_dstirq = pin;        /* INTIN# */
722
723         Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, %d-%d\n",
724                 intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3,
725                 (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus,
726                 intsrc.mpc_srcbusirq, intsrc.mpc_dstapic, intsrc.mpc_dstirq);
727
728         mp_irqs[mp_irq_entries] = intsrc;
729         if (++mp_irq_entries == MAX_IRQ_SOURCES)
730                 panic("Max # of irq sources exceeded!\n");
731 }
732
733 void __init mp_config_acpi_legacy_irqs(void)
734 {
735         struct mpc_config_intsrc intsrc;
736         int i = 0;
737         int ioapic = -1;
738
739         /* 
740          * Fabricate the legacy ISA bus (bus #31).
741          */
742         set_bit(MP_ISA_BUS, mp_bus_not_pci);
743
744         /* 
745          * Locate the IOAPIC that manages the ISA IRQs (0-15). 
746          */
747         ioapic = mp_find_ioapic(0);
748         if (ioapic < 0)
749                 return;
750
751         intsrc.mpc_type = MP_INTSRC;
752         intsrc.mpc_irqflag = 0; /* Conforming */
753         intsrc.mpc_srcbus = MP_ISA_BUS;
754         intsrc.mpc_dstapic = mp_ioapics[ioapic].mpc_apicid;
755
756         /* 
757          * Use the default configuration for the IRQs 0-15.  Unless
758          * overridden by (MADT) interrupt source override entries.
759          */
760         for (i = 0; i < 16; i++) {
761                 int idx;
762
763                 for (idx = 0; idx < mp_irq_entries; idx++) {
764                         struct mpc_config_intsrc *irq = mp_irqs + idx;
765
766                         /* Do we already have a mapping for this ISA IRQ? */
767                         if (irq->mpc_srcbus == MP_ISA_BUS
768                             && irq->mpc_srcbusirq == i)
769                                 break;
770
771                         /* Do we already have a mapping for this IOAPIC pin */
772                         if ((irq->mpc_dstapic == intsrc.mpc_dstapic) &&
773                             (irq->mpc_dstirq == i))
774                                 break;
775                 }
776
777                 if (idx != mp_irq_entries) {
778                         printk(KERN_DEBUG "ACPI: IRQ%d used by override.\n", i);
779                         continue;       /* IRQ already used */
780                 }
781
782                 intsrc.mpc_irqtype = mp_INT;
783                 intsrc.mpc_srcbusirq = i;       /* Identity mapped */
784                 intsrc.mpc_dstirq = i;
785
786                 Dprintk("Int: type %d, pol %d, trig %d, bus %d, irq %d, "
787                         "%d-%d\n", intsrc.mpc_irqtype, intsrc.mpc_irqflag & 3,
788                         (intsrc.mpc_irqflag >> 2) & 3, intsrc.mpc_srcbus,
789                         intsrc.mpc_srcbusirq, intsrc.mpc_dstapic,
790                         intsrc.mpc_dstirq);
791
792                 mp_irqs[mp_irq_entries] = intsrc;
793                 if (++mp_irq_entries == MAX_IRQ_SOURCES)
794                         panic("Max # of irq sources exceeded!\n");
795         }
796 }
797
798 int mp_register_gsi(u32 gsi, int triggering, int polarity)
799 {
800         int ioapic = -1;
801         int ioapic_pin = 0;
802         int idx, bit = 0;
803
804         if (acpi_irq_model != ACPI_IRQ_MODEL_IOAPIC)
805                 return gsi;
806
807         /* Don't set up the ACPI SCI because it's already set up */
808         if (acpi_gbl_FADT.sci_interrupt == gsi)
809                 return gsi;
810
811         ioapic = mp_find_ioapic(gsi);
812         if (ioapic < 0) {
813                 printk(KERN_WARNING "No IOAPIC for GSI %u\n", gsi);
814                 return gsi;
815         }
816
817         ioapic_pin = gsi - mp_ioapic_routing[ioapic].gsi_base;
818
819         /* 
820          * Avoid pin reprogramming.  PRTs typically include entries  
821          * with redundant pin->gsi mappings (but unique PCI devices);
822          * we only program the IOAPIC on the first.
823          */
824         bit = ioapic_pin % 32;
825         idx = (ioapic_pin < 32) ? 0 : (ioapic_pin / 32);
826         if (idx > 3) {
827                 printk(KERN_ERR "Invalid reference to IOAPIC pin "
828                        "%d-%d\n", mp_ioapic_routing[ioapic].apic_id,
829                        ioapic_pin);
830                 return gsi;
831         }
832         if ((1 << bit) & mp_ioapic_routing[ioapic].pin_programmed[idx]) {
833                 Dprintk(KERN_DEBUG "Pin %d-%d already programmed\n",
834                         mp_ioapic_routing[ioapic].apic_id, ioapic_pin);
835                 return gsi;
836         }
837
838         mp_ioapic_routing[ioapic].pin_programmed[idx] |= (1 << bit);
839
840         io_apic_set_pci_routing(ioapic, ioapic_pin, gsi,
841                                 triggering == ACPI_EDGE_SENSITIVE ? 0 : 1,
842                                 polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
843         return gsi;
844 }
845 #endif /* CONFIG_ACPI */