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