2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/smp_lock.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/acpi.h>
31 #include <linux/sysdev.h>
33 #include <acpi/acpi_bus.h>
39 #include <asm/proto.h>
40 #include <asm/mach_apic.h>
45 #define __apicdebuginit __init
47 int sis_apic_bug; /* not actually supported, dummy for compile */
49 static int no_timer_check;
51 int disable_timer_pin_1 __initdata;
53 int timer_over_8254 __initdata = 0;
55 /* Where if anywhere is the i8259 connect in external int mode */
56 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
58 static DEFINE_SPINLOCK(ioapic_lock);
59 static DEFINE_SPINLOCK(vector_lock);
62 * # of IRQ routing registers
64 int nr_ioapic_registers[MAX_IO_APICS];
67 * Rough estimation of how many shared IRQs there are, can
70 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
71 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
74 * This is performance-critical, we want to do it O(1)
76 * the indexing order of this array favors 1:1 mappings
77 * between pins and IRQs.
80 static struct irq_pin_list {
81 short apic, pin, next;
82 } irq_2_pin[PIN_MAP_SIZE];
84 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
86 #define vector_to_irq(vector) \
87 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
89 #define vector_to_irq(vector) (vector)
92 #define __DO_ACTION(R, ACTION, FINAL) \
96 struct irq_pin_list *entry = irq_2_pin + irq; \
98 BUG_ON(irq >= NR_IRQS); \
104 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
106 io_apic_modify(entry->apic, reg); \
109 entry = irq_2_pin + entry->next; \
115 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
121 cpus_and(tmp, mask, cpu_online_map);
125 cpus_and(mask, tmp, CPU_MASK_ALL);
127 dest = cpu_mask_to_apicid(mask);
130 * Only the high 8 bits are valid.
132 dest = SET_APIC_LOGICAL_ID(dest);
134 spin_lock_irqsave(&ioapic_lock, flags);
135 __DO_ACTION(1, = dest, )
136 set_irq_info(irq, mask);
137 spin_unlock_irqrestore(&ioapic_lock, flags);
141 static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
144 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
145 * shared ISA-space IRQs, so we have to support them. We are super
146 * fast in the common case, and fast for shared ISA-space IRQs.
148 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
150 static int first_free_entry = NR_IRQS;
151 struct irq_pin_list *entry = irq_2_pin + irq;
153 BUG_ON(irq >= NR_IRQS);
155 entry = irq_2_pin + entry->next;
157 if (entry->pin != -1) {
158 entry->next = first_free_entry;
159 entry = irq_2_pin + entry->next;
160 if (++first_free_entry >= PIN_MAP_SIZE)
161 panic("io_apic.c: ran out of irq_2_pin entries!");
168 #define DO_ACTION(name,R,ACTION, FINAL) \
170 static void name##_IO_APIC_irq (unsigned int irq) \
171 __DO_ACTION(R, ACTION, FINAL)
173 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
175 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
178 static void mask_IO_APIC_irq (unsigned int irq)
182 spin_lock_irqsave(&ioapic_lock, flags);
183 __mask_IO_APIC_irq(irq);
184 spin_unlock_irqrestore(&ioapic_lock, flags);
187 static void unmask_IO_APIC_irq (unsigned int irq)
191 spin_lock_irqsave(&ioapic_lock, flags);
192 __unmask_IO_APIC_irq(irq);
193 spin_unlock_irqrestore(&ioapic_lock, flags);
196 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
198 struct IO_APIC_route_entry entry;
201 /* Check delivery_mode to be sure we're not clearing an SMI pin */
202 spin_lock_irqsave(&ioapic_lock, flags);
203 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
204 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
205 spin_unlock_irqrestore(&ioapic_lock, flags);
206 if (entry.delivery_mode == dest_SMI)
209 * Disable it in the IO-APIC irq-routing table:
211 memset(&entry, 0, sizeof(entry));
213 spin_lock_irqsave(&ioapic_lock, flags);
214 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
215 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
216 spin_unlock_irqrestore(&ioapic_lock, flags);
219 static void clear_IO_APIC (void)
223 for (apic = 0; apic < nr_ioapics; apic++)
224 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
225 clear_IO_APIC_pin(apic, pin);
228 int skip_ioapic_setup;
231 /* dummy parsing: see setup.c */
233 static int __init disable_ioapic_setup(char *str)
235 skip_ioapic_setup = 1;
239 static int __init enable_ioapic_setup(char *str)
242 skip_ioapic_setup = 0;
246 __setup("noapic", disable_ioapic_setup);
247 __setup("apic", enable_ioapic_setup);
249 static int __init setup_disable_8254_timer(char *s)
251 timer_over_8254 = -1;
254 static int __init setup_enable_8254_timer(char *s)
260 __setup("disable_8254_timer", setup_disable_8254_timer);
261 __setup("enable_8254_timer", setup_enable_8254_timer);
263 #include <asm/pci-direct.h>
264 #include <linux/pci_ids.h>
265 #include <linux/pci.h>
270 static int nvidia_hpet_detected __initdata;
272 static int __init nvidia_hpet_check(unsigned long phys, unsigned long size)
274 nvidia_hpet_detected = 1;
279 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
280 off. Check for an Nvidia or VIA PCI bridge and turn it off.
281 Use pci direct infrastructure because this runs before the PCI subsystem.
283 Can be overwritten with "apic"
285 And another hack to disable the IOMMU on VIA chipsets.
287 ... and others. Really should move this somewhere else.
290 void __init check_ioapic(void)
293 /* Poor man's PCI discovery */
294 for (num = 0; num < 32; num++) {
295 for (slot = 0; slot < 32; slot++) {
296 for (func = 0; func < 8; func++) {
300 class = read_pci_config(num,slot,func,
302 if (class == 0xffffffff)
305 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
308 vendor = read_pci_config(num, slot, func,
312 case PCI_VENDOR_ID_VIA:
314 if ((end_pfn > MAX_DMA32_PFN ||
316 !iommu_aperture_allowed) {
318 "Looks like a VIA chipset. Disabling IOMMU. Override with \"iommu=allowed\"\n");
319 iommu_aperture_disabled = 1;
323 case PCI_VENDOR_ID_NVIDIA:
326 * All timer overrides on Nvidia are
327 * wrong unless HPET is enabled.
329 nvidia_hpet_detected = 0;
330 acpi_table_parse(ACPI_HPET,
332 if (nvidia_hpet_detected == 0) {
333 acpi_skip_timer_override = 1;
334 printk(KERN_INFO "Nvidia board "
335 "detected. Ignoring ACPI "
336 "timer override.\n");
339 /* RED-PEN skip them on mptables too? */
342 /* This should be actually default, but
343 for 2.6.16 let's do it for ATI only where
344 it's really needed. */
345 case PCI_VENDOR_ID_ATI:
346 if (timer_over_8254 == 1) {
349 "ATI board detected. Disabling timer routing over 8254.\n");
355 /* No multi-function device? */
356 type = read_pci_config_byte(num,slot,func,
366 * Find the IRQ entry number of a certain pin.
368 static int find_irq_entry(int apic, int pin, int type)
372 for (i = 0; i < mp_irq_entries; i++)
373 if (mp_irqs[i].mpc_irqtype == type &&
374 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
375 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
376 mp_irqs[i].mpc_dstirq == pin)
383 * Find the pin to which IRQ[irq] (ISA) is connected
385 static int __init find_isa_irq_pin(int irq, int type)
389 for (i = 0; i < mp_irq_entries; i++) {
390 int lbus = mp_irqs[i].mpc_srcbus;
392 if (mp_bus_id_to_type[lbus] == MP_BUS_ISA &&
393 (mp_irqs[i].mpc_irqtype == type) &&
394 (mp_irqs[i].mpc_srcbusirq == irq))
396 return mp_irqs[i].mpc_dstirq;
401 static int __init find_isa_irq_apic(int irq, int type)
405 for (i = 0; i < mp_irq_entries; i++) {
406 int lbus = mp_irqs[i].mpc_srcbus;
408 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA) &&
409 (mp_irqs[i].mpc_irqtype == type) &&
410 (mp_irqs[i].mpc_srcbusirq == irq))
413 if (i < mp_irq_entries) {
415 for(apic = 0; apic < nr_ioapics; apic++) {
416 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
425 * Find a specific PCI IRQ entry.
426 * Not an __init, possibly needed by modules
428 static int pin_2_irq(int idx, int apic, int pin);
430 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
432 int apic, i, best_guess = -1;
434 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
436 if (mp_bus_id_to_pci_bus[bus] == -1) {
437 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
440 for (i = 0; i < mp_irq_entries; i++) {
441 int lbus = mp_irqs[i].mpc_srcbus;
443 for (apic = 0; apic < nr_ioapics; apic++)
444 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
445 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
448 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
449 !mp_irqs[i].mpc_irqtype &&
451 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
452 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
454 if (!(apic || IO_APIC_IRQ(irq)))
457 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
460 * Use the first all-but-pin matching entry as a
461 * best-guess fuzzy result for broken mptables.
467 BUG_ON(best_guess >= NR_IRQS);
471 /* ISA interrupts are always polarity zero edge triggered,
472 * when listed as conforming in the MP table. */
474 #define default_ISA_trigger(idx) (0)
475 #define default_ISA_polarity(idx) (0)
477 /* PCI interrupts are always polarity one level triggered,
478 * when listed as conforming in the MP table. */
480 #define default_PCI_trigger(idx) (1)
481 #define default_PCI_polarity(idx) (1)
483 static int __init MPBIOS_polarity(int idx)
485 int bus = mp_irqs[idx].mpc_srcbus;
489 * Determine IRQ line polarity (high active or low active):
491 switch (mp_irqs[idx].mpc_irqflag & 3)
493 case 0: /* conforms, ie. bus-type dependent polarity */
495 switch (mp_bus_id_to_type[bus])
497 case MP_BUS_ISA: /* ISA pin */
499 polarity = default_ISA_polarity(idx);
502 case MP_BUS_PCI: /* PCI pin */
504 polarity = default_PCI_polarity(idx);
509 printk(KERN_WARNING "broken BIOS!!\n");
516 case 1: /* high active */
521 case 2: /* reserved */
523 printk(KERN_WARNING "broken BIOS!!\n");
527 case 3: /* low active */
532 default: /* invalid */
534 printk(KERN_WARNING "broken BIOS!!\n");
542 static int MPBIOS_trigger(int idx)
544 int bus = mp_irqs[idx].mpc_srcbus;
548 * Determine IRQ trigger mode (edge or level sensitive):
550 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
552 case 0: /* conforms, ie. bus-type dependent */
554 switch (mp_bus_id_to_type[bus])
556 case MP_BUS_ISA: /* ISA pin */
558 trigger = default_ISA_trigger(idx);
561 case MP_BUS_PCI: /* PCI pin */
563 trigger = default_PCI_trigger(idx);
568 printk(KERN_WARNING "broken BIOS!!\n");
580 case 2: /* reserved */
582 printk(KERN_WARNING "broken BIOS!!\n");
591 default: /* invalid */
593 printk(KERN_WARNING "broken BIOS!!\n");
601 static inline int irq_polarity(int idx)
603 return MPBIOS_polarity(idx);
606 static inline int irq_trigger(int idx)
608 return MPBIOS_trigger(idx);
611 static int next_irq = 16;
614 * gsi_irq_sharing -- Name overload! "irq" can be either a legacy IRQ
615 * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
616 * from ACPI, which can reach 800 in large boxen.
618 * Compact the sparse GSI space into a sequential IRQ series and reuse
619 * vectors if possible.
621 int gsi_irq_sharing(int gsi)
623 int i, tries, vector;
625 BUG_ON(gsi >= NR_IRQ_VECTORS);
627 if (platform_legacy_irq(gsi))
630 if (gsi_2_irq[gsi] != 0xFF)
631 return (int)gsi_2_irq[gsi];
635 vector = assign_irq_vector(gsi);
638 * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
639 * use of vector and if found, return that IRQ. However, we never want
640 * to share legacy IRQs, which usually have a different trigger mode
643 for (i = 0; i < NR_IRQS; i++)
644 if (IO_APIC_VECTOR(i) == vector)
646 if (platform_legacy_irq(i)) {
648 IO_APIC_VECTOR(i) = 0;
651 panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
655 printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
661 BUG_ON(i >= NR_IRQS);
663 IO_APIC_VECTOR(i) = vector;
664 printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
669 static int pin_2_irq(int idx, int apic, int pin)
672 int bus = mp_irqs[idx].mpc_srcbus;
675 * Debugging check, we are in big trouble if this message pops up!
677 if (mp_irqs[idx].mpc_dstirq != pin)
678 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
680 switch (mp_bus_id_to_type[bus])
682 case MP_BUS_ISA: /* ISA pin */
684 irq = mp_irqs[idx].mpc_srcbusirq;
687 case MP_BUS_PCI: /* PCI pin */
690 * PCI IRQs are mapped in order
694 irq += nr_ioapic_registers[i++];
696 irq = gsi_irq_sharing(irq);
701 printk(KERN_ERR "unknown bus type %d.\n",bus);
706 BUG_ON(irq >= NR_IRQS);
710 static inline int IO_APIC_irq_trigger(int irq)
714 for (apic = 0; apic < nr_ioapics; apic++) {
715 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
716 idx = find_irq_entry(apic,pin,mp_INT);
717 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
718 return irq_trigger(idx);
722 * nonexistent IRQs are edge default
727 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
728 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
730 int assign_irq_vector(int irq)
732 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
736 BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
738 spin_lock_irqsave(&vector_lock, flags);
740 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0) {
741 spin_unlock_irqrestore(&vector_lock, flags);
742 return IO_APIC_VECTOR(irq);
746 if (current_vector == IA32_SYSCALL_VECTOR)
749 if (current_vector >= FIRST_SYSTEM_VECTOR) {
750 /* If we run out of vectors on large boxen, must share them. */
751 offset = (offset + 1) % 8;
752 current_vector = FIRST_DEVICE_VECTOR + offset;
755 vector = current_vector;
756 vector_irq[vector] = irq;
757 if (irq != AUTO_ASSIGN)
758 IO_APIC_VECTOR(irq) = vector;
760 spin_unlock_irqrestore(&vector_lock, flags);
765 extern void (*interrupt[NR_IRQS])(void);
766 static struct hw_interrupt_type ioapic_level_type;
767 static struct hw_interrupt_type ioapic_edge_type;
769 #define IOAPIC_AUTO -1
770 #define IOAPIC_EDGE 0
771 #define IOAPIC_LEVEL 1
773 static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
777 idx = use_pci_vector() && !platform_legacy_irq(irq) ? vector : irq;
779 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
780 trigger == IOAPIC_LEVEL)
781 irq_desc[idx].chip = &ioapic_level_type;
783 irq_desc[idx].chip = &ioapic_edge_type;
784 set_intr_gate(vector, interrupt[idx]);
787 static void __init setup_IO_APIC_irqs(void)
789 struct IO_APIC_route_entry entry;
790 int apic, pin, idx, irq, first_notcon = 1, vector;
793 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
795 for (apic = 0; apic < nr_ioapics; apic++) {
796 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
799 * add it to the IO-APIC irq-routing table:
801 memset(&entry,0,sizeof(entry));
803 entry.delivery_mode = INT_DELIVERY_MODE;
804 entry.dest_mode = INT_DEST_MODE;
805 entry.mask = 0; /* enable IRQ */
806 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
808 idx = find_irq_entry(apic,pin,mp_INT);
811 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
814 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
818 entry.trigger = irq_trigger(idx);
819 entry.polarity = irq_polarity(idx);
821 if (irq_trigger(idx)) {
824 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
827 irq = pin_2_irq(idx, apic, pin);
828 add_pin_to_irq(irq, apic, pin);
830 if (!apic && !IO_APIC_IRQ(irq))
833 if (IO_APIC_IRQ(irq)) {
834 vector = assign_irq_vector(irq);
835 entry.vector = vector;
837 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
838 if (!apic && (irq < 16))
839 disable_8259A_irq(irq);
841 spin_lock_irqsave(&ioapic_lock, flags);
842 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
843 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
844 set_native_irq_info(irq, TARGET_CPUS);
845 spin_unlock_irqrestore(&ioapic_lock, flags);
850 apic_printk(APIC_VERBOSE," not connected.\n");
854 * Set up the 8259A-master output pin as broadcast to all
857 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
859 struct IO_APIC_route_entry entry;
862 memset(&entry,0,sizeof(entry));
864 disable_8259A_irq(0);
867 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
870 * We use logical delivery to get the timer IRQ
873 entry.dest_mode = INT_DEST_MODE;
874 entry.mask = 0; /* unmask IRQ now */
875 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
876 entry.delivery_mode = INT_DELIVERY_MODE;
879 entry.vector = vector;
882 * The timer IRQ doesn't have to know that behind the
883 * scene we have a 8259A-master in AEOI mode ...
885 irq_desc[0].chip = &ioapic_edge_type;
888 * Add it to the IO-APIC irq-routing table:
890 spin_lock_irqsave(&ioapic_lock, flags);
891 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
892 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
893 spin_unlock_irqrestore(&ioapic_lock, flags);
898 void __init UNEXPECTED_IO_APIC(void)
902 void __apicdebuginit print_IO_APIC(void)
905 union IO_APIC_reg_00 reg_00;
906 union IO_APIC_reg_01 reg_01;
907 union IO_APIC_reg_02 reg_02;
910 if (apic_verbosity == APIC_QUIET)
913 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
914 for (i = 0; i < nr_ioapics; i++)
915 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
916 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
919 * We are a bit conservative about what we expect. We have to
920 * know about every hardware change ASAP.
922 printk(KERN_INFO "testing the IO APIC.......................\n");
924 for (apic = 0; apic < nr_ioapics; apic++) {
926 spin_lock_irqsave(&ioapic_lock, flags);
927 reg_00.raw = io_apic_read(apic, 0);
928 reg_01.raw = io_apic_read(apic, 1);
929 if (reg_01.bits.version >= 0x10)
930 reg_02.raw = io_apic_read(apic, 2);
931 spin_unlock_irqrestore(&ioapic_lock, flags);
934 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
935 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
936 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
937 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
938 UNEXPECTED_IO_APIC();
940 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
941 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
942 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
943 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
944 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
945 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
946 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
947 (reg_01.bits.entries != 0x2E) &&
948 (reg_01.bits.entries != 0x3F) &&
949 (reg_01.bits.entries != 0x03)
951 UNEXPECTED_IO_APIC();
953 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
954 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
955 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
956 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
957 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
958 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
959 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
960 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
962 UNEXPECTED_IO_APIC();
963 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
964 UNEXPECTED_IO_APIC();
966 if (reg_01.bits.version >= 0x10) {
967 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
968 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
969 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
970 UNEXPECTED_IO_APIC();
973 printk(KERN_DEBUG ".... IRQ redirection table:\n");
975 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
976 " Stat Dest Deli Vect: \n");
978 for (i = 0; i <= reg_01.bits.entries; i++) {
979 struct IO_APIC_route_entry entry;
981 spin_lock_irqsave(&ioapic_lock, flags);
982 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
983 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
984 spin_unlock_irqrestore(&ioapic_lock, flags);
986 printk(KERN_DEBUG " %02x %03X %02X ",
988 entry.dest.logical.logical_dest,
989 entry.dest.physical.physical_dest
992 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
997 entry.delivery_status,
1004 if (use_pci_vector())
1005 printk(KERN_INFO "Using vector-based indexing\n");
1006 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1007 for (i = 0; i < NR_IRQS; i++) {
1008 struct irq_pin_list *entry = irq_2_pin + i;
1011 if (use_pci_vector() && !platform_legacy_irq(i))
1012 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1014 printk(KERN_DEBUG "IRQ%d ", i);
1016 printk("-> %d:%d", entry->apic, entry->pin);
1019 entry = irq_2_pin + entry->next;
1024 printk(KERN_INFO ".................................... done.\n");
1031 static __apicdebuginit void print_APIC_bitfield (int base)
1036 if (apic_verbosity == APIC_QUIET)
1039 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1040 for (i = 0; i < 8; i++) {
1041 v = apic_read(base + i*0x10);
1042 for (j = 0; j < 32; j++) {
1052 void __apicdebuginit print_local_APIC(void * dummy)
1054 unsigned int v, ver, maxlvt;
1056 if (apic_verbosity == APIC_QUIET)
1059 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1060 smp_processor_id(), hard_smp_processor_id());
1061 v = apic_read(APIC_ID);
1062 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1063 v = apic_read(APIC_LVR);
1064 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1065 ver = GET_APIC_VERSION(v);
1066 maxlvt = get_maxlvt();
1068 v = apic_read(APIC_TASKPRI);
1069 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1071 v = apic_read(APIC_ARBPRI);
1072 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1073 v & APIC_ARBPRI_MASK);
1074 v = apic_read(APIC_PROCPRI);
1075 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1077 v = apic_read(APIC_EOI);
1078 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1079 v = apic_read(APIC_RRR);
1080 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1081 v = apic_read(APIC_LDR);
1082 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1083 v = apic_read(APIC_DFR);
1084 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1085 v = apic_read(APIC_SPIV);
1086 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1088 printk(KERN_DEBUG "... APIC ISR field:\n");
1089 print_APIC_bitfield(APIC_ISR);
1090 printk(KERN_DEBUG "... APIC TMR field:\n");
1091 print_APIC_bitfield(APIC_TMR);
1092 printk(KERN_DEBUG "... APIC IRR field:\n");
1093 print_APIC_bitfield(APIC_IRR);
1095 v = apic_read(APIC_ESR);
1096 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1098 v = apic_read(APIC_ICR);
1099 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1100 v = apic_read(APIC_ICR2);
1101 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1103 v = apic_read(APIC_LVTT);
1104 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1106 if (maxlvt > 3) { /* PC is LVT#4. */
1107 v = apic_read(APIC_LVTPC);
1108 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1110 v = apic_read(APIC_LVT0);
1111 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1112 v = apic_read(APIC_LVT1);
1113 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1115 if (maxlvt > 2) { /* ERR is LVT#3. */
1116 v = apic_read(APIC_LVTERR);
1117 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1120 v = apic_read(APIC_TMICT);
1121 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1122 v = apic_read(APIC_TMCCT);
1123 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1124 v = apic_read(APIC_TDCR);
1125 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1129 void print_all_local_APICs (void)
1131 on_each_cpu(print_local_APIC, NULL, 1, 1);
1134 void __apicdebuginit print_PIC(void)
1137 unsigned long flags;
1139 if (apic_verbosity == APIC_QUIET)
1142 printk(KERN_DEBUG "\nprinting PIC contents\n");
1144 spin_lock_irqsave(&i8259A_lock, flags);
1146 v = inb(0xa1) << 8 | inb(0x21);
1147 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1149 v = inb(0xa0) << 8 | inb(0x20);
1150 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1154 v = inb(0xa0) << 8 | inb(0x20);
1158 spin_unlock_irqrestore(&i8259A_lock, flags);
1160 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1162 v = inb(0x4d1) << 8 | inb(0x4d0);
1163 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1168 static void __init enable_IO_APIC(void)
1170 union IO_APIC_reg_01 reg_01;
1171 int i8259_apic, i8259_pin;
1173 unsigned long flags;
1175 for (i = 0; i < PIN_MAP_SIZE; i++) {
1176 irq_2_pin[i].pin = -1;
1177 irq_2_pin[i].next = 0;
1181 * The number of IO-APIC IRQ registers (== #pins):
1183 for (apic = 0; apic < nr_ioapics; apic++) {
1184 spin_lock_irqsave(&ioapic_lock, flags);
1185 reg_01.raw = io_apic_read(apic, 1);
1186 spin_unlock_irqrestore(&ioapic_lock, flags);
1187 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1189 for(apic = 0; apic < nr_ioapics; apic++) {
1191 /* See if any of the pins is in ExtINT mode */
1192 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1193 struct IO_APIC_route_entry entry;
1194 spin_lock_irqsave(&ioapic_lock, flags);
1195 *(((int *)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1196 *(((int *)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1197 spin_unlock_irqrestore(&ioapic_lock, flags);
1200 /* If the interrupt line is enabled and in ExtInt mode
1201 * I have found the pin where the i8259 is connected.
1203 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1204 ioapic_i8259.apic = apic;
1205 ioapic_i8259.pin = pin;
1211 /* Look to see what if the MP table has reported the ExtINT */
1212 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1213 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1214 /* Trust the MP table if nothing is setup in the hardware */
1215 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1216 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1217 ioapic_i8259.pin = i8259_pin;
1218 ioapic_i8259.apic = i8259_apic;
1220 /* Complain if the MP table and the hardware disagree */
1221 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1222 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1224 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1228 * Do not trust the IO-APIC being empty at bootup
1234 * Not an __init, needed by the reboot code
1236 void disable_IO_APIC(void)
1239 * Clear the IO-APIC before rebooting:
1244 * If the i8259 is routed through an IOAPIC
1245 * Put that IOAPIC in virtual wire mode
1246 * so legacy interrupts can be delivered.
1248 if (ioapic_i8259.pin != -1) {
1249 struct IO_APIC_route_entry entry;
1250 unsigned long flags;
1252 memset(&entry, 0, sizeof(entry));
1253 entry.mask = 0; /* Enabled */
1254 entry.trigger = 0; /* Edge */
1256 entry.polarity = 0; /* High */
1257 entry.delivery_status = 0;
1258 entry.dest_mode = 0; /* Physical */
1259 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1261 entry.dest.physical.physical_dest =
1262 GET_APIC_ID(apic_read(APIC_ID));
1265 * Add it to the IO-APIC irq-routing table:
1267 spin_lock_irqsave(&ioapic_lock, flags);
1268 io_apic_write(ioapic_i8259.apic, 0x11+2*ioapic_i8259.pin,
1269 *(((int *)&entry)+1));
1270 io_apic_write(ioapic_i8259.apic, 0x10+2*ioapic_i8259.pin,
1271 *(((int *)&entry)+0));
1272 spin_unlock_irqrestore(&ioapic_lock, flags);
1275 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1279 * function to set the IO-APIC physical IDs based on the
1280 * values stored in the MPC table.
1282 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1285 static void __init setup_ioapic_ids_from_mpc (void)
1287 union IO_APIC_reg_00 reg_00;
1290 unsigned char old_id;
1291 unsigned long flags;
1294 * Set the IOAPIC ID to the value stored in the MPC table.
1296 for (apic = 0; apic < nr_ioapics; apic++) {
1298 /* Read the register 0 value */
1299 spin_lock_irqsave(&ioapic_lock, flags);
1300 reg_00.raw = io_apic_read(apic, 0);
1301 spin_unlock_irqrestore(&ioapic_lock, flags);
1303 old_id = mp_ioapics[apic].mpc_apicid;
1306 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1310 * We need to adjust the IRQ routing table
1311 * if the ID changed.
1313 if (old_id != mp_ioapics[apic].mpc_apicid)
1314 for (i = 0; i < mp_irq_entries; i++)
1315 if (mp_irqs[i].mpc_dstapic == old_id)
1316 mp_irqs[i].mpc_dstapic
1317 = mp_ioapics[apic].mpc_apicid;
1320 * Read the right value from the MPC table and
1321 * write it into the ID register.
1323 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1324 mp_ioapics[apic].mpc_apicid);
1326 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1327 spin_lock_irqsave(&ioapic_lock, flags);
1328 io_apic_write(apic, 0, reg_00.raw);
1329 spin_unlock_irqrestore(&ioapic_lock, flags);
1334 spin_lock_irqsave(&ioapic_lock, flags);
1335 reg_00.raw = io_apic_read(apic, 0);
1336 spin_unlock_irqrestore(&ioapic_lock, flags);
1337 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1338 printk("could not set ID!\n");
1340 apic_printk(APIC_VERBOSE," ok.\n");
1345 * There is a nasty bug in some older SMP boards, their mptable lies
1346 * about the timer IRQ. We do the following to work around the situation:
1348 * - timer IRQ defaults to IO-APIC IRQ
1349 * - if this function detects that timer IRQs are defunct, then we fall
1350 * back to ISA timer IRQs
1352 static int __init timer_irq_works(void)
1354 unsigned long t1 = jiffies;
1357 /* Let ten ticks pass... */
1358 mdelay((10 * 1000) / HZ);
1361 * Expect a few ticks at least, to be sure some possible
1362 * glue logic does not lock up after one or two first
1363 * ticks in a non-ExtINT mode. Also the local APIC
1364 * might have cached one ExtINT interrupt. Finally, at
1365 * least one tick may be lost due to delays.
1369 if (jiffies - t1 > 4)
1375 * In the SMP+IOAPIC case it might happen that there are an unspecified
1376 * number of pending IRQ events unhandled. These cases are very rare,
1377 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1378 * better to do it this way as thus we do not have to be aware of
1379 * 'pending' interrupts in the IRQ path, except at this point.
1382 * Edge triggered needs to resend any interrupt
1383 * that was delayed but this is now handled in the device
1388 * Starting up a edge-triggered IO-APIC interrupt is
1389 * nasty - we need to make sure that we get the edge.
1390 * If it is already asserted for some reason, we need
1391 * return 1 to indicate that is was pending.
1393 * This is not complete - we should be able to fake
1394 * an edge even if it isn't on the 8259A...
1397 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1399 int was_pending = 0;
1400 unsigned long flags;
1402 spin_lock_irqsave(&ioapic_lock, flags);
1404 disable_8259A_irq(irq);
1405 if (i8259A_irq_pending(irq))
1408 __unmask_IO_APIC_irq(irq);
1409 spin_unlock_irqrestore(&ioapic_lock, flags);
1415 * Once we have recorded IRQ_PENDING already, we can mask the
1416 * interrupt for real. This prevents IRQ storms from unhandled
1419 static void ack_edge_ioapic_irq(unsigned int irq)
1422 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1423 == (IRQ_PENDING | IRQ_DISABLED))
1424 mask_IO_APIC_irq(irq);
1429 * Level triggered interrupts can just be masked,
1430 * and shutting down and starting up the interrupt
1431 * is the same as enabling and disabling them -- except
1432 * with a startup need to return a "was pending" value.
1434 * Level triggered interrupts are special because we
1435 * do not touch any IO-APIC register while handling
1436 * them. We ack the APIC in the end-IRQ handler, not
1437 * in the start-IRQ-handler. Protection against reentrance
1438 * from the same interrupt is still provided, both by the
1439 * generic IRQ layer and by the fact that an unacked local
1440 * APIC does not accept IRQs.
1442 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1444 unmask_IO_APIC_irq(irq);
1446 return 0; /* don't check for pending */
1449 static void end_level_ioapic_irq (unsigned int irq)
1455 #ifdef CONFIG_PCI_MSI
1456 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1458 int irq = vector_to_irq(vector);
1460 return startup_edge_ioapic_irq(irq);
1463 static void ack_edge_ioapic_vector(unsigned int vector)
1465 int irq = vector_to_irq(vector);
1467 move_native_irq(vector);
1468 ack_edge_ioapic_irq(irq);
1471 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1473 int irq = vector_to_irq(vector);
1475 return startup_level_ioapic_irq (irq);
1478 static void end_level_ioapic_vector (unsigned int vector)
1480 int irq = vector_to_irq(vector);
1482 move_native_irq(vector);
1483 end_level_ioapic_irq(irq);
1486 static void mask_IO_APIC_vector (unsigned int vector)
1488 int irq = vector_to_irq(vector);
1490 mask_IO_APIC_irq(irq);
1493 static void unmask_IO_APIC_vector (unsigned int vector)
1495 int irq = vector_to_irq(vector);
1497 unmask_IO_APIC_irq(irq);
1501 static void set_ioapic_affinity_vector (unsigned int vector,
1504 int irq = vector_to_irq(vector);
1506 set_native_irq_info(vector, cpu_mask);
1507 set_ioapic_affinity_irq(irq, cpu_mask);
1509 #endif // CONFIG_SMP
1510 #endif // CONFIG_PCI_MSI
1512 static int ioapic_retrigger(unsigned int irq)
1514 send_IPI_self(IO_APIC_VECTOR(irq));
1520 * Level and edge triggered IO-APIC interrupts need different handling,
1521 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1522 * handled with the level-triggered descriptor, but that one has slightly
1523 * more overhead. Level-triggered interrupts cannot be handled with the
1524 * edge-triggered handler, without risking IRQ storms and other ugly
1528 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1529 .typename = "IO-APIC-edge",
1530 .startup = startup_edge_ioapic,
1531 .shutdown = shutdown_edge_ioapic,
1532 .enable = enable_edge_ioapic,
1533 .disable = disable_edge_ioapic,
1534 .ack = ack_edge_ioapic,
1535 .end = end_edge_ioapic,
1537 .set_affinity = set_ioapic_affinity,
1539 .retrigger = ioapic_retrigger,
1542 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1543 .typename = "IO-APIC-level",
1544 .startup = startup_level_ioapic,
1545 .shutdown = shutdown_level_ioapic,
1546 .enable = enable_level_ioapic,
1547 .disable = disable_level_ioapic,
1548 .ack = mask_and_ack_level_ioapic,
1549 .end = end_level_ioapic,
1551 .set_affinity = set_ioapic_affinity,
1553 .retrigger = ioapic_retrigger,
1556 static inline void init_IO_APIC_traps(void)
1561 * NOTE! The local APIC isn't very good at handling
1562 * multiple interrupts at the same interrupt level.
1563 * As the interrupt level is determined by taking the
1564 * vector number and shifting that right by 4, we
1565 * want to spread these out a bit so that they don't
1566 * all fall in the same interrupt level.
1568 * Also, we've got to be careful not to trash gate
1569 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1571 for (irq = 0; irq < NR_IRQS ; irq++) {
1573 if (use_pci_vector()) {
1574 if (!platform_legacy_irq(tmp))
1575 if ((tmp = vector_to_irq(tmp)) == -1)
1578 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1580 * Hmm.. We don't have an entry for this,
1581 * so default to an old-fashioned 8259
1582 * interrupt if we can..
1585 make_8259A_irq(irq);
1587 /* Strange. Oh, well.. */
1588 irq_desc[irq].chip = &no_irq_type;
1593 static void enable_lapic_irq (unsigned int irq)
1597 v = apic_read(APIC_LVT0);
1598 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1601 static void disable_lapic_irq (unsigned int irq)
1605 v = apic_read(APIC_LVT0);
1606 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1609 static void ack_lapic_irq (unsigned int irq)
1614 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1616 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1617 .typename = "local-APIC-edge",
1618 .startup = NULL, /* startup_irq() not used for IRQ0 */
1619 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1620 .enable = enable_lapic_irq,
1621 .disable = disable_lapic_irq,
1622 .ack = ack_lapic_irq,
1623 .end = end_lapic_irq,
1626 static void setup_nmi (void)
1629 * Dirty trick to enable the NMI watchdog ...
1630 * We put the 8259A master into AEOI mode and
1631 * unmask on all local APICs LVT0 as NMI.
1633 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1634 * is from Maciej W. Rozycki - so we do not have to EOI from
1635 * the NMI handler or the timer interrupt.
1637 printk(KERN_INFO "activating NMI Watchdog ...");
1639 enable_NMI_through_LVT0(NULL);
1645 * This looks a bit hackish but it's about the only one way of sending
1646 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1647 * not support the ExtINT mode, unfortunately. We need to send these
1648 * cycles as some i82489DX-based boards have glue logic that keeps the
1649 * 8259A interrupt line asserted until INTA. --macro
1651 static inline void unlock_ExtINT_logic(void)
1654 struct IO_APIC_route_entry entry0, entry1;
1655 unsigned char save_control, save_freq_select;
1656 unsigned long flags;
1658 pin = find_isa_irq_pin(8, mp_INT);
1659 apic = find_isa_irq_apic(8, mp_INT);
1663 spin_lock_irqsave(&ioapic_lock, flags);
1664 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1665 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1666 spin_unlock_irqrestore(&ioapic_lock, flags);
1667 clear_IO_APIC_pin(apic, pin);
1669 memset(&entry1, 0, sizeof(entry1));
1671 entry1.dest_mode = 0; /* physical delivery */
1672 entry1.mask = 0; /* unmask IRQ now */
1673 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1674 entry1.delivery_mode = dest_ExtINT;
1675 entry1.polarity = entry0.polarity;
1679 spin_lock_irqsave(&ioapic_lock, flags);
1680 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1681 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1682 spin_unlock_irqrestore(&ioapic_lock, flags);
1684 save_control = CMOS_READ(RTC_CONTROL);
1685 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1686 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1688 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1693 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1697 CMOS_WRITE(save_control, RTC_CONTROL);
1698 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1699 clear_IO_APIC_pin(apic, pin);
1701 spin_lock_irqsave(&ioapic_lock, flags);
1702 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1703 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1704 spin_unlock_irqrestore(&ioapic_lock, flags);
1707 int timer_uses_ioapic_pin_0;
1710 * This code may look a bit paranoid, but it's supposed to cooperate with
1711 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1712 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1713 * fanatically on his truly buggy board.
1715 * FIXME: really need to revamp this for modern platforms only.
1717 static inline void check_timer(void)
1719 int apic1, pin1, apic2, pin2;
1723 * get/set the timer IRQ vector:
1725 disable_8259A_irq(0);
1726 vector = assign_irq_vector(0);
1727 set_intr_gate(vector, interrupt[0]);
1730 * Subtle, code in do_timer_interrupt() expects an AEOI
1731 * mode for the 8259A whenever interrupts are routed
1732 * through I/O APICs. Also IRQ0 has to be enabled in
1733 * the 8259A which implies the virtual wire has to be
1734 * disabled in the local APIC.
1736 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1738 if (timer_over_8254 > 0)
1739 enable_8259A_irq(0);
1741 pin1 = find_isa_irq_pin(0, mp_INT);
1742 apic1 = find_isa_irq_apic(0, mp_INT);
1743 pin2 = ioapic_i8259.pin;
1744 apic2 = ioapic_i8259.apic;
1747 timer_uses_ioapic_pin_0 = 1;
1749 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1750 vector, apic1, pin1, apic2, pin2);
1754 * Ok, does IRQ0 through the IOAPIC work?
1756 unmask_IO_APIC_irq(0);
1757 if (!no_timer_check && timer_irq_works()) {
1758 nmi_watchdog_default();
1759 if (nmi_watchdog == NMI_IO_APIC) {
1760 disable_8259A_irq(0);
1762 enable_8259A_irq(0);
1764 if (disable_timer_pin_1 > 0)
1765 clear_IO_APIC_pin(0, pin1);
1768 clear_IO_APIC_pin(apic1, pin1);
1769 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1770 "connected to IO-APIC\n");
1773 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1774 "through the 8259A ... ");
1776 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1779 * legacy devices should be connected to IO APIC #0
1781 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1782 if (timer_irq_works()) {
1783 apic_printk(APIC_VERBOSE," works.\n");
1784 nmi_watchdog_default();
1785 if (nmi_watchdog == NMI_IO_APIC) {
1791 * Cleanup, just in case ...
1793 clear_IO_APIC_pin(apic2, pin2);
1795 apic_printk(APIC_VERBOSE," failed.\n");
1797 if (nmi_watchdog == NMI_IO_APIC) {
1798 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1802 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1804 disable_8259A_irq(0);
1805 irq_desc[0].chip = &lapic_irq_type;
1806 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1807 enable_8259A_irq(0);
1809 if (timer_irq_works()) {
1810 apic_printk(APIC_VERBOSE," works.\n");
1813 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1814 apic_printk(APIC_VERBOSE," failed.\n");
1816 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1820 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1822 unlock_ExtINT_logic();
1824 if (timer_irq_works()) {
1825 apic_printk(APIC_VERBOSE," works.\n");
1828 apic_printk(APIC_VERBOSE," failed :(.\n");
1829 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1832 static int __init notimercheck(char *s)
1837 __setup("no_timer_check", notimercheck);
1841 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1842 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1843 * Linux doesn't really care, as it's not actually used
1844 * for any interrupt handling anyway.
1846 #define PIC_IRQS (1<<2)
1848 void __init setup_IO_APIC(void)
1853 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1855 io_apic_irqs = ~PIC_IRQS;
1857 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1860 * Set up the IO-APIC IRQ routing table.
1863 setup_ioapic_ids_from_mpc();
1865 setup_IO_APIC_irqs();
1866 init_IO_APIC_traps();
1872 struct sysfs_ioapic_data {
1873 struct sys_device dev;
1874 struct IO_APIC_route_entry entry[0];
1876 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1878 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1880 struct IO_APIC_route_entry *entry;
1881 struct sysfs_ioapic_data *data;
1882 unsigned long flags;
1885 data = container_of(dev, struct sysfs_ioapic_data, dev);
1886 entry = data->entry;
1887 spin_lock_irqsave(&ioapic_lock, flags);
1888 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1889 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1890 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1892 spin_unlock_irqrestore(&ioapic_lock, flags);
1897 static int ioapic_resume(struct sys_device *dev)
1899 struct IO_APIC_route_entry *entry;
1900 struct sysfs_ioapic_data *data;
1901 unsigned long flags;
1902 union IO_APIC_reg_00 reg_00;
1905 data = container_of(dev, struct sysfs_ioapic_data, dev);
1906 entry = data->entry;
1908 spin_lock_irqsave(&ioapic_lock, flags);
1909 reg_00.raw = io_apic_read(dev->id, 0);
1910 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1911 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1912 io_apic_write(dev->id, 0, reg_00.raw);
1914 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1915 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1916 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1918 spin_unlock_irqrestore(&ioapic_lock, flags);
1923 static struct sysdev_class ioapic_sysdev_class = {
1924 set_kset_name("ioapic"),
1925 .suspend = ioapic_suspend,
1926 .resume = ioapic_resume,
1929 static int __init ioapic_init_sysfs(void)
1931 struct sys_device * dev;
1932 int i, size, error = 0;
1934 error = sysdev_class_register(&ioapic_sysdev_class);
1938 for (i = 0; i < nr_ioapics; i++ ) {
1939 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1940 * sizeof(struct IO_APIC_route_entry);
1941 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1942 if (!mp_ioapic_data[i]) {
1943 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1946 memset(mp_ioapic_data[i], 0, size);
1947 dev = &mp_ioapic_data[i]->dev;
1949 dev->cls = &ioapic_sysdev_class;
1950 error = sysdev_register(dev);
1952 kfree(mp_ioapic_data[i]);
1953 mp_ioapic_data[i] = NULL;
1954 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1962 device_initcall(ioapic_init_sysfs);
1964 /* --------------------------------------------------------------------------
1965 ACPI-based IOAPIC Configuration
1966 -------------------------------------------------------------------------- */
1970 #define IO_APIC_MAX_ID 0xFE
1972 int __init io_apic_get_version (int ioapic)
1974 union IO_APIC_reg_01 reg_01;
1975 unsigned long flags;
1977 spin_lock_irqsave(&ioapic_lock, flags);
1978 reg_01.raw = io_apic_read(ioapic, 1);
1979 spin_unlock_irqrestore(&ioapic_lock, flags);
1981 return reg_01.bits.version;
1985 int __init io_apic_get_redir_entries (int ioapic)
1987 union IO_APIC_reg_01 reg_01;
1988 unsigned long flags;
1990 spin_lock_irqsave(&ioapic_lock, flags);
1991 reg_01.raw = io_apic_read(ioapic, 1);
1992 spin_unlock_irqrestore(&ioapic_lock, flags);
1994 return reg_01.bits.entries;
1998 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
2000 struct IO_APIC_route_entry entry;
2001 unsigned long flags;
2003 if (!IO_APIC_IRQ(irq)) {
2004 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2010 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2011 * Note that we mask (disable) IRQs now -- these get enabled when the
2012 * corresponding device driver registers for this IRQ.
2015 memset(&entry,0,sizeof(entry));
2017 entry.delivery_mode = INT_DELIVERY_MODE;
2018 entry.dest_mode = INT_DEST_MODE;
2019 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2020 entry.trigger = triggering;
2021 entry.polarity = polarity;
2022 entry.mask = 1; /* Disabled (masked) */
2024 irq = gsi_irq_sharing(irq);
2026 * IRQs < 16 are already in the irq_2_pin[] map
2029 add_pin_to_irq(irq, ioapic, pin);
2031 entry.vector = assign_irq_vector(irq);
2033 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2034 "IRQ %d Mode:%i Active:%i)\n", ioapic,
2035 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2036 triggering, polarity);
2038 ioapic_register_intr(irq, entry.vector, triggering);
2040 if (!ioapic && (irq < 16))
2041 disable_8259A_irq(irq);
2043 spin_lock_irqsave(&ioapic_lock, flags);
2044 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2045 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2046 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
2047 spin_unlock_irqrestore(&ioapic_lock, flags);
2052 #endif /* CONFIG_ACPI */
2056 * This function currently is only a helper for the i386 smp boot process where
2057 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2058 * so mask in all cases should simply be TARGET_CPUS
2061 void __init setup_ioapic_dest(void)
2063 int pin, ioapic, irq, irq_entry;
2065 if (skip_ioapic_setup == 1)
2068 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2069 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2070 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2071 if (irq_entry == -1)
2073 irq = pin_2_irq(irq_entry, ioapic, pin);
2074 set_ioapic_affinity_irq(irq, TARGET_CPUS);