blob: 0a7f6d6b1206f5b9aa9dbc5978eb624cf3ce1f6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
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
Yinghai Lu497c9a12008-08-19 20:50:28 -070065#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020067#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010069#define __apicdebuginit(type) static type __init
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020072 * Is the SiS APIC rmw bug present ?
73 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 */
75int sis_apic_bug = -1;
76
Yinghai Luefa25592008-08-19 20:50:36 -070077static DEFINE_SPINLOCK(ioapic_lock);
78static DEFINE_SPINLOCK(vector_lock);
79
Yinghai Luefa25592008-08-19 20:50:36 -070080/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * # of IRQ routing registers
82 */
83int nr_ioapic_registers[MAX_IO_APICS];
84
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040085/* I/O APIC entries */
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +053086struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040087int nr_ioapics;
88
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040089/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +053090struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040091
92/* # of MP IRQ source entries */
93int mp_irq_entries;
94
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040095#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
96int mp_bus_id_to_type[MAX_MP_BUSSES];
97#endif
98
99DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
100
Yinghai Luefa25592008-08-19 20:50:36 -0700101int skip_ioapic_setup;
102
Ingo Molnar54168ed2008-08-20 09:07:45 +0200103static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700104{
105 /* disable IO-APIC */
106 disable_ioapic_setup();
107 return 0;
108}
109early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200110
Yinghai Lu0f978f42008-08-19 20:50:26 -0700111struct irq_pin_list;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200112
113/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * This is performance-critical, we want to do it O(1)
115 *
116 * the indexing order of this array favors 1:1 mappings
117 * between pins and IRQs.
118 */
119
Yinghai Lu0f978f42008-08-19 20:50:26 -0700120struct irq_pin_list {
121 int apic, pin;
122 struct irq_pin_list *next;
123};
Yinghai Lu301e6192008-08-19 20:50:02 -0700124
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800125static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700126{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800127 struct irq_pin_list *pin;
128 int node;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700129
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800130 node = cpu_to_node(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700131
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800132 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700133
Yinghai Lu0f978f42008-08-19 20:50:26 -0700134 return pin;
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800137struct irq_cfg {
138 struct irq_pin_list *irq_2_pin;
Mike Travis22f65d32008-12-16 17:33:56 -0800139 cpumask_var_t domain;
140 cpumask_var_t old_domain;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800141 unsigned move_cleanup_count;
142 u8 vector;
143 u8 move_in_progress : 1;
Yinghai Lu48a1b102008-12-11 00:15:01 -0800144#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
145 u8 move_desc_pending : 1;
146#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800147};
148
149/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
150#ifdef CONFIG_SPARSE_IRQ
151static struct irq_cfg irq_cfgx[] = {
152#else
153static struct irq_cfg irq_cfgx[NR_IRQS] = {
154#endif
Mike Travis22f65d32008-12-16 17:33:56 -0800155 [0] = { .vector = IRQ0_VECTOR, },
156 [1] = { .vector = IRQ1_VECTOR, },
157 [2] = { .vector = IRQ2_VECTOR, },
158 [3] = { .vector = IRQ3_VECTOR, },
159 [4] = { .vector = IRQ4_VECTOR, },
160 [5] = { .vector = IRQ5_VECTOR, },
161 [6] = { .vector = IRQ6_VECTOR, },
162 [7] = { .vector = IRQ7_VECTOR, },
163 [8] = { .vector = IRQ8_VECTOR, },
164 [9] = { .vector = IRQ9_VECTOR, },
165 [10] = { .vector = IRQ10_VECTOR, },
166 [11] = { .vector = IRQ11_VECTOR, },
167 [12] = { .vector = IRQ12_VECTOR, },
168 [13] = { .vector = IRQ13_VECTOR, },
169 [14] = { .vector = IRQ14_VECTOR, },
170 [15] = { .vector = IRQ15_VECTOR, },
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800171};
172
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800173int __init arch_early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800174{
175 struct irq_cfg *cfg;
176 struct irq_desc *desc;
177 int count;
178 int i;
179
180 cfg = irq_cfgx;
181 count = ARRAY_SIZE(irq_cfgx);
182
183 for (i = 0; i < count; i++) {
184 desc = irq_to_desc(i);
185 desc->chip_data = &cfg[i];
Mike Travis22f65d32008-12-16 17:33:56 -0800186 alloc_bootmem_cpumask_var(&cfg[i].domain);
187 alloc_bootmem_cpumask_var(&cfg[i].old_domain);
188 if (i < NR_IRQS_LEGACY)
189 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800190 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800191
192 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800193}
194
195#ifdef CONFIG_SPARSE_IRQ
196static struct irq_cfg *irq_cfg(unsigned int irq)
197{
198 struct irq_cfg *cfg = NULL;
199 struct irq_desc *desc;
200
201 desc = irq_to_desc(irq);
202 if (desc)
203 cfg = desc->chip_data;
204
205 return cfg;
206}
207
208static struct irq_cfg *get_one_free_irq_cfg(int cpu)
209{
210 struct irq_cfg *cfg;
211 int node;
212
213 node = cpu_to_node(cpu);
214
215 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800216 if (cfg) {
Mike Travis80855f72008-12-31 18:08:47 -0800217 if (!alloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800218 kfree(cfg);
219 cfg = NULL;
Mike Travis80855f72008-12-31 18:08:47 -0800220 } else if (!alloc_cpumask_var_node(&cfg->old_domain,
221 GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800222 free_cpumask_var(cfg->domain);
223 kfree(cfg);
224 cfg = NULL;
225 } else {
226 cpumask_clear(cfg->domain);
227 cpumask_clear(cfg->old_domain);
228 }
229 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800230
231 return cfg;
232}
233
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800234int arch_init_chip_data(struct irq_desc *desc, int cpu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800235{
236 struct irq_cfg *cfg;
237
238 cfg = desc->chip_data;
239 if (!cfg) {
240 desc->chip_data = get_one_free_irq_cfg(cpu);
241 if (!desc->chip_data) {
242 printk(KERN_ERR "can not alloc irq_cfg\n");
243 BUG_ON(1);
244 }
245 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800246
247 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800248}
249
Yinghai Lu48a1b102008-12-11 00:15:01 -0800250#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
251
252static void
253init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int cpu)
254{
255 struct irq_pin_list *old_entry, *head, *tail, *entry;
256
257 cfg->irq_2_pin = NULL;
258 old_entry = old_cfg->irq_2_pin;
259 if (!old_entry)
260 return;
261
262 entry = get_one_free_irq_2_pin(cpu);
263 if (!entry)
264 return;
265
266 entry->apic = old_entry->apic;
267 entry->pin = old_entry->pin;
268 head = entry;
269 tail = entry;
270 old_entry = old_entry->next;
271 while (old_entry) {
272 entry = get_one_free_irq_2_pin(cpu);
273 if (!entry) {
274 entry = head;
275 while (entry) {
276 head = entry->next;
277 kfree(entry);
278 entry = head;
279 }
280 /* still use the old one */
281 return;
282 }
283 entry->apic = old_entry->apic;
284 entry->pin = old_entry->pin;
285 tail->next = entry;
286 tail = entry;
287 old_entry = old_entry->next;
288 }
289
290 tail->next = NULL;
291 cfg->irq_2_pin = head;
292}
293
294static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
295{
296 struct irq_pin_list *entry, *next;
297
298 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
299 return;
300
301 entry = old_cfg->irq_2_pin;
302
303 while (entry) {
304 next = entry->next;
305 kfree(entry);
306 entry = next;
307 }
308 old_cfg->irq_2_pin = NULL;
309}
310
311void arch_init_copy_chip_data(struct irq_desc *old_desc,
312 struct irq_desc *desc, int cpu)
313{
314 struct irq_cfg *cfg;
315 struct irq_cfg *old_cfg;
316
317 cfg = get_one_free_irq_cfg(cpu);
318
319 if (!cfg)
320 return;
321
322 desc->chip_data = cfg;
323
324 old_cfg = old_desc->chip_data;
325
326 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
327
328 init_copy_irq_2_pin(old_cfg, cfg, cpu);
329}
330
331static void free_irq_cfg(struct irq_cfg *old_cfg)
332{
333 kfree(old_cfg);
334}
335
336void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
337{
338 struct irq_cfg *old_cfg, *cfg;
339
340 old_cfg = old_desc->chip_data;
341 cfg = desc->chip_data;
342
343 if (old_cfg == cfg)
344 return;
345
346 if (old_cfg) {
347 free_irq_2_pin(old_cfg, cfg);
348 free_irq_cfg(old_cfg);
349 old_desc->chip_data = NULL;
350 }
351}
352
Ingo Molnard733e002008-12-17 13:35:51 +0100353static void
354set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800355{
356 struct irq_cfg *cfg = desc->chip_data;
357
358 if (!cfg->move_in_progress) {
359 /* it means that domain is not changed */
Mike Travis7f7ace02009-01-10 21:58:08 -0800360 if (!cpumask_intersects(desc->affinity, mask))
Yinghai Lu48a1b102008-12-11 00:15:01 -0800361 cfg->move_desc_pending = 1;
362 }
363}
364#endif
365
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800366#else
367static struct irq_cfg *irq_cfg(unsigned int irq)
368{
369 return irq < nr_irqs ? irq_cfgx + irq : NULL;
370}
371
372#endif
373
Yinghai Lu48a1b102008-12-11 00:15:01 -0800374#ifndef CONFIG_NUMA_MIGRATE_IRQ_DESC
Mike Travise7986732008-12-16 17:33:52 -0800375static inline void
376set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800377{
378}
Yinghai Lu48a1b102008-12-11 00:15:01 -0800379#endif
Yinghai Lu3145e942008-12-05 18:58:34 -0800380
Linus Torvalds130fe052006-11-01 09:11:00 -0800381struct io_apic {
382 unsigned int index;
383 unsigned int unused[3];
384 unsigned int data;
385};
386
387static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
388{
389 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +0530390 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800391}
392
393static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
394{
395 struct io_apic __iomem *io_apic = io_apic_base(apic);
396 writel(reg, &io_apic->index);
397 return readl(&io_apic->data);
398}
399
400static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
401{
402 struct io_apic __iomem *io_apic = io_apic_base(apic);
403 writel(reg, &io_apic->index);
404 writel(value, &io_apic->data);
405}
406
407/*
408 * Re-write a value: to be used for read-modify-write
409 * cycles where the read already set up the index register.
410 *
411 * Older SiS APIC requires we rewrite the index register
412 */
413static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
414{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200415 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200416
417 if (sis_apic_bug)
418 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800419 writel(value, &io_apic->data);
420}
421
Yinghai Lu3145e942008-12-05 18:58:34 -0800422static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700423{
424 struct irq_pin_list *entry;
425 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700426
427 spin_lock_irqsave(&ioapic_lock, flags);
428 entry = cfg->irq_2_pin;
429 for (;;) {
430 unsigned int reg;
431 int pin;
432
433 if (!entry)
434 break;
435 pin = entry->pin;
436 reg = io_apic_read(entry->apic, 0x10 + pin*2);
437 /* Is the remote IRR bit set? */
438 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
439 spin_unlock_irqrestore(&ioapic_lock, flags);
440 return true;
441 }
442 if (!entry->next)
443 break;
444 entry = entry->next;
445 }
446 spin_unlock_irqrestore(&ioapic_lock, flags);
447
448 return false;
449}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700450
Andi Kleencf4c6a22006-09-26 10:52:30 +0200451union entry_union {
452 struct { u32 w1, w2; };
453 struct IO_APIC_route_entry entry;
454};
455
456static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
457{
458 union entry_union eu;
459 unsigned long flags;
460 spin_lock_irqsave(&ioapic_lock, flags);
461 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
462 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
463 spin_unlock_irqrestore(&ioapic_lock, flags);
464 return eu.entry;
465}
466
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800467/*
468 * When we write a new IO APIC routing entry, we need to write the high
469 * word first! If the mask bit in the low word is clear, we will enable
470 * the interrupt, and we need to make sure the entry is fully populated
471 * before that happens.
472 */
Andi Kleend15512f2006-12-07 02:14:07 +0100473static void
474__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
475{
476 union entry_union eu;
477 eu.entry = e;
478 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
479 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
480}
481
Andi Kleencf4c6a22006-09-26 10:52:30 +0200482static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
483{
484 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200485 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100486 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800487 spin_unlock_irqrestore(&ioapic_lock, flags);
488}
489
490/*
491 * When we mask an IO APIC routing entry, we need to write the low
492 * word first, in order to set the mask bit before we change the
493 * high bits!
494 */
495static void ioapic_mask_entry(int apic, int pin)
496{
497 unsigned long flags;
498 union entry_union eu = { .entry.mask = 1 };
499
500 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200501 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
502 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
503 spin_unlock_irqrestore(&ioapic_lock, flags);
504}
505
Yinghai Lu497c9a12008-08-19 20:50:28 -0700506#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -0800507static void send_cleanup_vector(struct irq_cfg *cfg)
508{
509 cpumask_var_t cleanup_mask;
510
511 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
512 unsigned int i;
513 cfg->move_cleanup_count = 0;
514 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
515 cfg->move_cleanup_count++;
516 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
517 send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
518 } else {
519 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
520 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
521 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
522 free_cpumask_var(cleanup_mask);
523 }
524 cfg->move_in_progress = 0;
525}
526
Yinghai Lu3145e942008-12-05 18:58:34 -0800527static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700528{
529 int apic, pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700530 struct irq_pin_list *entry;
Yinghai Lu3145e942008-12-05 18:58:34 -0800531 u8 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700532
Yinghai Lu497c9a12008-08-19 20:50:28 -0700533 entry = cfg->irq_2_pin;
534 for (;;) {
535 unsigned int reg;
536
537 if (!entry)
538 break;
539
540 apic = entry->apic;
541 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200542#ifdef CONFIG_INTR_REMAP
543 /*
544 * With interrupt-remapping, destination information comes
545 * from interrupt-remapping table entry.
546 */
547 if (!irq_remapped(irq))
548 io_apic_write(apic, 0x11 + pin*2, dest);
549#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700550 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200551#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700552 reg = io_apic_read(apic, 0x10 + pin*2);
553 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
554 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200555 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700556 if (!entry->next)
557 break;
558 entry = entry->next;
559 }
560}
Yinghai Luefa25592008-08-19 20:50:36 -0700561
Mike Travise7986732008-12-16 17:33:52 -0800562static int
563assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
Yinghai Luefa25592008-08-19 20:50:36 -0700564
Mike Travis22f65d32008-12-16 17:33:56 -0800565/*
566 * Either sets desc->affinity to a valid value, and returns cpu_mask_to_apicid
567 * of that, or returns BAD_APICID and leaves desc->affinity untouched.
568 */
569static unsigned int
570set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700571{
572 struct irq_cfg *cfg;
Yinghai Lu3145e942008-12-05 18:58:34 -0800573 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700574
Rusty Russell0de26522008-12-13 21:20:26 +1030575 if (!cpumask_intersects(mask, cpu_online_mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800576 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700577
Yinghai Lu3145e942008-12-05 18:58:34 -0800578 irq = desc->irq;
579 cfg = desc->chip_data;
580 if (assign_irq_vector(irq, cfg, mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800581 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700582
Mike Travis7f7ace02009-01-10 21:58:08 -0800583 cpumask_and(desc->affinity, cfg->domain, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -0800584 set_extra_move_desc(desc, mask);
Mike Travis7f7ace02009-01-10 21:58:08 -0800585 return 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#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100801void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 unsigned int cfg;
804
805 /*
806 * Wait for idle.
807 */
808 apic_wait_icr_idle();
809 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
810 /*
811 * Send the IPI. The write to APIC_ICR fires this off.
812 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100813 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200815#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Ingo Molnar54168ed2008-08-20 09:07:45 +0200817#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/*
819 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
820 * specific CPU-side IRQs.
821 */
822
823#define MAX_PIRQS 8
824static int pirq_entries [MAX_PIRQS];
825static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827static int __init ioapic_pirq_setup(char *str)
828{
829 int i, max;
830 int ints[MAX_PIRQS+1];
831
832 get_options(str, ARRAY_SIZE(ints), ints);
833
834 for (i = 0; i < MAX_PIRQS; i++)
835 pirq_entries[i] = -1;
836
837 pirqs_enabled = 1;
838 apic_printk(APIC_VERBOSE, KERN_INFO
839 "PIRQ redirection, working around broken MP-BIOS.\n");
840 max = MAX_PIRQS;
841 if (ints[0] < MAX_PIRQS)
842 max = ints[0];
843
844 for (i = 0; i < max; i++) {
845 apic_printk(APIC_VERBOSE, KERN_DEBUG
846 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
847 /*
848 * PIRQs are mapped upside down, usually.
849 */
850 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
851 }
852 return 1;
853}
854
855__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200856#endif /* CONFIG_X86_32 */
857
858#ifdef CONFIG_INTR_REMAP
859/* I/O APIC RTE contents at the OS boot up */
860static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
861
862/*
863 * Saves and masks all the unmasked IO-APIC RTE's
864 */
865int save_mask_IO_APIC_setup(void)
866{
867 union IO_APIC_reg_01 reg_01;
868 unsigned long flags;
869 int apic, pin;
870
871 /*
872 * The number of IO-APIC IRQ registers (== #pins):
873 */
874 for (apic = 0; apic < nr_ioapics; apic++) {
875 spin_lock_irqsave(&ioapic_lock, flags);
876 reg_01.raw = io_apic_read(apic, 1);
877 spin_unlock_irqrestore(&ioapic_lock, flags);
878 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
879 }
880
881 for (apic = 0; apic < nr_ioapics; apic++) {
882 early_ioapic_entries[apic] =
883 kzalloc(sizeof(struct IO_APIC_route_entry) *
884 nr_ioapic_registers[apic], GFP_KERNEL);
885 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400886 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200887 }
888
889 for (apic = 0; apic < nr_ioapics; apic++)
890 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
891 struct IO_APIC_route_entry entry;
892
893 entry = early_ioapic_entries[apic][pin] =
894 ioapic_read_entry(apic, pin);
895 if (!entry.mask) {
896 entry.mask = 1;
897 ioapic_write_entry(apic, pin, entry);
898 }
899 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400900
Ingo Molnar54168ed2008-08-20 09:07:45 +0200901 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400902
903nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400904 while (apic >= 0)
905 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400906 memset(early_ioapic_entries, 0,
907 ARRAY_SIZE(early_ioapic_entries));
908
909 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200910}
911
912void restore_IO_APIC_setup(void)
913{
914 int apic, pin;
915
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400916 for (apic = 0; apic < nr_ioapics; apic++) {
917 if (!early_ioapic_entries[apic])
918 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200919 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
920 ioapic_write_entry(apic, pin,
921 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400922 kfree(early_ioapic_entries[apic]);
923 early_ioapic_entries[apic] = NULL;
924 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200925}
926
927void reinit_intr_remapped_IO_APIC(int intr_remapping)
928{
929 /*
930 * for now plain restore of previous settings.
931 * TBD: In the case of OS enabling interrupt-remapping,
932 * IO-APIC RTE's need to be setup to point to interrupt-remapping
933 * table entries. for now, do a plain restore, and wait for
934 * the setup_IO_APIC_irqs() to do proper initialization.
935 */
936 restore_IO_APIC_setup();
937}
938#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940/*
941 * Find the IRQ entry number of a certain pin.
942 */
943static int find_irq_entry(int apic, int pin, int type)
944{
945 int i;
946
947 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530948 if (mp_irqs[i].irqtype == type &&
949 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
950 mp_irqs[i].dstapic == MP_APIC_ALL) &&
951 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return i;
953
954 return -1;
955}
956
957/*
958 * Find the pin to which IRQ[irq] (ISA) is connected
959 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800960static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
962 int i;
963
964 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530965 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300967 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530968 (mp_irqs[i].irqtype == type) &&
969 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530971 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 }
973 return -1;
974}
975
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800976static int __init find_isa_irq_apic(int irq, int type)
977{
978 int i;
979
980 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530981 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800982
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300983 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530984 (mp_irqs[i].irqtype == type) &&
985 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800986 break;
987 }
988 if (i < mp_irq_entries) {
989 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200990 for(apic = 0; apic < nr_ioapics; apic++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530991 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800992 return apic;
993 }
994 }
995
996 return -1;
997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/*
1000 * Find a specific PCI IRQ entry.
1001 * Not an __init, possibly needed by modules
1002 */
1003static int pin_2_irq(int idx, int apic, int pin);
1004
1005int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
1006{
1007 int apic, i, best_guess = -1;
1008
Ingo Molnar54168ed2008-08-20 09:07:45 +02001009 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1010 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +04001011 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001012 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return -1;
1014 }
1015 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301016 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 for (apic = 0; apic < nr_ioapics; apic++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301019 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1020 mp_irqs[i].dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 break;
1022
Alexey Starikovskiy47cab822008-03-20 14:54:30 +03001023 if (!test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301024 !mp_irqs[i].irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 (bus == lbus) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301026 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1027 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 if (!(apic || IO_APIC_IRQ(irq)))
1030 continue;
1031
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301032 if (pin == (mp_irqs[i].srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return irq;
1034 /*
1035 * Use the first all-but-pin matching entry as a
1036 * best-guess fuzzy result for broken mptables.
1037 */
1038 if (best_guess < 0)
1039 best_guess = irq;
1040 }
1041 }
1042 return best_guess;
1043}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001044
Alexey Dobriyan129f6942005-06-23 00:08:33 -07001045EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001047#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
1049 * EISA Edge/Level control register, ELCR
1050 */
1051static int EISA_ELCR(unsigned int irq)
1052{
Yinghai Lu99d093d2008-12-05 18:58:32 -08001053 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 unsigned int port = 0x4d0 + (irq >> 3);
1055 return (inb(port) >> (irq & 7)) & 1;
1056 }
1057 apic_printk(APIC_VERBOSE, KERN_INFO
1058 "Broken MPtable reports ISA irq %d\n", irq);
1059 return 0;
1060}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001061
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001062#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001064/* ISA interrupts are always polarity zero edge triggered,
1065 * when listed as conforming in the MP table. */
1066
1067#define default_ISA_trigger(idx) (0)
1068#define default_ISA_polarity(idx) (0)
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070/* EISA interrupts are always polarity zero and can be edge or level
1071 * trigger depending on the ELCR value. If an interrupt is listed as
1072 * EISA conforming in the MP table, that means its trigger type must
1073 * be read in from the ELCR */
1074
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301075#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001076#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
1078/* PCI interrupts are always polarity one level triggered,
1079 * when listed as conforming in the MP table. */
1080
1081#define default_PCI_trigger(idx) (1)
1082#define default_PCI_polarity(idx) (1)
1083
1084/* MCA interrupts are always polarity zero level triggered,
1085 * when listed as conforming in the MP table. */
1086
1087#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001088#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Shaohua Li61fd47e2007-11-17 01:05:28 -05001090static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301092 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 int polarity;
1094
1095 /*
1096 * Determine IRQ line polarity (high active or low active):
1097 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301098 switch (mp_irqs[idx].irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001100 case 0: /* conforms, ie. bus-type dependent polarity */
1101 if (test_bit(bus, mp_bus_not_pci))
1102 polarity = default_ISA_polarity(idx);
1103 else
1104 polarity = default_PCI_polarity(idx);
1105 break;
1106 case 1: /* high active */
1107 {
1108 polarity = 0;
1109 break;
1110 }
1111 case 2: /* reserved */
1112 {
1113 printk(KERN_WARNING "broken BIOS!!\n");
1114 polarity = 1;
1115 break;
1116 }
1117 case 3: /* low active */
1118 {
1119 polarity = 1;
1120 break;
1121 }
1122 default: /* invalid */
1123 {
1124 printk(KERN_WARNING "broken BIOS!!\n");
1125 polarity = 1;
1126 break;
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129 return polarity;
1130}
1131
1132static int MPBIOS_trigger(int idx)
1133{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301134 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 int trigger;
1136
1137 /*
1138 * Determine IRQ trigger mode (edge or level sensitive):
1139 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301140 switch ((mp_irqs[idx].irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001142 case 0: /* conforms, ie. bus-type dependent */
1143 if (test_bit(bus, mp_bus_not_pci))
1144 trigger = default_ISA_trigger(idx);
1145 else
1146 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001147#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001148 switch (mp_bus_id_to_type[bus]) {
1149 case MP_BUS_ISA: /* ISA pin */
1150 {
1151 /* set before the switch */
1152 break;
1153 }
1154 case MP_BUS_EISA: /* EISA pin */
1155 {
1156 trigger = default_EISA_trigger(idx);
1157 break;
1158 }
1159 case MP_BUS_PCI: /* PCI pin */
1160 {
1161 /* set before the switch */
1162 break;
1163 }
1164 case MP_BUS_MCA: /* MCA pin */
1165 {
1166 trigger = default_MCA_trigger(idx);
1167 break;
1168 }
1169 default:
1170 {
1171 printk(KERN_WARNING "broken BIOS!!\n");
1172 trigger = 1;
1173 break;
1174 }
1175 }
1176#endif
1177 break;
1178 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001179 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001180 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001181 break;
1182 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001183 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001184 {
1185 printk(KERN_WARNING "broken BIOS!!\n");
1186 trigger = 1;
1187 break;
1188 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001189 case 3: /* level */
1190 {
1191 trigger = 1;
1192 break;
1193 }
1194 default: /* invalid */
1195 {
1196 printk(KERN_WARNING "broken BIOS!!\n");
1197 trigger = 0;
1198 break;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 return trigger;
1202}
1203
1204static inline int irq_polarity(int idx)
1205{
1206 return MPBIOS_polarity(idx);
1207}
1208
1209static inline int irq_trigger(int idx)
1210{
1211 return MPBIOS_trigger(idx);
1212}
1213
Yinghai Luefa25592008-08-19 20:50:36 -07001214int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215static int pin_2_irq(int idx, int apic, int pin)
1216{
1217 int irq, i;
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301218 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /*
1221 * Debugging check, we are in big trouble if this message pops up!
1222 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301223 if (mp_irqs[idx].dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1225
Ingo Molnar54168ed2008-08-20 09:07:45 +02001226 if (test_bit(bus, mp_bus_not_pci)) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301227 irq = mp_irqs[idx].srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001228 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001229 /*
1230 * PCI IRQs are mapped in order
1231 */
1232 i = irq = 0;
1233 while (i < apic)
1234 irq += nr_ioapic_registers[i++];
1235 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001236 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001237 * For MPS mode, so far only needed by ES7000 platform
1238 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001239 if (ioapic_renumber_irq)
1240 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 }
1242
Ingo Molnar54168ed2008-08-20 09:07:45 +02001243#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /*
1245 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1246 */
1247 if ((pin >= 16) && (pin <= 23)) {
1248 if (pirq_entries[pin-16] != -1) {
1249 if (!pirq_entries[pin-16]) {
1250 apic_printk(APIC_VERBOSE, KERN_DEBUG
1251 "disabling PIRQ%d\n", pin-16);
1252 } else {
1253 irq = pirq_entries[pin-16];
1254 apic_printk(APIC_VERBOSE, KERN_DEBUG
1255 "using PIRQ%d -> IRQ %d\n",
1256 pin-16, irq);
1257 }
1258 }
1259 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001260#endif
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return irq;
1263}
1264
Yinghai Lu497c9a12008-08-19 20:50:28 -07001265void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001267 /* Used to the online set of cpus does not change
1268 * during assign_irq_vector.
1269 */
1270 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Yinghai Lu497c9a12008-08-19 20:50:28 -07001273void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001274{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001275 spin_unlock(&vector_lock);
1276}
1277
Mike Travise7986732008-12-16 17:33:52 -08001278static int
1279__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001280{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001281 /*
1282 * NOTE! The local APIC isn't very good at handling
1283 * multiple interrupts at the same interrupt level.
1284 * As the interrupt level is determined by taking the
1285 * vector number and shifting that right by 4, we
1286 * want to spread these out a bit so that they don't
1287 * all fall in the same interrupt level.
1288 *
1289 * Also, we've got to be careful not to trash gate
1290 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1291 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001292 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1293 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001294 int cpu, err;
1295 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001296
Ingo Molnar54168ed2008-08-20 09:07:45 +02001297 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1298 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001299
Mike Travis22f65d32008-12-16 17:33:56 -08001300 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1301 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001302
Ingo Molnar54168ed2008-08-20 09:07:45 +02001303 old_vector = cfg->vector;
1304 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001305 cpumask_and(tmp_mask, mask, cpu_online_mask);
1306 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1307 if (!cpumask_empty(tmp_mask)) {
1308 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001309 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001310 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001311 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001312
Mike Travise7986732008-12-16 17:33:52 -08001313 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001314 err = -ENOSPC;
1315 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001316 int new_cpu;
1317 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001318
Mike Travis22f65d32008-12-16 17:33:56 -08001319 vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001320
Ingo Molnar54168ed2008-08-20 09:07:45 +02001321 vector = current_vector;
1322 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001323next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001324 vector += 8;
1325 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001326 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001327 offset = (offset + 1) % 8;
1328 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001329 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001330 if (unlikely(current_vector == vector))
1331 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001332
1333 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001334 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001335
Mike Travis22f65d32008-12-16 17:33:56 -08001336 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001337 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1338 goto next;
1339 /* Found one! */
1340 current_vector = vector;
1341 current_offset = offset;
1342 if (old_vector) {
1343 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001344 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001345 }
Mike Travis22f65d32008-12-16 17:33:56 -08001346 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001347 per_cpu(vector_irq, new_cpu)[vector] = irq;
1348 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001349 cpumask_copy(cfg->domain, tmp_mask);
1350 err = 0;
1351 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001352 }
Mike Travis22f65d32008-12-16 17:33:56 -08001353 free_cpumask_var(tmp_mask);
1354 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001355}
1356
Mike Travise7986732008-12-16 17:33:52 -08001357static int
1358assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001359{
1360 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001361 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001362
1363 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001364 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001365 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001366 return err;
1367}
1368
Yinghai Lu3145e942008-12-05 18:58:34 -08001369static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001370{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001371 int cpu, vector;
1372
Yinghai Lu497c9a12008-08-19 20:50:28 -07001373 BUG_ON(!cfg->vector);
1374
1375 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001376 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001377 per_cpu(vector_irq, cpu)[vector] = -1;
1378
1379 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001380 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001381
1382 if (likely(!cfg->move_in_progress))
1383 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001384 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001385 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1386 vector++) {
1387 if (per_cpu(vector_irq, cpu)[vector] != irq)
1388 continue;
1389 per_cpu(vector_irq, cpu)[vector] = -1;
1390 break;
1391 }
1392 }
1393 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001394}
1395
1396void __setup_vector_irq(int cpu)
1397{
1398 /* Initialize vector_irq on a new cpu */
1399 /* This function must be called with vector_lock held */
1400 int irq, vector;
1401 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001402 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001403
1404 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001405 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001406 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001407 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001408 continue;
1409 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001410 per_cpu(vector_irq, cpu)[vector] = irq;
1411 }
1412 /* Mark the free vectors */
1413 for (vector = 0; vector < NR_VECTORS; ++vector) {
1414 irq = per_cpu(vector_irq, cpu)[vector];
1415 if (irq < 0)
1416 continue;
1417
1418 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001419 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001420 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001421 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001422}
Glauber Costa3fde6902008-05-28 20:34:19 -07001423
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001424static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001425#ifdef CONFIG_INTR_REMAP
1426static struct irq_chip ir_ioapic_chip;
1427#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Ingo Molnar54168ed2008-08-20 09:07:45 +02001429#define IOAPIC_AUTO -1
1430#define IOAPIC_EDGE 0
1431#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001433#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001434static inline int IO_APIC_irq_trigger(int irq)
1435{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001436 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001437
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001438 for (apic = 0; apic < nr_ioapics; apic++) {
1439 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1440 idx = find_irq_entry(apic, pin, mp_INT);
1441 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1442 return irq_trigger(idx);
1443 }
1444 }
1445 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001446 * nonexistent IRQs are edge default
1447 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001448 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001449}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001450#else
1451static inline int IO_APIC_irq_trigger(int irq)
1452{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001453 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001454}
1455#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001456
Yinghai Lu3145e942008-12-05 18:58:34 -08001457static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
Yinghai Lu199751d2008-08-19 20:50:27 -07001459
Jan Beulich6ebcc002006-06-26 13:56:46 +02001460 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001461 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001462 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001463 else
1464 desc->status &= ~IRQ_LEVEL;
1465
Ingo Molnar54168ed2008-08-20 09:07:45 +02001466#ifdef CONFIG_INTR_REMAP
1467 if (irq_remapped(irq)) {
1468 desc->status |= IRQ_MOVE_PCNTXT;
1469 if (trigger)
1470 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1471 handle_fasteoi_irq,
1472 "fasteoi");
1473 else
1474 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1475 handle_edge_irq, "edge");
1476 return;
1477 }
1478#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001479 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1480 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001481 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001482 handle_fasteoi_irq,
1483 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001484 else
Ingo Molnara460e742006-10-17 00:10:03 -07001485 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001486 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001487}
1488
1489static int setup_ioapic_entry(int apic, int irq,
1490 struct IO_APIC_route_entry *entry,
1491 unsigned int destination, int trigger,
1492 int polarity, int vector)
1493{
1494 /*
1495 * add it to the IO-APIC irq-routing table:
1496 */
1497 memset(entry,0,sizeof(*entry));
1498
Ingo Molnar54168ed2008-08-20 09:07:45 +02001499#ifdef CONFIG_INTR_REMAP
1500 if (intr_remapping_enabled) {
1501 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1502 struct irte irte;
1503 struct IR_IO_APIC_route_entry *ir_entry =
1504 (struct IR_IO_APIC_route_entry *) entry;
1505 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001506
Ingo Molnar54168ed2008-08-20 09:07:45 +02001507 if (!iommu)
1508 panic("No mapping iommu for ioapic %d\n", apic);
1509
1510 index = alloc_irte(iommu, irq, 1);
1511 if (index < 0)
1512 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1513
1514 memset(&irte, 0, sizeof(irte));
1515
1516 irte.present = 1;
1517 irte.dst_mode = INT_DEST_MODE;
1518 irte.trigger_mode = trigger;
1519 irte.dlvry_mode = INT_DELIVERY_MODE;
1520 irte.vector = vector;
1521 irte.dest_id = IRTE_DEST(destination);
1522
1523 modify_irte(irq, &irte);
1524
1525 ir_entry->index2 = (index >> 15) & 0x1;
1526 ir_entry->zero = 0;
1527 ir_entry->format = 1;
1528 ir_entry->index = (index & 0x7fff);
1529 } else
1530#endif
1531 {
1532 entry->delivery_mode = INT_DELIVERY_MODE;
1533 entry->dest_mode = INT_DEST_MODE;
1534 entry->dest = destination;
1535 }
1536
1537 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001538 entry->trigger = trigger;
1539 entry->polarity = polarity;
1540 entry->vector = vector;
1541
1542 /* Mask level triggered irqs.
1543 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1544 */
1545 if (trigger)
1546 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001547 return 0;
1548}
1549
Yinghai Lu3145e942008-12-05 18:58:34 -08001550static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001551 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001552{
1553 struct irq_cfg *cfg;
1554 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001555 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001556
1557 if (!IO_APIC_IRQ(irq))
1558 return;
1559
Yinghai Lu3145e942008-12-05 18:58:34 -08001560 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001561
Mike Travis22f65d32008-12-16 17:33:56 -08001562 if (assign_irq_vector(irq, cfg, TARGET_CPUS))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001563 return;
1564
Mike Travis22f65d32008-12-16 17:33:56 -08001565 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001566
1567 apic_printk(APIC_VERBOSE,KERN_DEBUG
1568 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1569 "IRQ %d Mode:%i Active:%i)\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301570 apic, mp_ioapics[apic].apicid, pin, cfg->vector,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001571 irq, trigger, polarity);
1572
1573
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301574 if (setup_ioapic_entry(mp_ioapics[apic].apicid, irq, &entry,
Mike Travis22f65d32008-12-16 17:33:56 -08001575 dest, trigger, polarity, cfg->vector)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001576 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301577 mp_ioapics[apic].apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001578 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001579 return;
1580 }
1581
Yinghai Lu3145e942008-12-05 18:58:34 -08001582 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001583 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001584 disable_8259A_irq(irq);
1585
1586 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587}
1588
1589static void __init setup_IO_APIC_irqs(void)
1590{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001591 int apic, pin, idx, irq;
1592 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001593 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001594 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001595 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1598
1599 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001600 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001602 idx = find_irq_entry(apic, pin, mp_INT);
1603 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001604 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001605 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001606 apic_printk(APIC_VERBOSE,
1607 KERN_DEBUG " %d-%d",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301608 mp_ioapics[apic].apicid, pin);
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001609 } else
1610 apic_printk(APIC_VERBOSE, " %d-%d",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301611 mp_ioapics[apic].apicid, pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001612 continue;
1613 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001614 if (notcon) {
1615 apic_printk(APIC_VERBOSE,
1616 " (apicid-pin) not connected\n");
1617 notcon = 0;
1618 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001619
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001620 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001621#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001622 if (multi_timer_check(apic, irq))
1623 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001624#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001625 desc = irq_to_desc_alloc_cpu(irq, cpu);
1626 if (!desc) {
1627 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1628 continue;
1629 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001630 cfg = desc->chip_data;
1631 add_pin_to_irq_cpu(cfg, cpu, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001632
Yinghai Lu3145e942008-12-05 18:58:34 -08001633 setup_IO_APIC_irq(apic, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001634 irq_trigger(idx), irq_polarity(idx));
1635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001638 if (notcon)
1639 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001640 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
1643/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001644 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001646static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1647 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
1649 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Ingo Molnar54168ed2008-08-20 09:07:45 +02001651#ifdef CONFIG_INTR_REMAP
1652 if (intr_remapping_enabled)
1653 return;
1654#endif
1655
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001656 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /*
1659 * We use logical delivery to get the timer IRQ
1660 * to the first CPU.
1661 */
1662 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001663 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001664 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 entry.delivery_mode = INT_DELIVERY_MODE;
1666 entry.polarity = 0;
1667 entry.trigger = 0;
1668 entry.vector = vector;
1669
1670 /*
1671 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001672 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001674 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 /*
1677 * Add it to the IO-APIC irq-routing table:
1678 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001679 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001682
1683__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
1685 int apic, i;
1686 union IO_APIC_reg_00 reg_00;
1687 union IO_APIC_reg_01 reg_01;
1688 union IO_APIC_reg_02 reg_02;
1689 union IO_APIC_reg_03 reg_03;
1690 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001691 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001692 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001693 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 if (apic_verbosity == APIC_QUIET)
1696 return;
1697
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001698 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 for (i = 0; i < nr_ioapics; i++)
1700 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301701 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 /*
1704 * We are a bit conservative about what we expect. We have to
1705 * know about every hardware change ASAP.
1706 */
1707 printk(KERN_INFO "testing the IO APIC.......................\n");
1708
1709 for (apic = 0; apic < nr_ioapics; apic++) {
1710
1711 spin_lock_irqsave(&ioapic_lock, flags);
1712 reg_00.raw = io_apic_read(apic, 0);
1713 reg_01.raw = io_apic_read(apic, 1);
1714 if (reg_01.bits.version >= 0x10)
1715 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001716 if (reg_01.bits.version >= 0x20)
1717 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 spin_unlock_irqrestore(&ioapic_lock, flags);
1719
Ingo Molnar54168ed2008-08-20 09:07:45 +02001720 printk("\n");
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301721 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1723 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1724 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1725 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Ingo Molnar54168ed2008-08-20 09:07:45 +02001727 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1731 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 /*
1734 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1735 * but the value of reg_02 is read as the previous read register
1736 * value, so ignore it if reg_02 == reg_01.
1737 */
1738 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1739 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1740 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742
1743 /*
1744 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1745 * or reg_03, but the value of reg_0[23] is read as the previous read
1746 * register value, so ignore it if reg_03 == reg_0[12].
1747 */
1748 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1749 reg_03.raw != reg_01.raw) {
1750 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1751 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753
1754 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1755
Yinghai Lud83e94a2008-08-19 20:50:33 -07001756 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1757 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 for (i = 0; i <= reg_01.bits.entries; i++) {
1760 struct IO_APIC_route_entry entry;
1761
Andi Kleencf4c6a22006-09-26 10:52:30 +02001762 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Ingo Molnar54168ed2008-08-20 09:07:45 +02001764 printk(KERN_DEBUG " %02x %03X ",
1765 i,
1766 entry.dest
1767 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1770 entry.mask,
1771 entry.trigger,
1772 entry.irr,
1773 entry.polarity,
1774 entry.delivery_status,
1775 entry.dest_mode,
1776 entry.delivery_mode,
1777 entry.vector
1778 );
1779 }
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001782 for_each_irq_desc(irq, desc) {
1783 struct irq_pin_list *entry;
1784
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001785 cfg = desc->chip_data;
1786 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001787 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001789 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 for (;;) {
1791 printk("-> %d:%d", entry->apic, entry->pin);
1792 if (!entry->next)
1793 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001794 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796 printk("\n");
1797 }
1798
1799 printk(KERN_INFO ".................................... done.\n");
1800
1801 return;
1802}
1803
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001804__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 unsigned int v;
1807 int i, j;
1808
1809 if (apic_verbosity == APIC_QUIET)
1810 return;
1811
1812 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1813 for (i = 0; i < 8; i++) {
1814 v = apic_read(base + i*0x10);
1815 for (j = 0; j < 32; j++) {
1816 if (v & (1<<j))
1817 printk("1");
1818 else
1819 printk("0");
1820 }
1821 printk("\n");
1822 }
1823}
1824
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001825__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001828 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 if (apic_verbosity == APIC_QUIET)
1831 return;
1832
1833 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1834 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001835 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001836 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 v = apic_read(APIC_LVR);
1838 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1839 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001840 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 v = apic_read(APIC_TASKPRI);
1843 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1844
Ingo Molnar54168ed2008-08-20 09:07:45 +02001845 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001846 if (!APIC_XAPIC(ver)) {
1847 v = apic_read(APIC_ARBPRI);
1848 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1849 v & APIC_ARBPRI_MASK);
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 v = apic_read(APIC_PROCPRI);
1852 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1853 }
1854
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001855 /*
1856 * Remote read supported only in the 82489DX and local APIC for
1857 * Pentium processors.
1858 */
1859 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1860 v = apic_read(APIC_RRR);
1861 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1862 }
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 v = apic_read(APIC_LDR);
1865 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001866 if (!x2apic_enabled()) {
1867 v = apic_read(APIC_DFR);
1868 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 v = apic_read(APIC_SPIV);
1871 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1872
1873 printk(KERN_DEBUG "... APIC ISR field:\n");
1874 print_APIC_bitfield(APIC_ISR);
1875 printk(KERN_DEBUG "... APIC TMR field:\n");
1876 print_APIC_bitfield(APIC_TMR);
1877 printk(KERN_DEBUG "... APIC IRR field:\n");
1878 print_APIC_bitfield(APIC_IRR);
1879
Ingo Molnar54168ed2008-08-20 09:07:45 +02001880 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1881 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001883
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 v = apic_read(APIC_ESR);
1885 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1886 }
1887
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001888 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001889 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1890 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 v = apic_read(APIC_LVTT);
1893 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1894
1895 if (maxlvt > 3) { /* PC is LVT#4. */
1896 v = apic_read(APIC_LVTPC);
1897 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1898 }
1899 v = apic_read(APIC_LVT0);
1900 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1901 v = apic_read(APIC_LVT1);
1902 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1903
1904 if (maxlvt > 2) { /* ERR is LVT#3. */
1905 v = apic_read(APIC_LVTERR);
1906 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1907 }
1908
1909 v = apic_read(APIC_TMICT);
1910 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1911 v = apic_read(APIC_TMCCT);
1912 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1913 v = apic_read(APIC_TDCR);
1914 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1915 printk("\n");
1916}
1917
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001918__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001920 int cpu;
1921
1922 preempt_disable();
1923 for_each_online_cpu(cpu)
1924 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1925 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001928__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 unsigned int v;
1931 unsigned long flags;
1932
1933 if (apic_verbosity == APIC_QUIET)
1934 return;
1935
1936 printk(KERN_DEBUG "\nprinting PIC contents\n");
1937
1938 spin_lock_irqsave(&i8259A_lock, flags);
1939
1940 v = inb(0xa1) << 8 | inb(0x21);
1941 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1942
1943 v = inb(0xa0) << 8 | inb(0x20);
1944 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1945
Ingo Molnar54168ed2008-08-20 09:07:45 +02001946 outb(0x0b,0xa0);
1947 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001949 outb(0x0a,0xa0);
1950 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 spin_unlock_irqrestore(&i8259A_lock, flags);
1953
1954 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1955
1956 v = inb(0x4d1) << 8 | inb(0x4d0);
1957 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1958}
1959
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001960__apicdebuginit(int) print_all_ICs(void)
1961{
1962 print_PIC();
1963 print_all_local_APICs();
1964 print_IO_APIC();
1965
1966 return 0;
1967}
1968
1969fs_initcall(print_all_ICs);
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Yinghai Luefa25592008-08-19 20:50:36 -07001972/* Where if anywhere is the i8259 connect in external int mode */
1973static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1974
Ingo Molnar54168ed2008-08-20 09:07:45 +02001975void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001978 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001979 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 unsigned long flags;
1981
Ingo Molnar54168ed2008-08-20 09:07:45 +02001982#ifdef CONFIG_X86_32
1983 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (!pirqs_enabled)
1985 for (i = 0; i < MAX_PIRQS; i++)
1986 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001987#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 /*
1990 * The number of IO-APIC IRQ registers (== #pins):
1991 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001992 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001994 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001996 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1997 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001998 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001999 int pin;
2000 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01002001 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002002 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02002003 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002004
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002005 /* If the interrupt line is enabled and in ExtInt mode
2006 * I have found the pin where the i8259 is connected.
2007 */
2008 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
2009 ioapic_i8259.apic = apic;
2010 ioapic_i8259.pin = pin;
2011 goto found_i8259;
2012 }
2013 }
2014 }
2015 found_i8259:
2016 /* Look to see what if the MP table has reported the ExtINT */
2017 /* If we could not find the appropriate pin by looking at the ioapic
2018 * the i8259 probably is not connected the ioapic but give the
2019 * mptable a chance anyway.
2020 */
2021 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
2022 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
2023 /* Trust the MP table if nothing is setup in the hardware */
2024 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
2025 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
2026 ioapic_i8259.pin = i8259_pin;
2027 ioapic_i8259.apic = i8259_apic;
2028 }
2029 /* Complain if the MP table and the hardware disagree */
2030 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
2031 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
2032 {
2033 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035
2036 /*
2037 * Do not trust the IO-APIC being empty at bootup
2038 */
2039 clear_IO_APIC();
2040}
2041
2042/*
2043 * Not an __init, needed by the reboot code
2044 */
2045void disable_IO_APIC(void)
2046{
2047 /*
2048 * Clear the IO-APIC before rebooting:
2049 */
2050 clear_IO_APIC();
2051
Eric W. Biederman650927e2005-06-25 14:57:44 -07002052 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02002053 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07002054 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02002055 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07002056 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002057 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07002058 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07002059
2060 memset(&entry, 0, sizeof(entry));
2061 entry.mask = 0; /* Enabled */
2062 entry.trigger = 0; /* Edge */
2063 entry.irr = 0;
2064 entry.polarity = 0; /* High */
2065 entry.delivery_status = 0;
2066 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002067 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07002068 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002069 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07002070
2071 /*
2072 * Add it to the IO-APIC irq-routing table:
2073 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02002074 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07002075 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002076
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002077 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078}
2079
Ingo Molnar54168ed2008-08-20 09:07:45 +02002080#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081/*
2082 * function to set the IO-APIC physical IDs based on the
2083 * values stored in the MPC table.
2084 *
2085 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2086 */
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static void __init setup_ioapic_ids_from_mpc(void)
2089{
2090 union IO_APIC_reg_00 reg_00;
2091 physid_mask_t phys_id_present_map;
2092 int apic;
2093 int i;
2094 unsigned char old_id;
2095 unsigned long flags;
2096
Yinghai Lua4dbc342008-07-25 02:14:28 -07002097 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07002098 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002101 * Don't check I/O APIC IDs for xAPIC systems. They have
2102 * no meaning without the serial APIC bus.
2103 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002104 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2105 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002106 return;
2107 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 * This is broken; anything with a real cpu count has to
2109 * circumvent this idiocy regardless.
2110 */
2111 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2112
2113 /*
2114 * Set the IOAPIC ID to the value stored in the MPC table.
2115 */
2116 for (apic = 0; apic < nr_ioapics; apic++) {
2117
2118 /* Read the register 0 value */
2119 spin_lock_irqsave(&ioapic_lock, flags);
2120 reg_00.raw = io_apic_read(apic, 0);
2121 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002122
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302123 old_id = mp_ioapics[apic].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302125 if (mp_ioapics[apic].apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302127 apic, mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2129 reg_00.bits.ID);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302130 mp_ioapics[apic].apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 /*
2134 * Sanity check, is the ID really free? Every APIC in a
2135 * system must have a unique ID or we get lots of nice
2136 * 'stuck on smp_invalidate_needed IPI wait' messages.
2137 */
2138 if (check_apicid_used(phys_id_present_map,
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302139 mp_ioapics[apic].apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302141 apic, mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 for (i = 0; i < get_physical_broadcast(); i++)
2143 if (!physid_isset(i, phys_id_present_map))
2144 break;
2145 if (i >= get_physical_broadcast())
2146 panic("Max APIC ID exceeded!\n");
2147 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2148 i);
2149 physid_set(i, phys_id_present_map);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302150 mp_ioapics[apic].apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 } else {
2152 physid_mask_t tmp;
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302153 tmp = apicid_to_cpu_present(mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 apic_printk(APIC_VERBOSE, "Setting %d in the "
2155 "phys_id_present_map\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302156 mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2158 }
2159
2160
2161 /*
2162 * We need to adjust the IRQ routing table
2163 * if the ID changed.
2164 */
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302165 if (old_id != mp_ioapics[apic].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05302167 if (mp_irqs[i].dstapic == old_id)
2168 mp_irqs[i].dstapic
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302169 = mp_ioapics[apic].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 /*
2172 * Read the right value from the MPC table and
2173 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 apic_printk(APIC_VERBOSE, KERN_INFO
2176 "...changing IO-APIC physical APIC ID to %d ...",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302177 mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302179 reg_00.bits.ID = mp_ioapics[apic].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002181 io_apic_write(apic, 0, reg_00.raw);
2182 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 /*
2185 * Sanity check
2186 */
2187 spin_lock_irqsave(&ioapic_lock, flags);
2188 reg_00.raw = io_apic_read(apic, 0);
2189 spin_unlock_irqrestore(&ioapic_lock, flags);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05302190 if (reg_00.bits.ID != mp_ioapics[apic].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 printk("could not set ID!\n");
2192 else
2193 apic_printk(APIC_VERBOSE, " ok.\n");
2194 }
2195}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002198int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002199
2200static int __init notimercheck(char *s)
2201{
2202 no_timer_check = 1;
2203 return 1;
2204}
2205__setup("no_timer_check", notimercheck);
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207/*
2208 * There is a nasty bug in some older SMP boards, their mptable lies
2209 * about the timer IRQ. We do the following to work around the situation:
2210 *
2211 * - timer IRQ defaults to IO-APIC IRQ
2212 * - if this function detects that timer IRQs are defunct, then we fall
2213 * back to ISA timer IRQs
2214 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002215static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002218 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Zachary Amsden8542b202006-12-07 02:14:09 +01002220 if (no_timer_check)
2221 return 1;
2222
Ingo Molnar4aae0702007-12-18 18:05:58 +01002223 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 local_irq_enable();
2225 /* Let ten ticks pass... */
2226 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002227 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 /*
2230 * Expect a few ticks at least, to be sure some possible
2231 * glue logic does not lock up after one or two first
2232 * ticks in a non-ExtINT mode. Also the local APIC
2233 * might have cached one ExtINT interrupt. Finally, at
2234 * least one tick may be lost due to delays.
2235 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002236
2237 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002238 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return 0;
2241}
2242
2243/*
2244 * In the SMP+IOAPIC case it might happen that there are an unspecified
2245 * number of pending IRQ events unhandled. These cases are very rare,
2246 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2247 * better to do it this way as thus we do not have to be aware of
2248 * 'pending' interrupts in the IRQ path, except at this point.
2249 */
2250/*
2251 * Edge triggered needs to resend any interrupt
2252 * that was delayed but this is now handled in the device
2253 * independent code.
2254 */
2255
2256/*
2257 * Starting up a edge-triggered IO-APIC interrupt is
2258 * nasty - we need to make sure that we get the edge.
2259 * If it is already asserted for some reason, we need
2260 * return 1 to indicate that is was pending.
2261 *
2262 * This is not complete - we should be able to fake
2263 * an edge even if it isn't on the 8259A...
2264 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002265
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002266static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 int was_pending = 0;
2269 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002270 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002273 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 disable_8259A_irq(irq);
2275 if (i8259A_irq_pending(irq))
2276 was_pending = 1;
2277 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002278 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002279 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 spin_unlock_irqrestore(&ioapic_lock, flags);
2281
2282 return was_pending;
2283}
2284
Ingo Molnar54168ed2008-08-20 09:07:45 +02002285#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002286static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002288
2289 struct irq_cfg *cfg = irq_cfg(irq);
2290 unsigned long flags;
2291
2292 spin_lock_irqsave(&vector_lock, flags);
Mike Travis22f65d32008-12-16 17:33:56 -08002293 send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002294 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002295
2296 return 1;
2297}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002298#else
2299static int ioapic_retrigger_irq(unsigned int irq)
2300{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002301 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002302
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002303 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002304}
2305#endif
2306
2307/*
2308 * Level and edge triggered IO-APIC interrupts need different handling,
2309 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2310 * handled with the level-triggered descriptor, but that one has slightly
2311 * more overhead. Level-triggered interrupts cannot be handled with the
2312 * edge-triggered handler, without risking IRQ storms and other ugly
2313 * races.
2314 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002315
Yinghai Lu497c9a12008-08-19 20:50:28 -07002316#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002317
2318#ifdef CONFIG_INTR_REMAP
2319static void ir_irq_migration(struct work_struct *work);
2320
2321static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2322
2323/*
2324 * Migrate the IO-APIC irq in the presence of intr-remapping.
2325 *
2326 * For edge triggered, irq migration is a simple atomic update(of vector
2327 * and cpu destination) of IRTE and flush the hardware cache.
2328 *
2329 * For level triggered, we need to modify the io-apic RTE aswell with the update
2330 * vector information, along with modifying IRTE with vector and destination.
2331 * So irq migration for level triggered is little bit more complex compared to
2332 * edge triggered migration. But the good news is, we use the same algorithm
2333 * for level triggered migration as we have today, only difference being,
2334 * we now initiate the irq migration from process context instead of the
2335 * interrupt context.
2336 *
2337 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2338 * suppression) to the IO-APIC, level triggered irq migration will also be
2339 * as simple as edge triggered migration and we can do the irq migration
2340 * with a simple atomic update to IO-APIC RTE.
2341 */
Mike Travise7986732008-12-16 17:33:52 -08002342static void
2343migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002344{
2345 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002346 struct irte irte;
2347 int modify_ioapic_rte;
2348 unsigned int dest;
2349 unsigned long flags;
Yinghai Lu3145e942008-12-05 18:58:34 -08002350 unsigned int irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002351
Mike Travis22f65d32008-12-16 17:33:56 -08002352 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002353 return;
2354
Yinghai Lu3145e942008-12-05 18:58:34 -08002355 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002356 if (get_irte(irq, &irte))
2357 return;
2358
Yinghai Lu3145e942008-12-05 18:58:34 -08002359 cfg = desc->chip_data;
2360 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002361 return;
2362
Yinghai Lu3145e942008-12-05 18:58:34 -08002363 set_extra_move_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002364
Mike Travis22f65d32008-12-16 17:33:56 -08002365 dest = cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002366
Ingo Molnar54168ed2008-08-20 09:07:45 +02002367 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2368 if (modify_ioapic_rte) {
2369 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08002370 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002371 spin_unlock_irqrestore(&ioapic_lock, flags);
2372 }
2373
2374 irte.vector = cfg->vector;
2375 irte.dest_id = IRTE_DEST(dest);
2376
2377 /*
2378 * Modified the IRTE and flushes the Interrupt entry cache.
2379 */
2380 modify_irte(irq, &irte);
2381
Mike Travis22f65d32008-12-16 17:33:56 -08002382 if (cfg->move_in_progress)
2383 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002384
Mike Travis7f7ace02009-01-10 21:58:08 -08002385 cpumask_copy(desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002386}
2387
Yinghai Lu3145e942008-12-05 18:58:34 -08002388static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002389{
2390 int ret = -1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002391 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002392
Yinghai Lu3145e942008-12-05 18:58:34 -08002393 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002394
Yinghai Lu3145e942008-12-05 18:58:34 -08002395 if (io_apic_level_ack_pending(cfg)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002396 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002397 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002398 * vector information in the IO-APIC RTE and that will confuse
2399 * the EOI broadcast performed by cpu.
2400 * So, delay the irq migration to the next instance.
2401 */
2402 schedule_delayed_work(&ir_migration_work, 1);
2403 goto unmask;
2404 }
2405
2406 /* everthing is clear. we have right of way */
Mike Travis7f7ace02009-01-10 21:58:08 -08002407 migrate_ioapic_irq_desc(desc, desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002408
2409 ret = 0;
2410 desc->status &= ~IRQ_MOVE_PENDING;
Mike Travis7f7ace02009-01-10 21:58:08 -08002411 cpumask_clear(desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002412
2413unmask:
Yinghai Lu3145e942008-12-05 18:58:34 -08002414 unmask_IO_APIC_irq_desc(desc);
2415
Ingo Molnar54168ed2008-08-20 09:07:45 +02002416 return ret;
2417}
2418
2419static void ir_irq_migration(struct work_struct *work)
2420{
2421 unsigned int irq;
2422 struct irq_desc *desc;
2423
2424 for_each_irq_desc(irq, desc) {
2425 if (desc->status & IRQ_MOVE_PENDING) {
2426 unsigned long flags;
2427
2428 spin_lock_irqsave(&desc->lock, flags);
2429 if (!desc->chip->set_affinity ||
2430 !(desc->status & IRQ_MOVE_PENDING)) {
2431 desc->status &= ~IRQ_MOVE_PENDING;
2432 spin_unlock_irqrestore(&desc->lock, flags);
2433 continue;
2434 }
2435
Mike Travis7f7ace02009-01-10 21:58:08 -08002436 desc->chip->set_affinity(irq, desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002437 spin_unlock_irqrestore(&desc->lock, flags);
2438 }
2439 }
2440}
2441
2442/*
2443 * Migrates the IRQ destination in the process context.
2444 */
Rusty Russell968ea6d2008-12-13 21:55:51 +10302445static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2446 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002447{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002448 if (desc->status & IRQ_LEVEL) {
2449 desc->status |= IRQ_MOVE_PENDING;
Mike Travis7f7ace02009-01-10 21:58:08 -08002450 cpumask_copy(desc->pending_mask, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002451 migrate_irq_remapped_level_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002452 return;
2453 }
2454
Yinghai Lu3145e942008-12-05 18:58:34 -08002455 migrate_ioapic_irq_desc(desc, mask);
2456}
Rusty Russell0de26522008-12-13 21:20:26 +10302457static void set_ir_ioapic_affinity_irq(unsigned int irq,
2458 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002459{
2460 struct irq_desc *desc = irq_to_desc(irq);
2461
Yinghai Lu3145e942008-12-05 18:58:34 -08002462 set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002463}
2464#endif
2465
Yinghai Lu497c9a12008-08-19 20:50:28 -07002466asmlinkage void smp_irq_move_cleanup_interrupt(void)
2467{
2468 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002469
Yinghai Lu497c9a12008-08-19 20:50:28 -07002470 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002471 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002472 irq_enter();
2473
2474 me = smp_processor_id();
2475 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2476 unsigned int irq;
2477 struct irq_desc *desc;
2478 struct irq_cfg *cfg;
2479 irq = __get_cpu_var(vector_irq)[vector];
2480
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002481 if (irq == -1)
2482 continue;
2483
Yinghai Lu497c9a12008-08-19 20:50:28 -07002484 desc = irq_to_desc(irq);
2485 if (!desc)
2486 continue;
2487
2488 cfg = irq_cfg(irq);
2489 spin_lock(&desc->lock);
2490 if (!cfg->move_cleanup_count)
2491 goto unlock;
2492
Mike Travis22f65d32008-12-16 17:33:56 -08002493 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002494 goto unlock;
2495
2496 __get_cpu_var(vector_irq)[vector] = -1;
2497 cfg->move_cleanup_count--;
2498unlock:
2499 spin_unlock(&desc->lock);
2500 }
2501
2502 irq_exit();
2503}
2504
Yinghai Lu3145e942008-12-05 18:58:34 -08002505static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002506{
Yinghai Lu3145e942008-12-05 18:58:34 -08002507 struct irq_desc *desc = *descp;
2508 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002509 unsigned vector, me;
2510
Yinghai Lu48a1b102008-12-11 00:15:01 -08002511 if (likely(!cfg->move_in_progress)) {
2512#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2513 if (likely(!cfg->move_desc_pending))
2514 return;
2515
Yinghai Lub9098952008-12-19 13:48:34 -08002516 /* domain has not changed, but affinity did */
Yinghai Lu48a1b102008-12-11 00:15:01 -08002517 me = smp_processor_id();
Mike Travis7f7ace02009-01-10 21:58:08 -08002518 if (cpumask_test_cpu(me, desc->affinity)) {
Yinghai Lu48a1b102008-12-11 00:15:01 -08002519 *descp = desc = move_irq_desc(desc, me);
2520 /* get the new one */
2521 cfg = desc->chip_data;
2522 cfg->move_desc_pending = 0;
2523 }
2524#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002525 return;
Yinghai Lu48a1b102008-12-11 00:15:01 -08002526 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002527
2528 vector = ~get_irq_regs()->orig_ax;
2529 me = smp_processor_id();
Yinghai Lu10b888d2009-01-31 14:50:07 -08002530
2531 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain)) {
Yinghai Lu48a1b102008-12-11 00:15:01 -08002532#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2533 *descp = desc = move_irq_desc(desc, me);
2534 /* get the new one */
2535 cfg = desc->chip_data;
2536#endif
Mike Travis22f65d32008-12-16 17:33:56 -08002537 send_cleanup_vector(cfg);
Yinghai Lu10b888d2009-01-31 14:50:07 -08002538 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002539}
2540#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002541static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002542#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002543
Ingo Molnar54168ed2008-08-20 09:07:45 +02002544#ifdef CONFIG_INTR_REMAP
2545static void ack_x2apic_level(unsigned int irq)
2546{
2547 ack_x2APIC_irq();
2548}
2549
2550static void ack_x2apic_edge(unsigned int irq)
2551{
2552 ack_x2APIC_irq();
2553}
Yinghai Lu3145e942008-12-05 18:58:34 -08002554
Ingo Molnar54168ed2008-08-20 09:07:45 +02002555#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002556
Yinghai Lu1d025192008-08-19 20:50:34 -07002557static void ack_apic_edge(unsigned int irq)
2558{
Yinghai Lu3145e942008-12-05 18:58:34 -08002559 struct irq_desc *desc = irq_to_desc(irq);
2560
2561 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002562 move_native_irq(irq);
2563 ack_APIC_irq();
2564}
2565
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002566atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002567
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002568static void ack_apic_level(unsigned int irq)
2569{
Yinghai Lu3145e942008-12-05 18:58:34 -08002570 struct irq_desc *desc = irq_to_desc(irq);
2571
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002572#ifdef CONFIG_X86_32
2573 unsigned long v;
2574 int i;
2575#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002576 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002577 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002578
Yinghai Lu3145e942008-12-05 18:58:34 -08002579 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002580#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002581 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002582 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002583 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002584 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002585 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002586#endif
2587
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002588#ifdef CONFIG_X86_32
2589 /*
2590 * It appears there is an erratum which affects at least version 0x11
2591 * of I/O APIC (that's the 82093AA and cores integrated into various
2592 * chipsets). Under certain conditions a level-triggered interrupt is
2593 * erroneously delivered as edge-triggered one but the respective IRR
2594 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2595 * message but it will never arrive and further interrupts are blocked
2596 * from the source. The exact reason is so far unknown, but the
2597 * phenomenon was observed when two consecutive interrupt requests
2598 * from a given source get delivered to the same CPU and the source is
2599 * temporarily disabled in between.
2600 *
2601 * A workaround is to simulate an EOI message manually. We achieve it
2602 * by setting the trigger mode to edge and then to level when the edge
2603 * trigger mode gets detected in the TMR of a local APIC for a
2604 * level-triggered interrupt. We mask the source for the time of the
2605 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2606 * The idea is from Manfred Spraul. --macro
2607 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002608 cfg = desc->chip_data;
2609 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002610
2611 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2612#endif
2613
Ingo Molnar54168ed2008-08-20 09:07:45 +02002614 /*
2615 * We must acknowledge the irq before we move it or the acknowledge will
2616 * not propagate properly.
2617 */
2618 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002619
Ingo Molnar54168ed2008-08-20 09:07:45 +02002620 /* Now we can move and renable the irq */
2621 if (unlikely(do_unmask_irq)) {
2622 /* Only migrate the irq if the ack has been received.
2623 *
2624 * On rare occasions the broadcast level triggered ack gets
2625 * delayed going to ioapics, and if we reprogram the
2626 * vector while Remote IRR is still set the irq will never
2627 * fire again.
2628 *
2629 * To prevent this scenario we read the Remote IRR bit
2630 * of the ioapic. This has two effects.
2631 * - On any sane system the read of the ioapic will
2632 * flush writes (and acks) going to the ioapic from
2633 * this cpu.
2634 * - We get to see if the ACK has actually been delivered.
2635 *
2636 * Based on failed experiments of reprogramming the
2637 * ioapic entry from outside of irq context starting
2638 * with masking the ioapic entry and then polling until
2639 * Remote IRR was clear before reprogramming the
2640 * ioapic I don't trust the Remote IRR bit to be
2641 * completey accurate.
2642 *
2643 * However there appears to be no other way to plug
2644 * this race, so if the Remote IRR bit is not
2645 * accurate and is causing problems then it is a hardware bug
2646 * and you can go talk to the chipset vendor about it.
2647 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002648 cfg = desc->chip_data;
2649 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002650 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002651 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002652 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002653
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002654#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002655 if (!(v & (1 << (i & 0x1f)))) {
2656 atomic_inc(&irq_mis_count);
2657 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002658 __mask_and_edge_IO_APIC_irq(cfg);
2659 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002660 spin_unlock(&ioapic_lock);
2661 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002662#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002663}
Yinghai Lu1d025192008-08-19 20:50:34 -07002664
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002665static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002666 .name = "IO-APIC",
2667 .startup = startup_ioapic_irq,
2668 .mask = mask_IO_APIC_irq,
2669 .unmask = unmask_IO_APIC_irq,
2670 .ack = ack_apic_edge,
2671 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002672#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002673 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002674#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002675 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676};
2677
Ingo Molnar54168ed2008-08-20 09:07:45 +02002678#ifdef CONFIG_INTR_REMAP
2679static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002680 .name = "IR-IO-APIC",
2681 .startup = startup_ioapic_irq,
2682 .mask = mask_IO_APIC_irq,
2683 .unmask = unmask_IO_APIC_irq,
2684 .ack = ack_x2apic_edge,
2685 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002686#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002687 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002688#endif
2689 .retrigger = ioapic_retrigger_irq,
2690};
2691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693static inline void init_IO_APIC_traps(void)
2694{
2695 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002696 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002697 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 /*
2700 * NOTE! The local APIC isn't very good at handling
2701 * multiple interrupts at the same interrupt level.
2702 * As the interrupt level is determined by taking the
2703 * vector number and shifting that right by 4, we
2704 * want to spread these out a bit so that they don't
2705 * all fall in the same interrupt level.
2706 *
2707 * Also, we've got to be careful not to trash gate
2708 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2709 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002710 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002711 cfg = desc->chip_data;
2712 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /*
2714 * Hmm.. We don't have an entry for this,
2715 * so default to an old-fashioned 8259
2716 * interrupt if we can..
2717 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002718 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002720 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002722 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724 }
2725}
2726
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002727/*
2728 * The local APIC irq-chip implementation:
2729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002731static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 unsigned long v;
2734
2735 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002736 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002739static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002741 unsigned long v;
2742
2743 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002744 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
Yinghai Lu3145e942008-12-05 18:58:34 -08002747static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002748{
2749 ack_APIC_irq();
2750}
2751
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002752static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002753 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002754 .mask = mask_lapic_irq,
2755 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002756 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757};
2758
Yinghai Lu3145e942008-12-05 18:58:34 -08002759static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002760{
Yinghai Lu08678b02008-08-19 20:50:05 -07002761 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002762 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2763 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002764}
2765
Jan Beuliche9427102008-01-30 13:31:24 +01002766static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
2768 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002769 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 * We put the 8259A master into AEOI mode and
2771 * unmask on all local APICs LVT0 as NMI.
2772 *
2773 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2774 * is from Maciej W. Rozycki - so we do not have to EOI from
2775 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002776 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2778
Jan Beuliche9427102008-01-30 13:31:24 +01002779 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 apic_printk(APIC_VERBOSE, " done.\n");
2782}
2783
2784/*
2785 * This looks a bit hackish but it's about the only one way of sending
2786 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2787 * not support the ExtINT mode, unfortunately. We need to send these
2788 * cycles as some i82489DX-based boards have glue logic that keeps the
2789 * 8259A interrupt line asserted until INTA. --macro
2790 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002791static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002793 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct IO_APIC_route_entry entry0, entry1;
2795 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002797 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002798 if (pin == -1) {
2799 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002801 }
2802 apic = find_isa_irq_apic(8, mp_INT);
2803 if (apic == -1) {
2804 WARN_ON_ONCE(1);
2805 return;
2806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Andi Kleencf4c6a22006-09-26 10:52:30 +02002808 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002809 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 memset(&entry1, 0, sizeof(entry1));
2812
2813 entry1.dest_mode = 0; /* physical delivery */
2814 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002815 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 entry1.delivery_mode = dest_ExtINT;
2817 entry1.polarity = entry0.polarity;
2818 entry1.trigger = 0;
2819 entry1.vector = 0;
2820
Andi Kleencf4c6a22006-09-26 10:52:30 +02002821 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 save_control = CMOS_READ(RTC_CONTROL);
2824 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2825 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2826 RTC_FREQ_SELECT);
2827 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2828
2829 i = 100;
2830 while (i-- > 0) {
2831 mdelay(10);
2832 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2833 i -= 10;
2834 }
2835
2836 CMOS_WRITE(save_control, RTC_CONTROL);
2837 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002838 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Andi Kleencf4c6a22006-09-26 10:52:30 +02002840 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
Yinghai Luefa25592008-08-19 20:50:36 -07002843static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002844/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002845static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002846{
2847 disable_timer_pin_1 = 1;
2848 return 0;
2849}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002850early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002851
2852int timer_through_8259 __initdata;
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854/*
2855 * This code may look a bit paranoid, but it's supposed to cooperate with
2856 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2857 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2858 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002859 *
2860 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002862static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Yinghai Lu3145e942008-12-05 18:58:34 -08002864 struct irq_desc *desc = irq_to_desc(0);
2865 struct irq_cfg *cfg = desc->chip_data;
2866 int cpu = boot_cpu_id;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002867 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002868 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002869 unsigned int ver;
2870 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002871
2872 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002873
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002874 ver = apic_read(APIC_LVR);
2875 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 /*
2878 * get/set the timer IRQ vector:
2879 */
2880 disable_8259A_irq(0);
Yinghai Lu3145e942008-12-05 18:58:34 -08002881 assign_irq_vector(0, cfg, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002884 * As IRQ0 is to be enabled in the 8259A, the virtual
2885 * wire has to be disabled in the local APIC. Also
2886 * timer interrupts need to be acknowledged manually in
2887 * the 8259A for the i82489DX when using the NMI
2888 * watchdog as that APIC treats NMIs as level-triggered.
2889 * The AEOI mode will finish them in the 8259A
2890 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002892 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002894#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002895 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002898 pin1 = find_isa_irq_pin(0, mp_INT);
2899 apic1 = find_isa_irq_apic(0, mp_INT);
2900 pin2 = ioapic_i8259.pin;
2901 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002903 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2904 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002905 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002907 /*
2908 * Some BIOS writers are clueless and report the ExtINTA
2909 * I/O APIC input from the cascaded 8259A as the timer
2910 * interrupt input. So just in case, if only one pin
2911 * was found above, try it both directly and through the
2912 * 8259A.
2913 */
2914 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002915#ifdef CONFIG_INTR_REMAP
2916 if (intr_remapping_enabled)
2917 panic("BIOS bug: timer not connected to IO-APIC");
2918#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002919 pin1 = pin2;
2920 apic1 = apic2;
2921 no_pin1 = 1;
2922 } else if (pin2 == -1) {
2923 pin2 = pin1;
2924 apic2 = apic1;
2925 }
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (pin1 != -1) {
2928 /*
2929 * Ok, does IRQ0 through the IOAPIC work?
2930 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002931 if (no_pin1) {
Yinghai Lu3145e942008-12-05 18:58:34 -08002932 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002933 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002934 }
Yinghai Lu3145e942008-12-05 18:58:34 -08002935 unmask_IO_APIC_irq_desc(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (timer_irq_works()) {
2937 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 setup_nmi();
2939 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002941 if (disable_timer_pin_1 > 0)
2942 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002945#ifdef CONFIG_INTR_REMAP
2946 if (intr_remapping_enabled)
2947 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2948#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002949 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002950 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002951 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2952 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002954 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2955 "(IRQ0) through the 8259A ...\n");
2956 apic_printk(APIC_QUIET, KERN_INFO
2957 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /*
2959 * legacy devices should be connected to IO APIC #0
2960 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002961 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002962 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Yinghai Lu3145e942008-12-05 18:58:34 -08002963 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002964 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002966 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002967 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002969 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002971 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975 /*
2976 * Cleanup, just in case ...
2977 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002978 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002979 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002980 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002984 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2985 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002986 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002988#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002989 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002992 apic_printk(APIC_QUIET, KERN_INFO
2993 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Yinghai Lu3145e942008-12-05 18:58:34 -08002995 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002996 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 enable_8259A_irq(0);
2998
2999 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003000 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01003003 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003004 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003005 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003007 apic_printk(APIC_QUIET, KERN_INFO
3008 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 init_8259A(0);
3011 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01003012 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 unlock_ExtINT_logic();
3015
3016 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003017 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003018 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003020 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01003022 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003023out:
3024 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
3027/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003028 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
3029 * to devices. However there may be an I/O APIC pin available for
3030 * this interrupt regardless. The pin may be left unconnected, but
3031 * typically it will be reused as an ExtINT cascade interrupt for
3032 * the master 8259A. In the MPS case such a pin will normally be
3033 * reported as an ExtINT interrupt in the MP table. With ACPI
3034 * there is no provision for ExtINT interrupts, and in the absence
3035 * of an override it would be treated as an ordinary ISA I/O APIC
3036 * interrupt, that is edge-triggered and unmasked by default. We
3037 * used to do this, but it caused problems on some systems because
3038 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3039 * the same ExtINT cascade interrupt to drive the local APIC of the
3040 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3041 * the I/O APIC in all cases now. No actual device should request
3042 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 */
3044#define PIC_IRQS (1 << PIC_CASCADE_IR)
3045
3046void __init setup_IO_APIC(void)
3047{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003048
3049#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003051#else
3052 /*
3053 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3054 */
3055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003057 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Ingo Molnar54168ed2008-08-20 09:07:45 +02003059 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003060 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003061 * Set up IO-APIC IRQ routing.
3062 */
3063#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003064 if (!acpi_ioapic)
3065 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 sync_Arb_IDs();
3068 setup_IO_APIC_irqs();
3069 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08003070 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
3073/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003074 * Called after all the initialization is done. If we didnt find any
3075 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078static int __init io_apic_bug_finalize(void)
3079{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003080 if (sis_apic_bug == -1)
3081 sis_apic_bug = 0;
3082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
3085late_initcall(io_apic_bug_finalize);
3086
3087struct sysfs_ioapic_data {
3088 struct sys_device dev;
3089 struct IO_APIC_route_entry entry[0];
3090};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003091static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Pavel Machek438510f2005-04-16 15:25:24 -07003093static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct IO_APIC_route_entry *entry;
3096 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 data = container_of(dev, struct sysfs_ioapic_data, dev);
3100 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003101 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3102 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 return 0;
3105}
3106
3107static int ioapic_resume(struct sys_device *dev)
3108{
3109 struct IO_APIC_route_entry *entry;
3110 struct sysfs_ioapic_data *data;
3111 unsigned long flags;
3112 union IO_APIC_reg_00 reg_00;
3113 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 data = container_of(dev, struct sysfs_ioapic_data, dev);
3116 entry = data->entry;
3117
3118 spin_lock_irqsave(&ioapic_lock, flags);
3119 reg_00.raw = io_apic_read(dev->id, 0);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05303120 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3121 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 io_apic_write(dev->id, 0, reg_00.raw);
3123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003125 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003126 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 return 0;
3129}
3130
3131static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003132 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 .suspend = ioapic_suspend,
3134 .resume = ioapic_resume,
3135};
3136
3137static int __init ioapic_init_sysfs(void)
3138{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003139 struct sys_device * dev;
3140 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 error = sysdev_class_register(&ioapic_sysdev_class);
3143 if (error)
3144 return error;
3145
Ingo Molnar54168ed2008-08-20 09:07:45 +02003146 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003147 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003149 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (!mp_ioapic_data[i]) {
3151 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3152 continue;
3153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003155 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 dev->cls = &ioapic_sysdev_class;
3157 error = sysdev_register(dev);
3158 if (error) {
3159 kfree(mp_ioapic_data[i]);
3160 mp_ioapic_data[i] = NULL;
3161 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3162 continue;
3163 }
3164 }
3165
3166 return 0;
3167}
3168
3169device_initcall(ioapic_init_sysfs);
3170
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003171/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003172 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003173 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003174unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003175{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003176 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003177 unsigned int irq;
3178 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003179 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003180 struct irq_cfg *cfg_new = NULL;
3181 int cpu = boot_cpu_id;
3182 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003183
3184 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003185 spin_lock_irqsave(&vector_lock, flags);
Mike Travis95949492009-01-10 22:24:06 -08003186 for (new = irq_want; new < nr_irqs; new++) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003187 if (platform_legacy_irq(new))
3188 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003189
3190 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3191 if (!desc_new) {
3192 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003193 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003194 }
3195 cfg_new = desc_new->chip_data;
3196
3197 if (cfg_new->vector != 0)
3198 continue;
Yinghai Lu3145e942008-12-05 18:58:34 -08003199 if (__assign_irq_vector(new, cfg_new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003200 irq = new;
3201 break;
3202 }
3203 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003204
Yinghai Lu199751d2008-08-19 20:50:27 -07003205 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003206 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003207 /* restore it, in case dynamic_irq_init clear it */
3208 if (desc_new)
3209 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003210 }
3211 return irq;
3212}
3213
Yinghai Lube5d5352008-12-05 18:58:33 -08003214static int nr_irqs_gsi = NR_IRQS_LEGACY;
Yinghai Lu199751d2008-08-19 20:50:27 -07003215int create_irq(void)
3216{
Yinghai Lube5d5352008-12-05 18:58:33 -08003217 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003218 int irq;
3219
Yinghai Lube5d5352008-12-05 18:58:33 -08003220 irq_want = nr_irqs_gsi;
3221 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003222
3223 if (irq == 0)
3224 irq = -1;
3225
3226 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003227}
3228
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003229void destroy_irq(unsigned int irq)
3230{
3231 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003232 struct irq_cfg *cfg;
3233 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003234
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003235 /* store it, in case dynamic_irq_cleanup clear it */
3236 desc = irq_to_desc(irq);
3237 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003238 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003239 /* connect back irq_cfg */
3240 if (desc)
3241 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003242
Ingo Molnar54168ed2008-08-20 09:07:45 +02003243#ifdef CONFIG_INTR_REMAP
3244 free_irte(irq);
3245#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003246 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003247 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003248 spin_unlock_irqrestore(&vector_lock, flags);
3249}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003250
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003251/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003252 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003253 */
3254#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003255static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003256{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003257 struct irq_cfg *cfg;
3258 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003259 unsigned dest;
3260
Jan Beulichf1182632009-01-14 12:27:35 +00003261 if (disable_apic)
3262 return -ENXIO;
3263
Yinghai Lu3145e942008-12-05 18:58:34 -08003264 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08003265 err = assign_irq_vector(irq, cfg, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003266 if (err)
3267 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003268
Mike Travis22f65d32008-12-16 17:33:56 -08003269 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003270
Ingo Molnar54168ed2008-08-20 09:07:45 +02003271#ifdef CONFIG_INTR_REMAP
3272 if (irq_remapped(irq)) {
3273 struct irte irte;
3274 int ir_index;
3275 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003276
Ingo Molnar54168ed2008-08-20 09:07:45 +02003277 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3278 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003279
Ingo Molnar54168ed2008-08-20 09:07:45 +02003280 memset (&irte, 0, sizeof(irte));
3281
3282 irte.present = 1;
3283 irte.dst_mode = INT_DEST_MODE;
3284 irte.trigger_mode = 0; /* edge */
3285 irte.dlvry_mode = INT_DELIVERY_MODE;
3286 irte.vector = cfg->vector;
3287 irte.dest_id = IRTE_DEST(dest);
3288
3289 modify_irte(irq, &irte);
3290
3291 msg->address_hi = MSI_ADDR_BASE_HI;
3292 msg->data = sub_handle;
3293 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3294 MSI_ADDR_IR_SHV |
3295 MSI_ADDR_IR_INDEX1(ir_index) |
3296 MSI_ADDR_IR_INDEX2(ir_index);
3297 } else
3298#endif
3299 {
3300 msg->address_hi = MSI_ADDR_BASE_HI;
3301 msg->address_lo =
3302 MSI_ADDR_BASE_LO |
3303 ((INT_DEST_MODE == 0) ?
3304 MSI_ADDR_DEST_MODE_PHYSICAL:
3305 MSI_ADDR_DEST_MODE_LOGICAL) |
3306 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3307 MSI_ADDR_REDIRECTION_CPU:
3308 MSI_ADDR_REDIRECTION_LOWPRI) |
3309 MSI_ADDR_DEST_ID(dest);
3310
3311 msg->data =
3312 MSI_DATA_TRIGGER_EDGE |
3313 MSI_DATA_LEVEL_ASSERT |
3314 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3315 MSI_DATA_DELIVERY_FIXED:
3316 MSI_DATA_DELIVERY_LOWPRI) |
3317 MSI_DATA_VECTOR(cfg->vector);
3318 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003319 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003320}
3321
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003322#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303323static void set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003324{
Yinghai Lu3145e942008-12-05 18:58:34 -08003325 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003326 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003327 struct msi_msg msg;
3328 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003329
Mike Travis22f65d32008-12-16 17:33:56 -08003330 dest = set_desc_affinity(desc, mask);
3331 if (dest == BAD_APICID)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003332 return;
3333
Yinghai Lu3145e942008-12-05 18:58:34 -08003334 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003335
Yinghai Lu3145e942008-12-05 18:58:34 -08003336 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003337
3338 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003339 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003340 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3341 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3342
Yinghai Lu3145e942008-12-05 18:58:34 -08003343 write_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003344}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003345#ifdef CONFIG_INTR_REMAP
3346/*
3347 * Migrate the MSI irq to another cpumask. This migration is
3348 * done in the process context using interrupt-remapping hardware.
3349 */
Mike Travise7986732008-12-16 17:33:52 -08003350static void
3351ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003352{
Yinghai Lu3145e942008-12-05 18:58:34 -08003353 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003354 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003355 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003356 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003357
3358 if (get_irte(irq, &irte))
3359 return;
3360
Mike Travis22f65d32008-12-16 17:33:56 -08003361 dest = set_desc_affinity(desc, mask);
3362 if (dest == BAD_APICID)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003363 return;
3364
Ingo Molnar54168ed2008-08-20 09:07:45 +02003365 irte.vector = cfg->vector;
3366 irte.dest_id = IRTE_DEST(dest);
3367
3368 /*
3369 * atomically update the IRTE with the new destination and vector.
3370 */
3371 modify_irte(irq, &irte);
3372
3373 /*
3374 * After this point, all the interrupts will start arriving
3375 * at the new destination. So, time to cleanup the previous
3376 * vector allocation.
3377 */
Mike Travis22f65d32008-12-16 17:33:56 -08003378 if (cfg->move_in_progress)
3379 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003380}
Yinghai Lu3145e942008-12-05 18:58:34 -08003381
Ingo Molnar54168ed2008-08-20 09:07:45 +02003382#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003383#endif /* CONFIG_SMP */
3384
3385/*
3386 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3387 * which implement the MSI or MSI-X Capability Structure.
3388 */
3389static struct irq_chip msi_chip = {
3390 .name = "PCI-MSI",
3391 .unmask = unmask_msi_irq,
3392 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003393 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003394#ifdef CONFIG_SMP
3395 .set_affinity = set_msi_irq_affinity,
3396#endif
3397 .retrigger = ioapic_retrigger_irq,
3398};
3399
Ingo Molnar54168ed2008-08-20 09:07:45 +02003400#ifdef CONFIG_INTR_REMAP
3401static struct irq_chip msi_ir_chip = {
3402 .name = "IR-PCI-MSI",
3403 .unmask = unmask_msi_irq,
3404 .mask = mask_msi_irq,
3405 .ack = ack_x2apic_edge,
3406#ifdef CONFIG_SMP
3407 .set_affinity = ir_set_msi_irq_affinity,
3408#endif
3409 .retrigger = ioapic_retrigger_irq,
3410};
3411
3412/*
3413 * Map the PCI dev to the corresponding remapping hardware unit
3414 * and allocate 'nvec' consecutive interrupt-remapping table entries
3415 * in it.
3416 */
3417static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3418{
3419 struct intel_iommu *iommu;
3420 int index;
3421
3422 iommu = map_dev_to_ir(dev);
3423 if (!iommu) {
3424 printk(KERN_ERR
3425 "Unable to map PCI %s to iommu\n", pci_name(dev));
3426 return -ENOENT;
3427 }
3428
3429 index = alloc_irte(iommu, irq, nvec);
3430 if (index < 0) {
3431 printk(KERN_ERR
3432 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003433 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003434 return -ENOSPC;
3435 }
3436 return index;
3437}
3438#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003439
Yinghai Lu3145e942008-12-05 18:58:34 -08003440static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003441{
3442 int ret;
3443 struct msi_msg msg;
3444
3445 ret = msi_compose_msg(dev, irq, &msg);
3446 if (ret < 0)
3447 return ret;
3448
Yinghai Lu3145e942008-12-05 18:58:34 -08003449 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003450 write_msi_msg(irq, &msg);
3451
Ingo Molnar54168ed2008-08-20 09:07:45 +02003452#ifdef CONFIG_INTR_REMAP
3453 if (irq_remapped(irq)) {
3454 struct irq_desc *desc = irq_to_desc(irq);
3455 /*
3456 * irq migration in process context
3457 */
3458 desc->status |= IRQ_MOVE_PCNTXT;
3459 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3460 } else
3461#endif
3462 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003463
Yinghai Luc81bba42008-09-25 11:53:11 -07003464 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3465
Yinghai Lu1d025192008-08-19 20:50:34 -07003466 return 0;
3467}
3468
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003469int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003470{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003471 unsigned int irq;
3472 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003473 unsigned int irq_want;
3474
Yinghai Lube5d5352008-12-05 18:58:33 -08003475 irq_want = nr_irqs_gsi;
Yinghai Lu199751d2008-08-19 20:50:27 -07003476 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003477 if (irq == 0)
3478 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003479
Ingo Molnar54168ed2008-08-20 09:07:45 +02003480#ifdef CONFIG_INTR_REMAP
3481 if (!intr_remapping_enabled)
3482 goto no_ir;
3483
3484 ret = msi_alloc_irte(dev, irq, 1);
3485 if (ret < 0)
3486 goto error;
3487no_ir:
3488#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003489 ret = setup_msi_irq(dev, msidesc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003490 if (ret < 0) {
3491 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003492 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003493 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003494 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003495
3496#ifdef CONFIG_INTR_REMAP
3497error:
3498 destroy_irq(irq);
3499 return ret;
3500#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003501}
3502
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003503int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3504{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003505 unsigned int irq;
3506 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003507 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003508 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003509
Ingo Molnar54168ed2008-08-20 09:07:45 +02003510#ifdef CONFIG_INTR_REMAP
3511 struct intel_iommu *iommu = 0;
3512 int index = 0;
3513#endif
3514
Yinghai Lube5d5352008-12-05 18:58:33 -08003515 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003516 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003517 list_for_each_entry(msidesc, &dev->msi_list, list) {
3518 irq = create_irq_nr(irq_want);
Yinghai Lube5d5352008-12-05 18:58:33 -08003519 irq_want++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003520 if (irq == 0)
3521 return -1;
3522#ifdef CONFIG_INTR_REMAP
3523 if (!intr_remapping_enabled)
3524 goto no_ir;
3525
3526 if (!sub_handle) {
3527 /*
3528 * allocate the consecutive block of IRTE's
3529 * for 'nvec'
3530 */
3531 index = msi_alloc_irte(dev, irq, nvec);
3532 if (index < 0) {
3533 ret = index;
3534 goto error;
3535 }
3536 } else {
3537 iommu = map_dev_to_ir(dev);
3538 if (!iommu) {
3539 ret = -ENOENT;
3540 goto error;
3541 }
3542 /*
3543 * setup the mapping between the irq and the IRTE
3544 * base index, the sub_handle pointing to the
3545 * appropriate interrupt remap table entry.
3546 */
3547 set_irte_irq(irq, iommu, index, sub_handle);
3548 }
3549no_ir:
3550#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003551 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003552 if (ret < 0)
3553 goto error;
3554 sub_handle++;
3555 }
3556 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003557
3558error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003559 destroy_irq(irq);
3560 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003561}
3562
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003563void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003564{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003565 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003566}
3567
Ingo Molnar54168ed2008-08-20 09:07:45 +02003568#ifdef CONFIG_DMAR
3569#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -08003570static void dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003571{
Yinghai Lu3145e942008-12-05 18:58:34 -08003572 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003573 struct irq_cfg *cfg;
3574 struct msi_msg msg;
3575 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003576
Mike Travis22f65d32008-12-16 17:33:56 -08003577 dest = set_desc_affinity(desc, mask);
3578 if (dest == BAD_APICID)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003579 return;
3580
Yinghai Lu3145e942008-12-05 18:58:34 -08003581 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003582
3583 dmar_msi_read(irq, &msg);
3584
3585 msg.data &= ~MSI_DATA_VECTOR_MASK;
3586 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3587 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3588 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3589
3590 dmar_msi_write(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003591}
Yinghai Lu3145e942008-12-05 18:58:34 -08003592
Ingo Molnar54168ed2008-08-20 09:07:45 +02003593#endif /* CONFIG_SMP */
3594
3595struct irq_chip dmar_msi_type = {
3596 .name = "DMAR_MSI",
3597 .unmask = dmar_msi_unmask,
3598 .mask = dmar_msi_mask,
3599 .ack = ack_apic_edge,
3600#ifdef CONFIG_SMP
3601 .set_affinity = dmar_msi_set_affinity,
3602#endif
3603 .retrigger = ioapic_retrigger_irq,
3604};
3605
3606int arch_setup_dmar_msi(unsigned int irq)
3607{
3608 int ret;
3609 struct msi_msg msg;
3610
3611 ret = msi_compose_msg(NULL, irq, &msg);
3612 if (ret < 0)
3613 return ret;
3614 dmar_msi_write(irq, &msg);
3615 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3616 "edge");
3617 return 0;
3618}
3619#endif
3620
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003621#ifdef CONFIG_HPET_TIMER
3622
3623#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -08003624static void hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003625{
Yinghai Lu3145e942008-12-05 18:58:34 -08003626 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003627 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003628 struct msi_msg msg;
3629 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003630
Mike Travis22f65d32008-12-16 17:33:56 -08003631 dest = set_desc_affinity(desc, mask);
3632 if (dest == BAD_APICID)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003633 return;
3634
Yinghai Lu3145e942008-12-05 18:58:34 -08003635 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003636
3637 hpet_msi_read(irq, &msg);
3638
3639 msg.data &= ~MSI_DATA_VECTOR_MASK;
3640 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3641 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3642 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3643
3644 hpet_msi_write(irq, &msg);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003645}
Yinghai Lu3145e942008-12-05 18:58:34 -08003646
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003647#endif /* CONFIG_SMP */
3648
3649struct irq_chip hpet_msi_type = {
3650 .name = "HPET_MSI",
3651 .unmask = hpet_msi_unmask,
3652 .mask = hpet_msi_mask,
3653 .ack = ack_apic_edge,
3654#ifdef CONFIG_SMP
3655 .set_affinity = hpet_msi_set_affinity,
3656#endif
3657 .retrigger = ioapic_retrigger_irq,
3658};
3659
3660int arch_setup_hpet_msi(unsigned int irq)
3661{
3662 int ret;
3663 struct msi_msg msg;
3664
3665 ret = msi_compose_msg(NULL, irq, &msg);
3666 if (ret < 0)
3667 return ret;
3668
3669 hpet_msi_write(irq, &msg);
3670 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3671 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003672
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003673 return 0;
3674}
3675#endif
3676
Ingo Molnar54168ed2008-08-20 09:07:45 +02003677#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003678/*
3679 * Hypertransport interrupt support
3680 */
3681#ifdef CONFIG_HT_IRQ
3682
3683#ifdef CONFIG_SMP
3684
Yinghai Lu497c9a12008-08-19 20:50:28 -07003685static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003686{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003687 struct ht_irq_msg msg;
3688 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003689
Yinghai Lu497c9a12008-08-19 20:50:28 -07003690 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003691 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003692
Yinghai Lu497c9a12008-08-19 20:50:28 -07003693 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003694 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003695
Eric W. Biedermanec683072006-11-08 17:44:57 -08003696 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003697}
3698
Mike Travis22f65d32008-12-16 17:33:56 -08003699static void set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003700{
Yinghai Lu3145e942008-12-05 18:58:34 -08003701 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003702 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003703 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003704
Mike Travis22f65d32008-12-16 17:33:56 -08003705 dest = set_desc_affinity(desc, mask);
3706 if (dest == BAD_APICID)
Yinghai Lu497c9a12008-08-19 20:50:28 -07003707 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003708
Yinghai Lu3145e942008-12-05 18:58:34 -08003709 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003710
Yinghai Lu497c9a12008-08-19 20:50:28 -07003711 target_ht_irq(irq, dest, cfg->vector);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003712}
Yinghai Lu3145e942008-12-05 18:58:34 -08003713
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003714#endif
3715
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003716static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003717 .name = "PCI-HT",
3718 .mask = mask_ht_irq,
3719 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003720 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003721#ifdef CONFIG_SMP
3722 .set_affinity = set_ht_irq_affinity,
3723#endif
3724 .retrigger = ioapic_retrigger_irq,
3725};
3726
3727int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3728{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003729 struct irq_cfg *cfg;
3730 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003731
Jan Beulichf1182632009-01-14 12:27:35 +00003732 if (disable_apic)
3733 return -ENXIO;
3734
Yinghai Lu3145e942008-12-05 18:58:34 -08003735 cfg = irq_cfg(irq);
Mike Travise7986732008-12-16 17:33:52 -08003736 err = assign_irq_vector(irq, cfg, TARGET_CPUS);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003737 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003738 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003739 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003740
Mike Travis22f65d32008-12-16 17:33:56 -08003741 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003742
Eric W. Biedermanec683072006-11-08 17:44:57 -08003743 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003744
Eric W. Biedermanec683072006-11-08 17:44:57 -08003745 msg.address_lo =
3746 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003747 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003748 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003749 ((INT_DEST_MODE == 0) ?
3750 HT_IRQ_LOW_DM_PHYSICAL :
3751 HT_IRQ_LOW_DM_LOGICAL) |
3752 HT_IRQ_LOW_RQEOI_EDGE |
3753 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3754 HT_IRQ_LOW_MT_FIXED :
3755 HT_IRQ_LOW_MT_ARBITRATED) |
3756 HT_IRQ_LOW_IRQ_MASKED;
3757
Eric W. Biedermanec683072006-11-08 17:44:57 -08003758 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003759
Ingo Molnara460e742006-10-17 00:10:03 -07003760 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3761 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003762
3763 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003764 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003765 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003766}
3767#endif /* CONFIG_HT_IRQ */
3768
Nick Piggin03b48632009-01-20 04:36:04 +01003769#ifdef CONFIG_X86_UV
Dean Nelson4173a0e2008-10-02 12:18:21 -05003770/*
3771 * Re-target the irq to the specified CPU and enable the specified MMR located
3772 * on the specified blade to allow the sending of MSIs to the specified CPU.
3773 */
3774int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3775 unsigned long mmr_offset)
3776{
Mike Travis22f65d32008-12-16 17:33:56 -08003777 const struct cpumask *eligible_cpu = cpumask_of(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003778 struct irq_cfg *cfg;
3779 int mmr_pnode;
3780 unsigned long mmr_value;
3781 struct uv_IO_APIC_route_entry *entry;
3782 unsigned long flags;
3783 int err;
3784
Yinghai Lu3145e942008-12-05 18:58:34 -08003785 cfg = irq_cfg(irq);
3786
Mike Travise7986732008-12-16 17:33:52 -08003787 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003788 if (err != 0)
3789 return err;
3790
3791 spin_lock_irqsave(&vector_lock, flags);
3792 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3793 irq_name);
3794 spin_unlock_irqrestore(&vector_lock, flags);
3795
Dean Nelson4173a0e2008-10-02 12:18:21 -05003796 mmr_value = 0;
3797 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3798 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3799
3800 entry->vector = cfg->vector;
3801 entry->delivery_mode = INT_DELIVERY_MODE;
3802 entry->dest_mode = INT_DEST_MODE;
3803 entry->polarity = 0;
3804 entry->trigger = 0;
3805 entry->mask = 0;
Mike Travise7986732008-12-16 17:33:52 -08003806 entry->dest = cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003807
3808 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3809 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3810
3811 return irq;
3812}
3813
3814/*
3815 * Disable the specified MMR located on the specified blade so that MSIs are
3816 * longer allowed to be sent.
3817 */
3818void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3819{
3820 unsigned long mmr_value;
3821 struct uv_IO_APIC_route_entry *entry;
3822 int mmr_pnode;
3823
3824 mmr_value = 0;
3825 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3826 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3827
3828 entry->mask = 1;
3829
3830 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3831 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3832}
3833#endif /* CONFIG_X86_64 */
3834
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003835int __init io_apic_get_redir_entries (int ioapic)
3836{
3837 union IO_APIC_reg_01 reg_01;
3838 unsigned long flags;
3839
3840 spin_lock_irqsave(&ioapic_lock, flags);
3841 reg_01.raw = io_apic_read(ioapic, 1);
3842 spin_unlock_irqrestore(&ioapic_lock, flags);
3843
3844 return reg_01.bits.entries;
3845}
3846
Yinghai Lube5d5352008-12-05 18:58:33 -08003847void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003848{
Yinghai Lube5d5352008-12-05 18:58:33 -08003849 int idx;
3850 int nr = 0;
3851
3852 for (idx = 0; idx < nr_ioapics; idx++)
3853 nr += io_apic_get_redir_entries(idx) + 1;
3854
3855 if (nr > nr_irqs_gsi)
3856 nr_irqs_gsi = nr;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003857}
3858
Yinghai Lu4a046d12009-01-12 17:39:24 -08003859#ifdef CONFIG_SPARSE_IRQ
3860int __init arch_probe_nr_irqs(void)
3861{
3862 int nr;
3863
3864 nr = ((8 * nr_cpu_ids) > (32 * nr_ioapics) ?
3865 (NR_VECTORS + (8 * nr_cpu_ids)) :
3866 (NR_VECTORS + (32 * nr_ioapics)));
3867
3868 if (nr < nr_irqs && nr > nr_irqs_gsi)
3869 nr_irqs = nr;
3870
3871 return 0;
3872}
3873#endif
3874
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003876 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 -------------------------------------------------------------------------- */
3878
Len Brown888ba6c2005-08-24 12:07:20 -04003879#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Ingo Molnar54168ed2008-08-20 09:07:45 +02003881#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003882int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
3884 union IO_APIC_reg_00 reg_00;
3885 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3886 physid_mask_t tmp;
3887 unsigned long flags;
3888 int i = 0;
3889
3890 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003891 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3892 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003894 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3896 * advantage of new APIC bus architecture.
3897 */
3898
3899 if (physids_empty(apic_id_map))
3900 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3901
3902 spin_lock_irqsave(&ioapic_lock, flags);
3903 reg_00.raw = io_apic_read(ioapic, 0);
3904 spin_unlock_irqrestore(&ioapic_lock, flags);
3905
3906 if (apic_id >= get_physical_broadcast()) {
3907 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3908 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3909 apic_id = reg_00.bits.ID;
3910 }
3911
3912 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003913 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 * 'stuck on smp_invalidate_needed IPI wait' messages.
3915 */
3916 if (check_apicid_used(apic_id_map, apic_id)) {
3917
3918 for (i = 0; i < get_physical_broadcast(); i++) {
3919 if (!check_apicid_used(apic_id_map, i))
3920 break;
3921 }
3922
3923 if (i == get_physical_broadcast())
3924 panic("Max apic_id exceeded!\n");
3925
3926 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3927 "trying %d\n", ioapic, apic_id, i);
3928
3929 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
3932 tmp = apicid_to_cpu_present(apic_id);
3933 physids_or(apic_id_map, apic_id_map, tmp);
3934
3935 if (reg_00.bits.ID != apic_id) {
3936 reg_00.bits.ID = apic_id;
3937
3938 spin_lock_irqsave(&ioapic_lock, flags);
3939 io_apic_write(ioapic, 0, reg_00.raw);
3940 reg_00.raw = io_apic_read(ioapic, 0);
3941 spin_unlock_irqrestore(&ioapic_lock, flags);
3942
3943 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003944 if (reg_00.bits.ID != apic_id) {
3945 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3946 return -1;
3947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 }
3949
3950 apic_printk(APIC_VERBOSE, KERN_INFO
3951 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3952
3953 return apic_id;
3954}
3955
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003956int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
3958 union IO_APIC_reg_01 reg_01;
3959 unsigned long flags;
3960
3961 spin_lock_irqsave(&ioapic_lock, flags);
3962 reg_01.raw = io_apic_read(ioapic, 1);
3963 spin_unlock_irqrestore(&ioapic_lock, flags);
3964
3965 return reg_01.bits.version;
3966}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003967#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968
Ingo Molnar54168ed2008-08-20 09:07:45 +02003969int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003971 struct irq_desc *desc;
3972 struct irq_cfg *cfg;
3973 int cpu = boot_cpu_id;
3974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003976 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 ioapic);
3978 return -EINVAL;
3979 }
3980
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003981 desc = irq_to_desc_alloc_cpu(irq, cpu);
3982 if (!desc) {
3983 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3984 return 0;
3985 }
3986
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 * IRQs < 16 are already in the irq_2_pin[] map
3989 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003990 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003991 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003992 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Yinghai Lu3145e942008-12-05 18:58:34 -08003995 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
3997 return 0;
3998}
3999
Ingo Molnar54168ed2008-08-20 09:07:45 +02004000
Shaohua Li61fd47e2007-11-17 01:05:28 -05004001int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
4002{
4003 int i;
4004
4005 if (skip_ioapic_setup)
4006 return -1;
4007
4008 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05304009 if (mp_irqs[i].irqtype == mp_INT &&
4010 mp_irqs[i].srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05004011 break;
4012 if (i >= mp_irq_entries)
4013 return -1;
4014
4015 *trigger = irq_trigger(i);
4016 *polarity = irq_polarity(i);
4017 return 0;
4018}
4019
Len Brown888ba6c2005-08-24 12:07:20 -04004020#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02004021
Yinghai Lu497c9a12008-08-19 20:50:28 -07004022/*
4023 * This function currently is only a helper for the i386 smp boot process where
4024 * we need to reprogram the ioredtbls to cater for the cpus which have come online
4025 * so mask in all cases should simply be TARGET_CPUS
4026 */
4027#ifdef CONFIG_SMP
4028void __init setup_ioapic_dest(void)
4029{
4030 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004031 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004032 struct irq_cfg *cfg;
Mike Travis22f65d32008-12-16 17:33:56 -08004033 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004034
4035 if (skip_ioapic_setup == 1)
4036 return;
4037
4038 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
4039 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4040 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4041 if (irq_entry == -1)
4042 continue;
4043 irq = pin_2_irq(irq_entry, ioapic, pin);
4044
4045 /* setup_IO_APIC_irqs could fail to get vector for some device
4046 * when you have too many devices, because at that time only boot
4047 * cpu is online.
4048 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08004049 desc = irq_to_desc(irq);
4050 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004051 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08004052 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07004053 irq_trigger(irq_entry),
4054 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004055 continue;
4056
4057 }
4058
4059 /*
4060 * Honour affinities which have been set in early boot
4061 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004062 if (desc->status &
4063 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
Mike Travis7f7ace02009-01-10 21:58:08 -08004064 mask = desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004065 else
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004066 mask = TARGET_CPUS;
4067
4068#ifdef CONFIG_INTR_REMAP
4069 if (intr_remapping_enabled)
Yinghai Lu3145e942008-12-05 18:58:34 -08004070 set_ir_ioapic_affinity_irq_desc(desc, mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004071 else
4072#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08004073 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07004074 }
4075
4076 }
4077}
4078#endif
4079
Ingo Molnar54168ed2008-08-20 09:07:45 +02004080#define IOAPIC_RESOURCE_NAME_SIZE 11
4081
4082static struct resource *ioapic_resources;
4083
4084static struct resource * __init ioapic_setup_resources(void)
4085{
4086 unsigned long n;
4087 struct resource *res;
4088 char *mem;
4089 int i;
4090
4091 if (nr_ioapics <= 0)
4092 return NULL;
4093
4094 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4095 n *= nr_ioapics;
4096
4097 mem = alloc_bootmem(n);
4098 res = (void *)mem;
4099
4100 if (mem != NULL) {
4101 mem += sizeof(struct resource) * nr_ioapics;
4102
4103 for (i = 0; i < nr_ioapics; i++) {
4104 res[i].name = mem;
4105 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4106 sprintf(mem, "IOAPIC %u", i);
4107 mem += IOAPIC_RESOURCE_NAME_SIZE;
4108 }
4109 }
4110
4111 ioapic_resources = res;
4112
4113 return res;
4114}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004115
Yinghai Luf3294a32008-06-27 01:41:56 -07004116void __init ioapic_init_mappings(void)
4117{
4118 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004119 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004120 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004121
Ingo Molnar54168ed2008-08-20 09:07:45 +02004122 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07004123 for (i = 0; i < nr_ioapics; i++) {
4124 if (smp_found_config) {
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05304125 ioapic_phys = mp_ioapics[i].apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004126#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004127 if (!ioapic_phys) {
4128 printk(KERN_ERR
4129 "WARNING: bogus zero IO-APIC "
4130 "address found in MPTABLE, "
4131 "disabling IO/APIC support!\n");
4132 smp_found_config = 0;
4133 skip_ioapic_setup = 1;
4134 goto fake_ioapic_page;
4135 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004136#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004137 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004138#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004139fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004140#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004141 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004142 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004143 ioapic_phys = __pa(ioapic_phys);
4144 }
4145 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004146 apic_printk(APIC_VERBOSE,
4147 "mapped IOAPIC to %08lx (%08lx)\n",
4148 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004149 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004150
Ingo Molnar54168ed2008-08-20 09:07:45 +02004151 if (ioapic_res != NULL) {
4152 ioapic_res->start = ioapic_phys;
4153 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4154 ioapic_res++;
4155 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004156 }
4157}
4158
Ingo Molnar54168ed2008-08-20 09:07:45 +02004159static int __init ioapic_insert_resources(void)
4160{
4161 int i;
4162 struct resource *r = ioapic_resources;
4163
4164 if (!r) {
4165 printk(KERN_ERR
4166 "IO APIC resources could be not be allocated.\n");
4167 return -1;
4168 }
4169
4170 for (i = 0; i < nr_ioapics; i++) {
4171 insert_resource(&iomem_resource, r);
4172 r++;
4173 }
4174
4175 return 0;
4176}
4177
4178/* Insert the IO APIC resources after PCI initialization has occured to handle
4179 * IO APICS that are mapped in on a BAR in PCI space. */
4180late_initcall(ioapic_insert_resources);