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>
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,
38 } __attribute__ ((packed
));
40 struct IO_APIC_reg_01
{
45 } __attribute__ ((packed
));
47 struct IO_APIC_reg_02
{
48 __u32 __reserved_2
: 24,
51 } __attribute__ ((packed
));
54 * # of IO-APICs and # of IRQ routing registers
57 int nr_ioapic_registers
[MAX_IO_APICS
];
59 enum ioapic_irq_destination_types
{
65 struct IO_APIC_route_entry
{
67 delivery_mode
: 3, /* 000: FIXED
71 dest_mode
: 1, /* 0: physical, 1: logical */
75 trigger
: 1, /* 0: edge, 1: level */
76 mask
: 1, /* 0: enabled, 1: disabled */
79 union { struct { __u32
91 } __attribute__ ((packed
));
94 * MP-BIOS irq configuration table structures:
97 enum mp_irq_source_types
{
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)
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
{
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
;
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");
183 #define DO_ACTION(name,R,ACTION, FINAL) \
185 static void name##_IO_APIC_irq(unsigned int irq) \
188 struct irq_pin_list *entry = irq_2_pin + irq; \
195 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
197 io_apic_modify(entry->apic, reg); \
200 entry = irq_2_pin + entry->next; \
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
));
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)
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.
242 int pirq_entries
[MAX_PIRQS
];
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
)
256 for (i
= 0; i
< MAX_PIRQS
; i
++)
257 pirq_entries
[i
] = -1;
261 printk("PIRQ redirection, trusting MP-BIOS.\n");
265 printk("PIRQ redirection, working around broken MP-BIOS.\n");
267 if (ints
[0] < MAX_PIRQS
)
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
)
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
))
298 * Find the pin to which IRQ0 (ISA) is connected
300 static int __init
find_timer_pin(int type
)
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
;
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
)
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
)
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
);
346 * EISA Edge/Level control register, ELCR
348 static int __init
EISA_ELCR(unsigned int irq
)
351 unsigned int port
= 0x4d0 + (irq
>> 3);
352 return (inb(port
) >> (irq
& 7)) & 1;
354 printk("Broken MPtable reports ISA irq %d\n", irq
);
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
;
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
);
393 polarity
= default_EISA_polarity(idx
);
396 case MP_BUS_PCI
: /* PCI pin */
403 printk("broken BIOS!!\n");
410 case 1: /* high active */
415 case 2: /* reserved */
417 printk("broken BIOS!!\n");
421 case 3: /* low active */
426 default: /* invalid */
428 printk("broken BIOS!!\n");
436 static int __init
MPBIOS_trigger(int idx
)
438 int bus
= mp_irqs
[idx
].mpc_srcbus
;
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
])
452 trigger
= default_ISA_trigger(idx
);
457 trigger
= default_EISA_trigger(idx
);
460 case MP_BUS_PCI
: /* PCI pin, level */
467 printk("broken BIOS!!\n");
479 case 2: /* reserved */
481 printk("broken BIOS!!\n");
490 default: /* invalid */
492 printk("broken BIOS!!\n");
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
)
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 */
526 irq
= mp_irqs
[idx
].mpc_srcbusirq
;
529 case MP_BUS_PCI
: /* PCI pin */
532 * PCI IRQs are mapped in order
536 irq
+= nr_ioapic_registers
[i
++];
542 printk("unknown bus type %d.\n",bus
);
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);
556 irq
= pirq_entries
[pin
-16];
557 printk("using PIRQ%d -> IRQ %d\n",
565 static inline int IO_APIC_irq_trigger(int irq
)
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
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
);
590 if (current_vector
> 0xFE) {
592 current_vector
= IRQ0_TRAP_VECTOR
+ offset
;
593 printk("WARNING: ASSIGN_IRQ_VECTOR wrapped back to %02X\n",
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
);
626 printk(" IO-APIC (apicid-pin) %d-%d", mp_apics
[apic
].mpc_apicid
, pin
);
629 printk(", %d-%d", mp_apics
[apic
].mpc_apicid
, pin
);
633 entry
.trigger
= irq_trigger(idx
);
634 entry
.polarity
= irq_polarity(idx
);
636 if (irq_trigger(idx
)) {
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
))
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));
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
);
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)
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 *)®_00
= io_apic_read(apic
, 0);
716 *(int *)®_01
= io_apic_read(apic
, 1);
717 *(int *)®_02
= io_apic_read(apic
, 2);
718 printk("\nIO APIC #%d......\n", mp_apics
[apic
].mpc_apicid
);
719 printk(".... register #00: %08X\n", *(int *)®_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 *)®_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 *)®_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",
770 entry
.delivery_status
,
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
;
782 printk(KERN_DEBUG
"IRQ%d ", i
);
784 printk("-> %d", entry
->pin
);
787 entry
= irq_2_pin
+ entry
->next
;
792 printk(".................................... done.\n");
797 static void __init
init_sym_mode(void)
801 for (i
= 0; i
< PIN_MAP_SIZE
; i
++) {
802 irq_2_pin
[i
].pin
= -1;
803 irq_2_pin
[i
].next
= 0;
806 for (i
= 0; i
< MAX_PIRQS
; i
++)
807 pirq_entries
[i
] =- 1;
809 printk("enabling symmetric IO mode... ");
814 printk("...done.\n");
817 * The number of IO-APIC IRQ registers (== #pins):
820 struct IO_APIC_reg_01 reg_01
;
823 for (i
= 0; i
< nr_ioapics
; i
++) {
824 *(int *)®_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
836 * Not an initfunc, needed by the reboot code
838 void init_pic_mode(void)
841 * Clear the IO-APIC before rebooting:
846 * Put it back into PIC mode (has an effect only on
849 printk("disabling symmetric IO mode... ");
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");
876 *(int *)®_00
= io_apic_read(0, 0);
877 printk("...changing IO-APIC physical APIC ID to 2...\n");
879 io_apic_write(0, 0, *(int *)®_00
);
884 *(int *)®_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)
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
++) {
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
;
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
)
923 mp_irqs
[0].mpc_dstirq
= 2;
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
;
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
972 static void enable_edge_ioapic_irq(unsigned int 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
)
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.
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.
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.
1051 * Edge triggered interrupts need to remember
1055 handle_IRQ_event(irq
, regs
, action
);
1057 spin_lock(&irq_controller_lock
);
1058 if (!(desc
->status
& IRQ_PENDING
))
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.
1090 if (!(status
& (IRQ_DISABLED
| IRQ_INPROGRESS
))) {
1091 action
= desc
->action
;
1092 status
|= IRQ_INPROGRESS
;
1094 desc
->status
= status
;
1097 spin_unlock(&irq_controller_lock
);
1099 /* Exit early if we had no action or it was disabled */
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
1121 static struct hw_interrupt_type ioapic_edge_irq_type
= {
1123 startup_edge_ioapic_irq
,
1124 shutdown_edge_ioapic_irq
,
1126 enable_edge_ioapic_irq
,
1127 disable_edge_ioapic_irq
1130 static struct hw_interrupt_type ioapic_level_irq_type
= {
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)
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
;
1158 irq_desc
[i
].handler
= &ioapic_edge_irq_type
;
1160 * disable it in the 8259A:
1163 disable_8259A_irq(i
);
1165 if (!IO_APIC_IRQ(i
))
1169 * Hmm.. We don't have an entry for this,
1170 * so default to an old-fashioned 8259
1171 * interrupt if we can..
1178 /* Strange. Oh, well.. */
1179 irq_desc
[i
].handler
= &no_irq_type
;
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)
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()) {
1201 printk("..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1202 printk("...trying to set up timer as ExtINT... ");
1205 printk(".. (found pin %d) ...", pin2
);
1207 * legacy devices should be connected to IO APIC #0
1209 setup_ExtINT_pin(0, pin2
, 0);
1213 if (!timer_irq_works()) {
1214 printk(" failed.\n");
1215 printk("...trying to set up timer as BP IRQ...");
1220 clear_IO_APIC_pin(0, pin1
);
1222 clear_IO_APIC_pin(0, pin2
);
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
1249 #define PIC_IRQS ((1<<2)|(1<<13))
1251 void __init
setup_IO_APIC(void)
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
1272 setup_IO_APIC_irqs();
1273 init_IO_APIC_traps();