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