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/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/sched.h>
29 #include <linux/config.h>
30 #include <linux/smp_lock.h>
31 #include <linux/mc146818rtc.h>
32 #include <linux/acpi.h>
33 #include <linux/sysdev.h>
38 #include <asm/proto.h>
40 #define __apicdebuginit __init
42 int sis_apic_bug; /* not actually supported, dummy for compile */
44 #undef APIC_LOCKUP_DEBUG
46 #define APIC_LOCKUP_DEBUG
48 static spinlock_t ioapic_lock = SPIN_LOCK_UNLOCKED;
51 * # of IRQ routing registers
53 int nr_ioapic_registers[MAX_IO_APICS];
56 * Rough estimation of how many shared IRQs there are, can
59 #define MAX_PLUS_SHARED_IRQS NR_IRQS
60 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
63 * This is performance-critical, we want to do it O(1)
65 * the indexing order of this array favors 1:1 mappings
66 * between pins and IRQs.
69 static struct irq_pin_list {
70 short apic, pin, next;
71 } irq_2_pin[PIN_MAP_SIZE];
73 int vector_irq[NR_VECTORS] = { [0 ... NR_VECTORS - 1] = -1};
75 #define vector_to_irq(vector) \
76 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
78 #define vector_to_irq(vector) (vector)
82 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
83 * shared ISA-space IRQs, so we have to support them. We are super
84 * fast in the common case, and fast for shared ISA-space IRQs.
86 static void __init add_pin_to_irq(unsigned int irq, int apic, int pin)
88 static int first_free_entry = NR_IRQS;
89 struct irq_pin_list *entry = irq_2_pin + irq;
92 entry = irq_2_pin + entry->next;
94 if (entry->pin != -1) {
95 entry->next = first_free_entry;
96 entry = irq_2_pin + entry->next;
97 if (++first_free_entry >= PIN_MAP_SIZE)
98 panic("io_apic.c: whoops");
104 #define __DO_ACTION(R, ACTION, FINAL) \
108 struct irq_pin_list *entry = irq_2_pin + irq; \
115 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
117 io_apic_modify(entry->apic, reg); \
120 entry = irq_2_pin + entry->next; \
125 #define DO_ACTION(name,R,ACTION, FINAL) \
127 static void name##_IO_APIC_irq (unsigned int irq) \
128 __DO_ACTION(R, ACTION, FINAL)
130 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
132 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
134 DO_ACTION( __mask_and_edge, 0, = (reg & 0xffff7fff) | 0x00010000, )
135 /* mask = 1, trigger = 0 */
136 DO_ACTION( __unmask_and_level, 0, = (reg & 0xfffeffff) | 0x00008000, )
137 /* mask = 0, trigger = 1 */
139 static void mask_IO_APIC_irq (unsigned int irq)
143 spin_lock_irqsave(&ioapic_lock, flags);
144 __mask_IO_APIC_irq(irq);
145 spin_unlock_irqrestore(&ioapic_lock, flags);
148 static void unmask_IO_APIC_irq (unsigned int irq)
152 spin_lock_irqsave(&ioapic_lock, flags);
153 __unmask_IO_APIC_irq(irq);
154 spin_unlock_irqrestore(&ioapic_lock, flags);
157 void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
159 struct IO_APIC_route_entry entry;
162 /* Check delivery_mode to be sure we're not clearing an SMI pin */
163 spin_lock_irqsave(&ioapic_lock, flags);
164 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
165 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
166 spin_unlock_irqrestore(&ioapic_lock, flags);
167 if (entry.delivery_mode == dest_SMI)
170 * Disable it in the IO-APIC irq-routing table:
172 memset(&entry, 0, sizeof(entry));
174 spin_lock_irqsave(&ioapic_lock, flags);
175 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
176 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
177 spin_unlock_irqrestore(&ioapic_lock, flags);
180 static void clear_IO_APIC (void)
184 for (apic = 0; apic < nr_ioapics; apic++)
185 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
186 clear_IO_APIC_pin(apic, pin);
190 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
191 * specific CPU-side IRQs.
195 int pirq_entries [MAX_PIRQS];
197 int skip_ioapic_setup;
200 /* dummy parsing: see setup.c */
202 static int __init disable_ioapic_setup(char *str)
204 skip_ioapic_setup = 1;
208 static int __init enable_ioapic_setup(char *str)
211 skip_ioapic_setup = 0;
215 __setup("noapic", disable_ioapic_setup);
216 __setup("apic", enable_ioapic_setup);
218 #include <asm/pci-direct.h>
219 #include <linux/pci_ids.h>
220 #include <linux/pci.h>
222 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
223 off. Check for an Nvidia or VIA PCI bridge and turn it off.
224 Use pci direct infrastructure because this runs before the PCI subsystem.
226 Can be overwritten with "apic"
228 And another hack to disable the IOMMU on VIA chipsets.
231 void __init check_ioapic(void)
237 /* Poor man's PCI discovery */
238 for (num = 0; num < 32; num++) {
239 for (slot = 0; slot < 32; slot++) {
240 for (func = 0; func < 8; func++) {
244 class = read_pci_config(num,slot,func,
246 if (class == 0xffffffff)
249 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
252 vendor = read_pci_config(num, slot, func,
256 case PCI_VENDOR_ID_VIA:
257 #ifdef CONFIG_GART_IOMMU
258 if ((end_pfn >= (0xffffffff>>PAGE_SHIFT) ||
260 !iommu_aperture_allowed) {
262 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
263 iommu_aperture_disabled = 1;
267 case PCI_VENDOR_ID_NVIDIA:
269 /* All timer overrides on Nvidia
270 seem to be wrong. Skip them. */
271 acpi_skip_timer_override = 1;
273 "Nvidia board detected. Ignoring ACPI timer override.\n");
275 /* RED-PEN skip them on mptables too? */
279 /* No multi-function device? */
280 type = read_pci_config_byte(num,slot,func,
289 static int __init ioapic_pirq_setup(char *str)
292 int ints[MAX_PIRQS+1];
294 get_options(str, ARRAY_SIZE(ints), ints);
296 for (i = 0; i < MAX_PIRQS; i++)
297 pirq_entries[i] = -1;
300 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
302 if (ints[0] < MAX_PIRQS)
305 for (i = 0; i < max; i++) {
306 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
308 * PIRQs are mapped upside down, usually.
310 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
315 __setup("pirq=", ioapic_pirq_setup);
318 * Find the IRQ entry number of a certain pin.
320 static int __init find_irq_entry(int apic, int pin, int type)
324 for (i = 0; i < mp_irq_entries; i++)
325 if (mp_irqs[i].mpc_irqtype == type &&
326 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
327 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
328 mp_irqs[i].mpc_dstirq == pin)
335 * Find the pin to which IRQ[irq] (ISA) is connected
337 static int find_isa_irq_pin(int irq, int type)
341 for (i = 0; i < mp_irq_entries; i++) {
342 int lbus = mp_irqs[i].mpc_srcbus;
344 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
345 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
346 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
347 (mp_irqs[i].mpc_irqtype == type) &&
348 (mp_irqs[i].mpc_srcbusirq == irq))
350 return mp_irqs[i].mpc_dstirq;
356 * Find a specific PCI IRQ entry.
357 * Not an __init, possibly needed by modules
359 static int pin_2_irq(int idx, int apic, int pin);
361 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
363 int apic, i, best_guess = -1;
365 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
367 if (mp_bus_id_to_pci_bus[bus] == -1) {
368 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
371 for (i = 0; i < mp_irq_entries; i++) {
372 int lbus = mp_irqs[i].mpc_srcbus;
374 for (apic = 0; apic < nr_ioapics; apic++)
375 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
376 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
379 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
380 !mp_irqs[i].mpc_irqtype &&
382 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
383 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
385 if (!(apic || IO_APIC_IRQ(irq)))
388 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
391 * Use the first all-but-pin matching entry as a
392 * best-guess fuzzy result for broken mptables.
402 * EISA Edge/Level control register, ELCR
404 static int __init EISA_ELCR(unsigned int irq)
407 unsigned int port = 0x4d0 + (irq >> 3);
408 return (inb(port) >> (irq & 7)) & 1;
410 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
414 /* EISA interrupts are always polarity zero and can be edge or level
415 * trigger depending on the ELCR value. If an interrupt is listed as
416 * EISA conforming in the MP table, that means its trigger type must
417 * be read in from the ELCR */
419 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
420 #define default_EISA_polarity(idx) (0)
422 /* ISA interrupts are always polarity zero edge triggered,
423 * when listed as conforming in the MP table. */
425 #define default_ISA_trigger(idx) (0)
426 #define default_ISA_polarity(idx) (0)
428 /* PCI interrupts are always polarity one level triggered,
429 * when listed as conforming in the MP table. */
431 #define default_PCI_trigger(idx) (1)
432 #define default_PCI_polarity(idx) (1)
434 /* MCA interrupts are always polarity zero level triggered,
435 * when listed as conforming in the MP table. */
437 #define default_MCA_trigger(idx) (1)
438 #define default_MCA_polarity(idx) (0)
440 static int __init MPBIOS_polarity(int idx)
442 int bus = mp_irqs[idx].mpc_srcbus;
446 * Determine IRQ line polarity (high active or low active):
448 switch (mp_irqs[idx].mpc_irqflag & 3)
450 case 0: /* conforms, ie. bus-type dependent polarity */
452 switch (mp_bus_id_to_type[bus])
454 case MP_BUS_ISA: /* ISA pin */
456 polarity = default_ISA_polarity(idx);
459 case MP_BUS_EISA: /* EISA pin */
461 polarity = default_EISA_polarity(idx);
464 case MP_BUS_PCI: /* PCI pin */
466 polarity = default_PCI_polarity(idx);
469 case MP_BUS_MCA: /* MCA pin */
471 polarity = default_MCA_polarity(idx);
476 printk(KERN_WARNING "broken BIOS!!\n");
483 case 1: /* high active */
488 case 2: /* reserved */
490 printk(KERN_WARNING "broken BIOS!!\n");
494 case 3: /* low active */
499 default: /* invalid */
501 printk(KERN_WARNING "broken BIOS!!\n");
509 static int __init MPBIOS_trigger(int idx)
511 int bus = mp_irqs[idx].mpc_srcbus;
515 * Determine IRQ trigger mode (edge or level sensitive):
517 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
519 case 0: /* conforms, ie. bus-type dependent */
521 switch (mp_bus_id_to_type[bus])
523 case MP_BUS_ISA: /* ISA pin */
525 trigger = default_ISA_trigger(idx);
528 case MP_BUS_EISA: /* EISA pin */
530 trigger = default_EISA_trigger(idx);
533 case MP_BUS_PCI: /* PCI pin */
535 trigger = default_PCI_trigger(idx);
538 case MP_BUS_MCA: /* MCA pin */
540 trigger = default_MCA_trigger(idx);
545 printk(KERN_WARNING "broken BIOS!!\n");
557 case 2: /* reserved */
559 printk(KERN_WARNING "broken BIOS!!\n");
568 default: /* invalid */
570 printk(KERN_WARNING "broken BIOS!!\n");
578 static inline int irq_polarity(int idx)
580 return MPBIOS_polarity(idx);
583 static inline int irq_trigger(int idx)
585 return MPBIOS_trigger(idx);
588 static int pin_2_irq(int idx, int apic, int pin)
591 int bus = mp_irqs[idx].mpc_srcbus;
594 * Debugging check, we are in big trouble if this message pops up!
596 if (mp_irqs[idx].mpc_dstirq != pin)
597 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
599 switch (mp_bus_id_to_type[bus])
601 case MP_BUS_ISA: /* ISA pin */
605 irq = mp_irqs[idx].mpc_srcbusirq;
608 case MP_BUS_PCI: /* PCI pin */
611 * PCI IRQs are mapped in order
615 irq += nr_ioapic_registers[i++];
621 printk(KERN_ERR "unknown bus type %d.\n",bus);
628 * PCI IRQ command line redirection. Yes, limits are hardcoded.
630 if ((pin >= 16) && (pin <= 23)) {
631 if (pirq_entries[pin-16] != -1) {
632 if (!pirq_entries[pin-16]) {
633 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
635 irq = pirq_entries[pin-16];
636 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
644 static inline int IO_APIC_irq_trigger(int irq)
648 for (apic = 0; apic < nr_ioapics; apic++) {
649 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
650 idx = find_irq_entry(apic,pin,mp_INT);
651 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
652 return irq_trigger(idx);
656 * nonexistent IRQs are edge default
661 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
662 u8 irq_vector[NR_IRQ_VECTORS] = { FIRST_DEVICE_VECTOR , 0 };
664 #ifdef CONFIG_PCI_MSI
665 int assign_irq_vector(int irq)
667 int __init assign_irq_vector(int irq)
670 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
672 BUG_ON(irq >= NR_IRQ_VECTORS);
673 if (IO_APIC_VECTOR(irq) > 0)
674 return IO_APIC_VECTOR(irq);
677 if (current_vector == IA32_SYSCALL_VECTOR)
680 if (current_vector >= FIRST_SYSTEM_VECTOR) {
684 current_vector = FIRST_DEVICE_VECTOR + offset;
687 vector_irq[current_vector] = irq;
688 if (irq != AUTO_ASSIGN)
689 IO_APIC_VECTOR(irq) = current_vector;
691 return current_vector;
694 extern void (*interrupt[NR_IRQS])(void);
695 static struct hw_interrupt_type ioapic_level_type;
696 static struct hw_interrupt_type ioapic_edge_type;
698 #define IOAPIC_AUTO -1
699 #define IOAPIC_EDGE 0
700 #define IOAPIC_LEVEL 1
702 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
704 if (use_pci_vector() && !platform_legacy_irq(irq)) {
705 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
706 trigger == IOAPIC_LEVEL)
707 irq_desc[vector].handler = &ioapic_level_type;
709 irq_desc[vector].handler = &ioapic_edge_type;
710 set_intr_gate(vector, interrupt[vector]);
712 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
713 trigger == IOAPIC_LEVEL)
714 irq_desc[irq].handler = &ioapic_level_type;
716 irq_desc[irq].handler = &ioapic_edge_type;
717 set_intr_gate(vector, interrupt[irq]);
721 void __init setup_IO_APIC_irqs(void)
723 struct IO_APIC_route_entry entry;
724 int apic, pin, idx, irq, first_notcon = 1, vector;
727 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
729 for (apic = 0; apic < nr_ioapics; apic++) {
730 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
733 * add it to the IO-APIC irq-routing table:
735 memset(&entry,0,sizeof(entry));
737 entry.delivery_mode = dest_LowestPrio;
738 entry.dest_mode = INT_DELIVERY_MODE;
739 entry.mask = 0; /* enable IRQ */
740 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
742 idx = find_irq_entry(apic,pin,mp_INT);
745 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
748 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
752 entry.trigger = irq_trigger(idx);
753 entry.polarity = irq_polarity(idx);
755 if (irq_trigger(idx)) {
758 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
761 irq = pin_2_irq(idx, apic, pin);
762 add_pin_to_irq(irq, apic, pin);
764 if (!apic && !IO_APIC_IRQ(irq))
767 if (IO_APIC_IRQ(irq)) {
768 vector = assign_irq_vector(irq);
769 entry.vector = vector;
771 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
772 if (!apic && (irq < 16))
773 disable_8259A_irq(irq);
775 spin_lock_irqsave(&ioapic_lock, flags);
776 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
777 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
778 spin_unlock_irqrestore(&ioapic_lock, flags);
783 apic_printk(APIC_VERBOSE," not connected.\n");
787 * Set up the 8259A-master output pin as broadcast to all
790 void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
792 struct IO_APIC_route_entry entry;
795 memset(&entry,0,sizeof(entry));
797 disable_8259A_irq(0);
800 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
803 * We use logical delivery to get the timer IRQ
806 entry.dest_mode = INT_DELIVERY_MODE;
807 entry.mask = 0; /* unmask IRQ now */
808 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
809 entry.delivery_mode = dest_LowestPrio;
812 entry.vector = vector;
815 * The timer IRQ doesn't have to know that behind the
816 * scene we have a 8259A-master in AEOI mode ...
818 irq_desc[0].handler = &ioapic_edge_type;
821 * Add it to the IO-APIC irq-routing table:
823 spin_lock_irqsave(&ioapic_lock, flags);
824 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
825 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
826 spin_unlock_irqrestore(&ioapic_lock, flags);
831 void __init UNEXPECTED_IO_APIC(void)
835 void __apicdebuginit print_IO_APIC(void)
838 union IO_APIC_reg_00 reg_00;
839 union IO_APIC_reg_01 reg_01;
840 union IO_APIC_reg_02 reg_02;
843 if (apic_verbosity == APIC_QUIET)
846 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
847 for (i = 0; i < nr_ioapics; i++)
848 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
849 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
852 * We are a bit conservative about what we expect. We have to
853 * know about every hardware change ASAP.
855 printk(KERN_INFO "testing the IO APIC.......................\n");
857 for (apic = 0; apic < nr_ioapics; apic++) {
859 spin_lock_irqsave(&ioapic_lock, flags);
860 reg_00.raw = io_apic_read(apic, 0);
861 reg_01.raw = io_apic_read(apic, 1);
862 if (reg_01.bits.version >= 0x10)
863 reg_02.raw = io_apic_read(apic, 2);
864 spin_unlock_irqrestore(&ioapic_lock, flags);
867 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
868 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
869 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
870 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
871 UNEXPECTED_IO_APIC();
873 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
874 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
875 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
876 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
877 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
878 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
879 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
880 (reg_01.bits.entries != 0x2E) &&
881 (reg_01.bits.entries != 0x3F) &&
882 (reg_01.bits.entries != 0x03)
884 UNEXPECTED_IO_APIC();
886 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
887 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
888 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
889 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
890 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
891 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
892 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
893 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
895 UNEXPECTED_IO_APIC();
896 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
897 UNEXPECTED_IO_APIC();
899 if (reg_01.bits.version >= 0x10) {
900 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
901 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
902 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
903 UNEXPECTED_IO_APIC();
906 printk(KERN_DEBUG ".... IRQ redirection table:\n");
908 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
909 " Stat Dest Deli Vect: \n");
911 for (i = 0; i <= reg_01.bits.entries; i++) {
912 struct IO_APIC_route_entry entry;
914 spin_lock_irqsave(&ioapic_lock, flags);
915 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
916 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
917 spin_unlock_irqrestore(&ioapic_lock, flags);
919 printk(KERN_DEBUG " %02x %03X %02X ",
921 entry.dest.logical.logical_dest,
922 entry.dest.physical.physical_dest
925 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
930 entry.delivery_status,
937 if (use_pci_vector())
938 printk(KERN_INFO "Using vector-based indexing\n");
939 printk(KERN_DEBUG "IRQ to pin mappings:\n");
940 for (i = 0; i < NR_IRQS; i++) {
941 struct irq_pin_list *entry = irq_2_pin + i;
944 if (use_pci_vector() && !platform_legacy_irq(i))
945 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
947 printk(KERN_DEBUG "IRQ%d ", i);
949 printk("-> %d:%d", entry->apic, entry->pin);
952 entry = irq_2_pin + entry->next;
957 printk(KERN_INFO ".................................... done.\n");
962 static __apicdebuginit void print_APIC_bitfield (int base)
967 if (apic_verbosity == APIC_QUIET)
970 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
971 for (i = 0; i < 8; i++) {
972 v = apic_read(base + i*0x10);
973 for (j = 0; j < 32; j++) {
983 void __apicdebuginit print_local_APIC(void * dummy)
985 unsigned int v, ver, maxlvt;
987 if (apic_verbosity == APIC_QUIET)
990 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
991 smp_processor_id(), hard_smp_processor_id());
992 v = apic_read(APIC_ID);
993 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
994 v = apic_read(APIC_LVR);
995 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
996 ver = GET_APIC_VERSION(v);
997 maxlvt = get_maxlvt();
999 v = apic_read(APIC_TASKPRI);
1000 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1002 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1003 v = apic_read(APIC_ARBPRI);
1004 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1005 v & APIC_ARBPRI_MASK);
1006 v = apic_read(APIC_PROCPRI);
1007 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1010 v = apic_read(APIC_EOI);
1011 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1012 v = apic_read(APIC_RRR);
1013 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1014 v = apic_read(APIC_LDR);
1015 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1016 v = apic_read(APIC_DFR);
1017 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1018 v = apic_read(APIC_SPIV);
1019 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1021 printk(KERN_DEBUG "... APIC ISR field:\n");
1022 print_APIC_bitfield(APIC_ISR);
1023 printk(KERN_DEBUG "... APIC TMR field:\n");
1024 print_APIC_bitfield(APIC_TMR);
1025 printk(KERN_DEBUG "... APIC IRR field:\n");
1026 print_APIC_bitfield(APIC_IRR);
1028 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1029 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1030 apic_write(APIC_ESR, 0);
1031 v = apic_read(APIC_ESR);
1032 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1035 v = apic_read(APIC_ICR);
1036 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1037 v = apic_read(APIC_ICR2);
1038 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1040 v = apic_read(APIC_LVTT);
1041 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1043 if (maxlvt > 3) { /* PC is LVT#4. */
1044 v = apic_read(APIC_LVTPC);
1045 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1047 v = apic_read(APIC_LVT0);
1048 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1049 v = apic_read(APIC_LVT1);
1050 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1052 if (maxlvt > 2) { /* ERR is LVT#3. */
1053 v = apic_read(APIC_LVTERR);
1054 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1057 v = apic_read(APIC_TMICT);
1058 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1059 v = apic_read(APIC_TMCCT);
1060 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1061 v = apic_read(APIC_TDCR);
1062 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1066 void print_all_local_APICs (void)
1068 on_each_cpu(print_local_APIC, NULL, 1, 1);
1071 void __apicdebuginit print_PIC(void)
1073 extern spinlock_t i8259A_lock;
1075 unsigned long flags;
1077 if (apic_verbosity == APIC_QUIET)
1080 printk(KERN_DEBUG "\nprinting PIC contents\n");
1082 spin_lock_irqsave(&i8259A_lock, flags);
1084 v = inb(0xa1) << 8 | inb(0x21);
1085 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1087 v = inb(0xa0) << 8 | inb(0x20);
1088 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1092 v = inb(0xa0) << 8 | inb(0x20);
1096 spin_unlock_irqrestore(&i8259A_lock, flags);
1098 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1100 v = inb(0x4d1) << 8 | inb(0x4d0);
1101 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1104 static void __init enable_IO_APIC(void)
1106 union IO_APIC_reg_01 reg_01;
1108 unsigned long flags;
1110 for (i = 0; i < PIN_MAP_SIZE; i++) {
1111 irq_2_pin[i].pin = -1;
1112 irq_2_pin[i].next = 0;
1115 for (i = 0; i < MAX_PIRQS; i++)
1116 pirq_entries[i] = -1;
1119 * The number of IO-APIC IRQ registers (== #pins):
1121 for (i = 0; i < nr_ioapics; i++) {
1122 spin_lock_irqsave(&ioapic_lock, flags);
1123 reg_01.raw = io_apic_read(i, 1);
1124 spin_unlock_irqrestore(&ioapic_lock, flags);
1125 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1129 * Do not trust the IO-APIC being empty at bootup
1135 * Not an __init, needed by the reboot code
1137 void disable_IO_APIC(void)
1141 * Clear the IO-APIC before rebooting:
1146 * If the i82559 is routed through an IOAPIC
1147 * Put that IOAPIC in virtual wire mode
1148 * so legacy interrups can be delivered.
1150 pin = find_isa_irq_pin(0, mp_ExtINT);
1152 struct IO_APIC_route_entry entry;
1153 unsigned long flags;
1155 memset(&entry, 0, sizeof(entry));
1156 entry.mask = 0; /* Enabled */
1157 entry.trigger = 0; /* Edge */
1159 entry.polarity = 0; /* High */
1160 entry.delivery_status = 0;
1161 entry.dest_mode = 0; /* Physical */
1162 entry.delivery_mode = 7; /* ExtInt */
1164 entry.dest.physical.physical_dest = 0;
1168 * Add it to the IO-APIC irq-routing table:
1170 spin_lock_irqsave(&ioapic_lock, flags);
1171 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
1172 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
1173 spin_unlock_irqrestore(&ioapic_lock, flags);
1176 disconnect_bsp_APIC();
1180 * function to set the IO-APIC physical IDs based on the
1181 * values stored in the MPC table.
1183 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1186 static void __init setup_ioapic_ids_from_mpc (void)
1188 union IO_APIC_reg_00 reg_00;
1189 physid_mask_t phys_id_present_map = phys_cpu_present_map;
1192 unsigned char old_id;
1193 unsigned long flags;
1196 * Set the IOAPIC ID to the value stored in the MPC table.
1198 for (apic = 0; apic < nr_ioapics; apic++) {
1200 /* Read the register 0 value */
1201 spin_lock_irqsave(&ioapic_lock, flags);
1202 reg_00.raw = io_apic_read(apic, 0);
1203 spin_unlock_irqrestore(&ioapic_lock, flags);
1205 old_id = mp_ioapics[apic].mpc_apicid;
1207 if (mp_ioapics[apic].mpc_apicid >= 0xf) {
1208 apic_printk(APIC_QUIET,KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1209 apic, mp_ioapics[apic].mpc_apicid);
1210 apic_printk(APIC_QUIET,KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1212 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1216 * Sanity check, is the ID really free? Every APIC in a
1217 * system must have a unique ID or we get lots of nice
1218 * 'stuck on smp_invalidate_needed IPI wait' messages.
1220 if (physid_isset(mp_ioapics[apic].mpc_apicid, phys_id_present_map)) {
1221 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1222 apic, mp_ioapics[apic].mpc_apicid);
1223 for (i = 0; i < 0xf; i++)
1224 if (!physid_isset(i, phys_id_present_map))
1227 panic("Max APIC ID exceeded!\n");
1228 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1230 physid_set(i, phys_id_present_map);
1231 mp_ioapics[apic].mpc_apicid = i;
1234 "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1235 physid_set(mp_ioapics[apic].mpc_apicid, phys_id_present_map);
1240 * We need to adjust the IRQ routing table
1241 * if the ID changed.
1243 if (old_id != mp_ioapics[apic].mpc_apicid)
1244 for (i = 0; i < mp_irq_entries; i++)
1245 if (mp_irqs[i].mpc_dstapic == old_id)
1246 mp_irqs[i].mpc_dstapic
1247 = mp_ioapics[apic].mpc_apicid;
1250 * Read the right value from the MPC table and
1251 * write it into the ID register.
1253 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1254 mp_ioapics[apic].mpc_apicid);
1256 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1257 spin_lock_irqsave(&ioapic_lock, flags);
1258 io_apic_write(apic, 0, reg_00.raw);
1259 spin_unlock_irqrestore(&ioapic_lock, flags);
1264 spin_lock_irqsave(&ioapic_lock, flags);
1265 reg_00.raw = io_apic_read(apic, 0);
1266 spin_unlock_irqrestore(&ioapic_lock, flags);
1267 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1268 printk("could not set ID!\n");
1270 apic_printk(APIC_VERBOSE," ok.\n");
1275 * There is a nasty bug in some older SMP boards, their mptable lies
1276 * about the timer IRQ. We do the following to work around the situation:
1278 * - timer IRQ defaults to IO-APIC IRQ
1279 * - if this function detects that timer IRQs are defunct, then we fall
1280 * back to ISA timer IRQs
1282 static int __init timer_irq_works(void)
1284 unsigned long t1 = jiffies;
1287 /* Let ten ticks pass... */
1288 mdelay((10 * 1000) / HZ);
1291 * Expect a few ticks at least, to be sure some possible
1292 * glue logic does not lock up after one or two first
1293 * ticks in a non-ExtINT mode. Also the local APIC
1294 * might have cached one ExtINT interrupt. Finally, at
1295 * least one tick may be lost due to delays.
1299 if (jiffies - t1 > 4)
1305 * In the SMP+IOAPIC case it might happen that there are an unspecified
1306 * number of pending IRQ events unhandled. These cases are very rare,
1307 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1308 * better to do it this way as thus we do not have to be aware of
1309 * 'pending' interrupts in the IRQ path, except at this point.
1312 * Edge triggered needs to resend any interrupt
1313 * that was delayed but this is now handled in the device
1318 * Starting up a edge-triggered IO-APIC interrupt is
1319 * nasty - we need to make sure that we get the edge.
1320 * If it is already asserted for some reason, we need
1321 * return 1 to indicate that is was pending.
1323 * This is not complete - we should be able to fake
1324 * an edge even if it isn't on the 8259A...
1327 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1329 int was_pending = 0;
1330 unsigned long flags;
1332 spin_lock_irqsave(&ioapic_lock, flags);
1334 disable_8259A_irq(irq);
1335 if (i8259A_irq_pending(irq))
1338 __unmask_IO_APIC_irq(irq);
1339 spin_unlock_irqrestore(&ioapic_lock, flags);
1345 * Once we have recorded IRQ_PENDING already, we can mask the
1346 * interrupt for real. This prevents IRQ storms from unhandled
1349 static void ack_edge_ioapic_irq(unsigned int irq)
1351 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1352 == (IRQ_PENDING | IRQ_DISABLED))
1353 mask_IO_APIC_irq(irq);
1358 * Level triggered interrupts can just be masked,
1359 * and shutting down and starting up the interrupt
1360 * is the same as enabling and disabling them -- except
1361 * with a startup need to return a "was pending" value.
1363 * Level triggered interrupts are special because we
1364 * do not touch any IO-APIC register while handling
1365 * them. We ack the APIC in the end-IRQ handler, not
1366 * in the start-IRQ-handler. Protection against reentrance
1367 * from the same interrupt is still provided, both by the
1368 * generic IRQ layer and by the fact that an unacked local
1369 * APIC does not accept IRQs.
1371 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1373 unmask_IO_APIC_irq(irq);
1375 return 0; /* don't check for pending */
1378 static void end_level_ioapic_irq (unsigned int irq)
1384 * It appears there is an erratum which affects at least version 0x11
1385 * of I/O APIC (that's the 82093AA and cores integrated into various
1386 * chipsets). Under certain conditions a level-triggered interrupt is
1387 * erroneously delivered as edge-triggered one but the respective IRR
1388 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1389 * message but it will never arrive and further interrupts are blocked
1390 * from the source. The exact reason is so far unknown, but the
1391 * phenomenon was observed when two consecutive interrupt requests
1392 * from a given source get delivered to the same CPU and the source is
1393 * temporarily disabled in between.
1395 * A workaround is to simulate an EOI message manually. We achieve it
1396 * by setting the trigger mode to edge and then to level when the edge
1397 * trigger mode gets detected in the TMR of a local APIC for a
1398 * level-triggered interrupt. We mask the source for the time of the
1399 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1400 * The idea is from Manfred Spraul. --macro
1402 i = IO_APIC_VECTOR(irq);
1403 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1407 if (!(v & (1 << (i & 0x1f)))) {
1408 #ifdef APIC_LOCKUP_DEBUG
1409 struct irq_pin_list *entry;
1412 #ifdef APIC_MISMATCH_DEBUG
1413 atomic_inc(&irq_mis_count);
1415 spin_lock(&ioapic_lock);
1416 __mask_and_edge_IO_APIC_irq(irq);
1417 #ifdef APIC_LOCKUP_DEBUG
1418 for (entry = irq_2_pin + irq;;) {
1421 if (entry->pin == -1)
1423 reg = io_apic_read(entry->apic, 0x10 + entry->pin * 2);
1424 if (reg & 0x00004000)
1425 printk(KERN_CRIT "Aieee!!! Remote IRR"
1426 " still set after unlock!\n");
1429 entry = irq_2_pin + entry->next;
1432 __unmask_and_level_IO_APIC_irq(irq);
1433 spin_unlock(&ioapic_lock);
1437 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
1439 unsigned long flags;
1442 dest = cpu_mask_to_apicid(mask);
1445 * Only the first 8 bits are valid.
1449 spin_lock_irqsave(&ioapic_lock, flags);
1450 __DO_ACTION(1, = dest, )
1451 spin_unlock_irqrestore(&ioapic_lock, flags);
1454 #ifdef CONFIG_PCI_MSI
1455 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1457 int irq = vector_to_irq(vector);
1459 return startup_edge_ioapic_irq(irq);
1462 static void ack_edge_ioapic_vector(unsigned int vector)
1464 int irq = vector_to_irq(vector);
1466 ack_edge_ioapic_irq(irq);
1469 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1471 int irq = vector_to_irq(vector);
1473 return startup_level_ioapic_irq (irq);
1476 static void end_level_ioapic_vector (unsigned int vector)
1478 int irq = vector_to_irq(vector);
1480 end_level_ioapic_irq(irq);
1483 static void mask_IO_APIC_vector (unsigned int vector)
1485 int irq = vector_to_irq(vector);
1487 mask_IO_APIC_irq(irq);
1490 static void unmask_IO_APIC_vector (unsigned int vector)
1492 int irq = vector_to_irq(vector);
1494 unmask_IO_APIC_irq(irq);
1497 static void set_ioapic_affinity_vector (unsigned int vector,
1500 int irq = vector_to_irq(vector);
1502 set_ioapic_affinity_irq(irq, cpu_mask);
1507 * Level and edge triggered IO-APIC interrupts need different handling,
1508 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1509 * handled with the level-triggered descriptor, but that one has slightly
1510 * more overhead. Level-triggered interrupts cannot be handled with the
1511 * edge-triggered handler, without risking IRQ storms and other ugly
1515 static struct hw_interrupt_type ioapic_edge_type = {
1516 .typename = "IO-APIC-edge",
1517 .startup = startup_edge_ioapic,
1518 .shutdown = shutdown_edge_ioapic,
1519 .enable = enable_edge_ioapic,
1520 .disable = disable_edge_ioapic,
1521 .ack = ack_edge_ioapic,
1522 .end = end_edge_ioapic,
1523 .set_affinity = set_ioapic_affinity,
1526 static struct hw_interrupt_type ioapic_level_type = {
1527 .typename = "IO-APIC-level",
1528 .startup = startup_level_ioapic,
1529 .shutdown = shutdown_level_ioapic,
1530 .enable = enable_level_ioapic,
1531 .disable = disable_level_ioapic,
1532 .ack = mask_and_ack_level_ioapic,
1533 .end = end_level_ioapic,
1534 .set_affinity = set_ioapic_affinity,
1537 static inline void init_IO_APIC_traps(void)
1542 * NOTE! The local APIC isn't very good at handling
1543 * multiple interrupts at the same interrupt level.
1544 * As the interrupt level is determined by taking the
1545 * vector number and shifting that right by 4, we
1546 * want to spread these out a bit so that they don't
1547 * all fall in the same interrupt level.
1549 * Also, we've got to be careful not to trash gate
1550 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1552 for (irq = 0; irq < NR_IRQS ; irq++) {
1554 if (use_pci_vector()) {
1555 if (!platform_legacy_irq(tmp))
1556 if ((tmp = vector_to_irq(tmp)) == -1)
1559 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1561 * Hmm.. We don't have an entry for this,
1562 * so default to an old-fashioned 8259
1563 * interrupt if we can..
1566 make_8259A_irq(irq);
1568 /* Strange. Oh, well.. */
1569 irq_desc[irq].handler = &no_irq_type;
1574 static void enable_lapic_irq (unsigned int irq)
1578 v = apic_read(APIC_LVT0);
1579 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1582 static void disable_lapic_irq (unsigned int irq)
1586 v = apic_read(APIC_LVT0);
1587 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1590 static void ack_lapic_irq (unsigned int irq)
1595 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1597 static struct hw_interrupt_type lapic_irq_type = {
1598 .typename = "local-APIC-edge",
1599 .startup = NULL, /* startup_irq() not used for IRQ0 */
1600 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1601 .enable = enable_lapic_irq,
1602 .disable = disable_lapic_irq,
1603 .ack = ack_lapic_irq,
1604 .end = end_lapic_irq,
1607 static void setup_nmi (void)
1610 * Dirty trick to enable the NMI watchdog ...
1611 * We put the 8259A master into AEOI mode and
1612 * unmask on all local APICs LVT0 as NMI.
1614 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1615 * is from Maciej W. Rozycki - so we do not have to EOI from
1616 * the NMI handler or the timer interrupt.
1618 printk(KERN_INFO "activating NMI Watchdog ...");
1620 enable_NMI_through_LVT0(NULL);
1626 * This looks a bit hackish but it's about the only one way of sending
1627 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1628 * not support the ExtINT mode, unfortunately. We need to send these
1629 * cycles as some i82489DX-based boards have glue logic that keeps the
1630 * 8259A interrupt line asserted until INTA. --macro
1632 static inline void unlock_ExtINT_logic(void)
1635 struct IO_APIC_route_entry entry0, entry1;
1636 unsigned char save_control, save_freq_select;
1637 unsigned long flags;
1639 pin = find_isa_irq_pin(8, mp_INT);
1643 spin_lock_irqsave(&ioapic_lock, flags);
1644 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1645 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1646 spin_unlock_irqrestore(&ioapic_lock, flags);
1647 clear_IO_APIC_pin(0, pin);
1649 memset(&entry1, 0, sizeof(entry1));
1651 entry1.dest_mode = 0; /* physical delivery */
1652 entry1.mask = 0; /* unmask IRQ now */
1653 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1654 entry1.delivery_mode = dest_ExtINT;
1655 entry1.polarity = entry0.polarity;
1659 spin_lock_irqsave(&ioapic_lock, flags);
1660 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1661 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1662 spin_unlock_irqrestore(&ioapic_lock, flags);
1664 save_control = CMOS_READ(RTC_CONTROL);
1665 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1666 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1668 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1673 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1677 CMOS_WRITE(save_control, RTC_CONTROL);
1678 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1679 clear_IO_APIC_pin(0, pin);
1681 spin_lock_irqsave(&ioapic_lock, flags);
1682 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1683 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1684 spin_unlock_irqrestore(&ioapic_lock, flags);
1688 * This code may look a bit paranoid, but it's supposed to cooperate with
1689 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1690 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1691 * fanatically on his truly buggy board.
1693 static inline void check_timer(void)
1699 * get/set the timer IRQ vector:
1701 disable_8259A_irq(0);
1702 vector = assign_irq_vector(0);
1703 set_intr_gate(vector, interrupt[0]);
1706 * Subtle, code in do_timer_interrupt() expects an AEOI
1707 * mode for the 8259A whenever interrupts are routed
1708 * through I/O APICs. Also IRQ0 has to be enabled in
1709 * the 8259A which implies the virtual wire has to be
1710 * disabled in the local APIC.
1712 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1714 enable_8259A_irq(0);
1716 pin1 = find_isa_irq_pin(0, mp_INT);
1717 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1719 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1723 * Ok, does IRQ0 through the IOAPIC work?
1725 unmask_IO_APIC_irq(0);
1726 if (timer_irq_works()) {
1727 nmi_watchdog_default();
1728 if (nmi_watchdog == NMI_IO_APIC) {
1729 disable_8259A_irq(0);
1731 enable_8259A_irq(0);
1732 check_nmi_watchdog();
1736 clear_IO_APIC_pin(0, pin1);
1737 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1740 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1742 apic_printk(APIC_VERBOSE,"\n..... (found pin %d) ...", pin2);
1744 * legacy devices should be connected to IO APIC #0
1746 setup_ExtINT_IRQ0_pin(pin2, vector);
1747 if (timer_irq_works()) {
1749 nmi_watchdog_default();
1750 if (nmi_watchdog == NMI_IO_APIC) {
1752 check_nmi_watchdog();
1757 * Cleanup, just in case ...
1759 clear_IO_APIC_pin(0, pin2);
1761 printk(" failed.\n");
1764 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1768 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1770 disable_8259A_irq(0);
1771 irq_desc[0].handler = &lapic_irq_type;
1772 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1773 enable_8259A_irq(0);
1775 if (timer_irq_works()) {
1776 apic_printk(APIC_QUIET, " works.\n");
1779 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1780 apic_printk(APIC_VERBOSE," failed.\n");
1782 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1786 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1788 unlock_ExtINT_logic();
1790 if (timer_irq_works()) {
1791 apic_printk(APIC_VERBOSE," works.\n");
1794 apic_printk(APIC_VERBOSE," failed :(.\n");
1795 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1800 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1801 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1802 * Linux doesn't really care, as it's not actually used
1803 * for any interrupt handling anyway.
1805 #define PIC_IRQS (1<<2)
1807 void __init setup_IO_APIC(void)
1812 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1814 io_apic_irqs = ~PIC_IRQS;
1816 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1819 * Set up the IO-APIC IRQ routing table.
1822 setup_ioapic_ids_from_mpc();
1824 setup_IO_APIC_irqs();
1825 init_IO_APIC_traps();
1831 struct sysfs_ioapic_data {
1832 struct sys_device dev;
1833 struct IO_APIC_route_entry entry[0];
1835 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1837 static int ioapic_suspend(struct sys_device *dev, u32 state)
1839 struct IO_APIC_route_entry *entry;
1840 struct sysfs_ioapic_data *data;
1841 unsigned long flags;
1844 data = container_of(dev, struct sysfs_ioapic_data, dev);
1845 entry = data->entry;
1846 spin_lock_irqsave(&ioapic_lock, flags);
1847 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1848 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1849 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1851 spin_unlock_irqrestore(&ioapic_lock, flags);
1856 static int ioapic_resume(struct sys_device *dev)
1858 struct IO_APIC_route_entry *entry;
1859 struct sysfs_ioapic_data *data;
1860 unsigned long flags;
1861 union IO_APIC_reg_00 reg_00;
1864 data = container_of(dev, struct sysfs_ioapic_data, dev);
1865 entry = data->entry;
1867 spin_lock_irqsave(&ioapic_lock, flags);
1868 reg_00.raw = io_apic_read(dev->id, 0);
1869 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1870 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1871 io_apic_write(dev->id, 0, reg_00.raw);
1873 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1874 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1875 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1877 spin_unlock_irqrestore(&ioapic_lock, flags);
1882 static struct sysdev_class ioapic_sysdev_class = {
1883 set_kset_name("ioapic"),
1884 .suspend = ioapic_suspend,
1885 .resume = ioapic_resume,
1888 static int __init ioapic_init_sysfs(void)
1890 struct sys_device * dev;
1891 int i, size, error = 0;
1893 error = sysdev_class_register(&ioapic_sysdev_class);
1897 for (i = 0; i < nr_ioapics; i++ ) {
1898 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1899 * sizeof(struct IO_APIC_route_entry);
1900 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1901 if (!mp_ioapic_data[i]) {
1902 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1905 memset(mp_ioapic_data[i], 0, size);
1906 dev = &mp_ioapic_data[i]->dev;
1908 dev->cls = &ioapic_sysdev_class;
1909 error = sysdev_register(dev);
1911 kfree(mp_ioapic_data[i]);
1912 mp_ioapic_data[i] = NULL;
1913 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1921 device_initcall(ioapic_init_sysfs);
1923 /* --------------------------------------------------------------------------
1924 ACPI-based IOAPIC Configuration
1925 -------------------------------------------------------------------------- */
1927 #ifdef CONFIG_ACPI_BOOT
1929 #define IO_APIC_MAX_ID 15
1931 int __init io_apic_get_unique_id (int ioapic, int apic_id)
1933 union IO_APIC_reg_00 reg_00;
1934 static physid_mask_t apic_id_map;
1935 unsigned long flags;
1939 * The P4 platform supports up to 256 APIC IDs on two separate APIC
1940 * buses (one for LAPICs, one for IOAPICs), where predecessors only
1941 * supports up to 16 on one shared APIC bus.
1943 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
1944 * advantage of new APIC bus architecture.
1947 if (physids_empty(apic_id_map))
1948 apic_id_map = phys_cpu_present_map;
1950 spin_lock_irqsave(&ioapic_lock, flags);
1951 reg_00.raw = io_apic_read(ioapic, 0);
1952 spin_unlock_irqrestore(&ioapic_lock, flags);
1954 if (apic_id >= IO_APIC_MAX_ID) {
1955 apic_printk(APIC_QUIET, KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
1956 "%d\n", ioapic, apic_id, reg_00.bits.ID);
1957 apic_id = reg_00.bits.ID;
1961 * Every APIC in a system must have a unique ID or we get lots of nice
1962 * 'stuck on smp_invalidate_needed IPI wait' messages.
1964 if (physid_isset(apic_id, apic_id_map)) {
1966 for (i = 0; i < IO_APIC_MAX_ID; i++) {
1967 if (!physid_isset(i, apic_id_map))
1971 if (i == IO_APIC_MAX_ID)
1972 panic("Max apic_id exceeded!\n");
1974 apic_printk(APIC_VERBOSE, KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
1975 "trying %d\n", ioapic, apic_id, i);
1980 physid_set(apic_id, apic_id_map);
1982 if (reg_00.bits.ID != apic_id) {
1983 reg_00.bits.ID = apic_id;
1985 spin_lock_irqsave(&ioapic_lock, flags);
1986 io_apic_write(ioapic, 0, reg_00.raw);
1987 reg_00.raw = io_apic_read(ioapic, 0);
1988 spin_unlock_irqrestore(&ioapic_lock, flags);
1991 if (reg_00.bits.ID != apic_id)
1992 panic("IOAPIC[%d]: Unable change apic_id!\n", ioapic);
1995 apic_printk(APIC_VERBOSE,KERN_INFO "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2001 int __init io_apic_get_version (int ioapic)
2003 union IO_APIC_reg_01 reg_01;
2004 unsigned long flags;
2006 spin_lock_irqsave(&ioapic_lock, flags);
2007 reg_01.raw = io_apic_read(ioapic, 1);
2008 spin_unlock_irqrestore(&ioapic_lock, flags);
2010 return reg_01.bits.version;
2014 int __init io_apic_get_redir_entries (int ioapic)
2016 union IO_APIC_reg_01 reg_01;
2017 unsigned long flags;
2019 spin_lock_irqsave(&ioapic_lock, flags);
2020 reg_01.raw = io_apic_read(ioapic, 1);
2021 spin_unlock_irqrestore(&ioapic_lock, flags);
2023 return reg_01.bits.entries;
2027 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2029 struct IO_APIC_route_entry entry;
2030 unsigned long flags;
2032 if (!IO_APIC_IRQ(irq)) {
2033 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2039 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2040 * Note that we mask (disable) IRQs now -- these get enabled when the
2041 * corresponding device driver registers for this IRQ.
2044 memset(&entry,0,sizeof(entry));
2046 entry.delivery_mode = dest_LowestPrio;
2047 entry.dest_mode = INT_DELIVERY_MODE;
2048 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2049 entry.trigger = edge_level;
2050 entry.polarity = active_high_low;
2051 entry.mask = 1; /* Disabled (masked) */
2054 * IRQs < 16 are already in the irq_2_pin[] map
2057 add_pin_to_irq(irq, ioapic, pin);
2059 entry.vector = assign_irq_vector(irq);
2061 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2062 "IRQ %d Mode:%i Active:%i)\n", ioapic,
2063 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2064 edge_level, active_high_low);
2066 ioapic_register_intr(irq, entry.vector, edge_level);
2068 if (!ioapic && (irq < 16))
2069 disable_8259A_irq(irq);
2071 spin_lock_irqsave(&ioapic_lock, flags);
2072 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2073 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2074 spin_unlock_irqrestore(&ioapic_lock, flags);
2079 #endif /*CONFIG_ACPI_BOOT*/
2082 void send_IPI_self(int vector)
2089 apic_wait_icr_idle();
2090 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
2093 * Send the IPI. The write to APIC_ICR fires this off.
2095 apic_write_around(APIC_ICR, cfg);
2101 * This function currently is only a helper for the i386 smp boot process where
2102 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2103 * so mask in all cases should simply be TARGET_CPUS
2105 void __init setup_ioapic_dest(void)
2107 int pin, ioapic, irq, irq_entry;
2109 if (skip_ioapic_setup == 1)
2112 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2113 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2114 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2115 if (irq_entry == -1)
2117 irq = pin_2_irq(irq_entry, ioapic, pin);
2118 set_ioapic_affinity_irq(irq, TARGET_CPUS);