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;
54 int timer_over_8254 __initdata = 0;
56 /* Where if anywhere is the i8259 connect in external int mode */
57 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
60 static DEFINE_SPINLOCK(ioapic_lock);
61 static DEFINE_SPINLOCK(vector_lock);
64 * # of IRQ routing registers
66 int nr_ioapic_registers[MAX_IO_APICS];
69 * Rough estimation of how many shared IRQs there are, can
72 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
73 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
76 * This is performance-critical, we want to do it O(1)
78 * the indexing order of this array favors 1:1 mappings
79 * between pins and IRQs.
82 static struct irq_pin_list {
83 short apic, pin, next;
84 } irq_2_pin[PIN_MAP_SIZE];
86 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
88 #define vector_to_irq(vector) \
89 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
91 #define vector_to_irq(vector) (vector)
96 #include <xen/interface/xen.h>
97 #include <xen/interface/physdev.h>
100 #define make_8259A_irq(_irq) (io_apic_irqs &= ~(1UL<<(_irq)))
101 #define disable_8259A_irq(_irq) ((void)0)
102 #define i8259A_irq_pending(_irq) (0)
104 unsigned long io_apic_irqs;
106 static inline unsigned int xen_io_apic_read(unsigned int apic, unsigned int reg)
108 struct physdev_apic apic_op;
111 apic_op.apic_physbase = mp_ioapics[apic].mpc_apicaddr;
113 ret = HYPERVISOR_physdev_op(PHYSDEVOP_apic_read, &apic_op);
116 return apic_op.value;
119 static inline void xen_io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
121 struct physdev_apic apic_op;
123 apic_op.apic_physbase = mp_ioapics[apic].mpc_apicaddr;
125 apic_op.value = value;
126 HYPERVISOR_physdev_op(PHYSDEVOP_apic_write, &apic_op);
129 #define io_apic_read(a,r) xen_io_apic_read(a,r)
130 #define io_apic_write(a,r,v) xen_io_apic_write(a,r,v)
132 #define clear_IO_APIC() ((void)0)
137 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
143 cpus_and(tmp, mask, cpu_online_map);
147 cpus_and(mask, tmp, CPU_MASK_ALL);
149 dest = cpu_mask_to_apicid(mask);
152 * Only the high 8 bits are valid.
154 dest = SET_APIC_LOGICAL_ID(dest);
156 spin_lock_irqsave(&ioapic_lock, flags);
157 __DO_ACTION(1, = dest, )
158 set_irq_info(irq, mask);
159 spin_unlock_irqrestore(&ioapic_lock, flags);
163 #endif /* !CONFIG_XEN */
166 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
167 * shared ISA-space IRQs, so we have to support them. We are super
168 * fast in the common case, and fast for shared ISA-space IRQs.
170 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
172 static int first_free_entry = NR_IRQS;
173 struct irq_pin_list *entry = irq_2_pin + irq;
175 BUG_ON(irq >= NR_IRQS);
177 entry = irq_2_pin + entry->next;
179 if (entry->pin != -1) {
180 entry->next = first_free_entry;
181 entry = irq_2_pin + entry->next;
182 if (++first_free_entry >= PIN_MAP_SIZE)
183 panic("io_apic.c: ran out of irq_2_pin entries!");
190 #define __DO_ACTION(R, ACTION, FINAL) \
194 struct irq_pin_list *entry = irq_2_pin + irq; \
196 BUG_ON(irq >= NR_IRQS); \
202 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
204 io_apic_modify(entry->apic, reg); \
207 entry = irq_2_pin + entry->next; \
212 #define DO_ACTION(name,R,ACTION, FINAL) \
214 static void name##_IO_APIC_irq (unsigned int irq) \
215 __DO_ACTION(R, ACTION, FINAL)
217 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
219 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
222 static void mask_IO_APIC_irq (unsigned int irq)
226 spin_lock_irqsave(&ioapic_lock, flags);
227 __mask_IO_APIC_irq(irq);
228 spin_unlock_irqrestore(&ioapic_lock, flags);
231 static void unmask_IO_APIC_irq (unsigned int irq)
235 spin_lock_irqsave(&ioapic_lock, flags);
236 __unmask_IO_APIC_irq(irq);
237 spin_unlock_irqrestore(&ioapic_lock, flags);
240 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
242 struct IO_APIC_route_entry entry;
245 /* Check delivery_mode to be sure we're not clearing an SMI pin */
246 spin_lock_irqsave(&ioapic_lock, flags);
247 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
248 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
249 spin_unlock_irqrestore(&ioapic_lock, flags);
250 if (entry.delivery_mode == dest_SMI)
253 * Disable it in the IO-APIC irq-routing table:
255 memset(&entry, 0, sizeof(entry));
257 spin_lock_irqsave(&ioapic_lock, flags);
258 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
259 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
260 spin_unlock_irqrestore(&ioapic_lock, flags);
263 static void clear_IO_APIC (void)
267 for (apic = 0; apic < nr_ioapics; apic++)
268 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
269 clear_IO_APIC_pin(apic, pin);
272 #endif /* !CONFIG_XEN */
274 static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
277 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
278 * specific CPU-side IRQs.
282 static int pirq_entries [MAX_PIRQS];
283 static int pirqs_enabled;
284 int skip_ioapic_setup;
287 /* dummy parsing: see setup.c */
289 static int __init disable_ioapic_setup(char *str)
291 skip_ioapic_setup = 1;
295 static int __init enable_ioapic_setup(char *str)
298 skip_ioapic_setup = 0;
302 __setup("noapic", disable_ioapic_setup);
303 __setup("apic", enable_ioapic_setup);
306 static int __init setup_disable_8254_timer(char *s)
308 timer_over_8254 = -1;
311 static int __init setup_enable_8254_timer(char *s)
317 __setup("disable_8254_timer", setup_disable_8254_timer);
318 __setup("enable_8254_timer", setup_enable_8254_timer);
319 #endif /* !CONFIG_XEN */
321 #include <asm/pci-direct.h>
322 #include <linux/pci_ids.h>
323 #include <linux/pci.h>
328 static int nvidia_hpet_detected __initdata;
330 static int __init nvidia_hpet_check(unsigned long phys, unsigned long size)
332 nvidia_hpet_detected = 1;
337 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
338 off. Check for an Nvidia or VIA PCI bridge and turn it off.
339 Use pci direct infrastructure because this runs before the PCI subsystem.
341 Can be overwritten with "apic"
343 And another hack to disable the IOMMU on VIA chipsets.
345 ... and others. Really should move this somewhere else.
348 void __init check_ioapic(void)
351 /* Poor man's PCI discovery */
352 for (num = 0; num < 32; num++) {
353 for (slot = 0; slot < 32; slot++) {
354 for (func = 0; func < 8; func++) {
358 class = read_pci_config(num,slot,func,
360 if (class == 0xffffffff)
363 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
366 vendor = read_pci_config(num, slot, func,
370 case PCI_VENDOR_ID_VIA:
372 if ((end_pfn > MAX_DMA32_PFN ||
374 !iommu_aperture_allowed) {
376 "Looks like a VIA chipset. Disabling IOMMU. Override with \"iommu=allowed\"\n");
377 iommu_aperture_disabled = 1;
381 case PCI_VENDOR_ID_NVIDIA:
384 * All timer overrides on Nvidia are
385 * wrong unless HPET is enabled.
387 nvidia_hpet_detected = 0;
388 acpi_table_parse(ACPI_HPET,
390 if (nvidia_hpet_detected == 0) {
391 acpi_skip_timer_override = 1;
392 printk(KERN_INFO "Nvidia board "
393 "detected. Ignoring ACPI "
394 "timer override.\n");
397 /* RED-PEN skip them on mptables too? */
399 case PCI_VENDOR_ID_ATI:
401 /* This should be actually default, but
402 for 2.6.16 let's do it for ATI only where
403 it's really needed. */
405 if (timer_over_8254 == 1) {
408 "ATI board detected. Disabling timer routing over 8254.\n");
415 /* No multi-function device? */
416 type = read_pci_config_byte(num,slot,func,
425 static int __init ioapic_pirq_setup(char *str)
428 int ints[MAX_PIRQS+1];
430 get_options(str, ARRAY_SIZE(ints), ints);
432 for (i = 0; i < MAX_PIRQS; i++)
433 pirq_entries[i] = -1;
436 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
438 if (ints[0] < MAX_PIRQS)
441 for (i = 0; i < max; i++) {
442 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
444 * PIRQs are mapped upside down, usually.
446 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
451 __setup("pirq=", ioapic_pirq_setup);
454 * Find the IRQ entry number of a certain pin.
456 static int find_irq_entry(int apic, int pin, int type)
460 for (i = 0; i < mp_irq_entries; i++)
461 if (mp_irqs[i].mpc_irqtype == type &&
462 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
463 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
464 mp_irqs[i].mpc_dstirq == pin)
472 * Find the pin to which IRQ[irq] (ISA) is connected
474 static int __init find_isa_irq_pin(int irq, int type)
478 for (i = 0; i < mp_irq_entries; i++) {
479 int lbus = mp_irqs[i].mpc_srcbus;
481 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
482 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
483 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
484 (mp_irqs[i].mpc_irqtype == type) &&
485 (mp_irqs[i].mpc_srcbusirq == irq))
487 return mp_irqs[i].mpc_dstirq;
492 static int __init find_isa_irq_apic(int irq, int type)
496 for (i = 0; i < mp_irq_entries; i++) {
497 int lbus = mp_irqs[i].mpc_srcbus;
499 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
500 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
501 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
502 (mp_irqs[i].mpc_irqtype == type) &&
503 (mp_irqs[i].mpc_srcbusirq == irq))
506 if (i < mp_irq_entries) {
508 for(apic = 0; apic < nr_ioapics; apic++) {
509 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
519 * Find a specific PCI IRQ entry.
520 * Not an __init, possibly needed by modules
522 static int pin_2_irq(int idx, int apic, int pin);
524 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
526 int apic, i, best_guess = -1;
528 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
530 if (mp_bus_id_to_pci_bus[bus] == -1) {
531 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
534 for (i = 0; i < mp_irq_entries; i++) {
535 int lbus = mp_irqs[i].mpc_srcbus;
537 for (apic = 0; apic < nr_ioapics; apic++)
538 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
539 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
542 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
543 !mp_irqs[i].mpc_irqtype &&
545 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
546 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
548 if (!(apic || IO_APIC_IRQ(irq)))
551 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
554 * Use the first all-but-pin matching entry as a
555 * best-guess fuzzy result for broken mptables.
561 BUG_ON(best_guess >= NR_IRQS);
566 * EISA Edge/Level control register, ELCR
568 static int EISA_ELCR(unsigned int irq)
571 unsigned int port = 0x4d0 + (irq >> 3);
572 return (inb(port) >> (irq & 7)) & 1;
574 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
578 /* EISA interrupts are always polarity zero and can be edge or level
579 * trigger depending on the ELCR value. If an interrupt is listed as
580 * EISA conforming in the MP table, that means its trigger type must
581 * be read in from the ELCR */
583 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
584 #define default_EISA_polarity(idx) (0)
586 /* ISA interrupts are always polarity zero edge triggered,
587 * when listed as conforming in the MP table. */
589 #define default_ISA_trigger(idx) (0)
590 #define default_ISA_polarity(idx) (0)
592 /* PCI interrupts are always polarity one level triggered,
593 * when listed as conforming in the MP table. */
595 #define default_PCI_trigger(idx) (1)
596 #define default_PCI_polarity(idx) (1)
598 /* MCA interrupts are always polarity zero level triggered,
599 * when listed as conforming in the MP table. */
601 #define default_MCA_trigger(idx) (1)
602 #define default_MCA_polarity(idx) (0)
604 static int __init MPBIOS_polarity(int idx)
606 int bus = mp_irqs[idx].mpc_srcbus;
610 * Determine IRQ line polarity (high active or low active):
612 switch (mp_irqs[idx].mpc_irqflag & 3)
614 case 0: /* conforms, ie. bus-type dependent polarity */
616 switch (mp_bus_id_to_type[bus])
618 case MP_BUS_ISA: /* ISA pin */
620 polarity = default_ISA_polarity(idx);
623 case MP_BUS_EISA: /* EISA pin */
625 polarity = default_EISA_polarity(idx);
628 case MP_BUS_PCI: /* PCI pin */
630 polarity = default_PCI_polarity(idx);
633 case MP_BUS_MCA: /* MCA pin */
635 polarity = default_MCA_polarity(idx);
640 printk(KERN_WARNING "broken BIOS!!\n");
647 case 1: /* high active */
652 case 2: /* reserved */
654 printk(KERN_WARNING "broken BIOS!!\n");
658 case 3: /* low active */
663 default: /* invalid */
665 printk(KERN_WARNING "broken BIOS!!\n");
673 static int MPBIOS_trigger(int idx)
675 int bus = mp_irqs[idx].mpc_srcbus;
679 * Determine IRQ trigger mode (edge or level sensitive):
681 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
683 case 0: /* conforms, ie. bus-type dependent */
685 switch (mp_bus_id_to_type[bus])
687 case MP_BUS_ISA: /* ISA pin */
689 trigger = default_ISA_trigger(idx);
692 case MP_BUS_EISA: /* EISA pin */
694 trigger = default_EISA_trigger(idx);
697 case MP_BUS_PCI: /* PCI pin */
699 trigger = default_PCI_trigger(idx);
702 case MP_BUS_MCA: /* MCA pin */
704 trigger = default_MCA_trigger(idx);
709 printk(KERN_WARNING "broken BIOS!!\n");
721 case 2: /* reserved */
723 printk(KERN_WARNING "broken BIOS!!\n");
732 default: /* invalid */
734 printk(KERN_WARNING "broken BIOS!!\n");
742 static inline int irq_polarity(int idx)
744 return MPBIOS_polarity(idx);
747 static inline int irq_trigger(int idx)
749 return MPBIOS_trigger(idx);
752 static int next_irq = 16;
755 * gsi_irq_sharing -- Name overload! "irq" can be either a legacy IRQ
756 * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
757 * from ACPI, which can reach 800 in large boxen.
759 * Compact the sparse GSI space into a sequential IRQ series and reuse
760 * vectors if possible.
762 int gsi_irq_sharing(int gsi)
764 int i, tries, vector;
766 BUG_ON(gsi >= NR_IRQ_VECTORS);
768 if (platform_legacy_irq(gsi))
771 if (gsi_2_irq[gsi] != 0xFF)
772 return (int)gsi_2_irq[gsi];
776 vector = assign_irq_vector(gsi);
779 * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
780 * use of vector and if found, return that IRQ. However, we never want
781 * to share legacy IRQs, which usually have a different trigger mode
784 for (i = 0; i < NR_IRQS; i++)
785 if (IO_APIC_VECTOR(i) == vector)
787 if (platform_legacy_irq(i)) {
789 IO_APIC_VECTOR(i) = 0;
792 panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
796 printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
802 BUG_ON(i >= NR_IRQS);
804 IO_APIC_VECTOR(i) = vector;
805 printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
810 static int pin_2_irq(int idx, int apic, int pin)
813 int bus = mp_irqs[idx].mpc_srcbus;
816 * Debugging check, we are in big trouble if this message pops up!
818 if (mp_irqs[idx].mpc_dstirq != pin)
819 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
821 switch (mp_bus_id_to_type[bus])
823 case MP_BUS_ISA: /* ISA pin */
827 irq = mp_irqs[idx].mpc_srcbusirq;
830 case MP_BUS_PCI: /* PCI pin */
833 * PCI IRQs are mapped in order
837 irq += nr_ioapic_registers[i++];
839 irq = gsi_irq_sharing(irq);
844 printk(KERN_ERR "unknown bus type %d.\n",bus);
849 BUG_ON(irq >= NR_IRQS);
852 * PCI IRQ command line redirection. Yes, limits are hardcoded.
854 if ((pin >= 16) && (pin <= 23)) {
855 if (pirq_entries[pin-16] != -1) {
856 if (!pirq_entries[pin-16]) {
857 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
859 irq = pirq_entries[pin-16];
860 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
865 BUG_ON(irq >= NR_IRQS);
869 static inline int IO_APIC_irq_trigger(int irq)
873 for (apic = 0; apic < nr_ioapics; apic++) {
874 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
875 idx = find_irq_entry(apic,pin,mp_INT);
876 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
877 return irq_trigger(idx);
881 * nonexistent IRQs are edge default
886 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
887 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly;
889 int assign_irq_vector(int irq)
891 struct physdev_irq irq_op;
894 BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
896 spin_lock_irqsave(&vector_lock, flags);
898 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0) {
899 spin_unlock_irqrestore(&vector_lock, flags);
900 return IO_APIC_VECTOR(irq);
903 if (HYPERVISOR_physdev_op(PHYSDEVOP_alloc_irq_vector, &irq_op))
906 vector_irq[irq_op.vector] = irq;
907 if (irq != AUTO_ASSIGN)
908 IO_APIC_VECTOR(irq) = irq_op.vector;
910 spin_unlock_irqrestore(&vector_lock, flags);
911 return irq_op.vector;
914 extern void (*interrupt[NR_IRQS])(void);
916 static struct hw_interrupt_type ioapic_level_type;
917 static struct hw_interrupt_type ioapic_edge_type;
919 #define IOAPIC_AUTO -1
920 #define IOAPIC_EDGE 0
921 #define IOAPIC_LEVEL 1
923 static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
927 idx = use_pci_vector() && !platform_legacy_irq(irq) ? vector : irq;
929 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
930 trigger == IOAPIC_LEVEL)
931 irq_desc[idx].chip = &ioapic_level_type;
933 irq_desc[idx].chip = &ioapic_edge_type;
934 set_intr_gate(vector, interrupt[idx]);
937 #define ioapic_register_intr(_irq,_vector,_trigger) ((void)0)
938 #endif /* !CONFIG_XEN */
940 static void __init setup_IO_APIC_irqs(void)
942 struct IO_APIC_route_entry entry;
943 int apic, pin, idx, irq, first_notcon = 1, vector;
946 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
948 for (apic = 0; apic < nr_ioapics; apic++) {
949 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
952 * add it to the IO-APIC irq-routing table:
954 memset(&entry,0,sizeof(entry));
956 entry.delivery_mode = INT_DELIVERY_MODE;
957 entry.dest_mode = INT_DEST_MODE;
958 entry.mask = 0; /* enable IRQ */
959 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
961 idx = find_irq_entry(apic,pin,mp_INT);
964 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
967 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
971 entry.trigger = irq_trigger(idx);
972 entry.polarity = irq_polarity(idx);
974 if (irq_trigger(idx)) {
977 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
980 irq = pin_2_irq(idx, apic, pin);
981 add_pin_to_irq(irq, apic, pin);
983 if (/* !apic && */ !IO_APIC_IRQ(irq))
986 if (IO_APIC_IRQ(irq)) {
987 vector = assign_irq_vector(irq);
988 entry.vector = vector;
990 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
991 if (!apic && (irq < 16))
992 disable_8259A_irq(irq);
994 spin_lock_irqsave(&ioapic_lock, flags);
995 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
996 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
997 set_native_irq_info(irq, TARGET_CPUS);
998 spin_unlock_irqrestore(&ioapic_lock, flags);
1003 apic_printk(APIC_VERBOSE," not connected.\n");
1008 * Set up the 8259A-master output pin as broadcast to all
1011 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
1013 struct IO_APIC_route_entry entry;
1014 unsigned long flags;
1016 memset(&entry,0,sizeof(entry));
1018 disable_8259A_irq(0);
1021 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1024 * We use logical delivery to get the timer IRQ
1027 entry.dest_mode = INT_DEST_MODE;
1028 entry.mask = 0; /* unmask IRQ now */
1029 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1030 entry.delivery_mode = INT_DELIVERY_MODE;
1033 entry.vector = vector;
1036 * The timer IRQ doesn't have to know that behind the
1037 * scene we have a 8259A-master in AEOI mode ...
1039 irq_desc[0].chip = &ioapic_edge_type;
1042 * Add it to the IO-APIC irq-routing table:
1044 spin_lock_irqsave(&ioapic_lock, flags);
1045 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
1046 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
1047 spin_unlock_irqrestore(&ioapic_lock, flags);
1049 enable_8259A_irq(0);
1052 void __init UNEXPECTED_IO_APIC(void)
1056 void __apicdebuginit print_IO_APIC(void)
1059 union IO_APIC_reg_00 reg_00;
1060 union IO_APIC_reg_01 reg_01;
1061 union IO_APIC_reg_02 reg_02;
1062 unsigned long flags;
1064 if (apic_verbosity == APIC_QUIET)
1067 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1068 for (i = 0; i < nr_ioapics; i++)
1069 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1070 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
1073 * We are a bit conservative about what we expect. We have to
1074 * know about every hardware change ASAP.
1076 printk(KERN_INFO "testing the IO APIC.......................\n");
1078 for (apic = 0; apic < nr_ioapics; apic++) {
1080 spin_lock_irqsave(&ioapic_lock, flags);
1081 reg_00.raw = io_apic_read(apic, 0);
1082 reg_01.raw = io_apic_read(apic, 1);
1083 if (reg_01.bits.version >= 0x10)
1084 reg_02.raw = io_apic_read(apic, 2);
1085 spin_unlock_irqrestore(&ioapic_lock, flags);
1088 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1089 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1090 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1091 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
1092 UNEXPECTED_IO_APIC();
1094 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1095 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1096 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
1097 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
1098 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
1099 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1100 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1101 (reg_01.bits.entries != 0x2E) &&
1102 (reg_01.bits.entries != 0x3F) &&
1103 (reg_01.bits.entries != 0x03)
1105 UNEXPECTED_IO_APIC();
1107 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1108 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1109 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1110 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
1111 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1112 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1113 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1114 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1116 UNEXPECTED_IO_APIC();
1117 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1118 UNEXPECTED_IO_APIC();
1120 if (reg_01.bits.version >= 0x10) {
1121 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1122 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1123 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1124 UNEXPECTED_IO_APIC();
1127 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1129 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1130 " Stat Dest Deli Vect: \n");
1132 for (i = 0; i <= reg_01.bits.entries; i++) {
1133 struct IO_APIC_route_entry entry;
1135 spin_lock_irqsave(&ioapic_lock, flags);
1136 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
1137 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
1138 spin_unlock_irqrestore(&ioapic_lock, flags);
1140 printk(KERN_DEBUG " %02x %03X %02X ",
1142 entry.dest.logical.logical_dest,
1143 entry.dest.physical.physical_dest
1146 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1151 entry.delivery_status,
1153 entry.delivery_mode,
1158 if (use_pci_vector())
1159 printk(KERN_INFO "Using vector-based indexing\n");
1160 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1161 for (i = 0; i < NR_IRQS; i++) {
1162 struct irq_pin_list *entry = irq_2_pin + i;
1165 if (use_pci_vector() && !platform_legacy_irq(i))
1166 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1168 printk(KERN_DEBUG "IRQ%d ", i);
1170 printk("-> %d:%d", entry->apic, entry->pin);
1173 entry = irq_2_pin + entry->next;
1178 printk(KERN_INFO ".................................... done.\n");
1185 static __apicdebuginit void print_APIC_bitfield (int base)
1190 if (apic_verbosity == APIC_QUIET)
1193 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1194 for (i = 0; i < 8; i++) {
1195 v = apic_read(base + i*0x10);
1196 for (j = 0; j < 32; j++) {
1206 void __apicdebuginit print_local_APIC(void * dummy)
1208 unsigned int v, ver, maxlvt;
1210 if (apic_verbosity == APIC_QUIET)
1213 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1214 smp_processor_id(), hard_smp_processor_id());
1215 v = apic_read(APIC_ID);
1216 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1217 v = apic_read(APIC_LVR);
1218 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1219 ver = GET_APIC_VERSION(v);
1220 maxlvt = get_maxlvt();
1222 v = apic_read(APIC_TASKPRI);
1223 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1225 v = apic_read(APIC_ARBPRI);
1226 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1227 v & APIC_ARBPRI_MASK);
1228 v = apic_read(APIC_PROCPRI);
1229 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1231 v = apic_read(APIC_EOI);
1232 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1233 v = apic_read(APIC_RRR);
1234 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1235 v = apic_read(APIC_LDR);
1236 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1237 v = apic_read(APIC_DFR);
1238 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1239 v = apic_read(APIC_SPIV);
1240 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1242 printk(KERN_DEBUG "... APIC ISR field:\n");
1243 print_APIC_bitfield(APIC_ISR);
1244 printk(KERN_DEBUG "... APIC TMR field:\n");
1245 print_APIC_bitfield(APIC_TMR);
1246 printk(KERN_DEBUG "... APIC IRR field:\n");
1247 print_APIC_bitfield(APIC_IRR);
1249 v = apic_read(APIC_ESR);
1250 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1252 v = apic_read(APIC_ICR);
1253 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1254 v = apic_read(APIC_ICR2);
1255 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1257 v = apic_read(APIC_LVTT);
1258 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1260 if (maxlvt > 3) { /* PC is LVT#4. */
1261 v = apic_read(APIC_LVTPC);
1262 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1264 v = apic_read(APIC_LVT0);
1265 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1266 v = apic_read(APIC_LVT1);
1267 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1269 if (maxlvt > 2) { /* ERR is LVT#3. */
1270 v = apic_read(APIC_LVTERR);
1271 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1274 v = apic_read(APIC_TMICT);
1275 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1276 v = apic_read(APIC_TMCCT);
1277 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1278 v = apic_read(APIC_TDCR);
1279 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1283 void print_all_local_APICs (void)
1285 on_each_cpu(print_local_APIC, NULL, 1, 1);
1288 void __apicdebuginit print_PIC(void)
1291 unsigned long flags;
1293 if (apic_verbosity == APIC_QUIET)
1296 printk(KERN_DEBUG "\nprinting PIC contents\n");
1298 spin_lock_irqsave(&i8259A_lock, flags);
1300 v = inb(0xa1) << 8 | inb(0x21);
1301 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1303 v = inb(0xa0) << 8 | inb(0x20);
1304 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1308 v = inb(0xa0) << 8 | inb(0x20);
1312 spin_unlock_irqrestore(&i8259A_lock, flags);
1314 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1316 v = inb(0x4d1) << 8 | inb(0x4d0);
1317 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1323 void __init print_IO_APIC(void) { }
1324 #endif /* !CONFIG_XEN */
1326 static void __init enable_IO_APIC(void)
1328 union IO_APIC_reg_01 reg_01;
1330 int i8259_apic, i8259_pin;
1333 unsigned long flags;
1335 for (i = 0; i < PIN_MAP_SIZE; i++) {
1336 irq_2_pin[i].pin = -1;
1337 irq_2_pin[i].next = 0;
1340 for (i = 0; i < MAX_PIRQS; i++)
1341 pirq_entries[i] = -1;
1344 * The number of IO-APIC IRQ registers (== #pins):
1346 for (apic = 0; apic < nr_ioapics; apic++) {
1347 spin_lock_irqsave(&ioapic_lock, flags);
1348 reg_01.raw = io_apic_read(apic, 1);
1349 spin_unlock_irqrestore(&ioapic_lock, flags);
1350 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1353 for(apic = 0; apic < nr_ioapics; apic++) {
1355 /* See if any of the pins is in ExtINT mode */
1356 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1357 struct IO_APIC_route_entry entry;
1358 spin_lock_irqsave(&ioapic_lock, flags);
1359 *(((int *)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1360 *(((int *)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1361 spin_unlock_irqrestore(&ioapic_lock, flags);
1364 /* If the interrupt line is enabled and in ExtInt mode
1365 * I have found the pin where the i8259 is connected.
1367 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1368 ioapic_i8259.apic = apic;
1369 ioapic_i8259.pin = pin;
1375 /* Look to see what if the MP table has reported the ExtINT */
1376 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1377 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1378 /* Trust the MP table if nothing is setup in the hardware */
1379 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1380 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1381 ioapic_i8259.pin = i8259_pin;
1382 ioapic_i8259.apic = i8259_apic;
1384 /* Complain if the MP table and the hardware disagree */
1385 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1386 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1388 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1393 * Do not trust the IO-APIC being empty at bootup
1399 * Not an __init, needed by the reboot code
1401 void disable_IO_APIC(void)
1404 * Clear the IO-APIC before rebooting:
1410 * If the i8259 is routed through an IOAPIC
1411 * Put that IOAPIC in virtual wire mode
1412 * so legacy interrupts can be delivered.
1414 if (ioapic_i8259.pin != -1) {
1415 struct IO_APIC_route_entry entry;
1416 unsigned long flags;
1418 memset(&entry, 0, sizeof(entry));
1419 entry.mask = 0; /* Enabled */
1420 entry.trigger = 0; /* Edge */
1422 entry.polarity = 0; /* High */
1423 entry.delivery_status = 0;
1424 entry.dest_mode = 0; /* Physical */
1425 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1427 entry.dest.physical.physical_dest =
1428 GET_APIC_ID(apic_read(APIC_ID));
1431 * Add it to the IO-APIC irq-routing table:
1433 spin_lock_irqsave(&ioapic_lock, flags);
1434 io_apic_write(ioapic_i8259.apic, 0x11+2*ioapic_i8259.pin,
1435 *(((int *)&entry)+1));
1436 io_apic_write(ioapic_i8259.apic, 0x10+2*ioapic_i8259.pin,
1437 *(((int *)&entry)+0));
1438 spin_unlock_irqrestore(&ioapic_lock, flags);
1441 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1446 * function to set the IO-APIC physical IDs based on the
1447 * values stored in the MPC table.
1449 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1453 static void __init setup_ioapic_ids_from_mpc (void)
1455 union IO_APIC_reg_00 reg_00;
1458 unsigned char old_id;
1459 unsigned long flags;
1462 * Set the IOAPIC ID to the value stored in the MPC table.
1464 for (apic = 0; apic < nr_ioapics; apic++) {
1466 /* Read the register 0 value */
1467 spin_lock_irqsave(&ioapic_lock, flags);
1468 reg_00.raw = io_apic_read(apic, 0);
1469 spin_unlock_irqrestore(&ioapic_lock, flags);
1471 old_id = mp_ioapics[apic].mpc_apicid;
1474 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1478 * We need to adjust the IRQ routing table
1479 * if the ID changed.
1481 if (old_id != mp_ioapics[apic].mpc_apicid)
1482 for (i = 0; i < mp_irq_entries; i++)
1483 if (mp_irqs[i].mpc_dstapic == old_id)
1484 mp_irqs[i].mpc_dstapic
1485 = mp_ioapics[apic].mpc_apicid;
1488 * Read the right value from the MPC table and
1489 * write it into the ID register.
1491 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1492 mp_ioapics[apic].mpc_apicid);
1494 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1495 spin_lock_irqsave(&ioapic_lock, flags);
1496 io_apic_write(apic, 0, reg_00.raw);
1497 spin_unlock_irqrestore(&ioapic_lock, flags);
1502 spin_lock_irqsave(&ioapic_lock, flags);
1503 reg_00.raw = io_apic_read(apic, 0);
1504 spin_unlock_irqrestore(&ioapic_lock, flags);
1505 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1506 printk("could not set ID!\n");
1508 apic_printk(APIC_VERBOSE," ok.\n");
1512 static void __init setup_ioapic_ids_from_mpc(void) { }
1516 * There is a nasty bug in some older SMP boards, their mptable lies
1517 * about the timer IRQ. We do the following to work around the situation:
1519 * - timer IRQ defaults to IO-APIC IRQ
1520 * - if this function detects that timer IRQs are defunct, then we fall
1521 * back to ISA timer IRQs
1524 static int __init timer_irq_works(void)
1526 unsigned long t1 = jiffies;
1529 /* Let ten ticks pass... */
1530 mdelay((10 * 1000) / HZ);
1533 * Expect a few ticks at least, to be sure some possible
1534 * glue logic does not lock up after one or two first
1535 * ticks in a non-ExtINT mode. Also the local APIC
1536 * might have cached one ExtINT interrupt. Finally, at
1537 * least one tick may be lost due to delays.
1541 if (jiffies - t1 > 4)
1547 * In the SMP+IOAPIC case it might happen that there are an unspecified
1548 * number of pending IRQ events unhandled. These cases are very rare,
1549 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1550 * better to do it this way as thus we do not have to be aware of
1551 * 'pending' interrupts in the IRQ path, except at this point.
1554 * Edge triggered needs to resend any interrupt
1555 * that was delayed but this is now handled in the device
1560 * Starting up a edge-triggered IO-APIC interrupt is
1561 * nasty - we need to make sure that we get the edge.
1562 * If it is already asserted for some reason, we need
1563 * return 1 to indicate that is was pending.
1565 * This is not complete - we should be able to fake
1566 * an edge even if it isn't on the 8259A...
1569 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1571 int was_pending = 0;
1572 unsigned long flags;
1574 spin_lock_irqsave(&ioapic_lock, flags);
1576 disable_8259A_irq(irq);
1577 if (i8259A_irq_pending(irq))
1580 __unmask_IO_APIC_irq(irq);
1581 spin_unlock_irqrestore(&ioapic_lock, flags);
1587 * Once we have recorded IRQ_PENDING already, we can mask the
1588 * interrupt for real. This prevents IRQ storms from unhandled
1591 static void ack_edge_ioapic_irq(unsigned int irq)
1594 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1595 == (IRQ_PENDING | IRQ_DISABLED))
1596 mask_IO_APIC_irq(irq);
1601 * Level triggered interrupts can just be masked,
1602 * and shutting down and starting up the interrupt
1603 * is the same as enabling and disabling them -- except
1604 * with a startup need to return a "was pending" value.
1606 * Level triggered interrupts are special because we
1607 * do not touch any IO-APIC register while handling
1608 * them. We ack the APIC in the end-IRQ handler, not
1609 * in the start-IRQ-handler. Protection against reentrance
1610 * from the same interrupt is still provided, both by the
1611 * generic IRQ layer and by the fact that an unacked local
1612 * APIC does not accept IRQs.
1614 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1616 unmask_IO_APIC_irq(irq);
1618 return 0; /* don't check for pending */
1621 static void end_level_ioapic_irq (unsigned int irq)
1627 #ifdef CONFIG_PCI_MSI
1628 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1630 int irq = vector_to_irq(vector);
1632 return startup_edge_ioapic_irq(irq);
1635 static void ack_edge_ioapic_vector(unsigned int vector)
1637 int irq = vector_to_irq(vector);
1639 move_native_irq(vector);
1640 ack_edge_ioapic_irq(irq);
1643 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1645 int irq = vector_to_irq(vector);
1647 return startup_level_ioapic_irq (irq);
1650 static void end_level_ioapic_vector (unsigned int vector)
1652 int irq = vector_to_irq(vector);
1654 move_native_irq(vector);
1655 end_level_ioapic_irq(irq);
1658 static void mask_IO_APIC_vector (unsigned int vector)
1660 int irq = vector_to_irq(vector);
1662 mask_IO_APIC_irq(irq);
1665 static void unmask_IO_APIC_vector (unsigned int vector)
1667 int irq = vector_to_irq(vector);
1669 unmask_IO_APIC_irq(irq);
1673 static void set_ioapic_affinity_vector (unsigned int vector,
1676 int irq = vector_to_irq(vector);
1678 set_native_irq_info(vector, cpu_mask);
1679 set_ioapic_affinity_irq(irq, cpu_mask);
1681 #endif // CONFIG_SMP
1682 #endif // CONFIG_PCI_MSI
1684 static int ioapic_retrigger(unsigned int irq)
1686 send_IPI_self(IO_APIC_VECTOR(irq));
1692 * Level and edge triggered IO-APIC interrupts need different handling,
1693 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1694 * handled with the level-triggered descriptor, but that one has slightly
1695 * more overhead. Level-triggered interrupts cannot be handled with the
1696 * edge-triggered handler, without risking IRQ storms and other ugly
1700 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1701 .typename = "IO-APIC-edge",
1702 .startup = startup_edge_ioapic,
1703 .shutdown = shutdown_edge_ioapic,
1704 .enable = enable_edge_ioapic,
1705 .disable = disable_edge_ioapic,
1706 .ack = ack_edge_ioapic,
1707 .end = end_edge_ioapic,
1709 .set_affinity = set_ioapic_affinity,
1711 .retrigger = ioapic_retrigger,
1714 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1715 .typename = "IO-APIC-level",
1716 .startup = startup_level_ioapic,
1717 .shutdown = shutdown_level_ioapic,
1718 .enable = enable_level_ioapic,
1719 .disable = disable_level_ioapic,
1720 .ack = mask_and_ack_level_ioapic,
1721 .end = end_level_ioapic,
1723 .set_affinity = set_ioapic_affinity,
1725 .retrigger = ioapic_retrigger,
1727 #endif /* !CONFIG_XEN */
1729 static inline void init_IO_APIC_traps(void)
1734 * NOTE! The local APIC isn't very good at handling
1735 * multiple interrupts at the same interrupt level.
1736 * As the interrupt level is determined by taking the
1737 * vector number and shifting that right by 4, we
1738 * want to spread these out a bit so that they don't
1739 * all fall in the same interrupt level.
1741 * Also, we've got to be careful not to trash gate
1742 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1744 for (irq = 0; irq < NR_IRQS ; irq++) {
1746 if (use_pci_vector()) {
1747 if (!platform_legacy_irq(tmp))
1748 if ((tmp = vector_to_irq(tmp)) == -1)
1751 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1753 * Hmm.. We don't have an entry for this,
1754 * so default to an old-fashioned 8259
1755 * interrupt if we can..
1758 make_8259A_irq(irq);
1761 /* Strange. Oh, well.. */
1762 irq_desc[irq].chip = &no_irq_type;
1769 static void enable_lapic_irq (unsigned int irq)
1773 v = apic_read(APIC_LVT0);
1774 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1777 static void disable_lapic_irq (unsigned int irq)
1781 v = apic_read(APIC_LVT0);
1782 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1785 static void ack_lapic_irq (unsigned int irq)
1790 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1792 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1793 .typename = "local-APIC-edge",
1794 .startup = NULL, /* startup_irq() not used for IRQ0 */
1795 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1796 .enable = enable_lapic_irq,
1797 .disable = disable_lapic_irq,
1798 .ack = ack_lapic_irq,
1799 .end = end_lapic_irq,
1802 static void setup_nmi (void)
1805 * Dirty trick to enable the NMI watchdog ...
1806 * We put the 8259A master into AEOI mode and
1807 * unmask on all local APICs LVT0 as NMI.
1809 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1810 * is from Maciej W. Rozycki - so we do not have to EOI from
1811 * the NMI handler or the timer interrupt.
1813 printk(KERN_INFO "activating NMI Watchdog ...");
1815 enable_NMI_through_LVT0(NULL);
1821 * This looks a bit hackish but it's about the only one way of sending
1822 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1823 * not support the ExtINT mode, unfortunately. We need to send these
1824 * cycles as some i82489DX-based boards have glue logic that keeps the
1825 * 8259A interrupt line asserted until INTA. --macro
1827 static inline void unlock_ExtINT_logic(void)
1830 struct IO_APIC_route_entry entry0, entry1;
1831 unsigned char save_control, save_freq_select;
1832 unsigned long flags;
1834 pin = find_isa_irq_pin(8, mp_INT);
1835 apic = find_isa_irq_apic(8, mp_INT);
1839 spin_lock_irqsave(&ioapic_lock, flags);
1840 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1841 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1842 spin_unlock_irqrestore(&ioapic_lock, flags);
1843 clear_IO_APIC_pin(apic, pin);
1845 memset(&entry1, 0, sizeof(entry1));
1847 entry1.dest_mode = 0; /* physical delivery */
1848 entry1.mask = 0; /* unmask IRQ now */
1849 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1850 entry1.delivery_mode = dest_ExtINT;
1851 entry1.polarity = entry0.polarity;
1855 spin_lock_irqsave(&ioapic_lock, flags);
1856 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1857 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1858 spin_unlock_irqrestore(&ioapic_lock, flags);
1860 save_control = CMOS_READ(RTC_CONTROL);
1861 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1862 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1864 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1869 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1873 CMOS_WRITE(save_control, RTC_CONTROL);
1874 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1875 clear_IO_APIC_pin(apic, pin);
1877 spin_lock_irqsave(&ioapic_lock, flags);
1878 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1879 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1880 spin_unlock_irqrestore(&ioapic_lock, flags);
1883 int timer_uses_ioapic_pin_0;
1886 * This code may look a bit paranoid, but it's supposed to cooperate with
1887 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1888 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1889 * fanatically on his truly buggy board.
1891 * FIXME: really need to revamp this for modern platforms only.
1893 static inline void check_timer(void)
1895 int apic1, pin1, apic2, pin2;
1899 * get/set the timer IRQ vector:
1901 disable_8259A_irq(0);
1902 vector = assign_irq_vector(0);
1903 set_intr_gate(vector, interrupt[0]);
1906 * Subtle, code in do_timer_interrupt() expects an AEOI
1907 * mode for the 8259A whenever interrupts are routed
1908 * through I/O APICs. Also IRQ0 has to be enabled in
1909 * the 8259A which implies the virtual wire has to be
1910 * disabled in the local APIC.
1912 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1914 if (timer_over_8254 > 0)
1915 enable_8259A_irq(0);
1917 pin1 = find_isa_irq_pin(0, mp_INT);
1918 apic1 = find_isa_irq_apic(0, mp_INT);
1919 pin2 = ioapic_i8259.pin;
1920 apic2 = ioapic_i8259.apic;
1923 timer_uses_ioapic_pin_0 = 1;
1925 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1926 vector, apic1, pin1, apic2, pin2);
1930 * Ok, does IRQ0 through the IOAPIC work?
1932 unmask_IO_APIC_irq(0);
1933 if (!no_timer_check && timer_irq_works()) {
1934 nmi_watchdog_default();
1935 if (nmi_watchdog == NMI_IO_APIC) {
1936 disable_8259A_irq(0);
1938 enable_8259A_irq(0);
1940 if (disable_timer_pin_1 > 0)
1941 clear_IO_APIC_pin(0, pin1);
1944 clear_IO_APIC_pin(apic1, pin1);
1945 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1946 "connected to IO-APIC\n");
1949 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1950 "through the 8259A ... ");
1952 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1955 * legacy devices should be connected to IO APIC #0
1957 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1958 if (timer_irq_works()) {
1959 apic_printk(APIC_VERBOSE," works.\n");
1960 nmi_watchdog_default();
1961 if (nmi_watchdog == NMI_IO_APIC) {
1967 * Cleanup, just in case ...
1969 clear_IO_APIC_pin(apic2, pin2);
1971 apic_printk(APIC_VERBOSE," failed.\n");
1973 if (nmi_watchdog == NMI_IO_APIC) {
1974 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1978 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1980 disable_8259A_irq(0);
1981 irq_desc[0].chip = &lapic_irq_type;
1982 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1983 enable_8259A_irq(0);
1985 if (timer_irq_works()) {
1986 apic_printk(APIC_VERBOSE," works.\n");
1989 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1990 apic_printk(APIC_VERBOSE," failed.\n");
1992 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1996 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1998 unlock_ExtINT_logic();
2000 if (timer_irq_works()) {
2001 apic_printk(APIC_VERBOSE," works.\n");
2004 apic_printk(APIC_VERBOSE," failed :(.\n");
2005 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
2008 int timer_uses_ioapic_pin_0;
2009 #define check_timer() ((void)0)
2010 #endif /* !CONFIG_XEN */
2012 static int __init notimercheck(char *s)
2017 __setup("no_timer_check", notimercheck);
2021 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2022 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2023 * Linux doesn't really care, as it's not actually used
2024 * for any interrupt handling anyway.
2026 #define PIC_IRQS (1<<2)
2028 void __init setup_IO_APIC(void)
2033 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2035 io_apic_irqs = ~PIC_IRQS;
2037 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2040 * Set up the IO-APIC IRQ routing table.
2043 setup_ioapic_ids_from_mpc();
2046 #endif /* !CONFIG_XEN */
2047 setup_IO_APIC_irqs();
2048 init_IO_APIC_traps();
2054 struct sysfs_ioapic_data {
2055 struct sys_device dev;
2056 struct IO_APIC_route_entry entry[0];
2058 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2060 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2062 struct IO_APIC_route_entry *entry;
2063 struct sysfs_ioapic_data *data;
2064 unsigned long flags;
2067 data = container_of(dev, struct sysfs_ioapic_data, dev);
2068 entry = data->entry;
2069 spin_lock_irqsave(&ioapic_lock, flags);
2070 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
2071 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
2072 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
2074 spin_unlock_irqrestore(&ioapic_lock, flags);
2079 static int ioapic_resume(struct sys_device *dev)
2081 struct IO_APIC_route_entry *entry;
2082 struct sysfs_ioapic_data *data;
2083 unsigned long flags;
2084 union IO_APIC_reg_00 reg_00;
2087 data = container_of(dev, struct sysfs_ioapic_data, dev);
2088 entry = data->entry;
2090 spin_lock_irqsave(&ioapic_lock, flags);
2091 reg_00.raw = io_apic_read(dev->id, 0);
2092 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
2093 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
2094 io_apic_write(dev->id, 0, reg_00.raw);
2096 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
2097 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
2098 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
2100 spin_unlock_irqrestore(&ioapic_lock, flags);
2105 static struct sysdev_class ioapic_sysdev_class = {
2106 set_kset_name("ioapic"),
2107 .suspend = ioapic_suspend,
2108 .resume = ioapic_resume,
2111 static int __init ioapic_init_sysfs(void)
2113 struct sys_device * dev;
2114 int i, size, error = 0;
2116 error = sysdev_class_register(&ioapic_sysdev_class);
2120 for (i = 0; i < nr_ioapics; i++ ) {
2121 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2122 * sizeof(struct IO_APIC_route_entry);
2123 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
2124 if (!mp_ioapic_data[i]) {
2125 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2128 memset(mp_ioapic_data[i], 0, size);
2129 dev = &mp_ioapic_data[i]->dev;
2131 dev->cls = &ioapic_sysdev_class;
2132 error = sysdev_register(dev);
2134 kfree(mp_ioapic_data[i]);
2135 mp_ioapic_data[i] = NULL;
2136 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2144 device_initcall(ioapic_init_sysfs);
2146 /* --------------------------------------------------------------------------
2147 ACPI-based IOAPIC Configuration
2148 -------------------------------------------------------------------------- */
2152 #define IO_APIC_MAX_ID 0xFE
2154 int __init io_apic_get_version (int ioapic)
2156 union IO_APIC_reg_01 reg_01;
2157 unsigned long flags;
2159 spin_lock_irqsave(&ioapic_lock, flags);
2160 reg_01.raw = io_apic_read(ioapic, 1);
2161 spin_unlock_irqrestore(&ioapic_lock, flags);
2163 return reg_01.bits.version;
2167 int __init io_apic_get_redir_entries (int ioapic)
2169 union IO_APIC_reg_01 reg_01;
2170 unsigned long flags;
2172 spin_lock_irqsave(&ioapic_lock, flags);
2173 reg_01.raw = io_apic_read(ioapic, 1);
2174 spin_unlock_irqrestore(&ioapic_lock, flags);
2176 return reg_01.bits.entries;
2180 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2182 struct IO_APIC_route_entry entry;
2183 unsigned long flags;
2185 if (!IO_APIC_IRQ(irq)) {
2186 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2192 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2193 * Note that we mask (disable) IRQs now -- these get enabled when the
2194 * corresponding device driver registers for this IRQ.
2197 memset(&entry,0,sizeof(entry));
2199 entry.delivery_mode = INT_DELIVERY_MODE;
2200 entry.dest_mode = INT_DEST_MODE;
2201 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2202 entry.trigger = edge_level;
2203 entry.polarity = active_high_low;
2204 entry.mask = 1; /* Disabled (masked) */
2206 irq = gsi_irq_sharing(irq);
2208 * IRQs < 16 are already in the irq_2_pin[] map
2211 add_pin_to_irq(irq, ioapic, pin);
2213 entry.vector = assign_irq_vector(irq);
2215 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2216 "IRQ %d Mode:%i Active:%i)\n", ioapic,
2217 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2218 edge_level, active_high_low);
2220 ioapic_register_intr(irq, entry.vector, edge_level);
2222 if (!ioapic && (irq < 16))
2223 disable_8259A_irq(irq);
2225 spin_lock_irqsave(&ioapic_lock, flags);
2226 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2227 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2228 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
2229 spin_unlock_irqrestore(&ioapic_lock, flags);
2234 #endif /* CONFIG_ACPI */
2239 * This function currently is only a helper for the i386 smp boot process where
2240 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2241 * so mask in all cases should simply be TARGET_CPUS
2244 void __init setup_ioapic_dest(void)
2246 int pin, ioapic, irq, irq_entry;
2248 if (skip_ioapic_setup == 1)
2251 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2252 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2253 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2254 if (irq_entry == -1)
2256 irq = pin_2_irq(irq_entry, ioapic, pin);
2257 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2263 #endif /* !CONFIG_XEN */