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 int disable_timer_pin_1 __initdata;
49 static DEFINE_SPINLOCK(ioapic_lock);
52 * # of IRQ routing registers
54 int nr_ioapic_registers[MAX_IO_APICS];
57 * Rough estimation of how many shared IRQs there are, can
60 #define MAX_PLUS_SHARED_IRQS NR_IRQS
61 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
64 * This is performance-critical, we want to do it O(1)
66 * the indexing order of this array favors 1:1 mappings
67 * between pins and IRQs.
70 static struct irq_pin_list {
71 short apic, pin, next;
72 } irq_2_pin[PIN_MAP_SIZE];
74 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
76 #define vector_to_irq(vector) \
77 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
79 #define vector_to_irq(vector) (vector)
82 #define __DO_ACTION(R, ACTION, FINAL) \
86 struct irq_pin_list *entry = irq_2_pin + irq; \
93 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
95 io_apic_modify(entry->apic, reg); \
98 entry = irq_2_pin + entry->next; \
104 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
110 cpus_and(tmp, mask, cpu_online_map);
114 cpus_and(mask, tmp, CPU_MASK_ALL);
116 dest = cpu_mask_to_apicid(mask);
119 * Only the high 8 bits are valid.
121 dest = SET_APIC_LOGICAL_ID(dest);
123 spin_lock_irqsave(&ioapic_lock, flags);
124 __DO_ACTION(1, = dest, )
125 set_irq_info(irq, mask);
126 spin_unlock_irqrestore(&ioapic_lock, flags);
131 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
132 * shared ISA-space IRQs, so we have to support them. We are super
133 * fast in the common case, and fast for shared ISA-space IRQs.
135 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
137 static int first_free_entry = NR_IRQS;
138 struct irq_pin_list *entry = irq_2_pin + irq;
141 entry = irq_2_pin + entry->next;
143 if (entry->pin != -1) {
144 entry->next = first_free_entry;
145 entry = irq_2_pin + entry->next;
146 if (++first_free_entry >= PIN_MAP_SIZE)
147 panic("io_apic.c: whoops");
154 #define DO_ACTION(name,R,ACTION, FINAL) \
156 static void name##_IO_APIC_irq (unsigned int irq) \
157 __DO_ACTION(R, ACTION, FINAL)
159 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
161 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
164 static void mask_IO_APIC_irq (unsigned int irq)
168 spin_lock_irqsave(&ioapic_lock, flags);
169 __mask_IO_APIC_irq(irq);
170 spin_unlock_irqrestore(&ioapic_lock, flags);
173 static void unmask_IO_APIC_irq (unsigned int irq)
177 spin_lock_irqsave(&ioapic_lock, flags);
178 __unmask_IO_APIC_irq(irq);
179 spin_unlock_irqrestore(&ioapic_lock, flags);
182 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
184 struct IO_APIC_route_entry entry;
187 /* Check delivery_mode to be sure we're not clearing an SMI pin */
188 spin_lock_irqsave(&ioapic_lock, flags);
189 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
190 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
191 spin_unlock_irqrestore(&ioapic_lock, flags);
192 if (entry.delivery_mode == dest_SMI)
195 * Disable it in the IO-APIC irq-routing table:
197 memset(&entry, 0, sizeof(entry));
199 spin_lock_irqsave(&ioapic_lock, flags);
200 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
201 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
202 spin_unlock_irqrestore(&ioapic_lock, flags);
205 static void clear_IO_APIC (void)
209 for (apic = 0; apic < nr_ioapics; apic++)
210 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
211 clear_IO_APIC_pin(apic, pin);
215 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
216 * specific CPU-side IRQs.
220 static int pirq_entries [MAX_PIRQS];
221 static int pirqs_enabled;
222 int skip_ioapic_setup;
225 /* dummy parsing: see setup.c */
227 static int __init disable_ioapic_setup(char *str)
229 skip_ioapic_setup = 1;
233 static int __init enable_ioapic_setup(char *str)
236 skip_ioapic_setup = 0;
240 __setup("noapic", disable_ioapic_setup);
241 __setup("apic", enable_ioapic_setup);
243 #include <asm/pci-direct.h>
244 #include <linux/pci_ids.h>
245 #include <linux/pci.h>
247 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
248 off. Check for an Nvidia or VIA PCI bridge and turn it off.
249 Use pci direct infrastructure because this runs before the PCI subsystem.
251 Can be overwritten with "apic"
253 And another hack to disable the IOMMU on VIA chipsets.
256 void __init check_ioapic(void)
262 /* Poor man's PCI discovery */
263 for (num = 0; num < 32; num++) {
264 for (slot = 0; slot < 32; slot++) {
265 for (func = 0; func < 8; func++) {
269 class = read_pci_config(num,slot,func,
271 if (class == 0xffffffff)
274 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
277 vendor = read_pci_config(num, slot, func,
281 case PCI_VENDOR_ID_VIA:
282 #ifdef CONFIG_GART_IOMMU
283 if ((end_pfn >= (0xffffffff>>PAGE_SHIFT) ||
285 !iommu_aperture_allowed) {
287 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
288 iommu_aperture_disabled = 1;
292 case PCI_VENDOR_ID_NVIDIA:
294 /* All timer overrides on Nvidia
295 seem to be wrong. Skip them. */
296 acpi_skip_timer_override = 1;
298 "Nvidia board detected. Ignoring ACPI timer override.\n");
300 /* RED-PEN skip them on mptables too? */
304 /* No multi-function device? */
305 type = read_pci_config_byte(num,slot,func,
314 static int __init ioapic_pirq_setup(char *str)
317 int ints[MAX_PIRQS+1];
319 get_options(str, ARRAY_SIZE(ints), ints);
321 for (i = 0; i < MAX_PIRQS; i++)
322 pirq_entries[i] = -1;
325 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
327 if (ints[0] < MAX_PIRQS)
330 for (i = 0; i < max; i++) {
331 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
333 * PIRQs are mapped upside down, usually.
335 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
340 __setup("pirq=", ioapic_pirq_setup);
343 * Find the IRQ entry number of a certain pin.
345 static int find_irq_entry(int apic, int pin, int type)
349 for (i = 0; i < mp_irq_entries; i++)
350 if (mp_irqs[i].mpc_irqtype == type &&
351 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
352 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
353 mp_irqs[i].mpc_dstirq == pin)
360 * Find the pin to which IRQ[irq] (ISA) is connected
362 static int find_isa_irq_pin(int irq, int type)
366 for (i = 0; i < mp_irq_entries; i++) {
367 int lbus = mp_irqs[i].mpc_srcbus;
369 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
370 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
371 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
372 (mp_irqs[i].mpc_irqtype == type) &&
373 (mp_irqs[i].mpc_srcbusirq == irq))
375 return mp_irqs[i].mpc_dstirq;
381 * Find a specific PCI IRQ entry.
382 * Not an __init, possibly needed by modules
384 static int pin_2_irq(int idx, int apic, int pin);
386 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
388 int apic, i, best_guess = -1;
390 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
392 if (mp_bus_id_to_pci_bus[bus] == -1) {
393 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
396 for (i = 0; i < mp_irq_entries; i++) {
397 int lbus = mp_irqs[i].mpc_srcbus;
399 for (apic = 0; apic < nr_ioapics; apic++)
400 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
401 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
404 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
405 !mp_irqs[i].mpc_irqtype &&
407 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
408 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
410 if (!(apic || IO_APIC_IRQ(irq)))
413 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
416 * Use the first all-but-pin matching entry as a
417 * best-guess fuzzy result for broken mptables.
427 * EISA Edge/Level control register, ELCR
429 static int EISA_ELCR(unsigned int irq)
432 unsigned int port = 0x4d0 + (irq >> 3);
433 return (inb(port) >> (irq & 7)) & 1;
435 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
439 /* EISA interrupts are always polarity zero and can be edge or level
440 * trigger depending on the ELCR value. If an interrupt is listed as
441 * EISA conforming in the MP table, that means its trigger type must
442 * be read in from the ELCR */
444 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
445 #define default_EISA_polarity(idx) (0)
447 /* ISA interrupts are always polarity zero edge triggered,
448 * when listed as conforming in the MP table. */
450 #define default_ISA_trigger(idx) (0)
451 #define default_ISA_polarity(idx) (0)
453 /* PCI interrupts are always polarity one level triggered,
454 * when listed as conforming in the MP table. */
456 #define default_PCI_trigger(idx) (1)
457 #define default_PCI_polarity(idx) (1)
459 /* MCA interrupts are always polarity zero level triggered,
460 * when listed as conforming in the MP table. */
462 #define default_MCA_trigger(idx) (1)
463 #define default_MCA_polarity(idx) (0)
465 static int __init MPBIOS_polarity(int idx)
467 int bus = mp_irqs[idx].mpc_srcbus;
471 * Determine IRQ line polarity (high active or low active):
473 switch (mp_irqs[idx].mpc_irqflag & 3)
475 case 0: /* conforms, ie. bus-type dependent polarity */
477 switch (mp_bus_id_to_type[bus])
479 case MP_BUS_ISA: /* ISA pin */
481 polarity = default_ISA_polarity(idx);
484 case MP_BUS_EISA: /* EISA pin */
486 polarity = default_EISA_polarity(idx);
489 case MP_BUS_PCI: /* PCI pin */
491 polarity = default_PCI_polarity(idx);
494 case MP_BUS_MCA: /* MCA pin */
496 polarity = default_MCA_polarity(idx);
501 printk(KERN_WARNING "broken BIOS!!\n");
508 case 1: /* high active */
513 case 2: /* reserved */
515 printk(KERN_WARNING "broken BIOS!!\n");
519 case 3: /* low active */
524 default: /* invalid */
526 printk(KERN_WARNING "broken BIOS!!\n");
534 static int MPBIOS_trigger(int idx)
536 int bus = mp_irqs[idx].mpc_srcbus;
540 * Determine IRQ trigger mode (edge or level sensitive):
542 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
544 case 0: /* conforms, ie. bus-type dependent */
546 switch (mp_bus_id_to_type[bus])
548 case MP_BUS_ISA: /* ISA pin */
550 trigger = default_ISA_trigger(idx);
553 case MP_BUS_EISA: /* EISA pin */
555 trigger = default_EISA_trigger(idx);
558 case MP_BUS_PCI: /* PCI pin */
560 trigger = default_PCI_trigger(idx);
563 case MP_BUS_MCA: /* MCA pin */
565 trigger = default_MCA_trigger(idx);
570 printk(KERN_WARNING "broken BIOS!!\n");
582 case 2: /* reserved */
584 printk(KERN_WARNING "broken BIOS!!\n");
593 default: /* invalid */
595 printk(KERN_WARNING "broken BIOS!!\n");
603 static inline int irq_polarity(int idx)
605 return MPBIOS_polarity(idx);
608 static inline int irq_trigger(int idx)
610 return MPBIOS_trigger(idx);
613 static int pin_2_irq(int idx, int apic, int pin)
616 int bus = mp_irqs[idx].mpc_srcbus;
619 * Debugging check, we are in big trouble if this message pops up!
621 if (mp_irqs[idx].mpc_dstirq != pin)
622 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
624 switch (mp_bus_id_to_type[bus])
626 case MP_BUS_ISA: /* ISA pin */
630 irq = mp_irqs[idx].mpc_srcbusirq;
633 case MP_BUS_PCI: /* PCI pin */
636 * PCI IRQs are mapped in order
640 irq += nr_ioapic_registers[i++];
646 printk(KERN_ERR "unknown bus type %d.\n",bus);
653 * PCI IRQ command line redirection. Yes, limits are hardcoded.
655 if ((pin >= 16) && (pin <= 23)) {
656 if (pirq_entries[pin-16] != -1) {
657 if (!pirq_entries[pin-16]) {
658 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
660 irq = pirq_entries[pin-16];
661 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
669 static inline int IO_APIC_irq_trigger(int irq)
673 for (apic = 0; apic < nr_ioapics; apic++) {
674 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
675 idx = find_irq_entry(apic,pin,mp_INT);
676 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
677 return irq_trigger(idx);
681 * nonexistent IRQs are edge default
686 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
687 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
689 int assign_irq_vector(int irq)
691 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
693 BUG_ON(irq >= NR_IRQ_VECTORS);
694 if (IO_APIC_VECTOR(irq) > 0)
695 return IO_APIC_VECTOR(irq);
698 if (current_vector == IA32_SYSCALL_VECTOR)
701 if (current_vector >= FIRST_SYSTEM_VECTOR) {
705 current_vector = FIRST_DEVICE_VECTOR + offset;
708 vector_irq[current_vector] = irq;
709 if (irq != AUTO_ASSIGN)
710 IO_APIC_VECTOR(irq) = current_vector;
712 return current_vector;
715 extern void (*interrupt[NR_IRQS])(void);
716 static struct hw_interrupt_type ioapic_level_type;
717 static struct hw_interrupt_type ioapic_edge_type;
719 #define IOAPIC_AUTO -1
720 #define IOAPIC_EDGE 0
721 #define IOAPIC_LEVEL 1
723 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
725 if (use_pci_vector() && !platform_legacy_irq(irq)) {
726 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
727 trigger == IOAPIC_LEVEL)
728 irq_desc[vector].handler = &ioapic_level_type;
730 irq_desc[vector].handler = &ioapic_edge_type;
731 set_intr_gate(vector, interrupt[vector]);
733 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
734 trigger == IOAPIC_LEVEL)
735 irq_desc[irq].handler = &ioapic_level_type;
737 irq_desc[irq].handler = &ioapic_edge_type;
738 set_intr_gate(vector, interrupt[irq]);
742 static void __init setup_IO_APIC_irqs(void)
744 struct IO_APIC_route_entry entry;
745 int apic, pin, idx, irq, first_notcon = 1, vector;
748 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
750 for (apic = 0; apic < nr_ioapics; apic++) {
751 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
754 * add it to the IO-APIC irq-routing table:
756 memset(&entry,0,sizeof(entry));
758 entry.delivery_mode = INT_DELIVERY_MODE;
759 entry.dest_mode = INT_DEST_MODE;
760 entry.mask = 0; /* enable IRQ */
761 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
763 idx = find_irq_entry(apic,pin,mp_INT);
766 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
769 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
773 entry.trigger = irq_trigger(idx);
774 entry.polarity = irq_polarity(idx);
776 if (irq_trigger(idx)) {
779 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
782 irq = pin_2_irq(idx, apic, pin);
783 add_pin_to_irq(irq, apic, pin);
785 if (!apic && !IO_APIC_IRQ(irq))
788 if (IO_APIC_IRQ(irq)) {
789 vector = assign_irq_vector(irq);
790 entry.vector = vector;
792 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
793 if (!apic && (irq < 16))
794 disable_8259A_irq(irq);
796 spin_lock_irqsave(&ioapic_lock, flags);
797 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
798 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
799 set_native_irq_info(irq, TARGET_CPUS);
800 spin_unlock_irqrestore(&ioapic_lock, flags);
805 apic_printk(APIC_VERBOSE," not connected.\n");
809 * Set up the 8259A-master output pin as broadcast to all
812 static void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
814 struct IO_APIC_route_entry entry;
817 memset(&entry,0,sizeof(entry));
819 disable_8259A_irq(0);
822 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
825 * We use logical delivery to get the timer IRQ
828 entry.dest_mode = INT_DEST_MODE;
829 entry.mask = 0; /* unmask IRQ now */
830 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
831 entry.delivery_mode = INT_DELIVERY_MODE;
834 entry.vector = vector;
837 * The timer IRQ doesn't have to know that behind the
838 * scene we have a 8259A-master in AEOI mode ...
840 irq_desc[0].handler = &ioapic_edge_type;
843 * Add it to the IO-APIC irq-routing table:
845 spin_lock_irqsave(&ioapic_lock, flags);
846 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
847 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
848 spin_unlock_irqrestore(&ioapic_lock, flags);
853 void __init UNEXPECTED_IO_APIC(void)
857 void __apicdebuginit print_IO_APIC(void)
860 union IO_APIC_reg_00 reg_00;
861 union IO_APIC_reg_01 reg_01;
862 union IO_APIC_reg_02 reg_02;
865 if (apic_verbosity == APIC_QUIET)
868 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
869 for (i = 0; i < nr_ioapics; i++)
870 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
871 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
874 * We are a bit conservative about what we expect. We have to
875 * know about every hardware change ASAP.
877 printk(KERN_INFO "testing the IO APIC.......................\n");
879 for (apic = 0; apic < nr_ioapics; apic++) {
881 spin_lock_irqsave(&ioapic_lock, flags);
882 reg_00.raw = io_apic_read(apic, 0);
883 reg_01.raw = io_apic_read(apic, 1);
884 if (reg_01.bits.version >= 0x10)
885 reg_02.raw = io_apic_read(apic, 2);
886 spin_unlock_irqrestore(&ioapic_lock, flags);
889 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
890 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
891 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
892 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
893 UNEXPECTED_IO_APIC();
895 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
896 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
897 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
898 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
899 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
900 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
901 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
902 (reg_01.bits.entries != 0x2E) &&
903 (reg_01.bits.entries != 0x3F) &&
904 (reg_01.bits.entries != 0x03)
906 UNEXPECTED_IO_APIC();
908 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
909 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
910 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
911 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
912 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
913 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
914 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
915 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
917 UNEXPECTED_IO_APIC();
918 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
919 UNEXPECTED_IO_APIC();
921 if (reg_01.bits.version >= 0x10) {
922 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
923 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
924 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
925 UNEXPECTED_IO_APIC();
928 printk(KERN_DEBUG ".... IRQ redirection table:\n");
930 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
931 " Stat Dest Deli Vect: \n");
933 for (i = 0; i <= reg_01.bits.entries; i++) {
934 struct IO_APIC_route_entry entry;
936 spin_lock_irqsave(&ioapic_lock, flags);
937 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
938 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
939 spin_unlock_irqrestore(&ioapic_lock, flags);
941 printk(KERN_DEBUG " %02x %03X %02X ",
943 entry.dest.logical.logical_dest,
944 entry.dest.physical.physical_dest
947 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
952 entry.delivery_status,
959 if (use_pci_vector())
960 printk(KERN_INFO "Using vector-based indexing\n");
961 printk(KERN_DEBUG "IRQ to pin mappings:\n");
962 for (i = 0; i < NR_IRQS; i++) {
963 struct irq_pin_list *entry = irq_2_pin + i;
966 if (use_pci_vector() && !platform_legacy_irq(i))
967 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
969 printk(KERN_DEBUG "IRQ%d ", i);
971 printk("-> %d:%d", entry->apic, entry->pin);
974 entry = irq_2_pin + entry->next;
979 printk(KERN_INFO ".................................... done.\n");
986 static __apicdebuginit void print_APIC_bitfield (int base)
991 if (apic_verbosity == APIC_QUIET)
994 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
995 for (i = 0; i < 8; i++) {
996 v = apic_read(base + i*0x10);
997 for (j = 0; j < 32; j++) {
1007 void __apicdebuginit print_local_APIC(void * dummy)
1009 unsigned int v, ver, maxlvt;
1011 if (apic_verbosity == APIC_QUIET)
1014 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1015 smp_processor_id(), hard_smp_processor_id());
1016 v = apic_read(APIC_ID);
1017 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1018 v = apic_read(APIC_LVR);
1019 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1020 ver = GET_APIC_VERSION(v);
1021 maxlvt = get_maxlvt();
1023 v = apic_read(APIC_TASKPRI);
1024 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1026 v = apic_read(APIC_ARBPRI);
1027 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1028 v & APIC_ARBPRI_MASK);
1029 v = apic_read(APIC_PROCPRI);
1030 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1032 v = apic_read(APIC_EOI);
1033 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1034 v = apic_read(APIC_RRR);
1035 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1036 v = apic_read(APIC_LDR);
1037 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1038 v = apic_read(APIC_DFR);
1039 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1040 v = apic_read(APIC_SPIV);
1041 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1043 printk(KERN_DEBUG "... APIC ISR field:\n");
1044 print_APIC_bitfield(APIC_ISR);
1045 printk(KERN_DEBUG "... APIC TMR field:\n");
1046 print_APIC_bitfield(APIC_TMR);
1047 printk(KERN_DEBUG "... APIC IRR field:\n");
1048 print_APIC_bitfield(APIC_IRR);
1050 v = apic_read(APIC_ESR);
1051 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1053 v = apic_read(APIC_ICR);
1054 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1055 v = apic_read(APIC_ICR2);
1056 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1058 v = apic_read(APIC_LVTT);
1059 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1061 if (maxlvt > 3) { /* PC is LVT#4. */
1062 v = apic_read(APIC_LVTPC);
1063 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1065 v = apic_read(APIC_LVT0);
1066 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1067 v = apic_read(APIC_LVT1);
1068 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1070 if (maxlvt > 2) { /* ERR is LVT#3. */
1071 v = apic_read(APIC_LVTERR);
1072 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1075 v = apic_read(APIC_TMICT);
1076 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1077 v = apic_read(APIC_TMCCT);
1078 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1079 v = apic_read(APIC_TDCR);
1080 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1084 void print_all_local_APICs (void)
1086 on_each_cpu(print_local_APIC, NULL, 1, 1);
1089 void __apicdebuginit print_PIC(void)
1092 unsigned long flags;
1094 if (apic_verbosity == APIC_QUIET)
1097 printk(KERN_DEBUG "\nprinting PIC contents\n");
1099 spin_lock_irqsave(&i8259A_lock, flags);
1101 v = inb(0xa1) << 8 | inb(0x21);
1102 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1104 v = inb(0xa0) << 8 | inb(0x20);
1105 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1109 v = inb(0xa0) << 8 | inb(0x20);
1113 spin_unlock_irqrestore(&i8259A_lock, flags);
1115 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1117 v = inb(0x4d1) << 8 | inb(0x4d0);
1118 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1123 static void __init enable_IO_APIC(void)
1125 union IO_APIC_reg_01 reg_01;
1127 unsigned long flags;
1129 for (i = 0; i < PIN_MAP_SIZE; i++) {
1130 irq_2_pin[i].pin = -1;
1131 irq_2_pin[i].next = 0;
1134 for (i = 0; i < MAX_PIRQS; i++)
1135 pirq_entries[i] = -1;
1138 * The number of IO-APIC IRQ registers (== #pins):
1140 for (i = 0; i < nr_ioapics; i++) {
1141 spin_lock_irqsave(&ioapic_lock, flags);
1142 reg_01.raw = io_apic_read(i, 1);
1143 spin_unlock_irqrestore(&ioapic_lock, flags);
1144 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1148 * Do not trust the IO-APIC being empty at bootup
1154 * Not an __init, needed by the reboot code
1156 void disable_IO_APIC(void)
1160 * Clear the IO-APIC before rebooting:
1165 * If the i8259 is routed through an IOAPIC
1166 * Put that IOAPIC in virtual wire mode
1167 * so legacy interrupts can be delivered.
1169 pin = find_isa_irq_pin(0, mp_ExtINT);
1171 struct IO_APIC_route_entry entry;
1172 unsigned long flags;
1174 memset(&entry, 0, sizeof(entry));
1175 entry.mask = 0; /* Enabled */
1176 entry.trigger = 0; /* Edge */
1178 entry.polarity = 0; /* High */
1179 entry.delivery_status = 0;
1180 entry.dest_mode = 0; /* Physical */
1181 entry.delivery_mode = 7; /* ExtInt */
1183 entry.dest.physical.physical_dest = 0;
1187 * Add it to the IO-APIC irq-routing table:
1189 spin_lock_irqsave(&ioapic_lock, flags);
1190 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
1191 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
1192 spin_unlock_irqrestore(&ioapic_lock, flags);
1195 disconnect_bsp_APIC(pin != -1);
1199 * function to set the IO-APIC physical IDs based on the
1200 * values stored in the MPC table.
1202 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1205 static void __init setup_ioapic_ids_from_mpc (void)
1207 union IO_APIC_reg_00 reg_00;
1210 unsigned char old_id;
1211 unsigned long flags;
1214 * Set the IOAPIC ID to the value stored in the MPC table.
1216 for (apic = 0; apic < nr_ioapics; apic++) {
1218 /* Read the register 0 value */
1219 spin_lock_irqsave(&ioapic_lock, flags);
1220 reg_00.raw = io_apic_read(apic, 0);
1221 spin_unlock_irqrestore(&ioapic_lock, flags);
1223 old_id = mp_ioapics[apic].mpc_apicid;
1226 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1230 * We need to adjust the IRQ routing table
1231 * if the ID changed.
1233 if (old_id != mp_ioapics[apic].mpc_apicid)
1234 for (i = 0; i < mp_irq_entries; i++)
1235 if (mp_irqs[i].mpc_dstapic == old_id)
1236 mp_irqs[i].mpc_dstapic
1237 = mp_ioapics[apic].mpc_apicid;
1240 * Read the right value from the MPC table and
1241 * write it into the ID register.
1243 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1244 mp_ioapics[apic].mpc_apicid);
1246 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1247 spin_lock_irqsave(&ioapic_lock, flags);
1248 io_apic_write(apic, 0, reg_00.raw);
1249 spin_unlock_irqrestore(&ioapic_lock, flags);
1254 spin_lock_irqsave(&ioapic_lock, flags);
1255 reg_00.raw = io_apic_read(apic, 0);
1256 spin_unlock_irqrestore(&ioapic_lock, flags);
1257 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1258 printk("could not set ID!\n");
1260 apic_printk(APIC_VERBOSE," ok.\n");
1265 * There is a nasty bug in some older SMP boards, their mptable lies
1266 * about the timer IRQ. We do the following to work around the situation:
1268 * - timer IRQ defaults to IO-APIC IRQ
1269 * - if this function detects that timer IRQs are defunct, then we fall
1270 * back to ISA timer IRQs
1272 static int __init timer_irq_works(void)
1274 unsigned long t1 = jiffies;
1277 /* Let ten ticks pass... */
1278 mdelay((10 * 1000) / HZ);
1281 * Expect a few ticks at least, to be sure some possible
1282 * glue logic does not lock up after one or two first
1283 * ticks in a non-ExtINT mode. Also the local APIC
1284 * might have cached one ExtINT interrupt. Finally, at
1285 * least one tick may be lost due to delays.
1289 if (jiffies - t1 > 4)
1295 * In the SMP+IOAPIC case it might happen that there are an unspecified
1296 * number of pending IRQ events unhandled. These cases are very rare,
1297 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1298 * better to do it this way as thus we do not have to be aware of
1299 * 'pending' interrupts in the IRQ path, except at this point.
1302 * Edge triggered needs to resend any interrupt
1303 * that was delayed but this is now handled in the device
1308 * Starting up a edge-triggered IO-APIC interrupt is
1309 * nasty - we need to make sure that we get the edge.
1310 * If it is already asserted for some reason, we need
1311 * return 1 to indicate that is was pending.
1313 * This is not complete - we should be able to fake
1314 * an edge even if it isn't on the 8259A...
1317 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1319 int was_pending = 0;
1320 unsigned long flags;
1322 spin_lock_irqsave(&ioapic_lock, flags);
1324 disable_8259A_irq(irq);
1325 if (i8259A_irq_pending(irq))
1328 __unmask_IO_APIC_irq(irq);
1329 spin_unlock_irqrestore(&ioapic_lock, flags);
1335 * Once we have recorded IRQ_PENDING already, we can mask the
1336 * interrupt for real. This prevents IRQ storms from unhandled
1339 static void ack_edge_ioapic_irq(unsigned int irq)
1342 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1343 == (IRQ_PENDING | IRQ_DISABLED))
1344 mask_IO_APIC_irq(irq);
1349 * Level triggered interrupts can just be masked,
1350 * and shutting down and starting up the interrupt
1351 * is the same as enabling and disabling them -- except
1352 * with a startup need to return a "was pending" value.
1354 * Level triggered interrupts are special because we
1355 * do not touch any IO-APIC register while handling
1356 * them. We ack the APIC in the end-IRQ handler, not
1357 * in the start-IRQ-handler. Protection against reentrance
1358 * from the same interrupt is still provided, both by the
1359 * generic IRQ layer and by the fact that an unacked local
1360 * APIC does not accept IRQs.
1362 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1364 unmask_IO_APIC_irq(irq);
1366 return 0; /* don't check for pending */
1369 static void end_level_ioapic_irq (unsigned int irq)
1375 #ifdef CONFIG_PCI_MSI
1376 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1378 int irq = vector_to_irq(vector);
1380 return startup_edge_ioapic_irq(irq);
1383 static void ack_edge_ioapic_vector(unsigned int vector)
1385 int irq = vector_to_irq(vector);
1387 move_native_irq(vector);
1388 ack_edge_ioapic_irq(irq);
1391 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1393 int irq = vector_to_irq(vector);
1395 return startup_level_ioapic_irq (irq);
1398 static void end_level_ioapic_vector (unsigned int vector)
1400 int irq = vector_to_irq(vector);
1402 move_native_irq(vector);
1403 end_level_ioapic_irq(irq);
1406 static void mask_IO_APIC_vector (unsigned int vector)
1408 int irq = vector_to_irq(vector);
1410 mask_IO_APIC_irq(irq);
1413 static void unmask_IO_APIC_vector (unsigned int vector)
1415 int irq = vector_to_irq(vector);
1417 unmask_IO_APIC_irq(irq);
1421 static void set_ioapic_affinity_vector (unsigned int vector,
1424 int irq = vector_to_irq(vector);
1426 set_native_irq_info(vector, cpu_mask);
1427 set_ioapic_affinity_irq(irq, cpu_mask);
1429 #endif // CONFIG_SMP
1430 #endif // CONFIG_PCI_MSI
1433 * Level and edge triggered IO-APIC interrupts need different handling,
1434 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1435 * handled with the level-triggered descriptor, but that one has slightly
1436 * more overhead. Level-triggered interrupts cannot be handled with the
1437 * edge-triggered handler, without risking IRQ storms and other ugly
1441 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1442 .typename = "IO-APIC-edge",
1443 .startup = startup_edge_ioapic,
1444 .shutdown = shutdown_edge_ioapic,
1445 .enable = enable_edge_ioapic,
1446 .disable = disable_edge_ioapic,
1447 .ack = ack_edge_ioapic,
1448 .end = end_edge_ioapic,
1450 .set_affinity = set_ioapic_affinity,
1454 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1455 .typename = "IO-APIC-level",
1456 .startup = startup_level_ioapic,
1457 .shutdown = shutdown_level_ioapic,
1458 .enable = enable_level_ioapic,
1459 .disable = disable_level_ioapic,
1460 .ack = mask_and_ack_level_ioapic,
1461 .end = end_level_ioapic,
1463 .set_affinity = set_ioapic_affinity,
1467 static inline void init_IO_APIC_traps(void)
1472 * NOTE! The local APIC isn't very good at handling
1473 * multiple interrupts at the same interrupt level.
1474 * As the interrupt level is determined by taking the
1475 * vector number and shifting that right by 4, we
1476 * want to spread these out a bit so that they don't
1477 * all fall in the same interrupt level.
1479 * Also, we've got to be careful not to trash gate
1480 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1482 for (irq = 0; irq < NR_IRQS ; irq++) {
1484 if (use_pci_vector()) {
1485 if (!platform_legacy_irq(tmp))
1486 if ((tmp = vector_to_irq(tmp)) == -1)
1489 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1491 * Hmm.. We don't have an entry for this,
1492 * so default to an old-fashioned 8259
1493 * interrupt if we can..
1496 make_8259A_irq(irq);
1498 /* Strange. Oh, well.. */
1499 irq_desc[irq].handler = &no_irq_type;
1504 static void enable_lapic_irq (unsigned int irq)
1508 v = apic_read(APIC_LVT0);
1509 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1512 static void disable_lapic_irq (unsigned int irq)
1516 v = apic_read(APIC_LVT0);
1517 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1520 static void ack_lapic_irq (unsigned int irq)
1525 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1527 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1528 .typename = "local-APIC-edge",
1529 .startup = NULL, /* startup_irq() not used for IRQ0 */
1530 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1531 .enable = enable_lapic_irq,
1532 .disable = disable_lapic_irq,
1533 .ack = ack_lapic_irq,
1534 .end = end_lapic_irq,
1537 static void setup_nmi (void)
1540 * Dirty trick to enable the NMI watchdog ...
1541 * We put the 8259A master into AEOI mode and
1542 * unmask on all local APICs LVT0 as NMI.
1544 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1545 * is from Maciej W. Rozycki - so we do not have to EOI from
1546 * the NMI handler or the timer interrupt.
1548 printk(KERN_INFO "activating NMI Watchdog ...");
1550 enable_NMI_through_LVT0(NULL);
1556 * This looks a bit hackish but it's about the only one way of sending
1557 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1558 * not support the ExtINT mode, unfortunately. We need to send these
1559 * cycles as some i82489DX-based boards have glue logic that keeps the
1560 * 8259A interrupt line asserted until INTA. --macro
1562 static inline void unlock_ExtINT_logic(void)
1565 struct IO_APIC_route_entry entry0, entry1;
1566 unsigned char save_control, save_freq_select;
1567 unsigned long flags;
1569 pin = find_isa_irq_pin(8, mp_INT);
1573 spin_lock_irqsave(&ioapic_lock, flags);
1574 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1575 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1576 spin_unlock_irqrestore(&ioapic_lock, flags);
1577 clear_IO_APIC_pin(0, pin);
1579 memset(&entry1, 0, sizeof(entry1));
1581 entry1.dest_mode = 0; /* physical delivery */
1582 entry1.mask = 0; /* unmask IRQ now */
1583 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1584 entry1.delivery_mode = dest_ExtINT;
1585 entry1.polarity = entry0.polarity;
1589 spin_lock_irqsave(&ioapic_lock, flags);
1590 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1591 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1592 spin_unlock_irqrestore(&ioapic_lock, flags);
1594 save_control = CMOS_READ(RTC_CONTROL);
1595 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1596 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1598 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1603 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1607 CMOS_WRITE(save_control, RTC_CONTROL);
1608 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1609 clear_IO_APIC_pin(0, pin);
1611 spin_lock_irqsave(&ioapic_lock, flags);
1612 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1613 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1614 spin_unlock_irqrestore(&ioapic_lock, flags);
1618 * This code may look a bit paranoid, but it's supposed to cooperate with
1619 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1620 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1621 * fanatically on his truly buggy board.
1623 static inline void check_timer(void)
1629 * get/set the timer IRQ vector:
1631 disable_8259A_irq(0);
1632 vector = assign_irq_vector(0);
1633 set_intr_gate(vector, interrupt[0]);
1636 * Subtle, code in do_timer_interrupt() expects an AEOI
1637 * mode for the 8259A whenever interrupts are routed
1638 * through I/O APICs. Also IRQ0 has to be enabled in
1639 * the 8259A which implies the virtual wire has to be
1640 * disabled in the local APIC.
1642 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1644 enable_8259A_irq(0);
1646 pin1 = find_isa_irq_pin(0, mp_INT);
1647 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1649 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1653 * Ok, does IRQ0 through the IOAPIC work?
1655 unmask_IO_APIC_irq(0);
1656 if (!no_timer_check && timer_irq_works()) {
1657 nmi_watchdog_default();
1658 if (nmi_watchdog == NMI_IO_APIC) {
1659 disable_8259A_irq(0);
1661 enable_8259A_irq(0);
1663 if (disable_timer_pin_1 > 0)
1664 clear_IO_APIC_pin(0, pin1);
1667 clear_IO_APIC_pin(0, pin1);
1668 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1671 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1673 apic_printk(APIC_VERBOSE,"\n..... (found pin %d) ...", pin2);
1675 * legacy devices should be connected to IO APIC #0
1677 setup_ExtINT_IRQ0_pin(pin2, vector);
1678 if (timer_irq_works()) {
1680 nmi_watchdog_default();
1681 if (nmi_watchdog == NMI_IO_APIC) {
1687 * Cleanup, just in case ...
1689 clear_IO_APIC_pin(0, pin2);
1691 printk(" failed.\n");
1694 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1698 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1700 disable_8259A_irq(0);
1701 irq_desc[0].handler = &lapic_irq_type;
1702 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1703 enable_8259A_irq(0);
1705 if (timer_irq_works()) {
1706 apic_printk(APIC_QUIET, " works.\n");
1709 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1710 apic_printk(APIC_VERBOSE," failed.\n");
1712 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1716 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1718 unlock_ExtINT_logic();
1720 if (timer_irq_works()) {
1721 apic_printk(APIC_VERBOSE," works.\n");
1724 apic_printk(APIC_VERBOSE," failed :(.\n");
1725 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1728 static int __init notimercheck(char *s)
1733 __setup("no_timer_check", notimercheck);
1737 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1738 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1739 * Linux doesn't really care, as it's not actually used
1740 * for any interrupt handling anyway.
1742 #define PIC_IRQS (1<<2)
1744 void __init setup_IO_APIC(void)
1749 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1751 io_apic_irqs = ~PIC_IRQS;
1753 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1756 * Set up the IO-APIC IRQ routing table.
1759 setup_ioapic_ids_from_mpc();
1761 setup_IO_APIC_irqs();
1762 init_IO_APIC_traps();
1768 struct sysfs_ioapic_data {
1769 struct sys_device dev;
1770 struct IO_APIC_route_entry entry[0];
1772 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1774 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1776 struct IO_APIC_route_entry *entry;
1777 struct sysfs_ioapic_data *data;
1778 unsigned long flags;
1781 data = container_of(dev, struct sysfs_ioapic_data, dev);
1782 entry = data->entry;
1783 spin_lock_irqsave(&ioapic_lock, flags);
1784 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1785 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1786 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1788 spin_unlock_irqrestore(&ioapic_lock, flags);
1793 static int ioapic_resume(struct sys_device *dev)
1795 struct IO_APIC_route_entry *entry;
1796 struct sysfs_ioapic_data *data;
1797 unsigned long flags;
1798 union IO_APIC_reg_00 reg_00;
1801 data = container_of(dev, struct sysfs_ioapic_data, dev);
1802 entry = data->entry;
1804 spin_lock_irqsave(&ioapic_lock, flags);
1805 reg_00.raw = io_apic_read(dev->id, 0);
1806 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1807 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1808 io_apic_write(dev->id, 0, reg_00.raw);
1810 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1811 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1812 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1814 spin_unlock_irqrestore(&ioapic_lock, flags);
1819 static struct sysdev_class ioapic_sysdev_class = {
1820 set_kset_name("ioapic"),
1821 .suspend = ioapic_suspend,
1822 .resume = ioapic_resume,
1825 static int __init ioapic_init_sysfs(void)
1827 struct sys_device * dev;
1828 int i, size, error = 0;
1830 error = sysdev_class_register(&ioapic_sysdev_class);
1834 for (i = 0; i < nr_ioapics; i++ ) {
1835 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1836 * sizeof(struct IO_APIC_route_entry);
1837 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1838 if (!mp_ioapic_data[i]) {
1839 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1842 memset(mp_ioapic_data[i], 0, size);
1843 dev = &mp_ioapic_data[i]->dev;
1845 dev->cls = &ioapic_sysdev_class;
1846 error = sysdev_register(dev);
1848 kfree(mp_ioapic_data[i]);
1849 mp_ioapic_data[i] = NULL;
1850 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1858 device_initcall(ioapic_init_sysfs);
1860 /* --------------------------------------------------------------------------
1861 ACPI-based IOAPIC Configuration
1862 -------------------------------------------------------------------------- */
1866 #define IO_APIC_MAX_ID 0xFE
1868 int __init io_apic_get_version (int ioapic)
1870 union IO_APIC_reg_01 reg_01;
1871 unsigned long flags;
1873 spin_lock_irqsave(&ioapic_lock, flags);
1874 reg_01.raw = io_apic_read(ioapic, 1);
1875 spin_unlock_irqrestore(&ioapic_lock, flags);
1877 return reg_01.bits.version;
1881 int __init io_apic_get_redir_entries (int ioapic)
1883 union IO_APIC_reg_01 reg_01;
1884 unsigned long flags;
1886 spin_lock_irqsave(&ioapic_lock, flags);
1887 reg_01.raw = io_apic_read(ioapic, 1);
1888 spin_unlock_irqrestore(&ioapic_lock, flags);
1890 return reg_01.bits.entries;
1894 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
1896 struct IO_APIC_route_entry entry;
1897 unsigned long flags;
1899 if (!IO_APIC_IRQ(irq)) {
1900 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1906 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1907 * Note that we mask (disable) IRQs now -- these get enabled when the
1908 * corresponding device driver registers for this IRQ.
1911 memset(&entry,0,sizeof(entry));
1913 entry.delivery_mode = INT_DELIVERY_MODE;
1914 entry.dest_mode = INT_DEST_MODE;
1915 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1916 entry.trigger = edge_level;
1917 entry.polarity = active_high_low;
1918 entry.mask = 1; /* Disabled (masked) */
1921 * IRQs < 16 are already in the irq_2_pin[] map
1924 add_pin_to_irq(irq, ioapic, pin);
1926 entry.vector = assign_irq_vector(irq);
1928 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1929 "IRQ %d Mode:%i Active:%i)\n", ioapic,
1930 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
1931 edge_level, active_high_low);
1933 ioapic_register_intr(irq, entry.vector, edge_level);
1935 if (!ioapic && (irq < 16))
1936 disable_8259A_irq(irq);
1938 spin_lock_irqsave(&ioapic_lock, flags);
1939 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
1940 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
1941 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
1942 spin_unlock_irqrestore(&ioapic_lock, flags);
1947 #endif /* CONFIG_ACPI */
1951 * This function currently is only a helper for the i386 smp boot process where
1952 * we need to reprogram the ioredtbls to cater for the cpus which have come online
1953 * so mask in all cases should simply be TARGET_CPUS
1956 void __init setup_ioapic_dest(void)
1958 int pin, ioapic, irq, irq_entry;
1960 if (skip_ioapic_setup == 1)
1963 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
1964 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
1965 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
1966 if (irq_entry == -1)
1968 irq = pin_2_irq(irq_entry, ioapic, pin);
1969 set_ioapic_affinity_irq(irq, TARGET_CPUS);