mips: rename offsets.c to asm-offsets.c
[linux-2.6/verdex.git] / arch / x86_64 / kernel / io_apic.c
blob5f1529be1237495ba9a81757ec91c89c684dc1ef
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/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>
33 #include <linux/sysdev.h>
35 #include <asm/io.h>
36 #include <asm/smp.h>
37 #include <asm/desc.h>
38 #include <asm/proto.h>
39 #include <asm/mach_apic.h>
40 #include <asm/acpi.h>
42 #define __apicdebuginit __init
44 int sis_apic_bug; /* not actually supported, dummy for compile */
46 static int no_timer_check;
48 static DEFINE_SPINLOCK(ioapic_lock);
51 * # of IRQ routing registers
53 int nr_ioapic_registers[MAX_IO_APICS];
56 * Rough estimation of how many shared IRQs there are, can
57 * be changed anytime.
59 #define MAX_PLUS_SHARED_IRQS NR_IRQS
60 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
63 * This is performance-critical, we want to do it O(1)
65 * the indexing order of this array favors 1:1 mappings
66 * between pins and IRQs.
69 static struct irq_pin_list {
70 short apic, pin, next;
71 } irq_2_pin[PIN_MAP_SIZE];
73 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
74 #ifdef CONFIG_PCI_MSI
75 #define vector_to_irq(vector) \
76 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
77 #else
78 #define vector_to_irq(vector) (vector)
79 #endif
81 #define __DO_ACTION(R, ACTION, FINAL) \
83 { \
84 int pin; \
85 struct irq_pin_list *entry = irq_2_pin + irq; \
87 for (;;) { \
88 unsigned int reg; \
89 pin = entry->pin; \
90 if (pin == -1) \
91 break; \
92 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
93 reg ACTION; \
94 io_apic_modify(entry->apic, reg); \
95 if (!entry->next) \
96 break; \
97 entry = irq_2_pin + entry->next; \
98 } \
99 FINAL; \
102 #ifdef CONFIG_SMP
103 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
105 unsigned long flags;
106 unsigned int dest;
107 cpumask_t tmp;
109 cpus_and(tmp, mask, cpu_online_map);
110 if (cpus_empty(tmp))
111 tmp = TARGET_CPUS;
113 cpus_and(mask, tmp, CPU_MASK_ALL);
115 dest = cpu_mask_to_apicid(mask);
118 * Only the high 8 bits are valid.
120 dest = SET_APIC_LOGICAL_ID(dest);
122 spin_lock_irqsave(&ioapic_lock, flags);
123 __DO_ACTION(1, = dest, )
124 set_irq_info(irq, mask);
125 spin_unlock_irqrestore(&ioapic_lock, flags);
127 #endif
130 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
131 * shared ISA-space IRQs, so we have to support them. We are super
132 * fast in the common case, and fast for shared ISA-space IRQs.
134 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
136 static int first_free_entry = NR_IRQS;
137 struct irq_pin_list *entry = irq_2_pin + irq;
139 while (entry->next)
140 entry = irq_2_pin + entry->next;
142 if (entry->pin != -1) {
143 entry->next = first_free_entry;
144 entry = irq_2_pin + entry->next;
145 if (++first_free_entry >= PIN_MAP_SIZE)
146 panic("io_apic.c: whoops");
148 entry->apic = apic;
149 entry->pin = pin;
153 #define DO_ACTION(name,R,ACTION, FINAL) \
155 static void name##_IO_APIC_irq (unsigned int irq) \
156 __DO_ACTION(R, ACTION, FINAL)
158 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
159 /* mask = 1 */
160 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
161 /* mask = 0 */
163 static void mask_IO_APIC_irq (unsigned int irq)
165 unsigned long flags;
167 spin_lock_irqsave(&ioapic_lock, flags);
168 __mask_IO_APIC_irq(irq);
169 spin_unlock_irqrestore(&ioapic_lock, flags);
172 static void unmask_IO_APIC_irq (unsigned int irq)
174 unsigned long flags;
176 spin_lock_irqsave(&ioapic_lock, flags);
177 __unmask_IO_APIC_irq(irq);
178 spin_unlock_irqrestore(&ioapic_lock, flags);
181 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
183 struct IO_APIC_route_entry entry;
184 unsigned long flags;
186 /* Check delivery_mode to be sure we're not clearing an SMI pin */
187 spin_lock_irqsave(&ioapic_lock, flags);
188 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
189 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
190 spin_unlock_irqrestore(&ioapic_lock, flags);
191 if (entry.delivery_mode == dest_SMI)
192 return;
194 * Disable it in the IO-APIC irq-routing table:
196 memset(&entry, 0, sizeof(entry));
197 entry.mask = 1;
198 spin_lock_irqsave(&ioapic_lock, flags);
199 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
200 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
201 spin_unlock_irqrestore(&ioapic_lock, flags);
204 static void clear_IO_APIC (void)
206 int apic, pin;
208 for (apic = 0; apic < nr_ioapics; apic++)
209 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
210 clear_IO_APIC_pin(apic, pin);
214 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
215 * specific CPU-side IRQs.
218 #define MAX_PIRQS 8
219 static int pirq_entries [MAX_PIRQS];
220 static int pirqs_enabled;
221 int skip_ioapic_setup;
222 int ioapic_force;
224 /* dummy parsing: see setup.c */
226 static int __init disable_ioapic_setup(char *str)
228 skip_ioapic_setup = 1;
229 return 1;
232 static int __init enable_ioapic_setup(char *str)
234 ioapic_force = 1;
235 skip_ioapic_setup = 0;
236 return 1;
239 __setup("noapic", disable_ioapic_setup);
240 __setup("apic", enable_ioapic_setup);
242 #include <asm/pci-direct.h>
243 #include <linux/pci_ids.h>
244 #include <linux/pci.h>
246 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
247 off. Check for an Nvidia or VIA PCI bridge and turn it off.
248 Use pci direct infrastructure because this runs before the PCI subsystem.
250 Can be overwritten with "apic"
252 And another hack to disable the IOMMU on VIA chipsets.
254 Kludge-O-Rama. */
255 void __init check_ioapic(void)
257 int num,slot,func;
258 if (ioapic_force)
259 return;
261 /* Poor man's PCI discovery */
262 for (num = 0; num < 32; num++) {
263 for (slot = 0; slot < 32; slot++) {
264 for (func = 0; func < 8; func++) {
265 u32 class;
266 u32 vendor;
267 u8 type;
268 class = read_pci_config(num,slot,func,
269 PCI_CLASS_REVISION);
270 if (class == 0xffffffff)
271 break;
273 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
274 continue;
276 vendor = read_pci_config(num, slot, func,
277 PCI_VENDOR_ID);
278 vendor &= 0xffff;
279 switch (vendor) {
280 case PCI_VENDOR_ID_VIA:
281 #ifdef CONFIG_GART_IOMMU
282 if ((end_pfn >= (0xffffffff>>PAGE_SHIFT) ||
283 force_iommu) &&
284 !iommu_aperture_allowed) {
285 printk(KERN_INFO
286 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
287 iommu_aperture_disabled = 1;
289 #endif
290 return;
291 case PCI_VENDOR_ID_NVIDIA:
292 #ifdef CONFIG_ACPI
293 /* All timer overrides on Nvidia
294 seem to be wrong. Skip them. */
295 acpi_skip_timer_override = 1;
296 printk(KERN_INFO
297 "Nvidia board detected. Ignoring ACPI timer override.\n");
298 #endif
299 /* RED-PEN skip them on mptables too? */
300 return;
303 /* No multi-function device? */
304 type = read_pci_config_byte(num,slot,func,
305 PCI_HEADER_TYPE);
306 if (!(type & 0x80))
307 break;
313 static int __init ioapic_pirq_setup(char *str)
315 int i, max;
316 int ints[MAX_PIRQS+1];
318 get_options(str, ARRAY_SIZE(ints), ints);
320 for (i = 0; i < MAX_PIRQS; i++)
321 pirq_entries[i] = -1;
323 pirqs_enabled = 1;
324 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
325 max = MAX_PIRQS;
326 if (ints[0] < MAX_PIRQS)
327 max = ints[0];
329 for (i = 0; i < max; i++) {
330 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
332 * PIRQs are mapped upside down, usually.
334 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
336 return 1;
339 __setup("pirq=", ioapic_pirq_setup);
342 * Find the IRQ entry number of a certain pin.
344 static int find_irq_entry(int apic, int pin, int type)
346 int i;
348 for (i = 0; i < mp_irq_entries; i++)
349 if (mp_irqs[i].mpc_irqtype == type &&
350 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
351 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
352 mp_irqs[i].mpc_dstirq == pin)
353 return i;
355 return -1;
359 * Find the pin to which IRQ[irq] (ISA) is connected
361 static int find_isa_irq_pin(int irq, int type)
363 int i;
365 for (i = 0; i < mp_irq_entries; i++) {
366 int lbus = mp_irqs[i].mpc_srcbus;
368 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
369 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
370 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
371 (mp_irqs[i].mpc_irqtype == type) &&
372 (mp_irqs[i].mpc_srcbusirq == irq))
374 return mp_irqs[i].mpc_dstirq;
376 return -1;
380 * Find a specific PCI IRQ entry.
381 * Not an __init, possibly needed by modules
383 static int pin_2_irq(int idx, int apic, int pin);
385 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
387 int apic, i, best_guess = -1;
389 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
390 bus, slot, pin);
391 if (mp_bus_id_to_pci_bus[bus] == -1) {
392 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
393 return -1;
395 for (i = 0; i < mp_irq_entries; i++) {
396 int lbus = mp_irqs[i].mpc_srcbus;
398 for (apic = 0; apic < nr_ioapics; apic++)
399 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
400 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
401 break;
403 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
404 !mp_irqs[i].mpc_irqtype &&
405 (bus == lbus) &&
406 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
407 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
409 if (!(apic || IO_APIC_IRQ(irq)))
410 continue;
412 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
413 return irq;
415 * Use the first all-but-pin matching entry as a
416 * best-guess fuzzy result for broken mptables.
418 if (best_guess < 0)
419 best_guess = irq;
422 return best_guess;
426 * EISA Edge/Level control register, ELCR
428 static int EISA_ELCR(unsigned int irq)
430 if (irq < 16) {
431 unsigned int port = 0x4d0 + (irq >> 3);
432 return (inb(port) >> (irq & 7)) & 1;
434 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
435 return 0;
438 /* EISA interrupts are always polarity zero and can be edge or level
439 * trigger depending on the ELCR value. If an interrupt is listed as
440 * EISA conforming in the MP table, that means its trigger type must
441 * be read in from the ELCR */
443 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
444 #define default_EISA_polarity(idx) (0)
446 /* ISA interrupts are always polarity zero edge triggered,
447 * when listed as conforming in the MP table. */
449 #define default_ISA_trigger(idx) (0)
450 #define default_ISA_polarity(idx) (0)
452 /* PCI interrupts are always polarity one level triggered,
453 * when listed as conforming in the MP table. */
455 #define default_PCI_trigger(idx) (1)
456 #define default_PCI_polarity(idx) (1)
458 /* MCA interrupts are always polarity zero level triggered,
459 * when listed as conforming in the MP table. */
461 #define default_MCA_trigger(idx) (1)
462 #define default_MCA_polarity(idx) (0)
464 static int __init MPBIOS_polarity(int idx)
466 int bus = mp_irqs[idx].mpc_srcbus;
467 int polarity;
470 * Determine IRQ line polarity (high active or low active):
472 switch (mp_irqs[idx].mpc_irqflag & 3)
474 case 0: /* conforms, ie. bus-type dependent polarity */
476 switch (mp_bus_id_to_type[bus])
478 case MP_BUS_ISA: /* ISA pin */
480 polarity = default_ISA_polarity(idx);
481 break;
483 case MP_BUS_EISA: /* EISA pin */
485 polarity = default_EISA_polarity(idx);
486 break;
488 case MP_BUS_PCI: /* PCI pin */
490 polarity = default_PCI_polarity(idx);
491 break;
493 case MP_BUS_MCA: /* MCA pin */
495 polarity = default_MCA_polarity(idx);
496 break;
498 default:
500 printk(KERN_WARNING "broken BIOS!!\n");
501 polarity = 1;
502 break;
505 break;
507 case 1: /* high active */
509 polarity = 0;
510 break;
512 case 2: /* reserved */
514 printk(KERN_WARNING "broken BIOS!!\n");
515 polarity = 1;
516 break;
518 case 3: /* low active */
520 polarity = 1;
521 break;
523 default: /* invalid */
525 printk(KERN_WARNING "broken BIOS!!\n");
526 polarity = 1;
527 break;
530 return polarity;
533 static int MPBIOS_trigger(int idx)
535 int bus = mp_irqs[idx].mpc_srcbus;
536 int trigger;
539 * Determine IRQ trigger mode (edge or level sensitive):
541 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
543 case 0: /* conforms, ie. bus-type dependent */
545 switch (mp_bus_id_to_type[bus])
547 case MP_BUS_ISA: /* ISA pin */
549 trigger = default_ISA_trigger(idx);
550 break;
552 case MP_BUS_EISA: /* EISA pin */
554 trigger = default_EISA_trigger(idx);
555 break;
557 case MP_BUS_PCI: /* PCI pin */
559 trigger = default_PCI_trigger(idx);
560 break;
562 case MP_BUS_MCA: /* MCA pin */
564 trigger = default_MCA_trigger(idx);
565 break;
567 default:
569 printk(KERN_WARNING "broken BIOS!!\n");
570 trigger = 1;
571 break;
574 break;
576 case 1: /* edge */
578 trigger = 0;
579 break;
581 case 2: /* reserved */
583 printk(KERN_WARNING "broken BIOS!!\n");
584 trigger = 1;
585 break;
587 case 3: /* level */
589 trigger = 1;
590 break;
592 default: /* invalid */
594 printk(KERN_WARNING "broken BIOS!!\n");
595 trigger = 0;
596 break;
599 return trigger;
602 static inline int irq_polarity(int idx)
604 return MPBIOS_polarity(idx);
607 static inline int irq_trigger(int idx)
609 return MPBIOS_trigger(idx);
612 static int pin_2_irq(int idx, int apic, int pin)
614 int irq, i;
615 int bus = mp_irqs[idx].mpc_srcbus;
618 * Debugging check, we are in big trouble if this message pops up!
620 if (mp_irqs[idx].mpc_dstirq != pin)
621 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
623 switch (mp_bus_id_to_type[bus])
625 case MP_BUS_ISA: /* ISA pin */
626 case MP_BUS_EISA:
627 case MP_BUS_MCA:
629 irq = mp_irqs[idx].mpc_srcbusirq;
630 break;
632 case MP_BUS_PCI: /* PCI pin */
635 * PCI IRQs are mapped in order
637 i = irq = 0;
638 while (i < apic)
639 irq += nr_ioapic_registers[i++];
640 irq += pin;
641 break;
643 default:
645 printk(KERN_ERR "unknown bus type %d.\n",bus);
646 irq = 0;
647 break;
652 * PCI IRQ command line redirection. Yes, limits are hardcoded.
654 if ((pin >= 16) && (pin <= 23)) {
655 if (pirq_entries[pin-16] != -1) {
656 if (!pirq_entries[pin-16]) {
657 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
658 } else {
659 irq = pirq_entries[pin-16];
660 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
661 pin-16, irq);
665 return irq;
668 static inline int IO_APIC_irq_trigger(int irq)
670 int apic, idx, pin;
672 for (apic = 0; apic < nr_ioapics; apic++) {
673 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
674 idx = find_irq_entry(apic,pin,mp_INT);
675 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
676 return irq_trigger(idx);
680 * nonexistent IRQs are edge default
682 return 0;
685 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
686 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
688 int assign_irq_vector(int irq)
690 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
692 BUG_ON(irq >= NR_IRQ_VECTORS);
693 if (IO_APIC_VECTOR(irq) > 0)
694 return IO_APIC_VECTOR(irq);
695 next:
696 current_vector += 8;
697 if (current_vector == IA32_SYSCALL_VECTOR)
698 goto next;
700 if (current_vector >= FIRST_SYSTEM_VECTOR) {
701 offset++;
702 if (!(offset%8))
703 return -ENOSPC;
704 current_vector = FIRST_DEVICE_VECTOR + offset;
707 vector_irq[current_vector] = irq;
708 if (irq != AUTO_ASSIGN)
709 IO_APIC_VECTOR(irq) = current_vector;
711 return current_vector;
714 extern void (*interrupt[NR_IRQS])(void);
715 static struct hw_interrupt_type ioapic_level_type;
716 static struct hw_interrupt_type ioapic_edge_type;
718 #define IOAPIC_AUTO -1
719 #define IOAPIC_EDGE 0
720 #define IOAPIC_LEVEL 1
722 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
724 if (use_pci_vector() && !platform_legacy_irq(irq)) {
725 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
726 trigger == IOAPIC_LEVEL)
727 irq_desc[vector].handler = &ioapic_level_type;
728 else
729 irq_desc[vector].handler = &ioapic_edge_type;
730 set_intr_gate(vector, interrupt[vector]);
731 } else {
732 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
733 trigger == IOAPIC_LEVEL)
734 irq_desc[irq].handler = &ioapic_level_type;
735 else
736 irq_desc[irq].handler = &ioapic_edge_type;
737 set_intr_gate(vector, interrupt[irq]);
741 static void __init setup_IO_APIC_irqs(void)
743 struct IO_APIC_route_entry entry;
744 int apic, pin, idx, irq, first_notcon = 1, vector;
745 unsigned long flags;
747 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
749 for (apic = 0; apic < nr_ioapics; apic++) {
750 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
753 * add it to the IO-APIC irq-routing table:
755 memset(&entry,0,sizeof(entry));
757 entry.delivery_mode = INT_DELIVERY_MODE;
758 entry.dest_mode = INT_DEST_MODE;
759 entry.mask = 0; /* enable IRQ */
760 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
762 idx = find_irq_entry(apic,pin,mp_INT);
763 if (idx == -1) {
764 if (first_notcon) {
765 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
766 first_notcon = 0;
767 } else
768 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
769 continue;
772 entry.trigger = irq_trigger(idx);
773 entry.polarity = irq_polarity(idx);
775 if (irq_trigger(idx)) {
776 entry.trigger = 1;
777 entry.mask = 1;
778 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
781 irq = pin_2_irq(idx, apic, pin);
782 add_pin_to_irq(irq, apic, pin);
784 if (!apic && !IO_APIC_IRQ(irq))
785 continue;
787 if (IO_APIC_IRQ(irq)) {
788 vector = assign_irq_vector(irq);
789 entry.vector = vector;
791 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
792 if (!apic && (irq < 16))
793 disable_8259A_irq(irq);
795 spin_lock_irqsave(&ioapic_lock, flags);
796 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
797 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
798 set_native_irq_info(irq, TARGET_CPUS);
799 spin_unlock_irqrestore(&ioapic_lock, flags);
803 if (!first_notcon)
804 apic_printk(APIC_VERBOSE," not connected.\n");
808 * Set up the 8259A-master output pin as broadcast to all
809 * CPUs.
811 static void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
813 struct IO_APIC_route_entry entry;
814 unsigned long flags;
816 memset(&entry,0,sizeof(entry));
818 disable_8259A_irq(0);
820 /* mask LVT0 */
821 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
824 * We use logical delivery to get the timer IRQ
825 * to the first CPU.
827 entry.dest_mode = INT_DEST_MODE;
828 entry.mask = 0; /* unmask IRQ now */
829 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
830 entry.delivery_mode = INT_DELIVERY_MODE;
831 entry.polarity = 0;
832 entry.trigger = 0;
833 entry.vector = vector;
836 * The timer IRQ doesn't have to know that behind the
837 * scene we have a 8259A-master in AEOI mode ...
839 irq_desc[0].handler = &ioapic_edge_type;
842 * Add it to the IO-APIC irq-routing table:
844 spin_lock_irqsave(&ioapic_lock, flags);
845 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
846 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
847 spin_unlock_irqrestore(&ioapic_lock, flags);
849 enable_8259A_irq(0);
852 void __init UNEXPECTED_IO_APIC(void)
856 void __apicdebuginit print_IO_APIC(void)
858 int apic, i;
859 union IO_APIC_reg_00 reg_00;
860 union IO_APIC_reg_01 reg_01;
861 union IO_APIC_reg_02 reg_02;
862 unsigned long flags;
864 if (apic_verbosity == APIC_QUIET)
865 return;
867 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
868 for (i = 0; i < nr_ioapics; i++)
869 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
870 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
873 * We are a bit conservative about what we expect. We have to
874 * know about every hardware change ASAP.
876 printk(KERN_INFO "testing the IO APIC.......................\n");
878 for (apic = 0; apic < nr_ioapics; apic++) {
880 spin_lock_irqsave(&ioapic_lock, flags);
881 reg_00.raw = io_apic_read(apic, 0);
882 reg_01.raw = io_apic_read(apic, 1);
883 if (reg_01.bits.version >= 0x10)
884 reg_02.raw = io_apic_read(apic, 2);
885 spin_unlock_irqrestore(&ioapic_lock, flags);
887 printk("\n");
888 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
889 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
890 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
891 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
892 UNEXPECTED_IO_APIC();
894 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
895 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
896 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
897 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
898 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
899 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
900 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
901 (reg_01.bits.entries != 0x2E) &&
902 (reg_01.bits.entries != 0x3F) &&
903 (reg_01.bits.entries != 0x03)
905 UNEXPECTED_IO_APIC();
907 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
908 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
909 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
910 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
911 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
912 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
913 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
914 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
916 UNEXPECTED_IO_APIC();
917 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
918 UNEXPECTED_IO_APIC();
920 if (reg_01.bits.version >= 0x10) {
921 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
922 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
923 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
924 UNEXPECTED_IO_APIC();
927 printk(KERN_DEBUG ".... IRQ redirection table:\n");
929 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
930 " Stat Dest Deli Vect: \n");
932 for (i = 0; i <= reg_01.bits.entries; i++) {
933 struct IO_APIC_route_entry entry;
935 spin_lock_irqsave(&ioapic_lock, flags);
936 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
937 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
938 spin_unlock_irqrestore(&ioapic_lock, flags);
940 printk(KERN_DEBUG " %02x %03X %02X ",
942 entry.dest.logical.logical_dest,
943 entry.dest.physical.physical_dest
946 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
947 entry.mask,
948 entry.trigger,
949 entry.irr,
950 entry.polarity,
951 entry.delivery_status,
952 entry.dest_mode,
953 entry.delivery_mode,
954 entry.vector
958 if (use_pci_vector())
959 printk(KERN_INFO "Using vector-based indexing\n");
960 printk(KERN_DEBUG "IRQ to pin mappings:\n");
961 for (i = 0; i < NR_IRQS; i++) {
962 struct irq_pin_list *entry = irq_2_pin + i;
963 if (entry->pin < 0)
964 continue;
965 if (use_pci_vector() && !platform_legacy_irq(i))
966 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
967 else
968 printk(KERN_DEBUG "IRQ%d ", i);
969 for (;;) {
970 printk("-> %d:%d", entry->apic, entry->pin);
971 if (!entry->next)
972 break;
973 entry = irq_2_pin + entry->next;
975 printk("\n");
978 printk(KERN_INFO ".................................... done.\n");
980 return;
983 #if 0
985 static __apicdebuginit void print_APIC_bitfield (int base)
987 unsigned int v;
988 int i, j;
990 if (apic_verbosity == APIC_QUIET)
991 return;
993 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
994 for (i = 0; i < 8; i++) {
995 v = apic_read(base + i*0x10);
996 for (j = 0; j < 32; j++) {
997 if (v & (1<<j))
998 printk("1");
999 else
1000 printk("0");
1002 printk("\n");
1006 void __apicdebuginit print_local_APIC(void * dummy)
1008 unsigned int v, ver, maxlvt;
1010 if (apic_verbosity == APIC_QUIET)
1011 return;
1013 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1014 smp_processor_id(), hard_smp_processor_id());
1015 v = apic_read(APIC_ID);
1016 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1017 v = apic_read(APIC_LVR);
1018 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1019 ver = GET_APIC_VERSION(v);
1020 maxlvt = get_maxlvt();
1022 v = apic_read(APIC_TASKPRI);
1023 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1025 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1026 v = apic_read(APIC_ARBPRI);
1027 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1028 v & APIC_ARBPRI_MASK);
1029 v = apic_read(APIC_PROCPRI);
1030 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1033 v = apic_read(APIC_EOI);
1034 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1035 v = apic_read(APIC_RRR);
1036 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1037 v = apic_read(APIC_LDR);
1038 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1039 v = apic_read(APIC_DFR);
1040 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1041 v = apic_read(APIC_SPIV);
1042 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1044 printk(KERN_DEBUG "... APIC ISR field:\n");
1045 print_APIC_bitfield(APIC_ISR);
1046 printk(KERN_DEBUG "... APIC TMR field:\n");
1047 print_APIC_bitfield(APIC_TMR);
1048 printk(KERN_DEBUG "... APIC IRR field:\n");
1049 print_APIC_bitfield(APIC_IRR);
1051 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1052 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1053 apic_write(APIC_ESR, 0);
1054 v = apic_read(APIC_ESR);
1055 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1058 v = apic_read(APIC_ICR);
1059 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1060 v = apic_read(APIC_ICR2);
1061 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1063 v = apic_read(APIC_LVTT);
1064 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1066 if (maxlvt > 3) { /* PC is LVT#4. */
1067 v = apic_read(APIC_LVTPC);
1068 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1070 v = apic_read(APIC_LVT0);
1071 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1072 v = apic_read(APIC_LVT1);
1073 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1075 if (maxlvt > 2) { /* ERR is LVT#3. */
1076 v = apic_read(APIC_LVTERR);
1077 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1080 v = apic_read(APIC_TMICT);
1081 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1082 v = apic_read(APIC_TMCCT);
1083 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1084 v = apic_read(APIC_TDCR);
1085 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1086 printk("\n");
1089 void print_all_local_APICs (void)
1091 on_each_cpu(print_local_APIC, NULL, 1, 1);
1094 void __apicdebuginit print_PIC(void)
1096 unsigned int v;
1097 unsigned long flags;
1099 if (apic_verbosity == APIC_QUIET)
1100 return;
1102 printk(KERN_DEBUG "\nprinting PIC contents\n");
1104 spin_lock_irqsave(&i8259A_lock, flags);
1106 v = inb(0xa1) << 8 | inb(0x21);
1107 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1109 v = inb(0xa0) << 8 | inb(0x20);
1110 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1112 outb(0x0b,0xa0);
1113 outb(0x0b,0x20);
1114 v = inb(0xa0) << 8 | inb(0x20);
1115 outb(0x0a,0xa0);
1116 outb(0x0a,0x20);
1118 spin_unlock_irqrestore(&i8259A_lock, flags);
1120 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1122 v = inb(0x4d1) << 8 | inb(0x4d0);
1123 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1126 #endif /* 0 */
1128 static void __init enable_IO_APIC(void)
1130 union IO_APIC_reg_01 reg_01;
1131 int i;
1132 unsigned long flags;
1134 for (i = 0; i < PIN_MAP_SIZE; i++) {
1135 irq_2_pin[i].pin = -1;
1136 irq_2_pin[i].next = 0;
1138 if (!pirqs_enabled)
1139 for (i = 0; i < MAX_PIRQS; i++)
1140 pirq_entries[i] = -1;
1143 * The number of IO-APIC IRQ registers (== #pins):
1145 for (i = 0; i < nr_ioapics; i++) {
1146 spin_lock_irqsave(&ioapic_lock, flags);
1147 reg_01.raw = io_apic_read(i, 1);
1148 spin_unlock_irqrestore(&ioapic_lock, flags);
1149 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1153 * Do not trust the IO-APIC being empty at bootup
1155 clear_IO_APIC();
1159 * Not an __init, needed by the reboot code
1161 void disable_IO_APIC(void)
1163 int pin;
1165 * Clear the IO-APIC before rebooting:
1167 clear_IO_APIC();
1170 * If the i8259 is routed through an IOAPIC
1171 * Put that IOAPIC in virtual wire mode
1172 * so legacy interrupts can be delivered.
1174 pin = find_isa_irq_pin(0, mp_ExtINT);
1175 if (pin != -1) {
1176 struct IO_APIC_route_entry entry;
1177 unsigned long flags;
1179 memset(&entry, 0, sizeof(entry));
1180 entry.mask = 0; /* Enabled */
1181 entry.trigger = 0; /* Edge */
1182 entry.irr = 0;
1183 entry.polarity = 0; /* High */
1184 entry.delivery_status = 0;
1185 entry.dest_mode = 0; /* Physical */
1186 entry.delivery_mode = 7; /* ExtInt */
1187 entry.vector = 0;
1188 entry.dest.physical.physical_dest = 0;
1192 * Add it to the IO-APIC irq-routing table:
1194 spin_lock_irqsave(&ioapic_lock, flags);
1195 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
1196 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
1197 spin_unlock_irqrestore(&ioapic_lock, flags);
1200 disconnect_bsp_APIC(pin != -1);
1204 * function to set the IO-APIC physical IDs based on the
1205 * values stored in the MPC table.
1207 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1210 static void __init setup_ioapic_ids_from_mpc (void)
1212 union IO_APIC_reg_00 reg_00;
1213 int apic;
1214 int i;
1215 unsigned char old_id;
1216 unsigned long flags;
1219 * Set the IOAPIC ID to the value stored in the MPC table.
1221 for (apic = 0; apic < nr_ioapics; apic++) {
1223 /* Read the register 0 value */
1224 spin_lock_irqsave(&ioapic_lock, flags);
1225 reg_00.raw = io_apic_read(apic, 0);
1226 spin_unlock_irqrestore(&ioapic_lock, flags);
1228 old_id = mp_ioapics[apic].mpc_apicid;
1231 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1235 * We need to adjust the IRQ routing table
1236 * if the ID changed.
1238 if (old_id != mp_ioapics[apic].mpc_apicid)
1239 for (i = 0; i < mp_irq_entries; i++)
1240 if (mp_irqs[i].mpc_dstapic == old_id)
1241 mp_irqs[i].mpc_dstapic
1242 = mp_ioapics[apic].mpc_apicid;
1245 * Read the right value from the MPC table and
1246 * write it into the ID register.
1248 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1249 mp_ioapics[apic].mpc_apicid);
1251 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1252 spin_lock_irqsave(&ioapic_lock, flags);
1253 io_apic_write(apic, 0, reg_00.raw);
1254 spin_unlock_irqrestore(&ioapic_lock, flags);
1257 * Sanity check
1259 spin_lock_irqsave(&ioapic_lock, flags);
1260 reg_00.raw = io_apic_read(apic, 0);
1261 spin_unlock_irqrestore(&ioapic_lock, flags);
1262 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1263 printk("could not set ID!\n");
1264 else
1265 apic_printk(APIC_VERBOSE," ok.\n");
1270 * There is a nasty bug in some older SMP boards, their mptable lies
1271 * about the timer IRQ. We do the following to work around the situation:
1273 * - timer IRQ defaults to IO-APIC IRQ
1274 * - if this function detects that timer IRQs are defunct, then we fall
1275 * back to ISA timer IRQs
1277 static int __init timer_irq_works(void)
1279 unsigned long t1 = jiffies;
1281 local_irq_enable();
1282 /* Let ten ticks pass... */
1283 mdelay((10 * 1000) / HZ);
1286 * Expect a few ticks at least, to be sure some possible
1287 * glue logic does not lock up after one or two first
1288 * ticks in a non-ExtINT mode. Also the local APIC
1289 * might have cached one ExtINT interrupt. Finally, at
1290 * least one tick may be lost due to delays.
1293 /* jiffies wrap? */
1294 if (jiffies - t1 > 4)
1295 return 1;
1296 return 0;
1300 * In the SMP+IOAPIC case it might happen that there are an unspecified
1301 * number of pending IRQ events unhandled. These cases are very rare,
1302 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1303 * better to do it this way as thus we do not have to be aware of
1304 * 'pending' interrupts in the IRQ path, except at this point.
1307 * Edge triggered needs to resend any interrupt
1308 * that was delayed but this is now handled in the device
1309 * independent code.
1313 * Starting up a edge-triggered IO-APIC interrupt is
1314 * nasty - we need to make sure that we get the edge.
1315 * If it is already asserted for some reason, we need
1316 * return 1 to indicate that is was pending.
1318 * This is not complete - we should be able to fake
1319 * an edge even if it isn't on the 8259A...
1322 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1324 int was_pending = 0;
1325 unsigned long flags;
1327 spin_lock_irqsave(&ioapic_lock, flags);
1328 if (irq < 16) {
1329 disable_8259A_irq(irq);
1330 if (i8259A_irq_pending(irq))
1331 was_pending = 1;
1333 __unmask_IO_APIC_irq(irq);
1334 spin_unlock_irqrestore(&ioapic_lock, flags);
1336 return was_pending;
1340 * Once we have recorded IRQ_PENDING already, we can mask the
1341 * interrupt for real. This prevents IRQ storms from unhandled
1342 * devices.
1344 static void ack_edge_ioapic_irq(unsigned int irq)
1346 move_irq(irq);
1347 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1348 == (IRQ_PENDING | IRQ_DISABLED))
1349 mask_IO_APIC_irq(irq);
1350 ack_APIC_irq();
1354 * Level triggered interrupts can just be masked,
1355 * and shutting down and starting up the interrupt
1356 * is the same as enabling and disabling them -- except
1357 * with a startup need to return a "was pending" value.
1359 * Level triggered interrupts are special because we
1360 * do not touch any IO-APIC register while handling
1361 * them. We ack the APIC in the end-IRQ handler, not
1362 * in the start-IRQ-handler. Protection against reentrance
1363 * from the same interrupt is still provided, both by the
1364 * generic IRQ layer and by the fact that an unacked local
1365 * APIC does not accept IRQs.
1367 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1369 unmask_IO_APIC_irq(irq);
1371 return 0; /* don't check for pending */
1374 static void end_level_ioapic_irq (unsigned int irq)
1376 move_irq(irq);
1377 ack_APIC_irq();
1380 #ifdef CONFIG_PCI_MSI
1381 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1383 int irq = vector_to_irq(vector);
1385 return startup_edge_ioapic_irq(irq);
1388 static void ack_edge_ioapic_vector(unsigned int vector)
1390 int irq = vector_to_irq(vector);
1392 move_native_irq(vector);
1393 ack_edge_ioapic_irq(irq);
1396 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1398 int irq = vector_to_irq(vector);
1400 return startup_level_ioapic_irq (irq);
1403 static void end_level_ioapic_vector (unsigned int vector)
1405 int irq = vector_to_irq(vector);
1407 move_native_irq(vector);
1408 end_level_ioapic_irq(irq);
1411 static void mask_IO_APIC_vector (unsigned int vector)
1413 int irq = vector_to_irq(vector);
1415 mask_IO_APIC_irq(irq);
1418 static void unmask_IO_APIC_vector (unsigned int vector)
1420 int irq = vector_to_irq(vector);
1422 unmask_IO_APIC_irq(irq);
1425 #ifdef CONFIG_SMP
1426 static void set_ioapic_affinity_vector (unsigned int vector,
1427 cpumask_t cpu_mask)
1429 int irq = vector_to_irq(vector);
1431 set_native_irq_info(vector, cpu_mask);
1432 set_ioapic_affinity_irq(irq, cpu_mask);
1434 #endif // CONFIG_SMP
1435 #endif // CONFIG_PCI_MSI
1438 * Level and edge triggered IO-APIC interrupts need different handling,
1439 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1440 * handled with the level-triggered descriptor, but that one has slightly
1441 * more overhead. Level-triggered interrupts cannot be handled with the
1442 * edge-triggered handler, without risking IRQ storms and other ugly
1443 * races.
1446 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1447 .typename = "IO-APIC-edge",
1448 .startup = startup_edge_ioapic,
1449 .shutdown = shutdown_edge_ioapic,
1450 .enable = enable_edge_ioapic,
1451 .disable = disable_edge_ioapic,
1452 .ack = ack_edge_ioapic,
1453 .end = end_edge_ioapic,
1454 #ifdef CONFIG_SMP
1455 .set_affinity = set_ioapic_affinity,
1456 #endif
1459 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1460 .typename = "IO-APIC-level",
1461 .startup = startup_level_ioapic,
1462 .shutdown = shutdown_level_ioapic,
1463 .enable = enable_level_ioapic,
1464 .disable = disable_level_ioapic,
1465 .ack = mask_and_ack_level_ioapic,
1466 .end = end_level_ioapic,
1467 #ifdef CONFIG_SMP
1468 .set_affinity = set_ioapic_affinity,
1469 #endif
1472 static inline void init_IO_APIC_traps(void)
1474 int irq;
1477 * NOTE! The local APIC isn't very good at handling
1478 * multiple interrupts at the same interrupt level.
1479 * As the interrupt level is determined by taking the
1480 * vector number and shifting that right by 4, we
1481 * want to spread these out a bit so that they don't
1482 * all fall in the same interrupt level.
1484 * Also, we've got to be careful not to trash gate
1485 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1487 for (irq = 0; irq < NR_IRQS ; irq++) {
1488 int tmp = irq;
1489 if (use_pci_vector()) {
1490 if (!platform_legacy_irq(tmp))
1491 if ((tmp = vector_to_irq(tmp)) == -1)
1492 continue;
1494 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1496 * Hmm.. We don't have an entry for this,
1497 * so default to an old-fashioned 8259
1498 * interrupt if we can..
1500 if (irq < 16)
1501 make_8259A_irq(irq);
1502 else
1503 /* Strange. Oh, well.. */
1504 irq_desc[irq].handler = &no_irq_type;
1509 static void enable_lapic_irq (unsigned int irq)
1511 unsigned long v;
1513 v = apic_read(APIC_LVT0);
1514 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1517 static void disable_lapic_irq (unsigned int irq)
1519 unsigned long v;
1521 v = apic_read(APIC_LVT0);
1522 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1525 static void ack_lapic_irq (unsigned int irq)
1527 ack_APIC_irq();
1530 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1532 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1533 .typename = "local-APIC-edge",
1534 .startup = NULL, /* startup_irq() not used for IRQ0 */
1535 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1536 .enable = enable_lapic_irq,
1537 .disable = disable_lapic_irq,
1538 .ack = ack_lapic_irq,
1539 .end = end_lapic_irq,
1542 static void setup_nmi (void)
1545 * Dirty trick to enable the NMI watchdog ...
1546 * We put the 8259A master into AEOI mode and
1547 * unmask on all local APICs LVT0 as NMI.
1549 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1550 * is from Maciej W. Rozycki - so we do not have to EOI from
1551 * the NMI handler or the timer interrupt.
1553 printk(KERN_INFO "activating NMI Watchdog ...");
1555 enable_NMI_through_LVT0(NULL);
1557 printk(" done.\n");
1561 * This looks a bit hackish but it's about the only one way of sending
1562 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1563 * not support the ExtINT mode, unfortunately. We need to send these
1564 * cycles as some i82489DX-based boards have glue logic that keeps the
1565 * 8259A interrupt line asserted until INTA. --macro
1567 static inline void unlock_ExtINT_logic(void)
1569 int pin, i;
1570 struct IO_APIC_route_entry entry0, entry1;
1571 unsigned char save_control, save_freq_select;
1572 unsigned long flags;
1574 pin = find_isa_irq_pin(8, mp_INT);
1575 if (pin == -1)
1576 return;
1578 spin_lock_irqsave(&ioapic_lock, flags);
1579 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1580 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1581 spin_unlock_irqrestore(&ioapic_lock, flags);
1582 clear_IO_APIC_pin(0, pin);
1584 memset(&entry1, 0, sizeof(entry1));
1586 entry1.dest_mode = 0; /* physical delivery */
1587 entry1.mask = 0; /* unmask IRQ now */
1588 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1589 entry1.delivery_mode = dest_ExtINT;
1590 entry1.polarity = entry0.polarity;
1591 entry1.trigger = 0;
1592 entry1.vector = 0;
1594 spin_lock_irqsave(&ioapic_lock, flags);
1595 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1596 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1597 spin_unlock_irqrestore(&ioapic_lock, flags);
1599 save_control = CMOS_READ(RTC_CONTROL);
1600 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1601 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1602 RTC_FREQ_SELECT);
1603 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1605 i = 100;
1606 while (i-- > 0) {
1607 mdelay(10);
1608 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1609 i -= 10;
1612 CMOS_WRITE(save_control, RTC_CONTROL);
1613 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1614 clear_IO_APIC_pin(0, pin);
1616 spin_lock_irqsave(&ioapic_lock, flags);
1617 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1618 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1619 spin_unlock_irqrestore(&ioapic_lock, flags);
1623 * This code may look a bit paranoid, but it's supposed to cooperate with
1624 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1625 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1626 * fanatically on his truly buggy board.
1628 static inline void check_timer(void)
1630 int pin1, pin2;
1631 int vector;
1634 * get/set the timer IRQ vector:
1636 disable_8259A_irq(0);
1637 vector = assign_irq_vector(0);
1638 set_intr_gate(vector, interrupt[0]);
1641 * Subtle, code in do_timer_interrupt() expects an AEOI
1642 * mode for the 8259A whenever interrupts are routed
1643 * through I/O APICs. Also IRQ0 has to be enabled in
1644 * the 8259A which implies the virtual wire has to be
1645 * disabled in the local APIC.
1647 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1648 init_8259A(1);
1649 enable_8259A_irq(0);
1651 pin1 = find_isa_irq_pin(0, mp_INT);
1652 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1654 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1656 if (pin1 != -1) {
1658 * Ok, does IRQ0 through the IOAPIC work?
1660 unmask_IO_APIC_irq(0);
1661 if (!no_timer_check && timer_irq_works()) {
1662 nmi_watchdog_default();
1663 if (nmi_watchdog == NMI_IO_APIC) {
1664 disable_8259A_irq(0);
1665 setup_nmi();
1666 enable_8259A_irq(0);
1668 return;
1670 clear_IO_APIC_pin(0, pin1);
1671 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1674 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1675 if (pin2 != -1) {
1676 apic_printk(APIC_VERBOSE,"\n..... (found pin %d) ...", pin2);
1678 * legacy devices should be connected to IO APIC #0
1680 setup_ExtINT_IRQ0_pin(pin2, vector);
1681 if (timer_irq_works()) {
1682 printk("works.\n");
1683 nmi_watchdog_default();
1684 if (nmi_watchdog == NMI_IO_APIC) {
1685 setup_nmi();
1687 return;
1690 * Cleanup, just in case ...
1692 clear_IO_APIC_pin(0, pin2);
1694 printk(" failed.\n");
1696 if (nmi_watchdog) {
1697 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1698 nmi_watchdog = 0;
1701 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1703 disable_8259A_irq(0);
1704 irq_desc[0].handler = &lapic_irq_type;
1705 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1706 enable_8259A_irq(0);
1708 if (timer_irq_works()) {
1709 apic_printk(APIC_QUIET, " works.\n");
1710 return;
1712 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1713 apic_printk(APIC_VERBOSE," failed.\n");
1715 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1717 init_8259A(0);
1718 make_8259A_irq(0);
1719 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1721 unlock_ExtINT_logic();
1723 if (timer_irq_works()) {
1724 apic_printk(APIC_VERBOSE," works.\n");
1725 return;
1727 apic_printk(APIC_VERBOSE," failed :(.\n");
1728 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1731 static int __init notimercheck(char *s)
1733 no_timer_check = 1;
1734 return 1;
1736 __setup("no_timer_check", notimercheck);
1740 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1741 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1742 * Linux doesn't really care, as it's not actually used
1743 * for any interrupt handling anyway.
1745 #define PIC_IRQS (1<<2)
1747 void __init setup_IO_APIC(void)
1749 enable_IO_APIC();
1751 if (acpi_ioapic)
1752 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1753 else
1754 io_apic_irqs = ~PIC_IRQS;
1756 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1759 * Set up the IO-APIC IRQ routing table.
1761 if (!acpi_ioapic)
1762 setup_ioapic_ids_from_mpc();
1763 sync_Arb_IDs();
1764 setup_IO_APIC_irqs();
1765 init_IO_APIC_traps();
1766 check_timer();
1767 if (!acpi_ioapic)
1768 print_IO_APIC();
1771 struct sysfs_ioapic_data {
1772 struct sys_device dev;
1773 struct IO_APIC_route_entry entry[0];
1775 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1777 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1779 struct IO_APIC_route_entry *entry;
1780 struct sysfs_ioapic_data *data;
1781 unsigned long flags;
1782 int i;
1784 data = container_of(dev, struct sysfs_ioapic_data, dev);
1785 entry = data->entry;
1786 spin_lock_irqsave(&ioapic_lock, flags);
1787 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1788 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1789 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1791 spin_unlock_irqrestore(&ioapic_lock, flags);
1793 return 0;
1796 static int ioapic_resume(struct sys_device *dev)
1798 struct IO_APIC_route_entry *entry;
1799 struct sysfs_ioapic_data *data;
1800 unsigned long flags;
1801 union IO_APIC_reg_00 reg_00;
1802 int i;
1804 data = container_of(dev, struct sysfs_ioapic_data, dev);
1805 entry = data->entry;
1807 spin_lock_irqsave(&ioapic_lock, flags);
1808 reg_00.raw = io_apic_read(dev->id, 0);
1809 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1810 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1811 io_apic_write(dev->id, 0, reg_00.raw);
1813 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1814 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1815 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1817 spin_unlock_irqrestore(&ioapic_lock, flags);
1819 return 0;
1822 static struct sysdev_class ioapic_sysdev_class = {
1823 set_kset_name("ioapic"),
1824 .suspend = ioapic_suspend,
1825 .resume = ioapic_resume,
1828 static int __init ioapic_init_sysfs(void)
1830 struct sys_device * dev;
1831 int i, size, error = 0;
1833 error = sysdev_class_register(&ioapic_sysdev_class);
1834 if (error)
1835 return error;
1837 for (i = 0; i < nr_ioapics; i++ ) {
1838 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1839 * sizeof(struct IO_APIC_route_entry);
1840 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1841 if (!mp_ioapic_data[i]) {
1842 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1843 continue;
1845 memset(mp_ioapic_data[i], 0, size);
1846 dev = &mp_ioapic_data[i]->dev;
1847 dev->id = i;
1848 dev->cls = &ioapic_sysdev_class;
1849 error = sysdev_register(dev);
1850 if (error) {
1851 kfree(mp_ioapic_data[i]);
1852 mp_ioapic_data[i] = NULL;
1853 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1854 continue;
1858 return 0;
1861 device_initcall(ioapic_init_sysfs);
1863 /* --------------------------------------------------------------------------
1864 ACPI-based IOAPIC Configuration
1865 -------------------------------------------------------------------------- */
1867 #ifdef CONFIG_ACPI
1869 #define IO_APIC_MAX_ID 0xFE
1871 int __init io_apic_get_version (int ioapic)
1873 union IO_APIC_reg_01 reg_01;
1874 unsigned long flags;
1876 spin_lock_irqsave(&ioapic_lock, flags);
1877 reg_01.raw = io_apic_read(ioapic, 1);
1878 spin_unlock_irqrestore(&ioapic_lock, flags);
1880 return reg_01.bits.version;
1884 int __init io_apic_get_redir_entries (int ioapic)
1886 union IO_APIC_reg_01 reg_01;
1887 unsigned long flags;
1889 spin_lock_irqsave(&ioapic_lock, flags);
1890 reg_01.raw = io_apic_read(ioapic, 1);
1891 spin_unlock_irqrestore(&ioapic_lock, flags);
1893 return reg_01.bits.entries;
1897 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
1899 struct IO_APIC_route_entry entry;
1900 unsigned long flags;
1902 if (!IO_APIC_IRQ(irq)) {
1903 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1904 ioapic);
1905 return -EINVAL;
1909 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1910 * Note that we mask (disable) IRQs now -- these get enabled when the
1911 * corresponding device driver registers for this IRQ.
1914 memset(&entry,0,sizeof(entry));
1916 entry.delivery_mode = INT_DELIVERY_MODE;
1917 entry.dest_mode = INT_DEST_MODE;
1918 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1919 entry.trigger = edge_level;
1920 entry.polarity = active_high_low;
1921 entry.mask = 1; /* Disabled (masked) */
1924 * IRQs < 16 are already in the irq_2_pin[] map
1926 if (irq >= 16)
1927 add_pin_to_irq(irq, ioapic, pin);
1929 entry.vector = assign_irq_vector(irq);
1931 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1932 "IRQ %d Mode:%i Active:%i)\n", ioapic,
1933 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
1934 edge_level, active_high_low);
1936 ioapic_register_intr(irq, entry.vector, edge_level);
1938 if (!ioapic && (irq < 16))
1939 disable_8259A_irq(irq);
1941 spin_lock_irqsave(&ioapic_lock, flags);
1942 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
1943 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
1944 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
1945 spin_unlock_irqrestore(&ioapic_lock, flags);
1947 return 0;
1950 #endif /* CONFIG_ACPI */
1954 * This function currently is only a helper for the i386 smp boot process where
1955 * we need to reprogram the ioredtbls to cater for the cpus which have come online
1956 * so mask in all cases should simply be TARGET_CPUS
1958 #ifdef CONFIG_SMP
1959 void __init setup_ioapic_dest(void)
1961 int pin, ioapic, irq, irq_entry;
1963 if (skip_ioapic_setup == 1)
1964 return;
1966 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
1967 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
1968 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
1969 if (irq_entry == -1)
1970 continue;
1971 irq = pin_2_irq(irq_entry, ioapic, pin);
1972 set_ioapic_affinity_irq(irq, TARGET_CPUS);
1977 #endif