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