blob: 57d60c741e377fd88006ec704a4d84ac51f56a4e [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>
Dean Nelson4173a0e2008-10-02 12:18:21 -050062#include <asm/uv/uv_hub.h>
63#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +010065#include <asm/genapic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010067#define __apicdebuginit(type) static type __init
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020070 * Is the SiS APIC rmw bug present ?
71 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73int sis_apic_bug = -1;
74
Yinghai Luefa25592008-08-19 20:50:36 -070075static DEFINE_SPINLOCK(ioapic_lock);
76static DEFINE_SPINLOCK(vector_lock);
77
Yinghai Luefa25592008-08-19 20:50:36 -070078/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * # of IRQ routing registers
80 */
81int nr_ioapic_registers[MAX_IO_APICS];
82
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040083/* I/O APIC entries */
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +053084struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040085int nr_ioapics;
86
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040087/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +053088struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040089
90/* # of MP IRQ source entries */
91int mp_irq_entries;
92
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040093#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
94int mp_bus_id_to_type[MAX_MP_BUSSES];
95#endif
96
97DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
98
Yinghai Luefa25592008-08-19 20:50:36 -070099int skip_ioapic_setup;
100
Ingo Molnar54168ed2008-08-20 09:07:45 +0200101static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700102{
103 /* disable IO-APIC */
104 disable_ioapic_setup();
105 return 0;
106}
107early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200108
Yinghai Lu0f978f42008-08-19 20:50:26 -0700109struct irq_pin_list;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200110
111/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 * This is performance-critical, we want to do it O(1)
113 *
114 * the indexing order of this array favors 1:1 mappings
115 * between pins and IRQs.
116 */
117
Yinghai Lu0f978f42008-08-19 20:50:26 -0700118struct irq_pin_list {
119 int apic, pin;
120 struct irq_pin_list *next;
121};
Yinghai Lu301e6192008-08-19 20:50:02 -0700122
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800123static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700124{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800125 struct irq_pin_list *pin;
126 int node;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700127
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800128 node = cpu_to_node(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700129
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800130 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700131
Yinghai Lu0f978f42008-08-19 20:50:26 -0700132 return pin;
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800135struct irq_cfg {
136 struct irq_pin_list *irq_2_pin;
Mike Travis22f65d32008-12-16 17:33:56 -0800137 cpumask_var_t domain;
138 cpumask_var_t old_domain;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800139 unsigned move_cleanup_count;
140 u8 vector;
141 u8 move_in_progress : 1;
Yinghai Lu48a1b102008-12-11 00:15:01 -0800142#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
143 u8 move_desc_pending : 1;
144#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800145};
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;
176 int i;
177
178 cfg = irq_cfgx;
179 count = ARRAY_SIZE(irq_cfgx);
180
181 for (i = 0; i < count; i++) {
182 desc = irq_to_desc(i);
183 desc->chip_data = &cfg[i];
Mike Travis22f65d32008-12-16 17:33:56 -0800184 alloc_bootmem_cpumask_var(&cfg[i].domain);
185 alloc_bootmem_cpumask_var(&cfg[i].old_domain);
186 if (i < NR_IRQS_LEGACY)
187 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800188 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800189
190 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800191}
192
193#ifdef CONFIG_SPARSE_IRQ
194static struct irq_cfg *irq_cfg(unsigned int irq)
195{
196 struct irq_cfg *cfg = NULL;
197 struct irq_desc *desc;
198
199 desc = irq_to_desc(irq);
200 if (desc)
201 cfg = desc->chip_data;
202
203 return cfg;
204}
205
206static struct irq_cfg *get_one_free_irq_cfg(int cpu)
207{
208 struct irq_cfg *cfg;
209 int node;
210
211 node = cpu_to_node(cpu);
212
213 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800214 if (cfg) {
Mike Travis80855f72008-12-31 18:08:47 -0800215 if (!alloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800216 kfree(cfg);
217 cfg = NULL;
Mike Travis80855f72008-12-31 18:08:47 -0800218 } else if (!alloc_cpumask_var_node(&cfg->old_domain,
219 GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800220 free_cpumask_var(cfg->domain);
221 kfree(cfg);
222 cfg = NULL;
223 } else {
224 cpumask_clear(cfg->domain);
225 cpumask_clear(cfg->old_domain);
226 }
227 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800228
229 return cfg;
230}
231
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800232int arch_init_chip_data(struct irq_desc *desc, int cpu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800233{
234 struct irq_cfg *cfg;
235
236 cfg = desc->chip_data;
237 if (!cfg) {
238 desc->chip_data = get_one_free_irq_cfg(cpu);
239 if (!desc->chip_data) {
240 printk(KERN_ERR "can not alloc irq_cfg\n");
241 BUG_ON(1);
242 }
243 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800244
245 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800246}
247
Yinghai Lu48a1b102008-12-11 00:15:01 -0800248#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
249
250static void
251init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int cpu)
252{
253 struct irq_pin_list *old_entry, *head, *tail, *entry;
254
255 cfg->irq_2_pin = NULL;
256 old_entry = old_cfg->irq_2_pin;
257 if (!old_entry)
258 return;
259
260 entry = get_one_free_irq_2_pin(cpu);
261 if (!entry)
262 return;
263
264 entry->apic = old_entry->apic;
265 entry->pin = old_entry->pin;
266 head = entry;
267 tail = entry;
268 old_entry = old_entry->next;
269 while (old_entry) {
270 entry = get_one_free_irq_2_pin(cpu);
271 if (!entry) {
272 entry = head;
273 while (entry) {
274 head = entry->next;
275 kfree(entry);
276 entry = head;
277 }
278 /* still use the old one */
279 return;
280 }
281 entry->apic = old_entry->apic;
282 entry->pin = old_entry->pin;
283 tail->next = entry;
284 tail = entry;
285 old_entry = old_entry->next;
286 }
287
288 tail->next = NULL;
289 cfg->irq_2_pin = head;
290}
291
292static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
293{
294 struct irq_pin_list *entry, *next;
295
296 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
297 return;
298
299 entry = old_cfg->irq_2_pin;
300
301 while (entry) {
302 next = entry->next;
303 kfree(entry);
304 entry = next;
305 }
306 old_cfg->irq_2_pin = NULL;
307}
308
309void arch_init_copy_chip_data(struct irq_desc *old_desc,
310 struct irq_desc *desc, int cpu)
311{
312 struct irq_cfg *cfg;
313 struct irq_cfg *old_cfg;
314
315 cfg = get_one_free_irq_cfg(cpu);
316
317 if (!cfg)
318 return;
319
320 desc->chip_data = cfg;
321
322 old_cfg = old_desc->chip_data;
323
324 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
325
326 init_copy_irq_2_pin(old_cfg, cfg, cpu);
327}
328
329static void free_irq_cfg(struct irq_cfg *old_cfg)
330{
331 kfree(old_cfg);
332}
333
334void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
335{
336 struct irq_cfg *old_cfg, *cfg;
337
338 old_cfg = old_desc->chip_data;
339 cfg = desc->chip_data;
340
341 if (old_cfg == cfg)
342 return;
343
344 if (old_cfg) {
345 free_irq_2_pin(old_cfg, cfg);
346 free_irq_cfg(old_cfg);
347 old_desc->chip_data = NULL;
348 }
349}
350
Ingo Molnard733e002008-12-17 13:35:51 +0100351static void
352set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800353{
354 struct irq_cfg *cfg = desc->chip_data;
355
356 if (!cfg->move_in_progress) {
357 /* it means that domain is not changed */
Mike Travis7f7ace02009-01-10 21:58:08 -0800358 if (!cpumask_intersects(desc->affinity, mask))
Yinghai Lu48a1b102008-12-11 00:15:01 -0800359 cfg->move_desc_pending = 1;
360 }
361}
362#endif
363
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800364#else
365static struct irq_cfg *irq_cfg(unsigned int irq)
366{
367 return irq < nr_irqs ? irq_cfgx + irq : NULL;
368}
369
370#endif
371
Yinghai Lu48a1b102008-12-11 00:15:01 -0800372#ifndef CONFIG_NUMA_MIGRATE_IRQ_DESC
Mike Travise7986732008-12-16 17:33:52 -0800373static inline void
374set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800375{
376}
Yinghai Lu48a1b102008-12-11 00:15:01 -0800377#endif
Yinghai Lu3145e942008-12-05 18:58:34 -0800378
Linus Torvalds130fe052006-11-01 09:11:00 -0800379struct io_apic {
380 unsigned int index;
381 unsigned int unused[3];
382 unsigned int data;
383};
384
385static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
386{
387 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +0530388 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800389}
390
391static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
392{
393 struct io_apic __iomem *io_apic = io_apic_base(apic);
394 writel(reg, &io_apic->index);
395 return readl(&io_apic->data);
396}
397
398static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
399{
400 struct io_apic __iomem *io_apic = io_apic_base(apic);
401 writel(reg, &io_apic->index);
402 writel(value, &io_apic->data);
403}
404
405/*
406 * Re-write a value: to be used for read-modify-write
407 * cycles where the read already set up the index register.
408 *
409 * Older SiS APIC requires we rewrite the index register
410 */
411static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
412{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200413 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200414
415 if (sis_apic_bug)
416 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800417 writel(value, &io_apic->data);
418}
419
Yinghai Lu3145e942008-12-05 18:58:34 -0800420static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700421{
422 struct irq_pin_list *entry;
423 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700424
425 spin_lock_irqsave(&ioapic_lock, flags);
426 entry = cfg->irq_2_pin;
427 for (;;) {
428 unsigned int reg;
429 int pin;
430
431 if (!entry)
432 break;
433 pin = entry->pin;
434 reg = io_apic_read(entry->apic, 0x10 + pin*2);
435 /* Is the remote IRR bit set? */
436 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
437 spin_unlock_irqrestore(&ioapic_lock, flags);
438 return true;
439 }
440 if (!entry->next)
441 break;
442 entry = entry->next;
443 }
444 spin_unlock_irqrestore(&ioapic_lock, flags);
445
446 return false;
447}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700448
Andi Kleencf4c6a22006-09-26 10:52:30 +0200449union entry_union {
450 struct { u32 w1, w2; };
451 struct IO_APIC_route_entry entry;
452};
453
454static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
455{
456 union entry_union eu;
457 unsigned long flags;
458 spin_lock_irqsave(&ioapic_lock, flags);
459 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
460 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
461 spin_unlock_irqrestore(&ioapic_lock, flags);
462 return eu.entry;
463}
464
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800465/*
466 * When we write a new IO APIC routing entry, we need to write the high
467 * word first! If the mask bit in the low word is clear, we will enable
468 * the interrupt, and we need to make sure the entry is fully populated
469 * before that happens.
470 */
Andi Kleend15512f2006-12-07 02:14:07 +0100471static void
472__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
473{
474 union entry_union eu;
475 eu.entry = e;
476 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
477 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
478}
479
Andi Kleencf4c6a22006-09-26 10:52:30 +0200480static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
481{
482 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200483 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100484 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800485 spin_unlock_irqrestore(&ioapic_lock, flags);
486}
487
488/*
489 * When we mask an IO APIC routing entry, we need to write the low
490 * word first, in order to set the mask bit before we change the
491 * high bits!
492 */
493static void ioapic_mask_entry(int apic, int pin)
494{
495 unsigned long flags;
496 union entry_union eu = { .entry.mask = 1 };
497
498 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200499 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
500 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
501 spin_unlock_irqrestore(&ioapic_lock, flags);
502}
503
Yinghai Lu497c9a12008-08-19 20:50:28 -0700504#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -0800505static void send_cleanup_vector(struct irq_cfg *cfg)
506{
507 cpumask_var_t cleanup_mask;
508
509 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
510 unsigned int i;
511 cfg->move_cleanup_count = 0;
512 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
513 cfg->move_cleanup_count++;
514 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
Ingo Molnardac5f412009-01-28 15:42:24 +0100515 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
Mike Travis22f65d32008-12-16 17:33:56 -0800516 } else {
517 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
518 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
Ingo Molnardac5f412009-01-28 15:42:24 +0100519 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
Mike Travis22f65d32008-12-16 17:33:56 -0800520 free_cpumask_var(cleanup_mask);
521 }
522 cfg->move_in_progress = 0;
523}
524
Yinghai Lu3145e942008-12-05 18:58:34 -0800525static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700526{
527 int apic, pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700528 struct irq_pin_list *entry;
Yinghai Lu3145e942008-12-05 18:58:34 -0800529 u8 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700530
Yinghai Lu497c9a12008-08-19 20:50:28 -0700531 entry = cfg->irq_2_pin;
532 for (;;) {
533 unsigned int reg;
534
535 if (!entry)
536 break;
537
538 apic = entry->apic;
539 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200540#ifdef CONFIG_INTR_REMAP
541 /*
542 * With interrupt-remapping, destination information comes
543 * from interrupt-remapping table entry.
544 */
545 if (!irq_remapped(irq))
546 io_apic_write(apic, 0x11 + pin*2, dest);
547#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700548 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200549#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700550 reg = io_apic_read(apic, 0x10 + pin*2);
551 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
552 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200553 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700554 if (!entry->next)
555 break;
556 entry = entry->next;
557 }
558}
Yinghai Luefa25592008-08-19 20:50:36 -0700559
Mike Travise7986732008-12-16 17:33:52 -0800560static int
561assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
Yinghai Luefa25592008-08-19 20:50:36 -0700562
Mike Travis22f65d32008-12-16 17:33:56 -0800563/*
Ingo Molnardebccb32009-01-28 15:20:18 +0100564 * Either sets desc->affinity to a valid value, and returns
565 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
566 * leaves desc->affinity untouched.
Mike Travis22f65d32008-12-16 17:33:56 -0800567 */
568static unsigned int
569set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700570{
571 struct irq_cfg *cfg;
Yinghai Lu3145e942008-12-05 18:58:34 -0800572 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700573
Rusty Russell0de26522008-12-13 21:20:26 +1030574 if (!cpumask_intersects(mask, cpu_online_mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800575 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700576
Yinghai Lu3145e942008-12-05 18:58:34 -0800577 irq = desc->irq;
578 cfg = desc->chip_data;
579 if (assign_irq_vector(irq, cfg, mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800580 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700581
Mike Travis7f7ace02009-01-10 21:58:08 -0800582 cpumask_and(desc->affinity, cfg->domain, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -0800583 set_extra_move_desc(desc, mask);
Ingo Molnardebccb32009-01-28 15:20:18 +0100584
585 return apic->cpu_mask_to_apicid_and(desc->affinity, cpu_online_mask);
Mike Travis22f65d32008-12-16 17:33:56 -0800586}
Yinghai Lu3145e942008-12-05 18:58:34 -0800587
Mike Travis22f65d32008-12-16 17:33:56 -0800588static void
589set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700590{
591 struct irq_cfg *cfg;
592 unsigned long flags;
593 unsigned int dest;
Mike Travis22f65d32008-12-16 17:33:56 -0800594 unsigned int irq;
595
596 irq = desc->irq;
597 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700598
599 spin_lock_irqsave(&ioapic_lock, flags);
Mike Travis22f65d32008-12-16 17:33:56 -0800600 dest = set_desc_affinity(desc, mask);
601 if (dest != BAD_APICID) {
602 /* Only the high 8 bits are valid. */
603 dest = SET_APIC_LOGICAL_ID(dest);
604 __target_IO_APIC_irq(irq, dest, cfg);
605 }
Yinghai Lu497c9a12008-08-19 20:50:28 -0700606 spin_unlock_irqrestore(&ioapic_lock, flags);
607}
Yinghai Lu3145e942008-12-05 18:58:34 -0800608
Mike Travis22f65d32008-12-16 17:33:56 -0800609static void
610set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800611{
Yinghai Lu497c9a12008-08-19 20:50:28 -0700612 struct irq_desc *desc;
613
Yinghai Lu497c9a12008-08-19 20:50:28 -0700614 desc = irq_to_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800615
616 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700617}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700618#endif /* CONFIG_SMP */
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/*
621 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
622 * shared ISA-space IRQs, so we have to support them. We are super
623 * fast in the common case, and fast for shared ISA-space IRQs.
624 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800625static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700627 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Yinghai Lu0f978f42008-08-19 20:50:26 -0700629 entry = cfg->irq_2_pin;
630 if (!entry) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800631 entry = get_one_free_irq_2_pin(cpu);
632 if (!entry) {
633 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
634 apic, pin);
635 return;
636 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700637 cfg->irq_2_pin = entry;
638 entry->apic = apic;
639 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700640 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700642
643 while (entry->next) {
644 /* not again, please */
645 if (entry->apic == apic && entry->pin == pin)
646 return;
647
648 entry = entry->next;
649 }
650
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800651 entry->next = get_one_free_irq_2_pin(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700652 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 entry->apic = apic;
654 entry->pin = pin;
655}
656
657/*
658 * Reroute an IRQ to a different pin.
659 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800660static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 int oldapic, int oldpin,
662 int newapic, int newpin)
663{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700664 struct irq_pin_list *entry = cfg->irq_2_pin;
665 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Yinghai Lu0f978f42008-08-19 20:50:26 -0700667 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (entry->apic == oldapic && entry->pin == oldpin) {
669 entry->apic = newapic;
670 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700671 replaced = 1;
672 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700674 }
675 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700677
678 /* why? call replace before add? */
679 if (!replaced)
Yinghai Lu3145e942008-12-05 18:58:34 -0800680 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Yinghai Lu3145e942008-12-05 18:58:34 -0800683static inline void io_apic_modify_irq(struct irq_cfg *cfg,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400684 int mask_and, int mask_or,
685 void (*final)(struct irq_pin_list *entry))
686{
687 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400688 struct irq_pin_list *entry;
689
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400690 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
691 unsigned int reg;
692 pin = entry->pin;
693 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
694 reg &= mask_and;
695 reg |= mask_or;
696 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
697 if (final)
698 final(entry);
699 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700700}
701
Yinghai Lu3145e942008-12-05 18:58:34 -0800702static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400703{
Yinghai Lu3145e942008-12-05 18:58:34 -0800704 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400705}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700706
707#ifdef CONFIG_X86_64
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530708static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700709{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400710 /*
711 * Synchronize the IO-APIC and the CPU by doing
712 * a dummy read from the IO-APIC
713 */
714 struct io_apic __iomem *io_apic;
715 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700716 readl(&io_apic->data);
717}
718
Yinghai Lu3145e942008-12-05 18:58:34 -0800719static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400720{
Yinghai Lu3145e942008-12-05 18:58:34 -0800721 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400722}
723#else /* CONFIG_X86_32 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800724static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400725{
Yinghai Lu3145e942008-12-05 18:58:34 -0800726 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400727}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700728
Yinghai Lu3145e942008-12-05 18:58:34 -0800729static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400730{
Yinghai Lu3145e942008-12-05 18:58:34 -0800731 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400732 IO_APIC_REDIR_MASKED, NULL);
733}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700734
Yinghai Lu3145e942008-12-05 18:58:34 -0800735static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400736{
Yinghai Lu3145e942008-12-05 18:58:34 -0800737 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400738 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
739}
740#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700741
Yinghai Lu3145e942008-12-05 18:58:34 -0800742static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Yinghai Lu3145e942008-12-05 18:58:34 -0800744 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 unsigned long flags;
746
Yinghai Lu3145e942008-12-05 18:58:34 -0800747 BUG_ON(!cfg);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800750 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 spin_unlock_irqrestore(&ioapic_lock, flags);
752}
753
Yinghai Lu3145e942008-12-05 18:58:34 -0800754static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Yinghai Lu3145e942008-12-05 18:58:34 -0800756 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 unsigned long flags;
758
759 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800760 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 spin_unlock_irqrestore(&ioapic_lock, flags);
762}
763
Yinghai Lu3145e942008-12-05 18:58:34 -0800764static void mask_IO_APIC_irq(unsigned int irq)
765{
766 struct irq_desc *desc = irq_to_desc(irq);
767
768 mask_IO_APIC_irq_desc(desc);
769}
770static void unmask_IO_APIC_irq(unsigned int irq)
771{
772 struct irq_desc *desc = irq_to_desc(irq);
773
774 unmask_IO_APIC_irq_desc(desc);
775}
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
778{
779 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200782 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (entry.delivery_mode == dest_SMI)
784 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /*
786 * Disable it in the IO-APIC irq-routing table:
787 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800788 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Ingo Molnar54168ed2008-08-20 09:07:45 +0200791static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 int apic, pin;
794
795 for (apic = 0; apic < nr_ioapics; apic++)
796 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
797 clear_IO_APIC_pin(apic, pin);
798}
799
Ingo Molnar54168ed2008-08-20 09:07:45 +0200800#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801/*
802 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
803 * specific CPU-side IRQs.
804 */
805
806#define MAX_PIRQS 8
807static int pirq_entries [MAX_PIRQS];
808static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static int __init ioapic_pirq_setup(char *str)
811{
812 int i, max;
813 int ints[MAX_PIRQS+1];
814
815 get_options(str, ARRAY_SIZE(ints), ints);
816
817 for (i = 0; i < MAX_PIRQS; i++)
818 pirq_entries[i] = -1;
819
820 pirqs_enabled = 1;
821 apic_printk(APIC_VERBOSE, KERN_INFO
822 "PIRQ redirection, working around broken MP-BIOS.\n");
823 max = MAX_PIRQS;
824 if (ints[0] < MAX_PIRQS)
825 max = ints[0];
826
827 for (i = 0; i < max; i++) {
828 apic_printk(APIC_VERBOSE, KERN_DEBUG
829 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
830 /*
831 * PIRQs are mapped upside down, usually.
832 */
833 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
834 }
835 return 1;
836}
837
838__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200839#endif /* CONFIG_X86_32 */
840
841#ifdef CONFIG_INTR_REMAP
842/* I/O APIC RTE contents at the OS boot up */
843static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
844
845/*
846 * Saves and masks all the unmasked IO-APIC RTE's
847 */
848int save_mask_IO_APIC_setup(void)
849{
850 union IO_APIC_reg_01 reg_01;
851 unsigned long flags;
852 int apic, pin;
853
854 /*
855 * The number of IO-APIC IRQ registers (== #pins):
856 */
857 for (apic = 0; apic < nr_ioapics; apic++) {
858 spin_lock_irqsave(&ioapic_lock, flags);
859 reg_01.raw = io_apic_read(apic, 1);
860 spin_unlock_irqrestore(&ioapic_lock, flags);
861 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
862 }
863
864 for (apic = 0; apic < nr_ioapics; apic++) {
865 early_ioapic_entries[apic] =
866 kzalloc(sizeof(struct IO_APIC_route_entry) *
867 nr_ioapic_registers[apic], GFP_KERNEL);
868 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400869 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200870 }
871
872 for (apic = 0; apic < nr_ioapics; apic++)
873 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
874 struct IO_APIC_route_entry entry;
875
876 entry = early_ioapic_entries[apic][pin] =
877 ioapic_read_entry(apic, pin);
878 if (!entry.mask) {
879 entry.mask = 1;
880 ioapic_write_entry(apic, pin, entry);
881 }
882 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400883
Ingo Molnar54168ed2008-08-20 09:07:45 +0200884 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400885
886nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400887 while (apic >= 0)
888 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400889 memset(early_ioapic_entries, 0,
890 ARRAY_SIZE(early_ioapic_entries));
891
892 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200893}
894
895void restore_IO_APIC_setup(void)
896{
897 int apic, pin;
898
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400899 for (apic = 0; apic < nr_ioapics; apic++) {
900 if (!early_ioapic_entries[apic])
901 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200902 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
903 ioapic_write_entry(apic, pin,
904 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400905 kfree(early_ioapic_entries[apic]);
906 early_ioapic_entries[apic] = NULL;
907 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200908}
909
910void reinit_intr_remapped_IO_APIC(int intr_remapping)
911{
912 /*
913 * for now plain restore of previous settings.
914 * TBD: In the case of OS enabling interrupt-remapping,
915 * IO-APIC RTE's need to be setup to point to interrupt-remapping
916 * table entries. for now, do a plain restore, and wait for
917 * the setup_IO_APIC_irqs() to do proper initialization.
918 */
919 restore_IO_APIC_setup();
920}
921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923/*
924 * Find the IRQ entry number of a certain pin.
925 */
926static int find_irq_entry(int apic, int pin, int type)
927{
928 int i;
929
930 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530931 if (mp_irqs[i].irqtype == type &&
932 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
933 mp_irqs[i].dstapic == MP_APIC_ALL) &&
934 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return i;
936
937 return -1;
938}
939
940/*
941 * Find the pin to which IRQ[irq] (ISA) is connected
942 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800943static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
945 int i;
946
947 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530948 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300950 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530951 (mp_irqs[i].irqtype == type) &&
952 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530954 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956 return -1;
957}
958
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800959static int __init find_isa_irq_apic(int irq, int type)
960{
961 int i;
962
963 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530964 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800965
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300966 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530967 (mp_irqs[i].irqtype == type) &&
968 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800969 break;
970 }
971 if (i < mp_irq_entries) {
972 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200973 for(apic = 0; apic < nr_ioapics; apic++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530974 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800975 return apic;
976 }
977 }
978
979 return -1;
980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/*
983 * Find a specific PCI IRQ entry.
984 * Not an __init, possibly needed by modules
985 */
986static int pin_2_irq(int idx, int apic, int pin);
987
988int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
989{
990 int apic, i, best_guess = -1;
991
Ingo Molnar54168ed2008-08-20 09:07:45 +0200992 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
993 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400994 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200995 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return -1;
997 }
998 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530999 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
1001 for (apic = 0; apic < nr_ioapics; apic++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301002 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1003 mp_irqs[i].dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 break;
1005
Alexey Starikovskiy47cab822008-03-20 14:54:30 +03001006 if (!test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301007 !mp_irqs[i].irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 (bus == lbus) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301009 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1010 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (!(apic || IO_APIC_IRQ(irq)))
1013 continue;
1014
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301015 if (pin == (mp_irqs[i].srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return irq;
1017 /*
1018 * Use the first all-but-pin matching entry as a
1019 * best-guess fuzzy result for broken mptables.
1020 */
1021 if (best_guess < 0)
1022 best_guess = irq;
1023 }
1024 }
1025 return best_guess;
1026}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001027
Alexey Dobriyan129f6942005-06-23 00:08:33 -07001028EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001030#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031/*
1032 * EISA Edge/Level control register, ELCR
1033 */
1034static int EISA_ELCR(unsigned int irq)
1035{
Yinghai Lu99d093d2008-12-05 18:58:32 -08001036 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 unsigned int port = 0x4d0 + (irq >> 3);
1038 return (inb(port) >> (irq & 7)) & 1;
1039 }
1040 apic_printk(APIC_VERBOSE, KERN_INFO
1041 "Broken MPtable reports ISA irq %d\n", irq);
1042 return 0;
1043}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001044
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001045#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001047/* ISA interrupts are always polarity zero edge triggered,
1048 * when listed as conforming in the MP table. */
1049
1050#define default_ISA_trigger(idx) (0)
1051#define default_ISA_polarity(idx) (0)
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/* EISA interrupts are always polarity zero and can be edge or level
1054 * trigger depending on the ELCR value. If an interrupt is listed as
1055 * EISA conforming in the MP table, that means its trigger type must
1056 * be read in from the ELCR */
1057
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301058#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001059#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061/* PCI interrupts are always polarity one level triggered,
1062 * when listed as conforming in the MP table. */
1063
1064#define default_PCI_trigger(idx) (1)
1065#define default_PCI_polarity(idx) (1)
1066
1067/* MCA interrupts are always polarity zero level triggered,
1068 * when listed as conforming in the MP table. */
1069
1070#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001071#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Shaohua Li61fd47e2007-11-17 01:05:28 -05001073static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301075 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 int polarity;
1077
1078 /*
1079 * Determine IRQ line polarity (high active or low active):
1080 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301081 switch (mp_irqs[idx].irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001083 case 0: /* conforms, ie. bus-type dependent polarity */
1084 if (test_bit(bus, mp_bus_not_pci))
1085 polarity = default_ISA_polarity(idx);
1086 else
1087 polarity = default_PCI_polarity(idx);
1088 break;
1089 case 1: /* high active */
1090 {
1091 polarity = 0;
1092 break;
1093 }
1094 case 2: /* reserved */
1095 {
1096 printk(KERN_WARNING "broken BIOS!!\n");
1097 polarity = 1;
1098 break;
1099 }
1100 case 3: /* low active */
1101 {
1102 polarity = 1;
1103 break;
1104 }
1105 default: /* invalid */
1106 {
1107 printk(KERN_WARNING "broken BIOS!!\n");
1108 polarity = 1;
1109 break;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112 return polarity;
1113}
1114
1115static int MPBIOS_trigger(int idx)
1116{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301117 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int trigger;
1119
1120 /*
1121 * Determine IRQ trigger mode (edge or level sensitive):
1122 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301123 switch ((mp_irqs[idx].irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001125 case 0: /* conforms, ie. bus-type dependent */
1126 if (test_bit(bus, mp_bus_not_pci))
1127 trigger = default_ISA_trigger(idx);
1128 else
1129 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001130#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001131 switch (mp_bus_id_to_type[bus]) {
1132 case MP_BUS_ISA: /* ISA pin */
1133 {
1134 /* set before the switch */
1135 break;
1136 }
1137 case MP_BUS_EISA: /* EISA pin */
1138 {
1139 trigger = default_EISA_trigger(idx);
1140 break;
1141 }
1142 case MP_BUS_PCI: /* PCI pin */
1143 {
1144 /* set before the switch */
1145 break;
1146 }
1147 case MP_BUS_MCA: /* MCA pin */
1148 {
1149 trigger = default_MCA_trigger(idx);
1150 break;
1151 }
1152 default:
1153 {
1154 printk(KERN_WARNING "broken BIOS!!\n");
1155 trigger = 1;
1156 break;
1157 }
1158 }
1159#endif
1160 break;
1161 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001162 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001163 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001164 break;
1165 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001166 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001167 {
1168 printk(KERN_WARNING "broken BIOS!!\n");
1169 trigger = 1;
1170 break;
1171 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001172 case 3: /* level */
1173 {
1174 trigger = 1;
1175 break;
1176 }
1177 default: /* invalid */
1178 {
1179 printk(KERN_WARNING "broken BIOS!!\n");
1180 trigger = 0;
1181 break;
1182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184 return trigger;
1185}
1186
1187static inline int irq_polarity(int idx)
1188{
1189 return MPBIOS_polarity(idx);
1190}
1191
1192static inline int irq_trigger(int idx)
1193{
1194 return MPBIOS_trigger(idx);
1195}
1196
Yinghai Luefa25592008-08-19 20:50:36 -07001197int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198static int pin_2_irq(int idx, int apic, int pin)
1199{
1200 int irq, i;
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301201 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 /*
1204 * Debugging check, we are in big trouble if this message pops up!
1205 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301206 if (mp_irqs[idx].dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1208
Ingo Molnar54168ed2008-08-20 09:07:45 +02001209 if (test_bit(bus, mp_bus_not_pci)) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301210 irq = mp_irqs[idx].srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001211 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001212 /*
1213 * PCI IRQs are mapped in order
1214 */
1215 i = irq = 0;
1216 while (i < apic)
1217 irq += nr_ioapic_registers[i++];
1218 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001219 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001220 * For MPS mode, so far only needed by ES7000 platform
1221 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001222 if (ioapic_renumber_irq)
1223 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225
Ingo Molnar54168ed2008-08-20 09:07:45 +02001226#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 /*
1228 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1229 */
1230 if ((pin >= 16) && (pin <= 23)) {
1231 if (pirq_entries[pin-16] != -1) {
1232 if (!pirq_entries[pin-16]) {
1233 apic_printk(APIC_VERBOSE, KERN_DEBUG
1234 "disabling PIRQ%d\n", pin-16);
1235 } else {
1236 irq = pirq_entries[pin-16];
1237 apic_printk(APIC_VERBOSE, KERN_DEBUG
1238 "using PIRQ%d -> IRQ %d\n",
1239 pin-16, irq);
1240 }
1241 }
1242 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001243#endif
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return irq;
1246}
1247
Yinghai Lu497c9a12008-08-19 20:50:28 -07001248void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001250 /* Used to the online set of cpus does not change
1251 * during assign_irq_vector.
1252 */
1253 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254}
1255
Yinghai Lu497c9a12008-08-19 20:50:28 -07001256void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001257{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001258 spin_unlock(&vector_lock);
1259}
1260
Mike Travise7986732008-12-16 17:33:52 -08001261static int
1262__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001263{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001264 /*
1265 * NOTE! The local APIC isn't very good at handling
1266 * multiple interrupts at the same interrupt level.
1267 * As the interrupt level is determined by taking the
1268 * vector number and shifting that right by 4, we
1269 * want to spread these out a bit so that they don't
1270 * all fall in the same interrupt level.
1271 *
1272 * Also, we've got to be careful not to trash gate
1273 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1274 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001275 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1276 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001277 int cpu, err;
1278 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001279
Ingo Molnar54168ed2008-08-20 09:07:45 +02001280 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1281 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001282
Mike Travis22f65d32008-12-16 17:33:56 -08001283 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1284 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001285
Ingo Molnar54168ed2008-08-20 09:07:45 +02001286 old_vector = cfg->vector;
1287 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001288 cpumask_and(tmp_mask, mask, cpu_online_mask);
1289 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1290 if (!cpumask_empty(tmp_mask)) {
1291 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001292 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001293 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001294 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001295
Mike Travise7986732008-12-16 17:33:52 -08001296 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001297 err = -ENOSPC;
1298 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001299 int new_cpu;
1300 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001301
Ingo Molnare2d40b12009-01-28 06:50:47 +01001302 apic->vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001303
Ingo Molnar54168ed2008-08-20 09:07:45 +02001304 vector = current_vector;
1305 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001306next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001307 vector += 8;
1308 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001309 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001310 offset = (offset + 1) % 8;
1311 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001312 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001313 if (unlikely(current_vector == vector))
1314 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001315
1316 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001317 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001318
Mike Travis22f65d32008-12-16 17:33:56 -08001319 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001320 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1321 goto next;
1322 /* Found one! */
1323 current_vector = vector;
1324 current_offset = offset;
1325 if (old_vector) {
1326 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001327 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001328 }
Mike Travis22f65d32008-12-16 17:33:56 -08001329 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001330 per_cpu(vector_irq, new_cpu)[vector] = irq;
1331 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001332 cpumask_copy(cfg->domain, tmp_mask);
1333 err = 0;
1334 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001335 }
Mike Travis22f65d32008-12-16 17:33:56 -08001336 free_cpumask_var(tmp_mask);
1337 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001338}
1339
Mike Travise7986732008-12-16 17:33:52 -08001340static int
1341assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001342{
1343 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001344 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001345
1346 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001347 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001348 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001349 return err;
1350}
1351
Yinghai Lu3145e942008-12-05 18:58:34 -08001352static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001353{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001354 int cpu, vector;
1355
Yinghai Lu497c9a12008-08-19 20:50:28 -07001356 BUG_ON(!cfg->vector);
1357
1358 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001359 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001360 per_cpu(vector_irq, cpu)[vector] = -1;
1361
1362 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001363 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001364
1365 if (likely(!cfg->move_in_progress))
1366 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001367 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001368 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1369 vector++) {
1370 if (per_cpu(vector_irq, cpu)[vector] != irq)
1371 continue;
1372 per_cpu(vector_irq, cpu)[vector] = -1;
1373 break;
1374 }
1375 }
1376 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001377}
1378
1379void __setup_vector_irq(int cpu)
1380{
1381 /* Initialize vector_irq on a new cpu */
1382 /* This function must be called with vector_lock held */
1383 int irq, vector;
1384 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001385 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001386
1387 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001388 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001389 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001390 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001391 continue;
1392 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001393 per_cpu(vector_irq, cpu)[vector] = irq;
1394 }
1395 /* Mark the free vectors */
1396 for (vector = 0; vector < NR_VECTORS; ++vector) {
1397 irq = per_cpu(vector_irq, cpu)[vector];
1398 if (irq < 0)
1399 continue;
1400
1401 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001402 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001403 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001404 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001405}
Glauber Costa3fde6902008-05-28 20:34:19 -07001406
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001407static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001408#ifdef CONFIG_INTR_REMAP
1409static struct irq_chip ir_ioapic_chip;
1410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Ingo Molnar54168ed2008-08-20 09:07:45 +02001412#define IOAPIC_AUTO -1
1413#define IOAPIC_EDGE 0
1414#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001416#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001417static inline int IO_APIC_irq_trigger(int irq)
1418{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001419 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001420
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001421 for (apic = 0; apic < nr_ioapics; apic++) {
1422 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1423 idx = find_irq_entry(apic, pin, mp_INT);
1424 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1425 return irq_trigger(idx);
1426 }
1427 }
1428 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001429 * nonexistent IRQs are edge default
1430 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001431 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001432}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001433#else
1434static inline int IO_APIC_irq_trigger(int irq)
1435{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001436 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001437}
1438#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001439
Yinghai Lu3145e942008-12-05 18:58:34 -08001440static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
Yinghai Lu199751d2008-08-19 20:50:27 -07001442
Jan Beulich6ebcc002006-06-26 13:56:46 +02001443 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001444 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001445 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001446 else
1447 desc->status &= ~IRQ_LEVEL;
1448
Ingo Molnar54168ed2008-08-20 09:07:45 +02001449#ifdef CONFIG_INTR_REMAP
1450 if (irq_remapped(irq)) {
1451 desc->status |= IRQ_MOVE_PCNTXT;
1452 if (trigger)
1453 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1454 handle_fasteoi_irq,
1455 "fasteoi");
1456 else
1457 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1458 handle_edge_irq, "edge");
1459 return;
1460 }
1461#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001462 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1463 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001464 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001465 handle_fasteoi_irq,
1466 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001467 else
Ingo Molnara460e742006-10-17 00:10:03 -07001468 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001469 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001470}
1471
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001472static int setup_ioapic_entry(int apic_id, int irq,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001473 struct IO_APIC_route_entry *entry,
1474 unsigned int destination, int trigger,
1475 int polarity, int vector)
1476{
1477 /*
1478 * add it to the IO-APIC irq-routing table:
1479 */
1480 memset(entry,0,sizeof(*entry));
1481
Ingo Molnar54168ed2008-08-20 09:07:45 +02001482#ifdef CONFIG_INTR_REMAP
1483 if (intr_remapping_enabled) {
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001484 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001485 struct irte irte;
1486 struct IR_IO_APIC_route_entry *ir_entry =
1487 (struct IR_IO_APIC_route_entry *) entry;
1488 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001489
Ingo Molnar54168ed2008-08-20 09:07:45 +02001490 if (!iommu)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001491 panic("No mapping iommu for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001492
1493 index = alloc_irte(iommu, irq, 1);
1494 if (index < 0)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001495 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001496
1497 memset(&irte, 0, sizeof(irte));
1498
1499 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001500 irte.dst_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001501 irte.trigger_mode = trigger;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001502 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001503 irte.vector = vector;
1504 irte.dest_id = IRTE_DEST(destination);
1505
1506 modify_irte(irq, &irte);
1507
1508 ir_entry->index2 = (index >> 15) & 0x1;
1509 ir_entry->zero = 0;
1510 ir_entry->format = 1;
1511 ir_entry->index = (index & 0x7fff);
1512 } else
1513#endif
1514 {
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001515 entry->delivery_mode = apic->irq_delivery_mode;
1516 entry->dest_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001517 entry->dest = destination;
1518 }
1519
1520 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001521 entry->trigger = trigger;
1522 entry->polarity = polarity;
1523 entry->vector = vector;
1524
1525 /* Mask level triggered irqs.
1526 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1527 */
1528 if (trigger)
1529 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001530 return 0;
1531}
1532
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001533static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001534 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001535{
1536 struct irq_cfg *cfg;
1537 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001538 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001539
1540 if (!IO_APIC_IRQ(irq))
1541 return;
1542
Yinghai Lu3145e942008-12-05 18:58:34 -08001543 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001544
Ingo Molnarfe402e12009-01-28 04:32:51 +01001545 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001546 return;
1547
Ingo Molnardebccb32009-01-28 15:20:18 +01001548 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07001549
1550 apic_printk(APIC_VERBOSE,KERN_DEBUG
1551 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1552 "IRQ %d Mode:%i Active:%i)\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001553 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001554 irq, trigger, polarity);
1555
1556
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001557 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
Mike Travis22f65d32008-12-16 17:33:56 -08001558 dest, trigger, polarity, cfg->vector)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001559 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001560 mp_ioapics[apic_id].apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001561 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001562 return;
1563 }
1564
Yinghai Lu3145e942008-12-05 18:58:34 -08001565 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001566 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001567 disable_8259A_irq(irq);
1568
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001569 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570}
1571
1572static void __init setup_IO_APIC_irqs(void)
1573{
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001574 int apic_id, pin, idx, irq;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001575 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001576 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001577 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001578 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1581
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001582 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
1583 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001585 idx = find_irq_entry(apic_id, pin, mp_INT);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001586 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001587 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001588 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001589 apic_printk(APIC_VERBOSE,
1590 KERN_DEBUG " %d-%d",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001591 mp_ioapics[apic_id].apicid, pin);
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001592 } else
1593 apic_printk(APIC_VERBOSE, " %d-%d",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001594 mp_ioapics[apic_id].apicid, pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001595 continue;
1596 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001597 if (notcon) {
1598 apic_printk(APIC_VERBOSE,
1599 " (apicid-pin) not connected\n");
1600 notcon = 0;
1601 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001602
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001603 irq = pin_2_irq(idx, apic_id, pin);
Ingo Molnar33a201f2009-01-28 07:17:26 +01001604
1605 /*
1606 * Skip the timer IRQ if there's a quirk handler
1607 * installed and if it returns 1:
1608 */
1609 if (apic->multi_timer_check &&
1610 apic->multi_timer_check(apic_id, irq))
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001611 continue;
Ingo Molnar33a201f2009-01-28 07:17:26 +01001612
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001613 desc = irq_to_desc_alloc_cpu(irq, cpu);
1614 if (!desc) {
1615 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1616 continue;
1617 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001618 cfg = desc->chip_data;
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001619 add_pin_to_irq_cpu(cfg, cpu, apic_id, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001620
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001621 setup_IO_APIC_irq(apic_id, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001622 irq_trigger(idx), irq_polarity(idx));
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001626 if (notcon)
1627 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001628 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001632 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001634static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001635 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Ingo Molnar54168ed2008-08-20 09:07:45 +02001639#ifdef CONFIG_INTR_REMAP
1640 if (intr_remapping_enabled)
1641 return;
1642#endif
1643
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001644 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
1646 /*
1647 * We use logical delivery to get the timer IRQ
1648 * to the first CPU.
1649 */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001650 entry.dest_mode = apic->irq_dest_mode;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001651 entry.mask = 1; /* mask IRQ now */
Ingo Molnardebccb32009-01-28 15:20:18 +01001652 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001653 entry.delivery_mode = apic->irq_delivery_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 entry.polarity = 0;
1655 entry.trigger = 0;
1656 entry.vector = vector;
1657
1658 /*
1659 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001660 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001662 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /*
1665 * Add it to the IO-APIC irq-routing table:
1666 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001667 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001670
1671__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672{
1673 int apic, i;
1674 union IO_APIC_reg_00 reg_00;
1675 union IO_APIC_reg_01 reg_01;
1676 union IO_APIC_reg_02 reg_02;
1677 union IO_APIC_reg_03 reg_03;
1678 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001679 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001680 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001681 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 if (apic_verbosity == APIC_QUIET)
1684 return;
1685
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001686 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 for (i = 0; i < nr_ioapics; i++)
1688 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +05301689 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 /*
1692 * We are a bit conservative about what we expect. We have to
1693 * know about every hardware change ASAP.
1694 */
1695 printk(KERN_INFO "testing the IO APIC.......................\n");
1696
1697 for (apic = 0; apic < nr_ioapics; apic++) {
1698
1699 spin_lock_irqsave(&ioapic_lock, flags);
1700 reg_00.raw = io_apic_read(apic, 0);
1701 reg_01.raw = io_apic_read(apic, 1);
1702 if (reg_01.bits.version >= 0x10)
1703 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001704 if (reg_01.bits.version >= 0x20)
1705 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 spin_unlock_irqrestore(&ioapic_lock, flags);
1707
Ingo Molnar54168ed2008-08-20 09:07:45 +02001708 printk("\n");
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +05301709 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1711 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1712 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1713 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Ingo Molnar54168ed2008-08-20 09:07:45 +02001715 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1719 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 /*
1722 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1723 * but the value of reg_02 is read as the previous read register
1724 * value, so ignore it if reg_02 == reg_01.
1725 */
1726 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1727 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1728 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
1730
1731 /*
1732 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1733 * or reg_03, but the value of reg_0[23] is read as the previous read
1734 * register value, so ignore it if reg_03 == reg_0[12].
1735 */
1736 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1737 reg_03.raw != reg_01.raw) {
1738 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1739 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
1742 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1743
Yinghai Lud83e94a2008-08-19 20:50:33 -07001744 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1745 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 for (i = 0; i <= reg_01.bits.entries; i++) {
1748 struct IO_APIC_route_entry entry;
1749
Andi Kleencf4c6a22006-09-26 10:52:30 +02001750 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Ingo Molnar54168ed2008-08-20 09:07:45 +02001752 printk(KERN_DEBUG " %02x %03X ",
1753 i,
1754 entry.dest
1755 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1758 entry.mask,
1759 entry.trigger,
1760 entry.irr,
1761 entry.polarity,
1762 entry.delivery_status,
1763 entry.dest_mode,
1764 entry.delivery_mode,
1765 entry.vector
1766 );
1767 }
1768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001770 for_each_irq_desc(irq, desc) {
1771 struct irq_pin_list *entry;
1772
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001773 cfg = desc->chip_data;
1774 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001775 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001777 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 for (;;) {
1779 printk("-> %d:%d", entry->apic, entry->pin);
1780 if (!entry->next)
1781 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001782 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
1784 printk("\n");
1785 }
1786
1787 printk(KERN_INFO ".................................... done.\n");
1788
1789 return;
1790}
1791
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001792__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 unsigned int v;
1795 int i, j;
1796
1797 if (apic_verbosity == APIC_QUIET)
1798 return;
1799
1800 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1801 for (i = 0; i < 8; i++) {
1802 v = apic_read(base + i*0x10);
1803 for (j = 0; j < 32; j++) {
1804 if (v & (1<<j))
1805 printk("1");
1806 else
1807 printk("0");
1808 }
1809 printk("\n");
1810 }
1811}
1812
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001813__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001816 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 if (apic_verbosity == APIC_QUIET)
1819 return;
1820
1821 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1822 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001823 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001824 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 v = apic_read(APIC_LVR);
1826 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1827 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001828 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 v = apic_read(APIC_TASKPRI);
1831 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1832
Ingo Molnar54168ed2008-08-20 09:07:45 +02001833 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001834 if (!APIC_XAPIC(ver)) {
1835 v = apic_read(APIC_ARBPRI);
1836 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1837 v & APIC_ARBPRI_MASK);
1838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 v = apic_read(APIC_PROCPRI);
1840 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1841 }
1842
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001843 /*
1844 * Remote read supported only in the 82489DX and local APIC for
1845 * Pentium processors.
1846 */
1847 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1848 v = apic_read(APIC_RRR);
1849 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1850 }
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 v = apic_read(APIC_LDR);
1853 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001854 if (!x2apic_enabled()) {
1855 v = apic_read(APIC_DFR);
1856 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 v = apic_read(APIC_SPIV);
1859 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1860
1861 printk(KERN_DEBUG "... APIC ISR field:\n");
1862 print_APIC_bitfield(APIC_ISR);
1863 printk(KERN_DEBUG "... APIC TMR field:\n");
1864 print_APIC_bitfield(APIC_TMR);
1865 printk(KERN_DEBUG "... APIC IRR field:\n");
1866 print_APIC_bitfield(APIC_IRR);
1867
Ingo Molnar54168ed2008-08-20 09:07:45 +02001868 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1869 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001871
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 v = apic_read(APIC_ESR);
1873 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1874 }
1875
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001876 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001877 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1878 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 v = apic_read(APIC_LVTT);
1881 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1882
1883 if (maxlvt > 3) { /* PC is LVT#4. */
1884 v = apic_read(APIC_LVTPC);
1885 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1886 }
1887 v = apic_read(APIC_LVT0);
1888 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1889 v = apic_read(APIC_LVT1);
1890 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1891
1892 if (maxlvt > 2) { /* ERR is LVT#3. */
1893 v = apic_read(APIC_LVTERR);
1894 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1895 }
1896
1897 v = apic_read(APIC_TMICT);
1898 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1899 v = apic_read(APIC_TMCCT);
1900 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1901 v = apic_read(APIC_TDCR);
1902 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1903 printk("\n");
1904}
1905
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001906__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001908 int cpu;
1909
1910 preempt_disable();
1911 for_each_online_cpu(cpu)
1912 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1913 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914}
1915
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001916__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 unsigned int v;
1919 unsigned long flags;
1920
1921 if (apic_verbosity == APIC_QUIET)
1922 return;
1923
1924 printk(KERN_DEBUG "\nprinting PIC contents\n");
1925
1926 spin_lock_irqsave(&i8259A_lock, flags);
1927
1928 v = inb(0xa1) << 8 | inb(0x21);
1929 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1930
1931 v = inb(0xa0) << 8 | inb(0x20);
1932 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1933
Ingo Molnar54168ed2008-08-20 09:07:45 +02001934 outb(0x0b,0xa0);
1935 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001937 outb(0x0a,0xa0);
1938 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 spin_unlock_irqrestore(&i8259A_lock, flags);
1941
1942 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1943
1944 v = inb(0x4d1) << 8 | inb(0x4d0);
1945 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1946}
1947
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001948__apicdebuginit(int) print_all_ICs(void)
1949{
1950 print_PIC();
1951 print_all_local_APICs();
1952 print_IO_APIC();
1953
1954 return 0;
1955}
1956
1957fs_initcall(print_all_ICs);
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Yinghai Luefa25592008-08-19 20:50:36 -07001960/* Where if anywhere is the i8259 connect in external int mode */
1961static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1962
Ingo Molnar54168ed2008-08-20 09:07:45 +02001963void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001966 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001967 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 unsigned long flags;
1969
Ingo Molnar54168ed2008-08-20 09:07:45 +02001970#ifdef CONFIG_X86_32
1971 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (!pirqs_enabled)
1973 for (i = 0; i < MAX_PIRQS; i++)
1974 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001975#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
1977 /*
1978 * The number of IO-APIC IRQ registers (== #pins):
1979 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001980 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001982 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001984 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1985 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001986 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001987 int pin;
1988 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001989 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001990 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001991 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001992
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001993 /* If the interrupt line is enabled and in ExtInt mode
1994 * I have found the pin where the i8259 is connected.
1995 */
1996 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1997 ioapic_i8259.apic = apic;
1998 ioapic_i8259.pin = pin;
1999 goto found_i8259;
2000 }
2001 }
2002 }
2003 found_i8259:
2004 /* Look to see what if the MP table has reported the ExtINT */
2005 /* If we could not find the appropriate pin by looking at the ioapic
2006 * the i8259 probably is not connected the ioapic but give the
2007 * mptable a chance anyway.
2008 */
2009 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
2010 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
2011 /* Trust the MP table if nothing is setup in the hardware */
2012 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
2013 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
2014 ioapic_i8259.pin = i8259_pin;
2015 ioapic_i8259.apic = i8259_apic;
2016 }
2017 /* Complain if the MP table and the hardware disagree */
2018 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
2019 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
2020 {
2021 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
2023
2024 /*
2025 * Do not trust the IO-APIC being empty at bootup
2026 */
2027 clear_IO_APIC();
2028}
2029
2030/*
2031 * Not an __init, needed by the reboot code
2032 */
2033void disable_IO_APIC(void)
2034{
2035 /*
2036 * Clear the IO-APIC before rebooting:
2037 */
2038 clear_IO_APIC();
2039
Eric W. Biederman650927e2005-06-25 14:57:44 -07002040 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02002041 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07002042 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02002043 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07002044 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002045 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07002046 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07002047
2048 memset(&entry, 0, sizeof(entry));
2049 entry.mask = 0; /* Enabled */
2050 entry.trigger = 0; /* Edge */
2051 entry.irr = 0;
2052 entry.polarity = 0; /* High */
2053 entry.delivery_status = 0;
2054 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002055 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07002056 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002057 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07002058
2059 /*
2060 * Add it to the IO-APIC irq-routing table:
2061 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02002062 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07002063 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002064
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002065 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066}
2067
Ingo Molnar54168ed2008-08-20 09:07:45 +02002068#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069/*
2070 * function to set the IO-APIC physical IDs based on the
2071 * values stored in the MPC table.
2072 *
2073 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2074 */
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076static void __init setup_ioapic_ids_from_mpc(void)
2077{
2078 union IO_APIC_reg_00 reg_00;
2079 physid_mask_t phys_id_present_map;
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002080 int apic_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 int i;
2082 unsigned char old_id;
2083 unsigned long flags;
2084
Yinghai Lua4dbc342008-07-25 02:14:28 -07002085 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07002086 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002089 * Don't check I/O APIC IDs for xAPIC systems. They have
2090 * no meaning without the serial APIC bus.
2091 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002092 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2093 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002094 return;
2095 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 * This is broken; anything with a real cpu count has to
2097 * circumvent this idiocy regardless.
2098 */
Ingo Molnard190cb82009-01-28 06:50:47 +01002099 phys_id_present_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 /*
2102 * Set the IOAPIC ID to the value stored in the MPC table.
2103 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002104 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 /* Read the register 0 value */
2107 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002108 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002110
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002111 old_id = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002113 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002115 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2117 reg_00.bits.ID);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002118 mp_ioapics[apic_id].apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 }
2120
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 /*
2122 * Sanity check, is the ID really free? Every APIC in a
2123 * system must have a unique ID or we get lots of nice
2124 * 'stuck on smp_invalidate_needed IPI wait' messages.
2125 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01002126 if (apic->check_apicid_used(phys_id_present_map,
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002127 mp_ioapics[apic_id].apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002129 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 for (i = 0; i < get_physical_broadcast(); i++)
2131 if (!physid_isset(i, phys_id_present_map))
2132 break;
2133 if (i >= get_physical_broadcast())
2134 panic("Max APIC ID exceeded!\n");
2135 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2136 i);
2137 physid_set(i, phys_id_present_map);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002138 mp_ioapics[apic_id].apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 } else {
2140 physid_mask_t tmp;
Ingo Molnar80587142009-01-28 06:50:47 +01002141 tmp = apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 apic_printk(APIC_VERBOSE, "Setting %d in the "
2143 "phys_id_present_map\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002144 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2146 }
2147
2148
2149 /*
2150 * We need to adjust the IRQ routing table
2151 * if the ID changed.
2152 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002153 if (old_id != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05302155 if (mp_irqs[i].dstapic == old_id)
2156 mp_irqs[i].dstapic
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002157 = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 /*
2160 * Read the right value from the MPC table and
2161 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002162 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 apic_printk(APIC_VERBOSE, KERN_INFO
2164 "...changing IO-APIC physical APIC ID to %d ...",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002165 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002167 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002169 io_apic_write(apic_id, 0, reg_00.raw);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002170 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /*
2173 * Sanity check
2174 */
2175 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002176 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002178 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 printk("could not set ID!\n");
2180 else
2181 apic_printk(APIC_VERBOSE, " ok.\n");
2182 }
2183}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002186int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002187
2188static int __init notimercheck(char *s)
2189{
2190 no_timer_check = 1;
2191 return 1;
2192}
2193__setup("no_timer_check", notimercheck);
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195/*
2196 * There is a nasty bug in some older SMP boards, their mptable lies
2197 * about the timer IRQ. We do the following to work around the situation:
2198 *
2199 * - timer IRQ defaults to IO-APIC IRQ
2200 * - if this function detects that timer IRQs are defunct, then we fall
2201 * back to ISA timer IRQs
2202 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002203static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002206 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Zachary Amsden8542b202006-12-07 02:14:09 +01002208 if (no_timer_check)
2209 return 1;
2210
Ingo Molnar4aae0702007-12-18 18:05:58 +01002211 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 local_irq_enable();
2213 /* Let ten ticks pass... */
2214 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002215 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 /*
2218 * Expect a few ticks at least, to be sure some possible
2219 * glue logic does not lock up after one or two first
2220 * ticks in a non-ExtINT mode. Also the local APIC
2221 * might have cached one ExtINT interrupt. Finally, at
2222 * least one tick may be lost due to delays.
2223 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002224
2225 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002226 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return 0;
2229}
2230
2231/*
2232 * In the SMP+IOAPIC case it might happen that there are an unspecified
2233 * number of pending IRQ events unhandled. These cases are very rare,
2234 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2235 * better to do it this way as thus we do not have to be aware of
2236 * 'pending' interrupts in the IRQ path, except at this point.
2237 */
2238/*
2239 * Edge triggered needs to resend any interrupt
2240 * that was delayed but this is now handled in the device
2241 * independent code.
2242 */
2243
2244/*
2245 * Starting up a edge-triggered IO-APIC interrupt is
2246 * nasty - we need to make sure that we get the edge.
2247 * If it is already asserted for some reason, we need
2248 * return 1 to indicate that is was pending.
2249 *
2250 * This is not complete - we should be able to fake
2251 * an edge even if it isn't on the 8259A...
2252 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002253
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002254static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
2256 int was_pending = 0;
2257 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002258 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002261 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 disable_8259A_irq(irq);
2263 if (i8259A_irq_pending(irq))
2264 was_pending = 1;
2265 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002266 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002267 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 spin_unlock_irqrestore(&ioapic_lock, flags);
2269
2270 return was_pending;
2271}
2272
Ingo Molnar54168ed2008-08-20 09:07:45 +02002273#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002274static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002276
2277 struct irq_cfg *cfg = irq_cfg(irq);
2278 unsigned long flags;
2279
2280 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnardac5f412009-01-28 15:42:24 +01002281 apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002282 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002283
2284 return 1;
2285}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002286#else
2287static int ioapic_retrigger_irq(unsigned int irq)
2288{
Ingo Molnardac5f412009-01-28 15:42:24 +01002289 apic->send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002290
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002291 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002292}
2293#endif
2294
2295/*
2296 * Level and edge triggered IO-APIC interrupts need different handling,
2297 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2298 * handled with the level-triggered descriptor, but that one has slightly
2299 * more overhead. Level-triggered interrupts cannot be handled with the
2300 * edge-triggered handler, without risking IRQ storms and other ugly
2301 * races.
2302 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002303
Yinghai Lu497c9a12008-08-19 20:50:28 -07002304#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002305
2306#ifdef CONFIG_INTR_REMAP
2307static void ir_irq_migration(struct work_struct *work);
2308
2309static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2310
2311/*
2312 * Migrate the IO-APIC irq in the presence of intr-remapping.
2313 *
2314 * For edge triggered, irq migration is a simple atomic update(of vector
2315 * and cpu destination) of IRTE and flush the hardware cache.
2316 *
2317 * For level triggered, we need to modify the io-apic RTE aswell with the update
2318 * vector information, along with modifying IRTE with vector and destination.
2319 * So irq migration for level triggered is little bit more complex compared to
2320 * edge triggered migration. But the good news is, we use the same algorithm
2321 * for level triggered migration as we have today, only difference being,
2322 * we now initiate the irq migration from process context instead of the
2323 * interrupt context.
2324 *
2325 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2326 * suppression) to the IO-APIC, level triggered irq migration will also be
2327 * as simple as edge triggered migration and we can do the irq migration
2328 * with a simple atomic update to IO-APIC RTE.
2329 */
Mike Travise7986732008-12-16 17:33:52 -08002330static void
2331migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002332{
2333 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002334 struct irte irte;
2335 int modify_ioapic_rte;
2336 unsigned int dest;
2337 unsigned long flags;
Yinghai Lu3145e942008-12-05 18:58:34 -08002338 unsigned int irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002339
Mike Travis22f65d32008-12-16 17:33:56 -08002340 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002341 return;
2342
Yinghai Lu3145e942008-12-05 18:58:34 -08002343 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002344 if (get_irte(irq, &irte))
2345 return;
2346
Yinghai Lu3145e942008-12-05 18:58:34 -08002347 cfg = desc->chip_data;
2348 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002349 return;
2350
Yinghai Lu3145e942008-12-05 18:58:34 -08002351 set_extra_move_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002352
Ingo Molnardebccb32009-01-28 15:20:18 +01002353 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002354
Ingo Molnar54168ed2008-08-20 09:07:45 +02002355 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2356 if (modify_ioapic_rte) {
2357 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08002358 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002359 spin_unlock_irqrestore(&ioapic_lock, flags);
2360 }
2361
2362 irte.vector = cfg->vector;
2363 irte.dest_id = IRTE_DEST(dest);
2364
2365 /*
2366 * Modified the IRTE and flushes the Interrupt entry cache.
2367 */
2368 modify_irte(irq, &irte);
2369
Mike Travis22f65d32008-12-16 17:33:56 -08002370 if (cfg->move_in_progress)
2371 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002372
Mike Travis7f7ace02009-01-10 21:58:08 -08002373 cpumask_copy(desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002374}
2375
Yinghai Lu3145e942008-12-05 18:58:34 -08002376static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002377{
2378 int ret = -1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002379 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002380
Yinghai Lu3145e942008-12-05 18:58:34 -08002381 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002382
Yinghai Lu3145e942008-12-05 18:58:34 -08002383 if (io_apic_level_ack_pending(cfg)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002384 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002385 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002386 * vector information in the IO-APIC RTE and that will confuse
2387 * the EOI broadcast performed by cpu.
2388 * So, delay the irq migration to the next instance.
2389 */
2390 schedule_delayed_work(&ir_migration_work, 1);
2391 goto unmask;
2392 }
2393
2394 /* everthing is clear. we have right of way */
Mike Travis7f7ace02009-01-10 21:58:08 -08002395 migrate_ioapic_irq_desc(desc, desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002396
2397 ret = 0;
2398 desc->status &= ~IRQ_MOVE_PENDING;
Mike Travis7f7ace02009-01-10 21:58:08 -08002399 cpumask_clear(desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002400
2401unmask:
Yinghai Lu3145e942008-12-05 18:58:34 -08002402 unmask_IO_APIC_irq_desc(desc);
2403
Ingo Molnar54168ed2008-08-20 09:07:45 +02002404 return ret;
2405}
2406
2407static void ir_irq_migration(struct work_struct *work)
2408{
2409 unsigned int irq;
2410 struct irq_desc *desc;
2411
2412 for_each_irq_desc(irq, desc) {
2413 if (desc->status & IRQ_MOVE_PENDING) {
2414 unsigned long flags;
2415
2416 spin_lock_irqsave(&desc->lock, flags);
2417 if (!desc->chip->set_affinity ||
2418 !(desc->status & IRQ_MOVE_PENDING)) {
2419 desc->status &= ~IRQ_MOVE_PENDING;
2420 spin_unlock_irqrestore(&desc->lock, flags);
2421 continue;
2422 }
2423
Mike Travis7f7ace02009-01-10 21:58:08 -08002424 desc->chip->set_affinity(irq, desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002425 spin_unlock_irqrestore(&desc->lock, flags);
2426 }
2427 }
2428}
2429
2430/*
2431 * Migrates the IRQ destination in the process context.
2432 */
Rusty Russell968ea6d2008-12-13 21:55:51 +10302433static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2434 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002435{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002436 if (desc->status & IRQ_LEVEL) {
2437 desc->status |= IRQ_MOVE_PENDING;
Mike Travis7f7ace02009-01-10 21:58:08 -08002438 cpumask_copy(desc->pending_mask, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002439 migrate_irq_remapped_level_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002440 return;
2441 }
2442
Yinghai Lu3145e942008-12-05 18:58:34 -08002443 migrate_ioapic_irq_desc(desc, mask);
2444}
Rusty Russell0de26522008-12-13 21:20:26 +10302445static void set_ir_ioapic_affinity_irq(unsigned int irq,
2446 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002447{
2448 struct irq_desc *desc = irq_to_desc(irq);
2449
Yinghai Lu3145e942008-12-05 18:58:34 -08002450 set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002451}
2452#endif
2453
Yinghai Lu497c9a12008-08-19 20:50:28 -07002454asmlinkage void smp_irq_move_cleanup_interrupt(void)
2455{
2456 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002457
Yinghai Lu497c9a12008-08-19 20:50:28 -07002458 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002459 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002460 irq_enter();
2461
2462 me = smp_processor_id();
2463 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2464 unsigned int irq;
2465 struct irq_desc *desc;
2466 struct irq_cfg *cfg;
2467 irq = __get_cpu_var(vector_irq)[vector];
2468
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002469 if (irq == -1)
2470 continue;
2471
Yinghai Lu497c9a12008-08-19 20:50:28 -07002472 desc = irq_to_desc(irq);
2473 if (!desc)
2474 continue;
2475
2476 cfg = irq_cfg(irq);
2477 spin_lock(&desc->lock);
2478 if (!cfg->move_cleanup_count)
2479 goto unlock;
2480
Mike Travis22f65d32008-12-16 17:33:56 -08002481 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002482 goto unlock;
2483
2484 __get_cpu_var(vector_irq)[vector] = -1;
2485 cfg->move_cleanup_count--;
2486unlock:
2487 spin_unlock(&desc->lock);
2488 }
2489
2490 irq_exit();
2491}
2492
Yinghai Lu3145e942008-12-05 18:58:34 -08002493static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002494{
Yinghai Lu3145e942008-12-05 18:58:34 -08002495 struct irq_desc *desc = *descp;
2496 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002497 unsigned vector, me;
2498
Yinghai Lu48a1b102008-12-11 00:15:01 -08002499 if (likely(!cfg->move_in_progress)) {
2500#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2501 if (likely(!cfg->move_desc_pending))
2502 return;
2503
Yinghai Lub9098952008-12-19 13:48:34 -08002504 /* domain has not changed, but affinity did */
Yinghai Lu48a1b102008-12-11 00:15:01 -08002505 me = smp_processor_id();
Mike Travis7f7ace02009-01-10 21:58:08 -08002506 if (cpumask_test_cpu(me, desc->affinity)) {
Yinghai Lu48a1b102008-12-11 00:15:01 -08002507 *descp = desc = move_irq_desc(desc, me);
2508 /* get the new one */
2509 cfg = desc->chip_data;
2510 cfg->move_desc_pending = 0;
2511 }
2512#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002513 return;
Yinghai Lu48a1b102008-12-11 00:15:01 -08002514 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002515
2516 vector = ~get_irq_regs()->orig_ax;
2517 me = smp_processor_id();
Yinghai Lu48a1b102008-12-11 00:15:01 -08002518#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2519 *descp = desc = move_irq_desc(desc, me);
2520 /* get the new one */
2521 cfg = desc->chip_data;
2522#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002523
Mike Travis22f65d32008-12-16 17:33:56 -08002524 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2525 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002526}
2527#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002528static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002529#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002530
Ingo Molnar54168ed2008-08-20 09:07:45 +02002531#ifdef CONFIG_INTR_REMAP
2532static void ack_x2apic_level(unsigned int irq)
2533{
2534 ack_x2APIC_irq();
2535}
2536
2537static void ack_x2apic_edge(unsigned int irq)
2538{
2539 ack_x2APIC_irq();
2540}
Yinghai Lu3145e942008-12-05 18:58:34 -08002541
Ingo Molnar54168ed2008-08-20 09:07:45 +02002542#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002543
Yinghai Lu1d025192008-08-19 20:50:34 -07002544static void ack_apic_edge(unsigned int irq)
2545{
Yinghai Lu3145e942008-12-05 18:58:34 -08002546 struct irq_desc *desc = irq_to_desc(irq);
2547
2548 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002549 move_native_irq(irq);
2550 ack_APIC_irq();
2551}
2552
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002553atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002554
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002555static void ack_apic_level(unsigned int irq)
2556{
Yinghai Lu3145e942008-12-05 18:58:34 -08002557 struct irq_desc *desc = irq_to_desc(irq);
2558
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002559#ifdef CONFIG_X86_32
2560 unsigned long v;
2561 int i;
2562#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002563 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002564 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002565
Yinghai Lu3145e942008-12-05 18:58:34 -08002566 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002567#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002568 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002569 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002570 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002571 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002572 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002573#endif
2574
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002575#ifdef CONFIG_X86_32
2576 /*
2577 * It appears there is an erratum which affects at least version 0x11
2578 * of I/O APIC (that's the 82093AA and cores integrated into various
2579 * chipsets). Under certain conditions a level-triggered interrupt is
2580 * erroneously delivered as edge-triggered one but the respective IRR
2581 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2582 * message but it will never arrive and further interrupts are blocked
2583 * from the source. The exact reason is so far unknown, but the
2584 * phenomenon was observed when two consecutive interrupt requests
2585 * from a given source get delivered to the same CPU and the source is
2586 * temporarily disabled in between.
2587 *
2588 * A workaround is to simulate an EOI message manually. We achieve it
2589 * by setting the trigger mode to edge and then to level when the edge
2590 * trigger mode gets detected in the TMR of a local APIC for a
2591 * level-triggered interrupt. We mask the source for the time of the
2592 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2593 * The idea is from Manfred Spraul. --macro
2594 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002595 cfg = desc->chip_data;
2596 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002597
2598 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2599#endif
2600
Ingo Molnar54168ed2008-08-20 09:07:45 +02002601 /*
2602 * We must acknowledge the irq before we move it or the acknowledge will
2603 * not propagate properly.
2604 */
2605 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002606
Ingo Molnar54168ed2008-08-20 09:07:45 +02002607 /* Now we can move and renable the irq */
2608 if (unlikely(do_unmask_irq)) {
2609 /* Only migrate the irq if the ack has been received.
2610 *
2611 * On rare occasions the broadcast level triggered ack gets
2612 * delayed going to ioapics, and if we reprogram the
2613 * vector while Remote IRR is still set the irq will never
2614 * fire again.
2615 *
2616 * To prevent this scenario we read the Remote IRR bit
2617 * of the ioapic. This has two effects.
2618 * - On any sane system the read of the ioapic will
2619 * flush writes (and acks) going to the ioapic from
2620 * this cpu.
2621 * - We get to see if the ACK has actually been delivered.
2622 *
2623 * Based on failed experiments of reprogramming the
2624 * ioapic entry from outside of irq context starting
2625 * with masking the ioapic entry and then polling until
2626 * Remote IRR was clear before reprogramming the
2627 * ioapic I don't trust the Remote IRR bit to be
2628 * completey accurate.
2629 *
2630 * However there appears to be no other way to plug
2631 * this race, so if the Remote IRR bit is not
2632 * accurate and is causing problems then it is a hardware bug
2633 * and you can go talk to the chipset vendor about it.
2634 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002635 cfg = desc->chip_data;
2636 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002637 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002638 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002639 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002640
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002641#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002642 if (!(v & (1 << (i & 0x1f)))) {
2643 atomic_inc(&irq_mis_count);
2644 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002645 __mask_and_edge_IO_APIC_irq(cfg);
2646 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002647 spin_unlock(&ioapic_lock);
2648 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002649#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002650}
Yinghai Lu1d025192008-08-19 20:50:34 -07002651
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002652static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002653 .name = "IO-APIC",
2654 .startup = startup_ioapic_irq,
2655 .mask = mask_IO_APIC_irq,
2656 .unmask = unmask_IO_APIC_irq,
2657 .ack = ack_apic_edge,
2658 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002659#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002660 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002661#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002662 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663};
2664
Ingo Molnar54168ed2008-08-20 09:07:45 +02002665#ifdef CONFIG_INTR_REMAP
2666static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002667 .name = "IR-IO-APIC",
2668 .startup = startup_ioapic_irq,
2669 .mask = mask_IO_APIC_irq,
2670 .unmask = unmask_IO_APIC_irq,
2671 .ack = ack_x2apic_edge,
2672 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002673#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002674 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002675#endif
2676 .retrigger = ioapic_retrigger_irq,
2677};
2678#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680static inline void init_IO_APIC_traps(void)
2681{
2682 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002683 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002684 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
2686 /*
2687 * NOTE! The local APIC isn't very good at handling
2688 * multiple interrupts at the same interrupt level.
2689 * As the interrupt level is determined by taking the
2690 * vector number and shifting that right by 4, we
2691 * want to spread these out a bit so that they don't
2692 * all fall in the same interrupt level.
2693 *
2694 * Also, we've got to be careful not to trash gate
2695 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2696 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002697 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002698 cfg = desc->chip_data;
2699 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /*
2701 * Hmm.. We don't have an entry for this,
2702 * so default to an old-fashioned 8259
2703 * interrupt if we can..
2704 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002705 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002707 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002709 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
2711 }
2712}
2713
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002714/*
2715 * The local APIC irq-chip implementation:
2716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002718static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
2720 unsigned long v;
2721
2722 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002723 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002726static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002728 unsigned long v;
2729
2730 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002731 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732}
2733
Yinghai Lu3145e942008-12-05 18:58:34 -08002734static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002735{
2736 ack_APIC_irq();
2737}
2738
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002739static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002740 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002741 .mask = mask_lapic_irq,
2742 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002743 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744};
2745
Yinghai Lu3145e942008-12-05 18:58:34 -08002746static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002747{
Yinghai Lu08678b02008-08-19 20:50:05 -07002748 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002749 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2750 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002751}
2752
Jan Beuliche9427102008-01-30 13:31:24 +01002753static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002756 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 * We put the 8259A master into AEOI mode and
2758 * unmask on all local APICs LVT0 as NMI.
2759 *
2760 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2761 * is from Maciej W. Rozycki - so we do not have to EOI from
2762 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2765
Jan Beuliche9427102008-01-30 13:31:24 +01002766 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 apic_printk(APIC_VERBOSE, " done.\n");
2769}
2770
2771/*
2772 * This looks a bit hackish but it's about the only one way of sending
2773 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2774 * not support the ExtINT mode, unfortunately. We need to send these
2775 * cycles as some i82489DX-based boards have glue logic that keeps the
2776 * 8259A interrupt line asserted until INTA. --macro
2777 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002778static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002780 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 struct IO_APIC_route_entry entry0, entry1;
2782 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002784 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002785 if (pin == -1) {
2786 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002788 }
2789 apic = find_isa_irq_apic(8, mp_INT);
2790 if (apic == -1) {
2791 WARN_ON_ONCE(1);
2792 return;
2793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Andi Kleencf4c6a22006-09-26 10:52:30 +02002795 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002796 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 memset(&entry1, 0, sizeof(entry1));
2799
2800 entry1.dest_mode = 0; /* physical delivery */
2801 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002802 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 entry1.delivery_mode = dest_ExtINT;
2804 entry1.polarity = entry0.polarity;
2805 entry1.trigger = 0;
2806 entry1.vector = 0;
2807
Andi Kleencf4c6a22006-09-26 10:52:30 +02002808 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
2810 save_control = CMOS_READ(RTC_CONTROL);
2811 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2812 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2813 RTC_FREQ_SELECT);
2814 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2815
2816 i = 100;
2817 while (i-- > 0) {
2818 mdelay(10);
2819 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2820 i -= 10;
2821 }
2822
2823 CMOS_WRITE(save_control, RTC_CONTROL);
2824 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002825 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Andi Kleencf4c6a22006-09-26 10:52:30 +02002827 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
Yinghai Luefa25592008-08-19 20:50:36 -07002830static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002831/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002832static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002833{
2834 disable_timer_pin_1 = 1;
2835 return 0;
2836}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002837early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002838
2839int timer_through_8259 __initdata;
2840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841/*
2842 * This code may look a bit paranoid, but it's supposed to cooperate with
2843 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2844 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2845 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002846 *
2847 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002849static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Yinghai Lu3145e942008-12-05 18:58:34 -08002851 struct irq_desc *desc = irq_to_desc(0);
2852 struct irq_cfg *cfg = desc->chip_data;
2853 int cpu = boot_cpu_id;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002854 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002855 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002856 unsigned int ver;
2857 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002858
2859 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002860
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002861 ver = apic_read(APIC_LVR);
2862 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 /*
2865 * get/set the timer IRQ vector:
2866 */
2867 disable_8259A_irq(0);
Ingo Molnarfe402e12009-01-28 04:32:51 +01002868 assign_irq_vector(0, cfg, apic->target_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002871 * As IRQ0 is to be enabled in the 8259A, the virtual
2872 * wire has to be disabled in the local APIC. Also
2873 * timer interrupts need to be acknowledged manually in
2874 * the 8259A for the i82489DX when using the NMI
2875 * watchdog as that APIC treats NMIs as level-triggered.
2876 * The AEOI mode will finish them in the 8259A
2877 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002879 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002881#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002882 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002883#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002885 pin1 = find_isa_irq_pin(0, mp_INT);
2886 apic1 = find_isa_irq_apic(0, mp_INT);
2887 pin2 = ioapic_i8259.pin;
2888 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002890 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2891 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002892 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002894 /*
2895 * Some BIOS writers are clueless and report the ExtINTA
2896 * I/O APIC input from the cascaded 8259A as the timer
2897 * interrupt input. So just in case, if only one pin
2898 * was found above, try it both directly and through the
2899 * 8259A.
2900 */
2901 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002902#ifdef CONFIG_INTR_REMAP
2903 if (intr_remapping_enabled)
2904 panic("BIOS bug: timer not connected to IO-APIC");
2905#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002906 pin1 = pin2;
2907 apic1 = apic2;
2908 no_pin1 = 1;
2909 } else if (pin2 == -1) {
2910 pin2 = pin1;
2911 apic2 = apic1;
2912 }
2913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 if (pin1 != -1) {
2915 /*
2916 * Ok, does IRQ0 through the IOAPIC work?
2917 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002918 if (no_pin1) {
Yinghai Lu3145e942008-12-05 18:58:34 -08002919 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002920 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002921 }
Yinghai Lu3145e942008-12-05 18:58:34 -08002922 unmask_IO_APIC_irq_desc(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (timer_irq_works()) {
2924 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 setup_nmi();
2926 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002928 if (disable_timer_pin_1 > 0)
2929 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002930 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002932#ifdef CONFIG_INTR_REMAP
2933 if (intr_remapping_enabled)
2934 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2935#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002936 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002937 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002938 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2939 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002941 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2942 "(IRQ0) through the 8259A ...\n");
2943 apic_printk(APIC_QUIET, KERN_INFO
2944 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 /*
2946 * legacy devices should be connected to IO APIC #0
2947 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002948 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002949 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Yinghai Lu3145e942008-12-05 18:58:34 -08002950 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002951 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002953 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002954 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002956 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002958 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002960 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
2962 /*
2963 * Cleanup, just in case ...
2964 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002965 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002966 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002967 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002971 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2972 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002973 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002975#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002976 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002977#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002979 apic_printk(APIC_QUIET, KERN_INFO
2980 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Yinghai Lu3145e942008-12-05 18:58:34 -08002982 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002983 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 enable_8259A_irq(0);
2985
2986 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002987 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002988 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002990 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002991 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002992 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002994 apic_printk(APIC_QUIET, KERN_INFO
2995 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 init_8259A(0);
2998 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002999 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
3001 unlock_ExtINT_logic();
3002
3003 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003004 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003005 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003007 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003009 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003010out:
3011 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
3014/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003015 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
3016 * to devices. However there may be an I/O APIC pin available for
3017 * this interrupt regardless. The pin may be left unconnected, but
3018 * typically it will be reused as an ExtINT cascade interrupt for
3019 * the master 8259A. In the MPS case such a pin will normally be
3020 * reported as an ExtINT interrupt in the MP table. With ACPI
3021 * there is no provision for ExtINT interrupts, and in the absence
3022 * of an override it would be treated as an ordinary ISA I/O APIC
3023 * interrupt, that is edge-triggered and unmasked by default. We
3024 * used to do this, but it caused problems on some systems because
3025 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3026 * the same ExtINT cascade interrupt to drive the local APIC of the
3027 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3028 * the I/O APIC in all cases now. No actual device should request
3029 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 */
3031#define PIC_IRQS (1 << PIC_CASCADE_IR)
3032
3033void __init setup_IO_APIC(void)
3034{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003035
3036#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003038#else
3039 /*
3040 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3041 */
3042#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003044 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Ingo Molnar54168ed2008-08-20 09:07:45 +02003046 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003047 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003048 * Set up IO-APIC IRQ routing.
3049 */
3050#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003051 if (!acpi_ioapic)
3052 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 sync_Arb_IDs();
3055 setup_IO_APIC_irqs();
3056 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08003057 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
3060/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003061 * Called after all the initialization is done. If we didnt find any
3062 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065static int __init io_apic_bug_finalize(void)
3066{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003067 if (sis_apic_bug == -1)
3068 sis_apic_bug = 0;
3069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070}
3071
3072late_initcall(io_apic_bug_finalize);
3073
3074struct sysfs_ioapic_data {
3075 struct sys_device dev;
3076 struct IO_APIC_route_entry entry[0];
3077};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003078static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Pavel Machek438510f2005-04-16 15:25:24 -07003080static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 struct IO_APIC_route_entry *entry;
3083 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003085
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 data = container_of(dev, struct sysfs_ioapic_data, dev);
3087 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003088 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3089 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 return 0;
3092}
3093
3094static int ioapic_resume(struct sys_device *dev)
3095{
3096 struct IO_APIC_route_entry *entry;
3097 struct sysfs_ioapic_data *data;
3098 unsigned long flags;
3099 union IO_APIC_reg_00 reg_00;
3100 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003101
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 data = container_of(dev, struct sysfs_ioapic_data, dev);
3103 entry = data->entry;
3104
3105 spin_lock_irqsave(&ioapic_lock, flags);
3106 reg_00.raw = io_apic_read(dev->id, 0);
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +05303107 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3108 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 io_apic_write(dev->id, 0, reg_00.raw);
3110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003112 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003113 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 return 0;
3116}
3117
3118static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f42007-12-20 02:09:39 +01003119 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 .suspend = ioapic_suspend,
3121 .resume = ioapic_resume,
3122};
3123
3124static int __init ioapic_init_sysfs(void)
3125{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003126 struct sys_device * dev;
3127 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 error = sysdev_class_register(&ioapic_sysdev_class);
3130 if (error)
3131 return error;
3132
Ingo Molnar54168ed2008-08-20 09:07:45 +02003133 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003134 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003136 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 if (!mp_ioapic_data[i]) {
3138 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3139 continue;
3140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003142 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 dev->cls = &ioapic_sysdev_class;
3144 error = sysdev_register(dev);
3145 if (error) {
3146 kfree(mp_ioapic_data[i]);
3147 mp_ioapic_data[i] = NULL;
3148 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3149 continue;
3150 }
3151 }
3152
3153 return 0;
3154}
3155
3156device_initcall(ioapic_init_sysfs);
3157
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003158/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003159 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003160 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003161unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003162{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003163 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003164 unsigned int irq;
3165 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003166 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003167 struct irq_cfg *cfg_new = NULL;
3168 int cpu = boot_cpu_id;
3169 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003170
3171 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003172 spin_lock_irqsave(&vector_lock, flags);
Mike Travis95949492009-01-10 22:24:06 -08003173 for (new = irq_want; new < nr_irqs; new++) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003174 if (platform_legacy_irq(new))
3175 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003176
3177 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3178 if (!desc_new) {
3179 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003180 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003181 }
3182 cfg_new = desc_new->chip_data;
3183
3184 if (cfg_new->vector != 0)
3185 continue;
Ingo Molnarfe402e12009-01-28 04:32:51 +01003186 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003187 irq = new;
3188 break;
3189 }
3190 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003191
Yinghai Lu199751d2008-08-19 20:50:27 -07003192 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003193 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003194 /* restore it, in case dynamic_irq_init clear it */
3195 if (desc_new)
3196 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003197 }
3198 return irq;
3199}
3200
Yinghai Lube5d5352008-12-05 18:58:33 -08003201static int nr_irqs_gsi = NR_IRQS_LEGACY;
Yinghai Lu199751d2008-08-19 20:50:27 -07003202int create_irq(void)
3203{
Yinghai Lube5d5352008-12-05 18:58:33 -08003204 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003205 int irq;
3206
Yinghai Lube5d5352008-12-05 18:58:33 -08003207 irq_want = nr_irqs_gsi;
3208 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003209
3210 if (irq == 0)
3211 irq = -1;
3212
3213 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003214}
3215
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003216void destroy_irq(unsigned int irq)
3217{
3218 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003219 struct irq_cfg *cfg;
3220 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003221
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003222 /* store it, in case dynamic_irq_cleanup clear it */
3223 desc = irq_to_desc(irq);
3224 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003225 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003226 /* connect back irq_cfg */
3227 if (desc)
3228 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003229
Ingo Molnar54168ed2008-08-20 09:07:45 +02003230#ifdef CONFIG_INTR_REMAP
3231 free_irte(irq);
3232#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003233 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003234 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003235 spin_unlock_irqrestore(&vector_lock, flags);
3236}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003237
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003238/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003239 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003240 */
3241#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003242static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003243{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003244 struct irq_cfg *cfg;
3245 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003246 unsigned dest;
3247
Jan Beulichf1182632009-01-14 12:27:35 +00003248 if (disable_apic)
3249 return -ENXIO;
3250
Yinghai Lu3145e942008-12-05 18:58:34 -08003251 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003252 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07003253 if (err)
3254 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003255
Ingo Molnardebccb32009-01-28 15:20:18 +01003256 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003257
Ingo Molnar54168ed2008-08-20 09:07:45 +02003258#ifdef CONFIG_INTR_REMAP
3259 if (irq_remapped(irq)) {
3260 struct irte irte;
3261 int ir_index;
3262 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003263
Ingo Molnar54168ed2008-08-20 09:07:45 +02003264 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3265 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003266
Ingo Molnar54168ed2008-08-20 09:07:45 +02003267 memset (&irte, 0, sizeof(irte));
3268
3269 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003270 irte.dst_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003271 irte.trigger_mode = 0; /* edge */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003272 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003273 irte.vector = cfg->vector;
3274 irte.dest_id = IRTE_DEST(dest);
3275
3276 modify_irte(irq, &irte);
3277
3278 msg->address_hi = MSI_ADDR_BASE_HI;
3279 msg->data = sub_handle;
3280 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3281 MSI_ADDR_IR_SHV |
3282 MSI_ADDR_IR_INDEX1(ir_index) |
3283 MSI_ADDR_IR_INDEX2(ir_index);
3284 } else
3285#endif
3286 {
3287 msg->address_hi = MSI_ADDR_BASE_HI;
3288 msg->address_lo =
3289 MSI_ADDR_BASE_LO |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003290 ((apic->irq_dest_mode == 0) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003291 MSI_ADDR_DEST_MODE_PHYSICAL:
3292 MSI_ADDR_DEST_MODE_LOGICAL) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003293 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003294 MSI_ADDR_REDIRECTION_CPU:
3295 MSI_ADDR_REDIRECTION_LOWPRI) |
3296 MSI_ADDR_DEST_ID(dest);
3297
3298 msg->data =
3299 MSI_DATA_TRIGGER_EDGE |
3300 MSI_DATA_LEVEL_ASSERT |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003301 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003302 MSI_DATA_DELIVERY_FIXED:
3303 MSI_DATA_DELIVERY_LOWPRI) |
3304 MSI_DATA_VECTOR(cfg->vector);
3305 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003306 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003307}
3308
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003309#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303310static void set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003311{
Yinghai Lu3145e942008-12-05 18:58:34 -08003312 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003313 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003314 struct msi_msg msg;
3315 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003316
Mike Travis22f65d32008-12-16 17:33:56 -08003317 dest = set_desc_affinity(desc, mask);
3318 if (dest == BAD_APICID)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003319 return;
3320
Yinghai Lu3145e942008-12-05 18:58:34 -08003321 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003322
Yinghai Lu3145e942008-12-05 18:58:34 -08003323 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003324
3325 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003326 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003327 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3328 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3329
Yinghai Lu3145e942008-12-05 18:58:34 -08003330 write_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003331}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003332#ifdef CONFIG_INTR_REMAP
3333/*
3334 * Migrate the MSI irq to another cpumask. This migration is
3335 * done in the process context using interrupt-remapping hardware.
3336 */
Mike Travise7986732008-12-16 17:33:52 -08003337static void
3338ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003339{
Yinghai Lu3145e942008-12-05 18:58:34 -08003340 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003341 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003342 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003343 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003344
3345 if (get_irte(irq, &irte))
3346 return;
3347
Mike Travis22f65d32008-12-16 17:33:56 -08003348 dest = set_desc_affinity(desc, mask);
3349 if (dest == BAD_APICID)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003350 return;
3351
Ingo Molnar54168ed2008-08-20 09:07:45 +02003352 irte.vector = cfg->vector;
3353 irte.dest_id = IRTE_DEST(dest);
3354
3355 /*
3356 * atomically update the IRTE with the new destination and vector.
3357 */
3358 modify_irte(irq, &irte);
3359
3360 /*
3361 * After this point, all the interrupts will start arriving
3362 * at the new destination. So, time to cleanup the previous
3363 * vector allocation.
3364 */
Mike Travis22f65d32008-12-16 17:33:56 -08003365 if (cfg->move_in_progress)
3366 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003367}
Yinghai Lu3145e942008-12-05 18:58:34 -08003368
Ingo Molnar54168ed2008-08-20 09:07:45 +02003369#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003370#endif /* CONFIG_SMP */
3371
3372/*
3373 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3374 * which implement the MSI or MSI-X Capability Structure.
3375 */
3376static struct irq_chip msi_chip = {
3377 .name = "PCI-MSI",
3378 .unmask = unmask_msi_irq,
3379 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003380 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003381#ifdef CONFIG_SMP
3382 .set_affinity = set_msi_irq_affinity,
3383#endif
3384 .retrigger = ioapic_retrigger_irq,
3385};
3386
Ingo Molnar54168ed2008-08-20 09:07:45 +02003387#ifdef CONFIG_INTR_REMAP
3388static struct irq_chip msi_ir_chip = {
3389 .name = "IR-PCI-MSI",
3390 .unmask = unmask_msi_irq,
3391 .mask = mask_msi_irq,
3392 .ack = ack_x2apic_edge,
3393#ifdef CONFIG_SMP
3394 .set_affinity = ir_set_msi_irq_affinity,
3395#endif
3396 .retrigger = ioapic_retrigger_irq,
3397};
3398
3399/*
3400 * Map the PCI dev to the corresponding remapping hardware unit
3401 * and allocate 'nvec' consecutive interrupt-remapping table entries
3402 * in it.
3403 */
3404static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3405{
3406 struct intel_iommu *iommu;
3407 int index;
3408
3409 iommu = map_dev_to_ir(dev);
3410 if (!iommu) {
3411 printk(KERN_ERR
3412 "Unable to map PCI %s to iommu\n", pci_name(dev));
3413 return -ENOENT;
3414 }
3415
3416 index = alloc_irte(iommu, irq, nvec);
3417 if (index < 0) {
3418 printk(KERN_ERR
3419 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003420 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003421 return -ENOSPC;
3422 }
3423 return index;
3424}
3425#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003426
Yinghai Lu3145e942008-12-05 18:58:34 -08003427static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003428{
3429 int ret;
3430 struct msi_msg msg;
3431
3432 ret = msi_compose_msg(dev, irq, &msg);
3433 if (ret < 0)
3434 return ret;
3435
Yinghai Lu3145e942008-12-05 18:58:34 -08003436 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003437 write_msi_msg(irq, &msg);
3438
Ingo Molnar54168ed2008-08-20 09:07:45 +02003439#ifdef CONFIG_INTR_REMAP
3440 if (irq_remapped(irq)) {
3441 struct irq_desc *desc = irq_to_desc(irq);
3442 /*
3443 * irq migration in process context
3444 */
3445 desc->status |= IRQ_MOVE_PCNTXT;
3446 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3447 } else
3448#endif
3449 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003450
Yinghai Luc81bba42008-09-25 11:53:11 -07003451 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3452
Yinghai Lu1d025192008-08-19 20:50:34 -07003453 return 0;
3454}
3455
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003456int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3457{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003458 unsigned int irq;
3459 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003460 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003461 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003462
Ingo Molnar54168ed2008-08-20 09:07:45 +02003463#ifdef CONFIG_INTR_REMAP
3464 struct intel_iommu *iommu = 0;
3465 int index = 0;
3466#endif
3467
Yinghai Lube5d5352008-12-05 18:58:33 -08003468 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003469 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003470 list_for_each_entry(msidesc, &dev->msi_list, list) {
3471 irq = create_irq_nr(irq_want);
Yinghai Lube5d5352008-12-05 18:58:33 -08003472 irq_want++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003473 if (irq == 0)
3474 return -1;
3475#ifdef CONFIG_INTR_REMAP
3476 if (!intr_remapping_enabled)
3477 goto no_ir;
3478
3479 if (!sub_handle) {
3480 /*
3481 * allocate the consecutive block of IRTE's
3482 * for 'nvec'
3483 */
3484 index = msi_alloc_irte(dev, irq, nvec);
3485 if (index < 0) {
3486 ret = index;
3487 goto error;
3488 }
3489 } else {
3490 iommu = map_dev_to_ir(dev);
3491 if (!iommu) {
3492 ret = -ENOENT;
3493 goto error;
3494 }
3495 /*
3496 * setup the mapping between the irq and the IRTE
3497 * base index, the sub_handle pointing to the
3498 * appropriate interrupt remap table entry.
3499 */
3500 set_irte_irq(irq, iommu, index, sub_handle);
3501 }
3502no_ir:
3503#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003504 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003505 if (ret < 0)
3506 goto error;
3507 sub_handle++;
3508 }
3509 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003510
3511error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003512 destroy_irq(irq);
3513 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003514}
3515
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003516void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003517{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003518 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003519}
3520
Ingo Molnar54168ed2008-08-20 09:07:45 +02003521#ifdef CONFIG_DMAR
3522#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -08003523static void dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003524{
Yinghai Lu3145e942008-12-05 18:58:34 -08003525 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003526 struct irq_cfg *cfg;
3527 struct msi_msg msg;
3528 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003529
Mike Travis22f65d32008-12-16 17:33:56 -08003530 dest = set_desc_affinity(desc, mask);
3531 if (dest == BAD_APICID)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003532 return;
3533
Yinghai Lu3145e942008-12-05 18:58:34 -08003534 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003535
3536 dmar_msi_read(irq, &msg);
3537
3538 msg.data &= ~MSI_DATA_VECTOR_MASK;
3539 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3540 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3541 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3542
3543 dmar_msi_write(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003544}
Yinghai Lu3145e942008-12-05 18:58:34 -08003545
Ingo Molnar54168ed2008-08-20 09:07:45 +02003546#endif /* CONFIG_SMP */
3547
3548struct irq_chip dmar_msi_type = {
3549 .name = "DMAR_MSI",
3550 .unmask = dmar_msi_unmask,
3551 .mask = dmar_msi_mask,
3552 .ack = ack_apic_edge,
3553#ifdef CONFIG_SMP
3554 .set_affinity = dmar_msi_set_affinity,
3555#endif
3556 .retrigger = ioapic_retrigger_irq,
3557};
3558
3559int arch_setup_dmar_msi(unsigned int irq)
3560{
3561 int ret;
3562 struct msi_msg msg;
3563
3564 ret = msi_compose_msg(NULL, irq, &msg);
3565 if (ret < 0)
3566 return ret;
3567 dmar_msi_write(irq, &msg);
3568 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3569 "edge");
3570 return 0;
3571}
3572#endif
3573
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003574#ifdef CONFIG_HPET_TIMER
3575
3576#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -08003577static void hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003578{
Yinghai Lu3145e942008-12-05 18:58:34 -08003579 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003580 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003581 struct msi_msg msg;
3582 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003583
Mike Travis22f65d32008-12-16 17:33:56 -08003584 dest = set_desc_affinity(desc, mask);
3585 if (dest == BAD_APICID)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003586 return;
3587
Yinghai Lu3145e942008-12-05 18:58:34 -08003588 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003589
3590 hpet_msi_read(irq, &msg);
3591
3592 msg.data &= ~MSI_DATA_VECTOR_MASK;
3593 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3594 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3595 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3596
3597 hpet_msi_write(irq, &msg);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003598}
Yinghai Lu3145e942008-12-05 18:58:34 -08003599
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003600#endif /* CONFIG_SMP */
3601
3602struct irq_chip hpet_msi_type = {
3603 .name = "HPET_MSI",
3604 .unmask = hpet_msi_unmask,
3605 .mask = hpet_msi_mask,
3606 .ack = ack_apic_edge,
3607#ifdef CONFIG_SMP
3608 .set_affinity = hpet_msi_set_affinity,
3609#endif
3610 .retrigger = ioapic_retrigger_irq,
3611};
3612
3613int arch_setup_hpet_msi(unsigned int irq)
3614{
3615 int ret;
3616 struct msi_msg msg;
3617
3618 ret = msi_compose_msg(NULL, irq, &msg);
3619 if (ret < 0)
3620 return ret;
3621
3622 hpet_msi_write(irq, &msg);
3623 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3624 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003625
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003626 return 0;
3627}
3628#endif
3629
Ingo Molnar54168ed2008-08-20 09:07:45 +02003630#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003631/*
3632 * Hypertransport interrupt support
3633 */
3634#ifdef CONFIG_HT_IRQ
3635
3636#ifdef CONFIG_SMP
3637
Yinghai Lu497c9a12008-08-19 20:50:28 -07003638static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003639{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003640 struct ht_irq_msg msg;
3641 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003642
Yinghai Lu497c9a12008-08-19 20:50:28 -07003643 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003644 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003645
Yinghai Lu497c9a12008-08-19 20:50:28 -07003646 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003647 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003648
Eric W. Biedermanec683072006-11-08 17:44:57 -08003649 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003650}
3651
Mike Travis22f65d32008-12-16 17:33:56 -08003652static void set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003653{
Yinghai Lu3145e942008-12-05 18:58:34 -08003654 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003655 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003656 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003657
Mike Travis22f65d32008-12-16 17:33:56 -08003658 dest = set_desc_affinity(desc, mask);
3659 if (dest == BAD_APICID)
Yinghai Lu497c9a12008-08-19 20:50:28 -07003660 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003661
Yinghai Lu3145e942008-12-05 18:58:34 -08003662 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003663
Yinghai Lu497c9a12008-08-19 20:50:28 -07003664 target_ht_irq(irq, dest, cfg->vector);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003665}
Yinghai Lu3145e942008-12-05 18:58:34 -08003666
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003667#endif
3668
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003669static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003670 .name = "PCI-HT",
3671 .mask = mask_ht_irq,
3672 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003673 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003674#ifdef CONFIG_SMP
3675 .set_affinity = set_ht_irq_affinity,
3676#endif
3677 .retrigger = ioapic_retrigger_irq,
3678};
3679
3680int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3681{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003682 struct irq_cfg *cfg;
3683 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003684
Jan Beulichf1182632009-01-14 12:27:35 +00003685 if (disable_apic)
3686 return -ENXIO;
3687
Yinghai Lu3145e942008-12-05 18:58:34 -08003688 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003689 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Ingo Molnar54168ed2008-08-20 09:07:45 +02003690 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003691 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003692 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003693
Ingo Molnardebccb32009-01-28 15:20:18 +01003694 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3695 apic->target_cpus());
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003696
Eric W. Biedermanec683072006-11-08 17:44:57 -08003697 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003698
Eric W. Biedermanec683072006-11-08 17:44:57 -08003699 msg.address_lo =
3700 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003701 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003702 HT_IRQ_LOW_VECTOR(cfg->vector) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003703 ((apic->irq_dest_mode == 0) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003704 HT_IRQ_LOW_DM_PHYSICAL :
3705 HT_IRQ_LOW_DM_LOGICAL) |
3706 HT_IRQ_LOW_RQEOI_EDGE |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003707 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003708 HT_IRQ_LOW_MT_FIXED :
3709 HT_IRQ_LOW_MT_ARBITRATED) |
3710 HT_IRQ_LOW_IRQ_MASKED;
3711
Eric W. Biedermanec683072006-11-08 17:44:57 -08003712 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003713
Ingo Molnara460e742006-10-17 00:10:03 -07003714 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3715 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003716
3717 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003718 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003719 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003720}
3721#endif /* CONFIG_HT_IRQ */
3722
Nick Piggin03b48632009-01-20 04:36:04 +01003723#ifdef CONFIG_X86_UV
Dean Nelson4173a0e2008-10-02 12:18:21 -05003724/*
3725 * Re-target the irq to the specified CPU and enable the specified MMR located
3726 * on the specified blade to allow the sending of MSIs to the specified CPU.
3727 */
3728int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3729 unsigned long mmr_offset)
3730{
Mike Travis22f65d32008-12-16 17:33:56 -08003731 const struct cpumask *eligible_cpu = cpumask_of(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003732 struct irq_cfg *cfg;
3733 int mmr_pnode;
3734 unsigned long mmr_value;
3735 struct uv_IO_APIC_route_entry *entry;
3736 unsigned long flags;
3737 int err;
3738
Yinghai Lu3145e942008-12-05 18:58:34 -08003739 cfg = irq_cfg(irq);
3740
Mike Travise7986732008-12-16 17:33:52 -08003741 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003742 if (err != 0)
3743 return err;
3744
3745 spin_lock_irqsave(&vector_lock, flags);
3746 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3747 irq_name);
3748 spin_unlock_irqrestore(&vector_lock, flags);
3749
Dean Nelson4173a0e2008-10-02 12:18:21 -05003750 mmr_value = 0;
3751 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3752 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3753
3754 entry->vector = cfg->vector;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003755 entry->delivery_mode = apic->irq_delivery_mode;
3756 entry->dest_mode = apic->irq_dest_mode;
Dean Nelson4173a0e2008-10-02 12:18:21 -05003757 entry->polarity = 0;
3758 entry->trigger = 0;
3759 entry->mask = 0;
Ingo Molnardebccb32009-01-28 15:20:18 +01003760 entry->dest = apic->cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003761
3762 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3763 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3764
3765 return irq;
3766}
3767
3768/*
3769 * Disable the specified MMR located on the specified blade so that MSIs are
3770 * longer allowed to be sent.
3771 */
3772void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3773{
3774 unsigned long mmr_value;
3775 struct uv_IO_APIC_route_entry *entry;
3776 int mmr_pnode;
3777
3778 mmr_value = 0;
3779 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3780 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3781
3782 entry->mask = 1;
3783
3784 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3785 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3786}
3787#endif /* CONFIG_X86_64 */
3788
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003789int __init io_apic_get_redir_entries (int ioapic)
3790{
3791 union IO_APIC_reg_01 reg_01;
3792 unsigned long flags;
3793
3794 spin_lock_irqsave(&ioapic_lock, flags);
3795 reg_01.raw = io_apic_read(ioapic, 1);
3796 spin_unlock_irqrestore(&ioapic_lock, flags);
3797
3798 return reg_01.bits.entries;
3799}
3800
Yinghai Lube5d5352008-12-05 18:58:33 -08003801void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003802{
Yinghai Lube5d5352008-12-05 18:58:33 -08003803 int idx;
3804 int nr = 0;
3805
3806 for (idx = 0; idx < nr_ioapics; idx++)
3807 nr += io_apic_get_redir_entries(idx) + 1;
3808
3809 if (nr > nr_irqs_gsi)
3810 nr_irqs_gsi = nr;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003811}
3812
Yinghai Lu4a046d12009-01-12 17:39:24 -08003813#ifdef CONFIG_SPARSE_IRQ
3814int __init arch_probe_nr_irqs(void)
3815{
3816 int nr;
3817
3818 nr = ((8 * nr_cpu_ids) > (32 * nr_ioapics) ?
3819 (NR_VECTORS + (8 * nr_cpu_ids)) :
3820 (NR_VECTORS + (32 * nr_ioapics)));
3821
3822 if (nr < nr_irqs && nr > nr_irqs_gsi)
3823 nr_irqs = nr;
3824
3825 return 0;
3826}
3827#endif
3828
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003830 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 -------------------------------------------------------------------------- */
3832
Len Brown888ba6c2005-08-24 12:07:20 -04003833#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
Ingo Molnar54168ed2008-08-20 09:07:45 +02003835#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003836int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837{
3838 union IO_APIC_reg_00 reg_00;
3839 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3840 physid_mask_t tmp;
3841 unsigned long flags;
3842 int i = 0;
3843
3844 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003845 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3846 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003848 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3850 * advantage of new APIC bus architecture.
3851 */
3852
3853 if (physids_empty(apic_id_map))
Ingo Molnard190cb82009-01-28 06:50:47 +01003854 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
3856 spin_lock_irqsave(&ioapic_lock, flags);
3857 reg_00.raw = io_apic_read(ioapic, 0);
3858 spin_unlock_irqrestore(&ioapic_lock, flags);
3859
3860 if (apic_id >= get_physical_broadcast()) {
3861 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3862 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3863 apic_id = reg_00.bits.ID;
3864 }
3865
3866 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003867 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 * 'stuck on smp_invalidate_needed IPI wait' messages.
3869 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003870 if (apic->check_apicid_used(apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
3872 for (i = 0; i < get_physical_broadcast(); i++) {
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003873 if (!apic->check_apicid_used(apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 break;
3875 }
3876
3877 if (i == get_physical_broadcast())
3878 panic("Max apic_id exceeded!\n");
3879
3880 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3881 "trying %d\n", ioapic, apic_id, i);
3882
3883 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885
Ingo Molnar80587142009-01-28 06:50:47 +01003886 tmp = apic->apicid_to_cpu_present(apic_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 physids_or(apic_id_map, apic_id_map, tmp);
3888
3889 if (reg_00.bits.ID != apic_id) {
3890 reg_00.bits.ID = apic_id;
3891
3892 spin_lock_irqsave(&ioapic_lock, flags);
3893 io_apic_write(ioapic, 0, reg_00.raw);
3894 reg_00.raw = io_apic_read(ioapic, 0);
3895 spin_unlock_irqrestore(&ioapic_lock, flags);
3896
3897 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003898 if (reg_00.bits.ID != apic_id) {
3899 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3900 return -1;
3901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
3903
3904 apic_printk(APIC_VERBOSE, KERN_INFO
3905 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3906
3907 return apic_id;
3908}
3909
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003910int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
3912 union IO_APIC_reg_01 reg_01;
3913 unsigned long flags;
3914
3915 spin_lock_irqsave(&ioapic_lock, flags);
3916 reg_01.raw = io_apic_read(ioapic, 1);
3917 spin_unlock_irqrestore(&ioapic_lock, flags);
3918
3919 return reg_01.bits.version;
3920}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003921#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Ingo Molnar54168ed2008-08-20 09:07:45 +02003923int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003925 struct irq_desc *desc;
3926 struct irq_cfg *cfg;
3927 int cpu = boot_cpu_id;
3928
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003930 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 ioapic);
3932 return -EINVAL;
3933 }
3934
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003935 desc = irq_to_desc_alloc_cpu(irq, cpu);
3936 if (!desc) {
3937 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3938 return 0;
3939 }
3940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 * IRQs < 16 are already in the irq_2_pin[] map
3943 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003944 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003945 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003946 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Yinghai Lu3145e942008-12-05 18:58:34 -08003949 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950
3951 return 0;
3952}
3953
Ingo Molnar54168ed2008-08-20 09:07:45 +02003954
Shaohua Li61fd47e2007-11-17 01:05:28 -05003955int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3956{
3957 int i;
3958
3959 if (skip_ioapic_setup)
3960 return -1;
3961
3962 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05303963 if (mp_irqs[i].irqtype == mp_INT &&
3964 mp_irqs[i].srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003965 break;
3966 if (i >= mp_irq_entries)
3967 return -1;
3968
3969 *trigger = irq_trigger(i);
3970 *polarity = irq_polarity(i);
3971 return 0;
3972}
3973
Len Brown888ba6c2005-08-24 12:07:20 -04003974#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003975
Yinghai Lu497c9a12008-08-19 20:50:28 -07003976/*
3977 * This function currently is only a helper for the i386 smp boot process where
3978 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01003979 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07003980 */
3981#ifdef CONFIG_SMP
3982void __init setup_ioapic_dest(void)
3983{
3984 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003985 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003986 struct irq_cfg *cfg;
Mike Travis22f65d32008-12-16 17:33:56 -08003987 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003988
3989 if (skip_ioapic_setup == 1)
3990 return;
3991
3992 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3993 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3994 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3995 if (irq_entry == -1)
3996 continue;
3997 irq = pin_2_irq(irq_entry, ioapic, pin);
3998
3999 /* setup_IO_APIC_irqs could fail to get vector for some device
4000 * when you have too many devices, because at that time only boot
4001 * cpu is online.
4002 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08004003 desc = irq_to_desc(irq);
4004 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004005 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08004006 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07004007 irq_trigger(irq_entry),
4008 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004009 continue;
4010
4011 }
4012
4013 /*
4014 * Honour affinities which have been set in early boot
4015 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004016 if (desc->status &
4017 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
Mike Travis7f7ace02009-01-10 21:58:08 -08004018 mask = desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004019 else
Ingo Molnarfe402e12009-01-28 04:32:51 +01004020 mask = apic->target_cpus();
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004021
4022#ifdef CONFIG_INTR_REMAP
4023 if (intr_remapping_enabled)
Yinghai Lu3145e942008-12-05 18:58:34 -08004024 set_ir_ioapic_affinity_irq_desc(desc, mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004025 else
4026#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08004027 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07004028 }
4029
4030 }
4031}
4032#endif
4033
Ingo Molnar54168ed2008-08-20 09:07:45 +02004034#define IOAPIC_RESOURCE_NAME_SIZE 11
4035
4036static struct resource *ioapic_resources;
4037
4038static struct resource * __init ioapic_setup_resources(void)
4039{
4040 unsigned long n;
4041 struct resource *res;
4042 char *mem;
4043 int i;
4044
4045 if (nr_ioapics <= 0)
4046 return NULL;
4047
4048 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4049 n *= nr_ioapics;
4050
4051 mem = alloc_bootmem(n);
4052 res = (void *)mem;
4053
4054 if (mem != NULL) {
4055 mem += sizeof(struct resource) * nr_ioapics;
4056
4057 for (i = 0; i < nr_ioapics; i++) {
4058 res[i].name = mem;
4059 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4060 sprintf(mem, "IOAPIC %u", i);
4061 mem += IOAPIC_RESOURCE_NAME_SIZE;
4062 }
4063 }
4064
4065 ioapic_resources = res;
4066
4067 return res;
4068}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004069
Yinghai Luf3294a32008-06-27 01:41:56 -07004070void __init ioapic_init_mappings(void)
4071{
4072 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004073 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004074 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004075
Ingo Molnar54168ed2008-08-20 09:07:45 +02004076 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07004077 for (i = 0; i < nr_ioapics; i++) {
4078 if (smp_found_config) {
Jaswinder Singh Rajputb5ba7e6d2009-01-12 17:46:17 +05304079 ioapic_phys = mp_ioapics[i].apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004080#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004081 if (!ioapic_phys) {
4082 printk(KERN_ERR
4083 "WARNING: bogus zero IO-APIC "
4084 "address found in MPTABLE, "
4085 "disabling IO/APIC support!\n");
4086 smp_found_config = 0;
4087 skip_ioapic_setup = 1;
4088 goto fake_ioapic_page;
4089 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004090#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004091 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004092#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004093fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004094#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004095 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004096 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004097 ioapic_phys = __pa(ioapic_phys);
4098 }
4099 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004100 apic_printk(APIC_VERBOSE,
4101 "mapped IOAPIC to %08lx (%08lx)\n",
4102 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004103 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004104
Ingo Molnar54168ed2008-08-20 09:07:45 +02004105 if (ioapic_res != NULL) {
4106 ioapic_res->start = ioapic_phys;
4107 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4108 ioapic_res++;
4109 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004110 }
4111}
4112
Ingo Molnar54168ed2008-08-20 09:07:45 +02004113static int __init ioapic_insert_resources(void)
4114{
4115 int i;
4116 struct resource *r = ioapic_resources;
4117
4118 if (!r) {
4119 printk(KERN_ERR
4120 "IO APIC resources could be not be allocated.\n");
4121 return -1;
4122 }
4123
4124 for (i = 0; i < nr_ioapics; i++) {
4125 insert_resource(&iomem_resource, r);
4126 r++;
4127 }
4128
4129 return 0;
4130}
4131
4132/* Insert the IO APIC resources after PCI initialization has occured to handle
4133 * IO APICS that are mapped in on a BAR in PCI space. */
4134late_initcall(ioapic_insert_resources);