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>
45 #include <asm/proto.h>
49 #include <asm/msidef.h>
50 #include <asm/hypertransport.h>
53 #include <mach_apic.h>
58 unsigned move_cleanup_count;
60 u8 move_in_progress : 1;
63 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
64 static struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
65 [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
66 [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
67 [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
68 [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
69 [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
70 [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
71 [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
72 [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
73 [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
74 [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
75 [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
76 [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
77 [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
78 [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
79 [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
80 [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
83 static int assign_irq_vector(int irq, cpumask_t mask);
85 int first_system_vector = 0xfe;
87 char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
89 #define __apicdebuginit __init
91 int sis_apic_bug; /* not actually supported, dummy for compile */
93 static int no_timer_check;
95 static int disable_timer_pin_1 __initdata;
97 int timer_through_8259 __initdata;
99 /* Where if anywhere is the i8259 connect in external int mode */
100 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
102 static DEFINE_SPINLOCK(ioapic_lock);
103 DEFINE_SPINLOCK(vector_lock);
106 * # of IRQ routing registers
108 int nr_ioapic_registers[MAX_IO_APICS];
110 /* I/O APIC entries */
111 struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
114 /* MP IRQ source entries */
115 struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
117 /* # of MP IRQ source entries */
120 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
123 * Rough estimation of how many shared IRQs there are, can
124 * be changed anytime.
126 #define MAX_PLUS_SHARED_IRQS NR_IRQS
127 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
130 * This is performance-critical, we want to do it O(1)
132 * the indexing order of this array favors 1:1 mappings
133 * between pins and IRQs.
136 static struct irq_pin_list {
137 short apic, pin, next;
138 } irq_2_pin[PIN_MAP_SIZE];
142 unsigned int unused[3];
146 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
148 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
149 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
152 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
154 struct io_apic __iomem *io_apic = io_apic_base(apic);
155 writel(reg, &io_apic->index);
156 return readl(&io_apic->data);
159 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
161 struct io_apic __iomem *io_apic = io_apic_base(apic);
162 writel(reg, &io_apic->index);
163 writel(value, &io_apic->data);
167 * Re-write a value: to be used for read-modify-write
168 * cycles where the read already set up the index register.
170 static inline void io_apic_modify(unsigned int apic, unsigned int value)
172 struct io_apic __iomem *io_apic = io_apic_base(apic);
173 writel(value, &io_apic->data);
176 static bool io_apic_level_ack_pending(unsigned int irq)
178 struct irq_pin_list *entry;
181 spin_lock_irqsave(&ioapic_lock, flags);
182 entry = irq_2_pin + irq;
190 reg = io_apic_read(entry->apic, 0x10 + pin*2);
191 /* Is the remote IRR bit set? */
192 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
193 spin_unlock_irqrestore(&ioapic_lock, flags);
198 entry = irq_2_pin + entry->next;
200 spin_unlock_irqrestore(&ioapic_lock, flags);
206 * Synchronize the IO-APIC and the CPU by doing
207 * a dummy read from the IO-APIC
209 static inline void io_apic_sync(unsigned int apic)
211 struct io_apic __iomem *io_apic = io_apic_base(apic);
212 readl(&io_apic->data);
215 #define __DO_ACTION(R, ACTION, FINAL) \
219 struct irq_pin_list *entry = irq_2_pin + irq; \
221 BUG_ON(irq >= NR_IRQS); \
227 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
229 io_apic_modify(entry->apic, reg); \
233 entry = irq_2_pin + entry->next; \
238 struct { u32 w1, w2; };
239 struct IO_APIC_route_entry entry;
242 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
244 union entry_union eu;
246 spin_lock_irqsave(&ioapic_lock, flags);
247 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
248 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
249 spin_unlock_irqrestore(&ioapic_lock, flags);
254 * When we write a new IO APIC routing entry, we need to write the high
255 * word first! If the mask bit in the low word is clear, we will enable
256 * the interrupt, and we need to make sure the entry is fully populated
257 * before that happens.
260 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
262 union entry_union eu;
264 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
265 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
268 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
271 spin_lock_irqsave(&ioapic_lock, flags);
272 __ioapic_write_entry(apic, pin, e);
273 spin_unlock_irqrestore(&ioapic_lock, flags);
277 * When we mask an IO APIC routing entry, we need to write the low
278 * word first, in order to set the mask bit before we change the
281 static void ioapic_mask_entry(int apic, int pin)
284 union entry_union eu = { .entry.mask = 1 };
286 spin_lock_irqsave(&ioapic_lock, flags);
287 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
288 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
289 spin_unlock_irqrestore(&ioapic_lock, flags);
293 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
296 struct irq_pin_list *entry = irq_2_pin + irq;
298 BUG_ON(irq >= NR_IRQS);
305 io_apic_write(apic, 0x11 + pin*2, dest);
306 reg = io_apic_read(apic, 0x10 + pin*2);
307 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
309 io_apic_modify(apic, reg);
312 entry = irq_2_pin + entry->next;
316 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
318 struct irq_cfg *cfg = irq_cfg + irq;
323 cpus_and(tmp, mask, cpu_online_map);
327 if (assign_irq_vector(irq, mask))
330 cpus_and(tmp, cfg->domain, mask);
331 dest = cpu_mask_to_apicid(tmp);
334 * Only the high 8 bits are valid.
336 dest = SET_APIC_LOGICAL_ID(dest);
338 spin_lock_irqsave(&ioapic_lock, flags);
339 __target_IO_APIC_irq(irq, dest, cfg->vector);
340 irq_desc[irq].affinity = mask;
341 spin_unlock_irqrestore(&ioapic_lock, flags);
346 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
347 * shared ISA-space IRQs, so we have to support them. We are super
348 * fast in the common case, and fast for shared ISA-space IRQs.
350 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
352 static int first_free_entry = NR_IRQS;
353 struct irq_pin_list *entry = irq_2_pin + irq;
355 BUG_ON(irq >= NR_IRQS);
357 entry = irq_2_pin + entry->next;
359 if (entry->pin != -1) {
360 entry->next = first_free_entry;
361 entry = irq_2_pin + entry->next;
362 if (++first_free_entry >= PIN_MAP_SIZE)
363 panic("io_apic.c: ran out of irq_2_pin entries!");
370 * Reroute an IRQ to a different pin.
372 static void __init replace_pin_at_irq(unsigned int irq,
373 int oldapic, int oldpin,
374 int newapic, int newpin)
376 struct irq_pin_list *entry = irq_2_pin + irq;
379 if (entry->apic == oldapic && entry->pin == oldpin) {
380 entry->apic = newapic;
385 entry = irq_2_pin + entry->next;
390 #define DO_ACTION(name,R,ACTION, FINAL) \
392 static void name##_IO_APIC_irq (unsigned int irq) \
393 __DO_ACTION(R, ACTION, FINAL)
396 DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, io_apic_sync(entry->apic))
399 DO_ACTION(__unmask, 0, &= ~IO_APIC_REDIR_MASKED, )
401 static void mask_IO_APIC_irq (unsigned int irq)
405 spin_lock_irqsave(&ioapic_lock, flags);
406 __mask_IO_APIC_irq(irq);
407 spin_unlock_irqrestore(&ioapic_lock, flags);
410 static void unmask_IO_APIC_irq (unsigned int irq)
414 spin_lock_irqsave(&ioapic_lock, flags);
415 __unmask_IO_APIC_irq(irq);
416 spin_unlock_irqrestore(&ioapic_lock, flags);
419 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
421 struct IO_APIC_route_entry entry;
423 /* Check delivery_mode to be sure we're not clearing an SMI pin */
424 entry = ioapic_read_entry(apic, pin);
425 if (entry.delivery_mode == dest_SMI)
428 * Disable it in the IO-APIC irq-routing table:
430 ioapic_mask_entry(apic, pin);
433 static void clear_IO_APIC (void)
437 for (apic = 0; apic < nr_ioapics; apic++)
438 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
439 clear_IO_APIC_pin(apic, pin);
442 int skip_ioapic_setup;
445 static int __init parse_noapic(char *str)
447 disable_ioapic_setup();
450 early_param("noapic", parse_noapic);
452 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
453 static int __init disable_timer_pin_setup(char *arg)
455 disable_timer_pin_1 = 1;
458 __setup("disable_timer_pin_1", disable_timer_pin_setup);
462 * Find the IRQ entry number of a certain pin.
464 static int find_irq_entry(int apic, int pin, int type)
468 for (i = 0; i < mp_irq_entries; i++)
469 if (mp_irqs[i].mp_irqtype == type &&
470 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
471 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
472 mp_irqs[i].mp_dstirq == pin)
479 * Find the pin to which IRQ[irq] (ISA) is connected
481 static int __init find_isa_irq_pin(int irq, int type)
485 for (i = 0; i < mp_irq_entries; i++) {
486 int lbus = mp_irqs[i].mp_srcbus;
488 if (test_bit(lbus, mp_bus_not_pci) &&
489 (mp_irqs[i].mp_irqtype == type) &&
490 (mp_irqs[i].mp_srcbusirq == irq))
492 return mp_irqs[i].mp_dstirq;
497 static int __init find_isa_irq_apic(int irq, int type)
501 for (i = 0; i < mp_irq_entries; i++) {
502 int lbus = mp_irqs[i].mp_srcbus;
504 if (test_bit(lbus, mp_bus_not_pci) &&
505 (mp_irqs[i].mp_irqtype == type) &&
506 (mp_irqs[i].mp_srcbusirq == irq))
509 if (i < mp_irq_entries) {
511 for(apic = 0; apic < nr_ioapics; apic++) {
512 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
521 * Find a specific PCI IRQ entry.
522 * Not an __init, possibly needed by modules
524 static int pin_2_irq(int idx, int apic, int pin);
526 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
528 int apic, i, best_guess = -1;
530 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
532 if (test_bit(bus, mp_bus_not_pci)) {
533 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
536 for (i = 0; i < mp_irq_entries; i++) {
537 int lbus = mp_irqs[i].mp_srcbus;
539 for (apic = 0; apic < nr_ioapics; apic++)
540 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
541 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
544 if (!test_bit(lbus, mp_bus_not_pci) &&
545 !mp_irqs[i].mp_irqtype &&
547 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
548 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
550 if (!(apic || IO_APIC_IRQ(irq)))
553 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
556 * Use the first all-but-pin matching entry as a
557 * best-guess fuzzy result for broken mptables.
563 BUG_ON(best_guess >= NR_IRQS);
567 /* ISA interrupts are always polarity zero edge triggered,
568 * when listed as conforming in the MP table. */
570 #define default_ISA_trigger(idx) (0)
571 #define default_ISA_polarity(idx) (0)
573 /* PCI interrupts are always polarity one level triggered,
574 * when listed as conforming in the MP table. */
576 #define default_PCI_trigger(idx) (1)
577 #define default_PCI_polarity(idx) (1)
579 static int MPBIOS_polarity(int idx)
581 int bus = mp_irqs[idx].mp_srcbus;
585 * Determine IRQ line polarity (high active or low active):
587 switch (mp_irqs[idx].mp_irqflag & 3)
589 case 0: /* conforms, ie. bus-type dependent polarity */
590 if (test_bit(bus, mp_bus_not_pci))
591 polarity = default_ISA_polarity(idx);
593 polarity = default_PCI_polarity(idx);
595 case 1: /* high active */
600 case 2: /* reserved */
602 printk(KERN_WARNING "broken BIOS!!\n");
606 case 3: /* low active */
611 default: /* invalid */
613 printk(KERN_WARNING "broken BIOS!!\n");
621 static int MPBIOS_trigger(int idx)
623 int bus = mp_irqs[idx].mp_srcbus;
627 * Determine IRQ trigger mode (edge or level sensitive):
629 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
631 case 0: /* conforms, ie. bus-type dependent */
632 if (test_bit(bus, mp_bus_not_pci))
633 trigger = default_ISA_trigger(idx);
635 trigger = default_PCI_trigger(idx);
642 case 2: /* reserved */
644 printk(KERN_WARNING "broken BIOS!!\n");
653 default: /* invalid */
655 printk(KERN_WARNING "broken BIOS!!\n");
663 static inline int irq_polarity(int idx)
665 return MPBIOS_polarity(idx);
668 static inline int irq_trigger(int idx)
670 return MPBIOS_trigger(idx);
673 static int pin_2_irq(int idx, int apic, int pin)
676 int bus = mp_irqs[idx].mp_srcbus;
679 * Debugging check, we are in big trouble if this message pops up!
681 if (mp_irqs[idx].mp_dstirq != pin)
682 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
684 if (test_bit(bus, mp_bus_not_pci)) {
685 irq = mp_irqs[idx].mp_srcbusirq;
688 * PCI IRQs are mapped in order
692 irq += nr_ioapic_registers[i++];
695 BUG_ON(irq >= NR_IRQS);
699 static int __assign_irq_vector(int irq, cpumask_t mask)
702 * NOTE! The local APIC isn't very good at handling
703 * multiple interrupts at the same interrupt level.
704 * As the interrupt level is determined by taking the
705 * vector number and shifting that right by 4, we
706 * want to spread these out a bit so that they don't
707 * all fall in the same interrupt level.
709 * Also, we've got to be careful not to trash gate
710 * 0x80, because int 0x80 is hm, kind of importantish. ;)
712 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
713 unsigned int old_vector;
717 BUG_ON((unsigned)irq >= NR_IRQS);
720 /* Only try and allocate irqs on cpus that are present */
721 cpus_and(mask, mask, cpu_online_map);
723 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
726 old_vector = cfg->vector;
729 cpus_and(tmp, cfg->domain, mask);
730 if (!cpus_empty(tmp))
734 for_each_cpu_mask_nr(cpu, mask) {
735 cpumask_t domain, new_mask;
739 domain = vector_allocation_domain(cpu);
740 cpus_and(new_mask, domain, cpu_online_map);
742 vector = current_vector;
743 offset = current_offset;
746 if (vector >= first_system_vector) {
747 /* If we run out of vectors on large boxen, must share them. */
748 offset = (offset + 1) % 8;
749 vector = FIRST_DEVICE_VECTOR + offset;
751 if (unlikely(current_vector == vector))
753 if (vector == IA32_SYSCALL_VECTOR)
755 for_each_cpu_mask_nr(new_cpu, new_mask)
756 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
759 current_vector = vector;
760 current_offset = offset;
762 cfg->move_in_progress = 1;
763 cfg->old_domain = cfg->domain;
765 for_each_cpu_mask_nr(new_cpu, new_mask)
766 per_cpu(vector_irq, new_cpu)[vector] = irq;
767 cfg->vector = vector;
768 cfg->domain = domain;
774 static int assign_irq_vector(int irq, cpumask_t mask)
779 spin_lock_irqsave(&vector_lock, flags);
780 err = __assign_irq_vector(irq, mask);
781 spin_unlock_irqrestore(&vector_lock, flags);
785 static void __clear_irq_vector(int irq)
791 BUG_ON((unsigned)irq >= NR_IRQS);
793 BUG_ON(!cfg->vector);
795 vector = cfg->vector;
796 cpus_and(mask, cfg->domain, cpu_online_map);
797 for_each_cpu_mask_nr(cpu, mask)
798 per_cpu(vector_irq, cpu)[vector] = -1;
801 cpus_clear(cfg->domain);
804 static void __setup_vector_irq(int cpu)
806 /* Initialize vector_irq on a new cpu */
807 /* This function must be called with vector_lock held */
810 /* Mark the inuse vectors */
811 for (irq = 0; irq < NR_IRQS; ++irq) {
812 if (!cpu_isset(cpu, irq_cfg[irq].domain))
814 vector = irq_cfg[irq].vector;
815 per_cpu(vector_irq, cpu)[vector] = irq;
817 /* Mark the free vectors */
818 for (vector = 0; vector < NR_VECTORS; ++vector) {
819 irq = per_cpu(vector_irq, cpu)[vector];
822 if (!cpu_isset(cpu, irq_cfg[irq].domain))
823 per_cpu(vector_irq, cpu)[vector] = -1;
827 void setup_vector_irq(int cpu)
829 spin_lock(&vector_lock);
830 __setup_vector_irq(smp_processor_id());
831 spin_unlock(&vector_lock);
835 static struct irq_chip ioapic_chip;
837 static void ioapic_register_intr(int irq, unsigned long trigger)
840 irq_desc[irq].status |= IRQ_LEVEL;
841 set_irq_chip_and_handler_name(irq, &ioapic_chip,
842 handle_fasteoi_irq, "fasteoi");
844 irq_desc[irq].status &= ~IRQ_LEVEL;
845 set_irq_chip_and_handler_name(irq, &ioapic_chip,
846 handle_edge_irq, "edge");
850 static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
851 int trigger, int polarity)
853 struct irq_cfg *cfg = irq_cfg + irq;
854 struct IO_APIC_route_entry entry;
857 if (!IO_APIC_IRQ(irq))
861 if (assign_irq_vector(irq, mask))
864 cpus_and(mask, cfg->domain, mask);
866 apic_printk(APIC_VERBOSE,KERN_DEBUG
867 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
868 "IRQ %d Mode:%i Active:%i)\n",
869 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
870 irq, trigger, polarity);
873 * add it to the IO-APIC irq-routing table:
875 memset(&entry,0,sizeof(entry));
877 entry.delivery_mode = INT_DELIVERY_MODE;
878 entry.dest_mode = INT_DEST_MODE;
879 entry.dest = cpu_mask_to_apicid(mask);
880 entry.mask = 0; /* enable IRQ */
881 entry.trigger = trigger;
882 entry.polarity = polarity;
883 entry.vector = cfg->vector;
885 /* Mask level triggered irqs.
886 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
891 ioapic_register_intr(irq, trigger);
893 disable_8259A_irq(irq);
895 ioapic_write_entry(apic, pin, entry);
898 static void __init setup_IO_APIC_irqs(void)
900 int apic, pin, idx, irq, first_notcon = 1;
902 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
904 for (apic = 0; apic < nr_ioapics; apic++) {
905 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
907 idx = find_irq_entry(apic,pin,mp_INT);
910 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
913 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
917 apic_printk(APIC_VERBOSE, " not connected.\n");
921 irq = pin_2_irq(idx, apic, pin);
922 add_pin_to_irq(irq, apic, pin);
924 setup_IO_APIC_irq(apic, pin, irq,
925 irq_trigger(idx), irq_polarity(idx));
930 apic_printk(APIC_VERBOSE, " not connected.\n");
934 * Set up the timer pin, possibly with the 8259A-master behind.
936 static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
939 struct IO_APIC_route_entry entry;
941 memset(&entry, 0, sizeof(entry));
944 * We use logical delivery to get the timer IRQ
947 entry.dest_mode = INT_DEST_MODE;
948 entry.mask = 1; /* mask IRQ now */
949 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
950 entry.delivery_mode = INT_DELIVERY_MODE;
953 entry.vector = vector;
956 * The timer IRQ doesn't have to know that behind the
957 * scene we may have a 8259A-master in AEOI mode ...
959 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
962 * Add it to the IO-APIC irq-routing table:
964 ioapic_write_entry(apic, pin, entry);
967 void __apicdebuginit print_IO_APIC(void)
970 union IO_APIC_reg_00 reg_00;
971 union IO_APIC_reg_01 reg_01;
972 union IO_APIC_reg_02 reg_02;
975 if (apic_verbosity == APIC_QUIET)
978 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
979 for (i = 0; i < nr_ioapics; i++)
980 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
981 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
984 * We are a bit conservative about what we expect. We have to
985 * know about every hardware change ASAP.
987 printk(KERN_INFO "testing the IO APIC.......................\n");
989 for (apic = 0; apic < nr_ioapics; apic++) {
991 spin_lock_irqsave(&ioapic_lock, flags);
992 reg_00.raw = io_apic_read(apic, 0);
993 reg_01.raw = io_apic_read(apic, 1);
994 if (reg_01.bits.version >= 0x10)
995 reg_02.raw = io_apic_read(apic, 2);
996 spin_unlock_irqrestore(&ioapic_lock, flags);
999 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
1000 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1001 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1003 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1004 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1006 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1007 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1009 if (reg_01.bits.version >= 0x10) {
1010 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1011 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1014 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1016 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1017 " Stat Dmod Deli Vect: \n");
1019 for (i = 0; i <= reg_01.bits.entries; i++) {
1020 struct IO_APIC_route_entry entry;
1022 entry = ioapic_read_entry(apic, i);
1024 printk(KERN_DEBUG " %02x %03X ",
1029 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1034 entry.delivery_status,
1036 entry.delivery_mode,
1041 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1042 for (i = 0; i < NR_IRQS; i++) {
1043 struct irq_pin_list *entry = irq_2_pin + i;
1046 printk(KERN_DEBUG "IRQ%d ", i);
1048 printk("-> %d:%d", entry->apic, entry->pin);
1051 entry = irq_2_pin + entry->next;
1056 printk(KERN_INFO ".................................... done.\n");
1063 static __apicdebuginit void print_APIC_bitfield (int base)
1068 if (apic_verbosity == APIC_QUIET)
1071 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1072 for (i = 0; i < 8; i++) {
1073 v = apic_read(base + i*0x10);
1074 for (j = 0; j < 32; j++) {
1084 void __apicdebuginit print_local_APIC(void * dummy)
1086 unsigned int v, ver, maxlvt;
1088 if (apic_verbosity == APIC_QUIET)
1091 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1092 smp_processor_id(), hard_smp_processor_id());
1093 v = apic_read(APIC_ID);
1094 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(read_apic_id()));
1095 v = apic_read(APIC_LVR);
1096 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1097 ver = GET_APIC_VERSION(v);
1098 maxlvt = lapic_get_maxlvt();
1100 v = apic_read(APIC_TASKPRI);
1101 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1103 v = apic_read(APIC_ARBPRI);
1104 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1105 v & APIC_ARBPRI_MASK);
1106 v = apic_read(APIC_PROCPRI);
1107 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1109 v = apic_read(APIC_EOI);
1110 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1111 v = apic_read(APIC_RRR);
1112 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1113 v = apic_read(APIC_LDR);
1114 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1115 v = apic_read(APIC_DFR);
1116 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1117 v = apic_read(APIC_SPIV);
1118 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1120 printk(KERN_DEBUG "... APIC ISR field:\n");
1121 print_APIC_bitfield(APIC_ISR);
1122 printk(KERN_DEBUG "... APIC TMR field:\n");
1123 print_APIC_bitfield(APIC_TMR);
1124 printk(KERN_DEBUG "... APIC IRR field:\n");
1125 print_APIC_bitfield(APIC_IRR);
1127 v = apic_read(APIC_ESR);
1128 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1130 v = apic_read(APIC_ICR);
1131 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1132 v = apic_read(APIC_ICR2);
1133 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1135 v = apic_read(APIC_LVTT);
1136 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1138 if (maxlvt > 3) { /* PC is LVT#4. */
1139 v = apic_read(APIC_LVTPC);
1140 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1142 v = apic_read(APIC_LVT0);
1143 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1144 v = apic_read(APIC_LVT1);
1145 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1147 if (maxlvt > 2) { /* ERR is LVT#3. */
1148 v = apic_read(APIC_LVTERR);
1149 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1152 v = apic_read(APIC_TMICT);
1153 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1154 v = apic_read(APIC_TMCCT);
1155 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1156 v = apic_read(APIC_TDCR);
1157 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1161 void print_all_local_APICs (void)
1163 on_each_cpu(print_local_APIC, NULL, 1);
1166 void __apicdebuginit print_PIC(void)
1169 unsigned long flags;
1171 if (apic_verbosity == APIC_QUIET)
1174 printk(KERN_DEBUG "\nprinting PIC contents\n");
1176 spin_lock_irqsave(&i8259A_lock, flags);
1178 v = inb(0xa1) << 8 | inb(0x21);
1179 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1181 v = inb(0xa0) << 8 | inb(0x20);
1182 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1186 v = inb(0xa0) << 8 | inb(0x20);
1190 spin_unlock_irqrestore(&i8259A_lock, flags);
1192 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1194 v = inb(0x4d1) << 8 | inb(0x4d0);
1195 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1200 void __init enable_IO_APIC(void)
1202 union IO_APIC_reg_01 reg_01;
1203 int i8259_apic, i8259_pin;
1205 unsigned long flags;
1207 for (i = 0; i < PIN_MAP_SIZE; i++) {
1208 irq_2_pin[i].pin = -1;
1209 irq_2_pin[i].next = 0;
1213 * The number of IO-APIC IRQ registers (== #pins):
1215 for (apic = 0; apic < nr_ioapics; apic++) {
1216 spin_lock_irqsave(&ioapic_lock, flags);
1217 reg_01.raw = io_apic_read(apic, 1);
1218 spin_unlock_irqrestore(&ioapic_lock, flags);
1219 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1221 for(apic = 0; apic < nr_ioapics; apic++) {
1223 /* See if any of the pins is in ExtINT mode */
1224 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1225 struct IO_APIC_route_entry entry;
1226 entry = ioapic_read_entry(apic, pin);
1228 /* If the interrupt line is enabled and in ExtInt mode
1229 * I have found the pin where the i8259 is connected.
1231 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1232 ioapic_i8259.apic = apic;
1233 ioapic_i8259.pin = pin;
1239 /* Look to see what if the MP table has reported the ExtINT */
1240 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1241 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1242 /* Trust the MP table if nothing is setup in the hardware */
1243 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1244 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1245 ioapic_i8259.pin = i8259_pin;
1246 ioapic_i8259.apic = i8259_apic;
1248 /* Complain if the MP table and the hardware disagree */
1249 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1250 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1252 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1256 * Do not trust the IO-APIC being empty at bootup
1262 * Not an __init, needed by the reboot code
1264 void disable_IO_APIC(void)
1267 * Clear the IO-APIC before rebooting:
1272 * If the i8259 is routed through an IOAPIC
1273 * Put that IOAPIC in virtual wire mode
1274 * so legacy interrupts can be delivered.
1276 if (ioapic_i8259.pin != -1) {
1277 struct IO_APIC_route_entry entry;
1279 memset(&entry, 0, sizeof(entry));
1280 entry.mask = 0; /* Enabled */
1281 entry.trigger = 0; /* Edge */
1283 entry.polarity = 0; /* High */
1284 entry.delivery_status = 0;
1285 entry.dest_mode = 0; /* Physical */
1286 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1288 entry.dest = GET_APIC_ID(read_apic_id());
1291 * Add it to the IO-APIC irq-routing table:
1293 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1296 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1300 * There is a nasty bug in some older SMP boards, their mptable lies
1301 * about the timer IRQ. We do the following to work around the situation:
1303 * - timer IRQ defaults to IO-APIC IRQ
1304 * - if this function detects that timer IRQs are defunct, then we fall
1305 * back to ISA timer IRQs
1307 static int __init timer_irq_works(void)
1309 unsigned long t1 = jiffies;
1310 unsigned long flags;
1312 local_save_flags(flags);
1314 /* Let ten ticks pass... */
1315 mdelay((10 * 1000) / HZ);
1316 local_irq_restore(flags);
1319 * Expect a few ticks at least, to be sure some possible
1320 * glue logic does not lock up after one or two first
1321 * ticks in a non-ExtINT mode. Also the local APIC
1322 * might have cached one ExtINT interrupt. Finally, at
1323 * least one tick may be lost due to delays.
1327 if (time_after(jiffies, t1 + 4))
1333 * In the SMP+IOAPIC case it might happen that there are an unspecified
1334 * number of pending IRQ events unhandled. These cases are very rare,
1335 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1336 * better to do it this way as thus we do not have to be aware of
1337 * 'pending' interrupts in the IRQ path, except at this point.
1340 * Edge triggered needs to resend any interrupt
1341 * that was delayed but this is now handled in the device
1346 * Starting up a edge-triggered IO-APIC interrupt is
1347 * nasty - we need to make sure that we get the edge.
1348 * If it is already asserted for some reason, we need
1349 * return 1 to indicate that is was pending.
1351 * This is not complete - we should be able to fake
1352 * an edge even if it isn't on the 8259A...
1355 static unsigned int startup_ioapic_irq(unsigned int irq)
1357 int was_pending = 0;
1358 unsigned long flags;
1360 spin_lock_irqsave(&ioapic_lock, flags);
1362 disable_8259A_irq(irq);
1363 if (i8259A_irq_pending(irq))
1366 __unmask_IO_APIC_irq(irq);
1367 spin_unlock_irqrestore(&ioapic_lock, flags);
1372 static int ioapic_retrigger_irq(unsigned int irq)
1374 struct irq_cfg *cfg = &irq_cfg[irq];
1375 unsigned long flags;
1377 spin_lock_irqsave(&vector_lock, flags);
1378 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
1379 spin_unlock_irqrestore(&vector_lock, flags);
1385 * Level and edge triggered IO-APIC interrupts need different handling,
1386 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1387 * handled with the level-triggered descriptor, but that one has slightly
1388 * more overhead. Level-triggered interrupts cannot be handled with the
1389 * edge-triggered handler, without risking IRQ storms and other ugly
1394 asmlinkage void smp_irq_move_cleanup_interrupt(void)
1396 unsigned vector, me;
1401 me = smp_processor_id();
1402 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1404 struct irq_desc *desc;
1405 struct irq_cfg *cfg;
1406 irq = __get_cpu_var(vector_irq)[vector];
1410 desc = irq_desc + irq;
1411 cfg = irq_cfg + irq;
1412 spin_lock(&desc->lock);
1413 if (!cfg->move_cleanup_count)
1416 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
1419 __get_cpu_var(vector_irq)[vector] = -1;
1420 cfg->move_cleanup_count--;
1422 spin_unlock(&desc->lock);
1428 static void irq_complete_move(unsigned int irq)
1430 struct irq_cfg *cfg = irq_cfg + irq;
1431 unsigned vector, me;
1433 if (likely(!cfg->move_in_progress))
1436 vector = ~get_irq_regs()->orig_ax;
1437 me = smp_processor_id();
1438 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
1439 cpumask_t cleanup_mask;
1441 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1442 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1443 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1444 cfg->move_in_progress = 0;
1448 static inline void irq_complete_move(unsigned int irq) {}
1451 static void ack_apic_edge(unsigned int irq)
1453 irq_complete_move(irq);
1454 move_native_irq(irq);
1458 static void ack_apic_level(unsigned int irq)
1460 int do_unmask_irq = 0;
1462 irq_complete_move(irq);
1463 #ifdef CONFIG_GENERIC_PENDING_IRQ
1464 /* If we are moving the irq we need to mask it */
1465 if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1467 mask_IO_APIC_irq(irq);
1472 * We must acknowledge the irq before we move it or the acknowledge will
1473 * not propagate properly.
1477 /* Now we can move and renable the irq */
1478 if (unlikely(do_unmask_irq)) {
1479 /* Only migrate the irq if the ack has been received.
1481 * On rare occasions the broadcast level triggered ack gets
1482 * delayed going to ioapics, and if we reprogram the
1483 * vector while Remote IRR is still set the irq will never
1486 * To prevent this scenario we read the Remote IRR bit
1487 * of the ioapic. This has two effects.
1488 * - On any sane system the read of the ioapic will
1489 * flush writes (and acks) going to the ioapic from
1491 * - We get to see if the ACK has actually been delivered.
1493 * Based on failed experiments of reprogramming the
1494 * ioapic entry from outside of irq context starting
1495 * with masking the ioapic entry and then polling until
1496 * Remote IRR was clear before reprogramming the
1497 * ioapic I don't trust the Remote IRR bit to be
1498 * completey accurate.
1500 * However there appears to be no other way to plug
1501 * this race, so if the Remote IRR bit is not
1502 * accurate and is causing problems then it is a hardware bug
1503 * and you can go talk to the chipset vendor about it.
1505 if (!io_apic_level_ack_pending(irq))
1506 move_masked_irq(irq);
1507 unmask_IO_APIC_irq(irq);
1511 static struct irq_chip ioapic_chip __read_mostly = {
1513 .startup = startup_ioapic_irq,
1514 .mask = mask_IO_APIC_irq,
1515 .unmask = unmask_IO_APIC_irq,
1516 .ack = ack_apic_edge,
1517 .eoi = ack_apic_level,
1519 .set_affinity = set_ioapic_affinity_irq,
1521 .retrigger = ioapic_retrigger_irq,
1524 static inline void init_IO_APIC_traps(void)
1529 * NOTE! The local APIC isn't very good at handling
1530 * multiple interrupts at the same interrupt level.
1531 * As the interrupt level is determined by taking the
1532 * vector number and shifting that right by 4, we
1533 * want to spread these out a bit so that they don't
1534 * all fall in the same interrupt level.
1536 * Also, we've got to be careful not to trash gate
1537 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1539 for (irq = 0; irq < NR_IRQS ; irq++) {
1540 if (IO_APIC_IRQ(irq) && !irq_cfg[irq].vector) {
1542 * Hmm.. We don't have an entry for this,
1543 * so default to an old-fashioned 8259
1544 * interrupt if we can..
1547 make_8259A_irq(irq);
1549 /* Strange. Oh, well.. */
1550 irq_desc[irq].chip = &no_irq_chip;
1555 static void unmask_lapic_irq(unsigned int irq)
1559 v = apic_read(APIC_LVT0);
1560 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1563 static void mask_lapic_irq(unsigned int irq)
1567 v = apic_read(APIC_LVT0);
1568 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1571 static void ack_lapic_irq (unsigned int irq)
1576 static struct irq_chip lapic_chip __read_mostly = {
1577 .name = "local-APIC",
1578 .mask = mask_lapic_irq,
1579 .unmask = unmask_lapic_irq,
1580 .ack = ack_lapic_irq,
1583 static void lapic_register_intr(int irq)
1585 irq_desc[irq].status &= ~IRQ_LEVEL;
1586 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
1590 static void __init setup_nmi(void)
1593 * Dirty trick to enable the NMI watchdog ...
1594 * We put the 8259A master into AEOI mode and
1595 * unmask on all local APICs LVT0 as NMI.
1597 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1598 * is from Maciej W. Rozycki - so we do not have to EOI from
1599 * the NMI handler or the timer interrupt.
1601 printk(KERN_INFO "activating NMI Watchdog ...");
1603 enable_NMI_through_LVT0();
1609 * This looks a bit hackish but it's about the only one way of sending
1610 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1611 * not support the ExtINT mode, unfortunately. We need to send these
1612 * cycles as some i82489DX-based boards have glue logic that keeps the
1613 * 8259A interrupt line asserted until INTA. --macro
1615 static inline void __init unlock_ExtINT_logic(void)
1618 struct IO_APIC_route_entry entry0, entry1;
1619 unsigned char save_control, save_freq_select;
1621 pin = find_isa_irq_pin(8, mp_INT);
1622 apic = find_isa_irq_apic(8, mp_INT);
1626 entry0 = ioapic_read_entry(apic, pin);
1628 clear_IO_APIC_pin(apic, pin);
1630 memset(&entry1, 0, sizeof(entry1));
1632 entry1.dest_mode = 0; /* physical delivery */
1633 entry1.mask = 0; /* unmask IRQ now */
1634 entry1.dest = hard_smp_processor_id();
1635 entry1.delivery_mode = dest_ExtINT;
1636 entry1.polarity = entry0.polarity;
1640 ioapic_write_entry(apic, pin, entry1);
1642 save_control = CMOS_READ(RTC_CONTROL);
1643 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1644 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1646 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1651 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1655 CMOS_WRITE(save_control, RTC_CONTROL);
1656 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1657 clear_IO_APIC_pin(apic, pin);
1659 ioapic_write_entry(apic, pin, entry0);
1663 * This code may look a bit paranoid, but it's supposed to cooperate with
1664 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1665 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1666 * fanatically on his truly buggy board.
1668 * FIXME: really need to revamp this for modern platforms only.
1670 static inline void __init check_timer(void)
1672 struct irq_cfg *cfg = irq_cfg + 0;
1673 int apic1, pin1, apic2, pin2;
1674 unsigned long flags;
1677 local_irq_save(flags);
1680 * get/set the timer IRQ vector:
1682 disable_8259A_irq(0);
1683 assign_irq_vector(0, TARGET_CPUS);
1686 * As IRQ0 is to be enabled in the 8259A, the virtual
1687 * wire has to be disabled in the local APIC.
1689 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1692 pin1 = find_isa_irq_pin(0, mp_INT);
1693 apic1 = find_isa_irq_apic(0, mp_INT);
1694 pin2 = ioapic_i8259.pin;
1695 apic2 = ioapic_i8259.apic;
1697 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1698 cfg->vector, apic1, pin1, apic2, pin2);
1701 * Some BIOS writers are clueless and report the ExtINTA
1702 * I/O APIC input from the cascaded 8259A as the timer
1703 * interrupt input. So just in case, if only one pin
1704 * was found above, try it both directly and through the
1711 } else if (pin2 == -1) {
1718 * Ok, does IRQ0 through the IOAPIC work?
1721 add_pin_to_irq(0, apic1, pin1);
1722 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
1724 unmask_IO_APIC_irq(0);
1725 if (!no_timer_check && timer_irq_works()) {
1726 if (nmi_watchdog == NMI_IO_APIC) {
1728 enable_8259A_irq(0);
1730 if (disable_timer_pin_1 > 0)
1731 clear_IO_APIC_pin(0, pin1);
1734 clear_IO_APIC_pin(apic1, pin1);
1736 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: "
1737 "8254 timer not connected to IO-APIC\n");
1739 apic_printk(APIC_VERBOSE,KERN_INFO
1740 "...trying to set up timer (IRQ0) "
1741 "through the 8259A ... ");
1742 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1745 * legacy devices should be connected to IO APIC #0
1747 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
1748 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
1749 unmask_IO_APIC_irq(0);
1750 enable_8259A_irq(0);
1751 if (timer_irq_works()) {
1752 apic_printk(APIC_VERBOSE," works.\n");
1753 timer_through_8259 = 1;
1754 if (nmi_watchdog == NMI_IO_APIC) {
1755 disable_8259A_irq(0);
1757 enable_8259A_irq(0);
1762 * Cleanup, just in case ...
1764 disable_8259A_irq(0);
1765 clear_IO_APIC_pin(apic2, pin2);
1766 apic_printk(APIC_VERBOSE," failed.\n");
1769 if (nmi_watchdog == NMI_IO_APIC) {
1770 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1771 nmi_watchdog = NMI_NONE;
1774 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1776 lapic_register_intr(0);
1777 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
1778 enable_8259A_irq(0);
1780 if (timer_irq_works()) {
1781 apic_printk(APIC_VERBOSE," works.\n");
1784 disable_8259A_irq(0);
1785 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
1786 apic_printk(APIC_VERBOSE," failed.\n");
1788 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1792 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1794 unlock_ExtINT_logic();
1796 if (timer_irq_works()) {
1797 apic_printk(APIC_VERBOSE," works.\n");
1800 apic_printk(APIC_VERBOSE," failed :(.\n");
1801 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1803 local_irq_restore(flags);
1806 static int __init notimercheck(char *s)
1811 __setup("no_timer_check", notimercheck);
1814 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
1815 * to devices. However there may be an I/O APIC pin available for
1816 * this interrupt regardless. The pin may be left unconnected, but
1817 * typically it will be reused as an ExtINT cascade interrupt for
1818 * the master 8259A. In the MPS case such a pin will normally be
1819 * reported as an ExtINT interrupt in the MP table. With ACPI
1820 * there is no provision for ExtINT interrupts, and in the absence
1821 * of an override it would be treated as an ordinary ISA I/O APIC
1822 * interrupt, that is edge-triggered and unmasked by default. We
1823 * used to do this, but it caused problems on some systems because
1824 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
1825 * the same ExtINT cascade interrupt to drive the local APIC of the
1826 * bootstrap processor. Therefore we refrain from routing IRQ2 to
1827 * the I/O APIC in all cases now. No actual device should request
1828 * it anyway. --macro
1830 #define PIC_IRQS (1<<2)
1832 void __init setup_IO_APIC(void)
1836 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
1839 io_apic_irqs = ~PIC_IRQS;
1841 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1844 setup_IO_APIC_irqs();
1845 init_IO_APIC_traps();
1851 struct sysfs_ioapic_data {
1852 struct sys_device dev;
1853 struct IO_APIC_route_entry entry[0];
1855 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1857 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1859 struct IO_APIC_route_entry *entry;
1860 struct sysfs_ioapic_data *data;
1863 data = container_of(dev, struct sysfs_ioapic_data, dev);
1864 entry = data->entry;
1865 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
1866 *entry = ioapic_read_entry(dev->id, i);
1871 static int ioapic_resume(struct sys_device *dev)
1873 struct IO_APIC_route_entry *entry;
1874 struct sysfs_ioapic_data *data;
1875 unsigned long flags;
1876 union IO_APIC_reg_00 reg_00;
1879 data = container_of(dev, struct sysfs_ioapic_data, dev);
1880 entry = data->entry;
1882 spin_lock_irqsave(&ioapic_lock, flags);
1883 reg_00.raw = io_apic_read(dev->id, 0);
1884 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
1885 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
1886 io_apic_write(dev->id, 0, reg_00.raw);
1888 spin_unlock_irqrestore(&ioapic_lock, flags);
1889 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
1890 ioapic_write_entry(dev->id, i, entry[i]);
1895 static struct sysdev_class ioapic_sysdev_class = {
1897 .suspend = ioapic_suspend,
1898 .resume = ioapic_resume,
1901 static int __init ioapic_init_sysfs(void)
1903 struct sys_device * dev;
1906 error = sysdev_class_register(&ioapic_sysdev_class);
1910 for (i = 0; i < nr_ioapics; i++ ) {
1911 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1912 * sizeof(struct IO_APIC_route_entry);
1913 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
1914 if (!mp_ioapic_data[i]) {
1915 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1918 dev = &mp_ioapic_data[i]->dev;
1920 dev->cls = &ioapic_sysdev_class;
1921 error = sysdev_register(dev);
1923 kfree(mp_ioapic_data[i]);
1924 mp_ioapic_data[i] = NULL;
1925 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1933 device_initcall(ioapic_init_sysfs);
1936 * Dynamic irq allocate and deallocation
1938 int create_irq(void)
1940 /* Allocate an unused irq */
1943 unsigned long flags;
1946 spin_lock_irqsave(&vector_lock, flags);
1947 for (new = (NR_IRQS - 1); new >= 0; new--) {
1948 if (platform_legacy_irq(new))
1950 if (irq_cfg[new].vector != 0)
1952 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
1956 spin_unlock_irqrestore(&vector_lock, flags);
1959 dynamic_irq_init(irq);
1964 void destroy_irq(unsigned int irq)
1966 unsigned long flags;
1968 dynamic_irq_cleanup(irq);
1970 spin_lock_irqsave(&vector_lock, flags);
1971 __clear_irq_vector(irq);
1972 spin_unlock_irqrestore(&vector_lock, flags);
1976 * MSI message composition
1978 #ifdef CONFIG_PCI_MSI
1979 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
1981 struct irq_cfg *cfg = irq_cfg + irq;
1987 err = assign_irq_vector(irq, tmp);
1989 cpus_and(tmp, cfg->domain, tmp);
1990 dest = cpu_mask_to_apicid(tmp);
1992 msg->address_hi = MSI_ADDR_BASE_HI;
1995 ((INT_DEST_MODE == 0) ?
1996 MSI_ADDR_DEST_MODE_PHYSICAL:
1997 MSI_ADDR_DEST_MODE_LOGICAL) |
1998 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1999 MSI_ADDR_REDIRECTION_CPU:
2000 MSI_ADDR_REDIRECTION_LOWPRI) |
2001 MSI_ADDR_DEST_ID(dest);
2004 MSI_DATA_TRIGGER_EDGE |
2005 MSI_DATA_LEVEL_ASSERT |
2006 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2007 MSI_DATA_DELIVERY_FIXED:
2008 MSI_DATA_DELIVERY_LOWPRI) |
2009 MSI_DATA_VECTOR(cfg->vector);
2015 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2017 struct irq_cfg *cfg = irq_cfg + irq;
2022 cpus_and(tmp, mask, cpu_online_map);
2023 if (cpus_empty(tmp))
2026 if (assign_irq_vector(irq, mask))
2029 cpus_and(tmp, cfg->domain, mask);
2030 dest = cpu_mask_to_apicid(tmp);
2032 read_msi_msg(irq, &msg);
2034 msg.data &= ~MSI_DATA_VECTOR_MASK;
2035 msg.data |= MSI_DATA_VECTOR(cfg->vector);
2036 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2037 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2039 write_msi_msg(irq, &msg);
2040 irq_desc[irq].affinity = mask;
2042 #endif /* CONFIG_SMP */
2045 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2046 * which implement the MSI or MSI-X Capability Structure.
2048 static struct irq_chip msi_chip = {
2050 .unmask = unmask_msi_irq,
2051 .mask = mask_msi_irq,
2052 .ack = ack_apic_edge,
2054 .set_affinity = set_msi_irq_affinity,
2056 .retrigger = ioapic_retrigger_irq,
2059 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
2067 ret = msi_compose_msg(dev, irq, &msg);
2073 set_irq_msi(irq, desc);
2074 write_msi_msg(irq, &msg);
2076 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
2081 void arch_teardown_msi_irq(unsigned int irq)
2088 static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
2090 struct irq_cfg *cfg = irq_cfg + irq;
2095 cpus_and(tmp, mask, cpu_online_map);
2096 if (cpus_empty(tmp))
2099 if (assign_irq_vector(irq, mask))
2102 cpus_and(tmp, cfg->domain, mask);
2103 dest = cpu_mask_to_apicid(tmp);
2105 dmar_msi_read(irq, &msg);
2107 msg.data &= ~MSI_DATA_VECTOR_MASK;
2108 msg.data |= MSI_DATA_VECTOR(cfg->vector);
2109 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2110 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2112 dmar_msi_write(irq, &msg);
2113 irq_desc[irq].affinity = mask;
2115 #endif /* CONFIG_SMP */
2117 struct irq_chip dmar_msi_type = {
2119 .unmask = dmar_msi_unmask,
2120 .mask = dmar_msi_mask,
2121 .ack = ack_apic_edge,
2123 .set_affinity = dmar_msi_set_affinity,
2125 .retrigger = ioapic_retrigger_irq,
2128 int arch_setup_dmar_msi(unsigned int irq)
2133 ret = msi_compose_msg(NULL, irq, &msg);
2136 dmar_msi_write(irq, &msg);
2137 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
2143 #endif /* CONFIG_PCI_MSI */
2145 * Hypertransport interrupt support
2147 #ifdef CONFIG_HT_IRQ
2151 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2153 struct ht_irq_msg msg;
2154 fetch_ht_irq_msg(irq, &msg);
2156 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2157 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2159 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2160 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2162 write_ht_irq_msg(irq, &msg);
2165 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2167 struct irq_cfg *cfg = irq_cfg + irq;
2171 cpus_and(tmp, mask, cpu_online_map);
2172 if (cpus_empty(tmp))
2175 if (assign_irq_vector(irq, mask))
2178 cpus_and(tmp, cfg->domain, mask);
2179 dest = cpu_mask_to_apicid(tmp);
2181 target_ht_irq(irq, dest, cfg->vector);
2182 irq_desc[irq].affinity = mask;
2186 static struct irq_chip ht_irq_chip = {
2188 .mask = mask_ht_irq,
2189 .unmask = unmask_ht_irq,
2190 .ack = ack_apic_edge,
2192 .set_affinity = set_ht_irq_affinity,
2194 .retrigger = ioapic_retrigger_irq,
2197 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2199 struct irq_cfg *cfg = irq_cfg + irq;
2204 err = assign_irq_vector(irq, tmp);
2206 struct ht_irq_msg msg;
2209 cpus_and(tmp, cfg->domain, tmp);
2210 dest = cpu_mask_to_apicid(tmp);
2212 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2216 HT_IRQ_LOW_DEST_ID(dest) |
2217 HT_IRQ_LOW_VECTOR(cfg->vector) |
2218 ((INT_DEST_MODE == 0) ?
2219 HT_IRQ_LOW_DM_PHYSICAL :
2220 HT_IRQ_LOW_DM_LOGICAL) |
2221 HT_IRQ_LOW_RQEOI_EDGE |
2222 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2223 HT_IRQ_LOW_MT_FIXED :
2224 HT_IRQ_LOW_MT_ARBITRATED) |
2225 HT_IRQ_LOW_IRQ_MASKED;
2227 write_ht_irq_msg(irq, &msg);
2229 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2230 handle_edge_irq, "edge");
2234 #endif /* CONFIG_HT_IRQ */
2236 /* --------------------------------------------------------------------------
2237 ACPI-based IOAPIC Configuration
2238 -------------------------------------------------------------------------- */
2242 #define IO_APIC_MAX_ID 0xFE
2244 int __init io_apic_get_redir_entries (int ioapic)
2246 union IO_APIC_reg_01 reg_01;
2247 unsigned long flags;
2249 spin_lock_irqsave(&ioapic_lock, flags);
2250 reg_01.raw = io_apic_read(ioapic, 1);
2251 spin_unlock_irqrestore(&ioapic_lock, flags);
2253 return reg_01.bits.entries;
2257 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2259 if (!IO_APIC_IRQ(irq)) {
2260 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2266 * IRQs < 16 are already in the irq_2_pin[] map
2269 add_pin_to_irq(irq, ioapic, pin);
2271 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
2277 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2281 if (skip_ioapic_setup)
2284 for (i = 0; i < mp_irq_entries; i++)
2285 if (mp_irqs[i].mp_irqtype == mp_INT &&
2286 mp_irqs[i].mp_srcbusirq == bus_irq)
2288 if (i >= mp_irq_entries)
2291 *trigger = irq_trigger(i);
2292 *polarity = irq_polarity(i);
2296 #endif /* CONFIG_ACPI */
2299 * This function currently is only a helper for the i386 smp boot process where
2300 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2301 * so mask in all cases should simply be TARGET_CPUS
2304 void __init setup_ioapic_dest(void)
2306 int pin, ioapic, irq, irq_entry;
2308 if (skip_ioapic_setup == 1)
2311 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2312 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2313 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2314 if (irq_entry == -1)
2316 irq = pin_2_irq(irq_entry, ioapic, pin);
2318 /* setup_IO_APIC_irqs could fail to get vector for some device
2319 * when you have too many devices, because at that time only boot
2322 if (!irq_cfg[irq].vector)
2323 setup_IO_APIC_irq(ioapic, pin, irq,
2324 irq_trigger(irq_entry),
2325 irq_polarity(irq_entry));
2327 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2334 #define IOAPIC_RESOURCE_NAME_SIZE 11
2336 static struct resource *ioapic_resources;
2338 static struct resource * __init ioapic_setup_resources(void)
2341 struct resource *res;
2345 if (nr_ioapics <= 0)
2348 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
2351 mem = alloc_bootmem(n);
2355 mem += sizeof(struct resource) * nr_ioapics;
2357 for (i = 0; i < nr_ioapics; i++) {
2359 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
2360 sprintf(mem, "IOAPIC %u", i);
2361 mem += IOAPIC_RESOURCE_NAME_SIZE;
2365 ioapic_resources = res;
2370 void __init ioapic_init_mappings(void)
2372 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2373 struct resource *ioapic_res;
2376 ioapic_res = ioapic_setup_resources();
2377 for (i = 0; i < nr_ioapics; i++) {
2378 if (smp_found_config) {
2379 ioapic_phys = mp_ioapics[i].mp_apicaddr;
2381 ioapic_phys = (unsigned long)
2382 alloc_bootmem_pages(PAGE_SIZE);
2383 ioapic_phys = __pa(ioapic_phys);
2385 set_fixmap_nocache(idx, ioapic_phys);
2386 apic_printk(APIC_VERBOSE,
2387 "mapped IOAPIC to %016lx (%016lx)\n",
2388 __fix_to_virt(idx), ioapic_phys);
2391 if (ioapic_res != NULL) {
2392 ioapic_res->start = ioapic_phys;
2393 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
2399 static int __init ioapic_insert_resources(void)
2402 struct resource *r = ioapic_resources;
2406 "IO APIC resources could be not be allocated.\n");
2410 for (i = 0; i < nr_ioapics; i++) {
2411 insert_resource(&iomem_resource, r);
2418 /* Insert the IO APIC resources after PCI initialization has occured to handle
2419 * IO APICS that are mapped in on a BAR in PCI space. */
2420 late_initcall(ioapic_insert_resources);