blob: 7f23ce7f5518d4a900c851550d3ce780d28ca7f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070028#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020038#include <linux/jiffies.h> /* time_after() */
Yinghai Lud4057bd2008-08-19 20:50:38 -070039#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070044#include <linux/hpet.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070045
Yinghai Lud4057bd2008-08-19 20:50:38 -070046#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48#include <asm/smp.h>
49#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070050#include <asm/proto.h>
51#include <asm/acpi.h>
52#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070054#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020055#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070056#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070057#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070058#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070059#include <asm/irq_remapping.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070060#include <asm/hpet.h>
Dean Nelson4173a0e2008-10-02 12:18:21 -050061#include <asm/uv/uv_hub.h>
62#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Yinghai Lu497c9a12008-08-19 20:50:28 -070064#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020066#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010068#define __apicdebuginit(type) static type __init
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020071 * Is the SiS APIC rmw bug present ?
72 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74int sis_apic_bug = -1;
75
Yinghai Luefa25592008-08-19 20:50:36 -070076static DEFINE_SPINLOCK(ioapic_lock);
77static DEFINE_SPINLOCK(vector_lock);
78
Yinghai Luefa25592008-08-19 20:50:36 -070079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * # of IRQ routing registers
81 */
82int nr_ioapic_registers[MAX_IO_APICS];
83
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040084/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040085struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040086int nr_ioapics;
87
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040088/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040089struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040090
91/* # of MP IRQ source entries */
92int mp_irq_entries;
93
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040094#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
95int mp_bus_id_to_type[MAX_MP_BUSSES];
96#endif
97
98DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
99
Yinghai Luefa25592008-08-19 20:50:36 -0700100int skip_ioapic_setup;
101
Ingo Molnar54168ed2008-08-20 09:07:45 +0200102static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700103{
104 /* disable IO-APIC */
105 disable_ioapic_setup();
106 return 0;
107}
108early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200109
Yinghai Lu0f978f42008-08-19 20:50:26 -0700110struct irq_pin_list;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200111
112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * This is performance-critical, we want to do it O(1)
114 *
115 * the indexing order of this array favors 1:1 mappings
116 * between pins and IRQs.
117 */
118
Yinghai Lu0f978f42008-08-19 20:50:26 -0700119struct irq_pin_list {
120 int apic, pin;
121 struct irq_pin_list *next;
122};
Yinghai Lu301e6192008-08-19 20:50:02 -0700123
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800124static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700125{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800126 struct irq_pin_list *pin;
127 int node;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700128
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800129 node = cpu_to_node(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700130
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800131 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
132 printk(KERN_DEBUG " alloc irq_2_pin on cpu %d node %d\n", cpu, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700133
Yinghai Lu0f978f42008-08-19 20:50:26 -0700134 return pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700135}
136
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800137struct irq_cfg {
138 struct irq_pin_list *irq_2_pin;
139 cpumask_t domain;
140 cpumask_t old_domain;
141 unsigned move_cleanup_count;
142 u8 vector;
143 u8 move_in_progress : 1;
144};
145
146/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
147#ifdef CONFIG_SPARSE_IRQ
148static struct irq_cfg irq_cfgx[] = {
149#else
150static struct irq_cfg irq_cfgx[NR_IRQS] = {
151#endif
152 [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
153 [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
154 [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
155 [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
156 [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
157 [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
158 [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
159 [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
160 [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
161 [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
162 [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
163 [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
164 [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
165 [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
166 [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
167 [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
168};
169
170void __init arch_early_irq_init(void)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700171{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800172 struct irq_cfg *cfg;
173 struct irq_desc *desc;
174 int count;
175 int i;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700176
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800177 cfg = irq_cfgx;
178 count = ARRAY_SIZE(irq_cfgx);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700179
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800180 for (i = 0; i < count; i++) {
181 desc = irq_to_desc(i);
182 desc->chip_data = &cfg[i];
183 }
184}
185
186#ifdef CONFIG_SPARSE_IRQ
187static struct irq_cfg *irq_cfg(unsigned int irq)
188{
189 struct irq_cfg *cfg = NULL;
190 struct irq_desc *desc;
191
192 desc = irq_to_desc(irq);
193 if (desc)
194 cfg = desc->chip_data;
195
196 return cfg;
197}
198
199static struct irq_cfg *get_one_free_irq_cfg(int cpu)
200{
201 struct irq_cfg *cfg;
202 int node;
203
204 node = cpu_to_node(cpu);
205
206 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
207 printk(KERN_DEBUG " alloc irq_cfg on cpu %d node %d\n", cpu, node);
208
209 return cfg;
210}
211
212void arch_init_chip_data(struct irq_desc *desc, int cpu)
213{
214 struct irq_cfg *cfg;
215
216 cfg = desc->chip_data;
217 if (!cfg) {
218 desc->chip_data = get_one_free_irq_cfg(cpu);
219 if (!desc->chip_data) {
220 printk(KERN_ERR "can not alloc irq_cfg\n");
221 BUG_ON(1);
222 }
223 }
224}
225
226#else
227static struct irq_cfg *irq_cfg(unsigned int irq)
228{
229 return irq < nr_irqs ? irq_cfgx + irq : NULL;
230}
231
232#endif
233
Mike Travise7986732008-12-16 17:33:52 -0800234static inline void
235set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800236{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700237}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds130fe052006-11-01 09:11:00 -0800239struct io_apic {
240 unsigned int index;
241 unsigned int unused[3];
242 unsigned int data;
243};
244
245static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
246{
247 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400248 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800249}
250
251static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
252{
253 struct io_apic __iomem *io_apic = io_apic_base(apic);
254 writel(reg, &io_apic->index);
255 return readl(&io_apic->data);
256}
257
258static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
259{
260 struct io_apic __iomem *io_apic = io_apic_base(apic);
261 writel(reg, &io_apic->index);
262 writel(value, &io_apic->data);
263}
264
265/*
266 * Re-write a value: to be used for read-modify-write
267 * cycles where the read already set up the index register.
268 *
269 * Older SiS APIC requires we rewrite the index register
270 */
271static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
272{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200273 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200274
275 if (sis_apic_bug)
276 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800277 writel(value, &io_apic->data);
278}
279
Yinghai Lu3145e942008-12-05 18:58:34 -0800280static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700281{
282 struct irq_pin_list *entry;
283 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700284
285 spin_lock_irqsave(&ioapic_lock, flags);
286 entry = cfg->irq_2_pin;
287 for (;;) {
288 unsigned int reg;
289 int pin;
290
291 if (!entry)
292 break;
293 pin = entry->pin;
294 reg = io_apic_read(entry->apic, 0x10 + pin*2);
295 /* Is the remote IRR bit set? */
296 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
297 spin_unlock_irqrestore(&ioapic_lock, flags);
298 return true;
299 }
300 if (!entry->next)
301 break;
302 entry = entry->next;
303 }
304 spin_unlock_irqrestore(&ioapic_lock, flags);
305
306 return false;
307}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700308
Andi Kleencf4c6a22006-09-26 10:52:30 +0200309union entry_union {
310 struct { u32 w1, w2; };
311 struct IO_APIC_route_entry entry;
312};
313
314static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
315{
316 union entry_union eu;
317 unsigned long flags;
318 spin_lock_irqsave(&ioapic_lock, flags);
319 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
320 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
321 spin_unlock_irqrestore(&ioapic_lock, flags);
322 return eu.entry;
323}
324
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800325/*
326 * When we write a new IO APIC routing entry, we need to write the high
327 * word first! If the mask bit in the low word is clear, we will enable
328 * the interrupt, and we need to make sure the entry is fully populated
329 * before that happens.
330 */
Andi Kleend15512f2006-12-07 02:14:07 +0100331static void
332__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
333{
334 union entry_union eu;
335 eu.entry = e;
336 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
337 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
338}
339
Andi Kleencf4c6a22006-09-26 10:52:30 +0200340static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
341{
342 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200343 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100344 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800345 spin_unlock_irqrestore(&ioapic_lock, flags);
346}
347
348/*
349 * When we mask an IO APIC routing entry, we need to write the low
350 * word first, in order to set the mask bit before we change the
351 * high bits!
352 */
353static void ioapic_mask_entry(int apic, int pin)
354{
355 unsigned long flags;
356 union entry_union eu = { .entry.mask = 1 };
357
358 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200359 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
360 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
361 spin_unlock_irqrestore(&ioapic_lock, flags);
362}
363
Yinghai Lu497c9a12008-08-19 20:50:28 -0700364#ifdef CONFIG_SMP
Yinghai Lu3145e942008-12-05 18:58:34 -0800365static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700366{
367 int apic, pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700368 struct irq_pin_list *entry;
Yinghai Lu3145e942008-12-05 18:58:34 -0800369 u8 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700370
Yinghai Lu497c9a12008-08-19 20:50:28 -0700371 entry = cfg->irq_2_pin;
372 for (;;) {
373 unsigned int reg;
374
375 if (!entry)
376 break;
377
378 apic = entry->apic;
379 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200380#ifdef CONFIG_INTR_REMAP
381 /*
382 * With interrupt-remapping, destination information comes
383 * from interrupt-remapping table entry.
384 */
385 if (!irq_remapped(irq))
386 io_apic_write(apic, 0x11 + pin*2, dest);
387#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700388 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200389#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700390 reg = io_apic_read(apic, 0x10 + pin*2);
391 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
392 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200393 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700394 if (!entry->next)
395 break;
396 entry = entry->next;
397 }
398}
Yinghai Luefa25592008-08-19 20:50:36 -0700399
Mike Travise7986732008-12-16 17:33:52 -0800400static int
401assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
Yinghai Luefa25592008-08-19 20:50:36 -0700402
Rusty Russell968ea6d2008-12-13 21:55:51 +1030403static void set_ioapic_affinity_irq_desc(struct irq_desc *desc,
404 const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700405{
406 struct irq_cfg *cfg;
407 unsigned long flags;
408 unsigned int dest;
409 cpumask_t tmp;
Yinghai Lu3145e942008-12-05 18:58:34 -0800410 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700411
Rusty Russell0de26522008-12-13 21:20:26 +1030412 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -0700413 return;
414
Yinghai Lu3145e942008-12-05 18:58:34 -0800415 irq = desc->irq;
416 cfg = desc->chip_data;
Mike Travise7986732008-12-16 17:33:52 -0800417 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -0700418 return;
419
Mike Travise7986732008-12-16 17:33:52 -0800420 set_extra_move_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -0800421
Rusty Russell0de26522008-12-13 21:20:26 +1030422 cpumask_and(&tmp, &cfg->domain, mask);
Mike Travise7986732008-12-16 17:33:52 -0800423 dest = cpu_mask_to_apicid(&tmp);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700424 /*
425 * Only the high 8 bits are valid.
426 */
427 dest = SET_APIC_LOGICAL_ID(dest);
428
429 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800430 __target_IO_APIC_irq(irq, dest, cfg);
Rusty Russell0de26522008-12-13 21:20:26 +1030431 cpumask_copy(&desc->affinity, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700432 spin_unlock_irqrestore(&ioapic_lock, flags);
433}
Yinghai Lu3145e942008-12-05 18:58:34 -0800434
Rusty Russell968ea6d2008-12-13 21:55:51 +1030435static void set_ioapic_affinity_irq(unsigned int irq,
436 const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800437{
438 struct irq_desc *desc;
439
440 desc = irq_to_desc(irq);
441
442 set_ioapic_affinity_irq_desc(desc, mask);
443}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700444#endif /* CONFIG_SMP */
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446/*
447 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
448 * shared ISA-space IRQs, so we have to support them. We are super
449 * fast in the common case, and fast for shared ISA-space IRQs.
450 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800451static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700453 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Yinghai Lu0f978f42008-08-19 20:50:26 -0700455 entry = cfg->irq_2_pin;
456 if (!entry) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800457 entry = get_one_free_irq_2_pin(cpu);
458 if (!entry) {
459 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
460 apic, pin);
461 return;
462 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700463 cfg->irq_2_pin = entry;
464 entry->apic = apic;
465 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700466 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700468
469 while (entry->next) {
470 /* not again, please */
471 if (entry->apic == apic && entry->pin == pin)
472 return;
473
474 entry = entry->next;
475 }
476
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800477 entry->next = get_one_free_irq_2_pin(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700478 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 entry->apic = apic;
480 entry->pin = pin;
481}
482
483/*
484 * Reroute an IRQ to a different pin.
485 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800486static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 int oldapic, int oldpin,
488 int newapic, int newpin)
489{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700490 struct irq_pin_list *entry = cfg->irq_2_pin;
491 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Yinghai Lu0f978f42008-08-19 20:50:26 -0700493 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (entry->apic == oldapic && entry->pin == oldpin) {
495 entry->apic = newapic;
496 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700497 replaced = 1;
498 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700500 }
501 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700503
504 /* why? call replace before add? */
505 if (!replaced)
Yinghai Lu3145e942008-12-05 18:58:34 -0800506 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Yinghai Lu3145e942008-12-05 18:58:34 -0800509static inline void io_apic_modify_irq(struct irq_cfg *cfg,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400510 int mask_and, int mask_or,
511 void (*final)(struct irq_pin_list *entry))
512{
513 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400514 struct irq_pin_list *entry;
515
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400516 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
517 unsigned int reg;
518 pin = entry->pin;
519 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
520 reg &= mask_and;
521 reg |= mask_or;
522 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
523 if (final)
524 final(entry);
525 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700526}
527
Yinghai Lu3145e942008-12-05 18:58:34 -0800528static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400529{
Yinghai Lu3145e942008-12-05 18:58:34 -0800530 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400531}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700532
533#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400534void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700535{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400536 /*
537 * Synchronize the IO-APIC and the CPU by doing
538 * a dummy read from the IO-APIC
539 */
540 struct io_apic __iomem *io_apic;
541 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700542 readl(&io_apic->data);
543}
544
Yinghai Lu3145e942008-12-05 18:58:34 -0800545static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400546{
Yinghai Lu3145e942008-12-05 18:58:34 -0800547 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400548}
549#else /* CONFIG_X86_32 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800550static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400551{
Yinghai Lu3145e942008-12-05 18:58:34 -0800552 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400553}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700554
Yinghai Lu3145e942008-12-05 18:58:34 -0800555static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400556{
Yinghai Lu3145e942008-12-05 18:58:34 -0800557 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400558 IO_APIC_REDIR_MASKED, NULL);
559}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700560
Yinghai Lu3145e942008-12-05 18:58:34 -0800561static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400562{
Yinghai Lu3145e942008-12-05 18:58:34 -0800563 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400564 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
565}
566#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700567
Yinghai Lu3145e942008-12-05 18:58:34 -0800568static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Yinghai Lu3145e942008-12-05 18:58:34 -0800570 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 unsigned long flags;
572
Yinghai Lu3145e942008-12-05 18:58:34 -0800573 BUG_ON(!cfg);
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800576 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 spin_unlock_irqrestore(&ioapic_lock, flags);
578}
579
Yinghai Lu3145e942008-12-05 18:58:34 -0800580static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Yinghai Lu3145e942008-12-05 18:58:34 -0800582 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 unsigned long flags;
584
585 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800586 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 spin_unlock_irqrestore(&ioapic_lock, flags);
588}
589
Yinghai Lu3145e942008-12-05 18:58:34 -0800590static void mask_IO_APIC_irq(unsigned int irq)
591{
592 struct irq_desc *desc = irq_to_desc(irq);
593
594 mask_IO_APIC_irq_desc(desc);
595}
596static void unmask_IO_APIC_irq(unsigned int irq)
597{
598 struct irq_desc *desc = irq_to_desc(irq);
599
600 unmask_IO_APIC_irq_desc(desc);
601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
604{
605 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200608 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 if (entry.delivery_mode == dest_SMI)
610 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /*
612 * Disable it in the IO-APIC irq-routing table:
613 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800614 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Ingo Molnar54168ed2008-08-20 09:07:45 +0200617static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 int apic, pin;
620
621 for (apic = 0; apic < nr_ioapics; apic++)
622 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
623 clear_IO_APIC_pin(apic, pin);
624}
625
Ingo Molnar54168ed2008-08-20 09:07:45 +0200626#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100627void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
629 unsigned int cfg;
630
631 /*
632 * Wait for idle.
633 */
634 apic_wait_icr_idle();
635 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
636 /*
637 * Send the IPI. The write to APIC_ICR fires this off.
638 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100639 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200641#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Ingo Molnar54168ed2008-08-20 09:07:45 +0200643#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/*
645 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
646 * specific CPU-side IRQs.
647 */
648
649#define MAX_PIRQS 8
650static int pirq_entries [MAX_PIRQS];
651static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653static int __init ioapic_pirq_setup(char *str)
654{
655 int i, max;
656 int ints[MAX_PIRQS+1];
657
658 get_options(str, ARRAY_SIZE(ints), ints);
659
660 for (i = 0; i < MAX_PIRQS; i++)
661 pirq_entries[i] = -1;
662
663 pirqs_enabled = 1;
664 apic_printk(APIC_VERBOSE, KERN_INFO
665 "PIRQ redirection, working around broken MP-BIOS.\n");
666 max = MAX_PIRQS;
667 if (ints[0] < MAX_PIRQS)
668 max = ints[0];
669
670 for (i = 0; i < max; i++) {
671 apic_printk(APIC_VERBOSE, KERN_DEBUG
672 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
673 /*
674 * PIRQs are mapped upside down, usually.
675 */
676 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
677 }
678 return 1;
679}
680
681__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200682#endif /* CONFIG_X86_32 */
683
684#ifdef CONFIG_INTR_REMAP
685/* I/O APIC RTE contents at the OS boot up */
686static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
687
688/*
689 * Saves and masks all the unmasked IO-APIC RTE's
690 */
691int save_mask_IO_APIC_setup(void)
692{
693 union IO_APIC_reg_01 reg_01;
694 unsigned long flags;
695 int apic, pin;
696
697 /*
698 * The number of IO-APIC IRQ registers (== #pins):
699 */
700 for (apic = 0; apic < nr_ioapics; apic++) {
701 spin_lock_irqsave(&ioapic_lock, flags);
702 reg_01.raw = io_apic_read(apic, 1);
703 spin_unlock_irqrestore(&ioapic_lock, flags);
704 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
705 }
706
707 for (apic = 0; apic < nr_ioapics; apic++) {
708 early_ioapic_entries[apic] =
709 kzalloc(sizeof(struct IO_APIC_route_entry) *
710 nr_ioapic_registers[apic], GFP_KERNEL);
711 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400712 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200713 }
714
715 for (apic = 0; apic < nr_ioapics; apic++)
716 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
717 struct IO_APIC_route_entry entry;
718
719 entry = early_ioapic_entries[apic][pin] =
720 ioapic_read_entry(apic, pin);
721 if (!entry.mask) {
722 entry.mask = 1;
723 ioapic_write_entry(apic, pin, entry);
724 }
725 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400726
Ingo Molnar54168ed2008-08-20 09:07:45 +0200727 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400728
729nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400730 while (apic >= 0)
731 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400732 memset(early_ioapic_entries, 0,
733 ARRAY_SIZE(early_ioapic_entries));
734
735 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200736}
737
738void restore_IO_APIC_setup(void)
739{
740 int apic, pin;
741
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400742 for (apic = 0; apic < nr_ioapics; apic++) {
743 if (!early_ioapic_entries[apic])
744 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200745 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
746 ioapic_write_entry(apic, pin,
747 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400748 kfree(early_ioapic_entries[apic]);
749 early_ioapic_entries[apic] = NULL;
750 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200751}
752
753void reinit_intr_remapped_IO_APIC(int intr_remapping)
754{
755 /*
756 * for now plain restore of previous settings.
757 * TBD: In the case of OS enabling interrupt-remapping,
758 * IO-APIC RTE's need to be setup to point to interrupt-remapping
759 * table entries. for now, do a plain restore, and wait for
760 * the setup_IO_APIC_irqs() to do proper initialization.
761 */
762 restore_IO_APIC_setup();
763}
764#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766/*
767 * Find the IRQ entry number of a certain pin.
768 */
769static int find_irq_entry(int apic, int pin, int type)
770{
771 int i;
772
773 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400774 if (mp_irqs[i].mp_irqtype == type &&
775 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
776 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
777 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 return i;
779
780 return -1;
781}
782
783/*
784 * Find the pin to which IRQ[irq] (ISA) is connected
785 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800786static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
788 int i;
789
790 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400791 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300793 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400794 (mp_irqs[i].mp_irqtype == type) &&
795 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400797 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799 return -1;
800}
801
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800802static int __init find_isa_irq_apic(int irq, int type)
803{
804 int i;
805
806 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400807 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800808
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300809 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400810 (mp_irqs[i].mp_irqtype == type) &&
811 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800812 break;
813 }
814 if (i < mp_irq_entries) {
815 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200816 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400817 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800818 return apic;
819 }
820 }
821
822 return -1;
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
826 * Find a specific PCI IRQ entry.
827 * Not an __init, possibly needed by modules
828 */
829static int pin_2_irq(int idx, int apic, int pin);
830
831int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
832{
833 int apic, i, best_guess = -1;
834
Ingo Molnar54168ed2008-08-20 09:07:45 +0200835 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
836 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400837 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200838 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -1;
840 }
841 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400842 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400845 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
846 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 break;
848
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300849 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400850 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400852 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200853 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 if (!(apic || IO_APIC_IRQ(irq)))
856 continue;
857
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400858 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return irq;
860 /*
861 * Use the first all-but-pin matching entry as a
862 * best-guess fuzzy result for broken mptables.
863 */
864 if (best_guess < 0)
865 best_guess = irq;
866 }
867 }
868 return best_guess;
869}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200870
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700871EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300873#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/*
875 * EISA Edge/Level control register, ELCR
876 */
877static int EISA_ELCR(unsigned int irq)
878{
Yinghai Lu99d093d2008-12-05 18:58:32 -0800879 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unsigned int port = 0x4d0 + (irq >> 3);
881 return (inb(port) >> (irq & 7)) & 1;
882 }
883 apic_printk(APIC_VERBOSE, KERN_INFO
884 "Broken MPtable reports ISA irq %d\n", irq);
885 return 0;
886}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200887
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300890/* ISA interrupts are always polarity zero edge triggered,
891 * when listed as conforming in the MP table. */
892
893#define default_ISA_trigger(idx) (0)
894#define default_ISA_polarity(idx) (0)
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896/* EISA interrupts are always polarity zero and can be edge or level
897 * trigger depending on the ELCR value. If an interrupt is listed as
898 * EISA conforming in the MP table, that means its trigger type must
899 * be read in from the ELCR */
900
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400901#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300902#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904/* PCI interrupts are always polarity one level triggered,
905 * when listed as conforming in the MP table. */
906
907#define default_PCI_trigger(idx) (1)
908#define default_PCI_polarity(idx) (1)
909
910/* MCA interrupts are always polarity zero level triggered,
911 * when listed as conforming in the MP table. */
912
913#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300914#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Shaohua Li61fd47e2007-11-17 01:05:28 -0500916static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400918 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 int polarity;
920
921 /*
922 * Determine IRQ line polarity (high active or low active):
923 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200924 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200926 case 0: /* conforms, ie. bus-type dependent polarity */
927 if (test_bit(bus, mp_bus_not_pci))
928 polarity = default_ISA_polarity(idx);
929 else
930 polarity = default_PCI_polarity(idx);
931 break;
932 case 1: /* high active */
933 {
934 polarity = 0;
935 break;
936 }
937 case 2: /* reserved */
938 {
939 printk(KERN_WARNING "broken BIOS!!\n");
940 polarity = 1;
941 break;
942 }
943 case 3: /* low active */
944 {
945 polarity = 1;
946 break;
947 }
948 default: /* invalid */
949 {
950 printk(KERN_WARNING "broken BIOS!!\n");
951 polarity = 1;
952 break;
953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955 return polarity;
956}
957
958static int MPBIOS_trigger(int idx)
959{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400960 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 int trigger;
962
963 /*
964 * Determine IRQ trigger mode (edge or level sensitive):
965 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200966 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200968 case 0: /* conforms, ie. bus-type dependent */
969 if (test_bit(bus, mp_bus_not_pci))
970 trigger = default_ISA_trigger(idx);
971 else
972 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300973#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +0200974 switch (mp_bus_id_to_type[bus]) {
975 case MP_BUS_ISA: /* ISA pin */
976 {
977 /* set before the switch */
978 break;
979 }
980 case MP_BUS_EISA: /* EISA pin */
981 {
982 trigger = default_EISA_trigger(idx);
983 break;
984 }
985 case MP_BUS_PCI: /* PCI pin */
986 {
987 /* set before the switch */
988 break;
989 }
990 case MP_BUS_MCA: /* MCA pin */
991 {
992 trigger = default_MCA_trigger(idx);
993 break;
994 }
995 default:
996 {
997 printk(KERN_WARNING "broken BIOS!!\n");
998 trigger = 1;
999 break;
1000 }
1001 }
1002#endif
1003 break;
1004 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001005 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001006 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001007 break;
1008 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001009 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001010 {
1011 printk(KERN_WARNING "broken BIOS!!\n");
1012 trigger = 1;
1013 break;
1014 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001015 case 3: /* level */
1016 {
1017 trigger = 1;
1018 break;
1019 }
1020 default: /* invalid */
1021 {
1022 printk(KERN_WARNING "broken BIOS!!\n");
1023 trigger = 0;
1024 break;
1025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 return trigger;
1028}
1029
1030static inline int irq_polarity(int idx)
1031{
1032 return MPBIOS_polarity(idx);
1033}
1034
1035static inline int irq_trigger(int idx)
1036{
1037 return MPBIOS_trigger(idx);
1038}
1039
Yinghai Luefa25592008-08-19 20:50:36 -07001040int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041static int pin_2_irq(int idx, int apic, int pin)
1042{
1043 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001044 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 /*
1047 * Debugging check, we are in big trouble if this message pops up!
1048 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001049 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1051
Ingo Molnar54168ed2008-08-20 09:07:45 +02001052 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001053 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001054 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001055 /*
1056 * PCI IRQs are mapped in order
1057 */
1058 i = irq = 0;
1059 while (i < apic)
1060 irq += nr_ioapic_registers[i++];
1061 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001062 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001063 * For MPS mode, so far only needed by ES7000 platform
1064 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001065 if (ioapic_renumber_irq)
1066 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068
Ingo Molnar54168ed2008-08-20 09:07:45 +02001069#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /*
1071 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1072 */
1073 if ((pin >= 16) && (pin <= 23)) {
1074 if (pirq_entries[pin-16] != -1) {
1075 if (!pirq_entries[pin-16]) {
1076 apic_printk(APIC_VERBOSE, KERN_DEBUG
1077 "disabling PIRQ%d\n", pin-16);
1078 } else {
1079 irq = pirq_entries[pin-16];
1080 apic_printk(APIC_VERBOSE, KERN_DEBUG
1081 "using PIRQ%d -> IRQ %d\n",
1082 pin-16, irq);
1083 }
1084 }
1085 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001086#endif
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 return irq;
1089}
1090
Yinghai Lu497c9a12008-08-19 20:50:28 -07001091void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001093 /* Used to the online set of cpus does not change
1094 * during assign_irq_vector.
1095 */
1096 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Yinghai Lu497c9a12008-08-19 20:50:28 -07001099void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001100{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001101 spin_unlock(&vector_lock);
1102}
1103
Mike Travise7986732008-12-16 17:33:52 -08001104static int
1105__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001106{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001107 /*
1108 * NOTE! The local APIC isn't very good at handling
1109 * multiple interrupts at the same interrupt level.
1110 * As the interrupt level is determined by taking the
1111 * vector number and shifting that right by 4, we
1112 * want to spread these out a bit so that they don't
1113 * all fall in the same interrupt level.
1114 *
1115 * Also, we've got to be careful not to trash gate
1116 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1117 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001118 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1119 unsigned int old_vector;
1120 int cpu;
Mike Travise7986732008-12-16 17:33:52 -08001121 cpumask_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001122
Ingo Molnar54168ed2008-08-20 09:07:45 +02001123 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1124 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001125
Ingo Molnar54168ed2008-08-20 09:07:45 +02001126 old_vector = cfg->vector;
1127 if (old_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001128 cpus_and(tmp_mask, *mask, cpu_online_map);
1129 cpus_and(tmp_mask, cfg->domain, tmp_mask);
1130 if (!cpus_empty(tmp_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001131 return 0;
1132 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001133
Mike Travise7986732008-12-16 17:33:52 -08001134 /* Only try and allocate irqs on cpus that are present */
1135 for_each_cpu_and(cpu, mask, &cpu_online_map) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001136 int new_cpu;
1137 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001138
Mike Travise7986732008-12-16 17:33:52 -08001139 vector_allocation_domain(cpu, &tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001140
Ingo Molnar54168ed2008-08-20 09:07:45 +02001141 vector = current_vector;
1142 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001143next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001144 vector += 8;
1145 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001146 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001147 offset = (offset + 1) % 8;
1148 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001149 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001150 if (unlikely(current_vector == vector))
1151 continue;
1152#ifdef CONFIG_X86_64
1153 if (vector == IA32_SYSCALL_VECTOR)
1154 goto next;
1155#else
1156 if (vector == SYSCALL_VECTOR)
1157 goto next;
1158#endif
Mike Travise7986732008-12-16 17:33:52 -08001159 for_each_cpu_and(new_cpu, &tmp_mask, &cpu_online_map)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001160 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1161 goto next;
1162 /* Found one! */
1163 current_vector = vector;
1164 current_offset = offset;
1165 if (old_vector) {
1166 cfg->move_in_progress = 1;
1167 cfg->old_domain = cfg->domain;
1168 }
Mike Travise7986732008-12-16 17:33:52 -08001169 for_each_cpu_and(new_cpu, &tmp_mask, &cpu_online_map)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001170 per_cpu(vector_irq, new_cpu)[vector] = irq;
1171 cfg->vector = vector;
Mike Travise7986732008-12-16 17:33:52 -08001172 cfg->domain = tmp_mask;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001173 return 0;
1174 }
1175 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001176}
1177
Mike Travise7986732008-12-16 17:33:52 -08001178static int
1179assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001180{
1181 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001182 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001183
1184 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001185 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001186 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001187 return err;
1188}
1189
Yinghai Lu3145e942008-12-05 18:58:34 -08001190static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001191{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001192 cpumask_t mask;
1193 int cpu, vector;
1194
Yinghai Lu497c9a12008-08-19 20:50:28 -07001195 BUG_ON(!cfg->vector);
1196
1197 vector = cfg->vector;
1198 cpus_and(mask, cfg->domain, cpu_online_map);
1199 for_each_cpu_mask_nr(cpu, mask)
1200 per_cpu(vector_irq, cpu)[vector] = -1;
1201
1202 cfg->vector = 0;
1203 cpus_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001204
1205 if (likely(!cfg->move_in_progress))
1206 return;
1207 cpus_and(mask, cfg->old_domain, cpu_online_map);
1208 for_each_cpu_mask_nr(cpu, mask) {
1209 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1210 vector++) {
1211 if (per_cpu(vector_irq, cpu)[vector] != irq)
1212 continue;
1213 per_cpu(vector_irq, cpu)[vector] = -1;
1214 break;
1215 }
1216 }
1217 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001218}
1219
1220void __setup_vector_irq(int cpu)
1221{
1222 /* Initialize vector_irq on a new cpu */
1223 /* This function must be called with vector_lock held */
1224 int irq, vector;
1225 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001226 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001227
1228 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001229 for_each_irq_desc(irq, desc) {
1230 if (!desc)
1231 continue;
1232 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001233 if (!cpu_isset(cpu, cfg->domain))
1234 continue;
1235 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001236 per_cpu(vector_irq, cpu)[vector] = irq;
1237 }
1238 /* Mark the free vectors */
1239 for (vector = 0; vector < NR_VECTORS; ++vector) {
1240 irq = per_cpu(vector_irq, cpu)[vector];
1241 if (irq < 0)
1242 continue;
1243
1244 cfg = irq_cfg(irq);
1245 if (!cpu_isset(cpu, cfg->domain))
1246 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001247 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001248}
Glauber Costa3fde6902008-05-28 20:34:19 -07001249
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001250static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001251#ifdef CONFIG_INTR_REMAP
1252static struct irq_chip ir_ioapic_chip;
1253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Ingo Molnar54168ed2008-08-20 09:07:45 +02001255#define IOAPIC_AUTO -1
1256#define IOAPIC_EDGE 0
1257#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001259#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001260static inline int IO_APIC_irq_trigger(int irq)
1261{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001262 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001263
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001264 for (apic = 0; apic < nr_ioapics; apic++) {
1265 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1266 idx = find_irq_entry(apic, pin, mp_INT);
1267 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1268 return irq_trigger(idx);
1269 }
1270 }
1271 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001272 * nonexistent IRQs are edge default
1273 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001274 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001275}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001276#else
1277static inline int IO_APIC_irq_trigger(int irq)
1278{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001279 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001280}
1281#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001282
Yinghai Lu3145e942008-12-05 18:58:34 -08001283static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
Yinghai Lu199751d2008-08-19 20:50:27 -07001285
Jan Beulich6ebcc002006-06-26 13:56:46 +02001286 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001287 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001288 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001289 else
1290 desc->status &= ~IRQ_LEVEL;
1291
Ingo Molnar54168ed2008-08-20 09:07:45 +02001292#ifdef CONFIG_INTR_REMAP
1293 if (irq_remapped(irq)) {
1294 desc->status |= IRQ_MOVE_PCNTXT;
1295 if (trigger)
1296 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1297 handle_fasteoi_irq,
1298 "fasteoi");
1299 else
1300 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1301 handle_edge_irq, "edge");
1302 return;
1303 }
1304#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001305 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1306 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001307 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001308 handle_fasteoi_irq,
1309 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001310 else
Ingo Molnara460e742006-10-17 00:10:03 -07001311 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001312 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001313}
1314
1315static int setup_ioapic_entry(int apic, int irq,
1316 struct IO_APIC_route_entry *entry,
1317 unsigned int destination, int trigger,
1318 int polarity, int vector)
1319{
1320 /*
1321 * add it to the IO-APIC irq-routing table:
1322 */
1323 memset(entry,0,sizeof(*entry));
1324
Ingo Molnar54168ed2008-08-20 09:07:45 +02001325#ifdef CONFIG_INTR_REMAP
1326 if (intr_remapping_enabled) {
1327 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1328 struct irte irte;
1329 struct IR_IO_APIC_route_entry *ir_entry =
1330 (struct IR_IO_APIC_route_entry *) entry;
1331 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001332
Ingo Molnar54168ed2008-08-20 09:07:45 +02001333 if (!iommu)
1334 panic("No mapping iommu for ioapic %d\n", apic);
1335
1336 index = alloc_irte(iommu, irq, 1);
1337 if (index < 0)
1338 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1339
1340 memset(&irte, 0, sizeof(irte));
1341
1342 irte.present = 1;
1343 irte.dst_mode = INT_DEST_MODE;
1344 irte.trigger_mode = trigger;
1345 irte.dlvry_mode = INT_DELIVERY_MODE;
1346 irte.vector = vector;
1347 irte.dest_id = IRTE_DEST(destination);
1348
1349 modify_irte(irq, &irte);
1350
1351 ir_entry->index2 = (index >> 15) & 0x1;
1352 ir_entry->zero = 0;
1353 ir_entry->format = 1;
1354 ir_entry->index = (index & 0x7fff);
1355 } else
1356#endif
1357 {
1358 entry->delivery_mode = INT_DELIVERY_MODE;
1359 entry->dest_mode = INT_DEST_MODE;
1360 entry->dest = destination;
1361 }
1362
1363 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001364 entry->trigger = trigger;
1365 entry->polarity = polarity;
1366 entry->vector = vector;
1367
1368 /* Mask level triggered irqs.
1369 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1370 */
1371 if (trigger)
1372 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001373 return 0;
1374}
1375
Yinghai Lu3145e942008-12-05 18:58:34 -08001376static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001377 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001378{
1379 struct irq_cfg *cfg;
1380 struct IO_APIC_route_entry entry;
1381 cpumask_t mask;
1382
1383 if (!IO_APIC_IRQ(irq))
1384 return;
1385
Yinghai Lu3145e942008-12-05 18:58:34 -08001386 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001387
Mike Travise7986732008-12-16 17:33:52 -08001388 mask = *TARGET_CPUS;
1389 if (assign_irq_vector(irq, cfg, &mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001390 return;
1391
1392 cpus_and(mask, cfg->domain, mask);
1393
1394 apic_printk(APIC_VERBOSE,KERN_DEBUG
1395 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1396 "IRQ %d Mode:%i Active:%i)\n",
1397 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1398 irq, trigger, polarity);
1399
1400
1401 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
Mike Travise7986732008-12-16 17:33:52 -08001402 cpu_mask_to_apicid(&mask), trigger, polarity,
Yinghai Lu497c9a12008-08-19 20:50:28 -07001403 cfg->vector)) {
1404 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1405 mp_ioapics[apic].mp_apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001406 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001407 return;
1408 }
1409
Yinghai Lu3145e942008-12-05 18:58:34 -08001410 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001411 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001412 disable_8259A_irq(irq);
1413
1414 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
1417static void __init setup_IO_APIC_irqs(void)
1418{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001419 int apic, pin, idx, irq;
1420 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001421 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001422 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001423 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1426
1427 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001428 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001430 idx = find_irq_entry(apic, pin, mp_INT);
1431 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001432 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001433 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001434 apic_printk(APIC_VERBOSE,
1435 KERN_DEBUG " %d-%d",
1436 mp_ioapics[apic].mp_apicid,
1437 pin);
1438 } else
1439 apic_printk(APIC_VERBOSE, " %d-%d",
1440 mp_ioapics[apic].mp_apicid,
1441 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001442 continue;
1443 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001444 if (notcon) {
1445 apic_printk(APIC_VERBOSE,
1446 " (apicid-pin) not connected\n");
1447 notcon = 0;
1448 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001449
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001450 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001451#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001452 if (multi_timer_check(apic, irq))
1453 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001454#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001455 desc = irq_to_desc_alloc_cpu(irq, cpu);
1456 if (!desc) {
1457 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1458 continue;
1459 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001460 cfg = desc->chip_data;
1461 add_pin_to_irq_cpu(cfg, cpu, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001462
Yinghai Lu3145e942008-12-05 18:58:34 -08001463 setup_IO_APIC_irq(apic, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001464 irq_trigger(idx), irq_polarity(idx));
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001468 if (notcon)
1469 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001470 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
1473/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001474 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001476static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1477 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
1479 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Ingo Molnar54168ed2008-08-20 09:07:45 +02001481#ifdef CONFIG_INTR_REMAP
1482 if (intr_remapping_enabled)
1483 return;
1484#endif
1485
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001486 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 /*
1489 * We use logical delivery to get the timer IRQ
1490 * to the first CPU.
1491 */
1492 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001493 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001494 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 entry.delivery_mode = INT_DELIVERY_MODE;
1496 entry.polarity = 0;
1497 entry.trigger = 0;
1498 entry.vector = vector;
1499
1500 /*
1501 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001502 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001504 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 /*
1507 * Add it to the IO-APIC irq-routing table:
1508 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001509 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001512
1513__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 int apic, i;
1516 union IO_APIC_reg_00 reg_00;
1517 union IO_APIC_reg_01 reg_01;
1518 union IO_APIC_reg_02 reg_02;
1519 union IO_APIC_reg_03 reg_03;
1520 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001521 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001522 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001523 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 if (apic_verbosity == APIC_QUIET)
1526 return;
1527
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001528 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 for (i = 0; i < nr_ioapics; i++)
1530 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001531 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 /*
1534 * We are a bit conservative about what we expect. We have to
1535 * know about every hardware change ASAP.
1536 */
1537 printk(KERN_INFO "testing the IO APIC.......................\n");
1538
1539 for (apic = 0; apic < nr_ioapics; apic++) {
1540
1541 spin_lock_irqsave(&ioapic_lock, flags);
1542 reg_00.raw = io_apic_read(apic, 0);
1543 reg_01.raw = io_apic_read(apic, 1);
1544 if (reg_01.bits.version >= 0x10)
1545 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001546 if (reg_01.bits.version >= 0x20)
1547 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 spin_unlock_irqrestore(&ioapic_lock, flags);
1549
Ingo Molnar54168ed2008-08-20 09:07:45 +02001550 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001551 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1553 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1554 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1555 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Ingo Molnar54168ed2008-08-20 09:07:45 +02001557 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1561 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
1563 /*
1564 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1565 * but the value of reg_02 is read as the previous read register
1566 * value, so ignore it if reg_02 == reg_01.
1567 */
1568 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1569 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1570 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572
1573 /*
1574 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1575 * or reg_03, but the value of reg_0[23] is read as the previous read
1576 * register value, so ignore it if reg_03 == reg_0[12].
1577 */
1578 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1579 reg_03.raw != reg_01.raw) {
1580 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1581 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 }
1583
1584 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1585
Yinghai Lud83e94a2008-08-19 20:50:33 -07001586 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1587 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 for (i = 0; i <= reg_01.bits.entries; i++) {
1590 struct IO_APIC_route_entry entry;
1591
Andi Kleencf4c6a22006-09-26 10:52:30 +02001592 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Ingo Molnar54168ed2008-08-20 09:07:45 +02001594 printk(KERN_DEBUG " %02x %03X ",
1595 i,
1596 entry.dest
1597 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
1599 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1600 entry.mask,
1601 entry.trigger,
1602 entry.irr,
1603 entry.polarity,
1604 entry.delivery_status,
1605 entry.dest_mode,
1606 entry.delivery_mode,
1607 entry.vector
1608 );
1609 }
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001612 for_each_irq_desc(irq, desc) {
1613 struct irq_pin_list *entry;
1614
1615 if (!desc)
1616 continue;
1617 cfg = desc->chip_data;
1618 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001619 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001621 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 for (;;) {
1623 printk("-> %d:%d", entry->apic, entry->pin);
1624 if (!entry->next)
1625 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001626 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 }
1628 printk("\n");
1629 }
1630
1631 printk(KERN_INFO ".................................... done.\n");
1632
1633 return;
1634}
1635
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001636__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
1638 unsigned int v;
1639 int i, j;
1640
1641 if (apic_verbosity == APIC_QUIET)
1642 return;
1643
1644 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1645 for (i = 0; i < 8; i++) {
1646 v = apic_read(base + i*0x10);
1647 for (j = 0; j < 32; j++) {
1648 if (v & (1<<j))
1649 printk("1");
1650 else
1651 printk("0");
1652 }
1653 printk("\n");
1654 }
1655}
1656
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001657__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001660 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 if (apic_verbosity == APIC_QUIET)
1663 return;
1664
1665 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1666 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001667 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001668 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 v = apic_read(APIC_LVR);
1670 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1671 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001672 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 v = apic_read(APIC_TASKPRI);
1675 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1676
Ingo Molnar54168ed2008-08-20 09:07:45 +02001677 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001678 if (!APIC_XAPIC(ver)) {
1679 v = apic_read(APIC_ARBPRI);
1680 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1681 v & APIC_ARBPRI_MASK);
1682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 v = apic_read(APIC_PROCPRI);
1684 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1685 }
1686
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001687 /*
1688 * Remote read supported only in the 82489DX and local APIC for
1689 * Pentium processors.
1690 */
1691 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1692 v = apic_read(APIC_RRR);
1693 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1694 }
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 v = apic_read(APIC_LDR);
1697 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001698 if (!x2apic_enabled()) {
1699 v = apic_read(APIC_DFR);
1700 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 v = apic_read(APIC_SPIV);
1703 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1704
1705 printk(KERN_DEBUG "... APIC ISR field:\n");
1706 print_APIC_bitfield(APIC_ISR);
1707 printk(KERN_DEBUG "... APIC TMR field:\n");
1708 print_APIC_bitfield(APIC_TMR);
1709 printk(KERN_DEBUG "... APIC IRR field:\n");
1710 print_APIC_bitfield(APIC_IRR);
1711
Ingo Molnar54168ed2008-08-20 09:07:45 +02001712 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1713 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 v = apic_read(APIC_ESR);
1717 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1718 }
1719
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001720 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001721 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1722 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 v = apic_read(APIC_LVTT);
1725 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1726
1727 if (maxlvt > 3) { /* PC is LVT#4. */
1728 v = apic_read(APIC_LVTPC);
1729 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1730 }
1731 v = apic_read(APIC_LVT0);
1732 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1733 v = apic_read(APIC_LVT1);
1734 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1735
1736 if (maxlvt > 2) { /* ERR is LVT#3. */
1737 v = apic_read(APIC_LVTERR);
1738 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1739 }
1740
1741 v = apic_read(APIC_TMICT);
1742 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1743 v = apic_read(APIC_TMCCT);
1744 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1745 v = apic_read(APIC_TDCR);
1746 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1747 printk("\n");
1748}
1749
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001750__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001752 int cpu;
1753
1754 preempt_disable();
1755 for_each_online_cpu(cpu)
1756 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1757 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758}
1759
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001760__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 unsigned int v;
1763 unsigned long flags;
1764
1765 if (apic_verbosity == APIC_QUIET)
1766 return;
1767
1768 printk(KERN_DEBUG "\nprinting PIC contents\n");
1769
1770 spin_lock_irqsave(&i8259A_lock, flags);
1771
1772 v = inb(0xa1) << 8 | inb(0x21);
1773 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1774
1775 v = inb(0xa0) << 8 | inb(0x20);
1776 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1777
Ingo Molnar54168ed2008-08-20 09:07:45 +02001778 outb(0x0b,0xa0);
1779 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001781 outb(0x0a,0xa0);
1782 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 spin_unlock_irqrestore(&i8259A_lock, flags);
1785
1786 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1787
1788 v = inb(0x4d1) << 8 | inb(0x4d0);
1789 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1790}
1791
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001792__apicdebuginit(int) print_all_ICs(void)
1793{
1794 print_PIC();
1795 print_all_local_APICs();
1796 print_IO_APIC();
1797
1798 return 0;
1799}
1800
1801fs_initcall(print_all_ICs);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Yinghai Luefa25592008-08-19 20:50:36 -07001804/* Where if anywhere is the i8259 connect in external int mode */
1805static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1806
Ingo Molnar54168ed2008-08-20 09:07:45 +02001807void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001810 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001811 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 unsigned long flags;
1813
Ingo Molnar54168ed2008-08-20 09:07:45 +02001814#ifdef CONFIG_X86_32
1815 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 if (!pirqs_enabled)
1817 for (i = 0; i < MAX_PIRQS; i++)
1818 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001819#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 /*
1822 * The number of IO-APIC IRQ registers (== #pins):
1823 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001824 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001826 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001828 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1829 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001830 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001831 int pin;
1832 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001833 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001834 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001835 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001836
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001837 /* If the interrupt line is enabled and in ExtInt mode
1838 * I have found the pin where the i8259 is connected.
1839 */
1840 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1841 ioapic_i8259.apic = apic;
1842 ioapic_i8259.pin = pin;
1843 goto found_i8259;
1844 }
1845 }
1846 }
1847 found_i8259:
1848 /* Look to see what if the MP table has reported the ExtINT */
1849 /* If we could not find the appropriate pin by looking at the ioapic
1850 * the i8259 probably is not connected the ioapic but give the
1851 * mptable a chance anyway.
1852 */
1853 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1854 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1855 /* Trust the MP table if nothing is setup in the hardware */
1856 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1857 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1858 ioapic_i8259.pin = i8259_pin;
1859 ioapic_i8259.apic = i8259_apic;
1860 }
1861 /* Complain if the MP table and the hardware disagree */
1862 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1863 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1864 {
1865 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 }
1867
1868 /*
1869 * Do not trust the IO-APIC being empty at bootup
1870 */
1871 clear_IO_APIC();
1872}
1873
1874/*
1875 * Not an __init, needed by the reboot code
1876 */
1877void disable_IO_APIC(void)
1878{
1879 /*
1880 * Clear the IO-APIC before rebooting:
1881 */
1882 clear_IO_APIC();
1883
Eric W. Biederman650927e2005-06-25 14:57:44 -07001884 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001885 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001886 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001887 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001888 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001889 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001890 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001891
1892 memset(&entry, 0, sizeof(entry));
1893 entry.mask = 0; /* Enabled */
1894 entry.trigger = 0; /* Edge */
1895 entry.irr = 0;
1896 entry.polarity = 0; /* High */
1897 entry.delivery_status = 0;
1898 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001899 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001900 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001901 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001902
1903 /*
1904 * Add it to the IO-APIC irq-routing table:
1905 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001906 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001907 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001908
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001909 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Ingo Molnar54168ed2008-08-20 09:07:45 +02001912#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913/*
1914 * function to set the IO-APIC physical IDs based on the
1915 * values stored in the MPC table.
1916 *
1917 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1918 */
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920static void __init setup_ioapic_ids_from_mpc(void)
1921{
1922 union IO_APIC_reg_00 reg_00;
1923 physid_mask_t phys_id_present_map;
1924 int apic;
1925 int i;
1926 unsigned char old_id;
1927 unsigned long flags;
1928
Yinghai Lua4dbc342008-07-25 02:14:28 -07001929 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001930 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001933 * Don't check I/O APIC IDs for xAPIC systems. They have
1934 * no meaning without the serial APIC bus.
1935 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001936 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1937 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001938 return;
1939 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 * This is broken; anything with a real cpu count has to
1941 * circumvent this idiocy regardless.
1942 */
1943 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1944
1945 /*
1946 * Set the IOAPIC ID to the value stored in the MPC table.
1947 */
1948 for (apic = 0; apic < nr_ioapics; apic++) {
1949
1950 /* Read the register 0 value */
1951 spin_lock_irqsave(&ioapic_lock, flags);
1952 reg_00.raw = io_apic_read(apic, 0);
1953 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001954
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001955 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001957 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001959 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1961 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001962 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 /*
1966 * Sanity check, is the ID really free? Every APIC in a
1967 * system must have a unique ID or we get lots of nice
1968 * 'stuck on smp_invalidate_needed IPI wait' messages.
1969 */
1970 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001971 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001973 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 for (i = 0; i < get_physical_broadcast(); i++)
1975 if (!physid_isset(i, phys_id_present_map))
1976 break;
1977 if (i >= get_physical_broadcast())
1978 panic("Max APIC ID exceeded!\n");
1979 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1980 i);
1981 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001982 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 } else {
1984 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001985 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 apic_printk(APIC_VERBOSE, "Setting %d in the "
1987 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001988 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1990 }
1991
1992
1993 /*
1994 * We need to adjust the IRQ routing table
1995 * if the ID changed.
1996 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001997 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001999 if (mp_irqs[i].mp_dstapic == old_id)
2000 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002001 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
2003 /*
2004 * Read the right value from the MPC table and
2005 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 apic_printk(APIC_VERBOSE, KERN_INFO
2008 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002009 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002011 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002013 io_apic_write(apic, 0, reg_00.raw);
2014 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016 /*
2017 * Sanity check
2018 */
2019 spin_lock_irqsave(&ioapic_lock, flags);
2020 reg_00.raw = io_apic_read(apic, 0);
2021 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002022 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 printk("could not set ID!\n");
2024 else
2025 apic_printk(APIC_VERBOSE, " ok.\n");
2026 }
2027}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002028#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002030int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002031
2032static int __init notimercheck(char *s)
2033{
2034 no_timer_check = 1;
2035 return 1;
2036}
2037__setup("no_timer_check", notimercheck);
2038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039/*
2040 * There is a nasty bug in some older SMP boards, their mptable lies
2041 * about the timer IRQ. We do the following to work around the situation:
2042 *
2043 * - timer IRQ defaults to IO-APIC IRQ
2044 * - if this function detects that timer IRQs are defunct, then we fall
2045 * back to ISA timer IRQs
2046 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002047static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
2049 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002050 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Zachary Amsden8542b202006-12-07 02:14:09 +01002052 if (no_timer_check)
2053 return 1;
2054
Ingo Molnar4aae0702007-12-18 18:05:58 +01002055 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 local_irq_enable();
2057 /* Let ten ticks pass... */
2058 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002059 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /*
2062 * Expect a few ticks at least, to be sure some possible
2063 * glue logic does not lock up after one or two first
2064 * ticks in a non-ExtINT mode. Also the local APIC
2065 * might have cached one ExtINT interrupt. Finally, at
2066 * least one tick may be lost due to delays.
2067 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002068
2069 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002070 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return 0;
2073}
2074
2075/*
2076 * In the SMP+IOAPIC case it might happen that there are an unspecified
2077 * number of pending IRQ events unhandled. These cases are very rare,
2078 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2079 * better to do it this way as thus we do not have to be aware of
2080 * 'pending' interrupts in the IRQ path, except at this point.
2081 */
2082/*
2083 * Edge triggered needs to resend any interrupt
2084 * that was delayed but this is now handled in the device
2085 * independent code.
2086 */
2087
2088/*
2089 * Starting up a edge-triggered IO-APIC interrupt is
2090 * nasty - we need to make sure that we get the edge.
2091 * If it is already asserted for some reason, we need
2092 * return 1 to indicate that is was pending.
2093 *
2094 * This is not complete - we should be able to fake
2095 * an edge even if it isn't on the 8259A...
2096 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002097
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002098static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 int was_pending = 0;
2101 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002102 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002105 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 disable_8259A_irq(irq);
2107 if (i8259A_irq_pending(irq))
2108 was_pending = 1;
2109 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002110 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002111 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 spin_unlock_irqrestore(&ioapic_lock, flags);
2113
2114 return was_pending;
2115}
2116
Ingo Molnar54168ed2008-08-20 09:07:45 +02002117#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002118static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002120
2121 struct irq_cfg *cfg = irq_cfg(irq);
2122 unsigned long flags;
2123
2124 spin_lock_irqsave(&vector_lock, flags);
Mike Travise7986732008-12-16 17:33:52 -08002125 send_IPI_mask(&cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002126 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002127
2128 return 1;
2129}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002130#else
2131static int ioapic_retrigger_irq(unsigned int irq)
2132{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002133 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002134
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002135 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002136}
2137#endif
2138
2139/*
2140 * Level and edge triggered IO-APIC interrupts need different handling,
2141 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2142 * handled with the level-triggered descriptor, but that one has slightly
2143 * more overhead. Level-triggered interrupts cannot be handled with the
2144 * edge-triggered handler, without risking IRQ storms and other ugly
2145 * races.
2146 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002147
Yinghai Lu497c9a12008-08-19 20:50:28 -07002148#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002149
2150#ifdef CONFIG_INTR_REMAP
2151static void ir_irq_migration(struct work_struct *work);
2152
2153static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2154
2155/*
2156 * Migrate the IO-APIC irq in the presence of intr-remapping.
2157 *
2158 * For edge triggered, irq migration is a simple atomic update(of vector
2159 * and cpu destination) of IRTE and flush the hardware cache.
2160 *
2161 * For level triggered, we need to modify the io-apic RTE aswell with the update
2162 * vector information, along with modifying IRTE with vector and destination.
2163 * So irq migration for level triggered is little bit more complex compared to
2164 * edge triggered migration. But the good news is, we use the same algorithm
2165 * for level triggered migration as we have today, only difference being,
2166 * we now initiate the irq migration from process context instead of the
2167 * interrupt context.
2168 *
2169 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2170 * suppression) to the IO-APIC, level triggered irq migration will also be
2171 * as simple as edge triggered migration and we can do the irq migration
2172 * with a simple atomic update to IO-APIC RTE.
2173 */
Mike Travise7986732008-12-16 17:33:52 -08002174static void
2175migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002176{
2177 struct irq_cfg *cfg;
Mike Travise7986732008-12-16 17:33:52 -08002178 cpumask_t tmpmask;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002179 struct irte irte;
2180 int modify_ioapic_rte;
2181 unsigned int dest;
2182 unsigned long flags;
Yinghai Lu3145e942008-12-05 18:58:34 -08002183 unsigned int irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002184
Mike Travise7986732008-12-16 17:33:52 -08002185 cpus_and(tmpmask, *mask, cpu_online_map);
2186 if (cpus_empty(tmpmask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002187 return;
2188
Yinghai Lu3145e942008-12-05 18:58:34 -08002189 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002190 if (get_irte(irq, &irte))
2191 return;
2192
Yinghai Lu3145e942008-12-05 18:58:34 -08002193 cfg = desc->chip_data;
2194 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002195 return;
2196
Yinghai Lu3145e942008-12-05 18:58:34 -08002197 set_extra_move_desc(desc, mask);
2198
Mike Travise7986732008-12-16 17:33:52 -08002199 cpus_and(tmpmask, cfg->domain, *mask);
2200 dest = cpu_mask_to_apicid(&tmpmask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002201
Ingo Molnar54168ed2008-08-20 09:07:45 +02002202 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2203 if (modify_ioapic_rte) {
2204 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08002205 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002206 spin_unlock_irqrestore(&ioapic_lock, flags);
2207 }
2208
2209 irte.vector = cfg->vector;
2210 irte.dest_id = IRTE_DEST(dest);
2211
2212 /*
2213 * Modified the IRTE and flushes the Interrupt entry cache.
2214 */
2215 modify_irte(irq, &irte);
2216
2217 if (cfg->move_in_progress) {
Mike Travise7986732008-12-16 17:33:52 -08002218 cpus_and(tmpmask, cfg->old_domain, cpu_online_map);
2219 cfg->move_cleanup_count = cpus_weight(tmpmask);
2220 send_IPI_mask(&tmpmask, IRQ_MOVE_CLEANUP_VECTOR);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002221 cfg->move_in_progress = 0;
2222 }
2223
Mike Travise7986732008-12-16 17:33:52 -08002224 desc->affinity = *mask;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002225}
2226
Yinghai Lu3145e942008-12-05 18:58:34 -08002227static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002228{
2229 int ret = -1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002230 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002231
Yinghai Lu3145e942008-12-05 18:58:34 -08002232 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002233
Yinghai Lu3145e942008-12-05 18:58:34 -08002234 if (io_apic_level_ack_pending(cfg)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002235 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002236 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002237 * vector information in the IO-APIC RTE and that will confuse
2238 * the EOI broadcast performed by cpu.
2239 * So, delay the irq migration to the next instance.
2240 */
2241 schedule_delayed_work(&ir_migration_work, 1);
2242 goto unmask;
2243 }
2244
2245 /* everthing is clear. we have right of way */
Mike Travise7986732008-12-16 17:33:52 -08002246 migrate_ioapic_irq_desc(desc, &desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002247
2248 ret = 0;
2249 desc->status &= ~IRQ_MOVE_PENDING;
2250 cpus_clear(desc->pending_mask);
2251
2252unmask:
Yinghai Lu3145e942008-12-05 18:58:34 -08002253 unmask_IO_APIC_irq_desc(desc);
2254
Ingo Molnar54168ed2008-08-20 09:07:45 +02002255 return ret;
2256}
2257
2258static void ir_irq_migration(struct work_struct *work)
2259{
2260 unsigned int irq;
2261 struct irq_desc *desc;
2262
2263 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002264 if (!desc)
2265 continue;
2266
Ingo Molnar54168ed2008-08-20 09:07:45 +02002267 if (desc->status & IRQ_MOVE_PENDING) {
2268 unsigned long flags;
2269
2270 spin_lock_irqsave(&desc->lock, flags);
2271 if (!desc->chip->set_affinity ||
2272 !(desc->status & IRQ_MOVE_PENDING)) {
2273 desc->status &= ~IRQ_MOVE_PENDING;
2274 spin_unlock_irqrestore(&desc->lock, flags);
2275 continue;
2276 }
2277
Rusty Russell0de26522008-12-13 21:20:26 +10302278 desc->chip->set_affinity(irq, &desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002279 spin_unlock_irqrestore(&desc->lock, flags);
2280 }
2281 }
2282}
2283
2284/*
2285 * Migrates the IRQ destination in the process context.
2286 */
Rusty Russell968ea6d2008-12-13 21:55:51 +10302287static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2288 const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -08002289{
2290 if (desc->status & IRQ_LEVEL) {
2291 desc->status |= IRQ_MOVE_PENDING;
Rusty Russell968ea6d2008-12-13 21:55:51 +10302292 cpumask_copy(&desc->pending_mask, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002293 migrate_irq_remapped_level_desc(desc);
2294 return;
2295 }
2296
Mike Travise7986732008-12-16 17:33:52 -08002297 migrate_ioapic_irq_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002298}
Rusty Russell0de26522008-12-13 21:20:26 +10302299static void set_ir_ioapic_affinity_irq(unsigned int irq,
2300 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002301{
2302 struct irq_desc *desc = irq_to_desc(irq);
2303
Yinghai Lu3145e942008-12-05 18:58:34 -08002304 set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002305}
2306#endif
2307
Yinghai Lu497c9a12008-08-19 20:50:28 -07002308asmlinkage void smp_irq_move_cleanup_interrupt(void)
2309{
2310 unsigned vector, me;
2311 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002312#ifdef CONFIG_X86_64
2313 exit_idle();
2314#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002315 irq_enter();
2316
2317 me = smp_processor_id();
2318 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2319 unsigned int irq;
2320 struct irq_desc *desc;
2321 struct irq_cfg *cfg;
2322 irq = __get_cpu_var(vector_irq)[vector];
2323
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002324 if (irq == -1)
2325 continue;
2326
Yinghai Lu497c9a12008-08-19 20:50:28 -07002327 desc = irq_to_desc(irq);
2328 if (!desc)
2329 continue;
2330
2331 cfg = irq_cfg(irq);
2332 spin_lock(&desc->lock);
2333 if (!cfg->move_cleanup_count)
2334 goto unlock;
2335
2336 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2337 goto unlock;
2338
2339 __get_cpu_var(vector_irq)[vector] = -1;
2340 cfg->move_cleanup_count--;
2341unlock:
2342 spin_unlock(&desc->lock);
2343 }
2344
2345 irq_exit();
2346}
2347
Yinghai Lu3145e942008-12-05 18:58:34 -08002348static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002349{
Yinghai Lu3145e942008-12-05 18:58:34 -08002350 struct irq_desc *desc = *descp;
2351 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002352 unsigned vector, me;
2353
2354 if (likely(!cfg->move_in_progress))
2355 return;
2356
2357 vector = ~get_irq_regs()->orig_ax;
2358 me = smp_processor_id();
2359 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2360 cpumask_t cleanup_mask;
2361
2362 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2363 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
Mike Travise7986732008-12-16 17:33:52 -08002364 send_IPI_mask(&cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002365 cfg->move_in_progress = 0;
2366 }
2367}
2368#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002369static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002370#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002371
Ingo Molnar54168ed2008-08-20 09:07:45 +02002372#ifdef CONFIG_INTR_REMAP
2373static void ack_x2apic_level(unsigned int irq)
2374{
2375 ack_x2APIC_irq();
2376}
2377
2378static void ack_x2apic_edge(unsigned int irq)
2379{
2380 ack_x2APIC_irq();
2381}
Yinghai Lu3145e942008-12-05 18:58:34 -08002382
Ingo Molnar54168ed2008-08-20 09:07:45 +02002383#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002384
Yinghai Lu1d025192008-08-19 20:50:34 -07002385static void ack_apic_edge(unsigned int irq)
2386{
Yinghai Lu3145e942008-12-05 18:58:34 -08002387 struct irq_desc *desc = irq_to_desc(irq);
2388
2389 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002390 move_native_irq(irq);
2391 ack_APIC_irq();
2392}
2393
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002394atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002395
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002396static void ack_apic_level(unsigned int irq)
2397{
Yinghai Lu3145e942008-12-05 18:58:34 -08002398 struct irq_desc *desc = irq_to_desc(irq);
2399
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002400#ifdef CONFIG_X86_32
2401 unsigned long v;
2402 int i;
2403#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002404 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002405 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002406
Yinghai Lu3145e942008-12-05 18:58:34 -08002407 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002408#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002409 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002410 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002411 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002412 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002413 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002414#endif
2415
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002416#ifdef CONFIG_X86_32
2417 /*
2418 * It appears there is an erratum which affects at least version 0x11
2419 * of I/O APIC (that's the 82093AA and cores integrated into various
2420 * chipsets). Under certain conditions a level-triggered interrupt is
2421 * erroneously delivered as edge-triggered one but the respective IRR
2422 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2423 * message but it will never arrive and further interrupts are blocked
2424 * from the source. The exact reason is so far unknown, but the
2425 * phenomenon was observed when two consecutive interrupt requests
2426 * from a given source get delivered to the same CPU and the source is
2427 * temporarily disabled in between.
2428 *
2429 * A workaround is to simulate an EOI message manually. We achieve it
2430 * by setting the trigger mode to edge and then to level when the edge
2431 * trigger mode gets detected in the TMR of a local APIC for a
2432 * level-triggered interrupt. We mask the source for the time of the
2433 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2434 * The idea is from Manfred Spraul. --macro
2435 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002436 cfg = desc->chip_data;
2437 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002438
2439 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2440#endif
2441
Ingo Molnar54168ed2008-08-20 09:07:45 +02002442 /*
2443 * We must acknowledge the irq before we move it or the acknowledge will
2444 * not propagate properly.
2445 */
2446 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002447
Ingo Molnar54168ed2008-08-20 09:07:45 +02002448 /* Now we can move and renable the irq */
2449 if (unlikely(do_unmask_irq)) {
2450 /* Only migrate the irq if the ack has been received.
2451 *
2452 * On rare occasions the broadcast level triggered ack gets
2453 * delayed going to ioapics, and if we reprogram the
2454 * vector while Remote IRR is still set the irq will never
2455 * fire again.
2456 *
2457 * To prevent this scenario we read the Remote IRR bit
2458 * of the ioapic. This has two effects.
2459 * - On any sane system the read of the ioapic will
2460 * flush writes (and acks) going to the ioapic from
2461 * this cpu.
2462 * - We get to see if the ACK has actually been delivered.
2463 *
2464 * Based on failed experiments of reprogramming the
2465 * ioapic entry from outside of irq context starting
2466 * with masking the ioapic entry and then polling until
2467 * Remote IRR was clear before reprogramming the
2468 * ioapic I don't trust the Remote IRR bit to be
2469 * completey accurate.
2470 *
2471 * However there appears to be no other way to plug
2472 * this race, so if the Remote IRR bit is not
2473 * accurate and is causing problems then it is a hardware bug
2474 * and you can go talk to the chipset vendor about it.
2475 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002476 cfg = desc->chip_data;
2477 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002478 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002479 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002480 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002481
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002482#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002483 if (!(v & (1 << (i & 0x1f)))) {
2484 atomic_inc(&irq_mis_count);
2485 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002486 __mask_and_edge_IO_APIC_irq(cfg);
2487 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002488 spin_unlock(&ioapic_lock);
2489 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002490#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002491}
Yinghai Lu1d025192008-08-19 20:50:34 -07002492
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002493static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002494 .name = "IO-APIC",
2495 .startup = startup_ioapic_irq,
2496 .mask = mask_IO_APIC_irq,
2497 .unmask = unmask_IO_APIC_irq,
2498 .ack = ack_apic_edge,
2499 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002500#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002501 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002502#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002503 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504};
2505
Ingo Molnar54168ed2008-08-20 09:07:45 +02002506#ifdef CONFIG_INTR_REMAP
2507static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002508 .name = "IR-IO-APIC",
2509 .startup = startup_ioapic_irq,
2510 .mask = mask_IO_APIC_irq,
2511 .unmask = unmask_IO_APIC_irq,
2512 .ack = ack_x2apic_edge,
2513 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002514#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002515 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002516#endif
2517 .retrigger = ioapic_retrigger_irq,
2518};
2519#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521static inline void init_IO_APIC_traps(void)
2522{
2523 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002524 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002525 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 /*
2528 * NOTE! The local APIC isn't very good at handling
2529 * multiple interrupts at the same interrupt level.
2530 * As the interrupt level is determined by taking the
2531 * vector number and shifting that right by 4, we
2532 * want to spread these out a bit so that they don't
2533 * all fall in the same interrupt level.
2534 *
2535 * Also, we've got to be careful not to trash gate
2536 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2537 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002538 for_each_irq_desc(irq, desc) {
2539 if (!desc)
2540 continue;
2541
2542 cfg = desc->chip_data;
2543 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 /*
2545 * Hmm.. We don't have an entry for this,
2546 * so default to an old-fashioned 8259
2547 * interrupt if we can..
2548 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002549 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002551 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002553 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555 }
2556}
2557
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002558/*
2559 * The local APIC irq-chip implementation:
2560 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002562static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563{
2564 unsigned long v;
2565
2566 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002567 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002570static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002572 unsigned long v;
2573
2574 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002575 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576}
2577
Yinghai Lu3145e942008-12-05 18:58:34 -08002578static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002579{
2580 ack_APIC_irq();
2581}
2582
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002583static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002584 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002585 .mask = mask_lapic_irq,
2586 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002587 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588};
2589
Yinghai Lu3145e942008-12-05 18:58:34 -08002590static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002591{
Yinghai Lu08678b02008-08-19 20:50:05 -07002592 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002593 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2594 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002595}
2596
Jan Beuliche9427102008-01-30 13:31:24 +01002597static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002600 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 * We put the 8259A master into AEOI mode and
2602 * unmask on all local APICs LVT0 as NMI.
2603 *
2604 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2605 * is from Maciej W. Rozycki - so we do not have to EOI from
2606 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2609
Jan Beuliche9427102008-01-30 13:31:24 +01002610 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
2612 apic_printk(APIC_VERBOSE, " done.\n");
2613}
2614
2615/*
2616 * This looks a bit hackish but it's about the only one way of sending
2617 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2618 * not support the ExtINT mode, unfortunately. We need to send these
2619 * cycles as some i82489DX-based boards have glue logic that keeps the
2620 * 8259A interrupt line asserted until INTA. --macro
2621 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002622static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002624 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 struct IO_APIC_route_entry entry0, entry1;
2626 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002628 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002629 if (pin == -1) {
2630 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002632 }
2633 apic = find_isa_irq_apic(8, mp_INT);
2634 if (apic == -1) {
2635 WARN_ON_ONCE(1);
2636 return;
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Andi Kleencf4c6a22006-09-26 10:52:30 +02002639 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002640 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642 memset(&entry1, 0, sizeof(entry1));
2643
2644 entry1.dest_mode = 0; /* physical delivery */
2645 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002646 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 entry1.delivery_mode = dest_ExtINT;
2648 entry1.polarity = entry0.polarity;
2649 entry1.trigger = 0;
2650 entry1.vector = 0;
2651
Andi Kleencf4c6a22006-09-26 10:52:30 +02002652 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 save_control = CMOS_READ(RTC_CONTROL);
2655 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2656 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2657 RTC_FREQ_SELECT);
2658 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2659
2660 i = 100;
2661 while (i-- > 0) {
2662 mdelay(10);
2663 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2664 i -= 10;
2665 }
2666
2667 CMOS_WRITE(save_control, RTC_CONTROL);
2668 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002669 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670
Andi Kleencf4c6a22006-09-26 10:52:30 +02002671 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672}
2673
Yinghai Luefa25592008-08-19 20:50:36 -07002674static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002675/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002676static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002677{
2678 disable_timer_pin_1 = 1;
2679 return 0;
2680}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002681early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002682
2683int timer_through_8259 __initdata;
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685/*
2686 * This code may look a bit paranoid, but it's supposed to cooperate with
2687 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2688 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2689 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002690 *
2691 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002693static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Yinghai Lu3145e942008-12-05 18:58:34 -08002695 struct irq_desc *desc = irq_to_desc(0);
2696 struct irq_cfg *cfg = desc->chip_data;
2697 int cpu = boot_cpu_id;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002698 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002699 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002700 unsigned int ver;
2701 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002702
2703 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002704
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002705 ver = apic_read(APIC_LVR);
2706 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002707
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 /*
2709 * get/set the timer IRQ vector:
2710 */
2711 disable_8259A_irq(0);
Yinghai Lu3145e942008-12-05 18:58:34 -08002712 assign_irq_vector(0, cfg, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002715 * As IRQ0 is to be enabled in the 8259A, the virtual
2716 * wire has to be disabled in the local APIC. Also
2717 * timer interrupts need to be acknowledged manually in
2718 * the 8259A for the i82489DX when using the NMI
2719 * watchdog as that APIC treats NMIs as level-triggered.
2720 * The AEOI mode will finish them in the 8259A
2721 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002723 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002725#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002726 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002727#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002729 pin1 = find_isa_irq_pin(0, mp_INT);
2730 apic1 = find_isa_irq_apic(0, mp_INT);
2731 pin2 = ioapic_i8259.pin;
2732 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002734 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2735 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002736 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002738 /*
2739 * Some BIOS writers are clueless and report the ExtINTA
2740 * I/O APIC input from the cascaded 8259A as the timer
2741 * interrupt input. So just in case, if only one pin
2742 * was found above, try it both directly and through the
2743 * 8259A.
2744 */
2745 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002746#ifdef CONFIG_INTR_REMAP
2747 if (intr_remapping_enabled)
2748 panic("BIOS bug: timer not connected to IO-APIC");
2749#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002750 pin1 = pin2;
2751 apic1 = apic2;
2752 no_pin1 = 1;
2753 } else if (pin2 == -1) {
2754 pin2 = pin1;
2755 apic2 = apic1;
2756 }
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (pin1 != -1) {
2759 /*
2760 * Ok, does IRQ0 through the IOAPIC work?
2761 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002762 if (no_pin1) {
Yinghai Lu3145e942008-12-05 18:58:34 -08002763 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002764 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002765 }
Yinghai Lu3145e942008-12-05 18:58:34 -08002766 unmask_IO_APIC_irq_desc(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (timer_irq_works()) {
2768 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 setup_nmi();
2770 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002772 if (disable_timer_pin_1 > 0)
2773 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002774 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002776#ifdef CONFIG_INTR_REMAP
2777 if (intr_remapping_enabled)
2778 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2779#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002780 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002781 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002782 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2783 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002785 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2786 "(IRQ0) through the 8259A ...\n");
2787 apic_printk(APIC_QUIET, KERN_INFO
2788 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * legacy devices should be connected to IO APIC #0
2791 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002792 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002793 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Yinghai Lu3145e942008-12-05 18:58:34 -08002794 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002795 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002797 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002798 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002800 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002802 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
2806 /*
2807 * Cleanup, just in case ...
2808 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002809 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002810 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002811 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
2814 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002815 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2816 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002817 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002819#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002820 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002823 apic_printk(APIC_QUIET, KERN_INFO
2824 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Yinghai Lu3145e942008-12-05 18:58:34 -08002826 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002827 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 enable_8259A_irq(0);
2829
2830 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002831 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002832 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002834 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002835 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002836 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002838 apic_printk(APIC_QUIET, KERN_INFO
2839 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 init_8259A(0);
2842 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002843 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 unlock_ExtINT_logic();
2846
2847 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002848 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002849 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002851 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002853 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002854out:
2855 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}
2857
2858/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002859 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2860 * to devices. However there may be an I/O APIC pin available for
2861 * this interrupt regardless. The pin may be left unconnected, but
2862 * typically it will be reused as an ExtINT cascade interrupt for
2863 * the master 8259A. In the MPS case such a pin will normally be
2864 * reported as an ExtINT interrupt in the MP table. With ACPI
2865 * there is no provision for ExtINT interrupts, and in the absence
2866 * of an override it would be treated as an ordinary ISA I/O APIC
2867 * interrupt, that is edge-triggered and unmasked by default. We
2868 * used to do this, but it caused problems on some systems because
2869 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2870 * the same ExtINT cascade interrupt to drive the local APIC of the
2871 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2872 * the I/O APIC in all cases now. No actual device should request
2873 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 */
2875#define PIC_IRQS (1 << PIC_CASCADE_IR)
2876
2877void __init setup_IO_APIC(void)
2878{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002879
2880#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002882#else
2883 /*
2884 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2885 */
2886#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002888 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Ingo Molnar54168ed2008-08-20 09:07:45 +02002890 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002891 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002892 * Set up IO-APIC IRQ routing.
2893 */
2894#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002895 if (!acpi_ioapic)
2896 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002897#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 sync_Arb_IDs();
2899 setup_IO_APIC_irqs();
2900 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002901 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
2904/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002905 * Called after all the initialization is done. If we didnt find any
2906 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909static int __init io_apic_bug_finalize(void)
2910{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002911 if (sis_apic_bug == -1)
2912 sis_apic_bug = 0;
2913 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914}
2915
2916late_initcall(io_apic_bug_finalize);
2917
2918struct sysfs_ioapic_data {
2919 struct sys_device dev;
2920 struct IO_APIC_route_entry entry[0];
2921};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002922static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Pavel Machek438510f2005-04-16 15:25:24 -07002924static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925{
2926 struct IO_APIC_route_entry *entry;
2927 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 data = container_of(dev, struct sysfs_ioapic_data, dev);
2931 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002932 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2933 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
2935 return 0;
2936}
2937
2938static int ioapic_resume(struct sys_device *dev)
2939{
2940 struct IO_APIC_route_entry *entry;
2941 struct sysfs_ioapic_data *data;
2942 unsigned long flags;
2943 union IO_APIC_reg_00 reg_00;
2944 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 data = container_of(dev, struct sysfs_ioapic_data, dev);
2947 entry = data->entry;
2948
2949 spin_lock_irqsave(&ioapic_lock, flags);
2950 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002951 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2952 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 io_apic_write(dev->id, 0, reg_00.raw);
2954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002956 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002957 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958
2959 return 0;
2960}
2961
2962static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002963 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 .suspend = ioapic_suspend,
2965 .resume = ioapic_resume,
2966};
2967
2968static int __init ioapic_init_sysfs(void)
2969{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002970 struct sys_device * dev;
2971 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 error = sysdev_class_register(&ioapic_sysdev_class);
2974 if (error)
2975 return error;
2976
Ingo Molnar54168ed2008-08-20 09:07:45 +02002977 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002978 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002980 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (!mp_ioapic_data[i]) {
2982 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2983 continue;
2984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002986 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 dev->cls = &ioapic_sysdev_class;
2988 error = sysdev_register(dev);
2989 if (error) {
2990 kfree(mp_ioapic_data[i]);
2991 mp_ioapic_data[i] = NULL;
2992 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2993 continue;
2994 }
2995 }
2996
2997 return 0;
2998}
2999
3000device_initcall(ioapic_init_sysfs);
3001
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003002/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003003 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003004 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003005unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003006{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003007 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003008 unsigned int irq;
3009 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003010 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003011 struct irq_cfg *cfg_new = NULL;
3012 int cpu = boot_cpu_id;
3013 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003014
3015 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003016 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lube5d5352008-12-05 18:58:33 -08003017 for (new = irq_want; new < NR_IRQS; new++) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003018 if (platform_legacy_irq(new))
3019 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003020
3021 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3022 if (!desc_new) {
3023 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003024 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003025 }
3026 cfg_new = desc_new->chip_data;
3027
3028 if (cfg_new->vector != 0)
3029 continue;
Yinghai Lu3145e942008-12-05 18:58:34 -08003030 if (__assign_irq_vector(new, cfg_new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003031 irq = new;
3032 break;
3033 }
3034 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003035
Yinghai Lu199751d2008-08-19 20:50:27 -07003036 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003037 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003038 /* restore it, in case dynamic_irq_init clear it */
3039 if (desc_new)
3040 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003041 }
3042 return irq;
3043}
3044
Yinghai Lube5d5352008-12-05 18:58:33 -08003045static int nr_irqs_gsi = NR_IRQS_LEGACY;
Yinghai Lu199751d2008-08-19 20:50:27 -07003046int create_irq(void)
3047{
Yinghai Lube5d5352008-12-05 18:58:33 -08003048 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003049 int irq;
3050
Yinghai Lube5d5352008-12-05 18:58:33 -08003051 irq_want = nr_irqs_gsi;
3052 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003053
3054 if (irq == 0)
3055 irq = -1;
3056
3057 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003058}
3059
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003060void destroy_irq(unsigned int irq)
3061{
3062 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003063 struct irq_cfg *cfg;
3064 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003065
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003066 /* store it, in case dynamic_irq_cleanup clear it */
3067 desc = irq_to_desc(irq);
3068 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003069 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003070 /* connect back irq_cfg */
3071 if (desc)
3072 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003073
Ingo Molnar54168ed2008-08-20 09:07:45 +02003074#ifdef CONFIG_INTR_REMAP
3075 free_irte(irq);
3076#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003077 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003078 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003079 spin_unlock_irqrestore(&vector_lock, flags);
3080}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003081
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003082/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003083 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003084 */
3085#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003086static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003087{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003088 struct irq_cfg *cfg;
3089 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003090 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003091 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003092
Yinghai Lu3145e942008-12-05 18:58:34 -08003093 cfg = irq_cfg(irq);
Mike Travise7986732008-12-16 17:33:52 -08003094 tmp = *TARGET_CPUS;
3095 err = assign_irq_vector(irq, cfg, &tmp);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003096 if (err)
3097 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003098
Yinghai Lu497c9a12008-08-19 20:50:28 -07003099 cpus_and(tmp, cfg->domain, tmp);
Mike Travise7986732008-12-16 17:33:52 -08003100 dest = cpu_mask_to_apicid(&tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003101
Ingo Molnar54168ed2008-08-20 09:07:45 +02003102#ifdef CONFIG_INTR_REMAP
3103 if (irq_remapped(irq)) {
3104 struct irte irte;
3105 int ir_index;
3106 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003107
Ingo Molnar54168ed2008-08-20 09:07:45 +02003108 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3109 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003110
Ingo Molnar54168ed2008-08-20 09:07:45 +02003111 memset (&irte, 0, sizeof(irte));
3112
3113 irte.present = 1;
3114 irte.dst_mode = INT_DEST_MODE;
3115 irte.trigger_mode = 0; /* edge */
3116 irte.dlvry_mode = INT_DELIVERY_MODE;
3117 irte.vector = cfg->vector;
3118 irte.dest_id = IRTE_DEST(dest);
3119
3120 modify_irte(irq, &irte);
3121
3122 msg->address_hi = MSI_ADDR_BASE_HI;
3123 msg->data = sub_handle;
3124 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3125 MSI_ADDR_IR_SHV |
3126 MSI_ADDR_IR_INDEX1(ir_index) |
3127 MSI_ADDR_IR_INDEX2(ir_index);
3128 } else
3129#endif
3130 {
3131 msg->address_hi = MSI_ADDR_BASE_HI;
3132 msg->address_lo =
3133 MSI_ADDR_BASE_LO |
3134 ((INT_DEST_MODE == 0) ?
3135 MSI_ADDR_DEST_MODE_PHYSICAL:
3136 MSI_ADDR_DEST_MODE_LOGICAL) |
3137 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3138 MSI_ADDR_REDIRECTION_CPU:
3139 MSI_ADDR_REDIRECTION_LOWPRI) |
3140 MSI_ADDR_DEST_ID(dest);
3141
3142 msg->data =
3143 MSI_DATA_TRIGGER_EDGE |
3144 MSI_DATA_LEVEL_ASSERT |
3145 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3146 MSI_DATA_DELIVERY_FIXED:
3147 MSI_DATA_DELIVERY_LOWPRI) |
3148 MSI_DATA_VECTOR(cfg->vector);
3149 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003150 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003151}
3152
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003153#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303154static void set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003155{
Yinghai Lu3145e942008-12-05 18:58:34 -08003156 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003157 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003158 struct msi_msg msg;
3159 unsigned int dest;
3160 cpumask_t tmp;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003161
Rusty Russell0de26522008-12-13 21:20:26 +10303162 if (!cpumask_intersects(mask, cpu_online_mask))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003163 return;
3164
Yinghai Lu3145e942008-12-05 18:58:34 -08003165 cfg = desc->chip_data;
Mike Travise7986732008-12-16 17:33:52 -08003166 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003167 return;
3168
Mike Travise7986732008-12-16 17:33:52 -08003169 set_extra_move_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003170
Rusty Russell0de26522008-12-13 21:20:26 +10303171 cpumask_and(&tmp, &cfg->domain, mask);
Mike Travise7986732008-12-16 17:33:52 -08003172 dest = cpu_mask_to_apicid(&tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003173
Yinghai Lu3145e942008-12-05 18:58:34 -08003174 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003175
3176 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003177 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003178 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3179 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3180
Yinghai Lu3145e942008-12-05 18:58:34 -08003181 write_msi_msg_desc(desc, &msg);
Rusty Russell0de26522008-12-13 21:20:26 +10303182 cpumask_copy(&desc->affinity, mask);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003183}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003184#ifdef CONFIG_INTR_REMAP
3185/*
3186 * Migrate the MSI irq to another cpumask. This migration is
3187 * done in the process context using interrupt-remapping hardware.
3188 */
Mike Travise7986732008-12-16 17:33:52 -08003189static void
3190ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003191{
Yinghai Lu3145e942008-12-05 18:58:34 -08003192 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003193 struct irq_cfg *cfg;
3194 unsigned int dest;
3195 cpumask_t tmp, cleanup_mask;
3196 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003197
Rusty Russell0de26522008-12-13 21:20:26 +10303198 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003199 return;
3200
3201 if (get_irte(irq, &irte))
3202 return;
3203
Yinghai Lu3145e942008-12-05 18:58:34 -08003204 cfg = desc->chip_data;
Mike Travise7986732008-12-16 17:33:52 -08003205 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003206 return;
3207
Mike Travise7986732008-12-16 17:33:52 -08003208 set_extra_move_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003209
Rusty Russell0de26522008-12-13 21:20:26 +10303210 cpumask_and(&tmp, &cfg->domain, mask);
Mike Travise7986732008-12-16 17:33:52 -08003211 dest = cpu_mask_to_apicid(&tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003212
3213 irte.vector = cfg->vector;
3214 irte.dest_id = IRTE_DEST(dest);
3215
3216 /*
3217 * atomically update the IRTE with the new destination and vector.
3218 */
3219 modify_irte(irq, &irte);
3220
3221 /*
3222 * After this point, all the interrupts will start arriving
3223 * at the new destination. So, time to cleanup the previous
3224 * vector allocation.
3225 */
3226 if (cfg->move_in_progress) {
3227 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3228 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
Mike Travise7986732008-12-16 17:33:52 -08003229 send_IPI_mask(&cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003230 cfg->move_in_progress = 0;
3231 }
3232
Rusty Russell0de26522008-12-13 21:20:26 +10303233 cpumask_copy(&desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003234}
Yinghai Lu3145e942008-12-05 18:58:34 -08003235
Ingo Molnar54168ed2008-08-20 09:07:45 +02003236#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003237#endif /* CONFIG_SMP */
3238
3239/*
3240 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3241 * which implement the MSI or MSI-X Capability Structure.
3242 */
3243static struct irq_chip msi_chip = {
3244 .name = "PCI-MSI",
3245 .unmask = unmask_msi_irq,
3246 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003247 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003248#ifdef CONFIG_SMP
3249 .set_affinity = set_msi_irq_affinity,
3250#endif
3251 .retrigger = ioapic_retrigger_irq,
3252};
3253
Ingo Molnar54168ed2008-08-20 09:07:45 +02003254#ifdef CONFIG_INTR_REMAP
3255static struct irq_chip msi_ir_chip = {
3256 .name = "IR-PCI-MSI",
3257 .unmask = unmask_msi_irq,
3258 .mask = mask_msi_irq,
3259 .ack = ack_x2apic_edge,
3260#ifdef CONFIG_SMP
3261 .set_affinity = ir_set_msi_irq_affinity,
3262#endif
3263 .retrigger = ioapic_retrigger_irq,
3264};
3265
3266/*
3267 * Map the PCI dev to the corresponding remapping hardware unit
3268 * and allocate 'nvec' consecutive interrupt-remapping table entries
3269 * in it.
3270 */
3271static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3272{
3273 struct intel_iommu *iommu;
3274 int index;
3275
3276 iommu = map_dev_to_ir(dev);
3277 if (!iommu) {
3278 printk(KERN_ERR
3279 "Unable to map PCI %s to iommu\n", pci_name(dev));
3280 return -ENOENT;
3281 }
3282
3283 index = alloc_irte(iommu, irq, nvec);
3284 if (index < 0) {
3285 printk(KERN_ERR
3286 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003287 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003288 return -ENOSPC;
3289 }
3290 return index;
3291}
3292#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003293
Yinghai Lu3145e942008-12-05 18:58:34 -08003294static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003295{
3296 int ret;
3297 struct msi_msg msg;
3298
3299 ret = msi_compose_msg(dev, irq, &msg);
3300 if (ret < 0)
3301 return ret;
3302
Yinghai Lu3145e942008-12-05 18:58:34 -08003303 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003304 write_msi_msg(irq, &msg);
3305
Ingo Molnar54168ed2008-08-20 09:07:45 +02003306#ifdef CONFIG_INTR_REMAP
3307 if (irq_remapped(irq)) {
3308 struct irq_desc *desc = irq_to_desc(irq);
3309 /*
3310 * irq migration in process context
3311 */
3312 desc->status |= IRQ_MOVE_PCNTXT;
3313 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3314 } else
3315#endif
3316 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003317
Yinghai Luc81bba42008-09-25 11:53:11 -07003318 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3319
Yinghai Lu1d025192008-08-19 20:50:34 -07003320 return 0;
3321}
3322
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003323int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003324{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003325 unsigned int irq;
3326 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003327 unsigned int irq_want;
3328
Yinghai Lube5d5352008-12-05 18:58:33 -08003329 irq_want = nr_irqs_gsi;
Yinghai Lu199751d2008-08-19 20:50:27 -07003330 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003331 if (irq == 0)
3332 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003333
Ingo Molnar54168ed2008-08-20 09:07:45 +02003334#ifdef CONFIG_INTR_REMAP
3335 if (!intr_remapping_enabled)
3336 goto no_ir;
3337
3338 ret = msi_alloc_irte(dev, irq, 1);
3339 if (ret < 0)
3340 goto error;
3341no_ir:
3342#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003343 ret = setup_msi_irq(dev, msidesc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003344 if (ret < 0) {
3345 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003346 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003347 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003348 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003349
3350#ifdef CONFIG_INTR_REMAP
3351error:
3352 destroy_irq(irq);
3353 return ret;
3354#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003355}
3356
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003357int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3358{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003359 unsigned int irq;
3360 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003361 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003362 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003363
Ingo Molnar54168ed2008-08-20 09:07:45 +02003364#ifdef CONFIG_INTR_REMAP
3365 struct intel_iommu *iommu = 0;
3366 int index = 0;
3367#endif
3368
Yinghai Lube5d5352008-12-05 18:58:33 -08003369 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003370 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003371 list_for_each_entry(msidesc, &dev->msi_list, list) {
3372 irq = create_irq_nr(irq_want);
Yinghai Lube5d5352008-12-05 18:58:33 -08003373 irq_want++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003374 if (irq == 0)
3375 return -1;
3376#ifdef CONFIG_INTR_REMAP
3377 if (!intr_remapping_enabled)
3378 goto no_ir;
3379
3380 if (!sub_handle) {
3381 /*
3382 * allocate the consecutive block of IRTE's
3383 * for 'nvec'
3384 */
3385 index = msi_alloc_irte(dev, irq, nvec);
3386 if (index < 0) {
3387 ret = index;
3388 goto error;
3389 }
3390 } else {
3391 iommu = map_dev_to_ir(dev);
3392 if (!iommu) {
3393 ret = -ENOENT;
3394 goto error;
3395 }
3396 /*
3397 * setup the mapping between the irq and the IRTE
3398 * base index, the sub_handle pointing to the
3399 * appropriate interrupt remap table entry.
3400 */
3401 set_irte_irq(irq, iommu, index, sub_handle);
3402 }
3403no_ir:
3404#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003405 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003406 if (ret < 0)
3407 goto error;
3408 sub_handle++;
3409 }
3410 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003411
3412error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003413 destroy_irq(irq);
3414 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003415}
3416
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003417void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003418{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003419 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003420}
3421
Ingo Molnar54168ed2008-08-20 09:07:45 +02003422#ifdef CONFIG_DMAR
3423#ifdef CONFIG_SMP
Mike Travise7986732008-12-16 17:33:52 -08003424static void dmar_msi_set_affinity(unsigned int irq, const cpumask_t *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003425{
Yinghai Lu3145e942008-12-05 18:58:34 -08003426 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003427 struct irq_cfg *cfg;
3428 struct msi_msg msg;
3429 unsigned int dest;
3430 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003431
Rusty Russell0de26522008-12-13 21:20:26 +10303432 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003433 return;
3434
Yinghai Lu3145e942008-12-05 18:58:34 -08003435 cfg = desc->chip_data;
Mike Travise7986732008-12-16 17:33:52 -08003436 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003437 return;
3438
Mike Travise7986732008-12-16 17:33:52 -08003439 set_extra_move_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003440
Rusty Russell0de26522008-12-13 21:20:26 +10303441 cpumask_and(&tmp, &cfg->domain, mask);
Mike Travise7986732008-12-16 17:33:52 -08003442 dest = cpu_mask_to_apicid(&tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003443
3444 dmar_msi_read(irq, &msg);
3445
3446 msg.data &= ~MSI_DATA_VECTOR_MASK;
3447 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3448 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3449 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3450
3451 dmar_msi_write(irq, &msg);
Rusty Russell0de26522008-12-13 21:20:26 +10303452 cpumask_copy(&desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003453}
Yinghai Lu3145e942008-12-05 18:58:34 -08003454
Ingo Molnar54168ed2008-08-20 09:07:45 +02003455#endif /* CONFIG_SMP */
3456
3457struct irq_chip dmar_msi_type = {
3458 .name = "DMAR_MSI",
3459 .unmask = dmar_msi_unmask,
3460 .mask = dmar_msi_mask,
3461 .ack = ack_apic_edge,
3462#ifdef CONFIG_SMP
3463 .set_affinity = dmar_msi_set_affinity,
3464#endif
3465 .retrigger = ioapic_retrigger_irq,
3466};
3467
3468int arch_setup_dmar_msi(unsigned int irq)
3469{
3470 int ret;
3471 struct msi_msg msg;
3472
3473 ret = msi_compose_msg(NULL, irq, &msg);
3474 if (ret < 0)
3475 return ret;
3476 dmar_msi_write(irq, &msg);
3477 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3478 "edge");
3479 return 0;
3480}
3481#endif
3482
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003483#ifdef CONFIG_HPET_TIMER
3484
3485#ifdef CONFIG_SMP
Mike Travise7986732008-12-16 17:33:52 -08003486static void hpet_msi_set_affinity(unsigned int irq, const cpumask_t *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003487{
Yinghai Lu3145e942008-12-05 18:58:34 -08003488 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003489 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003490 struct msi_msg msg;
3491 unsigned int dest;
3492 cpumask_t tmp;
3493
Rusty Russell0de26522008-12-13 21:20:26 +10303494 if (!cpumask_intersects(mask, cpu_online_mask))
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003495 return;
3496
Yinghai Lu3145e942008-12-05 18:58:34 -08003497 cfg = desc->chip_data;
Mike Travise7986732008-12-16 17:33:52 -08003498 if (assign_irq_vector(irq, cfg, mask))
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003499 return;
3500
Mike Travise7986732008-12-16 17:33:52 -08003501 set_extra_move_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003502
Rusty Russell0de26522008-12-13 21:20:26 +10303503 cpumask_and(&tmp, &cfg->domain, mask);
Mike Travise7986732008-12-16 17:33:52 -08003504 dest = cpu_mask_to_apicid(&tmp);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003505
3506 hpet_msi_read(irq, &msg);
3507
3508 msg.data &= ~MSI_DATA_VECTOR_MASK;
3509 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3510 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3511 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3512
3513 hpet_msi_write(irq, &msg);
Rusty Russell0de26522008-12-13 21:20:26 +10303514 cpumask_copy(&desc->affinity, mask);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003515}
Yinghai Lu3145e942008-12-05 18:58:34 -08003516
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003517#endif /* CONFIG_SMP */
3518
3519struct irq_chip hpet_msi_type = {
3520 .name = "HPET_MSI",
3521 .unmask = hpet_msi_unmask,
3522 .mask = hpet_msi_mask,
3523 .ack = ack_apic_edge,
3524#ifdef CONFIG_SMP
3525 .set_affinity = hpet_msi_set_affinity,
3526#endif
3527 .retrigger = ioapic_retrigger_irq,
3528};
3529
3530int arch_setup_hpet_msi(unsigned int irq)
3531{
3532 int ret;
3533 struct msi_msg msg;
3534
3535 ret = msi_compose_msg(NULL, irq, &msg);
3536 if (ret < 0)
3537 return ret;
3538
3539 hpet_msi_write(irq, &msg);
3540 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3541 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003542
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003543 return 0;
3544}
3545#endif
3546
Ingo Molnar54168ed2008-08-20 09:07:45 +02003547#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003548/*
3549 * Hypertransport interrupt support
3550 */
3551#ifdef CONFIG_HT_IRQ
3552
3553#ifdef CONFIG_SMP
3554
Yinghai Lu497c9a12008-08-19 20:50:28 -07003555static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003556{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003557 struct ht_irq_msg msg;
3558 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003559
Yinghai Lu497c9a12008-08-19 20:50:28 -07003560 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003561 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003562
Yinghai Lu497c9a12008-08-19 20:50:28 -07003563 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003564 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003565
Eric W. Biedermanec683072006-11-08 17:44:57 -08003566 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003567}
3568
Mike Travise7986732008-12-16 17:33:52 -08003569static void set_ht_irq_affinity(unsigned int irq, const cpumask_t *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003570{
Yinghai Lu3145e942008-12-05 18:58:34 -08003571 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003572 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003573 unsigned int dest;
3574 cpumask_t tmp;
3575
Rusty Russell0de26522008-12-13 21:20:26 +10303576 if (!cpumask_intersects(mask, cpu_online_mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003577 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003578
Yinghai Lu3145e942008-12-05 18:58:34 -08003579 cfg = desc->chip_data;
Mike Travise7986732008-12-16 17:33:52 -08003580 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003581 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003582
Mike Travise7986732008-12-16 17:33:52 -08003583 set_extra_move_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08003584
Rusty Russell0de26522008-12-13 21:20:26 +10303585 cpumask_and(&tmp, &cfg->domain, mask);
Mike Travise7986732008-12-16 17:33:52 -08003586 dest = cpu_mask_to_apicid(&tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003587
Yinghai Lu497c9a12008-08-19 20:50:28 -07003588 target_ht_irq(irq, dest, cfg->vector);
Rusty Russell0de26522008-12-13 21:20:26 +10303589 cpumask_copy(&desc->affinity, mask);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003590}
Yinghai Lu3145e942008-12-05 18:58:34 -08003591
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003592#endif
3593
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003594static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003595 .name = "PCI-HT",
3596 .mask = mask_ht_irq,
3597 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003598 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003599#ifdef CONFIG_SMP
3600 .set_affinity = set_ht_irq_affinity,
3601#endif
3602 .retrigger = ioapic_retrigger_irq,
3603};
3604
3605int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3606{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003607 struct irq_cfg *cfg;
3608 int err;
3609 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003610
Yinghai Lu3145e942008-12-05 18:58:34 -08003611 cfg = irq_cfg(irq);
Mike Travise7986732008-12-16 17:33:52 -08003612 err = assign_irq_vector(irq, cfg, TARGET_CPUS);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003613 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003614 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003615 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003616
Yinghai Lu497c9a12008-08-19 20:50:28 -07003617 cpus_and(tmp, cfg->domain, tmp);
Mike Travise7986732008-12-16 17:33:52 -08003618 dest = cpu_mask_to_apicid(&tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003619
Eric W. Biedermanec683072006-11-08 17:44:57 -08003620 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003621
Eric W. Biedermanec683072006-11-08 17:44:57 -08003622 msg.address_lo =
3623 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003624 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003625 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003626 ((INT_DEST_MODE == 0) ?
3627 HT_IRQ_LOW_DM_PHYSICAL :
3628 HT_IRQ_LOW_DM_LOGICAL) |
3629 HT_IRQ_LOW_RQEOI_EDGE |
3630 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3631 HT_IRQ_LOW_MT_FIXED :
3632 HT_IRQ_LOW_MT_ARBITRATED) |
3633 HT_IRQ_LOW_IRQ_MASKED;
3634
Eric W. Biedermanec683072006-11-08 17:44:57 -08003635 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003636
Ingo Molnara460e742006-10-17 00:10:03 -07003637 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3638 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003639
3640 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003641 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003642 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003643}
3644#endif /* CONFIG_HT_IRQ */
3645
Dean Nelson4173a0e2008-10-02 12:18:21 -05003646#ifdef CONFIG_X86_64
3647/*
3648 * Re-target the irq to the specified CPU and enable the specified MMR located
3649 * on the specified blade to allow the sending of MSIs to the specified CPU.
3650 */
3651int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3652 unsigned long mmr_offset)
3653{
Mike Travise7986732008-12-16 17:33:52 -08003654 const cpumask_t *eligible_cpu = &cpumask_of_cpu(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003655 struct irq_cfg *cfg;
3656 int mmr_pnode;
3657 unsigned long mmr_value;
3658 struct uv_IO_APIC_route_entry *entry;
3659 unsigned long flags;
3660 int err;
3661
Yinghai Lu3145e942008-12-05 18:58:34 -08003662 cfg = irq_cfg(irq);
3663
Mike Travise7986732008-12-16 17:33:52 -08003664 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003665 if (err != 0)
3666 return err;
3667
3668 spin_lock_irqsave(&vector_lock, flags);
3669 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3670 irq_name);
3671 spin_unlock_irqrestore(&vector_lock, flags);
3672
Dean Nelson4173a0e2008-10-02 12:18:21 -05003673 mmr_value = 0;
3674 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3675 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3676
3677 entry->vector = cfg->vector;
3678 entry->delivery_mode = INT_DELIVERY_MODE;
3679 entry->dest_mode = INT_DEST_MODE;
3680 entry->polarity = 0;
3681 entry->trigger = 0;
3682 entry->mask = 0;
Mike Travise7986732008-12-16 17:33:52 -08003683 entry->dest = cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003684
3685 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3686 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3687
3688 return irq;
3689}
3690
3691/*
3692 * Disable the specified MMR located on the specified blade so that MSIs are
3693 * longer allowed to be sent.
3694 */
3695void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3696{
3697 unsigned long mmr_value;
3698 struct uv_IO_APIC_route_entry *entry;
3699 int mmr_pnode;
3700
3701 mmr_value = 0;
3702 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3703 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3704
3705 entry->mask = 1;
3706
3707 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3708 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3709}
3710#endif /* CONFIG_X86_64 */
3711
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003712int __init io_apic_get_redir_entries (int ioapic)
3713{
3714 union IO_APIC_reg_01 reg_01;
3715 unsigned long flags;
3716
3717 spin_lock_irqsave(&ioapic_lock, flags);
3718 reg_01.raw = io_apic_read(ioapic, 1);
3719 spin_unlock_irqrestore(&ioapic_lock, flags);
3720
3721 return reg_01.bits.entries;
3722}
3723
Yinghai Lube5d5352008-12-05 18:58:33 -08003724void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003725{
Yinghai Lube5d5352008-12-05 18:58:33 -08003726 int idx;
3727 int nr = 0;
3728
3729 for (idx = 0; idx < nr_ioapics; idx++)
3730 nr += io_apic_get_redir_entries(idx) + 1;
3731
3732 if (nr > nr_irqs_gsi)
3733 nr_irqs_gsi = nr;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003734}
3735
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003737 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 -------------------------------------------------------------------------- */
3739
Len Brown888ba6c2005-08-24 12:07:20 -04003740#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741
Ingo Molnar54168ed2008-08-20 09:07:45 +02003742#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003743int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
3745 union IO_APIC_reg_00 reg_00;
3746 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3747 physid_mask_t tmp;
3748 unsigned long flags;
3749 int i = 0;
3750
3751 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003752 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3753 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003755 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3757 * advantage of new APIC bus architecture.
3758 */
3759
3760 if (physids_empty(apic_id_map))
3761 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3762
3763 spin_lock_irqsave(&ioapic_lock, flags);
3764 reg_00.raw = io_apic_read(ioapic, 0);
3765 spin_unlock_irqrestore(&ioapic_lock, flags);
3766
3767 if (apic_id >= get_physical_broadcast()) {
3768 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3769 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3770 apic_id = reg_00.bits.ID;
3771 }
3772
3773 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003774 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 * 'stuck on smp_invalidate_needed IPI wait' messages.
3776 */
3777 if (check_apicid_used(apic_id_map, apic_id)) {
3778
3779 for (i = 0; i < get_physical_broadcast(); i++) {
3780 if (!check_apicid_used(apic_id_map, i))
3781 break;
3782 }
3783
3784 if (i == get_physical_broadcast())
3785 panic("Max apic_id exceeded!\n");
3786
3787 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3788 "trying %d\n", ioapic, apic_id, i);
3789
3790 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
3793 tmp = apicid_to_cpu_present(apic_id);
3794 physids_or(apic_id_map, apic_id_map, tmp);
3795
3796 if (reg_00.bits.ID != apic_id) {
3797 reg_00.bits.ID = apic_id;
3798
3799 spin_lock_irqsave(&ioapic_lock, flags);
3800 io_apic_write(ioapic, 0, reg_00.raw);
3801 reg_00.raw = io_apic_read(ioapic, 0);
3802 spin_unlock_irqrestore(&ioapic_lock, flags);
3803
3804 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003805 if (reg_00.bits.ID != apic_id) {
3806 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3807 return -1;
3808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 }
3810
3811 apic_printk(APIC_VERBOSE, KERN_INFO
3812 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3813
3814 return apic_id;
3815}
3816
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003817int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818{
3819 union IO_APIC_reg_01 reg_01;
3820 unsigned long flags;
3821
3822 spin_lock_irqsave(&ioapic_lock, flags);
3823 reg_01.raw = io_apic_read(ioapic, 1);
3824 spin_unlock_irqrestore(&ioapic_lock, flags);
3825
3826 return reg_01.bits.version;
3827}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003828#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
Ingo Molnar54168ed2008-08-20 09:07:45 +02003830int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003832 struct irq_desc *desc;
3833 struct irq_cfg *cfg;
3834 int cpu = boot_cpu_id;
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003837 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 ioapic);
3839 return -EINVAL;
3840 }
3841
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003842 desc = irq_to_desc_alloc_cpu(irq, cpu);
3843 if (!desc) {
3844 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3845 return 0;
3846 }
3847
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 * IRQs < 16 are already in the irq_2_pin[] map
3850 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003851 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003852 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003853 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Yinghai Lu3145e942008-12-05 18:58:34 -08003856 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
3858 return 0;
3859}
3860
Ingo Molnar54168ed2008-08-20 09:07:45 +02003861
Shaohua Li61fd47e2007-11-17 01:05:28 -05003862int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3863{
3864 int i;
3865
3866 if (skip_ioapic_setup)
3867 return -1;
3868
3869 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003870 if (mp_irqs[i].mp_irqtype == mp_INT &&
3871 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003872 break;
3873 if (i >= mp_irq_entries)
3874 return -1;
3875
3876 *trigger = irq_trigger(i);
3877 *polarity = irq_polarity(i);
3878 return 0;
3879}
3880
Len Brown888ba6c2005-08-24 12:07:20 -04003881#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003882
Yinghai Lu497c9a12008-08-19 20:50:28 -07003883/*
3884 * This function currently is only a helper for the i386 smp boot process where
3885 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3886 * so mask in all cases should simply be TARGET_CPUS
3887 */
3888#ifdef CONFIG_SMP
3889void __init setup_ioapic_dest(void)
3890{
3891 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003892 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003893 struct irq_cfg *cfg;
Mike Travise7986732008-12-16 17:33:52 -08003894 const cpumask_t *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003895
3896 if (skip_ioapic_setup == 1)
3897 return;
3898
3899 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3900 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3901 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3902 if (irq_entry == -1)
3903 continue;
3904 irq = pin_2_irq(irq_entry, ioapic, pin);
3905
3906 /* setup_IO_APIC_irqs could fail to get vector for some device
3907 * when you have too many devices, because at that time only boot
3908 * cpu is online.
3909 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003910 desc = irq_to_desc(irq);
3911 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003912 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08003913 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07003914 irq_trigger(irq_entry),
3915 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003916 continue;
3917
3918 }
3919
3920 /*
3921 * Honour affinities which have been set in early boot
3922 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003923 if (desc->status &
3924 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
Mike Travise7986732008-12-16 17:33:52 -08003925 mask = &desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003926 else
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003927 mask = TARGET_CPUS;
3928
3929#ifdef CONFIG_INTR_REMAP
3930 if (intr_remapping_enabled)
Mike Travise7986732008-12-16 17:33:52 -08003931 set_ir_ioapic_affinity_irq_desc(desc, mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003932 else
3933#endif
Mike Travise7986732008-12-16 17:33:52 -08003934 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003935 }
3936
3937 }
3938}
3939#endif
3940
Ingo Molnar54168ed2008-08-20 09:07:45 +02003941#define IOAPIC_RESOURCE_NAME_SIZE 11
3942
3943static struct resource *ioapic_resources;
3944
3945static struct resource * __init ioapic_setup_resources(void)
3946{
3947 unsigned long n;
3948 struct resource *res;
3949 char *mem;
3950 int i;
3951
3952 if (nr_ioapics <= 0)
3953 return NULL;
3954
3955 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3956 n *= nr_ioapics;
3957
3958 mem = alloc_bootmem(n);
3959 res = (void *)mem;
3960
3961 if (mem != NULL) {
3962 mem += sizeof(struct resource) * nr_ioapics;
3963
3964 for (i = 0; i < nr_ioapics; i++) {
3965 res[i].name = mem;
3966 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3967 sprintf(mem, "IOAPIC %u", i);
3968 mem += IOAPIC_RESOURCE_NAME_SIZE;
3969 }
3970 }
3971
3972 ioapic_resources = res;
3973
3974 return res;
3975}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003976
Yinghai Luf3294a32008-06-27 01:41:56 -07003977void __init ioapic_init_mappings(void)
3978{
3979 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003980 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003981 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07003982
Ingo Molnar54168ed2008-08-20 09:07:45 +02003983 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003984 for (i = 0; i < nr_ioapics; i++) {
3985 if (smp_found_config) {
3986 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003987#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003988 if (!ioapic_phys) {
3989 printk(KERN_ERR
3990 "WARNING: bogus zero IO-APIC "
3991 "address found in MPTABLE, "
3992 "disabling IO/APIC support!\n");
3993 smp_found_config = 0;
3994 skip_ioapic_setup = 1;
3995 goto fake_ioapic_page;
3996 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02003997#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003998 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003999#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004000fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004001#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004002 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004003 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004004 ioapic_phys = __pa(ioapic_phys);
4005 }
4006 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004007 apic_printk(APIC_VERBOSE,
4008 "mapped IOAPIC to %08lx (%08lx)\n",
4009 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004010 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004011
Ingo Molnar54168ed2008-08-20 09:07:45 +02004012 if (ioapic_res != NULL) {
4013 ioapic_res->start = ioapic_phys;
4014 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4015 ioapic_res++;
4016 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004017 }
4018}
4019
Ingo Molnar54168ed2008-08-20 09:07:45 +02004020static int __init ioapic_insert_resources(void)
4021{
4022 int i;
4023 struct resource *r = ioapic_resources;
4024
4025 if (!r) {
4026 printk(KERN_ERR
4027 "IO APIC resources could be not be allocated.\n");
4028 return -1;
4029 }
4030
4031 for (i = 0; i < nr_ioapics; i++) {
4032 insert_resource(&iomem_resource, r);
4033 r++;
4034 }
4035
4036 return 0;
4037}
4038
4039/* Insert the IO APIC resources after PCI initialization has occured to handle
4040 * IO APICS that are mapped in on a BAR in PCI space. */
4041late_initcall(ioapic_insert_resources);