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