[PATCH] x86_64: Fix off by one in IOMMU check
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86_64 / kernel / io_apic.c
blob83ea86e0633a975d3033041785a598064932d4fd
1 /*
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>
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/config.h>
29 #include <linux/smp_lock.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
34 #include <asm/io.h>
35 #include <asm/smp.h>
36 #include <asm/desc.h>
37 #include <asm/proto.h>
38 #include <asm/mach_apic.h>
39 #include <asm/acpi.h>
40 #include <asm/dma.h>
42 #define __apicdebuginit __init
44 int sis_apic_bug; /* not actually supported, dummy for compile */
46 static int no_timer_check;
48 int disable_timer_pin_1 __initdata;
50 static DEFINE_SPINLOCK(ioapic_lock);
53 * # of IRQ routing registers
55 int nr_ioapic_registers[MAX_IO_APICS];
58 * Rough estimation of how many shared IRQs there are, can
59 * be changed anytime.
61 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
62 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
65 * This is performance-critical, we want to do it O(1)
67 * the indexing order of this array favors 1:1 mappings
68 * between pins and IRQs.
71 static struct irq_pin_list {
72 short apic, pin, next;
73 } irq_2_pin[PIN_MAP_SIZE];
75 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
76 #ifdef CONFIG_PCI_MSI
77 #define vector_to_irq(vector) \
78 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
79 #else
80 #define vector_to_irq(vector) (vector)
81 #endif
83 #define __DO_ACTION(R, ACTION, FINAL) \
85 { \
86 int pin; \
87 struct irq_pin_list *entry = irq_2_pin + irq; \
89 BUG_ON(irq >= NR_IRQS); \
90 for (;;) { \
91 unsigned int reg; \
92 pin = entry->pin; \
93 if (pin == -1) \
94 break; \
95 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
96 reg ACTION; \
97 io_apic_modify(entry->apic, reg); \
98 if (!entry->next) \
99 break; \
100 entry = irq_2_pin + entry->next; \
102 FINAL; \
105 #ifdef CONFIG_SMP
106 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
108 unsigned long flags;
109 unsigned int dest;
110 cpumask_t tmp;
112 cpus_and(tmp, mask, cpu_online_map);
113 if (cpus_empty(tmp))
114 tmp = TARGET_CPUS;
116 cpus_and(mask, tmp, CPU_MASK_ALL);
118 dest = cpu_mask_to_apicid(mask);
121 * Only the high 8 bits are valid.
123 dest = SET_APIC_LOGICAL_ID(dest);
125 spin_lock_irqsave(&ioapic_lock, flags);
126 __DO_ACTION(1, = dest, )
127 set_irq_info(irq, mask);
128 spin_unlock_irqrestore(&ioapic_lock, flags);
130 #endif
132 static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
135 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
136 * shared ISA-space IRQs, so we have to support them. We are super
137 * fast in the common case, and fast for shared ISA-space IRQs.
139 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
141 static int first_free_entry = NR_IRQS;
142 struct irq_pin_list *entry = irq_2_pin + irq;
144 BUG_ON(irq >= NR_IRQS);
145 while (entry->next)
146 entry = irq_2_pin + entry->next;
148 if (entry->pin != -1) {
149 entry->next = first_free_entry;
150 entry = irq_2_pin + entry->next;
151 if (++first_free_entry >= PIN_MAP_SIZE)
152 panic("io_apic.c: ran out of irq_2_pin entries!");
154 entry->apic = apic;
155 entry->pin = pin;
159 #define DO_ACTION(name,R,ACTION, FINAL) \
161 static void name##_IO_APIC_irq (unsigned int irq) \
162 __DO_ACTION(R, ACTION, FINAL)
164 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
165 /* mask = 1 */
166 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
167 /* mask = 0 */
169 static void mask_IO_APIC_irq (unsigned int irq)
171 unsigned long flags;
173 spin_lock_irqsave(&ioapic_lock, flags);
174 __mask_IO_APIC_irq(irq);
175 spin_unlock_irqrestore(&ioapic_lock, flags);
178 static void unmask_IO_APIC_irq (unsigned int irq)
180 unsigned long flags;
182 spin_lock_irqsave(&ioapic_lock, flags);
183 __unmask_IO_APIC_irq(irq);
184 spin_unlock_irqrestore(&ioapic_lock, flags);
187 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
189 struct IO_APIC_route_entry entry;
190 unsigned long flags;
192 /* Check delivery_mode to be sure we're not clearing an SMI pin */
193 spin_lock_irqsave(&ioapic_lock, flags);
194 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
195 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
196 spin_unlock_irqrestore(&ioapic_lock, flags);
197 if (entry.delivery_mode == dest_SMI)
198 return;
200 * Disable it in the IO-APIC irq-routing table:
202 memset(&entry, 0, sizeof(entry));
203 entry.mask = 1;
204 spin_lock_irqsave(&ioapic_lock, flags);
205 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
206 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
207 spin_unlock_irqrestore(&ioapic_lock, flags);
210 static void clear_IO_APIC (void)
212 int apic, pin;
214 for (apic = 0; apic < nr_ioapics; apic++)
215 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
216 clear_IO_APIC_pin(apic, pin);
220 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
221 * specific CPU-side IRQs.
224 #define MAX_PIRQS 8
225 static int pirq_entries [MAX_PIRQS];
226 static int pirqs_enabled;
227 int skip_ioapic_setup;
228 int ioapic_force;
230 /* dummy parsing: see setup.c */
232 static int __init disable_ioapic_setup(char *str)
234 skip_ioapic_setup = 1;
235 return 1;
238 static int __init enable_ioapic_setup(char *str)
240 ioapic_force = 1;
241 skip_ioapic_setup = 0;
242 return 1;
245 __setup("noapic", disable_ioapic_setup);
246 __setup("apic", enable_ioapic_setup);
248 #include <asm/pci-direct.h>
249 #include <linux/pci_ids.h>
250 #include <linux/pci.h>
252 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
253 off. Check for an Nvidia or VIA PCI bridge and turn it off.
254 Use pci direct infrastructure because this runs before the PCI subsystem.
256 Can be overwritten with "apic"
258 And another hack to disable the IOMMU on VIA chipsets.
260 Kludge-O-Rama. */
261 void __init check_ioapic(void)
263 int num,slot,func;
264 /* Poor man's PCI discovery */
265 for (num = 0; num < 32; num++) {
266 for (slot = 0; slot < 32; slot++) {
267 for (func = 0; func < 8; func++) {
268 u32 class;
269 u32 vendor;
270 u8 type;
271 class = read_pci_config(num,slot,func,
272 PCI_CLASS_REVISION);
273 if (class == 0xffffffff)
274 break;
276 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
277 continue;
279 vendor = read_pci_config(num, slot, func,
280 PCI_VENDOR_ID);
281 vendor &= 0xffff;
282 switch (vendor) {
283 case PCI_VENDOR_ID_VIA:
284 #ifdef CONFIG_GART_IOMMU
285 if ((end_pfn > MAX_DMA32_PFN ||
286 force_iommu) &&
287 !iommu_aperture_allowed) {
288 printk(KERN_INFO
289 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
290 iommu_aperture_disabled = 1;
292 #endif
293 return;
294 case PCI_VENDOR_ID_NVIDIA:
295 #ifdef CONFIG_ACPI
296 /* All timer overrides on Nvidia
297 seem to be wrong. Skip them. */
298 acpi_skip_timer_override = 1;
299 printk(KERN_INFO
300 "Nvidia board detected. Ignoring ACPI timer override.\n");
301 #endif
302 /* RED-PEN skip them on mptables too? */
303 return;
306 /* No multi-function device? */
307 type = read_pci_config_byte(num,slot,func,
308 PCI_HEADER_TYPE);
309 if (!(type & 0x80))
310 break;
316 static int __init ioapic_pirq_setup(char *str)
318 int i, max;
319 int ints[MAX_PIRQS+1];
321 get_options(str, ARRAY_SIZE(ints), ints);
323 for (i = 0; i < MAX_PIRQS; i++)
324 pirq_entries[i] = -1;
326 pirqs_enabled = 1;
327 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
328 max = MAX_PIRQS;
329 if (ints[0] < MAX_PIRQS)
330 max = ints[0];
332 for (i = 0; i < max; i++) {
333 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
335 * PIRQs are mapped upside down, usually.
337 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
339 return 1;
342 __setup("pirq=", ioapic_pirq_setup);
345 * Find the IRQ entry number of a certain pin.
347 static int find_irq_entry(int apic, int pin, int type)
349 int i;
351 for (i = 0; i < mp_irq_entries; i++)
352 if (mp_irqs[i].mpc_irqtype == type &&
353 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
354 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
355 mp_irqs[i].mpc_dstirq == pin)
356 return i;
358 return -1;
362 * Find the pin to which IRQ[irq] (ISA) is connected
364 static int find_isa_irq_pin(int irq, int type)
366 int i;
368 for (i = 0; i < mp_irq_entries; i++) {
369 int lbus = mp_irqs[i].mpc_srcbus;
371 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
372 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
373 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
374 (mp_irqs[i].mpc_irqtype == type) &&
375 (mp_irqs[i].mpc_srcbusirq == irq))
377 return mp_irqs[i].mpc_dstirq;
379 return -1;
383 * Find a specific PCI IRQ entry.
384 * Not an __init, possibly needed by modules
386 static int pin_2_irq(int idx, int apic, int pin);
388 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
390 int apic, i, best_guess = -1;
392 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
393 bus, slot, pin);
394 if (mp_bus_id_to_pci_bus[bus] == -1) {
395 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
396 return -1;
398 for (i = 0; i < mp_irq_entries; i++) {
399 int lbus = mp_irqs[i].mpc_srcbus;
401 for (apic = 0; apic < nr_ioapics; apic++)
402 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
403 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
404 break;
406 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
407 !mp_irqs[i].mpc_irqtype &&
408 (bus == lbus) &&
409 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
410 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
412 if (!(apic || IO_APIC_IRQ(irq)))
413 continue;
415 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
416 return irq;
418 * Use the first all-but-pin matching entry as a
419 * best-guess fuzzy result for broken mptables.
421 if (best_guess < 0)
422 best_guess = irq;
425 BUG_ON(best_guess >= NR_IRQS);
426 return best_guess;
430 * EISA Edge/Level control register, ELCR
432 static int EISA_ELCR(unsigned int irq)
434 if (irq < 16) {
435 unsigned int port = 0x4d0 + (irq >> 3);
436 return (inb(port) >> (irq & 7)) & 1;
438 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
439 return 0;
442 /* EISA interrupts are always polarity zero and can be edge or level
443 * trigger depending on the ELCR value. If an interrupt is listed as
444 * EISA conforming in the MP table, that means its trigger type must
445 * be read in from the ELCR */
447 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
448 #define default_EISA_polarity(idx) (0)
450 /* ISA interrupts are always polarity zero edge triggered,
451 * when listed as conforming in the MP table. */
453 #define default_ISA_trigger(idx) (0)
454 #define default_ISA_polarity(idx) (0)
456 /* PCI interrupts are always polarity one level triggered,
457 * when listed as conforming in the MP table. */
459 #define default_PCI_trigger(idx) (1)
460 #define default_PCI_polarity(idx) (1)
462 /* MCA interrupts are always polarity zero level triggered,
463 * when listed as conforming in the MP table. */
465 #define default_MCA_trigger(idx) (1)
466 #define default_MCA_polarity(idx) (0)
468 static int __init MPBIOS_polarity(int idx)
470 int bus = mp_irqs[idx].mpc_srcbus;
471 int polarity;
474 * Determine IRQ line polarity (high active or low active):
476 switch (mp_irqs[idx].mpc_irqflag & 3)
478 case 0: /* conforms, ie. bus-type dependent polarity */
480 switch (mp_bus_id_to_type[bus])
482 case MP_BUS_ISA: /* ISA pin */
484 polarity = default_ISA_polarity(idx);
485 break;
487 case MP_BUS_EISA: /* EISA pin */
489 polarity = default_EISA_polarity(idx);
490 break;
492 case MP_BUS_PCI: /* PCI pin */
494 polarity = default_PCI_polarity(idx);
495 break;
497 case MP_BUS_MCA: /* MCA pin */
499 polarity = default_MCA_polarity(idx);
500 break;
502 default:
504 printk(KERN_WARNING "broken BIOS!!\n");
505 polarity = 1;
506 break;
509 break;
511 case 1: /* high active */
513 polarity = 0;
514 break;
516 case 2: /* reserved */
518 printk(KERN_WARNING "broken BIOS!!\n");
519 polarity = 1;
520 break;
522 case 3: /* low active */
524 polarity = 1;
525 break;
527 default: /* invalid */
529 printk(KERN_WARNING "broken BIOS!!\n");
530 polarity = 1;
531 break;
534 return polarity;
537 static int MPBIOS_trigger(int idx)
539 int bus = mp_irqs[idx].mpc_srcbus;
540 int trigger;
543 * Determine IRQ trigger mode (edge or level sensitive):
545 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
547 case 0: /* conforms, ie. bus-type dependent */
549 switch (mp_bus_id_to_type[bus])
551 case MP_BUS_ISA: /* ISA pin */
553 trigger = default_ISA_trigger(idx);
554 break;
556 case MP_BUS_EISA: /* EISA pin */
558 trigger = default_EISA_trigger(idx);
559 break;
561 case MP_BUS_PCI: /* PCI pin */
563 trigger = default_PCI_trigger(idx);
564 break;
566 case MP_BUS_MCA: /* MCA pin */
568 trigger = default_MCA_trigger(idx);
569 break;
571 default:
573 printk(KERN_WARNING "broken BIOS!!\n");
574 trigger = 1;
575 break;
578 break;
580 case 1: /* edge */
582 trigger = 0;
583 break;
585 case 2: /* reserved */
587 printk(KERN_WARNING "broken BIOS!!\n");
588 trigger = 1;
589 break;
591 case 3: /* level */
593 trigger = 1;
594 break;
596 default: /* invalid */
598 printk(KERN_WARNING "broken BIOS!!\n");
599 trigger = 0;
600 break;
603 return trigger;
606 static inline int irq_polarity(int idx)
608 return MPBIOS_polarity(idx);
611 static inline int irq_trigger(int idx)
613 return MPBIOS_trigger(idx);
616 static int next_irq = 16;
619 * gsi_irq_sharing -- Name overload! "irq" can be either a legacy IRQ
620 * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
621 * from ACPI, which can reach 800 in large boxen.
623 * Compact the sparse GSI space into a sequential IRQ series and reuse
624 * vectors if possible.
626 int gsi_irq_sharing(int gsi)
628 int i, tries, vector;
630 BUG_ON(gsi >= NR_IRQ_VECTORS);
632 if (platform_legacy_irq(gsi))
633 return gsi;
635 if (gsi_2_irq[gsi] != 0xFF)
636 return (int)gsi_2_irq[gsi];
638 tries = NR_IRQS;
639 try_again:
640 vector = assign_irq_vector(gsi);
643 * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
644 * use of vector and if found, return that IRQ. However, we never want
645 * to share legacy IRQs, which usually have a different trigger mode
646 * than PCI.
648 for (i = 0; i < NR_IRQS; i++)
649 if (IO_APIC_VECTOR(i) == vector)
650 break;
651 if (platform_legacy_irq(i)) {
652 if (--tries >= 0) {
653 IO_APIC_VECTOR(i) = 0;
654 goto try_again;
656 panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
658 if (i < NR_IRQS) {
659 gsi_2_irq[gsi] = i;
660 printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
661 gsi, vector, i);
662 return i;
665 i = next_irq++;
666 BUG_ON(i >= NR_IRQS);
667 gsi_2_irq[gsi] = i;
668 IO_APIC_VECTOR(i) = vector;
669 printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
670 gsi, vector, i);
671 return i;
674 static int pin_2_irq(int idx, int apic, int pin)
676 int irq, i;
677 int bus = mp_irqs[idx].mpc_srcbus;
680 * Debugging check, we are in big trouble if this message pops up!
682 if (mp_irqs[idx].mpc_dstirq != pin)
683 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
685 switch (mp_bus_id_to_type[bus])
687 case MP_BUS_ISA: /* ISA pin */
688 case MP_BUS_EISA:
689 case MP_BUS_MCA:
691 irq = mp_irqs[idx].mpc_srcbusirq;
692 break;
694 case MP_BUS_PCI: /* PCI pin */
697 * PCI IRQs are mapped in order
699 i = irq = 0;
700 while (i < apic)
701 irq += nr_ioapic_registers[i++];
702 irq += pin;
703 irq = gsi_irq_sharing(irq);
704 break;
706 default:
708 printk(KERN_ERR "unknown bus type %d.\n",bus);
709 irq = 0;
710 break;
713 BUG_ON(irq >= NR_IRQS);
716 * PCI IRQ command line redirection. Yes, limits are hardcoded.
718 if ((pin >= 16) && (pin <= 23)) {
719 if (pirq_entries[pin-16] != -1) {
720 if (!pirq_entries[pin-16]) {
721 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
722 } else {
723 irq = pirq_entries[pin-16];
724 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
725 pin-16, irq);
729 BUG_ON(irq >= NR_IRQS);
730 return irq;
733 static inline int IO_APIC_irq_trigger(int irq)
735 int apic, idx, pin;
737 for (apic = 0; apic < nr_ioapics; apic++) {
738 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
739 idx = find_irq_entry(apic,pin,mp_INT);
740 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
741 return irq_trigger(idx);
745 * nonexistent IRQs are edge default
747 return 0;
750 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
751 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
753 int assign_irq_vector(int irq)
755 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
757 BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
758 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0)
759 return IO_APIC_VECTOR(irq);
760 next:
761 current_vector += 8;
762 if (current_vector == IA32_SYSCALL_VECTOR)
763 goto next;
765 if (current_vector >= FIRST_SYSTEM_VECTOR) {
766 /* If we run out of vectors on large boxen, must share them. */
767 offset = (offset + 1) % 8;
768 current_vector = FIRST_DEVICE_VECTOR + offset;
771 vector_irq[current_vector] = irq;
772 if (irq != AUTO_ASSIGN)
773 IO_APIC_VECTOR(irq) = current_vector;
775 return current_vector;
778 extern void (*interrupt[NR_IRQS])(void);
779 static struct hw_interrupt_type ioapic_level_type;
780 static struct hw_interrupt_type ioapic_edge_type;
782 #define IOAPIC_AUTO -1
783 #define IOAPIC_EDGE 0
784 #define IOAPIC_LEVEL 1
786 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
788 if (use_pci_vector() && !platform_legacy_irq(irq)) {
789 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
790 trigger == IOAPIC_LEVEL)
791 irq_desc[vector].handler = &ioapic_level_type;
792 else
793 irq_desc[vector].handler = &ioapic_edge_type;
794 set_intr_gate(vector, interrupt[vector]);
795 } else {
796 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
797 trigger == IOAPIC_LEVEL)
798 irq_desc[irq].handler = &ioapic_level_type;
799 else
800 irq_desc[irq].handler = &ioapic_edge_type;
801 set_intr_gate(vector, interrupt[irq]);
805 static void __init setup_IO_APIC_irqs(void)
807 struct IO_APIC_route_entry entry;
808 int apic, pin, idx, irq, first_notcon = 1, vector;
809 unsigned long flags;
811 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
813 for (apic = 0; apic < nr_ioapics; apic++) {
814 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
817 * add it to the IO-APIC irq-routing table:
819 memset(&entry,0,sizeof(entry));
821 entry.delivery_mode = INT_DELIVERY_MODE;
822 entry.dest_mode = INT_DEST_MODE;
823 entry.mask = 0; /* enable IRQ */
824 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
826 idx = find_irq_entry(apic,pin,mp_INT);
827 if (idx == -1) {
828 if (first_notcon) {
829 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
830 first_notcon = 0;
831 } else
832 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
833 continue;
836 entry.trigger = irq_trigger(idx);
837 entry.polarity = irq_polarity(idx);
839 if (irq_trigger(idx)) {
840 entry.trigger = 1;
841 entry.mask = 1;
842 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
845 irq = pin_2_irq(idx, apic, pin);
846 add_pin_to_irq(irq, apic, pin);
848 if (!apic && !IO_APIC_IRQ(irq))
849 continue;
851 if (IO_APIC_IRQ(irq)) {
852 vector = assign_irq_vector(irq);
853 entry.vector = vector;
855 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
856 if (!apic && (irq < 16))
857 disable_8259A_irq(irq);
859 spin_lock_irqsave(&ioapic_lock, flags);
860 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
861 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
862 set_native_irq_info(irq, TARGET_CPUS);
863 spin_unlock_irqrestore(&ioapic_lock, flags);
867 if (!first_notcon)
868 apic_printk(APIC_VERBOSE," not connected.\n");
872 * Set up the 8259A-master output pin as broadcast to all
873 * CPUs.
875 static void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
877 struct IO_APIC_route_entry entry;
878 unsigned long flags;
880 memset(&entry,0,sizeof(entry));
882 disable_8259A_irq(0);
884 /* mask LVT0 */
885 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
888 * We use logical delivery to get the timer IRQ
889 * to the first CPU.
891 entry.dest_mode = INT_DEST_MODE;
892 entry.mask = 0; /* unmask IRQ now */
893 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
894 entry.delivery_mode = INT_DELIVERY_MODE;
895 entry.polarity = 0;
896 entry.trigger = 0;
897 entry.vector = vector;
900 * The timer IRQ doesn't have to know that behind the
901 * scene we have a 8259A-master in AEOI mode ...
903 irq_desc[0].handler = &ioapic_edge_type;
906 * Add it to the IO-APIC irq-routing table:
908 spin_lock_irqsave(&ioapic_lock, flags);
909 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
910 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
911 spin_unlock_irqrestore(&ioapic_lock, flags);
913 enable_8259A_irq(0);
916 void __init UNEXPECTED_IO_APIC(void)
920 void __apicdebuginit print_IO_APIC(void)
922 int apic, i;
923 union IO_APIC_reg_00 reg_00;
924 union IO_APIC_reg_01 reg_01;
925 union IO_APIC_reg_02 reg_02;
926 unsigned long flags;
928 if (apic_verbosity == APIC_QUIET)
929 return;
931 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
932 for (i = 0; i < nr_ioapics; i++)
933 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
934 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
937 * We are a bit conservative about what we expect. We have to
938 * know about every hardware change ASAP.
940 printk(KERN_INFO "testing the IO APIC.......................\n");
942 for (apic = 0; apic < nr_ioapics; apic++) {
944 spin_lock_irqsave(&ioapic_lock, flags);
945 reg_00.raw = io_apic_read(apic, 0);
946 reg_01.raw = io_apic_read(apic, 1);
947 if (reg_01.bits.version >= 0x10)
948 reg_02.raw = io_apic_read(apic, 2);
949 spin_unlock_irqrestore(&ioapic_lock, flags);
951 printk("\n");
952 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
953 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
954 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
955 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
956 UNEXPECTED_IO_APIC();
958 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
959 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
960 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
961 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
962 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
963 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
964 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
965 (reg_01.bits.entries != 0x2E) &&
966 (reg_01.bits.entries != 0x3F) &&
967 (reg_01.bits.entries != 0x03)
969 UNEXPECTED_IO_APIC();
971 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
972 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
973 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
974 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
975 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
976 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
977 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
978 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
980 UNEXPECTED_IO_APIC();
981 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
982 UNEXPECTED_IO_APIC();
984 if (reg_01.bits.version >= 0x10) {
985 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
986 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
987 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
988 UNEXPECTED_IO_APIC();
991 printk(KERN_DEBUG ".... IRQ redirection table:\n");
993 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
994 " Stat Dest Deli Vect: \n");
996 for (i = 0; i <= reg_01.bits.entries; i++) {
997 struct IO_APIC_route_entry entry;
999 spin_lock_irqsave(&ioapic_lock, flags);
1000 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
1001 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
1002 spin_unlock_irqrestore(&ioapic_lock, flags);
1004 printk(KERN_DEBUG " %02x %03X %02X ",
1006 entry.dest.logical.logical_dest,
1007 entry.dest.physical.physical_dest
1010 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1011 entry.mask,
1012 entry.trigger,
1013 entry.irr,
1014 entry.polarity,
1015 entry.delivery_status,
1016 entry.dest_mode,
1017 entry.delivery_mode,
1018 entry.vector
1022 if (use_pci_vector())
1023 printk(KERN_INFO "Using vector-based indexing\n");
1024 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1025 for (i = 0; i < NR_IRQS; i++) {
1026 struct irq_pin_list *entry = irq_2_pin + i;
1027 if (entry->pin < 0)
1028 continue;
1029 if (use_pci_vector() && !platform_legacy_irq(i))
1030 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1031 else
1032 printk(KERN_DEBUG "IRQ%d ", i);
1033 for (;;) {
1034 printk("-> %d:%d", entry->apic, entry->pin);
1035 if (!entry->next)
1036 break;
1037 entry = irq_2_pin + entry->next;
1039 printk("\n");
1042 printk(KERN_INFO ".................................... done.\n");
1044 return;
1047 #if 0
1049 static __apicdebuginit void print_APIC_bitfield (int base)
1051 unsigned int v;
1052 int i, j;
1054 if (apic_verbosity == APIC_QUIET)
1055 return;
1057 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1058 for (i = 0; i < 8; i++) {
1059 v = apic_read(base + i*0x10);
1060 for (j = 0; j < 32; j++) {
1061 if (v & (1<<j))
1062 printk("1");
1063 else
1064 printk("0");
1066 printk("\n");
1070 void __apicdebuginit print_local_APIC(void * dummy)
1072 unsigned int v, ver, maxlvt;
1074 if (apic_verbosity == APIC_QUIET)
1075 return;
1077 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1078 smp_processor_id(), hard_smp_processor_id());
1079 v = apic_read(APIC_ID);
1080 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1081 v = apic_read(APIC_LVR);
1082 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1083 ver = GET_APIC_VERSION(v);
1084 maxlvt = get_maxlvt();
1086 v = apic_read(APIC_TASKPRI);
1087 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1089 v = apic_read(APIC_ARBPRI);
1090 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1091 v & APIC_ARBPRI_MASK);
1092 v = apic_read(APIC_PROCPRI);
1093 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1095 v = apic_read(APIC_EOI);
1096 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1097 v = apic_read(APIC_RRR);
1098 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1099 v = apic_read(APIC_LDR);
1100 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1101 v = apic_read(APIC_DFR);
1102 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1103 v = apic_read(APIC_SPIV);
1104 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1106 printk(KERN_DEBUG "... APIC ISR field:\n");
1107 print_APIC_bitfield(APIC_ISR);
1108 printk(KERN_DEBUG "... APIC TMR field:\n");
1109 print_APIC_bitfield(APIC_TMR);
1110 printk(KERN_DEBUG "... APIC IRR field:\n");
1111 print_APIC_bitfield(APIC_IRR);
1113 v = apic_read(APIC_ESR);
1114 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1116 v = apic_read(APIC_ICR);
1117 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1118 v = apic_read(APIC_ICR2);
1119 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1121 v = apic_read(APIC_LVTT);
1122 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1124 if (maxlvt > 3) { /* PC is LVT#4. */
1125 v = apic_read(APIC_LVTPC);
1126 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1128 v = apic_read(APIC_LVT0);
1129 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1130 v = apic_read(APIC_LVT1);
1131 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1133 if (maxlvt > 2) { /* ERR is LVT#3. */
1134 v = apic_read(APIC_LVTERR);
1135 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1138 v = apic_read(APIC_TMICT);
1139 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1140 v = apic_read(APIC_TMCCT);
1141 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1142 v = apic_read(APIC_TDCR);
1143 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1144 printk("\n");
1147 void print_all_local_APICs (void)
1149 on_each_cpu(print_local_APIC, NULL, 1, 1);
1152 void __apicdebuginit print_PIC(void)
1154 unsigned int v;
1155 unsigned long flags;
1157 if (apic_verbosity == APIC_QUIET)
1158 return;
1160 printk(KERN_DEBUG "\nprinting PIC contents\n");
1162 spin_lock_irqsave(&i8259A_lock, flags);
1164 v = inb(0xa1) << 8 | inb(0x21);
1165 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1167 v = inb(0xa0) << 8 | inb(0x20);
1168 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1170 outb(0x0b,0xa0);
1171 outb(0x0b,0x20);
1172 v = inb(0xa0) << 8 | inb(0x20);
1173 outb(0x0a,0xa0);
1174 outb(0x0a,0x20);
1176 spin_unlock_irqrestore(&i8259A_lock, flags);
1178 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1180 v = inb(0x4d1) << 8 | inb(0x4d0);
1181 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1184 #endif /* 0 */
1186 static void __init enable_IO_APIC(void)
1188 union IO_APIC_reg_01 reg_01;
1189 int i;
1190 unsigned long flags;
1192 for (i = 0; i < PIN_MAP_SIZE; i++) {
1193 irq_2_pin[i].pin = -1;
1194 irq_2_pin[i].next = 0;
1196 if (!pirqs_enabled)
1197 for (i = 0; i < MAX_PIRQS; i++)
1198 pirq_entries[i] = -1;
1201 * The number of IO-APIC IRQ registers (== #pins):
1203 for (i = 0; i < nr_ioapics; i++) {
1204 spin_lock_irqsave(&ioapic_lock, flags);
1205 reg_01.raw = io_apic_read(i, 1);
1206 spin_unlock_irqrestore(&ioapic_lock, flags);
1207 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1211 * Do not trust the IO-APIC being empty at bootup
1213 clear_IO_APIC();
1217 * Not an __init, needed by the reboot code
1219 void disable_IO_APIC(void)
1221 int pin;
1223 * Clear the IO-APIC before rebooting:
1225 clear_IO_APIC();
1228 * If the i8259 is routed through an IOAPIC
1229 * Put that IOAPIC in virtual wire mode
1230 * so legacy interrupts can be delivered.
1232 pin = find_isa_irq_pin(0, mp_ExtINT);
1233 if (pin != -1) {
1234 struct IO_APIC_route_entry entry;
1235 unsigned long flags;
1237 memset(&entry, 0, sizeof(entry));
1238 entry.mask = 0; /* Enabled */
1239 entry.trigger = 0; /* Edge */
1240 entry.irr = 0;
1241 entry.polarity = 0; /* High */
1242 entry.delivery_status = 0;
1243 entry.dest_mode = 0; /* Physical */
1244 entry.delivery_mode = 7; /* ExtInt */
1245 entry.vector = 0;
1246 entry.dest.physical.physical_dest = 0;
1250 * Add it to the IO-APIC irq-routing table:
1252 spin_lock_irqsave(&ioapic_lock, flags);
1253 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
1254 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
1255 spin_unlock_irqrestore(&ioapic_lock, flags);
1258 disconnect_bsp_APIC(pin != -1);
1262 * function to set the IO-APIC physical IDs based on the
1263 * values stored in the MPC table.
1265 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1268 static void __init setup_ioapic_ids_from_mpc (void)
1270 union IO_APIC_reg_00 reg_00;
1271 int apic;
1272 int i;
1273 unsigned char old_id;
1274 unsigned long flags;
1277 * Set the IOAPIC ID to the value stored in the MPC table.
1279 for (apic = 0; apic < nr_ioapics; apic++) {
1281 /* Read the register 0 value */
1282 spin_lock_irqsave(&ioapic_lock, flags);
1283 reg_00.raw = io_apic_read(apic, 0);
1284 spin_unlock_irqrestore(&ioapic_lock, flags);
1286 old_id = mp_ioapics[apic].mpc_apicid;
1289 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1293 * We need to adjust the IRQ routing table
1294 * if the ID changed.
1296 if (old_id != mp_ioapics[apic].mpc_apicid)
1297 for (i = 0; i < mp_irq_entries; i++)
1298 if (mp_irqs[i].mpc_dstapic == old_id)
1299 mp_irqs[i].mpc_dstapic
1300 = mp_ioapics[apic].mpc_apicid;
1303 * Read the right value from the MPC table and
1304 * write it into the ID register.
1306 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1307 mp_ioapics[apic].mpc_apicid);
1309 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1310 spin_lock_irqsave(&ioapic_lock, flags);
1311 io_apic_write(apic, 0, reg_00.raw);
1312 spin_unlock_irqrestore(&ioapic_lock, flags);
1315 * Sanity check
1317 spin_lock_irqsave(&ioapic_lock, flags);
1318 reg_00.raw = io_apic_read(apic, 0);
1319 spin_unlock_irqrestore(&ioapic_lock, flags);
1320 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1321 printk("could not set ID!\n");
1322 else
1323 apic_printk(APIC_VERBOSE," ok.\n");
1328 * There is a nasty bug in some older SMP boards, their mptable lies
1329 * about the timer IRQ. We do the following to work around the situation:
1331 * - timer IRQ defaults to IO-APIC IRQ
1332 * - if this function detects that timer IRQs are defunct, then we fall
1333 * back to ISA timer IRQs
1335 static int __init timer_irq_works(void)
1337 unsigned long t1 = jiffies;
1339 local_irq_enable();
1340 /* Let ten ticks pass... */
1341 mdelay((10 * 1000) / HZ);
1344 * Expect a few ticks at least, to be sure some possible
1345 * glue logic does not lock up after one or two first
1346 * ticks in a non-ExtINT mode. Also the local APIC
1347 * might have cached one ExtINT interrupt. Finally, at
1348 * least one tick may be lost due to delays.
1351 /* jiffies wrap? */
1352 if (jiffies - t1 > 4)
1353 return 1;
1354 return 0;
1358 * In the SMP+IOAPIC case it might happen that there are an unspecified
1359 * number of pending IRQ events unhandled. These cases are very rare,
1360 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1361 * better to do it this way as thus we do not have to be aware of
1362 * 'pending' interrupts in the IRQ path, except at this point.
1365 * Edge triggered needs to resend any interrupt
1366 * that was delayed but this is now handled in the device
1367 * independent code.
1371 * Starting up a edge-triggered IO-APIC interrupt is
1372 * nasty - we need to make sure that we get the edge.
1373 * If it is already asserted for some reason, we need
1374 * return 1 to indicate that is was pending.
1376 * This is not complete - we should be able to fake
1377 * an edge even if it isn't on the 8259A...
1380 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1382 int was_pending = 0;
1383 unsigned long flags;
1385 spin_lock_irqsave(&ioapic_lock, flags);
1386 if (irq < 16) {
1387 disable_8259A_irq(irq);
1388 if (i8259A_irq_pending(irq))
1389 was_pending = 1;
1391 __unmask_IO_APIC_irq(irq);
1392 spin_unlock_irqrestore(&ioapic_lock, flags);
1394 return was_pending;
1398 * Once we have recorded IRQ_PENDING already, we can mask the
1399 * interrupt for real. This prevents IRQ storms from unhandled
1400 * devices.
1402 static void ack_edge_ioapic_irq(unsigned int irq)
1404 move_irq(irq);
1405 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1406 == (IRQ_PENDING | IRQ_DISABLED))
1407 mask_IO_APIC_irq(irq);
1408 ack_APIC_irq();
1412 * Level triggered interrupts can just be masked,
1413 * and shutting down and starting up the interrupt
1414 * is the same as enabling and disabling them -- except
1415 * with a startup need to return a "was pending" value.
1417 * Level triggered interrupts are special because we
1418 * do not touch any IO-APIC register while handling
1419 * them. We ack the APIC in the end-IRQ handler, not
1420 * in the start-IRQ-handler. Protection against reentrance
1421 * from the same interrupt is still provided, both by the
1422 * generic IRQ layer and by the fact that an unacked local
1423 * APIC does not accept IRQs.
1425 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1427 unmask_IO_APIC_irq(irq);
1429 return 0; /* don't check for pending */
1432 static void end_level_ioapic_irq (unsigned int irq)
1434 move_irq(irq);
1435 ack_APIC_irq();
1438 #ifdef CONFIG_PCI_MSI
1439 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1441 int irq = vector_to_irq(vector);
1443 return startup_edge_ioapic_irq(irq);
1446 static void ack_edge_ioapic_vector(unsigned int vector)
1448 int irq = vector_to_irq(vector);
1450 move_native_irq(vector);
1451 ack_edge_ioapic_irq(irq);
1454 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1456 int irq = vector_to_irq(vector);
1458 return startup_level_ioapic_irq (irq);
1461 static void end_level_ioapic_vector (unsigned int vector)
1463 int irq = vector_to_irq(vector);
1465 move_native_irq(vector);
1466 end_level_ioapic_irq(irq);
1469 static void mask_IO_APIC_vector (unsigned int vector)
1471 int irq = vector_to_irq(vector);
1473 mask_IO_APIC_irq(irq);
1476 static void unmask_IO_APIC_vector (unsigned int vector)
1478 int irq = vector_to_irq(vector);
1480 unmask_IO_APIC_irq(irq);
1483 #ifdef CONFIG_SMP
1484 static void set_ioapic_affinity_vector (unsigned int vector,
1485 cpumask_t cpu_mask)
1487 int irq = vector_to_irq(vector);
1489 set_native_irq_info(vector, cpu_mask);
1490 set_ioapic_affinity_irq(irq, cpu_mask);
1492 #endif // CONFIG_SMP
1493 #endif // CONFIG_PCI_MSI
1496 * Level and edge triggered IO-APIC interrupts need different handling,
1497 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1498 * handled with the level-triggered descriptor, but that one has slightly
1499 * more overhead. Level-triggered interrupts cannot be handled with the
1500 * edge-triggered handler, without risking IRQ storms and other ugly
1501 * races.
1504 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1505 .typename = "IO-APIC-edge",
1506 .startup = startup_edge_ioapic,
1507 .shutdown = shutdown_edge_ioapic,
1508 .enable = enable_edge_ioapic,
1509 .disable = disable_edge_ioapic,
1510 .ack = ack_edge_ioapic,
1511 .end = end_edge_ioapic,
1512 #ifdef CONFIG_SMP
1513 .set_affinity = set_ioapic_affinity,
1514 #endif
1517 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1518 .typename = "IO-APIC-level",
1519 .startup = startup_level_ioapic,
1520 .shutdown = shutdown_level_ioapic,
1521 .enable = enable_level_ioapic,
1522 .disable = disable_level_ioapic,
1523 .ack = mask_and_ack_level_ioapic,
1524 .end = end_level_ioapic,
1525 #ifdef CONFIG_SMP
1526 .set_affinity = set_ioapic_affinity,
1527 #endif
1530 static inline void init_IO_APIC_traps(void)
1532 int irq;
1535 * NOTE! The local APIC isn't very good at handling
1536 * multiple interrupts at the same interrupt level.
1537 * As the interrupt level is determined by taking the
1538 * vector number and shifting that right by 4, we
1539 * want to spread these out a bit so that they don't
1540 * all fall in the same interrupt level.
1542 * Also, we've got to be careful not to trash gate
1543 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1545 for (irq = 0; irq < NR_IRQS ; irq++) {
1546 int tmp = irq;
1547 if (use_pci_vector()) {
1548 if (!platform_legacy_irq(tmp))
1549 if ((tmp = vector_to_irq(tmp)) == -1)
1550 continue;
1552 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1554 * Hmm.. We don't have an entry for this,
1555 * so default to an old-fashioned 8259
1556 * interrupt if we can..
1558 if (irq < 16)
1559 make_8259A_irq(irq);
1560 else
1561 /* Strange. Oh, well.. */
1562 irq_desc[irq].handler = &no_irq_type;
1567 static void enable_lapic_irq (unsigned int irq)
1569 unsigned long v;
1571 v = apic_read(APIC_LVT0);
1572 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1575 static void disable_lapic_irq (unsigned int irq)
1577 unsigned long v;
1579 v = apic_read(APIC_LVT0);
1580 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1583 static void ack_lapic_irq (unsigned int irq)
1585 ack_APIC_irq();
1588 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1590 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1591 .typename = "local-APIC-edge",
1592 .startup = NULL, /* startup_irq() not used for IRQ0 */
1593 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1594 .enable = enable_lapic_irq,
1595 .disable = disable_lapic_irq,
1596 .ack = ack_lapic_irq,
1597 .end = end_lapic_irq,
1600 static void setup_nmi (void)
1603 * Dirty trick to enable the NMI watchdog ...
1604 * We put the 8259A master into AEOI mode and
1605 * unmask on all local APICs LVT0 as NMI.
1607 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1608 * is from Maciej W. Rozycki - so we do not have to EOI from
1609 * the NMI handler or the timer interrupt.
1611 printk(KERN_INFO "activating NMI Watchdog ...");
1613 enable_NMI_through_LVT0(NULL);
1615 printk(" done.\n");
1619 * This looks a bit hackish but it's about the only one way of sending
1620 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1621 * not support the ExtINT mode, unfortunately. We need to send these
1622 * cycles as some i82489DX-based boards have glue logic that keeps the
1623 * 8259A interrupt line asserted until INTA. --macro
1625 static inline void unlock_ExtINT_logic(void)
1627 int pin, i;
1628 struct IO_APIC_route_entry entry0, entry1;
1629 unsigned char save_control, save_freq_select;
1630 unsigned long flags;
1632 pin = find_isa_irq_pin(8, mp_INT);
1633 if (pin == -1)
1634 return;
1636 spin_lock_irqsave(&ioapic_lock, flags);
1637 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1638 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1639 spin_unlock_irqrestore(&ioapic_lock, flags);
1640 clear_IO_APIC_pin(0, pin);
1642 memset(&entry1, 0, sizeof(entry1));
1644 entry1.dest_mode = 0; /* physical delivery */
1645 entry1.mask = 0; /* unmask IRQ now */
1646 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1647 entry1.delivery_mode = dest_ExtINT;
1648 entry1.polarity = entry0.polarity;
1649 entry1.trigger = 0;
1650 entry1.vector = 0;
1652 spin_lock_irqsave(&ioapic_lock, flags);
1653 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1654 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1655 spin_unlock_irqrestore(&ioapic_lock, flags);
1657 save_control = CMOS_READ(RTC_CONTROL);
1658 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1659 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1660 RTC_FREQ_SELECT);
1661 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1663 i = 100;
1664 while (i-- > 0) {
1665 mdelay(10);
1666 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1667 i -= 10;
1670 CMOS_WRITE(save_control, RTC_CONTROL);
1671 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1672 clear_IO_APIC_pin(0, pin);
1674 spin_lock_irqsave(&ioapic_lock, flags);
1675 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1676 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1677 spin_unlock_irqrestore(&ioapic_lock, flags);
1681 * This code may look a bit paranoid, but it's supposed to cooperate with
1682 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1683 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1684 * fanatically on his truly buggy board.
1686 static inline void check_timer(void)
1688 int pin1, pin2;
1689 int vector;
1692 * get/set the timer IRQ vector:
1694 disable_8259A_irq(0);
1695 vector = assign_irq_vector(0);
1696 set_intr_gate(vector, interrupt[0]);
1699 * Subtle, code in do_timer_interrupt() expects an AEOI
1700 * mode for the 8259A whenever interrupts are routed
1701 * through I/O APICs. Also IRQ0 has to be enabled in
1702 * the 8259A which implies the virtual wire has to be
1703 * disabled in the local APIC.
1705 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1706 init_8259A(1);
1707 enable_8259A_irq(0);
1709 pin1 = find_isa_irq_pin(0, mp_INT);
1710 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1712 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1714 if (pin1 != -1) {
1716 * Ok, does IRQ0 through the IOAPIC work?
1718 unmask_IO_APIC_irq(0);
1719 if (!no_timer_check && timer_irq_works()) {
1720 nmi_watchdog_default();
1721 if (nmi_watchdog == NMI_IO_APIC) {
1722 disable_8259A_irq(0);
1723 setup_nmi();
1724 enable_8259A_irq(0);
1726 if (disable_timer_pin_1 > 0)
1727 clear_IO_APIC_pin(0, pin1);
1728 return;
1730 clear_IO_APIC_pin(0, pin1);
1731 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1734 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1735 if (pin2 != -1) {
1736 apic_printk(APIC_VERBOSE,"\n..... (found pin %d) ...", pin2);
1738 * legacy devices should be connected to IO APIC #0
1740 setup_ExtINT_IRQ0_pin(pin2, vector);
1741 if (timer_irq_works()) {
1742 printk("works.\n");
1743 nmi_watchdog_default();
1744 if (nmi_watchdog == NMI_IO_APIC) {
1745 setup_nmi();
1747 return;
1750 * Cleanup, just in case ...
1752 clear_IO_APIC_pin(0, pin2);
1754 printk(" failed.\n");
1756 if (nmi_watchdog) {
1757 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1758 nmi_watchdog = 0;
1761 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1763 disable_8259A_irq(0);
1764 irq_desc[0].handler = &lapic_irq_type;
1765 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1766 enable_8259A_irq(0);
1768 if (timer_irq_works()) {
1769 apic_printk(APIC_QUIET, " works.\n");
1770 return;
1772 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1773 apic_printk(APIC_VERBOSE," failed.\n");
1775 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1777 init_8259A(0);
1778 make_8259A_irq(0);
1779 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1781 unlock_ExtINT_logic();
1783 if (timer_irq_works()) {
1784 apic_printk(APIC_VERBOSE," works.\n");
1785 return;
1787 apic_printk(APIC_VERBOSE," failed :(.\n");
1788 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1791 static int __init notimercheck(char *s)
1793 no_timer_check = 1;
1794 return 1;
1796 __setup("no_timer_check", notimercheck);
1800 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1801 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1802 * Linux doesn't really care, as it's not actually used
1803 * for any interrupt handling anyway.
1805 #define PIC_IRQS (1<<2)
1807 void __init setup_IO_APIC(void)
1809 enable_IO_APIC();
1811 if (acpi_ioapic)
1812 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1813 else
1814 io_apic_irqs = ~PIC_IRQS;
1816 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1819 * Set up the IO-APIC IRQ routing table.
1821 if (!acpi_ioapic)
1822 setup_ioapic_ids_from_mpc();
1823 sync_Arb_IDs();
1824 setup_IO_APIC_irqs();
1825 init_IO_APIC_traps();
1826 check_timer();
1827 if (!acpi_ioapic)
1828 print_IO_APIC();
1831 struct sysfs_ioapic_data {
1832 struct sys_device dev;
1833 struct IO_APIC_route_entry entry[0];
1835 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1837 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1839 struct IO_APIC_route_entry *entry;
1840 struct sysfs_ioapic_data *data;
1841 unsigned long flags;
1842 int i;
1844 data = container_of(dev, struct sysfs_ioapic_data, dev);
1845 entry = data->entry;
1846 spin_lock_irqsave(&ioapic_lock, flags);
1847 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1848 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1849 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1851 spin_unlock_irqrestore(&ioapic_lock, flags);
1853 return 0;
1856 static int ioapic_resume(struct sys_device *dev)
1858 struct IO_APIC_route_entry *entry;
1859 struct sysfs_ioapic_data *data;
1860 unsigned long flags;
1861 union IO_APIC_reg_00 reg_00;
1862 int i;
1864 data = container_of(dev, struct sysfs_ioapic_data, dev);
1865 entry = data->entry;
1867 spin_lock_irqsave(&ioapic_lock, flags);
1868 reg_00.raw = io_apic_read(dev->id, 0);
1869 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1870 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1871 io_apic_write(dev->id, 0, reg_00.raw);
1873 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1874 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1875 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1877 spin_unlock_irqrestore(&ioapic_lock, flags);
1879 return 0;
1882 static struct sysdev_class ioapic_sysdev_class = {
1883 set_kset_name("ioapic"),
1884 .suspend = ioapic_suspend,
1885 .resume = ioapic_resume,
1888 static int __init ioapic_init_sysfs(void)
1890 struct sys_device * dev;
1891 int i, size, error = 0;
1893 error = sysdev_class_register(&ioapic_sysdev_class);
1894 if (error)
1895 return error;
1897 for (i = 0; i < nr_ioapics; i++ ) {
1898 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1899 * sizeof(struct IO_APIC_route_entry);
1900 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1901 if (!mp_ioapic_data[i]) {
1902 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1903 continue;
1905 memset(mp_ioapic_data[i], 0, size);
1906 dev = &mp_ioapic_data[i]->dev;
1907 dev->id = i;
1908 dev->cls = &ioapic_sysdev_class;
1909 error = sysdev_register(dev);
1910 if (error) {
1911 kfree(mp_ioapic_data[i]);
1912 mp_ioapic_data[i] = NULL;
1913 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1914 continue;
1918 return 0;
1921 device_initcall(ioapic_init_sysfs);
1923 /* --------------------------------------------------------------------------
1924 ACPI-based IOAPIC Configuration
1925 -------------------------------------------------------------------------- */
1927 #ifdef CONFIG_ACPI
1929 #define IO_APIC_MAX_ID 0xFE
1931 int __init io_apic_get_version (int ioapic)
1933 union IO_APIC_reg_01 reg_01;
1934 unsigned long flags;
1936 spin_lock_irqsave(&ioapic_lock, flags);
1937 reg_01.raw = io_apic_read(ioapic, 1);
1938 spin_unlock_irqrestore(&ioapic_lock, flags);
1940 return reg_01.bits.version;
1944 int __init io_apic_get_redir_entries (int ioapic)
1946 union IO_APIC_reg_01 reg_01;
1947 unsigned long flags;
1949 spin_lock_irqsave(&ioapic_lock, flags);
1950 reg_01.raw = io_apic_read(ioapic, 1);
1951 spin_unlock_irqrestore(&ioapic_lock, flags);
1953 return reg_01.bits.entries;
1957 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
1959 struct IO_APIC_route_entry entry;
1960 unsigned long flags;
1962 if (!IO_APIC_IRQ(irq)) {
1963 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1964 ioapic);
1965 return -EINVAL;
1969 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1970 * Note that we mask (disable) IRQs now -- these get enabled when the
1971 * corresponding device driver registers for this IRQ.
1974 memset(&entry,0,sizeof(entry));
1976 entry.delivery_mode = INT_DELIVERY_MODE;
1977 entry.dest_mode = INT_DEST_MODE;
1978 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1979 entry.trigger = edge_level;
1980 entry.polarity = active_high_low;
1981 entry.mask = 1; /* Disabled (masked) */
1983 irq = gsi_irq_sharing(irq);
1985 * IRQs < 16 are already in the irq_2_pin[] map
1987 if (irq >= 16)
1988 add_pin_to_irq(irq, ioapic, pin);
1990 entry.vector = assign_irq_vector(irq);
1992 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1993 "IRQ %d Mode:%i Active:%i)\n", ioapic,
1994 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
1995 edge_level, active_high_low);
1997 ioapic_register_intr(irq, entry.vector, edge_level);
1999 if (!ioapic && (irq < 16))
2000 disable_8259A_irq(irq);
2002 spin_lock_irqsave(&ioapic_lock, flags);
2003 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2004 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2005 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
2006 spin_unlock_irqrestore(&ioapic_lock, flags);
2008 return 0;
2011 #endif /* CONFIG_ACPI */
2015 * This function currently is only a helper for the i386 smp boot process where
2016 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2017 * so mask in all cases should simply be TARGET_CPUS
2019 #ifdef CONFIG_SMP
2020 void __init setup_ioapic_dest(void)
2022 int pin, ioapic, irq, irq_entry;
2024 if (skip_ioapic_setup == 1)
2025 return;
2027 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2028 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2029 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2030 if (irq_entry == -1)
2031 continue;
2032 irq = pin_2_irq(irq_entry, ioapic, pin);
2033 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2038 #endif