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