2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
24 #include <linux/irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/delay.h>
28 #include <linux/sched.h>
29 #include <linux/config.h>
30 #include <linux/smp_lock.h>
31 #include <linux/mc146818rtc.h>
32 #include <linux/compiler.h>
33 #include <linux/acpi.h>
34 #include <linux/module.h>
35 #include <linux/sysdev.h>
40 #include <asm/timer.h>
41 #include <asm/i8259.h>
43 #include <mach_apic.h>
47 int (*ioapic_renumber_irq)(int ioapic, int irq);
48 atomic_t irq_mis_count;
50 static DEFINE_SPINLOCK(ioapic_lock);
53 * Is the SiS APIC rmw bug present ?
54 * -1 = don't know, 0 = no, 1 = yes
56 int sis_apic_bug = -1;
59 * # of IRQ routing registers
61 int nr_ioapic_registers[MAX_IO_APICS];
64 * Rough estimation of how many shared IRQs there are, can
67 #define MAX_PLUS_SHARED_IRQS NR_IRQS
68 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
71 * This is performance-critical, we want to do it O(1)
73 * the indexing order of this array favors 1:1 mappings
74 * between pins and IRQs.
77 static struct irq_pin_list {
79 } irq_2_pin[PIN_MAP_SIZE];
81 int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
83 #define vector_to_irq(vector) \
84 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
86 #define vector_to_irq(vector) (vector)
90 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
91 * shared ISA-space IRQs, so we have to support them. We are super
92 * fast in the common case, and fast for shared ISA-space IRQs.
94 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
96 static int first_free_entry = NR_IRQS;
97 struct irq_pin_list *entry = irq_2_pin + irq;
100 entry = irq_2_pin + entry->next;
102 if (entry->pin != -1) {
103 entry->next = first_free_entry;
104 entry = irq_2_pin + entry->next;
105 if (++first_free_entry >= PIN_MAP_SIZE)
106 panic("io_apic.c: whoops");
113 * Reroute an IRQ to a different pin.
115 static void __init replace_pin_at_irq(unsigned int irq,
116 int oldapic, int oldpin,
117 int newapic, int newpin)
119 struct irq_pin_list *entry = irq_2_pin + irq;
122 if (entry->apic == oldapic && entry->pin == oldpin) {
123 entry->apic = newapic;
128 entry = irq_2_pin + entry->next;
132 static void __modify_IO_APIC_irq (unsigned int irq, unsigned long enable, unsigned long disable)
134 struct irq_pin_list *entry = irq_2_pin + irq;
135 unsigned int pin, reg;
141 reg = io_apic_read(entry->apic, 0x10 + pin*2);
144 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
147 entry = irq_2_pin + entry->next;
152 static void __mask_IO_APIC_irq (unsigned int irq)
154 __modify_IO_APIC_irq(irq, 0x00010000, 0);
158 static void __unmask_IO_APIC_irq (unsigned int irq)
160 __modify_IO_APIC_irq(irq, 0, 0x00010000);
163 /* mask = 1, trigger = 0 */
164 static void __mask_and_edge_IO_APIC_irq (unsigned int irq)
166 __modify_IO_APIC_irq(irq, 0x00010000, 0x00008000);
169 /* mask = 0, trigger = 1 */
170 static void __unmask_and_level_IO_APIC_irq (unsigned int irq)
172 __modify_IO_APIC_irq(irq, 0x00008000, 0x00010000);
175 static void mask_IO_APIC_irq (unsigned int irq)
179 spin_lock_irqsave(&ioapic_lock, flags);
180 __mask_IO_APIC_irq(irq);
181 spin_unlock_irqrestore(&ioapic_lock, flags);
184 static void unmask_IO_APIC_irq (unsigned int irq)
188 spin_lock_irqsave(&ioapic_lock, flags);
189 __unmask_IO_APIC_irq(irq);
190 spin_unlock_irqrestore(&ioapic_lock, flags);
193 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
195 struct IO_APIC_route_entry entry;
198 /* Check delivery_mode to be sure we're not clearing an SMI pin */
199 spin_lock_irqsave(&ioapic_lock, flags);
200 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
201 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
202 spin_unlock_irqrestore(&ioapic_lock, flags);
203 if (entry.delivery_mode == dest_SMI)
207 * Disable it in the IO-APIC irq-routing table:
209 memset(&entry, 0, sizeof(entry));
211 spin_lock_irqsave(&ioapic_lock, flags);
212 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
213 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
214 spin_unlock_irqrestore(&ioapic_lock, flags);
217 static void clear_IO_APIC (void)
221 for (apic = 0; apic < nr_ioapics; apic++)
222 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
223 clear_IO_APIC_pin(apic, pin);
227 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
231 struct irq_pin_list *entry = irq_2_pin + irq;
232 unsigned int apicid_value;
235 cpus_and(tmp, cpumask, cpu_online_map);
239 cpus_and(cpumask, tmp, CPU_MASK_ALL);
241 apicid_value = cpu_mask_to_apicid(cpumask);
242 /* Prepare to do the io_apic_write */
243 apicid_value = apicid_value << 24;
244 spin_lock_irqsave(&ioapic_lock, flags);
249 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
252 entry = irq_2_pin + entry->next;
254 set_irq_info(irq, cpumask);
255 spin_unlock_irqrestore(&ioapic_lock, flags);
258 #if defined(CONFIG_IRQBALANCE)
259 # include <asm/processor.h> /* kernel_thread() */
260 # include <linux/kernel_stat.h> /* kstat */
261 # include <linux/slab.h> /* kmalloc() */
262 # include <linux/timer.h> /* time_after() */
264 # ifdef CONFIG_BALANCED_IRQ_DEBUG
265 # define TDprintk(x...) do { printk("<%ld:%s:%d>: ", jiffies, __FILE__, __LINE__); printk(x); } while (0)
266 # define Dprintk(x...) do { TDprintk(x); } while (0)
268 # define TDprintk(x...)
269 # define Dprintk(x...)
273 #define IRQBALANCE_CHECK_ARCH -999
274 static int irqbalance_disabled = IRQBALANCE_CHECK_ARCH;
275 static int physical_balance = 0;
277 static struct irq_cpu_info {
278 unsigned long * last_irq;
279 unsigned long * irq_delta;
281 } irq_cpu_data[NR_CPUS];
283 #define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
284 #define LAST_CPU_IRQ(cpu,irq) (irq_cpu_data[cpu].last_irq[irq])
285 #define IRQ_DELTA(cpu,irq) (irq_cpu_data[cpu].irq_delta[irq])
287 #define IDLE_ENOUGH(cpu,now) \
288 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
290 #define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
292 #define CPU_TO_PACKAGEINDEX(i) (first_cpu(cpu_sibling_map[i]))
294 #define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
295 #define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
296 #define BALANCED_IRQ_MORE_DELTA (HZ/10)
297 #define BALANCED_IRQ_LESS_DELTA (HZ)
299 static long balanced_irq_interval = MAX_BALANCED_IRQ_INTERVAL;
301 static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
302 unsigned long now, int direction)
310 if (unlikely(cpu == curr_cpu))
313 if (direction == 1) {
322 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu,allowed_mask) ||
323 (search_idle && !IDLE_ENOUGH(cpu,now)));
328 static inline void balance_irq(int cpu, int irq)
330 unsigned long now = jiffies;
331 cpumask_t allowed_mask;
332 unsigned int new_cpu;
334 if (irqbalance_disabled)
337 cpus_and(allowed_mask, cpu_online_map, irq_affinity[irq]);
338 new_cpu = move(cpu, allowed_mask, now, 1);
339 if (cpu != new_cpu) {
340 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
344 static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
347 Dprintk("Rotating IRQs among CPUs.\n");
348 for (i = 0; i < NR_CPUS; i++) {
349 for (j = 0; cpu_online(i) && (j < NR_IRQS); j++) {
350 if (!irq_desc[j].action)
352 /* Is it a significant load ? */
353 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i),j) <
354 useful_load_threshold)
359 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
360 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
364 static void do_irq_balance(void)
367 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
368 unsigned long move_this_load = 0;
369 int max_loaded = 0, min_loaded = 0;
371 unsigned long useful_load_threshold = balanced_irq_interval + 10;
373 int tmp_loaded, first_attempt = 1;
374 unsigned long tmp_cpu_irq;
375 unsigned long imbalance = 0;
376 cpumask_t allowed_mask, target_cpu_mask, tmp;
378 for (i = 0; i < NR_CPUS; i++) {
383 package_index = CPU_TO_PACKAGEINDEX(i);
384 for (j = 0; j < NR_IRQS; j++) {
385 unsigned long value_now, delta;
386 /* Is this an active IRQ? */
387 if (!irq_desc[j].action)
389 if ( package_index == i )
390 IRQ_DELTA(package_index,j) = 0;
391 /* Determine the total count per processor per IRQ */
392 value_now = (unsigned long) kstat_cpu(i).irqs[j];
394 /* Determine the activity per processor per IRQ */
395 delta = value_now - LAST_CPU_IRQ(i,j);
397 /* Update last_cpu_irq[][] for the next time */
398 LAST_CPU_IRQ(i,j) = value_now;
400 /* Ignore IRQs whose rate is less than the clock */
401 if (delta < useful_load_threshold)
403 /* update the load for the processor or package total */
404 IRQ_DELTA(package_index,j) += delta;
406 /* Keep track of the higher numbered sibling as well */
407 if (i != package_index)
410 * We have sibling A and sibling B in the package
412 * cpu_irq[A] = load for cpu A + load for cpu B
413 * cpu_irq[B] = load for cpu B
415 CPU_IRQ(package_index) += delta;
418 /* Find the least loaded processor package */
419 for (i = 0; i < NR_CPUS; i++) {
422 if (i != CPU_TO_PACKAGEINDEX(i))
424 if (min_cpu_irq > CPU_IRQ(i)) {
425 min_cpu_irq = CPU_IRQ(i);
429 max_cpu_irq = ULONG_MAX;
432 /* Look for heaviest loaded processor.
433 * We may come back to get the next heaviest loaded processor.
434 * Skip processors with trivial loads.
438 for (i = 0; i < NR_CPUS; i++) {
441 if (i != CPU_TO_PACKAGEINDEX(i))
443 if (max_cpu_irq <= CPU_IRQ(i))
445 if (tmp_cpu_irq < CPU_IRQ(i)) {
446 tmp_cpu_irq = CPU_IRQ(i);
451 if (tmp_loaded == -1) {
452 /* In the case of small number of heavy interrupt sources,
453 * loading some of the cpus too much. We use Ingo's original
454 * approach to rotate them around.
456 if (!first_attempt && imbalance >= useful_load_threshold) {
457 rotate_irqs_among_cpus(useful_load_threshold);
460 goto not_worth_the_effort;
463 first_attempt = 0; /* heaviest search */
464 max_cpu_irq = tmp_cpu_irq; /* load */
465 max_loaded = tmp_loaded; /* processor */
466 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
468 Dprintk("max_loaded cpu = %d\n", max_loaded);
469 Dprintk("min_loaded cpu = %d\n", min_loaded);
470 Dprintk("max_cpu_irq load = %ld\n", max_cpu_irq);
471 Dprintk("min_cpu_irq load = %ld\n", min_cpu_irq);
472 Dprintk("load imbalance = %lu\n", imbalance);
474 /* if imbalance is less than approx 10% of max load, then
475 * observe diminishing returns action. - quit
477 if (imbalance < (max_cpu_irq >> 3)) {
478 Dprintk("Imbalance too trivial\n");
479 goto not_worth_the_effort;
483 /* if we select an IRQ to move that can't go where we want, then
484 * see if there is another one to try.
488 for (j = 0; j < NR_IRQS; j++) {
489 /* Is this an active IRQ? */
490 if (!irq_desc[j].action)
492 if (imbalance <= IRQ_DELTA(max_loaded,j))
494 /* Try to find the IRQ that is closest to the imbalance
495 * without going over.
497 if (move_this_load < IRQ_DELTA(max_loaded,j)) {
498 move_this_load = IRQ_DELTA(max_loaded,j);
502 if (selected_irq == -1) {
506 imbalance = move_this_load;
508 /* For physical_balance case, we accumlated both load
509 * values in the one of the siblings cpu_irq[],
510 * to use the same code for physical and logical processors
511 * as much as possible.
513 * NOTE: the cpu_irq[] array holds the sum of the load for
514 * sibling A and sibling B in the slot for the lowest numbered
515 * sibling (A), _AND_ the load for sibling B in the slot for
516 * the higher numbered sibling.
518 * We seek the least loaded sibling by making the comparison
521 load = CPU_IRQ(min_loaded) >> 1;
522 for_each_cpu_mask(j, cpu_sibling_map[min_loaded]) {
523 if (load > CPU_IRQ(j)) {
524 /* This won't change cpu_sibling_map[min_loaded] */
530 cpus_and(allowed_mask, cpu_online_map, irq_affinity[selected_irq]);
531 target_cpu_mask = cpumask_of_cpu(min_loaded);
532 cpus_and(tmp, target_cpu_mask, allowed_mask);
534 if (!cpus_empty(tmp)) {
536 Dprintk("irq = %d moved to cpu = %d\n",
537 selected_irq, min_loaded);
538 /* mark for change destination */
539 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
541 /* Since we made a change, come back sooner to
542 * check for more variation.
544 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
545 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
550 not_worth_the_effort:
552 * if we did not find an IRQ to move, then adjust the time interval
555 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
556 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
557 Dprintk("IRQ worth rotating not found\n");
561 static int balanced_irq(void *unused)
564 unsigned long prev_balance_time = jiffies;
565 long time_remaining = balanced_irq_interval;
569 /* push everything to CPU 0 to give us a starting point. */
570 for (i = 0 ; i < NR_IRQS ; i++) {
571 pending_irq_cpumask[i] = cpumask_of_cpu(0);
572 set_pending_irq(i, cpumask_of_cpu(0));
576 time_remaining = schedule_timeout_interruptible(time_remaining);
578 if (time_after(jiffies,
579 prev_balance_time+balanced_irq_interval)) {
582 prev_balance_time = jiffies;
583 time_remaining = balanced_irq_interval;
590 static int __init balanced_irq_init(void)
593 struct cpuinfo_x86 *c;
596 cpus_shift_right(tmp, cpu_online_map, 2);
598 /* When not overwritten by the command line ask subarchitecture. */
599 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
600 irqbalance_disabled = NO_BALANCE_IRQ;
601 if (irqbalance_disabled)
604 /* disable irqbalance completely if there is only one processor online */
605 if (num_online_cpus() < 2) {
606 irqbalance_disabled = 1;
610 * Enable physical balance only if more than 1 physical processor
613 if (smp_num_siblings > 1 && !cpus_empty(tmp))
614 physical_balance = 1;
616 for (i = 0; i < NR_CPUS; i++) {
619 irq_cpu_data[i].irq_delta = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
620 irq_cpu_data[i].last_irq = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
621 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
622 printk(KERN_ERR "balanced_irq_init: out of memory");
625 memset(irq_cpu_data[i].irq_delta,0,sizeof(unsigned long) * NR_IRQS);
626 memset(irq_cpu_data[i].last_irq,0,sizeof(unsigned long) * NR_IRQS);
629 printk(KERN_INFO "Starting balanced_irq\n");
630 if (kernel_thread(balanced_irq, NULL, CLONE_KERNEL) >= 0)
633 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
635 for (i = 0; i < NR_CPUS; i++) {
636 kfree(irq_cpu_data[i].irq_delta);
637 kfree(irq_cpu_data[i].last_irq);
642 int __init irqbalance_disable(char *str)
644 irqbalance_disabled = 1;
648 __setup("noirqbalance", irqbalance_disable);
650 late_initcall(balanced_irq_init);
651 #endif /* CONFIG_IRQBALANCE */
652 #endif /* CONFIG_SMP */
655 void fastcall send_IPI_self(int vector)
662 apic_wait_icr_idle();
663 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
665 * Send the IPI. The write to APIC_ICR fires this off.
667 apic_write_around(APIC_ICR, cfg);
669 #endif /* !CONFIG_SMP */
673 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
674 * specific CPU-side IRQs.
678 static int pirq_entries [MAX_PIRQS];
679 static int pirqs_enabled;
680 int skip_ioapic_setup;
682 static int __init ioapic_setup(char *str)
684 skip_ioapic_setup = 1;
688 __setup("noapic", ioapic_setup);
690 static int __init ioapic_pirq_setup(char *str)
693 int ints[MAX_PIRQS+1];
695 get_options(str, ARRAY_SIZE(ints), ints);
697 for (i = 0; i < MAX_PIRQS; i++)
698 pirq_entries[i] = -1;
701 apic_printk(APIC_VERBOSE, KERN_INFO
702 "PIRQ redirection, working around broken MP-BIOS.\n");
704 if (ints[0] < MAX_PIRQS)
707 for (i = 0; i < max; i++) {
708 apic_printk(APIC_VERBOSE, KERN_DEBUG
709 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
711 * PIRQs are mapped upside down, usually.
713 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
718 __setup("pirq=", ioapic_pirq_setup);
721 * Find the IRQ entry number of a certain pin.
723 static int find_irq_entry(int apic, int pin, int type)
727 for (i = 0; i < mp_irq_entries; i++)
728 if (mp_irqs[i].mpc_irqtype == type &&
729 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
730 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
731 mp_irqs[i].mpc_dstirq == pin)
738 * Find the pin to which IRQ[irq] (ISA) is connected
740 static int find_isa_irq_pin(int irq, int type)
744 for (i = 0; i < mp_irq_entries; i++) {
745 int lbus = mp_irqs[i].mpc_srcbus;
747 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
748 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
749 mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
750 mp_bus_id_to_type[lbus] == MP_BUS_NEC98
752 (mp_irqs[i].mpc_irqtype == type) &&
753 (mp_irqs[i].mpc_srcbusirq == irq))
755 return mp_irqs[i].mpc_dstirq;
761 * Find a specific PCI IRQ entry.
762 * Not an __init, possibly needed by modules
764 static int pin_2_irq(int idx, int apic, int pin);
766 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
768 int apic, i, best_guess = -1;
770 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
771 "slot:%d, pin:%d.\n", bus, slot, pin);
772 if (mp_bus_id_to_pci_bus[bus] == -1) {
773 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
776 for (i = 0; i < mp_irq_entries; i++) {
777 int lbus = mp_irqs[i].mpc_srcbus;
779 for (apic = 0; apic < nr_ioapics; apic++)
780 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
781 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
784 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
785 !mp_irqs[i].mpc_irqtype &&
787 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
788 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
790 if (!(apic || IO_APIC_IRQ(irq)))
793 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
796 * Use the first all-but-pin matching entry as a
797 * best-guess fuzzy result for broken mptables.
805 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
808 * This function currently is only a helper for the i386 smp boot process where
809 * we need to reprogram the ioredtbls to cater for the cpus which have come online
810 * so mask in all cases should simply be TARGET_CPUS
813 void __init setup_ioapic_dest(void)
815 int pin, ioapic, irq, irq_entry;
817 if (skip_ioapic_setup == 1)
820 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
821 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
822 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
825 irq = pin_2_irq(irq_entry, ioapic, pin);
826 set_ioapic_affinity_irq(irq, TARGET_CPUS);
834 * EISA Edge/Level control register, ELCR
836 static int EISA_ELCR(unsigned int irq)
839 unsigned int port = 0x4d0 + (irq >> 3);
840 return (inb(port) >> (irq & 7)) & 1;
842 apic_printk(APIC_VERBOSE, KERN_INFO
843 "Broken MPtable reports ISA irq %d\n", irq);
847 /* EISA interrupts are always polarity zero and can be edge or level
848 * trigger depending on the ELCR value. If an interrupt is listed as
849 * EISA conforming in the MP table, that means its trigger type must
850 * be read in from the ELCR */
852 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
853 #define default_EISA_polarity(idx) (0)
855 /* ISA interrupts are always polarity zero edge triggered,
856 * when listed as conforming in the MP table. */
858 #define default_ISA_trigger(idx) (0)
859 #define default_ISA_polarity(idx) (0)
861 /* PCI interrupts are always polarity one level triggered,
862 * when listed as conforming in the MP table. */
864 #define default_PCI_trigger(idx) (1)
865 #define default_PCI_polarity(idx) (1)
867 /* MCA interrupts are always polarity zero level triggered,
868 * when listed as conforming in the MP table. */
870 #define default_MCA_trigger(idx) (1)
871 #define default_MCA_polarity(idx) (0)
873 /* NEC98 interrupts are always polarity zero edge triggered,
874 * when listed as conforming in the MP table. */
876 #define default_NEC98_trigger(idx) (0)
877 #define default_NEC98_polarity(idx) (0)
879 static int __init MPBIOS_polarity(int idx)
881 int bus = mp_irqs[idx].mpc_srcbus;
885 * Determine IRQ line polarity (high active or low active):
887 switch (mp_irqs[idx].mpc_irqflag & 3)
889 case 0: /* conforms, ie. bus-type dependent polarity */
891 switch (mp_bus_id_to_type[bus])
893 case MP_BUS_ISA: /* ISA pin */
895 polarity = default_ISA_polarity(idx);
898 case MP_BUS_EISA: /* EISA pin */
900 polarity = default_EISA_polarity(idx);
903 case MP_BUS_PCI: /* PCI pin */
905 polarity = default_PCI_polarity(idx);
908 case MP_BUS_MCA: /* MCA pin */
910 polarity = default_MCA_polarity(idx);
913 case MP_BUS_NEC98: /* NEC 98 pin */
915 polarity = default_NEC98_polarity(idx);
920 printk(KERN_WARNING "broken BIOS!!\n");
927 case 1: /* high active */
932 case 2: /* reserved */
934 printk(KERN_WARNING "broken BIOS!!\n");
938 case 3: /* low active */
943 default: /* invalid */
945 printk(KERN_WARNING "broken BIOS!!\n");
953 static int MPBIOS_trigger(int idx)
955 int bus = mp_irqs[idx].mpc_srcbus;
959 * Determine IRQ trigger mode (edge or level sensitive):
961 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
963 case 0: /* conforms, ie. bus-type dependent */
965 switch (mp_bus_id_to_type[bus])
967 case MP_BUS_ISA: /* ISA pin */
969 trigger = default_ISA_trigger(idx);
972 case MP_BUS_EISA: /* EISA pin */
974 trigger = default_EISA_trigger(idx);
977 case MP_BUS_PCI: /* PCI pin */
979 trigger = default_PCI_trigger(idx);
982 case MP_BUS_MCA: /* MCA pin */
984 trigger = default_MCA_trigger(idx);
987 case MP_BUS_NEC98: /* NEC 98 pin */
989 trigger = default_NEC98_trigger(idx);
994 printk(KERN_WARNING "broken BIOS!!\n");
1006 case 2: /* reserved */
1008 printk(KERN_WARNING "broken BIOS!!\n");
1017 default: /* invalid */
1019 printk(KERN_WARNING "broken BIOS!!\n");
1027 static inline int irq_polarity(int idx)
1029 return MPBIOS_polarity(idx);
1032 static inline int irq_trigger(int idx)
1034 return MPBIOS_trigger(idx);
1037 static int pin_2_irq(int idx, int apic, int pin)
1040 int bus = mp_irqs[idx].mpc_srcbus;
1043 * Debugging check, we are in big trouble if this message pops up!
1045 if (mp_irqs[idx].mpc_dstirq != pin)
1046 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1048 switch (mp_bus_id_to_type[bus])
1050 case MP_BUS_ISA: /* ISA pin */
1055 irq = mp_irqs[idx].mpc_srcbusirq;
1058 case MP_BUS_PCI: /* PCI pin */
1061 * PCI IRQs are mapped in order
1065 irq += nr_ioapic_registers[i++];
1069 * For MPS mode, so far only needed by ES7000 platform
1071 if (ioapic_renumber_irq)
1072 irq = ioapic_renumber_irq(apic, irq);
1078 printk(KERN_ERR "unknown bus type %d.\n",bus);
1085 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1087 if ((pin >= 16) && (pin <= 23)) {
1088 if (pirq_entries[pin-16] != -1) {
1089 if (!pirq_entries[pin-16]) {
1090 apic_printk(APIC_VERBOSE, KERN_DEBUG
1091 "disabling PIRQ%d\n", pin-16);
1093 irq = pirq_entries[pin-16];
1094 apic_printk(APIC_VERBOSE, KERN_DEBUG
1095 "using PIRQ%d -> IRQ %d\n",
1103 static inline int IO_APIC_irq_trigger(int irq)
1107 for (apic = 0; apic < nr_ioapics; apic++) {
1108 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1109 idx = find_irq_entry(apic,pin,mp_INT);
1110 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
1111 return irq_trigger(idx);
1115 * nonexistent IRQs are edge default
1120 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
1121 u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
1123 int assign_irq_vector(int irq)
1125 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
1127 BUG_ON(irq >= NR_IRQ_VECTORS);
1128 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0)
1129 return IO_APIC_VECTOR(irq);
1131 current_vector += 8;
1132 if (current_vector == SYSCALL_VECTOR)
1135 if (current_vector >= FIRST_SYSTEM_VECTOR) {
1139 current_vector = FIRST_DEVICE_VECTOR + offset;
1142 vector_irq[current_vector] = irq;
1143 if (irq != AUTO_ASSIGN)
1144 IO_APIC_VECTOR(irq) = current_vector;
1146 return current_vector;
1149 static struct hw_interrupt_type ioapic_level_type;
1150 static struct hw_interrupt_type ioapic_edge_type;
1152 #define IOAPIC_AUTO -1
1153 #define IOAPIC_EDGE 0
1154 #define IOAPIC_LEVEL 1
1156 static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
1158 if (use_pci_vector() && !platform_legacy_irq(irq)) {
1159 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1160 trigger == IOAPIC_LEVEL)
1161 irq_desc[vector].handler = &ioapic_level_type;
1163 irq_desc[vector].handler = &ioapic_edge_type;
1164 set_intr_gate(vector, interrupt[vector]);
1166 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1167 trigger == IOAPIC_LEVEL)
1168 irq_desc[irq].handler = &ioapic_level_type;
1170 irq_desc[irq].handler = &ioapic_edge_type;
1171 set_intr_gate(vector, interrupt[irq]);
1175 static void __init setup_IO_APIC_irqs(void)
1177 struct IO_APIC_route_entry entry;
1178 int apic, pin, idx, irq, first_notcon = 1, vector;
1179 unsigned long flags;
1181 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1183 for (apic = 0; apic < nr_ioapics; apic++) {
1184 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1187 * add it to the IO-APIC irq-routing table:
1189 memset(&entry,0,sizeof(entry));
1191 entry.delivery_mode = INT_DELIVERY_MODE;
1192 entry.dest_mode = INT_DEST_MODE;
1193 entry.mask = 0; /* enable IRQ */
1194 entry.dest.logical.logical_dest =
1195 cpu_mask_to_apicid(TARGET_CPUS);
1197 idx = find_irq_entry(apic,pin,mp_INT);
1200 apic_printk(APIC_VERBOSE, KERN_DEBUG
1201 " IO-APIC (apicid-pin) %d-%d",
1202 mp_ioapics[apic].mpc_apicid,
1206 apic_printk(APIC_VERBOSE, ", %d-%d",
1207 mp_ioapics[apic].mpc_apicid, pin);
1211 entry.trigger = irq_trigger(idx);
1212 entry.polarity = irq_polarity(idx);
1214 if (irq_trigger(idx)) {
1219 irq = pin_2_irq(idx, apic, pin);
1221 * skip adding the timer int on secondary nodes, which causes
1222 * a small but painful rift in the time-space continuum
1224 if (multi_timer_check(apic, irq))
1227 add_pin_to_irq(irq, apic, pin);
1229 if (!apic && !IO_APIC_IRQ(irq))
1232 if (IO_APIC_IRQ(irq)) {
1233 vector = assign_irq_vector(irq);
1234 entry.vector = vector;
1235 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
1237 if (!apic && (irq < 16))
1238 disable_8259A_irq(irq);
1240 spin_lock_irqsave(&ioapic_lock, flags);
1241 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
1242 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
1243 set_native_irq_info(irq, TARGET_CPUS);
1244 spin_unlock_irqrestore(&ioapic_lock, flags);
1249 apic_printk(APIC_VERBOSE, " not connected.\n");
1253 * Set up the 8259A-master output pin:
1255 static void __init setup_ExtINT_IRQ0_pin(unsigned int pin, int vector)
1257 struct IO_APIC_route_entry entry;
1258 unsigned long flags;
1260 memset(&entry,0,sizeof(entry));
1262 disable_8259A_irq(0);
1265 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1268 * We use logical delivery to get the timer IRQ
1271 entry.dest_mode = INT_DEST_MODE;
1272 entry.mask = 0; /* unmask IRQ now */
1273 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1274 entry.delivery_mode = INT_DELIVERY_MODE;
1277 entry.vector = vector;
1280 * The timer IRQ doesn't have to know that behind the
1281 * scene we have a 8259A-master in AEOI mode ...
1283 irq_desc[0].handler = &ioapic_edge_type;
1286 * Add it to the IO-APIC irq-routing table:
1288 spin_lock_irqsave(&ioapic_lock, flags);
1289 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
1290 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
1291 spin_unlock_irqrestore(&ioapic_lock, flags);
1293 enable_8259A_irq(0);
1296 static inline void UNEXPECTED_IO_APIC(void)
1300 void __init print_IO_APIC(void)
1303 union IO_APIC_reg_00 reg_00;
1304 union IO_APIC_reg_01 reg_01;
1305 union IO_APIC_reg_02 reg_02;
1306 union IO_APIC_reg_03 reg_03;
1307 unsigned long flags;
1309 if (apic_verbosity == APIC_QUIET)
1312 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1313 for (i = 0; i < nr_ioapics; i++)
1314 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1315 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
1318 * We are a bit conservative about what we expect. We have to
1319 * know about every hardware change ASAP.
1321 printk(KERN_INFO "testing the IO APIC.......................\n");
1323 for (apic = 0; apic < nr_ioapics; apic++) {
1325 spin_lock_irqsave(&ioapic_lock, flags);
1326 reg_00.raw = io_apic_read(apic, 0);
1327 reg_01.raw = io_apic_read(apic, 1);
1328 if (reg_01.bits.version >= 0x10)
1329 reg_02.raw = io_apic_read(apic, 2);
1330 if (reg_01.bits.version >= 0x20)
1331 reg_03.raw = io_apic_read(apic, 3);
1332 spin_unlock_irqrestore(&ioapic_lock, flags);
1334 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1335 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1336 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1337 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1338 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1339 if (reg_00.bits.ID >= get_physical_broadcast())
1340 UNEXPECTED_IO_APIC();
1341 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
1342 UNEXPECTED_IO_APIC();
1344 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1345 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1346 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
1347 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
1348 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
1349 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1350 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1351 (reg_01.bits.entries != 0x2E) &&
1352 (reg_01.bits.entries != 0x3F)
1354 UNEXPECTED_IO_APIC();
1356 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1357 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1358 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1359 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1360 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1361 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1362 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1364 UNEXPECTED_IO_APIC();
1365 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1366 UNEXPECTED_IO_APIC();
1369 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1370 * but the value of reg_02 is read as the previous read register
1371 * value, so ignore it if reg_02 == reg_01.
1373 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1374 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1375 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1376 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1377 UNEXPECTED_IO_APIC();
1381 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1382 * or reg_03, but the value of reg_0[23] is read as the previous read
1383 * register value, so ignore it if reg_03 == reg_0[12].
1385 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1386 reg_03.raw != reg_01.raw) {
1387 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1388 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1389 if (reg_03.bits.__reserved_1)
1390 UNEXPECTED_IO_APIC();
1393 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1395 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1396 " Stat Dest Deli Vect: \n");
1398 for (i = 0; i <= reg_01.bits.entries; i++) {
1399 struct IO_APIC_route_entry entry;
1401 spin_lock_irqsave(&ioapic_lock, flags);
1402 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
1403 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
1404 spin_unlock_irqrestore(&ioapic_lock, flags);
1406 printk(KERN_DEBUG " %02x %03X %02X ",
1408 entry.dest.logical.logical_dest,
1409 entry.dest.physical.physical_dest
1412 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1417 entry.delivery_status,
1419 entry.delivery_mode,
1424 if (use_pci_vector())
1425 printk(KERN_INFO "Using vector-based indexing\n");
1426 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1427 for (i = 0; i < NR_IRQS; i++) {
1428 struct irq_pin_list *entry = irq_2_pin + i;
1431 if (use_pci_vector() && !platform_legacy_irq(i))
1432 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1434 printk(KERN_DEBUG "IRQ%d ", i);
1436 printk("-> %d:%d", entry->apic, entry->pin);
1439 entry = irq_2_pin + entry->next;
1444 printk(KERN_INFO ".................................... done.\n");
1451 static void print_APIC_bitfield (int base)
1456 if (apic_verbosity == APIC_QUIET)
1459 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1460 for (i = 0; i < 8; i++) {
1461 v = apic_read(base + i*0x10);
1462 for (j = 0; j < 32; j++) {
1472 void /*__init*/ print_local_APIC(void * dummy)
1474 unsigned int v, ver, maxlvt;
1476 if (apic_verbosity == APIC_QUIET)
1479 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1480 smp_processor_id(), hard_smp_processor_id());
1481 v = apic_read(APIC_ID);
1482 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1483 v = apic_read(APIC_LVR);
1484 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1485 ver = GET_APIC_VERSION(v);
1486 maxlvt = get_maxlvt();
1488 v = apic_read(APIC_TASKPRI);
1489 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1491 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1492 v = apic_read(APIC_ARBPRI);
1493 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1494 v & APIC_ARBPRI_MASK);
1495 v = apic_read(APIC_PROCPRI);
1496 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1499 v = apic_read(APIC_EOI);
1500 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1501 v = apic_read(APIC_RRR);
1502 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1503 v = apic_read(APIC_LDR);
1504 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1505 v = apic_read(APIC_DFR);
1506 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1507 v = apic_read(APIC_SPIV);
1508 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1510 printk(KERN_DEBUG "... APIC ISR field:\n");
1511 print_APIC_bitfield(APIC_ISR);
1512 printk(KERN_DEBUG "... APIC TMR field:\n");
1513 print_APIC_bitfield(APIC_TMR);
1514 printk(KERN_DEBUG "... APIC IRR field:\n");
1515 print_APIC_bitfield(APIC_IRR);
1517 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1518 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1519 apic_write(APIC_ESR, 0);
1520 v = apic_read(APIC_ESR);
1521 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1524 v = apic_read(APIC_ICR);
1525 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1526 v = apic_read(APIC_ICR2);
1527 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1529 v = apic_read(APIC_LVTT);
1530 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1532 if (maxlvt > 3) { /* PC is LVT#4. */
1533 v = apic_read(APIC_LVTPC);
1534 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1536 v = apic_read(APIC_LVT0);
1537 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1538 v = apic_read(APIC_LVT1);
1539 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1541 if (maxlvt > 2) { /* ERR is LVT#3. */
1542 v = apic_read(APIC_LVTERR);
1543 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1546 v = apic_read(APIC_TMICT);
1547 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1548 v = apic_read(APIC_TMCCT);
1549 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1550 v = apic_read(APIC_TDCR);
1551 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1555 void print_all_local_APICs (void)
1557 on_each_cpu(print_local_APIC, NULL, 1, 1);
1560 void /*__init*/ print_PIC(void)
1563 unsigned long flags;
1565 if (apic_verbosity == APIC_QUIET)
1568 printk(KERN_DEBUG "\nprinting PIC contents\n");
1570 spin_lock_irqsave(&i8259A_lock, flags);
1572 v = inb(0xa1) << 8 | inb(0x21);
1573 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1575 v = inb(0xa0) << 8 | inb(0x20);
1576 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1580 v = inb(0xa0) << 8 | inb(0x20);
1584 spin_unlock_irqrestore(&i8259A_lock, flags);
1586 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1588 v = inb(0x4d1) << 8 | inb(0x4d0);
1589 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1594 static void __init enable_IO_APIC(void)
1596 union IO_APIC_reg_01 reg_01;
1598 unsigned long flags;
1600 for (i = 0; i < PIN_MAP_SIZE; i++) {
1601 irq_2_pin[i].pin = -1;
1602 irq_2_pin[i].next = 0;
1605 for (i = 0; i < MAX_PIRQS; i++)
1606 pirq_entries[i] = -1;
1609 * The number of IO-APIC IRQ registers (== #pins):
1611 for (i = 0; i < nr_ioapics; i++) {
1612 spin_lock_irqsave(&ioapic_lock, flags);
1613 reg_01.raw = io_apic_read(i, 1);
1614 spin_unlock_irqrestore(&ioapic_lock, flags);
1615 nr_ioapic_registers[i] = reg_01.bits.entries+1;
1619 * Do not trust the IO-APIC being empty at bootup
1625 * Not an __init, needed by the reboot code
1627 void disable_IO_APIC(void)
1631 * Clear the IO-APIC before rebooting:
1636 * If the i8259 is routed through an IOAPIC
1637 * Put that IOAPIC in virtual wire mode
1638 * so legacy interrupts can be delivered.
1640 pin = find_isa_irq_pin(0, mp_ExtINT);
1642 struct IO_APIC_route_entry entry;
1643 unsigned long flags;
1645 memset(&entry, 0, sizeof(entry));
1646 entry.mask = 0; /* Enabled */
1647 entry.trigger = 0; /* Edge */
1649 entry.polarity = 0; /* High */
1650 entry.delivery_status = 0;
1651 entry.dest_mode = 0; /* Physical */
1652 entry.delivery_mode = 7; /* ExtInt */
1654 entry.dest.physical.physical_dest = 0;
1658 * Add it to the IO-APIC irq-routing table:
1660 spin_lock_irqsave(&ioapic_lock, flags);
1661 io_apic_write(0, 0x11+2*pin, *(((int *)&entry)+1));
1662 io_apic_write(0, 0x10+2*pin, *(((int *)&entry)+0));
1663 spin_unlock_irqrestore(&ioapic_lock, flags);
1665 disconnect_bsp_APIC(pin != -1);
1669 * function to set the IO-APIC physical IDs based on the
1670 * values stored in the MPC table.
1672 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1675 #ifndef CONFIG_X86_NUMAQ
1676 static void __init setup_ioapic_ids_from_mpc(void)
1678 union IO_APIC_reg_00 reg_00;
1679 physid_mask_t phys_id_present_map;
1682 unsigned char old_id;
1683 unsigned long flags;
1686 * Don't check I/O APIC IDs for xAPIC systems. They have
1687 * no meaning without the serial APIC bus.
1689 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL && boot_cpu_data.x86 < 15))
1692 * This is broken; anything with a real cpu count has to
1693 * circumvent this idiocy regardless.
1695 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1698 * Set the IOAPIC ID to the value stored in the MPC table.
1700 for (apic = 0; apic < nr_ioapics; apic++) {
1702 /* Read the register 0 value */
1703 spin_lock_irqsave(&ioapic_lock, flags);
1704 reg_00.raw = io_apic_read(apic, 0);
1705 spin_unlock_irqrestore(&ioapic_lock, flags);
1707 old_id = mp_ioapics[apic].mpc_apicid;
1709 if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
1710 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1711 apic, mp_ioapics[apic].mpc_apicid);
1712 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1714 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1718 * Sanity check, is the ID really free? Every APIC in a
1719 * system must have a unique ID or we get lots of nice
1720 * 'stuck on smp_invalidate_needed IPI wait' messages.
1722 if (check_apicid_used(phys_id_present_map,
1723 mp_ioapics[apic].mpc_apicid)) {
1724 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1725 apic, mp_ioapics[apic].mpc_apicid);
1726 for (i = 0; i < get_physical_broadcast(); i++)
1727 if (!physid_isset(i, phys_id_present_map))
1729 if (i >= get_physical_broadcast())
1730 panic("Max APIC ID exceeded!\n");
1731 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1733 physid_set(i, phys_id_present_map);
1734 mp_ioapics[apic].mpc_apicid = i;
1737 tmp = apicid_to_cpu_present(mp_ioapics[apic].mpc_apicid);
1738 apic_printk(APIC_VERBOSE, "Setting %d in the "
1739 "phys_id_present_map\n",
1740 mp_ioapics[apic].mpc_apicid);
1741 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1746 * We need to adjust the IRQ routing table
1747 * if the ID changed.
1749 if (old_id != mp_ioapics[apic].mpc_apicid)
1750 for (i = 0; i < mp_irq_entries; i++)
1751 if (mp_irqs[i].mpc_dstapic == old_id)
1752 mp_irqs[i].mpc_dstapic
1753 = mp_ioapics[apic].mpc_apicid;
1756 * Read the right value from the MPC table and
1757 * write it into the ID register.
1759 apic_printk(APIC_VERBOSE, KERN_INFO
1760 "...changing IO-APIC physical APIC ID to %d ...",
1761 mp_ioapics[apic].mpc_apicid);
1763 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1764 spin_lock_irqsave(&ioapic_lock, flags);
1765 io_apic_write(apic, 0, reg_00.raw);
1766 spin_unlock_irqrestore(&ioapic_lock, flags);
1771 spin_lock_irqsave(&ioapic_lock, flags);
1772 reg_00.raw = io_apic_read(apic, 0);
1773 spin_unlock_irqrestore(&ioapic_lock, flags);
1774 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1775 printk("could not set ID!\n");
1777 apic_printk(APIC_VERBOSE, " ok.\n");
1781 static void __init setup_ioapic_ids_from_mpc(void) { }
1785 * There is a nasty bug in some older SMP boards, their mptable lies
1786 * about the timer IRQ. We do the following to work around the situation:
1788 * - timer IRQ defaults to IO-APIC IRQ
1789 * - if this function detects that timer IRQs are defunct, then we fall
1790 * back to ISA timer IRQs
1792 static int __init timer_irq_works(void)
1794 unsigned long t1 = jiffies;
1797 /* Let ten ticks pass... */
1798 mdelay((10 * 1000) / HZ);
1801 * Expect a few ticks at least, to be sure some possible
1802 * glue logic does not lock up after one or two first
1803 * ticks in a non-ExtINT mode. Also the local APIC
1804 * might have cached one ExtINT interrupt. Finally, at
1805 * least one tick may be lost due to delays.
1807 if (jiffies - t1 > 4)
1814 * In the SMP+IOAPIC case it might happen that there are an unspecified
1815 * number of pending IRQ events unhandled. These cases are very rare,
1816 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1817 * better to do it this way as thus we do not have to be aware of
1818 * 'pending' interrupts in the IRQ path, except at this point.
1821 * Edge triggered needs to resend any interrupt
1822 * that was delayed but this is now handled in the device
1827 * Starting up a edge-triggered IO-APIC interrupt is
1828 * nasty - we need to make sure that we get the edge.
1829 * If it is already asserted for some reason, we need
1830 * return 1 to indicate that is was pending.
1832 * This is not complete - we should be able to fake
1833 * an edge even if it isn't on the 8259A...
1835 static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1837 int was_pending = 0;
1838 unsigned long flags;
1840 spin_lock_irqsave(&ioapic_lock, flags);
1842 disable_8259A_irq(irq);
1843 if (i8259A_irq_pending(irq))
1846 __unmask_IO_APIC_irq(irq);
1847 spin_unlock_irqrestore(&ioapic_lock, flags);
1853 * Once we have recorded IRQ_PENDING already, we can mask the
1854 * interrupt for real. This prevents IRQ storms from unhandled
1857 static void ack_edge_ioapic_irq(unsigned int irq)
1860 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1861 == (IRQ_PENDING | IRQ_DISABLED))
1862 mask_IO_APIC_irq(irq);
1867 * Level triggered interrupts can just be masked,
1868 * and shutting down and starting up the interrupt
1869 * is the same as enabling and disabling them -- except
1870 * with a startup need to return a "was pending" value.
1872 * Level triggered interrupts are special because we
1873 * do not touch any IO-APIC register while handling
1874 * them. We ack the APIC in the end-IRQ handler, not
1875 * in the start-IRQ-handler. Protection against reentrance
1876 * from the same interrupt is still provided, both by the
1877 * generic IRQ layer and by the fact that an unacked local
1878 * APIC does not accept IRQs.
1880 static unsigned int startup_level_ioapic_irq (unsigned int irq)
1882 unmask_IO_APIC_irq(irq);
1884 return 0; /* don't check for pending */
1887 static void end_level_ioapic_irq (unsigned int irq)
1894 * It appears there is an erratum which affects at least version 0x11
1895 * of I/O APIC (that's the 82093AA and cores integrated into various
1896 * chipsets). Under certain conditions a level-triggered interrupt is
1897 * erroneously delivered as edge-triggered one but the respective IRR
1898 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1899 * message but it will never arrive and further interrupts are blocked
1900 * from the source. The exact reason is so far unknown, but the
1901 * phenomenon was observed when two consecutive interrupt requests
1902 * from a given source get delivered to the same CPU and the source is
1903 * temporarily disabled in between.
1905 * A workaround is to simulate an EOI message manually. We achieve it
1906 * by setting the trigger mode to edge and then to level when the edge
1907 * trigger mode gets detected in the TMR of a local APIC for a
1908 * level-triggered interrupt. We mask the source for the time of the
1909 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1910 * The idea is from Manfred Spraul. --macro
1912 i = IO_APIC_VECTOR(irq);
1914 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1918 if (!(v & (1 << (i & 0x1f)))) {
1919 atomic_inc(&irq_mis_count);
1920 spin_lock(&ioapic_lock);
1921 __mask_and_edge_IO_APIC_irq(irq);
1922 __unmask_and_level_IO_APIC_irq(irq);
1923 spin_unlock(&ioapic_lock);
1927 #ifdef CONFIG_PCI_MSI
1928 static unsigned int startup_edge_ioapic_vector(unsigned int vector)
1930 int irq = vector_to_irq(vector);
1932 return startup_edge_ioapic_irq(irq);
1935 static void ack_edge_ioapic_vector(unsigned int vector)
1937 int irq = vector_to_irq(vector);
1940 ack_edge_ioapic_irq(irq);
1943 static unsigned int startup_level_ioapic_vector (unsigned int vector)
1945 int irq = vector_to_irq(vector);
1947 return startup_level_ioapic_irq (irq);
1950 static void end_level_ioapic_vector (unsigned int vector)
1952 int irq = vector_to_irq(vector);
1955 end_level_ioapic_irq(irq);
1958 static void mask_IO_APIC_vector (unsigned int vector)
1960 int irq = vector_to_irq(vector);
1962 mask_IO_APIC_irq(irq);
1965 static void unmask_IO_APIC_vector (unsigned int vector)
1967 int irq = vector_to_irq(vector);
1969 unmask_IO_APIC_irq(irq);
1973 static void set_ioapic_affinity_vector (unsigned int vector,
1976 int irq = vector_to_irq(vector);
1978 set_native_irq_info(vector, cpu_mask);
1979 set_ioapic_affinity_irq(irq, cpu_mask);
1985 * Level and edge triggered IO-APIC interrupts need different handling,
1986 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1987 * handled with the level-triggered descriptor, but that one has slightly
1988 * more overhead. Level-triggered interrupts cannot be handled with the
1989 * edge-triggered handler, without risking IRQ storms and other ugly
1992 static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1993 .typename = "IO-APIC-edge",
1994 .startup = startup_edge_ioapic,
1995 .shutdown = shutdown_edge_ioapic,
1996 .enable = enable_edge_ioapic,
1997 .disable = disable_edge_ioapic,
1998 .ack = ack_edge_ioapic,
1999 .end = end_edge_ioapic,
2001 .set_affinity = set_ioapic_affinity,
2005 static struct hw_interrupt_type ioapic_level_type __read_mostly = {
2006 .typename = "IO-APIC-level",
2007 .startup = startup_level_ioapic,
2008 .shutdown = shutdown_level_ioapic,
2009 .enable = enable_level_ioapic,
2010 .disable = disable_level_ioapic,
2011 .ack = mask_and_ack_level_ioapic,
2012 .end = end_level_ioapic,
2014 .set_affinity = set_ioapic_affinity,
2018 static inline void init_IO_APIC_traps(void)
2023 * NOTE! The local APIC isn't very good at handling
2024 * multiple interrupts at the same interrupt level.
2025 * As the interrupt level is determined by taking the
2026 * vector number and shifting that right by 4, we
2027 * want to spread these out a bit so that they don't
2028 * all fall in the same interrupt level.
2030 * Also, we've got to be careful not to trash gate
2031 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2033 for (irq = 0; irq < NR_IRQS ; irq++) {
2035 if (use_pci_vector()) {
2036 if (!platform_legacy_irq(tmp))
2037 if ((tmp = vector_to_irq(tmp)) == -1)
2040 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
2042 * Hmm.. We don't have an entry for this,
2043 * so default to an old-fashioned 8259
2044 * interrupt if we can..
2047 make_8259A_irq(irq);
2049 /* Strange. Oh, well.. */
2050 irq_desc[irq].handler = &no_irq_type;
2055 static void enable_lapic_irq (unsigned int irq)
2059 v = apic_read(APIC_LVT0);
2060 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
2063 static void disable_lapic_irq (unsigned int irq)
2067 v = apic_read(APIC_LVT0);
2068 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
2071 static void ack_lapic_irq (unsigned int irq)
2076 static void end_lapic_irq (unsigned int i) { /* nothing */ }
2078 static struct hw_interrupt_type lapic_irq_type __read_mostly = {
2079 .typename = "local-APIC-edge",
2080 .startup = NULL, /* startup_irq() not used for IRQ0 */
2081 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
2082 .enable = enable_lapic_irq,
2083 .disable = disable_lapic_irq,
2084 .ack = ack_lapic_irq,
2085 .end = end_lapic_irq
2088 static void setup_nmi (void)
2091 * Dirty trick to enable the NMI watchdog ...
2092 * We put the 8259A master into AEOI mode and
2093 * unmask on all local APICs LVT0 as NMI.
2095 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2096 * is from Maciej W. Rozycki - so we do not have to EOI from
2097 * the NMI handler or the timer interrupt.
2099 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2101 on_each_cpu(enable_NMI_through_LVT0, NULL, 1, 1);
2103 apic_printk(APIC_VERBOSE, " done.\n");
2107 * This looks a bit hackish but it's about the only one way of sending
2108 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2109 * not support the ExtINT mode, unfortunately. We need to send these
2110 * cycles as some i82489DX-based boards have glue logic that keeps the
2111 * 8259A interrupt line asserted until INTA. --macro
2113 static inline void unlock_ExtINT_logic(void)
2116 struct IO_APIC_route_entry entry0, entry1;
2117 unsigned char save_control, save_freq_select;
2118 unsigned long flags;
2120 pin = find_isa_irq_pin(8, mp_INT);
2124 spin_lock_irqsave(&ioapic_lock, flags);
2125 *(((int *)&entry0) + 1) = io_apic_read(0, 0x11 + 2 * pin);
2126 *(((int *)&entry0) + 0) = io_apic_read(0, 0x10 + 2 * pin);
2127 spin_unlock_irqrestore(&ioapic_lock, flags);
2128 clear_IO_APIC_pin(0, pin);
2130 memset(&entry1, 0, sizeof(entry1));
2132 entry1.dest_mode = 0; /* physical delivery */
2133 entry1.mask = 0; /* unmask IRQ now */
2134 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2135 entry1.delivery_mode = dest_ExtINT;
2136 entry1.polarity = entry0.polarity;
2140 spin_lock_irqsave(&ioapic_lock, flags);
2141 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
2142 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
2143 spin_unlock_irqrestore(&ioapic_lock, flags);
2145 save_control = CMOS_READ(RTC_CONTROL);
2146 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2147 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2149 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2154 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2158 CMOS_WRITE(save_control, RTC_CONTROL);
2159 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2160 clear_IO_APIC_pin(0, pin);
2162 spin_lock_irqsave(&ioapic_lock, flags);
2163 io_apic_write(0, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
2164 io_apic_write(0, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
2165 spin_unlock_irqrestore(&ioapic_lock, flags);
2169 * This code may look a bit paranoid, but it's supposed to cooperate with
2170 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2171 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2172 * fanatically on his truly buggy board.
2174 static inline void check_timer(void)
2180 * get/set the timer IRQ vector:
2182 disable_8259A_irq(0);
2183 vector = assign_irq_vector(0);
2184 set_intr_gate(vector, interrupt[0]);
2187 * Subtle, code in do_timer_interrupt() expects an AEOI
2188 * mode for the 8259A whenever interrupts are routed
2189 * through I/O APICs. Also IRQ0 has to be enabled in
2190 * the 8259A which implies the virtual wire has to be
2191 * disabled in the local APIC.
2193 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2196 enable_8259A_irq(0);
2198 pin1 = find_isa_irq_pin(0, mp_INT);
2199 pin2 = find_isa_irq_pin(0, mp_ExtINT);
2201 printk(KERN_INFO "..TIMER: vector=0x%02X pin1=%d pin2=%d\n", vector, pin1, pin2);
2205 * Ok, does IRQ0 through the IOAPIC work?
2207 unmask_IO_APIC_irq(0);
2208 if (timer_irq_works()) {
2209 if (nmi_watchdog == NMI_IO_APIC) {
2210 disable_8259A_irq(0);
2212 enable_8259A_irq(0);
2216 clear_IO_APIC_pin(0, pin1);
2217 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to IO-APIC\n");
2220 printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
2222 printk("\n..... (found pin %d) ...", pin2);
2224 * legacy devices should be connected to IO APIC #0
2226 setup_ExtINT_IRQ0_pin(pin2, vector);
2227 if (timer_irq_works()) {
2230 replace_pin_at_irq(0, 0, pin1, 0, pin2);
2232 add_pin_to_irq(0, 0, pin2);
2233 if (nmi_watchdog == NMI_IO_APIC) {
2239 * Cleanup, just in case ...
2241 clear_IO_APIC_pin(0, pin2);
2243 printk(" failed.\n");
2245 if (nmi_watchdog == NMI_IO_APIC) {
2246 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
2250 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2252 disable_8259A_irq(0);
2253 irq_desc[0].handler = &lapic_irq_type;
2254 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
2255 enable_8259A_irq(0);
2257 if (timer_irq_works()) {
2258 printk(" works.\n");
2261 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
2262 printk(" failed.\n");
2264 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2269 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
2271 unlock_ExtINT_logic();
2273 if (timer_irq_works()) {
2274 printk(" works.\n");
2277 printk(" failed :(.\n");
2278 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2279 "report. Then try booting with the 'noapic' option");
2284 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2285 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2286 * Linux doesn't really care, as it's not actually used
2287 * for any interrupt handling anyway.
2289 #define PIC_IRQS (1 << PIC_CASCADE_IR)
2291 void __init setup_IO_APIC(void)
2296 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2298 io_apic_irqs = ~PIC_IRQS;
2300 printk("ENABLING IO-APIC IRQs\n");
2303 * Set up IO-APIC IRQ routing.
2306 setup_ioapic_ids_from_mpc();
2308 setup_IO_APIC_irqs();
2309 init_IO_APIC_traps();
2316 * Called after all the initialization is done. If we didnt find any
2317 * APIC bugs then we can allow the modify fast path
2320 static int __init io_apic_bug_finalize(void)
2322 if(sis_apic_bug == -1)
2327 late_initcall(io_apic_bug_finalize);
2329 struct sysfs_ioapic_data {
2330 struct sys_device dev;
2331 struct IO_APIC_route_entry entry[0];
2333 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2335 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2337 struct IO_APIC_route_entry *entry;
2338 struct sysfs_ioapic_data *data;
2339 unsigned long flags;
2342 data = container_of(dev, struct sysfs_ioapic_data, dev);
2343 entry = data->entry;
2344 spin_lock_irqsave(&ioapic_lock, flags);
2345 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
2346 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
2347 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
2349 spin_unlock_irqrestore(&ioapic_lock, flags);
2354 static int ioapic_resume(struct sys_device *dev)
2356 struct IO_APIC_route_entry *entry;
2357 struct sysfs_ioapic_data *data;
2358 unsigned long flags;
2359 union IO_APIC_reg_00 reg_00;
2362 data = container_of(dev, struct sysfs_ioapic_data, dev);
2363 entry = data->entry;
2365 spin_lock_irqsave(&ioapic_lock, flags);
2366 reg_00.raw = io_apic_read(dev->id, 0);
2367 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
2368 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
2369 io_apic_write(dev->id, 0, reg_00.raw);
2371 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
2372 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
2373 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
2375 spin_unlock_irqrestore(&ioapic_lock, flags);
2380 static struct sysdev_class ioapic_sysdev_class = {
2381 set_kset_name("ioapic"),
2382 .suspend = ioapic_suspend,
2383 .resume = ioapic_resume,
2386 static int __init ioapic_init_sysfs(void)
2388 struct sys_device * dev;
2389 int i, size, error = 0;
2391 error = sysdev_class_register(&ioapic_sysdev_class);
2395 for (i = 0; i < nr_ioapics; i++ ) {
2396 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2397 * sizeof(struct IO_APIC_route_entry);
2398 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
2399 if (!mp_ioapic_data[i]) {
2400 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2403 memset(mp_ioapic_data[i], 0, size);
2404 dev = &mp_ioapic_data[i]->dev;
2406 dev->cls = &ioapic_sysdev_class;
2407 error = sysdev_register(dev);
2409 kfree(mp_ioapic_data[i]);
2410 mp_ioapic_data[i] = NULL;
2411 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2419 device_initcall(ioapic_init_sysfs);
2421 /* --------------------------------------------------------------------------
2422 ACPI-based IOAPIC Configuration
2423 -------------------------------------------------------------------------- */
2427 int __init io_apic_get_unique_id (int ioapic, int apic_id)
2429 union IO_APIC_reg_00 reg_00;
2430 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2432 unsigned long flags;
2436 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2437 * buses (one for LAPICs, one for IOAPICs), where predecessors only
2438 * supports up to 16 on one shared APIC bus.
2440 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2441 * advantage of new APIC bus architecture.
2444 if (physids_empty(apic_id_map))
2445 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2447 spin_lock_irqsave(&ioapic_lock, flags);
2448 reg_00.raw = io_apic_read(ioapic, 0);
2449 spin_unlock_irqrestore(&ioapic_lock, flags);
2451 if (apic_id >= get_physical_broadcast()) {
2452 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2453 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2454 apic_id = reg_00.bits.ID;
2458 * Every APIC in a system must have a unique ID or we get lots of nice
2459 * 'stuck on smp_invalidate_needed IPI wait' messages.
2461 if (check_apicid_used(apic_id_map, apic_id)) {
2463 for (i = 0; i < get_physical_broadcast(); i++) {
2464 if (!check_apicid_used(apic_id_map, i))
2468 if (i == get_physical_broadcast())
2469 panic("Max apic_id exceeded!\n");
2471 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2472 "trying %d\n", ioapic, apic_id, i);
2477 tmp = apicid_to_cpu_present(apic_id);
2478 physids_or(apic_id_map, apic_id_map, tmp);
2480 if (reg_00.bits.ID != apic_id) {
2481 reg_00.bits.ID = apic_id;
2483 spin_lock_irqsave(&ioapic_lock, flags);
2484 io_apic_write(ioapic, 0, reg_00.raw);
2485 reg_00.raw = io_apic_read(ioapic, 0);
2486 spin_unlock_irqrestore(&ioapic_lock, flags);
2489 if (reg_00.bits.ID != apic_id)
2490 panic("IOAPIC[%d]: Unable change apic_id!\n", ioapic);
2493 apic_printk(APIC_VERBOSE, KERN_INFO
2494 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2500 int __init io_apic_get_version (int ioapic)
2502 union IO_APIC_reg_01 reg_01;
2503 unsigned long flags;
2505 spin_lock_irqsave(&ioapic_lock, flags);
2506 reg_01.raw = io_apic_read(ioapic, 1);
2507 spin_unlock_irqrestore(&ioapic_lock, flags);
2509 return reg_01.bits.version;
2513 int __init io_apic_get_redir_entries (int ioapic)
2515 union IO_APIC_reg_01 reg_01;
2516 unsigned long flags;
2518 spin_lock_irqsave(&ioapic_lock, flags);
2519 reg_01.raw = io_apic_read(ioapic, 1);
2520 spin_unlock_irqrestore(&ioapic_lock, flags);
2522 return reg_01.bits.entries;
2526 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2528 struct IO_APIC_route_entry entry;
2529 unsigned long flags;
2531 if (!IO_APIC_IRQ(irq)) {
2532 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2538 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2539 * Note that we mask (disable) IRQs now -- these get enabled when the
2540 * corresponding device driver registers for this IRQ.
2543 memset(&entry,0,sizeof(entry));
2545 entry.delivery_mode = INT_DELIVERY_MODE;
2546 entry.dest_mode = INT_DEST_MODE;
2547 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2548 entry.trigger = edge_level;
2549 entry.polarity = active_high_low;
2553 * IRQs < 16 are already in the irq_2_pin[] map
2556 add_pin_to_irq(irq, ioapic, pin);
2558 entry.vector = assign_irq_vector(irq);
2560 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2561 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
2562 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2563 edge_level, active_high_low);
2565 ioapic_register_intr(irq, entry.vector, edge_level);
2567 if (!ioapic && (irq < 16))
2568 disable_8259A_irq(irq);
2570 spin_lock_irqsave(&ioapic_lock, flags);
2571 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2572 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
2573 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
2574 spin_unlock_irqrestore(&ioapic_lock, flags);
2579 #endif /* CONFIG_ACPI */