blob: d7f0993b805688c39f276dfc9eead2f0c76da4b7 [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>
Dean Nelson4173a0e2008-10-02 12:18:21 -050061#include <asm/uv/uv_hub.h>
62#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Yinghai Lu497c9a12008-08-19 20:50:28 -070064#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020066#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010068#define __apicdebuginit(type) static type __init
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020071 * Is the SiS APIC rmw bug present ?
72 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74int sis_apic_bug = -1;
75
Yinghai Luefa25592008-08-19 20:50:36 -070076static DEFINE_SPINLOCK(ioapic_lock);
77static DEFINE_SPINLOCK(vector_lock);
78
Yinghai Luefa25592008-08-19 20:50:36 -070079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * # of IRQ routing registers
81 */
82int nr_ioapic_registers[MAX_IO_APICS];
83
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040084/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040085struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040086int nr_ioapics;
87
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040088/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040089struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040090
91/* # of MP IRQ source entries */
92int mp_irq_entries;
93
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040094#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
95int mp_bus_id_to_type[MAX_MP_BUSSES];
96#endif
97
98DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
99
Yinghai Luefa25592008-08-19 20:50:36 -0700100int skip_ioapic_setup;
101
Ingo Molnar54168ed2008-08-20 09:07:45 +0200102static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700103{
104 /* disable IO-APIC */
105 disable_ioapic_setup();
106 return 0;
107}
108early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200109
Yinghai Lu0f978f42008-08-19 20:50:26 -0700110struct irq_pin_list;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200111
112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * This is performance-critical, we want to do it O(1)
114 *
115 * the indexing order of this array favors 1:1 mappings
116 * between pins and IRQs.
117 */
118
Yinghai Lu0f978f42008-08-19 20:50:26 -0700119struct irq_pin_list {
120 int apic, pin;
121 struct irq_pin_list *next;
122};
Yinghai Lu301e6192008-08-19 20:50:02 -0700123
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800124static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700125{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800126 struct irq_pin_list *pin;
127 int node;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700128
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800129 node = cpu_to_node(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700130
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800131 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
132 printk(KERN_DEBUG " alloc irq_2_pin on cpu %d node %d\n", cpu, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700133
Yinghai Lu0f978f42008-08-19 20:50:26 -0700134 return pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700135}
136
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800137struct irq_cfg {
138 struct irq_pin_list *irq_2_pin;
139 cpumask_t domain;
140 cpumask_t old_domain;
141 unsigned move_cleanup_count;
142 u8 vector;
143 u8 move_in_progress : 1;
144};
145
146/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
147#ifdef CONFIG_SPARSE_IRQ
148static struct irq_cfg irq_cfgx[] = {
149#else
150static struct irq_cfg irq_cfgx[NR_IRQS] = {
151#endif
152 [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
153 [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
154 [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
155 [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
156 [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
157 [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
158 [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
159 [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
160 [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
161 [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
162 [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
163 [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
164 [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
165 [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
166 [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
167 [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
168};
169
170void __init arch_early_irq_init(void)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700171{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800172 struct irq_cfg *cfg;
173 struct irq_desc *desc;
174 int count;
175 int i;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700176
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800177 cfg = irq_cfgx;
178 count = ARRAY_SIZE(irq_cfgx);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700179
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800180 for (i = 0; i < count; i++) {
181 desc = irq_to_desc(i);
182 desc->chip_data = &cfg[i];
183 }
184}
185
186#ifdef CONFIG_SPARSE_IRQ
187static struct irq_cfg *irq_cfg(unsigned int irq)
188{
189 struct irq_cfg *cfg = NULL;
190 struct irq_desc *desc;
191
192 desc = irq_to_desc(irq);
193 if (desc)
194 cfg = desc->chip_data;
195
196 return cfg;
197}
198
199static struct irq_cfg *get_one_free_irq_cfg(int cpu)
200{
201 struct irq_cfg *cfg;
202 int node;
203
204 node = cpu_to_node(cpu);
205
206 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
207 printk(KERN_DEBUG " alloc irq_cfg on cpu %d node %d\n", cpu, node);
208
209 return cfg;
210}
211
212void arch_init_chip_data(struct irq_desc *desc, int cpu)
213{
214 struct irq_cfg *cfg;
215
216 cfg = desc->chip_data;
217 if (!cfg) {
218 desc->chip_data = get_one_free_irq_cfg(cpu);
219 if (!desc->chip_data) {
220 printk(KERN_ERR "can not alloc irq_cfg\n");
221 BUG_ON(1);
222 }
223 }
224}
225
226#else
227static struct irq_cfg *irq_cfg(unsigned int irq)
228{
229 return irq < nr_irqs ? irq_cfgx + irq : NULL;
230}
231
232#endif
233
Yinghai Lu3145e942008-12-05 18:58:34 -0800234static inline void set_extra_move_desc(struct irq_desc *desc, cpumask_t mask)
235{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds130fe052006-11-01 09:11:00 -0800238struct io_apic {
239 unsigned int index;
240 unsigned int unused[3];
241 unsigned int data;
242};
243
244static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
245{
246 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400247 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800248}
249
250static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
251{
252 struct io_apic __iomem *io_apic = io_apic_base(apic);
253 writel(reg, &io_apic->index);
254 return readl(&io_apic->data);
255}
256
257static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
258{
259 struct io_apic __iomem *io_apic = io_apic_base(apic);
260 writel(reg, &io_apic->index);
261 writel(value, &io_apic->data);
262}
263
264/*
265 * Re-write a value: to be used for read-modify-write
266 * cycles where the read already set up the index register.
267 *
268 * Older SiS APIC requires we rewrite the index register
269 */
270static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
271{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200272 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200273
274 if (sis_apic_bug)
275 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800276 writel(value, &io_apic->data);
277}
278
Yinghai Lu3145e942008-12-05 18:58:34 -0800279static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700280{
281 struct irq_pin_list *entry;
282 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700283
284 spin_lock_irqsave(&ioapic_lock, flags);
285 entry = cfg->irq_2_pin;
286 for (;;) {
287 unsigned int reg;
288 int pin;
289
290 if (!entry)
291 break;
292 pin = entry->pin;
293 reg = io_apic_read(entry->apic, 0x10 + pin*2);
294 /* Is the remote IRR bit set? */
295 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
296 spin_unlock_irqrestore(&ioapic_lock, flags);
297 return true;
298 }
299 if (!entry->next)
300 break;
301 entry = entry->next;
302 }
303 spin_unlock_irqrestore(&ioapic_lock, flags);
304
305 return false;
306}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700307
Andi Kleencf4c6a22006-09-26 10:52:30 +0200308union entry_union {
309 struct { u32 w1, w2; };
310 struct IO_APIC_route_entry entry;
311};
312
313static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
314{
315 union entry_union eu;
316 unsigned long flags;
317 spin_lock_irqsave(&ioapic_lock, flags);
318 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
319 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
320 spin_unlock_irqrestore(&ioapic_lock, flags);
321 return eu.entry;
322}
323
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800324/*
325 * When we write a new IO APIC routing entry, we need to write the high
326 * word first! If the mask bit in the low word is clear, we will enable
327 * the interrupt, and we need to make sure the entry is fully populated
328 * before that happens.
329 */
Andi Kleend15512f2006-12-07 02:14:07 +0100330static void
331__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
332{
333 union entry_union eu;
334 eu.entry = e;
335 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
336 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
337}
338
Andi Kleencf4c6a22006-09-26 10:52:30 +0200339static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
340{
341 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200342 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100343 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800344 spin_unlock_irqrestore(&ioapic_lock, flags);
345}
346
347/*
348 * When we mask an IO APIC routing entry, we need to write the low
349 * word first, in order to set the mask bit before we change the
350 * high bits!
351 */
352static void ioapic_mask_entry(int apic, int pin)
353{
354 unsigned long flags;
355 union entry_union eu = { .entry.mask = 1 };
356
357 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200358 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
359 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
360 spin_unlock_irqrestore(&ioapic_lock, flags);
361}
362
Yinghai Lu497c9a12008-08-19 20:50:28 -0700363#ifdef CONFIG_SMP
Yinghai Lu3145e942008-12-05 18:58:34 -0800364static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700365{
366 int apic, pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700367 struct irq_pin_list *entry;
Yinghai Lu3145e942008-12-05 18:58:34 -0800368 u8 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700369
Yinghai Lu497c9a12008-08-19 20:50:28 -0700370 entry = cfg->irq_2_pin;
371 for (;;) {
372 unsigned int reg;
373
374 if (!entry)
375 break;
376
377 apic = entry->apic;
378 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200379#ifdef CONFIG_INTR_REMAP
380 /*
381 * With interrupt-remapping, destination information comes
382 * from interrupt-remapping table entry.
383 */
384 if (!irq_remapped(irq))
385 io_apic_write(apic, 0x11 + pin*2, dest);
386#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700387 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200388#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700389 reg = io_apic_read(apic, 0x10 + pin*2);
390 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
391 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200392 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700393 if (!entry->next)
394 break;
395 entry = entry->next;
396 }
397}
Yinghai Luefa25592008-08-19 20:50:36 -0700398
Yinghai Lu3145e942008-12-05 18:58:34 -0800399static int assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask);
Yinghai Luefa25592008-08-19 20:50:36 -0700400
Rusty Russell968ea6d2008-12-13 21:55:51 +1030401static void set_ioapic_affinity_irq_desc(struct irq_desc *desc,
402 const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700403{
404 struct irq_cfg *cfg;
405 unsigned long flags;
406 unsigned int dest;
407 cpumask_t tmp;
Yinghai Lu3145e942008-12-05 18:58:34 -0800408 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700409
Rusty Russell0de26522008-12-13 21:20:26 +1030410 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -0700411 return;
412
Yinghai Lu3145e942008-12-05 18:58:34 -0800413 irq = desc->irq;
414 cfg = desc->chip_data;
Rusty Russell968ea6d2008-12-13 21:55:51 +1030415 if (assign_irq_vector(irq, cfg, *mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -0700416 return;
417
Rusty Russell968ea6d2008-12-13 21:55:51 +1030418 set_extra_move_desc(desc, *mask);
Yinghai Lu3145e942008-12-05 18:58:34 -0800419
Rusty Russell0de26522008-12-13 21:20:26 +1030420 cpumask_and(&tmp, &cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700421 dest = cpu_mask_to_apicid(tmp);
422 /*
423 * Only the high 8 bits are valid.
424 */
425 dest = SET_APIC_LOGICAL_ID(dest);
426
427 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800428 __target_IO_APIC_irq(irq, dest, cfg);
Rusty Russell0de26522008-12-13 21:20:26 +1030429 cpumask_copy(&desc->affinity, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700430 spin_unlock_irqrestore(&ioapic_lock, flags);
431}
Yinghai Lu3145e942008-12-05 18:58:34 -0800432
Rusty Russell968ea6d2008-12-13 21:55:51 +1030433static void set_ioapic_affinity_irq(unsigned int irq,
434 const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800435{
436 struct irq_desc *desc;
437
438 desc = irq_to_desc(irq);
439
440 set_ioapic_affinity_irq_desc(desc, mask);
441}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700442#endif /* CONFIG_SMP */
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
445 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
446 * shared ISA-space IRQs, so we have to support them. We are super
447 * fast in the common case, and fast for shared ISA-space IRQs.
448 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800449static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700451 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Yinghai Lu0f978f42008-08-19 20:50:26 -0700453 entry = cfg->irq_2_pin;
454 if (!entry) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800455 entry = get_one_free_irq_2_pin(cpu);
456 if (!entry) {
457 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
458 apic, pin);
459 return;
460 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700461 cfg->irq_2_pin = entry;
462 entry->apic = apic;
463 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700464 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700466
467 while (entry->next) {
468 /* not again, please */
469 if (entry->apic == apic && entry->pin == pin)
470 return;
471
472 entry = entry->next;
473 }
474
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800475 entry->next = get_one_free_irq_2_pin(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700476 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 entry->apic = apic;
478 entry->pin = pin;
479}
480
481/*
482 * Reroute an IRQ to a different pin.
483 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800484static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int oldapic, int oldpin,
486 int newapic, int newpin)
487{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700488 struct irq_pin_list *entry = cfg->irq_2_pin;
489 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Yinghai Lu0f978f42008-08-19 20:50:26 -0700491 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (entry->apic == oldapic && entry->pin == oldpin) {
493 entry->apic = newapic;
494 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700495 replaced = 1;
496 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700498 }
499 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700501
502 /* why? call replace before add? */
503 if (!replaced)
Yinghai Lu3145e942008-12-05 18:58:34 -0800504 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Yinghai Lu3145e942008-12-05 18:58:34 -0800507static inline void io_apic_modify_irq(struct irq_cfg *cfg,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400508 int mask_and, int mask_or,
509 void (*final)(struct irq_pin_list *entry))
510{
511 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400512 struct irq_pin_list *entry;
513
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400514 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
515 unsigned int reg;
516 pin = entry->pin;
517 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
518 reg &= mask_and;
519 reg |= mask_or;
520 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
521 if (final)
522 final(entry);
523 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700524}
525
Yinghai Lu3145e942008-12-05 18:58:34 -0800526static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400527{
Yinghai Lu3145e942008-12-05 18:58:34 -0800528 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400529}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700530
531#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400532void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700533{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400534 /*
535 * Synchronize the IO-APIC and the CPU by doing
536 * a dummy read from the IO-APIC
537 */
538 struct io_apic __iomem *io_apic;
539 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700540 readl(&io_apic->data);
541}
542
Yinghai Lu3145e942008-12-05 18:58:34 -0800543static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400544{
Yinghai Lu3145e942008-12-05 18:58:34 -0800545 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400546}
547#else /* CONFIG_X86_32 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800548static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400549{
Yinghai Lu3145e942008-12-05 18:58:34 -0800550 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400551}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700552
Yinghai Lu3145e942008-12-05 18:58:34 -0800553static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400554{
Yinghai Lu3145e942008-12-05 18:58:34 -0800555 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400556 IO_APIC_REDIR_MASKED, NULL);
557}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700558
Yinghai Lu3145e942008-12-05 18:58:34 -0800559static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400560{
Yinghai Lu3145e942008-12-05 18:58:34 -0800561 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400562 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
563}
564#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700565
Yinghai Lu3145e942008-12-05 18:58:34 -0800566static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Yinghai Lu3145e942008-12-05 18:58:34 -0800568 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 unsigned long flags;
570
Yinghai Lu3145e942008-12-05 18:58:34 -0800571 BUG_ON(!cfg);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800574 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 spin_unlock_irqrestore(&ioapic_lock, flags);
576}
577
Yinghai Lu3145e942008-12-05 18:58:34 -0800578static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Yinghai Lu3145e942008-12-05 18:58:34 -0800580 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 unsigned long flags;
582
583 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800584 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 spin_unlock_irqrestore(&ioapic_lock, flags);
586}
587
Yinghai Lu3145e942008-12-05 18:58:34 -0800588static void mask_IO_APIC_irq(unsigned int irq)
589{
590 struct irq_desc *desc = irq_to_desc(irq);
591
592 mask_IO_APIC_irq_desc(desc);
593}
594static void unmask_IO_APIC_irq(unsigned int irq)
595{
596 struct irq_desc *desc = irq_to_desc(irq);
597
598 unmask_IO_APIC_irq_desc(desc);
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
602{
603 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200606 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (entry.delivery_mode == dest_SMI)
608 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /*
610 * Disable it in the IO-APIC irq-routing table:
611 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800612 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Ingo Molnar54168ed2008-08-20 09:07:45 +0200615static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 int apic, pin;
618
619 for (apic = 0; apic < nr_ioapics; apic++)
620 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
621 clear_IO_APIC_pin(apic, pin);
622}
623
Ingo Molnar54168ed2008-08-20 09:07:45 +0200624#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100625void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 unsigned int cfg;
628
629 /*
630 * Wait for idle.
631 */
632 apic_wait_icr_idle();
633 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
634 /*
635 * Send the IPI. The write to APIC_ICR fires this off.
636 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100637 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200639#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Ingo Molnar54168ed2008-08-20 09:07:45 +0200641#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
644 * specific CPU-side IRQs.
645 */
646
647#define MAX_PIRQS 8
648static int pirq_entries [MAX_PIRQS];
649static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651static int __init ioapic_pirq_setup(char *str)
652{
653 int i, max;
654 int ints[MAX_PIRQS+1];
655
656 get_options(str, ARRAY_SIZE(ints), ints);
657
658 for (i = 0; i < MAX_PIRQS; i++)
659 pirq_entries[i] = -1;
660
661 pirqs_enabled = 1;
662 apic_printk(APIC_VERBOSE, KERN_INFO
663 "PIRQ redirection, working around broken MP-BIOS.\n");
664 max = MAX_PIRQS;
665 if (ints[0] < MAX_PIRQS)
666 max = ints[0];
667
668 for (i = 0; i < max; i++) {
669 apic_printk(APIC_VERBOSE, KERN_DEBUG
670 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
671 /*
672 * PIRQs are mapped upside down, usually.
673 */
674 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
675 }
676 return 1;
677}
678
679__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200680#endif /* CONFIG_X86_32 */
681
682#ifdef CONFIG_INTR_REMAP
683/* I/O APIC RTE contents at the OS boot up */
684static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
685
686/*
687 * Saves and masks all the unmasked IO-APIC RTE's
688 */
689int save_mask_IO_APIC_setup(void)
690{
691 union IO_APIC_reg_01 reg_01;
692 unsigned long flags;
693 int apic, pin;
694
695 /*
696 * The number of IO-APIC IRQ registers (== #pins):
697 */
698 for (apic = 0; apic < nr_ioapics; apic++) {
699 spin_lock_irqsave(&ioapic_lock, flags);
700 reg_01.raw = io_apic_read(apic, 1);
701 spin_unlock_irqrestore(&ioapic_lock, flags);
702 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
703 }
704
705 for (apic = 0; apic < nr_ioapics; apic++) {
706 early_ioapic_entries[apic] =
707 kzalloc(sizeof(struct IO_APIC_route_entry) *
708 nr_ioapic_registers[apic], GFP_KERNEL);
709 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400710 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200711 }
712
713 for (apic = 0; apic < nr_ioapics; apic++)
714 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
715 struct IO_APIC_route_entry entry;
716
717 entry = early_ioapic_entries[apic][pin] =
718 ioapic_read_entry(apic, pin);
719 if (!entry.mask) {
720 entry.mask = 1;
721 ioapic_write_entry(apic, pin, entry);
722 }
723 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400724
Ingo Molnar54168ed2008-08-20 09:07:45 +0200725 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400726
727nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400728 while (apic >= 0)
729 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400730 memset(early_ioapic_entries, 0,
731 ARRAY_SIZE(early_ioapic_entries));
732
733 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200734}
735
736void restore_IO_APIC_setup(void)
737{
738 int apic, pin;
739
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400740 for (apic = 0; apic < nr_ioapics; apic++) {
741 if (!early_ioapic_entries[apic])
742 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200743 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
744 ioapic_write_entry(apic, pin,
745 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400746 kfree(early_ioapic_entries[apic]);
747 early_ioapic_entries[apic] = NULL;
748 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200749}
750
751void reinit_intr_remapped_IO_APIC(int intr_remapping)
752{
753 /*
754 * for now plain restore of previous settings.
755 * TBD: In the case of OS enabling interrupt-remapping,
756 * IO-APIC RTE's need to be setup to point to interrupt-remapping
757 * table entries. for now, do a plain restore, and wait for
758 * the setup_IO_APIC_irqs() to do proper initialization.
759 */
760 restore_IO_APIC_setup();
761}
762#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764/*
765 * Find the IRQ entry number of a certain pin.
766 */
767static int find_irq_entry(int apic, int pin, int type)
768{
769 int i;
770
771 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400772 if (mp_irqs[i].mp_irqtype == type &&
773 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
774 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
775 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return i;
777
778 return -1;
779}
780
781/*
782 * Find the pin to which IRQ[irq] (ISA) is connected
783 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800784static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 int i;
787
788 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400789 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300791 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400792 (mp_irqs[i].mp_irqtype == type) &&
793 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400795 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
797 return -1;
798}
799
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800800static int __init find_isa_irq_apic(int irq, int type)
801{
802 int i;
803
804 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400805 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800806
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300807 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400808 (mp_irqs[i].mp_irqtype == type) &&
809 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800810 break;
811 }
812 if (i < mp_irq_entries) {
813 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200814 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400815 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800816 return apic;
817 }
818 }
819
820 return -1;
821}
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/*
824 * Find a specific PCI IRQ entry.
825 * Not an __init, possibly needed by modules
826 */
827static int pin_2_irq(int idx, int apic, int pin);
828
829int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
830{
831 int apic, i, best_guess = -1;
832
Ingo Molnar54168ed2008-08-20 09:07:45 +0200833 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
834 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400835 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200836 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -1;
838 }
839 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400840 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400843 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
844 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 break;
846
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300847 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400848 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400850 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200851 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (!(apic || IO_APIC_IRQ(irq)))
854 continue;
855
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400856 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return irq;
858 /*
859 * Use the first all-but-pin matching entry as a
860 * best-guess fuzzy result for broken mptables.
861 */
862 if (best_guess < 0)
863 best_guess = irq;
864 }
865 }
866 return best_guess;
867}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200868
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700869EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300871#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/*
873 * EISA Edge/Level control register, ELCR
874 */
875static int EISA_ELCR(unsigned int irq)
876{
Yinghai Lu99d093d2008-12-05 18:58:32 -0800877 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 unsigned int port = 0x4d0 + (irq >> 3);
879 return (inb(port) >> (irq & 7)) & 1;
880 }
881 apic_printk(APIC_VERBOSE, KERN_INFO
882 "Broken MPtable reports ISA irq %d\n", irq);
883 return 0;
884}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200885
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300886#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300888/* ISA interrupts are always polarity zero edge triggered,
889 * when listed as conforming in the MP table. */
890
891#define default_ISA_trigger(idx) (0)
892#define default_ISA_polarity(idx) (0)
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894/* EISA interrupts are always polarity zero and can be edge or level
895 * trigger depending on the ELCR value. If an interrupt is listed as
896 * EISA conforming in the MP table, that means its trigger type must
897 * be read in from the ELCR */
898
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400899#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300900#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902/* PCI interrupts are always polarity one level triggered,
903 * when listed as conforming in the MP table. */
904
905#define default_PCI_trigger(idx) (1)
906#define default_PCI_polarity(idx) (1)
907
908/* MCA interrupts are always polarity zero level triggered,
909 * when listed as conforming in the MP table. */
910
911#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300912#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Shaohua Li61fd47e2007-11-17 01:05:28 -0500914static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400916 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 int polarity;
918
919 /*
920 * Determine IRQ line polarity (high active or low active):
921 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200922 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200924 case 0: /* conforms, ie. bus-type dependent polarity */
925 if (test_bit(bus, mp_bus_not_pci))
926 polarity = default_ISA_polarity(idx);
927 else
928 polarity = default_PCI_polarity(idx);
929 break;
930 case 1: /* high active */
931 {
932 polarity = 0;
933 break;
934 }
935 case 2: /* reserved */
936 {
937 printk(KERN_WARNING "broken BIOS!!\n");
938 polarity = 1;
939 break;
940 }
941 case 3: /* low active */
942 {
943 polarity = 1;
944 break;
945 }
946 default: /* invalid */
947 {
948 printk(KERN_WARNING "broken BIOS!!\n");
949 polarity = 1;
950 break;
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953 return polarity;
954}
955
956static int MPBIOS_trigger(int idx)
957{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400958 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int trigger;
960
961 /*
962 * Determine IRQ trigger mode (edge or level sensitive):
963 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200964 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200966 case 0: /* conforms, ie. bus-type dependent */
967 if (test_bit(bus, mp_bus_not_pci))
968 trigger = default_ISA_trigger(idx);
969 else
970 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300971#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200972 switch (mp_bus_id_to_type[bus]) {
973 case MP_BUS_ISA: /* ISA pin */
974 {
975 /* set before the switch */
976 break;
977 }
978 case MP_BUS_EISA: /* EISA pin */
979 {
980 trigger = default_EISA_trigger(idx);
981 break;
982 }
983 case MP_BUS_PCI: /* PCI pin */
984 {
985 /* set before the switch */
986 break;
987 }
988 case MP_BUS_MCA: /* MCA pin */
989 {
990 trigger = default_MCA_trigger(idx);
991 break;
992 }
993 default:
994 {
995 printk(KERN_WARNING "broken BIOS!!\n");
996 trigger = 1;
997 break;
998 }
999 }
1000#endif
1001 break;
1002 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001003 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001004 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001005 break;
1006 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001007 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001008 {
1009 printk(KERN_WARNING "broken BIOS!!\n");
1010 trigger = 1;
1011 break;
1012 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001013 case 3: /* level */
1014 {
1015 trigger = 1;
1016 break;
1017 }
1018 default: /* invalid */
1019 {
1020 printk(KERN_WARNING "broken BIOS!!\n");
1021 trigger = 0;
1022 break;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 return trigger;
1026}
1027
1028static inline int irq_polarity(int idx)
1029{
1030 return MPBIOS_polarity(idx);
1031}
1032
1033static inline int irq_trigger(int idx)
1034{
1035 return MPBIOS_trigger(idx);
1036}
1037
Yinghai Luefa25592008-08-19 20:50:36 -07001038int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039static int pin_2_irq(int idx, int apic, int pin)
1040{
1041 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001042 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 /*
1045 * Debugging check, we are in big trouble if this message pops up!
1046 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001047 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1049
Ingo Molnar54168ed2008-08-20 09:07:45 +02001050 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001051 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001052 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001053 /*
1054 * PCI IRQs are mapped in order
1055 */
1056 i = irq = 0;
1057 while (i < apic)
1058 irq += nr_ioapic_registers[i++];
1059 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001060 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001061 * For MPS mode, so far only needed by ES7000 platform
1062 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001063 if (ioapic_renumber_irq)
1064 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
Ingo Molnar54168ed2008-08-20 09:07:45 +02001067#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 /*
1069 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1070 */
1071 if ((pin >= 16) && (pin <= 23)) {
1072 if (pirq_entries[pin-16] != -1) {
1073 if (!pirq_entries[pin-16]) {
1074 apic_printk(APIC_VERBOSE, KERN_DEBUG
1075 "disabling PIRQ%d\n", pin-16);
1076 } else {
1077 irq = pirq_entries[pin-16];
1078 apic_printk(APIC_VERBOSE, KERN_DEBUG
1079 "using PIRQ%d -> IRQ %d\n",
1080 pin-16, irq);
1081 }
1082 }
1083 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001084#endif
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 return irq;
1087}
1088
Yinghai Lu497c9a12008-08-19 20:50:28 -07001089void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001091 /* Used to the online set of cpus does not change
1092 * during assign_irq_vector.
1093 */
1094 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095}
1096
Yinghai Lu497c9a12008-08-19 20:50:28 -07001097void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001098{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001099 spin_unlock(&vector_lock);
1100}
1101
Yinghai Lu3145e942008-12-05 18:58:34 -08001102static int __assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001103{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001104 /*
1105 * NOTE! The local APIC isn't very good at handling
1106 * multiple interrupts at the same interrupt level.
1107 * As the interrupt level is determined by taking the
1108 * vector number and shifting that right by 4, we
1109 * want to spread these out a bit so that they don't
1110 * all fall in the same interrupt level.
1111 *
1112 * Also, we've got to be careful not to trash gate
1113 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1114 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001115 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1116 unsigned int old_vector;
1117 int cpu;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001118
Ingo Molnar54168ed2008-08-20 09:07:45 +02001119 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1120 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001121
Yinghai Lu3145e942008-12-05 18:58:34 -08001122 /* Only try and allocate irqs on cpus that are present */
1123 cpus_and(mask, mask, cpu_online_map);
1124
Ingo Molnar54168ed2008-08-20 09:07:45 +02001125 old_vector = cfg->vector;
1126 if (old_vector) {
1127 cpumask_t tmp;
1128 cpus_and(tmp, cfg->domain, mask);
1129 if (!cpus_empty(tmp))
1130 return 0;
1131 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001132
Ingo Molnar54168ed2008-08-20 09:07:45 +02001133 for_each_cpu_mask_nr(cpu, mask) {
1134 cpumask_t domain, new_mask;
1135 int new_cpu;
1136 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001137
Ingo Molnar54168ed2008-08-20 09:07:45 +02001138 domain = vector_allocation_domain(cpu);
1139 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001140
Ingo Molnar54168ed2008-08-20 09:07:45 +02001141 vector = current_vector;
1142 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001143next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001144 vector += 8;
1145 if (vector >= first_system_vector) {
1146 /* If we run out of vectors on large boxen, must share them. */
1147 offset = (offset + 1) % 8;
1148 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001149 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001150 if (unlikely(current_vector == vector))
1151 continue;
1152#ifdef CONFIG_X86_64
1153 if (vector == IA32_SYSCALL_VECTOR)
1154 goto next;
1155#else
1156 if (vector == SYSCALL_VECTOR)
1157 goto next;
1158#endif
1159 for_each_cpu_mask_nr(new_cpu, new_mask)
1160 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1161 goto next;
1162 /* Found one! */
1163 current_vector = vector;
1164 current_offset = offset;
1165 if (old_vector) {
1166 cfg->move_in_progress = 1;
1167 cfg->old_domain = cfg->domain;
1168 }
1169 for_each_cpu_mask_nr(new_cpu, new_mask)
1170 per_cpu(vector_irq, new_cpu)[vector] = irq;
1171 cfg->vector = vector;
1172 cfg->domain = domain;
1173 return 0;
1174 }
1175 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001176}
1177
Yinghai Lu3145e942008-12-05 18:58:34 -08001178static int assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001179{
1180 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001181 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001182
1183 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001184 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001185 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001186 return err;
1187}
1188
Yinghai Lu3145e942008-12-05 18:58:34 -08001189static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001190{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001191 cpumask_t mask;
1192 int cpu, vector;
1193
Yinghai Lu497c9a12008-08-19 20:50:28 -07001194 BUG_ON(!cfg->vector);
1195
1196 vector = cfg->vector;
1197 cpus_and(mask, cfg->domain, cpu_online_map);
1198 for_each_cpu_mask_nr(cpu, mask)
1199 per_cpu(vector_irq, cpu)[vector] = -1;
1200
1201 cfg->vector = 0;
1202 cpus_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001203
1204 if (likely(!cfg->move_in_progress))
1205 return;
1206 cpus_and(mask, cfg->old_domain, cpu_online_map);
1207 for_each_cpu_mask_nr(cpu, mask) {
1208 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1209 vector++) {
1210 if (per_cpu(vector_irq, cpu)[vector] != irq)
1211 continue;
1212 per_cpu(vector_irq, cpu)[vector] = -1;
1213 break;
1214 }
1215 }
1216 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001217}
1218
1219void __setup_vector_irq(int cpu)
1220{
1221 /* Initialize vector_irq on a new cpu */
1222 /* This function must be called with vector_lock held */
1223 int irq, vector;
1224 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001225 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001226
1227 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001228 for_each_irq_desc(irq, desc) {
1229 if (!desc)
1230 continue;
1231 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001232 if (!cpu_isset(cpu, cfg->domain))
1233 continue;
1234 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001235 per_cpu(vector_irq, cpu)[vector] = irq;
1236 }
1237 /* Mark the free vectors */
1238 for (vector = 0; vector < NR_VECTORS; ++vector) {
1239 irq = per_cpu(vector_irq, cpu)[vector];
1240 if (irq < 0)
1241 continue;
1242
1243 cfg = irq_cfg(irq);
1244 if (!cpu_isset(cpu, cfg->domain))
1245 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001246 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001247}
Glauber Costa3fde6902008-05-28 20:34:19 -07001248
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001249static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001250#ifdef CONFIG_INTR_REMAP
1251static struct irq_chip ir_ioapic_chip;
1252#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Ingo Molnar54168ed2008-08-20 09:07:45 +02001254#define IOAPIC_AUTO -1
1255#define IOAPIC_EDGE 0
1256#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001258#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001259static inline int IO_APIC_irq_trigger(int irq)
1260{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001261 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001262
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001263 for (apic = 0; apic < nr_ioapics; apic++) {
1264 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1265 idx = find_irq_entry(apic, pin, mp_INT);
1266 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1267 return irq_trigger(idx);
1268 }
1269 }
1270 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001271 * nonexistent IRQs are edge default
1272 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001273 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001274}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001275#else
1276static inline int IO_APIC_irq_trigger(int irq)
1277{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001278 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001279}
1280#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001281
Yinghai Lu3145e942008-12-05 18:58:34 -08001282static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Yinghai Lu199751d2008-08-19 20:50:27 -07001284
Jan Beulich6ebcc002006-06-26 13:56:46 +02001285 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001286 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001287 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001288 else
1289 desc->status &= ~IRQ_LEVEL;
1290
Ingo Molnar54168ed2008-08-20 09:07:45 +02001291#ifdef CONFIG_INTR_REMAP
1292 if (irq_remapped(irq)) {
1293 desc->status |= IRQ_MOVE_PCNTXT;
1294 if (trigger)
1295 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1296 handle_fasteoi_irq,
1297 "fasteoi");
1298 else
1299 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1300 handle_edge_irq, "edge");
1301 return;
1302 }
1303#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001304 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1305 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001306 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001307 handle_fasteoi_irq,
1308 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001309 else
Ingo Molnara460e742006-10-17 00:10:03 -07001310 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001311 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001312}
1313
1314static int setup_ioapic_entry(int apic, int irq,
1315 struct IO_APIC_route_entry *entry,
1316 unsigned int destination, int trigger,
1317 int polarity, int vector)
1318{
1319 /*
1320 * add it to the IO-APIC irq-routing table:
1321 */
1322 memset(entry,0,sizeof(*entry));
1323
Ingo Molnar54168ed2008-08-20 09:07:45 +02001324#ifdef CONFIG_INTR_REMAP
1325 if (intr_remapping_enabled) {
1326 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1327 struct irte irte;
1328 struct IR_IO_APIC_route_entry *ir_entry =
1329 (struct IR_IO_APIC_route_entry *) entry;
1330 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001331
Ingo Molnar54168ed2008-08-20 09:07:45 +02001332 if (!iommu)
1333 panic("No mapping iommu for ioapic %d\n", apic);
1334
1335 index = alloc_irte(iommu, irq, 1);
1336 if (index < 0)
1337 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1338
1339 memset(&irte, 0, sizeof(irte));
1340
1341 irte.present = 1;
1342 irte.dst_mode = INT_DEST_MODE;
1343 irte.trigger_mode = trigger;
1344 irte.dlvry_mode = INT_DELIVERY_MODE;
1345 irte.vector = vector;
1346 irte.dest_id = IRTE_DEST(destination);
1347
1348 modify_irte(irq, &irte);
1349
1350 ir_entry->index2 = (index >> 15) & 0x1;
1351 ir_entry->zero = 0;
1352 ir_entry->format = 1;
1353 ir_entry->index = (index & 0x7fff);
1354 } else
1355#endif
1356 {
1357 entry->delivery_mode = INT_DELIVERY_MODE;
1358 entry->dest_mode = INT_DEST_MODE;
1359 entry->dest = destination;
1360 }
1361
1362 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001363 entry->trigger = trigger;
1364 entry->polarity = polarity;
1365 entry->vector = vector;
1366
1367 /* Mask level triggered irqs.
1368 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1369 */
1370 if (trigger)
1371 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001372 return 0;
1373}
1374
Yinghai Lu3145e942008-12-05 18:58:34 -08001375static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001376 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001377{
1378 struct irq_cfg *cfg;
1379 struct IO_APIC_route_entry entry;
1380 cpumask_t mask;
1381
1382 if (!IO_APIC_IRQ(irq))
1383 return;
1384
Yinghai Lu3145e942008-12-05 18:58:34 -08001385 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001386
1387 mask = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08001388 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001389 return;
1390
1391 cpus_and(mask, cfg->domain, mask);
1392
1393 apic_printk(APIC_VERBOSE,KERN_DEBUG
1394 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1395 "IRQ %d Mode:%i Active:%i)\n",
1396 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1397 irq, trigger, polarity);
1398
1399
1400 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1401 cpu_mask_to_apicid(mask), trigger, polarity,
1402 cfg->vector)) {
1403 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1404 mp_ioapics[apic].mp_apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001405 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001406 return;
1407 }
1408
Yinghai Lu3145e942008-12-05 18:58:34 -08001409 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001410 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001411 disable_8259A_irq(irq);
1412
1413 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
1416static void __init setup_IO_APIC_irqs(void)
1417{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001418 int apic, pin, idx, irq;
1419 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001420 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001421 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001422 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
1424 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1425
1426 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001427 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001429 idx = find_irq_entry(apic, pin, mp_INT);
1430 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001431 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001432 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001433 apic_printk(APIC_VERBOSE,
1434 KERN_DEBUG " %d-%d",
1435 mp_ioapics[apic].mp_apicid,
1436 pin);
1437 } else
1438 apic_printk(APIC_VERBOSE, " %d-%d",
1439 mp_ioapics[apic].mp_apicid,
1440 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001441 continue;
1442 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001443 if (notcon) {
1444 apic_printk(APIC_VERBOSE,
1445 " (apicid-pin) not connected\n");
1446 notcon = 0;
1447 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001448
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001449 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001450#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001451 if (multi_timer_check(apic, irq))
1452 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001453#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001454 desc = irq_to_desc_alloc_cpu(irq, cpu);
1455 if (!desc) {
1456 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1457 continue;
1458 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001459 cfg = desc->chip_data;
1460 add_pin_to_irq_cpu(cfg, cpu, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001461
Yinghai Lu3145e942008-12-05 18:58:34 -08001462 setup_IO_APIC_irq(apic, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001463 irq_trigger(idx), irq_polarity(idx));
1464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001467 if (notcon)
1468 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001469 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
1472/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001473 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001475static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1476 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Ingo Molnar54168ed2008-08-20 09:07:45 +02001480#ifdef CONFIG_INTR_REMAP
1481 if (intr_remapping_enabled)
1482 return;
1483#endif
1484
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001485 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 /*
1488 * We use logical delivery to get the timer IRQ
1489 * to the first CPU.
1490 */
1491 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001492 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001493 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 entry.delivery_mode = INT_DELIVERY_MODE;
1495 entry.polarity = 0;
1496 entry.trigger = 0;
1497 entry.vector = vector;
1498
1499 /*
1500 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001501 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001503 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /*
1506 * Add it to the IO-APIC irq-routing table:
1507 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001508 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001511
1512__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 int apic, i;
1515 union IO_APIC_reg_00 reg_00;
1516 union IO_APIC_reg_01 reg_01;
1517 union IO_APIC_reg_02 reg_02;
1518 union IO_APIC_reg_03 reg_03;
1519 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001520 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001521 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001522 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 if (apic_verbosity == APIC_QUIET)
1525 return;
1526
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001527 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 for (i = 0; i < nr_ioapics; i++)
1529 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001530 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 /*
1533 * We are a bit conservative about what we expect. We have to
1534 * know about every hardware change ASAP.
1535 */
1536 printk(KERN_INFO "testing the IO APIC.......................\n");
1537
1538 for (apic = 0; apic < nr_ioapics; apic++) {
1539
1540 spin_lock_irqsave(&ioapic_lock, flags);
1541 reg_00.raw = io_apic_read(apic, 0);
1542 reg_01.raw = io_apic_read(apic, 1);
1543 if (reg_01.bits.version >= 0x10)
1544 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001545 if (reg_01.bits.version >= 0x20)
1546 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 spin_unlock_irqrestore(&ioapic_lock, flags);
1548
Ingo Molnar54168ed2008-08-20 09:07:45 +02001549 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001550 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1552 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1553 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1554 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Ingo Molnar54168ed2008-08-20 09:07:45 +02001556 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1560 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 /*
1563 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1564 * but the value of reg_02 is read as the previous read register
1565 * value, so ignore it if reg_02 == reg_01.
1566 */
1567 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1568 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1569 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571
1572 /*
1573 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1574 * or reg_03, but the value of reg_0[23] is read as the previous read
1575 * register value, so ignore it if reg_03 == reg_0[12].
1576 */
1577 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1578 reg_03.raw != reg_01.raw) {
1579 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1580 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 }
1582
1583 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1584
Yinghai Lud83e94a2008-08-19 20:50:33 -07001585 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1586 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 for (i = 0; i <= reg_01.bits.entries; i++) {
1589 struct IO_APIC_route_entry entry;
1590
Andi Kleencf4c6a22006-09-26 10:52:30 +02001591 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Ingo Molnar54168ed2008-08-20 09:07:45 +02001593 printk(KERN_DEBUG " %02x %03X ",
1594 i,
1595 entry.dest
1596 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1599 entry.mask,
1600 entry.trigger,
1601 entry.irr,
1602 entry.polarity,
1603 entry.delivery_status,
1604 entry.dest_mode,
1605 entry.delivery_mode,
1606 entry.vector
1607 );
1608 }
1609 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001611 for_each_irq_desc(irq, desc) {
1612 struct irq_pin_list *entry;
1613
1614 if (!desc)
1615 continue;
1616 cfg = desc->chip_data;
1617 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001618 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001620 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 for (;;) {
1622 printk("-> %d:%d", entry->apic, entry->pin);
1623 if (!entry->next)
1624 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001625 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627 printk("\n");
1628 }
1629
1630 printk(KERN_INFO ".................................... done.\n");
1631
1632 return;
1633}
1634
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001635__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
1637 unsigned int v;
1638 int i, j;
1639
1640 if (apic_verbosity == APIC_QUIET)
1641 return;
1642
1643 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1644 for (i = 0; i < 8; i++) {
1645 v = apic_read(base + i*0x10);
1646 for (j = 0; j < 32; j++) {
1647 if (v & (1<<j))
1648 printk("1");
1649 else
1650 printk("0");
1651 }
1652 printk("\n");
1653 }
1654}
1655
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001656__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001659 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 if (apic_verbosity == APIC_QUIET)
1662 return;
1663
1664 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1665 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001666 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001667 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 v = apic_read(APIC_LVR);
1669 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1670 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001671 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 v = apic_read(APIC_TASKPRI);
1674 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1675
Ingo Molnar54168ed2008-08-20 09:07:45 +02001676 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001677 if (!APIC_XAPIC(ver)) {
1678 v = apic_read(APIC_ARBPRI);
1679 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1680 v & APIC_ARBPRI_MASK);
1681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 v = apic_read(APIC_PROCPRI);
1683 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1684 }
1685
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001686 /*
1687 * Remote read supported only in the 82489DX and local APIC for
1688 * Pentium processors.
1689 */
1690 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1691 v = apic_read(APIC_RRR);
1692 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1693 }
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 v = apic_read(APIC_LDR);
1696 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001697 if (!x2apic_enabled()) {
1698 v = apic_read(APIC_DFR);
1699 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 v = apic_read(APIC_SPIV);
1702 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1703
1704 printk(KERN_DEBUG "... APIC ISR field:\n");
1705 print_APIC_bitfield(APIC_ISR);
1706 printk(KERN_DEBUG "... APIC TMR field:\n");
1707 print_APIC_bitfield(APIC_TMR);
1708 printk(KERN_DEBUG "... APIC IRR field:\n");
1709 print_APIC_bitfield(APIC_IRR);
1710
Ingo Molnar54168ed2008-08-20 09:07:45 +02001711 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1712 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 v = apic_read(APIC_ESR);
1716 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1717 }
1718
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001719 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001720 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1721 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
1723 v = apic_read(APIC_LVTT);
1724 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1725
1726 if (maxlvt > 3) { /* PC is LVT#4. */
1727 v = apic_read(APIC_LVTPC);
1728 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1729 }
1730 v = apic_read(APIC_LVT0);
1731 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1732 v = apic_read(APIC_LVT1);
1733 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1734
1735 if (maxlvt > 2) { /* ERR is LVT#3. */
1736 v = apic_read(APIC_LVTERR);
1737 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1738 }
1739
1740 v = apic_read(APIC_TMICT);
1741 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1742 v = apic_read(APIC_TMCCT);
1743 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1744 v = apic_read(APIC_TDCR);
1745 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1746 printk("\n");
1747}
1748
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001749__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001751 int cpu;
1752
1753 preempt_disable();
1754 for_each_online_cpu(cpu)
1755 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1756 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001759__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 unsigned int v;
1762 unsigned long flags;
1763
1764 if (apic_verbosity == APIC_QUIET)
1765 return;
1766
1767 printk(KERN_DEBUG "\nprinting PIC contents\n");
1768
1769 spin_lock_irqsave(&i8259A_lock, flags);
1770
1771 v = inb(0xa1) << 8 | inb(0x21);
1772 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1773
1774 v = inb(0xa0) << 8 | inb(0x20);
1775 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1776
Ingo Molnar54168ed2008-08-20 09:07:45 +02001777 outb(0x0b,0xa0);
1778 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001780 outb(0x0a,0xa0);
1781 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 spin_unlock_irqrestore(&i8259A_lock, flags);
1784
1785 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1786
1787 v = inb(0x4d1) << 8 | inb(0x4d0);
1788 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1789}
1790
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001791__apicdebuginit(int) print_all_ICs(void)
1792{
1793 print_PIC();
1794 print_all_local_APICs();
1795 print_IO_APIC();
1796
1797 return 0;
1798}
1799
1800fs_initcall(print_all_ICs);
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Yinghai Luefa25592008-08-19 20:50:36 -07001803/* Where if anywhere is the i8259 connect in external int mode */
1804static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1805
Ingo Molnar54168ed2008-08-20 09:07:45 +02001806void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001809 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001810 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 unsigned long flags;
1812
Ingo Molnar54168ed2008-08-20 09:07:45 +02001813#ifdef CONFIG_X86_32
1814 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (!pirqs_enabled)
1816 for (i = 0; i < MAX_PIRQS; i++)
1817 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001818#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 /*
1821 * The number of IO-APIC IRQ registers (== #pins):
1822 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001823 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001825 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001827 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1828 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001829 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001830 int pin;
1831 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001832 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001833 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001834 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001835
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001836 /* If the interrupt line is enabled and in ExtInt mode
1837 * I have found the pin where the i8259 is connected.
1838 */
1839 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1840 ioapic_i8259.apic = apic;
1841 ioapic_i8259.pin = pin;
1842 goto found_i8259;
1843 }
1844 }
1845 }
1846 found_i8259:
1847 /* Look to see what if the MP table has reported the ExtINT */
1848 /* If we could not find the appropriate pin by looking at the ioapic
1849 * the i8259 probably is not connected the ioapic but give the
1850 * mptable a chance anyway.
1851 */
1852 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1853 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1854 /* Trust the MP table if nothing is setup in the hardware */
1855 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1856 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1857 ioapic_i8259.pin = i8259_pin;
1858 ioapic_i8259.apic = i8259_apic;
1859 }
1860 /* Complain if the MP table and the hardware disagree */
1861 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1862 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1863 {
1864 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 }
1866
1867 /*
1868 * Do not trust the IO-APIC being empty at bootup
1869 */
1870 clear_IO_APIC();
1871}
1872
1873/*
1874 * Not an __init, needed by the reboot code
1875 */
1876void disable_IO_APIC(void)
1877{
1878 /*
1879 * Clear the IO-APIC before rebooting:
1880 */
1881 clear_IO_APIC();
1882
Eric W. Biederman650927e2005-06-25 14:57:44 -07001883 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001884 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001885 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001886 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001887 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001888 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001889 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001890
1891 memset(&entry, 0, sizeof(entry));
1892 entry.mask = 0; /* Enabled */
1893 entry.trigger = 0; /* Edge */
1894 entry.irr = 0;
1895 entry.polarity = 0; /* High */
1896 entry.delivery_status = 0;
1897 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001898 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001899 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001900 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001901
1902 /*
1903 * Add it to the IO-APIC irq-routing table:
1904 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001905 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001906 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001907
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001908 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Ingo Molnar54168ed2008-08-20 09:07:45 +02001911#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912/*
1913 * function to set the IO-APIC physical IDs based on the
1914 * values stored in the MPC table.
1915 *
1916 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1917 */
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919static void __init setup_ioapic_ids_from_mpc(void)
1920{
1921 union IO_APIC_reg_00 reg_00;
1922 physid_mask_t phys_id_present_map;
1923 int apic;
1924 int i;
1925 unsigned char old_id;
1926 unsigned long flags;
1927
Yinghai Lua4dbc342008-07-25 02:14:28 -07001928 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001929 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001932 * Don't check I/O APIC IDs for xAPIC systems. They have
1933 * no meaning without the serial APIC bus.
1934 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001935 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1936 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001937 return;
1938 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 * This is broken; anything with a real cpu count has to
1940 * circumvent this idiocy regardless.
1941 */
1942 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1943
1944 /*
1945 * Set the IOAPIC ID to the value stored in the MPC table.
1946 */
1947 for (apic = 0; apic < nr_ioapics; apic++) {
1948
1949 /* Read the register 0 value */
1950 spin_lock_irqsave(&ioapic_lock, flags);
1951 reg_00.raw = io_apic_read(apic, 0);
1952 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001953
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001954 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001956 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001958 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1960 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001961 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 /*
1965 * Sanity check, is the ID really free? Every APIC in a
1966 * system must have a unique ID or we get lots of nice
1967 * 'stuck on smp_invalidate_needed IPI wait' messages.
1968 */
1969 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001970 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001972 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 for (i = 0; i < get_physical_broadcast(); i++)
1974 if (!physid_isset(i, phys_id_present_map))
1975 break;
1976 if (i >= get_physical_broadcast())
1977 panic("Max APIC ID exceeded!\n");
1978 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1979 i);
1980 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001981 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 } else {
1983 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001984 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 apic_printk(APIC_VERBOSE, "Setting %d in the "
1986 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001987 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1989 }
1990
1991
1992 /*
1993 * We need to adjust the IRQ routing table
1994 * if the ID changed.
1995 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001996 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001998 if (mp_irqs[i].mp_dstapic == old_id)
1999 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002000 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 /*
2003 * Read the right value from the MPC table and
2004 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 apic_printk(APIC_VERBOSE, KERN_INFO
2007 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002008 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002010 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002012 io_apic_write(apic, 0, reg_00.raw);
2013 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 /*
2016 * Sanity check
2017 */
2018 spin_lock_irqsave(&ioapic_lock, flags);
2019 reg_00.raw = io_apic_read(apic, 0);
2020 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002021 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 printk("could not set ID!\n");
2023 else
2024 apic_printk(APIC_VERBOSE, " ok.\n");
2025 }
2026}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002027#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002029int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002030
2031static int __init notimercheck(char *s)
2032{
2033 no_timer_check = 1;
2034 return 1;
2035}
2036__setup("no_timer_check", notimercheck);
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038/*
2039 * There is a nasty bug in some older SMP boards, their mptable lies
2040 * about the timer IRQ. We do the following to work around the situation:
2041 *
2042 * - timer IRQ defaults to IO-APIC IRQ
2043 * - if this function detects that timer IRQs are defunct, then we fall
2044 * back to ISA timer IRQs
2045 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002046static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
2048 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002049 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Zachary Amsden8542b202006-12-07 02:14:09 +01002051 if (no_timer_check)
2052 return 1;
2053
Ingo Molnar4aae0702007-12-18 18:05:58 +01002054 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 local_irq_enable();
2056 /* Let ten ticks pass... */
2057 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002058 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 /*
2061 * Expect a few ticks at least, to be sure some possible
2062 * glue logic does not lock up after one or two first
2063 * ticks in a non-ExtINT mode. Also the local APIC
2064 * might have cached one ExtINT interrupt. Finally, at
2065 * least one tick may be lost due to delays.
2066 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002067
2068 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002069 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 0;
2072}
2073
2074/*
2075 * In the SMP+IOAPIC case it might happen that there are an unspecified
2076 * number of pending IRQ events unhandled. These cases are very rare,
2077 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2078 * better to do it this way as thus we do not have to be aware of
2079 * 'pending' interrupts in the IRQ path, except at this point.
2080 */
2081/*
2082 * Edge triggered needs to resend any interrupt
2083 * that was delayed but this is now handled in the device
2084 * independent code.
2085 */
2086
2087/*
2088 * Starting up a edge-triggered IO-APIC interrupt is
2089 * nasty - we need to make sure that we get the edge.
2090 * If it is already asserted for some reason, we need
2091 * return 1 to indicate that is was pending.
2092 *
2093 * This is not complete - we should be able to fake
2094 * an edge even if it isn't on the 8259A...
2095 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002096
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002097static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
2099 int was_pending = 0;
2100 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002101 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002104 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 disable_8259A_irq(irq);
2106 if (i8259A_irq_pending(irq))
2107 was_pending = 1;
2108 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002109 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002110 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 spin_unlock_irqrestore(&ioapic_lock, flags);
2112
2113 return was_pending;
2114}
2115
Ingo Molnar54168ed2008-08-20 09:07:45 +02002116#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002117static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002119
2120 struct irq_cfg *cfg = irq_cfg(irq);
2121 unsigned long flags;
2122
2123 spin_lock_irqsave(&vector_lock, flags);
2124 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2125 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002126
2127 return 1;
2128}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002129#else
2130static int ioapic_retrigger_irq(unsigned int irq)
2131{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002132 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002133
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002134 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002135}
2136#endif
2137
2138/*
2139 * Level and edge triggered IO-APIC interrupts need different handling,
2140 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2141 * handled with the level-triggered descriptor, but that one has slightly
2142 * more overhead. Level-triggered interrupts cannot be handled with the
2143 * edge-triggered handler, without risking IRQ storms and other ugly
2144 * races.
2145 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002146
Yinghai Lu497c9a12008-08-19 20:50:28 -07002147#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002148
2149#ifdef CONFIG_INTR_REMAP
2150static void ir_irq_migration(struct work_struct *work);
2151
2152static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2153
2154/*
2155 * Migrate the IO-APIC irq in the presence of intr-remapping.
2156 *
2157 * For edge triggered, irq migration is a simple atomic update(of vector
2158 * and cpu destination) of IRTE and flush the hardware cache.
2159 *
2160 * For level triggered, we need to modify the io-apic RTE aswell with the update
2161 * vector information, along with modifying IRTE with vector and destination.
2162 * So irq migration for level triggered is little bit more complex compared to
2163 * edge triggered migration. But the good news is, we use the same algorithm
2164 * for level triggered migration as we have today, only difference being,
2165 * we now initiate the irq migration from process context instead of the
2166 * interrupt context.
2167 *
2168 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2169 * suppression) to the IO-APIC, level triggered irq migration will also be
2170 * as simple as edge triggered migration and we can do the irq migration
2171 * with a simple atomic update to IO-APIC RTE.
2172 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002173static void migrate_ioapic_irq_desc(struct irq_desc *desc, cpumask_t mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002174{
2175 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002176 cpumask_t tmp, cleanup_mask;
2177 struct irte irte;
2178 int modify_ioapic_rte;
2179 unsigned int dest;
2180 unsigned long flags;
Yinghai Lu3145e942008-12-05 18:58:34 -08002181 unsigned int irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002182
2183 cpus_and(tmp, mask, cpu_online_map);
2184 if (cpus_empty(tmp))
2185 return;
2186
Yinghai Lu3145e942008-12-05 18:58:34 -08002187 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002188 if (get_irte(irq, &irte))
2189 return;
2190
Yinghai Lu3145e942008-12-05 18:58:34 -08002191 cfg = desc->chip_data;
2192 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002193 return;
2194
Yinghai Lu3145e942008-12-05 18:58:34 -08002195 set_extra_move_desc(desc, mask);
2196
Ingo Molnar54168ed2008-08-20 09:07:45 +02002197 cpus_and(tmp, cfg->domain, mask);
2198 dest = cpu_mask_to_apicid(tmp);
2199
Ingo Molnar54168ed2008-08-20 09:07:45 +02002200 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2201 if (modify_ioapic_rte) {
2202 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08002203 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002204 spin_unlock_irqrestore(&ioapic_lock, flags);
2205 }
2206
2207 irte.vector = cfg->vector;
2208 irte.dest_id = IRTE_DEST(dest);
2209
2210 /*
2211 * Modified the IRTE and flushes the Interrupt entry cache.
2212 */
2213 modify_irte(irq, &irte);
2214
2215 if (cfg->move_in_progress) {
2216 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2217 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2218 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2219 cfg->move_in_progress = 0;
2220 }
2221
2222 desc->affinity = mask;
2223}
2224
Yinghai Lu3145e942008-12-05 18:58:34 -08002225static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002226{
2227 int ret = -1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002228 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002229
Yinghai Lu3145e942008-12-05 18:58:34 -08002230 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002231
Yinghai Lu3145e942008-12-05 18:58:34 -08002232 if (io_apic_level_ack_pending(cfg)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002233 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002234 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002235 * vector information in the IO-APIC RTE and that will confuse
2236 * the EOI broadcast performed by cpu.
2237 * So, delay the irq migration to the next instance.
2238 */
2239 schedule_delayed_work(&ir_migration_work, 1);
2240 goto unmask;
2241 }
2242
2243 /* everthing is clear. we have right of way */
Yinghai Lu3145e942008-12-05 18:58:34 -08002244 migrate_ioapic_irq_desc(desc, desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002245
2246 ret = 0;
2247 desc->status &= ~IRQ_MOVE_PENDING;
2248 cpus_clear(desc->pending_mask);
2249
2250unmask:
Yinghai Lu3145e942008-12-05 18:58:34 -08002251 unmask_IO_APIC_irq_desc(desc);
2252
Ingo Molnar54168ed2008-08-20 09:07:45 +02002253 return ret;
2254}
2255
2256static void ir_irq_migration(struct work_struct *work)
2257{
2258 unsigned int irq;
2259 struct irq_desc *desc;
2260
2261 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002262 if (!desc)
2263 continue;
2264
Ingo Molnar54168ed2008-08-20 09:07:45 +02002265 if (desc->status & IRQ_MOVE_PENDING) {
2266 unsigned long flags;
2267
2268 spin_lock_irqsave(&desc->lock, flags);
2269 if (!desc->chip->set_affinity ||
2270 !(desc->status & IRQ_MOVE_PENDING)) {
2271 desc->status &= ~IRQ_MOVE_PENDING;
2272 spin_unlock_irqrestore(&desc->lock, flags);
2273 continue;
2274 }
2275
Rusty Russell0de26522008-12-13 21:20:26 +10302276 desc->chip->set_affinity(irq, &desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002277 spin_unlock_irqrestore(&desc->lock, flags);
2278 }
2279 }
2280}
2281
2282/*
2283 * Migrates the IRQ destination in the process context.
2284 */
Rusty Russell968ea6d2008-12-13 21:55:51 +10302285static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2286 const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -08002287{
2288 if (desc->status & IRQ_LEVEL) {
2289 desc->status |= IRQ_MOVE_PENDING;
Rusty Russell968ea6d2008-12-13 21:55:51 +10302290 cpumask_copy(&desc->pending_mask, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002291 migrate_irq_remapped_level_desc(desc);
2292 return;
2293 }
2294
2295 migrate_ioapic_irq_desc(desc, mask);
2296}
Rusty Russell0de26522008-12-13 21:20:26 +10302297static void set_ir_ioapic_affinity_irq(unsigned int irq,
2298 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002299{
2300 struct irq_desc *desc = irq_to_desc(irq);
2301
Yinghai Lu3145e942008-12-05 18:58:34 -08002302 set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002303}
2304#endif
2305
Yinghai Lu497c9a12008-08-19 20:50:28 -07002306asmlinkage void smp_irq_move_cleanup_interrupt(void)
2307{
2308 unsigned vector, me;
2309 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002310#ifdef CONFIG_X86_64
2311 exit_idle();
2312#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002313 irq_enter();
2314
2315 me = smp_processor_id();
2316 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2317 unsigned int irq;
2318 struct irq_desc *desc;
2319 struct irq_cfg *cfg;
2320 irq = __get_cpu_var(vector_irq)[vector];
2321
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002322 if (irq == -1)
2323 continue;
2324
Yinghai Lu497c9a12008-08-19 20:50:28 -07002325 desc = irq_to_desc(irq);
2326 if (!desc)
2327 continue;
2328
2329 cfg = irq_cfg(irq);
2330 spin_lock(&desc->lock);
2331 if (!cfg->move_cleanup_count)
2332 goto unlock;
2333
2334 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2335 goto unlock;
2336
2337 __get_cpu_var(vector_irq)[vector] = -1;
2338 cfg->move_cleanup_count--;
2339unlock:
2340 spin_unlock(&desc->lock);
2341 }
2342
2343 irq_exit();
2344}
2345
Yinghai Lu3145e942008-12-05 18:58:34 -08002346static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002347{
Yinghai Lu3145e942008-12-05 18:58:34 -08002348 struct irq_desc *desc = *descp;
2349 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002350 unsigned vector, me;
2351
2352 if (likely(!cfg->move_in_progress))
2353 return;
2354
2355 vector = ~get_irq_regs()->orig_ax;
2356 me = smp_processor_id();
2357 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2358 cpumask_t cleanup_mask;
2359
2360 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2361 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2362 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2363 cfg->move_in_progress = 0;
2364 }
2365}
2366#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002367static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002368#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002369
Ingo Molnar54168ed2008-08-20 09:07:45 +02002370#ifdef CONFIG_INTR_REMAP
2371static void ack_x2apic_level(unsigned int irq)
2372{
2373 ack_x2APIC_irq();
2374}
2375
2376static void ack_x2apic_edge(unsigned int irq)
2377{
2378 ack_x2APIC_irq();
2379}
Yinghai Lu3145e942008-12-05 18:58:34 -08002380
Ingo Molnar54168ed2008-08-20 09:07:45 +02002381#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002382
Yinghai Lu1d025192008-08-19 20:50:34 -07002383static void ack_apic_edge(unsigned int irq)
2384{
Yinghai Lu3145e942008-12-05 18:58:34 -08002385 struct irq_desc *desc = irq_to_desc(irq);
2386
2387 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002388 move_native_irq(irq);
2389 ack_APIC_irq();
2390}
2391
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002392atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002393
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002394static void ack_apic_level(unsigned int irq)
2395{
Yinghai Lu3145e942008-12-05 18:58:34 -08002396 struct irq_desc *desc = irq_to_desc(irq);
2397
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002398#ifdef CONFIG_X86_32
2399 unsigned long v;
2400 int i;
2401#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002402 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002403 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002404
Yinghai Lu3145e942008-12-05 18:58:34 -08002405 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002406#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002407 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002408 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002409 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002410 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002411 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002412#endif
2413
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002414#ifdef CONFIG_X86_32
2415 /*
2416 * It appears there is an erratum which affects at least version 0x11
2417 * of I/O APIC (that's the 82093AA and cores integrated into various
2418 * chipsets). Under certain conditions a level-triggered interrupt is
2419 * erroneously delivered as edge-triggered one but the respective IRR
2420 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2421 * message but it will never arrive and further interrupts are blocked
2422 * from the source. The exact reason is so far unknown, but the
2423 * phenomenon was observed when two consecutive interrupt requests
2424 * from a given source get delivered to the same CPU and the source is
2425 * temporarily disabled in between.
2426 *
2427 * A workaround is to simulate an EOI message manually. We achieve it
2428 * by setting the trigger mode to edge and then to level when the edge
2429 * trigger mode gets detected in the TMR of a local APIC for a
2430 * level-triggered interrupt. We mask the source for the time of the
2431 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2432 * The idea is from Manfred Spraul. --macro
2433 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002434 cfg = desc->chip_data;
2435 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002436
2437 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2438#endif
2439
Ingo Molnar54168ed2008-08-20 09:07:45 +02002440 /*
2441 * We must acknowledge the irq before we move it or the acknowledge will
2442 * not propagate properly.
2443 */
2444 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002445
Ingo Molnar54168ed2008-08-20 09:07:45 +02002446 /* Now we can move and renable the irq */
2447 if (unlikely(do_unmask_irq)) {
2448 /* Only migrate the irq if the ack has been received.
2449 *
2450 * On rare occasions the broadcast level triggered ack gets
2451 * delayed going to ioapics, and if we reprogram the
2452 * vector while Remote IRR is still set the irq will never
2453 * fire again.
2454 *
2455 * To prevent this scenario we read the Remote IRR bit
2456 * of the ioapic. This has two effects.
2457 * - On any sane system the read of the ioapic will
2458 * flush writes (and acks) going to the ioapic from
2459 * this cpu.
2460 * - We get to see if the ACK has actually been delivered.
2461 *
2462 * Based on failed experiments of reprogramming the
2463 * ioapic entry from outside of irq context starting
2464 * with masking the ioapic entry and then polling until
2465 * Remote IRR was clear before reprogramming the
2466 * ioapic I don't trust the Remote IRR bit to be
2467 * completey accurate.
2468 *
2469 * However there appears to be no other way to plug
2470 * this race, so if the Remote IRR bit is not
2471 * accurate and is causing problems then it is a hardware bug
2472 * and you can go talk to the chipset vendor about it.
2473 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002474 cfg = desc->chip_data;
2475 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002476 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002477 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002478 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002479
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002480#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002481 if (!(v & (1 << (i & 0x1f)))) {
2482 atomic_inc(&irq_mis_count);
2483 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002484 __mask_and_edge_IO_APIC_irq(cfg);
2485 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002486 spin_unlock(&ioapic_lock);
2487 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002488#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002489}
Yinghai Lu1d025192008-08-19 20:50:34 -07002490
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002491static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002492 .name = "IO-APIC",
2493 .startup = startup_ioapic_irq,
2494 .mask = mask_IO_APIC_irq,
2495 .unmask = unmask_IO_APIC_irq,
2496 .ack = ack_apic_edge,
2497 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002498#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002499 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002500#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002501 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502};
2503
Ingo Molnar54168ed2008-08-20 09:07:45 +02002504#ifdef CONFIG_INTR_REMAP
2505static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002506 .name = "IR-IO-APIC",
2507 .startup = startup_ioapic_irq,
2508 .mask = mask_IO_APIC_irq,
2509 .unmask = unmask_IO_APIC_irq,
2510 .ack = ack_x2apic_edge,
2511 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002512#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002513 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002514#endif
2515 .retrigger = ioapic_retrigger_irq,
2516};
2517#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519static inline void init_IO_APIC_traps(void)
2520{
2521 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002522 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002523 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 /*
2526 * NOTE! The local APIC isn't very good at handling
2527 * multiple interrupts at the same interrupt level.
2528 * As the interrupt level is determined by taking the
2529 * vector number and shifting that right by 4, we
2530 * want to spread these out a bit so that they don't
2531 * all fall in the same interrupt level.
2532 *
2533 * Also, we've got to be careful not to trash gate
2534 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2535 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002536 for_each_irq_desc(irq, desc) {
2537 if (!desc)
2538 continue;
2539
2540 cfg = desc->chip_data;
2541 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 /*
2543 * Hmm.. We don't have an entry for this,
2544 * so default to an old-fashioned 8259
2545 * interrupt if we can..
2546 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002547 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002549 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002551 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553 }
2554}
2555
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002556/*
2557 * The local APIC irq-chip implementation:
2558 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002560static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 unsigned long v;
2563
2564 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002565 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002568static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002570 unsigned long v;
2571
2572 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002573 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Yinghai Lu3145e942008-12-05 18:58:34 -08002576static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002577{
2578 ack_APIC_irq();
2579}
2580
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002581static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002582 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002583 .mask = mask_lapic_irq,
2584 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002585 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586};
2587
Yinghai Lu3145e942008-12-05 18:58:34 -08002588static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002589{
Yinghai Lu08678b02008-08-19 20:50:05 -07002590 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002591 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2592 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002593}
2594
Jan Beuliche9427102008-01-30 13:31:24 +01002595static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596{
2597 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002598 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 * We put the 8259A master into AEOI mode and
2600 * unmask on all local APICs LVT0 as NMI.
2601 *
2602 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2603 * is from Maciej W. Rozycki - so we do not have to EOI from
2604 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2607
Jan Beuliche9427102008-01-30 13:31:24 +01002608 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 apic_printk(APIC_VERBOSE, " done.\n");
2611}
2612
2613/*
2614 * This looks a bit hackish but it's about the only one way of sending
2615 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2616 * not support the ExtINT mode, unfortunately. We need to send these
2617 * cycles as some i82489DX-based boards have glue logic that keeps the
2618 * 8259A interrupt line asserted until INTA. --macro
2619 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002620static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002622 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 struct IO_APIC_route_entry entry0, entry1;
2624 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002626 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002627 if (pin == -1) {
2628 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002630 }
2631 apic = find_isa_irq_apic(8, mp_INT);
2632 if (apic == -1) {
2633 WARN_ON_ONCE(1);
2634 return;
2635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Andi Kleencf4c6a22006-09-26 10:52:30 +02002637 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002638 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 memset(&entry1, 0, sizeof(entry1));
2641
2642 entry1.dest_mode = 0; /* physical delivery */
2643 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002644 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 entry1.delivery_mode = dest_ExtINT;
2646 entry1.polarity = entry0.polarity;
2647 entry1.trigger = 0;
2648 entry1.vector = 0;
2649
Andi Kleencf4c6a22006-09-26 10:52:30 +02002650 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 save_control = CMOS_READ(RTC_CONTROL);
2653 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2654 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2655 RTC_FREQ_SELECT);
2656 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2657
2658 i = 100;
2659 while (i-- > 0) {
2660 mdelay(10);
2661 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2662 i -= 10;
2663 }
2664
2665 CMOS_WRITE(save_control, RTC_CONTROL);
2666 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002667 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Andi Kleencf4c6a22006-09-26 10:52:30 +02002669 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
Yinghai Luefa25592008-08-19 20:50:36 -07002672static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002673/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002674static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002675{
2676 disable_timer_pin_1 = 1;
2677 return 0;
2678}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002679early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002680
2681int timer_through_8259 __initdata;
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683/*
2684 * This code may look a bit paranoid, but it's supposed to cooperate with
2685 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2686 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2687 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002688 *
2689 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002691static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Yinghai Lu3145e942008-12-05 18:58:34 -08002693 struct irq_desc *desc = irq_to_desc(0);
2694 struct irq_cfg *cfg = desc->chip_data;
2695 int cpu = boot_cpu_id;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002696 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002697 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002698 unsigned int ver;
2699 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002700
2701 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002702
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002703 ver = apic_read(APIC_LVR);
2704 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /*
2707 * get/set the timer IRQ vector:
2708 */
2709 disable_8259A_irq(0);
Yinghai Lu3145e942008-12-05 18:58:34 -08002710 assign_irq_vector(0, cfg, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002713 * As IRQ0 is to be enabled in the 8259A, the virtual
2714 * wire has to be disabled in the local APIC. Also
2715 * timer interrupts need to be acknowledged manually in
2716 * the 8259A for the i82489DX when using the NMI
2717 * watchdog as that APIC treats NMIs as level-triggered.
2718 * The AEOI mode will finish them in the 8259A
2719 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002721 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002723#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002724 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002727 pin1 = find_isa_irq_pin(0, mp_INT);
2728 apic1 = find_isa_irq_apic(0, mp_INT);
2729 pin2 = ioapic_i8259.pin;
2730 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002732 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2733 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002734 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002736 /*
2737 * Some BIOS writers are clueless and report the ExtINTA
2738 * I/O APIC input from the cascaded 8259A as the timer
2739 * interrupt input. So just in case, if only one pin
2740 * was found above, try it both directly and through the
2741 * 8259A.
2742 */
2743 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002744#ifdef CONFIG_INTR_REMAP
2745 if (intr_remapping_enabled)
2746 panic("BIOS bug: timer not connected to IO-APIC");
2747#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002748 pin1 = pin2;
2749 apic1 = apic2;
2750 no_pin1 = 1;
2751 } else if (pin2 == -1) {
2752 pin2 = pin1;
2753 apic2 = apic1;
2754 }
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 if (pin1 != -1) {
2757 /*
2758 * Ok, does IRQ0 through the IOAPIC work?
2759 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002760 if (no_pin1) {
Yinghai Lu3145e942008-12-05 18:58:34 -08002761 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002762 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002763 }
Yinghai Lu3145e942008-12-05 18:58:34 -08002764 unmask_IO_APIC_irq_desc(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (timer_irq_works()) {
2766 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 setup_nmi();
2768 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002770 if (disable_timer_pin_1 > 0)
2771 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002772 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002774#ifdef CONFIG_INTR_REMAP
2775 if (intr_remapping_enabled)
2776 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2777#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002778 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002779 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002780 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2781 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002783 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2784 "(IRQ0) through the 8259A ...\n");
2785 apic_printk(APIC_QUIET, KERN_INFO
2786 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 /*
2788 * legacy devices should be connected to IO APIC #0
2789 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002790 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002791 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Yinghai Lu3145e942008-12-05 18:58:34 -08002792 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002793 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002795 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002796 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002798 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002800 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002802 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804 /*
2805 * Cleanup, just in case ...
2806 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002807 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002808 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002809 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002813 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2814 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002815 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002817#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002818 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002819#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002821 apic_printk(APIC_QUIET, KERN_INFO
2822 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Yinghai Lu3145e942008-12-05 18:58:34 -08002824 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002825 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 enable_8259A_irq(0);
2827
2828 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002829 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002830 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002832 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002833 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002834 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002836 apic_printk(APIC_QUIET, KERN_INFO
2837 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 init_8259A(0);
2840 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002841 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 unlock_ExtINT_logic();
2844
2845 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002846 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002847 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 }
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002849 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002851 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002852out:
2853 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
2856/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002857 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2858 * to devices. However there may be an I/O APIC pin available for
2859 * this interrupt regardless. The pin may be left unconnected, but
2860 * typically it will be reused as an ExtINT cascade interrupt for
2861 * the master 8259A. In the MPS case such a pin will normally be
2862 * reported as an ExtINT interrupt in the MP table. With ACPI
2863 * there is no provision for ExtINT interrupts, and in the absence
2864 * of an override it would be treated as an ordinary ISA I/O APIC
2865 * interrupt, that is edge-triggered and unmasked by default. We
2866 * used to do this, but it caused problems on some systems because
2867 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2868 * the same ExtINT cascade interrupt to drive the local APIC of the
2869 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2870 * the I/O APIC in all cases now. No actual device should request
2871 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 */
2873#define PIC_IRQS (1 << PIC_CASCADE_IR)
2874
2875void __init setup_IO_APIC(void)
2876{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002877
2878#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002880#else
2881 /*
2882 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2883 */
2884#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002886 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Ingo Molnar54168ed2008-08-20 09:07:45 +02002888 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002889 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002890 * Set up IO-APIC IRQ routing.
2891 */
2892#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002893 if (!acpi_ioapic)
2894 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 sync_Arb_IDs();
2897 setup_IO_APIC_irqs();
2898 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002899 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
2902/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002903 * Called after all the initialization is done. If we didnt find any
2904 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907static int __init io_apic_bug_finalize(void)
2908{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002909 if (sis_apic_bug == -1)
2910 sis_apic_bug = 0;
2911 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
2914late_initcall(io_apic_bug_finalize);
2915
2916struct sysfs_ioapic_data {
2917 struct sys_device dev;
2918 struct IO_APIC_route_entry entry[0];
2919};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002920static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Pavel Machek438510f2005-04-16 15:25:24 -07002922static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 struct IO_APIC_route_entry *entry;
2925 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 data = container_of(dev, struct sysfs_ioapic_data, dev);
2929 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002930 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2931 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
2933 return 0;
2934}
2935
2936static int ioapic_resume(struct sys_device *dev)
2937{
2938 struct IO_APIC_route_entry *entry;
2939 struct sysfs_ioapic_data *data;
2940 unsigned long flags;
2941 union IO_APIC_reg_00 reg_00;
2942 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 data = container_of(dev, struct sysfs_ioapic_data, dev);
2945 entry = data->entry;
2946
2947 spin_lock_irqsave(&ioapic_lock, flags);
2948 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002949 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2950 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 io_apic_write(dev->id, 0, reg_00.raw);
2952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002954 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002955 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 return 0;
2958}
2959
2960static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002961 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 .suspend = ioapic_suspend,
2963 .resume = ioapic_resume,
2964};
2965
2966static int __init ioapic_init_sysfs(void)
2967{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002968 struct sys_device * dev;
2969 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 error = sysdev_class_register(&ioapic_sysdev_class);
2972 if (error)
2973 return error;
2974
Ingo Molnar54168ed2008-08-20 09:07:45 +02002975 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002976 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002978 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (!mp_ioapic_data[i]) {
2980 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2981 continue;
2982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002984 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 dev->cls = &ioapic_sysdev_class;
2986 error = sysdev_register(dev);
2987 if (error) {
2988 kfree(mp_ioapic_data[i]);
2989 mp_ioapic_data[i] = NULL;
2990 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2991 continue;
2992 }
2993 }
2994
2995 return 0;
2996}
2997
2998device_initcall(ioapic_init_sysfs);
2999
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003000/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003001 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003002 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003003unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003004{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003005 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003006 unsigned int irq;
3007 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003008 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003009 struct irq_cfg *cfg_new = NULL;
3010 int cpu = boot_cpu_id;
3011 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003012
3013 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003014 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lube5d5352008-12-05 18:58:33 -08003015 for (new = irq_want; new < NR_IRQS; new++) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003016 if (platform_legacy_irq(new))
3017 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003018
3019 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3020 if (!desc_new) {
3021 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003022 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003023 }
3024 cfg_new = desc_new->chip_data;
3025
3026 if (cfg_new->vector != 0)
3027 continue;
Yinghai Lu3145e942008-12-05 18:58:34 -08003028 if (__assign_irq_vector(new, cfg_new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003029 irq = new;
3030 break;
3031 }
3032 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003033
Yinghai Lu199751d2008-08-19 20:50:27 -07003034 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003035 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003036 /* restore it, in case dynamic_irq_init clear it */
3037 if (desc_new)
3038 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003039 }
3040 return irq;
3041}
3042
Yinghai Lube5d5352008-12-05 18:58:33 -08003043static int nr_irqs_gsi = NR_IRQS_LEGACY;
Yinghai Lu199751d2008-08-19 20:50:27 -07003044int create_irq(void)
3045{
Yinghai Lube5d5352008-12-05 18:58:33 -08003046 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003047 int irq;
3048
Yinghai Lube5d5352008-12-05 18:58:33 -08003049 irq_want = nr_irqs_gsi;
3050 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003051
3052 if (irq == 0)
3053 irq = -1;
3054
3055 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003056}
3057
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003058void destroy_irq(unsigned int irq)
3059{
3060 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003061 struct irq_cfg *cfg;
3062 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003063
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003064 /* store it, in case dynamic_irq_cleanup clear it */
3065 desc = irq_to_desc(irq);
3066 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003067 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003068 /* connect back irq_cfg */
3069 if (desc)
3070 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003071
Ingo Molnar54168ed2008-08-20 09:07:45 +02003072#ifdef CONFIG_INTR_REMAP
3073 free_irte(irq);
3074#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003075 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003076 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003077 spin_unlock_irqrestore(&vector_lock, flags);
3078}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003079
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003080/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003081 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003082 */
3083#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003084static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003085{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003086 struct irq_cfg *cfg;
3087 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003088 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003089 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003090
Yinghai Lu3145e942008-12-05 18:58:34 -08003091 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003092 tmp = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08003093 err = assign_irq_vector(irq, cfg, tmp);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003094 if (err)
3095 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003096
Yinghai Lu497c9a12008-08-19 20:50:28 -07003097 cpus_and(tmp, cfg->domain, tmp);
3098 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003099
Ingo Molnar54168ed2008-08-20 09:07:45 +02003100#ifdef CONFIG_INTR_REMAP
3101 if (irq_remapped(irq)) {
3102 struct irte irte;
3103 int ir_index;
3104 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003105
Ingo Molnar54168ed2008-08-20 09:07:45 +02003106 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3107 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003108
Ingo Molnar54168ed2008-08-20 09:07:45 +02003109 memset (&irte, 0, sizeof(irte));
3110
3111 irte.present = 1;
3112 irte.dst_mode = INT_DEST_MODE;
3113 irte.trigger_mode = 0; /* edge */
3114 irte.dlvry_mode = INT_DELIVERY_MODE;
3115 irte.vector = cfg->vector;
3116 irte.dest_id = IRTE_DEST(dest);
3117
3118 modify_irte(irq, &irte);
3119
3120 msg->address_hi = MSI_ADDR_BASE_HI;
3121 msg->data = sub_handle;
3122 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3123 MSI_ADDR_IR_SHV |
3124 MSI_ADDR_IR_INDEX1(ir_index) |
3125 MSI_ADDR_IR_INDEX2(ir_index);
3126 } else
3127#endif
3128 {
3129 msg->address_hi = MSI_ADDR_BASE_HI;
3130 msg->address_lo =
3131 MSI_ADDR_BASE_LO |
3132 ((INT_DEST_MODE == 0) ?
3133 MSI_ADDR_DEST_MODE_PHYSICAL:
3134 MSI_ADDR_DEST_MODE_LOGICAL) |
3135 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3136 MSI_ADDR_REDIRECTION_CPU:
3137 MSI_ADDR_REDIRECTION_LOWPRI) |
3138 MSI_ADDR_DEST_ID(dest);
3139
3140 msg->data =
3141 MSI_DATA_TRIGGER_EDGE |
3142 MSI_DATA_LEVEL_ASSERT |
3143 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3144 MSI_DATA_DELIVERY_FIXED:
3145 MSI_DATA_DELIVERY_LOWPRI) |
3146 MSI_DATA_VECTOR(cfg->vector);
3147 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003148 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003149}
3150
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003151#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303152static void set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003153{
Yinghai Lu3145e942008-12-05 18:58:34 -08003154 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003155 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003156 struct msi_msg msg;
3157 unsigned int dest;
3158 cpumask_t tmp;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003159
Rusty Russell0de26522008-12-13 21:20:26 +10303160 if (!cpumask_intersects(mask, cpu_online_mask))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003161 return;
3162
Yinghai Lu3145e942008-12-05 18:58:34 -08003163 cfg = desc->chip_data;
Rusty Russell968ea6d2008-12-13 21:55:51 +10303164 if (assign_irq_vector(irq, cfg, *mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003165 return;
3166
Rusty Russell968ea6d2008-12-13 21:55:51 +10303167 set_extra_move_desc(desc, *mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003168
Rusty Russell0de26522008-12-13 21:20:26 +10303169 cpumask_and(&tmp, &cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003170 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003171
Yinghai Lu3145e942008-12-05 18:58:34 -08003172 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003173
3174 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003175 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003176 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3177 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3178
Yinghai Lu3145e942008-12-05 18:58:34 -08003179 write_msi_msg_desc(desc, &msg);
Rusty Russell0de26522008-12-13 21:20:26 +10303180 cpumask_copy(&desc->affinity, mask);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003181}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003182#ifdef CONFIG_INTR_REMAP
3183/*
3184 * Migrate the MSI irq to another cpumask. This migration is
3185 * done in the process context using interrupt-remapping hardware.
3186 */
Rusty Russell0de26522008-12-13 21:20:26 +10303187static void ir_set_msi_irq_affinity(unsigned int irq,
3188 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003189{
Yinghai Lu3145e942008-12-05 18:58:34 -08003190 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003191 struct irq_cfg *cfg;
3192 unsigned int dest;
3193 cpumask_t tmp, cleanup_mask;
3194 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003195
Rusty Russell0de26522008-12-13 21:20:26 +10303196 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003197 return;
3198
3199 if (get_irte(irq, &irte))
3200 return;
3201
Yinghai Lu3145e942008-12-05 18:58:34 -08003202 cfg = desc->chip_data;
Rusty Russell968ea6d2008-12-13 21:55:51 +10303203 if (assign_irq_vector(irq, cfg, *mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003204 return;
3205
Yinghai Lu3145e942008-12-05 18:58:34 -08003206 set_extra_move_desc(desc, mask);
3207
Rusty Russell0de26522008-12-13 21:20:26 +10303208 cpumask_and(&tmp, &cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003209 dest = cpu_mask_to_apicid(tmp);
3210
3211 irte.vector = cfg->vector;
3212 irte.dest_id = IRTE_DEST(dest);
3213
3214 /*
3215 * atomically update the IRTE with the new destination and vector.
3216 */
3217 modify_irte(irq, &irte);
3218
3219 /*
3220 * After this point, all the interrupts will start arriving
3221 * at the new destination. So, time to cleanup the previous
3222 * vector allocation.
3223 */
3224 if (cfg->move_in_progress) {
3225 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3226 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3227 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3228 cfg->move_in_progress = 0;
3229 }
3230
Rusty Russell0de26522008-12-13 21:20:26 +10303231 cpumask_copy(&desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003232}
Yinghai Lu3145e942008-12-05 18:58:34 -08003233
Ingo Molnar54168ed2008-08-20 09:07:45 +02003234#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003235#endif /* CONFIG_SMP */
3236
3237/*
3238 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3239 * which implement the MSI or MSI-X Capability Structure.
3240 */
3241static struct irq_chip msi_chip = {
3242 .name = "PCI-MSI",
3243 .unmask = unmask_msi_irq,
3244 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003245 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003246#ifdef CONFIG_SMP
3247 .set_affinity = set_msi_irq_affinity,
3248#endif
3249 .retrigger = ioapic_retrigger_irq,
3250};
3251
Ingo Molnar54168ed2008-08-20 09:07:45 +02003252#ifdef CONFIG_INTR_REMAP
3253static struct irq_chip msi_ir_chip = {
3254 .name = "IR-PCI-MSI",
3255 .unmask = unmask_msi_irq,
3256 .mask = mask_msi_irq,
3257 .ack = ack_x2apic_edge,
3258#ifdef CONFIG_SMP
3259 .set_affinity = ir_set_msi_irq_affinity,
3260#endif
3261 .retrigger = ioapic_retrigger_irq,
3262};
3263
3264/*
3265 * Map the PCI dev to the corresponding remapping hardware unit
3266 * and allocate 'nvec' consecutive interrupt-remapping table entries
3267 * in it.
3268 */
3269static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3270{
3271 struct intel_iommu *iommu;
3272 int index;
3273
3274 iommu = map_dev_to_ir(dev);
3275 if (!iommu) {
3276 printk(KERN_ERR
3277 "Unable to map PCI %s to iommu\n", pci_name(dev));
3278 return -ENOENT;
3279 }
3280
3281 index = alloc_irte(iommu, irq, nvec);
3282 if (index < 0) {
3283 printk(KERN_ERR
3284 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003285 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003286 return -ENOSPC;
3287 }
3288 return index;
3289}
3290#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003291
Yinghai Lu3145e942008-12-05 18:58:34 -08003292static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003293{
3294 int ret;
3295 struct msi_msg msg;
3296
3297 ret = msi_compose_msg(dev, irq, &msg);
3298 if (ret < 0)
3299 return ret;
3300
Yinghai Lu3145e942008-12-05 18:58:34 -08003301 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003302 write_msi_msg(irq, &msg);
3303
Ingo Molnar54168ed2008-08-20 09:07:45 +02003304#ifdef CONFIG_INTR_REMAP
3305 if (irq_remapped(irq)) {
3306 struct irq_desc *desc = irq_to_desc(irq);
3307 /*
3308 * irq migration in process context
3309 */
3310 desc->status |= IRQ_MOVE_PCNTXT;
3311 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3312 } else
3313#endif
3314 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003315
Yinghai Luc81bba42008-09-25 11:53:11 -07003316 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3317
Yinghai Lu1d025192008-08-19 20:50:34 -07003318 return 0;
3319}
3320
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003321int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003322{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003323 unsigned int irq;
3324 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003325 unsigned int irq_want;
3326
Yinghai Lube5d5352008-12-05 18:58:33 -08003327 irq_want = nr_irqs_gsi;
Yinghai Lu199751d2008-08-19 20:50:27 -07003328 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003329 if (irq == 0)
3330 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003331
Ingo Molnar54168ed2008-08-20 09:07:45 +02003332#ifdef CONFIG_INTR_REMAP
3333 if (!intr_remapping_enabled)
3334 goto no_ir;
3335
3336 ret = msi_alloc_irte(dev, irq, 1);
3337 if (ret < 0)
3338 goto error;
3339no_ir:
3340#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003341 ret = setup_msi_irq(dev, msidesc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003342 if (ret < 0) {
3343 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003344 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003345 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003346 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003347
3348#ifdef CONFIG_INTR_REMAP
3349error:
3350 destroy_irq(irq);
3351 return ret;
3352#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003353}
3354
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003355int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3356{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003357 unsigned int irq;
3358 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003359 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003360 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003361
Ingo Molnar54168ed2008-08-20 09:07:45 +02003362#ifdef CONFIG_INTR_REMAP
3363 struct intel_iommu *iommu = 0;
3364 int index = 0;
3365#endif
3366
Yinghai Lube5d5352008-12-05 18:58:33 -08003367 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003368 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003369 list_for_each_entry(msidesc, &dev->msi_list, list) {
3370 irq = create_irq_nr(irq_want);
Yinghai Lube5d5352008-12-05 18:58:33 -08003371 irq_want++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003372 if (irq == 0)
3373 return -1;
3374#ifdef CONFIG_INTR_REMAP
3375 if (!intr_remapping_enabled)
3376 goto no_ir;
3377
3378 if (!sub_handle) {
3379 /*
3380 * allocate the consecutive block of IRTE's
3381 * for 'nvec'
3382 */
3383 index = msi_alloc_irte(dev, irq, nvec);
3384 if (index < 0) {
3385 ret = index;
3386 goto error;
3387 }
3388 } else {
3389 iommu = map_dev_to_ir(dev);
3390 if (!iommu) {
3391 ret = -ENOENT;
3392 goto error;
3393 }
3394 /*
3395 * setup the mapping between the irq and the IRTE
3396 * base index, the sub_handle pointing to the
3397 * appropriate interrupt remap table entry.
3398 */
3399 set_irte_irq(irq, iommu, index, sub_handle);
3400 }
3401no_ir:
3402#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003403 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003404 if (ret < 0)
3405 goto error;
3406 sub_handle++;
3407 }
3408 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003409
3410error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003411 destroy_irq(irq);
3412 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003413}
3414
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003415void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003416{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003417 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003418}
3419
Ingo Molnar54168ed2008-08-20 09:07:45 +02003420#ifdef CONFIG_DMAR
3421#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303422static void dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003423{
Yinghai Lu3145e942008-12-05 18:58:34 -08003424 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003425 struct irq_cfg *cfg;
3426 struct msi_msg msg;
3427 unsigned int dest;
3428 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003429
Rusty Russell0de26522008-12-13 21:20:26 +10303430 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003431 return;
3432
Yinghai Lu3145e942008-12-05 18:58:34 -08003433 cfg = desc->chip_data;
Rusty Russell968ea6d2008-12-13 21:55:51 +10303434 if (assign_irq_vector(irq, cfg, *mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003435 return;
3436
Rusty Russell968ea6d2008-12-13 21:55:51 +10303437 set_extra_move_desc(desc, *mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003438
Rusty Russell0de26522008-12-13 21:20:26 +10303439 cpumask_and(&tmp, &cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003440 dest = cpu_mask_to_apicid(tmp);
3441
3442 dmar_msi_read(irq, &msg);
3443
3444 msg.data &= ~MSI_DATA_VECTOR_MASK;
3445 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3446 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3447 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3448
3449 dmar_msi_write(irq, &msg);
Rusty Russell0de26522008-12-13 21:20:26 +10303450 cpumask_copy(&desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003451}
Yinghai Lu3145e942008-12-05 18:58:34 -08003452
Ingo Molnar54168ed2008-08-20 09:07:45 +02003453#endif /* CONFIG_SMP */
3454
3455struct irq_chip dmar_msi_type = {
3456 .name = "DMAR_MSI",
3457 .unmask = dmar_msi_unmask,
3458 .mask = dmar_msi_mask,
3459 .ack = ack_apic_edge,
3460#ifdef CONFIG_SMP
3461 .set_affinity = dmar_msi_set_affinity,
3462#endif
3463 .retrigger = ioapic_retrigger_irq,
3464};
3465
3466int arch_setup_dmar_msi(unsigned int irq)
3467{
3468 int ret;
3469 struct msi_msg msg;
3470
3471 ret = msi_compose_msg(NULL, irq, &msg);
3472 if (ret < 0)
3473 return ret;
3474 dmar_msi_write(irq, &msg);
3475 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3476 "edge");
3477 return 0;
3478}
3479#endif
3480
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003481#ifdef CONFIG_HPET_TIMER
3482
3483#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303484static void hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003485{
Yinghai Lu3145e942008-12-05 18:58:34 -08003486 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003487 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003488 struct msi_msg msg;
3489 unsigned int dest;
3490 cpumask_t tmp;
3491
Rusty Russell0de26522008-12-13 21:20:26 +10303492 if (!cpumask_intersects(mask, cpu_online_mask))
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003493 return;
3494
Yinghai Lu3145e942008-12-05 18:58:34 -08003495 cfg = desc->chip_data;
Rusty Russell968ea6d2008-12-13 21:55:51 +10303496 if (assign_irq_vector(irq, cfg, *mask))
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003497 return;
3498
Rusty Russell968ea6d2008-12-13 21:55:51 +10303499 set_extra_move_desc(desc, *mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003500
Rusty Russell0de26522008-12-13 21:20:26 +10303501 cpumask_and(&tmp, &cfg->domain, mask);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003502 dest = cpu_mask_to_apicid(tmp);
3503
3504 hpet_msi_read(irq, &msg);
3505
3506 msg.data &= ~MSI_DATA_VECTOR_MASK;
3507 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3508 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3509 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3510
3511 hpet_msi_write(irq, &msg);
Rusty Russell0de26522008-12-13 21:20:26 +10303512 cpumask_copy(&desc->affinity, mask);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003513}
Yinghai Lu3145e942008-12-05 18:58:34 -08003514
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003515#endif /* CONFIG_SMP */
3516
3517struct irq_chip hpet_msi_type = {
3518 .name = "HPET_MSI",
3519 .unmask = hpet_msi_unmask,
3520 .mask = hpet_msi_mask,
3521 .ack = ack_apic_edge,
3522#ifdef CONFIG_SMP
3523 .set_affinity = hpet_msi_set_affinity,
3524#endif
3525 .retrigger = ioapic_retrigger_irq,
3526};
3527
3528int arch_setup_hpet_msi(unsigned int irq)
3529{
3530 int ret;
3531 struct msi_msg msg;
3532
3533 ret = msi_compose_msg(NULL, irq, &msg);
3534 if (ret < 0)
3535 return ret;
3536
3537 hpet_msi_write(irq, &msg);
3538 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3539 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003540
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003541 return 0;
3542}
3543#endif
3544
Ingo Molnar54168ed2008-08-20 09:07:45 +02003545#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003546/*
3547 * Hypertransport interrupt support
3548 */
3549#ifdef CONFIG_HT_IRQ
3550
3551#ifdef CONFIG_SMP
3552
Yinghai Lu497c9a12008-08-19 20:50:28 -07003553static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003554{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003555 struct ht_irq_msg msg;
3556 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003557
Yinghai Lu497c9a12008-08-19 20:50:28 -07003558 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003559 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003560
Yinghai Lu497c9a12008-08-19 20:50:28 -07003561 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003562 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003563
Eric W. Biedermanec683072006-11-08 17:44:57 -08003564 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003565}
3566
Rusty Russell0de26522008-12-13 21:20:26 +10303567static void set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003568{
Yinghai Lu3145e942008-12-05 18:58:34 -08003569 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003570 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003571 unsigned int dest;
3572 cpumask_t tmp;
3573
Rusty Russell0de26522008-12-13 21:20:26 +10303574 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003575 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003576
Yinghai Lu3145e942008-12-05 18:58:34 -08003577 cfg = desc->chip_data;
Rusty Russell968ea6d2008-12-13 21:55:51 +10303578 if (assign_irq_vector(irq, cfg, *mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003579 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003580
Rusty Russell968ea6d2008-12-13 21:55:51 +10303581 set_extra_move_desc(desc, *mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003582
Rusty Russell0de26522008-12-13 21:20:26 +10303583 cpumask_and(&tmp, &cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003584 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003585
Yinghai Lu497c9a12008-08-19 20:50:28 -07003586 target_ht_irq(irq, dest, cfg->vector);
Rusty Russell0de26522008-12-13 21:20:26 +10303587 cpumask_copy(&desc->affinity, mask);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003588}
Yinghai Lu3145e942008-12-05 18:58:34 -08003589
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003590#endif
3591
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003592static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003593 .name = "PCI-HT",
3594 .mask = mask_ht_irq,
3595 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003596 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003597#ifdef CONFIG_SMP
3598 .set_affinity = set_ht_irq_affinity,
3599#endif
3600 .retrigger = ioapic_retrigger_irq,
3601};
3602
3603int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3604{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003605 struct irq_cfg *cfg;
3606 int err;
3607 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003608
Yinghai Lu3145e942008-12-05 18:58:34 -08003609 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003610 tmp = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08003611 err = assign_irq_vector(irq, cfg, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003612 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003613 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003614 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003615
Yinghai Lu497c9a12008-08-19 20:50:28 -07003616 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003617 dest = cpu_mask_to_apicid(tmp);
3618
Eric W. Biedermanec683072006-11-08 17:44:57 -08003619 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003620
Eric W. Biedermanec683072006-11-08 17:44:57 -08003621 msg.address_lo =
3622 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003623 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003624 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003625 ((INT_DEST_MODE == 0) ?
3626 HT_IRQ_LOW_DM_PHYSICAL :
3627 HT_IRQ_LOW_DM_LOGICAL) |
3628 HT_IRQ_LOW_RQEOI_EDGE |
3629 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3630 HT_IRQ_LOW_MT_FIXED :
3631 HT_IRQ_LOW_MT_ARBITRATED) |
3632 HT_IRQ_LOW_IRQ_MASKED;
3633
Eric W. Biedermanec683072006-11-08 17:44:57 -08003634 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003635
Ingo Molnara460e742006-10-17 00:10:03 -07003636 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3637 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003638
3639 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003640 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003641 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003642}
3643#endif /* CONFIG_HT_IRQ */
3644
Dean Nelson4173a0e2008-10-02 12:18:21 -05003645#ifdef CONFIG_X86_64
3646/*
3647 * Re-target the irq to the specified CPU and enable the specified MMR located
3648 * on the specified blade to allow the sending of MSIs to the specified CPU.
3649 */
3650int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3651 unsigned long mmr_offset)
3652{
3653 const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
3654 struct irq_cfg *cfg;
3655 int mmr_pnode;
3656 unsigned long mmr_value;
3657 struct uv_IO_APIC_route_entry *entry;
3658 unsigned long flags;
3659 int err;
3660
Yinghai Lu3145e942008-12-05 18:58:34 -08003661 cfg = irq_cfg(irq);
3662
3663 err = assign_irq_vector(irq, cfg, *eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003664 if (err != 0)
3665 return err;
3666
3667 spin_lock_irqsave(&vector_lock, flags);
3668 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3669 irq_name);
3670 spin_unlock_irqrestore(&vector_lock, flags);
3671
Dean Nelson4173a0e2008-10-02 12:18:21 -05003672 mmr_value = 0;
3673 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3674 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3675
3676 entry->vector = cfg->vector;
3677 entry->delivery_mode = INT_DELIVERY_MODE;
3678 entry->dest_mode = INT_DEST_MODE;
3679 entry->polarity = 0;
3680 entry->trigger = 0;
3681 entry->mask = 0;
3682 entry->dest = cpu_mask_to_apicid(*eligible_cpu);
3683
3684 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3685 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3686
3687 return irq;
3688}
3689
3690/*
3691 * Disable the specified MMR located on the specified blade so that MSIs are
3692 * longer allowed to be sent.
3693 */
3694void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3695{
3696 unsigned long mmr_value;
3697 struct uv_IO_APIC_route_entry *entry;
3698 int mmr_pnode;
3699
3700 mmr_value = 0;
3701 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3702 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3703
3704 entry->mask = 1;
3705
3706 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3707 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3708}
3709#endif /* CONFIG_X86_64 */
3710
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003711int __init io_apic_get_redir_entries (int ioapic)
3712{
3713 union IO_APIC_reg_01 reg_01;
3714 unsigned long flags;
3715
3716 spin_lock_irqsave(&ioapic_lock, flags);
3717 reg_01.raw = io_apic_read(ioapic, 1);
3718 spin_unlock_irqrestore(&ioapic_lock, flags);
3719
3720 return reg_01.bits.entries;
3721}
3722
Yinghai Lube5d5352008-12-05 18:58:33 -08003723void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003724{
Yinghai Lube5d5352008-12-05 18:58:33 -08003725 int idx;
3726 int nr = 0;
3727
3728 for (idx = 0; idx < nr_ioapics; idx++)
3729 nr += io_apic_get_redir_entries(idx) + 1;
3730
3731 if (nr > nr_irqs_gsi)
3732 nr_irqs_gsi = nr;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003733}
3734
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003736 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 -------------------------------------------------------------------------- */
3738
Len Brown888ba6c2005-08-24 12:07:20 -04003739#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740
Ingo Molnar54168ed2008-08-20 09:07:45 +02003741#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003742int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743{
3744 union IO_APIC_reg_00 reg_00;
3745 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3746 physid_mask_t tmp;
3747 unsigned long flags;
3748 int i = 0;
3749
3750 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003751 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3752 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003754 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3756 * advantage of new APIC bus architecture.
3757 */
3758
3759 if (physids_empty(apic_id_map))
3760 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3761
3762 spin_lock_irqsave(&ioapic_lock, flags);
3763 reg_00.raw = io_apic_read(ioapic, 0);
3764 spin_unlock_irqrestore(&ioapic_lock, flags);
3765
3766 if (apic_id >= get_physical_broadcast()) {
3767 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3768 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3769 apic_id = reg_00.bits.ID;
3770 }
3771
3772 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003773 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 * 'stuck on smp_invalidate_needed IPI wait' messages.
3775 */
3776 if (check_apicid_used(apic_id_map, apic_id)) {
3777
3778 for (i = 0; i < get_physical_broadcast(); i++) {
3779 if (!check_apicid_used(apic_id_map, i))
3780 break;
3781 }
3782
3783 if (i == get_physical_broadcast())
3784 panic("Max apic_id exceeded!\n");
3785
3786 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3787 "trying %d\n", ioapic, apic_id, i);
3788
3789 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
3792 tmp = apicid_to_cpu_present(apic_id);
3793 physids_or(apic_id_map, apic_id_map, tmp);
3794
3795 if (reg_00.bits.ID != apic_id) {
3796 reg_00.bits.ID = apic_id;
3797
3798 spin_lock_irqsave(&ioapic_lock, flags);
3799 io_apic_write(ioapic, 0, reg_00.raw);
3800 reg_00.raw = io_apic_read(ioapic, 0);
3801 spin_unlock_irqrestore(&ioapic_lock, flags);
3802
3803 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003804 if (reg_00.bits.ID != apic_id) {
3805 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3806 return -1;
3807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 }
3809
3810 apic_printk(APIC_VERBOSE, KERN_INFO
3811 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3812
3813 return apic_id;
3814}
3815
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003816int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817{
3818 union IO_APIC_reg_01 reg_01;
3819 unsigned long flags;
3820
3821 spin_lock_irqsave(&ioapic_lock, flags);
3822 reg_01.raw = io_apic_read(ioapic, 1);
3823 spin_unlock_irqrestore(&ioapic_lock, flags);
3824
3825 return reg_01.bits.version;
3826}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003827#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828
Ingo Molnar54168ed2008-08-20 09:07:45 +02003829int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003831 struct irq_desc *desc;
3832 struct irq_cfg *cfg;
3833 int cpu = boot_cpu_id;
3834
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003836 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 ioapic);
3838 return -EINVAL;
3839 }
3840
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003841 desc = irq_to_desc_alloc_cpu(irq, cpu);
3842 if (!desc) {
3843 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3844 return 0;
3845 }
3846
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 * IRQs < 16 are already in the irq_2_pin[] map
3849 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003850 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003851 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003852 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
Yinghai Lu3145e942008-12-05 18:58:34 -08003855 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856
3857 return 0;
3858}
3859
Ingo Molnar54168ed2008-08-20 09:07:45 +02003860
Shaohua Li61fd47e2007-11-17 01:05:28 -05003861int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3862{
3863 int i;
3864
3865 if (skip_ioapic_setup)
3866 return -1;
3867
3868 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003869 if (mp_irqs[i].mp_irqtype == mp_INT &&
3870 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003871 break;
3872 if (i >= mp_irq_entries)
3873 return -1;
3874
3875 *trigger = irq_trigger(i);
3876 *polarity = irq_polarity(i);
3877 return 0;
3878}
3879
Len Brown888ba6c2005-08-24 12:07:20 -04003880#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003881
Yinghai Lu497c9a12008-08-19 20:50:28 -07003882/*
3883 * This function currently is only a helper for the i386 smp boot process where
3884 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3885 * so mask in all cases should simply be TARGET_CPUS
3886 */
3887#ifdef CONFIG_SMP
3888void __init setup_ioapic_dest(void)
3889{
3890 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003891 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003892 struct irq_cfg *cfg;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003893 cpumask_t mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003894
3895 if (skip_ioapic_setup == 1)
3896 return;
3897
3898 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3899 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3900 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3901 if (irq_entry == -1)
3902 continue;
3903 irq = pin_2_irq(irq_entry, ioapic, pin);
3904
3905 /* setup_IO_APIC_irqs could fail to get vector for some device
3906 * when you have too many devices, because at that time only boot
3907 * cpu is online.
3908 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003909 desc = irq_to_desc(irq);
3910 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003911 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08003912 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07003913 irq_trigger(irq_entry),
3914 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003915 continue;
3916
3917 }
3918
3919 /*
3920 * Honour affinities which have been set in early boot
3921 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003922 if (desc->status &
3923 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
3924 mask = desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003925 else
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003926 mask = TARGET_CPUS;
3927
3928#ifdef CONFIG_INTR_REMAP
3929 if (intr_remapping_enabled)
Rusty Russell968ea6d2008-12-13 21:55:51 +10303930 set_ir_ioapic_affinity_irq_desc(desc, &mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003931 else
3932#endif
Rusty Russell968ea6d2008-12-13 21:55:51 +10303933 set_ioapic_affinity_irq_desc(desc, &mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003934 }
3935
3936 }
3937}
3938#endif
3939
Ingo Molnar54168ed2008-08-20 09:07:45 +02003940#define IOAPIC_RESOURCE_NAME_SIZE 11
3941
3942static struct resource *ioapic_resources;
3943
3944static struct resource * __init ioapic_setup_resources(void)
3945{
3946 unsigned long n;
3947 struct resource *res;
3948 char *mem;
3949 int i;
3950
3951 if (nr_ioapics <= 0)
3952 return NULL;
3953
3954 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3955 n *= nr_ioapics;
3956
3957 mem = alloc_bootmem(n);
3958 res = (void *)mem;
3959
3960 if (mem != NULL) {
3961 mem += sizeof(struct resource) * nr_ioapics;
3962
3963 for (i = 0; i < nr_ioapics; i++) {
3964 res[i].name = mem;
3965 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3966 sprintf(mem, "IOAPIC %u", i);
3967 mem += IOAPIC_RESOURCE_NAME_SIZE;
3968 }
3969 }
3970
3971 ioapic_resources = res;
3972
3973 return res;
3974}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003975
Yinghai Luf3294a32008-06-27 01:41:56 -07003976void __init ioapic_init_mappings(void)
3977{
3978 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003979 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003980 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07003981
Ingo Molnar54168ed2008-08-20 09:07:45 +02003982 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003983 for (i = 0; i < nr_ioapics; i++) {
3984 if (smp_found_config) {
3985 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003986#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003987 if (!ioapic_phys) {
3988 printk(KERN_ERR
3989 "WARNING: bogus zero IO-APIC "
3990 "address found in MPTABLE, "
3991 "disabling IO/APIC support!\n");
3992 smp_found_config = 0;
3993 skip_ioapic_setup = 1;
3994 goto fake_ioapic_page;
3995 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02003996#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003997 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003998#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003999fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004000#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004001 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004002 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004003 ioapic_phys = __pa(ioapic_phys);
4004 }
4005 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004006 apic_printk(APIC_VERBOSE,
4007 "mapped IOAPIC to %08lx (%08lx)\n",
4008 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004009 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004010
Ingo Molnar54168ed2008-08-20 09:07:45 +02004011 if (ioapic_res != NULL) {
4012 ioapic_res->start = ioapic_phys;
4013 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4014 ioapic_res++;
4015 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004016 }
4017}
4018
Ingo Molnar54168ed2008-08-20 09:07:45 +02004019static int __init ioapic_insert_resources(void)
4020{
4021 int i;
4022 struct resource *r = ioapic_resources;
4023
4024 if (!r) {
4025 printk(KERN_ERR
4026 "IO APIC resources could be not be allocated.\n");
4027 return -1;
4028 }
4029
4030 for (i = 0; i < nr_ioapics; i++) {
4031 insert_resource(&iomem_resource, r);
4032 r++;
4033 }
4034
4035 return 0;
4036}
4037
4038/* Insert the IO APIC resources after PCI initialization has occured to handle
4039 * IO APICS that are mapped in on a BAR in PCI space. */
4040late_initcall(ioapic_insert_resources);