x86, arch/x86/kernel/io_apic_32.c: use kzalloc instead of kmalloc/memset
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / io_apic_32.c
blobe22cbfbce04908af00add4f59b5ab08bf4340353
1 /*
2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/mc146818rtc.h>
29 #include <linux/compiler.h>
30 #include <linux/acpi.h>
31 #include <linux/module.h>
32 #include <linux/sysdev.h>
33 #include <linux/pci.h>
34 #include <linux/msi.h>
35 #include <linux/htirq.h>
36 #include <linux/freezer.h>
37 #include <linux/kthread.h>
38 #include <linux/jiffies.h> /* time_after() */
40 #include <asm/io.h>
41 #include <asm/smp.h>
42 #include <asm/desc.h>
43 #include <asm/timer.h>
44 #include <asm/i8259.h>
45 #include <asm/nmi.h>
46 #include <asm/msidef.h>
47 #include <asm/hypertransport.h>
49 #include <mach_apic.h>
50 #include <mach_apicdef.h>
52 int (*ioapic_renumber_irq)(int ioapic, int irq);
53 atomic_t irq_mis_count;
55 /* Where if anywhere is the i8259 connect in external int mode */
56 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
58 static DEFINE_SPINLOCK(ioapic_lock);
59 static DEFINE_SPINLOCK(vector_lock);
61 int timer_through_8259 __initdata;
64 * Is the SiS APIC rmw bug present ?
65 * -1 = don't know, 0 = no, 1 = yes
67 int sis_apic_bug = -1;
70 * # of IRQ routing registers
72 int nr_ioapic_registers[MAX_IO_APICS];
74 /* I/O APIC entries */
75 struct mpc_config_ioapic mp_ioapics[MAX_IO_APICS];
76 int nr_ioapics;
78 /* MP IRQ source entries */
79 struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
81 /* # of MP IRQ source entries */
82 int mp_irq_entries;
84 static int disable_timer_pin_1 __initdata;
87 * Rough estimation of how many shared IRQs there are, can
88 * be changed anytime.
90 #define MAX_PLUS_SHARED_IRQS NR_IRQS
91 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
94 * This is performance-critical, we want to do it O(1)
96 * the indexing order of this array favors 1:1 mappings
97 * between pins and IRQs.
100 static struct irq_pin_list {
101 int apic, pin, next;
102 } irq_2_pin[PIN_MAP_SIZE];
104 struct io_apic {
105 unsigned int index;
106 unsigned int unused[3];
107 unsigned int data;
110 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
112 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
113 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
116 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
118 struct io_apic __iomem *io_apic = io_apic_base(apic);
119 writel(reg, &io_apic->index);
120 return readl(&io_apic->data);
123 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
125 struct io_apic __iomem *io_apic = io_apic_base(apic);
126 writel(reg, &io_apic->index);
127 writel(value, &io_apic->data);
131 * Re-write a value: to be used for read-modify-write
132 * cycles where the read already set up the index register.
134 * Older SiS APIC requires we rewrite the index register
136 static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
138 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
139 if (sis_apic_bug)
140 writel(reg, &io_apic->index);
141 writel(value, &io_apic->data);
144 union entry_union {
145 struct { u32 w1, w2; };
146 struct IO_APIC_route_entry entry;
149 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
151 union entry_union eu;
152 unsigned long flags;
153 spin_lock_irqsave(&ioapic_lock, flags);
154 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
155 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
156 spin_unlock_irqrestore(&ioapic_lock, flags);
157 return eu.entry;
161 * When we write a new IO APIC routing entry, we need to write the high
162 * word first! If the mask bit in the low word is clear, we will enable
163 * the interrupt, and we need to make sure the entry is fully populated
164 * before that happens.
166 static void
167 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
169 union entry_union eu;
170 eu.entry = e;
171 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
172 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
175 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
177 unsigned long flags;
178 spin_lock_irqsave(&ioapic_lock, flags);
179 __ioapic_write_entry(apic, pin, e);
180 spin_unlock_irqrestore(&ioapic_lock, flags);
184 * When we mask an IO APIC routing entry, we need to write the low
185 * word first, in order to set the mask bit before we change the
186 * high bits!
188 static void ioapic_mask_entry(int apic, int pin)
190 unsigned long flags;
191 union entry_union eu = { .entry.mask = 1 };
193 spin_lock_irqsave(&ioapic_lock, flags);
194 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
195 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
196 spin_unlock_irqrestore(&ioapic_lock, flags);
200 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
201 * shared ISA-space IRQs, so we have to support them. We are super
202 * fast in the common case, and fast for shared ISA-space IRQs.
204 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
206 static int first_free_entry = NR_IRQS;
207 struct irq_pin_list *entry = irq_2_pin + irq;
209 while (entry->next)
210 entry = irq_2_pin + entry->next;
212 if (entry->pin != -1) {
213 entry->next = first_free_entry;
214 entry = irq_2_pin + entry->next;
215 if (++first_free_entry >= PIN_MAP_SIZE)
216 panic("io_apic.c: whoops");
218 entry->apic = apic;
219 entry->pin = pin;
223 * Reroute an IRQ to a different pin.
225 static void __init replace_pin_at_irq(unsigned int irq,
226 int oldapic, int oldpin,
227 int newapic, int newpin)
229 struct irq_pin_list *entry = irq_2_pin + irq;
231 while (1) {
232 if (entry->apic == oldapic && entry->pin == oldpin) {
233 entry->apic = newapic;
234 entry->pin = newpin;
236 if (!entry->next)
237 break;
238 entry = irq_2_pin + entry->next;
242 static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
244 struct irq_pin_list *entry = irq_2_pin + irq;
245 unsigned int pin, reg;
247 for (;;) {
248 pin = entry->pin;
249 if (pin == -1)
250 break;
251 reg = io_apic_read(entry->apic, 0x10 + pin*2);
252 reg &= ~disable;
253 reg |= enable;
254 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
255 if (!entry->next)
256 break;
257 entry = irq_2_pin + entry->next;
261 /* mask = 1 */
262 static void __mask_IO_APIC_irq(unsigned int irq)
264 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
267 /* mask = 0 */
268 static void __unmask_IO_APIC_irq(unsigned int irq)
270 __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
273 /* mask = 1, trigger = 0 */
274 static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
276 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
277 IO_APIC_REDIR_LEVEL_TRIGGER);
280 /* mask = 0, trigger = 1 */
281 static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
283 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
284 IO_APIC_REDIR_MASKED);
287 static void mask_IO_APIC_irq(unsigned int irq)
289 unsigned long flags;
291 spin_lock_irqsave(&ioapic_lock, flags);
292 __mask_IO_APIC_irq(irq);
293 spin_unlock_irqrestore(&ioapic_lock, flags);
296 static void unmask_IO_APIC_irq(unsigned int irq)
298 unsigned long flags;
300 spin_lock_irqsave(&ioapic_lock, flags);
301 __unmask_IO_APIC_irq(irq);
302 spin_unlock_irqrestore(&ioapic_lock, flags);
305 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
307 struct IO_APIC_route_entry entry;
309 /* Check delivery_mode to be sure we're not clearing an SMI pin */
310 entry = ioapic_read_entry(apic, pin);
311 if (entry.delivery_mode == dest_SMI)
312 return;
315 * Disable it in the IO-APIC irq-routing table:
317 ioapic_mask_entry(apic, pin);
320 static void clear_IO_APIC(void)
322 int apic, pin;
324 for (apic = 0; apic < nr_ioapics; apic++)
325 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
326 clear_IO_APIC_pin(apic, pin);
329 #ifdef CONFIG_SMP
330 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
332 unsigned long flags;
333 int pin;
334 struct irq_pin_list *entry = irq_2_pin + irq;
335 unsigned int apicid_value;
336 cpumask_t tmp;
338 cpus_and(tmp, cpumask, cpu_online_map);
339 if (cpus_empty(tmp))
340 tmp = TARGET_CPUS;
342 cpus_and(cpumask, tmp, CPU_MASK_ALL);
344 apicid_value = cpu_mask_to_apicid(cpumask);
345 /* Prepare to do the io_apic_write */
346 apicid_value = apicid_value << 24;
347 spin_lock_irqsave(&ioapic_lock, flags);
348 for (;;) {
349 pin = entry->pin;
350 if (pin == -1)
351 break;
352 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
353 if (!entry->next)
354 break;
355 entry = irq_2_pin + entry->next;
357 irq_desc[irq].affinity = cpumask;
358 spin_unlock_irqrestore(&ioapic_lock, flags);
361 #if defined(CONFIG_IRQBALANCE)
362 # include <asm/processor.h> /* kernel_thread() */
363 # include <linux/kernel_stat.h> /* kstat */
364 # include <linux/slab.h> /* kmalloc() */
365 # include <linux/timer.h>
367 #define IRQBALANCE_CHECK_ARCH -999
368 #define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
369 #define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
370 #define BALANCED_IRQ_MORE_DELTA (HZ/10)
371 #define BALANCED_IRQ_LESS_DELTA (HZ)
373 static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
374 static int physical_balance __read_mostly;
375 static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
377 static struct irq_cpu_info {
378 unsigned long *last_irq;
379 unsigned long *irq_delta;
380 unsigned long irq;
381 } irq_cpu_data[NR_CPUS];
383 #define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
384 #define LAST_CPU_IRQ(cpu, irq) (irq_cpu_data[cpu].last_irq[irq])
385 #define IRQ_DELTA(cpu, irq) (irq_cpu_data[cpu].irq_delta[irq])
387 #define IDLE_ENOUGH(cpu,now) \
388 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
390 #define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
392 #define CPU_TO_PACKAGEINDEX(i) (first_cpu(per_cpu(cpu_sibling_map, i)))
394 static cpumask_t balance_irq_affinity[NR_IRQS] = {
395 [0 ... NR_IRQS-1] = CPU_MASK_ALL
398 void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
400 balance_irq_affinity[irq] = mask;
403 static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
404 unsigned long now, int direction)
406 int search_idle = 1;
407 int cpu = curr_cpu;
409 goto inside;
411 do {
412 if (unlikely(cpu == curr_cpu))
413 search_idle = 0;
414 inside:
415 if (direction == 1) {
416 cpu++;
417 if (cpu >= NR_CPUS)
418 cpu = 0;
419 } else {
420 cpu--;
421 if (cpu == -1)
422 cpu = NR_CPUS-1;
424 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu, allowed_mask) ||
425 (search_idle && !IDLE_ENOUGH(cpu, now)));
427 return cpu;
430 static inline void balance_irq(int cpu, int irq)
432 unsigned long now = jiffies;
433 cpumask_t allowed_mask;
434 unsigned int new_cpu;
436 if (irqbalance_disabled)
437 return;
439 cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
440 new_cpu = move(cpu, allowed_mask, now, 1);
441 if (cpu != new_cpu)
442 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
445 static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
447 int i, j;
449 for_each_online_cpu(i) {
450 for (j = 0; j < NR_IRQS; j++) {
451 if (!irq_desc[j].action)
452 continue;
453 /* Is it a significant load ? */
454 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i), j) <
455 useful_load_threshold)
456 continue;
457 balance_irq(i, j);
460 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
461 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
462 return;
465 static void do_irq_balance(void)
467 int i, j;
468 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
469 unsigned long move_this_load = 0;
470 int max_loaded = 0, min_loaded = 0;
471 int load;
472 unsigned long useful_load_threshold = balanced_irq_interval + 10;
473 int selected_irq;
474 int tmp_loaded, first_attempt = 1;
475 unsigned long tmp_cpu_irq;
476 unsigned long imbalance = 0;
477 cpumask_t allowed_mask, target_cpu_mask, tmp;
479 for_each_possible_cpu(i) {
480 int package_index;
481 CPU_IRQ(i) = 0;
482 if (!cpu_online(i))
483 continue;
484 package_index = CPU_TO_PACKAGEINDEX(i);
485 for (j = 0; j < NR_IRQS; j++) {
486 unsigned long value_now, delta;
487 /* Is this an active IRQ or balancing disabled ? */
488 if (!irq_desc[j].action || irq_balancing_disabled(j))
489 continue;
490 if (package_index == i)
491 IRQ_DELTA(package_index, j) = 0;
492 /* Determine the total count per processor per IRQ */
493 value_now = (unsigned long) kstat_cpu(i).irqs[j];
495 /* Determine the activity per processor per IRQ */
496 delta = value_now - LAST_CPU_IRQ(i, j);
498 /* Update last_cpu_irq[][] for the next time */
499 LAST_CPU_IRQ(i, j) = value_now;
501 /* Ignore IRQs whose rate is less than the clock */
502 if (delta < useful_load_threshold)
503 continue;
504 /* update the load for the processor or package total */
505 IRQ_DELTA(package_index, j) += delta;
507 /* Keep track of the higher numbered sibling as well */
508 if (i != package_index)
509 CPU_IRQ(i) += delta;
511 * We have sibling A and sibling B in the package
513 * cpu_irq[A] = load for cpu A + load for cpu B
514 * cpu_irq[B] = load for cpu B
516 CPU_IRQ(package_index) += delta;
519 /* Find the least loaded processor package */
520 for_each_online_cpu(i) {
521 if (i != CPU_TO_PACKAGEINDEX(i))
522 continue;
523 if (min_cpu_irq > CPU_IRQ(i)) {
524 min_cpu_irq = CPU_IRQ(i);
525 min_loaded = i;
528 max_cpu_irq = ULONG_MAX;
530 tryanothercpu:
532 * Look for heaviest loaded processor.
533 * We may come back to get the next heaviest loaded processor.
534 * Skip processors with trivial loads.
536 tmp_cpu_irq = 0;
537 tmp_loaded = -1;
538 for_each_online_cpu(i) {
539 if (i != CPU_TO_PACKAGEINDEX(i))
540 continue;
541 if (max_cpu_irq <= CPU_IRQ(i))
542 continue;
543 if (tmp_cpu_irq < CPU_IRQ(i)) {
544 tmp_cpu_irq = CPU_IRQ(i);
545 tmp_loaded = i;
549 if (tmp_loaded == -1) {
551 * In the case of small number of heavy interrupt sources,
552 * loading some of the cpus too much. We use Ingo's original
553 * approach to rotate them around.
555 if (!first_attempt && imbalance >= useful_load_threshold) {
556 rotate_irqs_among_cpus(useful_load_threshold);
557 return;
559 goto not_worth_the_effort;
562 first_attempt = 0; /* heaviest search */
563 max_cpu_irq = tmp_cpu_irq; /* load */
564 max_loaded = tmp_loaded; /* processor */
565 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
568 * if imbalance is less than approx 10% of max load, then
569 * observe diminishing returns action. - quit
571 if (imbalance < (max_cpu_irq >> 3))
572 goto not_worth_the_effort;
574 tryanotherirq:
575 /* if we select an IRQ to move that can't go where we want, then
576 * see if there is another one to try.
578 move_this_load = 0;
579 selected_irq = -1;
580 for (j = 0; j < NR_IRQS; j++) {
581 /* Is this an active IRQ? */
582 if (!irq_desc[j].action)
583 continue;
584 if (imbalance <= IRQ_DELTA(max_loaded, j))
585 continue;
586 /* Try to find the IRQ that is closest to the imbalance
587 * without going over.
589 if (move_this_load < IRQ_DELTA(max_loaded, j)) {
590 move_this_load = IRQ_DELTA(max_loaded, j);
591 selected_irq = j;
594 if (selected_irq == -1)
595 goto tryanothercpu;
597 imbalance = move_this_load;
599 /* For physical_balance case, we accumulated both load
600 * values in the one of the siblings cpu_irq[],
601 * to use the same code for physical and logical processors
602 * as much as possible.
604 * NOTE: the cpu_irq[] array holds the sum of the load for
605 * sibling A and sibling B in the slot for the lowest numbered
606 * sibling (A), _AND_ the load for sibling B in the slot for
607 * the higher numbered sibling.
609 * We seek the least loaded sibling by making the comparison
610 * (A+B)/2 vs B
612 load = CPU_IRQ(min_loaded) >> 1;
613 for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) {
614 if (load > CPU_IRQ(j)) {
615 /* This won't change cpu_sibling_map[min_loaded] */
616 load = CPU_IRQ(j);
617 min_loaded = j;
621 cpus_and(allowed_mask,
622 cpu_online_map,
623 balance_irq_affinity[selected_irq]);
624 target_cpu_mask = cpumask_of_cpu(min_loaded);
625 cpus_and(tmp, target_cpu_mask, allowed_mask);
627 if (!cpus_empty(tmp)) {
628 /* mark for change destination */
629 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
631 /* Since we made a change, come back sooner to
632 * check for more variation.
634 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
635 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
636 return;
638 goto tryanotherirq;
640 not_worth_the_effort:
642 * if we did not find an IRQ to move, then adjust the time interval
643 * upward
645 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
646 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
647 return;
650 static int balanced_irq(void *unused)
652 int i;
653 unsigned long prev_balance_time = jiffies;
654 long time_remaining = balanced_irq_interval;
656 /* push everything to CPU 0 to give us a starting point. */
657 for (i = 0 ; i < NR_IRQS ; i++) {
658 irq_desc[i].pending_mask = cpumask_of_cpu(0);
659 set_pending_irq(i, cpumask_of_cpu(0));
662 set_freezable();
663 for ( ; ; ) {
664 time_remaining = schedule_timeout_interruptible(time_remaining);
665 try_to_freeze();
666 if (time_after(jiffies,
667 prev_balance_time+balanced_irq_interval)) {
668 preempt_disable();
669 do_irq_balance();
670 prev_balance_time = jiffies;
671 time_remaining = balanced_irq_interval;
672 preempt_enable();
675 return 0;
678 static int __init balanced_irq_init(void)
680 int i;
681 struct cpuinfo_x86 *c;
682 cpumask_t tmp;
684 cpus_shift_right(tmp, cpu_online_map, 2);
685 c = &boot_cpu_data;
686 /* When not overwritten by the command line ask subarchitecture. */
687 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
688 irqbalance_disabled = NO_BALANCE_IRQ;
689 if (irqbalance_disabled)
690 return 0;
692 /* disable irqbalance completely if there is only one processor online */
693 if (num_online_cpus() < 2) {
694 irqbalance_disabled = 1;
695 return 0;
698 * Enable physical balance only if more than 1 physical processor
699 * is present
701 if (smp_num_siblings > 1 && !cpus_empty(tmp))
702 physical_balance = 1;
704 for_each_online_cpu(i) {
705 irq_cpu_data[i].irq_delta = kzalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
706 irq_cpu_data[i].last_irq = kzalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
707 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
708 printk(KERN_ERR "balanced_irq_init: out of memory");
709 goto failed;
713 printk(KERN_INFO "Starting balanced_irq\n");
714 if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
715 return 0;
716 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
717 failed:
718 for_each_possible_cpu(i) {
719 kfree(irq_cpu_data[i].irq_delta);
720 irq_cpu_data[i].irq_delta = NULL;
721 kfree(irq_cpu_data[i].last_irq);
722 irq_cpu_data[i].last_irq = NULL;
724 return 0;
727 int __devinit irqbalance_disable(char *str)
729 irqbalance_disabled = 1;
730 return 1;
733 __setup("noirqbalance", irqbalance_disable);
735 late_initcall(balanced_irq_init);
736 #endif /* CONFIG_IRQBALANCE */
737 #endif /* CONFIG_SMP */
739 #ifndef CONFIG_SMP
740 void send_IPI_self(int vector)
742 unsigned int cfg;
745 * Wait for idle.
747 apic_wait_icr_idle();
748 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
750 * Send the IPI. The write to APIC_ICR fires this off.
752 apic_write_around(APIC_ICR, cfg);
754 #endif /* !CONFIG_SMP */
758 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
759 * specific CPU-side IRQs.
762 #define MAX_PIRQS 8
763 static int pirq_entries [MAX_PIRQS];
764 static int pirqs_enabled;
765 int skip_ioapic_setup;
767 static int __init ioapic_pirq_setup(char *str)
769 int i, max;
770 int ints[MAX_PIRQS+1];
772 get_options(str, ARRAY_SIZE(ints), ints);
774 for (i = 0; i < MAX_PIRQS; i++)
775 pirq_entries[i] = -1;
777 pirqs_enabled = 1;
778 apic_printk(APIC_VERBOSE, KERN_INFO
779 "PIRQ redirection, working around broken MP-BIOS.\n");
780 max = MAX_PIRQS;
781 if (ints[0] < MAX_PIRQS)
782 max = ints[0];
784 for (i = 0; i < max; i++) {
785 apic_printk(APIC_VERBOSE, KERN_DEBUG
786 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
788 * PIRQs are mapped upside down, usually.
790 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
792 return 1;
795 __setup("pirq=", ioapic_pirq_setup);
798 * Find the IRQ entry number of a certain pin.
800 static int find_irq_entry(int apic, int pin, int type)
802 int i;
804 for (i = 0; i < mp_irq_entries; i++)
805 if (mp_irqs[i].mpc_irqtype == type &&
806 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
807 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
808 mp_irqs[i].mpc_dstirq == pin)
809 return i;
811 return -1;
815 * Find the pin to which IRQ[irq] (ISA) is connected
817 static int __init find_isa_irq_pin(int irq, int type)
819 int i;
821 for (i = 0; i < mp_irq_entries; i++) {
822 int lbus = mp_irqs[i].mpc_srcbus;
824 if (test_bit(lbus, mp_bus_not_pci) &&
825 (mp_irqs[i].mpc_irqtype == type) &&
826 (mp_irqs[i].mpc_srcbusirq == irq))
828 return mp_irqs[i].mpc_dstirq;
830 return -1;
833 static int __init find_isa_irq_apic(int irq, int type)
835 int i;
837 for (i = 0; i < mp_irq_entries; i++) {
838 int lbus = mp_irqs[i].mpc_srcbus;
840 if (test_bit(lbus, mp_bus_not_pci) &&
841 (mp_irqs[i].mpc_irqtype == type) &&
842 (mp_irqs[i].mpc_srcbusirq == irq))
843 break;
845 if (i < mp_irq_entries) {
846 int apic;
847 for (apic = 0; apic < nr_ioapics; apic++) {
848 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
849 return apic;
853 return -1;
857 * Find a specific PCI IRQ entry.
858 * Not an __init, possibly needed by modules
860 static int pin_2_irq(int idx, int apic, int pin);
862 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
864 int apic, i, best_guess = -1;
866 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
867 "slot:%d, pin:%d.\n", bus, slot, pin);
868 if (mp_bus_id_to_pci_bus[bus] == -1) {
869 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
870 return -1;
872 for (i = 0; i < mp_irq_entries; i++) {
873 int lbus = mp_irqs[i].mpc_srcbus;
875 for (apic = 0; apic < nr_ioapics; apic++)
876 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
877 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
878 break;
880 if (!test_bit(lbus, mp_bus_not_pci) &&
881 !mp_irqs[i].mpc_irqtype &&
882 (bus == lbus) &&
883 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
884 int irq = pin_2_irq(i, apic, mp_irqs[i].mpc_dstirq);
886 if (!(apic || IO_APIC_IRQ(irq)))
887 continue;
889 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
890 return irq;
892 * Use the first all-but-pin matching entry as a
893 * best-guess fuzzy result for broken mptables.
895 if (best_guess < 0)
896 best_guess = irq;
899 return best_guess;
901 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
904 * This function currently is only a helper for the i386 smp boot process where
905 * we need to reprogram the ioredtbls to cater for the cpus which have come online
906 * so mask in all cases should simply be TARGET_CPUS
908 #ifdef CONFIG_SMP
909 void __init setup_ioapic_dest(void)
911 int pin, ioapic, irq, irq_entry;
913 if (skip_ioapic_setup == 1)
914 return;
916 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
917 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
918 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
919 if (irq_entry == -1)
920 continue;
921 irq = pin_2_irq(irq_entry, ioapic, pin);
922 set_ioapic_affinity_irq(irq, TARGET_CPUS);
927 #endif
929 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
931 * EISA Edge/Level control register, ELCR
933 static int EISA_ELCR(unsigned int irq)
935 if (irq < 16) {
936 unsigned int port = 0x4d0 + (irq >> 3);
937 return (inb(port) >> (irq & 7)) & 1;
939 apic_printk(APIC_VERBOSE, KERN_INFO
940 "Broken MPtable reports ISA irq %d\n", irq);
941 return 0;
943 #endif
945 /* ISA interrupts are always polarity zero edge triggered,
946 * when listed as conforming in the MP table. */
948 #define default_ISA_trigger(idx) (0)
949 #define default_ISA_polarity(idx) (0)
951 /* EISA interrupts are always polarity zero and can be edge or level
952 * trigger depending on the ELCR value. If an interrupt is listed as
953 * EISA conforming in the MP table, that means its trigger type must
954 * be read in from the ELCR */
956 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
957 #define default_EISA_polarity(idx) default_ISA_polarity(idx)
959 /* PCI interrupts are always polarity one level triggered,
960 * when listed as conforming in the MP table. */
962 #define default_PCI_trigger(idx) (1)
963 #define default_PCI_polarity(idx) (1)
965 /* MCA interrupts are always polarity zero level triggered,
966 * when listed as conforming in the MP table. */
968 #define default_MCA_trigger(idx) (1)
969 #define default_MCA_polarity(idx) default_ISA_polarity(idx)
971 static int MPBIOS_polarity(int idx)
973 int bus = mp_irqs[idx].mpc_srcbus;
974 int polarity;
977 * Determine IRQ line polarity (high active or low active):
979 switch (mp_irqs[idx].mpc_irqflag & 3) {
980 case 0: /* conforms, ie. bus-type dependent polarity */
982 polarity = test_bit(bus, mp_bus_not_pci)?
983 default_ISA_polarity(idx):
984 default_PCI_polarity(idx);
985 break;
987 case 1: /* high active */
989 polarity = 0;
990 break;
992 case 2: /* reserved */
994 printk(KERN_WARNING "broken BIOS!!\n");
995 polarity = 1;
996 break;
998 case 3: /* low active */
1000 polarity = 1;
1001 break;
1003 default: /* invalid */
1005 printk(KERN_WARNING "broken BIOS!!\n");
1006 polarity = 1;
1007 break;
1010 return polarity;
1013 static int MPBIOS_trigger(int idx)
1015 int bus = mp_irqs[idx].mpc_srcbus;
1016 int trigger;
1019 * Determine IRQ trigger mode (edge or level sensitive):
1021 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3) {
1022 case 0: /* conforms, ie. bus-type dependent */
1024 trigger = test_bit(bus, mp_bus_not_pci)?
1025 default_ISA_trigger(idx):
1026 default_PCI_trigger(idx);
1027 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
1028 switch (mp_bus_id_to_type[bus]) {
1029 case MP_BUS_ISA: /* ISA pin */
1031 /* set before the switch */
1032 break;
1034 case MP_BUS_EISA: /* EISA pin */
1036 trigger = default_EISA_trigger(idx);
1037 break;
1039 case MP_BUS_PCI: /* PCI pin */
1041 /* set before the switch */
1042 break;
1044 case MP_BUS_MCA: /* MCA pin */
1046 trigger = default_MCA_trigger(idx);
1047 break;
1049 default:
1051 printk(KERN_WARNING "broken BIOS!!\n");
1052 trigger = 1;
1053 break;
1056 #endif
1057 break;
1059 case 1: /* edge */
1061 trigger = 0;
1062 break;
1064 case 2: /* reserved */
1066 printk(KERN_WARNING "broken BIOS!!\n");
1067 trigger = 1;
1068 break;
1070 case 3: /* level */
1072 trigger = 1;
1073 break;
1075 default: /* invalid */
1077 printk(KERN_WARNING "broken BIOS!!\n");
1078 trigger = 0;
1079 break;
1082 return trigger;
1085 static inline int irq_polarity(int idx)
1087 return MPBIOS_polarity(idx);
1090 static inline int irq_trigger(int idx)
1092 return MPBIOS_trigger(idx);
1095 static int pin_2_irq(int idx, int apic, int pin)
1097 int irq, i;
1098 int bus = mp_irqs[idx].mpc_srcbus;
1101 * Debugging check, we are in big trouble if this message pops up!
1103 if (mp_irqs[idx].mpc_dstirq != pin)
1104 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1106 if (test_bit(bus, mp_bus_not_pci))
1107 irq = mp_irqs[idx].mpc_srcbusirq;
1108 else {
1110 * PCI IRQs are mapped in order
1112 i = irq = 0;
1113 while (i < apic)
1114 irq += nr_ioapic_registers[i++];
1115 irq += pin;
1118 * For MPS mode, so far only needed by ES7000 platform
1120 if (ioapic_renumber_irq)
1121 irq = ioapic_renumber_irq(apic, irq);
1125 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1127 if ((pin >= 16) && (pin <= 23)) {
1128 if (pirq_entries[pin-16] != -1) {
1129 if (!pirq_entries[pin-16]) {
1130 apic_printk(APIC_VERBOSE, KERN_DEBUG
1131 "disabling PIRQ%d\n", pin-16);
1132 } else {
1133 irq = pirq_entries[pin-16];
1134 apic_printk(APIC_VERBOSE, KERN_DEBUG
1135 "using PIRQ%d -> IRQ %d\n",
1136 pin-16, irq);
1140 return irq;
1143 static inline int IO_APIC_irq_trigger(int irq)
1145 int apic, idx, pin;
1147 for (apic = 0; apic < nr_ioapics; apic++) {
1148 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1149 idx = find_irq_entry(apic, pin, mp_INT);
1150 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1151 return irq_trigger(idx);
1155 * nonexistent IRQs are edge default
1157 return 0;
1160 /* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
1161 static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
1163 static int __assign_irq_vector(int irq)
1165 static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
1166 int vector, offset;
1168 BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
1170 if (irq_vector[irq] > 0)
1171 return irq_vector[irq];
1173 vector = current_vector;
1174 offset = current_offset;
1175 next:
1176 vector += 8;
1177 if (vector >= FIRST_SYSTEM_VECTOR) {
1178 offset = (offset + 1) % 8;
1179 vector = FIRST_DEVICE_VECTOR + offset;
1181 if (vector == current_vector)
1182 return -ENOSPC;
1183 if (test_and_set_bit(vector, used_vectors))
1184 goto next;
1186 current_vector = vector;
1187 current_offset = offset;
1188 irq_vector[irq] = vector;
1190 return vector;
1193 static int assign_irq_vector(int irq)
1195 unsigned long flags;
1196 int vector;
1198 spin_lock_irqsave(&vector_lock, flags);
1199 vector = __assign_irq_vector(irq);
1200 spin_unlock_irqrestore(&vector_lock, flags);
1202 return vector;
1204 static struct irq_chip ioapic_chip;
1206 #define IOAPIC_AUTO -1
1207 #define IOAPIC_EDGE 0
1208 #define IOAPIC_LEVEL 1
1210 static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
1212 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1213 trigger == IOAPIC_LEVEL) {
1214 irq_desc[irq].status |= IRQ_LEVEL;
1215 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1216 handle_fasteoi_irq, "fasteoi");
1217 } else {
1218 irq_desc[irq].status &= ~IRQ_LEVEL;
1219 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1220 handle_edge_irq, "edge");
1222 set_intr_gate(vector, interrupt[irq]);
1225 static void __init setup_IO_APIC_irqs(void)
1227 struct IO_APIC_route_entry entry;
1228 int apic, pin, idx, irq, first_notcon = 1, vector;
1230 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1232 for (apic = 0; apic < nr_ioapics; apic++) {
1233 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1236 * add it to the IO-APIC irq-routing table:
1238 memset(&entry, 0, sizeof(entry));
1240 entry.delivery_mode = INT_DELIVERY_MODE;
1241 entry.dest_mode = INT_DEST_MODE;
1242 entry.mask = 0; /* enable IRQ */
1243 entry.dest.logical.logical_dest =
1244 cpu_mask_to_apicid(TARGET_CPUS);
1246 idx = find_irq_entry(apic, pin, mp_INT);
1247 if (idx == -1) {
1248 if (first_notcon) {
1249 apic_printk(APIC_VERBOSE, KERN_DEBUG
1250 " IO-APIC (apicid-pin) %d-%d",
1251 mp_ioapics[apic].mpc_apicid,
1252 pin);
1253 first_notcon = 0;
1254 } else
1255 apic_printk(APIC_VERBOSE, ", %d-%d",
1256 mp_ioapics[apic].mpc_apicid, pin);
1257 continue;
1260 if (!first_notcon) {
1261 apic_printk(APIC_VERBOSE, " not connected.\n");
1262 first_notcon = 1;
1265 entry.trigger = irq_trigger(idx);
1266 entry.polarity = irq_polarity(idx);
1268 if (irq_trigger(idx)) {
1269 entry.trigger = 1;
1270 entry.mask = 1;
1273 irq = pin_2_irq(idx, apic, pin);
1275 * skip adding the timer int on secondary nodes, which causes
1276 * a small but painful rift in the time-space continuum
1278 if (multi_timer_check(apic, irq))
1279 continue;
1280 else
1281 add_pin_to_irq(irq, apic, pin);
1283 if (!apic && !IO_APIC_IRQ(irq))
1284 continue;
1286 if (IO_APIC_IRQ(irq)) {
1287 vector = assign_irq_vector(irq);
1288 entry.vector = vector;
1289 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
1291 if (!apic && (irq < 16))
1292 disable_8259A_irq(irq);
1294 ioapic_write_entry(apic, pin, entry);
1298 if (!first_notcon)
1299 apic_printk(APIC_VERBOSE, " not connected.\n");
1303 * Set up the timer pin, possibly with the 8259A-master behind.
1305 static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1306 int vector)
1308 struct IO_APIC_route_entry entry;
1310 memset(&entry, 0, sizeof(entry));
1313 * We use logical delivery to get the timer IRQ
1314 * to the first CPU.
1316 entry.dest_mode = INT_DEST_MODE;
1317 entry.mask = 1; /* mask IRQ now */
1318 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1319 entry.delivery_mode = INT_DELIVERY_MODE;
1320 entry.polarity = 0;
1321 entry.trigger = 0;
1322 entry.vector = vector;
1325 * The timer IRQ doesn't have to know that behind the
1326 * scene we may have a 8259A-master in AEOI mode ...
1328 ioapic_register_intr(0, vector, IOAPIC_EDGE);
1331 * Add it to the IO-APIC irq-routing table:
1333 ioapic_write_entry(apic, pin, entry);
1336 void __init print_IO_APIC(void)
1338 int apic, i;
1339 union IO_APIC_reg_00 reg_00;
1340 union IO_APIC_reg_01 reg_01;
1341 union IO_APIC_reg_02 reg_02;
1342 union IO_APIC_reg_03 reg_03;
1343 unsigned long flags;
1345 if (apic_verbosity == APIC_QUIET)
1346 return;
1348 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1349 for (i = 0; i < nr_ioapics; i++)
1350 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1351 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
1354 * We are a bit conservative about what we expect. We have to
1355 * know about every hardware change ASAP.
1357 printk(KERN_INFO "testing the IO APIC.......................\n");
1359 for (apic = 0; apic < nr_ioapics; apic++) {
1361 spin_lock_irqsave(&ioapic_lock, flags);
1362 reg_00.raw = io_apic_read(apic, 0);
1363 reg_01.raw = io_apic_read(apic, 1);
1364 if (reg_01.bits.version >= 0x10)
1365 reg_02.raw = io_apic_read(apic, 2);
1366 if (reg_01.bits.version >= 0x20)
1367 reg_03.raw = io_apic_read(apic, 3);
1368 spin_unlock_irqrestore(&ioapic_lock, flags);
1370 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1371 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1372 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1373 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1374 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1376 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1377 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1379 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1380 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1383 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1384 * but the value of reg_02 is read as the previous read register
1385 * value, so ignore it if reg_02 == reg_01.
1387 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1388 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1389 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1393 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1394 * or reg_03, but the value of reg_0[23] is read as the previous read
1395 * register value, so ignore it if reg_03 == reg_0[12].
1397 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1398 reg_03.raw != reg_01.raw) {
1399 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1400 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1403 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1405 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1406 " Stat Dest Deli Vect: \n");
1408 for (i = 0; i <= reg_01.bits.entries; i++) {
1409 struct IO_APIC_route_entry entry;
1411 entry = ioapic_read_entry(apic, i);
1413 printk(KERN_DEBUG " %02x %03X %02X ",
1415 entry.dest.logical.logical_dest,
1416 entry.dest.physical.physical_dest
1419 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1420 entry.mask,
1421 entry.trigger,
1422 entry.irr,
1423 entry.polarity,
1424 entry.delivery_status,
1425 entry.dest_mode,
1426 entry.delivery_mode,
1427 entry.vector
1431 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1432 for (i = 0; i < NR_IRQS; i++) {
1433 struct irq_pin_list *entry = irq_2_pin + i;
1434 if (entry->pin < 0)
1435 continue;
1436 printk(KERN_DEBUG "IRQ%d ", i);
1437 for (;;) {
1438 printk("-> %d:%d", entry->apic, entry->pin);
1439 if (!entry->next)
1440 break;
1441 entry = irq_2_pin + entry->next;
1443 printk("\n");
1446 printk(KERN_INFO ".................................... done.\n");
1448 return;
1451 #if 0
1453 static void print_APIC_bitfield(int base)
1455 unsigned int v;
1456 int i, j;
1458 if (apic_verbosity == APIC_QUIET)
1459 return;
1461 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1462 for (i = 0; i < 8; i++) {
1463 v = apic_read(base + i*0x10);
1464 for (j = 0; j < 32; j++) {
1465 if (v & (1<<j))
1466 printk("1");
1467 else
1468 printk("0");
1470 printk("\n");
1474 void /*__init*/ print_local_APIC(void *dummy)
1476 unsigned int v, ver, maxlvt;
1478 if (apic_verbosity == APIC_QUIET)
1479 return;
1481 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1482 smp_processor_id(), hard_smp_processor_id());
1483 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
1484 GET_APIC_ID(read_apic_id()));
1485 v = apic_read(APIC_LVR);
1486 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1487 ver = GET_APIC_VERSION(v);
1488 maxlvt = lapic_get_maxlvt();
1490 v = apic_read(APIC_TASKPRI);
1491 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1493 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1494 v = apic_read(APIC_ARBPRI);
1495 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1496 v & APIC_ARBPRI_MASK);
1497 v = apic_read(APIC_PROCPRI);
1498 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1501 v = apic_read(APIC_EOI);
1502 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1503 v = apic_read(APIC_RRR);
1504 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1505 v = apic_read(APIC_LDR);
1506 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1507 v = apic_read(APIC_DFR);
1508 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1509 v = apic_read(APIC_SPIV);
1510 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1512 printk(KERN_DEBUG "... APIC ISR field:\n");
1513 print_APIC_bitfield(APIC_ISR);
1514 printk(KERN_DEBUG "... APIC TMR field:\n");
1515 print_APIC_bitfield(APIC_TMR);
1516 printk(KERN_DEBUG "... APIC IRR field:\n");
1517 print_APIC_bitfield(APIC_IRR);
1519 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1520 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1521 apic_write(APIC_ESR, 0);
1522 v = apic_read(APIC_ESR);
1523 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1526 v = apic_read(APIC_ICR);
1527 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1528 v = apic_read(APIC_ICR2);
1529 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1531 v = apic_read(APIC_LVTT);
1532 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1534 if (maxlvt > 3) { /* PC is LVT#4. */
1535 v = apic_read(APIC_LVTPC);
1536 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1538 v = apic_read(APIC_LVT0);
1539 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1540 v = apic_read(APIC_LVT1);
1541 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1543 if (maxlvt > 2) { /* ERR is LVT#3. */
1544 v = apic_read(APIC_LVTERR);
1545 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1548 v = apic_read(APIC_TMICT);
1549 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1550 v = apic_read(APIC_TMCCT);
1551 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1552 v = apic_read(APIC_TDCR);
1553 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1554 printk("\n");
1557 void print_all_local_APICs(void)
1559 on_each_cpu(print_local_APIC, NULL, 1, 1);
1562 void /*__init*/ print_PIC(void)
1564 unsigned int v;
1565 unsigned long flags;
1567 if (apic_verbosity == APIC_QUIET)
1568 return;
1570 printk(KERN_DEBUG "\nprinting PIC contents\n");
1572 spin_lock_irqsave(&i8259A_lock, flags);
1574 v = inb(0xa1) << 8 | inb(0x21);
1575 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1577 v = inb(0xa0) << 8 | inb(0x20);
1578 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1580 outb(0x0b, 0xa0);
1581 outb(0x0b, 0x20);
1582 v = inb(0xa0) << 8 | inb(0x20);
1583 outb(0x0a, 0xa0);
1584 outb(0x0a, 0x20);
1586 spin_unlock_irqrestore(&i8259A_lock, flags);
1588 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1590 v = inb(0x4d1) << 8 | inb(0x4d0);
1591 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1594 #endif /* 0 */
1596 static void __init enable_IO_APIC(void)
1598 union IO_APIC_reg_01 reg_01;
1599 int i8259_apic, i8259_pin;
1600 int i, apic;
1601 unsigned long flags;
1603 for (i = 0; i < PIN_MAP_SIZE; i++) {
1604 irq_2_pin[i].pin = -1;
1605 irq_2_pin[i].next = 0;
1607 if (!pirqs_enabled)
1608 for (i = 0; i < MAX_PIRQS; i++)
1609 pirq_entries[i] = -1;
1612 * The number of IO-APIC IRQ registers (== #pins):
1614 for (apic = 0; apic < nr_ioapics; apic++) {
1615 spin_lock_irqsave(&ioapic_lock, flags);
1616 reg_01.raw = io_apic_read(apic, 1);
1617 spin_unlock_irqrestore(&ioapic_lock, flags);
1618 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1620 for (apic = 0; apic < nr_ioapics; apic++) {
1621 int pin;
1622 /* See if any of the pins is in ExtINT mode */
1623 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1624 struct IO_APIC_route_entry entry;
1625 entry = ioapic_read_entry(apic, pin);
1628 /* If the interrupt line is enabled and in ExtInt mode
1629 * I have found the pin where the i8259 is connected.
1631 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1632 ioapic_i8259.apic = apic;
1633 ioapic_i8259.pin = pin;
1634 goto found_i8259;
1638 found_i8259:
1639 /* Look to see what if the MP table has reported the ExtINT */
1640 /* If we could not find the appropriate pin by looking at the ioapic
1641 * the i8259 probably is not connected the ioapic but give the
1642 * mptable a chance anyway.
1644 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1645 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1646 /* Trust the MP table if nothing is setup in the hardware */
1647 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1648 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1649 ioapic_i8259.pin = i8259_pin;
1650 ioapic_i8259.apic = i8259_apic;
1652 /* Complain if the MP table and the hardware disagree */
1653 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1654 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1656 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1660 * Do not trust the IO-APIC being empty at bootup
1662 clear_IO_APIC();
1666 * Not an __init, needed by the reboot code
1668 void disable_IO_APIC(void)
1671 * Clear the IO-APIC before rebooting:
1673 clear_IO_APIC();
1676 * If the i8259 is routed through an IOAPIC
1677 * Put that IOAPIC in virtual wire mode
1678 * so legacy interrupts can be delivered.
1680 if (ioapic_i8259.pin != -1) {
1681 struct IO_APIC_route_entry entry;
1683 memset(&entry, 0, sizeof(entry));
1684 entry.mask = 0; /* Enabled */
1685 entry.trigger = 0; /* Edge */
1686 entry.irr = 0;
1687 entry.polarity = 0; /* High */
1688 entry.delivery_status = 0;
1689 entry.dest_mode = 0; /* Physical */
1690 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1691 entry.vector = 0;
1692 entry.dest.physical.physical_dest =
1693 GET_APIC_ID(read_apic_id());
1696 * Add it to the IO-APIC irq-routing table:
1698 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1700 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1704 * function to set the IO-APIC physical IDs based on the
1705 * values stored in the MPC table.
1707 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1710 #ifndef CONFIG_X86_NUMAQ
1711 static void __init setup_ioapic_ids_from_mpc(void)
1713 union IO_APIC_reg_00 reg_00;
1714 physid_mask_t phys_id_present_map;
1715 int apic;
1716 int i;
1717 unsigned char old_id;
1718 unsigned long flags;
1721 * Don't check I/O APIC IDs for xAPIC systems. They have
1722 * no meaning without the serial APIC bus.
1724 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1725 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
1726 return;
1728 * This is broken; anything with a real cpu count has to
1729 * circumvent this idiocy regardless.
1731 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1734 * Set the IOAPIC ID to the value stored in the MPC table.
1736 for (apic = 0; apic < nr_ioapics; apic++) {
1738 /* Read the register 0 value */
1739 spin_lock_irqsave(&ioapic_lock, flags);
1740 reg_00.raw = io_apic_read(apic, 0);
1741 spin_unlock_irqrestore(&ioapic_lock, flags);
1743 old_id = mp_ioapics[apic].mpc_apicid;
1745 if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
1746 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1747 apic, mp_ioapics[apic].mpc_apicid);
1748 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1749 reg_00.bits.ID);
1750 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1754 * Sanity check, is the ID really free? Every APIC in a
1755 * system must have a unique ID or we get lots of nice
1756 * 'stuck on smp_invalidate_needed IPI wait' messages.
1758 if (check_apicid_used(phys_id_present_map,
1759 mp_ioapics[apic].mpc_apicid)) {
1760 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1761 apic, mp_ioapics[apic].mpc_apicid);
1762 for (i = 0; i < get_physical_broadcast(); i++)
1763 if (!physid_isset(i, phys_id_present_map))
1764 break;
1765 if (i >= get_physical_broadcast())
1766 panic("Max APIC ID exceeded!\n");
1767 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1769 physid_set(i, phys_id_present_map);
1770 mp_ioapics[apic].mpc_apicid = i;
1771 } else {
1772 physid_mask_t tmp;
1773 tmp = apicid_to_cpu_present(mp_ioapics[apic].mpc_apicid);
1774 apic_printk(APIC_VERBOSE, "Setting %d in the "
1775 "phys_id_present_map\n",
1776 mp_ioapics[apic].mpc_apicid);
1777 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1782 * We need to adjust the IRQ routing table
1783 * if the ID changed.
1785 if (old_id != mp_ioapics[apic].mpc_apicid)
1786 for (i = 0; i < mp_irq_entries; i++)
1787 if (mp_irqs[i].mpc_dstapic == old_id)
1788 mp_irqs[i].mpc_dstapic
1789 = mp_ioapics[apic].mpc_apicid;
1792 * Read the right value from the MPC table and
1793 * write it into the ID register.
1795 apic_printk(APIC_VERBOSE, KERN_INFO
1796 "...changing IO-APIC physical APIC ID to %d ...",
1797 mp_ioapics[apic].mpc_apicid);
1799 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1800 spin_lock_irqsave(&ioapic_lock, flags);
1801 io_apic_write(apic, 0, reg_00.raw);
1802 spin_unlock_irqrestore(&ioapic_lock, flags);
1805 * Sanity check
1807 spin_lock_irqsave(&ioapic_lock, flags);
1808 reg_00.raw = io_apic_read(apic, 0);
1809 spin_unlock_irqrestore(&ioapic_lock, flags);
1810 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1811 printk("could not set ID!\n");
1812 else
1813 apic_printk(APIC_VERBOSE, " ok.\n");
1816 #else
1817 static void __init setup_ioapic_ids_from_mpc(void) { }
1818 #endif
1820 int no_timer_check __initdata;
1822 static int __init notimercheck(char *s)
1824 no_timer_check = 1;
1825 return 1;
1827 __setup("no_timer_check", notimercheck);
1830 * There is a nasty bug in some older SMP boards, their mptable lies
1831 * about the timer IRQ. We do the following to work around the situation:
1833 * - timer IRQ defaults to IO-APIC IRQ
1834 * - if this function detects that timer IRQs are defunct, then we fall
1835 * back to ISA timer IRQs
1837 static int __init timer_irq_works(void)
1839 unsigned long t1 = jiffies;
1840 unsigned long flags;
1842 if (no_timer_check)
1843 return 1;
1845 local_save_flags(flags);
1846 local_irq_enable();
1847 /* Let ten ticks pass... */
1848 mdelay((10 * 1000) / HZ);
1849 local_irq_restore(flags);
1852 * Expect a few ticks at least, to be sure some possible
1853 * glue logic does not lock up after one or two first
1854 * ticks in a non-ExtINT mode. Also the local APIC
1855 * might have cached one ExtINT interrupt. Finally, at
1856 * least one tick may be lost due to delays.
1858 if (time_after(jiffies, t1 + 4))
1859 return 1;
1861 return 0;
1865 * In the SMP+IOAPIC case it might happen that there are an unspecified
1866 * number of pending IRQ events unhandled. These cases are very rare,
1867 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1868 * better to do it this way as thus we do not have to be aware of
1869 * 'pending' interrupts in the IRQ path, except at this point.
1872 * Edge triggered needs to resend any interrupt
1873 * that was delayed but this is now handled in the device
1874 * independent code.
1878 * Startup quirk:
1880 * Starting up a edge-triggered IO-APIC interrupt is
1881 * nasty - we need to make sure that we get the edge.
1882 * If it is already asserted for some reason, we need
1883 * return 1 to indicate that is was pending.
1885 * This is not complete - we should be able to fake
1886 * an edge even if it isn't on the 8259A...
1888 * (We do this for level-triggered IRQs too - it cannot hurt.)
1890 static unsigned int startup_ioapic_irq(unsigned int irq)
1892 int was_pending = 0;
1893 unsigned long flags;
1895 spin_lock_irqsave(&ioapic_lock, flags);
1896 if (irq < 16) {
1897 disable_8259A_irq(irq);
1898 if (i8259A_irq_pending(irq))
1899 was_pending = 1;
1901 __unmask_IO_APIC_irq(irq);
1902 spin_unlock_irqrestore(&ioapic_lock, flags);
1904 return was_pending;
1907 static void ack_ioapic_irq(unsigned int irq)
1909 move_native_irq(irq);
1910 ack_APIC_irq();
1913 static void ack_ioapic_quirk_irq(unsigned int irq)
1915 unsigned long v;
1916 int i;
1918 move_native_irq(irq);
1920 * It appears there is an erratum which affects at least version 0x11
1921 * of I/O APIC (that's the 82093AA and cores integrated into various
1922 * chipsets). Under certain conditions a level-triggered interrupt is
1923 * erroneously delivered as edge-triggered one but the respective IRR
1924 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1925 * message but it will never arrive and further interrupts are blocked
1926 * from the source. The exact reason is so far unknown, but the
1927 * phenomenon was observed when two consecutive interrupt requests
1928 * from a given source get delivered to the same CPU and the source is
1929 * temporarily disabled in between.
1931 * A workaround is to simulate an EOI message manually. We achieve it
1932 * by setting the trigger mode to edge and then to level when the edge
1933 * trigger mode gets detected in the TMR of a local APIC for a
1934 * level-triggered interrupt. We mask the source for the time of the
1935 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1936 * The idea is from Manfred Spraul. --macro
1938 i = irq_vector[irq];
1940 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1942 ack_APIC_irq();
1944 if (!(v & (1 << (i & 0x1f)))) {
1945 atomic_inc(&irq_mis_count);
1946 spin_lock(&ioapic_lock);
1947 __mask_and_edge_IO_APIC_irq(irq);
1948 __unmask_and_level_IO_APIC_irq(irq);
1949 spin_unlock(&ioapic_lock);
1953 static int ioapic_retrigger_irq(unsigned int irq)
1955 send_IPI_self(irq_vector[irq]);
1957 return 1;
1960 static struct irq_chip ioapic_chip __read_mostly = {
1961 .name = "IO-APIC",
1962 .startup = startup_ioapic_irq,
1963 .mask = mask_IO_APIC_irq,
1964 .unmask = unmask_IO_APIC_irq,
1965 .ack = ack_ioapic_irq,
1966 .eoi = ack_ioapic_quirk_irq,
1967 #ifdef CONFIG_SMP
1968 .set_affinity = set_ioapic_affinity_irq,
1969 #endif
1970 .retrigger = ioapic_retrigger_irq,
1974 static inline void init_IO_APIC_traps(void)
1976 int irq;
1979 * NOTE! The local APIC isn't very good at handling
1980 * multiple interrupts at the same interrupt level.
1981 * As the interrupt level is determined by taking the
1982 * vector number and shifting that right by 4, we
1983 * want to spread these out a bit so that they don't
1984 * all fall in the same interrupt level.
1986 * Also, we've got to be careful not to trash gate
1987 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1989 for (irq = 0; irq < NR_IRQS ; irq++) {
1990 if (IO_APIC_IRQ(irq) && !irq_vector[irq]) {
1992 * Hmm.. We don't have an entry for this,
1993 * so default to an old-fashioned 8259
1994 * interrupt if we can..
1996 if (irq < 16)
1997 make_8259A_irq(irq);
1998 else
1999 /* Strange. Oh, well.. */
2000 irq_desc[irq].chip = &no_irq_chip;
2006 * The local APIC irq-chip implementation:
2009 static void ack_apic(unsigned int irq)
2011 ack_APIC_irq();
2014 static void mask_lapic_irq(unsigned int irq)
2016 unsigned long v;
2018 v = apic_read(APIC_LVT0);
2019 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
2022 static void unmask_lapic_irq(unsigned int irq)
2024 unsigned long v;
2026 v = apic_read(APIC_LVT0);
2027 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
2030 static struct irq_chip lapic_chip __read_mostly = {
2031 .name = "local-APIC",
2032 .mask = mask_lapic_irq,
2033 .unmask = unmask_lapic_irq,
2034 .eoi = ack_apic,
2037 static void __init setup_nmi(void)
2040 * Dirty trick to enable the NMI watchdog ...
2041 * We put the 8259A master into AEOI mode and
2042 * unmask on all local APICs LVT0 as NMI.
2044 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2045 * is from Maciej W. Rozycki - so we do not have to EOI from
2046 * the NMI handler or the timer interrupt.
2048 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2050 enable_NMI_through_LVT0();
2052 apic_printk(APIC_VERBOSE, " done.\n");
2056 * This looks a bit hackish but it's about the only one way of sending
2057 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2058 * not support the ExtINT mode, unfortunately. We need to send these
2059 * cycles as some i82489DX-based boards have glue logic that keeps the
2060 * 8259A interrupt line asserted until INTA. --macro
2062 static inline void __init unlock_ExtINT_logic(void)
2064 int apic, pin, i;
2065 struct IO_APIC_route_entry entry0, entry1;
2066 unsigned char save_control, save_freq_select;
2068 pin = find_isa_irq_pin(8, mp_INT);
2069 if (pin == -1) {
2070 WARN_ON_ONCE(1);
2071 return;
2073 apic = find_isa_irq_apic(8, mp_INT);
2074 if (apic == -1) {
2075 WARN_ON_ONCE(1);
2076 return;
2079 entry0 = ioapic_read_entry(apic, pin);
2080 clear_IO_APIC_pin(apic, pin);
2082 memset(&entry1, 0, sizeof(entry1));
2084 entry1.dest_mode = 0; /* physical delivery */
2085 entry1.mask = 0; /* unmask IRQ now */
2086 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2087 entry1.delivery_mode = dest_ExtINT;
2088 entry1.polarity = entry0.polarity;
2089 entry1.trigger = 0;
2090 entry1.vector = 0;
2092 ioapic_write_entry(apic, pin, entry1);
2094 save_control = CMOS_READ(RTC_CONTROL);
2095 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2096 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2097 RTC_FREQ_SELECT);
2098 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2100 i = 100;
2101 while (i-- > 0) {
2102 mdelay(10);
2103 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2104 i -= 10;
2107 CMOS_WRITE(save_control, RTC_CONTROL);
2108 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2109 clear_IO_APIC_pin(apic, pin);
2111 ioapic_write_entry(apic, pin, entry0);
2115 * This code may look a bit paranoid, but it's supposed to cooperate with
2116 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2117 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2118 * fanatically on his truly buggy board.
2120 static inline void __init check_timer(void)
2122 int apic1, pin1, apic2, pin2;
2123 int no_pin1 = 0;
2124 int vector;
2125 unsigned int ver;
2126 unsigned long flags;
2128 local_irq_save(flags);
2130 ver = apic_read(APIC_LVR);
2131 ver = GET_APIC_VERSION(ver);
2134 * get/set the timer IRQ vector:
2136 disable_8259A_irq(0);
2137 vector = assign_irq_vector(0);
2138 set_intr_gate(vector, interrupt[0]);
2141 * As IRQ0 is to be enabled in the 8259A, the virtual
2142 * wire has to be disabled in the local APIC. Also
2143 * timer interrupts need to be acknowledged manually in
2144 * the 8259A for the i82489DX when using the NMI
2145 * watchdog as that APIC treats NMIs as level-triggered.
2146 * The AEOI mode will finish them in the 8259A
2147 * automatically.
2149 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2150 init_8259A(1);
2151 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2153 pin1 = find_isa_irq_pin(0, mp_INT);
2154 apic1 = find_isa_irq_apic(0, mp_INT);
2155 pin2 = ioapic_i8259.pin;
2156 apic2 = ioapic_i8259.apic;
2158 printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
2159 vector, apic1, pin1, apic2, pin2);
2162 * Some BIOS writers are clueless and report the ExtINTA
2163 * I/O APIC input from the cascaded 8259A as the timer
2164 * interrupt input. So just in case, if only one pin
2165 * was found above, try it both directly and through the
2166 * 8259A.
2168 if (pin1 == -1) {
2169 pin1 = pin2;
2170 apic1 = apic2;
2171 no_pin1 = 1;
2172 } else if (pin2 == -1) {
2173 pin2 = pin1;
2174 apic2 = apic1;
2177 if (pin1 != -1) {
2179 * Ok, does IRQ0 through the IOAPIC work?
2181 if (no_pin1) {
2182 add_pin_to_irq(0, apic1, pin1);
2183 setup_timer_IRQ0_pin(apic1, pin1, vector);
2185 unmask_IO_APIC_irq(0);
2186 if (timer_irq_works()) {
2187 if (nmi_watchdog == NMI_IO_APIC) {
2188 setup_nmi();
2189 enable_8259A_irq(0);
2191 if (disable_timer_pin_1 > 0)
2192 clear_IO_APIC_pin(0, pin1);
2193 goto out;
2195 clear_IO_APIC_pin(apic1, pin1);
2196 if (!no_pin1)
2197 printk(KERN_ERR "..MP-BIOS bug: "
2198 "8254 timer not connected to IO-APIC\n");
2200 printk(KERN_INFO "...trying to set up timer (IRQ0) "
2201 "through the 8259A ... ");
2202 printk("\n..... (found pin %d) ...", pin2);
2204 * legacy devices should be connected to IO APIC #0
2206 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
2207 setup_timer_IRQ0_pin(apic2, pin2, vector);
2208 unmask_IO_APIC_irq(0);
2209 enable_8259A_irq(0);
2210 if (timer_irq_works()) {
2211 printk("works.\n");
2212 timer_through_8259 = 1;
2213 if (nmi_watchdog == NMI_IO_APIC) {
2214 disable_8259A_irq(0);
2215 setup_nmi();
2216 enable_8259A_irq(0);
2218 goto out;
2221 * Cleanup, just in case ...
2223 disable_8259A_irq(0);
2224 clear_IO_APIC_pin(apic2, pin2);
2225 printk(" failed.\n");
2228 if (nmi_watchdog == NMI_IO_APIC) {
2229 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
2230 nmi_watchdog = NMI_NONE;
2232 timer_ack = 0;
2234 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2236 set_irq_chip_and_handler_name(0, &lapic_chip, handle_fasteoi_irq,
2237 "fasteoi");
2238 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
2239 enable_8259A_irq(0);
2241 if (timer_irq_works()) {
2242 printk(" works.\n");
2243 goto out;
2245 disable_8259A_irq(0);
2246 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
2247 printk(" failed.\n");
2249 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2251 init_8259A(0);
2252 make_8259A_irq(0);
2253 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
2255 unlock_ExtINT_logic();
2257 if (timer_irq_works()) {
2258 printk(" works.\n");
2259 goto out;
2261 printk(" failed :(.\n");
2262 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2263 "report. Then try booting with the 'noapic' option");
2264 out:
2265 local_irq_restore(flags);
2270 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2271 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2272 * Linux doesn't really care, as it's not actually used
2273 * for any interrupt handling anyway.
2275 #define PIC_IRQS (1 << PIC_CASCADE_IR)
2277 void __init setup_IO_APIC(void)
2279 int i;
2281 /* Reserve all the system vectors. */
2282 for (i = FIRST_SYSTEM_VECTOR; i < NR_VECTORS; i++)
2283 set_bit(i, used_vectors);
2285 enable_IO_APIC();
2287 if (acpi_ioapic)
2288 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2289 else
2290 io_apic_irqs = ~PIC_IRQS;
2292 printk("ENABLING IO-APIC IRQs\n");
2295 * Set up IO-APIC IRQ routing.
2297 if (!acpi_ioapic)
2298 setup_ioapic_ids_from_mpc();
2299 sync_Arb_IDs();
2300 setup_IO_APIC_irqs();
2301 init_IO_APIC_traps();
2302 check_timer();
2303 if (!acpi_ioapic)
2304 print_IO_APIC();
2308 * Called after all the initialization is done. If we didnt find any
2309 * APIC bugs then we can allow the modify fast path
2312 static int __init io_apic_bug_finalize(void)
2314 if (sis_apic_bug == -1)
2315 sis_apic_bug = 0;
2316 return 0;
2319 late_initcall(io_apic_bug_finalize);
2321 struct sysfs_ioapic_data {
2322 struct sys_device dev;
2323 struct IO_APIC_route_entry entry[0];
2325 static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
2327 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2329 struct IO_APIC_route_entry *entry;
2330 struct sysfs_ioapic_data *data;
2331 int i;
2333 data = container_of(dev, struct sysfs_ioapic_data, dev);
2334 entry = data->entry;
2335 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
2336 entry[i] = ioapic_read_entry(dev->id, i);
2338 return 0;
2341 static int ioapic_resume(struct sys_device *dev)
2343 struct IO_APIC_route_entry *entry;
2344 struct sysfs_ioapic_data *data;
2345 unsigned long flags;
2346 union IO_APIC_reg_00 reg_00;
2347 int i;
2349 data = container_of(dev, struct sysfs_ioapic_data, dev);
2350 entry = data->entry;
2352 spin_lock_irqsave(&ioapic_lock, flags);
2353 reg_00.raw = io_apic_read(dev->id, 0);
2354 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
2355 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
2356 io_apic_write(dev->id, 0, reg_00.raw);
2358 spin_unlock_irqrestore(&ioapic_lock, flags);
2359 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
2360 ioapic_write_entry(dev->id, i, entry[i]);
2362 return 0;
2365 static struct sysdev_class ioapic_sysdev_class = {
2366 .name = "ioapic",
2367 .suspend = ioapic_suspend,
2368 .resume = ioapic_resume,
2371 static int __init ioapic_init_sysfs(void)
2373 struct sys_device *dev;
2374 int i, size, error = 0;
2376 error = sysdev_class_register(&ioapic_sysdev_class);
2377 if (error)
2378 return error;
2380 for (i = 0; i < nr_ioapics; i++) {
2381 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2382 * sizeof(struct IO_APIC_route_entry);
2383 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
2384 if (!mp_ioapic_data[i]) {
2385 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2386 continue;
2388 dev = &mp_ioapic_data[i]->dev;
2389 dev->id = i;
2390 dev->cls = &ioapic_sysdev_class;
2391 error = sysdev_register(dev);
2392 if (error) {
2393 kfree(mp_ioapic_data[i]);
2394 mp_ioapic_data[i] = NULL;
2395 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2396 continue;
2400 return 0;
2403 device_initcall(ioapic_init_sysfs);
2406 * Dynamic irq allocate and deallocation
2408 int create_irq(void)
2410 /* Allocate an unused irq */
2411 int irq, new, vector = 0;
2412 unsigned long flags;
2414 irq = -ENOSPC;
2415 spin_lock_irqsave(&vector_lock, flags);
2416 for (new = (NR_IRQS - 1); new >= 0; new--) {
2417 if (platform_legacy_irq(new))
2418 continue;
2419 if (irq_vector[new] != 0)
2420 continue;
2421 vector = __assign_irq_vector(new);
2422 if (likely(vector > 0))
2423 irq = new;
2424 break;
2426 spin_unlock_irqrestore(&vector_lock, flags);
2428 if (irq >= 0) {
2429 set_intr_gate(vector, interrupt[irq]);
2430 dynamic_irq_init(irq);
2432 return irq;
2435 void destroy_irq(unsigned int irq)
2437 unsigned long flags;
2439 dynamic_irq_cleanup(irq);
2441 spin_lock_irqsave(&vector_lock, flags);
2442 clear_bit(irq_vector[irq], used_vectors);
2443 irq_vector[irq] = 0;
2444 spin_unlock_irqrestore(&vector_lock, flags);
2448 * MSI message composition
2450 #ifdef CONFIG_PCI_MSI
2451 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
2453 int vector;
2454 unsigned dest;
2456 vector = assign_irq_vector(irq);
2457 if (vector >= 0) {
2458 dest = cpu_mask_to_apicid(TARGET_CPUS);
2460 msg->address_hi = MSI_ADDR_BASE_HI;
2461 msg->address_lo =
2462 MSI_ADDR_BASE_LO |
2463 ((INT_DEST_MODE == 0) ?
2464 MSI_ADDR_DEST_MODE_PHYSICAL:
2465 MSI_ADDR_DEST_MODE_LOGICAL) |
2466 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2467 MSI_ADDR_REDIRECTION_CPU:
2468 MSI_ADDR_REDIRECTION_LOWPRI) |
2469 MSI_ADDR_DEST_ID(dest);
2471 msg->data =
2472 MSI_DATA_TRIGGER_EDGE |
2473 MSI_DATA_LEVEL_ASSERT |
2474 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2475 MSI_DATA_DELIVERY_FIXED:
2476 MSI_DATA_DELIVERY_LOWPRI) |
2477 MSI_DATA_VECTOR(vector);
2479 return vector;
2482 #ifdef CONFIG_SMP
2483 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2485 struct msi_msg msg;
2486 unsigned int dest;
2487 cpumask_t tmp;
2488 int vector;
2490 cpus_and(tmp, mask, cpu_online_map);
2491 if (cpus_empty(tmp))
2492 tmp = TARGET_CPUS;
2494 vector = assign_irq_vector(irq);
2495 if (vector < 0)
2496 return;
2498 dest = cpu_mask_to_apicid(mask);
2500 read_msi_msg(irq, &msg);
2502 msg.data &= ~MSI_DATA_VECTOR_MASK;
2503 msg.data |= MSI_DATA_VECTOR(vector);
2504 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2505 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2507 write_msi_msg(irq, &msg);
2508 irq_desc[irq].affinity = mask;
2510 #endif /* CONFIG_SMP */
2513 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2514 * which implement the MSI or MSI-X Capability Structure.
2516 static struct irq_chip msi_chip = {
2517 .name = "PCI-MSI",
2518 .unmask = unmask_msi_irq,
2519 .mask = mask_msi_irq,
2520 .ack = ack_ioapic_irq,
2521 #ifdef CONFIG_SMP
2522 .set_affinity = set_msi_irq_affinity,
2523 #endif
2524 .retrigger = ioapic_retrigger_irq,
2527 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
2529 struct msi_msg msg;
2530 int irq, ret;
2531 irq = create_irq();
2532 if (irq < 0)
2533 return irq;
2535 ret = msi_compose_msg(dev, irq, &msg);
2536 if (ret < 0) {
2537 destroy_irq(irq);
2538 return ret;
2541 set_irq_msi(irq, desc);
2542 write_msi_msg(irq, &msg);
2544 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2545 "edge");
2547 return 0;
2550 void arch_teardown_msi_irq(unsigned int irq)
2552 destroy_irq(irq);
2555 #endif /* CONFIG_PCI_MSI */
2558 * Hypertransport interrupt support
2560 #ifdef CONFIG_HT_IRQ
2562 #ifdef CONFIG_SMP
2564 static void target_ht_irq(unsigned int irq, unsigned int dest)
2566 struct ht_irq_msg msg;
2567 fetch_ht_irq_msg(irq, &msg);
2569 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2570 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
2572 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2573 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
2575 write_ht_irq_msg(irq, &msg);
2578 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2580 unsigned int dest;
2581 cpumask_t tmp;
2583 cpus_and(tmp, mask, cpu_online_map);
2584 if (cpus_empty(tmp))
2585 tmp = TARGET_CPUS;
2587 cpus_and(mask, tmp, CPU_MASK_ALL);
2589 dest = cpu_mask_to_apicid(mask);
2591 target_ht_irq(irq, dest);
2592 irq_desc[irq].affinity = mask;
2594 #endif
2596 static struct irq_chip ht_irq_chip = {
2597 .name = "PCI-HT",
2598 .mask = mask_ht_irq,
2599 .unmask = unmask_ht_irq,
2600 .ack = ack_ioapic_irq,
2601 #ifdef CONFIG_SMP
2602 .set_affinity = set_ht_irq_affinity,
2603 #endif
2604 .retrigger = ioapic_retrigger_irq,
2607 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2609 int vector;
2611 vector = assign_irq_vector(irq);
2612 if (vector >= 0) {
2613 struct ht_irq_msg msg;
2614 unsigned dest;
2615 cpumask_t tmp;
2617 cpus_clear(tmp);
2618 cpu_set(vector >> 8, tmp);
2619 dest = cpu_mask_to_apicid(tmp);
2621 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
2623 msg.address_lo =
2624 HT_IRQ_LOW_BASE |
2625 HT_IRQ_LOW_DEST_ID(dest) |
2626 HT_IRQ_LOW_VECTOR(vector) |
2627 ((INT_DEST_MODE == 0) ?
2628 HT_IRQ_LOW_DM_PHYSICAL :
2629 HT_IRQ_LOW_DM_LOGICAL) |
2630 HT_IRQ_LOW_RQEOI_EDGE |
2631 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2632 HT_IRQ_LOW_MT_FIXED :
2633 HT_IRQ_LOW_MT_ARBITRATED) |
2634 HT_IRQ_LOW_IRQ_MASKED;
2636 write_ht_irq_msg(irq, &msg);
2638 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2639 handle_edge_irq, "edge");
2641 return vector;
2643 #endif /* CONFIG_HT_IRQ */
2645 /* --------------------------------------------------------------------------
2646 ACPI-based IOAPIC Configuration
2647 -------------------------------------------------------------------------- */
2649 #ifdef CONFIG_ACPI
2651 int __init io_apic_get_unique_id(int ioapic, int apic_id)
2653 union IO_APIC_reg_00 reg_00;
2654 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2655 physid_mask_t tmp;
2656 unsigned long flags;
2657 int i = 0;
2660 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2661 * buses (one for LAPICs, one for IOAPICs), where predecessors only
2662 * supports up to 16 on one shared APIC bus.
2664 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2665 * advantage of new APIC bus architecture.
2668 if (physids_empty(apic_id_map))
2669 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2671 spin_lock_irqsave(&ioapic_lock, flags);
2672 reg_00.raw = io_apic_read(ioapic, 0);
2673 spin_unlock_irqrestore(&ioapic_lock, flags);
2675 if (apic_id >= get_physical_broadcast()) {
2676 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2677 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2678 apic_id = reg_00.bits.ID;
2682 * Every APIC in a system must have a unique ID or we get lots of nice
2683 * 'stuck on smp_invalidate_needed IPI wait' messages.
2685 if (check_apicid_used(apic_id_map, apic_id)) {
2687 for (i = 0; i < get_physical_broadcast(); i++) {
2688 if (!check_apicid_used(apic_id_map, i))
2689 break;
2692 if (i == get_physical_broadcast())
2693 panic("Max apic_id exceeded!\n");
2695 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2696 "trying %d\n", ioapic, apic_id, i);
2698 apic_id = i;
2701 tmp = apicid_to_cpu_present(apic_id);
2702 physids_or(apic_id_map, apic_id_map, tmp);
2704 if (reg_00.bits.ID != apic_id) {
2705 reg_00.bits.ID = apic_id;
2707 spin_lock_irqsave(&ioapic_lock, flags);
2708 io_apic_write(ioapic, 0, reg_00.raw);
2709 reg_00.raw = io_apic_read(ioapic, 0);
2710 spin_unlock_irqrestore(&ioapic_lock, flags);
2712 /* Sanity check */
2713 if (reg_00.bits.ID != apic_id) {
2714 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2715 return -1;
2719 apic_printk(APIC_VERBOSE, KERN_INFO
2720 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2722 return apic_id;
2726 int __init io_apic_get_version(int ioapic)
2728 union IO_APIC_reg_01 reg_01;
2729 unsigned long flags;
2731 spin_lock_irqsave(&ioapic_lock, flags);
2732 reg_01.raw = io_apic_read(ioapic, 1);
2733 spin_unlock_irqrestore(&ioapic_lock, flags);
2735 return reg_01.bits.version;
2739 int __init io_apic_get_redir_entries(int ioapic)
2741 union IO_APIC_reg_01 reg_01;
2742 unsigned long flags;
2744 spin_lock_irqsave(&ioapic_lock, flags);
2745 reg_01.raw = io_apic_read(ioapic, 1);
2746 spin_unlock_irqrestore(&ioapic_lock, flags);
2748 return reg_01.bits.entries;
2752 int io_apic_set_pci_routing(int ioapic, int pin, int irq, int edge_level, int active_high_low)
2754 struct IO_APIC_route_entry entry;
2756 if (!IO_APIC_IRQ(irq)) {
2757 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2758 ioapic);
2759 return -EINVAL;
2763 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2764 * Note that we mask (disable) IRQs now -- these get enabled when the
2765 * corresponding device driver registers for this IRQ.
2768 memset(&entry, 0, sizeof(entry));
2770 entry.delivery_mode = INT_DELIVERY_MODE;
2771 entry.dest_mode = INT_DEST_MODE;
2772 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2773 entry.trigger = edge_level;
2774 entry.polarity = active_high_low;
2775 entry.mask = 1;
2778 * IRQs < 16 are already in the irq_2_pin[] map
2780 if (irq >= 16)
2781 add_pin_to_irq(irq, ioapic, pin);
2783 entry.vector = assign_irq_vector(irq);
2785 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2786 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
2787 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2788 edge_level, active_high_low);
2790 ioapic_register_intr(irq, entry.vector, edge_level);
2792 if (!ioapic && (irq < 16))
2793 disable_8259A_irq(irq);
2795 ioapic_write_entry(ioapic, pin, entry);
2797 return 0;
2800 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2802 int i;
2804 if (skip_ioapic_setup)
2805 return -1;
2807 for (i = 0; i < mp_irq_entries; i++)
2808 if (mp_irqs[i].mpc_irqtype == mp_INT &&
2809 mp_irqs[i].mpc_srcbusirq == bus_irq)
2810 break;
2811 if (i >= mp_irq_entries)
2812 return -1;
2814 *trigger = irq_trigger(i);
2815 *polarity = irq_polarity(i);
2816 return 0;
2819 #endif /* CONFIG_ACPI */
2821 static int __init parse_disable_timer_pin_1(char *arg)
2823 disable_timer_pin_1 = 1;
2824 return 0;
2826 early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2828 static int __init parse_enable_timer_pin_1(char *arg)
2830 disable_timer_pin_1 = -1;
2831 return 0;
2833 early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2835 static int __init parse_noapic(char *arg)
2837 /* disable IO-APIC */
2838 disable_ioapic_setup();
2839 return 0;
2841 early_param("noapic", parse_noapic);