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