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>
66 #define __apicdebuginit(type) static type __init
67 #define for_each_irq_pin(entry, head) \
68 for (entry = head; entry; entry = entry->next)
71 * Is the SiS APIC rmw bug present ?
72 * -1 = don't know, 0 = no, 1 = yes
74 int sis_apic_bug = -1;
76 static DEFINE_SPINLOCK(ioapic_lock);
77 static DEFINE_SPINLOCK(vector_lock);
80 * # of IRQ routing registers
82 int nr_ioapic_registers[MAX_IO_APICS];
84 /* I/O APIC entries */
85 struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
88 /* IO APIC gsi routing info */
89 struct mp_ioapic_gsi mp_gsi_routing[MAX_IO_APICS];
91 /* MP IRQ source entries */
92 struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
94 /* # of MP IRQ source entries */
97 /* Number of legacy interrupts */
98 static int nr_legacy_irqs __read_mostly = NR_IRQS_LEGACY;
100 static int nr_irqs_gsi = NR_IRQS_LEGACY;
102 #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
103 int mp_bus_id_to_type[MAX_MP_BUSSES];
106 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
108 int skip_ioapic_setup;
110 void arch_disable_smp_support(void)
114 noioapicreroute = -1;
116 skip_ioapic_setup = 1;
119 static int __init parse_noapic(char *str)
121 /* disable IO-APIC */
122 arch_disable_smp_support();
125 early_param("noapic", parse_noapic);
127 struct irq_pin_list {
129 struct irq_pin_list *next;
132 static struct irq_pin_list *get_one_free_irq_2_pin(int node)
134 struct irq_pin_list *pin;
136 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
141 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
142 #ifdef CONFIG_SPARSE_IRQ
143 static struct irq_cfg irq_cfgx[] = {
145 static struct irq_cfg irq_cfgx[NR_IRQS] = {
147 [0] = { .vector = IRQ0_VECTOR, },
148 [1] = { .vector = IRQ1_VECTOR, },
149 [2] = { .vector = IRQ2_VECTOR, },
150 [3] = { .vector = IRQ3_VECTOR, },
151 [4] = { .vector = IRQ4_VECTOR, },
152 [5] = { .vector = IRQ5_VECTOR, },
153 [6] = { .vector = IRQ6_VECTOR, },
154 [7] = { .vector = IRQ7_VECTOR, },
155 [8] = { .vector = IRQ8_VECTOR, },
156 [9] = { .vector = IRQ9_VECTOR, },
157 [10] = { .vector = IRQ10_VECTOR, },
158 [11] = { .vector = IRQ11_VECTOR, },
159 [12] = { .vector = IRQ12_VECTOR, },
160 [13] = { .vector = IRQ13_VECTOR, },
161 [14] = { .vector = IRQ14_VECTOR, },
162 [15] = { .vector = IRQ15_VECTOR, },
165 void __init io_apic_disable_legacy(void)
171 int __init arch_early_irq_init(void)
174 struct irq_desc *desc;
180 count = ARRAY_SIZE(irq_cfgx);
181 node= cpu_to_node(boot_cpu_id);
183 for (i = 0; i < count; i++) {
184 desc = irq_to_desc(i);
185 desc->chip_data = &cfg[i];
186 zalloc_cpumask_var_node(&cfg[i].domain, GFP_NOWAIT, node);
187 zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_NOWAIT, node);
188 if (i < nr_legacy_irqs)
189 cpumask_setall(cfg[i].domain);
195 #ifdef CONFIG_SPARSE_IRQ
196 struct irq_cfg *irq_cfg(unsigned int irq)
198 struct irq_cfg *cfg = NULL;
199 struct irq_desc *desc;
201 desc = irq_to_desc(irq);
203 cfg = desc->chip_data;
208 static struct irq_cfg *get_one_free_irq_cfg(int node)
212 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
214 if (!zalloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
217 } else if (!zalloc_cpumask_var_node(&cfg->old_domain,
219 free_cpumask_var(cfg->domain);
228 int arch_init_chip_data(struct irq_desc *desc, int node)
232 cfg = desc->chip_data;
234 desc->chip_data = get_one_free_irq_cfg(node);
235 if (!desc->chip_data) {
236 printk(KERN_ERR "can not alloc irq_cfg\n");
244 /* for move_irq_desc */
246 init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int node)
248 struct irq_pin_list *old_entry, *head, *tail, *entry;
250 cfg->irq_2_pin = NULL;
251 old_entry = old_cfg->irq_2_pin;
255 entry = get_one_free_irq_2_pin(node);
259 entry->apic = old_entry->apic;
260 entry->pin = old_entry->pin;
263 old_entry = old_entry->next;
265 entry = get_one_free_irq_2_pin(node);
273 /* still use the old one */
276 entry->apic = old_entry->apic;
277 entry->pin = old_entry->pin;
280 old_entry = old_entry->next;
284 cfg->irq_2_pin = head;
287 static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
289 struct irq_pin_list *entry, *next;
291 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
294 entry = old_cfg->irq_2_pin;
301 old_cfg->irq_2_pin = NULL;
304 void arch_init_copy_chip_data(struct irq_desc *old_desc,
305 struct irq_desc *desc, int node)
308 struct irq_cfg *old_cfg;
310 cfg = get_one_free_irq_cfg(node);
315 desc->chip_data = cfg;
317 old_cfg = old_desc->chip_data;
319 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
321 init_copy_irq_2_pin(old_cfg, cfg, node);
324 static void free_irq_cfg(struct irq_cfg *old_cfg)
329 void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
331 struct irq_cfg *old_cfg, *cfg;
333 old_cfg = old_desc->chip_data;
334 cfg = desc->chip_data;
340 free_irq_2_pin(old_cfg, cfg);
341 free_irq_cfg(old_cfg);
342 old_desc->chip_data = NULL;
345 /* end for move_irq_desc */
348 struct irq_cfg *irq_cfg(unsigned int irq)
350 return irq < nr_irqs ? irq_cfgx + irq : NULL;
357 unsigned int unused[3];
359 unsigned int unused2[11];
363 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
365 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
366 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
369 static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
371 struct io_apic __iomem *io_apic = io_apic_base(apic);
372 writel(vector, &io_apic->eoi);
375 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
377 struct io_apic __iomem *io_apic = io_apic_base(apic);
378 writel(reg, &io_apic->index);
379 return readl(&io_apic->data);
382 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
384 struct io_apic __iomem *io_apic = io_apic_base(apic);
385 writel(reg, &io_apic->index);
386 writel(value, &io_apic->data);
390 * Re-write a value: to be used for read-modify-write
391 * cycles where the read already set up the index register.
393 * Older SiS APIC requires we rewrite the index register
395 static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
397 struct io_apic __iomem *io_apic = io_apic_base(apic);
400 writel(reg, &io_apic->index);
401 writel(value, &io_apic->data);
404 static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
406 struct irq_pin_list *entry;
409 spin_lock_irqsave(&ioapic_lock, flags);
410 for_each_irq_pin(entry, cfg->irq_2_pin) {
415 reg = io_apic_read(entry->apic, 0x10 + pin*2);
416 /* Is the remote IRR bit set? */
417 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
418 spin_unlock_irqrestore(&ioapic_lock, flags);
422 spin_unlock_irqrestore(&ioapic_lock, flags);
428 struct { u32 w1, w2; };
429 struct IO_APIC_route_entry entry;
432 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
434 union entry_union eu;
436 spin_lock_irqsave(&ioapic_lock, flags);
437 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
438 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
439 spin_unlock_irqrestore(&ioapic_lock, flags);
444 * When we write a new IO APIC routing entry, we need to write the high
445 * word first! If the mask bit in the low word is clear, we will enable
446 * the interrupt, and we need to make sure the entry is fully populated
447 * before that happens.
450 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
452 union entry_union eu = {{0, 0}};
455 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
456 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
459 void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
462 spin_lock_irqsave(&ioapic_lock, flags);
463 __ioapic_write_entry(apic, pin, e);
464 spin_unlock_irqrestore(&ioapic_lock, flags);
468 * When we mask an IO APIC routing entry, we need to write the low
469 * word first, in order to set the mask bit before we change the
472 static void ioapic_mask_entry(int apic, int pin)
475 union entry_union eu = { .entry.mask = 1 };
477 spin_lock_irqsave(&ioapic_lock, flags);
478 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
479 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
480 spin_unlock_irqrestore(&ioapic_lock, flags);
484 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
485 * shared ISA-space IRQs, so we have to support them. We are super
486 * fast in the common case, and fast for shared ISA-space IRQs.
489 add_pin_to_irq_node_nopanic(struct irq_cfg *cfg, int node, int apic, int pin)
491 struct irq_pin_list **last, *entry;
493 /* don't allow duplicates */
494 last = &cfg->irq_2_pin;
495 for_each_irq_pin(entry, cfg->irq_2_pin) {
496 if (entry->apic == apic && entry->pin == pin)
501 entry = get_one_free_irq_2_pin(node);
503 printk(KERN_ERR "can not alloc irq_pin_list (%d,%d,%d)\n",
514 static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
516 if (add_pin_to_irq_node_nopanic(cfg, node, apic, pin))
517 panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
521 * Reroute an IRQ to a different pin.
523 static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
524 int oldapic, int oldpin,
525 int newapic, int newpin)
527 struct irq_pin_list *entry;
529 for_each_irq_pin(entry, cfg->irq_2_pin) {
530 if (entry->apic == oldapic && entry->pin == oldpin) {
531 entry->apic = newapic;
533 /* every one is different, right? */
538 /* old apic/pin didn't exist, so just add new ones */
539 add_pin_to_irq_node(cfg, node, newapic, newpin);
542 static void io_apic_modify_irq(struct irq_cfg *cfg,
543 int mask_and, int mask_or,
544 void (*final)(struct irq_pin_list *entry))
547 struct irq_pin_list *entry;
549 for_each_irq_pin(entry, cfg->irq_2_pin) {
552 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
555 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
561 static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
563 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
566 static void io_apic_sync(struct irq_pin_list *entry)
569 * Synchronize the IO-APIC and the CPU by doing
570 * a dummy read from the IO-APIC
572 struct io_apic __iomem *io_apic;
573 io_apic = io_apic_base(entry->apic);
574 readl(&io_apic->data);
577 static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
579 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
582 static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
584 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
585 IO_APIC_REDIR_MASKED, NULL);
588 static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
590 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
591 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
594 static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
596 struct irq_cfg *cfg = desc->chip_data;
601 spin_lock_irqsave(&ioapic_lock, flags);
602 __mask_IO_APIC_irq(cfg);
603 spin_unlock_irqrestore(&ioapic_lock, flags);
606 static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
608 struct irq_cfg *cfg = desc->chip_data;
611 spin_lock_irqsave(&ioapic_lock, flags);
612 __unmask_IO_APIC_irq(cfg);
613 spin_unlock_irqrestore(&ioapic_lock, flags);
616 static void mask_IO_APIC_irq(unsigned int irq)
618 struct irq_desc *desc = irq_to_desc(irq);
620 mask_IO_APIC_irq_desc(desc);
622 static void unmask_IO_APIC_irq(unsigned int irq)
624 struct irq_desc *desc = irq_to_desc(irq);
626 unmask_IO_APIC_irq_desc(desc);
629 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
631 struct IO_APIC_route_entry entry;
633 /* Check delivery_mode to be sure we're not clearing an SMI pin */
634 entry = ioapic_read_entry(apic, pin);
635 if (entry.delivery_mode == dest_SMI)
638 * Disable it in the IO-APIC irq-routing table:
640 ioapic_mask_entry(apic, pin);
643 static void clear_IO_APIC (void)
647 for (apic = 0; apic < nr_ioapics; apic++)
648 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
649 clear_IO_APIC_pin(apic, pin);
654 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
655 * specific CPU-side IRQs.
659 static int pirq_entries[MAX_PIRQS] = {
660 [0 ... MAX_PIRQS - 1] = -1
663 static int __init ioapic_pirq_setup(char *str)
666 int ints[MAX_PIRQS+1];
668 get_options(str, ARRAY_SIZE(ints), ints);
670 apic_printk(APIC_VERBOSE, KERN_INFO
671 "PIRQ redirection, working around broken MP-BIOS.\n");
673 if (ints[0] < MAX_PIRQS)
676 for (i = 0; i < max; i++) {
677 apic_printk(APIC_VERBOSE, KERN_DEBUG
678 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
680 * PIRQs are mapped upside down, usually.
682 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
687 __setup("pirq=", ioapic_pirq_setup);
688 #endif /* CONFIG_X86_32 */
690 struct IO_APIC_route_entry **alloc_ioapic_entries(void)
693 struct IO_APIC_route_entry **ioapic_entries;
695 ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
700 for (apic = 0; apic < nr_ioapics; apic++) {
701 ioapic_entries[apic] =
702 kzalloc(sizeof(struct IO_APIC_route_entry) *
703 nr_ioapic_registers[apic], GFP_ATOMIC);
704 if (!ioapic_entries[apic])
708 return ioapic_entries;
712 kfree(ioapic_entries[apic]);
713 kfree(ioapic_entries);
719 * Saves all the IO-APIC RTE's
721 int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
728 for (apic = 0; apic < nr_ioapics; apic++) {
729 if (!ioapic_entries[apic])
732 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
733 ioapic_entries[apic][pin] =
734 ioapic_read_entry(apic, pin);
741 * Mask all IO APIC entries.
743 void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
750 for (apic = 0; apic < nr_ioapics; apic++) {
751 if (!ioapic_entries[apic])
754 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
755 struct IO_APIC_route_entry entry;
757 entry = ioapic_entries[apic][pin];
760 ioapic_write_entry(apic, pin, entry);
767 * Restore IO APIC entries which was saved in ioapic_entries.
769 int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
776 for (apic = 0; apic < nr_ioapics; apic++) {
777 if (!ioapic_entries[apic])
780 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
781 ioapic_write_entry(apic, pin,
782 ioapic_entries[apic][pin]);
787 void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
791 for (apic = 0; apic < nr_ioapics; apic++)
792 kfree(ioapic_entries[apic]);
794 kfree(ioapic_entries);
798 * Find the IRQ entry number of a certain pin.
800 static int find_irq_entry(int apic, int pin, int type)
804 for (i = 0; i < mp_irq_entries; i++)
805 if (mp_irqs[i].irqtype == type &&
806 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
807 mp_irqs[i].dstapic == MP_APIC_ALL) &&
808 mp_irqs[i].dstirq == pin)
815 * Find the pin to which IRQ[irq] (ISA) is connected
817 static int __init find_isa_irq_pin(int irq, int type)
821 for (i = 0; i < mp_irq_entries; i++) {
822 int lbus = mp_irqs[i].srcbus;
824 if (test_bit(lbus, mp_bus_not_pci) &&
825 (mp_irqs[i].irqtype == type) &&
826 (mp_irqs[i].srcbusirq == irq))
828 return mp_irqs[i].dstirq;
833 static int __init find_isa_irq_apic(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))
845 if (i < mp_irq_entries) {
847 for(apic = 0; apic < nr_ioapics; apic++) {
848 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
856 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
858 * EISA Edge/Level control register, ELCR
860 static int EISA_ELCR(unsigned int irq)
862 if (irq < nr_legacy_irqs) {
863 unsigned int port = 0x4d0 + (irq >> 3);
864 return (inb(port) >> (irq & 7)) & 1;
866 apic_printk(APIC_VERBOSE, KERN_INFO
867 "Broken MPtable reports ISA irq %d\n", irq);
873 /* ISA interrupts are always polarity zero edge triggered,
874 * when listed as conforming in the MP table. */
876 #define default_ISA_trigger(idx) (0)
877 #define default_ISA_polarity(idx) (0)
879 /* EISA interrupts are always polarity zero and can be edge or level
880 * trigger depending on the ELCR value. If an interrupt is listed as
881 * EISA conforming in the MP table, that means its trigger type must
882 * be read in from the ELCR */
884 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
885 #define default_EISA_polarity(idx) default_ISA_polarity(idx)
887 /* PCI interrupts are always polarity one level triggered,
888 * when listed as conforming in the MP table. */
890 #define default_PCI_trigger(idx) (1)
891 #define default_PCI_polarity(idx) (1)
893 /* MCA interrupts are always polarity zero level triggered,
894 * when listed as conforming in the MP table. */
896 #define default_MCA_trigger(idx) (1)
897 #define default_MCA_polarity(idx) default_ISA_polarity(idx)
899 static int MPBIOS_polarity(int idx)
901 int bus = mp_irqs[idx].srcbus;
905 * Determine IRQ line polarity (high active or low active):
907 switch (mp_irqs[idx].irqflag & 3)
909 case 0: /* conforms, ie. bus-type dependent polarity */
910 if (test_bit(bus, mp_bus_not_pci))
911 polarity = default_ISA_polarity(idx);
913 polarity = default_PCI_polarity(idx);
915 case 1: /* high active */
920 case 2: /* reserved */
922 printk(KERN_WARNING "broken BIOS!!\n");
926 case 3: /* low active */
931 default: /* invalid */
933 printk(KERN_WARNING "broken BIOS!!\n");
941 static int MPBIOS_trigger(int idx)
943 int bus = mp_irqs[idx].srcbus;
947 * Determine IRQ trigger mode (edge or level sensitive):
949 switch ((mp_irqs[idx].irqflag>>2) & 3)
951 case 0: /* conforms, ie. bus-type dependent */
952 if (test_bit(bus, mp_bus_not_pci))
953 trigger = default_ISA_trigger(idx);
955 trigger = default_PCI_trigger(idx);
956 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
957 switch (mp_bus_id_to_type[bus]) {
958 case MP_BUS_ISA: /* ISA pin */
960 /* set before the switch */
963 case MP_BUS_EISA: /* EISA pin */
965 trigger = default_EISA_trigger(idx);
968 case MP_BUS_PCI: /* PCI pin */
970 /* set before the switch */
973 case MP_BUS_MCA: /* MCA pin */
975 trigger = default_MCA_trigger(idx);
980 printk(KERN_WARNING "broken BIOS!!\n");
992 case 2: /* reserved */
994 printk(KERN_WARNING "broken BIOS!!\n");
1003 default: /* invalid */
1005 printk(KERN_WARNING "broken BIOS!!\n");
1013 static inline int irq_polarity(int idx)
1015 return MPBIOS_polarity(idx);
1018 static inline int irq_trigger(int idx)
1020 return MPBIOS_trigger(idx);
1023 int (*ioapic_renumber_irq)(int ioapic, int irq);
1024 static int pin_2_irq(int idx, int apic, int pin)
1027 int bus = mp_irqs[idx].srcbus;
1030 * Debugging check, we are in big trouble if this message pops up!
1032 if (mp_irqs[idx].dstirq != pin)
1033 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1035 if (test_bit(bus, mp_bus_not_pci)) {
1036 irq = mp_irqs[idx].srcbusirq;
1039 * PCI IRQs are mapped in order
1043 irq += nr_ioapic_registers[i++];
1046 * For MPS mode, so far only needed by ES7000 platform
1048 if (ioapic_renumber_irq)
1049 irq = ioapic_renumber_irq(apic, irq);
1052 #ifdef CONFIG_X86_32
1054 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1056 if ((pin >= 16) && (pin <= 23)) {
1057 if (pirq_entries[pin-16] != -1) {
1058 if (!pirq_entries[pin-16]) {
1059 apic_printk(APIC_VERBOSE, KERN_DEBUG
1060 "disabling PIRQ%d\n", pin-16);
1062 irq = pirq_entries[pin-16];
1063 apic_printk(APIC_VERBOSE, KERN_DEBUG
1064 "using PIRQ%d -> IRQ %d\n",
1075 * Find a specific PCI IRQ entry.
1076 * Not an __init, possibly needed by modules
1078 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
1079 struct io_apic_irq_attr *irq_attr)
1081 int apic, i, best_guess = -1;
1083 apic_printk(APIC_DEBUG,
1084 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1086 if (test_bit(bus, mp_bus_not_pci)) {
1087 apic_printk(APIC_VERBOSE,
1088 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1091 for (i = 0; i < mp_irq_entries; i++) {
1092 int lbus = mp_irqs[i].srcbus;
1094 for (apic = 0; apic < nr_ioapics; apic++)
1095 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1096 mp_irqs[i].dstapic == MP_APIC_ALL)
1099 if (!test_bit(lbus, mp_bus_not_pci) &&
1100 !mp_irqs[i].irqtype &&
1102 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1103 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1105 if (!(apic || IO_APIC_IRQ(irq)))
1108 if (pin == (mp_irqs[i].srcbusirq & 3)) {
1109 set_io_apic_irq_attr(irq_attr, apic,
1116 * Use the first all-but-pin matching entry as a
1117 * best-guess fuzzy result for broken mptables.
1119 if (best_guess < 0) {
1120 set_io_apic_irq_attr(irq_attr, apic,
1130 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1132 void lock_vector_lock(void)
1134 /* Used to the online set of cpus does not change
1135 * during assign_irq_vector.
1137 spin_lock(&vector_lock);
1140 void unlock_vector_lock(void)
1142 spin_unlock(&vector_lock);
1146 __assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
1149 * NOTE! The local APIC isn't very good at handling
1150 * multiple interrupts at the same interrupt level.
1151 * As the interrupt level is determined by taking the
1152 * vector number and shifting that right by 4, we
1153 * want to spread these out a bit so that they don't
1154 * all fall in the same interrupt level.
1156 * Also, we've got to be careful not to trash gate
1157 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1159 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1160 unsigned int old_vector;
1162 cpumask_var_t tmp_mask;
1164 if (cfg->move_in_progress)
1167 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1170 old_vector = cfg->vector;
1172 cpumask_and(tmp_mask, mask, cpu_online_mask);
1173 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1174 if (!cpumask_empty(tmp_mask)) {
1175 free_cpumask_var(tmp_mask);
1180 /* Only try and allocate irqs on cpus that are present */
1182 for_each_cpu_and(cpu, mask, cpu_online_mask) {
1186 apic->vector_allocation_domain(cpu, tmp_mask);
1188 vector = current_vector;
1189 offset = current_offset;
1192 if (vector >= first_system_vector) {
1193 /* If out of vectors on large boxen, must share them. */
1194 offset = (offset + 1) % 8;
1195 vector = FIRST_DEVICE_VECTOR + offset;
1197 if (unlikely(current_vector == vector))
1200 if (test_bit(vector, used_vectors))
1203 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
1204 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1207 current_vector = vector;
1208 current_offset = offset;
1210 cfg->move_in_progress = 1;
1211 cpumask_copy(cfg->old_domain, cfg->domain);
1213 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
1214 per_cpu(vector_irq, new_cpu)[vector] = irq;
1215 cfg->vector = vector;
1216 cpumask_copy(cfg->domain, tmp_mask);
1220 free_cpumask_var(tmp_mask);
1224 int assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
1227 unsigned long flags;
1229 spin_lock_irqsave(&vector_lock, flags);
1230 err = __assign_irq_vector(irq, cfg, mask);
1231 spin_unlock_irqrestore(&vector_lock, flags);
1235 static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
1239 BUG_ON(!cfg->vector);
1241 vector = cfg->vector;
1242 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
1243 per_cpu(vector_irq, cpu)[vector] = -1;
1246 cpumask_clear(cfg->domain);
1248 if (likely(!cfg->move_in_progress))
1250 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
1251 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1253 if (per_cpu(vector_irq, cpu)[vector] != irq)
1255 per_cpu(vector_irq, cpu)[vector] = -1;
1259 cfg->move_in_progress = 0;
1262 void __setup_vector_irq(int cpu)
1264 /* Initialize vector_irq on a new cpu */
1265 /* This function must be called with vector_lock held */
1267 struct irq_cfg *cfg;
1268 struct irq_desc *desc;
1270 /* Mark the inuse vectors */
1271 for_each_irq_desc(irq, desc) {
1272 cfg = desc->chip_data;
1273 if (!cpumask_test_cpu(cpu, cfg->domain))
1275 vector = cfg->vector;
1276 per_cpu(vector_irq, cpu)[vector] = irq;
1278 /* Mark the free vectors */
1279 for (vector = 0; vector < NR_VECTORS; ++vector) {
1280 irq = per_cpu(vector_irq, cpu)[vector];
1285 if (!cpumask_test_cpu(cpu, cfg->domain))
1286 per_cpu(vector_irq, cpu)[vector] = -1;
1290 static struct irq_chip ioapic_chip;
1291 static struct irq_chip ir_ioapic_chip;
1293 #define IOAPIC_AUTO -1
1294 #define IOAPIC_EDGE 0
1295 #define IOAPIC_LEVEL 1
1297 #ifdef CONFIG_X86_32
1298 static inline int IO_APIC_irq_trigger(int irq)
1302 for (apic = 0; apic < nr_ioapics; apic++) {
1303 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1304 idx = find_irq_entry(apic, pin, mp_INT);
1305 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1306 return irq_trigger(idx);
1310 * nonexistent IRQs are edge default
1315 static inline int IO_APIC_irq_trigger(int irq)
1321 static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
1324 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1325 trigger == IOAPIC_LEVEL)
1326 desc->status |= IRQ_LEVEL;
1328 desc->status &= ~IRQ_LEVEL;
1330 if (irq_remapped(irq)) {
1331 desc->status |= IRQ_MOVE_PCNTXT;
1333 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1337 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1338 handle_edge_irq, "edge");
1342 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1343 trigger == IOAPIC_LEVEL)
1344 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1348 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1349 handle_edge_irq, "edge");
1352 int setup_ioapic_entry(int apic_id, int irq,
1353 struct IO_APIC_route_entry *entry,
1354 unsigned int destination, int trigger,
1355 int polarity, int vector, int pin)
1358 * add it to the IO-APIC irq-routing table:
1360 memset(entry,0,sizeof(*entry));
1362 if (intr_remapping_enabled) {
1363 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
1365 struct IR_IO_APIC_route_entry *ir_entry =
1366 (struct IR_IO_APIC_route_entry *) entry;
1370 panic("No mapping iommu for ioapic %d\n", apic_id);
1372 index = alloc_irte(iommu, irq, 1);
1374 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
1376 memset(&irte, 0, sizeof(irte));
1379 irte.dst_mode = apic->irq_dest_mode;
1381 * Trigger mode in the IRTE will always be edge, and the
1382 * actual level or edge trigger will be setup in the IO-APIC
1383 * RTE. This will help simplify level triggered irq migration.
1384 * For more details, see the comments above explainig IO-APIC
1385 * irq migration in the presence of interrupt-remapping.
1387 irte.trigger_mode = 0;
1388 irte.dlvry_mode = apic->irq_delivery_mode;
1389 irte.vector = vector;
1390 irte.dest_id = IRTE_DEST(destination);
1392 /* Set source-id of interrupt request */
1393 set_ioapic_sid(&irte, apic_id);
1395 modify_irte(irq, &irte);
1397 ir_entry->index2 = (index >> 15) & 0x1;
1399 ir_entry->format = 1;
1400 ir_entry->index = (index & 0x7fff);
1402 * IO-APIC RTE will be configured with virtual vector.
1403 * irq handler will do the explicit EOI to the io-apic.
1405 ir_entry->vector = pin;
1407 entry->delivery_mode = apic->irq_delivery_mode;
1408 entry->dest_mode = apic->irq_dest_mode;
1409 entry->dest = destination;
1410 entry->vector = vector;
1413 entry->mask = 0; /* enable IRQ */
1414 entry->trigger = trigger;
1415 entry->polarity = polarity;
1417 /* Mask level triggered irqs.
1418 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1425 static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
1426 int trigger, int polarity)
1428 struct irq_cfg *cfg;
1429 struct IO_APIC_route_entry entry;
1432 if (!IO_APIC_IRQ(irq))
1435 cfg = desc->chip_data;
1437 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
1440 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
1442 apic_printk(APIC_VERBOSE,KERN_DEBUG
1443 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1444 "IRQ %d Mode:%i Active:%i)\n",
1445 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
1446 irq, trigger, polarity);
1449 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
1450 dest, trigger, polarity, cfg->vector, pin)) {
1451 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1452 mp_ioapics[apic_id].apicid, pin);
1453 __clear_irq_vector(irq, cfg);
1457 ioapic_register_intr(irq, desc, trigger);
1458 if (irq < nr_legacy_irqs)
1459 disable_8259A_irq(irq);
1461 ioapic_write_entry(apic_id, pin, entry);
1465 DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
1466 } mp_ioapic_routing[MAX_IO_APICS];
1468 static void __init setup_IO_APIC_irqs(void)
1470 int apic_id = 0, pin, idx, irq;
1472 struct irq_desc *desc;
1473 struct irq_cfg *cfg;
1474 int node = cpu_to_node(boot_cpu_id);
1476 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1479 if (!acpi_disabled && acpi_ioapic) {
1480 apic_id = mp_find_ioapic(0);
1486 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
1487 idx = find_irq_entry(apic_id, pin, mp_INT);
1491 apic_printk(APIC_VERBOSE,
1492 KERN_DEBUG " %d-%d",
1493 mp_ioapics[apic_id].apicid, pin);
1495 apic_printk(APIC_VERBOSE, " %d-%d",
1496 mp_ioapics[apic_id].apicid, pin);
1500 apic_printk(APIC_VERBOSE,
1501 " (apicid-pin) not connected\n");
1505 irq = pin_2_irq(idx, apic_id, pin);
1508 * Skip the timer IRQ if there's a quirk handler
1509 * installed and if it returns 1:
1511 if (apic->multi_timer_check &&
1512 apic->multi_timer_check(apic_id, irq))
1515 desc = irq_to_desc_alloc_node(irq, node);
1517 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1520 cfg = desc->chip_data;
1521 add_pin_to_irq_node(cfg, node, apic_id, pin);
1523 * don't mark it in pin_programmed, so later acpi could
1524 * set it correctly when irq < 16
1526 setup_IO_APIC_irq(apic_id, pin, irq, desc,
1527 irq_trigger(idx), irq_polarity(idx));
1531 apic_printk(APIC_VERBOSE,
1532 " (apicid-pin) not connected\n");
1536 * Set up the timer pin, possibly with the 8259A-master behind.
1538 static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
1541 struct IO_APIC_route_entry entry;
1543 if (intr_remapping_enabled)
1546 memset(&entry, 0, sizeof(entry));
1549 * We use logical delivery to get the timer IRQ
1552 entry.dest_mode = apic->irq_dest_mode;
1553 entry.mask = 0; /* don't mask IRQ for edge */
1554 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
1555 entry.delivery_mode = apic->irq_delivery_mode;
1558 entry.vector = vector;
1561 * The timer IRQ doesn't have to know that behind the
1562 * scene we may have a 8259A-master in AEOI mode ...
1564 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1567 * Add it to the IO-APIC irq-routing table:
1569 ioapic_write_entry(apic_id, pin, entry);
1573 __apicdebuginit(void) print_IO_APIC(void)
1576 union IO_APIC_reg_00 reg_00;
1577 union IO_APIC_reg_01 reg_01;
1578 union IO_APIC_reg_02 reg_02;
1579 union IO_APIC_reg_03 reg_03;
1580 unsigned long flags;
1581 struct irq_cfg *cfg;
1582 struct irq_desc *desc;
1585 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1586 for (i = 0; i < nr_ioapics; i++)
1587 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1588 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
1591 * We are a bit conservative about what we expect. We have to
1592 * know about every hardware change ASAP.
1594 printk(KERN_INFO "testing the IO APIC.......................\n");
1596 for (apic = 0; apic < nr_ioapics; apic++) {
1598 spin_lock_irqsave(&ioapic_lock, flags);
1599 reg_00.raw = io_apic_read(apic, 0);
1600 reg_01.raw = io_apic_read(apic, 1);
1601 if (reg_01.bits.version >= 0x10)
1602 reg_02.raw = io_apic_read(apic, 2);
1603 if (reg_01.bits.version >= 0x20)
1604 reg_03.raw = io_apic_read(apic, 3);
1605 spin_unlock_irqrestore(&ioapic_lock, flags);
1608 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
1609 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1610 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1611 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1612 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1614 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1615 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1617 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1618 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1621 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1622 * but the value of reg_02 is read as the previous read register
1623 * value, so ignore it if reg_02 == reg_01.
1625 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1626 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1627 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1631 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1632 * or reg_03, but the value of reg_0[23] is read as the previous read
1633 * register value, so ignore it if reg_03 == reg_0[12].
1635 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1636 reg_03.raw != reg_01.raw) {
1637 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1638 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1641 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1643 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1644 " Stat Dmod Deli Vect: \n");
1646 for (i = 0; i <= reg_01.bits.entries; i++) {
1647 struct IO_APIC_route_entry entry;
1649 entry = ioapic_read_entry(apic, i);
1651 printk(KERN_DEBUG " %02x %03X ",
1656 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1661 entry.delivery_status,
1663 entry.delivery_mode,
1668 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1669 for_each_irq_desc(irq, desc) {
1670 struct irq_pin_list *entry;
1672 cfg = desc->chip_data;
1673 entry = cfg->irq_2_pin;
1676 printk(KERN_DEBUG "IRQ%d ", irq);
1677 for_each_irq_pin(entry, cfg->irq_2_pin)
1678 printk("-> %d:%d", entry->apic, entry->pin);
1682 printk(KERN_INFO ".................................... done.\n");
1687 __apicdebuginit(void) print_APIC_field(int base)
1693 for (i = 0; i < 8; i++)
1694 printk(KERN_CONT "%08x", apic_read(base + i*0x10));
1696 printk(KERN_CONT "\n");
1699 __apicdebuginit(void) print_local_APIC(void *dummy)
1701 unsigned int i, v, ver, maxlvt;
1704 printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1705 smp_processor_id(), hard_smp_processor_id());
1706 v = apic_read(APIC_ID);
1707 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
1708 v = apic_read(APIC_LVR);
1709 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1710 ver = GET_APIC_VERSION(v);
1711 maxlvt = lapic_get_maxlvt();
1713 v = apic_read(APIC_TASKPRI);
1714 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1716 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1717 if (!APIC_XAPIC(ver)) {
1718 v = apic_read(APIC_ARBPRI);
1719 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1720 v & APIC_ARBPRI_MASK);
1722 v = apic_read(APIC_PROCPRI);
1723 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1727 * Remote read supported only in the 82489DX and local APIC for
1728 * Pentium processors.
1730 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1731 v = apic_read(APIC_RRR);
1732 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1735 v = apic_read(APIC_LDR);
1736 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1737 if (!x2apic_enabled()) {
1738 v = apic_read(APIC_DFR);
1739 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1741 v = apic_read(APIC_SPIV);
1742 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1744 printk(KERN_DEBUG "... APIC ISR field:\n");
1745 print_APIC_field(APIC_ISR);
1746 printk(KERN_DEBUG "... APIC TMR field:\n");
1747 print_APIC_field(APIC_TMR);
1748 printk(KERN_DEBUG "... APIC IRR field:\n");
1749 print_APIC_field(APIC_IRR);
1751 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1752 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1753 apic_write(APIC_ESR, 0);
1755 v = apic_read(APIC_ESR);
1756 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1759 icr = apic_icr_read();
1760 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1761 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
1763 v = apic_read(APIC_LVTT);
1764 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1766 if (maxlvt > 3) { /* PC is LVT#4. */
1767 v = apic_read(APIC_LVTPC);
1768 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1770 v = apic_read(APIC_LVT0);
1771 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1772 v = apic_read(APIC_LVT1);
1773 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1775 if (maxlvt > 2) { /* ERR is LVT#3. */
1776 v = apic_read(APIC_LVTERR);
1777 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1780 v = apic_read(APIC_TMICT);
1781 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1782 v = apic_read(APIC_TMCCT);
1783 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1784 v = apic_read(APIC_TDCR);
1785 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1787 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1788 v = apic_read(APIC_EFEAT);
1789 maxlvt = (v >> 16) & 0xff;
1790 printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1791 v = apic_read(APIC_ECTRL);
1792 printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1793 for (i = 0; i < maxlvt; i++) {
1794 v = apic_read(APIC_EILVTn(i));
1795 printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1801 __apicdebuginit(void) print_local_APICs(int maxcpu)
1809 for_each_online_cpu(cpu) {
1812 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1817 __apicdebuginit(void) print_PIC(void)
1820 unsigned long flags;
1822 if (!nr_legacy_irqs)
1825 printk(KERN_DEBUG "\nprinting PIC contents\n");
1827 spin_lock_irqsave(&i8259A_lock, flags);
1829 v = inb(0xa1) << 8 | inb(0x21);
1830 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1832 v = inb(0xa0) << 8 | inb(0x20);
1833 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1837 v = inb(0xa0) << 8 | inb(0x20);
1841 spin_unlock_irqrestore(&i8259A_lock, flags);
1843 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1845 v = inb(0x4d1) << 8 | inb(0x4d0);
1846 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1849 static int __initdata show_lapic = 1;
1850 static __init int setup_show_lapic(char *arg)
1854 if (strcmp(arg, "all") == 0) {
1855 show_lapic = CONFIG_NR_CPUS;
1857 get_option(&arg, &num);
1864 __setup("show_lapic=", setup_show_lapic);
1866 __apicdebuginit(int) print_ICs(void)
1868 if (apic_verbosity == APIC_QUIET)
1873 /* don't print out if apic is not there */
1874 if (!cpu_has_apic && !apic_from_smp_config())
1877 print_local_APICs(show_lapic);
1883 fs_initcall(print_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 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 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2238 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2240 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
2241 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2242 free_cpumask_var(cleanup_mask);
2244 cfg->move_in_progress = 0;
2247 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
2250 struct irq_pin_list *entry;
2251 u8 vector = cfg->vector;
2253 for_each_irq_pin(entry, cfg->irq_2_pin) {
2259 * With interrupt-remapping, destination information comes
2260 * from interrupt-remapping table entry.
2262 if (!irq_remapped(irq))
2263 io_apic_write(apic, 0x11 + pin*2, dest);
2264 reg = io_apic_read(apic, 0x10 + pin*2);
2265 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2267 io_apic_modify(apic, 0x10 + pin*2, reg);
2272 * Either sets desc->affinity to a valid value, and returns
2273 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
2274 * leaves desc->affinity untouched.
2277 set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
2279 struct irq_cfg *cfg;
2282 if (!cpumask_intersects(mask, cpu_online_mask))
2286 cfg = desc->chip_data;
2287 if (assign_irq_vector(irq, cfg, mask))
2290 cpumask_copy(desc->affinity, mask);
2292 return apic->cpu_mask_to_apicid_and(desc->affinity, cfg->domain);
2296 set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2298 struct irq_cfg *cfg;
2299 unsigned long flags;
2305 cfg = desc->chip_data;
2307 spin_lock_irqsave(&ioapic_lock, flags);
2308 dest = set_desc_affinity(desc, mask);
2309 if (dest != BAD_APICID) {
2310 /* Only the high 8 bits are valid. */
2311 dest = SET_APIC_LOGICAL_ID(dest);
2312 __target_IO_APIC_irq(irq, dest, cfg);
2315 spin_unlock_irqrestore(&ioapic_lock, flags);
2321 set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
2323 struct irq_desc *desc;
2325 desc = irq_to_desc(irq);
2327 return set_ioapic_affinity_irq_desc(desc, mask);
2330 #ifdef CONFIG_INTR_REMAP
2333 * Migrate the IO-APIC irq in the presence of intr-remapping.
2335 * For both level and edge triggered, irq migration is a simple atomic
2336 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
2338 * For level triggered, we eliminate the io-apic RTE modification (with the
2339 * updated vector information), by using a virtual vector (io-apic pin number).
2340 * Real vector that is used for interrupting cpu will be coming from
2341 * the interrupt-remapping table entry.
2344 migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2346 struct irq_cfg *cfg;
2352 if (!cpumask_intersects(mask, cpu_online_mask))
2356 if (get_irte(irq, &irte))
2359 cfg = desc->chip_data;
2360 if (assign_irq_vector(irq, cfg, mask))
2363 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
2365 irte.vector = cfg->vector;
2366 irte.dest_id = IRTE_DEST(dest);
2369 * Modified the IRTE and flushes the Interrupt entry cache.
2371 modify_irte(irq, &irte);
2373 if (cfg->move_in_progress)
2374 send_cleanup_vector(cfg);
2376 cpumask_copy(desc->affinity, mask);
2382 * Migrates the IRQ destination in the process context.
2384 static int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2385 const struct cpumask *mask)
2387 return migrate_ioapic_irq_desc(desc, mask);
2389 static int set_ir_ioapic_affinity_irq(unsigned int irq,
2390 const struct cpumask *mask)
2392 struct irq_desc *desc = irq_to_desc(irq);
2394 return set_ir_ioapic_affinity_irq_desc(desc, mask);
2397 static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2398 const struct cpumask *mask)
2404 asmlinkage void smp_irq_move_cleanup_interrupt(void)
2406 unsigned vector, me;
2412 me = smp_processor_id();
2413 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2416 struct irq_desc *desc;
2417 struct irq_cfg *cfg;
2418 irq = __get_cpu_var(vector_irq)[vector];
2423 desc = irq_to_desc(irq);
2428 spin_lock(&desc->lock);
2430 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2433 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2435 * Check if the vector that needs to be cleanedup is
2436 * registered at the cpu's IRR. If so, then this is not
2437 * the best time to clean it up. Lets clean it up in the
2438 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2441 if (irr & (1 << (vector % 32))) {
2442 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2445 __get_cpu_var(vector_irq)[vector] = -1;
2447 spin_unlock(&desc->lock);
2453 static void irq_complete_move(struct irq_desc **descp)
2455 struct irq_desc *desc = *descp;
2456 struct irq_cfg *cfg = desc->chip_data;
2457 unsigned vector, me;
2459 if (likely(!cfg->move_in_progress))
2462 vector = ~get_irq_regs()->orig_ax;
2463 me = smp_processor_id();
2465 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2466 send_cleanup_vector(cfg);
2469 static inline void irq_complete_move(struct irq_desc **descp) {}
2472 static void ack_apic_edge(unsigned int irq)
2474 struct irq_desc *desc = irq_to_desc(irq);
2476 irq_complete_move(&desc);
2477 move_native_irq(irq);
2481 atomic_t irq_mis_count;
2483 static void ack_apic_level(unsigned int irq)
2485 struct irq_desc *desc = irq_to_desc(irq);
2488 struct irq_cfg *cfg;
2489 int do_unmask_irq = 0;
2491 irq_complete_move(&desc);
2492 #ifdef CONFIG_GENERIC_PENDING_IRQ
2493 /* If we are moving the irq we need to mask it */
2494 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
2496 mask_IO_APIC_irq_desc(desc);
2501 * It appears there is an erratum which affects at least version 0x11
2502 * of I/O APIC (that's the 82093AA and cores integrated into various
2503 * chipsets). Under certain conditions a level-triggered interrupt is
2504 * erroneously delivered as edge-triggered one but the respective IRR
2505 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2506 * message but it will never arrive and further interrupts are blocked
2507 * from the source. The exact reason is so far unknown, but the
2508 * phenomenon was observed when two consecutive interrupt requests
2509 * from a given source get delivered to the same CPU and the source is
2510 * temporarily disabled in between.
2512 * A workaround is to simulate an EOI message manually. We achieve it
2513 * by setting the trigger mode to edge and then to level when the edge
2514 * trigger mode gets detected in the TMR of a local APIC for a
2515 * level-triggered interrupt. We mask the source for the time of the
2516 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2517 * The idea is from Manfred Spraul. --macro
2519 cfg = desc->chip_data;
2521 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2524 * We must acknowledge the irq before we move it or the acknowledge will
2525 * not propagate properly.
2529 /* Now we can move and renable the irq */
2530 if (unlikely(do_unmask_irq)) {
2531 /* Only migrate the irq if the ack has been received.
2533 * On rare occasions the broadcast level triggered ack gets
2534 * delayed going to ioapics, and if we reprogram the
2535 * vector while Remote IRR is still set the irq will never
2538 * To prevent this scenario we read the Remote IRR bit
2539 * of the ioapic. This has two effects.
2540 * - On any sane system the read of the ioapic will
2541 * flush writes (and acks) going to the ioapic from
2543 * - We get to see if the ACK has actually been delivered.
2545 * Based on failed experiments of reprogramming the
2546 * ioapic entry from outside of irq context starting
2547 * with masking the ioapic entry and then polling until
2548 * Remote IRR was clear before reprogramming the
2549 * ioapic I don't trust the Remote IRR bit to be
2550 * completey accurate.
2552 * However there appears to be no other way to plug
2553 * this race, so if the Remote IRR bit is not
2554 * accurate and is causing problems then it is a hardware bug
2555 * and you can go talk to the chipset vendor about it.
2557 cfg = desc->chip_data;
2558 if (!io_apic_level_ack_pending(cfg))
2559 move_masked_irq(irq);
2560 unmask_IO_APIC_irq_desc(desc);
2563 /* Tail end of version 0x11 I/O APIC bug workaround */
2564 if (!(v & (1 << (i & 0x1f)))) {
2565 atomic_inc(&irq_mis_count);
2566 spin_lock(&ioapic_lock);
2567 __mask_and_edge_IO_APIC_irq(cfg);
2568 __unmask_and_level_IO_APIC_irq(cfg);
2569 spin_unlock(&ioapic_lock);
2573 #ifdef CONFIG_INTR_REMAP
2574 static void __eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2576 struct irq_pin_list *entry;
2578 for_each_irq_pin(entry, cfg->irq_2_pin)
2579 io_apic_eoi(entry->apic, entry->pin);
2583 eoi_ioapic_irq(struct irq_desc *desc)
2585 struct irq_cfg *cfg;
2586 unsigned long flags;
2590 cfg = desc->chip_data;
2592 spin_lock_irqsave(&ioapic_lock, flags);
2593 __eoi_ioapic_irq(irq, cfg);
2594 spin_unlock_irqrestore(&ioapic_lock, flags);
2597 static void ir_ack_apic_edge(unsigned int irq)
2602 static void ir_ack_apic_level(unsigned int irq)
2604 struct irq_desc *desc = irq_to_desc(irq);
2607 eoi_ioapic_irq(desc);
2609 #endif /* CONFIG_INTR_REMAP */
2611 static struct irq_chip ioapic_chip __read_mostly = {
2613 .startup = startup_ioapic_irq,
2614 .mask = mask_IO_APIC_irq,
2615 .unmask = unmask_IO_APIC_irq,
2616 .ack = ack_apic_edge,
2617 .eoi = ack_apic_level,
2619 .set_affinity = set_ioapic_affinity_irq,
2621 .retrigger = ioapic_retrigger_irq,
2624 static struct irq_chip ir_ioapic_chip __read_mostly = {
2625 .name = "IR-IO-APIC",
2626 .startup = startup_ioapic_irq,
2627 .mask = mask_IO_APIC_irq,
2628 .unmask = unmask_IO_APIC_irq,
2629 #ifdef CONFIG_INTR_REMAP
2630 .ack = ir_ack_apic_edge,
2631 .eoi = ir_ack_apic_level,
2633 .set_affinity = set_ir_ioapic_affinity_irq,
2636 .retrigger = ioapic_retrigger_irq,
2639 static inline void init_IO_APIC_traps(void)
2642 struct irq_desc *desc;
2643 struct irq_cfg *cfg;
2646 * NOTE! The local APIC isn't very good at handling
2647 * multiple interrupts at the same interrupt level.
2648 * As the interrupt level is determined by taking the
2649 * vector number and shifting that right by 4, we
2650 * want to spread these out a bit so that they don't
2651 * all fall in the same interrupt level.
2653 * Also, we've got to be careful not to trash gate
2654 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2656 for_each_irq_desc(irq, desc) {
2657 cfg = desc->chip_data;
2658 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
2660 * Hmm.. We don't have an entry for this,
2661 * so default to an old-fashioned 8259
2662 * interrupt if we can..
2664 if (irq < nr_legacy_irqs)
2665 make_8259A_irq(irq);
2667 /* Strange. Oh, well.. */
2668 desc->chip = &no_irq_chip;
2674 * The local APIC irq-chip implementation:
2677 static void mask_lapic_irq(unsigned int irq)
2681 v = apic_read(APIC_LVT0);
2682 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2685 static void unmask_lapic_irq(unsigned int irq)
2689 v = apic_read(APIC_LVT0);
2690 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2693 static void ack_lapic_irq(unsigned int irq)
2698 static struct irq_chip lapic_chip __read_mostly = {
2699 .name = "local-APIC",
2700 .mask = mask_lapic_irq,
2701 .unmask = unmask_lapic_irq,
2702 .ack = ack_lapic_irq,
2705 static void lapic_register_intr(int irq, struct irq_desc *desc)
2707 desc->status &= ~IRQ_LEVEL;
2708 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2712 static void __init setup_nmi(void)
2715 * Dirty trick to enable the NMI watchdog ...
2716 * We put the 8259A master into AEOI mode and
2717 * unmask on all local APICs LVT0 as NMI.
2719 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2720 * is from Maciej W. Rozycki - so we do not have to EOI from
2721 * the NMI handler or the timer interrupt.
2723 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2725 enable_NMI_through_LVT0();
2727 apic_printk(APIC_VERBOSE, " done.\n");
2731 * This looks a bit hackish but it's about the only one way of sending
2732 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2733 * not support the ExtINT mode, unfortunately. We need to send these
2734 * cycles as some i82489DX-based boards have glue logic that keeps the
2735 * 8259A interrupt line asserted until INTA. --macro
2737 static inline void __init unlock_ExtINT_logic(void)
2740 struct IO_APIC_route_entry entry0, entry1;
2741 unsigned char save_control, save_freq_select;
2743 pin = find_isa_irq_pin(8, mp_INT);
2748 apic = find_isa_irq_apic(8, mp_INT);
2754 entry0 = ioapic_read_entry(apic, pin);
2755 clear_IO_APIC_pin(apic, pin);
2757 memset(&entry1, 0, sizeof(entry1));
2759 entry1.dest_mode = 0; /* physical delivery */
2760 entry1.mask = 0; /* unmask IRQ now */
2761 entry1.dest = hard_smp_processor_id();
2762 entry1.delivery_mode = dest_ExtINT;
2763 entry1.polarity = entry0.polarity;
2767 ioapic_write_entry(apic, pin, entry1);
2769 save_control = CMOS_READ(RTC_CONTROL);
2770 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2771 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2773 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2778 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2782 CMOS_WRITE(save_control, RTC_CONTROL);
2783 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2784 clear_IO_APIC_pin(apic, pin);
2786 ioapic_write_entry(apic, pin, entry0);
2789 static int disable_timer_pin_1 __initdata;
2790 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
2791 static int __init disable_timer_pin_setup(char *arg)
2793 disable_timer_pin_1 = 1;
2796 early_param("disable_timer_pin_1", disable_timer_pin_setup);
2798 int timer_through_8259 __initdata;
2801 * This code may look a bit paranoid, but it's supposed to cooperate with
2802 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2803 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2804 * fanatically on his truly buggy board.
2806 * FIXME: really need to revamp this for all platforms.
2808 static inline void __init check_timer(void)
2810 struct irq_desc *desc = irq_to_desc(0);
2811 struct irq_cfg *cfg = desc->chip_data;
2812 int node = cpu_to_node(boot_cpu_id);
2813 int apic1, pin1, apic2, pin2;
2814 unsigned long flags;
2817 local_irq_save(flags);
2820 * get/set the timer IRQ vector:
2822 disable_8259A_irq(0);
2823 assign_irq_vector(0, cfg, apic->target_cpus());
2826 * As IRQ0 is to be enabled in the 8259A, the virtual
2827 * wire has to be disabled in the local APIC. Also
2828 * timer interrupts need to be acknowledged manually in
2829 * the 8259A for the i82489DX when using the NMI
2830 * watchdog as that APIC treats NMIs as level-triggered.
2831 * The AEOI mode will finish them in the 8259A
2834 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2836 #ifdef CONFIG_X86_32
2840 ver = apic_read(APIC_LVR);
2841 ver = GET_APIC_VERSION(ver);
2842 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2846 pin1 = find_isa_irq_pin(0, mp_INT);
2847 apic1 = find_isa_irq_apic(0, mp_INT);
2848 pin2 = ioapic_i8259.pin;
2849 apic2 = ioapic_i8259.apic;
2851 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2852 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2853 cfg->vector, apic1, pin1, apic2, pin2);
2856 * Some BIOS writers are clueless and report the ExtINTA
2857 * I/O APIC input from the cascaded 8259A as the timer
2858 * interrupt input. So just in case, if only one pin
2859 * was found above, try it both directly and through the
2863 if (intr_remapping_enabled)
2864 panic("BIOS bug: timer not connected to IO-APIC");
2868 } else if (pin2 == -1) {
2875 * Ok, does IRQ0 through the IOAPIC work?
2878 add_pin_to_irq_node(cfg, node, apic1, pin1);
2879 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2881 /* for edge trigger, setup_IO_APIC_irq already
2882 * leave it unmasked.
2883 * so only need to unmask if it is level-trigger
2884 * do we really have level trigger timer?
2887 idx = find_irq_entry(apic1, pin1, mp_INT);
2888 if (idx != -1 && irq_trigger(idx))
2889 unmask_IO_APIC_irq_desc(desc);
2891 if (timer_irq_works()) {
2892 if (nmi_watchdog == NMI_IO_APIC) {
2894 enable_8259A_irq(0);
2896 if (disable_timer_pin_1 > 0)
2897 clear_IO_APIC_pin(0, pin1);
2900 if (intr_remapping_enabled)
2901 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2902 local_irq_disable();
2903 clear_IO_APIC_pin(apic1, pin1);
2905 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2906 "8254 timer not connected to IO-APIC\n");
2908 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2909 "(IRQ0) through the 8259A ...\n");
2910 apic_printk(APIC_QUIET, KERN_INFO
2911 "..... (found apic %d pin %d) ...\n", apic2, pin2);
2913 * legacy devices should be connected to IO APIC #0
2915 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
2916 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2917 enable_8259A_irq(0);
2918 if (timer_irq_works()) {
2919 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2920 timer_through_8259 = 1;
2921 if (nmi_watchdog == NMI_IO_APIC) {
2922 disable_8259A_irq(0);
2924 enable_8259A_irq(0);
2929 * Cleanup, just in case ...
2931 local_irq_disable();
2932 disable_8259A_irq(0);
2933 clear_IO_APIC_pin(apic2, pin2);
2934 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2937 if (nmi_watchdog == NMI_IO_APIC) {
2938 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2939 "through the IO-APIC - disabling NMI Watchdog!\n");
2940 nmi_watchdog = NMI_NONE;
2942 #ifdef CONFIG_X86_32
2946 apic_printk(APIC_QUIET, KERN_INFO
2947 "...trying to set up timer as Virtual Wire IRQ...\n");
2949 lapic_register_intr(0, desc);
2950 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
2951 enable_8259A_irq(0);
2953 if (timer_irq_works()) {
2954 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2957 local_irq_disable();
2958 disable_8259A_irq(0);
2959 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2960 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2962 apic_printk(APIC_QUIET, KERN_INFO
2963 "...trying to set up timer as ExtINT IRQ...\n");
2967 apic_write(APIC_LVT0, APIC_DM_EXTINT);
2969 unlock_ExtINT_logic();
2971 if (timer_irq_works()) {
2972 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2975 local_irq_disable();
2976 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2977 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2978 "report. Then try booting with the 'noapic' option.\n");
2980 local_irq_restore(flags);
2984 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2985 * to devices. However there may be an I/O APIC pin available for
2986 * this interrupt regardless. The pin may be left unconnected, but
2987 * typically it will be reused as an ExtINT cascade interrupt for
2988 * the master 8259A. In the MPS case such a pin will normally be
2989 * reported as an ExtINT interrupt in the MP table. With ACPI
2990 * there is no provision for ExtINT interrupts, and in the absence
2991 * of an override it would be treated as an ordinary ISA I/O APIC
2992 * interrupt, that is edge-triggered and unmasked by default. We
2993 * used to do this, but it caused problems on some systems because
2994 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2995 * the same ExtINT cascade interrupt to drive the local APIC of the
2996 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2997 * the I/O APIC in all cases now. No actual device should request
2998 * it anyway. --macro
3000 #define PIC_IRQS (1UL << PIC_CASCADE_IR)
3002 void __init setup_IO_APIC(void)
3006 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3008 io_apic_irqs = nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
3010 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
3012 * Set up IO-APIC IRQ routing.
3014 x86_init.mpparse.setup_ioapic_ids();
3017 setup_IO_APIC_irqs();
3018 init_IO_APIC_traps();
3024 * Called after all the initialization is done. If we didnt find any
3025 * APIC bugs then we can allow the modify fast path
3028 static int __init io_apic_bug_finalize(void)
3030 if (sis_apic_bug == -1)
3035 late_initcall(io_apic_bug_finalize);
3037 struct sysfs_ioapic_data {
3038 struct sys_device dev;
3039 struct IO_APIC_route_entry entry[0];
3041 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
3043 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
3045 struct IO_APIC_route_entry *entry;
3046 struct sysfs_ioapic_data *data;
3049 data = container_of(dev, struct sysfs_ioapic_data, dev);
3050 entry = data->entry;
3051 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3052 *entry = ioapic_read_entry(dev->id, i);
3057 static int ioapic_resume(struct sys_device *dev)
3059 struct IO_APIC_route_entry *entry;
3060 struct sysfs_ioapic_data *data;
3061 unsigned long flags;
3062 union IO_APIC_reg_00 reg_00;
3065 data = container_of(dev, struct sysfs_ioapic_data, dev);
3066 entry = data->entry;
3068 spin_lock_irqsave(&ioapic_lock, flags);
3069 reg_00.raw = io_apic_read(dev->id, 0);
3070 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3071 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
3072 io_apic_write(dev->id, 0, reg_00.raw);
3074 spin_unlock_irqrestore(&ioapic_lock, flags);
3075 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
3076 ioapic_write_entry(dev->id, i, entry[i]);
3081 static struct sysdev_class ioapic_sysdev_class = {
3083 .suspend = ioapic_suspend,
3084 .resume = ioapic_resume,
3087 static int __init ioapic_init_sysfs(void)
3089 struct sys_device * dev;
3092 error = sysdev_class_register(&ioapic_sysdev_class);
3096 for (i = 0; i < nr_ioapics; i++ ) {
3097 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
3098 * sizeof(struct IO_APIC_route_entry);
3099 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
3100 if (!mp_ioapic_data[i]) {
3101 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3104 dev = &mp_ioapic_data[i]->dev;
3106 dev->cls = &ioapic_sysdev_class;
3107 error = sysdev_register(dev);
3109 kfree(mp_ioapic_data[i]);
3110 mp_ioapic_data[i] = NULL;
3111 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3119 device_initcall(ioapic_init_sysfs);
3122 * Dynamic irq allocate and deallocation
3124 unsigned int create_irq_nr(unsigned int irq_want, int node)
3126 /* Allocate an unused irq */
3129 unsigned long flags;
3130 struct irq_cfg *cfg_new = NULL;
3131 struct irq_desc *desc_new = NULL;
3134 if (irq_want < nr_irqs_gsi)
3135 irq_want = nr_irqs_gsi;
3137 spin_lock_irqsave(&vector_lock, flags);
3138 for (new = irq_want; new < nr_irqs; new++) {
3139 desc_new = irq_to_desc_alloc_node(new, node);
3141 printk(KERN_INFO "can not get irq_desc for %d\n", new);
3144 cfg_new = desc_new->chip_data;
3146 if (cfg_new->vector != 0)
3149 desc_new = move_irq_desc(desc_new, node);
3151 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
3155 spin_unlock_irqrestore(&vector_lock, flags);
3158 dynamic_irq_init(irq);
3159 /* restore it, in case dynamic_irq_init clear it */
3161 desc_new->chip_data = cfg_new;
3166 int create_irq(void)
3168 int node = cpu_to_node(boot_cpu_id);
3169 unsigned int irq_want;
3172 irq_want = nr_irqs_gsi;
3173 irq = create_irq_nr(irq_want, node);
3181 void destroy_irq(unsigned int irq)
3183 unsigned long flags;
3184 struct irq_cfg *cfg;
3185 struct irq_desc *desc;
3187 /* store it, in case dynamic_irq_cleanup clear it */
3188 desc = irq_to_desc(irq);
3189 cfg = desc->chip_data;
3190 dynamic_irq_cleanup(irq);
3191 /* connect back irq_cfg */
3192 desc->chip_data = cfg;
3195 spin_lock_irqsave(&vector_lock, flags);
3196 __clear_irq_vector(irq, cfg);
3197 spin_unlock_irqrestore(&vector_lock, flags);
3201 * MSI message composition
3203 #ifdef CONFIG_PCI_MSI
3204 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
3206 struct irq_cfg *cfg;
3214 err = assign_irq_vector(irq, cfg, apic->target_cpus());
3218 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
3220 if (irq_remapped(irq)) {
3225 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3226 BUG_ON(ir_index == -1);
3228 memset (&irte, 0, sizeof(irte));
3231 irte.dst_mode = apic->irq_dest_mode;
3232 irte.trigger_mode = 0; /* edge */
3233 irte.dlvry_mode = apic->irq_delivery_mode;
3234 irte.vector = cfg->vector;
3235 irte.dest_id = IRTE_DEST(dest);
3237 /* Set source-id of interrupt request */
3238 set_msi_sid(&irte, pdev);
3240 modify_irte(irq, &irte);
3242 msg->address_hi = MSI_ADDR_BASE_HI;
3243 msg->data = sub_handle;
3244 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3246 MSI_ADDR_IR_INDEX1(ir_index) |
3247 MSI_ADDR_IR_INDEX2(ir_index);
3249 if (x2apic_enabled())
3250 msg->address_hi = MSI_ADDR_BASE_HI |
3251 MSI_ADDR_EXT_DEST_ID(dest);
3253 msg->address_hi = MSI_ADDR_BASE_HI;
3257 ((apic->irq_dest_mode == 0) ?
3258 MSI_ADDR_DEST_MODE_PHYSICAL:
3259 MSI_ADDR_DEST_MODE_LOGICAL) |
3260 ((apic->irq_delivery_mode != dest_LowestPrio) ?
3261 MSI_ADDR_REDIRECTION_CPU:
3262 MSI_ADDR_REDIRECTION_LOWPRI) |
3263 MSI_ADDR_DEST_ID(dest);
3266 MSI_DATA_TRIGGER_EDGE |
3267 MSI_DATA_LEVEL_ASSERT |
3268 ((apic->irq_delivery_mode != dest_LowestPrio) ?
3269 MSI_DATA_DELIVERY_FIXED:
3270 MSI_DATA_DELIVERY_LOWPRI) |
3271 MSI_DATA_VECTOR(cfg->vector);
3277 static int set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
3279 struct irq_desc *desc = irq_to_desc(irq);
3280 struct irq_cfg *cfg;
3284 dest = set_desc_affinity(desc, mask);
3285 if (dest == BAD_APICID)
3288 cfg = desc->chip_data;
3290 read_msi_msg_desc(desc, &msg);
3292 msg.data &= ~MSI_DATA_VECTOR_MASK;
3293 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3294 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3295 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3297 write_msi_msg_desc(desc, &msg);
3301 #ifdef CONFIG_INTR_REMAP
3303 * Migrate the MSI irq to another cpumask. This migration is
3304 * done in the process context using interrupt-remapping hardware.
3307 ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
3309 struct irq_desc *desc = irq_to_desc(irq);
3310 struct irq_cfg *cfg = desc->chip_data;
3314 if (get_irte(irq, &irte))
3317 dest = set_desc_affinity(desc, mask);
3318 if (dest == BAD_APICID)
3321 irte.vector = cfg->vector;
3322 irte.dest_id = IRTE_DEST(dest);
3325 * atomically update the IRTE with the new destination and vector.
3327 modify_irte(irq, &irte);
3330 * After this point, all the interrupts will start arriving
3331 * at the new destination. So, time to cleanup the previous
3332 * vector allocation.
3334 if (cfg->move_in_progress)
3335 send_cleanup_vector(cfg);
3341 #endif /* CONFIG_SMP */
3344 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3345 * which implement the MSI or MSI-X Capability Structure.
3347 static struct irq_chip msi_chip = {
3349 .unmask = unmask_msi_irq,
3350 .mask = mask_msi_irq,
3351 .ack = ack_apic_edge,
3353 .set_affinity = set_msi_irq_affinity,
3355 .retrigger = ioapic_retrigger_irq,
3358 static struct irq_chip msi_ir_chip = {
3359 .name = "IR-PCI-MSI",
3360 .unmask = unmask_msi_irq,
3361 .mask = mask_msi_irq,
3362 #ifdef CONFIG_INTR_REMAP
3363 .ack = ir_ack_apic_edge,
3365 .set_affinity = ir_set_msi_irq_affinity,
3368 .retrigger = ioapic_retrigger_irq,
3372 * Map the PCI dev to the corresponding remapping hardware unit
3373 * and allocate 'nvec' consecutive interrupt-remapping table entries
3376 static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3378 struct intel_iommu *iommu;
3381 iommu = map_dev_to_ir(dev);
3384 "Unable to map PCI %s to iommu\n", pci_name(dev));
3388 index = alloc_irte(iommu, irq, nvec);
3391 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3398 static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
3403 ret = msi_compose_msg(dev, irq, &msg);
3407 set_irq_msi(irq, msidesc);
3408 write_msi_msg(irq, &msg);
3410 if (irq_remapped(irq)) {
3411 struct irq_desc *desc = irq_to_desc(irq);
3413 * irq migration in process context
3415 desc->status |= IRQ_MOVE_PCNTXT;
3416 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3418 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
3420 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3425 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3428 int ret, sub_handle;
3429 struct msi_desc *msidesc;
3430 unsigned int irq_want;
3431 struct intel_iommu *iommu = NULL;
3435 /* x86 doesn't support multiple MSI yet */
3436 if (type == PCI_CAP_ID_MSI && nvec > 1)
3439 node = dev_to_node(&dev->dev);
3440 irq_want = nr_irqs_gsi;
3442 list_for_each_entry(msidesc, &dev->msi_list, list) {
3443 irq = create_irq_nr(irq_want, node);
3447 if (!intr_remapping_enabled)
3452 * allocate the consecutive block of IRTE's
3455 index = msi_alloc_irte(dev, irq, nvec);
3461 iommu = map_dev_to_ir(dev);
3467 * setup the mapping between the irq and the IRTE
3468 * base index, the sub_handle pointing to the
3469 * appropriate interrupt remap table entry.
3471 set_irte_irq(irq, iommu, index, sub_handle);
3474 ret = setup_msi_irq(dev, msidesc, irq);
3486 void arch_teardown_msi_irq(unsigned int irq)
3491 #if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
3493 static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
3495 struct irq_desc *desc = irq_to_desc(irq);
3496 struct irq_cfg *cfg;
3500 dest = set_desc_affinity(desc, mask);
3501 if (dest == BAD_APICID)
3504 cfg = desc->chip_data;
3506 dmar_msi_read(irq, &msg);
3508 msg.data &= ~MSI_DATA_VECTOR_MASK;
3509 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3510 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3511 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3513 dmar_msi_write(irq, &msg);
3518 #endif /* CONFIG_SMP */
3520 static struct irq_chip dmar_msi_type = {
3522 .unmask = dmar_msi_unmask,
3523 .mask = dmar_msi_mask,
3524 .ack = ack_apic_edge,
3526 .set_affinity = dmar_msi_set_affinity,
3528 .retrigger = ioapic_retrigger_irq,
3531 int arch_setup_dmar_msi(unsigned int irq)
3536 ret = msi_compose_msg(NULL, irq, &msg);
3539 dmar_msi_write(irq, &msg);
3540 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3546 #ifdef CONFIG_HPET_TIMER
3549 static int hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
3551 struct irq_desc *desc = irq_to_desc(irq);
3552 struct irq_cfg *cfg;
3556 dest = set_desc_affinity(desc, mask);
3557 if (dest == BAD_APICID)
3560 cfg = desc->chip_data;
3562 hpet_msi_read(irq, &msg);
3564 msg.data &= ~MSI_DATA_VECTOR_MASK;
3565 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3566 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3567 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3569 hpet_msi_write(irq, &msg);
3574 #endif /* CONFIG_SMP */
3576 static struct irq_chip hpet_msi_type = {
3578 .unmask = hpet_msi_unmask,
3579 .mask = hpet_msi_mask,
3580 .ack = ack_apic_edge,
3582 .set_affinity = hpet_msi_set_affinity,
3584 .retrigger = ioapic_retrigger_irq,
3587 int arch_setup_hpet_msi(unsigned int irq)
3591 struct irq_desc *desc = irq_to_desc(irq);
3593 ret = msi_compose_msg(NULL, irq, &msg);
3597 hpet_msi_write(irq, &msg);
3598 desc->status |= IRQ_MOVE_PCNTXT;
3599 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3606 #endif /* CONFIG_PCI_MSI */
3608 * Hypertransport interrupt support
3610 #ifdef CONFIG_HT_IRQ
3614 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
3616 struct ht_irq_msg msg;
3617 fetch_ht_irq_msg(irq, &msg);
3619 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
3620 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
3622 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
3623 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
3625 write_ht_irq_msg(irq, &msg);
3628 static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
3630 struct irq_desc *desc = irq_to_desc(irq);
3631 struct irq_cfg *cfg;
3634 dest = set_desc_affinity(desc, mask);
3635 if (dest == BAD_APICID)
3638 cfg = desc->chip_data;
3640 target_ht_irq(irq, dest, cfg->vector);
3647 static struct irq_chip ht_irq_chip = {
3649 .mask = mask_ht_irq,
3650 .unmask = unmask_ht_irq,
3651 .ack = ack_apic_edge,
3653 .set_affinity = set_ht_irq_affinity,
3655 .retrigger = ioapic_retrigger_irq,
3658 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3660 struct irq_cfg *cfg;
3667 err = assign_irq_vector(irq, cfg, apic->target_cpus());
3669 struct ht_irq_msg msg;
3672 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3673 apic->target_cpus());
3675 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
3679 HT_IRQ_LOW_DEST_ID(dest) |
3680 HT_IRQ_LOW_VECTOR(cfg->vector) |
3681 ((apic->irq_dest_mode == 0) ?
3682 HT_IRQ_LOW_DM_PHYSICAL :
3683 HT_IRQ_LOW_DM_LOGICAL) |
3684 HT_IRQ_LOW_RQEOI_EDGE |
3685 ((apic->irq_delivery_mode != dest_LowestPrio) ?
3686 HT_IRQ_LOW_MT_FIXED :
3687 HT_IRQ_LOW_MT_ARBITRATED) |
3688 HT_IRQ_LOW_IRQ_MASKED;
3690 write_ht_irq_msg(irq, &msg);
3692 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3693 handle_edge_irq, "edge");
3695 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
3699 #endif /* CONFIG_HT_IRQ */
3701 int __init io_apic_get_redir_entries (int ioapic)
3703 union IO_APIC_reg_01 reg_01;
3704 unsigned long flags;
3706 spin_lock_irqsave(&ioapic_lock, flags);
3707 reg_01.raw = io_apic_read(ioapic, 1);
3708 spin_unlock_irqrestore(&ioapic_lock, flags);
3710 return reg_01.bits.entries;
3713 void __init probe_nr_irqs_gsi(void)
3717 nr = acpi_probe_gsi();
3718 if (nr > nr_irqs_gsi) {
3721 /* for acpi=off or acpi is not compiled in */
3725 for (idx = 0; idx < nr_ioapics; idx++)
3726 nr += io_apic_get_redir_entries(idx) + 1;
3728 if (nr > nr_irqs_gsi)
3732 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
3735 #ifdef CONFIG_SPARSE_IRQ
3736 int __init arch_probe_nr_irqs(void)
3740 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3741 nr_irqs = NR_VECTORS * nr_cpu_ids;
3743 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3744 #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3746 * for MSI and HT dyn irq
3748 nr += nr_irqs_gsi * 16;
3757 static int __io_apic_set_pci_routing(struct device *dev, int irq,
3758 struct io_apic_irq_attr *irq_attr)
3760 struct irq_desc *desc;
3761 struct irq_cfg *cfg;
3764 int trigger, polarity;
3766 ioapic = irq_attr->ioapic;
3767 if (!IO_APIC_IRQ(irq)) {
3768 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3774 node = dev_to_node(dev);
3776 node = cpu_to_node(boot_cpu_id);
3778 desc = irq_to_desc_alloc_node(irq, node);
3780 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3784 pin = irq_attr->ioapic_pin;
3785 trigger = irq_attr->trigger;
3786 polarity = irq_attr->polarity;
3789 * IRQs < 16 are already in the irq_2_pin[] map
3791 if (irq >= nr_legacy_irqs) {
3792 cfg = desc->chip_data;
3793 if (add_pin_to_irq_node_nopanic(cfg, node, ioapic, pin)) {
3794 printk(KERN_INFO "can not add pin %d for irq %d\n",
3800 setup_IO_APIC_irq(ioapic, pin, irq, desc, trigger, polarity);
3805 int io_apic_set_pci_routing(struct device *dev, int irq,
3806 struct io_apic_irq_attr *irq_attr)
3810 * Avoid pin reprogramming. PRTs typically include entries
3811 * with redundant pin->gsi mappings (but unique PCI devices);
3812 * we only program the IOAPIC on the first.
3814 ioapic = irq_attr->ioapic;
3815 pin = irq_attr->ioapic_pin;
3816 if (test_bit(pin, mp_ioapic_routing[ioapic].pin_programmed)) {
3817 pr_debug("Pin %d-%d already programmed\n",
3818 mp_ioapics[ioapic].apicid, pin);
3821 set_bit(pin, mp_ioapic_routing[ioapic].pin_programmed);
3823 return __io_apic_set_pci_routing(dev, irq, irq_attr);
3826 u8 __init io_apic_unique_id(u8 id)
3828 #ifdef CONFIG_X86_32
3829 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
3830 !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
3831 return io_apic_get_unique_id(nr_ioapics, id);
3836 DECLARE_BITMAP(used, 256);
3838 bitmap_zero(used, 256);
3839 for (i = 0; i < nr_ioapics; i++) {
3840 struct mpc_ioapic *ia = &mp_ioapics[i];
3841 __set_bit(ia->apicid, used);
3843 if (!test_bit(id, used))
3845 return find_first_zero_bit(used, 256);
3849 #ifdef CONFIG_X86_32
3850 int __init io_apic_get_unique_id(int ioapic, int apic_id)
3852 union IO_APIC_reg_00 reg_00;
3853 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3855 unsigned long flags;
3859 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3860 * buses (one for LAPICs, one for IOAPICs), where predecessors only
3861 * supports up to 16 on one shared APIC bus.
3863 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3864 * advantage of new APIC bus architecture.
3867 if (physids_empty(apic_id_map))
3868 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
3870 spin_lock_irqsave(&ioapic_lock, flags);
3871 reg_00.raw = io_apic_read(ioapic, 0);
3872 spin_unlock_irqrestore(&ioapic_lock, flags);
3874 if (apic_id >= get_physical_broadcast()) {
3875 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3876 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3877 apic_id = reg_00.bits.ID;
3881 * Every APIC in a system must have a unique ID or we get lots of nice
3882 * 'stuck on smp_invalidate_needed IPI wait' messages.
3884 if (apic->check_apicid_used(apic_id_map, apic_id)) {
3886 for (i = 0; i < get_physical_broadcast(); i++) {
3887 if (!apic->check_apicid_used(apic_id_map, i))
3891 if (i == get_physical_broadcast())
3892 panic("Max apic_id exceeded!\n");
3894 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3895 "trying %d\n", ioapic, apic_id, i);
3900 tmp = apic->apicid_to_cpu_present(apic_id);
3901 physids_or(apic_id_map, apic_id_map, tmp);
3903 if (reg_00.bits.ID != apic_id) {
3904 reg_00.bits.ID = apic_id;
3906 spin_lock_irqsave(&ioapic_lock, flags);
3907 io_apic_write(ioapic, 0, reg_00.raw);
3908 reg_00.raw = io_apic_read(ioapic, 0);
3909 spin_unlock_irqrestore(&ioapic_lock, flags);
3912 if (reg_00.bits.ID != apic_id) {
3913 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3918 apic_printk(APIC_VERBOSE, KERN_INFO
3919 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3925 int __init io_apic_get_version(int ioapic)
3927 union IO_APIC_reg_01 reg_01;
3928 unsigned long flags;
3930 spin_lock_irqsave(&ioapic_lock, flags);
3931 reg_01.raw = io_apic_read(ioapic, 1);
3932 spin_unlock_irqrestore(&ioapic_lock, flags);
3934 return reg_01.bits.version;
3937 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3941 if (skip_ioapic_setup)
3944 for (i = 0; i < mp_irq_entries; i++)
3945 if (mp_irqs[i].irqtype == mp_INT &&
3946 mp_irqs[i].srcbusirq == bus_irq)
3948 if (i >= mp_irq_entries)
3951 *trigger = irq_trigger(i);
3952 *polarity = irq_polarity(i);
3957 * This function currently is only a helper for the i386 smp boot process where
3958 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3959 * so mask in all cases should simply be apic->target_cpus()
3962 void __init setup_ioapic_dest(void)
3964 int pin, ioapic = 0, irq, irq_entry;
3965 struct irq_desc *desc;
3966 const struct cpumask *mask;
3968 if (skip_ioapic_setup == 1)
3972 if (!acpi_disabled && acpi_ioapic) {
3973 ioapic = mp_find_ioapic(0);
3979 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3980 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3981 if (irq_entry == -1)
3983 irq = pin_2_irq(irq_entry, ioapic, pin);
3985 desc = irq_to_desc(irq);
3988 * Honour affinities which have been set in early boot
3991 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
3992 mask = desc->affinity;
3994 mask = apic->target_cpus();
3996 if (intr_remapping_enabled)
3997 set_ir_ioapic_affinity_irq_desc(desc, mask);
3999 set_ioapic_affinity_irq_desc(desc, mask);
4005 #define IOAPIC_RESOURCE_NAME_SIZE 11
4007 static struct resource *ioapic_resources;
4009 static struct resource * __init ioapic_setup_resources(int nr_ioapics)
4012 struct resource *res;
4016 if (nr_ioapics <= 0)
4019 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4022 mem = alloc_bootmem(n);
4025 mem += sizeof(struct resource) * nr_ioapics;
4027 for (i = 0; i < nr_ioapics; i++) {
4029 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4030 sprintf(mem, "IOAPIC %u", i);
4031 mem += IOAPIC_RESOURCE_NAME_SIZE;
4034 ioapic_resources = res;
4039 void __init ioapic_init_mappings(void)
4041 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
4042 struct resource *ioapic_res;
4045 ioapic_res = ioapic_setup_resources(nr_ioapics);
4046 for (i = 0; i < nr_ioapics; i++) {
4047 if (smp_found_config) {
4048 ioapic_phys = mp_ioapics[i].apicaddr;
4049 #ifdef CONFIG_X86_32
4052 "WARNING: bogus zero IO-APIC "
4053 "address found in MPTABLE, "
4054 "disabling IO/APIC support!\n");
4055 smp_found_config = 0;
4056 skip_ioapic_setup = 1;
4057 goto fake_ioapic_page;
4061 #ifdef CONFIG_X86_32
4064 ioapic_phys = (unsigned long)
4065 alloc_bootmem_pages(PAGE_SIZE);
4066 ioapic_phys = __pa(ioapic_phys);
4068 set_fixmap_nocache(idx, ioapic_phys);
4069 apic_printk(APIC_VERBOSE,
4070 "mapped IOAPIC to %08lx (%08lx)\n",
4071 __fix_to_virt(idx), ioapic_phys);
4074 ioapic_res->start = ioapic_phys;
4075 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4080 void __init ioapic_insert_resources(void)
4083 struct resource *r = ioapic_resources;
4088 "IO APIC resources couldn't be allocated.\n");
4092 for (i = 0; i < nr_ioapics; i++) {
4093 insert_resource(&iomem_resource, r);
4098 int mp_find_ioapic(int gsi)
4102 /* Find the IOAPIC that manages this GSI. */
4103 for (i = 0; i < nr_ioapics; i++) {
4104 if ((gsi >= mp_gsi_routing[i].gsi_base)
4105 && (gsi <= mp_gsi_routing[i].gsi_end))
4109 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
4113 int mp_find_ioapic_pin(int ioapic, int gsi)
4115 if (WARN_ON(ioapic == -1))
4117 if (WARN_ON(gsi > mp_gsi_routing[ioapic].gsi_end))
4120 return gsi - mp_gsi_routing[ioapic].gsi_base;
4123 static int bad_ioapic(unsigned long address)
4125 if (nr_ioapics >= MAX_IO_APICS) {
4126 printk(KERN_WARNING "WARING: Max # of I/O APICs (%d) exceeded "
4127 "(found %d), skipping\n", MAX_IO_APICS, nr_ioapics);
4131 printk(KERN_WARNING "WARNING: Bogus (zero) I/O APIC address"
4132 " found in table, skipping!\n");
4138 void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
4142 if (bad_ioapic(address))
4147 mp_ioapics[idx].type = MP_IOAPIC;
4148 mp_ioapics[idx].flags = MPC_APIC_USABLE;
4149 mp_ioapics[idx].apicaddr = address;
4151 set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
4152 mp_ioapics[idx].apicid = io_apic_unique_id(id);
4153 mp_ioapics[idx].apicver = io_apic_get_version(idx);
4156 * Build basic GSI lookup table to facilitate gsi->io_apic lookups
4157 * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
4159 mp_gsi_routing[idx].gsi_base = gsi_base;
4160 mp_gsi_routing[idx].gsi_end = gsi_base +
4161 io_apic_get_redir_entries(idx);
4163 printk(KERN_INFO "IOAPIC[%d]: apic_id %d, version %d, address 0x%x, "
4164 "GSI %d-%d\n", idx, mp_ioapics[idx].apicid,
4165 mp_ioapics[idx].apicver, mp_ioapics[idx].apicaddr,
4166 mp_gsi_routing[idx].gsi_base, mp_gsi_routing[idx].gsi_end);