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>
35 #include <linux/jiffies.h>
37 #include <acpi/acpi_bus.h>
39 #include <linux/bootmem.h>
40 #include <linux/dmar.h>
46 #include <asm/proto.h>
49 #include <asm/i8259.h>
51 #include <asm/msidef.h>
52 #include <asm/hypertransport.h>
53 #include <asm/irq_remapping.h>
56 #include <mach_apic.h>
61 unsigned move_cleanup_count;
63 u8 move_in_progress : 1;
66 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
67 static struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
68 [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
69 [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
70 [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
71 [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
72 [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
73 [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
74 [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
75 [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
76 [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
77 [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
78 [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
79 [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
80 [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
81 [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
82 [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
83 [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
86 static int assign_irq_vector(int irq, cpumask_t mask);
88 int first_system_vector = 0xfe;
90 char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
92 #define __apicdebuginit __init
94 int sis_apic_bug; /* not actually supported, dummy for compile */
96 static int no_timer_check;
98 static int disable_timer_pin_1 __initdata;
100 int timer_through_8259 __initdata;
102 /* Where if anywhere is the i8259 connect in external int mode */
103 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
105 static DEFINE_SPINLOCK(ioapic_lock);
106 DEFINE_SPINLOCK(vector_lock);
109 * # of IRQ routing registers
111 int nr_ioapic_registers[MAX_IO_APICS];
113 /* I/O APIC RTE contents at the OS boot up */
114 struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
116 /* I/O APIC entries */
117 struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
120 /* MP IRQ source entries */
121 struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
123 /* # of MP IRQ source entries */
126 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
129 * Rough estimation of how many shared IRQs there are, can
130 * be changed anytime.
132 #define MAX_PLUS_SHARED_IRQS NR_IRQS
133 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
136 * This is performance-critical, we want to do it O(1)
138 * the indexing order of this array favors 1:1 mappings
139 * between pins and IRQs.
142 static struct irq_pin_list {
143 short apic, pin, next;
144 } irq_2_pin[PIN_MAP_SIZE];
148 unsigned int unused[3];
152 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
154 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
155 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
158 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
160 struct io_apic __iomem *io_apic = io_apic_base(apic);
161 writel(reg, &io_apic->index);
162 return readl(&io_apic->data);
165 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
167 struct io_apic __iomem *io_apic = io_apic_base(apic);
168 writel(reg, &io_apic->index);
169 writel(value, &io_apic->data);
173 * Re-write a value: to be used for read-modify-write
174 * cycles where the read already set up the index register.
176 static inline void io_apic_modify(unsigned int apic, unsigned int value)
178 struct io_apic __iomem *io_apic = io_apic_base(apic);
179 writel(value, &io_apic->data);
182 static bool io_apic_level_ack_pending(unsigned int irq)
184 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 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
199 spin_unlock_irqrestore(&ioapic_lock, flags);
204 entry = irq_2_pin + entry->next;
206 spin_unlock_irqrestore(&ioapic_lock, flags);
212 * Synchronize the IO-APIC and the CPU by doing
213 * a dummy read from the IO-APIC
215 static inline void io_apic_sync(unsigned int apic)
217 struct io_apic __iomem *io_apic = io_apic_base(apic);
218 readl(&io_apic->data);
221 #define __DO_ACTION(R, ACTION, FINAL) \
225 struct irq_pin_list *entry = irq_2_pin + irq; \
227 BUG_ON(irq >= NR_IRQS); \
233 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
235 io_apic_modify(entry->apic, reg); \
239 entry = irq_2_pin + entry->next; \
244 struct { u32 w1, w2; };
245 struct IO_APIC_route_entry entry;
248 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
250 union entry_union eu;
252 spin_lock_irqsave(&ioapic_lock, flags);
253 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
254 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
255 spin_unlock_irqrestore(&ioapic_lock, flags);
260 * When we write a new IO APIC routing entry, we need to write the high
261 * word first! If the mask bit in the low word is clear, we will enable
262 * the interrupt, and we need to make sure the entry is fully populated
263 * before that happens.
266 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
268 union entry_union eu;
270 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
271 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
274 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
277 spin_lock_irqsave(&ioapic_lock, flags);
278 __ioapic_write_entry(apic, pin, e);
279 spin_unlock_irqrestore(&ioapic_lock, flags);
283 * When we mask an IO APIC routing entry, we need to write the low
284 * word first, in order to set the mask bit before we change the
287 static void ioapic_mask_entry(int apic, int pin)
290 union entry_union eu = { .entry.mask = 1 };
292 spin_lock_irqsave(&ioapic_lock, flags);
293 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
294 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
295 spin_unlock_irqrestore(&ioapic_lock, flags);
299 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
302 struct irq_pin_list *entry = irq_2_pin + irq;
304 BUG_ON(irq >= NR_IRQS);
312 * With interrupt-remapping, destination information comes
313 * from interrupt-remapping table entry.
315 if (!irq_remapped(irq))
316 io_apic_write(apic, 0x11 + pin*2, dest);
317 reg = io_apic_read(apic, 0x10 + pin*2);
318 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
320 io_apic_modify(apic, reg);
323 entry = irq_2_pin + entry->next;
327 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
329 struct irq_cfg *cfg = irq_cfg + irq;
334 cpus_and(tmp, mask, cpu_online_map);
338 if (assign_irq_vector(irq, mask))
341 cpus_and(tmp, cfg->domain, mask);
342 dest = cpu_mask_to_apicid(tmp);
345 * Only the high 8 bits are valid.
347 dest = SET_APIC_LOGICAL_ID(dest);
349 spin_lock_irqsave(&ioapic_lock, flags);
350 __target_IO_APIC_irq(irq, dest, cfg->vector);
351 irq_desc[irq].affinity = mask;
352 spin_unlock_irqrestore(&ioapic_lock, flags);
357 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
358 * shared ISA-space IRQs, so we have to support them. We are super
359 * fast in the common case, and fast for shared ISA-space IRQs.
361 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
363 static int first_free_entry = NR_IRQS;
364 struct irq_pin_list *entry = irq_2_pin + irq;
366 BUG_ON(irq >= NR_IRQS);
368 entry = irq_2_pin + entry->next;
370 if (entry->pin != -1) {
371 entry->next = first_free_entry;
372 entry = irq_2_pin + entry->next;
373 if (++first_free_entry >= PIN_MAP_SIZE)
374 panic("io_apic.c: ran out of irq_2_pin entries!");
381 * Reroute an IRQ to a different pin.
383 static void __init replace_pin_at_irq(unsigned int irq,
384 int oldapic, int oldpin,
385 int newapic, int newpin)
387 struct irq_pin_list *entry = irq_2_pin + irq;
390 if (entry->apic == oldapic && entry->pin == oldpin) {
391 entry->apic = newapic;
396 entry = irq_2_pin + entry->next;
401 #define DO_ACTION(name,R,ACTION, FINAL) \
403 static void name##_IO_APIC_irq (unsigned int irq) \
404 __DO_ACTION(R, ACTION, FINAL)
407 DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, io_apic_sync(entry->apic))
410 DO_ACTION(__unmask, 0, &= ~IO_APIC_REDIR_MASKED, )
412 static void mask_IO_APIC_irq (unsigned int irq)
416 spin_lock_irqsave(&ioapic_lock, flags);
417 __mask_IO_APIC_irq(irq);
418 spin_unlock_irqrestore(&ioapic_lock, flags);
421 static void unmask_IO_APIC_irq (unsigned int irq)
425 spin_lock_irqsave(&ioapic_lock, flags);
426 __unmask_IO_APIC_irq(irq);
427 spin_unlock_irqrestore(&ioapic_lock, flags);
430 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
432 struct IO_APIC_route_entry entry;
434 /* Check delivery_mode to be sure we're not clearing an SMI pin */
435 entry = ioapic_read_entry(apic, pin);
436 if (entry.delivery_mode == dest_SMI)
439 * Disable it in the IO-APIC irq-routing table:
441 ioapic_mask_entry(apic, pin);
444 static void clear_IO_APIC (void)
448 for (apic = 0; apic < nr_ioapics; apic++)
449 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
450 clear_IO_APIC_pin(apic, pin);
454 * Saves and masks all the unmasked IO-APIC RTE's
456 int save_mask_IO_APIC_setup(void)
458 union IO_APIC_reg_01 reg_01;
463 * The number of IO-APIC IRQ registers (== #pins):
465 for (apic = 0; apic < nr_ioapics; apic++) {
466 spin_lock_irqsave(&ioapic_lock, flags);
467 reg_01.raw = io_apic_read(apic, 1);
468 spin_unlock_irqrestore(&ioapic_lock, flags);
469 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
472 for (apic = 0; apic < nr_ioapics; apic++) {
473 early_ioapic_entries[apic] =
474 kzalloc(sizeof(struct IO_APIC_route_entry) *
475 nr_ioapic_registers[apic], GFP_KERNEL);
476 if (!early_ioapic_entries[apic])
480 for (apic = 0; apic < nr_ioapics; apic++)
481 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
482 struct IO_APIC_route_entry entry;
484 entry = early_ioapic_entries[apic][pin] =
485 ioapic_read_entry(apic, pin);
488 ioapic_write_entry(apic, pin, entry);
494 void restore_IO_APIC_setup(void)
498 for (apic = 0; apic < nr_ioapics; apic++)
499 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
500 ioapic_write_entry(apic, pin,
501 early_ioapic_entries[apic][pin]);
504 void reinit_intr_remapped_IO_APIC(int intr_remapping)
507 * for now plain restore of previous settings.
508 * TBD: In the case of OS enabling interrupt-remapping,
509 * IO-APIC RTE's need to be setup to point to interrupt-remapping
510 * table entries. for now, do a plain restore, and wait for
511 * the setup_IO_APIC_irqs() to do proper initialization.
513 restore_IO_APIC_setup();
516 int skip_ioapic_setup;
519 static int __init parse_noapic(char *str)
521 disable_ioapic_setup();
524 early_param("noapic", parse_noapic);
526 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
527 static int __init disable_timer_pin_setup(char *arg)
529 disable_timer_pin_1 = 1;
532 __setup("disable_timer_pin_1", disable_timer_pin_setup);
536 * Find the IRQ entry number of a certain pin.
538 static int find_irq_entry(int apic, int pin, int type)
542 for (i = 0; i < mp_irq_entries; i++)
543 if (mp_irqs[i].mp_irqtype == type &&
544 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
545 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
546 mp_irqs[i].mp_dstirq == pin)
553 * Find the pin to which IRQ[irq] (ISA) is connected
555 static int __init find_isa_irq_pin(int irq, int type)
559 for (i = 0; i < mp_irq_entries; i++) {
560 int lbus = mp_irqs[i].mp_srcbus;
562 if (test_bit(lbus, mp_bus_not_pci) &&
563 (mp_irqs[i].mp_irqtype == type) &&
564 (mp_irqs[i].mp_srcbusirq == irq))
566 return mp_irqs[i].mp_dstirq;
571 static int __init find_isa_irq_apic(int irq, int type)
575 for (i = 0; i < mp_irq_entries; i++) {
576 int lbus = mp_irqs[i].mp_srcbus;
578 if (test_bit(lbus, mp_bus_not_pci) &&
579 (mp_irqs[i].mp_irqtype == type) &&
580 (mp_irqs[i].mp_srcbusirq == irq))
583 if (i < mp_irq_entries) {
585 for(apic = 0; apic < nr_ioapics; apic++) {
586 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
595 * Find a specific PCI IRQ entry.
596 * Not an __init, possibly needed by modules
598 static int pin_2_irq(int idx, int apic, int pin);
600 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
602 int apic, i, best_guess = -1;
604 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
606 if (test_bit(bus, mp_bus_not_pci)) {
607 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
610 for (i = 0; i < mp_irq_entries; i++) {
611 int lbus = mp_irqs[i].mp_srcbus;
613 for (apic = 0; apic < nr_ioapics; apic++)
614 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
615 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
618 if (!test_bit(lbus, mp_bus_not_pci) &&
619 !mp_irqs[i].mp_irqtype &&
621 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
622 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
624 if (!(apic || IO_APIC_IRQ(irq)))
627 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
630 * Use the first all-but-pin matching entry as a
631 * best-guess fuzzy result for broken mptables.
637 BUG_ON(best_guess >= NR_IRQS);
641 /* ISA interrupts are always polarity zero edge triggered,
642 * when listed as conforming in the MP table. */
644 #define default_ISA_trigger(idx) (0)
645 #define default_ISA_polarity(idx) (0)
647 /* PCI interrupts are always polarity one level triggered,
648 * when listed as conforming in the MP table. */
650 #define default_PCI_trigger(idx) (1)
651 #define default_PCI_polarity(idx) (1)
653 static int MPBIOS_polarity(int idx)
655 int bus = mp_irqs[idx].mp_srcbus;
659 * Determine IRQ line polarity (high active or low active):
661 switch (mp_irqs[idx].mp_irqflag & 3)
663 case 0: /* conforms, ie. bus-type dependent polarity */
664 if (test_bit(bus, mp_bus_not_pci))
665 polarity = default_ISA_polarity(idx);
667 polarity = default_PCI_polarity(idx);
669 case 1: /* high active */
674 case 2: /* reserved */
676 printk(KERN_WARNING "broken BIOS!!\n");
680 case 3: /* low active */
685 default: /* invalid */
687 printk(KERN_WARNING "broken BIOS!!\n");
695 static int MPBIOS_trigger(int idx)
697 int bus = mp_irqs[idx].mp_srcbus;
701 * Determine IRQ trigger mode (edge or level sensitive):
703 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
705 case 0: /* conforms, ie. bus-type dependent */
706 if (test_bit(bus, mp_bus_not_pci))
707 trigger = default_ISA_trigger(idx);
709 trigger = default_PCI_trigger(idx);
716 case 2: /* reserved */
718 printk(KERN_WARNING "broken BIOS!!\n");
727 default: /* invalid */
729 printk(KERN_WARNING "broken BIOS!!\n");
737 static inline int irq_polarity(int idx)
739 return MPBIOS_polarity(idx);
742 static inline int irq_trigger(int idx)
744 return MPBIOS_trigger(idx);
747 static int pin_2_irq(int idx, int apic, int pin)
750 int bus = mp_irqs[idx].mp_srcbus;
753 * Debugging check, we are in big trouble if this message pops up!
755 if (mp_irqs[idx].mp_dstirq != pin)
756 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
758 if (test_bit(bus, mp_bus_not_pci)) {
759 irq = mp_irqs[idx].mp_srcbusirq;
762 * PCI IRQs are mapped in order
766 irq += nr_ioapic_registers[i++];
769 BUG_ON(irq >= NR_IRQS);
773 static int __assign_irq_vector(int irq, cpumask_t mask)
776 * NOTE! The local APIC isn't very good at handling
777 * multiple interrupts at the same interrupt level.
778 * As the interrupt level is determined by taking the
779 * vector number and shifting that right by 4, we
780 * want to spread these out a bit so that they don't
781 * all fall in the same interrupt level.
783 * Also, we've got to be careful not to trash gate
784 * 0x80, because int 0x80 is hm, kind of importantish. ;)
786 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
787 unsigned int old_vector;
791 BUG_ON((unsigned)irq >= NR_IRQS);
794 /* Only try and allocate irqs on cpus that are present */
795 cpus_and(mask, mask, cpu_online_map);
797 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
800 old_vector = cfg->vector;
803 cpus_and(tmp, cfg->domain, mask);
804 if (!cpus_empty(tmp))
808 for_each_cpu_mask_nr(cpu, mask) {
809 cpumask_t domain, new_mask;
813 domain = vector_allocation_domain(cpu);
814 cpus_and(new_mask, domain, cpu_online_map);
816 vector = current_vector;
817 offset = current_offset;
820 if (vector >= first_system_vector) {
821 /* If we run out of vectors on large boxen, must share them. */
822 offset = (offset + 1) % 8;
823 vector = FIRST_DEVICE_VECTOR + offset;
825 if (unlikely(current_vector == vector))
827 if (vector == IA32_SYSCALL_VECTOR)
829 for_each_cpu_mask_nr(new_cpu, new_mask)
830 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
833 current_vector = vector;
834 current_offset = offset;
836 cfg->move_in_progress = 1;
837 cfg->old_domain = cfg->domain;
839 for_each_cpu_mask_nr(new_cpu, new_mask)
840 per_cpu(vector_irq, new_cpu)[vector] = irq;
841 cfg->vector = vector;
842 cfg->domain = domain;
848 static int assign_irq_vector(int irq, cpumask_t mask)
853 spin_lock_irqsave(&vector_lock, flags);
854 err = __assign_irq_vector(irq, mask);
855 spin_unlock_irqrestore(&vector_lock, flags);
859 static void __clear_irq_vector(int irq)
865 BUG_ON((unsigned)irq >= NR_IRQS);
867 BUG_ON(!cfg->vector);
869 vector = cfg->vector;
870 cpus_and(mask, cfg->domain, cpu_online_map);
871 for_each_cpu_mask_nr(cpu, mask)
872 per_cpu(vector_irq, cpu)[vector] = -1;
875 cpus_clear(cfg->domain);
878 static void __setup_vector_irq(int cpu)
880 /* Initialize vector_irq on a new cpu */
881 /* This function must be called with vector_lock held */
884 /* Mark the inuse vectors */
885 for (irq = 0; irq < NR_IRQS; ++irq) {
886 if (!cpu_isset(cpu, irq_cfg[irq].domain))
888 vector = irq_cfg[irq].vector;
889 per_cpu(vector_irq, cpu)[vector] = irq;
891 /* Mark the free vectors */
892 for (vector = 0; vector < NR_VECTORS; ++vector) {
893 irq = per_cpu(vector_irq, cpu)[vector];
896 if (!cpu_isset(cpu, irq_cfg[irq].domain))
897 per_cpu(vector_irq, cpu)[vector] = -1;
901 void setup_vector_irq(int cpu)
903 spin_lock(&vector_lock);
904 __setup_vector_irq(smp_processor_id());
905 spin_unlock(&vector_lock);
909 static struct irq_chip ioapic_chip;
910 #ifdef CONFIG_INTR_REMAP
911 static struct irq_chip ir_ioapic_chip;
914 static void ioapic_register_intr(int irq, unsigned long trigger)
917 irq_desc[irq].status |= IRQ_LEVEL;
919 irq_desc[irq].status &= ~IRQ_LEVEL;
921 #ifdef CONFIG_INTR_REMAP
922 if (irq_remapped(irq)) {
923 irq_desc[irq].status |= IRQ_MOVE_PCNTXT;
925 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
929 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
930 handle_edge_irq, "edge");
935 set_irq_chip_and_handler_name(irq, &ioapic_chip,
939 set_irq_chip_and_handler_name(irq, &ioapic_chip,
940 handle_edge_irq, "edge");
943 static int setup_ioapic_entry(int apic, int irq,
944 struct IO_APIC_route_entry *entry,
945 unsigned int destination, int trigger,
946 int polarity, int vector)
949 * add it to the IO-APIC irq-routing table:
951 memset(entry,0,sizeof(*entry));
953 #ifdef CONFIG_INTR_REMAP
954 if (intr_remapping_enabled) {
955 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
957 struct IR_IO_APIC_route_entry *ir_entry =
958 (struct IR_IO_APIC_route_entry *) entry;
962 panic("No mapping iommu for ioapic %d\n", apic);
964 index = alloc_irte(iommu, irq, 1);
966 panic("Failed to allocate IRTE for ioapic %d\n", apic);
968 memset(&irte, 0, sizeof(irte));
971 irte.dst_mode = INT_DEST_MODE;
972 irte.trigger_mode = trigger;
973 irte.dlvry_mode = INT_DELIVERY_MODE;
974 irte.vector = vector;
975 irte.dest_id = IRTE_DEST(destination);
977 modify_irte(irq, &irte);
979 ir_entry->index2 = (index >> 15) & 0x1;
981 ir_entry->format = 1;
982 ir_entry->index = (index & 0x7fff);
986 entry->delivery_mode = INT_DELIVERY_MODE;
987 entry->dest_mode = INT_DEST_MODE;
988 entry->dest = destination;
991 entry->mask = 0; /* enable IRQ */
992 entry->trigger = trigger;
993 entry->polarity = polarity;
994 entry->vector = vector;
996 /* Mask level triggered irqs.
997 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1004 static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
1005 int trigger, int polarity)
1007 struct irq_cfg *cfg = irq_cfg + irq;
1008 struct IO_APIC_route_entry entry;
1011 if (!IO_APIC_IRQ(irq))
1015 if (assign_irq_vector(irq, mask))
1018 cpus_and(mask, cfg->domain, mask);
1020 apic_printk(APIC_VERBOSE,KERN_DEBUG
1021 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1022 "IRQ %d Mode:%i Active:%i)\n",
1023 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1024 irq, trigger, polarity);
1027 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1028 cpu_mask_to_apicid(mask), trigger, polarity,
1030 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1031 mp_ioapics[apic].mp_apicid, pin);
1032 __clear_irq_vector(irq);
1036 ioapic_register_intr(irq, trigger);
1038 disable_8259A_irq(irq);
1040 ioapic_write_entry(apic, pin, entry);
1043 static void __init setup_IO_APIC_irqs(void)
1045 int apic, pin, idx, irq, first_notcon = 1;
1047 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1049 for (apic = 0; apic < nr_ioapics; apic++) {
1050 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1052 idx = find_irq_entry(apic,pin,mp_INT);
1055 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
1058 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
1061 if (!first_notcon) {
1062 apic_printk(APIC_VERBOSE, " not connected.\n");
1066 irq = pin_2_irq(idx, apic, pin);
1067 add_pin_to_irq(irq, apic, pin);
1069 setup_IO_APIC_irq(apic, pin, irq,
1070 irq_trigger(idx), irq_polarity(idx));
1075 apic_printk(APIC_VERBOSE, " not connected.\n");
1079 * Set up the timer pin, possibly with the 8259A-master behind.
1081 static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1084 struct IO_APIC_route_entry entry;
1086 if (intr_remapping_enabled)
1089 memset(&entry, 0, sizeof(entry));
1092 * We use logical delivery to get the timer IRQ
1095 entry.dest_mode = INT_DEST_MODE;
1096 entry.mask = 1; /* mask IRQ now */
1097 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
1098 entry.delivery_mode = INT_DELIVERY_MODE;
1101 entry.vector = vector;
1104 * The timer IRQ doesn't have to know that behind the
1105 * scene we may have a 8259A-master in AEOI mode ...
1107 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1110 * Add it to the IO-APIC irq-routing table:
1112 ioapic_write_entry(apic, pin, entry);
1115 void __apicdebuginit print_IO_APIC(void)
1118 union IO_APIC_reg_00 reg_00;
1119 union IO_APIC_reg_01 reg_01;
1120 union IO_APIC_reg_02 reg_02;
1121 unsigned long flags;
1123 if (apic_verbosity == APIC_QUIET)
1126 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1127 for (i = 0; i < nr_ioapics; i++)
1128 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1129 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
1132 * We are a bit conservative about what we expect. We have to
1133 * know about every hardware change ASAP.
1135 printk(KERN_INFO "testing the IO APIC.......................\n");
1137 for (apic = 0; apic < nr_ioapics; apic++) {
1139 spin_lock_irqsave(&ioapic_lock, flags);
1140 reg_00.raw = io_apic_read(apic, 0);
1141 reg_01.raw = io_apic_read(apic, 1);
1142 if (reg_01.bits.version >= 0x10)
1143 reg_02.raw = io_apic_read(apic, 2);
1144 spin_unlock_irqrestore(&ioapic_lock, flags);
1147 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
1148 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1149 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1151 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1152 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1154 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1155 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1157 if (reg_01.bits.version >= 0x10) {
1158 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1159 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1162 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1164 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1165 " Stat Dmod Deli Vect: \n");
1167 for (i = 0; i <= reg_01.bits.entries; i++) {
1168 struct IO_APIC_route_entry entry;
1170 entry = ioapic_read_entry(apic, i);
1172 printk(KERN_DEBUG " %02x %03X ",
1177 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1182 entry.delivery_status,
1184 entry.delivery_mode,
1189 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1190 for (i = 0; i < NR_IRQS; i++) {
1191 struct irq_pin_list *entry = irq_2_pin + i;
1194 printk(KERN_DEBUG "IRQ%d ", i);
1196 printk("-> %d:%d", entry->apic, entry->pin);
1199 entry = irq_2_pin + entry->next;
1204 printk(KERN_INFO ".................................... done.\n");
1211 static __apicdebuginit void print_APIC_bitfield (int base)
1216 if (apic_verbosity == APIC_QUIET)
1219 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1220 for (i = 0; i < 8; i++) {
1221 v = apic_read(base + i*0x10);
1222 for (j = 0; j < 32; j++) {
1232 void __apicdebuginit print_local_APIC(void * dummy)
1234 unsigned int v, ver, maxlvt;
1237 if (apic_verbosity == APIC_QUIET)
1240 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1241 smp_processor_id(), hard_smp_processor_id());
1242 v = apic_read(APIC_ID);
1243 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
1244 v = apic_read(APIC_LVR);
1245 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1246 ver = GET_APIC_VERSION(v);
1247 maxlvt = lapic_get_maxlvt();
1249 v = apic_read(APIC_TASKPRI);
1250 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1252 v = apic_read(APIC_ARBPRI);
1253 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1254 v & APIC_ARBPRI_MASK);
1255 v = apic_read(APIC_PROCPRI);
1256 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1258 v = apic_read(APIC_EOI);
1259 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1260 v = apic_read(APIC_RRR);
1261 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1262 v = apic_read(APIC_LDR);
1263 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1264 v = apic_read(APIC_DFR);
1265 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1266 v = apic_read(APIC_SPIV);
1267 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1269 printk(KERN_DEBUG "... APIC ISR field:\n");
1270 print_APIC_bitfield(APIC_ISR);
1271 printk(KERN_DEBUG "... APIC TMR field:\n");
1272 print_APIC_bitfield(APIC_TMR);
1273 printk(KERN_DEBUG "... APIC IRR field:\n");
1274 print_APIC_bitfield(APIC_IRR);
1276 v = apic_read(APIC_ESR);
1277 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1279 icr = apic_icr_read();
1280 printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
1281 printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
1283 v = apic_read(APIC_LVTT);
1284 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1286 if (maxlvt > 3) { /* PC is LVT#4. */
1287 v = apic_read(APIC_LVTPC);
1288 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1290 v = apic_read(APIC_LVT0);
1291 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1292 v = apic_read(APIC_LVT1);
1293 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1295 if (maxlvt > 2) { /* ERR is LVT#3. */
1296 v = apic_read(APIC_LVTERR);
1297 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1300 v = apic_read(APIC_TMICT);
1301 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1302 v = apic_read(APIC_TMCCT);
1303 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1304 v = apic_read(APIC_TDCR);
1305 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1309 void print_all_local_APICs (void)
1311 on_each_cpu(print_local_APIC, NULL, 1);
1314 void __apicdebuginit print_PIC(void)
1317 unsigned long flags;
1319 if (apic_verbosity == APIC_QUIET)
1322 printk(KERN_DEBUG "\nprinting PIC contents\n");
1324 spin_lock_irqsave(&i8259A_lock, flags);
1326 v = inb(0xa1) << 8 | inb(0x21);
1327 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1329 v = inb(0xa0) << 8 | inb(0x20);
1330 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1334 v = inb(0xa0) << 8 | inb(0x20);
1338 spin_unlock_irqrestore(&i8259A_lock, flags);
1340 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1342 v = inb(0x4d1) << 8 | inb(0x4d0);
1343 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1348 void __init enable_IO_APIC(void)
1350 union IO_APIC_reg_01 reg_01;
1351 int i8259_apic, i8259_pin;
1353 unsigned long flags;
1355 for (i = 0; i < PIN_MAP_SIZE; i++) {
1356 irq_2_pin[i].pin = -1;
1357 irq_2_pin[i].next = 0;
1361 * The number of IO-APIC IRQ registers (== #pins):
1363 for (apic = 0; apic < nr_ioapics; apic++) {
1364 spin_lock_irqsave(&ioapic_lock, flags);
1365 reg_01.raw = io_apic_read(apic, 1);
1366 spin_unlock_irqrestore(&ioapic_lock, flags);
1367 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1369 for(apic = 0; apic < nr_ioapics; apic++) {
1371 /* See if any of the pins is in ExtINT mode */
1372 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1373 struct IO_APIC_route_entry entry;
1374 entry = ioapic_read_entry(apic, pin);
1376 /* If the interrupt line is enabled and in ExtInt mode
1377 * I have found the pin where the i8259 is connected.
1379 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1380 ioapic_i8259.apic = apic;
1381 ioapic_i8259.pin = pin;
1387 /* Look to see what if the MP table has reported the ExtINT */
1388 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1389 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1390 /* Trust the MP table if nothing is setup in the hardware */
1391 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1392 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1393 ioapic_i8259.pin = i8259_pin;
1394 ioapic_i8259.apic = i8259_apic;
1396 /* Complain if the MP table and the hardware disagree */
1397 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1398 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1400 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1404 * Do not trust the IO-APIC being empty at bootup
1410 * Not an __init, needed by the reboot code
1412 void disable_IO_APIC(void)
1415 * Clear the IO-APIC before rebooting:
1420 * If the i8259 is routed through an IOAPIC
1421 * Put that IOAPIC in virtual wire mode
1422 * so legacy interrupts can be delivered.
1424 if (ioapic_i8259.pin != -1) {
1425 struct IO_APIC_route_entry entry;
1427 memset(&entry, 0, sizeof(entry));
1428 entry.mask = 0; /* Enabled */
1429 entry.trigger = 0; /* Edge */
1431 entry.polarity = 0; /* High */
1432 entry.delivery_status = 0;
1433 entry.dest_mode = 0; /* Physical */
1434 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1436 entry.dest = read_apic_id();
1439 * Add it to the IO-APIC irq-routing table:
1441 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1444 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1448 * There is a nasty bug in some older SMP boards, their mptable lies
1449 * about the timer IRQ. We do the following to work around the situation:
1451 * - timer IRQ defaults to IO-APIC IRQ
1452 * - if this function detects that timer IRQs are defunct, then we fall
1453 * back to ISA timer IRQs
1455 static int __init timer_irq_works(void)
1457 unsigned long t1 = jiffies;
1458 unsigned long flags;
1460 local_save_flags(flags);
1462 /* Let ten ticks pass... */
1463 mdelay((10 * 1000) / HZ);
1464 local_irq_restore(flags);
1467 * Expect a few ticks at least, to be sure some possible
1468 * glue logic does not lock up after one or two first
1469 * ticks in a non-ExtINT mode. Also the local APIC
1470 * might have cached one ExtINT interrupt. Finally, at
1471 * least one tick may be lost due to delays.
1475 if (time_after(jiffies, t1 + 4))
1481 * In the SMP+IOAPIC case it might happen that there are an unspecified
1482 * number of pending IRQ events unhandled. These cases are very rare,
1483 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1484 * better to do it this way as thus we do not have to be aware of
1485 * 'pending' interrupts in the IRQ path, except at this point.
1488 * Edge triggered needs to resend any interrupt
1489 * that was delayed but this is now handled in the device
1494 * Starting up a edge-triggered IO-APIC interrupt is
1495 * nasty - we need to make sure that we get the edge.
1496 * If it is already asserted for some reason, we need
1497 * return 1 to indicate that is was pending.
1499 * This is not complete - we should be able to fake
1500 * an edge even if it isn't on the 8259A...
1503 static unsigned int startup_ioapic_irq(unsigned int irq)
1505 int was_pending = 0;
1506 unsigned long flags;
1508 spin_lock_irqsave(&ioapic_lock, flags);
1510 disable_8259A_irq(irq);
1511 if (i8259A_irq_pending(irq))
1514 __unmask_IO_APIC_irq(irq);
1515 spin_unlock_irqrestore(&ioapic_lock, flags);
1520 static int ioapic_retrigger_irq(unsigned int irq)
1522 struct irq_cfg *cfg = &irq_cfg[irq];
1523 unsigned long flags;
1525 spin_lock_irqsave(&vector_lock, flags);
1526 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
1527 spin_unlock_irqrestore(&vector_lock, flags);
1533 * Level and edge triggered IO-APIC interrupts need different handling,
1534 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1535 * handled with the level-triggered descriptor, but that one has slightly
1536 * more overhead. Level-triggered interrupts cannot be handled with the
1537 * edge-triggered handler, without risking IRQ storms and other ugly
1543 #ifdef CONFIG_INTR_REMAP
1544 static void ir_irq_migration(struct work_struct *work);
1546 static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
1549 * Migrate the IO-APIC irq in the presence of intr-remapping.
1551 * For edge triggered, irq migration is a simple atomic update(of vector
1552 * and cpu destination) of IRTE and flush the hardware cache.
1554 * For level triggered, we need to modify the io-apic RTE aswell with the update
1555 * vector information, along with modifying IRTE with vector and destination.
1556 * So irq migration for level triggered is little bit more complex compared to
1557 * edge triggered migration. But the good news is, we use the same algorithm
1558 * for level triggered migration as we have today, only difference being,
1559 * we now initiate the irq migration from process context instead of the
1560 * interrupt context.
1562 * In future, when we do a directed EOI (combined with cpu EOI broadcast
1563 * suppression) to the IO-APIC, level triggered irq migration will also be
1564 * as simple as edge triggered migration and we can do the irq migration
1565 * with a simple atomic update to IO-APIC RTE.
1567 static void migrate_ioapic_irq(int irq, cpumask_t mask)
1569 struct irq_cfg *cfg = irq_cfg + irq;
1570 struct irq_desc *desc = irq_desc + irq;
1571 cpumask_t tmp, cleanup_mask;
1573 int modify_ioapic_rte = desc->status & IRQ_LEVEL;
1575 unsigned long flags;
1577 cpus_and(tmp, mask, cpu_online_map);
1578 if (cpus_empty(tmp))
1581 if (get_irte(irq, &irte))
1584 if (assign_irq_vector(irq, mask))
1587 cpus_and(tmp, cfg->domain, mask);
1588 dest = cpu_mask_to_apicid(tmp);
1590 if (modify_ioapic_rte) {
1591 spin_lock_irqsave(&ioapic_lock, flags);
1592 __target_IO_APIC_irq(irq, dest, cfg->vector);
1593 spin_unlock_irqrestore(&ioapic_lock, flags);
1596 irte.vector = cfg->vector;
1597 irte.dest_id = IRTE_DEST(dest);
1600 * Modified the IRTE and flushes the Interrupt entry cache.
1602 modify_irte(irq, &irte);
1604 if (cfg->move_in_progress) {
1605 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1606 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1607 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1608 cfg->move_in_progress = 0;
1611 irq_desc[irq].affinity = mask;
1614 static int migrate_irq_remapped_level(int irq)
1618 mask_IO_APIC_irq(irq);
1620 if (io_apic_level_ack_pending(irq)) {
1622 * Interrupt in progress. Migrating irq now will change the
1623 * vector information in the IO-APIC RTE and that will confuse
1624 * the EOI broadcast performed by cpu.
1625 * So, delay the irq migration to the next instance.
1627 schedule_delayed_work(&ir_migration_work, 1);
1631 /* everthing is clear. we have right of way */
1632 migrate_ioapic_irq(irq, irq_desc[irq].pending_mask);
1635 irq_desc[irq].status &= ~IRQ_MOVE_PENDING;
1636 cpus_clear(irq_desc[irq].pending_mask);
1639 unmask_IO_APIC_irq(irq);
1643 static void ir_irq_migration(struct work_struct *work)
1647 for (irq = 0; irq < NR_IRQS; irq++) {
1648 struct irq_desc *desc = irq_desc + irq;
1649 if (desc->status & IRQ_MOVE_PENDING) {
1650 unsigned long flags;
1652 spin_lock_irqsave(&desc->lock, flags);
1653 if (!desc->chip->set_affinity ||
1654 !(desc->status & IRQ_MOVE_PENDING)) {
1655 desc->status &= ~IRQ_MOVE_PENDING;
1656 spin_unlock_irqrestore(&desc->lock, flags);
1660 desc->chip->set_affinity(irq,
1661 irq_desc[irq].pending_mask);
1662 spin_unlock_irqrestore(&desc->lock, flags);
1668 * Migrates the IRQ destination in the process context.
1670 static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
1672 if (irq_desc[irq].status & IRQ_LEVEL) {
1673 irq_desc[irq].status |= IRQ_MOVE_PENDING;
1674 irq_desc[irq].pending_mask = mask;
1675 migrate_irq_remapped_level(irq);
1679 migrate_ioapic_irq(irq, mask);
1683 asmlinkage void smp_irq_move_cleanup_interrupt(void)
1685 unsigned vector, me;
1690 me = smp_processor_id();
1691 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1693 struct irq_desc *desc;
1694 struct irq_cfg *cfg;
1695 irq = __get_cpu_var(vector_irq)[vector];
1699 desc = irq_desc + irq;
1700 cfg = irq_cfg + irq;
1701 spin_lock(&desc->lock);
1702 if (!cfg->move_cleanup_count)
1705 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
1708 __get_cpu_var(vector_irq)[vector] = -1;
1709 cfg->move_cleanup_count--;
1711 spin_unlock(&desc->lock);
1717 static void irq_complete_move(unsigned int irq)
1719 struct irq_cfg *cfg = irq_cfg + irq;
1720 unsigned vector, me;
1722 if (likely(!cfg->move_in_progress))
1725 vector = ~get_irq_regs()->orig_ax;
1726 me = smp_processor_id();
1727 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
1728 cpumask_t cleanup_mask;
1730 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1731 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1732 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1733 cfg->move_in_progress = 0;
1737 static inline void irq_complete_move(unsigned int irq) {}
1739 #ifdef CONFIG_INTR_REMAP
1740 static void ack_x2apic_level(unsigned int irq)
1745 static void ack_x2apic_edge(unsigned int irq)
1751 static void ack_apic_edge(unsigned int irq)
1753 irq_complete_move(irq);
1754 move_native_irq(irq);
1758 static void ack_apic_level(unsigned int irq)
1760 int do_unmask_irq = 0;
1762 irq_complete_move(irq);
1763 #ifdef CONFIG_GENERIC_PENDING_IRQ
1764 /* If we are moving the irq we need to mask it */
1765 if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1767 mask_IO_APIC_irq(irq);
1772 * We must acknowledge the irq before we move it or the acknowledge will
1773 * not propagate properly.
1777 /* Now we can move and renable the irq */
1778 if (unlikely(do_unmask_irq)) {
1779 /* Only migrate the irq if the ack has been received.
1781 * On rare occasions the broadcast level triggered ack gets
1782 * delayed going to ioapics, and if we reprogram the
1783 * vector while Remote IRR is still set the irq will never
1786 * To prevent this scenario we read the Remote IRR bit
1787 * of the ioapic. This has two effects.
1788 * - On any sane system the read of the ioapic will
1789 * flush writes (and acks) going to the ioapic from
1791 * - We get to see if the ACK has actually been delivered.
1793 * Based on failed experiments of reprogramming the
1794 * ioapic entry from outside of irq context starting
1795 * with masking the ioapic entry and then polling until
1796 * Remote IRR was clear before reprogramming the
1797 * ioapic I don't trust the Remote IRR bit to be
1798 * completey accurate.
1800 * However there appears to be no other way to plug
1801 * this race, so if the Remote IRR bit is not
1802 * accurate and is causing problems then it is a hardware bug
1803 * and you can go talk to the chipset vendor about it.
1805 if (!io_apic_level_ack_pending(irq))
1806 move_masked_irq(irq);
1807 unmask_IO_APIC_irq(irq);
1811 static struct irq_chip ioapic_chip __read_mostly = {
1813 .startup = startup_ioapic_irq,
1814 .mask = mask_IO_APIC_irq,
1815 .unmask = unmask_IO_APIC_irq,
1816 .ack = ack_apic_edge,
1817 .eoi = ack_apic_level,
1819 .set_affinity = set_ioapic_affinity_irq,
1821 .retrigger = ioapic_retrigger_irq,
1824 #ifdef CONFIG_INTR_REMAP
1825 static struct irq_chip ir_ioapic_chip __read_mostly = {
1826 .name = "IR-IO-APIC",
1827 .startup = startup_ioapic_irq,
1828 .mask = mask_IO_APIC_irq,
1829 .unmask = unmask_IO_APIC_irq,
1830 .ack = ack_x2apic_edge,
1831 .eoi = ack_x2apic_level,
1833 .set_affinity = set_ir_ioapic_affinity_irq,
1835 .retrigger = ioapic_retrigger_irq,
1839 static inline void init_IO_APIC_traps(void)
1844 * NOTE! The local APIC isn't very good at handling
1845 * multiple interrupts at the same interrupt level.
1846 * As the interrupt level is determined by taking the
1847 * vector number and shifting that right by 4, we
1848 * want to spread these out a bit so that they don't
1849 * all fall in the same interrupt level.
1851 * Also, we've got to be careful not to trash gate
1852 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1854 for (irq = 0; irq < NR_IRQS ; irq++) {
1855 if (IO_APIC_IRQ(irq) && !irq_cfg[irq].vector) {
1857 * Hmm.. We don't have an entry for this,
1858 * so default to an old-fashioned 8259
1859 * interrupt if we can..
1862 make_8259A_irq(irq);
1864 /* Strange. Oh, well.. */
1865 irq_desc[irq].chip = &no_irq_chip;
1870 static void unmask_lapic_irq(unsigned int irq)
1874 v = apic_read(APIC_LVT0);
1875 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1878 static void mask_lapic_irq(unsigned int irq)
1882 v = apic_read(APIC_LVT0);
1883 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1886 static void ack_lapic_irq (unsigned int irq)
1891 static struct irq_chip lapic_chip __read_mostly = {
1892 .name = "local-APIC",
1893 .mask = mask_lapic_irq,
1894 .unmask = unmask_lapic_irq,
1895 .ack = ack_lapic_irq,
1898 static void lapic_register_intr(int irq)
1900 irq_desc[irq].status &= ~IRQ_LEVEL;
1901 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
1905 static void __init setup_nmi(void)
1908 * Dirty trick to enable the NMI watchdog ...
1909 * We put the 8259A master into AEOI mode and
1910 * unmask on all local APICs LVT0 as NMI.
1912 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1913 * is from Maciej W. Rozycki - so we do not have to EOI from
1914 * the NMI handler or the timer interrupt.
1916 printk(KERN_INFO "activating NMI Watchdog ...");
1918 enable_NMI_through_LVT0();
1924 * This looks a bit hackish but it's about the only one way of sending
1925 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1926 * not support the ExtINT mode, unfortunately. We need to send these
1927 * cycles as some i82489DX-based boards have glue logic that keeps the
1928 * 8259A interrupt line asserted until INTA. --macro
1930 static inline void __init unlock_ExtINT_logic(void)
1933 struct IO_APIC_route_entry entry0, entry1;
1934 unsigned char save_control, save_freq_select;
1936 pin = find_isa_irq_pin(8, mp_INT);
1937 apic = find_isa_irq_apic(8, mp_INT);
1941 entry0 = ioapic_read_entry(apic, pin);
1943 clear_IO_APIC_pin(apic, pin);
1945 memset(&entry1, 0, sizeof(entry1));
1947 entry1.dest_mode = 0; /* physical delivery */
1948 entry1.mask = 0; /* unmask IRQ now */
1949 entry1.dest = hard_smp_processor_id();
1950 entry1.delivery_mode = dest_ExtINT;
1951 entry1.polarity = entry0.polarity;
1955 ioapic_write_entry(apic, pin, entry1);
1957 save_control = CMOS_READ(RTC_CONTROL);
1958 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1959 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1961 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1966 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1970 CMOS_WRITE(save_control, RTC_CONTROL);
1971 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1972 clear_IO_APIC_pin(apic, pin);
1974 ioapic_write_entry(apic, pin, entry0);
1978 * This code may look a bit paranoid, but it's supposed to cooperate with
1979 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1980 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1981 * fanatically on his truly buggy board.
1983 * FIXME: really need to revamp this for modern platforms only.
1985 static inline void __init check_timer(void)
1987 struct irq_cfg *cfg = irq_cfg + 0;
1988 int apic1, pin1, apic2, pin2;
1989 unsigned long flags;
1992 local_irq_save(flags);
1995 * get/set the timer IRQ vector:
1997 disable_8259A_irq(0);
1998 assign_irq_vector(0, TARGET_CPUS);
2001 * As IRQ0 is to be enabled in the 8259A, the virtual
2002 * wire has to be disabled in the local APIC.
2004 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2007 pin1 = find_isa_irq_pin(0, mp_INT);
2008 apic1 = find_isa_irq_apic(0, mp_INT);
2009 pin2 = ioapic_i8259.pin;
2010 apic2 = ioapic_i8259.apic;
2012 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2013 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2014 cfg->vector, apic1, pin1, apic2, pin2);
2017 * Some BIOS writers are clueless and report the ExtINTA
2018 * I/O APIC input from the cascaded 8259A as the timer
2019 * interrupt input. So just in case, if only one pin
2020 * was found above, try it both directly and through the
2024 if (intr_remapping_enabled)
2025 panic("BIOS bug: timer not connected to IO-APIC");
2029 } else if (pin2 == -1) {
2036 * Ok, does IRQ0 through the IOAPIC work?
2039 add_pin_to_irq(0, apic1, pin1);
2040 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2042 unmask_IO_APIC_irq(0);
2043 if (!no_timer_check && timer_irq_works()) {
2044 if (nmi_watchdog == NMI_IO_APIC) {
2046 enable_8259A_irq(0);
2048 if (disable_timer_pin_1 > 0)
2049 clear_IO_APIC_pin(0, pin1);
2052 if (intr_remapping_enabled)
2053 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2054 clear_IO_APIC_pin(apic1, pin1);
2056 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2057 "8254 timer not connected to IO-APIC\n");
2059 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2060 "(IRQ0) through the 8259A ...\n");
2061 apic_printk(APIC_QUIET, KERN_INFO
2062 "..... (found apic %d pin %d) ...\n", apic2, pin2);
2064 * legacy devices should be connected to IO APIC #0
2066 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
2067 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2068 unmask_IO_APIC_irq(0);
2069 enable_8259A_irq(0);
2070 if (timer_irq_works()) {
2071 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2072 timer_through_8259 = 1;
2073 if (nmi_watchdog == NMI_IO_APIC) {
2074 disable_8259A_irq(0);
2076 enable_8259A_irq(0);
2081 * Cleanup, just in case ...
2083 disable_8259A_irq(0);
2084 clear_IO_APIC_pin(apic2, pin2);
2085 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2088 if (nmi_watchdog == NMI_IO_APIC) {
2089 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2090 "through the IO-APIC - disabling NMI Watchdog!\n");
2091 nmi_watchdog = NMI_NONE;
2094 apic_printk(APIC_QUIET, KERN_INFO
2095 "...trying to set up timer as Virtual Wire IRQ...\n");
2097 lapic_register_intr(0);
2098 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
2099 enable_8259A_irq(0);
2101 if (timer_irq_works()) {
2102 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2105 disable_8259A_irq(0);
2106 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2107 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2109 apic_printk(APIC_QUIET, KERN_INFO
2110 "...trying to set up timer as ExtINT IRQ...\n");
2114 apic_write(APIC_LVT0, APIC_DM_EXTINT);
2116 unlock_ExtINT_logic();
2118 if (timer_irq_works()) {
2119 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2122 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2123 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2124 "report. Then try booting with the 'noapic' option.\n");
2126 local_irq_restore(flags);
2129 static int __init notimercheck(char *s)
2134 __setup("no_timer_check", notimercheck);
2137 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2138 * to devices. However there may be an I/O APIC pin available for
2139 * this interrupt regardless. The pin may be left unconnected, but
2140 * typically it will be reused as an ExtINT cascade interrupt for
2141 * the master 8259A. In the MPS case such a pin will normally be
2142 * reported as an ExtINT interrupt in the MP table. With ACPI
2143 * there is no provision for ExtINT interrupts, and in the absence
2144 * of an override it would be treated as an ordinary ISA I/O APIC
2145 * interrupt, that is edge-triggered and unmasked by default. We
2146 * used to do this, but it caused problems on some systems because
2147 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2148 * the same ExtINT cascade interrupt to drive the local APIC of the
2149 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2150 * the I/O APIC in all cases now. No actual device should request
2151 * it anyway. --macro
2153 #define PIC_IRQS (1<<2)
2155 void __init setup_IO_APIC(void)
2159 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2162 io_apic_irqs = ~PIC_IRQS;
2164 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2167 setup_IO_APIC_irqs();
2168 init_IO_APIC_traps();
2174 struct sysfs_ioapic_data {
2175 struct sys_device dev;
2176 struct IO_APIC_route_entry entry[0];
2178 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2180 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2182 struct IO_APIC_route_entry *entry;
2183 struct sysfs_ioapic_data *data;
2186 data = container_of(dev, struct sysfs_ioapic_data, dev);
2187 entry = data->entry;
2188 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2189 *entry = ioapic_read_entry(dev->id, i);
2194 static int ioapic_resume(struct sys_device *dev)
2196 struct IO_APIC_route_entry *entry;
2197 struct sysfs_ioapic_data *data;
2198 unsigned long flags;
2199 union IO_APIC_reg_00 reg_00;
2202 data = container_of(dev, struct sysfs_ioapic_data, dev);
2203 entry = data->entry;
2205 spin_lock_irqsave(&ioapic_lock, flags);
2206 reg_00.raw = io_apic_read(dev->id, 0);
2207 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2208 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
2209 io_apic_write(dev->id, 0, reg_00.raw);
2211 spin_unlock_irqrestore(&ioapic_lock, flags);
2212 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
2213 ioapic_write_entry(dev->id, i, entry[i]);
2218 static struct sysdev_class ioapic_sysdev_class = {
2220 .suspend = ioapic_suspend,
2221 .resume = ioapic_resume,
2224 static int __init ioapic_init_sysfs(void)
2226 struct sys_device * dev;
2229 error = sysdev_class_register(&ioapic_sysdev_class);
2233 for (i = 0; i < nr_ioapics; i++ ) {
2234 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2235 * sizeof(struct IO_APIC_route_entry);
2236 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
2237 if (!mp_ioapic_data[i]) {
2238 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2241 dev = &mp_ioapic_data[i]->dev;
2243 dev->cls = &ioapic_sysdev_class;
2244 error = sysdev_register(dev);
2246 kfree(mp_ioapic_data[i]);
2247 mp_ioapic_data[i] = NULL;
2248 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2256 device_initcall(ioapic_init_sysfs);
2259 * Dynamic irq allocate and deallocation
2261 int create_irq(void)
2263 /* Allocate an unused irq */
2266 unsigned long flags;
2269 spin_lock_irqsave(&vector_lock, flags);
2270 for (new = (NR_IRQS - 1); new >= 0; new--) {
2271 if (platform_legacy_irq(new))
2273 if (irq_cfg[new].vector != 0)
2275 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
2279 spin_unlock_irqrestore(&vector_lock, flags);
2282 dynamic_irq_init(irq);
2287 void destroy_irq(unsigned int irq)
2289 unsigned long flags;
2291 dynamic_irq_cleanup(irq);
2293 #ifdef CONFIG_INTR_REMAP
2296 spin_lock_irqsave(&vector_lock, flags);
2297 __clear_irq_vector(irq);
2298 spin_unlock_irqrestore(&vector_lock, flags);
2302 * MSI message composition
2304 #ifdef CONFIG_PCI_MSI
2305 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
2307 struct irq_cfg *cfg = irq_cfg + irq;
2313 err = assign_irq_vector(irq, tmp);
2317 cpus_and(tmp, cfg->domain, tmp);
2318 dest = cpu_mask_to_apicid(tmp);
2320 #ifdef CONFIG_INTR_REMAP
2321 if (irq_remapped(irq)) {
2326 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
2327 BUG_ON(ir_index == -1);
2329 memset (&irte, 0, sizeof(irte));
2332 irte.dst_mode = INT_DEST_MODE;
2333 irte.trigger_mode = 0; /* edge */
2334 irte.dlvry_mode = INT_DELIVERY_MODE;
2335 irte.vector = cfg->vector;
2336 irte.dest_id = IRTE_DEST(dest);
2338 modify_irte(irq, &irte);
2340 msg->address_hi = MSI_ADDR_BASE_HI;
2341 msg->data = sub_handle;
2342 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
2344 MSI_ADDR_IR_INDEX1(ir_index) |
2345 MSI_ADDR_IR_INDEX2(ir_index);
2349 msg->address_hi = MSI_ADDR_BASE_HI;
2352 ((INT_DEST_MODE == 0) ?
2353 MSI_ADDR_DEST_MODE_PHYSICAL:
2354 MSI_ADDR_DEST_MODE_LOGICAL) |
2355 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2356 MSI_ADDR_REDIRECTION_CPU:
2357 MSI_ADDR_REDIRECTION_LOWPRI) |
2358 MSI_ADDR_DEST_ID(dest);
2361 MSI_DATA_TRIGGER_EDGE |
2362 MSI_DATA_LEVEL_ASSERT |
2363 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2364 MSI_DATA_DELIVERY_FIXED:
2365 MSI_DATA_DELIVERY_LOWPRI) |
2366 MSI_DATA_VECTOR(cfg->vector);
2372 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2374 struct irq_cfg *cfg = irq_cfg + irq;
2379 cpus_and(tmp, mask, cpu_online_map);
2380 if (cpus_empty(tmp))
2383 if (assign_irq_vector(irq, mask))
2386 cpus_and(tmp, cfg->domain, mask);
2387 dest = cpu_mask_to_apicid(tmp);
2389 read_msi_msg(irq, &msg);
2391 msg.data &= ~MSI_DATA_VECTOR_MASK;
2392 msg.data |= MSI_DATA_VECTOR(cfg->vector);
2393 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2394 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2396 write_msi_msg(irq, &msg);
2397 irq_desc[irq].affinity = mask;
2400 #ifdef CONFIG_INTR_REMAP
2402 * Migrate the MSI irq to another cpumask. This migration is
2403 * done in the process context using interrupt-remapping hardware.
2405 static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2407 struct irq_cfg *cfg = irq_cfg + irq;
2409 cpumask_t tmp, cleanup_mask;
2412 cpus_and(tmp, mask, cpu_online_map);
2413 if (cpus_empty(tmp))
2416 if (get_irte(irq, &irte))
2419 if (assign_irq_vector(irq, mask))
2422 cpus_and(tmp, cfg->domain, mask);
2423 dest = cpu_mask_to_apicid(tmp);
2425 irte.vector = cfg->vector;
2426 irte.dest_id = IRTE_DEST(dest);
2429 * atomically update the IRTE with the new destination and vector.
2431 modify_irte(irq, &irte);
2434 * After this point, all the interrupts will start arriving
2435 * at the new destination. So, time to cleanup the previous
2436 * vector allocation.
2438 if (cfg->move_in_progress) {
2439 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2440 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2441 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2442 cfg->move_in_progress = 0;
2445 irq_desc[irq].affinity = mask;
2448 #endif /* CONFIG_SMP */
2451 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2452 * which implement the MSI or MSI-X Capability Structure.
2454 static struct irq_chip msi_chip = {
2456 .unmask = unmask_msi_irq,
2457 .mask = mask_msi_irq,
2458 .ack = ack_apic_edge,
2460 .set_affinity = set_msi_irq_affinity,
2462 .retrigger = ioapic_retrigger_irq,
2465 #ifdef CONFIG_INTR_REMAP
2466 static struct irq_chip msi_ir_chip = {
2467 .name = "IR-PCI-MSI",
2468 .unmask = unmask_msi_irq,
2469 .mask = mask_msi_irq,
2470 .ack = ack_x2apic_edge,
2472 .set_affinity = ir_set_msi_irq_affinity,
2474 .retrigger = ioapic_retrigger_irq,
2478 * Map the PCI dev to the corresponding remapping hardware unit
2479 * and allocate 'nvec' consecutive interrupt-remapping table entries
2482 static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
2484 struct intel_iommu *iommu;
2487 iommu = map_dev_to_ir(dev);
2490 "Unable to map PCI %s to iommu\n", pci_name(dev));
2494 index = alloc_irte(iommu, irq, nvec);
2497 "Unable to allocate %d IRTE for PCI %s\n", nvec,
2505 static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
2510 ret = msi_compose_msg(dev, irq, &msg);
2514 set_irq_msi(irq, desc);
2515 write_msi_msg(irq, &msg);
2517 #ifdef CONFIG_INTR_REMAP
2518 if (irq_remapped(irq)) {
2519 struct irq_desc *desc = irq_desc + irq;
2521 * irq migration in process context
2523 desc->status |= IRQ_MOVE_PCNTXT;
2524 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
2527 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
2532 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
2540 #ifdef CONFIG_INTR_REMAP
2541 if (!intr_remapping_enabled)
2544 ret = msi_alloc_irte(dev, irq, 1);
2549 ret = setup_msi_irq(dev, desc, irq);
2556 #ifdef CONFIG_INTR_REMAP
2563 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
2565 int irq, ret, sub_handle;
2566 struct msi_desc *desc;
2567 #ifdef CONFIG_INTR_REMAP
2568 struct intel_iommu *iommu = 0;
2573 list_for_each_entry(desc, &dev->msi_list, list) {
2577 #ifdef CONFIG_INTR_REMAP
2578 if (!intr_remapping_enabled)
2583 * allocate the consecutive block of IRTE's
2586 index = msi_alloc_irte(dev, irq, nvec);
2592 iommu = map_dev_to_ir(dev);
2598 * setup the mapping between the irq and the IRTE
2599 * base index, the sub_handle pointing to the
2600 * appropriate interrupt remap table entry.
2602 set_irte_irq(irq, iommu, index, sub_handle);
2606 ret = setup_msi_irq(dev, desc, irq);
2618 void arch_teardown_msi_irq(unsigned int irq)
2625 static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
2627 struct irq_cfg *cfg = irq_cfg + irq;
2632 cpus_and(tmp, mask, cpu_online_map);
2633 if (cpus_empty(tmp))
2636 if (assign_irq_vector(irq, mask))
2639 cpus_and(tmp, cfg->domain, mask);
2640 dest = cpu_mask_to_apicid(tmp);
2642 dmar_msi_read(irq, &msg);
2644 msg.data &= ~MSI_DATA_VECTOR_MASK;
2645 msg.data |= MSI_DATA_VECTOR(cfg->vector);
2646 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2647 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2649 dmar_msi_write(irq, &msg);
2650 irq_desc[irq].affinity = mask;
2652 #endif /* CONFIG_SMP */
2654 struct irq_chip dmar_msi_type = {
2656 .unmask = dmar_msi_unmask,
2657 .mask = dmar_msi_mask,
2658 .ack = ack_apic_edge,
2660 .set_affinity = dmar_msi_set_affinity,
2662 .retrigger = ioapic_retrigger_irq,
2665 int arch_setup_dmar_msi(unsigned int irq)
2670 ret = msi_compose_msg(NULL, irq, &msg);
2673 dmar_msi_write(irq, &msg);
2674 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
2680 #endif /* CONFIG_PCI_MSI */
2682 * Hypertransport interrupt support
2684 #ifdef CONFIG_HT_IRQ
2688 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2690 struct ht_irq_msg msg;
2691 fetch_ht_irq_msg(irq, &msg);
2693 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2694 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2696 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2697 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2699 write_ht_irq_msg(irq, &msg);
2702 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2704 struct irq_cfg *cfg = irq_cfg + irq;
2708 cpus_and(tmp, mask, cpu_online_map);
2709 if (cpus_empty(tmp))
2712 if (assign_irq_vector(irq, mask))
2715 cpus_and(tmp, cfg->domain, mask);
2716 dest = cpu_mask_to_apicid(tmp);
2718 target_ht_irq(irq, dest, cfg->vector);
2719 irq_desc[irq].affinity = mask;
2723 static struct irq_chip ht_irq_chip = {
2725 .mask = mask_ht_irq,
2726 .unmask = unmask_ht_irq,
2727 .ack = ack_apic_edge,
2729 .set_affinity = set_ht_irq_affinity,
2731 .retrigger = ioapic_retrigger_irq,
2734 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2736 struct irq_cfg *cfg = irq_cfg + irq;
2741 err = assign_irq_vector(irq, tmp);
2743 struct ht_irq_msg msg;
2746 cpus_and(tmp, cfg->domain, tmp);
2747 dest = cpu_mask_to_apicid(tmp);
2749 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2753 HT_IRQ_LOW_DEST_ID(dest) |
2754 HT_IRQ_LOW_VECTOR(cfg->vector) |
2755 ((INT_DEST_MODE == 0) ?
2756 HT_IRQ_LOW_DM_PHYSICAL :
2757 HT_IRQ_LOW_DM_LOGICAL) |
2758 HT_IRQ_LOW_RQEOI_EDGE |
2759 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2760 HT_IRQ_LOW_MT_FIXED :
2761 HT_IRQ_LOW_MT_ARBITRATED) |
2762 HT_IRQ_LOW_IRQ_MASKED;
2764 write_ht_irq_msg(irq, &msg);
2766 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2767 handle_edge_irq, "edge");
2771 #endif /* CONFIG_HT_IRQ */
2773 /* --------------------------------------------------------------------------
2774 ACPI-based IOAPIC Configuration
2775 -------------------------------------------------------------------------- */
2779 #define IO_APIC_MAX_ID 0xFE
2781 int __init io_apic_get_redir_entries (int ioapic)
2783 union IO_APIC_reg_01 reg_01;
2784 unsigned long flags;
2786 spin_lock_irqsave(&ioapic_lock, flags);
2787 reg_01.raw = io_apic_read(ioapic, 1);
2788 spin_unlock_irqrestore(&ioapic_lock, flags);
2790 return reg_01.bits.entries;
2794 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2796 if (!IO_APIC_IRQ(irq)) {
2797 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2803 * IRQs < 16 are already in the irq_2_pin[] map
2806 add_pin_to_irq(irq, ioapic, pin);
2808 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
2814 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2818 if (skip_ioapic_setup)
2821 for (i = 0; i < mp_irq_entries; i++)
2822 if (mp_irqs[i].mp_irqtype == mp_INT &&
2823 mp_irqs[i].mp_srcbusirq == bus_irq)
2825 if (i >= mp_irq_entries)
2828 *trigger = irq_trigger(i);
2829 *polarity = irq_polarity(i);
2833 #endif /* CONFIG_ACPI */
2836 * This function currently is only a helper for the i386 smp boot process where
2837 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2838 * so mask in all cases should simply be TARGET_CPUS
2841 void __init setup_ioapic_dest(void)
2843 int pin, ioapic, irq, irq_entry;
2845 if (skip_ioapic_setup == 1)
2848 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2849 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2850 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2851 if (irq_entry == -1)
2853 irq = pin_2_irq(irq_entry, ioapic, pin);
2855 /* setup_IO_APIC_irqs could fail to get vector for some device
2856 * when you have too many devices, because at that time only boot
2859 if (!irq_cfg[irq].vector)
2860 setup_IO_APIC_irq(ioapic, pin, irq,
2861 irq_trigger(irq_entry),
2862 irq_polarity(irq_entry));
2863 #ifdef CONFIG_INTR_REMAP
2864 else if (intr_remapping_enabled)
2865 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
2868 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2875 #define IOAPIC_RESOURCE_NAME_SIZE 11
2877 static struct resource *ioapic_resources;
2879 static struct resource * __init ioapic_setup_resources(void)
2882 struct resource *res;
2886 if (nr_ioapics <= 0)
2889 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
2892 mem = alloc_bootmem(n);
2896 mem += sizeof(struct resource) * nr_ioapics;
2898 for (i = 0; i < nr_ioapics; i++) {
2900 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
2901 sprintf(mem, "IOAPIC %u", i);
2902 mem += IOAPIC_RESOURCE_NAME_SIZE;
2906 ioapic_resources = res;
2911 void __init ioapic_init_mappings(void)
2913 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2914 struct resource *ioapic_res;
2917 ioapic_res = ioapic_setup_resources();
2918 for (i = 0; i < nr_ioapics; i++) {
2919 if (smp_found_config) {
2920 ioapic_phys = mp_ioapics[i].mp_apicaddr;
2922 ioapic_phys = (unsigned long)
2923 alloc_bootmem_pages(PAGE_SIZE);
2924 ioapic_phys = __pa(ioapic_phys);
2926 set_fixmap_nocache(idx, ioapic_phys);
2927 apic_printk(APIC_VERBOSE,
2928 "mapped IOAPIC to %016lx (%016lx)\n",
2929 __fix_to_virt(idx), ioapic_phys);
2932 if (ioapic_res != NULL) {
2933 ioapic_res->start = ioapic_phys;
2934 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
2940 static int __init ioapic_insert_resources(void)
2943 struct resource *r = ioapic_resources;
2947 "IO APIC resources could be not be allocated.\n");
2951 for (i = 0; i < nr_ioapics; i++) {
2952 insert_resource(&iomem_resource, r);
2959 /* Insert the IO APIC resources after PCI initialization has occured to handle
2960 * IO APICS that are mapped in on a BAR in PCI space. */
2961 late_initcall(ioapic_insert_resources);