blob: ed68a6f99dc27288924909b11987caec57ea24db [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 return cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700281}
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700282#else
Yinghai Lua1420f32008-08-19 20:50:24 -0700283
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700284#define for_each_irq_cfg(irq, cfg) \
285 for (irq = 0, cfg = &irq_cfgx[irq]; irq < nr_irqs; irq++, cfg = &irq_cfgx[irq])
286
287DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irqs, PAGE_SIZE, init_work);
288
289struct irq_cfg *irq_cfg(unsigned int irq)
290{
291 if (irq < nr_irqs)
292 return &irq_cfgx[irq];
293
294 return NULL;
295}
296struct irq_cfg *irq_cfg_alloc(unsigned int irq)
297{
298 return irq_cfg(irq);
299}
300
301#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
303 * This is performance-critical, we want to do it O(1)
304 *
305 * the indexing order of this array favors 1:1 mappings
306 * between pins and IRQs.
307 */
308
Yinghai Lu0f978f42008-08-19 20:50:26 -0700309struct irq_pin_list {
310 int apic, pin;
311 struct irq_pin_list *next;
312};
Yinghai Lu301e6192008-08-19 20:50:02 -0700313
Yinghai Lu0f978f42008-08-19 20:50:26 -0700314static struct irq_pin_list *irq_2_pin_head;
315/* fill one page ? */
316static int nr_irq_2_pin = 0x100;
317static struct irq_pin_list *irq_2_pin_ptr;
318static void __init irq_2_pin_init_work(void *data)
319{
320 struct dyn_array *da = data;
321 struct irq_pin_list *pin;
322 int i;
323
324 pin = *da->name;
325
326 for (i = 1; i < *da->nr; i++)
327 pin[i-1].next = &pin[i];
328
329 irq_2_pin_ptr = &pin[0];
330}
331DEFINE_DYN_ARRAY(irq_2_pin_head, sizeof(struct irq_pin_list), nr_irq_2_pin, PAGE_SIZE, irq_2_pin_init_work);
332
333static struct irq_pin_list *get_one_free_irq_2_pin(void)
334{
335 struct irq_pin_list *pin;
336 int i;
337
338 pin = irq_2_pin_ptr;
339
340 if (pin) {
341 irq_2_pin_ptr = pin->next;
342 pin->next = NULL;
343 return pin;
344 }
345
346 /*
347 * we run out of pre-allocate ones, allocate more
348 */
349 printk(KERN_DEBUG "try to get more irq_2_pin %d\n", nr_irq_2_pin);
350
351 if (after_bootmem)
352 pin = kzalloc(sizeof(struct irq_pin_list)*nr_irq_2_pin,
353 GFP_ATOMIC);
354 else
355 pin = __alloc_bootmem_nopanic(sizeof(struct irq_pin_list) *
356 nr_irq_2_pin, PAGE_SIZE, 0);
357
358 if (!pin)
359 panic("can not get more irq_2_pin\n");
360
361 for (i = 1; i < nr_irq_2_pin; i++)
362 pin[i-1].next = &pin[i];
363
364 irq_2_pin_ptr = pin->next;
365 pin->next = NULL;
366
367 return pin;
368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds130fe052006-11-01 09:11:00 -0800370struct io_apic {
371 unsigned int index;
372 unsigned int unused[3];
373 unsigned int data;
374};
375
376static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
377{
378 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400379 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800380}
381
382static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
383{
384 struct io_apic __iomem *io_apic = io_apic_base(apic);
385 writel(reg, &io_apic->index);
386 return readl(&io_apic->data);
387}
388
389static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
390{
391 struct io_apic __iomem *io_apic = io_apic_base(apic);
392 writel(reg, &io_apic->index);
393 writel(value, &io_apic->data);
394}
395
396/*
397 * Re-write a value: to be used for read-modify-write
398 * cycles where the read already set up the index register.
399 *
400 * Older SiS APIC requires we rewrite the index register
401 */
402static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
403{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200404 struct io_apic __iomem *io_apic = io_apic_base(apic);
405 if (sis_apic_bug)
406 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800407 writel(value, &io_apic->data);
408}
409
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700410static bool io_apic_level_ack_pending(unsigned int irq)
411{
412 struct irq_pin_list *entry;
413 unsigned long flags;
414 struct irq_cfg *cfg = irq_cfg(irq);
415
416 spin_lock_irqsave(&ioapic_lock, flags);
417 entry = cfg->irq_2_pin;
418 for (;;) {
419 unsigned int reg;
420 int pin;
421
422 if (!entry)
423 break;
424 pin = entry->pin;
425 reg = io_apic_read(entry->apic, 0x10 + pin*2);
426 /* Is the remote IRR bit set? */
427 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
428 spin_unlock_irqrestore(&ioapic_lock, flags);
429 return true;
430 }
431 if (!entry->next)
432 break;
433 entry = entry->next;
434 }
435 spin_unlock_irqrestore(&ioapic_lock, flags);
436
437 return false;
438}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700439
Andi Kleencf4c6a22006-09-26 10:52:30 +0200440union entry_union {
441 struct { u32 w1, w2; };
442 struct IO_APIC_route_entry entry;
443};
444
445static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
446{
447 union entry_union eu;
448 unsigned long flags;
449 spin_lock_irqsave(&ioapic_lock, flags);
450 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
451 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
452 spin_unlock_irqrestore(&ioapic_lock, flags);
453 return eu.entry;
454}
455
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800456/*
457 * When we write a new IO APIC routing entry, we need to write the high
458 * word first! If the mask bit in the low word is clear, we will enable
459 * the interrupt, and we need to make sure the entry is fully populated
460 * before that happens.
461 */
Andi Kleend15512f2006-12-07 02:14:07 +0100462static void
463__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
464{
465 union entry_union eu;
466 eu.entry = e;
467 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
468 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
469}
470
Andi Kleencf4c6a22006-09-26 10:52:30 +0200471static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
472{
473 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200474 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100475 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800476 spin_unlock_irqrestore(&ioapic_lock, flags);
477}
478
479/*
480 * When we mask an IO APIC routing entry, we need to write the low
481 * word first, in order to set the mask bit before we change the
482 * high bits!
483 */
484static void ioapic_mask_entry(int apic, int pin)
485{
486 unsigned long flags;
487 union entry_union eu = { .entry.mask = 1 };
488
489 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200490 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
491 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
492 spin_unlock_irqrestore(&ioapic_lock, flags);
493}
494
Yinghai Lu497c9a12008-08-19 20:50:28 -0700495#ifdef CONFIG_SMP
496static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
497{
498 int apic, pin;
499 struct irq_cfg *cfg;
500 struct irq_pin_list *entry;
501
502 cfg = irq_cfg(irq);
503 entry = cfg->irq_2_pin;
504 for (;;) {
505 unsigned int reg;
506
507 if (!entry)
508 break;
509
510 apic = entry->apic;
511 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200512#ifdef CONFIG_INTR_REMAP
513 /*
514 * With interrupt-remapping, destination information comes
515 * from interrupt-remapping table entry.
516 */
517 if (!irq_remapped(irq))
518 io_apic_write(apic, 0x11 + pin*2, dest);
519#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700520 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200521#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700522 reg = io_apic_read(apic, 0x10 + pin*2);
523 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
524 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200525 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700526 if (!entry->next)
527 break;
528 entry = entry->next;
529 }
530}
Yinghai Luefa25592008-08-19 20:50:36 -0700531
532static int assign_irq_vector(int irq, cpumask_t mask);
533
Yinghai Lu497c9a12008-08-19 20:50:28 -0700534static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
535{
536 struct irq_cfg *cfg;
537 unsigned long flags;
538 unsigned int dest;
539 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200540 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700541
Yinghai Lu497c9a12008-08-19 20:50:28 -0700542 cpus_and(tmp, mask, cpu_online_map);
543 if (cpus_empty(tmp))
544 return;
545
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700546 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700547 if (assign_irq_vector(irq, mask))
548 return;
549
550 cpus_and(tmp, cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700551 dest = cpu_mask_to_apicid(tmp);
552 /*
553 * Only the high 8 bits are valid.
554 */
555 dest = SET_APIC_LOGICAL_ID(dest);
556
Ingo Molnar54168ed2008-08-20 09:07:45 +0200557 desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700558 spin_lock_irqsave(&ioapic_lock, flags);
559 __target_IO_APIC_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200560 desc->affinity = mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700561 spin_unlock_irqrestore(&ioapic_lock, flags);
562}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700563#endif /* CONFIG_SMP */
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/*
566 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
567 * shared ISA-space IRQs, so we have to support them. We are super
568 * fast in the common case, and fast for shared ISA-space IRQs.
569 */
570static void add_pin_to_irq(unsigned int irq, int apic, int pin)
571{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700572 struct irq_cfg *cfg;
573 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Yinghai Lu0f978f42008-08-19 20:50:26 -0700575 /* first time to refer irq_cfg, so with new */
576 cfg = irq_cfg_alloc(irq);
577 entry = cfg->irq_2_pin;
578 if (!entry) {
579 entry = get_one_free_irq_2_pin();
580 cfg->irq_2_pin = entry;
581 entry->apic = apic;
582 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700583 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700585
586 while (entry->next) {
587 /* not again, please */
588 if (entry->apic == apic && entry->pin == pin)
589 return;
590
591 entry = entry->next;
592 }
593
594 entry->next = get_one_free_irq_2_pin();
595 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 entry->apic = apic;
597 entry->pin = pin;
598}
599
600/*
601 * Reroute an IRQ to a different pin.
602 */
603static void __init replace_pin_at_irq(unsigned int irq,
604 int oldapic, int oldpin,
605 int newapic, int newpin)
606{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700607 struct irq_cfg *cfg = irq_cfg(irq);
608 struct irq_pin_list *entry = cfg->irq_2_pin;
609 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Yinghai Lu0f978f42008-08-19 20:50:26 -0700611 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (entry->apic == oldapic && entry->pin == oldpin) {
613 entry->apic = newapic;
614 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700615 replaced = 1;
616 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700618 }
619 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700621
622 /* why? call replace before add? */
623 if (!replaced)
624 add_pin_to_irq(irq, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400627static inline void io_apic_modify_irq(unsigned int irq,
628 int mask_and, int mask_or,
629 void (*final)(struct irq_pin_list *entry))
630{
631 int pin;
632 struct irq_cfg *cfg;
633 struct irq_pin_list *entry;
634
635 cfg = irq_cfg(irq);
636 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
637 unsigned int reg;
638 pin = entry->pin;
639 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
640 reg &= mask_and;
641 reg |= mask_or;
642 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
643 if (final)
644 final(entry);
645 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700646}
647
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400648static void __unmask_IO_APIC_irq(unsigned int irq)
649{
650 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED, 0, NULL);
651}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700652
653#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400654void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700655{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400656 /*
657 * Synchronize the IO-APIC and the CPU by doing
658 * a dummy read from the IO-APIC
659 */
660 struct io_apic __iomem *io_apic;
661 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700662 readl(&io_apic->data);
663}
664
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400665static void __mask_IO_APIC_irq(unsigned int irq)
666{
667 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
668}
669#else /* CONFIG_X86_32 */
670static void __mask_IO_APIC_irq(unsigned int irq)
671{
672 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, NULL);
673}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700674
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400675static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
676{
677 io_apic_modify_irq(irq, ~IO_APIC_REDIR_LEVEL_TRIGGER,
678 IO_APIC_REDIR_MASKED, NULL);
679}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700680
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400681static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
682{
683 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED,
684 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
685}
686#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700687
Ingo Molnar54168ed2008-08-20 09:07:45 +0200688static void mask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 unsigned long flags;
691
692 spin_lock_irqsave(&ioapic_lock, flags);
693 __mask_IO_APIC_irq(irq);
694 spin_unlock_irqrestore(&ioapic_lock, flags);
695}
696
Ingo Molnar54168ed2008-08-20 09:07:45 +0200697static void unmask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 unsigned long flags;
700
701 spin_lock_irqsave(&ioapic_lock, flags);
702 __unmask_IO_APIC_irq(irq);
703 spin_unlock_irqrestore(&ioapic_lock, flags);
704}
705
706static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
707{
708 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200711 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (entry.delivery_mode == dest_SMI)
713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * Disable it in the IO-APIC irq-routing table:
716 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800717 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Ingo Molnar54168ed2008-08-20 09:07:45 +0200720static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721{
722 int apic, pin;
723
724 for (apic = 0; apic < nr_ioapics; apic++)
725 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
726 clear_IO_APIC_pin(apic, pin);
727}
728
Ingo Molnar54168ed2008-08-20 09:07:45 +0200729#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100730void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 unsigned int cfg;
733
734 /*
735 * Wait for idle.
736 */
737 apic_wait_icr_idle();
738 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
739 /*
740 * Send the IPI. The write to APIC_ICR fires this off.
741 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100742 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200744#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Ingo Molnar54168ed2008-08-20 09:07:45 +0200746#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747/*
748 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
749 * specific CPU-side IRQs.
750 */
751
752#define MAX_PIRQS 8
753static int pirq_entries [MAX_PIRQS];
754static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756static int __init ioapic_pirq_setup(char *str)
757{
758 int i, max;
759 int ints[MAX_PIRQS+1];
760
761 get_options(str, ARRAY_SIZE(ints), ints);
762
763 for (i = 0; i < MAX_PIRQS; i++)
764 pirq_entries[i] = -1;
765
766 pirqs_enabled = 1;
767 apic_printk(APIC_VERBOSE, KERN_INFO
768 "PIRQ redirection, working around broken MP-BIOS.\n");
769 max = MAX_PIRQS;
770 if (ints[0] < MAX_PIRQS)
771 max = ints[0];
772
773 for (i = 0; i < max; i++) {
774 apic_printk(APIC_VERBOSE, KERN_DEBUG
775 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
776 /*
777 * PIRQs are mapped upside down, usually.
778 */
779 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
780 }
781 return 1;
782}
783
784__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200785#endif /* CONFIG_X86_32 */
786
787#ifdef CONFIG_INTR_REMAP
788/* I/O APIC RTE contents at the OS boot up */
789static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
790
791/*
792 * Saves and masks all the unmasked IO-APIC RTE's
793 */
794int save_mask_IO_APIC_setup(void)
795{
796 union IO_APIC_reg_01 reg_01;
797 unsigned long flags;
798 int apic, pin;
799
800 /*
801 * The number of IO-APIC IRQ registers (== #pins):
802 */
803 for (apic = 0; apic < nr_ioapics; apic++) {
804 spin_lock_irqsave(&ioapic_lock, flags);
805 reg_01.raw = io_apic_read(apic, 1);
806 spin_unlock_irqrestore(&ioapic_lock, flags);
807 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
808 }
809
810 for (apic = 0; apic < nr_ioapics; apic++) {
811 early_ioapic_entries[apic] =
812 kzalloc(sizeof(struct IO_APIC_route_entry) *
813 nr_ioapic_registers[apic], GFP_KERNEL);
814 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400815 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200816 }
817
818 for (apic = 0; apic < nr_ioapics; apic++)
819 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
820 struct IO_APIC_route_entry entry;
821
822 entry = early_ioapic_entries[apic][pin] =
823 ioapic_read_entry(apic, pin);
824 if (!entry.mask) {
825 entry.mask = 1;
826 ioapic_write_entry(apic, pin, entry);
827 }
828 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400829
Ingo Molnar54168ed2008-08-20 09:07:45 +0200830 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400831
832nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400833 while (apic >= 0)
834 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400835 memset(early_ioapic_entries, 0,
836 ARRAY_SIZE(early_ioapic_entries));
837
838 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200839}
840
841void restore_IO_APIC_setup(void)
842{
843 int apic, pin;
844
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400845 for (apic = 0; apic < nr_ioapics; apic++) {
846 if (!early_ioapic_entries[apic])
847 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200848 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
849 ioapic_write_entry(apic, pin,
850 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400851 kfree(early_ioapic_entries[apic]);
852 early_ioapic_entries[apic] = NULL;
853 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200854}
855
856void reinit_intr_remapped_IO_APIC(int intr_remapping)
857{
858 /*
859 * for now plain restore of previous settings.
860 * TBD: In the case of OS enabling interrupt-remapping,
861 * IO-APIC RTE's need to be setup to point to interrupt-remapping
862 * table entries. for now, do a plain restore, and wait for
863 * the setup_IO_APIC_irqs() to do proper initialization.
864 */
865 restore_IO_APIC_setup();
866}
867#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869/*
870 * Find the IRQ entry number of a certain pin.
871 */
872static int find_irq_entry(int apic, int pin, int type)
873{
874 int i;
875
876 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400877 if (mp_irqs[i].mp_irqtype == type &&
878 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
879 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
880 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return i;
882
883 return -1;
884}
885
886/*
887 * Find the pin to which IRQ[irq] (ISA) is connected
888 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800889static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 int i;
892
893 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400894 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300896 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400897 (mp_irqs[i].mp_irqtype == type) &&
898 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400900 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 return -1;
903}
904
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800905static int __init find_isa_irq_apic(int irq, int type)
906{
907 int i;
908
909 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400910 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800911
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300912 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400913 (mp_irqs[i].mp_irqtype == type) &&
914 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800915 break;
916 }
917 if (i < mp_irq_entries) {
918 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200919 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400920 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800921 return apic;
922 }
923 }
924
925 return -1;
926}
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928/*
929 * Find a specific PCI IRQ entry.
930 * Not an __init, possibly needed by modules
931 */
932static int pin_2_irq(int idx, int apic, int pin);
933
934int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
935{
936 int apic, i, best_guess = -1;
937
Ingo Molnar54168ed2008-08-20 09:07:45 +0200938 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
939 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400940 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200941 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return -1;
943 }
944 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400945 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400948 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
949 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 break;
951
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300952 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400953 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400955 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200956 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (!(apic || IO_APIC_IRQ(irq)))
959 continue;
960
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400961 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 return irq;
963 /*
964 * Use the first all-but-pin matching entry as a
965 * best-guess fuzzy result for broken mptables.
966 */
967 if (best_guess < 0)
968 best_guess = irq;
969 }
970 }
971 return best_guess;
972}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200973
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700974EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300976#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/*
978 * EISA Edge/Level control register, ELCR
979 */
980static int EISA_ELCR(unsigned int irq)
981{
982 if (irq < 16) {
983 unsigned int port = 0x4d0 + (irq >> 3);
984 return (inb(port) >> (irq & 7)) & 1;
985 }
986 apic_printk(APIC_VERBOSE, KERN_INFO
987 "Broken MPtable reports ISA irq %d\n", irq);
988 return 0;
989}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200990
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300991#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300993/* ISA interrupts are always polarity zero edge triggered,
994 * when listed as conforming in the MP table. */
995
996#define default_ISA_trigger(idx) (0)
997#define default_ISA_polarity(idx) (0)
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999/* EISA interrupts are always polarity zero and can be edge or level
1000 * trigger depending on the ELCR value. If an interrupt is listed as
1001 * EISA conforming in the MP table, that means its trigger type must
1002 * be read in from the ELCR */
1003
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001004#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001005#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007/* PCI interrupts are always polarity one level triggered,
1008 * when listed as conforming in the MP table. */
1009
1010#define default_PCI_trigger(idx) (1)
1011#define default_PCI_polarity(idx) (1)
1012
1013/* MCA interrupts are always polarity zero level triggered,
1014 * when listed as conforming in the MP table. */
1015
1016#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001017#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Shaohua Li61fd47e2007-11-17 01:05:28 -05001019static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001021 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int polarity;
1023
1024 /*
1025 * Determine IRQ line polarity (high active or low active):
1026 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001027 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001029 case 0: /* conforms, ie. bus-type dependent polarity */
1030 if (test_bit(bus, mp_bus_not_pci))
1031 polarity = default_ISA_polarity(idx);
1032 else
1033 polarity = default_PCI_polarity(idx);
1034 break;
1035 case 1: /* high active */
1036 {
1037 polarity = 0;
1038 break;
1039 }
1040 case 2: /* reserved */
1041 {
1042 printk(KERN_WARNING "broken BIOS!!\n");
1043 polarity = 1;
1044 break;
1045 }
1046 case 3: /* low active */
1047 {
1048 polarity = 1;
1049 break;
1050 }
1051 default: /* invalid */
1052 {
1053 printk(KERN_WARNING "broken BIOS!!\n");
1054 polarity = 1;
1055 break;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
1058 return polarity;
1059}
1060
1061static int MPBIOS_trigger(int idx)
1062{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001063 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 int trigger;
1065
1066 /*
1067 * Determine IRQ trigger mode (edge or level sensitive):
1068 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001069 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001071 case 0: /* conforms, ie. bus-type dependent */
1072 if (test_bit(bus, mp_bus_not_pci))
1073 trigger = default_ISA_trigger(idx);
1074 else
1075 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001076#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001077 switch (mp_bus_id_to_type[bus]) {
1078 case MP_BUS_ISA: /* ISA pin */
1079 {
1080 /* set before the switch */
1081 break;
1082 }
1083 case MP_BUS_EISA: /* EISA pin */
1084 {
1085 trigger = default_EISA_trigger(idx);
1086 break;
1087 }
1088 case MP_BUS_PCI: /* PCI pin */
1089 {
1090 /* set before the switch */
1091 break;
1092 }
1093 case MP_BUS_MCA: /* MCA pin */
1094 {
1095 trigger = default_MCA_trigger(idx);
1096 break;
1097 }
1098 default:
1099 {
1100 printk(KERN_WARNING "broken BIOS!!\n");
1101 trigger = 1;
1102 break;
1103 }
1104 }
1105#endif
1106 break;
1107 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001108 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001109 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001110 break;
1111 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001112 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001113 {
1114 printk(KERN_WARNING "broken BIOS!!\n");
1115 trigger = 1;
1116 break;
1117 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001118 case 3: /* level */
1119 {
1120 trigger = 1;
1121 break;
1122 }
1123 default: /* invalid */
1124 {
1125 printk(KERN_WARNING "broken BIOS!!\n");
1126 trigger = 0;
1127 break;
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130 return trigger;
1131}
1132
1133static inline int irq_polarity(int idx)
1134{
1135 return MPBIOS_polarity(idx);
1136}
1137
1138static inline int irq_trigger(int idx)
1139{
1140 return MPBIOS_trigger(idx);
1141}
1142
Yinghai Luefa25592008-08-19 20:50:36 -07001143int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static int pin_2_irq(int idx, int apic, int pin)
1145{
1146 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001147 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /*
1150 * Debugging check, we are in big trouble if this message pops up!
1151 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001152 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1154
Ingo Molnar54168ed2008-08-20 09:07:45 +02001155 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001156 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001157 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001158 /*
1159 * PCI IRQs are mapped in order
1160 */
1161 i = irq = 0;
1162 while (i < apic)
1163 irq += nr_ioapic_registers[i++];
1164 irq += pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001165 /*
1166 * For MPS mode, so far only needed by ES7000 platform
1167 */
1168 if (ioapic_renumber_irq)
1169 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171
Ingo Molnar54168ed2008-08-20 09:07:45 +02001172#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /*
1174 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1175 */
1176 if ((pin >= 16) && (pin <= 23)) {
1177 if (pirq_entries[pin-16] != -1) {
1178 if (!pirq_entries[pin-16]) {
1179 apic_printk(APIC_VERBOSE, KERN_DEBUG
1180 "disabling PIRQ%d\n", pin-16);
1181 } else {
1182 irq = pirq_entries[pin-16];
1183 apic_printk(APIC_VERBOSE, KERN_DEBUG
1184 "using PIRQ%d -> IRQ %d\n",
1185 pin-16, irq);
1186 }
1187 }
1188 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001189#endif
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return irq;
1192}
1193
Yinghai Lu497c9a12008-08-19 20:50:28 -07001194void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001196 /* Used to the online set of cpus does not change
1197 * during assign_irq_vector.
1198 */
1199 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Yinghai Lu497c9a12008-08-19 20:50:28 -07001202void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001203{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001204 spin_unlock(&vector_lock);
1205}
1206
1207static int __assign_irq_vector(int irq, cpumask_t mask)
1208{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001209 /*
1210 * NOTE! The local APIC isn't very good at handling
1211 * multiple interrupts at the same interrupt level.
1212 * As the interrupt level is determined by taking the
1213 * vector number and shifting that right by 4, we
1214 * want to spread these out a bit so that they don't
1215 * all fall in the same interrupt level.
1216 *
1217 * Also, we've got to be careful not to trash gate
1218 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1219 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001220 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1221 unsigned int old_vector;
1222 int cpu;
1223 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001224
Ingo Molnar54168ed2008-08-20 09:07:45 +02001225 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001226
Ingo Molnar54168ed2008-08-20 09:07:45 +02001227 /* Only try and allocate irqs on cpus that are present */
1228 cpus_and(mask, mask, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001229
Ingo Molnar54168ed2008-08-20 09:07:45 +02001230 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1231 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001232
Ingo Molnar54168ed2008-08-20 09:07:45 +02001233 old_vector = cfg->vector;
1234 if (old_vector) {
1235 cpumask_t tmp;
1236 cpus_and(tmp, cfg->domain, mask);
1237 if (!cpus_empty(tmp))
1238 return 0;
1239 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001240
Ingo Molnar54168ed2008-08-20 09:07:45 +02001241 for_each_cpu_mask_nr(cpu, mask) {
1242 cpumask_t domain, new_mask;
1243 int new_cpu;
1244 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001245
Ingo Molnar54168ed2008-08-20 09:07:45 +02001246 domain = vector_allocation_domain(cpu);
1247 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001248
Ingo Molnar54168ed2008-08-20 09:07:45 +02001249 vector = current_vector;
1250 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001251next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001252 vector += 8;
1253 if (vector >= first_system_vector) {
1254 /* If we run out of vectors on large boxen, must share them. */
1255 offset = (offset + 1) % 8;
1256 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001257 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001258 if (unlikely(current_vector == vector))
1259 continue;
1260#ifdef CONFIG_X86_64
1261 if (vector == IA32_SYSCALL_VECTOR)
1262 goto next;
1263#else
1264 if (vector == SYSCALL_VECTOR)
1265 goto next;
1266#endif
1267 for_each_cpu_mask_nr(new_cpu, new_mask)
1268 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1269 goto next;
1270 /* Found one! */
1271 current_vector = vector;
1272 current_offset = offset;
1273 if (old_vector) {
1274 cfg->move_in_progress = 1;
1275 cfg->old_domain = cfg->domain;
1276 }
1277 for_each_cpu_mask_nr(new_cpu, new_mask)
1278 per_cpu(vector_irq, new_cpu)[vector] = irq;
1279 cfg->vector = vector;
1280 cfg->domain = domain;
1281 return 0;
1282 }
1283 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001284}
1285
1286static int assign_irq_vector(int irq, cpumask_t mask)
1287{
1288 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001289 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001290
1291 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001292 err = __assign_irq_vector(irq, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001293 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001294 return err;
1295}
1296
1297static void __clear_irq_vector(int irq)
1298{
1299 struct irq_cfg *cfg;
1300 cpumask_t mask;
1301 int cpu, vector;
1302
1303 cfg = irq_cfg(irq);
1304 BUG_ON(!cfg->vector);
1305
1306 vector = cfg->vector;
1307 cpus_and(mask, cfg->domain, cpu_online_map);
1308 for_each_cpu_mask_nr(cpu, mask)
1309 per_cpu(vector_irq, cpu)[vector] = -1;
1310
1311 cfg->vector = 0;
1312 cpus_clear(cfg->domain);
1313}
1314
1315void __setup_vector_irq(int cpu)
1316{
1317 /* Initialize vector_irq on a new cpu */
1318 /* This function must be called with vector_lock held */
1319 int irq, vector;
1320 struct irq_cfg *cfg;
1321
1322 /* Mark the inuse vectors */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001323 for_each_irq_cfg(irq, cfg) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001324 if (!cpu_isset(cpu, cfg->domain))
1325 continue;
1326 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001327 per_cpu(vector_irq, cpu)[vector] = irq;
1328 }
1329 /* Mark the free vectors */
1330 for (vector = 0; vector < NR_VECTORS; ++vector) {
1331 irq = per_cpu(vector_irq, cpu)[vector];
1332 if (irq < 0)
1333 continue;
1334
1335 cfg = irq_cfg(irq);
1336 if (!cpu_isset(cpu, cfg->domain))
1337 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001338 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001339}
Glauber Costa3fde6902008-05-28 20:34:19 -07001340
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001341static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001342#ifdef CONFIG_INTR_REMAP
1343static struct irq_chip ir_ioapic_chip;
1344#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Ingo Molnar54168ed2008-08-20 09:07:45 +02001346#define IOAPIC_AUTO -1
1347#define IOAPIC_EDGE 0
1348#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001350#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001351static inline int IO_APIC_irq_trigger(int irq)
1352{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001353 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001354
Ingo Molnar54168ed2008-08-20 09:07:45 +02001355 for (apic = 0; apic < nr_ioapics; apic++) {
1356 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1357 idx = find_irq_entry(apic, pin, mp_INT);
1358 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1359 return irq_trigger(idx);
1360 }
1361 }
1362 /*
1363 * nonexistent IRQs are edge default
1364 */
1365 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001366}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001367#else
1368static inline int IO_APIC_irq_trigger(int irq)
1369{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001370 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001371}
1372#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001373
Yinghai Lu497c9a12008-08-19 20:50:28 -07001374static void ioapic_register_intr(int irq, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Yinghai Lu08678b02008-08-19 20:50:05 -07001376 struct irq_desc *desc;
1377
Yinghai Lu199751d2008-08-19 20:50:27 -07001378 /* first time to use this irq_desc */
1379 if (irq < 16)
1380 desc = irq_to_desc(irq);
1381 else
1382 desc = irq_to_desc_alloc(irq);
1383
Jan Beulich6ebcc002006-06-26 13:56:46 +02001384 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001385 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001386 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001387 else
1388 desc->status &= ~IRQ_LEVEL;
1389
Ingo Molnar54168ed2008-08-20 09:07:45 +02001390#ifdef CONFIG_INTR_REMAP
1391 if (irq_remapped(irq)) {
1392 desc->status |= IRQ_MOVE_PCNTXT;
1393 if (trigger)
1394 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1395 handle_fasteoi_irq,
1396 "fasteoi");
1397 else
1398 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1399 handle_edge_irq, "edge");
1400 return;
1401 }
1402#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001403 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1404 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001405 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001406 handle_fasteoi_irq,
1407 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001408 else
Ingo Molnara460e742006-10-17 00:10:03 -07001409 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001410 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001411}
1412
1413static int setup_ioapic_entry(int apic, int irq,
1414 struct IO_APIC_route_entry *entry,
1415 unsigned int destination, int trigger,
1416 int polarity, int vector)
1417{
1418 /*
1419 * add it to the IO-APIC irq-routing table:
1420 */
1421 memset(entry,0,sizeof(*entry));
1422
Ingo Molnar54168ed2008-08-20 09:07:45 +02001423#ifdef CONFIG_INTR_REMAP
1424 if (intr_remapping_enabled) {
1425 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1426 struct irte irte;
1427 struct IR_IO_APIC_route_entry *ir_entry =
1428 (struct IR_IO_APIC_route_entry *) entry;
1429 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001430
Ingo Molnar54168ed2008-08-20 09:07:45 +02001431 if (!iommu)
1432 panic("No mapping iommu for ioapic %d\n", apic);
1433
1434 index = alloc_irte(iommu, irq, 1);
1435 if (index < 0)
1436 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1437
1438 memset(&irte, 0, sizeof(irte));
1439
1440 irte.present = 1;
1441 irte.dst_mode = INT_DEST_MODE;
1442 irte.trigger_mode = trigger;
1443 irte.dlvry_mode = INT_DELIVERY_MODE;
1444 irte.vector = vector;
1445 irte.dest_id = IRTE_DEST(destination);
1446
1447 modify_irte(irq, &irte);
1448
1449 ir_entry->index2 = (index >> 15) & 0x1;
1450 ir_entry->zero = 0;
1451 ir_entry->format = 1;
1452 ir_entry->index = (index & 0x7fff);
1453 } else
1454#endif
1455 {
1456 entry->delivery_mode = INT_DELIVERY_MODE;
1457 entry->dest_mode = INT_DEST_MODE;
1458 entry->dest = destination;
1459 }
1460
1461 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001462 entry->trigger = trigger;
1463 entry->polarity = polarity;
1464 entry->vector = vector;
1465
1466 /* Mask level triggered irqs.
1467 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1468 */
1469 if (trigger)
1470 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001471 return 0;
1472}
1473
1474static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001475 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001476{
1477 struct irq_cfg *cfg;
1478 struct IO_APIC_route_entry entry;
1479 cpumask_t mask;
1480
1481 if (!IO_APIC_IRQ(irq))
1482 return;
1483
1484 cfg = irq_cfg(irq);
1485
1486 mask = TARGET_CPUS;
1487 if (assign_irq_vector(irq, mask))
1488 return;
1489
1490 cpus_and(mask, cfg->domain, mask);
1491
1492 apic_printk(APIC_VERBOSE,KERN_DEBUG
1493 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1494 "IRQ %d Mode:%i Active:%i)\n",
1495 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1496 irq, trigger, polarity);
1497
1498
1499 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1500 cpu_mask_to_apicid(mask), trigger, polarity,
1501 cfg->vector)) {
1502 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1503 mp_ioapics[apic].mp_apicid, pin);
1504 __clear_irq_vector(irq);
1505 return;
1506 }
1507
1508 ioapic_register_intr(irq, trigger);
1509 if (irq < 16)
1510 disable_8259A_irq(irq);
1511
1512 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
1515static void __init setup_IO_APIC_irqs(void)
1516{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001517 int apic, pin, idx, irq;
1518 int notcon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1521
1522 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001523 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001525 idx = find_irq_entry(apic, pin, mp_INT);
1526 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001527 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001528 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001529 apic_printk(APIC_VERBOSE,
1530 KERN_DEBUG " %d-%d",
1531 mp_ioapics[apic].mp_apicid,
1532 pin);
1533 } else
1534 apic_printk(APIC_VERBOSE, " %d-%d",
1535 mp_ioapics[apic].mp_apicid,
1536 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001537 continue;
1538 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001539 if (notcon) {
1540 apic_printk(APIC_VERBOSE,
1541 " (apicid-pin) not connected\n");
1542 notcon = 0;
1543 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001544
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001545 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001546#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001547 if (multi_timer_check(apic, irq))
1548 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001549#endif
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001550 add_pin_to_irq(irq, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001551
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001552 setup_IO_APIC_irq(apic, pin, irq,
1553 irq_trigger(idx), irq_polarity(idx));
1554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001557 if (notcon)
1558 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001559 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
1562/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001563 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001565static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1566 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Ingo Molnar54168ed2008-08-20 09:07:45 +02001570#ifdef CONFIG_INTR_REMAP
1571 if (intr_remapping_enabled)
1572 return;
1573#endif
1574
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001575 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 /*
1578 * We use logical delivery to get the timer IRQ
1579 * to the first CPU.
1580 */
1581 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001582 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001583 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 entry.delivery_mode = INT_DELIVERY_MODE;
1585 entry.polarity = 0;
1586 entry.trigger = 0;
1587 entry.vector = vector;
1588
1589 /*
1590 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001591 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001593 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 /*
1596 * Add it to the IO-APIC irq-routing table:
1597 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001598 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001601
1602__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 int apic, i;
1605 union IO_APIC_reg_00 reg_00;
1606 union IO_APIC_reg_01 reg_01;
1607 union IO_APIC_reg_02 reg_02;
1608 union IO_APIC_reg_03 reg_03;
1609 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001610 struct irq_cfg *cfg;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001611 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 if (apic_verbosity == APIC_QUIET)
1614 return;
1615
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001616 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 for (i = 0; i < nr_ioapics; i++)
1618 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001619 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 /*
1622 * We are a bit conservative about what we expect. We have to
1623 * know about every hardware change ASAP.
1624 */
1625 printk(KERN_INFO "testing the IO APIC.......................\n");
1626
1627 for (apic = 0; apic < nr_ioapics; apic++) {
1628
1629 spin_lock_irqsave(&ioapic_lock, flags);
1630 reg_00.raw = io_apic_read(apic, 0);
1631 reg_01.raw = io_apic_read(apic, 1);
1632 if (reg_01.bits.version >= 0x10)
1633 reg_02.raw = io_apic_read(apic, 2);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001634 if (reg_01.bits.version >= 0x20)
1635 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 spin_unlock_irqrestore(&ioapic_lock, flags);
1637
Ingo Molnar54168ed2008-08-20 09:07:45 +02001638 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001639 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1641 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1642 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1643 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Ingo Molnar54168ed2008-08-20 09:07:45 +02001645 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
1648 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1649 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 /*
1652 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1653 * but the value of reg_02 is read as the previous read register
1654 * value, so ignore it if reg_02 == reg_01.
1655 */
1656 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1657 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1658 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
1660
1661 /*
1662 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1663 * or reg_03, but the value of reg_0[23] is read as the previous read
1664 * register value, so ignore it if reg_03 == reg_0[12].
1665 */
1666 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1667 reg_03.raw != reg_01.raw) {
1668 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1669 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671
1672 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1673
Yinghai Lud83e94a2008-08-19 20:50:33 -07001674 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1675 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 for (i = 0; i <= reg_01.bits.entries; i++) {
1678 struct IO_APIC_route_entry entry;
1679
Andi Kleencf4c6a22006-09-26 10:52:30 +02001680 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Ingo Molnar54168ed2008-08-20 09:07:45 +02001682 printk(KERN_DEBUG " %02x %03X ",
1683 i,
1684 entry.dest
1685 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1688 entry.mask,
1689 entry.trigger,
1690 entry.irr,
1691 entry.polarity,
1692 entry.delivery_status,
1693 entry.dest_mode,
1694 entry.delivery_mode,
1695 entry.vector
1696 );
1697 }
1698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001700 for_each_irq_cfg(irq, cfg) {
Yinghai Lu0f978f42008-08-19 20:50:26 -07001701 struct irq_pin_list *entry = cfg->irq_2_pin;
1702 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001704 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 for (;;) {
1706 printk("-> %d:%d", entry->apic, entry->pin);
1707 if (!entry->next)
1708 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001709 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711 printk("\n");
1712 }
1713
1714 printk(KERN_INFO ".................................... done.\n");
1715
1716 return;
1717}
1718
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001719__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 unsigned int v;
1722 int i, j;
1723
1724 if (apic_verbosity == APIC_QUIET)
1725 return;
1726
1727 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1728 for (i = 0; i < 8; i++) {
1729 v = apic_read(base + i*0x10);
1730 for (j = 0; j < 32; j++) {
1731 if (v & (1<<j))
1732 printk("1");
1733 else
1734 printk("0");
1735 }
1736 printk("\n");
1737 }
1738}
1739
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001740__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
1742 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001743 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 if (apic_verbosity == APIC_QUIET)
1746 return;
1747
1748 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1749 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001750 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001751 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 v = apic_read(APIC_LVR);
1753 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1754 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001755 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 v = apic_read(APIC_TASKPRI);
1758 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1759
Ingo Molnar54168ed2008-08-20 09:07:45 +02001760 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001761 if (!APIC_XAPIC(ver)) {
1762 v = apic_read(APIC_ARBPRI);
1763 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1764 v & APIC_ARBPRI_MASK);
1765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 v = apic_read(APIC_PROCPRI);
1767 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1768 }
1769
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001770 /*
1771 * Remote read supported only in the 82489DX and local APIC for
1772 * Pentium processors.
1773 */
1774 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1775 v = apic_read(APIC_RRR);
1776 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1777 }
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 v = apic_read(APIC_LDR);
1780 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001781 if (!x2apic_enabled()) {
1782 v = apic_read(APIC_DFR);
1783 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 v = apic_read(APIC_SPIV);
1786 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1787
1788 printk(KERN_DEBUG "... APIC ISR field:\n");
1789 print_APIC_bitfield(APIC_ISR);
1790 printk(KERN_DEBUG "... APIC TMR field:\n");
1791 print_APIC_bitfield(APIC_TMR);
1792 printk(KERN_DEBUG "... APIC IRR field:\n");
1793 print_APIC_bitfield(APIC_IRR);
1794
Ingo Molnar54168ed2008-08-20 09:07:45 +02001795 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1796 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001798
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 v = apic_read(APIC_ESR);
1800 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1801 }
1802
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001803 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001804 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1805 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 v = apic_read(APIC_LVTT);
1808 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1809
1810 if (maxlvt > 3) { /* PC is LVT#4. */
1811 v = apic_read(APIC_LVTPC);
1812 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1813 }
1814 v = apic_read(APIC_LVT0);
1815 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1816 v = apic_read(APIC_LVT1);
1817 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1818
1819 if (maxlvt > 2) { /* ERR is LVT#3. */
1820 v = apic_read(APIC_LVTERR);
1821 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1822 }
1823
1824 v = apic_read(APIC_TMICT);
1825 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1826 v = apic_read(APIC_TMCCT);
1827 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1828 v = apic_read(APIC_TDCR);
1829 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1830 printk("\n");
1831}
1832
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001833__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001835 int cpu;
1836
1837 preempt_disable();
1838 for_each_online_cpu(cpu)
1839 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1840 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001843__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 unsigned int v;
1846 unsigned long flags;
1847
1848 if (apic_verbosity == APIC_QUIET)
1849 return;
1850
1851 printk(KERN_DEBUG "\nprinting PIC contents\n");
1852
1853 spin_lock_irqsave(&i8259A_lock, flags);
1854
1855 v = inb(0xa1) << 8 | inb(0x21);
1856 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1857
1858 v = inb(0xa0) << 8 | inb(0x20);
1859 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1860
Ingo Molnar54168ed2008-08-20 09:07:45 +02001861 outb(0x0b,0xa0);
1862 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001864 outb(0x0a,0xa0);
1865 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 spin_unlock_irqrestore(&i8259A_lock, flags);
1868
1869 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1870
1871 v = inb(0x4d1) << 8 | inb(0x4d0);
1872 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1873}
1874
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001875__apicdebuginit(int) print_all_ICs(void)
1876{
1877 print_PIC();
1878 print_all_local_APICs();
1879 print_IO_APIC();
1880
1881 return 0;
1882}
1883
1884fs_initcall(print_all_ICs);
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Yinghai Luefa25592008-08-19 20:50:36 -07001887/* Where if anywhere is the i8259 connect in external int mode */
1888static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1889
Ingo Molnar54168ed2008-08-20 09:07:45 +02001890void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
1892 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001893 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001894 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 unsigned long flags;
1896
Ingo Molnar54168ed2008-08-20 09:07:45 +02001897#ifdef CONFIG_X86_32
1898 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (!pirqs_enabled)
1900 for (i = 0; i < MAX_PIRQS; i++)
1901 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /*
1905 * The number of IO-APIC IRQ registers (== #pins):
1906 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001907 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001909 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001911 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1912 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001913 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001914 int pin;
1915 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001916 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001917 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001918 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001919
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001920 /* If the interrupt line is enabled and in ExtInt mode
1921 * I have found the pin where the i8259 is connected.
1922 */
1923 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1924 ioapic_i8259.apic = apic;
1925 ioapic_i8259.pin = pin;
1926 goto found_i8259;
1927 }
1928 }
1929 }
1930 found_i8259:
1931 /* Look to see what if the MP table has reported the ExtINT */
1932 /* If we could not find the appropriate pin by looking at the ioapic
1933 * the i8259 probably is not connected the ioapic but give the
1934 * mptable a chance anyway.
1935 */
1936 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1937 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1938 /* Trust the MP table if nothing is setup in the hardware */
1939 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1940 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1941 ioapic_i8259.pin = i8259_pin;
1942 ioapic_i8259.apic = i8259_apic;
1943 }
1944 /* Complain if the MP table and the hardware disagree */
1945 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1946 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1947 {
1948 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950
1951 /*
1952 * Do not trust the IO-APIC being empty at bootup
1953 */
1954 clear_IO_APIC();
1955}
1956
1957/*
1958 * Not an __init, needed by the reboot code
1959 */
1960void disable_IO_APIC(void)
1961{
1962 /*
1963 * Clear the IO-APIC before rebooting:
1964 */
1965 clear_IO_APIC();
1966
Eric W. Biederman650927e2005-06-25 14:57:44 -07001967 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001968 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001969 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001970 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001971 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001972 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001973 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001974
1975 memset(&entry, 0, sizeof(entry));
1976 entry.mask = 0; /* Enabled */
1977 entry.trigger = 0; /* Edge */
1978 entry.irr = 0;
1979 entry.polarity = 0; /* High */
1980 entry.delivery_status = 0;
1981 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001982 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001983 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001984 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001985
1986 /*
1987 * Add it to the IO-APIC irq-routing table:
1988 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001989 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001990 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001991
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001992 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Ingo Molnar54168ed2008-08-20 09:07:45 +02001995#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996/*
1997 * function to set the IO-APIC physical IDs based on the
1998 * values stored in the MPC table.
1999 *
2000 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2001 */
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003static void __init setup_ioapic_ids_from_mpc(void)
2004{
2005 union IO_APIC_reg_00 reg_00;
2006 physid_mask_t phys_id_present_map;
2007 int apic;
2008 int i;
2009 unsigned char old_id;
2010 unsigned long flags;
2011
Yinghai Lua4dbc342008-07-25 02:14:28 -07002012 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07002013 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07002014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002016 * Don't check I/O APIC IDs for xAPIC systems. They have
2017 * no meaning without the serial APIC bus.
2018 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002019 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2020 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002021 return;
2022 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 * This is broken; anything with a real cpu count has to
2024 * circumvent this idiocy regardless.
2025 */
2026 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2027
2028 /*
2029 * Set the IOAPIC ID to the value stored in the MPC table.
2030 */
2031 for (apic = 0; apic < nr_ioapics; apic++) {
2032
2033 /* Read the register 0 value */
2034 spin_lock_irqsave(&ioapic_lock, flags);
2035 reg_00.raw = io_apic_read(apic, 0);
2036 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002037
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002038 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002040 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002042 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2044 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002045 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 /*
2049 * Sanity check, is the ID really free? Every APIC in a
2050 * system must have a unique ID or we get lots of nice
2051 * 'stuck on smp_invalidate_needed IPI wait' messages.
2052 */
2053 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002054 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002056 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 for (i = 0; i < get_physical_broadcast(); i++)
2058 if (!physid_isset(i, phys_id_present_map))
2059 break;
2060 if (i >= get_physical_broadcast())
2061 panic("Max APIC ID exceeded!\n");
2062 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2063 i);
2064 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002065 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 } else {
2067 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002068 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 apic_printk(APIC_VERBOSE, "Setting %d in the "
2070 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002071 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2073 }
2074
2075
2076 /*
2077 * We need to adjust the IRQ routing table
2078 * if the ID changed.
2079 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002080 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002082 if (mp_irqs[i].mp_dstapic == old_id)
2083 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002084 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 /*
2087 * Read the right value from the MPC table and
2088 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002089 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 apic_printk(APIC_VERBOSE, KERN_INFO
2091 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002092 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002094 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002096 io_apic_write(apic, 0, reg_00.raw);
2097 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099 /*
2100 * Sanity check
2101 */
2102 spin_lock_irqsave(&ioapic_lock, flags);
2103 reg_00.raw = io_apic_read(apic, 0);
2104 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002105 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 printk("could not set ID!\n");
2107 else
2108 apic_printk(APIC_VERBOSE, " ok.\n");
2109 }
2110}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002111#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002113int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002114
2115static int __init notimercheck(char *s)
2116{
2117 no_timer_check = 1;
2118 return 1;
2119}
2120__setup("no_timer_check", notimercheck);
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122/*
2123 * There is a nasty bug in some older SMP boards, their mptable lies
2124 * about the timer IRQ. We do the following to work around the situation:
2125 *
2126 * - timer IRQ defaults to IO-APIC IRQ
2127 * - if this function detects that timer IRQs are defunct, then we fall
2128 * back to ISA timer IRQs
2129 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002130static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
2132 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002133 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Zachary Amsden8542b202006-12-07 02:14:09 +01002135 if (no_timer_check)
2136 return 1;
2137
Ingo Molnar4aae0702007-12-18 18:05:58 +01002138 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 local_irq_enable();
2140 /* Let ten ticks pass... */
2141 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002142 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 /*
2145 * Expect a few ticks at least, to be sure some possible
2146 * glue logic does not lock up after one or two first
2147 * ticks in a non-ExtINT mode. Also the local APIC
2148 * might have cached one ExtINT interrupt. Finally, at
2149 * least one tick may be lost due to delays.
2150 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002151
2152 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002153 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return 0;
2156}
2157
2158/*
2159 * In the SMP+IOAPIC case it might happen that there are an unspecified
2160 * number of pending IRQ events unhandled. These cases are very rare,
2161 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2162 * better to do it this way as thus we do not have to be aware of
2163 * 'pending' interrupts in the IRQ path, except at this point.
2164 */
2165/*
2166 * Edge triggered needs to resend any interrupt
2167 * that was delayed but this is now handled in the device
2168 * independent code.
2169 */
2170
2171/*
2172 * Starting up a edge-triggered IO-APIC interrupt is
2173 * nasty - we need to make sure that we get the edge.
2174 * If it is already asserted for some reason, we need
2175 * return 1 to indicate that is was pending.
2176 *
2177 * This is not complete - we should be able to fake
2178 * an edge even if it isn't on the 8259A...
2179 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002180
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002181static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 int was_pending = 0;
2184 unsigned long flags;
2185
2186 spin_lock_irqsave(&ioapic_lock, flags);
2187 if (irq < 16) {
2188 disable_8259A_irq(irq);
2189 if (i8259A_irq_pending(irq))
2190 was_pending = 1;
2191 }
2192 __unmask_IO_APIC_irq(irq);
2193 spin_unlock_irqrestore(&ioapic_lock, flags);
2194
2195 return was_pending;
2196}
2197
Ingo Molnar54168ed2008-08-20 09:07:45 +02002198#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002199static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002201
2202 struct irq_cfg *cfg = irq_cfg(irq);
2203 unsigned long flags;
2204
2205 spin_lock_irqsave(&vector_lock, flags);
2206 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2207 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002208
2209 return 1;
2210}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002211#else
2212static int ioapic_retrigger_irq(unsigned int irq)
2213{
2214 send_IPI_self(irq_cfg(irq)->vector);
2215
2216 return 1;
2217}
2218#endif
2219
2220/*
2221 * Level and edge triggered IO-APIC interrupts need different handling,
2222 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2223 * handled with the level-triggered descriptor, but that one has slightly
2224 * more overhead. Level-triggered interrupts cannot be handled with the
2225 * edge-triggered handler, without risking IRQ storms and other ugly
2226 * races.
2227 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002228
Yinghai Lu497c9a12008-08-19 20:50:28 -07002229#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002230
2231#ifdef CONFIG_INTR_REMAP
2232static void ir_irq_migration(struct work_struct *work);
2233
2234static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2235
2236/*
2237 * Migrate the IO-APIC irq in the presence of intr-remapping.
2238 *
2239 * For edge triggered, irq migration is a simple atomic update(of vector
2240 * and cpu destination) of IRTE and flush the hardware cache.
2241 *
2242 * For level triggered, we need to modify the io-apic RTE aswell with the update
2243 * vector information, along with modifying IRTE with vector and destination.
2244 * So irq migration for level triggered is little bit more complex compared to
2245 * edge triggered migration. But the good news is, we use the same algorithm
2246 * for level triggered migration as we have today, only difference being,
2247 * we now initiate the irq migration from process context instead of the
2248 * interrupt context.
2249 *
2250 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2251 * suppression) to the IO-APIC, level triggered irq migration will also be
2252 * as simple as edge triggered migration and we can do the irq migration
2253 * with a simple atomic update to IO-APIC RTE.
2254 */
2255static void migrate_ioapic_irq(int irq, cpumask_t mask)
2256{
2257 struct irq_cfg *cfg;
2258 struct irq_desc *desc;
2259 cpumask_t tmp, cleanup_mask;
2260 struct irte irte;
2261 int modify_ioapic_rte;
2262 unsigned int dest;
2263 unsigned long flags;
2264
2265 cpus_and(tmp, mask, cpu_online_map);
2266 if (cpus_empty(tmp))
2267 return;
2268
2269 if (get_irte(irq, &irte))
2270 return;
2271
2272 if (assign_irq_vector(irq, mask))
2273 return;
2274
2275 cfg = irq_cfg(irq);
2276 cpus_and(tmp, cfg->domain, mask);
2277 dest = cpu_mask_to_apicid(tmp);
2278
2279 desc = irq_to_desc(irq);
2280 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2281 if (modify_ioapic_rte) {
2282 spin_lock_irqsave(&ioapic_lock, flags);
2283 __target_IO_APIC_irq(irq, dest, cfg->vector);
2284 spin_unlock_irqrestore(&ioapic_lock, flags);
2285 }
2286
2287 irte.vector = cfg->vector;
2288 irte.dest_id = IRTE_DEST(dest);
2289
2290 /*
2291 * Modified the IRTE and flushes the Interrupt entry cache.
2292 */
2293 modify_irte(irq, &irte);
2294
2295 if (cfg->move_in_progress) {
2296 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2297 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2298 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2299 cfg->move_in_progress = 0;
2300 }
2301
2302 desc->affinity = mask;
2303}
2304
2305static int migrate_irq_remapped_level(int irq)
2306{
2307 int ret = -1;
2308 struct irq_desc *desc = irq_to_desc(irq);
2309
2310 mask_IO_APIC_irq(irq);
2311
2312 if (io_apic_level_ack_pending(irq)) {
2313 /*
2314 * Interrupt in progress. Migrating irq now will change the
2315 * vector information in the IO-APIC RTE and that will confuse
2316 * the EOI broadcast performed by cpu.
2317 * So, delay the irq migration to the next instance.
2318 */
2319 schedule_delayed_work(&ir_migration_work, 1);
2320 goto unmask;
2321 }
2322
2323 /* everthing is clear. we have right of way */
2324 migrate_ioapic_irq(irq, desc->pending_mask);
2325
2326 ret = 0;
2327 desc->status &= ~IRQ_MOVE_PENDING;
2328 cpus_clear(desc->pending_mask);
2329
2330unmask:
2331 unmask_IO_APIC_irq(irq);
2332 return ret;
2333}
2334
2335static void ir_irq_migration(struct work_struct *work)
2336{
2337 unsigned int irq;
2338 struct irq_desc *desc;
2339
2340 for_each_irq_desc(irq, desc) {
2341 if (desc->status & IRQ_MOVE_PENDING) {
2342 unsigned long flags;
2343
2344 spin_lock_irqsave(&desc->lock, flags);
2345 if (!desc->chip->set_affinity ||
2346 !(desc->status & IRQ_MOVE_PENDING)) {
2347 desc->status &= ~IRQ_MOVE_PENDING;
2348 spin_unlock_irqrestore(&desc->lock, flags);
2349 continue;
2350 }
2351
2352 desc->chip->set_affinity(irq, desc->pending_mask);
2353 spin_unlock_irqrestore(&desc->lock, flags);
2354 }
2355 }
2356}
2357
2358/*
2359 * Migrates the IRQ destination in the process context.
2360 */
2361static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2362{
2363 struct irq_desc *desc = irq_to_desc(irq);
2364
2365 if (desc->status & IRQ_LEVEL) {
2366 desc->status |= IRQ_MOVE_PENDING;
2367 desc->pending_mask = mask;
2368 migrate_irq_remapped_level(irq);
2369 return;
2370 }
2371
2372 migrate_ioapic_irq(irq, mask);
2373}
2374#endif
2375
Yinghai Lu497c9a12008-08-19 20:50:28 -07002376asmlinkage void smp_irq_move_cleanup_interrupt(void)
2377{
2378 unsigned vector, me;
2379 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002380#ifdef CONFIG_X86_64
2381 exit_idle();
2382#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002383 irq_enter();
2384
2385 me = smp_processor_id();
2386 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2387 unsigned int irq;
2388 struct irq_desc *desc;
2389 struct irq_cfg *cfg;
2390 irq = __get_cpu_var(vector_irq)[vector];
2391
2392 desc = irq_to_desc(irq);
2393 if (!desc)
2394 continue;
2395
2396 cfg = irq_cfg(irq);
2397 spin_lock(&desc->lock);
2398 if (!cfg->move_cleanup_count)
2399 goto unlock;
2400
2401 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2402 goto unlock;
2403
2404 __get_cpu_var(vector_irq)[vector] = -1;
2405 cfg->move_cleanup_count--;
2406unlock:
2407 spin_unlock(&desc->lock);
2408 }
2409
2410 irq_exit();
2411}
2412
2413static void irq_complete_move(unsigned int irq)
2414{
2415 struct irq_cfg *cfg = irq_cfg(irq);
2416 unsigned vector, me;
2417
2418 if (likely(!cfg->move_in_progress))
2419 return;
2420
2421 vector = ~get_irq_regs()->orig_ax;
2422 me = smp_processor_id();
2423 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2424 cpumask_t cleanup_mask;
2425
2426 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2427 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2428 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2429 cfg->move_in_progress = 0;
2430 }
2431}
2432#else
2433static inline void irq_complete_move(unsigned int irq) {}
2434#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002435#ifdef CONFIG_INTR_REMAP
2436static void ack_x2apic_level(unsigned int irq)
2437{
2438 ack_x2APIC_irq();
2439}
2440
2441static void ack_x2apic_edge(unsigned int irq)
2442{
2443 ack_x2APIC_irq();
2444}
2445#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002446
Yinghai Lu1d025192008-08-19 20:50:34 -07002447static void ack_apic_edge(unsigned int irq)
2448{
2449 irq_complete_move(irq);
2450 move_native_irq(irq);
2451 ack_APIC_irq();
2452}
2453
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002454#ifdef CONFIG_X86_32
2455atomic_t irq_mis_count;
2456#endif
2457
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002458static void ack_apic_level(unsigned int irq)
2459{
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002460#ifdef CONFIG_X86_32
2461 unsigned long v;
2462 int i;
2463#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002464 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002465
Ingo Molnar54168ed2008-08-20 09:07:45 +02002466 irq_complete_move(irq);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002467#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002468 /* If we are moving the irq we need to mask it */
2469 if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2470 do_unmask_irq = 1;
2471 mask_IO_APIC_irq(irq);
2472 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002473#endif
2474
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002475#ifdef CONFIG_X86_32
2476 /*
2477 * It appears there is an erratum which affects at least version 0x11
2478 * of I/O APIC (that's the 82093AA and cores integrated into various
2479 * chipsets). Under certain conditions a level-triggered interrupt is
2480 * erroneously delivered as edge-triggered one but the respective IRR
2481 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2482 * message but it will never arrive and further interrupts are blocked
2483 * from the source. The exact reason is so far unknown, but the
2484 * phenomenon was observed when two consecutive interrupt requests
2485 * from a given source get delivered to the same CPU and the source is
2486 * temporarily disabled in between.
2487 *
2488 * A workaround is to simulate an EOI message manually. We achieve it
2489 * by setting the trigger mode to edge and then to level when the edge
2490 * trigger mode gets detected in the TMR of a local APIC for a
2491 * level-triggered interrupt. We mask the source for the time of the
2492 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2493 * The idea is from Manfred Spraul. --macro
2494 */
2495 i = irq_cfg(irq)->vector;
2496
2497 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2498#endif
2499
Ingo Molnar54168ed2008-08-20 09:07:45 +02002500 /*
2501 * We must acknowledge the irq before we move it or the acknowledge will
2502 * not propagate properly.
2503 */
2504 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002505
Ingo Molnar54168ed2008-08-20 09:07:45 +02002506 /* Now we can move and renable the irq */
2507 if (unlikely(do_unmask_irq)) {
2508 /* Only migrate the irq if the ack has been received.
2509 *
2510 * On rare occasions the broadcast level triggered ack gets
2511 * delayed going to ioapics, and if we reprogram the
2512 * vector while Remote IRR is still set the irq will never
2513 * fire again.
2514 *
2515 * To prevent this scenario we read the Remote IRR bit
2516 * of the ioapic. This has two effects.
2517 * - On any sane system the read of the ioapic will
2518 * flush writes (and acks) going to the ioapic from
2519 * this cpu.
2520 * - We get to see if the ACK has actually been delivered.
2521 *
2522 * Based on failed experiments of reprogramming the
2523 * ioapic entry from outside of irq context starting
2524 * with masking the ioapic entry and then polling until
2525 * Remote IRR was clear before reprogramming the
2526 * ioapic I don't trust the Remote IRR bit to be
2527 * completey accurate.
2528 *
2529 * However there appears to be no other way to plug
2530 * this race, so if the Remote IRR bit is not
2531 * accurate and is causing problems then it is a hardware bug
2532 * and you can go talk to the chipset vendor about it.
2533 */
2534 if (!io_apic_level_ack_pending(irq))
2535 move_masked_irq(irq);
2536 unmask_IO_APIC_irq(irq);
2537 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002538
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002539#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002540 if (!(v & (1 << (i & 0x1f)))) {
2541 atomic_inc(&irq_mis_count);
2542 spin_lock(&ioapic_lock);
2543 __mask_and_edge_IO_APIC_irq(irq);
2544 __unmask_and_level_IO_APIC_irq(irq);
2545 spin_unlock(&ioapic_lock);
2546 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002547#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002548}
Yinghai Lu1d025192008-08-19 20:50:34 -07002549
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002550static struct irq_chip ioapic_chip __read_mostly = {
2551 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002552 .startup = startup_ioapic_irq,
2553 .mask = mask_IO_APIC_irq,
2554 .unmask = unmask_IO_APIC_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07002555 .ack = ack_apic_edge,
2556 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002557#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002558 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002559#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002560 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561};
2562
Ingo Molnar54168ed2008-08-20 09:07:45 +02002563#ifdef CONFIG_INTR_REMAP
2564static struct irq_chip ir_ioapic_chip __read_mostly = {
2565 .name = "IR-IO-APIC",
2566 .startup = startup_ioapic_irq,
2567 .mask = mask_IO_APIC_irq,
2568 .unmask = unmask_IO_APIC_irq,
2569 .ack = ack_x2apic_edge,
2570 .eoi = ack_x2apic_level,
2571#ifdef CONFIG_SMP
2572 .set_affinity = set_ir_ioapic_affinity_irq,
2573#endif
2574 .retrigger = ioapic_retrigger_irq,
2575};
2576#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
2578static inline void init_IO_APIC_traps(void)
2579{
2580 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002581 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002582 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 /*
2585 * NOTE! The local APIC isn't very good at handling
2586 * multiple interrupts at the same interrupt level.
2587 * As the interrupt level is determined by taking the
2588 * vector number and shifting that right by 4, we
2589 * want to spread these out a bit so that they don't
2590 * all fall in the same interrupt level.
2591 *
2592 * Also, we've got to be careful not to trash gate
2593 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2594 */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07002595 for_each_irq_cfg(irq, cfg) {
Yinghai Luda51a822008-08-19 20:50:25 -07002596 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 /*
2598 * Hmm.. We don't have an entry for this,
2599 * so default to an old-fashioned 8259
2600 * interrupt if we can..
2601 */
2602 if (irq < 16)
2603 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002604 else {
2605 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002607 desc->chip = &no_irq_chip;
2608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 }
2610 }
2611}
2612
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002613/*
2614 * The local APIC irq-chip implementation:
2615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002617static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
2619 unsigned long v;
2620
2621 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002622 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002625static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002627 unsigned long v;
2628
2629 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002630 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
Ingo Molnar54168ed2008-08-20 09:07:45 +02002633static void ack_lapic_irq (unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002634{
2635 ack_APIC_irq();
2636}
2637
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002638static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002639 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002640 .mask = mask_lapic_irq,
2641 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002642 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643};
2644
Yinghai Lu497c9a12008-08-19 20:50:28 -07002645static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002646{
Yinghai Lu08678b02008-08-19 20:50:05 -07002647 struct irq_desc *desc;
2648
2649 desc = irq_to_desc(irq);
2650 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002651 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2652 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002653}
2654
Jan Beuliche9427102008-01-30 13:31:24 +01002655static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002658 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 * We put the 8259A master into AEOI mode and
2660 * unmask on all local APICs LVT0 as NMI.
2661 *
2662 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2663 * is from Maciej W. Rozycki - so we do not have to EOI from
2664 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002665 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2667
Jan Beuliche9427102008-01-30 13:31:24 +01002668 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 apic_printk(APIC_VERBOSE, " done.\n");
2671}
2672
2673/*
2674 * This looks a bit hackish but it's about the only one way of sending
2675 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2676 * not support the ExtINT mode, unfortunately. We need to send these
2677 * cycles as some i82489DX-based boards have glue logic that keeps the
2678 * 8259A interrupt line asserted until INTA. --macro
2679 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002680static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002682 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 struct IO_APIC_route_entry entry0, entry1;
2684 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002686 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002687 if (pin == -1) {
2688 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002690 }
2691 apic = find_isa_irq_apic(8, mp_INT);
2692 if (apic == -1) {
2693 WARN_ON_ONCE(1);
2694 return;
2695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Andi Kleencf4c6a22006-09-26 10:52:30 +02002697 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002698 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 memset(&entry1, 0, sizeof(entry1));
2701
2702 entry1.dest_mode = 0; /* physical delivery */
2703 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002704 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 entry1.delivery_mode = dest_ExtINT;
2706 entry1.polarity = entry0.polarity;
2707 entry1.trigger = 0;
2708 entry1.vector = 0;
2709
Andi Kleencf4c6a22006-09-26 10:52:30 +02002710 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 save_control = CMOS_READ(RTC_CONTROL);
2713 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2714 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2715 RTC_FREQ_SELECT);
2716 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2717
2718 i = 100;
2719 while (i-- > 0) {
2720 mdelay(10);
2721 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2722 i -= 10;
2723 }
2724
2725 CMOS_WRITE(save_control, RTC_CONTROL);
2726 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002727 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Andi Kleencf4c6a22006-09-26 10:52:30 +02002729 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Yinghai Luefa25592008-08-19 20:50:36 -07002732static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002733/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002734static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002735{
2736 disable_timer_pin_1 = 1;
2737 return 0;
2738}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002739early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002740
2741int timer_through_8259 __initdata;
2742
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743/*
2744 * This code may look a bit paranoid, but it's supposed to cooperate with
2745 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2746 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2747 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002748 *
2749 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002751static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002753 struct irq_cfg *cfg = irq_cfg(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002754 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002755 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002756 unsigned int ver;
2757 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002758
2759 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002760
Ingo Molnar54168ed2008-08-20 09:07:45 +02002761 ver = apic_read(APIC_LVR);
2762 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 /*
2765 * get/set the timer IRQ vector:
2766 */
2767 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002768 assign_irq_vector(0, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002771 * As IRQ0 is to be enabled in the 8259A, the virtual
2772 * wire has to be disabled in the local APIC. Also
2773 * timer interrupts need to be acknowledged manually in
2774 * the 8259A for the i82489DX when using the NMI
2775 * watchdog as that APIC treats NMIs as level-triggered.
2776 * The AEOI mode will finish them in the 8259A
2777 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002779 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002781#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002782 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002783#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002785 pin1 = find_isa_irq_pin(0, mp_INT);
2786 apic1 = find_isa_irq_apic(0, mp_INT);
2787 pin2 = ioapic_i8259.pin;
2788 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002790 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2791 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002792 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002794 /*
2795 * Some BIOS writers are clueless and report the ExtINTA
2796 * I/O APIC input from the cascaded 8259A as the timer
2797 * interrupt input. So just in case, if only one pin
2798 * was found above, try it both directly and through the
2799 * 8259A.
2800 */
2801 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002802#ifdef CONFIG_INTR_REMAP
2803 if (intr_remapping_enabled)
2804 panic("BIOS bug: timer not connected to IO-APIC");
2805#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002806 pin1 = pin2;
2807 apic1 = apic2;
2808 no_pin1 = 1;
2809 } else if (pin2 == -1) {
2810 pin2 = pin1;
2811 apic2 = apic1;
2812 }
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (pin1 != -1) {
2815 /*
2816 * Ok, does IRQ0 through the IOAPIC work?
2817 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002818 if (no_pin1) {
2819 add_pin_to_irq(0, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002820 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 unmask_IO_APIC_irq(0);
2823 if (timer_irq_works()) {
2824 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 setup_nmi();
2826 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002828 if (disable_timer_pin_1 > 0)
2829 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002830 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002832#ifdef CONFIG_INTR_REMAP
2833 if (intr_remapping_enabled)
2834 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2835#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002836 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002837 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002838 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2839 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002841 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2842 "(IRQ0) through the 8259A ...\n");
2843 apic_printk(APIC_QUIET, KERN_INFO
2844 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 /*
2846 * legacy devices should be connected to IO APIC #0
2847 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002848 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002849 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002850 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002851 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002853 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002854 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002856 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002858 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002860 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
2862 /*
2863 * Cleanup, just in case ...
2864 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002865 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002866 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002867 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
2870 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002871 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2872 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002873 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002875#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002876 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002877#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002879 apic_printk(APIC_QUIET, KERN_INFO
2880 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Yinghai Lu497c9a12008-08-19 20:50:28 -07002882 lapic_register_intr(0);
2883 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 enable_8259A_irq(0);
2885
2886 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002887 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002888 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002890 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002891 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002892 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002894 apic_printk(APIC_QUIET, KERN_INFO
2895 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 init_8259A(0);
2898 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002899 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901 unlock_ExtINT_logic();
2902
2903 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002904 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002905 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002907 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002909 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002910out:
2911 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
2914/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002915 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2916 * to devices. However there may be an I/O APIC pin available for
2917 * this interrupt regardless. The pin may be left unconnected, but
2918 * typically it will be reused as an ExtINT cascade interrupt for
2919 * the master 8259A. In the MPS case such a pin will normally be
2920 * reported as an ExtINT interrupt in the MP table. With ACPI
2921 * there is no provision for ExtINT interrupts, and in the absence
2922 * of an override it would be treated as an ordinary ISA I/O APIC
2923 * interrupt, that is edge-triggered and unmasked by default. We
2924 * used to do this, but it caused problems on some systems because
2925 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2926 * the same ExtINT cascade interrupt to drive the local APIC of the
2927 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2928 * the I/O APIC in all cases now. No actual device should request
2929 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 */
2931#define PIC_IRQS (1 << PIC_CASCADE_IR)
2932
2933void __init setup_IO_APIC(void)
2934{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002935
2936#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002938#else
2939 /*
2940 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2941 */
2942#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002944 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Ingo Molnar54168ed2008-08-20 09:07:45 +02002946 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2947 /*
2948 * Set up IO-APIC IRQ routing.
2949 */
2950#ifdef CONFIG_X86_32
2951 if (!acpi_ioapic)
2952 setup_ioapic_ids_from_mpc();
2953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 sync_Arb_IDs();
2955 setup_IO_APIC_irqs();
2956 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002957 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958}
2959
2960/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002961 * Called after all the initialization is done. If we didnt find any
2962 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965static int __init io_apic_bug_finalize(void)
2966{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002967 if (sis_apic_bug == -1)
2968 sis_apic_bug = 0;
2969 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
2972late_initcall(io_apic_bug_finalize);
2973
2974struct sysfs_ioapic_data {
2975 struct sys_device dev;
2976 struct IO_APIC_route_entry entry[0];
2977};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002978static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Pavel Machek438510f2005-04-16 15:25:24 -07002980static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 struct IO_APIC_route_entry *entry;
2983 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 data = container_of(dev, struct sysfs_ioapic_data, dev);
2987 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002988 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2989 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991 return 0;
2992}
2993
2994static int ioapic_resume(struct sys_device *dev)
2995{
2996 struct IO_APIC_route_entry *entry;
2997 struct sysfs_ioapic_data *data;
2998 unsigned long flags;
2999 union IO_APIC_reg_00 reg_00;
3000 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 data = container_of(dev, struct sysfs_ioapic_data, dev);
3003 entry = data->entry;
3004
3005 spin_lock_irqsave(&ioapic_lock, flags);
3006 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04003007 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
3008 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 io_apic_write(dev->id, 0, reg_00.raw);
3010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003012 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003013 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 return 0;
3016}
3017
3018static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003019 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 .suspend = ioapic_suspend,
3021 .resume = ioapic_resume,
3022};
3023
3024static int __init ioapic_init_sysfs(void)
3025{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003026 struct sys_device * dev;
3027 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029 error = sysdev_class_register(&ioapic_sysdev_class);
3030 if (error)
3031 return error;
3032
Ingo Molnar54168ed2008-08-20 09:07:45 +02003033 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003034 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003036 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 if (!mp_ioapic_data[i]) {
3038 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3039 continue;
3040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003042 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 dev->cls = &ioapic_sysdev_class;
3044 error = sysdev_register(dev);
3045 if (error) {
3046 kfree(mp_ioapic_data[i]);
3047 mp_ioapic_data[i] = NULL;
3048 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3049 continue;
3050 }
3051 }
3052
3053 return 0;
3054}
3055
3056device_initcall(ioapic_init_sysfs);
3057
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003058/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003059 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003060 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003061unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003062{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003063 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003064 unsigned int irq;
3065 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003066 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07003067 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003068
Yinghai Lu497c9a12008-08-19 20:50:28 -07003069#ifndef CONFIG_HAVE_SPARSE_IRQ
Yinghai Lu199751d2008-08-19 20:50:27 -07003070 irq_want = nr_irqs - 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003071#endif
Yinghai Lu199751d2008-08-19 20:50:27 -07003072
3073 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003074 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003075 for (new = irq_want; new > 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003076 if (platform_legacy_irq(new))
3077 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07003078 cfg_new = irq_cfg(new);
3079 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003080 continue;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003081 /* check if need to create one */
Yinghai Luda51a822008-08-19 20:50:25 -07003082 if (!cfg_new)
3083 cfg_new = irq_cfg_alloc(new);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003084 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003085 irq = new;
3086 break;
3087 }
3088 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003089
Yinghai Lu199751d2008-08-19 20:50:27 -07003090 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003091 dynamic_irq_init(irq);
3092 }
3093 return irq;
3094}
3095
Yinghai Lu199751d2008-08-19 20:50:27 -07003096int create_irq(void)
3097{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003098 int irq;
3099
3100 irq = create_irq_nr(nr_irqs - 1);
3101
3102 if (irq == 0)
3103 irq = -1;
3104
3105 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003106}
3107
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003108void destroy_irq(unsigned int irq)
3109{
3110 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003111
3112 dynamic_irq_cleanup(irq);
3113
Ingo Molnar54168ed2008-08-20 09:07:45 +02003114#ifdef CONFIG_INTR_REMAP
3115 free_irte(irq);
3116#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003117 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003118 __clear_irq_vector(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003119 spin_unlock_irqrestore(&vector_lock, flags);
3120}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003121
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003122/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003123 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003124 */
3125#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003126static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003127{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003128 struct irq_cfg *cfg;
3129 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003130 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003131 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003132
Yinghai Lu497c9a12008-08-19 20:50:28 -07003133 tmp = TARGET_CPUS;
3134 err = assign_irq_vector(irq, tmp);
3135 if (err)
3136 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003137
Yinghai Lu497c9a12008-08-19 20:50:28 -07003138 cfg = irq_cfg(irq);
3139 cpus_and(tmp, cfg->domain, tmp);
3140 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003141
Ingo Molnar54168ed2008-08-20 09:07:45 +02003142#ifdef CONFIG_INTR_REMAP
3143 if (irq_remapped(irq)) {
3144 struct irte irte;
3145 int ir_index;
3146 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003147
Ingo Molnar54168ed2008-08-20 09:07:45 +02003148 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3149 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003150
Ingo Molnar54168ed2008-08-20 09:07:45 +02003151 memset (&irte, 0, sizeof(irte));
3152
3153 irte.present = 1;
3154 irte.dst_mode = INT_DEST_MODE;
3155 irte.trigger_mode = 0; /* edge */
3156 irte.dlvry_mode = INT_DELIVERY_MODE;
3157 irte.vector = cfg->vector;
3158 irte.dest_id = IRTE_DEST(dest);
3159
3160 modify_irte(irq, &irte);
3161
3162 msg->address_hi = MSI_ADDR_BASE_HI;
3163 msg->data = sub_handle;
3164 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3165 MSI_ADDR_IR_SHV |
3166 MSI_ADDR_IR_INDEX1(ir_index) |
3167 MSI_ADDR_IR_INDEX2(ir_index);
3168 } else
3169#endif
3170 {
3171 msg->address_hi = MSI_ADDR_BASE_HI;
3172 msg->address_lo =
3173 MSI_ADDR_BASE_LO |
3174 ((INT_DEST_MODE == 0) ?
3175 MSI_ADDR_DEST_MODE_PHYSICAL:
3176 MSI_ADDR_DEST_MODE_LOGICAL) |
3177 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3178 MSI_ADDR_REDIRECTION_CPU:
3179 MSI_ADDR_REDIRECTION_LOWPRI) |
3180 MSI_ADDR_DEST_ID(dest);
3181
3182 msg->data =
3183 MSI_DATA_TRIGGER_EDGE |
3184 MSI_DATA_LEVEL_ASSERT |
3185 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3186 MSI_DATA_DELIVERY_FIXED:
3187 MSI_DATA_DELIVERY_LOWPRI) |
3188 MSI_DATA_VECTOR(cfg->vector);
3189 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003190 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003191}
3192
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003193#ifdef CONFIG_SMP
3194static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3195{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003196 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003197 struct msi_msg msg;
3198 unsigned int dest;
3199 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003200 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003201
3202 cpus_and(tmp, mask, cpu_online_map);
3203 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003204 return;
3205
Yinghai Lu497c9a12008-08-19 20:50:28 -07003206 if (assign_irq_vector(irq, mask))
3207 return;
3208
3209 cfg = irq_cfg(irq);
3210 cpus_and(tmp, cfg->domain, mask);
3211 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003212
3213 read_msi_msg(irq, &msg);
3214
3215 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003216 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003217 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3218 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3219
3220 write_msi_msg(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003221 desc = irq_to_desc(irq);
3222 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003223}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003224
3225#ifdef CONFIG_INTR_REMAP
3226/*
3227 * Migrate the MSI irq to another cpumask. This migration is
3228 * done in the process context using interrupt-remapping hardware.
3229 */
3230static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3231{
3232 struct irq_cfg *cfg;
3233 unsigned int dest;
3234 cpumask_t tmp, cleanup_mask;
3235 struct irte irte;
3236 struct irq_desc *desc;
3237
3238 cpus_and(tmp, mask, cpu_online_map);
3239 if (cpus_empty(tmp))
3240 return;
3241
3242 if (get_irte(irq, &irte))
3243 return;
3244
3245 if (assign_irq_vector(irq, mask))
3246 return;
3247
3248 cfg = irq_cfg(irq);
3249 cpus_and(tmp, cfg->domain, mask);
3250 dest = cpu_mask_to_apicid(tmp);
3251
3252 irte.vector = cfg->vector;
3253 irte.dest_id = IRTE_DEST(dest);
3254
3255 /*
3256 * atomically update the IRTE with the new destination and vector.
3257 */
3258 modify_irte(irq, &irte);
3259
3260 /*
3261 * After this point, all the interrupts will start arriving
3262 * at the new destination. So, time to cleanup the previous
3263 * vector allocation.
3264 */
3265 if (cfg->move_in_progress) {
3266 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3267 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3268 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3269 cfg->move_in_progress = 0;
3270 }
3271
3272 desc = irq_to_desc(irq);
3273 desc->affinity = mask;
3274}
3275#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003276#endif /* CONFIG_SMP */
3277
3278/*
3279 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3280 * which implement the MSI or MSI-X Capability Structure.
3281 */
3282static struct irq_chip msi_chip = {
3283 .name = "PCI-MSI",
3284 .unmask = unmask_msi_irq,
3285 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003286 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003287#ifdef CONFIG_SMP
3288 .set_affinity = set_msi_irq_affinity,
3289#endif
3290 .retrigger = ioapic_retrigger_irq,
3291};
3292
Ingo Molnar54168ed2008-08-20 09:07:45 +02003293#ifdef CONFIG_INTR_REMAP
3294static struct irq_chip msi_ir_chip = {
3295 .name = "IR-PCI-MSI",
3296 .unmask = unmask_msi_irq,
3297 .mask = mask_msi_irq,
3298 .ack = ack_x2apic_edge,
3299#ifdef CONFIG_SMP
3300 .set_affinity = ir_set_msi_irq_affinity,
3301#endif
3302 .retrigger = ioapic_retrigger_irq,
3303};
3304
3305/*
3306 * Map the PCI dev to the corresponding remapping hardware unit
3307 * and allocate 'nvec' consecutive interrupt-remapping table entries
3308 * in it.
3309 */
3310static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3311{
3312 struct intel_iommu *iommu;
3313 int index;
3314
3315 iommu = map_dev_to_ir(dev);
3316 if (!iommu) {
3317 printk(KERN_ERR
3318 "Unable to map PCI %s to iommu\n", pci_name(dev));
3319 return -ENOENT;
3320 }
3321
3322 index = alloc_irte(iommu, irq, nvec);
3323 if (index < 0) {
3324 printk(KERN_ERR
3325 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3326 pci_name(dev));
3327 return -ENOSPC;
3328 }
3329 return index;
3330}
3331#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003332
3333static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3334{
3335 int ret;
3336 struct msi_msg msg;
3337
3338 ret = msi_compose_msg(dev, irq, &msg);
3339 if (ret < 0)
3340 return ret;
3341
3342 set_irq_msi(irq, desc);
3343 write_msi_msg(irq, &msg);
3344
Ingo Molnar54168ed2008-08-20 09:07:45 +02003345#ifdef CONFIG_INTR_REMAP
3346 if (irq_remapped(irq)) {
3347 struct irq_desc *desc = irq_to_desc(irq);
3348 /*
3349 * irq migration in process context
3350 */
3351 desc->status |= IRQ_MOVE_PCNTXT;
3352 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3353 } else
3354#endif
3355 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003356
3357 return 0;
3358}
3359
Yinghai Lu199751d2008-08-19 20:50:27 -07003360static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3361{
3362 unsigned int irq;
3363
3364 irq = dev->bus->number;
3365 irq <<= 8;
3366 irq |= dev->devfn;
3367 irq <<= 12;
3368
3369 return irq;
3370}
3371
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003372int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003373{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003374 unsigned int irq;
3375 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003376 unsigned int irq_want;
3377
3378 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3379
3380 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003381 if (irq == 0)
3382 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003383
Ingo Molnar54168ed2008-08-20 09:07:45 +02003384#ifdef CONFIG_INTR_REMAP
3385 if (!intr_remapping_enabled)
3386 goto no_ir;
3387
3388 ret = msi_alloc_irte(dev, irq, 1);
3389 if (ret < 0)
3390 goto error;
3391no_ir:
3392#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003393 ret = setup_msi_irq(dev, desc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003394 if (ret < 0) {
3395 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003396 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003397 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003398 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003399
3400#ifdef CONFIG_INTR_REMAP
3401error:
3402 destroy_irq(irq);
3403 return ret;
3404#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003405}
3406
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003407int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3408{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003409 unsigned int irq;
3410 int ret, sub_handle;
3411 struct msi_desc *desc;
3412 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003413
Ingo Molnar54168ed2008-08-20 09:07:45 +02003414#ifdef CONFIG_INTR_REMAP
3415 struct intel_iommu *iommu = 0;
3416 int index = 0;
3417#endif
3418
3419 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3420 sub_handle = 0;
3421 list_for_each_entry(desc, &dev->msi_list, list) {
3422 irq = create_irq_nr(irq_want--);
3423 if (irq == 0)
3424 return -1;
3425#ifdef CONFIG_INTR_REMAP
3426 if (!intr_remapping_enabled)
3427 goto no_ir;
3428
3429 if (!sub_handle) {
3430 /*
3431 * allocate the consecutive block of IRTE's
3432 * for 'nvec'
3433 */
3434 index = msi_alloc_irte(dev, irq, nvec);
3435 if (index < 0) {
3436 ret = index;
3437 goto error;
3438 }
3439 } else {
3440 iommu = map_dev_to_ir(dev);
3441 if (!iommu) {
3442 ret = -ENOENT;
3443 goto error;
3444 }
3445 /*
3446 * setup the mapping between the irq and the IRTE
3447 * base index, the sub_handle pointing to the
3448 * appropriate interrupt remap table entry.
3449 */
3450 set_irte_irq(irq, iommu, index, sub_handle);
3451 }
3452no_ir:
3453#endif
3454 ret = setup_msi_irq(dev, desc, irq);
3455 if (ret < 0)
3456 goto error;
3457 sub_handle++;
3458 }
3459 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003460
3461error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003462 destroy_irq(irq);
3463 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003464}
3465
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003466void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003467{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003468 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003469}
3470
Ingo Molnar54168ed2008-08-20 09:07:45 +02003471#ifdef CONFIG_DMAR
3472#ifdef CONFIG_SMP
3473static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3474{
3475 struct irq_cfg *cfg;
3476 struct msi_msg msg;
3477 unsigned int dest;
3478 cpumask_t tmp;
3479 struct irq_desc *desc;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003480
Ingo Molnar54168ed2008-08-20 09:07:45 +02003481 cpus_and(tmp, mask, cpu_online_map);
3482 if (cpus_empty(tmp))
3483 return;
3484
3485 if (assign_irq_vector(irq, mask))
3486 return;
3487
3488 cfg = irq_cfg(irq);
3489 cpus_and(tmp, cfg->domain, mask);
3490 dest = cpu_mask_to_apicid(tmp);
3491
3492 dmar_msi_read(irq, &msg);
3493
3494 msg.data &= ~MSI_DATA_VECTOR_MASK;
3495 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3496 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3497 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3498
3499 dmar_msi_write(irq, &msg);
3500 desc = irq_to_desc(irq);
3501 desc->affinity = mask;
3502}
3503#endif /* CONFIG_SMP */
3504
3505struct irq_chip dmar_msi_type = {
3506 .name = "DMAR_MSI",
3507 .unmask = dmar_msi_unmask,
3508 .mask = dmar_msi_mask,
3509 .ack = ack_apic_edge,
3510#ifdef CONFIG_SMP
3511 .set_affinity = dmar_msi_set_affinity,
3512#endif
3513 .retrigger = ioapic_retrigger_irq,
3514};
3515
3516int arch_setup_dmar_msi(unsigned int irq)
3517{
3518 int ret;
3519 struct msi_msg msg;
3520
3521 ret = msi_compose_msg(NULL, irq, &msg);
3522 if (ret < 0)
3523 return ret;
3524 dmar_msi_write(irq, &msg);
3525 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3526 "edge");
3527 return 0;
3528}
3529#endif
3530
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003531#ifdef CONFIG_HPET_TIMER
3532
3533#ifdef CONFIG_SMP
3534static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
3535{
3536 struct irq_cfg *cfg;
3537 struct irq_desc *desc;
3538 struct msi_msg msg;
3539 unsigned int dest;
3540 cpumask_t tmp;
3541
3542 cpus_and(tmp, mask, cpu_online_map);
3543 if (cpus_empty(tmp))
3544 return;
3545
3546 if (assign_irq_vector(irq, mask))
3547 return;
3548
3549 cfg = irq_cfg(irq);
3550 cpus_and(tmp, cfg->domain, mask);
3551 dest = cpu_mask_to_apicid(tmp);
3552
3553 hpet_msi_read(irq, &msg);
3554
3555 msg.data &= ~MSI_DATA_VECTOR_MASK;
3556 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3557 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3558 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3559
3560 hpet_msi_write(irq, &msg);
3561 desc = irq_to_desc(irq);
3562 desc->affinity = mask;
3563}
3564#endif /* CONFIG_SMP */
3565
3566struct irq_chip hpet_msi_type = {
3567 .name = "HPET_MSI",
3568 .unmask = hpet_msi_unmask,
3569 .mask = hpet_msi_mask,
3570 .ack = ack_apic_edge,
3571#ifdef CONFIG_SMP
3572 .set_affinity = hpet_msi_set_affinity,
3573#endif
3574 .retrigger = ioapic_retrigger_irq,
3575};
3576
3577int arch_setup_hpet_msi(unsigned int irq)
3578{
3579 int ret;
3580 struct msi_msg msg;
3581
3582 ret = msi_compose_msg(NULL, irq, &msg);
3583 if (ret < 0)
3584 return ret;
3585
3586 hpet_msi_write(irq, &msg);
3587 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3588 "edge");
3589 return 0;
3590}
3591#endif
3592
Ingo Molnar54168ed2008-08-20 09:07:45 +02003593#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003594/*
3595 * Hypertransport interrupt support
3596 */
3597#ifdef CONFIG_HT_IRQ
3598
3599#ifdef CONFIG_SMP
3600
Yinghai Lu497c9a12008-08-19 20:50:28 -07003601static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003602{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003603 struct ht_irq_msg msg;
3604 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003605
Yinghai Lu497c9a12008-08-19 20:50:28 -07003606 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003607 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003608
Yinghai Lu497c9a12008-08-19 20:50:28 -07003609 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003610 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003611
Eric W. Biedermanec683072006-11-08 17:44:57 -08003612 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003613}
3614
3615static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3616{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003617 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003618 unsigned int dest;
3619 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003620 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003621
3622 cpus_and(tmp, mask, cpu_online_map);
3623 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003624 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003625
Yinghai Lu497c9a12008-08-19 20:50:28 -07003626 if (assign_irq_vector(irq, mask))
3627 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003628
Yinghai Lu497c9a12008-08-19 20:50:28 -07003629 cfg = irq_cfg(irq);
3630 cpus_and(tmp, cfg->domain, mask);
3631 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003632
Yinghai Lu497c9a12008-08-19 20:50:28 -07003633 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003634 desc = irq_to_desc(irq);
3635 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003636}
3637#endif
3638
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003639static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003640 .name = "PCI-HT",
3641 .mask = mask_ht_irq,
3642 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003643 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003644#ifdef CONFIG_SMP
3645 .set_affinity = set_ht_irq_affinity,
3646#endif
3647 .retrigger = ioapic_retrigger_irq,
3648};
3649
3650int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3651{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003652 struct irq_cfg *cfg;
3653 int err;
3654 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003655
Yinghai Lu497c9a12008-08-19 20:50:28 -07003656 tmp = TARGET_CPUS;
3657 err = assign_irq_vector(irq, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003658 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003659 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003660 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003661
Yinghai Lu497c9a12008-08-19 20:50:28 -07003662 cfg = irq_cfg(irq);
3663 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003664 dest = cpu_mask_to_apicid(tmp);
3665
Eric W. Biedermanec683072006-11-08 17:44:57 -08003666 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003667
Eric W. Biedermanec683072006-11-08 17:44:57 -08003668 msg.address_lo =
3669 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003670 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003671 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003672 ((INT_DEST_MODE == 0) ?
3673 HT_IRQ_LOW_DM_PHYSICAL :
3674 HT_IRQ_LOW_DM_LOGICAL) |
3675 HT_IRQ_LOW_RQEOI_EDGE |
3676 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3677 HT_IRQ_LOW_MT_FIXED :
3678 HT_IRQ_LOW_MT_ARBITRATED) |
3679 HT_IRQ_LOW_IRQ_MASKED;
3680
Eric W. Biedermanec683072006-11-08 17:44:57 -08003681 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003682
Ingo Molnara460e742006-10-17 00:10:03 -07003683 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3684 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003685 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003686 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003687}
3688#endif /* CONFIG_HT_IRQ */
3689
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003690int __init io_apic_get_redir_entries (int ioapic)
3691{
3692 union IO_APIC_reg_01 reg_01;
3693 unsigned long flags;
3694
3695 spin_lock_irqsave(&ioapic_lock, flags);
3696 reg_01.raw = io_apic_read(ioapic, 1);
3697 spin_unlock_irqrestore(&ioapic_lock, flags);
3698
3699 return reg_01.bits.entries;
3700}
3701
3702int __init probe_nr_irqs(void)
3703{
3704 int idx;
3705 int nr = 0;
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003706#ifndef CONFIG_XEN
3707 int nr_min = 32;
3708#else
3709 int nr_min = NR_IRQS;
3710#endif
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003711
3712 for (idx = 0; idx < nr_ioapics; idx++)
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003713 nr += io_apic_get_redir_entries(idx) + 1;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003714
3715 /* double it for hotplug and msi and nmi */
3716 nr <<= 1;
3717
3718 /* something wrong ? */
Yinghai Lu052c0bf2008-08-21 13:10:09 -07003719 if (nr < nr_min)
3720 nr = nr_min;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003721
3722 return nr;
3723}
3724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003726 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 -------------------------------------------------------------------------- */
3728
Len Brown888ba6c2005-08-24 12:07:20 -04003729#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730
Ingo Molnar54168ed2008-08-20 09:07:45 +02003731#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003732int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
3734 union IO_APIC_reg_00 reg_00;
3735 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3736 physid_mask_t tmp;
3737 unsigned long flags;
3738 int i = 0;
3739
3740 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003741 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3742 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003744 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3746 * advantage of new APIC bus architecture.
3747 */
3748
3749 if (physids_empty(apic_id_map))
3750 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3751
3752 spin_lock_irqsave(&ioapic_lock, flags);
3753 reg_00.raw = io_apic_read(ioapic, 0);
3754 spin_unlock_irqrestore(&ioapic_lock, flags);
3755
3756 if (apic_id >= get_physical_broadcast()) {
3757 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3758 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3759 apic_id = reg_00.bits.ID;
3760 }
3761
3762 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003763 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 * 'stuck on smp_invalidate_needed IPI wait' messages.
3765 */
3766 if (check_apicid_used(apic_id_map, apic_id)) {
3767
3768 for (i = 0; i < get_physical_broadcast(); i++) {
3769 if (!check_apicid_used(apic_id_map, i))
3770 break;
3771 }
3772
3773 if (i == get_physical_broadcast())
3774 panic("Max apic_id exceeded!\n");
3775
3776 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3777 "trying %d\n", ioapic, apic_id, i);
3778
3779 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 tmp = apicid_to_cpu_present(apic_id);
3783 physids_or(apic_id_map, apic_id_map, tmp);
3784
3785 if (reg_00.bits.ID != apic_id) {
3786 reg_00.bits.ID = apic_id;
3787
3788 spin_lock_irqsave(&ioapic_lock, flags);
3789 io_apic_write(ioapic, 0, reg_00.raw);
3790 reg_00.raw = io_apic_read(ioapic, 0);
3791 spin_unlock_irqrestore(&ioapic_lock, flags);
3792
3793 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003794 if (reg_00.bits.ID != apic_id) {
3795 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3796 return -1;
3797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
3799
3800 apic_printk(APIC_VERBOSE, KERN_INFO
3801 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3802
3803 return apic_id;
3804}
3805
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003806int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
3808 union IO_APIC_reg_01 reg_01;
3809 unsigned long flags;
3810
3811 spin_lock_irqsave(&ioapic_lock, flags);
3812 reg_01.raw = io_apic_read(ioapic, 1);
3813 spin_unlock_irqrestore(&ioapic_lock, flags);
3814
3815 return reg_01.bits.version;
3816}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003817#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Ingo Molnar54168ed2008-08-20 09:07:45 +02003819int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003822 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 ioapic);
3824 return -EINVAL;
3825 }
3826
3827 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 * IRQs < 16 are already in the irq_2_pin[] map
3829 */
3830 if (irq >= 16)
3831 add_pin_to_irq(irq, ioapic, pin);
3832
Yinghai Lu497c9a12008-08-19 20:50:28 -07003833 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834
3835 return 0;
3836}
3837
Ingo Molnar54168ed2008-08-20 09:07:45 +02003838
Shaohua Li61fd47e2007-11-17 01:05:28 -05003839int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3840{
3841 int i;
3842
3843 if (skip_ioapic_setup)
3844 return -1;
3845
3846 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003847 if (mp_irqs[i].mp_irqtype == mp_INT &&
3848 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003849 break;
3850 if (i >= mp_irq_entries)
3851 return -1;
3852
3853 *trigger = irq_trigger(i);
3854 *polarity = irq_polarity(i);
3855 return 0;
3856}
3857
Len Brown888ba6c2005-08-24 12:07:20 -04003858#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003859
Yinghai Lu497c9a12008-08-19 20:50:28 -07003860/*
3861 * This function currently is only a helper for the i386 smp boot process where
3862 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3863 * so mask in all cases should simply be TARGET_CPUS
3864 */
3865#ifdef CONFIG_SMP
3866void __init setup_ioapic_dest(void)
3867{
3868 int pin, ioapic, irq, irq_entry;
3869 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003870
3871 if (skip_ioapic_setup == 1)
3872 return;
3873
3874 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3875 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3876 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3877 if (irq_entry == -1)
3878 continue;
3879 irq = pin_2_irq(irq_entry, ioapic, pin);
3880
3881 /* setup_IO_APIC_irqs could fail to get vector for some device
3882 * when you have too many devices, because at that time only boot
3883 * cpu is online.
3884 */
3885 cfg = irq_cfg(irq);
3886 if (!cfg->vector)
3887 setup_IO_APIC_irq(ioapic, pin, irq,
3888 irq_trigger(irq_entry),
3889 irq_polarity(irq_entry));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003890#ifdef CONFIG_INTR_REMAP
3891 else if (intr_remapping_enabled)
3892 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
3893#endif
3894 else
Yinghai Lu497c9a12008-08-19 20:50:28 -07003895 set_ioapic_affinity_irq(irq, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003896 }
3897
3898 }
3899}
3900#endif
3901
Ingo Molnar54168ed2008-08-20 09:07:45 +02003902#define IOAPIC_RESOURCE_NAME_SIZE 11
3903
3904static struct resource *ioapic_resources;
3905
3906static struct resource * __init ioapic_setup_resources(void)
3907{
3908 unsigned long n;
3909 struct resource *res;
3910 char *mem;
3911 int i;
3912
3913 if (nr_ioapics <= 0)
3914 return NULL;
3915
3916 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3917 n *= nr_ioapics;
3918
3919 mem = alloc_bootmem(n);
3920 res = (void *)mem;
3921
3922 if (mem != NULL) {
3923 mem += sizeof(struct resource) * nr_ioapics;
3924
3925 for (i = 0; i < nr_ioapics; i++) {
3926 res[i].name = mem;
3927 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3928 sprintf(mem, "IOAPIC %u", i);
3929 mem += IOAPIC_RESOURCE_NAME_SIZE;
3930 }
3931 }
3932
3933 ioapic_resources = res;
3934
3935 return res;
3936}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003937
Yinghai Luf3294a32008-06-27 01:41:56 -07003938void __init ioapic_init_mappings(void)
3939{
3940 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3941 int i;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003942 struct resource *ioapic_res;
Yinghai Luf3294a32008-06-27 01:41:56 -07003943
Ingo Molnar54168ed2008-08-20 09:07:45 +02003944 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003945 for (i = 0; i < nr_ioapics; i++) {
3946 if (smp_found_config) {
3947 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003948#ifdef CONFIG_X86_32
3949 if (!ioapic_phys) {
3950 printk(KERN_ERR
3951 "WARNING: bogus zero IO-APIC "
3952 "address found in MPTABLE, "
3953 "disabling IO/APIC support!\n");
3954 smp_found_config = 0;
3955 skip_ioapic_setup = 1;
3956 goto fake_ioapic_page;
3957 }
3958#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003959 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003960#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003961fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003962#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003963 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003964 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07003965 ioapic_phys = __pa(ioapic_phys);
3966 }
3967 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003968 apic_printk(APIC_VERBOSE,
3969 "mapped IOAPIC to %08lx (%08lx)\n",
3970 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07003971 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003972
Ingo Molnar54168ed2008-08-20 09:07:45 +02003973 if (ioapic_res != NULL) {
3974 ioapic_res->start = ioapic_phys;
3975 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3976 ioapic_res++;
3977 }
Yinghai Luf3294a32008-06-27 01:41:56 -07003978 }
3979}
3980
Ingo Molnar54168ed2008-08-20 09:07:45 +02003981static int __init ioapic_insert_resources(void)
3982{
3983 int i;
3984 struct resource *r = ioapic_resources;
3985
3986 if (!r) {
3987 printk(KERN_ERR
3988 "IO APIC resources could be not be allocated.\n");
3989 return -1;
3990 }
3991
3992 for (i = 0; i < nr_ioapics; i++) {
3993 insert_resource(&iomem_resource, r);
3994 r++;
3995 }
3996
3997 return 0;
3998}
3999
4000/* Insert the IO APIC resources after PCI initialization has occured to handle
4001 * IO APICS that are mapped in on a BAR in PCI space. */
4002late_initcall(ioapic_insert_resources);