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>
39 #include <asm/mach_apic.h>
42 #define __apicdebuginit __init
44 int sis_apic_bug; /* not actually supported, dummy for compile */
46 static int no_timer_check;
48 static DEFINE_SPINLOCK(ioapic_lock);
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 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, )
135 static void mask_IO_APIC_irq (unsigned int irq)
139 spin_lock_irqsave(&ioapic_lock, flags);
140 __mask_IO_APIC_irq(irq);
141 spin_unlock_irqrestore(&ioapic_lock, flags);
144 static void unmask_IO_APIC_irq (unsigned int irq)
148 spin_lock_irqsave(&ioapic_lock, flags);
149 __unmask_IO_APIC_irq(irq);
150 spin_unlock_irqrestore(&ioapic_lock, flags);
153 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
155 struct IO_APIC_route_entry entry;
158 /* Check delivery_mode to be sure we're not clearing an SMI pin */
159 spin_lock_irqsave(&ioapic_lock, flags);
160 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
161 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
162 spin_unlock_irqrestore(&ioapic_lock, flags);
163 if (entry.delivery_mode == dest_SMI)
166 * Disable it in the IO-APIC irq-routing table:
168 memset(&entry, 0, sizeof(entry));
170 spin_lock_irqsave(&ioapic_lock, flags);
171 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
172 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
173 spin_unlock_irqrestore(&ioapic_lock, flags);
176 static void clear_IO_APIC (void)
180 for (apic = 0; apic < nr_ioapics; apic++)
181 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
182 clear_IO_APIC_pin(apic, pin);
186 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
187 * specific CPU-side IRQs.
191 static int pirq_entries [MAX_PIRQS];
192 static int pirqs_enabled;
193 int skip_ioapic_setup;
196 /* dummy parsing: see setup.c */
198 static int __init disable_ioapic_setup(char *str)
200 skip_ioapic_setup = 1;
204 static int __init enable_ioapic_setup(char *str)
207 skip_ioapic_setup = 0;
211 __setup("noapic", disable_ioapic_setup);
212 __setup("apic", enable_ioapic_setup);
214 #include <asm/pci-direct.h>
215 #include <linux/pci_ids.h>
216 #include <linux/pci.h>
218 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
219 off. Check for an Nvidia or VIA PCI bridge and turn it off.
220 Use pci direct infrastructure because this runs before the PCI subsystem.
222 Can be overwritten with "apic"
224 And another hack to disable the IOMMU on VIA chipsets.
227 void __init check_ioapic(void)
233 /* Poor man's PCI discovery */
234 for (num = 0; num < 32; num++) {
235 for (slot = 0; slot < 32; slot++) {
236 for (func = 0; func < 8; func++) {
240 class = read_pci_config(num,slot,func,
242 if (class == 0xffffffff)
245 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
248 vendor = read_pci_config(num, slot, func,
252 case PCI_VENDOR_ID_VIA:
253 #ifdef CONFIG_GART_IOMMU
254 if ((end_pfn >= (0xffffffff>>PAGE_SHIFT) ||
256 !iommu_aperture_allowed) {
258 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
259 iommu_aperture_disabled = 1;
263 case PCI_VENDOR_ID_NVIDIA:
265 /* All timer overrides on Nvidia
266 seem to be wrong. Skip them. */
267 acpi_skip_timer_override = 1;
269 "Nvidia board detected. Ignoring ACPI timer override.\n");
271 /* RED-PEN skip them on mptables too? */
275 /* No multi-function device? */
276 type = read_pci_config_byte(num,slot,func,
285 static int __init ioapic_pirq_setup(char *str)
288 int ints[MAX_PIRQS+1];
290 get_options(str, ARRAY_SIZE(ints), ints);
292 for (i = 0; i < MAX_PIRQS; i++)
293 pirq_entries[i] = -1;
296 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
298 if (ints[0] < MAX_PIRQS)
301 for (i = 0; i < max; i++) {
302 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
304 * PIRQs are mapped upside down, usually.
306 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
311 __setup("pirq=", ioapic_pirq_setup);
314 * Find the IRQ entry number of a certain pin.
316 static int find_irq_entry(int apic, int pin, int type)
320 for (i = 0; i < mp_irq_entries; i++)
321 if (mp_irqs[i].mpc_irqtype == type &&
322 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
323 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
324 mp_irqs[i].mpc_dstirq == pin)
331 * Find the pin to which IRQ[irq] (ISA) is connected
333 static int __init find_isa_irq_pin(int irq, int type)
337 for (i = 0; i < mp_irq_entries; i++) {
338 int lbus = mp_irqs[i].mpc_srcbus;
340 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
341 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
342 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
343 (mp_irqs[i].mpc_irqtype == type) &&
344 (mp_irqs[i].mpc_srcbusirq == irq))
346 return mp_irqs[i].mpc_dstirq;
352 * Find a specific PCI IRQ entry.
353 * Not an __init, possibly needed by modules
355 static int pin_2_irq(int idx, int apic, int pin);
357 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
359 int apic, i, best_guess = -1;
361 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
363 if (mp_bus_id_to_pci_bus[bus] == -1) {
364 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
367 for (i = 0; i < mp_irq_entries; i++) {
368 int lbus = mp_irqs[i].mpc_srcbus;
370 for (apic = 0; apic < nr_ioapics; apic++)
371 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
372 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
375 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
376 !mp_irqs[i].mpc_irqtype &&
378 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
379 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
381 if (!(apic || IO_APIC_IRQ(irq)))
384 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
387 * Use the first all-but-pin matching entry as a
388 * best-guess fuzzy result for broken mptables.
398 * EISA Edge/Level control register, ELCR
400 static int EISA_ELCR(unsigned int irq)
403 unsigned int port = 0x4d0 + (irq >> 3);
404 return (inb(port) >> (irq & 7)) & 1;
406 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
410 /* EISA interrupts are always polarity zero and can be edge or level
411 * trigger depending on the ELCR value. If an interrupt is listed as
412 * EISA conforming in the MP table, that means its trigger type must
413 * be read in from the ELCR */
415 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
416 #define default_EISA_polarity(idx) (0)
418 /* ISA interrupts are always polarity zero edge triggered,
419 * when listed as conforming in the MP table. */
421 #define default_ISA_trigger(idx) (0)
422 #define default_ISA_polarity(idx) (0)
424 /* PCI interrupts are always polarity one level triggered,
425 * when listed as conforming in the MP table. */
427 #define default_PCI_trigger(idx) (1)
428 #define default_PCI_polarity(idx) (1)
430 /* MCA interrupts are always polarity zero level triggered,
431 * when listed as conforming in the MP table. */
433 #define default_MCA_trigger(idx) (1)
434 #define default_MCA_polarity(idx) (0)
436 static int __init MPBIOS_polarity(int idx)
438 int bus = mp_irqs[idx].mpc_srcbus;
442 * Determine IRQ line polarity (high active or low active):
444 switch (mp_irqs[idx].mpc_irqflag & 3)
446 case 0: /* conforms, ie. bus-type dependent polarity */
448 switch (mp_bus_id_to_type[bus])
450 case MP_BUS_ISA: /* ISA pin */
452 polarity = default_ISA_polarity(idx);
455 case MP_BUS_EISA: /* EISA pin */
457 polarity = default_EISA_polarity(idx);
460 case MP_BUS_PCI: /* PCI pin */
462 polarity = default_PCI_polarity(idx);
465 case MP_BUS_MCA: /* MCA pin */
467 polarity = default_MCA_polarity(idx);
472 printk(KERN_WARNING "broken BIOS!!\n");
479 case 1: /* high active */
484 case 2: /* reserved */
486 printk(KERN_WARNING "broken BIOS!!\n");
490 case 3: /* low active */
495 default: /* invalid */
497 printk(KERN_WARNING "broken BIOS!!\n");
505 static int MPBIOS_trigger(int idx)
507 int bus = mp_irqs[idx].mpc_srcbus;
511 * Determine IRQ trigger mode (edge or level sensitive):
513 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
515 case 0: /* conforms, ie. bus-type dependent */
517 switch (mp_bus_id_to_type[bus])
519 case MP_BUS_ISA: /* ISA pin */
521 trigger = default_ISA_trigger(idx);
524 case MP_BUS_EISA: /* EISA pin */
526 trigger = default_EISA_trigger(idx);
529 case MP_BUS_PCI: /* PCI pin */
531 trigger = default_PCI_trigger(idx);
534 case MP_BUS_MCA: /* MCA pin */
536 trigger = default_MCA_trigger(idx);
541 printk(KERN_WARNING "broken BIOS!!\n");
553 case 2: /* reserved */
555 printk(KERN_WARNING "broken BIOS!!\n");
564 default: /* invalid */
566 printk(KERN_WARNING "broken BIOS!!\n");
574 static inline int irq_polarity(int idx)
576 return MPBIOS_polarity(idx);
579 static inline int irq_trigger(int idx)
581 return MPBIOS_trigger(idx);
584 static int pin_2_irq(int idx, int apic, int pin)
587 int bus = mp_irqs[idx].mpc_srcbus;
590 * Debugging check, we are in big trouble if this message pops up!
592 if (mp_irqs[idx].mpc_dstirq != pin)
593 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
595 switch (mp_bus_id_to_type[bus])
597 case MP_BUS_ISA: /* ISA pin */
601 irq = mp_irqs[idx].mpc_srcbusirq;
604 case MP_BUS_PCI: /* PCI pin */
607 * PCI IRQs are mapped in order
611 irq += nr_ioapic_registers[i++];
617 printk(KERN_ERR "unknown bus type %d.\n",bus);
624 * PCI IRQ command line redirection. Yes, limits are hardcoded.
626 if ((pin >= 16) && (pin <= 23)) {
627 if (pirq_entries[pin-16] != -1) {
628 if (!pirq_entries[pin-16]) {
629 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
631 irq = pirq_entries[pin-16];
632 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
640 static inline int IO_APIC_irq_trigger(int irq)
644 for (apic = 0; apic < nr_ioapics; apic++) {
645 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
646 idx = find_irq_entry(apic,pin,mp_INT);
647 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
648 return irq_trigger(idx);
652 * nonexistent IRQs are edge default
657 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
658 u8 irq_vector[NR_IRQ_VECTORS] = { FIRST_DEVICE_VECTOR , 0 };
660 int assign_irq_vector(int irq)
662 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
664 BUG_ON(irq >= NR_IRQ_VECTORS);
665 if (IO_APIC_VECTOR(irq) > 0)
666 return IO_APIC_VECTOR(irq);
669 if (current_vector == IA32_SYSCALL_VECTOR)
672 if (current_vector >= FIRST_SYSTEM_VECTOR) {
676 current_vector = FIRST_DEVICE_VECTOR + offset;
679 vector_irq[current_vector] = irq;
680 if (irq != AUTO_ASSIGN)
681 IO_APIC_VECTOR(irq) = current_vector;
683 return current_vector;
686 extern void (*interrupt[NR_IRQS])(void);
687 static struct hw_interrupt_type ioapic_level_type;
688 static struct hw_interrupt_type ioapic_edge_type;
690 #define IOAPIC_AUTO -1
691 #define IOAPIC_EDGE 0
692 #define IOAPIC_LEVEL 1
694 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
696 if (use_pci_vector() && !platform_legacy_irq(irq)) {
697 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
698 trigger == IOAPIC_LEVEL)
699 irq_desc[vector].handler = &ioapic_level_type;
701 irq_desc[vector].handler = &ioapic_edge_type;
702 set_intr_gate(vector, interrupt[vector]);
704 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
705 trigger == IOAPIC_LEVEL)
706 irq_desc[irq].handler = &ioapic_level_type;
708 irq_desc[irq].handler = &ioapic_edge_type;
709 set_intr_gate(vector, interrupt[irq]);
713 static void __init setup_IO_APIC_irqs(void)
715 struct IO_APIC_route_entry entry;
716 int apic, pin, idx, irq, first_notcon = 1, vector;
719 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
721 for (apic = 0; apic < nr_ioapics; apic++) {
722 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
725 * add it to the IO-APIC irq-routing table:
727 memset(&entry,0,sizeof(entry));
729 entry.delivery_mode = INT_DELIVERY_MODE;
730 entry.dest_mode = INT_DEST_MODE;
731 entry.mask = 0; /* enable IRQ */
732 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
734 idx = find_irq_entry(apic,pin,mp_INT);
737 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
740 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
744 entry.trigger = irq_trigger(idx);
745 entry.polarity = irq_polarity(idx);
747 if (irq_trigger(idx)) {
750 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
753 irq = pin_2_irq(idx, apic, pin);
754 add_pin_to_irq(irq, apic, pin);
756 if (!apic && !IO_APIC_IRQ(irq))
759 if (IO_APIC_IRQ(irq)) {
760 vector = assign_irq_vector(irq);
761 entry.vector = vector;
763 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
764 if (!apic && (irq < 16))
765 disable_8259A_irq(irq);
767 spin_lock_irqsave(&ioapic_lock, flags);
768 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
769 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
770 spin_unlock_irqrestore(&ioapic_lock, flags);
775 apic_printk(APIC_VERBOSE," not connected.\n");
779 * Set up the 8259A-master output pin as broadcast to all
782 static void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
784 struct IO_APIC_route_entry entry;
787 memset(&entry,0,sizeof(entry));
789 disable_8259A_irq(0);
792 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
795 * We use logical delivery to get the timer IRQ
798 entry.dest_mode = INT_DEST_MODE;
799 entry.mask = 0; /* unmask IRQ now */
800 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
801 entry.delivery_mode = INT_DELIVERY_MODE;
804 entry.vector = vector;
807 * The timer IRQ doesn't have to know that behind the
808 * scene we have a 8259A-master in AEOI mode ...
810 irq_desc[0].handler = &ioapic_edge_type;
813 * Add it to the IO-APIC irq-routing table:
815 spin_lock_irqsave(&ioapic_lock, flags);
816 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
817 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
818 spin_unlock_irqrestore(&ioapic_lock, flags);
823 void __init UNEXPECTED_IO_APIC(void)
827 void __apicdebuginit print_IO_APIC(void)
830 union IO_APIC_reg_00 reg_00;
831 union IO_APIC_reg_01 reg_01;
832 union IO_APIC_reg_02 reg_02;
835 if (apic_verbosity == APIC_QUIET)
838 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
839 for (i = 0; i < nr_ioapics; i++)
840 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
841 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
844 * We are a bit conservative about what we expect. We have to
845 * know about every hardware change ASAP.
847 printk(KERN_INFO "testing the IO APIC.......................\n");
849 for (apic = 0; apic < nr_ioapics; apic++) {
851 spin_lock_irqsave(&ioapic_lock, flags);
852 reg_00.raw = io_apic_read(apic, 0);
853 reg_01.raw = io_apic_read(apic, 1);
854 if (reg_01.bits.version >= 0x10)
855 reg_02.raw = io_apic_read(apic, 2);
856 spin_unlock_irqrestore(&ioapic_lock, flags);
859 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
860 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
861 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
862 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
863 UNEXPECTED_IO_APIC();
865 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
866 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
867 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
868 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
869 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
870 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
871 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
872 (reg_01.bits.entries != 0x2E) &&
873 (reg_01.bits.entries != 0x3F) &&
874 (reg_01.bits.entries != 0x03)
876 UNEXPECTED_IO_APIC();
878 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
879 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
880 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
881 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
882 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
883 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
884 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
885 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
887 UNEXPECTED_IO_APIC();
888 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
889 UNEXPECTED_IO_APIC();
891 if (reg_01.bits.version >= 0x10) {
892 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
893 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
894 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
895 UNEXPECTED_IO_APIC();
898 printk(KERN_DEBUG ".... IRQ redirection table:\n");
900 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
901 " Stat Dest Deli Vect: \n");
903 for (i = 0; i <= reg_01.bits.entries; i++) {
904 struct IO_APIC_route_entry entry;
906 spin_lock_irqsave(&ioapic_lock, flags);
907 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
908 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
909 spin_unlock_irqrestore(&ioapic_lock, flags);
911 printk(KERN_DEBUG " %02x %03X %02X ",
913 entry.dest.logical.logical_dest,
914 entry.dest.physical.physical_dest
917 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
922 entry.delivery_status,
929 if (use_pci_vector())
930 printk(KERN_INFO "Using vector-based indexing\n");
931 printk(KERN_DEBUG "IRQ to pin mappings:\n");
932 for (i = 0; i < NR_IRQS; i++) {
933 struct irq_pin_list *entry = irq_2_pin + i;
936 if (use_pci_vector() && !platform_legacy_irq(i))
937 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
939 printk(KERN_DEBUG "IRQ%d ", i);
941 printk("-> %d:%d", entry->apic, entry->pin);
944 entry = irq_2_pin + entry->next;
949 printk(KERN_INFO ".................................... done.\n");
956 static __apicdebuginit void print_APIC_bitfield (int base)
961 if (apic_verbosity == APIC_QUIET)
964 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
965 for (i = 0; i < 8; i++) {
966 v = apic_read(base + i*0x10);
967 for (j = 0; j < 32; j++) {
977 void __apicdebuginit print_local_APIC(void * dummy)
979 unsigned int v, ver, maxlvt;
981 if (apic_verbosity == APIC_QUIET)
984 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
985 smp_processor_id(), hard_smp_processor_id());
986 v = apic_read(APIC_ID);
987 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
988 v = apic_read(APIC_LVR);
989 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
990 ver = GET_APIC_VERSION(v);
991 maxlvt = get_maxlvt();
993 v = apic_read(APIC_TASKPRI);
994 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
996 if (APIC_INTEGRATED(ver)) { /* !82489DX */
997 v = apic_read(APIC_ARBPRI);
998 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
999 v & APIC_ARBPRI_MASK);
1000 v = apic_read(APIC_PROCPRI);
1001 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1004 v = apic_read(APIC_EOI);
1005 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1006 v = apic_read(APIC_RRR);
1007 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1008 v = apic_read(APIC_LDR);
1009 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1010 v = apic_read(APIC_DFR);
1011 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1012 v = apic_read(APIC_SPIV);
1013 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1015 printk(KERN_DEBUG "... APIC ISR field:\n");
1016 print_APIC_bitfield(APIC_ISR);
1017 printk(KERN_DEBUG "... APIC TMR field:\n");
1018 print_APIC_bitfield(APIC_TMR);
1019 printk(KERN_DEBUG "... APIC IRR field:\n");
1020 print_APIC_bitfield(APIC_IRR);
1022 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1023 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1024 apic_write(APIC_ESR, 0);
1025 v = apic_read(APIC_ESR);
1026 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1029 v = apic_read(APIC_ICR);
1030 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1031 v = apic_read(APIC_ICR2);
1032 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1034 v = apic_read(APIC_LVTT);
1035 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1037 if (maxlvt > 3) { /* PC is LVT#4. */
1038 v = apic_read(APIC_LVTPC);
1039 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1041 v = apic_read(APIC_LVT0);
1042 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1043 v = apic_read(APIC_LVT1);
1044 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1046 if (maxlvt > 2) { /* ERR is LVT#3. */
1047 v = apic_read(APIC_LVTERR);
1048 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1051 v = apic_read(APIC_TMICT);
1052 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1053 v = apic_read(APIC_TMCCT);
1054 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1055 v = apic_read(APIC_TDCR);
1056 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1060 void print_all_local_APICs (void)
1062 on_each_cpu(print_local_APIC, NULL, 1, 1);
1065 void __apicdebuginit print_PIC(void)
1067 extern spinlock_t i8259A_lock;
1069 unsigned long flags;
1071 if (apic_verbosity == APIC_QUIET)
1074 printk(KERN_DEBUG "\nprinting PIC contents\n");
1076 spin_lock_irqsave(&i8259A_lock, flags);
1078 v = inb(0xa1) << 8 | inb(0x21);
1079 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1081 v = inb(0xa0) << 8 | inb(0x20);
1082 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1086 v = inb(0xa0) << 8 | inb(0x20);
1090 spin_unlock_irqrestore(&i8259A_lock, flags);
1092 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1094 v = inb(0x4d1) << 8 | inb(0x4d0);
1095 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1100 static void __init enable_IO_APIC(void)
1102 union IO_APIC_reg_01 reg_01;
1104 unsigned long flags;
1106 for (i = 0; i < PIN_MAP_SIZE; i++) {
1107 irq_2_pin[i].pin = -1;
1108 irq_2_pin[i].next = 0;
1111 for (i = 0; i < MAX_PIRQS; i++)
1112 pirq_entries[i] = -1;
1115 * The number of IO-APIC IRQ registers (== #pins):
1117 for (i = 0; i < nr_ioapics; i++) {
1118 spin_lock_irqsave(&ioapic_lock, flags);
1119 reg_01.raw = io_apic_read(i, 1);
1120 spin_unlock_irqrestore(&ioapic_lock, flags);
1121 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1125 * Do not trust the IO-APIC being empty at bootup
1131 * Not an __init, needed by the reboot code
1133 void disable_IO_APIC(void)
1136 * Clear the IO-APIC before rebooting:
1140 disconnect_bsp_APIC();
1144 * function to set the IO-APIC physical IDs based on the
1145 * values stored in the MPC table.
1147 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1150 static void __init setup_ioapic_ids_from_mpc (void)
1152 union IO_APIC_reg_00 reg_00;
1155 unsigned char old_id;
1156 unsigned long flags;
1159 * Set the IOAPIC ID to the value stored in the MPC table.
1161 for (apic = 0; apic < nr_ioapics; apic++) {
1163 /* Read the register 0 value */
1164 spin_lock_irqsave(&ioapic_lock, flags);
1165 reg_00.raw = io_apic_read(apic, 0);
1166 spin_unlock_irqrestore(&ioapic_lock, flags);
1168 old_id = mp_ioapics[apic].mpc_apicid;
1171 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1175 * We need to adjust the IRQ routing table
1176 * if the ID changed.
1178 if (old_id != mp_ioapics[apic].mpc_apicid)
1179 for (i = 0; i < mp_irq_entries; i++)
1180 if (mp_irqs[i].mpc_dstapic == old_id)
1181 mp_irqs[i].mpc_dstapic
1182 = mp_ioapics[apic].mpc_apicid;
1185 * Read the right value from the MPC table and
1186 * write it into the ID register.
1188 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1189 mp_ioapics[apic].mpc_apicid);
1191 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1192 spin_lock_irqsave(&ioapic_lock, flags);
1193 io_apic_write(apic, 0, reg_00.raw);
1194 spin_unlock_irqrestore(&ioapic_lock, flags);
1199 spin_lock_irqsave(&ioapic_lock, flags);
1200 reg_00.raw = io_apic_read(apic, 0);
1201 spin_unlock_irqrestore(&ioapic_lock, flags);
1202 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1203 printk("could not set ID!\n");
1205 apic_printk(APIC_VERBOSE," ok.\n");
1210 * There is a nasty bug in some older SMP boards, their mptable lies
1211 * about the timer IRQ. We do the following to work around the situation:
1213 * - timer IRQ defaults to IO-APIC IRQ
1214 * - if this function detects that timer IRQs are defunct, then we fall
1215 * back to ISA timer IRQs
1217 static int __init timer_irq_works(void)
1219 unsigned long t1 = jiffies;
1222 /* Let ten ticks pass... */
1223 mdelay((10 * 1000) / HZ);
1226 * Expect a few ticks at least, to be sure some possible
1227 * glue logic does not lock up after one or two first
1228 * ticks in a non-ExtINT mode. Also the local APIC
1229 * might have cached one ExtINT interrupt. Finally, at
1230 * least one tick may be lost due to delays.
1234 if (jiffies - t1 > 4)
1240 * In the SMP+IOAPIC case it might happen that there are an unspecified
1241 * number of pending IRQ events unhandled. These cases are very rare,
1242 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1243 * better to do it this way as thus we do not have to be aware of
1244 * 'pending' interrupts in the IRQ path, except at this point.
1247 * Edge triggered needs to resend any interrupt
1248 * that was delayed but this is now handled in the device
1253 * Starting up a edge-triggered IO-APIC interrupt is
1254 * nasty - we need to make sure that we get the edge.
1255 * If it is already asserted for some reason, we need
1256 * return 1 to indicate that is was pending.
1258 * This is not complete - we should be able to fake
1259 * an edge even if it isn't on the 8259A...
1262 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1264 int was_pending = 0;
1265 unsigned long flags;
1267 spin_lock_irqsave(&ioapic_lock, flags);
1269 disable_8259A_irq(irq);
1270 if (i8259A_irq_pending(irq))
1273 __unmask_IO_APIC_irq(irq);
1274 spin_unlock_irqrestore(&ioapic_lock, flags);
1280 * Once we have recorded IRQ_PENDING already, we can mask the
1281 * interrupt for real. This prevents IRQ storms from unhandled
1284 static void ack_edge_ioapic_irq(unsigned int irq)
1286 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1287 == (IRQ_PENDING | IRQ_DISABLED))
1288 mask_IO_APIC_irq(irq);
1293 * Level triggered interrupts can just be masked,
1294 * and shutting down and starting up the interrupt
1295 * is the same as enabling and disabling them -- except
1296 * with a startup need to return a "was pending" value.
1298 * Level triggered interrupts are special because we
1299 * do not touch any IO-APIC register while handling
1300 * them. We ack the APIC in the end-IRQ handler, not
1301 * in the start-IRQ-handler. Protection against reentrance
1302 * from the same interrupt is still provided, both by the
1303 * generic IRQ layer and by the fact that an unacked local
1304 * APIC does not accept IRQs.
1306 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1308 unmask_IO_APIC_irq(irq);
1310 return 0; /* don't check for pending */
1313 static void end_level_ioapic_irq (unsigned int irq)
1318 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
1320 unsigned long flags;
1323 dest = cpu_mask_to_apicid(mask);
1326 * Only the high 8 bits are valid.
1328 dest = SET_APIC_LOGICAL_ID(dest);
1330 spin_lock_irqsave(&ioapic_lock, flags);
1331 __DO_ACTION(1, = dest, )
1332 spin_unlock_irqrestore(&ioapic_lock, flags);
1335 #ifdef CONFIG_PCI_MSI
1336 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1338 int irq = vector_to_irq(vector);
1340 return startup_edge_ioapic_irq(irq);
1343 static void ack_edge_ioapic_vector(unsigned int vector)
1345 int irq = vector_to_irq(vector);
1347 ack_edge_ioapic_irq(irq);
1350 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1352 int irq = vector_to_irq(vector);
1354 return startup_level_ioapic_irq (irq);
1357 static void end_level_ioapic_vector (unsigned int vector)
1359 int irq = vector_to_irq(vector);
1361 end_level_ioapic_irq(irq);
1364 static void mask_IO_APIC_vector (unsigned int vector)
1366 int irq = vector_to_irq(vector);
1368 mask_IO_APIC_irq(irq);
1371 static void unmask_IO_APIC_vector (unsigned int vector)
1373 int irq = vector_to_irq(vector);
1375 unmask_IO_APIC_irq(irq);
1378 static void set_ioapic_affinity_vector (unsigned int vector,
1381 int irq = vector_to_irq(vector);
1383 set_ioapic_affinity_irq(irq, cpu_mask);
1388 * Level and edge triggered IO-APIC interrupts need different handling,
1389 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1390 * handled with the level-triggered descriptor, but that one has slightly
1391 * more overhead. Level-triggered interrupts cannot be handled with the
1392 * edge-triggered handler, without risking IRQ storms and other ugly
1396 static struct hw_interrupt_type ioapic_edge_type = {
1397 .typename = "IO-APIC-edge",
1398 .startup = startup_edge_ioapic,
1399 .shutdown = shutdown_edge_ioapic,
1400 .enable = enable_edge_ioapic,
1401 .disable = disable_edge_ioapic,
1402 .ack = ack_edge_ioapic,
1403 .end = end_edge_ioapic,
1404 .set_affinity = set_ioapic_affinity,
1407 static struct hw_interrupt_type ioapic_level_type = {
1408 .typename = "IO-APIC-level",
1409 .startup = startup_level_ioapic,
1410 .shutdown = shutdown_level_ioapic,
1411 .enable = enable_level_ioapic,
1412 .disable = disable_level_ioapic,
1413 .ack = mask_and_ack_level_ioapic,
1414 .end = end_level_ioapic,
1415 .set_affinity = set_ioapic_affinity,
1418 static inline void init_IO_APIC_traps(void)
1423 * NOTE! The local APIC isn't very good at handling
1424 * multiple interrupts at the same interrupt level.
1425 * As the interrupt level is determined by taking the
1426 * vector number and shifting that right by 4, we
1427 * want to spread these out a bit so that they don't
1428 * all fall in the same interrupt level.
1430 * Also, we've got to be careful not to trash gate
1431 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1433 for (irq = 0; irq < NR_IRQS ; irq++) {
1435 if (use_pci_vector()) {
1436 if (!platform_legacy_irq(tmp))
1437 if ((tmp = vector_to_irq(tmp)) == -1)
1440 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1442 * Hmm.. We don't have an entry for this,
1443 * so default to an old-fashioned 8259
1444 * interrupt if we can..
1447 make_8259A_irq(irq);
1449 /* Strange. Oh, well.. */
1450 irq_desc[irq].handler = &no_irq_type;
1455 static void enable_lapic_irq (unsigned int irq)
1459 v = apic_read(APIC_LVT0);
1460 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1463 static void disable_lapic_irq (unsigned int irq)
1467 v = apic_read(APIC_LVT0);
1468 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1471 static void ack_lapic_irq (unsigned int irq)
1476 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1478 static struct hw_interrupt_type lapic_irq_type = {
1479 .typename = "local-APIC-edge",
1480 .startup = NULL, /* startup_irq() not used for IRQ0 */
1481 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1482 .enable = enable_lapic_irq,
1483 .disable = disable_lapic_irq,
1484 .ack = ack_lapic_irq,
1485 .end = end_lapic_irq,
1488 static void setup_nmi (void)
1491 * Dirty trick to enable the NMI watchdog ...
1492 * We put the 8259A master into AEOI mode and
1493 * unmask on all local APICs LVT0 as NMI.
1495 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1496 * is from Maciej W. Rozycki - so we do not have to EOI from
1497 * the NMI handler or the timer interrupt.
1499 printk(KERN_INFO "activating NMI Watchdog ...");
1501 enable_NMI_through_LVT0(NULL);
1507 * This looks a bit hackish but it's about the only one way of sending
1508 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1509 * not support the ExtINT mode, unfortunately. We need to send these
1510 * cycles as some i82489DX-based boards have glue logic that keeps the
1511 * 8259A interrupt line asserted until INTA. --macro
1513 static inline void unlock_ExtINT_logic(void)
1516 struct IO_APIC_route_entry entry0, entry1;
1517 unsigned char save_control, save_freq_select;
1518 unsigned long flags;
1520 pin = find_isa_irq_pin(8, mp_INT);
1524 spin_lock_irqsave(&ioapic_lock, flags);
1525 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1526 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1527 spin_unlock_irqrestore(&ioapic_lock, flags);
1528 clear_IO_APIC_pin(0, pin);
1530 memset(&entry1, 0, sizeof(entry1));
1532 entry1.dest_mode = 0; /* physical delivery */
1533 entry1.mask = 0; /* unmask IRQ now */
1534 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1535 entry1.delivery_mode = dest_ExtINT;
1536 entry1.polarity = entry0.polarity;
1540 spin_lock_irqsave(&ioapic_lock, flags);
1541 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1542 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1543 spin_unlock_irqrestore(&ioapic_lock, flags);
1545 save_control = CMOS_READ(RTC_CONTROL);
1546 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1547 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1549 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1554 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1558 CMOS_WRITE(save_control, RTC_CONTROL);
1559 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1560 clear_IO_APIC_pin(0, pin);
1562 spin_lock_irqsave(&ioapic_lock, flags);
1563 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1564 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1565 spin_unlock_irqrestore(&ioapic_lock, flags);
1569 * This code may look a bit paranoid, but it's supposed to cooperate with
1570 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1571 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1572 * fanatically on his truly buggy board.
1574 static inline void check_timer(void)
1580 * get/set the timer IRQ vector:
1582 disable_8259A_irq(0);
1583 vector = assign_irq_vector(0);
1584 set_intr_gate(vector, interrupt[0]);
1587 * Subtle, code in do_timer_interrupt() expects an AEOI
1588 * mode for the 8259A whenever interrupts are routed
1589 * through I/O APICs. Also IRQ0 has to be enabled in
1590 * the 8259A which implies the virtual wire has to be
1591 * disabled in the local APIC.
1593 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1595 enable_8259A_irq(0);
1597 pin1 = find_isa_irq_pin(0, mp_INT);
1598 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1600 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1604 * Ok, does IRQ0 through the IOAPIC work?
1606 unmask_IO_APIC_irq(0);
1607 if (!no_timer_check && timer_irq_works()) {
1608 nmi_watchdog_default();
1609 if (nmi_watchdog == NMI_IO_APIC) {
1610 disable_8259A_irq(0);
1612 enable_8259A_irq(0);
1616 clear_IO_APIC_pin(0, pin1);
1617 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1620 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1622 apic_printk(APIC_VERBOSE,"\n..... (found pin %d) ...", pin2);
1624 * legacy devices should be connected to IO APIC #0
1626 setup_ExtINT_IRQ0_pin(pin2, vector);
1627 if (timer_irq_works()) {
1629 nmi_watchdog_default();
1630 if (nmi_watchdog == NMI_IO_APIC) {
1636 * Cleanup, just in case ...
1638 clear_IO_APIC_pin(0, pin2);
1640 printk(" failed.\n");
1643 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1647 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1649 disable_8259A_irq(0);
1650 irq_desc[0].handler = &lapic_irq_type;
1651 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1652 enable_8259A_irq(0);
1654 if (timer_irq_works()) {
1655 apic_printk(APIC_QUIET, " works.\n");
1658 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1659 apic_printk(APIC_VERBOSE," failed.\n");
1661 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1665 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1667 unlock_ExtINT_logic();
1669 if (timer_irq_works()) {
1670 apic_printk(APIC_VERBOSE," works.\n");
1673 apic_printk(APIC_VERBOSE," failed :(.\n");
1674 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1677 static int __init notimercheck(char *s)
1682 __setup("no_timer_check", notimercheck);
1686 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1687 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1688 * Linux doesn't really care, as it's not actually used
1689 * for any interrupt handling anyway.
1691 #define PIC_IRQS (1<<2)
1693 void __init setup_IO_APIC(void)
1698 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1700 io_apic_irqs = ~PIC_IRQS;
1702 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1705 * Set up the IO-APIC IRQ routing table.
1708 setup_ioapic_ids_from_mpc();
1710 setup_IO_APIC_irqs();
1711 init_IO_APIC_traps();
1717 struct sysfs_ioapic_data {
1718 struct sys_device dev;
1719 struct IO_APIC_route_entry entry[0];
1721 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1723 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1725 struct IO_APIC_route_entry *entry;
1726 struct sysfs_ioapic_data *data;
1727 unsigned long flags;
1730 data = container_of(dev, struct sysfs_ioapic_data, dev);
1731 entry = data->entry;
1732 spin_lock_irqsave(&ioapic_lock, flags);
1733 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1734 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1735 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1737 spin_unlock_irqrestore(&ioapic_lock, flags);
1742 static int ioapic_resume(struct sys_device *dev)
1744 struct IO_APIC_route_entry *entry;
1745 struct sysfs_ioapic_data *data;
1746 unsigned long flags;
1747 union IO_APIC_reg_00 reg_00;
1750 data = container_of(dev, struct sysfs_ioapic_data, dev);
1751 entry = data->entry;
1753 spin_lock_irqsave(&ioapic_lock, flags);
1754 reg_00.raw = io_apic_read(dev->id, 0);
1755 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1756 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1757 io_apic_write(dev->id, 0, reg_00.raw);
1759 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1760 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1761 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1763 spin_unlock_irqrestore(&ioapic_lock, flags);
1768 static struct sysdev_class ioapic_sysdev_class = {
1769 set_kset_name("ioapic"),
1770 .suspend = ioapic_suspend,
1771 .resume = ioapic_resume,
1774 static int __init ioapic_init_sysfs(void)
1776 struct sys_device * dev;
1777 int i, size, error = 0;
1779 error = sysdev_class_register(&ioapic_sysdev_class);
1783 for (i = 0; i < nr_ioapics; i++ ) {
1784 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1785 * sizeof(struct IO_APIC_route_entry);
1786 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1787 if (!mp_ioapic_data[i]) {
1788 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1791 memset(mp_ioapic_data[i], 0, size);
1792 dev = &mp_ioapic_data[i]->dev;
1794 dev->cls = &ioapic_sysdev_class;
1795 error = sysdev_register(dev);
1797 kfree(mp_ioapic_data[i]);
1798 mp_ioapic_data[i] = NULL;
1799 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1807 device_initcall(ioapic_init_sysfs);
1809 /* --------------------------------------------------------------------------
1810 ACPI-based IOAPIC Configuration
1811 -------------------------------------------------------------------------- */
1813 #ifdef CONFIG_ACPI_BOOT
1815 #define IO_APIC_MAX_ID 0xFE
1817 int __init io_apic_get_version (int ioapic)
1819 union IO_APIC_reg_01 reg_01;
1820 unsigned long flags;
1822 spin_lock_irqsave(&ioapic_lock, flags);
1823 reg_01.raw = io_apic_read(ioapic, 1);
1824 spin_unlock_irqrestore(&ioapic_lock, flags);
1826 return reg_01.bits.version;
1830 int __init io_apic_get_redir_entries (int ioapic)
1832 union IO_APIC_reg_01 reg_01;
1833 unsigned long flags;
1835 spin_lock_irqsave(&ioapic_lock, flags);
1836 reg_01.raw = io_apic_read(ioapic, 1);
1837 spin_unlock_irqrestore(&ioapic_lock, flags);
1839 return reg_01.bits.entries;
1843 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
1845 struct IO_APIC_route_entry entry;
1846 unsigned long flags;
1848 if (!IO_APIC_IRQ(irq)) {
1849 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1855 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1856 * Note that we mask (disable) IRQs now -- these get enabled when the
1857 * corresponding device driver registers for this IRQ.
1860 memset(&entry,0,sizeof(entry));
1862 entry.delivery_mode = INT_DELIVERY_MODE;
1863 entry.dest_mode = INT_DEST_MODE;
1864 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1865 entry.trigger = edge_level;
1866 entry.polarity = active_high_low;
1867 entry.mask = 1; /* Disabled (masked) */
1870 * IRQs < 16 are already in the irq_2_pin[] map
1873 add_pin_to_irq(irq, ioapic, pin);
1875 entry.vector = assign_irq_vector(irq);
1877 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1878 "IRQ %d Mode:%i Active:%i)\n", ioapic,
1879 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
1880 edge_level, active_high_low);
1882 ioapic_register_intr(irq, entry.vector, edge_level);
1884 if (!ioapic && (irq < 16))
1885 disable_8259A_irq(irq);
1887 spin_lock_irqsave(&ioapic_lock, flags);
1888 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
1889 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
1890 spin_unlock_irqrestore(&ioapic_lock, flags);
1895 #endif /*CONFIG_ACPI_BOOT*/
1899 * This function currently is only a helper for the i386 smp boot process where
1900 * we need to reprogram the ioredtbls to cater for the cpus which have come online
1901 * so mask in all cases should simply be TARGET_CPUS
1903 void __init setup_ioapic_dest(void)
1905 int pin, ioapic, irq, irq_entry;
1907 if (skip_ioapic_setup == 1)
1910 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
1911 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
1912 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
1913 if (irq_entry == -1)
1915 irq = pin_2_irq(irq_entry, ioapic, pin);
1916 set_ioapic_affinity_irq(irq, TARGET_CPUS);