2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/acpi.h>
31 #include <linux/sysdev.h>
33 #include <acpi/acpi_bus.h>
39 #include <asm/proto.h>
40 #include <asm/mach_apic.h>
45 #define __apicdebuginit __init
47 int sis_apic_bug; /* not actually supported, dummy for compile */
49 static int no_timer_check;
51 int disable_timer_pin_1 __initdata;
53 int timer_over_8254 __initdata = 0;
55 /* Where if anywhere is the i8259 connect in external int mode */
56 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
58 static DEFINE_SPINLOCK(ioapic_lock);
59 static DEFINE_SPINLOCK(vector_lock);
62 * # of IRQ routing registers
64 int nr_ioapic_registers[MAX_IO_APICS];
67 * Rough estimation of how many shared IRQs there are, can
70 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
71 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
74 * This is performance-critical, we want to do it O(1)
76 * the indexing order of this array favors 1:1 mappings
77 * between pins and IRQs.
80 static struct irq_pin_list {
81 short apic, pin, next;
82 } irq_2_pin[PIN_MAP_SIZE];
84 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
86 #define vector_to_irq(vector) \
87 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
89 #define vector_to_irq(vector) (vector)
92 #define __DO_ACTION(R, ACTION, FINAL) \
96 struct irq_pin_list *entry = irq_2_pin + irq; \
98 BUG_ON(irq >= NR_IRQS); \
104 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
106 io_apic_modify(entry->apic, reg); \
109 entry = irq_2_pin + entry->next; \
115 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
121 cpus_and(tmp, mask, cpu_online_map);
125 cpus_and(mask, tmp, CPU_MASK_ALL);
127 dest = cpu_mask_to_apicid(mask);
130 * Only the high 8 bits are valid.
132 dest = SET_APIC_LOGICAL_ID(dest);
134 spin_lock_irqsave(&ioapic_lock, flags);
135 __DO_ACTION(1, = dest, )
136 set_irq_info(irq, mask);
137 spin_unlock_irqrestore(&ioapic_lock, flags);
141 static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
144 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
145 * shared ISA-space IRQs, so we have to support them. We are super
146 * fast in the common case, and fast for shared ISA-space IRQs.
148 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
150 static int first_free_entry = NR_IRQS;
151 struct irq_pin_list *entry = irq_2_pin + irq;
153 BUG_ON(irq >= NR_IRQS);
155 entry = irq_2_pin + entry->next;
157 if (entry->pin != -1) {
158 entry->next = first_free_entry;
159 entry = irq_2_pin + entry->next;
160 if (++first_free_entry >= PIN_MAP_SIZE)
161 panic("io_apic.c: ran out of irq_2_pin entries!");
168 #define DO_ACTION(name,R,ACTION, FINAL) \
170 static void name##_IO_APIC_irq (unsigned int irq) \
171 __DO_ACTION(R, ACTION, FINAL)
173 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
175 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
178 static void mask_IO_APIC_irq (unsigned int irq)
182 spin_lock_irqsave(&ioapic_lock, flags);
183 __mask_IO_APIC_irq(irq);
184 spin_unlock_irqrestore(&ioapic_lock, flags);
187 static void unmask_IO_APIC_irq (unsigned int irq)
191 spin_lock_irqsave(&ioapic_lock, flags);
192 __unmask_IO_APIC_irq(irq);
193 spin_unlock_irqrestore(&ioapic_lock, flags);
196 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
198 struct IO_APIC_route_entry entry;
201 /* Check delivery_mode to be sure we're not clearing an SMI pin */
202 spin_lock_irqsave(&ioapic_lock, flags);
203 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
204 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
205 spin_unlock_irqrestore(&ioapic_lock, flags);
206 if (entry.delivery_mode == dest_SMI)
209 * Disable it in the IO-APIC irq-routing table:
211 memset(&entry, 0, sizeof(entry));
213 spin_lock_irqsave(&ioapic_lock, flags);
214 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
215 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
216 spin_unlock_irqrestore(&ioapic_lock, flags);
219 static void clear_IO_APIC (void)
223 for (apic = 0; apic < nr_ioapics; apic++)
224 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
225 clear_IO_APIC_pin(apic, pin);
228 int skip_ioapic_setup;
231 /* dummy parsing: see setup.c */
233 static int __init disable_ioapic_setup(char *str)
235 skip_ioapic_setup = 1;
239 static int __init enable_ioapic_setup(char *str)
242 skip_ioapic_setup = 0;
246 __setup("noapic", disable_ioapic_setup);
247 __setup("apic", enable_ioapic_setup);
249 static int __init setup_disable_8254_timer(char *s)
251 timer_over_8254 = -1;
254 static int __init setup_enable_8254_timer(char *s)
260 __setup("disable_8254_timer", setup_disable_8254_timer);
261 __setup("enable_8254_timer", setup_enable_8254_timer);
263 #include <asm/pci-direct.h>
264 #include <linux/pci_ids.h>
265 #include <linux/pci.h>
270 static int nvidia_hpet_detected __initdata;
272 static int __init nvidia_hpet_check(unsigned long phys, unsigned long size)
274 nvidia_hpet_detected = 1;
279 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
280 off. Check for an Nvidia or VIA PCI bridge and turn it off.
281 Use pci direct infrastructure because this runs before the PCI subsystem.
283 Can be overwritten with "apic"
285 And another hack to disable the IOMMU on VIA chipsets.
287 ... and others. Really should move this somewhere else.
290 void __init check_ioapic(void)
293 /* Poor man's PCI discovery */
294 for (num = 0; num < 32; num++) {
295 for (slot = 0; slot < 32; slot++) {
296 for (func = 0; func < 8; func++) {
300 class = read_pci_config(num,slot,func,
302 if (class == 0xffffffff)
305 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
308 vendor = read_pci_config(num, slot, func,
312 case PCI_VENDOR_ID_VIA:
314 if ((end_pfn > MAX_DMA32_PFN ||
316 !iommu_aperture_allowed) {
318 "Looks like a VIA chipset. Disabling IOMMU. Override with \"iommu=allowed\"\n");
319 iommu_aperture_disabled = 1;
323 case PCI_VENDOR_ID_NVIDIA:
326 * All timer overrides on Nvidia are
327 * wrong unless HPET is enabled.
329 nvidia_hpet_detected = 0;
330 acpi_table_parse(ACPI_HPET,
332 if (nvidia_hpet_detected == 0) {
333 acpi_skip_timer_override = 1;
334 printk(KERN_INFO "Nvidia board "
335 "detected. Ignoring ACPI "
336 "timer override.\n");
339 /* RED-PEN skip them on mptables too? */
342 /* This should be actually default, but
343 for 2.6.16 let's do it for ATI only where
344 it's really needed. */
345 case PCI_VENDOR_ID_ATI:
346 if (timer_over_8254 == 1) {
349 "ATI board detected. Disabling timer routing over 8254.\n");
355 /* No multi-function device? */
356 type = read_pci_config_byte(num,slot,func,
366 * Find the IRQ entry number of a certain pin.
368 static int find_irq_entry(int apic, int pin, int type)
372 for (i = 0; i < mp_irq_entries; i++)
373 if (mp_irqs[i].mpc_irqtype == type &&
374 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
375 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
376 mp_irqs[i].mpc_dstirq == pin)
383 * Find the pin to which IRQ[irq] (ISA) is connected
385 static int __init find_isa_irq_pin(int irq, int type)
389 for (i = 0; i < mp_irq_entries; i++) {
390 int lbus = mp_irqs[i].mpc_srcbus;
392 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
393 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
394 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
395 (mp_irqs[i].mpc_irqtype == type) &&
396 (mp_irqs[i].mpc_srcbusirq == irq))
398 return mp_irqs[i].mpc_dstirq;
403 static int __init find_isa_irq_apic(int irq, int type)
407 for (i = 0; i < mp_irq_entries; i++) {
408 int lbus = mp_irqs[i].mpc_srcbus;
410 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
411 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
412 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
413 (mp_irqs[i].mpc_irqtype == type) &&
414 (mp_irqs[i].mpc_srcbusirq == irq))
417 if (i < mp_irq_entries) {
419 for(apic = 0; apic < nr_ioapics; apic++) {
420 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
429 * Find a specific PCI IRQ entry.
430 * Not an __init, possibly needed by modules
432 static int pin_2_irq(int idx, int apic, int pin);
434 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
436 int apic, i, best_guess = -1;
438 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
440 if (mp_bus_id_to_pci_bus[bus] == -1) {
441 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
444 for (i = 0; i < mp_irq_entries; i++) {
445 int lbus = mp_irqs[i].mpc_srcbus;
447 for (apic = 0; apic < nr_ioapics; apic++)
448 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
449 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
452 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
453 !mp_irqs[i].mpc_irqtype &&
455 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
456 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
458 if (!(apic || IO_APIC_IRQ(irq)))
461 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
464 * Use the first all-but-pin matching entry as a
465 * best-guess fuzzy result for broken mptables.
471 BUG_ON(best_guess >= NR_IRQS);
476 * EISA Edge/Level control register, ELCR
478 static int EISA_ELCR(unsigned int irq)
481 unsigned int port = 0x4d0 + (irq >> 3);
482 return (inb(port) >> (irq & 7)) & 1;
484 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
488 /* EISA interrupts are always polarity zero and can be edge or level
489 * trigger depending on the ELCR value. If an interrupt is listed as
490 * EISA conforming in the MP table, that means its trigger type must
491 * be read in from the ELCR */
493 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
494 #define default_EISA_polarity(idx) (0)
496 /* ISA interrupts are always polarity zero edge triggered,
497 * when listed as conforming in the MP table. */
499 #define default_ISA_trigger(idx) (0)
500 #define default_ISA_polarity(idx) (0)
502 /* PCI interrupts are always polarity one level triggered,
503 * when listed as conforming in the MP table. */
505 #define default_PCI_trigger(idx) (1)
506 #define default_PCI_polarity(idx) (1)
508 /* MCA interrupts are always polarity zero level triggered,
509 * when listed as conforming in the MP table. */
511 #define default_MCA_trigger(idx) (1)
512 #define default_MCA_polarity(idx) (0)
514 static int __init MPBIOS_polarity(int idx)
516 int bus = mp_irqs[idx].mpc_srcbus;
520 * Determine IRQ line polarity (high active or low active):
522 switch (mp_irqs[idx].mpc_irqflag & 3)
524 case 0: /* conforms, ie. bus-type dependent polarity */
526 switch (mp_bus_id_to_type[bus])
528 case MP_BUS_ISA: /* ISA pin */
530 polarity = default_ISA_polarity(idx);
533 case MP_BUS_EISA: /* EISA pin */
535 polarity = default_EISA_polarity(idx);
538 case MP_BUS_PCI: /* PCI pin */
540 polarity = default_PCI_polarity(idx);
543 case MP_BUS_MCA: /* MCA pin */
545 polarity = default_MCA_polarity(idx);
550 printk(KERN_WARNING "broken BIOS!!\n");
557 case 1: /* high active */
562 case 2: /* reserved */
564 printk(KERN_WARNING "broken BIOS!!\n");
568 case 3: /* low active */
573 default: /* invalid */
575 printk(KERN_WARNING "broken BIOS!!\n");
583 static int MPBIOS_trigger(int idx)
585 int bus = mp_irqs[idx].mpc_srcbus;
589 * Determine IRQ trigger mode (edge or level sensitive):
591 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
593 case 0: /* conforms, ie. bus-type dependent */
595 switch (mp_bus_id_to_type[bus])
597 case MP_BUS_ISA: /* ISA pin */
599 trigger = default_ISA_trigger(idx);
602 case MP_BUS_EISA: /* EISA pin */
604 trigger = default_EISA_trigger(idx);
607 case MP_BUS_PCI: /* PCI pin */
609 trigger = default_PCI_trigger(idx);
612 case MP_BUS_MCA: /* MCA pin */
614 trigger = default_MCA_trigger(idx);
619 printk(KERN_WARNING "broken BIOS!!\n");
631 case 2: /* reserved */
633 printk(KERN_WARNING "broken BIOS!!\n");
642 default: /* invalid */
644 printk(KERN_WARNING "broken BIOS!!\n");
652 static inline int irq_polarity(int idx)
654 return MPBIOS_polarity(idx);
657 static inline int irq_trigger(int idx)
659 return MPBIOS_trigger(idx);
662 static int next_irq = 16;
665 * gsi_irq_sharing -- Name overload! "irq" can be either a legacy IRQ
666 * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
667 * from ACPI, which can reach 800 in large boxen.
669 * Compact the sparse GSI space into a sequential IRQ series and reuse
670 * vectors if possible.
672 int gsi_irq_sharing(int gsi)
674 int i, tries, vector;
676 BUG_ON(gsi >= NR_IRQ_VECTORS);
678 if (platform_legacy_irq(gsi))
681 if (gsi_2_irq[gsi] != 0xFF)
682 return (int)gsi_2_irq[gsi];
686 vector = assign_irq_vector(gsi);
689 * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
690 * use of vector and if found, return that IRQ. However, we never want
691 * to share legacy IRQs, which usually have a different trigger mode
694 for (i = 0; i < NR_IRQS; i++)
695 if (IO_APIC_VECTOR(i) == vector)
697 if (platform_legacy_irq(i)) {
699 IO_APIC_VECTOR(i) = 0;
702 panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
706 printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
712 BUG_ON(i >= NR_IRQS);
714 IO_APIC_VECTOR(i) = vector;
715 printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
720 static int pin_2_irq(int idx, int apic, int pin)
723 int bus = mp_irqs[idx].mpc_srcbus;
726 * Debugging check, we are in big trouble if this message pops up!
728 if (mp_irqs[idx].mpc_dstirq != pin)
729 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
731 switch (mp_bus_id_to_type[bus])
733 case MP_BUS_ISA: /* ISA pin */
737 irq = mp_irqs[idx].mpc_srcbusirq;
740 case MP_BUS_PCI: /* PCI pin */
743 * PCI IRQs are mapped in order
747 irq += nr_ioapic_registers[i++];
749 irq = gsi_irq_sharing(irq);
754 printk(KERN_ERR "unknown bus type %d.\n",bus);
759 BUG_ON(irq >= NR_IRQS);
763 static inline int IO_APIC_irq_trigger(int irq)
767 for (apic = 0; apic < nr_ioapics; apic++) {
768 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
769 idx = find_irq_entry(apic,pin,mp_INT);
770 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
771 return irq_trigger(idx);
775 * nonexistent IRQs are edge default
780 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
781 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
783 int assign_irq_vector(int irq)
785 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
789 BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
791 spin_lock_irqsave(&vector_lock, flags);
793 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0) {
794 spin_unlock_irqrestore(&vector_lock, flags);
795 return IO_APIC_VECTOR(irq);
799 if (current_vector == IA32_SYSCALL_VECTOR)
802 if (current_vector >= FIRST_SYSTEM_VECTOR) {
803 /* If we run out of vectors on large boxen, must share them. */
804 offset = (offset + 1) % 8;
805 current_vector = FIRST_DEVICE_VECTOR + offset;
808 vector = current_vector;
809 vector_irq[vector] = irq;
810 if (irq != AUTO_ASSIGN)
811 IO_APIC_VECTOR(irq) = vector;
813 spin_unlock_irqrestore(&vector_lock, flags);
818 extern void (*interrupt[NR_IRQS])(void);
819 static struct hw_interrupt_type ioapic_level_type;
820 static struct hw_interrupt_type ioapic_edge_type;
822 #define IOAPIC_AUTO -1
823 #define IOAPIC_EDGE 0
824 #define IOAPIC_LEVEL 1
826 static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
830 idx = use_pci_vector() && !platform_legacy_irq(irq) ? vector : irq;
832 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
833 trigger == IOAPIC_LEVEL)
834 irq_desc[idx].chip = &ioapic_level_type;
836 irq_desc[idx].chip = &ioapic_edge_type;
837 set_intr_gate(vector, interrupt[idx]);
840 static void __init setup_IO_APIC_irqs(void)
842 struct IO_APIC_route_entry entry;
843 int apic, pin, idx, irq, first_notcon = 1, vector;
846 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
848 for (apic = 0; apic < nr_ioapics; apic++) {
849 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
852 * add it to the IO-APIC irq-routing table:
854 memset(&entry,0,sizeof(entry));
856 entry.delivery_mode = INT_DELIVERY_MODE;
857 entry.dest_mode = INT_DEST_MODE;
858 entry.mask = 0; /* enable IRQ */
859 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
861 idx = find_irq_entry(apic,pin,mp_INT);
864 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
867 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
871 entry.trigger = irq_trigger(idx);
872 entry.polarity = irq_polarity(idx);
874 if (irq_trigger(idx)) {
877 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
880 irq = pin_2_irq(idx, apic, pin);
881 add_pin_to_irq(irq, apic, pin);
883 if (!apic && !IO_APIC_IRQ(irq))
886 if (IO_APIC_IRQ(irq)) {
887 vector = assign_irq_vector(irq);
888 entry.vector = vector;
890 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
891 if (!apic && (irq < 16))
892 disable_8259A_irq(irq);
894 spin_lock_irqsave(&ioapic_lock, flags);
895 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
896 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
897 set_native_irq_info(irq, TARGET_CPUS);
898 spin_unlock_irqrestore(&ioapic_lock, flags);
903 apic_printk(APIC_VERBOSE," not connected.\n");
907 * Set up the 8259A-master output pin as broadcast to all
910 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
912 struct IO_APIC_route_entry entry;
915 memset(&entry,0,sizeof(entry));
917 disable_8259A_irq(0);
920 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
923 * We use logical delivery to get the timer IRQ
926 entry.dest_mode = INT_DEST_MODE;
927 entry.mask = 0; /* unmask IRQ now */
928 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
929 entry.delivery_mode = INT_DELIVERY_MODE;
932 entry.vector = vector;
935 * The timer IRQ doesn't have to know that behind the
936 * scene we have a 8259A-master in AEOI mode ...
938 irq_desc[0].chip = &ioapic_edge_type;
941 * Add it to the IO-APIC irq-routing table:
943 spin_lock_irqsave(&ioapic_lock, flags);
944 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
945 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
946 spin_unlock_irqrestore(&ioapic_lock, flags);
951 void __init UNEXPECTED_IO_APIC(void)
955 void __apicdebuginit print_IO_APIC(void)
958 union IO_APIC_reg_00 reg_00;
959 union IO_APIC_reg_01 reg_01;
960 union IO_APIC_reg_02 reg_02;
963 if (apic_verbosity == APIC_QUIET)
966 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
967 for (i = 0; i < nr_ioapics; i++)
968 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
969 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
972 * We are a bit conservative about what we expect. We have to
973 * know about every hardware change ASAP.
975 printk(KERN_INFO "testing the IO APIC.......................\n");
977 for (apic = 0; apic < nr_ioapics; apic++) {
979 spin_lock_irqsave(&ioapic_lock, flags);
980 reg_00.raw = io_apic_read(apic, 0);
981 reg_01.raw = io_apic_read(apic, 1);
982 if (reg_01.bits.version >= 0x10)
983 reg_02.raw = io_apic_read(apic, 2);
984 spin_unlock_irqrestore(&ioapic_lock, flags);
987 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
988 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
989 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
990 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
991 UNEXPECTED_IO_APIC();
993 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
994 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
995 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
996 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
997 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
998 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
999 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1000 (reg_01.bits.entries != 0x2E) &&
1001 (reg_01.bits.entries != 0x3F) &&
1002 (reg_01.bits.entries != 0x03)
1004 UNEXPECTED_IO_APIC();
1006 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1007 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1008 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1009 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
1010 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1011 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1012 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1013 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1015 UNEXPECTED_IO_APIC();
1016 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1017 UNEXPECTED_IO_APIC();
1019 if (reg_01.bits.version >= 0x10) {
1020 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1021 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1022 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1023 UNEXPECTED_IO_APIC();
1026 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1028 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1029 " Stat Dest Deli Vect: \n");
1031 for (i = 0; i <= reg_01.bits.entries; i++) {
1032 struct IO_APIC_route_entry entry;
1034 spin_lock_irqsave(&ioapic_lock, flags);
1035 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
1036 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
1037 spin_unlock_irqrestore(&ioapic_lock, flags);
1039 printk(KERN_DEBUG " %02x %03X %02X ",
1041 entry.dest.logical.logical_dest,
1042 entry.dest.physical.physical_dest
1045 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1050 entry.delivery_status,
1052 entry.delivery_mode,
1057 if (use_pci_vector())
1058 printk(KERN_INFO "Using vector-based indexing\n");
1059 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1060 for (i = 0; i < NR_IRQS; i++) {
1061 struct irq_pin_list *entry = irq_2_pin + i;
1064 if (use_pci_vector() && !platform_legacy_irq(i))
1065 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1067 printk(KERN_DEBUG "IRQ%d ", i);
1069 printk("-> %d:%d", entry->apic, entry->pin);
1072 entry = irq_2_pin + entry->next;
1077 printk(KERN_INFO ".................................... done.\n");
1084 static __apicdebuginit void print_APIC_bitfield (int base)
1089 if (apic_verbosity == APIC_QUIET)
1092 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1093 for (i = 0; i < 8; i++) {
1094 v = apic_read(base + i*0x10);
1095 for (j = 0; j < 32; j++) {
1105 void __apicdebuginit print_local_APIC(void * dummy)
1107 unsigned int v, ver, maxlvt;
1109 if (apic_verbosity == APIC_QUIET)
1112 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1113 smp_processor_id(), hard_smp_processor_id());
1114 v = apic_read(APIC_ID);
1115 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1116 v = apic_read(APIC_LVR);
1117 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1118 ver = GET_APIC_VERSION(v);
1119 maxlvt = get_maxlvt();
1121 v = apic_read(APIC_TASKPRI);
1122 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1124 v = apic_read(APIC_ARBPRI);
1125 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1126 v & APIC_ARBPRI_MASK);
1127 v = apic_read(APIC_PROCPRI);
1128 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1130 v = apic_read(APIC_EOI);
1131 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1132 v = apic_read(APIC_RRR);
1133 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1134 v = apic_read(APIC_LDR);
1135 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1136 v = apic_read(APIC_DFR);
1137 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1138 v = apic_read(APIC_SPIV);
1139 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1141 printk(KERN_DEBUG "... APIC ISR field:\n");
1142 print_APIC_bitfield(APIC_ISR);
1143 printk(KERN_DEBUG "... APIC TMR field:\n");
1144 print_APIC_bitfield(APIC_TMR);
1145 printk(KERN_DEBUG "... APIC IRR field:\n");
1146 print_APIC_bitfield(APIC_IRR);
1148 v = apic_read(APIC_ESR);
1149 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1151 v = apic_read(APIC_ICR);
1152 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1153 v = apic_read(APIC_ICR2);
1154 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1156 v = apic_read(APIC_LVTT);
1157 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1159 if (maxlvt > 3) { /* PC is LVT#4. */
1160 v = apic_read(APIC_LVTPC);
1161 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1163 v = apic_read(APIC_LVT0);
1164 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1165 v = apic_read(APIC_LVT1);
1166 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1168 if (maxlvt > 2) { /* ERR is LVT#3. */
1169 v = apic_read(APIC_LVTERR);
1170 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1173 v = apic_read(APIC_TMICT);
1174 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1175 v = apic_read(APIC_TMCCT);
1176 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1177 v = apic_read(APIC_TDCR);
1178 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1182 void print_all_local_APICs (void)
1184 on_each_cpu(print_local_APIC, NULL, 1, 1);
1187 void __apicdebuginit print_PIC(void)
1190 unsigned long flags;
1192 if (apic_verbosity == APIC_QUIET)
1195 printk(KERN_DEBUG "\nprinting PIC contents\n");
1197 spin_lock_irqsave(&i8259A_lock, flags);
1199 v = inb(0xa1) << 8 | inb(0x21);
1200 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1202 v = inb(0xa0) << 8 | inb(0x20);
1203 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1207 v = inb(0xa0) << 8 | inb(0x20);
1211 spin_unlock_irqrestore(&i8259A_lock, flags);
1213 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1215 v = inb(0x4d1) << 8 | inb(0x4d0);
1216 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1221 static void __init enable_IO_APIC(void)
1223 union IO_APIC_reg_01 reg_01;
1224 int i8259_apic, i8259_pin;
1226 unsigned long flags;
1228 for (i = 0; i < PIN_MAP_SIZE; i++) {
1229 irq_2_pin[i].pin = -1;
1230 irq_2_pin[i].next = 0;
1234 * The number of IO-APIC IRQ registers (== #pins):
1236 for (apic = 0; apic < nr_ioapics; apic++) {
1237 spin_lock_irqsave(&ioapic_lock, flags);
1238 reg_01.raw = io_apic_read(apic, 1);
1239 spin_unlock_irqrestore(&ioapic_lock, flags);
1240 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1242 for(apic = 0; apic < nr_ioapics; apic++) {
1244 /* See if any of the pins is in ExtINT mode */
1245 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1246 struct IO_APIC_route_entry entry;
1247 spin_lock_irqsave(&ioapic_lock, flags);
1248 *(((int *)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1249 *(((int *)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1250 spin_unlock_irqrestore(&ioapic_lock, flags);
1253 /* If the interrupt line is enabled and in ExtInt mode
1254 * I have found the pin where the i8259 is connected.
1256 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1257 ioapic_i8259.apic = apic;
1258 ioapic_i8259.pin = pin;
1264 /* Look to see what if the MP table has reported the ExtINT */
1265 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1266 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1267 /* Trust the MP table if nothing is setup in the hardware */
1268 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1269 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1270 ioapic_i8259.pin = i8259_pin;
1271 ioapic_i8259.apic = i8259_apic;
1273 /* Complain if the MP table and the hardware disagree */
1274 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1275 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1277 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1281 * Do not trust the IO-APIC being empty at bootup
1287 * Not an __init, needed by the reboot code
1289 void disable_IO_APIC(void)
1292 * Clear the IO-APIC before rebooting:
1297 * If the i8259 is routed through an IOAPIC
1298 * Put that IOAPIC in virtual wire mode
1299 * so legacy interrupts can be delivered.
1301 if (ioapic_i8259.pin != -1) {
1302 struct IO_APIC_route_entry entry;
1303 unsigned long flags;
1305 memset(&entry, 0, sizeof(entry));
1306 entry.mask = 0; /* Enabled */
1307 entry.trigger = 0; /* Edge */
1309 entry.polarity = 0; /* High */
1310 entry.delivery_status = 0;
1311 entry.dest_mode = 0; /* Physical */
1312 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1314 entry.dest.physical.physical_dest =
1315 GET_APIC_ID(apic_read(APIC_ID));
1318 * Add it to the IO-APIC irq-routing table:
1320 spin_lock_irqsave(&ioapic_lock, flags);
1321 io_apic_write(ioapic_i8259.apic, 0x11+2*ioapic_i8259.pin,
1322 *(((int *)&entry)+1));
1323 io_apic_write(ioapic_i8259.apic, 0x10+2*ioapic_i8259.pin,
1324 *(((int *)&entry)+0));
1325 spin_unlock_irqrestore(&ioapic_lock, flags);
1328 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1332 * function to set the IO-APIC physical IDs based on the
1333 * values stored in the MPC table.
1335 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1338 static void __init setup_ioapic_ids_from_mpc (void)
1340 union IO_APIC_reg_00 reg_00;
1343 unsigned char old_id;
1344 unsigned long flags;
1347 * Set the IOAPIC ID to the value stored in the MPC table.
1349 for (apic = 0; apic < nr_ioapics; apic++) {
1351 /* Read the register 0 value */
1352 spin_lock_irqsave(&ioapic_lock, flags);
1353 reg_00.raw = io_apic_read(apic, 0);
1354 spin_unlock_irqrestore(&ioapic_lock, flags);
1356 old_id = mp_ioapics[apic].mpc_apicid;
1359 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1363 * We need to adjust the IRQ routing table
1364 * if the ID changed.
1366 if (old_id != mp_ioapics[apic].mpc_apicid)
1367 for (i = 0; i < mp_irq_entries; i++)
1368 if (mp_irqs[i].mpc_dstapic == old_id)
1369 mp_irqs[i].mpc_dstapic
1370 = mp_ioapics[apic].mpc_apicid;
1373 * Read the right value from the MPC table and
1374 * write it into the ID register.
1376 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1377 mp_ioapics[apic].mpc_apicid);
1379 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1380 spin_lock_irqsave(&ioapic_lock, flags);
1381 io_apic_write(apic, 0, reg_00.raw);
1382 spin_unlock_irqrestore(&ioapic_lock, flags);
1387 spin_lock_irqsave(&ioapic_lock, flags);
1388 reg_00.raw = io_apic_read(apic, 0);
1389 spin_unlock_irqrestore(&ioapic_lock, flags);
1390 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1391 printk("could not set ID!\n");
1393 apic_printk(APIC_VERBOSE," ok.\n");
1398 * There is a nasty bug in some older SMP boards, their mptable lies
1399 * about the timer IRQ. We do the following to work around the situation:
1401 * - timer IRQ defaults to IO-APIC IRQ
1402 * - if this function detects that timer IRQs are defunct, then we fall
1403 * back to ISA timer IRQs
1405 static int __init timer_irq_works(void)
1407 unsigned long t1 = jiffies;
1410 /* Let ten ticks pass... */
1411 mdelay((10 * 1000) / HZ);
1414 * Expect a few ticks at least, to be sure some possible
1415 * glue logic does not lock up after one or two first
1416 * ticks in a non-ExtINT mode. Also the local APIC
1417 * might have cached one ExtINT interrupt. Finally, at
1418 * least one tick may be lost due to delays.
1422 if (jiffies - t1 > 4)
1428 * In the SMP+IOAPIC case it might happen that there are an unspecified
1429 * number of pending IRQ events unhandled. These cases are very rare,
1430 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1431 * better to do it this way as thus we do not have to be aware of
1432 * 'pending' interrupts in the IRQ path, except at this point.
1435 * Edge triggered needs to resend any interrupt
1436 * that was delayed but this is now handled in the device
1441 * Starting up a edge-triggered IO-APIC interrupt is
1442 * nasty - we need to make sure that we get the edge.
1443 * If it is already asserted for some reason, we need
1444 * return 1 to indicate that is was pending.
1446 * This is not complete - we should be able to fake
1447 * an edge even if it isn't on the 8259A...
1450 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1452 int was_pending = 0;
1453 unsigned long flags;
1455 spin_lock_irqsave(&ioapic_lock, flags);
1457 disable_8259A_irq(irq);
1458 if (i8259A_irq_pending(irq))
1461 __unmask_IO_APIC_irq(irq);
1462 spin_unlock_irqrestore(&ioapic_lock, flags);
1468 * Once we have recorded IRQ_PENDING already, we can mask the
1469 * interrupt for real. This prevents IRQ storms from unhandled
1472 static void ack_edge_ioapic_irq(unsigned int irq)
1475 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1476 == (IRQ_PENDING | IRQ_DISABLED))
1477 mask_IO_APIC_irq(irq);
1482 * Level triggered interrupts can just be masked,
1483 * and shutting down and starting up the interrupt
1484 * is the same as enabling and disabling them -- except
1485 * with a startup need to return a "was pending" value.
1487 * Level triggered interrupts are special because we
1488 * do not touch any IO-APIC register while handling
1489 * them. We ack the APIC in the end-IRQ handler, not
1490 * in the start-IRQ-handler. Protection against reentrance
1491 * from the same interrupt is still provided, both by the
1492 * generic IRQ layer and by the fact that an unacked local
1493 * APIC does not accept IRQs.
1495 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1497 unmask_IO_APIC_irq(irq);
1499 return 0; /* don't check for pending */
1502 static void end_level_ioapic_irq (unsigned int irq)
1508 #ifdef CONFIG_PCI_MSI
1509 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1511 int irq = vector_to_irq(vector);
1513 return startup_edge_ioapic_irq(irq);
1516 static void ack_edge_ioapic_vector(unsigned int vector)
1518 int irq = vector_to_irq(vector);
1520 move_native_irq(vector);
1521 ack_edge_ioapic_irq(irq);
1524 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1526 int irq = vector_to_irq(vector);
1528 return startup_level_ioapic_irq (irq);
1531 static void end_level_ioapic_vector (unsigned int vector)
1533 int irq = vector_to_irq(vector);
1535 move_native_irq(vector);
1536 end_level_ioapic_irq(irq);
1539 static void mask_IO_APIC_vector (unsigned int vector)
1541 int irq = vector_to_irq(vector);
1543 mask_IO_APIC_irq(irq);
1546 static void unmask_IO_APIC_vector (unsigned int vector)
1548 int irq = vector_to_irq(vector);
1550 unmask_IO_APIC_irq(irq);
1554 static void set_ioapic_affinity_vector (unsigned int vector,
1557 int irq = vector_to_irq(vector);
1559 set_native_irq_info(vector, cpu_mask);
1560 set_ioapic_affinity_irq(irq, cpu_mask);
1562 #endif // CONFIG_SMP
1563 #endif // CONFIG_PCI_MSI
1565 static int ioapic_retrigger(unsigned int irq)
1567 send_IPI_self(IO_APIC_VECTOR(irq));
1573 * Level and edge triggered IO-APIC interrupts need different handling,
1574 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1575 * handled with the level-triggered descriptor, but that one has slightly
1576 * more overhead. Level-triggered interrupts cannot be handled with the
1577 * edge-triggered handler, without risking IRQ storms and other ugly
1581 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1582 .typename = "IO-APIC-edge",
1583 .startup = startup_edge_ioapic,
1584 .shutdown = shutdown_edge_ioapic,
1585 .enable = enable_edge_ioapic,
1586 .disable = disable_edge_ioapic,
1587 .ack = ack_edge_ioapic,
1588 .end = end_edge_ioapic,
1590 .set_affinity = set_ioapic_affinity,
1592 .retrigger = ioapic_retrigger,
1595 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1596 .typename = "IO-APIC-level",
1597 .startup = startup_level_ioapic,
1598 .shutdown = shutdown_level_ioapic,
1599 .enable = enable_level_ioapic,
1600 .disable = disable_level_ioapic,
1601 .ack = mask_and_ack_level_ioapic,
1602 .end = end_level_ioapic,
1604 .set_affinity = set_ioapic_affinity,
1606 .retrigger = ioapic_retrigger,
1609 static inline void init_IO_APIC_traps(void)
1614 * NOTE! The local APIC isn't very good at handling
1615 * multiple interrupts at the same interrupt level.
1616 * As the interrupt level is determined by taking the
1617 * vector number and shifting that right by 4, we
1618 * want to spread these out a bit so that they don't
1619 * all fall in the same interrupt level.
1621 * Also, we've got to be careful not to trash gate
1622 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1624 for (irq = 0; irq < NR_IRQS ; irq++) {
1626 if (use_pci_vector()) {
1627 if (!platform_legacy_irq(tmp))
1628 if ((tmp = vector_to_irq(tmp)) == -1)
1631 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1633 * Hmm.. We don't have an entry for this,
1634 * so default to an old-fashioned 8259
1635 * interrupt if we can..
1638 make_8259A_irq(irq);
1640 /* Strange. Oh, well.. */
1641 irq_desc[irq].chip = &no_irq_type;
1646 static void enable_lapic_irq (unsigned int irq)
1650 v = apic_read(APIC_LVT0);
1651 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1654 static void disable_lapic_irq (unsigned int irq)
1658 v = apic_read(APIC_LVT0);
1659 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1662 static void ack_lapic_irq (unsigned int irq)
1667 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1669 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1670 .typename = "local-APIC-edge",
1671 .startup = NULL, /* startup_irq() not used for IRQ0 */
1672 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1673 .enable = enable_lapic_irq,
1674 .disable = disable_lapic_irq,
1675 .ack = ack_lapic_irq,
1676 .end = end_lapic_irq,
1679 static void setup_nmi (void)
1682 * Dirty trick to enable the NMI watchdog ...
1683 * We put the 8259A master into AEOI mode and
1684 * unmask on all local APICs LVT0 as NMI.
1686 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1687 * is from Maciej W. Rozycki - so we do not have to EOI from
1688 * the NMI handler or the timer interrupt.
1690 printk(KERN_INFO "activating NMI Watchdog ...");
1692 enable_NMI_through_LVT0(NULL);
1698 * This looks a bit hackish but it's about the only one way of sending
1699 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1700 * not support the ExtINT mode, unfortunately. We need to send these
1701 * cycles as some i82489DX-based boards have glue logic that keeps the
1702 * 8259A interrupt line asserted until INTA. --macro
1704 static inline void unlock_ExtINT_logic(void)
1707 struct IO_APIC_route_entry entry0, entry1;
1708 unsigned char save_control, save_freq_select;
1709 unsigned long flags;
1711 pin = find_isa_irq_pin(8, mp_INT);
1712 apic = find_isa_irq_apic(8, mp_INT);
1716 spin_lock_irqsave(&ioapic_lock, flags);
1717 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1718 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1719 spin_unlock_irqrestore(&ioapic_lock, flags);
1720 clear_IO_APIC_pin(apic, pin);
1722 memset(&entry1, 0, sizeof(entry1));
1724 entry1.dest_mode = 0; /* physical delivery */
1725 entry1.mask = 0; /* unmask IRQ now */
1726 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1727 entry1.delivery_mode = dest_ExtINT;
1728 entry1.polarity = entry0.polarity;
1732 spin_lock_irqsave(&ioapic_lock, flags);
1733 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1734 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1735 spin_unlock_irqrestore(&ioapic_lock, flags);
1737 save_control = CMOS_READ(RTC_CONTROL);
1738 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1739 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1741 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1746 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1750 CMOS_WRITE(save_control, RTC_CONTROL);
1751 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1752 clear_IO_APIC_pin(apic, pin);
1754 spin_lock_irqsave(&ioapic_lock, flags);
1755 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1756 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1757 spin_unlock_irqrestore(&ioapic_lock, flags);
1760 int timer_uses_ioapic_pin_0;
1763 * This code may look a bit paranoid, but it's supposed to cooperate with
1764 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1765 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1766 * fanatically on his truly buggy board.
1768 * FIXME: really need to revamp this for modern platforms only.
1770 static inline void check_timer(void)
1772 int apic1, pin1, apic2, pin2;
1776 * get/set the timer IRQ vector:
1778 disable_8259A_irq(0);
1779 vector = assign_irq_vector(0);
1780 set_intr_gate(vector, interrupt[0]);
1783 * Subtle, code in do_timer_interrupt() expects an AEOI
1784 * mode for the 8259A whenever interrupts are routed
1785 * through I/O APICs. Also IRQ0 has to be enabled in
1786 * the 8259A which implies the virtual wire has to be
1787 * disabled in the local APIC.
1789 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1791 if (timer_over_8254 > 0)
1792 enable_8259A_irq(0);
1794 pin1 = find_isa_irq_pin(0, mp_INT);
1795 apic1 = find_isa_irq_apic(0, mp_INT);
1796 pin2 = ioapic_i8259.pin;
1797 apic2 = ioapic_i8259.apic;
1800 timer_uses_ioapic_pin_0 = 1;
1802 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1803 vector, apic1, pin1, apic2, pin2);
1807 * Ok, does IRQ0 through the IOAPIC work?
1809 unmask_IO_APIC_irq(0);
1810 if (!no_timer_check && timer_irq_works()) {
1811 nmi_watchdog_default();
1812 if (nmi_watchdog == NMI_IO_APIC) {
1813 disable_8259A_irq(0);
1815 enable_8259A_irq(0);
1817 if (disable_timer_pin_1 > 0)
1818 clear_IO_APIC_pin(0, pin1);
1821 clear_IO_APIC_pin(apic1, pin1);
1822 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1823 "connected to IO-APIC\n");
1826 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1827 "through the 8259A ... ");
1829 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1832 * legacy devices should be connected to IO APIC #0
1834 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1835 if (timer_irq_works()) {
1836 apic_printk(APIC_VERBOSE," works.\n");
1837 nmi_watchdog_default();
1838 if (nmi_watchdog == NMI_IO_APIC) {
1844 * Cleanup, just in case ...
1846 clear_IO_APIC_pin(apic2, pin2);
1848 apic_printk(APIC_VERBOSE," failed.\n");
1850 if (nmi_watchdog == NMI_IO_APIC) {
1851 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1855 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1857 disable_8259A_irq(0);
1858 irq_desc[0].chip = &lapic_irq_type;
1859 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1860 enable_8259A_irq(0);
1862 if (timer_irq_works()) {
1863 apic_printk(APIC_VERBOSE," works.\n");
1866 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1867 apic_printk(APIC_VERBOSE," failed.\n");
1869 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1873 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1875 unlock_ExtINT_logic();
1877 if (timer_irq_works()) {
1878 apic_printk(APIC_VERBOSE," works.\n");
1881 apic_printk(APIC_VERBOSE," failed :(.\n");
1882 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1885 static int __init notimercheck(char *s)
1890 __setup("no_timer_check", notimercheck);
1894 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1895 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1896 * Linux doesn't really care, as it's not actually used
1897 * for any interrupt handling anyway.
1899 #define PIC_IRQS (1<<2)
1901 void __init setup_IO_APIC(void)
1906 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1908 io_apic_irqs = ~PIC_IRQS;
1910 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1913 * Set up the IO-APIC IRQ routing table.
1916 setup_ioapic_ids_from_mpc();
1918 setup_IO_APIC_irqs();
1919 init_IO_APIC_traps();
1925 struct sysfs_ioapic_data {
1926 struct sys_device dev;
1927 struct IO_APIC_route_entry entry[0];
1929 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1931 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1933 struct IO_APIC_route_entry *entry;
1934 struct sysfs_ioapic_data *data;
1935 unsigned long flags;
1938 data = container_of(dev, struct sysfs_ioapic_data, dev);
1939 entry = data->entry;
1940 spin_lock_irqsave(&ioapic_lock, flags);
1941 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1942 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1943 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1945 spin_unlock_irqrestore(&ioapic_lock, flags);
1950 static int ioapic_resume(struct sys_device *dev)
1952 struct IO_APIC_route_entry *entry;
1953 struct sysfs_ioapic_data *data;
1954 unsigned long flags;
1955 union IO_APIC_reg_00 reg_00;
1958 data = container_of(dev, struct sysfs_ioapic_data, dev);
1959 entry = data->entry;
1961 spin_lock_irqsave(&ioapic_lock, flags);
1962 reg_00.raw = io_apic_read(dev->id, 0);
1963 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1964 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1965 io_apic_write(dev->id, 0, reg_00.raw);
1967 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1968 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1969 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1971 spin_unlock_irqrestore(&ioapic_lock, flags);
1976 static struct sysdev_class ioapic_sysdev_class = {
1977 set_kset_name("ioapic"),
1978 .suspend = ioapic_suspend,
1979 .resume = ioapic_resume,
1982 static int __init ioapic_init_sysfs(void)
1984 struct sys_device * dev;
1985 int i, size, error = 0;
1987 error = sysdev_class_register(&ioapic_sysdev_class);
1991 for (i = 0; i < nr_ioapics; i++ ) {
1992 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1993 * sizeof(struct IO_APIC_route_entry);
1994 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1995 if (!mp_ioapic_data[i]) {
1996 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1999 memset(mp_ioapic_data[i], 0, size);
2000 dev = &mp_ioapic_data[i]->dev;
2002 dev->cls = &ioapic_sysdev_class;
2003 error = sysdev_register(dev);
2005 kfree(mp_ioapic_data[i]);
2006 mp_ioapic_data[i] = NULL;
2007 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2015 device_initcall(ioapic_init_sysfs);
2017 /* --------------------------------------------------------------------------
2018 ACPI-based IOAPIC Configuration
2019 -------------------------------------------------------------------------- */
2023 #define IO_APIC_MAX_ID 0xFE
2025 int __init io_apic_get_version (int ioapic)
2027 union IO_APIC_reg_01 reg_01;
2028 unsigned long flags;
2030 spin_lock_irqsave(&ioapic_lock, flags);
2031 reg_01.raw = io_apic_read(ioapic, 1);
2032 spin_unlock_irqrestore(&ioapic_lock, flags);
2034 return reg_01.bits.version;
2038 int __init io_apic_get_redir_entries (int ioapic)
2040 union IO_APIC_reg_01 reg_01;
2041 unsigned long flags;
2043 spin_lock_irqsave(&ioapic_lock, flags);
2044 reg_01.raw = io_apic_read(ioapic, 1);
2045 spin_unlock_irqrestore(&ioapic_lock, flags);
2047 return reg_01.bits.entries;
2051 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2053 struct IO_APIC_route_entry entry;
2054 unsigned long flags;
2056 if (!IO_APIC_IRQ(irq)) {
2057 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2063 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2064 * Note that we mask (disable) IRQs now -- these get enabled when the
2065 * corresponding device driver registers for this IRQ.
2068 memset(&entry,0,sizeof(entry));
2070 entry.delivery_mode = INT_DELIVERY_MODE;
2071 entry.dest_mode = INT_DEST_MODE;
2072 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2073 entry.trigger = triggering;
2074 entry.polarity = polarity;
2075 entry.mask = 1; /* Disabled (masked) */
2077 irq = gsi_irq_sharing(irq);
2079 * IRQs < 16 are already in the irq_2_pin[] map
2082 add_pin_to_irq(irq, ioapic, pin);
2084 entry.vector = assign_irq_vector(irq);
2086 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2087 "IRQ %d Mode:%i Active:%i)\n", ioapic,
2088 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2089 triggering, polarity);
2091 ioapic_register_intr(irq, entry.vector, triggering);
2093 if (!ioapic && (irq < 16))
2094 disable_8259A_irq(irq);
2096 spin_lock_irqsave(&ioapic_lock, flags);
2097 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2098 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2099 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
2100 spin_unlock_irqrestore(&ioapic_lock, flags);
2105 #endif /* CONFIG_ACPI */
2109 * This function currently is only a helper for the i386 smp boot process where
2110 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2111 * so mask in all cases should simply be TARGET_CPUS
2114 void __init setup_ioapic_dest(void)
2116 int pin, ioapic, irq, irq_entry;
2118 if (skip_ioapic_setup == 1)
2121 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2122 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2123 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2124 if (irq_entry == -1)
2126 irq = pin_2_irq(irq_entry, ioapic, pin);
2127 set_ioapic_affinity_irq(irq, TARGET_CPUS);