2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000, 2009 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/pci.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/compiler.h>
31 #include <linux/acpi.h>
32 #include <linux/module.h>
33 #include <linux/sysdev.h>
34 #include <linux/msi.h>
35 #include <linux/htirq.h>
36 #include <linux/freezer.h>
37 #include <linux/kthread.h>
38 #include <linux/jiffies.h> /* time_after() */
40 #include <acpi/acpi_bus.h>
42 #include <linux/bootmem.h>
43 #include <linux/dmar.h>
44 #include <linux/hpet.h>
51 #include <asm/proto.h>
54 #include <asm/timer.h>
55 #include <asm/i8259.h>
57 #include <asm/msidef.h>
58 #include <asm/hypertransport.h>
59 #include <asm/setup.h>
60 #include <asm/irq_remapping.h>
62 #include <asm/hw_irq.h>
63 #include <asm/uv/uv_hub.h>
64 #include <asm/uv/uv_irq.h>
68 #define __apicdebuginit(type) static type __init
69 #define for_each_irq_pin(entry, head) \
70 for (entry = head; entry; entry = entry->next)
73 * Is the SiS APIC rmw bug present ?
74 * -1 = don't know, 0 = no, 1 = yes
76 int sis_apic_bug
= -1;
78 static DEFINE_SPINLOCK(ioapic_lock
);
79 static DEFINE_SPINLOCK(vector_lock
);
82 * # of IRQ routing registers
84 int nr_ioapic_registers
[MAX_IO_APICS
];
86 /* I/O APIC entries */
87 struct mpc_ioapic mp_ioapics
[MAX_IO_APICS
];
90 /* IO APIC gsi routing info */
91 struct mp_ioapic_gsi mp_gsi_routing
[MAX_IO_APICS
];
93 /* MP IRQ source entries */
94 struct mpc_intsrc mp_irqs
[MAX_IRQ_SOURCES
];
96 /* # of MP IRQ source entries */
99 /* Number of legacy interrupts */
100 static int nr_legacy_irqs __read_mostly
= NR_IRQS_LEGACY
;
102 static int nr_irqs_gsi
= NR_IRQS_LEGACY
;
104 #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
105 int mp_bus_id_to_type
[MAX_MP_BUSSES
];
108 DECLARE_BITMAP(mp_bus_not_pci
, MAX_MP_BUSSES
);
110 int skip_ioapic_setup
;
112 void arch_disable_smp_support(void)
116 noioapicreroute
= -1;
118 skip_ioapic_setup
= 1;
121 static int __init
parse_noapic(char *str
)
123 /* disable IO-APIC */
124 arch_disable_smp_support();
127 early_param("noapic", parse_noapic
);
129 struct irq_pin_list
{
131 struct irq_pin_list
*next
;
134 static struct irq_pin_list
*get_one_free_irq_2_pin(int node
)
136 struct irq_pin_list
*pin
;
138 pin
= kzalloc_node(sizeof(*pin
), GFP_ATOMIC
, node
);
144 * This is performance-critical, we want to do it O(1)
146 * Most irqs are mapped 1:1 with pins.
149 struct irq_pin_list
*irq_2_pin
;
150 cpumask_var_t domain
;
151 cpumask_var_t old_domain
;
152 unsigned move_cleanup_count
;
154 u8 move_in_progress
: 1;
157 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
158 #ifdef CONFIG_SPARSE_IRQ
159 static struct irq_cfg irq_cfgx
[] = {
161 static struct irq_cfg irq_cfgx
[NR_IRQS
] = {
163 [0] = { .vector
= IRQ0_VECTOR
, },
164 [1] = { .vector
= IRQ1_VECTOR
, },
165 [2] = { .vector
= IRQ2_VECTOR
, },
166 [3] = { .vector
= IRQ3_VECTOR
, },
167 [4] = { .vector
= IRQ4_VECTOR
, },
168 [5] = { .vector
= IRQ5_VECTOR
, },
169 [6] = { .vector
= IRQ6_VECTOR
, },
170 [7] = { .vector
= IRQ7_VECTOR
, },
171 [8] = { .vector
= IRQ8_VECTOR
, },
172 [9] = { .vector
= IRQ9_VECTOR
, },
173 [10] = { .vector
= IRQ10_VECTOR
, },
174 [11] = { .vector
= IRQ11_VECTOR
, },
175 [12] = { .vector
= IRQ12_VECTOR
, },
176 [13] = { .vector
= IRQ13_VECTOR
, },
177 [14] = { .vector
= IRQ14_VECTOR
, },
178 [15] = { .vector
= IRQ15_VECTOR
, },
181 void __init
io_apic_disable_legacy(void)
187 int __init
arch_early_irq_init(void)
190 struct irq_desc
*desc
;
196 count
= ARRAY_SIZE(irq_cfgx
);
197 node
= cpu_to_node(boot_cpu_id
);
199 for (i
= 0; i
< count
; i
++) {
200 desc
= irq_to_desc(i
);
201 desc
->chip_data
= &cfg
[i
];
202 zalloc_cpumask_var_node(&cfg
[i
].domain
, GFP_NOWAIT
, node
);
203 zalloc_cpumask_var_node(&cfg
[i
].old_domain
, GFP_NOWAIT
, node
);
204 if (i
< nr_legacy_irqs
)
205 cpumask_setall(cfg
[i
].domain
);
211 #ifdef CONFIG_SPARSE_IRQ
212 static struct irq_cfg
*irq_cfg(unsigned int irq
)
214 struct irq_cfg
*cfg
= NULL
;
215 struct irq_desc
*desc
;
217 desc
= irq_to_desc(irq
);
219 cfg
= desc
->chip_data
;
224 static struct irq_cfg
*get_one_free_irq_cfg(int node
)
228 cfg
= kzalloc_node(sizeof(*cfg
), GFP_ATOMIC
, node
);
230 if (!zalloc_cpumask_var_node(&cfg
->domain
, GFP_ATOMIC
, node
)) {
233 } else if (!zalloc_cpumask_var_node(&cfg
->old_domain
,
235 free_cpumask_var(cfg
->domain
);
244 int arch_init_chip_data(struct irq_desc
*desc
, int node
)
248 cfg
= desc
->chip_data
;
250 desc
->chip_data
= get_one_free_irq_cfg(node
);
251 if (!desc
->chip_data
) {
252 printk(KERN_ERR
"can not alloc irq_cfg\n");
260 /* for move_irq_desc */
262 init_copy_irq_2_pin(struct irq_cfg
*old_cfg
, struct irq_cfg
*cfg
, int node
)
264 struct irq_pin_list
*old_entry
, *head
, *tail
, *entry
;
266 cfg
->irq_2_pin
= NULL
;
267 old_entry
= old_cfg
->irq_2_pin
;
271 entry
= get_one_free_irq_2_pin(node
);
275 entry
->apic
= old_entry
->apic
;
276 entry
->pin
= old_entry
->pin
;
279 old_entry
= old_entry
->next
;
281 entry
= get_one_free_irq_2_pin(node
);
289 /* still use the old one */
292 entry
->apic
= old_entry
->apic
;
293 entry
->pin
= old_entry
->pin
;
296 old_entry
= old_entry
->next
;
300 cfg
->irq_2_pin
= head
;
303 static void free_irq_2_pin(struct irq_cfg
*old_cfg
, struct irq_cfg
*cfg
)
305 struct irq_pin_list
*entry
, *next
;
307 if (old_cfg
->irq_2_pin
== cfg
->irq_2_pin
)
310 entry
= old_cfg
->irq_2_pin
;
317 old_cfg
->irq_2_pin
= NULL
;
320 void arch_init_copy_chip_data(struct irq_desc
*old_desc
,
321 struct irq_desc
*desc
, int node
)
324 struct irq_cfg
*old_cfg
;
326 cfg
= get_one_free_irq_cfg(node
);
331 desc
->chip_data
= cfg
;
333 old_cfg
= old_desc
->chip_data
;
335 memcpy(cfg
, old_cfg
, sizeof(struct irq_cfg
));
337 init_copy_irq_2_pin(old_cfg
, cfg
, node
);
340 static void free_irq_cfg(struct irq_cfg
*old_cfg
)
345 void arch_free_chip_data(struct irq_desc
*old_desc
, struct irq_desc
*desc
)
347 struct irq_cfg
*old_cfg
, *cfg
;
349 old_cfg
= old_desc
->chip_data
;
350 cfg
= desc
->chip_data
;
356 free_irq_2_pin(old_cfg
, cfg
);
357 free_irq_cfg(old_cfg
);
358 old_desc
->chip_data
= NULL
;
361 /* end for move_irq_desc */
364 static struct irq_cfg
*irq_cfg(unsigned int irq
)
366 return irq
< nr_irqs
? irq_cfgx
+ irq
: NULL
;
373 unsigned int unused
[3];
375 unsigned int unused2
[11];
379 static __attribute_const__
struct io_apic __iomem
*io_apic_base(int idx
)
381 return (void __iomem
*) __fix_to_virt(FIX_IO_APIC_BASE_0
+ idx
)
382 + (mp_ioapics
[idx
].apicaddr
& ~PAGE_MASK
);
385 static inline void io_apic_eoi(unsigned int apic
, unsigned int vector
)
387 struct io_apic __iomem
*io_apic
= io_apic_base(apic
);
388 writel(vector
, &io_apic
->eoi
);
391 static inline unsigned int io_apic_read(unsigned int apic
, unsigned int reg
)
393 struct io_apic __iomem
*io_apic
= io_apic_base(apic
);
394 writel(reg
, &io_apic
->index
);
395 return readl(&io_apic
->data
);
398 static inline void io_apic_write(unsigned int apic
, unsigned int reg
, unsigned int value
)
400 struct io_apic __iomem
*io_apic
= io_apic_base(apic
);
401 writel(reg
, &io_apic
->index
);
402 writel(value
, &io_apic
->data
);
406 * Re-write a value: to be used for read-modify-write
407 * cycles where the read already set up the index register.
409 * Older SiS APIC requires we rewrite the index register
411 static inline void io_apic_modify(unsigned int apic
, unsigned int reg
, unsigned int value
)
413 struct io_apic __iomem
*io_apic
= io_apic_base(apic
);
416 writel(reg
, &io_apic
->index
);
417 writel(value
, &io_apic
->data
);
420 static bool io_apic_level_ack_pending(struct irq_cfg
*cfg
)
422 struct irq_pin_list
*entry
;
425 spin_lock_irqsave(&ioapic_lock
, flags
);
426 for_each_irq_pin(entry
, cfg
->irq_2_pin
) {
431 reg
= io_apic_read(entry
->apic
, 0x10 + pin
*2);
432 /* Is the remote IRR bit set? */
433 if (reg
& IO_APIC_REDIR_REMOTE_IRR
) {
434 spin_unlock_irqrestore(&ioapic_lock
, flags
);
438 spin_unlock_irqrestore(&ioapic_lock
, flags
);
444 struct { u32 w1
, w2
; };
445 struct IO_APIC_route_entry entry
;
448 static struct IO_APIC_route_entry
ioapic_read_entry(int apic
, int pin
)
450 union entry_union eu
;
452 spin_lock_irqsave(&ioapic_lock
, flags
);
453 eu
.w1
= io_apic_read(apic
, 0x10 + 2 * pin
);
454 eu
.w2
= io_apic_read(apic
, 0x11 + 2 * pin
);
455 spin_unlock_irqrestore(&ioapic_lock
, flags
);
460 * When we write a new IO APIC routing entry, we need to write the high
461 * word first! If the mask bit in the low word is clear, we will enable
462 * the interrupt, and we need to make sure the entry is fully populated
463 * before that happens.
466 __ioapic_write_entry(int apic
, int pin
, struct IO_APIC_route_entry e
)
468 union entry_union eu
= {{0, 0}};
471 io_apic_write(apic
, 0x11 + 2*pin
, eu
.w2
);
472 io_apic_write(apic
, 0x10 + 2*pin
, eu
.w1
);
475 void ioapic_write_entry(int apic
, int pin
, struct IO_APIC_route_entry e
)
478 spin_lock_irqsave(&ioapic_lock
, flags
);
479 __ioapic_write_entry(apic
, pin
, e
);
480 spin_unlock_irqrestore(&ioapic_lock
, flags
);
484 * When we mask an IO APIC routing entry, we need to write the low
485 * word first, in order to set the mask bit before we change the
488 static void ioapic_mask_entry(int apic
, int pin
)
491 union entry_union eu
= { .entry
.mask
= 1 };
493 spin_lock_irqsave(&ioapic_lock
, flags
);
494 io_apic_write(apic
, 0x10 + 2*pin
, eu
.w1
);
495 io_apic_write(apic
, 0x11 + 2*pin
, eu
.w2
);
496 spin_unlock_irqrestore(&ioapic_lock
, flags
);
500 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
501 * shared ISA-space IRQs, so we have to support them. We are super
502 * fast in the common case, and fast for shared ISA-space IRQs.
505 add_pin_to_irq_node_nopanic(struct irq_cfg
*cfg
, int node
, int apic
, int pin
)
507 struct irq_pin_list
**last
, *entry
;
509 /* don't allow duplicates */
510 last
= &cfg
->irq_2_pin
;
511 for_each_irq_pin(entry
, cfg
->irq_2_pin
) {
512 if (entry
->apic
== apic
&& entry
->pin
== pin
)
517 entry
= get_one_free_irq_2_pin(node
);
519 printk(KERN_ERR
"can not alloc irq_pin_list (%d,%d,%d)\n",
530 static void add_pin_to_irq_node(struct irq_cfg
*cfg
, int node
, int apic
, int pin
)
532 if (add_pin_to_irq_node_nopanic(cfg
, node
, apic
, pin
))
533 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
537 * Reroute an IRQ to a different pin.
539 static void __init
replace_pin_at_irq_node(struct irq_cfg
*cfg
, int node
,
540 int oldapic
, int oldpin
,
541 int newapic
, int newpin
)
543 struct irq_pin_list
*entry
;
545 for_each_irq_pin(entry
, cfg
->irq_2_pin
) {
546 if (entry
->apic
== oldapic
&& entry
->pin
== oldpin
) {
547 entry
->apic
= newapic
;
549 /* every one is different, right? */
554 /* old apic/pin didn't exist, so just add new ones */
555 add_pin_to_irq_node(cfg
, node
, newapic
, newpin
);
558 static void io_apic_modify_irq(struct irq_cfg
*cfg
,
559 int mask_and
, int mask_or
,
560 void (*final
)(struct irq_pin_list
*entry
))
563 struct irq_pin_list
*entry
;
565 for_each_irq_pin(entry
, cfg
->irq_2_pin
) {
568 reg
= io_apic_read(entry
->apic
, 0x10 + pin
* 2);
571 io_apic_modify(entry
->apic
, 0x10 + pin
* 2, reg
);
577 static void __unmask_IO_APIC_irq(struct irq_cfg
*cfg
)
579 io_apic_modify_irq(cfg
, ~IO_APIC_REDIR_MASKED
, 0, NULL
);
582 static void io_apic_sync(struct irq_pin_list
*entry
)
585 * Synchronize the IO-APIC and the CPU by doing
586 * a dummy read from the IO-APIC
588 struct io_apic __iomem
*io_apic
;
589 io_apic
= io_apic_base(entry
->apic
);
590 readl(&io_apic
->data
);
593 static void __mask_IO_APIC_irq(struct irq_cfg
*cfg
)
595 io_apic_modify_irq(cfg
, ~0, IO_APIC_REDIR_MASKED
, &io_apic_sync
);
598 static void __mask_and_edge_IO_APIC_irq(struct irq_cfg
*cfg
)
600 io_apic_modify_irq(cfg
, ~IO_APIC_REDIR_LEVEL_TRIGGER
,
601 IO_APIC_REDIR_MASKED
, NULL
);
604 static void __unmask_and_level_IO_APIC_irq(struct irq_cfg
*cfg
)
606 io_apic_modify_irq(cfg
, ~IO_APIC_REDIR_MASKED
,
607 IO_APIC_REDIR_LEVEL_TRIGGER
, NULL
);
610 static void mask_IO_APIC_irq_desc(struct irq_desc
*desc
)
612 struct irq_cfg
*cfg
= desc
->chip_data
;
617 spin_lock_irqsave(&ioapic_lock
, flags
);
618 __mask_IO_APIC_irq(cfg
);
619 spin_unlock_irqrestore(&ioapic_lock
, flags
);
622 static void unmask_IO_APIC_irq_desc(struct irq_desc
*desc
)
624 struct irq_cfg
*cfg
= desc
->chip_data
;
627 spin_lock_irqsave(&ioapic_lock
, flags
);
628 __unmask_IO_APIC_irq(cfg
);
629 spin_unlock_irqrestore(&ioapic_lock
, flags
);
632 static void mask_IO_APIC_irq(unsigned int irq
)
634 struct irq_desc
*desc
= irq_to_desc(irq
);
636 mask_IO_APIC_irq_desc(desc
);
638 static void unmask_IO_APIC_irq(unsigned int irq
)
640 struct irq_desc
*desc
= irq_to_desc(irq
);
642 unmask_IO_APIC_irq_desc(desc
);
645 static void clear_IO_APIC_pin(unsigned int apic
, unsigned int pin
)
647 struct IO_APIC_route_entry entry
;
649 /* Check delivery_mode to be sure we're not clearing an SMI pin */
650 entry
= ioapic_read_entry(apic
, pin
);
651 if (entry
.delivery_mode
== dest_SMI
)
654 * Disable it in the IO-APIC irq-routing table:
656 ioapic_mask_entry(apic
, pin
);
659 static void clear_IO_APIC (void)
663 for (apic
= 0; apic
< nr_ioapics
; apic
++)
664 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++)
665 clear_IO_APIC_pin(apic
, pin
);
670 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
671 * specific CPU-side IRQs.
675 static int pirq_entries
[MAX_PIRQS
] = {
676 [0 ... MAX_PIRQS
- 1] = -1
679 static int __init
ioapic_pirq_setup(char *str
)
682 int ints
[MAX_PIRQS
+1];
684 get_options(str
, ARRAY_SIZE(ints
), ints
);
686 apic_printk(APIC_VERBOSE
, KERN_INFO
687 "PIRQ redirection, working around broken MP-BIOS.\n");
689 if (ints
[0] < MAX_PIRQS
)
692 for (i
= 0; i
< max
; i
++) {
693 apic_printk(APIC_VERBOSE
, KERN_DEBUG
694 "... PIRQ%d -> IRQ %d\n", i
, ints
[i
+1]);
696 * PIRQs are mapped upside down, usually.
698 pirq_entries
[MAX_PIRQS
-i
-1] = ints
[i
+1];
703 __setup("pirq=", ioapic_pirq_setup
);
704 #endif /* CONFIG_X86_32 */
706 struct IO_APIC_route_entry
**alloc_ioapic_entries(void)
709 struct IO_APIC_route_entry
**ioapic_entries
;
711 ioapic_entries
= kzalloc(sizeof(*ioapic_entries
) * nr_ioapics
,
716 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
717 ioapic_entries
[apic
] =
718 kzalloc(sizeof(struct IO_APIC_route_entry
) *
719 nr_ioapic_registers
[apic
], GFP_ATOMIC
);
720 if (!ioapic_entries
[apic
])
724 return ioapic_entries
;
728 kfree(ioapic_entries
[apic
]);
729 kfree(ioapic_entries
);
735 * Saves all the IO-APIC RTE's
737 int save_IO_APIC_setup(struct IO_APIC_route_entry
**ioapic_entries
)
744 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
745 if (!ioapic_entries
[apic
])
748 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++)
749 ioapic_entries
[apic
][pin
] =
750 ioapic_read_entry(apic
, pin
);
757 * Mask all IO APIC entries.
759 void mask_IO_APIC_setup(struct IO_APIC_route_entry
**ioapic_entries
)
766 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
767 if (!ioapic_entries
[apic
])
770 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++) {
771 struct IO_APIC_route_entry entry
;
773 entry
= ioapic_entries
[apic
][pin
];
776 ioapic_write_entry(apic
, pin
, entry
);
783 * Restore IO APIC entries which was saved in ioapic_entries.
785 int restore_IO_APIC_setup(struct IO_APIC_route_entry
**ioapic_entries
)
792 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
793 if (!ioapic_entries
[apic
])
796 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++)
797 ioapic_write_entry(apic
, pin
,
798 ioapic_entries
[apic
][pin
]);
803 void free_ioapic_entries(struct IO_APIC_route_entry
**ioapic_entries
)
807 for (apic
= 0; apic
< nr_ioapics
; apic
++)
808 kfree(ioapic_entries
[apic
]);
810 kfree(ioapic_entries
);
814 * Find the IRQ entry number of a certain pin.
816 static int find_irq_entry(int apic
, int pin
, int type
)
820 for (i
= 0; i
< mp_irq_entries
; i
++)
821 if (mp_irqs
[i
].irqtype
== type
&&
822 (mp_irqs
[i
].dstapic
== mp_ioapics
[apic
].apicid
||
823 mp_irqs
[i
].dstapic
== MP_APIC_ALL
) &&
824 mp_irqs
[i
].dstirq
== pin
)
831 * Find the pin to which IRQ[irq] (ISA) is connected
833 static int __init
find_isa_irq_pin(int irq
, int type
)
837 for (i
= 0; i
< mp_irq_entries
; i
++) {
838 int lbus
= mp_irqs
[i
].srcbus
;
840 if (test_bit(lbus
, mp_bus_not_pci
) &&
841 (mp_irqs
[i
].irqtype
== type
) &&
842 (mp_irqs
[i
].srcbusirq
== irq
))
844 return mp_irqs
[i
].dstirq
;
849 static int __init
find_isa_irq_apic(int irq
, int type
)
853 for (i
= 0; i
< mp_irq_entries
; i
++) {
854 int lbus
= mp_irqs
[i
].srcbus
;
856 if (test_bit(lbus
, mp_bus_not_pci
) &&
857 (mp_irqs
[i
].irqtype
== type
) &&
858 (mp_irqs
[i
].srcbusirq
== irq
))
861 if (i
< mp_irq_entries
) {
863 for(apic
= 0; apic
< nr_ioapics
; apic
++) {
864 if (mp_ioapics
[apic
].apicid
== mp_irqs
[i
].dstapic
)
872 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
874 * EISA Edge/Level control register, ELCR
876 static int EISA_ELCR(unsigned int irq
)
878 if (irq
< nr_legacy_irqs
) {
879 unsigned int port
= 0x4d0 + (irq
>> 3);
880 return (inb(port
) >> (irq
& 7)) & 1;
882 apic_printk(APIC_VERBOSE
, KERN_INFO
883 "Broken MPtable reports ISA irq %d\n", irq
);
889 /* ISA interrupts are always polarity zero edge triggered,
890 * when listed as conforming in the MP table. */
892 #define default_ISA_trigger(idx) (0)
893 #define default_ISA_polarity(idx) (0)
895 /* EISA interrupts are always polarity zero and can be edge or level
896 * trigger depending on the ELCR value. If an interrupt is listed as
897 * EISA conforming in the MP table, that means its trigger type must
898 * be read in from the ELCR */
900 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
901 #define default_EISA_polarity(idx) default_ISA_polarity(idx)
903 /* PCI interrupts are always polarity one level triggered,
904 * when listed as conforming in the MP table. */
906 #define default_PCI_trigger(idx) (1)
907 #define default_PCI_polarity(idx) (1)
909 /* MCA interrupts are always polarity zero level triggered,
910 * when listed as conforming in the MP table. */
912 #define default_MCA_trigger(idx) (1)
913 #define default_MCA_polarity(idx) default_ISA_polarity(idx)
915 static int MPBIOS_polarity(int idx
)
917 int bus
= mp_irqs
[idx
].srcbus
;
921 * Determine IRQ line polarity (high active or low active):
923 switch (mp_irqs
[idx
].irqflag
& 3)
925 case 0: /* conforms, ie. bus-type dependent polarity */
926 if (test_bit(bus
, mp_bus_not_pci
))
927 polarity
= default_ISA_polarity(idx
);
929 polarity
= default_PCI_polarity(idx
);
931 case 1: /* high active */
936 case 2: /* reserved */
938 printk(KERN_WARNING
"broken BIOS!!\n");
942 case 3: /* low active */
947 default: /* invalid */
949 printk(KERN_WARNING
"broken BIOS!!\n");
957 static int MPBIOS_trigger(int idx
)
959 int bus
= mp_irqs
[idx
].srcbus
;
963 * Determine IRQ trigger mode (edge or level sensitive):
965 switch ((mp_irqs
[idx
].irqflag
>>2) & 3)
967 case 0: /* conforms, ie. bus-type dependent */
968 if (test_bit(bus
, mp_bus_not_pci
))
969 trigger
= default_ISA_trigger(idx
);
971 trigger
= default_PCI_trigger(idx
);
972 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
973 switch (mp_bus_id_to_type
[bus
]) {
974 case MP_BUS_ISA
: /* ISA pin */
976 /* set before the switch */
979 case MP_BUS_EISA
: /* EISA pin */
981 trigger
= default_EISA_trigger(idx
);
984 case MP_BUS_PCI
: /* PCI pin */
986 /* set before the switch */
989 case MP_BUS_MCA
: /* MCA pin */
991 trigger
= default_MCA_trigger(idx
);
996 printk(KERN_WARNING
"broken BIOS!!\n");
1008 case 2: /* reserved */
1010 printk(KERN_WARNING
"broken BIOS!!\n");
1019 default: /* invalid */
1021 printk(KERN_WARNING
"broken BIOS!!\n");
1029 static inline int irq_polarity(int idx
)
1031 return MPBIOS_polarity(idx
);
1034 static inline int irq_trigger(int idx
)
1036 return MPBIOS_trigger(idx
);
1039 int (*ioapic_renumber_irq
)(int ioapic
, int irq
);
1040 static int pin_2_irq(int idx
, int apic
, int pin
)
1043 int bus
= mp_irqs
[idx
].srcbus
;
1046 * Debugging check, we are in big trouble if this message pops up!
1048 if (mp_irqs
[idx
].dstirq
!= pin
)
1049 printk(KERN_ERR
"broken BIOS or MPTABLE parser, ayiee!!\n");
1051 if (test_bit(bus
, mp_bus_not_pci
)) {
1052 irq
= mp_irqs
[idx
].srcbusirq
;
1055 * PCI IRQs are mapped in order
1059 irq
+= nr_ioapic_registers
[i
++];
1062 * For MPS mode, so far only needed by ES7000 platform
1064 if (ioapic_renumber_irq
)
1065 irq
= ioapic_renumber_irq(apic
, irq
);
1068 #ifdef CONFIG_X86_32
1070 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1072 if ((pin
>= 16) && (pin
<= 23)) {
1073 if (pirq_entries
[pin
-16] != -1) {
1074 if (!pirq_entries
[pin
-16]) {
1075 apic_printk(APIC_VERBOSE
, KERN_DEBUG
1076 "disabling PIRQ%d\n", pin
-16);
1078 irq
= pirq_entries
[pin
-16];
1079 apic_printk(APIC_VERBOSE
, KERN_DEBUG
1080 "using PIRQ%d -> IRQ %d\n",
1091 * Find a specific PCI IRQ entry.
1092 * Not an __init, possibly needed by modules
1094 int IO_APIC_get_PCI_irq_vector(int bus
, int slot
, int pin
,
1095 struct io_apic_irq_attr
*irq_attr
)
1097 int apic
, i
, best_guess
= -1;
1099 apic_printk(APIC_DEBUG
,
1100 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1102 if (test_bit(bus
, mp_bus_not_pci
)) {
1103 apic_printk(APIC_VERBOSE
,
1104 "PCI BIOS passed nonexistent PCI bus %d!\n", bus
);
1107 for (i
= 0; i
< mp_irq_entries
; i
++) {
1108 int lbus
= mp_irqs
[i
].srcbus
;
1110 for (apic
= 0; apic
< nr_ioapics
; apic
++)
1111 if (mp_ioapics
[apic
].apicid
== mp_irqs
[i
].dstapic
||
1112 mp_irqs
[i
].dstapic
== MP_APIC_ALL
)
1115 if (!test_bit(lbus
, mp_bus_not_pci
) &&
1116 !mp_irqs
[i
].irqtype
&&
1118 (slot
== ((mp_irqs
[i
].srcbusirq
>> 2) & 0x1f))) {
1119 int irq
= pin_2_irq(i
, apic
, mp_irqs
[i
].dstirq
);
1121 if (!(apic
|| IO_APIC_IRQ(irq
)))
1124 if (pin
== (mp_irqs
[i
].srcbusirq
& 3)) {
1125 set_io_apic_irq_attr(irq_attr
, apic
,
1132 * Use the first all-but-pin matching entry as a
1133 * best-guess fuzzy result for broken mptables.
1135 if (best_guess
< 0) {
1136 set_io_apic_irq_attr(irq_attr
, apic
,
1146 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector
);
1148 void lock_vector_lock(void)
1150 /* Used to the online set of cpus does not change
1151 * during assign_irq_vector.
1153 spin_lock(&vector_lock
);
1156 void unlock_vector_lock(void)
1158 spin_unlock(&vector_lock
);
1162 __assign_irq_vector(int irq
, struct irq_cfg
*cfg
, const struct cpumask
*mask
)
1165 * NOTE! The local APIC isn't very good at handling
1166 * multiple interrupts at the same interrupt level.
1167 * As the interrupt level is determined by taking the
1168 * vector number and shifting that right by 4, we
1169 * want to spread these out a bit so that they don't
1170 * all fall in the same interrupt level.
1172 * Also, we've got to be careful not to trash gate
1173 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1175 static int current_vector
= FIRST_DEVICE_VECTOR
, current_offset
= 0;
1176 unsigned int old_vector
;
1178 cpumask_var_t tmp_mask
;
1180 if ((cfg
->move_in_progress
) || cfg
->move_cleanup_count
)
1183 if (!alloc_cpumask_var(&tmp_mask
, GFP_ATOMIC
))
1186 old_vector
= cfg
->vector
;
1188 cpumask_and(tmp_mask
, mask
, cpu_online_mask
);
1189 cpumask_and(tmp_mask
, cfg
->domain
, tmp_mask
);
1190 if (!cpumask_empty(tmp_mask
)) {
1191 free_cpumask_var(tmp_mask
);
1196 /* Only try and allocate irqs on cpus that are present */
1198 for_each_cpu_and(cpu
, mask
, cpu_online_mask
) {
1202 apic
->vector_allocation_domain(cpu
, tmp_mask
);
1204 vector
= current_vector
;
1205 offset
= current_offset
;
1208 if (vector
>= first_system_vector
) {
1209 /* If out of vectors on large boxen, must share them. */
1210 offset
= (offset
+ 1) % 8;
1211 vector
= FIRST_DEVICE_VECTOR
+ offset
;
1213 if (unlikely(current_vector
== vector
))
1216 if (test_bit(vector
, used_vectors
))
1219 for_each_cpu_and(new_cpu
, tmp_mask
, cpu_online_mask
)
1220 if (per_cpu(vector_irq
, new_cpu
)[vector
] != -1)
1223 current_vector
= vector
;
1224 current_offset
= offset
;
1226 cfg
->move_in_progress
= 1;
1227 cpumask_copy(cfg
->old_domain
, cfg
->domain
);
1229 for_each_cpu_and(new_cpu
, tmp_mask
, cpu_online_mask
)
1230 per_cpu(vector_irq
, new_cpu
)[vector
] = irq
;
1231 cfg
->vector
= vector
;
1232 cpumask_copy(cfg
->domain
, tmp_mask
);
1236 free_cpumask_var(tmp_mask
);
1241 assign_irq_vector(int irq
, struct irq_cfg
*cfg
, const struct cpumask
*mask
)
1244 unsigned long flags
;
1246 spin_lock_irqsave(&vector_lock
, flags
);
1247 err
= __assign_irq_vector(irq
, cfg
, mask
);
1248 spin_unlock_irqrestore(&vector_lock
, flags
);
1252 static void __clear_irq_vector(int irq
, struct irq_cfg
*cfg
)
1256 BUG_ON(!cfg
->vector
);
1258 vector
= cfg
->vector
;
1259 for_each_cpu_and(cpu
, cfg
->domain
, cpu_online_mask
)
1260 per_cpu(vector_irq
, cpu
)[vector
] = -1;
1263 cpumask_clear(cfg
->domain
);
1265 if (likely(!cfg
->move_in_progress
))
1267 for_each_cpu_and(cpu
, cfg
->old_domain
, cpu_online_mask
) {
1268 for (vector
= FIRST_EXTERNAL_VECTOR
; vector
< NR_VECTORS
;
1270 if (per_cpu(vector_irq
, cpu
)[vector
] != irq
)
1272 per_cpu(vector_irq
, cpu
)[vector
] = -1;
1276 cfg
->move_in_progress
= 0;
1279 void __setup_vector_irq(int cpu
)
1281 /* Initialize vector_irq on a new cpu */
1282 /* This function must be called with vector_lock held */
1284 struct irq_cfg
*cfg
;
1285 struct irq_desc
*desc
;
1287 /* Mark the inuse vectors */
1288 for_each_irq_desc(irq
, desc
) {
1289 cfg
= desc
->chip_data
;
1290 if (!cpumask_test_cpu(cpu
, cfg
->domain
))
1292 vector
= cfg
->vector
;
1293 per_cpu(vector_irq
, cpu
)[vector
] = irq
;
1295 /* Mark the free vectors */
1296 for (vector
= 0; vector
< NR_VECTORS
; ++vector
) {
1297 irq
= per_cpu(vector_irq
, cpu
)[vector
];
1302 if (!cpumask_test_cpu(cpu
, cfg
->domain
))
1303 per_cpu(vector_irq
, cpu
)[vector
] = -1;
1307 static struct irq_chip ioapic_chip
;
1308 static struct irq_chip ir_ioapic_chip
;
1310 #define IOAPIC_AUTO -1
1311 #define IOAPIC_EDGE 0
1312 #define IOAPIC_LEVEL 1
1314 #ifdef CONFIG_X86_32
1315 static inline int IO_APIC_irq_trigger(int irq
)
1319 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
1320 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++) {
1321 idx
= find_irq_entry(apic
, pin
, mp_INT
);
1322 if ((idx
!= -1) && (irq
== pin_2_irq(idx
, apic
, pin
)))
1323 return irq_trigger(idx
);
1327 * nonexistent IRQs are edge default
1332 static inline int IO_APIC_irq_trigger(int irq
)
1338 static void ioapic_register_intr(int irq
, struct irq_desc
*desc
, unsigned long trigger
)
1341 if ((trigger
== IOAPIC_AUTO
&& IO_APIC_irq_trigger(irq
)) ||
1342 trigger
== IOAPIC_LEVEL
)
1343 desc
->status
|= IRQ_LEVEL
;
1345 desc
->status
&= ~IRQ_LEVEL
;
1347 if (irq_remapped(irq
)) {
1348 desc
->status
|= IRQ_MOVE_PCNTXT
;
1350 set_irq_chip_and_handler_name(irq
, &ir_ioapic_chip
,
1354 set_irq_chip_and_handler_name(irq
, &ir_ioapic_chip
,
1355 handle_edge_irq
, "edge");
1359 if ((trigger
== IOAPIC_AUTO
&& IO_APIC_irq_trigger(irq
)) ||
1360 trigger
== IOAPIC_LEVEL
)
1361 set_irq_chip_and_handler_name(irq
, &ioapic_chip
,
1365 set_irq_chip_and_handler_name(irq
, &ioapic_chip
,
1366 handle_edge_irq
, "edge");
1369 int setup_ioapic_entry(int apic_id
, int irq
,
1370 struct IO_APIC_route_entry
*entry
,
1371 unsigned int destination
, int trigger
,
1372 int polarity
, int vector
, int pin
)
1375 * add it to the IO-APIC irq-routing table:
1377 memset(entry
,0,sizeof(*entry
));
1379 if (intr_remapping_enabled
) {
1380 struct intel_iommu
*iommu
= map_ioapic_to_ir(apic_id
);
1382 struct IR_IO_APIC_route_entry
*ir_entry
=
1383 (struct IR_IO_APIC_route_entry
*) entry
;
1387 panic("No mapping iommu for ioapic %d\n", apic_id
);
1389 index
= alloc_irte(iommu
, irq
, 1);
1391 panic("Failed to allocate IRTE for ioapic %d\n", apic_id
);
1393 memset(&irte
, 0, sizeof(irte
));
1396 irte
.dst_mode
= apic
->irq_dest_mode
;
1398 * Trigger mode in the IRTE will always be edge, and the
1399 * actual level or edge trigger will be setup in the IO-APIC
1400 * RTE. This will help simplify level triggered irq migration.
1401 * For more details, see the comments above explainig IO-APIC
1402 * irq migration in the presence of interrupt-remapping.
1404 irte
.trigger_mode
= 0;
1405 irte
.dlvry_mode
= apic
->irq_delivery_mode
;
1406 irte
.vector
= vector
;
1407 irte
.dest_id
= IRTE_DEST(destination
);
1409 /* Set source-id of interrupt request */
1410 set_ioapic_sid(&irte
, apic_id
);
1412 modify_irte(irq
, &irte
);
1414 ir_entry
->index2
= (index
>> 15) & 0x1;
1416 ir_entry
->format
= 1;
1417 ir_entry
->index
= (index
& 0x7fff);
1419 * IO-APIC RTE will be configured with virtual vector.
1420 * irq handler will do the explicit EOI to the io-apic.
1422 ir_entry
->vector
= pin
;
1424 entry
->delivery_mode
= apic
->irq_delivery_mode
;
1425 entry
->dest_mode
= apic
->irq_dest_mode
;
1426 entry
->dest
= destination
;
1427 entry
->vector
= vector
;
1430 entry
->mask
= 0; /* enable IRQ */
1431 entry
->trigger
= trigger
;
1432 entry
->polarity
= polarity
;
1434 /* Mask level triggered irqs.
1435 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1442 static void setup_IO_APIC_irq(int apic_id
, int pin
, unsigned int irq
, struct irq_desc
*desc
,
1443 int trigger
, int polarity
)
1445 struct irq_cfg
*cfg
;
1446 struct IO_APIC_route_entry entry
;
1449 if (!IO_APIC_IRQ(irq
))
1452 cfg
= desc
->chip_data
;
1454 if (assign_irq_vector(irq
, cfg
, apic
->target_cpus()))
1457 dest
= apic
->cpu_mask_to_apicid_and(cfg
->domain
, apic
->target_cpus());
1459 apic_printk(APIC_VERBOSE
,KERN_DEBUG
1460 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1461 "IRQ %d Mode:%i Active:%i)\n",
1462 apic_id
, mp_ioapics
[apic_id
].apicid
, pin
, cfg
->vector
,
1463 irq
, trigger
, polarity
);
1466 if (setup_ioapic_entry(mp_ioapics
[apic_id
].apicid
, irq
, &entry
,
1467 dest
, trigger
, polarity
, cfg
->vector
, pin
)) {
1468 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1469 mp_ioapics
[apic_id
].apicid
, pin
);
1470 __clear_irq_vector(irq
, cfg
);
1474 ioapic_register_intr(irq
, desc
, trigger
);
1475 if (irq
< nr_legacy_irqs
)
1476 disable_8259A_irq(irq
);
1478 ioapic_write_entry(apic_id
, pin
, entry
);
1482 DECLARE_BITMAP(pin_programmed
, MP_MAX_IOAPIC_PIN
+ 1);
1483 } mp_ioapic_routing
[MAX_IO_APICS
];
1485 static void __init
setup_IO_APIC_irqs(void)
1487 int apic_id
= 0, pin
, idx
, irq
;
1489 struct irq_desc
*desc
;
1490 struct irq_cfg
*cfg
;
1491 int node
= cpu_to_node(boot_cpu_id
);
1493 apic_printk(APIC_VERBOSE
, KERN_DEBUG
"init IO_APIC IRQs\n");
1496 if (!acpi_disabled
&& acpi_ioapic
) {
1497 apic_id
= mp_find_ioapic(0);
1503 for (pin
= 0; pin
< nr_ioapic_registers
[apic_id
]; pin
++) {
1504 idx
= find_irq_entry(apic_id
, pin
, mp_INT
);
1508 apic_printk(APIC_VERBOSE
,
1509 KERN_DEBUG
" %d-%d",
1510 mp_ioapics
[apic_id
].apicid
, pin
);
1512 apic_printk(APIC_VERBOSE
, " %d-%d",
1513 mp_ioapics
[apic_id
].apicid
, pin
);
1517 apic_printk(APIC_VERBOSE
,
1518 " (apicid-pin) not connected\n");
1522 irq
= pin_2_irq(idx
, apic_id
, pin
);
1525 * Skip the timer IRQ if there's a quirk handler
1526 * installed and if it returns 1:
1528 if (apic
->multi_timer_check
&&
1529 apic
->multi_timer_check(apic_id
, irq
))
1532 desc
= irq_to_desc_alloc_node(irq
, node
);
1534 printk(KERN_INFO
"can not get irq_desc for %d\n", irq
);
1537 cfg
= desc
->chip_data
;
1538 add_pin_to_irq_node(cfg
, node
, apic_id
, pin
);
1540 * don't mark it in pin_programmed, so later acpi could
1541 * set it correctly when irq < 16
1543 setup_IO_APIC_irq(apic_id
, pin
, irq
, desc
,
1544 irq_trigger(idx
), irq_polarity(idx
));
1548 apic_printk(APIC_VERBOSE
,
1549 " (apicid-pin) not connected\n");
1553 * Set up the timer pin, possibly with the 8259A-master behind.
1555 static void __init
setup_timer_IRQ0_pin(unsigned int apic_id
, unsigned int pin
,
1558 struct IO_APIC_route_entry entry
;
1560 if (intr_remapping_enabled
)
1563 memset(&entry
, 0, sizeof(entry
));
1566 * We use logical delivery to get the timer IRQ
1569 entry
.dest_mode
= apic
->irq_dest_mode
;
1570 entry
.mask
= 0; /* don't mask IRQ for edge */
1571 entry
.dest
= apic
->cpu_mask_to_apicid(apic
->target_cpus());
1572 entry
.delivery_mode
= apic
->irq_delivery_mode
;
1575 entry
.vector
= vector
;
1578 * The timer IRQ doesn't have to know that behind the
1579 * scene we may have a 8259A-master in AEOI mode ...
1581 set_irq_chip_and_handler_name(0, &ioapic_chip
, handle_edge_irq
, "edge");
1584 * Add it to the IO-APIC irq-routing table:
1586 ioapic_write_entry(apic_id
, pin
, entry
);
1590 __apicdebuginit(void) print_IO_APIC(void)
1593 union IO_APIC_reg_00 reg_00
;
1594 union IO_APIC_reg_01 reg_01
;
1595 union IO_APIC_reg_02 reg_02
;
1596 union IO_APIC_reg_03 reg_03
;
1597 unsigned long flags
;
1598 struct irq_cfg
*cfg
;
1599 struct irq_desc
*desc
;
1602 if (apic_verbosity
== APIC_QUIET
)
1605 printk(KERN_DEBUG
"number of MP IRQ sources: %d.\n", mp_irq_entries
);
1606 for (i
= 0; i
< nr_ioapics
; i
++)
1607 printk(KERN_DEBUG
"number of IO-APIC #%d registers: %d.\n",
1608 mp_ioapics
[i
].apicid
, nr_ioapic_registers
[i
]);
1611 * We are a bit conservative about what we expect. We have to
1612 * know about every hardware change ASAP.
1614 printk(KERN_INFO
"testing the IO APIC.......................\n");
1616 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
1618 spin_lock_irqsave(&ioapic_lock
, flags
);
1619 reg_00
.raw
= io_apic_read(apic
, 0);
1620 reg_01
.raw
= io_apic_read(apic
, 1);
1621 if (reg_01
.bits
.version
>= 0x10)
1622 reg_02
.raw
= io_apic_read(apic
, 2);
1623 if (reg_01
.bits
.version
>= 0x20)
1624 reg_03
.raw
= io_apic_read(apic
, 3);
1625 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1628 printk(KERN_DEBUG
"IO APIC #%d......\n", mp_ioapics
[apic
].apicid
);
1629 printk(KERN_DEBUG
".... register #00: %08X\n", reg_00
.raw
);
1630 printk(KERN_DEBUG
"....... : physical APIC id: %02X\n", reg_00
.bits
.ID
);
1631 printk(KERN_DEBUG
"....... : Delivery Type: %X\n", reg_00
.bits
.delivery_type
);
1632 printk(KERN_DEBUG
"....... : LTS : %X\n", reg_00
.bits
.LTS
);
1634 printk(KERN_DEBUG
".... register #01: %08X\n", *(int *)®_01
);
1635 printk(KERN_DEBUG
"....... : max redirection entries: %04X\n", reg_01
.bits
.entries
);
1637 printk(KERN_DEBUG
"....... : PRQ implemented: %X\n", reg_01
.bits
.PRQ
);
1638 printk(KERN_DEBUG
"....... : IO APIC version: %04X\n", reg_01
.bits
.version
);
1641 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1642 * but the value of reg_02 is read as the previous read register
1643 * value, so ignore it if reg_02 == reg_01.
1645 if (reg_01
.bits
.version
>= 0x10 && reg_02
.raw
!= reg_01
.raw
) {
1646 printk(KERN_DEBUG
".... register #02: %08X\n", reg_02
.raw
);
1647 printk(KERN_DEBUG
"....... : arbitration: %02X\n", reg_02
.bits
.arbitration
);
1651 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1652 * or reg_03, but the value of reg_0[23] is read as the previous read
1653 * register value, so ignore it if reg_03 == reg_0[12].
1655 if (reg_01
.bits
.version
>= 0x20 && reg_03
.raw
!= reg_02
.raw
&&
1656 reg_03
.raw
!= reg_01
.raw
) {
1657 printk(KERN_DEBUG
".... register #03: %08X\n", reg_03
.raw
);
1658 printk(KERN_DEBUG
"....... : Boot DT : %X\n", reg_03
.bits
.boot_DT
);
1661 printk(KERN_DEBUG
".... IRQ redirection table:\n");
1663 printk(KERN_DEBUG
" NR Dst Mask Trig IRR Pol"
1664 " Stat Dmod Deli Vect: \n");
1666 for (i
= 0; i
<= reg_01
.bits
.entries
; i
++) {
1667 struct IO_APIC_route_entry entry
;
1669 entry
= ioapic_read_entry(apic
, i
);
1671 printk(KERN_DEBUG
" %02x %03X ",
1676 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1681 entry
.delivery_status
,
1683 entry
.delivery_mode
,
1688 printk(KERN_DEBUG
"IRQ to pin mappings:\n");
1689 for_each_irq_desc(irq
, desc
) {
1690 struct irq_pin_list
*entry
;
1692 cfg
= desc
->chip_data
;
1693 entry
= cfg
->irq_2_pin
;
1696 printk(KERN_DEBUG
"IRQ%d ", irq
);
1697 for_each_irq_pin(entry
, cfg
->irq_2_pin
)
1698 printk("-> %d:%d", entry
->apic
, entry
->pin
);
1702 printk(KERN_INFO
".................................... done.\n");
1707 __apicdebuginit(void) print_APIC_field(int base
)
1711 if (apic_verbosity
== APIC_QUIET
)
1716 for (i
= 0; i
< 8; i
++)
1717 printk(KERN_CONT
"%08x", apic_read(base
+ i
*0x10));
1719 printk(KERN_CONT
"\n");
1722 __apicdebuginit(void) print_local_APIC(void *dummy
)
1724 unsigned int i
, v
, ver
, maxlvt
;
1727 if (apic_verbosity
== APIC_QUIET
)
1730 printk(KERN_DEBUG
"printing local APIC contents on CPU#%d/%d:\n",
1731 smp_processor_id(), hard_smp_processor_id());
1732 v
= apic_read(APIC_ID
);
1733 printk(KERN_INFO
"... APIC ID: %08x (%01x)\n", v
, read_apic_id());
1734 v
= apic_read(APIC_LVR
);
1735 printk(KERN_INFO
"... APIC VERSION: %08x\n", v
);
1736 ver
= GET_APIC_VERSION(v
);
1737 maxlvt
= lapic_get_maxlvt();
1739 v
= apic_read(APIC_TASKPRI
);
1740 printk(KERN_DEBUG
"... APIC TASKPRI: %08x (%02x)\n", v
, v
& APIC_TPRI_MASK
);
1742 if (APIC_INTEGRATED(ver
)) { /* !82489DX */
1743 if (!APIC_XAPIC(ver
)) {
1744 v
= apic_read(APIC_ARBPRI
);
1745 printk(KERN_DEBUG
"... APIC ARBPRI: %08x (%02x)\n", v
,
1746 v
& APIC_ARBPRI_MASK
);
1748 v
= apic_read(APIC_PROCPRI
);
1749 printk(KERN_DEBUG
"... APIC PROCPRI: %08x\n", v
);
1753 * Remote read supported only in the 82489DX and local APIC for
1754 * Pentium processors.
1756 if (!APIC_INTEGRATED(ver
) || maxlvt
== 3) {
1757 v
= apic_read(APIC_RRR
);
1758 printk(KERN_DEBUG
"... APIC RRR: %08x\n", v
);
1761 v
= apic_read(APIC_LDR
);
1762 printk(KERN_DEBUG
"... APIC LDR: %08x\n", v
);
1763 if (!x2apic_enabled()) {
1764 v
= apic_read(APIC_DFR
);
1765 printk(KERN_DEBUG
"... APIC DFR: %08x\n", v
);
1767 v
= apic_read(APIC_SPIV
);
1768 printk(KERN_DEBUG
"... APIC SPIV: %08x\n", v
);
1770 printk(KERN_DEBUG
"... APIC ISR field:\n");
1771 print_APIC_field(APIC_ISR
);
1772 printk(KERN_DEBUG
"... APIC TMR field:\n");
1773 print_APIC_field(APIC_TMR
);
1774 printk(KERN_DEBUG
"... APIC IRR field:\n");
1775 print_APIC_field(APIC_IRR
);
1777 if (APIC_INTEGRATED(ver
)) { /* !82489DX */
1778 if (maxlvt
> 3) /* Due to the Pentium erratum 3AP. */
1779 apic_write(APIC_ESR
, 0);
1781 v
= apic_read(APIC_ESR
);
1782 printk(KERN_DEBUG
"... APIC ESR: %08x\n", v
);
1785 icr
= apic_icr_read();
1786 printk(KERN_DEBUG
"... APIC ICR: %08x\n", (u32
)icr
);
1787 printk(KERN_DEBUG
"... APIC ICR2: %08x\n", (u32
)(icr
>> 32));
1789 v
= apic_read(APIC_LVTT
);
1790 printk(KERN_DEBUG
"... APIC LVTT: %08x\n", v
);
1792 if (maxlvt
> 3) { /* PC is LVT#4. */
1793 v
= apic_read(APIC_LVTPC
);
1794 printk(KERN_DEBUG
"... APIC LVTPC: %08x\n", v
);
1796 v
= apic_read(APIC_LVT0
);
1797 printk(KERN_DEBUG
"... APIC LVT0: %08x\n", v
);
1798 v
= apic_read(APIC_LVT1
);
1799 printk(KERN_DEBUG
"... APIC LVT1: %08x\n", v
);
1801 if (maxlvt
> 2) { /* ERR is LVT#3. */
1802 v
= apic_read(APIC_LVTERR
);
1803 printk(KERN_DEBUG
"... APIC LVTERR: %08x\n", v
);
1806 v
= apic_read(APIC_TMICT
);
1807 printk(KERN_DEBUG
"... APIC TMICT: %08x\n", v
);
1808 v
= apic_read(APIC_TMCCT
);
1809 printk(KERN_DEBUG
"... APIC TMCCT: %08x\n", v
);
1810 v
= apic_read(APIC_TDCR
);
1811 printk(KERN_DEBUG
"... APIC TDCR: %08x\n", v
);
1813 if (boot_cpu_has(X86_FEATURE_EXTAPIC
)) {
1814 v
= apic_read(APIC_EFEAT
);
1815 maxlvt
= (v
>> 16) & 0xff;
1816 printk(KERN_DEBUG
"... APIC EFEAT: %08x\n", v
);
1817 v
= apic_read(APIC_ECTRL
);
1818 printk(KERN_DEBUG
"... APIC ECTRL: %08x\n", v
);
1819 for (i
= 0; i
< maxlvt
; i
++) {
1820 v
= apic_read(APIC_EILVTn(i
));
1821 printk(KERN_DEBUG
"... APIC EILVT%d: %08x\n", i
, v
);
1827 __apicdebuginit(void) print_all_local_APICs(void)
1832 for_each_online_cpu(cpu
)
1833 smp_call_function_single(cpu
, print_local_APIC
, NULL
, 1);
1837 __apicdebuginit(void) print_PIC(void)
1840 unsigned long flags
;
1842 if (apic_verbosity
== APIC_QUIET
|| !nr_legacy_irqs
)
1845 printk(KERN_DEBUG
"\nprinting PIC contents\n");
1847 spin_lock_irqsave(&i8259A_lock
, flags
);
1849 v
= inb(0xa1) << 8 | inb(0x21);
1850 printk(KERN_DEBUG
"... PIC IMR: %04x\n", v
);
1852 v
= inb(0xa0) << 8 | inb(0x20);
1853 printk(KERN_DEBUG
"... PIC IRR: %04x\n", v
);
1857 v
= inb(0xa0) << 8 | inb(0x20);
1861 spin_unlock_irqrestore(&i8259A_lock
, flags
);
1863 printk(KERN_DEBUG
"... PIC ISR: %04x\n", v
);
1865 v
= inb(0x4d1) << 8 | inb(0x4d0);
1866 printk(KERN_DEBUG
"... PIC ELCR: %04x\n", v
);
1869 __apicdebuginit(int) print_all_ICs(void)
1873 /* don't print out if apic is not there */
1874 if (!cpu_has_apic
&& !apic_from_smp_config())
1877 print_all_local_APICs();
1883 fs_initcall(print_all_ICs
);
1886 /* Where if anywhere is the i8259 connect in external int mode */
1887 static struct { int pin
, apic
; } ioapic_i8259
= { -1, -1 };
1889 void __init
enable_IO_APIC(void)
1891 union IO_APIC_reg_01 reg_01
;
1892 int i8259_apic
, i8259_pin
;
1894 unsigned long flags
;
1897 * The number of IO-APIC IRQ registers (== #pins):
1899 for (apic
= 0; apic
< nr_ioapics
; apic
++) {
1900 spin_lock_irqsave(&ioapic_lock
, flags
);
1901 reg_01
.raw
= io_apic_read(apic
, 1);
1902 spin_unlock_irqrestore(&ioapic_lock
, flags
);
1903 nr_ioapic_registers
[apic
] = reg_01
.bits
.entries
+1;
1906 if (!nr_legacy_irqs
)
1909 for(apic
= 0; apic
< nr_ioapics
; apic
++) {
1911 /* See if any of the pins is in ExtINT mode */
1912 for (pin
= 0; pin
< nr_ioapic_registers
[apic
]; pin
++) {
1913 struct IO_APIC_route_entry entry
;
1914 entry
= ioapic_read_entry(apic
, pin
);
1916 /* If the interrupt line is enabled and in ExtInt mode
1917 * I have found the pin where the i8259 is connected.
1919 if ((entry
.mask
== 0) && (entry
.delivery_mode
== dest_ExtINT
)) {
1920 ioapic_i8259
.apic
= apic
;
1921 ioapic_i8259
.pin
= pin
;
1927 /* Look to see what if the MP table has reported the ExtINT */
1928 /* If we could not find the appropriate pin by looking at the ioapic
1929 * the i8259 probably is not connected the ioapic but give the
1930 * mptable a chance anyway.
1932 i8259_pin
= find_isa_irq_pin(0, mp_ExtINT
);
1933 i8259_apic
= find_isa_irq_apic(0, mp_ExtINT
);
1934 /* Trust the MP table if nothing is setup in the hardware */
1935 if ((ioapic_i8259
.pin
== -1) && (i8259_pin
>= 0)) {
1936 printk(KERN_WARNING
"ExtINT not setup in hardware but reported by MP table\n");
1937 ioapic_i8259
.pin
= i8259_pin
;
1938 ioapic_i8259
.apic
= i8259_apic
;
1940 /* Complain if the MP table and the hardware disagree */
1941 if (((ioapic_i8259
.apic
!= i8259_apic
) || (ioapic_i8259
.pin
!= i8259_pin
)) &&
1942 (i8259_pin
>= 0) && (ioapic_i8259
.pin
>= 0))
1944 printk(KERN_WARNING
"ExtINT in hardware and MP table differ\n");
1948 * Do not trust the IO-APIC being empty at bootup
1954 * Not an __init, needed by the reboot code
1956 void disable_IO_APIC(void)
1959 * Clear the IO-APIC before rebooting:
1963 if (!nr_legacy_irqs
)
1967 * If the i8259 is routed through an IOAPIC
1968 * Put that IOAPIC in virtual wire mode
1969 * so legacy interrupts can be delivered.
1971 * With interrupt-remapping, for now we will use virtual wire A mode,
1972 * as virtual wire B is little complex (need to configure both
1973 * IOAPIC RTE aswell as interrupt-remapping table entry).
1974 * As this gets called during crash dump, keep this simple for now.
1976 if (ioapic_i8259
.pin
!= -1 && !intr_remapping_enabled
) {
1977 struct IO_APIC_route_entry entry
;
1979 memset(&entry
, 0, sizeof(entry
));
1980 entry
.mask
= 0; /* Enabled */
1981 entry
.trigger
= 0; /* Edge */
1983 entry
.polarity
= 0; /* High */
1984 entry
.delivery_status
= 0;
1985 entry
.dest_mode
= 0; /* Physical */
1986 entry
.delivery_mode
= dest_ExtINT
; /* ExtInt */
1988 entry
.dest
= read_apic_id();
1991 * Add it to the IO-APIC irq-routing table:
1993 ioapic_write_entry(ioapic_i8259
.apic
, ioapic_i8259
.pin
, entry
);
1997 * Use virtual wire A mode when interrupt remapping is enabled.
1999 if (cpu_has_apic
|| apic_from_smp_config())
2000 disconnect_bsp_APIC(!intr_remapping_enabled
&&
2001 ioapic_i8259
.pin
!= -1);
2004 #ifdef CONFIG_X86_32
2006 * function to set the IO-APIC physical IDs based on the
2007 * values stored in the MPC table.
2009 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2012 void __init
setup_ioapic_ids_from_mpc(void)
2014 union IO_APIC_reg_00 reg_00
;
2015 physid_mask_t phys_id_present_map
;
2018 unsigned char old_id
;
2019 unsigned long flags
;
2024 * Don't check I/O APIC IDs for xAPIC systems. They have
2025 * no meaning without the serial APIC bus.
2027 if (!(boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
)
2028 || APIC_XAPIC(apic_version
[boot_cpu_physical_apicid
]))
2031 * This is broken; anything with a real cpu count has to
2032 * circumvent this idiocy regardless.
2034 phys_id_present_map
= apic
->ioapic_phys_id_map(phys_cpu_present_map
);
2037 * Set the IOAPIC ID to the value stored in the MPC table.
2039 for (apic_id
= 0; apic_id
< nr_ioapics
; apic_id
++) {
2041 /* Read the register 0 value */
2042 spin_lock_irqsave(&ioapic_lock
, flags
);
2043 reg_00
.raw
= io_apic_read(apic_id
, 0);
2044 spin_unlock_irqrestore(&ioapic_lock
, flags
);
2046 old_id
= mp_ioapics
[apic_id
].apicid
;
2048 if (mp_ioapics
[apic_id
].apicid
>= get_physical_broadcast()) {
2049 printk(KERN_ERR
"BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
2050 apic_id
, mp_ioapics
[apic_id
].apicid
);
2051 printk(KERN_ERR
"... fixing up to %d. (tell your hw vendor)\n",
2053 mp_ioapics
[apic_id
].apicid
= reg_00
.bits
.ID
;
2057 * Sanity check, is the ID really free? Every APIC in a
2058 * system must have a unique ID or we get lots of nice
2059 * 'stuck on smp_invalidate_needed IPI wait' messages.
2061 if (apic
->check_apicid_used(phys_id_present_map
,
2062 mp_ioapics
[apic_id
].apicid
)) {
2063 printk(KERN_ERR
"BIOS bug, IO-APIC#%d ID %d is already used!...\n",
2064 apic_id
, mp_ioapics
[apic_id
].apicid
);
2065 for (i
= 0; i
< get_physical_broadcast(); i
++)
2066 if (!physid_isset(i
, phys_id_present_map
))
2068 if (i
>= get_physical_broadcast())
2069 panic("Max APIC ID exceeded!\n");
2070 printk(KERN_ERR
"... fixing up to %d. (tell your hw vendor)\n",
2072 physid_set(i
, phys_id_present_map
);
2073 mp_ioapics
[apic_id
].apicid
= i
;
2076 tmp
= apic
->apicid_to_cpu_present(mp_ioapics
[apic_id
].apicid
);
2077 apic_printk(APIC_VERBOSE
, "Setting %d in the "
2078 "phys_id_present_map\n",
2079 mp_ioapics
[apic_id
].apicid
);
2080 physids_or(phys_id_present_map
, phys_id_present_map
, tmp
);
2085 * We need to adjust the IRQ routing table
2086 * if the ID changed.
2088 if (old_id
!= mp_ioapics
[apic_id
].apicid
)
2089 for (i
= 0; i
< mp_irq_entries
; i
++)
2090 if (mp_irqs
[i
].dstapic
== old_id
)
2092 = mp_ioapics
[apic_id
].apicid
;
2095 * Read the right value from the MPC table and
2096 * write it into the ID register.
2098 apic_printk(APIC_VERBOSE
, KERN_INFO
2099 "...changing IO-APIC physical APIC ID to %d ...",
2100 mp_ioapics
[apic_id
].apicid
);
2102 reg_00
.bits
.ID
= mp_ioapics
[apic_id
].apicid
;
2103 spin_lock_irqsave(&ioapic_lock
, flags
);
2104 io_apic_write(apic_id
, 0, reg_00
.raw
);
2105 spin_unlock_irqrestore(&ioapic_lock
, flags
);
2110 spin_lock_irqsave(&ioapic_lock
, flags
);
2111 reg_00
.raw
= io_apic_read(apic_id
, 0);
2112 spin_unlock_irqrestore(&ioapic_lock
, flags
);
2113 if (reg_00
.bits
.ID
!= mp_ioapics
[apic_id
].apicid
)
2114 printk("could not set ID!\n");
2116 apic_printk(APIC_VERBOSE
, " ok.\n");
2121 int no_timer_check __initdata
;
2123 static int __init
notimercheck(char *s
)
2128 __setup("no_timer_check", notimercheck
);
2131 * There is a nasty bug in some older SMP boards, their mptable lies
2132 * about the timer IRQ. We do the following to work around the situation:
2134 * - timer IRQ defaults to IO-APIC IRQ
2135 * - if this function detects that timer IRQs are defunct, then we fall
2136 * back to ISA timer IRQs
2138 static int __init
timer_irq_works(void)
2140 unsigned long t1
= jiffies
;
2141 unsigned long flags
;
2146 local_save_flags(flags
);
2148 /* Let ten ticks pass... */
2149 mdelay((10 * 1000) / HZ
);
2150 local_irq_restore(flags
);
2153 * Expect a few ticks at least, to be sure some possible
2154 * glue logic does not lock up after one or two first
2155 * ticks in a non-ExtINT mode. Also the local APIC
2156 * might have cached one ExtINT interrupt. Finally, at
2157 * least one tick may be lost due to delays.
2161 if (time_after(jiffies
, t1
+ 4))
2167 * In the SMP+IOAPIC case it might happen that there are an unspecified
2168 * number of pending IRQ events unhandled. These cases are very rare,
2169 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2170 * better to do it this way as thus we do not have to be aware of
2171 * 'pending' interrupts in the IRQ path, except at this point.
2174 * Edge triggered needs to resend any interrupt
2175 * that was delayed but this is now handled in the device
2180 * Starting up a edge-triggered IO-APIC interrupt is
2181 * nasty - we need to make sure that we get the edge.
2182 * If it is already asserted for some reason, we need
2183 * return 1 to indicate that is was pending.
2185 * This is not complete - we should be able to fake
2186 * an edge even if it isn't on the 8259A...
2189 static unsigned int startup_ioapic_irq(unsigned int irq
)
2191 int was_pending
= 0;
2192 unsigned long flags
;
2193 struct irq_cfg
*cfg
;
2195 spin_lock_irqsave(&ioapic_lock
, flags
);
2196 if (irq
< nr_legacy_irqs
) {
2197 disable_8259A_irq(irq
);
2198 if (i8259A_irq_pending(irq
))
2202 __unmask_IO_APIC_irq(cfg
);
2203 spin_unlock_irqrestore(&ioapic_lock
, flags
);
2208 static int ioapic_retrigger_irq(unsigned int irq
)
2211 struct irq_cfg
*cfg
= irq_cfg(irq
);
2212 unsigned long flags
;
2214 spin_lock_irqsave(&vector_lock
, flags
);
2215 apic
->send_IPI_mask(cpumask_of(cpumask_first(cfg
->domain
)), cfg
->vector
);
2216 spin_unlock_irqrestore(&vector_lock
, flags
);
2222 * Level and edge triggered IO-APIC interrupts need different handling,
2223 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2224 * handled with the level-triggered descriptor, but that one has slightly
2225 * more overhead. Level-triggered interrupts cannot be handled with the
2226 * edge-triggered handler, without risking IRQ storms and other ugly
2231 static void send_cleanup_vector(struct irq_cfg
*cfg
)
2233 cpumask_var_t cleanup_mask
;
2235 if (unlikely(!alloc_cpumask_var(&cleanup_mask
, GFP_ATOMIC
))) {
2237 cfg
->move_cleanup_count
= 0;
2238 for_each_cpu_and(i
, cfg
->old_domain
, cpu_online_mask
)
2239 cfg
->move_cleanup_count
++;
2240 for_each_cpu_and(i
, cfg
->old_domain
, cpu_online_mask
)
2241 apic
->send_IPI_mask(cpumask_of(i
), IRQ_MOVE_CLEANUP_VECTOR
);
2243 cpumask_and(cleanup_mask
, cfg
->old_domain
, cpu_online_mask
);
2244 cfg
->move_cleanup_count
= cpumask_weight(cleanup_mask
);
2245 apic
->send_IPI_mask(cleanup_mask
, IRQ_MOVE_CLEANUP_VECTOR
);
2246 free_cpumask_var(cleanup_mask
);
2248 cfg
->move_in_progress
= 0;
2251 static void __target_IO_APIC_irq(unsigned int irq
, unsigned int dest
, struct irq_cfg
*cfg
)
2254 struct irq_pin_list
*entry
;
2255 u8 vector
= cfg
->vector
;
2257 for_each_irq_pin(entry
, cfg
->irq_2_pin
) {
2263 * With interrupt-remapping, destination information comes
2264 * from interrupt-remapping table entry.
2266 if (!irq_remapped(irq
))
2267 io_apic_write(apic
, 0x11 + pin
*2, dest
);
2268 reg
= io_apic_read(apic
, 0x10 + pin
*2);
2269 reg
&= ~IO_APIC_REDIR_VECTOR_MASK
;
2271 io_apic_modify(apic
, 0x10 + pin
*2, reg
);
2276 assign_irq_vector(int irq
, struct irq_cfg
*cfg
, const struct cpumask
*mask
);
2279 * Either sets desc->affinity to a valid value, and returns
2280 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
2281 * leaves desc->affinity untouched.
2284 set_desc_affinity(struct irq_desc
*desc
, const struct cpumask
*mask
)
2286 struct irq_cfg
*cfg
;
2289 if (!cpumask_intersects(mask
, cpu_online_mask
))
2293 cfg
= desc
->chip_data
;
2294 if (assign_irq_vector(irq
, cfg
, mask
))
2297 cpumask_copy(desc
->affinity
, mask
);
2299 return apic
->cpu_mask_to_apicid_and(desc
->affinity
, cfg
->domain
);
2303 set_ioapic_affinity_irq_desc(struct irq_desc
*desc
, const struct cpumask
*mask
)
2305 struct irq_cfg
*cfg
;
2306 unsigned long flags
;
2312 cfg
= desc
->chip_data
;
2314 spin_lock_irqsave(&ioapic_lock
, flags
);
2315 dest
= set_desc_affinity(desc
, mask
);
2316 if (dest
!= BAD_APICID
) {
2317 /* Only the high 8 bits are valid. */
2318 dest
= SET_APIC_LOGICAL_ID(dest
);
2319 __target_IO_APIC_irq(irq
, dest
, cfg
);
2322 spin_unlock_irqrestore(&ioapic_lock
, flags
);
2328 set_ioapic_affinity_irq(unsigned int irq
, const struct cpumask
*mask
)
2330 struct irq_desc
*desc
;
2332 desc
= irq_to_desc(irq
);
2334 return set_ioapic_affinity_irq_desc(desc
, mask
);
2337 #ifdef CONFIG_INTR_REMAP
2340 * Migrate the IO-APIC irq in the presence of intr-remapping.
2342 * For both level and edge triggered, irq migration is a simple atomic
2343 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
2345 * For level triggered, we eliminate the io-apic RTE modification (with the
2346 * updated vector information), by using a virtual vector (io-apic pin number).
2347 * Real vector that is used for interrupting cpu will be coming from
2348 * the interrupt-remapping table entry.
2351 migrate_ioapic_irq_desc(struct irq_desc
*desc
, const struct cpumask
*mask
)
2353 struct irq_cfg
*cfg
;
2359 if (!cpumask_intersects(mask
, cpu_online_mask
))
2363 if (get_irte(irq
, &irte
))
2366 cfg
= desc
->chip_data
;
2367 if (assign_irq_vector(irq
, cfg
, mask
))
2370 dest
= apic
->cpu_mask_to_apicid_and(cfg
->domain
, mask
);
2372 irte
.vector
= cfg
->vector
;
2373 irte
.dest_id
= IRTE_DEST(dest
);
2376 * Modified the IRTE and flushes the Interrupt entry cache.
2378 modify_irte(irq
, &irte
);
2380 if (cfg
->move_in_progress
)
2381 send_cleanup_vector(cfg
);
2383 cpumask_copy(desc
->affinity
, mask
);
2389 * Migrates the IRQ destination in the process context.
2391 static int set_ir_ioapic_affinity_irq_desc(struct irq_desc
*desc
,
2392 const struct cpumask
*mask
)
2394 return migrate_ioapic_irq_desc(desc
, mask
);
2396 static int set_ir_ioapic_affinity_irq(unsigned int irq
,
2397 const struct cpumask
*mask
)
2399 struct irq_desc
*desc
= irq_to_desc(irq
);
2401 return set_ir_ioapic_affinity_irq_desc(desc
, mask
);
2404 static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc
*desc
,
2405 const struct cpumask
*mask
)
2411 asmlinkage
void smp_irq_move_cleanup_interrupt(void)
2413 unsigned vector
, me
;
2419 me
= smp_processor_id();
2420 for (vector
= FIRST_EXTERNAL_VECTOR
; vector
< NR_VECTORS
; vector
++) {
2423 struct irq_desc
*desc
;
2424 struct irq_cfg
*cfg
;
2425 irq
= __get_cpu_var(vector_irq
)[vector
];
2430 desc
= irq_to_desc(irq
);
2435 spin_lock(&desc
->lock
);
2436 if (!cfg
->move_cleanup_count
)
2439 if (vector
== cfg
->vector
&& cpumask_test_cpu(me
, cfg
->domain
))
2442 irr
= apic_read(APIC_IRR
+ (vector
/ 32 * 0x10));
2444 * Check if the vector that needs to be cleanedup is
2445 * registered at the cpu's IRR. If so, then this is not
2446 * the best time to clean it up. Lets clean it up in the
2447 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2450 if (irr
& (1 << (vector
% 32))) {
2451 apic
->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR
);
2454 __get_cpu_var(vector_irq
)[vector
] = -1;
2455 cfg
->move_cleanup_count
--;
2457 spin_unlock(&desc
->lock
);
2463 static void irq_complete_move(struct irq_desc
**descp
)
2465 struct irq_desc
*desc
= *descp
;
2466 struct irq_cfg
*cfg
= desc
->chip_data
;
2467 unsigned vector
, me
;
2469 if (likely(!cfg
->move_in_progress
))
2472 vector
= ~get_irq_regs()->orig_ax
;
2473 me
= smp_processor_id();
2475 if (vector
== cfg
->vector
&& cpumask_test_cpu(me
, cfg
->domain
))
2476 send_cleanup_vector(cfg
);
2479 static inline void irq_complete_move(struct irq_desc
**descp
) {}
2482 static void ack_apic_edge(unsigned int irq
)
2484 struct irq_desc
*desc
= irq_to_desc(irq
);
2486 irq_complete_move(&desc
);
2487 move_native_irq(irq
);
2491 atomic_t irq_mis_count
;
2493 static void ack_apic_level(unsigned int irq
)
2495 struct irq_desc
*desc
= irq_to_desc(irq
);
2498 struct irq_cfg
*cfg
;
2499 int do_unmask_irq
= 0;
2501 irq_complete_move(&desc
);
2502 #ifdef CONFIG_GENERIC_PENDING_IRQ
2503 /* If we are moving the irq we need to mask it */
2504 if (unlikely(desc
->status
& IRQ_MOVE_PENDING
)) {
2506 mask_IO_APIC_irq_desc(desc
);
2511 * It appears there is an erratum which affects at least version 0x11
2512 * of I/O APIC (that's the 82093AA and cores integrated into various
2513 * chipsets). Under certain conditions a level-triggered interrupt is
2514 * erroneously delivered as edge-triggered one but the respective IRR
2515 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2516 * message but it will never arrive and further interrupts are blocked
2517 * from the source. The exact reason is so far unknown, but the
2518 * phenomenon was observed when two consecutive interrupt requests
2519 * from a given source get delivered to the same CPU and the source is
2520 * temporarily disabled in between.
2522 * A workaround is to simulate an EOI message manually. We achieve it
2523 * by setting the trigger mode to edge and then to level when the edge
2524 * trigger mode gets detected in the TMR of a local APIC for a
2525 * level-triggered interrupt. We mask the source for the time of the
2526 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2527 * The idea is from Manfred Spraul. --macro
2529 cfg
= desc
->chip_data
;
2531 v
= apic_read(APIC_TMR
+ ((i
& ~0x1f) >> 1));
2534 * We must acknowledge the irq before we move it or the acknowledge will
2535 * not propagate properly.
2539 /* Now we can move and renable the irq */
2540 if (unlikely(do_unmask_irq
)) {
2541 /* Only migrate the irq if the ack has been received.
2543 * On rare occasions the broadcast level triggered ack gets
2544 * delayed going to ioapics, and if we reprogram the
2545 * vector while Remote IRR is still set the irq will never
2548 * To prevent this scenario we read the Remote IRR bit
2549 * of the ioapic. This has two effects.
2550 * - On any sane system the read of the ioapic will
2551 * flush writes (and acks) going to the ioapic from
2553 * - We get to see if the ACK has actually been delivered.
2555 * Based on failed experiments of reprogramming the
2556 * ioapic entry from outside of irq context starting
2557 * with masking the ioapic entry and then polling until
2558 * Remote IRR was clear before reprogramming the
2559 * ioapic I don't trust the Remote IRR bit to be
2560 * completey accurate.
2562 * However there appears to be no other way to plug
2563 * this race, so if the Remote IRR bit is not
2564 * accurate and is causing problems then it is a hardware bug
2565 * and you can go talk to the chipset vendor about it.
2567 cfg
= desc
->chip_data
;
2568 if (!io_apic_level_ack_pending(cfg
))
2569 move_masked_irq(irq
);
2570 unmask_IO_APIC_irq_desc(desc
);
2573 /* Tail end of version 0x11 I/O APIC bug workaround */
2574 if (!(v
& (1 << (i
& 0x1f)))) {
2575 atomic_inc(&irq_mis_count
);
2576 spin_lock(&ioapic_lock
);
2577 __mask_and_edge_IO_APIC_irq(cfg
);
2578 __unmask_and_level_IO_APIC_irq(cfg
);
2579 spin_unlock(&ioapic_lock
);
2583 #ifdef CONFIG_INTR_REMAP
2584 static void __eoi_ioapic_irq(unsigned int irq
, struct irq_cfg
*cfg
)
2586 struct irq_pin_list
*entry
;
2588 for_each_irq_pin(entry
, cfg
->irq_2_pin
)
2589 io_apic_eoi(entry
->apic
, entry
->pin
);
2593 eoi_ioapic_irq(struct irq_desc
*desc
)
2595 struct irq_cfg
*cfg
;
2596 unsigned long flags
;
2600 cfg
= desc
->chip_data
;
2602 spin_lock_irqsave(&ioapic_lock
, flags
);
2603 __eoi_ioapic_irq(irq
, cfg
);
2604 spin_unlock_irqrestore(&ioapic_lock
, flags
);
2607 static void ir_ack_apic_edge(unsigned int irq
)
2612 static void ir_ack_apic_level(unsigned int irq
)
2614 struct irq_desc
*desc
= irq_to_desc(irq
);
2617 eoi_ioapic_irq(desc
);
2619 #endif /* CONFIG_INTR_REMAP */
2621 static struct irq_chip ioapic_chip __read_mostly
= {
2623 .startup
= startup_ioapic_irq
,
2624 .mask
= mask_IO_APIC_irq
,
2625 .unmask
= unmask_IO_APIC_irq
,
2626 .ack
= ack_apic_edge
,
2627 .eoi
= ack_apic_level
,
2629 .set_affinity
= set_ioapic_affinity_irq
,
2631 .retrigger
= ioapic_retrigger_irq
,
2634 static struct irq_chip ir_ioapic_chip __read_mostly
= {
2635 .name
= "IR-IO-APIC",
2636 .startup
= startup_ioapic_irq
,
2637 .mask
= mask_IO_APIC_irq
,
2638 .unmask
= unmask_IO_APIC_irq
,
2639 #ifdef CONFIG_INTR_REMAP
2640 .ack
= ir_ack_apic_edge
,
2641 .eoi
= ir_ack_apic_level
,
2643 .set_affinity
= set_ir_ioapic_affinity_irq
,
2646 .retrigger
= ioapic_retrigger_irq
,
2649 static inline void init_IO_APIC_traps(void)
2652 struct irq_desc
*desc
;
2653 struct irq_cfg
*cfg
;
2656 * NOTE! The local APIC isn't very good at handling
2657 * multiple interrupts at the same interrupt level.
2658 * As the interrupt level is determined by taking the
2659 * vector number and shifting that right by 4, we
2660 * want to spread these out a bit so that they don't
2661 * all fall in the same interrupt level.
2663 * Also, we've got to be careful not to trash gate
2664 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2666 for_each_irq_desc(irq
, desc
) {
2667 cfg
= desc
->chip_data
;
2668 if (IO_APIC_IRQ(irq
) && cfg
&& !cfg
->vector
) {
2670 * Hmm.. We don't have an entry for this,
2671 * so default to an old-fashioned 8259
2672 * interrupt if we can..
2674 if (irq
< nr_legacy_irqs
)
2675 make_8259A_irq(irq
);
2677 /* Strange. Oh, well.. */
2678 desc
->chip
= &no_irq_chip
;
2684 * The local APIC irq-chip implementation:
2687 static void mask_lapic_irq(unsigned int irq
)
2691 v
= apic_read(APIC_LVT0
);
2692 apic_write(APIC_LVT0
, v
| APIC_LVT_MASKED
);
2695 static void unmask_lapic_irq(unsigned int irq
)
2699 v
= apic_read(APIC_LVT0
);
2700 apic_write(APIC_LVT0
, v
& ~APIC_LVT_MASKED
);
2703 static void ack_lapic_irq(unsigned int irq
)
2708 static struct irq_chip lapic_chip __read_mostly
= {
2709 .name
= "local-APIC",
2710 .mask
= mask_lapic_irq
,
2711 .unmask
= unmask_lapic_irq
,
2712 .ack
= ack_lapic_irq
,
2715 static void lapic_register_intr(int irq
, struct irq_desc
*desc
)
2717 desc
->status
&= ~IRQ_LEVEL
;
2718 set_irq_chip_and_handler_name(irq
, &lapic_chip
, handle_edge_irq
,
2722 static void __init
setup_nmi(void)
2725 * Dirty trick to enable the NMI watchdog ...
2726 * We put the 8259A master into AEOI mode and
2727 * unmask on all local APICs LVT0 as NMI.
2729 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2730 * is from Maciej W. Rozycki - so we do not have to EOI from
2731 * the NMI handler or the timer interrupt.
2733 apic_printk(APIC_VERBOSE
, KERN_INFO
"activating NMI Watchdog ...");
2735 enable_NMI_through_LVT0();
2737 apic_printk(APIC_VERBOSE
, " done.\n");
2741 * This looks a bit hackish but it's about the only one way of sending
2742 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2743 * not support the ExtINT mode, unfortunately. We need to send these
2744 * cycles as some i82489DX-based boards have glue logic that keeps the
2745 * 8259A interrupt line asserted until INTA. --macro
2747 static inline void __init
unlock_ExtINT_logic(void)
2750 struct IO_APIC_route_entry entry0
, entry1
;
2751 unsigned char save_control
, save_freq_select
;
2753 pin
= find_isa_irq_pin(8, mp_INT
);
2758 apic
= find_isa_irq_apic(8, mp_INT
);
2764 entry0
= ioapic_read_entry(apic
, pin
);
2765 clear_IO_APIC_pin(apic
, pin
);
2767 memset(&entry1
, 0, sizeof(entry1
));
2769 entry1
.dest_mode
= 0; /* physical delivery */
2770 entry1
.mask
= 0; /* unmask IRQ now */
2771 entry1
.dest
= hard_smp_processor_id();
2772 entry1
.delivery_mode
= dest_ExtINT
;
2773 entry1
.polarity
= entry0
.polarity
;
2777 ioapic_write_entry(apic
, pin
, entry1
);
2779 save_control
= CMOS_READ(RTC_CONTROL
);
2780 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
2781 CMOS_WRITE((save_freq_select
& ~RTC_RATE_SELECT
) | 0x6,
2783 CMOS_WRITE(save_control
| RTC_PIE
, RTC_CONTROL
);
2788 if ((CMOS_READ(RTC_INTR_FLAGS
) & RTC_PF
) == RTC_PF
)
2792 CMOS_WRITE(save_control
, RTC_CONTROL
);
2793 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
2794 clear_IO_APIC_pin(apic
, pin
);
2796 ioapic_write_entry(apic
, pin
, entry0
);
2799 static int disable_timer_pin_1 __initdata
;
2800 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
2801 static int __init
disable_timer_pin_setup(char *arg
)
2803 disable_timer_pin_1
= 1;
2806 early_param("disable_timer_pin_1", disable_timer_pin_setup
);
2808 int timer_through_8259 __initdata
;
2811 * This code may look a bit paranoid, but it's supposed to cooperate with
2812 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2813 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2814 * fanatically on his truly buggy board.
2816 * FIXME: really need to revamp this for all platforms.
2818 static inline void __init
check_timer(void)
2820 struct irq_desc
*desc
= irq_to_desc(0);
2821 struct irq_cfg
*cfg
= desc
->chip_data
;
2822 int node
= cpu_to_node(boot_cpu_id
);
2823 int apic1
, pin1
, apic2
, pin2
;
2824 unsigned long flags
;
2827 local_irq_save(flags
);
2830 * get/set the timer IRQ vector:
2832 disable_8259A_irq(0);
2833 assign_irq_vector(0, cfg
, apic
->target_cpus());
2836 * As IRQ0 is to be enabled in the 8259A, the virtual
2837 * wire has to be disabled in the local APIC. Also
2838 * timer interrupts need to be acknowledged manually in
2839 * the 8259A for the i82489DX when using the NMI
2840 * watchdog as that APIC treats NMIs as level-triggered.
2841 * The AEOI mode will finish them in the 8259A
2844 apic_write(APIC_LVT0
, APIC_LVT_MASKED
| APIC_DM_EXTINT
);
2846 #ifdef CONFIG_X86_32
2850 ver
= apic_read(APIC_LVR
);
2851 ver
= GET_APIC_VERSION(ver
);
2852 timer_ack
= (nmi_watchdog
== NMI_IO_APIC
&& !APIC_INTEGRATED(ver
));
2856 pin1
= find_isa_irq_pin(0, mp_INT
);
2857 apic1
= find_isa_irq_apic(0, mp_INT
);
2858 pin2
= ioapic_i8259
.pin
;
2859 apic2
= ioapic_i8259
.apic
;
2861 apic_printk(APIC_QUIET
, KERN_INFO
"..TIMER: vector=0x%02X "
2862 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2863 cfg
->vector
, apic1
, pin1
, apic2
, pin2
);
2866 * Some BIOS writers are clueless and report the ExtINTA
2867 * I/O APIC input from the cascaded 8259A as the timer
2868 * interrupt input. So just in case, if only one pin
2869 * was found above, try it both directly and through the
2873 if (intr_remapping_enabled
)
2874 panic("BIOS bug: timer not connected to IO-APIC");
2878 } else if (pin2
== -1) {
2885 * Ok, does IRQ0 through the IOAPIC work?
2888 add_pin_to_irq_node(cfg
, node
, apic1
, pin1
);
2889 setup_timer_IRQ0_pin(apic1
, pin1
, cfg
->vector
);
2891 /* for edge trigger, setup_IO_APIC_irq already
2892 * leave it unmasked.
2893 * so only need to unmask if it is level-trigger
2894 * do we really have level trigger timer?
2897 idx
= find_irq_entry(apic1
, pin1
, mp_INT
);
2898 if (idx
!= -1 && irq_trigger(idx
))
2899 unmask_IO_APIC_irq_desc(desc
);
2901 if (timer_irq_works()) {
2902 if (nmi_watchdog
== NMI_IO_APIC
) {
2904 enable_8259A_irq(0);
2906 if (disable_timer_pin_1
> 0)
2907 clear_IO_APIC_pin(0, pin1
);
2910 if (intr_remapping_enabled
)
2911 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2912 local_irq_disable();
2913 clear_IO_APIC_pin(apic1
, pin1
);
2915 apic_printk(APIC_QUIET
, KERN_ERR
"..MP-BIOS bug: "
2916 "8254 timer not connected to IO-APIC\n");
2918 apic_printk(APIC_QUIET
, KERN_INFO
"...trying to set up timer "
2919 "(IRQ0) through the 8259A ...\n");
2920 apic_printk(APIC_QUIET
, KERN_INFO
2921 "..... (found apic %d pin %d) ...\n", apic2
, pin2
);
2923 * legacy devices should be connected to IO APIC #0
2925 replace_pin_at_irq_node(cfg
, node
, apic1
, pin1
, apic2
, pin2
);
2926 setup_timer_IRQ0_pin(apic2
, pin2
, cfg
->vector
);
2927 enable_8259A_irq(0);
2928 if (timer_irq_works()) {
2929 apic_printk(APIC_QUIET
, KERN_INFO
"....... works.\n");
2930 timer_through_8259
= 1;
2931 if (nmi_watchdog
== NMI_IO_APIC
) {
2932 disable_8259A_irq(0);
2934 enable_8259A_irq(0);
2939 * Cleanup, just in case ...
2941 local_irq_disable();
2942 disable_8259A_irq(0);
2943 clear_IO_APIC_pin(apic2
, pin2
);
2944 apic_printk(APIC_QUIET
, KERN_INFO
"....... failed.\n");
2947 if (nmi_watchdog
== NMI_IO_APIC
) {
2948 apic_printk(APIC_QUIET
, KERN_WARNING
"timer doesn't work "
2949 "through the IO-APIC - disabling NMI Watchdog!\n");
2950 nmi_watchdog
= NMI_NONE
;
2952 #ifdef CONFIG_X86_32
2956 apic_printk(APIC_QUIET
, KERN_INFO
2957 "...trying to set up timer as Virtual Wire IRQ...\n");
2959 lapic_register_intr(0, desc
);
2960 apic_write(APIC_LVT0
, APIC_DM_FIXED
| cfg
->vector
); /* Fixed mode */
2961 enable_8259A_irq(0);
2963 if (timer_irq_works()) {
2964 apic_printk(APIC_QUIET
, KERN_INFO
"..... works.\n");
2967 local_irq_disable();
2968 disable_8259A_irq(0);
2969 apic_write(APIC_LVT0
, APIC_LVT_MASKED
| APIC_DM_FIXED
| cfg
->vector
);
2970 apic_printk(APIC_QUIET
, KERN_INFO
"..... failed.\n");
2972 apic_printk(APIC_QUIET
, KERN_INFO
2973 "...trying to set up timer as ExtINT IRQ...\n");
2977 apic_write(APIC_LVT0
, APIC_DM_EXTINT
);
2979 unlock_ExtINT_logic();
2981 if (timer_irq_works()) {
2982 apic_printk(APIC_QUIET
, KERN_INFO
"..... works.\n");
2985 local_irq_disable();
2986 apic_printk(APIC_QUIET
, KERN_INFO
"..... failed :(.\n");
2987 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2988 "report. Then try booting with the 'noapic' option.\n");
2990 local_irq_restore(flags
);
2994 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2995 * to devices. However there may be an I/O APIC pin available for
2996 * this interrupt regardless. The pin may be left unconnected, but
2997 * typically it will be reused as an ExtINT cascade interrupt for
2998 * the master 8259A. In the MPS case such a pin will normally be
2999 * reported as an ExtINT interrupt in the MP table. With ACPI
3000 * there is no provision for ExtINT interrupts, and in the absence
3001 * of an override it would be treated as an ordinary ISA I/O APIC
3002 * interrupt, that is edge-triggered and unmasked by default. We
3003 * used to do this, but it caused problems on some systems because
3004 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3005 * the same ExtINT cascade interrupt to drive the local APIC of the
3006 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3007 * the I/O APIC in all cases now. No actual device should request
3008 * it anyway. --macro
3010 #define PIC_IRQS (1UL << PIC_CASCADE_IR)
3012 void __init
setup_IO_APIC(void)
3016 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3018 io_apic_irqs
= nr_legacy_irqs
? ~PIC_IRQS
: ~0UL;
3020 apic_printk(APIC_VERBOSE
, "ENABLING IO-APIC IRQs\n");
3022 * Set up IO-APIC IRQ routing.
3024 x86_init
.mpparse
.setup_ioapic_ids();
3027 setup_IO_APIC_irqs();
3028 init_IO_APIC_traps();
3034 * Called after all the initialization is done. If we didnt find any
3035 * APIC bugs then we can allow the modify fast path
3038 static int __init
io_apic_bug_finalize(void)
3040 if (sis_apic_bug
== -1)
3045 late_initcall(io_apic_bug_finalize
);
3047 struct sysfs_ioapic_data
{
3048 struct sys_device dev
;
3049 struct IO_APIC_route_entry entry
[0];
3051 static struct sysfs_ioapic_data
* mp_ioapic_data
[MAX_IO_APICS
];
3053 static int ioapic_suspend(struct sys_device
*dev
, pm_message_t state
)
3055 struct IO_APIC_route_entry
*entry
;
3056 struct sysfs_ioapic_data
*data
;
3059 data
= container_of(dev
, struct sysfs_ioapic_data
, dev
);
3060 entry
= data
->entry
;
3061 for (i
= 0; i
< nr_ioapic_registers
[dev
->id
]; i
++, entry
++ )
3062 *entry
= ioapic_read_entry(dev
->id
, i
);
3067 static int ioapic_resume(struct sys_device
*dev
)
3069 struct IO_APIC_route_entry
*entry
;
3070 struct sysfs_ioapic_data
*data
;
3071 unsigned long flags
;
3072 union IO_APIC_reg_00 reg_00
;
3075 data
= container_of(dev
, struct sysfs_ioapic_data
, dev
);
3076 entry
= data
->entry
;
3078 spin_lock_irqsave(&ioapic_lock
, flags
);
3079 reg_00
.raw
= io_apic_read(dev
->id
, 0);
3080 if (reg_00
.bits
.ID
!= mp_ioapics
[dev
->id
].apicid
) {
3081 reg_00
.bits
.ID
= mp_ioapics
[dev
->id
].apicid
;
3082 io_apic_write(dev
->id
, 0, reg_00
.raw
);
3084 spin_unlock_irqrestore(&ioapic_lock
, flags
);
3085 for (i
= 0; i
< nr_ioapic_registers
[dev
->id
]; i
++)
3086 ioapic_write_entry(dev
->id
, i
, entry
[i
]);
3091 static struct sysdev_class ioapic_sysdev_class
= {
3093 .suspend
= ioapic_suspend
,
3094 .resume
= ioapic_resume
,
3097 static int __init
ioapic_init_sysfs(void)
3099 struct sys_device
* dev
;
3102 error
= sysdev_class_register(&ioapic_sysdev_class
);
3106 for (i
= 0; i
< nr_ioapics
; i
++ ) {
3107 size
= sizeof(struct sys_device
) + nr_ioapic_registers
[i
]
3108 * sizeof(struct IO_APIC_route_entry
);
3109 mp_ioapic_data
[i
] = kzalloc(size
, GFP_KERNEL
);
3110 if (!mp_ioapic_data
[i
]) {
3111 printk(KERN_ERR
"Can't suspend/resume IOAPIC %d\n", i
);
3114 dev
= &mp_ioapic_data
[i
]->dev
;
3116 dev
->cls
= &ioapic_sysdev_class
;
3117 error
= sysdev_register(dev
);
3119 kfree(mp_ioapic_data
[i
]);
3120 mp_ioapic_data
[i
] = NULL
;
3121 printk(KERN_ERR
"Can't suspend/resume IOAPIC %d\n", i
);
3129 device_initcall(ioapic_init_sysfs
);
3132 * Dynamic irq allocate and deallocation
3134 unsigned int create_irq_nr(unsigned int irq_want
, int node
)
3136 /* Allocate an unused irq */
3139 unsigned long flags
;
3140 struct irq_cfg
*cfg_new
= NULL
;
3141 struct irq_desc
*desc_new
= NULL
;
3144 if (irq_want
< nr_irqs_gsi
)
3145 irq_want
= nr_irqs_gsi
;
3147 spin_lock_irqsave(&vector_lock
, flags
);
3148 for (new = irq_want
; new < nr_irqs
; new++) {
3149 desc_new
= irq_to_desc_alloc_node(new, node
);
3151 printk(KERN_INFO
"can not get irq_desc for %d\n", new);
3154 cfg_new
= desc_new
->chip_data
;
3156 if (cfg_new
->vector
!= 0)
3159 desc_new
= move_irq_desc(desc_new
, node
);
3161 if (__assign_irq_vector(new, cfg_new
, apic
->target_cpus()) == 0)
3165 spin_unlock_irqrestore(&vector_lock
, flags
);
3168 dynamic_irq_init(irq
);
3169 /* restore it, in case dynamic_irq_init clear it */
3171 desc_new
->chip_data
= cfg_new
;
3176 int create_irq(void)
3178 int node
= cpu_to_node(boot_cpu_id
);
3179 unsigned int irq_want
;
3182 irq_want
= nr_irqs_gsi
;
3183 irq
= create_irq_nr(irq_want
, node
);
3191 void destroy_irq(unsigned int irq
)
3193 unsigned long flags
;
3194 struct irq_cfg
*cfg
;
3195 struct irq_desc
*desc
;
3197 /* store it, in case dynamic_irq_cleanup clear it */
3198 desc
= irq_to_desc(irq
);
3199 cfg
= desc
->chip_data
;
3200 dynamic_irq_cleanup(irq
);
3201 /* connect back irq_cfg */
3202 desc
->chip_data
= cfg
;
3205 spin_lock_irqsave(&vector_lock
, flags
);
3206 __clear_irq_vector(irq
, cfg
);
3207 spin_unlock_irqrestore(&vector_lock
, flags
);
3211 * MSI message composition
3213 #ifdef CONFIG_PCI_MSI
3214 static int msi_compose_msg(struct pci_dev
*pdev
, unsigned int irq
, struct msi_msg
*msg
)
3216 struct irq_cfg
*cfg
;
3224 err
= assign_irq_vector(irq
, cfg
, apic
->target_cpus());
3228 dest
= apic
->cpu_mask_to_apicid_and(cfg
->domain
, apic
->target_cpus());
3230 if (irq_remapped(irq
)) {
3235 ir_index
= map_irq_to_irte_handle(irq
, &sub_handle
);
3236 BUG_ON(ir_index
== -1);
3238 memset (&irte
, 0, sizeof(irte
));
3241 irte
.dst_mode
= apic
->irq_dest_mode
;
3242 irte
.trigger_mode
= 0; /* edge */
3243 irte
.dlvry_mode
= apic
->irq_delivery_mode
;
3244 irte
.vector
= cfg
->vector
;
3245 irte
.dest_id
= IRTE_DEST(dest
);
3247 /* Set source-id of interrupt request */
3248 set_msi_sid(&irte
, pdev
);
3250 modify_irte(irq
, &irte
);
3252 msg
->address_hi
= MSI_ADDR_BASE_HI
;
3253 msg
->data
= sub_handle
;
3254 msg
->address_lo
= MSI_ADDR_BASE_LO
| MSI_ADDR_IR_EXT_INT
|
3256 MSI_ADDR_IR_INDEX1(ir_index
) |
3257 MSI_ADDR_IR_INDEX2(ir_index
);
3259 if (x2apic_enabled())
3260 msg
->address_hi
= MSI_ADDR_BASE_HI
|
3261 MSI_ADDR_EXT_DEST_ID(dest
);
3263 msg
->address_hi
= MSI_ADDR_BASE_HI
;
3267 ((apic
->irq_dest_mode
== 0) ?
3268 MSI_ADDR_DEST_MODE_PHYSICAL
:
3269 MSI_ADDR_DEST_MODE_LOGICAL
) |
3270 ((apic
->irq_delivery_mode
!= dest_LowestPrio
) ?
3271 MSI_ADDR_REDIRECTION_CPU
:
3272 MSI_ADDR_REDIRECTION_LOWPRI
) |
3273 MSI_ADDR_DEST_ID(dest
);
3276 MSI_DATA_TRIGGER_EDGE
|
3277 MSI_DATA_LEVEL_ASSERT
|
3278 ((apic
->irq_delivery_mode
!= dest_LowestPrio
) ?
3279 MSI_DATA_DELIVERY_FIXED
:
3280 MSI_DATA_DELIVERY_LOWPRI
) |
3281 MSI_DATA_VECTOR(cfg
->vector
);
3287 static int set_msi_irq_affinity(unsigned int irq
, const struct cpumask
*mask
)
3289 struct irq_desc
*desc
= irq_to_desc(irq
);
3290 struct irq_cfg
*cfg
;
3294 dest
= set_desc_affinity(desc
, mask
);
3295 if (dest
== BAD_APICID
)
3298 cfg
= desc
->chip_data
;
3300 read_msi_msg_desc(desc
, &msg
);
3302 msg
.data
&= ~MSI_DATA_VECTOR_MASK
;
3303 msg
.data
|= MSI_DATA_VECTOR(cfg
->vector
);
3304 msg
.address_lo
&= ~MSI_ADDR_DEST_ID_MASK
;
3305 msg
.address_lo
|= MSI_ADDR_DEST_ID(dest
);
3307 write_msi_msg_desc(desc
, &msg
);
3311 #ifdef CONFIG_INTR_REMAP
3313 * Migrate the MSI irq to another cpumask. This migration is
3314 * done in the process context using interrupt-remapping hardware.
3317 ir_set_msi_irq_affinity(unsigned int irq
, const struct cpumask
*mask
)
3319 struct irq_desc
*desc
= irq_to_desc(irq
);
3320 struct irq_cfg
*cfg
= desc
->chip_data
;
3324 if (get_irte(irq
, &irte
))
3327 dest
= set_desc_affinity(desc
, mask
);
3328 if (dest
== BAD_APICID
)
3331 irte
.vector
= cfg
->vector
;
3332 irte
.dest_id
= IRTE_DEST(dest
);
3335 * atomically update the IRTE with the new destination and vector.
3337 modify_irte(irq
, &irte
);
3340 * After this point, all the interrupts will start arriving
3341 * at the new destination. So, time to cleanup the previous
3342 * vector allocation.
3344 if (cfg
->move_in_progress
)
3345 send_cleanup_vector(cfg
);
3351 #endif /* CONFIG_SMP */
3354 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3355 * which implement the MSI or MSI-X Capability Structure.
3357 static struct irq_chip msi_chip
= {
3359 .unmask
= unmask_msi_irq
,
3360 .mask
= mask_msi_irq
,
3361 .ack
= ack_apic_edge
,
3363 .set_affinity
= set_msi_irq_affinity
,
3365 .retrigger
= ioapic_retrigger_irq
,
3368 static struct irq_chip msi_ir_chip
= {
3369 .name
= "IR-PCI-MSI",
3370 .unmask
= unmask_msi_irq
,
3371 .mask
= mask_msi_irq
,
3372 #ifdef CONFIG_INTR_REMAP
3373 .ack
= ir_ack_apic_edge
,
3375 .set_affinity
= ir_set_msi_irq_affinity
,
3378 .retrigger
= ioapic_retrigger_irq
,
3382 * Map the PCI dev to the corresponding remapping hardware unit
3383 * and allocate 'nvec' consecutive interrupt-remapping table entries
3386 static int msi_alloc_irte(struct pci_dev
*dev
, int irq
, int nvec
)
3388 struct intel_iommu
*iommu
;
3391 iommu
= map_dev_to_ir(dev
);
3394 "Unable to map PCI %s to iommu\n", pci_name(dev
));
3398 index
= alloc_irte(iommu
, irq
, nvec
);
3401 "Unable to allocate %d IRTE for PCI %s\n", nvec
,
3408 static int setup_msi_irq(struct pci_dev
*dev
, struct msi_desc
*msidesc
, int irq
)
3413 ret
= msi_compose_msg(dev
, irq
, &msg
);
3417 set_irq_msi(irq
, msidesc
);
3418 write_msi_msg(irq
, &msg
);
3420 if (irq_remapped(irq
)) {
3421 struct irq_desc
*desc
= irq_to_desc(irq
);
3423 * irq migration in process context
3425 desc
->status
|= IRQ_MOVE_PCNTXT
;
3426 set_irq_chip_and_handler_name(irq
, &msi_ir_chip
, handle_edge_irq
, "edge");
3428 set_irq_chip_and_handler_name(irq
, &msi_chip
, handle_edge_irq
, "edge");
3430 dev_printk(KERN_DEBUG
, &dev
->dev
, "irq %d for MSI/MSI-X\n", irq
);
3435 int arch_setup_msi_irqs(struct pci_dev
*dev
, int nvec
, int type
)
3438 int ret
, sub_handle
;
3439 struct msi_desc
*msidesc
;
3440 unsigned int irq_want
;
3441 struct intel_iommu
*iommu
= NULL
;
3445 /* x86 doesn't support multiple MSI yet */
3446 if (type
== PCI_CAP_ID_MSI
&& nvec
> 1)
3449 node
= dev_to_node(&dev
->dev
);
3450 irq_want
= nr_irqs_gsi
;
3452 list_for_each_entry(msidesc
, &dev
->msi_list
, list
) {
3453 irq
= create_irq_nr(irq_want
, node
);
3457 if (!intr_remapping_enabled
)
3462 * allocate the consecutive block of IRTE's
3465 index
= msi_alloc_irte(dev
, irq
, nvec
);
3471 iommu
= map_dev_to_ir(dev
);
3477 * setup the mapping between the irq and the IRTE
3478 * base index, the sub_handle pointing to the
3479 * appropriate interrupt remap table entry.
3481 set_irte_irq(irq
, iommu
, index
, sub_handle
);
3484 ret
= setup_msi_irq(dev
, msidesc
, irq
);
3496 void arch_teardown_msi_irq(unsigned int irq
)
3501 #if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
3503 static int dmar_msi_set_affinity(unsigned int irq
, const struct cpumask
*mask
)
3505 struct irq_desc
*desc
= irq_to_desc(irq
);
3506 struct irq_cfg
*cfg
;
3510 dest
= set_desc_affinity(desc
, mask
);
3511 if (dest
== BAD_APICID
)
3514 cfg
= desc
->chip_data
;
3516 dmar_msi_read(irq
, &msg
);
3518 msg
.data
&= ~MSI_DATA_VECTOR_MASK
;
3519 msg
.data
|= MSI_DATA_VECTOR(cfg
->vector
);
3520 msg
.address_lo
&= ~MSI_ADDR_DEST_ID_MASK
;
3521 msg
.address_lo
|= MSI_ADDR_DEST_ID(dest
);
3523 dmar_msi_write(irq
, &msg
);
3528 #endif /* CONFIG_SMP */
3530 static struct irq_chip dmar_msi_type
= {
3532 .unmask
= dmar_msi_unmask
,
3533 .mask
= dmar_msi_mask
,
3534 .ack
= ack_apic_edge
,
3536 .set_affinity
= dmar_msi_set_affinity
,
3538 .retrigger
= ioapic_retrigger_irq
,
3541 int arch_setup_dmar_msi(unsigned int irq
)
3546 ret
= msi_compose_msg(NULL
, irq
, &msg
);
3549 dmar_msi_write(irq
, &msg
);
3550 set_irq_chip_and_handler_name(irq
, &dmar_msi_type
, handle_edge_irq
,
3556 #ifdef CONFIG_HPET_TIMER
3559 static int hpet_msi_set_affinity(unsigned int irq
, const struct cpumask
*mask
)
3561 struct irq_desc
*desc
= irq_to_desc(irq
);
3562 struct irq_cfg
*cfg
;
3566 dest
= set_desc_affinity(desc
, mask
);
3567 if (dest
== BAD_APICID
)
3570 cfg
= desc
->chip_data
;
3572 hpet_msi_read(irq
, &msg
);
3574 msg
.data
&= ~MSI_DATA_VECTOR_MASK
;
3575 msg
.data
|= MSI_DATA_VECTOR(cfg
->vector
);
3576 msg
.address_lo
&= ~MSI_ADDR_DEST_ID_MASK
;
3577 msg
.address_lo
|= MSI_ADDR_DEST_ID(dest
);
3579 hpet_msi_write(irq
, &msg
);
3584 #endif /* CONFIG_SMP */
3586 static struct irq_chip hpet_msi_type
= {
3588 .unmask
= hpet_msi_unmask
,
3589 .mask
= hpet_msi_mask
,
3590 .ack
= ack_apic_edge
,
3592 .set_affinity
= hpet_msi_set_affinity
,
3594 .retrigger
= ioapic_retrigger_irq
,
3597 int arch_setup_hpet_msi(unsigned int irq
)
3601 struct irq_desc
*desc
= irq_to_desc(irq
);
3603 ret
= msi_compose_msg(NULL
, irq
, &msg
);
3607 hpet_msi_write(irq
, &msg
);
3608 desc
->status
|= IRQ_MOVE_PCNTXT
;
3609 set_irq_chip_and_handler_name(irq
, &hpet_msi_type
, handle_edge_irq
,
3616 #endif /* CONFIG_PCI_MSI */
3618 * Hypertransport interrupt support
3620 #ifdef CONFIG_HT_IRQ
3624 static void target_ht_irq(unsigned int irq
, unsigned int dest
, u8 vector
)
3626 struct ht_irq_msg msg
;
3627 fetch_ht_irq_msg(irq
, &msg
);
3629 msg
.address_lo
&= ~(HT_IRQ_LOW_VECTOR_MASK
| HT_IRQ_LOW_DEST_ID_MASK
);
3630 msg
.address_hi
&= ~(HT_IRQ_HIGH_DEST_ID_MASK
);
3632 msg
.address_lo
|= HT_IRQ_LOW_VECTOR(vector
) | HT_IRQ_LOW_DEST_ID(dest
);
3633 msg
.address_hi
|= HT_IRQ_HIGH_DEST_ID(dest
);
3635 write_ht_irq_msg(irq
, &msg
);
3638 static int set_ht_irq_affinity(unsigned int irq
, const struct cpumask
*mask
)
3640 struct irq_desc
*desc
= irq_to_desc(irq
);
3641 struct irq_cfg
*cfg
;
3644 dest
= set_desc_affinity(desc
, mask
);
3645 if (dest
== BAD_APICID
)
3648 cfg
= desc
->chip_data
;
3650 target_ht_irq(irq
, dest
, cfg
->vector
);
3657 static struct irq_chip ht_irq_chip
= {
3659 .mask
= mask_ht_irq
,
3660 .unmask
= unmask_ht_irq
,
3661 .ack
= ack_apic_edge
,
3663 .set_affinity
= set_ht_irq_affinity
,
3665 .retrigger
= ioapic_retrigger_irq
,
3668 int arch_setup_ht_irq(unsigned int irq
, struct pci_dev
*dev
)
3670 struct irq_cfg
*cfg
;
3677 err
= assign_irq_vector(irq
, cfg
, apic
->target_cpus());
3679 struct ht_irq_msg msg
;
3682 dest
= apic
->cpu_mask_to_apicid_and(cfg
->domain
,
3683 apic
->target_cpus());
3685 msg
.address_hi
= HT_IRQ_HIGH_DEST_ID(dest
);
3689 HT_IRQ_LOW_DEST_ID(dest
) |
3690 HT_IRQ_LOW_VECTOR(cfg
->vector
) |
3691 ((apic
->irq_dest_mode
== 0) ?
3692 HT_IRQ_LOW_DM_PHYSICAL
:
3693 HT_IRQ_LOW_DM_LOGICAL
) |
3694 HT_IRQ_LOW_RQEOI_EDGE
|
3695 ((apic
->irq_delivery_mode
!= dest_LowestPrio
) ?
3696 HT_IRQ_LOW_MT_FIXED
:
3697 HT_IRQ_LOW_MT_ARBITRATED
) |
3698 HT_IRQ_LOW_IRQ_MASKED
;
3700 write_ht_irq_msg(irq
, &msg
);
3702 set_irq_chip_and_handler_name(irq
, &ht_irq_chip
,
3703 handle_edge_irq
, "edge");
3705 dev_printk(KERN_DEBUG
, &dev
->dev
, "irq %d for HT\n", irq
);
3709 #endif /* CONFIG_HT_IRQ */
3711 #ifdef CONFIG_X86_UV
3713 * Re-target the irq to the specified CPU and enable the specified MMR located
3714 * on the specified blade to allow the sending of MSIs to the specified CPU.
3716 int arch_enable_uv_irq(char *irq_name
, unsigned int irq
, int cpu
, int mmr_blade
,
3717 unsigned long mmr_offset
)
3719 const struct cpumask
*eligible_cpu
= cpumask_of(cpu
);
3720 struct irq_cfg
*cfg
;
3722 unsigned long mmr_value
;
3723 struct uv_IO_APIC_route_entry
*entry
;
3724 unsigned long flags
;
3727 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry
) != sizeof(unsigned long));
3731 err
= assign_irq_vector(irq
, cfg
, eligible_cpu
);
3735 spin_lock_irqsave(&vector_lock
, flags
);
3736 set_irq_chip_and_handler_name(irq
, &uv_irq_chip
, handle_percpu_irq
,
3738 spin_unlock_irqrestore(&vector_lock
, flags
);
3741 entry
= (struct uv_IO_APIC_route_entry
*)&mmr_value
;
3742 entry
->vector
= cfg
->vector
;
3743 entry
->delivery_mode
= apic
->irq_delivery_mode
;
3744 entry
->dest_mode
= apic
->irq_dest_mode
;
3745 entry
->polarity
= 0;
3748 entry
->dest
= apic
->cpu_mask_to_apicid(eligible_cpu
);
3750 mmr_pnode
= uv_blade_to_pnode(mmr_blade
);
3751 uv_write_global_mmr64(mmr_pnode
, mmr_offset
, mmr_value
);
3753 if (cfg
->move_in_progress
)
3754 send_cleanup_vector(cfg
);
3760 * Disable the specified MMR located on the specified blade so that MSIs are
3761 * longer allowed to be sent.
3763 void arch_disable_uv_irq(int mmr_blade
, unsigned long mmr_offset
)
3765 unsigned long mmr_value
;
3766 struct uv_IO_APIC_route_entry
*entry
;
3769 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry
) != sizeof(unsigned long));
3772 entry
= (struct uv_IO_APIC_route_entry
*)&mmr_value
;
3775 mmr_pnode
= uv_blade_to_pnode(mmr_blade
);
3776 uv_write_global_mmr64(mmr_pnode
, mmr_offset
, mmr_value
);
3778 #endif /* CONFIG_X86_64 */
3780 int __init
io_apic_get_redir_entries (int ioapic
)
3782 union IO_APIC_reg_01 reg_01
;
3783 unsigned long flags
;
3785 spin_lock_irqsave(&ioapic_lock
, flags
);
3786 reg_01
.raw
= io_apic_read(ioapic
, 1);
3787 spin_unlock_irqrestore(&ioapic_lock
, flags
);
3789 return reg_01
.bits
.entries
;
3792 void __init
probe_nr_irqs_gsi(void)
3796 nr
= acpi_probe_gsi();
3797 if (nr
> nr_irqs_gsi
) {
3800 /* for acpi=off or acpi is not compiled in */
3804 for (idx
= 0; idx
< nr_ioapics
; idx
++)
3805 nr
+= io_apic_get_redir_entries(idx
) + 1;
3807 if (nr
> nr_irqs_gsi
)
3811 printk(KERN_DEBUG
"nr_irqs_gsi: %d\n", nr_irqs_gsi
);
3814 #ifdef CONFIG_SPARSE_IRQ
3815 int __init
arch_probe_nr_irqs(void)
3819 if (nr_irqs
> (NR_VECTORS
* nr_cpu_ids
))
3820 nr_irqs
= NR_VECTORS
* nr_cpu_ids
;
3822 nr
= nr_irqs_gsi
+ 8 * nr_cpu_ids
;
3823 #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3825 * for MSI and HT dyn irq
3827 nr
+= nr_irqs_gsi
* 16;
3836 static int __io_apic_set_pci_routing(struct device
*dev
, int irq
,
3837 struct io_apic_irq_attr
*irq_attr
)
3839 struct irq_desc
*desc
;
3840 struct irq_cfg
*cfg
;
3843 int trigger
, polarity
;
3845 ioapic
= irq_attr
->ioapic
;
3846 if (!IO_APIC_IRQ(irq
)) {
3847 apic_printk(APIC_QUIET
,KERN_ERR
"IOAPIC[%d]: Invalid reference to IRQ 0\n",
3853 node
= dev_to_node(dev
);
3855 node
= cpu_to_node(boot_cpu_id
);
3857 desc
= irq_to_desc_alloc_node(irq
, node
);
3859 printk(KERN_INFO
"can not get irq_desc %d\n", irq
);
3863 pin
= irq_attr
->ioapic_pin
;
3864 trigger
= irq_attr
->trigger
;
3865 polarity
= irq_attr
->polarity
;
3868 * IRQs < 16 are already in the irq_2_pin[] map
3870 if (irq
>= nr_legacy_irqs
) {
3871 cfg
= desc
->chip_data
;
3872 if (add_pin_to_irq_node_nopanic(cfg
, node
, ioapic
, pin
)) {
3873 printk(KERN_INFO
"can not add pin %d for irq %d\n",
3879 setup_IO_APIC_irq(ioapic
, pin
, irq
, desc
, trigger
, polarity
);
3884 int io_apic_set_pci_routing(struct device
*dev
, int irq
,
3885 struct io_apic_irq_attr
*irq_attr
)
3889 * Avoid pin reprogramming. PRTs typically include entries
3890 * with redundant pin->gsi mappings (but unique PCI devices);
3891 * we only program the IOAPIC on the first.
3893 ioapic
= irq_attr
->ioapic
;
3894 pin
= irq_attr
->ioapic_pin
;
3895 if (test_bit(pin
, mp_ioapic_routing
[ioapic
].pin_programmed
)) {
3896 pr_debug("Pin %d-%d already programmed\n",
3897 mp_ioapics
[ioapic
].apicid
, pin
);
3900 set_bit(pin
, mp_ioapic_routing
[ioapic
].pin_programmed
);
3902 return __io_apic_set_pci_routing(dev
, irq
, irq_attr
);
3905 u8 __init
io_apic_unique_id(u8 id
)
3907 #ifdef CONFIG_X86_32
3908 if ((boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
) &&
3909 !APIC_XAPIC(apic_version
[boot_cpu_physical_apicid
]))
3910 return io_apic_get_unique_id(nr_ioapics
, id
);
3915 DECLARE_BITMAP(used
, 256);
3917 bitmap_zero(used
, 256);
3918 for (i
= 0; i
< nr_ioapics
; i
++) {
3919 struct mpc_ioapic
*ia
= &mp_ioapics
[i
];
3920 __set_bit(ia
->apicid
, used
);
3922 if (!test_bit(id
, used
))
3924 return find_first_zero_bit(used
, 256);
3928 #ifdef CONFIG_X86_32
3929 int __init
io_apic_get_unique_id(int ioapic
, int apic_id
)
3931 union IO_APIC_reg_00 reg_00
;
3932 static physid_mask_t apic_id_map
= PHYSID_MASK_NONE
;
3934 unsigned long flags
;
3938 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3939 * buses (one for LAPICs, one for IOAPICs), where predecessors only
3940 * supports up to 16 on one shared APIC bus.
3942 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3943 * advantage of new APIC bus architecture.
3946 if (physids_empty(apic_id_map
))
3947 apic_id_map
= apic
->ioapic_phys_id_map(phys_cpu_present_map
);
3949 spin_lock_irqsave(&ioapic_lock
, flags
);
3950 reg_00
.raw
= io_apic_read(ioapic
, 0);
3951 spin_unlock_irqrestore(&ioapic_lock
, flags
);
3953 if (apic_id
>= get_physical_broadcast()) {
3954 printk(KERN_WARNING
"IOAPIC[%d]: Invalid apic_id %d, trying "
3955 "%d\n", ioapic
, apic_id
, reg_00
.bits
.ID
);
3956 apic_id
= reg_00
.bits
.ID
;
3960 * Every APIC in a system must have a unique ID or we get lots of nice
3961 * 'stuck on smp_invalidate_needed IPI wait' messages.
3963 if (apic
->check_apicid_used(apic_id_map
, apic_id
)) {
3965 for (i
= 0; i
< get_physical_broadcast(); i
++) {
3966 if (!apic
->check_apicid_used(apic_id_map
, i
))
3970 if (i
== get_physical_broadcast())
3971 panic("Max apic_id exceeded!\n");
3973 printk(KERN_WARNING
"IOAPIC[%d]: apic_id %d already used, "
3974 "trying %d\n", ioapic
, apic_id
, i
);
3979 tmp
= apic
->apicid_to_cpu_present(apic_id
);
3980 physids_or(apic_id_map
, apic_id_map
, tmp
);
3982 if (reg_00
.bits
.ID
!= apic_id
) {
3983 reg_00
.bits
.ID
= apic_id
;
3985 spin_lock_irqsave(&ioapic_lock
, flags
);
3986 io_apic_write(ioapic
, 0, reg_00
.raw
);
3987 reg_00
.raw
= io_apic_read(ioapic
, 0);
3988 spin_unlock_irqrestore(&ioapic_lock
, flags
);
3991 if (reg_00
.bits
.ID
!= apic_id
) {
3992 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic
);
3997 apic_printk(APIC_VERBOSE
, KERN_INFO
3998 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic
, apic_id
);
4004 int __init
io_apic_get_version(int ioapic
)
4006 union IO_APIC_reg_01 reg_01
;
4007 unsigned long flags
;
4009 spin_lock_irqsave(&ioapic_lock
, flags
);
4010 reg_01
.raw
= io_apic_read(ioapic
, 1);
4011 spin_unlock_irqrestore(&ioapic_lock
, flags
);
4013 return reg_01
.bits
.version
;
4016 int acpi_get_override_irq(int bus_irq
, int *trigger
, int *polarity
)
4020 if (skip_ioapic_setup
)
4023 for (i
= 0; i
< mp_irq_entries
; i
++)
4024 if (mp_irqs
[i
].irqtype
== mp_INT
&&
4025 mp_irqs
[i
].srcbusirq
== bus_irq
)
4027 if (i
>= mp_irq_entries
)
4030 *trigger
= irq_trigger(i
);
4031 *polarity
= irq_polarity(i
);
4036 * This function currently is only a helper for the i386 smp boot process where
4037 * we need to reprogram the ioredtbls to cater for the cpus which have come online
4038 * so mask in all cases should simply be apic->target_cpus()
4041 void __init
setup_ioapic_dest(void)
4043 int pin
, ioapic
= 0, irq
, irq_entry
;
4044 struct irq_desc
*desc
;
4045 const struct cpumask
*mask
;
4047 if (skip_ioapic_setup
== 1)
4051 if (!acpi_disabled
&& acpi_ioapic
) {
4052 ioapic
= mp_find_ioapic(0);
4058 for (pin
= 0; pin
< nr_ioapic_registers
[ioapic
]; pin
++) {
4059 irq_entry
= find_irq_entry(ioapic
, pin
, mp_INT
);
4060 if (irq_entry
== -1)
4062 irq
= pin_2_irq(irq_entry
, ioapic
, pin
);
4064 desc
= irq_to_desc(irq
);
4067 * Honour affinities which have been set in early boot
4070 (IRQ_NO_BALANCING
| IRQ_AFFINITY_SET
))
4071 mask
= desc
->affinity
;
4073 mask
= apic
->target_cpus();
4075 if (intr_remapping_enabled
)
4076 set_ir_ioapic_affinity_irq_desc(desc
, mask
);
4078 set_ioapic_affinity_irq_desc(desc
, mask
);
4084 #define IOAPIC_RESOURCE_NAME_SIZE 11
4086 static struct resource
*ioapic_resources
;
4088 static struct resource
* __init
ioapic_setup_resources(int nr_ioapics
)
4091 struct resource
*res
;
4095 if (nr_ioapics
<= 0)
4098 n
= IOAPIC_RESOURCE_NAME_SIZE
+ sizeof(struct resource
);
4101 mem
= alloc_bootmem(n
);
4104 mem
+= sizeof(struct resource
) * nr_ioapics
;
4106 for (i
= 0; i
< nr_ioapics
; i
++) {
4108 res
[i
].flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
;
4109 sprintf(mem
, "IOAPIC %u", i
);
4110 mem
+= IOAPIC_RESOURCE_NAME_SIZE
;
4113 ioapic_resources
= res
;
4118 void __init
ioapic_init_mappings(void)
4120 unsigned long ioapic_phys
, idx
= FIX_IO_APIC_BASE_0
;
4121 struct resource
*ioapic_res
;
4124 ioapic_res
= ioapic_setup_resources(nr_ioapics
);
4125 for (i
= 0; i
< nr_ioapics
; i
++) {
4126 if (smp_found_config
) {
4127 ioapic_phys
= mp_ioapics
[i
].apicaddr
;
4128 #ifdef CONFIG_X86_32
4131 "WARNING: bogus zero IO-APIC "
4132 "address found in MPTABLE, "
4133 "disabling IO/APIC support!\n");
4134 smp_found_config
= 0;
4135 skip_ioapic_setup
= 1;
4136 goto fake_ioapic_page
;
4140 #ifdef CONFIG_X86_32
4143 ioapic_phys
= (unsigned long)
4144 alloc_bootmem_pages(PAGE_SIZE
);
4145 ioapic_phys
= __pa(ioapic_phys
);
4147 set_fixmap_nocache(idx
, ioapic_phys
);
4148 apic_printk(APIC_VERBOSE
,
4149 "mapped IOAPIC to %08lx (%08lx)\n",
4150 __fix_to_virt(idx
), ioapic_phys
);
4153 ioapic_res
->start
= ioapic_phys
;
4154 ioapic_res
->end
= ioapic_phys
+ (4 * 1024) - 1;
4159 void __init
ioapic_insert_resources(void)
4162 struct resource
*r
= ioapic_resources
;
4167 "IO APIC resources couldn't be allocated.\n");
4171 for (i
= 0; i
< nr_ioapics
; i
++) {
4172 insert_resource(&iomem_resource
, r
);
4177 int mp_find_ioapic(int gsi
)
4181 /* Find the IOAPIC that manages this GSI. */
4182 for (i
= 0; i
< nr_ioapics
; i
++) {
4183 if ((gsi
>= mp_gsi_routing
[i
].gsi_base
)
4184 && (gsi
<= mp_gsi_routing
[i
].gsi_end
))
4188 printk(KERN_ERR
"ERROR: Unable to locate IOAPIC for GSI %d\n", gsi
);
4192 int mp_find_ioapic_pin(int ioapic
, int gsi
)
4194 if (WARN_ON(ioapic
== -1))
4196 if (WARN_ON(gsi
> mp_gsi_routing
[ioapic
].gsi_end
))
4199 return gsi
- mp_gsi_routing
[ioapic
].gsi_base
;
4202 static int bad_ioapic(unsigned long address
)
4204 if (nr_ioapics
>= MAX_IO_APICS
) {
4205 printk(KERN_WARNING
"WARING: Max # of I/O APICs (%d) exceeded "
4206 "(found %d), skipping\n", MAX_IO_APICS
, nr_ioapics
);
4210 printk(KERN_WARNING
"WARNING: Bogus (zero) I/O APIC address"
4211 " found in table, skipping!\n");
4217 void __init
mp_register_ioapic(int id
, u32 address
, u32 gsi_base
)
4221 if (bad_ioapic(address
))
4226 mp_ioapics
[idx
].type
= MP_IOAPIC
;
4227 mp_ioapics
[idx
].flags
= MPC_APIC_USABLE
;
4228 mp_ioapics
[idx
].apicaddr
= address
;
4230 set_fixmap_nocache(FIX_IO_APIC_BASE_0
+ idx
, address
);
4231 mp_ioapics
[idx
].apicid
= io_apic_unique_id(id
);
4232 mp_ioapics
[idx
].apicver
= io_apic_get_version(idx
);
4235 * Build basic GSI lookup table to facilitate gsi->io_apic lookups
4236 * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
4238 mp_gsi_routing
[idx
].gsi_base
= gsi_base
;
4239 mp_gsi_routing
[idx
].gsi_end
= gsi_base
+
4240 io_apic_get_redir_entries(idx
);
4242 printk(KERN_INFO
"IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
4243 "GSI %d-%d\n", idx
, mp_ioapics
[idx
].apicid
,
4244 mp_ioapics
[idx
].apicver
, mp_ioapics
[idx
].apicaddr
,
4245 mp_gsi_routing
[idx
].gsi_base
, mp_gsi_routing
[idx
].gsi_end
);