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/config.h>
29 #include <linux/smp_lock.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
37 #include <asm/proto.h>
38 #include <asm/mach_apic.h>
41 #define __apicdebuginit __init
43 int sis_apic_bug; /* not actually supported, dummy for compile */
45 static int no_timer_check;
47 static DEFINE_SPINLOCK(ioapic_lock);
50 * # of IRQ routing registers
52 int nr_ioapic_registers[MAX_IO_APICS];
55 * Rough estimation of how many shared IRQs there are, can
58 #define MAX_PLUS_SHARED_IRQS NR_IRQS
59 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
62 * This is performance-critical, we want to do it O(1)
64 * the indexing order of this array favors 1:1 mappings
65 * between pins and IRQs.
68 static struct irq_pin_list {
69 short apic, pin, next;
70 } irq_2_pin[PIN_MAP_SIZE];
72 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
74 #define vector_to_irq(vector) \
75 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
77 #define vector_to_irq(vector) (vector)
80 #define __DO_ACTION(R, ACTION, FINAL) \
84 struct irq_pin_list *entry = irq_2_pin + irq; \
91 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
93 io_apic_modify(entry->apic, reg); \
96 entry = irq_2_pin + entry->next; \
102 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
108 cpus_and(tmp, mask, cpu_online_map);
112 cpus_and(mask, tmp, CPU_MASK_ALL);
114 dest = cpu_mask_to_apicid(mask);
117 * Only the high 8 bits are valid.
119 dest = SET_APIC_LOGICAL_ID(dest);
121 spin_lock_irqsave(&ioapic_lock, flags);
122 __DO_ACTION(1, = dest, )
123 set_irq_info(irq, mask);
124 spin_unlock_irqrestore(&ioapic_lock, flags);
129 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
130 * shared ISA-space IRQs, so we have to support them. We are super
131 * fast in the common case, and fast for shared ISA-space IRQs.
133 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
135 static int first_free_entry = NR_IRQS;
136 struct irq_pin_list *entry = irq_2_pin + irq;
139 entry = irq_2_pin + entry->next;
141 if (entry->pin != -1) {
142 entry->next = first_free_entry;
143 entry = irq_2_pin + entry->next;
144 if (++first_free_entry >= PIN_MAP_SIZE)
145 panic("io_apic.c: whoops");
152 #define DO_ACTION(name,R,ACTION, FINAL) \
154 static void name##_IO_APIC_irq (unsigned int irq) \
155 __DO_ACTION(R, ACTION, FINAL)
157 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
159 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
162 static void mask_IO_APIC_irq (unsigned int irq)
166 spin_lock_irqsave(&ioapic_lock, flags);
167 __mask_IO_APIC_irq(irq);
168 spin_unlock_irqrestore(&ioapic_lock, flags);
171 static void unmask_IO_APIC_irq (unsigned int irq)
175 spin_lock_irqsave(&ioapic_lock, flags);
176 __unmask_IO_APIC_irq(irq);
177 spin_unlock_irqrestore(&ioapic_lock, flags);
180 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
182 struct IO_APIC_route_entry entry;
185 /* Check delivery_mode to be sure we're not clearing an SMI pin */
186 spin_lock_irqsave(&ioapic_lock, flags);
187 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
188 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
189 spin_unlock_irqrestore(&ioapic_lock, flags);
190 if (entry.delivery_mode == dest_SMI)
193 * Disable it in the IO-APIC irq-routing table:
195 memset(&entry, 0, sizeof(entry));
197 spin_lock_irqsave(&ioapic_lock, flags);
198 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
199 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
200 spin_unlock_irqrestore(&ioapic_lock, flags);
203 static void clear_IO_APIC (void)
207 for (apic = 0; apic < nr_ioapics; apic++)
208 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
209 clear_IO_APIC_pin(apic, pin);
213 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
214 * specific CPU-side IRQs.
218 static int pirq_entries [MAX_PIRQS];
219 static int pirqs_enabled;
220 int skip_ioapic_setup;
223 /* dummy parsing: see setup.c */
225 static int __init disable_ioapic_setup(char *str)
227 skip_ioapic_setup = 1;
231 static int __init enable_ioapic_setup(char *str)
234 skip_ioapic_setup = 0;
238 __setup("noapic", disable_ioapic_setup);
239 __setup("apic", enable_ioapic_setup);
241 #include <asm/pci-direct.h>
242 #include <linux/pci_ids.h>
243 #include <linux/pci.h>
245 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
246 off. Check for an Nvidia or VIA PCI bridge and turn it off.
247 Use pci direct infrastructure because this runs before the PCI subsystem.
249 Can be overwritten with "apic"
251 And another hack to disable the IOMMU on VIA chipsets.
254 void __init check_ioapic(void)
260 /* Poor man's PCI discovery */
261 for (num = 0; num < 32; num++) {
262 for (slot = 0; slot < 32; slot++) {
263 for (func = 0; func < 8; func++) {
267 class = read_pci_config(num,slot,func,
269 if (class == 0xffffffff)
272 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
275 vendor = read_pci_config(num, slot, func,
279 case PCI_VENDOR_ID_VIA:
280 #ifdef CONFIG_GART_IOMMU
281 if ((end_pfn >= (0xffffffff>>PAGE_SHIFT) ||
283 !iommu_aperture_allowed) {
285 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
286 iommu_aperture_disabled = 1;
290 case PCI_VENDOR_ID_NVIDIA:
292 /* All timer overrides on Nvidia
293 seem to be wrong. Skip them. */
294 acpi_skip_timer_override = 1;
296 "Nvidia board detected. Ignoring ACPI timer override.\n");
298 /* RED-PEN skip them on mptables too? */
302 /* No multi-function device? */
303 type = read_pci_config_byte(num,slot,func,
312 static int __init ioapic_pirq_setup(char *str)
315 int ints[MAX_PIRQS+1];
317 get_options(str, ARRAY_SIZE(ints), ints);
319 for (i = 0; i < MAX_PIRQS; i++)
320 pirq_entries[i] = -1;
323 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
325 if (ints[0] < MAX_PIRQS)
328 for (i = 0; i < max; i++) {
329 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
331 * PIRQs are mapped upside down, usually.
333 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
338 __setup("pirq=", ioapic_pirq_setup);
341 * Find the IRQ entry number of a certain pin.
343 static int find_irq_entry(int apic, int pin, int type)
347 for (i = 0; i < mp_irq_entries; i++)
348 if (mp_irqs[i].mpc_irqtype == type &&
349 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
350 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
351 mp_irqs[i].mpc_dstirq == pin)
358 * Find the pin to which IRQ[irq] (ISA) is connected
360 static int find_isa_irq_pin(int irq, int type)
364 for (i = 0; i < mp_irq_entries; i++) {
365 int lbus = mp_irqs[i].mpc_srcbus;
367 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
368 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
369 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
370 (mp_irqs[i].mpc_irqtype == type) &&
371 (mp_irqs[i].mpc_srcbusirq == irq))
373 return mp_irqs[i].mpc_dstirq;
379 * Find a specific PCI IRQ entry.
380 * Not an __init, possibly needed by modules
382 static int pin_2_irq(int idx, int apic, int pin);
384 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
386 int apic, i, best_guess = -1;
388 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
390 if (mp_bus_id_to_pci_bus[bus] == -1) {
391 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
394 for (i = 0; i < mp_irq_entries; i++) {
395 int lbus = mp_irqs[i].mpc_srcbus;
397 for (apic = 0; apic < nr_ioapics; apic++)
398 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
399 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
402 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
403 !mp_irqs[i].mpc_irqtype &&
405 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
406 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
408 if (!(apic || IO_APIC_IRQ(irq)))
411 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
414 * Use the first all-but-pin matching entry as a
415 * best-guess fuzzy result for broken mptables.
425 * EISA Edge/Level control register, ELCR
427 static int EISA_ELCR(unsigned int irq)
430 unsigned int port = 0x4d0 + (irq >> 3);
431 return (inb(port) >> (irq & 7)) & 1;
433 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
437 /* EISA interrupts are always polarity zero and can be edge or level
438 * trigger depending on the ELCR value. If an interrupt is listed as
439 * EISA conforming in the MP table, that means its trigger type must
440 * be read in from the ELCR */
442 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
443 #define default_EISA_polarity(idx) (0)
445 /* ISA interrupts are always polarity zero edge triggered,
446 * when listed as conforming in the MP table. */
448 #define default_ISA_trigger(idx) (0)
449 #define default_ISA_polarity(idx) (0)
451 /* PCI interrupts are always polarity one level triggered,
452 * when listed as conforming in the MP table. */
454 #define default_PCI_trigger(idx) (1)
455 #define default_PCI_polarity(idx) (1)
457 /* MCA interrupts are always polarity zero level triggered,
458 * when listed as conforming in the MP table. */
460 #define default_MCA_trigger(idx) (1)
461 #define default_MCA_polarity(idx) (0)
463 static int __init MPBIOS_polarity(int idx)
465 int bus = mp_irqs[idx].mpc_srcbus;
469 * Determine IRQ line polarity (high active or low active):
471 switch (mp_irqs[idx].mpc_irqflag & 3)
473 case 0: /* conforms, ie. bus-type dependent polarity */
475 switch (mp_bus_id_to_type[bus])
477 case MP_BUS_ISA: /* ISA pin */
479 polarity = default_ISA_polarity(idx);
482 case MP_BUS_EISA: /* EISA pin */
484 polarity = default_EISA_polarity(idx);
487 case MP_BUS_PCI: /* PCI pin */
489 polarity = default_PCI_polarity(idx);
492 case MP_BUS_MCA: /* MCA pin */
494 polarity = default_MCA_polarity(idx);
499 printk(KERN_WARNING "broken BIOS!!\n");
506 case 1: /* high active */
511 case 2: /* reserved */
513 printk(KERN_WARNING "broken BIOS!!\n");
517 case 3: /* low active */
522 default: /* invalid */
524 printk(KERN_WARNING "broken BIOS!!\n");
532 static int MPBIOS_trigger(int idx)
534 int bus = mp_irqs[idx].mpc_srcbus;
538 * Determine IRQ trigger mode (edge or level sensitive):
540 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
542 case 0: /* conforms, ie. bus-type dependent */
544 switch (mp_bus_id_to_type[bus])
546 case MP_BUS_ISA: /* ISA pin */
548 trigger = default_ISA_trigger(idx);
551 case MP_BUS_EISA: /* EISA pin */
553 trigger = default_EISA_trigger(idx);
556 case MP_BUS_PCI: /* PCI pin */
558 trigger = default_PCI_trigger(idx);
561 case MP_BUS_MCA: /* MCA pin */
563 trigger = default_MCA_trigger(idx);
568 printk(KERN_WARNING "broken BIOS!!\n");
580 case 2: /* reserved */
582 printk(KERN_WARNING "broken BIOS!!\n");
591 default: /* invalid */
593 printk(KERN_WARNING "broken BIOS!!\n");
601 static inline int irq_polarity(int idx)
603 return MPBIOS_polarity(idx);
606 static inline int irq_trigger(int idx)
608 return MPBIOS_trigger(idx);
611 static int pin_2_irq(int idx, int apic, int pin)
614 int bus = mp_irqs[idx].mpc_srcbus;
617 * Debugging check, we are in big trouble if this message pops up!
619 if (mp_irqs[idx].mpc_dstirq != pin)
620 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
622 switch (mp_bus_id_to_type[bus])
624 case MP_BUS_ISA: /* ISA pin */
628 irq = mp_irqs[idx].mpc_srcbusirq;
631 case MP_BUS_PCI: /* PCI pin */
634 * PCI IRQs are mapped in order
638 irq += nr_ioapic_registers[i++];
644 printk(KERN_ERR "unknown bus type %d.\n",bus);
651 * PCI IRQ command line redirection. Yes, limits are hardcoded.
653 if ((pin >= 16) && (pin <= 23)) {
654 if (pirq_entries[pin-16] != -1) {
655 if (!pirq_entries[pin-16]) {
656 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
658 irq = pirq_entries[pin-16];
659 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
667 static inline int IO_APIC_irq_trigger(int irq)
671 for (apic = 0; apic < nr_ioapics; apic++) {
672 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
673 idx = find_irq_entry(apic,pin,mp_INT);
674 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
675 return irq_trigger(idx);
679 * nonexistent IRQs are edge default
684 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
685 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
687 int assign_irq_vector(int irq)
689 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
691 BUG_ON(irq >= NR_IRQ_VECTORS);
692 if (IO_APIC_VECTOR(irq) > 0)
693 return IO_APIC_VECTOR(irq);
696 if (current_vector == IA32_SYSCALL_VECTOR)
699 if (current_vector >= FIRST_SYSTEM_VECTOR) {
703 current_vector = FIRST_DEVICE_VECTOR + offset;
706 vector_irq[current_vector] = irq;
707 if (irq != AUTO_ASSIGN)
708 IO_APIC_VECTOR(irq) = current_vector;
710 return current_vector;
713 extern void (*interrupt[NR_IRQS])(void);
714 static struct hw_interrupt_type ioapic_level_type;
715 static struct hw_interrupt_type ioapic_edge_type;
717 #define IOAPIC_AUTO -1
718 #define IOAPIC_EDGE 0
719 #define IOAPIC_LEVEL 1
721 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
723 if (use_pci_vector() && !platform_legacy_irq(irq)) {
724 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
725 trigger == IOAPIC_LEVEL)
726 irq_desc[vector].handler = &ioapic_level_type;
728 irq_desc[vector].handler = &ioapic_edge_type;
729 set_intr_gate(vector, interrupt[vector]);
731 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
732 trigger == IOAPIC_LEVEL)
733 irq_desc[irq].handler = &ioapic_level_type;
735 irq_desc[irq].handler = &ioapic_edge_type;
736 set_intr_gate(vector, interrupt[irq]);
740 static void __init setup_IO_APIC_irqs(void)
742 struct IO_APIC_route_entry entry;
743 int apic, pin, idx, irq, first_notcon = 1, vector;
746 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
748 for (apic = 0; apic < nr_ioapics; apic++) {
749 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
752 * add it to the IO-APIC irq-routing table:
754 memset(&entry,0,sizeof(entry));
756 entry.delivery_mode = INT_DELIVERY_MODE;
757 entry.dest_mode = INT_DEST_MODE;
758 entry.mask = 0; /* enable IRQ */
759 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
761 idx = find_irq_entry(apic,pin,mp_INT);
764 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
767 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
771 entry.trigger = irq_trigger(idx);
772 entry.polarity = irq_polarity(idx);
774 if (irq_trigger(idx)) {
777 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
780 irq = pin_2_irq(idx, apic, pin);
781 add_pin_to_irq(irq, apic, pin);
783 if (!apic && !IO_APIC_IRQ(irq))
786 if (IO_APIC_IRQ(irq)) {
787 vector = assign_irq_vector(irq);
788 entry.vector = vector;
790 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
791 if (!apic && (irq < 16))
792 disable_8259A_irq(irq);
794 spin_lock_irqsave(&ioapic_lock, flags);
795 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
796 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
797 set_native_irq_info(irq, TARGET_CPUS);
798 spin_unlock_irqrestore(&ioapic_lock, flags);
803 apic_printk(APIC_VERBOSE," not connected.\n");
807 * Set up the 8259A-master output pin as broadcast to all
810 static void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
812 struct IO_APIC_route_entry entry;
815 memset(&entry,0,sizeof(entry));
817 disable_8259A_irq(0);
820 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
823 * We use logical delivery to get the timer IRQ
826 entry.dest_mode = INT_DEST_MODE;
827 entry.mask = 0; /* unmask IRQ now */
828 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
829 entry.delivery_mode = INT_DELIVERY_MODE;
832 entry.vector = vector;
835 * The timer IRQ doesn't have to know that behind the
836 * scene we have a 8259A-master in AEOI mode ...
838 irq_desc[0].handler = &ioapic_edge_type;
841 * Add it to the IO-APIC irq-routing table:
843 spin_lock_irqsave(&ioapic_lock, flags);
844 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
845 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
846 spin_unlock_irqrestore(&ioapic_lock, flags);
851 void __init UNEXPECTED_IO_APIC(void)
855 void __apicdebuginit print_IO_APIC(void)
858 union IO_APIC_reg_00 reg_00;
859 union IO_APIC_reg_01 reg_01;
860 union IO_APIC_reg_02 reg_02;
863 if (apic_verbosity == APIC_QUIET)
866 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
867 for (i = 0; i < nr_ioapics; i++)
868 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
869 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
872 * We are a bit conservative about what we expect. We have to
873 * know about every hardware change ASAP.
875 printk(KERN_INFO "testing the IO APIC.......................\n");
877 for (apic = 0; apic < nr_ioapics; apic++) {
879 spin_lock_irqsave(&ioapic_lock, flags);
880 reg_00.raw = io_apic_read(apic, 0);
881 reg_01.raw = io_apic_read(apic, 1);
882 if (reg_01.bits.version >= 0x10)
883 reg_02.raw = io_apic_read(apic, 2);
884 spin_unlock_irqrestore(&ioapic_lock, flags);
887 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
888 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
889 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
890 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
891 UNEXPECTED_IO_APIC();
893 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
894 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
895 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
896 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
897 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
898 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
899 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
900 (reg_01.bits.entries != 0x2E) &&
901 (reg_01.bits.entries != 0x3F) &&
902 (reg_01.bits.entries != 0x03)
904 UNEXPECTED_IO_APIC();
906 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
907 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
908 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
909 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
910 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
911 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
912 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
913 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
915 UNEXPECTED_IO_APIC();
916 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
917 UNEXPECTED_IO_APIC();
919 if (reg_01.bits.version >= 0x10) {
920 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
921 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
922 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
923 UNEXPECTED_IO_APIC();
926 printk(KERN_DEBUG ".... IRQ redirection table:\n");
928 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
929 " Stat Dest Deli Vect: \n");
931 for (i = 0; i <= reg_01.bits.entries; i++) {
932 struct IO_APIC_route_entry entry;
934 spin_lock_irqsave(&ioapic_lock, flags);
935 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
936 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
937 spin_unlock_irqrestore(&ioapic_lock, flags);
939 printk(KERN_DEBUG " %02x %03X %02X ",
941 entry.dest.logical.logical_dest,
942 entry.dest.physical.physical_dest
945 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
950 entry.delivery_status,
957 if (use_pci_vector())
958 printk(KERN_INFO "Using vector-based indexing\n");
959 printk(KERN_DEBUG "IRQ to pin mappings:\n");
960 for (i = 0; i < NR_IRQS; i++) {
961 struct irq_pin_list *entry = irq_2_pin + i;
964 if (use_pci_vector() && !platform_legacy_irq(i))
965 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
967 printk(KERN_DEBUG "IRQ%d ", i);
969 printk("-> %d:%d", entry->apic, entry->pin);
972 entry = irq_2_pin + entry->next;
977 printk(KERN_INFO ".................................... done.\n");
984 static __apicdebuginit void print_APIC_bitfield (int base)
989 if (apic_verbosity == APIC_QUIET)
992 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
993 for (i = 0; i < 8; i++) {
994 v = apic_read(base + i*0x10);
995 for (j = 0; j < 32; j++) {
1005 void __apicdebuginit print_local_APIC(void * dummy)
1007 unsigned int v, ver, maxlvt;
1009 if (apic_verbosity == APIC_QUIET)
1012 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1013 smp_processor_id(), hard_smp_processor_id());
1014 v = apic_read(APIC_ID);
1015 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1016 v = apic_read(APIC_LVR);
1017 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1018 ver = GET_APIC_VERSION(v);
1019 maxlvt = get_maxlvt();
1021 v = apic_read(APIC_TASKPRI);
1022 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1024 v = apic_read(APIC_ARBPRI);
1025 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1026 v & APIC_ARBPRI_MASK);
1027 v = apic_read(APIC_PROCPRI);
1028 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1030 v = apic_read(APIC_EOI);
1031 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1032 v = apic_read(APIC_RRR);
1033 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1034 v = apic_read(APIC_LDR);
1035 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1036 v = apic_read(APIC_DFR);
1037 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1038 v = apic_read(APIC_SPIV);
1039 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1041 printk(KERN_DEBUG "... APIC ISR field:\n");
1042 print_APIC_bitfield(APIC_ISR);
1043 printk(KERN_DEBUG "... APIC TMR field:\n");
1044 print_APIC_bitfield(APIC_TMR);
1045 printk(KERN_DEBUG "... APIC IRR field:\n");
1046 print_APIC_bitfield(APIC_IRR);
1048 v = apic_read(APIC_ESR);
1049 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1051 v = apic_read(APIC_ICR);
1052 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1053 v = apic_read(APIC_ICR2);
1054 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1056 v = apic_read(APIC_LVTT);
1057 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1059 if (maxlvt > 3) { /* PC is LVT#4. */
1060 v = apic_read(APIC_LVTPC);
1061 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1063 v = apic_read(APIC_LVT0);
1064 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1065 v = apic_read(APIC_LVT1);
1066 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1068 if (maxlvt > 2) { /* ERR is LVT#3. */
1069 v = apic_read(APIC_LVTERR);
1070 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1073 v = apic_read(APIC_TMICT);
1074 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1075 v = apic_read(APIC_TMCCT);
1076 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1077 v = apic_read(APIC_TDCR);
1078 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1082 void print_all_local_APICs (void)
1084 on_each_cpu(print_local_APIC, NULL, 1, 1);
1087 void __apicdebuginit print_PIC(void)
1090 unsigned long flags;
1092 if (apic_verbosity == APIC_QUIET)
1095 printk(KERN_DEBUG "\nprinting PIC contents\n");
1097 spin_lock_irqsave(&i8259A_lock, flags);
1099 v = inb(0xa1) << 8 | inb(0x21);
1100 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1102 v = inb(0xa0) << 8 | inb(0x20);
1103 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1107 v = inb(0xa0) << 8 | inb(0x20);
1111 spin_unlock_irqrestore(&i8259A_lock, flags);
1113 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1115 v = inb(0x4d1) << 8 | inb(0x4d0);
1116 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1121 static void __init enable_IO_APIC(void)
1123 union IO_APIC_reg_01 reg_01;
1125 unsigned long flags;
1127 for (i = 0; i < PIN_MAP_SIZE; i++) {
1128 irq_2_pin[i].pin = -1;
1129 irq_2_pin[i].next = 0;
1132 for (i = 0; i < MAX_PIRQS; i++)
1133 pirq_entries[i] = -1;
1136 * The number of IO-APIC IRQ registers (== #pins):
1138 for (i = 0; i < nr_ioapics; i++) {
1139 spin_lock_irqsave(&ioapic_lock, flags);
1140 reg_01.raw = io_apic_read(i, 1);
1141 spin_unlock_irqrestore(&ioapic_lock, flags);
1142 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1146 * Do not trust the IO-APIC being empty at bootup
1152 * Not an __init, needed by the reboot code
1154 void disable_IO_APIC(void)
1158 * Clear the IO-APIC before rebooting:
1163 * If the i8259 is routed through an IOAPIC
1164 * Put that IOAPIC in virtual wire mode
1165 * so legacy interrupts can be delivered.
1167 pin = find_isa_irq_pin(0, mp_ExtINT);
1169 struct IO_APIC_route_entry entry;
1170 unsigned long flags;
1172 memset(&entry, 0, sizeof(entry));
1173 entry.mask = 0; /* Enabled */
1174 entry.trigger = 0; /* Edge */
1176 entry.polarity = 0; /* High */
1177 entry.delivery_status = 0;
1178 entry.dest_mode = 0; /* Physical */
1179 entry.delivery_mode = 7; /* ExtInt */
1181 entry.dest.physical.physical_dest = 0;
1185 * Add it to the IO-APIC irq-routing table:
1187 spin_lock_irqsave(&ioapic_lock, flags);
1188 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
1189 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
1190 spin_unlock_irqrestore(&ioapic_lock, flags);
1193 disconnect_bsp_APIC(pin != -1);
1197 * function to set the IO-APIC physical IDs based on the
1198 * values stored in the MPC table.
1200 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1203 static void __init setup_ioapic_ids_from_mpc (void)
1205 union IO_APIC_reg_00 reg_00;
1208 unsigned char old_id;
1209 unsigned long flags;
1212 * Set the IOAPIC ID to the value stored in the MPC table.
1214 for (apic = 0; apic < nr_ioapics; apic++) {
1216 /* Read the register 0 value */
1217 spin_lock_irqsave(&ioapic_lock, flags);
1218 reg_00.raw = io_apic_read(apic, 0);
1219 spin_unlock_irqrestore(&ioapic_lock, flags);
1221 old_id = mp_ioapics[apic].mpc_apicid;
1224 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1228 * We need to adjust the IRQ routing table
1229 * if the ID changed.
1231 if (old_id != mp_ioapics[apic].mpc_apicid)
1232 for (i = 0; i < mp_irq_entries; i++)
1233 if (mp_irqs[i].mpc_dstapic == old_id)
1234 mp_irqs[i].mpc_dstapic
1235 = mp_ioapics[apic].mpc_apicid;
1238 * Read the right value from the MPC table and
1239 * write it into the ID register.
1241 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1242 mp_ioapics[apic].mpc_apicid);
1244 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1245 spin_lock_irqsave(&ioapic_lock, flags);
1246 io_apic_write(apic, 0, reg_00.raw);
1247 spin_unlock_irqrestore(&ioapic_lock, flags);
1252 spin_lock_irqsave(&ioapic_lock, flags);
1253 reg_00.raw = io_apic_read(apic, 0);
1254 spin_unlock_irqrestore(&ioapic_lock, flags);
1255 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1256 printk("could not set ID!\n");
1258 apic_printk(APIC_VERBOSE," ok.\n");
1263 * There is a nasty bug in some older SMP boards, their mptable lies
1264 * about the timer IRQ. We do the following to work around the situation:
1266 * - timer IRQ defaults to IO-APIC IRQ
1267 * - if this function detects that timer IRQs are defunct, then we fall
1268 * back to ISA timer IRQs
1270 static int __init timer_irq_works(void)
1272 unsigned long t1 = jiffies;
1275 /* Let ten ticks pass... */
1276 mdelay((10 * 1000) / HZ);
1279 * Expect a few ticks at least, to be sure some possible
1280 * glue logic does not lock up after one or two first
1281 * ticks in a non-ExtINT mode. Also the local APIC
1282 * might have cached one ExtINT interrupt. Finally, at
1283 * least one tick may be lost due to delays.
1287 if (jiffies - t1 > 4)
1293 * In the SMP+IOAPIC case it might happen that there are an unspecified
1294 * number of pending IRQ events unhandled. These cases are very rare,
1295 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1296 * better to do it this way as thus we do not have to be aware of
1297 * 'pending' interrupts in the IRQ path, except at this point.
1300 * Edge triggered needs to resend any interrupt
1301 * that was delayed but this is now handled in the device
1306 * Starting up a edge-triggered IO-APIC interrupt is
1307 * nasty - we need to make sure that we get the edge.
1308 * If it is already asserted for some reason, we need
1309 * return 1 to indicate that is was pending.
1311 * This is not complete - we should be able to fake
1312 * an edge even if it isn't on the 8259A...
1315 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1317 int was_pending = 0;
1318 unsigned long flags;
1320 spin_lock_irqsave(&ioapic_lock, flags);
1322 disable_8259A_irq(irq);
1323 if (i8259A_irq_pending(irq))
1326 __unmask_IO_APIC_irq(irq);
1327 spin_unlock_irqrestore(&ioapic_lock, flags);
1333 * Once we have recorded IRQ_PENDING already, we can mask the
1334 * interrupt for real. This prevents IRQ storms from unhandled
1337 static void ack_edge_ioapic_irq(unsigned int irq)
1340 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1341 == (IRQ_PENDING | IRQ_DISABLED))
1342 mask_IO_APIC_irq(irq);
1347 * Level triggered interrupts can just be masked,
1348 * and shutting down and starting up the interrupt
1349 * is the same as enabling and disabling them -- except
1350 * with a startup need to return a "was pending" value.
1352 * Level triggered interrupts are special because we
1353 * do not touch any IO-APIC register while handling
1354 * them. We ack the APIC in the end-IRQ handler, not
1355 * in the start-IRQ-handler. Protection against reentrance
1356 * from the same interrupt is still provided, both by the
1357 * generic IRQ layer and by the fact that an unacked local
1358 * APIC does not accept IRQs.
1360 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1362 unmask_IO_APIC_irq(irq);
1364 return 0; /* don't check for pending */
1367 static void end_level_ioapic_irq (unsigned int irq)
1373 #ifdef CONFIG_PCI_MSI
1374 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1376 int irq = vector_to_irq(vector);
1378 return startup_edge_ioapic_irq(irq);
1381 static void ack_edge_ioapic_vector(unsigned int vector)
1383 int irq = vector_to_irq(vector);
1385 move_native_irq(vector);
1386 ack_edge_ioapic_irq(irq);
1389 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1391 int irq = vector_to_irq(vector);
1393 return startup_level_ioapic_irq (irq);
1396 static void end_level_ioapic_vector (unsigned int vector)
1398 int irq = vector_to_irq(vector);
1400 move_native_irq(vector);
1401 end_level_ioapic_irq(irq);
1404 static void mask_IO_APIC_vector (unsigned int vector)
1406 int irq = vector_to_irq(vector);
1408 mask_IO_APIC_irq(irq);
1411 static void unmask_IO_APIC_vector (unsigned int vector)
1413 int irq = vector_to_irq(vector);
1415 unmask_IO_APIC_irq(irq);
1419 static void set_ioapic_affinity_vector (unsigned int vector,
1422 int irq = vector_to_irq(vector);
1424 set_native_irq_info(vector, cpu_mask);
1425 set_ioapic_affinity_irq(irq, cpu_mask);
1427 #endif // CONFIG_SMP
1428 #endif // CONFIG_PCI_MSI
1431 * Level and edge triggered IO-APIC interrupts need different handling,
1432 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1433 * handled with the level-triggered descriptor, but that one has slightly
1434 * more overhead. Level-triggered interrupts cannot be handled with the
1435 * edge-triggered handler, without risking IRQ storms and other ugly
1439 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1440 .typename = "IO-APIC-edge",
1441 .startup = startup_edge_ioapic,
1442 .shutdown = shutdown_edge_ioapic,
1443 .enable = enable_edge_ioapic,
1444 .disable = disable_edge_ioapic,
1445 .ack = ack_edge_ioapic,
1446 .end = end_edge_ioapic,
1448 .set_affinity = set_ioapic_affinity,
1452 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1453 .typename = "IO-APIC-level",
1454 .startup = startup_level_ioapic,
1455 .shutdown = shutdown_level_ioapic,
1456 .enable = enable_level_ioapic,
1457 .disable = disable_level_ioapic,
1458 .ack = mask_and_ack_level_ioapic,
1459 .end = end_level_ioapic,
1461 .set_affinity = set_ioapic_affinity,
1465 static inline void init_IO_APIC_traps(void)
1470 * NOTE! The local APIC isn't very good at handling
1471 * multiple interrupts at the same interrupt level.
1472 * As the interrupt level is determined by taking the
1473 * vector number and shifting that right by 4, we
1474 * want to spread these out a bit so that they don't
1475 * all fall in the same interrupt level.
1477 * Also, we've got to be careful not to trash gate
1478 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1480 for (irq = 0; irq < NR_IRQS ; irq++) {
1482 if (use_pci_vector()) {
1483 if (!platform_legacy_irq(tmp))
1484 if ((tmp = vector_to_irq(tmp)) == -1)
1487 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1489 * Hmm.. We don't have an entry for this,
1490 * so default to an old-fashioned 8259
1491 * interrupt if we can..
1494 make_8259A_irq(irq);
1496 /* Strange. Oh, well.. */
1497 irq_desc[irq].handler = &no_irq_type;
1502 static void enable_lapic_irq (unsigned int irq)
1506 v = apic_read(APIC_LVT0);
1507 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1510 static void disable_lapic_irq (unsigned int irq)
1514 v = apic_read(APIC_LVT0);
1515 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1518 static void ack_lapic_irq (unsigned int irq)
1523 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1525 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1526 .typename = "local-APIC-edge",
1527 .startup = NULL, /* startup_irq() not used for IRQ0 */
1528 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1529 .enable = enable_lapic_irq,
1530 .disable = disable_lapic_irq,
1531 .ack = ack_lapic_irq,
1532 .end = end_lapic_irq,
1535 static void setup_nmi (void)
1538 * Dirty trick to enable the NMI watchdog ...
1539 * We put the 8259A master into AEOI mode and
1540 * unmask on all local APICs LVT0 as NMI.
1542 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1543 * is from Maciej W. Rozycki - so we do not have to EOI from
1544 * the NMI handler or the timer interrupt.
1546 printk(KERN_INFO "activating NMI Watchdog ...");
1548 enable_NMI_through_LVT0(NULL);
1554 * This looks a bit hackish but it's about the only one way of sending
1555 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1556 * not support the ExtINT mode, unfortunately. We need to send these
1557 * cycles as some i82489DX-based boards have glue logic that keeps the
1558 * 8259A interrupt line asserted until INTA. --macro
1560 static inline void unlock_ExtINT_logic(void)
1563 struct IO_APIC_route_entry entry0, entry1;
1564 unsigned char save_control, save_freq_select;
1565 unsigned long flags;
1567 pin = find_isa_irq_pin(8, mp_INT);
1571 spin_lock_irqsave(&ioapic_lock, flags);
1572 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1573 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1574 spin_unlock_irqrestore(&ioapic_lock, flags);
1575 clear_IO_APIC_pin(0, pin);
1577 memset(&entry1, 0, sizeof(entry1));
1579 entry1.dest_mode = 0; /* physical delivery */
1580 entry1.mask = 0; /* unmask IRQ now */
1581 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1582 entry1.delivery_mode = dest_ExtINT;
1583 entry1.polarity = entry0.polarity;
1587 spin_lock_irqsave(&ioapic_lock, flags);
1588 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1589 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1590 spin_unlock_irqrestore(&ioapic_lock, flags);
1592 save_control = CMOS_READ(RTC_CONTROL);
1593 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1594 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1596 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1601 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1605 CMOS_WRITE(save_control, RTC_CONTROL);
1606 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1607 clear_IO_APIC_pin(0, pin);
1609 spin_lock_irqsave(&ioapic_lock, flags);
1610 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1611 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1612 spin_unlock_irqrestore(&ioapic_lock, flags);
1616 * This code may look a bit paranoid, but it's supposed to cooperate with
1617 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1618 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1619 * fanatically on his truly buggy board.
1621 static inline void check_timer(void)
1627 * get/set the timer IRQ vector:
1629 disable_8259A_irq(0);
1630 vector = assign_irq_vector(0);
1631 set_intr_gate(vector, interrupt[0]);
1634 * Subtle, code in do_timer_interrupt() expects an AEOI
1635 * mode for the 8259A whenever interrupts are routed
1636 * through I/O APICs. Also IRQ0 has to be enabled in
1637 * the 8259A which implies the virtual wire has to be
1638 * disabled in the local APIC.
1640 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1642 enable_8259A_irq(0);
1644 pin1 = find_isa_irq_pin(0, mp_INT);
1645 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1647 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1651 * Ok, does IRQ0 through the IOAPIC work?
1653 unmask_IO_APIC_irq(0);
1654 if (!no_timer_check && timer_irq_works()) {
1655 nmi_watchdog_default();
1656 if (nmi_watchdog == NMI_IO_APIC) {
1657 disable_8259A_irq(0);
1659 enable_8259A_irq(0);
1663 clear_IO_APIC_pin(0, pin1);
1664 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1667 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1669 apic_printk(APIC_VERBOSE,"\n..... (found pin %d) ...", pin2);
1671 * legacy devices should be connected to IO APIC #0
1673 setup_ExtINT_IRQ0_pin(pin2, vector);
1674 if (timer_irq_works()) {
1676 nmi_watchdog_default();
1677 if (nmi_watchdog == NMI_IO_APIC) {
1683 * Cleanup, just in case ...
1685 clear_IO_APIC_pin(0, pin2);
1687 printk(" failed.\n");
1690 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1694 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1696 disable_8259A_irq(0);
1697 irq_desc[0].handler = &lapic_irq_type;
1698 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1699 enable_8259A_irq(0);
1701 if (timer_irq_works()) {
1702 apic_printk(APIC_QUIET, " works.\n");
1705 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1706 apic_printk(APIC_VERBOSE," failed.\n");
1708 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1712 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1714 unlock_ExtINT_logic();
1716 if (timer_irq_works()) {
1717 apic_printk(APIC_VERBOSE," works.\n");
1720 apic_printk(APIC_VERBOSE," failed :(.\n");
1721 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1724 static int __init notimercheck(char *s)
1729 __setup("no_timer_check", notimercheck);
1733 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1734 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1735 * Linux doesn't really care, as it's not actually used
1736 * for any interrupt handling anyway.
1738 #define PIC_IRQS (1<<2)
1740 void __init setup_IO_APIC(void)
1745 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1747 io_apic_irqs = ~PIC_IRQS;
1749 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1752 * Set up the IO-APIC IRQ routing table.
1755 setup_ioapic_ids_from_mpc();
1757 setup_IO_APIC_irqs();
1758 init_IO_APIC_traps();
1764 struct sysfs_ioapic_data {
1765 struct sys_device dev;
1766 struct IO_APIC_route_entry entry[0];
1768 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1770 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1772 struct IO_APIC_route_entry *entry;
1773 struct sysfs_ioapic_data *data;
1774 unsigned long flags;
1777 data = container_of(dev, struct sysfs_ioapic_data, dev);
1778 entry = data->entry;
1779 spin_lock_irqsave(&ioapic_lock, flags);
1780 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1781 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1782 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1784 spin_unlock_irqrestore(&ioapic_lock, flags);
1789 static int ioapic_resume(struct sys_device *dev)
1791 struct IO_APIC_route_entry *entry;
1792 struct sysfs_ioapic_data *data;
1793 unsigned long flags;
1794 union IO_APIC_reg_00 reg_00;
1797 data = container_of(dev, struct sysfs_ioapic_data, dev);
1798 entry = data->entry;
1800 spin_lock_irqsave(&ioapic_lock, flags);
1801 reg_00.raw = io_apic_read(dev->id, 0);
1802 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1803 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1804 io_apic_write(dev->id, 0, reg_00.raw);
1806 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1807 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1808 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1810 spin_unlock_irqrestore(&ioapic_lock, flags);
1815 static struct sysdev_class ioapic_sysdev_class = {
1816 set_kset_name("ioapic"),
1817 .suspend = ioapic_suspend,
1818 .resume = ioapic_resume,
1821 static int __init ioapic_init_sysfs(void)
1823 struct sys_device * dev;
1824 int i, size, error = 0;
1826 error = sysdev_class_register(&ioapic_sysdev_class);
1830 for (i = 0; i < nr_ioapics; i++ ) {
1831 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1832 * sizeof(struct IO_APIC_route_entry);
1833 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1834 if (!mp_ioapic_data[i]) {
1835 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1838 memset(mp_ioapic_data[i], 0, size);
1839 dev = &mp_ioapic_data[i]->dev;
1841 dev->cls = &ioapic_sysdev_class;
1842 error = sysdev_register(dev);
1844 kfree(mp_ioapic_data[i]);
1845 mp_ioapic_data[i] = NULL;
1846 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1854 device_initcall(ioapic_init_sysfs);
1856 /* --------------------------------------------------------------------------
1857 ACPI-based IOAPIC Configuration
1858 -------------------------------------------------------------------------- */
1862 #define IO_APIC_MAX_ID 0xFE
1864 int __init io_apic_get_version (int ioapic)
1866 union IO_APIC_reg_01 reg_01;
1867 unsigned long flags;
1869 spin_lock_irqsave(&ioapic_lock, flags);
1870 reg_01.raw = io_apic_read(ioapic, 1);
1871 spin_unlock_irqrestore(&ioapic_lock, flags);
1873 return reg_01.bits.version;
1877 int __init io_apic_get_redir_entries (int ioapic)
1879 union IO_APIC_reg_01 reg_01;
1880 unsigned long flags;
1882 spin_lock_irqsave(&ioapic_lock, flags);
1883 reg_01.raw = io_apic_read(ioapic, 1);
1884 spin_unlock_irqrestore(&ioapic_lock, flags);
1886 return reg_01.bits.entries;
1890 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
1892 struct IO_APIC_route_entry entry;
1893 unsigned long flags;
1895 if (!IO_APIC_IRQ(irq)) {
1896 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1902 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1903 * Note that we mask (disable) IRQs now -- these get enabled when the
1904 * corresponding device driver registers for this IRQ.
1907 memset(&entry,0,sizeof(entry));
1909 entry.delivery_mode = INT_DELIVERY_MODE;
1910 entry.dest_mode = INT_DEST_MODE;
1911 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1912 entry.trigger = edge_level;
1913 entry.polarity = active_high_low;
1914 entry.mask = 1; /* Disabled (masked) */
1917 * IRQs < 16 are already in the irq_2_pin[] map
1920 add_pin_to_irq(irq, ioapic, pin);
1922 entry.vector = assign_irq_vector(irq);
1924 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1925 "IRQ %d Mode:%i Active:%i)\n", ioapic,
1926 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
1927 edge_level, active_high_low);
1929 ioapic_register_intr(irq, entry.vector, edge_level);
1931 if (!ioapic && (irq < 16))
1932 disable_8259A_irq(irq);
1934 spin_lock_irqsave(&ioapic_lock, flags);
1935 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
1936 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
1937 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
1938 spin_unlock_irqrestore(&ioapic_lock, flags);
1943 #endif /* CONFIG_ACPI */
1947 * This function currently is only a helper for the i386 smp boot process where
1948 * we need to reprogram the ioredtbls to cater for the cpus which have come online
1949 * so mask in all cases should simply be TARGET_CPUS
1952 void __init setup_ioapic_dest(void)
1954 int pin, ioapic, irq, irq_entry;
1956 if (skip_ioapic_setup == 1)
1959 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
1960 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
1961 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
1962 if (irq_entry == -1)
1964 irq = pin_2_irq(irq_entry, ioapic, pin);
1965 set_ioapic_affinity_irq(irq, TARGET_CPUS);