2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/config.h>
29 #include <linux/smp_lock.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
34 #include <acpi/acpi_bus.h>
40 #include <asm/proto.h>
41 #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);
61 * # of IRQ routing registers
63 int nr_ioapic_registers[MAX_IO_APICS];
66 * Rough estimation of how many shared IRQs there are, can
69 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
70 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
73 * This is performance-critical, we want to do it O(1)
75 * the indexing order of this array favors 1:1 mappings
76 * between pins and IRQs.
79 static struct irq_pin_list {
80 short apic, pin, next;
81 } irq_2_pin[PIN_MAP_SIZE];
83 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
85 #define vector_to_irq(vector) \
86 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
88 #define vector_to_irq(vector) (vector)
91 #define __DO_ACTION(R, ACTION, FINAL) \
95 struct irq_pin_list *entry = irq_2_pin + irq; \
97 BUG_ON(irq >= NR_IRQS); \
103 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
105 io_apic_modify(entry->apic, reg); \
108 entry = irq_2_pin + entry->next; \
114 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
120 cpus_and(tmp, mask, cpu_online_map);
124 cpus_and(mask, tmp, CPU_MASK_ALL);
126 dest = cpu_mask_to_apicid(mask);
129 * Only the high 8 bits are valid.
131 dest = SET_APIC_LOGICAL_ID(dest);
133 spin_lock_irqsave(&ioapic_lock, flags);
134 __DO_ACTION(1, = dest, )
135 set_irq_info(irq, mask);
136 spin_unlock_irqrestore(&ioapic_lock, flags);
140 static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
143 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
144 * shared ISA-space IRQs, so we have to support them. We are super
145 * fast in the common case, and fast for shared ISA-space IRQs.
147 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
149 static int first_free_entry = NR_IRQS;
150 struct irq_pin_list *entry = irq_2_pin + irq;
152 BUG_ON(irq >= NR_IRQS);
154 entry = irq_2_pin + entry->next;
156 if (entry->pin != -1) {
157 entry->next = first_free_entry;
158 entry = irq_2_pin + entry->next;
159 if (++first_free_entry >= PIN_MAP_SIZE)
160 panic("io_apic.c: ran out of irq_2_pin entries!");
167 #define DO_ACTION(name,R,ACTION, FINAL) \
169 static void name##_IO_APIC_irq (unsigned int irq) \
170 __DO_ACTION(R, ACTION, FINAL)
172 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
174 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
177 static void mask_IO_APIC_irq (unsigned int irq)
181 spin_lock_irqsave(&ioapic_lock, flags);
182 __mask_IO_APIC_irq(irq);
183 spin_unlock_irqrestore(&ioapic_lock, flags);
186 static void unmask_IO_APIC_irq (unsigned int irq)
190 spin_lock_irqsave(&ioapic_lock, flags);
191 __unmask_IO_APIC_irq(irq);
192 spin_unlock_irqrestore(&ioapic_lock, flags);
195 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
197 struct IO_APIC_route_entry entry;
200 /* Check delivery_mode to be sure we're not clearing an SMI pin */
201 spin_lock_irqsave(&ioapic_lock, flags);
202 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
203 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
204 spin_unlock_irqrestore(&ioapic_lock, flags);
205 if (entry.delivery_mode == dest_SMI)
208 * Disable it in the IO-APIC irq-routing table:
210 memset(&entry, 0, sizeof(entry));
212 spin_lock_irqsave(&ioapic_lock, flags);
213 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
214 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
215 spin_unlock_irqrestore(&ioapic_lock, flags);
218 static void clear_IO_APIC (void)
222 for (apic = 0; apic < nr_ioapics; apic++)
223 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
224 clear_IO_APIC_pin(apic, pin);
228 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
229 * specific CPU-side IRQs.
233 static int pirq_entries [MAX_PIRQS];
234 static int pirqs_enabled;
235 int skip_ioapic_setup;
238 /* dummy parsing: see setup.c */
240 static int __init disable_ioapic_setup(char *str)
242 skip_ioapic_setup = 1;
246 static int __init enable_ioapic_setup(char *str)
249 skip_ioapic_setup = 0;
253 __setup("noapic", disable_ioapic_setup);
254 __setup("apic", enable_ioapic_setup);
256 static int __init setup_disable_8254_timer(char *s)
258 timer_over_8254 = -1;
261 static int __init setup_enable_8254_timer(char *s)
267 __setup("disable_8254_timer", setup_disable_8254_timer);
268 __setup("enable_8254_timer", setup_enable_8254_timer);
270 #include <asm/pci-direct.h>
271 #include <linux/pci_ids.h>
272 #include <linux/pci.h>
274 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
275 off. Check for an Nvidia or VIA PCI bridge and turn it off.
276 Use pci direct infrastructure because this runs before the PCI subsystem.
278 Can be overwritten with "apic"
280 And another hack to disable the IOMMU on VIA chipsets.
282 ... and others. Really should move this somewhere else.
285 void __init check_ioapic(void)
288 /* Poor man's PCI discovery */
289 for (num = 0; num < 32; num++) {
290 for (slot = 0; slot < 32; slot++) {
291 for (func = 0; func < 8; func++) {
295 class = read_pci_config(num,slot,func,
297 if (class == 0xffffffff)
300 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
303 vendor = read_pci_config(num, slot, func,
307 case PCI_VENDOR_ID_VIA:
308 #ifdef CONFIG_GART_IOMMU
309 if ((end_pfn > MAX_DMA32_PFN ||
311 !iommu_aperture_allowed) {
313 "Looks like a VIA chipset. Disabling IOMMU. Override with \"iommu=allowed\"\n");
314 iommu_aperture_disabled = 1;
318 case PCI_VENDOR_ID_NVIDIA:
320 /* All timer overrides on Nvidia
321 seem to be wrong. Skip them. */
322 acpi_skip_timer_override = 1;
324 "Nvidia board detected. Ignoring ACPI timer override.\n");
326 /* RED-PEN skip them on mptables too? */
329 /* This should be actually default, but
330 for 2.6.16 let's do it for ATI only where
331 it's really needed. */
332 case PCI_VENDOR_ID_ATI:
333 if (timer_over_8254 == 1) {
336 "ATI board detected. Disabling timer routing over 8254.\n");
342 /* No multi-function device? */
343 type = read_pci_config_byte(num,slot,func,
352 static int __init ioapic_pirq_setup(char *str)
355 int ints[MAX_PIRQS+1];
357 get_options(str, ARRAY_SIZE(ints), ints);
359 for (i = 0; i < MAX_PIRQS; i++)
360 pirq_entries[i] = -1;
363 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
365 if (ints[0] < MAX_PIRQS)
368 for (i = 0; i < max; i++) {
369 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
371 * PIRQs are mapped upside down, usually.
373 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
378 __setup("pirq=", ioapic_pirq_setup);
381 * Find the IRQ entry number of a certain pin.
383 static int find_irq_entry(int apic, int pin, int type)
387 for (i = 0; i < mp_irq_entries; i++)
388 if (mp_irqs[i].mpc_irqtype == type &&
389 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
390 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
391 mp_irqs[i].mpc_dstirq == pin)
398 * Find the pin to which IRQ[irq] (ISA) is connected
400 static int __init find_isa_irq_pin(int irq, int type)
404 for (i = 0; i < mp_irq_entries; i++) {
405 int lbus = mp_irqs[i].mpc_srcbus;
407 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
408 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
409 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
410 (mp_irqs[i].mpc_irqtype == type) &&
411 (mp_irqs[i].mpc_srcbusirq == irq))
413 return mp_irqs[i].mpc_dstirq;
418 static int __init find_isa_irq_apic(int irq, int type)
422 for (i = 0; i < mp_irq_entries; i++) {
423 int lbus = mp_irqs[i].mpc_srcbus;
425 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
426 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
427 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
428 (mp_irqs[i].mpc_irqtype == type) &&
429 (mp_irqs[i].mpc_srcbusirq == irq))
432 if (i < mp_irq_entries) {
434 for(apic = 0; apic < nr_ioapics; apic++) {
435 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
444 * Find a specific PCI IRQ entry.
445 * Not an __init, possibly needed by modules
447 static int pin_2_irq(int idx, int apic, int pin);
449 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
451 int apic, i, best_guess = -1;
453 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
455 if (mp_bus_id_to_pci_bus[bus] == -1) {
456 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
459 for (i = 0; i < mp_irq_entries; i++) {
460 int lbus = mp_irqs[i].mpc_srcbus;
462 for (apic = 0; apic < nr_ioapics; apic++)
463 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
464 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
467 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
468 !mp_irqs[i].mpc_irqtype &&
470 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
471 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
473 if (!(apic || IO_APIC_IRQ(irq)))
476 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
479 * Use the first all-but-pin matching entry as a
480 * best-guess fuzzy result for broken mptables.
486 BUG_ON(best_guess >= NR_IRQS);
491 * EISA Edge/Level control register, ELCR
493 static int EISA_ELCR(unsigned int irq)
496 unsigned int port = 0x4d0 + (irq >> 3);
497 return (inb(port) >> (irq & 7)) & 1;
499 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
503 /* EISA interrupts are always polarity zero and can be edge or level
504 * trigger depending on the ELCR value. If an interrupt is listed as
505 * EISA conforming in the MP table, that means its trigger type must
506 * be read in from the ELCR */
508 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
509 #define default_EISA_polarity(idx) (0)
511 /* ISA interrupts are always polarity zero edge triggered,
512 * when listed as conforming in the MP table. */
514 #define default_ISA_trigger(idx) (0)
515 #define default_ISA_polarity(idx) (0)
517 /* PCI interrupts are always polarity one level triggered,
518 * when listed as conforming in the MP table. */
520 #define default_PCI_trigger(idx) (1)
521 #define default_PCI_polarity(idx) (1)
523 /* MCA interrupts are always polarity zero level triggered,
524 * when listed as conforming in the MP table. */
526 #define default_MCA_trigger(idx) (1)
527 #define default_MCA_polarity(idx) (0)
529 static int __init MPBIOS_polarity(int idx)
531 int bus = mp_irqs[idx].mpc_srcbus;
535 * Determine IRQ line polarity (high active or low active):
537 switch (mp_irqs[idx].mpc_irqflag & 3)
539 case 0: /* conforms, ie. bus-type dependent polarity */
541 switch (mp_bus_id_to_type[bus])
543 case MP_BUS_ISA: /* ISA pin */
545 polarity = default_ISA_polarity(idx);
548 case MP_BUS_EISA: /* EISA pin */
550 polarity = default_EISA_polarity(idx);
553 case MP_BUS_PCI: /* PCI pin */
555 polarity = default_PCI_polarity(idx);
558 case MP_BUS_MCA: /* MCA pin */
560 polarity = default_MCA_polarity(idx);
565 printk(KERN_WARNING "broken BIOS!!\n");
572 case 1: /* high active */
577 case 2: /* reserved */
579 printk(KERN_WARNING "broken BIOS!!\n");
583 case 3: /* low active */
588 default: /* invalid */
590 printk(KERN_WARNING "broken BIOS!!\n");
598 static int MPBIOS_trigger(int idx)
600 int bus = mp_irqs[idx].mpc_srcbus;
604 * Determine IRQ trigger mode (edge or level sensitive):
606 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
608 case 0: /* conforms, ie. bus-type dependent */
610 switch (mp_bus_id_to_type[bus])
612 case MP_BUS_ISA: /* ISA pin */
614 trigger = default_ISA_trigger(idx);
617 case MP_BUS_EISA: /* EISA pin */
619 trigger = default_EISA_trigger(idx);
622 case MP_BUS_PCI: /* PCI pin */
624 trigger = default_PCI_trigger(idx);
627 case MP_BUS_MCA: /* MCA pin */
629 trigger = default_MCA_trigger(idx);
634 printk(KERN_WARNING "broken BIOS!!\n");
646 case 2: /* reserved */
648 printk(KERN_WARNING "broken BIOS!!\n");
657 default: /* invalid */
659 printk(KERN_WARNING "broken BIOS!!\n");
667 static inline int irq_polarity(int idx)
669 return MPBIOS_polarity(idx);
672 static inline int irq_trigger(int idx)
674 return MPBIOS_trigger(idx);
677 static int next_irq = 16;
680 * gsi_irq_sharing -- Name overload! "irq" can be either a legacy IRQ
681 * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
682 * from ACPI, which can reach 800 in large boxen.
684 * Compact the sparse GSI space into a sequential IRQ series and reuse
685 * vectors if possible.
687 int gsi_irq_sharing(int gsi)
689 int i, tries, vector;
691 BUG_ON(gsi >= NR_IRQ_VECTORS);
693 if (platform_legacy_irq(gsi))
696 if (gsi_2_irq[gsi] != 0xFF)
697 return (int)gsi_2_irq[gsi];
701 vector = assign_irq_vector(gsi);
704 * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
705 * use of vector and if found, return that IRQ. However, we never want
706 * to share legacy IRQs, which usually have a different trigger mode
709 for (i = 0; i < NR_IRQS; i++)
710 if (IO_APIC_VECTOR(i) == vector)
712 if (platform_legacy_irq(i)) {
714 IO_APIC_VECTOR(i) = 0;
717 panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
721 printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
727 BUG_ON(i >= NR_IRQS);
729 IO_APIC_VECTOR(i) = vector;
730 printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
735 static int pin_2_irq(int idx, int apic, int pin)
738 int bus = mp_irqs[idx].mpc_srcbus;
741 * Debugging check, we are in big trouble if this message pops up!
743 if (mp_irqs[idx].mpc_dstirq != pin)
744 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
746 switch (mp_bus_id_to_type[bus])
748 case MP_BUS_ISA: /* ISA pin */
752 irq = mp_irqs[idx].mpc_srcbusirq;
755 case MP_BUS_PCI: /* PCI pin */
758 * PCI IRQs are mapped in order
762 irq += nr_ioapic_registers[i++];
764 irq = gsi_irq_sharing(irq);
769 printk(KERN_ERR "unknown bus type %d.\n",bus);
774 BUG_ON(irq >= NR_IRQS);
777 * PCI IRQ command line redirection. Yes, limits are hardcoded.
779 if ((pin >= 16) && (pin <= 23)) {
780 if (pirq_entries[pin-16] != -1) {
781 if (!pirq_entries[pin-16]) {
782 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
784 irq = pirq_entries[pin-16];
785 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
790 BUG_ON(irq >= NR_IRQS);
794 static inline int IO_APIC_irq_trigger(int irq)
798 for (apic = 0; apic < nr_ioapics; apic++) {
799 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
800 idx = find_irq_entry(apic,pin,mp_INT);
801 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
802 return irq_trigger(idx);
806 * nonexistent IRQs are edge default
811 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
812 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
814 int assign_irq_vector(int irq)
816 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
818 BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
819 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0)
820 return IO_APIC_VECTOR(irq);
823 if (current_vector == IA32_SYSCALL_VECTOR)
826 if (current_vector >= FIRST_SYSTEM_VECTOR) {
827 /* If we run out of vectors on large boxen, must share them. */
828 offset = (offset + 1) % 8;
829 current_vector = FIRST_DEVICE_VECTOR + offset;
832 vector_irq[current_vector] = irq;
833 if (irq != AUTO_ASSIGN)
834 IO_APIC_VECTOR(irq) = current_vector;
836 return current_vector;
839 extern void (*interrupt[NR_IRQS])(void);
840 static struct hw_interrupt_type ioapic_level_type;
841 static struct hw_interrupt_type ioapic_edge_type;
843 #define IOAPIC_AUTO -1
844 #define IOAPIC_EDGE 0
845 #define IOAPIC_LEVEL 1
847 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
849 if (use_pci_vector() && !platform_legacy_irq(irq)) {
850 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
851 trigger == IOAPIC_LEVEL)
852 irq_desc[vector].handler = &ioapic_level_type;
854 irq_desc[vector].handler = &ioapic_edge_type;
855 set_intr_gate(vector, interrupt[vector]);
857 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
858 trigger == IOAPIC_LEVEL)
859 irq_desc[irq].handler = &ioapic_level_type;
861 irq_desc[irq].handler = &ioapic_edge_type;
862 set_intr_gate(vector, interrupt[irq]);
866 static void __init setup_IO_APIC_irqs(void)
868 struct IO_APIC_route_entry entry;
869 int apic, pin, idx, irq, first_notcon = 1, vector;
872 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
874 for (apic = 0; apic < nr_ioapics; apic++) {
875 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
878 * add it to the IO-APIC irq-routing table:
880 memset(&entry,0,sizeof(entry));
882 entry.delivery_mode = INT_DELIVERY_MODE;
883 entry.dest_mode = INT_DEST_MODE;
884 entry.mask = 0; /* enable IRQ */
885 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
887 idx = find_irq_entry(apic,pin,mp_INT);
890 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
893 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
897 entry.trigger = irq_trigger(idx);
898 entry.polarity = irq_polarity(idx);
900 if (irq_trigger(idx)) {
903 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
906 irq = pin_2_irq(idx, apic, pin);
907 add_pin_to_irq(irq, apic, pin);
909 if (!apic && !IO_APIC_IRQ(irq))
912 if (IO_APIC_IRQ(irq)) {
913 vector = assign_irq_vector(irq);
914 entry.vector = vector;
916 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
917 if (!apic && (irq < 16))
918 disable_8259A_irq(irq);
920 spin_lock_irqsave(&ioapic_lock, flags);
921 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
922 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
923 set_native_irq_info(irq, TARGET_CPUS);
924 spin_unlock_irqrestore(&ioapic_lock, flags);
929 apic_printk(APIC_VERBOSE," not connected.\n");
933 * Set up the 8259A-master output pin as broadcast to all
936 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
938 struct IO_APIC_route_entry entry;
941 memset(&entry,0,sizeof(entry));
943 disable_8259A_irq(0);
946 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
949 * We use logical delivery to get the timer IRQ
952 entry.dest_mode = INT_DEST_MODE;
953 entry.mask = 0; /* unmask IRQ now */
954 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
955 entry.delivery_mode = INT_DELIVERY_MODE;
958 entry.vector = vector;
961 * The timer IRQ doesn't have to know that behind the
962 * scene we have a 8259A-master in AEOI mode ...
964 irq_desc[0].handler = &ioapic_edge_type;
967 * Add it to the IO-APIC irq-routing table:
969 spin_lock_irqsave(&ioapic_lock, flags);
970 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
971 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
972 spin_unlock_irqrestore(&ioapic_lock, flags);
977 void __init UNEXPECTED_IO_APIC(void)
981 void __apicdebuginit print_IO_APIC(void)
984 union IO_APIC_reg_00 reg_00;
985 union IO_APIC_reg_01 reg_01;
986 union IO_APIC_reg_02 reg_02;
989 if (apic_verbosity == APIC_QUIET)
992 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
993 for (i = 0; i < nr_ioapics; i++)
994 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
995 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
998 * We are a bit conservative about what we expect. We have to
999 * know about every hardware change ASAP.
1001 printk(KERN_INFO "testing the IO APIC.......................\n");
1003 for (apic = 0; apic < nr_ioapics; apic++) {
1005 spin_lock_irqsave(&ioapic_lock, flags);
1006 reg_00.raw = io_apic_read(apic, 0);
1007 reg_01.raw = io_apic_read(apic, 1);
1008 if (reg_01.bits.version >= 0x10)
1009 reg_02.raw = io_apic_read(apic, 2);
1010 spin_unlock_irqrestore(&ioapic_lock, flags);
1013 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1014 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1015 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1016 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
1017 UNEXPECTED_IO_APIC();
1019 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
1020 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1021 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
1022 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
1023 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
1024 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1025 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1026 (reg_01.bits.entries != 0x2E) &&
1027 (reg_01.bits.entries != 0x3F) &&
1028 (reg_01.bits.entries != 0x03)
1030 UNEXPECTED_IO_APIC();
1032 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1033 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1034 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1035 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
1036 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1037 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1038 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1039 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1041 UNEXPECTED_IO_APIC();
1042 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1043 UNEXPECTED_IO_APIC();
1045 if (reg_01.bits.version >= 0x10) {
1046 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1047 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1048 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1049 UNEXPECTED_IO_APIC();
1052 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1054 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1055 " Stat Dest Deli Vect: \n");
1057 for (i = 0; i <= reg_01.bits.entries; i++) {
1058 struct IO_APIC_route_entry entry;
1060 spin_lock_irqsave(&ioapic_lock, flags);
1061 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
1062 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
1063 spin_unlock_irqrestore(&ioapic_lock, flags);
1065 printk(KERN_DEBUG " %02x %03X %02X ",
1067 entry.dest.logical.logical_dest,
1068 entry.dest.physical.physical_dest
1071 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1076 entry.delivery_status,
1078 entry.delivery_mode,
1083 if (use_pci_vector())
1084 printk(KERN_INFO "Using vector-based indexing\n");
1085 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1086 for (i = 0; i < NR_IRQS; i++) {
1087 struct irq_pin_list *entry = irq_2_pin + i;
1090 if (use_pci_vector() && !platform_legacy_irq(i))
1091 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1093 printk(KERN_DEBUG "IRQ%d ", i);
1095 printk("-> %d:%d", entry->apic, entry->pin);
1098 entry = irq_2_pin + entry->next;
1103 printk(KERN_INFO ".................................... done.\n");
1110 static __apicdebuginit void print_APIC_bitfield (int base)
1115 if (apic_verbosity == APIC_QUIET)
1118 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1119 for (i = 0; i < 8; i++) {
1120 v = apic_read(base + i*0x10);
1121 for (j = 0; j < 32; j++) {
1131 void __apicdebuginit print_local_APIC(void * dummy)
1133 unsigned int v, ver, maxlvt;
1135 if (apic_verbosity == APIC_QUIET)
1138 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1139 smp_processor_id(), hard_smp_processor_id());
1140 v = apic_read(APIC_ID);
1141 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1142 v = apic_read(APIC_LVR);
1143 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1144 ver = GET_APIC_VERSION(v);
1145 maxlvt = get_maxlvt();
1147 v = apic_read(APIC_TASKPRI);
1148 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1150 v = apic_read(APIC_ARBPRI);
1151 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1152 v & APIC_ARBPRI_MASK);
1153 v = apic_read(APIC_PROCPRI);
1154 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1156 v = apic_read(APIC_EOI);
1157 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1158 v = apic_read(APIC_RRR);
1159 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1160 v = apic_read(APIC_LDR);
1161 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1162 v = apic_read(APIC_DFR);
1163 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1164 v = apic_read(APIC_SPIV);
1165 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1167 printk(KERN_DEBUG "... APIC ISR field:\n");
1168 print_APIC_bitfield(APIC_ISR);
1169 printk(KERN_DEBUG "... APIC TMR field:\n");
1170 print_APIC_bitfield(APIC_TMR);
1171 printk(KERN_DEBUG "... APIC IRR field:\n");
1172 print_APIC_bitfield(APIC_IRR);
1174 v = apic_read(APIC_ESR);
1175 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1177 v = apic_read(APIC_ICR);
1178 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1179 v = apic_read(APIC_ICR2);
1180 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1182 v = apic_read(APIC_LVTT);
1183 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1185 if (maxlvt > 3) { /* PC is LVT#4. */
1186 v = apic_read(APIC_LVTPC);
1187 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1189 v = apic_read(APIC_LVT0);
1190 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1191 v = apic_read(APIC_LVT1);
1192 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1194 if (maxlvt > 2) { /* ERR is LVT#3. */
1195 v = apic_read(APIC_LVTERR);
1196 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1199 v = apic_read(APIC_TMICT);
1200 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1201 v = apic_read(APIC_TMCCT);
1202 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1203 v = apic_read(APIC_TDCR);
1204 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1208 void print_all_local_APICs (void)
1210 on_each_cpu(print_local_APIC, NULL, 1, 1);
1213 void __apicdebuginit print_PIC(void)
1216 unsigned long flags;
1218 if (apic_verbosity == APIC_QUIET)
1221 printk(KERN_DEBUG "\nprinting PIC contents\n");
1223 spin_lock_irqsave(&i8259A_lock, flags);
1225 v = inb(0xa1) << 8 | inb(0x21);
1226 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1228 v = inb(0xa0) << 8 | inb(0x20);
1229 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1233 v = inb(0xa0) << 8 | inb(0x20);
1237 spin_unlock_irqrestore(&i8259A_lock, flags);
1239 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1241 v = inb(0x4d1) << 8 | inb(0x4d0);
1242 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1247 static void __init enable_IO_APIC(void)
1249 union IO_APIC_reg_01 reg_01;
1250 int i8259_apic, i8259_pin;
1252 unsigned long flags;
1254 for (i = 0; i < PIN_MAP_SIZE; i++) {
1255 irq_2_pin[i].pin = -1;
1256 irq_2_pin[i].next = 0;
1259 for (i = 0; i < MAX_PIRQS; i++)
1260 pirq_entries[i] = -1;
1263 * The number of IO-APIC IRQ registers (== #pins):
1265 for (apic = 0; apic < nr_ioapics; apic++) {
1266 spin_lock_irqsave(&ioapic_lock, flags);
1267 reg_01.raw = io_apic_read(apic, 1);
1268 spin_unlock_irqrestore(&ioapic_lock, flags);
1269 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1271 for(apic = 0; apic < nr_ioapics; apic++) {
1273 /* See if any of the pins is in ExtINT mode */
1274 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1275 struct IO_APIC_route_entry entry;
1276 spin_lock_irqsave(&ioapic_lock, flags);
1277 *(((int *)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1278 *(((int *)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1279 spin_unlock_irqrestore(&ioapic_lock, flags);
1282 /* If the interrupt line is enabled and in ExtInt mode
1283 * I have found the pin where the i8259 is connected.
1285 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1286 ioapic_i8259.apic = apic;
1287 ioapic_i8259.pin = pin;
1293 /* Look to see what if the MP table has reported the ExtINT */
1294 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1295 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1296 /* Trust the MP table if nothing is setup in the hardware */
1297 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1298 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1299 ioapic_i8259.pin = i8259_pin;
1300 ioapic_i8259.apic = i8259_apic;
1302 /* Complain if the MP table and the hardware disagree */
1303 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1304 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1306 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1310 * Do not trust the IO-APIC being empty at bootup
1316 * Not an __init, needed by the reboot code
1318 void disable_IO_APIC(void)
1321 * Clear the IO-APIC before rebooting:
1326 * If the i8259 is routed through an IOAPIC
1327 * Put that IOAPIC in virtual wire mode
1328 * so legacy interrupts can be delivered.
1330 if (ioapic_i8259.pin != -1) {
1331 struct IO_APIC_route_entry entry;
1332 unsigned long flags;
1334 memset(&entry, 0, sizeof(entry));
1335 entry.mask = 0; /* Enabled */
1336 entry.trigger = 0; /* Edge */
1338 entry.polarity = 0; /* High */
1339 entry.delivery_status = 0;
1340 entry.dest_mode = 0; /* Physical */
1341 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1343 entry.dest.physical.physical_dest =
1344 GET_APIC_ID(apic_read(APIC_ID));
1347 * Add it to the IO-APIC irq-routing table:
1349 spin_lock_irqsave(&ioapic_lock, flags);
1350 io_apic_write(ioapic_i8259.apic, 0x11+2*ioapic_i8259.pin,
1351 *(((int *)&entry)+1));
1352 io_apic_write(ioapic_i8259.apic, 0x10+2*ioapic_i8259.pin,
1353 *(((int *)&entry)+0));
1354 spin_unlock_irqrestore(&ioapic_lock, flags);
1357 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1361 * function to set the IO-APIC physical IDs based on the
1362 * values stored in the MPC table.
1364 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1367 static void __init setup_ioapic_ids_from_mpc (void)
1369 union IO_APIC_reg_00 reg_00;
1372 unsigned char old_id;
1373 unsigned long flags;
1376 * Set the IOAPIC ID to the value stored in the MPC table.
1378 for (apic = 0; apic < nr_ioapics; apic++) {
1380 /* Read the register 0 value */
1381 spin_lock_irqsave(&ioapic_lock, flags);
1382 reg_00.raw = io_apic_read(apic, 0);
1383 spin_unlock_irqrestore(&ioapic_lock, flags);
1385 old_id = mp_ioapics[apic].mpc_apicid;
1388 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1392 * We need to adjust the IRQ routing table
1393 * if the ID changed.
1395 if (old_id != mp_ioapics[apic].mpc_apicid)
1396 for (i = 0; i < mp_irq_entries; i++)
1397 if (mp_irqs[i].mpc_dstapic == old_id)
1398 mp_irqs[i].mpc_dstapic
1399 = mp_ioapics[apic].mpc_apicid;
1402 * Read the right value from the MPC table and
1403 * write it into the ID register.
1405 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1406 mp_ioapics[apic].mpc_apicid);
1408 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1409 spin_lock_irqsave(&ioapic_lock, flags);
1410 io_apic_write(apic, 0, reg_00.raw);
1411 spin_unlock_irqrestore(&ioapic_lock, flags);
1416 spin_lock_irqsave(&ioapic_lock, flags);
1417 reg_00.raw = io_apic_read(apic, 0);
1418 spin_unlock_irqrestore(&ioapic_lock, flags);
1419 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1420 printk("could not set ID!\n");
1422 apic_printk(APIC_VERBOSE," ok.\n");
1427 * There is a nasty bug in some older SMP boards, their mptable lies
1428 * about the timer IRQ. We do the following to work around the situation:
1430 * - timer IRQ defaults to IO-APIC IRQ
1431 * - if this function detects that timer IRQs are defunct, then we fall
1432 * back to ISA timer IRQs
1434 static int __init timer_irq_works(void)
1436 unsigned long t1 = jiffies;
1439 /* Let ten ticks pass... */
1440 mdelay((10 * 1000) / HZ);
1443 * Expect a few ticks at least, to be sure some possible
1444 * glue logic does not lock up after one or two first
1445 * ticks in a non-ExtINT mode. Also the local APIC
1446 * might have cached one ExtINT interrupt. Finally, at
1447 * least one tick may be lost due to delays.
1451 if (jiffies - t1 > 4)
1457 * In the SMP+IOAPIC case it might happen that there are an unspecified
1458 * number of pending IRQ events unhandled. These cases are very rare,
1459 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1460 * better to do it this way as thus we do not have to be aware of
1461 * 'pending' interrupts in the IRQ path, except at this point.
1464 * Edge triggered needs to resend any interrupt
1465 * that was delayed but this is now handled in the device
1470 * Starting up a edge-triggered IO-APIC interrupt is
1471 * nasty - we need to make sure that we get the edge.
1472 * If it is already asserted for some reason, we need
1473 * return 1 to indicate that is was pending.
1475 * This is not complete - we should be able to fake
1476 * an edge even if it isn't on the 8259A...
1479 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1481 int was_pending = 0;
1482 unsigned long flags;
1484 spin_lock_irqsave(&ioapic_lock, flags);
1486 disable_8259A_irq(irq);
1487 if (i8259A_irq_pending(irq))
1490 __unmask_IO_APIC_irq(irq);
1491 spin_unlock_irqrestore(&ioapic_lock, flags);
1497 * Once we have recorded IRQ_PENDING already, we can mask the
1498 * interrupt for real. This prevents IRQ storms from unhandled
1501 static void ack_edge_ioapic_irq(unsigned int irq)
1504 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1505 == (IRQ_PENDING | IRQ_DISABLED))
1506 mask_IO_APIC_irq(irq);
1511 * Level triggered interrupts can just be masked,
1512 * and shutting down and starting up the interrupt
1513 * is the same as enabling and disabling them -- except
1514 * with a startup need to return a "was pending" value.
1516 * Level triggered interrupts are special because we
1517 * do not touch any IO-APIC register while handling
1518 * them. We ack the APIC in the end-IRQ handler, not
1519 * in the start-IRQ-handler. Protection against reentrance
1520 * from the same interrupt is still provided, both by the
1521 * generic IRQ layer and by the fact that an unacked local
1522 * APIC does not accept IRQs.
1524 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1526 unmask_IO_APIC_irq(irq);
1528 return 0; /* don't check for pending */
1531 static void end_level_ioapic_irq (unsigned int irq)
1537 #ifdef CONFIG_PCI_MSI
1538 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1540 int irq = vector_to_irq(vector);
1542 return startup_edge_ioapic_irq(irq);
1545 static void ack_edge_ioapic_vector(unsigned int vector)
1547 int irq = vector_to_irq(vector);
1549 move_native_irq(vector);
1550 ack_edge_ioapic_irq(irq);
1553 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1555 int irq = vector_to_irq(vector);
1557 return startup_level_ioapic_irq (irq);
1560 static void end_level_ioapic_vector (unsigned int vector)
1562 int irq = vector_to_irq(vector);
1564 move_native_irq(vector);
1565 end_level_ioapic_irq(irq);
1568 static void mask_IO_APIC_vector (unsigned int vector)
1570 int irq = vector_to_irq(vector);
1572 mask_IO_APIC_irq(irq);
1575 static void unmask_IO_APIC_vector (unsigned int vector)
1577 int irq = vector_to_irq(vector);
1579 unmask_IO_APIC_irq(irq);
1583 static void set_ioapic_affinity_vector (unsigned int vector,
1586 int irq = vector_to_irq(vector);
1588 set_native_irq_info(vector, cpu_mask);
1589 set_ioapic_affinity_irq(irq, cpu_mask);
1591 #endif // CONFIG_SMP
1592 #endif // CONFIG_PCI_MSI
1595 * Level and edge triggered IO-APIC interrupts need different handling,
1596 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1597 * handled with the level-triggered descriptor, but that one has slightly
1598 * more overhead. Level-triggered interrupts cannot be handled with the
1599 * edge-triggered handler, without risking IRQ storms and other ugly
1603 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1604 .typename = "IO-APIC-edge",
1605 .startup = startup_edge_ioapic,
1606 .shutdown = shutdown_edge_ioapic,
1607 .enable = enable_edge_ioapic,
1608 .disable = disable_edge_ioapic,
1609 .ack = ack_edge_ioapic,
1610 .end = end_edge_ioapic,
1612 .set_affinity = set_ioapic_affinity,
1616 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1617 .typename = "IO-APIC-level",
1618 .startup = startup_level_ioapic,
1619 .shutdown = shutdown_level_ioapic,
1620 .enable = enable_level_ioapic,
1621 .disable = disable_level_ioapic,
1622 .ack = mask_and_ack_level_ioapic,
1623 .end = end_level_ioapic,
1625 .set_affinity = set_ioapic_affinity,
1629 static inline void init_IO_APIC_traps(void)
1634 * NOTE! The local APIC isn't very good at handling
1635 * multiple interrupts at the same interrupt level.
1636 * As the interrupt level is determined by taking the
1637 * vector number and shifting that right by 4, we
1638 * want to spread these out a bit so that they don't
1639 * all fall in the same interrupt level.
1641 * Also, we've got to be careful not to trash gate
1642 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1644 for (irq = 0; irq < NR_IRQS ; irq++) {
1646 if (use_pci_vector()) {
1647 if (!platform_legacy_irq(tmp))
1648 if ((tmp = vector_to_irq(tmp)) == -1)
1651 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1653 * Hmm.. We don't have an entry for this,
1654 * so default to an old-fashioned 8259
1655 * interrupt if we can..
1658 make_8259A_irq(irq);
1660 /* Strange. Oh, well.. */
1661 irq_desc[irq].handler = &no_irq_type;
1666 static void enable_lapic_irq (unsigned int irq)
1670 v = apic_read(APIC_LVT0);
1671 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1674 static void disable_lapic_irq (unsigned int irq)
1678 v = apic_read(APIC_LVT0);
1679 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1682 static void ack_lapic_irq (unsigned int irq)
1687 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1689 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1690 .typename = "local-APIC-edge",
1691 .startup = NULL, /* startup_irq() not used for IRQ0 */
1692 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1693 .enable = enable_lapic_irq,
1694 .disable = disable_lapic_irq,
1695 .ack = ack_lapic_irq,
1696 .end = end_lapic_irq,
1699 static void setup_nmi (void)
1702 * Dirty trick to enable the NMI watchdog ...
1703 * We put the 8259A master into AEOI mode and
1704 * unmask on all local APICs LVT0 as NMI.
1706 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1707 * is from Maciej W. Rozycki - so we do not have to EOI from
1708 * the NMI handler or the timer interrupt.
1710 printk(KERN_INFO "activating NMI Watchdog ...");
1712 enable_NMI_through_LVT0(NULL);
1718 * This looks a bit hackish but it's about the only one way of sending
1719 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1720 * not support the ExtINT mode, unfortunately. We need to send these
1721 * cycles as some i82489DX-based boards have glue logic that keeps the
1722 * 8259A interrupt line asserted until INTA. --macro
1724 static inline void unlock_ExtINT_logic(void)
1727 struct IO_APIC_route_entry entry0, entry1;
1728 unsigned char save_control, save_freq_select;
1729 unsigned long flags;
1731 pin = find_isa_irq_pin(8, mp_INT);
1732 apic = find_isa_irq_apic(8, mp_INT);
1736 spin_lock_irqsave(&ioapic_lock, flags);
1737 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1738 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1739 spin_unlock_irqrestore(&ioapic_lock, flags);
1740 clear_IO_APIC_pin(apic, pin);
1742 memset(&entry1, 0, sizeof(entry1));
1744 entry1.dest_mode = 0; /* physical delivery */
1745 entry1.mask = 0; /* unmask IRQ now */
1746 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1747 entry1.delivery_mode = dest_ExtINT;
1748 entry1.polarity = entry0.polarity;
1752 spin_lock_irqsave(&ioapic_lock, flags);
1753 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1754 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1755 spin_unlock_irqrestore(&ioapic_lock, flags);
1757 save_control = CMOS_READ(RTC_CONTROL);
1758 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1759 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1761 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1766 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1770 CMOS_WRITE(save_control, RTC_CONTROL);
1771 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1772 clear_IO_APIC_pin(apic, pin);
1774 spin_lock_irqsave(&ioapic_lock, flags);
1775 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1776 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1777 spin_unlock_irqrestore(&ioapic_lock, flags);
1781 * This code may look a bit paranoid, but it's supposed to cooperate with
1782 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1783 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1784 * fanatically on his truly buggy board.
1786 * FIXME: really need to revamp this for modern platforms only.
1788 static inline void check_timer(void)
1790 int apic1, pin1, apic2, pin2;
1794 * get/set the timer IRQ vector:
1796 disable_8259A_irq(0);
1797 vector = assign_irq_vector(0);
1798 set_intr_gate(vector, interrupt[0]);
1801 * Subtle, code in do_timer_interrupt() expects an AEOI
1802 * mode for the 8259A whenever interrupts are routed
1803 * through I/O APICs. Also IRQ0 has to be enabled in
1804 * the 8259A which implies the virtual wire has to be
1805 * disabled in the local APIC.
1807 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1809 if (timer_over_8254 > 0)
1810 enable_8259A_irq(0);
1812 pin1 = find_isa_irq_pin(0, mp_INT);
1813 apic1 = find_isa_irq_apic(0, mp_INT);
1814 pin2 = ioapic_i8259.pin;
1815 apic2 = ioapic_i8259.apic;
1817 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1818 vector, apic1, pin1, apic2, pin2);
1822 * Ok, does IRQ0 through the IOAPIC work?
1824 unmask_IO_APIC_irq(0);
1825 if (!no_timer_check && timer_irq_works()) {
1826 nmi_watchdog_default();
1827 if (nmi_watchdog == NMI_IO_APIC) {
1828 disable_8259A_irq(0);
1830 enable_8259A_irq(0);
1832 if (disable_timer_pin_1 > 0)
1833 clear_IO_APIC_pin(0, pin1);
1836 clear_IO_APIC_pin(apic1, pin1);
1837 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1838 "connected to IO-APIC\n");
1841 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1842 "through the 8259A ... ");
1844 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1847 * legacy devices should be connected to IO APIC #0
1849 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1850 if (timer_irq_works()) {
1851 apic_printk(APIC_VERBOSE," works.\n");
1852 nmi_watchdog_default();
1853 if (nmi_watchdog == NMI_IO_APIC) {
1859 * Cleanup, just in case ...
1861 clear_IO_APIC_pin(apic2, pin2);
1863 apic_printk(APIC_VERBOSE," failed.\n");
1865 if (nmi_watchdog == NMI_IO_APIC) {
1866 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1870 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1872 disable_8259A_irq(0);
1873 irq_desc[0].handler = &lapic_irq_type;
1874 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1875 enable_8259A_irq(0);
1877 if (timer_irq_works()) {
1878 apic_printk(APIC_VERBOSE," works.\n");
1881 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1882 apic_printk(APIC_VERBOSE," failed.\n");
1884 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1888 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1890 unlock_ExtINT_logic();
1892 if (timer_irq_works()) {
1893 apic_printk(APIC_VERBOSE," works.\n");
1896 apic_printk(APIC_VERBOSE," failed :(.\n");
1897 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1900 static int __init notimercheck(char *s)
1905 __setup("no_timer_check", notimercheck);
1909 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1910 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1911 * Linux doesn't really care, as it's not actually used
1912 * for any interrupt handling anyway.
1914 #define PIC_IRQS (1<<2)
1916 void __init setup_IO_APIC(void)
1921 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1923 io_apic_irqs = ~PIC_IRQS;
1925 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1928 * Set up the IO-APIC IRQ routing table.
1931 setup_ioapic_ids_from_mpc();
1933 setup_IO_APIC_irqs();
1934 init_IO_APIC_traps();
1940 struct sysfs_ioapic_data {
1941 struct sys_device dev;
1942 struct IO_APIC_route_entry entry[0];
1944 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1946 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1948 struct IO_APIC_route_entry *entry;
1949 struct sysfs_ioapic_data *data;
1950 unsigned long flags;
1953 data = container_of(dev, struct sysfs_ioapic_data, dev);
1954 entry = data->entry;
1955 spin_lock_irqsave(&ioapic_lock, flags);
1956 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1957 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1958 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1960 spin_unlock_irqrestore(&ioapic_lock, flags);
1965 static int ioapic_resume(struct sys_device *dev)
1967 struct IO_APIC_route_entry *entry;
1968 struct sysfs_ioapic_data *data;
1969 unsigned long flags;
1970 union IO_APIC_reg_00 reg_00;
1973 data = container_of(dev, struct sysfs_ioapic_data, dev);
1974 entry = data->entry;
1976 spin_lock_irqsave(&ioapic_lock, flags);
1977 reg_00.raw = io_apic_read(dev->id, 0);
1978 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1979 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1980 io_apic_write(dev->id, 0, reg_00.raw);
1982 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1983 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1984 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1986 spin_unlock_irqrestore(&ioapic_lock, flags);
1991 static struct sysdev_class ioapic_sysdev_class = {
1992 set_kset_name("ioapic"),
1993 .suspend = ioapic_suspend,
1994 .resume = ioapic_resume,
1997 static int __init ioapic_init_sysfs(void)
1999 struct sys_device * dev;
2000 int i, size, error = 0;
2002 error = sysdev_class_register(&ioapic_sysdev_class);
2006 for (i = 0; i < nr_ioapics; i++ ) {
2007 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2008 * sizeof(struct IO_APIC_route_entry);
2009 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
2010 if (!mp_ioapic_data[i]) {
2011 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2014 memset(mp_ioapic_data[i], 0, size);
2015 dev = &mp_ioapic_data[i]->dev;
2017 dev->cls = &ioapic_sysdev_class;
2018 error = sysdev_register(dev);
2020 kfree(mp_ioapic_data[i]);
2021 mp_ioapic_data[i] = NULL;
2022 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2030 device_initcall(ioapic_init_sysfs);
2032 /* --------------------------------------------------------------------------
2033 ACPI-based IOAPIC Configuration
2034 -------------------------------------------------------------------------- */
2038 #define IO_APIC_MAX_ID 0xFE
2040 int __init io_apic_get_version (int ioapic)
2042 union IO_APIC_reg_01 reg_01;
2043 unsigned long flags;
2045 spin_lock_irqsave(&ioapic_lock, flags);
2046 reg_01.raw = io_apic_read(ioapic, 1);
2047 spin_unlock_irqrestore(&ioapic_lock, flags);
2049 return reg_01.bits.version;
2053 int __init io_apic_get_redir_entries (int ioapic)
2055 union IO_APIC_reg_01 reg_01;
2056 unsigned long flags;
2058 spin_lock_irqsave(&ioapic_lock, flags);
2059 reg_01.raw = io_apic_read(ioapic, 1);
2060 spin_unlock_irqrestore(&ioapic_lock, flags);
2062 return reg_01.bits.entries;
2066 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2068 struct IO_APIC_route_entry entry;
2069 unsigned long flags;
2071 if (!IO_APIC_IRQ(irq)) {
2072 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2078 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2079 * Note that we mask (disable) IRQs now -- these get enabled when the
2080 * corresponding device driver registers for this IRQ.
2083 memset(&entry,0,sizeof(entry));
2085 entry.delivery_mode = INT_DELIVERY_MODE;
2086 entry.dest_mode = INT_DEST_MODE;
2087 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2088 entry.trigger = triggering;
2089 entry.polarity = polarity;
2090 entry.mask = 1; /* Disabled (masked) */
2092 irq = gsi_irq_sharing(irq);
2094 * IRQs < 16 are already in the irq_2_pin[] map
2097 add_pin_to_irq(irq, ioapic, pin);
2099 entry.vector = assign_irq_vector(irq);
2101 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2102 "IRQ %d Mode:%i Active:%i)\n", ioapic,
2103 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2104 triggering, polarity);
2106 ioapic_register_intr(irq, entry.vector, triggering);
2108 if (!ioapic && (irq < 16))
2109 disable_8259A_irq(irq);
2111 spin_lock_irqsave(&ioapic_lock, flags);
2112 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2113 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2114 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
2115 spin_unlock_irqrestore(&ioapic_lock, flags);
2120 #endif /* CONFIG_ACPI */
2124 * This function currently is only a helper for the i386 smp boot process where
2125 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2126 * so mask in all cases should simply be TARGET_CPUS
2129 void __init setup_ioapic_dest(void)
2131 int pin, ioapic, irq, irq_entry;
2133 if (skip_ioapic_setup == 1)
2136 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2137 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2138 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2139 if (irq_entry == -1)
2141 irq = pin_2_irq(irq_entry, ioapic, pin);
2142 set_ioapic_affinity_irq(irq, TARGET_CPUS);