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