Import 2.3.13pre1
[davej-history.git] / arch / i386 / kernel / io_apic.c
blobcae25555adc0fc41674db149fdcfd0b31f906a4e
1 /*
2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
16 #include <linux/sched.h>
17 #include <linux/smp_lock.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
20 #include <asm/io.h>
22 #include "irq.h"
25 * volatile is justified in this case, IO-APIC register contents
26 * might change spontaneously, GCC should not cache it
28 #define IO_APIC_BASE(idx) ((volatile int *)__fix_to_virt(FIX_IO_APIC_BASE_0 + idx))
31 * The structure of the IO-APIC:
34 struct IO_APIC_reg_00 {
35 __u32 __reserved_2 : 24,
36 ID : 4,
37 __reserved_1 : 4;
38 } __attribute__ ((packed));
40 struct IO_APIC_reg_01 {
41 __u32 version : 8,
42 __reserved_2 : 8,
43 entries : 8,
44 __reserved_1 : 8;
45 } __attribute__ ((packed));
47 struct IO_APIC_reg_02 {
48 __u32 __reserved_2 : 24,
49 arbitration : 4,
50 __reserved_1 : 4;
51 } __attribute__ ((packed));
54 * # of IO-APICs and # of IRQ routing registers
56 int nr_ioapics = 0;
57 int nr_ioapic_registers[MAX_IO_APICS];
59 enum ioapic_irq_destination_types {
60 dest_Fixed = 0,
61 dest_LowestPrio = 1,
62 dest_ExtINT = 7
65 struct IO_APIC_route_entry {
66 __u32 vector : 8,
67 delivery_mode : 3, /* 000: FIXED
68 * 001: lowest prio
69 * 111: ExtINT
71 dest_mode : 1, /* 0: physical, 1: logical */
72 delivery_status : 1,
73 polarity : 1,
74 irr : 1,
75 trigger : 1, /* 0: edge, 1: level */
76 mask : 1, /* 0: enabled, 1: disabled */
77 __reserved_2 : 15;
79 union { struct { __u32
80 __reserved_1 : 24,
81 physical_dest : 4,
82 __reserved_2 : 4;
83 } physical;
85 struct { __u32
86 __reserved_1 : 24,
87 logical_dest : 8;
88 } logical;
89 } dest;
91 } __attribute__ ((packed));
94 * MP-BIOS irq configuration table structures:
97 enum mp_irq_source_types {
98 mp_INT = 0,
99 mp_NMI = 1,
100 mp_SMI = 2,
101 mp_ExtINT = 3
104 struct mpc_config_ioapic mp_apics[MAX_IO_APICS];/* I/O APIC entries */
105 int mp_irq_entries = 0; /* # of MP IRQ source entries */
106 struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
107 /* MP IRQ source entries */
108 int mpc_default_type = 0; /* non-0 if default (table-less)
109 MP configuration */
113 * This is performance-critical, we want to do it O(1)
115 * the indexing order of this array favors 1:1 mappings
116 * between pins and IRQs.
119 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
121 *IO_APIC_BASE(apic) = reg;
122 return *(IO_APIC_BASE(apic)+4);
125 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
127 *IO_APIC_BASE(apic) = reg;
128 *(IO_APIC_BASE(apic)+4) = value;
132 * Re-write a value: to be used for read-modify-write
133 * cycles where the read already set up the index register.
135 static inline void io_apic_modify(unsigned int apic, unsigned int value)
137 *(IO_APIC_BASE(apic)+4) = value;
141 * Synchronize the IO-APIC and the CPU by doing
142 * a dummy read from the IO-APIC
144 static inline void io_apic_sync(unsigned int apic)
146 (void) *(IO_APIC_BASE(apic)+4);
150 * Rough estimation of how many shared IRQs there are, can
151 * be changed anytime.
153 #define MAX_PLUS_SHARED_IRQS NR_IRQS
154 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
156 static struct irq_pin_list {
157 int apic, pin, next;
158 } irq_2_pin[PIN_MAP_SIZE];
161 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
162 * shared ISA-space IRQs, so we have to support them. We are super
163 * fast in the common case, and fast for shared ISA-space IRQs.
165 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
167 static int first_free_entry = NR_IRQS;
168 struct irq_pin_list *entry = irq_2_pin + irq;
170 while (entry->next)
171 entry = irq_2_pin + entry->next;
173 if (entry->pin != -1) {
174 entry->next = first_free_entry;
175 entry = irq_2_pin + entry->next;
176 if (++first_free_entry >= PIN_MAP_SIZE)
177 panic("io_apic.c: whoops");
179 entry->apic = apic;
180 entry->pin = pin;
183 #define DO_ACTION(name,R,ACTION, FINAL) \
185 static void name##_IO_APIC_irq(unsigned int irq) \
187 int pin; \
188 struct irq_pin_list *entry = irq_2_pin + irq; \
190 for (;;) { \
191 unsigned int reg; \
192 pin = entry->pin; \
193 if (pin == -1) \
194 break; \
195 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
196 reg ACTION; \
197 io_apic_modify(entry->apic, reg); \
198 if (!entry->next) \
199 break; \
200 entry = irq_2_pin + entry->next; \
202 FINAL; \
206 * We disable IO-APIC IRQs by setting their 'destination CPU mask' to
207 * zero. Trick by Ramesh Nalluri.
209 DO_ACTION( disable, 1, &= 0x00ffffff, io_apic_sync(entry->apic))/* destination = 0x00 */
210 DO_ACTION( enable, 1, |= 0xff000000, ) /* destination = 0xff */
211 DO_ACTION( mask, 0, |= 0x00010000, io_apic_sync(entry->apic))/* mask = 1 */
212 DO_ACTION( unmask, 0, &= 0xfffeffff, ) /* mask = 0 */
214 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
216 struct IO_APIC_route_entry entry;
219 * Disable it in the IO-APIC irq-routing table:
221 memset(&entry, 0, sizeof(entry));
222 entry.mask = 1;
223 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
224 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
227 static void clear_IO_APIC (void)
229 int apic, pin;
231 for (apic = 0; apic < nr_ioapics; apic++)
232 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
233 clear_IO_APIC_pin(apic, pin);
237 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
238 * specific CPU-side IRQs.
241 #define MAX_PIRQS 8
242 int pirq_entries [MAX_PIRQS];
243 int pirqs_enabled;
245 void __init ioapic_setup(char *str, int *ints)
247 extern int skip_ioapic_setup; /* defined in arch/i386/kernel/smp.c */
249 skip_ioapic_setup = 1;
252 void __init ioapic_pirq_setup(char *str, int *ints)
254 int i, max;
256 for (i = 0; i < MAX_PIRQS; i++)
257 pirq_entries[i] = -1;
259 if (!ints) {
260 pirqs_enabled = 0;
261 printk("PIRQ redirection, trusting MP-BIOS.\n");
263 } else {
264 pirqs_enabled = 1;
265 printk("PIRQ redirection, working around broken MP-BIOS.\n");
266 max = MAX_PIRQS;
267 if (ints[0] < MAX_PIRQS)
268 max = ints[0];
270 for (i = 0; i < max; i++) {
271 printk("... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
273 * PIRQs are mapped upside down, usually.
275 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
281 * Find the IRQ entry number of a certain pin.
283 static int __init find_irq_entry(int apic, int pin, int type)
285 int i;
287 for (i = 0; i < mp_irq_entries; i++)
288 if ( (mp_irqs[i].mpc_irqtype == type) &&
289 (mp_irqs[i].mpc_dstapic == mp_apics[apic].mpc_apicid) &&
290 (mp_irqs[i].mpc_dstirq == pin))
292 return i;
294 return -1;
298 * Find the pin to which IRQ0 (ISA) is connected
300 static int __init find_timer_pin(int type)
302 int i;
304 for (i = 0; i < mp_irq_entries; i++) {
305 int lbus = mp_irqs[i].mpc_srcbus;
307 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
308 mp_bus_id_to_type[lbus] == MP_BUS_EISA) &&
309 (mp_irqs[i].mpc_irqtype == type) &&
310 (mp_irqs[i].mpc_srcbusirq == 0x00))
312 return mp_irqs[i].mpc_dstirq;
314 return -1;
318 * Find a specific PCI IRQ entry.
319 * Not an initfunc, possibly needed by modules
321 static int __init pin_2_irq(int idx, int apic, int pin);
322 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pci_pin)
324 int apic, i;
326 for (i = 0; i < mp_irq_entries; i++) {
327 int lbus = mp_irqs[i].mpc_srcbus;
329 for (apic = 0; apic < nr_ioapics; apic++)
330 if (mp_apics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
331 break;
333 if ((apic || IO_APIC_IRQ(mp_irqs[i].mpc_dstirq)) &&
334 (mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
335 !mp_irqs[i].mpc_irqtype &&
336 (bus == mp_bus_id_to_pci_bus[mp_irqs[i].mpc_srcbus]) &&
337 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f)) &&
338 (pci_pin == (mp_irqs[i].mpc_srcbusirq & 3)))
340 return pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
342 return -1;
346 * EISA Edge/Level control register, ELCR
348 static int __init EISA_ELCR(unsigned int irq)
350 if (irq < 16) {
351 unsigned int port = 0x4d0 + (irq >> 3);
352 return (inb(port) >> (irq & 7)) & 1;
354 printk("Broken MPtable reports ISA irq %d\n", irq);
355 return 0;
358 /* EISA interrupts are always polarity zero and can be edge or level
359 * trigger depending on the ELCR value. If an interrupt is listed as
360 * EISA conforming in the MP table, that means its trigger type must
361 * be read in from the ELCR */
363 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_dstirq))
364 #define default_EISA_polarity(idx) (0)
366 /* ISA interrupts are always polarity zero edge triggered, even when
367 * listed as conforming in the MP table. */
369 #define default_ISA_trigger(idx) (0)
370 #define default_ISA_polarity(idx) (0)
372 static int __init MPBIOS_polarity(int idx)
374 int bus = mp_irqs[idx].mpc_srcbus;
375 int polarity;
378 * Determine IRQ line polarity (high active or low active):
380 switch (mp_irqs[idx].mpc_irqflag & 3)
382 case 0: /* conforms, ie. bus-type dependent polarity */
384 switch (mp_bus_id_to_type[bus])
386 case MP_BUS_ISA: /* ISA pin */
388 polarity = default_ISA_polarity(idx);
389 break;
391 case MP_BUS_EISA:
393 polarity = default_EISA_polarity(idx);
394 break;
396 case MP_BUS_PCI: /* PCI pin */
398 polarity = 1;
399 break;
401 default:
403 printk("broken BIOS!!\n");
404 polarity = 1;
405 break;
408 break;
410 case 1: /* high active */
412 polarity = 0;
413 break;
415 case 2: /* reserved */
417 printk("broken BIOS!!\n");
418 polarity = 1;
419 break;
421 case 3: /* low active */
423 polarity = 1;
424 break;
426 default: /* invalid */
428 printk("broken BIOS!!\n");
429 polarity = 1;
430 break;
433 return polarity;
436 static int __init MPBIOS_trigger(int idx)
438 int bus = mp_irqs[idx].mpc_srcbus;
439 int trigger;
442 * Determine IRQ trigger mode (edge or level sensitive):
444 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
446 case 0: /* conforms, ie. bus-type dependent */
448 switch (mp_bus_id_to_type[bus])
450 case MP_BUS_ISA:
452 trigger = default_ISA_trigger(idx);
453 break;
455 case MP_BUS_EISA:
457 trigger = default_EISA_trigger(idx);
458 break;
460 case MP_BUS_PCI: /* PCI pin, level */
462 trigger = 1;
463 break;
465 default:
467 printk("broken BIOS!!\n");
468 trigger = 1;
469 break;
472 break;
474 case 1: /* edge */
476 trigger = 0;
477 break;
479 case 2: /* reserved */
481 printk("broken BIOS!!\n");
482 trigger = 1;
483 break;
485 case 3: /* level */
487 trigger = 1;
488 break;
490 default: /* invalid */
492 printk("broken BIOS!!\n");
493 trigger = 0;
494 break;
497 return trigger;
500 static inline int irq_polarity(int idx)
502 return MPBIOS_polarity(idx);
505 static inline int irq_trigger(int idx)
507 return MPBIOS_trigger(idx);
510 static int __init pin_2_irq(int idx, int apic, int pin)
512 int irq, i;
513 int bus = mp_irqs[idx].mpc_srcbus;
516 * Debugging check, we are in big trouble if this message pops up!
518 if (mp_irqs[idx].mpc_dstirq != pin)
519 printk("broken BIOS or MPTABLE parser, ayiee!!\n");
521 switch (mp_bus_id_to_type[bus])
523 case MP_BUS_ISA: /* ISA pin */
524 case MP_BUS_EISA:
526 irq = mp_irqs[idx].mpc_srcbusirq;
527 break;
529 case MP_BUS_PCI: /* PCI pin */
532 * PCI IRQs are mapped in order
534 i = irq = 0;
535 while (i < apic)
536 irq += nr_ioapic_registers[i++];
537 irq += pin;
538 break;
540 default:
542 printk("unknown bus type %d.\n",bus);
543 irq = 0;
544 break;
549 * PCI IRQ command line redirection. Yes, limits are hardcoded.
551 if ((pin >= 16) && (pin <= 23)) {
552 if (pirq_entries[pin-16] != -1) {
553 if (!pirq_entries[pin-16]) {
554 printk("disabling PIRQ%d\n", pin-16);
555 } else {
556 irq = pirq_entries[pin-16];
557 printk("using PIRQ%d -> IRQ %d\n",
558 pin-16, irq);
562 return irq;
565 static inline int IO_APIC_irq_trigger(int irq)
567 int apic, idx, pin;
569 for (apic = 0; apic < nr_ioapics; apic++) {
570 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
571 idx = find_irq_entry(apic,pin,mp_INT);
572 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
573 return irq_trigger(idx);
577 * nonexistent IRQs are edge default
579 return 0;
582 int irq_vector[NR_IRQS] = { IRQ0_TRAP_VECTOR , 0 };
584 static int __init assign_irq_vector(int irq)
586 static int current_vector = IRQ0_TRAP_VECTOR, offset = 0;
587 if (IO_APIC_VECTOR(irq) > 0)
588 return IO_APIC_VECTOR(irq);
589 current_vector += 8;
590 if (current_vector > 0xFE) {
591 offset++;
592 current_vector = IRQ0_TRAP_VECTOR + offset;
593 printk("WARNING: ASSIGN_IRQ_VECTOR wrapped back to %02X\n",
594 current_vector);
596 if (current_vector == SYSCALL_VECTOR)
597 panic("ran out of interrupt sources!");
599 IO_APIC_VECTOR(irq) = current_vector;
600 return current_vector;
603 void __init setup_IO_APIC_irqs(void)
605 struct IO_APIC_route_entry entry;
606 int apic, pin, idx, irq, first_notcon = 1;
608 printk("init IO_APIC IRQs\n");
610 for (apic = 0; apic < nr_ioapics; apic++) {
611 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
614 * add it to the IO-APIC irq-routing table:
616 memset(&entry,0,sizeof(entry));
618 entry.delivery_mode = dest_LowestPrio;
619 entry.dest_mode = 1; /* logical delivery */
620 entry.mask = 0; /* enable IRQ */
621 entry.dest.logical.logical_dest = 0; /* but no route */
623 idx = find_irq_entry(apic,pin,mp_INT);
624 if (idx == -1) {
625 if (first_notcon) {
626 printk(" IO-APIC (apicid-pin) %d-%d", mp_apics[apic].mpc_apicid, pin);
627 first_notcon = 0;
628 } else
629 printk(", %d-%d", mp_apics[apic].mpc_apicid, pin);
630 continue;
633 entry.trigger = irq_trigger(idx);
634 entry.polarity = irq_polarity(idx);
636 if (irq_trigger(idx)) {
637 entry.trigger = 1;
638 entry.mask = 1;
639 entry.dest.logical.logical_dest = 0xff;
642 irq = pin_2_irq(idx,apic,pin);
643 add_pin_to_irq(irq, apic, pin);
645 if (!apic && !IO_APIC_IRQ(irq))
646 continue;
648 entry.vector = assign_irq_vector(irq);
650 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
651 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
655 if (!first_notcon)
656 printk(" not connected.\n");
660 * Set up a certain pin as ExtINT delivered interrupt
662 void __init setup_ExtINT_pin(unsigned int apic, unsigned int pin, int irq)
664 struct IO_APIC_route_entry entry;
667 * add it to the IO-APIC irq-routing table:
669 memset(&entry,0,sizeof(entry));
671 entry.delivery_mode = dest_ExtINT;
672 entry.dest_mode = 0; /* physical delivery */
673 entry.mask = 0; /* unmask IRQ now */
675 * We use physical delivery to get the timer IRQ
676 * to the boot CPU. 'boot_cpu_id' is the physical
677 * APIC ID of the boot CPU.
679 entry.dest.physical.physical_dest = boot_cpu_id;
681 entry.vector = assign_irq_vector(irq);
683 entry.polarity = 0;
684 entry.trigger = 0;
686 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
687 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
690 void __init UNEXPECTED_IO_APIC(void)
692 printk(" WARNING: unexpected IO-APIC, please mail\n");
693 printk(" to linux-smp@vger.rutgers.edu\n");
696 void __init print_IO_APIC(void)
698 int apic, i;
699 struct IO_APIC_reg_00 reg_00;
700 struct IO_APIC_reg_01 reg_01;
701 struct IO_APIC_reg_02 reg_02;
703 printk("number of MP IRQ sources: %d.\n", mp_irq_entries);
704 for (i = 0; i < nr_ioapics; i++)
705 printk("number of IO-APIC #%d registers: %d.\n", mp_apics[i].mpc_apicid, nr_ioapic_registers[i]);
708 * We are a bit conservative about what we expect. We have to
709 * know about every hardware change ASAP.
711 printk("testing the IO APIC.......................\n");
713 for (apic = 0; apic < nr_ioapics; apic++) {
715 *(int *)&reg_00 = io_apic_read(apic, 0);
716 *(int *)&reg_01 = io_apic_read(apic, 1);
717 *(int *)&reg_02 = io_apic_read(apic, 2);
718 printk("\nIO APIC #%d......\n", mp_apics[apic].mpc_apicid);
719 printk(".... register #00: %08X\n", *(int *)&reg_00);
720 printk("....... : physical APIC id: %02X\n", reg_00.ID);
721 if (reg_00.__reserved_1 || reg_00.__reserved_2)
722 UNEXPECTED_IO_APIC();
724 printk(".... register #01: %08X\n", *(int *)&reg_01);
725 printk("....... : max redirection entries: %04X\n", reg_01.entries);
726 if ( (reg_01.entries != 0x0f) && /* older (Neptune) boards */
727 (reg_01.entries != 0x17) && /* typical ISA+PCI boards */
728 (reg_01.entries != 0x1b) && /* Compaq Proliant boards */
729 (reg_01.entries != 0x1f) && /* dual Xeon boards */
730 (reg_01.entries != 0x3F) /* bigger Xeon boards */
732 UNEXPECTED_IO_APIC();
734 printk("....... : IO APIC version: %04X\n", reg_01.version);
735 if ( (reg_01.version != 0x10) && /* oldest IO-APICs */
736 (reg_01.version != 0x11) && /* Pentium/Pro IO-APICs */
737 (reg_01.version != 0x13) /* Xeon IO-APICs */
739 UNEXPECTED_IO_APIC();
740 if (reg_01.__reserved_1 || reg_01.__reserved_2)
741 UNEXPECTED_IO_APIC();
743 printk(".... register #02: %08X\n", *(int *)&reg_02);
744 printk("....... : arbitration: %02X\n", reg_02.arbitration);
745 if (reg_02.__reserved_1 || reg_02.__reserved_2)
746 UNEXPECTED_IO_APIC();
748 printk(".... IRQ redirection table:\n");
750 printk(" NR Log Phy ");
751 printk("Mask Trig IRR Pol Stat Dest Deli Vect: \n");
753 for (i = 0; i <= reg_01.entries; i++) {
754 struct IO_APIC_route_entry entry;
756 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
757 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
759 printk(" %02x %03X %02X ",
761 entry.dest.logical.logical_dest,
762 entry.dest.physical.physical_dest
765 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
766 entry.mask,
767 entry.trigger,
768 entry.irr,
769 entry.polarity,
770 entry.delivery_status,
771 entry.dest_mode,
772 entry.delivery_mode,
773 entry.vector
777 printk(KERN_DEBUG "IRQ to pin mappings:\n");
778 for (i = 0; i < NR_IRQS; i++) {
779 struct irq_pin_list *entry = irq_2_pin + i;
780 if (entry->pin < 0)
781 continue;
782 printk(KERN_DEBUG "IRQ%d ", i);
783 for (;;) {
784 printk("-> %d", entry->pin);
785 if (!entry->next)
786 break;
787 entry = irq_2_pin + entry->next;
789 printk("\n");
792 printk(".................................... done.\n");
794 return;
797 static void __init init_sym_mode(void)
799 int i;
801 for (i = 0; i < PIN_MAP_SIZE; i++) {
802 irq_2_pin[i].pin = -1;
803 irq_2_pin[i].next = 0;
805 if (!pirqs_enabled)
806 for (i = 0; i < MAX_PIRQS; i++)
807 pirq_entries[i] =- 1;
809 printk("enabling symmetric IO mode... ");
811 outb(0x70, 0x22);
812 outb(0x01, 0x23);
814 printk("...done.\n");
817 * The number of IO-APIC IRQ registers (== #pins):
820 struct IO_APIC_reg_01 reg_01;
821 int i;
823 for (i = 0; i < nr_ioapics; i++) {
824 *(int *)&reg_01 = io_apic_read(i, 1);
825 nr_ioapic_registers[i] = reg_01.entries+1;
830 * Do not trust the IO-APIC being empty at bootup
832 clear_IO_APIC();
836 * Not an initfunc, needed by the reboot code
838 void init_pic_mode(void)
841 * Clear the IO-APIC before rebooting:
843 clear_IO_APIC();
846 * Put it back into PIC mode (has an effect only on
847 * certain boards)
849 printk("disabling symmetric IO mode... ");
850 outb_p(0x70, 0x22);
851 outb_p(0x00, 0x23);
852 printk("...done.\n");
855 static void __init setup_ioapic_id(void)
857 struct IO_APIC_reg_00 reg_00;
860 * 'default' mptable configurations mean a hardwired setup,
861 * 2 CPUs, 16 APIC registers. IO-APIC ID is usually set to 0,
862 * setting it to ID 2 should be fine.
866 * Sanity check, is ID 2 really free? Every APIC in the
867 * system must have a unique ID or we get lots of nice
868 * 'stuck on smp_invalidate_needed IPI wait' messages.
870 if (cpu_present_map & (1<<0x2))
871 panic("APIC ID 2 already used");
874 * Set the ID
876 *(int *)&reg_00 = io_apic_read(0, 0);
877 printk("...changing IO-APIC physical APIC ID to 2...\n");
878 reg_00.ID = 0x2;
879 io_apic_write(0, 0, *(int *)&reg_00);
882 * Sanity check
884 *(int *)&reg_00 = io_apic_read(0, 0);
885 if (reg_00.ID != 0x2)
886 panic("could not set ID");
889 static void __init construct_default_ISA_mptable(void)
891 int i, pos = 0;
892 const int bus_type = (mpc_default_type == 2 || mpc_default_type == 3 ||
893 mpc_default_type == 6) ? MP_BUS_EISA : MP_BUS_ISA;
895 for (i = 0; i < 16; i++) {
896 if (!IO_APIC_IRQ(i))
897 continue;
899 mp_irqs[pos].mpc_irqtype = mp_INT;
900 mp_irqs[pos].mpc_irqflag = 0; /* default */
901 mp_irqs[pos].mpc_srcbus = 0;
902 mp_irqs[pos].mpc_srcbusirq = i;
903 mp_irqs[pos].mpc_dstapic = 0;
904 mp_irqs[pos].mpc_dstirq = i;
905 pos++;
907 mp_irq_entries = pos;
908 mp_bus_id_to_type[0] = bus_type;
911 * MP specification 1.4 defines some extra rules for default
912 * configurations, fix them up here:
915 switch (mpc_default_type)
917 case 2:
918 break;
919 default:
921 * pin 2 is IRQ0:
923 mp_irqs[0].mpc_dstirq = 2;
926 setup_ioapic_id();
930 * There is a nasty bug in some older SMP boards, their mptable lies
931 * about the timer IRQ. We do the following to work around the situation:
933 * - timer IRQ defaults to IO-APIC IRQ
934 * - if this function detects that timer IRQs are defunct, then we fall
935 * back to ISA timer IRQs
937 static int __init timer_irq_works(void)
939 unsigned int t1 = jiffies;
941 sti();
942 mdelay(100);
944 if (jiffies-t1>1)
945 return 1;
947 return 0;
951 * In the SMP+IOAPIC case it might happen that there are an unspecified
952 * number of pending IRQ events unhandled. These cases are very rare,
953 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
954 * better to do it this way as thus we do not have to be aware of
955 * 'pending' interrupts in the IRQ path, except at this point.
957 static inline void self_IPI(unsigned int irq)
959 irq_desc_t *desc = irq_desc + irq;
960 unsigned int status = desc->status;
962 if ((status & (IRQ_PENDING | IRQ_REPLAY)) == IRQ_PENDING) {
963 desc->status = status | IRQ_REPLAY;
964 send_IPI_self(IO_APIC_VECTOR(irq));
969 * Edge triggered needs to resend any interrupt
970 * that was delayed.
972 static void enable_edge_ioapic_irq(unsigned int irq)
974 self_IPI(irq);
975 enable_IO_APIC_irq(irq);
978 static void disable_edge_ioapic_irq(unsigned int irq)
980 disable_IO_APIC_irq(irq);
984 * Starting up a edge-triggered IO-APIC interrupt is
985 * nasty - we need to make sure that we get the edge.
986 * If it is already asserted for some reason, we need
987 * to fake an edge by marking it IRQ_PENDING..
989 * This is not complete - we should be able to fake
990 * an edge even if it isn't on the 8259A...
993 static void startup_edge_ioapic_irq(unsigned int irq)
995 if (irq < 16) {
996 disable_8259A_irq(irq);
997 if (i8259A_irq_pending(irq))
998 irq_desc[irq].status |= IRQ_PENDING;
1000 enable_edge_ioapic_irq(irq);
1003 #define shutdown_edge_ioapic_irq disable_edge_ioapic_irq
1006 * Level triggered interrupts can just be masked,
1007 * and shutting down and starting up the interrupt
1008 * is the same as enabling and disabling them.
1010 #define startup_level_ioapic_irq unmask_IO_APIC_irq
1011 #define shutdown_level_ioapic_irq mask_IO_APIC_irq
1012 #define enable_level_ioapic_irq unmask_IO_APIC_irq
1013 #define disable_level_ioapic_irq mask_IO_APIC_irq
1015 static void do_edge_ioapic_IRQ(unsigned int irq, struct pt_regs * regs)
1017 irq_desc_t *desc = irq_desc + irq;
1018 struct irqaction * action;
1019 unsigned int status;
1021 spin_lock(&irq_controller_lock);
1024 * Edge triggered IRQs can be acknowledged immediately
1025 * and do not need to be masked.
1027 ack_APIC_irq();
1028 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
1029 status |= IRQ_PENDING;
1032 * If the IRQ is disabled for whatever reason, we cannot
1033 * use the action we have.
1035 action = NULL;
1036 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
1037 action = desc->action;
1038 status &= ~IRQ_PENDING;
1039 status |= IRQ_INPROGRESS;
1041 desc->status = status;
1042 spin_unlock(&irq_controller_lock);
1045 * If there is no IRQ handler or it was disabled, exit early.
1047 if (!action)
1048 return;
1051 * Edge triggered interrupts need to remember
1052 * pending events.
1054 for (;;) {
1055 handle_IRQ_event(irq, regs, action);
1057 spin_lock(&irq_controller_lock);
1058 if (!(desc->status & IRQ_PENDING))
1059 break;
1060 desc->status &= ~IRQ_PENDING;
1061 spin_unlock(&irq_controller_lock);
1063 desc->status &= ~IRQ_INPROGRESS;
1064 spin_unlock(&irq_controller_lock);
1067 static void do_level_ioapic_IRQ(unsigned int irq, struct pt_regs * regs)
1069 irq_desc_t *desc = irq_desc + irq;
1070 struct irqaction * action;
1071 unsigned int status;
1073 spin_lock(&irq_controller_lock);
1075 * In the level triggered case we first disable the IRQ
1076 * in the IO-APIC, then we 'early ACK' the IRQ, then we
1077 * handle it and enable the IRQ when finished.
1079 * disable has to happen before the ACK, to avoid IRQ storms.
1080 * So this all has to be within the spinlock.
1082 mask_IO_APIC_irq(irq);
1083 status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
1086 * If the IRQ is disabled for whatever reason, we must
1087 * not enter the IRQ action.
1089 action = NULL;
1090 if (!(status & (IRQ_DISABLED | IRQ_INPROGRESS))) {
1091 action = desc->action;
1092 status |= IRQ_INPROGRESS;
1094 desc->status = status;
1096 ack_APIC_irq();
1097 spin_unlock(&irq_controller_lock);
1099 /* Exit early if we had no action or it was disabled */
1100 if (!action)
1101 return;
1103 handle_IRQ_event(irq, regs, action);
1105 spin_lock(&irq_controller_lock);
1106 desc->status &= ~IRQ_INPROGRESS;
1107 if (!(desc->status & IRQ_DISABLED))
1108 unmask_IO_APIC_irq(irq);
1109 spin_unlock(&irq_controller_lock);
1113 * Level and edge triggered IO-APIC interrupts need different handling,
1114 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1115 * handled with the level-triggered descriptor, but that one has slightly
1116 * more overhead. Level-triggered interrupts cannot be handled with the
1117 * edge-triggered handler, without risking IRQ storms and other ugly
1118 * races.
1121 static struct hw_interrupt_type ioapic_edge_irq_type = {
1122 "IO-APIC-edge",
1123 startup_edge_ioapic_irq,
1124 shutdown_edge_ioapic_irq,
1125 do_edge_ioapic_IRQ,
1126 enable_edge_ioapic_irq,
1127 disable_edge_ioapic_irq
1130 static struct hw_interrupt_type ioapic_level_irq_type = {
1131 "IO-APIC-level",
1132 startup_level_ioapic_irq,
1133 shutdown_level_ioapic_irq,
1134 do_level_ioapic_IRQ,
1135 enable_level_ioapic_irq,
1136 disable_level_ioapic_irq
1139 static inline void init_IO_APIC_traps(void)
1141 int i;
1143 * NOTE! The local APIC isn't very good at handling
1144 * multiple interrupts at the same interrupt level.
1145 * As the interrupt level is determined by taking the
1146 * vector number and shifting that right by 4, we
1147 * want to spread these out a bit so that they don't
1148 * all fall in the same interrupt level.
1150 * Also, we've got to be careful not to trash gate
1151 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1153 for (i = 0; i < NR_IRQS ; i++) {
1154 if (IO_APIC_VECTOR(i) > 0) {
1155 if (IO_APIC_irq_trigger(i))
1156 irq_desc[i].handler = &ioapic_level_irq_type;
1157 else
1158 irq_desc[i].handler = &ioapic_edge_irq_type;
1160 * disable it in the 8259A:
1162 if (i < 16)
1163 disable_8259A_irq(i);
1164 } else {
1165 if (!IO_APIC_IRQ(i))
1166 continue;
1169 * Hmm.. We don't have an entry for this,
1170 * so default to an old-fashioned 8259
1171 * interrupt if we can..
1173 if (i < 16) {
1174 make_8259A_irq(i);
1175 continue;
1178 /* Strange. Oh, well.. */
1179 irq_desc[i].handler = &no_irq_type;
1182 init_IRQ_SMP();
1186 * This code may look a bit paranoid, but it's supposed to cooperate with
1187 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1188 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1189 * fanatically on his truly buggy board.
1191 static inline void check_timer(void)
1193 int pin1, pin2;
1195 pin1 = find_timer_pin(mp_INT);
1196 pin2 = find_timer_pin(mp_ExtINT);
1197 enable_IO_APIC_irq(0);
1198 if (!timer_irq_works()) {
1200 if (pin1 != -1)
1201 printk("..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1202 printk("...trying to set up timer as ExtINT... ");
1204 if (pin2 != -1) {
1205 printk(".. (found pin %d) ...", pin2);
1207 * legacy devices should be connected to IO APIC #0
1209 setup_ExtINT_pin(0, pin2, 0);
1210 make_8259A_irq(0);
1213 if (!timer_irq_works()) {
1214 printk(" failed.\n");
1215 printk("...trying to set up timer as BP IRQ...");
1217 * Just in case ...
1219 if (pin1 != -1)
1220 clear_IO_APIC_pin(0, pin1);
1221 if (pin2 != -1)
1222 clear_IO_APIC_pin(0, pin2);
1224 make_8259A_irq(0);
1226 if (!timer_irq_works()) {
1227 printk(" failed.\n");
1228 panic("IO-APIC + timer doesn't work!");
1231 printk(" works.\n");
1237 * IRQ's that are handled by the old PIC in all cases:
1238 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1239 * Linux doesn't really care, as it's not actually used
1240 * for any interrupt handling anyway.
1241 * - IRQ13 is the FPU error IRQ, and may be connected
1242 * directly from the FPU to the old PIC. Linux doesn't
1243 * really care, because Linux doesn't want to use IRQ13
1244 * anyway (exception 16 is the proper FPU error signal)
1246 * Additionally, something is definitely wrong with irq9
1247 * on PIIX4 boards.
1249 #define PIC_IRQS ((1<<2)|(1<<13))
1251 void __init setup_IO_APIC(void)
1253 init_sym_mode();
1255 printk("ENABLING IO-APIC IRQs\n");
1256 io_apic_irqs = ~PIC_IRQS;
1259 * If there are no explicit MP IRQ entries, it's either one of the
1260 * default configuration types or we are broken. In both cases it's
1261 * fine to set up most of the low 16 IO-APIC pins to ISA defaults.
1263 if (!mp_irq_entries) {
1264 printk("no explicit IRQ entries, using default mptable\n");
1265 construct_default_ISA_mptable();
1269 * Set up the IO-APIC IRQ routing table by parsing the MP-BIOS
1270 * mptable:
1272 setup_IO_APIC_irqs();
1273 init_IO_APIC_traps();
1274 check_timer();
1276 print_IO_APIC();