2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/pci.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/acpi.h>
31 #include <linux/sysdev.h>
32 #include <linux/msi.h>
33 #include <linux/htirq.h>
34 #include <linux/dmar.h>
36 #include <acpi/acpi_bus.h>
43 #include <asm/proto.h>
44 #include <asm/mach_apic.h>
48 #include <asm/msidef.h>
49 #include <asm/hypertransport.h>
56 unsigned move_cleanup_count;
58 u8 move_in_progress : 1;
61 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
62 struct irq_cfg irq_cfg[NR_IRQS] __read_mostly;
64 static int assign_irq_vector(int irq, cpumask_t mask);
66 #define __apicdebuginit __init
68 int sis_apic_bug; /* not actually supported, dummy for compile */
71 #include <xen/interface/xen.h>
72 #include <xen/interface/physdev.h>
75 #define make_8259A_irq(_irq) (io_apic_irqs &= ~(1UL<<(_irq)))
76 #define disable_8259A_irq(_irq) ((void)0)
77 #define i8259A_irq_pending(_irq) (0)
79 unsigned long io_apic_irqs;
81 #define clear_IO_APIC() ((void)0)
83 static int no_timer_check;
85 static int disable_timer_pin_1 __initdata;
87 int timer_over_8254 __initdata = 1;
89 /* Where if anywhere is the i8259 connect in external int mode */
90 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
93 static DEFINE_SPINLOCK(ioapic_lock);
94 DEFINE_SPINLOCK(vector_lock);
97 * # of IRQ routing registers
99 int nr_ioapic_registers[MAX_IO_APICS];
102 * Rough estimation of how many shared IRQs there are, can
103 * be changed anytime.
105 #define MAX_PLUS_SHARED_IRQS NR_IRQS
106 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
109 * This is performance-critical, we want to do it O(1)
111 * the indexing order of this array favors 1:1 mappings
112 * between pins and IRQs.
115 static struct irq_pin_list {
116 short apic, pin, next;
117 } irq_2_pin[PIN_MAP_SIZE];
122 unsigned int unused[3];
126 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
128 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
129 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
133 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
136 struct io_apic __iomem *io_apic = io_apic_base(apic);
137 writel(reg, &io_apic->index);
138 return readl(&io_apic->data);
140 struct physdev_apic apic_op;
143 apic_op.apic_physbase = mp_ioapics[apic].mpc_apicaddr;
145 ret = HYPERVISOR_physdev_op(PHYSDEVOP_apic_read, &apic_op);
148 return apic_op.value;
152 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
155 struct io_apic __iomem *io_apic = io_apic_base(apic);
156 writel(reg, &io_apic->index);
157 writel(value, &io_apic->data);
159 struct physdev_apic apic_op;
161 apic_op.apic_physbase = mp_ioapics[apic].mpc_apicaddr;
163 apic_op.value = value;
164 WARN_ON(HYPERVISOR_physdev_op(PHYSDEVOP_apic_write, &apic_op));
169 #define io_apic_modify io_apic_write
172 * Re-write a value: to be used for read-modify-write
173 * cycles where the read already set up the index register.
175 static inline void io_apic_modify(unsigned int apic, unsigned int value)
177 struct io_apic __iomem *io_apic = io_apic_base(apic);
178 writel(value, &io_apic->data);
181 static int io_apic_level_ack_pending(unsigned int irq)
183 struct irq_pin_list *entry;
187 spin_lock_irqsave(&ioapic_lock, flags);
188 entry = irq_2_pin + irq;
196 reg = io_apic_read(entry->apic, 0x10 + pin*2);
197 /* Is the remote IRR bit set? */
198 pending |= (reg >> 14) & 1;
201 entry = irq_2_pin + entry->next;
203 spin_unlock_irqrestore(&ioapic_lock, flags);
209 * Synchronize the IO-APIC and the CPU by doing
210 * a dummy read from the IO-APIC
212 static inline void io_apic_sync(unsigned int apic)
215 struct io_apic __iomem *io_apic = io_apic_base(apic);
216 readl(&io_apic->data);
221 struct { u32 w1, w2; };
222 struct IO_APIC_route_entry entry;
225 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
227 union entry_union eu;
229 spin_lock_irqsave(&ioapic_lock, flags);
230 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
231 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
232 spin_unlock_irqrestore(&ioapic_lock, flags);
237 * When we write a new IO APIC routing entry, we need to write the high
238 * word first! If the mask bit in the low word is clear, we will enable
239 * the interrupt, and we need to make sure the entry is fully populated
240 * before that happens.
243 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
245 union entry_union eu;
247 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
248 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
251 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
254 spin_lock_irqsave(&ioapic_lock, flags);
255 __ioapic_write_entry(apic, pin, e);
256 spin_unlock_irqrestore(&ioapic_lock, flags);
261 * When we mask an IO APIC routing entry, we need to write the low
262 * word first, in order to set the mask bit before we change the
265 static void ioapic_mask_entry(int apic, int pin)
268 union entry_union eu = { .entry.mask = 1 };
270 spin_lock_irqsave(&ioapic_lock, flags);
271 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
272 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
273 spin_unlock_irqrestore(&ioapic_lock, flags);
277 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
280 struct irq_pin_list *entry = irq_2_pin + irq;
282 BUG_ON(irq >= NR_IRQS);
289 io_apic_write(apic, 0x11 + pin*2, dest);
290 reg = io_apic_read(apic, 0x10 + pin*2);
293 io_apic_modify(apic, reg);
296 entry = irq_2_pin + entry->next;
300 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
302 struct irq_cfg *cfg = irq_cfg + irq;
307 cpus_and(tmp, mask, cpu_online_map);
311 if (assign_irq_vector(irq, mask))
314 cpus_and(tmp, cfg->domain, mask);
315 dest = cpu_mask_to_apicid(tmp);
318 * Only the high 8 bits are valid.
320 dest = SET_APIC_LOGICAL_ID(dest);
322 spin_lock_irqsave(&ioapic_lock, flags);
323 __target_IO_APIC_irq(irq, dest, cfg->vector);
324 irq_desc[irq].affinity = mask;
325 spin_unlock_irqrestore(&ioapic_lock, flags);
331 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
332 * shared ISA-space IRQs, so we have to support them. We are super
333 * fast in the common case, and fast for shared ISA-space IRQs.
335 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
337 static int first_free_entry = NR_IRQS;
338 struct irq_pin_list *entry = irq_2_pin + irq;
340 BUG_ON(irq >= NR_IRQS);
342 entry = irq_2_pin + entry->next;
344 if (entry->pin != -1) {
345 entry->next = first_free_entry;
346 entry = irq_2_pin + entry->next;
347 if (++first_free_entry >= PIN_MAP_SIZE)
348 panic("io_apic.c: ran out of irq_2_pin entries!");
355 #define __DO_ACTION(R, ACTION, FINAL) \
359 struct irq_pin_list *entry = irq_2_pin + irq; \
361 BUG_ON(irq >= NR_IRQS); \
367 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
369 io_apic_modify(entry->apic, reg); \
373 entry = irq_2_pin + entry->next; \
377 #define DO_ACTION(name,R,ACTION, FINAL) \
379 static void name##_IO_APIC_irq (unsigned int irq) \
380 __DO_ACTION(R, ACTION, FINAL)
382 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
384 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
387 static void mask_IO_APIC_irq (unsigned int irq)
391 spin_lock_irqsave(&ioapic_lock, flags);
392 __mask_IO_APIC_irq(irq);
393 spin_unlock_irqrestore(&ioapic_lock, flags);
396 static void unmask_IO_APIC_irq (unsigned int irq)
400 spin_lock_irqsave(&ioapic_lock, flags);
401 __unmask_IO_APIC_irq(irq);
402 spin_unlock_irqrestore(&ioapic_lock, flags);
405 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
407 struct IO_APIC_route_entry entry;
409 /* Check delivery_mode to be sure we're not clearing an SMI pin */
410 entry = ioapic_read_entry(apic, pin);
411 if (entry.delivery_mode == dest_SMI)
414 * Disable it in the IO-APIC irq-routing table:
416 ioapic_mask_entry(apic, pin);
419 static void clear_IO_APIC (void)
423 for (apic = 0; apic < nr_ioapics; apic++)
424 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
425 clear_IO_APIC_pin(apic, pin);
428 #endif /* !CONFIG_XEN */
430 int skip_ioapic_setup;
433 static int __init parse_noapic(char *str)
435 disable_ioapic_setup();
438 early_param("noapic", parse_noapic);
441 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
442 static int __init disable_timer_pin_setup(char *arg)
444 disable_timer_pin_1 = 1;
447 __setup("disable_timer_pin_1", disable_timer_pin_setup);
449 static int __init setup_disable_8254_timer(char *s)
451 timer_over_8254 = -1;
454 static int __init setup_enable_8254_timer(char *s)
460 __setup("disable_8254_timer", setup_disable_8254_timer);
461 __setup("enable_8254_timer", setup_enable_8254_timer);
462 #endif /* !CONFIG_XEN */
466 * Find the IRQ entry number of a certain pin.
468 static int find_irq_entry(int apic, int pin, int type)
472 for (i = 0; i < mp_irq_entries; i++)
473 if (mp_irqs[i].mpc_irqtype == type &&
474 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
475 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
476 mp_irqs[i].mpc_dstirq == pin)
484 * Find the pin to which IRQ[irq] (ISA) is connected
486 static int __init find_isa_irq_pin(int irq, int type)
490 for (i = 0; i < mp_irq_entries; i++) {
491 int lbus = mp_irqs[i].mpc_srcbus;
493 if (test_bit(lbus, mp_bus_not_pci) &&
494 (mp_irqs[i].mpc_irqtype == type) &&
495 (mp_irqs[i].mpc_srcbusirq == irq))
497 return mp_irqs[i].mpc_dstirq;
502 static int __init find_isa_irq_apic(int irq, int type)
506 for (i = 0; i < mp_irq_entries; i++) {
507 int lbus = mp_irqs[i].mpc_srcbus;
509 if (test_bit(lbus, mp_bus_not_pci) &&
510 (mp_irqs[i].mpc_irqtype == type) &&
511 (mp_irqs[i].mpc_srcbusirq == irq))
514 if (i < mp_irq_entries) {
516 for(apic = 0; apic < nr_ioapics; apic++) {
517 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
527 * Find a specific PCI IRQ entry.
528 * Not an __init, possibly needed by modules
530 static int pin_2_irq(int idx, int apic, int pin);
532 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
534 int apic, i, best_guess = -1;
536 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
538 if (mp_bus_id_to_pci_bus[bus] == -1) {
539 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
542 for (i = 0; i < mp_irq_entries; i++) {
543 int lbus = mp_irqs[i].mpc_srcbus;
545 for (apic = 0; apic < nr_ioapics; apic++)
546 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
547 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
550 if (!test_bit(lbus, mp_bus_not_pci) &&
551 !mp_irqs[i].mpc_irqtype &&
553 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
554 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
556 if (!(apic || IO_APIC_IRQ(irq)))
559 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
562 * Use the first all-but-pin matching entry as a
563 * best-guess fuzzy result for broken mptables.
569 BUG_ON(best_guess >= NR_IRQS);
573 /* ISA interrupts are always polarity zero edge triggered,
574 * when listed as conforming in the MP table. */
576 #define default_ISA_trigger(idx) (0)
577 #define default_ISA_polarity(idx) (0)
579 /* PCI interrupts are always polarity one level triggered,
580 * when listed as conforming in the MP table. */
582 #define default_PCI_trigger(idx) (1)
583 #define default_PCI_polarity(idx) (1)
585 static int MPBIOS_polarity(int idx)
587 int bus = mp_irqs[idx].mpc_srcbus;
591 * Determine IRQ line polarity (high active or low active):
593 switch (mp_irqs[idx].mpc_irqflag & 3)
595 case 0: /* conforms, ie. bus-type dependent polarity */
596 if (test_bit(bus, mp_bus_not_pci))
597 polarity = default_ISA_polarity(idx);
599 polarity = default_PCI_polarity(idx);
601 case 1: /* high active */
606 case 2: /* reserved */
608 printk(KERN_WARNING "broken BIOS!!\n");
612 case 3: /* low active */
617 default: /* invalid */
619 printk(KERN_WARNING "broken BIOS!!\n");
627 static int MPBIOS_trigger(int idx)
629 int bus = mp_irqs[idx].mpc_srcbus;
633 * Determine IRQ trigger mode (edge or level sensitive):
635 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
637 case 0: /* conforms, ie. bus-type dependent */
638 if (test_bit(bus, mp_bus_not_pci))
639 trigger = default_ISA_trigger(idx);
641 trigger = default_PCI_trigger(idx);
648 case 2: /* reserved */
650 printk(KERN_WARNING "broken BIOS!!\n");
659 default: /* invalid */
661 printk(KERN_WARNING "broken BIOS!!\n");
669 static inline int irq_polarity(int idx)
671 return MPBIOS_polarity(idx);
674 static inline int irq_trigger(int idx)
676 return MPBIOS_trigger(idx);
679 static int pin_2_irq(int idx, int apic, int pin)
682 int bus = mp_irqs[idx].mpc_srcbus;
685 * Debugging check, we are in big trouble if this message pops up!
687 if (mp_irqs[idx].mpc_dstirq != pin)
688 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
690 if (test_bit(bus, mp_bus_not_pci)) {
691 irq = mp_irqs[idx].mpc_srcbusirq;
694 * PCI IRQs are mapped in order
698 irq += nr_ioapic_registers[i++];
701 BUG_ON(irq >= NR_IRQS);
705 static int __assign_irq_vector(int irq, cpumask_t mask)
707 struct physdev_irq irq_op;
710 BUG_ON((unsigned)irq >= NR_IRQS);
713 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
720 if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op))
723 cfg->vector = irq_op.vector;
728 static int assign_irq_vector(int irq, cpumask_t mask)
733 spin_lock_irqsave(&vector_lock, flags);
734 err = __assign_irq_vector(irq, mask);
735 spin_unlock_irqrestore(&vector_lock, flags);
740 static void __clear_irq_vector(int irq)
746 BUG_ON((unsigned)irq >= NR_IRQS);
748 BUG_ON(!cfg->vector);
750 vector = cfg->vector;
751 cpus_and(mask, cfg->domain, cpu_online_map);
752 for_each_cpu_mask(cpu, mask)
753 per_cpu(vector_irq, cpu)[vector] = -1;
756 cfg->domain = CPU_MASK_NONE;
759 void __setup_vector_irq(int cpu)
761 /* Initialize vector_irq on a new cpu */
762 /* This function must be called with vector_lock held */
765 /* Mark the inuse vectors */
766 for (irq = 0; irq < NR_IRQS; ++irq) {
767 if (!cpu_isset(cpu, irq_cfg[irq].domain))
769 vector = irq_cfg[irq].vector;
770 per_cpu(vector_irq, cpu)[vector] = irq;
772 /* Mark the free vectors */
773 for (vector = 0; vector < NR_VECTORS; ++vector) {
774 irq = per_cpu(vector_irq, cpu)[vector];
777 if (!cpu_isset(cpu, irq_cfg[irq].domain))
778 per_cpu(vector_irq, cpu)[vector] = -1;
782 static struct irq_chip ioapic_chip;
784 static void ioapic_register_intr(int irq, unsigned long trigger)
787 irq_desc[irq].status |= IRQ_LEVEL;
788 set_irq_chip_and_handler_name(irq, &ioapic_chip,
789 handle_fasteoi_irq, "fasteoi");
791 irq_desc[irq].status &= ~IRQ_LEVEL;
792 set_irq_chip_and_handler_name(irq, &ioapic_chip,
793 handle_edge_irq, "edge");
797 #define ioapic_register_intr(irq,trigger) ((void)0)
798 #endif /* !CONFIG_XEN */
800 static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
801 int trigger, int polarity)
803 struct irq_cfg *cfg = irq_cfg + irq;
804 struct IO_APIC_route_entry entry;
807 if (!IO_APIC_IRQ(irq))
811 if (assign_irq_vector(irq, mask))
815 cpus_and(mask, cfg->domain, mask);
818 apic_printk(APIC_VERBOSE,KERN_DEBUG
819 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
820 "IRQ %d Mode:%i Active:%i)\n",
821 apic, mp_ioapics[apic].mpc_apicid, pin, cfg->vector,
822 irq, trigger, polarity);
825 * add it to the IO-APIC irq-routing table:
827 memset(&entry,0,sizeof(entry));
829 entry.delivery_mode = INT_DELIVERY_MODE;
830 entry.dest_mode = INT_DEST_MODE;
831 entry.dest = cpu_mask_to_apicid(mask);
832 entry.mask = 0; /* enable IRQ */
833 entry.trigger = trigger;
834 entry.polarity = polarity;
835 entry.vector = cfg->vector;
837 /* Mask level triggered irqs.
838 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
843 ioapic_register_intr(irq, trigger);
845 disable_8259A_irq(irq);
847 ioapic_write_entry(apic, pin, entry);
850 static void __init setup_IO_APIC_irqs(void)
852 int apic, pin, idx, irq, first_notcon = 1;
854 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
856 for (apic = 0; apic < nr_ioapics; apic++) {
857 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
859 idx = find_irq_entry(apic,pin,mp_INT);
862 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
865 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
869 apic_printk(APIC_VERBOSE, " not connected.\n");
873 irq = pin_2_irq(idx, apic, pin);
874 add_pin_to_irq(irq, apic, pin);
876 setup_IO_APIC_irq(apic, pin, irq,
877 irq_trigger(idx), irq_polarity(idx));
882 apic_printk(APIC_VERBOSE, " not connected.\n");
887 * Set up the 8259A-master output pin as broadcast to all
890 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
892 struct IO_APIC_route_entry entry;
895 memset(&entry,0,sizeof(entry));
897 disable_8259A_irq(0);
900 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
903 * We use logical delivery to get the timer IRQ
906 entry.dest_mode = INT_DEST_MODE;
907 entry.mask = 0; /* unmask IRQ now */
908 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
909 entry.delivery_mode = INT_DELIVERY_MODE;
912 entry.vector = vector;
915 * The timer IRQ doesn't have to know that behind the
916 * scene we have a 8259A-master in AEOI mode ...
918 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
921 * Add it to the IO-APIC irq-routing table:
923 spin_lock_irqsave(&ioapic_lock, flags);
924 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
925 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
926 spin_unlock_irqrestore(&ioapic_lock, flags);
931 void __apicdebuginit print_IO_APIC(void)
934 union IO_APIC_reg_00 reg_00;
935 union IO_APIC_reg_01 reg_01;
936 union IO_APIC_reg_02 reg_02;
939 if (apic_verbosity == APIC_QUIET)
942 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
943 for (i = 0; i < nr_ioapics; i++)
944 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
945 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
948 * We are a bit conservative about what we expect. We have to
949 * know about every hardware change ASAP.
951 printk(KERN_INFO "testing the IO APIC.......................\n");
953 for (apic = 0; apic < nr_ioapics; apic++) {
955 spin_lock_irqsave(&ioapic_lock, flags);
956 reg_00.raw = io_apic_read(apic, 0);
957 reg_01.raw = io_apic_read(apic, 1);
958 if (reg_01.bits.version >= 0x10)
959 reg_02.raw = io_apic_read(apic, 2);
960 spin_unlock_irqrestore(&ioapic_lock, flags);
963 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
964 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
965 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
967 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
968 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
970 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
971 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
973 if (reg_01.bits.version >= 0x10) {
974 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
975 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
978 printk(KERN_DEBUG ".... IRQ redirection table:\n");
980 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
981 " Stat Dmod Deli Vect: \n");
983 for (i = 0; i <= reg_01.bits.entries; i++) {
984 struct IO_APIC_route_entry entry;
986 entry = ioapic_read_entry(apic, i);
988 printk(KERN_DEBUG " %02x %03X ",
993 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
998 entry.delivery_status,
1000 entry.delivery_mode,
1005 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1006 for (i = 0; i < NR_IRQS; i++) {
1007 struct irq_pin_list *entry = irq_2_pin + i;
1010 printk(KERN_DEBUG "IRQ%d ", i);
1012 printk("-> %d:%d", entry->apic, entry->pin);
1015 entry = irq_2_pin + entry->next;
1020 printk(KERN_INFO ".................................... done.\n");
1025 static __apicdebuginit void print_APIC_bitfield (int base)
1030 if (apic_verbosity == APIC_QUIET)
1033 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1034 for (i = 0; i < 8; i++) {
1035 v = apic_read(base + i*0x10);
1036 for (j = 0; j < 32; j++) {
1046 void __apicdebuginit print_local_APIC(void * dummy)
1048 unsigned int v, ver, maxlvt;
1050 if (apic_verbosity == APIC_QUIET)
1053 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1054 smp_processor_id(), hard_smp_processor_id());
1055 v = apic_read(APIC_ID);
1056 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1057 v = apic_read(APIC_LVR);
1058 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1059 ver = GET_APIC_VERSION(v);
1060 maxlvt = get_maxlvt();
1062 v = apic_read(APIC_TASKPRI);
1063 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1065 v = apic_read(APIC_ARBPRI);
1066 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1067 v & APIC_ARBPRI_MASK);
1068 v = apic_read(APIC_PROCPRI);
1069 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1071 v = apic_read(APIC_EOI);
1072 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1073 v = apic_read(APIC_RRR);
1074 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1075 v = apic_read(APIC_LDR);
1076 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1077 v = apic_read(APIC_DFR);
1078 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1079 v = apic_read(APIC_SPIV);
1080 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1082 printk(KERN_DEBUG "... APIC ISR field:\n");
1083 print_APIC_bitfield(APIC_ISR);
1084 printk(KERN_DEBUG "... APIC TMR field:\n");
1085 print_APIC_bitfield(APIC_TMR);
1086 printk(KERN_DEBUG "... APIC IRR field:\n");
1087 print_APIC_bitfield(APIC_IRR);
1089 v = apic_read(APIC_ESR);
1090 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1092 v = apic_read(APIC_ICR);
1093 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1094 v = apic_read(APIC_ICR2);
1095 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1097 v = apic_read(APIC_LVTT);
1098 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1100 if (maxlvt > 3) { /* PC is LVT#4. */
1101 v = apic_read(APIC_LVTPC);
1102 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1104 v = apic_read(APIC_LVT0);
1105 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1106 v = apic_read(APIC_LVT1);
1107 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1109 if (maxlvt > 2) { /* ERR is LVT#3. */
1110 v = apic_read(APIC_LVTERR);
1111 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1114 v = apic_read(APIC_TMICT);
1115 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1116 v = apic_read(APIC_TMCCT);
1117 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1118 v = apic_read(APIC_TDCR);
1119 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1123 void print_all_local_APICs (void)
1125 on_each_cpu(print_local_APIC, NULL, 1, 1);
1128 void __apicdebuginit print_PIC(void)
1131 unsigned long flags;
1133 if (apic_verbosity == APIC_QUIET)
1136 printk(KERN_DEBUG "\nprinting PIC contents\n");
1138 spin_lock_irqsave(&i8259A_lock, flags);
1140 v = inb(0xa1) << 8 | inb(0x21);
1141 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1143 v = inb(0xa0) << 8 | inb(0x20);
1144 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1148 v = inb(0xa0) << 8 | inb(0x20);
1152 spin_unlock_irqrestore(&i8259A_lock, flags);
1154 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1156 v = inb(0x4d1) << 8 | inb(0x4d0);
1157 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1159 #endif /* !CONFIG_XEN */
1161 static void __init enable_IO_APIC(void)
1163 union IO_APIC_reg_01 reg_01;
1165 int i8259_apic, i8259_pin;
1168 unsigned long flags;
1170 for (i = 0; i < PIN_MAP_SIZE; i++) {
1171 irq_2_pin[i].pin = -1;
1172 irq_2_pin[i].next = 0;
1176 * The number of IO-APIC IRQ registers (== #pins):
1178 for (apic = 0; apic < nr_ioapics; apic++) {
1179 spin_lock_irqsave(&ioapic_lock, flags);
1180 reg_01.raw = io_apic_read(apic, 1);
1181 spin_unlock_irqrestore(&ioapic_lock, flags);
1182 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1185 for(apic = 0; apic < nr_ioapics; apic++) {
1187 /* See if any of the pins is in ExtINT mode */
1188 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1189 struct IO_APIC_route_entry entry;
1190 entry = ioapic_read_entry(apic, pin);
1192 /* If the interrupt line is enabled and in ExtInt mode
1193 * I have found the pin where the i8259 is connected.
1195 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1196 ioapic_i8259.apic = apic;
1197 ioapic_i8259.pin = pin;
1203 /* Look to see what if the MP table has reported the ExtINT */
1204 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1205 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1206 /* Trust the MP table if nothing is setup in the hardware */
1207 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1208 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1209 ioapic_i8259.pin = i8259_pin;
1210 ioapic_i8259.apic = i8259_apic;
1212 /* Complain if the MP table and the hardware disagree */
1213 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1214 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1216 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1221 * Do not trust the IO-APIC being empty at bootup
1227 * Not an __init, needed by the reboot code
1229 void disable_IO_APIC(void)
1232 * Clear the IO-APIC before rebooting:
1238 * If the i8259 is routed through an IOAPIC
1239 * Put that IOAPIC in virtual wire mode
1240 * so legacy interrupts can be delivered.
1242 if (ioapic_i8259.pin != -1) {
1243 struct IO_APIC_route_entry entry;
1245 memset(&entry, 0, sizeof(entry));
1246 entry.mask = 0; /* Enabled */
1247 entry.trigger = 0; /* Edge */
1249 entry.polarity = 0; /* High */
1250 entry.delivery_status = 0;
1251 entry.dest_mode = 0; /* Physical */
1252 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1254 entry.dest = GET_APIC_ID(apic_read(APIC_ID));
1257 * Add it to the IO-APIC irq-routing table:
1259 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1262 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1267 * There is a nasty bug in some older SMP boards, their mptable lies
1268 * about the timer IRQ. We do the following to work around the situation:
1270 * - timer IRQ defaults to IO-APIC IRQ
1271 * - if this function detects that timer IRQs are defunct, then we fall
1272 * back to ISA timer IRQs
1275 static int __init timer_irq_works(void)
1277 unsigned long t1 = jiffies;
1278 unsigned long flags;
1280 local_save_flags(flags);
1282 /* Let ten ticks pass... */
1283 mdelay((10 * 1000) / HZ);
1284 local_irq_restore(flags);
1287 * Expect a few ticks at least, to be sure some possible
1288 * glue logic does not lock up after one or two first
1289 * ticks in a non-ExtINT mode. Also the local APIC
1290 * might have cached one ExtINT interrupt. Finally, at
1291 * least one tick may be lost due to delays.
1295 if (jiffies - t1 > 4)
1301 * In the SMP+IOAPIC case it might happen that there are an unspecified
1302 * number of pending IRQ events unhandled. These cases are very rare,
1303 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1304 * better to do it this way as thus we do not have to be aware of
1305 * 'pending' interrupts in the IRQ path, except at this point.
1308 * Edge triggered needs to resend any interrupt
1309 * that was delayed but this is now handled in the device
1314 * Starting up a edge-triggered IO-APIC interrupt is
1315 * nasty - we need to make sure that we get the edge.
1316 * If it is already asserted for some reason, we need
1317 * return 1 to indicate that is was pending.
1319 * This is not complete - we should be able to fake
1320 * an edge even if it isn't on the 8259A...
1323 static unsigned int startup_ioapic_irq(unsigned int irq)
1325 int was_pending = 0;
1326 unsigned long flags;
1328 spin_lock_irqsave(&ioapic_lock, flags);
1330 disable_8259A_irq(irq);
1331 if (i8259A_irq_pending(irq))
1334 __unmask_IO_APIC_irq(irq);
1335 spin_unlock_irqrestore(&ioapic_lock, flags);
1340 static int ioapic_retrigger_irq(unsigned int irq)
1342 struct irq_cfg *cfg = &irq_cfg[irq];
1344 unsigned long flags;
1346 spin_lock_irqsave(&vector_lock, flags);
1348 cpu_set(first_cpu(cfg->domain), mask);
1350 send_IPI_mask(mask, cfg->vector);
1351 spin_unlock_irqrestore(&vector_lock, flags);
1357 * Level and edge triggered IO-APIC interrupts need different handling,
1358 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1359 * handled with the level-triggered descriptor, but that one has slightly
1360 * more overhead. Level-triggered interrupts cannot be handled with the
1361 * edge-triggered handler, without risking IRQ storms and other ugly
1366 asmlinkage void smp_irq_move_cleanup_interrupt(void)
1368 unsigned vector, me;
1373 me = smp_processor_id();
1374 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1376 struct irq_desc *desc;
1377 struct irq_cfg *cfg;
1378 irq = __get_cpu_var(vector_irq)[vector];
1382 desc = irq_desc + irq;
1383 cfg = irq_cfg + irq;
1384 spin_lock(&desc->lock);
1385 if (!cfg->move_cleanup_count)
1388 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
1391 __get_cpu_var(vector_irq)[vector] = -1;
1392 cfg->move_cleanup_count--;
1394 spin_unlock(&desc->lock);
1400 static void irq_complete_move(unsigned int irq)
1402 struct irq_cfg *cfg = irq_cfg + irq;
1403 unsigned vector, me;
1405 if (likely(!cfg->move_in_progress))
1408 vector = ~get_irq_regs()->orig_rax;
1409 me = smp_processor_id();
1410 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
1411 cpumask_t cleanup_mask;
1413 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1414 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1415 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1416 cfg->move_in_progress = 0;
1420 static inline void irq_complete_move(unsigned int irq) {}
1423 static void ack_apic_edge(unsigned int irq)
1425 irq_complete_move(irq);
1426 move_native_irq(irq);
1430 static void ack_apic_level(unsigned int irq)
1432 int do_unmask_irq = 0;
1434 irq_complete_move(irq);
1435 #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
1436 /* If we are moving the irq we need to mask it */
1437 if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1439 mask_IO_APIC_irq(irq);
1444 * We must acknowledge the irq before we move it or the acknowledge will
1445 * not propagate properly.
1449 /* Now we can move and renable the irq */
1450 if (unlikely(do_unmask_irq)) {
1451 /* Only migrate the irq if the ack has been received.
1453 * On rare occasions the broadcast level triggered ack gets
1454 * delayed going to ioapics, and if we reprogram the
1455 * vector while Remote IRR is still set the irq will never
1458 * To prevent this scenario we read the Remote IRR bit
1459 * of the ioapic. This has two effects.
1460 * - On any sane system the read of the ioapic will
1461 * flush writes (and acks) going to the ioapic from
1463 * - We get to see if the ACK has actually been delivered.
1465 * Based on failed experiments of reprogramming the
1466 * ioapic entry from outside of irq context starting
1467 * with masking the ioapic entry and then polling until
1468 * Remote IRR was clear before reprogramming the
1469 * ioapic I don't trust the Remote IRR bit to be
1470 * completey accurate.
1472 * However there appears to be no other way to plug
1473 * this race, so if the Remote IRR bit is not
1474 * accurate and is causing problems then it is a hardware bug
1475 * and you can go talk to the chipset vendor about it.
1477 if (!io_apic_level_ack_pending(irq))
1478 move_masked_irq(irq);
1479 unmask_IO_APIC_irq(irq);
1483 static struct irq_chip ioapic_chip __read_mostly = {
1485 .startup = startup_ioapic_irq,
1486 .mask = mask_IO_APIC_irq,
1487 .unmask = unmask_IO_APIC_irq,
1488 .ack = ack_apic_edge,
1489 .eoi = ack_apic_level,
1491 .set_affinity = set_ioapic_affinity_irq,
1493 .retrigger = ioapic_retrigger_irq,
1495 #endif /* !CONFIG_XEN */
1497 static inline void init_IO_APIC_traps(void)
1502 * NOTE! The local APIC isn't very good at handling
1503 * multiple interrupts at the same interrupt level.
1504 * As the interrupt level is determined by taking the
1505 * vector number and shifting that right by 4, we
1506 * want to spread these out a bit so that they don't
1507 * all fall in the same interrupt level.
1509 * Also, we've got to be careful not to trash gate
1510 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1512 for (irq = 0; irq < NR_IRQS ; irq++) {
1514 if (IO_APIC_IRQ(tmp) && !irq_cfg[tmp].vector) {
1516 * Hmm.. We don't have an entry for this,
1517 * so default to an old-fashioned 8259
1518 * interrupt if we can..
1521 make_8259A_irq(irq);
1524 /* Strange. Oh, well.. */
1525 irq_desc[irq].chip = &no_irq_chip;
1532 static void enable_lapic_irq (unsigned int irq)
1536 v = apic_read(APIC_LVT0);
1537 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1540 static void disable_lapic_irq (unsigned int irq)
1544 v = apic_read(APIC_LVT0);
1545 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1548 static void ack_lapic_irq (unsigned int irq)
1553 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1555 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1556 .name = "local-APIC",
1557 .typename = "local-APIC-edge",
1558 .startup = NULL, /* startup_irq() not used for IRQ0 */
1559 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1560 .enable = enable_lapic_irq,
1561 .disable = disable_lapic_irq,
1562 .ack = ack_lapic_irq,
1563 .end = end_lapic_irq,
1566 static void setup_nmi (void)
1569 * Dirty trick to enable the NMI watchdog ...
1570 * We put the 8259A master into AEOI mode and
1571 * unmask on all local APICs LVT0 as NMI.
1573 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1574 * is from Maciej W. Rozycki - so we do not have to EOI from
1575 * the NMI handler or the timer interrupt.
1577 printk(KERN_INFO "activating NMI Watchdog ...");
1579 enable_NMI_through_LVT0(NULL);
1585 * This looks a bit hackish but it's about the only one way of sending
1586 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1587 * not support the ExtINT mode, unfortunately. We need to send these
1588 * cycles as some i82489DX-based boards have glue logic that keeps the
1589 * 8259A interrupt line asserted until INTA. --macro
1591 static inline void unlock_ExtINT_logic(void)
1594 struct IO_APIC_route_entry entry0, entry1;
1595 unsigned char save_control, save_freq_select;
1596 unsigned long flags;
1598 pin = find_isa_irq_pin(8, mp_INT);
1599 apic = find_isa_irq_apic(8, mp_INT);
1603 spin_lock_irqsave(&ioapic_lock, flags);
1604 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1605 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1606 spin_unlock_irqrestore(&ioapic_lock, flags);
1607 clear_IO_APIC_pin(apic, pin);
1609 memset(&entry1, 0, sizeof(entry1));
1611 entry1.dest_mode = 0; /* physical delivery */
1612 entry1.mask = 0; /* unmask IRQ now */
1613 entry1.dest = hard_smp_processor_id();
1614 entry1.delivery_mode = dest_ExtINT;
1615 entry1.polarity = entry0.polarity;
1619 spin_lock_irqsave(&ioapic_lock, flags);
1620 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1621 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1622 spin_unlock_irqrestore(&ioapic_lock, flags);
1624 save_control = CMOS_READ(RTC_CONTROL);
1625 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1626 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1628 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1633 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1637 CMOS_WRITE(save_control, RTC_CONTROL);
1638 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1639 clear_IO_APIC_pin(apic, pin);
1641 spin_lock_irqsave(&ioapic_lock, flags);
1642 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1643 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1644 spin_unlock_irqrestore(&ioapic_lock, flags);
1648 * This code may look a bit paranoid, but it's supposed to cooperate with
1649 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1650 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1651 * fanatically on his truly buggy board.
1653 * FIXME: really need to revamp this for modern platforms only.
1655 static inline void check_timer(void)
1657 struct irq_cfg *cfg = irq_cfg + 0;
1658 int apic1, pin1, apic2, pin2;
1659 unsigned long flags;
1661 local_irq_save(flags);
1664 * get/set the timer IRQ vector:
1666 disable_8259A_irq(0);
1667 assign_irq_vector(0, TARGET_CPUS);
1670 * Subtle, code in do_timer_interrupt() expects an AEOI
1671 * mode for the 8259A whenever interrupts are routed
1672 * through I/O APICs. Also IRQ0 has to be enabled in
1673 * the 8259A which implies the virtual wire has to be
1674 * disabled in the local APIC.
1676 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1678 if (timer_over_8254 > 0)
1679 enable_8259A_irq(0);
1681 pin1 = find_isa_irq_pin(0, mp_INT);
1682 apic1 = find_isa_irq_apic(0, mp_INT);
1683 pin2 = ioapic_i8259.pin;
1684 apic2 = ioapic_i8259.apic;
1686 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1687 cfg->vector, apic1, pin1, apic2, pin2);
1691 * Ok, does IRQ0 through the IOAPIC work?
1693 unmask_IO_APIC_irq(0);
1694 if (!no_timer_check && timer_irq_works()) {
1695 nmi_watchdog_default();
1696 if (nmi_watchdog == NMI_IO_APIC) {
1697 disable_8259A_irq(0);
1699 enable_8259A_irq(0);
1701 if (disable_timer_pin_1 > 0)
1702 clear_IO_APIC_pin(0, pin1);
1705 clear_IO_APIC_pin(apic1, pin1);
1706 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1707 "connected to IO-APIC\n");
1710 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1711 "through the 8259A ... ");
1713 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1716 * legacy devices should be connected to IO APIC #0
1718 setup_ExtINT_IRQ0_pin(apic2, pin2, cfg->vector);
1719 if (timer_irq_works()) {
1720 apic_printk(APIC_VERBOSE," works.\n");
1721 nmi_watchdog_default();
1722 if (nmi_watchdog == NMI_IO_APIC) {
1728 * Cleanup, just in case ...
1730 clear_IO_APIC_pin(apic2, pin2);
1732 apic_printk(APIC_VERBOSE," failed.\n");
1734 if (nmi_watchdog == NMI_IO_APIC) {
1735 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1739 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1741 disable_8259A_irq(0);
1742 irq_desc[0].chip = &lapic_irq_type;
1743 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
1744 enable_8259A_irq(0);
1746 if (timer_irq_works()) {
1747 apic_printk(APIC_VERBOSE," works.\n");
1750 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
1751 apic_printk(APIC_VERBOSE," failed.\n");
1753 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1757 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1759 unlock_ExtINT_logic();
1761 if (timer_irq_works()) {
1762 apic_printk(APIC_VERBOSE," works.\n");
1765 apic_printk(APIC_VERBOSE," failed :(.\n");
1766 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1768 local_irq_restore(flags);
1771 static int __init notimercheck(char *s)
1776 __setup("no_timer_check", notimercheck);
1778 #define check_timer() ((void)0)
1779 int timer_uses_ioapic_pin_0 = 0;
1780 #endif /* !CONFIG_XEN */
1784 * IRQs that are handled by the PIC in the MPS IOAPIC case.
1785 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1786 * Linux doesn't really care, as it's not actually used
1787 * for any interrupt handling anyway.
1789 #define PIC_IRQS (1<<2)
1791 void __init setup_IO_APIC(void)
1796 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1798 io_apic_irqs = ~PIC_IRQS;
1800 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1804 #endif /* !CONFIG_XEN */
1805 setup_IO_APIC_irqs();
1806 init_IO_APIC_traps();
1812 struct sysfs_ioapic_data {
1813 struct sys_device dev;
1814 struct IO_APIC_route_entry entry[0];
1816 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1818 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1820 struct IO_APIC_route_entry *entry;
1821 struct sysfs_ioapic_data *data;
1824 data = container_of(dev, struct sysfs_ioapic_data, dev);
1825 entry = data->entry;
1826 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
1827 *entry = ioapic_read_entry(dev->id, i);
1832 static int ioapic_resume(struct sys_device *dev)
1834 struct IO_APIC_route_entry *entry;
1835 struct sysfs_ioapic_data *data;
1836 unsigned long flags;
1837 union IO_APIC_reg_00 reg_00;
1840 data = container_of(dev, struct sysfs_ioapic_data, dev);
1841 entry = data->entry;
1843 spin_lock_irqsave(&ioapic_lock, flags);
1844 reg_00.raw = io_apic_read(dev->id, 0);
1845 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1846 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1847 io_apic_write(dev->id, 0, reg_00.raw);
1849 spin_unlock_irqrestore(&ioapic_lock, flags);
1850 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
1851 ioapic_write_entry(dev->id, i, entry[i]);
1856 static struct sysdev_class ioapic_sysdev_class = {
1857 set_kset_name("ioapic"),
1858 .suspend = ioapic_suspend,
1859 .resume = ioapic_resume,
1862 static int __init ioapic_init_sysfs(void)
1864 struct sys_device * dev;
1867 error = sysdev_class_register(&ioapic_sysdev_class);
1871 for (i = 0; i < nr_ioapics; i++ ) {
1872 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1873 * sizeof(struct IO_APIC_route_entry);
1874 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
1875 if (!mp_ioapic_data[i]) {
1876 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1879 dev = &mp_ioapic_data[i]->dev;
1881 dev->cls = &ioapic_sysdev_class;
1882 error = sysdev_register(dev);
1884 kfree(mp_ioapic_data[i]);
1885 mp_ioapic_data[i] = NULL;
1886 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1894 device_initcall(ioapic_init_sysfs);
1898 * Dynamic irq allocate and deallocation
1900 int create_irq(void)
1902 /* Allocate an unused irq */
1905 unsigned long flags;
1908 spin_lock_irqsave(&vector_lock, flags);
1909 for (new = (NR_IRQS - 1); new >= 0; new--) {
1910 if (platform_legacy_irq(new))
1912 if (irq_cfg[new].vector != 0)
1914 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
1918 spin_unlock_irqrestore(&vector_lock, flags);
1921 dynamic_irq_init(irq);
1926 void destroy_irq(unsigned int irq)
1928 unsigned long flags;
1930 dynamic_irq_cleanup(irq);
1932 spin_lock_irqsave(&vector_lock, flags);
1933 __clear_irq_vector(irq);
1934 spin_unlock_irqrestore(&vector_lock, flags);
1939 * MSI message composition
1941 #ifdef CONFIG_PCI_MSI
1942 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
1944 struct irq_cfg *cfg = irq_cfg + irq;
1950 err = assign_irq_vector(irq, tmp);
1952 cpus_and(tmp, cfg->domain, tmp);
1953 dest = cpu_mask_to_apicid(tmp);
1955 msg->address_hi = MSI_ADDR_BASE_HI;
1958 ((INT_DEST_MODE == 0) ?
1959 MSI_ADDR_DEST_MODE_PHYSICAL:
1960 MSI_ADDR_DEST_MODE_LOGICAL) |
1961 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1962 MSI_ADDR_REDIRECTION_CPU:
1963 MSI_ADDR_REDIRECTION_LOWPRI) |
1964 MSI_ADDR_DEST_ID(dest);
1967 MSI_DATA_TRIGGER_EDGE |
1968 MSI_DATA_LEVEL_ASSERT |
1969 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1970 MSI_DATA_DELIVERY_FIXED:
1971 MSI_DATA_DELIVERY_LOWPRI) |
1972 MSI_DATA_VECTOR(cfg->vector);
1978 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
1980 struct irq_cfg *cfg = irq_cfg + irq;
1985 cpus_and(tmp, mask, cpu_online_map);
1986 if (cpus_empty(tmp))
1989 if (assign_irq_vector(irq, mask))
1992 cpus_and(tmp, cfg->domain, mask);
1993 dest = cpu_mask_to_apicid(tmp);
1995 read_msi_msg(irq, &msg);
1997 msg.data &= ~MSI_DATA_VECTOR_MASK;
1998 msg.data |= MSI_DATA_VECTOR(cfg->vector);
1999 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2000 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2002 write_msi_msg(irq, &msg);
2003 irq_desc[irq].affinity = mask;
2005 #endif /* CONFIG_SMP */
2008 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2009 * which implement the MSI or MSI-X Capability Structure.
2011 static struct irq_chip msi_chip = {
2013 .unmask = unmask_msi_irq,
2014 .mask = mask_msi_irq,
2015 .ack = ack_apic_edge,
2017 .set_affinity = set_msi_irq_affinity,
2019 .retrigger = ioapic_retrigger_irq,
2022 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
2030 ret = msi_compose_msg(dev, irq, &msg);
2036 set_irq_msi(irq, desc);
2037 write_msi_msg(irq, &msg);
2039 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
2044 void arch_teardown_msi_irq(unsigned int irq)
2051 static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
2053 struct irq_cfg *cfg = irq_cfg + irq;
2058 cpus_and(tmp, mask, cpu_online_map);
2059 if (cpus_empty(tmp))
2062 if (assign_irq_vector(irq, mask))
2065 cpus_and(tmp, cfg->domain, mask);
2066 dest = cpu_mask_to_apicid(tmp);
2068 dmar_msi_read(irq, &msg);
2070 msg.data &= ~MSI_DATA_VECTOR_MASK;
2071 msg.data |= MSI_DATA_VECTOR(cfg->vector);
2072 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2073 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2075 dmar_msi_write(irq, &msg);
2076 irq_desc[irq].affinity = mask;
2078 #endif /* CONFIG_SMP */
2080 struct irq_chip dmar_msi_type = {
2082 .unmask = dmar_msi_unmask,
2083 .mask = dmar_msi_mask,
2084 .ack = ack_apic_edge,
2086 .set_affinity = dmar_msi_set_affinity,
2088 .retrigger = ioapic_retrigger_irq,
2091 int arch_setup_dmar_msi(unsigned int irq)
2096 ret = msi_compose_msg(NULL, irq, &msg);
2099 dmar_msi_write(irq, &msg);
2100 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
2106 #endif /* CONFIG_PCI_MSI */
2108 * Hypertransport interrupt support
2110 #ifdef CONFIG_HT_IRQ
2114 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2116 struct ht_irq_msg msg;
2117 fetch_ht_irq_msg(irq, &msg);
2119 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2120 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2122 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2123 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2125 write_ht_irq_msg(irq, &msg);
2128 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2130 struct irq_cfg *cfg = irq_cfg + irq;
2134 cpus_and(tmp, mask, cpu_online_map);
2135 if (cpus_empty(tmp))
2138 if (assign_irq_vector(irq, mask))
2141 cpus_and(tmp, cfg->domain, mask);
2142 dest = cpu_mask_to_apicid(tmp);
2144 target_ht_irq(irq, dest, cfg->vector);
2145 irq_desc[irq].affinity = mask;
2149 static struct irq_chip ht_irq_chip = {
2151 .mask = mask_ht_irq,
2152 .unmask = unmask_ht_irq,
2153 .ack = ack_apic_edge,
2155 .set_affinity = set_ht_irq_affinity,
2157 .retrigger = ioapic_retrigger_irq,
2160 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2162 struct irq_cfg *cfg = irq_cfg + irq;
2167 err = assign_irq_vector(irq, tmp);
2169 struct ht_irq_msg msg;
2172 cpus_and(tmp, cfg->domain, tmp);
2173 dest = cpu_mask_to_apicid(tmp);
2175 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2179 HT_IRQ_LOW_DEST_ID(dest) |
2180 HT_IRQ_LOW_VECTOR(cfg->vector) |
2181 ((INT_DEST_MODE == 0) ?
2182 HT_IRQ_LOW_DM_PHYSICAL :
2183 HT_IRQ_LOW_DM_LOGICAL) |
2184 HT_IRQ_LOW_RQEOI_EDGE |
2185 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2186 HT_IRQ_LOW_MT_FIXED :
2187 HT_IRQ_LOW_MT_ARBITRATED) |
2188 HT_IRQ_LOW_IRQ_MASKED;
2190 write_ht_irq_msg(irq, &msg);
2192 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2193 handle_edge_irq, "edge");
2197 #endif /* CONFIG_HT_IRQ */
2199 /* --------------------------------------------------------------------------
2200 ACPI-based IOAPIC Configuration
2201 -------------------------------------------------------------------------- */
2205 #define IO_APIC_MAX_ID 0xFE
2207 int __init io_apic_get_redir_entries (int ioapic)
2209 union IO_APIC_reg_01 reg_01;
2210 unsigned long flags;
2212 spin_lock_irqsave(&ioapic_lock, flags);
2213 reg_01.raw = io_apic_read(ioapic, 1);
2214 spin_unlock_irqrestore(&ioapic_lock, flags);
2216 return reg_01.bits.entries;
2220 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2222 if (!IO_APIC_IRQ(irq)) {
2223 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2229 * IRQs < 16 are already in the irq_2_pin[] map
2232 add_pin_to_irq(irq, ioapic, pin);
2234 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
2240 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2244 if (skip_ioapic_setup)
2247 for (i = 0; i < mp_irq_entries; i++)
2248 if (mp_irqs[i].mpc_irqtype == mp_INT &&
2249 mp_irqs[i].mpc_srcbusirq == bus_irq)
2251 if (i >= mp_irq_entries)
2254 *trigger = irq_trigger(i);
2255 *polarity = irq_polarity(i);
2259 #endif /* CONFIG_ACPI */
2263 * This function currently is only a helper for the i386 smp boot process where
2264 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2265 * so mask in all cases should simply be TARGET_CPUS
2268 void __init setup_ioapic_dest(void)
2270 int pin, ioapic, irq, irq_entry;
2272 if (skip_ioapic_setup == 1)
2275 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2276 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2277 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2278 if (irq_entry == -1)
2280 irq = pin_2_irq(irq_entry, ioapic, pin);
2282 /* setup_IO_APIC_irqs could fail to get vector for some device
2283 * when you have too many devices, because at that time only boot
2286 if (!irq_cfg[irq].vector)
2287 setup_IO_APIC_irq(ioapic, pin, irq,
2288 irq_trigger(irq_entry),
2289 irq_polarity(irq_entry));
2291 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2297 #endif /* !CONFIG_XEN */