blob: 1184210e6d0cf3dc0af170d14af286d3dfa1d78b [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;
Yinghai Lua1420f32008-08-19 20:50:24 -0700111struct irq_cfg {
Yinghai Luda51a822008-08-19 20:50:25 -0700112 unsigned int irq;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700113 struct irq_pin_list *irq_2_pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700114 cpumask_t domain;
115 cpumask_t old_domain;
116 unsigned move_cleanup_count;
Yinghai Lua1420f32008-08-19 20:50:24 -0700117 u8 vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700118 u8 move_in_progress : 1;
Yinghai Lua1420f32008-08-19 20:50:24 -0700119};
120
Yinghai Lua1420f32008-08-19 20:50:24 -0700121/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200122static struct irq_cfg irq_cfgx[NR_IRQS] = {
Yinghai Lu497c9a12008-08-19 20:50:28 -0700123 [0] = { .irq = 0, .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
124 [1] = { .irq = 1, .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
125 [2] = { .irq = 2, .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
126 [3] = { .irq = 3, .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
127 [4] = { .irq = 4, .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
128 [5] = { .irq = 5, .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
129 [6] = { .irq = 6, .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
130 [7] = { .irq = 7, .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
131 [8] = { .irq = 8, .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
132 [9] = { .irq = 9, .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
133 [10] = { .irq = 10, .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
134 [11] = { .irq = 11, .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
135 [12] = { .irq = 12, .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
136 [13] = { .irq = 13, .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
137 [14] = { .irq = 14, .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
138 [15] = { .irq = 15, .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
Yinghai Lua1420f32008-08-19 20:50:24 -0700139};
140
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700141#define for_each_irq_cfg(irq, cfg) \
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200142 for (irq = 0, cfg = irq_cfgx; irq < nr_irqs; irq++, cfg++)
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700143
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200144static struct irq_cfg *irq_cfg(unsigned int irq)
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700145{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200146 return irq < nr_irqs ? irq_cfgx + irq : NULL;
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700147}
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200148
149static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700150{
Thomas Gleixner2cc21ef2008-10-15 14:16:55 +0200151 return irq_cfg(irq);
Yinghai Lu8f09cd22008-08-19 20:50:51 -0700152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200155 * Rough estimation of how many shared IRQs there are, can be changed
156 * anytime.
157 */
158#define MAX_PLUS_SHARED_IRQS NR_IRQS
159#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
160
161/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 * This is performance-critical, we want to do it O(1)
163 *
164 * the indexing order of this array favors 1:1 mappings
165 * between pins and IRQs.
166 */
167
Yinghai Lu0f978f42008-08-19 20:50:26 -0700168struct irq_pin_list {
169 int apic, pin;
170 struct irq_pin_list *next;
171};
Yinghai Lu301e6192008-08-19 20:50:02 -0700172
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200173static struct irq_pin_list irq_2_pin_head[PIN_MAP_SIZE];
Yinghai Lu0f978f42008-08-19 20:50:26 -0700174static struct irq_pin_list *irq_2_pin_ptr;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200175
176static void __init irq_2_pin_init(void)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700177{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200178 struct irq_pin_list *pin = irq_2_pin_head;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700179 int i;
180
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200181 for (i = 1; i < PIN_MAP_SIZE; i++)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700182 pin[i-1].next = &pin[i];
183
184 irq_2_pin_ptr = &pin[0];
185}
Yinghai Lu0f978f42008-08-19 20:50:26 -0700186
187static struct irq_pin_list *get_one_free_irq_2_pin(void)
188{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200189 struct irq_pin_list *pin = irq_2_pin_ptr;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700190
191 if (!pin)
192 panic("can not get more irq_2_pin\n");
193
Yinghai Lu0f978f42008-08-19 20:50:26 -0700194 irq_2_pin_ptr = pin->next;
195 pin->next = NULL;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700196 return pin;
197}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds130fe052006-11-01 09:11:00 -0800199struct io_apic {
200 unsigned int index;
201 unsigned int unused[3];
202 unsigned int data;
203};
204
205static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
206{
207 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400208 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800209}
210
211static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
212{
213 struct io_apic __iomem *io_apic = io_apic_base(apic);
214 writel(reg, &io_apic->index);
215 return readl(&io_apic->data);
216}
217
218static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
219{
220 struct io_apic __iomem *io_apic = io_apic_base(apic);
221 writel(reg, &io_apic->index);
222 writel(value, &io_apic->data);
223}
224
225/*
226 * Re-write a value: to be used for read-modify-write
227 * cycles where the read already set up the index register.
228 *
229 * Older SiS APIC requires we rewrite the index register
230 */
231static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
232{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200233 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200234
235 if (sis_apic_bug)
236 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800237 writel(value, &io_apic->data);
238}
239
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700240static bool io_apic_level_ack_pending(unsigned int irq)
241{
242 struct irq_pin_list *entry;
243 unsigned long flags;
244 struct irq_cfg *cfg = irq_cfg(irq);
245
246 spin_lock_irqsave(&ioapic_lock, flags);
247 entry = cfg->irq_2_pin;
248 for (;;) {
249 unsigned int reg;
250 int pin;
251
252 if (!entry)
253 break;
254 pin = entry->pin;
255 reg = io_apic_read(entry->apic, 0x10 + pin*2);
256 /* Is the remote IRR bit set? */
257 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
258 spin_unlock_irqrestore(&ioapic_lock, flags);
259 return true;
260 }
261 if (!entry->next)
262 break;
263 entry = entry->next;
264 }
265 spin_unlock_irqrestore(&ioapic_lock, flags);
266
267 return false;
268}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700269
Andi Kleencf4c6a22006-09-26 10:52:30 +0200270union entry_union {
271 struct { u32 w1, w2; };
272 struct IO_APIC_route_entry entry;
273};
274
275static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
276{
277 union entry_union eu;
278 unsigned long flags;
279 spin_lock_irqsave(&ioapic_lock, flags);
280 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
281 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
282 spin_unlock_irqrestore(&ioapic_lock, flags);
283 return eu.entry;
284}
285
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800286/*
287 * When we write a new IO APIC routing entry, we need to write the high
288 * word first! If the mask bit in the low word is clear, we will enable
289 * the interrupt, and we need to make sure the entry is fully populated
290 * before that happens.
291 */
Andi Kleend15512f2006-12-07 02:14:07 +0100292static void
293__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
294{
295 union entry_union eu;
296 eu.entry = e;
297 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
298 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
299}
300
Andi Kleencf4c6a22006-09-26 10:52:30 +0200301static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
302{
303 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200304 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100305 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800306 spin_unlock_irqrestore(&ioapic_lock, flags);
307}
308
309/*
310 * When we mask an IO APIC routing entry, we need to write the low
311 * word first, in order to set the mask bit before we change the
312 * high bits!
313 */
314static void ioapic_mask_entry(int apic, int pin)
315{
316 unsigned long flags;
317 union entry_union eu = { .entry.mask = 1 };
318
319 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200320 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
321 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
322 spin_unlock_irqrestore(&ioapic_lock, flags);
323}
324
Yinghai Lu497c9a12008-08-19 20:50:28 -0700325#ifdef CONFIG_SMP
326static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
327{
328 int apic, pin;
329 struct irq_cfg *cfg;
330 struct irq_pin_list *entry;
331
332 cfg = irq_cfg(irq);
333 entry = cfg->irq_2_pin;
334 for (;;) {
335 unsigned int reg;
336
337 if (!entry)
338 break;
339
340 apic = entry->apic;
341 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200342#ifdef CONFIG_INTR_REMAP
343 /*
344 * With interrupt-remapping, destination information comes
345 * from interrupt-remapping table entry.
346 */
347 if (!irq_remapped(irq))
348 io_apic_write(apic, 0x11 + pin*2, dest);
349#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700350 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200351#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700352 reg = io_apic_read(apic, 0x10 + pin*2);
353 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
354 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200355 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700356 if (!entry->next)
357 break;
358 entry = entry->next;
359 }
360}
Yinghai Luefa25592008-08-19 20:50:36 -0700361
362static int assign_irq_vector(int irq, cpumask_t mask);
363
Rusty Russell0de26522008-12-13 21:20:26 +1030364static void set_ioapic_affinity_irq(unsigned int irq,
365 const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700366{
367 struct irq_cfg *cfg;
368 unsigned long flags;
369 unsigned int dest;
370 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200371 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700372
Rusty Russell0de26522008-12-13 21:20:26 +1030373 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -0700374 return;
375
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700376 cfg = irq_cfg(irq);
Rusty Russell0de26522008-12-13 21:20:26 +1030377 if (assign_irq_vector(irq, *mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -0700378 return;
379
Rusty Russell0de26522008-12-13 21:20:26 +1030380 cpumask_and(&tmp, &cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700381 dest = cpu_mask_to_apicid(tmp);
382 /*
383 * Only the high 8 bits are valid.
384 */
385 dest = SET_APIC_LOGICAL_ID(dest);
386
Ingo Molnar54168ed2008-08-20 09:07:45 +0200387 desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700388 spin_lock_irqsave(&ioapic_lock, flags);
389 __target_IO_APIC_irq(irq, dest, cfg->vector);
Rusty Russell0de26522008-12-13 21:20:26 +1030390 cpumask_copy(&desc->affinity, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700391 spin_unlock_irqrestore(&ioapic_lock, flags);
392}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700393#endif /* CONFIG_SMP */
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/*
396 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
397 * shared ISA-space IRQs, so we have to support them. We are super
398 * fast in the common case, and fast for shared ISA-space IRQs.
399 */
400static void add_pin_to_irq(unsigned int irq, int apic, int pin)
401{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700402 struct irq_cfg *cfg;
403 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Yinghai Lu0f978f42008-08-19 20:50:26 -0700405 /* first time to refer irq_cfg, so with new */
406 cfg = irq_cfg_alloc(irq);
407 entry = cfg->irq_2_pin;
408 if (!entry) {
409 entry = get_one_free_irq_2_pin();
410 cfg->irq_2_pin = entry;
411 entry->apic = apic;
412 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700413 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700415
416 while (entry->next) {
417 /* not again, please */
418 if (entry->apic == apic && entry->pin == pin)
419 return;
420
421 entry = entry->next;
422 }
423
424 entry->next = get_one_free_irq_2_pin();
425 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 entry->apic = apic;
427 entry->pin = pin;
428}
429
430/*
431 * Reroute an IRQ to a different pin.
432 */
433static void __init replace_pin_at_irq(unsigned int irq,
434 int oldapic, int oldpin,
435 int newapic, int newpin)
436{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700437 struct irq_cfg *cfg = irq_cfg(irq);
438 struct irq_pin_list *entry = cfg->irq_2_pin;
439 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Yinghai Lu0f978f42008-08-19 20:50:26 -0700441 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (entry->apic == oldapic && entry->pin == oldpin) {
443 entry->apic = newapic;
444 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700445 replaced = 1;
446 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700448 }
449 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700451
452 /* why? call replace before add? */
453 if (!replaced)
454 add_pin_to_irq(irq, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400457static inline void io_apic_modify_irq(unsigned int irq,
458 int mask_and, int mask_or,
459 void (*final)(struct irq_pin_list *entry))
460{
461 int pin;
462 struct irq_cfg *cfg;
463 struct irq_pin_list *entry;
464
465 cfg = irq_cfg(irq);
466 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
467 unsigned int reg;
468 pin = entry->pin;
469 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
470 reg &= mask_and;
471 reg |= mask_or;
472 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
473 if (final)
474 final(entry);
475 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700476}
477
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400478static void __unmask_IO_APIC_irq(unsigned int irq)
479{
480 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED, 0, NULL);
481}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700482
483#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400484void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700485{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400486 /*
487 * Synchronize the IO-APIC and the CPU by doing
488 * a dummy read from the IO-APIC
489 */
490 struct io_apic __iomem *io_apic;
491 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700492 readl(&io_apic->data);
493}
494
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400495static void __mask_IO_APIC_irq(unsigned int irq)
496{
497 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
498}
499#else /* CONFIG_X86_32 */
500static void __mask_IO_APIC_irq(unsigned int irq)
501{
502 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, NULL);
503}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700504
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400505static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
506{
507 io_apic_modify_irq(irq, ~IO_APIC_REDIR_LEVEL_TRIGGER,
508 IO_APIC_REDIR_MASKED, NULL);
509}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700510
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400511static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
512{
513 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED,
514 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
515}
516#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700517
Ingo Molnar54168ed2008-08-20 09:07:45 +0200518static void mask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 unsigned long flags;
521
522 spin_lock_irqsave(&ioapic_lock, flags);
523 __mask_IO_APIC_irq(irq);
524 spin_unlock_irqrestore(&ioapic_lock, flags);
525}
526
Ingo Molnar54168ed2008-08-20 09:07:45 +0200527static void unmask_IO_APIC_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 unsigned long flags;
530
531 spin_lock_irqsave(&ioapic_lock, flags);
532 __unmask_IO_APIC_irq(irq);
533 spin_unlock_irqrestore(&ioapic_lock, flags);
534}
535
536static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
537{
538 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200541 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (entry.delivery_mode == dest_SMI)
543 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 /*
545 * Disable it in the IO-APIC irq-routing table:
546 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800547 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Ingo Molnar54168ed2008-08-20 09:07:45 +0200550static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 int apic, pin;
553
554 for (apic = 0; apic < nr_ioapics; apic++)
555 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
556 clear_IO_APIC_pin(apic, pin);
557}
558
Ingo Molnar54168ed2008-08-20 09:07:45 +0200559#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100560void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 unsigned int cfg;
563
564 /*
565 * Wait for idle.
566 */
567 apic_wait_icr_idle();
568 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
569 /*
570 * Send the IPI. The write to APIC_ICR fires this off.
571 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100572 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200574#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Ingo Molnar54168ed2008-08-20 09:07:45 +0200576#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*
578 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
579 * specific CPU-side IRQs.
580 */
581
582#define MAX_PIRQS 8
583static int pirq_entries [MAX_PIRQS];
584static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586static int __init ioapic_pirq_setup(char *str)
587{
588 int i, max;
589 int ints[MAX_PIRQS+1];
590
591 get_options(str, ARRAY_SIZE(ints), ints);
592
593 for (i = 0; i < MAX_PIRQS; i++)
594 pirq_entries[i] = -1;
595
596 pirqs_enabled = 1;
597 apic_printk(APIC_VERBOSE, KERN_INFO
598 "PIRQ redirection, working around broken MP-BIOS.\n");
599 max = MAX_PIRQS;
600 if (ints[0] < MAX_PIRQS)
601 max = ints[0];
602
603 for (i = 0; i < max; i++) {
604 apic_printk(APIC_VERBOSE, KERN_DEBUG
605 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
606 /*
607 * PIRQs are mapped upside down, usually.
608 */
609 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
610 }
611 return 1;
612}
613
614__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200615#endif /* CONFIG_X86_32 */
616
617#ifdef CONFIG_INTR_REMAP
618/* I/O APIC RTE contents at the OS boot up */
619static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
620
621/*
622 * Saves and masks all the unmasked IO-APIC RTE's
623 */
624int save_mask_IO_APIC_setup(void)
625{
626 union IO_APIC_reg_01 reg_01;
627 unsigned long flags;
628 int apic, pin;
629
630 /*
631 * The number of IO-APIC IRQ registers (== #pins):
632 */
633 for (apic = 0; apic < nr_ioapics; apic++) {
634 spin_lock_irqsave(&ioapic_lock, flags);
635 reg_01.raw = io_apic_read(apic, 1);
636 spin_unlock_irqrestore(&ioapic_lock, flags);
637 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
638 }
639
640 for (apic = 0; apic < nr_ioapics; apic++) {
641 early_ioapic_entries[apic] =
642 kzalloc(sizeof(struct IO_APIC_route_entry) *
643 nr_ioapic_registers[apic], GFP_KERNEL);
644 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400645 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200646 }
647
648 for (apic = 0; apic < nr_ioapics; apic++)
649 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
650 struct IO_APIC_route_entry entry;
651
652 entry = early_ioapic_entries[apic][pin] =
653 ioapic_read_entry(apic, pin);
654 if (!entry.mask) {
655 entry.mask = 1;
656 ioapic_write_entry(apic, pin, entry);
657 }
658 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400659
Ingo Molnar54168ed2008-08-20 09:07:45 +0200660 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400661
662nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400663 while (apic >= 0)
664 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400665 memset(early_ioapic_entries, 0,
666 ARRAY_SIZE(early_ioapic_entries));
667
668 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200669}
670
671void restore_IO_APIC_setup(void)
672{
673 int apic, pin;
674
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400675 for (apic = 0; apic < nr_ioapics; apic++) {
676 if (!early_ioapic_entries[apic])
677 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200678 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
679 ioapic_write_entry(apic, pin,
680 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400681 kfree(early_ioapic_entries[apic]);
682 early_ioapic_entries[apic] = NULL;
683 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200684}
685
686void reinit_intr_remapped_IO_APIC(int intr_remapping)
687{
688 /*
689 * for now plain restore of previous settings.
690 * TBD: In the case of OS enabling interrupt-remapping,
691 * IO-APIC RTE's need to be setup to point to interrupt-remapping
692 * table entries. for now, do a plain restore, and wait for
693 * the setup_IO_APIC_irqs() to do proper initialization.
694 */
695 restore_IO_APIC_setup();
696}
697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699/*
700 * Find the IRQ entry number of a certain pin.
701 */
702static int find_irq_entry(int apic, int pin, int type)
703{
704 int i;
705
706 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400707 if (mp_irqs[i].mp_irqtype == type &&
708 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
709 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
710 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return i;
712
713 return -1;
714}
715
716/*
717 * Find the pin to which IRQ[irq] (ISA) is connected
718 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800719static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 int i;
722
723 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400724 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300726 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400727 (mp_irqs[i].mp_irqtype == type) &&
728 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400730 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732 return -1;
733}
734
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800735static int __init find_isa_irq_apic(int irq, int type)
736{
737 int i;
738
739 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400740 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800741
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300742 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400743 (mp_irqs[i].mp_irqtype == type) &&
744 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800745 break;
746 }
747 if (i < mp_irq_entries) {
748 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200749 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400750 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800751 return apic;
752 }
753 }
754
755 return -1;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * Find a specific PCI IRQ entry.
760 * Not an __init, possibly needed by modules
761 */
762static int pin_2_irq(int idx, int apic, int pin);
763
764int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
765{
766 int apic, i, best_guess = -1;
767
Ingo Molnar54168ed2008-08-20 09:07:45 +0200768 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
769 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400770 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200771 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return -1;
773 }
774 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400775 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400778 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
779 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 break;
781
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300782 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400783 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400785 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200786 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 if (!(apic || IO_APIC_IRQ(irq)))
789 continue;
790
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400791 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return irq;
793 /*
794 * Use the first all-but-pin matching entry as a
795 * best-guess fuzzy result for broken mptables.
796 */
797 if (best_guess < 0)
798 best_guess = irq;
799 }
800 }
801 return best_guess;
802}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200803
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700804EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300806#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/*
808 * EISA Edge/Level control register, ELCR
809 */
810static int EISA_ELCR(unsigned int irq)
811{
812 if (irq < 16) {
813 unsigned int port = 0x4d0 + (irq >> 3);
814 return (inb(port) >> (irq & 7)) & 1;
815 }
816 apic_printk(APIC_VERBOSE, KERN_INFO
817 "Broken MPtable reports ISA irq %d\n", irq);
818 return 0;
819}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200820
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300823/* ISA interrupts are always polarity zero edge triggered,
824 * when listed as conforming in the MP table. */
825
826#define default_ISA_trigger(idx) (0)
827#define default_ISA_polarity(idx) (0)
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829/* EISA interrupts are always polarity zero and can be edge or level
830 * trigger depending on the ELCR value. If an interrupt is listed as
831 * EISA conforming in the MP table, that means its trigger type must
832 * be read in from the ELCR */
833
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400834#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300835#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837/* PCI interrupts are always polarity one level triggered,
838 * when listed as conforming in the MP table. */
839
840#define default_PCI_trigger(idx) (1)
841#define default_PCI_polarity(idx) (1)
842
843/* MCA interrupts are always polarity zero level triggered,
844 * when listed as conforming in the MP table. */
845
846#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300847#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Shaohua Li61fd47e2007-11-17 01:05:28 -0500849static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400851 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int polarity;
853
854 /*
855 * Determine IRQ line polarity (high active or low active):
856 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200857 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200859 case 0: /* conforms, ie. bus-type dependent polarity */
860 if (test_bit(bus, mp_bus_not_pci))
861 polarity = default_ISA_polarity(idx);
862 else
863 polarity = default_PCI_polarity(idx);
864 break;
865 case 1: /* high active */
866 {
867 polarity = 0;
868 break;
869 }
870 case 2: /* reserved */
871 {
872 printk(KERN_WARNING "broken BIOS!!\n");
873 polarity = 1;
874 break;
875 }
876 case 3: /* low active */
877 {
878 polarity = 1;
879 break;
880 }
881 default: /* invalid */
882 {
883 printk(KERN_WARNING "broken BIOS!!\n");
884 polarity = 1;
885 break;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 return polarity;
889}
890
891static int MPBIOS_trigger(int idx)
892{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400893 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 int trigger;
895
896 /*
897 * Determine IRQ trigger mode (edge or level sensitive):
898 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200899 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200901 case 0: /* conforms, ie. bus-type dependent */
902 if (test_bit(bus, mp_bus_not_pci))
903 trigger = default_ISA_trigger(idx);
904 else
905 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300906#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200907 switch (mp_bus_id_to_type[bus]) {
908 case MP_BUS_ISA: /* ISA pin */
909 {
910 /* set before the switch */
911 break;
912 }
913 case MP_BUS_EISA: /* EISA pin */
914 {
915 trigger = default_EISA_trigger(idx);
916 break;
917 }
918 case MP_BUS_PCI: /* PCI pin */
919 {
920 /* set before the switch */
921 break;
922 }
923 case MP_BUS_MCA: /* MCA pin */
924 {
925 trigger = default_MCA_trigger(idx);
926 break;
927 }
928 default:
929 {
930 printk(KERN_WARNING "broken BIOS!!\n");
931 trigger = 1;
932 break;
933 }
934 }
935#endif
936 break;
937 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200938 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200939 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200940 break;
941 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200942 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200943 {
944 printk(KERN_WARNING "broken BIOS!!\n");
945 trigger = 1;
946 break;
947 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200948 case 3: /* level */
949 {
950 trigger = 1;
951 break;
952 }
953 default: /* invalid */
954 {
955 printk(KERN_WARNING "broken BIOS!!\n");
956 trigger = 0;
957 break;
958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960 return trigger;
961}
962
963static inline int irq_polarity(int idx)
964{
965 return MPBIOS_polarity(idx);
966}
967
968static inline int irq_trigger(int idx)
969{
970 return MPBIOS_trigger(idx);
971}
972
Yinghai Luefa25592008-08-19 20:50:36 -0700973int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974static int pin_2_irq(int idx, int apic, int pin)
975{
976 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400977 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
979 /*
980 * Debugging check, we are in big trouble if this message pops up!
981 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400982 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
984
Ingo Molnar54168ed2008-08-20 09:07:45 +0200985 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400986 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200987 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300988 /*
989 * PCI IRQs are mapped in order
990 */
991 i = irq = 0;
992 while (i < apic)
993 irq += nr_ioapic_registers[i++];
994 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200995 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +0200996 * For MPS mode, so far only needed by ES7000 platform
997 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200998 if (ioapic_renumber_irq)
999 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001
Ingo Molnar54168ed2008-08-20 09:07:45 +02001002#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 /*
1004 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1005 */
1006 if ((pin >= 16) && (pin <= 23)) {
1007 if (pirq_entries[pin-16] != -1) {
1008 if (!pirq_entries[pin-16]) {
1009 apic_printk(APIC_VERBOSE, KERN_DEBUG
1010 "disabling PIRQ%d\n", pin-16);
1011 } else {
1012 irq = pirq_entries[pin-16];
1013 apic_printk(APIC_VERBOSE, KERN_DEBUG
1014 "using PIRQ%d -> IRQ %d\n",
1015 pin-16, irq);
1016 }
1017 }
1018 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001019#endif
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return irq;
1022}
1023
Yinghai Lu497c9a12008-08-19 20:50:28 -07001024void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001026 /* Used to the online set of cpus does not change
1027 * during assign_irq_vector.
1028 */
1029 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Yinghai Lu497c9a12008-08-19 20:50:28 -07001032void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001033{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001034 spin_unlock(&vector_lock);
1035}
1036
1037static int __assign_irq_vector(int irq, cpumask_t mask)
1038{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001039 /*
1040 * NOTE! The local APIC isn't very good at handling
1041 * multiple interrupts at the same interrupt level.
1042 * As the interrupt level is determined by taking the
1043 * vector number and shifting that right by 4, we
1044 * want to spread these out a bit so that they don't
1045 * all fall in the same interrupt level.
1046 *
1047 * Also, we've got to be careful not to trash gate
1048 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1049 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001050 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1051 unsigned int old_vector;
1052 int cpu;
1053 struct irq_cfg *cfg;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001054
Ingo Molnar54168ed2008-08-20 09:07:45 +02001055 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001056
Ingo Molnar54168ed2008-08-20 09:07:45 +02001057 /* Only try and allocate irqs on cpus that are present */
1058 cpus_and(mask, mask, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001059
Ingo Molnar54168ed2008-08-20 09:07:45 +02001060 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1061 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001062
Ingo Molnar54168ed2008-08-20 09:07:45 +02001063 old_vector = cfg->vector;
1064 if (old_vector) {
1065 cpumask_t tmp;
1066 cpus_and(tmp, cfg->domain, mask);
1067 if (!cpus_empty(tmp))
1068 return 0;
1069 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001070
Ingo Molnar54168ed2008-08-20 09:07:45 +02001071 for_each_cpu_mask_nr(cpu, mask) {
1072 cpumask_t domain, new_mask;
1073 int new_cpu;
1074 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001075
Ingo Molnar54168ed2008-08-20 09:07:45 +02001076 domain = vector_allocation_domain(cpu);
1077 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001078
Ingo Molnar54168ed2008-08-20 09:07:45 +02001079 vector = current_vector;
1080 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001081next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001082 vector += 8;
1083 if (vector >= first_system_vector) {
1084 /* If we run out of vectors on large boxen, must share them. */
1085 offset = (offset + 1) % 8;
1086 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001087 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001088 if (unlikely(current_vector == vector))
1089 continue;
1090#ifdef CONFIG_X86_64
1091 if (vector == IA32_SYSCALL_VECTOR)
1092 goto next;
1093#else
1094 if (vector == SYSCALL_VECTOR)
1095 goto next;
1096#endif
1097 for_each_cpu_mask_nr(new_cpu, new_mask)
1098 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1099 goto next;
1100 /* Found one! */
1101 current_vector = vector;
1102 current_offset = offset;
1103 if (old_vector) {
1104 cfg->move_in_progress = 1;
1105 cfg->old_domain = cfg->domain;
1106 }
1107 for_each_cpu_mask_nr(new_cpu, new_mask)
1108 per_cpu(vector_irq, new_cpu)[vector] = irq;
1109 cfg->vector = vector;
1110 cfg->domain = domain;
1111 return 0;
1112 }
1113 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001114}
1115
1116static int assign_irq_vector(int irq, cpumask_t mask)
1117{
1118 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001119 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001120
1121 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001122 err = __assign_irq_vector(irq, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001123 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001124 return err;
1125}
1126
1127static void __clear_irq_vector(int irq)
1128{
1129 struct irq_cfg *cfg;
1130 cpumask_t mask;
1131 int cpu, vector;
1132
1133 cfg = irq_cfg(irq);
1134 BUG_ON(!cfg->vector);
1135
1136 vector = cfg->vector;
1137 cpus_and(mask, cfg->domain, cpu_online_map);
1138 for_each_cpu_mask_nr(cpu, mask)
1139 per_cpu(vector_irq, cpu)[vector] = -1;
1140
1141 cfg->vector = 0;
1142 cpus_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001143
1144 if (likely(!cfg->move_in_progress))
1145 return;
1146 cpus_and(mask, cfg->old_domain, cpu_online_map);
1147 for_each_cpu_mask_nr(cpu, mask) {
1148 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1149 vector++) {
1150 if (per_cpu(vector_irq, cpu)[vector] != irq)
1151 continue;
1152 per_cpu(vector_irq, cpu)[vector] = -1;
1153 break;
1154 }
1155 }
1156 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001157}
1158
1159void __setup_vector_irq(int cpu)
1160{
1161 /* Initialize vector_irq on a new cpu */
1162 /* This function must be called with vector_lock held */
1163 int irq, vector;
1164 struct irq_cfg *cfg;
1165
1166 /* Mark the inuse vectors */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001167 for_each_irq_cfg(irq, cfg) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001168 if (!cpu_isset(cpu, cfg->domain))
1169 continue;
1170 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001171 per_cpu(vector_irq, cpu)[vector] = irq;
1172 }
1173 /* Mark the free vectors */
1174 for (vector = 0; vector < NR_VECTORS; ++vector) {
1175 irq = per_cpu(vector_irq, cpu)[vector];
1176 if (irq < 0)
1177 continue;
1178
1179 cfg = irq_cfg(irq);
1180 if (!cpu_isset(cpu, cfg->domain))
1181 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001182 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001183}
Glauber Costa3fde6902008-05-28 20:34:19 -07001184
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001185static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001186#ifdef CONFIG_INTR_REMAP
1187static struct irq_chip ir_ioapic_chip;
1188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Ingo Molnar54168ed2008-08-20 09:07:45 +02001190#define IOAPIC_AUTO -1
1191#define IOAPIC_EDGE 0
1192#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001194#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001195static inline int IO_APIC_irq_trigger(int irq)
1196{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001197 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001198
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001199 for (apic = 0; apic < nr_ioapics; apic++) {
1200 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1201 idx = find_irq_entry(apic, pin, mp_INT);
1202 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1203 return irq_trigger(idx);
1204 }
1205 }
1206 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001207 * nonexistent IRQs are edge default
1208 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001209 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001210}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001211#else
1212static inline int IO_APIC_irq_trigger(int irq)
1213{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001214 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001215}
1216#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001217
Yinghai Lu497c9a12008-08-19 20:50:28 -07001218static void ioapic_register_intr(int irq, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Yinghai Lu08678b02008-08-19 20:50:05 -07001220 struct irq_desc *desc;
1221
Thomas Gleixneree32c972008-10-15 14:34:09 +02001222 desc = irq_to_desc(irq);
Yinghai Lu199751d2008-08-19 20:50:27 -07001223
Jan Beulich6ebcc002006-06-26 13:56:46 +02001224 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001225 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001226 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001227 else
1228 desc->status &= ~IRQ_LEVEL;
1229
Ingo Molnar54168ed2008-08-20 09:07:45 +02001230#ifdef CONFIG_INTR_REMAP
1231 if (irq_remapped(irq)) {
1232 desc->status |= IRQ_MOVE_PCNTXT;
1233 if (trigger)
1234 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1235 handle_fasteoi_irq,
1236 "fasteoi");
1237 else
1238 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1239 handle_edge_irq, "edge");
1240 return;
1241 }
1242#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001243 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1244 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001245 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001246 handle_fasteoi_irq,
1247 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001248 else
Ingo Molnara460e742006-10-17 00:10:03 -07001249 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001250 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001251}
1252
1253static int setup_ioapic_entry(int apic, int irq,
1254 struct IO_APIC_route_entry *entry,
1255 unsigned int destination, int trigger,
1256 int polarity, int vector)
1257{
1258 /*
1259 * add it to the IO-APIC irq-routing table:
1260 */
1261 memset(entry,0,sizeof(*entry));
1262
Ingo Molnar54168ed2008-08-20 09:07:45 +02001263#ifdef CONFIG_INTR_REMAP
1264 if (intr_remapping_enabled) {
1265 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1266 struct irte irte;
1267 struct IR_IO_APIC_route_entry *ir_entry =
1268 (struct IR_IO_APIC_route_entry *) entry;
1269 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001270
Ingo Molnar54168ed2008-08-20 09:07:45 +02001271 if (!iommu)
1272 panic("No mapping iommu for ioapic %d\n", apic);
1273
1274 index = alloc_irte(iommu, irq, 1);
1275 if (index < 0)
1276 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1277
1278 memset(&irte, 0, sizeof(irte));
1279
1280 irte.present = 1;
1281 irte.dst_mode = INT_DEST_MODE;
1282 irte.trigger_mode = trigger;
1283 irte.dlvry_mode = INT_DELIVERY_MODE;
1284 irte.vector = vector;
1285 irte.dest_id = IRTE_DEST(destination);
1286
1287 modify_irte(irq, &irte);
1288
1289 ir_entry->index2 = (index >> 15) & 0x1;
1290 ir_entry->zero = 0;
1291 ir_entry->format = 1;
1292 ir_entry->index = (index & 0x7fff);
1293 } else
1294#endif
1295 {
1296 entry->delivery_mode = INT_DELIVERY_MODE;
1297 entry->dest_mode = INT_DEST_MODE;
1298 entry->dest = destination;
1299 }
1300
1301 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001302 entry->trigger = trigger;
1303 entry->polarity = polarity;
1304 entry->vector = vector;
1305
1306 /* Mask level triggered irqs.
1307 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1308 */
1309 if (trigger)
1310 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001311 return 0;
1312}
1313
1314static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001315 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001316{
1317 struct irq_cfg *cfg;
1318 struct IO_APIC_route_entry entry;
1319 cpumask_t mask;
1320
1321 if (!IO_APIC_IRQ(irq))
1322 return;
1323
1324 cfg = irq_cfg(irq);
1325
1326 mask = TARGET_CPUS;
1327 if (assign_irq_vector(irq, mask))
1328 return;
1329
1330 cpus_and(mask, cfg->domain, mask);
1331
1332 apic_printk(APIC_VERBOSE,KERN_DEBUG
1333 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1334 "IRQ %d Mode:%i Active:%i)\n",
1335 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1336 irq, trigger, polarity);
1337
1338
1339 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1340 cpu_mask_to_apicid(mask), trigger, polarity,
1341 cfg->vector)) {
1342 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1343 mp_ioapics[apic].mp_apicid, pin);
1344 __clear_irq_vector(irq);
1345 return;
1346 }
1347
1348 ioapic_register_intr(irq, trigger);
1349 if (irq < 16)
1350 disable_8259A_irq(irq);
1351
1352 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355static void __init setup_IO_APIC_irqs(void)
1356{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001357 int apic, pin, idx, irq;
1358 int notcon = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1361
1362 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001363 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001365 idx = find_irq_entry(apic, pin, mp_INT);
1366 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001367 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001368 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001369 apic_printk(APIC_VERBOSE,
1370 KERN_DEBUG " %d-%d",
1371 mp_ioapics[apic].mp_apicid,
1372 pin);
1373 } else
1374 apic_printk(APIC_VERBOSE, " %d-%d",
1375 mp_ioapics[apic].mp_apicid,
1376 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001377 continue;
1378 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001379 if (notcon) {
1380 apic_printk(APIC_VERBOSE,
1381 " (apicid-pin) not connected\n");
1382 notcon = 0;
1383 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001384
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001385 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001386#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001387 if (multi_timer_check(apic, irq))
1388 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001389#endif
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001390 add_pin_to_irq(irq, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001391
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001392 setup_IO_APIC_irq(apic, pin, irq,
1393 irq_trigger(idx), irq_polarity(idx));
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 }
1396
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001397 if (notcon)
1398 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001399 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
1402/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001403 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001405static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1406 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Ingo Molnar54168ed2008-08-20 09:07:45 +02001410#ifdef CONFIG_INTR_REMAP
1411 if (intr_remapping_enabled)
1412 return;
1413#endif
1414
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001415 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 /*
1418 * We use logical delivery to get the timer IRQ
1419 * to the first CPU.
1420 */
1421 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001422 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001423 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 entry.delivery_mode = INT_DELIVERY_MODE;
1425 entry.polarity = 0;
1426 entry.trigger = 0;
1427 entry.vector = vector;
1428
1429 /*
1430 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001431 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001433 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 /*
1436 * Add it to the IO-APIC irq-routing table:
1437 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001438 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439}
1440
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001441
1442__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
1444 int apic, i;
1445 union IO_APIC_reg_00 reg_00;
1446 union IO_APIC_reg_01 reg_01;
1447 union IO_APIC_reg_02 reg_02;
1448 union IO_APIC_reg_03 reg_03;
1449 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001450 struct irq_cfg *cfg;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001451 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 if (apic_verbosity == APIC_QUIET)
1454 return;
1455
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001456 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 for (i = 0; i < nr_ioapics; i++)
1458 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001459 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 /*
1462 * We are a bit conservative about what we expect. We have to
1463 * know about every hardware change ASAP.
1464 */
1465 printk(KERN_INFO "testing the IO APIC.......................\n");
1466
1467 for (apic = 0; apic < nr_ioapics; apic++) {
1468
1469 spin_lock_irqsave(&ioapic_lock, flags);
1470 reg_00.raw = io_apic_read(apic, 0);
1471 reg_01.raw = io_apic_read(apic, 1);
1472 if (reg_01.bits.version >= 0x10)
1473 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001474 if (reg_01.bits.version >= 0x20)
1475 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 spin_unlock_irqrestore(&ioapic_lock, flags);
1477
Ingo Molnar54168ed2008-08-20 09:07:45 +02001478 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001479 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1481 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1482 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1483 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Ingo Molnar54168ed2008-08-20 09:07:45 +02001485 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1489 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 /*
1492 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1493 * but the value of reg_02 is read as the previous read register
1494 * value, so ignore it if reg_02 == reg_01.
1495 */
1496 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1497 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1498 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
1500
1501 /*
1502 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1503 * or reg_03, but the value of reg_0[23] is read as the previous read
1504 * register value, so ignore it if reg_03 == reg_0[12].
1505 */
1506 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1507 reg_03.raw != reg_01.raw) {
1508 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1509 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
1511
1512 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1513
Yinghai Lud83e94a2008-08-19 20:50:33 -07001514 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1515 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
1517 for (i = 0; i <= reg_01.bits.entries; i++) {
1518 struct IO_APIC_route_entry entry;
1519
Andi Kleencf4c6a22006-09-26 10:52:30 +02001520 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Ingo Molnar54168ed2008-08-20 09:07:45 +02001522 printk(KERN_DEBUG " %02x %03X ",
1523 i,
1524 entry.dest
1525 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1528 entry.mask,
1529 entry.trigger,
1530 entry.irr,
1531 entry.polarity,
1532 entry.delivery_status,
1533 entry.dest_mode,
1534 entry.delivery_mode,
1535 entry.vector
1536 );
1537 }
1538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001540 for_each_irq_cfg(irq, cfg) {
Yinghai Lu0f978f42008-08-19 20:50:26 -07001541 struct irq_pin_list *entry = cfg->irq_2_pin;
1542 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001544 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 for (;;) {
1546 printk("-> %d:%d", entry->apic, entry->pin);
1547 if (!entry->next)
1548 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001549 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551 printk("\n");
1552 }
1553
1554 printk(KERN_INFO ".................................... done.\n");
1555
1556 return;
1557}
1558
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001559__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560{
1561 unsigned int v;
1562 int i, j;
1563
1564 if (apic_verbosity == APIC_QUIET)
1565 return;
1566
1567 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1568 for (i = 0; i < 8; i++) {
1569 v = apic_read(base + i*0x10);
1570 for (j = 0; j < 32; j++) {
1571 if (v & (1<<j))
1572 printk("1");
1573 else
1574 printk("0");
1575 }
1576 printk("\n");
1577 }
1578}
1579
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001580__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
1582 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001583 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
1585 if (apic_verbosity == APIC_QUIET)
1586 return;
1587
1588 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1589 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001590 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001591 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 v = apic_read(APIC_LVR);
1593 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1594 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001595 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 v = apic_read(APIC_TASKPRI);
1598 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1599
Ingo Molnar54168ed2008-08-20 09:07:45 +02001600 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001601 if (!APIC_XAPIC(ver)) {
1602 v = apic_read(APIC_ARBPRI);
1603 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1604 v & APIC_ARBPRI_MASK);
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 v = apic_read(APIC_PROCPRI);
1607 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1608 }
1609
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001610 /*
1611 * Remote read supported only in the 82489DX and local APIC for
1612 * Pentium processors.
1613 */
1614 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1615 v = apic_read(APIC_RRR);
1616 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1617 }
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 v = apic_read(APIC_LDR);
1620 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001621 if (!x2apic_enabled()) {
1622 v = apic_read(APIC_DFR);
1623 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 v = apic_read(APIC_SPIV);
1626 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1627
1628 printk(KERN_DEBUG "... APIC ISR field:\n");
1629 print_APIC_bitfield(APIC_ISR);
1630 printk(KERN_DEBUG "... APIC TMR field:\n");
1631 print_APIC_bitfield(APIC_TMR);
1632 printk(KERN_DEBUG "... APIC IRR field:\n");
1633 print_APIC_bitfield(APIC_IRR);
1634
Ingo Molnar54168ed2008-08-20 09:07:45 +02001635 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1636 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 v = apic_read(APIC_ESR);
1640 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1641 }
1642
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001643 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001644 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1645 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 v = apic_read(APIC_LVTT);
1648 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1649
1650 if (maxlvt > 3) { /* PC is LVT#4. */
1651 v = apic_read(APIC_LVTPC);
1652 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1653 }
1654 v = apic_read(APIC_LVT0);
1655 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1656 v = apic_read(APIC_LVT1);
1657 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1658
1659 if (maxlvt > 2) { /* ERR is LVT#3. */
1660 v = apic_read(APIC_LVTERR);
1661 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1662 }
1663
1664 v = apic_read(APIC_TMICT);
1665 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1666 v = apic_read(APIC_TMCCT);
1667 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1668 v = apic_read(APIC_TDCR);
1669 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1670 printk("\n");
1671}
1672
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001673__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001675 int cpu;
1676
1677 preempt_disable();
1678 for_each_online_cpu(cpu)
1679 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1680 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001683__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 unsigned int v;
1686 unsigned long flags;
1687
1688 if (apic_verbosity == APIC_QUIET)
1689 return;
1690
1691 printk(KERN_DEBUG "\nprinting PIC contents\n");
1692
1693 spin_lock_irqsave(&i8259A_lock, flags);
1694
1695 v = inb(0xa1) << 8 | inb(0x21);
1696 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1697
1698 v = inb(0xa0) << 8 | inb(0x20);
1699 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1700
Ingo Molnar54168ed2008-08-20 09:07:45 +02001701 outb(0x0b,0xa0);
1702 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001704 outb(0x0a,0xa0);
1705 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 spin_unlock_irqrestore(&i8259A_lock, flags);
1708
1709 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1710
1711 v = inb(0x4d1) << 8 | inb(0x4d0);
1712 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1713}
1714
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001715__apicdebuginit(int) print_all_ICs(void)
1716{
1717 print_PIC();
1718 print_all_local_APICs();
1719 print_IO_APIC();
1720
1721 return 0;
1722}
1723
1724fs_initcall(print_all_ICs);
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Yinghai Luefa25592008-08-19 20:50:36 -07001727/* Where if anywhere is the i8259 connect in external int mode */
1728static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1729
Ingo Molnar54168ed2008-08-20 09:07:45 +02001730void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
1732 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001733 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001734 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 unsigned long flags;
1736
Ingo Molnar54168ed2008-08-20 09:07:45 +02001737#ifdef CONFIG_X86_32
1738 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (!pirqs_enabled)
1740 for (i = 0; i < MAX_PIRQS; i++)
1741 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001742#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /*
1745 * The number of IO-APIC IRQ registers (== #pins):
1746 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001747 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001749 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001751 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1752 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001753 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001754 int pin;
1755 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001756 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001757 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001758 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001759
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001760 /* If the interrupt line is enabled and in ExtInt mode
1761 * I have found the pin where the i8259 is connected.
1762 */
1763 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1764 ioapic_i8259.apic = apic;
1765 ioapic_i8259.pin = pin;
1766 goto found_i8259;
1767 }
1768 }
1769 }
1770 found_i8259:
1771 /* Look to see what if the MP table has reported the ExtINT */
1772 /* If we could not find the appropriate pin by looking at the ioapic
1773 * the i8259 probably is not connected the ioapic but give the
1774 * mptable a chance anyway.
1775 */
1776 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1777 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1778 /* Trust the MP table if nothing is setup in the hardware */
1779 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1780 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1781 ioapic_i8259.pin = i8259_pin;
1782 ioapic_i8259.apic = i8259_apic;
1783 }
1784 /* Complain if the MP table and the hardware disagree */
1785 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1786 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1787 {
1788 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
1790
1791 /*
1792 * Do not trust the IO-APIC being empty at bootup
1793 */
1794 clear_IO_APIC();
1795}
1796
1797/*
1798 * Not an __init, needed by the reboot code
1799 */
1800void disable_IO_APIC(void)
1801{
1802 /*
1803 * Clear the IO-APIC before rebooting:
1804 */
1805 clear_IO_APIC();
1806
Eric W. Biederman650927e2005-06-25 14:57:44 -07001807 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001808 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001809 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001810 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001811 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001812 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001813 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001814
1815 memset(&entry, 0, sizeof(entry));
1816 entry.mask = 0; /* Enabled */
1817 entry.trigger = 0; /* Edge */
1818 entry.irr = 0;
1819 entry.polarity = 0; /* High */
1820 entry.delivery_status = 0;
1821 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001822 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001823 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001824 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001825
1826 /*
1827 * Add it to the IO-APIC irq-routing table:
1828 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001829 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001830 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001831
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001832 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834
Ingo Molnar54168ed2008-08-20 09:07:45 +02001835#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/*
1837 * function to set the IO-APIC physical IDs based on the
1838 * values stored in the MPC table.
1839 *
1840 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1841 */
1842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843static void __init setup_ioapic_ids_from_mpc(void)
1844{
1845 union IO_APIC_reg_00 reg_00;
1846 physid_mask_t phys_id_present_map;
1847 int apic;
1848 int i;
1849 unsigned char old_id;
1850 unsigned long flags;
1851
Yinghai Lua4dbc342008-07-25 02:14:28 -07001852 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001853 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001856 * Don't check I/O APIC IDs for xAPIC systems. They have
1857 * no meaning without the serial APIC bus.
1858 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001859 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1860 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001861 return;
1862 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 * This is broken; anything with a real cpu count has to
1864 * circumvent this idiocy regardless.
1865 */
1866 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1867
1868 /*
1869 * Set the IOAPIC ID to the value stored in the MPC table.
1870 */
1871 for (apic = 0; apic < nr_ioapics; apic++) {
1872
1873 /* Read the register 0 value */
1874 spin_lock_irqsave(&ioapic_lock, flags);
1875 reg_00.raw = io_apic_read(apic, 0);
1876 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001877
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001878 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001880 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001882 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1884 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001885 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
1889 * Sanity check, is the ID really free? Every APIC in a
1890 * system must have a unique ID or we get lots of nice
1891 * 'stuck on smp_invalidate_needed IPI wait' messages.
1892 */
1893 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001894 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001896 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 for (i = 0; i < get_physical_broadcast(); i++)
1898 if (!physid_isset(i, phys_id_present_map))
1899 break;
1900 if (i >= get_physical_broadcast())
1901 panic("Max APIC ID exceeded!\n");
1902 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1903 i);
1904 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001905 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 } else {
1907 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001908 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 apic_printk(APIC_VERBOSE, "Setting %d in the "
1910 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001911 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1913 }
1914
1915
1916 /*
1917 * We need to adjust the IRQ routing table
1918 * if the ID changed.
1919 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001920 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001922 if (mp_irqs[i].mp_dstapic == old_id)
1923 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001924 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
1926 /*
1927 * Read the right value from the MPC table and
1928 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 apic_printk(APIC_VERBOSE, KERN_INFO
1931 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001932 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001934 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07001936 io_apic_write(apic, 0, reg_00.raw);
1937 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /*
1940 * Sanity check
1941 */
1942 spin_lock_irqsave(&ioapic_lock, flags);
1943 reg_00.raw = io_apic_read(apic, 0);
1944 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001945 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 printk("could not set ID!\n");
1947 else
1948 apic_printk(APIC_VERBOSE, " ok.\n");
1949 }
1950}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001953int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001954
1955static int __init notimercheck(char *s)
1956{
1957 no_timer_check = 1;
1958 return 1;
1959}
1960__setup("no_timer_check", notimercheck);
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962/*
1963 * There is a nasty bug in some older SMP boards, their mptable lies
1964 * about the timer IRQ. We do the following to work around the situation:
1965 *
1966 * - timer IRQ defaults to IO-APIC IRQ
1967 * - if this function detects that timer IRQs are defunct, then we fall
1968 * back to ISA timer IRQs
1969 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001970static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
1972 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001973 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Zachary Amsden8542b202006-12-07 02:14:09 +01001975 if (no_timer_check)
1976 return 1;
1977
Ingo Molnar4aae0702007-12-18 18:05:58 +01001978 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 local_irq_enable();
1980 /* Let ten ticks pass... */
1981 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001982 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 /*
1985 * Expect a few ticks at least, to be sure some possible
1986 * glue logic does not lock up after one or two first
1987 * ticks in a non-ExtINT mode. Also the local APIC
1988 * might have cached one ExtINT interrupt. Finally, at
1989 * least one tick may be lost due to delays.
1990 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001991
1992 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01001993 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 0;
1996}
1997
1998/*
1999 * In the SMP+IOAPIC case it might happen that there are an unspecified
2000 * number of pending IRQ events unhandled. These cases are very rare,
2001 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2002 * better to do it this way as thus we do not have to be aware of
2003 * 'pending' interrupts in the IRQ path, except at this point.
2004 */
2005/*
2006 * Edge triggered needs to resend any interrupt
2007 * that was delayed but this is now handled in the device
2008 * independent code.
2009 */
2010
2011/*
2012 * Starting up a edge-triggered IO-APIC interrupt is
2013 * nasty - we need to make sure that we get the edge.
2014 * If it is already asserted for some reason, we need
2015 * return 1 to indicate that is was pending.
2016 *
2017 * This is not complete - we should be able to fake
2018 * an edge even if it isn't on the 8259A...
2019 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002020
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002021static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 int was_pending = 0;
2024 unsigned long flags;
2025
2026 spin_lock_irqsave(&ioapic_lock, flags);
2027 if (irq < 16) {
2028 disable_8259A_irq(irq);
2029 if (i8259A_irq_pending(irq))
2030 was_pending = 1;
2031 }
2032 __unmask_IO_APIC_irq(irq);
2033 spin_unlock_irqrestore(&ioapic_lock, flags);
2034
2035 return was_pending;
2036}
2037
Ingo Molnar54168ed2008-08-20 09:07:45 +02002038#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002039static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002041
2042 struct irq_cfg *cfg = irq_cfg(irq);
2043 unsigned long flags;
2044
2045 spin_lock_irqsave(&vector_lock, flags);
2046 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2047 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002048
2049 return 1;
2050}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002051#else
2052static int ioapic_retrigger_irq(unsigned int irq)
2053{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002054 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002055
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002056 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002057}
2058#endif
2059
2060/*
2061 * Level and edge triggered IO-APIC interrupts need different handling,
2062 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2063 * handled with the level-triggered descriptor, but that one has slightly
2064 * more overhead. Level-triggered interrupts cannot be handled with the
2065 * edge-triggered handler, without risking IRQ storms and other ugly
2066 * races.
2067 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002068
Yinghai Lu497c9a12008-08-19 20:50:28 -07002069#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002070
2071#ifdef CONFIG_INTR_REMAP
2072static void ir_irq_migration(struct work_struct *work);
2073
2074static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2075
2076/*
2077 * Migrate the IO-APIC irq in the presence of intr-remapping.
2078 *
2079 * For edge triggered, irq migration is a simple atomic update(of vector
2080 * and cpu destination) of IRTE and flush the hardware cache.
2081 *
2082 * For level triggered, we need to modify the io-apic RTE aswell with the update
2083 * vector information, along with modifying IRTE with vector and destination.
2084 * So irq migration for level triggered is little bit more complex compared to
2085 * edge triggered migration. But the good news is, we use the same algorithm
2086 * for level triggered migration as we have today, only difference being,
2087 * we now initiate the irq migration from process context instead of the
2088 * interrupt context.
2089 *
2090 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2091 * suppression) to the IO-APIC, level triggered irq migration will also be
2092 * as simple as edge triggered migration and we can do the irq migration
2093 * with a simple atomic update to IO-APIC RTE.
2094 */
2095static void migrate_ioapic_irq(int irq, cpumask_t mask)
2096{
2097 struct irq_cfg *cfg;
2098 struct irq_desc *desc;
2099 cpumask_t tmp, cleanup_mask;
2100 struct irte irte;
2101 int modify_ioapic_rte;
2102 unsigned int dest;
2103 unsigned long flags;
2104
2105 cpus_and(tmp, mask, cpu_online_map);
2106 if (cpus_empty(tmp))
2107 return;
2108
2109 if (get_irte(irq, &irte))
2110 return;
2111
2112 if (assign_irq_vector(irq, mask))
2113 return;
2114
2115 cfg = irq_cfg(irq);
2116 cpus_and(tmp, cfg->domain, mask);
2117 dest = cpu_mask_to_apicid(tmp);
2118
2119 desc = irq_to_desc(irq);
2120 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2121 if (modify_ioapic_rte) {
2122 spin_lock_irqsave(&ioapic_lock, flags);
2123 __target_IO_APIC_irq(irq, dest, cfg->vector);
2124 spin_unlock_irqrestore(&ioapic_lock, flags);
2125 }
2126
2127 irte.vector = cfg->vector;
2128 irte.dest_id = IRTE_DEST(dest);
2129
2130 /*
2131 * Modified the IRTE and flushes the Interrupt entry cache.
2132 */
2133 modify_irte(irq, &irte);
2134
2135 if (cfg->move_in_progress) {
2136 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2137 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2138 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2139 cfg->move_in_progress = 0;
2140 }
2141
2142 desc->affinity = mask;
2143}
2144
2145static int migrate_irq_remapped_level(int irq)
2146{
2147 int ret = -1;
2148 struct irq_desc *desc = irq_to_desc(irq);
2149
2150 mask_IO_APIC_irq(irq);
2151
2152 if (io_apic_level_ack_pending(irq)) {
2153 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002154 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002155 * vector information in the IO-APIC RTE and that will confuse
2156 * the EOI broadcast performed by cpu.
2157 * So, delay the irq migration to the next instance.
2158 */
2159 schedule_delayed_work(&ir_migration_work, 1);
2160 goto unmask;
2161 }
2162
2163 /* everthing is clear. we have right of way */
2164 migrate_ioapic_irq(irq, desc->pending_mask);
2165
2166 ret = 0;
2167 desc->status &= ~IRQ_MOVE_PENDING;
2168 cpus_clear(desc->pending_mask);
2169
2170unmask:
2171 unmask_IO_APIC_irq(irq);
2172 return ret;
2173}
2174
2175static void ir_irq_migration(struct work_struct *work)
2176{
2177 unsigned int irq;
2178 struct irq_desc *desc;
2179
2180 for_each_irq_desc(irq, desc) {
2181 if (desc->status & IRQ_MOVE_PENDING) {
2182 unsigned long flags;
2183
2184 spin_lock_irqsave(&desc->lock, flags);
2185 if (!desc->chip->set_affinity ||
2186 !(desc->status & IRQ_MOVE_PENDING)) {
2187 desc->status &= ~IRQ_MOVE_PENDING;
2188 spin_unlock_irqrestore(&desc->lock, flags);
2189 continue;
2190 }
2191
Rusty Russell0de26522008-12-13 21:20:26 +10302192 desc->chip->set_affinity(irq, &desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002193 spin_unlock_irqrestore(&desc->lock, flags);
2194 }
2195 }
2196}
2197
2198/*
2199 * Migrates the IRQ destination in the process context.
2200 */
Rusty Russell0de26522008-12-13 21:20:26 +10302201static void set_ir_ioapic_affinity_irq(unsigned int irq,
2202 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002203{
2204 struct irq_desc *desc = irq_to_desc(irq);
2205
2206 if (desc->status & IRQ_LEVEL) {
2207 desc->status |= IRQ_MOVE_PENDING;
Rusty Russell0de26522008-12-13 21:20:26 +10302208 cpumask_copy(&desc->pending_mask, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002209 migrate_irq_remapped_level(irq);
2210 return;
2211 }
2212
Rusty Russell0de26522008-12-13 21:20:26 +10302213 migrate_ioapic_irq(irq, *mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002214}
2215#endif
2216
Yinghai Lu497c9a12008-08-19 20:50:28 -07002217asmlinkage void smp_irq_move_cleanup_interrupt(void)
2218{
2219 unsigned vector, me;
2220 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002221#ifdef CONFIG_X86_64
2222 exit_idle();
2223#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002224 irq_enter();
2225
2226 me = smp_processor_id();
2227 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2228 unsigned int irq;
2229 struct irq_desc *desc;
2230 struct irq_cfg *cfg;
2231 irq = __get_cpu_var(vector_irq)[vector];
2232
2233 desc = irq_to_desc(irq);
2234 if (!desc)
2235 continue;
2236
2237 cfg = irq_cfg(irq);
2238 spin_lock(&desc->lock);
2239 if (!cfg->move_cleanup_count)
2240 goto unlock;
2241
2242 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2243 goto unlock;
2244
2245 __get_cpu_var(vector_irq)[vector] = -1;
2246 cfg->move_cleanup_count--;
2247unlock:
2248 spin_unlock(&desc->lock);
2249 }
2250
2251 irq_exit();
2252}
2253
2254static void irq_complete_move(unsigned int irq)
2255{
2256 struct irq_cfg *cfg = irq_cfg(irq);
2257 unsigned vector, me;
2258
2259 if (likely(!cfg->move_in_progress))
2260 return;
2261
2262 vector = ~get_irq_regs()->orig_ax;
2263 me = smp_processor_id();
2264 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2265 cpumask_t cleanup_mask;
2266
2267 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2268 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2269 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2270 cfg->move_in_progress = 0;
2271 }
2272}
2273#else
2274static inline void irq_complete_move(unsigned int irq) {}
2275#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002276#ifdef CONFIG_INTR_REMAP
2277static void ack_x2apic_level(unsigned int irq)
2278{
2279 ack_x2APIC_irq();
2280}
2281
2282static void ack_x2apic_edge(unsigned int irq)
2283{
2284 ack_x2APIC_irq();
2285}
2286#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002287
Yinghai Lu1d025192008-08-19 20:50:34 -07002288static void ack_apic_edge(unsigned int irq)
2289{
2290 irq_complete_move(irq);
2291 move_native_irq(irq);
2292 ack_APIC_irq();
2293}
2294
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002295atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002296
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002297static void ack_apic_level(unsigned int irq)
2298{
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002299#ifdef CONFIG_X86_32
2300 unsigned long v;
2301 int i;
2302#endif
Ingo Molnar54168ed2008-08-20 09:07:45 +02002303 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002304
Ingo Molnar54168ed2008-08-20 09:07:45 +02002305 irq_complete_move(irq);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002306#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002307 /* If we are moving the irq we need to mask it */
2308 if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2309 do_unmask_irq = 1;
2310 mask_IO_APIC_irq(irq);
2311 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002312#endif
2313
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002314#ifdef CONFIG_X86_32
2315 /*
2316 * It appears there is an erratum which affects at least version 0x11
2317 * of I/O APIC (that's the 82093AA and cores integrated into various
2318 * chipsets). Under certain conditions a level-triggered interrupt is
2319 * erroneously delivered as edge-triggered one but the respective IRR
2320 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2321 * message but it will never arrive and further interrupts are blocked
2322 * from the source. The exact reason is so far unknown, but the
2323 * phenomenon was observed when two consecutive interrupt requests
2324 * from a given source get delivered to the same CPU and the source is
2325 * temporarily disabled in between.
2326 *
2327 * A workaround is to simulate an EOI message manually. We achieve it
2328 * by setting the trigger mode to edge and then to level when the edge
2329 * trigger mode gets detected in the TMR of a local APIC for a
2330 * level-triggered interrupt. We mask the source for the time of the
2331 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2332 * The idea is from Manfred Spraul. --macro
2333 */
2334 i = irq_cfg(irq)->vector;
2335
2336 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2337#endif
2338
Ingo Molnar54168ed2008-08-20 09:07:45 +02002339 /*
2340 * We must acknowledge the irq before we move it or the acknowledge will
2341 * not propagate properly.
2342 */
2343 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002344
Ingo Molnar54168ed2008-08-20 09:07:45 +02002345 /* Now we can move and renable the irq */
2346 if (unlikely(do_unmask_irq)) {
2347 /* Only migrate the irq if the ack has been received.
2348 *
2349 * On rare occasions the broadcast level triggered ack gets
2350 * delayed going to ioapics, and if we reprogram the
2351 * vector while Remote IRR is still set the irq will never
2352 * fire again.
2353 *
2354 * To prevent this scenario we read the Remote IRR bit
2355 * of the ioapic. This has two effects.
2356 * - On any sane system the read of the ioapic will
2357 * flush writes (and acks) going to the ioapic from
2358 * this cpu.
2359 * - We get to see if the ACK has actually been delivered.
2360 *
2361 * Based on failed experiments of reprogramming the
2362 * ioapic entry from outside of irq context starting
2363 * with masking the ioapic entry and then polling until
2364 * Remote IRR was clear before reprogramming the
2365 * ioapic I don't trust the Remote IRR bit to be
2366 * completey accurate.
2367 *
2368 * However there appears to be no other way to plug
2369 * this race, so if the Remote IRR bit is not
2370 * accurate and is causing problems then it is a hardware bug
2371 * and you can go talk to the chipset vendor about it.
2372 */
2373 if (!io_apic_level_ack_pending(irq))
2374 move_masked_irq(irq);
2375 unmask_IO_APIC_irq(irq);
2376 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002377
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002378#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002379 if (!(v & (1 << (i & 0x1f)))) {
2380 atomic_inc(&irq_mis_count);
2381 spin_lock(&ioapic_lock);
2382 __mask_and_edge_IO_APIC_irq(irq);
2383 __unmask_and_level_IO_APIC_irq(irq);
2384 spin_unlock(&ioapic_lock);
2385 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002386#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002387}
Yinghai Lu1d025192008-08-19 20:50:34 -07002388
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002389static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002390 .name = "IO-APIC",
2391 .startup = startup_ioapic_irq,
2392 .mask = mask_IO_APIC_irq,
2393 .unmask = unmask_IO_APIC_irq,
2394 .ack = ack_apic_edge,
2395 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002396#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002397 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002398#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002399 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400};
2401
Ingo Molnar54168ed2008-08-20 09:07:45 +02002402#ifdef CONFIG_INTR_REMAP
2403static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002404 .name = "IR-IO-APIC",
2405 .startup = startup_ioapic_irq,
2406 .mask = mask_IO_APIC_irq,
2407 .unmask = unmask_IO_APIC_irq,
2408 .ack = ack_x2apic_edge,
2409 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002410#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002411 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002412#endif
2413 .retrigger = ioapic_retrigger_irq,
2414};
2415#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417static inline void init_IO_APIC_traps(void)
2418{
2419 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002420 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002421 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
2423 /*
2424 * NOTE! The local APIC isn't very good at handling
2425 * multiple interrupts at the same interrupt level.
2426 * As the interrupt level is determined by taking the
2427 * vector number and shifting that right by 4, we
2428 * want to spread these out a bit so that they don't
2429 * all fall in the same interrupt level.
2430 *
2431 * Also, we've got to be careful not to trash gate
2432 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2433 */
Yinghai Lu8f09cd22008-08-19 20:50:51 -07002434 for_each_irq_cfg(irq, cfg) {
Yinghai Luda51a822008-08-19 20:50:25 -07002435 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 /*
2437 * Hmm.. We don't have an entry for this,
2438 * so default to an old-fashioned 8259
2439 * interrupt if we can..
2440 */
2441 if (irq < 16)
2442 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002443 else {
2444 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002446 desc->chip = &no_irq_chip;
2447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 }
2449 }
2450}
2451
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002452/*
2453 * The local APIC irq-chip implementation:
2454 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002456static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457{
2458 unsigned long v;
2459
2460 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002461 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462}
2463
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002464static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002466 unsigned long v;
2467
2468 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002469 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
Ingo Molnar54168ed2008-08-20 09:07:45 +02002472static void ack_lapic_irq (unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002473{
2474 ack_APIC_irq();
2475}
2476
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002477static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002478 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002479 .mask = mask_lapic_irq,
2480 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002481 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482};
2483
Yinghai Lu497c9a12008-08-19 20:50:28 -07002484static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002485{
Yinghai Lu08678b02008-08-19 20:50:05 -07002486 struct irq_desc *desc;
2487
2488 desc = irq_to_desc(irq);
2489 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002490 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2491 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002492}
2493
Jan Beuliche9427102008-01-30 13:31:24 +01002494static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495{
2496 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002497 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 * We put the 8259A master into AEOI mode and
2499 * unmask on all local APICs LVT0 as NMI.
2500 *
2501 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2502 * is from Maciej W. Rozycki - so we do not have to EOI from
2503 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2506
Jan Beuliche9427102008-01-30 13:31:24 +01002507 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
2509 apic_printk(APIC_VERBOSE, " done.\n");
2510}
2511
2512/*
2513 * This looks a bit hackish but it's about the only one way of sending
2514 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2515 * not support the ExtINT mode, unfortunately. We need to send these
2516 * cycles as some i82489DX-based boards have glue logic that keeps the
2517 * 8259A interrupt line asserted until INTA. --macro
2518 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002519static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002521 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 struct IO_APIC_route_entry entry0, entry1;
2523 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002525 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002526 if (pin == -1) {
2527 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002529 }
2530 apic = find_isa_irq_apic(8, mp_INT);
2531 if (apic == -1) {
2532 WARN_ON_ONCE(1);
2533 return;
2534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Andi Kleencf4c6a22006-09-26 10:52:30 +02002536 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002537 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 memset(&entry1, 0, sizeof(entry1));
2540
2541 entry1.dest_mode = 0; /* physical delivery */
2542 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002543 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 entry1.delivery_mode = dest_ExtINT;
2545 entry1.polarity = entry0.polarity;
2546 entry1.trigger = 0;
2547 entry1.vector = 0;
2548
Andi Kleencf4c6a22006-09-26 10:52:30 +02002549 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 save_control = CMOS_READ(RTC_CONTROL);
2552 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2553 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2554 RTC_FREQ_SELECT);
2555 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2556
2557 i = 100;
2558 while (i-- > 0) {
2559 mdelay(10);
2560 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2561 i -= 10;
2562 }
2563
2564 CMOS_WRITE(save_control, RTC_CONTROL);
2565 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002566 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Andi Kleencf4c6a22006-09-26 10:52:30 +02002568 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
Yinghai Luefa25592008-08-19 20:50:36 -07002571static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002572/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002573static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002574{
2575 disable_timer_pin_1 = 1;
2576 return 0;
2577}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002578early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002579
2580int timer_through_8259 __initdata;
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582/*
2583 * This code may look a bit paranoid, but it's supposed to cooperate with
2584 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2585 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2586 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002587 *
2588 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002590static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002592 struct irq_cfg *cfg = irq_cfg(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002593 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002594 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002595 unsigned int ver;
2596 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002597
2598 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002599
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002600 ver = apic_read(APIC_LVR);
2601 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002602
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 /*
2604 * get/set the timer IRQ vector:
2605 */
2606 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002607 assign_irq_vector(0, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002610 * As IRQ0 is to be enabled in the 8259A, the virtual
2611 * wire has to be disabled in the local APIC. Also
2612 * timer interrupts need to be acknowledged manually in
2613 * the 8259A for the i82489DX when using the NMI
2614 * watchdog as that APIC treats NMIs as level-triggered.
2615 * The AEOI mode will finish them in the 8259A
2616 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002618 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002620#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002621 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002622#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002624 pin1 = find_isa_irq_pin(0, mp_INT);
2625 apic1 = find_isa_irq_apic(0, mp_INT);
2626 pin2 = ioapic_i8259.pin;
2627 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002629 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2630 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002631 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002633 /*
2634 * Some BIOS writers are clueless and report the ExtINTA
2635 * I/O APIC input from the cascaded 8259A as the timer
2636 * interrupt input. So just in case, if only one pin
2637 * was found above, try it both directly and through the
2638 * 8259A.
2639 */
2640 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002641#ifdef CONFIG_INTR_REMAP
2642 if (intr_remapping_enabled)
2643 panic("BIOS bug: timer not connected to IO-APIC");
2644#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002645 pin1 = pin2;
2646 apic1 = apic2;
2647 no_pin1 = 1;
2648 } else if (pin2 == -1) {
2649 pin2 = pin1;
2650 apic2 = apic1;
2651 }
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (pin1 != -1) {
2654 /*
2655 * Ok, does IRQ0 through the IOAPIC work?
2656 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002657 if (no_pin1) {
2658 add_pin_to_irq(0, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002659 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 unmask_IO_APIC_irq(0);
2662 if (timer_irq_works()) {
2663 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 setup_nmi();
2665 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002667 if (disable_timer_pin_1 > 0)
2668 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002669 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002671#ifdef CONFIG_INTR_REMAP
2672 if (intr_remapping_enabled)
2673 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2674#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002675 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002676 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002677 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2678 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002680 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2681 "(IRQ0) through the 8259A ...\n");
2682 apic_printk(APIC_QUIET, KERN_INFO
2683 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 /*
2685 * legacy devices should be connected to IO APIC #0
2686 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002687 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002688 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002689 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002690 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002692 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002693 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002695 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002697 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002699 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
2701 /*
2702 * Cleanup, just in case ...
2703 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002704 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002705 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002706 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708
2709 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002710 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2711 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002712 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002714#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002715 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002716#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002718 apic_printk(APIC_QUIET, KERN_INFO
2719 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Yinghai Lu497c9a12008-08-19 20:50:28 -07002721 lapic_register_intr(0);
2722 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 enable_8259A_irq(0);
2724
2725 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002726 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002727 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002729 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002730 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002731 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002733 apic_printk(APIC_QUIET, KERN_INFO
2734 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 init_8259A(0);
2737 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002738 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 unlock_ExtINT_logic();
2741
2742 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002743 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002744 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002746 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002748 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002749out:
2750 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
2753/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002754 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2755 * to devices. However there may be an I/O APIC pin available for
2756 * this interrupt regardless. The pin may be left unconnected, but
2757 * typically it will be reused as an ExtINT cascade interrupt for
2758 * the master 8259A. In the MPS case such a pin will normally be
2759 * reported as an ExtINT interrupt in the MP table. With ACPI
2760 * there is no provision for ExtINT interrupts, and in the absence
2761 * of an override it would be treated as an ordinary ISA I/O APIC
2762 * interrupt, that is edge-triggered and unmasked by default. We
2763 * used to do this, but it caused problems on some systems because
2764 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2765 * the same ExtINT cascade interrupt to drive the local APIC of the
2766 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2767 * the I/O APIC in all cases now. No actual device should request
2768 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 */
2770#define PIC_IRQS (1 << PIC_CASCADE_IR)
2771
2772void __init setup_IO_APIC(void)
2773{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002774
2775#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002777#else
2778 /*
2779 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2780 */
2781#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002783 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Ingo Molnar54168ed2008-08-20 09:07:45 +02002785 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002786 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002787 * Set up IO-APIC IRQ routing.
2788 */
2789#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002790 if (!acpi_ioapic)
2791 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002792#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 sync_Arb_IDs();
2794 setup_IO_APIC_irqs();
2795 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002796 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
2798
2799/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002800 * Called after all the initialization is done. If we didnt find any
2801 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804static int __init io_apic_bug_finalize(void)
2805{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002806 if (sis_apic_bug == -1)
2807 sis_apic_bug = 0;
2808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809}
2810
2811late_initcall(io_apic_bug_finalize);
2812
2813struct sysfs_ioapic_data {
2814 struct sys_device dev;
2815 struct IO_APIC_route_entry entry[0];
2816};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002817static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Pavel Machek438510f2005-04-16 15:25:24 -07002819static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 struct IO_APIC_route_entry *entry;
2822 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 data = container_of(dev, struct sysfs_ioapic_data, dev);
2826 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002827 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2828 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
2830 return 0;
2831}
2832
2833static int ioapic_resume(struct sys_device *dev)
2834{
2835 struct IO_APIC_route_entry *entry;
2836 struct sysfs_ioapic_data *data;
2837 unsigned long flags;
2838 union IO_APIC_reg_00 reg_00;
2839 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 data = container_of(dev, struct sysfs_ioapic_data, dev);
2842 entry = data->entry;
2843
2844 spin_lock_irqsave(&ioapic_lock, flags);
2845 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002846 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2847 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 io_apic_write(dev->id, 0, reg_00.raw);
2849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002851 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002852 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 return 0;
2855}
2856
2857static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002858 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 .suspend = ioapic_suspend,
2860 .resume = ioapic_resume,
2861};
2862
2863static int __init ioapic_init_sysfs(void)
2864{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002865 struct sys_device * dev;
2866 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 error = sysdev_class_register(&ioapic_sysdev_class);
2869 if (error)
2870 return error;
2871
Ingo Molnar54168ed2008-08-20 09:07:45 +02002872 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002873 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002875 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 if (!mp_ioapic_data[i]) {
2877 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2878 continue;
2879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002881 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 dev->cls = &ioapic_sysdev_class;
2883 error = sysdev_register(dev);
2884 if (error) {
2885 kfree(mp_ioapic_data[i]);
2886 mp_ioapic_data[i] = NULL;
2887 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2888 continue;
2889 }
2890 }
2891
2892 return 0;
2893}
2894
2895device_initcall(ioapic_init_sysfs);
2896
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002897/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002898 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002899 */
Yinghai Lu199751d2008-08-19 20:50:27 -07002900unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002901{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002902 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002903 unsigned int irq;
2904 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002905 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07002906 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002907
Yinghai Lu199751d2008-08-19 20:50:27 -07002908 irq_want = nr_irqs - 1;
2909
2910 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002911 spin_lock_irqsave(&vector_lock, flags);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002912 for (new = irq_want; new > 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002913 if (platform_legacy_irq(new))
2914 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07002915 cfg_new = irq_cfg(new);
2916 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002917 continue;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002918 /* check if need to create one */
Yinghai Luda51a822008-08-19 20:50:25 -07002919 if (!cfg_new)
2920 cfg_new = irq_cfg_alloc(new);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002921 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002922 irq = new;
2923 break;
2924 }
2925 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002926
Yinghai Lu199751d2008-08-19 20:50:27 -07002927 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002928 dynamic_irq_init(irq);
2929 }
2930 return irq;
2931}
2932
Yinghai Lu199751d2008-08-19 20:50:27 -07002933int create_irq(void)
2934{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002935 int irq;
2936
2937 irq = create_irq_nr(nr_irqs - 1);
2938
2939 if (irq == 0)
2940 irq = -1;
2941
2942 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07002943}
2944
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002945void destroy_irq(unsigned int irq)
2946{
2947 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002948
2949 dynamic_irq_cleanup(irq);
2950
Ingo Molnar54168ed2008-08-20 09:07:45 +02002951#ifdef CONFIG_INTR_REMAP
2952 free_irte(irq);
2953#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002954 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002955 __clear_irq_vector(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002956 spin_unlock_irqrestore(&vector_lock, flags);
2957}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002958
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002959/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002960 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002961 */
2962#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002963static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002964{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002965 struct irq_cfg *cfg;
2966 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002967 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002968 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002969
Yinghai Lu497c9a12008-08-19 20:50:28 -07002970 tmp = TARGET_CPUS;
2971 err = assign_irq_vector(irq, tmp);
2972 if (err)
2973 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002974
Yinghai Lu497c9a12008-08-19 20:50:28 -07002975 cfg = irq_cfg(irq);
2976 cpus_and(tmp, cfg->domain, tmp);
2977 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002978
Ingo Molnar54168ed2008-08-20 09:07:45 +02002979#ifdef CONFIG_INTR_REMAP
2980 if (irq_remapped(irq)) {
2981 struct irte irte;
2982 int ir_index;
2983 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002984
Ingo Molnar54168ed2008-08-20 09:07:45 +02002985 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
2986 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002987
Ingo Molnar54168ed2008-08-20 09:07:45 +02002988 memset (&irte, 0, sizeof(irte));
2989
2990 irte.present = 1;
2991 irte.dst_mode = INT_DEST_MODE;
2992 irte.trigger_mode = 0; /* edge */
2993 irte.dlvry_mode = INT_DELIVERY_MODE;
2994 irte.vector = cfg->vector;
2995 irte.dest_id = IRTE_DEST(dest);
2996
2997 modify_irte(irq, &irte);
2998
2999 msg->address_hi = MSI_ADDR_BASE_HI;
3000 msg->data = sub_handle;
3001 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3002 MSI_ADDR_IR_SHV |
3003 MSI_ADDR_IR_INDEX1(ir_index) |
3004 MSI_ADDR_IR_INDEX2(ir_index);
3005 } else
3006#endif
3007 {
3008 msg->address_hi = MSI_ADDR_BASE_HI;
3009 msg->address_lo =
3010 MSI_ADDR_BASE_LO |
3011 ((INT_DEST_MODE == 0) ?
3012 MSI_ADDR_DEST_MODE_PHYSICAL:
3013 MSI_ADDR_DEST_MODE_LOGICAL) |
3014 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3015 MSI_ADDR_REDIRECTION_CPU:
3016 MSI_ADDR_REDIRECTION_LOWPRI) |
3017 MSI_ADDR_DEST_ID(dest);
3018
3019 msg->data =
3020 MSI_DATA_TRIGGER_EDGE |
3021 MSI_DATA_LEVEL_ASSERT |
3022 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3023 MSI_DATA_DELIVERY_FIXED:
3024 MSI_DATA_DELIVERY_LOWPRI) |
3025 MSI_DATA_VECTOR(cfg->vector);
3026 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003027 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003028}
3029
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003030#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303031static void set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003032{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003033 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003034 struct msi_msg msg;
3035 unsigned int dest;
3036 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003037 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003038
Rusty Russell0de26522008-12-13 21:20:26 +10303039 if (!cpumask_intersects(mask, cpu_online_mask))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003040 return;
3041
Rusty Russell0de26522008-12-13 21:20:26 +10303042 if (assign_irq_vector(irq, *mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003043 return;
3044
3045 cfg = irq_cfg(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303046 cpumask_and(&tmp, &cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003047 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003048
3049 read_msi_msg(irq, &msg);
3050
3051 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003052 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003053 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3054 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3055
3056 write_msi_msg(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003057 desc = irq_to_desc(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303058 cpumask_copy(&desc->affinity, mask);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003059}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003060
3061#ifdef CONFIG_INTR_REMAP
3062/*
3063 * Migrate the MSI irq to another cpumask. This migration is
3064 * done in the process context using interrupt-remapping hardware.
3065 */
Rusty Russell0de26522008-12-13 21:20:26 +10303066static void ir_set_msi_irq_affinity(unsigned int irq,
3067 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003068{
3069 struct irq_cfg *cfg;
3070 unsigned int dest;
3071 cpumask_t tmp, cleanup_mask;
3072 struct irte irte;
3073 struct irq_desc *desc;
3074
Rusty Russell0de26522008-12-13 21:20:26 +10303075 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003076 return;
3077
3078 if (get_irte(irq, &irte))
3079 return;
3080
Rusty Russell0de26522008-12-13 21:20:26 +10303081 if (assign_irq_vector(irq, *mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003082 return;
3083
3084 cfg = irq_cfg(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303085 cpumask_and(&tmp, &cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003086 dest = cpu_mask_to_apicid(tmp);
3087
3088 irte.vector = cfg->vector;
3089 irte.dest_id = IRTE_DEST(dest);
3090
3091 /*
3092 * atomically update the IRTE with the new destination and vector.
3093 */
3094 modify_irte(irq, &irte);
3095
3096 /*
3097 * After this point, all the interrupts will start arriving
3098 * at the new destination. So, time to cleanup the previous
3099 * vector allocation.
3100 */
3101 if (cfg->move_in_progress) {
3102 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3103 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3104 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3105 cfg->move_in_progress = 0;
3106 }
3107
3108 desc = irq_to_desc(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303109 cpumask_copy(&desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003110}
3111#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003112#endif /* CONFIG_SMP */
3113
3114/*
3115 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3116 * which implement the MSI or MSI-X Capability Structure.
3117 */
3118static struct irq_chip msi_chip = {
3119 .name = "PCI-MSI",
3120 .unmask = unmask_msi_irq,
3121 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003122 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003123#ifdef CONFIG_SMP
3124 .set_affinity = set_msi_irq_affinity,
3125#endif
3126 .retrigger = ioapic_retrigger_irq,
3127};
3128
Ingo Molnar54168ed2008-08-20 09:07:45 +02003129#ifdef CONFIG_INTR_REMAP
3130static struct irq_chip msi_ir_chip = {
3131 .name = "IR-PCI-MSI",
3132 .unmask = unmask_msi_irq,
3133 .mask = mask_msi_irq,
3134 .ack = ack_x2apic_edge,
3135#ifdef CONFIG_SMP
3136 .set_affinity = ir_set_msi_irq_affinity,
3137#endif
3138 .retrigger = ioapic_retrigger_irq,
3139};
3140
3141/*
3142 * Map the PCI dev to the corresponding remapping hardware unit
3143 * and allocate 'nvec' consecutive interrupt-remapping table entries
3144 * in it.
3145 */
3146static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3147{
3148 struct intel_iommu *iommu;
3149 int index;
3150
3151 iommu = map_dev_to_ir(dev);
3152 if (!iommu) {
3153 printk(KERN_ERR
3154 "Unable to map PCI %s to iommu\n", pci_name(dev));
3155 return -ENOENT;
3156 }
3157
3158 index = alloc_irte(iommu, irq, nvec);
3159 if (index < 0) {
3160 printk(KERN_ERR
3161 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003162 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003163 return -ENOSPC;
3164 }
3165 return index;
3166}
3167#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003168
3169static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3170{
3171 int ret;
3172 struct msi_msg msg;
3173
3174 ret = msi_compose_msg(dev, irq, &msg);
3175 if (ret < 0)
3176 return ret;
3177
3178 set_irq_msi(irq, desc);
3179 write_msi_msg(irq, &msg);
3180
Ingo Molnar54168ed2008-08-20 09:07:45 +02003181#ifdef CONFIG_INTR_REMAP
3182 if (irq_remapped(irq)) {
3183 struct irq_desc *desc = irq_to_desc(irq);
3184 /*
3185 * irq migration in process context
3186 */
3187 desc->status |= IRQ_MOVE_PCNTXT;
3188 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3189 } else
3190#endif
3191 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003192
Yinghai Luc81bba42008-09-25 11:53:11 -07003193 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3194
Yinghai Lu1d025192008-08-19 20:50:34 -07003195 return 0;
3196}
3197
Yinghai Lu199751d2008-08-19 20:50:27 -07003198static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3199{
3200 unsigned int irq;
3201
3202 irq = dev->bus->number;
3203 irq <<= 8;
3204 irq |= dev->devfn;
3205 irq <<= 12;
3206
3207 return irq;
3208}
3209
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003210int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003211{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003212 unsigned int irq;
3213 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003214 unsigned int irq_want;
3215
3216 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3217
3218 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003219 if (irq == 0)
3220 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003221
Ingo Molnar54168ed2008-08-20 09:07:45 +02003222#ifdef CONFIG_INTR_REMAP
3223 if (!intr_remapping_enabled)
3224 goto no_ir;
3225
3226 ret = msi_alloc_irte(dev, irq, 1);
3227 if (ret < 0)
3228 goto error;
3229no_ir:
3230#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003231 ret = setup_msi_irq(dev, desc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003232 if (ret < 0) {
3233 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003234 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003235 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003236 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003237
3238#ifdef CONFIG_INTR_REMAP
3239error:
3240 destroy_irq(irq);
3241 return ret;
3242#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003243}
3244
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003245int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3246{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003247 unsigned int irq;
3248 int ret, sub_handle;
3249 struct msi_desc *desc;
3250 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003251
Ingo Molnar54168ed2008-08-20 09:07:45 +02003252#ifdef CONFIG_INTR_REMAP
3253 struct intel_iommu *iommu = 0;
3254 int index = 0;
3255#endif
3256
3257 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3258 sub_handle = 0;
3259 list_for_each_entry(desc, &dev->msi_list, list) {
3260 irq = create_irq_nr(irq_want--);
3261 if (irq == 0)
3262 return -1;
3263#ifdef CONFIG_INTR_REMAP
3264 if (!intr_remapping_enabled)
3265 goto no_ir;
3266
3267 if (!sub_handle) {
3268 /*
3269 * allocate the consecutive block of IRTE's
3270 * for 'nvec'
3271 */
3272 index = msi_alloc_irte(dev, irq, nvec);
3273 if (index < 0) {
3274 ret = index;
3275 goto error;
3276 }
3277 } else {
3278 iommu = map_dev_to_ir(dev);
3279 if (!iommu) {
3280 ret = -ENOENT;
3281 goto error;
3282 }
3283 /*
3284 * setup the mapping between the irq and the IRTE
3285 * base index, the sub_handle pointing to the
3286 * appropriate interrupt remap table entry.
3287 */
3288 set_irte_irq(irq, iommu, index, sub_handle);
3289 }
3290no_ir:
3291#endif
3292 ret = setup_msi_irq(dev, desc, irq);
3293 if (ret < 0)
3294 goto error;
3295 sub_handle++;
3296 }
3297 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003298
3299error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003300 destroy_irq(irq);
3301 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003302}
3303
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003304void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003305{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003306 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003307}
3308
Ingo Molnar54168ed2008-08-20 09:07:45 +02003309#ifdef CONFIG_DMAR
3310#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303311static void dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003312{
3313 struct irq_cfg *cfg;
3314 struct msi_msg msg;
3315 unsigned int dest;
3316 cpumask_t tmp;
3317 struct irq_desc *desc;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003318
Rusty Russell0de26522008-12-13 21:20:26 +10303319 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003320 return;
3321
Rusty Russell0de26522008-12-13 21:20:26 +10303322 if (assign_irq_vector(irq, *mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003323 return;
3324
3325 cfg = irq_cfg(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303326 cpumask_and(&tmp, &cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003327 dest = cpu_mask_to_apicid(tmp);
3328
3329 dmar_msi_read(irq, &msg);
3330
3331 msg.data &= ~MSI_DATA_VECTOR_MASK;
3332 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3333 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3334 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3335
3336 dmar_msi_write(irq, &msg);
3337 desc = irq_to_desc(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303338 cpumask_copy(&desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003339}
3340#endif /* CONFIG_SMP */
3341
3342struct irq_chip dmar_msi_type = {
3343 .name = "DMAR_MSI",
3344 .unmask = dmar_msi_unmask,
3345 .mask = dmar_msi_mask,
3346 .ack = ack_apic_edge,
3347#ifdef CONFIG_SMP
3348 .set_affinity = dmar_msi_set_affinity,
3349#endif
3350 .retrigger = ioapic_retrigger_irq,
3351};
3352
3353int arch_setup_dmar_msi(unsigned int irq)
3354{
3355 int ret;
3356 struct msi_msg msg;
3357
3358 ret = msi_compose_msg(NULL, irq, &msg);
3359 if (ret < 0)
3360 return ret;
3361 dmar_msi_write(irq, &msg);
3362 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3363 "edge");
3364 return 0;
3365}
3366#endif
3367
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003368#ifdef CONFIG_HPET_TIMER
3369
3370#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303371static void hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003372{
3373 struct irq_cfg *cfg;
3374 struct irq_desc *desc;
3375 struct msi_msg msg;
3376 unsigned int dest;
3377 cpumask_t tmp;
3378
Rusty Russell0de26522008-12-13 21:20:26 +10303379 if (!cpumask_intersects(mask, cpu_online_mask))
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003380 return;
3381
Rusty Russell0de26522008-12-13 21:20:26 +10303382 if (assign_irq_vector(irq, *mask))
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003383 return;
3384
3385 cfg = irq_cfg(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303386 cpumask_and(&tmp, &cfg->domain, mask);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003387 dest = cpu_mask_to_apicid(tmp);
3388
3389 hpet_msi_read(irq, &msg);
3390
3391 msg.data &= ~MSI_DATA_VECTOR_MASK;
3392 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3393 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3394 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3395
3396 hpet_msi_write(irq, &msg);
3397 desc = irq_to_desc(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303398 cpumask_copy(&desc->affinity, mask);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003399}
3400#endif /* CONFIG_SMP */
3401
3402struct irq_chip hpet_msi_type = {
3403 .name = "HPET_MSI",
3404 .unmask = hpet_msi_unmask,
3405 .mask = hpet_msi_mask,
3406 .ack = ack_apic_edge,
3407#ifdef CONFIG_SMP
3408 .set_affinity = hpet_msi_set_affinity,
3409#endif
3410 .retrigger = ioapic_retrigger_irq,
3411};
3412
3413int arch_setup_hpet_msi(unsigned int irq)
3414{
3415 int ret;
3416 struct msi_msg msg;
3417
3418 ret = msi_compose_msg(NULL, irq, &msg);
3419 if (ret < 0)
3420 return ret;
3421
3422 hpet_msi_write(irq, &msg);
3423 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3424 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003425
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003426 return 0;
3427}
3428#endif
3429
Ingo Molnar54168ed2008-08-20 09:07:45 +02003430#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003431/*
3432 * Hypertransport interrupt support
3433 */
3434#ifdef CONFIG_HT_IRQ
3435
3436#ifdef CONFIG_SMP
3437
Yinghai Lu497c9a12008-08-19 20:50:28 -07003438static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003439{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003440 struct ht_irq_msg msg;
3441 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003442
Yinghai Lu497c9a12008-08-19 20:50:28 -07003443 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003444 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003445
Yinghai Lu497c9a12008-08-19 20:50:28 -07003446 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003447 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003448
Eric W. Biedermanec683072006-11-08 17:44:57 -08003449 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003450}
3451
Rusty Russell0de26522008-12-13 21:20:26 +10303452static void set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003453{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003454 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003455 unsigned int dest;
3456 cpumask_t tmp;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003457 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003458
Rusty Russell0de26522008-12-13 21:20:26 +10303459 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003460 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003461
Rusty Russell0de26522008-12-13 21:20:26 +10303462 if (assign_irq_vector(irq, *mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003463 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003464
Yinghai Lu497c9a12008-08-19 20:50:28 -07003465 cfg = irq_cfg(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303466 cpumask_and(&tmp, &cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003467 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003468
Yinghai Lu497c9a12008-08-19 20:50:28 -07003469 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003470 desc = irq_to_desc(irq);
Rusty Russell0de26522008-12-13 21:20:26 +10303471 cpumask_copy(&desc->affinity, mask);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003472}
3473#endif
3474
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003475static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003476 .name = "PCI-HT",
3477 .mask = mask_ht_irq,
3478 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003479 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003480#ifdef CONFIG_SMP
3481 .set_affinity = set_ht_irq_affinity,
3482#endif
3483 .retrigger = ioapic_retrigger_irq,
3484};
3485
3486int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3487{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003488 struct irq_cfg *cfg;
3489 int err;
3490 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003491
Yinghai Lu497c9a12008-08-19 20:50:28 -07003492 tmp = TARGET_CPUS;
3493 err = assign_irq_vector(irq, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003494 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003495 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003496 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003497
Yinghai Lu497c9a12008-08-19 20:50:28 -07003498 cfg = irq_cfg(irq);
3499 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003500 dest = cpu_mask_to_apicid(tmp);
3501
Eric W. Biedermanec683072006-11-08 17:44:57 -08003502 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003503
Eric W. Biedermanec683072006-11-08 17:44:57 -08003504 msg.address_lo =
3505 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003506 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003507 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003508 ((INT_DEST_MODE == 0) ?
3509 HT_IRQ_LOW_DM_PHYSICAL :
3510 HT_IRQ_LOW_DM_LOGICAL) |
3511 HT_IRQ_LOW_RQEOI_EDGE |
3512 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3513 HT_IRQ_LOW_MT_FIXED :
3514 HT_IRQ_LOW_MT_ARBITRATED) |
3515 HT_IRQ_LOW_IRQ_MASKED;
3516
Eric W. Biedermanec683072006-11-08 17:44:57 -08003517 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003518
Ingo Molnara460e742006-10-17 00:10:03 -07003519 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3520 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003521
3522 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003523 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003524 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003525}
3526#endif /* CONFIG_HT_IRQ */
3527
Dean Nelson4173a0e2008-10-02 12:18:21 -05003528#ifdef CONFIG_X86_64
3529/*
3530 * Re-target the irq to the specified CPU and enable the specified MMR located
3531 * on the specified blade to allow the sending of MSIs to the specified CPU.
3532 */
3533int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3534 unsigned long mmr_offset)
3535{
3536 const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
3537 struct irq_cfg *cfg;
3538 int mmr_pnode;
3539 unsigned long mmr_value;
3540 struct uv_IO_APIC_route_entry *entry;
3541 unsigned long flags;
3542 int err;
3543
3544 err = assign_irq_vector(irq, *eligible_cpu);
3545 if (err != 0)
3546 return err;
3547
3548 spin_lock_irqsave(&vector_lock, flags);
3549 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3550 irq_name);
3551 spin_unlock_irqrestore(&vector_lock, flags);
3552
3553 cfg = irq_cfg(irq);
3554
3555 mmr_value = 0;
3556 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3557 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3558
3559 entry->vector = cfg->vector;
3560 entry->delivery_mode = INT_DELIVERY_MODE;
3561 entry->dest_mode = INT_DEST_MODE;
3562 entry->polarity = 0;
3563 entry->trigger = 0;
3564 entry->mask = 0;
3565 entry->dest = cpu_mask_to_apicid(*eligible_cpu);
3566
3567 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3568 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3569
3570 return irq;
3571}
3572
3573/*
3574 * Disable the specified MMR located on the specified blade so that MSIs are
3575 * longer allowed to be sent.
3576 */
3577void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3578{
3579 unsigned long mmr_value;
3580 struct uv_IO_APIC_route_entry *entry;
3581 int mmr_pnode;
3582
3583 mmr_value = 0;
3584 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3585 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3586
3587 entry->mask = 1;
3588
3589 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3590 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3591}
3592#endif /* CONFIG_X86_64 */
3593
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003594int __init io_apic_get_redir_entries (int ioapic)
3595{
3596 union IO_APIC_reg_01 reg_01;
3597 unsigned long flags;
3598
3599 spin_lock_irqsave(&ioapic_lock, flags);
3600 reg_01.raw = io_apic_read(ioapic, 1);
3601 spin_unlock_irqrestore(&ioapic_lock, flags);
3602
3603 return reg_01.bits.entries;
3604}
3605
3606int __init probe_nr_irqs(void)
3607{
Thomas Gleixnera1967d62008-11-21 11:16:48 -08003608 return NR_IRQS;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003609}
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003612 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 -------------------------------------------------------------------------- */
3614
Len Brown888ba6c2005-08-24 12:07:20 -04003615#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
Ingo Molnar54168ed2008-08-20 09:07:45 +02003617#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003618int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
3620 union IO_APIC_reg_00 reg_00;
3621 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3622 physid_mask_t tmp;
3623 unsigned long flags;
3624 int i = 0;
3625
3626 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003627 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3628 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003630 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3632 * advantage of new APIC bus architecture.
3633 */
3634
3635 if (physids_empty(apic_id_map))
3636 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3637
3638 spin_lock_irqsave(&ioapic_lock, flags);
3639 reg_00.raw = io_apic_read(ioapic, 0);
3640 spin_unlock_irqrestore(&ioapic_lock, flags);
3641
3642 if (apic_id >= get_physical_broadcast()) {
3643 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3644 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3645 apic_id = reg_00.bits.ID;
3646 }
3647
3648 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003649 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 * 'stuck on smp_invalidate_needed IPI wait' messages.
3651 */
3652 if (check_apicid_used(apic_id_map, apic_id)) {
3653
3654 for (i = 0; i < get_physical_broadcast(); i++) {
3655 if (!check_apicid_used(apic_id_map, i))
3656 break;
3657 }
3658
3659 if (i == get_physical_broadcast())
3660 panic("Max apic_id exceeded!\n");
3661
3662 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3663 "trying %d\n", ioapic, apic_id, i);
3664
3665 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
3668 tmp = apicid_to_cpu_present(apic_id);
3669 physids_or(apic_id_map, apic_id_map, tmp);
3670
3671 if (reg_00.bits.ID != apic_id) {
3672 reg_00.bits.ID = apic_id;
3673
3674 spin_lock_irqsave(&ioapic_lock, flags);
3675 io_apic_write(ioapic, 0, reg_00.raw);
3676 reg_00.raw = io_apic_read(ioapic, 0);
3677 spin_unlock_irqrestore(&ioapic_lock, flags);
3678
3679 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003680 if (reg_00.bits.ID != apic_id) {
3681 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3682 return -1;
3683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
3685
3686 apic_printk(APIC_VERBOSE, KERN_INFO
3687 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3688
3689 return apic_id;
3690}
3691
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003692int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
3694 union IO_APIC_reg_01 reg_01;
3695 unsigned long flags;
3696
3697 spin_lock_irqsave(&ioapic_lock, flags);
3698 reg_01.raw = io_apic_read(ioapic, 1);
3699 spin_unlock_irqrestore(&ioapic_lock, flags);
3700
3701 return reg_01.bits.version;
3702}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003703#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
Ingo Molnar54168ed2008-08-20 09:07:45 +02003705int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003708 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 ioapic);
3710 return -EINVAL;
3711 }
3712
3713 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 * IRQs < 16 are already in the irq_2_pin[] map
3715 */
3716 if (irq >= 16)
3717 add_pin_to_irq(irq, ioapic, pin);
3718
Yinghai Lu497c9a12008-08-19 20:50:28 -07003719 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 return 0;
3722}
3723
Ingo Molnar54168ed2008-08-20 09:07:45 +02003724
Shaohua Li61fd47e2007-11-17 01:05:28 -05003725int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3726{
3727 int i;
3728
3729 if (skip_ioapic_setup)
3730 return -1;
3731
3732 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003733 if (mp_irqs[i].mp_irqtype == mp_INT &&
3734 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003735 break;
3736 if (i >= mp_irq_entries)
3737 return -1;
3738
3739 *trigger = irq_trigger(i);
3740 *polarity = irq_polarity(i);
3741 return 0;
3742}
3743
Len Brown888ba6c2005-08-24 12:07:20 -04003744#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003745
Yinghai Lu497c9a12008-08-19 20:50:28 -07003746/*
3747 * This function currently is only a helper for the i386 smp boot process where
3748 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3749 * so mask in all cases should simply be TARGET_CPUS
3750 */
3751#ifdef CONFIG_SMP
3752void __init setup_ioapic_dest(void)
3753{
3754 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003755 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003756 struct irq_cfg *cfg;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003757 cpumask_t mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003758
3759 if (skip_ioapic_setup == 1)
3760 return;
3761
3762 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3763 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3764 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3765 if (irq_entry == -1)
3766 continue;
3767 irq = pin_2_irq(irq_entry, ioapic, pin);
3768
3769 /* setup_IO_APIC_irqs could fail to get vector for some device
3770 * when you have too many devices, because at that time only boot
3771 * cpu is online.
3772 */
3773 cfg = irq_cfg(irq);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003774 if (!cfg->vector) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07003775 setup_IO_APIC_irq(ioapic, pin, irq,
3776 irq_trigger(irq_entry),
3777 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003778 continue;
3779
3780 }
3781
3782 /*
3783 * Honour affinities which have been set in early boot
3784 */
3785 desc = irq_to_desc(irq);
3786 if (desc->status &
3787 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
3788 mask = desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003789 else
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003790 mask = TARGET_CPUS;
3791
3792#ifdef CONFIG_INTR_REMAP
3793 if (intr_remapping_enabled)
Rusty Russell0de26522008-12-13 21:20:26 +10303794 set_ir_ioapic_affinity_irq(irq, &mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003795 else
3796#endif
Rusty Russell0de26522008-12-13 21:20:26 +10303797 set_ioapic_affinity_irq(irq, &mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003798 }
3799
3800 }
3801}
3802#endif
3803
Ingo Molnar54168ed2008-08-20 09:07:45 +02003804#define IOAPIC_RESOURCE_NAME_SIZE 11
3805
3806static struct resource *ioapic_resources;
3807
3808static struct resource * __init ioapic_setup_resources(void)
3809{
3810 unsigned long n;
3811 struct resource *res;
3812 char *mem;
3813 int i;
3814
3815 if (nr_ioapics <= 0)
3816 return NULL;
3817
3818 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3819 n *= nr_ioapics;
3820
3821 mem = alloc_bootmem(n);
3822 res = (void *)mem;
3823
3824 if (mem != NULL) {
3825 mem += sizeof(struct resource) * nr_ioapics;
3826
3827 for (i = 0; i < nr_ioapics; i++) {
3828 res[i].name = mem;
3829 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3830 sprintf(mem, "IOAPIC %u", i);
3831 mem += IOAPIC_RESOURCE_NAME_SIZE;
3832 }
3833 }
3834
3835 ioapic_resources = res;
3836
3837 return res;
3838}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003839
Yinghai Luf3294a32008-06-27 01:41:56 -07003840void __init ioapic_init_mappings(void)
3841{
3842 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003843 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003844 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07003845
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003846 irq_2_pin_init();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003847 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003848 for (i = 0; i < nr_ioapics; i++) {
3849 if (smp_found_config) {
3850 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003851#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003852 if (!ioapic_phys) {
3853 printk(KERN_ERR
3854 "WARNING: bogus zero IO-APIC "
3855 "address found in MPTABLE, "
3856 "disabling IO/APIC support!\n");
3857 smp_found_config = 0;
3858 skip_ioapic_setup = 1;
3859 goto fake_ioapic_page;
3860 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02003861#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003862 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003863#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003864fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003865#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003866 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003867 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07003868 ioapic_phys = __pa(ioapic_phys);
3869 }
3870 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003871 apic_printk(APIC_VERBOSE,
3872 "mapped IOAPIC to %08lx (%08lx)\n",
3873 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07003874 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003875
Ingo Molnar54168ed2008-08-20 09:07:45 +02003876 if (ioapic_res != NULL) {
3877 ioapic_res->start = ioapic_phys;
3878 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3879 ioapic_res++;
3880 }
Yinghai Luf3294a32008-06-27 01:41:56 -07003881 }
3882}
3883
Ingo Molnar54168ed2008-08-20 09:07:45 +02003884static int __init ioapic_insert_resources(void)
3885{
3886 int i;
3887 struct resource *r = ioapic_resources;
3888
3889 if (!r) {
3890 printk(KERN_ERR
3891 "IO APIC resources could be not be allocated.\n");
3892 return -1;
3893 }
3894
3895 for (i = 0; i < nr_ioapics; i++) {
3896 insert_resource(&iomem_resource, r);
3897 r++;
3898 }
3899
3900 return 0;
3901}
3902
3903/* Insert the IO APIC resources after PCI initialization has occured to handle
3904 * IO APICS that are mapped in on a BAR in PCI space. */
3905late_initcall(ioapic_insert_resources);