blob: 5c7630b40a549de3a6e3ff81a79010191af5b456 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070028#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020038#include <linux/jiffies.h> /* time_after() */
Yinghai Lud4057bd2008-08-19 20:50:38 -070039#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070044#include <linux/hpet.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070045
Yinghai Lud4057bd2008-08-19 20:50:38 -070046#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48#include <asm/smp.h>
Jaswinder Singh Rajput6d652ea2009-01-07 21:38:59 +053049#include <asm/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070051#include <asm/proto.h>
52#include <asm/acpi.h>
53#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070055#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020056#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070057#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070058#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070059#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070060#include <asm/irq_remapping.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070061#include <asm/hpet.h>
Dean Nelson4173a0e2008-10-02 12:18:21 -050062#include <asm/uv/uv_hub.h>
63#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Ingo Molnar7b6aa332009-02-17 13:58:15 +010065#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010067#define __apicdebuginit(type) static type __init
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020070 * Is the SiS APIC rmw bug present ?
71 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73int sis_apic_bug = -1;
74
Yinghai Luefa25592008-08-19 20:50:36 -070075static DEFINE_SPINLOCK(ioapic_lock);
76static DEFINE_SPINLOCK(vector_lock);
77
Yinghai Luefa25592008-08-19 20:50:36 -070078/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 * # of IRQ routing registers
80 */
81int nr_ioapic_registers[MAX_IO_APICS];
82
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040083/* I/O APIC entries */
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +053084struct mpc_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040085int nr_ioapics;
86
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040087/* MP IRQ source entries */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +053088struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040089
90/* # of MP IRQ source entries */
91int mp_irq_entries;
92
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040093#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
94int mp_bus_id_to_type[MAX_MP_BUSSES];
95#endif
96
97DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
98
Yinghai Luefa25592008-08-19 20:50:36 -070099int skip_ioapic_setup;
100
Ingo Molnar65a4e572009-01-31 03:36:17 +0100101void arch_disable_smp_support(void)
102{
103#ifdef CONFIG_PCI
104 noioapicquirk = 1;
105 noioapicreroute = -1;
106#endif
107 skip_ioapic_setup = 1;
108}
109
Ingo Molnar54168ed2008-08-20 09:07:45 +0200110static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700111{
112 /* disable IO-APIC */
Ingo Molnar65a4e572009-01-31 03:36:17 +0100113 arch_disable_smp_support();
Yinghai Luefa25592008-08-19 20:50:36 -0700114 return 0;
115}
116early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200117
Yinghai Lu0f978f42008-08-19 20:50:26 -0700118struct irq_pin_list;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200119
120/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 * This is performance-critical, we want to do it O(1)
122 *
123 * the indexing order of this array favors 1:1 mappings
124 * between pins and IRQs.
125 */
126
Yinghai Lu0f978f42008-08-19 20:50:26 -0700127struct irq_pin_list {
128 int apic, pin;
129 struct irq_pin_list *next;
130};
Yinghai Lu301e6192008-08-19 20:50:02 -0700131
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800132static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700133{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800134 struct irq_pin_list *pin;
135 int node;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700136
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800137 node = cpu_to_node(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700138
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800139 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700140
Yinghai Lu0f978f42008-08-19 20:50:26 -0700141 return pin;
142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800144struct irq_cfg {
145 struct irq_pin_list *irq_2_pin;
Mike Travis22f65d32008-12-16 17:33:56 -0800146 cpumask_var_t domain;
147 cpumask_var_t old_domain;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800148 unsigned move_cleanup_count;
149 u8 vector;
150 u8 move_in_progress : 1;
151};
152
153/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
154#ifdef CONFIG_SPARSE_IRQ
155static struct irq_cfg irq_cfgx[] = {
156#else
157static struct irq_cfg irq_cfgx[NR_IRQS] = {
158#endif
Mike Travis22f65d32008-12-16 17:33:56 -0800159 [0] = { .vector = IRQ0_VECTOR, },
160 [1] = { .vector = IRQ1_VECTOR, },
161 [2] = { .vector = IRQ2_VECTOR, },
162 [3] = { .vector = IRQ3_VECTOR, },
163 [4] = { .vector = IRQ4_VECTOR, },
164 [5] = { .vector = IRQ5_VECTOR, },
165 [6] = { .vector = IRQ6_VECTOR, },
166 [7] = { .vector = IRQ7_VECTOR, },
167 [8] = { .vector = IRQ8_VECTOR, },
168 [9] = { .vector = IRQ9_VECTOR, },
169 [10] = { .vector = IRQ10_VECTOR, },
170 [11] = { .vector = IRQ11_VECTOR, },
171 [12] = { .vector = IRQ12_VECTOR, },
172 [13] = { .vector = IRQ13_VECTOR, },
173 [14] = { .vector = IRQ14_VECTOR, },
174 [15] = { .vector = IRQ15_VECTOR, },
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800175};
176
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800177int __init arch_early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800178{
179 struct irq_cfg *cfg;
180 struct irq_desc *desc;
181 int count;
182 int i;
183
184 cfg = irq_cfgx;
185 count = ARRAY_SIZE(irq_cfgx);
186
187 for (i = 0; i < count; i++) {
188 desc = irq_to_desc(i);
189 desc->chip_data = &cfg[i];
Mike Travis22f65d32008-12-16 17:33:56 -0800190 alloc_bootmem_cpumask_var(&cfg[i].domain);
191 alloc_bootmem_cpumask_var(&cfg[i].old_domain);
192 if (i < NR_IRQS_LEGACY)
193 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800194 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800195
196 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800197}
198
199#ifdef CONFIG_SPARSE_IRQ
200static struct irq_cfg *irq_cfg(unsigned int irq)
201{
202 struct irq_cfg *cfg = NULL;
203 struct irq_desc *desc;
204
205 desc = irq_to_desc(irq);
206 if (desc)
207 cfg = desc->chip_data;
208
209 return cfg;
210}
211
212static struct irq_cfg *get_one_free_irq_cfg(int cpu)
213{
214 struct irq_cfg *cfg;
215 int node;
216
217 node = cpu_to_node(cpu);
218
219 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800220 if (cfg) {
Mike Travis80855f72008-12-31 18:08:47 -0800221 if (!alloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800222 kfree(cfg);
223 cfg = NULL;
Mike Travis80855f72008-12-31 18:08:47 -0800224 } else if (!alloc_cpumask_var_node(&cfg->old_domain,
225 GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800226 free_cpumask_var(cfg->domain);
227 kfree(cfg);
228 cfg = NULL;
229 } else {
230 cpumask_clear(cfg->domain);
231 cpumask_clear(cfg->old_domain);
232 }
233 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800234
235 return cfg;
236}
237
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800238int arch_init_chip_data(struct irq_desc *desc, int cpu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800239{
240 struct irq_cfg *cfg;
241
242 cfg = desc->chip_data;
243 if (!cfg) {
244 desc->chip_data = get_one_free_irq_cfg(cpu);
245 if (!desc->chip_data) {
246 printk(KERN_ERR "can not alloc irq_cfg\n");
247 BUG_ON(1);
248 }
249 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800250
251 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800252}
253
Yinghai Lufcef5912009-04-27 17:58:23 -0700254/* for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800255static void
256init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int cpu)
257{
258 struct irq_pin_list *old_entry, *head, *tail, *entry;
259
260 cfg->irq_2_pin = NULL;
261 old_entry = old_cfg->irq_2_pin;
262 if (!old_entry)
263 return;
264
265 entry = get_one_free_irq_2_pin(cpu);
266 if (!entry)
267 return;
268
269 entry->apic = old_entry->apic;
270 entry->pin = old_entry->pin;
271 head = entry;
272 tail = entry;
273 old_entry = old_entry->next;
274 while (old_entry) {
275 entry = get_one_free_irq_2_pin(cpu);
276 if (!entry) {
277 entry = head;
278 while (entry) {
279 head = entry->next;
280 kfree(entry);
281 entry = head;
282 }
283 /* still use the old one */
284 return;
285 }
286 entry->apic = old_entry->apic;
287 entry->pin = old_entry->pin;
288 tail->next = entry;
289 tail = entry;
290 old_entry = old_entry->next;
291 }
292
293 tail->next = NULL;
294 cfg->irq_2_pin = head;
295}
296
297static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
298{
299 struct irq_pin_list *entry, *next;
300
301 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
302 return;
303
304 entry = old_cfg->irq_2_pin;
305
306 while (entry) {
307 next = entry->next;
308 kfree(entry);
309 entry = next;
310 }
311 old_cfg->irq_2_pin = NULL;
312}
313
314void arch_init_copy_chip_data(struct irq_desc *old_desc,
315 struct irq_desc *desc, int cpu)
316{
317 struct irq_cfg *cfg;
318 struct irq_cfg *old_cfg;
319
320 cfg = get_one_free_irq_cfg(cpu);
321
322 if (!cfg)
323 return;
324
325 desc->chip_data = cfg;
326
327 old_cfg = old_desc->chip_data;
328
329 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
330
331 init_copy_irq_2_pin(old_cfg, cfg, cpu);
332}
333
334static void free_irq_cfg(struct irq_cfg *old_cfg)
335{
336 kfree(old_cfg);
337}
338
339void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
340{
341 struct irq_cfg *old_cfg, *cfg;
342
343 old_cfg = old_desc->chip_data;
344 cfg = desc->chip_data;
345
346 if (old_cfg == cfg)
347 return;
348
349 if (old_cfg) {
350 free_irq_2_pin(old_cfg, cfg);
351 free_irq_cfg(old_cfg);
352 old_desc->chip_data = NULL;
353 }
354}
Yinghai Lufcef5912009-04-27 17:58:23 -0700355/* end for move_irq_desc */
Yinghai Lu48a1b102008-12-11 00:15:01 -0800356
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800357#else
358static struct irq_cfg *irq_cfg(unsigned int irq)
359{
360 return irq < nr_irqs ? irq_cfgx + irq : NULL;
361}
362
363#endif
364
Linus Torvalds130fe052006-11-01 09:11:00 -0800365struct io_apic {
366 unsigned int index;
367 unsigned int unused[3];
368 unsigned int data;
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700369 unsigned int unused2[11];
370 unsigned int eoi;
Linus Torvalds130fe052006-11-01 09:11:00 -0800371};
372
373static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
374{
375 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +0530376 + (mp_ioapics[idx].apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800377}
378
Suresh Siddha0280f7c2009-03-16 17:05:01 -0700379static inline void io_apic_eoi(unsigned int apic, unsigned int vector)
380{
381 struct io_apic __iomem *io_apic = io_apic_base(apic);
382 writel(vector, &io_apic->eoi);
383}
384
Linus Torvalds130fe052006-11-01 09:11:00 -0800385static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
386{
387 struct io_apic __iomem *io_apic = io_apic_base(apic);
388 writel(reg, &io_apic->index);
389 return readl(&io_apic->data);
390}
391
392static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
393{
394 struct io_apic __iomem *io_apic = io_apic_base(apic);
395 writel(reg, &io_apic->index);
396 writel(value, &io_apic->data);
397}
398
399/*
400 * Re-write a value: to be used for read-modify-write
401 * cycles where the read already set up the index register.
402 *
403 * Older SiS APIC requires we rewrite the index register
404 */
405static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
406{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200407 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200408
409 if (sis_apic_bug)
410 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800411 writel(value, &io_apic->data);
412}
413
Yinghai Lu3145e942008-12-05 18:58:34 -0800414static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700415{
416 struct irq_pin_list *entry;
417 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700418
419 spin_lock_irqsave(&ioapic_lock, flags);
420 entry = cfg->irq_2_pin;
421 for (;;) {
422 unsigned int reg;
423 int pin;
424
425 if (!entry)
426 break;
427 pin = entry->pin;
428 reg = io_apic_read(entry->apic, 0x10 + pin*2);
429 /* Is the remote IRR bit set? */
430 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
431 spin_unlock_irqrestore(&ioapic_lock, flags);
432 return true;
433 }
434 if (!entry->next)
435 break;
436 entry = entry->next;
437 }
438 spin_unlock_irqrestore(&ioapic_lock, flags);
439
440 return false;
441}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700442
Andi Kleencf4c6a22006-09-26 10:52:30 +0200443union entry_union {
444 struct { u32 w1, w2; };
445 struct IO_APIC_route_entry entry;
446};
447
448static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
449{
450 union entry_union eu;
451 unsigned long flags;
452 spin_lock_irqsave(&ioapic_lock, flags);
453 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
454 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
455 spin_unlock_irqrestore(&ioapic_lock, flags);
456 return eu.entry;
457}
458
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800459/*
460 * When we write a new IO APIC routing entry, we need to write the high
461 * word first! If the mask bit in the low word is clear, we will enable
462 * the interrupt, and we need to make sure the entry is fully populated
463 * before that happens.
464 */
Andi Kleend15512f2006-12-07 02:14:07 +0100465static void
466__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
467{
468 union entry_union eu;
469 eu.entry = e;
470 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
471 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
472}
473
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -0800474void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
Andi Kleencf4c6a22006-09-26 10:52:30 +0200475{
476 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200477 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100478 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800479 spin_unlock_irqrestore(&ioapic_lock, flags);
480}
481
482/*
483 * When we mask an IO APIC routing entry, we need to write the low
484 * word first, in order to set the mask bit before we change the
485 * high bits!
486 */
487static void ioapic_mask_entry(int apic, int pin)
488{
489 unsigned long flags;
490 union entry_union eu = { .entry.mask = 1 };
491
492 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200493 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
494 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
495 spin_unlock_irqrestore(&ioapic_lock, flags);
496}
497
Yinghai Lu497c9a12008-08-19 20:50:28 -0700498#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -0800499static void send_cleanup_vector(struct irq_cfg *cfg)
500{
501 cpumask_var_t cleanup_mask;
502
503 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
504 unsigned int i;
505 cfg->move_cleanup_count = 0;
506 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
507 cfg->move_cleanup_count++;
508 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
Ingo Molnardac5f412009-01-28 15:42:24 +0100509 apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
Mike Travis22f65d32008-12-16 17:33:56 -0800510 } else {
511 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
512 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
Ingo Molnardac5f412009-01-28 15:42:24 +0100513 apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
Mike Travis22f65d32008-12-16 17:33:56 -0800514 free_cpumask_var(cleanup_mask);
515 }
516 cfg->move_in_progress = 0;
517}
518
Yinghai Lu3145e942008-12-05 18:58:34 -0800519static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700520{
521 int apic, pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700522 struct irq_pin_list *entry;
Yinghai Lu3145e942008-12-05 18:58:34 -0800523 u8 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700524
Yinghai Lu497c9a12008-08-19 20:50:28 -0700525 entry = cfg->irq_2_pin;
526 for (;;) {
527 unsigned int reg;
528
529 if (!entry)
530 break;
531
532 apic = entry->apic;
533 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200534 /*
535 * With interrupt-remapping, destination information comes
536 * from interrupt-remapping table entry.
537 */
538 if (!irq_remapped(irq))
539 io_apic_write(apic, 0x11 + pin*2, dest);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700540 reg = io_apic_read(apic, 0x10 + pin*2);
541 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
542 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200543 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700544 if (!entry->next)
545 break;
546 entry = entry->next;
547 }
548}
Yinghai Luefa25592008-08-19 20:50:36 -0700549
Mike Travise7986732008-12-16 17:33:52 -0800550static int
551assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
Yinghai Luefa25592008-08-19 20:50:36 -0700552
Mike Travis22f65d32008-12-16 17:33:56 -0800553/*
Ingo Molnardebccb32009-01-28 15:20:18 +0100554 * Either sets desc->affinity to a valid value, and returns
555 * ->cpu_mask_to_apicid of that, or returns BAD_APICID and
556 * leaves desc->affinity untouched.
Mike Travis22f65d32008-12-16 17:33:56 -0800557 */
558static unsigned int
559set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700560{
561 struct irq_cfg *cfg;
Yinghai Lu3145e942008-12-05 18:58:34 -0800562 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700563
Rusty Russell0de26522008-12-13 21:20:26 +1030564 if (!cpumask_intersects(mask, cpu_online_mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800565 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700566
Yinghai Lu3145e942008-12-05 18:58:34 -0800567 irq = desc->irq;
568 cfg = desc->chip_data;
569 if (assign_irq_vector(irq, cfg, mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800570 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700571
Rusty Russelle06b1b52009-03-24 14:17:19 -0700572 cpumask_copy(desc->affinity, mask);
573
574 return apic->cpu_mask_to_apicid_and(desc->affinity, cfg->domain);
Mike Travis22f65d32008-12-16 17:33:56 -0800575}
Yinghai Lu3145e942008-12-05 18:58:34 -0800576
Yinghai Lud5dedd42009-04-27 17:59:21 -0700577static int
Mike Travis22f65d32008-12-16 17:33:56 -0800578set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700579{
580 struct irq_cfg *cfg;
581 unsigned long flags;
582 unsigned int dest;
Mike Travis22f65d32008-12-16 17:33:56 -0800583 unsigned int irq;
Yinghai Lud5dedd42009-04-27 17:59:21 -0700584 int ret = -1;
Mike Travis22f65d32008-12-16 17:33:56 -0800585
586 irq = desc->irq;
587 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700588
589 spin_lock_irqsave(&ioapic_lock, flags);
Mike Travis22f65d32008-12-16 17:33:56 -0800590 dest = set_desc_affinity(desc, mask);
591 if (dest != BAD_APICID) {
592 /* Only the high 8 bits are valid. */
593 dest = SET_APIC_LOGICAL_ID(dest);
594 __target_IO_APIC_irq(irq, dest, cfg);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700595 ret = 0;
Mike Travis22f65d32008-12-16 17:33:56 -0800596 }
Yinghai Lu497c9a12008-08-19 20:50:28 -0700597 spin_unlock_irqrestore(&ioapic_lock, flags);
Yinghai Lud5dedd42009-04-27 17:59:21 -0700598
599 return ret;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700600}
Yinghai Lu3145e942008-12-05 18:58:34 -0800601
Yinghai Lud5dedd42009-04-27 17:59:21 -0700602static int
Mike Travis22f65d32008-12-16 17:33:56 -0800603set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800604{
Yinghai Lu497c9a12008-08-19 20:50:28 -0700605 struct irq_desc *desc;
606
Yinghai Lu497c9a12008-08-19 20:50:28 -0700607 desc = irq_to_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800608
Yinghai Lud5dedd42009-04-27 17:59:21 -0700609 return set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700610}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700611#endif /* CONFIG_SMP */
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613/*
614 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
615 * shared ISA-space IRQs, so we have to support them. We are super
616 * fast in the common case, and fast for shared ISA-space IRQs.
617 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800618static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700620 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Yinghai Lu0f978f42008-08-19 20:50:26 -0700622 entry = cfg->irq_2_pin;
623 if (!entry) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800624 entry = get_one_free_irq_2_pin(cpu);
625 if (!entry) {
626 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
627 apic, pin);
628 return;
629 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700630 cfg->irq_2_pin = entry;
631 entry->apic = apic;
632 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700633 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700635
636 while (entry->next) {
637 /* not again, please */
638 if (entry->apic == apic && entry->pin == pin)
639 return;
640
641 entry = entry->next;
642 }
643
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800644 entry->next = get_one_free_irq_2_pin(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700645 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 entry->apic = apic;
647 entry->pin = pin;
648}
649
650/*
651 * Reroute an IRQ to a different pin.
652 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800653static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 int oldapic, int oldpin,
655 int newapic, int newpin)
656{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700657 struct irq_pin_list *entry = cfg->irq_2_pin;
658 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Yinghai Lu0f978f42008-08-19 20:50:26 -0700660 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (entry->apic == oldapic && entry->pin == oldpin) {
662 entry->apic = newapic;
663 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700664 replaced = 1;
665 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700667 }
668 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700670
671 /* why? call replace before add? */
672 if (!replaced)
Yinghai Lu3145e942008-12-05 18:58:34 -0800673 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Yinghai Lu3145e942008-12-05 18:58:34 -0800676static inline void io_apic_modify_irq(struct irq_cfg *cfg,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400677 int mask_and, int mask_or,
678 void (*final)(struct irq_pin_list *entry))
679{
680 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400681 struct irq_pin_list *entry;
682
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400683 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
684 unsigned int reg;
685 pin = entry->pin;
686 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
687 reg &= mask_and;
688 reg |= mask_or;
689 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
690 if (final)
691 final(entry);
692 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700693}
694
Yinghai Lu3145e942008-12-05 18:58:34 -0800695static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400696{
Yinghai Lu3145e942008-12-05 18:58:34 -0800697 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400698}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700699
700#ifdef CONFIG_X86_64
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530701static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700702{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400703 /*
704 * Synchronize the IO-APIC and the CPU by doing
705 * a dummy read from the IO-APIC
706 */
707 struct io_apic __iomem *io_apic;
708 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700709 readl(&io_apic->data);
710}
711
Yinghai Lu3145e942008-12-05 18:58:34 -0800712static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400713{
Yinghai Lu3145e942008-12-05 18:58:34 -0800714 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400715}
716#else /* CONFIG_X86_32 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800717static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400718{
Yinghai Lu3145e942008-12-05 18:58:34 -0800719 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400720}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700721
Yinghai Lu3145e942008-12-05 18:58:34 -0800722static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400723{
Yinghai Lu3145e942008-12-05 18:58:34 -0800724 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400725 IO_APIC_REDIR_MASKED, NULL);
726}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700727
Yinghai Lu3145e942008-12-05 18:58:34 -0800728static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400729{
Yinghai Lu3145e942008-12-05 18:58:34 -0800730 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400731 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
732}
733#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700734
Yinghai Lu3145e942008-12-05 18:58:34 -0800735static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
Yinghai Lu3145e942008-12-05 18:58:34 -0800737 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned long flags;
739
Yinghai Lu3145e942008-12-05 18:58:34 -0800740 BUG_ON(!cfg);
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800743 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 spin_unlock_irqrestore(&ioapic_lock, flags);
745}
746
Yinghai Lu3145e942008-12-05 18:58:34 -0800747static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Yinghai Lu3145e942008-12-05 18:58:34 -0800749 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 unsigned long flags;
751
752 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800753 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 spin_unlock_irqrestore(&ioapic_lock, flags);
755}
756
Yinghai Lu3145e942008-12-05 18:58:34 -0800757static void mask_IO_APIC_irq(unsigned int irq)
758{
759 struct irq_desc *desc = irq_to_desc(irq);
760
761 mask_IO_APIC_irq_desc(desc);
762}
763static void unmask_IO_APIC_irq(unsigned int irq)
764{
765 struct irq_desc *desc = irq_to_desc(irq);
766
767 unmask_IO_APIC_irq_desc(desc);
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
771{
772 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200775 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (entry.delivery_mode == dest_SMI)
777 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /*
779 * Disable it in the IO-APIC irq-routing table:
780 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800781 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
Ingo Molnar54168ed2008-08-20 09:07:45 +0200784static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 int apic, pin;
787
788 for (apic = 0; apic < nr_ioapics; apic++)
789 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
790 clear_IO_APIC_pin(apic, pin);
791}
792
Ingo Molnar54168ed2008-08-20 09:07:45 +0200793#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794/*
795 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
796 * specific CPU-side IRQs.
797 */
798
799#define MAX_PIRQS 8
Yinghai Lu3bd25d02009-02-15 02:54:03 -0800800static int pirq_entries[MAX_PIRQS] = {
801 [0 ... MAX_PIRQS - 1] = -1
802};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804static int __init ioapic_pirq_setup(char *str)
805{
806 int i, max;
807 int ints[MAX_PIRQS+1];
808
809 get_options(str, ARRAY_SIZE(ints), ints);
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 apic_printk(APIC_VERBOSE, KERN_INFO
812 "PIRQ redirection, working around broken MP-BIOS.\n");
813 max = MAX_PIRQS;
814 if (ints[0] < MAX_PIRQS)
815 max = ints[0];
816
817 for (i = 0; i < max; i++) {
818 apic_printk(APIC_VERBOSE, KERN_DEBUG
819 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
820 /*
821 * PIRQs are mapped upside down, usually.
822 */
823 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
824 }
825 return 1;
826}
827
828__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200829#endif /* CONFIG_X86_32 */
830
831#ifdef CONFIG_INTR_REMAP
Fenghua Yub24696b2009-03-27 14:22:44 -0700832struct IO_APIC_route_entry **alloc_ioapic_entries(void)
833{
834 int apic;
835 struct IO_APIC_route_entry **ioapic_entries;
836
837 ioapic_entries = kzalloc(sizeof(*ioapic_entries) * nr_ioapics,
838 GFP_ATOMIC);
839 if (!ioapic_entries)
840 return 0;
841
842 for (apic = 0; apic < nr_ioapics; apic++) {
843 ioapic_entries[apic] =
844 kzalloc(sizeof(struct IO_APIC_route_entry) *
845 nr_ioapic_registers[apic], GFP_ATOMIC);
846 if (!ioapic_entries[apic])
847 goto nomem;
848 }
849
850 return ioapic_entries;
851
852nomem:
853 while (--apic >= 0)
854 kfree(ioapic_entries[apic]);
855 kfree(ioapic_entries);
856
857 return 0;
858}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200859
860/*
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700861 * Saves all the IO-APIC RTE's
Ingo Molnar54168ed2008-08-20 09:07:45 +0200862 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700863int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200864{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200865 int apic, pin;
866
Fenghua Yub24696b2009-03-27 14:22:44 -0700867 if (!ioapic_entries)
868 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200869
870 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700871 if (!ioapic_entries[apic])
872 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200873
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700874 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
Fenghua Yub24696b2009-03-27 14:22:44 -0700875 ioapic_entries[apic][pin] =
Ingo Molnar54168ed2008-08-20 09:07:45 +0200876 ioapic_read_entry(apic, pin);
Fenghua Yub24696b2009-03-27 14:22:44 -0700877 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400878
Ingo Molnar54168ed2008-08-20 09:07:45 +0200879 return 0;
880}
881
Fenghua Yub24696b2009-03-27 14:22:44 -0700882/*
883 * Mask all IO APIC entries.
884 */
885void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700886{
887 int apic, pin;
888
Fenghua Yub24696b2009-03-27 14:22:44 -0700889 if (!ioapic_entries)
890 return;
891
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700892 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700893 if (!ioapic_entries[apic])
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700894 break;
Fenghua Yub24696b2009-03-27 14:22:44 -0700895
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700896 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
897 struct IO_APIC_route_entry entry;
898
Fenghua Yub24696b2009-03-27 14:22:44 -0700899 entry = ioapic_entries[apic][pin];
Suresh Siddha05c3dc22009-03-16 17:05:03 -0700900 if (!entry.mask) {
901 entry.mask = 1;
902 ioapic_write_entry(apic, pin, entry);
903 }
904 }
905 }
906}
907
Fenghua Yub24696b2009-03-27 14:22:44 -0700908/*
909 * Restore IO APIC entries which was saved in ioapic_entries.
910 */
911int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200912{
913 int apic, pin;
914
Fenghua Yub24696b2009-03-27 14:22:44 -0700915 if (!ioapic_entries)
916 return -ENOMEM;
917
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400918 for (apic = 0; apic < nr_ioapics; apic++) {
Fenghua Yub24696b2009-03-27 14:22:44 -0700919 if (!ioapic_entries[apic])
920 return -ENOMEM;
921
Ingo Molnar54168ed2008-08-20 09:07:45 +0200922 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
923 ioapic_write_entry(apic, pin,
Fenghua Yub24696b2009-03-27 14:22:44 -0700924 ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400925 }
Fenghua Yub24696b2009-03-27 14:22:44 -0700926 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200927}
928
Fenghua Yub24696b2009-03-27 14:22:44 -0700929void reinit_intr_remapped_IO_APIC(int intr_remapping,
930 struct IO_APIC_route_entry **ioapic_entries)
931
Ingo Molnar54168ed2008-08-20 09:07:45 +0200932{
933 /*
934 * for now plain restore of previous settings.
935 * TBD: In the case of OS enabling interrupt-remapping,
936 * IO-APIC RTE's need to be setup to point to interrupt-remapping
937 * table entries. for now, do a plain restore, and wait for
938 * the setup_IO_APIC_irqs() to do proper initialization.
939 */
Fenghua Yub24696b2009-03-27 14:22:44 -0700940 restore_IO_APIC_setup(ioapic_entries);
941}
942
943void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
944{
945 int apic;
946
947 for (apic = 0; apic < nr_ioapics; apic++)
948 kfree(ioapic_entries[apic]);
949
950 kfree(ioapic_entries);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200951}
952#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954/*
955 * Find the IRQ entry number of a certain pin.
956 */
957static int find_irq_entry(int apic, int pin, int type)
958{
959 int i;
960
961 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530962 if (mp_irqs[i].irqtype == type &&
963 (mp_irqs[i].dstapic == mp_ioapics[apic].apicid ||
964 mp_irqs[i].dstapic == MP_APIC_ALL) &&
965 mp_irqs[i].dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return i;
967
968 return -1;
969}
970
971/*
972 * Find the pin to which IRQ[irq] (ISA) is connected
973 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800974static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 int i;
977
978 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530979 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300981 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530982 (mp_irqs[i].irqtype == type) &&
983 (mp_irqs[i].srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530985 return mp_irqs[i].dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
987 return -1;
988}
989
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800990static int __init find_isa_irq_apic(int irq, int type)
991{
992 int i;
993
994 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530995 int lbus = mp_irqs[i].srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800996
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300997 if (test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +0530998 (mp_irqs[i].irqtype == type) &&
999 (mp_irqs[i].srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001000 break;
1001 }
1002 if (i < mp_irq_entries) {
1003 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001004 for(apic = 0; apic < nr_ioapics; apic++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301005 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001006 return apic;
1007 }
1008 }
1009
1010 return -1;
1011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013/*
1014 * Find a specific PCI IRQ entry.
1015 * Not an __init, possibly needed by modules
1016 */
1017static int pin_2_irq(int idx, int apic, int pin);
1018
1019int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
1020{
1021 int apic, i, best_guess = -1;
1022
Ingo Molnar54168ed2008-08-20 09:07:45 +02001023 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1024 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +04001025 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001026 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -1;
1028 }
1029 for (i = 0; i < mp_irq_entries; i++) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301030 int lbus = mp_irqs[i].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 for (apic = 0; apic < nr_ioapics; apic++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301033 if (mp_ioapics[apic].apicid == mp_irqs[i].dstapic ||
1034 mp_irqs[i].dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 break;
1036
Alexey Starikovskiy47cab822008-03-20 14:54:30 +03001037 if (!test_bit(lbus, mp_bus_not_pci) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301038 !mp_irqs[i].irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 (bus == lbus) &&
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301040 (slot == ((mp_irqs[i].srcbusirq >> 2) & 0x1f))) {
1041 int irq = pin_2_irq(i, apic, mp_irqs[i].dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 if (!(apic || IO_APIC_IRQ(irq)))
1044 continue;
1045
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301046 if (pin == (mp_irqs[i].srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return irq;
1048 /*
1049 * Use the first all-but-pin matching entry as a
1050 * best-guess fuzzy result for broken mptables.
1051 */
1052 if (best_guess < 0)
1053 best_guess = irq;
1054 }
1055 }
1056 return best_guess;
1057}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001058
Alexey Dobriyan129f6942005-06-23 00:08:33 -07001059EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001061#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*
1063 * EISA Edge/Level control register, ELCR
1064 */
1065static int EISA_ELCR(unsigned int irq)
1066{
Yinghai Lu99d093d2008-12-05 18:58:32 -08001067 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 unsigned int port = 0x4d0 + (irq >> 3);
1069 return (inb(port) >> (irq & 7)) & 1;
1070 }
1071 apic_printk(APIC_VERBOSE, KERN_INFO
1072 "Broken MPtable reports ISA irq %d\n", irq);
1073 return 0;
1074}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001075
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001076#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001078/* ISA interrupts are always polarity zero edge triggered,
1079 * when listed as conforming in the MP table. */
1080
1081#define default_ISA_trigger(idx) (0)
1082#define default_ISA_polarity(idx) (0)
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084/* EISA interrupts are always polarity zero and can be edge or level
1085 * trigger depending on the ELCR value. If an interrupt is listed as
1086 * EISA conforming in the MP table, that means its trigger type must
1087 * be read in from the ELCR */
1088
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301089#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001090#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092/* PCI interrupts are always polarity one level triggered,
1093 * when listed as conforming in the MP table. */
1094
1095#define default_PCI_trigger(idx) (1)
1096#define default_PCI_polarity(idx) (1)
1097
1098/* MCA interrupts are always polarity zero level triggered,
1099 * when listed as conforming in the MP table. */
1100
1101#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001102#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Shaohua Li61fd47e2007-11-17 01:05:28 -05001104static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301106 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 int polarity;
1108
1109 /*
1110 * Determine IRQ line polarity (high active or low active):
1111 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301112 switch (mp_irqs[idx].irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001114 case 0: /* conforms, ie. bus-type dependent polarity */
1115 if (test_bit(bus, mp_bus_not_pci))
1116 polarity = default_ISA_polarity(idx);
1117 else
1118 polarity = default_PCI_polarity(idx);
1119 break;
1120 case 1: /* high active */
1121 {
1122 polarity = 0;
1123 break;
1124 }
1125 case 2: /* reserved */
1126 {
1127 printk(KERN_WARNING "broken BIOS!!\n");
1128 polarity = 1;
1129 break;
1130 }
1131 case 3: /* low active */
1132 {
1133 polarity = 1;
1134 break;
1135 }
1136 default: /* invalid */
1137 {
1138 printk(KERN_WARNING "broken BIOS!!\n");
1139 polarity = 1;
1140 break;
1141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143 return polarity;
1144}
1145
1146static int MPBIOS_trigger(int idx)
1147{
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301148 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int trigger;
1150
1151 /*
1152 * Determine IRQ trigger mode (edge or level sensitive):
1153 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301154 switch ((mp_irqs[idx].irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001156 case 0: /* conforms, ie. bus-type dependent */
1157 if (test_bit(bus, mp_bus_not_pci))
1158 trigger = default_ISA_trigger(idx);
1159 else
1160 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001161#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001162 switch (mp_bus_id_to_type[bus]) {
1163 case MP_BUS_ISA: /* ISA pin */
1164 {
1165 /* set before the switch */
1166 break;
1167 }
1168 case MP_BUS_EISA: /* EISA pin */
1169 {
1170 trigger = default_EISA_trigger(idx);
1171 break;
1172 }
1173 case MP_BUS_PCI: /* PCI pin */
1174 {
1175 /* set before the switch */
1176 break;
1177 }
1178 case MP_BUS_MCA: /* MCA pin */
1179 {
1180 trigger = default_MCA_trigger(idx);
1181 break;
1182 }
1183 default:
1184 {
1185 printk(KERN_WARNING "broken BIOS!!\n");
1186 trigger = 1;
1187 break;
1188 }
1189 }
1190#endif
1191 break;
1192 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001193 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001194 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001195 break;
1196 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001197 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001198 {
1199 printk(KERN_WARNING "broken BIOS!!\n");
1200 trigger = 1;
1201 break;
1202 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001203 case 3: /* level */
1204 {
1205 trigger = 1;
1206 break;
1207 }
1208 default: /* invalid */
1209 {
1210 printk(KERN_WARNING "broken BIOS!!\n");
1211 trigger = 0;
1212 break;
1213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 return trigger;
1216}
1217
1218static inline int irq_polarity(int idx)
1219{
1220 return MPBIOS_polarity(idx);
1221}
1222
1223static inline int irq_trigger(int idx)
1224{
1225 return MPBIOS_trigger(idx);
1226}
1227
Yinghai Luefa25592008-08-19 20:50:36 -07001228int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229static int pin_2_irq(int idx, int apic, int pin)
1230{
1231 int irq, i;
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301232 int bus = mp_irqs[idx].srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /*
1235 * Debugging check, we are in big trouble if this message pops up!
1236 */
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301237 if (mp_irqs[idx].dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1239
Ingo Molnar54168ed2008-08-20 09:07:45 +02001240 if (test_bit(bus, mp_bus_not_pci)) {
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05301241 irq = mp_irqs[idx].srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001242 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001243 /*
1244 * PCI IRQs are mapped in order
1245 */
1246 i = irq = 0;
1247 while (i < apic)
1248 irq += nr_ioapic_registers[i++];
1249 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001250 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001251 * For MPS mode, so far only needed by ES7000 platform
1252 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001253 if (ioapic_renumber_irq)
1254 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
Ingo Molnar54168ed2008-08-20 09:07:45 +02001257#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /*
1259 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1260 */
1261 if ((pin >= 16) && (pin <= 23)) {
1262 if (pirq_entries[pin-16] != -1) {
1263 if (!pirq_entries[pin-16]) {
1264 apic_printk(APIC_VERBOSE, KERN_DEBUG
1265 "disabling PIRQ%d\n", pin-16);
1266 } else {
1267 irq = pirq_entries[pin-16];
1268 apic_printk(APIC_VERBOSE, KERN_DEBUG
1269 "using PIRQ%d -> IRQ %d\n",
1270 pin-16, irq);
1271 }
1272 }
1273 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001274#endif
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return irq;
1277}
1278
Yinghai Lu497c9a12008-08-19 20:50:28 -07001279void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001281 /* Used to the online set of cpus does not change
1282 * during assign_irq_vector.
1283 */
1284 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
Yinghai Lu497c9a12008-08-19 20:50:28 -07001287void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001288{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001289 spin_unlock(&vector_lock);
1290}
1291
Mike Travise7986732008-12-16 17:33:52 -08001292static int
1293__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001294{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001295 /*
1296 * NOTE! The local APIC isn't very good at handling
1297 * multiple interrupts at the same interrupt level.
1298 * As the interrupt level is determined by taking the
1299 * vector number and shifting that right by 4, we
1300 * want to spread these out a bit so that they don't
1301 * all fall in the same interrupt level.
1302 *
1303 * Also, we've got to be careful not to trash gate
1304 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1305 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001306 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1307 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001308 int cpu, err;
1309 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001310
Ingo Molnar54168ed2008-08-20 09:07:45 +02001311 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1312 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001313
Mike Travis22f65d32008-12-16 17:33:56 -08001314 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1315 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001316
Ingo Molnar54168ed2008-08-20 09:07:45 +02001317 old_vector = cfg->vector;
1318 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001319 cpumask_and(tmp_mask, mask, cpu_online_mask);
1320 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1321 if (!cpumask_empty(tmp_mask)) {
1322 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001323 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001324 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001325 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001326
Mike Travise7986732008-12-16 17:33:52 -08001327 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001328 err = -ENOSPC;
1329 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001330 int new_cpu;
1331 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001332
Ingo Molnare2d40b12009-01-28 06:50:47 +01001333 apic->vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001334
Ingo Molnar54168ed2008-08-20 09:07:45 +02001335 vector = current_vector;
1336 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001337next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001338 vector += 8;
1339 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001340 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001341 offset = (offset + 1) % 8;
1342 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001343 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001344 if (unlikely(current_vector == vector))
1345 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001346
1347 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001348 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001349
Mike Travis22f65d32008-12-16 17:33:56 -08001350 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001351 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1352 goto next;
1353 /* Found one! */
1354 current_vector = vector;
1355 current_offset = offset;
1356 if (old_vector) {
1357 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001358 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001359 }
Mike Travis22f65d32008-12-16 17:33:56 -08001360 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001361 per_cpu(vector_irq, new_cpu)[vector] = irq;
1362 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001363 cpumask_copy(cfg->domain, tmp_mask);
1364 err = 0;
1365 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001366 }
Mike Travis22f65d32008-12-16 17:33:56 -08001367 free_cpumask_var(tmp_mask);
1368 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001369}
1370
Mike Travise7986732008-12-16 17:33:52 -08001371static int
1372assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001373{
1374 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001375 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001376
1377 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001378 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001379 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001380 return err;
1381}
1382
Yinghai Lu3145e942008-12-05 18:58:34 -08001383static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001384{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001385 int cpu, vector;
1386
Yinghai Lu497c9a12008-08-19 20:50:28 -07001387 BUG_ON(!cfg->vector);
1388
1389 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001390 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001391 per_cpu(vector_irq, cpu)[vector] = -1;
1392
1393 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001394 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001395
1396 if (likely(!cfg->move_in_progress))
1397 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001398 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001399 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1400 vector++) {
1401 if (per_cpu(vector_irq, cpu)[vector] != irq)
1402 continue;
1403 per_cpu(vector_irq, cpu)[vector] = -1;
1404 break;
1405 }
1406 }
1407 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001408}
1409
1410void __setup_vector_irq(int cpu)
1411{
1412 /* Initialize vector_irq on a new cpu */
1413 /* This function must be called with vector_lock held */
1414 int irq, vector;
1415 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001416 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001417
1418 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001419 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001420 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001421 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001422 continue;
1423 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001424 per_cpu(vector_irq, cpu)[vector] = irq;
1425 }
1426 /* Mark the free vectors */
1427 for (vector = 0; vector < NR_VECTORS; ++vector) {
1428 irq = per_cpu(vector_irq, cpu)[vector];
1429 if (irq < 0)
1430 continue;
1431
1432 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001433 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001434 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001435 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001436}
Glauber Costa3fde6902008-05-28 20:34:19 -07001437
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001438static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001439static struct irq_chip ir_ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Ingo Molnar54168ed2008-08-20 09:07:45 +02001441#define IOAPIC_AUTO -1
1442#define IOAPIC_EDGE 0
1443#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001445#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001446static inline int IO_APIC_irq_trigger(int irq)
1447{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001448 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001449
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001450 for (apic = 0; apic < nr_ioapics; apic++) {
1451 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1452 idx = find_irq_entry(apic, pin, mp_INT);
1453 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1454 return irq_trigger(idx);
1455 }
1456 }
1457 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001458 * nonexistent IRQs are edge default
1459 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001460 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001461}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001462#else
1463static inline int IO_APIC_irq_trigger(int irq)
1464{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001465 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001466}
1467#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001468
Yinghai Lu3145e942008-12-05 18:58:34 -08001469static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Yinghai Lu199751d2008-08-19 20:50:27 -07001471
Jan Beulich6ebcc002006-06-26 13:56:46 +02001472 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001473 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001474 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001475 else
1476 desc->status &= ~IRQ_LEVEL;
1477
Ingo Molnar54168ed2008-08-20 09:07:45 +02001478 if (irq_remapped(irq)) {
1479 desc->status |= IRQ_MOVE_PCNTXT;
1480 if (trigger)
1481 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1482 handle_fasteoi_irq,
1483 "fasteoi");
1484 else
1485 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1486 handle_edge_irq, "edge");
1487 return;
1488 }
Suresh Siddha29b61be2009-03-16 17:05:02 -07001489
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001490 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1491 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001492 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001493 handle_fasteoi_irq,
1494 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001495 else
Ingo Molnara460e742006-10-17 00:10:03 -07001496 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001497 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001498}
1499
Jeremy Fitzhardingeca97ab92009-02-09 12:05:47 -08001500int setup_ioapic_entry(int apic_id, int irq,
1501 struct IO_APIC_route_entry *entry,
1502 unsigned int destination, int trigger,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001503 int polarity, int vector, int pin)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001504{
1505 /*
1506 * add it to the IO-APIC irq-routing table:
1507 */
1508 memset(entry,0,sizeof(*entry));
1509
Ingo Molnar54168ed2008-08-20 09:07:45 +02001510 if (intr_remapping_enabled) {
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001511 struct intel_iommu *iommu = map_ioapic_to_ir(apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001512 struct irte irte;
1513 struct IR_IO_APIC_route_entry *ir_entry =
1514 (struct IR_IO_APIC_route_entry *) entry;
1515 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001516
Ingo Molnar54168ed2008-08-20 09:07:45 +02001517 if (!iommu)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001518 panic("No mapping iommu for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001519
1520 index = alloc_irte(iommu, irq, 1);
1521 if (index < 0)
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001522 panic("Failed to allocate IRTE for ioapic %d\n", apic_id);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001523
1524 memset(&irte, 0, sizeof(irte));
1525
1526 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001527 irte.dst_mode = apic->irq_dest_mode;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001528 /*
1529 * Trigger mode in the IRTE will always be edge, and the
1530 * actual level or edge trigger will be setup in the IO-APIC
1531 * RTE. This will help simplify level triggered irq migration.
1532 * For more details, see the comments above explainig IO-APIC
1533 * irq migration in the presence of interrupt-remapping.
1534 */
1535 irte.trigger_mode = 0;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001536 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001537 irte.vector = vector;
1538 irte.dest_id = IRTE_DEST(destination);
1539
1540 modify_irte(irq, &irte);
1541
1542 ir_entry->index2 = (index >> 15) & 0x1;
1543 ir_entry->zero = 0;
1544 ir_entry->format = 1;
1545 ir_entry->index = (index & 0x7fff);
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001546 /*
1547 * IO-APIC RTE will be configured with virtual vector.
1548 * irq handler will do the explicit EOI to the io-apic.
1549 */
1550 ir_entry->vector = pin;
Suresh Siddha29b61be2009-03-16 17:05:02 -07001551 } else {
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001552 entry->delivery_mode = apic->irq_delivery_mode;
1553 entry->dest_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001554 entry->dest = destination;
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001555 entry->vector = vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001556 }
1557
1558 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001559 entry->trigger = trigger;
1560 entry->polarity = polarity;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001561
1562 /* Mask level triggered irqs.
1563 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1564 */
1565 if (trigger)
1566 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001567 return 0;
1568}
1569
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001570static void setup_IO_APIC_irq(int apic_id, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001571 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001572{
1573 struct irq_cfg *cfg;
1574 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001575 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001576
1577 if (!IO_APIC_IRQ(irq))
1578 return;
1579
Yinghai Lu3145e942008-12-05 18:58:34 -08001580 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001581
Ingo Molnarfe402e12009-01-28 04:32:51 +01001582 if (assign_irq_vector(irq, cfg, apic->target_cpus()))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001583 return;
1584
Ingo Molnardebccb32009-01-28 15:20:18 +01001585 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07001586
1587 apic_printk(APIC_VERBOSE,KERN_DEBUG
1588 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1589 "IRQ %d Mode:%i Active:%i)\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001590 apic_id, mp_ioapics[apic_id].apicid, pin, cfg->vector,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001591 irq, trigger, polarity);
1592
1593
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001594 if (setup_ioapic_entry(mp_ioapics[apic_id].apicid, irq, &entry,
Suresh Siddha0280f7c2009-03-16 17:05:01 -07001595 dest, trigger, polarity, cfg->vector, pin)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001596 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001597 mp_ioapics[apic_id].apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001598 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001599 return;
1600 }
1601
Yinghai Lu3145e942008-12-05 18:58:34 -08001602 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001603 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001604 disable_8259A_irq(irq);
1605
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001606 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
1609static void __init setup_IO_APIC_irqs(void)
1610{
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001611 int apic_id, pin, idx, irq;
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001612 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001613 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001614 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001615 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
1617 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1618
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001619 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
1620 for (pin = 0; pin < nr_ioapic_registers[apic_id]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001622 idx = find_irq_entry(apic_id, pin, mp_INT);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001623 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001624 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001625 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001626 apic_printk(APIC_VERBOSE,
1627 KERN_DEBUG " %d-%d",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001628 mp_ioapics[apic_id].apicid, pin);
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001629 } else
1630 apic_printk(APIC_VERBOSE, " %d-%d",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001631 mp_ioapics[apic_id].apicid, pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001632 continue;
1633 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001634 if (notcon) {
1635 apic_printk(APIC_VERBOSE,
1636 " (apicid-pin) not connected\n");
1637 notcon = 0;
1638 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001639
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001640 irq = pin_2_irq(idx, apic_id, pin);
Ingo Molnar33a201f2009-01-28 07:17:26 +01001641
1642 /*
1643 * Skip the timer IRQ if there's a quirk handler
1644 * installed and if it returns 1:
1645 */
1646 if (apic->multi_timer_check &&
1647 apic->multi_timer_check(apic_id, irq))
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001648 continue;
Ingo Molnar33a201f2009-01-28 07:17:26 +01001649
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001650 desc = irq_to_desc_alloc_cpu(irq, cpu);
1651 if (!desc) {
1652 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1653 continue;
1654 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001655 cfg = desc->chip_data;
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001656 add_pin_to_irq_cpu(cfg, cpu, apic_id, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001657
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001658 setup_IO_APIC_irq(apic_id, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001659 irq_trigger(idx), irq_polarity(idx));
1660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001663 if (notcon)
1664 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001665 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
1668/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001669 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001671static void __init setup_timer_IRQ0_pin(unsigned int apic_id, unsigned int pin,
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001672 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
1674 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Ingo Molnar54168ed2008-08-20 09:07:45 +02001676 if (intr_remapping_enabled)
1677 return;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001678
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001679 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /*
1682 * We use logical delivery to get the timer IRQ
1683 * to the first CPU.
1684 */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001685 entry.dest_mode = apic->irq_dest_mode;
Yinghai Luf72dcca2009-02-08 16:18:03 -08001686 entry.mask = 0; /* don't mask IRQ for edge */
Ingo Molnardebccb32009-01-28 15:20:18 +01001687 entry.dest = apic->cpu_mask_to_apicid(apic->target_cpus());
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01001688 entry.delivery_mode = apic->irq_delivery_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 entry.polarity = 0;
1690 entry.trigger = 0;
1691 entry.vector = vector;
1692
1693 /*
1694 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001695 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001697 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 /*
1700 * Add it to the IO-APIC irq-routing table:
1701 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01001702 ioapic_write_entry(apic_id, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001705
1706__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 int apic, i;
1709 union IO_APIC_reg_00 reg_00;
1710 union IO_APIC_reg_01 reg_01;
1711 union IO_APIC_reg_02 reg_02;
1712 union IO_APIC_reg_03 reg_03;
1713 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001714 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001715 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001716 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
1718 if (apic_verbosity == APIC_QUIET)
1719 return;
1720
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001721 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 for (i = 0; i < nr_ioapics; i++)
1723 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301724 mp_ioapics[i].apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 /*
1727 * We are a bit conservative about what we expect. We have to
1728 * know about every hardware change ASAP.
1729 */
1730 printk(KERN_INFO "testing the IO APIC.......................\n");
1731
1732 for (apic = 0; apic < nr_ioapics; apic++) {
1733
1734 spin_lock_irqsave(&ioapic_lock, flags);
1735 reg_00.raw = io_apic_read(apic, 0);
1736 reg_01.raw = io_apic_read(apic, 1);
1737 if (reg_01.bits.version >= 0x10)
1738 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001739 if (reg_01.bits.version >= 0x20)
1740 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 spin_unlock_irqrestore(&ioapic_lock, flags);
1742
Ingo Molnar54168ed2008-08-20 09:07:45 +02001743 printk("\n");
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05301744 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1746 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1747 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1748 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Ingo Molnar54168ed2008-08-20 09:07:45 +02001750 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1754 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 /*
1757 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1758 * but the value of reg_02 is read as the previous read register
1759 * value, so ignore it if reg_02 == reg_01.
1760 */
1761 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1762 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1763 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
1765
1766 /*
1767 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1768 * or reg_03, but the value of reg_0[23] is read as the previous read
1769 * register value, so ignore it if reg_03 == reg_0[12].
1770 */
1771 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1772 reg_03.raw != reg_01.raw) {
1773 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1774 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
1776
1777 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1778
Yinghai Lud83e94a2008-08-19 20:50:33 -07001779 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1780 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 for (i = 0; i <= reg_01.bits.entries; i++) {
1783 struct IO_APIC_route_entry entry;
1784
Andi Kleencf4c6a22006-09-26 10:52:30 +02001785 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Ingo Molnar54168ed2008-08-20 09:07:45 +02001787 printk(KERN_DEBUG " %02x %03X ",
1788 i,
1789 entry.dest
1790 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1793 entry.mask,
1794 entry.trigger,
1795 entry.irr,
1796 entry.polarity,
1797 entry.delivery_status,
1798 entry.dest_mode,
1799 entry.delivery_mode,
1800 entry.vector
1801 );
1802 }
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001805 for_each_irq_desc(irq, desc) {
1806 struct irq_pin_list *entry;
1807
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001808 cfg = desc->chip_data;
1809 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001810 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001812 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 for (;;) {
1814 printk("-> %d:%d", entry->apic, entry->pin);
1815 if (!entry->next)
1816 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001817 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819 printk("\n");
1820 }
1821
1822 printk(KERN_INFO ".................................... done.\n");
1823
1824 return;
1825}
1826
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001827__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
1829 unsigned int v;
1830 int i, j;
1831
1832 if (apic_verbosity == APIC_QUIET)
1833 return;
1834
1835 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1836 for (i = 0; i < 8; i++) {
1837 v = apic_read(base + i*0x10);
1838 for (j = 0; j < 32; j++) {
1839 if (v & (1<<j))
1840 printk("1");
1841 else
1842 printk("0");
1843 }
1844 printk("\n");
1845 }
1846}
1847
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001848__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001851 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 if (apic_verbosity == APIC_QUIET)
1854 return;
1855
1856 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1857 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001858 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001859 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 v = apic_read(APIC_LVR);
1861 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1862 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001863 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 v = apic_read(APIC_TASKPRI);
1866 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1867
Ingo Molnar54168ed2008-08-20 09:07:45 +02001868 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001869 if (!APIC_XAPIC(ver)) {
1870 v = apic_read(APIC_ARBPRI);
1871 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1872 v & APIC_ARBPRI_MASK);
1873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 v = apic_read(APIC_PROCPRI);
1875 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1876 }
1877
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001878 /*
1879 * Remote read supported only in the 82489DX and local APIC for
1880 * Pentium processors.
1881 */
1882 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1883 v = apic_read(APIC_RRR);
1884 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1885 }
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 v = apic_read(APIC_LDR);
1888 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001889 if (!x2apic_enabled()) {
1890 v = apic_read(APIC_DFR);
1891 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 v = apic_read(APIC_SPIV);
1894 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1895
1896 printk(KERN_DEBUG "... APIC ISR field:\n");
1897 print_APIC_bitfield(APIC_ISR);
1898 printk(KERN_DEBUG "... APIC TMR field:\n");
1899 print_APIC_bitfield(APIC_TMR);
1900 printk(KERN_DEBUG "... APIC IRR field:\n");
1901 print_APIC_bitfield(APIC_IRR);
1902
Ingo Molnar54168ed2008-08-20 09:07:45 +02001903 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1904 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 v = apic_read(APIC_ESR);
1908 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1909 }
1910
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001911 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001912 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1913 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 v = apic_read(APIC_LVTT);
1916 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1917
1918 if (maxlvt > 3) { /* PC is LVT#4. */
1919 v = apic_read(APIC_LVTPC);
1920 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1921 }
1922 v = apic_read(APIC_LVT0);
1923 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1924 v = apic_read(APIC_LVT1);
1925 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1926
1927 if (maxlvt > 2) { /* ERR is LVT#3. */
1928 v = apic_read(APIC_LVTERR);
1929 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1930 }
1931
1932 v = apic_read(APIC_TMICT);
1933 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1934 v = apic_read(APIC_TMCCT);
1935 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1936 v = apic_read(APIC_TDCR);
1937 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1938 printk("\n");
1939}
1940
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001941__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001943 int cpu;
1944
1945 preempt_disable();
1946 for_each_online_cpu(cpu)
1947 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1948 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001951__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 unsigned int v;
1954 unsigned long flags;
1955
1956 if (apic_verbosity == APIC_QUIET)
1957 return;
1958
1959 printk(KERN_DEBUG "\nprinting PIC contents\n");
1960
1961 spin_lock_irqsave(&i8259A_lock, flags);
1962
1963 v = inb(0xa1) << 8 | inb(0x21);
1964 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1965
1966 v = inb(0xa0) << 8 | inb(0x20);
1967 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1968
Ingo Molnar54168ed2008-08-20 09:07:45 +02001969 outb(0x0b,0xa0);
1970 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001972 outb(0x0a,0xa0);
1973 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
1975 spin_unlock_irqrestore(&i8259A_lock, flags);
1976
1977 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1978
1979 v = inb(0x4d1) << 8 | inb(0x4d0);
1980 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1981}
1982
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001983__apicdebuginit(int) print_all_ICs(void)
1984{
1985 print_PIC();
1986 print_all_local_APICs();
1987 print_IO_APIC();
1988
1989 return 0;
1990}
1991
1992fs_initcall(print_all_ICs);
1993
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Yinghai Luefa25592008-08-19 20:50:36 -07001995/* Where if anywhere is the i8259 connect in external int mode */
1996static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1997
Ingo Molnar54168ed2008-08-20 09:07:45 +02001998void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999{
2000 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002001 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002002 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 unsigned long flags;
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 /*
2006 * The number of IO-APIC IRQ registers (== #pins):
2007 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002008 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002010 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002012 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
2013 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002014 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002015 int pin;
2016 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01002017 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002018 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02002019 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002020
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002021 /* If the interrupt line is enabled and in ExtInt mode
2022 * I have found the pin where the i8259 is connected.
2023 */
2024 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
2025 ioapic_i8259.apic = apic;
2026 ioapic_i8259.pin = pin;
2027 goto found_i8259;
2028 }
2029 }
2030 }
2031 found_i8259:
2032 /* Look to see what if the MP table has reported the ExtINT */
2033 /* If we could not find the appropriate pin by looking at the ioapic
2034 * the i8259 probably is not connected the ioapic but give the
2035 * mptable a chance anyway.
2036 */
2037 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
2038 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
2039 /* Trust the MP table if nothing is setup in the hardware */
2040 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
2041 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
2042 ioapic_i8259.pin = i8259_pin;
2043 ioapic_i8259.apic = i8259_apic;
2044 }
2045 /* Complain if the MP table and the hardware disagree */
2046 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
2047 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
2048 {
2049 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051
2052 /*
2053 * Do not trust the IO-APIC being empty at bootup
2054 */
2055 clear_IO_APIC();
2056}
2057
2058/*
2059 * Not an __init, needed by the reboot code
2060 */
2061void disable_IO_APIC(void)
2062{
2063 /*
2064 * Clear the IO-APIC before rebooting:
2065 */
2066 clear_IO_APIC();
2067
Eric W. Biederman650927e2005-06-25 14:57:44 -07002068 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02002069 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07002070 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02002071 * so legacy interrupts can be delivered.
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07002072 *
2073 * With interrupt-remapping, for now we will use virtual wire A mode,
2074 * as virtual wire B is little complex (need to configure both
2075 * IOAPIC RTE aswell as interrupt-remapping table entry).
2076 * As this gets called during crash dump, keep this simple for now.
Eric W. Biederman650927e2005-06-25 14:57:44 -07002077 */
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07002078 if (ioapic_i8259.pin != -1 && !intr_remapping_enabled) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07002079 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07002080
2081 memset(&entry, 0, sizeof(entry));
2082 entry.mask = 0; /* Enabled */
2083 entry.trigger = 0; /* Edge */
2084 entry.irr = 0;
2085 entry.polarity = 0; /* High */
2086 entry.delivery_status = 0;
2087 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002088 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07002089 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002090 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07002091
2092 /*
2093 * Add it to the IO-APIC irq-routing table:
2094 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02002095 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07002096 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002097
Suresh Siddha7c6d9f92009-03-16 17:04:59 -07002098 /*
2099 * Use virtual wire A mode when interrupt remapping is enabled.
2100 */
2101 disconnect_bsp_APIC(!intr_remapping_enabled && ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
Ingo Molnar54168ed2008-08-20 09:07:45 +02002104#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105/*
2106 * function to set the IO-APIC physical IDs based on the
2107 * values stored in the MPC table.
2108 *
2109 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2110 */
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112static void __init setup_ioapic_ids_from_mpc(void)
2113{
2114 union IO_APIC_reg_00 reg_00;
2115 physid_mask_t phys_id_present_map;
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002116 int apic_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 int i;
2118 unsigned char old_id;
2119 unsigned long flags;
2120
Yinghai Lua4dbc342008-07-25 02:14:28 -07002121 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07002122 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07002123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002125 * Don't check I/O APIC IDs for xAPIC systems. They have
2126 * no meaning without the serial APIC bus.
2127 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002128 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2129 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002130 return;
2131 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 * This is broken; anything with a real cpu count has to
2133 * circumvent this idiocy regardless.
2134 */
Ingo Molnard190cb82009-01-28 06:50:47 +01002135 phys_id_present_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 /*
2138 * Set the IOAPIC ID to the value stored in the MPC table.
2139 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002140 for (apic_id = 0; apic_id < nr_ioapics; apic_id++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 /* Read the register 0 value */
2143 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002144 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002146
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002147 old_id = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002149 if (mp_ioapics[apic_id].apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002151 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2153 reg_00.bits.ID);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002154 mp_ioapics[apic_id].apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 /*
2158 * Sanity check, is the ID really free? Every APIC in a
2159 * system must have a unique ID or we get lots of nice
2160 * 'stuck on smp_invalidate_needed IPI wait' messages.
2161 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01002162 if (apic->check_apicid_used(phys_id_present_map,
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002163 mp_ioapics[apic_id].apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002165 apic_id, mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 for (i = 0; i < get_physical_broadcast(); i++)
2167 if (!physid_isset(i, phys_id_present_map))
2168 break;
2169 if (i >= get_physical_broadcast())
2170 panic("Max APIC ID exceeded!\n");
2171 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2172 i);
2173 physid_set(i, phys_id_present_map);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002174 mp_ioapics[apic_id].apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 } else {
2176 physid_mask_t tmp;
Ingo Molnar80587142009-01-28 06:50:47 +01002177 tmp = apic->apicid_to_cpu_present(mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 apic_printk(APIC_VERBOSE, "Setting %d in the "
2179 "phys_id_present_map\n",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002180 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2182 }
2183
2184
2185 /*
2186 * We need to adjust the IRQ routing table
2187 * if the ID changed.
2188 */
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002189 if (old_id != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05302191 if (mp_irqs[i].dstapic == old_id)
2192 mp_irqs[i].dstapic
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002193 = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
2195 /*
2196 * Read the right value from the MPC table and
2197 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 apic_printk(APIC_VERBOSE, KERN_INFO
2200 "...changing IO-APIC physical APIC ID to %d ...",
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002201 mp_ioapics[apic_id].apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002203 reg_00.bits.ID = mp_ioapics[apic_id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002205 io_apic_write(apic_id, 0, reg_00.raw);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002206 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 /*
2209 * Sanity check
2210 */
2211 spin_lock_irqsave(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002212 reg_00.raw = io_apic_read(apic_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 spin_unlock_irqrestore(&ioapic_lock, flags);
Ingo Molnarc8d46cf2009-01-28 00:14:11 +01002214 if (reg_00.bits.ID != mp_ioapics[apic_id].apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 printk("could not set ID!\n");
2216 else
2217 apic_printk(APIC_VERBOSE, " ok.\n");
2218 }
2219}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002222int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002223
2224static int __init notimercheck(char *s)
2225{
2226 no_timer_check = 1;
2227 return 1;
2228}
2229__setup("no_timer_check", notimercheck);
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231/*
2232 * There is a nasty bug in some older SMP boards, their mptable lies
2233 * about the timer IRQ. We do the following to work around the situation:
2234 *
2235 * - timer IRQ defaults to IO-APIC IRQ
2236 * - if this function detects that timer IRQs are defunct, then we fall
2237 * back to ISA timer IRQs
2238 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002239static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
2241 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002242 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Zachary Amsden8542b202006-12-07 02:14:09 +01002244 if (no_timer_check)
2245 return 1;
2246
Ingo Molnar4aae0702007-12-18 18:05:58 +01002247 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 local_irq_enable();
2249 /* Let ten ticks pass... */
2250 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002251 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 /*
2254 * Expect a few ticks at least, to be sure some possible
2255 * glue logic does not lock up after one or two first
2256 * ticks in a non-ExtINT mode. Also the local APIC
2257 * might have cached one ExtINT interrupt. Finally, at
2258 * least one tick may be lost due to delays.
2259 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002260
2261 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002262 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return 0;
2265}
2266
2267/*
2268 * In the SMP+IOAPIC case it might happen that there are an unspecified
2269 * number of pending IRQ events unhandled. These cases are very rare,
2270 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2271 * better to do it this way as thus we do not have to be aware of
2272 * 'pending' interrupts in the IRQ path, except at this point.
2273 */
2274/*
2275 * Edge triggered needs to resend any interrupt
2276 * that was delayed but this is now handled in the device
2277 * independent code.
2278 */
2279
2280/*
2281 * Starting up a edge-triggered IO-APIC interrupt is
2282 * nasty - we need to make sure that we get the edge.
2283 * If it is already asserted for some reason, we need
2284 * return 1 to indicate that is was pending.
2285 *
2286 * This is not complete - we should be able to fake
2287 * an edge even if it isn't on the 8259A...
2288 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002289
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002290static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 int was_pending = 0;
2293 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002294 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295
2296 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002297 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 disable_8259A_irq(irq);
2299 if (i8259A_irq_pending(irq))
2300 was_pending = 1;
2301 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002302 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002303 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 spin_unlock_irqrestore(&ioapic_lock, flags);
2305
2306 return was_pending;
2307}
2308
Ingo Molnar54168ed2008-08-20 09:07:45 +02002309#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002310static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002312
2313 struct irq_cfg *cfg = irq_cfg(irq);
2314 unsigned long flags;
2315
2316 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnardac5f412009-01-28 15:42:24 +01002317 apic->send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002318 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002319
2320 return 1;
2321}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002322#else
2323static int ioapic_retrigger_irq(unsigned int irq)
2324{
Ingo Molnardac5f412009-01-28 15:42:24 +01002325 apic->send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002326
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002327 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002328}
2329#endif
2330
2331/*
2332 * Level and edge triggered IO-APIC interrupts need different handling,
2333 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2334 * handled with the level-triggered descriptor, but that one has slightly
2335 * more overhead. Level-triggered interrupts cannot be handled with the
2336 * edge-triggered handler, without risking IRQ storms and other ugly
2337 * races.
2338 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002339
Yinghai Lu497c9a12008-08-19 20:50:28 -07002340#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002341
2342#ifdef CONFIG_INTR_REMAP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002343
2344/*
2345 * Migrate the IO-APIC irq in the presence of intr-remapping.
2346 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002347 * For both level and edge triggered, irq migration is a simple atomic
2348 * update(of vector and cpu destination) of IRTE and flush the hardware cache.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002349 *
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002350 * For level triggered, we eliminate the io-apic RTE modification (with the
2351 * updated vector information), by using a virtual vector (io-apic pin number).
2352 * Real vector that is used for interrupting cpu will be coming from
2353 * the interrupt-remapping table entry.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002354 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002355static int
Mike Travise7986732008-12-16 17:33:52 -08002356migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002357{
2358 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002359 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002360 unsigned int dest;
Yinghai Lu3145e942008-12-05 18:58:34 -08002361 unsigned int irq;
Yinghai Lud5dedd42009-04-27 17:59:21 -07002362 int ret = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002363
Mike Travis22f65d32008-12-16 17:33:56 -08002364 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002365 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002366
Yinghai Lu3145e942008-12-05 18:58:34 -08002367 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002368 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002369 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002370
Yinghai Lu3145e942008-12-05 18:58:34 -08002371 cfg = desc->chip_data;
2372 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lud5dedd42009-04-27 17:59:21 -07002373 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002374
Ingo Molnardebccb32009-01-28 15:20:18 +01002375 dest = apic->cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002376
Ingo Molnar54168ed2008-08-20 09:07:45 +02002377 irte.vector = cfg->vector;
2378 irte.dest_id = IRTE_DEST(dest);
2379
2380 /*
2381 * Modified the IRTE and flushes the Interrupt entry cache.
2382 */
2383 modify_irte(irq, &irte);
2384
Mike Travis22f65d32008-12-16 17:33:56 -08002385 if (cfg->move_in_progress)
2386 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002387
Mike Travis7f7ace02009-01-10 21:58:08 -08002388 cpumask_copy(desc->affinity, mask);
Yinghai Lud5dedd42009-04-27 17:59:21 -07002389
2390 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002391}
2392
Ingo Molnar54168ed2008-08-20 09:07:45 +02002393/*
2394 * Migrates the IRQ destination in the process context.
2395 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07002396static int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Rusty Russell968ea6d2008-12-13 21:55:51 +10302397 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002398{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002399 return migrate_ioapic_irq_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002400}
Yinghai Lud5dedd42009-04-27 17:59:21 -07002401static int set_ir_ioapic_affinity_irq(unsigned int irq,
Rusty Russell0de26522008-12-13 21:20:26 +10302402 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002403{
2404 struct irq_desc *desc = irq_to_desc(irq);
2405
Yinghai Lud5dedd42009-04-27 17:59:21 -07002406 return set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002407}
Suresh Siddha29b61be2009-03-16 17:05:02 -07002408#else
Yinghai Lud5dedd42009-04-27 17:59:21 -07002409static inline int set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
Suresh Siddha29b61be2009-03-16 17:05:02 -07002410 const struct cpumask *mask)
2411{
Yinghai Lud5dedd42009-04-27 17:59:21 -07002412 return 0;
Suresh Siddha29b61be2009-03-16 17:05:02 -07002413}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002414#endif
2415
Yinghai Lu497c9a12008-08-19 20:50:28 -07002416asmlinkage void smp_irq_move_cleanup_interrupt(void)
2417{
2418 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002419
Yinghai Lu497c9a12008-08-19 20:50:28 -07002420 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002421 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002422 irq_enter();
2423
2424 me = smp_processor_id();
2425 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2426 unsigned int irq;
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002427 unsigned int irr;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002428 struct irq_desc *desc;
2429 struct irq_cfg *cfg;
2430 irq = __get_cpu_var(vector_irq)[vector];
2431
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002432 if (irq == -1)
2433 continue;
2434
Yinghai Lu497c9a12008-08-19 20:50:28 -07002435 desc = irq_to_desc(irq);
2436 if (!desc)
2437 continue;
2438
2439 cfg = irq_cfg(irq);
2440 spin_lock(&desc->lock);
2441 if (!cfg->move_cleanup_count)
2442 goto unlock;
2443
Mike Travis22f65d32008-12-16 17:33:56 -08002444 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002445 goto unlock;
2446
Suresh Siddha68a8ca52009-03-16 17:05:04 -07002447 irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2448 /*
2449 * Check if the vector that needs to be cleanedup is
2450 * registered at the cpu's IRR. If so, then this is not
2451 * the best time to clean it up. Lets clean it up in the
2452 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2453 * to myself.
2454 */
2455 if (irr & (1 << (vector % 32))) {
2456 apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2457 goto unlock;
2458 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002459 __get_cpu_var(vector_irq)[vector] = -1;
2460 cfg->move_cleanup_count--;
2461unlock:
2462 spin_unlock(&desc->lock);
2463 }
2464
2465 irq_exit();
2466}
2467
Yinghai Lu3145e942008-12-05 18:58:34 -08002468static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002469{
Yinghai Lu3145e942008-12-05 18:58:34 -08002470 struct irq_desc *desc = *descp;
2471 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002472 unsigned vector, me;
2473
Yinghai Lufcef5912009-04-27 17:58:23 -07002474 if (likely(!cfg->move_in_progress))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002475 return;
2476
2477 vector = ~get_irq_regs()->orig_ax;
2478 me = smp_processor_id();
Yinghai Lu10b888d2009-01-31 14:50:07 -08002479
Yinghai Lufcef5912009-04-27 17:58:23 -07002480 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Mike Travis22f65d32008-12-16 17:33:56 -08002481 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002482}
2483#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002484static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002485#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002486
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002487static void __eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
2488{
2489 int apic, pin;
2490 struct irq_pin_list *entry;
2491
2492 entry = cfg->irq_2_pin;
2493 for (;;) {
2494
2495 if (!entry)
2496 break;
2497
2498 apic = entry->apic;
2499 pin = entry->pin;
2500 io_apic_eoi(apic, pin);
2501 entry = entry->next;
2502 }
2503}
2504
2505static void
2506eoi_ioapic_irq(struct irq_desc *desc)
2507{
2508 struct irq_cfg *cfg;
2509 unsigned long flags;
2510 unsigned int irq;
2511
2512 irq = desc->irq;
2513 cfg = desc->chip_data;
2514
2515 spin_lock_irqsave(&ioapic_lock, flags);
2516 __eoi_ioapic_irq(irq, cfg);
2517 spin_unlock_irqrestore(&ioapic_lock, flags);
2518}
2519
Weidong Han746cddd2009-04-10 17:17:17 +08002520#ifdef CONFIG_X86_X2APIC
Ingo Molnar54168ed2008-08-20 09:07:45 +02002521static void ack_x2apic_level(unsigned int irq)
2522{
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002523 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002524 ack_x2APIC_irq();
Suresh Siddha0280f7c2009-03-16 17:05:01 -07002525 eoi_ioapic_irq(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002526}
2527
2528static void ack_x2apic_edge(unsigned int irq)
2529{
2530 ack_x2APIC_irq();
2531}
2532#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002533
Yinghai Lu1d025192008-08-19 20:50:34 -07002534static void ack_apic_edge(unsigned int irq)
2535{
Yinghai Lu3145e942008-12-05 18:58:34 -08002536 struct irq_desc *desc = irq_to_desc(irq);
2537
2538 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002539 move_native_irq(irq);
2540 ack_APIC_irq();
2541}
2542
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002543atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002544
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002545static void ack_apic_level(unsigned int irq)
2546{
Yinghai Lu3145e942008-12-05 18:58:34 -08002547 struct irq_desc *desc = irq_to_desc(irq);
2548
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002549#ifdef CONFIG_X86_32
2550 unsigned long v;
2551 int i;
2552#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002553 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002554 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002555
Yinghai Lu3145e942008-12-05 18:58:34 -08002556 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002557#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002558 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002559 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002560 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002561 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002562 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002563#endif
2564
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002565#ifdef CONFIG_X86_32
2566 /*
2567 * It appears there is an erratum which affects at least version 0x11
2568 * of I/O APIC (that's the 82093AA and cores integrated into various
2569 * chipsets). Under certain conditions a level-triggered interrupt is
2570 * erroneously delivered as edge-triggered one but the respective IRR
2571 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2572 * message but it will never arrive and further interrupts are blocked
2573 * from the source. The exact reason is so far unknown, but the
2574 * phenomenon was observed when two consecutive interrupt requests
2575 * from a given source get delivered to the same CPU and the source is
2576 * temporarily disabled in between.
2577 *
2578 * A workaround is to simulate an EOI message manually. We achieve it
2579 * by setting the trigger mode to edge and then to level when the edge
2580 * trigger mode gets detected in the TMR of a local APIC for a
2581 * level-triggered interrupt. We mask the source for the time of the
2582 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2583 * The idea is from Manfred Spraul. --macro
2584 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002585 cfg = desc->chip_data;
2586 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002587
2588 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2589#endif
2590
Ingo Molnar54168ed2008-08-20 09:07:45 +02002591 /*
2592 * We must acknowledge the irq before we move it or the acknowledge will
2593 * not propagate properly.
2594 */
2595 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002596
Weidong Han746cddd2009-04-10 17:17:17 +08002597 if (irq_remapped(irq))
2598 eoi_ioapic_irq(desc);
2599
Ingo Molnar54168ed2008-08-20 09:07:45 +02002600 /* Now we can move and renable the irq */
2601 if (unlikely(do_unmask_irq)) {
2602 /* Only migrate the irq if the ack has been received.
2603 *
2604 * On rare occasions the broadcast level triggered ack gets
2605 * delayed going to ioapics, and if we reprogram the
2606 * vector while Remote IRR is still set the irq will never
2607 * fire again.
2608 *
2609 * To prevent this scenario we read the Remote IRR bit
2610 * of the ioapic. This has two effects.
2611 * - On any sane system the read of the ioapic will
2612 * flush writes (and acks) going to the ioapic from
2613 * this cpu.
2614 * - We get to see if the ACK has actually been delivered.
2615 *
2616 * Based on failed experiments of reprogramming the
2617 * ioapic entry from outside of irq context starting
2618 * with masking the ioapic entry and then polling until
2619 * Remote IRR was clear before reprogramming the
2620 * ioapic I don't trust the Remote IRR bit to be
2621 * completey accurate.
2622 *
2623 * However there appears to be no other way to plug
2624 * this race, so if the Remote IRR bit is not
2625 * accurate and is causing problems then it is a hardware bug
2626 * and you can go talk to the chipset vendor about it.
2627 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002628 cfg = desc->chip_data;
2629 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002630 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002631 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002632 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002633
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002634#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002635 if (!(v & (1 << (i & 0x1f)))) {
2636 atomic_inc(&irq_mis_count);
2637 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002638 __mask_and_edge_IO_APIC_irq(cfg);
2639 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002640 spin_unlock(&ioapic_lock);
2641 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002642#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002643}
Yinghai Lu1d025192008-08-19 20:50:34 -07002644
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002645#ifdef CONFIG_INTR_REMAP
2646static void ir_ack_apic_edge(unsigned int irq)
2647{
2648#ifdef CONFIG_X86_X2APIC
2649 if (x2apic_enabled())
2650 return ack_x2apic_edge(irq);
2651#endif
2652 return ack_apic_edge(irq);
2653}
2654
2655static void ir_ack_apic_level(unsigned int irq)
2656{
2657#ifdef CONFIG_X86_X2APIC
2658 if (x2apic_enabled())
2659 return ack_x2apic_level(irq);
2660#endif
2661 return ack_apic_level(irq);
2662}
2663#endif /* CONFIG_INTR_REMAP */
2664
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 +02002678static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002679 .name = "IR-IO-APIC",
2680 .startup = startup_ioapic_irq,
2681 .mask = mask_IO_APIC_irq,
2682 .unmask = unmask_IO_APIC_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302683#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08002684 .ack = ir_ack_apic_edge,
2685 .eoi = ir_ack_apic_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
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05302689#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002690 .retrigger = ioapic_retrigger_irq,
2691};
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 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002870
2871 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 /*
2874 * get/set the timer IRQ vector:
2875 */
2876 disable_8259A_irq(0);
Ingo Molnarfe402e12009-01-28 04:32:51 +01002877 assign_irq_vector(0, cfg, apic->target_cpus());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002880 * As IRQ0 is to be enabled in the 8259A, the virtual
2881 * wire has to be disabled in the local APIC. Also
2882 * timer interrupts need to be acknowledged manually in
2883 * the 8259A for the i82489DX when using the NMI
2884 * watchdog as that APIC treats NMIs as level-triggered.
2885 * The AEOI mode will finish them in the 8259A
2886 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002888 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002890#ifdef CONFIG_X86_32
Yinghai Luf72dcca2009-02-08 16:18:03 -08002891 {
2892 unsigned int ver;
2893
2894 ver = apic_read(APIC_LVR);
2895 ver = GET_APIC_VERSION(ver);
2896 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2897 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002900 pin1 = find_isa_irq_pin(0, mp_INT);
2901 apic1 = find_isa_irq_apic(0, mp_INT);
2902 pin2 = ioapic_i8259.pin;
2903 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002905 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2906 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002907 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002909 /*
2910 * Some BIOS writers are clueless and report the ExtINTA
2911 * I/O APIC input from the cascaded 8259A as the timer
2912 * interrupt input. So just in case, if only one pin
2913 * was found above, try it both directly and through the
2914 * 8259A.
2915 */
2916 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002917 if (intr_remapping_enabled)
2918 panic("BIOS bug: timer not connected to IO-APIC");
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);
Yinghai Luf72dcca2009-02-08 16:18:03 -08002934 } else {
2935 /* for edge trigger, setup_IO_APIC_irq already
2936 * leave it unmasked.
2937 * so only need to unmask if it is level-trigger
2938 * do we really have level trigger timer?
2939 */
2940 int idx;
2941 idx = find_irq_entry(apic1, pin1, mp_INT);
2942 if (idx != -1 && irq_trigger(idx))
2943 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (timer_irq_works()) {
2946 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 setup_nmi();
2948 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002950 if (disable_timer_pin_1 > 0)
2951 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002952 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002954 if (intr_remapping_enabled)
2955 panic("timer doesn't work through Interrupt-remapped IO-APIC");
Yinghai Luf72dcca2009-02-08 16:18:03 -08002956 local_irq_disable();
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002957 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002958 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002959 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2960 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002962 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2963 "(IRQ0) through the 8259A ...\n");
2964 apic_printk(APIC_QUIET, KERN_INFO
2965 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 /*
2967 * legacy devices should be connected to IO APIC #0
2968 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002969 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002970 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002971 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002973 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002974 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002976 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002978 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
2982 /*
2983 * Cleanup, just in case ...
2984 */
Yinghai Luf72dcca2009-02-08 16:18:03 -08002985 local_irq_disable();
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002986 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002987 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002988 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002992 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2993 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002994 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002996#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002997 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003000 apic_printk(APIC_QUIET, KERN_INFO
3001 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Yinghai Lu3145e942008-12-05 18:58:34 -08003003 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003004 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 enable_8259A_irq(0);
3006
3007 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003008 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08003011 local_irq_disable();
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01003012 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003013 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003014 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003016 apic_printk(APIC_QUIET, KERN_INFO
3017 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 init_8259A(0);
3020 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01003021 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 unlock_ExtINT_logic();
3024
3025 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003026 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
Yinghai Luf72dcca2009-02-08 16:18:03 -08003029 local_irq_disable();
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003030 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003032 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003033out:
3034 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035}
3036
3037/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003038 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
3039 * to devices. However there may be an I/O APIC pin available for
3040 * this interrupt regardless. The pin may be left unconnected, but
3041 * typically it will be reused as an ExtINT cascade interrupt for
3042 * the master 8259A. In the MPS case such a pin will normally be
3043 * reported as an ExtINT interrupt in the MP table. With ACPI
3044 * there is no provision for ExtINT interrupts, and in the absence
3045 * of an override it would be treated as an ordinary ISA I/O APIC
3046 * interrupt, that is edge-triggered and unmasked by default. We
3047 * used to do this, but it caused problems on some systems because
3048 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3049 * the same ExtINT cascade interrupt to drive the local APIC of the
3050 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3051 * the I/O APIC in all cases now. No actual device should request
3052 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 */
3054#define PIC_IRQS (1 << PIC_CASCADE_IR)
3055
3056void __init setup_IO_APIC(void)
3057{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003058
Ingo Molnar54168ed2008-08-20 09:07:45 +02003059 /*
3060 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3061 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003063 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Ingo Molnar54168ed2008-08-20 09:07:45 +02003065 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003066 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003067 * Set up IO-APIC IRQ routing.
3068 */
3069#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003070 if (!acpi_ioapic)
3071 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003072#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 sync_Arb_IDs();
3074 setup_IO_APIC_irqs();
3075 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08003076 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
3079/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003080 * Called after all the initialization is done. If we didnt find any
3081 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084static int __init io_apic_bug_finalize(void)
3085{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003086 if (sis_apic_bug == -1)
3087 sis_apic_bug = 0;
3088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
3090
3091late_initcall(io_apic_bug_finalize);
3092
3093struct sysfs_ioapic_data {
3094 struct sys_device dev;
3095 struct IO_APIC_route_entry entry[0];
3096};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003097static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
Pavel Machek438510f2005-04-16 15:25:24 -07003099static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
3101 struct IO_APIC_route_entry *entry;
3102 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 data = container_of(dev, struct sysfs_ioapic_data, dev);
3106 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003107 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3108 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 return 0;
3111}
3112
3113static int ioapic_resume(struct sys_device *dev)
3114{
3115 struct IO_APIC_route_entry *entry;
3116 struct sysfs_ioapic_data *data;
3117 unsigned long flags;
3118 union IO_APIC_reg_00 reg_00;
3119 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003120
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 data = container_of(dev, struct sysfs_ioapic_data, dev);
3122 entry = data->entry;
3123
3124 spin_lock_irqsave(&ioapic_lock, flags);
3125 reg_00.raw = io_apic_read(dev->id, 0);
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05303126 if (reg_00.bits.ID != mp_ioapics[dev->id].apicid) {
3127 reg_00.bits.ID = mp_ioapics[dev->id].apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 io_apic_write(dev->id, 0, reg_00.raw);
3129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003131 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003132 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 return 0;
3135}
3136
3137static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003138 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 .suspend = ioapic_suspend,
3140 .resume = ioapic_resume,
3141};
3142
3143static int __init ioapic_init_sysfs(void)
3144{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003145 struct sys_device * dev;
3146 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 error = sysdev_class_register(&ioapic_sysdev_class);
3149 if (error)
3150 return error;
3151
Ingo Molnar54168ed2008-08-20 09:07:45 +02003152 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003153 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003155 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (!mp_ioapic_data[i]) {
3157 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3158 continue;
3159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003161 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 dev->cls = &ioapic_sysdev_class;
3163 error = sysdev_register(dev);
3164 if (error) {
3165 kfree(mp_ioapic_data[i]);
3166 mp_ioapic_data[i] = NULL;
3167 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3168 continue;
3169 }
3170 }
3171
3172 return 0;
3173}
3174
3175device_initcall(ioapic_init_sysfs);
3176
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003177static int nr_irqs_gsi = NR_IRQS_LEGACY;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003178/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003179 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003180 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003181unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003182{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003183 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003184 unsigned int irq;
3185 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003186 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003187 struct irq_cfg *cfg_new = NULL;
3188 int cpu = boot_cpu_id;
3189 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003190
3191 irq = 0;
Yinghai Luabcaa2b2009-02-08 16:18:03 -08003192 if (irq_want < nr_irqs_gsi)
3193 irq_want = nr_irqs_gsi;
3194
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003195 spin_lock_irqsave(&vector_lock, flags);
Mike Travis95949492009-01-10 22:24:06 -08003196 for (new = irq_want; new < nr_irqs; new++) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003197 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3198 if (!desc_new) {
3199 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003200 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003201 }
3202 cfg_new = desc_new->chip_data;
3203
3204 if (cfg_new->vector != 0)
3205 continue;
Ingo Molnarfe402e12009-01-28 04:32:51 +01003206 if (__assign_irq_vector(new, cfg_new, apic->target_cpus()) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003207 irq = new;
3208 break;
3209 }
3210 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003211
Yinghai Lu199751d2008-08-19 20:50:27 -07003212 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003213 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003214 /* restore it, in case dynamic_irq_init clear it */
3215 if (desc_new)
3216 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003217 }
3218 return irq;
3219}
3220
Yinghai Lu199751d2008-08-19 20:50:27 -07003221int create_irq(void)
3222{
Yinghai Lube5d5352008-12-05 18:58:33 -08003223 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003224 int irq;
3225
Yinghai Lube5d5352008-12-05 18:58:33 -08003226 irq_want = nr_irqs_gsi;
3227 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003228
3229 if (irq == 0)
3230 irq = -1;
3231
3232 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003233}
3234
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003235void destroy_irq(unsigned int irq)
3236{
3237 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003238 struct irq_cfg *cfg;
3239 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003240
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003241 /* store it, in case dynamic_irq_cleanup clear it */
3242 desc = irq_to_desc(irq);
3243 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003244 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003245 /* connect back irq_cfg */
3246 if (desc)
3247 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003248
Ingo Molnar54168ed2008-08-20 09:07:45 +02003249 free_irte(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003250 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003251 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003252 spin_unlock_irqrestore(&vector_lock, flags);
3253}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003254
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003255/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003256 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003257 */
3258#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003259static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003260{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003261 struct irq_cfg *cfg;
3262 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003263 unsigned dest;
3264
Jan Beulichf1182632009-01-14 12:27:35 +00003265 if (disable_apic)
3266 return -ENXIO;
3267
Yinghai Lu3145e942008-12-05 18:58:34 -08003268 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003269 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Yinghai Lu497c9a12008-08-19 20:50:28 -07003270 if (err)
3271 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003272
Ingo Molnardebccb32009-01-28 15:20:18 +01003273 dest = apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus());
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003274
Ingo Molnar54168ed2008-08-20 09:07:45 +02003275 if (irq_remapped(irq)) {
3276 struct irte irte;
3277 int ir_index;
3278 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003279
Ingo Molnar54168ed2008-08-20 09:07:45 +02003280 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3281 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003282
Ingo Molnar54168ed2008-08-20 09:07:45 +02003283 memset (&irte, 0, sizeof(irte));
3284
3285 irte.present = 1;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003286 irte.dst_mode = apic->irq_dest_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003287 irte.trigger_mode = 0; /* edge */
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003288 irte.dlvry_mode = apic->irq_delivery_mode;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003289 irte.vector = cfg->vector;
3290 irte.dest_id = IRTE_DEST(dest);
3291
3292 modify_irte(irq, &irte);
3293
3294 msg->address_hi = MSI_ADDR_BASE_HI;
3295 msg->data = sub_handle;
3296 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3297 MSI_ADDR_IR_SHV |
3298 MSI_ADDR_IR_INDEX1(ir_index) |
3299 MSI_ADDR_IR_INDEX2(ir_index);
Suresh Siddha29b61be2009-03-16 17:05:02 -07003300 } else {
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003301 if (x2apic_enabled())
3302 msg->address_hi = MSI_ADDR_BASE_HI |
3303 MSI_ADDR_EXT_DEST_ID(dest);
3304 else
3305 msg->address_hi = MSI_ADDR_BASE_HI;
3306
Ingo Molnar54168ed2008-08-20 09:07:45 +02003307 msg->address_lo =
3308 MSI_ADDR_BASE_LO |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003309 ((apic->irq_dest_mode == 0) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003310 MSI_ADDR_DEST_MODE_PHYSICAL:
3311 MSI_ADDR_DEST_MODE_LOGICAL) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003312 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003313 MSI_ADDR_REDIRECTION_CPU:
3314 MSI_ADDR_REDIRECTION_LOWPRI) |
3315 MSI_ADDR_DEST_ID(dest);
3316
3317 msg->data =
3318 MSI_DATA_TRIGGER_EDGE |
3319 MSI_DATA_LEVEL_ASSERT |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003320 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Ingo Molnar54168ed2008-08-20 09:07:45 +02003321 MSI_DATA_DELIVERY_FIXED:
3322 MSI_DATA_DELIVERY_LOWPRI) |
3323 MSI_DATA_VECTOR(cfg->vector);
3324 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003325 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003326}
3327
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003328#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003329static int set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003330{
Yinghai Lu3145e942008-12-05 18:58:34 -08003331 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003332 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003333 struct msi_msg msg;
3334 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003335
Mike Travis22f65d32008-12-16 17:33:56 -08003336 dest = set_desc_affinity(desc, mask);
3337 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003338 return -1;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003339
Yinghai Lu3145e942008-12-05 18:58:34 -08003340 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003341
Yinghai Lu3145e942008-12-05 18:58:34 -08003342 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003343
3344 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003345 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003346 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3347 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3348
Yinghai Lu3145e942008-12-05 18:58:34 -08003349 write_msi_msg_desc(desc, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003350
3351 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003352}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003353#ifdef CONFIG_INTR_REMAP
3354/*
3355 * Migrate the MSI irq to another cpumask. This migration is
3356 * done in the process context using interrupt-remapping hardware.
3357 */
Yinghai Lud5dedd42009-04-27 17:59:21 -07003358static int
Mike Travise7986732008-12-16 17:33:52 -08003359ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003360{
Yinghai Lu3145e942008-12-05 18:58:34 -08003361 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003362 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003363 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003364 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003365
3366 if (get_irte(irq, &irte))
Yinghai Lud5dedd42009-04-27 17:59:21 -07003367 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003368
Mike Travis22f65d32008-12-16 17:33:56 -08003369 dest = set_desc_affinity(desc, mask);
3370 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003371 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003372
Ingo Molnar54168ed2008-08-20 09:07:45 +02003373 irte.vector = cfg->vector;
3374 irte.dest_id = IRTE_DEST(dest);
3375
3376 /*
3377 * atomically update the IRTE with the new destination and vector.
3378 */
3379 modify_irte(irq, &irte);
3380
3381 /*
3382 * After this point, all the interrupts will start arriving
3383 * at the new destination. So, time to cleanup the previous
3384 * vector allocation.
3385 */
Mike Travis22f65d32008-12-16 17:33:56 -08003386 if (cfg->move_in_progress)
3387 send_cleanup_vector(cfg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003388
3389 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003390}
Yinghai Lu3145e942008-12-05 18:58:34 -08003391
Ingo Molnar54168ed2008-08-20 09:07:45 +02003392#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003393#endif /* CONFIG_SMP */
3394
3395/*
3396 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3397 * which implement the MSI or MSI-X Capability Structure.
3398 */
3399static struct irq_chip msi_chip = {
3400 .name = "PCI-MSI",
3401 .unmask = unmask_msi_irq,
3402 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003403 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003404#ifdef CONFIG_SMP
3405 .set_affinity = set_msi_irq_affinity,
3406#endif
3407 .retrigger = ioapic_retrigger_irq,
3408};
3409
Ingo Molnar54168ed2008-08-20 09:07:45 +02003410static struct irq_chip msi_ir_chip = {
3411 .name = "IR-PCI-MSI",
3412 .unmask = unmask_msi_irq,
3413 .mask = mask_msi_irq,
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303414#ifdef CONFIG_INTR_REMAP
Han, Weidongd0b03bd2009-04-03 17:15:50 +08003415 .ack = ir_ack_apic_edge,
Ingo Molnar54168ed2008-08-20 09:07:45 +02003416#ifdef CONFIG_SMP
3417 .set_affinity = ir_set_msi_irq_affinity,
3418#endif
Jaswinder Singh Rajputa1e38ca2009-03-23 02:11:25 +05303419#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02003420 .retrigger = ioapic_retrigger_irq,
3421};
3422
3423/*
3424 * Map the PCI dev to the corresponding remapping hardware unit
3425 * and allocate 'nvec' consecutive interrupt-remapping table entries
3426 * in it.
3427 */
3428static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3429{
3430 struct intel_iommu *iommu;
3431 int index;
3432
3433 iommu = map_dev_to_ir(dev);
3434 if (!iommu) {
3435 printk(KERN_ERR
3436 "Unable to map PCI %s to iommu\n", pci_name(dev));
3437 return -ENOENT;
3438 }
3439
3440 index = alloc_irte(iommu, irq, nvec);
3441 if (index < 0) {
3442 printk(KERN_ERR
3443 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003444 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003445 return -ENOSPC;
3446 }
3447 return index;
3448}
Yinghai Lu1d025192008-08-19 20:50:34 -07003449
Yinghai Lu3145e942008-12-05 18:58:34 -08003450static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003451{
3452 int ret;
3453 struct msi_msg msg;
3454
3455 ret = msi_compose_msg(dev, irq, &msg);
3456 if (ret < 0)
3457 return ret;
3458
Yinghai Lu3145e942008-12-05 18:58:34 -08003459 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003460 write_msi_msg(irq, &msg);
3461
Ingo Molnar54168ed2008-08-20 09:07:45 +02003462 if (irq_remapped(irq)) {
3463 struct irq_desc *desc = irq_to_desc(irq);
3464 /*
3465 * irq migration in process context
3466 */
3467 desc->status |= IRQ_MOVE_PCNTXT;
3468 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3469 } else
Ingo Molnar54168ed2008-08-20 09:07:45 +02003470 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003471
Yinghai Luc81bba42008-09-25 11:53:11 -07003472 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3473
Yinghai Lu1d025192008-08-19 20:50:34 -07003474 return 0;
3475}
3476
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003477int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3478{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003479 unsigned int irq;
3480 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003481 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003482 unsigned int irq_want;
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003483 struct intel_iommu *iommu = NULL;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003484 int index = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003485
Matthew Wilcox1c8d7b02009-03-17 08:54:10 -04003486 /* x86 doesn't support multiple MSI yet */
3487 if (type == PCI_CAP_ID_MSI && nvec > 1)
3488 return 1;
3489
Yinghai Lube5d5352008-12-05 18:58:33 -08003490 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003491 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003492 list_for_each_entry(msidesc, &dev->msi_list, list) {
3493 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003494 if (irq == 0)
3495 return -1;
Yinghai Luf1ee5542009-02-08 16:18:03 -08003496 irq_want = irq + 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003497 if (!intr_remapping_enabled)
3498 goto no_ir;
3499
3500 if (!sub_handle) {
3501 /*
3502 * allocate the consecutive block of IRTE's
3503 * for 'nvec'
3504 */
3505 index = msi_alloc_irte(dev, irq, nvec);
3506 if (index < 0) {
3507 ret = index;
3508 goto error;
3509 }
3510 } else {
3511 iommu = map_dev_to_ir(dev);
3512 if (!iommu) {
3513 ret = -ENOENT;
3514 goto error;
3515 }
3516 /*
3517 * setup the mapping between the irq and the IRTE
3518 * base index, the sub_handle pointing to the
3519 * appropriate interrupt remap table entry.
3520 */
3521 set_irte_irq(irq, iommu, index, sub_handle);
3522 }
3523no_ir:
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003524 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003525 if (ret < 0)
3526 goto error;
3527 sub_handle++;
3528 }
3529 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003530
3531error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003532 destroy_irq(irq);
3533 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003534}
3535
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003536void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003537{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003538 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003539}
3540
Suresh Siddha9d783ba2009-03-16 17:04:55 -07003541#if defined (CONFIG_DMAR) || defined (CONFIG_INTR_REMAP)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003542#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003543static int dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003544{
Yinghai Lu3145e942008-12-05 18:58:34 -08003545 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003546 struct irq_cfg *cfg;
3547 struct msi_msg msg;
3548 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003549
Mike Travis22f65d32008-12-16 17:33:56 -08003550 dest = set_desc_affinity(desc, mask);
3551 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003552 return -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003553
Yinghai Lu3145e942008-12-05 18:58:34 -08003554 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003555
3556 dmar_msi_read(irq, &msg);
3557
3558 msg.data &= ~MSI_DATA_VECTOR_MASK;
3559 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3560 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3561 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3562
3563 dmar_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003564
3565 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003566}
Yinghai Lu3145e942008-12-05 18:58:34 -08003567
Ingo Molnar54168ed2008-08-20 09:07:45 +02003568#endif /* CONFIG_SMP */
3569
3570struct irq_chip dmar_msi_type = {
3571 .name = "DMAR_MSI",
3572 .unmask = dmar_msi_unmask,
3573 .mask = dmar_msi_mask,
3574 .ack = ack_apic_edge,
3575#ifdef CONFIG_SMP
3576 .set_affinity = dmar_msi_set_affinity,
3577#endif
3578 .retrigger = ioapic_retrigger_irq,
3579};
3580
3581int arch_setup_dmar_msi(unsigned int irq)
3582{
3583 int ret;
3584 struct msi_msg msg;
3585
3586 ret = msi_compose_msg(NULL, irq, &msg);
3587 if (ret < 0)
3588 return ret;
3589 dmar_msi_write(irq, &msg);
3590 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3591 "edge");
3592 return 0;
3593}
3594#endif
3595
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003596#ifdef CONFIG_HPET_TIMER
3597
3598#ifdef CONFIG_SMP
Yinghai Lud5dedd42009-04-27 17:59:21 -07003599static int hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003600{
Yinghai Lu3145e942008-12-05 18:58:34 -08003601 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003602 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003603 struct msi_msg msg;
3604 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003605
Mike Travis22f65d32008-12-16 17:33:56 -08003606 dest = set_desc_affinity(desc, mask);
3607 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003608 return -1;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003609
Yinghai Lu3145e942008-12-05 18:58:34 -08003610 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003611
3612 hpet_msi_read(irq, &msg);
3613
3614 msg.data &= ~MSI_DATA_VECTOR_MASK;
3615 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3616 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3617 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3618
3619 hpet_msi_write(irq, &msg);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003620
3621 return 0;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003622}
Yinghai Lu3145e942008-12-05 18:58:34 -08003623
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003624#endif /* CONFIG_SMP */
3625
Dmitri Vorobiev1cc18522009-03-22 19:11:09 +02003626static struct irq_chip hpet_msi_type = {
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003627 .name = "HPET_MSI",
3628 .unmask = hpet_msi_unmask,
3629 .mask = hpet_msi_mask,
3630 .ack = ack_apic_edge,
3631#ifdef CONFIG_SMP
3632 .set_affinity = hpet_msi_set_affinity,
3633#endif
3634 .retrigger = ioapic_retrigger_irq,
3635};
3636
3637int arch_setup_hpet_msi(unsigned int irq)
3638{
3639 int ret;
3640 struct msi_msg msg;
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003641 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003642
3643 ret = msi_compose_msg(NULL, irq, &msg);
3644 if (ret < 0)
3645 return ret;
3646
3647 hpet_msi_write(irq, &msg);
Pallipadi, Venkatesh6ec3cfe2009-04-13 15:20:58 -07003648 desc->status |= IRQ_MOVE_PCNTXT;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003649 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3650 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003651
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003652 return 0;
3653}
3654#endif
3655
Ingo Molnar54168ed2008-08-20 09:07:45 +02003656#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003657/*
3658 * Hypertransport interrupt support
3659 */
3660#ifdef CONFIG_HT_IRQ
3661
3662#ifdef CONFIG_SMP
3663
Yinghai Lu497c9a12008-08-19 20:50:28 -07003664static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003665{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003666 struct ht_irq_msg msg;
3667 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003668
Yinghai Lu497c9a12008-08-19 20:50:28 -07003669 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003670 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003671
Yinghai Lu497c9a12008-08-19 20:50:28 -07003672 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003673 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003674
Eric W. Biedermanec683072006-11-08 17:44:57 -08003675 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003676}
3677
Yinghai Lud5dedd42009-04-27 17:59:21 -07003678static int set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003679{
Yinghai Lu3145e942008-12-05 18:58:34 -08003680 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003681 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003682 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003683
Mike Travis22f65d32008-12-16 17:33:56 -08003684 dest = set_desc_affinity(desc, mask);
3685 if (dest == BAD_APICID)
Yinghai Lud5dedd42009-04-27 17:59:21 -07003686 return -1;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003687
Yinghai Lu3145e942008-12-05 18:58:34 -08003688 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003689
Yinghai Lu497c9a12008-08-19 20:50:28 -07003690 target_ht_irq(irq, dest, cfg->vector);
Yinghai Lud5dedd42009-04-27 17:59:21 -07003691
3692 return 0;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003693}
Yinghai Lu3145e942008-12-05 18:58:34 -08003694
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003695#endif
3696
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003697static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003698 .name = "PCI-HT",
3699 .mask = mask_ht_irq,
3700 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003701 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003702#ifdef CONFIG_SMP
3703 .set_affinity = set_ht_irq_affinity,
3704#endif
3705 .retrigger = ioapic_retrigger_irq,
3706};
3707
3708int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3709{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003710 struct irq_cfg *cfg;
3711 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003712
Jan Beulichf1182632009-01-14 12:27:35 +00003713 if (disable_apic)
3714 return -ENXIO;
3715
Yinghai Lu3145e942008-12-05 18:58:34 -08003716 cfg = irq_cfg(irq);
Ingo Molnarfe402e12009-01-28 04:32:51 +01003717 err = assign_irq_vector(irq, cfg, apic->target_cpus());
Ingo Molnar54168ed2008-08-20 09:07:45 +02003718 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003719 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003720 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003721
Ingo Molnardebccb32009-01-28 15:20:18 +01003722 dest = apic->cpu_mask_to_apicid_and(cfg->domain,
3723 apic->target_cpus());
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003724
Eric W. Biedermanec683072006-11-08 17:44:57 -08003725 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003726
Eric W. Biedermanec683072006-11-08 17:44:57 -08003727 msg.address_lo =
3728 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003729 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003730 HT_IRQ_LOW_VECTOR(cfg->vector) |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003731 ((apic->irq_dest_mode == 0) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003732 HT_IRQ_LOW_DM_PHYSICAL :
3733 HT_IRQ_LOW_DM_LOGICAL) |
3734 HT_IRQ_LOW_RQEOI_EDGE |
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003735 ((apic->irq_delivery_mode != dest_LowestPrio) ?
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003736 HT_IRQ_LOW_MT_FIXED :
3737 HT_IRQ_LOW_MT_ARBITRATED) |
3738 HT_IRQ_LOW_IRQ_MASKED;
3739
Eric W. Biedermanec683072006-11-08 17:44:57 -08003740 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003741
Ingo Molnara460e742006-10-17 00:10:03 -07003742 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3743 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003744
3745 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003746 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003747 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003748}
3749#endif /* CONFIG_HT_IRQ */
3750
Nick Piggin03b48632009-01-20 04:36:04 +01003751#ifdef CONFIG_X86_UV
Dean Nelson4173a0e2008-10-02 12:18:21 -05003752/*
3753 * Re-target the irq to the specified CPU and enable the specified MMR located
3754 * on the specified blade to allow the sending of MSIs to the specified CPU.
3755 */
3756int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3757 unsigned long mmr_offset)
3758{
Mike Travis22f65d32008-12-16 17:33:56 -08003759 const struct cpumask *eligible_cpu = cpumask_of(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003760 struct irq_cfg *cfg;
3761 int mmr_pnode;
3762 unsigned long mmr_value;
3763 struct uv_IO_APIC_route_entry *entry;
3764 unsigned long flags;
3765 int err;
3766
Yinghai Lu3145e942008-12-05 18:58:34 -08003767 cfg = irq_cfg(irq);
3768
Mike Travise7986732008-12-16 17:33:52 -08003769 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003770 if (err != 0)
3771 return err;
3772
3773 spin_lock_irqsave(&vector_lock, flags);
3774 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3775 irq_name);
3776 spin_unlock_irqrestore(&vector_lock, flags);
3777
Dean Nelson4173a0e2008-10-02 12:18:21 -05003778 mmr_value = 0;
3779 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3780 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3781
3782 entry->vector = cfg->vector;
Ingo Molnar9b5bc8d2009-01-28 04:09:58 +01003783 entry->delivery_mode = apic->irq_delivery_mode;
3784 entry->dest_mode = apic->irq_dest_mode;
Dean Nelson4173a0e2008-10-02 12:18:21 -05003785 entry->polarity = 0;
3786 entry->trigger = 0;
3787 entry->mask = 0;
Ingo Molnardebccb32009-01-28 15:20:18 +01003788 entry->dest = apic->cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003789
3790 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3791 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3792
3793 return irq;
3794}
3795
3796/*
3797 * Disable the specified MMR located on the specified blade so that MSIs are
3798 * longer allowed to be sent.
3799 */
3800void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3801{
3802 unsigned long mmr_value;
3803 struct uv_IO_APIC_route_entry *entry;
3804 int mmr_pnode;
3805
3806 mmr_value = 0;
3807 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3808 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3809
3810 entry->mask = 1;
3811
3812 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3813 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3814}
3815#endif /* CONFIG_X86_64 */
3816
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003817int __init io_apic_get_redir_entries (int ioapic)
3818{
3819 union IO_APIC_reg_01 reg_01;
3820 unsigned long flags;
3821
3822 spin_lock_irqsave(&ioapic_lock, flags);
3823 reg_01.raw = io_apic_read(ioapic, 1);
3824 spin_unlock_irqrestore(&ioapic_lock, flags);
3825
3826 return reg_01.bits.entries;
3827}
3828
Yinghai Lube5d5352008-12-05 18:58:33 -08003829void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003830{
Yinghai Lube5d5352008-12-05 18:58:33 -08003831 int nr = 0;
3832
Yinghai Lucc6c5002009-02-08 16:18:03 -08003833 nr = acpi_probe_gsi();
3834 if (nr > nr_irqs_gsi) {
Yinghai Lube5d5352008-12-05 18:58:33 -08003835 nr_irqs_gsi = nr;
Yinghai Lucc6c5002009-02-08 16:18:03 -08003836 } else {
3837 /* for acpi=off or acpi is not compiled in */
3838 int idx;
3839
3840 nr = 0;
3841 for (idx = 0; idx < nr_ioapics; idx++)
3842 nr += io_apic_get_redir_entries(idx) + 1;
3843
3844 if (nr > nr_irqs_gsi)
3845 nr_irqs_gsi = nr;
3846 }
3847
3848 printk(KERN_DEBUG "nr_irqs_gsi: %d\n", nr_irqs_gsi);
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003849}
3850
Yinghai Lu4a046d12009-01-12 17:39:24 -08003851#ifdef CONFIG_SPARSE_IRQ
3852int __init arch_probe_nr_irqs(void)
3853{
3854 int nr;
3855
Yinghai Luf1ee5542009-02-08 16:18:03 -08003856 if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3857 nr_irqs = NR_VECTORS * nr_cpu_ids;
Yinghai Lu4a046d12009-01-12 17:39:24 -08003858
Yinghai Luf1ee5542009-02-08 16:18:03 -08003859 nr = nr_irqs_gsi + 8 * nr_cpu_ids;
3860#if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3861 /*
3862 * for MSI and HT dyn irq
3863 */
3864 nr += nr_irqs_gsi * 16;
3865#endif
3866 if (nr < nr_irqs)
Yinghai Lu4a046d12009-01-12 17:39:24 -08003867 nr_irqs = nr;
3868
3869 return 0;
3870}
3871#endif
3872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003874 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 -------------------------------------------------------------------------- */
3876
Len Brown888ba6c2005-08-24 12:07:20 -04003877#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Ingo Molnar54168ed2008-08-20 09:07:45 +02003879#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003880int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
3882 union IO_APIC_reg_00 reg_00;
3883 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3884 physid_mask_t tmp;
3885 unsigned long flags;
3886 int i = 0;
3887
3888 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003889 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3890 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003892 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3894 * advantage of new APIC bus architecture.
3895 */
3896
3897 if (physids_empty(apic_id_map))
Ingo Molnard190cb82009-01-28 06:50:47 +01003898 apic_id_map = apic->ioapic_phys_id_map(phys_cpu_present_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
3900 spin_lock_irqsave(&ioapic_lock, flags);
3901 reg_00.raw = io_apic_read(ioapic, 0);
3902 spin_unlock_irqrestore(&ioapic_lock, flags);
3903
3904 if (apic_id >= get_physical_broadcast()) {
3905 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3906 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3907 apic_id = reg_00.bits.ID;
3908 }
3909
3910 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003911 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 * 'stuck on smp_invalidate_needed IPI wait' messages.
3913 */
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003914 if (apic->check_apicid_used(apic_id_map, apic_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
3916 for (i = 0; i < get_physical_broadcast(); i++) {
Ingo Molnard1d7cae2009-01-28 05:41:42 +01003917 if (!apic->check_apicid_used(apic_id_map, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 break;
3919 }
3920
3921 if (i == get_physical_broadcast())
3922 panic("Max apic_id exceeded!\n");
3923
3924 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3925 "trying %d\n", ioapic, apic_id, i);
3926
3927 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Ingo Molnar80587142009-01-28 06:50:47 +01003930 tmp = apic->apicid_to_cpu_present(apic_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 physids_or(apic_id_map, apic_id_map, tmp);
3932
3933 if (reg_00.bits.ID != apic_id) {
3934 reg_00.bits.ID = apic_id;
3935
3936 spin_lock_irqsave(&ioapic_lock, flags);
3937 io_apic_write(ioapic, 0, reg_00.raw);
3938 reg_00.raw = io_apic_read(ioapic, 0);
3939 spin_unlock_irqrestore(&ioapic_lock, flags);
3940
3941 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003942 if (reg_00.bits.ID != apic_id) {
3943 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3944 return -1;
3945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 }
3947
3948 apic_printk(APIC_VERBOSE, KERN_INFO
3949 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3950
3951 return apic_id;
3952}
3953
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003954int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
3956 union IO_APIC_reg_01 reg_01;
3957 unsigned long flags;
3958
3959 spin_lock_irqsave(&ioapic_lock, flags);
3960 reg_01.raw = io_apic_read(ioapic, 1);
3961 spin_unlock_irqrestore(&ioapic_lock, flags);
3962
3963 return reg_01.bits.version;
3964}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003965#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Ingo Molnar54168ed2008-08-20 09:07:45 +02003967int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003969 struct irq_desc *desc;
3970 struct irq_cfg *cfg;
3971 int cpu = boot_cpu_id;
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003974 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 ioapic);
3976 return -EINVAL;
3977 }
3978
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003979 desc = irq_to_desc_alloc_cpu(irq, cpu);
3980 if (!desc) {
3981 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3982 return 0;
3983 }
3984
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 * IRQs < 16 are already in the irq_2_pin[] map
3987 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003988 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003989 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003990 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Yinghai Lu3145e942008-12-05 18:58:34 -08003993 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 return 0;
3996}
3997
Ingo Molnar54168ed2008-08-20 09:07:45 +02003998
Shaohua Li61fd47e2007-11-17 01:05:28 -05003999int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
4000{
4001 int i;
4002
4003 if (skip_ioapic_setup)
4004 return -1;
4005
4006 for (i = 0; i < mp_irq_entries; i++)
Jaswinder Singh Rajputc2c21742009-01-12 17:47:22 +05304007 if (mp_irqs[i].irqtype == mp_INT &&
4008 mp_irqs[i].srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05004009 break;
4010 if (i >= mp_irq_entries)
4011 return -1;
4012
4013 *trigger = irq_trigger(i);
4014 *polarity = irq_polarity(i);
4015 return 0;
4016}
4017
Len Brown888ba6c2005-08-24 12:07:20 -04004018#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02004019
Yinghai Lu497c9a12008-08-19 20:50:28 -07004020/*
4021 * This function currently is only a helper for the i386 smp boot process where
4022 * we need to reprogram the ioredtbls to cater for the cpus which have come online
Ingo Molnarfe402e12009-01-28 04:32:51 +01004023 * so mask in all cases should simply be apic->target_cpus()
Yinghai Lu497c9a12008-08-19 20:50:28 -07004024 */
4025#ifdef CONFIG_SMP
4026void __init setup_ioapic_dest(void)
4027{
4028 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004029 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004030 struct irq_cfg *cfg;
Mike Travis22f65d32008-12-16 17:33:56 -08004031 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004032
4033 if (skip_ioapic_setup == 1)
4034 return;
4035
4036 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
4037 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4038 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4039 if (irq_entry == -1)
4040 continue;
4041 irq = pin_2_irq(irq_entry, ioapic, pin);
4042
4043 /* setup_IO_APIC_irqs could fail to get vector for some device
4044 * when you have too many devices, because at that time only boot
4045 * cpu is online.
4046 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08004047 desc = irq_to_desc(irq);
4048 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004049 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08004050 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07004051 irq_trigger(irq_entry),
4052 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004053 continue;
4054
4055 }
4056
4057 /*
4058 * Honour affinities which have been set in early boot
4059 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004060 if (desc->status &
4061 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
Mike Travis7f7ace02009-01-10 21:58:08 -08004062 mask = desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004063 else
Ingo Molnarfe402e12009-01-28 04:32:51 +01004064 mask = apic->target_cpus();
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004065
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004066 if (intr_remapping_enabled)
Yinghai Lu3145e942008-12-05 18:58:34 -08004067 set_ir_ioapic_affinity_irq_desc(desc, mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004068 else
Yinghai Lu3145e942008-12-05 18:58:34 -08004069 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07004070 }
4071
4072 }
4073}
4074#endif
4075
Ingo Molnar54168ed2008-08-20 09:07:45 +02004076#define IOAPIC_RESOURCE_NAME_SIZE 11
4077
4078static struct resource *ioapic_resources;
4079
4080static struct resource * __init ioapic_setup_resources(void)
4081{
4082 unsigned long n;
4083 struct resource *res;
4084 char *mem;
4085 int i;
4086
4087 if (nr_ioapics <= 0)
4088 return NULL;
4089
4090 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4091 n *= nr_ioapics;
4092
4093 mem = alloc_bootmem(n);
4094 res = (void *)mem;
4095
4096 if (mem != NULL) {
4097 mem += sizeof(struct resource) * nr_ioapics;
4098
4099 for (i = 0; i < nr_ioapics; i++) {
4100 res[i].name = mem;
4101 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4102 sprintf(mem, "IOAPIC %u", i);
4103 mem += IOAPIC_RESOURCE_NAME_SIZE;
4104 }
4105 }
4106
4107 ioapic_resources = res;
4108
4109 return res;
4110}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004111
Yinghai Luf3294a32008-06-27 01:41:56 -07004112void __init ioapic_init_mappings(void)
4113{
4114 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004115 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004116 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004117
Ingo Molnar54168ed2008-08-20 09:07:45 +02004118 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07004119 for (i = 0; i < nr_ioapics; i++) {
4120 if (smp_found_config) {
Jaswinder Singh Rajputb5ba7e62009-01-12 17:46:17 +05304121 ioapic_phys = mp_ioapics[i].apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004122#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004123 if (!ioapic_phys) {
4124 printk(KERN_ERR
4125 "WARNING: bogus zero IO-APIC "
4126 "address found in MPTABLE, "
4127 "disabling IO/APIC support!\n");
4128 smp_found_config = 0;
4129 skip_ioapic_setup = 1;
4130 goto fake_ioapic_page;
4131 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004132#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004133 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004134#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004135fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004136#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004137 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004138 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004139 ioapic_phys = __pa(ioapic_phys);
4140 }
4141 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004142 apic_printk(APIC_VERBOSE,
4143 "mapped IOAPIC to %08lx (%08lx)\n",
4144 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004145 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004146
Ingo Molnar54168ed2008-08-20 09:07:45 +02004147 if (ioapic_res != NULL) {
4148 ioapic_res->start = ioapic_phys;
4149 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4150 ioapic_res++;
4151 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004152 }
4153}
4154
Ingo Molnar54168ed2008-08-20 09:07:45 +02004155static int __init ioapic_insert_resources(void)
4156{
4157 int i;
4158 struct resource *r = ioapic_resources;
4159
4160 if (!r) {
Bartlomiej Zolnierkiewicz04c93ce2009-03-20 21:02:55 +01004161 if (nr_ioapics > 0) {
4162 printk(KERN_ERR
4163 "IO APIC resources couldn't be allocated.\n");
4164 return -1;
4165 }
4166 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004167 }
4168
4169 for (i = 0; i < nr_ioapics; i++) {
4170 insert_resource(&iomem_resource, r);
4171 r++;
4172 }
4173
4174 return 0;
4175}
4176
4177/* Insert the IO APIC resources after PCI initialization has occured to handle
4178 * IO APICS that are mapped in on a BAR in PCI space. */
4179late_initcall(ioapic_insert_resources);