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