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