2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/config.h>
29 #include <linux/smp_lock.h>
30 #include <linux/mc146818rtc.h>
31 #include <linux/acpi.h>
32 #include <linux/sysdev.h>
37 #include <asm/proto.h>
38 #include <asm/mach_apic.h>
41 #define __apicdebuginit __init
43 int sis_apic_bug; /* not actually supported, dummy for compile */
45 static int no_timer_check;
47 int disable_timer_pin_1 __initdata;
49 static DEFINE_SPINLOCK(ioapic_lock);
52 * # of IRQ routing registers
54 int nr_ioapic_registers[MAX_IO_APICS];
57 * Rough estimation of how many shared IRQs there are, can
60 #define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
61 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
64 * This is performance-critical, we want to do it O(1)
66 * the indexing order of this array favors 1:1 mappings
67 * between pins and IRQs.
70 static struct irq_pin_list {
71 short apic, pin, next;
72 } irq_2_pin[PIN_MAP_SIZE];
74 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
76 #define vector_to_irq(vector) \
77 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
79 #define vector_to_irq(vector) (vector)
82 #define __DO_ACTION(R, ACTION, FINAL) \
86 struct irq_pin_list *entry = irq_2_pin + irq; \
88 BUG_ON(irq >= NR_IRQS); \
94 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
96 io_apic_modify(entry->apic, reg); \
99 entry = irq_2_pin + entry->next; \
105 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
111 cpus_and(tmp, mask, cpu_online_map);
115 cpus_and(mask, tmp, CPU_MASK_ALL);
117 dest = cpu_mask_to_apicid(mask);
120 * Only the high 8 bits are valid.
122 dest = SET_APIC_LOGICAL_ID(dest);
124 spin_lock_irqsave(&ioapic_lock, flags);
125 __DO_ACTION(1, = dest, )
126 set_irq_info(irq, mask);
127 spin_unlock_irqrestore(&ioapic_lock, flags);
131 static u8 gsi_2_irq[NR_IRQ_VECTORS] = { [0 ... NR_IRQ_VECTORS-1] = 0xFF };
134 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
135 * shared ISA-space IRQs, so we have to support them. We are super
136 * fast in the common case, and fast for shared ISA-space IRQs.
138 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
140 static int first_free_entry = NR_IRQS;
141 struct irq_pin_list *entry = irq_2_pin + irq;
143 BUG_ON(irq >= NR_IRQS);
145 entry = irq_2_pin + entry->next;
147 if (entry->pin != -1) {
148 entry->next = first_free_entry;
149 entry = irq_2_pin + entry->next;
150 if (++first_free_entry >= PIN_MAP_SIZE)
151 panic("io_apic.c: ran out of irq_2_pin entries!");
158 #define DO_ACTION(name,R,ACTION, FINAL) \
160 static void name##_IO_APIC_irq (unsigned int irq) \
161 __DO_ACTION(R, ACTION, FINAL)
163 DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
165 DO_ACTION( __unmask, 0, &= 0xfffeffff, )
168 static void mask_IO_APIC_irq (unsigned int irq)
172 spin_lock_irqsave(&ioapic_lock, flags);
173 __mask_IO_APIC_irq(irq);
174 spin_unlock_irqrestore(&ioapic_lock, flags);
177 static void unmask_IO_APIC_irq (unsigned int irq)
181 spin_lock_irqsave(&ioapic_lock, flags);
182 __unmask_IO_APIC_irq(irq);
183 spin_unlock_irqrestore(&ioapic_lock, flags);
186 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
188 struct IO_APIC_route_entry entry;
191 /* Check delivery_mode to be sure we're not clearing an SMI pin */
192 spin_lock_irqsave(&ioapic_lock, flags);
193 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
194 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
195 spin_unlock_irqrestore(&ioapic_lock, flags);
196 if (entry.delivery_mode == dest_SMI)
199 * Disable it in the IO-APIC irq-routing table:
201 memset(&entry, 0, sizeof(entry));
203 spin_lock_irqsave(&ioapic_lock, flags);
204 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
205 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
206 spin_unlock_irqrestore(&ioapic_lock, flags);
209 static void clear_IO_APIC (void)
213 for (apic = 0; apic < nr_ioapics; apic++)
214 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
215 clear_IO_APIC_pin(apic, pin);
219 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
220 * specific CPU-side IRQs.
224 static int pirq_entries [MAX_PIRQS];
225 static int pirqs_enabled;
226 int skip_ioapic_setup;
229 /* dummy parsing: see setup.c */
231 static int __init disable_ioapic_setup(char *str)
233 skip_ioapic_setup = 1;
237 static int __init enable_ioapic_setup(char *str)
240 skip_ioapic_setup = 0;
244 __setup("noapic", disable_ioapic_setup);
245 __setup("apic", enable_ioapic_setup);
247 #include <asm/pci-direct.h>
248 #include <linux/pci_ids.h>
249 #include <linux/pci.h>
251 /* Temporary Hack. Nvidia and VIA boards currently only work with IO-APIC
252 off. Check for an Nvidia or VIA PCI bridge and turn it off.
253 Use pci direct infrastructure because this runs before the PCI subsystem.
255 Can be overwritten with "apic"
257 And another hack to disable the IOMMU on VIA chipsets.
260 void __init check_ioapic(void)
266 /* Poor man's PCI discovery */
267 for (num = 0; num < 32; num++) {
268 for (slot = 0; slot < 32; slot++) {
269 for (func = 0; func < 8; func++) {
273 class = read_pci_config(num,slot,func,
275 if (class == 0xffffffff)
278 if ((class >> 16) != PCI_CLASS_BRIDGE_PCI)
281 vendor = read_pci_config(num, slot, func,
285 case PCI_VENDOR_ID_VIA:
286 #ifdef CONFIG_GART_IOMMU
287 if ((end_pfn >= (0xffffffff>>PAGE_SHIFT) ||
289 !iommu_aperture_allowed) {
291 "Looks like a VIA chipset. Disabling IOMMU. Overwrite with \"iommu=allowed\"\n");
292 iommu_aperture_disabled = 1;
296 case PCI_VENDOR_ID_NVIDIA:
298 /* All timer overrides on Nvidia
299 seem to be wrong. Skip them. */
300 acpi_skip_timer_override = 1;
302 "Nvidia board detected. Ignoring ACPI timer override.\n");
304 /* RED-PEN skip them on mptables too? */
308 /* No multi-function device? */
309 type = read_pci_config_byte(num,slot,func,
318 static int __init ioapic_pirq_setup(char *str)
321 int ints[MAX_PIRQS+1];
323 get_options(str, ARRAY_SIZE(ints), ints);
325 for (i = 0; i < MAX_PIRQS; i++)
326 pirq_entries[i] = -1;
329 apic_printk(APIC_VERBOSE, "PIRQ redirection, working around broken MP-BIOS.\n");
331 if (ints[0] < MAX_PIRQS)
334 for (i = 0; i < max; i++) {
335 apic_printk(APIC_VERBOSE, "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
337 * PIRQs are mapped upside down, usually.
339 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
344 __setup("pirq=", ioapic_pirq_setup);
347 * Find the IRQ entry number of a certain pin.
349 static int find_irq_entry(int apic, int pin, int type)
353 for (i = 0; i < mp_irq_entries; i++)
354 if (mp_irqs[i].mpc_irqtype == type &&
355 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
356 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
357 mp_irqs[i].mpc_dstirq == pin)
364 * Find the pin to which IRQ[irq] (ISA) is connected
366 static int find_isa_irq_pin(int irq, int type)
370 for (i = 0; i < mp_irq_entries; i++) {
371 int lbus = mp_irqs[i].mpc_srcbus;
373 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
374 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
375 mp_bus_id_to_type[lbus] == MP_BUS_MCA) &&
376 (mp_irqs[i].mpc_irqtype == type) &&
377 (mp_irqs[i].mpc_srcbusirq == irq))
379 return mp_irqs[i].mpc_dstirq;
385 * Find a specific PCI IRQ entry.
386 * Not an __init, possibly needed by modules
388 static int pin_2_irq(int idx, int apic, int pin);
390 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
392 int apic, i, best_guess = -1;
394 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
396 if (mp_bus_id_to_pci_bus[bus] == -1) {
397 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
400 for (i = 0; i < mp_irq_entries; i++) {
401 int lbus = mp_irqs[i].mpc_srcbus;
403 for (apic = 0; apic < nr_ioapics; apic++)
404 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
405 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
408 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
409 !mp_irqs[i].mpc_irqtype &&
411 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
412 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
414 if (!(apic || IO_APIC_IRQ(irq)))
417 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
420 * Use the first all-but-pin matching entry as a
421 * best-guess fuzzy result for broken mptables.
427 BUG_ON(best_guess >= NR_IRQS);
432 * EISA Edge/Level control register, ELCR
434 static int EISA_ELCR(unsigned int irq)
437 unsigned int port = 0x4d0 + (irq >> 3);
438 return (inb(port) >> (irq & 7)) & 1;
440 apic_printk(APIC_VERBOSE, "Broken MPtable reports ISA irq %d\n", irq);
444 /* EISA interrupts are always polarity zero and can be edge or level
445 * trigger depending on the ELCR value. If an interrupt is listed as
446 * EISA conforming in the MP table, that means its trigger type must
447 * be read in from the ELCR */
449 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
450 #define default_EISA_polarity(idx) (0)
452 /* ISA interrupts are always polarity zero edge triggered,
453 * when listed as conforming in the MP table. */
455 #define default_ISA_trigger(idx) (0)
456 #define default_ISA_polarity(idx) (0)
458 /* PCI interrupts are always polarity one level triggered,
459 * when listed as conforming in the MP table. */
461 #define default_PCI_trigger(idx) (1)
462 #define default_PCI_polarity(idx) (1)
464 /* MCA interrupts are always polarity zero level triggered,
465 * when listed as conforming in the MP table. */
467 #define default_MCA_trigger(idx) (1)
468 #define default_MCA_polarity(idx) (0)
470 static int __init MPBIOS_polarity(int idx)
472 int bus = mp_irqs[idx].mpc_srcbus;
476 * Determine IRQ line polarity (high active or low active):
478 switch (mp_irqs[idx].mpc_irqflag & 3)
480 case 0: /* conforms, ie. bus-type dependent polarity */
482 switch (mp_bus_id_to_type[bus])
484 case MP_BUS_ISA: /* ISA pin */
486 polarity = default_ISA_polarity(idx);
489 case MP_BUS_EISA: /* EISA pin */
491 polarity = default_EISA_polarity(idx);
494 case MP_BUS_PCI: /* PCI pin */
496 polarity = default_PCI_polarity(idx);
499 case MP_BUS_MCA: /* MCA pin */
501 polarity = default_MCA_polarity(idx);
506 printk(KERN_WARNING "broken BIOS!!\n");
513 case 1: /* high active */
518 case 2: /* reserved */
520 printk(KERN_WARNING "broken BIOS!!\n");
524 case 3: /* low active */
529 default: /* invalid */
531 printk(KERN_WARNING "broken BIOS!!\n");
539 static int MPBIOS_trigger(int idx)
541 int bus = mp_irqs[idx].mpc_srcbus;
545 * Determine IRQ trigger mode (edge or level sensitive):
547 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
549 case 0: /* conforms, ie. bus-type dependent */
551 switch (mp_bus_id_to_type[bus])
553 case MP_BUS_ISA: /* ISA pin */
555 trigger = default_ISA_trigger(idx);
558 case MP_BUS_EISA: /* EISA pin */
560 trigger = default_EISA_trigger(idx);
563 case MP_BUS_PCI: /* PCI pin */
565 trigger = default_PCI_trigger(idx);
568 case MP_BUS_MCA: /* MCA pin */
570 trigger = default_MCA_trigger(idx);
575 printk(KERN_WARNING "broken BIOS!!\n");
587 case 2: /* reserved */
589 printk(KERN_WARNING "broken BIOS!!\n");
598 default: /* invalid */
600 printk(KERN_WARNING "broken BIOS!!\n");
608 static inline int irq_polarity(int idx)
610 return MPBIOS_polarity(idx);
613 static inline int irq_trigger(int idx)
615 return MPBIOS_trigger(idx);
618 static int next_irq = 16;
621 * gsi_irq_sharing -- Name overload! "irq" can be either a legacy IRQ
622 * in the range 0-15, a linux IRQ in the range 0-223, or a GSI number
623 * from ACPI, which can reach 800 in large boxen.
625 * Compact the sparse GSI space into a sequential IRQ series and reuse
626 * vectors if possible.
628 int gsi_irq_sharing(int gsi)
630 int i, tries, vector;
632 BUG_ON(gsi >= NR_IRQ_VECTORS);
634 if (platform_legacy_irq(gsi))
637 if (gsi_2_irq[gsi] != 0xFF)
638 return (int)gsi_2_irq[gsi];
642 vector = assign_irq_vector(gsi);
645 * Sharing vectors means sharing IRQs, so scan irq_vectors for previous
646 * use of vector and if found, return that IRQ. However, we never want
647 * to share legacy IRQs, which usually have a different trigger mode
650 for (i = 0; i < NR_IRQS; i++)
651 if (IO_APIC_VECTOR(i) == vector)
653 if (platform_legacy_irq(i)) {
655 IO_APIC_VECTOR(i) = 0;
658 panic("gsi_irq_sharing: didn't find an IRQ using vector 0x%02X for GSI %d", vector, gsi);
662 printk(KERN_INFO "GSI %d sharing vector 0x%02X and IRQ %d\n",
668 BUG_ON(i >= NR_IRQS);
670 IO_APIC_VECTOR(i) = vector;
671 printk(KERN_INFO "GSI %d assigned vector 0x%02X and IRQ %d\n",
676 static int pin_2_irq(int idx, int apic, int pin)
679 int bus = mp_irqs[idx].mpc_srcbus;
682 * Debugging check, we are in big trouble if this message pops up!
684 if (mp_irqs[idx].mpc_dstirq != pin)
685 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
687 switch (mp_bus_id_to_type[bus])
689 case MP_BUS_ISA: /* ISA pin */
693 irq = mp_irqs[idx].mpc_srcbusirq;
696 case MP_BUS_PCI: /* PCI pin */
699 * PCI IRQs are mapped in order
703 irq += nr_ioapic_registers[i++];
705 irq = gsi_irq_sharing(irq);
710 printk(KERN_ERR "unknown bus type %d.\n",bus);
715 BUG_ON(irq >= NR_IRQS);
718 * PCI IRQ command line redirection. Yes, limits are hardcoded.
720 if ((pin >= 16) && (pin <= 23)) {
721 if (pirq_entries[pin-16] != -1) {
722 if (!pirq_entries[pin-16]) {
723 apic_printk(APIC_VERBOSE, "disabling PIRQ%d\n", pin-16);
725 irq = pirq_entries[pin-16];
726 apic_printk(APIC_VERBOSE, "using PIRQ%d -> IRQ %d\n",
731 BUG_ON(irq >= NR_IRQS);
735 static inline int IO_APIC_irq_trigger(int irq)
739 for (apic = 0; apic < nr_ioapics; apic++) {
740 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
741 idx = find_irq_entry(apic,pin,mp_INT);
742 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
743 return irq_trigger(idx);
747 * nonexistent IRQs are edge default
752 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
753 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
755 int assign_irq_vector(int irq)
757 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
759 BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
760 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0)
761 return IO_APIC_VECTOR(irq);
764 if (current_vector == IA32_SYSCALL_VECTOR)
767 if (current_vector >= FIRST_SYSTEM_VECTOR) {
768 /* If we run out of vectors on large boxen, must share them. */
769 offset = (offset + 1) % 8;
770 current_vector = FIRST_DEVICE_VECTOR + offset;
773 vector_irq[current_vector] = irq;
774 if (irq != AUTO_ASSIGN)
775 IO_APIC_VECTOR(irq) = current_vector;
777 return current_vector;
780 extern void (*interrupt[NR_IRQS])(void);
781 static struct hw_interrupt_type ioapic_level_type;
782 static struct hw_interrupt_type ioapic_edge_type;
784 #define IOAPIC_AUTO -1
785 #define IOAPIC_EDGE 0
786 #define IOAPIC_LEVEL 1
788 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
790 if (use_pci_vector() && !platform_legacy_irq(irq)) {
791 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
792 trigger == IOAPIC_LEVEL)
793 irq_desc[vector].handler = &ioapic_level_type;
795 irq_desc[vector].handler = &ioapic_edge_type;
796 set_intr_gate(vector, interrupt[vector]);
798 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
799 trigger == IOAPIC_LEVEL)
800 irq_desc[irq].handler = &ioapic_level_type;
802 irq_desc[irq].handler = &ioapic_edge_type;
803 set_intr_gate(vector, interrupt[irq]);
807 static void __init setup_IO_APIC_irqs(void)
809 struct IO_APIC_route_entry entry;
810 int apic, pin, idx, irq, first_notcon = 1, vector;
813 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
815 for (apic = 0; apic < nr_ioapics; apic++) {
816 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
819 * add it to the IO-APIC irq-routing table:
821 memset(&entry,0,sizeof(entry));
823 entry.delivery_mode = INT_DELIVERY_MODE;
824 entry.dest_mode = INT_DEST_MODE;
825 entry.mask = 0; /* enable IRQ */
826 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
828 idx = find_irq_entry(apic,pin,mp_INT);
831 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
834 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
838 entry.trigger = irq_trigger(idx);
839 entry.polarity = irq_polarity(idx);
841 if (irq_trigger(idx)) {
844 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
847 irq = pin_2_irq(idx, apic, pin);
848 add_pin_to_irq(irq, apic, pin);
850 if (!apic && !IO_APIC_IRQ(irq))
853 if (IO_APIC_IRQ(irq)) {
854 vector = assign_irq_vector(irq);
855 entry.vector = vector;
857 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
858 if (!apic && (irq < 16))
859 disable_8259A_irq(irq);
861 spin_lock_irqsave(&ioapic_lock, flags);
862 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
863 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
864 set_native_irq_info(irq, TARGET_CPUS);
865 spin_unlock_irqrestore(&ioapic_lock, flags);
870 apic_printk(APIC_VERBOSE," not connected.\n");
874 * Set up the 8259A-master output pin as broadcast to all
877 static void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
879 struct IO_APIC_route_entry entry;
882 memset(&entry,0,sizeof(entry));
884 disable_8259A_irq(0);
887 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
890 * We use logical delivery to get the timer IRQ
893 entry.dest_mode = INT_DEST_MODE;
894 entry.mask = 0; /* unmask IRQ now */
895 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
896 entry.delivery_mode = INT_DELIVERY_MODE;
899 entry.vector = vector;
902 * The timer IRQ doesn't have to know that behind the
903 * scene we have a 8259A-master in AEOI mode ...
905 irq_desc[0].handler = &ioapic_edge_type;
908 * Add it to the IO-APIC irq-routing table:
910 spin_lock_irqsave(&ioapic_lock, flags);
911 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
912 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
913 spin_unlock_irqrestore(&ioapic_lock, flags);
918 void __init UNEXPECTED_IO_APIC(void)
922 void __apicdebuginit print_IO_APIC(void)
925 union IO_APIC_reg_00 reg_00;
926 union IO_APIC_reg_01 reg_01;
927 union IO_APIC_reg_02 reg_02;
930 if (apic_verbosity == APIC_QUIET)
933 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
934 for (i = 0; i < nr_ioapics; i++)
935 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
936 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
939 * We are a bit conservative about what we expect. We have to
940 * know about every hardware change ASAP.
942 printk(KERN_INFO "testing the IO APIC.......................\n");
944 for (apic = 0; apic < nr_ioapics; apic++) {
946 spin_lock_irqsave(&ioapic_lock, flags);
947 reg_00.raw = io_apic_read(apic, 0);
948 reg_01.raw = io_apic_read(apic, 1);
949 if (reg_01.bits.version >= 0x10)
950 reg_02.raw = io_apic_read(apic, 2);
951 spin_unlock_irqrestore(&ioapic_lock, flags);
954 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
955 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
956 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
957 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
958 UNEXPECTED_IO_APIC();
960 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01);
961 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
962 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
963 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
964 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
965 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
966 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
967 (reg_01.bits.entries != 0x2E) &&
968 (reg_01.bits.entries != 0x3F) &&
969 (reg_01.bits.entries != 0x03)
971 UNEXPECTED_IO_APIC();
973 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
974 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
975 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
976 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
977 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
978 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
979 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
980 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
982 UNEXPECTED_IO_APIC();
983 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
984 UNEXPECTED_IO_APIC();
986 if (reg_01.bits.version >= 0x10) {
987 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
988 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
989 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
990 UNEXPECTED_IO_APIC();
993 printk(KERN_DEBUG ".... IRQ redirection table:\n");
995 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
996 " Stat Dest Deli Vect: \n");
998 for (i = 0; i <= reg_01.bits.entries; i++) {
999 struct IO_APIC_route_entry entry;
1001 spin_lock_irqsave(&ioapic_lock, flags);
1002 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
1003 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
1004 spin_unlock_irqrestore(&ioapic_lock, flags);
1006 printk(KERN_DEBUG " %02x %03X %02X ",
1008 entry.dest.logical.logical_dest,
1009 entry.dest.physical.physical_dest
1012 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1017 entry.delivery_status,
1019 entry.delivery_mode,
1024 if (use_pci_vector())
1025 printk(KERN_INFO "Using vector-based indexing\n");
1026 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1027 for (i = 0; i < NR_IRQS; i++) {
1028 struct irq_pin_list *entry = irq_2_pin + i;
1031 if (use_pci_vector() && !platform_legacy_irq(i))
1032 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1034 printk(KERN_DEBUG "IRQ%d ", i);
1036 printk("-> %d:%d", entry->apic, entry->pin);
1039 entry = irq_2_pin + entry->next;
1044 printk(KERN_INFO ".................................... done.\n");
1051 static __apicdebuginit void print_APIC_bitfield (int base)
1056 if (apic_verbosity == APIC_QUIET)
1059 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1060 for (i = 0; i < 8; i++) {
1061 v = apic_read(base + i*0x10);
1062 for (j = 0; j < 32; j++) {
1072 void __apicdebuginit print_local_APIC(void * dummy)
1074 unsigned int v, ver, maxlvt;
1076 if (apic_verbosity == APIC_QUIET)
1079 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1080 smp_processor_id(), hard_smp_processor_id());
1081 v = apic_read(APIC_ID);
1082 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1083 v = apic_read(APIC_LVR);
1084 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1085 ver = GET_APIC_VERSION(v);
1086 maxlvt = get_maxlvt();
1088 v = apic_read(APIC_TASKPRI);
1089 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1091 v = apic_read(APIC_ARBPRI);
1092 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1093 v & APIC_ARBPRI_MASK);
1094 v = apic_read(APIC_PROCPRI);
1095 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1097 v = apic_read(APIC_EOI);
1098 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1099 v = apic_read(APIC_RRR);
1100 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1101 v = apic_read(APIC_LDR);
1102 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1103 v = apic_read(APIC_DFR);
1104 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1105 v = apic_read(APIC_SPIV);
1106 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1108 printk(KERN_DEBUG "... APIC ISR field:\n");
1109 print_APIC_bitfield(APIC_ISR);
1110 printk(KERN_DEBUG "... APIC TMR field:\n");
1111 print_APIC_bitfield(APIC_TMR);
1112 printk(KERN_DEBUG "... APIC IRR field:\n");
1113 print_APIC_bitfield(APIC_IRR);
1115 v = apic_read(APIC_ESR);
1116 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1118 v = apic_read(APIC_ICR);
1119 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1120 v = apic_read(APIC_ICR2);
1121 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1123 v = apic_read(APIC_LVTT);
1124 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1126 if (maxlvt > 3) { /* PC is LVT#4. */
1127 v = apic_read(APIC_LVTPC);
1128 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1130 v = apic_read(APIC_LVT0);
1131 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1132 v = apic_read(APIC_LVT1);
1133 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1135 if (maxlvt > 2) { /* ERR is LVT#3. */
1136 v = apic_read(APIC_LVTERR);
1137 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1140 v = apic_read(APIC_TMICT);
1141 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1142 v = apic_read(APIC_TMCCT);
1143 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1144 v = apic_read(APIC_TDCR);
1145 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1149 void print_all_local_APICs (void)
1151 on_each_cpu(print_local_APIC, NULL, 1, 1);
1154 void __apicdebuginit print_PIC(void)
1157 unsigned long flags;
1159 if (apic_verbosity == APIC_QUIET)
1162 printk(KERN_DEBUG "\nprinting PIC contents\n");
1164 spin_lock_irqsave(&i8259A_lock, flags);
1166 v = inb(0xa1) << 8 | inb(0x21);
1167 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1169 v = inb(0xa0) << 8 | inb(0x20);
1170 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1174 v = inb(0xa0) << 8 | inb(0x20);
1178 spin_unlock_irqrestore(&i8259A_lock, flags);
1180 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1182 v = inb(0x4d1) << 8 | inb(0x4d0);
1183 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1188 static void __init enable_IO_APIC(void)
1190 union IO_APIC_reg_01 reg_01;
1192 unsigned long flags;
1194 for (i = 0; i < PIN_MAP_SIZE; i++) {
1195 irq_2_pin[i].pin = -1;
1196 irq_2_pin[i].next = 0;
1199 for (i = 0; i < MAX_PIRQS; i++)
1200 pirq_entries[i] = -1;
1203 * The number of IO-APIC IRQ registers (== #pins):
1205 for (i = 0; i < nr_ioapics; i++) {
1206 spin_lock_irqsave(&ioapic_lock, flags);
1207 reg_01.raw = io_apic_read(i, 1);
1208 spin_unlock_irqrestore(&ioapic_lock, flags);
1209 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1213 * Do not trust the IO-APIC being empty at bootup
1219 * Not an __init, needed by the reboot code
1221 void disable_IO_APIC(void)
1225 * Clear the IO-APIC before rebooting:
1230 * If the i8259 is routed through an IOAPIC
1231 * Put that IOAPIC in virtual wire mode
1232 * so legacy interrupts can be delivered.
1234 pin = find_isa_irq_pin(0, mp_ExtINT);
1236 struct IO_APIC_route_entry entry;
1237 unsigned long flags;
1239 memset(&entry, 0, sizeof(entry));
1240 entry.mask = 0; /* Enabled */
1241 entry.trigger = 0; /* Edge */
1243 entry.polarity = 0; /* High */
1244 entry.delivery_status = 0;
1245 entry.dest_mode = 0; /* Physical */
1246 entry.delivery_mode = 7; /* ExtInt */
1248 entry.dest.physical.physical_dest = 0;
1252 * Add it to the IO-APIC irq-routing table:
1254 spin_lock_irqsave(&ioapic_lock, flags);
1255 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
1256 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
1257 spin_unlock_irqrestore(&ioapic_lock, flags);
1260 disconnect_bsp_APIC(pin != -1);
1264 * function to set the IO-APIC physical IDs based on the
1265 * values stored in the MPC table.
1267 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1270 static void __init setup_ioapic_ids_from_mpc (void)
1272 union IO_APIC_reg_00 reg_00;
1275 unsigned char old_id;
1276 unsigned long flags;
1279 * Set the IOAPIC ID to the value stored in the MPC table.
1281 for (apic = 0; apic < nr_ioapics; apic++) {
1283 /* Read the register 0 value */
1284 spin_lock_irqsave(&ioapic_lock, flags);
1285 reg_00.raw = io_apic_read(apic, 0);
1286 spin_unlock_irqrestore(&ioapic_lock, flags);
1288 old_id = mp_ioapics[apic].mpc_apicid;
1291 printk(KERN_INFO "Using IO-APIC %d\n", mp_ioapics[apic].mpc_apicid);
1295 * We need to adjust the IRQ routing table
1296 * if the ID changed.
1298 if (old_id != mp_ioapics[apic].mpc_apicid)
1299 for (i = 0; i < mp_irq_entries; i++)
1300 if (mp_irqs[i].mpc_dstapic == old_id)
1301 mp_irqs[i].mpc_dstapic
1302 = mp_ioapics[apic].mpc_apicid;
1305 * Read the right value from the MPC table and
1306 * write it into the ID register.
1308 apic_printk(APIC_VERBOSE,KERN_INFO "...changing IO-APIC physical APIC ID to %d ...",
1309 mp_ioapics[apic].mpc_apicid);
1311 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1312 spin_lock_irqsave(&ioapic_lock, flags);
1313 io_apic_write(apic, 0, reg_00.raw);
1314 spin_unlock_irqrestore(&ioapic_lock, flags);
1319 spin_lock_irqsave(&ioapic_lock, flags);
1320 reg_00.raw = io_apic_read(apic, 0);
1321 spin_unlock_irqrestore(&ioapic_lock, flags);
1322 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1323 printk("could not set ID!\n");
1325 apic_printk(APIC_VERBOSE," ok.\n");
1330 * There is a nasty bug in some older SMP boards, their mptable lies
1331 * about the timer IRQ. We do the following to work around the situation:
1333 * - timer IRQ defaults to IO-APIC IRQ
1334 * - if this function detects that timer IRQs are defunct, then we fall
1335 * back to ISA timer IRQs
1337 static int __init timer_irq_works(void)
1339 unsigned long t1 = jiffies;
1342 /* Let ten ticks pass... */
1343 mdelay((10 * 1000) / HZ);
1346 * Expect a few ticks at least, to be sure some possible
1347 * glue logic does not lock up after one or two first
1348 * ticks in a non-ExtINT mode. Also the local APIC
1349 * might have cached one ExtINT interrupt. Finally, at
1350 * least one tick may be lost due to delays.
1354 if (jiffies - t1 > 4)
1360 * In the SMP+IOAPIC case it might happen that there are an unspecified
1361 * number of pending IRQ events unhandled. These cases are very rare,
1362 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1363 * better to do it this way as thus we do not have to be aware of
1364 * 'pending' interrupts in the IRQ path, except at this point.
1367 * Edge triggered needs to resend any interrupt
1368 * that was delayed but this is now handled in the device
1373 * Starting up a edge-triggered IO-APIC interrupt is
1374 * nasty - we need to make sure that we get the edge.
1375 * If it is already asserted for some reason, we need
1376 * return 1 to indicate that is was pending.
1378 * This is not complete - we should be able to fake
1379 * an edge even if it isn't on the 8259A...
1382 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1384 int was_pending = 0;
1385 unsigned long flags;
1387 spin_lock_irqsave(&ioapic_lock, flags);
1389 disable_8259A_irq(irq);
1390 if (i8259A_irq_pending(irq))
1393 __unmask_IO_APIC_irq(irq);
1394 spin_unlock_irqrestore(&ioapic_lock, flags);
1400 * Once we have recorded IRQ_PENDING already, we can mask the
1401 * interrupt for real. This prevents IRQ storms from unhandled
1404 static void ack_edge_ioapic_irq(unsigned int irq)
1407 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1408 == (IRQ_PENDING | IRQ_DISABLED))
1409 mask_IO_APIC_irq(irq);
1414 * Level triggered interrupts can just be masked,
1415 * and shutting down and starting up the interrupt
1416 * is the same as enabling and disabling them -- except
1417 * with a startup need to return a "was pending" value.
1419 * Level triggered interrupts are special because we
1420 * do not touch any IO-APIC register while handling
1421 * them. We ack the APIC in the end-IRQ handler, not
1422 * in the start-IRQ-handler. Protection against reentrance
1423 * from the same interrupt is still provided, both by the
1424 * generic IRQ layer and by the fact that an unacked local
1425 * APIC does not accept IRQs.
1427 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1429 unmask_IO_APIC_irq(irq);
1431 return 0; /* don't check for pending */
1434 static void end_level_ioapic_irq (unsigned int irq)
1440 #ifdef CONFIG_PCI_MSI
1441 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1443 int irq = vector_to_irq(vector);
1445 return startup_edge_ioapic_irq(irq);
1448 static void ack_edge_ioapic_vector(unsigned int vector)
1450 int irq = vector_to_irq(vector);
1452 move_native_irq(vector);
1453 ack_edge_ioapic_irq(irq);
1456 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1458 int irq = vector_to_irq(vector);
1460 return startup_level_ioapic_irq (irq);
1463 static void end_level_ioapic_vector (unsigned int vector)
1465 int irq = vector_to_irq(vector);
1467 move_native_irq(vector);
1468 end_level_ioapic_irq(irq);
1471 static void mask_IO_APIC_vector (unsigned int vector)
1473 int irq = vector_to_irq(vector);
1475 mask_IO_APIC_irq(irq);
1478 static void unmask_IO_APIC_vector (unsigned int vector)
1480 int irq = vector_to_irq(vector);
1482 unmask_IO_APIC_irq(irq);
1486 static void set_ioapic_affinity_vector (unsigned int vector,
1489 int irq = vector_to_irq(vector);
1491 set_native_irq_info(vector, cpu_mask);
1492 set_ioapic_affinity_irq(irq, cpu_mask);
1494 #endif // CONFIG_SMP
1495 #endif // CONFIG_PCI_MSI
1498 * Level and edge triggered IO-APIC interrupts need different handling,
1499 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1500 * handled with the level-triggered descriptor, but that one has slightly
1501 * more overhead. Level-triggered interrupts cannot be handled with the
1502 * edge-triggered handler, without risking IRQ storms and other ugly
1506 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1507 .typename = "IO-APIC-edge",
1508 .startup = startup_edge_ioapic,
1509 .shutdown = shutdown_edge_ioapic,
1510 .enable = enable_edge_ioapic,
1511 .disable = disable_edge_ioapic,
1512 .ack = ack_edge_ioapic,
1513 .end = end_edge_ioapic,
1515 .set_affinity = set_ioapic_affinity,
1519 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1520 .typename = "IO-APIC-level",
1521 .startup = startup_level_ioapic,
1522 .shutdown = shutdown_level_ioapic,
1523 .enable = enable_level_ioapic,
1524 .disable = disable_level_ioapic,
1525 .ack = mask_and_ack_level_ioapic,
1526 .end = end_level_ioapic,
1528 .set_affinity = set_ioapic_affinity,
1532 static inline void init_IO_APIC_traps(void)
1537 * NOTE! The local APIC isn't very good at handling
1538 * multiple interrupts at the same interrupt level.
1539 * As the interrupt level is determined by taking the
1540 * vector number and shifting that right by 4, we
1541 * want to spread these out a bit so that they don't
1542 * all fall in the same interrupt level.
1544 * Also, we've got to be careful not to trash gate
1545 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1547 for (irq = 0; irq < NR_IRQS ; irq++) {
1549 if (use_pci_vector()) {
1550 if (!platform_legacy_irq(tmp))
1551 if ((tmp = vector_to_irq(tmp)) == -1)
1554 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
1556 * Hmm.. We don't have an entry for this,
1557 * so default to an old-fashioned 8259
1558 * interrupt if we can..
1561 make_8259A_irq(irq);
1563 /* Strange. Oh, well.. */
1564 irq_desc[irq].handler = &no_irq_type;
1569 static void enable_lapic_irq (unsigned int irq)
1573 v = apic_read(APIC_LVT0);
1574 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
1577 static void disable_lapic_irq (unsigned int irq)
1581 v = apic_read(APIC_LVT0);
1582 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
1585 static void ack_lapic_irq (unsigned int irq)
1590 static void end_lapic_irq (unsigned int i) { /* nothing */ }
1592 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1593 .typename = "local-APIC-edge",
1594 .startup = NULL, /* startup_irq() not used for IRQ0 */
1595 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1596 .enable = enable_lapic_irq,
1597 .disable = disable_lapic_irq,
1598 .ack = ack_lapic_irq,
1599 .end = end_lapic_irq,
1602 static void setup_nmi (void)
1605 * Dirty trick to enable the NMI watchdog ...
1606 * We put the 8259A master into AEOI mode and
1607 * unmask on all local APICs LVT0 as NMI.
1609 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1610 * is from Maciej W. Rozycki - so we do not have to EOI from
1611 * the NMI handler or the timer interrupt.
1613 printk(KERN_INFO "activating NMI Watchdog ...");
1615 enable_NMI_through_LVT0(NULL);
1621 * This looks a bit hackish but it's about the only one way of sending
1622 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1623 * not support the ExtINT mode, unfortunately. We need to send these
1624 * cycles as some i82489DX-based boards have glue logic that keeps the
1625 * 8259A interrupt line asserted until INTA. --macro
1627 static inline void unlock_ExtINT_logic(void)
1630 struct IO_APIC_route_entry entry0, entry1;
1631 unsigned char save_control, save_freq_select;
1632 unsigned long flags;
1634 pin = find_isa_irq_pin(8, mp_INT);
1638 spin_lock_irqsave(&ioapic_lock, flags);
1639 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
1640 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
1641 spin_unlock_irqrestore(&ioapic_lock, flags);
1642 clear_IO_APIC_pin(0, pin);
1644 memset(&entry1, 0, sizeof(entry1));
1646 entry1.dest_mode = 0; /* physical delivery */
1647 entry1.mask = 0; /* unmask IRQ now */
1648 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1649 entry1.delivery_mode = dest_ExtINT;
1650 entry1.polarity = entry0.polarity;
1654 spin_lock_irqsave(&ioapic_lock, flags);
1655 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1656 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1657 spin_unlock_irqrestore(&ioapic_lock, flags);
1659 save_control = CMOS_READ(RTC_CONTROL);
1660 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1661 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1663 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1668 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1672 CMOS_WRITE(save_control, RTC_CONTROL);
1673 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1674 clear_IO_APIC_pin(0, pin);
1676 spin_lock_irqsave(&ioapic_lock, flags);
1677 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1678 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1679 spin_unlock_irqrestore(&ioapic_lock, flags);
1683 * This code may look a bit paranoid, but it's supposed to cooperate with
1684 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1685 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1686 * fanatically on his truly buggy board.
1688 static inline void check_timer(void)
1694 * get/set the timer IRQ vector:
1696 disable_8259A_irq(0);
1697 vector = assign_irq_vector(0);
1698 set_intr_gate(vector, interrupt[0]);
1701 * Subtle, code in do_timer_interrupt() expects an AEOI
1702 * mode for the 8259A whenever interrupts are routed
1703 * through I/O APICs. Also IRQ0 has to be enabled in
1704 * the 8259A which implies the virtual wire has to be
1705 * disabled in the local APIC.
1707 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1709 enable_8259A_irq(0);
1711 pin1 = find_isa_irq_pin(0, mp_INT);
1712 pin2 = find_isa_irq_pin(0, mp_ExtINT);
1714 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
1718 * Ok, does IRQ0 through the IOAPIC work?
1720 unmask_IO_APIC_irq(0);
1721 if (!no_timer_check && timer_irq_works()) {
1722 nmi_watchdog_default();
1723 if (nmi_watchdog == NMI_IO_APIC) {
1724 disable_8259A_irq(0);
1726 enable_8259A_irq(0);
1728 if (disable_timer_pin_1 > 0)
1729 clear_IO_APIC_pin(0, pin1);
1732 clear_IO_APIC_pin(0, pin1);
1733 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
1736 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1738 apic_printk(APIC_VERBOSE,"\n..... (found pin %d) ...", pin2);
1740 * legacy devices should be connected to IO APIC #0
1742 setup_ExtINT_IRQ0_pin(pin2, vector);
1743 if (timer_irq_works()) {
1745 nmi_watchdog_default();
1746 if (nmi_watchdog == NMI_IO_APIC) {
1752 * Cleanup, just in case ...
1754 clear_IO_APIC_pin(0, pin2);
1756 printk(" failed.\n");
1759 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1763 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1765 disable_8259A_irq(0);
1766 irq_desc[0].handler = &lapic_irq_type;
1767 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1768 enable_8259A_irq(0);
1770 if (timer_irq_works()) {
1771 apic_printk(APIC_QUIET, " works.\n");
1774 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1775 apic_printk(APIC_VERBOSE," failed.\n");
1777 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1781 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
1783 unlock_ExtINT_logic();
1785 if (timer_irq_works()) {
1786 apic_printk(APIC_VERBOSE," works.\n");
1789 apic_printk(APIC_VERBOSE," failed :(.\n");
1790 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1793 static int __init notimercheck(char *s)
1798 __setup("no_timer_check", notimercheck);
1802 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1803 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1804 * Linux doesn't really care, as it's not actually used
1805 * for any interrupt handling anyway.
1807 #define PIC_IRQS (1<<2)
1809 void __init setup_IO_APIC(void)
1814 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1816 io_apic_irqs = ~PIC_IRQS;
1818 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1821 * Set up the IO-APIC IRQ routing table.
1824 setup_ioapic_ids_from_mpc();
1826 setup_IO_APIC_irqs();
1827 init_IO_APIC_traps();
1833 struct sysfs_ioapic_data {
1834 struct sys_device dev;
1835 struct IO_APIC_route_entry entry[0];
1837 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1839 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1841 struct IO_APIC_route_entry *entry;
1842 struct sysfs_ioapic_data *data;
1843 unsigned long flags;
1846 data = container_of(dev, struct sysfs_ioapic_data, dev);
1847 entry = data->entry;
1848 spin_lock_irqsave(&ioapic_lock, flags);
1849 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1850 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
1851 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
1853 spin_unlock_irqrestore(&ioapic_lock, flags);
1858 static int ioapic_resume(struct sys_device *dev)
1860 struct IO_APIC_route_entry *entry;
1861 struct sysfs_ioapic_data *data;
1862 unsigned long flags;
1863 union IO_APIC_reg_00 reg_00;
1866 data = container_of(dev, struct sysfs_ioapic_data, dev);
1867 entry = data->entry;
1869 spin_lock_irqsave(&ioapic_lock, flags);
1870 reg_00.raw = io_apic_read(dev->id, 0);
1871 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1872 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1873 io_apic_write(dev->id, 0, reg_00.raw);
1875 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
1876 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
1877 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
1879 spin_unlock_irqrestore(&ioapic_lock, flags);
1884 static struct sysdev_class ioapic_sysdev_class = {
1885 set_kset_name("ioapic"),
1886 .suspend = ioapic_suspend,
1887 .resume = ioapic_resume,
1890 static int __init ioapic_init_sysfs(void)
1892 struct sys_device * dev;
1893 int i, size, error = 0;
1895 error = sysdev_class_register(&ioapic_sysdev_class);
1899 for (i = 0; i < nr_ioapics; i++ ) {
1900 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1901 * sizeof(struct IO_APIC_route_entry);
1902 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1903 if (!mp_ioapic_data[i]) {
1904 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1907 memset(mp_ioapic_data[i], 0, size);
1908 dev = &mp_ioapic_data[i]->dev;
1910 dev->cls = &ioapic_sysdev_class;
1911 error = sysdev_register(dev);
1913 kfree(mp_ioapic_data[i]);
1914 mp_ioapic_data[i] = NULL;
1915 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1923 device_initcall(ioapic_init_sysfs);
1925 /* --------------------------------------------------------------------------
1926 ACPI-based IOAPIC Configuration
1927 -------------------------------------------------------------------------- */
1931 #define IO_APIC_MAX_ID 0xFE
1933 int __init io_apic_get_version (int ioapic)
1935 union IO_APIC_reg_01 reg_01;
1936 unsigned long flags;
1938 spin_lock_irqsave(&ioapic_lock, flags);
1939 reg_01.raw = io_apic_read(ioapic, 1);
1940 spin_unlock_irqrestore(&ioapic_lock, flags);
1942 return reg_01.bits.version;
1946 int __init io_apic_get_redir_entries (int ioapic)
1948 union IO_APIC_reg_01 reg_01;
1949 unsigned long flags;
1951 spin_lock_irqsave(&ioapic_lock, flags);
1952 reg_01.raw = io_apic_read(ioapic, 1);
1953 spin_unlock_irqrestore(&ioapic_lock, flags);
1955 return reg_01.bits.entries;
1959 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
1961 struct IO_APIC_route_entry entry;
1962 unsigned long flags;
1964 if (!IO_APIC_IRQ(irq)) {
1965 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1971 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
1972 * Note that we mask (disable) IRQs now -- these get enabled when the
1973 * corresponding device driver registers for this IRQ.
1976 memset(&entry,0,sizeof(entry));
1978 entry.delivery_mode = INT_DELIVERY_MODE;
1979 entry.dest_mode = INT_DEST_MODE;
1980 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1981 entry.trigger = edge_level;
1982 entry.polarity = active_high_low;
1983 entry.mask = 1; /* Disabled (masked) */
1985 irq = gsi_irq_sharing(irq);
1987 * IRQs < 16 are already in the irq_2_pin[] map
1990 add_pin_to_irq(irq, ioapic, pin);
1992 entry.vector = assign_irq_vector(irq);
1994 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
1995 "IRQ %d Mode:%i Active:%i)\n", ioapic,
1996 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
1997 edge_level, active_high_low);
1999 ioapic_register_intr(irq, entry.vector, edge_level);
2001 if (!ioapic && (irq < 16))
2002 disable_8259A_irq(irq);
2004 spin_lock_irqsave(&ioapic_lock, flags);
2005 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2006 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2007 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
2008 spin_unlock_irqrestore(&ioapic_lock, flags);
2013 #endif /* CONFIG_ACPI */
2017 * This function currently is only a helper for the i386 smp boot process where
2018 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2019 * so mask in all cases should simply be TARGET_CPUS
2022 void __init setup_ioapic_dest(void)
2024 int pin, ioapic, irq, irq_entry;
2026 if (skip_ioapic_setup == 1)
2029 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2030 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2031 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2032 if (irq_entry == -1)
2034 irq = pin_2_irq(irq_entry, ioapic, pin);
2035 set_ioapic_affinity_irq(irq, TARGET_CPUS);