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>
38 int sis_apic_bug; /* not actually supported, dummy for compile */
40 #undef APIC_LOCKUP_DEBUG
42 #define APIC_LOCKUP_DEBUG
44 static spinlock_t ioapic_lock = SPIN_LOCK_UNLOCKED;
47 * # of IRQ routing registers
49 int nr_ioapic_registers[MAX_IO_APICS];
52 * Rough estimation of how many shared IRQs there are, can
55 #define MAX_PLUS_SHARED_IRQS NR_IRQS
56 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
59 * This is performance-critical, we want to do it O(1)
61 * the indexing order of this array favors 1:1 mappings
62 * between pins and IRQs.
65 static struct irq_pin_list {
66 short apic, pin, next;
67 } irq_2_pin[PIN_MAP_SIZE];
70 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
71 * shared ISA-space IRQs, so we have to support them. We are super
72 * fast in the common case, and fast for shared ISA-space IRQs.
74 static void __init add_pin_to_irq(unsigned int irq, int apic, int pin)
76 static int first_free_entry = NR_IRQS;
77 struct irq_pin_list *entry = irq_2_pin + irq;
80 entry = irq_2_pin + entry->next;
82 if (entry->pin != -1) {
83 entry->next = first_free_entry;
84 entry = irq_2_pin + entry->next;
85 if (++first_free_entry >= PIN_MAP_SIZE)
86 panic("io_apic.c: whoops");
92 #define __DO_ACTION(R, ACTION, FINAL) \
96 struct irq_pin_list *entry = irq_2_pin + irq; \
103 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
105 io_apic_modify(entry->apic, reg); \
108 entry = irq_2_pin + entry->next; \
113 #define DO_ACTION(name,R,ACTION, FINAL) \
115 static void name##_IO_APIC_irq (unsigned int irq) \
116 __DO_ACTION(R, ACTION, FINAL)
118 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
120 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
122 DO_ACTION( __mask_and_edge, 0, = (reg & 0xffff7fff) | 0x00010000, )
123 /* mask = 1, trigger = 0 */
124 DO_ACTION( __unmask_and_level, 0, = (reg & 0xfffeffff) | 0x00008000, )
125 /* mask = 0, trigger = 1 */
127 static void mask_IO_APIC_irq (unsigned int irq)
131 spin_lock_irqsave(&ioapic_lock, flags);
132 __mask_IO_APIC_irq(irq);
133 spin_unlock_irqrestore(&ioapic_lock, flags);
136 static void unmask_IO_APIC_irq (unsigned int irq)
140 spin_lock_irqsave(&ioapic_lock, flags);
141 __unmask_IO_APIC_irq(irq);
142 spin_unlock_irqrestore(&ioapic_lock, flags);
145 void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
147 struct IO_APIC_route_entry entry;
151 * Disable it in the IO-APIC irq-routing table:
153 memset(&entry, 0, sizeof(entry));
155 spin_lock_irqsave(&ioapic_lock, flags);
156 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
157 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
158 spin_unlock_irqrestore(&ioapic_lock, flags);
161 static void clear_IO_APIC (void)
165 for (apic = 0; apic < nr_ioapics; apic++)
166 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
167 clear_IO_APIC_pin(apic, pin);
171 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
172 * specific CPU-side IRQs.
176 int pirq_entries [MAX_PIRQS];
178 int skip_ioapic_setup;
180 static int __init ioapic_setup(char *str)
182 skip_ioapic_setup = 1;
186 __setup("noapic", ioapic_setup);
188 static int __init ioapic_pirq_setup(char *str)
191 int ints[MAX_PIRQS+1];
193 get_options(str, ARRAY_SIZE(ints), ints);
195 for (i = 0; i < MAX_PIRQS; i++)
196 pirq_entries[i] = -1;
199 printk(KERN_INFO "PIRQ redirection, working around broken MP-BIOS.\n");
201 if (ints[0] < MAX_PIRQS)
204 for (i = 0; i < max; i++) {
205 printk(KERN_DEBUG "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
207 * PIRQs are mapped upside down, usually.
209 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
214 __setup("pirq=", ioapic_pirq_setup);
217 * Find the IRQ entry number of a certain pin.
219 static int __init find_irq_entry(int apic, int pin, int type)
223 for (i = 0; i < mp_irq_entries; i++)
224 if (mp_irqs[i].mpc_irqtype == type &&
225 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
226 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
227 mp_irqs[i].mpc_dstirq == pin)
234 * Find the pin to which IRQ[irq] (ISA) is connected
236 static int __init find_isa_irq_pin(int irq, int type)
240 for (i = 0; i < mp_irq_entries; i++) {
241 int lbus = mp_irqs[i].mpc_srcbus;
243 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
244 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
245 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
246 (mp_irqs[i].mpc_irqtype == type) &&
247 (mp_irqs[i].mpc_srcbusirq == irq))
249 return mp_irqs[i].mpc_dstirq;
255 * Find a specific PCI IRQ entry.
256 * Not an __init, possibly needed by modules
258 static int pin_2_irq(int idx, int apic, int pin);
260 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
262 int apic, i, best_guess = -1;
264 Dprintk("querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
266 if (mp_bus_id_to_pci_bus[bus] == -1) {
267 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
270 for (i = 0; i < mp_irq_entries; i++) {
271 int lbus = mp_irqs[i].mpc_srcbus;
273 for (apic = 0; apic < nr_ioapics; apic++)
274 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
275 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
278 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
279 !mp_irqs[i].mpc_irqtype &&
281 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
282 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
284 if (!(apic || IO_APIC_IRQ(irq)))
287 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
290 * Use the first all-but-pin matching entry as a
291 * best-guess fuzzy result for broken mptables.
301 * EISA Edge/Level control register, ELCR
303 static int __init EISA_ELCR(unsigned int irq)
306 unsigned int port = 0x4d0 + (irq >> 3);
307 return (inb(port) >> (irq & 7)) & 1;
309 printk(KERN_INFO "Broken MPtable reports ISA irq %d\n", irq);
313 /* EISA interrupts are always polarity zero and can be edge or level
314 * trigger depending on the ELCR value. If an interrupt is listed as
315 * EISA conforming in the MP table, that means its trigger type must
316 * be read in from the ELCR */
318 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
319 #define default_EISA_polarity(idx) (0)
321 /* ISA interrupts are always polarity zero edge triggered,
322 * when listed as conforming in the MP table. */
324 #define default_ISA_trigger(idx) (0)
325 #define default_ISA_polarity(idx) (0)
327 /* PCI interrupts are always polarity one level triggered,
328 * when listed as conforming in the MP table. */
330 #define default_PCI_trigger(idx) (1)
331 #define default_PCI_polarity(idx) (1)
333 /* MCA interrupts are always polarity zero level triggered,
334 * when listed as conforming in the MP table. */
336 #define default_MCA_trigger(idx) (1)
337 #define default_MCA_polarity(idx) (0)
339 static int __init MPBIOS_polarity(int idx)
341 int bus = mp_irqs[idx].mpc_srcbus;
345 * Determine IRQ line polarity (high active or low active):
347 switch (mp_irqs[idx].mpc_irqflag & 3)
349 case 0: /* conforms, ie. bus-type dependent polarity */
351 switch (mp_bus_id_to_type[bus])
353 case MP_BUS_ISA: /* ISA pin */
355 polarity = default_ISA_polarity(idx);
358 case MP_BUS_EISA: /* EISA pin */
360 polarity = default_EISA_polarity(idx);
363 case MP_BUS_PCI: /* PCI pin */
365 polarity = default_PCI_polarity(idx);
368 case MP_BUS_MCA: /* MCA pin */
370 polarity = default_MCA_polarity(idx);
375 printk(KERN_WARNING "broken BIOS!!\n");
382 case 1: /* high active */
387 case 2: /* reserved */
389 printk(KERN_WARNING "broken BIOS!!\n");
393 case 3: /* low active */
398 default: /* invalid */
400 printk(KERN_WARNING "broken BIOS!!\n");
408 static int __init MPBIOS_trigger(int idx)
410 int bus = mp_irqs[idx].mpc_srcbus;
414 * Determine IRQ trigger mode (edge or level sensitive):
416 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
418 case 0: /* conforms, ie. bus-type dependent */
420 switch (mp_bus_id_to_type[bus])
422 case MP_BUS_ISA: /* ISA pin */
424 trigger = default_ISA_trigger(idx);
427 case MP_BUS_EISA: /* EISA pin */
429 trigger = default_EISA_trigger(idx);
432 case MP_BUS_PCI: /* PCI pin */
434 trigger = default_PCI_trigger(idx);
437 case MP_BUS_MCA: /* MCA pin */
439 trigger = default_MCA_trigger(idx);
444 printk(KERN_WARNING "broken BIOS!!\n");
456 case 2: /* reserved */
458 printk(KERN_WARNING "broken BIOS!!\n");
467 default: /* invalid */
469 printk(KERN_WARNING "broken BIOS!!\n");
477 static inline int irq_polarity(int idx)
479 return MPBIOS_polarity(idx);
482 static inline int irq_trigger(int idx)
484 return MPBIOS_trigger(idx);
487 static int pin_2_irq(int idx, int apic, int pin)
490 int bus = mp_irqs[idx].mpc_srcbus;
493 * Debugging check, we are in big trouble if this message pops up!
495 if (mp_irqs[idx].mpc_dstirq != pin)
496 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
498 switch (mp_bus_id_to_type[bus])
500 case MP_BUS_ISA: /* ISA pin */
504 irq = mp_irqs[idx].mpc_srcbusirq;
507 case MP_BUS_PCI: /* PCI pin */
510 * PCI IRQs are mapped in order
514 irq += nr_ioapic_registers[i++];
520 printk(KERN_ERR "unknown bus type %d.\n",bus);
527 * PCI IRQ command line redirection. Yes, limits are hardcoded.
529 if ((pin >= 16) && (pin <= 23)) {
530 if (pirq_entries[pin-16] != -1) {
531 if (!pirq_entries[pin-16]) {
532 printk(KERN_DEBUG "disabling PIRQ%d\n", pin-16);
534 irq = pirq_entries[pin-16];
535 printk(KERN_DEBUG "using PIRQ%d -> IRQ %d\n",
543 static inline int IO_APIC_irq_trigger(int irq)
547 for (apic = 0; apic < nr_ioapics; apic++) {
548 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
549 idx = find_irq_entry(apic,pin,mp_INT);
550 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
551 return irq_trigger(idx);
555 * nonexistent IRQs are edge default
560 int irq_vector[NR_IRQS] = { FIRST_DEVICE_VECTOR , 0 };
562 static int __init assign_irq_vector(int irq)
564 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
565 if (IO_APIC_VECTOR(irq) > 0)
566 return IO_APIC_VECTOR(irq);
569 if (current_vector == IA32_SYSCALL_VECTOR)
572 if (current_vector > FIRST_SYSTEM_VECTOR) {
574 current_vector = FIRST_DEVICE_VECTOR + offset;
577 if (current_vector == FIRST_SYSTEM_VECTOR)
578 panic("ran out of interrupt sources!");
580 IO_APIC_VECTOR(irq) = current_vector;
581 return current_vector;
584 extern void (*interrupt[NR_IRQS])(void);
585 static struct hw_interrupt_type ioapic_level_irq_type;
586 static struct hw_interrupt_type ioapic_edge_irq_type;
588 void __init setup_IO_APIC_irqs(void)
590 struct IO_APIC_route_entry entry;
591 int apic, pin, idx, irq, first_notcon = 1, vector;
594 printk(KERN_DEBUG "init IO_APIC IRQs\n");
596 for (apic = 0; apic < nr_ioapics; apic++) {
597 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
600 * add it to the IO-APIC irq-routing table:
602 memset(&entry,0,sizeof(entry));
604 entry.delivery_mode = dest_LowestPrio;
605 entry.dest_mode = INT_DELIVERY_MODE;
606 entry.mask = 0; /* enable IRQ */
607 entry.dest.logical.logical_dest = TARGET_CPUS;
609 idx = find_irq_entry(apic,pin,mp_INT);
612 printk(KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
615 printk(", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
619 entry.trigger = irq_trigger(idx);
620 entry.polarity = irq_polarity(idx);
622 if (irq_trigger(idx)) {
625 entry.dest.logical.logical_dest = TARGET_CPUS;
628 irq = pin_2_irq(idx, apic, pin);
629 add_pin_to_irq(irq, apic, pin);
631 if (!apic && !IO_APIC_IRQ(irq))
634 if (IO_APIC_IRQ(irq)) {
635 vector = assign_irq_vector(irq);
636 entry.vector = vector;
638 if (IO_APIC_irq_trigger(irq))
639 irq_desc[irq].handler = &ioapic_level_irq_type;
641 irq_desc[irq].handler = &ioapic_edge_irq_type;
643 set_intr_gate(vector, interrupt[irq]);
645 if (!apic && (irq < 16))
646 disable_8259A_irq(irq);
648 spin_lock_irqsave(&ioapic_lock, flags);
649 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
650 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
651 spin_unlock_irqrestore(&ioapic_lock, flags);
656 printk(" not connected.\n");
660 * Set up the 8259A-master output pin as broadcast to all
663 void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
665 struct IO_APIC_route_entry entry;
668 memset(&entry,0,sizeof(entry));
670 disable_8259A_irq(0);
673 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
676 * We use logical delivery to get the timer IRQ
679 entry.dest_mode = INT_DELIVERY_MODE;
680 entry.mask = 0; /* unmask IRQ now */
681 entry.dest.logical.logical_dest = TARGET_CPUS;
682 entry.delivery_mode = dest_LowestPrio;
685 entry.vector = vector;
688 * The timer IRQ doesn't have to know that behind the
689 * scene we have a 8259A-master in AEOI mode ...
691 irq_desc[0].handler = &ioapic_edge_irq_type;
694 * Add it to the IO-APIC irq-routing table:
696 spin_lock_irqsave(&ioapic_lock, flags);
697 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
698 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
699 spin_unlock_irqrestore(&ioapic_lock, flags);
704 void __init UNEXPECTED_IO_APIC(void)
707 printk(KERN_WARNING " WARNING: unexpected IO-APIC, please mail\n");
708 printk(KERN_WARNING " to linux-smp@vger.kernel.org\n");
712 void __init print_IO_APIC(void)
715 union IO_APIC_reg_00 reg_00;
716 union IO_APIC_reg_01 reg_01;
717 union IO_APIC_reg_02 reg_02;
720 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
721 for (i = 0; i < nr_ioapics; i++)
722 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
723 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
726 * We are a bit conservative about what we expect. We have to
727 * know about every hardware change ASAP.
729 printk(KERN_INFO "testing the IO APIC.......................\n");
731 for (apic = 0; apic < nr_ioapics; apic++) {
733 spin_lock_irqsave(&ioapic_lock, flags);
734 reg_00.raw = io_apic_read(apic, 0);
735 reg_01.raw = io_apic_read(apic, 1);
736 if (reg_01.bits.version >= 0x10)
737 reg_02.raw = io_apic_read(apic, 2);
738 spin_unlock_irqrestore(&ioapic_lock, flags);
741 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
742 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
743 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
744 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
745 UNEXPECTED_IO_APIC();
747 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
748 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
749 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
750 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
751 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
752 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
753 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
754 (reg_01.bits.entries != 0x2E) &&
755 (reg_01.bits.entries != 0x3F) &&
756 (reg_01.bits.entries != 0x03)
758 UNEXPECTED_IO_APIC();
760 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
761 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
762 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
763 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
764 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
765 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
766 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
767 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
769 UNEXPECTED_IO_APIC();
770 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
771 UNEXPECTED_IO_APIC();
773 if (reg_01.bits.version >= 0x10) {
774 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
775 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
776 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
777 UNEXPECTED_IO_APIC();
780 printk(KERN_DEBUG ".... IRQ redirection table:\n");
782 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
783 " Stat Dest Deli Vect: \n");
785 for (i = 0; i <= reg_01.bits.entries; i++) {
786 struct IO_APIC_route_entry entry;
788 spin_lock_irqsave(&ioapic_lock, flags);
789 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
790 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
791 spin_unlock_irqrestore(&ioapic_lock, flags);
793 printk(KERN_DEBUG " %02x %03X %02X ",
795 entry.dest.logical.logical_dest,
796 entry.dest.physical.physical_dest
799 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
804 entry.delivery_status,
811 printk(KERN_DEBUG "IRQ to pin mappings:\n");
812 for (i = 0; i < NR_IRQS; i++) {
813 struct irq_pin_list *entry = irq_2_pin + i;
816 printk(KERN_DEBUG "IRQ%d ", i);
818 printk("-> %d:%d", entry->apic, entry->pin);
821 entry = irq_2_pin + entry->next;
826 printk(KERN_INFO ".................................... done.\n");
831 static void print_APIC_bitfield (int base)
836 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
837 for (i = 0; i < 8; i++) {
838 v = apic_read(base + i*0x10);
839 for (j = 0; j < 32; j++) {
849 void /*__init*/ print_local_APIC(void * dummy)
851 unsigned int v, ver, maxlvt;
853 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
854 smp_processor_id(), hard_smp_processor_id());
855 v = apic_read(APIC_ID);
856 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
857 v = apic_read(APIC_LVR);
858 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
859 ver = GET_APIC_VERSION(v);
860 maxlvt = get_maxlvt();
862 v = apic_read(APIC_TASKPRI);
863 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
865 if (APIC_INTEGRATED(ver)) { /* !82489DX */
866 v = apic_read(APIC_ARBPRI);
867 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
868 v & APIC_ARBPRI_MASK);
869 v = apic_read(APIC_PROCPRI);
870 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
873 v = apic_read(APIC_EOI);
874 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
875 v = apic_read(APIC_RRR);
876 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
877 v = apic_read(APIC_LDR);
878 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
879 v = apic_read(APIC_DFR);
880 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
881 v = apic_read(APIC_SPIV);
882 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
884 printk(KERN_DEBUG "... APIC ISR field:\n");
885 print_APIC_bitfield(APIC_ISR);
886 printk(KERN_DEBUG "... APIC TMR field:\n");
887 print_APIC_bitfield(APIC_TMR);
888 printk(KERN_DEBUG "... APIC IRR field:\n");
889 print_APIC_bitfield(APIC_IRR);
891 if (APIC_INTEGRATED(ver)) { /* !82489DX */
892 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
893 apic_write(APIC_ESR, 0);
894 v = apic_read(APIC_ESR);
895 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
898 v = apic_read(APIC_ICR);
899 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
900 v = apic_read(APIC_ICR2);
901 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
903 v = apic_read(APIC_LVTT);
904 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
906 if (maxlvt > 3) { /* PC is LVT#4. */
907 v = apic_read(APIC_LVTPC);
908 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
910 v = apic_read(APIC_LVT0);
911 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
912 v = apic_read(APIC_LVT1);
913 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
915 if (maxlvt > 2) { /* ERR is LVT#3. */
916 v = apic_read(APIC_LVTERR);
917 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
920 v = apic_read(APIC_TMICT);
921 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
922 v = apic_read(APIC_TMCCT);
923 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
924 v = apic_read(APIC_TDCR);
925 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
929 void print_all_local_APICs (void)
931 on_each_cpu(print_local_APIC, NULL, 1, 1);
934 void /*__init*/ print_PIC(void)
936 extern spinlock_t i8259A_lock;
940 printk(KERN_DEBUG "\nprinting PIC contents\n");
942 spin_lock_irqsave(&i8259A_lock, flags);
944 v = inb(0xa1) << 8 | inb(0x21);
945 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
947 v = inb(0xa0) << 8 | inb(0x20);
948 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
952 v = inb(0xa0) << 8 | inb(0x20);
956 spin_unlock_irqrestore(&i8259A_lock, flags);
958 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
960 v = inb(0x4d1) << 8 | inb(0x4d0);
961 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
964 static void __init enable_IO_APIC(void)
966 union IO_APIC_reg_01 reg_01;
970 for (i = 0; i < PIN_MAP_SIZE; i++) {
971 irq_2_pin[i].pin = -1;
972 irq_2_pin[i].next = 0;
975 for (i = 0; i < MAX_PIRQS; i++)
976 pirq_entries[i] = -1;
979 * The number of IO-APIC IRQ registers (== #pins):
981 for (i = 0; i < nr_ioapics; i++) {
982 spin_lock_irqsave(&ioapic_lock, flags);
983 reg_01.raw = io_apic_read(i, 1);
984 spin_unlock_irqrestore(&ioapic_lock, flags);
985 nr_ioapic_registers[i] = reg_01.bits.entries+1;
989 * Do not trust the IO-APIC being empty at bootup
995 * Not an __init, needed by the reboot code
997 void disable_IO_APIC(void)
1000 * Clear the IO-APIC before rebooting:
1004 disconnect_bsp_APIC();
1008 * function to set the IO-APIC physical IDs based on the
1009 * values stored in the MPC table.
1011 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1014 static void __init setup_ioapic_ids_from_mpc (void)
1016 union IO_APIC_reg_00 reg_00;
1017 unsigned long phys_id_present_map = phys_cpu_present_map;
1020 unsigned char old_id;
1021 unsigned long flags;
1023 if (acpi_ioapic) return; /* ACPI does that already */
1026 * Set the IOAPIC ID to the value stored in the MPC table.
1028 for (apic = 0; apic < nr_ioapics; apic++) {
1030 /* Read the register 0 value */
1031 spin_lock_irqsave(&ioapic_lock, flags);
1032 reg_00.raw = io_apic_read(apic, 0);
1033 spin_unlock_irqrestore(&ioapic_lock, flags);
1035 old_id = mp_ioapics[apic].mpc_apicid;
1037 if (mp_ioapics[apic].mpc_apicid >= 0xf) {
1038 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1039 apic, mp_ioapics[apic].mpc_apicid);
1040 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1042 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1046 * Sanity check, is the ID really free? Every APIC in a
1047 * system must have a unique ID or we get lots of nice
1048 * 'stuck on smp_invalidate_needed IPI wait' messages.
1050 if (phys_id_present_map & (1 << mp_ioapics[apic].mpc_apicid)) {
1051 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1052 apic, mp_ioapics[apic].mpc_apicid);
1053 for (i = 0; i < 0xf; i++)
1054 if (!(phys_id_present_map & (1 << i)))
1057 panic("Max APIC ID exceeded!\n");
1058 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1060 phys_id_present_map |= 1 << i;
1061 mp_ioapics[apic].mpc_apicid = i;
1064 "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1065 phys_id_present_map |= 1 << mp_ioapics[apic].mpc_apicid;
1070 * We need to adjust the IRQ routing table
1071 * if the ID changed.
1073 if (old_id != mp_ioapics[apic].mpc_apicid)
1074 for (i = 0; i < mp_irq_entries; i++)
1075 if (mp_irqs[i].mpc_dstapic == old_id)
1076 mp_irqs[i].mpc_dstapic
1077 = mp_ioapics[apic].mpc_apicid;
1080 * Read the right value from the MPC table and
1081 * write it into the ID register.
1083 printk(KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1084 mp_ioapics[apic].mpc_apicid);
1086 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1087 spin_lock_irqsave(&ioapic_lock, flags);
1088 io_apic_write(apic, 0, reg_00.raw);
1089 spin_unlock_irqrestore(&ioapic_lock, flags);
1094 spin_lock_irqsave(&ioapic_lock, flags);
1095 reg_00.raw = io_apic_read(apic, 0);
1096 spin_unlock_irqrestore(&ioapic_lock, flags);
1097 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1098 panic("could not set ID!\n");
1105 * There is a nasty bug in some older SMP boards, their mptable lies
1106 * about the timer IRQ. We do the following to work around the situation:
1108 * - timer IRQ defaults to IO-APIC IRQ
1109 * - if this function detects that timer IRQs are defunct, then we fall
1110 * back to ISA timer IRQs
1112 static int __init timer_irq_works(void)
1114 unsigned long t1 = jiffies;
1117 /* Let ten ticks pass... */
1118 mdelay((10 * 1000) / HZ);
1121 * Expect a few ticks at least, to be sure some possible
1122 * glue logic does not lock up after one or two first
1123 * ticks in a non-ExtINT mode. Also the local APIC
1124 * might have cached one ExtINT interrupt. Finally, at
1125 * least one tick may be lost due to delays.
1129 if (jiffies - t1 > 4)
1135 * In the SMP+IOAPIC case it might happen that there are an unspecified
1136 * number of pending IRQ events unhandled. These cases are very rare,
1137 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1138 * better to do it this way as thus we do not have to be aware of
1139 * 'pending' interrupts in the IRQ path, except at this point.
1142 * Edge triggered needs to resend any interrupt
1143 * that was delayed but this is now handled in the device
1146 #define enable_edge_ioapic_irq unmask_IO_APIC_irq
1148 static void disable_edge_ioapic_irq (unsigned int irq) { /* nothing */ }
1151 * Starting up a edge-triggered IO-APIC interrupt is
1152 * nasty - we need to make sure that we get the edge.
1153 * If it is already asserted for some reason, we need
1154 * return 1 to indicate that is was pending.
1156 * This is not complete - we should be able to fake
1157 * an edge even if it isn't on the 8259A...
1160 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1162 int was_pending = 0;
1163 unsigned long flags;
1165 spin_lock_irqsave(&ioapic_lock, flags);
1167 disable_8259A_irq(irq);
1168 if (i8259A_irq_pending(irq))
1171 __unmask_IO_APIC_irq(irq);
1172 spin_unlock_irqrestore(&ioapic_lock, flags);
1177 #define shutdown_edge_ioapic_irq disable_edge_ioapic_irq
1180 * Once we have recorded IRQ_PENDING already, we can mask the
1181 * interrupt for real. This prevents IRQ storms from unhandled
1184 static void ack_edge_ioapic_irq(unsigned int irq)
1186 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1187 == (IRQ_PENDING | IRQ_DISABLED))
1188 mask_IO_APIC_irq(irq);
1192 static void end_edge_ioapic_irq (unsigned int i) { /* nothing */ }
1196 * Level triggered interrupts can just be masked,
1197 * and shutting down and starting up the interrupt
1198 * is the same as enabling and disabling them -- except
1199 * with a startup need to return a "was pending" value.
1201 * Level triggered interrupts are special because we
1202 * do not touch any IO-APIC register while handling
1203 * them. We ack the APIC in the end-IRQ handler, not
1204 * in the start-IRQ-handler. Protection against reentrance
1205 * from the same interrupt is still provided, both by the
1206 * generic IRQ layer and by the fact that an unacked local
1207 * APIC does not accept IRQs.
1209 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1211 unmask_IO_APIC_irq(irq);
1213 return 0; /* don't check for pending */
1216 #define shutdown_level_ioapic_irq mask_IO_APIC_irq
1217 #define enable_level_ioapic_irq unmask_IO_APIC_irq
1218 #define disable_level_ioapic_irq mask_IO_APIC_irq
1220 static void end_level_ioapic_irq (unsigned int irq)
1226 * It appears there is an erratum which affects at least version 0x11
1227 * of I/O APIC (that's the 82093AA and cores integrated into various
1228 * chipsets). Under certain conditions a level-triggered interrupt is
1229 * erroneously delivered as edge-triggered one but the respective IRR
1230 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1231 * message but it will never arrive and further interrupts are blocked
1232 * from the source. The exact reason is so far unknown, but the
1233 * phenomenon was observed when two consecutive interrupt requests
1234 * from a given source get delivered to the same CPU and the source is
1235 * temporarily disabled in between.
1237 * A workaround is to simulate an EOI message manually. We achieve it
1238 * by setting the trigger mode to edge and then to level when the edge
1239 * trigger mode gets detected in the TMR of a local APIC for a
1240 * level-triggered interrupt. We mask the source for the time of the
1241 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1242 * The idea is from Manfred Spraul. --macro
1244 i = IO_APIC_VECTOR(irq);
1245 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1249 if (!(v & (1 << (i & 0x1f)))) {
1250 #ifdef APIC_LOCKUP_DEBUG
1251 struct irq_pin_list *entry;
1254 #ifdef APIC_MISMATCH_DEBUG
1255 atomic_inc(&irq_mis_count);
1257 spin_lock(&ioapic_lock);
1258 __mask_and_edge_IO_APIC_irq(irq);
1259 #ifdef APIC_LOCKUP_DEBUG
1260 for (entry = irq_2_pin + irq;;) {
1263 if (entry->pin == -1)
1265 reg = io_apic_read(entry->apic, 0x10 + entry->pin * 2);
1266 if (reg & 0x00004000)
1267 printk(KERN_CRIT "Aieee!!! Remote IRR"
1268 " still set after unlock!\n");
1271 entry = irq_2_pin + entry->next;
1274 __unmask_and_level_IO_APIC_irq(irq);
1275 spin_unlock(&ioapic_lock);
1279 static void mask_and_ack_level_ioapic_irq (unsigned int irq) { /* nothing */ }
1281 static void set_ioapic_affinity (unsigned int irq, unsigned long mask)
1283 unsigned long flags;
1285 * Only the first 8 bits are valid.
1289 spin_lock_irqsave(&ioapic_lock, flags);
1290 __DO_ACTION(1, = mask, )
1291 spin_unlock_irqrestore(&ioapic_lock, flags);
1295 * Level and edge triggered IO-APIC interrupts need different handling,
1296 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1297 * handled with the level-triggered descriptor, but that one has slightly
1298 * more overhead. Level-triggered interrupts cannot be handled with the
1299 * edge-triggered handler, without risking IRQ storms and other ugly
1303 static struct hw_interrupt_type ioapic_edge_irq_type = {
1304 .typename = "IO-APIC-edge",
1305 .startup = startup_edge_ioapic_irq,
1306 .shutdown = shutdown_edge_ioapic_irq,
1307 .enable = enable_edge_ioapic_irq,
1308 .disable = disable_edge_ioapic_irq,
1309 .ack = ack_edge_ioapic_irq,
1310 .end = end_edge_ioapic_irq,
1311 .set_affinity = set_ioapic_affinity,
1314 static struct hw_interrupt_type ioapic_level_irq_type = {
1315 .typename = "IO-APIC-level",
1316 .startup = startup_level_ioapic_irq,
1317 .shutdown = shutdown_level_ioapic_irq,
1318 .enable = enable_level_ioapic_irq,
1319 .disable = disable_level_ioapic_irq,
1320 .ack = mask_and_ack_level_ioapic_irq,
1321 .end = end_level_ioapic_irq,
1322 .set_affinity = set_ioapic_affinity,
1325 static inline void init_IO_APIC_traps(void)
1330 * NOTE! The local APIC isn't very good at handling
1331 * multiple interrupts at the same interrupt level.
1332 * As the interrupt level is determined by taking the
1333 * vector number and shifting that right by 4, we
1334 * want to spread these out a bit so that they don't
1335 * all fall in the same interrupt level.
1337 * Also, we've got to be careful not to trash gate
1338 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1340 for (irq = 0; irq < NR_IRQS ; irq++) {
1341 if (IO_APIC_IRQ(irq) && !IO_APIC_VECTOR(irq)) {
1343 * Hmm.. We don't have an entry for this,
1344 * so default to an old-fashioned 8259
1345 * interrupt if we can..
1348 make_8259A_irq(irq);
1350 /* Strange. Oh, well.. */
1351 irq_desc[irq].handler = &no_irq_type;
1356 static void enable_lapic_irq (unsigned int irq)
1360 v = apic_read(APIC_LVT0);
1361 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1364 static void disable_lapic_irq (unsigned int irq)
1368 v = apic_read(APIC_LVT0);
1369 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1372 static void ack_lapic_irq (unsigned int irq)
1377 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1379 static struct hw_interrupt_type lapic_irq_type = {
1380 .typename = "local-APIC-edge",
1381 .startup = NULL, /* startup_irq() not used for IRQ0 */
1382 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1383 .enable = enable_lapic_irq,
1384 .disable = disable_lapic_irq,
1385 .ack = ack_lapic_irq,
1386 .end = end_lapic_irq,
1389 static void setup_nmi (void)
1392 * Dirty trick to enable the NMI watchdog ...
1393 * We put the 8259A master into AEOI mode and
1394 * unmask on all local APICs LVT0 as NMI.
1396 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1397 * is from Maciej W. Rozycki - so we do not have to EOI from
1398 * the NMI handler or the timer interrupt.
1400 printk(KERN_INFO "activating NMI Watchdog ...");
1402 enable_NMI_through_LVT0(NULL);
1408 * This looks a bit hackish but it's about the only one way of sending
1409 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1410 * not support the ExtINT mode, unfortunately. We need to send these
1411 * cycles as some i82489DX-based boards have glue logic that keeps the
1412 * 8259A interrupt line asserted until INTA. --macro
1414 static inline void unlock_ExtINT_logic(void)
1417 struct IO_APIC_route_entry entry0, entry1;
1418 unsigned char save_control, save_freq_select;
1419 unsigned long flags;
1421 pin = find_isa_irq_pin(8, mp_INT);
1425 spin_lock_irqsave(&ioapic_lock, flags);
1426 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1427 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1428 spin_unlock_irqrestore(&ioapic_lock, flags);
1429 clear_IO_APIC_pin(0, pin);
1431 memset(&entry1, 0, sizeof(entry1));
1433 entry1.dest_mode = 0; /* physical delivery */
1434 entry1.mask = 0; /* unmask IRQ now */
1435 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1436 entry1.delivery_mode = dest_ExtINT;
1437 entry1.polarity = entry0.polarity;
1441 spin_lock_irqsave(&ioapic_lock, flags);
1442 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1443 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1444 spin_unlock_irqrestore(&ioapic_lock, flags);
1446 save_control = CMOS_READ(RTC_CONTROL);
1447 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1448 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1450 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1455 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1459 CMOS_WRITE(save_control, RTC_CONTROL);
1460 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1461 clear_IO_APIC_pin(0, pin);
1463 spin_lock_irqsave(&ioapic_lock, flags);
1464 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1465 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1466 spin_unlock_irqrestore(&ioapic_lock, flags);
1470 * This code may look a bit paranoid, but it's supposed to cooperate with
1471 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1472 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1473 * fanatically on his truly buggy board.
1475 static inline void check_timer(void)
1481 * get/set the timer IRQ vector:
1483 disable_8259A_irq(0);
1484 vector = assign_irq_vector(0);
1485 set_intr_gate(vector, interrupt[0]);
1488 * Subtle, code in do_timer_interrupt() expects an AEOI
1489 * mode for the 8259A whenever interrupts are routed
1490 * through I/O APICs. Also IRQ0 has to be enabled in
1491 * the 8259A which implies the virtual wire has to be
1492 * disabled in the local APIC.
1494 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1496 enable_8259A_irq(0);
1498 pin1 = find_isa_irq_pin(0, mp_INT);
1499 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1501 printk(KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1505 * Ok, does IRQ0 through the IOAPIC work?
1507 unmask_IO_APIC_irq(0);
1508 if (timer_irq_works()) {
1509 if (nmi_watchdog == NMI_IO_APIC) {
1510 disable_8259A_irq(0);
1512 enable_8259A_irq(0);
1513 check_nmi_watchdog();
1517 clear_IO_APIC_pin(0, pin1);
1518 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1521 printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1523 printk("\n..... (found pin %d) ...", pin2);
1525 * legacy devices should be connected to IO APIC #0
1527 setup_ExtINT_IRQ0_pin(pin2, vector);
1528 if (timer_irq_works()) {
1530 if (nmi_watchdog == NMI_IO_APIC) {
1532 check_nmi_watchdog();
1537 * Cleanup, just in case ...
1539 clear_IO_APIC_pin(0, pin2);
1541 printk(" failed.\n");
1544 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1548 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1550 disable_8259A_irq(0);
1551 irq_desc[0].handler = &lapic_irq_type;
1552 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1553 enable_8259A_irq(0);
1555 if (timer_irq_works()) {
1556 printk(" works.\n");
1559 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1560 printk(" failed.\n");
1562 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1566 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1568 unlock_ExtINT_logic();
1570 if (timer_irq_works()) {
1571 printk(" works.\n");
1574 printk(" failed :(.\n");
1575 panic("IO-APIC + timer doesn't work! pester mingo@redhat.com");
1580 * IRQ's that are handled by the old PIC in all cases:
1581 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1582 * Linux doesn't really care, as it's not actually used
1583 * for any interrupt handling anyway.
1584 * - There used to be IRQ13 here as well, but all
1585 * MPS-compliant must not use it for FPU coupling and we
1586 * want to use exception 16 anyway. And there are
1587 * systems who connect it to an I/O APIC for other uses.
1588 * Thus we don't mark it special any longer.
1590 * Additionally, something is definitely wrong with irq9
1593 #define PIC_IRQS (1<<2)
1595 void __init setup_IO_APIC(void)
1599 io_apic_irqs = ~PIC_IRQS;
1600 printk("ENABLING IO-APIC IRQs\n");
1603 * Set up the IO-APIC IRQ routing table.
1605 setup_ioapic_ids_from_mpc();
1607 setup_IO_APIC_irqs();
1608 init_IO_APIC_traps();
1613 /* Ensure the ACPI SCI interrupt level is active low, edge-triggered */
1615 void __init mp_config_ioapic_for_sci(int irq)
1621 ioapic = mp_find_ioapic(irq);
1623 ioapic_pin = irq - mp_ioapic_routing[ioapic].irq_start;
1625 io_apic_set_pci_routing(ioapic, ioapic_pin, irq);
1630 /* --------------------------------------------------------------------------
1631 ACPI-based IOAPIC Configuration
1632 -------------------------------------------------------------------------- */
1634 #ifdef CONFIG_ACPI_BOOT
1636 #define IO_APIC_MAX_ID 15
1638 int __init io_apic_get_unique_id (int ioapic, int apic_id)
1640 union IO_APIC_reg_00 reg_00;
1641 static unsigned long apic_id_map = 0;
1642 unsigned long flags;
1646 * The P4 platform supports up to 256 APIC IDs on two separate APIC
1647 * buses (one for LAPICs, one for IOAPICs), where predecessors only
1648 * supports up to 16 on one shared APIC bus.
1650 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
1651 * advantage of new APIC bus architecture.
1655 apic_id_map = phys_cpu_present_map;
1657 spin_lock_irqsave(&ioapic_lock, flags);
1658 reg_00.raw = io_apic_read(ioapic, 0);
1659 spin_unlock_irqrestore(&ioapic_lock, flags);
1661 if (apic_id >= IO_APIC_MAX_ID) {
1662 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
1663 "%d\n", ioapic, apic_id, reg_00.bits.ID);
1664 apic_id = reg_00.bits.ID;
1668 * Every APIC in a system must have a unique ID or we get lots of nice
1669 * 'stuck on smp_invalidate_needed IPI wait' messages.
1671 if (apic_id_map & (1 << apic_id)) {
1673 for (i = 0; i < IO_APIC_MAX_ID; i++) {
1674 if (!(apic_id_map & (1 << i)))
1678 if (i == IO_APIC_MAX_ID)
1679 panic("Max apic_id exceeded!\n");
1681 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
1682 "trying %d\n", ioapic, apic_id, i);
1687 apic_id_map |= (1 << apic_id);
1689 if (reg_00.bits.ID != apic_id) {
1690 reg_00.bits.ID = apic_id;
1692 spin_lock_irqsave(&ioapic_lock, flags);
1693 io_apic_write(ioapic, 0, reg_00.raw);
1694 reg_00.raw = io_apic_read(ioapic, 0);
1695 spin_unlock_irqrestore(&ioapic_lock, flags);
1698 if (reg_00.bits.ID != apic_id)
1699 panic("IOAPIC[%d]: Unable change apic_id!\n", ioapic);
1702 printk(KERN_INFO "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
1708 int __init io_apic_get_version (int ioapic)
1710 union IO_APIC_reg_01 reg_01;
1711 unsigned long flags;
1713 spin_lock_irqsave(&ioapic_lock, flags);
1714 reg_01.raw = io_apic_read(ioapic, 1);
1715 spin_unlock_irqrestore(&ioapic_lock, flags);
1717 return reg_01.bits.version;
1721 int __init io_apic_get_redir_entries (int ioapic)
1723 union IO_APIC_reg_01 reg_01;
1724 unsigned long flags;
1726 spin_lock_irqsave(&ioapic_lock, flags);
1727 reg_01.raw = io_apic_read(ioapic, 1);
1728 spin_unlock_irqrestore(&ioapic_lock, flags);
1730 return reg_01.bits.entries;
1734 int io_apic_set_pci_routing (int ioapic, int pin, int irq)
1736 struct IO_APIC_route_entry entry;
1737 unsigned long flags;
1739 if (!IO_APIC_IRQ(irq)) {
1740 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0/n",
1746 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1747 * Note that we mask (disable) IRQs now -- these get enabled when the
1748 * corresponding device driver registers for this IRQ.
1751 memset(&entry,0,sizeof(entry));
1753 entry.delivery_mode = dest_LowestPrio;
1754 entry.dest_mode = INT_DELIVERY_MODE;
1755 entry.dest.logical.logical_dest = TARGET_CPUS;
1756 entry.mask = 1; /* Disabled (masked) */
1757 entry.trigger = 1; /* Level sensitive */
1758 entry.polarity = 1; /* Low active */
1760 add_pin_to_irq(irq, ioapic, pin);
1762 entry.vector = assign_irq_vector(irq);
1764 printk(KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1765 "IRQ %d)\n", ioapic,
1766 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq);
1768 irq_desc[irq].handler = &ioapic_level_irq_type;
1770 set_intr_gate(entry.vector, interrupt[irq]);
1772 if (!ioapic && (irq < 16))
1773 disable_8259A_irq(irq);
1775 spin_lock_irqsave(&ioapic_lock, flags);
1776 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
1777 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
1778 spin_unlock_irqrestore(&ioapic_lock, flags);
1780 return entry.vector;
1783 #endif /*CONFIG_ACPI_BOOT*/
1786 void send_IPI_self(int vector)
1793 apic_wait_icr_idle();
1794 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
1797 * Send the IPI. The write to APIC_ICR fires this off.
1799 apic_write_around(APIC_ICR, cfg);