[PATCH] x86_64: serialize assign_irq_vector() use of static variables
[deliverable/linux.git] / arch / i386 / kernel / io_apic.c
CommitLineData
1da177e4
LT
1/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
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>
14 *
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
21 */
22
23#include <linux/mm.h>
1da177e4
LT
24#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
28#include <linux/config.h>
29#include <linux/smp_lock.h>
30#include <linux/mc146818rtc.h>
31#include <linux/compiler.h>
32#include <linux/acpi.h>
129f6946 33#include <linux/module.h>
1da177e4 34#include <linux/sysdev.h>
54d5d424 35
1da177e4
LT
36#include <asm/io.h>
37#include <asm/smp.h>
38#include <asm/desc.h>
39#include <asm/timer.h>
306e440d 40#include <asm/i8259.h>
1da177e4
LT
41
42#include <mach_apic.h>
43
44#include "io_ports.h"
45
46int (*ioapic_renumber_irq)(int ioapic, int irq);
47atomic_t irq_mis_count;
48
fcfd636a
EB
49/* Where if anywhere is the i8259 connect in external int mode */
50static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
51
1da177e4 52static DEFINE_SPINLOCK(ioapic_lock);
0a1ad60d 53static DEFINE_SPINLOCK(vector_lock);
1da177e4 54
f9262c12
AK
55int timer_over_8254 __initdata = 1;
56
1da177e4
LT
57/*
58 * Is the SiS APIC rmw bug present ?
59 * -1 = don't know, 0 = no, 1 = yes
60 */
61int sis_apic_bug = -1;
62
63/*
64 * # of IRQ routing registers
65 */
66int nr_ioapic_registers[MAX_IO_APICS];
67
66759a01
CE
68int disable_timer_pin_1 __initdata;
69
1da177e4
LT
70/*
71 * Rough estimation of how many shared IRQs there are, can
72 * be changed anytime.
73 */
74#define MAX_PLUS_SHARED_IRQS NR_IRQS
75#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
76
77/*
78 * This is performance-critical, we want to do it O(1)
79 *
80 * the indexing order of this array favors 1:1 mappings
81 * between pins and IRQs.
82 */
83
84static struct irq_pin_list {
85 int apic, pin, next;
86} irq_2_pin[PIN_MAP_SIZE];
87
6c231b7b 88int vector_irq[NR_VECTORS] __read_mostly = { [0 ... NR_VECTORS - 1] = -1};
1da177e4
LT
89#ifdef CONFIG_PCI_MSI
90#define vector_to_irq(vector) \
91 (platform_legacy_irq(vector) ? vector : vector_irq[vector])
92#else
93#define vector_to_irq(vector) (vector)
94#endif
95
96/*
97 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
98 * shared ISA-space IRQs, so we have to support them. We are super
99 * fast in the common case, and fast for shared ISA-space IRQs.
100 */
101static void add_pin_to_irq(unsigned int irq, int apic, int pin)
102{
103 static int first_free_entry = NR_IRQS;
104 struct irq_pin_list *entry = irq_2_pin + irq;
105
106 while (entry->next)
107 entry = irq_2_pin + entry->next;
108
109 if (entry->pin != -1) {
110 entry->next = first_free_entry;
111 entry = irq_2_pin + entry->next;
112 if (++first_free_entry >= PIN_MAP_SIZE)
113 panic("io_apic.c: whoops");
114 }
115 entry->apic = apic;
116 entry->pin = pin;
117}
118
119/*
120 * Reroute an IRQ to a different pin.
121 */
122static void __init replace_pin_at_irq(unsigned int irq,
123 int oldapic, int oldpin,
124 int newapic, int newpin)
125{
126 struct irq_pin_list *entry = irq_2_pin + irq;
127
128 while (1) {
129 if (entry->apic == oldapic && entry->pin == oldpin) {
130 entry->apic = newapic;
131 entry->pin = newpin;
132 }
133 if (!entry->next)
134 break;
135 entry = irq_2_pin + entry->next;
136 }
137}
138
139static void __modify_IO_APIC_irq (unsigned int irq, unsigned long enable, unsigned long disable)
140{
141 struct irq_pin_list *entry = irq_2_pin + irq;
142 unsigned int pin, reg;
143
144 for (;;) {
145 pin = entry->pin;
146 if (pin == -1)
147 break;
148 reg = io_apic_read(entry->apic, 0x10 + pin*2);
149 reg &= ~disable;
150 reg |= enable;
151 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
152 if (!entry->next)
153 break;
154 entry = irq_2_pin + entry->next;
155 }
156}
157
158/* mask = 1 */
159static void __mask_IO_APIC_irq (unsigned int irq)
160{
161 __modify_IO_APIC_irq(irq, 0x00010000, 0);
162}
163
164/* mask = 0 */
165static void __unmask_IO_APIC_irq (unsigned int irq)
166{
167 __modify_IO_APIC_irq(irq, 0, 0x00010000);
168}
169
170/* mask = 1, trigger = 0 */
171static void __mask_and_edge_IO_APIC_irq (unsigned int irq)
172{
173 __modify_IO_APIC_irq(irq, 0x00010000, 0x00008000);
174}
175
176/* mask = 0, trigger = 1 */
177static void __unmask_and_level_IO_APIC_irq (unsigned int irq)
178{
179 __modify_IO_APIC_irq(irq, 0x00008000, 0x00010000);
180}
181
182static void mask_IO_APIC_irq (unsigned int irq)
183{
184 unsigned long flags;
185
186 spin_lock_irqsave(&ioapic_lock, flags);
187 __mask_IO_APIC_irq(irq);
188 spin_unlock_irqrestore(&ioapic_lock, flags);
189}
190
191static void unmask_IO_APIC_irq (unsigned int irq)
192{
193 unsigned long flags;
194
195 spin_lock_irqsave(&ioapic_lock, flags);
196 __unmask_IO_APIC_irq(irq);
197 spin_unlock_irqrestore(&ioapic_lock, flags);
198}
199
200static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
201{
202 struct IO_APIC_route_entry entry;
203 unsigned long flags;
204
205 /* Check delivery_mode to be sure we're not clearing an SMI pin */
206 spin_lock_irqsave(&ioapic_lock, flags);
207 *(((int*)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
208 *(((int*)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
209 spin_unlock_irqrestore(&ioapic_lock, flags);
210 if (entry.delivery_mode == dest_SMI)
211 return;
212
213 /*
214 * Disable it in the IO-APIC irq-routing table:
215 */
216 memset(&entry, 0, sizeof(entry));
217 entry.mask = 1;
218 spin_lock_irqsave(&ioapic_lock, flags);
219 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry) + 0));
220 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry) + 1));
221 spin_unlock_irqrestore(&ioapic_lock, flags);
222}
223
224static void clear_IO_APIC (void)
225{
226 int apic, pin;
227
228 for (apic = 0; apic < nr_ioapics; apic++)
229 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
230 clear_IO_APIC_pin(apic, pin);
231}
232
54d5d424 233#ifdef CONFIG_SMP
1da177e4
LT
234static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
235{
236 unsigned long flags;
237 int pin;
238 struct irq_pin_list *entry = irq_2_pin + irq;
239 unsigned int apicid_value;
54d5d424 240 cpumask_t tmp;
1da177e4 241
54d5d424
AR
242 cpus_and(tmp, cpumask, cpu_online_map);
243 if (cpus_empty(tmp))
244 tmp = TARGET_CPUS;
245
246 cpus_and(cpumask, tmp, CPU_MASK_ALL);
247
1da177e4
LT
248 apicid_value = cpu_mask_to_apicid(cpumask);
249 /* Prepare to do the io_apic_write */
250 apicid_value = apicid_value << 24;
251 spin_lock_irqsave(&ioapic_lock, flags);
252 for (;;) {
253 pin = entry->pin;
254 if (pin == -1)
255 break;
256 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
257 if (!entry->next)
258 break;
259 entry = irq_2_pin + entry->next;
260 }
54d5d424 261 set_irq_info(irq, cpumask);
1da177e4
LT
262 spin_unlock_irqrestore(&ioapic_lock, flags);
263}
264
265#if defined(CONFIG_IRQBALANCE)
266# include <asm/processor.h> /* kernel_thread() */
267# include <linux/kernel_stat.h> /* kstat */
268# include <linux/slab.h> /* kmalloc() */
269# include <linux/timer.h> /* time_after() */
270
1b61b910 271#ifdef CONFIG_BALANCED_IRQ_DEBUG
1da177e4
LT
272# define TDprintk(x...) do { printk("<%ld:%s:%d>: ", jiffies, __FILE__, __LINE__); printk(x); } while (0)
273# define Dprintk(x...) do { TDprintk(x); } while (0)
274# else
275# define TDprintk(x...)
276# define Dprintk(x...)
277# endif
278
1da177e4 279#define IRQBALANCE_CHECK_ARCH -999
1b61b910
ZY
280#define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
281#define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
282#define BALANCED_IRQ_MORE_DELTA (HZ/10)
283#define BALANCED_IRQ_LESS_DELTA (HZ)
284
285static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
286static int physical_balance __read_mostly;
287static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
1da177e4
LT
288
289static struct irq_cpu_info {
290 unsigned long * last_irq;
291 unsigned long * irq_delta;
292 unsigned long irq;
293} irq_cpu_data[NR_CPUS];
294
295#define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
296#define LAST_CPU_IRQ(cpu,irq) (irq_cpu_data[cpu].last_irq[irq])
297#define IRQ_DELTA(cpu,irq) (irq_cpu_data[cpu].irq_delta[irq])
298
299#define IDLE_ENOUGH(cpu,now) \
300 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
301
302#define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
303
304#define CPU_TO_PACKAGEINDEX(i) (first_cpu(cpu_sibling_map[i]))
305
1b61b910
ZY
306static cpumask_t balance_irq_affinity[NR_IRQS] = {
307 [0 ... NR_IRQS-1] = CPU_MASK_ALL
308};
1da177e4 309
1b61b910
ZY
310void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
311{
312 balance_irq_affinity[irq] = mask;
313}
1da177e4
LT
314
315static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
316 unsigned long now, int direction)
317{
318 int search_idle = 1;
319 int cpu = curr_cpu;
320
321 goto inside;
322
323 do {
324 if (unlikely(cpu == curr_cpu))
325 search_idle = 0;
326inside:
327 if (direction == 1) {
328 cpu++;
329 if (cpu >= NR_CPUS)
330 cpu = 0;
331 } else {
332 cpu--;
333 if (cpu == -1)
334 cpu = NR_CPUS-1;
335 }
336 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu,allowed_mask) ||
337 (search_idle && !IDLE_ENOUGH(cpu,now)));
338
339 return cpu;
340}
341
342static inline void balance_irq(int cpu, int irq)
343{
344 unsigned long now = jiffies;
345 cpumask_t allowed_mask;
346 unsigned int new_cpu;
347
348 if (irqbalance_disabled)
349 return;
350
1b61b910 351 cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
1da177e4
LT
352 new_cpu = move(cpu, allowed_mask, now, 1);
353 if (cpu != new_cpu) {
54d5d424 354 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
1da177e4
LT
355 }
356}
357
358static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
359{
360 int i, j;
361 Dprintk("Rotating IRQs among CPUs.\n");
394e3902
AM
362 for_each_online_cpu(i) {
363 for (j = 0; j < NR_IRQS; j++) {
1da177e4
LT
364 if (!irq_desc[j].action)
365 continue;
366 /* Is it a significant load ? */
367 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i),j) <
368 useful_load_threshold)
369 continue;
370 balance_irq(i, j);
371 }
372 }
373 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
374 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
375 return;
376}
377
378static void do_irq_balance(void)
379{
380 int i, j;
381 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
382 unsigned long move_this_load = 0;
383 int max_loaded = 0, min_loaded = 0;
384 int load;
385 unsigned long useful_load_threshold = balanced_irq_interval + 10;
386 int selected_irq;
387 int tmp_loaded, first_attempt = 1;
388 unsigned long tmp_cpu_irq;
389 unsigned long imbalance = 0;
390 cpumask_t allowed_mask, target_cpu_mask, tmp;
391
c8912599 392 for_each_possible_cpu(i) {
1da177e4
LT
393 int package_index;
394 CPU_IRQ(i) = 0;
395 if (!cpu_online(i))
396 continue;
397 package_index = CPU_TO_PACKAGEINDEX(i);
398 for (j = 0; j < NR_IRQS; j++) {
399 unsigned long value_now, delta;
400 /* Is this an active IRQ? */
401 if (!irq_desc[j].action)
402 continue;
403 if ( package_index == i )
404 IRQ_DELTA(package_index,j) = 0;
405 /* Determine the total count per processor per IRQ */
406 value_now = (unsigned long) kstat_cpu(i).irqs[j];
407
408 /* Determine the activity per processor per IRQ */
409 delta = value_now - LAST_CPU_IRQ(i,j);
410
411 /* Update last_cpu_irq[][] for the next time */
412 LAST_CPU_IRQ(i,j) = value_now;
413
414 /* Ignore IRQs whose rate is less than the clock */
415 if (delta < useful_load_threshold)
416 continue;
417 /* update the load for the processor or package total */
418 IRQ_DELTA(package_index,j) += delta;
419
420 /* Keep track of the higher numbered sibling as well */
421 if (i != package_index)
422 CPU_IRQ(i) += delta;
423 /*
424 * We have sibling A and sibling B in the package
425 *
426 * cpu_irq[A] = load for cpu A + load for cpu B
427 * cpu_irq[B] = load for cpu B
428 */
429 CPU_IRQ(package_index) += delta;
430 }
431 }
432 /* Find the least loaded processor package */
394e3902 433 for_each_online_cpu(i) {
1da177e4
LT
434 if (i != CPU_TO_PACKAGEINDEX(i))
435 continue;
436 if (min_cpu_irq > CPU_IRQ(i)) {
437 min_cpu_irq = CPU_IRQ(i);
438 min_loaded = i;
439 }
440 }
441 max_cpu_irq = ULONG_MAX;
442
443tryanothercpu:
444 /* Look for heaviest loaded processor.
445 * We may come back to get the next heaviest loaded processor.
446 * Skip processors with trivial loads.
447 */
448 tmp_cpu_irq = 0;
449 tmp_loaded = -1;
394e3902 450 for_each_online_cpu(i) {
1da177e4
LT
451 if (i != CPU_TO_PACKAGEINDEX(i))
452 continue;
453 if (max_cpu_irq <= CPU_IRQ(i))
454 continue;
455 if (tmp_cpu_irq < CPU_IRQ(i)) {
456 tmp_cpu_irq = CPU_IRQ(i);
457 tmp_loaded = i;
458 }
459 }
460
461 if (tmp_loaded == -1) {
462 /* In the case of small number of heavy interrupt sources,
463 * loading some of the cpus too much. We use Ingo's original
464 * approach to rotate them around.
465 */
466 if (!first_attempt && imbalance >= useful_load_threshold) {
467 rotate_irqs_among_cpus(useful_load_threshold);
468 return;
469 }
470 goto not_worth_the_effort;
471 }
472
473 first_attempt = 0; /* heaviest search */
474 max_cpu_irq = tmp_cpu_irq; /* load */
475 max_loaded = tmp_loaded; /* processor */
476 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
477
478 Dprintk("max_loaded cpu = %d\n", max_loaded);
479 Dprintk("min_loaded cpu = %d\n", min_loaded);
480 Dprintk("max_cpu_irq load = %ld\n", max_cpu_irq);
481 Dprintk("min_cpu_irq load = %ld\n", min_cpu_irq);
482 Dprintk("load imbalance = %lu\n", imbalance);
483
484 /* if imbalance is less than approx 10% of max load, then
485 * observe diminishing returns action. - quit
486 */
487 if (imbalance < (max_cpu_irq >> 3)) {
488 Dprintk("Imbalance too trivial\n");
489 goto not_worth_the_effort;
490 }
491
492tryanotherirq:
493 /* if we select an IRQ to move that can't go where we want, then
494 * see if there is another one to try.
495 */
496 move_this_load = 0;
497 selected_irq = -1;
498 for (j = 0; j < NR_IRQS; j++) {
499 /* Is this an active IRQ? */
500 if (!irq_desc[j].action)
501 continue;
502 if (imbalance <= IRQ_DELTA(max_loaded,j))
503 continue;
504 /* Try to find the IRQ that is closest to the imbalance
505 * without going over.
506 */
507 if (move_this_load < IRQ_DELTA(max_loaded,j)) {
508 move_this_load = IRQ_DELTA(max_loaded,j);
509 selected_irq = j;
510 }
511 }
512 if (selected_irq == -1) {
513 goto tryanothercpu;
514 }
515
516 imbalance = move_this_load;
517
518 /* For physical_balance case, we accumlated both load
519 * values in the one of the siblings cpu_irq[],
520 * to use the same code for physical and logical processors
521 * as much as possible.
522 *
523 * NOTE: the cpu_irq[] array holds the sum of the load for
524 * sibling A and sibling B in the slot for the lowest numbered
525 * sibling (A), _AND_ the load for sibling B in the slot for
526 * the higher numbered sibling.
527 *
528 * We seek the least loaded sibling by making the comparison
529 * (A+B)/2 vs B
530 */
531 load = CPU_IRQ(min_loaded) >> 1;
532 for_each_cpu_mask(j, cpu_sibling_map[min_loaded]) {
533 if (load > CPU_IRQ(j)) {
534 /* This won't change cpu_sibling_map[min_loaded] */
535 load = CPU_IRQ(j);
536 min_loaded = j;
537 }
538 }
539
1b61b910
ZY
540 cpus_and(allowed_mask,
541 cpu_online_map,
542 balance_irq_affinity[selected_irq]);
1da177e4
LT
543 target_cpu_mask = cpumask_of_cpu(min_loaded);
544 cpus_and(tmp, target_cpu_mask, allowed_mask);
545
546 if (!cpus_empty(tmp)) {
1da177e4
LT
547
548 Dprintk("irq = %d moved to cpu = %d\n",
549 selected_irq, min_loaded);
550 /* mark for change destination */
54d5d424
AR
551 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
552
1da177e4
LT
553 /* Since we made a change, come back sooner to
554 * check for more variation.
555 */
556 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
557 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
558 return;
559 }
560 goto tryanotherirq;
561
562not_worth_the_effort:
563 /*
564 * if we did not find an IRQ to move, then adjust the time interval
565 * upward
566 */
567 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
568 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
569 Dprintk("IRQ worth rotating not found\n");
570 return;
571}
572
573static int balanced_irq(void *unused)
574{
575 int i;
576 unsigned long prev_balance_time = jiffies;
577 long time_remaining = balanced_irq_interval;
578
579 daemonize("kirqd");
580
581 /* push everything to CPU 0 to give us a starting point. */
582 for (i = 0 ; i < NR_IRQS ; i++) {
54d5d424
AR
583 pending_irq_cpumask[i] = cpumask_of_cpu(0);
584 set_pending_irq(i, cpumask_of_cpu(0));
1da177e4
LT
585 }
586
587 for ( ; ; ) {
52e6e630 588 time_remaining = schedule_timeout_interruptible(time_remaining);
3e1d1d28 589 try_to_freeze();
1da177e4
LT
590 if (time_after(jiffies,
591 prev_balance_time+balanced_irq_interval)) {
f3705136 592 preempt_disable();
1da177e4
LT
593 do_irq_balance();
594 prev_balance_time = jiffies;
595 time_remaining = balanced_irq_interval;
f3705136 596 preempt_enable();
1da177e4
LT
597 }
598 }
599 return 0;
600}
601
602static int __init balanced_irq_init(void)
603{
604 int i;
605 struct cpuinfo_x86 *c;
606 cpumask_t tmp;
607
608 cpus_shift_right(tmp, cpu_online_map, 2);
609 c = &boot_cpu_data;
610 /* When not overwritten by the command line ask subarchitecture. */
611 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
612 irqbalance_disabled = NO_BALANCE_IRQ;
613 if (irqbalance_disabled)
614 return 0;
615
616 /* disable irqbalance completely if there is only one processor online */
617 if (num_online_cpus() < 2) {
618 irqbalance_disabled = 1;
619 return 0;
620 }
621 /*
622 * Enable physical balance only if more than 1 physical processor
623 * is present
624 */
625 if (smp_num_siblings > 1 && !cpus_empty(tmp))
626 physical_balance = 1;
627
394e3902 628 for_each_online_cpu(i) {
1da177e4
LT
629 irq_cpu_data[i].irq_delta = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
630 irq_cpu_data[i].last_irq = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
631 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
632 printk(KERN_ERR "balanced_irq_init: out of memory");
633 goto failed;
634 }
635 memset(irq_cpu_data[i].irq_delta,0,sizeof(unsigned long) * NR_IRQS);
636 memset(irq_cpu_data[i].last_irq,0,sizeof(unsigned long) * NR_IRQS);
637 }
638
639 printk(KERN_INFO "Starting balanced_irq\n");
640 if (kernel_thread(balanced_irq, NULL, CLONE_KERNEL) >= 0)
641 return 0;
642 else
643 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
644failed:
c8912599 645 for_each_possible_cpu(i) {
4ae6673e 646 kfree(irq_cpu_data[i].irq_delta);
394e3902 647 irq_cpu_data[i].irq_delta = NULL;
4ae6673e 648 kfree(irq_cpu_data[i].last_irq);
394e3902 649 irq_cpu_data[i].last_irq = NULL;
1da177e4
LT
650 }
651 return 0;
652}
653
654int __init irqbalance_disable(char *str)
655{
656 irqbalance_disabled = 1;
9b41046c 657 return 1;
1da177e4
LT
658}
659
660__setup("noirqbalance", irqbalance_disable);
661
1da177e4 662late_initcall(balanced_irq_init);
1da177e4 663#endif /* CONFIG_IRQBALANCE */
54d5d424 664#endif /* CONFIG_SMP */
1da177e4
LT
665
666#ifndef CONFIG_SMP
667void fastcall send_IPI_self(int vector)
668{
669 unsigned int cfg;
670
671 /*
672 * Wait for idle.
673 */
674 apic_wait_icr_idle();
675 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
676 /*
677 * Send the IPI. The write to APIC_ICR fires this off.
678 */
679 apic_write_around(APIC_ICR, cfg);
680}
681#endif /* !CONFIG_SMP */
682
683
684/*
685 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
686 * specific CPU-side IRQs.
687 */
688
689#define MAX_PIRQS 8
690static int pirq_entries [MAX_PIRQS];
691static int pirqs_enabled;
692int skip_ioapic_setup;
693
694static int __init ioapic_setup(char *str)
695{
696 skip_ioapic_setup = 1;
697 return 1;
698}
699
700__setup("noapic", ioapic_setup);
701
702static int __init ioapic_pirq_setup(char *str)
703{
704 int i, max;
705 int ints[MAX_PIRQS+1];
706
707 get_options(str, ARRAY_SIZE(ints), ints);
708
709 for (i = 0; i < MAX_PIRQS; i++)
710 pirq_entries[i] = -1;
711
712 pirqs_enabled = 1;
713 apic_printk(APIC_VERBOSE, KERN_INFO
714 "PIRQ redirection, working around broken MP-BIOS.\n");
715 max = MAX_PIRQS;
716 if (ints[0] < MAX_PIRQS)
717 max = ints[0];
718
719 for (i = 0; i < max; i++) {
720 apic_printk(APIC_VERBOSE, KERN_DEBUG
721 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
722 /*
723 * PIRQs are mapped upside down, usually.
724 */
725 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
726 }
727 return 1;
728}
729
730__setup("pirq=", ioapic_pirq_setup);
731
732/*
733 * Find the IRQ entry number of a certain pin.
734 */
735static int find_irq_entry(int apic, int pin, int type)
736{
737 int i;
738
739 for (i = 0; i < mp_irq_entries; i++)
740 if (mp_irqs[i].mpc_irqtype == type &&
741 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
742 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
743 mp_irqs[i].mpc_dstirq == pin)
744 return i;
745
746 return -1;
747}
748
749/*
750 * Find the pin to which IRQ[irq] (ISA) is connected
751 */
fcfd636a 752static int __init find_isa_irq_pin(int irq, int type)
1da177e4
LT
753{
754 int i;
755
756 for (i = 0; i < mp_irq_entries; i++) {
757 int lbus = mp_irqs[i].mpc_srcbus;
758
759 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
760 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
761 mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
762 mp_bus_id_to_type[lbus] == MP_BUS_NEC98
763 ) &&
764 (mp_irqs[i].mpc_irqtype == type) &&
765 (mp_irqs[i].mpc_srcbusirq == irq))
766
767 return mp_irqs[i].mpc_dstirq;
768 }
769 return -1;
770}
771
fcfd636a
EB
772static int __init find_isa_irq_apic(int irq, int type)
773{
774 int i;
775
776 for (i = 0; i < mp_irq_entries; i++) {
777 int lbus = mp_irqs[i].mpc_srcbus;
778
779 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
780 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
781 mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
782 mp_bus_id_to_type[lbus] == MP_BUS_NEC98
783 ) &&
784 (mp_irqs[i].mpc_irqtype == type) &&
785 (mp_irqs[i].mpc_srcbusirq == irq))
786 break;
787 }
788 if (i < mp_irq_entries) {
789 int apic;
790 for(apic = 0; apic < nr_ioapics; apic++) {
791 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
792 return apic;
793 }
794 }
795
796 return -1;
797}
798
1da177e4
LT
799/*
800 * Find a specific PCI IRQ entry.
801 * Not an __init, possibly needed by modules
802 */
803static int pin_2_irq(int idx, int apic, int pin);
804
805int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
806{
807 int apic, i, best_guess = -1;
808
809 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
810 "slot:%d, pin:%d.\n", bus, slot, pin);
811 if (mp_bus_id_to_pci_bus[bus] == -1) {
812 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
813 return -1;
814 }
815 for (i = 0; i < mp_irq_entries; i++) {
816 int lbus = mp_irqs[i].mpc_srcbus;
817
818 for (apic = 0; apic < nr_ioapics; apic++)
819 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
820 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
821 break;
822
823 if ((mp_bus_id_to_type[lbus] == MP_BUS_PCI) &&
824 !mp_irqs[i].mpc_irqtype &&
825 (bus == lbus) &&
826 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
827 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
828
829 if (!(apic || IO_APIC_IRQ(irq)))
830 continue;
831
832 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
833 return irq;
834 /*
835 * Use the first all-but-pin matching entry as a
836 * best-guess fuzzy result for broken mptables.
837 */
838 if (best_guess < 0)
839 best_guess = irq;
840 }
841 }
842 return best_guess;
843}
129f6946 844EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1da177e4
LT
845
846/*
847 * This function currently is only a helper for the i386 smp boot process where
848 * we need to reprogram the ioredtbls to cater for the cpus which have come online
849 * so mask in all cases should simply be TARGET_CPUS
850 */
54d5d424 851#ifdef CONFIG_SMP
1da177e4
LT
852void __init setup_ioapic_dest(void)
853{
854 int pin, ioapic, irq, irq_entry;
855
856 if (skip_ioapic_setup == 1)
857 return;
858
859 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
860 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
861 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
862 if (irq_entry == -1)
863 continue;
864 irq = pin_2_irq(irq_entry, ioapic, pin);
865 set_ioapic_affinity_irq(irq, TARGET_CPUS);
866 }
867
868 }
869}
54d5d424 870#endif
1da177e4
LT
871
872/*
873 * EISA Edge/Level control register, ELCR
874 */
875static int EISA_ELCR(unsigned int irq)
876{
877 if (irq < 16) {
878 unsigned int port = 0x4d0 + (irq >> 3);
879 return (inb(port) >> (irq & 7)) & 1;
880 }
881 apic_printk(APIC_VERBOSE, KERN_INFO
882 "Broken MPtable reports ISA irq %d\n", irq);
883 return 0;
884}
885
886/* EISA interrupts are always polarity zero and can be edge or level
887 * trigger depending on the ELCR value. If an interrupt is listed as
888 * EISA conforming in the MP table, that means its trigger type must
889 * be read in from the ELCR */
890
891#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
892#define default_EISA_polarity(idx) (0)
893
894/* ISA interrupts are always polarity zero edge triggered,
895 * when listed as conforming in the MP table. */
896
897#define default_ISA_trigger(idx) (0)
898#define default_ISA_polarity(idx) (0)
899
900/* PCI interrupts are always polarity one level triggered,
901 * when listed as conforming in the MP table. */
902
903#define default_PCI_trigger(idx) (1)
904#define default_PCI_polarity(idx) (1)
905
906/* MCA interrupts are always polarity zero level triggered,
907 * when listed as conforming in the MP table. */
908
909#define default_MCA_trigger(idx) (1)
910#define default_MCA_polarity(idx) (0)
911
912/* NEC98 interrupts are always polarity zero edge triggered,
913 * when listed as conforming in the MP table. */
914
915#define default_NEC98_trigger(idx) (0)
916#define default_NEC98_polarity(idx) (0)
917
918static int __init MPBIOS_polarity(int idx)
919{
920 int bus = mp_irqs[idx].mpc_srcbus;
921 int polarity;
922
923 /*
924 * Determine IRQ line polarity (high active or low active):
925 */
926 switch (mp_irqs[idx].mpc_irqflag & 3)
927 {
928 case 0: /* conforms, ie. bus-type dependent polarity */
929 {
930 switch (mp_bus_id_to_type[bus])
931 {
932 case MP_BUS_ISA: /* ISA pin */
933 {
934 polarity = default_ISA_polarity(idx);
935 break;
936 }
937 case MP_BUS_EISA: /* EISA pin */
938 {
939 polarity = default_EISA_polarity(idx);
940 break;
941 }
942 case MP_BUS_PCI: /* PCI pin */
943 {
944 polarity = default_PCI_polarity(idx);
945 break;
946 }
947 case MP_BUS_MCA: /* MCA pin */
948 {
949 polarity = default_MCA_polarity(idx);
950 break;
951 }
952 case MP_BUS_NEC98: /* NEC 98 pin */
953 {
954 polarity = default_NEC98_polarity(idx);
955 break;
956 }
957 default:
958 {
959 printk(KERN_WARNING "broken BIOS!!\n");
960 polarity = 1;
961 break;
962 }
963 }
964 break;
965 }
966 case 1: /* high active */
967 {
968 polarity = 0;
969 break;
970 }
971 case 2: /* reserved */
972 {
973 printk(KERN_WARNING "broken BIOS!!\n");
974 polarity = 1;
975 break;
976 }
977 case 3: /* low active */
978 {
979 polarity = 1;
980 break;
981 }
982 default: /* invalid */
983 {
984 printk(KERN_WARNING "broken BIOS!!\n");
985 polarity = 1;
986 break;
987 }
988 }
989 return polarity;
990}
991
992static int MPBIOS_trigger(int idx)
993{
994 int bus = mp_irqs[idx].mpc_srcbus;
995 int trigger;
996
997 /*
998 * Determine IRQ trigger mode (edge or level sensitive):
999 */
1000 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
1001 {
1002 case 0: /* conforms, ie. bus-type dependent */
1003 {
1004 switch (mp_bus_id_to_type[bus])
1005 {
1006 case MP_BUS_ISA: /* ISA pin */
1007 {
1008 trigger = default_ISA_trigger(idx);
1009 break;
1010 }
1011 case MP_BUS_EISA: /* EISA pin */
1012 {
1013 trigger = default_EISA_trigger(idx);
1014 break;
1015 }
1016 case MP_BUS_PCI: /* PCI pin */
1017 {
1018 trigger = default_PCI_trigger(idx);
1019 break;
1020 }
1021 case MP_BUS_MCA: /* MCA pin */
1022 {
1023 trigger = default_MCA_trigger(idx);
1024 break;
1025 }
1026 case MP_BUS_NEC98: /* NEC 98 pin */
1027 {
1028 trigger = default_NEC98_trigger(idx);
1029 break;
1030 }
1031 default:
1032 {
1033 printk(KERN_WARNING "broken BIOS!!\n");
1034 trigger = 1;
1035 break;
1036 }
1037 }
1038 break;
1039 }
1040 case 1: /* edge */
1041 {
1042 trigger = 0;
1043 break;
1044 }
1045 case 2: /* reserved */
1046 {
1047 printk(KERN_WARNING "broken BIOS!!\n");
1048 trigger = 1;
1049 break;
1050 }
1051 case 3: /* level */
1052 {
1053 trigger = 1;
1054 break;
1055 }
1056 default: /* invalid */
1057 {
1058 printk(KERN_WARNING "broken BIOS!!\n");
1059 trigger = 0;
1060 break;
1061 }
1062 }
1063 return trigger;
1064}
1065
1066static inline int irq_polarity(int idx)
1067{
1068 return MPBIOS_polarity(idx);
1069}
1070
1071static inline int irq_trigger(int idx)
1072{
1073 return MPBIOS_trigger(idx);
1074}
1075
1076static int pin_2_irq(int idx, int apic, int pin)
1077{
1078 int irq, i;
1079 int bus = mp_irqs[idx].mpc_srcbus;
1080
1081 /*
1082 * Debugging check, we are in big trouble if this message pops up!
1083 */
1084 if (mp_irqs[idx].mpc_dstirq != pin)
1085 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1086
1087 switch (mp_bus_id_to_type[bus])
1088 {
1089 case MP_BUS_ISA: /* ISA pin */
1090 case MP_BUS_EISA:
1091 case MP_BUS_MCA:
1092 case MP_BUS_NEC98:
1093 {
1094 irq = mp_irqs[idx].mpc_srcbusirq;
1095 break;
1096 }
1097 case MP_BUS_PCI: /* PCI pin */
1098 {
1099 /*
1100 * PCI IRQs are mapped in order
1101 */
1102 i = irq = 0;
1103 while (i < apic)
1104 irq += nr_ioapic_registers[i++];
1105 irq += pin;
1106
1107 /*
1108 * For MPS mode, so far only needed by ES7000 platform
1109 */
1110 if (ioapic_renumber_irq)
1111 irq = ioapic_renumber_irq(apic, irq);
1112
1113 break;
1114 }
1115 default:
1116 {
1117 printk(KERN_ERR "unknown bus type %d.\n",bus);
1118 irq = 0;
1119 break;
1120 }
1121 }
1122
1123 /*
1124 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1125 */
1126 if ((pin >= 16) && (pin <= 23)) {
1127 if (pirq_entries[pin-16] != -1) {
1128 if (!pirq_entries[pin-16]) {
1129 apic_printk(APIC_VERBOSE, KERN_DEBUG
1130 "disabling PIRQ%d\n", pin-16);
1131 } else {
1132 irq = pirq_entries[pin-16];
1133 apic_printk(APIC_VERBOSE, KERN_DEBUG
1134 "using PIRQ%d -> IRQ %d\n",
1135 pin-16, irq);
1136 }
1137 }
1138 }
1139 return irq;
1140}
1141
1142static inline int IO_APIC_irq_trigger(int irq)
1143{
1144 int apic, idx, pin;
1145
1146 for (apic = 0; apic < nr_ioapics; apic++) {
1147 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1148 idx = find_irq_entry(apic,pin,mp_INT);
1149 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
1150 return irq_trigger(idx);
1151 }
1152 }
1153 /*
1154 * nonexistent IRQs are edge default
1155 */
1156 return 0;
1157}
1158
1159/* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
6c231b7b 1160u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
1da177e4
LT
1161
1162int assign_irq_vector(int irq)
1163{
1164 static int current_vector = FIRST_DEVICE_VECTOR, offset = 0;
0a1ad60d 1165 int vector;
1da177e4 1166
0a1ad60d
JB
1167 BUG_ON(irq != AUTO_ASSIGN && (unsigned)irq >= NR_IRQ_VECTORS);
1168
1169 spin_lock(&vector_lock);
1170
1171 if (irq != AUTO_ASSIGN && IO_APIC_VECTOR(irq) > 0) {
1172 spin_unlock(&vector_lock);
1da177e4 1173 return IO_APIC_VECTOR(irq);
0a1ad60d 1174 }
1da177e4
LT
1175next:
1176 current_vector += 8;
1177 if (current_vector == SYSCALL_VECTOR)
1178 goto next;
1179
1180 if (current_vector >= FIRST_SYSTEM_VECTOR) {
1181 offset++;
0a1ad60d
JB
1182 if (!(offset%8)) {
1183 spin_unlock(&vector_lock);
1da177e4 1184 return -ENOSPC;
0a1ad60d 1185 }
1da177e4
LT
1186 current_vector = FIRST_DEVICE_VECTOR + offset;
1187 }
1188
0a1ad60d
JB
1189 vector = current_vector;
1190 vector_irq[vector] = irq;
1da177e4 1191 if (irq != AUTO_ASSIGN)
0a1ad60d
JB
1192 IO_APIC_VECTOR(irq) = vector;
1193
1194 spin_unlock(&vector_lock);
1da177e4 1195
0a1ad60d 1196 return vector;
1da177e4
LT
1197}
1198
1199static struct hw_interrupt_type ioapic_level_type;
1200static struct hw_interrupt_type ioapic_edge_type;
1201
1202#define IOAPIC_AUTO -1
1203#define IOAPIC_EDGE 0
1204#define IOAPIC_LEVEL 1
1205
1206static inline void ioapic_register_intr(int irq, int vector, unsigned long trigger)
1207{
1208 if (use_pci_vector() && !platform_legacy_irq(irq)) {
1209 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1210 trigger == IOAPIC_LEVEL)
1211 irq_desc[vector].handler = &ioapic_level_type;
1212 else
1213 irq_desc[vector].handler = &ioapic_edge_type;
1214 set_intr_gate(vector, interrupt[vector]);
1215 } else {
1216 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1217 trigger == IOAPIC_LEVEL)
1218 irq_desc[irq].handler = &ioapic_level_type;
1219 else
1220 irq_desc[irq].handler = &ioapic_edge_type;
1221 set_intr_gate(vector, interrupt[irq]);
1222 }
1223}
1224
1225static void __init setup_IO_APIC_irqs(void)
1226{
1227 struct IO_APIC_route_entry entry;
1228 int apic, pin, idx, irq, first_notcon = 1, vector;
1229 unsigned long flags;
1230
1231 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1232
1233 for (apic = 0; apic < nr_ioapics; apic++) {
1234 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1235
1236 /*
1237 * add it to the IO-APIC irq-routing table:
1238 */
1239 memset(&entry,0,sizeof(entry));
1240
1241 entry.delivery_mode = INT_DELIVERY_MODE;
1242 entry.dest_mode = INT_DEST_MODE;
1243 entry.mask = 0; /* enable IRQ */
1244 entry.dest.logical.logical_dest =
1245 cpu_mask_to_apicid(TARGET_CPUS);
1246
1247 idx = find_irq_entry(apic,pin,mp_INT);
1248 if (idx == -1) {
1249 if (first_notcon) {
1250 apic_printk(APIC_VERBOSE, KERN_DEBUG
1251 " IO-APIC (apicid-pin) %d-%d",
1252 mp_ioapics[apic].mpc_apicid,
1253 pin);
1254 first_notcon = 0;
1255 } else
1256 apic_printk(APIC_VERBOSE, ", %d-%d",
1257 mp_ioapics[apic].mpc_apicid, pin);
1258 continue;
1259 }
1260
1261 entry.trigger = irq_trigger(idx);
1262 entry.polarity = irq_polarity(idx);
1263
1264 if (irq_trigger(idx)) {
1265 entry.trigger = 1;
1266 entry.mask = 1;
1267 }
1268
1269 irq = pin_2_irq(idx, apic, pin);
1270 /*
1271 * skip adding the timer int on secondary nodes, which causes
1272 * a small but painful rift in the time-space continuum
1273 */
1274 if (multi_timer_check(apic, irq))
1275 continue;
1276 else
1277 add_pin_to_irq(irq, apic, pin);
1278
1279 if (!apic && !IO_APIC_IRQ(irq))
1280 continue;
1281
1282 if (IO_APIC_IRQ(irq)) {
1283 vector = assign_irq_vector(irq);
1284 entry.vector = vector;
1285 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
1286
1287 if (!apic && (irq < 16))
1288 disable_8259A_irq(irq);
1289 }
1290 spin_lock_irqsave(&ioapic_lock, flags);
1291 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
1292 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
54d5d424 1293 set_native_irq_info(irq, TARGET_CPUS);
1da177e4
LT
1294 spin_unlock_irqrestore(&ioapic_lock, flags);
1295 }
1296 }
1297
1298 if (!first_notcon)
1299 apic_printk(APIC_VERBOSE, " not connected.\n");
1300}
1301
1302/*
1303 * Set up the 8259A-master output pin:
1304 */
fcfd636a 1305static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
1da177e4
LT
1306{
1307 struct IO_APIC_route_entry entry;
1308 unsigned long flags;
1309
1310 memset(&entry,0,sizeof(entry));
1311
1312 disable_8259A_irq(0);
1313
1314 /* mask LVT0 */
1315 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1316
1317 /*
1318 * We use logical delivery to get the timer IRQ
1319 * to the first CPU.
1320 */
1321 entry.dest_mode = INT_DEST_MODE;
1322 entry.mask = 0; /* unmask IRQ now */
1323 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1324 entry.delivery_mode = INT_DELIVERY_MODE;
1325 entry.polarity = 0;
1326 entry.trigger = 0;
1327 entry.vector = vector;
1328
1329 /*
1330 * The timer IRQ doesn't have to know that behind the
1331 * scene we have a 8259A-master in AEOI mode ...
1332 */
1333 irq_desc[0].handler = &ioapic_edge_type;
1334
1335 /*
1336 * Add it to the IO-APIC irq-routing table:
1337 */
1338 spin_lock_irqsave(&ioapic_lock, flags);
fcfd636a
EB
1339 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
1340 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
1da177e4
LT
1341 spin_unlock_irqrestore(&ioapic_lock, flags);
1342
1343 enable_8259A_irq(0);
1344}
1345
1346static inline void UNEXPECTED_IO_APIC(void)
1347{
1348}
1349
1350void __init print_IO_APIC(void)
1351{
1352 int apic, i;
1353 union IO_APIC_reg_00 reg_00;
1354 union IO_APIC_reg_01 reg_01;
1355 union IO_APIC_reg_02 reg_02;
1356 union IO_APIC_reg_03 reg_03;
1357 unsigned long flags;
1358
1359 if (apic_verbosity == APIC_QUIET)
1360 return;
1361
1362 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1363 for (i = 0; i < nr_ioapics; i++)
1364 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1365 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
1366
1367 /*
1368 * We are a bit conservative about what we expect. We have to
1369 * know about every hardware change ASAP.
1370 */
1371 printk(KERN_INFO "testing the IO APIC.......................\n");
1372
1373 for (apic = 0; apic < nr_ioapics; apic++) {
1374
1375 spin_lock_irqsave(&ioapic_lock, flags);
1376 reg_00.raw = io_apic_read(apic, 0);
1377 reg_01.raw = io_apic_read(apic, 1);
1378 if (reg_01.bits.version >= 0x10)
1379 reg_02.raw = io_apic_read(apic, 2);
1380 if (reg_01.bits.version >= 0x20)
1381 reg_03.raw = io_apic_read(apic, 3);
1382 spin_unlock_irqrestore(&ioapic_lock, flags);
1383
1384 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1385 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1386 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1387 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1388 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1389 if (reg_00.bits.ID >= get_physical_broadcast())
1390 UNEXPECTED_IO_APIC();
1391 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
1392 UNEXPECTED_IO_APIC();
1393
1394 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1395 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1396 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
1397 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
1398 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
1399 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1400 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1401 (reg_01.bits.entries != 0x2E) &&
1402 (reg_01.bits.entries != 0x3F)
1403 )
1404 UNEXPECTED_IO_APIC();
1405
1406 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1407 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1408 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1409 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1410 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1411 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1412 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1413 )
1414 UNEXPECTED_IO_APIC();
1415 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1416 UNEXPECTED_IO_APIC();
1417
1418 /*
1419 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1420 * but the value of reg_02 is read as the previous read register
1421 * value, so ignore it if reg_02 == reg_01.
1422 */
1423 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1424 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1425 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1426 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1427 UNEXPECTED_IO_APIC();
1428 }
1429
1430 /*
1431 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1432 * or reg_03, but the value of reg_0[23] is read as the previous read
1433 * register value, so ignore it if reg_03 == reg_0[12].
1434 */
1435 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1436 reg_03.raw != reg_01.raw) {
1437 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1438 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1439 if (reg_03.bits.__reserved_1)
1440 UNEXPECTED_IO_APIC();
1441 }
1442
1443 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1444
1445 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1446 " Stat Dest Deli Vect: \n");
1447
1448 for (i = 0; i <= reg_01.bits.entries; i++) {
1449 struct IO_APIC_route_entry entry;
1450
1451 spin_lock_irqsave(&ioapic_lock, flags);
1452 *(((int *)&entry)+0) = io_apic_read(apic, 0x10+i*2);
1453 *(((int *)&entry)+1) = io_apic_read(apic, 0x11+i*2);
1454 spin_unlock_irqrestore(&ioapic_lock, flags);
1455
1456 printk(KERN_DEBUG " %02x %03X %02X ",
1457 i,
1458 entry.dest.logical.logical_dest,
1459 entry.dest.physical.physical_dest
1460 );
1461
1462 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1463 entry.mask,
1464 entry.trigger,
1465 entry.irr,
1466 entry.polarity,
1467 entry.delivery_status,
1468 entry.dest_mode,
1469 entry.delivery_mode,
1470 entry.vector
1471 );
1472 }
1473 }
1474 if (use_pci_vector())
1475 printk(KERN_INFO "Using vector-based indexing\n");
1476 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1477 for (i = 0; i < NR_IRQS; i++) {
1478 struct irq_pin_list *entry = irq_2_pin + i;
1479 if (entry->pin < 0)
1480 continue;
1481 if (use_pci_vector() && !platform_legacy_irq(i))
1482 printk(KERN_DEBUG "IRQ%d ", IO_APIC_VECTOR(i));
1483 else
1484 printk(KERN_DEBUG "IRQ%d ", i);
1485 for (;;) {
1486 printk("-> %d:%d", entry->apic, entry->pin);
1487 if (!entry->next)
1488 break;
1489 entry = irq_2_pin + entry->next;
1490 }
1491 printk("\n");
1492 }
1493
1494 printk(KERN_INFO ".................................... done.\n");
1495
1496 return;
1497}
1498
1499#if 0
1500
1501static void print_APIC_bitfield (int base)
1502{
1503 unsigned int v;
1504 int i, j;
1505
1506 if (apic_verbosity == APIC_QUIET)
1507 return;
1508
1509 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1510 for (i = 0; i < 8; i++) {
1511 v = apic_read(base + i*0x10);
1512 for (j = 0; j < 32; j++) {
1513 if (v & (1<<j))
1514 printk("1");
1515 else
1516 printk("0");
1517 }
1518 printk("\n");
1519 }
1520}
1521
1522void /*__init*/ print_local_APIC(void * dummy)
1523{
1524 unsigned int v, ver, maxlvt;
1525
1526 if (apic_verbosity == APIC_QUIET)
1527 return;
1528
1529 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1530 smp_processor_id(), hard_smp_processor_id());
1531 v = apic_read(APIC_ID);
1532 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1533 v = apic_read(APIC_LVR);
1534 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1535 ver = GET_APIC_VERSION(v);
1536 maxlvt = get_maxlvt();
1537
1538 v = apic_read(APIC_TASKPRI);
1539 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1540
1541 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1542 v = apic_read(APIC_ARBPRI);
1543 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1544 v & APIC_ARBPRI_MASK);
1545 v = apic_read(APIC_PROCPRI);
1546 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1547 }
1548
1549 v = apic_read(APIC_EOI);
1550 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1551 v = apic_read(APIC_RRR);
1552 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1553 v = apic_read(APIC_LDR);
1554 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1555 v = apic_read(APIC_DFR);
1556 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1557 v = apic_read(APIC_SPIV);
1558 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1559
1560 printk(KERN_DEBUG "... APIC ISR field:\n");
1561 print_APIC_bitfield(APIC_ISR);
1562 printk(KERN_DEBUG "... APIC TMR field:\n");
1563 print_APIC_bitfield(APIC_TMR);
1564 printk(KERN_DEBUG "... APIC IRR field:\n");
1565 print_APIC_bitfield(APIC_IRR);
1566
1567 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1568 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1569 apic_write(APIC_ESR, 0);
1570 v = apic_read(APIC_ESR);
1571 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1572 }
1573
1574 v = apic_read(APIC_ICR);
1575 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1576 v = apic_read(APIC_ICR2);
1577 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1578
1579 v = apic_read(APIC_LVTT);
1580 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1581
1582 if (maxlvt > 3) { /* PC is LVT#4. */
1583 v = apic_read(APIC_LVTPC);
1584 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1585 }
1586 v = apic_read(APIC_LVT0);
1587 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1588 v = apic_read(APIC_LVT1);
1589 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1590
1591 if (maxlvt > 2) { /* ERR is LVT#3. */
1592 v = apic_read(APIC_LVTERR);
1593 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1594 }
1595
1596 v = apic_read(APIC_TMICT);
1597 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1598 v = apic_read(APIC_TMCCT);
1599 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1600 v = apic_read(APIC_TDCR);
1601 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1602 printk("\n");
1603}
1604
1605void print_all_local_APICs (void)
1606{
1607 on_each_cpu(print_local_APIC, NULL, 1, 1);
1608}
1609
1610void /*__init*/ print_PIC(void)
1611{
1da177e4
LT
1612 unsigned int v;
1613 unsigned long flags;
1614
1615 if (apic_verbosity == APIC_QUIET)
1616 return;
1617
1618 printk(KERN_DEBUG "\nprinting PIC contents\n");
1619
1620 spin_lock_irqsave(&i8259A_lock, flags);
1621
1622 v = inb(0xa1) << 8 | inb(0x21);
1623 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1624
1625 v = inb(0xa0) << 8 | inb(0x20);
1626 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1627
1628 outb(0x0b,0xa0);
1629 outb(0x0b,0x20);
1630 v = inb(0xa0) << 8 | inb(0x20);
1631 outb(0x0a,0xa0);
1632 outb(0x0a,0x20);
1633
1634 spin_unlock_irqrestore(&i8259A_lock, flags);
1635
1636 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1637
1638 v = inb(0x4d1) << 8 | inb(0x4d0);
1639 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1640}
1641
1642#endif /* 0 */
1643
1644static void __init enable_IO_APIC(void)
1645{
1646 union IO_APIC_reg_01 reg_01;
fcfd636a
EB
1647 int i8259_apic, i8259_pin;
1648 int i, apic;
1da177e4
LT
1649 unsigned long flags;
1650
1651 for (i = 0; i < PIN_MAP_SIZE; i++) {
1652 irq_2_pin[i].pin = -1;
1653 irq_2_pin[i].next = 0;
1654 }
1655 if (!pirqs_enabled)
1656 for (i = 0; i < MAX_PIRQS; i++)
1657 pirq_entries[i] = -1;
1658
1659 /*
1660 * The number of IO-APIC IRQ registers (== #pins):
1661 */
fcfd636a 1662 for (apic = 0; apic < nr_ioapics; apic++) {
1da177e4 1663 spin_lock_irqsave(&ioapic_lock, flags);
fcfd636a 1664 reg_01.raw = io_apic_read(apic, 1);
1da177e4 1665 spin_unlock_irqrestore(&ioapic_lock, flags);
fcfd636a
EB
1666 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1667 }
1668 for(apic = 0; apic < nr_ioapics; apic++) {
1669 int pin;
1670 /* See if any of the pins is in ExtINT mode */
1008fddc 1671 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
fcfd636a
EB
1672 struct IO_APIC_route_entry entry;
1673 spin_lock_irqsave(&ioapic_lock, flags);
1674 *(((int *)&entry) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1675 *(((int *)&entry) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1676 spin_unlock_irqrestore(&ioapic_lock, flags);
1677
1678
1679 /* If the interrupt line is enabled and in ExtInt mode
1680 * I have found the pin where the i8259 is connected.
1681 */
1682 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1683 ioapic_i8259.apic = apic;
1684 ioapic_i8259.pin = pin;
1685 goto found_i8259;
1686 }
1687 }
1688 }
1689 found_i8259:
1690 /* Look to see what if the MP table has reported the ExtINT */
1691 /* If we could not find the appropriate pin by looking at the ioapic
1692 * the i8259 probably is not connected the ioapic but give the
1693 * mptable a chance anyway.
1694 */
1695 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1696 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1697 /* Trust the MP table if nothing is setup in the hardware */
1698 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1699 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1700 ioapic_i8259.pin = i8259_pin;
1701 ioapic_i8259.apic = i8259_apic;
1702 }
1703 /* Complain if the MP table and the hardware disagree */
1704 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1705 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1706 {
1707 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1da177e4
LT
1708 }
1709
1710 /*
1711 * Do not trust the IO-APIC being empty at bootup
1712 */
1713 clear_IO_APIC();
1714}
1715
1716/*
1717 * Not an __init, needed by the reboot code
1718 */
1719void disable_IO_APIC(void)
1720{
1721 /*
1722 * Clear the IO-APIC before rebooting:
1723 */
1724 clear_IO_APIC();
1725
650927ef 1726 /*
0b968d23 1727 * If the i8259 is routed through an IOAPIC
650927ef 1728 * Put that IOAPIC in virtual wire mode
0b968d23 1729 * so legacy interrupts can be delivered.
650927ef 1730 */
fcfd636a 1731 if (ioapic_i8259.pin != -1) {
650927ef
EB
1732 struct IO_APIC_route_entry entry;
1733 unsigned long flags;
1734
1735 memset(&entry, 0, sizeof(entry));
1736 entry.mask = 0; /* Enabled */
1737 entry.trigger = 0; /* Edge */
1738 entry.irr = 0;
1739 entry.polarity = 0; /* High */
1740 entry.delivery_status = 0;
1741 entry.dest_mode = 0; /* Physical */
fcfd636a 1742 entry.delivery_mode = dest_ExtINT; /* ExtInt */
650927ef 1743 entry.vector = 0;
76865c3f
VG
1744 entry.dest.physical.physical_dest =
1745 GET_APIC_ID(apic_read(APIC_ID));
650927ef
EB
1746
1747 /*
1748 * Add it to the IO-APIC irq-routing table:
1749 */
1750 spin_lock_irqsave(&ioapic_lock, flags);
fcfd636a
EB
1751 io_apic_write(ioapic_i8259.apic, 0x11+2*ioapic_i8259.pin,
1752 *(((int *)&entry)+1));
1753 io_apic_write(ioapic_i8259.apic, 0x10+2*ioapic_i8259.pin,
1754 *(((int *)&entry)+0));
650927ef
EB
1755 spin_unlock_irqrestore(&ioapic_lock, flags);
1756 }
fcfd636a 1757 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1da177e4
LT
1758}
1759
1760/*
1761 * function to set the IO-APIC physical IDs based on the
1762 * values stored in the MPC table.
1763 *
1764 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1765 */
1766
1767#ifndef CONFIG_X86_NUMAQ
1768static void __init setup_ioapic_ids_from_mpc(void)
1769{
1770 union IO_APIC_reg_00 reg_00;
1771 physid_mask_t phys_id_present_map;
1772 int apic;
1773 int i;
1774 unsigned char old_id;
1775 unsigned long flags;
1776
ca05fea6
NP
1777 /*
1778 * Don't check I/O APIC IDs for xAPIC systems. They have
1779 * no meaning without the serial APIC bus.
1780 */
7c5c1e42
SL
1781 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1782 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
ca05fea6 1783 return;
1da177e4
LT
1784 /*
1785 * This is broken; anything with a real cpu count has to
1786 * circumvent this idiocy regardless.
1787 */
1788 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1789
1790 /*
1791 * Set the IOAPIC ID to the value stored in the MPC table.
1792 */
1793 for (apic = 0; apic < nr_ioapics; apic++) {
1794
1795 /* Read the register 0 value */
1796 spin_lock_irqsave(&ioapic_lock, flags);
1797 reg_00.raw = io_apic_read(apic, 0);
1798 spin_unlock_irqrestore(&ioapic_lock, flags);
1799
1800 old_id = mp_ioapics[apic].mpc_apicid;
1801
1802 if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
1803 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1804 apic, mp_ioapics[apic].mpc_apicid);
1805 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1806 reg_00.bits.ID);
1807 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1808 }
1809
1da177e4
LT
1810 /*
1811 * Sanity check, is the ID really free? Every APIC in a
1812 * system must have a unique ID or we get lots of nice
1813 * 'stuck on smp_invalidate_needed IPI wait' messages.
1814 */
1815 if (check_apicid_used(phys_id_present_map,
1816 mp_ioapics[apic].mpc_apicid)) {
1817 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1818 apic, mp_ioapics[apic].mpc_apicid);
1819 for (i = 0; i < get_physical_broadcast(); i++)
1820 if (!physid_isset(i, phys_id_present_map))
1821 break;
1822 if (i >= get_physical_broadcast())
1823 panic("Max APIC ID exceeded!\n");
1824 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1825 i);
1826 physid_set(i, phys_id_present_map);
1827 mp_ioapics[apic].mpc_apicid = i;
1828 } else {
1829 physid_mask_t tmp;
1830 tmp = apicid_to_cpu_present(mp_ioapics[apic].mpc_apicid);
1831 apic_printk(APIC_VERBOSE, "Setting %d in the "
1832 "phys_id_present_map\n",
1833 mp_ioapics[apic].mpc_apicid);
1834 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1835 }
1836
1837
1838 /*
1839 * We need to adjust the IRQ routing table
1840 * if the ID changed.
1841 */
1842 if (old_id != mp_ioapics[apic].mpc_apicid)
1843 for (i = 0; i < mp_irq_entries; i++)
1844 if (mp_irqs[i].mpc_dstapic == old_id)
1845 mp_irqs[i].mpc_dstapic
1846 = mp_ioapics[apic].mpc_apicid;
1847
1848 /*
1849 * Read the right value from the MPC table and
1850 * write it into the ID register.
1851 */
1852 apic_printk(APIC_VERBOSE, KERN_INFO
1853 "...changing IO-APIC physical APIC ID to %d ...",
1854 mp_ioapics[apic].mpc_apicid);
1855
1856 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1857 spin_lock_irqsave(&ioapic_lock, flags);
1858 io_apic_write(apic, 0, reg_00.raw);
1859 spin_unlock_irqrestore(&ioapic_lock, flags);
1860
1861 /*
1862 * Sanity check
1863 */
1864 spin_lock_irqsave(&ioapic_lock, flags);
1865 reg_00.raw = io_apic_read(apic, 0);
1866 spin_unlock_irqrestore(&ioapic_lock, flags);
1867 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1868 printk("could not set ID!\n");
1869 else
1870 apic_printk(APIC_VERBOSE, " ok.\n");
1871 }
1872}
1873#else
1874static void __init setup_ioapic_ids_from_mpc(void) { }
1875#endif
1876
1877/*
1878 * There is a nasty bug in some older SMP boards, their mptable lies
1879 * about the timer IRQ. We do the following to work around the situation:
1880 *
1881 * - timer IRQ defaults to IO-APIC IRQ
1882 * - if this function detects that timer IRQs are defunct, then we fall
1883 * back to ISA timer IRQs
1884 */
1885static int __init timer_irq_works(void)
1886{
1887 unsigned long t1 = jiffies;
1888
1889 local_irq_enable();
1890 /* Let ten ticks pass... */
1891 mdelay((10 * 1000) / HZ);
1892
1893 /*
1894 * Expect a few ticks at least, to be sure some possible
1895 * glue logic does not lock up after one or two first
1896 * ticks in a non-ExtINT mode. Also the local APIC
1897 * might have cached one ExtINT interrupt. Finally, at
1898 * least one tick may be lost due to delays.
1899 */
1900 if (jiffies - t1 > 4)
1901 return 1;
1902
1903 return 0;
1904}
1905
1906/*
1907 * In the SMP+IOAPIC case it might happen that there are an unspecified
1908 * number of pending IRQ events unhandled. These cases are very rare,
1909 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1910 * better to do it this way as thus we do not have to be aware of
1911 * 'pending' interrupts in the IRQ path, except at this point.
1912 */
1913/*
1914 * Edge triggered needs to resend any interrupt
1915 * that was delayed but this is now handled in the device
1916 * independent code.
1917 */
1918
1919/*
1920 * Starting up a edge-triggered IO-APIC interrupt is
1921 * nasty - we need to make sure that we get the edge.
1922 * If it is already asserted for some reason, we need
1923 * return 1 to indicate that is was pending.
1924 *
1925 * This is not complete - we should be able to fake
1926 * an edge even if it isn't on the 8259A...
1927 */
1928static unsigned int startup_edge_ioapic_irq(unsigned int irq)
1929{
1930 int was_pending = 0;
1931 unsigned long flags;
1932
1933 spin_lock_irqsave(&ioapic_lock, flags);
1934 if (irq < 16) {
1935 disable_8259A_irq(irq);
1936 if (i8259A_irq_pending(irq))
1937 was_pending = 1;
1938 }
1939 __unmask_IO_APIC_irq(irq);
1940 spin_unlock_irqrestore(&ioapic_lock, flags);
1941
1942 return was_pending;
1943}
1944
1945/*
1946 * Once we have recorded IRQ_PENDING already, we can mask the
1947 * interrupt for real. This prevents IRQ storms from unhandled
1948 * devices.
1949 */
1950static void ack_edge_ioapic_irq(unsigned int irq)
1951{
1952 move_irq(irq);
1953 if ((irq_desc[irq].status & (IRQ_PENDING | IRQ_DISABLED))
1954 == (IRQ_PENDING | IRQ_DISABLED))
1955 mask_IO_APIC_irq(irq);
1956 ack_APIC_irq();
1957}
1958
1959/*
1960 * Level triggered interrupts can just be masked,
1961 * and shutting down and starting up the interrupt
1962 * is the same as enabling and disabling them -- except
1963 * with a startup need to return a "was pending" value.
1964 *
1965 * Level triggered interrupts are special because we
1966 * do not touch any IO-APIC register while handling
1967 * them. We ack the APIC in the end-IRQ handler, not
1968 * in the start-IRQ-handler. Protection against reentrance
1969 * from the same interrupt is still provided, both by the
1970 * generic IRQ layer and by the fact that an unacked local
1971 * APIC does not accept IRQs.
1972 */
1973static unsigned int startup_level_ioapic_irq (unsigned int irq)
1974{
1975 unmask_IO_APIC_irq(irq);
1976
1977 return 0; /* don't check for pending */
1978}
1979
1980static void end_level_ioapic_irq (unsigned int irq)
1981{
1982 unsigned long v;
1983 int i;
1984
1985 move_irq(irq);
1986/*
1987 * It appears there is an erratum which affects at least version 0x11
1988 * of I/O APIC (that's the 82093AA and cores integrated into various
1989 * chipsets). Under certain conditions a level-triggered interrupt is
1990 * erroneously delivered as edge-triggered one but the respective IRR
1991 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1992 * message but it will never arrive and further interrupts are blocked
1993 * from the source. The exact reason is so far unknown, but the
1994 * phenomenon was observed when two consecutive interrupt requests
1995 * from a given source get delivered to the same CPU and the source is
1996 * temporarily disabled in between.
1997 *
1998 * A workaround is to simulate an EOI message manually. We achieve it
1999 * by setting the trigger mode to edge and then to level when the edge
2000 * trigger mode gets detected in the TMR of a local APIC for a
2001 * level-triggered interrupt. We mask the source for the time of the
2002 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2003 * The idea is from Manfred Spraul. --macro
2004 */
2005 i = IO_APIC_VECTOR(irq);
2006
2007 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2008
2009 ack_APIC_irq();
2010
2011 if (!(v & (1 << (i & 0x1f)))) {
2012 atomic_inc(&irq_mis_count);
2013 spin_lock(&ioapic_lock);
2014 __mask_and_edge_IO_APIC_irq(irq);
2015 __unmask_and_level_IO_APIC_irq(irq);
2016 spin_unlock(&ioapic_lock);
2017 }
2018}
2019
2020#ifdef CONFIG_PCI_MSI
2021static unsigned int startup_edge_ioapic_vector(unsigned int vector)
2022{
2023 int irq = vector_to_irq(vector);
2024
2025 return startup_edge_ioapic_irq(irq);
2026}
2027
2028static void ack_edge_ioapic_vector(unsigned int vector)
2029{
2030 int irq = vector_to_irq(vector);
2031
fe655d3a 2032 move_native_irq(vector);
1da177e4
LT
2033 ack_edge_ioapic_irq(irq);
2034}
2035
2036static unsigned int startup_level_ioapic_vector (unsigned int vector)
2037{
2038 int irq = vector_to_irq(vector);
2039
2040 return startup_level_ioapic_irq (irq);
2041}
2042
2043static void end_level_ioapic_vector (unsigned int vector)
2044{
2045 int irq = vector_to_irq(vector);
2046
fe655d3a 2047 move_native_irq(vector);
1da177e4
LT
2048 end_level_ioapic_irq(irq);
2049}
2050
2051static void mask_IO_APIC_vector (unsigned int vector)
2052{
2053 int irq = vector_to_irq(vector);
2054
2055 mask_IO_APIC_irq(irq);
2056}
2057
2058static void unmask_IO_APIC_vector (unsigned int vector)
2059{
2060 int irq = vector_to_irq(vector);
2061
2062 unmask_IO_APIC_irq(irq);
2063}
2064
54d5d424 2065#ifdef CONFIG_SMP
1da177e4
LT
2066static void set_ioapic_affinity_vector (unsigned int vector,
2067 cpumask_t cpu_mask)
2068{
2069 int irq = vector_to_irq(vector);
2070
54d5d424 2071 set_native_irq_info(vector, cpu_mask);
1da177e4
LT
2072 set_ioapic_affinity_irq(irq, cpu_mask);
2073}
2074#endif
54d5d424 2075#endif
1da177e4
LT
2076
2077/*
2078 * Level and edge triggered IO-APIC interrupts need different handling,
2079 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2080 * handled with the level-triggered descriptor, but that one has slightly
2081 * more overhead. Level-triggered interrupts cannot be handled with the
2082 * edge-triggered handler, without risking IRQ storms and other ugly
2083 * races.
2084 */
6c231b7b 2085static struct hw_interrupt_type ioapic_edge_type __read_mostly = {
1da177e4
LT
2086 .typename = "IO-APIC-edge",
2087 .startup = startup_edge_ioapic,
2088 .shutdown = shutdown_edge_ioapic,
2089 .enable = enable_edge_ioapic,
2090 .disable = disable_edge_ioapic,
2091 .ack = ack_edge_ioapic,
2092 .end = end_edge_ioapic,
54d5d424 2093#ifdef CONFIG_SMP
1da177e4 2094 .set_affinity = set_ioapic_affinity,
54d5d424 2095#endif
1da177e4
LT
2096};
2097
6c231b7b 2098static struct hw_interrupt_type ioapic_level_type __read_mostly = {
1da177e4
LT
2099 .typename = "IO-APIC-level",
2100 .startup = startup_level_ioapic,
2101 .shutdown = shutdown_level_ioapic,
2102 .enable = enable_level_ioapic,
2103 .disable = disable_level_ioapic,
2104 .ack = mask_and_ack_level_ioapic,
2105 .end = end_level_ioapic,
54d5d424 2106#ifdef CONFIG_SMP
1da177e4 2107 .set_affinity = set_ioapic_affinity,
54d5d424 2108#endif
1da177e4
LT
2109};
2110
2111static inline void init_IO_APIC_traps(void)
2112{
2113 int irq;
2114
2115 /*
2116 * NOTE! The local APIC isn't very good at handling
2117 * multiple interrupts at the same interrupt level.
2118 * As the interrupt level is determined by taking the
2119 * vector number and shifting that right by 4, we
2120 * want to spread these out a bit so that they don't
2121 * all fall in the same interrupt level.
2122 *
2123 * Also, we've got to be careful not to trash gate
2124 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2125 */
2126 for (irq = 0; irq < NR_IRQS ; irq++) {
2127 int tmp = irq;
2128 if (use_pci_vector()) {
2129 if (!platform_legacy_irq(tmp))
2130 if ((tmp = vector_to_irq(tmp)) == -1)
2131 continue;
2132 }
2133 if (IO_APIC_IRQ(tmp) && !IO_APIC_VECTOR(tmp)) {
2134 /*
2135 * Hmm.. We don't have an entry for this,
2136 * so default to an old-fashioned 8259
2137 * interrupt if we can..
2138 */
2139 if (irq < 16)
2140 make_8259A_irq(irq);
2141 else
2142 /* Strange. Oh, well.. */
2143 irq_desc[irq].handler = &no_irq_type;
2144 }
2145 }
2146}
2147
2148static void enable_lapic_irq (unsigned int irq)
2149{
2150 unsigned long v;
2151
2152 v = apic_read(APIC_LVT0);
2153 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
2154}
2155
2156static void disable_lapic_irq (unsigned int irq)
2157{
2158 unsigned long v;
2159
2160 v = apic_read(APIC_LVT0);
2161 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
2162}
2163
2164static void ack_lapic_irq (unsigned int irq)
2165{
2166 ack_APIC_irq();
2167}
2168
2169static void end_lapic_irq (unsigned int i) { /* nothing */ }
2170
6c231b7b 2171static struct hw_interrupt_type lapic_irq_type __read_mostly = {
1da177e4
LT
2172 .typename = "local-APIC-edge",
2173 .startup = NULL, /* startup_irq() not used for IRQ0 */
2174 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
2175 .enable = enable_lapic_irq,
2176 .disable = disable_lapic_irq,
2177 .ack = ack_lapic_irq,
2178 .end = end_lapic_irq
2179};
2180
2181static void setup_nmi (void)
2182{
2183 /*
2184 * Dirty trick to enable the NMI watchdog ...
2185 * We put the 8259A master into AEOI mode and
2186 * unmask on all local APICs LVT0 as NMI.
2187 *
2188 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2189 * is from Maciej W. Rozycki - so we do not have to EOI from
2190 * the NMI handler or the timer interrupt.
2191 */
2192 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2193
2194 on_each_cpu(enable_NMI_through_LVT0, NULL, 1, 1);
2195
2196 apic_printk(APIC_VERBOSE, " done.\n");
2197}
2198
2199/*
2200 * This looks a bit hackish but it's about the only one way of sending
2201 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2202 * not support the ExtINT mode, unfortunately. We need to send these
2203 * cycles as some i82489DX-based boards have glue logic that keeps the
2204 * 8259A interrupt line asserted until INTA. --macro
2205 */
2206static inline void unlock_ExtINT_logic(void)
2207{
fcfd636a 2208 int apic, pin, i;
1da177e4
LT
2209 struct IO_APIC_route_entry entry0, entry1;
2210 unsigned char save_control, save_freq_select;
2211 unsigned long flags;
2212
fcfd636a
EB
2213 pin = find_isa_irq_pin(8, mp_INT);
2214 apic = find_isa_irq_apic(8, mp_INT);
1da177e4
LT
2215 if (pin == -1)
2216 return;
2217
2218 spin_lock_irqsave(&ioapic_lock, flags);
fcfd636a
EB
2219 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
2220 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1da177e4 2221 spin_unlock_irqrestore(&ioapic_lock, flags);
fcfd636a 2222 clear_IO_APIC_pin(apic, pin);
1da177e4
LT
2223
2224 memset(&entry1, 0, sizeof(entry1));
2225
2226 entry1.dest_mode = 0; /* physical delivery */
2227 entry1.mask = 0; /* unmask IRQ now */
2228 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2229 entry1.delivery_mode = dest_ExtINT;
2230 entry1.polarity = entry0.polarity;
2231 entry1.trigger = 0;
2232 entry1.vector = 0;
2233
2234 spin_lock_irqsave(&ioapic_lock, flags);
fcfd636a
EB
2235 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
2236 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1da177e4
LT
2237 spin_unlock_irqrestore(&ioapic_lock, flags);
2238
2239 save_control = CMOS_READ(RTC_CONTROL);
2240 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2241 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2242 RTC_FREQ_SELECT);
2243 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2244
2245 i = 100;
2246 while (i-- > 0) {
2247 mdelay(10);
2248 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2249 i -= 10;
2250 }
2251
2252 CMOS_WRITE(save_control, RTC_CONTROL);
2253 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
fcfd636a 2254 clear_IO_APIC_pin(apic, pin);
1da177e4
LT
2255
2256 spin_lock_irqsave(&ioapic_lock, flags);
fcfd636a
EB
2257 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
2258 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1da177e4
LT
2259 spin_unlock_irqrestore(&ioapic_lock, flags);
2260}
2261
e0c1e9bf
KM
2262int timer_uses_ioapic_pin_0;
2263
1da177e4
LT
2264/*
2265 * This code may look a bit paranoid, but it's supposed to cooperate with
2266 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2267 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2268 * fanatically on his truly buggy board.
2269 */
2270static inline void check_timer(void)
2271{
fcfd636a 2272 int apic1, pin1, apic2, pin2;
1da177e4
LT
2273 int vector;
2274
2275 /*
2276 * get/set the timer IRQ vector:
2277 */
2278 disable_8259A_irq(0);
2279 vector = assign_irq_vector(0);
2280 set_intr_gate(vector, interrupt[0]);
2281
2282 /*
2283 * Subtle, code in do_timer_interrupt() expects an AEOI
2284 * mode for the 8259A whenever interrupts are routed
2285 * through I/O APICs. Also IRQ0 has to be enabled in
2286 * the 8259A which implies the virtual wire has to be
2287 * disabled in the local APIC.
2288 */
2289 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2290 init_8259A(1);
2291 timer_ack = 1;
f9262c12
AK
2292 if (timer_over_8254 > 0)
2293 enable_8259A_irq(0);
1da177e4 2294
fcfd636a
EB
2295 pin1 = find_isa_irq_pin(0, mp_INT);
2296 apic1 = find_isa_irq_apic(0, mp_INT);
2297 pin2 = ioapic_i8259.pin;
2298 apic2 = ioapic_i8259.apic;
1da177e4 2299
e0c1e9bf
KM
2300 if (pin1 == 0)
2301 timer_uses_ioapic_pin_0 = 1;
2302
fcfd636a
EB
2303 printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
2304 vector, apic1, pin1, apic2, pin2);
1da177e4
LT
2305
2306 if (pin1 != -1) {
2307 /*
2308 * Ok, does IRQ0 through the IOAPIC work?
2309 */
2310 unmask_IO_APIC_irq(0);
2311 if (timer_irq_works()) {
2312 if (nmi_watchdog == NMI_IO_APIC) {
2313 disable_8259A_irq(0);
2314 setup_nmi();
2315 enable_8259A_irq(0);
1da177e4 2316 }
66759a01
CE
2317 if (disable_timer_pin_1 > 0)
2318 clear_IO_APIC_pin(0, pin1);
1da177e4
LT
2319 return;
2320 }
fcfd636a
EB
2321 clear_IO_APIC_pin(apic1, pin1);
2322 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to "
2323 "IO-APIC\n");
1da177e4
LT
2324 }
2325
2326 printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
2327 if (pin2 != -1) {
2328 printk("\n..... (found pin %d) ...", pin2);
2329 /*
2330 * legacy devices should be connected to IO APIC #0
2331 */
fcfd636a 2332 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1da177e4
LT
2333 if (timer_irq_works()) {
2334 printk("works.\n");
2335 if (pin1 != -1)
fcfd636a 2336 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
1da177e4 2337 else
fcfd636a 2338 add_pin_to_irq(0, apic2, pin2);
1da177e4
LT
2339 if (nmi_watchdog == NMI_IO_APIC) {
2340 setup_nmi();
1da177e4
LT
2341 }
2342 return;
2343 }
2344 /*
2345 * Cleanup, just in case ...
2346 */
fcfd636a 2347 clear_IO_APIC_pin(apic2, pin2);
1da177e4
LT
2348 }
2349 printk(" failed.\n");
2350
2351 if (nmi_watchdog == NMI_IO_APIC) {
2352 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
2353 nmi_watchdog = 0;
2354 }
2355
2356 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2357
2358 disable_8259A_irq(0);
2359 irq_desc[0].handler = &lapic_irq_type;
2360 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
2361 enable_8259A_irq(0);
2362
2363 if (timer_irq_works()) {
2364 printk(" works.\n");
2365 return;
2366 }
2367 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
2368 printk(" failed.\n");
2369
2370 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2371
2372 timer_ack = 0;
2373 init_8259A(0);
2374 make_8259A_irq(0);
2375 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
2376
2377 unlock_ExtINT_logic();
2378
2379 if (timer_irq_works()) {
2380 printk(" works.\n");
2381 return;
2382 }
2383 printk(" failed :(.\n");
2384 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2385 "report. Then try booting with the 'noapic' option");
2386}
2387
2388/*
2389 *
2390 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2391 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2392 * Linux doesn't really care, as it's not actually used
2393 * for any interrupt handling anyway.
2394 */
2395#define PIC_IRQS (1 << PIC_CASCADE_IR)
2396
2397void __init setup_IO_APIC(void)
2398{
2399 enable_IO_APIC();
2400
2401 if (acpi_ioapic)
2402 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2403 else
2404 io_apic_irqs = ~PIC_IRQS;
2405
2406 printk("ENABLING IO-APIC IRQs\n");
2407
2408 /*
2409 * Set up IO-APIC IRQ routing.
2410 */
2411 if (!acpi_ioapic)
2412 setup_ioapic_ids_from_mpc();
2413 sync_Arb_IDs();
2414 setup_IO_APIC_irqs();
2415 init_IO_APIC_traps();
1e4c85f9 2416 check_timer();
1da177e4
LT
2417 if (!acpi_ioapic)
2418 print_IO_APIC();
2419}
2420
f9262c12
AK
2421static int __init setup_disable_8254_timer(char *s)
2422{
2423 timer_over_8254 = -1;
2424 return 1;
2425}
2426static int __init setup_enable_8254_timer(char *s)
2427{
2428 timer_over_8254 = 2;
2429 return 1;
2430}
2431
2432__setup("disable_8254_timer", setup_disable_8254_timer);
2433__setup("enable_8254_timer", setup_enable_8254_timer);
2434
1da177e4
LT
2435/*
2436 * Called after all the initialization is done. If we didnt find any
2437 * APIC bugs then we can allow the modify fast path
2438 */
2439
2440static int __init io_apic_bug_finalize(void)
2441{
2442 if(sis_apic_bug == -1)
2443 sis_apic_bug = 0;
2444 return 0;
2445}
2446
2447late_initcall(io_apic_bug_finalize);
2448
2449struct sysfs_ioapic_data {
2450 struct sys_device dev;
2451 struct IO_APIC_route_entry entry[0];
2452};
2453static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2454
438510f6 2455static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
2456{
2457 struct IO_APIC_route_entry *entry;
2458 struct sysfs_ioapic_data *data;
2459 unsigned long flags;
2460 int i;
2461
2462 data = container_of(dev, struct sysfs_ioapic_data, dev);
2463 entry = data->entry;
2464 spin_lock_irqsave(&ioapic_lock, flags);
2465 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
2466 *(((int *)entry) + 1) = io_apic_read(dev->id, 0x11 + 2 * i);
2467 *(((int *)entry) + 0) = io_apic_read(dev->id, 0x10 + 2 * i);
2468 }
2469 spin_unlock_irqrestore(&ioapic_lock, flags);
2470
2471 return 0;
2472}
2473
2474static int ioapic_resume(struct sys_device *dev)
2475{
2476 struct IO_APIC_route_entry *entry;
2477 struct sysfs_ioapic_data *data;
2478 unsigned long flags;
2479 union IO_APIC_reg_00 reg_00;
2480 int i;
2481
2482 data = container_of(dev, struct sysfs_ioapic_data, dev);
2483 entry = data->entry;
2484
2485 spin_lock_irqsave(&ioapic_lock, flags);
2486 reg_00.raw = io_apic_read(dev->id, 0);
2487 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
2488 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
2489 io_apic_write(dev->id, 0, reg_00.raw);
2490 }
2491 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ ) {
2492 io_apic_write(dev->id, 0x11+2*i, *(((int *)entry)+1));
2493 io_apic_write(dev->id, 0x10+2*i, *(((int *)entry)+0));
2494 }
2495 spin_unlock_irqrestore(&ioapic_lock, flags);
2496
2497 return 0;
2498}
2499
2500static struct sysdev_class ioapic_sysdev_class = {
2501 set_kset_name("ioapic"),
2502 .suspend = ioapic_suspend,
2503 .resume = ioapic_resume,
2504};
2505
2506static int __init ioapic_init_sysfs(void)
2507{
2508 struct sys_device * dev;
2509 int i, size, error = 0;
2510
2511 error = sysdev_class_register(&ioapic_sysdev_class);
2512 if (error)
2513 return error;
2514
2515 for (i = 0; i < nr_ioapics; i++ ) {
2516 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2517 * sizeof(struct IO_APIC_route_entry);
2518 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
2519 if (!mp_ioapic_data[i]) {
2520 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2521 continue;
2522 }
2523 memset(mp_ioapic_data[i], 0, size);
2524 dev = &mp_ioapic_data[i]->dev;
2525 dev->id = i;
2526 dev->cls = &ioapic_sysdev_class;
2527 error = sysdev_register(dev);
2528 if (error) {
2529 kfree(mp_ioapic_data[i]);
2530 mp_ioapic_data[i] = NULL;
2531 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2532 continue;
2533 }
2534 }
2535
2536 return 0;
2537}
2538
2539device_initcall(ioapic_init_sysfs);
2540
2541/* --------------------------------------------------------------------------
2542 ACPI-based IOAPIC Configuration
2543 -------------------------------------------------------------------------- */
2544
888ba6c6 2545#ifdef CONFIG_ACPI
1da177e4
LT
2546
2547int __init io_apic_get_unique_id (int ioapic, int apic_id)
2548{
2549 union IO_APIC_reg_00 reg_00;
2550 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2551 physid_mask_t tmp;
2552 unsigned long flags;
2553 int i = 0;
2554
2555 /*
2556 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2557 * buses (one for LAPICs, one for IOAPICs), where predecessors only
2558 * supports up to 16 on one shared APIC bus.
2559 *
2560 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2561 * advantage of new APIC bus architecture.
2562 */
2563
2564 if (physids_empty(apic_id_map))
2565 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2566
2567 spin_lock_irqsave(&ioapic_lock, flags);
2568 reg_00.raw = io_apic_read(ioapic, 0);
2569 spin_unlock_irqrestore(&ioapic_lock, flags);
2570
2571 if (apic_id >= get_physical_broadcast()) {
2572 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2573 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2574 apic_id = reg_00.bits.ID;
2575 }
2576
2577 /*
2578 * Every APIC in a system must have a unique ID or we get lots of nice
2579 * 'stuck on smp_invalidate_needed IPI wait' messages.
2580 */
2581 if (check_apicid_used(apic_id_map, apic_id)) {
2582
2583 for (i = 0; i < get_physical_broadcast(); i++) {
2584 if (!check_apicid_used(apic_id_map, i))
2585 break;
2586 }
2587
2588 if (i == get_physical_broadcast())
2589 panic("Max apic_id exceeded!\n");
2590
2591 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2592 "trying %d\n", ioapic, apic_id, i);
2593
2594 apic_id = i;
2595 }
2596
2597 tmp = apicid_to_cpu_present(apic_id);
2598 physids_or(apic_id_map, apic_id_map, tmp);
2599
2600 if (reg_00.bits.ID != apic_id) {
2601 reg_00.bits.ID = apic_id;
2602
2603 spin_lock_irqsave(&ioapic_lock, flags);
2604 io_apic_write(ioapic, 0, reg_00.raw);
2605 reg_00.raw = io_apic_read(ioapic, 0);
2606 spin_unlock_irqrestore(&ioapic_lock, flags);
2607
2608 /* Sanity check */
6070f9ec
AD
2609 if (reg_00.bits.ID != apic_id) {
2610 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2611 return -1;
2612 }
1da177e4
LT
2613 }
2614
2615 apic_printk(APIC_VERBOSE, KERN_INFO
2616 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2617
2618 return apic_id;
2619}
2620
2621
2622int __init io_apic_get_version (int ioapic)
2623{
2624 union IO_APIC_reg_01 reg_01;
2625 unsigned long flags;
2626
2627 spin_lock_irqsave(&ioapic_lock, flags);
2628 reg_01.raw = io_apic_read(ioapic, 1);
2629 spin_unlock_irqrestore(&ioapic_lock, flags);
2630
2631 return reg_01.bits.version;
2632}
2633
2634
2635int __init io_apic_get_redir_entries (int ioapic)
2636{
2637 union IO_APIC_reg_01 reg_01;
2638 unsigned long flags;
2639
2640 spin_lock_irqsave(&ioapic_lock, flags);
2641 reg_01.raw = io_apic_read(ioapic, 1);
2642 spin_unlock_irqrestore(&ioapic_lock, flags);
2643
2644 return reg_01.bits.entries;
2645}
2646
2647
2648int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2649{
2650 struct IO_APIC_route_entry entry;
2651 unsigned long flags;
2652
2653 if (!IO_APIC_IRQ(irq)) {
2654 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2655 ioapic);
2656 return -EINVAL;
2657 }
2658
2659 /*
2660 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2661 * Note that we mask (disable) IRQs now -- these get enabled when the
2662 * corresponding device driver registers for this IRQ.
2663 */
2664
2665 memset(&entry,0,sizeof(entry));
2666
2667 entry.delivery_mode = INT_DELIVERY_MODE;
2668 entry.dest_mode = INT_DEST_MODE;
2669 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2670 entry.trigger = edge_level;
2671 entry.polarity = active_high_low;
2672 entry.mask = 1;
2673
2674 /*
2675 * IRQs < 16 are already in the irq_2_pin[] map
2676 */
2677 if (irq >= 16)
2678 add_pin_to_irq(irq, ioapic, pin);
2679
2680 entry.vector = assign_irq_vector(irq);
2681
2682 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2683 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
2684 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2685 edge_level, active_high_low);
2686
2687 ioapic_register_intr(irq, entry.vector, edge_level);
2688
2689 if (!ioapic && (irq < 16))
2690 disable_8259A_irq(irq);
2691
2692 spin_lock_irqsave(&ioapic_lock, flags);
2693 io_apic_write(ioapic, 0x11+2*pin, *(((int *)&entry)+1));
2694 io_apic_write(ioapic, 0x10+2*pin, *(((int *)&entry)+0));
54d5d424 2695 set_native_irq_info(use_pci_vector() ? entry.vector : irq, TARGET_CPUS);
1da177e4
LT
2696 spin_unlock_irqrestore(&ioapic_lock, flags);
2697
2698 return 0;
2699}
2700
888ba6c6 2701#endif /* CONFIG_ACPI */
This page took 0.293469 seconds and 5 git commands to generate.