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