blob: 6ce5873a406c43576f14aba38a3df12ce31954bc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070028#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020038#include <linux/jiffies.h> /* time_after() */
Yinghai Lud4057bd2008-08-19 20:50:38 -070039#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070044#include <linux/hpet.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070045
Yinghai Lud4057bd2008-08-19 20:50:38 -070046#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48#include <asm/smp.h>
49#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070050#include <asm/proto.h>
51#include <asm/acpi.h>
52#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070054#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020055#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070056#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070057#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070058#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070059#include <asm/irq_remapping.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070060#include <asm/hpet.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Yinghai Lu497c9a12008-08-19 20:50:28 -070062#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020064#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010066#define __apicdebuginit(type) static type __init
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020069 * Is the SiS APIC rmw bug present ?
70 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72int sis_apic_bug = -1;
73
Yinghai Luefa25592008-08-19 20:50:36 -070074static DEFINE_SPINLOCK(ioapic_lock);
75static DEFINE_SPINLOCK(vector_lock);
76
Yinghai Luefa25592008-08-19 20:50:36 -070077/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 * # of IRQ routing registers
79 */
80int nr_ioapic_registers[MAX_IO_APICS];
81
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040082/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040083struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040084int nr_ioapics;
85
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040086/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040087struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040088
89/* # of MP IRQ source entries */
90int mp_irq_entries;
91
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040092#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
93int mp_bus_id_to_type[MAX_MP_BUSSES];
94#endif
95
96DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
97
Yinghai Luefa25592008-08-19 20:50:36 -070098int skip_ioapic_setup;
99
Ingo Molnar54168ed2008-08-20 09:07:45 +0200100static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700101{
102 /* disable IO-APIC */
103 disable_ioapic_setup();
104 return 0;
105}
106early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200107
Yinghai Luda51a822008-08-19 20:50:25 -0700108struct irq_cfg;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700109struct irq_pin_list;
Yinghai Lua1420f32008-08-19 20:50:24 -0700110struct irq_cfg {
Yinghai Luda51a822008-08-19 20:50:25 -0700111 unsigned int irq;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700112#ifdef CONFIG_HAVE_SPARSE_IRQ
Yinghai Luda51a822008-08-19 20:50:25 -0700113 struct irq_cfg *next;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700114#endif
Yinghai Lu0f978f42008-08-19 20:50:26 -0700115 struct irq_pin_list *irq_2_pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700116 cpumask_t domain;
117 cpumask_t old_domain;
118 unsigned move_cleanup_count;
Yinghai Lua1420f32008-08-19 20:50:24 -0700119 u8 vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700120 u8 move_in_progress : 1;
Yinghai Lua1420f32008-08-19 20:50:24 -0700121};
122
Yinghai Lua1420f32008-08-19 20:50:24 -0700123/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
124static struct irq_cfg irq_cfg_legacy[] __initdata = {
Yinghai Lu497c9a12008-08-19 20:50:28 -0700125 [0] = { .irq = 0, .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
126 [1] = { .irq = 1, .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
127 [2] = { .irq = 2, .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
128 [3] = { .irq = 3, .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
129 [4] = { .irq = 4, .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
130 [5] = { .irq = 5, .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
131 [6] = { .irq = 6, .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
132 [7] = { .irq = 7, .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
133 [8] = { .irq = 8, .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
134 [9] = { .irq = 9, .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
135 [10] = { .irq = 10, .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
136 [11] = { .irq = 11, .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
137 [12] = { .irq = 12, .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
138 [13] = { .irq = 13, .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
139 [14] = { .irq = 14, .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
140 [15] = { .irq = 15, .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
Yinghai Lua1420f32008-08-19 20:50:24 -0700141};
142
Yinghai Luda51a822008-08-19 20:50:25 -0700143static struct irq_cfg irq_cfg_init = { .irq = -1U, };
Yinghai Lua1420f32008-08-19 20:50:24 -0700144
Yinghai Luda51a822008-08-19 20:50:25 -0700145static void init_one_irq_cfg(struct irq_cfg *cfg)
146{
147 memcpy(cfg, &irq_cfg_init, sizeof(struct irq_cfg));
Yinghai Lua1420f32008-08-19 20:50:24 -0700148}
149
150static struct irq_cfg *irq_cfgx;
Yinghai Lue89eb432008-08-20 20:46:25 -0700151
Cyrill Gorcunovac54a6c2008-09-04 22:37:50 +0400152#ifdef CONFIG_HAVE_SPARSE_IRQ
Yinghai Lue89eb432008-08-20 20:46:25 -0700153/*
154 * Protect the irq_cfgx_free freelist:
155 */
156static DEFINE_SPINLOCK(irq_cfg_lock);
157
Yinghai Luda51a822008-08-19 20:50:25 -0700158static struct irq_cfg *irq_cfgx_free;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700159#endif
Cyrill Gorcunovac54a6c2008-09-04 22:37:50 +0400160
Yinghai Luda51a822008-08-19 20:50:25 -0700161static void __init init_work(void *data)
162{
163 struct dyn_array *da = data;
164 struct irq_cfg *cfg;
165 int legacy_count;
166 int i;
167
168 cfg = *da->name;
169
170 memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
171
Cyrill Gorcunov676f4a92008-09-04 22:37:49 +0400172 legacy_count = ARRAY_SIZE(irq_cfg_legacy);
Yinghai Luda51a822008-08-19 20:50:25 -0700173 for (i = legacy_count; i < *da->nr; i++)
174 init_one_irq_cfg(&cfg[i]);
175
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700176#ifdef CONFIG_HAVE_SPARSE_IRQ
Yinghai Luda51a822008-08-19 20:50:25 -0700177 for (i = 1; i < *da->nr; i++)
178 cfg[i-1].next = &cfg[i];
179
180 irq_cfgx_free = &irq_cfgx[legacy_count];
181 irq_cfgx[legacy_count - 1].next = NULL;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700182#endif
Yinghai Luda51a822008-08-19 20:50:25 -0700183}
184
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700185#ifdef CONFIG_HAVE_SPARSE_IRQ
186/* need to be biger than size of irq_cfg_legacy */
187static int nr_irq_cfg = 32;
188
189static int __init parse_nr_irq_cfg(char *arg)
190{
191 if (arg) {
192 nr_irq_cfg = simple_strtoul(arg, NULL, 0);
193 if (nr_irq_cfg < 32)
194 nr_irq_cfg = 32;
195 }
196 return 0;
197}
198
199early_param("nr_irq_cfg", parse_nr_irq_cfg);
200
201#define for_each_irq_cfg(irqX, cfg) \
202 for (cfg = irq_cfgx, irqX = cfg->irq; cfg; cfg = cfg->next, irqX = cfg ? cfg->irq : -1U)
203
Yinghai Luda51a822008-08-19 20:50:25 -0700204
205DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irq_cfg, PAGE_SIZE, init_work);
Yinghai Lua1420f32008-08-19 20:50:24 -0700206
207static struct irq_cfg *irq_cfg(unsigned int irq)
208{
Yinghai Luda51a822008-08-19 20:50:25 -0700209 struct irq_cfg *cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700210
Yinghai Luda51a822008-08-19 20:50:25 -0700211 cfg = irq_cfgx;
212 while (cfg) {
213 if (cfg->irq == irq)
214 return cfg;
215
216 cfg = cfg->next;
217 }
218
219 return NULL;
220}
221
222static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
223{
224 struct irq_cfg *cfg, *cfg_pri;
Yinghai Lue89eb432008-08-20 20:46:25 -0700225 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -0700226 int count = 0;
Yinghai Lue89eb432008-08-20 20:46:25 -0700227 int i;
Yinghai Luda51a822008-08-19 20:50:25 -0700228
229 cfg_pri = cfg = irq_cfgx;
230 while (cfg) {
231 if (cfg->irq == irq)
232 return cfg;
233
234 cfg_pri = cfg;
235 cfg = cfg->next;
236 count++;
237 }
238
Yinghai Lue89eb432008-08-20 20:46:25 -0700239 spin_lock_irqsave(&irq_cfg_lock, flags);
Yinghai Luda51a822008-08-19 20:50:25 -0700240 if (!irq_cfgx_free) {
241 unsigned long phys;
242 unsigned long total_bytes;
243 /*
244 * we run out of pre-allocate ones, allocate more
245 */
246 printk(KERN_DEBUG "try to get more irq_cfg %d\n", nr_irq_cfg);
247
248 total_bytes = sizeof(struct irq_cfg) * nr_irq_cfg;
249 if (after_bootmem)
250 cfg = kzalloc(total_bytes, GFP_ATOMIC);
251 else
252 cfg = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
253
254 if (!cfg)
255 panic("please boot with nr_irq_cfg= %d\n", count * 2);
256
257 phys = __pa(cfg);
258 printk(KERN_DEBUG "irq_irq ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
259
260 for (i = 0; i < nr_irq_cfg; i++)
261 init_one_irq_cfg(&cfg[i]);
262
263 for (i = 1; i < nr_irq_cfg; i++)
264 cfg[i-1].next = &cfg[i];
265
266 irq_cfgx_free = cfg;
267 }
268
269 cfg = irq_cfgx_free;
270 irq_cfgx_free = irq_cfgx_free->next;
271 cfg->next = NULL;
272 if (cfg_pri)
273 cfg_pri->next = cfg;
274 else
275 irq_cfgx = cfg;
276 cfg->irq = irq;
Yinghai Lue89eb432008-08-20 20:46:25 -0700277
278 spin_unlock_irqrestore(&irq_cfg_lock, flags);
279
Yinghai Luda51a822008-08-19 20:50:25 -0700280 printk(KERN_DEBUG "found new irq_cfg for irq %d\n", cfg->irq);
Yinghai Luda51a822008-08-19 20:50:25 -0700281#ifdef CONFIG_HAVE_SPARSE_IRQ_DEBUG
282 {
283 /* dump the results */
284 struct irq_cfg *cfg;
285 unsigned long phys;
286 unsigned long bytes = sizeof(struct irq_cfg);
287
288 printk(KERN_DEBUG "=========================== %d\n", irq);
289 printk(KERN_DEBUG "irq_cfg dump after get that for %d\n", irq);
290 for_each_irq_cfg(cfg) {
291 phys = __pa(cfg);
292 printk(KERN_DEBUG "irq_cfg %d ==> [%#lx - %#lx]\n", cfg->irq, phys, phys + bytes);
293 }
294 printk(KERN_DEBUG "===========================\n");
295 }
296#endif
297 return cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700298}
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700299#else
Yinghai Lua1420f32008-08-19 20:50:24 -0700300
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700301#define for_each_irq_cfg(irq, cfg) \
302 for (irq = 0, cfg = &irq_cfgx[irq]; irq < nr_irqs; irq++, cfg = &irq_cfgx[irq])
303
304DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irqs, PAGE_SIZE, init_work);
305
306struct irq_cfg *irq_cfg(unsigned int irq)
307{
308 if (irq < nr_irqs)
309 return &irq_cfgx[irq];
310
311 return NULL;
312}
313struct irq_cfg *irq_cfg_alloc(unsigned int irq)
314{
315 return irq_cfg(irq);
316}
317
318#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/*
320 * This is performance-critical, we want to do it O(1)
321 *
322 * the indexing order of this array favors 1:1 mappings
323 * between pins and IRQs.
324 */
325
Yinghai Lu0f978f42008-08-19 20:50:26 -0700326struct irq_pin_list {
327 int apic, pin;
328 struct irq_pin_list *next;
329};
Yinghai Lu301e6192008-08-19 20:50:02 -0700330
Yinghai Lu0f978f42008-08-19 20:50:26 -0700331static struct irq_pin_list *irq_2_pin_head;
332/* fill one page ? */
333static int nr_irq_2_pin = 0x100;
334static struct irq_pin_list *irq_2_pin_ptr;
335static void __init irq_2_pin_init_work(void *data)
336{
337 struct dyn_array *da = data;
338 struct irq_pin_list *pin;
339 int i;
340
341 pin = *da->name;
342
343 for (i = 1; i < *da->nr; i++)
344 pin[i-1].next = &pin[i];
345
346 irq_2_pin_ptr = &pin[0];
347}
348DEFINE_DYN_ARRAY(irq_2_pin_head, sizeof(struct irq_pin_list), nr_irq_2_pin, PAGE_SIZE, irq_2_pin_init_work);
349
350static struct irq_pin_list *get_one_free_irq_2_pin(void)
351{
352 struct irq_pin_list *pin;
353 int i;
354
355 pin = irq_2_pin_ptr;
356
357 if (pin) {
358 irq_2_pin_ptr = pin->next;
359 pin->next = NULL;
360 return pin;
361 }
362
363 /*
364 * we run out of pre-allocate ones, allocate more
365 */
366 printk(KERN_DEBUG "try to get more irq_2_pin %d\n", nr_irq_2_pin);
367
368 if (after_bootmem)
369 pin = kzalloc(sizeof(struct irq_pin_list)*nr_irq_2_pin,
370 GFP_ATOMIC);
371 else
372 pin = __alloc_bootmem_nopanic(sizeof(struct irq_pin_list) *
373 nr_irq_2_pin, PAGE_SIZE, 0);
374
375 if (!pin)
376 panic("can not get more irq_2_pin\n");
377
378 for (i = 1; i < nr_irq_2_pin; i++)
379 pin[i-1].next = &pin[i];
380
381 irq_2_pin_ptr = pin->next;
382 pin->next = NULL;
383
384 return pin;
385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds130fe052006-11-01 09:11:00 -0800387struct io_apic {
388 unsigned int index;
389 unsigned int unused[3];
390 unsigned int data;
391};
392
393static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
394{
395 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400396 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800397}
398
399static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
400{
401 struct io_apic __iomem *io_apic = io_apic_base(apic);
402 writel(reg, &io_apic->index);
403 return readl(&io_apic->data);
404}
405
406static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
407{
408 struct io_apic __iomem *io_apic = io_apic_base(apic);
409 writel(reg, &io_apic->index);
410 writel(value, &io_apic->data);
411}
412
413/*
414 * Re-write a value: to be used for read-modify-write
415 * cycles where the read already set up the index register.
416 *
417 * Older SiS APIC requires we rewrite the index register
418 */
419static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
420{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200421 struct io_apic __iomem *io_apic = io_apic_base(apic);
422 if (sis_apic_bug)
423 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800424 writel(value, &io_apic->data);
425}
426
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700427static bool io_apic_level_ack_pending(unsigned int irq)
428{
429 struct irq_pin_list *entry;
430 unsigned long flags;
431 struct irq_cfg *cfg = irq_cfg(irq);
432
433 spin_lock_irqsave(&ioapic_lock, flags);
434 entry = cfg->irq_2_pin;
435 for (;;) {
436 unsigned int reg;
437 int pin;
438
439 if (!entry)
440 break;
441 pin = entry->pin;
442 reg = io_apic_read(entry->apic, 0x10 + pin*2);
443 /* Is the remote IRR bit set? */
444 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
445 spin_unlock_irqrestore(&ioapic_lock, flags);
446 return true;
447 }
448 if (!entry->next)
449 break;
450 entry = entry->next;
451 }
452 spin_unlock_irqrestore(&ioapic_lock, flags);
453
454 return false;
455}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700456
Andi Kleencf4c6a22006-09-26 10:52:30 +0200457union entry_union {
458 struct { u32 w1, w2; };
459 struct IO_APIC_route_entry entry;
460};
461
462static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
463{
464 union entry_union eu;
465 unsigned long flags;
466 spin_lock_irqsave(&ioapic_lock, flags);
467 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
468 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
469 spin_unlock_irqrestore(&ioapic_lock, flags);
470 return eu.entry;
471}
472
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800473/*
474 * When we write a new IO APIC routing entry, we need to write the high
475 * word first! If the mask bit in the low word is clear, we will enable
476 * the interrupt, and we need to make sure the entry is fully populated
477 * before that happens.
478 */
Andi Kleend15512f2006-12-07 02:14:07 +0100479static void
480__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
481{
482 union entry_union eu;
483 eu.entry = e;
484 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
485 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
486}
487
Andi Kleencf4c6a22006-09-26 10:52:30 +0200488static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
489{
490 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200491 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100492 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800493 spin_unlock_irqrestore(&ioapic_lock, flags);
494}
495
496/*
497 * When we mask an IO APIC routing entry, we need to write the low
498 * word first, in order to set the mask bit before we change the
499 * high bits!
500 */
501static void ioapic_mask_entry(int apic, int pin)
502{
503 unsigned long flags;
504 union entry_union eu = { .entry.mask = 1 };
505
506 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200507 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
508 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
509 spin_unlock_irqrestore(&ioapic_lock, flags);
510}
511
Yinghai Lu497c9a12008-08-19 20:50:28 -0700512#ifdef CONFIG_SMP
513static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
514{
515 int apic, pin;
516 struct irq_cfg *cfg;
517 struct irq_pin_list *entry;
518
519 cfg = irq_cfg(irq);
520 entry = cfg->irq_2_pin;
521 for (;;) {
522 unsigned int reg;
523
524 if (!entry)
525 break;
526
527 apic = entry->apic;
528 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200529#ifdef CONFIG_INTR_REMAP
530 /*
531 * With interrupt-remapping, destination information comes
532 * from interrupt-remapping table entry.
533 */
534 if (!irq_remapped(irq))
535 io_apic_write(apic, 0x11 + pin*2, dest);
536#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700537 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200538#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700539 reg = io_apic_read(apic, 0x10 + pin*2);
540 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
541 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200542 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700543 if (!entry->next)
544 break;
545 entry = entry->next;
546 }
547}
Yinghai Luefa25592008-08-19 20:50:36 -0700548
549static int assign_irq_vector(int irq, cpumask_t mask);
550
Yinghai Lu497c9a12008-08-19 20:50:28 -0700551static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
552{
553 struct irq_cfg *cfg;
554 unsigned long flags;
555 unsigned int dest;
556 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200557 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700558
Yinghai Lu497c9a12008-08-19 20:50:28 -0700559 cpus_and(tmp, mask, cpu_online_map);
560 if (cpus_empty(tmp))
561 return;
562
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700563 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700564 if (assign_irq_vector(irq, mask))
565 return;
566
567 cpus_and(tmp, cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700568 dest = cpu_mask_to_apicid(tmp);
569 /*
570 * Only the high 8 bits are valid.
571 */
572 dest = SET_APIC_LOGICAL_ID(dest);
573
Ingo Molnar54168ed2008-08-20 09:07:45 +0200574 desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700575 spin_lock_irqsave(&ioapic_lock, flags);
576 __target_IO_APIC_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200577 desc->affinity = mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700578 spin_unlock_irqrestore(&ioapic_lock, flags);
579}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700580#endif /* CONFIG_SMP */
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582/*
583 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
584 * shared ISA-space IRQs, so we have to support them. We are super
585 * fast in the common case, and fast for shared ISA-space IRQs.
586 */
587static void add_pin_to_irq(unsigned int irq, int apic, int pin)
588{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700589 struct irq_cfg *cfg;
590 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Yinghai Lu0f978f42008-08-19 20:50:26 -0700592 /* first time to refer irq_cfg, so with new */
593 cfg = irq_cfg_alloc(irq);
594 entry = cfg->irq_2_pin;
595 if (!entry) {
596 entry = get_one_free_irq_2_pin();
597 cfg->irq_2_pin = entry;
598 entry->apic = apic;
599 entry->pin = pin;
600 printk(KERN_DEBUG " 0 add_pin_to_irq: irq %d --> apic %d pin %d\n", irq, apic, pin);
601 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700603
604 while (entry->next) {
605 /* not again, please */
606 if (entry->apic == apic && entry->pin == pin)
607 return;
608
609 entry = entry->next;
610 }
611
612 entry->next = get_one_free_irq_2_pin();
613 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 entry->apic = apic;
615 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700616 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 -0700617}
618
619/*
620 * Reroute an IRQ to a different pin.
621 */
622static void __init replace_pin_at_irq(unsigned int irq,
623 int oldapic, int oldpin,
624 int newapic, int newpin)
625{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700626 struct irq_cfg *cfg = irq_cfg(irq);
627 struct irq_pin_list *entry = cfg->irq_2_pin;
628 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Yinghai Lu0f978f42008-08-19 20:50:26 -0700630 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (entry->apic == oldapic && entry->pin == oldpin) {
632 entry->apic = newapic;
633 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700634 replaced = 1;
635 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700637 }
638 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700640
641 /* why? call replace before add? */
642 if (!replaced)
643 add_pin_to_irq(irq, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400646static inline void io_apic_modify_irq(unsigned int irq,
647 int mask_and, int mask_or,
648 void (*final)(struct irq_pin_list *entry))
649{
650 int pin;
651 struct irq_cfg *cfg;
652 struct irq_pin_list *entry;
653
654 cfg = irq_cfg(irq);
655 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
656 unsigned int reg;
657 pin = entry->pin;
658 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
659 reg &= mask_and;
660 reg |= mask_or;
661 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
662 if (final)
663 final(entry);
664 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700665}
666
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400667static void __unmask_IO_APIC_irq(unsigned int irq)
668{
669 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED, 0, NULL);
670}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700671
672#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400673void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700674{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400675 /*
676 * Synchronize the IO-APIC and the CPU by doing
677 * a dummy read from the IO-APIC
678 */
679 struct io_apic __iomem *io_apic;
680 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700681 readl(&io_apic->data);
682}
683
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400684static void __mask_IO_APIC_irq(unsigned int irq)
685{
686 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
687}
688#else /* CONFIG_X86_32 */
689static void __mask_IO_APIC_irq(unsigned int irq)
690{
691 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, NULL);
692}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700693
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400694static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
695{
696 io_apic_modify_irq(irq, ~IO_APIC_REDIR_LEVEL_TRIGGER,
697 IO_APIC_REDIR_MASKED, NULL);
698}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700699
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400700static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
701{
702 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED,
703 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
704}
705#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700706
Ingo Molnar54168ed2008-08-20 09:07:45 +0200707static void mask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 unsigned long flags;
710
711 spin_lock_irqsave(&ioapic_lock, flags);
712 __mask_IO_APIC_irq(irq);
713 spin_unlock_irqrestore(&ioapic_lock, flags);
714}
715
Ingo Molnar54168ed2008-08-20 09:07:45 +0200716static void unmask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 unsigned long flags;
719
720 spin_lock_irqsave(&ioapic_lock, flags);
721 __unmask_IO_APIC_irq(irq);
722 spin_unlock_irqrestore(&ioapic_lock, flags);
723}
724
725static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
726{
727 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200730 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (entry.delivery_mode == dest_SMI)
732 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * Disable it in the IO-APIC irq-routing table:
735 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800736 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Ingo Molnar54168ed2008-08-20 09:07:45 +0200739static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
741 int apic, pin;
742
743 for (apic = 0; apic < nr_ioapics; apic++)
744 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
745 clear_IO_APIC_pin(apic, pin);
746}
747
Ingo Molnar54168ed2008-08-20 09:07:45 +0200748#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100749void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 unsigned int cfg;
752
753 /*
754 * Wait for idle.
755 */
756 apic_wait_icr_idle();
757 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
758 /*
759 * Send the IPI. The write to APIC_ICR fires this off.
760 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100761 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200763#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Ingo Molnar54168ed2008-08-20 09:07:45 +0200765#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/*
767 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
768 * specific CPU-side IRQs.
769 */
770
771#define MAX_PIRQS 8
772static int pirq_entries [MAX_PIRQS];
773static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775static int __init ioapic_pirq_setup(char *str)
776{
777 int i, max;
778 int ints[MAX_PIRQS+1];
779
780 get_options(str, ARRAY_SIZE(ints), ints);
781
782 for (i = 0; i < MAX_PIRQS; i++)
783 pirq_entries[i] = -1;
784
785 pirqs_enabled = 1;
786 apic_printk(APIC_VERBOSE, KERN_INFO
787 "PIRQ redirection, working around broken MP-BIOS.\n");
788 max = MAX_PIRQS;
789 if (ints[0] < MAX_PIRQS)
790 max = ints[0];
791
792 for (i = 0; i < max; i++) {
793 apic_printk(APIC_VERBOSE, KERN_DEBUG
794 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
795 /*
796 * PIRQs are mapped upside down, usually.
797 */
798 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
799 }
800 return 1;
801}
802
803__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200804#endif /* CONFIG_X86_32 */
805
806#ifdef CONFIG_INTR_REMAP
807/* I/O APIC RTE contents at the OS boot up */
808static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
809
810/*
811 * Saves and masks all the unmasked IO-APIC RTE's
812 */
813int save_mask_IO_APIC_setup(void)
814{
815 union IO_APIC_reg_01 reg_01;
816 unsigned long flags;
817 int apic, pin;
818
819 /*
820 * The number of IO-APIC IRQ registers (== #pins):
821 */
822 for (apic = 0; apic < nr_ioapics; apic++) {
823 spin_lock_irqsave(&ioapic_lock, flags);
824 reg_01.raw = io_apic_read(apic, 1);
825 spin_unlock_irqrestore(&ioapic_lock, flags);
826 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
827 }
828
829 for (apic = 0; apic < nr_ioapics; apic++) {
830 early_ioapic_entries[apic] =
831 kzalloc(sizeof(struct IO_APIC_route_entry) *
832 nr_ioapic_registers[apic], GFP_KERNEL);
833 if (!early_ioapic_entries[apic])
834 return -ENOMEM;
835 }
836
837 for (apic = 0; apic < nr_ioapics; apic++)
838 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
839 struct IO_APIC_route_entry entry;
840
841 entry = early_ioapic_entries[apic][pin] =
842 ioapic_read_entry(apic, pin);
843 if (!entry.mask) {
844 entry.mask = 1;
845 ioapic_write_entry(apic, pin, entry);
846 }
847 }
848 return 0;
849}
850
851void restore_IO_APIC_setup(void)
852{
853 int apic, pin;
854
855 for (apic = 0; apic < nr_ioapics; apic++)
856 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
857 ioapic_write_entry(apic, pin,
858 early_ioapic_entries[apic][pin]);
859}
860
861void reinit_intr_remapped_IO_APIC(int intr_remapping)
862{
863 /*
864 * for now plain restore of previous settings.
865 * TBD: In the case of OS enabling interrupt-remapping,
866 * IO-APIC RTE's need to be setup to point to interrupt-remapping
867 * table entries. for now, do a plain restore, and wait for
868 * the setup_IO_APIC_irqs() to do proper initialization.
869 */
870 restore_IO_APIC_setup();
871}
872#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874/*
875 * Find the IRQ entry number of a certain pin.
876 */
877static int find_irq_entry(int apic, int pin, int type)
878{
879 int i;
880
881 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400882 if (mp_irqs[i].mp_irqtype == type &&
883 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
884 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
885 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 return i;
887
888 return -1;
889}
890
891/*
892 * Find the pin to which IRQ[irq] (ISA) is connected
893 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800894static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
896 int i;
897
898 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400899 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300901 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400902 (mp_irqs[i].mp_irqtype == type) &&
903 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400905 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907 return -1;
908}
909
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800910static int __init find_isa_irq_apic(int irq, int type)
911{
912 int i;
913
914 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400915 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800916
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300917 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400918 (mp_irqs[i].mp_irqtype == type) &&
919 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800920 break;
921 }
922 if (i < mp_irq_entries) {
923 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200924 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400925 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800926 return apic;
927 }
928 }
929
930 return -1;
931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/*
934 * Find a specific PCI IRQ entry.
935 * Not an __init, possibly needed by modules
936 */
937static int pin_2_irq(int idx, int apic, int pin);
938
939int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
940{
941 int apic, i, best_guess = -1;
942
Ingo Molnar54168ed2008-08-20 09:07:45 +0200943 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
944 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400945 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200946 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 return -1;
948 }
949 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400950 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400953 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
954 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 break;
956
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300957 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400958 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400960 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200961 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (!(apic || IO_APIC_IRQ(irq)))
964 continue;
965
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400966 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return irq;
968 /*
969 * Use the first all-but-pin matching entry as a
970 * best-guess fuzzy result for broken mptables.
971 */
972 if (best_guess < 0)
973 best_guess = irq;
974 }
975 }
976 return best_guess;
977}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200978
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700979EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300981#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/*
983 * EISA Edge/Level control register, ELCR
984 */
985static int EISA_ELCR(unsigned int irq)
986{
987 if (irq < 16) {
988 unsigned int port = 0x4d0 + (irq >> 3);
989 return (inb(port) >> (irq & 7)) & 1;
990 }
991 apic_printk(APIC_VERBOSE, KERN_INFO
992 "Broken MPtable reports ISA irq %d\n", irq);
993 return 0;
994}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200995
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300998/* ISA interrupts are always polarity zero edge triggered,
999 * when listed as conforming in the MP table. */
1000
1001#define default_ISA_trigger(idx) (0)
1002#define default_ISA_polarity(idx) (0)
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004/* EISA interrupts are always polarity zero and can be edge or level
1005 * trigger depending on the ELCR value. If an interrupt is listed as
1006 * EISA conforming in the MP table, that means its trigger type must
1007 * be read in from the ELCR */
1008
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001009#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001010#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012/* PCI interrupts are always polarity one level triggered,
1013 * when listed as conforming in the MP table. */
1014
1015#define default_PCI_trigger(idx) (1)
1016#define default_PCI_polarity(idx) (1)
1017
1018/* MCA interrupts are always polarity zero level triggered,
1019 * when listed as conforming in the MP table. */
1020
1021#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001022#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Shaohua Li61fd47e2007-11-17 01:05:28 -05001024static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001026 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 int polarity;
1028
1029 /*
1030 * Determine IRQ line polarity (high active or low active):
1031 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001032 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001034 case 0: /* conforms, ie. bus-type dependent polarity */
1035 if (test_bit(bus, mp_bus_not_pci))
1036 polarity = default_ISA_polarity(idx);
1037 else
1038 polarity = default_PCI_polarity(idx);
1039 break;
1040 case 1: /* high active */
1041 {
1042 polarity = 0;
1043 break;
1044 }
1045 case 2: /* reserved */
1046 {
1047 printk(KERN_WARNING "broken BIOS!!\n");
1048 polarity = 1;
1049 break;
1050 }
1051 case 3: /* low active */
1052 {
1053 polarity = 1;
1054 break;
1055 }
1056 default: /* invalid */
1057 {
1058 printk(KERN_WARNING "broken BIOS!!\n");
1059 polarity = 1;
1060 break;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063 return polarity;
1064}
1065
1066static int MPBIOS_trigger(int idx)
1067{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001068 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 int trigger;
1070
1071 /*
1072 * Determine IRQ trigger mode (edge or level sensitive):
1073 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001074 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001076 case 0: /* conforms, ie. bus-type dependent */
1077 if (test_bit(bus, mp_bus_not_pci))
1078 trigger = default_ISA_trigger(idx);
1079 else
1080 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001081#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001082 switch (mp_bus_id_to_type[bus]) {
1083 case MP_BUS_ISA: /* ISA pin */
1084 {
1085 /* set before the switch */
1086 break;
1087 }
1088 case MP_BUS_EISA: /* EISA pin */
1089 {
1090 trigger = default_EISA_trigger(idx);
1091 break;
1092 }
1093 case MP_BUS_PCI: /* PCI pin */
1094 {
1095 /* set before the switch */
1096 break;
1097 }
1098 case MP_BUS_MCA: /* MCA pin */
1099 {
1100 trigger = default_MCA_trigger(idx);
1101 break;
1102 }
1103 default:
1104 {
1105 printk(KERN_WARNING "broken BIOS!!\n");
1106 trigger = 1;
1107 break;
1108 }
1109 }
1110#endif
1111 break;
1112 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001113 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001114 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001115 break;
1116 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001117 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001118 {
1119 printk(KERN_WARNING "broken BIOS!!\n");
1120 trigger = 1;
1121 break;
1122 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001123 case 3: /* level */
1124 {
1125 trigger = 1;
1126 break;
1127 }
1128 default: /* invalid */
1129 {
1130 printk(KERN_WARNING "broken BIOS!!\n");
1131 trigger = 0;
1132 break;
1133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135 return trigger;
1136}
1137
1138static inline int irq_polarity(int idx)
1139{
1140 return MPBIOS_polarity(idx);
1141}
1142
1143static inline int irq_trigger(int idx)
1144{
1145 return MPBIOS_trigger(idx);
1146}
1147
Yinghai Luefa25592008-08-19 20:50:36 -07001148int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149static int pin_2_irq(int idx, int apic, int pin)
1150{
1151 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001152 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 /*
1155 * Debugging check, we are in big trouble if this message pops up!
1156 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001157 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1159
Ingo Molnar54168ed2008-08-20 09:07:45 +02001160 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001161 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001162 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001163 /*
1164 * PCI IRQs are mapped in order
1165 */
1166 i = irq = 0;
1167 while (i < apic)
1168 irq += nr_ioapic_registers[i++];
1169 irq += pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001170 /*
1171 * For MPS mode, so far only needed by ES7000 platform
1172 */
1173 if (ioapic_renumber_irq)
1174 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
Ingo Molnar54168ed2008-08-20 09:07:45 +02001177#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 /*
1179 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1180 */
1181 if ((pin >= 16) && (pin <= 23)) {
1182 if (pirq_entries[pin-16] != -1) {
1183 if (!pirq_entries[pin-16]) {
1184 apic_printk(APIC_VERBOSE, KERN_DEBUG
1185 "disabling PIRQ%d\n", pin-16);
1186 } else {
1187 irq = pirq_entries[pin-16];
1188 apic_printk(APIC_VERBOSE, KERN_DEBUG
1189 "using PIRQ%d -> IRQ %d\n",
1190 pin-16, irq);
1191 }
1192 }
1193 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001194#endif
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return irq;
1197}
1198
Yinghai Lu497c9a12008-08-19 20:50:28 -07001199void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001201 /* Used to the online set of cpus does not change
1202 * during assign_irq_vector.
1203 */
1204 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Yinghai Lu497c9a12008-08-19 20:50:28 -07001207void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001208{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001209 spin_unlock(&vector_lock);
1210}
1211
1212static int __assign_irq_vector(int irq, cpumask_t mask)
1213{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001214 /*
1215 * NOTE! The local APIC isn't very good at handling
1216 * multiple interrupts at the same interrupt level.
1217 * As the interrupt level is determined by taking the
1218 * vector number and shifting that right by 4, we
1219 * want to spread these out a bit so that they don't
1220 * all fall in the same interrupt level.
1221 *
1222 * Also, we've got to be careful not to trash gate
1223 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1224 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001225 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1226 unsigned int old_vector;
1227 int cpu;
1228 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001229
Ingo Molnar54168ed2008-08-20 09:07:45 +02001230 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001231
Ingo Molnar54168ed2008-08-20 09:07:45 +02001232 /* Only try and allocate irqs on cpus that are present */
1233 cpus_and(mask, mask, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001234
Ingo Molnar54168ed2008-08-20 09:07:45 +02001235 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1236 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001237
Ingo Molnar54168ed2008-08-20 09:07:45 +02001238 old_vector = cfg->vector;
1239 if (old_vector) {
1240 cpumask_t tmp;
1241 cpus_and(tmp, cfg->domain, mask);
1242 if (!cpus_empty(tmp))
1243 return 0;
1244 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001245
Ingo Molnar54168ed2008-08-20 09:07:45 +02001246 for_each_cpu_mask_nr(cpu, mask) {
1247 cpumask_t domain, new_mask;
1248 int new_cpu;
1249 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001250
Ingo Molnar54168ed2008-08-20 09:07:45 +02001251 domain = vector_allocation_domain(cpu);
1252 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001253
Ingo Molnar54168ed2008-08-20 09:07:45 +02001254 vector = current_vector;
1255 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001256next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001257 vector += 8;
1258 if (vector >= first_system_vector) {
1259 /* If we run out of vectors on large boxen, must share them. */
1260 offset = (offset + 1) % 8;
1261 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001262 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001263 if (unlikely(current_vector == vector))
1264 continue;
1265#ifdef CONFIG_X86_64
1266 if (vector == IA32_SYSCALL_VECTOR)
1267 goto next;
1268#else
1269 if (vector == SYSCALL_VECTOR)
1270 goto next;
1271#endif
1272 for_each_cpu_mask_nr(new_cpu, new_mask)
1273 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1274 goto next;
1275 /* Found one! */
1276 current_vector = vector;
1277 current_offset = offset;
1278 if (old_vector) {
1279 cfg->move_in_progress = 1;
1280 cfg->old_domain = cfg->domain;
1281 }
1282 for_each_cpu_mask_nr(new_cpu, new_mask)
1283 per_cpu(vector_irq, new_cpu)[vector] = irq;
1284 cfg->vector = vector;
1285 cfg->domain = domain;
1286 return 0;
1287 }
1288 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001289}
1290
1291static int assign_irq_vector(int irq, cpumask_t mask)
1292{
1293 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001294 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001295
1296 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001297 err = __assign_irq_vector(irq, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001298 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001299 return err;
1300}
1301
1302static void __clear_irq_vector(int irq)
1303{
1304 struct irq_cfg *cfg;
1305 cpumask_t mask;
1306 int cpu, vector;
1307
1308 cfg = irq_cfg(irq);
1309 BUG_ON(!cfg->vector);
1310
1311 vector = cfg->vector;
1312 cpus_and(mask, cfg->domain, cpu_online_map);
1313 for_each_cpu_mask_nr(cpu, mask)
1314 per_cpu(vector_irq, cpu)[vector] = -1;
1315
1316 cfg->vector = 0;
1317 cpus_clear(cfg->domain);
1318}
1319
1320void __setup_vector_irq(int cpu)
1321{
1322 /* Initialize vector_irq on a new cpu */
1323 /* This function must be called with vector_lock held */
1324 int irq, vector;
1325 struct irq_cfg *cfg;
1326
1327 /* Mark the inuse vectors */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001328 for_each_irq_cfg(irq, cfg) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001329 if (!cpu_isset(cpu, cfg->domain))
1330 continue;
1331 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001332 per_cpu(vector_irq, cpu)[vector] = irq;
1333 }
1334 /* Mark the free vectors */
1335 for (vector = 0; vector < NR_VECTORS; ++vector) {
1336 irq = per_cpu(vector_irq, cpu)[vector];
1337 if (irq < 0)
1338 continue;
1339
1340 cfg = irq_cfg(irq);
1341 if (!cpu_isset(cpu, cfg->domain))
1342 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001343 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001344}
Glauber Costa3fde6902008-05-28 20:34:19 -07001345
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001346static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001347#ifdef CONFIG_INTR_REMAP
1348static struct irq_chip ir_ioapic_chip;
1349#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Ingo Molnar54168ed2008-08-20 09:07:45 +02001351#define IOAPIC_AUTO -1
1352#define IOAPIC_EDGE 0
1353#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001355#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001356static inline int IO_APIC_irq_trigger(int irq)
1357{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001358 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001359
Ingo Molnar54168ed2008-08-20 09:07:45 +02001360 for (apic = 0; apic < nr_ioapics; apic++) {
1361 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1362 idx = find_irq_entry(apic, pin, mp_INT);
1363 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1364 return irq_trigger(idx);
1365 }
1366 }
1367 /*
1368 * nonexistent IRQs are edge default
1369 */
1370 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001371}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001372#else
1373static inline int IO_APIC_irq_trigger(int irq)
1374{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001375 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001376}
1377#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001378
Yinghai Lu497c9a12008-08-19 20:50:28 -07001379static void ioapic_register_intr(int irq, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Yinghai Lu08678b02008-08-19 20:50:05 -07001381 struct irq_desc *desc;
1382
Yinghai Lu199751d2008-08-19 20:50:27 -07001383 /* first time to use this irq_desc */
1384 if (irq < 16)
1385 desc = irq_to_desc(irq);
1386 else
1387 desc = irq_to_desc_alloc(irq);
1388
Jan Beulich6ebcc002006-06-26 13:56:46 +02001389 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001390 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001391 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001392 else
1393 desc->status &= ~IRQ_LEVEL;
1394
Ingo Molnar54168ed2008-08-20 09:07:45 +02001395#ifdef CONFIG_INTR_REMAP
1396 if (irq_remapped(irq)) {
1397 desc->status |= IRQ_MOVE_PCNTXT;
1398 if (trigger)
1399 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1400 handle_fasteoi_irq,
1401 "fasteoi");
1402 else
1403 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1404 handle_edge_irq, "edge");
1405 return;
1406 }
1407#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001408 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1409 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001410 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001411 handle_fasteoi_irq,
1412 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001413 else
Ingo Molnara460e742006-10-17 00:10:03 -07001414 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001415 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001416}
1417
1418static int setup_ioapic_entry(int apic, int irq,
1419 struct IO_APIC_route_entry *entry,
1420 unsigned int destination, int trigger,
1421 int polarity, int vector)
1422{
1423 /*
1424 * add it to the IO-APIC irq-routing table:
1425 */
1426 memset(entry,0,sizeof(*entry));
1427
Ingo Molnar54168ed2008-08-20 09:07:45 +02001428#ifdef CONFIG_INTR_REMAP
1429 if (intr_remapping_enabled) {
1430 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1431 struct irte irte;
1432 struct IR_IO_APIC_route_entry *ir_entry =
1433 (struct IR_IO_APIC_route_entry *) entry;
1434 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001435
Ingo Molnar54168ed2008-08-20 09:07:45 +02001436 if (!iommu)
1437 panic("No mapping iommu for ioapic %d\n", apic);
1438
1439 index = alloc_irte(iommu, irq, 1);
1440 if (index < 0)
1441 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1442
1443 memset(&irte, 0, sizeof(irte));
1444
1445 irte.present = 1;
1446 irte.dst_mode = INT_DEST_MODE;
1447 irte.trigger_mode = trigger;
1448 irte.dlvry_mode = INT_DELIVERY_MODE;
1449 irte.vector = vector;
1450 irte.dest_id = IRTE_DEST(destination);
1451
1452 modify_irte(irq, &irte);
1453
1454 ir_entry->index2 = (index >> 15) & 0x1;
1455 ir_entry->zero = 0;
1456 ir_entry->format = 1;
1457 ir_entry->index = (index & 0x7fff);
1458 } else
1459#endif
1460 {
1461 entry->delivery_mode = INT_DELIVERY_MODE;
1462 entry->dest_mode = INT_DEST_MODE;
1463 entry->dest = destination;
1464 }
1465
1466 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001467 entry->trigger = trigger;
1468 entry->polarity = polarity;
1469 entry->vector = vector;
1470
1471 /* Mask level triggered irqs.
1472 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1473 */
1474 if (trigger)
1475 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001476 return 0;
1477}
1478
1479static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001480 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001481{
1482 struct irq_cfg *cfg;
1483 struct IO_APIC_route_entry entry;
1484 cpumask_t mask;
1485
1486 if (!IO_APIC_IRQ(irq))
1487 return;
1488
1489 cfg = irq_cfg(irq);
1490
1491 mask = TARGET_CPUS;
1492 if (assign_irq_vector(irq, mask))
1493 return;
1494
1495 cpus_and(mask, cfg->domain, mask);
1496
1497 apic_printk(APIC_VERBOSE,KERN_DEBUG
1498 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1499 "IRQ %d Mode:%i Active:%i)\n",
1500 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1501 irq, trigger, polarity);
1502
1503
1504 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1505 cpu_mask_to_apicid(mask), trigger, polarity,
1506 cfg->vector)) {
1507 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1508 mp_ioapics[apic].mp_apicid, pin);
1509 __clear_irq_vector(irq);
1510 return;
1511 }
1512
1513 ioapic_register_intr(irq, trigger);
1514 if (irq < 16)
1515 disable_8259A_irq(irq);
1516
1517 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
1520static void __init setup_IO_APIC_irqs(void)
1521{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001522 int apic, pin, idx, irq;
1523 int notcon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1526
1527 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001528 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001530 idx = find_irq_entry(apic, pin, mp_INT);
1531 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001532 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001533 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001534 apic_printk(APIC_VERBOSE,
1535 KERN_DEBUG " %d-%d",
1536 mp_ioapics[apic].mp_apicid,
1537 pin);
1538 } else
1539 apic_printk(APIC_VERBOSE, " %d-%d",
1540 mp_ioapics[apic].mp_apicid,
1541 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001542 continue;
1543 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001544 if (notcon) {
1545 apic_printk(APIC_VERBOSE,
1546 " (apicid-pin) not connected\n");
1547 notcon = 0;
1548 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001549
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001550 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001551#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001552 if (multi_timer_check(apic, irq))
1553 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001554#endif
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001555 add_pin_to_irq(irq, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001556
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001557 setup_IO_APIC_irq(apic, pin, irq,
1558 irq_trigger(idx), irq_polarity(idx));
1559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001562 if (notcon)
1563 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001564 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001568 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001570static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1571 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
1573 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Ingo Molnar54168ed2008-08-20 09:07:45 +02001575#ifdef CONFIG_INTR_REMAP
1576 if (intr_remapping_enabled)
1577 return;
1578#endif
1579
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001580 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /*
1583 * We use logical delivery to get the timer IRQ
1584 * to the first CPU.
1585 */
1586 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001587 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001588 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 entry.delivery_mode = INT_DELIVERY_MODE;
1590 entry.polarity = 0;
1591 entry.trigger = 0;
1592 entry.vector = vector;
1593
1594 /*
1595 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001596 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001598 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 /*
1601 * Add it to the IO-APIC irq-routing table:
1602 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001603 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001606
1607__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 int apic, i;
1610 union IO_APIC_reg_00 reg_00;
1611 union IO_APIC_reg_01 reg_01;
1612 union IO_APIC_reg_02 reg_02;
1613 union IO_APIC_reg_03 reg_03;
1614 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001615 struct irq_cfg *cfg;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001616 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618 if (apic_verbosity == APIC_QUIET)
1619 return;
1620
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001621 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 for (i = 0; i < nr_ioapics; i++)
1623 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001624 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /*
1627 * We are a bit conservative about what we expect. We have to
1628 * know about every hardware change ASAP.
1629 */
1630 printk(KERN_INFO "testing the IO APIC.......................\n");
1631
1632 for (apic = 0; apic < nr_ioapics; apic++) {
1633
1634 spin_lock_irqsave(&ioapic_lock, flags);
1635 reg_00.raw = io_apic_read(apic, 0);
1636 reg_01.raw = io_apic_read(apic, 1);
1637 if (reg_01.bits.version >= 0x10)
1638 reg_02.raw = io_apic_read(apic, 2);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001639 if (reg_01.bits.version >= 0x20)
1640 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 spin_unlock_irqrestore(&ioapic_lock, flags);
1642
Ingo Molnar54168ed2008-08-20 09:07:45 +02001643 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001644 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1646 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1647 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1648 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Ingo Molnar54168ed2008-08-20 09:07:45 +02001650 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1654 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
1656 /*
1657 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1658 * but the value of reg_02 is read as the previous read register
1659 * value, so ignore it if reg_02 == reg_01.
1660 */
1661 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1662 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1663 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
1666 /*
1667 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1668 * or reg_03, but the value of reg_0[23] is read as the previous read
1669 * register value, so ignore it if reg_03 == reg_0[12].
1670 */
1671 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1672 reg_03.raw != reg_01.raw) {
1673 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1674 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676
1677 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1678
Yinghai Lud83e94a2008-08-19 20:50:33 -07001679 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1680 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 for (i = 0; i <= reg_01.bits.entries; i++) {
1683 struct IO_APIC_route_entry entry;
1684
Andi Kleencf4c6a22006-09-26 10:52:30 +02001685 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Ingo Molnar54168ed2008-08-20 09:07:45 +02001687 printk(KERN_DEBUG " %02x %03X ",
1688 i,
1689 entry.dest
1690 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
1692 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1693 entry.mask,
1694 entry.trigger,
1695 entry.irr,
1696 entry.polarity,
1697 entry.delivery_status,
1698 entry.dest_mode,
1699 entry.delivery_mode,
1700 entry.vector
1701 );
1702 }
1703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001705 for_each_irq_cfg(irq, cfg) {
Yinghai Lu0f978f42008-08-19 20:50:26 -07001706 struct irq_pin_list *entry = cfg->irq_2_pin;
1707 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001709 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 for (;;) {
1711 printk("-> %d:%d", entry->apic, entry->pin);
1712 if (!entry->next)
1713 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001714 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716 printk("\n");
1717 }
1718
1719 printk(KERN_INFO ".................................... done.\n");
1720
1721 return;
1722}
1723
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001724__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
1726 unsigned int v;
1727 int i, j;
1728
1729 if (apic_verbosity == APIC_QUIET)
1730 return;
1731
1732 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1733 for (i = 0; i < 8; i++) {
1734 v = apic_read(base + i*0x10);
1735 for (j = 0; j < 32; j++) {
1736 if (v & (1<<j))
1737 printk("1");
1738 else
1739 printk("0");
1740 }
1741 printk("\n");
1742 }
1743}
1744
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001745__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001748 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 if (apic_verbosity == APIC_QUIET)
1751 return;
1752
1753 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1754 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001755 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001756 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 v = apic_read(APIC_LVR);
1758 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1759 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001760 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 v = apic_read(APIC_TASKPRI);
1763 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1764
Ingo Molnar54168ed2008-08-20 09:07:45 +02001765 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001766 if (!APIC_XAPIC(ver)) {
1767 v = apic_read(APIC_ARBPRI);
1768 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1769 v & APIC_ARBPRI_MASK);
1770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 v = apic_read(APIC_PROCPRI);
1772 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1773 }
1774
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001775 /*
1776 * Remote read supported only in the 82489DX and local APIC for
1777 * Pentium processors.
1778 */
1779 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1780 v = apic_read(APIC_RRR);
1781 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1782 }
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 v = apic_read(APIC_LDR);
1785 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001786 if (!x2apic_enabled()) {
1787 v = apic_read(APIC_DFR);
1788 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 v = apic_read(APIC_SPIV);
1791 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1792
1793 printk(KERN_DEBUG "... APIC ISR field:\n");
1794 print_APIC_bitfield(APIC_ISR);
1795 printk(KERN_DEBUG "... APIC TMR field:\n");
1796 print_APIC_bitfield(APIC_TMR);
1797 printk(KERN_DEBUG "... APIC IRR field:\n");
1798 print_APIC_bitfield(APIC_IRR);
1799
Ingo Molnar54168ed2008-08-20 09:07:45 +02001800 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1801 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 v = apic_read(APIC_ESR);
1805 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1806 }
1807
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001808 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001809 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1810 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 v = apic_read(APIC_LVTT);
1813 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1814
1815 if (maxlvt > 3) { /* PC is LVT#4. */
1816 v = apic_read(APIC_LVTPC);
1817 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1818 }
1819 v = apic_read(APIC_LVT0);
1820 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1821 v = apic_read(APIC_LVT1);
1822 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1823
1824 if (maxlvt > 2) { /* ERR is LVT#3. */
1825 v = apic_read(APIC_LVTERR);
1826 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1827 }
1828
1829 v = apic_read(APIC_TMICT);
1830 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1831 v = apic_read(APIC_TMCCT);
1832 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1833 v = apic_read(APIC_TDCR);
1834 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1835 printk("\n");
1836}
1837
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001838__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001840 int cpu;
1841
1842 preempt_disable();
1843 for_each_online_cpu(cpu)
1844 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1845 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001848__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 unsigned int v;
1851 unsigned long flags;
1852
1853 if (apic_verbosity == APIC_QUIET)
1854 return;
1855
1856 printk(KERN_DEBUG "\nprinting PIC contents\n");
1857
1858 spin_lock_irqsave(&i8259A_lock, flags);
1859
1860 v = inb(0xa1) << 8 | inb(0x21);
1861 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1862
1863 v = inb(0xa0) << 8 | inb(0x20);
1864 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1865
Ingo Molnar54168ed2008-08-20 09:07:45 +02001866 outb(0x0b,0xa0);
1867 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001869 outb(0x0a,0xa0);
1870 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 spin_unlock_irqrestore(&i8259A_lock, flags);
1873
1874 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1875
1876 v = inb(0x4d1) << 8 | inb(0x4d0);
1877 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1878}
1879
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001880__apicdebuginit(int) print_all_ICs(void)
1881{
1882 print_PIC();
1883 print_all_local_APICs();
1884 print_IO_APIC();
1885
1886 return 0;
1887}
1888
1889fs_initcall(print_all_ICs);
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Yinghai Luefa25592008-08-19 20:50:36 -07001892/* Where if anywhere is the i8259 connect in external int mode */
1893static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1894
Ingo Molnar54168ed2008-08-20 09:07:45 +02001895void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
1897 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001898 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001899 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 unsigned long flags;
1901
Ingo Molnar54168ed2008-08-20 09:07:45 +02001902#ifdef CONFIG_X86_32
1903 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (!pirqs_enabled)
1905 for (i = 0; i < MAX_PIRQS; i++)
1906 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 /*
1910 * The number of IO-APIC IRQ registers (== #pins):
1911 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001912 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001914 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001916 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1917 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001918 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001919 int pin;
1920 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001921 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001922 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001923 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001924
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001925 /* If the interrupt line is enabled and in ExtInt mode
1926 * I have found the pin where the i8259 is connected.
1927 */
1928 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1929 ioapic_i8259.apic = apic;
1930 ioapic_i8259.pin = pin;
1931 goto found_i8259;
1932 }
1933 }
1934 }
1935 found_i8259:
1936 /* Look to see what if the MP table has reported the ExtINT */
1937 /* If we could not find the appropriate pin by looking at the ioapic
1938 * the i8259 probably is not connected the ioapic but give the
1939 * mptable a chance anyway.
1940 */
1941 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1942 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1943 /* Trust the MP table if nothing is setup in the hardware */
1944 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1945 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1946 ioapic_i8259.pin = i8259_pin;
1947 ioapic_i8259.apic = i8259_apic;
1948 }
1949 /* Complain if the MP table and the hardware disagree */
1950 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1951 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1952 {
1953 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 }
1955
1956 /*
1957 * Do not trust the IO-APIC being empty at bootup
1958 */
1959 clear_IO_APIC();
1960}
1961
1962/*
1963 * Not an __init, needed by the reboot code
1964 */
1965void disable_IO_APIC(void)
1966{
1967 /*
1968 * Clear the IO-APIC before rebooting:
1969 */
1970 clear_IO_APIC();
1971
Eric W. Biederman650927e2005-06-25 14:57:44 -07001972 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001973 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001974 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001975 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001976 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001977 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001978 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001979
1980 memset(&entry, 0, sizeof(entry));
1981 entry.mask = 0; /* Enabled */
1982 entry.trigger = 0; /* Edge */
1983 entry.irr = 0;
1984 entry.polarity = 0; /* High */
1985 entry.delivery_status = 0;
1986 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001987 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001988 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001989 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001990
1991 /*
1992 * Add it to the IO-APIC irq-routing table:
1993 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001994 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001995 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001996
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001997 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Ingo Molnar54168ed2008-08-20 09:07:45 +02002000#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001/*
2002 * function to set the IO-APIC physical IDs based on the
2003 * values stored in the MPC table.
2004 *
2005 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2006 */
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008static void __init setup_ioapic_ids_from_mpc(void)
2009{
2010 union IO_APIC_reg_00 reg_00;
2011 physid_mask_t phys_id_present_map;
2012 int apic;
2013 int i;
2014 unsigned char old_id;
2015 unsigned long flags;
2016
Yinghai Lua4dbc342008-07-25 02:14:28 -07002017 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07002018 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07002019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002021 * Don't check I/O APIC IDs for xAPIC systems. They have
2022 * no meaning without the serial APIC bus.
2023 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002024 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2025 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002026 return;
2027 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 * This is broken; anything with a real cpu count has to
2029 * circumvent this idiocy regardless.
2030 */
2031 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2032
2033 /*
2034 * Set the IOAPIC ID to the value stored in the MPC table.
2035 */
2036 for (apic = 0; apic < nr_ioapics; apic++) {
2037
2038 /* Read the register 0 value */
2039 spin_lock_irqsave(&ioapic_lock, flags);
2040 reg_00.raw = io_apic_read(apic, 0);
2041 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002042
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002043 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002045 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002047 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2049 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002050 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 /*
2054 * Sanity check, is the ID really free? Every APIC in a
2055 * system must have a unique ID or we get lots of nice
2056 * 'stuck on smp_invalidate_needed IPI wait' messages.
2057 */
2058 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002059 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002061 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 for (i = 0; i < get_physical_broadcast(); i++)
2063 if (!physid_isset(i, phys_id_present_map))
2064 break;
2065 if (i >= get_physical_broadcast())
2066 panic("Max APIC ID exceeded!\n");
2067 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2068 i);
2069 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002070 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 } else {
2072 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002073 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 apic_printk(APIC_VERBOSE, "Setting %d in the "
2075 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002076 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2078 }
2079
2080
2081 /*
2082 * We need to adjust the IRQ routing table
2083 * if the ID changed.
2084 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002085 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002087 if (mp_irqs[i].mp_dstapic == old_id)
2088 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002089 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 /*
2092 * Read the right value from the MPC table and
2093 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002094 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 apic_printk(APIC_VERBOSE, KERN_INFO
2096 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002097 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002099 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002101 io_apic_write(apic, 0, reg_00.raw);
2102 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 /*
2105 * Sanity check
2106 */
2107 spin_lock_irqsave(&ioapic_lock, flags);
2108 reg_00.raw = io_apic_read(apic, 0);
2109 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002110 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 printk("could not set ID!\n");
2112 else
2113 apic_printk(APIC_VERBOSE, " ok.\n");
2114 }
2115}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002118int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002119
2120static int __init notimercheck(char *s)
2121{
2122 no_timer_check = 1;
2123 return 1;
2124}
2125__setup("no_timer_check", notimercheck);
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127/*
2128 * There is a nasty bug in some older SMP boards, their mptable lies
2129 * about the timer IRQ. We do the following to work around the situation:
2130 *
2131 * - timer IRQ defaults to IO-APIC IRQ
2132 * - if this function detects that timer IRQs are defunct, then we fall
2133 * back to ISA timer IRQs
2134 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002135static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
2137 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002138 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Zachary Amsden8542b202006-12-07 02:14:09 +01002140 if (no_timer_check)
2141 return 1;
2142
Ingo Molnar4aae0702007-12-18 18:05:58 +01002143 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 local_irq_enable();
2145 /* Let ten ticks pass... */
2146 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002147 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /*
2150 * Expect a few ticks at least, to be sure some possible
2151 * glue logic does not lock up after one or two first
2152 * ticks in a non-ExtINT mode. Also the local APIC
2153 * might have cached one ExtINT interrupt. Finally, at
2154 * least one tick may be lost due to delays.
2155 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002156
2157 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002158 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return 0;
2161}
2162
2163/*
2164 * In the SMP+IOAPIC case it might happen that there are an unspecified
2165 * number of pending IRQ events unhandled. These cases are very rare,
2166 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2167 * better to do it this way as thus we do not have to be aware of
2168 * 'pending' interrupts in the IRQ path, except at this point.
2169 */
2170/*
2171 * Edge triggered needs to resend any interrupt
2172 * that was delayed but this is now handled in the device
2173 * independent code.
2174 */
2175
2176/*
2177 * Starting up a edge-triggered IO-APIC interrupt is
2178 * nasty - we need to make sure that we get the edge.
2179 * If it is already asserted for some reason, we need
2180 * return 1 to indicate that is was pending.
2181 *
2182 * This is not complete - we should be able to fake
2183 * an edge even if it isn't on the 8259A...
2184 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002185
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002186static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 int was_pending = 0;
2189 unsigned long flags;
2190
2191 spin_lock_irqsave(&ioapic_lock, flags);
2192 if (irq < 16) {
2193 disable_8259A_irq(irq);
2194 if (i8259A_irq_pending(irq))
2195 was_pending = 1;
2196 }
2197 __unmask_IO_APIC_irq(irq);
2198 spin_unlock_irqrestore(&ioapic_lock, flags);
2199
2200 return was_pending;
2201}
2202
Ingo Molnar54168ed2008-08-20 09:07:45 +02002203#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002204static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002206
2207 struct irq_cfg *cfg = irq_cfg(irq);
2208 unsigned long flags;
2209
2210 spin_lock_irqsave(&vector_lock, flags);
2211 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2212 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002213
2214 return 1;
2215}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002216#else
2217static int ioapic_retrigger_irq(unsigned int irq)
2218{
2219 send_IPI_self(irq_cfg(irq)->vector);
2220
2221 return 1;
2222}
2223#endif
2224
2225/*
2226 * Level and edge triggered IO-APIC interrupts need different handling,
2227 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2228 * handled with the level-triggered descriptor, but that one has slightly
2229 * more overhead. Level-triggered interrupts cannot be handled with the
2230 * edge-triggered handler, without risking IRQ storms and other ugly
2231 * races.
2232 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002233
Yinghai Lu497c9a12008-08-19 20:50:28 -07002234#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002235
2236#ifdef CONFIG_INTR_REMAP
2237static void ir_irq_migration(struct work_struct *work);
2238
2239static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2240
2241/*
2242 * Migrate the IO-APIC irq in the presence of intr-remapping.
2243 *
2244 * For edge triggered, irq migration is a simple atomic update(of vector
2245 * and cpu destination) of IRTE and flush the hardware cache.
2246 *
2247 * For level triggered, we need to modify the io-apic RTE aswell with the update
2248 * vector information, along with modifying IRTE with vector and destination.
2249 * So irq migration for level triggered is little bit more complex compared to
2250 * edge triggered migration. But the good news is, we use the same algorithm
2251 * for level triggered migration as we have today, only difference being,
2252 * we now initiate the irq migration from process context instead of the
2253 * interrupt context.
2254 *
2255 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2256 * suppression) to the IO-APIC, level triggered irq migration will also be
2257 * as simple as edge triggered migration and we can do the irq migration
2258 * with a simple atomic update to IO-APIC RTE.
2259 */
2260static void migrate_ioapic_irq(int irq, cpumask_t mask)
2261{
2262 struct irq_cfg *cfg;
2263 struct irq_desc *desc;
2264 cpumask_t tmp, cleanup_mask;
2265 struct irte irte;
2266 int modify_ioapic_rte;
2267 unsigned int dest;
2268 unsigned long flags;
2269
2270 cpus_and(tmp, mask, cpu_online_map);
2271 if (cpus_empty(tmp))
2272 return;
2273
2274 if (get_irte(irq, &irte))
2275 return;
2276
2277 if (assign_irq_vector(irq, mask))
2278 return;
2279
2280 cfg = irq_cfg(irq);
2281 cpus_and(tmp, cfg->domain, mask);
2282 dest = cpu_mask_to_apicid(tmp);
2283
2284 desc = irq_to_desc(irq);
2285 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2286 if (modify_ioapic_rte) {
2287 spin_lock_irqsave(&ioapic_lock, flags);
2288 __target_IO_APIC_irq(irq, dest, cfg->vector);
2289 spin_unlock_irqrestore(&ioapic_lock, flags);
2290 }
2291
2292 irte.vector = cfg->vector;
2293 irte.dest_id = IRTE_DEST(dest);
2294
2295 /*
2296 * Modified the IRTE and flushes the Interrupt entry cache.
2297 */
2298 modify_irte(irq, &irte);
2299
2300 if (cfg->move_in_progress) {
2301 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2302 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2303 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2304 cfg->move_in_progress = 0;
2305 }
2306
2307 desc->affinity = mask;
2308}
2309
2310static int migrate_irq_remapped_level(int irq)
2311{
2312 int ret = -1;
2313 struct irq_desc *desc = irq_to_desc(irq);
2314
2315 mask_IO_APIC_irq(irq);
2316
2317 if (io_apic_level_ack_pending(irq)) {
2318 /*
2319 * Interrupt in progress. Migrating irq now will change the
2320 * vector information in the IO-APIC RTE and that will confuse
2321 * the EOI broadcast performed by cpu.
2322 * So, delay the irq migration to the next instance.
2323 */
2324 schedule_delayed_work(&ir_migration_work, 1);
2325 goto unmask;
2326 }
2327
2328 /* everthing is clear. we have right of way */
2329 migrate_ioapic_irq(irq, desc->pending_mask);
2330
2331 ret = 0;
2332 desc->status &= ~IRQ_MOVE_PENDING;
2333 cpus_clear(desc->pending_mask);
2334
2335unmask:
2336 unmask_IO_APIC_irq(irq);
2337 return ret;
2338}
2339
2340static void ir_irq_migration(struct work_struct *work)
2341{
2342 unsigned int irq;
2343 struct irq_desc *desc;
2344
2345 for_each_irq_desc(irq, desc) {
2346 if (desc->status & IRQ_MOVE_PENDING) {
2347 unsigned long flags;
2348
2349 spin_lock_irqsave(&desc->lock, flags);
2350 if (!desc->chip->set_affinity ||
2351 !(desc->status & IRQ_MOVE_PENDING)) {
2352 desc->status &= ~IRQ_MOVE_PENDING;
2353 spin_unlock_irqrestore(&desc->lock, flags);
2354 continue;
2355 }
2356
2357 desc->chip->set_affinity(irq, desc->pending_mask);
2358 spin_unlock_irqrestore(&desc->lock, flags);
2359 }
2360 }
2361}
2362
2363/*
2364 * Migrates the IRQ destination in the process context.
2365 */
2366static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2367{
2368 struct irq_desc *desc = irq_to_desc(irq);
2369
2370 if (desc->status & IRQ_LEVEL) {
2371 desc->status |= IRQ_MOVE_PENDING;
2372 desc->pending_mask = mask;
2373 migrate_irq_remapped_level(irq);
2374 return;
2375 }
2376
2377 migrate_ioapic_irq(irq, mask);
2378}
2379#endif
2380
Yinghai Lu497c9a12008-08-19 20:50:28 -07002381asmlinkage void smp_irq_move_cleanup_interrupt(void)
2382{
2383 unsigned vector, me;
2384 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002385#ifdef CONFIG_X86_64
2386 exit_idle();
2387#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002388 irq_enter();
2389
2390 me = smp_processor_id();
2391 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2392 unsigned int irq;
2393 struct irq_desc *desc;
2394 struct irq_cfg *cfg;
2395 irq = __get_cpu_var(vector_irq)[vector];
2396
2397 desc = irq_to_desc(irq);
2398 if (!desc)
2399 continue;
2400
2401 cfg = irq_cfg(irq);
2402 spin_lock(&desc->lock);
2403 if (!cfg->move_cleanup_count)
2404 goto unlock;
2405
2406 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2407 goto unlock;
2408
2409 __get_cpu_var(vector_irq)[vector] = -1;
2410 cfg->move_cleanup_count--;
2411unlock:
2412 spin_unlock(&desc->lock);
2413 }
2414
2415 irq_exit();
2416}
2417
2418static void irq_complete_move(unsigned int irq)
2419{
2420 struct irq_cfg *cfg = irq_cfg(irq);
2421 unsigned vector, me;
2422
2423 if (likely(!cfg->move_in_progress))
2424 return;
2425
2426 vector = ~get_irq_regs()->orig_ax;
2427 me = smp_processor_id();
2428 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2429 cpumask_t cleanup_mask;
2430
2431 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2432 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2433 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2434 cfg->move_in_progress = 0;
2435 }
2436}
2437#else
2438static inline void irq_complete_move(unsigned int irq) {}
2439#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002440#ifdef CONFIG_INTR_REMAP
2441static void ack_x2apic_level(unsigned int irq)
2442{
2443 ack_x2APIC_irq();
2444}
2445
2446static void ack_x2apic_edge(unsigned int irq)
2447{
2448 ack_x2APIC_irq();
2449}
2450#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002451
Yinghai Lu1d025192008-08-19 20:50:34 -07002452static void ack_apic_edge(unsigned int irq)
2453{
2454 irq_complete_move(irq);
2455 move_native_irq(irq);
2456 ack_APIC_irq();
2457}
2458
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002459#ifdef CONFIG_X86_32
2460atomic_t irq_mis_count;
2461#endif
2462
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002463static void ack_apic_level(unsigned int irq)
2464{
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002465#ifdef CONFIG_X86_32
2466 unsigned long v;
2467 int i;
2468#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002469 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002470
Ingo Molnar54168ed2008-08-20 09:07:45 +02002471 irq_complete_move(irq);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002472#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002473 /* If we are moving the irq we need to mask it */
2474 if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2475 do_unmask_irq = 1;
2476 mask_IO_APIC_irq(irq);
2477 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002478#endif
2479
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002480#ifdef CONFIG_X86_32
2481 /*
2482 * It appears there is an erratum which affects at least version 0x11
2483 * of I/O APIC (that's the 82093AA and cores integrated into various
2484 * chipsets). Under certain conditions a level-triggered interrupt is
2485 * erroneously delivered as edge-triggered one but the respective IRR
2486 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2487 * message but it will never arrive and further interrupts are blocked
2488 * from the source. The exact reason is so far unknown, but the
2489 * phenomenon was observed when two consecutive interrupt requests
2490 * from a given source get delivered to the same CPU and the source is
2491 * temporarily disabled in between.
2492 *
2493 * A workaround is to simulate an EOI message manually. We achieve it
2494 * by setting the trigger mode to edge and then to level when the edge
2495 * trigger mode gets detected in the TMR of a local APIC for a
2496 * level-triggered interrupt. We mask the source for the time of the
2497 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2498 * The idea is from Manfred Spraul. --macro
2499 */
2500 i = irq_cfg(irq)->vector;
2501
2502 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2503#endif
2504
Ingo Molnar54168ed2008-08-20 09:07:45 +02002505 /*
2506 * We must acknowledge the irq before we move it or the acknowledge will
2507 * not propagate properly.
2508 */
2509 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002510
Ingo Molnar54168ed2008-08-20 09:07:45 +02002511 /* Now we can move and renable the irq */
2512 if (unlikely(do_unmask_irq)) {
2513 /* Only migrate the irq if the ack has been received.
2514 *
2515 * On rare occasions the broadcast level triggered ack gets
2516 * delayed going to ioapics, and if we reprogram the
2517 * vector while Remote IRR is still set the irq will never
2518 * fire again.
2519 *
2520 * To prevent this scenario we read the Remote IRR bit
2521 * of the ioapic. This has two effects.
2522 * - On any sane system the read of the ioapic will
2523 * flush writes (and acks) going to the ioapic from
2524 * this cpu.
2525 * - We get to see if the ACK has actually been delivered.
2526 *
2527 * Based on failed experiments of reprogramming the
2528 * ioapic entry from outside of irq context starting
2529 * with masking the ioapic entry and then polling until
2530 * Remote IRR was clear before reprogramming the
2531 * ioapic I don't trust the Remote IRR bit to be
2532 * completey accurate.
2533 *
2534 * However there appears to be no other way to plug
2535 * this race, so if the Remote IRR bit is not
2536 * accurate and is causing problems then it is a hardware bug
2537 * and you can go talk to the chipset vendor about it.
2538 */
2539 if (!io_apic_level_ack_pending(irq))
2540 move_masked_irq(irq);
2541 unmask_IO_APIC_irq(irq);
2542 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002543
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002544#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002545 if (!(v & (1 << (i & 0x1f)))) {
2546 atomic_inc(&irq_mis_count);
2547 spin_lock(&ioapic_lock);
2548 __mask_and_edge_IO_APIC_irq(irq);
2549 __unmask_and_level_IO_APIC_irq(irq);
2550 spin_unlock(&ioapic_lock);
2551 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002552#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002553}
Yinghai Lu1d025192008-08-19 20:50:34 -07002554
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002555static struct irq_chip ioapic_chip __read_mostly = {
2556 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002557 .startup = startup_ioapic_irq,
2558 .mask = mask_IO_APIC_irq,
2559 .unmask = unmask_IO_APIC_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07002560 .ack = ack_apic_edge,
2561 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002562#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002563 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002564#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002565 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566};
2567
Ingo Molnar54168ed2008-08-20 09:07:45 +02002568#ifdef CONFIG_INTR_REMAP
2569static struct irq_chip ir_ioapic_chip __read_mostly = {
2570 .name = "IR-IO-APIC",
2571 .startup = startup_ioapic_irq,
2572 .mask = mask_IO_APIC_irq,
2573 .unmask = unmask_IO_APIC_irq,
2574 .ack = ack_x2apic_edge,
2575 .eoi = ack_x2apic_level,
2576#ifdef CONFIG_SMP
2577 .set_affinity = set_ir_ioapic_affinity_irq,
2578#endif
2579 .retrigger = ioapic_retrigger_irq,
2580};
2581#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583static inline void init_IO_APIC_traps(void)
2584{
2585 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002586 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002587 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /*
2590 * NOTE! The local APIC isn't very good at handling
2591 * multiple interrupts at the same interrupt level.
2592 * As the interrupt level is determined by taking the
2593 * vector number and shifting that right by 4, we
2594 * want to spread these out a bit so that they don't
2595 * all fall in the same interrupt level.
2596 *
2597 * Also, we've got to be careful not to trash gate
2598 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2599 */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07002600 for_each_irq_cfg(irq, cfg) {
Yinghai Luda51a822008-08-19 20:50:25 -07002601 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /*
2603 * Hmm.. We don't have an entry for this,
2604 * so default to an old-fashioned 8259
2605 * interrupt if we can..
2606 */
2607 if (irq < 16)
2608 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002609 else {
2610 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002612 desc->chip = &no_irq_chip;
2613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
2615 }
2616}
2617
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002618/*
2619 * The local APIC irq-chip implementation:
2620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002622static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
2624 unsigned long v;
2625
2626 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002627 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002630static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002632 unsigned long v;
2633
2634 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002635 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
Ingo Molnar54168ed2008-08-20 09:07:45 +02002638static void ack_lapic_irq (unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002639{
2640 ack_APIC_irq();
2641}
2642
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002643static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002644 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002645 .mask = mask_lapic_irq,
2646 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002647 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648};
2649
Yinghai Lu497c9a12008-08-19 20:50:28 -07002650static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002651{
Yinghai Lu08678b02008-08-19 20:50:05 -07002652 struct irq_desc *desc;
2653
2654 desc = irq_to_desc(irq);
2655 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002656 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2657 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002658}
2659
Jan Beuliche9427102008-01-30 13:31:24 +01002660static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002663 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 * We put the 8259A master into AEOI mode and
2665 * unmask on all local APICs LVT0 as NMI.
2666 *
2667 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2668 * is from Maciej W. Rozycki - so we do not have to EOI from
2669 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2672
Jan Beuliche9427102008-01-30 13:31:24 +01002673 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 apic_printk(APIC_VERBOSE, " done.\n");
2676}
2677
2678/*
2679 * This looks a bit hackish but it's about the only one way of sending
2680 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2681 * not support the ExtINT mode, unfortunately. We need to send these
2682 * cycles as some i82489DX-based boards have glue logic that keeps the
2683 * 8259A interrupt line asserted until INTA. --macro
2684 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002685static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002687 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 struct IO_APIC_route_entry entry0, entry1;
2689 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002691 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002692 if (pin == -1) {
2693 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002695 }
2696 apic = find_isa_irq_apic(8, mp_INT);
2697 if (apic == -1) {
2698 WARN_ON_ONCE(1);
2699 return;
2700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Andi Kleencf4c6a22006-09-26 10:52:30 +02002702 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002703 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 memset(&entry1, 0, sizeof(entry1));
2706
2707 entry1.dest_mode = 0; /* physical delivery */
2708 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002709 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 entry1.delivery_mode = dest_ExtINT;
2711 entry1.polarity = entry0.polarity;
2712 entry1.trigger = 0;
2713 entry1.vector = 0;
2714
Andi Kleencf4c6a22006-09-26 10:52:30 +02002715 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 save_control = CMOS_READ(RTC_CONTROL);
2718 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2719 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2720 RTC_FREQ_SELECT);
2721 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2722
2723 i = 100;
2724 while (i-- > 0) {
2725 mdelay(10);
2726 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2727 i -= 10;
2728 }
2729
2730 CMOS_WRITE(save_control, RTC_CONTROL);
2731 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002732 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Andi Kleencf4c6a22006-09-26 10:52:30 +02002734 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Yinghai Luefa25592008-08-19 20:50:36 -07002737static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002738/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002739static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002740{
2741 disable_timer_pin_1 = 1;
2742 return 0;
2743}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002744early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002745
2746int timer_through_8259 __initdata;
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748/*
2749 * This code may look a bit paranoid, but it's supposed to cooperate with
2750 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2751 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2752 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002753 *
2754 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002756static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002758 struct irq_cfg *cfg = irq_cfg(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002759 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002760 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002761 unsigned int ver;
2762 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002763
2764 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002765
Ingo Molnar54168ed2008-08-20 09:07:45 +02002766 ver = apic_read(APIC_LVR);
2767 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 /*
2770 * get/set the timer IRQ vector:
2771 */
2772 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002773 assign_irq_vector(0, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
2775 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002776 * As IRQ0 is to be enabled in the 8259A, the virtual
2777 * wire has to be disabled in the local APIC. Also
2778 * timer interrupts need to be acknowledged manually in
2779 * the 8259A for the i82489DX when using the NMI
2780 * watchdog as that APIC treats NMIs as level-triggered.
2781 * The AEOI mode will finish them in the 8259A
2782 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002784 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002786#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002787 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002788#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002790 pin1 = find_isa_irq_pin(0, mp_INT);
2791 apic1 = find_isa_irq_apic(0, mp_INT);
2792 pin2 = ioapic_i8259.pin;
2793 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002795 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2796 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002797 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002799 /*
2800 * Some BIOS writers are clueless and report the ExtINTA
2801 * I/O APIC input from the cascaded 8259A as the timer
2802 * interrupt input. So just in case, if only one pin
2803 * was found above, try it both directly and through the
2804 * 8259A.
2805 */
2806 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002807#ifdef CONFIG_INTR_REMAP
2808 if (intr_remapping_enabled)
2809 panic("BIOS bug: timer not connected to IO-APIC");
2810#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002811 pin1 = pin2;
2812 apic1 = apic2;
2813 no_pin1 = 1;
2814 } else if (pin2 == -1) {
2815 pin2 = pin1;
2816 apic2 = apic1;
2817 }
2818
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 if (pin1 != -1) {
2820 /*
2821 * Ok, does IRQ0 through the IOAPIC work?
2822 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002823 if (no_pin1) {
2824 add_pin_to_irq(0, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002825 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 unmask_IO_APIC_irq(0);
2828 if (timer_irq_works()) {
2829 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 setup_nmi();
2831 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002833 if (disable_timer_pin_1 > 0)
2834 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002835 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002837#ifdef CONFIG_INTR_REMAP
2838 if (intr_remapping_enabled)
2839 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2840#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002841 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002842 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002843 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2844 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002846 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2847 "(IRQ0) through the 8259A ...\n");
2848 apic_printk(APIC_QUIET, KERN_INFO
2849 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 /*
2851 * legacy devices should be connected to IO APIC #0
2852 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002853 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002854 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002855 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002856 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002858 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002859 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002861 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002863 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 }
2867 /*
2868 * Cleanup, just in case ...
2869 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002870 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002871 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002872 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002876 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2877 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002878 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002880#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002881 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002882#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002884 apic_printk(APIC_QUIET, KERN_INFO
2885 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Yinghai Lu497c9a12008-08-19 20:50:28 -07002887 lapic_register_intr(0);
2888 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 enable_8259A_irq(0);
2890
2891 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002892 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002893 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002895 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002896 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002897 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002899 apic_printk(APIC_QUIET, KERN_INFO
2900 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 init_8259A(0);
2903 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002904 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906 unlock_ExtINT_logic();
2907
2908 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002909 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002910 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002912 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002914 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002915out:
2916 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
2919/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002920 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2921 * to devices. However there may be an I/O APIC pin available for
2922 * this interrupt regardless. The pin may be left unconnected, but
2923 * typically it will be reused as an ExtINT cascade interrupt for
2924 * the master 8259A. In the MPS case such a pin will normally be
2925 * reported as an ExtINT interrupt in the MP table. With ACPI
2926 * there is no provision for ExtINT interrupts, and in the absence
2927 * of an override it would be treated as an ordinary ISA I/O APIC
2928 * interrupt, that is edge-triggered and unmasked by default. We
2929 * used to do this, but it caused problems on some systems because
2930 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2931 * the same ExtINT cascade interrupt to drive the local APIC of the
2932 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2933 * the I/O APIC in all cases now. No actual device should request
2934 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 */
2936#define PIC_IRQS (1 << PIC_CASCADE_IR)
2937
2938void __init setup_IO_APIC(void)
2939{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002940
2941#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002943#else
2944 /*
2945 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2946 */
2947#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002949 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Ingo Molnar54168ed2008-08-20 09:07:45 +02002951 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2952 /*
2953 * Set up IO-APIC IRQ routing.
2954 */
2955#ifdef CONFIG_X86_32
2956 if (!acpi_ioapic)
2957 setup_ioapic_ids_from_mpc();
2958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 sync_Arb_IDs();
2960 setup_IO_APIC_irqs();
2961 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002962 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
2965/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002966 * Called after all the initialization is done. If we didnt find any
2967 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970static int __init io_apic_bug_finalize(void)
2971{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002972 if (sis_apic_bug == -1)
2973 sis_apic_bug = 0;
2974 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
2977late_initcall(io_apic_bug_finalize);
2978
2979struct sysfs_ioapic_data {
2980 struct sys_device dev;
2981 struct IO_APIC_route_entry entry[0];
2982};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002983static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Pavel Machek438510f2005-04-16 15:25:24 -07002985static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986{
2987 struct IO_APIC_route_entry *entry;
2988 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 data = container_of(dev, struct sysfs_ioapic_data, dev);
2992 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002993 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2994 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 return 0;
2997}
2998
2999static int ioapic_resume(struct sys_device *dev)
3000{
3001 struct IO_APIC_route_entry *entry;
3002 struct sysfs_ioapic_data *data;
3003 unsigned long flags;
3004 union IO_APIC_reg_00 reg_00;
3005 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003006
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 data = container_of(dev, struct sysfs_ioapic_data, dev);
3008 entry = data->entry;
3009
3010 spin_lock_irqsave(&ioapic_lock, flags);
3011 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04003012 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
3013 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 io_apic_write(dev->id, 0, reg_00.raw);
3015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003017 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003018 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
3020 return 0;
3021}
3022
3023static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003024 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 .suspend = ioapic_suspend,
3026 .resume = ioapic_resume,
3027};
3028
3029static int __init ioapic_init_sysfs(void)
3030{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003031 struct sys_device * dev;
3032 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 error = sysdev_class_register(&ioapic_sysdev_class);
3035 if (error)
3036 return error;
3037
Ingo Molnar54168ed2008-08-20 09:07:45 +02003038 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003039 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003041 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (!mp_ioapic_data[i]) {
3043 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3044 continue;
3045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003047 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 dev->cls = &ioapic_sysdev_class;
3049 error = sysdev_register(dev);
3050 if (error) {
3051 kfree(mp_ioapic_data[i]);
3052 mp_ioapic_data[i] = NULL;
3053 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3054 continue;
3055 }
3056 }
3057
3058 return 0;
3059}
3060
3061device_initcall(ioapic_init_sysfs);
3062
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003063/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003064 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003065 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003066unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003067{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003068 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003069 unsigned int irq;
3070 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003071 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07003072 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003073
Yinghai Lu497c9a12008-08-19 20:50:28 -07003074#ifndef CONFIG_HAVE_SPARSE_IRQ
Yinghai Lu199751d2008-08-19 20:50:27 -07003075 irq_want = nr_irqs - 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003076#endif
Yinghai Lu199751d2008-08-19 20:50:27 -07003077
3078 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003079 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003080 for (new = irq_want; new > 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003081 if (platform_legacy_irq(new))
3082 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07003083 cfg_new = irq_cfg(new);
3084 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003085 continue;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003086 /* check if need to create one */
Yinghai Luda51a822008-08-19 20:50:25 -07003087 if (!cfg_new)
3088 cfg_new = irq_cfg_alloc(new);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003089 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003090 irq = new;
3091 break;
3092 }
3093 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003094
Yinghai Lu199751d2008-08-19 20:50:27 -07003095 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003096 dynamic_irq_init(irq);
3097 }
3098 return irq;
3099}
3100
Yinghai Lu199751d2008-08-19 20:50:27 -07003101int create_irq(void)
3102{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003103 int irq;
3104
3105 irq = create_irq_nr(nr_irqs - 1);
3106
3107 if (irq == 0)
3108 irq = -1;
3109
3110 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003111}
3112
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003113void destroy_irq(unsigned int irq)
3114{
3115 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003116
3117 dynamic_irq_cleanup(irq);
3118
Ingo Molnar54168ed2008-08-20 09:07:45 +02003119#ifdef CONFIG_INTR_REMAP
3120 free_irte(irq);
3121#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003122 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003123 __clear_irq_vector(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003124 spin_unlock_irqrestore(&vector_lock, flags);
3125}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003126
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003127/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003128 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003129 */
3130#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003131static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003132{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003133 struct irq_cfg *cfg;
3134 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003135 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003136 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003137
Yinghai Lu497c9a12008-08-19 20:50:28 -07003138 tmp = TARGET_CPUS;
3139 err = assign_irq_vector(irq, tmp);
3140 if (err)
3141 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003142
Yinghai Lu497c9a12008-08-19 20:50:28 -07003143 cfg = irq_cfg(irq);
3144 cpus_and(tmp, cfg->domain, tmp);
3145 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003146
Ingo Molnar54168ed2008-08-20 09:07:45 +02003147#ifdef CONFIG_INTR_REMAP
3148 if (irq_remapped(irq)) {
3149 struct irte irte;
3150 int ir_index;
3151 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003152
Ingo Molnar54168ed2008-08-20 09:07:45 +02003153 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3154 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003155
Ingo Molnar54168ed2008-08-20 09:07:45 +02003156 memset (&irte, 0, sizeof(irte));
3157
3158 irte.present = 1;
3159 irte.dst_mode = INT_DEST_MODE;
3160 irte.trigger_mode = 0; /* edge */
3161 irte.dlvry_mode = INT_DELIVERY_MODE;
3162 irte.vector = cfg->vector;
3163 irte.dest_id = IRTE_DEST(dest);
3164
3165 modify_irte(irq, &irte);
3166
3167 msg->address_hi = MSI_ADDR_BASE_HI;
3168 msg->data = sub_handle;
3169 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3170 MSI_ADDR_IR_SHV |
3171 MSI_ADDR_IR_INDEX1(ir_index) |
3172 MSI_ADDR_IR_INDEX2(ir_index);
3173 } else
3174#endif
3175 {
3176 msg->address_hi = MSI_ADDR_BASE_HI;
3177 msg->address_lo =
3178 MSI_ADDR_BASE_LO |
3179 ((INT_DEST_MODE == 0) ?
3180 MSI_ADDR_DEST_MODE_PHYSICAL:
3181 MSI_ADDR_DEST_MODE_LOGICAL) |
3182 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3183 MSI_ADDR_REDIRECTION_CPU:
3184 MSI_ADDR_REDIRECTION_LOWPRI) |
3185 MSI_ADDR_DEST_ID(dest);
3186
3187 msg->data =
3188 MSI_DATA_TRIGGER_EDGE |
3189 MSI_DATA_LEVEL_ASSERT |
3190 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3191 MSI_DATA_DELIVERY_FIXED:
3192 MSI_DATA_DELIVERY_LOWPRI) |
3193 MSI_DATA_VECTOR(cfg->vector);
3194 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003195 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003196}
3197
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003198#ifdef CONFIG_SMP
3199static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3200{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003201 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003202 struct msi_msg msg;
3203 unsigned int dest;
3204 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003205 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003206
3207 cpus_and(tmp, mask, cpu_online_map);
3208 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003209 return;
3210
Yinghai Lu497c9a12008-08-19 20:50:28 -07003211 if (assign_irq_vector(irq, mask))
3212 return;
3213
3214 cfg = irq_cfg(irq);
3215 cpus_and(tmp, cfg->domain, mask);
3216 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003217
3218 read_msi_msg(irq, &msg);
3219
3220 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003221 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003222 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3223 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3224
3225 write_msi_msg(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003226 desc = irq_to_desc(irq);
3227 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003228}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003229
3230#ifdef CONFIG_INTR_REMAP
3231/*
3232 * Migrate the MSI irq to another cpumask. This migration is
3233 * done in the process context using interrupt-remapping hardware.
3234 */
3235static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3236{
3237 struct irq_cfg *cfg;
3238 unsigned int dest;
3239 cpumask_t tmp, cleanup_mask;
3240 struct irte irte;
3241 struct irq_desc *desc;
3242
3243 cpus_and(tmp, mask, cpu_online_map);
3244 if (cpus_empty(tmp))
3245 return;
3246
3247 if (get_irte(irq, &irte))
3248 return;
3249
3250 if (assign_irq_vector(irq, mask))
3251 return;
3252
3253 cfg = irq_cfg(irq);
3254 cpus_and(tmp, cfg->domain, mask);
3255 dest = cpu_mask_to_apicid(tmp);
3256
3257 irte.vector = cfg->vector;
3258 irte.dest_id = IRTE_DEST(dest);
3259
3260 /*
3261 * atomically update the IRTE with the new destination and vector.
3262 */
3263 modify_irte(irq, &irte);
3264
3265 /*
3266 * After this point, all the interrupts will start arriving
3267 * at the new destination. So, time to cleanup the previous
3268 * vector allocation.
3269 */
3270 if (cfg->move_in_progress) {
3271 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3272 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3273 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3274 cfg->move_in_progress = 0;
3275 }
3276
3277 desc = irq_to_desc(irq);
3278 desc->affinity = mask;
3279}
3280#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003281#endif /* CONFIG_SMP */
3282
3283/*
3284 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3285 * which implement the MSI or MSI-X Capability Structure.
3286 */
3287static struct irq_chip msi_chip = {
3288 .name = "PCI-MSI",
3289 .unmask = unmask_msi_irq,
3290 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003291 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003292#ifdef CONFIG_SMP
3293 .set_affinity = set_msi_irq_affinity,
3294#endif
3295 .retrigger = ioapic_retrigger_irq,
3296};
3297
Ingo Molnar54168ed2008-08-20 09:07:45 +02003298#ifdef CONFIG_INTR_REMAP
3299static struct irq_chip msi_ir_chip = {
3300 .name = "IR-PCI-MSI",
3301 .unmask = unmask_msi_irq,
3302 .mask = mask_msi_irq,
3303 .ack = ack_x2apic_edge,
3304#ifdef CONFIG_SMP
3305 .set_affinity = ir_set_msi_irq_affinity,
3306#endif
3307 .retrigger = ioapic_retrigger_irq,
3308};
3309
3310/*
3311 * Map the PCI dev to the corresponding remapping hardware unit
3312 * and allocate 'nvec' consecutive interrupt-remapping table entries
3313 * in it.
3314 */
3315static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3316{
3317 struct intel_iommu *iommu;
3318 int index;
3319
3320 iommu = map_dev_to_ir(dev);
3321 if (!iommu) {
3322 printk(KERN_ERR
3323 "Unable to map PCI %s to iommu\n", pci_name(dev));
3324 return -ENOENT;
3325 }
3326
3327 index = alloc_irte(iommu, irq, nvec);
3328 if (index < 0) {
3329 printk(KERN_ERR
3330 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3331 pci_name(dev));
3332 return -ENOSPC;
3333 }
3334 return index;
3335}
3336#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003337
3338static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3339{
3340 int ret;
3341 struct msi_msg msg;
3342
3343 ret = msi_compose_msg(dev, irq, &msg);
3344 if (ret < 0)
3345 return ret;
3346
3347 set_irq_msi(irq, desc);
3348 write_msi_msg(irq, &msg);
3349
Ingo Molnar54168ed2008-08-20 09:07:45 +02003350#ifdef CONFIG_INTR_REMAP
3351 if (irq_remapped(irq)) {
3352 struct irq_desc *desc = irq_to_desc(irq);
3353 /*
3354 * irq migration in process context
3355 */
3356 desc->status |= IRQ_MOVE_PCNTXT;
3357 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3358 } else
3359#endif
3360 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003361
3362 return 0;
3363}
3364
Yinghai Lu199751d2008-08-19 20:50:27 -07003365static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3366{
3367 unsigned int irq;
3368
3369 irq = dev->bus->number;
3370 irq <<= 8;
3371 irq |= dev->devfn;
3372 irq <<= 12;
3373
3374 return irq;
3375}
3376
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003377int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003378{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003379 unsigned int irq;
3380 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003381 unsigned int irq_want;
3382
3383 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3384
3385 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003386 if (irq == 0)
3387 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003388
Ingo Molnar54168ed2008-08-20 09:07:45 +02003389#ifdef CONFIG_INTR_REMAP
3390 if (!intr_remapping_enabled)
3391 goto no_ir;
3392
3393 ret = msi_alloc_irte(dev, irq, 1);
3394 if (ret < 0)
3395 goto error;
3396no_ir:
3397#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003398 ret = setup_msi_irq(dev, desc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003399 if (ret < 0) {
3400 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003401 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003402 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003403 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003404
3405#ifdef CONFIG_INTR_REMAP
3406error:
3407 destroy_irq(irq);
3408 return ret;
3409#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003410}
3411
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003412int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3413{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003414 unsigned int irq;
3415 int ret, sub_handle;
3416 struct msi_desc *desc;
3417 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003418
Ingo Molnar54168ed2008-08-20 09:07:45 +02003419#ifdef CONFIG_INTR_REMAP
3420 struct intel_iommu *iommu = 0;
3421 int index = 0;
3422#endif
3423
3424 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3425 sub_handle = 0;
3426 list_for_each_entry(desc, &dev->msi_list, list) {
3427 irq = create_irq_nr(irq_want--);
3428 if (irq == 0)
3429 return -1;
3430#ifdef CONFIG_INTR_REMAP
3431 if (!intr_remapping_enabled)
3432 goto no_ir;
3433
3434 if (!sub_handle) {
3435 /*
3436 * allocate the consecutive block of IRTE's
3437 * for 'nvec'
3438 */
3439 index = msi_alloc_irte(dev, irq, nvec);
3440 if (index < 0) {
3441 ret = index;
3442 goto error;
3443 }
3444 } else {
3445 iommu = map_dev_to_ir(dev);
3446 if (!iommu) {
3447 ret = -ENOENT;
3448 goto error;
3449 }
3450 /*
3451 * setup the mapping between the irq and the IRTE
3452 * base index, the sub_handle pointing to the
3453 * appropriate interrupt remap table entry.
3454 */
3455 set_irte_irq(irq, iommu, index, sub_handle);
3456 }
3457no_ir:
3458#endif
3459 ret = setup_msi_irq(dev, desc, irq);
3460 if (ret < 0)
3461 goto error;
3462 sub_handle++;
3463 }
3464 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003465
3466error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003467 destroy_irq(irq);
3468 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003469}
3470
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003471void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003472{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003473 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003474}
3475
Ingo Molnar54168ed2008-08-20 09:07:45 +02003476#ifdef CONFIG_DMAR
3477#ifdef CONFIG_SMP
3478static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3479{
3480 struct irq_cfg *cfg;
3481 struct msi_msg msg;
3482 unsigned int dest;
3483 cpumask_t tmp;
3484 struct irq_desc *desc;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003485
Ingo Molnar54168ed2008-08-20 09:07:45 +02003486 cpus_and(tmp, mask, cpu_online_map);
3487 if (cpus_empty(tmp))
3488 return;
3489
3490 if (assign_irq_vector(irq, mask))
3491 return;
3492
3493 cfg = irq_cfg(irq);
3494 cpus_and(tmp, cfg->domain, mask);
3495 dest = cpu_mask_to_apicid(tmp);
3496
3497 dmar_msi_read(irq, &msg);
3498
3499 msg.data &= ~MSI_DATA_VECTOR_MASK;
3500 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3501 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3502 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3503
3504 dmar_msi_write(irq, &msg);
3505 desc = irq_to_desc(irq);
3506 desc->affinity = mask;
3507}
3508#endif /* CONFIG_SMP */
3509
3510struct irq_chip dmar_msi_type = {
3511 .name = "DMAR_MSI",
3512 .unmask = dmar_msi_unmask,
3513 .mask = dmar_msi_mask,
3514 .ack = ack_apic_edge,
3515#ifdef CONFIG_SMP
3516 .set_affinity = dmar_msi_set_affinity,
3517#endif
3518 .retrigger = ioapic_retrigger_irq,
3519};
3520
3521int arch_setup_dmar_msi(unsigned int irq)
3522{
3523 int ret;
3524 struct msi_msg msg;
3525
3526 ret = msi_compose_msg(NULL, irq, &msg);
3527 if (ret < 0)
3528 return ret;
3529 dmar_msi_write(irq, &msg);
3530 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3531 "edge");
3532 return 0;
3533}
3534#endif
3535
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003536#ifdef CONFIG_HPET_TIMER
3537
3538#ifdef CONFIG_SMP
3539static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
3540{
3541 struct irq_cfg *cfg;
3542 struct irq_desc *desc;
3543 struct msi_msg msg;
3544 unsigned int dest;
3545 cpumask_t tmp;
3546
3547 cpus_and(tmp, mask, cpu_online_map);
3548 if (cpus_empty(tmp))
3549 return;
3550
3551 if (assign_irq_vector(irq, mask))
3552 return;
3553
3554 cfg = irq_cfg(irq);
3555 cpus_and(tmp, cfg->domain, mask);
3556 dest = cpu_mask_to_apicid(tmp);
3557
3558 hpet_msi_read(irq, &msg);
3559
3560 msg.data &= ~MSI_DATA_VECTOR_MASK;
3561 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3562 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3563 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3564
3565 hpet_msi_write(irq, &msg);
3566 desc = irq_to_desc(irq);
3567 desc->affinity = mask;
3568}
3569#endif /* CONFIG_SMP */
3570
3571struct irq_chip hpet_msi_type = {
3572 .name = "HPET_MSI",
3573 .unmask = hpet_msi_unmask,
3574 .mask = hpet_msi_mask,
3575 .ack = ack_apic_edge,
3576#ifdef CONFIG_SMP
3577 .set_affinity = hpet_msi_set_affinity,
3578#endif
3579 .retrigger = ioapic_retrigger_irq,
3580};
3581
3582int arch_setup_hpet_msi(unsigned int irq)
3583{
3584 int ret;
3585 struct msi_msg msg;
3586
3587 ret = msi_compose_msg(NULL, irq, &msg);
3588 if (ret < 0)
3589 return ret;
3590
3591 hpet_msi_write(irq, &msg);
3592 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3593 "edge");
3594 return 0;
3595}
3596#endif
3597
Ingo Molnar54168ed2008-08-20 09:07:45 +02003598#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003599/*
3600 * Hypertransport interrupt support
3601 */
3602#ifdef CONFIG_HT_IRQ
3603
3604#ifdef CONFIG_SMP
3605
Yinghai Lu497c9a12008-08-19 20:50:28 -07003606static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003607{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003608 struct ht_irq_msg msg;
3609 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003610
Yinghai Lu497c9a12008-08-19 20:50:28 -07003611 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003612 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003613
Yinghai Lu497c9a12008-08-19 20:50:28 -07003614 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003615 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003616
Eric W. Biedermanec683072006-11-08 17:44:57 -08003617 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003618}
3619
3620static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3621{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003622 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003623 unsigned int dest;
3624 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003625 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003626
3627 cpus_and(tmp, mask, cpu_online_map);
3628 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003629 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003630
Yinghai Lu497c9a12008-08-19 20:50:28 -07003631 if (assign_irq_vector(irq, mask))
3632 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003633
Yinghai Lu497c9a12008-08-19 20:50:28 -07003634 cfg = irq_cfg(irq);
3635 cpus_and(tmp, cfg->domain, mask);
3636 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003637
Yinghai Lu497c9a12008-08-19 20:50:28 -07003638 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003639 desc = irq_to_desc(irq);
3640 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003641}
3642#endif
3643
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003644static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003645 .name = "PCI-HT",
3646 .mask = mask_ht_irq,
3647 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003648 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003649#ifdef CONFIG_SMP
3650 .set_affinity = set_ht_irq_affinity,
3651#endif
3652 .retrigger = ioapic_retrigger_irq,
3653};
3654
3655int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3656{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003657 struct irq_cfg *cfg;
3658 int err;
3659 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003660
Yinghai Lu497c9a12008-08-19 20:50:28 -07003661 tmp = TARGET_CPUS;
3662 err = assign_irq_vector(irq, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003663 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003664 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003665 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003666
Yinghai Lu497c9a12008-08-19 20:50:28 -07003667 cfg = irq_cfg(irq);
3668 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003669 dest = cpu_mask_to_apicid(tmp);
3670
Eric W. Biedermanec683072006-11-08 17:44:57 -08003671 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003672
Eric W. Biedermanec683072006-11-08 17:44:57 -08003673 msg.address_lo =
3674 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003675 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003676 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003677 ((INT_DEST_MODE == 0) ?
3678 HT_IRQ_LOW_DM_PHYSICAL :
3679 HT_IRQ_LOW_DM_LOGICAL) |
3680 HT_IRQ_LOW_RQEOI_EDGE |
3681 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3682 HT_IRQ_LOW_MT_FIXED :
3683 HT_IRQ_LOW_MT_ARBITRATED) |
3684 HT_IRQ_LOW_IRQ_MASKED;
3685
Eric W. Biedermanec683072006-11-08 17:44:57 -08003686 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003687
Ingo Molnara460e742006-10-17 00:10:03 -07003688 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3689 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003690 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003691 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003692}
3693#endif /* CONFIG_HT_IRQ */
3694
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003695int __init io_apic_get_redir_entries (int ioapic)
3696{
3697 union IO_APIC_reg_01 reg_01;
3698 unsigned long flags;
3699
3700 spin_lock_irqsave(&ioapic_lock, flags);
3701 reg_01.raw = io_apic_read(ioapic, 1);
3702 spin_unlock_irqrestore(&ioapic_lock, flags);
3703
3704 return reg_01.bits.entries;
3705}
3706
3707int __init probe_nr_irqs(void)
3708{
3709 int idx;
3710 int nr = 0;
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003711#ifndef CONFIG_XEN
3712 int nr_min = 32;
3713#else
3714 int nr_min = NR_IRQS;
3715#endif
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003716
3717 for (idx = 0; idx < nr_ioapics; idx++)
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003718 nr += io_apic_get_redir_entries(idx) + 1;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003719
3720 /* double it for hotplug and msi and nmi */
3721 nr <<= 1;
3722
3723 /* something wrong ? */
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003724 if (nr < nr_min)
3725 nr = nr_min;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003726
3727 return nr;
3728}
3729
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003731 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 -------------------------------------------------------------------------- */
3733
Len Brown888ba6c2005-08-24 12:07:20 -04003734#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Ingo Molnar54168ed2008-08-20 09:07:45 +02003736#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003737int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738{
3739 union IO_APIC_reg_00 reg_00;
3740 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3741 physid_mask_t tmp;
3742 unsigned long flags;
3743 int i = 0;
3744
3745 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003746 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3747 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003749 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3751 * advantage of new APIC bus architecture.
3752 */
3753
3754 if (physids_empty(apic_id_map))
3755 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3756
3757 spin_lock_irqsave(&ioapic_lock, flags);
3758 reg_00.raw = io_apic_read(ioapic, 0);
3759 spin_unlock_irqrestore(&ioapic_lock, flags);
3760
3761 if (apic_id >= get_physical_broadcast()) {
3762 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3763 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3764 apic_id = reg_00.bits.ID;
3765 }
3766
3767 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003768 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 * 'stuck on smp_invalidate_needed IPI wait' messages.
3770 */
3771 if (check_apicid_used(apic_id_map, apic_id)) {
3772
3773 for (i = 0; i < get_physical_broadcast(); i++) {
3774 if (!check_apicid_used(apic_id_map, i))
3775 break;
3776 }
3777
3778 if (i == get_physical_broadcast())
3779 panic("Max apic_id exceeded!\n");
3780
3781 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3782 "trying %d\n", ioapic, apic_id, i);
3783
3784 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786
3787 tmp = apicid_to_cpu_present(apic_id);
3788 physids_or(apic_id_map, apic_id_map, tmp);
3789
3790 if (reg_00.bits.ID != apic_id) {
3791 reg_00.bits.ID = apic_id;
3792
3793 spin_lock_irqsave(&ioapic_lock, flags);
3794 io_apic_write(ioapic, 0, reg_00.raw);
3795 reg_00.raw = io_apic_read(ioapic, 0);
3796 spin_unlock_irqrestore(&ioapic_lock, flags);
3797
3798 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003799 if (reg_00.bits.ID != apic_id) {
3800 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3801 return -1;
3802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 }
3804
3805 apic_printk(APIC_VERBOSE, KERN_INFO
3806 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3807
3808 return apic_id;
3809}
3810
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003811int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813 union IO_APIC_reg_01 reg_01;
3814 unsigned long flags;
3815
3816 spin_lock_irqsave(&ioapic_lock, flags);
3817 reg_01.raw = io_apic_read(ioapic, 1);
3818 spin_unlock_irqrestore(&ioapic_lock, flags);
3819
3820 return reg_01.bits.version;
3821}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003822#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823
Ingo Molnar54168ed2008-08-20 09:07:45 +02003824int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003827 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 ioapic);
3829 return -EINVAL;
3830 }
3831
3832 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 * IRQs < 16 are already in the irq_2_pin[] map
3834 */
3835 if (irq >= 16)
3836 add_pin_to_irq(irq, ioapic, pin);
3837
Yinghai Lu497c9a12008-08-19 20:50:28 -07003838 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
3840 return 0;
3841}
3842
Ingo Molnar54168ed2008-08-20 09:07:45 +02003843
Shaohua Li61fd47e2007-11-17 01:05:28 -05003844int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3845{
3846 int i;
3847
3848 if (skip_ioapic_setup)
3849 return -1;
3850
3851 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003852 if (mp_irqs[i].mp_irqtype == mp_INT &&
3853 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003854 break;
3855 if (i >= mp_irq_entries)
3856 return -1;
3857
3858 *trigger = irq_trigger(i);
3859 *polarity = irq_polarity(i);
3860 return 0;
3861}
3862
Len Brown888ba6c2005-08-24 12:07:20 -04003863#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003864
Yinghai Lu497c9a12008-08-19 20:50:28 -07003865/*
3866 * This function currently is only a helper for the i386 smp boot process where
3867 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3868 * so mask in all cases should simply be TARGET_CPUS
3869 */
3870#ifdef CONFIG_SMP
3871void __init setup_ioapic_dest(void)
3872{
3873 int pin, ioapic, irq, irq_entry;
3874 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003875
3876 if (skip_ioapic_setup == 1)
3877 return;
3878
3879 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3880 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3881 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3882 if (irq_entry == -1)
3883 continue;
3884 irq = pin_2_irq(irq_entry, ioapic, pin);
3885
3886 /* setup_IO_APIC_irqs could fail to get vector for some device
3887 * when you have too many devices, because at that time only boot
3888 * cpu is online.
3889 */
3890 cfg = irq_cfg(irq);
3891 if (!cfg->vector)
3892 setup_IO_APIC_irq(ioapic, pin, irq,
3893 irq_trigger(irq_entry),
3894 irq_polarity(irq_entry));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003895#ifdef CONFIG_INTR_REMAP
3896 else if (intr_remapping_enabled)
3897 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
3898#endif
3899 else
Yinghai Lu497c9a12008-08-19 20:50:28 -07003900 set_ioapic_affinity_irq(irq, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003901 }
3902
3903 }
3904}
3905#endif
3906
Ingo Molnar54168ed2008-08-20 09:07:45 +02003907#define IOAPIC_RESOURCE_NAME_SIZE 11
3908
3909static struct resource *ioapic_resources;
3910
3911static struct resource * __init ioapic_setup_resources(void)
3912{
3913 unsigned long n;
3914 struct resource *res;
3915 char *mem;
3916 int i;
3917
3918 if (nr_ioapics <= 0)
3919 return NULL;
3920
3921 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3922 n *= nr_ioapics;
3923
3924 mem = alloc_bootmem(n);
3925 res = (void *)mem;
3926
3927 if (mem != NULL) {
3928 mem += sizeof(struct resource) * nr_ioapics;
3929
3930 for (i = 0; i < nr_ioapics; i++) {
3931 res[i].name = mem;
3932 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3933 sprintf(mem, "IOAPIC %u", i);
3934 mem += IOAPIC_RESOURCE_NAME_SIZE;
3935 }
3936 }
3937
3938 ioapic_resources = res;
3939
3940 return res;
3941}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003942
Yinghai Luf3294a32008-06-27 01:41:56 -07003943void __init ioapic_init_mappings(void)
3944{
3945 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3946 int i;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003947 struct resource *ioapic_res;
Yinghai Luf3294a32008-06-27 01:41:56 -07003948
Ingo Molnar54168ed2008-08-20 09:07:45 +02003949 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003950 for (i = 0; i < nr_ioapics; i++) {
3951 if (smp_found_config) {
3952 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003953#ifdef CONFIG_X86_32
3954 if (!ioapic_phys) {
3955 printk(KERN_ERR
3956 "WARNING: bogus zero IO-APIC "
3957 "address found in MPTABLE, "
3958 "disabling IO/APIC support!\n");
3959 smp_found_config = 0;
3960 skip_ioapic_setup = 1;
3961 goto fake_ioapic_page;
3962 }
3963#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003964 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003965#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003966fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003967#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003968 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003969 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07003970 ioapic_phys = __pa(ioapic_phys);
3971 }
3972 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003973 apic_printk(APIC_VERBOSE,
3974 "mapped IOAPIC to %08lx (%08lx)\n",
3975 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07003976 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003977
Ingo Molnar54168ed2008-08-20 09:07:45 +02003978 if (ioapic_res != NULL) {
3979 ioapic_res->start = ioapic_phys;
3980 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3981 ioapic_res++;
3982 }
Yinghai Luf3294a32008-06-27 01:41:56 -07003983 }
3984}
3985
Ingo Molnar54168ed2008-08-20 09:07:45 +02003986static int __init ioapic_insert_resources(void)
3987{
3988 int i;
3989 struct resource *r = ioapic_resources;
3990
3991 if (!r) {
3992 printk(KERN_ERR
3993 "IO APIC resources could be not be allocated.\n");
3994 return -1;
3995 }
3996
3997 for (i = 0; i < nr_ioapics; i++) {
3998 insert_resource(&iomem_resource, r);
3999 r++;
4000 }
4001
4002 return 0;
4003}
4004
4005/* Insert the IO APIC resources after PCI initialization has occured to handle
4006 * IO APICS that are mapped in on a BAR in PCI space. */
4007late_initcall(ioapic_insert_resources);