2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 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 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/pci.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
33 #include <linux/msi.h>
35 #include <acpi/acpi_bus.h>
41 #include <asm/proto.h>
42 #include <asm/mach_apic.h>
46 #include <asm/msidef.h>
47 #include <asm/hypertransport.h>
49 static int assign_irq_vector(int irq
, cpumask_t mask
);
51 #define __apicdebuginit __init
53 int sis_apic_bug
; /* not actually supported, dummy for compile */
55 static int no_timer_check
;
57 static int disable_timer_pin_1 __initdata
;
59 int timer_over_8254 __initdata
= 0;
61 /* Where if anywhere is the i8259 connect in external int mode */
62 static struct { int pin
, apic
; } ioapic_i8259
= { -1, -1 };
64 static DEFINE_SPINLOCK(ioapic_lock
);
65 static DEFINE_SPINLOCK(vector_lock
);
68 * # of IRQ routing registers
70 int nr_ioapic_registers
[MAX_IO_APICS
];
73 * Rough estimation of how many shared IRQs there are, can
76 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
77 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
80 * This is performance-critical, we want to do it O(1)
82 * the indexing order of this array favors 1:1 mappings
83 * between pins and IRQs.
86 static struct irq_pin_list
{
87 short apic
, pin
, next
;
88 } irq_2_pin
[PIN_MAP_SIZE
];
90 #define __DO_ACTION(R, ACTION, FINAL) \
94 struct irq_pin_list *entry = irq_2_pin + irq; \
96 BUG_ON(irq >= NR_IRQS); \
102 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
104 io_apic_modify(entry->apic, reg); \
107 entry = irq_2_pin + entry->next; \
113 struct { u32 w1
, w2
; };
114 struct IO_APIC_route_entry entry
;
117 static struct IO_APIC_route_entry
ioapic_read_entry(int apic
, int pin
)
119 union entry_union eu
;
121 spin_lock_irqsave(&ioapic_lock
, flags
);
122 eu
.w1
= io_apic_read(apic
, 0x10 + 2 * pin
);
123 eu
.w2
= io_apic_read(apic
, 0x11 + 2 * pin
);
124 spin_unlock_irqrestore(&ioapic_lock
, flags
);
128 static void ioapic_write_entry(int apic
, int pin
, struct IO_APIC_route_entry e
)
131 union entry_union eu
;
133 spin_lock_irqsave(&ioapic_lock
, flags
);
134 io_apic_write(apic
, 0x10 + 2*pin
, eu
.w1
);
135 io_apic_write(apic
, 0x11 + 2*pin
, eu
.w2
);
136 spin_unlock_irqrestore(&ioapic_lock
, flags
);
140 static void __target_IO_APIC_irq(unsigned int irq
, unsigned int dest
, u8 vector
)
143 struct irq_pin_list
*entry
= irq_2_pin
+ irq
;
145 BUG_ON(irq
>= NR_IRQS
);
152 io_apic_write(apic
, 0x11 + pin
*2, dest
);
153 reg
= io_apic_read(apic
, 0x10 + pin
*2);
156 io_apic_modify(apic
, reg
);
159 entry
= irq_2_pin
+ entry
->next
;
163 static void set_ioapic_affinity_irq(unsigned int irq
, cpumask_t mask
)
170 cpus_and(tmp
, mask
, cpu_online_map
);
174 cpus_and(mask
, tmp
, CPU_MASK_ALL
);
176 vector
= assign_irq_vector(irq
, mask
);
181 cpu_set(vector
>> 8, tmp
);
182 dest
= cpu_mask_to_apicid(tmp
);
185 * Only the high 8 bits are valid.
187 dest
= SET_APIC_LOGICAL_ID(dest
);
189 spin_lock_irqsave(&ioapic_lock
, flags
);
190 __target_IO_APIC_irq(irq
, dest
, vector
& 0xff);
191 set_native_irq_info(irq
, mask
);
192 spin_unlock_irqrestore(&ioapic_lock
, flags
);
197 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
198 * shared ISA-space IRQs, so we have to support them. We are super
199 * fast in the common case, and fast for shared ISA-space IRQs.
201 static void add_pin_to_irq(unsigned int irq
, int apic
, int pin
)
203 static int first_free_entry
= NR_IRQS
;
204 struct irq_pin_list
*entry
= irq_2_pin
+ irq
;
206 BUG_ON(irq
>= NR_IRQS
);
208 entry
= irq_2_pin
+ entry
->next
;
210 if (entry
->pin
!= -1) {
211 entry
->next
= first_free_entry
;
212 entry
= irq_2_pin
+ entry
->next
;
213 if (++first_free_entry
>= PIN_MAP_SIZE
)
214 panic("io_apic.c: ran out of irq_2_pin entries!");
221 #define DO_ACTION(name,R,ACTION, FINAL) \
223 static void name##_IO_APIC_irq (unsigned int irq) \
224 __DO_ACTION(R, ACTION, FINAL)
226 DO_ACTION( __mask
, 0, |= 0x00010000, io_apic_sync(entry
->apic
) )
228 DO_ACTION( __unmask
, 0, &= 0xfffeffff, )
231 static void mask_IO_APIC_irq (unsigned int irq
)
235 spin_lock_irqsave(&ioapic_lock
, flags
);
236 __mask_IO_APIC_irq(irq
);
237 spin_unlock_irqrestore(&ioapic_lock
, flags
);
240 static void unmask_IO_APIC_irq (unsigned int irq
)
244 spin_lock_irqsave(&ioapic_lock
, flags
);
245 __unmask_IO_APIC_irq(irq
);
246 spin_unlock_irqrestore(&ioapic_lock
, flags
);
249 static void clear_IO_APIC_pin(unsigned int apic
, unsigned int pin
)
251 struct IO_APIC_route_entry entry
;
253 /* Check delivery_mode to be sure we're not clearing an SMI pin */
254 entry
= ioapic_read_entry(apic
, pin
);
255 if (entry
.delivery_mode
== dest_SMI
)
258 * Disable it in the IO-APIC irq-routing table:
260 memset(&entry
, 0, sizeof(entry
));
262 ioapic_write_entry(apic
, pin
, entry
);
265 static void clear_IO_APIC (void)
269 for (apic
= 0; apic
< nr_ioapics
; apic
++)
270 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++)
271 clear_IO_APIC_pin(apic
, pin
);
274 int skip_ioapic_setup
;
277 /* dummy parsing: see setup.c */
279 static int __init
disable_ioapic_setup(char *str
)
281 skip_ioapic_setup
= 1;
284 early_param("noapic", disable_ioapic_setup
);
286 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
287 static int __init
disable_timer_pin_setup(char *arg
)
289 disable_timer_pin_1
= 1;
292 __setup("disable_timer_pin_1", disable_timer_pin_setup
);
294 static int __init
setup_disable_8254_timer(char *s
)
296 timer_over_8254
= -1;
299 static int __init
setup_enable_8254_timer(char *s
)
305 __setup("disable_8254_timer", setup_disable_8254_timer
);
306 __setup("enable_8254_timer", setup_enable_8254_timer
);
310 * Find the IRQ entry number of a certain pin.
312 static int find_irq_entry(int apic
, int pin
, int type
)
316 for (i
= 0; i
< mp_irq_entries
; i
++)
317 if (mp_irqs
[i
].mpc_irqtype
== type
&&
318 (mp_irqs
[i
].mpc_dstapic
== mp_ioapics
[apic
].mpc_apicid
||
319 mp_irqs
[i
].mpc_dstapic
== MP_APIC_ALL
) &&
320 mp_irqs
[i
].mpc_dstirq
== pin
)
327 * Find the pin to which IRQ[irq] (ISA) is connected
329 static int __init
find_isa_irq_pin(int irq
, int type
)
333 for (i
= 0; i
< mp_irq_entries
; i
++) {
334 int lbus
= mp_irqs
[i
].mpc_srcbus
;
336 if (test_bit(lbus
, mp_bus_not_pci
) &&
337 (mp_irqs
[i
].mpc_irqtype
== type
) &&
338 (mp_irqs
[i
].mpc_srcbusirq
== irq
))
340 return mp_irqs
[i
].mpc_dstirq
;
345 static int __init
find_isa_irq_apic(int irq
, int type
)
349 for (i
= 0; i
< mp_irq_entries
; i
++) {
350 int lbus
= mp_irqs
[i
].mpc_srcbus
;
352 if (test_bit(lbus
, mp_bus_not_pci
) &&
353 (mp_irqs
[i
].mpc_irqtype
== type
) &&
354 (mp_irqs
[i
].mpc_srcbusirq
== irq
))
357 if (i
< mp_irq_entries
) {
359 for(apic
= 0; apic
< nr_ioapics
; apic
++) {
360 if (mp_ioapics
[apic
].mpc_apicid
== mp_irqs
[i
].mpc_dstapic
)
369 * Find a specific PCI IRQ entry.
370 * Not an __init, possibly needed by modules
372 static int pin_2_irq(int idx
, int apic
, int pin
);
374 int IO_APIC_get_PCI_irq_vector(int bus
, int slot
, int pin
)
376 int apic
, i
, best_guess
= -1;
378 apic_printk(APIC_DEBUG
, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
380 if (mp_bus_id_to_pci_bus
[bus
] == -1) {
381 apic_printk(APIC_VERBOSE
, "PCI BIOS passed nonexistent PCI bus %d!\n", bus
);
384 for (i
= 0; i
< mp_irq_entries
; i
++) {
385 int lbus
= mp_irqs
[i
].mpc_srcbus
;
387 for (apic
= 0; apic
< nr_ioapics
; apic
++)
388 if (mp_ioapics
[apic
].mpc_apicid
== mp_irqs
[i
].mpc_dstapic
||
389 mp_irqs
[i
].mpc_dstapic
== MP_APIC_ALL
)
392 if (!test_bit(lbus
, mp_bus_not_pci
) &&
393 !mp_irqs
[i
].mpc_irqtype
&&
395 (slot
== ((mp_irqs
[i
].mpc_srcbusirq
>> 2) & 0x1f))) {
396 int irq
= pin_2_irq(i
,apic
,mp_irqs
[i
].mpc_dstirq
);
398 if (!(apic
|| IO_APIC_IRQ(irq
)))
401 if (pin
== (mp_irqs
[i
].mpc_srcbusirq
& 3))
404 * Use the first all-but-pin matching entry as a
405 * best-guess fuzzy result for broken mptables.
411 BUG_ON(best_guess
>= NR_IRQS
);
415 /* ISA interrupts are always polarity zero edge triggered,
416 * when listed as conforming in the MP table. */
418 #define default_ISA_trigger(idx) (0)
419 #define default_ISA_polarity(idx) (0)
421 /* PCI interrupts are always polarity one level triggered,
422 * when listed as conforming in the MP table. */
424 #define default_PCI_trigger(idx) (1)
425 #define default_PCI_polarity(idx) (1)
427 static int __init
MPBIOS_polarity(int idx
)
429 int bus
= mp_irqs
[idx
].mpc_srcbus
;
433 * Determine IRQ line polarity (high active or low active):
435 switch (mp_irqs
[idx
].mpc_irqflag
& 3)
437 case 0: /* conforms, ie. bus-type dependent polarity */
438 if (test_bit(bus
, mp_bus_not_pci
))
439 polarity
= default_ISA_polarity(idx
);
441 polarity
= default_PCI_polarity(idx
);
443 case 1: /* high active */
448 case 2: /* reserved */
450 printk(KERN_WARNING
"broken BIOS!!\n");
454 case 3: /* low active */
459 default: /* invalid */
461 printk(KERN_WARNING
"broken BIOS!!\n");
469 static int MPBIOS_trigger(int idx
)
471 int bus
= mp_irqs
[idx
].mpc_srcbus
;
475 * Determine IRQ trigger mode (edge or level sensitive):
477 switch ((mp_irqs
[idx
].mpc_irqflag
>>2) & 3)
479 case 0: /* conforms, ie. bus-type dependent */
480 if (test_bit(bus
, mp_bus_not_pci
))
481 trigger
= default_ISA_trigger(idx
);
483 trigger
= default_PCI_trigger(idx
);
490 case 2: /* reserved */
492 printk(KERN_WARNING
"broken BIOS!!\n");
501 default: /* invalid */
503 printk(KERN_WARNING
"broken BIOS!!\n");
511 static inline int irq_polarity(int idx
)
513 return MPBIOS_polarity(idx
);
516 static inline int irq_trigger(int idx
)
518 return MPBIOS_trigger(idx
);
521 static int pin_2_irq(int idx
, int apic
, int pin
)
524 int bus
= mp_irqs
[idx
].mpc_srcbus
;
527 * Debugging check, we are in big trouble if this message pops up!
529 if (mp_irqs
[idx
].mpc_dstirq
!= pin
)
530 printk(KERN_ERR
"broken BIOS or MPTABLE parser, ayiee!!\n");
532 if (test_bit(bus
, mp_bus_not_pci
)) {
533 irq
= mp_irqs
[idx
].mpc_srcbusirq
;
536 * PCI IRQs are mapped in order
540 irq
+= nr_ioapic_registers
[i
++];
543 BUG_ON(irq
>= NR_IRQS
);
547 static inline int IO_APIC_irq_trigger(int irq
)
551 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
552 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++) {
553 idx
= find_irq_entry(apic
,pin
,mp_INT
);
554 if ((idx
!= -1) && (irq
== pin_2_irq(idx
,apic
,pin
)))
555 return irq_trigger(idx
);
559 * nonexistent IRQs are edge default
564 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
565 unsigned int irq_vector
[NR_IRQ_VECTORS
] __read_mostly
= { FIRST_EXTERNAL_VECTOR
, 0 };
567 static int __assign_irq_vector(int irq
, cpumask_t mask
)
570 * NOTE! The local APIC isn't very good at handling
571 * multiple interrupts at the same interrupt level.
572 * As the interrupt level is determined by taking the
573 * vector number and shifting that right by 4, we
574 * want to spread these out a bit so that they don't
575 * all fall in the same interrupt level.
577 * Also, we've got to be careful not to trash gate
578 * 0x80, because int 0x80 is hm, kind of importantish. ;)
583 } pos
[NR_CPUS
] = { [ 0 ... NR_CPUS
- 1] = {FIRST_DEVICE_VECTOR
, 0} };
587 BUG_ON((unsigned)irq
>= NR_IRQ_VECTORS
);
589 if (IO_APIC_VECTOR(irq
) > 0)
590 old_vector
= IO_APIC_VECTOR(irq
);
591 if ((old_vector
> 0) && cpu_isset(old_vector
>> 8, mask
)) {
595 for_each_cpu_mask(cpu
, mask
) {
597 vector
= pos
[cpu
].vector
;
598 offset
= pos
[cpu
].offset
;
601 if (vector
>= FIRST_SYSTEM_VECTOR
) {
602 /* If we run out of vectors on large boxen, must share them. */
603 offset
= (offset
+ 1) % 8;
604 vector
= FIRST_DEVICE_VECTOR
+ offset
;
606 if (unlikely(pos
[cpu
].vector
== vector
))
608 if (vector
== IA32_SYSCALL_VECTOR
)
610 if (per_cpu(vector_irq
, cpu
)[vector
] != -1)
613 pos
[cpu
].vector
= vector
;
614 pos
[cpu
].offset
= offset
;
615 if (old_vector
>= 0) {
616 int old_cpu
= old_vector
>> 8;
618 per_cpu(vector_irq
, old_cpu
)[old_vector
] = -1;
620 per_cpu(vector_irq
, cpu
)[vector
] = irq
;
622 IO_APIC_VECTOR(irq
) = vector
;
628 static int assign_irq_vector(int irq
, cpumask_t mask
)
633 spin_lock_irqsave(&vector_lock
, flags
);
634 vector
= __assign_irq_vector(irq
, mask
);
635 spin_unlock_irqrestore(&vector_lock
, flags
);
639 extern void (*interrupt
[NR_IRQS
])(void);
641 static struct irq_chip ioapic_chip
;
643 #define IOAPIC_AUTO -1
644 #define IOAPIC_EDGE 0
645 #define IOAPIC_LEVEL 1
647 static void ioapic_register_intr(int irq
, int vector
, unsigned long trigger
)
649 if ((trigger
== IOAPIC_AUTO
&& IO_APIC_irq_trigger(irq
)) ||
650 trigger
== IOAPIC_LEVEL
)
651 set_irq_chip_and_handler(irq
, &ioapic_chip
,
654 set_irq_chip_and_handler(irq
, &ioapic_chip
,
658 static void __init
setup_IO_APIC_irqs(void)
660 struct IO_APIC_route_entry entry
;
661 int apic
, pin
, idx
, irq
, first_notcon
= 1, vector
;
664 apic_printk(APIC_VERBOSE
, KERN_DEBUG
"init IO_APIC IRQs\n");
666 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
667 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++) {
670 * add it to the IO-APIC irq-routing table:
672 memset(&entry
,0,sizeof(entry
));
674 entry
.delivery_mode
= INT_DELIVERY_MODE
;
675 entry
.dest_mode
= INT_DEST_MODE
;
676 entry
.mask
= 0; /* enable IRQ */
677 entry
.dest
.logical
.logical_dest
= cpu_mask_to_apicid(TARGET_CPUS
);
679 idx
= find_irq_entry(apic
,pin
,mp_INT
);
682 apic_printk(APIC_VERBOSE
, KERN_DEBUG
" IO-APIC (apicid-pin) %d-%d", mp_ioapics
[apic
].mpc_apicid
, pin
);
685 apic_printk(APIC_VERBOSE
, ", %d-%d", mp_ioapics
[apic
].mpc_apicid
, pin
);
689 entry
.trigger
= irq_trigger(idx
);
690 entry
.polarity
= irq_polarity(idx
);
692 if (irq_trigger(idx
)) {
695 entry
.dest
.logical
.logical_dest
= cpu_mask_to_apicid(TARGET_CPUS
);
698 irq
= pin_2_irq(idx
, apic
, pin
);
699 add_pin_to_irq(irq
, apic
, pin
);
701 if (!apic
&& !IO_APIC_IRQ(irq
))
704 if (IO_APIC_IRQ(irq
)) {
706 vector
= assign_irq_vector(irq
, TARGET_CPUS
);
711 cpu_set(vector
>> 8, mask
);
712 entry
.dest
.logical
.logical_dest
= cpu_mask_to_apicid(mask
);
713 entry
.vector
= vector
& 0xff;
715 ioapic_register_intr(irq
, vector
, IOAPIC_AUTO
);
716 if (!apic
&& (irq
< 16))
717 disable_8259A_irq(irq
);
719 ioapic_write_entry(apic
, pin
, entry
);
721 spin_lock_irqsave(&ioapic_lock
, flags
);
722 set_native_irq_info(irq
, TARGET_CPUS
);
723 spin_unlock_irqrestore(&ioapic_lock
, flags
);
728 apic_printk(APIC_VERBOSE
," not connected.\n");
732 * Set up the 8259A-master output pin as broadcast to all
735 static void __init
setup_ExtINT_IRQ0_pin(unsigned int apic
, unsigned int pin
, int vector
)
737 struct IO_APIC_route_entry entry
;
740 memset(&entry
,0,sizeof(entry
));
742 disable_8259A_irq(0);
745 apic_write(APIC_LVT0
, APIC_LVT_MASKED
| APIC_DM_EXTINT
);
748 * We use logical delivery to get the timer IRQ
751 entry
.dest_mode
= INT_DEST_MODE
;
752 entry
.mask
= 0; /* unmask IRQ now */
753 entry
.dest
.logical
.logical_dest
= cpu_mask_to_apicid(TARGET_CPUS
);
754 entry
.delivery_mode
= INT_DELIVERY_MODE
;
757 entry
.vector
= vector
;
760 * The timer IRQ doesn't have to know that behind the
761 * scene we have a 8259A-master in AEOI mode ...
763 set_irq_chip_and_handler(0, &ioapic_chip
, handle_edge_irq
);
766 * Add it to the IO-APIC irq-routing table:
768 spin_lock_irqsave(&ioapic_lock
, flags
);
769 io_apic_write(apic
, 0x11+2*pin
, *(((int *)&entry
)+1));
770 io_apic_write(apic
, 0x10+2*pin
, *(((int *)&entry
)+0));
771 spin_unlock_irqrestore(&ioapic_lock
, flags
);
776 void __init
UNEXPECTED_IO_APIC(void)
780 void __apicdebuginit
print_IO_APIC(void)
783 union IO_APIC_reg_00 reg_00
;
784 union IO_APIC_reg_01 reg_01
;
785 union IO_APIC_reg_02 reg_02
;
788 if (apic_verbosity
== APIC_QUIET
)
791 printk(KERN_DEBUG
"number of MP IRQ sources: %d.\n", mp_irq_entries
);
792 for (i
= 0; i
< nr_ioapics
; i
++)
793 printk(KERN_DEBUG
"number of IO-APIC #%d registers: %d.\n",
794 mp_ioapics
[i
].mpc_apicid
, nr_ioapic_registers
[i
]);
797 * We are a bit conservative about what we expect. We have to
798 * know about every hardware change ASAP.
800 printk(KERN_INFO
"testing the IO APIC.......................\n");
802 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
804 spin_lock_irqsave(&ioapic_lock
, flags
);
805 reg_00
.raw
= io_apic_read(apic
, 0);
806 reg_01
.raw
= io_apic_read(apic
, 1);
807 if (reg_01
.bits
.version
>= 0x10)
808 reg_02
.raw
= io_apic_read(apic
, 2);
809 spin_unlock_irqrestore(&ioapic_lock
, flags
);
812 printk(KERN_DEBUG
"IO APIC #%d......\n", mp_ioapics
[apic
].mpc_apicid
);
813 printk(KERN_DEBUG
".... register #00: %08X\n", reg_00
.raw
);
814 printk(KERN_DEBUG
"....... : physical APIC id: %02X\n", reg_00
.bits
.ID
);
815 if (reg_00
.bits
.__reserved_1
|| reg_00
.bits
.__reserved_2
)
816 UNEXPECTED_IO_APIC();
818 printk(KERN_DEBUG
".... register #01: %08X\n", *(int *)®_01
);
819 printk(KERN_DEBUG
"....... : max redirection entries: %04X\n", reg_01
.bits
.entries
);
820 if ( (reg_01
.bits
.entries
!= 0x0f) && /* older (Neptune) boards */
821 (reg_01
.bits
.entries
!= 0x17) && /* typical ISA+PCI boards */
822 (reg_01
.bits
.entries
!= 0x1b) && /* Compaq Proliant boards */
823 (reg_01
.bits
.entries
!= 0x1f) && /* dual Xeon boards */
824 (reg_01
.bits
.entries
!= 0x22) && /* bigger Xeon boards */
825 (reg_01
.bits
.entries
!= 0x2E) &&
826 (reg_01
.bits
.entries
!= 0x3F) &&
827 (reg_01
.bits
.entries
!= 0x03)
829 UNEXPECTED_IO_APIC();
831 printk(KERN_DEBUG
"....... : PRQ implemented: %X\n", reg_01
.bits
.PRQ
);
832 printk(KERN_DEBUG
"....... : IO APIC version: %04X\n", reg_01
.bits
.version
);
833 if ( (reg_01
.bits
.version
!= 0x01) && /* 82489DX IO-APICs */
834 (reg_01
.bits
.version
!= 0x02) && /* 82801BA IO-APICs (ICH2) */
835 (reg_01
.bits
.version
!= 0x10) && /* oldest IO-APICs */
836 (reg_01
.bits
.version
!= 0x11) && /* Pentium/Pro IO-APICs */
837 (reg_01
.bits
.version
!= 0x13) && /* Xeon IO-APICs */
838 (reg_01
.bits
.version
!= 0x20) /* Intel P64H (82806 AA) */
840 UNEXPECTED_IO_APIC();
841 if (reg_01
.bits
.__reserved_1
|| reg_01
.bits
.__reserved_2
)
842 UNEXPECTED_IO_APIC();
844 if (reg_01
.bits
.version
>= 0x10) {
845 printk(KERN_DEBUG
".... register #02: %08X\n", reg_02
.raw
);
846 printk(KERN_DEBUG
"....... : arbitration: %02X\n", reg_02
.bits
.arbitration
);
847 if (reg_02
.bits
.__reserved_1
|| reg_02
.bits
.__reserved_2
)
848 UNEXPECTED_IO_APIC();
851 printk(KERN_DEBUG
".... IRQ redirection table:\n");
853 printk(KERN_DEBUG
" NR Log Phy Mask Trig IRR Pol"
854 " Stat Dest Deli Vect: \n");
856 for (i
= 0; i
<= reg_01
.bits
.entries
; i
++) {
857 struct IO_APIC_route_entry entry
;
859 entry
= ioapic_read_entry(apic
, i
);
861 printk(KERN_DEBUG
" %02x %03X %02X ",
863 entry
.dest
.logical
.logical_dest
,
864 entry
.dest
.physical
.physical_dest
867 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
872 entry
.delivery_status
,
879 printk(KERN_DEBUG
"IRQ to pin mappings:\n");
880 for (i
= 0; i
< NR_IRQS
; i
++) {
881 struct irq_pin_list
*entry
= irq_2_pin
+ i
;
884 printk(KERN_DEBUG
"IRQ%d ", i
);
886 printk("-> %d:%d", entry
->apic
, entry
->pin
);
889 entry
= irq_2_pin
+ entry
->next
;
894 printk(KERN_INFO
".................................... done.\n");
901 static __apicdebuginit
void print_APIC_bitfield (int base
)
906 if (apic_verbosity
== APIC_QUIET
)
909 printk(KERN_DEBUG
"0123456789abcdef0123456789abcdef\n" KERN_DEBUG
);
910 for (i
= 0; i
< 8; i
++) {
911 v
= apic_read(base
+ i
*0x10);
912 for (j
= 0; j
< 32; j
++) {
922 void __apicdebuginit
print_local_APIC(void * dummy
)
924 unsigned int v
, ver
, maxlvt
;
926 if (apic_verbosity
== APIC_QUIET
)
929 printk("\n" KERN_DEBUG
"printing local APIC contents on CPU#%d/%d:\n",
930 smp_processor_id(), hard_smp_processor_id());
931 v
= apic_read(APIC_ID
);
932 printk(KERN_INFO
"... APIC ID: %08x (%01x)\n", v
, GET_APIC_ID(v
));
933 v
= apic_read(APIC_LVR
);
934 printk(KERN_INFO
"... APIC VERSION: %08x\n", v
);
935 ver
= GET_APIC_VERSION(v
);
936 maxlvt
= get_maxlvt();
938 v
= apic_read(APIC_TASKPRI
);
939 printk(KERN_DEBUG
"... APIC TASKPRI: %08x (%02x)\n", v
, v
& APIC_TPRI_MASK
);
941 v
= apic_read(APIC_ARBPRI
);
942 printk(KERN_DEBUG
"... APIC ARBPRI: %08x (%02x)\n", v
,
943 v
& APIC_ARBPRI_MASK
);
944 v
= apic_read(APIC_PROCPRI
);
945 printk(KERN_DEBUG
"... APIC PROCPRI: %08x\n", v
);
947 v
= apic_read(APIC_EOI
);
948 printk(KERN_DEBUG
"... APIC EOI: %08x\n", v
);
949 v
= apic_read(APIC_RRR
);
950 printk(KERN_DEBUG
"... APIC RRR: %08x\n", v
);
951 v
= apic_read(APIC_LDR
);
952 printk(KERN_DEBUG
"... APIC LDR: %08x\n", v
);
953 v
= apic_read(APIC_DFR
);
954 printk(KERN_DEBUG
"... APIC DFR: %08x\n", v
);
955 v
= apic_read(APIC_SPIV
);
956 printk(KERN_DEBUG
"... APIC SPIV: %08x\n", v
);
958 printk(KERN_DEBUG
"... APIC ISR field:\n");
959 print_APIC_bitfield(APIC_ISR
);
960 printk(KERN_DEBUG
"... APIC TMR field:\n");
961 print_APIC_bitfield(APIC_TMR
);
962 printk(KERN_DEBUG
"... APIC IRR field:\n");
963 print_APIC_bitfield(APIC_IRR
);
965 v
= apic_read(APIC_ESR
);
966 printk(KERN_DEBUG
"... APIC ESR: %08x\n", v
);
968 v
= apic_read(APIC_ICR
);
969 printk(KERN_DEBUG
"... APIC ICR: %08x\n", v
);
970 v
= apic_read(APIC_ICR2
);
971 printk(KERN_DEBUG
"... APIC ICR2: %08x\n", v
);
973 v
= apic_read(APIC_LVTT
);
974 printk(KERN_DEBUG
"... APIC LVTT: %08x\n", v
);
976 if (maxlvt
> 3) { /* PC is LVT#4. */
977 v
= apic_read(APIC_LVTPC
);
978 printk(KERN_DEBUG
"... APIC LVTPC: %08x\n", v
);
980 v
= apic_read(APIC_LVT0
);
981 printk(KERN_DEBUG
"... APIC LVT0: %08x\n", v
);
982 v
= apic_read(APIC_LVT1
);
983 printk(KERN_DEBUG
"... APIC LVT1: %08x\n", v
);
985 if (maxlvt
> 2) { /* ERR is LVT#3. */
986 v
= apic_read(APIC_LVTERR
);
987 printk(KERN_DEBUG
"... APIC LVTERR: %08x\n", v
);
990 v
= apic_read(APIC_TMICT
);
991 printk(KERN_DEBUG
"... APIC TMICT: %08x\n", v
);
992 v
= apic_read(APIC_TMCCT
);
993 printk(KERN_DEBUG
"... APIC TMCCT: %08x\n", v
);
994 v
= apic_read(APIC_TDCR
);
995 printk(KERN_DEBUG
"... APIC TDCR: %08x\n", v
);
999 void print_all_local_APICs (void)
1001 on_each_cpu(print_local_APIC
, NULL
, 1, 1);
1004 void __apicdebuginit
print_PIC(void)
1007 unsigned long flags
;
1009 if (apic_verbosity
== APIC_QUIET
)
1012 printk(KERN_DEBUG
"\nprinting PIC contents\n");
1014 spin_lock_irqsave(&i8259A_lock
, flags
);
1016 v
= inb(0xa1) << 8 | inb(0x21);
1017 printk(KERN_DEBUG
"... PIC IMR: %04x\n", v
);
1019 v
= inb(0xa0) << 8 | inb(0x20);
1020 printk(KERN_DEBUG
"... PIC IRR: %04x\n", v
);
1024 v
= inb(0xa0) << 8 | inb(0x20);
1028 spin_unlock_irqrestore(&i8259A_lock
, flags
);
1030 printk(KERN_DEBUG
"... PIC ISR: %04x\n", v
);
1032 v
= inb(0x4d1) << 8 | inb(0x4d0);
1033 printk(KERN_DEBUG
"... PIC ELCR: %04x\n", v
);
1038 static void __init
enable_IO_APIC(void)
1040 union IO_APIC_reg_01 reg_01
;
1041 int i8259_apic
, i8259_pin
;
1043 unsigned long flags
;
1045 for (i
= 0; i
< PIN_MAP_SIZE
; i
++) {
1046 irq_2_pin
[i
].pin
= -1;
1047 irq_2_pin
[i
].next
= 0;
1051 * The number of IO-APIC IRQ registers (== #pins):
1053 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
1054 spin_lock_irqsave(&ioapic_lock
, flags
);
1055 reg_01
.raw
= io_apic_read(apic
, 1);
1056 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1057 nr_ioapic_registers
[apic
] = reg_01
.bits
.entries
+1;
1059 for(apic
= 0; apic
< nr_ioapics
; apic
++) {
1061 /* See if any of the pins is in ExtINT mode */
1062 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++) {
1063 struct IO_APIC_route_entry entry
;
1064 entry
= ioapic_read_entry(apic
, pin
);
1066 /* If the interrupt line is enabled and in ExtInt mode
1067 * I have found the pin where the i8259 is connected.
1069 if ((entry
.mask
== 0) && (entry
.delivery_mode
== dest_ExtINT
)) {
1070 ioapic_i8259
.apic
= apic
;
1071 ioapic_i8259
.pin
= pin
;
1077 /* Look to see what if the MP table has reported the ExtINT */
1078 i8259_pin
= find_isa_irq_pin(0, mp_ExtINT
);
1079 i8259_apic
= find_isa_irq_apic(0, mp_ExtINT
);
1080 /* Trust the MP table if nothing is setup in the hardware */
1081 if ((ioapic_i8259
.pin
== -1) && (i8259_pin
>= 0)) {
1082 printk(KERN_WARNING
"ExtINT not setup in hardware but reported by MP table\n");
1083 ioapic_i8259
.pin
= i8259_pin
;
1084 ioapic_i8259
.apic
= i8259_apic
;
1086 /* Complain if the MP table and the hardware disagree */
1087 if (((ioapic_i8259
.apic
!= i8259_apic
) || (ioapic_i8259
.pin
!= i8259_pin
)) &&
1088 (i8259_pin
>= 0) && (ioapic_i8259
.pin
>= 0))
1090 printk(KERN_WARNING
"ExtINT in hardware and MP table differ\n");
1094 * Do not trust the IO-APIC being empty at bootup
1100 * Not an __init, needed by the reboot code
1102 void disable_IO_APIC(void)
1105 * Clear the IO-APIC before rebooting:
1110 * If the i8259 is routed through an IOAPIC
1111 * Put that IOAPIC in virtual wire mode
1112 * so legacy interrupts can be delivered.
1114 if (ioapic_i8259
.pin
!= -1) {
1115 struct IO_APIC_route_entry entry
;
1117 memset(&entry
, 0, sizeof(entry
));
1118 entry
.mask
= 0; /* Enabled */
1119 entry
.trigger
= 0; /* Edge */
1121 entry
.polarity
= 0; /* High */
1122 entry
.delivery_status
= 0;
1123 entry
.dest_mode
= 0; /* Physical */
1124 entry
.delivery_mode
= dest_ExtINT
; /* ExtInt */
1126 entry
.dest
.physical
.physical_dest
=
1127 GET_APIC_ID(apic_read(APIC_ID
));
1130 * Add it to the IO-APIC irq-routing table:
1132 ioapic_write_entry(ioapic_i8259
.apic
, ioapic_i8259
.pin
, entry
);
1135 disconnect_bsp_APIC(ioapic_i8259
.pin
!= -1);
1139 * There is a nasty bug in some older SMP boards, their mptable lies
1140 * about the timer IRQ. We do the following to work around the situation:
1142 * - timer IRQ defaults to IO-APIC IRQ
1143 * - if this function detects that timer IRQs are defunct, then we fall
1144 * back to ISA timer IRQs
1146 static int __init
timer_irq_works(void)
1148 unsigned long t1
= jiffies
;
1151 /* Let ten ticks pass... */
1152 mdelay((10 * 1000) / HZ
);
1155 * Expect a few ticks at least, to be sure some possible
1156 * glue logic does not lock up after one or two first
1157 * ticks in a non-ExtINT mode. Also the local APIC
1158 * might have cached one ExtINT interrupt. Finally, at
1159 * least one tick may be lost due to delays.
1163 if (jiffies
- t1
> 4)
1169 * In the SMP+IOAPIC case it might happen that there are an unspecified
1170 * number of pending IRQ events unhandled. These cases are very rare,
1171 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1172 * better to do it this way as thus we do not have to be aware of
1173 * 'pending' interrupts in the IRQ path, except at this point.
1176 * Edge triggered needs to resend any interrupt
1177 * that was delayed but this is now handled in the device
1182 * Starting up a edge-triggered IO-APIC interrupt is
1183 * nasty - we need to make sure that we get the edge.
1184 * If it is already asserted for some reason, we need
1185 * return 1 to indicate that is was pending.
1187 * This is not complete - we should be able to fake
1188 * an edge even if it isn't on the 8259A...
1191 static unsigned int startup_ioapic_irq(unsigned int irq
)
1193 int was_pending
= 0;
1194 unsigned long flags
;
1196 spin_lock_irqsave(&ioapic_lock
, flags
);
1198 disable_8259A_irq(irq
);
1199 if (i8259A_irq_pending(irq
))
1202 __unmask_IO_APIC_irq(irq
);
1203 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1208 static int ioapic_retrigger_irq(unsigned int irq
)
1213 vector
= irq_vector
[irq
];
1215 cpu_set(vector
>> 8, mask
);
1217 send_IPI_mask(mask
, vector
& 0xff);
1223 * Level and edge triggered IO-APIC interrupts need different handling,
1224 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1225 * handled with the level-triggered descriptor, but that one has slightly
1226 * more overhead. Level-triggered interrupts cannot be handled with the
1227 * edge-triggered handler, without risking IRQ storms and other ugly
1231 static void ack_apic_edge(unsigned int irq
)
1233 move_native_irq(irq
);
1237 static void ack_apic_level(unsigned int irq
)
1239 int do_unmask_irq
= 0;
1241 #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
1242 /* If we are moving the irq we need to mask it */
1243 if (unlikely(irq_desc
[irq
].status
& IRQ_MOVE_PENDING
)) {
1245 mask_IO_APIC_irq(irq
);
1250 * We must acknowledge the irq before we move it or the acknowledge will
1251 * not propogate properly.
1255 /* Now we can move and renable the irq */
1256 move_masked_irq(irq
);
1257 if (unlikely(do_unmask_irq
))
1258 unmask_IO_APIC_irq(irq
);
1261 static struct irq_chip ioapic_chip __read_mostly
= {
1263 .startup
= startup_ioapic_irq
,
1264 .mask
= mask_IO_APIC_irq
,
1265 .unmask
= unmask_IO_APIC_irq
,
1266 .ack
= ack_apic_edge
,
1267 .eoi
= ack_apic_level
,
1269 .set_affinity
= set_ioapic_affinity_irq
,
1271 .retrigger
= ioapic_retrigger_irq
,
1274 static inline void init_IO_APIC_traps(void)
1279 * NOTE! The local APIC isn't very good at handling
1280 * multiple interrupts at the same interrupt level.
1281 * As the interrupt level is determined by taking the
1282 * vector number and shifting that right by 4, we
1283 * want to spread these out a bit so that they don't
1284 * all fall in the same interrupt level.
1286 * Also, we've got to be careful not to trash gate
1287 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1289 for (irq
= 0; irq
< NR_IRQS
; irq
++) {
1291 if (IO_APIC_IRQ(tmp
) && !IO_APIC_VECTOR(tmp
)) {
1293 * Hmm.. We don't have an entry for this,
1294 * so default to an old-fashioned 8259
1295 * interrupt if we can..
1298 make_8259A_irq(irq
);
1300 /* Strange. Oh, well.. */
1301 irq_desc
[irq
].chip
= &no_irq_chip
;
1306 static void enable_lapic_irq (unsigned int irq
)
1310 v
= apic_read(APIC_LVT0
);
1311 apic_write(APIC_LVT0
, v
& ~APIC_LVT_MASKED
);
1314 static void disable_lapic_irq (unsigned int irq
)
1318 v
= apic_read(APIC_LVT0
);
1319 apic_write(APIC_LVT0
, v
| APIC_LVT_MASKED
);
1322 static void ack_lapic_irq (unsigned int irq
)
1327 static void end_lapic_irq (unsigned int i
) { /* nothing */ }
1329 static struct hw_interrupt_type lapic_irq_type __read_mostly
= {
1330 .typename
= "local-APIC-edge",
1331 .startup
= NULL
, /* startup_irq() not used for IRQ0 */
1332 .shutdown
= NULL
, /* shutdown_irq() not used for IRQ0 */
1333 .enable
= enable_lapic_irq
,
1334 .disable
= disable_lapic_irq
,
1335 .ack
= ack_lapic_irq
,
1336 .end
= end_lapic_irq
,
1339 static void setup_nmi (void)
1342 * Dirty trick to enable the NMI watchdog ...
1343 * We put the 8259A master into AEOI mode and
1344 * unmask on all local APICs LVT0 as NMI.
1346 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1347 * is from Maciej W. Rozycki - so we do not have to EOI from
1348 * the NMI handler or the timer interrupt.
1350 printk(KERN_INFO
"activating NMI Watchdog ...");
1352 enable_NMI_through_LVT0(NULL
);
1358 * This looks a bit hackish but it's about the only one way of sending
1359 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1360 * not support the ExtINT mode, unfortunately. We need to send these
1361 * cycles as some i82489DX-based boards have glue logic that keeps the
1362 * 8259A interrupt line asserted until INTA. --macro
1364 static inline void unlock_ExtINT_logic(void)
1367 struct IO_APIC_route_entry entry0
, entry1
;
1368 unsigned char save_control
, save_freq_select
;
1369 unsigned long flags
;
1371 pin
= find_isa_irq_pin(8, mp_INT
);
1372 apic
= find_isa_irq_apic(8, mp_INT
);
1376 spin_lock_irqsave(&ioapic_lock
, flags
);
1377 *(((int *)&entry0
) + 1) = io_apic_read(apic
, 0x11 + 2 * pin
);
1378 *(((int *)&entry0
) + 0) = io_apic_read(apic
, 0x10 + 2 * pin
);
1379 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1380 clear_IO_APIC_pin(apic
, pin
);
1382 memset(&entry1
, 0, sizeof(entry1
));
1384 entry1
.dest_mode
= 0; /* physical delivery */
1385 entry1
.mask
= 0; /* unmask IRQ now */
1386 entry1
.dest
.physical
.physical_dest
= hard_smp_processor_id();
1387 entry1
.delivery_mode
= dest_ExtINT
;
1388 entry1
.polarity
= entry0
.polarity
;
1392 spin_lock_irqsave(&ioapic_lock
, flags
);
1393 io_apic_write(apic
, 0x11 + 2 * pin
, *(((int *)&entry1
) + 1));
1394 io_apic_write(apic
, 0x10 + 2 * pin
, *(((int *)&entry1
) + 0));
1395 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1397 save_control
= CMOS_READ(RTC_CONTROL
);
1398 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
1399 CMOS_WRITE((save_freq_select
& ~RTC_RATE_SELECT
) | 0x6,
1401 CMOS_WRITE(save_control
| RTC_PIE
, RTC_CONTROL
);
1406 if ((CMOS_READ(RTC_INTR_FLAGS
) & RTC_PF
) == RTC_PF
)
1410 CMOS_WRITE(save_control
, RTC_CONTROL
);
1411 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
1412 clear_IO_APIC_pin(apic
, pin
);
1414 spin_lock_irqsave(&ioapic_lock
, flags
);
1415 io_apic_write(apic
, 0x11 + 2 * pin
, *(((int *)&entry0
) + 1));
1416 io_apic_write(apic
, 0x10 + 2 * pin
, *(((int *)&entry0
) + 0));
1417 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1421 * This code may look a bit paranoid, but it's supposed to cooperate with
1422 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1423 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1424 * fanatically on his truly buggy board.
1426 * FIXME: really need to revamp this for modern platforms only.
1428 static inline void check_timer(void)
1430 int apic1
, pin1
, apic2
, pin2
;
1434 * get/set the timer IRQ vector:
1436 disable_8259A_irq(0);
1437 vector
= assign_irq_vector(0, TARGET_CPUS
);
1440 * Subtle, code in do_timer_interrupt() expects an AEOI
1441 * mode for the 8259A whenever interrupts are routed
1442 * through I/O APICs. Also IRQ0 has to be enabled in
1443 * the 8259A which implies the virtual wire has to be
1444 * disabled in the local APIC.
1446 apic_write(APIC_LVT0
, APIC_LVT_MASKED
| APIC_DM_EXTINT
);
1448 if (timer_over_8254
> 0)
1449 enable_8259A_irq(0);
1451 pin1
= find_isa_irq_pin(0, mp_INT
);
1452 apic1
= find_isa_irq_apic(0, mp_INT
);
1453 pin2
= ioapic_i8259
.pin
;
1454 apic2
= ioapic_i8259
.apic
;
1456 apic_printk(APIC_VERBOSE
,KERN_INFO
"..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1457 vector
, apic1
, pin1
, apic2
, pin2
);
1461 * Ok, does IRQ0 through the IOAPIC work?
1463 unmask_IO_APIC_irq(0);
1464 if (!no_timer_check
&& timer_irq_works()) {
1465 nmi_watchdog_default();
1466 if (nmi_watchdog
== NMI_IO_APIC
) {
1467 disable_8259A_irq(0);
1469 enable_8259A_irq(0);
1471 if (disable_timer_pin_1
> 0)
1472 clear_IO_APIC_pin(0, pin1
);
1475 clear_IO_APIC_pin(apic1
, pin1
);
1476 apic_printk(APIC_QUIET
,KERN_ERR
"..MP-BIOS bug: 8254 timer not "
1477 "connected to IO-APIC\n");
1480 apic_printk(APIC_VERBOSE
,KERN_INFO
"...trying to set up timer (IRQ0) "
1481 "through the 8259A ... ");
1483 apic_printk(APIC_VERBOSE
,"\n..... (found apic %d pin %d) ...",
1486 * legacy devices should be connected to IO APIC #0
1488 setup_ExtINT_IRQ0_pin(apic2
, pin2
, vector
);
1489 if (timer_irq_works()) {
1490 apic_printk(APIC_VERBOSE
," works.\n");
1491 nmi_watchdog_default();
1492 if (nmi_watchdog
== NMI_IO_APIC
) {
1498 * Cleanup, just in case ...
1500 clear_IO_APIC_pin(apic2
, pin2
);
1502 apic_printk(APIC_VERBOSE
," failed.\n");
1504 if (nmi_watchdog
== NMI_IO_APIC
) {
1505 printk(KERN_WARNING
"timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1509 apic_printk(APIC_VERBOSE
, KERN_INFO
"...trying to set up timer as Virtual Wire IRQ...");
1511 disable_8259A_irq(0);
1512 irq_desc
[0].chip
= &lapic_irq_type
;
1513 apic_write(APIC_LVT0
, APIC_DM_FIXED
| vector
); /* Fixed mode */
1514 enable_8259A_irq(0);
1516 if (timer_irq_works()) {
1517 apic_printk(APIC_VERBOSE
," works.\n");
1520 apic_write(APIC_LVT0
, APIC_LVT_MASKED
| APIC_DM_FIXED
| vector
);
1521 apic_printk(APIC_VERBOSE
," failed.\n");
1523 apic_printk(APIC_VERBOSE
, KERN_INFO
"...trying to set up timer as ExtINT IRQ...");
1527 apic_write(APIC_LVT0
, APIC_DM_EXTINT
);
1529 unlock_ExtINT_logic();
1531 if (timer_irq_works()) {
1532 apic_printk(APIC_VERBOSE
," works.\n");
1535 apic_printk(APIC_VERBOSE
," failed :(.\n");
1536 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1539 static int __init
notimercheck(char *s
)
1544 __setup("no_timer_check", notimercheck
);
1548 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1549 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1550 * Linux doesn't really care, as it's not actually used
1551 * for any interrupt handling anyway.
1553 #define PIC_IRQS (1<<2)
1555 void __init
setup_IO_APIC(void)
1560 io_apic_irqs
= ~0; /* all IRQs go through IOAPIC */
1562 io_apic_irqs
= ~PIC_IRQS
;
1564 apic_printk(APIC_VERBOSE
, "ENABLING IO-APIC IRQs\n");
1567 setup_IO_APIC_irqs();
1568 init_IO_APIC_traps();
1574 struct sysfs_ioapic_data
{
1575 struct sys_device dev
;
1576 struct IO_APIC_route_entry entry
[0];
1578 static struct sysfs_ioapic_data
* mp_ioapic_data
[MAX_IO_APICS
];
1580 static int ioapic_suspend(struct sys_device
*dev
, pm_message_t state
)
1582 struct IO_APIC_route_entry
*entry
;
1583 struct sysfs_ioapic_data
*data
;
1586 data
= container_of(dev
, struct sysfs_ioapic_data
, dev
);
1587 entry
= data
->entry
;
1588 for (i
= 0; i
< nr_ioapic_registers
[dev
->id
]; i
++, entry
++ )
1589 *entry
= ioapic_read_entry(dev
->id
, i
);
1594 static int ioapic_resume(struct sys_device
*dev
)
1596 struct IO_APIC_route_entry
*entry
;
1597 struct sysfs_ioapic_data
*data
;
1598 unsigned long flags
;
1599 union IO_APIC_reg_00 reg_00
;
1602 data
= container_of(dev
, struct sysfs_ioapic_data
, dev
);
1603 entry
= data
->entry
;
1605 spin_lock_irqsave(&ioapic_lock
, flags
);
1606 reg_00
.raw
= io_apic_read(dev
->id
, 0);
1607 if (reg_00
.bits
.ID
!= mp_ioapics
[dev
->id
].mpc_apicid
) {
1608 reg_00
.bits
.ID
= mp_ioapics
[dev
->id
].mpc_apicid
;
1609 io_apic_write(dev
->id
, 0, reg_00
.raw
);
1611 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1612 for (i
= 0; i
< nr_ioapic_registers
[dev
->id
]; i
++)
1613 ioapic_write_entry(dev
->id
, i
, entry
[i
]);
1618 static struct sysdev_class ioapic_sysdev_class
= {
1619 set_kset_name("ioapic"),
1620 .suspend
= ioapic_suspend
,
1621 .resume
= ioapic_resume
,
1624 static int __init
ioapic_init_sysfs(void)
1626 struct sys_device
* dev
;
1627 int i
, size
, error
= 0;
1629 error
= sysdev_class_register(&ioapic_sysdev_class
);
1633 for (i
= 0; i
< nr_ioapics
; i
++ ) {
1634 size
= sizeof(struct sys_device
) + nr_ioapic_registers
[i
]
1635 * sizeof(struct IO_APIC_route_entry
);
1636 mp_ioapic_data
[i
] = kmalloc(size
, GFP_KERNEL
);
1637 if (!mp_ioapic_data
[i
]) {
1638 printk(KERN_ERR
"Can't suspend/resume IOAPIC %d\n", i
);
1641 memset(mp_ioapic_data
[i
], 0, size
);
1642 dev
= &mp_ioapic_data
[i
]->dev
;
1644 dev
->cls
= &ioapic_sysdev_class
;
1645 error
= sysdev_register(dev
);
1647 kfree(mp_ioapic_data
[i
]);
1648 mp_ioapic_data
[i
] = NULL
;
1649 printk(KERN_ERR
"Can't suspend/resume IOAPIC %d\n", i
);
1657 device_initcall(ioapic_init_sysfs
);
1660 * Dynamic irq allocate and deallocation
1662 int create_irq(void)
1664 /* Allocate an unused irq */
1668 unsigned long flags
;
1671 spin_lock_irqsave(&vector_lock
, flags
);
1672 for (new = (NR_IRQS
- 1); new >= 0; new--) {
1673 if (platform_legacy_irq(new))
1675 if (irq_vector
[new] != 0)
1677 vector
= __assign_irq_vector(new, TARGET_CPUS
);
1678 if (likely(vector
> 0))
1682 spin_unlock_irqrestore(&vector_lock
, flags
);
1685 dynamic_irq_init(irq
);
1690 void destroy_irq(unsigned int irq
)
1692 unsigned long flags
;
1694 dynamic_irq_cleanup(irq
);
1696 spin_lock_irqsave(&vector_lock
, flags
);
1697 irq_vector
[irq
] = 0;
1698 spin_unlock_irqrestore(&vector_lock
, flags
);
1702 * MSI mesage composition
1704 #ifdef CONFIG_PCI_MSI
1705 static int msi_compose_msg(struct pci_dev
*pdev
, unsigned int irq
, struct msi_msg
*msg
)
1710 vector
= assign_irq_vector(irq
, TARGET_CPUS
);
1715 cpu_set(vector
>> 8, tmp
);
1716 dest
= cpu_mask_to_apicid(tmp
);
1718 msg
->address_hi
= MSI_ADDR_BASE_HI
;
1721 ((INT_DEST_MODE
== 0) ?
1722 MSI_ADDR_DEST_MODE_PHYSICAL
:
1723 MSI_ADDR_DEST_MODE_LOGICAL
) |
1724 ((INT_DELIVERY_MODE
!= dest_LowestPrio
) ?
1725 MSI_ADDR_REDIRECTION_CPU
:
1726 MSI_ADDR_REDIRECTION_LOWPRI
) |
1727 MSI_ADDR_DEST_ID(dest
);
1730 MSI_DATA_TRIGGER_EDGE
|
1731 MSI_DATA_LEVEL_ASSERT
|
1732 ((INT_DELIVERY_MODE
!= dest_LowestPrio
) ?
1733 MSI_DATA_DELIVERY_FIXED
:
1734 MSI_DATA_DELIVERY_LOWPRI
) |
1735 MSI_DATA_VECTOR(vector
);
1741 static void set_msi_irq_affinity(unsigned int irq
, cpumask_t mask
)
1748 cpus_and(tmp
, mask
, cpu_online_map
);
1749 if (cpus_empty(tmp
))
1752 cpus_and(mask
, tmp
, CPU_MASK_ALL
);
1754 vector
= assign_irq_vector(irq
, mask
);
1759 cpu_set(vector
>> 8, tmp
);
1760 dest
= cpu_mask_to_apicid(tmp
);
1762 read_msi_msg(irq
, &msg
);
1764 msg
.data
&= ~MSI_DATA_VECTOR_MASK
;
1765 msg
.data
|= MSI_DATA_VECTOR(vector
);
1766 msg
.address_lo
&= ~MSI_ADDR_DEST_ID_MASK
;
1767 msg
.address_lo
|= MSI_ADDR_DEST_ID(dest
);
1769 write_msi_msg(irq
, &msg
);
1770 set_native_irq_info(irq
, mask
);
1772 #endif /* CONFIG_SMP */
1775 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
1776 * which implement the MSI or MSI-X Capability Structure.
1778 static struct irq_chip msi_chip
= {
1780 .unmask
= unmask_msi_irq
,
1781 .mask
= mask_msi_irq
,
1782 .ack
= ack_apic_edge
,
1784 .set_affinity
= set_msi_irq_affinity
,
1786 .retrigger
= ioapic_retrigger_irq
,
1789 int arch_setup_msi_irq(unsigned int irq
, struct pci_dev
*dev
)
1793 ret
= msi_compose_msg(dev
, irq
, &msg
);
1797 write_msi_msg(irq
, &msg
);
1799 set_irq_chip_and_handler(irq
, &msi_chip
, handle_edge_irq
);
1804 void arch_teardown_msi_irq(unsigned int irq
)
1809 #endif /* CONFIG_PCI_MSI */
1812 * Hypertransport interrupt support
1814 #ifdef CONFIG_HT_IRQ
1818 static void target_ht_irq(unsigned int irq
, unsigned int dest
, u8 vector
)
1821 low
= read_ht_irq_low(irq
);
1822 high
= read_ht_irq_high(irq
);
1824 low
&= ~(HT_IRQ_LOW_VECTOR_MASK
| HT_IRQ_LOW_DEST_ID_MASK
);
1825 high
&= ~(HT_IRQ_HIGH_DEST_ID_MASK
);
1827 low
|= HT_IRQ_LOW_VECTOR(vector
) | HT_IRQ_LOW_DEST_ID(dest
);
1828 high
|= HT_IRQ_HIGH_DEST_ID(dest
);
1830 write_ht_irq_low(irq
, low
);
1831 write_ht_irq_high(irq
, high
);
1834 static void set_ht_irq_affinity(unsigned int irq
, cpumask_t mask
)
1840 cpus_and(tmp
, mask
, cpu_online_map
);
1841 if (cpus_empty(tmp
))
1844 cpus_and(mask
, tmp
, CPU_MASK_ALL
);
1846 vector
= assign_irq_vector(irq
, mask
);
1851 cpu_set(vector
>> 8, tmp
);
1852 dest
= cpu_mask_to_apicid(tmp
);
1854 target_ht_irq(irq
, dest
, vector
& 0xff);
1855 set_native_irq_info(irq
, mask
);
1859 static struct hw_interrupt_type ht_irq_chip
= {
1861 .mask
= mask_ht_irq
,
1862 .unmask
= unmask_ht_irq
,
1863 .ack
= ack_apic_edge
,
1865 .set_affinity
= set_ht_irq_affinity
,
1867 .retrigger
= ioapic_retrigger_irq
,
1870 int arch_setup_ht_irq(unsigned int irq
, struct pci_dev
*dev
)
1874 vector
= assign_irq_vector(irq
, TARGET_CPUS
);
1881 cpu_set(vector
>> 8, tmp
);
1882 dest
= cpu_mask_to_apicid(tmp
);
1884 high
= HT_IRQ_HIGH_DEST_ID(dest
);
1886 low
= HT_IRQ_LOW_BASE
|
1887 HT_IRQ_LOW_DEST_ID(dest
) |
1888 HT_IRQ_LOW_VECTOR(vector
) |
1889 ((INT_DEST_MODE
== 0) ?
1890 HT_IRQ_LOW_DM_PHYSICAL
:
1891 HT_IRQ_LOW_DM_LOGICAL
) |
1892 HT_IRQ_LOW_RQEOI_EDGE
|
1893 ((INT_DELIVERY_MODE
!= dest_LowestPrio
) ?
1894 HT_IRQ_LOW_MT_FIXED
:
1895 HT_IRQ_LOW_MT_ARBITRATED
);
1897 write_ht_irq_low(irq
, low
);
1898 write_ht_irq_high(irq
, high
);
1900 set_irq_chip_and_handler(irq
, &ht_irq_chip
, handle_edge_irq
);
1904 #endif /* CONFIG_HT_IRQ */
1906 /* --------------------------------------------------------------------------
1907 ACPI-based IOAPIC Configuration
1908 -------------------------------------------------------------------------- */
1912 #define IO_APIC_MAX_ID 0xFE
1914 int __init
io_apic_get_redir_entries (int ioapic
)
1916 union IO_APIC_reg_01 reg_01
;
1917 unsigned long flags
;
1919 spin_lock_irqsave(&ioapic_lock
, flags
);
1920 reg_01
.raw
= io_apic_read(ioapic
, 1);
1921 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1923 return reg_01
.bits
.entries
;
1927 int io_apic_set_pci_routing (int ioapic
, int pin
, int irq
, int triggering
, int polarity
)
1929 struct IO_APIC_route_entry entry
;
1930 unsigned long flags
;
1934 if (!IO_APIC_IRQ(irq
)) {
1935 apic_printk(APIC_QUIET
,KERN_ERR
"IOAPIC[%d]: Invalid reference to IRQ 0\n",
1941 * IRQs < 16 are already in the irq_2_pin[] map
1944 add_pin_to_irq(irq
, ioapic
, pin
);
1947 vector
= assign_irq_vector(irq
, TARGET_CPUS
);
1952 cpu_set(vector
>> 8, mask
);
1955 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1956 * Note that we mask (disable) IRQs now -- these get enabled when the
1957 * corresponding device driver registers for this IRQ.
1960 memset(&entry
,0,sizeof(entry
));
1962 entry
.delivery_mode
= INT_DELIVERY_MODE
;
1963 entry
.dest_mode
= INT_DEST_MODE
;
1964 entry
.dest
.logical
.logical_dest
= cpu_mask_to_apicid(mask
);
1965 entry
.trigger
= triggering
;
1966 entry
.polarity
= polarity
;
1967 entry
.mask
= 1; /* Disabled (masked) */
1968 entry
.vector
= vector
& 0xff;
1970 apic_printk(APIC_VERBOSE
,KERN_DEBUG
"IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1971 "IRQ %d Mode:%i Active:%i)\n", ioapic
,
1972 mp_ioapics
[ioapic
].mpc_apicid
, pin
, entry
.vector
, irq
,
1973 triggering
, polarity
);
1975 ioapic_register_intr(irq
, entry
.vector
, triggering
);
1977 if (!ioapic
&& (irq
< 16))
1978 disable_8259A_irq(irq
);
1980 ioapic_write_entry(ioapic
, pin
, entry
);
1982 spin_lock_irqsave(&ioapic_lock
, flags
);
1983 set_native_irq_info(irq
, TARGET_CPUS
);
1984 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1989 #endif /* CONFIG_ACPI */
1993 * This function currently is only a helper for the i386 smp boot process where
1994 * we need to reprogram the ioredtbls to cater for the cpus which have come online
1995 * so mask in all cases should simply be TARGET_CPUS
1998 void __init
setup_ioapic_dest(void)
2000 int pin
, ioapic
, irq
, irq_entry
;
2002 if (skip_ioapic_setup
== 1)
2005 for (ioapic
= 0; ioapic
< nr_ioapics
; ioapic
++) {
2006 for (pin
= 0; pin
< nr_ioapic_registers
[ioapic
]; pin
++) {
2007 irq_entry
= find_irq_entry(ioapic
, pin
, mp_INT
);
2008 if (irq_entry
== -1)
2010 irq
= pin_2_irq(irq_entry
, ioapic
, pin
);
2011 set_ioapic_affinity_irq(irq
, TARGET_CPUS
);