blob: d22fecf828b872eaed99626d1cd9fb7b5d1fe2c5 [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
Cyrill Gorcunovac54a6c2008-09-04 22:37:50 +0400150#ifdef CONFIG_HAVE_SPARSE_IRQ
Yinghai Lue89eb432008-08-20 20:46:25 -0700151/*
152 * Protect the irq_cfgx_free freelist:
153 */
154static DEFINE_SPINLOCK(irq_cfg_lock);
155
Yinghai Luda51a822008-08-19 20:50:25 -0700156static struct irq_cfg *irq_cfgx_free;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700157#endif
Cyrill Gorcunovac54a6c2008-09-04 22:37:50 +0400158
Yinghai Luda51a822008-08-19 20:50:25 -0700159static void __init init_work(void *data)
160{
161 struct dyn_array *da = data;
162 struct irq_cfg *cfg;
163 int legacy_count;
164 int i;
165
166 cfg = *da->name;
167
168 memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
169
Cyrill Gorcunov676f4a92008-09-04 22:37:49 +0400170 legacy_count = ARRAY_SIZE(irq_cfg_legacy);
Yinghai Luda51a822008-08-19 20:50:25 -0700171 for (i = legacy_count; i < *da->nr; i++)
172 init_one_irq_cfg(&cfg[i]);
173
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700174#ifdef CONFIG_HAVE_SPARSE_IRQ
Yinghai Luda51a822008-08-19 20:50:25 -0700175 for (i = 1; i < *da->nr; i++)
176 cfg[i-1].next = &cfg[i];
177
178 irq_cfgx_free = &irq_cfgx[legacy_count];
179 irq_cfgx[legacy_count - 1].next = NULL;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700180#endif
Yinghai Luda51a822008-08-19 20:50:25 -0700181}
182
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700183#ifdef CONFIG_HAVE_SPARSE_IRQ
184/* need to be biger than size of irq_cfg_legacy */
185static int nr_irq_cfg = 32;
186
187static int __init parse_nr_irq_cfg(char *arg)
188{
189 if (arg) {
190 nr_irq_cfg = simple_strtoul(arg, NULL, 0);
191 if (nr_irq_cfg < 32)
192 nr_irq_cfg = 32;
193 }
194 return 0;
195}
196
197early_param("nr_irq_cfg", parse_nr_irq_cfg);
198
199#define for_each_irq_cfg(irqX, cfg) \
200 for (cfg = irq_cfgx, irqX = cfg->irq; cfg; cfg = cfg->next, irqX = cfg ? cfg->irq : -1U)
201
Yinghai Luda51a822008-08-19 20:50:25 -0700202
203DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irq_cfg, PAGE_SIZE, init_work);
Yinghai Lua1420f32008-08-19 20:50:24 -0700204
205static struct irq_cfg *irq_cfg(unsigned int irq)
206{
Yinghai Luda51a822008-08-19 20:50:25 -0700207 struct irq_cfg *cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700208
Yinghai Luda51a822008-08-19 20:50:25 -0700209 cfg = irq_cfgx;
210 while (cfg) {
211 if (cfg->irq == irq)
212 return cfg;
213
214 cfg = cfg->next;
215 }
216
217 return NULL;
218}
219
220static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
221{
222 struct irq_cfg *cfg, *cfg_pri;
Yinghai Lue89eb432008-08-20 20:46:25 -0700223 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -0700224 int count = 0;
Yinghai Lue89eb432008-08-20 20:46:25 -0700225 int i;
Yinghai Luda51a822008-08-19 20:50:25 -0700226
227 cfg_pri = cfg = irq_cfgx;
228 while (cfg) {
229 if (cfg->irq == irq)
230 return cfg;
231
232 cfg_pri = cfg;
233 cfg = cfg->next;
234 count++;
235 }
236
Yinghai Lue89eb432008-08-20 20:46:25 -0700237 spin_lock_irqsave(&irq_cfg_lock, flags);
Yinghai Luda51a822008-08-19 20:50:25 -0700238 if (!irq_cfgx_free) {
239 unsigned long phys;
240 unsigned long total_bytes;
241 /*
242 * we run out of pre-allocate ones, allocate more
243 */
244 printk(KERN_DEBUG "try to get more irq_cfg %d\n", nr_irq_cfg);
245
246 total_bytes = sizeof(struct irq_cfg) * nr_irq_cfg;
247 if (after_bootmem)
248 cfg = kzalloc(total_bytes, GFP_ATOMIC);
249 else
250 cfg = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
251
252 if (!cfg)
253 panic("please boot with nr_irq_cfg= %d\n", count * 2);
254
255 phys = __pa(cfg);
256 printk(KERN_DEBUG "irq_irq ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
257
258 for (i = 0; i < nr_irq_cfg; i++)
259 init_one_irq_cfg(&cfg[i]);
260
261 for (i = 1; i < nr_irq_cfg; i++)
262 cfg[i-1].next = &cfg[i];
263
264 irq_cfgx_free = cfg;
265 }
266
267 cfg = irq_cfgx_free;
268 irq_cfgx_free = irq_cfgx_free->next;
269 cfg->next = NULL;
270 if (cfg_pri)
271 cfg_pri->next = cfg;
272 else
273 irq_cfgx = cfg;
274 cfg->irq = irq;
Yinghai Lue89eb432008-08-20 20:46:25 -0700275
276 spin_unlock_irqrestore(&irq_cfg_lock, flags);
277
Yinghai Luda51a822008-08-19 20:50:25 -0700278 printk(KERN_DEBUG "found new irq_cfg for irq %d\n", cfg->irq);
Yinghai Luda51a822008-08-19 20:50:25 -0700279#ifdef CONFIG_HAVE_SPARSE_IRQ_DEBUG
280 {
281 /* dump the results */
282 struct irq_cfg *cfg;
283 unsigned long phys;
284 unsigned long bytes = sizeof(struct irq_cfg);
285
286 printk(KERN_DEBUG "=========================== %d\n", irq);
287 printk(KERN_DEBUG "irq_cfg dump after get that for %d\n", irq);
288 for_each_irq_cfg(cfg) {
289 phys = __pa(cfg);
290 printk(KERN_DEBUG "irq_cfg %d ==> [%#lx - %#lx]\n", cfg->irq, phys, phys + bytes);
291 }
292 printk(KERN_DEBUG "===========================\n");
293 }
294#endif
295 return cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700296}
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700297#else
Yinghai Lua1420f32008-08-19 20:50:24 -0700298
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700299#define for_each_irq_cfg(irq, cfg) \
300 for (irq = 0, cfg = &irq_cfgx[irq]; irq < nr_irqs; irq++, cfg = &irq_cfgx[irq])
301
302DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irqs, PAGE_SIZE, init_work);
303
304struct irq_cfg *irq_cfg(unsigned int irq)
305{
306 if (irq < nr_irqs)
307 return &irq_cfgx[irq];
308
309 return NULL;
310}
311struct irq_cfg *irq_cfg_alloc(unsigned int irq)
312{
313 return irq_cfg(irq);
314}
315
316#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/*
318 * This is performance-critical, we want to do it O(1)
319 *
320 * the indexing order of this array favors 1:1 mappings
321 * between pins and IRQs.
322 */
323
Yinghai Lu0f978f42008-08-19 20:50:26 -0700324struct irq_pin_list {
325 int apic, pin;
326 struct irq_pin_list *next;
327};
Yinghai Lu301e6192008-08-19 20:50:02 -0700328
Yinghai Lu0f978f42008-08-19 20:50:26 -0700329static struct irq_pin_list *irq_2_pin_head;
330/* fill one page ? */
331static int nr_irq_2_pin = 0x100;
332static struct irq_pin_list *irq_2_pin_ptr;
333static void __init irq_2_pin_init_work(void *data)
334{
335 struct dyn_array *da = data;
336 struct irq_pin_list *pin;
337 int i;
338
339 pin = *da->name;
340
341 for (i = 1; i < *da->nr; i++)
342 pin[i-1].next = &pin[i];
343
344 irq_2_pin_ptr = &pin[0];
345}
346DEFINE_DYN_ARRAY(irq_2_pin_head, sizeof(struct irq_pin_list), nr_irq_2_pin, PAGE_SIZE, irq_2_pin_init_work);
347
348static struct irq_pin_list *get_one_free_irq_2_pin(void)
349{
350 struct irq_pin_list *pin;
351 int i;
352
353 pin = irq_2_pin_ptr;
354
355 if (pin) {
356 irq_2_pin_ptr = pin->next;
357 pin->next = NULL;
358 return pin;
359 }
360
361 /*
362 * we run out of pre-allocate ones, allocate more
363 */
364 printk(KERN_DEBUG "try to get more irq_2_pin %d\n", nr_irq_2_pin);
365
366 if (after_bootmem)
367 pin = kzalloc(sizeof(struct irq_pin_list)*nr_irq_2_pin,
368 GFP_ATOMIC);
369 else
370 pin = __alloc_bootmem_nopanic(sizeof(struct irq_pin_list) *
371 nr_irq_2_pin, PAGE_SIZE, 0);
372
373 if (!pin)
374 panic("can not get more irq_2_pin\n");
375
376 for (i = 1; i < nr_irq_2_pin; i++)
377 pin[i-1].next = &pin[i];
378
379 irq_2_pin_ptr = pin->next;
380 pin->next = NULL;
381
382 return pin;
383}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds130fe052006-11-01 09:11:00 -0800385struct io_apic {
386 unsigned int index;
387 unsigned int unused[3];
388 unsigned int data;
389};
390
391static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
392{
393 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400394 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800395}
396
397static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
398{
399 struct io_apic __iomem *io_apic = io_apic_base(apic);
400 writel(reg, &io_apic->index);
401 return readl(&io_apic->data);
402}
403
404static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
405{
406 struct io_apic __iomem *io_apic = io_apic_base(apic);
407 writel(reg, &io_apic->index);
408 writel(value, &io_apic->data);
409}
410
411/*
412 * Re-write a value: to be used for read-modify-write
413 * cycles where the read already set up the index register.
414 *
415 * Older SiS APIC requires we rewrite the index register
416 */
417static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
418{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200419 struct io_apic __iomem *io_apic = io_apic_base(apic);
420 if (sis_apic_bug)
421 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800422 writel(value, &io_apic->data);
423}
424
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700425static bool io_apic_level_ack_pending(unsigned int irq)
426{
427 struct irq_pin_list *entry;
428 unsigned long flags;
429 struct irq_cfg *cfg = irq_cfg(irq);
430
431 spin_lock_irqsave(&ioapic_lock, flags);
432 entry = cfg->irq_2_pin;
433 for (;;) {
434 unsigned int reg;
435 int pin;
436
437 if (!entry)
438 break;
439 pin = entry->pin;
440 reg = io_apic_read(entry->apic, 0x10 + pin*2);
441 /* Is the remote IRR bit set? */
442 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
443 spin_unlock_irqrestore(&ioapic_lock, flags);
444 return true;
445 }
446 if (!entry->next)
447 break;
448 entry = entry->next;
449 }
450 spin_unlock_irqrestore(&ioapic_lock, flags);
451
452 return false;
453}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700454
Andi Kleencf4c6a22006-09-26 10:52:30 +0200455union entry_union {
456 struct { u32 w1, w2; };
457 struct IO_APIC_route_entry entry;
458};
459
460static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
461{
462 union entry_union eu;
463 unsigned long flags;
464 spin_lock_irqsave(&ioapic_lock, flags);
465 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
466 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
467 spin_unlock_irqrestore(&ioapic_lock, flags);
468 return eu.entry;
469}
470
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800471/*
472 * When we write a new IO APIC routing entry, we need to write the high
473 * word first! If the mask bit in the low word is clear, we will enable
474 * the interrupt, and we need to make sure the entry is fully populated
475 * before that happens.
476 */
Andi Kleend15512f2006-12-07 02:14:07 +0100477static void
478__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
479{
480 union entry_union eu;
481 eu.entry = e;
482 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
483 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
484}
485
Andi Kleencf4c6a22006-09-26 10:52:30 +0200486static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
487{
488 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200489 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100490 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800491 spin_unlock_irqrestore(&ioapic_lock, flags);
492}
493
494/*
495 * When we mask an IO APIC routing entry, we need to write the low
496 * word first, in order to set the mask bit before we change the
497 * high bits!
498 */
499static void ioapic_mask_entry(int apic, int pin)
500{
501 unsigned long flags;
502 union entry_union eu = { .entry.mask = 1 };
503
504 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200505 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
506 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
507 spin_unlock_irqrestore(&ioapic_lock, flags);
508}
509
Yinghai Lu497c9a12008-08-19 20:50:28 -0700510#ifdef CONFIG_SMP
511static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
512{
513 int apic, pin;
514 struct irq_cfg *cfg;
515 struct irq_pin_list *entry;
516
517 cfg = irq_cfg(irq);
518 entry = cfg->irq_2_pin;
519 for (;;) {
520 unsigned int reg;
521
522 if (!entry)
523 break;
524
525 apic = entry->apic;
526 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200527#ifdef CONFIG_INTR_REMAP
528 /*
529 * With interrupt-remapping, destination information comes
530 * from interrupt-remapping table entry.
531 */
532 if (!irq_remapped(irq))
533 io_apic_write(apic, 0x11 + pin*2, dest);
534#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700535 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200536#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700537 reg = io_apic_read(apic, 0x10 + pin*2);
538 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
539 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200540 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700541 if (!entry->next)
542 break;
543 entry = entry->next;
544 }
545}
Yinghai Luefa25592008-08-19 20:50:36 -0700546
547static int assign_irq_vector(int irq, cpumask_t mask);
548
Yinghai Lu497c9a12008-08-19 20:50:28 -0700549static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
550{
551 struct irq_cfg *cfg;
552 unsigned long flags;
553 unsigned int dest;
554 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200555 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700556
Yinghai Lu497c9a12008-08-19 20:50:28 -0700557 cpus_and(tmp, mask, cpu_online_map);
558 if (cpus_empty(tmp))
559 return;
560
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700561 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700562 if (assign_irq_vector(irq, mask))
563 return;
564
565 cpus_and(tmp, cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700566 dest = cpu_mask_to_apicid(tmp);
567 /*
568 * Only the high 8 bits are valid.
569 */
570 dest = SET_APIC_LOGICAL_ID(dest);
571
Ingo Molnar54168ed2008-08-20 09:07:45 +0200572 desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700573 spin_lock_irqsave(&ioapic_lock, flags);
574 __target_IO_APIC_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200575 desc->affinity = mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700576 spin_unlock_irqrestore(&ioapic_lock, flags);
577}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700578#endif /* CONFIG_SMP */
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/*
581 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
582 * shared ISA-space IRQs, so we have to support them. We are super
583 * fast in the common case, and fast for shared ISA-space IRQs.
584 */
585static void add_pin_to_irq(unsigned int irq, int apic, int pin)
586{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700587 struct irq_cfg *cfg;
588 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Yinghai Lu0f978f42008-08-19 20:50:26 -0700590 /* first time to refer irq_cfg, so with new */
591 cfg = irq_cfg_alloc(irq);
592 entry = cfg->irq_2_pin;
593 if (!entry) {
594 entry = get_one_free_irq_2_pin();
595 cfg->irq_2_pin = entry;
596 entry->apic = apic;
597 entry->pin = pin;
598 printk(KERN_DEBUG " 0 add_pin_to_irq: irq %d --> apic %d pin %d\n", irq, apic, pin);
599 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700601
602 while (entry->next) {
603 /* not again, please */
604 if (entry->apic == apic && entry->pin == pin)
605 return;
606
607 entry = entry->next;
608 }
609
610 entry->next = get_one_free_irq_2_pin();
611 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 entry->apic = apic;
613 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700614 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 -0700615}
616
617/*
618 * Reroute an IRQ to a different pin.
619 */
620static void __init replace_pin_at_irq(unsigned int irq,
621 int oldapic, int oldpin,
622 int newapic, int newpin)
623{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700624 struct irq_cfg *cfg = irq_cfg(irq);
625 struct irq_pin_list *entry = cfg->irq_2_pin;
626 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Yinghai Lu0f978f42008-08-19 20:50:26 -0700628 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (entry->apic == oldapic && entry->pin == oldpin) {
630 entry->apic = newapic;
631 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700632 replaced = 1;
633 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700635 }
636 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700638
639 /* why? call replace before add? */
640 if (!replaced)
641 add_pin_to_irq(irq, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Yinghai Lu4e738e22008-08-19 20:50:47 -0700644#define __DO_ACTION(R, ACTION_ENABLE, ACTION_DISABLE, FINAL) \
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700645 \
646{ \
647 int pin; \
648 struct irq_cfg *cfg; \
649 struct irq_pin_list *entry; \
650 \
651 cfg = irq_cfg(irq); \
652 entry = cfg->irq_2_pin; \
653 for (;;) { \
654 unsigned int reg; \
655 if (!entry) \
656 break; \
657 pin = entry->pin; \
658 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
Yinghai Lu4e738e22008-08-19 20:50:47 -0700659 reg ACTION_DISABLE; \
660 reg ACTION_ENABLE; \
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700661 io_apic_modify(entry->apic, 0x10 + R + pin*2, reg); \
662 FINAL; \
663 if (!entry->next) \
664 break; \
665 entry = entry->next; \
666 } \
667}
668
Yinghai Lu4e738e22008-08-19 20:50:47 -0700669#define DO_ACTION(name,R, ACTION_ENABLE, ACTION_DISABLE, FINAL) \
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700670 \
671 static void name##_IO_APIC_irq (unsigned int irq) \
Yinghai Lu4e738e22008-08-19 20:50:47 -0700672 __DO_ACTION(R, ACTION_ENABLE, ACTION_DISABLE, FINAL)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700673
674/* mask = 0 */
Yinghai Lu4e738e22008-08-19 20:50:47 -0700675DO_ACTION(__unmask, 0, |= 0, &= ~IO_APIC_REDIR_MASKED, )
676
677#ifdef CONFIG_X86_64
678/*
679 * Synchronize the IO-APIC and the CPU by doing
680 * a dummy read from the IO-APIC
681 */
682static inline void io_apic_sync(unsigned int apic)
683{
684 struct io_apic __iomem *io_apic = io_apic_base(apic);
685 readl(&io_apic->data);
686}
687
688/* mask = 1 */
689DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, &= ~0, io_apic_sync(entry->apic))
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700690
691#else
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693/* mask = 1 */
Yinghai Lu4e738e22008-08-19 20:50:47 -0700694DO_ACTION(__mask, 0, |= IO_APIC_REDIR_MASKED, &= ~0, )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696/* mask = 1, trigger = 0 */
Yinghai Lu4e738e22008-08-19 20:50:47 -0700697DO_ACTION(__mask_and_edge, 0, |= IO_APIC_REDIR_MASKED, &= ~IO_APIC_REDIR_LEVEL_TRIGGER, )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699/* mask = 0, trigger = 1 */
Yinghai Lu4e738e22008-08-19 20:50:47 -0700700DO_ACTION(__unmask_and_level, 0, |= IO_APIC_REDIR_LEVEL_TRIGGER, &= ~IO_APIC_REDIR_MASKED, )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700702#endif
703
Ingo Molnar54168ed2008-08-20 09:07:45 +0200704static void mask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
706 unsigned long flags;
707
708 spin_lock_irqsave(&ioapic_lock, flags);
709 __mask_IO_APIC_irq(irq);
710 spin_unlock_irqrestore(&ioapic_lock, flags);
711}
712
Ingo Molnar54168ed2008-08-20 09:07:45 +0200713static void unmask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 unsigned long flags;
716
717 spin_lock_irqsave(&ioapic_lock, flags);
718 __unmask_IO_APIC_irq(irq);
719 spin_unlock_irqrestore(&ioapic_lock, flags);
720}
721
722static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
723{
724 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200727 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (entry.delivery_mode == dest_SMI)
729 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /*
731 * Disable it in the IO-APIC irq-routing table:
732 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800733 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Ingo Molnar54168ed2008-08-20 09:07:45 +0200736static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 int apic, pin;
739
740 for (apic = 0; apic < nr_ioapics; apic++)
741 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
742 clear_IO_APIC_pin(apic, pin);
743}
744
Ingo Molnar54168ed2008-08-20 09:07:45 +0200745#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100746void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
748 unsigned int cfg;
749
750 /*
751 * Wait for idle.
752 */
753 apic_wait_icr_idle();
754 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
755 /*
756 * Send the IPI. The write to APIC_ICR fires this off.
757 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100758 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200760#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Ingo Molnar54168ed2008-08-20 09:07:45 +0200762#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/*
764 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
765 * specific CPU-side IRQs.
766 */
767
768#define MAX_PIRQS 8
769static int pirq_entries [MAX_PIRQS];
770static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772static int __init ioapic_pirq_setup(char *str)
773{
774 int i, max;
775 int ints[MAX_PIRQS+1];
776
777 get_options(str, ARRAY_SIZE(ints), ints);
778
779 for (i = 0; i < MAX_PIRQS; i++)
780 pirq_entries[i] = -1;
781
782 pirqs_enabled = 1;
783 apic_printk(APIC_VERBOSE, KERN_INFO
784 "PIRQ redirection, working around broken MP-BIOS.\n");
785 max = MAX_PIRQS;
786 if (ints[0] < MAX_PIRQS)
787 max = ints[0];
788
789 for (i = 0; i < max; i++) {
790 apic_printk(APIC_VERBOSE, KERN_DEBUG
791 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
792 /*
793 * PIRQs are mapped upside down, usually.
794 */
795 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
796 }
797 return 1;
798}
799
800__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200801#endif /* CONFIG_X86_32 */
802
803#ifdef CONFIG_INTR_REMAP
804/* I/O APIC RTE contents at the OS boot up */
805static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
806
807/*
808 * Saves and masks all the unmasked IO-APIC RTE's
809 */
810int save_mask_IO_APIC_setup(void)
811{
812 union IO_APIC_reg_01 reg_01;
813 unsigned long flags;
814 int apic, pin;
815
816 /*
817 * The number of IO-APIC IRQ registers (== #pins):
818 */
819 for (apic = 0; apic < nr_ioapics; apic++) {
820 spin_lock_irqsave(&ioapic_lock, flags);
821 reg_01.raw = io_apic_read(apic, 1);
822 spin_unlock_irqrestore(&ioapic_lock, flags);
823 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
824 }
825
826 for (apic = 0; apic < nr_ioapics; apic++) {
827 early_ioapic_entries[apic] =
828 kzalloc(sizeof(struct IO_APIC_route_entry) *
829 nr_ioapic_registers[apic], GFP_KERNEL);
830 if (!early_ioapic_entries[apic])
831 return -ENOMEM;
832 }
833
834 for (apic = 0; apic < nr_ioapics; apic++)
835 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
836 struct IO_APIC_route_entry entry;
837
838 entry = early_ioapic_entries[apic][pin] =
839 ioapic_read_entry(apic, pin);
840 if (!entry.mask) {
841 entry.mask = 1;
842 ioapic_write_entry(apic, pin, entry);
843 }
844 }
845 return 0;
846}
847
848void restore_IO_APIC_setup(void)
849{
850 int apic, pin;
851
852 for (apic = 0; apic < nr_ioapics; apic++)
853 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
854 ioapic_write_entry(apic, pin,
855 early_ioapic_entries[apic][pin]);
856}
857
858void reinit_intr_remapped_IO_APIC(int intr_remapping)
859{
860 /*
861 * for now plain restore of previous settings.
862 * TBD: In the case of OS enabling interrupt-remapping,
863 * IO-APIC RTE's need to be setup to point to interrupt-remapping
864 * table entries. for now, do a plain restore, and wait for
865 * the setup_IO_APIC_irqs() to do proper initialization.
866 */
867 restore_IO_APIC_setup();
868}
869#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871/*
872 * Find the IRQ entry number of a certain pin.
873 */
874static int find_irq_entry(int apic, int pin, int type)
875{
876 int i;
877
878 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400879 if (mp_irqs[i].mp_irqtype == type &&
880 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
881 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
882 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return i;
884
885 return -1;
886}
887
888/*
889 * Find the pin to which IRQ[irq] (ISA) is connected
890 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800891static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 int i;
894
895 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400896 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300898 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400899 (mp_irqs[i].mp_irqtype == type) &&
900 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400902 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 return -1;
905}
906
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800907static int __init find_isa_irq_apic(int irq, int type)
908{
909 int i;
910
911 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400912 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800913
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300914 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400915 (mp_irqs[i].mp_irqtype == type) &&
916 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800917 break;
918 }
919 if (i < mp_irq_entries) {
920 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200921 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400922 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800923 return apic;
924 }
925 }
926
927 return -1;
928}
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/*
931 * Find a specific PCI IRQ entry.
932 * Not an __init, possibly needed by modules
933 */
934static int pin_2_irq(int idx, int apic, int pin);
935
936int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
937{
938 int apic, i, best_guess = -1;
939
Ingo Molnar54168ed2008-08-20 09:07:45 +0200940 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
941 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400942 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200943 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 return -1;
945 }
946 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400947 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400950 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
951 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 break;
953
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300954 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400955 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400957 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200958 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 if (!(apic || IO_APIC_IRQ(irq)))
961 continue;
962
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400963 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return irq;
965 /*
966 * Use the first all-but-pin matching entry as a
967 * best-guess fuzzy result for broken mptables.
968 */
969 if (best_guess < 0)
970 best_guess = irq;
971 }
972 }
973 return best_guess;
974}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200975
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700976EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300978#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979/*
980 * EISA Edge/Level control register, ELCR
981 */
982static int EISA_ELCR(unsigned int irq)
983{
984 if (irq < 16) {
985 unsigned int port = 0x4d0 + (irq >> 3);
986 return (inb(port) >> (irq & 7)) & 1;
987 }
988 apic_printk(APIC_VERBOSE, KERN_INFO
989 "Broken MPtable reports ISA irq %d\n", irq);
990 return 0;
991}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200992
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300993#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300995/* ISA interrupts are always polarity zero edge triggered,
996 * when listed as conforming in the MP table. */
997
998#define default_ISA_trigger(idx) (0)
999#define default_ISA_polarity(idx) (0)
1000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001/* EISA interrupts are always polarity zero and can be edge or level
1002 * trigger depending on the ELCR value. If an interrupt is listed as
1003 * EISA conforming in the MP table, that means its trigger type must
1004 * be read in from the ELCR */
1005
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001006#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001007#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009/* PCI interrupts are always polarity one level triggered,
1010 * when listed as conforming in the MP table. */
1011
1012#define default_PCI_trigger(idx) (1)
1013#define default_PCI_polarity(idx) (1)
1014
1015/* MCA interrupts are always polarity zero level triggered,
1016 * when listed as conforming in the MP table. */
1017
1018#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001019#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Shaohua Li61fd47e2007-11-17 01:05:28 -05001021static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001023 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 int polarity;
1025
1026 /*
1027 * Determine IRQ line polarity (high active or low active):
1028 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001029 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001031 case 0: /* conforms, ie. bus-type dependent polarity */
1032 if (test_bit(bus, mp_bus_not_pci))
1033 polarity = default_ISA_polarity(idx);
1034 else
1035 polarity = default_PCI_polarity(idx);
1036 break;
1037 case 1: /* high active */
1038 {
1039 polarity = 0;
1040 break;
1041 }
1042 case 2: /* reserved */
1043 {
1044 printk(KERN_WARNING "broken BIOS!!\n");
1045 polarity = 1;
1046 break;
1047 }
1048 case 3: /* low active */
1049 {
1050 polarity = 1;
1051 break;
1052 }
1053 default: /* invalid */
1054 {
1055 printk(KERN_WARNING "broken BIOS!!\n");
1056 polarity = 1;
1057 break;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 return polarity;
1061}
1062
1063static int MPBIOS_trigger(int idx)
1064{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001065 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 int trigger;
1067
1068 /*
1069 * Determine IRQ trigger mode (edge or level sensitive):
1070 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001071 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001073 case 0: /* conforms, ie. bus-type dependent */
1074 if (test_bit(bus, mp_bus_not_pci))
1075 trigger = default_ISA_trigger(idx);
1076 else
1077 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001078#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001079 switch (mp_bus_id_to_type[bus]) {
1080 case MP_BUS_ISA: /* ISA pin */
1081 {
1082 /* set before the switch */
1083 break;
1084 }
1085 case MP_BUS_EISA: /* EISA pin */
1086 {
1087 trigger = default_EISA_trigger(idx);
1088 break;
1089 }
1090 case MP_BUS_PCI: /* PCI pin */
1091 {
1092 /* set before the switch */
1093 break;
1094 }
1095 case MP_BUS_MCA: /* MCA pin */
1096 {
1097 trigger = default_MCA_trigger(idx);
1098 break;
1099 }
1100 default:
1101 {
1102 printk(KERN_WARNING "broken BIOS!!\n");
1103 trigger = 1;
1104 break;
1105 }
1106 }
1107#endif
1108 break;
1109 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001110 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001111 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001112 break;
1113 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001114 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001115 {
1116 printk(KERN_WARNING "broken BIOS!!\n");
1117 trigger = 1;
1118 break;
1119 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001120 case 3: /* level */
1121 {
1122 trigger = 1;
1123 break;
1124 }
1125 default: /* invalid */
1126 {
1127 printk(KERN_WARNING "broken BIOS!!\n");
1128 trigger = 0;
1129 break;
1130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 }
1132 return trigger;
1133}
1134
1135static inline int irq_polarity(int idx)
1136{
1137 return MPBIOS_polarity(idx);
1138}
1139
1140static inline int irq_trigger(int idx)
1141{
1142 return MPBIOS_trigger(idx);
1143}
1144
Yinghai Luefa25592008-08-19 20:50:36 -07001145int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146static int pin_2_irq(int idx, int apic, int pin)
1147{
1148 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001149 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 /*
1152 * Debugging check, we are in big trouble if this message pops up!
1153 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001154 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1156
Ingo Molnar54168ed2008-08-20 09:07:45 +02001157 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001158 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001159 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001160 /*
1161 * PCI IRQs are mapped in order
1162 */
1163 i = irq = 0;
1164 while (i < apic)
1165 irq += nr_ioapic_registers[i++];
1166 irq += pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001167 /*
1168 * For MPS mode, so far only needed by ES7000 platform
1169 */
1170 if (ioapic_renumber_irq)
1171 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
1173
Ingo Molnar54168ed2008-08-20 09:07:45 +02001174#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 /*
1176 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1177 */
1178 if ((pin >= 16) && (pin <= 23)) {
1179 if (pirq_entries[pin-16] != -1) {
1180 if (!pirq_entries[pin-16]) {
1181 apic_printk(APIC_VERBOSE, KERN_DEBUG
1182 "disabling PIRQ%d\n", pin-16);
1183 } else {
1184 irq = pirq_entries[pin-16];
1185 apic_printk(APIC_VERBOSE, KERN_DEBUG
1186 "using PIRQ%d -> IRQ %d\n",
1187 pin-16, irq);
1188 }
1189 }
1190 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001191#endif
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 return irq;
1194}
1195
Yinghai Lu497c9a12008-08-19 20:50:28 -07001196void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001198 /* Used to the online set of cpus does not change
1199 * during assign_irq_vector.
1200 */
1201 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Yinghai Lu497c9a12008-08-19 20:50:28 -07001204void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001205{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001206 spin_unlock(&vector_lock);
1207}
1208
1209static int __assign_irq_vector(int irq, cpumask_t mask)
1210{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001211 /*
1212 * NOTE! The local APIC isn't very good at handling
1213 * multiple interrupts at the same interrupt level.
1214 * As the interrupt level is determined by taking the
1215 * vector number and shifting that right by 4, we
1216 * want to spread these out a bit so that they don't
1217 * all fall in the same interrupt level.
1218 *
1219 * Also, we've got to be careful not to trash gate
1220 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1221 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001222 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1223 unsigned int old_vector;
1224 int cpu;
1225 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001226
Ingo Molnar54168ed2008-08-20 09:07:45 +02001227 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001228
Ingo Molnar54168ed2008-08-20 09:07:45 +02001229 /* Only try and allocate irqs on cpus that are present */
1230 cpus_and(mask, mask, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001231
Ingo Molnar54168ed2008-08-20 09:07:45 +02001232 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1233 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001234
Ingo Molnar54168ed2008-08-20 09:07:45 +02001235 old_vector = cfg->vector;
1236 if (old_vector) {
1237 cpumask_t tmp;
1238 cpus_and(tmp, cfg->domain, mask);
1239 if (!cpus_empty(tmp))
1240 return 0;
1241 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001242
Ingo Molnar54168ed2008-08-20 09:07:45 +02001243 for_each_cpu_mask_nr(cpu, mask) {
1244 cpumask_t domain, new_mask;
1245 int new_cpu;
1246 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001247
Ingo Molnar54168ed2008-08-20 09:07:45 +02001248 domain = vector_allocation_domain(cpu);
1249 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001250
Ingo Molnar54168ed2008-08-20 09:07:45 +02001251 vector = current_vector;
1252 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001253next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001254 vector += 8;
1255 if (vector >= first_system_vector) {
1256 /* If we run out of vectors on large boxen, must share them. */
1257 offset = (offset + 1) % 8;
1258 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001259 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001260 if (unlikely(current_vector == vector))
1261 continue;
1262#ifdef CONFIG_X86_64
1263 if (vector == IA32_SYSCALL_VECTOR)
1264 goto next;
1265#else
1266 if (vector == SYSCALL_VECTOR)
1267 goto next;
1268#endif
1269 for_each_cpu_mask_nr(new_cpu, new_mask)
1270 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1271 goto next;
1272 /* Found one! */
1273 current_vector = vector;
1274 current_offset = offset;
1275 if (old_vector) {
1276 cfg->move_in_progress = 1;
1277 cfg->old_domain = cfg->domain;
1278 }
1279 for_each_cpu_mask_nr(new_cpu, new_mask)
1280 per_cpu(vector_irq, new_cpu)[vector] = irq;
1281 cfg->vector = vector;
1282 cfg->domain = domain;
1283 return 0;
1284 }
1285 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001286}
1287
1288static int assign_irq_vector(int irq, cpumask_t mask)
1289{
1290 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001291 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001292
1293 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001294 err = __assign_irq_vector(irq, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001295 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001296 return err;
1297}
1298
1299static void __clear_irq_vector(int irq)
1300{
1301 struct irq_cfg *cfg;
1302 cpumask_t mask;
1303 int cpu, vector;
1304
1305 cfg = irq_cfg(irq);
1306 BUG_ON(!cfg->vector);
1307
1308 vector = cfg->vector;
1309 cpus_and(mask, cfg->domain, cpu_online_map);
1310 for_each_cpu_mask_nr(cpu, mask)
1311 per_cpu(vector_irq, cpu)[vector] = -1;
1312
1313 cfg->vector = 0;
1314 cpus_clear(cfg->domain);
1315}
1316
1317void __setup_vector_irq(int cpu)
1318{
1319 /* Initialize vector_irq on a new cpu */
1320 /* This function must be called with vector_lock held */
1321 int irq, vector;
1322 struct irq_cfg *cfg;
1323
1324 /* Mark the inuse vectors */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001325 for_each_irq_cfg(irq, cfg) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001326 if (!cpu_isset(cpu, cfg->domain))
1327 continue;
1328 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001329 per_cpu(vector_irq, cpu)[vector] = irq;
1330 }
1331 /* Mark the free vectors */
1332 for (vector = 0; vector < NR_VECTORS; ++vector) {
1333 irq = per_cpu(vector_irq, cpu)[vector];
1334 if (irq < 0)
1335 continue;
1336
1337 cfg = irq_cfg(irq);
1338 if (!cpu_isset(cpu, cfg->domain))
1339 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001340 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001341}
Glauber Costa3fde6902008-05-28 20:34:19 -07001342
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001343static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001344#ifdef CONFIG_INTR_REMAP
1345static struct irq_chip ir_ioapic_chip;
1346#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Ingo Molnar54168ed2008-08-20 09:07:45 +02001348#define IOAPIC_AUTO -1
1349#define IOAPIC_EDGE 0
1350#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001352#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001353static inline int IO_APIC_irq_trigger(int irq)
1354{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001355 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001356
Ingo Molnar54168ed2008-08-20 09:07:45 +02001357 for (apic = 0; apic < nr_ioapics; apic++) {
1358 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1359 idx = find_irq_entry(apic, pin, mp_INT);
1360 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1361 return irq_trigger(idx);
1362 }
1363 }
1364 /*
1365 * nonexistent IRQs are edge default
1366 */
1367 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001368}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001369#else
1370static inline int IO_APIC_irq_trigger(int irq)
1371{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001372 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001373}
1374#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001375
Yinghai Lu497c9a12008-08-19 20:50:28 -07001376static void ioapic_register_intr(int irq, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Yinghai Lu08678b02008-08-19 20:50:05 -07001378 struct irq_desc *desc;
1379
Yinghai Lu199751d2008-08-19 20:50:27 -07001380 /* first time to use this irq_desc */
1381 if (irq < 16)
1382 desc = irq_to_desc(irq);
1383 else
1384 desc = irq_to_desc_alloc(irq);
1385
Jan Beulich6ebcc002006-06-26 13:56:46 +02001386 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001387 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001388 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001389 else
1390 desc->status &= ~IRQ_LEVEL;
1391
Ingo Molnar54168ed2008-08-20 09:07:45 +02001392#ifdef CONFIG_INTR_REMAP
1393 if (irq_remapped(irq)) {
1394 desc->status |= IRQ_MOVE_PCNTXT;
1395 if (trigger)
1396 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1397 handle_fasteoi_irq,
1398 "fasteoi");
1399 else
1400 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1401 handle_edge_irq, "edge");
1402 return;
1403 }
1404#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001405 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1406 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001407 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001408 handle_fasteoi_irq,
1409 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001410 else
Ingo Molnara460e742006-10-17 00:10:03 -07001411 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001412 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001413}
1414
1415static int setup_ioapic_entry(int apic, int irq,
1416 struct IO_APIC_route_entry *entry,
1417 unsigned int destination, int trigger,
1418 int polarity, int vector)
1419{
1420 /*
1421 * add it to the IO-APIC irq-routing table:
1422 */
1423 memset(entry,0,sizeof(*entry));
1424
Ingo Molnar54168ed2008-08-20 09:07:45 +02001425#ifdef CONFIG_INTR_REMAP
1426 if (intr_remapping_enabled) {
1427 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1428 struct irte irte;
1429 struct IR_IO_APIC_route_entry *ir_entry =
1430 (struct IR_IO_APIC_route_entry *) entry;
1431 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001432
Ingo Molnar54168ed2008-08-20 09:07:45 +02001433 if (!iommu)
1434 panic("No mapping iommu for ioapic %d\n", apic);
1435
1436 index = alloc_irte(iommu, irq, 1);
1437 if (index < 0)
1438 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1439
1440 memset(&irte, 0, sizeof(irte));
1441
1442 irte.present = 1;
1443 irte.dst_mode = INT_DEST_MODE;
1444 irte.trigger_mode = trigger;
1445 irte.dlvry_mode = INT_DELIVERY_MODE;
1446 irte.vector = vector;
1447 irte.dest_id = IRTE_DEST(destination);
1448
1449 modify_irte(irq, &irte);
1450
1451 ir_entry->index2 = (index >> 15) & 0x1;
1452 ir_entry->zero = 0;
1453 ir_entry->format = 1;
1454 ir_entry->index = (index & 0x7fff);
1455 } else
1456#endif
1457 {
1458 entry->delivery_mode = INT_DELIVERY_MODE;
1459 entry->dest_mode = INT_DEST_MODE;
1460 entry->dest = destination;
1461 }
1462
1463 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001464 entry->trigger = trigger;
1465 entry->polarity = polarity;
1466 entry->vector = vector;
1467
1468 /* Mask level triggered irqs.
1469 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1470 */
1471 if (trigger)
1472 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001473 return 0;
1474}
1475
1476static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001477 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001478{
1479 struct irq_cfg *cfg;
1480 struct IO_APIC_route_entry entry;
1481 cpumask_t mask;
1482
1483 if (!IO_APIC_IRQ(irq))
1484 return;
1485
1486 cfg = irq_cfg(irq);
1487
1488 mask = TARGET_CPUS;
1489 if (assign_irq_vector(irq, mask))
1490 return;
1491
1492 cpus_and(mask, cfg->domain, mask);
1493
1494 apic_printk(APIC_VERBOSE,KERN_DEBUG
1495 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1496 "IRQ %d Mode:%i Active:%i)\n",
1497 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1498 irq, trigger, polarity);
1499
1500
1501 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1502 cpu_mask_to_apicid(mask), trigger, polarity,
1503 cfg->vector)) {
1504 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1505 mp_ioapics[apic].mp_apicid, pin);
1506 __clear_irq_vector(irq);
1507 return;
1508 }
1509
1510 ioapic_register_intr(irq, trigger);
1511 if (irq < 16)
1512 disable_8259A_irq(irq);
1513
1514 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
1517static void __init setup_IO_APIC_irqs(void)
1518{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001519 int apic, pin, idx, irq, first_notcon = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1522
1523 for (apic = 0; apic < nr_ioapics; apic++) {
1524 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1525
Yinghai Lu497c9a12008-08-19 20:50:28 -07001526 idx = find_irq_entry(apic,pin,mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (idx == -1) {
1528 if (first_notcon) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001529 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 -07001530 first_notcon = 0;
1531 } else
Yinghai Lu497c9a12008-08-19 20:50:28 -07001532 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 continue;
1534 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001535 if (!first_notcon) {
1536 apic_printk(APIC_VERBOSE, " not connected.\n");
1537 first_notcon = 1;
1538 }
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001541#ifdef CONFIG_X86_32
Yinghai Lu497c9a12008-08-19 20:50:28 -07001542 if (multi_timer_check(apic, irq))
1543 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001544#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07001545 add_pin_to_irq(irq, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001546
Yinghai Lu497c9a12008-08-19 20:50:28 -07001547 setup_IO_APIC_irq(apic, pin, irq,
1548 irq_trigger(idx), irq_polarity(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550 }
1551
1552 if (!first_notcon)
1553 apic_printk(APIC_VERBOSE, " not connected.\n");
1554}
1555
1556/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001557 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001559static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1560 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Ingo Molnar54168ed2008-08-20 09:07:45 +02001564#ifdef CONFIG_INTR_REMAP
1565 if (intr_remapping_enabled)
1566 return;
1567#endif
1568
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001569 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 /*
1572 * We use logical delivery to get the timer IRQ
1573 * to the first CPU.
1574 */
1575 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001576 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001577 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 entry.delivery_mode = INT_DELIVERY_MODE;
1579 entry.polarity = 0;
1580 entry.trigger = 0;
1581 entry.vector = vector;
1582
1583 /*
1584 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001585 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001587 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 /*
1590 * Add it to the IO-APIC irq-routing table:
1591 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001592 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001595
1596__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 int apic, i;
1599 union IO_APIC_reg_00 reg_00;
1600 union IO_APIC_reg_01 reg_01;
1601 union IO_APIC_reg_02 reg_02;
1602 union IO_APIC_reg_03 reg_03;
1603 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001604 struct irq_cfg *cfg;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001605 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 if (apic_verbosity == APIC_QUIET)
1608 return;
1609
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001610 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 for (i = 0; i < nr_ioapics; i++)
1612 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001613 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615 /*
1616 * We are a bit conservative about what we expect. We have to
1617 * know about every hardware change ASAP.
1618 */
1619 printk(KERN_INFO "testing the IO APIC.......................\n");
1620
1621 for (apic = 0; apic < nr_ioapics; apic++) {
1622
1623 spin_lock_irqsave(&ioapic_lock, flags);
1624 reg_00.raw = io_apic_read(apic, 0);
1625 reg_01.raw = io_apic_read(apic, 1);
1626 if (reg_01.bits.version >= 0x10)
1627 reg_02.raw = io_apic_read(apic, 2);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001628 if (reg_01.bits.version >= 0x20)
1629 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 spin_unlock_irqrestore(&ioapic_lock, flags);
1631
Ingo Molnar54168ed2008-08-20 09:07:45 +02001632 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001633 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1635 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1636 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1637 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Ingo Molnar54168ed2008-08-20 09:07:45 +02001639 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1643 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 /*
1646 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1647 * but the value of reg_02 is read as the previous read register
1648 * value, so ignore it if reg_02 == reg_01.
1649 */
1650 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1651 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1652 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654
1655 /*
1656 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1657 * or reg_03, but the value of reg_0[23] is read as the previous read
1658 * register value, so ignore it if reg_03 == reg_0[12].
1659 */
1660 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1661 reg_03.raw != reg_01.raw) {
1662 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1663 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
1666 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1667
Yinghai Lud83e94a2008-08-19 20:50:33 -07001668 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1669 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 for (i = 0; i <= reg_01.bits.entries; i++) {
1672 struct IO_APIC_route_entry entry;
1673
Andi Kleencf4c6a22006-09-26 10:52:30 +02001674 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Ingo Molnar54168ed2008-08-20 09:07:45 +02001676 printk(KERN_DEBUG " %02x %03X ",
1677 i,
1678 entry.dest
1679 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1682 entry.mask,
1683 entry.trigger,
1684 entry.irr,
1685 entry.polarity,
1686 entry.delivery_status,
1687 entry.dest_mode,
1688 entry.delivery_mode,
1689 entry.vector
1690 );
1691 }
1692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001694 for_each_irq_cfg(irq, cfg) {
Yinghai Lu0f978f42008-08-19 20:50:26 -07001695 struct irq_pin_list *entry = cfg->irq_2_pin;
1696 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001698 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 for (;;) {
1700 printk("-> %d:%d", entry->apic, entry->pin);
1701 if (!entry->next)
1702 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001703 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 printk("\n");
1706 }
1707
1708 printk(KERN_INFO ".................................... done.\n");
1709
1710 return;
1711}
1712
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001713__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 unsigned int v;
1716 int i, j;
1717
1718 if (apic_verbosity == APIC_QUIET)
1719 return;
1720
1721 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1722 for (i = 0; i < 8; i++) {
1723 v = apic_read(base + i*0x10);
1724 for (j = 0; j < 32; j++) {
1725 if (v & (1<<j))
1726 printk("1");
1727 else
1728 printk("0");
1729 }
1730 printk("\n");
1731 }
1732}
1733
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001734__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
1736 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001737 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if (apic_verbosity == APIC_QUIET)
1740 return;
1741
1742 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1743 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001744 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001745 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 v = apic_read(APIC_LVR);
1747 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1748 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001749 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 v = apic_read(APIC_TASKPRI);
1752 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1753
Ingo Molnar54168ed2008-08-20 09:07:45 +02001754 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001755 if (!APIC_XAPIC(ver)) {
1756 v = apic_read(APIC_ARBPRI);
1757 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1758 v & APIC_ARBPRI_MASK);
1759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 v = apic_read(APIC_PROCPRI);
1761 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1762 }
1763
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001764 /*
1765 * Remote read supported only in the 82489DX and local APIC for
1766 * Pentium processors.
1767 */
1768 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1769 v = apic_read(APIC_RRR);
1770 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1771 }
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 v = apic_read(APIC_LDR);
1774 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001775 if (!x2apic_enabled()) {
1776 v = apic_read(APIC_DFR);
1777 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 v = apic_read(APIC_SPIV);
1780 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1781
1782 printk(KERN_DEBUG "... APIC ISR field:\n");
1783 print_APIC_bitfield(APIC_ISR);
1784 printk(KERN_DEBUG "... APIC TMR field:\n");
1785 print_APIC_bitfield(APIC_TMR);
1786 printk(KERN_DEBUG "... APIC IRR field:\n");
1787 print_APIC_bitfield(APIC_IRR);
1788
Ingo Molnar54168ed2008-08-20 09:07:45 +02001789 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1790 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 v = apic_read(APIC_ESR);
1794 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1795 }
1796
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001797 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001798 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1799 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
1801 v = apic_read(APIC_LVTT);
1802 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1803
1804 if (maxlvt > 3) { /* PC is LVT#4. */
1805 v = apic_read(APIC_LVTPC);
1806 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1807 }
1808 v = apic_read(APIC_LVT0);
1809 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1810 v = apic_read(APIC_LVT1);
1811 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1812
1813 if (maxlvt > 2) { /* ERR is LVT#3. */
1814 v = apic_read(APIC_LVTERR);
1815 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1816 }
1817
1818 v = apic_read(APIC_TMICT);
1819 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1820 v = apic_read(APIC_TMCCT);
1821 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1822 v = apic_read(APIC_TDCR);
1823 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1824 printk("\n");
1825}
1826
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001827__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001829 int cpu;
1830
1831 preempt_disable();
1832 for_each_online_cpu(cpu)
1833 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1834 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001837__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 unsigned int v;
1840 unsigned long flags;
1841
1842 if (apic_verbosity == APIC_QUIET)
1843 return;
1844
1845 printk(KERN_DEBUG "\nprinting PIC contents\n");
1846
1847 spin_lock_irqsave(&i8259A_lock, flags);
1848
1849 v = inb(0xa1) << 8 | inb(0x21);
1850 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1851
1852 v = inb(0xa0) << 8 | inb(0x20);
1853 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1854
Ingo Molnar54168ed2008-08-20 09:07:45 +02001855 outb(0x0b,0xa0);
1856 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001858 outb(0x0a,0xa0);
1859 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 spin_unlock_irqrestore(&i8259A_lock, flags);
1862
1863 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1864
1865 v = inb(0x4d1) << 8 | inb(0x4d0);
1866 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1867}
1868
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001869__apicdebuginit(int) print_all_ICs(void)
1870{
1871 print_PIC();
1872 print_all_local_APICs();
1873 print_IO_APIC();
1874
1875 return 0;
1876}
1877
1878fs_initcall(print_all_ICs);
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Yinghai Luefa25592008-08-19 20:50:36 -07001881/* Where if anywhere is the i8259 connect in external int mode */
1882static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1883
Ingo Molnar54168ed2008-08-20 09:07:45 +02001884void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
1886 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001887 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001888 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 unsigned long flags;
1890
Ingo Molnar54168ed2008-08-20 09:07:45 +02001891#ifdef CONFIG_X86_32
1892 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (!pirqs_enabled)
1894 for (i = 0; i < MAX_PIRQS; i++)
1895 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
1898 /*
1899 * The number of IO-APIC IRQ registers (== #pins):
1900 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001901 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001903 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001905 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1906 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001907 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001908 int pin;
1909 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001910 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001911 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001912 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001913
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001914 /* If the interrupt line is enabled and in ExtInt mode
1915 * I have found the pin where the i8259 is connected.
1916 */
1917 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1918 ioapic_i8259.apic = apic;
1919 ioapic_i8259.pin = pin;
1920 goto found_i8259;
1921 }
1922 }
1923 }
1924 found_i8259:
1925 /* Look to see what if the MP table has reported the ExtINT */
1926 /* If we could not find the appropriate pin by looking at the ioapic
1927 * the i8259 probably is not connected the ioapic but give the
1928 * mptable a chance anyway.
1929 */
1930 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1931 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1932 /* Trust the MP table if nothing is setup in the hardware */
1933 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1934 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1935 ioapic_i8259.pin = i8259_pin;
1936 ioapic_i8259.apic = i8259_apic;
1937 }
1938 /* Complain if the MP table and the hardware disagree */
1939 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1940 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1941 {
1942 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
1945 /*
1946 * Do not trust the IO-APIC being empty at bootup
1947 */
1948 clear_IO_APIC();
1949}
1950
1951/*
1952 * Not an __init, needed by the reboot code
1953 */
1954void disable_IO_APIC(void)
1955{
1956 /*
1957 * Clear the IO-APIC before rebooting:
1958 */
1959 clear_IO_APIC();
1960
Eric W. Biederman650927e2005-06-25 14:57:44 -07001961 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001962 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001963 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001964 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001965 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001966 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001967 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001968
1969 memset(&entry, 0, sizeof(entry));
1970 entry.mask = 0; /* Enabled */
1971 entry.trigger = 0; /* Edge */
1972 entry.irr = 0;
1973 entry.polarity = 0; /* High */
1974 entry.delivery_status = 0;
1975 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001976 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001977 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001978 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001979
1980 /*
1981 * Add it to the IO-APIC irq-routing table:
1982 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001983 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001984 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001985
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001986 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Ingo Molnar54168ed2008-08-20 09:07:45 +02001989#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990/*
1991 * function to set the IO-APIC physical IDs based on the
1992 * values stored in the MPC table.
1993 *
1994 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1995 */
1996
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997static void __init setup_ioapic_ids_from_mpc(void)
1998{
1999 union IO_APIC_reg_00 reg_00;
2000 physid_mask_t phys_id_present_map;
2001 int apic;
2002 int i;
2003 unsigned char old_id;
2004 unsigned long flags;
2005
Yinghai Lua4dbc342008-07-25 02:14:28 -07002006 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07002007 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002010 * Don't check I/O APIC IDs for xAPIC systems. They have
2011 * no meaning without the serial APIC bus.
2012 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002013 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2014 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002015 return;
2016 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 * This is broken; anything with a real cpu count has to
2018 * circumvent this idiocy regardless.
2019 */
2020 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2021
2022 /*
2023 * Set the IOAPIC ID to the value stored in the MPC table.
2024 */
2025 for (apic = 0; apic < nr_ioapics; apic++) {
2026
2027 /* Read the register 0 value */
2028 spin_lock_irqsave(&ioapic_lock, flags);
2029 reg_00.raw = io_apic_read(apic, 0);
2030 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002031
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002032 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002034 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002036 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2038 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002039 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Sanity check, is the ID really free? Every APIC in a
2044 * system must have a unique ID or we get lots of nice
2045 * 'stuck on smp_invalidate_needed IPI wait' messages.
2046 */
2047 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002048 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002050 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 for (i = 0; i < get_physical_broadcast(); i++)
2052 if (!physid_isset(i, phys_id_present_map))
2053 break;
2054 if (i >= get_physical_broadcast())
2055 panic("Max APIC ID exceeded!\n");
2056 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2057 i);
2058 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002059 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 } else {
2061 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002062 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 apic_printk(APIC_VERBOSE, "Setting %d in the "
2064 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002065 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2067 }
2068
2069
2070 /*
2071 * We need to adjust the IRQ routing table
2072 * if the ID changed.
2073 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002074 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002076 if (mp_irqs[i].mp_dstapic == old_id)
2077 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002078 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /*
2081 * Read the right value from the MPC table and
2082 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 apic_printk(APIC_VERBOSE, KERN_INFO
2085 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002086 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002088 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002090 io_apic_write(apic, 0, reg_00.raw);
2091 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 /*
2094 * Sanity check
2095 */
2096 spin_lock_irqsave(&ioapic_lock, flags);
2097 reg_00.raw = io_apic_read(apic, 0);
2098 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002099 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 printk("could not set ID!\n");
2101 else
2102 apic_printk(APIC_VERBOSE, " ok.\n");
2103 }
2104}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002105#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002107int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002108
2109static int __init notimercheck(char *s)
2110{
2111 no_timer_check = 1;
2112 return 1;
2113}
2114__setup("no_timer_check", notimercheck);
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/*
2117 * There is a nasty bug in some older SMP boards, their mptable lies
2118 * about the timer IRQ. We do the following to work around the situation:
2119 *
2120 * - timer IRQ defaults to IO-APIC IRQ
2121 * - if this function detects that timer IRQs are defunct, then we fall
2122 * back to ISA timer IRQs
2123 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002124static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
2126 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002127 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Zachary Amsden8542b202006-12-07 02:14:09 +01002129 if (no_timer_check)
2130 return 1;
2131
Ingo Molnar4aae0702007-12-18 18:05:58 +01002132 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 local_irq_enable();
2134 /* Let ten ticks pass... */
2135 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002136 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 /*
2139 * Expect a few ticks at least, to be sure some possible
2140 * glue logic does not lock up after one or two first
2141 * ticks in a non-ExtINT mode. Also the local APIC
2142 * might have cached one ExtINT interrupt. Finally, at
2143 * least one tick may be lost due to delays.
2144 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002145
2146 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002147 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return 0;
2150}
2151
2152/*
2153 * In the SMP+IOAPIC case it might happen that there are an unspecified
2154 * number of pending IRQ events unhandled. These cases are very rare,
2155 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2156 * better to do it this way as thus we do not have to be aware of
2157 * 'pending' interrupts in the IRQ path, except at this point.
2158 */
2159/*
2160 * Edge triggered needs to resend any interrupt
2161 * that was delayed but this is now handled in the device
2162 * independent code.
2163 */
2164
2165/*
2166 * Starting up a edge-triggered IO-APIC interrupt is
2167 * nasty - we need to make sure that we get the edge.
2168 * If it is already asserted for some reason, we need
2169 * return 1 to indicate that is was pending.
2170 *
2171 * This is not complete - we should be able to fake
2172 * an edge even if it isn't on the 8259A...
2173 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002174
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002175static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176{
2177 int was_pending = 0;
2178 unsigned long flags;
2179
2180 spin_lock_irqsave(&ioapic_lock, flags);
2181 if (irq < 16) {
2182 disable_8259A_irq(irq);
2183 if (i8259A_irq_pending(irq))
2184 was_pending = 1;
2185 }
2186 __unmask_IO_APIC_irq(irq);
2187 spin_unlock_irqrestore(&ioapic_lock, flags);
2188
2189 return was_pending;
2190}
2191
Ingo Molnar54168ed2008-08-20 09:07:45 +02002192#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002193static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002195
2196 struct irq_cfg *cfg = irq_cfg(irq);
2197 unsigned long flags;
2198
2199 spin_lock_irqsave(&vector_lock, flags);
2200 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2201 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002202
2203 return 1;
2204}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002205#else
2206static int ioapic_retrigger_irq(unsigned int irq)
2207{
2208 send_IPI_self(irq_cfg(irq)->vector);
2209
2210 return 1;
2211}
2212#endif
2213
2214/*
2215 * Level and edge triggered IO-APIC interrupts need different handling,
2216 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2217 * handled with the level-triggered descriptor, but that one has slightly
2218 * more overhead. Level-triggered interrupts cannot be handled with the
2219 * edge-triggered handler, without risking IRQ storms and other ugly
2220 * races.
2221 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002222
Yinghai Lu497c9a12008-08-19 20:50:28 -07002223#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002224
2225#ifdef CONFIG_INTR_REMAP
2226static void ir_irq_migration(struct work_struct *work);
2227
2228static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2229
2230/*
2231 * Migrate the IO-APIC irq in the presence of intr-remapping.
2232 *
2233 * For edge triggered, irq migration is a simple atomic update(of vector
2234 * and cpu destination) of IRTE and flush the hardware cache.
2235 *
2236 * For level triggered, we need to modify the io-apic RTE aswell with the update
2237 * vector information, along with modifying IRTE with vector and destination.
2238 * So irq migration for level triggered is little bit more complex compared to
2239 * edge triggered migration. But the good news is, we use the same algorithm
2240 * for level triggered migration as we have today, only difference being,
2241 * we now initiate the irq migration from process context instead of the
2242 * interrupt context.
2243 *
2244 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2245 * suppression) to the IO-APIC, level triggered irq migration will also be
2246 * as simple as edge triggered migration and we can do the irq migration
2247 * with a simple atomic update to IO-APIC RTE.
2248 */
2249static void migrate_ioapic_irq(int irq, cpumask_t mask)
2250{
2251 struct irq_cfg *cfg;
2252 struct irq_desc *desc;
2253 cpumask_t tmp, cleanup_mask;
2254 struct irte irte;
2255 int modify_ioapic_rte;
2256 unsigned int dest;
2257 unsigned long flags;
2258
2259 cpus_and(tmp, mask, cpu_online_map);
2260 if (cpus_empty(tmp))
2261 return;
2262
2263 if (get_irte(irq, &irte))
2264 return;
2265
2266 if (assign_irq_vector(irq, mask))
2267 return;
2268
2269 cfg = irq_cfg(irq);
2270 cpus_and(tmp, cfg->domain, mask);
2271 dest = cpu_mask_to_apicid(tmp);
2272
2273 desc = irq_to_desc(irq);
2274 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2275 if (modify_ioapic_rte) {
2276 spin_lock_irqsave(&ioapic_lock, flags);
2277 __target_IO_APIC_irq(irq, dest, cfg->vector);
2278 spin_unlock_irqrestore(&ioapic_lock, flags);
2279 }
2280
2281 irte.vector = cfg->vector;
2282 irte.dest_id = IRTE_DEST(dest);
2283
2284 /*
2285 * Modified the IRTE and flushes the Interrupt entry cache.
2286 */
2287 modify_irte(irq, &irte);
2288
2289 if (cfg->move_in_progress) {
2290 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2291 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2292 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2293 cfg->move_in_progress = 0;
2294 }
2295
2296 desc->affinity = mask;
2297}
2298
2299static int migrate_irq_remapped_level(int irq)
2300{
2301 int ret = -1;
2302 struct irq_desc *desc = irq_to_desc(irq);
2303
2304 mask_IO_APIC_irq(irq);
2305
2306 if (io_apic_level_ack_pending(irq)) {
2307 /*
2308 * Interrupt in progress. Migrating irq now will change the
2309 * vector information in the IO-APIC RTE and that will confuse
2310 * the EOI broadcast performed by cpu.
2311 * So, delay the irq migration to the next instance.
2312 */
2313 schedule_delayed_work(&ir_migration_work, 1);
2314 goto unmask;
2315 }
2316
2317 /* everthing is clear. we have right of way */
2318 migrate_ioapic_irq(irq, desc->pending_mask);
2319
2320 ret = 0;
2321 desc->status &= ~IRQ_MOVE_PENDING;
2322 cpus_clear(desc->pending_mask);
2323
2324unmask:
2325 unmask_IO_APIC_irq(irq);
2326 return ret;
2327}
2328
2329static void ir_irq_migration(struct work_struct *work)
2330{
2331 unsigned int irq;
2332 struct irq_desc *desc;
2333
2334 for_each_irq_desc(irq, desc) {
2335 if (desc->status & IRQ_MOVE_PENDING) {
2336 unsigned long flags;
2337
2338 spin_lock_irqsave(&desc->lock, flags);
2339 if (!desc->chip->set_affinity ||
2340 !(desc->status & IRQ_MOVE_PENDING)) {
2341 desc->status &= ~IRQ_MOVE_PENDING;
2342 spin_unlock_irqrestore(&desc->lock, flags);
2343 continue;
2344 }
2345
2346 desc->chip->set_affinity(irq, desc->pending_mask);
2347 spin_unlock_irqrestore(&desc->lock, flags);
2348 }
2349 }
2350}
2351
2352/*
2353 * Migrates the IRQ destination in the process context.
2354 */
2355static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2356{
2357 struct irq_desc *desc = irq_to_desc(irq);
2358
2359 if (desc->status & IRQ_LEVEL) {
2360 desc->status |= IRQ_MOVE_PENDING;
2361 desc->pending_mask = mask;
2362 migrate_irq_remapped_level(irq);
2363 return;
2364 }
2365
2366 migrate_ioapic_irq(irq, mask);
2367}
2368#endif
2369
Yinghai Lu497c9a12008-08-19 20:50:28 -07002370asmlinkage void smp_irq_move_cleanup_interrupt(void)
2371{
2372 unsigned vector, me;
2373 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002374#ifdef CONFIG_X86_64
2375 exit_idle();
2376#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002377 irq_enter();
2378
2379 me = smp_processor_id();
2380 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2381 unsigned int irq;
2382 struct irq_desc *desc;
2383 struct irq_cfg *cfg;
2384 irq = __get_cpu_var(vector_irq)[vector];
2385
2386 desc = irq_to_desc(irq);
2387 if (!desc)
2388 continue;
2389
2390 cfg = irq_cfg(irq);
2391 spin_lock(&desc->lock);
2392 if (!cfg->move_cleanup_count)
2393 goto unlock;
2394
2395 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2396 goto unlock;
2397
2398 __get_cpu_var(vector_irq)[vector] = -1;
2399 cfg->move_cleanup_count--;
2400unlock:
2401 spin_unlock(&desc->lock);
2402 }
2403
2404 irq_exit();
2405}
2406
2407static void irq_complete_move(unsigned int irq)
2408{
2409 struct irq_cfg *cfg = irq_cfg(irq);
2410 unsigned vector, me;
2411
2412 if (likely(!cfg->move_in_progress))
2413 return;
2414
2415 vector = ~get_irq_regs()->orig_ax;
2416 me = smp_processor_id();
2417 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2418 cpumask_t cleanup_mask;
2419
2420 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2421 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2422 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2423 cfg->move_in_progress = 0;
2424 }
2425}
2426#else
2427static inline void irq_complete_move(unsigned int irq) {}
2428#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002429#ifdef CONFIG_INTR_REMAP
2430static void ack_x2apic_level(unsigned int irq)
2431{
2432 ack_x2APIC_irq();
2433}
2434
2435static void ack_x2apic_edge(unsigned int irq)
2436{
2437 ack_x2APIC_irq();
2438}
2439#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002440
Yinghai Lu1d025192008-08-19 20:50:34 -07002441static void ack_apic_edge(unsigned int irq)
2442{
2443 irq_complete_move(irq);
2444 move_native_irq(irq);
2445 ack_APIC_irq();
2446}
2447
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002448#ifdef CONFIG_X86_32
2449atomic_t irq_mis_count;
2450#endif
2451
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002452static void ack_apic_level(unsigned int irq)
2453{
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002454#ifdef CONFIG_X86_32
2455 unsigned long v;
2456 int i;
2457#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002458 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002459
Ingo Molnar54168ed2008-08-20 09:07:45 +02002460 irq_complete_move(irq);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002461#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002462 /* If we are moving the irq we need to mask it */
2463 if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2464 do_unmask_irq = 1;
2465 mask_IO_APIC_irq(irq);
2466 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002467#endif
2468
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002469#ifdef CONFIG_X86_32
2470 /*
2471 * It appears there is an erratum which affects at least version 0x11
2472 * of I/O APIC (that's the 82093AA and cores integrated into various
2473 * chipsets). Under certain conditions a level-triggered interrupt is
2474 * erroneously delivered as edge-triggered one but the respective IRR
2475 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2476 * message but it will never arrive and further interrupts are blocked
2477 * from the source. The exact reason is so far unknown, but the
2478 * phenomenon was observed when two consecutive interrupt requests
2479 * from a given source get delivered to the same CPU and the source is
2480 * temporarily disabled in between.
2481 *
2482 * A workaround is to simulate an EOI message manually. We achieve it
2483 * by setting the trigger mode to edge and then to level when the edge
2484 * trigger mode gets detected in the TMR of a local APIC for a
2485 * level-triggered interrupt. We mask the source for the time of the
2486 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2487 * The idea is from Manfred Spraul. --macro
2488 */
2489 i = irq_cfg(irq)->vector;
2490
2491 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2492#endif
2493
Ingo Molnar54168ed2008-08-20 09:07:45 +02002494 /*
2495 * We must acknowledge the irq before we move it or the acknowledge will
2496 * not propagate properly.
2497 */
2498 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002499
Ingo Molnar54168ed2008-08-20 09:07:45 +02002500 /* Now we can move and renable the irq */
2501 if (unlikely(do_unmask_irq)) {
2502 /* Only migrate the irq if the ack has been received.
2503 *
2504 * On rare occasions the broadcast level triggered ack gets
2505 * delayed going to ioapics, and if we reprogram the
2506 * vector while Remote IRR is still set the irq will never
2507 * fire again.
2508 *
2509 * To prevent this scenario we read the Remote IRR bit
2510 * of the ioapic. This has two effects.
2511 * - On any sane system the read of the ioapic will
2512 * flush writes (and acks) going to the ioapic from
2513 * this cpu.
2514 * - We get to see if the ACK has actually been delivered.
2515 *
2516 * Based on failed experiments of reprogramming the
2517 * ioapic entry from outside of irq context starting
2518 * with masking the ioapic entry and then polling until
2519 * Remote IRR was clear before reprogramming the
2520 * ioapic I don't trust the Remote IRR bit to be
2521 * completey accurate.
2522 *
2523 * However there appears to be no other way to plug
2524 * this race, so if the Remote IRR bit is not
2525 * accurate and is causing problems then it is a hardware bug
2526 * and you can go talk to the chipset vendor about it.
2527 */
2528 if (!io_apic_level_ack_pending(irq))
2529 move_masked_irq(irq);
2530 unmask_IO_APIC_irq(irq);
2531 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002532
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002533#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002534 if (!(v & (1 << (i & 0x1f)))) {
2535 atomic_inc(&irq_mis_count);
2536 spin_lock(&ioapic_lock);
2537 __mask_and_edge_IO_APIC_irq(irq);
2538 __unmask_and_level_IO_APIC_irq(irq);
2539 spin_unlock(&ioapic_lock);
2540 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002541#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002542}
Yinghai Lu1d025192008-08-19 20:50:34 -07002543
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002544static struct irq_chip ioapic_chip __read_mostly = {
2545 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002546 .startup = startup_ioapic_irq,
2547 .mask = mask_IO_APIC_irq,
2548 .unmask = unmask_IO_APIC_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07002549 .ack = ack_apic_edge,
2550 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002551#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002552 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002553#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002554 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555};
2556
Ingo Molnar54168ed2008-08-20 09:07:45 +02002557#ifdef CONFIG_INTR_REMAP
2558static struct irq_chip ir_ioapic_chip __read_mostly = {
2559 .name = "IR-IO-APIC",
2560 .startup = startup_ioapic_irq,
2561 .mask = mask_IO_APIC_irq,
2562 .unmask = unmask_IO_APIC_irq,
2563 .ack = ack_x2apic_edge,
2564 .eoi = ack_x2apic_level,
2565#ifdef CONFIG_SMP
2566 .set_affinity = set_ir_ioapic_affinity_irq,
2567#endif
2568 .retrigger = ioapic_retrigger_irq,
2569};
2570#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
2572static inline void init_IO_APIC_traps(void)
2573{
2574 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002575 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002576 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578 /*
2579 * NOTE! The local APIC isn't very good at handling
2580 * multiple interrupts at the same interrupt level.
2581 * As the interrupt level is determined by taking the
2582 * vector number and shifting that right by 4, we
2583 * want to spread these out a bit so that they don't
2584 * all fall in the same interrupt level.
2585 *
2586 * Also, we've got to be careful not to trash gate
2587 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2588 */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07002589 for_each_irq_cfg(irq, cfg) {
Yinghai Luda51a822008-08-19 20:50:25 -07002590 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 /*
2592 * Hmm.. We don't have an entry for this,
2593 * so default to an old-fashioned 8259
2594 * interrupt if we can..
2595 */
2596 if (irq < 16)
2597 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002598 else {
2599 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002601 desc->chip = &no_irq_chip;
2602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
2604 }
2605}
2606
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002607/*
2608 * The local APIC irq-chip implementation:
2609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002611static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612{
2613 unsigned long v;
2614
2615 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002616 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002619static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002621 unsigned long v;
2622
2623 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002624 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
Ingo Molnar54168ed2008-08-20 09:07:45 +02002627static void ack_lapic_irq (unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002628{
2629 ack_APIC_irq();
2630}
2631
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002632static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002633 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002634 .mask = mask_lapic_irq,
2635 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002636 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637};
2638
Yinghai Lu497c9a12008-08-19 20:50:28 -07002639static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002640{
Yinghai Lu08678b02008-08-19 20:50:05 -07002641 struct irq_desc *desc;
2642
2643 desc = irq_to_desc(irq);
2644 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002645 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2646 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002647}
2648
Jan Beuliche9427102008-01-30 13:31:24 +01002649static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
2651 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002652 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 * We put the 8259A master into AEOI mode and
2654 * unmask on all local APICs LVT0 as NMI.
2655 *
2656 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2657 * is from Maciej W. Rozycki - so we do not have to EOI from
2658 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002659 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2661
Jan Beuliche9427102008-01-30 13:31:24 +01002662 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 apic_printk(APIC_VERBOSE, " done.\n");
2665}
2666
2667/*
2668 * This looks a bit hackish but it's about the only one way of sending
2669 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2670 * not support the ExtINT mode, unfortunately. We need to send these
2671 * cycles as some i82489DX-based boards have glue logic that keeps the
2672 * 8259A interrupt line asserted until INTA. --macro
2673 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002674static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002676 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 struct IO_APIC_route_entry entry0, entry1;
2678 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002680 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002681 if (pin == -1) {
2682 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002684 }
2685 apic = find_isa_irq_apic(8, mp_INT);
2686 if (apic == -1) {
2687 WARN_ON_ONCE(1);
2688 return;
2689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Andi Kleencf4c6a22006-09-26 10:52:30 +02002691 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002692 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 memset(&entry1, 0, sizeof(entry1));
2695
2696 entry1.dest_mode = 0; /* physical delivery */
2697 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002698 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 entry1.delivery_mode = dest_ExtINT;
2700 entry1.polarity = entry0.polarity;
2701 entry1.trigger = 0;
2702 entry1.vector = 0;
2703
Andi Kleencf4c6a22006-09-26 10:52:30 +02002704 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
2706 save_control = CMOS_READ(RTC_CONTROL);
2707 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2708 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2709 RTC_FREQ_SELECT);
2710 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2711
2712 i = 100;
2713 while (i-- > 0) {
2714 mdelay(10);
2715 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2716 i -= 10;
2717 }
2718
2719 CMOS_WRITE(save_control, RTC_CONTROL);
2720 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002721 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
Andi Kleencf4c6a22006-09-26 10:52:30 +02002723 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Yinghai Luefa25592008-08-19 20:50:36 -07002726static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002727/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002728static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002729{
2730 disable_timer_pin_1 = 1;
2731 return 0;
2732}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002733early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002734
2735int timer_through_8259 __initdata;
2736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737/*
2738 * This code may look a bit paranoid, but it's supposed to cooperate with
2739 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2740 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2741 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002742 *
2743 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002745static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002747 struct irq_cfg *cfg = irq_cfg(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002748 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002749 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002750 unsigned int ver;
2751 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002752
2753 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002754
Ingo Molnar54168ed2008-08-20 09:07:45 +02002755 ver = apic_read(APIC_LVR);
2756 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 /*
2759 * get/set the timer IRQ vector:
2760 */
2761 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002762 assign_irq_vector(0, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002765 * As IRQ0 is to be enabled in the 8259A, the virtual
2766 * wire has to be disabled in the local APIC. Also
2767 * timer interrupts need to be acknowledged manually in
2768 * the 8259A for the i82489DX when using the NMI
2769 * watchdog as that APIC treats NMIs as level-triggered.
2770 * The AEOI mode will finish them in the 8259A
2771 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002773 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002775#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002776 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002777#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002779 pin1 = find_isa_irq_pin(0, mp_INT);
2780 apic1 = find_isa_irq_apic(0, mp_INT);
2781 pin2 = ioapic_i8259.pin;
2782 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002784 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2785 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002786 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002788 /*
2789 * Some BIOS writers are clueless and report the ExtINTA
2790 * I/O APIC input from the cascaded 8259A as the timer
2791 * interrupt input. So just in case, if only one pin
2792 * was found above, try it both directly and through the
2793 * 8259A.
2794 */
2795 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002796#ifdef CONFIG_INTR_REMAP
2797 if (intr_remapping_enabled)
2798 panic("BIOS bug: timer not connected to IO-APIC");
2799#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002800 pin1 = pin2;
2801 apic1 = apic2;
2802 no_pin1 = 1;
2803 } else if (pin2 == -1) {
2804 pin2 = pin1;
2805 apic2 = apic1;
2806 }
2807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (pin1 != -1) {
2809 /*
2810 * Ok, does IRQ0 through the IOAPIC work?
2811 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002812 if (no_pin1) {
2813 add_pin_to_irq(0, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002814 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 unmask_IO_APIC_irq(0);
2817 if (timer_irq_works()) {
2818 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 setup_nmi();
2820 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002822 if (disable_timer_pin_1 > 0)
2823 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002826#ifdef CONFIG_INTR_REMAP
2827 if (intr_remapping_enabled)
2828 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2829#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002830 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002831 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002832 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2833 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002835 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2836 "(IRQ0) through the 8259A ...\n");
2837 apic_printk(APIC_QUIET, KERN_INFO
2838 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 /*
2840 * legacy devices should be connected to IO APIC #0
2841 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002842 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002843 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002844 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002845 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002847 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002848 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002850 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002852 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002854 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856 /*
2857 * Cleanup, just in case ...
2858 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002859 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002860 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002861 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
2864 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002865 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2866 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002867 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002869#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002870 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002871#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002873 apic_printk(APIC_QUIET, KERN_INFO
2874 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Yinghai Lu497c9a12008-08-19 20:50:28 -07002876 lapic_register_intr(0);
2877 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 enable_8259A_irq(0);
2879
2880 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002881 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002882 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002884 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002885 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002886 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002888 apic_printk(APIC_QUIET, KERN_INFO
2889 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 init_8259A(0);
2892 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002893 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
2895 unlock_ExtINT_logic();
2896
2897 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002898 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002899 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002901 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002903 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002904out:
2905 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
2908/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002909 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2910 * to devices. However there may be an I/O APIC pin available for
2911 * this interrupt regardless. The pin may be left unconnected, but
2912 * typically it will be reused as an ExtINT cascade interrupt for
2913 * the master 8259A. In the MPS case such a pin will normally be
2914 * reported as an ExtINT interrupt in the MP table. With ACPI
2915 * there is no provision for ExtINT interrupts, and in the absence
2916 * of an override it would be treated as an ordinary ISA I/O APIC
2917 * interrupt, that is edge-triggered and unmasked by default. We
2918 * used to do this, but it caused problems on some systems because
2919 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2920 * the same ExtINT cascade interrupt to drive the local APIC of the
2921 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2922 * the I/O APIC in all cases now. No actual device should request
2923 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 */
2925#define PIC_IRQS (1 << PIC_CASCADE_IR)
2926
2927void __init setup_IO_APIC(void)
2928{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002929
2930#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002932#else
2933 /*
2934 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2935 */
2936#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002938 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Ingo Molnar54168ed2008-08-20 09:07:45 +02002940 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2941 /*
2942 * Set up IO-APIC IRQ routing.
2943 */
2944#ifdef CONFIG_X86_32
2945 if (!acpi_ioapic)
2946 setup_ioapic_ids_from_mpc();
2947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 sync_Arb_IDs();
2949 setup_IO_APIC_irqs();
2950 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002951 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
2954/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002955 * Called after all the initialization is done. If we didnt find any
2956 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959static int __init io_apic_bug_finalize(void)
2960{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002961 if (sis_apic_bug == -1)
2962 sis_apic_bug = 0;
2963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964}
2965
2966late_initcall(io_apic_bug_finalize);
2967
2968struct sysfs_ioapic_data {
2969 struct sys_device dev;
2970 struct IO_APIC_route_entry entry[0];
2971};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002972static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
Pavel Machek438510f2005-04-16 15:25:24 -07002974static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
2976 struct IO_APIC_route_entry *entry;
2977 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 data = container_of(dev, struct sysfs_ioapic_data, dev);
2981 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002982 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2983 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 return 0;
2986}
2987
2988static int ioapic_resume(struct sys_device *dev)
2989{
2990 struct IO_APIC_route_entry *entry;
2991 struct sysfs_ioapic_data *data;
2992 unsigned long flags;
2993 union IO_APIC_reg_00 reg_00;
2994 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002995
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 data = container_of(dev, struct sysfs_ioapic_data, dev);
2997 entry = data->entry;
2998
2999 spin_lock_irqsave(&ioapic_lock, flags);
3000 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04003001 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
3002 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 io_apic_write(dev->id, 0, reg_00.raw);
3004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003006 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003007 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008
3009 return 0;
3010}
3011
3012static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003013 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 .suspend = ioapic_suspend,
3015 .resume = ioapic_resume,
3016};
3017
3018static int __init ioapic_init_sysfs(void)
3019{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003020 struct sys_device * dev;
3021 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 error = sysdev_class_register(&ioapic_sysdev_class);
3024 if (error)
3025 return error;
3026
Ingo Molnar54168ed2008-08-20 09:07:45 +02003027 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003028 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003030 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (!mp_ioapic_data[i]) {
3032 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3033 continue;
3034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003036 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 dev->cls = &ioapic_sysdev_class;
3038 error = sysdev_register(dev);
3039 if (error) {
3040 kfree(mp_ioapic_data[i]);
3041 mp_ioapic_data[i] = NULL;
3042 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3043 continue;
3044 }
3045 }
3046
3047 return 0;
3048}
3049
3050device_initcall(ioapic_init_sysfs);
3051
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003052/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003053 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003054 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003055unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003056{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003057 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003058 unsigned int irq;
3059 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003060 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07003061 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003062
Yinghai Lu497c9a12008-08-19 20:50:28 -07003063#ifndef CONFIG_HAVE_SPARSE_IRQ
Yinghai Lu199751d2008-08-19 20:50:27 -07003064 irq_want = nr_irqs - 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003065#endif
Yinghai Lu199751d2008-08-19 20:50:27 -07003066
3067 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003068 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003069 for (new = irq_want; new > 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003070 if (platform_legacy_irq(new))
3071 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07003072 cfg_new = irq_cfg(new);
3073 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003074 continue;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003075 /* check if need to create one */
Yinghai Luda51a822008-08-19 20:50:25 -07003076 if (!cfg_new)
3077 cfg_new = irq_cfg_alloc(new);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003078 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003079 irq = new;
3080 break;
3081 }
3082 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003083
Yinghai Lu199751d2008-08-19 20:50:27 -07003084 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003085 dynamic_irq_init(irq);
3086 }
3087 return irq;
3088}
3089
Yinghai Lu199751d2008-08-19 20:50:27 -07003090int create_irq(void)
3091{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003092 int irq;
3093
3094 irq = create_irq_nr(nr_irqs - 1);
3095
3096 if (irq == 0)
3097 irq = -1;
3098
3099 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003100}
3101
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003102void destroy_irq(unsigned int irq)
3103{
3104 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003105
3106 dynamic_irq_cleanup(irq);
3107
Ingo Molnar54168ed2008-08-20 09:07:45 +02003108#ifdef CONFIG_INTR_REMAP
3109 free_irte(irq);
3110#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003111 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003112 __clear_irq_vector(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003113 spin_unlock_irqrestore(&vector_lock, flags);
3114}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003115
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003116/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003117 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003118 */
3119#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003120static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003121{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003122 struct irq_cfg *cfg;
3123 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003124 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003125 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003126
Yinghai Lu497c9a12008-08-19 20:50:28 -07003127 tmp = TARGET_CPUS;
3128 err = assign_irq_vector(irq, tmp);
3129 if (err)
3130 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003131
Yinghai Lu497c9a12008-08-19 20:50:28 -07003132 cfg = irq_cfg(irq);
3133 cpus_and(tmp, cfg->domain, tmp);
3134 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003135
Ingo Molnar54168ed2008-08-20 09:07:45 +02003136#ifdef CONFIG_INTR_REMAP
3137 if (irq_remapped(irq)) {
3138 struct irte irte;
3139 int ir_index;
3140 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003141
Ingo Molnar54168ed2008-08-20 09:07:45 +02003142 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3143 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003144
Ingo Molnar54168ed2008-08-20 09:07:45 +02003145 memset (&irte, 0, sizeof(irte));
3146
3147 irte.present = 1;
3148 irte.dst_mode = INT_DEST_MODE;
3149 irte.trigger_mode = 0; /* edge */
3150 irte.dlvry_mode = INT_DELIVERY_MODE;
3151 irte.vector = cfg->vector;
3152 irte.dest_id = IRTE_DEST(dest);
3153
3154 modify_irte(irq, &irte);
3155
3156 msg->address_hi = MSI_ADDR_BASE_HI;
3157 msg->data = sub_handle;
3158 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3159 MSI_ADDR_IR_SHV |
3160 MSI_ADDR_IR_INDEX1(ir_index) |
3161 MSI_ADDR_IR_INDEX2(ir_index);
3162 } else
3163#endif
3164 {
3165 msg->address_hi = MSI_ADDR_BASE_HI;
3166 msg->address_lo =
3167 MSI_ADDR_BASE_LO |
3168 ((INT_DEST_MODE == 0) ?
3169 MSI_ADDR_DEST_MODE_PHYSICAL:
3170 MSI_ADDR_DEST_MODE_LOGICAL) |
3171 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3172 MSI_ADDR_REDIRECTION_CPU:
3173 MSI_ADDR_REDIRECTION_LOWPRI) |
3174 MSI_ADDR_DEST_ID(dest);
3175
3176 msg->data =
3177 MSI_DATA_TRIGGER_EDGE |
3178 MSI_DATA_LEVEL_ASSERT |
3179 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3180 MSI_DATA_DELIVERY_FIXED:
3181 MSI_DATA_DELIVERY_LOWPRI) |
3182 MSI_DATA_VECTOR(cfg->vector);
3183 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003184 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003185}
3186
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003187#ifdef CONFIG_SMP
3188static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3189{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003190 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003191 struct msi_msg msg;
3192 unsigned int dest;
3193 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003194 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003195
3196 cpus_and(tmp, mask, cpu_online_map);
3197 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003198 return;
3199
Yinghai Lu497c9a12008-08-19 20:50:28 -07003200 if (assign_irq_vector(irq, mask))
3201 return;
3202
3203 cfg = irq_cfg(irq);
3204 cpus_and(tmp, cfg->domain, mask);
3205 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003206
3207 read_msi_msg(irq, &msg);
3208
3209 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003210 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003211 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3212 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3213
3214 write_msi_msg(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003215 desc = irq_to_desc(irq);
3216 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003217}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003218
3219#ifdef CONFIG_INTR_REMAP
3220/*
3221 * Migrate the MSI irq to another cpumask. This migration is
3222 * done in the process context using interrupt-remapping hardware.
3223 */
3224static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3225{
3226 struct irq_cfg *cfg;
3227 unsigned int dest;
3228 cpumask_t tmp, cleanup_mask;
3229 struct irte irte;
3230 struct irq_desc *desc;
3231
3232 cpus_and(tmp, mask, cpu_online_map);
3233 if (cpus_empty(tmp))
3234 return;
3235
3236 if (get_irte(irq, &irte))
3237 return;
3238
3239 if (assign_irq_vector(irq, mask))
3240 return;
3241
3242 cfg = irq_cfg(irq);
3243 cpus_and(tmp, cfg->domain, mask);
3244 dest = cpu_mask_to_apicid(tmp);
3245
3246 irte.vector = cfg->vector;
3247 irte.dest_id = IRTE_DEST(dest);
3248
3249 /*
3250 * atomically update the IRTE with the new destination and vector.
3251 */
3252 modify_irte(irq, &irte);
3253
3254 /*
3255 * After this point, all the interrupts will start arriving
3256 * at the new destination. So, time to cleanup the previous
3257 * vector allocation.
3258 */
3259 if (cfg->move_in_progress) {
3260 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3261 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3262 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3263 cfg->move_in_progress = 0;
3264 }
3265
3266 desc = irq_to_desc(irq);
3267 desc->affinity = mask;
3268}
3269#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003270#endif /* CONFIG_SMP */
3271
3272/*
3273 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3274 * which implement the MSI or MSI-X Capability Structure.
3275 */
3276static struct irq_chip msi_chip = {
3277 .name = "PCI-MSI",
3278 .unmask = unmask_msi_irq,
3279 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003280 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003281#ifdef CONFIG_SMP
3282 .set_affinity = set_msi_irq_affinity,
3283#endif
3284 .retrigger = ioapic_retrigger_irq,
3285};
3286
Ingo Molnar54168ed2008-08-20 09:07:45 +02003287#ifdef CONFIG_INTR_REMAP
3288static struct irq_chip msi_ir_chip = {
3289 .name = "IR-PCI-MSI",
3290 .unmask = unmask_msi_irq,
3291 .mask = mask_msi_irq,
3292 .ack = ack_x2apic_edge,
3293#ifdef CONFIG_SMP
3294 .set_affinity = ir_set_msi_irq_affinity,
3295#endif
3296 .retrigger = ioapic_retrigger_irq,
3297};
3298
3299/*
3300 * Map the PCI dev to the corresponding remapping hardware unit
3301 * and allocate 'nvec' consecutive interrupt-remapping table entries
3302 * in it.
3303 */
3304static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3305{
3306 struct intel_iommu *iommu;
3307 int index;
3308
3309 iommu = map_dev_to_ir(dev);
3310 if (!iommu) {
3311 printk(KERN_ERR
3312 "Unable to map PCI %s to iommu\n", pci_name(dev));
3313 return -ENOENT;
3314 }
3315
3316 index = alloc_irte(iommu, irq, nvec);
3317 if (index < 0) {
3318 printk(KERN_ERR
3319 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3320 pci_name(dev));
3321 return -ENOSPC;
3322 }
3323 return index;
3324}
3325#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003326
3327static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3328{
3329 int ret;
3330 struct msi_msg msg;
3331
3332 ret = msi_compose_msg(dev, irq, &msg);
3333 if (ret < 0)
3334 return ret;
3335
3336 set_irq_msi(irq, desc);
3337 write_msi_msg(irq, &msg);
3338
Ingo Molnar54168ed2008-08-20 09:07:45 +02003339#ifdef CONFIG_INTR_REMAP
3340 if (irq_remapped(irq)) {
3341 struct irq_desc *desc = irq_to_desc(irq);
3342 /*
3343 * irq migration in process context
3344 */
3345 desc->status |= IRQ_MOVE_PCNTXT;
3346 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3347 } else
3348#endif
3349 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003350
3351 return 0;
3352}
3353
Yinghai Lu199751d2008-08-19 20:50:27 -07003354static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3355{
3356 unsigned int irq;
3357
3358 irq = dev->bus->number;
3359 irq <<= 8;
3360 irq |= dev->devfn;
3361 irq <<= 12;
3362
3363 return irq;
3364}
3365
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003366int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003367{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003368 unsigned int irq;
3369 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003370 unsigned int irq_want;
3371
3372 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3373
3374 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003375 if (irq == 0)
3376 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003377
Ingo Molnar54168ed2008-08-20 09:07:45 +02003378#ifdef CONFIG_INTR_REMAP
3379 if (!intr_remapping_enabled)
3380 goto no_ir;
3381
3382 ret = msi_alloc_irte(dev, irq, 1);
3383 if (ret < 0)
3384 goto error;
3385no_ir:
3386#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003387 ret = setup_msi_irq(dev, desc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003388 if (ret < 0) {
3389 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003390 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003391 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003392 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003393
3394#ifdef CONFIG_INTR_REMAP
3395error:
3396 destroy_irq(irq);
3397 return ret;
3398#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003399}
3400
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003401int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3402{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003403 unsigned int irq;
3404 int ret, sub_handle;
3405 struct msi_desc *desc;
3406 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003407
Ingo Molnar54168ed2008-08-20 09:07:45 +02003408#ifdef CONFIG_INTR_REMAP
3409 struct intel_iommu *iommu = 0;
3410 int index = 0;
3411#endif
3412
3413 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3414 sub_handle = 0;
3415 list_for_each_entry(desc, &dev->msi_list, list) {
3416 irq = create_irq_nr(irq_want--);
3417 if (irq == 0)
3418 return -1;
3419#ifdef CONFIG_INTR_REMAP
3420 if (!intr_remapping_enabled)
3421 goto no_ir;
3422
3423 if (!sub_handle) {
3424 /*
3425 * allocate the consecutive block of IRTE's
3426 * for 'nvec'
3427 */
3428 index = msi_alloc_irte(dev, irq, nvec);
3429 if (index < 0) {
3430 ret = index;
3431 goto error;
3432 }
3433 } else {
3434 iommu = map_dev_to_ir(dev);
3435 if (!iommu) {
3436 ret = -ENOENT;
3437 goto error;
3438 }
3439 /*
3440 * setup the mapping between the irq and the IRTE
3441 * base index, the sub_handle pointing to the
3442 * appropriate interrupt remap table entry.
3443 */
3444 set_irte_irq(irq, iommu, index, sub_handle);
3445 }
3446no_ir:
3447#endif
3448 ret = setup_msi_irq(dev, desc, irq);
3449 if (ret < 0)
3450 goto error;
3451 sub_handle++;
3452 }
3453 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003454
3455error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003456 destroy_irq(irq);
3457 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003458}
3459
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003460void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003461{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003462 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003463}
3464
Ingo Molnar54168ed2008-08-20 09:07:45 +02003465#ifdef CONFIG_DMAR
3466#ifdef CONFIG_SMP
3467static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3468{
3469 struct irq_cfg *cfg;
3470 struct msi_msg msg;
3471 unsigned int dest;
3472 cpumask_t tmp;
3473 struct irq_desc *desc;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003474
Ingo Molnar54168ed2008-08-20 09:07:45 +02003475 cpus_and(tmp, mask, cpu_online_map);
3476 if (cpus_empty(tmp))
3477 return;
3478
3479 if (assign_irq_vector(irq, mask))
3480 return;
3481
3482 cfg = irq_cfg(irq);
3483 cpus_and(tmp, cfg->domain, mask);
3484 dest = cpu_mask_to_apicid(tmp);
3485
3486 dmar_msi_read(irq, &msg);
3487
3488 msg.data &= ~MSI_DATA_VECTOR_MASK;
3489 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3490 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3491 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3492
3493 dmar_msi_write(irq, &msg);
3494 desc = irq_to_desc(irq);
3495 desc->affinity = mask;
3496}
3497#endif /* CONFIG_SMP */
3498
3499struct irq_chip dmar_msi_type = {
3500 .name = "DMAR_MSI",
3501 .unmask = dmar_msi_unmask,
3502 .mask = dmar_msi_mask,
3503 .ack = ack_apic_edge,
3504#ifdef CONFIG_SMP
3505 .set_affinity = dmar_msi_set_affinity,
3506#endif
3507 .retrigger = ioapic_retrigger_irq,
3508};
3509
3510int arch_setup_dmar_msi(unsigned int irq)
3511{
3512 int ret;
3513 struct msi_msg msg;
3514
3515 ret = msi_compose_msg(NULL, irq, &msg);
3516 if (ret < 0)
3517 return ret;
3518 dmar_msi_write(irq, &msg);
3519 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3520 "edge");
3521 return 0;
3522}
3523#endif
3524
3525#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003526/*
3527 * Hypertransport interrupt support
3528 */
3529#ifdef CONFIG_HT_IRQ
3530
3531#ifdef CONFIG_SMP
3532
Yinghai Lu497c9a12008-08-19 20:50:28 -07003533static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003534{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003535 struct ht_irq_msg msg;
3536 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003537
Yinghai Lu497c9a12008-08-19 20:50:28 -07003538 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003539 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003540
Yinghai Lu497c9a12008-08-19 20:50:28 -07003541 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003542 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003543
Eric W. Biedermanec683072006-11-08 17:44:57 -08003544 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003545}
3546
3547static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3548{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003549 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003550 unsigned int dest;
3551 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003552 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003553
3554 cpus_and(tmp, mask, cpu_online_map);
3555 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003556 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003557
Yinghai Lu497c9a12008-08-19 20:50:28 -07003558 if (assign_irq_vector(irq, mask))
3559 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003560
Yinghai Lu497c9a12008-08-19 20:50:28 -07003561 cfg = irq_cfg(irq);
3562 cpus_and(tmp, cfg->domain, mask);
3563 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003564
Yinghai Lu497c9a12008-08-19 20:50:28 -07003565 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003566 desc = irq_to_desc(irq);
3567 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003568}
3569#endif
3570
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003571static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003572 .name = "PCI-HT",
3573 .mask = mask_ht_irq,
3574 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003575 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003576#ifdef CONFIG_SMP
3577 .set_affinity = set_ht_irq_affinity,
3578#endif
3579 .retrigger = ioapic_retrigger_irq,
3580};
3581
3582int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3583{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003584 struct irq_cfg *cfg;
3585 int err;
3586 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003587
Yinghai Lu497c9a12008-08-19 20:50:28 -07003588 tmp = TARGET_CPUS;
3589 err = assign_irq_vector(irq, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003590 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003591 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003592 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003593
Yinghai Lu497c9a12008-08-19 20:50:28 -07003594 cfg = irq_cfg(irq);
3595 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003596 dest = cpu_mask_to_apicid(tmp);
3597
Eric W. Biedermanec683072006-11-08 17:44:57 -08003598 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003599
Eric W. Biedermanec683072006-11-08 17:44:57 -08003600 msg.address_lo =
3601 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003602 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003603 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003604 ((INT_DEST_MODE == 0) ?
3605 HT_IRQ_LOW_DM_PHYSICAL :
3606 HT_IRQ_LOW_DM_LOGICAL) |
3607 HT_IRQ_LOW_RQEOI_EDGE |
3608 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3609 HT_IRQ_LOW_MT_FIXED :
3610 HT_IRQ_LOW_MT_ARBITRATED) |
3611 HT_IRQ_LOW_IRQ_MASKED;
3612
Eric W. Biedermanec683072006-11-08 17:44:57 -08003613 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003614
Ingo Molnara460e742006-10-17 00:10:03 -07003615 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3616 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003617 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003618 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003619}
3620#endif /* CONFIG_HT_IRQ */
3621
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003622int __init io_apic_get_redir_entries (int ioapic)
3623{
3624 union IO_APIC_reg_01 reg_01;
3625 unsigned long flags;
3626
3627 spin_lock_irqsave(&ioapic_lock, flags);
3628 reg_01.raw = io_apic_read(ioapic, 1);
3629 spin_unlock_irqrestore(&ioapic_lock, flags);
3630
3631 return reg_01.bits.entries;
3632}
3633
3634int __init probe_nr_irqs(void)
3635{
3636 int idx;
3637 int nr = 0;
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003638#ifndef CONFIG_XEN
3639 int nr_min = 32;
3640#else
3641 int nr_min = NR_IRQS;
3642#endif
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003643
3644 for (idx = 0; idx < nr_ioapics; idx++)
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003645 nr += io_apic_get_redir_entries(idx) + 1;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003646
3647 /* double it for hotplug and msi and nmi */
3648 nr <<= 1;
3649
3650 /* something wrong ? */
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003651 if (nr < nr_min)
3652 nr = nr_min;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003653
3654 return nr;
3655}
3656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003658 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 -------------------------------------------------------------------------- */
3660
Len Brown888ba6c2005-08-24 12:07:20 -04003661#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
Ingo Molnar54168ed2008-08-20 09:07:45 +02003663#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003664int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
3666 union IO_APIC_reg_00 reg_00;
3667 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3668 physid_mask_t tmp;
3669 unsigned long flags;
3670 int i = 0;
3671
3672 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003673 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3674 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003676 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3678 * advantage of new APIC bus architecture.
3679 */
3680
3681 if (physids_empty(apic_id_map))
3682 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3683
3684 spin_lock_irqsave(&ioapic_lock, flags);
3685 reg_00.raw = io_apic_read(ioapic, 0);
3686 spin_unlock_irqrestore(&ioapic_lock, flags);
3687
3688 if (apic_id >= get_physical_broadcast()) {
3689 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3690 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3691 apic_id = reg_00.bits.ID;
3692 }
3693
3694 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003695 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 * 'stuck on smp_invalidate_needed IPI wait' messages.
3697 */
3698 if (check_apicid_used(apic_id_map, apic_id)) {
3699
3700 for (i = 0; i < get_physical_broadcast(); i++) {
3701 if (!check_apicid_used(apic_id_map, i))
3702 break;
3703 }
3704
3705 if (i == get_physical_broadcast())
3706 panic("Max apic_id exceeded!\n");
3707
3708 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3709 "trying %d\n", ioapic, apic_id, i);
3710
3711 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713
3714 tmp = apicid_to_cpu_present(apic_id);
3715 physids_or(apic_id_map, apic_id_map, tmp);
3716
3717 if (reg_00.bits.ID != apic_id) {
3718 reg_00.bits.ID = apic_id;
3719
3720 spin_lock_irqsave(&ioapic_lock, flags);
3721 io_apic_write(ioapic, 0, reg_00.raw);
3722 reg_00.raw = io_apic_read(ioapic, 0);
3723 spin_unlock_irqrestore(&ioapic_lock, flags);
3724
3725 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003726 if (reg_00.bits.ID != apic_id) {
3727 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3728 return -1;
3729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 }
3731
3732 apic_printk(APIC_VERBOSE, KERN_INFO
3733 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3734
3735 return apic_id;
3736}
3737
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003738int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
3740 union IO_APIC_reg_01 reg_01;
3741 unsigned long flags;
3742
3743 spin_lock_irqsave(&ioapic_lock, flags);
3744 reg_01.raw = io_apic_read(ioapic, 1);
3745 spin_unlock_irqrestore(&ioapic_lock, flags);
3746
3747 return reg_01.bits.version;
3748}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750
Ingo Molnar54168ed2008-08-20 09:07:45 +02003751int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003754 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 ioapic);
3756 return -EINVAL;
3757 }
3758
3759 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 * IRQs < 16 are already in the irq_2_pin[] map
3761 */
3762 if (irq >= 16)
3763 add_pin_to_irq(irq, ioapic, pin);
3764
Yinghai Lu497c9a12008-08-19 20:50:28 -07003765 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
3767 return 0;
3768}
3769
Ingo Molnar54168ed2008-08-20 09:07:45 +02003770
Shaohua Li61fd47e2007-11-17 01:05:28 -05003771int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3772{
3773 int i;
3774
3775 if (skip_ioapic_setup)
3776 return -1;
3777
3778 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003779 if (mp_irqs[i].mp_irqtype == mp_INT &&
3780 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003781 break;
3782 if (i >= mp_irq_entries)
3783 return -1;
3784
3785 *trigger = irq_trigger(i);
3786 *polarity = irq_polarity(i);
3787 return 0;
3788}
3789
Len Brown888ba6c2005-08-24 12:07:20 -04003790#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003791
Yinghai Lu497c9a12008-08-19 20:50:28 -07003792/*
3793 * This function currently is only a helper for the i386 smp boot process where
3794 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3795 * so mask in all cases should simply be TARGET_CPUS
3796 */
3797#ifdef CONFIG_SMP
3798void __init setup_ioapic_dest(void)
3799{
3800 int pin, ioapic, irq, irq_entry;
3801 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003802
3803 if (skip_ioapic_setup == 1)
3804 return;
3805
3806 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3807 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3808 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3809 if (irq_entry == -1)
3810 continue;
3811 irq = pin_2_irq(irq_entry, ioapic, pin);
3812
3813 /* setup_IO_APIC_irqs could fail to get vector for some device
3814 * when you have too many devices, because at that time only boot
3815 * cpu is online.
3816 */
3817 cfg = irq_cfg(irq);
3818 if (!cfg->vector)
3819 setup_IO_APIC_irq(ioapic, pin, irq,
3820 irq_trigger(irq_entry),
3821 irq_polarity(irq_entry));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003822#ifdef CONFIG_INTR_REMAP
3823 else if (intr_remapping_enabled)
3824 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
3825#endif
3826 else
Yinghai Lu497c9a12008-08-19 20:50:28 -07003827 set_ioapic_affinity_irq(irq, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003828 }
3829
3830 }
3831}
3832#endif
3833
Ingo Molnar54168ed2008-08-20 09:07:45 +02003834#define IOAPIC_RESOURCE_NAME_SIZE 11
3835
3836static struct resource *ioapic_resources;
3837
3838static struct resource * __init ioapic_setup_resources(void)
3839{
3840 unsigned long n;
3841 struct resource *res;
3842 char *mem;
3843 int i;
3844
3845 if (nr_ioapics <= 0)
3846 return NULL;
3847
3848 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3849 n *= nr_ioapics;
3850
3851 mem = alloc_bootmem(n);
3852 res = (void *)mem;
3853
3854 if (mem != NULL) {
3855 mem += sizeof(struct resource) * nr_ioapics;
3856
3857 for (i = 0; i < nr_ioapics; i++) {
3858 res[i].name = mem;
3859 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3860 sprintf(mem, "IOAPIC %u", i);
3861 mem += IOAPIC_RESOURCE_NAME_SIZE;
3862 }
3863 }
3864
3865 ioapic_resources = res;
3866
3867 return res;
3868}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003869
Yinghai Luf3294a32008-06-27 01:41:56 -07003870void __init ioapic_init_mappings(void)
3871{
3872 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3873 int i;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003874 struct resource *ioapic_res;
Yinghai Luf3294a32008-06-27 01:41:56 -07003875
Ingo Molnar54168ed2008-08-20 09:07:45 +02003876 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003877 for (i = 0; i < nr_ioapics; i++) {
3878 if (smp_found_config) {
3879 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003880#ifdef CONFIG_X86_32
3881 if (!ioapic_phys) {
3882 printk(KERN_ERR
3883 "WARNING: bogus zero IO-APIC "
3884 "address found in MPTABLE, "
3885 "disabling IO/APIC support!\n");
3886 smp_found_config = 0;
3887 skip_ioapic_setup = 1;
3888 goto fake_ioapic_page;
3889 }
3890#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003891 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003892#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003893fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003894#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003895 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003896 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07003897 ioapic_phys = __pa(ioapic_phys);
3898 }
3899 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003900 apic_printk(APIC_VERBOSE,
3901 "mapped IOAPIC to %08lx (%08lx)\n",
3902 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07003903 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003904
Ingo Molnar54168ed2008-08-20 09:07:45 +02003905 if (ioapic_res != NULL) {
3906 ioapic_res->start = ioapic_phys;
3907 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3908 ioapic_res++;
3909 }
Yinghai Luf3294a32008-06-27 01:41:56 -07003910 }
3911}
3912
Ingo Molnar54168ed2008-08-20 09:07:45 +02003913static int __init ioapic_insert_resources(void)
3914{
3915 int i;
3916 struct resource *r = ioapic_resources;
3917
3918 if (!r) {
3919 printk(KERN_ERR
3920 "IO APIC resources could be not be allocated.\n");
3921 return -1;
3922 }
3923
3924 for (i = 0; i < nr_ioapics; i++) {
3925 insert_resource(&iomem_resource, r);
3926 r++;
3927 }
3928
3929 return 0;
3930}
3931
3932/* Insert the IO APIC resources after PCI initialization has occured to handle
3933 * IO APICS that are mapped in on a BAR in PCI space. */
3934late_initcall(ioapic_insert_resources);