blob: a1a2e070f31afc8446c2c7186925f76daf043d55 [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;
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
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)
171{
172 struct irq_cfg *cfg;
173 struct irq_desc *desc;
174 int count;
175 int i;
176
177 cfg = irq_cfgx;
178 count = ARRAY_SIZE(irq_cfgx);
179
180 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{
236}
237
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
Yinghai Lu3145e942008-12-05 18:58:34 -0800401static void set_ioapic_affinity_irq_desc(struct irq_desc *desc, cpumask_t mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700402{
403 struct irq_cfg *cfg;
404 unsigned long flags;
405 unsigned int dest;
406 cpumask_t tmp;
Yinghai Lu3145e942008-12-05 18:58:34 -0800407 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700408
Yinghai Lu497c9a12008-08-19 20:50:28 -0700409 cpus_and(tmp, mask, cpu_online_map);
410 if (cpus_empty(tmp))
411 return;
412
Yinghai Lu3145e942008-12-05 18:58:34 -0800413 irq = desc->irq;
414 cfg = desc->chip_data;
415 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -0700416 return;
417
Yinghai Lu3145e942008-12-05 18:58:34 -0800418 set_extra_move_desc(desc, mask);
419
Yinghai Lu497c9a12008-08-19 20:50:28 -0700420 cpus_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);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200429 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
433static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
434{
435 struct irq_desc *desc;
436
437 desc = irq_to_desc(irq);
438
439 set_ioapic_affinity_irq_desc(desc, mask);
440}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700441#endif /* CONFIG_SMP */
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
444 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
445 * shared ISA-space IRQs, so we have to support them. We are super
446 * fast in the common case, and fast for shared ISA-space IRQs.
447 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800448static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700450 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Yinghai Lu0f978f42008-08-19 20:50:26 -0700452 entry = cfg->irq_2_pin;
453 if (!entry) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800454 entry = get_one_free_irq_2_pin(cpu);
455 if (!entry) {
456 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
457 apic, pin);
458 return;
459 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700460 cfg->irq_2_pin = entry;
461 entry->apic = apic;
462 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700463 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700465
466 while (entry->next) {
467 /* not again, please */
468 if (entry->apic == apic && entry->pin == pin)
469 return;
470
471 entry = entry->next;
472 }
473
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800474 entry->next = get_one_free_irq_2_pin(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700475 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 entry->apic = apic;
477 entry->pin = pin;
478}
479
480/*
481 * Reroute an IRQ to a different pin.
482 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800483static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int oldapic, int oldpin,
485 int newapic, int newpin)
486{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700487 struct irq_pin_list *entry = cfg->irq_2_pin;
488 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Yinghai Lu0f978f42008-08-19 20:50:26 -0700490 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (entry->apic == oldapic && entry->pin == oldpin) {
492 entry->apic = newapic;
493 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700494 replaced = 1;
495 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700497 }
498 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700500
501 /* why? call replace before add? */
502 if (!replaced)
Yinghai Lu3145e942008-12-05 18:58:34 -0800503 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Yinghai Lu3145e942008-12-05 18:58:34 -0800506static inline void io_apic_modify_irq(struct irq_cfg *cfg,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400507 int mask_and, int mask_or,
508 void (*final)(struct irq_pin_list *entry))
509{
510 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400511 struct irq_pin_list *entry;
512
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400513 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
514 unsigned int reg;
515 pin = entry->pin;
516 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
517 reg &= mask_and;
518 reg |= mask_or;
519 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
520 if (final)
521 final(entry);
522 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700523}
524
Yinghai Lu3145e942008-12-05 18:58:34 -0800525static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400526{
Yinghai Lu3145e942008-12-05 18:58:34 -0800527 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400528}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700529
530#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400531void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700532{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400533 /*
534 * Synchronize the IO-APIC and the CPU by doing
535 * a dummy read from the IO-APIC
536 */
537 struct io_apic __iomem *io_apic;
538 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700539 readl(&io_apic->data);
540}
541
Yinghai Lu3145e942008-12-05 18:58:34 -0800542static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400543{
Yinghai Lu3145e942008-12-05 18:58:34 -0800544 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400545}
546#else /* CONFIG_X86_32 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800547static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400548{
Yinghai Lu3145e942008-12-05 18:58:34 -0800549 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400550}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700551
Yinghai Lu3145e942008-12-05 18:58:34 -0800552static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400553{
Yinghai Lu3145e942008-12-05 18:58:34 -0800554 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400555 IO_APIC_REDIR_MASKED, NULL);
556}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700557
Yinghai Lu3145e942008-12-05 18:58:34 -0800558static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400559{
Yinghai Lu3145e942008-12-05 18:58:34 -0800560 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400561 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
562}
563#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700564
Yinghai Lu3145e942008-12-05 18:58:34 -0800565static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Yinghai Lu3145e942008-12-05 18:58:34 -0800567 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned long flags;
569
Yinghai Lu3145e942008-12-05 18:58:34 -0800570 BUG_ON(!cfg);
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800573 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 spin_unlock_irqrestore(&ioapic_lock, flags);
575}
576
Yinghai Lu3145e942008-12-05 18:58:34 -0800577static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Yinghai Lu3145e942008-12-05 18:58:34 -0800579 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 unsigned long flags;
581
582 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800583 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 spin_unlock_irqrestore(&ioapic_lock, flags);
585}
586
Yinghai Lu3145e942008-12-05 18:58:34 -0800587static void mask_IO_APIC_irq(unsigned int irq)
588{
589 struct irq_desc *desc = irq_to_desc(irq);
590
591 mask_IO_APIC_irq_desc(desc);
592}
593static void unmask_IO_APIC_irq(unsigned int irq)
594{
595 struct irq_desc *desc = irq_to_desc(irq);
596
597 unmask_IO_APIC_irq_desc(desc);
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
601{
602 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200605 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (entry.delivery_mode == dest_SMI)
607 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /*
609 * Disable it in the IO-APIC irq-routing table:
610 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800611 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Ingo Molnar54168ed2008-08-20 09:07:45 +0200614static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
616 int apic, pin;
617
618 for (apic = 0; apic < nr_ioapics; apic++)
619 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
620 clear_IO_APIC_pin(apic, pin);
621}
622
Ingo Molnar54168ed2008-08-20 09:07:45 +0200623#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100624void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
626 unsigned int cfg;
627
628 /*
629 * Wait for idle.
630 */
631 apic_wait_icr_idle();
632 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
633 /*
634 * Send the IPI. The write to APIC_ICR fires this off.
635 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100636 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200638#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Ingo Molnar54168ed2008-08-20 09:07:45 +0200640#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/*
642 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
643 * specific CPU-side IRQs.
644 */
645
646#define MAX_PIRQS 8
647static int pirq_entries [MAX_PIRQS];
648static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650static int __init ioapic_pirq_setup(char *str)
651{
652 int i, max;
653 int ints[MAX_PIRQS+1];
654
655 get_options(str, ARRAY_SIZE(ints), ints);
656
657 for (i = 0; i < MAX_PIRQS; i++)
658 pirq_entries[i] = -1;
659
660 pirqs_enabled = 1;
661 apic_printk(APIC_VERBOSE, KERN_INFO
662 "PIRQ redirection, working around broken MP-BIOS.\n");
663 max = MAX_PIRQS;
664 if (ints[0] < MAX_PIRQS)
665 max = ints[0];
666
667 for (i = 0; i < max; i++) {
668 apic_printk(APIC_VERBOSE, KERN_DEBUG
669 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
670 /*
671 * PIRQs are mapped upside down, usually.
672 */
673 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
674 }
675 return 1;
676}
677
678__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200679#endif /* CONFIG_X86_32 */
680
681#ifdef CONFIG_INTR_REMAP
682/* I/O APIC RTE contents at the OS boot up */
683static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
684
685/*
686 * Saves and masks all the unmasked IO-APIC RTE's
687 */
688int save_mask_IO_APIC_setup(void)
689{
690 union IO_APIC_reg_01 reg_01;
691 unsigned long flags;
692 int apic, pin;
693
694 /*
695 * The number of IO-APIC IRQ registers (== #pins):
696 */
697 for (apic = 0; apic < nr_ioapics; apic++) {
698 spin_lock_irqsave(&ioapic_lock, flags);
699 reg_01.raw = io_apic_read(apic, 1);
700 spin_unlock_irqrestore(&ioapic_lock, flags);
701 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
702 }
703
704 for (apic = 0; apic < nr_ioapics; apic++) {
705 early_ioapic_entries[apic] =
706 kzalloc(sizeof(struct IO_APIC_route_entry) *
707 nr_ioapic_registers[apic], GFP_KERNEL);
708 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400709 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200710 }
711
712 for (apic = 0; apic < nr_ioapics; apic++)
713 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
714 struct IO_APIC_route_entry entry;
715
716 entry = early_ioapic_entries[apic][pin] =
717 ioapic_read_entry(apic, pin);
718 if (!entry.mask) {
719 entry.mask = 1;
720 ioapic_write_entry(apic, pin, entry);
721 }
722 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400723
Ingo Molnar54168ed2008-08-20 09:07:45 +0200724 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400725
726nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400727 while (apic >= 0)
728 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400729 memset(early_ioapic_entries, 0,
730 ARRAY_SIZE(early_ioapic_entries));
731
732 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200733}
734
735void restore_IO_APIC_setup(void)
736{
737 int apic, pin;
738
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400739 for (apic = 0; apic < nr_ioapics; apic++) {
740 if (!early_ioapic_entries[apic])
741 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200742 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
743 ioapic_write_entry(apic, pin,
744 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400745 kfree(early_ioapic_entries[apic]);
746 early_ioapic_entries[apic] = NULL;
747 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200748}
749
750void reinit_intr_remapped_IO_APIC(int intr_remapping)
751{
752 /*
753 * for now plain restore of previous settings.
754 * TBD: In the case of OS enabling interrupt-remapping,
755 * IO-APIC RTE's need to be setup to point to interrupt-remapping
756 * table entries. for now, do a plain restore, and wait for
757 * the setup_IO_APIC_irqs() to do proper initialization.
758 */
759 restore_IO_APIC_setup();
760}
761#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
763/*
764 * Find the IRQ entry number of a certain pin.
765 */
766static int find_irq_entry(int apic, int pin, int type)
767{
768 int i;
769
770 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400771 if (mp_irqs[i].mp_irqtype == type &&
772 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
773 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
774 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 return i;
776
777 return -1;
778}
779
780/*
781 * Find the pin to which IRQ[irq] (ISA) is connected
782 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800783static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 int i;
786
787 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400788 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300790 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400791 (mp_irqs[i].mp_irqtype == type) &&
792 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400794 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796 return -1;
797}
798
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800799static int __init find_isa_irq_apic(int irq, int type)
800{
801 int i;
802
803 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400804 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800805
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300806 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400807 (mp_irqs[i].mp_irqtype == type) &&
808 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800809 break;
810 }
811 if (i < mp_irq_entries) {
812 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200813 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400814 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800815 return apic;
816 }
817 }
818
819 return -1;
820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/*
823 * Find a specific PCI IRQ entry.
824 * Not an __init, possibly needed by modules
825 */
826static int pin_2_irq(int idx, int apic, int pin);
827
828int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
829{
830 int apic, i, best_guess = -1;
831
Ingo Molnar54168ed2008-08-20 09:07:45 +0200832 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
833 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400834 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200835 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -1;
837 }
838 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400839 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400842 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
843 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 break;
845
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300846 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400847 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400849 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200850 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (!(apic || IO_APIC_IRQ(irq)))
853 continue;
854
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400855 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return irq;
857 /*
858 * Use the first all-but-pin matching entry as a
859 * best-guess fuzzy result for broken mptables.
860 */
861 if (best_guess < 0)
862 best_guess = irq;
863 }
864 }
865 return best_guess;
866}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200867
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700868EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300870#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/*
872 * EISA Edge/Level control register, ELCR
873 */
874static int EISA_ELCR(unsigned int irq)
875{
Yinghai Lu99d093d2008-12-05 18:58:32 -0800876 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 unsigned int port = 0x4d0 + (irq >> 3);
878 return (inb(port) >> (irq & 7)) & 1;
879 }
880 apic_printk(APIC_VERBOSE, KERN_INFO
881 "Broken MPtable reports ISA irq %d\n", irq);
882 return 0;
883}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200884
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300885#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300887/* ISA interrupts are always polarity zero edge triggered,
888 * when listed as conforming in the MP table. */
889
890#define default_ISA_trigger(idx) (0)
891#define default_ISA_polarity(idx) (0)
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/* EISA interrupts are always polarity zero and can be edge or level
894 * trigger depending on the ELCR value. If an interrupt is listed as
895 * EISA conforming in the MP table, that means its trigger type must
896 * be read in from the ELCR */
897
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400898#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300899#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901/* PCI interrupts are always polarity one level triggered,
902 * when listed as conforming in the MP table. */
903
904#define default_PCI_trigger(idx) (1)
905#define default_PCI_polarity(idx) (1)
906
907/* MCA interrupts are always polarity zero level triggered,
908 * when listed as conforming in the MP table. */
909
910#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300911#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Shaohua Li61fd47e2007-11-17 01:05:28 -0500913static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400915 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 int polarity;
917
918 /*
919 * Determine IRQ line polarity (high active or low active):
920 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200921 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200923 case 0: /* conforms, ie. bus-type dependent polarity */
924 if (test_bit(bus, mp_bus_not_pci))
925 polarity = default_ISA_polarity(idx);
926 else
927 polarity = default_PCI_polarity(idx);
928 break;
929 case 1: /* high active */
930 {
931 polarity = 0;
932 break;
933 }
934 case 2: /* reserved */
935 {
936 printk(KERN_WARNING "broken BIOS!!\n");
937 polarity = 1;
938 break;
939 }
940 case 3: /* low active */
941 {
942 polarity = 1;
943 break;
944 }
945 default: /* invalid */
946 {
947 printk(KERN_WARNING "broken BIOS!!\n");
948 polarity = 1;
949 break;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952 return polarity;
953}
954
955static int MPBIOS_trigger(int idx)
956{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400957 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int trigger;
959
960 /*
961 * Determine IRQ trigger mode (edge or level sensitive):
962 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200963 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200965 case 0: /* conforms, ie. bus-type dependent */
966 if (test_bit(bus, mp_bus_not_pci))
967 trigger = default_ISA_trigger(idx);
968 else
969 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300970#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200971 switch (mp_bus_id_to_type[bus]) {
972 case MP_BUS_ISA: /* ISA pin */
973 {
974 /* set before the switch */
975 break;
976 }
977 case MP_BUS_EISA: /* EISA pin */
978 {
979 trigger = default_EISA_trigger(idx);
980 break;
981 }
982 case MP_BUS_PCI: /* PCI pin */
983 {
984 /* set before the switch */
985 break;
986 }
987 case MP_BUS_MCA: /* MCA pin */
988 {
989 trigger = default_MCA_trigger(idx);
990 break;
991 }
992 default:
993 {
994 printk(KERN_WARNING "broken BIOS!!\n");
995 trigger = 1;
996 break;
997 }
998 }
999#endif
1000 break;
1001 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001002 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001003 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001004 break;
1005 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001006 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001007 {
1008 printk(KERN_WARNING "broken BIOS!!\n");
1009 trigger = 1;
1010 break;
1011 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001012 case 3: /* level */
1013 {
1014 trigger = 1;
1015 break;
1016 }
1017 default: /* invalid */
1018 {
1019 printk(KERN_WARNING "broken BIOS!!\n");
1020 trigger = 0;
1021 break;
1022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 return trigger;
1025}
1026
1027static inline int irq_polarity(int idx)
1028{
1029 return MPBIOS_polarity(idx);
1030}
1031
1032static inline int irq_trigger(int idx)
1033{
1034 return MPBIOS_trigger(idx);
1035}
1036
Yinghai Luefa25592008-08-19 20:50:36 -07001037int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038static int pin_2_irq(int idx, int apic, int pin)
1039{
1040 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001041 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 /*
1044 * Debugging check, we are in big trouble if this message pops up!
1045 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001046 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1048
Ingo Molnar54168ed2008-08-20 09:07:45 +02001049 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001050 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001051 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001052 /*
1053 * PCI IRQs are mapped in order
1054 */
1055 i = irq = 0;
1056 while (i < apic)
1057 irq += nr_ioapic_registers[i++];
1058 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001059 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001060 * For MPS mode, so far only needed by ES7000 platform
1061 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001062 if (ioapic_renumber_irq)
1063 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065
Ingo Molnar54168ed2008-08-20 09:07:45 +02001066#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 /*
1068 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1069 */
1070 if ((pin >= 16) && (pin <= 23)) {
1071 if (pirq_entries[pin-16] != -1) {
1072 if (!pirq_entries[pin-16]) {
1073 apic_printk(APIC_VERBOSE, KERN_DEBUG
1074 "disabling PIRQ%d\n", pin-16);
1075 } else {
1076 irq = pirq_entries[pin-16];
1077 apic_printk(APIC_VERBOSE, KERN_DEBUG
1078 "using PIRQ%d -> IRQ %d\n",
1079 pin-16, irq);
1080 }
1081 }
1082 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001083#endif
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return irq;
1086}
1087
Yinghai Lu497c9a12008-08-19 20:50:28 -07001088void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001090 /* Used to the online set of cpus does not change
1091 * during assign_irq_vector.
1092 */
1093 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Yinghai Lu497c9a12008-08-19 20:50:28 -07001096void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001097{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001098 spin_unlock(&vector_lock);
1099}
1100
Yinghai Lu3145e942008-12-05 18:58:34 -08001101static int __assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001102{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001103 /*
1104 * NOTE! The local APIC isn't very good at handling
1105 * multiple interrupts at the same interrupt level.
1106 * As the interrupt level is determined by taking the
1107 * vector number and shifting that right by 4, we
1108 * want to spread these out a bit so that they don't
1109 * all fall in the same interrupt level.
1110 *
1111 * Also, we've got to be careful not to trash gate
1112 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1113 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001114 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1115 unsigned int old_vector;
1116 int cpu;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001117
Ingo Molnar54168ed2008-08-20 09:07:45 +02001118 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1119 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001120
Yinghai Lu3145e942008-12-05 18:58:34 -08001121 /* Only try and allocate irqs on cpus that are present */
1122 cpus_and(mask, mask, cpu_online_map);
1123
Ingo Molnar54168ed2008-08-20 09:07:45 +02001124 old_vector = cfg->vector;
1125 if (old_vector) {
1126 cpumask_t tmp;
1127 cpus_and(tmp, cfg->domain, mask);
1128 if (!cpus_empty(tmp))
1129 return 0;
1130 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001131
Ingo Molnar54168ed2008-08-20 09:07:45 +02001132 for_each_cpu_mask_nr(cpu, mask) {
1133 cpumask_t domain, new_mask;
1134 int new_cpu;
1135 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001136
Ingo Molnar54168ed2008-08-20 09:07:45 +02001137 domain = vector_allocation_domain(cpu);
1138 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001139
Ingo Molnar54168ed2008-08-20 09:07:45 +02001140 vector = current_vector;
1141 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001142next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001143 vector += 8;
1144 if (vector >= first_system_vector) {
1145 /* If we run out of vectors on large boxen, must share them. */
1146 offset = (offset + 1) % 8;
1147 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001148 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001149 if (unlikely(current_vector == vector))
1150 continue;
1151#ifdef CONFIG_X86_64
1152 if (vector == IA32_SYSCALL_VECTOR)
1153 goto next;
1154#else
1155 if (vector == SYSCALL_VECTOR)
1156 goto next;
1157#endif
1158 for_each_cpu_mask_nr(new_cpu, new_mask)
1159 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1160 goto next;
1161 /* Found one! */
1162 current_vector = vector;
1163 current_offset = offset;
1164 if (old_vector) {
1165 cfg->move_in_progress = 1;
1166 cfg->old_domain = cfg->domain;
1167 }
1168 for_each_cpu_mask_nr(new_cpu, new_mask)
1169 per_cpu(vector_irq, new_cpu)[vector] = irq;
1170 cfg->vector = vector;
1171 cfg->domain = domain;
1172 return 0;
1173 }
1174 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001175}
1176
Yinghai Lu3145e942008-12-05 18:58:34 -08001177static int assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001178{
1179 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001180 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001181
1182 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001183 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001184 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001185 return err;
1186}
1187
Yinghai Lu3145e942008-12-05 18:58:34 -08001188static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001189{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001190 cpumask_t mask;
1191 int cpu, vector;
1192
Yinghai Lu497c9a12008-08-19 20:50:28 -07001193 BUG_ON(!cfg->vector);
1194
1195 vector = cfg->vector;
1196 cpus_and(mask, cfg->domain, cpu_online_map);
1197 for_each_cpu_mask_nr(cpu, mask)
1198 per_cpu(vector_irq, cpu)[vector] = -1;
1199
1200 cfg->vector = 0;
1201 cpus_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001202
1203 if (likely(!cfg->move_in_progress))
1204 return;
1205 cpus_and(mask, cfg->old_domain, cpu_online_map);
1206 for_each_cpu_mask_nr(cpu, mask) {
1207 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1208 vector++) {
1209 if (per_cpu(vector_irq, cpu)[vector] != irq)
1210 continue;
1211 per_cpu(vector_irq, cpu)[vector] = -1;
1212 break;
1213 }
1214 }
1215 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001216}
1217
1218void __setup_vector_irq(int cpu)
1219{
1220 /* Initialize vector_irq on a new cpu */
1221 /* This function must be called with vector_lock held */
1222 int irq, vector;
1223 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001224 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001225
1226 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001227 for_each_irq_desc(irq, desc) {
1228 if (!desc)
1229 continue;
1230 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001231 if (!cpu_isset(cpu, cfg->domain))
1232 continue;
1233 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001234 per_cpu(vector_irq, cpu)[vector] = irq;
1235 }
1236 /* Mark the free vectors */
1237 for (vector = 0; vector < NR_VECTORS; ++vector) {
1238 irq = per_cpu(vector_irq, cpu)[vector];
1239 if (irq < 0)
1240 continue;
1241
1242 cfg = irq_cfg(irq);
1243 if (!cpu_isset(cpu, cfg->domain))
1244 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001245 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001246}
Glauber Costa3fde6902008-05-28 20:34:19 -07001247
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001248static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001249#ifdef CONFIG_INTR_REMAP
1250static struct irq_chip ir_ioapic_chip;
1251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Ingo Molnar54168ed2008-08-20 09:07:45 +02001253#define IOAPIC_AUTO -1
1254#define IOAPIC_EDGE 0
1255#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001257#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001258static inline int IO_APIC_irq_trigger(int irq)
1259{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001260 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001261
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001262 for (apic = 0; apic < nr_ioapics; apic++) {
1263 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1264 idx = find_irq_entry(apic, pin, mp_INT);
1265 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1266 return irq_trigger(idx);
1267 }
1268 }
1269 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001270 * nonexistent IRQs are edge default
1271 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001272 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001273}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001274#else
1275static inline int IO_APIC_irq_trigger(int irq)
1276{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001277 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001278}
1279#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001280
Yinghai Lu3145e942008-12-05 18:58:34 -08001281static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Yinghai Lu199751d2008-08-19 20:50:27 -07001283
Jan Beulich6ebcc002006-06-26 13:56:46 +02001284 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001285 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001286 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001287 else
1288 desc->status &= ~IRQ_LEVEL;
1289
Ingo Molnar54168ed2008-08-20 09:07:45 +02001290#ifdef CONFIG_INTR_REMAP
1291 if (irq_remapped(irq)) {
1292 desc->status |= IRQ_MOVE_PCNTXT;
1293 if (trigger)
1294 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1295 handle_fasteoi_irq,
1296 "fasteoi");
1297 else
1298 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1299 handle_edge_irq, "edge");
1300 return;
1301 }
1302#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001303 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1304 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001305 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001306 handle_fasteoi_irq,
1307 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001308 else
Ingo Molnara460e742006-10-17 00:10:03 -07001309 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001310 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001311}
1312
1313static int setup_ioapic_entry(int apic, int irq,
1314 struct IO_APIC_route_entry *entry,
1315 unsigned int destination, int trigger,
1316 int polarity, int vector)
1317{
1318 /*
1319 * add it to the IO-APIC irq-routing table:
1320 */
1321 memset(entry,0,sizeof(*entry));
1322
Ingo Molnar54168ed2008-08-20 09:07:45 +02001323#ifdef CONFIG_INTR_REMAP
1324 if (intr_remapping_enabled) {
1325 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1326 struct irte irte;
1327 struct IR_IO_APIC_route_entry *ir_entry =
1328 (struct IR_IO_APIC_route_entry *) entry;
1329 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001330
Ingo Molnar54168ed2008-08-20 09:07:45 +02001331 if (!iommu)
1332 panic("No mapping iommu for ioapic %d\n", apic);
1333
1334 index = alloc_irte(iommu, irq, 1);
1335 if (index < 0)
1336 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1337
1338 memset(&irte, 0, sizeof(irte));
1339
1340 irte.present = 1;
1341 irte.dst_mode = INT_DEST_MODE;
1342 irte.trigger_mode = trigger;
1343 irte.dlvry_mode = INT_DELIVERY_MODE;
1344 irte.vector = vector;
1345 irte.dest_id = IRTE_DEST(destination);
1346
1347 modify_irte(irq, &irte);
1348
1349 ir_entry->index2 = (index >> 15) & 0x1;
1350 ir_entry->zero = 0;
1351 ir_entry->format = 1;
1352 ir_entry->index = (index & 0x7fff);
1353 } else
1354#endif
1355 {
1356 entry->delivery_mode = INT_DELIVERY_MODE;
1357 entry->dest_mode = INT_DEST_MODE;
1358 entry->dest = destination;
1359 }
1360
1361 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001362 entry->trigger = trigger;
1363 entry->polarity = polarity;
1364 entry->vector = vector;
1365
1366 /* Mask level triggered irqs.
1367 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1368 */
1369 if (trigger)
1370 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001371 return 0;
1372}
1373
Yinghai Lu3145e942008-12-05 18:58:34 -08001374static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001375 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001376{
1377 struct irq_cfg *cfg;
1378 struct IO_APIC_route_entry entry;
1379 cpumask_t mask;
1380
1381 if (!IO_APIC_IRQ(irq))
1382 return;
1383
Yinghai Lu3145e942008-12-05 18:58:34 -08001384 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001385
1386 mask = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08001387 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001388 return;
1389
1390 cpus_and(mask, cfg->domain, mask);
1391
1392 apic_printk(APIC_VERBOSE,KERN_DEBUG
1393 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1394 "IRQ %d Mode:%i Active:%i)\n",
1395 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1396 irq, trigger, polarity);
1397
1398
1399 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1400 cpu_mask_to_apicid(mask), trigger, polarity,
1401 cfg->vector)) {
1402 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1403 mp_ioapics[apic].mp_apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001404 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001405 return;
1406 }
1407
Yinghai Lu3145e942008-12-05 18:58:34 -08001408 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001409 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001410 disable_8259A_irq(irq);
1411
1412 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415static void __init setup_IO_APIC_irqs(void)
1416{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001417 int apic, pin, idx, irq;
1418 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001419 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001420 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001421 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1424
1425 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001426 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001428 idx = find_irq_entry(apic, pin, mp_INT);
1429 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001430 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001431 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001432 apic_printk(APIC_VERBOSE,
1433 KERN_DEBUG " %d-%d",
1434 mp_ioapics[apic].mp_apicid,
1435 pin);
1436 } else
1437 apic_printk(APIC_VERBOSE, " %d-%d",
1438 mp_ioapics[apic].mp_apicid,
1439 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001440 continue;
1441 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001442 if (notcon) {
1443 apic_printk(APIC_VERBOSE,
1444 " (apicid-pin) not connected\n");
1445 notcon = 0;
1446 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001447
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001448 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001449#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001450 if (multi_timer_check(apic, irq))
1451 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001452#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001453 desc = irq_to_desc_alloc_cpu(irq, cpu);
1454 if (!desc) {
1455 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1456 continue;
1457 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001458 cfg = desc->chip_data;
1459 add_pin_to_irq_cpu(cfg, cpu, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001460
Yinghai Lu3145e942008-12-05 18:58:34 -08001461 setup_IO_APIC_irq(apic, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001462 irq_trigger(idx), irq_polarity(idx));
1463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 }
1465
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001466 if (notcon)
1467 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001468 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001472 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001474static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1475 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
1477 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Ingo Molnar54168ed2008-08-20 09:07:45 +02001479#ifdef CONFIG_INTR_REMAP
1480 if (intr_remapping_enabled)
1481 return;
1482#endif
1483
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001484 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /*
1487 * We use logical delivery to get the timer IRQ
1488 * to the first CPU.
1489 */
1490 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001491 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001492 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 entry.delivery_mode = INT_DELIVERY_MODE;
1494 entry.polarity = 0;
1495 entry.trigger = 0;
1496 entry.vector = vector;
1497
1498 /*
1499 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001500 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001502 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 /*
1505 * Add it to the IO-APIC irq-routing table:
1506 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001507 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508}
1509
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001510
1511__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 int apic, i;
1514 union IO_APIC_reg_00 reg_00;
1515 union IO_APIC_reg_01 reg_01;
1516 union IO_APIC_reg_02 reg_02;
1517 union IO_APIC_reg_03 reg_03;
1518 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001519 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001520 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001521 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 if (apic_verbosity == APIC_QUIET)
1524 return;
1525
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001526 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 for (i = 0; i < nr_ioapics; i++)
1528 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001529 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 /*
1532 * We are a bit conservative about what we expect. We have to
1533 * know about every hardware change ASAP.
1534 */
1535 printk(KERN_INFO "testing the IO APIC.......................\n");
1536
1537 for (apic = 0; apic < nr_ioapics; apic++) {
1538
1539 spin_lock_irqsave(&ioapic_lock, flags);
1540 reg_00.raw = io_apic_read(apic, 0);
1541 reg_01.raw = io_apic_read(apic, 1);
1542 if (reg_01.bits.version >= 0x10)
1543 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001544 if (reg_01.bits.version >= 0x20)
1545 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 spin_unlock_irqrestore(&ioapic_lock, flags);
1547
Ingo Molnar54168ed2008-08-20 09:07:45 +02001548 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001549 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1551 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1552 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1553 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Ingo Molnar54168ed2008-08-20 09:07:45 +02001555 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1559 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /*
1562 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1563 * but the value of reg_02 is read as the previous read register
1564 * value, so ignore it if reg_02 == reg_01.
1565 */
1566 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1567 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1568 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570
1571 /*
1572 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1573 * or reg_03, but the value of reg_0[23] is read as the previous read
1574 * register value, so ignore it if reg_03 == reg_0[12].
1575 */
1576 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1577 reg_03.raw != reg_01.raw) {
1578 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1579 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581
1582 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1583
Yinghai Lud83e94a2008-08-19 20:50:33 -07001584 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1585 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 for (i = 0; i <= reg_01.bits.entries; i++) {
1588 struct IO_APIC_route_entry entry;
1589
Andi Kleencf4c6a22006-09-26 10:52:30 +02001590 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Ingo Molnar54168ed2008-08-20 09:07:45 +02001592 printk(KERN_DEBUG " %02x %03X ",
1593 i,
1594 entry.dest
1595 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1598 entry.mask,
1599 entry.trigger,
1600 entry.irr,
1601 entry.polarity,
1602 entry.delivery_status,
1603 entry.dest_mode,
1604 entry.delivery_mode,
1605 entry.vector
1606 );
1607 }
1608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001610 for_each_irq_desc(irq, desc) {
1611 struct irq_pin_list *entry;
1612
1613 if (!desc)
1614 continue;
1615 cfg = desc->chip_data;
1616 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001617 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001619 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 for (;;) {
1621 printk("-> %d:%d", entry->apic, entry->pin);
1622 if (!entry->next)
1623 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001624 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 printk("\n");
1627 }
1628
1629 printk(KERN_INFO ".................................... done.\n");
1630
1631 return;
1632}
1633
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001634__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 unsigned int v;
1637 int i, j;
1638
1639 if (apic_verbosity == APIC_QUIET)
1640 return;
1641
1642 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1643 for (i = 0; i < 8; i++) {
1644 v = apic_read(base + i*0x10);
1645 for (j = 0; j < 32; j++) {
1646 if (v & (1<<j))
1647 printk("1");
1648 else
1649 printk("0");
1650 }
1651 printk("\n");
1652 }
1653}
1654
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001655__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001658 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 if (apic_verbosity == APIC_QUIET)
1661 return;
1662
1663 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1664 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001665 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001666 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 v = apic_read(APIC_LVR);
1668 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1669 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001670 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 v = apic_read(APIC_TASKPRI);
1673 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1674
Ingo Molnar54168ed2008-08-20 09:07:45 +02001675 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001676 if (!APIC_XAPIC(ver)) {
1677 v = apic_read(APIC_ARBPRI);
1678 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1679 v & APIC_ARBPRI_MASK);
1680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 v = apic_read(APIC_PROCPRI);
1682 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1683 }
1684
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001685 /*
1686 * Remote read supported only in the 82489DX and local APIC for
1687 * Pentium processors.
1688 */
1689 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1690 v = apic_read(APIC_RRR);
1691 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1692 }
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 v = apic_read(APIC_LDR);
1695 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001696 if (!x2apic_enabled()) {
1697 v = apic_read(APIC_DFR);
1698 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 v = apic_read(APIC_SPIV);
1701 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1702
1703 printk(KERN_DEBUG "... APIC ISR field:\n");
1704 print_APIC_bitfield(APIC_ISR);
1705 printk(KERN_DEBUG "... APIC TMR field:\n");
1706 print_APIC_bitfield(APIC_TMR);
1707 printk(KERN_DEBUG "... APIC IRR field:\n");
1708 print_APIC_bitfield(APIC_IRR);
1709
Ingo Molnar54168ed2008-08-20 09:07:45 +02001710 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1711 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 v = apic_read(APIC_ESR);
1715 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1716 }
1717
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001718 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001719 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1720 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 v = apic_read(APIC_LVTT);
1723 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1724
1725 if (maxlvt > 3) { /* PC is LVT#4. */
1726 v = apic_read(APIC_LVTPC);
1727 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1728 }
1729 v = apic_read(APIC_LVT0);
1730 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1731 v = apic_read(APIC_LVT1);
1732 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1733
1734 if (maxlvt > 2) { /* ERR is LVT#3. */
1735 v = apic_read(APIC_LVTERR);
1736 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1737 }
1738
1739 v = apic_read(APIC_TMICT);
1740 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1741 v = apic_read(APIC_TMCCT);
1742 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1743 v = apic_read(APIC_TDCR);
1744 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1745 printk("\n");
1746}
1747
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001748__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001750 int cpu;
1751
1752 preempt_disable();
1753 for_each_online_cpu(cpu)
1754 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1755 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001758__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 unsigned int v;
1761 unsigned long flags;
1762
1763 if (apic_verbosity == APIC_QUIET)
1764 return;
1765
1766 printk(KERN_DEBUG "\nprinting PIC contents\n");
1767
1768 spin_lock_irqsave(&i8259A_lock, flags);
1769
1770 v = inb(0xa1) << 8 | inb(0x21);
1771 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1772
1773 v = inb(0xa0) << 8 | inb(0x20);
1774 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1775
Ingo Molnar54168ed2008-08-20 09:07:45 +02001776 outb(0x0b,0xa0);
1777 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001779 outb(0x0a,0xa0);
1780 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 spin_unlock_irqrestore(&i8259A_lock, flags);
1783
1784 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1785
1786 v = inb(0x4d1) << 8 | inb(0x4d0);
1787 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1788}
1789
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001790__apicdebuginit(int) print_all_ICs(void)
1791{
1792 print_PIC();
1793 print_all_local_APICs();
1794 print_IO_APIC();
1795
1796 return 0;
1797}
1798
1799fs_initcall(print_all_ICs);
1800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Yinghai Luefa25592008-08-19 20:50:36 -07001802/* Where if anywhere is the i8259 connect in external int mode */
1803static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1804
Ingo Molnar54168ed2008-08-20 09:07:45 +02001805void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001808 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001809 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 unsigned long flags;
1811
Ingo Molnar54168ed2008-08-20 09:07:45 +02001812#ifdef CONFIG_X86_32
1813 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 if (!pirqs_enabled)
1815 for (i = 0; i < MAX_PIRQS; i++)
1816 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001817#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 /*
1820 * The number of IO-APIC IRQ registers (== #pins):
1821 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001822 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001824 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001826 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1827 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001828 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001829 int pin;
1830 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001831 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001832 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001833 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001834
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001835 /* If the interrupt line is enabled and in ExtInt mode
1836 * I have found the pin where the i8259 is connected.
1837 */
1838 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1839 ioapic_i8259.apic = apic;
1840 ioapic_i8259.pin = pin;
1841 goto found_i8259;
1842 }
1843 }
1844 }
1845 found_i8259:
1846 /* Look to see what if the MP table has reported the ExtINT */
1847 /* If we could not find the appropriate pin by looking at the ioapic
1848 * the i8259 probably is not connected the ioapic but give the
1849 * mptable a chance anyway.
1850 */
1851 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1852 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1853 /* Trust the MP table if nothing is setup in the hardware */
1854 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1855 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1856 ioapic_i8259.pin = i8259_pin;
1857 ioapic_i8259.apic = i8259_apic;
1858 }
1859 /* Complain if the MP table and the hardware disagree */
1860 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1861 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1862 {
1863 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
1865
1866 /*
1867 * Do not trust the IO-APIC being empty at bootup
1868 */
1869 clear_IO_APIC();
1870}
1871
1872/*
1873 * Not an __init, needed by the reboot code
1874 */
1875void disable_IO_APIC(void)
1876{
1877 /*
1878 * Clear the IO-APIC before rebooting:
1879 */
1880 clear_IO_APIC();
1881
Eric W. Biederman650927e2005-06-25 14:57:44 -07001882 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001883 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001884 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001885 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001886 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001887 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001888 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001889
1890 memset(&entry, 0, sizeof(entry));
1891 entry.mask = 0; /* Enabled */
1892 entry.trigger = 0; /* Edge */
1893 entry.irr = 0;
1894 entry.polarity = 0; /* High */
1895 entry.delivery_status = 0;
1896 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001897 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001898 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001899 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001900
1901 /*
1902 * Add it to the IO-APIC irq-routing table:
1903 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001904 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001905 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001906
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001907 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
Ingo Molnar54168ed2008-08-20 09:07:45 +02001910#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911/*
1912 * function to set the IO-APIC physical IDs based on the
1913 * values stored in the MPC table.
1914 *
1915 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1916 */
1917
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918static void __init setup_ioapic_ids_from_mpc(void)
1919{
1920 union IO_APIC_reg_00 reg_00;
1921 physid_mask_t phys_id_present_map;
1922 int apic;
1923 int i;
1924 unsigned char old_id;
1925 unsigned long flags;
1926
Yinghai Lua4dbc342008-07-25 02:14:28 -07001927 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001928 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001931 * Don't check I/O APIC IDs for xAPIC systems. They have
1932 * no meaning without the serial APIC bus.
1933 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001934 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1935 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001936 return;
1937 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 * This is broken; anything with a real cpu count has to
1939 * circumvent this idiocy regardless.
1940 */
1941 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1942
1943 /*
1944 * Set the IOAPIC ID to the value stored in the MPC table.
1945 */
1946 for (apic = 0; apic < nr_ioapics; apic++) {
1947
1948 /* Read the register 0 value */
1949 spin_lock_irqsave(&ioapic_lock, flags);
1950 reg_00.raw = io_apic_read(apic, 0);
1951 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001952
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001953 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001955 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001957 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1959 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001960 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /*
1964 * Sanity check, is the ID really free? Every APIC in a
1965 * system must have a unique ID or we get lots of nice
1966 * 'stuck on smp_invalidate_needed IPI wait' messages.
1967 */
1968 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001969 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001971 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 for (i = 0; i < get_physical_broadcast(); i++)
1973 if (!physid_isset(i, phys_id_present_map))
1974 break;
1975 if (i >= get_physical_broadcast())
1976 panic("Max APIC ID exceeded!\n");
1977 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1978 i);
1979 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001980 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 } else {
1982 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001983 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 apic_printk(APIC_VERBOSE, "Setting %d in the "
1985 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001986 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1988 }
1989
1990
1991 /*
1992 * We need to adjust the IRQ routing table
1993 * if the ID changed.
1994 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001995 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001997 if (mp_irqs[i].mp_dstapic == old_id)
1998 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001999 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 /*
2002 * Read the right value from the MPC table and
2003 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 apic_printk(APIC_VERBOSE, KERN_INFO
2006 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002007 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002009 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002011 io_apic_write(apic, 0, reg_00.raw);
2012 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
2014 /*
2015 * Sanity check
2016 */
2017 spin_lock_irqsave(&ioapic_lock, flags);
2018 reg_00.raw = io_apic_read(apic, 0);
2019 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002020 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 printk("could not set ID!\n");
2022 else
2023 apic_printk(APIC_VERBOSE, " ok.\n");
2024 }
2025}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002026#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002028int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002029
2030static int __init notimercheck(char *s)
2031{
2032 no_timer_check = 1;
2033 return 1;
2034}
2035__setup("no_timer_check", notimercheck);
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/*
2038 * There is a nasty bug in some older SMP boards, their mptable lies
2039 * about the timer IRQ. We do the following to work around the situation:
2040 *
2041 * - timer IRQ defaults to IO-APIC IRQ
2042 * - if this function detects that timer IRQs are defunct, then we fall
2043 * back to ISA timer IRQs
2044 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002045static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
2047 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002048 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Zachary Amsden8542b202006-12-07 02:14:09 +01002050 if (no_timer_check)
2051 return 1;
2052
Ingo Molnar4aae0702007-12-18 18:05:58 +01002053 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 local_irq_enable();
2055 /* Let ten ticks pass... */
2056 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002057 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059 /*
2060 * Expect a few ticks at least, to be sure some possible
2061 * glue logic does not lock up after one or two first
2062 * ticks in a non-ExtINT mode. Also the local APIC
2063 * might have cached one ExtINT interrupt. Finally, at
2064 * least one tick may be lost due to delays.
2065 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002066
2067 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002068 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return 0;
2071}
2072
2073/*
2074 * In the SMP+IOAPIC case it might happen that there are an unspecified
2075 * number of pending IRQ events unhandled. These cases are very rare,
2076 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2077 * better to do it this way as thus we do not have to be aware of
2078 * 'pending' interrupts in the IRQ path, except at this point.
2079 */
2080/*
2081 * Edge triggered needs to resend any interrupt
2082 * that was delayed but this is now handled in the device
2083 * independent code.
2084 */
2085
2086/*
2087 * Starting up a edge-triggered IO-APIC interrupt is
2088 * nasty - we need to make sure that we get the edge.
2089 * If it is already asserted for some reason, we need
2090 * return 1 to indicate that is was pending.
2091 *
2092 * This is not complete - we should be able to fake
2093 * an edge even if it isn't on the 8259A...
2094 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002095
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002096static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
2098 int was_pending = 0;
2099 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002100 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002103 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 disable_8259A_irq(irq);
2105 if (i8259A_irq_pending(irq))
2106 was_pending = 1;
2107 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002108 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002109 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 spin_unlock_irqrestore(&ioapic_lock, flags);
2111
2112 return was_pending;
2113}
2114
Ingo Molnar54168ed2008-08-20 09:07:45 +02002115#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002116static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002118
2119 struct irq_cfg *cfg = irq_cfg(irq);
2120 unsigned long flags;
2121
2122 spin_lock_irqsave(&vector_lock, flags);
2123 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2124 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002125
2126 return 1;
2127}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002128#else
2129static int ioapic_retrigger_irq(unsigned int irq)
2130{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002131 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002132
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002133 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002134}
2135#endif
2136
2137/*
2138 * Level and edge triggered IO-APIC interrupts need different handling,
2139 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2140 * handled with the level-triggered descriptor, but that one has slightly
2141 * more overhead. Level-triggered interrupts cannot be handled with the
2142 * edge-triggered handler, without risking IRQ storms and other ugly
2143 * races.
2144 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002145
Yinghai Lu497c9a12008-08-19 20:50:28 -07002146#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002147
2148#ifdef CONFIG_INTR_REMAP
2149static void ir_irq_migration(struct work_struct *work);
2150
2151static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2152
2153/*
2154 * Migrate the IO-APIC irq in the presence of intr-remapping.
2155 *
2156 * For edge triggered, irq migration is a simple atomic update(of vector
2157 * and cpu destination) of IRTE and flush the hardware cache.
2158 *
2159 * For level triggered, we need to modify the io-apic RTE aswell with the update
2160 * vector information, along with modifying IRTE with vector and destination.
2161 * So irq migration for level triggered is little bit more complex compared to
2162 * edge triggered migration. But the good news is, we use the same algorithm
2163 * for level triggered migration as we have today, only difference being,
2164 * we now initiate the irq migration from process context instead of the
2165 * interrupt context.
2166 *
2167 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2168 * suppression) to the IO-APIC, level triggered irq migration will also be
2169 * as simple as edge triggered migration and we can do the irq migration
2170 * with a simple atomic update to IO-APIC RTE.
2171 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002172static void migrate_ioapic_irq_desc(struct irq_desc *desc, cpumask_t mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002173{
2174 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002175 cpumask_t tmp, cleanup_mask;
2176 struct irte irte;
2177 int modify_ioapic_rte;
2178 unsigned int dest;
2179 unsigned long flags;
Yinghai Lu3145e942008-12-05 18:58:34 -08002180 unsigned int irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002181
2182 cpus_and(tmp, mask, cpu_online_map);
2183 if (cpus_empty(tmp))
2184 return;
2185
Yinghai Lu3145e942008-12-05 18:58:34 -08002186 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002187 if (get_irte(irq, &irte))
2188 return;
2189
Yinghai Lu3145e942008-12-05 18:58:34 -08002190 cfg = desc->chip_data;
2191 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002192 return;
2193
Yinghai Lu3145e942008-12-05 18:58:34 -08002194 set_extra_move_desc(desc, mask);
2195
Ingo Molnar54168ed2008-08-20 09:07:45 +02002196 cpus_and(tmp, cfg->domain, mask);
2197 dest = cpu_mask_to_apicid(tmp);
2198
Ingo Molnar54168ed2008-08-20 09:07:45 +02002199 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2200 if (modify_ioapic_rte) {
2201 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08002202 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002203 spin_unlock_irqrestore(&ioapic_lock, flags);
2204 }
2205
2206 irte.vector = cfg->vector;
2207 irte.dest_id = IRTE_DEST(dest);
2208
2209 /*
2210 * Modified the IRTE and flushes the Interrupt entry cache.
2211 */
2212 modify_irte(irq, &irte);
2213
2214 if (cfg->move_in_progress) {
2215 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2216 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2217 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2218 cfg->move_in_progress = 0;
2219 }
2220
2221 desc->affinity = mask;
2222}
2223
Yinghai Lu3145e942008-12-05 18:58:34 -08002224static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002225{
2226 int ret = -1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002227 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002228
Yinghai Lu3145e942008-12-05 18:58:34 -08002229 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002230
Yinghai Lu3145e942008-12-05 18:58:34 -08002231 if (io_apic_level_ack_pending(cfg)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002232 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002233 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002234 * vector information in the IO-APIC RTE and that will confuse
2235 * the EOI broadcast performed by cpu.
2236 * So, delay the irq migration to the next instance.
2237 */
2238 schedule_delayed_work(&ir_migration_work, 1);
2239 goto unmask;
2240 }
2241
2242 /* everthing is clear. we have right of way */
Yinghai Lu3145e942008-12-05 18:58:34 -08002243 migrate_ioapic_irq_desc(desc, desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002244
2245 ret = 0;
2246 desc->status &= ~IRQ_MOVE_PENDING;
2247 cpus_clear(desc->pending_mask);
2248
2249unmask:
Yinghai Lu3145e942008-12-05 18:58:34 -08002250 unmask_IO_APIC_irq_desc(desc);
2251
Ingo Molnar54168ed2008-08-20 09:07:45 +02002252 return ret;
2253}
2254
2255static void ir_irq_migration(struct work_struct *work)
2256{
2257 unsigned int irq;
2258 struct irq_desc *desc;
2259
2260 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002261 if (!desc)
2262 continue;
2263
Ingo Molnar54168ed2008-08-20 09:07:45 +02002264 if (desc->status & IRQ_MOVE_PENDING) {
2265 unsigned long flags;
2266
2267 spin_lock_irqsave(&desc->lock, flags);
2268 if (!desc->chip->set_affinity ||
2269 !(desc->status & IRQ_MOVE_PENDING)) {
2270 desc->status &= ~IRQ_MOVE_PENDING;
2271 spin_unlock_irqrestore(&desc->lock, flags);
2272 continue;
2273 }
2274
2275 desc->chip->set_affinity(irq, desc->pending_mask);
2276 spin_unlock_irqrestore(&desc->lock, flags);
2277 }
2278 }
2279}
2280
2281/*
2282 * Migrates the IRQ destination in the process context.
2283 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002284static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc, cpumask_t mask)
2285{
2286 if (desc->status & IRQ_LEVEL) {
2287 desc->status |= IRQ_MOVE_PENDING;
2288 desc->pending_mask = mask;
2289 migrate_irq_remapped_level_desc(desc);
2290 return;
2291 }
2292
2293 migrate_ioapic_irq_desc(desc, mask);
2294}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002295static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2296{
2297 struct irq_desc *desc = irq_to_desc(irq);
2298
Yinghai Lu3145e942008-12-05 18:58:34 -08002299 set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002300}
2301#endif
2302
Yinghai Lu497c9a12008-08-19 20:50:28 -07002303asmlinkage void smp_irq_move_cleanup_interrupt(void)
2304{
2305 unsigned vector, me;
2306 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002307#ifdef CONFIG_X86_64
2308 exit_idle();
2309#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002310 irq_enter();
2311
2312 me = smp_processor_id();
2313 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2314 unsigned int irq;
2315 struct irq_desc *desc;
2316 struct irq_cfg *cfg;
2317 irq = __get_cpu_var(vector_irq)[vector];
2318
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002319 if (irq == -1)
2320 continue;
2321
Yinghai Lu497c9a12008-08-19 20:50:28 -07002322 desc = irq_to_desc(irq);
2323 if (!desc)
2324 continue;
2325
2326 cfg = irq_cfg(irq);
2327 spin_lock(&desc->lock);
2328 if (!cfg->move_cleanup_count)
2329 goto unlock;
2330
2331 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2332 goto unlock;
2333
2334 __get_cpu_var(vector_irq)[vector] = -1;
2335 cfg->move_cleanup_count--;
2336unlock:
2337 spin_unlock(&desc->lock);
2338 }
2339
2340 irq_exit();
2341}
2342
Yinghai Lu3145e942008-12-05 18:58:34 -08002343static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002344{
Yinghai Lu3145e942008-12-05 18:58:34 -08002345 struct irq_desc *desc = *descp;
2346 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002347 unsigned vector, me;
2348
2349 if (likely(!cfg->move_in_progress))
2350 return;
2351
2352 vector = ~get_irq_regs()->orig_ax;
2353 me = smp_processor_id();
2354 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2355 cpumask_t cleanup_mask;
2356
2357 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2358 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2359 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2360 cfg->move_in_progress = 0;
2361 }
2362}
2363#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002364static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002365#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002366
Ingo Molnar54168ed2008-08-20 09:07:45 +02002367#ifdef CONFIG_INTR_REMAP
2368static void ack_x2apic_level(unsigned int irq)
2369{
2370 ack_x2APIC_irq();
2371}
2372
2373static void ack_x2apic_edge(unsigned int irq)
2374{
2375 ack_x2APIC_irq();
2376}
Yinghai Lu3145e942008-12-05 18:58:34 -08002377
Ingo Molnar54168ed2008-08-20 09:07:45 +02002378#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002379
Yinghai Lu1d025192008-08-19 20:50:34 -07002380static void ack_apic_edge(unsigned int irq)
2381{
Yinghai Lu3145e942008-12-05 18:58:34 -08002382 struct irq_desc *desc = irq_to_desc(irq);
2383
2384 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002385 move_native_irq(irq);
2386 ack_APIC_irq();
2387}
2388
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002389atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002390
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002391static void ack_apic_level(unsigned int irq)
2392{
Yinghai Lu3145e942008-12-05 18:58:34 -08002393 struct irq_desc *desc = irq_to_desc(irq);
2394
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002395#ifdef CONFIG_X86_32
2396 unsigned long v;
2397 int i;
2398#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002399 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002400 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002401
Yinghai Lu3145e942008-12-05 18:58:34 -08002402 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002403#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002404 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002405 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002406 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002407 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002408 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002409#endif
2410
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002411#ifdef CONFIG_X86_32
2412 /*
2413 * It appears there is an erratum which affects at least version 0x11
2414 * of I/O APIC (that's the 82093AA and cores integrated into various
2415 * chipsets). Under certain conditions a level-triggered interrupt is
2416 * erroneously delivered as edge-triggered one but the respective IRR
2417 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2418 * message but it will never arrive and further interrupts are blocked
2419 * from the source. The exact reason is so far unknown, but the
2420 * phenomenon was observed when two consecutive interrupt requests
2421 * from a given source get delivered to the same CPU and the source is
2422 * temporarily disabled in between.
2423 *
2424 * A workaround is to simulate an EOI message manually. We achieve it
2425 * by setting the trigger mode to edge and then to level when the edge
2426 * trigger mode gets detected in the TMR of a local APIC for a
2427 * level-triggered interrupt. We mask the source for the time of the
2428 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2429 * The idea is from Manfred Spraul. --macro
2430 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002431 cfg = desc->chip_data;
2432 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002433
2434 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2435#endif
2436
Ingo Molnar54168ed2008-08-20 09:07:45 +02002437 /*
2438 * We must acknowledge the irq before we move it or the acknowledge will
2439 * not propagate properly.
2440 */
2441 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002442
Ingo Molnar54168ed2008-08-20 09:07:45 +02002443 /* Now we can move and renable the irq */
2444 if (unlikely(do_unmask_irq)) {
2445 /* Only migrate the irq if the ack has been received.
2446 *
2447 * On rare occasions the broadcast level triggered ack gets
2448 * delayed going to ioapics, and if we reprogram the
2449 * vector while Remote IRR is still set the irq will never
2450 * fire again.
2451 *
2452 * To prevent this scenario we read the Remote IRR bit
2453 * of the ioapic. This has two effects.
2454 * - On any sane system the read of the ioapic will
2455 * flush writes (and acks) going to the ioapic from
2456 * this cpu.
2457 * - We get to see if the ACK has actually been delivered.
2458 *
2459 * Based on failed experiments of reprogramming the
2460 * ioapic entry from outside of irq context starting
2461 * with masking the ioapic entry and then polling until
2462 * Remote IRR was clear before reprogramming the
2463 * ioapic I don't trust the Remote IRR bit to be
2464 * completey accurate.
2465 *
2466 * However there appears to be no other way to plug
2467 * this race, so if the Remote IRR bit is not
2468 * accurate and is causing problems then it is a hardware bug
2469 * and you can go talk to the chipset vendor about it.
2470 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002471 cfg = desc->chip_data;
2472 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002473 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002474 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002475 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002476
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002477#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002478 if (!(v & (1 << (i & 0x1f)))) {
2479 atomic_inc(&irq_mis_count);
2480 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002481 __mask_and_edge_IO_APIC_irq(cfg);
2482 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002483 spin_unlock(&ioapic_lock);
2484 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002485#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002486}
Yinghai Lu1d025192008-08-19 20:50:34 -07002487
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002488static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002489 .name = "IO-APIC",
2490 .startup = startup_ioapic_irq,
2491 .mask = mask_IO_APIC_irq,
2492 .unmask = unmask_IO_APIC_irq,
2493 .ack = ack_apic_edge,
2494 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002495#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002496 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002497#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002498 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499};
2500
Ingo Molnar54168ed2008-08-20 09:07:45 +02002501#ifdef CONFIG_INTR_REMAP
2502static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002503 .name = "IR-IO-APIC",
2504 .startup = startup_ioapic_irq,
2505 .mask = mask_IO_APIC_irq,
2506 .unmask = unmask_IO_APIC_irq,
2507 .ack = ack_x2apic_edge,
2508 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002509#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002510 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002511#endif
2512 .retrigger = ioapic_retrigger_irq,
2513};
2514#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516static inline void init_IO_APIC_traps(void)
2517{
2518 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002519 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002520 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
2522 /*
2523 * NOTE! The local APIC isn't very good at handling
2524 * multiple interrupts at the same interrupt level.
2525 * As the interrupt level is determined by taking the
2526 * vector number and shifting that right by 4, we
2527 * want to spread these out a bit so that they don't
2528 * all fall in the same interrupt level.
2529 *
2530 * Also, we've got to be careful not to trash gate
2531 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2532 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002533 for_each_irq_desc(irq, desc) {
2534 if (!desc)
2535 continue;
2536
2537 cfg = desc->chip_data;
2538 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /*
2540 * Hmm.. We don't have an entry for this,
2541 * so default to an old-fashioned 8259
2542 * interrupt if we can..
2543 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002544 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002546 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002548 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
2550 }
2551}
2552
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002553/*
2554 * The local APIC irq-chip implementation:
2555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002557static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
2559 unsigned long v;
2560
2561 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002562 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002565static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002567 unsigned long v;
2568
2569 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002570 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Yinghai Lu3145e942008-12-05 18:58:34 -08002573static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002574{
2575 ack_APIC_irq();
2576}
2577
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002578static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002579 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002580 .mask = mask_lapic_irq,
2581 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002582 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583};
2584
Yinghai Lu3145e942008-12-05 18:58:34 -08002585static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002586{
Yinghai Lu08678b02008-08-19 20:50:05 -07002587 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002588 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2589 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002590}
2591
Jan Beuliche9427102008-01-30 13:31:24 +01002592static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593{
2594 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002595 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 * We put the 8259A master into AEOI mode and
2597 * unmask on all local APICs LVT0 as NMI.
2598 *
2599 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2600 * is from Maciej W. Rozycki - so we do not have to EOI from
2601 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2604
Jan Beuliche9427102008-01-30 13:31:24 +01002605 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 apic_printk(APIC_VERBOSE, " done.\n");
2608}
2609
2610/*
2611 * This looks a bit hackish but it's about the only one way of sending
2612 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2613 * not support the ExtINT mode, unfortunately. We need to send these
2614 * cycles as some i82489DX-based boards have glue logic that keeps the
2615 * 8259A interrupt line asserted until INTA. --macro
2616 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002617static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002619 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 struct IO_APIC_route_entry entry0, entry1;
2621 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002623 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002624 if (pin == -1) {
2625 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002627 }
2628 apic = find_isa_irq_apic(8, mp_INT);
2629 if (apic == -1) {
2630 WARN_ON_ONCE(1);
2631 return;
2632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Andi Kleencf4c6a22006-09-26 10:52:30 +02002634 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002635 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 memset(&entry1, 0, sizeof(entry1));
2638
2639 entry1.dest_mode = 0; /* physical delivery */
2640 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002641 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 entry1.delivery_mode = dest_ExtINT;
2643 entry1.polarity = entry0.polarity;
2644 entry1.trigger = 0;
2645 entry1.vector = 0;
2646
Andi Kleencf4c6a22006-09-26 10:52:30 +02002647 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
2649 save_control = CMOS_READ(RTC_CONTROL);
2650 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2651 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2652 RTC_FREQ_SELECT);
2653 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2654
2655 i = 100;
2656 while (i-- > 0) {
2657 mdelay(10);
2658 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2659 i -= 10;
2660 }
2661
2662 CMOS_WRITE(save_control, RTC_CONTROL);
2663 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002664 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665
Andi Kleencf4c6a22006-09-26 10:52:30 +02002666 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667}
2668
Yinghai Luefa25592008-08-19 20:50:36 -07002669static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002670/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002671static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002672{
2673 disable_timer_pin_1 = 1;
2674 return 0;
2675}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002676early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002677
2678int timer_through_8259 __initdata;
2679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680/*
2681 * This code may look a bit paranoid, but it's supposed to cooperate with
2682 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2683 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2684 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002685 *
2686 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002688static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689{
Yinghai Lu3145e942008-12-05 18:58:34 -08002690 struct irq_desc *desc = irq_to_desc(0);
2691 struct irq_cfg *cfg = desc->chip_data;
2692 int cpu = boot_cpu_id;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002693 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002694 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002695 unsigned int ver;
2696 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002697
2698 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002699
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002700 ver = apic_read(APIC_LVR);
2701 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 /*
2704 * get/set the timer IRQ vector:
2705 */
2706 disable_8259A_irq(0);
Yinghai Lu3145e942008-12-05 18:58:34 -08002707 assign_irq_vector(0, cfg, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002710 * As IRQ0 is to be enabled in the 8259A, the virtual
2711 * wire has to be disabled in the local APIC. Also
2712 * timer interrupts need to be acknowledged manually in
2713 * the 8259A for the i82489DX when using the NMI
2714 * watchdog as that APIC treats NMIs as level-triggered.
2715 * The AEOI mode will finish them in the 8259A
2716 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002718 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002720#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002721 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002722#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002724 pin1 = find_isa_irq_pin(0, mp_INT);
2725 apic1 = find_isa_irq_apic(0, mp_INT);
2726 pin2 = ioapic_i8259.pin;
2727 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002729 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2730 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002731 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002733 /*
2734 * Some BIOS writers are clueless and report the ExtINTA
2735 * I/O APIC input from the cascaded 8259A as the timer
2736 * interrupt input. So just in case, if only one pin
2737 * was found above, try it both directly and through the
2738 * 8259A.
2739 */
2740 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002741#ifdef CONFIG_INTR_REMAP
2742 if (intr_remapping_enabled)
2743 panic("BIOS bug: timer not connected to IO-APIC");
2744#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002745 pin1 = pin2;
2746 apic1 = apic2;
2747 no_pin1 = 1;
2748 } else if (pin2 == -1) {
2749 pin2 = pin1;
2750 apic2 = apic1;
2751 }
2752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (pin1 != -1) {
2754 /*
2755 * Ok, does IRQ0 through the IOAPIC work?
2756 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002757 if (no_pin1) {
Yinghai Lu3145e942008-12-05 18:58:34 -08002758 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002759 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002760 }
Yinghai Lu3145e942008-12-05 18:58:34 -08002761 unmask_IO_APIC_irq_desc(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if (timer_irq_works()) {
2763 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 setup_nmi();
2765 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002767 if (disable_timer_pin_1 > 0)
2768 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002769 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002771#ifdef CONFIG_INTR_REMAP
2772 if (intr_remapping_enabled)
2773 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2774#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002775 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002776 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002777 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2778 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002780 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2781 "(IRQ0) through the 8259A ...\n");
2782 apic_printk(APIC_QUIET, KERN_INFO
2783 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 /*
2785 * legacy devices should be connected to IO APIC #0
2786 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002787 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002788 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Yinghai Lu3145e942008-12-05 18:58:34 -08002789 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002790 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002792 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002793 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002795 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002797 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
2801 /*
2802 * Cleanup, just in case ...
2803 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002804 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002805 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002806 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002810 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2811 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002812 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002814#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002815 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002818 apic_printk(APIC_QUIET, KERN_INFO
2819 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Yinghai Lu3145e942008-12-05 18:58:34 -08002821 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002822 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 enable_8259A_irq(0);
2824
2825 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002826 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002827 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002829 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002830 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002831 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002833 apic_printk(APIC_QUIET, KERN_INFO
2834 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 init_8259A(0);
2837 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002838 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
2840 unlock_ExtINT_logic();
2841
2842 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002843 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002846 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002848 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002849out:
2850 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
2853/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002854 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2855 * to devices. However there may be an I/O APIC pin available for
2856 * this interrupt regardless. The pin may be left unconnected, but
2857 * typically it will be reused as an ExtINT cascade interrupt for
2858 * the master 8259A. In the MPS case such a pin will normally be
2859 * reported as an ExtINT interrupt in the MP table. With ACPI
2860 * there is no provision for ExtINT interrupts, and in the absence
2861 * of an override it would be treated as an ordinary ISA I/O APIC
2862 * interrupt, that is edge-triggered and unmasked by default. We
2863 * used to do this, but it caused problems on some systems because
2864 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2865 * the same ExtINT cascade interrupt to drive the local APIC of the
2866 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2867 * the I/O APIC in all cases now. No actual device should request
2868 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 */
2870#define PIC_IRQS (1 << PIC_CASCADE_IR)
2871
2872void __init setup_IO_APIC(void)
2873{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002874
2875#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002877#else
2878 /*
2879 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2880 */
2881#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002883 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Ingo Molnar54168ed2008-08-20 09:07:45 +02002885 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002886 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002887 * Set up IO-APIC IRQ routing.
2888 */
2889#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002890 if (!acpi_ioapic)
2891 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002892#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 sync_Arb_IDs();
2894 setup_IO_APIC_irqs();
2895 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002896 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
2899/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002900 * Called after all the initialization is done. If we didnt find any
2901 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904static int __init io_apic_bug_finalize(void)
2905{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002906 if (sis_apic_bug == -1)
2907 sis_apic_bug = 0;
2908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
2911late_initcall(io_apic_bug_finalize);
2912
2913struct sysfs_ioapic_data {
2914 struct sys_device dev;
2915 struct IO_APIC_route_entry entry[0];
2916};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002917static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Pavel Machek438510f2005-04-16 15:25:24 -07002919static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 struct IO_APIC_route_entry *entry;
2922 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 data = container_of(dev, struct sysfs_ioapic_data, dev);
2926 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002927 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2928 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 return 0;
2931}
2932
2933static int ioapic_resume(struct sys_device *dev)
2934{
2935 struct IO_APIC_route_entry *entry;
2936 struct sysfs_ioapic_data *data;
2937 unsigned long flags;
2938 union IO_APIC_reg_00 reg_00;
2939 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 data = container_of(dev, struct sysfs_ioapic_data, dev);
2942 entry = data->entry;
2943
2944 spin_lock_irqsave(&ioapic_lock, flags);
2945 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002946 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2947 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 io_apic_write(dev->id, 0, reg_00.raw);
2949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002951 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002952 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
2954 return 0;
2955}
2956
2957static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002958 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 .suspend = ioapic_suspend,
2960 .resume = ioapic_resume,
2961};
2962
2963static int __init ioapic_init_sysfs(void)
2964{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002965 struct sys_device * dev;
2966 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 error = sysdev_class_register(&ioapic_sysdev_class);
2969 if (error)
2970 return error;
2971
Ingo Molnar54168ed2008-08-20 09:07:45 +02002972 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002973 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002975 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 if (!mp_ioapic_data[i]) {
2977 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2978 continue;
2979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002981 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 dev->cls = &ioapic_sysdev_class;
2983 error = sysdev_register(dev);
2984 if (error) {
2985 kfree(mp_ioapic_data[i]);
2986 mp_ioapic_data[i] = NULL;
2987 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2988 continue;
2989 }
2990 }
2991
2992 return 0;
2993}
2994
2995device_initcall(ioapic_init_sysfs);
2996
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002997/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002998 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002999 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003000unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003001{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003002 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003003 unsigned int irq;
3004 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003005 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003006 struct irq_cfg *cfg_new = NULL;
3007 int cpu = boot_cpu_id;
3008 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003009
3010 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003011 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lube5d5352008-12-05 18:58:33 -08003012 for (new = irq_want; new < NR_IRQS; new++) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003013 if (platform_legacy_irq(new))
3014 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003015
3016 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3017 if (!desc_new) {
3018 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003019 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003020 }
3021 cfg_new = desc_new->chip_data;
3022
3023 if (cfg_new->vector != 0)
3024 continue;
Yinghai Lu3145e942008-12-05 18:58:34 -08003025 if (__assign_irq_vector(new, cfg_new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003026 irq = new;
3027 break;
3028 }
3029 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003030
Yinghai Lu199751d2008-08-19 20:50:27 -07003031 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003032 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003033 /* restore it, in case dynamic_irq_init clear it */
3034 if (desc_new)
3035 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003036 }
3037 return irq;
3038}
3039
Yinghai Lube5d5352008-12-05 18:58:33 -08003040static int nr_irqs_gsi = NR_IRQS_LEGACY;
Yinghai Lu199751d2008-08-19 20:50:27 -07003041int create_irq(void)
3042{
Yinghai Lube5d5352008-12-05 18:58:33 -08003043 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003044 int irq;
3045
Yinghai Lube5d5352008-12-05 18:58:33 -08003046 irq_want = nr_irqs_gsi;
3047 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003048
3049 if (irq == 0)
3050 irq = -1;
3051
3052 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003053}
3054
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003055void destroy_irq(unsigned int irq)
3056{
3057 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003058 struct irq_cfg *cfg;
3059 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003060
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003061 /* store it, in case dynamic_irq_cleanup clear it */
3062 desc = irq_to_desc(irq);
3063 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003064 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003065 /* connect back irq_cfg */
3066 if (desc)
3067 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003068
Ingo Molnar54168ed2008-08-20 09:07:45 +02003069#ifdef CONFIG_INTR_REMAP
3070 free_irte(irq);
3071#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003072 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003073 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003074 spin_unlock_irqrestore(&vector_lock, flags);
3075}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003076
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003077/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003078 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003079 */
3080#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003081static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003082{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003083 struct irq_cfg *cfg;
3084 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003085 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003086 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003087
Yinghai Lu3145e942008-12-05 18:58:34 -08003088 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003089 tmp = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08003090 err = assign_irq_vector(irq, cfg, tmp);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003091 if (err)
3092 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003093
Yinghai Lu497c9a12008-08-19 20:50:28 -07003094 cpus_and(tmp, cfg->domain, tmp);
3095 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003096
Ingo Molnar54168ed2008-08-20 09:07:45 +02003097#ifdef CONFIG_INTR_REMAP
3098 if (irq_remapped(irq)) {
3099 struct irte irte;
3100 int ir_index;
3101 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003102
Ingo Molnar54168ed2008-08-20 09:07:45 +02003103 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3104 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003105
Ingo Molnar54168ed2008-08-20 09:07:45 +02003106 memset (&irte, 0, sizeof(irte));
3107
3108 irte.present = 1;
3109 irte.dst_mode = INT_DEST_MODE;
3110 irte.trigger_mode = 0; /* edge */
3111 irte.dlvry_mode = INT_DELIVERY_MODE;
3112 irte.vector = cfg->vector;
3113 irte.dest_id = IRTE_DEST(dest);
3114
3115 modify_irte(irq, &irte);
3116
3117 msg->address_hi = MSI_ADDR_BASE_HI;
3118 msg->data = sub_handle;
3119 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3120 MSI_ADDR_IR_SHV |
3121 MSI_ADDR_IR_INDEX1(ir_index) |
3122 MSI_ADDR_IR_INDEX2(ir_index);
3123 } else
3124#endif
3125 {
3126 msg->address_hi = MSI_ADDR_BASE_HI;
3127 msg->address_lo =
3128 MSI_ADDR_BASE_LO |
3129 ((INT_DEST_MODE == 0) ?
3130 MSI_ADDR_DEST_MODE_PHYSICAL:
3131 MSI_ADDR_DEST_MODE_LOGICAL) |
3132 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3133 MSI_ADDR_REDIRECTION_CPU:
3134 MSI_ADDR_REDIRECTION_LOWPRI) |
3135 MSI_ADDR_DEST_ID(dest);
3136
3137 msg->data =
3138 MSI_DATA_TRIGGER_EDGE |
3139 MSI_DATA_LEVEL_ASSERT |
3140 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3141 MSI_DATA_DELIVERY_FIXED:
3142 MSI_DATA_DELIVERY_LOWPRI) |
3143 MSI_DATA_VECTOR(cfg->vector);
3144 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003145 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003146}
3147
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003148#ifdef CONFIG_SMP
3149static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3150{
Yinghai Lu3145e942008-12-05 18:58:34 -08003151 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003152 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003153 struct msi_msg msg;
3154 unsigned int dest;
3155 cpumask_t tmp;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003156
3157 cpus_and(tmp, mask, cpu_online_map);
3158 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003159 return;
3160
Yinghai Lu3145e942008-12-05 18:58:34 -08003161 cfg = desc->chip_data;
3162 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003163 return;
3164
Yinghai Lu3145e942008-12-05 18:58:34 -08003165 set_extra_move_desc(desc, mask);
3166
Yinghai Lu497c9a12008-08-19 20:50:28 -07003167 cpus_and(tmp, cfg->domain, mask);
3168 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003169
Yinghai Lu3145e942008-12-05 18:58:34 -08003170 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003171
3172 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003173 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003174 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3175 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3176
Yinghai Lu3145e942008-12-05 18:58:34 -08003177 write_msi_msg_desc(desc, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003178 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003179}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003180#ifdef CONFIG_INTR_REMAP
3181/*
3182 * Migrate the MSI irq to another cpumask. This migration is
3183 * done in the process context using interrupt-remapping hardware.
3184 */
3185static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3186{
Yinghai Lu3145e942008-12-05 18:58:34 -08003187 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003188 struct irq_cfg *cfg;
3189 unsigned int dest;
3190 cpumask_t tmp, cleanup_mask;
3191 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003192
3193 cpus_and(tmp, mask, cpu_online_map);
3194 if (cpus_empty(tmp))
3195 return;
3196
3197 if (get_irte(irq, &irte))
3198 return;
3199
Yinghai Lu3145e942008-12-05 18:58:34 -08003200 cfg = desc->chip_data;
3201 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003202 return;
3203
Yinghai Lu3145e942008-12-05 18:58:34 -08003204 set_extra_move_desc(desc, mask);
3205
Ingo Molnar54168ed2008-08-20 09:07:45 +02003206 cpus_and(tmp, cfg->domain, mask);
3207 dest = cpu_mask_to_apicid(tmp);
3208
3209 irte.vector = cfg->vector;
3210 irte.dest_id = IRTE_DEST(dest);
3211
3212 /*
3213 * atomically update the IRTE with the new destination and vector.
3214 */
3215 modify_irte(irq, &irte);
3216
3217 /*
3218 * After this point, all the interrupts will start arriving
3219 * at the new destination. So, time to cleanup the previous
3220 * vector allocation.
3221 */
3222 if (cfg->move_in_progress) {
3223 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3224 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3225 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3226 cfg->move_in_progress = 0;
3227 }
3228
Ingo Molnar54168ed2008-08-20 09:07:45 +02003229 desc->affinity = mask;
3230}
Yinghai Lu3145e942008-12-05 18:58:34 -08003231
Ingo Molnar54168ed2008-08-20 09:07:45 +02003232#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003233#endif /* CONFIG_SMP */
3234
3235/*
3236 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3237 * which implement the MSI or MSI-X Capability Structure.
3238 */
3239static struct irq_chip msi_chip = {
3240 .name = "PCI-MSI",
3241 .unmask = unmask_msi_irq,
3242 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003243 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003244#ifdef CONFIG_SMP
3245 .set_affinity = set_msi_irq_affinity,
3246#endif
3247 .retrigger = ioapic_retrigger_irq,
3248};
3249
Ingo Molnar54168ed2008-08-20 09:07:45 +02003250#ifdef CONFIG_INTR_REMAP
3251static struct irq_chip msi_ir_chip = {
3252 .name = "IR-PCI-MSI",
3253 .unmask = unmask_msi_irq,
3254 .mask = mask_msi_irq,
3255 .ack = ack_x2apic_edge,
3256#ifdef CONFIG_SMP
3257 .set_affinity = ir_set_msi_irq_affinity,
3258#endif
3259 .retrigger = ioapic_retrigger_irq,
3260};
3261
3262/*
3263 * Map the PCI dev to the corresponding remapping hardware unit
3264 * and allocate 'nvec' consecutive interrupt-remapping table entries
3265 * in it.
3266 */
3267static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3268{
3269 struct intel_iommu *iommu;
3270 int index;
3271
3272 iommu = map_dev_to_ir(dev);
3273 if (!iommu) {
3274 printk(KERN_ERR
3275 "Unable to map PCI %s to iommu\n", pci_name(dev));
3276 return -ENOENT;
3277 }
3278
3279 index = alloc_irte(iommu, irq, nvec);
3280 if (index < 0) {
3281 printk(KERN_ERR
3282 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003283 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003284 return -ENOSPC;
3285 }
3286 return index;
3287}
3288#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003289
Yinghai Lu3145e942008-12-05 18:58:34 -08003290static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003291{
3292 int ret;
3293 struct msi_msg msg;
3294
3295 ret = msi_compose_msg(dev, irq, &msg);
3296 if (ret < 0)
3297 return ret;
3298
Yinghai Lu3145e942008-12-05 18:58:34 -08003299 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003300 write_msi_msg(irq, &msg);
3301
Ingo Molnar54168ed2008-08-20 09:07:45 +02003302#ifdef CONFIG_INTR_REMAP
3303 if (irq_remapped(irq)) {
3304 struct irq_desc *desc = irq_to_desc(irq);
3305 /*
3306 * irq migration in process context
3307 */
3308 desc->status |= IRQ_MOVE_PCNTXT;
3309 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3310 } else
3311#endif
3312 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003313
Yinghai Luc81bba42008-09-25 11:53:11 -07003314 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3315
Yinghai Lu1d025192008-08-19 20:50:34 -07003316 return 0;
3317}
3318
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003319int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003320{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003321 unsigned int irq;
3322 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003323 unsigned int irq_want;
3324
Yinghai Lube5d5352008-12-05 18:58:33 -08003325 irq_want = nr_irqs_gsi;
Yinghai Lu199751d2008-08-19 20:50:27 -07003326 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003327 if (irq == 0)
3328 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003329
Ingo Molnar54168ed2008-08-20 09:07:45 +02003330#ifdef CONFIG_INTR_REMAP
3331 if (!intr_remapping_enabled)
3332 goto no_ir;
3333
3334 ret = msi_alloc_irte(dev, irq, 1);
3335 if (ret < 0)
3336 goto error;
3337no_ir:
3338#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003339 ret = setup_msi_irq(dev, msidesc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003340 if (ret < 0) {
3341 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003342 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003343 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003344 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003345
3346#ifdef CONFIG_INTR_REMAP
3347error:
3348 destroy_irq(irq);
3349 return ret;
3350#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003351}
3352
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003353int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3354{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003355 unsigned int irq;
3356 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003357 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003358 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003359
Ingo Molnar54168ed2008-08-20 09:07:45 +02003360#ifdef CONFIG_INTR_REMAP
3361 struct intel_iommu *iommu = 0;
3362 int index = 0;
3363#endif
3364
Yinghai Lube5d5352008-12-05 18:58:33 -08003365 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003366 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003367 list_for_each_entry(msidesc, &dev->msi_list, list) {
3368 irq = create_irq_nr(irq_want);
Yinghai Lube5d5352008-12-05 18:58:33 -08003369 irq_want++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003370 if (irq == 0)
3371 return -1;
3372#ifdef CONFIG_INTR_REMAP
3373 if (!intr_remapping_enabled)
3374 goto no_ir;
3375
3376 if (!sub_handle) {
3377 /*
3378 * allocate the consecutive block of IRTE's
3379 * for 'nvec'
3380 */
3381 index = msi_alloc_irte(dev, irq, nvec);
3382 if (index < 0) {
3383 ret = index;
3384 goto error;
3385 }
3386 } else {
3387 iommu = map_dev_to_ir(dev);
3388 if (!iommu) {
3389 ret = -ENOENT;
3390 goto error;
3391 }
3392 /*
3393 * setup the mapping between the irq and the IRTE
3394 * base index, the sub_handle pointing to the
3395 * appropriate interrupt remap table entry.
3396 */
3397 set_irte_irq(irq, iommu, index, sub_handle);
3398 }
3399no_ir:
3400#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003401 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003402 if (ret < 0)
3403 goto error;
3404 sub_handle++;
3405 }
3406 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003407
3408error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003409 destroy_irq(irq);
3410 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003411}
3412
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003413void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003414{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003415 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003416}
3417
Ingo Molnar54168ed2008-08-20 09:07:45 +02003418#ifdef CONFIG_DMAR
3419#ifdef CONFIG_SMP
3420static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3421{
Yinghai Lu3145e942008-12-05 18:58:34 -08003422 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003423 struct irq_cfg *cfg;
3424 struct msi_msg msg;
3425 unsigned int dest;
3426 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003427
Ingo Molnar54168ed2008-08-20 09:07:45 +02003428 cpus_and(tmp, mask, cpu_online_map);
3429 if (cpus_empty(tmp))
3430 return;
3431
Yinghai Lu3145e942008-12-05 18:58:34 -08003432 cfg = desc->chip_data;
3433 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003434 return;
3435
Yinghai Lu3145e942008-12-05 18:58:34 -08003436 set_extra_move_desc(desc, mask);
3437
Ingo Molnar54168ed2008-08-20 09:07:45 +02003438 cpus_and(tmp, cfg->domain, mask);
3439 dest = cpu_mask_to_apicid(tmp);
3440
3441 dmar_msi_read(irq, &msg);
3442
3443 msg.data &= ~MSI_DATA_VECTOR_MASK;
3444 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3445 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3446 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3447
3448 dmar_msi_write(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003449 desc->affinity = mask;
3450}
Yinghai Lu3145e942008-12-05 18:58:34 -08003451
Ingo Molnar54168ed2008-08-20 09:07:45 +02003452#endif /* CONFIG_SMP */
3453
3454struct irq_chip dmar_msi_type = {
3455 .name = "DMAR_MSI",
3456 .unmask = dmar_msi_unmask,
3457 .mask = dmar_msi_mask,
3458 .ack = ack_apic_edge,
3459#ifdef CONFIG_SMP
3460 .set_affinity = dmar_msi_set_affinity,
3461#endif
3462 .retrigger = ioapic_retrigger_irq,
3463};
3464
3465int arch_setup_dmar_msi(unsigned int irq)
3466{
3467 int ret;
3468 struct msi_msg msg;
3469
3470 ret = msi_compose_msg(NULL, irq, &msg);
3471 if (ret < 0)
3472 return ret;
3473 dmar_msi_write(irq, &msg);
3474 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3475 "edge");
3476 return 0;
3477}
3478#endif
3479
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003480#ifdef CONFIG_HPET_TIMER
3481
3482#ifdef CONFIG_SMP
3483static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
3484{
Yinghai Lu3145e942008-12-05 18:58:34 -08003485 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003486 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003487 struct msi_msg msg;
3488 unsigned int dest;
3489 cpumask_t tmp;
3490
3491 cpus_and(tmp, mask, cpu_online_map);
3492 if (cpus_empty(tmp))
3493 return;
3494
Yinghai Lu3145e942008-12-05 18:58:34 -08003495 cfg = desc->chip_data;
3496 if (assign_irq_vector(irq, cfg, mask))
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003497 return;
3498
Yinghai Lu3145e942008-12-05 18:58:34 -08003499 set_extra_move_desc(desc, mask);
3500
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003501 cpus_and(tmp, cfg->domain, mask);
3502 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);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003512 desc->affinity = mask;
3513}
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
3567static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3568{
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
3574 cpus_and(tmp, mask, cpu_online_map);
3575 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003576 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003577
Yinghai Lu3145e942008-12-05 18:58:34 -08003578 cfg = desc->chip_data;
3579 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003580 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003581
Yinghai Lu3145e942008-12-05 18:58:34 -08003582 set_extra_move_desc(desc, mask);
3583
Yinghai Lu497c9a12008-08-19 20:50:28 -07003584 cpus_and(tmp, cfg->domain, mask);
3585 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003586
Yinghai Lu497c9a12008-08-19 20:50:28 -07003587 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003588 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003589}
Yinghai Lu3145e942008-12-05 18:58:34 -08003590
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003591#endif
3592
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003593static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003594 .name = "PCI-HT",
3595 .mask = mask_ht_irq,
3596 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003597 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003598#ifdef CONFIG_SMP
3599 .set_affinity = set_ht_irq_affinity,
3600#endif
3601 .retrigger = ioapic_retrigger_irq,
3602};
3603
3604int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3605{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003606 struct irq_cfg *cfg;
3607 int err;
3608 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003609
Yinghai Lu3145e942008-12-05 18:58:34 -08003610 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003611 tmp = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08003612 err = assign_irq_vector(irq, cfg, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003613 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003614 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003615 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003616
Yinghai Lu497c9a12008-08-19 20:50:28 -07003617 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003618 dest = cpu_mask_to_apicid(tmp);
3619
Eric W. Biedermanec683072006-11-08 17:44:57 -08003620 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003621
Eric W. Biedermanec683072006-11-08 17:44:57 -08003622 msg.address_lo =
3623 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003624 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003625 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003626 ((INT_DEST_MODE == 0) ?
3627 HT_IRQ_LOW_DM_PHYSICAL :
3628 HT_IRQ_LOW_DM_LOGICAL) |
3629 HT_IRQ_LOW_RQEOI_EDGE |
3630 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3631 HT_IRQ_LOW_MT_FIXED :
3632 HT_IRQ_LOW_MT_ARBITRATED) |
3633 HT_IRQ_LOW_IRQ_MASKED;
3634
Eric W. Biedermanec683072006-11-08 17:44:57 -08003635 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003636
Ingo Molnara460e742006-10-17 00:10:03 -07003637 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3638 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003639
3640 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003641 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003642 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003643}
3644#endif /* CONFIG_HT_IRQ */
3645
Dean Nelson4173a0e2008-10-02 12:18:21 -05003646#ifdef CONFIG_X86_64
3647/*
3648 * Re-target the irq to the specified CPU and enable the specified MMR located
3649 * on the specified blade to allow the sending of MSIs to the specified CPU.
3650 */
3651int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3652 unsigned long mmr_offset)
3653{
3654 const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
3655 struct irq_cfg *cfg;
3656 int mmr_pnode;
3657 unsigned long mmr_value;
3658 struct uv_IO_APIC_route_entry *entry;
3659 unsigned long flags;
3660 int err;
3661
Yinghai Lu3145e942008-12-05 18:58:34 -08003662 cfg = irq_cfg(irq);
3663
3664 err = assign_irq_vector(irq, cfg, *eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003665 if (err != 0)
3666 return err;
3667
3668 spin_lock_irqsave(&vector_lock, flags);
3669 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3670 irq_name);
3671 spin_unlock_irqrestore(&vector_lock, flags);
3672
Dean Nelson4173a0e2008-10-02 12:18:21 -05003673 mmr_value = 0;
3674 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3675 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3676
3677 entry->vector = cfg->vector;
3678 entry->delivery_mode = INT_DELIVERY_MODE;
3679 entry->dest_mode = INT_DEST_MODE;
3680 entry->polarity = 0;
3681 entry->trigger = 0;
3682 entry->mask = 0;
3683 entry->dest = cpu_mask_to_apicid(*eligible_cpu);
3684
3685 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3686 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3687
3688 return irq;
3689}
3690
3691/*
3692 * Disable the specified MMR located on the specified blade so that MSIs are
3693 * longer allowed to be sent.
3694 */
3695void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3696{
3697 unsigned long mmr_value;
3698 struct uv_IO_APIC_route_entry *entry;
3699 int mmr_pnode;
3700
3701 mmr_value = 0;
3702 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3703 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3704
3705 entry->mask = 1;
3706
3707 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3708 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3709}
3710#endif /* CONFIG_X86_64 */
3711
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003712int __init io_apic_get_redir_entries (int ioapic)
3713{
3714 union IO_APIC_reg_01 reg_01;
3715 unsigned long flags;
3716
3717 spin_lock_irqsave(&ioapic_lock, flags);
3718 reg_01.raw = io_apic_read(ioapic, 1);
3719 spin_unlock_irqrestore(&ioapic_lock, flags);
3720
3721 return reg_01.bits.entries;
3722}
3723
Yinghai Lube5d5352008-12-05 18:58:33 -08003724void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003725{
Yinghai Lube5d5352008-12-05 18:58:33 -08003726 int idx;
3727 int nr = 0;
3728
3729 for (idx = 0; idx < nr_ioapics; idx++)
3730 nr += io_apic_get_redir_entries(idx) + 1;
3731
3732 if (nr > nr_irqs_gsi)
3733 nr_irqs_gsi = nr;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003734}
3735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003737 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 -------------------------------------------------------------------------- */
3739
Len Brown888ba6c2005-08-24 12:07:20 -04003740#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Ingo Molnar54168ed2008-08-20 09:07:45 +02003742#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003743int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
3745 union IO_APIC_reg_00 reg_00;
3746 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3747 physid_mask_t tmp;
3748 unsigned long flags;
3749 int i = 0;
3750
3751 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003752 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3753 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003755 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3757 * advantage of new APIC bus architecture.
3758 */
3759
3760 if (physids_empty(apic_id_map))
3761 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3762
3763 spin_lock_irqsave(&ioapic_lock, flags);
3764 reg_00.raw = io_apic_read(ioapic, 0);
3765 spin_unlock_irqrestore(&ioapic_lock, flags);
3766
3767 if (apic_id >= get_physical_broadcast()) {
3768 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3769 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3770 apic_id = reg_00.bits.ID;
3771 }
3772
3773 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003774 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 * 'stuck on smp_invalidate_needed IPI wait' messages.
3776 */
3777 if (check_apicid_used(apic_id_map, apic_id)) {
3778
3779 for (i = 0; i < get_physical_broadcast(); i++) {
3780 if (!check_apicid_used(apic_id_map, i))
3781 break;
3782 }
3783
3784 if (i == get_physical_broadcast())
3785 panic("Max apic_id exceeded!\n");
3786
3787 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3788 "trying %d\n", ioapic, apic_id, i);
3789
3790 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 tmp = apicid_to_cpu_present(apic_id);
3794 physids_or(apic_id_map, apic_id_map, tmp);
3795
3796 if (reg_00.bits.ID != apic_id) {
3797 reg_00.bits.ID = apic_id;
3798
3799 spin_lock_irqsave(&ioapic_lock, flags);
3800 io_apic_write(ioapic, 0, reg_00.raw);
3801 reg_00.raw = io_apic_read(ioapic, 0);
3802 spin_unlock_irqrestore(&ioapic_lock, flags);
3803
3804 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003805 if (reg_00.bits.ID != apic_id) {
3806 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3807 return -1;
3808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
3810
3811 apic_printk(APIC_VERBOSE, KERN_INFO
3812 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3813
3814 return apic_id;
3815}
3816
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003817int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
3819 union IO_APIC_reg_01 reg_01;
3820 unsigned long flags;
3821
3822 spin_lock_irqsave(&ioapic_lock, flags);
3823 reg_01.raw = io_apic_read(ioapic, 1);
3824 spin_unlock_irqrestore(&ioapic_lock, flags);
3825
3826 return reg_01.bits.version;
3827}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003828#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Ingo Molnar54168ed2008-08-20 09:07:45 +02003830int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003832 struct irq_desc *desc;
3833 struct irq_cfg *cfg;
3834 int cpu = boot_cpu_id;
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003837 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 ioapic);
3839 return -EINVAL;
3840 }
3841
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003842 desc = irq_to_desc_alloc_cpu(irq, cpu);
3843 if (!desc) {
3844 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3845 return 0;
3846 }
3847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 * IRQs < 16 are already in the irq_2_pin[] map
3850 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003851 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003852 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003853 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Yinghai Lu3145e942008-12-05 18:58:34 -08003856 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
3858 return 0;
3859}
3860
Ingo Molnar54168ed2008-08-20 09:07:45 +02003861
Shaohua Li61fd47e2007-11-17 01:05:28 -05003862int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3863{
3864 int i;
3865
3866 if (skip_ioapic_setup)
3867 return -1;
3868
3869 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003870 if (mp_irqs[i].mp_irqtype == mp_INT &&
3871 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003872 break;
3873 if (i >= mp_irq_entries)
3874 return -1;
3875
3876 *trigger = irq_trigger(i);
3877 *polarity = irq_polarity(i);
3878 return 0;
3879}
3880
Len Brown888ba6c2005-08-24 12:07:20 -04003881#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003882
Yinghai Lu497c9a12008-08-19 20:50:28 -07003883/*
3884 * This function currently is only a helper for the i386 smp boot process where
3885 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3886 * so mask in all cases should simply be TARGET_CPUS
3887 */
3888#ifdef CONFIG_SMP
3889void __init setup_ioapic_dest(void)
3890{
3891 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003892 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003893 struct irq_cfg *cfg;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003894 cpumask_t mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003895
3896 if (skip_ioapic_setup == 1)
3897 return;
3898
3899 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3900 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3901 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3902 if (irq_entry == -1)
3903 continue;
3904 irq = pin_2_irq(irq_entry, ioapic, pin);
3905
3906 /* setup_IO_APIC_irqs could fail to get vector for some device
3907 * when you have too many devices, because at that time only boot
3908 * cpu is online.
3909 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003910 desc = irq_to_desc(irq);
3911 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003912 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08003913 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07003914 irq_trigger(irq_entry),
3915 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003916 continue;
3917
3918 }
3919
3920 /*
3921 * Honour affinities which have been set in early boot
3922 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003923 if (desc->status &
3924 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
3925 mask = desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003926 else
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003927 mask = TARGET_CPUS;
3928
3929#ifdef CONFIG_INTR_REMAP
3930 if (intr_remapping_enabled)
Yinghai Lu3145e942008-12-05 18:58:34 -08003931 set_ir_ioapic_affinity_irq_desc(desc, mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003932 else
3933#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08003934 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003935 }
3936
3937 }
3938}
3939#endif
3940
Ingo Molnar54168ed2008-08-20 09:07:45 +02003941#define IOAPIC_RESOURCE_NAME_SIZE 11
3942
3943static struct resource *ioapic_resources;
3944
3945static struct resource * __init ioapic_setup_resources(void)
3946{
3947 unsigned long n;
3948 struct resource *res;
3949 char *mem;
3950 int i;
3951
3952 if (nr_ioapics <= 0)
3953 return NULL;
3954
3955 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3956 n *= nr_ioapics;
3957
3958 mem = alloc_bootmem(n);
3959 res = (void *)mem;
3960
3961 if (mem != NULL) {
3962 mem += sizeof(struct resource) * nr_ioapics;
3963
3964 for (i = 0; i < nr_ioapics; i++) {
3965 res[i].name = mem;
3966 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3967 sprintf(mem, "IOAPIC %u", i);
3968 mem += IOAPIC_RESOURCE_NAME_SIZE;
3969 }
3970 }
3971
3972 ioapic_resources = res;
3973
3974 return res;
3975}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003976
Yinghai Luf3294a32008-06-27 01:41:56 -07003977void __init ioapic_init_mappings(void)
3978{
3979 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003980 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003981 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07003982
Ingo Molnar54168ed2008-08-20 09:07:45 +02003983 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003984 for (i = 0; i < nr_ioapics; i++) {
3985 if (smp_found_config) {
3986 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003987#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003988 if (!ioapic_phys) {
3989 printk(KERN_ERR
3990 "WARNING: bogus zero IO-APIC "
3991 "address found in MPTABLE, "
3992 "disabling IO/APIC support!\n");
3993 smp_found_config = 0;
3994 skip_ioapic_setup = 1;
3995 goto fake_ioapic_page;
3996 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02003997#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003998 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003999#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004000fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004001#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004002 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004003 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004004 ioapic_phys = __pa(ioapic_phys);
4005 }
4006 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004007 apic_printk(APIC_VERBOSE,
4008 "mapped IOAPIC to %08lx (%08lx)\n",
4009 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004010 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004011
Ingo Molnar54168ed2008-08-20 09:07:45 +02004012 if (ioapic_res != NULL) {
4013 ioapic_res->start = ioapic_phys;
4014 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4015 ioapic_res++;
4016 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004017 }
4018}
4019
Ingo Molnar54168ed2008-08-20 09:07:45 +02004020static int __init ioapic_insert_resources(void)
4021{
4022 int i;
4023 struct resource *r = ioapic_resources;
4024
4025 if (!r) {
4026 printk(KERN_ERR
4027 "IO APIC resources could be not be allocated.\n");
4028 return -1;
4029 }
4030
4031 for (i = 0; i < nr_ioapics; i++) {
4032 insert_resource(&iomem_resource, r);
4033 r++;
4034 }
4035
4036 return 0;
4037}
4038
4039/* Insert the IO APIC resources after PCI initialization has occured to handle
4040 * IO APICS that are mapped in on a BAR in PCI space. */
4041late_initcall(ioapic_insert_resources);