2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/config.h>
29 #include <linux/smp_lock.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
37 #include <asm/proto.h>
38 #include <asm/mach_apic.h>
42 #define __apicdebuginit __init
44 int sis_apic_bug; /* not actually supported, dummy for compile */
46 static int no_timer_check;
48 int disable_timer_pin_1 __initdata;
50 /* Where if anywhere is the i8259 connect in external int mode */
51 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
53 static DEFINE_SPINLOCK(ioapic_lock);
56 * # of IRQ routing registers
58 int nr_ioapic_registers[MAX_IO_APICS];
61 * Rough estimation of how many shared IRQs there are, can
64 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
65 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
68 * This is performance-critical, we want to do it O(1)
70 * the indexing order of this array favors 1:1 mappings
71 * between pins and IRQs.
74 static struct irq_pin_list {
75 short apic, pin, next;
76 } irq_2_pin[PIN_MAP_SIZE];
78 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
80 #define vector_to_irq(vector) \
81 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
83 #define vector_to_irq(vector) (vector)
86 #define __DO_ACTION(R, ACTION, FINAL) \
90 struct irq_pin_list *entry = irq_2_pin + irq; \
92 BUG_ON(irq >= NR_IRQS); \
98 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
100 io_apic_modify(entry->apic, reg); \
103 entry = irq_2_pin + entry->next; \
109 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
115 cpus_and(tmp, mask, cpu_online_map);
119 cpus_and(mask, tmp, CPU_MASK_ALL);
121 dest = cpu_mask_to_apicid(mask);
124 * Only the high 8 bits are valid.
126 dest = SET_APIC_LOGICAL_ID(dest);
128 spin_lock_irqsave(&ioapic_lock, flags);
129 __DO_ACTION(1, = dest, )
130 set_irq_info(irq, mask);
131 spin_unlock_irqrestore(&ioapic_lock, flags);
135 static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
138 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
139 * shared ISA-space IRQs, so we have to support them. We are super
140 * fast in the common case, and fast for shared ISA-space IRQs.
142 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
144 static int first_free_entry = NR_IRQS;
145 struct irq_pin_list *entry = irq_2_pin + irq;
147 BUG_ON(irq >= NR_IRQS);
149 entry = irq_2_pin + entry->next;
151 if (entry->pin != -1) {
152 entry->next = first_free_entry;
153 entry = irq_2_pin + entry->next;
154 if (++first_free_entry >= PIN_MAP_SIZE)
155 panic("io_apic.c: ran out of irq_2_pin entries!");
162 #define DO_ACTION(name,R,ACTION, FINAL) \
164 static void name##_IO_APIC_irq (unsigned int irq) \
165 __DO_ACTION(R, ACTION, FINAL)
167 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
169 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
172 static void mask_IO_APIC_irq (unsigned int irq)
176 spin_lock_irqsave(&ioapic_lock, flags);
177 __mask_IO_APIC_irq(irq);
178 spin_unlock_irqrestore(&ioapic_lock, flags);
181 static void unmask_IO_APIC_irq (unsigned int irq)
185 spin_lock_irqsave(&ioapic_lock, flags);
186 __unmask_IO_APIC_irq(irq);
187 spin_unlock_irqrestore(&ioapic_lock, flags);
190 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
192 struct IO_APIC_route_entry entry;
195 /* Check delivery_mode to be sure we're not clearing an SMI pin */
196 spin_lock_irqsave(&ioapic_lock, flags);
197 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
198 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
199 spin_unlock_irqrestore(&ioapic_lock, flags);
200 if (entry.delivery_mode == dest_SMI)
203 * Disable it in the IO-APIC irq-routing table:
205 memset(&entry, 0, sizeof(entry));
207 spin_lock_irqsave(&ioapic_lock, flags);
208 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
209 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
210 spin_unlock_irqrestore(&ioapic_lock, flags);
213 static void clear_IO_APIC (void)
217 for (apic = 0; apic < nr_ioapics; apic++)
218 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
219 clear_IO_APIC_pin(apic, pin);
223 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
224 * specific CPU-side IRQs.
228 static int pirq_entries [MAX_PIRQS];
229 static int pirqs_enabled;
230 int skip_ioapic_setup;
233 /* dummy parsing: see setup.c */
235 static int __init disable_ioapic_setup(char *str)
237 skip_ioapic_setup = 1;
241 static int __init enable_ioapic_setup(char *str)
244 skip_ioapic_setup = 0;
248 __setup("noapic", disable_ioapic_setup);
249 __setup("apic", enable_ioapic_setup);
251 #include <asm/pci-direct.h>
252 #include <linux/pci_ids.h>
253 #include <linux/pci.h>
255 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
256 off. Check for an Nvidia or VIA PCI bridge and turn it off.
257 Use pci direct infrastructure because this runs before the PCI subsystem.
259 Can be overwritten with "apic"
261 And another hack to disable the IOMMU on VIA chipsets.
264 void __init check_ioapic(void)
267 /* Poor man's PCI discovery */
268 for (num = 0; num < 32; num++) {
269 for (slot = 0; slot < 32; slot++) {
270 for (func = 0; func < 8; func++) {
274 class = read_pci_config(num,slot,func,
276 if (class == 0xffffffff)
279 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
282 vendor = read_pci_config(num, slot, func,
286 case PCI_VENDOR_ID_VIA:
287 #ifdef CONFIG_GART_IOMMU
288 if ((end_pfn > MAX_DMA32_PFN ||
290 !iommu_aperture_allowed) {
292 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
293 iommu_aperture_disabled = 1;
297 case PCI_VENDOR_ID_NVIDIA:
299 /* All timer overrides on Nvidia
300 seem to be wrong. Skip them. */
301 acpi_skip_timer_override = 1;
303 "Nvidia board detected. Ignoring ACPI timer override.\n");
305 /* RED-PEN skip them on mptables too? */
307 case PCI_VENDOR_ID_ATI:
308 if (apic_runs_main_timer != 0)
311 "ATI board detected. Using APIC/PM timer.\n");
312 apic_runs_main_timer = 1;
317 /* No multi-function device? */
318 type = read_pci_config_byte(num,slot,func,
327 static int __init ioapic_pirq_setup(char *str)
330 int ints[MAX_PIRQS+1];
332 get_options(str, ARRAY_SIZE(ints), ints);
334 for (i = 0; i < MAX_PIRQS; i++)
335 pirq_entries[i] = -1;
338 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
340 if (ints[0] < MAX_PIRQS)
343 for (i = 0; i < max; i++) {
344 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
346 * PIRQs are mapped upside down, usually.
348 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
353 __setup("pirq=", ioapic_pirq_setup);
356 * Find the IRQ entry number of a certain pin.
358 static int find_irq_entry(int apic, int pin, int type)
362 for (i = 0; i < mp_irq_entries; i++)
363 if (mp_irqs[i].mpc_irqtype == type &&
364 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
365 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
366 mp_irqs[i].mpc_dstirq == pin)
373 * Find the pin to which IRQ[irq] (ISA) is connected
375 static int __init find_isa_irq_pin(int irq, int type)
379 for (i = 0; i < mp_irq_entries; i++) {
380 int lbus = mp_irqs[i].mpc_srcbus;
382 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
383 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
384 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
385 (mp_irqs[i].mpc_irqtype == type) &&
386 (mp_irqs[i].mpc_srcbusirq == irq))
388 return mp_irqs[i].mpc_dstirq;
393 static int __init find_isa_irq_apic(int irq, int type)
397 for (i = 0; i < mp_irq_entries; i++) {
398 int lbus = mp_irqs[i].mpc_srcbus;
400 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
401 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
402 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
403 (mp_irqs[i].mpc_irqtype == type) &&
404 (mp_irqs[i].mpc_srcbusirq == irq))
407 if (i < mp_irq_entries) {
409 for(apic = 0; apic < nr_ioapics; apic++) {
410 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
419 * Find a specific PCI IRQ entry.
420 * Not an __init, possibly needed by modules
422 static int pin_2_irq(int idx, int apic, int pin);
424 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
426 int apic, i, best_guess = -1;
428 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
430 if (mp_bus_id_to_pci_bus[bus] == -1) {
431 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
434 for (i = 0; i < mp_irq_entries; i++) {
435 int lbus = mp_irqs[i].mpc_srcbus;
437 for (apic = 0; apic < nr_ioapics; apic++)
438 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
439 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
442 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
443 !mp_irqs[i].mpc_irqtype &&
445 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
446 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
448 if (!(apic || IO_APIC_IRQ(irq)))
451 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
454 * Use the first all-but-pin matching entry as a
455 * best-guess fuzzy result for broken mptables.
461 BUG_ON(best_guess >= NR_IRQS);
466 * EISA Edge/Level control register, ELCR
468 static int EISA_ELCR(unsigned int irq)
471 unsigned int port = 0x4d0 + (irq >> 3);
472 return (inb(port) >> (irq & 7)) & 1;
474 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
478 /* EISA interrupts are always polarity zero and can be edge or level
479 * trigger depending on the ELCR value. If an interrupt is listed as
480 * EISA conforming in the MP table, that means its trigger type must
481 * be read in from the ELCR */
483 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
484 #define default_EISA_polarity(idx) (0)
486 /* ISA interrupts are always polarity zero edge triggered,
487 * when listed as conforming in the MP table. */
489 #define default_ISA_trigger(idx) (0)
490 #define default_ISA_polarity(idx) (0)
492 /* PCI interrupts are always polarity one level triggered,
493 * when listed as conforming in the MP table. */
495 #define default_PCI_trigger(idx) (1)
496 #define default_PCI_polarity(idx) (1)
498 /* MCA interrupts are always polarity zero level triggered,
499 * when listed as conforming in the MP table. */
501 #define default_MCA_trigger(idx) (1)
502 #define default_MCA_polarity(idx) (0)
504 static int __init MPBIOS_polarity(int idx)
506 int bus = mp_irqs[idx].mpc_srcbus;
510 * Determine IRQ line polarity (high active or low active):
512 switch (mp_irqs[idx].mpc_irqflag & 3)
514 case 0: /* conforms, ie. bus-type dependent polarity */
516 switch (mp_bus_id_to_type[bus])
518 case MP_BUS_ISA: /* ISA pin */
520 polarity = default_ISA_polarity(idx);
523 case MP_BUS_EISA: /* EISA pin */
525 polarity = default_EISA_polarity(idx);
528 case MP_BUS_PCI: /* PCI pin */
530 polarity = default_PCI_polarity(idx);
533 case MP_BUS_MCA: /* MCA pin */
535 polarity = default_MCA_polarity(idx);
540 printk(KERN_WARNING "broken BIOS!!\n");
547 case 1: /* high active */
552 case 2: /* reserved */
554 printk(KERN_WARNING "broken BIOS!!\n");
558 case 3: /* low active */
563 default: /* invalid */
565 printk(KERN_WARNING "broken BIOS!!\n");
573 static int MPBIOS_trigger(int idx)
575 int bus = mp_irqs[idx].mpc_srcbus;
579 * Determine IRQ trigger mode (edge or level sensitive):
581 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
583 case 0: /* conforms, ie. bus-type dependent */
585 switch (mp_bus_id_to_type[bus])
587 case MP_BUS_ISA: /* ISA pin */
589 trigger = default_ISA_trigger(idx);
592 case MP_BUS_EISA: /* EISA pin */
594 trigger = default_EISA_trigger(idx);
597 case MP_BUS_PCI: /* PCI pin */
599 trigger = default_PCI_trigger(idx);
602 case MP_BUS_MCA: /* MCA pin */
604 trigger = default_MCA_trigger(idx);
609 printk(KERN_WARNING "broken BIOS!!\n");
621 case 2: /* reserved */
623 printk(KERN_WARNING "broken BIOS!!\n");
632 default: /* invalid */
634 printk(KERN_WARNING "broken BIOS!!\n");
642 static inline int irq_polarity(int idx)
644 return MPBIOS_polarity(idx);
647 static inline int irq_trigger(int idx)
649 return MPBIOS_trigger(idx);
652 static int next_irq = 16;
655 * gsi_irq_sharing -- Name overload! "irq" can be either a legacy IRQ
656 * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
657 * from ACPI, which can reach 800 in large boxen.
659 * Compact the sparse GSI space into a sequential IRQ series and reuse
660 * vectors if possible.
662 int gsi_irq_sharing(int gsi)
664 int i, tries, vector;
666 BUG_ON(gsi >= NR_IRQ_VECTORS);
668 if (platform_legacy_irq(gsi))
671 if (gsi_2_irq[gsi] != 0xFF)
672 return (int)gsi_2_irq[gsi];
676 vector = assign_irq_vector(gsi);
679 * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
680 * use of vector and if found, return that IRQ. However, we never want
681 * to share legacy IRQs, which usually have a different trigger mode
684 for (i = 0; i < NR_IRQS; i++)
685 if (IO_APIC_VECTOR(i) == vector)
687 if (platform_legacy_irq(i)) {
689 IO_APIC_VECTOR(i) = 0;
692 panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
696 printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
702 BUG_ON(i >= NR_IRQS);
704 IO_APIC_VECTOR(i) = vector;
705 printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
710 static int pin_2_irq(int idx, int apic, int pin)
713 int bus = mp_irqs[idx].mpc_srcbus;
716 * Debugging check, we are in big trouble if this message pops up!
718 if (mp_irqs[idx].mpc_dstirq != pin)
719 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
721 switch (mp_bus_id_to_type[bus])
723 case MP_BUS_ISA: /* ISA pin */
727 irq = mp_irqs[idx].mpc_srcbusirq;
730 case MP_BUS_PCI: /* PCI pin */
733 * PCI IRQs are mapped in order
737 irq += nr_ioapic_registers[i++];
739 irq = gsi_irq_sharing(irq);
744 printk(KERN_ERR "unknown bus type %d.\n",bus);
749 BUG_ON(irq >= NR_IRQS);
752 * PCI IRQ command line redirection. Yes, limits are hardcoded.
754 if ((pin >= 16) && (pin <= 23)) {
755 if (pirq_entries[pin-16] != -1) {
756 if (!pirq_entries[pin-16]) {
757 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
759 irq = pirq_entries[pin-16];
760 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
765 BUG_ON(irq >= NR_IRQS);
769 static inline int IO_APIC_irq_trigger(int irq)
773 for (apic = 0; apic < nr_ioapics; apic++) {
774 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
775 idx = find_irq_entry(apic,pin,mp_INT);
776 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
777 return irq_trigger(idx);
781 * nonexistent IRQs are edge default
786 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
787 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
789 int assign_irq_vector(int irq)
791 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
793 BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
794 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0)
795 return IO_APIC_VECTOR(irq);
798 if (current_vector == IA32_SYSCALL_VECTOR)
801 if (current_vector >= FIRST_SYSTEM_VECTOR) {
802 /* If we run out of vectors on large boxen, must share them. */
803 offset = (offset + 1) % 8;
804 current_vector = FIRST_DEVICE_VECTOR + offset;
807 vector_irq[current_vector] = irq;
808 if (irq != AUTO_ASSIGN)
809 IO_APIC_VECTOR(irq) = current_vector;
811 return current_vector;
814 extern void (*interrupt[NR_IRQS])(void);
815 static struct hw_interrupt_type ioapic_level_type;
816 static struct hw_interrupt_type ioapic_edge_type;
818 #define IOAPIC_AUTO -1
819 #define IOAPIC_EDGE 0
820 #define IOAPIC_LEVEL 1
822 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
824 if (use_pci_vector() && !platform_legacy_irq(irq)) {
825 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
826 trigger == IOAPIC_LEVEL)
827 irq_desc[vector].handler = &ioapic_level_type;
829 irq_desc[vector].handler = &ioapic_edge_type;
830 set_intr_gate(vector, interrupt[vector]);
832 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
833 trigger == IOAPIC_LEVEL)
834 irq_desc[irq].handler = &ioapic_level_type;
836 irq_desc[irq].handler = &ioapic_edge_type;
837 set_intr_gate(vector, interrupt[irq]);
841 static void __init setup_IO_APIC_irqs(void)
843 struct IO_APIC_route_entry entry;
844 int apic, pin, idx, irq, first_notcon = 1, vector;
847 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
849 for (apic = 0; apic < nr_ioapics; apic++) {
850 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
853 * add it to the IO-APIC irq-routing table:
855 memset(&entry,0,sizeof(entry));
857 entry.delivery_mode = INT_DELIVERY_MODE;
858 entry.dest_mode = INT_DEST_MODE;
859 entry.mask = 0; /* enable IRQ */
860 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
862 idx = find_irq_entry(apic,pin,mp_INT);
865 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
868 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
872 entry.trigger = irq_trigger(idx);
873 entry.polarity = irq_polarity(idx);
875 if (irq_trigger(idx)) {
878 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
881 irq = pin_2_irq(idx, apic, pin);
882 add_pin_to_irq(irq, apic, pin);
884 if (!apic && !IO_APIC_IRQ(irq))
887 if (IO_APIC_IRQ(irq)) {
888 vector = assign_irq_vector(irq);
889 entry.vector = vector;
891 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
892 if (!apic && (irq < 16))
893 disable_8259A_irq(irq);
895 spin_lock_irqsave(&ioapic_lock, flags);
896 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
897 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
898 set_native_irq_info(irq, TARGET_CPUS);
899 spin_unlock_irqrestore(&ioapic_lock, flags);
904 apic_printk(APIC_VERBOSE," not connected.\n");
908 * Set up the 8259A-master output pin as broadcast to all
911 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
913 struct IO_APIC_route_entry entry;
916 memset(&entry,0,sizeof(entry));
918 disable_8259A_irq(0);
921 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
924 * We use logical delivery to get the timer IRQ
927 entry.dest_mode = INT_DEST_MODE;
928 entry.mask = 0; /* unmask IRQ now */
929 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
930 entry.delivery_mode = INT_DELIVERY_MODE;
933 entry.vector = vector;
936 * The timer IRQ doesn't have to know that behind the
937 * scene we have a 8259A-master in AEOI mode ...
939 irq_desc[0].handler = &ioapic_edge_type;
942 * Add it to the IO-APIC irq-routing table:
944 spin_lock_irqsave(&ioapic_lock, flags);
945 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
946 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
947 spin_unlock_irqrestore(&ioapic_lock, flags);
952 void __init UNEXPECTED_IO_APIC(void)
956 void __apicdebuginit print_IO_APIC(void)
959 union IO_APIC_reg_00 reg_00;
960 union IO_APIC_reg_01 reg_01;
961 union IO_APIC_reg_02 reg_02;
964 if (apic_verbosity == APIC_QUIET)
967 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
968 for (i = 0; i < nr_ioapics; i++)
969 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
970 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
973 * We are a bit conservative about what we expect. We have to
974 * know about every hardware change ASAP.
976 printk(KERN_INFO "testing the IO APIC.......................\n");
978 for (apic = 0; apic < nr_ioapics; apic++) {
980 spin_lock_irqsave(&ioapic_lock, flags);
981 reg_00.raw = io_apic_read(apic, 0);
982 reg_01.raw = io_apic_read(apic, 1);
983 if (reg_01.bits.version >= 0x10)
984 reg_02.raw = io_apic_read(apic, 2);
985 spin_unlock_irqrestore(&ioapic_lock, flags);
988 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
989 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
990 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
991 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
992 UNEXPECTED_IO_APIC();
994 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
995 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
996 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
997 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
998 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
999 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1000 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1001 (reg_01.bits.entries != 0x2E) &&
1002 (reg_01.bits.entries != 0x3F) &&
1003 (reg_01.bits.entries != 0x03)
1005 UNEXPECTED_IO_APIC();
1007 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1008 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1009 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1010 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
1011 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1012 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1013 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1014 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1016 UNEXPECTED_IO_APIC();
1017 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1018 UNEXPECTED_IO_APIC();
1020 if (reg_01.bits.version >= 0x10) {
1021 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1022 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1023 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1024 UNEXPECTED_IO_APIC();
1027 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1029 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1030 " Stat Dest Deli Vect: \n");
1032 for (i = 0; i <= reg_01.bits.entries; i++) {
1033 struct IO_APIC_route_entry entry;
1035 spin_lock_irqsave(&ioapic_lock, flags);
1036 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
1037 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
1038 spin_unlock_irqrestore(&ioapic_lock, flags);
1040 printk(KERN_DEBUG " %02x %03X %02X ",
1042 entry.dest.logical.logical_dest,
1043 entry.dest.physical.physical_dest
1046 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1051 entry.delivery_status,
1053 entry.delivery_mode,
1058 if (use_pci_vector())
1059 printk(KERN_INFO "Using vector-based indexing\n");
1060 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1061 for (i = 0; i < NR_IRQS; i++) {
1062 struct irq_pin_list *entry = irq_2_pin + i;
1065 if (use_pci_vector() && !platform_legacy_irq(i))
1066 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1068 printk(KERN_DEBUG "IRQ%d ", i);
1070 printk("-> %d:%d", entry->apic, entry->pin);
1073 entry = irq_2_pin + entry->next;
1078 printk(KERN_INFO ".................................... done.\n");
1085 static __apicdebuginit void print_APIC_bitfield (int base)
1090 if (apic_verbosity == APIC_QUIET)
1093 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1094 for (i = 0; i < 8; i++) {
1095 v = apic_read(base + i*0x10);
1096 for (j = 0; j < 32; j++) {
1106 void __apicdebuginit print_local_APIC(void * dummy)
1108 unsigned int v, ver, maxlvt;
1110 if (apic_verbosity == APIC_QUIET)
1113 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1114 smp_processor_id(), hard_smp_processor_id());
1115 v = apic_read(APIC_ID);
1116 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1117 v = apic_read(APIC_LVR);
1118 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1119 ver = GET_APIC_VERSION(v);
1120 maxlvt = get_maxlvt();
1122 v = apic_read(APIC_TASKPRI);
1123 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1125 v = apic_read(APIC_ARBPRI);
1126 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1127 v & APIC_ARBPRI_MASK);
1128 v = apic_read(APIC_PROCPRI);
1129 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1131 v = apic_read(APIC_EOI);
1132 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1133 v = apic_read(APIC_RRR);
1134 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1135 v = apic_read(APIC_LDR);
1136 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1137 v = apic_read(APIC_DFR);
1138 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1139 v = apic_read(APIC_SPIV);
1140 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1142 printk(KERN_DEBUG "... APIC ISR field:\n");
1143 print_APIC_bitfield(APIC_ISR);
1144 printk(KERN_DEBUG "... APIC TMR field:\n");
1145 print_APIC_bitfield(APIC_TMR);
1146 printk(KERN_DEBUG "... APIC IRR field:\n");
1147 print_APIC_bitfield(APIC_IRR);
1149 v = apic_read(APIC_ESR);
1150 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1152 v = apic_read(APIC_ICR);
1153 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1154 v = apic_read(APIC_ICR2);
1155 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1157 v = apic_read(APIC_LVTT);
1158 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1160 if (maxlvt > 3) { /* PC is LVT#4. */
1161 v = apic_read(APIC_LVTPC);
1162 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1164 v = apic_read(APIC_LVT0);
1165 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1166 v = apic_read(APIC_LVT1);
1167 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1169 if (maxlvt > 2) { /* ERR is LVT#3. */
1170 v = apic_read(APIC_LVTERR);
1171 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1174 v = apic_read(APIC_TMICT);
1175 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1176 v = apic_read(APIC_TMCCT);
1177 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1178 v = apic_read(APIC_TDCR);
1179 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1183 void print_all_local_APICs (void)
1185 on_each_cpu(print_local_APIC, NULL, 1, 1);
1188 void __apicdebuginit print_PIC(void)
1191 unsigned long flags;
1193 if (apic_verbosity == APIC_QUIET)
1196 printk(KERN_DEBUG "\nprinting PIC contents\n");
1198 spin_lock_irqsave(&i8259A_lock, flags);
1200 v = inb(0xa1) << 8 | inb(0x21);
1201 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1203 v = inb(0xa0) << 8 | inb(0x20);
1204 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1208 v = inb(0xa0) << 8 | inb(0x20);
1212 spin_unlock_irqrestore(&i8259A_lock, flags);
1214 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1216 v = inb(0x4d1) << 8 | inb(0x4d0);
1217 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1222 static void __init enable_IO_APIC(void)
1224 union IO_APIC_reg_01 reg_01;
1225 int i8259_apic, i8259_pin;
1227 unsigned long flags;
1229 for (i = 0; i < PIN_MAP_SIZE; i++) {
1230 irq_2_pin[i].pin = -1;
1231 irq_2_pin[i].next = 0;
1234 for (i = 0; i < MAX_PIRQS; i++)
1235 pirq_entries[i] = -1;
1238 * The number of IO-APIC IRQ registers (== #pins):
1240 for (apic = 0; apic < nr_ioapics; apic++) {
1241 spin_lock_irqsave(&ioapic_lock, flags);
1242 reg_01.raw = io_apic_read(apic, 1);
1243 spin_unlock_irqrestore(&ioapic_lock, flags);
1244 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1246 for(apic = 0; apic < nr_ioapics; apic++) {
1248 /* See if any of the pins is in ExtINT mode */
1249 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1250 struct IO_APIC_route_entry entry;
1251 spin_lock_irqsave(&ioapic_lock, flags);
1252 *(((int *)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1253 *(((int *)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1254 spin_unlock_irqrestore(&ioapic_lock, flags);
1257 /* If the interrupt line is enabled and in ExtInt mode
1258 * I have found the pin where the i8259 is connected.
1260 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1261 ioapic_i8259.apic = apic;
1262 ioapic_i8259.pin = pin;
1268 /* Look to see what if the MP table has reported the ExtINT */
1269 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1270 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1271 /* Trust the MP table if nothing is setup in the hardware */
1272 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1273 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1274 ioapic_i8259.pin = i8259_pin;
1275 ioapic_i8259.apic = i8259_apic;
1277 /* Complain if the MP table and the hardware disagree */
1278 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1279 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1281 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1285 * Do not trust the IO-APIC being empty at bootup
1291 * Not an __init, needed by the reboot code
1293 void disable_IO_APIC(void)
1296 * Clear the IO-APIC before rebooting:
1301 * If the i8259 is routed through an IOAPIC
1302 * Put that IOAPIC in virtual wire mode
1303 * so legacy interrupts can be delivered.
1305 if (ioapic_i8259.pin != -1) {
1306 struct IO_APIC_route_entry entry;
1307 unsigned long flags;
1309 memset(&entry, 0, sizeof(entry));
1310 entry.mask = 0; /* Enabled */
1311 entry.trigger = 0; /* Edge */
1313 entry.polarity = 0; /* High */
1314 entry.delivery_status = 0;
1315 entry.dest_mode = 0; /* Physical */
1316 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1318 entry.dest.physical.physical_dest =
1319 GET_APIC_ID(apic_read(APIC_ID));
1322 * Add it to the IO-APIC irq-routing table:
1324 spin_lock_irqsave(&ioapic_lock, flags);
1325 io_apic_write(ioapic_i8259.apic, 0x11+2*ioapic_i8259.pin,
1326 *(((int *)&entry)+1));
1327 io_apic_write(ioapic_i8259.apic, 0x10+2*ioapic_i8259.pin,
1328 *(((int *)&entry)+0));
1329 spin_unlock_irqrestore(&ioapic_lock, flags);
1332 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1336 * function to set the IO-APIC physical IDs based on the
1337 * values stored in the MPC table.
1339 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1342 static void __init setup_ioapic_ids_from_mpc (void)
1344 union IO_APIC_reg_00 reg_00;
1347 unsigned char old_id;
1348 unsigned long flags;
1351 * Set the IOAPIC ID to the value stored in the MPC table.
1353 for (apic = 0; apic < nr_ioapics; apic++) {
1355 /* Read the register 0 value */
1356 spin_lock_irqsave(&ioapic_lock, flags);
1357 reg_00.raw = io_apic_read(apic, 0);
1358 spin_unlock_irqrestore(&ioapic_lock, flags);
1360 old_id = mp_ioapics[apic].mpc_apicid;
1363 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1367 * We need to adjust the IRQ routing table
1368 * if the ID changed.
1370 if (old_id != mp_ioapics[apic].mpc_apicid)
1371 for (i = 0; i < mp_irq_entries; i++)
1372 if (mp_irqs[i].mpc_dstapic == old_id)
1373 mp_irqs[i].mpc_dstapic
1374 = mp_ioapics[apic].mpc_apicid;
1377 * Read the right value from the MPC table and
1378 * write it into the ID register.
1380 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1381 mp_ioapics[apic].mpc_apicid);
1383 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1384 spin_lock_irqsave(&ioapic_lock, flags);
1385 io_apic_write(apic, 0, reg_00.raw);
1386 spin_unlock_irqrestore(&ioapic_lock, flags);
1391 spin_lock_irqsave(&ioapic_lock, flags);
1392 reg_00.raw = io_apic_read(apic, 0);
1393 spin_unlock_irqrestore(&ioapic_lock, flags);
1394 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1395 printk("could not set ID!\n");
1397 apic_printk(APIC_VERBOSE," ok.\n");
1402 * There is a nasty bug in some older SMP boards, their mptable lies
1403 * about the timer IRQ. We do the following to work around the situation:
1405 * - timer IRQ defaults to IO-APIC IRQ
1406 * - if this function detects that timer IRQs are defunct, then we fall
1407 * back to ISA timer IRQs
1409 static int __init timer_irq_works(void)
1411 unsigned long t1 = jiffies;
1414 /* Let ten ticks pass... */
1415 mdelay((10 * 1000) / HZ);
1418 * Expect a few ticks at least, to be sure some possible
1419 * glue logic does not lock up after one or two first
1420 * ticks in a non-ExtINT mode. Also the local APIC
1421 * might have cached one ExtINT interrupt. Finally, at
1422 * least one tick may be lost due to delays.
1426 if (jiffies - t1 > 4)
1432 * In the SMP+IOAPIC case it might happen that there are an unspecified
1433 * number of pending IRQ events unhandled. These cases are very rare,
1434 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1435 * better to do it this way as thus we do not have to be aware of
1436 * 'pending' interrupts in the IRQ path, except at this point.
1439 * Edge triggered needs to resend any interrupt
1440 * that was delayed but this is now handled in the device
1445 * Starting up a edge-triggered IO-APIC interrupt is
1446 * nasty - we need to make sure that we get the edge.
1447 * If it is already asserted for some reason, we need
1448 * return 1 to indicate that is was pending.
1450 * This is not complete - we should be able to fake
1451 * an edge even if it isn't on the 8259A...
1454 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1456 int was_pending = 0;
1457 unsigned long flags;
1459 spin_lock_irqsave(&ioapic_lock, flags);
1461 disable_8259A_irq(irq);
1462 if (i8259A_irq_pending(irq))
1465 __unmask_IO_APIC_irq(irq);
1466 spin_unlock_irqrestore(&ioapic_lock, flags);
1472 * Once we have recorded IRQ_PENDING already, we can mask the
1473 * interrupt for real. This prevents IRQ storms from unhandled
1476 static void ack_edge_ioapic_irq(unsigned int irq)
1479 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1480 == (IRQ_PENDING | IRQ_DISABLED))
1481 mask_IO_APIC_irq(irq);
1486 * Level triggered interrupts can just be masked,
1487 * and shutting down and starting up the interrupt
1488 * is the same as enabling and disabling them -- except
1489 * with a startup need to return a "was pending" value.
1491 * Level triggered interrupts are special because we
1492 * do not touch any IO-APIC register while handling
1493 * them. We ack the APIC in the end-IRQ handler, not
1494 * in the start-IRQ-handler. Protection against reentrance
1495 * from the same interrupt is still provided, both by the
1496 * generic IRQ layer and by the fact that an unacked local
1497 * APIC does not accept IRQs.
1499 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1501 unmask_IO_APIC_irq(irq);
1503 return 0; /* don't check for pending */
1506 static void end_level_ioapic_irq (unsigned int irq)
1512 #ifdef CONFIG_PCI_MSI
1513 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1515 int irq = vector_to_irq(vector);
1517 return startup_edge_ioapic_irq(irq);
1520 static void ack_edge_ioapic_vector(unsigned int vector)
1522 int irq = vector_to_irq(vector);
1524 move_native_irq(vector);
1525 ack_edge_ioapic_irq(irq);
1528 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1530 int irq = vector_to_irq(vector);
1532 return startup_level_ioapic_irq (irq);
1535 static void end_level_ioapic_vector (unsigned int vector)
1537 int irq = vector_to_irq(vector);
1539 move_native_irq(vector);
1540 end_level_ioapic_irq(irq);
1543 static void mask_IO_APIC_vector (unsigned int vector)
1545 int irq = vector_to_irq(vector);
1547 mask_IO_APIC_irq(irq);
1550 static void unmask_IO_APIC_vector (unsigned int vector)
1552 int irq = vector_to_irq(vector);
1554 unmask_IO_APIC_irq(irq);
1558 static void set_ioapic_affinity_vector (unsigned int vector,
1561 int irq = vector_to_irq(vector);
1563 set_native_irq_info(vector, cpu_mask);
1564 set_ioapic_affinity_irq(irq, cpu_mask);
1566 #endif // CONFIG_SMP
1567 #endif // CONFIG_PCI_MSI
1570 * Level and edge triggered IO-APIC interrupts need different handling,
1571 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1572 * handled with the level-triggered descriptor, but that one has slightly
1573 * more overhead. Level-triggered interrupts cannot be handled with the
1574 * edge-triggered handler, without risking IRQ storms and other ugly
1578 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1579 .typename = "IO-APIC-edge",
1580 .startup = startup_edge_ioapic,
1581 .shutdown = shutdown_edge_ioapic,
1582 .enable = enable_edge_ioapic,
1583 .disable = disable_edge_ioapic,
1584 .ack = ack_edge_ioapic,
1585 .end = end_edge_ioapic,
1587 .set_affinity = set_ioapic_affinity,
1591 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1592 .typename = "IO-APIC-level",
1593 .startup = startup_level_ioapic,
1594 .shutdown = shutdown_level_ioapic,
1595 .enable = enable_level_ioapic,
1596 .disable = disable_level_ioapic,
1597 .ack = mask_and_ack_level_ioapic,
1598 .end = end_level_ioapic,
1600 .set_affinity = set_ioapic_affinity,
1604 static inline void init_IO_APIC_traps(void)
1609 * NOTE! The local APIC isn't very good at handling
1610 * multiple interrupts at the same interrupt level.
1611 * As the interrupt level is determined by taking the
1612 * vector number and shifting that right by 4, we
1613 * want to spread these out a bit so that they don't
1614 * all fall in the same interrupt level.
1616 * Also, we've got to be careful not to trash gate
1617 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1619 for (irq = 0; irq < NR_IRQS ; irq++) {
1621 if (use_pci_vector()) {
1622 if (!platform_legacy_irq(tmp))
1623 if ((tmp = vector_to_irq(tmp)) == -1)
1626 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1628 * Hmm.. We don't have an entry for this,
1629 * so default to an old-fashioned 8259
1630 * interrupt if we can..
1633 make_8259A_irq(irq);
1635 /* Strange. Oh, well.. */
1636 irq_desc[irq].handler = &no_irq_type;
1641 static void enable_lapic_irq (unsigned int irq)
1645 v = apic_read(APIC_LVT0);
1646 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1649 static void disable_lapic_irq (unsigned int irq)
1653 v = apic_read(APIC_LVT0);
1654 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1657 static void ack_lapic_irq (unsigned int irq)
1662 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1664 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1665 .typename = "local-APIC-edge",
1666 .startup = NULL, /* startup_irq() not used for IRQ0 */
1667 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1668 .enable = enable_lapic_irq,
1669 .disable = disable_lapic_irq,
1670 .ack = ack_lapic_irq,
1671 .end = end_lapic_irq,
1674 static void setup_nmi (void)
1677 * Dirty trick to enable the NMI watchdog ...
1678 * We put the 8259A master into AEOI mode and
1679 * unmask on all local APICs LVT0 as NMI.
1681 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1682 * is from Maciej W. Rozycki - so we do not have to EOI from
1683 * the NMI handler or the timer interrupt.
1685 printk(KERN_INFO "activating NMI Watchdog ...");
1687 enable_NMI_through_LVT0(NULL);
1693 * This looks a bit hackish but it's about the only one way of sending
1694 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1695 * not support the ExtINT mode, unfortunately. We need to send these
1696 * cycles as some i82489DX-based boards have glue logic that keeps the
1697 * 8259A interrupt line asserted until INTA. --macro
1699 static inline void unlock_ExtINT_logic(void)
1702 struct IO_APIC_route_entry entry0, entry1;
1703 unsigned char save_control, save_freq_select;
1704 unsigned long flags;
1706 pin = find_isa_irq_pin(8, mp_INT);
1707 apic = find_isa_irq_apic(8, mp_INT);
1711 spin_lock_irqsave(&ioapic_lock, flags);
1712 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1713 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1714 spin_unlock_irqrestore(&ioapic_lock, flags);
1715 clear_IO_APIC_pin(apic, pin);
1717 memset(&entry1, 0, sizeof(entry1));
1719 entry1.dest_mode = 0; /* physical delivery */
1720 entry1.mask = 0; /* unmask IRQ now */
1721 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1722 entry1.delivery_mode = dest_ExtINT;
1723 entry1.polarity = entry0.polarity;
1727 spin_lock_irqsave(&ioapic_lock, flags);
1728 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1729 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1730 spin_unlock_irqrestore(&ioapic_lock, flags);
1732 save_control = CMOS_READ(RTC_CONTROL);
1733 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1734 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1736 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1741 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1745 CMOS_WRITE(save_control, RTC_CONTROL);
1746 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1747 clear_IO_APIC_pin(apic, pin);
1749 spin_lock_irqsave(&ioapic_lock, flags);
1750 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1751 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1752 spin_unlock_irqrestore(&ioapic_lock, flags);
1756 * This code may look a bit paranoid, but it's supposed to cooperate with
1757 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1758 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1759 * fanatically on his truly buggy board.
1761 static inline void check_timer(void)
1763 int apic1, pin1, apic2, pin2;
1767 * get/set the timer IRQ vector:
1769 disable_8259A_irq(0);
1770 vector = assign_irq_vector(0);
1771 set_intr_gate(vector, interrupt[0]);
1774 * Subtle, code in do_timer_interrupt() expects an AEOI
1775 * mode for the 8259A whenever interrupts are routed
1776 * through I/O APICs. Also IRQ0 has to be enabled in
1777 * the 8259A which implies the virtual wire has to be
1778 * disabled in the local APIC.
1780 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1782 enable_8259A_irq(0);
1784 pin1 = find_isa_irq_pin(0, mp_INT);
1785 apic1 = find_isa_irq_apic(0, mp_INT);
1786 pin2 = ioapic_i8259.pin;
1787 apic2 = ioapic_i8259.apic;
1789 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1790 vector, apic1, pin1, apic2, pin2);
1794 * Ok, does IRQ0 through the IOAPIC work?
1796 unmask_IO_APIC_irq(0);
1797 if (!no_timer_check && timer_irq_works()) {
1798 nmi_watchdog_default();
1799 if (nmi_watchdog == NMI_IO_APIC) {
1800 disable_8259A_irq(0);
1802 enable_8259A_irq(0);
1804 if (disable_timer_pin_1 > 0)
1805 clear_IO_APIC_pin(0, pin1);
1808 clear_IO_APIC_pin(apic1, pin1);
1809 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1810 "connected to IO-APIC\n");
1813 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1814 "through the 8259A ... ");
1816 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1819 * legacy devices should be connected to IO APIC #0
1821 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1822 if (timer_irq_works()) {
1824 nmi_watchdog_default();
1825 if (nmi_watchdog == NMI_IO_APIC) {
1831 * Cleanup, just in case ...
1833 clear_IO_APIC_pin(apic2, pin2);
1835 printk(" failed.\n");
1838 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1842 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1844 disable_8259A_irq(0);
1845 irq_desc[0].handler = &lapic_irq_type;
1846 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1847 enable_8259A_irq(0);
1849 if (timer_irq_works()) {
1850 apic_printk(APIC_QUIET, " works.\n");
1853 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1854 apic_printk(APIC_VERBOSE," failed.\n");
1856 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1860 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1862 unlock_ExtINT_logic();
1864 if (timer_irq_works()) {
1865 apic_printk(APIC_VERBOSE," works.\n");
1868 apic_printk(APIC_VERBOSE," failed :(.\n");
1869 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1872 static int __init notimercheck(char *s)
1877 __setup("no_timer_check", notimercheck);
1881 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1882 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1883 * Linux doesn't really care, as it's not actually used
1884 * for any interrupt handling anyway.
1886 #define PIC_IRQS (1<<2)
1888 void __init setup_IO_APIC(void)
1893 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1895 io_apic_irqs = ~PIC_IRQS;
1897 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1900 * Set up the IO-APIC IRQ routing table.
1903 setup_ioapic_ids_from_mpc();
1905 setup_IO_APIC_irqs();
1906 init_IO_APIC_traps();
1912 struct sysfs_ioapic_data {
1913 struct sys_device dev;
1914 struct IO_APIC_route_entry entry[0];
1916 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1918 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1920 struct IO_APIC_route_entry *entry;
1921 struct sysfs_ioapic_data *data;
1922 unsigned long flags;
1925 data = container_of(dev, struct sysfs_ioapic_data, dev);
1926 entry = data->entry;
1927 spin_lock_irqsave(&ioapic_lock, flags);
1928 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1929 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1930 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1932 spin_unlock_irqrestore(&ioapic_lock, flags);
1937 static int ioapic_resume(struct sys_device *dev)
1939 struct IO_APIC_route_entry *entry;
1940 struct sysfs_ioapic_data *data;
1941 unsigned long flags;
1942 union IO_APIC_reg_00 reg_00;
1945 data = container_of(dev, struct sysfs_ioapic_data, dev);
1946 entry = data->entry;
1948 spin_lock_irqsave(&ioapic_lock, flags);
1949 reg_00.raw = io_apic_read(dev->id, 0);
1950 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1951 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1952 io_apic_write(dev->id, 0, reg_00.raw);
1954 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1955 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1956 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1958 spin_unlock_irqrestore(&ioapic_lock, flags);
1963 static struct sysdev_class ioapic_sysdev_class = {
1964 set_kset_name("ioapic"),
1965 .suspend = ioapic_suspend,
1966 .resume = ioapic_resume,
1969 static int __init ioapic_init_sysfs(void)
1971 struct sys_device * dev;
1972 int i, size, error = 0;
1974 error = sysdev_class_register(&ioapic_sysdev_class);
1978 for (i = 0; i < nr_ioapics; i++ ) {
1979 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1980 * sizeof(struct IO_APIC_route_entry);
1981 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1982 if (!mp_ioapic_data[i]) {
1983 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1986 memset(mp_ioapic_data[i], 0, size);
1987 dev = &mp_ioapic_data[i]->dev;
1989 dev->cls = &ioapic_sysdev_class;
1990 error = sysdev_register(dev);
1992 kfree(mp_ioapic_data[i]);
1993 mp_ioapic_data[i] = NULL;
1994 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2002 device_initcall(ioapic_init_sysfs);
2004 /* --------------------------------------------------------------------------
2005 ACPI-based IOAPIC Configuration
2006 -------------------------------------------------------------------------- */
2010 #define IO_APIC_MAX_ID 0xFE
2012 int __init io_apic_get_version (int ioapic)
2014 union IO_APIC_reg_01 reg_01;
2015 unsigned long flags;
2017 spin_lock_irqsave(&ioapic_lock, flags);
2018 reg_01.raw = io_apic_read(ioapic, 1);
2019 spin_unlock_irqrestore(&ioapic_lock, flags);
2021 return reg_01.bits.version;
2025 int __init io_apic_get_redir_entries (int ioapic)
2027 union IO_APIC_reg_01 reg_01;
2028 unsigned long flags;
2030 spin_lock_irqsave(&ioapic_lock, flags);
2031 reg_01.raw = io_apic_read(ioapic, 1);
2032 spin_unlock_irqrestore(&ioapic_lock, flags);
2034 return reg_01.bits.entries;
2038 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2040 struct IO_APIC_route_entry entry;
2041 unsigned long flags;
2043 if (!IO_APIC_IRQ(irq)) {
2044 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2050 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2051 * Note that we mask (disable) IRQs now -- these get enabled when the
2052 * corresponding device driver registers for this IRQ.
2055 memset(&entry,0,sizeof(entry));
2057 entry.delivery_mode = INT_DELIVERY_MODE;
2058 entry.dest_mode = INT_DEST_MODE;
2059 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2060 entry.trigger = triggering;
2061 entry.polarity = polarity;
2062 entry.mask = 1; /* Disabled (masked) */
2064 irq = gsi_irq_sharing(irq);
2066 * IRQs < 16 are already in the irq_2_pin[] map
2069 add_pin_to_irq(irq, ioapic, pin);
2071 entry.vector = assign_irq_vector(irq);
2073 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2074 "IRQ %d Mode:%i Active:%i)\n", ioapic,
2075 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2076 triggering, polarity);
2078 ioapic_register_intr(irq, entry.vector, triggering);
2080 if (!ioapic && (irq < 16))
2081 disable_8259A_irq(irq);
2083 spin_lock_irqsave(&ioapic_lock, flags);
2084 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2085 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2086 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
2087 spin_unlock_irqrestore(&ioapic_lock, flags);
2092 #endif /* CONFIG_ACPI */
2096 * This function currently is only a helper for the i386 smp boot process where
2097 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2098 * so mask in all cases should simply be TARGET_CPUS
2101 void __init setup_ioapic_dest(void)
2103 int pin, ioapic, irq, irq_entry;
2105 if (skip_ioapic_setup == 1)
2108 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2109 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2110 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2111 if (irq_entry == -1)
2113 irq = pin_2_irq(irq_entry, ioapic, pin);
2114 set_ioapic_affinity_irq(irq, TARGET_CPUS);