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/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/sched.h>
29 #include <linux/config.h>
30 #include <linux/smp_lock.h>
31 #include <linux/mc146818rtc.h>
32 #include <linux/acpi.h>
37 #include <asm/proto.h>
39 int sis_apic_bug; /* not actually supported, dummy for compile */
41 #undef APIC_LOCKUP_DEBUG
43 #define APIC_LOCKUP_DEBUG
45 static spinlock_t ioapic_lock = SPIN_LOCK_UNLOCKED;
48 * # of IRQ routing registers
50 int nr_ioapic_registers[MAX_IO_APICS];
53 * Rough estimation of how many shared IRQs there are, can
56 #define MAX_PLUS_SHARED_IRQS NR_IRQS
57 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
60 * This is performance-critical, we want to do it O(1)
62 * the indexing order of this array favors 1:1 mappings
63 * between pins and IRQs.
66 static struct irq_pin_list {
67 short apic, pin, next;
68 } irq_2_pin[PIN_MAP_SIZE];
70 #ifdef CONFIG_PCI_USE_VECTOR
71 int vector_irq[NR_IRQS] = { [0 ... NR_IRQS -1] = -1};
72 #define vector_to_irq(vector) \
73 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
75 #define vector_to_irq(vector) (vector)
79 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
80 * shared ISA-space IRQs, so we have to support them. We are super
81 * fast in the common case, and fast for shared ISA-space IRQs.
83 static void __init add_pin_to_irq(unsigned int irq, int apic, int pin)
85 static int first_free_entry = NR_IRQS;
86 struct irq_pin_list *entry = irq_2_pin + irq;
89 entry = irq_2_pin + entry->next;
91 if (entry->pin != -1) {
92 entry->next = first_free_entry;
93 entry = irq_2_pin + entry->next;
94 if (++first_free_entry >= PIN_MAP_SIZE)
95 panic("io_apic.c: whoops");
101 #define __DO_ACTION(R, ACTION, FINAL) \
105 struct irq_pin_list *entry = irq_2_pin + irq; \
112 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
114 io_apic_modify(entry->apic, reg); \
117 entry = irq_2_pin + entry->next; \
122 #define DO_ACTION(name,R,ACTION, FINAL) \
124 static void name##_IO_APIC_irq (unsigned int irq) \
125 __DO_ACTION(R, ACTION, FINAL)
127 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
129 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
131 DO_ACTION( __mask_and_edge, 0, = (reg & 0xffff7fff) | 0x00010000, )
132 /* mask = 1, trigger = 0 */
133 DO_ACTION( __unmask_and_level, 0, = (reg & 0xfffeffff) | 0x00008000, )
134 /* mask = 0, trigger = 1 */
136 static void mask_IO_APIC_irq (unsigned int irq)
140 spin_lock_irqsave(&ioapic_lock, flags);
141 __mask_IO_APIC_irq(irq);
142 spin_unlock_irqrestore(&ioapic_lock, flags);
145 static void unmask_IO_APIC_irq (unsigned int irq)
149 spin_lock_irqsave(&ioapic_lock, flags);
150 __unmask_IO_APIC_irq(irq);
151 spin_unlock_irqrestore(&ioapic_lock, flags);
154 void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
156 struct IO_APIC_route_entry entry;
159 /* Check delivery_mode to be sure we're not clearing an SMI pin */
160 spin_lock_irqsave(&ioapic_lock, flags);
161 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
162 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
163 spin_unlock_irqrestore(&ioapic_lock, flags);
164 if (entry.delivery_mode == dest_SMI)
167 * Disable it in the IO-APIC irq-routing table:
169 memset(&entry, 0, sizeof(entry));
171 spin_lock_irqsave(&ioapic_lock, flags);
172 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
173 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
174 spin_unlock_irqrestore(&ioapic_lock, flags);
177 static void clear_IO_APIC (void)
181 for (apic = 0; apic < nr_ioapics; apic++)
182 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
183 clear_IO_APIC_pin(apic, pin);
187 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
188 * specific CPU-side IRQs.
192 int pirq_entries [MAX_PIRQS];
194 int skip_ioapic_setup;
197 /* dummy parsing: see setup.c */
199 static int __init disable_ioapic_setup(char *str)
201 skip_ioapic_setup = 1;
205 static int __init enable_ioapic_setup(char *str)
208 skip_ioapic_setup = 0;
212 __setup("noapic", disable_ioapic_setup);
213 __setup("apic", enable_ioapic_setup);
215 #include <asm/pci-direct.h>
216 #include <linux/pci_ids.h>
217 #include <linux/pci.h>
219 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
220 off. Check for an Nvidia or VIA PCI bridge and turn it off.
221 Use pci direct infrastructure because this runs before the PCI subsystem.
223 Can be overwritten with "apic"
225 And another hack to disable the IOMMU on VIA chipsets.
228 void __init check_ioapic(void)
234 /* Poor man's PCI discovery */
235 for (num = 0; num < 32; num++) {
236 for (slot = 0; slot < 32; slot++) {
237 for (func = 0; func < 8; func++) {
241 class = read_pci_config(num,slot,func,
243 if (class == 0xffffffff)
246 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
249 vendor = read_pci_config(num, slot, func,
253 case PCI_VENDOR_ID_VIA:
254 #ifdef CONFIG_GART_IOMMU
255 if (end_pfn >= (0xffffffff>>PAGE_SHIFT) &&
256 !iommu_aperture_allowed) {
258 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
259 iommu_aperture_disabled = 1;
263 case PCI_VENDOR_ID_NVIDIA:
266 "PCI bridge %02x:%02x from %x found. Setting \"noapic\". Overwrite with \"apic\"\n",
268 skip_ioapic_setup = 1;
273 /* No multi-function device? */
274 type = read_pci_config_byte(num,slot,func,
283 static int __init ioapic_pirq_setup(char *str)
286 int ints[MAX_PIRQS+1];
288 get_options(str, ARRAY_SIZE(ints), ints);
290 for (i = 0; i < MAX_PIRQS; i++)
291 pirq_entries[i] = -1;
294 printk(KERN_INFO "PIRQ redirection, working around broken MP-BIOS.\n");
296 if (ints[0] < MAX_PIRQS)
299 for (i = 0; i < max; i++) {
300 printk(KERN_DEBUG "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
302 * PIRQs are mapped upside down, usually.
304 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
309 __setup("pirq=", ioapic_pirq_setup);
312 * Find the IRQ entry number of a certain pin.
314 static int __init find_irq_entry(int apic, int pin, int type)
318 for (i = 0; i < mp_irq_entries; i++)
319 if (mp_irqs[i].mpc_irqtype == type &&
320 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
321 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
322 mp_irqs[i].mpc_dstirq == pin)
329 * Find the pin to which IRQ[irq] (ISA) is connected
331 static int __init find_isa_irq_pin(int irq, int type)
335 for (i = 0; i < mp_irq_entries; i++) {
336 int lbus = mp_irqs[i].mpc_srcbus;
338 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
339 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
340 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
341 (mp_irqs[i].mpc_irqtype == type) &&
342 (mp_irqs[i].mpc_srcbusirq == irq))
344 return mp_irqs[i].mpc_dstirq;
350 * Find a specific PCI IRQ entry.
351 * Not an __init, possibly needed by modules
353 static int pin_2_irq(int idx, int apic, int pin);
355 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
357 int apic, i, best_guess = -1;
359 Dprintk("querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
361 if (mp_bus_id_to_pci_bus[bus] == -1) {
362 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
365 for (i = 0; i < mp_irq_entries; i++) {
366 int lbus = mp_irqs[i].mpc_srcbus;
368 for (apic = 0; apic < nr_ioapics; apic++)
369 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
370 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
373 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
374 !mp_irqs[i].mpc_irqtype &&
376 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
377 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
379 if (!(apic || IO_APIC_IRQ(irq)))
382 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
385 * Use the first all-but-pin matching entry as a
386 * best-guess fuzzy result for broken mptables.
396 * EISA Edge/Level control register, ELCR
398 static int __init EISA_ELCR(unsigned int irq)
401 unsigned int port = 0x4d0 + (irq >> 3);
402 return (inb(port) >> (irq & 7)) & 1;
404 printk(KERN_INFO "Broken MPtable reports ISA irq %d\n", irq);
408 /* EISA interrupts are always polarity zero and can be edge or level
409 * trigger depending on the ELCR value. If an interrupt is listed as
410 * EISA conforming in the MP table, that means its trigger type must
411 * be read in from the ELCR */
413 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
414 #define default_EISA_polarity(idx) (0)
416 /* ISA interrupts are always polarity zero edge triggered,
417 * when listed as conforming in the MP table. */
419 #define default_ISA_trigger(idx) (0)
420 #define default_ISA_polarity(idx) (0)
422 /* PCI interrupts are always polarity one level triggered,
423 * when listed as conforming in the MP table. */
425 #define default_PCI_trigger(idx) (1)
426 #define default_PCI_polarity(idx) (1)
428 /* MCA interrupts are always polarity zero level triggered,
429 * when listed as conforming in the MP table. */
431 #define default_MCA_trigger(idx) (1)
432 #define default_MCA_polarity(idx) (0)
434 static int __init MPBIOS_polarity(int idx)
436 int bus = mp_irqs[idx].mpc_srcbus;
440 * Determine IRQ line polarity (high active or low active):
442 switch (mp_irqs[idx].mpc_irqflag & 3)
444 case 0: /* conforms, ie. bus-type dependent polarity */
446 switch (mp_bus_id_to_type[bus])
448 case MP_BUS_ISA: /* ISA pin */
450 polarity = default_ISA_polarity(idx);
453 case MP_BUS_EISA: /* EISA pin */
455 polarity = default_EISA_polarity(idx);
458 case MP_BUS_PCI: /* PCI pin */
460 polarity = default_PCI_polarity(idx);
463 case MP_BUS_MCA: /* MCA pin */
465 polarity = default_MCA_polarity(idx);
470 printk(KERN_WARNING "broken BIOS!!\n");
477 case 1: /* high active */
482 case 2: /* reserved */
484 printk(KERN_WARNING "broken BIOS!!\n");
488 case 3: /* low active */
493 default: /* invalid */
495 printk(KERN_WARNING "broken BIOS!!\n");
503 static int __init MPBIOS_trigger(int idx)
505 int bus = mp_irqs[idx].mpc_srcbus;
509 * Determine IRQ trigger mode (edge or level sensitive):
511 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
513 case 0: /* conforms, ie. bus-type dependent */
515 switch (mp_bus_id_to_type[bus])
517 case MP_BUS_ISA: /* ISA pin */
519 trigger = default_ISA_trigger(idx);
522 case MP_BUS_EISA: /* EISA pin */
524 trigger = default_EISA_trigger(idx);
527 case MP_BUS_PCI: /* PCI pin */
529 trigger = default_PCI_trigger(idx);
532 case MP_BUS_MCA: /* MCA pin */
534 trigger = default_MCA_trigger(idx);
539 printk(KERN_WARNING "broken BIOS!!\n");
551 case 2: /* reserved */
553 printk(KERN_WARNING "broken BIOS!!\n");
562 default: /* invalid */
564 printk(KERN_WARNING "broken BIOS!!\n");
572 static inline int irq_polarity(int idx)
574 return MPBIOS_polarity(idx);
577 static inline int irq_trigger(int idx)
579 return MPBIOS_trigger(idx);
582 static int pin_2_irq(int idx, int apic, int pin)
585 int bus = mp_irqs[idx].mpc_srcbus;
588 * Debugging check, we are in big trouble if this message pops up!
590 if (mp_irqs[idx].mpc_dstirq != pin)
591 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
593 switch (mp_bus_id_to_type[bus])
595 case MP_BUS_ISA: /* ISA pin */
599 irq = mp_irqs[idx].mpc_srcbusirq;
602 case MP_BUS_PCI: /* PCI pin */
605 * PCI IRQs are mapped in order
609 irq += nr_ioapic_registers[i++];
615 printk(KERN_ERR "unknown bus type %d.\n",bus);
622 * PCI IRQ command line redirection. Yes, limits are hardcoded.
624 if ((pin >= 16) && (pin <= 23)) {
625 if (pirq_entries[pin-16] != -1) {
626 if (!pirq_entries[pin-16]) {
627 printk(KERN_DEBUG "disabling PIRQ%d\n", pin-16);
629 irq = pirq_entries[pin-16];
630 printk(KERN_DEBUG "using PIRQ%d -> IRQ %d\n",
638 static inline int IO_APIC_irq_trigger(int irq)
642 for (apic = 0; apic < nr_ioapics; apic++) {
643 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
644 idx = find_irq_entry(apic,pin,mp_INT);
645 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
646 return irq_trigger(idx);
650 * nonexistent IRQs are edge default
655 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
656 u8 irq_vector[NR_IRQ_VECTORS] = { FIRST_DEVICE_VECTOR , 0 };
658 #ifndef CONFIG_PCI_USE_VECTOR
659 int __init assign_irq_vector(int irq)
661 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
662 BUG_ON(irq >= NR_IRQ_VECTORS);
663 if (IO_APIC_VECTOR(irq) > 0)
664 return IO_APIC_VECTOR(irq);
667 if (current_vector == IA32_SYSCALL_VECTOR)
670 if (current_vector > FIRST_SYSTEM_VECTOR) {
672 current_vector = FIRST_DEVICE_VECTOR + offset;
675 if (current_vector == FIRST_SYSTEM_VECTOR)
676 panic("ran out of interrupt sources!");
678 IO_APIC_VECTOR(irq) = current_vector;
679 return current_vector;
683 extern void (*interrupt[NR_IRQS])(void);
684 static struct hw_interrupt_type ioapic_level_type;
685 static struct hw_interrupt_type ioapic_edge_type;
687 #define IOAPIC_AUTO -1
688 #define IOAPIC_EDGE 0
689 #define IOAPIC_LEVEL 1
691 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
693 if (use_pci_vector() && !platform_legacy_irq(irq)) {
694 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
695 trigger == IOAPIC_LEVEL)
696 irq_desc[vector].handler = &ioapic_level_type;
698 irq_desc[vector].handler = &ioapic_edge_type;
699 set_intr_gate(vector, interrupt[vector]);
701 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
702 trigger == IOAPIC_LEVEL)
703 irq_desc[irq].handler = &ioapic_level_type;
705 irq_desc[irq].handler = &ioapic_edge_type;
706 set_intr_gate(vector, interrupt[irq]);
710 void __init setup_IO_APIC_irqs(void)
712 struct IO_APIC_route_entry entry;
713 int apic, pin, idx, irq, first_notcon = 1, vector;
716 printk(KERN_DEBUG "init IO_APIC IRQs\n");
718 for (apic = 0; apic < nr_ioapics; apic++) {
719 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
722 * add it to the IO-APIC irq-routing table:
724 memset(&entry,0,sizeof(entry));
726 entry.delivery_mode = dest_LowestPrio;
727 entry.dest_mode = INT_DELIVERY_MODE;
728 entry.mask = 0; /* enable IRQ */
729 entry.dest.logical.logical_dest = TARGET_CPUS;
731 idx = find_irq_entry(apic,pin,mp_INT);
734 printk(KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
737 printk(", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
741 entry.trigger = irq_trigger(idx);
742 entry.polarity = irq_polarity(idx);
744 if (irq_trigger(idx)) {
747 entry.dest.logical.logical_dest = TARGET_CPUS;
750 irq = pin_2_irq(idx, apic, pin);
751 add_pin_to_irq(irq, apic, pin);
753 if (!apic && !IO_APIC_IRQ(irq))
756 if (IO_APIC_IRQ(irq)) {
757 vector = assign_irq_vector(irq);
758 entry.vector = vector;
760 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
761 if (!apic && (irq < 16))
762 disable_8259A_irq(irq);
764 spin_lock_irqsave(&ioapic_lock, flags);
765 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
766 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
767 spin_unlock_irqrestore(&ioapic_lock, flags);
772 printk(" not connected.\n");
776 * Set up the 8259A-master output pin as broadcast to all
779 void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
781 struct IO_APIC_route_entry entry;
784 memset(&entry,0,sizeof(entry));
786 disable_8259A_irq(0);
789 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
792 * We use logical delivery to get the timer IRQ
795 entry.dest_mode = INT_DELIVERY_MODE;
796 entry.mask = 0; /* unmask IRQ now */
797 entry.dest.logical.logical_dest = TARGET_CPUS;
798 entry.delivery_mode = dest_LowestPrio;
801 entry.vector = vector;
804 * The timer IRQ doesn't have to know that behind the
805 * scene we have a 8259A-master in AEOI mode ...
807 irq_desc[0].handler = &ioapic_edge_type;
810 * Add it to the IO-APIC irq-routing table:
812 spin_lock_irqsave(&ioapic_lock, flags);
813 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
814 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
815 spin_unlock_irqrestore(&ioapic_lock, flags);
820 void __init UNEXPECTED_IO_APIC(void)
823 printk(KERN_WARNING " WARNING: unexpected IO-APIC, please mail\n");
824 printk(KERN_WARNING " to linux-smp@vger.kernel.org\n");
828 void __init print_IO_APIC(void)
831 union IO_APIC_reg_00 reg_00;
832 union IO_APIC_reg_01 reg_01;
833 union IO_APIC_reg_02 reg_02;
836 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
837 for (i = 0; i < nr_ioapics; i++)
838 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
839 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
842 * We are a bit conservative about what we expect. We have to
843 * know about every hardware change ASAP.
845 printk(KERN_INFO "testing the IO APIC.......................\n");
847 for (apic = 0; apic < nr_ioapics; apic++) {
849 spin_lock_irqsave(&ioapic_lock, flags);
850 reg_00.raw = io_apic_read(apic, 0);
851 reg_01.raw = io_apic_read(apic, 1);
852 if (reg_01.bits.version >= 0x10)
853 reg_02.raw = io_apic_read(apic, 2);
854 spin_unlock_irqrestore(&ioapic_lock, flags);
857 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
858 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
859 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
860 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
861 UNEXPECTED_IO_APIC();
863 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
864 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
865 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
866 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
867 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
868 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
869 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
870 (reg_01.bits.entries != 0x2E) &&
871 (reg_01.bits.entries != 0x3F) &&
872 (reg_01.bits.entries != 0x03)
874 UNEXPECTED_IO_APIC();
876 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
877 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
878 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
879 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
880 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
881 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
882 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
883 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
885 UNEXPECTED_IO_APIC();
886 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
887 UNEXPECTED_IO_APIC();
889 if (reg_01.bits.version >= 0x10) {
890 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
891 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
892 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
893 UNEXPECTED_IO_APIC();
896 printk(KERN_DEBUG ".... IRQ redirection table:\n");
898 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
899 " Stat Dest Deli Vect: \n");
901 for (i = 0; i <= reg_01.bits.entries; i++) {
902 struct IO_APIC_route_entry entry;
904 spin_lock_irqsave(&ioapic_lock, flags);
905 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
906 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
907 spin_unlock_irqrestore(&ioapic_lock, flags);
909 printk(KERN_DEBUG " %02x %03X %02X ",
911 entry.dest.logical.logical_dest,
912 entry.dest.physical.physical_dest
915 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
920 entry.delivery_status,
927 printk(KERN_DEBUG "IRQ to pin mappings:\n");
928 for (i = 0; i < NR_IRQS; i++) {
929 struct irq_pin_list *entry = irq_2_pin + i;
932 printk(KERN_DEBUG "IRQ%d ", i);
934 printk("-> %d:%d", entry->apic, entry->pin);
937 entry = irq_2_pin + entry->next;
942 printk(KERN_INFO ".................................... done.\n");
947 static void print_APIC_bitfield (int base)
952 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
953 for (i = 0; i < 8; i++) {
954 v = apic_read(base + i*0x10);
955 for (j = 0; j < 32; j++) {
965 void /*__init*/ print_local_APIC(void * dummy)
967 unsigned int v, ver, maxlvt;
969 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
970 smp_processor_id(), hard_smp_processor_id());
971 v = apic_read(APIC_ID);
972 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
973 v = apic_read(APIC_LVR);
974 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
975 ver = GET_APIC_VERSION(v);
976 maxlvt = get_maxlvt();
978 v = apic_read(APIC_TASKPRI);
979 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
981 if (APIC_INTEGRATED(ver)) { /* !82489DX */
982 v = apic_read(APIC_ARBPRI);
983 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
984 v & APIC_ARBPRI_MASK);
985 v = apic_read(APIC_PROCPRI);
986 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
989 v = apic_read(APIC_EOI);
990 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
991 v = apic_read(APIC_RRR);
992 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
993 v = apic_read(APIC_LDR);
994 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
995 v = apic_read(APIC_DFR);
996 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
997 v = apic_read(APIC_SPIV);
998 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1000 printk(KERN_DEBUG "... APIC ISR field:\n");
1001 print_APIC_bitfield(APIC_ISR);
1002 printk(KERN_DEBUG "... APIC TMR field:\n");
1003 print_APIC_bitfield(APIC_TMR);
1004 printk(KERN_DEBUG "... APIC IRR field:\n");
1005 print_APIC_bitfield(APIC_IRR);
1007 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1008 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1009 apic_write(APIC_ESR, 0);
1010 v = apic_read(APIC_ESR);
1011 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1014 v = apic_read(APIC_ICR);
1015 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1016 v = apic_read(APIC_ICR2);
1017 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1019 v = apic_read(APIC_LVTT);
1020 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1022 if (maxlvt > 3) { /* PC is LVT#4. */
1023 v = apic_read(APIC_LVTPC);
1024 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1026 v = apic_read(APIC_LVT0);
1027 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1028 v = apic_read(APIC_LVT1);
1029 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1031 if (maxlvt > 2) { /* ERR is LVT#3. */
1032 v = apic_read(APIC_LVTERR);
1033 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1036 v = apic_read(APIC_TMICT);
1037 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1038 v = apic_read(APIC_TMCCT);
1039 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1040 v = apic_read(APIC_TDCR);
1041 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1045 void print_all_local_APICs (void)
1047 on_each_cpu(print_local_APIC, NULL, 1, 1);
1050 void /*__init*/ print_PIC(void)
1052 extern spinlock_t i8259A_lock;
1054 unsigned long flags;
1056 printk(KERN_DEBUG "\nprinting PIC contents\n");
1058 spin_lock_irqsave(&i8259A_lock, flags);
1060 v = inb(0xa1) << 8 | inb(0x21);
1061 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1063 v = inb(0xa0) << 8 | inb(0x20);
1064 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1068 v = inb(0xa0) << 8 | inb(0x20);
1072 spin_unlock_irqrestore(&i8259A_lock, flags);
1074 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1076 v = inb(0x4d1) << 8 | inb(0x4d0);
1077 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1080 static void __init enable_IO_APIC(void)
1082 union IO_APIC_reg_01 reg_01;
1084 unsigned long flags;
1086 for (i = 0; i < PIN_MAP_SIZE; i++) {
1087 irq_2_pin[i].pin = -1;
1088 irq_2_pin[i].next = 0;
1091 for (i = 0; i < MAX_PIRQS; i++)
1092 pirq_entries[i] = -1;
1095 * The number of IO-APIC IRQ registers (== #pins):
1097 for (i = 0; i < nr_ioapics; i++) {
1098 spin_lock_irqsave(&ioapic_lock, flags);
1099 reg_01.raw = io_apic_read(i, 1);
1100 spin_unlock_irqrestore(&ioapic_lock, flags);
1101 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1105 * Do not trust the IO-APIC being empty at bootup
1111 * Not an __init, needed by the reboot code
1113 void disable_IO_APIC(void)
1116 * Clear the IO-APIC before rebooting:
1120 disconnect_bsp_APIC();
1124 * function to set the IO-APIC physical IDs based on the
1125 * values stored in the MPC table.
1127 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1130 static void __init setup_ioapic_ids_from_mpc (void)
1132 union IO_APIC_reg_00 reg_00;
1133 physid_mask_t phys_id_present_map = phys_cpu_present_map;
1136 unsigned char old_id;
1137 unsigned long flags;
1140 * Set the IOAPIC ID to the value stored in the MPC table.
1142 for (apic = 0; apic < nr_ioapics; apic++) {
1144 /* Read the register 0 value */
1145 spin_lock_irqsave(&ioapic_lock, flags);
1146 reg_00.raw = io_apic_read(apic, 0);
1147 spin_unlock_irqrestore(&ioapic_lock, flags);
1149 old_id = mp_ioapics[apic].mpc_apicid;
1151 if (mp_ioapics[apic].mpc_apicid >= 0xf) {
1152 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1153 apic, mp_ioapics[apic].mpc_apicid);
1154 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1156 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1160 * Sanity check, is the ID really free? Every APIC in a
1161 * system must have a unique ID or we get lots of nice
1162 * 'stuck on smp_invalidate_needed IPI wait' messages.
1164 if (physid_isset(mp_ioapics[apic].mpc_apicid, phys_id_present_map)) {
1165 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1166 apic, mp_ioapics[apic].mpc_apicid);
1167 for (i = 0; i < 0xf; i++)
1168 if (!physid_isset(i, phys_id_present_map))
1171 panic("Max APIC ID exceeded!\n");
1172 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1174 physid_set(i, phys_id_present_map);
1175 mp_ioapics[apic].mpc_apicid = i;
1178 "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1179 physid_set(mp_ioapics[apic].mpc_apicid, phys_id_present_map);
1184 * We need to adjust the IRQ routing table
1185 * if the ID changed.
1187 if (old_id != mp_ioapics[apic].mpc_apicid)
1188 for (i = 0; i < mp_irq_entries; i++)
1189 if (mp_irqs[i].mpc_dstapic == old_id)
1190 mp_irqs[i].mpc_dstapic
1191 = mp_ioapics[apic].mpc_apicid;
1194 * Read the right value from the MPC table and
1195 * write it into the ID register.
1197 printk(KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1198 mp_ioapics[apic].mpc_apicid);
1200 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1201 spin_lock_irqsave(&ioapic_lock, flags);
1202 io_apic_write(apic, 0, reg_00.raw);
1203 spin_unlock_irqrestore(&ioapic_lock, flags);
1208 spin_lock_irqsave(&ioapic_lock, flags);
1209 reg_00.raw = io_apic_read(apic, 0);
1210 spin_unlock_irqrestore(&ioapic_lock, flags);
1211 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1212 panic("could not set ID!\n");
1219 * There is a nasty bug in some older SMP boards, their mptable lies
1220 * about the timer IRQ. We do the following to work around the situation:
1222 * - timer IRQ defaults to IO-APIC IRQ
1223 * - if this function detects that timer IRQs are defunct, then we fall
1224 * back to ISA timer IRQs
1226 static int __init timer_irq_works(void)
1228 unsigned long t1 = jiffies;
1231 /* Let ten ticks pass... */
1232 mdelay((10 * 1000) / HZ);
1235 * Expect a few ticks at least, to be sure some possible
1236 * glue logic does not lock up after one or two first
1237 * ticks in a non-ExtINT mode. Also the local APIC
1238 * might have cached one ExtINT interrupt. Finally, at
1239 * least one tick may be lost due to delays.
1243 if (jiffies - t1 > 4)
1249 * In the SMP+IOAPIC case it might happen that there are an unspecified
1250 * number of pending IRQ events unhandled. These cases are very rare,
1251 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1252 * better to do it this way as thus we do not have to be aware of
1253 * 'pending' interrupts in the IRQ path, except at this point.
1256 * Edge triggered needs to resend any interrupt
1257 * that was delayed but this is now handled in the device
1262 * Starting up a edge-triggered IO-APIC interrupt is
1263 * nasty - we need to make sure that we get the edge.
1264 * If it is already asserted for some reason, we need
1265 * return 1 to indicate that is was pending.
1267 * This is not complete - we should be able to fake
1268 * an edge even if it isn't on the 8259A...
1271 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1273 int was_pending = 0;
1274 unsigned long flags;
1276 spin_lock_irqsave(&ioapic_lock, flags);
1278 disable_8259A_irq(irq);
1279 if (i8259A_irq_pending(irq))
1282 __unmask_IO_APIC_irq(irq);
1283 spin_unlock_irqrestore(&ioapic_lock, flags);
1289 * Once we have recorded IRQ_PENDING already, we can mask the
1290 * interrupt for real. This prevents IRQ storms from unhandled
1293 static void ack_edge_ioapic_irq(unsigned int irq)
1295 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1296 == (IRQ_PENDING | IRQ_DISABLED))
1297 mask_IO_APIC_irq(irq);
1302 * Level triggered interrupts can just be masked,
1303 * and shutting down and starting up the interrupt
1304 * is the same as enabling and disabling them -- except
1305 * with a startup need to return a "was pending" value.
1307 * Level triggered interrupts are special because we
1308 * do not touch any IO-APIC register while handling
1309 * them. We ack the APIC in the end-IRQ handler, not
1310 * in the start-IRQ-handler. Protection against reentrance
1311 * from the same interrupt is still provided, both by the
1312 * generic IRQ layer and by the fact that an unacked local
1313 * APIC does not accept IRQs.
1315 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1317 unmask_IO_APIC_irq(irq);
1319 return 0; /* don't check for pending */
1322 static void end_level_ioapic_irq (unsigned int irq)
1328 * It appears there is an erratum which affects at least version 0x11
1329 * of I/O APIC (that's the 82093AA and cores integrated into various
1330 * chipsets). Under certain conditions a level-triggered interrupt is
1331 * erroneously delivered as edge-triggered one but the respective IRR
1332 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1333 * message but it will never arrive and further interrupts are blocked
1334 * from the source. The exact reason is so far unknown, but the
1335 * phenomenon was observed when two consecutive interrupt requests
1336 * from a given source get delivered to the same CPU and the source is
1337 * temporarily disabled in between.
1339 * A workaround is to simulate an EOI message manually. We achieve it
1340 * by setting the trigger mode to edge and then to level when the edge
1341 * trigger mode gets detected in the TMR of a local APIC for a
1342 * level-triggered interrupt. We mask the source for the time of the
1343 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1344 * The idea is from Manfred Spraul. --macro
1346 i = IO_APIC_VECTOR(irq);
1347 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1351 if (!(v & (1 << (i & 0x1f)))) {
1352 #ifdef APIC_LOCKUP_DEBUG
1353 struct irq_pin_list *entry;
1356 #ifdef APIC_MISMATCH_DEBUG
1357 atomic_inc(&irq_mis_count);
1359 spin_lock(&ioapic_lock);
1360 __mask_and_edge_IO_APIC_irq(irq);
1361 #ifdef APIC_LOCKUP_DEBUG
1362 for (entry = irq_2_pin + irq;;) {
1365 if (entry->pin == -1)
1367 reg = io_apic_read(entry->apic, 0x10 + entry->pin * 2);
1368 if (reg & 0x00004000)
1369 printk(KERN_CRIT "Aieee!!! Remote IRR"
1370 " still set after unlock!\n");
1373 entry = irq_2_pin + entry->next;
1376 __unmask_and_level_IO_APIC_irq(irq);
1377 spin_unlock(&ioapic_lock);
1381 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
1383 unsigned long flags;
1386 dest = cpu_mask_to_apicid(mk_cpumask_const(mask));
1389 * Only the first 8 bits are valid.
1393 spin_lock_irqsave(&ioapic_lock, flags);
1394 __DO_ACTION(1, = dest, )
1395 spin_unlock_irqrestore(&ioapic_lock, flags);
1398 #ifdef CONFIG_PCI_USE_VECTOR
1399 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1401 int irq = vector_to_irq(vector);
1403 return startup_edge_ioapic_irq(irq);
1406 static void ack_edge_ioapic_vector(unsigned int vector)
1408 int irq = vector_to_irq(vector);
1410 ack_edge_ioapic_irq(irq);
1413 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1415 int irq = vector_to_irq(vector);
1417 return startup_level_ioapic_irq (irq);
1420 static void end_level_ioapic_vector (unsigned int vector)
1422 int irq = vector_to_irq(vector);
1424 end_level_ioapic_irq(irq);
1427 static void mask_IO_APIC_vector (unsigned int vector)
1429 int irq = vector_to_irq(vector);
1431 mask_IO_APIC_irq(irq);
1434 static void unmask_IO_APIC_vector (unsigned int vector)
1436 int irq = vector_to_irq(vector);
1438 unmask_IO_APIC_irq(irq);
1441 static void set_ioapic_affinity_vector (unsigned int vector,
1444 int irq = vector_to_irq(vector);
1446 set_ioapic_affinity_irq(irq, cpu_mask);
1451 * Level and edge triggered IO-APIC interrupts need different handling,
1452 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1453 * handled with the level-triggered descriptor, but that one has slightly
1454 * more overhead. Level-triggered interrupts cannot be handled with the
1455 * edge-triggered handler, without risking IRQ storms and other ugly
1459 static struct hw_interrupt_type ioapic_edge_type = {
1460 .typename = "IO-APIC-edge",
1461 .startup = startup_edge_ioapic,
1462 .shutdown = shutdown_edge_ioapic,
1463 .enable = enable_edge_ioapic,
1464 .disable = disable_edge_ioapic,
1465 .ack = ack_edge_ioapic,
1466 .end = end_edge_ioapic,
1467 .set_affinity = set_ioapic_affinity,
1470 static struct hw_interrupt_type ioapic_level_type = {
1471 .typename = "IO-APIC-level",
1472 .startup = startup_level_ioapic,
1473 .shutdown = shutdown_level_ioapic,
1474 .enable = enable_level_ioapic,
1475 .disable = disable_level_ioapic,
1476 .ack = mask_and_ack_level_ioapic,
1477 .end = end_level_ioapic,
1478 .set_affinity = set_ioapic_affinity,
1481 static inline void init_IO_APIC_traps(void)
1486 * NOTE! The local APIC isn't very good at handling
1487 * multiple interrupts at the same interrupt level.
1488 * As the interrupt level is determined by taking the
1489 * vector number and shifting that right by 4, we
1490 * want to spread these out a bit so that they don't
1491 * all fall in the same interrupt level.
1493 * Also, we've got to be careful not to trash gate
1494 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1496 for (irq = 0; irq < NR_IRQS ; irq++) {
1498 if (use_pci_vector()) {
1499 if (!platform_legacy_irq(tmp))
1500 if ((tmp = vector_to_irq(tmp)) == -1)
1503 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1505 * Hmm.. We don't have an entry for this,
1506 * so default to an old-fashioned 8259
1507 * interrupt if we can..
1510 make_8259A_irq(irq);
1512 /* Strange. Oh, well.. */
1513 irq_desc[irq].handler = &no_irq_type;
1518 static void enable_lapic_irq (unsigned int irq)
1522 v = apic_read(APIC_LVT0);
1523 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1526 static void disable_lapic_irq (unsigned int irq)
1530 v = apic_read(APIC_LVT0);
1531 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1534 static void ack_lapic_irq (unsigned int irq)
1539 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1541 static struct hw_interrupt_type lapic_irq_type = {
1542 .typename = "local-APIC-edge",
1543 .startup = NULL, /* startup_irq() not used for IRQ0 */
1544 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1545 .enable = enable_lapic_irq,
1546 .disable = disable_lapic_irq,
1547 .ack = ack_lapic_irq,
1548 .end = end_lapic_irq,
1551 static void setup_nmi (void)
1554 * Dirty trick to enable the NMI watchdog ...
1555 * We put the 8259A master into AEOI mode and
1556 * unmask on all local APICs LVT0 as NMI.
1558 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1559 * is from Maciej W. Rozycki - so we do not have to EOI from
1560 * the NMI handler or the timer interrupt.
1562 printk(KERN_INFO "activating NMI Watchdog ...");
1564 enable_NMI_through_LVT0(NULL);
1570 * This looks a bit hackish but it's about the only one way of sending
1571 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1572 * not support the ExtINT mode, unfortunately. We need to send these
1573 * cycles as some i82489DX-based boards have glue logic that keeps the
1574 * 8259A interrupt line asserted until INTA. --macro
1576 static inline void unlock_ExtINT_logic(void)
1579 struct IO_APIC_route_entry entry0, entry1;
1580 unsigned char save_control, save_freq_select;
1581 unsigned long flags;
1583 pin = find_isa_irq_pin(8, mp_INT);
1587 spin_lock_irqsave(&ioapic_lock, flags);
1588 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1589 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1590 spin_unlock_irqrestore(&ioapic_lock, flags);
1591 clear_IO_APIC_pin(0, pin);
1593 memset(&entry1, 0, sizeof(entry1));
1595 entry1.dest_mode = 0; /* physical delivery */
1596 entry1.mask = 0; /* unmask IRQ now */
1597 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1598 entry1.delivery_mode = dest_ExtINT;
1599 entry1.polarity = entry0.polarity;
1603 spin_lock_irqsave(&ioapic_lock, flags);
1604 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1605 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1606 spin_unlock_irqrestore(&ioapic_lock, flags);
1608 save_control = CMOS_READ(RTC_CONTROL);
1609 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1610 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1612 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1617 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1621 CMOS_WRITE(save_control, RTC_CONTROL);
1622 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1623 clear_IO_APIC_pin(0, pin);
1625 spin_lock_irqsave(&ioapic_lock, flags);
1626 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1627 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1628 spin_unlock_irqrestore(&ioapic_lock, flags);
1632 * This code may look a bit paranoid, but it's supposed to cooperate with
1633 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1634 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1635 * fanatically on his truly buggy board.
1637 static inline void check_timer(void)
1643 * get/set the timer IRQ vector:
1645 disable_8259A_irq(0);
1646 vector = assign_irq_vector(0);
1647 set_intr_gate(vector, interrupt[0]);
1650 * Subtle, code in do_timer_interrupt() expects an AEOI
1651 * mode for the 8259A whenever interrupts are routed
1652 * through I/O APICs. Also IRQ0 has to be enabled in
1653 * the 8259A which implies the virtual wire has to be
1654 * disabled in the local APIC.
1656 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1658 enable_8259A_irq(0);
1660 pin1 = find_isa_irq_pin(0, mp_INT);
1661 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1663 printk(KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1667 * Ok, does IRQ0 through the IOAPIC work?
1669 unmask_IO_APIC_irq(0);
1670 if (timer_irq_works()) {
1671 nmi_watchdog_default();
1672 if (nmi_watchdog == NMI_IO_APIC) {
1673 disable_8259A_irq(0);
1675 enable_8259A_irq(0);
1676 check_nmi_watchdog();
1680 clear_IO_APIC_pin(0, pin1);
1681 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1684 printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1686 printk("\n..... (found pin %d) ...", pin2);
1688 * legacy devices should be connected to IO APIC #0
1690 setup_ExtINT_IRQ0_pin(pin2, vector);
1691 if (timer_irq_works()) {
1693 nmi_watchdog_default();
1694 if (nmi_watchdog == NMI_IO_APIC) {
1696 check_nmi_watchdog();
1701 * Cleanup, just in case ...
1703 clear_IO_APIC_pin(0, pin2);
1705 printk(" failed.\n");
1708 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1712 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1714 disable_8259A_irq(0);
1715 irq_desc[0].handler = &lapic_irq_type;
1716 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1717 enable_8259A_irq(0);
1719 if (timer_irq_works()) {
1720 printk(" works.\n");
1723 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1724 printk(" failed.\n");
1726 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1730 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1732 unlock_ExtINT_logic();
1734 if (timer_irq_works()) {
1735 printk(" works.\n");
1738 printk(" failed :(.\n");
1739 panic("IO-APIC + timer doesn't work! pester mingo@redhat.com");
1744 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1745 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1746 * Linux doesn't really care, as it's not actually used
1747 * for any interrupt handling anyway.
1749 #define PIC_IRQS (1<<2)
1751 void __init setup_IO_APIC(void)
1756 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1758 io_apic_irqs = ~PIC_IRQS;
1760 printk("ENABLING IO-APIC IRQs\n");
1763 * Set up the IO-APIC IRQ routing table.
1766 setup_ioapic_ids_from_mpc();
1768 setup_IO_APIC_irqs();
1769 init_IO_APIC_traps();
1775 /* --------------------------------------------------------------------------
1776 ACPI-based IOAPIC Configuration
1777 -------------------------------------------------------------------------- */
1779 #ifdef CONFIG_ACPI_BOOT
1781 #define IO_APIC_MAX_ID 15
1783 int __init io_apic_get_unique_id (int ioapic, int apic_id)
1785 union IO_APIC_reg_00 reg_00;
1786 static physid_mask_t apic_id_map;
1787 unsigned long flags;
1791 * The P4 platform supports up to 256 APIC IDs on two separate APIC
1792 * buses (one for LAPICs, one for IOAPICs), where predecessors only
1793 * supports up to 16 on one shared APIC bus.
1795 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
1796 * advantage of new APIC bus architecture.
1799 if (physids_empty(apic_id_map))
1800 apic_id_map = phys_cpu_present_map;
1802 spin_lock_irqsave(&ioapic_lock, flags);
1803 reg_00.raw = io_apic_read(ioapic, 0);
1804 spin_unlock_irqrestore(&ioapic_lock, flags);
1806 if (apic_id >= IO_APIC_MAX_ID) {
1807 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
1808 "%d\n", ioapic, apic_id, reg_00.bits.ID);
1809 apic_id = reg_00.bits.ID;
1813 * Every APIC in a system must have a unique ID or we get lots of nice
1814 * 'stuck on smp_invalidate_needed IPI wait' messages.
1816 if (physid_isset(apic_id, apic_id_map)) {
1818 for (i = 0; i < IO_APIC_MAX_ID; i++) {
1819 if (!physid_isset(i, apic_id_map))
1823 if (i == IO_APIC_MAX_ID)
1824 panic("Max apic_id exceeded!\n");
1826 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
1827 "trying %d\n", ioapic, apic_id, i);
1832 physid_set(apic_id, apic_id_map);
1834 if (reg_00.bits.ID != apic_id) {
1835 reg_00.bits.ID = apic_id;
1837 spin_lock_irqsave(&ioapic_lock, flags);
1838 io_apic_write(ioapic, 0, reg_00.raw);
1839 reg_00.raw = io_apic_read(ioapic, 0);
1840 spin_unlock_irqrestore(&ioapic_lock, flags);
1843 if (reg_00.bits.ID != apic_id)
1844 panic("IOAPIC[%d]: Unable change apic_id!\n", ioapic);
1847 printk(KERN_INFO "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
1853 int __init io_apic_get_version (int ioapic)
1855 union IO_APIC_reg_01 reg_01;
1856 unsigned long flags;
1858 spin_lock_irqsave(&ioapic_lock, flags);
1859 reg_01.raw = io_apic_read(ioapic, 1);
1860 spin_unlock_irqrestore(&ioapic_lock, flags);
1862 return reg_01.bits.version;
1866 int __init io_apic_get_redir_entries (int ioapic)
1868 union IO_APIC_reg_01 reg_01;
1869 unsigned long flags;
1871 spin_lock_irqsave(&ioapic_lock, flags);
1872 reg_01.raw = io_apic_read(ioapic, 1);
1873 spin_unlock_irqrestore(&ioapic_lock, flags);
1875 return reg_01.bits.entries;
1879 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
1881 struct IO_APIC_route_entry entry;
1882 unsigned long flags;
1884 if (!IO_APIC_IRQ(irq)) {
1885 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1891 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1892 * Note that we mask (disable) IRQs now -- these get enabled when the
1893 * corresponding device driver registers for this IRQ.
1896 memset(&entry,0,sizeof(entry));
1898 entry.delivery_mode = dest_LowestPrio;
1899 entry.dest_mode = INT_DELIVERY_MODE;
1900 entry.dest.logical.logical_dest = TARGET_CPUS;
1901 entry.trigger = edge_level;
1902 entry.polarity = active_high_low;
1903 entry.mask = 1; /* Disabled (masked) */
1906 * IRQs < 16 are already in the irq_2_pin[] map
1909 add_pin_to_irq(irq, ioapic, pin);
1911 entry.vector = assign_irq_vector(irq);
1913 printk(KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1914 "IRQ %d Mode:%i Active:%i)\n", ioapic,
1915 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
1916 edge_level, active_high_low);
1918 if (use_pci_vector() && !platform_legacy_irq(irq))
1919 irq = IO_APIC_VECTOR(irq);
1921 irq_desc[irq].handler = &ioapic_level_type;
1923 irq_desc[irq].handler = &ioapic_edge_type;
1926 set_intr_gate(entry.vector, interrupt[irq]);
1928 if (!ioapic && (irq < 16))
1929 disable_8259A_irq(irq);
1931 spin_lock_irqsave(&ioapic_lock, flags);
1932 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
1933 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
1934 spin_unlock_irqrestore(&ioapic_lock, flags);
1939 #endif /*CONFIG_ACPI_BOOT*/
1942 void send_IPI_self(int vector)
1949 apic_wait_icr_idle();
1950 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
1953 * Send the IPI. The write to APIC_ICR fires this off.
1955 apic_write_around(APIC_ICR, cfg);