blob: 7e92a9212fd762be15dad626a7491d432e2cadaf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070028#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020038#include <linux/jiffies.h> /* time_after() */
Yinghai Lud4057bd2008-08-19 20:50:38 -070039#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070044#include <linux/hpet.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070045
Yinghai Lud4057bd2008-08-19 20:50:38 -070046#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053049#include <asm/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070051#include <asm/proto.h>
52#include <asm/acpi.h>
53#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070055#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020056#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070057#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070058#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070059#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070060#include <asm/irq_remapping.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070061#include <asm/hpet.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +053062#include <asm/hw_irq.h>
Dean Nelson4173a0e2008-10-02 12:18:21 -050063#include <asm/uv/uv_hub.h>
64#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Ingo Molnar7b6aa332009-02-17 13:58:15 +010066#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010068#define __apicdebuginit(type) static type __init
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020071 * Is the SiS APIC rmw bug present ?
72 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74int sis_apic_bug = -1;
75
Yinghai Luefa25592008-08-19 20:50:36 -070076static DEFINE_SPINLOCK(ioapic_lock);
77static DEFINE_SPINLOCK(vector_lock);
78
Yinghai Luefa25592008-08-19 20:50:36 -070079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * # of IRQ routing registers
81 */
82int nr_ioapic_registers[MAX_IO_APICS];
83
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040084/* I/O APIC entries */
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +053085struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040086int nr_ioapics;
87
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040088/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +053089struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040090
91/* # of MP IRQ source entries */
92int mp_irq_entries;
93
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040094#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
95int mp_bus_id_to_type[MAX_MP_BUSSES];
96#endif
97
98DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
99
Yinghai Luefa25592008-08-19 20:50:36 -0700100int skip_ioapic_setup;
101
Ingo Molnar65a4e572009-01-31 03:36:17 +0100102void arch_disable_smp_support(void)
103{
104#ifdef CONFIG_PCI
105 noioapicquirk = 1;
106 noioapicreroute = -1;
107#endif
108 skip_ioapic_setup = 1;
109}
110
Ingo Molnar54168ed2008-08-20 09:07:45 +0200111static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700112{
113 /* disable IO-APIC */
Ingo Molnar65a4e572009-01-31 03:36:17 +0100114 arch_disable_smp_support();
Yinghai Luefa25592008-08-19 20:50:36 -0700115 return 0;
116}
117early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200118
Yinghai Lu0f978f42008-08-19 20:50:26 -0700119struct irq_pin_list {
120 int apic, pin;
121 struct irq_pin_list *next;
122};
Yinghai Lu301e6192008-08-19 20:50:02 -0700123
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700124static struct irq_pin_list *get_one_free_irq_2_pin(int node)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700125{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800126 struct irq_pin_list *pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700127
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800128 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700129
Yinghai Lu0f978f42008-08-19 20:50:26 -0700130 return pin;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeremy Fitzhardinge8e13d692009-06-08 03:14:59 -0700133/*
134 * This is performance-critical, we want to do it O(1)
135 *
136 * Most irqs are mapped 1:1 with pins.
137 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800138struct irq_cfg {
139 struct irq_pin_list *irq_2_pin;
Mike Travis22f65d32008-12-16 17:33:56 -0800140 cpumask_var_t domain;
141 cpumask_var_t old_domain;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800142 unsigned move_cleanup_count;
143 u8 vector;
144 u8 move_in_progress : 1;
145};
146
147/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
148#ifdef CONFIG_SPARSE_IRQ
149static struct irq_cfg irq_cfgx[] = {
150#else
151static struct irq_cfg irq_cfgx[NR_IRQS] = {
152#endif
Mike Travis22f65d32008-12-16 17:33:56 -0800153 [0] = { .vector = IRQ0_VECTOR, },
154 [1] = { .vector = IRQ1_VECTOR, },
155 [2] = { .vector = IRQ2_VECTOR, },
156 [3] = { .vector = IRQ3_VECTOR, },
157 [4] = { .vector = IRQ4_VECTOR, },
158 [5] = { .vector = IRQ5_VECTOR, },
159 [6] = { .vector = IRQ6_VECTOR, },
160 [7] = { .vector = IRQ7_VECTOR, },
161 [8] = { .vector = IRQ8_VECTOR, },
162 [9] = { .vector = IRQ9_VECTOR, },
163 [10] = { .vector = IRQ10_VECTOR, },
164 [11] = { .vector = IRQ11_VECTOR, },
165 [12] = { .vector = IRQ12_VECTOR, },
166 [13] = { .vector = IRQ13_VECTOR, },
167 [14] = { .vector = IRQ14_VECTOR, },
168 [15] = { .vector = IRQ15_VECTOR, },
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800169};
170
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800171int __init arch_early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800172{
173 struct irq_cfg *cfg;
174 struct irq_desc *desc;
175 int count;
Yinghai Ludad213ae2009-05-28 18:14:40 -0700176 int node;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800177 int i;
178
179 cfg = irq_cfgx;
180 count = ARRAY_SIZE(irq_cfgx);
Yinghai Ludad213ae2009-05-28 18:14:40 -0700181 node= cpu_to_node(boot_cpu_id);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800182
183 for (i = 0; i < count; i++) {
184 desc = irq_to_desc(i);
185 desc->chip_data = &cfg[i];
Yinghai Lu12274e92009-06-11 15:07:48 -0700186 zalloc_cpumask_var_node(&cfg[i].domain, GFP_NOWAIT, node);
187 zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_NOWAIT, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800188 if (i < NR_IRQS_LEGACY)
189 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800190 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800191
192 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800193}
194
195#ifdef CONFIG_SPARSE_IRQ
196static struct irq_cfg *irq_cfg(unsigned int irq)
197{
198 struct irq_cfg *cfg = NULL;
199 struct irq_desc *desc;
200
201 desc = irq_to_desc(irq);
202 if (desc)
203 cfg = desc->chip_data;
204
205 return cfg;
206}
207
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700208static struct irq_cfg *get_one_free_irq_cfg(int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800209{
210 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800211
212 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800213 if (cfg) {
Mike Travis80855f72008-12-31 18:08:47 -0800214 if (!alloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800215 kfree(cfg);
216 cfg = NULL;
Mike Travis80855f72008-12-31 18:08:47 -0800217 } else if (!alloc_cpumask_var_node(&cfg->old_domain,
218 GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800219 free_cpumask_var(cfg->domain);
220 kfree(cfg);
221 cfg = NULL;
222 } else {
223 cpumask_clear(cfg->domain);
224 cpumask_clear(cfg->old_domain);
225 }
226 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800227
228 return cfg;
229}
230
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700231int arch_init_chip_data(struct irq_desc *desc, int node)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800232{
233 struct irq_cfg *cfg;
234
235 cfg = desc->chip_data;
236 if (!cfg) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700237 desc->chip_data = get_one_free_irq_cfg(node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800238 if (!desc->chip_data) {
239 printk(KERN_ERR "can not alloc irq_cfg\n");
240 BUG_ON(1);
241 }
242 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800243
244 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800245}
246
Yinghai Lufcef5912009-04-27 17:58:23 -0700247/* for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800248static void
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700249init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int node)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800250{
251 struct irq_pin_list *old_entry, *head, *tail, *entry;
252
253 cfg->irq_2_pin = NULL;
254 old_entry = old_cfg->irq_2_pin;
255 if (!old_entry)
256 return;
257
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700258 entry = get_one_free_irq_2_pin(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800259 if (!entry)
260 return;
261
262 entry->apic = old_entry->apic;
263 entry->pin = old_entry->pin;
264 head = entry;
265 tail = entry;
266 old_entry = old_entry->next;
267 while (old_entry) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700268 entry = get_one_free_irq_2_pin(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800269 if (!entry) {
270 entry = head;
271 while (entry) {
272 head = entry->next;
273 kfree(entry);
274 entry = head;
275 }
276 /* still use the old one */
277 return;
278 }
279 entry->apic = old_entry->apic;
280 entry->pin = old_entry->pin;
281 tail->next = entry;
282 tail = entry;
283 old_entry = old_entry->next;
284 }
285
286 tail->next = NULL;
287 cfg->irq_2_pin = head;
288}
289
290static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
291{
292 struct irq_pin_list *entry, *next;
293
294 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
295 return;
296
297 entry = old_cfg->irq_2_pin;
298
299 while (entry) {
300 next = entry->next;
301 kfree(entry);
302 entry = next;
303 }
304 old_cfg->irq_2_pin = NULL;
305}
306
307void arch_init_copy_chip_data(struct irq_desc *old_desc,
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700308 struct irq_desc *desc, int node)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800309{
310 struct irq_cfg *cfg;
311 struct irq_cfg *old_cfg;
312
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700313 cfg = get_one_free_irq_cfg(node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800314
315 if (!cfg)
316 return;
317
318 desc->chip_data = cfg;
319
320 old_cfg = old_desc->chip_data;
321
322 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
323
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700324 init_copy_irq_2_pin(old_cfg, cfg, node);
Yinghai Lu48a1b102008-12-11 00:15:01 -0800325}
326
327static void free_irq_cfg(struct irq_cfg *old_cfg)
328{
329 kfree(old_cfg);
330}
331
332void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
333{
334 struct irq_cfg *old_cfg, *cfg;
335
336 old_cfg = old_desc->chip_data;
337 cfg = desc->chip_data;
338
339 if (old_cfg == cfg)
340 return;
341
342 if (old_cfg) {
343 free_irq_2_pin(old_cfg, cfg);
344 free_irq_cfg(old_cfg);
345 old_desc->chip_data = NULL;
346 }
347}
Yinghai Lufcef5912009-04-27 17:58:23 -0700348/* end for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800349
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800350#else
351static struct irq_cfg *irq_cfg(unsigned int irq)
352{
353 return irq < nr_irqs ? irq_cfgx + irq : NULL;
354}
355
356#endif
357
Linus Torvalds130fe052006-11-01 09:11:00 -0800358struct io_apic {
359 unsigned int index;
360 unsigned int unused[3];
361 unsigned int data;
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700362 unsigned int unused2[11];
363 unsigned int eoi;
Linus Torvalds130fe052006-11-01 09:11:00 -0800364};
365
366static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
367{
368 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +0530369 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800370}
371
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700372static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
373{
374 struct io_apic __iomem *io_apic = io_apic_base(apic);
375 writel(vector, &io_apic->eoi);
376}
377
Linus Torvalds130fe052006-11-01 09:11:00 -0800378static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
379{
380 struct io_apic __iomem *io_apic = io_apic_base(apic);
381 writel(reg, &io_apic->index);
382 return readl(&io_apic->data);
383}
384
385static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
386{
387 struct io_apic __iomem *io_apic = io_apic_base(apic);
388 writel(reg, &io_apic->index);
389 writel(value, &io_apic->data);
390}
391
392/*
393 * Re-write a value: to be used for read-modify-write
394 * cycles where the read already set up the index register.
395 *
396 * Older SiS APIC requires we rewrite the index register
397 */
398static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
399{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200400 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200401
402 if (sis_apic_bug)
403 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800404 writel(value, &io_apic->data);
405}
406
Yinghai Lu3145e942008-12-05 18:58:34 -0800407static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700408{
409 struct irq_pin_list *entry;
410 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700411
412 spin_lock_irqsave(&ioapic_lock, flags);
Jeremy Fitzhardinged8c52062009-06-08 03:17:58 -0700413 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700414 unsigned int reg;
415 int pin;
416
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700417 pin = entry->pin;
418 reg = io_apic_read(entry->apic, 0x10 + pin*2);
419 /* Is the remote IRR bit set? */
420 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
421 spin_unlock_irqrestore(&ioapic_lock, flags);
422 return true;
423 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700424 }
425 spin_unlock_irqrestore(&ioapic_lock, flags);
426
427 return false;
428}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700429
Andi Kleencf4c6a22006-09-26 10:52:30 +0200430union entry_union {
431 struct { u32 w1, w2; };
432 struct IO_APIC_route_entry entry;
433};
434
435static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
436{
437 union entry_union eu;
438 unsigned long flags;
439 spin_lock_irqsave(&ioapic_lock, flags);
440 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
441 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
442 spin_unlock_irqrestore(&ioapic_lock, flags);
443 return eu.entry;
444}
445
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800446/*
447 * When we write a new IO APIC routing entry, we need to write the high
448 * word first! If the mask bit in the low word is clear, we will enable
449 * the interrupt, and we need to make sure the entry is fully populated
450 * before that happens.
451 */
Andi Kleend15512f2006-12-07 02:14:07 +0100452static void
453__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
454{
Figo.zhang50a8d4d2009-06-17 22:25:20 +0800455 union entry_union eu = {{0, 0}};
456
Andi Kleend15512f2006-12-07 02:14:07 +0100457 eu.entry = e;
458 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
459 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
460}
461
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -0800462void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleencf4c6a22006-09-26 10:52:30 +0200463{
464 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200465 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100466 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800467 spin_unlock_irqrestore(&ioapic_lock, flags);
468}
469
470/*
471 * When we mask an IO APIC routing entry, we need to write the low
472 * word first, in order to set the mask bit before we change the
473 * high bits!
474 */
475static void ioapic_mask_entry(int apic, int pin)
476{
477 unsigned long flags;
478 union entry_union eu = { .entry.mask = 1 };
479
480 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200481 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
482 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
483 spin_unlock_irqrestore(&ioapic_lock, flags);
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
487 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
488 * shared ISA-space IRQs, so we have to support them. We are super
489 * fast in the common case, and fast for shared ISA-space IRQs.
490 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700491static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700493 struct irq_pin_list **entryp, *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700495 for (entryp = &cfg->irq_2_pin;
496 *entryp != NULL;
497 entryp = &(*entryp)->next) {
498 entry = *entryp;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700499 /* not again, please */
500 if (entry->apic == apic && entry->pin == pin)
501 return;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700502 }
503
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700504 entry = get_one_free_irq_2_pin(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 entry->apic = apic;
506 entry->pin = pin;
Jeremy Fitzhardinge875e68e2009-06-08 03:24:11 -0700507
508 *entryp = entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511/*
512 * Reroute an IRQ to a different pin.
513 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -0700514static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700515 int oldapic, int oldpin,
516 int newapic, int newpin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Jeremy Fitzhardinge535b6422009-06-08 03:29:26 -0700518 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Jeremy Fitzhardinge535b6422009-06-08 03:29:26 -0700520 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (entry->apic == oldapic && entry->pin == oldpin) {
522 entry->apic = newapic;
523 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700524 /* every one is different, right? */
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700525 return;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700528
Jeremy Fitzhardinge4eea6ff2009-06-08 03:32:15 -0700529 /* old apic/pin didn't exist, so just add new ones */
530 add_pin_to_irq_node(cfg, node, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Jeremy Fitzhardinge2f210de2009-06-08 02:55:22 -0700533static void io_apic_modify_irq(struct irq_cfg *cfg,
534 int mask_and, int mask_or,
535 void (*final)(struct irq_pin_list *entry))
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400536{
537 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400538 struct irq_pin_list *entry;
539
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400540 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
541 unsigned int reg;
542 pin = entry->pin;
543 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
544 reg &= mask_and;
545 reg |= mask_or;
546 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
547 if (final)
548 final(entry);
549 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700550}
551
Yinghai Lu3145e942008-12-05 18:58:34 -0800552static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400553{
Yinghai Lu3145e942008-12-05 18:58:34 -0800554 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400555}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700556
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530557static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700558{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400559 /*
560 * Synchronize the IO-APIC and the CPU by doing
561 * a dummy read from the IO-APIC
562 */
563 struct io_apic __iomem *io_apic;
564 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700565 readl(&io_apic->data);
566}
567
Yinghai Lu3145e942008-12-05 18:58:34 -0800568static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400569{
Yinghai Lu3145e942008-12-05 18:58:34 -0800570 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400571}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700572
Yinghai Lu3145e942008-12-05 18:58:34 -0800573static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400574{
Yinghai Lu3145e942008-12-05 18:58:34 -0800575 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400576 IO_APIC_REDIR_MASKED, NULL);
577}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700578
Yinghai Lu3145e942008-12-05 18:58:34 -0800579static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400580{
Yinghai Lu3145e942008-12-05 18:58:34 -0800581 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400582 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
583}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700584
Yinghai Lu3145e942008-12-05 18:58:34 -0800585static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Yinghai Lu3145e942008-12-05 18:58:34 -0800587 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 unsigned long flags;
589
Yinghai Lu3145e942008-12-05 18:58:34 -0800590 BUG_ON(!cfg);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800593 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 spin_unlock_irqrestore(&ioapic_lock, flags);
595}
596
Yinghai Lu3145e942008-12-05 18:58:34 -0800597static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Yinghai Lu3145e942008-12-05 18:58:34 -0800599 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 unsigned long flags;
601
602 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800603 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 spin_unlock_irqrestore(&ioapic_lock, flags);
605}
606
Yinghai Lu3145e942008-12-05 18:58:34 -0800607static void mask_IO_APIC_irq(unsigned int irq)
608{
609 struct irq_desc *desc = irq_to_desc(irq);
610
611 mask_IO_APIC_irq_desc(desc);
612}
613static void unmask_IO_APIC_irq(unsigned int irq)
614{
615 struct irq_desc *desc = irq_to_desc(irq);
616
617 unmask_IO_APIC_irq_desc(desc);
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
621{
622 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200625 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 if (entry.delivery_mode == dest_SMI)
627 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 /*
629 * Disable it in the IO-APIC irq-routing table:
630 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800631 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
Ingo Molnar54168ed2008-08-20 09:07:45 +0200634static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 int apic, pin;
637
638 for (apic = 0; apic < nr_ioapics; apic++)
639 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
640 clear_IO_APIC_pin(apic, pin);
641}
642
Ingo Molnar54168ed2008-08-20 09:07:45 +0200643#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/*
645 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
646 * specific CPU-side IRQs.
647 */
648
649#define MAX_PIRQS 8
Yinghai Lu3bd25d02009-02-15 02:54:03 -0800650static int pirq_entries[MAX_PIRQS] = {
651 [0 ... MAX_PIRQS - 1] = -1
652};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static int __init ioapic_pirq_setup(char *str)
655{
656 int i, max;
657 int ints[MAX_PIRQS+1];
658
659 get_options(str, ARRAY_SIZE(ints), ints);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 apic_printk(APIC_VERBOSE, KERN_INFO
662 "PIRQ redirection, working around broken MP-BIOS.\n");
663 max = MAX_PIRQS;
664 if (ints[0] < MAX_PIRQS)
665 max = ints[0];
666
667 for (i = 0; i < max; i++) {
668 apic_printk(APIC_VERBOSE, KERN_DEBUG
669 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
670 /*
671 * PIRQs are mapped upside down, usually.
672 */
673 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
674 }
675 return 1;
676}
677
678__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200679#endif /* CONFIG_X86_32 */
680
Fenghua Yub24696b2009-03-27 14:22:44 -0700681struct IO_APIC_route_entry **alloc_ioapic_entries(void)
682{
683 int apic;
684 struct IO_APIC_route_entry **ioapic_entries;
685
686 ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
687 GFP_ATOMIC);
688 if (!ioapic_entries)
689 return 0;
690
691 for (apic = 0; apic < nr_ioapics; apic++) {
692 ioapic_entries[apic] =
693 kzalloc(sizeof(struct IO_APIC_route_entry) *
694 nr_ioapic_registers[apic], GFP_ATOMIC);
695 if (!ioapic_entries[apic])
696 goto nomem;
697 }
698
699 return ioapic_entries;
700
701nomem:
702 while (--apic >= 0)
703 kfree(ioapic_entries[apic]);
704 kfree(ioapic_entries);
705
706 return 0;
707}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200708
709/*
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700710 * Saves all the IO-APIC RTE's
Ingo Molnar54168ed2008-08-20 09:07:45 +0200711 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700712int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200713{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200714 int apic, pin;
715
Fenghua Yub24696b2009-03-27 14:22:44 -0700716 if (!ioapic_entries)
717 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200718
719 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700720 if (!ioapic_entries[apic])
721 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200722
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700723 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
Fenghua Yub24696b2009-03-27 14:22:44 -0700724 ioapic_entries[apic][pin] =
Ingo Molnar54168ed2008-08-20 09:07:45 +0200725 ioapic_read_entry(apic, pin);
Fenghua Yub24696b2009-03-27 14:22:44 -0700726 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400727
Ingo Molnar54168ed2008-08-20 09:07:45 +0200728 return 0;
729}
730
Fenghua Yub24696b2009-03-27 14:22:44 -0700731/*
732 * Mask all IO APIC entries.
733 */
734void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700735{
736 int apic, pin;
737
Fenghua Yub24696b2009-03-27 14:22:44 -0700738 if (!ioapic_entries)
739 return;
740
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700741 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700742 if (!ioapic_entries[apic])
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700743 break;
Fenghua Yub24696b2009-03-27 14:22:44 -0700744
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700745 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
746 struct IO_APIC_route_entry entry;
747
Fenghua Yub24696b2009-03-27 14:22:44 -0700748 entry = ioapic_entries[apic][pin];
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700749 if (!entry.mask) {
750 entry.mask = 1;
751 ioapic_write_entry(apic, pin, entry);
752 }
753 }
754 }
755}
756
Fenghua Yub24696b2009-03-27 14:22:44 -0700757/*
758 * Restore IO APIC entries which was saved in ioapic_entries.
759 */
760int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200761{
762 int apic, pin;
763
Fenghua Yub24696b2009-03-27 14:22:44 -0700764 if (!ioapic_entries)
765 return -ENOMEM;
766
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400767 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700768 if (!ioapic_entries[apic])
769 return -ENOMEM;
770
Ingo Molnar54168ed2008-08-20 09:07:45 +0200771 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
772 ioapic_write_entry(apic, pin,
Fenghua Yub24696b2009-03-27 14:22:44 -0700773 ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400774 }
Fenghua Yub24696b2009-03-27 14:22:44 -0700775 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200776}
777
Fenghua Yub24696b2009-03-27 14:22:44 -0700778void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
779{
780 int apic;
781
782 for (apic = 0; apic < nr_ioapics; apic++)
783 kfree(ioapic_entries[apic]);
784
785 kfree(ioapic_entries);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200786}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788/*
789 * Find the IRQ entry number of a certain pin.
790 */
791static int find_irq_entry(int apic, int pin, int type)
792{
793 int i;
794
795 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530796 if (mp_irqs[i].irqtype == type &&
797 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
798 mp_irqs[i].dstapic == MP_APIC_ALL) &&
799 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return i;
801
802 return -1;
803}
804
805/*
806 * Find the pin to which IRQ[irq] (ISA) is connected
807 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800808static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
810 int i;
811
812 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530813 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300815 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530816 (mp_irqs[i].irqtype == type) &&
817 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530819 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 }
821 return -1;
822}
823
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800824static int __init find_isa_irq_apic(int irq, int type)
825{
826 int i;
827
828 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530829 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800830
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300831 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530832 (mp_irqs[i].irqtype == type) &&
833 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800834 break;
835 }
836 if (i < mp_irq_entries) {
837 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200838 for(apic = 0; apic < nr_ioapics; apic++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530839 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800840 return apic;
841 }
842 }
843
844 return -1;
845}
846
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300847#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848/*
849 * EISA Edge/Level control register, ELCR
850 */
851static int EISA_ELCR(unsigned int irq)
852{
Yinghai Lu99d093d2008-12-05 18:58:32 -0800853 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 unsigned int port = 0x4d0 + (irq >> 3);
855 return (inb(port) >> (irq & 7)) & 1;
856 }
857 apic_printk(APIC_VERBOSE, KERN_INFO
858 "Broken MPtable reports ISA irq %d\n", irq);
859 return 0;
860}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200861
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300862#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300864/* ISA interrupts are always polarity zero edge triggered,
865 * when listed as conforming in the MP table. */
866
867#define default_ISA_trigger(idx) (0)
868#define default_ISA_polarity(idx) (0)
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/* EISA interrupts are always polarity zero and can be edge or level
871 * trigger depending on the ELCR value. If an interrupt is listed as
872 * EISA conforming in the MP table, that means its trigger type must
873 * be read in from the ELCR */
874
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530875#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300876#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878/* PCI interrupts are always polarity one level triggered,
879 * when listed as conforming in the MP table. */
880
881#define default_PCI_trigger(idx) (1)
882#define default_PCI_polarity(idx) (1)
883
884/* MCA interrupts are always polarity zero level triggered,
885 * when listed as conforming in the MP table. */
886
887#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300888#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Shaohua Li61fd47e2007-11-17 01:05:28 -0500890static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530892 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 int polarity;
894
895 /*
896 * Determine IRQ line polarity (high active or low active):
897 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530898 switch (mp_irqs[idx].irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200900 case 0: /* conforms, ie. bus-type dependent polarity */
901 if (test_bit(bus, mp_bus_not_pci))
902 polarity = default_ISA_polarity(idx);
903 else
904 polarity = default_PCI_polarity(idx);
905 break;
906 case 1: /* high active */
907 {
908 polarity = 0;
909 break;
910 }
911 case 2: /* reserved */
912 {
913 printk(KERN_WARNING "broken BIOS!!\n");
914 polarity = 1;
915 break;
916 }
917 case 3: /* low active */
918 {
919 polarity = 1;
920 break;
921 }
922 default: /* invalid */
923 {
924 printk(KERN_WARNING "broken BIOS!!\n");
925 polarity = 1;
926 break;
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929 return polarity;
930}
931
932static int MPBIOS_trigger(int idx)
933{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530934 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 int trigger;
936
937 /*
938 * Determine IRQ trigger mode (edge or level sensitive):
939 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530940 switch ((mp_irqs[idx].irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200942 case 0: /* conforms, ie. bus-type dependent */
943 if (test_bit(bus, mp_bus_not_pci))
944 trigger = default_ISA_trigger(idx);
945 else
946 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300947#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200948 switch (mp_bus_id_to_type[bus]) {
949 case MP_BUS_ISA: /* ISA pin */
950 {
951 /* set before the switch */
952 break;
953 }
954 case MP_BUS_EISA: /* EISA pin */
955 {
956 trigger = default_EISA_trigger(idx);
957 break;
958 }
959 case MP_BUS_PCI: /* PCI pin */
960 {
961 /* set before the switch */
962 break;
963 }
964 case MP_BUS_MCA: /* MCA pin */
965 {
966 trigger = default_MCA_trigger(idx);
967 break;
968 }
969 default:
970 {
971 printk(KERN_WARNING "broken BIOS!!\n");
972 trigger = 1;
973 break;
974 }
975 }
976#endif
977 break;
978 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200979 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200980 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200981 break;
982 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200983 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200984 {
985 printk(KERN_WARNING "broken BIOS!!\n");
986 trigger = 1;
987 break;
988 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200989 case 3: /* level */
990 {
991 trigger = 1;
992 break;
993 }
994 default: /* invalid */
995 {
996 printk(KERN_WARNING "broken BIOS!!\n");
997 trigger = 0;
998 break;
999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 return trigger;
1002}
1003
1004static inline int irq_polarity(int idx)
1005{
1006 return MPBIOS_polarity(idx);
1007}
1008
1009static inline int irq_trigger(int idx)
1010{
1011 return MPBIOS_trigger(idx);
1012}
1013
Yinghai Luefa25592008-08-19 20:50:36 -07001014int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015static int pin_2_irq(int idx, int apic, int pin)
1016{
1017 int irq, i;
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301018 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /*
1021 * Debugging check, we are in big trouble if this message pops up!
1022 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301023 if (mp_irqs[idx].dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1025
Ingo Molnar54168ed2008-08-20 09:07:45 +02001026 if (test_bit(bus, mp_bus_not_pci)) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301027 irq = mp_irqs[idx].srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001028 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001029 /*
1030 * PCI IRQs are mapped in order
1031 */
1032 i = irq = 0;
1033 while (i < apic)
1034 irq += nr_ioapic_registers[i++];
1035 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001036 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001037 * For MPS mode, so far only needed by ES7000 platform
1038 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001039 if (ioapic_renumber_irq)
1040 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
1042
Ingo Molnar54168ed2008-08-20 09:07:45 +02001043#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1046 */
1047 if ((pin >= 16) && (pin <= 23)) {
1048 if (pirq_entries[pin-16] != -1) {
1049 if (!pirq_entries[pin-16]) {
1050 apic_printk(APIC_VERBOSE, KERN_DEBUG
1051 "disabling PIRQ%d\n", pin-16);
1052 } else {
1053 irq = pirq_entries[pin-16];
1054 apic_printk(APIC_VERBOSE, KERN_DEBUG
1055 "using PIRQ%d -> IRQ %d\n",
1056 pin-16, irq);
1057 }
1058 }
1059 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001060#endif
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return irq;
1063}
1064
Yinghai Lue20c06f2009-05-06 10:08:22 -07001065/*
1066 * Find a specific PCI IRQ entry.
1067 * Not an __init, possibly needed by modules
1068 */
1069int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
Yinghai Lue5198072009-05-15 13:05:16 -07001070 struct io_apic_irq_attr *irq_attr)
Yinghai Lue20c06f2009-05-06 10:08:22 -07001071{
1072 int apic, i, best_guess = -1;
1073
1074 apic_printk(APIC_DEBUG,
1075 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1076 bus, slot, pin);
1077 if (test_bit(bus, mp_bus_not_pci)) {
1078 apic_printk(APIC_VERBOSE,
1079 "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1080 return -1;
1081 }
1082 for (i = 0; i < mp_irq_entries; i++) {
1083 int lbus = mp_irqs[i].srcbus;
1084
1085 for (apic = 0; apic < nr_ioapics; apic++)
1086 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1087 mp_irqs[i].dstapic == MP_APIC_ALL)
1088 break;
1089
1090 if (!test_bit(lbus, mp_bus_not_pci) &&
1091 !mp_irqs[i].irqtype &&
1092 (bus == lbus) &&
1093 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1094 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
1095
1096 if (!(apic || IO_APIC_IRQ(irq)))
1097 continue;
1098
1099 if (pin == (mp_irqs[i].srcbusirq & 3)) {
Yinghai Lue5198072009-05-15 13:05:16 -07001100 set_io_apic_irq_attr(irq_attr, apic,
1101 mp_irqs[i].dstirq,
1102 irq_trigger(i),
1103 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001104 return irq;
1105 }
1106 /*
1107 * Use the first all-but-pin matching entry as a
1108 * best-guess fuzzy result for broken mptables.
1109 */
1110 if (best_guess < 0) {
Yinghai Lue5198072009-05-15 13:05:16 -07001111 set_io_apic_irq_attr(irq_attr, apic,
1112 mp_irqs[i].dstirq,
1113 irq_trigger(i),
1114 irq_polarity(i));
Yinghai Lue20c06f2009-05-06 10:08:22 -07001115 best_guess = irq;
1116 }
1117 }
1118 }
1119 return best_guess;
1120}
1121EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1122
Yinghai Lu497c9a12008-08-19 20:50:28 -07001123void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001125 /* Used to the online set of cpus does not change
1126 * during assign_irq_vector.
1127 */
1128 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129}
1130
Yinghai Lu497c9a12008-08-19 20:50:28 -07001131void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001132{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001133 spin_unlock(&vector_lock);
1134}
1135
Mike Travise7986732008-12-16 17:33:52 -08001136static int
1137__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001138{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001139 /*
1140 * NOTE! The local APIC isn't very good at handling
1141 * multiple interrupts at the same interrupt level.
1142 * As the interrupt level is determined by taking the
1143 * vector number and shifting that right by 4, we
1144 * want to spread these out a bit so that they don't
1145 * all fall in the same interrupt level.
1146 *
1147 * Also, we've got to be careful not to trash gate
1148 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1149 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001150 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1151 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001152 int cpu, err;
1153 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001154
Ingo Molnar54168ed2008-08-20 09:07:45 +02001155 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1156 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001157
Mike Travis22f65d32008-12-16 17:33:56 -08001158 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1159 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001160
Ingo Molnar54168ed2008-08-20 09:07:45 +02001161 old_vector = cfg->vector;
1162 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001163 cpumask_and(tmp_mask, mask, cpu_online_mask);
1164 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1165 if (!cpumask_empty(tmp_mask)) {
1166 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001167 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001168 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001169 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001170
Mike Travise7986732008-12-16 17:33:52 -08001171 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001172 err = -ENOSPC;
1173 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001174 int new_cpu;
1175 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001176
Ingo Molnare2d40b12009-01-28 06:50:47 +01001177 apic->vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001178
Ingo Molnar54168ed2008-08-20 09:07:45 +02001179 vector = current_vector;
1180 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001181next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001182 vector += 8;
1183 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001184 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001185 offset = (offset + 1) % 8;
1186 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001187 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001188 if (unlikely(current_vector == vector))
1189 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001190
1191 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001192 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001193
Mike Travis22f65d32008-12-16 17:33:56 -08001194 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001195 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1196 goto next;
1197 /* Found one! */
1198 current_vector = vector;
1199 current_offset = offset;
1200 if (old_vector) {
1201 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001202 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001203 }
Mike Travis22f65d32008-12-16 17:33:56 -08001204 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001205 per_cpu(vector_irq, new_cpu)[vector] = irq;
1206 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001207 cpumask_copy(cfg->domain, tmp_mask);
1208 err = 0;
1209 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001210 }
Mike Travis22f65d32008-12-16 17:33:56 -08001211 free_cpumask_var(tmp_mask);
1212 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001213}
1214
Mike Travise7986732008-12-16 17:33:52 -08001215static int
1216assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001217{
1218 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001219 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001220
1221 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001222 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001223 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001224 return err;
1225}
1226
Yinghai Lu3145e942008-12-05 18:58:34 -08001227static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001228{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001229 int cpu, vector;
1230
Yinghai Lu497c9a12008-08-19 20:50:28 -07001231 BUG_ON(!cfg->vector);
1232
1233 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001234 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001235 per_cpu(vector_irq, cpu)[vector] = -1;
1236
1237 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001238 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001239
1240 if (likely(!cfg->move_in_progress))
1241 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001242 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001243 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1244 vector++) {
1245 if (per_cpu(vector_irq, cpu)[vector] != irq)
1246 continue;
1247 per_cpu(vector_irq, cpu)[vector] = -1;
1248 break;
1249 }
1250 }
1251 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001252}
1253
1254void __setup_vector_irq(int cpu)
1255{
1256 /* Initialize vector_irq on a new cpu */
1257 /* This function must be called with vector_lock held */
1258 int irq, vector;
1259 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001260 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001261
1262 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001263 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001264 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001265 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001266 continue;
1267 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001268 per_cpu(vector_irq, cpu)[vector] = irq;
1269 }
1270 /* Mark the free vectors */
1271 for (vector = 0; vector < NR_VECTORS; ++vector) {
1272 irq = per_cpu(vector_irq, cpu)[vector];
1273 if (irq < 0)
1274 continue;
1275
1276 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001277 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001278 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001279 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001280}
Glauber Costa3fde6902008-05-28 20:34:19 -07001281
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001282static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001283static struct irq_chip ir_ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Ingo Molnar54168ed2008-08-20 09:07:45 +02001285#define IOAPIC_AUTO -1
1286#define IOAPIC_EDGE 0
1287#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001289#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001290static inline int IO_APIC_irq_trigger(int irq)
1291{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001292 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001293
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001294 for (apic = 0; apic < nr_ioapics; apic++) {
1295 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1296 idx = find_irq_entry(apic, pin, mp_INT);
1297 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1298 return irq_trigger(idx);
1299 }
1300 }
1301 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001302 * nonexistent IRQs are edge default
1303 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001304 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001305}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001306#else
1307static inline int IO_APIC_irq_trigger(int irq)
1308{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001309 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001310}
1311#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001312
Yinghai Lu3145e942008-12-05 18:58:34 -08001313static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314{
Yinghai Lu199751d2008-08-19 20:50:27 -07001315
Jan Beulich6ebcc002006-06-26 13:56:46 +02001316 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001317 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001318 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001319 else
1320 desc->status &= ~IRQ_LEVEL;
1321
Ingo Molnar54168ed2008-08-20 09:07:45 +02001322 if (irq_remapped(irq)) {
1323 desc->status |= IRQ_MOVE_PCNTXT;
1324 if (trigger)
1325 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1326 handle_fasteoi_irq,
1327 "fasteoi");
1328 else
1329 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1330 handle_edge_irq, "edge");
1331 return;
1332 }
Suresh Siddha29b61be2009-03-16 17:05:02 -07001333
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001334 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1335 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001336 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001337 handle_fasteoi_irq,
1338 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001339 else
Ingo Molnara460e742006-10-17 00:10:03 -07001340 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001341 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001342}
1343
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -08001344int setup_ioapic_entry(int apic_id, int irq,
1345 struct IO_APIC_route_entry *entry,
1346 unsigned int destination, int trigger,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001347 int polarity, int vector, int pin)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001348{
1349 /*
1350 * add it to the IO-APIC irq-routing table:
1351 */
1352 memset(entry,0,sizeof(*entry));
1353
Ingo Molnar54168ed2008-08-20 09:07:45 +02001354 if (intr_remapping_enabled) {
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001355 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001356 struct irte irte;
1357 struct IR_IO_APIC_route_entry *ir_entry =
1358 (struct IR_IO_APIC_route_entry *) entry;
1359 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001360
Ingo Molnar54168ed2008-08-20 09:07:45 +02001361 if (!iommu)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001362 panic("No mapping iommu for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001363
1364 index = alloc_irte(iommu, irq, 1);
1365 if (index < 0)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001366 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001367
1368 memset(&irte, 0, sizeof(irte));
1369
1370 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001371 irte.dst_mode = apic->irq_dest_mode;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001372 /*
1373 * Trigger mode in the IRTE will always be edge, and the
1374 * actual level or edge trigger will be setup in the IO-APIC
1375 * RTE. This will help simplify level triggered irq migration.
1376 * For more details, see the comments above explainig IO-APIC
1377 * irq migration in the presence of interrupt-remapping.
1378 */
1379 irte.trigger_mode = 0;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001380 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001381 irte.vector = vector;
1382 irte.dest_id = IRTE_DEST(destination);
1383
Weidong Hanf007e992009-05-23 00:41:15 +08001384 /* Set source-id of interrupt request */
1385 set_ioapic_sid(&irte, apic_id);
1386
Ingo Molnar54168ed2008-08-20 09:07:45 +02001387 modify_irte(irq, &irte);
1388
1389 ir_entry->index2 = (index >> 15) & 0x1;
1390 ir_entry->zero = 0;
1391 ir_entry->format = 1;
1392 ir_entry->index = (index & 0x7fff);
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001393 /*
1394 * IO-APIC RTE will be configured with virtual vector.
1395 * irq handler will do the explicit EOI to the io-apic.
1396 */
1397 ir_entry->vector = pin;
Suresh Siddha29b61be2009-03-16 17:05:02 -07001398 } else {
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001399 entry->delivery_mode = apic->irq_delivery_mode;
1400 entry->dest_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001401 entry->dest = destination;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001402 entry->vector = vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001403 }
1404
1405 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001406 entry->trigger = trigger;
1407 entry->polarity = polarity;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001408
1409 /* Mask level triggered irqs.
1410 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1411 */
1412 if (trigger)
1413 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001414 return 0;
1415}
1416
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001417static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001418 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001419{
1420 struct irq_cfg *cfg;
1421 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001422 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001423
1424 if (!IO_APIC_IRQ(irq))
1425 return;
1426
Yinghai Lu3145e942008-12-05 18:58:34 -08001427 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001428
Ingo Molnarfe402e12009-01-28 04:32:51 +01001429 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001430 return;
1431
Ingo Molnardebccb32009-01-28 15:20:18 +01001432 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07001433
1434 apic_printk(APIC_VERBOSE,KERN_DEBUG
1435 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1436 "IRQ %d Mode:%i Active:%i)\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001437 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001438 irq, trigger, polarity);
1439
1440
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001441 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001442 dest, trigger, polarity, cfg->vector, pin)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001443 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001444 mp_ioapics[apic_id].apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001445 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001446 return;
1447 }
1448
Yinghai Lu3145e942008-12-05 18:58:34 -08001449 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001450 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001451 disable_8259A_irq(irq);
1452
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001453 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Yinghai Lub9c61b702009-05-06 10:10:06 -07001456static struct {
1457 DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
1458} mp_ioapic_routing[MAX_IO_APICS];
1459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460static void __init setup_IO_APIC_irqs(void)
1461{
Yinghai Lub9c61b702009-05-06 10:10:06 -07001462 int apic_id = 0, pin, idx, irq;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001463 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001464 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001465 struct irq_cfg *cfg;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07001466 int node = cpu_to_node(boot_cpu_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1469
Yinghai Lub9c61b702009-05-06 10:10:06 -07001470#ifdef CONFIG_ACPI
1471 if (!acpi_disabled && acpi_ioapic) {
1472 apic_id = mp_find_ioapic(0);
1473 if (apic_id < 0)
1474 apic_id = 0;
1475 }
1476#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Yinghai Lub9c61b702009-05-06 10:10:06 -07001478 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
1479 idx = find_irq_entry(apic_id, pin, mp_INT);
1480 if (idx == -1) {
1481 if (!notcon) {
1482 notcon = 1;
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001483 apic_printk(APIC_VERBOSE,
Yinghai Lub9c61b702009-05-06 10:10:06 -07001484 KERN_DEBUG " %d-%d",
1485 mp_ioapics[apic_id].apicid, pin);
1486 } else
1487 apic_printk(APIC_VERBOSE, " %d-%d",
1488 mp_ioapics[apic_id].apicid, pin);
1489 continue;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001490 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07001491 if (notcon) {
1492 apic_printk(APIC_VERBOSE,
1493 " (apicid-pin) not connected\n");
1494 notcon = 0;
1495 }
1496
1497 irq = pin_2_irq(idx, apic_id, pin);
1498
1499 /*
1500 * Skip the timer IRQ if there's a quirk handler
1501 * installed and if it returns 1:
1502 */
1503 if (apic->multi_timer_check &&
1504 apic->multi_timer_check(apic_id, irq))
1505 continue;
1506
1507 desc = irq_to_desc_alloc_node(irq, node);
1508 if (!desc) {
1509 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1510 continue;
1511 }
1512 cfg = desc->chip_data;
1513 add_pin_to_irq_node(cfg, node, apic_id, pin);
Yinghai Lu4c6f18f2009-05-18 10:23:28 -07001514 /*
1515 * don't mark it in pin_programmed, so later acpi could
1516 * set it correctly when irq < 16
1517 */
Yinghai Lub9c61b702009-05-06 10:10:06 -07001518 setup_IO_APIC_irq(apic_id, pin, irq, desc,
1519 irq_trigger(idx), irq_polarity(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 }
1521
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001522 if (notcon)
1523 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001524 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001528 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001530static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001531 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532{
1533 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
Ingo Molnar54168ed2008-08-20 09:07:45 +02001535 if (intr_remapping_enabled)
1536 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001537
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001538 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540 /*
1541 * We use logical delivery to get the timer IRQ
1542 * to the first CPU.
1543 */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001544 entry.dest_mode = apic->irq_dest_mode;
Yinghai Luf72dcca2009-02-08 16:18:03 -08001545 entry.mask = 0; /* don't mask IRQ for edge */
Ingo Molnardebccb32009-01-28 15:20:18 +01001546 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001547 entry.delivery_mode = apic->irq_delivery_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 entry.polarity = 0;
1549 entry.trigger = 0;
1550 entry.vector = vector;
1551
1552 /*
1553 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001554 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001556 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 /*
1559 * Add it to the IO-APIC irq-routing table:
1560 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001561 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001564
1565__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566{
1567 int apic, i;
1568 union IO_APIC_reg_00 reg_00;
1569 union IO_APIC_reg_01 reg_01;
1570 union IO_APIC_reg_02 reg_02;
1571 union IO_APIC_reg_03 reg_03;
1572 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001573 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001574 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001575 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 if (apic_verbosity == APIC_QUIET)
1578 return;
1579
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001580 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 for (i = 0; i < nr_ioapics; i++)
1582 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301583 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 /*
1586 * We are a bit conservative about what we expect. We have to
1587 * know about every hardware change ASAP.
1588 */
1589 printk(KERN_INFO "testing the IO APIC.......................\n");
1590
1591 for (apic = 0; apic < nr_ioapics; apic++) {
1592
1593 spin_lock_irqsave(&ioapic_lock, flags);
1594 reg_00.raw = io_apic_read(apic, 0);
1595 reg_01.raw = io_apic_read(apic, 1);
1596 if (reg_01.bits.version >= 0x10)
1597 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001598 if (reg_01.bits.version >= 0x20)
1599 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 spin_unlock_irqrestore(&ioapic_lock, flags);
1601
Ingo Molnar54168ed2008-08-20 09:07:45 +02001602 printk("\n");
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301603 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1605 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1606 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1607 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Ingo Molnar54168ed2008-08-20 09:07:45 +02001609 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1613 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /*
1616 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1617 * but the value of reg_02 is read as the previous read register
1618 * value, so ignore it if reg_02 == reg_01.
1619 */
1620 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1621 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1622 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624
1625 /*
1626 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1627 * or reg_03, but the value of reg_0[23] is read as the previous read
1628 * register value, so ignore it if reg_03 == reg_0[12].
1629 */
1630 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1631 reg_03.raw != reg_01.raw) {
1632 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1633 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635
1636 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1637
Yinghai Lud83e94a2008-08-19 20:50:33 -07001638 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1639 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 for (i = 0; i <= reg_01.bits.entries; i++) {
1642 struct IO_APIC_route_entry entry;
1643
Andi Kleencf4c6a22006-09-26 10:52:30 +02001644 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Ingo Molnar54168ed2008-08-20 09:07:45 +02001646 printk(KERN_DEBUG " %02x %03X ",
1647 i,
1648 entry.dest
1649 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1652 entry.mask,
1653 entry.trigger,
1654 entry.irr,
1655 entry.polarity,
1656 entry.delivery_status,
1657 entry.dest_mode,
1658 entry.delivery_mode,
1659 entry.vector
1660 );
1661 }
1662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001664 for_each_irq_desc(irq, desc) {
1665 struct irq_pin_list *entry;
1666
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001667 cfg = desc->chip_data;
1668 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001669 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001671 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 for (;;) {
1673 printk("-> %d:%d", entry->apic, entry->pin);
1674 if (!entry->next)
1675 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001676 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 printk("\n");
1679 }
1680
1681 printk(KERN_INFO ".................................... done.\n");
1682
1683 return;
1684}
1685
Ingo Molnar251e1e42009-07-02 08:54:01 +02001686__apicdebuginit(void) print_APIC_field(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Ingo Molnar251e1e42009-07-02 08:54:01 +02001688 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
1690 if (apic_verbosity == APIC_QUIET)
1691 return;
1692
Ingo Molnar251e1e42009-07-02 08:54:01 +02001693 printk(KERN_DEBUG);
1694
1695 for (i = 0; i < 8; i++)
1696 printk(KERN_CONT "%08x", apic_read(base + i*0x10));
1697
1698 printk(KERN_CONT "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001701__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Andreas Herrmann97a52712009-05-08 18:23:50 +02001703 unsigned int i, v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001704 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 if (apic_verbosity == APIC_QUIET)
1707 return;
1708
Ingo Molnar251e1e42009-07-02 08:54:01 +02001709 printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001711 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001712 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 v = apic_read(APIC_LVR);
1714 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1715 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001716 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 v = apic_read(APIC_TASKPRI);
1719 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1720
Ingo Molnar54168ed2008-08-20 09:07:45 +02001721 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001722 if (!APIC_XAPIC(ver)) {
1723 v = apic_read(APIC_ARBPRI);
1724 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1725 v & APIC_ARBPRI_MASK);
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 v = apic_read(APIC_PROCPRI);
1728 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1729 }
1730
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001731 /*
1732 * Remote read supported only in the 82489DX and local APIC for
1733 * Pentium processors.
1734 */
1735 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1736 v = apic_read(APIC_RRR);
1737 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1738 }
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 v = apic_read(APIC_LDR);
1741 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001742 if (!x2apic_enabled()) {
1743 v = apic_read(APIC_DFR);
1744 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 v = apic_read(APIC_SPIV);
1747 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1748
1749 printk(KERN_DEBUG "... APIC ISR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001750 print_APIC_field(APIC_ISR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 printk(KERN_DEBUG "... APIC TMR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001752 print_APIC_field(APIC_TMR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 printk(KERN_DEBUG "... APIC IRR field:\n");
Ingo Molnar251e1e42009-07-02 08:54:01 +02001754 print_APIC_field(APIC_IRR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Ingo Molnar54168ed2008-08-20 09:07:45 +02001756 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1757 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 v = apic_read(APIC_ESR);
1761 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1762 }
1763
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001764 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001765 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1766 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
1768 v = apic_read(APIC_LVTT);
1769 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1770
1771 if (maxlvt > 3) { /* PC is LVT#4. */
1772 v = apic_read(APIC_LVTPC);
1773 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1774 }
1775 v = apic_read(APIC_LVT0);
1776 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1777 v = apic_read(APIC_LVT1);
1778 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1779
1780 if (maxlvt > 2) { /* ERR is LVT#3. */
1781 v = apic_read(APIC_LVTERR);
1782 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1783 }
1784
1785 v = apic_read(APIC_TMICT);
1786 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1787 v = apic_read(APIC_TMCCT);
1788 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1789 v = apic_read(APIC_TDCR);
1790 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
Andreas Herrmann97a52712009-05-08 18:23:50 +02001791
1792 if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1793 v = apic_read(APIC_EFEAT);
1794 maxlvt = (v >> 16) & 0xff;
1795 printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1796 v = apic_read(APIC_ECTRL);
1797 printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1798 for (i = 0; i < maxlvt; i++) {
1799 v = apic_read(APIC_EILVTn(i));
1800 printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1801 }
1802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 printk("\n");
1804}
1805
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001806__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001808 int cpu;
1809
1810 preempt_disable();
1811 for_each_online_cpu(cpu)
1812 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1813 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001816__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 unsigned int v;
1819 unsigned long flags;
1820
1821 if (apic_verbosity == APIC_QUIET)
1822 return;
1823
1824 printk(KERN_DEBUG "\nprinting PIC contents\n");
1825
1826 spin_lock_irqsave(&i8259A_lock, flags);
1827
1828 v = inb(0xa1) << 8 | inb(0x21);
1829 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1830
1831 v = inb(0xa0) << 8 | inb(0x20);
1832 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1833
Ingo Molnar54168ed2008-08-20 09:07:45 +02001834 outb(0x0b,0xa0);
1835 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001837 outb(0x0a,0xa0);
1838 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 spin_unlock_irqrestore(&i8259A_lock, flags);
1841
1842 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1843
1844 v = inb(0x4d1) << 8 | inb(0x4d0);
1845 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1846}
1847
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001848__apicdebuginit(int) print_all_ICs(void)
1849{
1850 print_PIC();
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001851
1852 /* don't print out if apic is not there */
1853 if (!cpu_has_apic || disable_apic)
1854 return 0;
1855
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001856 print_all_local_APICs();
1857 print_IO_APIC();
1858
1859 return 0;
1860}
1861
1862fs_initcall(print_all_ICs);
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Yinghai Luefa25592008-08-19 20:50:36 -07001865/* Where if anywhere is the i8259 connect in external int mode */
1866static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1867
Ingo Molnar54168ed2008-08-20 09:07:45 +02001868void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
1870 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001871 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001872 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 unsigned long flags;
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /*
1876 * The number of IO-APIC IRQ registers (== #pins):
1877 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001878 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001880 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001882 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1883 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001884 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001885 int pin;
1886 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001887 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001888 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001889 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001890
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001891 /* If the interrupt line is enabled and in ExtInt mode
1892 * I have found the pin where the i8259 is connected.
1893 */
1894 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1895 ioapic_i8259.apic = apic;
1896 ioapic_i8259.pin = pin;
1897 goto found_i8259;
1898 }
1899 }
1900 }
1901 found_i8259:
1902 /* Look to see what if the MP table has reported the ExtINT */
1903 /* If we could not find the appropriate pin by looking at the ioapic
1904 * the i8259 probably is not connected the ioapic but give the
1905 * mptable a chance anyway.
1906 */
1907 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1908 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1909 /* Trust the MP table if nothing is setup in the hardware */
1910 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1911 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1912 ioapic_i8259.pin = i8259_pin;
1913 ioapic_i8259.apic = i8259_apic;
1914 }
1915 /* Complain if the MP table and the hardware disagree */
1916 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1917 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1918 {
1919 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
1922 /*
1923 * Do not trust the IO-APIC being empty at bootup
1924 */
1925 clear_IO_APIC();
1926}
1927
1928/*
1929 * Not an __init, needed by the reboot code
1930 */
1931void disable_IO_APIC(void)
1932{
1933 /*
1934 * Clear the IO-APIC before rebooting:
1935 */
1936 clear_IO_APIC();
1937
Eric W. Biederman650927e2005-06-25 14:57:44 -07001938 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001939 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001940 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001941 * so legacy interrupts can be delivered.
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001942 *
1943 * With interrupt-remapping, for now we will use virtual wire A mode,
1944 * as virtual wire B is little complex (need to configure both
1945 * IOAPIC RTE aswell as interrupt-remapping table entry).
1946 * As this gets called during crash dump, keep this simple for now.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001947 */
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001948 if (ioapic_i8259.pin != -1 && !intr_remapping_enabled) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001949 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001950
1951 memset(&entry, 0, sizeof(entry));
1952 entry.mask = 0; /* Enabled */
1953 entry.trigger = 0; /* Edge */
1954 entry.irr = 0;
1955 entry.polarity = 0; /* High */
1956 entry.delivery_status = 0;
1957 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001958 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001959 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001960 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001961
1962 /*
1963 * Add it to the IO-APIC irq-routing table:
1964 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001965 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001966 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001967
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07001968 /*
1969 * Use virtual wire A mode when interrupt remapping is enabled.
1970 */
Cyrill Gorcunov3f4c3952009-06-17 22:13:22 +04001971 if (cpu_has_apic)
1972 disconnect_bsp_APIC(!intr_remapping_enabled &&
1973 ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974}
1975
Ingo Molnar54168ed2008-08-20 09:07:45 +02001976#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/*
1978 * function to set the IO-APIC physical IDs based on the
1979 * values stored in the MPC table.
1980 *
1981 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1982 */
1983
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984static void __init setup_ioapic_ids_from_mpc(void)
1985{
1986 union IO_APIC_reg_00 reg_00;
1987 physid_mask_t phys_id_present_map;
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001988 int apic_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 int i;
1990 unsigned char old_id;
1991 unsigned long flags;
1992
Yinghai Lua4dbc342008-07-25 02:14:28 -07001993 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001994 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001997 * Don't check I/O APIC IDs for xAPIC systems. They have
1998 * no meaning without the serial APIC bus.
1999 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002000 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2001 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002002 return;
2003 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 * This is broken; anything with a real cpu count has to
2005 * circumvent this idiocy regardless.
2006 */
Ingo Molnard190cb82009-01-28 06:50:47 +01002007 phys_id_present_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
2009 /*
2010 * Set the IOAPIC ID to the value stored in the MPC table.
2011 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002012 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 /* Read the register 0 value */
2015 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002016 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002018
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002019 old_id = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002021 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002023 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2025 reg_00.bits.ID);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002026 mp_ioapics[apic_id].apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /*
2030 * Sanity check, is the ID really free? Every APIC in a
2031 * system must have a unique ID or we get lots of nice
2032 * 'stuck on smp_invalidate_needed IPI wait' messages.
2033 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01002034 if (apic->check_apicid_used(phys_id_present_map,
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002035 mp_ioapics[apic_id].apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002037 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 for (i = 0; i < get_physical_broadcast(); i++)
2039 if (!physid_isset(i, phys_id_present_map))
2040 break;
2041 if (i >= get_physical_broadcast())
2042 panic("Max APIC ID exceeded!\n");
2043 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2044 i);
2045 physid_set(i, phys_id_present_map);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002046 mp_ioapics[apic_id].apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 } else {
2048 physid_mask_t tmp;
Ingo Molnar80587142009-01-28 06:50:47 +01002049 tmp = apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 apic_printk(APIC_VERBOSE, "Setting %d in the "
2051 "phys_id_present_map\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002052 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2054 }
2055
2056
2057 /*
2058 * We need to adjust the IRQ routing table
2059 * if the ID changed.
2060 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002061 if (old_id != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05302063 if (mp_irqs[i].dstapic == old_id)
2064 mp_irqs[i].dstapic
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002065 = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /*
2068 * Read the right value from the MPC table and
2069 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002070 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 apic_printk(APIC_VERBOSE, KERN_INFO
2072 "...changing IO-APIC physical APIC ID to %d ...",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002073 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002075 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002077 io_apic_write(apic_id, 0, reg_00.raw);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002078 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /*
2081 * Sanity check
2082 */
2083 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002084 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002086 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 printk("could not set ID!\n");
2088 else
2089 apic_printk(APIC_VERBOSE, " ok.\n");
2090 }
2091}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002092#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002094int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002095
2096static int __init notimercheck(char *s)
2097{
2098 no_timer_check = 1;
2099 return 1;
2100}
2101__setup("no_timer_check", notimercheck);
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103/*
2104 * There is a nasty bug in some older SMP boards, their mptable lies
2105 * about the timer IRQ. We do the following to work around the situation:
2106 *
2107 * - timer IRQ defaults to IO-APIC IRQ
2108 * - if this function detects that timer IRQs are defunct, then we fall
2109 * back to ISA timer IRQs
2110 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002111static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
2113 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002114 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
Zachary Amsden8542b202006-12-07 02:14:09 +01002116 if (no_timer_check)
2117 return 1;
2118
Ingo Molnar4aae0702007-12-18 18:05:58 +01002119 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 local_irq_enable();
2121 /* Let ten ticks pass... */
2122 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002123 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 /*
2126 * Expect a few ticks at least, to be sure some possible
2127 * glue logic does not lock up after one or two first
2128 * ticks in a non-ExtINT mode. Also the local APIC
2129 * might have cached one ExtINT interrupt. Finally, at
2130 * least one tick may be lost due to delays.
2131 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002132
2133 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002134 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 return 0;
2137}
2138
2139/*
2140 * In the SMP+IOAPIC case it might happen that there are an unspecified
2141 * number of pending IRQ events unhandled. These cases are very rare,
2142 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2143 * better to do it this way as thus we do not have to be aware of
2144 * 'pending' interrupts in the IRQ path, except at this point.
2145 */
2146/*
2147 * Edge triggered needs to resend any interrupt
2148 * that was delayed but this is now handled in the device
2149 * independent code.
2150 */
2151
2152/*
2153 * Starting up a edge-triggered IO-APIC interrupt is
2154 * nasty - we need to make sure that we get the edge.
2155 * If it is already asserted for some reason, we need
2156 * return 1 to indicate that is was pending.
2157 *
2158 * This is not complete - we should be able to fake
2159 * an edge even if it isn't on the 8259A...
2160 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002161
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002162static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 int was_pending = 0;
2165 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002166 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002169 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 disable_8259A_irq(irq);
2171 if (i8259A_irq_pending(irq))
2172 was_pending = 1;
2173 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002174 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002175 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 spin_unlock_irqrestore(&ioapic_lock, flags);
2177
2178 return was_pending;
2179}
2180
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002181static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002183
2184 struct irq_cfg *cfg = irq_cfg(irq);
2185 unsigned long flags;
2186
2187 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnardac5f412009-01-28 15:42:24 +01002188 apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002189 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002190
2191 return 1;
2192}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002193
2194/*
2195 * Level and edge triggered IO-APIC interrupts need different handling,
2196 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2197 * handled with the level-triggered descriptor, but that one has slightly
2198 * more overhead. Level-triggered interrupts cannot be handled with the
2199 * edge-triggered handler, without risking IRQ storms and other ugly
2200 * races.
2201 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002202
Yinghai Lu497c9a12008-08-19 20:50:28 -07002203#ifdef CONFIG_SMP
Gary Hadee85abf82009-04-08 14:07:25 -07002204static void send_cleanup_vector(struct irq_cfg *cfg)
2205{
2206 cpumask_var_t cleanup_mask;
2207
2208 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
2209 unsigned int i;
2210 cfg->move_cleanup_count = 0;
2211 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2212 cfg->move_cleanup_count++;
2213 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2214 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2215 } else {
2216 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
2217 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
2218 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2219 free_cpumask_var(cleanup_mask);
2220 }
2221 cfg->move_in_progress = 0;
2222}
2223
Ingo Molnar44204712009-05-01 19:02:50 +02002224static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Gary Hadee85abf82009-04-08 14:07:25 -07002225{
2226 int apic, pin;
2227 struct irq_pin_list *entry;
2228 u8 vector = cfg->vector;
2229
Jeremy Fitzhardinge638f2f82009-06-08 03:37:52 -07002230 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
Gary Hadee85abf82009-04-08 14:07:25 -07002231 unsigned int reg;
2232
Gary Hadee85abf82009-04-08 14:07:25 -07002233 apic = entry->apic;
2234 pin = entry->pin;
2235 /*
2236 * With interrupt-remapping, destination information comes
2237 * from interrupt-remapping table entry.
2238 */
2239 if (!irq_remapped(irq))
2240 io_apic_write(apic, 0x11 + pin*2, dest);
2241 reg = io_apic_read(apic, 0x10 + pin*2);
2242 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2243 reg |= vector;
2244 io_apic_modify(apic, 0x10 + pin*2, reg);
Gary Hadee85abf82009-04-08 14:07:25 -07002245 }
2246}
2247
Ingo Molnar44204712009-05-01 19:02:50 +02002248static int
2249assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
2250
Gary Hadee85abf82009-04-08 14:07:25 -07002251/*
2252 * Either sets desc->affinity to a valid value, and returns
2253 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
2254 * leaves desc->affinity untouched.
2255 */
2256static unsigned int
2257set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
2258{
2259 struct irq_cfg *cfg;
2260 unsigned int irq;
2261
2262 if (!cpumask_intersects(mask, cpu_online_mask))
2263 return BAD_APICID;
2264
2265 irq = desc->irq;
2266 cfg = desc->chip_data;
2267 if (assign_irq_vector(irq, cfg, mask))
2268 return BAD_APICID;
2269
Gary Hadee85abf82009-04-08 14:07:25 -07002270 cpumask_copy(desc->affinity, mask);
2271
2272 return apic->cpu_mask_to_apicid_and(desc->affinity, cfg->domain);
2273}
2274
Ingo Molnar44204712009-05-01 19:02:50 +02002275static int
Gary Hadee85abf82009-04-08 14:07:25 -07002276set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
2277{
2278 struct irq_cfg *cfg;
2279 unsigned long flags;
2280 unsigned int dest;
2281 unsigned int irq;
Ingo Molnar44204712009-05-01 19:02:50 +02002282 int ret = -1;
Gary Hadee85abf82009-04-08 14:07:25 -07002283
2284 irq = desc->irq;
2285 cfg = desc->chip_data;
2286
2287 spin_lock_irqsave(&ioapic_lock, flags);
2288 dest = set_desc_affinity(desc, mask);
2289 if (dest != BAD_APICID) {
2290 /* Only the high 8 bits are valid. */
2291 dest = SET_APIC_LOGICAL_ID(dest);
2292 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar44204712009-05-01 19:02:50 +02002293 ret = 0;
Gary Hadee85abf82009-04-08 14:07:25 -07002294 }
2295 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnar44204712009-05-01 19:02:50 +02002296
2297 return ret;
Gary Hadee85abf82009-04-08 14:07:25 -07002298}
2299
Ingo Molnar44204712009-05-01 19:02:50 +02002300static int
Gary Hadee85abf82009-04-08 14:07:25 -07002301set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
2302{
2303 struct irq_desc *desc;
2304
2305 desc = irq_to_desc(irq);
2306
Ingo Molnar44204712009-05-01 19:02:50 +02002307 return set_ioapic_affinity_irq_desc(desc, mask);
Gary Hadee85abf82009-04-08 14:07:25 -07002308}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002309
2310#ifdef CONFIG_INTR_REMAP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002311
2312/*
2313 * Migrate the IO-APIC irq in the presence of intr-remapping.
2314 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002315 * For both level and edge triggered, irq migration is a simple atomic
2316 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002317 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002318 * For level triggered, we eliminate the io-apic RTE modification (with the
2319 * updated vector information), by using a virtual vector (io-apic pin number).
2320 * Real vector that is used for interrupting cpu will be coming from
2321 * the interrupt-remapping table entry.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002322 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002323static int
Mike Travise7986732008-12-16 17:33:52 -08002324migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002325{
2326 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002327 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002328 unsigned int dest;
Yinghai Lu3145e942008-12-05 18:58:34 -08002329 unsigned int irq;
Yinghai Lud5dedd42009-04-27 17:59:21 -07002330 int ret = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002331
Mike Travis22f65d32008-12-16 17:33:56 -08002332 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002333 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002334
Yinghai Lu3145e942008-12-05 18:58:34 -08002335 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002336 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002337 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002338
Yinghai Lu3145e942008-12-05 18:58:34 -08002339 cfg = desc->chip_data;
2340 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002341 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002342
Ingo Molnardebccb32009-01-28 15:20:18 +01002343 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002344
Ingo Molnar54168ed2008-08-20 09:07:45 +02002345 irte.vector = cfg->vector;
2346 irte.dest_id = IRTE_DEST(dest);
2347
2348 /*
2349 * Modified the IRTE and flushes the Interrupt entry cache.
2350 */
2351 modify_irte(irq, &irte);
2352
Mike Travis22f65d32008-12-16 17:33:56 -08002353 if (cfg->move_in_progress)
2354 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002355
Mike Travis7f7ace02009-01-10 21:58:08 -08002356 cpumask_copy(desc->affinity, mask);
Yinghai Lud5dedd42009-04-27 17:59:21 -07002357
2358 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002359}
2360
Ingo Molnar54168ed2008-08-20 09:07:45 +02002361/*
2362 * Migrates the IRQ destination in the process context.
2363 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002364static int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Rusty Russell968ea6d2008-12-13 21:55:51 +10302365 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002366{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002367 return migrate_ioapic_irq_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002368}
Yinghai Lud5dedd42009-04-27 17:59:21 -07002369static int set_ir_ioapic_affinity_irq(unsigned int irq,
Rusty Russell0de26522008-12-13 21:20:26 +10302370 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002371{
2372 struct irq_desc *desc = irq_to_desc(irq);
2373
Yinghai Lud5dedd42009-04-27 17:59:21 -07002374 return set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002375}
Suresh Siddha29b61be2009-03-16 17:05:02 -07002376#else
Yinghai Lud5dedd42009-04-27 17:59:21 -07002377static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Suresh Siddha29b61be2009-03-16 17:05:02 -07002378 const struct cpumask *mask)
2379{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002380 return 0;
Suresh Siddha29b61be2009-03-16 17:05:02 -07002381}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002382#endif
2383
Yinghai Lu497c9a12008-08-19 20:50:28 -07002384asmlinkage void smp_irq_move_cleanup_interrupt(void)
2385{
2386 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002387
Yinghai Lu497c9a12008-08-19 20:50:28 -07002388 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002389 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002390 irq_enter();
2391
2392 me = smp_processor_id();
2393 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2394 unsigned int irq;
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002395 unsigned int irr;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002396 struct irq_desc *desc;
2397 struct irq_cfg *cfg;
2398 irq = __get_cpu_var(vector_irq)[vector];
2399
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002400 if (irq == -1)
2401 continue;
2402
Yinghai Lu497c9a12008-08-19 20:50:28 -07002403 desc = irq_to_desc(irq);
2404 if (!desc)
2405 continue;
2406
2407 cfg = irq_cfg(irq);
2408 spin_lock(&desc->lock);
2409 if (!cfg->move_cleanup_count)
2410 goto unlock;
2411
Mike Travis22f65d32008-12-16 17:33:56 -08002412 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002413 goto unlock;
2414
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002415 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2416 /*
2417 * Check if the vector that needs to be cleanedup is
2418 * registered at the cpu's IRR. If so, then this is not
2419 * the best time to clean it up. Lets clean it up in the
2420 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2421 * to myself.
2422 */
2423 if (irr & (1 << (vector % 32))) {
2424 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2425 goto unlock;
2426 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002427 __get_cpu_var(vector_irq)[vector] = -1;
2428 cfg->move_cleanup_count--;
2429unlock:
2430 spin_unlock(&desc->lock);
2431 }
2432
2433 irq_exit();
2434}
2435
Yinghai Lu3145e942008-12-05 18:58:34 -08002436static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002437{
Yinghai Lu3145e942008-12-05 18:58:34 -08002438 struct irq_desc *desc = *descp;
2439 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002440 unsigned vector, me;
2441
Yinghai Lufcef5912009-04-27 17:58:23 -07002442 if (likely(!cfg->move_in_progress))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002443 return;
2444
2445 vector = ~get_irq_regs()->orig_ax;
2446 me = smp_processor_id();
Yinghai Lu10b888d2009-01-31 14:50:07 -08002447
Yinghai Lufcef5912009-04-27 17:58:23 -07002448 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Mike Travis22f65d32008-12-16 17:33:56 -08002449 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002450}
2451#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002452static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002453#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002454
Yinghai Lu1d025192008-08-19 20:50:34 -07002455static void ack_apic_edge(unsigned int irq)
2456{
Yinghai Lu3145e942008-12-05 18:58:34 -08002457 struct irq_desc *desc = irq_to_desc(irq);
2458
2459 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002460 move_native_irq(irq);
2461 ack_APIC_irq();
2462}
2463
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002464atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002465
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002466static void ack_apic_level(unsigned int irq)
2467{
Yinghai Lu3145e942008-12-05 18:58:34 -08002468 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002469 unsigned long v;
2470 int i;
Yinghai Lu3145e942008-12-05 18:58:34 -08002471 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002472 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002473
Yinghai Lu3145e942008-12-05 18:58:34 -08002474 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002475#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002476 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002477 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002478 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002479 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002480 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002481#endif
2482
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002483 /*
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002484 * It appears there is an erratum which affects at least version 0x11
2485 * of I/O APIC (that's the 82093AA and cores integrated into various
2486 * chipsets). Under certain conditions a level-triggered interrupt is
2487 * erroneously delivered as edge-triggered one but the respective IRR
2488 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2489 * message but it will never arrive and further interrupts are blocked
2490 * from the source. The exact reason is so far unknown, but the
2491 * phenomenon was observed when two consecutive interrupt requests
2492 * from a given source get delivered to the same CPU and the source is
2493 * temporarily disabled in between.
2494 *
2495 * A workaround is to simulate an EOI message manually. We achieve it
2496 * by setting the trigger mode to edge and then to level when the edge
2497 * trigger mode gets detected in the TMR of a local APIC for a
2498 * level-triggered interrupt. We mask the source for the time of the
2499 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2500 * The idea is from Manfred Spraul. --macro
2501 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002502 cfg = desc->chip_data;
2503 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002504 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002505
Ingo Molnar54168ed2008-08-20 09:07:45 +02002506 /*
2507 * We must acknowledge the irq before we move it or the acknowledge will
2508 * not propagate properly.
2509 */
2510 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002511
Ingo Molnar54168ed2008-08-20 09:07:45 +02002512 /* Now we can move and renable the irq */
2513 if (unlikely(do_unmask_irq)) {
2514 /* Only migrate the irq if the ack has been received.
2515 *
2516 * On rare occasions the broadcast level triggered ack gets
2517 * delayed going to ioapics, and if we reprogram the
2518 * vector while Remote IRR is still set the irq will never
2519 * fire again.
2520 *
2521 * To prevent this scenario we read the Remote IRR bit
2522 * of the ioapic. This has two effects.
2523 * - On any sane system the read of the ioapic will
2524 * flush writes (and acks) going to the ioapic from
2525 * this cpu.
2526 * - We get to see if the ACK has actually been delivered.
2527 *
2528 * Based on failed experiments of reprogramming the
2529 * ioapic entry from outside of irq context starting
2530 * with masking the ioapic entry and then polling until
2531 * Remote IRR was clear before reprogramming the
2532 * ioapic I don't trust the Remote IRR bit to be
2533 * completey accurate.
2534 *
2535 * However there appears to be no other way to plug
2536 * this race, so if the Remote IRR bit is not
2537 * accurate and is causing problems then it is a hardware bug
2538 * and you can go talk to the chipset vendor about it.
2539 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002540 cfg = desc->chip_data;
2541 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002542 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002543 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002544 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002545
Jeremy Fitzhardinge916a0fe2009-06-08 03:00:22 -07002546 /* Tail end of version 0x11 I/O APIC bug workaround */
Yinghai Lu1d025192008-08-19 20:50:34 -07002547 if (!(v & (1 << (i & 0x1f)))) {
2548 atomic_inc(&irq_mis_count);
2549 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002550 __mask_and_edge_IO_APIC_irq(cfg);
2551 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002552 spin_unlock(&ioapic_lock);
2553 }
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002554}
Yinghai Lu1d025192008-08-19 20:50:34 -07002555
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002556#ifdef CONFIG_INTR_REMAP
Suresh Siddha25629d82009-04-20 13:02:28 -07002557static void __eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2558{
2559 int apic, pin;
2560 struct irq_pin_list *entry;
2561
2562 entry = cfg->irq_2_pin;
2563 for (;;) {
2564
2565 if (!entry)
2566 break;
2567
2568 apic = entry->apic;
2569 pin = entry->pin;
2570 io_apic_eoi(apic, pin);
2571 entry = entry->next;
2572 }
2573}
2574
2575static void
2576eoi_ioapic_irq(struct irq_desc *desc)
2577{
2578 struct irq_cfg *cfg;
2579 unsigned long flags;
2580 unsigned int irq;
2581
2582 irq = desc->irq;
2583 cfg = desc->chip_data;
2584
2585 spin_lock_irqsave(&ioapic_lock, flags);
2586 __eoi_ioapic_irq(irq, cfg);
2587 spin_unlock_irqrestore(&ioapic_lock, flags);
2588}
2589
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002590static void ir_ack_apic_edge(unsigned int irq)
2591{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002592 ack_APIC_irq();
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002593}
2594
2595static void ir_ack_apic_level(unsigned int irq)
2596{
Weidong Han5d0ae2d2009-04-17 16:42:13 +08002597 struct irq_desc *desc = irq_to_desc(irq);
2598
2599 ack_APIC_irq();
2600 eoi_ioapic_irq(desc);
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002601}
2602#endif /* CONFIG_INTR_REMAP */
2603
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002604static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002605 .name = "IO-APIC",
2606 .startup = startup_ioapic_irq,
2607 .mask = mask_IO_APIC_irq,
2608 .unmask = unmask_IO_APIC_irq,
2609 .ack = ack_apic_edge,
2610 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002611#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002612 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002613#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002614 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615};
2616
Ingo Molnar54168ed2008-08-20 09:07:45 +02002617static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002618 .name = "IR-IO-APIC",
2619 .startup = startup_ioapic_irq,
2620 .mask = mask_IO_APIC_irq,
2621 .unmask = unmask_IO_APIC_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302622#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002623 .ack = ir_ack_apic_edge,
2624 .eoi = ir_ack_apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002625#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002626 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002627#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302628#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002629 .retrigger = ioapic_retrigger_irq,
2630};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632static inline void init_IO_APIC_traps(void)
2633{
2634 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002635 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002636 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 /*
2639 * NOTE! The local APIC isn't very good at handling
2640 * multiple interrupts at the same interrupt level.
2641 * As the interrupt level is determined by taking the
2642 * vector number and shifting that right by 4, we
2643 * want to spread these out a bit so that they don't
2644 * all fall in the same interrupt level.
2645 *
2646 * Also, we've got to be careful not to trash gate
2647 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2648 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002649 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002650 cfg = desc->chip_data;
2651 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 /*
2653 * Hmm.. We don't have an entry for this,
2654 * so default to an old-fashioned 8259
2655 * interrupt if we can..
2656 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002657 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002659 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002661 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 }
2664}
2665
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002666/*
2667 * The local APIC irq-chip implementation:
2668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002670static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671{
2672 unsigned long v;
2673
2674 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002675 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002678static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002680 unsigned long v;
2681
2682 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002683 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Yinghai Lu3145e942008-12-05 18:58:34 -08002686static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002687{
2688 ack_APIC_irq();
2689}
2690
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002691static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002692 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002693 .mask = mask_lapic_irq,
2694 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002695 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696};
2697
Yinghai Lu3145e942008-12-05 18:58:34 -08002698static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002699{
Yinghai Lu08678b02008-08-19 20:50:05 -07002700 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002701 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2702 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002703}
2704
Jan Beuliche9427102008-01-30 13:31:24 +01002705static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
2707 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002708 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 * We put the 8259A master into AEOI mode and
2710 * unmask on all local APICs LVT0 as NMI.
2711 *
2712 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2713 * is from Maciej W. Rozycki - so we do not have to EOI from
2714 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2717
Jan Beuliche9427102008-01-30 13:31:24 +01002718 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 apic_printk(APIC_VERBOSE, " done.\n");
2721}
2722
2723/*
2724 * This looks a bit hackish but it's about the only one way of sending
2725 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2726 * not support the ExtINT mode, unfortunately. We need to send these
2727 * cycles as some i82489DX-based boards have glue logic that keeps the
2728 * 8259A interrupt line asserted until INTA. --macro
2729 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002730static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002732 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 struct IO_APIC_route_entry entry0, entry1;
2734 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002736 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002737 if (pin == -1) {
2738 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002740 }
2741 apic = find_isa_irq_apic(8, mp_INT);
2742 if (apic == -1) {
2743 WARN_ON_ONCE(1);
2744 return;
2745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Andi Kleencf4c6a22006-09-26 10:52:30 +02002747 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002748 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 memset(&entry1, 0, sizeof(entry1));
2751
2752 entry1.dest_mode = 0; /* physical delivery */
2753 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002754 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 entry1.delivery_mode = dest_ExtINT;
2756 entry1.polarity = entry0.polarity;
2757 entry1.trigger = 0;
2758 entry1.vector = 0;
2759
Andi Kleencf4c6a22006-09-26 10:52:30 +02002760 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762 save_control = CMOS_READ(RTC_CONTROL);
2763 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2764 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2765 RTC_FREQ_SELECT);
2766 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2767
2768 i = 100;
2769 while (i-- > 0) {
2770 mdelay(10);
2771 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2772 i -= 10;
2773 }
2774
2775 CMOS_WRITE(save_control, RTC_CONTROL);
2776 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002777 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Andi Kleencf4c6a22006-09-26 10:52:30 +02002779 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Yinghai Luefa25592008-08-19 20:50:36 -07002782static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002783/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002784static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002785{
2786 disable_timer_pin_1 = 1;
2787 return 0;
2788}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002789early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002790
2791int timer_through_8259 __initdata;
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793/*
2794 * This code may look a bit paranoid, but it's supposed to cooperate with
2795 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2796 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2797 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002798 *
2799 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002801static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
Yinghai Lu3145e942008-12-05 18:58:34 -08002803 struct irq_desc *desc = irq_to_desc(0);
2804 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002805 int node = cpu_to_node(boot_cpu_id);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002806 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002807 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002808 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002809
2810 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 /*
2813 * get/set the timer IRQ vector:
2814 */
2815 disable_8259A_irq(0);
Ingo Molnarfe402e12009-01-28 04:32:51 +01002816 assign_irq_vector(0, cfg, apic->target_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002819 * As IRQ0 is to be enabled in the 8259A, the virtual
2820 * wire has to be disabled in the local APIC. Also
2821 * timer interrupts need to be acknowledged manually in
2822 * the 8259A for the i82489DX when using the NMI
2823 * watchdog as that APIC treats NMIs as level-triggered.
2824 * The AEOI mode will finish them in the 8259A
2825 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002827 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002829#ifdef CONFIG_X86_32
Yinghai Luf72dcca2009-02-08 16:18:03 -08002830 {
2831 unsigned int ver;
2832
2833 ver = apic_read(APIC_LVR);
2834 ver = GET_APIC_VERSION(ver);
2835 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2836 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002837#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002839 pin1 = find_isa_irq_pin(0, mp_INT);
2840 apic1 = find_isa_irq_apic(0, mp_INT);
2841 pin2 = ioapic_i8259.pin;
2842 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002844 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2845 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002846 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002848 /*
2849 * Some BIOS writers are clueless and report the ExtINTA
2850 * I/O APIC input from the cascaded 8259A as the timer
2851 * interrupt input. So just in case, if only one pin
2852 * was found above, try it both directly and through the
2853 * 8259A.
2854 */
2855 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002856 if (intr_remapping_enabled)
2857 panic("BIOS bug: timer not connected to IO-APIC");
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002858 pin1 = pin2;
2859 apic1 = apic2;
2860 no_pin1 = 1;
2861 } else if (pin2 == -1) {
2862 pin2 = pin1;
2863 apic2 = apic1;
2864 }
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (pin1 != -1) {
2867 /*
2868 * Ok, does IRQ0 through the IOAPIC work?
2869 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002870 if (no_pin1) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002871 add_pin_to_irq_node(cfg, node, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002872 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Yinghai Luf72dcca2009-02-08 16:18:03 -08002873 } else {
2874 /* for edge trigger, setup_IO_APIC_irq already
2875 * leave it unmasked.
2876 * so only need to unmask if it is level-trigger
2877 * do we really have level trigger timer?
2878 */
2879 int idx;
2880 idx = find_irq_entry(apic1, pin1, mp_INT);
2881 if (idx != -1 && irq_trigger(idx))
2882 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (timer_irq_works()) {
2885 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 setup_nmi();
2887 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002889 if (disable_timer_pin_1 > 0)
2890 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002891 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002893 if (intr_remapping_enabled)
2894 panic("timer doesn't work through Interrupt-remapped IO-APIC");
Yinghai Luf72dcca2009-02-08 16:18:03 -08002895 local_irq_disable();
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002896 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002897 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002898 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2899 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002901 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2902 "(IRQ0) through the 8259A ...\n");
2903 apic_printk(APIC_QUIET, KERN_INFO
2904 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 /*
2906 * legacy devices should be connected to IO APIC #0
2907 */
Yinghai Lu85ac16d2009-04-27 18:00:38 -07002908 replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002909 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002910 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002912 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002913 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002915 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002917 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002919 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 }
2921 /*
2922 * Cleanup, just in case ...
2923 */
Yinghai Luf72dcca2009-02-08 16:18:03 -08002924 local_irq_disable();
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002925 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002926 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002927 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002931 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2932 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002933 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002935#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002936 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002937#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002939 apic_printk(APIC_QUIET, KERN_INFO
2940 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Yinghai Lu3145e942008-12-05 18:58:34 -08002942 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002943 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 enable_8259A_irq(0);
2945
2946 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002947 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002948 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002950 local_irq_disable();
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002951 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002952 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002953 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002955 apic_printk(APIC_QUIET, KERN_INFO
2956 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 init_8259A(0);
2959 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002960 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 unlock_ExtINT_logic();
2963
2964 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002965 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002966 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08002968 local_irq_disable();
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002969 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002971 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002972out:
2973 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974}
2975
2976/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002977 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2978 * to devices. However there may be an I/O APIC pin available for
2979 * this interrupt regardless. The pin may be left unconnected, but
2980 * typically it will be reused as an ExtINT cascade interrupt for
2981 * the master 8259A. In the MPS case such a pin will normally be
2982 * reported as an ExtINT interrupt in the MP table. With ACPI
2983 * there is no provision for ExtINT interrupts, and in the absence
2984 * of an override it would be treated as an ordinary ISA I/O APIC
2985 * interrupt, that is edge-triggered and unmasked by default. We
2986 * used to do this, but it caused problems on some systems because
2987 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2988 * the same ExtINT cascade interrupt to drive the local APIC of the
2989 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2990 * the I/O APIC in all cases now. No actual device should request
2991 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 */
2993#define PIC_IRQS (1 << PIC_CASCADE_IR)
2994
2995void __init setup_IO_APIC(void)
2996{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002997
Ingo Molnar54168ed2008-08-20 09:07:45 +02002998 /*
2999 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3000 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003002 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Ingo Molnar54168ed2008-08-20 09:07:45 +02003004 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003005 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003006 * Set up IO-APIC IRQ routing.
3007 */
3008#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003009 if (!acpi_ioapic)
3010 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003011#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 sync_Arb_IDs();
3013 setup_IO_APIC_irqs();
3014 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08003015 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
3018/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003019 * Called after all the initialization is done. If we didnt find any
3020 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023static int __init io_apic_bug_finalize(void)
3024{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003025 if (sis_apic_bug == -1)
3026 sis_apic_bug = 0;
3027 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028}
3029
3030late_initcall(io_apic_bug_finalize);
3031
3032struct sysfs_ioapic_data {
3033 struct sys_device dev;
3034 struct IO_APIC_route_entry entry[0];
3035};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003036static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
Pavel Machek438510f2005-04-16 15:25:24 -07003038static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039{
3040 struct IO_APIC_route_entry *entry;
3041 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 data = container_of(dev, struct sysfs_ioapic_data, dev);
3045 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003046 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3047 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 return 0;
3050}
3051
3052static int ioapic_resume(struct sys_device *dev)
3053{
3054 struct IO_APIC_route_entry *entry;
3055 struct sysfs_ioapic_data *data;
3056 unsigned long flags;
3057 union IO_APIC_reg_00 reg_00;
3058 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 data = container_of(dev, struct sysfs_ioapic_data, dev);
3061 entry = data->entry;
3062
3063 spin_lock_irqsave(&ioapic_lock, flags);
3064 reg_00.raw = io_apic_read(dev->id, 0);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05303065 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3066 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 io_apic_write(dev->id, 0, reg_00.raw);
3068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003070 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003071 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 return 0;
3074}
3075
3076static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003077 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 .suspend = ioapic_suspend,
3079 .resume = ioapic_resume,
3080};
3081
3082static int __init ioapic_init_sysfs(void)
3083{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003084 struct sys_device * dev;
3085 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087 error = sysdev_class_register(&ioapic_sysdev_class);
3088 if (error)
3089 return error;
3090
Ingo Molnar54168ed2008-08-20 09:07:45 +02003091 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003092 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003094 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 if (!mp_ioapic_data[i]) {
3096 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3097 continue;
3098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003100 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 dev->cls = &ioapic_sysdev_class;
3102 error = sysdev_register(dev);
3103 if (error) {
3104 kfree(mp_ioapic_data[i]);
3105 mp_ioapic_data[i] = NULL;
3106 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3107 continue;
3108 }
3109 }
3110
3111 return 0;
3112}
3113
3114device_initcall(ioapic_init_sysfs);
3115
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003116static int nr_irqs_gsi = NR_IRQS_LEGACY;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003117/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003118 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003119 */
Yinghai Lud047f53a2009-04-27 18:02:23 -07003120unsigned int create_irq_nr(unsigned int irq_want, int node)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003121{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003122 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003123 unsigned int irq;
3124 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003125 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003126 struct irq_cfg *cfg_new = NULL;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003127 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003128
3129 irq = 0;
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003130 if (irq_want < nr_irqs_gsi)
3131 irq_want = nr_irqs_gsi;
3132
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003133 spin_lock_irqsave(&vector_lock, flags);
Mike Travis95949492009-01-10 22:24:06 -08003134 for (new = irq_want; new < nr_irqs; new++) {
Yinghai Lu85ac16d2009-04-27 18:00:38 -07003135 desc_new = irq_to_desc_alloc_node(new, node);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003136 if (!desc_new) {
3137 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003138 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003139 }
3140 cfg_new = desc_new->chip_data;
3141
3142 if (cfg_new->vector != 0)
3143 continue;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003144
Yinghai Lu15e957d2009-04-30 01:17:50 -07003145 desc_new = move_irq_desc(desc_new, node);
Yinghai Lud047f53a2009-04-27 18:02:23 -07003146
Ingo Molnarfe402e12009-01-28 04:32:51 +01003147 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003148 irq = new;
3149 break;
3150 }
3151 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003152
Yinghai Lu199751d2008-08-19 20:50:27 -07003153 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003154 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003155 /* restore it, in case dynamic_irq_init clear it */
3156 if (desc_new)
3157 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003158 }
3159 return irq;
3160}
3161
Yinghai Lu199751d2008-08-19 20:50:27 -07003162int create_irq(void)
3163{
Yinghai Lud047f53a2009-04-27 18:02:23 -07003164 int node = cpu_to_node(boot_cpu_id);
Yinghai Lube5d5352008-12-05 18:58:33 -08003165 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003166 int irq;
3167
Yinghai Lube5d5352008-12-05 18:58:33 -08003168 irq_want = nr_irqs_gsi;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003169 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003170
3171 if (irq == 0)
3172 irq = -1;
3173
3174 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003175}
3176
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003177void destroy_irq(unsigned int irq)
3178{
3179 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003180 struct irq_cfg *cfg;
3181 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003182
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003183 /* store it, in case dynamic_irq_cleanup clear it */
3184 desc = irq_to_desc(irq);
3185 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003186 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003187 /* connect back irq_cfg */
Bartlomiej Zolnierkiewicz25f6e892009-07-30 23:21:18 +02003188 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003189
Ingo Molnar54168ed2008-08-20 09:07:45 +02003190 free_irte(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003191 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003192 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003193 spin_unlock_irqrestore(&vector_lock, flags);
3194}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003195
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003196/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003197 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003198 */
3199#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003200static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003201{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003202 struct irq_cfg *cfg;
3203 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003204 unsigned dest;
3205
Jan Beulichf1182632009-01-14 12:27:35 +00003206 if (disable_apic)
3207 return -ENXIO;
3208
Yinghai Lu3145e942008-12-05 18:58:34 -08003209 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003210 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07003211 if (err)
3212 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003213
Ingo Molnardebccb32009-01-28 15:20:18 +01003214 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003215
Ingo Molnar54168ed2008-08-20 09:07:45 +02003216 if (irq_remapped(irq)) {
3217 struct irte irte;
3218 int ir_index;
3219 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003220
Ingo Molnar54168ed2008-08-20 09:07:45 +02003221 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3222 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003223
Ingo Molnar54168ed2008-08-20 09:07:45 +02003224 memset (&irte, 0, sizeof(irte));
3225
3226 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003227 irte.dst_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003228 irte.trigger_mode = 0; /* edge */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003229 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003230 irte.vector = cfg->vector;
3231 irte.dest_id = IRTE_DEST(dest);
3232
Weidong Hanf007e992009-05-23 00:41:15 +08003233 /* Set source-id of interrupt request */
3234 set_msi_sid(&irte, pdev);
3235
Ingo Molnar54168ed2008-08-20 09:07:45 +02003236 modify_irte(irq, &irte);
3237
3238 msg->address_hi = MSI_ADDR_BASE_HI;
3239 msg->data = sub_handle;
3240 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3241 MSI_ADDR_IR_SHV |
3242 MSI_ADDR_IR_INDEX1(ir_index) |
3243 MSI_ADDR_IR_INDEX2(ir_index);
Suresh Siddha29b61be2009-03-16 17:05:02 -07003244 } else {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003245 if (x2apic_enabled())
3246 msg->address_hi = MSI_ADDR_BASE_HI |
3247 MSI_ADDR_EXT_DEST_ID(dest);
3248 else
3249 msg->address_hi = MSI_ADDR_BASE_HI;
3250
Ingo Molnar54168ed2008-08-20 09:07:45 +02003251 msg->address_lo =
3252 MSI_ADDR_BASE_LO |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003253 ((apic->irq_dest_mode == 0) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003254 MSI_ADDR_DEST_MODE_PHYSICAL:
3255 MSI_ADDR_DEST_MODE_LOGICAL) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003256 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003257 MSI_ADDR_REDIRECTION_CPU:
3258 MSI_ADDR_REDIRECTION_LOWPRI) |
3259 MSI_ADDR_DEST_ID(dest);
3260
3261 msg->data =
3262 MSI_DATA_TRIGGER_EDGE |
3263 MSI_DATA_LEVEL_ASSERT |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003264 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003265 MSI_DATA_DELIVERY_FIXED:
3266 MSI_DATA_DELIVERY_LOWPRI) |
3267 MSI_DATA_VECTOR(cfg->vector);
3268 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003269 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003270}
3271
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003272#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003273static int set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003274{
Yinghai Lu3145e942008-12-05 18:58:34 -08003275 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003276 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003277 struct msi_msg msg;
3278 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003279
Mike Travis22f65d32008-12-16 17:33:56 -08003280 dest = set_desc_affinity(desc, mask);
3281 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003282 return -1;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003283
Yinghai Lu3145e942008-12-05 18:58:34 -08003284 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003285
Yinghai Lu3145e942008-12-05 18:58:34 -08003286 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003287
3288 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003289 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003290 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3291 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3292
Yinghai Lu3145e942008-12-05 18:58:34 -08003293 write_msi_msg_desc(desc, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003294
3295 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003296}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003297#ifdef CONFIG_INTR_REMAP
3298/*
3299 * Migrate the MSI irq to another cpumask. This migration is
3300 * done in the process context using interrupt-remapping hardware.
3301 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07003302static int
Mike Travise7986732008-12-16 17:33:52 -08003303ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003304{
Yinghai Lu3145e942008-12-05 18:58:34 -08003305 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003306 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003307 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003308 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003309
3310 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003311 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003312
Mike Travis22f65d32008-12-16 17:33:56 -08003313 dest = set_desc_affinity(desc, mask);
3314 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003315 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003316
Ingo Molnar54168ed2008-08-20 09:07:45 +02003317 irte.vector = cfg->vector;
3318 irte.dest_id = IRTE_DEST(dest);
3319
3320 /*
3321 * atomically update the IRTE with the new destination and vector.
3322 */
3323 modify_irte(irq, &irte);
3324
3325 /*
3326 * After this point, all the interrupts will start arriving
3327 * at the new destination. So, time to cleanup the previous
3328 * vector allocation.
3329 */
Mike Travis22f65d32008-12-16 17:33:56 -08003330 if (cfg->move_in_progress)
3331 send_cleanup_vector(cfg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003332
3333 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003334}
Yinghai Lu3145e942008-12-05 18:58:34 -08003335
Ingo Molnar54168ed2008-08-20 09:07:45 +02003336#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003337#endif /* CONFIG_SMP */
3338
3339/*
3340 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3341 * which implement the MSI or MSI-X Capability Structure.
3342 */
3343static struct irq_chip msi_chip = {
3344 .name = "PCI-MSI",
3345 .unmask = unmask_msi_irq,
3346 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003347 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003348#ifdef CONFIG_SMP
3349 .set_affinity = set_msi_irq_affinity,
3350#endif
3351 .retrigger = ioapic_retrigger_irq,
3352};
3353
Ingo Molnar54168ed2008-08-20 09:07:45 +02003354static struct irq_chip msi_ir_chip = {
3355 .name = "IR-PCI-MSI",
3356 .unmask = unmask_msi_irq,
3357 .mask = mask_msi_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303358#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08003359 .ack = ir_ack_apic_edge,
Ingo Molnar54168ed2008-08-20 09:07:45 +02003360#ifdef CONFIG_SMP
3361 .set_affinity = ir_set_msi_irq_affinity,
3362#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303363#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02003364 .retrigger = ioapic_retrigger_irq,
3365};
3366
3367/*
3368 * Map the PCI dev to the corresponding remapping hardware unit
3369 * and allocate 'nvec' consecutive interrupt-remapping table entries
3370 * in it.
3371 */
3372static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3373{
3374 struct intel_iommu *iommu;
3375 int index;
3376
3377 iommu = map_dev_to_ir(dev);
3378 if (!iommu) {
3379 printk(KERN_ERR
3380 "Unable to map PCI %s to iommu\n", pci_name(dev));
3381 return -ENOENT;
3382 }
3383
3384 index = alloc_irte(iommu, irq, nvec);
3385 if (index < 0) {
3386 printk(KERN_ERR
3387 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003388 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003389 return -ENOSPC;
3390 }
3391 return index;
3392}
Yinghai Lu1d025192008-08-19 20:50:34 -07003393
Yinghai Lu3145e942008-12-05 18:58:34 -08003394static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003395{
3396 int ret;
3397 struct msi_msg msg;
3398
3399 ret = msi_compose_msg(dev, irq, &msg);
3400 if (ret < 0)
3401 return ret;
3402
Yinghai Lu3145e942008-12-05 18:58:34 -08003403 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003404 write_msi_msg(irq, &msg);
3405
Ingo Molnar54168ed2008-08-20 09:07:45 +02003406 if (irq_remapped(irq)) {
3407 struct irq_desc *desc = irq_to_desc(irq);
3408 /*
3409 * irq migration in process context
3410 */
3411 desc->status |= IRQ_MOVE_PCNTXT;
3412 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3413 } else
Ingo Molnar54168ed2008-08-20 09:07:45 +02003414 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003415
Yinghai Luc81bba42008-09-25 11:53:11 -07003416 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3417
Yinghai Lu1d025192008-08-19 20:50:34 -07003418 return 0;
3419}
3420
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003421int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3422{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003423 unsigned int irq;
3424 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003425 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003426 unsigned int irq_want;
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003427 struct intel_iommu *iommu = NULL;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003428 int index = 0;
Yinghai Lud047f53a2009-04-27 18:02:23 -07003429 int node;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003430
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -04003431 /* x86 doesn't support multiple MSI yet */
3432 if (type == PCI_CAP_ID_MSI && nvec > 1)
3433 return 1;
3434
Yinghai Lud047f53a2009-04-27 18:02:23 -07003435 node = dev_to_node(&dev->dev);
Yinghai Lube5d5352008-12-05 18:58:33 -08003436 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003437 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003438 list_for_each_entry(msidesc, &dev->msi_list, list) {
Yinghai Lud047f53a2009-04-27 18:02:23 -07003439 irq = create_irq_nr(irq_want, node);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003440 if (irq == 0)
3441 return -1;
Yinghai Luf1ee5542009-02-08 16:18:03 -08003442 irq_want = irq + 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003443 if (!intr_remapping_enabled)
3444 goto no_ir;
3445
3446 if (!sub_handle) {
3447 /*
3448 * allocate the consecutive block of IRTE's
3449 * for 'nvec'
3450 */
3451 index = msi_alloc_irte(dev, irq, nvec);
3452 if (index < 0) {
3453 ret = index;
3454 goto error;
3455 }
3456 } else {
3457 iommu = map_dev_to_ir(dev);
3458 if (!iommu) {
3459 ret = -ENOENT;
3460 goto error;
3461 }
3462 /*
3463 * setup the mapping between the irq and the IRTE
3464 * base index, the sub_handle pointing to the
3465 * appropriate interrupt remap table entry.
3466 */
3467 set_irte_irq(irq, iommu, index, sub_handle);
3468 }
3469no_ir:
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003470 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003471 if (ret < 0)
3472 goto error;
3473 sub_handle++;
3474 }
3475 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003476
3477error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003478 destroy_irq(irq);
3479 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003480}
3481
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003482void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003483{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003484 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003485}
3486
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003487#if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003488#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003489static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003490{
Yinghai Lu3145e942008-12-05 18:58:34 -08003491 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003492 struct irq_cfg *cfg;
3493 struct msi_msg msg;
3494 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003495
Mike Travis22f65d32008-12-16 17:33:56 -08003496 dest = set_desc_affinity(desc, mask);
3497 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003498 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003499
Yinghai Lu3145e942008-12-05 18:58:34 -08003500 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003501
3502 dmar_msi_read(irq, &msg);
3503
3504 msg.data &= ~MSI_DATA_VECTOR_MASK;
3505 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3506 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3507 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3508
3509 dmar_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003510
3511 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003512}
Yinghai Lu3145e942008-12-05 18:58:34 -08003513
Ingo Molnar54168ed2008-08-20 09:07:45 +02003514#endif /* CONFIG_SMP */
3515
Jaswinder Singh Rajput8f7007a2009-06-10 12:41:01 -07003516static struct irq_chip dmar_msi_type = {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003517 .name = "DMAR_MSI",
3518 .unmask = dmar_msi_unmask,
3519 .mask = dmar_msi_mask,
3520 .ack = ack_apic_edge,
3521#ifdef CONFIG_SMP
3522 .set_affinity = dmar_msi_set_affinity,
3523#endif
3524 .retrigger = ioapic_retrigger_irq,
3525};
3526
3527int arch_setup_dmar_msi(unsigned int irq)
3528{
3529 int ret;
3530 struct msi_msg msg;
3531
3532 ret = msi_compose_msg(NULL, irq, &msg);
3533 if (ret < 0)
3534 return ret;
3535 dmar_msi_write(irq, &msg);
3536 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3537 "edge");
3538 return 0;
3539}
3540#endif
3541
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003542#ifdef CONFIG_HPET_TIMER
3543
3544#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003545static int hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003546{
Yinghai Lu3145e942008-12-05 18:58:34 -08003547 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003548 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003549 struct msi_msg msg;
3550 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003551
Mike Travis22f65d32008-12-16 17:33:56 -08003552 dest = set_desc_affinity(desc, mask);
3553 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003554 return -1;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003555
Yinghai Lu3145e942008-12-05 18:58:34 -08003556 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003557
3558 hpet_msi_read(irq, &msg);
3559
3560 msg.data &= ~MSI_DATA_VECTOR_MASK;
3561 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3562 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3563 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3564
3565 hpet_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003566
3567 return 0;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003568}
Yinghai Lu3145e942008-12-05 18:58:34 -08003569
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003570#endif /* CONFIG_SMP */
3571
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003572static struct irq_chip hpet_msi_type = {
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003573 .name = "HPET_MSI",
3574 .unmask = hpet_msi_unmask,
3575 .mask = hpet_msi_mask,
3576 .ack = ack_apic_edge,
3577#ifdef CONFIG_SMP
3578 .set_affinity = hpet_msi_set_affinity,
3579#endif
3580 .retrigger = ioapic_retrigger_irq,
3581};
3582
3583int arch_setup_hpet_msi(unsigned int irq)
3584{
3585 int ret;
3586 struct msi_msg msg;
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003587 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003588
3589 ret = msi_compose_msg(NULL, irq, &msg);
3590 if (ret < 0)
3591 return ret;
3592
3593 hpet_msi_write(irq, &msg);
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003594 desc->status |= IRQ_MOVE_PCNTXT;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003595 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3596 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003597
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003598 return 0;
3599}
3600#endif
3601
Ingo Molnar54168ed2008-08-20 09:07:45 +02003602#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003603/*
3604 * Hypertransport interrupt support
3605 */
3606#ifdef CONFIG_HT_IRQ
3607
3608#ifdef CONFIG_SMP
3609
Yinghai Lu497c9a12008-08-19 20:50:28 -07003610static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003611{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003612 struct ht_irq_msg msg;
3613 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003614
Yinghai Lu497c9a12008-08-19 20:50:28 -07003615 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003616 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003617
Yinghai Lu497c9a12008-08-19 20:50:28 -07003618 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003619 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003620
Eric W. Biedermanec683072006-11-08 17:44:57 -08003621 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003622}
3623
Yinghai Lud5dedd42009-04-27 17:59:21 -07003624static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003625{
Yinghai Lu3145e942008-12-05 18:58:34 -08003626 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003627 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003628 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003629
Mike Travis22f65d32008-12-16 17:33:56 -08003630 dest = set_desc_affinity(desc, mask);
3631 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003632 return -1;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003633
Yinghai Lu3145e942008-12-05 18:58:34 -08003634 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003635
Yinghai Lu497c9a12008-08-19 20:50:28 -07003636 target_ht_irq(irq, dest, cfg->vector);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003637
3638 return 0;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003639}
Yinghai Lu3145e942008-12-05 18:58:34 -08003640
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003641#endif
3642
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003643static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003644 .name = "PCI-HT",
3645 .mask = mask_ht_irq,
3646 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003647 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003648#ifdef CONFIG_SMP
3649 .set_affinity = set_ht_irq_affinity,
3650#endif
3651 .retrigger = ioapic_retrigger_irq,
3652};
3653
3654int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3655{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003656 struct irq_cfg *cfg;
3657 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003658
Jan Beulichf1182632009-01-14 12:27:35 +00003659 if (disable_apic)
3660 return -ENXIO;
3661
Yinghai Lu3145e942008-12-05 18:58:34 -08003662 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003663 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Ingo Molnar54168ed2008-08-20 09:07:45 +02003664 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003665 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003666 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003667
Ingo Molnardebccb32009-01-28 15:20:18 +01003668 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3669 apic->target_cpus());
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003670
Eric W. Biedermanec683072006-11-08 17:44:57 -08003671 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003672
Eric W. Biedermanec683072006-11-08 17:44:57 -08003673 msg.address_lo =
3674 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003675 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003676 HT_IRQ_LOW_VECTOR(cfg->vector) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003677 ((apic->irq_dest_mode == 0) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003678 HT_IRQ_LOW_DM_PHYSICAL :
3679 HT_IRQ_LOW_DM_LOGICAL) |
3680 HT_IRQ_LOW_RQEOI_EDGE |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003681 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003682 HT_IRQ_LOW_MT_FIXED :
3683 HT_IRQ_LOW_MT_ARBITRATED) |
3684 HT_IRQ_LOW_IRQ_MASKED;
3685
Eric W. Biedermanec683072006-11-08 17:44:57 -08003686 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003687
Ingo Molnara460e742006-10-17 00:10:03 -07003688 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3689 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003690
3691 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003692 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003693 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003694}
3695#endif /* CONFIG_HT_IRQ */
3696
Nick Piggin03b48632009-01-20 04:36:04 +01003697#ifdef CONFIG_X86_UV
Dean Nelson4173a0e2008-10-02 12:18:21 -05003698/*
3699 * Re-target the irq to the specified CPU and enable the specified MMR located
3700 * on the specified blade to allow the sending of MSIs to the specified CPU.
3701 */
3702int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3703 unsigned long mmr_offset)
3704{
Mike Travis22f65d32008-12-16 17:33:56 -08003705 const struct cpumask *eligible_cpu = cpumask_of(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003706 struct irq_cfg *cfg;
3707 int mmr_pnode;
3708 unsigned long mmr_value;
3709 struct uv_IO_APIC_route_entry *entry;
3710 unsigned long flags;
3711 int err;
3712
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003713 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3714
Yinghai Lu3145e942008-12-05 18:58:34 -08003715 cfg = irq_cfg(irq);
3716
Mike Travise7986732008-12-16 17:33:52 -08003717 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003718 if (err != 0)
3719 return err;
3720
3721 spin_lock_irqsave(&vector_lock, flags);
3722 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3723 irq_name);
3724 spin_unlock_irqrestore(&vector_lock, flags);
3725
Dean Nelson4173a0e2008-10-02 12:18:21 -05003726 mmr_value = 0;
3727 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003728 entry->vector = cfg->vector;
3729 entry->delivery_mode = apic->irq_delivery_mode;
3730 entry->dest_mode = apic->irq_dest_mode;
3731 entry->polarity = 0;
3732 entry->trigger = 0;
3733 entry->mask = 0;
3734 entry->dest = apic->cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003735
3736 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3737 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3738
3739 return irq;
3740}
3741
3742/*
3743 * Disable the specified MMR located on the specified blade so that MSIs are
3744 * longer allowed to be sent.
3745 */
3746void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3747{
3748 unsigned long mmr_value;
3749 struct uv_IO_APIC_route_entry *entry;
3750 int mmr_pnode;
3751
Cyrill Gorcunov1cbac972009-05-02 13:39:56 +04003752 BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3753
Dean Nelson4173a0e2008-10-02 12:18:21 -05003754 mmr_value = 0;
3755 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
Dean Nelson4173a0e2008-10-02 12:18:21 -05003756 entry->mask = 1;
3757
3758 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3759 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3760}
3761#endif /* CONFIG_X86_64 */
3762
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003763int __init io_apic_get_redir_entries (int ioapic)
3764{
3765 union IO_APIC_reg_01 reg_01;
3766 unsigned long flags;
3767
3768 spin_lock_irqsave(&ioapic_lock, flags);
3769 reg_01.raw = io_apic_read(ioapic, 1);
3770 spin_unlock_irqrestore(&ioapic_lock, flags);
3771
3772 return reg_01.bits.entries;
3773}
3774
Yinghai Lube5d5352008-12-05 18:58:33 -08003775void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003776{
Yinghai Lube5d5352008-12-05 18:58:33 -08003777 int nr = 0;
3778
Yinghai Lucc6c5002009-02-08 16:18:03 -08003779 nr = acpi_probe_gsi();
3780 if (nr > nr_irqs_gsi) {
Yinghai Lube5d5352008-12-05 18:58:33 -08003781 nr_irqs_gsi = nr;
Yinghai Lucc6c5002009-02-08 16:18:03 -08003782 } else {
3783 /* for acpi=off or acpi is not compiled in */
3784 int idx;
3785
3786 nr = 0;
3787 for (idx = 0; idx < nr_ioapics; idx++)
3788 nr += io_apic_get_redir_entries(idx) + 1;
3789
3790 if (nr > nr_irqs_gsi)
3791 nr_irqs_gsi = nr;
3792 }
3793
3794 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003795}
3796
Yinghai Lu4a046d12009-01-12 17:39:24 -08003797#ifdef CONFIG_SPARSE_IRQ
3798int __init arch_probe_nr_irqs(void)
3799{
3800 int nr;
3801
Yinghai Luf1ee5542009-02-08 16:18:03 -08003802 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3803 nr_irqs = NR_VECTORS * nr_cpu_ids;
Yinghai Lu4a046d12009-01-12 17:39:24 -08003804
Yinghai Luf1ee5542009-02-08 16:18:03 -08003805 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3806#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3807 /*
3808 * for MSI and HT dyn irq
3809 */
3810 nr += nr_irqs_gsi * 16;
3811#endif
3812 if (nr < nr_irqs)
Yinghai Lu4a046d12009-01-12 17:39:24 -08003813 nr_irqs = nr;
3814
3815 return 0;
3816}
3817#endif
3818
Yinghai Lue5198072009-05-15 13:05:16 -07003819static int __io_apic_set_pci_routing(struct device *dev, int irq,
3820 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003821{
3822 struct irq_desc *desc;
3823 struct irq_cfg *cfg;
3824 int node;
Yinghai Lue5198072009-05-15 13:05:16 -07003825 int ioapic, pin;
3826 int trigger, polarity;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003827
Yinghai Lue5198072009-05-15 13:05:16 -07003828 ioapic = irq_attr->ioapic;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003829 if (!IO_APIC_IRQ(irq)) {
3830 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3831 ioapic);
3832 return -EINVAL;
3833 }
3834
3835 if (dev)
3836 node = dev_to_node(dev);
3837 else
3838 node = cpu_to_node(boot_cpu_id);
3839
3840 desc = irq_to_desc_alloc_node(irq, node);
3841 if (!desc) {
3842 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3843 return 0;
3844 }
3845
Yinghai Lue5198072009-05-15 13:05:16 -07003846 pin = irq_attr->ioapic_pin;
3847 trigger = irq_attr->trigger;
3848 polarity = irq_attr->polarity;
3849
Yinghai Lu5ef21832009-05-06 10:08:50 -07003850 /*
3851 * IRQs < 16 are already in the irq_2_pin[] map
3852 */
3853 if (irq >= NR_IRQS_LEGACY) {
3854 cfg = desc->chip_data;
3855 add_pin_to_irq_node(cfg, node, ioapic, pin);
3856 }
3857
Yinghai Lue5198072009-05-15 13:05:16 -07003858 setup_IO_APIC_irq(ioapic, pin, irq, desc, trigger, polarity);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003859
3860 return 0;
3861}
3862
Yinghai Lue5198072009-05-15 13:05:16 -07003863int io_apic_set_pci_routing(struct device *dev, int irq,
3864 struct io_apic_irq_attr *irq_attr)
Yinghai Lu5ef21832009-05-06 10:08:50 -07003865{
Yinghai Lue5198072009-05-15 13:05:16 -07003866 int ioapic, pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003867 /*
3868 * Avoid pin reprogramming. PRTs typically include entries
3869 * with redundant pin->gsi mappings (but unique PCI devices);
3870 * we only program the IOAPIC on the first.
3871 */
Yinghai Lue5198072009-05-15 13:05:16 -07003872 ioapic = irq_attr->ioapic;
3873 pin = irq_attr->ioapic_pin;
Yinghai Lu5ef21832009-05-06 10:08:50 -07003874 if (test_bit(pin, mp_ioapic_routing[ioapic].pin_programmed)) {
3875 pr_debug("Pin %d-%d already programmed\n",
3876 mp_ioapics[ioapic].apicid, pin);
3877 return 0;
3878 }
3879 set_bit(pin, mp_ioapic_routing[ioapic].pin_programmed);
3880
Yinghai Lue5198072009-05-15 13:05:16 -07003881 return __io_apic_set_pci_routing(dev, irq, irq_attr);
Yinghai Lu5ef21832009-05-06 10:08:50 -07003882}
3883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003885 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 -------------------------------------------------------------------------- */
3887
Len Brown888ba6c2005-08-24 12:07:20 -04003888#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Ingo Molnar54168ed2008-08-20 09:07:45 +02003890#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003891int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892{
3893 union IO_APIC_reg_00 reg_00;
3894 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3895 physid_mask_t tmp;
3896 unsigned long flags;
3897 int i = 0;
3898
3899 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003900 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3901 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003903 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3905 * advantage of new APIC bus architecture.
3906 */
3907
3908 if (physids_empty(apic_id_map))
Ingo Molnard190cb82009-01-28 06:50:47 +01003909 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
3911 spin_lock_irqsave(&ioapic_lock, flags);
3912 reg_00.raw = io_apic_read(ioapic, 0);
3913 spin_unlock_irqrestore(&ioapic_lock, flags);
3914
3915 if (apic_id >= get_physical_broadcast()) {
3916 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3917 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3918 apic_id = reg_00.bits.ID;
3919 }
3920
3921 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003922 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 * 'stuck on smp_invalidate_needed IPI wait' messages.
3924 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003925 if (apic->check_apicid_used(apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 for (i = 0; i < get_physical_broadcast(); i++) {
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003928 if (!apic->check_apicid_used(apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 break;
3930 }
3931
3932 if (i == get_physical_broadcast())
3933 panic("Max apic_id exceeded!\n");
3934
3935 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3936 "trying %d\n", ioapic, apic_id, i);
3937
3938 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Ingo Molnar80587142009-01-28 06:50:47 +01003941 tmp = apic->apicid_to_cpu_present(apic_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 physids_or(apic_id_map, apic_id_map, tmp);
3943
3944 if (reg_00.bits.ID != apic_id) {
3945 reg_00.bits.ID = apic_id;
3946
3947 spin_lock_irqsave(&ioapic_lock, flags);
3948 io_apic_write(ioapic, 0, reg_00.raw);
3949 reg_00.raw = io_apic_read(ioapic, 0);
3950 spin_unlock_irqrestore(&ioapic_lock, flags);
3951
3952 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003953 if (reg_00.bits.ID != apic_id) {
3954 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3955 return -1;
3956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 }
3958
3959 apic_printk(APIC_VERBOSE, KERN_INFO
3960 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3961
3962 return apic_id;
3963}
Naga Chumbalkar58f892e2009-05-26 21:48:07 +00003964#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003966int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967{
3968 union IO_APIC_reg_01 reg_01;
3969 unsigned long flags;
3970
3971 spin_lock_irqsave(&ioapic_lock, flags);
3972 reg_01.raw = io_apic_read(ioapic, 1);
3973 spin_unlock_irqrestore(&ioapic_lock, flags);
3974
3975 return reg_01.bits.version;
3976}
3977
Shaohua Li61fd47e2007-11-17 01:05:28 -05003978int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3979{
3980 int i;
3981
3982 if (skip_ioapic_setup)
3983 return -1;
3984
3985 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05303986 if (mp_irqs[i].irqtype == mp_INT &&
3987 mp_irqs[i].srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003988 break;
3989 if (i >= mp_irq_entries)
3990 return -1;
3991
3992 *trigger = irq_trigger(i);
3993 *polarity = irq_polarity(i);
3994 return 0;
3995}
3996
Len Brown888ba6c2005-08-24 12:07:20 -04003997#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003998
Yinghai Lu497c9a12008-08-19 20:50:28 -07003999/*
4000 * This function currently is only a helper for the i386 smp boot process where
4001 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01004002 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07004003 */
4004#ifdef CONFIG_SMP
4005void __init setup_ioapic_dest(void)
4006{
Yinghai Lub9c61b702009-05-06 10:10:06 -07004007 int pin, ioapic = 0, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004008 struct irq_desc *desc;
Mike Travis22f65d32008-12-16 17:33:56 -08004009 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004010
4011 if (skip_ioapic_setup == 1)
4012 return;
4013
Yinghai Lub9c61b702009-05-06 10:10:06 -07004014#ifdef CONFIG_ACPI
4015 if (!acpi_disabled && acpi_ioapic) {
4016 ioapic = mp_find_ioapic(0);
4017 if (ioapic < 0)
4018 ioapic = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004019 }
Yinghai Lub9c61b702009-05-06 10:10:06 -07004020#endif
4021
4022 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4023 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4024 if (irq_entry == -1)
4025 continue;
4026 irq = pin_2_irq(irq_entry, ioapic, pin);
4027
4028 desc = irq_to_desc(irq);
4029
4030 /*
4031 * Honour affinities which have been set in early boot
4032 */
4033 if (desc->status &
4034 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
4035 mask = desc->affinity;
4036 else
4037 mask = apic->target_cpus();
4038
4039 if (intr_remapping_enabled)
4040 set_ir_ioapic_affinity_irq_desc(desc, mask);
4041 else
4042 set_ioapic_affinity_irq_desc(desc, mask);
4043 }
4044
Yinghai Lu497c9a12008-08-19 20:50:28 -07004045}
4046#endif
4047
Ingo Molnar54168ed2008-08-20 09:07:45 +02004048#define IOAPIC_RESOURCE_NAME_SIZE 11
4049
4050static struct resource *ioapic_resources;
4051
4052static struct resource * __init ioapic_setup_resources(void)
4053{
4054 unsigned long n;
4055 struct resource *res;
4056 char *mem;
4057 int i;
4058
4059 if (nr_ioapics <= 0)
4060 return NULL;
4061
4062 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4063 n *= nr_ioapics;
4064
4065 mem = alloc_bootmem(n);
4066 res = (void *)mem;
4067
4068 if (mem != NULL) {
4069 mem += sizeof(struct resource) * nr_ioapics;
4070
4071 for (i = 0; i < nr_ioapics; i++) {
4072 res[i].name = mem;
4073 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4074 sprintf(mem, "IOAPIC %u", i);
4075 mem += IOAPIC_RESOURCE_NAME_SIZE;
4076 }
4077 }
4078
4079 ioapic_resources = res;
4080
4081 return res;
4082}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004083
Yinghai Luf3294a32008-06-27 01:41:56 -07004084void __init ioapic_init_mappings(void)
4085{
4086 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004087 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004088 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004089
Ingo Molnar54168ed2008-08-20 09:07:45 +02004090 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07004091 for (i = 0; i < nr_ioapics; i++) {
4092 if (smp_found_config) {
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05304093 ioapic_phys = mp_ioapics[i].apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004094#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004095 if (!ioapic_phys) {
4096 printk(KERN_ERR
4097 "WARNING: bogus zero IO-APIC "
4098 "address found in MPTABLE, "
4099 "disabling IO/APIC support!\n");
4100 smp_found_config = 0;
4101 skip_ioapic_setup = 1;
4102 goto fake_ioapic_page;
4103 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004104#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004105 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004106#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004107fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004108#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004109 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004110 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004111 ioapic_phys = __pa(ioapic_phys);
4112 }
4113 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004114 apic_printk(APIC_VERBOSE,
4115 "mapped IOAPIC to %08lx (%08lx)\n",
4116 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004117 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004118
Ingo Molnar54168ed2008-08-20 09:07:45 +02004119 if (ioapic_res != NULL) {
4120 ioapic_res->start = ioapic_phys;
4121 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4122 ioapic_res++;
4123 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004124 }
4125}
4126
Ingo Molnar54168ed2008-08-20 09:07:45 +02004127static int __init ioapic_insert_resources(void)
4128{
4129 int i;
4130 struct resource *r = ioapic_resources;
4131
4132 if (!r) {
Bartlomiej Zolnierkiewicz04c93ce2009-03-20 21:02:55 +01004133 if (nr_ioapics > 0) {
4134 printk(KERN_ERR
4135 "IO APIC resources couldn't be allocated.\n");
4136 return -1;
4137 }
4138 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004139 }
4140
4141 for (i = 0; i < nr_ioapics; i++) {
4142 insert_resource(&iomem_resource, r);
4143 r++;
4144 }
4145
4146 return 0;
4147}
4148
4149/* Insert the IO APIC resources after PCI initialization has occured to handle
4150 * IO APICS that are mapped in on a BAR in PCI space. */
4151late_initcall(ioapic_insert_resources);