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