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>
58 #define __apicdebuginit(type) static type __init
63 unsigned move_cleanup_count;
65 u8 move_in_progress : 1;
68 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
69 static struct irq_cfg irq_cfg_legacy[] __initdata = {
70 [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
71 [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
72 [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
73 [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
74 [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
75 [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
76 [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
77 [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
78 [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
79 [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
80 [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
81 [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
82 [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
83 [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
84 [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
85 [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
88 static struct irq_cfg *irq_cfg;
90 static void __init init_work(void *data)
92 struct dyn_array *da = data;
94 memcpy(*da->name, irq_cfg_legacy, sizeof(irq_cfg_legacy));
97 DEFINE_DYN_ARRAY(irq_cfg, sizeof(struct irq_cfg), nr_irqs, PAGE_SIZE, init_work);
99 static int assign_irq_vector(int irq, cpumask_t mask);
101 int first_system_vector = 0xfe;
103 char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
105 int sis_apic_bug; /* not actually supported, dummy for compile */
107 static int no_timer_check;
109 static int disable_timer_pin_1 __initdata;
111 int timer_through_8259 __initdata;
113 /* Where if anywhere is the i8259 connect in external int mode */
114 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
116 static DEFINE_SPINLOCK(ioapic_lock);
117 static DEFINE_SPINLOCK(vector_lock);
120 * # of IRQ routing registers
122 int nr_ioapic_registers[MAX_IO_APICS];
124 /* I/O APIC RTE contents at the OS boot up */
125 struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
127 /* I/O APIC entries */
128 struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
131 /* MP IRQ source entries */
132 struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
134 /* # of MP IRQ source entries */
137 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
140 * Rough estimation of how many shared IRQs there are, can
141 * be changed anytime.
147 * This is performance-critical, we want to do it O(1)
149 * the indexing order of this array favors 1:1 mappings
150 * between pins and IRQs.
153 static struct irq_pin_list {
158 DEFINE_DYN_ARRAY(irq_2_pin, sizeof(struct irq_pin_list), pin_map_size, sizeof(struct irq_pin_list), NULL);
163 unsigned int unused[3];
167 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
169 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
170 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
173 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
175 struct io_apic __iomem *io_apic = io_apic_base(apic);
176 writel(reg, &io_apic->index);
177 return readl(&io_apic->data);
180 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
182 struct io_apic __iomem *io_apic = io_apic_base(apic);
183 writel(reg, &io_apic->index);
184 writel(value, &io_apic->data);
188 * Re-write a value: to be used for read-modify-write
189 * cycles where the read already set up the index register.
191 static inline void io_apic_modify(unsigned int apic, unsigned int value)
193 struct io_apic __iomem *io_apic = io_apic_base(apic);
194 writel(value, &io_apic->data);
197 static bool io_apic_level_ack_pending(unsigned int irq)
199 struct irq_pin_list *entry;
202 spin_lock_irqsave(&ioapic_lock, flags);
203 entry = irq_2_pin + irq;
211 reg = io_apic_read(entry->apic, 0x10 + pin*2);
212 /* Is the remote IRR bit set? */
213 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
214 spin_unlock_irqrestore(&ioapic_lock, flags);
219 entry = irq_2_pin + entry->next;
221 spin_unlock_irqrestore(&ioapic_lock, flags);
227 * Synchronize the IO-APIC and the CPU by doing
228 * a dummy read from the IO-APIC
230 static inline void io_apic_sync(unsigned int apic)
232 struct io_apic __iomem *io_apic = io_apic_base(apic);
233 readl(&io_apic->data);
236 #define __DO_ACTION(R, ACTION, FINAL) \
240 struct irq_pin_list *entry = irq_2_pin + irq; \
242 BUG_ON(irq >= nr_irqs); \
248 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
250 io_apic_modify(entry->apic, reg); \
254 entry = irq_2_pin + entry->next; \
259 struct { u32 w1, w2; };
260 struct IO_APIC_route_entry entry;
263 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
265 union entry_union eu;
267 spin_lock_irqsave(&ioapic_lock, flags);
268 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
269 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
270 spin_unlock_irqrestore(&ioapic_lock, flags);
275 * When we write a new IO APIC routing entry, we need to write the high
276 * word first! If the mask bit in the low word is clear, we will enable
277 * the interrupt, and we need to make sure the entry is fully populated
278 * before that happens.
281 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
283 union entry_union eu;
285 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
286 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
289 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
292 spin_lock_irqsave(&ioapic_lock, flags);
293 __ioapic_write_entry(apic, pin, e);
294 spin_unlock_irqrestore(&ioapic_lock, flags);
298 * When we mask an IO APIC routing entry, we need to write the low
299 * word first, in order to set the mask bit before we change the
302 static void ioapic_mask_entry(int apic, int pin)
305 union entry_union eu = { .entry.mask = 1 };
307 spin_lock_irqsave(&ioapic_lock, flags);
308 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
309 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
310 spin_unlock_irqrestore(&ioapic_lock, flags);
314 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
317 struct irq_pin_list *entry = irq_2_pin + irq;
319 BUG_ON(irq >= nr_irqs);
327 * With interrupt-remapping, destination information comes
328 * from interrupt-remapping table entry.
330 if (!irq_remapped(irq))
331 io_apic_write(apic, 0x11 + pin*2, dest);
332 reg = io_apic_read(apic, 0x10 + pin*2);
333 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
335 io_apic_modify(apic, reg);
338 entry = irq_2_pin + entry->next;
342 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
344 struct irq_cfg *cfg = irq_cfg + irq;
349 cpus_and(tmp, mask, cpu_online_map);
353 if (assign_irq_vector(irq, mask))
356 cpus_and(tmp, cfg->domain, mask);
357 dest = cpu_mask_to_apicid(tmp);
360 * Only the high 8 bits are valid.
362 dest = SET_APIC_LOGICAL_ID(dest);
364 spin_lock_irqsave(&ioapic_lock, flags);
365 __target_IO_APIC_irq(irq, dest, cfg->vector);
366 irq_desc[irq].affinity = mask;
367 spin_unlock_irqrestore(&ioapic_lock, flags);
372 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
373 * shared ISA-space IRQs, so we have to support them. We are super
374 * fast in the common case, and fast for shared ISA-space IRQs.
376 int first_free_entry;
377 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
379 struct irq_pin_list *entry = irq_2_pin + irq;
381 BUG_ON(irq >= nr_irqs);
383 entry = irq_2_pin + entry->next;
385 if (entry->pin != -1) {
386 entry->next = first_free_entry;
387 entry = irq_2_pin + entry->next;
388 if (++first_free_entry >= pin_map_size)
389 panic("io_apic.c: ran out of irq_2_pin entries!");
396 * Reroute an IRQ to a different pin.
398 static void __init replace_pin_at_irq(unsigned int irq,
399 int oldapic, int oldpin,
400 int newapic, int newpin)
402 struct irq_pin_list *entry = irq_2_pin + irq;
405 if (entry->apic == oldapic && entry->pin == oldpin) {
406 entry->apic = newapic;
411 entry = irq_2_pin + entry->next;
416 #define DO_ACTION(name,R,ACTION, FINAL) \
418 static void name##_IO_APIC_irq (unsigned int irq) \
419 __DO_ACTION(R, ACTION, FINAL)
422 DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, io_apic_sync(entry->apic))
425 DO_ACTION(__unmask, 0, &= ~IO_APIC_REDIR_MASKED, )
427 static void mask_IO_APIC_irq (unsigned int irq)
431 spin_lock_irqsave(&ioapic_lock, flags);
432 __mask_IO_APIC_irq(irq);
433 spin_unlock_irqrestore(&ioapic_lock, flags);
436 static void unmask_IO_APIC_irq (unsigned int irq)
440 spin_lock_irqsave(&ioapic_lock, flags);
441 __unmask_IO_APIC_irq(irq);
442 spin_unlock_irqrestore(&ioapic_lock, flags);
445 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
447 struct IO_APIC_route_entry entry;
449 /* Check delivery_mode to be sure we're not clearing an SMI pin */
450 entry = ioapic_read_entry(apic, pin);
451 if (entry.delivery_mode == dest_SMI)
454 * Disable it in the IO-APIC irq-routing table:
456 ioapic_mask_entry(apic, pin);
459 static void clear_IO_APIC (void)
463 for (apic = 0; apic < nr_ioapics; apic++)
464 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
465 clear_IO_APIC_pin(apic, pin);
469 * Saves and masks all the unmasked IO-APIC RTE's
471 int save_mask_IO_APIC_setup(void)
473 union IO_APIC_reg_01 reg_01;
478 * The number of IO-APIC IRQ registers (== #pins):
480 for (apic = 0; apic < nr_ioapics; apic++) {
481 spin_lock_irqsave(&ioapic_lock, flags);
482 reg_01.raw = io_apic_read(apic, 1);
483 spin_unlock_irqrestore(&ioapic_lock, flags);
484 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
487 for (apic = 0; apic < nr_ioapics; apic++) {
488 early_ioapic_entries[apic] =
489 kzalloc(sizeof(struct IO_APIC_route_entry) *
490 nr_ioapic_registers[apic], GFP_KERNEL);
491 if (!early_ioapic_entries[apic])
495 for (apic = 0; apic < nr_ioapics; apic++)
496 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
497 struct IO_APIC_route_entry entry;
499 entry = early_ioapic_entries[apic][pin] =
500 ioapic_read_entry(apic, pin);
503 ioapic_write_entry(apic, pin, entry);
509 void restore_IO_APIC_setup(void)
513 for (apic = 0; apic < nr_ioapics; apic++)
514 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
515 ioapic_write_entry(apic, pin,
516 early_ioapic_entries[apic][pin]);
519 void reinit_intr_remapped_IO_APIC(int intr_remapping)
522 * for now plain restore of previous settings.
523 * TBD: In the case of OS enabling interrupt-remapping,
524 * IO-APIC RTE's need to be setup to point to interrupt-remapping
525 * table entries. for now, do a plain restore, and wait for
526 * the setup_IO_APIC_irqs() to do proper initialization.
528 restore_IO_APIC_setup();
531 int skip_ioapic_setup;
534 static int __init parse_noapic(char *str)
536 disable_ioapic_setup();
539 early_param("noapic", parse_noapic);
541 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
542 static int __init disable_timer_pin_setup(char *arg)
544 disable_timer_pin_1 = 1;
547 __setup("disable_timer_pin_1", disable_timer_pin_setup);
551 * Find the IRQ entry number of a certain pin.
553 static int find_irq_entry(int apic, int pin, int type)
557 for (i = 0; i < mp_irq_entries; i++)
558 if (mp_irqs[i].mp_irqtype == type &&
559 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
560 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
561 mp_irqs[i].mp_dstirq == pin)
568 * Find the pin to which IRQ[irq] (ISA) is connected
570 static int __init find_isa_irq_pin(int irq, int type)
574 for (i = 0; i < mp_irq_entries; i++) {
575 int lbus = mp_irqs[i].mp_srcbus;
577 if (test_bit(lbus, mp_bus_not_pci) &&
578 (mp_irqs[i].mp_irqtype == type) &&
579 (mp_irqs[i].mp_srcbusirq == irq))
581 return mp_irqs[i].mp_dstirq;
586 static int __init find_isa_irq_apic(int irq, int type)
590 for (i = 0; i < mp_irq_entries; i++) {
591 int lbus = mp_irqs[i].mp_srcbus;
593 if (test_bit(lbus, mp_bus_not_pci) &&
594 (mp_irqs[i].mp_irqtype == type) &&
595 (mp_irqs[i].mp_srcbusirq == irq))
598 if (i < mp_irq_entries) {
600 for(apic = 0; apic < nr_ioapics; apic++) {
601 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
610 * Find a specific PCI IRQ entry.
611 * Not an __init, possibly needed by modules
613 static int pin_2_irq(int idx, int apic, int pin);
615 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
617 int apic, i, best_guess = -1;
619 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
621 if (test_bit(bus, mp_bus_not_pci)) {
622 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
625 for (i = 0; i < mp_irq_entries; i++) {
626 int lbus = mp_irqs[i].mp_srcbus;
628 for (apic = 0; apic < nr_ioapics; apic++)
629 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
630 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
633 if (!test_bit(lbus, mp_bus_not_pci) &&
634 !mp_irqs[i].mp_irqtype &&
636 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
637 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
639 if (!(apic || IO_APIC_IRQ(irq)))
642 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
645 * Use the first all-but-pin matching entry as a
646 * best-guess fuzzy result for broken mptables.
652 BUG_ON(best_guess >= nr_irqs);
656 /* ISA interrupts are always polarity zero edge triggered,
657 * when listed as conforming in the MP table. */
659 #define default_ISA_trigger(idx) (0)
660 #define default_ISA_polarity(idx) (0)
662 /* PCI interrupts are always polarity one level triggered,
663 * when listed as conforming in the MP table. */
665 #define default_PCI_trigger(idx) (1)
666 #define default_PCI_polarity(idx) (1)
668 static int MPBIOS_polarity(int idx)
670 int bus = mp_irqs[idx].mp_srcbus;
674 * Determine IRQ line polarity (high active or low active):
676 switch (mp_irqs[idx].mp_irqflag & 3)
678 case 0: /* conforms, ie. bus-type dependent polarity */
679 if (test_bit(bus, mp_bus_not_pci))
680 polarity = default_ISA_polarity(idx);
682 polarity = default_PCI_polarity(idx);
684 case 1: /* high active */
689 case 2: /* reserved */
691 printk(KERN_WARNING "broken BIOS!!\n");
695 case 3: /* low active */
700 default: /* invalid */
702 printk(KERN_WARNING "broken BIOS!!\n");
710 static int MPBIOS_trigger(int idx)
712 int bus = mp_irqs[idx].mp_srcbus;
716 * Determine IRQ trigger mode (edge or level sensitive):
718 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
720 case 0: /* conforms, ie. bus-type dependent */
721 if (test_bit(bus, mp_bus_not_pci))
722 trigger = default_ISA_trigger(idx);
724 trigger = default_PCI_trigger(idx);
731 case 2: /* reserved */
733 printk(KERN_WARNING "broken BIOS!!\n");
742 default: /* invalid */
744 printk(KERN_WARNING "broken BIOS!!\n");
752 static inline int irq_polarity(int idx)
754 return MPBIOS_polarity(idx);
757 static inline int irq_trigger(int idx)
759 return MPBIOS_trigger(idx);
762 static int pin_2_irq(int idx, int apic, int pin)
765 int bus = mp_irqs[idx].mp_srcbus;
768 * Debugging check, we are in big trouble if this message pops up!
770 if (mp_irqs[idx].mp_dstirq != pin)
771 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
773 if (test_bit(bus, mp_bus_not_pci)) {
774 irq = mp_irqs[idx].mp_srcbusirq;
777 * PCI IRQs are mapped in order
781 irq += nr_ioapic_registers[i++];
784 BUG_ON(irq >= nr_irqs);
788 void lock_vector_lock(void)
790 /* Used to the online set of cpus does not change
791 * during assign_irq_vector.
793 spin_lock(&vector_lock);
796 void unlock_vector_lock(void)
798 spin_unlock(&vector_lock);
801 static int __assign_irq_vector(int irq, cpumask_t mask)
804 * NOTE! The local APIC isn't very good at handling
805 * multiple interrupts at the same interrupt level.
806 * As the interrupt level is determined by taking the
807 * vector number and shifting that right by 4, we
808 * want to spread these out a bit so that they don't
809 * all fall in the same interrupt level.
811 * Also, we've got to be careful not to trash gate
812 * 0x80, because int 0x80 is hm, kind of importantish. ;)
814 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
815 unsigned int old_vector;
819 BUG_ON((unsigned)irq >= nr_irqs);
822 /* Only try and allocate irqs on cpus that are present */
823 cpus_and(mask, mask, cpu_online_map);
825 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
828 old_vector = cfg->vector;
831 cpus_and(tmp, cfg->domain, mask);
832 if (!cpus_empty(tmp))
836 for_each_cpu_mask_nr(cpu, mask) {
837 cpumask_t domain, new_mask;
841 domain = vector_allocation_domain(cpu);
842 cpus_and(new_mask, domain, cpu_online_map);
844 vector = current_vector;
845 offset = current_offset;
848 if (vector >= first_system_vector) {
849 /* If we run out of vectors on large boxen, must share them. */
850 offset = (offset + 1) % 8;
851 vector = FIRST_DEVICE_VECTOR + offset;
853 if (unlikely(current_vector == vector))
855 if (vector == IA32_SYSCALL_VECTOR)
857 for_each_cpu_mask_nr(new_cpu, new_mask)
858 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
861 current_vector = vector;
862 current_offset = offset;
864 cfg->move_in_progress = 1;
865 cfg->old_domain = cfg->domain;
867 for_each_cpu_mask_nr(new_cpu, new_mask)
868 per_cpu(vector_irq, new_cpu)[vector] = irq;
869 cfg->vector = vector;
870 cfg->domain = domain;
876 static int assign_irq_vector(int irq, cpumask_t mask)
881 spin_lock_irqsave(&vector_lock, flags);
882 err = __assign_irq_vector(irq, mask);
883 spin_unlock_irqrestore(&vector_lock, flags);
887 static void __clear_irq_vector(int irq)
893 BUG_ON((unsigned)irq >= nr_irqs);
895 BUG_ON(!cfg->vector);
897 vector = cfg->vector;
898 cpus_and(mask, cfg->domain, cpu_online_map);
899 for_each_cpu_mask_nr(cpu, mask)
900 per_cpu(vector_irq, cpu)[vector] = -1;
903 cpus_clear(cfg->domain);
906 void __setup_vector_irq(int cpu)
908 /* Initialize vector_irq on a new cpu */
909 /* This function must be called with vector_lock held */
912 /* Mark the inuse vectors */
913 for (irq = 0; irq < nr_irqs; ++irq) {
914 if (!cpu_isset(cpu, irq_cfg[irq].domain))
916 vector = irq_cfg[irq].vector;
917 per_cpu(vector_irq, cpu)[vector] = irq;
919 /* Mark the free vectors */
920 for (vector = 0; vector < NR_VECTORS; ++vector) {
921 irq = per_cpu(vector_irq, cpu)[vector];
924 if (!cpu_isset(cpu, irq_cfg[irq].domain))
925 per_cpu(vector_irq, cpu)[vector] = -1;
929 static struct irq_chip ioapic_chip;
930 #ifdef CONFIG_INTR_REMAP
931 static struct irq_chip ir_ioapic_chip;
934 static void ioapic_register_intr(int irq, unsigned long trigger)
937 irq_desc[irq].status |= IRQ_LEVEL;
939 irq_desc[irq].status &= ~IRQ_LEVEL;
941 #ifdef CONFIG_INTR_REMAP
942 if (irq_remapped(irq)) {
943 irq_desc[irq].status |= IRQ_MOVE_PCNTXT;
945 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
949 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
950 handle_edge_irq, "edge");
955 set_irq_chip_and_handler_name(irq, &ioapic_chip,
959 set_irq_chip_and_handler_name(irq, &ioapic_chip,
960 handle_edge_irq, "edge");
963 static int setup_ioapic_entry(int apic, int irq,
964 struct IO_APIC_route_entry *entry,
965 unsigned int destination, int trigger,
966 int polarity, int vector)
969 * add it to the IO-APIC irq-routing table:
971 memset(entry,0,sizeof(*entry));
973 #ifdef CONFIG_INTR_REMAP
974 if (intr_remapping_enabled) {
975 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
977 struct IR_IO_APIC_route_entry *ir_entry =
978 (struct IR_IO_APIC_route_entry *) entry;
982 panic("No mapping iommu for ioapic %d\n", apic);
984 index = alloc_irte(iommu, irq, 1);
986 panic("Failed to allocate IRTE for ioapic %d\n", apic);
988 memset(&irte, 0, sizeof(irte));
991 irte.dst_mode = INT_DEST_MODE;
992 irte.trigger_mode = trigger;
993 irte.dlvry_mode = INT_DELIVERY_MODE;
994 irte.vector = vector;
995 irte.dest_id = IRTE_DEST(destination);
997 modify_irte(irq, &irte);
999 ir_entry->index2 = (index >> 15) & 0x1;
1001 ir_entry->format = 1;
1002 ir_entry->index = (index & 0x7fff);
1006 entry->delivery_mode = INT_DELIVERY_MODE;
1007 entry->dest_mode = INT_DEST_MODE;
1008 entry->dest = destination;
1011 entry->mask = 0; /* enable IRQ */
1012 entry->trigger = trigger;
1013 entry->polarity = polarity;
1014 entry->vector = vector;
1016 /* Mask level triggered irqs.
1017 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1024 static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
1025 int trigger, int polarity)
1027 struct irq_cfg *cfg = irq_cfg + irq;
1028 struct IO_APIC_route_entry entry;
1031 if (!IO_APIC_IRQ(irq))
1035 if (assign_irq_vector(irq, mask))
1038 cpus_and(mask, cfg->domain, mask);
1040 apic_printk(APIC_VERBOSE,KERN_DEBUG
1041 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1042 "IRQ %d Mode:%i Active:%i)\n",
1043 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1044 irq, trigger, polarity);
1047 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1048 cpu_mask_to_apicid(mask), trigger, polarity,
1050 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1051 mp_ioapics[apic].mp_apicid, pin);
1052 __clear_irq_vector(irq);
1056 ioapic_register_intr(irq, trigger);
1058 disable_8259A_irq(irq);
1060 ioapic_write_entry(apic, pin, entry);
1063 static void __init setup_IO_APIC_irqs(void)
1065 int apic, pin, idx, irq, first_notcon = 1;
1067 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1069 for (apic = 0; apic < nr_ioapics; apic++) {
1070 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1072 idx = find_irq_entry(apic,pin,mp_INT);
1075 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
1078 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
1081 if (!first_notcon) {
1082 apic_printk(APIC_VERBOSE, " not connected.\n");
1086 irq = pin_2_irq(idx, apic, pin);
1087 add_pin_to_irq(irq, apic, pin);
1089 setup_IO_APIC_irq(apic, pin, irq,
1090 irq_trigger(idx), irq_polarity(idx));
1095 apic_printk(APIC_VERBOSE, " not connected.\n");
1099 * Set up the timer pin, possibly with the 8259A-master behind.
1101 static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1104 struct IO_APIC_route_entry entry;
1106 if (intr_remapping_enabled)
1109 memset(&entry, 0, sizeof(entry));
1112 * We use logical delivery to get the timer IRQ
1115 entry.dest_mode = INT_DEST_MODE;
1116 entry.mask = 1; /* mask IRQ now */
1117 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
1118 entry.delivery_mode = INT_DELIVERY_MODE;
1121 entry.vector = vector;
1124 * The timer IRQ doesn't have to know that behind the
1125 * scene we may have a 8259A-master in AEOI mode ...
1127 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1130 * Add it to the IO-APIC irq-routing table:
1132 ioapic_write_entry(apic, pin, entry);
1136 __apicdebuginit(void) print_IO_APIC(void)
1139 union IO_APIC_reg_00 reg_00;
1140 union IO_APIC_reg_01 reg_01;
1141 union IO_APIC_reg_02 reg_02;
1142 unsigned long flags;
1144 if (apic_verbosity == APIC_QUIET)
1147 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1148 for (i = 0; i < nr_ioapics; i++)
1149 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1150 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
1153 * We are a bit conservative about what we expect. We have to
1154 * know about every hardware change ASAP.
1156 printk(KERN_INFO "testing the IO APIC.......................\n");
1158 for (apic = 0; apic < nr_ioapics; apic++) {
1160 spin_lock_irqsave(&ioapic_lock, flags);
1161 reg_00.raw = io_apic_read(apic, 0);
1162 reg_01.raw = io_apic_read(apic, 1);
1163 if (reg_01.bits.version >= 0x10)
1164 reg_02.raw = io_apic_read(apic, 2);
1165 spin_unlock_irqrestore(&ioapic_lock, flags);
1168 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
1169 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1170 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1172 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1173 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1175 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1176 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1178 if (reg_01.bits.version >= 0x10) {
1179 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1180 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1183 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1185 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1186 " Stat Dmod Deli Vect: \n");
1188 for (i = 0; i <= reg_01.bits.entries; i++) {
1189 struct IO_APIC_route_entry entry;
1191 entry = ioapic_read_entry(apic, i);
1193 printk(KERN_DEBUG " %02x %03X ",
1198 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1203 entry.delivery_status,
1205 entry.delivery_mode,
1210 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1211 for (i = 0; i < nr_irqs; i++) {
1212 struct irq_pin_list *entry = irq_2_pin + i;
1215 printk(KERN_DEBUG "IRQ%d ", i);
1217 printk("-> %d:%d", entry->apic, entry->pin);
1220 entry = irq_2_pin + entry->next;
1225 printk(KERN_INFO ".................................... done.\n");
1230 __apicdebuginit(void) print_APIC_bitfield(int base)
1235 if (apic_verbosity == APIC_QUIET)
1238 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1239 for (i = 0; i < 8; i++) {
1240 v = apic_read(base + i*0x10);
1241 for (j = 0; j < 32; j++) {
1251 __apicdebuginit(void) print_local_APIC(void *dummy)
1253 unsigned int v, ver, maxlvt;
1256 if (apic_verbosity == APIC_QUIET)
1259 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1260 smp_processor_id(), hard_smp_processor_id());
1261 v = apic_read(APIC_ID);
1262 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
1263 v = apic_read(APIC_LVR);
1264 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1265 ver = GET_APIC_VERSION(v);
1266 maxlvt = lapic_get_maxlvt();
1268 v = apic_read(APIC_TASKPRI);
1269 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1271 v = apic_read(APIC_ARBPRI);
1272 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1273 v & APIC_ARBPRI_MASK);
1274 v = apic_read(APIC_PROCPRI);
1275 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1277 v = apic_read(APIC_EOI);
1278 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1279 v = apic_read(APIC_RRR);
1280 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1281 v = apic_read(APIC_LDR);
1282 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1283 v = apic_read(APIC_DFR);
1284 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1285 v = apic_read(APIC_SPIV);
1286 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1288 printk(KERN_DEBUG "... APIC ISR field:\n");
1289 print_APIC_bitfield(APIC_ISR);
1290 printk(KERN_DEBUG "... APIC TMR field:\n");
1291 print_APIC_bitfield(APIC_TMR);
1292 printk(KERN_DEBUG "... APIC IRR field:\n");
1293 print_APIC_bitfield(APIC_IRR);
1295 v = apic_read(APIC_ESR);
1296 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1298 icr = apic_icr_read();
1299 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1300 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
1302 v = apic_read(APIC_LVTT);
1303 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1305 if (maxlvt > 3) { /* PC is LVT#4. */
1306 v = apic_read(APIC_LVTPC);
1307 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1309 v = apic_read(APIC_LVT0);
1310 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1311 v = apic_read(APIC_LVT1);
1312 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1314 if (maxlvt > 2) { /* ERR is LVT#3. */
1315 v = apic_read(APIC_LVTERR);
1316 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1319 v = apic_read(APIC_TMICT);
1320 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1321 v = apic_read(APIC_TMCCT);
1322 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1323 v = apic_read(APIC_TDCR);
1324 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1328 __apicdebuginit(void) print_all_local_APICs(void)
1330 on_each_cpu(print_local_APIC, NULL, 1);
1333 __apicdebuginit(void) print_PIC(void)
1336 unsigned long flags;
1338 if (apic_verbosity == APIC_QUIET)
1341 printk(KERN_DEBUG "\nprinting PIC contents\n");
1343 spin_lock_irqsave(&i8259A_lock, flags);
1345 v = inb(0xa1) << 8 | inb(0x21);
1346 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1348 v = inb(0xa0) << 8 | inb(0x20);
1349 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1353 v = inb(0xa0) << 8 | inb(0x20);
1357 spin_unlock_irqrestore(&i8259A_lock, flags);
1359 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1361 v = inb(0x4d1) << 8 | inb(0x4d0);
1362 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1365 __apicdebuginit(int) print_all_ICs(void)
1368 print_all_local_APICs();
1374 fs_initcall(print_all_ICs);
1377 void __init enable_IO_APIC(void)
1379 union IO_APIC_reg_01 reg_01;
1380 int i8259_apic, i8259_pin;
1382 unsigned long flags;
1384 for (i = 0; i < pin_map_size; i++) {
1385 irq_2_pin[i].pin = -1;
1386 irq_2_pin[i].next = 0;
1390 * The number of IO-APIC IRQ registers (== #pins):
1392 for (apic = 0; apic < nr_ioapics; apic++) {
1393 spin_lock_irqsave(&ioapic_lock, flags);
1394 reg_01.raw = io_apic_read(apic, 1);
1395 spin_unlock_irqrestore(&ioapic_lock, flags);
1396 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1398 for(apic = 0; apic < nr_ioapics; apic++) {
1400 /* See if any of the pins is in ExtINT mode */
1401 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1402 struct IO_APIC_route_entry entry;
1403 entry = ioapic_read_entry(apic, pin);
1405 /* If the interrupt line is enabled and in ExtInt mode
1406 * I have found the pin where the i8259 is connected.
1408 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1409 ioapic_i8259.apic = apic;
1410 ioapic_i8259.pin = pin;
1416 /* Look to see what if the MP table has reported the ExtINT */
1417 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1418 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1419 /* Trust the MP table if nothing is setup in the hardware */
1420 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1421 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1422 ioapic_i8259.pin = i8259_pin;
1423 ioapic_i8259.apic = i8259_apic;
1425 /* Complain if the MP table and the hardware disagree */
1426 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1427 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1429 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1433 * Do not trust the IO-APIC being empty at bootup
1439 * Not an __init, needed by the reboot code
1441 void disable_IO_APIC(void)
1444 * Clear the IO-APIC before rebooting:
1449 * If the i8259 is routed through an IOAPIC
1450 * Put that IOAPIC in virtual wire mode
1451 * so legacy interrupts can be delivered.
1453 if (ioapic_i8259.pin != -1) {
1454 struct IO_APIC_route_entry entry;
1456 memset(&entry, 0, sizeof(entry));
1457 entry.mask = 0; /* Enabled */
1458 entry.trigger = 0; /* Edge */
1460 entry.polarity = 0; /* High */
1461 entry.delivery_status = 0;
1462 entry.dest_mode = 0; /* Physical */
1463 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1465 entry.dest = read_apic_id();
1468 * Add it to the IO-APIC irq-routing table:
1470 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1473 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1477 * There is a nasty bug in some older SMP boards, their mptable lies
1478 * about the timer IRQ. We do the following to work around the situation:
1480 * - timer IRQ defaults to IO-APIC IRQ
1481 * - if this function detects that timer IRQs are defunct, then we fall
1482 * back to ISA timer IRQs
1484 static int __init timer_irq_works(void)
1486 unsigned long t1 = jiffies;
1487 unsigned long flags;
1489 local_save_flags(flags);
1491 /* Let ten ticks pass... */
1492 mdelay((10 * 1000) / HZ);
1493 local_irq_restore(flags);
1496 * Expect a few ticks at least, to be sure some possible
1497 * glue logic does not lock up after one or two first
1498 * ticks in a non-ExtINT mode. Also the local APIC
1499 * might have cached one ExtINT interrupt. Finally, at
1500 * least one tick may be lost due to delays.
1504 if (time_after(jiffies, t1 + 4))
1510 * In the SMP+IOAPIC case it might happen that there are an unspecified
1511 * number of pending IRQ events unhandled. These cases are very rare,
1512 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1513 * better to do it this way as thus we do not have to be aware of
1514 * 'pending' interrupts in the IRQ path, except at this point.
1517 * Edge triggered needs to resend any interrupt
1518 * that was delayed but this is now handled in the device
1523 * Starting up a edge-triggered IO-APIC interrupt is
1524 * nasty - we need to make sure that we get the edge.
1525 * If it is already asserted for some reason, we need
1526 * return 1 to indicate that is was pending.
1528 * This is not complete - we should be able to fake
1529 * an edge even if it isn't on the 8259A...
1532 static unsigned int startup_ioapic_irq(unsigned int irq)
1534 int was_pending = 0;
1535 unsigned long flags;
1537 spin_lock_irqsave(&ioapic_lock, flags);
1539 disable_8259A_irq(irq);
1540 if (i8259A_irq_pending(irq))
1543 __unmask_IO_APIC_irq(irq);
1544 spin_unlock_irqrestore(&ioapic_lock, flags);
1549 static int ioapic_retrigger_irq(unsigned int irq)
1551 struct irq_cfg *cfg = &irq_cfg[irq];
1552 unsigned long flags;
1554 spin_lock_irqsave(&vector_lock, flags);
1555 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
1556 spin_unlock_irqrestore(&vector_lock, flags);
1562 * Level and edge triggered IO-APIC interrupts need different handling,
1563 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1564 * handled with the level-triggered descriptor, but that one has slightly
1565 * more overhead. Level-triggered interrupts cannot be handled with the
1566 * edge-triggered handler, without risking IRQ storms and other ugly
1572 #ifdef CONFIG_INTR_REMAP
1573 static void ir_irq_migration(struct work_struct *work);
1575 static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
1578 * Migrate the IO-APIC irq in the presence of intr-remapping.
1580 * For edge triggered, irq migration is a simple atomic update(of vector
1581 * and cpu destination) of IRTE and flush the hardware cache.
1583 * For level triggered, we need to modify the io-apic RTE aswell with the update
1584 * vector information, along with modifying IRTE with vector and destination.
1585 * So irq migration for level triggered is little bit more complex compared to
1586 * edge triggered migration. But the good news is, we use the same algorithm
1587 * for level triggered migration as we have today, only difference being,
1588 * we now initiate the irq migration from process context instead of the
1589 * interrupt context.
1591 * In future, when we do a directed EOI (combined with cpu EOI broadcast
1592 * suppression) to the IO-APIC, level triggered irq migration will also be
1593 * as simple as edge triggered migration and we can do the irq migration
1594 * with a simple atomic update to IO-APIC RTE.
1596 static void migrate_ioapic_irq(int irq, cpumask_t mask)
1598 struct irq_cfg *cfg = irq_cfg + irq;
1599 struct irq_desc *desc = irq_desc + irq;
1600 cpumask_t tmp, cleanup_mask;
1602 int modify_ioapic_rte = desc->status & IRQ_LEVEL;
1604 unsigned long flags;
1606 cpus_and(tmp, mask, cpu_online_map);
1607 if (cpus_empty(tmp))
1610 if (get_irte(irq, &irte))
1613 if (assign_irq_vector(irq, mask))
1616 cpus_and(tmp, cfg->domain, mask);
1617 dest = cpu_mask_to_apicid(tmp);
1619 if (modify_ioapic_rte) {
1620 spin_lock_irqsave(&ioapic_lock, flags);
1621 __target_IO_APIC_irq(irq, dest, cfg->vector);
1622 spin_unlock_irqrestore(&ioapic_lock, flags);
1625 irte.vector = cfg->vector;
1626 irte.dest_id = IRTE_DEST(dest);
1629 * Modified the IRTE and flushes the Interrupt entry cache.
1631 modify_irte(irq, &irte);
1633 if (cfg->move_in_progress) {
1634 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1635 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1636 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1637 cfg->move_in_progress = 0;
1640 irq_desc[irq].affinity = mask;
1643 static int migrate_irq_remapped_level(int irq)
1647 mask_IO_APIC_irq(irq);
1649 if (io_apic_level_ack_pending(irq)) {
1651 * Interrupt in progress. Migrating irq now will change the
1652 * vector information in the IO-APIC RTE and that will confuse
1653 * the EOI broadcast performed by cpu.
1654 * So, delay the irq migration to the next instance.
1656 schedule_delayed_work(&ir_migration_work, 1);
1660 /* everthing is clear. we have right of way */
1661 migrate_ioapic_irq(irq, irq_desc[irq].pending_mask);
1664 irq_desc[irq].status &= ~IRQ_MOVE_PENDING;
1665 cpus_clear(irq_desc[irq].pending_mask);
1668 unmask_IO_APIC_irq(irq);
1672 static void ir_irq_migration(struct work_struct *work)
1676 for (irq = 0; irq < nr_irqs; irq++) {
1677 struct irq_desc *desc = irq_desc + irq;
1678 if (desc->status & IRQ_MOVE_PENDING) {
1679 unsigned long flags;
1681 spin_lock_irqsave(&desc->lock, flags);
1682 if (!desc->chip->set_affinity ||
1683 !(desc->status & IRQ_MOVE_PENDING)) {
1684 desc->status &= ~IRQ_MOVE_PENDING;
1685 spin_unlock_irqrestore(&desc->lock, flags);
1689 desc->chip->set_affinity(irq,
1690 irq_desc[irq].pending_mask);
1691 spin_unlock_irqrestore(&desc->lock, flags);
1697 * Migrates the IRQ destination in the process context.
1699 static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
1701 if (irq_desc[irq].status & IRQ_LEVEL) {
1702 irq_desc[irq].status |= IRQ_MOVE_PENDING;
1703 irq_desc[irq].pending_mask = mask;
1704 migrate_irq_remapped_level(irq);
1708 migrate_ioapic_irq(irq, mask);
1712 asmlinkage void smp_irq_move_cleanup_interrupt(void)
1714 unsigned vector, me;
1719 me = smp_processor_id();
1720 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1722 struct irq_desc *desc;
1723 struct irq_cfg *cfg;
1724 irq = __get_cpu_var(vector_irq)[vector];
1728 desc = irq_desc + irq;
1729 cfg = irq_cfg + irq;
1730 spin_lock(&desc->lock);
1731 if (!cfg->move_cleanup_count)
1734 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
1737 __get_cpu_var(vector_irq)[vector] = -1;
1738 cfg->move_cleanup_count--;
1740 spin_unlock(&desc->lock);
1746 static void irq_complete_move(unsigned int irq)
1748 struct irq_cfg *cfg = irq_cfg + irq;
1749 unsigned vector, me;
1751 if (likely(!cfg->move_in_progress))
1754 vector = ~get_irq_regs()->orig_ax;
1755 me = smp_processor_id();
1756 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
1757 cpumask_t cleanup_mask;
1759 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1760 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1761 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1762 cfg->move_in_progress = 0;
1766 static inline void irq_complete_move(unsigned int irq) {}
1768 #ifdef CONFIG_INTR_REMAP
1769 static void ack_x2apic_level(unsigned int irq)
1774 static void ack_x2apic_edge(unsigned int irq)
1780 static void ack_apic_edge(unsigned int irq)
1782 irq_complete_move(irq);
1783 move_native_irq(irq);
1787 static void ack_apic_level(unsigned int irq)
1789 int do_unmask_irq = 0;
1791 irq_complete_move(irq);
1792 #ifdef CONFIG_GENERIC_PENDING_IRQ
1793 /* If we are moving the irq we need to mask it */
1794 if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1796 mask_IO_APIC_irq(irq);
1801 * We must acknowledge the irq before we move it or the acknowledge will
1802 * not propagate properly.
1806 /* Now we can move and renable the irq */
1807 if (unlikely(do_unmask_irq)) {
1808 /* Only migrate the irq if the ack has been received.
1810 * On rare occasions the broadcast level triggered ack gets
1811 * delayed going to ioapics, and if we reprogram the
1812 * vector while Remote IRR is still set the irq will never
1815 * To prevent this scenario we read the Remote IRR bit
1816 * of the ioapic. This has two effects.
1817 * - On any sane system the read of the ioapic will
1818 * flush writes (and acks) going to the ioapic from
1820 * - We get to see if the ACK has actually been delivered.
1822 * Based on failed experiments of reprogramming the
1823 * ioapic entry from outside of irq context starting
1824 * with masking the ioapic entry and then polling until
1825 * Remote IRR was clear before reprogramming the
1826 * ioapic I don't trust the Remote IRR bit to be
1827 * completey accurate.
1829 * However there appears to be no other way to plug
1830 * this race, so if the Remote IRR bit is not
1831 * accurate and is causing problems then it is a hardware bug
1832 * and you can go talk to the chipset vendor about it.
1834 if (!io_apic_level_ack_pending(irq))
1835 move_masked_irq(irq);
1836 unmask_IO_APIC_irq(irq);
1840 static struct irq_chip ioapic_chip __read_mostly = {
1842 .startup = startup_ioapic_irq,
1843 .mask = mask_IO_APIC_irq,
1844 .unmask = unmask_IO_APIC_irq,
1845 .ack = ack_apic_edge,
1846 .eoi = ack_apic_level,
1848 .set_affinity = set_ioapic_affinity_irq,
1850 .retrigger = ioapic_retrigger_irq,
1853 #ifdef CONFIG_INTR_REMAP
1854 static struct irq_chip ir_ioapic_chip __read_mostly = {
1855 .name = "IR-IO-APIC",
1856 .startup = startup_ioapic_irq,
1857 .mask = mask_IO_APIC_irq,
1858 .unmask = unmask_IO_APIC_irq,
1859 .ack = ack_x2apic_edge,
1860 .eoi = ack_x2apic_level,
1862 .set_affinity = set_ir_ioapic_affinity_irq,
1864 .retrigger = ioapic_retrigger_irq,
1868 static inline void init_IO_APIC_traps(void)
1873 * NOTE! The local APIC isn't very good at handling
1874 * multiple interrupts at the same interrupt level.
1875 * As the interrupt level is determined by taking the
1876 * vector number and shifting that right by 4, we
1877 * want to spread these out a bit so that they don't
1878 * all fall in the same interrupt level.
1880 * Also, we've got to be careful not to trash gate
1881 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1883 for (irq = 0; irq < nr_irqs ; irq++) {
1884 if (IO_APIC_IRQ(irq) && !irq_cfg[irq].vector) {
1886 * Hmm.. We don't have an entry for this,
1887 * so default to an old-fashioned 8259
1888 * interrupt if we can..
1891 make_8259A_irq(irq);
1893 /* Strange. Oh, well.. */
1894 irq_desc[irq].chip = &no_irq_chip;
1899 static void unmask_lapic_irq(unsigned int irq)
1903 v = apic_read(APIC_LVT0);
1904 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1907 static void mask_lapic_irq(unsigned int irq)
1911 v = apic_read(APIC_LVT0);
1912 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1915 static void ack_lapic_irq (unsigned int irq)
1920 static struct irq_chip lapic_chip __read_mostly = {
1921 .name = "local-APIC",
1922 .mask = mask_lapic_irq,
1923 .unmask = unmask_lapic_irq,
1924 .ack = ack_lapic_irq,
1927 static void lapic_register_intr(int irq)
1929 irq_desc[irq].status &= ~IRQ_LEVEL;
1930 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
1934 static void __init setup_nmi(void)
1937 * Dirty trick to enable the NMI watchdog ...
1938 * We put the 8259A master into AEOI mode and
1939 * unmask on all local APICs LVT0 as NMI.
1941 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1942 * is from Maciej W. Rozycki - so we do not have to EOI from
1943 * the NMI handler or the timer interrupt.
1945 printk(KERN_INFO "activating NMI Watchdog ...");
1947 enable_NMI_through_LVT0();
1953 * This looks a bit hackish but it's about the only one way of sending
1954 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1955 * not support the ExtINT mode, unfortunately. We need to send these
1956 * cycles as some i82489DX-based boards have glue logic that keeps the
1957 * 8259A interrupt line asserted until INTA. --macro
1959 static inline void __init unlock_ExtINT_logic(void)
1962 struct IO_APIC_route_entry entry0, entry1;
1963 unsigned char save_control, save_freq_select;
1965 pin = find_isa_irq_pin(8, mp_INT);
1966 apic = find_isa_irq_apic(8, mp_INT);
1970 entry0 = ioapic_read_entry(apic, pin);
1972 clear_IO_APIC_pin(apic, pin);
1974 memset(&entry1, 0, sizeof(entry1));
1976 entry1.dest_mode = 0; /* physical delivery */
1977 entry1.mask = 0; /* unmask IRQ now */
1978 entry1.dest = hard_smp_processor_id();
1979 entry1.delivery_mode = dest_ExtINT;
1980 entry1.polarity = entry0.polarity;
1984 ioapic_write_entry(apic, pin, entry1);
1986 save_control = CMOS_READ(RTC_CONTROL);
1987 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1988 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1990 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1995 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1999 CMOS_WRITE(save_control, RTC_CONTROL);
2000 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2001 clear_IO_APIC_pin(apic, pin);
2003 ioapic_write_entry(apic, pin, entry0);
2007 * This code may look a bit paranoid, but it's supposed to cooperate with
2008 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2009 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2010 * fanatically on his truly buggy board.
2012 * FIXME: really need to revamp this for modern platforms only.
2014 static inline void __init check_timer(void)
2016 struct irq_cfg *cfg = irq_cfg + 0;
2017 int apic1, pin1, apic2, pin2;
2018 unsigned long flags;
2021 local_irq_save(flags);
2024 * get/set the timer IRQ vector:
2026 disable_8259A_irq(0);
2027 assign_irq_vector(0, TARGET_CPUS);
2030 * As IRQ0 is to be enabled in the 8259A, the virtual
2031 * wire has to be disabled in the local APIC.
2033 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2036 pin1 = find_isa_irq_pin(0, mp_INT);
2037 apic1 = find_isa_irq_apic(0, mp_INT);
2038 pin2 = ioapic_i8259.pin;
2039 apic2 = ioapic_i8259.apic;
2041 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2042 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2043 cfg->vector, apic1, pin1, apic2, pin2);
2046 * Some BIOS writers are clueless and report the ExtINTA
2047 * I/O APIC input from the cascaded 8259A as the timer
2048 * interrupt input. So just in case, if only one pin
2049 * was found above, try it both directly and through the
2053 if (intr_remapping_enabled)
2054 panic("BIOS bug: timer not connected to IO-APIC");
2058 } else if (pin2 == -1) {
2065 * Ok, does IRQ0 through the IOAPIC work?
2068 add_pin_to_irq(0, apic1, pin1);
2069 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2071 unmask_IO_APIC_irq(0);
2072 if (!no_timer_check && timer_irq_works()) {
2073 if (nmi_watchdog == NMI_IO_APIC) {
2075 enable_8259A_irq(0);
2077 if (disable_timer_pin_1 > 0)
2078 clear_IO_APIC_pin(0, pin1);
2081 if (intr_remapping_enabled)
2082 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2083 clear_IO_APIC_pin(apic1, pin1);
2085 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2086 "8254 timer not connected to IO-APIC\n");
2088 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2089 "(IRQ0) through the 8259A ...\n");
2090 apic_printk(APIC_QUIET, KERN_INFO
2091 "..... (found apic %d pin %d) ...\n", apic2, pin2);
2093 * legacy devices should be connected to IO APIC #0
2095 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
2096 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2097 unmask_IO_APIC_irq(0);
2098 enable_8259A_irq(0);
2099 if (timer_irq_works()) {
2100 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2101 timer_through_8259 = 1;
2102 if (nmi_watchdog == NMI_IO_APIC) {
2103 disable_8259A_irq(0);
2105 enable_8259A_irq(0);
2110 * Cleanup, just in case ...
2112 disable_8259A_irq(0);
2113 clear_IO_APIC_pin(apic2, pin2);
2114 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2117 if (nmi_watchdog == NMI_IO_APIC) {
2118 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2119 "through the IO-APIC - disabling NMI Watchdog!\n");
2120 nmi_watchdog = NMI_NONE;
2123 apic_printk(APIC_QUIET, KERN_INFO
2124 "...trying to set up timer as Virtual Wire IRQ...\n");
2126 lapic_register_intr(0);
2127 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
2128 enable_8259A_irq(0);
2130 if (timer_irq_works()) {
2131 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2134 disable_8259A_irq(0);
2135 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2136 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2138 apic_printk(APIC_QUIET, KERN_INFO
2139 "...trying to set up timer as ExtINT IRQ...\n");
2143 apic_write(APIC_LVT0, APIC_DM_EXTINT);
2145 unlock_ExtINT_logic();
2147 if (timer_irq_works()) {
2148 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2151 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2152 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2153 "report. Then try booting with the 'noapic' option.\n");
2155 local_irq_restore(flags);
2158 static int __init notimercheck(char *s)
2163 __setup("no_timer_check", notimercheck);
2166 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2167 * to devices. However there may be an I/O APIC pin available for
2168 * this interrupt regardless. The pin may be left unconnected, but
2169 * typically it will be reused as an ExtINT cascade interrupt for
2170 * the master 8259A. In the MPS case such a pin will normally be
2171 * reported as an ExtINT interrupt in the MP table. With ACPI
2172 * there is no provision for ExtINT interrupts, and in the absence
2173 * of an override it would be treated as an ordinary ISA I/O APIC
2174 * interrupt, that is edge-triggered and unmasked by default. We
2175 * used to do this, but it caused problems on some systems because
2176 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2177 * the same ExtINT cascade interrupt to drive the local APIC of the
2178 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2179 * the I/O APIC in all cases now. No actual device should request
2180 * it anyway. --macro
2182 #define PIC_IRQS (1<<2)
2184 void __init setup_IO_APIC(void)
2188 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2191 io_apic_irqs = ~PIC_IRQS;
2193 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2196 setup_IO_APIC_irqs();
2197 init_IO_APIC_traps();
2201 struct sysfs_ioapic_data {
2202 struct sys_device dev;
2203 struct IO_APIC_route_entry entry[0];
2205 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2207 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2209 struct IO_APIC_route_entry *entry;
2210 struct sysfs_ioapic_data *data;
2213 data = container_of(dev, struct sysfs_ioapic_data, dev);
2214 entry = data->entry;
2215 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2216 *entry = ioapic_read_entry(dev->id, i);
2221 static int ioapic_resume(struct sys_device *dev)
2223 struct IO_APIC_route_entry *entry;
2224 struct sysfs_ioapic_data *data;
2225 unsigned long flags;
2226 union IO_APIC_reg_00 reg_00;
2229 data = container_of(dev, struct sysfs_ioapic_data, dev);
2230 entry = data->entry;
2232 spin_lock_irqsave(&ioapic_lock, flags);
2233 reg_00.raw = io_apic_read(dev->id, 0);
2234 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2235 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
2236 io_apic_write(dev->id, 0, reg_00.raw);
2238 spin_unlock_irqrestore(&ioapic_lock, flags);
2239 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
2240 ioapic_write_entry(dev->id, i, entry[i]);
2245 static struct sysdev_class ioapic_sysdev_class = {
2247 .suspend = ioapic_suspend,
2248 .resume = ioapic_resume,
2251 static int __init ioapic_init_sysfs(void)
2253 struct sys_device * dev;
2256 error = sysdev_class_register(&ioapic_sysdev_class);
2260 for (i = 0; i < nr_ioapics; i++ ) {
2261 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2262 * sizeof(struct IO_APIC_route_entry);
2263 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
2264 if (!mp_ioapic_data[i]) {
2265 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2268 dev = &mp_ioapic_data[i]->dev;
2270 dev->cls = &ioapic_sysdev_class;
2271 error = sysdev_register(dev);
2273 kfree(mp_ioapic_data[i]);
2274 mp_ioapic_data[i] = NULL;
2275 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2283 device_initcall(ioapic_init_sysfs);
2286 * Dynamic irq allocate and deallocation
2288 int create_irq(void)
2290 /* Allocate an unused irq */
2293 unsigned long flags;
2296 spin_lock_irqsave(&vector_lock, flags);
2297 for (new = (nr_irqs - 1); new >= 0; new--) {
2298 if (platform_legacy_irq(new))
2300 if (irq_cfg[new].vector != 0)
2302 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
2306 spin_unlock_irqrestore(&vector_lock, flags);
2309 dynamic_irq_init(irq);
2314 void destroy_irq(unsigned int irq)
2316 unsigned long flags;
2318 dynamic_irq_cleanup(irq);
2320 #ifdef CONFIG_INTR_REMAP
2323 spin_lock_irqsave(&vector_lock, flags);
2324 __clear_irq_vector(irq);
2325 spin_unlock_irqrestore(&vector_lock, flags);
2329 * MSI message composition
2331 #ifdef CONFIG_PCI_MSI
2332 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
2334 struct irq_cfg *cfg = irq_cfg + irq;
2340 err = assign_irq_vector(irq, tmp);
2344 cpus_and(tmp, cfg->domain, tmp);
2345 dest = cpu_mask_to_apicid(tmp);
2347 #ifdef CONFIG_INTR_REMAP
2348 if (irq_remapped(irq)) {
2353 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
2354 BUG_ON(ir_index == -1);
2356 memset (&irte, 0, sizeof(irte));
2359 irte.dst_mode = INT_DEST_MODE;
2360 irte.trigger_mode = 0; /* edge */
2361 irte.dlvry_mode = INT_DELIVERY_MODE;
2362 irte.vector = cfg->vector;
2363 irte.dest_id = IRTE_DEST(dest);
2365 modify_irte(irq, &irte);
2367 msg->address_hi = MSI_ADDR_BASE_HI;
2368 msg->data = sub_handle;
2369 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
2371 MSI_ADDR_IR_INDEX1(ir_index) |
2372 MSI_ADDR_IR_INDEX2(ir_index);
2376 msg->address_hi = MSI_ADDR_BASE_HI;
2379 ((INT_DEST_MODE == 0) ?
2380 MSI_ADDR_DEST_MODE_PHYSICAL:
2381 MSI_ADDR_DEST_MODE_LOGICAL) |
2382 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2383 MSI_ADDR_REDIRECTION_CPU:
2384 MSI_ADDR_REDIRECTION_LOWPRI) |
2385 MSI_ADDR_DEST_ID(dest);
2388 MSI_DATA_TRIGGER_EDGE |
2389 MSI_DATA_LEVEL_ASSERT |
2390 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2391 MSI_DATA_DELIVERY_FIXED:
2392 MSI_DATA_DELIVERY_LOWPRI) |
2393 MSI_DATA_VECTOR(cfg->vector);
2399 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2401 struct irq_cfg *cfg = irq_cfg + irq;
2406 cpus_and(tmp, mask, cpu_online_map);
2407 if (cpus_empty(tmp))
2410 if (assign_irq_vector(irq, mask))
2413 cpus_and(tmp, cfg->domain, mask);
2414 dest = cpu_mask_to_apicid(tmp);
2416 read_msi_msg(irq, &msg);
2418 msg.data &= ~MSI_DATA_VECTOR_MASK;
2419 msg.data |= MSI_DATA_VECTOR(cfg->vector);
2420 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2421 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2423 write_msi_msg(irq, &msg);
2424 irq_desc[irq].affinity = mask;
2427 #ifdef CONFIG_INTR_REMAP
2429 * Migrate the MSI irq to another cpumask. This migration is
2430 * done in the process context using interrupt-remapping hardware.
2432 static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2434 struct irq_cfg *cfg = irq_cfg + irq;
2436 cpumask_t tmp, cleanup_mask;
2439 cpus_and(tmp, mask, cpu_online_map);
2440 if (cpus_empty(tmp))
2443 if (get_irte(irq, &irte))
2446 if (assign_irq_vector(irq, mask))
2449 cpus_and(tmp, cfg->domain, mask);
2450 dest = cpu_mask_to_apicid(tmp);
2452 irte.vector = cfg->vector;
2453 irte.dest_id = IRTE_DEST(dest);
2456 * atomically update the IRTE with the new destination and vector.
2458 modify_irte(irq, &irte);
2461 * After this point, all the interrupts will start arriving
2462 * at the new destination. So, time to cleanup the previous
2463 * vector allocation.
2465 if (cfg->move_in_progress) {
2466 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2467 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2468 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2469 cfg->move_in_progress = 0;
2472 irq_desc[irq].affinity = mask;
2475 #endif /* CONFIG_SMP */
2478 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2479 * which implement the MSI or MSI-X Capability Structure.
2481 static struct irq_chip msi_chip = {
2483 .unmask = unmask_msi_irq,
2484 .mask = mask_msi_irq,
2485 .ack = ack_apic_edge,
2487 .set_affinity = set_msi_irq_affinity,
2489 .retrigger = ioapic_retrigger_irq,
2492 #ifdef CONFIG_INTR_REMAP
2493 static struct irq_chip msi_ir_chip = {
2494 .name = "IR-PCI-MSI",
2495 .unmask = unmask_msi_irq,
2496 .mask = mask_msi_irq,
2497 .ack = ack_x2apic_edge,
2499 .set_affinity = ir_set_msi_irq_affinity,
2501 .retrigger = ioapic_retrigger_irq,
2505 * Map the PCI dev to the corresponding remapping hardware unit
2506 * and allocate 'nvec' consecutive interrupt-remapping table entries
2509 static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
2511 struct intel_iommu *iommu;
2514 iommu = map_dev_to_ir(dev);
2517 "Unable to map PCI %s to iommu\n", pci_name(dev));
2521 index = alloc_irte(iommu, irq, nvec);
2524 "Unable to allocate %d IRTE for PCI %s\n", nvec,
2532 static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
2537 ret = msi_compose_msg(dev, irq, &msg);
2541 set_irq_msi(irq, desc);
2542 write_msi_msg(irq, &msg);
2544 #ifdef CONFIG_INTR_REMAP
2545 if (irq_remapped(irq)) {
2546 struct irq_desc *desc = irq_desc + irq;
2548 * irq migration in process context
2550 desc->status |= IRQ_MOVE_PCNTXT;
2551 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
2554 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
2559 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
2567 #ifdef CONFIG_INTR_REMAP
2568 if (!intr_remapping_enabled)
2571 ret = msi_alloc_irte(dev, irq, 1);
2576 ret = setup_msi_irq(dev, desc, irq);
2583 #ifdef CONFIG_INTR_REMAP
2590 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
2592 int irq, ret, sub_handle;
2593 struct msi_desc *desc;
2594 #ifdef CONFIG_INTR_REMAP
2595 struct intel_iommu *iommu = 0;
2600 list_for_each_entry(desc, &dev->msi_list, list) {
2604 #ifdef CONFIG_INTR_REMAP
2605 if (!intr_remapping_enabled)
2610 * allocate the consecutive block of IRTE's
2613 index = msi_alloc_irte(dev, irq, nvec);
2619 iommu = map_dev_to_ir(dev);
2625 * setup the mapping between the irq and the IRTE
2626 * base index, the sub_handle pointing to the
2627 * appropriate interrupt remap table entry.
2629 set_irte_irq(irq, iommu, index, sub_handle);
2633 ret = setup_msi_irq(dev, desc, irq);
2645 void arch_teardown_msi_irq(unsigned int irq)
2652 static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
2654 struct irq_cfg *cfg = irq_cfg + irq;
2659 cpus_and(tmp, mask, cpu_online_map);
2660 if (cpus_empty(tmp))
2663 if (assign_irq_vector(irq, mask))
2666 cpus_and(tmp, cfg->domain, mask);
2667 dest = cpu_mask_to_apicid(tmp);
2669 dmar_msi_read(irq, &msg);
2671 msg.data &= ~MSI_DATA_VECTOR_MASK;
2672 msg.data |= MSI_DATA_VECTOR(cfg->vector);
2673 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2674 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2676 dmar_msi_write(irq, &msg);
2677 irq_desc[irq].affinity = mask;
2679 #endif /* CONFIG_SMP */
2681 struct irq_chip dmar_msi_type = {
2683 .unmask = dmar_msi_unmask,
2684 .mask = dmar_msi_mask,
2685 .ack = ack_apic_edge,
2687 .set_affinity = dmar_msi_set_affinity,
2689 .retrigger = ioapic_retrigger_irq,
2692 int arch_setup_dmar_msi(unsigned int irq)
2697 ret = msi_compose_msg(NULL, irq, &msg);
2700 dmar_msi_write(irq, &msg);
2701 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
2707 #endif /* CONFIG_PCI_MSI */
2709 * Hypertransport interrupt support
2711 #ifdef CONFIG_HT_IRQ
2715 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2717 struct ht_irq_msg msg;
2718 fetch_ht_irq_msg(irq, &msg);
2720 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2721 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2723 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2724 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2726 write_ht_irq_msg(irq, &msg);
2729 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2731 struct irq_cfg *cfg = irq_cfg + irq;
2735 cpus_and(tmp, mask, cpu_online_map);
2736 if (cpus_empty(tmp))
2739 if (assign_irq_vector(irq, mask))
2742 cpus_and(tmp, cfg->domain, mask);
2743 dest = cpu_mask_to_apicid(tmp);
2745 target_ht_irq(irq, dest, cfg->vector);
2746 irq_desc[irq].affinity = mask;
2750 static struct irq_chip ht_irq_chip = {
2752 .mask = mask_ht_irq,
2753 .unmask = unmask_ht_irq,
2754 .ack = ack_apic_edge,
2756 .set_affinity = set_ht_irq_affinity,
2758 .retrigger = ioapic_retrigger_irq,
2761 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2763 struct irq_cfg *cfg = irq_cfg + irq;
2768 err = assign_irq_vector(irq, tmp);
2770 struct ht_irq_msg msg;
2773 cpus_and(tmp, cfg->domain, tmp);
2774 dest = cpu_mask_to_apicid(tmp);
2776 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2780 HT_IRQ_LOW_DEST_ID(dest) |
2781 HT_IRQ_LOW_VECTOR(cfg->vector) |
2782 ((INT_DEST_MODE == 0) ?
2783 HT_IRQ_LOW_DM_PHYSICAL :
2784 HT_IRQ_LOW_DM_LOGICAL) |
2785 HT_IRQ_LOW_RQEOI_EDGE |
2786 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2787 HT_IRQ_LOW_MT_FIXED :
2788 HT_IRQ_LOW_MT_ARBITRATED) |
2789 HT_IRQ_LOW_IRQ_MASKED;
2791 write_ht_irq_msg(irq, &msg);
2793 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2794 handle_edge_irq, "edge");
2798 #endif /* CONFIG_HT_IRQ */
2800 /* --------------------------------------------------------------------------
2801 ACPI-based IOAPIC Configuration
2802 -------------------------------------------------------------------------- */
2806 #define IO_APIC_MAX_ID 0xFE
2808 int __init io_apic_get_redir_entries (int ioapic)
2810 union IO_APIC_reg_01 reg_01;
2811 unsigned long flags;
2813 spin_lock_irqsave(&ioapic_lock, flags);
2814 reg_01.raw = io_apic_read(ioapic, 1);
2815 spin_unlock_irqrestore(&ioapic_lock, flags);
2817 return reg_01.bits.entries;
2821 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2823 if (!IO_APIC_IRQ(irq)) {
2824 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2830 * IRQs < 16 are already in the irq_2_pin[] map
2833 add_pin_to_irq(irq, ioapic, pin);
2835 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
2841 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2845 if (skip_ioapic_setup)
2848 for (i = 0; i < mp_irq_entries; i++)
2849 if (mp_irqs[i].mp_irqtype == mp_INT &&
2850 mp_irqs[i].mp_srcbusirq == bus_irq)
2852 if (i >= mp_irq_entries)
2855 *trigger = irq_trigger(i);
2856 *polarity = irq_polarity(i);
2860 #endif /* CONFIG_ACPI */
2863 * This function currently is only a helper for the i386 smp boot process where
2864 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2865 * so mask in all cases should simply be TARGET_CPUS
2868 void __init setup_ioapic_dest(void)
2870 int pin, ioapic, irq, irq_entry;
2872 if (skip_ioapic_setup == 1)
2875 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2876 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2877 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2878 if (irq_entry == -1)
2880 irq = pin_2_irq(irq_entry, ioapic, pin);
2882 /* setup_IO_APIC_irqs could fail to get vector for some device
2883 * when you have too many devices, because at that time only boot
2886 if (!irq_cfg[irq].vector)
2887 setup_IO_APIC_irq(ioapic, pin, irq,
2888 irq_trigger(irq_entry),
2889 irq_polarity(irq_entry));
2890 #ifdef CONFIG_INTR_REMAP
2891 else if (intr_remapping_enabled)
2892 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
2895 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2902 #define IOAPIC_RESOURCE_NAME_SIZE 11
2904 static struct resource *ioapic_resources;
2906 static struct resource * __init ioapic_setup_resources(void)
2909 struct resource *res;
2913 if (nr_ioapics <= 0)
2916 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
2919 mem = alloc_bootmem(n);
2923 mem += sizeof(struct resource) * nr_ioapics;
2925 for (i = 0; i < nr_ioapics; i++) {
2927 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
2928 sprintf(mem, "IOAPIC %u", i);
2929 mem += IOAPIC_RESOURCE_NAME_SIZE;
2933 ioapic_resources = res;
2938 void __init ioapic_init_mappings(void)
2940 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2941 struct resource *ioapic_res;
2944 ioapic_res = ioapic_setup_resources();
2945 for (i = 0; i < nr_ioapics; i++) {
2946 if (smp_found_config) {
2947 ioapic_phys = mp_ioapics[i].mp_apicaddr;
2949 ioapic_phys = (unsigned long)
2950 alloc_bootmem_pages(PAGE_SIZE);
2951 ioapic_phys = __pa(ioapic_phys);
2953 set_fixmap_nocache(idx, ioapic_phys);
2954 apic_printk(APIC_VERBOSE,
2955 "mapped IOAPIC to %016lx (%016lx)\n",
2956 __fix_to_virt(idx), ioapic_phys);
2959 if (ioapic_res != NULL) {
2960 ioapic_res->start = ioapic_phys;
2961 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
2967 static int __init ioapic_insert_resources(void)
2970 struct resource *r = ioapic_resources;
2974 "IO APIC resources could be not be allocated.\n");
2978 for (i = 0; i < nr_ioapics; i++) {
2979 insert_resource(&iomem_resource, r);
2986 /* Insert the IO APIC resources after PCI initialization has occured to handle
2987 * IO APICS that are mapped in on a BAR in PCI space. */
2988 late_initcall(ioapic_insert_resources);