]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86/kernel/apic/io_apic.c
x86, x2apic: use virtual wire A mode in disable_IO_APIC() with interrupt-remapping
[net-next-2.6.git] / arch / x86 / kernel / apic / io_apic.c
CommitLineData
1da177e4
LT
1/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
8f47e163 4 * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
1da177e4
LT
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>
d4057bdb 28#include <linux/pci.h>
1da177e4
LT
29#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
129f6946 32#include <linux/module.h>
1da177e4 33#include <linux/sysdev.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>
54168ed7 38#include <linux/jiffies.h> /* time_after() */
d4057bdb
YL
39#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
58ac1e76 44#include <linux/hpet.h>
54d5d424 45
d4057bdb 46#include <asm/idle.h>
1da177e4
LT
47#include <asm/io.h>
48#include <asm/smp.h>
6d652ea1 49#include <asm/cpu.h>
1da177e4 50#include <asm/desc.h>
d4057bdb
YL
51#include <asm/proto.h>
52#include <asm/acpi.h>
53#include <asm/dma.h>
1da177e4 54#include <asm/timer.h>
306e440d 55#include <asm/i8259.h>
3e4ff115 56#include <asm/nmi.h>
2d3fcc1c 57#include <asm/msidef.h>
8b955b0d 58#include <asm/hypertransport.h>
a4dbc34d 59#include <asm/setup.h>
d4057bdb 60#include <asm/irq_remapping.h>
58ac1e76 61#include <asm/hpet.h>
4173a0e7
DN
62#include <asm/uv/uv_hub.h>
63#include <asm/uv/uv_irq.h>
1da177e4 64
7b6aa335 65#include <asm/apic.h>
1da177e4 66
32f71aff
MR
67#define __apicdebuginit(type) static type __init
68
1da177e4 69/*
54168ed7
IM
70 * Is the SiS APIC rmw bug present ?
71 * -1 = don't know, 0 = no, 1 = yes
1da177e4
LT
72 */
73int sis_apic_bug = -1;
74
efa2559f
YL
75static DEFINE_SPINLOCK(ioapic_lock);
76static DEFINE_SPINLOCK(vector_lock);
77
1da177e4
LT
78/*
79 * # of IRQ routing registers
80 */
81int nr_ioapic_registers[MAX_IO_APICS];
82
9f640ccb 83/* I/O APIC entries */
b5ba7e6d 84struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
9f640ccb
AS
85int nr_ioapics;
86
584f734d 87/* MP IRQ source entries */
c2c21745 88struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
584f734d
AS
89
90/* # of MP IRQ source entries */
91int mp_irq_entries;
92
8732fc4b
AS
93#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
94int mp_bus_id_to_type[MAX_MP_BUSSES];
95#endif
96
97DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
98
efa2559f
YL
99int skip_ioapic_setup;
100
65a4e574
IM
101void arch_disable_smp_support(void)
102{
103#ifdef CONFIG_PCI
104 noioapicquirk = 1;
105 noioapicreroute = -1;
106#endif
107 skip_ioapic_setup = 1;
108}
109
54168ed7 110static int __init parse_noapic(char *str)
efa2559f
YL
111{
112 /* disable IO-APIC */
65a4e574 113 arch_disable_smp_support();
efa2559f
YL
114 return 0;
115}
116early_param("noapic", parse_noapic);
66759a01 117
0f978f45 118struct irq_pin_list;
0b8f1efa
YL
119
120/*
121 * This is performance-critical, we want to do it O(1)
122 *
123 * the indexing order of this array favors 1:1 mappings
124 * between pins and IRQs.
125 */
126
127struct irq_pin_list {
128 int apic, pin;
129 struct irq_pin_list *next;
130};
131
132static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
133{
134 struct irq_pin_list *pin;
135 int node;
136
137 node = cpu_to_node(cpu);
138
139 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
0b8f1efa
YL
140
141 return pin;
142}
143
a1420f39 144struct irq_cfg {
0f978f45 145 struct irq_pin_list *irq_2_pin;
22f65d31
MT
146 cpumask_var_t domain;
147 cpumask_var_t old_domain;
497c9a19 148 unsigned move_cleanup_count;
a1420f39 149 u8 vector;
497c9a19 150 u8 move_in_progress : 1;
48a1b10a
YL
151#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
152 u8 move_desc_pending : 1;
153#endif
a1420f39
YL
154};
155
a1420f39 156/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
0b8f1efa
YL
157#ifdef CONFIG_SPARSE_IRQ
158static struct irq_cfg irq_cfgx[] = {
159#else
d6c88a50 160static struct irq_cfg irq_cfgx[NR_IRQS] = {
0b8f1efa 161#endif
22f65d31
MT
162 [0] = { .vector = IRQ0_VECTOR, },
163 [1] = { .vector = IRQ1_VECTOR, },
164 [2] = { .vector = IRQ2_VECTOR, },
165 [3] = { .vector = IRQ3_VECTOR, },
166 [4] = { .vector = IRQ4_VECTOR, },
167 [5] = { .vector = IRQ5_VECTOR, },
168 [6] = { .vector = IRQ6_VECTOR, },
169 [7] = { .vector = IRQ7_VECTOR, },
170 [8] = { .vector = IRQ8_VECTOR, },
171 [9] = { .vector = IRQ9_VECTOR, },
172 [10] = { .vector = IRQ10_VECTOR, },
173 [11] = { .vector = IRQ11_VECTOR, },
174 [12] = { .vector = IRQ12_VECTOR, },
175 [13] = { .vector = IRQ13_VECTOR, },
176 [14] = { .vector = IRQ14_VECTOR, },
177 [15] = { .vector = IRQ15_VECTOR, },
a1420f39
YL
178};
179
13a0c3c2 180int __init arch_early_irq_init(void)
8f09cd20 181{
0b8f1efa
YL
182 struct irq_cfg *cfg;
183 struct irq_desc *desc;
184 int count;
185 int i;
d6c88a50 186
0b8f1efa
YL
187 cfg = irq_cfgx;
188 count = ARRAY_SIZE(irq_cfgx);
8f09cd20 189
0b8f1efa
YL
190 for (i = 0; i < count; i++) {
191 desc = irq_to_desc(i);
192 desc->chip_data = &cfg[i];
22f65d31
MT
193 alloc_bootmem_cpumask_var(&cfg[i].domain);
194 alloc_bootmem_cpumask_var(&cfg[i].old_domain);
195 if (i < NR_IRQS_LEGACY)
196 cpumask_setall(cfg[i].domain);
0b8f1efa 197 }
13a0c3c2
YL
198
199 return 0;
0b8f1efa 200}
8f09cd20 201
0b8f1efa 202#ifdef CONFIG_SPARSE_IRQ
d6c88a50 203static struct irq_cfg *irq_cfg(unsigned int irq)
8f09cd20 204{
0b8f1efa
YL
205 struct irq_cfg *cfg = NULL;
206 struct irq_desc *desc;
1da177e4 207
0b8f1efa
YL
208 desc = irq_to_desc(irq);
209 if (desc)
210 cfg = desc->chip_data;
0f978f45 211
0b8f1efa 212 return cfg;
8f09cd20 213}
d6c88a50 214
0b8f1efa 215static struct irq_cfg *get_one_free_irq_cfg(int cpu)
8f09cd20 216{
0b8f1efa
YL
217 struct irq_cfg *cfg;
218 int node;
219
220 node = cpu_to_node(cpu);
0f978f45 221
0b8f1efa 222 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
22f65d31 223 if (cfg) {
80855f73 224 if (!alloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
22f65d31
MT
225 kfree(cfg);
226 cfg = NULL;
80855f73
MT
227 } else if (!alloc_cpumask_var_node(&cfg->old_domain,
228 GFP_ATOMIC, node)) {
22f65d31
MT
229 free_cpumask_var(cfg->domain);
230 kfree(cfg);
231 cfg = NULL;
232 } else {
233 cpumask_clear(cfg->domain);
234 cpumask_clear(cfg->old_domain);
235 }
236 }
0f978f45 237
0b8f1efa 238 return cfg;
8f09cd20
YL
239}
240
13a0c3c2 241int arch_init_chip_data(struct irq_desc *desc, int cpu)
0f978f45 242{
0b8f1efa 243 struct irq_cfg *cfg;
d6c88a50 244
0b8f1efa
YL
245 cfg = desc->chip_data;
246 if (!cfg) {
247 desc->chip_data = get_one_free_irq_cfg(cpu);
248 if (!desc->chip_data) {
249 printk(KERN_ERR "can not alloc irq_cfg\n");
250 BUG_ON(1);
251 }
252 }
1da177e4 253
13a0c3c2 254 return 0;
0b8f1efa 255}
0f978f45 256
48a1b10a 257#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
d6c88a50 258
48a1b10a
YL
259static void
260init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int cpu)
0f978f45 261{
48a1b10a
YL
262 struct irq_pin_list *old_entry, *head, *tail, *entry;
263
264 cfg->irq_2_pin = NULL;
265 old_entry = old_cfg->irq_2_pin;
266 if (!old_entry)
267 return;
0f978f45 268
48a1b10a
YL
269 entry = get_one_free_irq_2_pin(cpu);
270 if (!entry)
271 return;
0f978f45 272
48a1b10a
YL
273 entry->apic = old_entry->apic;
274 entry->pin = old_entry->pin;
275 head = entry;
276 tail = entry;
277 old_entry = old_entry->next;
278 while (old_entry) {
279 entry = get_one_free_irq_2_pin(cpu);
280 if (!entry) {
281 entry = head;
282 while (entry) {
283 head = entry->next;
284 kfree(entry);
285 entry = head;
286 }
287 /* still use the old one */
288 return;
289 }
290 entry->apic = old_entry->apic;
291 entry->pin = old_entry->pin;
292 tail->next = entry;
293 tail = entry;
294 old_entry = old_entry->next;
295 }
0f978f45 296
48a1b10a
YL
297 tail->next = NULL;
298 cfg->irq_2_pin = head;
0f978f45 299}
0f978f45 300
48a1b10a 301static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
0f978f45 302{
48a1b10a 303 struct irq_pin_list *entry, *next;
0f978f45 304
48a1b10a
YL
305 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
306 return;
301e6190 307
48a1b10a 308 entry = old_cfg->irq_2_pin;
0f978f45 309
48a1b10a
YL
310 while (entry) {
311 next = entry->next;
312 kfree(entry);
313 entry = next;
314 }
315 old_cfg->irq_2_pin = NULL;
0f978f45 316}
0f978f45 317
48a1b10a
YL
318void arch_init_copy_chip_data(struct irq_desc *old_desc,
319 struct irq_desc *desc, int cpu)
0f978f45 320{
48a1b10a
YL
321 struct irq_cfg *cfg;
322 struct irq_cfg *old_cfg;
0f978f45 323
48a1b10a 324 cfg = get_one_free_irq_cfg(cpu);
301e6190 325
48a1b10a
YL
326 if (!cfg)
327 return;
328
329 desc->chip_data = cfg;
330
331 old_cfg = old_desc->chip_data;
332
333 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
334
335 init_copy_irq_2_pin(old_cfg, cfg, cpu);
0f978f45 336}
1da177e4 337
48a1b10a
YL
338static void free_irq_cfg(struct irq_cfg *old_cfg)
339{
340 kfree(old_cfg);
341}
342
343void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
344{
345 struct irq_cfg *old_cfg, *cfg;
346
347 old_cfg = old_desc->chip_data;
348 cfg = desc->chip_data;
349
350 if (old_cfg == cfg)
351 return;
352
353 if (old_cfg) {
354 free_irq_2_pin(old_cfg, cfg);
355 free_irq_cfg(old_cfg);
356 old_desc->chip_data = NULL;
357 }
358}
359
d733e00d
IM
360static void
361set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
48a1b10a
YL
362{
363 struct irq_cfg *cfg = desc->chip_data;
364
365 if (!cfg->move_in_progress) {
366 /* it means that domain is not changed */
7f7ace0c 367 if (!cpumask_intersects(desc->affinity, mask))
48a1b10a
YL
368 cfg->move_desc_pending = 1;
369 }
0f978f45 370}
48a1b10a
YL
371#endif
372
0b8f1efa
YL
373#else
374static struct irq_cfg *irq_cfg(unsigned int irq)
375{
376 return irq < nr_irqs ? irq_cfgx + irq : NULL;
0f978f45 377}
1da177e4 378
0b8f1efa
YL
379#endif
380
48a1b10a 381#ifndef CONFIG_NUMA_MIGRATE_IRQ_DESC
e7986739
MT
382static inline void
383set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
3145e941
YL
384{
385}
48a1b10a 386#endif
1da177e4 387
130fe05d
LT
388struct io_apic {
389 unsigned int index;
390 unsigned int unused[3];
391 unsigned int data;
392};
393
394static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
395{
396 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
b5ba7e6d 397 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
130fe05d
LT
398}
399
400static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
401{
402 struct io_apic __iomem *io_apic = io_apic_base(apic);
403 writel(reg, &io_apic->index);
404 return readl(&io_apic->data);
405}
406
407static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
408{
409 struct io_apic __iomem *io_apic = io_apic_base(apic);
410 writel(reg, &io_apic->index);
411 writel(value, &io_apic->data);
412}
413
414/*
415 * Re-write a value: to be used for read-modify-write
416 * cycles where the read already set up the index register.
417 *
418 * Older SiS APIC requires we rewrite the index register
419 */
420static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
421{
54168ed7 422 struct io_apic __iomem *io_apic = io_apic_base(apic);
d6c88a50
TG
423
424 if (sis_apic_bug)
425 writel(reg, &io_apic->index);
130fe05d
LT
426 writel(value, &io_apic->data);
427}
428
3145e941 429static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
047c8fdb
YL
430{
431 struct irq_pin_list *entry;
432 unsigned long flags;
047c8fdb
YL
433
434 spin_lock_irqsave(&ioapic_lock, flags);
435 entry = cfg->irq_2_pin;
436 for (;;) {
437 unsigned int reg;
438 int pin;
439
440 if (!entry)
441 break;
442 pin = entry->pin;
443 reg = io_apic_read(entry->apic, 0x10 + pin*2);
444 /* Is the remote IRR bit set? */
445 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
446 spin_unlock_irqrestore(&ioapic_lock, flags);
447 return true;
448 }
449 if (!entry->next)
450 break;
451 entry = entry->next;
452 }
453 spin_unlock_irqrestore(&ioapic_lock, flags);
454
455 return false;
456}
047c8fdb 457
cf4c6a2f
AK
458union entry_union {
459 struct { u32 w1, w2; };
460 struct IO_APIC_route_entry entry;
461};
462
463static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
464{
465 union entry_union eu;
466 unsigned long flags;
467 spin_lock_irqsave(&ioapic_lock, flags);
468 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
469 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
470 spin_unlock_irqrestore(&ioapic_lock, flags);
471 return eu.entry;
472}
473
f9dadfa7
LT
474/*
475 * When we write a new IO APIC routing entry, we need to write the high
476 * word first! If the mask bit in the low word is clear, we will enable
477 * the interrupt, and we need to make sure the entry is fully populated
478 * before that happens.
479 */
d15512f4
AK
480static void
481__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
cf4c6a2f 482{
cf4c6a2f
AK
483 union entry_union eu;
484 eu.entry = e;
f9dadfa7
LT
485 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
486 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
d15512f4
AK
487}
488
ca97ab90 489void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
d15512f4
AK
490{
491 unsigned long flags;
492 spin_lock_irqsave(&ioapic_lock, flags);
493 __ioapic_write_entry(apic, pin, e);
f9dadfa7
LT
494 spin_unlock_irqrestore(&ioapic_lock, flags);
495}
496
497/*
498 * When we mask an IO APIC routing entry, we need to write the low
499 * word first, in order to set the mask bit before we change the
500 * high bits!
501 */
502static void ioapic_mask_entry(int apic, int pin)
503{
504 unsigned long flags;
505 union entry_union eu = { .entry.mask = 1 };
506
cf4c6a2f
AK
507 spin_lock_irqsave(&ioapic_lock, flags);
508 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
509 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
510 spin_unlock_irqrestore(&ioapic_lock, flags);
511}
512
497c9a19 513#ifdef CONFIG_SMP
22f65d31
MT
514static void send_cleanup_vector(struct irq_cfg *cfg)
515{
516 cpumask_var_t cleanup_mask;
517
518 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
519 unsigned int i;
520 cfg->move_cleanup_count = 0;
521 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
522 cfg->move_cleanup_count++;
523 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
dac5f412 524 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
22f65d31
MT
525 } else {
526 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
527 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
dac5f412 528 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
22f65d31
MT
529 free_cpumask_var(cleanup_mask);
530 }
531 cfg->move_in_progress = 0;
532}
533
3145e941 534static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
497c9a19
YL
535{
536 int apic, pin;
497c9a19 537 struct irq_pin_list *entry;
3145e941 538 u8 vector = cfg->vector;
497c9a19 539
497c9a19
YL
540 entry = cfg->irq_2_pin;
541 for (;;) {
542 unsigned int reg;
543
544 if (!entry)
545 break;
546
547 apic = entry->apic;
548 pin = entry->pin;
54168ed7
IM
549#ifdef CONFIG_INTR_REMAP
550 /*
551 * With interrupt-remapping, destination information comes
552 * from interrupt-remapping table entry.
553 */
554 if (!irq_remapped(irq))
555 io_apic_write(apic, 0x11 + pin*2, dest);
556#else
497c9a19 557 io_apic_write(apic, 0x11 + pin*2, dest);
54168ed7 558#endif
497c9a19
YL
559 reg = io_apic_read(apic, 0x10 + pin*2);
560 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
561 reg |= vector;
54168ed7 562 io_apic_modify(apic, 0x10 + pin*2, reg);
497c9a19
YL
563 if (!entry->next)
564 break;
565 entry = entry->next;
566 }
567}
efa2559f 568
e7986739
MT
569static int
570assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
efa2559f 571
22f65d31 572/*
debccb3e
IM
573 * Either sets desc->affinity to a valid value, and returns
574 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
575 * leaves desc->affinity untouched.
22f65d31
MT
576 */
577static unsigned int
578set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
497c9a19
YL
579{
580 struct irq_cfg *cfg;
3145e941 581 unsigned int irq;
497c9a19 582
0de26520 583 if (!cpumask_intersects(mask, cpu_online_mask))
22f65d31 584 return BAD_APICID;
497c9a19 585
3145e941
YL
586 irq = desc->irq;
587 cfg = desc->chip_data;
588 if (assign_irq_vector(irq, cfg, mask))
22f65d31 589 return BAD_APICID;
497c9a19 590
7f7ace0c 591 cpumask_and(desc->affinity, cfg->domain, mask);
3145e941 592 set_extra_move_desc(desc, mask);
debccb3e
IM
593
594 return apic->cpu_mask_to_apicid_and(desc->affinity, cpu_online_mask);
22f65d31 595}
3145e941 596
22f65d31
MT
597static void
598set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
497c9a19
YL
599{
600 struct irq_cfg *cfg;
601 unsigned long flags;
602 unsigned int dest;
22f65d31 603 unsigned int irq;
497c9a19 604
22f65d31
MT
605 irq = desc->irq;
606 cfg = desc->chip_data;
497c9a19 607
497c9a19 608 spin_lock_irqsave(&ioapic_lock, flags);
22f65d31
MT
609 dest = set_desc_affinity(desc, mask);
610 if (dest != BAD_APICID) {
611 /* Only the high 8 bits are valid. */
612 dest = SET_APIC_LOGICAL_ID(dest);
613 __target_IO_APIC_irq(irq, dest, cfg);
614 }
497c9a19
YL
615 spin_unlock_irqrestore(&ioapic_lock, flags);
616}
497c9a19 617
22f65d31
MT
618static void
619set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
3145e941
YL
620{
621 struct irq_desc *desc;
497c9a19 622
54168ed7 623 desc = irq_to_desc(irq);
3145e941
YL
624
625 set_ioapic_affinity_irq_desc(desc, mask);
497c9a19 626}
497c9a19
YL
627#endif /* CONFIG_SMP */
628
1da177e4
LT
629/*
630 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
631 * shared ISA-space IRQs, so we have to support them. We are super
632 * fast in the common case, and fast for shared ISA-space IRQs.
633 */
3145e941 634static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
1da177e4 635{
0f978f45
YL
636 struct irq_pin_list *entry;
637
0f978f45
YL
638 entry = cfg->irq_2_pin;
639 if (!entry) {
0b8f1efa
YL
640 entry = get_one_free_irq_2_pin(cpu);
641 if (!entry) {
642 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
643 apic, pin);
644 return;
645 }
0f978f45
YL
646 cfg->irq_2_pin = entry;
647 entry->apic = apic;
648 entry->pin = pin;
0f978f45
YL
649 return;
650 }
1da177e4 651
0f978f45
YL
652 while (entry->next) {
653 /* not again, please */
654 if (entry->apic == apic && entry->pin == pin)
655 return;
1da177e4 656
0f978f45 657 entry = entry->next;
1da177e4 658 }
0f978f45 659
0b8f1efa 660 entry->next = get_one_free_irq_2_pin(cpu);
0f978f45 661 entry = entry->next;
1da177e4
LT
662 entry->apic = apic;
663 entry->pin = pin;
664}
665
666/*
667 * Reroute an IRQ to a different pin.
668 */
3145e941 669static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
1da177e4
LT
670 int oldapic, int oldpin,
671 int newapic, int newpin)
672{
0f978f45
YL
673 struct irq_pin_list *entry = cfg->irq_2_pin;
674 int replaced = 0;
1da177e4 675
0f978f45 676 while (entry) {
1da177e4
LT
677 if (entry->apic == oldapic && entry->pin == oldpin) {
678 entry->apic = newapic;
679 entry->pin = newpin;
0f978f45
YL
680 replaced = 1;
681 /* every one is different, right? */
1da177e4 682 break;
0f978f45
YL
683 }
684 entry = entry->next;
1da177e4 685 }
0f978f45
YL
686
687 /* why? call replace before add? */
688 if (!replaced)
3145e941 689 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
1da177e4
LT
690}
691
3145e941 692static inline void io_apic_modify_irq(struct irq_cfg *cfg,
87783be4
CG
693 int mask_and, int mask_or,
694 void (*final)(struct irq_pin_list *entry))
695{
696 int pin;
87783be4 697 struct irq_pin_list *entry;
047c8fdb 698
87783be4
CG
699 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
700 unsigned int reg;
701 pin = entry->pin;
702 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
703 reg &= mask_and;
704 reg |= mask_or;
705 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
706 if (final)
707 final(entry);
708 }
709}
047c8fdb 710
3145e941 711static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
87783be4 712{
3145e941 713 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
87783be4 714}
047c8fdb 715
4e738e2f 716#ifdef CONFIG_X86_64
7f3e632f 717static void io_apic_sync(struct irq_pin_list *entry)
1da177e4 718{
87783be4
CG
719 /*
720 * Synchronize the IO-APIC and the CPU by doing
721 * a dummy read from the IO-APIC
722 */
723 struct io_apic __iomem *io_apic;
724 io_apic = io_apic_base(entry->apic);
4e738e2f 725 readl(&io_apic->data);
1da177e4
LT
726}
727
3145e941 728static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
87783be4 729{
3145e941 730 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
87783be4
CG
731}
732#else /* CONFIG_X86_32 */
3145e941 733static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
87783be4 734{
3145e941 735 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
87783be4 736}
1da177e4 737
3145e941 738static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
87783be4 739{
3145e941 740 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
87783be4
CG
741 IO_APIC_REDIR_MASKED, NULL);
742}
1da177e4 743
3145e941 744static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
87783be4 745{
3145e941 746 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
87783be4
CG
747 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
748}
749#endif /* CONFIG_X86_32 */
047c8fdb 750
3145e941 751static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
1da177e4 752{
3145e941 753 struct irq_cfg *cfg = desc->chip_data;
1da177e4
LT
754 unsigned long flags;
755
3145e941
YL
756 BUG_ON(!cfg);
757
1da177e4 758 spin_lock_irqsave(&ioapic_lock, flags);
3145e941 759 __mask_IO_APIC_irq(cfg);
1da177e4
LT
760 spin_unlock_irqrestore(&ioapic_lock, flags);
761}
762
3145e941 763static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
1da177e4 764{
3145e941 765 struct irq_cfg *cfg = desc->chip_data;
1da177e4
LT
766 unsigned long flags;
767
768 spin_lock_irqsave(&ioapic_lock, flags);
3145e941 769 __unmask_IO_APIC_irq(cfg);
1da177e4
LT
770 spin_unlock_irqrestore(&ioapic_lock, flags);
771}
772
3145e941
YL
773static void mask_IO_APIC_irq(unsigned int irq)
774{
775 struct irq_desc *desc = irq_to_desc(irq);
776
777 mask_IO_APIC_irq_desc(desc);
778}
779static void unmask_IO_APIC_irq(unsigned int irq)
780{
781 struct irq_desc *desc = irq_to_desc(irq);
782
783 unmask_IO_APIC_irq_desc(desc);
784}
785
1da177e4
LT
786static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
787{
788 struct IO_APIC_route_entry entry;
36062448 789
1da177e4 790 /* Check delivery_mode to be sure we're not clearing an SMI pin */
cf4c6a2f 791 entry = ioapic_read_entry(apic, pin);
1da177e4
LT
792 if (entry.delivery_mode == dest_SMI)
793 return;
1da177e4
LT
794 /*
795 * Disable it in the IO-APIC irq-routing table:
796 */
f9dadfa7 797 ioapic_mask_entry(apic, pin);
1da177e4
LT
798}
799
54168ed7 800static void clear_IO_APIC (void)
1da177e4
LT
801{
802 int apic, pin;
803
804 for (apic = 0; apic < nr_ioapics; apic++)
805 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
806 clear_IO_APIC_pin(apic, pin);
807}
808
54168ed7 809#ifdef CONFIG_X86_32
1da177e4
LT
810/*
811 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
812 * specific CPU-side IRQs.
813 */
814
815#define MAX_PIRQS 8
3bd25d0f
YL
816static int pirq_entries[MAX_PIRQS] = {
817 [0 ... MAX_PIRQS - 1] = -1
818};
1da177e4 819
1da177e4
LT
820static int __init ioapic_pirq_setup(char *str)
821{
822 int i, max;
823 int ints[MAX_PIRQS+1];
824
825 get_options(str, ARRAY_SIZE(ints), ints);
826
1da177e4
LT
827 apic_printk(APIC_VERBOSE, KERN_INFO
828 "PIRQ redirection, working around broken MP-BIOS.\n");
829 max = MAX_PIRQS;
830 if (ints[0] < MAX_PIRQS)
831 max = ints[0];
832
833 for (i = 0; i < max; i++) {
834 apic_printk(APIC_VERBOSE, KERN_DEBUG
835 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
836 /*
837 * PIRQs are mapped upside down, usually.
838 */
839 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
840 }
841 return 1;
842}
843
844__setup("pirq=", ioapic_pirq_setup);
54168ed7
IM
845#endif /* CONFIG_X86_32 */
846
847#ifdef CONFIG_INTR_REMAP
848/* I/O APIC RTE contents at the OS boot up */
849static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
850
851/*
852 * Saves and masks all the unmasked IO-APIC RTE's
853 */
854int save_mask_IO_APIC_setup(void)
855{
856 union IO_APIC_reg_01 reg_01;
857 unsigned long flags;
858 int apic, pin;
859
860 /*
861 * The number of IO-APIC IRQ registers (== #pins):
862 */
863 for (apic = 0; apic < nr_ioapics; apic++) {
864 spin_lock_irqsave(&ioapic_lock, flags);
865 reg_01.raw = io_apic_read(apic, 1);
866 spin_unlock_irqrestore(&ioapic_lock, flags);
867 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
868 }
869
870 for (apic = 0; apic < nr_ioapics; apic++) {
871 early_ioapic_entries[apic] =
872 kzalloc(sizeof(struct IO_APIC_route_entry) *
873 nr_ioapic_registers[apic], GFP_KERNEL);
874 if (!early_ioapic_entries[apic])
5ffa4eb2 875 goto nomem;
54168ed7
IM
876 }
877
878 for (apic = 0; apic < nr_ioapics; apic++)
879 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
880 struct IO_APIC_route_entry entry;
881
882 entry = early_ioapic_entries[apic][pin] =
883 ioapic_read_entry(apic, pin);
884 if (!entry.mask) {
885 entry.mask = 1;
886 ioapic_write_entry(apic, pin, entry);
887 }
888 }
5ffa4eb2 889
54168ed7 890 return 0;
5ffa4eb2
CG
891
892nomem:
c1370b49
CG
893 while (apic >= 0)
894 kfree(early_ioapic_entries[apic--]);
5ffa4eb2
CG
895 memset(early_ioapic_entries, 0,
896 ARRAY_SIZE(early_ioapic_entries));
897
898 return -ENOMEM;
54168ed7
IM
899}
900
901void restore_IO_APIC_setup(void)
902{
903 int apic, pin;
904
5ffa4eb2
CG
905 for (apic = 0; apic < nr_ioapics; apic++) {
906 if (!early_ioapic_entries[apic])
907 break;
54168ed7
IM
908 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
909 ioapic_write_entry(apic, pin,
910 early_ioapic_entries[apic][pin]);
5ffa4eb2
CG
911 kfree(early_ioapic_entries[apic]);
912 early_ioapic_entries[apic] = NULL;
913 }
54168ed7
IM
914}
915
916void reinit_intr_remapped_IO_APIC(int intr_remapping)
917{
918 /*
919 * for now plain restore of previous settings.
920 * TBD: In the case of OS enabling interrupt-remapping,
921 * IO-APIC RTE's need to be setup to point to interrupt-remapping
922 * table entries. for now, do a plain restore, and wait for
923 * the setup_IO_APIC_irqs() to do proper initialization.
924 */
925 restore_IO_APIC_setup();
926}
927#endif
1da177e4
LT
928
929/*
930 * Find the IRQ entry number of a certain pin.
931 */
932static int find_irq_entry(int apic, int pin, int type)
933{
934 int i;
935
936 for (i = 0; i < mp_irq_entries; i++)
c2c21745
JSR
937 if (mp_irqs[i].irqtype == type &&
938 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
939 mp_irqs[i].dstapic == MP_APIC_ALL) &&
940 mp_irqs[i].dstirq == pin)
1da177e4
LT
941 return i;
942
943 return -1;
944}
945
946/*
947 * Find the pin to which IRQ[irq] (ISA) is connected
948 */
fcfd636a 949static int __init find_isa_irq_pin(int irq, int type)
1da177e4
LT
950{
951 int i;
952
953 for (i = 0; i < mp_irq_entries; i++) {
c2c21745 954 int lbus = mp_irqs[i].srcbus;
1da177e4 955
d27e2b8e 956 if (test_bit(lbus, mp_bus_not_pci) &&
c2c21745
JSR
957 (mp_irqs[i].irqtype == type) &&
958 (mp_irqs[i].srcbusirq == irq))
1da177e4 959
c2c21745 960 return mp_irqs[i].dstirq;
1da177e4
LT
961 }
962 return -1;
963}
964
fcfd636a
EB
965static int __init find_isa_irq_apic(int irq, int type)
966{
967 int i;
968
969 for (i = 0; i < mp_irq_entries; i++) {
c2c21745 970 int lbus = mp_irqs[i].srcbus;
fcfd636a 971
73b2961b 972 if (test_bit(lbus, mp_bus_not_pci) &&
c2c21745
JSR
973 (mp_irqs[i].irqtype == type) &&
974 (mp_irqs[i].srcbusirq == irq))
fcfd636a
EB
975 break;
976 }
977 if (i < mp_irq_entries) {
978 int apic;
54168ed7 979 for(apic = 0; apic < nr_ioapics; apic++) {
c2c21745 980 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
fcfd636a
EB
981 return apic;
982 }
983 }
984
985 return -1;
986}
987
1da177e4
LT
988/*
989 * Find a specific PCI IRQ entry.
990 * Not an __init, possibly needed by modules
991 */
992static int pin_2_irq(int idx, int apic, int pin);
993
994int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
995{
996 int apic, i, best_guess = -1;
997
54168ed7
IM
998 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
999 bus, slot, pin);
ce6444d3 1000 if (test_bit(bus, mp_bus_not_pci)) {
54168ed7 1001 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1da177e4
LT
1002 return -1;
1003 }
1004 for (i = 0; i < mp_irq_entries; i++) {
c2c21745 1005 int lbus = mp_irqs[i].srcbus;
1da177e4
LT
1006
1007 for (apic = 0; apic < nr_ioapics; apic++)
c2c21745
JSR
1008 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1009 mp_irqs[i].dstapic == MP_APIC_ALL)
1da177e4
LT
1010 break;
1011
47cab822 1012 if (!test_bit(lbus, mp_bus_not_pci) &&
c2c21745 1013 !mp_irqs[i].irqtype &&
1da177e4 1014 (bus == lbus) &&
c2c21745
JSR
1015 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1016 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1da177e4
LT
1017
1018 if (!(apic || IO_APIC_IRQ(irq)))
1019 continue;
1020
c2c21745 1021 if (pin == (mp_irqs[i].srcbusirq & 3))
1da177e4
LT
1022 return irq;
1023 /*
1024 * Use the first all-but-pin matching entry as a
1025 * best-guess fuzzy result for broken mptables.
1026 */
1027 if (best_guess < 0)
1028 best_guess = irq;
1029 }
1030 }
1031 return best_guess;
1032}
54168ed7 1033
129f6946 1034EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1da177e4 1035
c0a282c2 1036#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
1da177e4
LT
1037/*
1038 * EISA Edge/Level control register, ELCR
1039 */
1040static int EISA_ELCR(unsigned int irq)
1041{
99d093d1 1042 if (irq < NR_IRQS_LEGACY) {
1da177e4
LT
1043 unsigned int port = 0x4d0 + (irq >> 3);
1044 return (inb(port) >> (irq & 7)) & 1;
1045 }
1046 apic_printk(APIC_VERBOSE, KERN_INFO
1047 "Broken MPtable reports ISA irq %d\n", irq);
1048 return 0;
1049}
54168ed7 1050
c0a282c2 1051#endif
1da177e4 1052
6728801d
AS
1053/* ISA interrupts are always polarity zero edge triggered,
1054 * when listed as conforming in the MP table. */
1055
1056#define default_ISA_trigger(idx) (0)
1057#define default_ISA_polarity(idx) (0)
1058
1da177e4
LT
1059/* EISA interrupts are always polarity zero and can be edge or level
1060 * trigger depending on the ELCR value. If an interrupt is listed as
1061 * EISA conforming in the MP table, that means its trigger type must
1062 * be read in from the ELCR */
1063
c2c21745 1064#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
6728801d 1065#define default_EISA_polarity(idx) default_ISA_polarity(idx)
1da177e4
LT
1066
1067/* PCI interrupts are always polarity one level triggered,
1068 * when listed as conforming in the MP table. */
1069
1070#define default_PCI_trigger(idx) (1)
1071#define default_PCI_polarity(idx) (1)
1072
1073/* MCA interrupts are always polarity zero level triggered,
1074 * when listed as conforming in the MP table. */
1075
1076#define default_MCA_trigger(idx) (1)
6728801d 1077#define default_MCA_polarity(idx) default_ISA_polarity(idx)
1da177e4 1078
61fd47e0 1079static int MPBIOS_polarity(int idx)
1da177e4 1080{
c2c21745 1081 int bus = mp_irqs[idx].srcbus;
1da177e4
LT
1082 int polarity;
1083
1084 /*
1085 * Determine IRQ line polarity (high active or low active):
1086 */
c2c21745 1087 switch (mp_irqs[idx].irqflag & 3)
36062448 1088 {
54168ed7
IM
1089 case 0: /* conforms, ie. bus-type dependent polarity */
1090 if (test_bit(bus, mp_bus_not_pci))
1091 polarity = default_ISA_polarity(idx);
1092 else
1093 polarity = default_PCI_polarity(idx);
1094 break;
1095 case 1: /* high active */
1096 {
1097 polarity = 0;
1098 break;
1099 }
1100 case 2: /* reserved */
1101 {
1102 printk(KERN_WARNING "broken BIOS!!\n");
1103 polarity = 1;
1104 break;
1105 }
1106 case 3: /* low active */
1107 {
1108 polarity = 1;
1109 break;
1110 }
1111 default: /* invalid */
1112 {
1113 printk(KERN_WARNING "broken BIOS!!\n");
1114 polarity = 1;
1115 break;
1116 }
1da177e4
LT
1117 }
1118 return polarity;
1119}
1120
1121static int MPBIOS_trigger(int idx)
1122{
c2c21745 1123 int bus = mp_irqs[idx].srcbus;
1da177e4
LT
1124 int trigger;
1125
1126 /*
1127 * Determine IRQ trigger mode (edge or level sensitive):
1128 */
c2c21745 1129 switch ((mp_irqs[idx].irqflag>>2) & 3)
1da177e4 1130 {
54168ed7
IM
1131 case 0: /* conforms, ie. bus-type dependent */
1132 if (test_bit(bus, mp_bus_not_pci))
1133 trigger = default_ISA_trigger(idx);
1134 else
1135 trigger = default_PCI_trigger(idx);
c0a282c2 1136#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
54168ed7
IM
1137 switch (mp_bus_id_to_type[bus]) {
1138 case MP_BUS_ISA: /* ISA pin */
1139 {
1140 /* set before the switch */
1141 break;
1142 }
1143 case MP_BUS_EISA: /* EISA pin */
1144 {
1145 trigger = default_EISA_trigger(idx);
1146 break;
1147 }
1148 case MP_BUS_PCI: /* PCI pin */
1149 {
1150 /* set before the switch */
1151 break;
1152 }
1153 case MP_BUS_MCA: /* MCA pin */
1154 {
1155 trigger = default_MCA_trigger(idx);
1156 break;
1157 }
1158 default:
1159 {
1160 printk(KERN_WARNING "broken BIOS!!\n");
1161 trigger = 1;
1162 break;
1163 }
1164 }
1165#endif
1da177e4 1166 break;
54168ed7 1167 case 1: /* edge */
1da177e4 1168 {
54168ed7 1169 trigger = 0;
1da177e4
LT
1170 break;
1171 }
54168ed7 1172 case 2: /* reserved */
1da177e4 1173 {
54168ed7
IM
1174 printk(KERN_WARNING "broken BIOS!!\n");
1175 trigger = 1;
1da177e4
LT
1176 break;
1177 }
54168ed7 1178 case 3: /* level */
1da177e4 1179 {
54168ed7 1180 trigger = 1;
1da177e4
LT
1181 break;
1182 }
54168ed7 1183 default: /* invalid */
1da177e4
LT
1184 {
1185 printk(KERN_WARNING "broken BIOS!!\n");
54168ed7 1186 trigger = 0;
1da177e4
LT
1187 break;
1188 }
1189 }
1190 return trigger;
1191}
1192
1193static inline int irq_polarity(int idx)
1194{
1195 return MPBIOS_polarity(idx);
1196}
1197
1198static inline int irq_trigger(int idx)
1199{
1200 return MPBIOS_trigger(idx);
1201}
1202
efa2559f 1203int (*ioapic_renumber_irq)(int ioapic, int irq);
1da177e4
LT
1204static int pin_2_irq(int idx, int apic, int pin)
1205{
1206 int irq, i;
c2c21745 1207 int bus = mp_irqs[idx].srcbus;
1da177e4
LT
1208
1209 /*
1210 * Debugging check, we are in big trouble if this message pops up!
1211 */
c2c21745 1212 if (mp_irqs[idx].dstirq != pin)
1da177e4
LT
1213 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1214
54168ed7 1215 if (test_bit(bus, mp_bus_not_pci)) {
c2c21745 1216 irq = mp_irqs[idx].srcbusirq;
54168ed7 1217 } else {
643befed
AS
1218 /*
1219 * PCI IRQs are mapped in order
1220 */
1221 i = irq = 0;
1222 while (i < apic)
1223 irq += nr_ioapic_registers[i++];
1224 irq += pin;
d6c88a50 1225 /*
54168ed7
IM
1226 * For MPS mode, so far only needed by ES7000 platform
1227 */
d6c88a50
TG
1228 if (ioapic_renumber_irq)
1229 irq = ioapic_renumber_irq(apic, irq);
1da177e4
LT
1230 }
1231
54168ed7 1232#ifdef CONFIG_X86_32
1da177e4
LT
1233 /*
1234 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1235 */
1236 if ((pin >= 16) && (pin <= 23)) {
1237 if (pirq_entries[pin-16] != -1) {
1238 if (!pirq_entries[pin-16]) {
1239 apic_printk(APIC_VERBOSE, KERN_DEBUG
1240 "disabling PIRQ%d\n", pin-16);
1241 } else {
1242 irq = pirq_entries[pin-16];
1243 apic_printk(APIC_VERBOSE, KERN_DEBUG
1244 "using PIRQ%d -> IRQ %d\n",
1245 pin-16, irq);
1246 }
1247 }
1248 }
54168ed7
IM
1249#endif
1250
1da177e4
LT
1251 return irq;
1252}
1253
497c9a19
YL
1254void lock_vector_lock(void)
1255{
1256 /* Used to the online set of cpus does not change
1257 * during assign_irq_vector.
1258 */
1259 spin_lock(&vector_lock);
1260}
1da177e4 1261
497c9a19 1262void unlock_vector_lock(void)
1da177e4 1263{
497c9a19
YL
1264 spin_unlock(&vector_lock);
1265}
1da177e4 1266
e7986739
MT
1267static int
1268__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
497c9a19 1269{
047c8fdb
YL
1270 /*
1271 * NOTE! The local APIC isn't very good at handling
1272 * multiple interrupts at the same interrupt level.
1273 * As the interrupt level is determined by taking the
1274 * vector number and shifting that right by 4, we
1275 * want to spread these out a bit so that they don't
1276 * all fall in the same interrupt level.
1277 *
1278 * Also, we've got to be careful not to trash gate
1279 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1280 */
54168ed7
IM
1281 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1282 unsigned int old_vector;
22f65d31
MT
1283 int cpu, err;
1284 cpumask_var_t tmp_mask;
ace80ab7 1285
54168ed7
IM
1286 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1287 return -EBUSY;
0a1ad60d 1288
22f65d31
MT
1289 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1290 return -ENOMEM;
ace80ab7 1291
54168ed7
IM
1292 old_vector = cfg->vector;
1293 if (old_vector) {
22f65d31
MT
1294 cpumask_and(tmp_mask, mask, cpu_online_mask);
1295 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1296 if (!cpumask_empty(tmp_mask)) {
1297 free_cpumask_var(tmp_mask);
54168ed7 1298 return 0;
22f65d31 1299 }
54168ed7 1300 }
497c9a19 1301
e7986739 1302 /* Only try and allocate irqs on cpus that are present */
22f65d31
MT
1303 err = -ENOSPC;
1304 for_each_cpu_and(cpu, mask, cpu_online_mask) {
54168ed7
IM
1305 int new_cpu;
1306 int vector, offset;
497c9a19 1307
e2d40b18 1308 apic->vector_allocation_domain(cpu, tmp_mask);
497c9a19 1309
54168ed7
IM
1310 vector = current_vector;
1311 offset = current_offset;
497c9a19 1312next:
54168ed7
IM
1313 vector += 8;
1314 if (vector >= first_system_vector) {
e7986739 1315 /* If out of vectors on large boxen, must share them. */
54168ed7
IM
1316 offset = (offset + 1) % 8;
1317 vector = FIRST_DEVICE_VECTOR + offset;
1318 }
1319 if (unlikely(current_vector == vector))
1320 continue;
b77b881f
YL
1321
1322 if (test_bit(vector, used_vectors))
54168ed7 1323 goto next;
b77b881f 1324
22f65d31 1325 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
54168ed7
IM
1326 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1327 goto next;
1328 /* Found one! */
1329 current_vector = vector;
1330 current_offset = offset;
1331 if (old_vector) {
1332 cfg->move_in_progress = 1;
22f65d31 1333 cpumask_copy(cfg->old_domain, cfg->domain);
7a959cff 1334 }
22f65d31 1335 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
54168ed7
IM
1336 per_cpu(vector_irq, new_cpu)[vector] = irq;
1337 cfg->vector = vector;
22f65d31
MT
1338 cpumask_copy(cfg->domain, tmp_mask);
1339 err = 0;
1340 break;
54168ed7 1341 }
22f65d31
MT
1342 free_cpumask_var(tmp_mask);
1343 return err;
497c9a19
YL
1344}
1345
e7986739
MT
1346static int
1347assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
497c9a19
YL
1348{
1349 int err;
ace80ab7 1350 unsigned long flags;
ace80ab7
EB
1351
1352 spin_lock_irqsave(&vector_lock, flags);
3145e941 1353 err = __assign_irq_vector(irq, cfg, mask);
26a3c49c 1354 spin_unlock_irqrestore(&vector_lock, flags);
497c9a19
YL
1355 return err;
1356}
1357
3145e941 1358static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
497c9a19 1359{
497c9a19
YL
1360 int cpu, vector;
1361
497c9a19
YL
1362 BUG_ON(!cfg->vector);
1363
1364 vector = cfg->vector;
22f65d31 1365 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
497c9a19
YL
1366 per_cpu(vector_irq, cpu)[vector] = -1;
1367
1368 cfg->vector = 0;
22f65d31 1369 cpumask_clear(cfg->domain);
0ca4b6b0
MW
1370
1371 if (likely(!cfg->move_in_progress))
1372 return;
22f65d31 1373 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
0ca4b6b0
MW
1374 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1375 vector++) {
1376 if (per_cpu(vector_irq, cpu)[vector] != irq)
1377 continue;
1378 per_cpu(vector_irq, cpu)[vector] = -1;
1379 break;
1380 }
1381 }
1382 cfg->move_in_progress = 0;
497c9a19
YL
1383}
1384
1385void __setup_vector_irq(int cpu)
1386{
1387 /* Initialize vector_irq on a new cpu */
1388 /* This function must be called with vector_lock held */
1389 int irq, vector;
1390 struct irq_cfg *cfg;
0b8f1efa 1391 struct irq_desc *desc;
497c9a19
YL
1392
1393 /* Mark the inuse vectors */
0b8f1efa 1394 for_each_irq_desc(irq, desc) {
0b8f1efa 1395 cfg = desc->chip_data;
22f65d31 1396 if (!cpumask_test_cpu(cpu, cfg->domain))
497c9a19
YL
1397 continue;
1398 vector = cfg->vector;
497c9a19
YL
1399 per_cpu(vector_irq, cpu)[vector] = irq;
1400 }
1401 /* Mark the free vectors */
1402 for (vector = 0; vector < NR_VECTORS; ++vector) {
1403 irq = per_cpu(vector_irq, cpu)[vector];
1404 if (irq < 0)
1405 continue;
1406
1407 cfg = irq_cfg(irq);
22f65d31 1408 if (!cpumask_test_cpu(cpu, cfg->domain))
497c9a19 1409 per_cpu(vector_irq, cpu)[vector] = -1;
54168ed7 1410 }
1da177e4 1411}
3fde6900 1412
f5b9ed7a 1413static struct irq_chip ioapic_chip;
54168ed7
IM
1414#ifdef CONFIG_INTR_REMAP
1415static struct irq_chip ir_ioapic_chip;
1416#endif
1da177e4 1417
54168ed7
IM
1418#define IOAPIC_AUTO -1
1419#define IOAPIC_EDGE 0
1420#define IOAPIC_LEVEL 1
1da177e4 1421
047c8fdb 1422#ifdef CONFIG_X86_32
1d025192
YL
1423static inline int IO_APIC_irq_trigger(int irq)
1424{
d6c88a50 1425 int apic, idx, pin;
1d025192 1426
d6c88a50
TG
1427 for (apic = 0; apic < nr_ioapics; apic++) {
1428 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1429 idx = find_irq_entry(apic, pin, mp_INT);
1430 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1431 return irq_trigger(idx);
1432 }
1433 }
1434 /*
54168ed7
IM
1435 * nonexistent IRQs are edge default
1436 */
d6c88a50 1437 return 0;
1d025192 1438}
047c8fdb
YL
1439#else
1440static inline int IO_APIC_irq_trigger(int irq)
1441{
54168ed7 1442 return 1;
047c8fdb
YL
1443}
1444#endif
1d025192 1445
3145e941 1446static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
1da177e4 1447{
199751d7 1448
6ebcc00e 1449 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
047c8fdb 1450 trigger == IOAPIC_LEVEL)
08678b08 1451 desc->status |= IRQ_LEVEL;
047c8fdb
YL
1452 else
1453 desc->status &= ~IRQ_LEVEL;
1454
54168ed7
IM
1455#ifdef CONFIG_INTR_REMAP
1456 if (irq_remapped(irq)) {
1457 desc->status |= IRQ_MOVE_PCNTXT;
1458 if (trigger)
1459 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1460 handle_fasteoi_irq,
1461 "fasteoi");
1462 else
1463 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1464 handle_edge_irq, "edge");
1465 return;
1466 }
1467#endif
047c8fdb
YL
1468 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1469 trigger == IOAPIC_LEVEL)
a460e745 1470 set_irq_chip_and_handler_name(irq, &ioapic_chip,
54168ed7
IM
1471 handle_fasteoi_irq,
1472 "fasteoi");
047c8fdb 1473 else
a460e745 1474 set_irq_chip_and_handler_name(irq, &ioapic_chip,
54168ed7 1475 handle_edge_irq, "edge");
1da177e4
LT
1476}
1477
ca97ab90
JF
1478int setup_ioapic_entry(int apic_id, int irq,
1479 struct IO_APIC_route_entry *entry,
1480 unsigned int destination, int trigger,
1481 int polarity, int vector)
1da177e4 1482{
497c9a19
YL
1483 /*
1484 * add it to the IO-APIC irq-routing table:
1485 */
1486 memset(entry,0,sizeof(*entry));
1487
54168ed7
IM
1488#ifdef CONFIG_INTR_REMAP
1489 if (intr_remapping_enabled) {
c8d46cf0 1490 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
54168ed7
IM
1491 struct irte irte;
1492 struct IR_IO_APIC_route_entry *ir_entry =
1493 (struct IR_IO_APIC_route_entry *) entry;
1494 int index;
1495
1496 if (!iommu)
c8d46cf0 1497 panic("No mapping iommu for ioapic %d\n", apic_id);
54168ed7
IM
1498
1499 index = alloc_irte(iommu, irq, 1);
1500 if (index < 0)
c8d46cf0 1501 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
54168ed7
IM
1502
1503 memset(&irte, 0, sizeof(irte));
1504
1505 irte.present = 1;
9b5bc8dc 1506 irte.dst_mode = apic->irq_dest_mode;
54168ed7 1507 irte.trigger_mode = trigger;
9b5bc8dc 1508 irte.dlvry_mode = apic->irq_delivery_mode;
54168ed7
IM
1509 irte.vector = vector;
1510 irte.dest_id = IRTE_DEST(destination);
1511
1512 modify_irte(irq, &irte);
1513
1514 ir_entry->index2 = (index >> 15) & 0x1;
1515 ir_entry->zero = 0;
1516 ir_entry->format = 1;
1517 ir_entry->index = (index & 0x7fff);
1518 } else
1519#endif
1520 {
9b5bc8dc
IM
1521 entry->delivery_mode = apic->irq_delivery_mode;
1522 entry->dest_mode = apic->irq_dest_mode;
54168ed7
IM
1523 entry->dest = destination;
1524 }
497c9a19 1525
54168ed7 1526 entry->mask = 0; /* enable IRQ */
497c9a19
YL
1527 entry->trigger = trigger;
1528 entry->polarity = polarity;
1529 entry->vector = vector;
1530
1531 /* Mask level triggered irqs.
1532 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1533 */
1534 if (trigger)
1535 entry->mask = 1;
497c9a19
YL
1536 return 0;
1537}
1538
c8d46cf0 1539static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
54168ed7 1540 int trigger, int polarity)
497c9a19
YL
1541{
1542 struct irq_cfg *cfg;
1da177e4 1543 struct IO_APIC_route_entry entry;
22f65d31 1544 unsigned int dest;
497c9a19
YL
1545
1546 if (!IO_APIC_IRQ(irq))
1547 return;
1548
3145e941 1549 cfg = desc->chip_data;
497c9a19 1550
fe402e1f 1551 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
497c9a19
YL
1552 return;
1553
debccb3e 1554 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
497c9a19
YL
1555
1556 apic_printk(APIC_VERBOSE,KERN_DEBUG
1557 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1558 "IRQ %d Mode:%i Active:%i)\n",
c8d46cf0 1559 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
497c9a19
YL
1560 irq, trigger, polarity);
1561
1562
c8d46cf0 1563 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
22f65d31 1564 dest, trigger, polarity, cfg->vector)) {
497c9a19 1565 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
c8d46cf0 1566 mp_ioapics[apic_id].apicid, pin);
3145e941 1567 __clear_irq_vector(irq, cfg);
497c9a19
YL
1568 return;
1569 }
1570
3145e941 1571 ioapic_register_intr(irq, desc, trigger);
99d093d1 1572 if (irq < NR_IRQS_LEGACY)
497c9a19
YL
1573 disable_8259A_irq(irq);
1574
c8d46cf0 1575 ioapic_write_entry(apic_id, pin, entry);
497c9a19
YL
1576}
1577
1578static void __init setup_IO_APIC_irqs(void)
1579{
c8d46cf0 1580 int apic_id, pin, idx, irq;
3c2cbd24 1581 int notcon = 0;
0b8f1efa 1582 struct irq_desc *desc;
3145e941 1583 struct irq_cfg *cfg;
0b8f1efa 1584 int cpu = boot_cpu_id;
1da177e4
LT
1585
1586 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1587
c8d46cf0
IM
1588 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
1589 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
20d225b9 1590
c8d46cf0 1591 idx = find_irq_entry(apic_id, pin, mp_INT);
3c2cbd24 1592 if (idx == -1) {
2a554fb1 1593 if (!notcon) {
3c2cbd24 1594 notcon = 1;
2a554fb1
CG
1595 apic_printk(APIC_VERBOSE,
1596 KERN_DEBUG " %d-%d",
c8d46cf0 1597 mp_ioapics[apic_id].apicid, pin);
2a554fb1
CG
1598 } else
1599 apic_printk(APIC_VERBOSE, " %d-%d",
c8d46cf0 1600 mp_ioapics[apic_id].apicid, pin);
3c2cbd24
CG
1601 continue;
1602 }
56ffa1a0
CG
1603 if (notcon) {
1604 apic_printk(APIC_VERBOSE,
1605 " (apicid-pin) not connected\n");
1606 notcon = 0;
1607 }
3c2cbd24 1608
c8d46cf0 1609 irq = pin_2_irq(idx, apic_id, pin);
33a201fa
IM
1610
1611 /*
1612 * Skip the timer IRQ if there's a quirk handler
1613 * installed and if it returns 1:
1614 */
1615 if (apic->multi_timer_check &&
1616 apic->multi_timer_check(apic_id, irq))
3c2cbd24 1617 continue;
33a201fa 1618
0b8f1efa
YL
1619 desc = irq_to_desc_alloc_cpu(irq, cpu);
1620 if (!desc) {
1621 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1622 continue;
1623 }
3145e941 1624 cfg = desc->chip_data;
c8d46cf0 1625 add_pin_to_irq_cpu(cfg, cpu, apic_id, pin);
36062448 1626
c8d46cf0 1627 setup_IO_APIC_irq(apic_id, pin, irq, desc,
3c2cbd24
CG
1628 irq_trigger(idx), irq_polarity(idx));
1629 }
1da177e4
LT
1630 }
1631
3c2cbd24
CG
1632 if (notcon)
1633 apic_printk(APIC_VERBOSE,
2a554fb1 1634 " (apicid-pin) not connected\n");
1da177e4
LT
1635}
1636
1637/*
f7633ce5 1638 * Set up the timer pin, possibly with the 8259A-master behind.
1da177e4 1639 */
c8d46cf0 1640static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
f7633ce5 1641 int vector)
1da177e4
LT
1642{
1643 struct IO_APIC_route_entry entry;
1da177e4 1644
54168ed7
IM
1645#ifdef CONFIG_INTR_REMAP
1646 if (intr_remapping_enabled)
1647 return;
1648#endif
1649
36062448 1650 memset(&entry, 0, sizeof(entry));
1da177e4
LT
1651
1652 /*
1653 * We use logical delivery to get the timer IRQ
1654 * to the first CPU.
1655 */
9b5bc8dc 1656 entry.dest_mode = apic->irq_dest_mode;
f72dccac 1657 entry.mask = 0; /* don't mask IRQ for edge */
debccb3e 1658 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
9b5bc8dc 1659 entry.delivery_mode = apic->irq_delivery_mode;
1da177e4
LT
1660 entry.polarity = 0;
1661 entry.trigger = 0;
1662 entry.vector = vector;
1663
1664 /*
1665 * The timer IRQ doesn't have to know that behind the
f7633ce5 1666 * scene we may have a 8259A-master in AEOI mode ...
1da177e4 1667 */
54168ed7 1668 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1da177e4
LT
1669
1670 /*
1671 * Add it to the IO-APIC irq-routing table:
1672 */
c8d46cf0 1673 ioapic_write_entry(apic_id, pin, entry);
1da177e4
LT
1674}
1675
32f71aff
MR
1676
1677__apicdebuginit(void) print_IO_APIC(void)
1da177e4
LT
1678{
1679 int apic, i;
1680 union IO_APIC_reg_00 reg_00;
1681 union IO_APIC_reg_01 reg_01;
1682 union IO_APIC_reg_02 reg_02;
1683 union IO_APIC_reg_03 reg_03;
1684 unsigned long flags;
0f978f45 1685 struct irq_cfg *cfg;
0b8f1efa 1686 struct irq_desc *desc;
8f09cd20 1687 unsigned int irq;
1da177e4
LT
1688
1689 if (apic_verbosity == APIC_QUIET)
1690 return;
1691
36062448 1692 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1da177e4
LT
1693 for (i = 0; i < nr_ioapics; i++)
1694 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
b5ba7e6d 1695 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
1da177e4
LT
1696
1697 /*
1698 * We are a bit conservative about what we expect. We have to
1699 * know about every hardware change ASAP.
1700 */
1701 printk(KERN_INFO "testing the IO APIC.......................\n");
1702
1703 for (apic = 0; apic < nr_ioapics; apic++) {
1704
1705 spin_lock_irqsave(&ioapic_lock, flags);
1706 reg_00.raw = io_apic_read(apic, 0);
1707 reg_01.raw = io_apic_read(apic, 1);
1708 if (reg_01.bits.version >= 0x10)
1709 reg_02.raw = io_apic_read(apic, 2);
d6c88a50
TG
1710 if (reg_01.bits.version >= 0x20)
1711 reg_03.raw = io_apic_read(apic, 3);
1da177e4
LT
1712 spin_unlock_irqrestore(&ioapic_lock, flags);
1713
54168ed7 1714 printk("\n");
b5ba7e6d 1715 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
1da177e4
LT
1716 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1717 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1718 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1719 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1da177e4 1720
54168ed7 1721 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1da177e4 1722 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1da177e4
LT
1723
1724 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1725 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1da177e4
LT
1726
1727 /*
1728 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1729 * but the value of reg_02 is read as the previous read register
1730 * value, so ignore it if reg_02 == reg_01.
1731 */
1732 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1733 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1734 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1da177e4
LT
1735 }
1736
1737 /*
1738 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1739 * or reg_03, but the value of reg_0[23] is read as the previous read
1740 * register value, so ignore it if reg_03 == reg_0[12].
1741 */
1742 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1743 reg_03.raw != reg_01.raw) {
1744 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1745 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1da177e4
LT
1746 }
1747
1748 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1749
d83e94ac
YL
1750 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1751 " Stat Dmod Deli Vect: \n");
1da177e4
LT
1752
1753 for (i = 0; i <= reg_01.bits.entries; i++) {
1754 struct IO_APIC_route_entry entry;
1755
cf4c6a2f 1756 entry = ioapic_read_entry(apic, i);
1da177e4 1757
54168ed7
IM
1758 printk(KERN_DEBUG " %02x %03X ",
1759 i,
1760 entry.dest
1761 );
1da177e4
LT
1762
1763 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1764 entry.mask,
1765 entry.trigger,
1766 entry.irr,
1767 entry.polarity,
1768 entry.delivery_status,
1769 entry.dest_mode,
1770 entry.delivery_mode,
1771 entry.vector
1772 );
1773 }
1774 }
1da177e4 1775 printk(KERN_DEBUG "IRQ to pin mappings:\n");
0b8f1efa
YL
1776 for_each_irq_desc(irq, desc) {
1777 struct irq_pin_list *entry;
1778
0b8f1efa
YL
1779 cfg = desc->chip_data;
1780 entry = cfg->irq_2_pin;
0f978f45 1781 if (!entry)
1da177e4 1782 continue;
8f09cd20 1783 printk(KERN_DEBUG "IRQ%d ", irq);
1da177e4
LT
1784 for (;;) {
1785 printk("-> %d:%d", entry->apic, entry->pin);
1786 if (!entry->next)
1787 break;
0f978f45 1788 entry = entry->next;
1da177e4
LT
1789 }
1790 printk("\n");
1791 }
1792
1793 printk(KERN_INFO ".................................... done.\n");
1794
1795 return;
1796}
1797
32f71aff 1798__apicdebuginit(void) print_APIC_bitfield(int base)
1da177e4
LT
1799{
1800 unsigned int v;
1801 int i, j;
1802
1803 if (apic_verbosity == APIC_QUIET)
1804 return;
1805
1806 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1807 for (i = 0; i < 8; i++) {
1808 v = apic_read(base + i*0x10);
1809 for (j = 0; j < 32; j++) {
1810 if (v & (1<<j))
1811 printk("1");
1812 else
1813 printk("0");
1814 }
1815 printk("\n");
1816 }
1817}
1818
32f71aff 1819__apicdebuginit(void) print_local_APIC(void *dummy)
1da177e4
LT
1820{
1821 unsigned int v, ver, maxlvt;
7ab6af7a 1822 u64 icr;
1da177e4
LT
1823
1824 if (apic_verbosity == APIC_QUIET)
1825 return;
1826
1827 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1828 smp_processor_id(), hard_smp_processor_id());
66823114 1829 v = apic_read(APIC_ID);
54168ed7 1830 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
1da177e4
LT
1831 v = apic_read(APIC_LVR);
1832 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1833 ver = GET_APIC_VERSION(v);
e05d723f 1834 maxlvt = lapic_get_maxlvt();
1da177e4
LT
1835
1836 v = apic_read(APIC_TASKPRI);
1837 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1838
54168ed7 1839 if (APIC_INTEGRATED(ver)) { /* !82489DX */
a11b5abe
YL
1840 if (!APIC_XAPIC(ver)) {
1841 v = apic_read(APIC_ARBPRI);
1842 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1843 v & APIC_ARBPRI_MASK);
1844 }
1da177e4
LT
1845 v = apic_read(APIC_PROCPRI);
1846 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1847 }
1848
a11b5abe
YL
1849 /*
1850 * Remote read supported only in the 82489DX and local APIC for
1851 * Pentium processors.
1852 */
1853 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1854 v = apic_read(APIC_RRR);
1855 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1856 }
1857
1da177e4
LT
1858 v = apic_read(APIC_LDR);
1859 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
a11b5abe
YL
1860 if (!x2apic_enabled()) {
1861 v = apic_read(APIC_DFR);
1862 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1863 }
1da177e4
LT
1864 v = apic_read(APIC_SPIV);
1865 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1866
1867 printk(KERN_DEBUG "... APIC ISR field:\n");
1868 print_APIC_bitfield(APIC_ISR);
1869 printk(KERN_DEBUG "... APIC TMR field:\n");
1870 print_APIC_bitfield(APIC_TMR);
1871 printk(KERN_DEBUG "... APIC IRR field:\n");
1872 print_APIC_bitfield(APIC_IRR);
1873
54168ed7
IM
1874 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1875 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1da177e4 1876 apic_write(APIC_ESR, 0);
54168ed7 1877
1da177e4
LT
1878 v = apic_read(APIC_ESR);
1879 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1880 }
1881
7ab6af7a 1882 icr = apic_icr_read();
0c425cec
IM
1883 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1884 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
1da177e4
LT
1885
1886 v = apic_read(APIC_LVTT);
1887 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1888
1889 if (maxlvt > 3) { /* PC is LVT#4. */
1890 v = apic_read(APIC_LVTPC);
1891 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1892 }
1893 v = apic_read(APIC_LVT0);
1894 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1895 v = apic_read(APIC_LVT1);
1896 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1897
1898 if (maxlvt > 2) { /* ERR is LVT#3. */
1899 v = apic_read(APIC_LVTERR);
1900 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1901 }
1902
1903 v = apic_read(APIC_TMICT);
1904 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1905 v = apic_read(APIC_TMCCT);
1906 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1907 v = apic_read(APIC_TDCR);
1908 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1909 printk("\n");
1910}
1911
32f71aff 1912__apicdebuginit(void) print_all_local_APICs(void)
1da177e4 1913{
ffd5aae7
YL
1914 int cpu;
1915
1916 preempt_disable();
1917 for_each_online_cpu(cpu)
1918 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1919 preempt_enable();
1da177e4
LT
1920}
1921
32f71aff 1922__apicdebuginit(void) print_PIC(void)
1da177e4 1923{
1da177e4
LT
1924 unsigned int v;
1925 unsigned long flags;
1926
1927 if (apic_verbosity == APIC_QUIET)
1928 return;
1929
1930 printk(KERN_DEBUG "\nprinting PIC contents\n");
1931
1932 spin_lock_irqsave(&i8259A_lock, flags);
1933
1934 v = inb(0xa1) << 8 | inb(0x21);
1935 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1936
1937 v = inb(0xa0) << 8 | inb(0x20);
1938 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1939
54168ed7
IM
1940 outb(0x0b,0xa0);
1941 outb(0x0b,0x20);
1da177e4 1942 v = inb(0xa0) << 8 | inb(0x20);
54168ed7
IM
1943 outb(0x0a,0xa0);
1944 outb(0x0a,0x20);
1da177e4
LT
1945
1946 spin_unlock_irqrestore(&i8259A_lock, flags);
1947
1948 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1949
1950 v = inb(0x4d1) << 8 | inb(0x4d0);
1951 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1952}
1953
32f71aff
MR
1954__apicdebuginit(int) print_all_ICs(void)
1955{
1956 print_PIC();
1957 print_all_local_APICs();
1958 print_IO_APIC();
1959
1960 return 0;
1961}
1962
1963fs_initcall(print_all_ICs);
1964
1da177e4 1965
efa2559f
YL
1966/* Where if anywhere is the i8259 connect in external int mode */
1967static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1968
54168ed7 1969void __init enable_IO_APIC(void)
1da177e4
LT
1970{
1971 union IO_APIC_reg_01 reg_01;
fcfd636a 1972 int i8259_apic, i8259_pin;
54168ed7 1973 int apic;
1da177e4
LT
1974 unsigned long flags;
1975
1da177e4
LT
1976 /*
1977 * The number of IO-APIC IRQ registers (== #pins):
1978 */
fcfd636a 1979 for (apic = 0; apic < nr_ioapics; apic++) {
1da177e4 1980 spin_lock_irqsave(&ioapic_lock, flags);
fcfd636a 1981 reg_01.raw = io_apic_read(apic, 1);
1da177e4 1982 spin_unlock_irqrestore(&ioapic_lock, flags);
fcfd636a
EB
1983 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1984 }
54168ed7 1985 for(apic = 0; apic < nr_ioapics; apic++) {
fcfd636a
EB
1986 int pin;
1987 /* See if any of the pins is in ExtINT mode */
1008fddc 1988 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
fcfd636a 1989 struct IO_APIC_route_entry entry;
cf4c6a2f 1990 entry = ioapic_read_entry(apic, pin);
fcfd636a 1991
fcfd636a
EB
1992 /* If the interrupt line is enabled and in ExtInt mode
1993 * I have found the pin where the i8259 is connected.
1994 */
1995 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1996 ioapic_i8259.apic = apic;
1997 ioapic_i8259.pin = pin;
1998 goto found_i8259;
1999 }
2000 }
2001 }
2002 found_i8259:
2003 /* Look to see what if the MP table has reported the ExtINT */
2004 /* If we could not find the appropriate pin by looking at the ioapic
2005 * the i8259 probably is not connected the ioapic but give the
2006 * mptable a chance anyway.
2007 */
2008 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
2009 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
2010 /* Trust the MP table if nothing is setup in the hardware */
2011 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
2012 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
2013 ioapic_i8259.pin = i8259_pin;
2014 ioapic_i8259.apic = i8259_apic;
2015 }
2016 /* Complain if the MP table and the hardware disagree */
2017 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
2018 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
2019 {
2020 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1da177e4
LT
2021 }
2022
2023 /*
2024 * Do not trust the IO-APIC being empty at bootup
2025 */
2026 clear_IO_APIC();
2027}
2028
2029/*
2030 * Not an __init, needed by the reboot code
2031 */
2032void disable_IO_APIC(void)
2033{
2034 /*
2035 * Clear the IO-APIC before rebooting:
2036 */
2037 clear_IO_APIC();
2038
650927ef 2039 /*
0b968d23 2040 * If the i8259 is routed through an IOAPIC
650927ef 2041 * Put that IOAPIC in virtual wire mode
0b968d23 2042 * so legacy interrupts can be delivered.
7c6d9f97
SS
2043 *
2044 * With interrupt-remapping, for now we will use virtual wire A mode,
2045 * as virtual wire B is little complex (need to configure both
2046 * IOAPIC RTE aswell as interrupt-remapping table entry).
2047 * As this gets called during crash dump, keep this simple for now.
650927ef 2048 */
7c6d9f97 2049 if (ioapic_i8259.pin != -1 && !intr_remapping_enabled) {
650927ef 2050 struct IO_APIC_route_entry entry;
650927ef
EB
2051
2052 memset(&entry, 0, sizeof(entry));
2053 entry.mask = 0; /* Enabled */
2054 entry.trigger = 0; /* Edge */
2055 entry.irr = 0;
2056 entry.polarity = 0; /* High */
2057 entry.delivery_status = 0;
2058 entry.dest_mode = 0; /* Physical */
fcfd636a 2059 entry.delivery_mode = dest_ExtINT; /* ExtInt */
650927ef 2060 entry.vector = 0;
54168ed7 2061 entry.dest = read_apic_id();
650927ef
EB
2062
2063 /*
2064 * Add it to the IO-APIC irq-routing table:
2065 */
cf4c6a2f 2066 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
650927ef 2067 }
54168ed7 2068
7c6d9f97
SS
2069 /*
2070 * Use virtual wire A mode when interrupt remapping is enabled.
2071 */
2072 disconnect_bsp_APIC(!intr_remapping_enabled && ioapic_i8259.pin != -1);
1da177e4
LT
2073}
2074
54168ed7 2075#ifdef CONFIG_X86_32
1da177e4
LT
2076/*
2077 * function to set the IO-APIC physical IDs based on the
2078 * values stored in the MPC table.
2079 *
2080 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2081 */
2082
1da177e4
LT
2083static void __init setup_ioapic_ids_from_mpc(void)
2084{
2085 union IO_APIC_reg_00 reg_00;
2086 physid_mask_t phys_id_present_map;
c8d46cf0 2087 int apic_id;
1da177e4
LT
2088 int i;
2089 unsigned char old_id;
2090 unsigned long flags;
2091
a4dbc34d 2092 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
d49c4288 2093 return;
d49c4288 2094
ca05fea6
NP
2095 /*
2096 * Don't check I/O APIC IDs for xAPIC systems. They have
2097 * no meaning without the serial APIC bus.
2098 */
7c5c1e42
SL
2099 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2100 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
ca05fea6 2101 return;
1da177e4
LT
2102 /*
2103 * This is broken; anything with a real cpu count has to
2104 * circumvent this idiocy regardless.
2105 */
d190cb87 2106 phys_id_present_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
1da177e4
LT
2107
2108 /*
2109 * Set the IOAPIC ID to the value stored in the MPC table.
2110 */
c8d46cf0 2111 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
1da177e4
LT
2112
2113 /* Read the register 0 value */
2114 spin_lock_irqsave(&ioapic_lock, flags);
c8d46cf0 2115 reg_00.raw = io_apic_read(apic_id, 0);
1da177e4 2116 spin_unlock_irqrestore(&ioapic_lock, flags);
36062448 2117
c8d46cf0 2118 old_id = mp_ioapics[apic_id].apicid;
1da177e4 2119
c8d46cf0 2120 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
1da177e4 2121 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
c8d46cf0 2122 apic_id, mp_ioapics[apic_id].apicid);
1da177e4
LT
2123 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2124 reg_00.bits.ID);
c8d46cf0 2125 mp_ioapics[apic_id].apicid = reg_00.bits.ID;
1da177e4
LT
2126 }
2127
1da177e4
LT
2128 /*
2129 * Sanity check, is the ID really free? Every APIC in a
2130 * system must have a unique ID or we get lots of nice
2131 * 'stuck on smp_invalidate_needed IPI wait' messages.
2132 */
d1d7cae8 2133 if (apic->check_apicid_used(phys_id_present_map,
c8d46cf0 2134 mp_ioapics[apic_id].apicid)) {
1da177e4 2135 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
c8d46cf0 2136 apic_id, mp_ioapics[apic_id].apicid);
1da177e4
LT
2137 for (i = 0; i < get_physical_broadcast(); i++)
2138 if (!physid_isset(i, phys_id_present_map))
2139 break;
2140 if (i >= get_physical_broadcast())
2141 panic("Max APIC ID exceeded!\n");
2142 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2143 i);
2144 physid_set(i, phys_id_present_map);
c8d46cf0 2145 mp_ioapics[apic_id].apicid = i;
1da177e4
LT
2146 } else {
2147 physid_mask_t tmp;
8058714a 2148 tmp = apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid);
1da177e4
LT
2149 apic_printk(APIC_VERBOSE, "Setting %d in the "
2150 "phys_id_present_map\n",
c8d46cf0 2151 mp_ioapics[apic_id].apicid);
1da177e4
LT
2152 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2153 }
2154
2155
2156 /*
2157 * We need to adjust the IRQ routing table
2158 * if the ID changed.
2159 */
c8d46cf0 2160 if (old_id != mp_ioapics[apic_id].apicid)
1da177e4 2161 for (i = 0; i < mp_irq_entries; i++)
c2c21745
JSR
2162 if (mp_irqs[i].dstapic == old_id)
2163 mp_irqs[i].dstapic
c8d46cf0 2164 = mp_ioapics[apic_id].apicid;
1da177e4
LT
2165
2166 /*
2167 * Read the right value from the MPC table and
2168 * write it into the ID register.
36062448 2169 */
1da177e4
LT
2170 apic_printk(APIC_VERBOSE, KERN_INFO
2171 "...changing IO-APIC physical APIC ID to %d ...",
c8d46cf0 2172 mp_ioapics[apic_id].apicid);
1da177e4 2173
c8d46cf0 2174 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
1da177e4 2175 spin_lock_irqsave(&ioapic_lock, flags);
c8d46cf0 2176 io_apic_write(apic_id, 0, reg_00.raw);
a2d332fa 2177 spin_unlock_irqrestore(&ioapic_lock, flags);
1da177e4
LT
2178
2179 /*
2180 * Sanity check
2181 */
2182 spin_lock_irqsave(&ioapic_lock, flags);
c8d46cf0 2183 reg_00.raw = io_apic_read(apic_id, 0);
1da177e4 2184 spin_unlock_irqrestore(&ioapic_lock, flags);
c8d46cf0 2185 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
1da177e4
LT
2186 printk("could not set ID!\n");
2187 else
2188 apic_printk(APIC_VERBOSE, " ok.\n");
2189 }
2190}
54168ed7 2191#endif
1da177e4 2192
7ce0bcfd 2193int no_timer_check __initdata;
8542b200
ZA
2194
2195static int __init notimercheck(char *s)
2196{
2197 no_timer_check = 1;
2198 return 1;
2199}
2200__setup("no_timer_check", notimercheck);
2201
1da177e4
LT
2202/*
2203 * There is a nasty bug in some older SMP boards, their mptable lies
2204 * about the timer IRQ. We do the following to work around the situation:
2205 *
2206 * - timer IRQ defaults to IO-APIC IRQ
2207 * - if this function detects that timer IRQs are defunct, then we fall
2208 * back to ISA timer IRQs
2209 */
f0a7a5c9 2210static int __init timer_irq_works(void)
1da177e4
LT
2211{
2212 unsigned long t1 = jiffies;
4aae0702 2213 unsigned long flags;
1da177e4 2214
8542b200
ZA
2215 if (no_timer_check)
2216 return 1;
2217
4aae0702 2218 local_save_flags(flags);
1da177e4
LT
2219 local_irq_enable();
2220 /* Let ten ticks pass... */
2221 mdelay((10 * 1000) / HZ);
4aae0702 2222 local_irq_restore(flags);
1da177e4
LT
2223
2224 /*
2225 * Expect a few ticks at least, to be sure some possible
2226 * glue logic does not lock up after one or two first
2227 * ticks in a non-ExtINT mode. Also the local APIC
2228 * might have cached one ExtINT interrupt. Finally, at
2229 * least one tick may be lost due to delays.
2230 */
54168ed7
IM
2231
2232 /* jiffies wrap? */
1d16b53e 2233 if (time_after(jiffies, t1 + 4))
1da177e4 2234 return 1;
1da177e4
LT
2235 return 0;
2236}
2237
2238/*
2239 * In the SMP+IOAPIC case it might happen that there are an unspecified
2240 * number of pending IRQ events unhandled. These cases are very rare,
2241 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2242 * better to do it this way as thus we do not have to be aware of
2243 * 'pending' interrupts in the IRQ path, except at this point.
2244 */
2245/*
2246 * Edge triggered needs to resend any interrupt
2247 * that was delayed but this is now handled in the device
2248 * independent code.
2249 */
2250
2251/*
2252 * Starting up a edge-triggered IO-APIC interrupt is
2253 * nasty - we need to make sure that we get the edge.
2254 * If it is already asserted for some reason, we need
2255 * return 1 to indicate that is was pending.
2256 *
2257 * This is not complete - we should be able to fake
2258 * an edge even if it isn't on the 8259A...
2259 */
54168ed7 2260
f5b9ed7a 2261static unsigned int startup_ioapic_irq(unsigned int irq)
1da177e4
LT
2262{
2263 int was_pending = 0;
2264 unsigned long flags;
0b8f1efa 2265 struct irq_cfg *cfg;
1da177e4
LT
2266
2267 spin_lock_irqsave(&ioapic_lock, flags);
99d093d1 2268 if (irq < NR_IRQS_LEGACY) {
1da177e4
LT
2269 disable_8259A_irq(irq);
2270 if (i8259A_irq_pending(irq))
2271 was_pending = 1;
2272 }
0b8f1efa 2273 cfg = irq_cfg(irq);
3145e941 2274 __unmask_IO_APIC_irq(cfg);
1da177e4
LT
2275 spin_unlock_irqrestore(&ioapic_lock, flags);
2276
2277 return was_pending;
2278}
2279
54168ed7 2280#ifdef CONFIG_X86_64
ace80ab7 2281static int ioapic_retrigger_irq(unsigned int irq)
1da177e4 2282{
54168ed7
IM
2283
2284 struct irq_cfg *cfg = irq_cfg(irq);
2285 unsigned long flags;
2286
2287 spin_lock_irqsave(&vector_lock, flags);
dac5f412 2288 apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
54168ed7 2289 spin_unlock_irqrestore(&vector_lock, flags);
c0ad90a3
IM
2290
2291 return 1;
2292}
54168ed7
IM
2293#else
2294static int ioapic_retrigger_irq(unsigned int irq)
497c9a19 2295{
dac5f412 2296 apic->send_IPI_self(irq_cfg(irq)->vector);
497c9a19 2297
d6c88a50 2298 return 1;
54168ed7
IM
2299}
2300#endif
497c9a19 2301
54168ed7
IM
2302/*
2303 * Level and edge triggered IO-APIC interrupts need different handling,
2304 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2305 * handled with the level-triggered descriptor, but that one has slightly
2306 * more overhead. Level-triggered interrupts cannot be handled with the
2307 * edge-triggered handler, without risking IRQ storms and other ugly
2308 * races.
2309 */
497c9a19 2310
54168ed7 2311#ifdef CONFIG_SMP
497c9a19 2312
54168ed7
IM
2313#ifdef CONFIG_INTR_REMAP
2314static void ir_irq_migration(struct work_struct *work);
497c9a19 2315
54168ed7 2316static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
497c9a19 2317
54168ed7
IM
2318/*
2319 * Migrate the IO-APIC irq in the presence of intr-remapping.
2320 *
2321 * For edge triggered, irq migration is a simple atomic update(of vector
2322 * and cpu destination) of IRTE and flush the hardware cache.
2323 *
2324 * For level triggered, we need to modify the io-apic RTE aswell with the update
2325 * vector information, along with modifying IRTE with vector and destination.
2326 * So irq migration for level triggered is little bit more complex compared to
2327 * edge triggered migration. But the good news is, we use the same algorithm
2328 * for level triggered migration as we have today, only difference being,
2329 * we now initiate the irq migration from process context instead of the
2330 * interrupt context.
2331 *
2332 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2333 * suppression) to the IO-APIC, level triggered irq migration will also be
2334 * as simple as edge triggered migration and we can do the irq migration
2335 * with a simple atomic update to IO-APIC RTE.
2336 */
e7986739
MT
2337static void
2338migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
497c9a19 2339{
54168ed7 2340 struct irq_cfg *cfg;
54168ed7
IM
2341 struct irte irte;
2342 int modify_ioapic_rte;
2343 unsigned int dest;
2344 unsigned long flags;
3145e941 2345 unsigned int irq;
497c9a19 2346
22f65d31 2347 if (!cpumask_intersects(mask, cpu_online_mask))
497c9a19
YL
2348 return;
2349
3145e941 2350 irq = desc->irq;
54168ed7
IM
2351 if (get_irte(irq, &irte))
2352 return;
497c9a19 2353
3145e941
YL
2354 cfg = desc->chip_data;
2355 if (assign_irq_vector(irq, cfg, mask))
54168ed7
IM
2356 return;
2357
3145e941
YL
2358 set_extra_move_desc(desc, mask);
2359
debccb3e 2360 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
54168ed7 2361
54168ed7
IM
2362 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2363 if (modify_ioapic_rte) {
2364 spin_lock_irqsave(&ioapic_lock, flags);
3145e941 2365 __target_IO_APIC_irq(irq, dest, cfg);
54168ed7
IM
2366 spin_unlock_irqrestore(&ioapic_lock, flags);
2367 }
2368
2369 irte.vector = cfg->vector;
2370 irte.dest_id = IRTE_DEST(dest);
2371
2372 /*
2373 * Modified the IRTE and flushes the Interrupt entry cache.
2374 */
2375 modify_irte(irq, &irte);
2376
22f65d31
MT
2377 if (cfg->move_in_progress)
2378 send_cleanup_vector(cfg);
54168ed7 2379
7f7ace0c 2380 cpumask_copy(desc->affinity, mask);
54168ed7
IM
2381}
2382
3145e941 2383static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
54168ed7
IM
2384{
2385 int ret = -1;
3145e941 2386 struct irq_cfg *cfg = desc->chip_data;
54168ed7 2387
3145e941 2388 mask_IO_APIC_irq_desc(desc);
54168ed7 2389
3145e941 2390 if (io_apic_level_ack_pending(cfg)) {
54168ed7 2391 /*
d6c88a50 2392 * Interrupt in progress. Migrating irq now will change the
54168ed7
IM
2393 * vector information in the IO-APIC RTE and that will confuse
2394 * the EOI broadcast performed by cpu.
2395 * So, delay the irq migration to the next instance.
2396 */
2397 schedule_delayed_work(&ir_migration_work, 1);
2398 goto unmask;
2399 }
2400
2401 /* everthing is clear. we have right of way */
7f7ace0c 2402 migrate_ioapic_irq_desc(desc, desc->pending_mask);
54168ed7
IM
2403
2404 ret = 0;
2405 desc->status &= ~IRQ_MOVE_PENDING;
7f7ace0c 2406 cpumask_clear(desc->pending_mask);
54168ed7
IM
2407
2408unmask:
3145e941
YL
2409 unmask_IO_APIC_irq_desc(desc);
2410
54168ed7
IM
2411 return ret;
2412}
2413
2414static void ir_irq_migration(struct work_struct *work)
2415{
2416 unsigned int irq;
2417 struct irq_desc *desc;
2418
2419 for_each_irq_desc(irq, desc) {
2420 if (desc->status & IRQ_MOVE_PENDING) {
2421 unsigned long flags;
2422
2423 spin_lock_irqsave(&desc->lock, flags);
2424 if (!desc->chip->set_affinity ||
2425 !(desc->status & IRQ_MOVE_PENDING)) {
2426 desc->status &= ~IRQ_MOVE_PENDING;
2427 spin_unlock_irqrestore(&desc->lock, flags);
2428 continue;
2429 }
2430
7f7ace0c 2431 desc->chip->set_affinity(irq, desc->pending_mask);
54168ed7
IM
2432 spin_unlock_irqrestore(&desc->lock, flags);
2433 }
2434 }
2435}
2436
2437/*
2438 * Migrates the IRQ destination in the process context.
2439 */
968ea6d8
RR
2440static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2441 const struct cpumask *mask)
54168ed7 2442{
54168ed7
IM
2443 if (desc->status & IRQ_LEVEL) {
2444 desc->status |= IRQ_MOVE_PENDING;
7f7ace0c 2445 cpumask_copy(desc->pending_mask, mask);
3145e941 2446 migrate_irq_remapped_level_desc(desc);
54168ed7
IM
2447 return;
2448 }
2449
3145e941
YL
2450 migrate_ioapic_irq_desc(desc, mask);
2451}
968ea6d8
RR
2452static void set_ir_ioapic_affinity_irq(unsigned int irq,
2453 const struct cpumask *mask)
3145e941
YL
2454{
2455 struct irq_desc *desc = irq_to_desc(irq);
2456
2457 set_ir_ioapic_affinity_irq_desc(desc, mask);
54168ed7
IM
2458}
2459#endif
2460
2461asmlinkage void smp_irq_move_cleanup_interrupt(void)
2462{
2463 unsigned vector, me;
8f2466f4 2464
54168ed7 2465 ack_APIC_irq();
54168ed7 2466 exit_idle();
54168ed7
IM
2467 irq_enter();
2468
2469 me = smp_processor_id();
2470 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2471 unsigned int irq;
2472 struct irq_desc *desc;
2473 struct irq_cfg *cfg;
2474 irq = __get_cpu_var(vector_irq)[vector];
2475
0b8f1efa
YL
2476 if (irq == -1)
2477 continue;
2478
54168ed7
IM
2479 desc = irq_to_desc(irq);
2480 if (!desc)
2481 continue;
2482
2483 cfg = irq_cfg(irq);
2484 spin_lock(&desc->lock);
2485 if (!cfg->move_cleanup_count)
2486 goto unlock;
2487
22f65d31 2488 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
54168ed7
IM
2489 goto unlock;
2490
2491 __get_cpu_var(vector_irq)[vector] = -1;
2492 cfg->move_cleanup_count--;
2493unlock:
2494 spin_unlock(&desc->lock);
2495 }
2496
2497 irq_exit();
2498}
2499
3145e941 2500static void irq_complete_move(struct irq_desc **descp)
54168ed7 2501{
3145e941
YL
2502 struct irq_desc *desc = *descp;
2503 struct irq_cfg *cfg = desc->chip_data;
54168ed7
IM
2504 unsigned vector, me;
2505
48a1b10a
YL
2506 if (likely(!cfg->move_in_progress)) {
2507#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2508 if (likely(!cfg->move_desc_pending))
2509 return;
2510
b9098957 2511 /* domain has not changed, but affinity did */
48a1b10a 2512 me = smp_processor_id();
7f7ace0c 2513 if (cpumask_test_cpu(me, desc->affinity)) {
48a1b10a
YL
2514 *descp = desc = move_irq_desc(desc, me);
2515 /* get the new one */
2516 cfg = desc->chip_data;
2517 cfg->move_desc_pending = 0;
2518 }
2519#endif
54168ed7 2520 return;
48a1b10a 2521 }
54168ed7
IM
2522
2523 vector = ~get_irq_regs()->orig_ax;
2524 me = smp_processor_id();
10b888d6
YL
2525
2526 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain)) {
48a1b10a
YL
2527#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2528 *descp = desc = move_irq_desc(desc, me);
2529 /* get the new one */
2530 cfg = desc->chip_data;
2531#endif
22f65d31 2532 send_cleanup_vector(cfg);
10b888d6 2533 }
497c9a19
YL
2534}
2535#else
3145e941 2536static inline void irq_complete_move(struct irq_desc **descp) {}
497c9a19 2537#endif
3145e941 2538
54168ed7
IM
2539#ifdef CONFIG_INTR_REMAP
2540static void ack_x2apic_level(unsigned int irq)
2541{
2542 ack_x2APIC_irq();
2543}
2544
2545static void ack_x2apic_edge(unsigned int irq)
2546{
2547 ack_x2APIC_irq();
2548}
3145e941 2549
54168ed7 2550#endif
497c9a19 2551
1d025192
YL
2552static void ack_apic_edge(unsigned int irq)
2553{
3145e941
YL
2554 struct irq_desc *desc = irq_to_desc(irq);
2555
2556 irq_complete_move(&desc);
1d025192
YL
2557 move_native_irq(irq);
2558 ack_APIC_irq();
2559}
2560
3eb2cce8 2561atomic_t irq_mis_count;
3eb2cce8 2562
047c8fdb
YL
2563static void ack_apic_level(unsigned int irq)
2564{
3145e941
YL
2565 struct irq_desc *desc = irq_to_desc(irq);
2566
3eb2cce8
YL
2567#ifdef CONFIG_X86_32
2568 unsigned long v;
2569 int i;
2570#endif
3145e941 2571 struct irq_cfg *cfg;
54168ed7 2572 int do_unmask_irq = 0;
047c8fdb 2573
3145e941 2574 irq_complete_move(&desc);
047c8fdb 2575#ifdef CONFIG_GENERIC_PENDING_IRQ
54168ed7 2576 /* If we are moving the irq we need to mask it */
3145e941 2577 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
54168ed7 2578 do_unmask_irq = 1;
3145e941 2579 mask_IO_APIC_irq_desc(desc);
54168ed7 2580 }
047c8fdb
YL
2581#endif
2582
3eb2cce8
YL
2583#ifdef CONFIG_X86_32
2584 /*
2585 * It appears there is an erratum which affects at least version 0x11
2586 * of I/O APIC (that's the 82093AA and cores integrated into various
2587 * chipsets). Under certain conditions a level-triggered interrupt is
2588 * erroneously delivered as edge-triggered one but the respective IRR
2589 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2590 * message but it will never arrive and further interrupts are blocked
2591 * from the source. The exact reason is so far unknown, but the
2592 * phenomenon was observed when two consecutive interrupt requests
2593 * from a given source get delivered to the same CPU and the source is
2594 * temporarily disabled in between.
2595 *
2596 * A workaround is to simulate an EOI message manually. We achieve it
2597 * by setting the trigger mode to edge and then to level when the edge
2598 * trigger mode gets detected in the TMR of a local APIC for a
2599 * level-triggered interrupt. We mask the source for the time of the
2600 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2601 * The idea is from Manfred Spraul. --macro
2602 */
3145e941
YL
2603 cfg = desc->chip_data;
2604 i = cfg->vector;
3eb2cce8
YL
2605
2606 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2607#endif
2608
54168ed7
IM
2609 /*
2610 * We must acknowledge the irq before we move it or the acknowledge will
2611 * not propagate properly.
2612 */
2613 ack_APIC_irq();
2614
2615 /* Now we can move and renable the irq */
2616 if (unlikely(do_unmask_irq)) {
2617 /* Only migrate the irq if the ack has been received.
2618 *
2619 * On rare occasions the broadcast level triggered ack gets
2620 * delayed going to ioapics, and if we reprogram the
2621 * vector while Remote IRR is still set the irq will never
2622 * fire again.
2623 *
2624 * To prevent this scenario we read the Remote IRR bit
2625 * of the ioapic. This has two effects.
2626 * - On any sane system the read of the ioapic will
2627 * flush writes (and acks) going to the ioapic from
2628 * this cpu.
2629 * - We get to see if the ACK has actually been delivered.
2630 *
2631 * Based on failed experiments of reprogramming the
2632 * ioapic entry from outside of irq context starting
2633 * with masking the ioapic entry and then polling until
2634 * Remote IRR was clear before reprogramming the
2635 * ioapic I don't trust the Remote IRR bit to be
2636 * completey accurate.
2637 *
2638 * However there appears to be no other way to plug
2639 * this race, so if the Remote IRR bit is not
2640 * accurate and is causing problems then it is a hardware bug
2641 * and you can go talk to the chipset vendor about it.
2642 */
3145e941
YL
2643 cfg = desc->chip_data;
2644 if (!io_apic_level_ack_pending(cfg))
54168ed7 2645 move_masked_irq(irq);
3145e941 2646 unmask_IO_APIC_irq_desc(desc);
54168ed7 2647 }
1d025192 2648
3eb2cce8 2649#ifdef CONFIG_X86_32
1d025192
YL
2650 if (!(v & (1 << (i & 0x1f)))) {
2651 atomic_inc(&irq_mis_count);
2652 spin_lock(&ioapic_lock);
3145e941
YL
2653 __mask_and_edge_IO_APIC_irq(cfg);
2654 __unmask_and_level_IO_APIC_irq(cfg);
1d025192
YL
2655 spin_unlock(&ioapic_lock);
2656 }
047c8fdb 2657#endif
3eb2cce8 2658}
1d025192 2659
f5b9ed7a 2660static struct irq_chip ioapic_chip __read_mostly = {
d6c88a50
TG
2661 .name = "IO-APIC",
2662 .startup = startup_ioapic_irq,
2663 .mask = mask_IO_APIC_irq,
2664 .unmask = unmask_IO_APIC_irq,
2665 .ack = ack_apic_edge,
2666 .eoi = ack_apic_level,
54d5d424 2667#ifdef CONFIG_SMP
d6c88a50 2668 .set_affinity = set_ioapic_affinity_irq,
54d5d424 2669#endif
ace80ab7 2670 .retrigger = ioapic_retrigger_irq,
1da177e4
LT
2671};
2672
54168ed7
IM
2673#ifdef CONFIG_INTR_REMAP
2674static struct irq_chip ir_ioapic_chip __read_mostly = {
d6c88a50
TG
2675 .name = "IR-IO-APIC",
2676 .startup = startup_ioapic_irq,
2677 .mask = mask_IO_APIC_irq,
2678 .unmask = unmask_IO_APIC_irq,
2679 .ack = ack_x2apic_edge,
2680 .eoi = ack_x2apic_level,
54168ed7 2681#ifdef CONFIG_SMP
d6c88a50 2682 .set_affinity = set_ir_ioapic_affinity_irq,
54168ed7
IM
2683#endif
2684 .retrigger = ioapic_retrigger_irq,
2685};
2686#endif
1da177e4
LT
2687
2688static inline void init_IO_APIC_traps(void)
2689{
2690 int irq;
08678b08 2691 struct irq_desc *desc;
da51a821 2692 struct irq_cfg *cfg;
1da177e4
LT
2693
2694 /*
2695 * NOTE! The local APIC isn't very good at handling
2696 * multiple interrupts at the same interrupt level.
2697 * As the interrupt level is determined by taking the
2698 * vector number and shifting that right by 4, we
2699 * want to spread these out a bit so that they don't
2700 * all fall in the same interrupt level.
2701 *
2702 * Also, we've got to be careful not to trash gate
2703 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2704 */
0b8f1efa 2705 for_each_irq_desc(irq, desc) {
0b8f1efa
YL
2706 cfg = desc->chip_data;
2707 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
1da177e4
LT
2708 /*
2709 * Hmm.. We don't have an entry for this,
2710 * so default to an old-fashioned 8259
2711 * interrupt if we can..
2712 */
99d093d1 2713 if (irq < NR_IRQS_LEGACY)
1da177e4 2714 make_8259A_irq(irq);
0b8f1efa 2715 else
1da177e4 2716 /* Strange. Oh, well.. */
08678b08 2717 desc->chip = &no_irq_chip;
1da177e4
LT
2718 }
2719 }
2720}
2721
f5b9ed7a
IM
2722/*
2723 * The local APIC irq-chip implementation:
2724 */
1da177e4 2725
36062448 2726static void mask_lapic_irq(unsigned int irq)
1da177e4
LT
2727{
2728 unsigned long v;
2729
2730 v = apic_read(APIC_LVT0);
593f4a78 2731 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1da177e4
LT
2732}
2733
36062448 2734static void unmask_lapic_irq(unsigned int irq)
1da177e4 2735{
f5b9ed7a 2736 unsigned long v;
1da177e4 2737
f5b9ed7a 2738 v = apic_read(APIC_LVT0);
593f4a78 2739 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
f5b9ed7a 2740}
1da177e4 2741
3145e941 2742static void ack_lapic_irq(unsigned int irq)
1d025192
YL
2743{
2744 ack_APIC_irq();
2745}
2746
f5b9ed7a 2747static struct irq_chip lapic_chip __read_mostly = {
9a1c6192 2748 .name = "local-APIC",
f5b9ed7a
IM
2749 .mask = mask_lapic_irq,
2750 .unmask = unmask_lapic_irq,
c88ac1df 2751 .ack = ack_lapic_irq,
1da177e4
LT
2752};
2753
3145e941 2754static void lapic_register_intr(int irq, struct irq_desc *desc)
c88ac1df 2755{
08678b08 2756 desc->status &= ~IRQ_LEVEL;
c88ac1df
MR
2757 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2758 "edge");
c88ac1df
MR
2759}
2760
e9427101 2761static void __init setup_nmi(void)
1da177e4
LT
2762{
2763 /*
36062448 2764 * Dirty trick to enable the NMI watchdog ...
1da177e4
LT
2765 * We put the 8259A master into AEOI mode and
2766 * unmask on all local APICs LVT0 as NMI.
2767 *
2768 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2769 * is from Maciej W. Rozycki - so we do not have to EOI from
2770 * the NMI handler or the timer interrupt.
36062448 2771 */
1da177e4
LT
2772 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2773
e9427101 2774 enable_NMI_through_LVT0();
1da177e4
LT
2775
2776 apic_printk(APIC_VERBOSE, " done.\n");
2777}
2778
2779/*
2780 * This looks a bit hackish but it's about the only one way of sending
2781 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2782 * not support the ExtINT mode, unfortunately. We need to send these
2783 * cycles as some i82489DX-based boards have glue logic that keeps the
2784 * 8259A interrupt line asserted until INTA. --macro
2785 */
28acf285 2786static inline void __init unlock_ExtINT_logic(void)
1da177e4 2787{
fcfd636a 2788 int apic, pin, i;
1da177e4
LT
2789 struct IO_APIC_route_entry entry0, entry1;
2790 unsigned char save_control, save_freq_select;
1da177e4 2791
fcfd636a 2792 pin = find_isa_irq_pin(8, mp_INT);
956fb531
AB
2793 if (pin == -1) {
2794 WARN_ON_ONCE(1);
2795 return;
2796 }
fcfd636a 2797 apic = find_isa_irq_apic(8, mp_INT);
956fb531
AB
2798 if (apic == -1) {
2799 WARN_ON_ONCE(1);
1da177e4 2800 return;
956fb531 2801 }
1da177e4 2802
cf4c6a2f 2803 entry0 = ioapic_read_entry(apic, pin);
fcfd636a 2804 clear_IO_APIC_pin(apic, pin);
1da177e4
LT
2805
2806 memset(&entry1, 0, sizeof(entry1));
2807
2808 entry1.dest_mode = 0; /* physical delivery */
2809 entry1.mask = 0; /* unmask IRQ now */
d83e94ac 2810 entry1.dest = hard_smp_processor_id();
1da177e4
LT
2811 entry1.delivery_mode = dest_ExtINT;
2812 entry1.polarity = entry0.polarity;
2813 entry1.trigger = 0;
2814 entry1.vector = 0;
2815
cf4c6a2f 2816 ioapic_write_entry(apic, pin, entry1);
1da177e4
LT
2817
2818 save_control = CMOS_READ(RTC_CONTROL);
2819 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2820 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2821 RTC_FREQ_SELECT);
2822 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2823
2824 i = 100;
2825 while (i-- > 0) {
2826 mdelay(10);
2827 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2828 i -= 10;
2829 }
2830
2831 CMOS_WRITE(save_control, RTC_CONTROL);
2832 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
fcfd636a 2833 clear_IO_APIC_pin(apic, pin);
1da177e4 2834
cf4c6a2f 2835 ioapic_write_entry(apic, pin, entry0);
1da177e4
LT
2836}
2837
efa2559f 2838static int disable_timer_pin_1 __initdata;
047c8fdb 2839/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
54168ed7 2840static int __init disable_timer_pin_setup(char *arg)
efa2559f
YL
2841{
2842 disable_timer_pin_1 = 1;
2843 return 0;
2844}
54168ed7 2845early_param("disable_timer_pin_1", disable_timer_pin_setup);
efa2559f
YL
2846
2847int timer_through_8259 __initdata;
2848
1da177e4
LT
2849/*
2850 * This code may look a bit paranoid, but it's supposed to cooperate with
2851 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2852 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2853 * fanatically on his truly buggy board.
54168ed7
IM
2854 *
2855 * FIXME: really need to revamp this for all platforms.
1da177e4 2856 */
8542b200 2857static inline void __init check_timer(void)
1da177e4 2858{
3145e941
YL
2859 struct irq_desc *desc = irq_to_desc(0);
2860 struct irq_cfg *cfg = desc->chip_data;
2861 int cpu = boot_cpu_id;
fcfd636a 2862 int apic1, pin1, apic2, pin2;
4aae0702 2863 unsigned long flags;
047c8fdb 2864 int no_pin1 = 0;
4aae0702
IM
2865
2866 local_irq_save(flags);
d4d25dec 2867
1da177e4
LT
2868 /*
2869 * get/set the timer IRQ vector:
2870 */
2871 disable_8259A_irq(0);
fe402e1f 2872 assign_irq_vector(0, cfg, apic->target_cpus());
1da177e4
LT
2873
2874 /*
d11d5794
MR
2875 * As IRQ0 is to be enabled in the 8259A, the virtual
2876 * wire has to be disabled in the local APIC. Also
2877 * timer interrupts need to be acknowledged manually in
2878 * the 8259A for the i82489DX when using the NMI
2879 * watchdog as that APIC treats NMIs as level-triggered.
2880 * The AEOI mode will finish them in the 8259A
2881 * automatically.
1da177e4 2882 */
593f4a78 2883 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1da177e4 2884 init_8259A(1);
54168ed7 2885#ifdef CONFIG_X86_32
f72dccac
YL
2886 {
2887 unsigned int ver;
2888
2889 ver = apic_read(APIC_LVR);
2890 ver = GET_APIC_VERSION(ver);
2891 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2892 }
54168ed7 2893#endif
1da177e4 2894
fcfd636a
EB
2895 pin1 = find_isa_irq_pin(0, mp_INT);
2896 apic1 = find_isa_irq_apic(0, mp_INT);
2897 pin2 = ioapic_i8259.pin;
2898 apic2 = ioapic_i8259.apic;
1da177e4 2899
49a66a0b
MR
2900 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2901 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
497c9a19 2902 cfg->vector, apic1, pin1, apic2, pin2);
1da177e4 2903
691874fa
MR
2904 /*
2905 * Some BIOS writers are clueless and report the ExtINTA
2906 * I/O APIC input from the cascaded 8259A as the timer
2907 * interrupt input. So just in case, if only one pin
2908 * was found above, try it both directly and through the
2909 * 8259A.
2910 */
2911 if (pin1 == -1) {
54168ed7
IM
2912#ifdef CONFIG_INTR_REMAP
2913 if (intr_remapping_enabled)
2914 panic("BIOS bug: timer not connected to IO-APIC");
2915#endif
691874fa
MR
2916 pin1 = pin2;
2917 apic1 = apic2;
2918 no_pin1 = 1;
2919 } else if (pin2 == -1) {
2920 pin2 = pin1;
2921 apic2 = apic1;
2922 }
2923
1da177e4
LT
2924 if (pin1 != -1) {
2925 /*
2926 * Ok, does IRQ0 through the IOAPIC work?
2927 */
691874fa 2928 if (no_pin1) {
3145e941 2929 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
497c9a19 2930 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
f72dccac
YL
2931 } else {
2932 /* for edge trigger, setup_IO_APIC_irq already
2933 * leave it unmasked.
2934 * so only need to unmask if it is level-trigger
2935 * do we really have level trigger timer?
2936 */
2937 int idx;
2938 idx = find_irq_entry(apic1, pin1, mp_INT);
2939 if (idx != -1 && irq_trigger(idx))
2940 unmask_IO_APIC_irq_desc(desc);
691874fa 2941 }
1da177e4
LT
2942 if (timer_irq_works()) {
2943 if (nmi_watchdog == NMI_IO_APIC) {
1da177e4
LT
2944 setup_nmi();
2945 enable_8259A_irq(0);
1da177e4 2946 }
66759a01
CE
2947 if (disable_timer_pin_1 > 0)
2948 clear_IO_APIC_pin(0, pin1);
4aae0702 2949 goto out;
1da177e4 2950 }
54168ed7
IM
2951#ifdef CONFIG_INTR_REMAP
2952 if (intr_remapping_enabled)
2953 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2954#endif
f72dccac 2955 local_irq_disable();
fcfd636a 2956 clear_IO_APIC_pin(apic1, pin1);
691874fa 2957 if (!no_pin1)
49a66a0b
MR
2958 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2959 "8254 timer not connected to IO-APIC\n");
1da177e4 2960
49a66a0b
MR
2961 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2962 "(IRQ0) through the 8259A ...\n");
2963 apic_printk(APIC_QUIET, KERN_INFO
2964 "..... (found apic %d pin %d) ...\n", apic2, pin2);
1da177e4
LT
2965 /*
2966 * legacy devices should be connected to IO APIC #0
2967 */
3145e941 2968 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
497c9a19 2969 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
ecd29476 2970 enable_8259A_irq(0);
1da177e4 2971 if (timer_irq_works()) {
49a66a0b 2972 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
35542c5e 2973 timer_through_8259 = 1;
1da177e4 2974 if (nmi_watchdog == NMI_IO_APIC) {
60134ebe 2975 disable_8259A_irq(0);
1da177e4 2976 setup_nmi();
60134ebe 2977 enable_8259A_irq(0);
1da177e4 2978 }
4aae0702 2979 goto out;
1da177e4
LT
2980 }
2981 /*
2982 * Cleanup, just in case ...
2983 */
f72dccac 2984 local_irq_disable();
ecd29476 2985 disable_8259A_irq(0);
fcfd636a 2986 clear_IO_APIC_pin(apic2, pin2);
49a66a0b 2987 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
1da177e4 2988 }
1da177e4
LT
2989
2990 if (nmi_watchdog == NMI_IO_APIC) {
49a66a0b
MR
2991 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2992 "through the IO-APIC - disabling NMI Watchdog!\n");
067fa0ff 2993 nmi_watchdog = NMI_NONE;
1da177e4 2994 }
54168ed7 2995#ifdef CONFIG_X86_32
d11d5794 2996 timer_ack = 0;
54168ed7 2997#endif
1da177e4 2998
49a66a0b
MR
2999 apic_printk(APIC_QUIET, KERN_INFO
3000 "...trying to set up timer as Virtual Wire IRQ...\n");
1da177e4 3001
3145e941 3002 lapic_register_intr(0, desc);
497c9a19 3003 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
1da177e4
LT
3004 enable_8259A_irq(0);
3005
3006 if (timer_irq_works()) {
49a66a0b 3007 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
4aae0702 3008 goto out;
1da177e4 3009 }
f72dccac 3010 local_irq_disable();
e67465f1 3011 disable_8259A_irq(0);
497c9a19 3012 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
49a66a0b 3013 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
1da177e4 3014
49a66a0b
MR
3015 apic_printk(APIC_QUIET, KERN_INFO
3016 "...trying to set up timer as ExtINT IRQ...\n");
1da177e4 3017
1da177e4
LT
3018 init_8259A(0);
3019 make_8259A_irq(0);
593f4a78 3020 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4
LT
3021
3022 unlock_ExtINT_logic();
3023
3024 if (timer_irq_works()) {
49a66a0b 3025 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
4aae0702 3026 goto out;
1da177e4 3027 }
f72dccac 3028 local_irq_disable();
49a66a0b 3029 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
1da177e4 3030 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
49a66a0b 3031 "report. Then try booting with the 'noapic' option.\n");
4aae0702
IM
3032out:
3033 local_irq_restore(flags);
1da177e4
LT
3034}
3035
3036/*
af174783
MR
3037 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
3038 * to devices. However there may be an I/O APIC pin available for
3039 * this interrupt regardless. The pin may be left unconnected, but
3040 * typically it will be reused as an ExtINT cascade interrupt for
3041 * the master 8259A. In the MPS case such a pin will normally be
3042 * reported as an ExtINT interrupt in the MP table. With ACPI
3043 * there is no provision for ExtINT interrupts, and in the absence
3044 * of an override it would be treated as an ordinary ISA I/O APIC
3045 * interrupt, that is edge-triggered and unmasked by default. We
3046 * used to do this, but it caused problems on some systems because
3047 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3048 * the same ExtINT cascade interrupt to drive the local APIC of the
3049 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3050 * the I/O APIC in all cases now. No actual device should request
3051 * it anyway. --macro
1da177e4
LT
3052 */
3053#define PIC_IRQS (1 << PIC_CASCADE_IR)
3054
3055void __init setup_IO_APIC(void)
3056{
54168ed7 3057
54168ed7
IM
3058 /*
3059 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3060 */
1da177e4 3061
af174783 3062 io_apic_irqs = ~PIC_IRQS;
1da177e4 3063
54168ed7 3064 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
d6c88a50 3065 /*
54168ed7
IM
3066 * Set up IO-APIC IRQ routing.
3067 */
3068#ifdef CONFIG_X86_32
d6c88a50
TG
3069 if (!acpi_ioapic)
3070 setup_ioapic_ids_from_mpc();
54168ed7 3071#endif
1da177e4
LT
3072 sync_Arb_IDs();
3073 setup_IO_APIC_irqs();
3074 init_IO_APIC_traps();
1e4c85f9 3075 check_timer();
1da177e4
LT
3076}
3077
3078/*
54168ed7
IM
3079 * Called after all the initialization is done. If we didnt find any
3080 * APIC bugs then we can allow the modify fast path
1da177e4 3081 */
36062448 3082
1da177e4
LT
3083static int __init io_apic_bug_finalize(void)
3084{
d6c88a50
TG
3085 if (sis_apic_bug == -1)
3086 sis_apic_bug = 0;
3087 return 0;
1da177e4
LT
3088}
3089
3090late_initcall(io_apic_bug_finalize);
3091
3092struct sysfs_ioapic_data {
3093 struct sys_device dev;
3094 struct IO_APIC_route_entry entry[0];
3095};
54168ed7 3096static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1da177e4 3097
438510f6 3098static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
3099{
3100 struct IO_APIC_route_entry *entry;
3101 struct sysfs_ioapic_data *data;
1da177e4 3102 int i;
36062448 3103
1da177e4
LT
3104 data = container_of(dev, struct sysfs_ioapic_data, dev);
3105 entry = data->entry;
54168ed7
IM
3106 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3107 *entry = ioapic_read_entry(dev->id, i);
1da177e4
LT
3108
3109 return 0;
3110}
3111
3112static int ioapic_resume(struct sys_device *dev)
3113{
3114 struct IO_APIC_route_entry *entry;
3115 struct sysfs_ioapic_data *data;
3116 unsigned long flags;
3117 union IO_APIC_reg_00 reg_00;
3118 int i;
36062448 3119
1da177e4
LT
3120 data = container_of(dev, struct sysfs_ioapic_data, dev);
3121 entry = data->entry;
3122
3123 spin_lock_irqsave(&ioapic_lock, flags);
3124 reg_00.raw = io_apic_read(dev->id, 0);
b5ba7e6d
JSR
3125 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3126 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
1da177e4
LT
3127 io_apic_write(dev->id, 0, reg_00.raw);
3128 }
1da177e4 3129 spin_unlock_irqrestore(&ioapic_lock, flags);
36062448 3130 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
cf4c6a2f 3131 ioapic_write_entry(dev->id, i, entry[i]);
1da177e4
LT
3132
3133 return 0;
3134}
3135
3136static struct sysdev_class ioapic_sysdev_class = {
af5ca3f4 3137 .name = "ioapic",
1da177e4
LT
3138 .suspend = ioapic_suspend,
3139 .resume = ioapic_resume,
3140};
3141
3142static int __init ioapic_init_sysfs(void)
3143{
54168ed7
IM
3144 struct sys_device * dev;
3145 int i, size, error;
1da177e4
LT
3146
3147 error = sysdev_class_register(&ioapic_sysdev_class);
3148 if (error)
3149 return error;
3150
54168ed7 3151 for (i = 0; i < nr_ioapics; i++ ) {
36062448 3152 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1da177e4 3153 * sizeof(struct IO_APIC_route_entry);
25556c16 3154 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
1da177e4
LT
3155 if (!mp_ioapic_data[i]) {
3156 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3157 continue;
3158 }
1da177e4 3159 dev = &mp_ioapic_data[i]->dev;
36062448 3160 dev->id = i;
1da177e4
LT
3161 dev->cls = &ioapic_sysdev_class;
3162 error = sysdev_register(dev);
3163 if (error) {
3164 kfree(mp_ioapic_data[i]);
3165 mp_ioapic_data[i] = NULL;
3166 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3167 continue;
3168 }
3169 }
3170
3171 return 0;
3172}
3173
3174device_initcall(ioapic_init_sysfs);
3175
abcaa2b8 3176static int nr_irqs_gsi = NR_IRQS_LEGACY;
3fc471ed 3177/*
95d77884 3178 * Dynamic irq allocate and deallocation
3fc471ed 3179 */
199751d7 3180unsigned int create_irq_nr(unsigned int irq_want)
3fc471ed 3181{
ace80ab7 3182 /* Allocate an unused irq */
54168ed7
IM
3183 unsigned int irq;
3184 unsigned int new;
3fc471ed 3185 unsigned long flags;
0b8f1efa
YL
3186 struct irq_cfg *cfg_new = NULL;
3187 int cpu = boot_cpu_id;
3188 struct irq_desc *desc_new = NULL;
199751d7
YL
3189
3190 irq = 0;
abcaa2b8
YL
3191 if (irq_want < nr_irqs_gsi)
3192 irq_want = nr_irqs_gsi;
3193
ace80ab7 3194 spin_lock_irqsave(&vector_lock, flags);
9594949b 3195 for (new = irq_want; new < nr_irqs; new++) {
0b8f1efa
YL
3196 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3197 if (!desc_new) {
3198 printk(KERN_INFO "can not get irq_desc for %d\n", new);
ace80ab7 3199 continue;
0b8f1efa
YL
3200 }
3201 cfg_new = desc_new->chip_data;
3202
3203 if (cfg_new->vector != 0)
ace80ab7 3204 continue;
fe402e1f 3205 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
ace80ab7
EB
3206 irq = new;
3207 break;
3208 }
3209 spin_unlock_irqrestore(&vector_lock, flags);
3fc471ed 3210
199751d7 3211 if (irq > 0) {
3fc471ed 3212 dynamic_irq_init(irq);
0b8f1efa
YL
3213 /* restore it, in case dynamic_irq_init clear it */
3214 if (desc_new)
3215 desc_new->chip_data = cfg_new;
3fc471ed
EB
3216 }
3217 return irq;
3218}
3219
199751d7
YL
3220int create_irq(void)
3221{
be5d5350 3222 unsigned int irq_want;
54168ed7
IM
3223 int irq;
3224
be5d5350
YL
3225 irq_want = nr_irqs_gsi;
3226 irq = create_irq_nr(irq_want);
54168ed7
IM
3227
3228 if (irq == 0)
3229 irq = -1;
3230
3231 return irq;
199751d7
YL
3232}
3233
3fc471ed
EB
3234void destroy_irq(unsigned int irq)
3235{
3236 unsigned long flags;
0b8f1efa
YL
3237 struct irq_cfg *cfg;
3238 struct irq_desc *desc;
3fc471ed 3239
0b8f1efa
YL
3240 /* store it, in case dynamic_irq_cleanup clear it */
3241 desc = irq_to_desc(irq);
3242 cfg = desc->chip_data;
3fc471ed 3243 dynamic_irq_cleanup(irq);
0b8f1efa
YL
3244 /* connect back irq_cfg */
3245 if (desc)
3246 desc->chip_data = cfg;
3fc471ed 3247
54168ed7
IM
3248#ifdef CONFIG_INTR_REMAP
3249 free_irte(irq);
3250#endif
3fc471ed 3251 spin_lock_irqsave(&vector_lock, flags);
3145e941 3252 __clear_irq_vector(irq, cfg);
3fc471ed
EB
3253 spin_unlock_irqrestore(&vector_lock, flags);
3254}
3fc471ed 3255
2d3fcc1c 3256/*
27b46d76 3257 * MSI message composition
2d3fcc1c
EB
3258 */
3259#ifdef CONFIG_PCI_MSI
3b7d1921 3260static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
2d3fcc1c 3261{
497c9a19
YL
3262 struct irq_cfg *cfg;
3263 int err;
2d3fcc1c
EB
3264 unsigned dest;
3265
f1182638
JB
3266 if (disable_apic)
3267 return -ENXIO;
3268
3145e941 3269 cfg = irq_cfg(irq);
fe402e1f 3270 err = assign_irq_vector(irq, cfg, apic->target_cpus());
497c9a19
YL
3271 if (err)
3272 return err;
2d3fcc1c 3273
debccb3e 3274 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
497c9a19 3275
54168ed7
IM
3276#ifdef CONFIG_INTR_REMAP
3277 if (irq_remapped(irq)) {
3278 struct irte irte;
3279 int ir_index;
3280 u16 sub_handle;
3281
3282 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3283 BUG_ON(ir_index == -1);
3284
3285 memset (&irte, 0, sizeof(irte));
3286
3287 irte.present = 1;
9b5bc8dc 3288 irte.dst_mode = apic->irq_dest_mode;
54168ed7 3289 irte.trigger_mode = 0; /* edge */
9b5bc8dc 3290 irte.dlvry_mode = apic->irq_delivery_mode;
54168ed7
IM
3291 irte.vector = cfg->vector;
3292 irte.dest_id = IRTE_DEST(dest);
3293
3294 modify_irte(irq, &irte);
3295
3296 msg->address_hi = MSI_ADDR_BASE_HI;
3297 msg->data = sub_handle;
3298 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3299 MSI_ADDR_IR_SHV |
3300 MSI_ADDR_IR_INDEX1(ir_index) |
3301 MSI_ADDR_IR_INDEX2(ir_index);
3302 } else
3303#endif
3304 {
9d783ba0
SS
3305 if (x2apic_enabled())
3306 msg->address_hi = MSI_ADDR_BASE_HI |
3307 MSI_ADDR_EXT_DEST_ID(dest);
3308 else
3309 msg->address_hi = MSI_ADDR_BASE_HI;
3310
54168ed7
IM
3311 msg->address_lo =
3312 MSI_ADDR_BASE_LO |
9b5bc8dc 3313 ((apic->irq_dest_mode == 0) ?
54168ed7
IM
3314 MSI_ADDR_DEST_MODE_PHYSICAL:
3315 MSI_ADDR_DEST_MODE_LOGICAL) |
9b5bc8dc 3316 ((apic->irq_delivery_mode != dest_LowestPrio) ?
54168ed7
IM
3317 MSI_ADDR_REDIRECTION_CPU:
3318 MSI_ADDR_REDIRECTION_LOWPRI) |
3319 MSI_ADDR_DEST_ID(dest);
497c9a19 3320
54168ed7
IM
3321 msg->data =
3322 MSI_DATA_TRIGGER_EDGE |
3323 MSI_DATA_LEVEL_ASSERT |
9b5bc8dc 3324 ((apic->irq_delivery_mode != dest_LowestPrio) ?
54168ed7
IM
3325 MSI_DATA_DELIVERY_FIXED:
3326 MSI_DATA_DELIVERY_LOWPRI) |
3327 MSI_DATA_VECTOR(cfg->vector);
3328 }
497c9a19 3329 return err;
2d3fcc1c
EB
3330}
3331
3b7d1921 3332#ifdef CONFIG_SMP
0de26520 3333static void set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
2d3fcc1c 3334{
3145e941 3335 struct irq_desc *desc = irq_to_desc(irq);
497c9a19 3336 struct irq_cfg *cfg;
3b7d1921
EB
3337 struct msi_msg msg;
3338 unsigned int dest;
3b7d1921 3339
22f65d31
MT
3340 dest = set_desc_affinity(desc, mask);
3341 if (dest == BAD_APICID)
497c9a19 3342 return;
2d3fcc1c 3343
3145e941 3344 cfg = desc->chip_data;
2d3fcc1c 3345
3145e941 3346 read_msi_msg_desc(desc, &msg);
3b7d1921
EB
3347
3348 msg.data &= ~MSI_DATA_VECTOR_MASK;
497c9a19 3349 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3b7d1921
EB
3350 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3351 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3352
3145e941 3353 write_msi_msg_desc(desc, &msg);
2d3fcc1c 3354}
54168ed7
IM
3355#ifdef CONFIG_INTR_REMAP
3356/*
3357 * Migrate the MSI irq to another cpumask. This migration is
3358 * done in the process context using interrupt-remapping hardware.
3359 */
e7986739
MT
3360static void
3361ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
54168ed7 3362{
3145e941 3363 struct irq_desc *desc = irq_to_desc(irq);
a7883dec 3364 struct irq_cfg *cfg = desc->chip_data;
54168ed7 3365 unsigned int dest;
54168ed7 3366 struct irte irte;
54168ed7
IM
3367
3368 if (get_irte(irq, &irte))
3369 return;
3370
22f65d31
MT
3371 dest = set_desc_affinity(desc, mask);
3372 if (dest == BAD_APICID)
54168ed7
IM
3373 return;
3374
54168ed7
IM
3375 irte.vector = cfg->vector;
3376 irte.dest_id = IRTE_DEST(dest);
3377
3378 /*
3379 * atomically update the IRTE with the new destination and vector.
3380 */
3381 modify_irte(irq, &irte);
3382
3383 /*
3384 * After this point, all the interrupts will start arriving
3385 * at the new destination. So, time to cleanup the previous
3386 * vector allocation.
3387 */
22f65d31
MT
3388 if (cfg->move_in_progress)
3389 send_cleanup_vector(cfg);
54168ed7 3390}
3145e941 3391
54168ed7 3392#endif
3b7d1921 3393#endif /* CONFIG_SMP */
2d3fcc1c 3394
3b7d1921
EB
3395/*
3396 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3397 * which implement the MSI or MSI-X Capability Structure.
3398 */
3399static struct irq_chip msi_chip = {
3400 .name = "PCI-MSI",
3401 .unmask = unmask_msi_irq,
3402 .mask = mask_msi_irq,
1d025192 3403 .ack = ack_apic_edge,
3b7d1921
EB
3404#ifdef CONFIG_SMP
3405 .set_affinity = set_msi_irq_affinity,
3406#endif
3407 .retrigger = ioapic_retrigger_irq,
2d3fcc1c
EB
3408};
3409
54168ed7
IM
3410#ifdef CONFIG_INTR_REMAP
3411static struct irq_chip msi_ir_chip = {
3412 .name = "IR-PCI-MSI",
3413 .unmask = unmask_msi_irq,
3414 .mask = mask_msi_irq,
3415 .ack = ack_x2apic_edge,
3416#ifdef CONFIG_SMP
3417 .set_affinity = ir_set_msi_irq_affinity,
3418#endif
3419 .retrigger = ioapic_retrigger_irq,
3420};
3421
3422/*
3423 * Map the PCI dev to the corresponding remapping hardware unit
3424 * and allocate 'nvec' consecutive interrupt-remapping table entries
3425 * in it.
3426 */
3427static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3428{
3429 struct intel_iommu *iommu;
3430 int index;
3431
3432 iommu = map_dev_to_ir(dev);
3433 if (!iommu) {
3434 printk(KERN_ERR
3435 "Unable to map PCI %s to iommu\n", pci_name(dev));
3436 return -ENOENT;
3437 }
3438
3439 index = alloc_irte(iommu, irq, nvec);
3440 if (index < 0) {
3441 printk(KERN_ERR
3442 "Unable to allocate %d IRTE for PCI %s\n", nvec,
d6c88a50 3443 pci_name(dev));
54168ed7
IM
3444 return -ENOSPC;
3445 }
3446 return index;
3447}
3448#endif
1d025192 3449
3145e941 3450static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
1d025192
YL
3451{
3452 int ret;
3453 struct msi_msg msg;
3454
3455 ret = msi_compose_msg(dev, irq, &msg);
3456 if (ret < 0)
3457 return ret;
3458
3145e941 3459 set_irq_msi(irq, msidesc);
1d025192
YL
3460 write_msi_msg(irq, &msg);
3461
54168ed7
IM
3462#ifdef CONFIG_INTR_REMAP
3463 if (irq_remapped(irq)) {
3464 struct irq_desc *desc = irq_to_desc(irq);
3465 /*
3466 * irq migration in process context
3467 */
3468 desc->status |= IRQ_MOVE_PCNTXT;
3469 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3470 } else
3471#endif
3472 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
1d025192 3473
c81bba49
YL
3474 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3475
1d025192
YL
3476 return 0;
3477}
3478
047c8fdb
YL
3479int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3480{
54168ed7
IM
3481 unsigned int irq;
3482 int ret, sub_handle;
0b8f1efa 3483 struct msi_desc *msidesc;
54168ed7
IM
3484 unsigned int irq_want;
3485
3486#ifdef CONFIG_INTR_REMAP
3487 struct intel_iommu *iommu = 0;
3488 int index = 0;
3489#endif
3490
be5d5350 3491 irq_want = nr_irqs_gsi;
54168ed7 3492 sub_handle = 0;
0b8f1efa
YL
3493 list_for_each_entry(msidesc, &dev->msi_list, list) {
3494 irq = create_irq_nr(irq_want);
54168ed7
IM
3495 if (irq == 0)
3496 return -1;
f1ee5548 3497 irq_want = irq + 1;
54168ed7
IM
3498#ifdef CONFIG_INTR_REMAP
3499 if (!intr_remapping_enabled)
3500 goto no_ir;
3501
3502 if (!sub_handle) {
3503 /*
3504 * allocate the consecutive block of IRTE's
3505 * for 'nvec'
3506 */
3507 index = msi_alloc_irte(dev, irq, nvec);
3508 if (index < 0) {
3509 ret = index;
3510 goto error;
3511 }
3512 } else {
3513 iommu = map_dev_to_ir(dev);
3514 if (!iommu) {
3515 ret = -ENOENT;
3516 goto error;
3517 }
3518 /*
3519 * setup the mapping between the irq and the IRTE
3520 * base index, the sub_handle pointing to the
3521 * appropriate interrupt remap table entry.
3522 */
3523 set_irte_irq(irq, iommu, index, sub_handle);
3524 }
3525no_ir:
3526#endif
0b8f1efa 3527 ret = setup_msi_irq(dev, msidesc, irq);
54168ed7
IM
3528 if (ret < 0)
3529 goto error;
3530 sub_handle++;
3531 }
3532 return 0;
047c8fdb
YL
3533
3534error:
54168ed7
IM
3535 destroy_irq(irq);
3536 return ret;
047c8fdb
YL
3537}
3538
3b7d1921
EB
3539void arch_teardown_msi_irq(unsigned int irq)
3540{
f7feaca7 3541 destroy_irq(irq);
3b7d1921
EB
3542}
3543
9d783ba0 3544#if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
54168ed7 3545#ifdef CONFIG_SMP
22f65d31 3546static void dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
54168ed7 3547{
3145e941 3548 struct irq_desc *desc = irq_to_desc(irq);
54168ed7
IM
3549 struct irq_cfg *cfg;
3550 struct msi_msg msg;
3551 unsigned int dest;
54168ed7 3552
22f65d31
MT
3553 dest = set_desc_affinity(desc, mask);
3554 if (dest == BAD_APICID)
54168ed7
IM
3555 return;
3556
3145e941 3557 cfg = desc->chip_data;
54168ed7
IM
3558
3559 dmar_msi_read(irq, &msg);
3560
3561 msg.data &= ~MSI_DATA_VECTOR_MASK;
3562 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3563 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3564 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3565
3566 dmar_msi_write(irq, &msg);
54168ed7 3567}
3145e941 3568
54168ed7
IM
3569#endif /* CONFIG_SMP */
3570
3571struct irq_chip dmar_msi_type = {
3572 .name = "DMAR_MSI",
3573 .unmask = dmar_msi_unmask,
3574 .mask = dmar_msi_mask,
3575 .ack = ack_apic_edge,
3576#ifdef CONFIG_SMP
3577 .set_affinity = dmar_msi_set_affinity,
3578#endif
3579 .retrigger = ioapic_retrigger_irq,
3580};
3581
3582int arch_setup_dmar_msi(unsigned int irq)
3583{
3584 int ret;
3585 struct msi_msg msg;
2d3fcc1c 3586
54168ed7
IM
3587 ret = msi_compose_msg(NULL, irq, &msg);
3588 if (ret < 0)
3589 return ret;
3590 dmar_msi_write(irq, &msg);
3591 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3592 "edge");
3593 return 0;
3594}
3595#endif
3596
58ac1e76 3597#ifdef CONFIG_HPET_TIMER
3598
3599#ifdef CONFIG_SMP
22f65d31 3600static void hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
58ac1e76 3601{
3145e941 3602 struct irq_desc *desc = irq_to_desc(irq);
58ac1e76 3603 struct irq_cfg *cfg;
58ac1e76 3604 struct msi_msg msg;
3605 unsigned int dest;
58ac1e76 3606
22f65d31
MT
3607 dest = set_desc_affinity(desc, mask);
3608 if (dest == BAD_APICID)
58ac1e76 3609 return;
3610
3145e941 3611 cfg = desc->chip_data;
58ac1e76 3612
3613 hpet_msi_read(irq, &msg);
3614
3615 msg.data &= ~MSI_DATA_VECTOR_MASK;
3616 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3617 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3618 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3619
3620 hpet_msi_write(irq, &msg);
58ac1e76 3621}
3145e941 3622
58ac1e76 3623#endif /* CONFIG_SMP */
3624
3625struct irq_chip hpet_msi_type = {
3626 .name = "HPET_MSI",
3627 .unmask = hpet_msi_unmask,
3628 .mask = hpet_msi_mask,
3629 .ack = ack_apic_edge,
3630#ifdef CONFIG_SMP
3631 .set_affinity = hpet_msi_set_affinity,
3632#endif
3633 .retrigger = ioapic_retrigger_irq,
3634};
3635
3636int arch_setup_hpet_msi(unsigned int irq)
3637{
3638 int ret;
3639 struct msi_msg msg;
3640
3641 ret = msi_compose_msg(NULL, irq, &msg);
3642 if (ret < 0)
3643 return ret;
3644
3645 hpet_msi_write(irq, &msg);
3646 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3647 "edge");
c81bba49 3648
58ac1e76 3649 return 0;
3650}
3651#endif
3652
54168ed7 3653#endif /* CONFIG_PCI_MSI */
8b955b0d
EB
3654/*
3655 * Hypertransport interrupt support
3656 */
3657#ifdef CONFIG_HT_IRQ
3658
3659#ifdef CONFIG_SMP
3660
497c9a19 3661static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
8b955b0d 3662{
ec68307c
EB
3663 struct ht_irq_msg msg;
3664 fetch_ht_irq_msg(irq, &msg);
8b955b0d 3665
497c9a19 3666 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
ec68307c 3667 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
8b955b0d 3668
497c9a19 3669 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
ec68307c 3670 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
8b955b0d 3671
ec68307c 3672 write_ht_irq_msg(irq, &msg);
8b955b0d
EB
3673}
3674
22f65d31 3675static void set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
8b955b0d 3676{
3145e941 3677 struct irq_desc *desc = irq_to_desc(irq);
497c9a19 3678 struct irq_cfg *cfg;
8b955b0d 3679 unsigned int dest;
8b955b0d 3680
22f65d31
MT
3681 dest = set_desc_affinity(desc, mask);
3682 if (dest == BAD_APICID)
497c9a19 3683 return;
8b955b0d 3684
3145e941 3685 cfg = desc->chip_data;
8b955b0d 3686
497c9a19 3687 target_ht_irq(irq, dest, cfg->vector);
8b955b0d 3688}
3145e941 3689
8b955b0d
EB
3690#endif
3691
c37e108d 3692static struct irq_chip ht_irq_chip = {
8b955b0d
EB
3693 .name = "PCI-HT",
3694 .mask = mask_ht_irq,
3695 .unmask = unmask_ht_irq,
1d025192 3696 .ack = ack_apic_edge,
8b955b0d
EB
3697#ifdef CONFIG_SMP
3698 .set_affinity = set_ht_irq_affinity,
3699#endif
3700 .retrigger = ioapic_retrigger_irq,
3701};
3702
3703int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3704{
497c9a19
YL
3705 struct irq_cfg *cfg;
3706 int err;
8b955b0d 3707
f1182638
JB
3708 if (disable_apic)
3709 return -ENXIO;
3710
3145e941 3711 cfg = irq_cfg(irq);
fe402e1f 3712 err = assign_irq_vector(irq, cfg, apic->target_cpus());
54168ed7 3713 if (!err) {
ec68307c 3714 struct ht_irq_msg msg;
8b955b0d 3715 unsigned dest;
8b955b0d 3716
debccb3e
IM
3717 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3718 apic->target_cpus());
8b955b0d 3719
ec68307c 3720 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
8b955b0d 3721
ec68307c
EB
3722 msg.address_lo =
3723 HT_IRQ_LOW_BASE |
8b955b0d 3724 HT_IRQ_LOW_DEST_ID(dest) |
497c9a19 3725 HT_IRQ_LOW_VECTOR(cfg->vector) |
9b5bc8dc 3726 ((apic->irq_dest_mode == 0) ?
8b955b0d
EB
3727 HT_IRQ_LOW_DM_PHYSICAL :
3728 HT_IRQ_LOW_DM_LOGICAL) |
3729 HT_IRQ_LOW_RQEOI_EDGE |
9b5bc8dc 3730 ((apic->irq_delivery_mode != dest_LowestPrio) ?
8b955b0d
EB
3731 HT_IRQ_LOW_MT_FIXED :
3732 HT_IRQ_LOW_MT_ARBITRATED) |
3733 HT_IRQ_LOW_IRQ_MASKED;
3734
ec68307c 3735 write_ht_irq_msg(irq, &msg);
8b955b0d 3736
a460e745
IM
3737 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3738 handle_edge_irq, "edge");
c81bba49
YL
3739
3740 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
8b955b0d 3741 }
497c9a19 3742 return err;
8b955b0d
EB
3743}
3744#endif /* CONFIG_HT_IRQ */
3745
03b48632 3746#ifdef CONFIG_X86_UV
4173a0e7
DN
3747/*
3748 * Re-target the irq to the specified CPU and enable the specified MMR located
3749 * on the specified blade to allow the sending of MSIs to the specified CPU.
3750 */
3751int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3752 unsigned long mmr_offset)
3753{
22f65d31 3754 const struct cpumask *eligible_cpu = cpumask_of(cpu);
4173a0e7
DN
3755 struct irq_cfg *cfg;
3756 int mmr_pnode;
3757 unsigned long mmr_value;
3758 struct uv_IO_APIC_route_entry *entry;
3759 unsigned long flags;
3760 int err;
3761
3145e941
YL
3762 cfg = irq_cfg(irq);
3763
e7986739 3764 err = assign_irq_vector(irq, cfg, eligible_cpu);
4173a0e7
DN
3765 if (err != 0)
3766 return err;
3767
3768 spin_lock_irqsave(&vector_lock, flags);
3769 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3770 irq_name);
3771 spin_unlock_irqrestore(&vector_lock, flags);
3772
4173a0e7
DN
3773 mmr_value = 0;
3774 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3775 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3776
3777 entry->vector = cfg->vector;
9b5bc8dc
IM
3778 entry->delivery_mode = apic->irq_delivery_mode;
3779 entry->dest_mode = apic->irq_dest_mode;
4173a0e7
DN
3780 entry->polarity = 0;
3781 entry->trigger = 0;
3782 entry->mask = 0;
debccb3e 3783 entry->dest = apic->cpu_mask_to_apicid(eligible_cpu);
4173a0e7
DN
3784
3785 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3786 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3787
3788 return irq;
3789}
3790
3791/*
3792 * Disable the specified MMR located on the specified blade so that MSIs are
3793 * longer allowed to be sent.
3794 */
3795void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3796{
3797 unsigned long mmr_value;
3798 struct uv_IO_APIC_route_entry *entry;
3799 int mmr_pnode;
3800
3801 mmr_value = 0;
3802 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3803 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3804
3805 entry->mask = 1;
3806
3807 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3808 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3809}
3810#endif /* CONFIG_X86_64 */
3811
9d6a4d08
YL
3812int __init io_apic_get_redir_entries (int ioapic)
3813{
3814 union IO_APIC_reg_01 reg_01;
3815 unsigned long flags;
3816
3817 spin_lock_irqsave(&ioapic_lock, flags);
3818 reg_01.raw = io_apic_read(ioapic, 1);
3819 spin_unlock_irqrestore(&ioapic_lock, flags);
3820
3821 return reg_01.bits.entries;
3822}
3823
be5d5350 3824void __init probe_nr_irqs_gsi(void)
9d6a4d08 3825{
be5d5350
YL
3826 int nr = 0;
3827
cc6c5006
YL
3828 nr = acpi_probe_gsi();
3829 if (nr > nr_irqs_gsi) {
be5d5350 3830 nr_irqs_gsi = nr;
cc6c5006
YL
3831 } else {
3832 /* for acpi=off or acpi is not compiled in */
3833 int idx;
3834
3835 nr = 0;
3836 for (idx = 0; idx < nr_ioapics; idx++)
3837 nr += io_apic_get_redir_entries(idx) + 1;
3838
3839 if (nr > nr_irqs_gsi)
3840 nr_irqs_gsi = nr;
3841 }
3842
3843 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
9d6a4d08
YL
3844}
3845
4a046d17
YL
3846#ifdef CONFIG_SPARSE_IRQ
3847int __init arch_probe_nr_irqs(void)
3848{
3849 int nr;
3850
f1ee5548
YL
3851 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3852 nr_irqs = NR_VECTORS * nr_cpu_ids;
4a046d17 3853
f1ee5548
YL
3854 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3855#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3856 /*
3857 * for MSI and HT dyn irq
3858 */
3859 nr += nr_irqs_gsi * 16;
3860#endif
3861 if (nr < nr_irqs)
4a046d17
YL
3862 nr_irqs = nr;
3863
3864 return 0;
3865}
3866#endif
3867
1da177e4 3868/* --------------------------------------------------------------------------
54168ed7 3869 ACPI-based IOAPIC Configuration
1da177e4
LT
3870 -------------------------------------------------------------------------- */
3871
888ba6c6 3872#ifdef CONFIG_ACPI
1da177e4 3873
54168ed7 3874#ifdef CONFIG_X86_32
36062448 3875int __init io_apic_get_unique_id(int ioapic, int apic_id)
1da177e4
LT
3876{
3877 union IO_APIC_reg_00 reg_00;
3878 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3879 physid_mask_t tmp;
3880 unsigned long flags;
3881 int i = 0;
3882
3883 /*
36062448
PC
3884 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3885 * buses (one for LAPICs, one for IOAPICs), where predecessors only
1da177e4 3886 * supports up to 16 on one shared APIC bus.
36062448 3887 *
1da177e4
LT
3888 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3889 * advantage of new APIC bus architecture.
3890 */
3891
3892 if (physids_empty(apic_id_map))
d190cb87 3893 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
1da177e4
LT
3894
3895 spin_lock_irqsave(&ioapic_lock, flags);
3896 reg_00.raw = io_apic_read(ioapic, 0);
3897 spin_unlock_irqrestore(&ioapic_lock, flags);
3898
3899 if (apic_id >= get_physical_broadcast()) {
3900 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3901 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3902 apic_id = reg_00.bits.ID;
3903 }
3904
3905 /*
36062448 3906 * Every APIC in a system must have a unique ID or we get lots of nice
1da177e4
LT
3907 * 'stuck on smp_invalidate_needed IPI wait' messages.
3908 */
d1d7cae8 3909 if (apic->check_apicid_used(apic_id_map, apic_id)) {
1da177e4
LT
3910
3911 for (i = 0; i < get_physical_broadcast(); i++) {
d1d7cae8 3912 if (!apic->check_apicid_used(apic_id_map, i))
1da177e4
LT
3913 break;
3914 }
3915
3916 if (i == get_physical_broadcast())
3917 panic("Max apic_id exceeded!\n");
3918
3919 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3920 "trying %d\n", ioapic, apic_id, i);
3921
3922 apic_id = i;
36062448 3923 }
1da177e4 3924
8058714a 3925 tmp = apic->apicid_to_cpu_present(apic_id);
1da177e4
LT
3926 physids_or(apic_id_map, apic_id_map, tmp);
3927
3928 if (reg_00.bits.ID != apic_id) {
3929 reg_00.bits.ID = apic_id;
3930
3931 spin_lock_irqsave(&ioapic_lock, flags);
3932 io_apic_write(ioapic, 0, reg_00.raw);
3933 reg_00.raw = io_apic_read(ioapic, 0);
3934 spin_unlock_irqrestore(&ioapic_lock, flags);
3935
3936 /* Sanity check */
6070f9ec
AD
3937 if (reg_00.bits.ID != apic_id) {
3938 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3939 return -1;
3940 }
1da177e4
LT
3941 }
3942
3943 apic_printk(APIC_VERBOSE, KERN_INFO
3944 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3945
3946 return apic_id;
3947}
3948
36062448 3949int __init io_apic_get_version(int ioapic)
1da177e4
LT
3950{
3951 union IO_APIC_reg_01 reg_01;
3952 unsigned long flags;
3953
3954 spin_lock_irqsave(&ioapic_lock, flags);
3955 reg_01.raw = io_apic_read(ioapic, 1);
3956 spin_unlock_irqrestore(&ioapic_lock, flags);
3957
3958 return reg_01.bits.version;
3959}
54168ed7 3960#endif
1da177e4 3961
54168ed7 3962int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
1da177e4 3963{
0b8f1efa
YL
3964 struct irq_desc *desc;
3965 struct irq_cfg *cfg;
3966 int cpu = boot_cpu_id;
3967
1da177e4 3968 if (!IO_APIC_IRQ(irq)) {
54168ed7 3969 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
1da177e4
LT
3970 ioapic);
3971 return -EINVAL;
3972 }
3973
0b8f1efa
YL
3974 desc = irq_to_desc_alloc_cpu(irq, cpu);
3975 if (!desc) {
3976 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3977 return 0;
3978 }
3979
1da177e4
LT
3980 /*
3981 * IRQs < 16 are already in the irq_2_pin[] map
3982 */
99d093d1 3983 if (irq >= NR_IRQS_LEGACY) {
0b8f1efa 3984 cfg = desc->chip_data;
3145e941 3985 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
0b8f1efa 3986 }
1da177e4 3987
3145e941 3988 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
1da177e4
LT
3989
3990 return 0;
3991}
3992
54168ed7 3993
61fd47e0
SL
3994int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3995{
3996 int i;
3997
3998 if (skip_ioapic_setup)
3999 return -1;
4000
4001 for (i = 0; i < mp_irq_entries; i++)
c2c21745
JSR
4002 if (mp_irqs[i].irqtype == mp_INT &&
4003 mp_irqs[i].srcbusirq == bus_irq)
61fd47e0
SL
4004 break;
4005 if (i >= mp_irq_entries)
4006 return -1;
4007
4008 *trigger = irq_trigger(i);
4009 *polarity = irq_polarity(i);
4010 return 0;
4011}
4012
888ba6c6 4013#endif /* CONFIG_ACPI */
1a3f239d 4014
497c9a19
YL
4015/*
4016 * This function currently is only a helper for the i386 smp boot process where
4017 * we need to reprogram the ioredtbls to cater for the cpus which have come online
fe402e1f 4018 * so mask in all cases should simply be apic->target_cpus()
497c9a19
YL
4019 */
4020#ifdef CONFIG_SMP
4021void __init setup_ioapic_dest(void)
4022{
4023 int pin, ioapic, irq, irq_entry;
6c2e9403 4024 struct irq_desc *desc;
497c9a19 4025 struct irq_cfg *cfg;
22f65d31 4026 const struct cpumask *mask;
497c9a19
YL
4027
4028 if (skip_ioapic_setup == 1)
4029 return;
4030
4031 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
4032 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4033 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4034 if (irq_entry == -1)
4035 continue;
4036 irq = pin_2_irq(irq_entry, ioapic, pin);
4037
4038 /* setup_IO_APIC_irqs could fail to get vector for some device
4039 * when you have too many devices, because at that time only boot
4040 * cpu is online.
4041 */
0b8f1efa
YL
4042 desc = irq_to_desc(irq);
4043 cfg = desc->chip_data;
6c2e9403 4044 if (!cfg->vector) {
3145e941 4045 setup_IO_APIC_irq(ioapic, pin, irq, desc,
497c9a19
YL
4046 irq_trigger(irq_entry),
4047 irq_polarity(irq_entry));
6c2e9403
TG
4048 continue;
4049
4050 }
4051
4052 /*
4053 * Honour affinities which have been set in early boot
4054 */
6c2e9403
TG
4055 if (desc->status &
4056 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
7f7ace0c 4057 mask = desc->affinity;
6c2e9403 4058 else
fe402e1f 4059 mask = apic->target_cpus();
6c2e9403 4060
54168ed7 4061#ifdef CONFIG_INTR_REMAP
6c2e9403 4062 if (intr_remapping_enabled)
3145e941 4063 set_ir_ioapic_affinity_irq_desc(desc, mask);
54168ed7 4064 else
6c2e9403 4065#endif
3145e941 4066 set_ioapic_affinity_irq_desc(desc, mask);
497c9a19
YL
4067 }
4068
4069 }
4070}
4071#endif
4072
54168ed7
IM
4073#define IOAPIC_RESOURCE_NAME_SIZE 11
4074
4075static struct resource *ioapic_resources;
4076
4077static struct resource * __init ioapic_setup_resources(void)
4078{
4079 unsigned long n;
4080 struct resource *res;
4081 char *mem;
4082 int i;
4083
4084 if (nr_ioapics <= 0)
4085 return NULL;
4086
4087 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4088 n *= nr_ioapics;
4089
4090 mem = alloc_bootmem(n);
4091 res = (void *)mem;
4092
4093 if (mem != NULL) {
4094 mem += sizeof(struct resource) * nr_ioapics;
4095
4096 for (i = 0; i < nr_ioapics; i++) {
4097 res[i].name = mem;
4098 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4099 sprintf(mem, "IOAPIC %u", i);
4100 mem += IOAPIC_RESOURCE_NAME_SIZE;
4101 }
4102 }
4103
4104 ioapic_resources = res;
4105
4106 return res;
4107}
54168ed7 4108
f3294a33
YL
4109void __init ioapic_init_mappings(void)
4110{
4111 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
54168ed7 4112 struct resource *ioapic_res;
d6c88a50 4113 int i;
f3294a33 4114
54168ed7 4115 ioapic_res = ioapic_setup_resources();
f3294a33
YL
4116 for (i = 0; i < nr_ioapics; i++) {
4117 if (smp_found_config) {
b5ba7e6d 4118 ioapic_phys = mp_ioapics[i].apicaddr;
54168ed7 4119#ifdef CONFIG_X86_32
d6c88a50
TG
4120 if (!ioapic_phys) {
4121 printk(KERN_ERR
4122 "WARNING: bogus zero IO-APIC "
4123 "address found in MPTABLE, "
4124 "disabling IO/APIC support!\n");
4125 smp_found_config = 0;
4126 skip_ioapic_setup = 1;
4127 goto fake_ioapic_page;
4128 }
54168ed7 4129#endif
f3294a33 4130 } else {
54168ed7 4131#ifdef CONFIG_X86_32
f3294a33 4132fake_ioapic_page:
54168ed7 4133#endif
f3294a33 4134 ioapic_phys = (unsigned long)
54168ed7 4135 alloc_bootmem_pages(PAGE_SIZE);
f3294a33
YL
4136 ioapic_phys = __pa(ioapic_phys);
4137 }
4138 set_fixmap_nocache(idx, ioapic_phys);
54168ed7
IM
4139 apic_printk(APIC_VERBOSE,
4140 "mapped IOAPIC to %08lx (%08lx)\n",
4141 __fix_to_virt(idx), ioapic_phys);
f3294a33 4142 idx++;
54168ed7 4143
54168ed7
IM
4144 if (ioapic_res != NULL) {
4145 ioapic_res->start = ioapic_phys;
4146 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4147 ioapic_res++;
4148 }
f3294a33
YL
4149 }
4150}
4151
54168ed7
IM
4152static int __init ioapic_insert_resources(void)
4153{
4154 int i;
4155 struct resource *r = ioapic_resources;
4156
4157 if (!r) {
4158 printk(KERN_ERR
4159 "IO APIC resources could be not be allocated.\n");
4160 return -1;
4161 }
4162
4163 for (i = 0; i < nr_ioapics; i++) {
4164 insert_resource(&iomem_resource, r);
4165 r++;
4166 }
4167
4168 return 0;
4169}
4170
4171/* Insert the IO APIC resources after PCI initialization has occured to handle
4172 * IO APICS that are mapped in on a BAR in PCI space. */
4173late_initcall(ioapic_insert_resources);