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/smp_lock.h>
29 #include <linux/pci.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
33 #include <linux/msi.h>
34 #include <linux/htirq.h>
36 #include <acpi/acpi_bus.h>
42 #include <asm/proto.h>
43 #include <asm/mach_apic.h>
47 #include <asm/msidef.h>
48 #include <asm/hypertransport.h>
50 static int assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result);
52 #define __apicdebuginit __init
54 int sis_apic_bug; /* not actually supported, dummy for compile */
56 static int no_timer_check;
58 static int disable_timer_pin_1 __initdata;
60 int timer_over_8254 __initdata = 1;
62 /* Where if anywhere is the i8259 connect in external int mode */
63 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
65 static DEFINE_SPINLOCK(ioapic_lock);
66 DEFINE_SPINLOCK(vector_lock);
69 * # of IRQ routing registers
71 int nr_ioapic_registers[MAX_IO_APICS];
74 * Rough estimation of how many shared IRQs there are, can
77 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
78 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
81 * This is performance-critical, we want to do it O(1)
83 * the indexing order of this array favors 1:1 mappings
84 * between pins and IRQs.
87 static struct irq_pin_list {
88 short apic, pin, next;
89 } irq_2_pin[PIN_MAP_SIZE];
93 unsigned int unused[3];
97 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
99 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
100 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
103 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
105 struct io_apic __iomem *io_apic = io_apic_base(apic);
106 writel(reg, &io_apic->index);
107 return readl(&io_apic->data);
110 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
112 struct io_apic __iomem *io_apic = io_apic_base(apic);
113 writel(reg, &io_apic->index);
114 writel(value, &io_apic->data);
118 * Re-write a value: to be used for read-modify-write
119 * cycles where the read already set up the index register.
121 static inline void io_apic_modify(unsigned int apic, unsigned int value)
123 struct io_apic __iomem *io_apic = io_apic_base(apic);
124 writel(value, &io_apic->data);
128 * Synchronize the IO-APIC and the CPU by doing
129 * a dummy read from the IO-APIC
131 static inline void io_apic_sync(unsigned int apic)
133 struct io_apic __iomem *io_apic = io_apic_base(apic);
134 readl(&io_apic->data);
137 #define __DO_ACTION(R, ACTION, FINAL) \
141 struct irq_pin_list *entry = irq_2_pin + irq; \
143 BUG_ON(irq >= NR_IRQS); \
149 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
151 io_apic_modify(entry->apic, reg); \
154 entry = irq_2_pin + entry->next; \
160 struct { u32 w1, w2; };
161 struct IO_APIC_route_entry entry;
164 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
166 union entry_union eu;
168 spin_lock_irqsave(&ioapic_lock, flags);
169 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
170 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
171 spin_unlock_irqrestore(&ioapic_lock, flags);
176 * When we write a new IO APIC routing entry, we need to write the high
177 * word first! If the mask bit in the low word is clear, we will enable
178 * the interrupt, and we need to make sure the entry is fully populated
179 * before that happens.
182 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
184 union entry_union eu;
186 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
187 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
190 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
193 spin_lock_irqsave(&ioapic_lock, flags);
194 __ioapic_write_entry(apic, pin, e);
195 spin_unlock_irqrestore(&ioapic_lock, flags);
199 * When we mask an IO APIC routing entry, we need to write the low
200 * word first, in order to set the mask bit before we change the
203 static void ioapic_mask_entry(int apic, int pin)
206 union entry_union eu = { .entry.mask = 1 };
208 spin_lock_irqsave(&ioapic_lock, flags);
209 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
210 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
211 spin_unlock_irqrestore(&ioapic_lock, flags);
215 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
218 struct irq_pin_list *entry = irq_2_pin + irq;
220 BUG_ON(irq >= NR_IRQS);
227 io_apic_write(apic, 0x11 + pin*2, dest);
228 reg = io_apic_read(apic, 0x10 + pin*2);
231 io_apic_modify(apic, reg);
234 entry = irq_2_pin + entry->next;
238 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
245 cpus_and(tmp, mask, cpu_online_map);
249 cpus_and(mask, tmp, CPU_MASK_ALL);
251 vector = assign_irq_vector(irq, mask, &tmp);
255 dest = cpu_mask_to_apicid(tmp);
258 * Only the high 8 bits are valid.
260 dest = SET_APIC_LOGICAL_ID(dest);
262 spin_lock_irqsave(&ioapic_lock, flags);
263 __target_IO_APIC_irq(irq, dest, vector);
264 set_native_irq_info(irq, mask);
265 spin_unlock_irqrestore(&ioapic_lock, flags);
270 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
271 * shared ISA-space IRQs, so we have to support them. We are super
272 * fast in the common case, and fast for shared ISA-space IRQs.
274 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
276 static int first_free_entry = NR_IRQS;
277 struct irq_pin_list *entry = irq_2_pin + irq;
279 BUG_ON(irq >= NR_IRQS);
281 entry = irq_2_pin + entry->next;
283 if (entry->pin != -1) {
284 entry->next = first_free_entry;
285 entry = irq_2_pin + entry->next;
286 if (++first_free_entry >= PIN_MAP_SIZE)
287 panic("io_apic.c: ran out of irq_2_pin entries!");
294 #define DO_ACTION(name,R,ACTION, FINAL) \
296 static void name##_IO_APIC_irq (unsigned int irq) \
297 __DO_ACTION(R, ACTION, FINAL)
299 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
301 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
304 static void mask_IO_APIC_irq (unsigned int irq)
308 spin_lock_irqsave(&ioapic_lock, flags);
309 __mask_IO_APIC_irq(irq);
310 spin_unlock_irqrestore(&ioapic_lock, flags);
313 static void unmask_IO_APIC_irq (unsigned int irq)
317 spin_lock_irqsave(&ioapic_lock, flags);
318 __unmask_IO_APIC_irq(irq);
319 spin_unlock_irqrestore(&ioapic_lock, flags);
322 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
324 struct IO_APIC_route_entry entry;
326 /* Check delivery_mode to be sure we're not clearing an SMI pin */
327 entry = ioapic_read_entry(apic, pin);
328 if (entry.delivery_mode == dest_SMI)
331 * Disable it in the IO-APIC irq-routing table:
333 ioapic_mask_entry(apic, pin);
336 static void clear_IO_APIC (void)
340 for (apic = 0; apic < nr_ioapics; apic++)
341 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
342 clear_IO_APIC_pin(apic, pin);
345 int skip_ioapic_setup;
348 /* dummy parsing: see setup.c */
350 static int __init disable_ioapic_setup(char *str)
352 skip_ioapic_setup = 1;
355 early_param("noapic", disable_ioapic_setup);
357 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
358 static int __init disable_timer_pin_setup(char *arg)
360 disable_timer_pin_1 = 1;
363 __setup("disable_timer_pin_1", disable_timer_pin_setup);
365 static int __init setup_disable_8254_timer(char *s)
367 timer_over_8254 = -1;
370 static int __init setup_enable_8254_timer(char *s)
376 __setup("disable_8254_timer", setup_disable_8254_timer);
377 __setup("enable_8254_timer", setup_enable_8254_timer);
381 * Find the IRQ entry number of a certain pin.
383 static int find_irq_entry(int apic, int pin, int type)
387 for (i = 0; i < mp_irq_entries; i++)
388 if (mp_irqs[i].mpc_irqtype == type &&
389 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
390 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
391 mp_irqs[i].mpc_dstirq == pin)
398 * Find the pin to which IRQ[irq] (ISA) is connected
400 static int __init find_isa_irq_pin(int irq, int type)
404 for (i = 0; i < mp_irq_entries; i++) {
405 int lbus = mp_irqs[i].mpc_srcbus;
407 if (test_bit(lbus, mp_bus_not_pci) &&
408 (mp_irqs[i].mpc_irqtype == type) &&
409 (mp_irqs[i].mpc_srcbusirq == irq))
411 return mp_irqs[i].mpc_dstirq;
416 static int __init find_isa_irq_apic(int irq, int type)
420 for (i = 0; i < mp_irq_entries; i++) {
421 int lbus = mp_irqs[i].mpc_srcbus;
423 if (test_bit(lbus, mp_bus_not_pci) &&
424 (mp_irqs[i].mpc_irqtype == type) &&
425 (mp_irqs[i].mpc_srcbusirq == irq))
428 if (i < mp_irq_entries) {
430 for(apic = 0; apic < nr_ioapics; apic++) {
431 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
440 * Find a specific PCI IRQ entry.
441 * Not an __init, possibly needed by modules
443 static int pin_2_irq(int idx, int apic, int pin);
445 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
447 int apic, i, best_guess = -1;
449 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
451 if (mp_bus_id_to_pci_bus[bus] == -1) {
452 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
455 for (i = 0; i < mp_irq_entries; i++) {
456 int lbus = mp_irqs[i].mpc_srcbus;
458 for (apic = 0; apic < nr_ioapics; apic++)
459 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
460 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
463 if (!test_bit(lbus, mp_bus_not_pci) &&
464 !mp_irqs[i].mpc_irqtype &&
466 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
467 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
469 if (!(apic || IO_APIC_IRQ(irq)))
472 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
475 * Use the first all-but-pin matching entry as a
476 * best-guess fuzzy result for broken mptables.
482 BUG_ON(best_guess >= NR_IRQS);
486 /* ISA interrupts are always polarity zero edge triggered,
487 * when listed as conforming in the MP table. */
489 #define default_ISA_trigger(idx) (0)
490 #define default_ISA_polarity(idx) (0)
492 /* PCI interrupts are always polarity one level triggered,
493 * when listed as conforming in the MP table. */
495 #define default_PCI_trigger(idx) (1)
496 #define default_PCI_polarity(idx) (1)
498 static int __init MPBIOS_polarity(int idx)
500 int bus = mp_irqs[idx].mpc_srcbus;
504 * Determine IRQ line polarity (high active or low active):
506 switch (mp_irqs[idx].mpc_irqflag & 3)
508 case 0: /* conforms, ie. bus-type dependent polarity */
509 if (test_bit(bus, mp_bus_not_pci))
510 polarity = default_ISA_polarity(idx);
512 polarity = default_PCI_polarity(idx);
514 case 1: /* high active */
519 case 2: /* reserved */
521 printk(KERN_WARNING "broken BIOS!!\n");
525 case 3: /* low active */
530 default: /* invalid */
532 printk(KERN_WARNING "broken BIOS!!\n");
540 static int MPBIOS_trigger(int idx)
542 int bus = mp_irqs[idx].mpc_srcbus;
546 * Determine IRQ trigger mode (edge or level sensitive):
548 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
550 case 0: /* conforms, ie. bus-type dependent */
551 if (test_bit(bus, mp_bus_not_pci))
552 trigger = default_ISA_trigger(idx);
554 trigger = default_PCI_trigger(idx);
561 case 2: /* reserved */
563 printk(KERN_WARNING "broken BIOS!!\n");
572 default: /* invalid */
574 printk(KERN_WARNING "broken BIOS!!\n");
582 static inline int irq_polarity(int idx)
584 return MPBIOS_polarity(idx);
587 static inline int irq_trigger(int idx)
589 return MPBIOS_trigger(idx);
592 static int pin_2_irq(int idx, int apic, int pin)
595 int bus = mp_irqs[idx].mpc_srcbus;
598 * Debugging check, we are in big trouble if this message pops up!
600 if (mp_irqs[idx].mpc_dstirq != pin)
601 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
603 if (test_bit(bus, mp_bus_not_pci)) {
604 irq = mp_irqs[idx].mpc_srcbusirq;
607 * PCI IRQs are mapped in order
611 irq += nr_ioapic_registers[i++];
614 BUG_ON(irq >= NR_IRQS);
618 static inline int IO_APIC_irq_trigger(int irq)
622 for (apic = 0; apic < nr_ioapics; apic++) {
623 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
624 idx = find_irq_entry(apic,pin,mp_INT);
625 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
626 return irq_trigger(idx);
630 * nonexistent IRQs are edge default
635 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
636 static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = {
637 [0] = FIRST_EXTERNAL_VECTOR + 0,
638 [1] = FIRST_EXTERNAL_VECTOR + 1,
639 [2] = FIRST_EXTERNAL_VECTOR + 2,
640 [3] = FIRST_EXTERNAL_VECTOR + 3,
641 [4] = FIRST_EXTERNAL_VECTOR + 4,
642 [5] = FIRST_EXTERNAL_VECTOR + 5,
643 [6] = FIRST_EXTERNAL_VECTOR + 6,
644 [7] = FIRST_EXTERNAL_VECTOR + 7,
645 [8] = FIRST_EXTERNAL_VECTOR + 8,
646 [9] = FIRST_EXTERNAL_VECTOR + 9,
647 [10] = FIRST_EXTERNAL_VECTOR + 10,
648 [11] = FIRST_EXTERNAL_VECTOR + 11,
649 [12] = FIRST_EXTERNAL_VECTOR + 12,
650 [13] = FIRST_EXTERNAL_VECTOR + 13,
651 [14] = FIRST_EXTERNAL_VECTOR + 14,
652 [15] = FIRST_EXTERNAL_VECTOR + 15,
655 static cpumask_t irq_domain[NR_IRQ_VECTORS] __read_mostly = {
674 static int __assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result)
677 * NOTE! The local APIC isn't very good at handling
678 * multiple interrupts at the same interrupt level.
679 * As the interrupt level is determined by taking the
680 * vector number and shifting that right by 4, we
681 * want to spread these out a bit so that they don't
682 * all fall in the same interrupt level.
684 * Also, we've got to be careful not to trash gate
685 * 0x80, because int 0x80 is hm, kind of importantish. ;)
687 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
691 BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
693 /* Only try and allocate irqs on cpus that are present */
694 cpus_and(mask, mask, cpu_online_map);
696 if (irq_vector[irq] > 0)
697 old_vector = irq_vector[irq];
698 if (old_vector > 0) {
699 cpus_and(*result, irq_domain[irq], mask);
700 if (!cpus_empty(*result))
704 for_each_cpu_mask(cpu, mask) {
705 cpumask_t domain, new_mask;
709 domain = vector_allocation_domain(cpu);
710 cpus_and(new_mask, domain, cpu_online_map);
712 vector = current_vector;
713 offset = current_offset;
716 if (vector >= FIRST_SYSTEM_VECTOR) {
717 /* If we run out of vectors on large boxen, must share them. */
718 offset = (offset + 1) % 8;
719 vector = FIRST_DEVICE_VECTOR + offset;
721 if (unlikely(current_vector == vector))
723 if (vector == IA32_SYSCALL_VECTOR)
725 for_each_cpu_mask(new_cpu, new_mask)
726 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
729 current_vector = vector;
730 current_offset = offset;
731 if (old_vector >= 0) {
734 cpus_and(old_mask, irq_domain[irq], cpu_online_map);
735 for_each_cpu_mask(old_cpu, old_mask)
736 per_cpu(vector_irq, old_cpu)[old_vector] = -1;
738 for_each_cpu_mask(new_cpu, new_mask)
739 per_cpu(vector_irq, new_cpu)[vector] = irq;
740 irq_vector[irq] = vector;
741 irq_domain[irq] = domain;
742 cpus_and(*result, domain, mask);
748 static int assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result)
753 spin_lock_irqsave(&vector_lock, flags);
754 vector = __assign_irq_vector(irq, mask, result);
755 spin_unlock_irqrestore(&vector_lock, flags);
759 static void __clear_irq_vector(int irq)
764 BUG_ON(!irq_vector[irq]);
766 vector = irq_vector[irq];
767 cpus_and(mask, irq_domain[irq], cpu_online_map);
768 for_each_cpu_mask(cpu, mask)
769 per_cpu(vector_irq, cpu)[vector] = -1;
772 irq_domain[irq] = CPU_MASK_NONE;
775 void __setup_vector_irq(int cpu)
777 /* Initialize vector_irq on a new cpu */
778 /* This function must be called with vector_lock held */
781 /* Mark the inuse vectors */
782 for (irq = 0; irq < NR_IRQ_VECTORS; ++irq) {
783 if (!cpu_isset(cpu, irq_domain[irq]))
785 vector = irq_vector[irq];
786 per_cpu(vector_irq, cpu)[vector] = irq;
788 /* Mark the free vectors */
789 for (vector = 0; vector < NR_VECTORS; ++vector) {
790 irq = per_cpu(vector_irq, cpu)[vector];
793 if (!cpu_isset(cpu, irq_domain[irq]))
794 per_cpu(vector_irq, cpu)[vector] = -1;
799 extern void (*interrupt[NR_IRQS])(void);
801 static struct irq_chip ioapic_chip;
803 #define IOAPIC_AUTO -1
804 #define IOAPIC_EDGE 0
805 #define IOAPIC_LEVEL 1
807 static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
809 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
810 trigger == IOAPIC_LEVEL)
811 set_irq_chip_and_handler_name(irq, &ioapic_chip,
812 handle_fasteoi_irq, "fasteoi");
814 irq_desc[irq].status |= IRQ_DELAYED_DISABLE;
815 set_irq_chip_and_handler_name(irq, &ioapic_chip,
816 handle_edge_irq, "edge");
819 static void __init setup_IO_APIC_irq(int apic, int pin, int idx, int irq)
821 struct IO_APIC_route_entry entry;
827 * add it to the IO-APIC irq-routing table:
829 memset(&entry,0,sizeof(entry));
831 entry.delivery_mode = INT_DELIVERY_MODE;
832 entry.dest_mode = INT_DEST_MODE;
833 entry.mask = 0; /* enable IRQ */
834 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
836 entry.trigger = irq_trigger(idx);
837 entry.polarity = irq_polarity(idx);
839 if (irq_trigger(idx)) {
842 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
845 if (!apic && !IO_APIC_IRQ(irq))
848 if (IO_APIC_IRQ(irq)) {
850 vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
854 entry.dest = cpu_mask_to_apicid(mask);
855 entry.vector = vector;
857 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
858 if (!apic && (irq < 16))
859 disable_8259A_irq(irq);
862 ioapic_write_entry(apic, pin, entry);
864 spin_lock_irqsave(&ioapic_lock, flags);
865 set_native_irq_info(irq, TARGET_CPUS);
866 spin_unlock_irqrestore(&ioapic_lock, flags);
870 static void __init setup_IO_APIC_irqs(void)
872 int apic, pin, idx, irq, first_notcon = 1;
874 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
876 for (apic = 0; apic < nr_ioapics; apic++) {
877 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
879 idx = find_irq_entry(apic,pin,mp_INT);
882 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
885 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
889 irq = pin_2_irq(idx, apic, pin);
890 add_pin_to_irq(irq, apic, pin);
892 setup_IO_APIC_irq(apic, pin, idx, irq);
898 apic_printk(APIC_VERBOSE," not connected.\n");
902 * Set up the 8259A-master output pin as broadcast to all
905 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
907 struct IO_APIC_route_entry entry;
910 memset(&entry,0,sizeof(entry));
912 disable_8259A_irq(0);
915 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
918 * We use logical delivery to get the timer IRQ
921 entry.dest_mode = INT_DEST_MODE;
922 entry.mask = 0; /* unmask IRQ now */
923 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
924 entry.delivery_mode = INT_DELIVERY_MODE;
927 entry.vector = vector;
930 * The timer IRQ doesn't have to know that behind the
931 * scene we have a 8259A-master in AEOI mode ...
933 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
936 * Add it to the IO-APIC irq-routing table:
938 spin_lock_irqsave(&ioapic_lock, flags);
939 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
940 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
941 spin_unlock_irqrestore(&ioapic_lock, flags);
946 void __init UNEXPECTED_IO_APIC(void)
950 void __apicdebuginit print_IO_APIC(void)
953 union IO_APIC_reg_00 reg_00;
954 union IO_APIC_reg_01 reg_01;
955 union IO_APIC_reg_02 reg_02;
958 if (apic_verbosity == APIC_QUIET)
961 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
962 for (i = 0; i < nr_ioapics; i++)
963 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
964 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
967 * We are a bit conservative about what we expect. We have to
968 * know about every hardware change ASAP.
970 printk(KERN_INFO "testing the IO APIC.......................\n");
972 for (apic = 0; apic < nr_ioapics; apic++) {
974 spin_lock_irqsave(&ioapic_lock, flags);
975 reg_00.raw = io_apic_read(apic, 0);
976 reg_01.raw = io_apic_read(apic, 1);
977 if (reg_01.bits.version >= 0x10)
978 reg_02.raw = io_apic_read(apic, 2);
979 spin_unlock_irqrestore(&ioapic_lock, flags);
982 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
983 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
984 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
985 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
986 UNEXPECTED_IO_APIC();
988 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
989 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
990 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
991 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
992 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
993 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
994 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
995 (reg_01.bits.entries != 0x2E) &&
996 (reg_01.bits.entries != 0x3F) &&
997 (reg_01.bits.entries != 0x03)
999 UNEXPECTED_IO_APIC();
1001 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1002 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1003 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1004 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
1005 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1006 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1007 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1008 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1010 UNEXPECTED_IO_APIC();
1011 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1012 UNEXPECTED_IO_APIC();
1014 if (reg_01.bits.version >= 0x10) {
1015 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1016 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1017 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1018 UNEXPECTED_IO_APIC();
1021 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1023 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1024 " Stat Dmod Deli Vect: \n");
1026 for (i = 0; i <= reg_01.bits.entries; i++) {
1027 struct IO_APIC_route_entry entry;
1029 entry = ioapic_read_entry(apic, i);
1031 printk(KERN_DEBUG " %02x %03X ",
1036 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1041 entry.delivery_status,
1043 entry.delivery_mode,
1048 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1049 for (i = 0; i < NR_IRQS; i++) {
1050 struct irq_pin_list *entry = irq_2_pin + i;
1053 printk(KERN_DEBUG "IRQ%d ", i);
1055 printk("-> %d:%d", entry->apic, entry->pin);
1058 entry = irq_2_pin + entry->next;
1063 printk(KERN_INFO ".................................... done.\n");
1070 static __apicdebuginit void print_APIC_bitfield (int base)
1075 if (apic_verbosity == APIC_QUIET)
1078 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1079 for (i = 0; i < 8; i++) {
1080 v = apic_read(base + i*0x10);
1081 for (j = 0; j < 32; j++) {
1091 void __apicdebuginit print_local_APIC(void * dummy)
1093 unsigned int v, ver, maxlvt;
1095 if (apic_verbosity == APIC_QUIET)
1098 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1099 smp_processor_id(), hard_smp_processor_id());
1100 v = apic_read(APIC_ID);
1101 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1102 v = apic_read(APIC_LVR);
1103 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1104 ver = GET_APIC_VERSION(v);
1105 maxlvt = get_maxlvt();
1107 v = apic_read(APIC_TASKPRI);
1108 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1110 v = apic_read(APIC_ARBPRI);
1111 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1112 v & APIC_ARBPRI_MASK);
1113 v = apic_read(APIC_PROCPRI);
1114 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1116 v = apic_read(APIC_EOI);
1117 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1118 v = apic_read(APIC_RRR);
1119 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1120 v = apic_read(APIC_LDR);
1121 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1122 v = apic_read(APIC_DFR);
1123 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1124 v = apic_read(APIC_SPIV);
1125 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1127 printk(KERN_DEBUG "... APIC ISR field:\n");
1128 print_APIC_bitfield(APIC_ISR);
1129 printk(KERN_DEBUG "... APIC TMR field:\n");
1130 print_APIC_bitfield(APIC_TMR);
1131 printk(KERN_DEBUG "... APIC IRR field:\n");
1132 print_APIC_bitfield(APIC_IRR);
1134 v = apic_read(APIC_ESR);
1135 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1137 v = apic_read(APIC_ICR);
1138 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1139 v = apic_read(APIC_ICR2);
1140 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1142 v = apic_read(APIC_LVTT);
1143 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1145 if (maxlvt > 3) { /* PC is LVT#4. */
1146 v = apic_read(APIC_LVTPC);
1147 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1149 v = apic_read(APIC_LVT0);
1150 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1151 v = apic_read(APIC_LVT1);
1152 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1154 if (maxlvt > 2) { /* ERR is LVT#3. */
1155 v = apic_read(APIC_LVTERR);
1156 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1159 v = apic_read(APIC_TMICT);
1160 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1161 v = apic_read(APIC_TMCCT);
1162 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1163 v = apic_read(APIC_TDCR);
1164 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1168 void print_all_local_APICs (void)
1170 on_each_cpu(print_local_APIC, NULL, 1, 1);
1173 void __apicdebuginit print_PIC(void)
1176 unsigned long flags;
1178 if (apic_verbosity == APIC_QUIET)
1181 printk(KERN_DEBUG "\nprinting PIC contents\n");
1183 spin_lock_irqsave(&i8259A_lock, flags);
1185 v = inb(0xa1) << 8 | inb(0x21);
1186 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1188 v = inb(0xa0) << 8 | inb(0x20);
1189 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1193 v = inb(0xa0) << 8 | inb(0x20);
1197 spin_unlock_irqrestore(&i8259A_lock, flags);
1199 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1201 v = inb(0x4d1) << 8 | inb(0x4d0);
1202 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1207 static void __init enable_IO_APIC(void)
1209 union IO_APIC_reg_01 reg_01;
1210 int i8259_apic, i8259_pin;
1212 unsigned long flags;
1214 for (i = 0; i < PIN_MAP_SIZE; i++) {
1215 irq_2_pin[i].pin = -1;
1216 irq_2_pin[i].next = 0;
1220 * The number of IO-APIC IRQ registers (== #pins):
1222 for (apic = 0; apic < nr_ioapics; apic++) {
1223 spin_lock_irqsave(&ioapic_lock, flags);
1224 reg_01.raw = io_apic_read(apic, 1);
1225 spin_unlock_irqrestore(&ioapic_lock, flags);
1226 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1228 for(apic = 0; apic < nr_ioapics; apic++) {
1230 /* See if any of the pins is in ExtINT mode */
1231 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1232 struct IO_APIC_route_entry entry;
1233 entry = ioapic_read_entry(apic, pin);
1235 /* If the interrupt line is enabled and in ExtInt mode
1236 * I have found the pin where the i8259 is connected.
1238 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1239 ioapic_i8259.apic = apic;
1240 ioapic_i8259.pin = pin;
1246 /* Look to see what if the MP table has reported the ExtINT */
1247 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1248 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1249 /* Trust the MP table if nothing is setup in the hardware */
1250 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1251 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1252 ioapic_i8259.pin = i8259_pin;
1253 ioapic_i8259.apic = i8259_apic;
1255 /* Complain if the MP table and the hardware disagree */
1256 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1257 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1259 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1263 * Do not trust the IO-APIC being empty at bootup
1269 * Not an __init, needed by the reboot code
1271 void disable_IO_APIC(void)
1274 * Clear the IO-APIC before rebooting:
1279 * If the i8259 is routed through an IOAPIC
1280 * Put that IOAPIC in virtual wire mode
1281 * so legacy interrupts can be delivered.
1283 if (ioapic_i8259.pin != -1) {
1284 struct IO_APIC_route_entry entry;
1286 memset(&entry, 0, sizeof(entry));
1287 entry.mask = 0; /* Enabled */
1288 entry.trigger = 0; /* Edge */
1290 entry.polarity = 0; /* High */
1291 entry.delivery_status = 0;
1292 entry.dest_mode = 0; /* Physical */
1293 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1295 entry.dest = GET_APIC_ID(apic_read(APIC_ID));
1298 * Add it to the IO-APIC irq-routing table:
1300 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1303 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1307 * There is a nasty bug in some older SMP boards, their mptable lies
1308 * about the timer IRQ. We do the following to work around the situation:
1310 * - timer IRQ defaults to IO-APIC IRQ
1311 * - if this function detects that timer IRQs are defunct, then we fall
1312 * back to ISA timer IRQs
1314 static int __init timer_irq_works(void)
1316 unsigned long t1 = jiffies;
1319 /* Let ten ticks pass... */
1320 mdelay((10 * 1000) / HZ);
1323 * Expect a few ticks at least, to be sure some possible
1324 * glue logic does not lock up after one or two first
1325 * ticks in a non-ExtINT mode. Also the local APIC
1326 * might have cached one ExtINT interrupt. Finally, at
1327 * least one tick may be lost due to delays.
1331 if (jiffies - t1 > 4)
1337 * In the SMP+IOAPIC case it might happen that there are an unspecified
1338 * number of pending IRQ events unhandled. These cases are very rare,
1339 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1340 * better to do it this way as thus we do not have to be aware of
1341 * 'pending' interrupts in the IRQ path, except at this point.
1344 * Edge triggered needs to resend any interrupt
1345 * that was delayed but this is now handled in the device
1350 * Starting up a edge-triggered IO-APIC interrupt is
1351 * nasty - we need to make sure that we get the edge.
1352 * If it is already asserted for some reason, we need
1353 * return 1 to indicate that is was pending.
1355 * This is not complete - we should be able to fake
1356 * an edge even if it isn't on the 8259A...
1359 static unsigned int startup_ioapic_irq(unsigned int irq)
1361 int was_pending = 0;
1362 unsigned long flags;
1364 spin_lock_irqsave(&ioapic_lock, flags);
1366 disable_8259A_irq(irq);
1367 if (i8259A_irq_pending(irq))
1370 __unmask_IO_APIC_irq(irq);
1371 spin_unlock_irqrestore(&ioapic_lock, flags);
1376 static int ioapic_retrigger_irq(unsigned int irq)
1380 unsigned long flags;
1382 spin_lock_irqsave(&vector_lock, flags);
1383 vector = irq_vector[irq];
1385 cpu_set(first_cpu(irq_domain[irq]), mask);
1387 send_IPI_mask(mask, vector);
1388 spin_unlock_irqrestore(&vector_lock, flags);
1394 * Level and edge triggered IO-APIC interrupts need different handling,
1395 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1396 * handled with the level-triggered descriptor, but that one has slightly
1397 * more overhead. Level-triggered interrupts cannot be handled with the
1398 * edge-triggered handler, without risking IRQ storms and other ugly
1402 static void ack_apic_edge(unsigned int irq)
1404 move_native_irq(irq);
1408 static void ack_apic_level(unsigned int irq)
1410 int do_unmask_irq = 0;
1412 #if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
1413 /* If we are moving the irq we need to mask it */
1414 if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1416 mask_IO_APIC_irq(irq);
1421 * We must acknowledge the irq before we move it or the acknowledge will
1422 * not propogate properly.
1426 /* Now we can move and renable the irq */
1427 move_masked_irq(irq);
1428 if (unlikely(do_unmask_irq))
1429 unmask_IO_APIC_irq(irq);
1432 static struct irq_chip ioapic_chip __read_mostly = {
1434 .startup = startup_ioapic_irq,
1435 .mask = mask_IO_APIC_irq,
1436 .unmask = unmask_IO_APIC_irq,
1437 .ack = ack_apic_edge,
1438 .eoi = ack_apic_level,
1440 .set_affinity = set_ioapic_affinity_irq,
1442 .retrigger = ioapic_retrigger_irq,
1445 static inline void init_IO_APIC_traps(void)
1450 * NOTE! The local APIC isn't very good at handling
1451 * multiple interrupts at the same interrupt level.
1452 * As the interrupt level is determined by taking the
1453 * vector number and shifting that right by 4, we
1454 * want to spread these out a bit so that they don't
1455 * all fall in the same interrupt level.
1457 * Also, we've got to be careful not to trash gate
1458 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1460 for (irq = 0; irq < NR_IRQS ; irq++) {
1462 if (IO_APIC_IRQ(tmp) && !irq_vector[tmp]) {
1464 * Hmm.. We don't have an entry for this,
1465 * so default to an old-fashioned 8259
1466 * interrupt if we can..
1469 make_8259A_irq(irq);
1471 /* Strange. Oh, well.. */
1472 irq_desc[irq].chip = &no_irq_chip;
1477 static void enable_lapic_irq (unsigned int irq)
1481 v = apic_read(APIC_LVT0);
1482 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1485 static void disable_lapic_irq (unsigned int irq)
1489 v = apic_read(APIC_LVT0);
1490 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1493 static void ack_lapic_irq (unsigned int irq)
1498 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1500 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1501 .typename = "local-APIC-edge",
1502 .startup = NULL, /* startup_irq() not used for IRQ0 */
1503 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1504 .enable = enable_lapic_irq,
1505 .disable = disable_lapic_irq,
1506 .ack = ack_lapic_irq,
1507 .end = end_lapic_irq,
1510 static void setup_nmi (void)
1513 * Dirty trick to enable the NMI watchdog ...
1514 * We put the 8259A master into AEOI mode and
1515 * unmask on all local APICs LVT0 as NMI.
1517 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1518 * is from Maciej W. Rozycki - so we do not have to EOI from
1519 * the NMI handler or the timer interrupt.
1521 printk(KERN_INFO "activating NMI Watchdog ...");
1523 enable_NMI_through_LVT0(NULL);
1529 * This looks a bit hackish but it's about the only one way of sending
1530 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1531 * not support the ExtINT mode, unfortunately. We need to send these
1532 * cycles as some i82489DX-based boards have glue logic that keeps the
1533 * 8259A interrupt line asserted until INTA. --macro
1535 static inline void unlock_ExtINT_logic(void)
1538 struct IO_APIC_route_entry entry0, entry1;
1539 unsigned char save_control, save_freq_select;
1540 unsigned long flags;
1542 pin = find_isa_irq_pin(8, mp_INT);
1543 apic = find_isa_irq_apic(8, mp_INT);
1547 spin_lock_irqsave(&ioapic_lock, flags);
1548 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1549 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1550 spin_unlock_irqrestore(&ioapic_lock, flags);
1551 clear_IO_APIC_pin(apic, pin);
1553 memset(&entry1, 0, sizeof(entry1));
1555 entry1.dest_mode = 0; /* physical delivery */
1556 entry1.mask = 0; /* unmask IRQ now */
1557 entry1.dest = hard_smp_processor_id();
1558 entry1.delivery_mode = dest_ExtINT;
1559 entry1.polarity = entry0.polarity;
1563 spin_lock_irqsave(&ioapic_lock, flags);
1564 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1565 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1566 spin_unlock_irqrestore(&ioapic_lock, flags);
1568 save_control = CMOS_READ(RTC_CONTROL);
1569 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1570 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1572 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1577 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1581 CMOS_WRITE(save_control, RTC_CONTROL);
1582 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1583 clear_IO_APIC_pin(apic, pin);
1585 spin_lock_irqsave(&ioapic_lock, flags);
1586 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1587 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1588 spin_unlock_irqrestore(&ioapic_lock, flags);
1592 * This code may look a bit paranoid, but it's supposed to cooperate with
1593 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1594 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1595 * fanatically on his truly buggy board.
1597 * FIXME: really need to revamp this for modern platforms only.
1599 static inline void check_timer(void)
1601 int apic1, pin1, apic2, pin2;
1606 * get/set the timer IRQ vector:
1608 disable_8259A_irq(0);
1609 vector = assign_irq_vector(0, TARGET_CPUS, &mask);
1612 * Subtle, code in do_timer_interrupt() expects an AEOI
1613 * mode for the 8259A whenever interrupts are routed
1614 * through I/O APICs. Also IRQ0 has to be enabled in
1615 * the 8259A which implies the virtual wire has to be
1616 * disabled in the local APIC.
1618 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1620 if (timer_over_8254 > 0)
1621 enable_8259A_irq(0);
1623 pin1 = find_isa_irq_pin(0, mp_INT);
1624 apic1 = find_isa_irq_apic(0, mp_INT);
1625 pin2 = ioapic_i8259.pin;
1626 apic2 = ioapic_i8259.apic;
1628 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1629 vector, apic1, pin1, apic2, pin2);
1633 * Ok, does IRQ0 through the IOAPIC work?
1635 unmask_IO_APIC_irq(0);
1636 if (!no_timer_check && timer_irq_works()) {
1637 nmi_watchdog_default();
1638 if (nmi_watchdog == NMI_IO_APIC) {
1639 disable_8259A_irq(0);
1641 enable_8259A_irq(0);
1643 if (disable_timer_pin_1 > 0)
1644 clear_IO_APIC_pin(0, pin1);
1647 clear_IO_APIC_pin(apic1, pin1);
1648 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1649 "connected to IO-APIC\n");
1652 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1653 "through the 8259A ... ");
1655 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1658 * legacy devices should be connected to IO APIC #0
1660 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1661 if (timer_irq_works()) {
1662 apic_printk(APIC_VERBOSE," works.\n");
1663 nmi_watchdog_default();
1664 if (nmi_watchdog == NMI_IO_APIC) {
1670 * Cleanup, just in case ...
1672 clear_IO_APIC_pin(apic2, pin2);
1674 apic_printk(APIC_VERBOSE," failed.\n");
1676 if (nmi_watchdog == NMI_IO_APIC) {
1677 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1681 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1683 disable_8259A_irq(0);
1684 irq_desc[0].chip = &lapic_irq_type;
1685 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1686 enable_8259A_irq(0);
1688 if (timer_irq_works()) {
1689 apic_printk(APIC_VERBOSE," works.\n");
1692 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1693 apic_printk(APIC_VERBOSE," failed.\n");
1695 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1699 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1701 unlock_ExtINT_logic();
1703 if (timer_irq_works()) {
1704 apic_printk(APIC_VERBOSE," works.\n");
1707 apic_printk(APIC_VERBOSE," failed :(.\n");
1708 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1711 static int __init notimercheck(char *s)
1716 __setup("no_timer_check", notimercheck);
1720 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1721 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1722 * Linux doesn't really care, as it's not actually used
1723 * for any interrupt handling anyway.
1725 #define PIC_IRQS (1<<2)
1727 void __init setup_IO_APIC(void)
1732 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1734 io_apic_irqs = ~PIC_IRQS;
1736 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1739 setup_IO_APIC_irqs();
1740 init_IO_APIC_traps();
1746 struct sysfs_ioapic_data {
1747 struct sys_device dev;
1748 struct IO_APIC_route_entry entry[0];
1750 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1752 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1754 struct IO_APIC_route_entry *entry;
1755 struct sysfs_ioapic_data *data;
1758 data = container_of(dev, struct sysfs_ioapic_data, dev);
1759 entry = data->entry;
1760 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
1761 *entry = ioapic_read_entry(dev->id, i);
1766 static int ioapic_resume(struct sys_device *dev)
1768 struct IO_APIC_route_entry *entry;
1769 struct sysfs_ioapic_data *data;
1770 unsigned long flags;
1771 union IO_APIC_reg_00 reg_00;
1774 data = container_of(dev, struct sysfs_ioapic_data, dev);
1775 entry = data->entry;
1777 spin_lock_irqsave(&ioapic_lock, flags);
1778 reg_00.raw = io_apic_read(dev->id, 0);
1779 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1780 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1781 io_apic_write(dev->id, 0, reg_00.raw);
1783 spin_unlock_irqrestore(&ioapic_lock, flags);
1784 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
1785 ioapic_write_entry(dev->id, i, entry[i]);
1790 static struct sysdev_class ioapic_sysdev_class = {
1791 set_kset_name("ioapic"),
1792 .suspend = ioapic_suspend,
1793 .resume = ioapic_resume,
1796 static int __init ioapic_init_sysfs(void)
1798 struct sys_device * dev;
1799 int i, size, error = 0;
1801 error = sysdev_class_register(&ioapic_sysdev_class);
1805 for (i = 0; i < nr_ioapics; i++ ) {
1806 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1807 * sizeof(struct IO_APIC_route_entry);
1808 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1809 if (!mp_ioapic_data[i]) {
1810 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1813 memset(mp_ioapic_data[i], 0, size);
1814 dev = &mp_ioapic_data[i]->dev;
1816 dev->cls = &ioapic_sysdev_class;
1817 error = sysdev_register(dev);
1819 kfree(mp_ioapic_data[i]);
1820 mp_ioapic_data[i] = NULL;
1821 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1829 device_initcall(ioapic_init_sysfs);
1832 * Dynamic irq allocate and deallocation
1834 int create_irq(void)
1836 /* Allocate an unused irq */
1840 unsigned long flags;
1844 spin_lock_irqsave(&vector_lock, flags);
1845 for (new = (NR_IRQS - 1); new >= 0; new--) {
1846 if (platform_legacy_irq(new))
1848 if (irq_vector[new] != 0)
1850 vector = __assign_irq_vector(new, TARGET_CPUS, &mask);
1851 if (likely(vector > 0))
1855 spin_unlock_irqrestore(&vector_lock, flags);
1858 dynamic_irq_init(irq);
1863 void destroy_irq(unsigned int irq)
1865 unsigned long flags;
1867 dynamic_irq_cleanup(irq);
1869 spin_lock_irqsave(&vector_lock, flags);
1870 __clear_irq_vector(irq);
1871 spin_unlock_irqrestore(&vector_lock, flags);
1875 * MSI mesage composition
1877 #ifdef CONFIG_PCI_MSI
1878 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
1884 vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
1886 dest = cpu_mask_to_apicid(tmp);
1888 msg->address_hi = MSI_ADDR_BASE_HI;
1891 ((INT_DEST_MODE == 0) ?
1892 MSI_ADDR_DEST_MODE_PHYSICAL:
1893 MSI_ADDR_DEST_MODE_LOGICAL) |
1894 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1895 MSI_ADDR_REDIRECTION_CPU:
1896 MSI_ADDR_REDIRECTION_LOWPRI) |
1897 MSI_ADDR_DEST_ID(dest);
1900 MSI_DATA_TRIGGER_EDGE |
1901 MSI_DATA_LEVEL_ASSERT |
1902 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1903 MSI_DATA_DELIVERY_FIXED:
1904 MSI_DATA_DELIVERY_LOWPRI) |
1905 MSI_DATA_VECTOR(vector);
1911 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
1918 cpus_and(tmp, mask, cpu_online_map);
1919 if (cpus_empty(tmp))
1922 cpus_and(mask, tmp, CPU_MASK_ALL);
1924 vector = assign_irq_vector(irq, mask, &tmp);
1928 dest = cpu_mask_to_apicid(tmp);
1930 read_msi_msg(irq, &msg);
1932 msg.data &= ~MSI_DATA_VECTOR_MASK;
1933 msg.data |= MSI_DATA_VECTOR(vector);
1934 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
1935 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
1937 write_msi_msg(irq, &msg);
1938 set_native_irq_info(irq, mask);
1940 #endif /* CONFIG_SMP */
1943 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
1944 * which implement the MSI or MSI-X Capability Structure.
1946 static struct irq_chip msi_chip = {
1948 .unmask = unmask_msi_irq,
1949 .mask = mask_msi_irq,
1950 .ack = ack_apic_edge,
1952 .set_affinity = set_msi_irq_affinity,
1954 .retrigger = ioapic_retrigger_irq,
1957 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
1965 set_irq_msi(irq, desc);
1966 ret = msi_compose_msg(dev, irq, &msg);
1972 write_msi_msg(irq, &msg);
1974 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
1979 void arch_teardown_msi_irq(unsigned int irq)
1984 #endif /* CONFIG_PCI_MSI */
1987 * Hypertransport interrupt support
1989 #ifdef CONFIG_HT_IRQ
1993 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
1995 struct ht_irq_msg msg;
1996 fetch_ht_irq_msg(irq, &msg);
1998 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
1999 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2001 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2002 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2004 write_ht_irq_msg(irq, &msg);
2007 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2013 cpus_and(tmp, mask, cpu_online_map);
2014 if (cpus_empty(tmp))
2017 cpus_and(mask, tmp, CPU_MASK_ALL);
2019 vector = assign_irq_vector(irq, mask, &tmp);
2023 dest = cpu_mask_to_apicid(tmp);
2025 target_ht_irq(irq, dest, vector);
2026 set_native_irq_info(irq, mask);
2030 static struct irq_chip ht_irq_chip = {
2032 .mask = mask_ht_irq,
2033 .unmask = unmask_ht_irq,
2034 .ack = ack_apic_edge,
2036 .set_affinity = set_ht_irq_affinity,
2038 .retrigger = ioapic_retrigger_irq,
2041 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2046 vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
2048 struct ht_irq_msg msg;
2051 dest = cpu_mask_to_apicid(tmp);
2053 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2057 HT_IRQ_LOW_DEST_ID(dest) |
2058 HT_IRQ_LOW_VECTOR(vector) |
2059 ((INT_DEST_MODE == 0) ?
2060 HT_IRQ_LOW_DM_PHYSICAL :
2061 HT_IRQ_LOW_DM_LOGICAL) |
2062 HT_IRQ_LOW_RQEOI_EDGE |
2063 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2064 HT_IRQ_LOW_MT_FIXED :
2065 HT_IRQ_LOW_MT_ARBITRATED) |
2066 HT_IRQ_LOW_IRQ_MASKED;
2068 write_ht_irq_msg(irq, &msg);
2070 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2071 handle_edge_irq, "edge");
2075 #endif /* CONFIG_HT_IRQ */
2077 /* --------------------------------------------------------------------------
2078 ACPI-based IOAPIC Configuration
2079 -------------------------------------------------------------------------- */
2083 #define IO_APIC_MAX_ID 0xFE
2085 int __init io_apic_get_redir_entries (int ioapic)
2087 union IO_APIC_reg_01 reg_01;
2088 unsigned long flags;
2090 spin_lock_irqsave(&ioapic_lock, flags);
2091 reg_01.raw = io_apic_read(ioapic, 1);
2092 spin_unlock_irqrestore(&ioapic_lock, flags);
2094 return reg_01.bits.entries;
2098 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2100 struct IO_APIC_route_entry entry;
2101 unsigned long flags;
2105 if (!IO_APIC_IRQ(irq)) {
2106 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2112 * IRQs < 16 are already in the irq_2_pin[] map
2115 add_pin_to_irq(irq, ioapic, pin);
2118 vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
2123 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2124 * Note that we mask (disable) IRQs now -- these get enabled when the
2125 * corresponding device driver registers for this IRQ.
2128 memset(&entry,0,sizeof(entry));
2130 entry.delivery_mode = INT_DELIVERY_MODE;
2131 entry.dest_mode = INT_DEST_MODE;
2132 entry.dest = cpu_mask_to_apicid(mask);
2133 entry.trigger = triggering;
2134 entry.polarity = polarity;
2135 entry.mask = 1; /* Disabled (masked) */
2136 entry.vector = vector & 0xff;
2138 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2139 "IRQ %d Mode:%i Active:%i)\n", ioapic,
2140 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2141 triggering, polarity);
2143 ioapic_register_intr(irq, entry.vector, triggering);
2145 if (!ioapic && (irq < 16))
2146 disable_8259A_irq(irq);
2148 ioapic_write_entry(ioapic, pin, entry);
2150 spin_lock_irqsave(&ioapic_lock, flags);
2151 set_native_irq_info(irq, TARGET_CPUS);
2152 spin_unlock_irqrestore(&ioapic_lock, flags);
2157 #endif /* CONFIG_ACPI */
2161 * This function currently is only a helper for the i386 smp boot process where
2162 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2163 * so mask in all cases should simply be TARGET_CPUS
2166 void __init setup_ioapic_dest(void)
2168 int pin, ioapic, irq, irq_entry;
2170 if (skip_ioapic_setup == 1)
2173 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2174 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2175 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2176 if (irq_entry == -1)
2178 irq = pin_2_irq(irq_entry, ioapic, pin);
2180 /* setup_IO_APIC_irqs could fail to get vector for some device
2181 * when you have too many devices, because at that time only boot
2184 if(!irq_vector[irq])
2185 setup_IO_APIC_irq(ioapic, pin, irq_entry, irq);
2187 set_ioapic_affinity_irq(irq, TARGET_CPUS);