blob: 4e44fd1f466ef122d53db3b1df34d04b651d6a31 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070028#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020038#include <linux/jiffies.h> /* time_after() */
Yinghai Lud4057bd2008-08-19 20:50:38 -070039#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070044
Yinghai Lud4057bd2008-08-19 20:50:38 -070045#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/io.h>
47#include <asm/smp.h>
48#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070049#include <asm/proto.h>
50#include <asm/acpi.h>
51#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070053#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020054#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070055#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070056#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070057#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070058#include <asm/irq_remapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Yinghai Lu497c9a12008-08-19 20:50:28 -070060#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020062#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010064#define __apicdebuginit(type) static type __init
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020067 * Is the SiS APIC rmw bug present ?
68 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 */
70int sis_apic_bug = -1;
71
Yinghai Luefa25592008-08-19 20:50:36 -070072static DEFINE_SPINLOCK(ioapic_lock);
73static DEFINE_SPINLOCK(vector_lock);
74
Yinghai Luefa25592008-08-19 20:50:36 -070075/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 * # of IRQ routing registers
77 */
78int nr_ioapic_registers[MAX_IO_APICS];
79
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040080/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040081struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040082int nr_ioapics;
83
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040084/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040085struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040086
87/* # of MP IRQ source entries */
88int mp_irq_entries;
89
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040090#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
91int mp_bus_id_to_type[MAX_MP_BUSSES];
92#endif
93
94DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
95
Yinghai Luefa25592008-08-19 20:50:36 -070096int skip_ioapic_setup;
97
Ingo Molnar54168ed2008-08-20 09:07:45 +020098static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -070099{
100 /* disable IO-APIC */
101 disable_ioapic_setup();
102 return 0;
103}
104early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200105
Yinghai Luda51a822008-08-19 20:50:25 -0700106struct irq_cfg;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700107struct irq_pin_list;
Yinghai Lua1420f32008-08-19 20:50:24 -0700108struct irq_cfg {
Yinghai Luda51a822008-08-19 20:50:25 -0700109 unsigned int irq;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700110#ifdef CONFIG_HAVE_SPARSE_IRQ
Yinghai Luda51a822008-08-19 20:50:25 -0700111 struct irq_cfg *next;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700112#endif
Yinghai Lu0f978f42008-08-19 20:50:26 -0700113 struct irq_pin_list *irq_2_pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700114 cpumask_t domain;
115 cpumask_t old_domain;
116 unsigned move_cleanup_count;
Yinghai Lua1420f32008-08-19 20:50:24 -0700117 u8 vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700118 u8 move_in_progress : 1;
Yinghai Lua1420f32008-08-19 20:50:24 -0700119};
120
Yinghai Lua1420f32008-08-19 20:50:24 -0700121/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
122static struct irq_cfg irq_cfg_legacy[] __initdata = {
Yinghai Lu497c9a12008-08-19 20:50:28 -0700123 [0] = { .irq = 0, .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
124 [1] = { .irq = 1, .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
125 [2] = { .irq = 2, .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
126 [3] = { .irq = 3, .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
127 [4] = { .irq = 4, .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
128 [5] = { .irq = 5, .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
129 [6] = { .irq = 6, .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
130 [7] = { .irq = 7, .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
131 [8] = { .irq = 8, .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
132 [9] = { .irq = 9, .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
133 [10] = { .irq = 10, .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
134 [11] = { .irq = 11, .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
135 [12] = { .irq = 12, .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
136 [13] = { .irq = 13, .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
137 [14] = { .irq = 14, .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
138 [15] = { .irq = 15, .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
Yinghai Lua1420f32008-08-19 20:50:24 -0700139};
140
Yinghai Luda51a822008-08-19 20:50:25 -0700141static struct irq_cfg irq_cfg_init = { .irq = -1U, };
Yinghai Lua1420f32008-08-19 20:50:24 -0700142
Yinghai Luda51a822008-08-19 20:50:25 -0700143static void init_one_irq_cfg(struct irq_cfg *cfg)
144{
145 memcpy(cfg, &irq_cfg_init, sizeof(struct irq_cfg));
Yinghai Lua1420f32008-08-19 20:50:24 -0700146}
147
148static struct irq_cfg *irq_cfgx;
Yinghai Lue89eb432008-08-20 20:46:25 -0700149
150/*
151 * Protect the irq_cfgx_free freelist:
152 */
153static DEFINE_SPINLOCK(irq_cfg_lock);
154
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700155#ifdef CONFIG_HAVE_SPARSE_IRQ
Yinghai Luda51a822008-08-19 20:50:25 -0700156static struct irq_cfg *irq_cfgx_free;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700157#endif
Yinghai Luda51a822008-08-19 20:50:25 -0700158static void __init init_work(void *data)
159{
160 struct dyn_array *da = data;
161 struct irq_cfg *cfg;
162 int legacy_count;
163 int i;
164
165 cfg = *da->name;
166
167 memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
168
169 legacy_count = sizeof(irq_cfg_legacy)/sizeof(irq_cfg_legacy[0]);
170 for (i = legacy_count; i < *da->nr; i++)
171 init_one_irq_cfg(&cfg[i]);
172
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700173#ifdef CONFIG_HAVE_SPARSE_IRQ
Yinghai Luda51a822008-08-19 20:50:25 -0700174 for (i = 1; i < *da->nr; i++)
175 cfg[i-1].next = &cfg[i];
176
177 irq_cfgx_free = &irq_cfgx[legacy_count];
178 irq_cfgx[legacy_count - 1].next = NULL;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700179#endif
Yinghai Luda51a822008-08-19 20:50:25 -0700180}
181
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700182#ifdef CONFIG_HAVE_SPARSE_IRQ
183/* need to be biger than size of irq_cfg_legacy */
184static int nr_irq_cfg = 32;
185
186static int __init parse_nr_irq_cfg(char *arg)
187{
188 if (arg) {
189 nr_irq_cfg = simple_strtoul(arg, NULL, 0);
190 if (nr_irq_cfg < 32)
191 nr_irq_cfg = 32;
192 }
193 return 0;
194}
195
196early_param("nr_irq_cfg", parse_nr_irq_cfg);
197
198#define for_each_irq_cfg(irqX, cfg) \
199 for (cfg = irq_cfgx, irqX = cfg->irq; cfg; cfg = cfg->next, irqX = cfg ? cfg->irq : -1U)
200
Yinghai Luda51a822008-08-19 20:50:25 -0700201
202DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irq_cfg, PAGE_SIZE, init_work);
Yinghai Lua1420f32008-08-19 20:50:24 -0700203
204static struct irq_cfg *irq_cfg(unsigned int irq)
205{
Yinghai Luda51a822008-08-19 20:50:25 -0700206 struct irq_cfg *cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700207
Yinghai Luda51a822008-08-19 20:50:25 -0700208 cfg = irq_cfgx;
209 while (cfg) {
210 if (cfg->irq == irq)
211 return cfg;
212
213 cfg = cfg->next;
214 }
215
216 return NULL;
217}
218
219static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
220{
221 struct irq_cfg *cfg, *cfg_pri;
Yinghai Lue89eb432008-08-20 20:46:25 -0700222 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -0700223 int count = 0;
Yinghai Lue89eb432008-08-20 20:46:25 -0700224 int i;
Yinghai Luda51a822008-08-19 20:50:25 -0700225
226 cfg_pri = cfg = irq_cfgx;
227 while (cfg) {
228 if (cfg->irq == irq)
229 return cfg;
230
231 cfg_pri = cfg;
232 cfg = cfg->next;
233 count++;
234 }
235
Yinghai Lue89eb432008-08-20 20:46:25 -0700236 spin_lock_irqsave(&irq_cfg_lock, flags);
Yinghai Luda51a822008-08-19 20:50:25 -0700237 if (!irq_cfgx_free) {
238 unsigned long phys;
239 unsigned long total_bytes;
240 /*
241 * we run out of pre-allocate ones, allocate more
242 */
243 printk(KERN_DEBUG "try to get more irq_cfg %d\n", nr_irq_cfg);
244
245 total_bytes = sizeof(struct irq_cfg) * nr_irq_cfg;
246 if (after_bootmem)
247 cfg = kzalloc(total_bytes, GFP_ATOMIC);
248 else
249 cfg = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
250
251 if (!cfg)
252 panic("please boot with nr_irq_cfg= %d\n", count * 2);
253
254 phys = __pa(cfg);
255 printk(KERN_DEBUG "irq_irq ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
256
257 for (i = 0; i < nr_irq_cfg; i++)
258 init_one_irq_cfg(&cfg[i]);
259
260 for (i = 1; i < nr_irq_cfg; i++)
261 cfg[i-1].next = &cfg[i];
262
263 irq_cfgx_free = cfg;
264 }
265
266 cfg = irq_cfgx_free;
267 irq_cfgx_free = irq_cfgx_free->next;
268 cfg->next = NULL;
269 if (cfg_pri)
270 cfg_pri->next = cfg;
271 else
272 irq_cfgx = cfg;
273 cfg->irq = irq;
Yinghai Lue89eb432008-08-20 20:46:25 -0700274
275 spin_unlock_irqrestore(&irq_cfg_lock, flags);
276
Yinghai Luda51a822008-08-19 20:50:25 -0700277 printk(KERN_DEBUG "found new irq_cfg for irq %d\n", cfg->irq);
Yinghai Luda51a822008-08-19 20:50:25 -0700278#ifdef CONFIG_HAVE_SPARSE_IRQ_DEBUG
279 {
280 /* dump the results */
281 struct irq_cfg *cfg;
282 unsigned long phys;
283 unsigned long bytes = sizeof(struct irq_cfg);
284
285 printk(KERN_DEBUG "=========================== %d\n", irq);
286 printk(KERN_DEBUG "irq_cfg dump after get that for %d\n", irq);
287 for_each_irq_cfg(cfg) {
288 phys = __pa(cfg);
289 printk(KERN_DEBUG "irq_cfg %d ==> [%#lx - %#lx]\n", cfg->irq, phys, phys + bytes);
290 }
291 printk(KERN_DEBUG "===========================\n");
292 }
293#endif
294 return cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700295}
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700296#else
Yinghai Lua1420f32008-08-19 20:50:24 -0700297
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700298#define for_each_irq_cfg(irq, cfg) \
299 for (irq = 0, cfg = &irq_cfgx[irq]; irq < nr_irqs; irq++, cfg = &irq_cfgx[irq])
300
301DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irqs, PAGE_SIZE, init_work);
302
303struct irq_cfg *irq_cfg(unsigned int irq)
304{
305 if (irq < nr_irqs)
306 return &irq_cfgx[irq];
307
308 return NULL;
309}
310struct irq_cfg *irq_cfg_alloc(unsigned int irq)
311{
312 return irq_cfg(irq);
313}
314
315#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/*
317 * This is performance-critical, we want to do it O(1)
318 *
319 * the indexing order of this array favors 1:1 mappings
320 * between pins and IRQs.
321 */
322
Yinghai Lu0f978f42008-08-19 20:50:26 -0700323struct irq_pin_list {
324 int apic, pin;
325 struct irq_pin_list *next;
326};
Yinghai Lu301e6192008-08-19 20:50:02 -0700327
Yinghai Lu0f978f42008-08-19 20:50:26 -0700328static struct irq_pin_list *irq_2_pin_head;
329/* fill one page ? */
330static int nr_irq_2_pin = 0x100;
331static struct irq_pin_list *irq_2_pin_ptr;
332static void __init irq_2_pin_init_work(void *data)
333{
334 struct dyn_array *da = data;
335 struct irq_pin_list *pin;
336 int i;
337
338 pin = *da->name;
339
340 for (i = 1; i < *da->nr; i++)
341 pin[i-1].next = &pin[i];
342
343 irq_2_pin_ptr = &pin[0];
344}
345DEFINE_DYN_ARRAY(irq_2_pin_head, sizeof(struct irq_pin_list), nr_irq_2_pin, PAGE_SIZE, irq_2_pin_init_work);
346
347static struct irq_pin_list *get_one_free_irq_2_pin(void)
348{
349 struct irq_pin_list *pin;
350 int i;
351
352 pin = irq_2_pin_ptr;
353
354 if (pin) {
355 irq_2_pin_ptr = pin->next;
356 pin->next = NULL;
357 return pin;
358 }
359
360 /*
361 * we run out of pre-allocate ones, allocate more
362 */
363 printk(KERN_DEBUG "try to get more irq_2_pin %d\n", nr_irq_2_pin);
364
365 if (after_bootmem)
366 pin = kzalloc(sizeof(struct irq_pin_list)*nr_irq_2_pin,
367 GFP_ATOMIC);
368 else
369 pin = __alloc_bootmem_nopanic(sizeof(struct irq_pin_list) *
370 nr_irq_2_pin, PAGE_SIZE, 0);
371
372 if (!pin)
373 panic("can not get more irq_2_pin\n");
374
375 for (i = 1; i < nr_irq_2_pin; i++)
376 pin[i-1].next = &pin[i];
377
378 irq_2_pin_ptr = pin->next;
379 pin->next = NULL;
380
381 return pin;
382}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds130fe052006-11-01 09:11:00 -0800384struct io_apic {
385 unsigned int index;
386 unsigned int unused[3];
387 unsigned int data;
388};
389
390static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
391{
392 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400393 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800394}
395
396static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
397{
398 struct io_apic __iomem *io_apic = io_apic_base(apic);
399 writel(reg, &io_apic->index);
400 return readl(&io_apic->data);
401}
402
403static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
404{
405 struct io_apic __iomem *io_apic = io_apic_base(apic);
406 writel(reg, &io_apic->index);
407 writel(value, &io_apic->data);
408}
409
410/*
411 * Re-write a value: to be used for read-modify-write
412 * cycles where the read already set up the index register.
413 *
414 * Older SiS APIC requires we rewrite the index register
415 */
416static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
417{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200418 struct io_apic __iomem *io_apic = io_apic_base(apic);
419 if (sis_apic_bug)
420 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800421 writel(value, &io_apic->data);
422}
423
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700424static bool io_apic_level_ack_pending(unsigned int irq)
425{
426 struct irq_pin_list *entry;
427 unsigned long flags;
428 struct irq_cfg *cfg = irq_cfg(irq);
429
430 spin_lock_irqsave(&ioapic_lock, flags);
431 entry = cfg->irq_2_pin;
432 for (;;) {
433 unsigned int reg;
434 int pin;
435
436 if (!entry)
437 break;
438 pin = entry->pin;
439 reg = io_apic_read(entry->apic, 0x10 + pin*2);
440 /* Is the remote IRR bit set? */
441 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
442 spin_unlock_irqrestore(&ioapic_lock, flags);
443 return true;
444 }
445 if (!entry->next)
446 break;
447 entry = entry->next;
448 }
449 spin_unlock_irqrestore(&ioapic_lock, flags);
450
451 return false;
452}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700453
Andi Kleencf4c6a22006-09-26 10:52:30 +0200454union entry_union {
455 struct { u32 w1, w2; };
456 struct IO_APIC_route_entry entry;
457};
458
459static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
460{
461 union entry_union eu;
462 unsigned long flags;
463 spin_lock_irqsave(&ioapic_lock, flags);
464 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
465 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
466 spin_unlock_irqrestore(&ioapic_lock, flags);
467 return eu.entry;
468}
469
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800470/*
471 * When we write a new IO APIC routing entry, we need to write the high
472 * word first! If the mask bit in the low word is clear, we will enable
473 * the interrupt, and we need to make sure the entry is fully populated
474 * before that happens.
475 */
Andi Kleend15512f2006-12-07 02:14:07 +0100476static void
477__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
478{
479 union entry_union eu;
480 eu.entry = e;
481 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
482 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
483}
484
Andi Kleencf4c6a22006-09-26 10:52:30 +0200485static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
486{
487 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200488 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100489 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800490 spin_unlock_irqrestore(&ioapic_lock, flags);
491}
492
493/*
494 * When we mask an IO APIC routing entry, we need to write the low
495 * word first, in order to set the mask bit before we change the
496 * high bits!
497 */
498static void ioapic_mask_entry(int apic, int pin)
499{
500 unsigned long flags;
501 union entry_union eu = { .entry.mask = 1 };
502
503 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200504 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
505 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
506 spin_unlock_irqrestore(&ioapic_lock, flags);
507}
508
Yinghai Lu497c9a12008-08-19 20:50:28 -0700509#ifdef CONFIG_SMP
510static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
511{
512 int apic, pin;
513 struct irq_cfg *cfg;
514 struct irq_pin_list *entry;
515
516 cfg = irq_cfg(irq);
517 entry = cfg->irq_2_pin;
518 for (;;) {
519 unsigned int reg;
520
521 if (!entry)
522 break;
523
524 apic = entry->apic;
525 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200526#ifdef CONFIG_INTR_REMAP
527 /*
528 * With interrupt-remapping, destination information comes
529 * from interrupt-remapping table entry.
530 */
531 if (!irq_remapped(irq))
532 io_apic_write(apic, 0x11 + pin*2, dest);
533#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700534 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200535#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700536 reg = io_apic_read(apic, 0x10 + pin*2);
537 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
538 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200539 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700540 if (!entry->next)
541 break;
542 entry = entry->next;
543 }
544}
Yinghai Luefa25592008-08-19 20:50:36 -0700545
546static int assign_irq_vector(int irq, cpumask_t mask);
547
Yinghai Lu497c9a12008-08-19 20:50:28 -0700548static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
549{
550 struct irq_cfg *cfg;
551 unsigned long flags;
552 unsigned int dest;
553 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200554 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700555
Yinghai Lu497c9a12008-08-19 20:50:28 -0700556 cpus_and(tmp, mask, cpu_online_map);
557 if (cpus_empty(tmp))
558 return;
559
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700560 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700561 if (assign_irq_vector(irq, mask))
562 return;
563
564 cpus_and(tmp, cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700565 dest = cpu_mask_to_apicid(tmp);
566 /*
567 * Only the high 8 bits are valid.
568 */
569 dest = SET_APIC_LOGICAL_ID(dest);
570
Ingo Molnar54168ed2008-08-20 09:07:45 +0200571 desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700572 spin_lock_irqsave(&ioapic_lock, flags);
573 __target_IO_APIC_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200574 desc->affinity = mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700575 spin_unlock_irqrestore(&ioapic_lock, flags);
576}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700577#endif /* CONFIG_SMP */
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579/*
580 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
581 * shared ISA-space IRQs, so we have to support them. We are super
582 * fast in the common case, and fast for shared ISA-space IRQs.
583 */
584static void add_pin_to_irq(unsigned int irq, int apic, int pin)
585{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700586 struct irq_cfg *cfg;
587 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Yinghai Lu0f978f42008-08-19 20:50:26 -0700589 /* first time to refer irq_cfg, so with new */
590 cfg = irq_cfg_alloc(irq);
591 entry = cfg->irq_2_pin;
592 if (!entry) {
593 entry = get_one_free_irq_2_pin();
594 cfg->irq_2_pin = entry;
595 entry->apic = apic;
596 entry->pin = pin;
597 printk(KERN_DEBUG " 0 add_pin_to_irq: irq %d --> apic %d pin %d\n", irq, apic, pin);
598 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700600
601 while (entry->next) {
602 /* not again, please */
603 if (entry->apic == apic && entry->pin == pin)
604 return;
605
606 entry = entry->next;
607 }
608
609 entry->next = get_one_free_irq_2_pin();
610 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 entry->apic = apic;
612 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700613 printk(KERN_DEBUG " x add_pin_to_irq: irq %d --> apic %d pin %d\n", irq, apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
617 * Reroute an IRQ to a different pin.
618 */
619static void __init replace_pin_at_irq(unsigned int irq,
620 int oldapic, int oldpin,
621 int newapic, int newpin)
622{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700623 struct irq_cfg *cfg = irq_cfg(irq);
624 struct irq_pin_list *entry = cfg->irq_2_pin;
625 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Yinghai Lu0f978f42008-08-19 20:50:26 -0700627 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (entry->apic == oldapic && entry->pin == oldpin) {
629 entry->apic = newapic;
630 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700631 replaced = 1;
632 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700634 }
635 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700637
638 /* why? call replace before add? */
639 if (!replaced)
640 add_pin_to_irq(irq, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Yinghai Lu4e738e22008-08-19 20:50:47 -0700643#define __DO_ACTION(R, ACTION_ENABLE, ACTION_DISABLE, FINAL) \
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700644 \
645{ \
646 int pin; \
647 struct irq_cfg *cfg; \
648 struct irq_pin_list *entry; \
649 \
650 cfg = irq_cfg(irq); \
651 entry = cfg->irq_2_pin; \
652 for (;;) { \
653 unsigned int reg; \
654 if (!entry) \
655 break; \
656 pin = entry->pin; \
657 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
Yinghai Lu4e738e22008-08-19 20:50:47 -0700658 reg ACTION_DISABLE; \
659 reg ACTION_ENABLE; \
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700660 io_apic_modify(entry->apic, 0x10 + R + pin*2, reg); \
661 FINAL; \
662 if (!entry->next) \
663 break; \
664 entry = entry->next; \
665 } \
666}
667
Yinghai Lu4e738e22008-08-19 20:50:47 -0700668#define DO_ACTION(name,R, ACTION_ENABLE, ACTION_DISABLE, FINAL) \
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700669 \
670 static void name##_IO_APIC_irq (unsigned int irq) \
Yinghai Lu4e738e22008-08-19 20:50:47 -0700671 __DO_ACTION(R, ACTION_ENABLE, ACTION_DISABLE, FINAL)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700672
673/* mask = 0 */
Yinghai Lu4e738e22008-08-19 20:50:47 -0700674DO_ACTION(__unmask, 0, |= 0, &= ~IO_APIC_REDIR_MASKED, )
675
676#ifdef CONFIG_X86_64
677/*
678 * Synchronize the IO-APIC and the CPU by doing
679 * a dummy read from the IO-APIC
680 */
681static inline void io_apic_sync(unsigned int apic)
682{
683 struct io_apic __iomem *io_apic = io_apic_base(apic);
684 readl(&io_apic->data);
685}
686
687/* mask = 1 */
688DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, &= ~0, io_apic_sync(entry->apic))
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700689
690#else
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/* mask = 1 */
Yinghai Lu4e738e22008-08-19 20:50:47 -0700693DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, &= ~0, )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695/* mask = 1, trigger = 0 */
Yinghai Lu4e738e22008-08-19 20:50:47 -0700696DO_ACTION(__mask_and_edge, 0, |= IO_APIC_REDIR_MASKED, &= ~IO_APIC_REDIR_LEVEL_TRIGGER, )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698/* mask = 0, trigger = 1 */
Yinghai Lu4e738e22008-08-19 20:50:47 -0700699DO_ACTION(__unmask_and_level, 0, |= IO_APIC_REDIR_LEVEL_TRIGGER, &= ~IO_APIC_REDIR_MASKED, )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700701#endif
702
Ingo Molnar54168ed2008-08-20 09:07:45 +0200703static void mask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 unsigned long flags;
706
707 spin_lock_irqsave(&ioapic_lock, flags);
708 __mask_IO_APIC_irq(irq);
709 spin_unlock_irqrestore(&ioapic_lock, flags);
710}
711
Ingo Molnar54168ed2008-08-20 09:07:45 +0200712static void unmask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 unsigned long flags;
715
716 spin_lock_irqsave(&ioapic_lock, flags);
717 __unmask_IO_APIC_irq(irq);
718 spin_unlock_irqrestore(&ioapic_lock, flags);
719}
720
721static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
722{
723 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200726 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (entry.delivery_mode == dest_SMI)
728 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /*
730 * Disable it in the IO-APIC irq-routing table:
731 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800732 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Ingo Molnar54168ed2008-08-20 09:07:45 +0200735static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 int apic, pin;
738
739 for (apic = 0; apic < nr_ioapics; apic++)
740 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
741 clear_IO_APIC_pin(apic, pin);
742}
743
Ingo Molnar54168ed2008-08-20 09:07:45 +0200744#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100745void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
747 unsigned int cfg;
748
749 /*
750 * Wait for idle.
751 */
752 apic_wait_icr_idle();
753 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
754 /*
755 * Send the IPI. The write to APIC_ICR fires this off.
756 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100757 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200759#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Ingo Molnar54168ed2008-08-20 09:07:45 +0200761#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/*
763 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
764 * specific CPU-side IRQs.
765 */
766
767#define MAX_PIRQS 8
768static int pirq_entries [MAX_PIRQS];
769static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771static int __init ioapic_pirq_setup(char *str)
772{
773 int i, max;
774 int ints[MAX_PIRQS+1];
775
776 get_options(str, ARRAY_SIZE(ints), ints);
777
778 for (i = 0; i < MAX_PIRQS; i++)
779 pirq_entries[i] = -1;
780
781 pirqs_enabled = 1;
782 apic_printk(APIC_VERBOSE, KERN_INFO
783 "PIRQ redirection, working around broken MP-BIOS.\n");
784 max = MAX_PIRQS;
785 if (ints[0] < MAX_PIRQS)
786 max = ints[0];
787
788 for (i = 0; i < max; i++) {
789 apic_printk(APIC_VERBOSE, KERN_DEBUG
790 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
791 /*
792 * PIRQs are mapped upside down, usually.
793 */
794 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
795 }
796 return 1;
797}
798
799__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200800#endif /* CONFIG_X86_32 */
801
802#ifdef CONFIG_INTR_REMAP
803/* I/O APIC RTE contents at the OS boot up */
804static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
805
806/*
807 * Saves and masks all the unmasked IO-APIC RTE's
808 */
809int save_mask_IO_APIC_setup(void)
810{
811 union IO_APIC_reg_01 reg_01;
812 unsigned long flags;
813 int apic, pin;
814
815 /*
816 * The number of IO-APIC IRQ registers (== #pins):
817 */
818 for (apic = 0; apic < nr_ioapics; apic++) {
819 spin_lock_irqsave(&ioapic_lock, flags);
820 reg_01.raw = io_apic_read(apic, 1);
821 spin_unlock_irqrestore(&ioapic_lock, flags);
822 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
823 }
824
825 for (apic = 0; apic < nr_ioapics; apic++) {
826 early_ioapic_entries[apic] =
827 kzalloc(sizeof(struct IO_APIC_route_entry) *
828 nr_ioapic_registers[apic], GFP_KERNEL);
829 if (!early_ioapic_entries[apic])
830 return -ENOMEM;
831 }
832
833 for (apic = 0; apic < nr_ioapics; apic++)
834 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
835 struct IO_APIC_route_entry entry;
836
837 entry = early_ioapic_entries[apic][pin] =
838 ioapic_read_entry(apic, pin);
839 if (!entry.mask) {
840 entry.mask = 1;
841 ioapic_write_entry(apic, pin, entry);
842 }
843 }
844 return 0;
845}
846
847void restore_IO_APIC_setup(void)
848{
849 int apic, pin;
850
851 for (apic = 0; apic < nr_ioapics; apic++)
852 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
853 ioapic_write_entry(apic, pin,
854 early_ioapic_entries[apic][pin]);
855}
856
857void reinit_intr_remapped_IO_APIC(int intr_remapping)
858{
859 /*
860 * for now plain restore of previous settings.
861 * TBD: In the case of OS enabling interrupt-remapping,
862 * IO-APIC RTE's need to be setup to point to interrupt-remapping
863 * table entries. for now, do a plain restore, and wait for
864 * the setup_IO_APIC_irqs() to do proper initialization.
865 */
866 restore_IO_APIC_setup();
867}
868#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870/*
871 * Find the IRQ entry number of a certain pin.
872 */
873static int find_irq_entry(int apic, int pin, int type)
874{
875 int i;
876
877 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400878 if (mp_irqs[i].mp_irqtype == type &&
879 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
880 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
881 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return i;
883
884 return -1;
885}
886
887/*
888 * Find the pin to which IRQ[irq] (ISA) is connected
889 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800890static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
892 int i;
893
894 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400895 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300897 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400898 (mp_irqs[i].mp_irqtype == type) &&
899 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400901 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
903 return -1;
904}
905
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800906static int __init find_isa_irq_apic(int irq, int type)
907{
908 int i;
909
910 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400911 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800912
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300913 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400914 (mp_irqs[i].mp_irqtype == type) &&
915 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800916 break;
917 }
918 if (i < mp_irq_entries) {
919 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200920 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400921 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800922 return apic;
923 }
924 }
925
926 return -1;
927}
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929/*
930 * Find a specific PCI IRQ entry.
931 * Not an __init, possibly needed by modules
932 */
933static int pin_2_irq(int idx, int apic, int pin);
934
935int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
936{
937 int apic, i, best_guess = -1;
938
Ingo Molnar54168ed2008-08-20 09:07:45 +0200939 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
940 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400941 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200942 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return -1;
944 }
945 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400946 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400949 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
950 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 break;
952
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300953 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400954 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400956 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200957 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (!(apic || IO_APIC_IRQ(irq)))
960 continue;
961
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400962 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return irq;
964 /*
965 * Use the first all-but-pin matching entry as a
966 * best-guess fuzzy result for broken mptables.
967 */
968 if (best_guess < 0)
969 best_guess = irq;
970 }
971 }
972 return best_guess;
973}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200974
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700975EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300977#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978/*
979 * EISA Edge/Level control register, ELCR
980 */
981static int EISA_ELCR(unsigned int irq)
982{
983 if (irq < 16) {
984 unsigned int port = 0x4d0 + (irq >> 3);
985 return (inb(port) >> (irq & 7)) & 1;
986 }
987 apic_printk(APIC_VERBOSE, KERN_INFO
988 "Broken MPtable reports ISA irq %d\n", irq);
989 return 0;
990}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200991
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300992#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300994/* ISA interrupts are always polarity zero edge triggered,
995 * when listed as conforming in the MP table. */
996
997#define default_ISA_trigger(idx) (0)
998#define default_ISA_polarity(idx) (0)
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000/* EISA interrupts are always polarity zero and can be edge or level
1001 * trigger depending on the ELCR value. If an interrupt is listed as
1002 * EISA conforming in the MP table, that means its trigger type must
1003 * be read in from the ELCR */
1004
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001005#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001006#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008/* PCI interrupts are always polarity one level triggered,
1009 * when listed as conforming in the MP table. */
1010
1011#define default_PCI_trigger(idx) (1)
1012#define default_PCI_polarity(idx) (1)
1013
1014/* MCA interrupts are always polarity zero level triggered,
1015 * when listed as conforming in the MP table. */
1016
1017#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001018#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
Shaohua Li61fd47e2007-11-17 01:05:28 -05001020static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001022 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 int polarity;
1024
1025 /*
1026 * Determine IRQ line polarity (high active or low active):
1027 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001028 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001030 case 0: /* conforms, ie. bus-type dependent polarity */
1031 if (test_bit(bus, mp_bus_not_pci))
1032 polarity = default_ISA_polarity(idx);
1033 else
1034 polarity = default_PCI_polarity(idx);
1035 break;
1036 case 1: /* high active */
1037 {
1038 polarity = 0;
1039 break;
1040 }
1041 case 2: /* reserved */
1042 {
1043 printk(KERN_WARNING "broken BIOS!!\n");
1044 polarity = 1;
1045 break;
1046 }
1047 case 3: /* low active */
1048 {
1049 polarity = 1;
1050 break;
1051 }
1052 default: /* invalid */
1053 {
1054 printk(KERN_WARNING "broken BIOS!!\n");
1055 polarity = 1;
1056 break;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059 return polarity;
1060}
1061
1062static int MPBIOS_trigger(int idx)
1063{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001064 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 int trigger;
1066
1067 /*
1068 * Determine IRQ trigger mode (edge or level sensitive):
1069 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001070 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001072 case 0: /* conforms, ie. bus-type dependent */
1073 if (test_bit(bus, mp_bus_not_pci))
1074 trigger = default_ISA_trigger(idx);
1075 else
1076 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001077#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001078 switch (mp_bus_id_to_type[bus]) {
1079 case MP_BUS_ISA: /* ISA pin */
1080 {
1081 /* set before the switch */
1082 break;
1083 }
1084 case MP_BUS_EISA: /* EISA pin */
1085 {
1086 trigger = default_EISA_trigger(idx);
1087 break;
1088 }
1089 case MP_BUS_PCI: /* PCI pin */
1090 {
1091 /* set before the switch */
1092 break;
1093 }
1094 case MP_BUS_MCA: /* MCA pin */
1095 {
1096 trigger = default_MCA_trigger(idx);
1097 break;
1098 }
1099 default:
1100 {
1101 printk(KERN_WARNING "broken BIOS!!\n");
1102 trigger = 1;
1103 break;
1104 }
1105 }
1106#endif
1107 break;
1108 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001109 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001110 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001111 break;
1112 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001113 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001114 {
1115 printk(KERN_WARNING "broken BIOS!!\n");
1116 trigger = 1;
1117 break;
1118 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001119 case 3: /* level */
1120 {
1121 trigger = 1;
1122 break;
1123 }
1124 default: /* invalid */
1125 {
1126 printk(KERN_WARNING "broken BIOS!!\n");
1127 trigger = 0;
1128 break;
1129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131 return trigger;
1132}
1133
1134static inline int irq_polarity(int idx)
1135{
1136 return MPBIOS_polarity(idx);
1137}
1138
1139static inline int irq_trigger(int idx)
1140{
1141 return MPBIOS_trigger(idx);
1142}
1143
Yinghai Luefa25592008-08-19 20:50:36 -07001144int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static int pin_2_irq(int idx, int apic, int pin)
1146{
1147 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001148 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 /*
1151 * Debugging check, we are in big trouble if this message pops up!
1152 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001153 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1155
Ingo Molnar54168ed2008-08-20 09:07:45 +02001156 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001157 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001158 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001159 /*
1160 * PCI IRQs are mapped in order
1161 */
1162 i = irq = 0;
1163 while (i < apic)
1164 irq += nr_ioapic_registers[i++];
1165 irq += pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001166 /*
1167 * For MPS mode, so far only needed by ES7000 platform
1168 */
1169 if (ioapic_renumber_irq)
1170 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
Ingo Molnar54168ed2008-08-20 09:07:45 +02001173#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 /*
1175 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1176 */
1177 if ((pin >= 16) && (pin <= 23)) {
1178 if (pirq_entries[pin-16] != -1) {
1179 if (!pirq_entries[pin-16]) {
1180 apic_printk(APIC_VERBOSE, KERN_DEBUG
1181 "disabling PIRQ%d\n", pin-16);
1182 } else {
1183 irq = pirq_entries[pin-16];
1184 apic_printk(APIC_VERBOSE, KERN_DEBUG
1185 "using PIRQ%d -> IRQ %d\n",
1186 pin-16, irq);
1187 }
1188 }
1189 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001190#endif
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return irq;
1193}
1194
Yinghai Lu497c9a12008-08-19 20:50:28 -07001195void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001197 /* Used to the online set of cpus does not change
1198 * during assign_irq_vector.
1199 */
1200 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Yinghai Lu497c9a12008-08-19 20:50:28 -07001203void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001204{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001205 spin_unlock(&vector_lock);
1206}
1207
1208static int __assign_irq_vector(int irq, cpumask_t mask)
1209{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001210 /*
1211 * NOTE! The local APIC isn't very good at handling
1212 * multiple interrupts at the same interrupt level.
1213 * As the interrupt level is determined by taking the
1214 * vector number and shifting that right by 4, we
1215 * want to spread these out a bit so that they don't
1216 * all fall in the same interrupt level.
1217 *
1218 * Also, we've got to be careful not to trash gate
1219 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1220 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001221 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1222 unsigned int old_vector;
1223 int cpu;
1224 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001225
Ingo Molnar54168ed2008-08-20 09:07:45 +02001226 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001227
Ingo Molnar54168ed2008-08-20 09:07:45 +02001228 /* Only try and allocate irqs on cpus that are present */
1229 cpus_and(mask, mask, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001230
Ingo Molnar54168ed2008-08-20 09:07:45 +02001231 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1232 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001233
Ingo Molnar54168ed2008-08-20 09:07:45 +02001234 old_vector = cfg->vector;
1235 if (old_vector) {
1236 cpumask_t tmp;
1237 cpus_and(tmp, cfg->domain, mask);
1238 if (!cpus_empty(tmp))
1239 return 0;
1240 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001241
Ingo Molnar54168ed2008-08-20 09:07:45 +02001242 for_each_cpu_mask_nr(cpu, mask) {
1243 cpumask_t domain, new_mask;
1244 int new_cpu;
1245 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001246
Ingo Molnar54168ed2008-08-20 09:07:45 +02001247 domain = vector_allocation_domain(cpu);
1248 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001249
Ingo Molnar54168ed2008-08-20 09:07:45 +02001250 vector = current_vector;
1251 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001252next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001253 vector += 8;
1254 if (vector >= first_system_vector) {
1255 /* If we run out of vectors on large boxen, must share them. */
1256 offset = (offset + 1) % 8;
1257 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001258 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001259 if (unlikely(current_vector == vector))
1260 continue;
1261#ifdef CONFIG_X86_64
1262 if (vector == IA32_SYSCALL_VECTOR)
1263 goto next;
1264#else
1265 if (vector == SYSCALL_VECTOR)
1266 goto next;
1267#endif
1268 for_each_cpu_mask_nr(new_cpu, new_mask)
1269 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1270 goto next;
1271 /* Found one! */
1272 current_vector = vector;
1273 current_offset = offset;
1274 if (old_vector) {
1275 cfg->move_in_progress = 1;
1276 cfg->old_domain = cfg->domain;
1277 }
1278 for_each_cpu_mask_nr(new_cpu, new_mask)
1279 per_cpu(vector_irq, new_cpu)[vector] = irq;
1280 cfg->vector = vector;
1281 cfg->domain = domain;
1282 return 0;
1283 }
1284 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001285}
1286
1287static int assign_irq_vector(int irq, cpumask_t mask)
1288{
1289 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001290 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001291
1292 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001293 err = __assign_irq_vector(irq, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001294 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001295 return err;
1296}
1297
1298static void __clear_irq_vector(int irq)
1299{
1300 struct irq_cfg *cfg;
1301 cpumask_t mask;
1302 int cpu, vector;
1303
1304 cfg = irq_cfg(irq);
1305 BUG_ON(!cfg->vector);
1306
1307 vector = cfg->vector;
1308 cpus_and(mask, cfg->domain, cpu_online_map);
1309 for_each_cpu_mask_nr(cpu, mask)
1310 per_cpu(vector_irq, cpu)[vector] = -1;
1311
1312 cfg->vector = 0;
1313 cpus_clear(cfg->domain);
1314}
1315
1316void __setup_vector_irq(int cpu)
1317{
1318 /* Initialize vector_irq on a new cpu */
1319 /* This function must be called with vector_lock held */
1320 int irq, vector;
1321 struct irq_cfg *cfg;
1322
1323 /* Mark the inuse vectors */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001324 for_each_irq_cfg(irq, cfg) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001325 if (!cpu_isset(cpu, cfg->domain))
1326 continue;
1327 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001328 per_cpu(vector_irq, cpu)[vector] = irq;
1329 }
1330 /* Mark the free vectors */
1331 for (vector = 0; vector < NR_VECTORS; ++vector) {
1332 irq = per_cpu(vector_irq, cpu)[vector];
1333 if (irq < 0)
1334 continue;
1335
1336 cfg = irq_cfg(irq);
1337 if (!cpu_isset(cpu, cfg->domain))
1338 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001339 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001340}
Glauber Costa3fde6902008-05-28 20:34:19 -07001341
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001342static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001343#ifdef CONFIG_INTR_REMAP
1344static struct irq_chip ir_ioapic_chip;
1345#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Ingo Molnar54168ed2008-08-20 09:07:45 +02001347#define IOAPIC_AUTO -1
1348#define IOAPIC_EDGE 0
1349#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001351#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001352static inline int IO_APIC_irq_trigger(int irq)
1353{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001354 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001355
Ingo Molnar54168ed2008-08-20 09:07:45 +02001356 for (apic = 0; apic < nr_ioapics; apic++) {
1357 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1358 idx = find_irq_entry(apic, pin, mp_INT);
1359 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1360 return irq_trigger(idx);
1361 }
1362 }
1363 /*
1364 * nonexistent IRQs are edge default
1365 */
1366 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001367}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001368#else
1369static inline int IO_APIC_irq_trigger(int irq)
1370{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001371 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001372}
1373#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001374
Yinghai Lu497c9a12008-08-19 20:50:28 -07001375static void ioapic_register_intr(int irq, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Yinghai Lu08678b02008-08-19 20:50:05 -07001377 struct irq_desc *desc;
1378
Yinghai Lu199751d2008-08-19 20:50:27 -07001379 /* first time to use this irq_desc */
1380 if (irq < 16)
1381 desc = irq_to_desc(irq);
1382 else
1383 desc = irq_to_desc_alloc(irq);
1384
Jan Beulich6ebcc002006-06-26 13:56:46 +02001385 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001386 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001387 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001388 else
1389 desc->status &= ~IRQ_LEVEL;
1390
Ingo Molnar54168ed2008-08-20 09:07:45 +02001391#ifdef CONFIG_INTR_REMAP
1392 if (irq_remapped(irq)) {
1393 desc->status |= IRQ_MOVE_PCNTXT;
1394 if (trigger)
1395 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1396 handle_fasteoi_irq,
1397 "fasteoi");
1398 else
1399 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1400 handle_edge_irq, "edge");
1401 return;
1402 }
1403#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001404 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1405 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001406 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001407 handle_fasteoi_irq,
1408 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001409 else
Ingo Molnara460e742006-10-17 00:10:03 -07001410 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001411 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001412}
1413
1414static int setup_ioapic_entry(int apic, int irq,
1415 struct IO_APIC_route_entry *entry,
1416 unsigned int destination, int trigger,
1417 int polarity, int vector)
1418{
1419 /*
1420 * add it to the IO-APIC irq-routing table:
1421 */
1422 memset(entry,0,sizeof(*entry));
1423
Ingo Molnar54168ed2008-08-20 09:07:45 +02001424#ifdef CONFIG_INTR_REMAP
1425 if (intr_remapping_enabled) {
1426 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1427 struct irte irte;
1428 struct IR_IO_APIC_route_entry *ir_entry =
1429 (struct IR_IO_APIC_route_entry *) entry;
1430 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001431
Ingo Molnar54168ed2008-08-20 09:07:45 +02001432 if (!iommu)
1433 panic("No mapping iommu for ioapic %d\n", apic);
1434
1435 index = alloc_irte(iommu, irq, 1);
1436 if (index < 0)
1437 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1438
1439 memset(&irte, 0, sizeof(irte));
1440
1441 irte.present = 1;
1442 irte.dst_mode = INT_DEST_MODE;
1443 irte.trigger_mode = trigger;
1444 irte.dlvry_mode = INT_DELIVERY_MODE;
1445 irte.vector = vector;
1446 irte.dest_id = IRTE_DEST(destination);
1447
1448 modify_irte(irq, &irte);
1449
1450 ir_entry->index2 = (index >> 15) & 0x1;
1451 ir_entry->zero = 0;
1452 ir_entry->format = 1;
1453 ir_entry->index = (index & 0x7fff);
1454 } else
1455#endif
1456 {
1457 entry->delivery_mode = INT_DELIVERY_MODE;
1458 entry->dest_mode = INT_DEST_MODE;
1459 entry->dest = destination;
1460 }
1461
1462 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001463 entry->trigger = trigger;
1464 entry->polarity = polarity;
1465 entry->vector = vector;
1466
1467 /* Mask level triggered irqs.
1468 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1469 */
1470 if (trigger)
1471 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001472 return 0;
1473}
1474
1475static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001476 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001477{
1478 struct irq_cfg *cfg;
1479 struct IO_APIC_route_entry entry;
1480 cpumask_t mask;
1481
1482 if (!IO_APIC_IRQ(irq))
1483 return;
1484
1485 cfg = irq_cfg(irq);
1486
1487 mask = TARGET_CPUS;
1488 if (assign_irq_vector(irq, mask))
1489 return;
1490
1491 cpus_and(mask, cfg->domain, mask);
1492
1493 apic_printk(APIC_VERBOSE,KERN_DEBUG
1494 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1495 "IRQ %d Mode:%i Active:%i)\n",
1496 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1497 irq, trigger, polarity);
1498
1499
1500 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1501 cpu_mask_to_apicid(mask), trigger, polarity,
1502 cfg->vector)) {
1503 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1504 mp_ioapics[apic].mp_apicid, pin);
1505 __clear_irq_vector(irq);
1506 return;
1507 }
1508
1509 ioapic_register_intr(irq, trigger);
1510 if (irq < 16)
1511 disable_8259A_irq(irq);
1512
1513 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
1515
1516static void __init setup_IO_APIC_irqs(void)
1517{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001518 int apic, pin, idx, irq, first_notcon = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1521
1522 for (apic = 0; apic < nr_ioapics; apic++) {
1523 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1524
Yinghai Lu497c9a12008-08-19 20:50:28 -07001525 idx = find_irq_entry(apic,pin,mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 if (idx == -1) {
1527 if (first_notcon) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001528 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 first_notcon = 0;
1530 } else
Yinghai Lu497c9a12008-08-19 20:50:28 -07001531 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 continue;
1533 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001534 if (!first_notcon) {
1535 apic_printk(APIC_VERBOSE, " not connected.\n");
1536 first_notcon = 1;
1537 }
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001540#ifdef CONFIG_X86_32
Yinghai Lu497c9a12008-08-19 20:50:28 -07001541 if (multi_timer_check(apic, irq))
1542 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001543#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07001544 add_pin_to_irq(irq, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001545
Yinghai Lu497c9a12008-08-19 20:50:28 -07001546 setup_IO_APIC_irq(apic, pin, irq,
1547 irq_trigger(idx), irq_polarity(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549 }
1550
1551 if (!first_notcon)
1552 apic_printk(APIC_VERBOSE, " not connected.\n");
1553}
1554
1555/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001556 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001558static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1559 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Ingo Molnar54168ed2008-08-20 09:07:45 +02001563#ifdef CONFIG_INTR_REMAP
1564 if (intr_remapping_enabled)
1565 return;
1566#endif
1567
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001568 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /*
1571 * We use logical delivery to get the timer IRQ
1572 * to the first CPU.
1573 */
1574 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001575 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001576 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 entry.delivery_mode = INT_DELIVERY_MODE;
1578 entry.polarity = 0;
1579 entry.trigger = 0;
1580 entry.vector = vector;
1581
1582 /*
1583 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001584 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001586 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
1589 * Add it to the IO-APIC irq-routing table:
1590 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001591 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001594
1595__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 int apic, i;
1598 union IO_APIC_reg_00 reg_00;
1599 union IO_APIC_reg_01 reg_01;
1600 union IO_APIC_reg_02 reg_02;
1601 union IO_APIC_reg_03 reg_03;
1602 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001603 struct irq_cfg *cfg;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001604 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 if (apic_verbosity == APIC_QUIET)
1607 return;
1608
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001609 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 for (i = 0; i < nr_ioapics; i++)
1611 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001612 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /*
1615 * We are a bit conservative about what we expect. We have to
1616 * know about every hardware change ASAP.
1617 */
1618 printk(KERN_INFO "testing the IO APIC.......................\n");
1619
1620 for (apic = 0; apic < nr_ioapics; apic++) {
1621
1622 spin_lock_irqsave(&ioapic_lock, flags);
1623 reg_00.raw = io_apic_read(apic, 0);
1624 reg_01.raw = io_apic_read(apic, 1);
1625 if (reg_01.bits.version >= 0x10)
1626 reg_02.raw = io_apic_read(apic, 2);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001627 if (reg_01.bits.version >= 0x20)
1628 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 spin_unlock_irqrestore(&ioapic_lock, flags);
1630
Ingo Molnar54168ed2008-08-20 09:07:45 +02001631 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001632 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1634 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1635 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1636 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Ingo Molnar54168ed2008-08-20 09:07:45 +02001638 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1642 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
1644 /*
1645 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1646 * but the value of reg_02 is read as the previous read register
1647 * value, so ignore it if reg_02 == reg_01.
1648 */
1649 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1650 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1651 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
1654 /*
1655 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1656 * or reg_03, but the value of reg_0[23] is read as the previous read
1657 * register value, so ignore it if reg_03 == reg_0[12].
1658 */
1659 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1660 reg_03.raw != reg_01.raw) {
1661 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1662 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 }
1664
1665 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1666
Yinghai Lud83e94a2008-08-19 20:50:33 -07001667 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1668 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 for (i = 0; i <= reg_01.bits.entries; i++) {
1671 struct IO_APIC_route_entry entry;
1672
Andi Kleencf4c6a22006-09-26 10:52:30 +02001673 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Ingo Molnar54168ed2008-08-20 09:07:45 +02001675 printk(KERN_DEBUG " %02x %03X ",
1676 i,
1677 entry.dest
1678 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1681 entry.mask,
1682 entry.trigger,
1683 entry.irr,
1684 entry.polarity,
1685 entry.delivery_status,
1686 entry.dest_mode,
1687 entry.delivery_mode,
1688 entry.vector
1689 );
1690 }
1691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001693 for_each_irq_cfg(irq, cfg) {
Yinghai Lu0f978f42008-08-19 20:50:26 -07001694 struct irq_pin_list *entry = cfg->irq_2_pin;
1695 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001697 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 for (;;) {
1699 printk("-> %d:%d", entry->apic, entry->pin);
1700 if (!entry->next)
1701 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001702 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704 printk("\n");
1705 }
1706
1707 printk(KERN_INFO ".................................... done.\n");
1708
1709 return;
1710}
1711
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001712__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 unsigned int v;
1715 int i, j;
1716
1717 if (apic_verbosity == APIC_QUIET)
1718 return;
1719
1720 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1721 for (i = 0; i < 8; i++) {
1722 v = apic_read(base + i*0x10);
1723 for (j = 0; j < 32; j++) {
1724 if (v & (1<<j))
1725 printk("1");
1726 else
1727 printk("0");
1728 }
1729 printk("\n");
1730 }
1731}
1732
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001733__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
1735 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001736 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 if (apic_verbosity == APIC_QUIET)
1739 return;
1740
1741 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1742 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001743 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001744 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 v = apic_read(APIC_LVR);
1746 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1747 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001748 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 v = apic_read(APIC_TASKPRI);
1751 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1752
Ingo Molnar54168ed2008-08-20 09:07:45 +02001753 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 v = apic_read(APIC_ARBPRI);
1755 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1756 v & APIC_ARBPRI_MASK);
1757 v = apic_read(APIC_PROCPRI);
1758 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1759 }
1760
1761 v = apic_read(APIC_EOI);
1762 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1763 v = apic_read(APIC_RRR);
1764 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1765 v = apic_read(APIC_LDR);
1766 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1767 v = apic_read(APIC_DFR);
1768 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1769 v = apic_read(APIC_SPIV);
1770 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1771
1772 printk(KERN_DEBUG "... APIC ISR field:\n");
1773 print_APIC_bitfield(APIC_ISR);
1774 printk(KERN_DEBUG "... APIC TMR field:\n");
1775 print_APIC_bitfield(APIC_TMR);
1776 printk(KERN_DEBUG "... APIC IRR field:\n");
1777 print_APIC_bitfield(APIC_IRR);
1778
Ingo Molnar54168ed2008-08-20 09:07:45 +02001779 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1780 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 v = apic_read(APIC_ESR);
1784 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1785 }
1786
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001787 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001788 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1789 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 v = apic_read(APIC_LVTT);
1792 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1793
1794 if (maxlvt > 3) { /* PC is LVT#4. */
1795 v = apic_read(APIC_LVTPC);
1796 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1797 }
1798 v = apic_read(APIC_LVT0);
1799 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1800 v = apic_read(APIC_LVT1);
1801 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1802
1803 if (maxlvt > 2) { /* ERR is LVT#3. */
1804 v = apic_read(APIC_LVTERR);
1805 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1806 }
1807
1808 v = apic_read(APIC_TMICT);
1809 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1810 v = apic_read(APIC_TMCCT);
1811 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1812 v = apic_read(APIC_TDCR);
1813 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1814 printk("\n");
1815}
1816
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001817__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001819 int cpu;
1820
1821 preempt_disable();
1822 for_each_online_cpu(cpu)
1823 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1824 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001827__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 unsigned int v;
1830 unsigned long flags;
1831
1832 if (apic_verbosity == APIC_QUIET)
1833 return;
1834
1835 printk(KERN_DEBUG "\nprinting PIC contents\n");
1836
1837 spin_lock_irqsave(&i8259A_lock, flags);
1838
1839 v = inb(0xa1) << 8 | inb(0x21);
1840 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1841
1842 v = inb(0xa0) << 8 | inb(0x20);
1843 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1844
Ingo Molnar54168ed2008-08-20 09:07:45 +02001845 outb(0x0b,0xa0);
1846 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001848 outb(0x0a,0xa0);
1849 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 spin_unlock_irqrestore(&i8259A_lock, flags);
1852
1853 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1854
1855 v = inb(0x4d1) << 8 | inb(0x4d0);
1856 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1857}
1858
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001859__apicdebuginit(int) print_all_ICs(void)
1860{
1861 print_PIC();
1862 print_all_local_APICs();
1863 print_IO_APIC();
1864
1865 return 0;
1866}
1867
1868fs_initcall(print_all_ICs);
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Yinghai Luefa25592008-08-19 20:50:36 -07001871/* Where if anywhere is the i8259 connect in external int mode */
1872static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1873
Ingo Molnar54168ed2008-08-20 09:07:45 +02001874void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001877 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001878 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 unsigned long flags;
1880
Ingo Molnar54168ed2008-08-20 09:07:45 +02001881#ifdef CONFIG_X86_32
1882 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (!pirqs_enabled)
1884 for (i = 0; i < MAX_PIRQS; i++)
1885 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001886#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
1888 /*
1889 * The number of IO-APIC IRQ registers (== #pins):
1890 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001891 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001893 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001895 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1896 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001897 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001898 int pin;
1899 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001900 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001901 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001902 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001903
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001904 /* If the interrupt line is enabled and in ExtInt mode
1905 * I have found the pin where the i8259 is connected.
1906 */
1907 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1908 ioapic_i8259.apic = apic;
1909 ioapic_i8259.pin = pin;
1910 goto found_i8259;
1911 }
1912 }
1913 }
1914 found_i8259:
1915 /* Look to see what if the MP table has reported the ExtINT */
1916 /* If we could not find the appropriate pin by looking at the ioapic
1917 * the i8259 probably is not connected the ioapic but give the
1918 * mptable a chance anyway.
1919 */
1920 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1921 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1922 /* Trust the MP table if nothing is setup in the hardware */
1923 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1924 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1925 ioapic_i8259.pin = i8259_pin;
1926 ioapic_i8259.apic = i8259_apic;
1927 }
1928 /* Complain if the MP table and the hardware disagree */
1929 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1930 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1931 {
1932 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
1935 /*
1936 * Do not trust the IO-APIC being empty at bootup
1937 */
1938 clear_IO_APIC();
1939}
1940
1941/*
1942 * Not an __init, needed by the reboot code
1943 */
1944void disable_IO_APIC(void)
1945{
1946 /*
1947 * Clear the IO-APIC before rebooting:
1948 */
1949 clear_IO_APIC();
1950
Eric W. Biederman650927e2005-06-25 14:57:44 -07001951 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001952 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001953 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001954 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001955 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001956 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001957 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001958
1959 memset(&entry, 0, sizeof(entry));
1960 entry.mask = 0; /* Enabled */
1961 entry.trigger = 0; /* Edge */
1962 entry.irr = 0;
1963 entry.polarity = 0; /* High */
1964 entry.delivery_status = 0;
1965 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001966 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001967 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001968 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001969
1970 /*
1971 * Add it to the IO-APIC irq-routing table:
1972 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001973 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001974 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001975
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001976 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Ingo Molnar54168ed2008-08-20 09:07:45 +02001979#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980/*
1981 * function to set the IO-APIC physical IDs based on the
1982 * values stored in the MPC table.
1983 *
1984 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1985 */
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987static void __init setup_ioapic_ids_from_mpc(void)
1988{
1989 union IO_APIC_reg_00 reg_00;
1990 physid_mask_t phys_id_present_map;
1991 int apic;
1992 int i;
1993 unsigned char old_id;
1994 unsigned long flags;
1995
Yinghai Lua4dbc342008-07-25 02:14:28 -07001996 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001997 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002000 * Don't check I/O APIC IDs for xAPIC systems. They have
2001 * no meaning without the serial APIC bus.
2002 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002003 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2004 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002005 return;
2006 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * This is broken; anything with a real cpu count has to
2008 * circumvent this idiocy regardless.
2009 */
2010 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2011
2012 /*
2013 * Set the IOAPIC ID to the value stored in the MPC table.
2014 */
2015 for (apic = 0; apic < nr_ioapics; apic++) {
2016
2017 /* Read the register 0 value */
2018 spin_lock_irqsave(&ioapic_lock, flags);
2019 reg_00.raw = io_apic_read(apic, 0);
2020 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002021
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002022 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002024 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002026 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2028 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002029 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 }
2031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 /*
2033 * Sanity check, is the ID really free? Every APIC in a
2034 * system must have a unique ID or we get lots of nice
2035 * 'stuck on smp_invalidate_needed IPI wait' messages.
2036 */
2037 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002038 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002040 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 for (i = 0; i < get_physical_broadcast(); i++)
2042 if (!physid_isset(i, phys_id_present_map))
2043 break;
2044 if (i >= get_physical_broadcast())
2045 panic("Max APIC ID exceeded!\n");
2046 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2047 i);
2048 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002049 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 } else {
2051 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002052 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 apic_printk(APIC_VERBOSE, "Setting %d in the "
2054 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002055 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2057 }
2058
2059
2060 /*
2061 * We need to adjust the IRQ routing table
2062 * if the ID changed.
2063 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002064 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002066 if (mp_irqs[i].mp_dstapic == old_id)
2067 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002068 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 /*
2071 * Read the right value from the MPC table and
2072 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 apic_printk(APIC_VERBOSE, KERN_INFO
2075 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002076 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002078 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002080 io_apic_write(apic, 0, reg_00.raw);
2081 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 /*
2084 * Sanity check
2085 */
2086 spin_lock_irqsave(&ioapic_lock, flags);
2087 reg_00.raw = io_apic_read(apic, 0);
2088 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002089 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 printk("could not set ID!\n");
2091 else
2092 apic_printk(APIC_VERBOSE, " ok.\n");
2093 }
2094}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002095#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002097int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002098
2099static int __init notimercheck(char *s)
2100{
2101 no_timer_check = 1;
2102 return 1;
2103}
2104__setup("no_timer_check", notimercheck);
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106/*
2107 * There is a nasty bug in some older SMP boards, their mptable lies
2108 * about the timer IRQ. We do the following to work around the situation:
2109 *
2110 * - timer IRQ defaults to IO-APIC IRQ
2111 * - if this function detects that timer IRQs are defunct, then we fall
2112 * back to ISA timer IRQs
2113 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002114static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
2116 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002117 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Zachary Amsden8542b202006-12-07 02:14:09 +01002119 if (no_timer_check)
2120 return 1;
2121
Ingo Molnar4aae0702007-12-18 18:05:58 +01002122 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 local_irq_enable();
2124 /* Let ten ticks pass... */
2125 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002126 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 /*
2129 * Expect a few ticks at least, to be sure some possible
2130 * glue logic does not lock up after one or two first
2131 * ticks in a non-ExtINT mode. Also the local APIC
2132 * might have cached one ExtINT interrupt. Finally, at
2133 * least one tick may be lost due to delays.
2134 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002135
2136 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002137 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 return 0;
2140}
2141
2142/*
2143 * In the SMP+IOAPIC case it might happen that there are an unspecified
2144 * number of pending IRQ events unhandled. These cases are very rare,
2145 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2146 * better to do it this way as thus we do not have to be aware of
2147 * 'pending' interrupts in the IRQ path, except at this point.
2148 */
2149/*
2150 * Edge triggered needs to resend any interrupt
2151 * that was delayed but this is now handled in the device
2152 * independent code.
2153 */
2154
2155/*
2156 * Starting up a edge-triggered IO-APIC interrupt is
2157 * nasty - we need to make sure that we get the edge.
2158 * If it is already asserted for some reason, we need
2159 * return 1 to indicate that is was pending.
2160 *
2161 * This is not complete - we should be able to fake
2162 * an edge even if it isn't on the 8259A...
2163 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002164
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002165static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 int was_pending = 0;
2168 unsigned long flags;
2169
2170 spin_lock_irqsave(&ioapic_lock, flags);
2171 if (irq < 16) {
2172 disable_8259A_irq(irq);
2173 if (i8259A_irq_pending(irq))
2174 was_pending = 1;
2175 }
2176 __unmask_IO_APIC_irq(irq);
2177 spin_unlock_irqrestore(&ioapic_lock, flags);
2178
2179 return was_pending;
2180}
2181
Ingo Molnar54168ed2008-08-20 09:07:45 +02002182#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002183static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002185
2186 struct irq_cfg *cfg = irq_cfg(irq);
2187 unsigned long flags;
2188
2189 spin_lock_irqsave(&vector_lock, flags);
2190 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2191 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002192
2193 return 1;
2194}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002195#else
2196static int ioapic_retrigger_irq(unsigned int irq)
2197{
2198 send_IPI_self(irq_cfg(irq)->vector);
2199
2200 return 1;
2201}
2202#endif
2203
2204/*
2205 * Level and edge triggered IO-APIC interrupts need different handling,
2206 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2207 * handled with the level-triggered descriptor, but that one has slightly
2208 * more overhead. Level-triggered interrupts cannot be handled with the
2209 * edge-triggered handler, without risking IRQ storms and other ugly
2210 * races.
2211 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002212
Yinghai Lu497c9a12008-08-19 20:50:28 -07002213#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002214
2215#ifdef CONFIG_INTR_REMAP
2216static void ir_irq_migration(struct work_struct *work);
2217
2218static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2219
2220/*
2221 * Migrate the IO-APIC irq in the presence of intr-remapping.
2222 *
2223 * For edge triggered, irq migration is a simple atomic update(of vector
2224 * and cpu destination) of IRTE and flush the hardware cache.
2225 *
2226 * For level triggered, we need to modify the io-apic RTE aswell with the update
2227 * vector information, along with modifying IRTE with vector and destination.
2228 * So irq migration for level triggered is little bit more complex compared to
2229 * edge triggered migration. But the good news is, we use the same algorithm
2230 * for level triggered migration as we have today, only difference being,
2231 * we now initiate the irq migration from process context instead of the
2232 * interrupt context.
2233 *
2234 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2235 * suppression) to the IO-APIC, level triggered irq migration will also be
2236 * as simple as edge triggered migration and we can do the irq migration
2237 * with a simple atomic update to IO-APIC RTE.
2238 */
2239static void migrate_ioapic_irq(int irq, cpumask_t mask)
2240{
2241 struct irq_cfg *cfg;
2242 struct irq_desc *desc;
2243 cpumask_t tmp, cleanup_mask;
2244 struct irte irte;
2245 int modify_ioapic_rte;
2246 unsigned int dest;
2247 unsigned long flags;
2248
2249 cpus_and(tmp, mask, cpu_online_map);
2250 if (cpus_empty(tmp))
2251 return;
2252
2253 if (get_irte(irq, &irte))
2254 return;
2255
2256 if (assign_irq_vector(irq, mask))
2257 return;
2258
2259 cfg = irq_cfg(irq);
2260 cpus_and(tmp, cfg->domain, mask);
2261 dest = cpu_mask_to_apicid(tmp);
2262
2263 desc = irq_to_desc(irq);
2264 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2265 if (modify_ioapic_rte) {
2266 spin_lock_irqsave(&ioapic_lock, flags);
2267 __target_IO_APIC_irq(irq, dest, cfg->vector);
2268 spin_unlock_irqrestore(&ioapic_lock, flags);
2269 }
2270
2271 irte.vector = cfg->vector;
2272 irte.dest_id = IRTE_DEST(dest);
2273
2274 /*
2275 * Modified the IRTE and flushes the Interrupt entry cache.
2276 */
2277 modify_irte(irq, &irte);
2278
2279 if (cfg->move_in_progress) {
2280 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2281 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2282 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2283 cfg->move_in_progress = 0;
2284 }
2285
2286 desc->affinity = mask;
2287}
2288
2289static int migrate_irq_remapped_level(int irq)
2290{
2291 int ret = -1;
2292 struct irq_desc *desc = irq_to_desc(irq);
2293
2294 mask_IO_APIC_irq(irq);
2295
2296 if (io_apic_level_ack_pending(irq)) {
2297 /*
2298 * Interrupt in progress. Migrating irq now will change the
2299 * vector information in the IO-APIC RTE and that will confuse
2300 * the EOI broadcast performed by cpu.
2301 * So, delay the irq migration to the next instance.
2302 */
2303 schedule_delayed_work(&ir_migration_work, 1);
2304 goto unmask;
2305 }
2306
2307 /* everthing is clear. we have right of way */
2308 migrate_ioapic_irq(irq, desc->pending_mask);
2309
2310 ret = 0;
2311 desc->status &= ~IRQ_MOVE_PENDING;
2312 cpus_clear(desc->pending_mask);
2313
2314unmask:
2315 unmask_IO_APIC_irq(irq);
2316 return ret;
2317}
2318
2319static void ir_irq_migration(struct work_struct *work)
2320{
2321 unsigned int irq;
2322 struct irq_desc *desc;
2323
2324 for_each_irq_desc(irq, desc) {
2325 if (desc->status & IRQ_MOVE_PENDING) {
2326 unsigned long flags;
2327
2328 spin_lock_irqsave(&desc->lock, flags);
2329 if (!desc->chip->set_affinity ||
2330 !(desc->status & IRQ_MOVE_PENDING)) {
2331 desc->status &= ~IRQ_MOVE_PENDING;
2332 spin_unlock_irqrestore(&desc->lock, flags);
2333 continue;
2334 }
2335
2336 desc->chip->set_affinity(irq, desc->pending_mask);
2337 spin_unlock_irqrestore(&desc->lock, flags);
2338 }
2339 }
2340}
2341
2342/*
2343 * Migrates the IRQ destination in the process context.
2344 */
2345static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2346{
2347 struct irq_desc *desc = irq_to_desc(irq);
2348
2349 if (desc->status & IRQ_LEVEL) {
2350 desc->status |= IRQ_MOVE_PENDING;
2351 desc->pending_mask = mask;
2352 migrate_irq_remapped_level(irq);
2353 return;
2354 }
2355
2356 migrate_ioapic_irq(irq, mask);
2357}
2358#endif
2359
Yinghai Lu497c9a12008-08-19 20:50:28 -07002360asmlinkage void smp_irq_move_cleanup_interrupt(void)
2361{
2362 unsigned vector, me;
2363 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002364#ifdef CONFIG_X86_64
2365 exit_idle();
2366#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002367 irq_enter();
2368
2369 me = smp_processor_id();
2370 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2371 unsigned int irq;
2372 struct irq_desc *desc;
2373 struct irq_cfg *cfg;
2374 irq = __get_cpu_var(vector_irq)[vector];
2375
2376 desc = irq_to_desc(irq);
2377 if (!desc)
2378 continue;
2379
2380 cfg = irq_cfg(irq);
2381 spin_lock(&desc->lock);
2382 if (!cfg->move_cleanup_count)
2383 goto unlock;
2384
2385 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2386 goto unlock;
2387
2388 __get_cpu_var(vector_irq)[vector] = -1;
2389 cfg->move_cleanup_count--;
2390unlock:
2391 spin_unlock(&desc->lock);
2392 }
2393
2394 irq_exit();
2395}
2396
2397static void irq_complete_move(unsigned int irq)
2398{
2399 struct irq_cfg *cfg = irq_cfg(irq);
2400 unsigned vector, me;
2401
2402 if (likely(!cfg->move_in_progress))
2403 return;
2404
2405 vector = ~get_irq_regs()->orig_ax;
2406 me = smp_processor_id();
2407 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2408 cpumask_t cleanup_mask;
2409
2410 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2411 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2412 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2413 cfg->move_in_progress = 0;
2414 }
2415}
2416#else
2417static inline void irq_complete_move(unsigned int irq) {}
2418#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002419#ifdef CONFIG_INTR_REMAP
2420static void ack_x2apic_level(unsigned int irq)
2421{
2422 ack_x2APIC_irq();
2423}
2424
2425static void ack_x2apic_edge(unsigned int irq)
2426{
2427 ack_x2APIC_irq();
2428}
2429#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002430
Yinghai Lu1d025192008-08-19 20:50:34 -07002431static void ack_apic_edge(unsigned int irq)
2432{
2433 irq_complete_move(irq);
2434 move_native_irq(irq);
2435 ack_APIC_irq();
2436}
2437
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002438#ifdef CONFIG_X86_32
2439atomic_t irq_mis_count;
2440#endif
2441
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002442static void ack_apic_level(unsigned int irq)
2443{
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002444#ifdef CONFIG_X86_32
2445 unsigned long v;
2446 int i;
2447#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002448 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002449
Ingo Molnar54168ed2008-08-20 09:07:45 +02002450 irq_complete_move(irq);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002451#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002452 /* If we are moving the irq we need to mask it */
2453 if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2454 do_unmask_irq = 1;
2455 mask_IO_APIC_irq(irq);
2456 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002457#endif
2458
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002459#ifdef CONFIG_X86_32
2460 /*
2461 * It appears there is an erratum which affects at least version 0x11
2462 * of I/O APIC (that's the 82093AA and cores integrated into various
2463 * chipsets). Under certain conditions a level-triggered interrupt is
2464 * erroneously delivered as edge-triggered one but the respective IRR
2465 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2466 * message but it will never arrive and further interrupts are blocked
2467 * from the source. The exact reason is so far unknown, but the
2468 * phenomenon was observed when two consecutive interrupt requests
2469 * from a given source get delivered to the same CPU and the source is
2470 * temporarily disabled in between.
2471 *
2472 * A workaround is to simulate an EOI message manually. We achieve it
2473 * by setting the trigger mode to edge and then to level when the edge
2474 * trigger mode gets detected in the TMR of a local APIC for a
2475 * level-triggered interrupt. We mask the source for the time of the
2476 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2477 * The idea is from Manfred Spraul. --macro
2478 */
2479 i = irq_cfg(irq)->vector;
2480
2481 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2482#endif
2483
Ingo Molnar54168ed2008-08-20 09:07:45 +02002484 /*
2485 * We must acknowledge the irq before we move it or the acknowledge will
2486 * not propagate properly.
2487 */
2488 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002489
Ingo Molnar54168ed2008-08-20 09:07:45 +02002490 /* Now we can move and renable the irq */
2491 if (unlikely(do_unmask_irq)) {
2492 /* Only migrate the irq if the ack has been received.
2493 *
2494 * On rare occasions the broadcast level triggered ack gets
2495 * delayed going to ioapics, and if we reprogram the
2496 * vector while Remote IRR is still set the irq will never
2497 * fire again.
2498 *
2499 * To prevent this scenario we read the Remote IRR bit
2500 * of the ioapic. This has two effects.
2501 * - On any sane system the read of the ioapic will
2502 * flush writes (and acks) going to the ioapic from
2503 * this cpu.
2504 * - We get to see if the ACK has actually been delivered.
2505 *
2506 * Based on failed experiments of reprogramming the
2507 * ioapic entry from outside of irq context starting
2508 * with masking the ioapic entry and then polling until
2509 * Remote IRR was clear before reprogramming the
2510 * ioapic I don't trust the Remote IRR bit to be
2511 * completey accurate.
2512 *
2513 * However there appears to be no other way to plug
2514 * this race, so if the Remote IRR bit is not
2515 * accurate and is causing problems then it is a hardware bug
2516 * and you can go talk to the chipset vendor about it.
2517 */
2518 if (!io_apic_level_ack_pending(irq))
2519 move_masked_irq(irq);
2520 unmask_IO_APIC_irq(irq);
2521 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002522
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002523#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002524 if (!(v & (1 << (i & 0x1f)))) {
2525 atomic_inc(&irq_mis_count);
2526 spin_lock(&ioapic_lock);
2527 __mask_and_edge_IO_APIC_irq(irq);
2528 __unmask_and_level_IO_APIC_irq(irq);
2529 spin_unlock(&ioapic_lock);
2530 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002531#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002532}
Yinghai Lu1d025192008-08-19 20:50:34 -07002533
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002534static struct irq_chip ioapic_chip __read_mostly = {
2535 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002536 .startup = startup_ioapic_irq,
2537 .mask = mask_IO_APIC_irq,
2538 .unmask = unmask_IO_APIC_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07002539 .ack = ack_apic_edge,
2540 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002541#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002542 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002543#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002544 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545};
2546
Ingo Molnar54168ed2008-08-20 09:07:45 +02002547#ifdef CONFIG_INTR_REMAP
2548static struct irq_chip ir_ioapic_chip __read_mostly = {
2549 .name = "IR-IO-APIC",
2550 .startup = startup_ioapic_irq,
2551 .mask = mask_IO_APIC_irq,
2552 .unmask = unmask_IO_APIC_irq,
2553 .ack = ack_x2apic_edge,
2554 .eoi = ack_x2apic_level,
2555#ifdef CONFIG_SMP
2556 .set_affinity = set_ir_ioapic_affinity_irq,
2557#endif
2558 .retrigger = ioapic_retrigger_irq,
2559};
2560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562static inline void init_IO_APIC_traps(void)
2563{
2564 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002565 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002566 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 /*
2569 * NOTE! The local APIC isn't very good at handling
2570 * multiple interrupts at the same interrupt level.
2571 * As the interrupt level is determined by taking the
2572 * vector number and shifting that right by 4, we
2573 * want to spread these out a bit so that they don't
2574 * all fall in the same interrupt level.
2575 *
2576 * Also, we've got to be careful not to trash gate
2577 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2578 */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07002579 for_each_irq_cfg(irq, cfg) {
Yinghai Luda51a822008-08-19 20:50:25 -07002580 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /*
2582 * Hmm.. We don't have an entry for this,
2583 * so default to an old-fashioned 8259
2584 * interrupt if we can..
2585 */
2586 if (irq < 16)
2587 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002588 else {
2589 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002591 desc->chip = &no_irq_chip;
2592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
2594 }
2595}
2596
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002597/*
2598 * The local APIC irq-chip implementation:
2599 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002601static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
2603 unsigned long v;
2604
2605 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002606 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002609static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002611 unsigned long v;
2612
2613 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002614 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615}
2616
Ingo Molnar54168ed2008-08-20 09:07:45 +02002617static void ack_lapic_irq (unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002618{
2619 ack_APIC_irq();
2620}
2621
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002622static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002623 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002624 .mask = mask_lapic_irq,
2625 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002626 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627};
2628
Yinghai Lu497c9a12008-08-19 20:50:28 -07002629static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002630{
Yinghai Lu08678b02008-08-19 20:50:05 -07002631 struct irq_desc *desc;
2632
2633 desc = irq_to_desc(irq);
2634 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002635 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2636 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002637}
2638
Jan Beuliche9427102008-01-30 13:31:24 +01002639static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640{
2641 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002642 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 * We put the 8259A master into AEOI mode and
2644 * unmask on all local APICs LVT0 as NMI.
2645 *
2646 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2647 * is from Maciej W. Rozycki - so we do not have to EOI from
2648 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002649 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2651
Jan Beuliche9427102008-01-30 13:31:24 +01002652 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 apic_printk(APIC_VERBOSE, " done.\n");
2655}
2656
2657/*
2658 * This looks a bit hackish but it's about the only one way of sending
2659 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2660 * not support the ExtINT mode, unfortunately. We need to send these
2661 * cycles as some i82489DX-based boards have glue logic that keeps the
2662 * 8259A interrupt line asserted until INTA. --macro
2663 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002664static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002666 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 struct IO_APIC_route_entry entry0, entry1;
2668 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002670 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002671 if (pin == -1) {
2672 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002674 }
2675 apic = find_isa_irq_apic(8, mp_INT);
2676 if (apic == -1) {
2677 WARN_ON_ONCE(1);
2678 return;
2679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Andi Kleencf4c6a22006-09-26 10:52:30 +02002681 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002682 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 memset(&entry1, 0, sizeof(entry1));
2685
2686 entry1.dest_mode = 0; /* physical delivery */
2687 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002688 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 entry1.delivery_mode = dest_ExtINT;
2690 entry1.polarity = entry0.polarity;
2691 entry1.trigger = 0;
2692 entry1.vector = 0;
2693
Andi Kleencf4c6a22006-09-26 10:52:30 +02002694 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 save_control = CMOS_READ(RTC_CONTROL);
2697 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2698 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2699 RTC_FREQ_SELECT);
2700 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2701
2702 i = 100;
2703 while (i-- > 0) {
2704 mdelay(10);
2705 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2706 i -= 10;
2707 }
2708
2709 CMOS_WRITE(save_control, RTC_CONTROL);
2710 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002711 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Andi Kleencf4c6a22006-09-26 10:52:30 +02002713 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Yinghai Luefa25592008-08-19 20:50:36 -07002716static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002717/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002718static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002719{
2720 disable_timer_pin_1 = 1;
2721 return 0;
2722}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002723early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002724
2725int timer_through_8259 __initdata;
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727/*
2728 * This code may look a bit paranoid, but it's supposed to cooperate with
2729 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2730 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2731 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002732 *
2733 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002735static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002737 struct irq_cfg *cfg = irq_cfg(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002738 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002739 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002740 unsigned int ver;
2741 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002742
2743 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002744
Ingo Molnar54168ed2008-08-20 09:07:45 +02002745 ver = apic_read(APIC_LVR);
2746 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 /*
2749 * get/set the timer IRQ vector:
2750 */
2751 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002752 assign_irq_vector(0, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002755 * As IRQ0 is to be enabled in the 8259A, the virtual
2756 * wire has to be disabled in the local APIC. Also
2757 * timer interrupts need to be acknowledged manually in
2758 * the 8259A for the i82489DX when using the NMI
2759 * watchdog as that APIC treats NMIs as level-triggered.
2760 * The AEOI mode will finish them in the 8259A
2761 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002763 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002765#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002766 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002767#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002769 pin1 = find_isa_irq_pin(0, mp_INT);
2770 apic1 = find_isa_irq_apic(0, mp_INT);
2771 pin2 = ioapic_i8259.pin;
2772 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002774 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2775 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002776 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002778 /*
2779 * Some BIOS writers are clueless and report the ExtINTA
2780 * I/O APIC input from the cascaded 8259A as the timer
2781 * interrupt input. So just in case, if only one pin
2782 * was found above, try it both directly and through the
2783 * 8259A.
2784 */
2785 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002786#ifdef CONFIG_INTR_REMAP
2787 if (intr_remapping_enabled)
2788 panic("BIOS bug: timer not connected to IO-APIC");
2789#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002790 pin1 = pin2;
2791 apic1 = apic2;
2792 no_pin1 = 1;
2793 } else if (pin2 == -1) {
2794 pin2 = pin1;
2795 apic2 = apic1;
2796 }
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (pin1 != -1) {
2799 /*
2800 * Ok, does IRQ0 through the IOAPIC work?
2801 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002802 if (no_pin1) {
2803 add_pin_to_irq(0, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002804 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 unmask_IO_APIC_irq(0);
2807 if (timer_irq_works()) {
2808 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 setup_nmi();
2810 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002812 if (disable_timer_pin_1 > 0)
2813 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002816#ifdef CONFIG_INTR_REMAP
2817 if (intr_remapping_enabled)
2818 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2819#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002820 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002821 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002822 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2823 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002825 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2826 "(IRQ0) through the 8259A ...\n");
2827 apic_printk(APIC_QUIET, KERN_INFO
2828 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /*
2830 * legacy devices should be connected to IO APIC #0
2831 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002832 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002833 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002834 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002835 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002837 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002838 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002840 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002842 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846 /*
2847 * Cleanup, just in case ...
2848 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002849 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002850 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002851 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002855 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2856 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002857 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002859#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002860 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002863 apic_printk(APIC_QUIET, KERN_INFO
2864 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Yinghai Lu497c9a12008-08-19 20:50:28 -07002866 lapic_register_intr(0);
2867 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 enable_8259A_irq(0);
2869
2870 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002871 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002874 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002875 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002876 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002878 apic_printk(APIC_QUIET, KERN_INFO
2879 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 init_8259A(0);
2882 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002883 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 unlock_ExtINT_logic();
2886
2887 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002888 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002889 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002891 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002893 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002894out:
2895 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
2898/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002899 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2900 * to devices. However there may be an I/O APIC pin available for
2901 * this interrupt regardless. The pin may be left unconnected, but
2902 * typically it will be reused as an ExtINT cascade interrupt for
2903 * the master 8259A. In the MPS case such a pin will normally be
2904 * reported as an ExtINT interrupt in the MP table. With ACPI
2905 * there is no provision for ExtINT interrupts, and in the absence
2906 * of an override it would be treated as an ordinary ISA I/O APIC
2907 * interrupt, that is edge-triggered and unmasked by default. We
2908 * used to do this, but it caused problems on some systems because
2909 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2910 * the same ExtINT cascade interrupt to drive the local APIC of the
2911 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2912 * the I/O APIC in all cases now. No actual device should request
2913 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 */
2915#define PIC_IRQS (1 << PIC_CASCADE_IR)
2916
2917void __init setup_IO_APIC(void)
2918{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002919
2920#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002922#else
2923 /*
2924 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2925 */
2926#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002928 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Ingo Molnar54168ed2008-08-20 09:07:45 +02002930 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2931 /*
2932 * Set up IO-APIC IRQ routing.
2933 */
2934#ifdef CONFIG_X86_32
2935 if (!acpi_ioapic)
2936 setup_ioapic_ids_from_mpc();
2937#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 sync_Arb_IDs();
2939 setup_IO_APIC_irqs();
2940 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002941 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
2944/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002945 * Called after all the initialization is done. If we didnt find any
2946 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949static int __init io_apic_bug_finalize(void)
2950{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002951 if (sis_apic_bug == -1)
2952 sis_apic_bug = 0;
2953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
2955
2956late_initcall(io_apic_bug_finalize);
2957
2958struct sysfs_ioapic_data {
2959 struct sys_device dev;
2960 struct IO_APIC_route_entry entry[0];
2961};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002962static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Pavel Machek438510f2005-04-16 15:25:24 -07002964static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 struct IO_APIC_route_entry *entry;
2967 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 data = container_of(dev, struct sysfs_ioapic_data, dev);
2971 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002972 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2973 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 return 0;
2976}
2977
2978static int ioapic_resume(struct sys_device *dev)
2979{
2980 struct IO_APIC_route_entry *entry;
2981 struct sysfs_ioapic_data *data;
2982 unsigned long flags;
2983 union IO_APIC_reg_00 reg_00;
2984 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 data = container_of(dev, struct sysfs_ioapic_data, dev);
2987 entry = data->entry;
2988
2989 spin_lock_irqsave(&ioapic_lock, flags);
2990 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002991 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2992 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 io_apic_write(dev->id, 0, reg_00.raw);
2994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002996 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002997 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 return 0;
3000}
3001
3002static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003003 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 .suspend = ioapic_suspend,
3005 .resume = ioapic_resume,
3006};
3007
3008static int __init ioapic_init_sysfs(void)
3009{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003010 struct sys_device * dev;
3011 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 error = sysdev_class_register(&ioapic_sysdev_class);
3014 if (error)
3015 return error;
3016
Ingo Molnar54168ed2008-08-20 09:07:45 +02003017 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003018 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003020 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (!mp_ioapic_data[i]) {
3022 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3023 continue;
3024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003026 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 dev->cls = &ioapic_sysdev_class;
3028 error = sysdev_register(dev);
3029 if (error) {
3030 kfree(mp_ioapic_data[i]);
3031 mp_ioapic_data[i] = NULL;
3032 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3033 continue;
3034 }
3035 }
3036
3037 return 0;
3038}
3039
3040device_initcall(ioapic_init_sysfs);
3041
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003042/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003043 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003044 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003045unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003046{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003047 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003048 unsigned int irq;
3049 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003050 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07003051 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003052
Yinghai Lu497c9a12008-08-19 20:50:28 -07003053#ifndef CONFIG_HAVE_SPARSE_IRQ
Yinghai Lu199751d2008-08-19 20:50:27 -07003054 irq_want = nr_irqs - 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003055#endif
Yinghai Lu199751d2008-08-19 20:50:27 -07003056
3057 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003058 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003059 for (new = irq_want; new > 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003060 if (platform_legacy_irq(new))
3061 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07003062 cfg_new = irq_cfg(new);
3063 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003064 continue;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003065 /* check if need to create one */
Yinghai Luda51a822008-08-19 20:50:25 -07003066 if (!cfg_new)
3067 cfg_new = irq_cfg_alloc(new);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003068 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003069 irq = new;
3070 break;
3071 }
3072 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003073
Yinghai Lu199751d2008-08-19 20:50:27 -07003074 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003075 dynamic_irq_init(irq);
3076 }
3077 return irq;
3078}
3079
Yinghai Lu199751d2008-08-19 20:50:27 -07003080int create_irq(void)
3081{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003082 int irq;
3083
3084 irq = create_irq_nr(nr_irqs - 1);
3085
3086 if (irq == 0)
3087 irq = -1;
3088
3089 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003090}
3091
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003092void destroy_irq(unsigned int irq)
3093{
3094 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003095
3096 dynamic_irq_cleanup(irq);
3097
Ingo Molnar54168ed2008-08-20 09:07:45 +02003098#ifdef CONFIG_INTR_REMAP
3099 free_irte(irq);
3100#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003101 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003102 __clear_irq_vector(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003103 spin_unlock_irqrestore(&vector_lock, flags);
3104}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003105
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003106/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003107 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003108 */
3109#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003110static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003111{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003112 struct irq_cfg *cfg;
3113 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003114 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003115 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003116
Yinghai Lu497c9a12008-08-19 20:50:28 -07003117 tmp = TARGET_CPUS;
3118 err = assign_irq_vector(irq, tmp);
3119 if (err)
3120 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003121
Yinghai Lu497c9a12008-08-19 20:50:28 -07003122 cfg = irq_cfg(irq);
3123 cpus_and(tmp, cfg->domain, tmp);
3124 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003125
Ingo Molnar54168ed2008-08-20 09:07:45 +02003126#ifdef CONFIG_INTR_REMAP
3127 if (irq_remapped(irq)) {
3128 struct irte irte;
3129 int ir_index;
3130 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003131
Ingo Molnar54168ed2008-08-20 09:07:45 +02003132 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3133 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003134
Ingo Molnar54168ed2008-08-20 09:07:45 +02003135 memset (&irte, 0, sizeof(irte));
3136
3137 irte.present = 1;
3138 irte.dst_mode = INT_DEST_MODE;
3139 irte.trigger_mode = 0; /* edge */
3140 irte.dlvry_mode = INT_DELIVERY_MODE;
3141 irte.vector = cfg->vector;
3142 irte.dest_id = IRTE_DEST(dest);
3143
3144 modify_irte(irq, &irte);
3145
3146 msg->address_hi = MSI_ADDR_BASE_HI;
3147 msg->data = sub_handle;
3148 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3149 MSI_ADDR_IR_SHV |
3150 MSI_ADDR_IR_INDEX1(ir_index) |
3151 MSI_ADDR_IR_INDEX2(ir_index);
3152 } else
3153#endif
3154 {
3155 msg->address_hi = MSI_ADDR_BASE_HI;
3156 msg->address_lo =
3157 MSI_ADDR_BASE_LO |
3158 ((INT_DEST_MODE == 0) ?
3159 MSI_ADDR_DEST_MODE_PHYSICAL:
3160 MSI_ADDR_DEST_MODE_LOGICAL) |
3161 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3162 MSI_ADDR_REDIRECTION_CPU:
3163 MSI_ADDR_REDIRECTION_LOWPRI) |
3164 MSI_ADDR_DEST_ID(dest);
3165
3166 msg->data =
3167 MSI_DATA_TRIGGER_EDGE |
3168 MSI_DATA_LEVEL_ASSERT |
3169 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3170 MSI_DATA_DELIVERY_FIXED:
3171 MSI_DATA_DELIVERY_LOWPRI) |
3172 MSI_DATA_VECTOR(cfg->vector);
3173 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003174 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003175}
3176
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003177#ifdef CONFIG_SMP
3178static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3179{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003180 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003181 struct msi_msg msg;
3182 unsigned int dest;
3183 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003184 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003185
3186 cpus_and(tmp, mask, cpu_online_map);
3187 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003188 return;
3189
Yinghai Lu497c9a12008-08-19 20:50:28 -07003190 if (assign_irq_vector(irq, mask))
3191 return;
3192
3193 cfg = irq_cfg(irq);
3194 cpus_and(tmp, cfg->domain, mask);
3195 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003196
3197 read_msi_msg(irq, &msg);
3198
3199 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003200 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003201 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3202 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3203
3204 write_msi_msg(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003205 desc = irq_to_desc(irq);
3206 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003207}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003208
3209#ifdef CONFIG_INTR_REMAP
3210/*
3211 * Migrate the MSI irq to another cpumask. This migration is
3212 * done in the process context using interrupt-remapping hardware.
3213 */
3214static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3215{
3216 struct irq_cfg *cfg;
3217 unsigned int dest;
3218 cpumask_t tmp, cleanup_mask;
3219 struct irte irte;
3220 struct irq_desc *desc;
3221
3222 cpus_and(tmp, mask, cpu_online_map);
3223 if (cpus_empty(tmp))
3224 return;
3225
3226 if (get_irte(irq, &irte))
3227 return;
3228
3229 if (assign_irq_vector(irq, mask))
3230 return;
3231
3232 cfg = irq_cfg(irq);
3233 cpus_and(tmp, cfg->domain, mask);
3234 dest = cpu_mask_to_apicid(tmp);
3235
3236 irte.vector = cfg->vector;
3237 irte.dest_id = IRTE_DEST(dest);
3238
3239 /*
3240 * atomically update the IRTE with the new destination and vector.
3241 */
3242 modify_irte(irq, &irte);
3243
3244 /*
3245 * After this point, all the interrupts will start arriving
3246 * at the new destination. So, time to cleanup the previous
3247 * vector allocation.
3248 */
3249 if (cfg->move_in_progress) {
3250 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3251 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3252 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3253 cfg->move_in_progress = 0;
3254 }
3255
3256 desc = irq_to_desc(irq);
3257 desc->affinity = mask;
3258}
3259#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003260#endif /* CONFIG_SMP */
3261
3262/*
3263 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3264 * which implement the MSI or MSI-X Capability Structure.
3265 */
3266static struct irq_chip msi_chip = {
3267 .name = "PCI-MSI",
3268 .unmask = unmask_msi_irq,
3269 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003270 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003271#ifdef CONFIG_SMP
3272 .set_affinity = set_msi_irq_affinity,
3273#endif
3274 .retrigger = ioapic_retrigger_irq,
3275};
3276
Ingo Molnar54168ed2008-08-20 09:07:45 +02003277#ifdef CONFIG_INTR_REMAP
3278static struct irq_chip msi_ir_chip = {
3279 .name = "IR-PCI-MSI",
3280 .unmask = unmask_msi_irq,
3281 .mask = mask_msi_irq,
3282 .ack = ack_x2apic_edge,
3283#ifdef CONFIG_SMP
3284 .set_affinity = ir_set_msi_irq_affinity,
3285#endif
3286 .retrigger = ioapic_retrigger_irq,
3287};
3288
3289/*
3290 * Map the PCI dev to the corresponding remapping hardware unit
3291 * and allocate 'nvec' consecutive interrupt-remapping table entries
3292 * in it.
3293 */
3294static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3295{
3296 struct intel_iommu *iommu;
3297 int index;
3298
3299 iommu = map_dev_to_ir(dev);
3300 if (!iommu) {
3301 printk(KERN_ERR
3302 "Unable to map PCI %s to iommu\n", pci_name(dev));
3303 return -ENOENT;
3304 }
3305
3306 index = alloc_irte(iommu, irq, nvec);
3307 if (index < 0) {
3308 printk(KERN_ERR
3309 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3310 pci_name(dev));
3311 return -ENOSPC;
3312 }
3313 return index;
3314}
3315#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003316
3317static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3318{
3319 int ret;
3320 struct msi_msg msg;
3321
3322 ret = msi_compose_msg(dev, irq, &msg);
3323 if (ret < 0)
3324 return ret;
3325
3326 set_irq_msi(irq, desc);
3327 write_msi_msg(irq, &msg);
3328
Ingo Molnar54168ed2008-08-20 09:07:45 +02003329#ifdef CONFIG_INTR_REMAP
3330 if (irq_remapped(irq)) {
3331 struct irq_desc *desc = irq_to_desc(irq);
3332 /*
3333 * irq migration in process context
3334 */
3335 desc->status |= IRQ_MOVE_PCNTXT;
3336 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3337 } else
3338#endif
3339 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003340
3341 return 0;
3342}
3343
Yinghai Lu199751d2008-08-19 20:50:27 -07003344static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3345{
3346 unsigned int irq;
3347
3348 irq = dev->bus->number;
3349 irq <<= 8;
3350 irq |= dev->devfn;
3351 irq <<= 12;
3352
3353 return irq;
3354}
3355
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003356int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003357{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003358 unsigned int irq;
3359 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003360 unsigned int irq_want;
3361
3362 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3363
3364 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003365 if (irq == 0)
3366 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003367
Ingo Molnar54168ed2008-08-20 09:07:45 +02003368#ifdef CONFIG_INTR_REMAP
3369 if (!intr_remapping_enabled)
3370 goto no_ir;
3371
3372 ret = msi_alloc_irte(dev, irq, 1);
3373 if (ret < 0)
3374 goto error;
3375no_ir:
3376#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003377 ret = setup_msi_irq(dev, desc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003378 if (ret < 0) {
3379 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003380 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003381 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003382 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003383
3384#ifdef CONFIG_INTR_REMAP
3385error:
3386 destroy_irq(irq);
3387 return ret;
3388#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003389}
3390
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003391int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3392{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003393 unsigned int irq;
3394 int ret, sub_handle;
3395 struct msi_desc *desc;
3396 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003397
Ingo Molnar54168ed2008-08-20 09:07:45 +02003398#ifdef CONFIG_INTR_REMAP
3399 struct intel_iommu *iommu = 0;
3400 int index = 0;
3401#endif
3402
3403 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3404 sub_handle = 0;
3405 list_for_each_entry(desc, &dev->msi_list, list) {
3406 irq = create_irq_nr(irq_want--);
3407 if (irq == 0)
3408 return -1;
3409#ifdef CONFIG_INTR_REMAP
3410 if (!intr_remapping_enabled)
3411 goto no_ir;
3412
3413 if (!sub_handle) {
3414 /*
3415 * allocate the consecutive block of IRTE's
3416 * for 'nvec'
3417 */
3418 index = msi_alloc_irte(dev, irq, nvec);
3419 if (index < 0) {
3420 ret = index;
3421 goto error;
3422 }
3423 } else {
3424 iommu = map_dev_to_ir(dev);
3425 if (!iommu) {
3426 ret = -ENOENT;
3427 goto error;
3428 }
3429 /*
3430 * setup the mapping between the irq and the IRTE
3431 * base index, the sub_handle pointing to the
3432 * appropriate interrupt remap table entry.
3433 */
3434 set_irte_irq(irq, iommu, index, sub_handle);
3435 }
3436no_ir:
3437#endif
3438 ret = setup_msi_irq(dev, desc, irq);
3439 if (ret < 0)
3440 goto error;
3441 sub_handle++;
3442 }
3443 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003444
3445error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003446 destroy_irq(irq);
3447 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003448}
3449
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003450void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003451{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003452 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003453}
3454
Ingo Molnar54168ed2008-08-20 09:07:45 +02003455#ifdef CONFIG_DMAR
3456#ifdef CONFIG_SMP
3457static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3458{
3459 struct irq_cfg *cfg;
3460 struct msi_msg msg;
3461 unsigned int dest;
3462 cpumask_t tmp;
3463 struct irq_desc *desc;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003464
Ingo Molnar54168ed2008-08-20 09:07:45 +02003465 cpus_and(tmp, mask, cpu_online_map);
3466 if (cpus_empty(tmp))
3467 return;
3468
3469 if (assign_irq_vector(irq, mask))
3470 return;
3471
3472 cfg = irq_cfg(irq);
3473 cpus_and(tmp, cfg->domain, mask);
3474 dest = cpu_mask_to_apicid(tmp);
3475
3476 dmar_msi_read(irq, &msg);
3477
3478 msg.data &= ~MSI_DATA_VECTOR_MASK;
3479 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3480 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3481 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3482
3483 dmar_msi_write(irq, &msg);
3484 desc = irq_to_desc(irq);
3485 desc->affinity = mask;
3486}
3487#endif /* CONFIG_SMP */
3488
3489struct irq_chip dmar_msi_type = {
3490 .name = "DMAR_MSI",
3491 .unmask = dmar_msi_unmask,
3492 .mask = dmar_msi_mask,
3493 .ack = ack_apic_edge,
3494#ifdef CONFIG_SMP
3495 .set_affinity = dmar_msi_set_affinity,
3496#endif
3497 .retrigger = ioapic_retrigger_irq,
3498};
3499
3500int arch_setup_dmar_msi(unsigned int irq)
3501{
3502 int ret;
3503 struct msi_msg msg;
3504
3505 ret = msi_compose_msg(NULL, irq, &msg);
3506 if (ret < 0)
3507 return ret;
3508 dmar_msi_write(irq, &msg);
3509 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3510 "edge");
3511 return 0;
3512}
3513#endif
3514
3515#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003516/*
3517 * Hypertransport interrupt support
3518 */
3519#ifdef CONFIG_HT_IRQ
3520
3521#ifdef CONFIG_SMP
3522
Yinghai Lu497c9a12008-08-19 20:50:28 -07003523static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003524{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003525 struct ht_irq_msg msg;
3526 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003527
Yinghai Lu497c9a12008-08-19 20:50:28 -07003528 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003529 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003530
Yinghai Lu497c9a12008-08-19 20:50:28 -07003531 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003532 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003533
Eric W. Biedermanec683072006-11-08 17:44:57 -08003534 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003535}
3536
3537static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3538{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003539 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003540 unsigned int dest;
3541 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003542 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003543
3544 cpus_and(tmp, mask, cpu_online_map);
3545 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003546 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003547
Yinghai Lu497c9a12008-08-19 20:50:28 -07003548 if (assign_irq_vector(irq, mask))
3549 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003550
Yinghai Lu497c9a12008-08-19 20:50:28 -07003551 cfg = irq_cfg(irq);
3552 cpus_and(tmp, cfg->domain, mask);
3553 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003554
Yinghai Lu497c9a12008-08-19 20:50:28 -07003555 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003556 desc = irq_to_desc(irq);
3557 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003558}
3559#endif
3560
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003561static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003562 .name = "PCI-HT",
3563 .mask = mask_ht_irq,
3564 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003565 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003566#ifdef CONFIG_SMP
3567 .set_affinity = set_ht_irq_affinity,
3568#endif
3569 .retrigger = ioapic_retrigger_irq,
3570};
3571
3572int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3573{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003574 struct irq_cfg *cfg;
3575 int err;
3576 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003577
Yinghai Lu497c9a12008-08-19 20:50:28 -07003578 tmp = TARGET_CPUS;
3579 err = assign_irq_vector(irq, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003580 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003581 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003582 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003583
Yinghai Lu497c9a12008-08-19 20:50:28 -07003584 cfg = irq_cfg(irq);
3585 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003586 dest = cpu_mask_to_apicid(tmp);
3587
Eric W. Biedermanec683072006-11-08 17:44:57 -08003588 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003589
Eric W. Biedermanec683072006-11-08 17:44:57 -08003590 msg.address_lo =
3591 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003592 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003593 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003594 ((INT_DEST_MODE == 0) ?
3595 HT_IRQ_LOW_DM_PHYSICAL :
3596 HT_IRQ_LOW_DM_LOGICAL) |
3597 HT_IRQ_LOW_RQEOI_EDGE |
3598 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3599 HT_IRQ_LOW_MT_FIXED :
3600 HT_IRQ_LOW_MT_ARBITRATED) |
3601 HT_IRQ_LOW_IRQ_MASKED;
3602
Eric W. Biedermanec683072006-11-08 17:44:57 -08003603 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003604
Ingo Molnara460e742006-10-17 00:10:03 -07003605 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3606 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003607 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003608 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003609}
3610#endif /* CONFIG_HT_IRQ */
3611
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003612int __init io_apic_get_redir_entries (int ioapic)
3613{
3614 union IO_APIC_reg_01 reg_01;
3615 unsigned long flags;
3616
3617 spin_lock_irqsave(&ioapic_lock, flags);
3618 reg_01.raw = io_apic_read(ioapic, 1);
3619 spin_unlock_irqrestore(&ioapic_lock, flags);
3620
3621 return reg_01.bits.entries;
3622}
3623
3624int __init probe_nr_irqs(void)
3625{
3626 int idx;
3627 int nr = 0;
3628
3629 for (idx = 0; idx < nr_ioapics; idx++)
3630 nr += io_apic_get_redir_entries(idx);
3631
3632 /* double it for hotplug and msi and nmi */
3633 nr <<= 1;
3634
3635 /* something wrong ? */
3636 if (nr < 32)
3637 nr = 32;
3638
3639 return nr;
3640}
3641
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003643 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 -------------------------------------------------------------------------- */
3645
Len Brown888ba6c2005-08-24 12:07:20 -04003646#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647
Ingo Molnar54168ed2008-08-20 09:07:45 +02003648#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003649int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
3651 union IO_APIC_reg_00 reg_00;
3652 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3653 physid_mask_t tmp;
3654 unsigned long flags;
3655 int i = 0;
3656
3657 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003658 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3659 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003661 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3663 * advantage of new APIC bus architecture.
3664 */
3665
3666 if (physids_empty(apic_id_map))
3667 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3668
3669 spin_lock_irqsave(&ioapic_lock, flags);
3670 reg_00.raw = io_apic_read(ioapic, 0);
3671 spin_unlock_irqrestore(&ioapic_lock, flags);
3672
3673 if (apic_id >= get_physical_broadcast()) {
3674 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3675 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3676 apic_id = reg_00.bits.ID;
3677 }
3678
3679 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003680 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 * 'stuck on smp_invalidate_needed IPI wait' messages.
3682 */
3683 if (check_apicid_used(apic_id_map, apic_id)) {
3684
3685 for (i = 0; i < get_physical_broadcast(); i++) {
3686 if (!check_apicid_used(apic_id_map, i))
3687 break;
3688 }
3689
3690 if (i == get_physical_broadcast())
3691 panic("Max apic_id exceeded!\n");
3692
3693 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3694 "trying %d\n", ioapic, apic_id, i);
3695
3696 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698
3699 tmp = apicid_to_cpu_present(apic_id);
3700 physids_or(apic_id_map, apic_id_map, tmp);
3701
3702 if (reg_00.bits.ID != apic_id) {
3703 reg_00.bits.ID = apic_id;
3704
3705 spin_lock_irqsave(&ioapic_lock, flags);
3706 io_apic_write(ioapic, 0, reg_00.raw);
3707 reg_00.raw = io_apic_read(ioapic, 0);
3708 spin_unlock_irqrestore(&ioapic_lock, flags);
3709
3710 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003711 if (reg_00.bits.ID != apic_id) {
3712 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3713 return -1;
3714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 }
3716
3717 apic_printk(APIC_VERBOSE, KERN_INFO
3718 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3719
3720 return apic_id;
3721}
3722
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003723int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
3725 union IO_APIC_reg_01 reg_01;
3726 unsigned long flags;
3727
3728 spin_lock_irqsave(&ioapic_lock, flags);
3729 reg_01.raw = io_apic_read(ioapic, 1);
3730 spin_unlock_irqrestore(&ioapic_lock, flags);
3731
3732 return reg_01.bits.version;
3733}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Ingo Molnar54168ed2008-08-20 09:07:45 +02003736int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003739 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 ioapic);
3741 return -EINVAL;
3742 }
3743
3744 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 * IRQs < 16 are already in the irq_2_pin[] map
3746 */
3747 if (irq >= 16)
3748 add_pin_to_irq(irq, ioapic, pin);
3749
Yinghai Lu497c9a12008-08-19 20:50:28 -07003750 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751
3752 return 0;
3753}
3754
Ingo Molnar54168ed2008-08-20 09:07:45 +02003755
Shaohua Li61fd47e2007-11-17 01:05:28 -05003756int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3757{
3758 int i;
3759
3760 if (skip_ioapic_setup)
3761 return -1;
3762
3763 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003764 if (mp_irqs[i].mp_irqtype == mp_INT &&
3765 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003766 break;
3767 if (i >= mp_irq_entries)
3768 return -1;
3769
3770 *trigger = irq_trigger(i);
3771 *polarity = irq_polarity(i);
3772 return 0;
3773}
3774
Len Brown888ba6c2005-08-24 12:07:20 -04003775#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003776
Yinghai Lu497c9a12008-08-19 20:50:28 -07003777/*
3778 * This function currently is only a helper for the i386 smp boot process where
3779 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3780 * so mask in all cases should simply be TARGET_CPUS
3781 */
3782#ifdef CONFIG_SMP
3783void __init setup_ioapic_dest(void)
3784{
3785 int pin, ioapic, irq, irq_entry;
3786 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003787
3788 if (skip_ioapic_setup == 1)
3789 return;
3790
3791 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3792 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3793 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3794 if (irq_entry == -1)
3795 continue;
3796 irq = pin_2_irq(irq_entry, ioapic, pin);
3797
3798 /* setup_IO_APIC_irqs could fail to get vector for some device
3799 * when you have too many devices, because at that time only boot
3800 * cpu is online.
3801 */
3802 cfg = irq_cfg(irq);
3803 if (!cfg->vector)
3804 setup_IO_APIC_irq(ioapic, pin, irq,
3805 irq_trigger(irq_entry),
3806 irq_polarity(irq_entry));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003807#ifdef CONFIG_INTR_REMAP
3808 else if (intr_remapping_enabled)
3809 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
3810#endif
3811 else
Yinghai Lu497c9a12008-08-19 20:50:28 -07003812 set_ioapic_affinity_irq(irq, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003813 }
3814
3815 }
3816}
3817#endif
3818
Ingo Molnar54168ed2008-08-20 09:07:45 +02003819#define IOAPIC_RESOURCE_NAME_SIZE 11
3820
3821static struct resource *ioapic_resources;
3822
3823static struct resource * __init ioapic_setup_resources(void)
3824{
3825 unsigned long n;
3826 struct resource *res;
3827 char *mem;
3828 int i;
3829
3830 if (nr_ioapics <= 0)
3831 return NULL;
3832
3833 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3834 n *= nr_ioapics;
3835
3836 mem = alloc_bootmem(n);
3837 res = (void *)mem;
3838
3839 if (mem != NULL) {
3840 mem += sizeof(struct resource) * nr_ioapics;
3841
3842 for (i = 0; i < nr_ioapics; i++) {
3843 res[i].name = mem;
3844 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3845 sprintf(mem, "IOAPIC %u", i);
3846 mem += IOAPIC_RESOURCE_NAME_SIZE;
3847 }
3848 }
3849
3850 ioapic_resources = res;
3851
3852 return res;
3853}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003854
Yinghai Luf3294a32008-06-27 01:41:56 -07003855void __init ioapic_init_mappings(void)
3856{
3857 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3858 int i;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003859 struct resource *ioapic_res;
Yinghai Luf3294a32008-06-27 01:41:56 -07003860
Ingo Molnar54168ed2008-08-20 09:07:45 +02003861 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003862 for (i = 0; i < nr_ioapics; i++) {
3863 if (smp_found_config) {
3864 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003865#ifdef CONFIG_X86_32
3866 if (!ioapic_phys) {
3867 printk(KERN_ERR
3868 "WARNING: bogus zero IO-APIC "
3869 "address found in MPTABLE, "
3870 "disabling IO/APIC support!\n");
3871 smp_found_config = 0;
3872 skip_ioapic_setup = 1;
3873 goto fake_ioapic_page;
3874 }
3875#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003876 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003877#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003878fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003879#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003880 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003881 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07003882 ioapic_phys = __pa(ioapic_phys);
3883 }
3884 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003885 apic_printk(APIC_VERBOSE,
3886 "mapped IOAPIC to %08lx (%08lx)\n",
3887 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07003888 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003889
Ingo Molnar54168ed2008-08-20 09:07:45 +02003890 if (ioapic_res != NULL) {
3891 ioapic_res->start = ioapic_phys;
3892 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3893 ioapic_res++;
3894 }
Yinghai Luf3294a32008-06-27 01:41:56 -07003895 }
3896}
3897
Ingo Molnar54168ed2008-08-20 09:07:45 +02003898static int __init ioapic_insert_resources(void)
3899{
3900 int i;
3901 struct resource *r = ioapic_resources;
3902
3903 if (!r) {
3904 printk(KERN_ERR
3905 "IO APIC resources could be not be allocated.\n");
3906 return -1;
3907 }
3908
3909 for (i = 0; i < nr_ioapics; i++) {
3910 insert_resource(&iomem_resource, r);
3911 r++;
3912 }
3913
3914 return 0;
3915}
3916
3917/* Insert the IO APIC resources after PCI initialization has occured to handle
3918 * IO APICS that are mapped in on a BAR in PCI space. */
3919late_initcall(ioapic_insert_resources);