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