blob: 60bb8b19f4cdde5799c8222e5e8682db04fabbd7 [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;
Mike Travis22f65d32008-12-16 17:33:56 -0800139 cpumask_var_t domain;
140 cpumask_var_t old_domain;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800141 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
Mike Travis22f65d32008-12-16 17:33:56 -0800152 [0] = { .vector = IRQ0_VECTOR, },
153 [1] = { .vector = IRQ1_VECTOR, },
154 [2] = { .vector = IRQ2_VECTOR, },
155 [3] = { .vector = IRQ3_VECTOR, },
156 [4] = { .vector = IRQ4_VECTOR, },
157 [5] = { .vector = IRQ5_VECTOR, },
158 [6] = { .vector = IRQ6_VECTOR, },
159 [7] = { .vector = IRQ7_VECTOR, },
160 [8] = { .vector = IRQ8_VECTOR, },
161 [9] = { .vector = IRQ9_VECTOR, },
162 [10] = { .vector = IRQ10_VECTOR, },
163 [11] = { .vector = IRQ11_VECTOR, },
164 [12] = { .vector = IRQ12_VECTOR, },
165 [13] = { .vector = IRQ13_VECTOR, },
166 [14] = { .vector = IRQ14_VECTOR, },
167 [15] = { .vector = IRQ15_VECTOR, },
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800168};
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];
Mike Travis22f65d32008-12-16 17:33:56 -0800183 alloc_bootmem_cpumask_var(&cfg[i].domain);
184 alloc_bootmem_cpumask_var(&cfg[i].old_domain);
185 if (i < NR_IRQS_LEGACY)
186 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800187 }
188}
189
190#ifdef CONFIG_SPARSE_IRQ
191static struct irq_cfg *irq_cfg(unsigned int irq)
192{
193 struct irq_cfg *cfg = NULL;
194 struct irq_desc *desc;
195
196 desc = irq_to_desc(irq);
197 if (desc)
198 cfg = desc->chip_data;
199
200 return cfg;
201}
202
203static struct irq_cfg *get_one_free_irq_cfg(int cpu)
204{
205 struct irq_cfg *cfg;
206 int node;
207
208 node = cpu_to_node(cpu);
209
210 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800211 if (cfg) {
212 /* FIXME: needs alloc_cpumask_var_node() */
213 if (!alloc_cpumask_var(&cfg->domain, GFP_ATOMIC)) {
214 kfree(cfg);
215 cfg = NULL;
216 } else if (!alloc_cpumask_var(&cfg->old_domain, GFP_ATOMIC)) {
217 free_cpumask_var(cfg->domain);
218 kfree(cfg);
219 cfg = NULL;
220 } else {
221 cpumask_clear(cfg->domain);
222 cpumask_clear(cfg->old_domain);
223 }
224 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800225 printk(KERN_DEBUG " alloc irq_cfg on cpu %d node %d\n", cpu, node);
226
227 return cfg;
228}
229
230void arch_init_chip_data(struct irq_desc *desc, int cpu)
231{
232 struct irq_cfg *cfg;
233
234 cfg = desc->chip_data;
235 if (!cfg) {
236 desc->chip_data = get_one_free_irq_cfg(cpu);
237 if (!desc->chip_data) {
238 printk(KERN_ERR "can not alloc irq_cfg\n");
239 BUG_ON(1);
240 }
241 }
242}
243
244#else
245static struct irq_cfg *irq_cfg(unsigned int irq)
246{
247 return irq < nr_irqs ? irq_cfgx + irq : NULL;
248}
249
250#endif
251
Mike Travise7986732008-12-16 17:33:52 -0800252static inline void
253set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800254{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700255}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Linus Torvalds130fe052006-11-01 09:11:00 -0800257struct io_apic {
258 unsigned int index;
259 unsigned int unused[3];
260 unsigned int data;
261};
262
263static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
264{
265 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400266 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800267}
268
269static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
270{
271 struct io_apic __iomem *io_apic = io_apic_base(apic);
272 writel(reg, &io_apic->index);
273 return readl(&io_apic->data);
274}
275
276static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
277{
278 struct io_apic __iomem *io_apic = io_apic_base(apic);
279 writel(reg, &io_apic->index);
280 writel(value, &io_apic->data);
281}
282
283/*
284 * Re-write a value: to be used for read-modify-write
285 * cycles where the read already set up the index register.
286 *
287 * Older SiS APIC requires we rewrite the index register
288 */
289static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
290{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200291 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200292
293 if (sis_apic_bug)
294 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800295 writel(value, &io_apic->data);
296}
297
Yinghai Lu3145e942008-12-05 18:58:34 -0800298static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700299{
300 struct irq_pin_list *entry;
301 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700302
303 spin_lock_irqsave(&ioapic_lock, flags);
304 entry = cfg->irq_2_pin;
305 for (;;) {
306 unsigned int reg;
307 int pin;
308
309 if (!entry)
310 break;
311 pin = entry->pin;
312 reg = io_apic_read(entry->apic, 0x10 + pin*2);
313 /* Is the remote IRR bit set? */
314 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
315 spin_unlock_irqrestore(&ioapic_lock, flags);
316 return true;
317 }
318 if (!entry->next)
319 break;
320 entry = entry->next;
321 }
322 spin_unlock_irqrestore(&ioapic_lock, flags);
323
324 return false;
325}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700326
Andi Kleencf4c6a22006-09-26 10:52:30 +0200327union entry_union {
328 struct { u32 w1, w2; };
329 struct IO_APIC_route_entry entry;
330};
331
332static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
333{
334 union entry_union eu;
335 unsigned long flags;
336 spin_lock_irqsave(&ioapic_lock, flags);
337 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
338 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
339 spin_unlock_irqrestore(&ioapic_lock, flags);
340 return eu.entry;
341}
342
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800343/*
344 * When we write a new IO APIC routing entry, we need to write the high
345 * word first! If the mask bit in the low word is clear, we will enable
346 * the interrupt, and we need to make sure the entry is fully populated
347 * before that happens.
348 */
Andi Kleend15512f2006-12-07 02:14:07 +0100349static void
350__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
351{
352 union entry_union eu;
353 eu.entry = e;
354 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
355 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
356}
357
Andi Kleencf4c6a22006-09-26 10:52:30 +0200358static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
359{
360 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200361 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100362 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800363 spin_unlock_irqrestore(&ioapic_lock, flags);
364}
365
366/*
367 * When we mask an IO APIC routing entry, we need to write the low
368 * word first, in order to set the mask bit before we change the
369 * high bits!
370 */
371static void ioapic_mask_entry(int apic, int pin)
372{
373 unsigned long flags;
374 union entry_union eu = { .entry.mask = 1 };
375
376 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200377 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
378 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
379 spin_unlock_irqrestore(&ioapic_lock, flags);
380}
381
Yinghai Lu497c9a12008-08-19 20:50:28 -0700382#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -0800383static void send_cleanup_vector(struct irq_cfg *cfg)
384{
385 cpumask_var_t cleanup_mask;
386
387 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
388 unsigned int i;
389 cfg->move_cleanup_count = 0;
390 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
391 cfg->move_cleanup_count++;
392 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
393 send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
394 } else {
395 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
396 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
397 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
398 free_cpumask_var(cleanup_mask);
399 }
400 cfg->move_in_progress = 0;
401}
402
Yinghai Lu3145e942008-12-05 18:58:34 -0800403static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700404{
405 int apic, pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700406 struct irq_pin_list *entry;
Yinghai Lu3145e942008-12-05 18:58:34 -0800407 u8 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700408
Yinghai Lu497c9a12008-08-19 20:50:28 -0700409 entry = cfg->irq_2_pin;
410 for (;;) {
411 unsigned int reg;
412
413 if (!entry)
414 break;
415
416 apic = entry->apic;
417 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200418#ifdef CONFIG_INTR_REMAP
419 /*
420 * With interrupt-remapping, destination information comes
421 * from interrupt-remapping table entry.
422 */
423 if (!irq_remapped(irq))
424 io_apic_write(apic, 0x11 + pin*2, dest);
425#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700426 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200427#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700428 reg = io_apic_read(apic, 0x10 + pin*2);
429 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
430 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200431 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700432 if (!entry->next)
433 break;
434 entry = entry->next;
435 }
436}
Yinghai Luefa25592008-08-19 20:50:36 -0700437
Mike Travise7986732008-12-16 17:33:52 -0800438static int
439assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
Yinghai Luefa25592008-08-19 20:50:36 -0700440
Mike Travis22f65d32008-12-16 17:33:56 -0800441/*
442 * Either sets desc->affinity to a valid value, and returns cpu_mask_to_apicid
443 * of that, or returns BAD_APICID and leaves desc->affinity untouched.
444 */
445static unsigned int
446set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700447{
448 struct irq_cfg *cfg;
Yinghai Lu3145e942008-12-05 18:58:34 -0800449 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700450
Rusty Russell0de26522008-12-13 21:20:26 +1030451 if (!cpumask_intersects(mask, cpu_online_mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800452 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700453
Yinghai Lu3145e942008-12-05 18:58:34 -0800454 irq = desc->irq;
455 cfg = desc->chip_data;
Mike Travise7986732008-12-16 17:33:52 -0800456 if (assign_irq_vector(irq, cfg, mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800457 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700458
Mike Travis22f65d32008-12-16 17:33:56 -0800459 cpumask_and(&desc->affinity, cfg->domain, mask);
Mike Travise7986732008-12-16 17:33:52 -0800460 set_extra_move_desc(desc, mask);
Mike Travis22f65d32008-12-16 17:33:56 -0800461 return cpu_mask_to_apicid_and(&desc->affinity, cpu_online_mask);
462}
Yinghai Lu3145e942008-12-05 18:58:34 -0800463
Mike Travis22f65d32008-12-16 17:33:56 -0800464static void
465set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
466{
467 struct irq_cfg *cfg;
468 unsigned long flags;
469 unsigned int dest;
470 unsigned int irq;
471
472 irq = desc->irq;
473 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700474
475 spin_lock_irqsave(&ioapic_lock, flags);
Mike Travis22f65d32008-12-16 17:33:56 -0800476 dest = set_desc_affinity(desc, mask);
477 if (dest != BAD_APICID) {
478 /* Only the high 8 bits are valid. */
479 dest = SET_APIC_LOGICAL_ID(dest);
480 __target_IO_APIC_irq(irq, dest, cfg);
481 }
Yinghai Lu497c9a12008-08-19 20:50:28 -0700482 spin_unlock_irqrestore(&ioapic_lock, flags);
483}
Yinghai Lu3145e942008-12-05 18:58:34 -0800484
Mike Travis22f65d32008-12-16 17:33:56 -0800485static void
486set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800487{
488 struct irq_desc *desc;
489
490 desc = irq_to_desc(irq);
491
492 set_ioapic_affinity_irq_desc(desc, mask);
493}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700494#endif /* CONFIG_SMP */
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
497 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
498 * shared ISA-space IRQs, so we have to support them. We are super
499 * fast in the common case, and fast for shared ISA-space IRQs.
500 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800501static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700503 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Yinghai Lu0f978f42008-08-19 20:50:26 -0700505 entry = cfg->irq_2_pin;
506 if (!entry) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800507 entry = get_one_free_irq_2_pin(cpu);
508 if (!entry) {
509 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
510 apic, pin);
511 return;
512 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700513 cfg->irq_2_pin = entry;
514 entry->apic = apic;
515 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700516 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700518
519 while (entry->next) {
520 /* not again, please */
521 if (entry->apic == apic && entry->pin == pin)
522 return;
523
524 entry = entry->next;
525 }
526
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800527 entry->next = get_one_free_irq_2_pin(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700528 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 entry->apic = apic;
530 entry->pin = pin;
531}
532
533/*
534 * Reroute an IRQ to a different pin.
535 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800536static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 int oldapic, int oldpin,
538 int newapic, int newpin)
539{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700540 struct irq_pin_list *entry = cfg->irq_2_pin;
541 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Yinghai Lu0f978f42008-08-19 20:50:26 -0700543 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (entry->apic == oldapic && entry->pin == oldpin) {
545 entry->apic = newapic;
546 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700547 replaced = 1;
548 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700550 }
551 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700553
554 /* why? call replace before add? */
555 if (!replaced)
Yinghai Lu3145e942008-12-05 18:58:34 -0800556 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Yinghai Lu3145e942008-12-05 18:58:34 -0800559static inline void io_apic_modify_irq(struct irq_cfg *cfg,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400560 int mask_and, int mask_or,
561 void (*final)(struct irq_pin_list *entry))
562{
563 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400564 struct irq_pin_list *entry;
565
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400566 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
567 unsigned int reg;
568 pin = entry->pin;
569 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
570 reg &= mask_and;
571 reg |= mask_or;
572 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
573 if (final)
574 final(entry);
575 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700576}
577
Yinghai Lu3145e942008-12-05 18:58:34 -0800578static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400579{
Yinghai Lu3145e942008-12-05 18:58:34 -0800580 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400581}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700582
583#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400584void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700585{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400586 /*
587 * Synchronize the IO-APIC and the CPU by doing
588 * a dummy read from the IO-APIC
589 */
590 struct io_apic __iomem *io_apic;
591 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700592 readl(&io_apic->data);
593}
594
Yinghai Lu3145e942008-12-05 18:58:34 -0800595static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400596{
Yinghai Lu3145e942008-12-05 18:58:34 -0800597 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400598}
599#else /* CONFIG_X86_32 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800600static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400601{
Yinghai Lu3145e942008-12-05 18:58:34 -0800602 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400603}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700604
Yinghai Lu3145e942008-12-05 18:58:34 -0800605static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400606{
Yinghai Lu3145e942008-12-05 18:58:34 -0800607 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400608 IO_APIC_REDIR_MASKED, NULL);
609}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700610
Yinghai Lu3145e942008-12-05 18:58:34 -0800611static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400612{
Yinghai Lu3145e942008-12-05 18:58:34 -0800613 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400614 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
615}
616#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700617
Yinghai Lu3145e942008-12-05 18:58:34 -0800618static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Yinghai Lu3145e942008-12-05 18:58:34 -0800620 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned long flags;
622
Yinghai Lu3145e942008-12-05 18:58:34 -0800623 BUG_ON(!cfg);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800626 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 spin_unlock_irqrestore(&ioapic_lock, flags);
628}
629
Yinghai Lu3145e942008-12-05 18:58:34 -0800630static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Yinghai Lu3145e942008-12-05 18:58:34 -0800632 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 unsigned long flags;
634
635 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800636 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 spin_unlock_irqrestore(&ioapic_lock, flags);
638}
639
Yinghai Lu3145e942008-12-05 18:58:34 -0800640static void mask_IO_APIC_irq(unsigned int irq)
641{
642 struct irq_desc *desc = irq_to_desc(irq);
643
644 mask_IO_APIC_irq_desc(desc);
645}
646static void unmask_IO_APIC_irq(unsigned int irq)
647{
648 struct irq_desc *desc = irq_to_desc(irq);
649
650 unmask_IO_APIC_irq_desc(desc);
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
654{
655 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200658 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (entry.delivery_mode == dest_SMI)
660 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 /*
662 * Disable it in the IO-APIC irq-routing table:
663 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800664 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Ingo Molnar54168ed2008-08-20 09:07:45 +0200667static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 int apic, pin;
670
671 for (apic = 0; apic < nr_ioapics; apic++)
672 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
673 clear_IO_APIC_pin(apic, pin);
674}
675
Ingo Molnar54168ed2008-08-20 09:07:45 +0200676#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100677void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 unsigned int cfg;
680
681 /*
682 * Wait for idle.
683 */
684 apic_wait_icr_idle();
685 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
686 /*
687 * Send the IPI. The write to APIC_ICR fires this off.
688 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100689 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200691#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Ingo Molnar54168ed2008-08-20 09:07:45 +0200693#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*
695 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
696 * specific CPU-side IRQs.
697 */
698
699#define MAX_PIRQS 8
700static int pirq_entries [MAX_PIRQS];
701static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703static int __init ioapic_pirq_setup(char *str)
704{
705 int i, max;
706 int ints[MAX_PIRQS+1];
707
708 get_options(str, ARRAY_SIZE(ints), ints);
709
710 for (i = 0; i < MAX_PIRQS; i++)
711 pirq_entries[i] = -1;
712
713 pirqs_enabled = 1;
714 apic_printk(APIC_VERBOSE, KERN_INFO
715 "PIRQ redirection, working around broken MP-BIOS.\n");
716 max = MAX_PIRQS;
717 if (ints[0] < MAX_PIRQS)
718 max = ints[0];
719
720 for (i = 0; i < max; i++) {
721 apic_printk(APIC_VERBOSE, KERN_DEBUG
722 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
723 /*
724 * PIRQs are mapped upside down, usually.
725 */
726 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
727 }
728 return 1;
729}
730
731__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200732#endif /* CONFIG_X86_32 */
733
734#ifdef CONFIG_INTR_REMAP
735/* I/O APIC RTE contents at the OS boot up */
736static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
737
738/*
739 * Saves and masks all the unmasked IO-APIC RTE's
740 */
741int save_mask_IO_APIC_setup(void)
742{
743 union IO_APIC_reg_01 reg_01;
744 unsigned long flags;
745 int apic, pin;
746
747 /*
748 * The number of IO-APIC IRQ registers (== #pins):
749 */
750 for (apic = 0; apic < nr_ioapics; apic++) {
751 spin_lock_irqsave(&ioapic_lock, flags);
752 reg_01.raw = io_apic_read(apic, 1);
753 spin_unlock_irqrestore(&ioapic_lock, flags);
754 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
755 }
756
757 for (apic = 0; apic < nr_ioapics; apic++) {
758 early_ioapic_entries[apic] =
759 kzalloc(sizeof(struct IO_APIC_route_entry) *
760 nr_ioapic_registers[apic], GFP_KERNEL);
761 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400762 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200763 }
764
765 for (apic = 0; apic < nr_ioapics; apic++)
766 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
767 struct IO_APIC_route_entry entry;
768
769 entry = early_ioapic_entries[apic][pin] =
770 ioapic_read_entry(apic, pin);
771 if (!entry.mask) {
772 entry.mask = 1;
773 ioapic_write_entry(apic, pin, entry);
774 }
775 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400776
Ingo Molnar54168ed2008-08-20 09:07:45 +0200777 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400778
779nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400780 while (apic >= 0)
781 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400782 memset(early_ioapic_entries, 0,
783 ARRAY_SIZE(early_ioapic_entries));
784
785 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200786}
787
788void restore_IO_APIC_setup(void)
789{
790 int apic, pin;
791
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400792 for (apic = 0; apic < nr_ioapics; apic++) {
793 if (!early_ioapic_entries[apic])
794 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200795 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
796 ioapic_write_entry(apic, pin,
797 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400798 kfree(early_ioapic_entries[apic]);
799 early_ioapic_entries[apic] = NULL;
800 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200801}
802
803void reinit_intr_remapped_IO_APIC(int intr_remapping)
804{
805 /*
806 * for now plain restore of previous settings.
807 * TBD: In the case of OS enabling interrupt-remapping,
808 * IO-APIC RTE's need to be setup to point to interrupt-remapping
809 * table entries. for now, do a plain restore, and wait for
810 * the setup_IO_APIC_irqs() to do proper initialization.
811 */
812 restore_IO_APIC_setup();
813}
814#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816/*
817 * Find the IRQ entry number of a certain pin.
818 */
819static int find_irq_entry(int apic, int pin, int type)
820{
821 int i;
822
823 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400824 if (mp_irqs[i].mp_irqtype == type &&
825 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
826 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
827 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return i;
829
830 return -1;
831}
832
833/*
834 * Find the pin to which IRQ[irq] (ISA) is connected
835 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800836static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
838 int i;
839
840 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400841 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300843 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400844 (mp_irqs[i].mp_irqtype == type) &&
845 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400847 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849 return -1;
850}
851
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800852static int __init find_isa_irq_apic(int irq, int type)
853{
854 int i;
855
856 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400857 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800858
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300859 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400860 (mp_irqs[i].mp_irqtype == type) &&
861 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800862 break;
863 }
864 if (i < mp_irq_entries) {
865 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200866 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400867 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800868 return apic;
869 }
870 }
871
872 return -1;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
876 * Find a specific PCI IRQ entry.
877 * Not an __init, possibly needed by modules
878 */
879static int pin_2_irq(int idx, int apic, int pin);
880
881int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
882{
883 int apic, i, best_guess = -1;
884
Ingo Molnar54168ed2008-08-20 09:07:45 +0200885 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
886 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400887 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200888 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return -1;
890 }
891 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400892 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400895 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
896 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300899 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400900 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400902 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200903 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (!(apic || IO_APIC_IRQ(irq)))
906 continue;
907
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400908 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return irq;
910 /*
911 * Use the first all-but-pin matching entry as a
912 * best-guess fuzzy result for broken mptables.
913 */
914 if (best_guess < 0)
915 best_guess = irq;
916 }
917 }
918 return best_guess;
919}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200920
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700921EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300923#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/*
925 * EISA Edge/Level control register, ELCR
926 */
927static int EISA_ELCR(unsigned int irq)
928{
Yinghai Lu99d093d2008-12-05 18:58:32 -0800929 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned int port = 0x4d0 + (irq >> 3);
931 return (inb(port) >> (irq & 7)) & 1;
932 }
933 apic_printk(APIC_VERBOSE, KERN_INFO
934 "Broken MPtable reports ISA irq %d\n", irq);
935 return 0;
936}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200937
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300938#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300940/* ISA interrupts are always polarity zero edge triggered,
941 * when listed as conforming in the MP table. */
942
943#define default_ISA_trigger(idx) (0)
944#define default_ISA_polarity(idx) (0)
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/* EISA interrupts are always polarity zero and can be edge or level
947 * trigger depending on the ELCR value. If an interrupt is listed as
948 * EISA conforming in the MP table, that means its trigger type must
949 * be read in from the ELCR */
950
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400951#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300952#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954/* PCI interrupts are always polarity one level triggered,
955 * when listed as conforming in the MP table. */
956
957#define default_PCI_trigger(idx) (1)
958#define default_PCI_polarity(idx) (1)
959
960/* MCA interrupts are always polarity zero level triggered,
961 * when listed as conforming in the MP table. */
962
963#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300964#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Shaohua Li61fd47e2007-11-17 01:05:28 -0500966static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400968 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 int polarity;
970
971 /*
972 * Determine IRQ line polarity (high active or low active):
973 */
Ingo Molnar54168ed2008-08-20 09:07:45 +0200974 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200976 case 0: /* conforms, ie. bus-type dependent polarity */
977 if (test_bit(bus, mp_bus_not_pci))
978 polarity = default_ISA_polarity(idx);
979 else
980 polarity = default_PCI_polarity(idx);
981 break;
982 case 1: /* high active */
983 {
984 polarity = 0;
985 break;
986 }
987 case 2: /* reserved */
988 {
989 printk(KERN_WARNING "broken BIOS!!\n");
990 polarity = 1;
991 break;
992 }
993 case 3: /* low active */
994 {
995 polarity = 1;
996 break;
997 }
998 default: /* invalid */
999 {
1000 printk(KERN_WARNING "broken BIOS!!\n");
1001 polarity = 1;
1002 break;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005 return polarity;
1006}
1007
1008static int MPBIOS_trigger(int idx)
1009{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001010 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 int trigger;
1012
1013 /*
1014 * Determine IRQ trigger mode (edge or level sensitive):
1015 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001016 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001018 case 0: /* conforms, ie. bus-type dependent */
1019 if (test_bit(bus, mp_bus_not_pci))
1020 trigger = default_ISA_trigger(idx);
1021 else
1022 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001023#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001024 switch (mp_bus_id_to_type[bus]) {
1025 case MP_BUS_ISA: /* ISA pin */
1026 {
1027 /* set before the switch */
1028 break;
1029 }
1030 case MP_BUS_EISA: /* EISA pin */
1031 {
1032 trigger = default_EISA_trigger(idx);
1033 break;
1034 }
1035 case MP_BUS_PCI: /* PCI pin */
1036 {
1037 /* set before the switch */
1038 break;
1039 }
1040 case MP_BUS_MCA: /* MCA pin */
1041 {
1042 trigger = default_MCA_trigger(idx);
1043 break;
1044 }
1045 default:
1046 {
1047 printk(KERN_WARNING "broken BIOS!!\n");
1048 trigger = 1;
1049 break;
1050 }
1051 }
1052#endif
1053 break;
1054 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001055 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001056 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001057 break;
1058 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001059 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001060 {
1061 printk(KERN_WARNING "broken BIOS!!\n");
1062 trigger = 1;
1063 break;
1064 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001065 case 3: /* level */
1066 {
1067 trigger = 1;
1068 break;
1069 }
1070 default: /* invalid */
1071 {
1072 printk(KERN_WARNING "broken BIOS!!\n");
1073 trigger = 0;
1074 break;
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077 return trigger;
1078}
1079
1080static inline int irq_polarity(int idx)
1081{
1082 return MPBIOS_polarity(idx);
1083}
1084
1085static inline int irq_trigger(int idx)
1086{
1087 return MPBIOS_trigger(idx);
1088}
1089
Yinghai Luefa25592008-08-19 20:50:36 -07001090int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091static int pin_2_irq(int idx, int apic, int pin)
1092{
1093 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001094 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 /*
1097 * Debugging check, we are in big trouble if this message pops up!
1098 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001099 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1101
Ingo Molnar54168ed2008-08-20 09:07:45 +02001102 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001103 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001104 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001105 /*
1106 * PCI IRQs are mapped in order
1107 */
1108 i = irq = 0;
1109 while (i < apic)
1110 irq += nr_ioapic_registers[i++];
1111 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001112 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001113 * For MPS mode, so far only needed by ES7000 platform
1114 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001115 if (ioapic_renumber_irq)
1116 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 }
1118
Ingo Molnar54168ed2008-08-20 09:07:45 +02001119#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /*
1121 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1122 */
1123 if ((pin >= 16) && (pin <= 23)) {
1124 if (pirq_entries[pin-16] != -1) {
1125 if (!pirq_entries[pin-16]) {
1126 apic_printk(APIC_VERBOSE, KERN_DEBUG
1127 "disabling PIRQ%d\n", pin-16);
1128 } else {
1129 irq = pirq_entries[pin-16];
1130 apic_printk(APIC_VERBOSE, KERN_DEBUG
1131 "using PIRQ%d -> IRQ %d\n",
1132 pin-16, irq);
1133 }
1134 }
1135 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001136#endif
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return irq;
1139}
1140
Yinghai Lu497c9a12008-08-19 20:50:28 -07001141void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001143 /* Used to the online set of cpus does not change
1144 * during assign_irq_vector.
1145 */
1146 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}
1148
Yinghai Lu497c9a12008-08-19 20:50:28 -07001149void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001150{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001151 spin_unlock(&vector_lock);
1152}
1153
Mike Travise7986732008-12-16 17:33:52 -08001154static int
1155__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001156{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001157 /*
1158 * NOTE! The local APIC isn't very good at handling
1159 * multiple interrupts at the same interrupt level.
1160 * As the interrupt level is determined by taking the
1161 * vector number and shifting that right by 4, we
1162 * want to spread these out a bit so that they don't
1163 * all fall in the same interrupt level.
1164 *
1165 * Also, we've got to be careful not to trash gate
1166 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1167 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001168 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1169 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001170 int cpu, err;
1171 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001172
Ingo Molnar54168ed2008-08-20 09:07:45 +02001173 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1174 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001175
Mike Travis22f65d32008-12-16 17:33:56 -08001176 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1177 return -ENOMEM;
1178
Ingo Molnar54168ed2008-08-20 09:07:45 +02001179 old_vector = cfg->vector;
1180 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001181 cpumask_and(tmp_mask, mask, cpu_online_mask);
1182 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1183 if (!cpumask_empty(tmp_mask)) {
1184 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001185 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001186 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001187 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001188
Mike Travise7986732008-12-16 17:33:52 -08001189 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001190 err = -ENOSPC;
1191 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001192 int new_cpu;
1193 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001194
Mike Travis22f65d32008-12-16 17:33:56 -08001195 vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001196
Ingo Molnar54168ed2008-08-20 09:07:45 +02001197 vector = current_vector;
1198 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001199next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001200 vector += 8;
1201 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001202 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001203 offset = (offset + 1) % 8;
1204 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001205 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001206 if (unlikely(current_vector == vector))
1207 continue;
1208#ifdef CONFIG_X86_64
1209 if (vector == IA32_SYSCALL_VECTOR)
1210 goto next;
1211#else
1212 if (vector == SYSCALL_VECTOR)
1213 goto next;
1214#endif
Mike Travis22f65d32008-12-16 17:33:56 -08001215 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001216 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1217 goto next;
1218 /* Found one! */
1219 current_vector = vector;
1220 current_offset = offset;
1221 if (old_vector) {
1222 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001223 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001224 }
Mike Travis22f65d32008-12-16 17:33:56 -08001225 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001226 per_cpu(vector_irq, new_cpu)[vector] = irq;
1227 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001228 cpumask_copy(cfg->domain, tmp_mask);
1229 err = 0;
1230 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001231 }
Mike Travis22f65d32008-12-16 17:33:56 -08001232 free_cpumask_var(tmp_mask);
1233 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001234}
1235
Mike Travise7986732008-12-16 17:33:52 -08001236static int
1237assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001238{
1239 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001240 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001241
1242 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001243 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001244 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001245 return err;
1246}
1247
Yinghai Lu3145e942008-12-05 18:58:34 -08001248static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001249{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001250 int cpu, vector;
1251
Yinghai Lu497c9a12008-08-19 20:50:28 -07001252 BUG_ON(!cfg->vector);
1253
1254 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001255 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001256 per_cpu(vector_irq, cpu)[vector] = -1;
1257
1258 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001259 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001260
1261 if (likely(!cfg->move_in_progress))
1262 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001263 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001264 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1265 vector++) {
1266 if (per_cpu(vector_irq, cpu)[vector] != irq)
1267 continue;
1268 per_cpu(vector_irq, cpu)[vector] = -1;
1269 break;
1270 }
1271 }
1272 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001273}
1274
1275void __setup_vector_irq(int cpu)
1276{
1277 /* Initialize vector_irq on a new cpu */
1278 /* This function must be called with vector_lock held */
1279 int irq, vector;
1280 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001281 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001282
1283 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001284 for_each_irq_desc(irq, desc) {
1285 if (!desc)
1286 continue;
1287 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001288 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001289 continue;
1290 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001291 per_cpu(vector_irq, cpu)[vector] = irq;
1292 }
1293 /* Mark the free vectors */
1294 for (vector = 0; vector < NR_VECTORS; ++vector) {
1295 irq = per_cpu(vector_irq, cpu)[vector];
1296 if (irq < 0)
1297 continue;
1298
1299 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001300 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001301 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001302 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001303}
Glauber Costa3fde6902008-05-28 20:34:19 -07001304
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001305static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001306#ifdef CONFIG_INTR_REMAP
1307static struct irq_chip ir_ioapic_chip;
1308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Ingo Molnar54168ed2008-08-20 09:07:45 +02001310#define IOAPIC_AUTO -1
1311#define IOAPIC_EDGE 0
1312#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001314#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001315static inline int IO_APIC_irq_trigger(int irq)
1316{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001317 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001318
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001319 for (apic = 0; apic < nr_ioapics; apic++) {
1320 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1321 idx = find_irq_entry(apic, pin, mp_INT);
1322 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1323 return irq_trigger(idx);
1324 }
1325 }
1326 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001327 * nonexistent IRQs are edge default
1328 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001329 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001330}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001331#else
1332static inline int IO_APIC_irq_trigger(int irq)
1333{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001334 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001335}
1336#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001337
Yinghai Lu3145e942008-12-05 18:58:34 -08001338static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
Yinghai Lu199751d2008-08-19 20:50:27 -07001340
Jan Beulich6ebcc002006-06-26 13:56:46 +02001341 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001342 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001343 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001344 else
1345 desc->status &= ~IRQ_LEVEL;
1346
Ingo Molnar54168ed2008-08-20 09:07:45 +02001347#ifdef CONFIG_INTR_REMAP
1348 if (irq_remapped(irq)) {
1349 desc->status |= IRQ_MOVE_PCNTXT;
1350 if (trigger)
1351 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1352 handle_fasteoi_irq,
1353 "fasteoi");
1354 else
1355 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1356 handle_edge_irq, "edge");
1357 return;
1358 }
1359#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001360 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1361 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001362 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001363 handle_fasteoi_irq,
1364 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001365 else
Ingo Molnara460e742006-10-17 00:10:03 -07001366 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001367 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001368}
1369
1370static int setup_ioapic_entry(int apic, int irq,
1371 struct IO_APIC_route_entry *entry,
1372 unsigned int destination, int trigger,
1373 int polarity, int vector)
1374{
1375 /*
1376 * add it to the IO-APIC irq-routing table:
1377 */
1378 memset(entry,0,sizeof(*entry));
1379
Ingo Molnar54168ed2008-08-20 09:07:45 +02001380#ifdef CONFIG_INTR_REMAP
1381 if (intr_remapping_enabled) {
1382 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1383 struct irte irte;
1384 struct IR_IO_APIC_route_entry *ir_entry =
1385 (struct IR_IO_APIC_route_entry *) entry;
1386 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001387
Ingo Molnar54168ed2008-08-20 09:07:45 +02001388 if (!iommu)
1389 panic("No mapping iommu for ioapic %d\n", apic);
1390
1391 index = alloc_irte(iommu, irq, 1);
1392 if (index < 0)
1393 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1394
1395 memset(&irte, 0, sizeof(irte));
1396
1397 irte.present = 1;
1398 irte.dst_mode = INT_DEST_MODE;
1399 irte.trigger_mode = trigger;
1400 irte.dlvry_mode = INT_DELIVERY_MODE;
1401 irte.vector = vector;
1402 irte.dest_id = IRTE_DEST(destination);
1403
1404 modify_irte(irq, &irte);
1405
1406 ir_entry->index2 = (index >> 15) & 0x1;
1407 ir_entry->zero = 0;
1408 ir_entry->format = 1;
1409 ir_entry->index = (index & 0x7fff);
1410 } else
1411#endif
1412 {
1413 entry->delivery_mode = INT_DELIVERY_MODE;
1414 entry->dest_mode = INT_DEST_MODE;
1415 entry->dest = destination;
1416 }
1417
1418 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001419 entry->trigger = trigger;
1420 entry->polarity = polarity;
1421 entry->vector = vector;
1422
1423 /* Mask level triggered irqs.
1424 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1425 */
1426 if (trigger)
1427 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001428 return 0;
1429}
1430
Yinghai Lu3145e942008-12-05 18:58:34 -08001431static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001432 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001433{
1434 struct irq_cfg *cfg;
1435 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001436 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001437
1438 if (!IO_APIC_IRQ(irq))
1439 return;
1440
Yinghai Lu3145e942008-12-05 18:58:34 -08001441 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001442
Mike Travis22f65d32008-12-16 17:33:56 -08001443 if (assign_irq_vector(irq, cfg, TARGET_CPUS))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001444 return;
1445
Mike Travis22f65d32008-12-16 17:33:56 -08001446 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001447
1448 apic_printk(APIC_VERBOSE,KERN_DEBUG
1449 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1450 "IRQ %d Mode:%i Active:%i)\n",
1451 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1452 irq, trigger, polarity);
1453
1454
1455 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
Mike Travis22f65d32008-12-16 17:33:56 -08001456 dest, trigger, polarity, cfg->vector)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001457 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1458 mp_ioapics[apic].mp_apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001459 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001460 return;
1461 }
1462
Yinghai Lu3145e942008-12-05 18:58:34 -08001463 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001464 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001465 disable_8259A_irq(irq);
1466
1467 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
1470static void __init setup_IO_APIC_irqs(void)
1471{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001472 int apic, pin, idx, irq;
1473 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001474 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001475 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001476 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
1478 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1479
1480 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001481 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001483 idx = find_irq_entry(apic, pin, mp_INT);
1484 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001485 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001486 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001487 apic_printk(APIC_VERBOSE,
1488 KERN_DEBUG " %d-%d",
1489 mp_ioapics[apic].mp_apicid,
1490 pin);
1491 } else
1492 apic_printk(APIC_VERBOSE, " %d-%d",
1493 mp_ioapics[apic].mp_apicid,
1494 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001495 continue;
1496 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001497 if (notcon) {
1498 apic_printk(APIC_VERBOSE,
1499 " (apicid-pin) not connected\n");
1500 notcon = 0;
1501 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001502
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001503 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001504#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001505 if (multi_timer_check(apic, irq))
1506 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001507#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001508 desc = irq_to_desc_alloc_cpu(irq, cpu);
1509 if (!desc) {
1510 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1511 continue;
1512 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001513 cfg = desc->chip_data;
1514 add_pin_to_irq_cpu(cfg, cpu, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001515
Yinghai Lu3145e942008-12-05 18:58:34 -08001516 setup_IO_APIC_irq(apic, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001517 irq_trigger(idx), irq_polarity(idx));
1518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001521 if (notcon)
1522 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001523 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
1526/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001527 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001529static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1530 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Ingo Molnar54168ed2008-08-20 09:07:45 +02001534#ifdef CONFIG_INTR_REMAP
1535 if (intr_remapping_enabled)
1536 return;
1537#endif
1538
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001539 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 /*
1542 * We use logical delivery to get the timer IRQ
1543 * to the first CPU.
1544 */
1545 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001546 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001547 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 entry.delivery_mode = INT_DELIVERY_MODE;
1549 entry.polarity = 0;
1550 entry.trigger = 0;
1551 entry.vector = vector;
1552
1553 /*
1554 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001555 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001557 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /*
1560 * Add it to the IO-APIC irq-routing table:
1561 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001562 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001565
1566__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 int apic, i;
1569 union IO_APIC_reg_00 reg_00;
1570 union IO_APIC_reg_01 reg_01;
1571 union IO_APIC_reg_02 reg_02;
1572 union IO_APIC_reg_03 reg_03;
1573 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001574 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001575 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001576 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 if (apic_verbosity == APIC_QUIET)
1579 return;
1580
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001581 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 for (i = 0; i < nr_ioapics; i++)
1583 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001584 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586 /*
1587 * We are a bit conservative about what we expect. We have to
1588 * know about every hardware change ASAP.
1589 */
1590 printk(KERN_INFO "testing the IO APIC.......................\n");
1591
1592 for (apic = 0; apic < nr_ioapics; apic++) {
1593
1594 spin_lock_irqsave(&ioapic_lock, flags);
1595 reg_00.raw = io_apic_read(apic, 0);
1596 reg_01.raw = io_apic_read(apic, 1);
1597 if (reg_01.bits.version >= 0x10)
1598 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001599 if (reg_01.bits.version >= 0x20)
1600 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 spin_unlock_irqrestore(&ioapic_lock, flags);
1602
Ingo Molnar54168ed2008-08-20 09:07:45 +02001603 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001604 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1606 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1607 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1608 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Ingo Molnar54168ed2008-08-20 09:07:45 +02001610 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1614 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /*
1617 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1618 * but the value of reg_02 is read as the previous read register
1619 * value, so ignore it if reg_02 == reg_01.
1620 */
1621 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1622 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1623 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625
1626 /*
1627 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1628 * or reg_03, but the value of reg_0[23] is read as the previous read
1629 * register value, so ignore it if reg_03 == reg_0[12].
1630 */
1631 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1632 reg_03.raw != reg_01.raw) {
1633 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1634 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 }
1636
1637 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1638
Yinghai Lud83e94a2008-08-19 20:50:33 -07001639 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1640 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 for (i = 0; i <= reg_01.bits.entries; i++) {
1643 struct IO_APIC_route_entry entry;
1644
Andi Kleencf4c6a22006-09-26 10:52:30 +02001645 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Ingo Molnar54168ed2008-08-20 09:07:45 +02001647 printk(KERN_DEBUG " %02x %03X ",
1648 i,
1649 entry.dest
1650 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
1652 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1653 entry.mask,
1654 entry.trigger,
1655 entry.irr,
1656 entry.polarity,
1657 entry.delivery_status,
1658 entry.dest_mode,
1659 entry.delivery_mode,
1660 entry.vector
1661 );
1662 }
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001665 for_each_irq_desc(irq, desc) {
1666 struct irq_pin_list *entry;
1667
1668 if (!desc)
1669 continue;
1670 cfg = desc->chip_data;
1671 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001672 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001674 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 for (;;) {
1676 printk("-> %d:%d", entry->apic, entry->pin);
1677 if (!entry->next)
1678 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001679 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681 printk("\n");
1682 }
1683
1684 printk(KERN_INFO ".................................... done.\n");
1685
1686 return;
1687}
1688
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001689__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 unsigned int v;
1692 int i, j;
1693
1694 if (apic_verbosity == APIC_QUIET)
1695 return;
1696
1697 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1698 for (i = 0; i < 8; i++) {
1699 v = apic_read(base + i*0x10);
1700 for (j = 0; j < 32; j++) {
1701 if (v & (1<<j))
1702 printk("1");
1703 else
1704 printk("0");
1705 }
1706 printk("\n");
1707 }
1708}
1709
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001710__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
1712 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001713 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
1715 if (apic_verbosity == APIC_QUIET)
1716 return;
1717
1718 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1719 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001720 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001721 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 v = apic_read(APIC_LVR);
1723 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1724 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001725 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 v = apic_read(APIC_TASKPRI);
1728 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1729
Ingo Molnar54168ed2008-08-20 09:07:45 +02001730 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001731 if (!APIC_XAPIC(ver)) {
1732 v = apic_read(APIC_ARBPRI);
1733 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1734 v & APIC_ARBPRI_MASK);
1735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 v = apic_read(APIC_PROCPRI);
1737 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1738 }
1739
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001740 /*
1741 * Remote read supported only in the 82489DX and local APIC for
1742 * Pentium processors.
1743 */
1744 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1745 v = apic_read(APIC_RRR);
1746 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1747 }
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 v = apic_read(APIC_LDR);
1750 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001751 if (!x2apic_enabled()) {
1752 v = apic_read(APIC_DFR);
1753 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 v = apic_read(APIC_SPIV);
1756 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1757
1758 printk(KERN_DEBUG "... APIC ISR field:\n");
1759 print_APIC_bitfield(APIC_ISR);
1760 printk(KERN_DEBUG "... APIC TMR field:\n");
1761 print_APIC_bitfield(APIC_TMR);
1762 printk(KERN_DEBUG "... APIC IRR field:\n");
1763 print_APIC_bitfield(APIC_IRR);
1764
Ingo Molnar54168ed2008-08-20 09:07:45 +02001765 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1766 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 v = apic_read(APIC_ESR);
1770 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1771 }
1772
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001773 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001774 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1775 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 v = apic_read(APIC_LVTT);
1778 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1779
1780 if (maxlvt > 3) { /* PC is LVT#4. */
1781 v = apic_read(APIC_LVTPC);
1782 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1783 }
1784 v = apic_read(APIC_LVT0);
1785 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1786 v = apic_read(APIC_LVT1);
1787 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1788
1789 if (maxlvt > 2) { /* ERR is LVT#3. */
1790 v = apic_read(APIC_LVTERR);
1791 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1792 }
1793
1794 v = apic_read(APIC_TMICT);
1795 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1796 v = apic_read(APIC_TMCCT);
1797 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1798 v = apic_read(APIC_TDCR);
1799 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1800 printk("\n");
1801}
1802
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001803__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001805 int cpu;
1806
1807 preempt_disable();
1808 for_each_online_cpu(cpu)
1809 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1810 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811}
1812
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001813__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 unsigned int v;
1816 unsigned long flags;
1817
1818 if (apic_verbosity == APIC_QUIET)
1819 return;
1820
1821 printk(KERN_DEBUG "\nprinting PIC contents\n");
1822
1823 spin_lock_irqsave(&i8259A_lock, flags);
1824
1825 v = inb(0xa1) << 8 | inb(0x21);
1826 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1827
1828 v = inb(0xa0) << 8 | inb(0x20);
1829 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1830
Ingo Molnar54168ed2008-08-20 09:07:45 +02001831 outb(0x0b,0xa0);
1832 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001834 outb(0x0a,0xa0);
1835 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 spin_unlock_irqrestore(&i8259A_lock, flags);
1838
1839 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1840
1841 v = inb(0x4d1) << 8 | inb(0x4d0);
1842 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1843}
1844
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001845__apicdebuginit(int) print_all_ICs(void)
1846{
1847 print_PIC();
1848 print_all_local_APICs();
1849 print_IO_APIC();
1850
1851 return 0;
1852}
1853
1854fs_initcall(print_all_ICs);
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
Yinghai Luefa25592008-08-19 20:50:36 -07001857/* Where if anywhere is the i8259 connect in external int mode */
1858static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1859
Ingo Molnar54168ed2008-08-20 09:07:45 +02001860void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861{
1862 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001863 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001864 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 unsigned long flags;
1866
Ingo Molnar54168ed2008-08-20 09:07:45 +02001867#ifdef CONFIG_X86_32
1868 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (!pirqs_enabled)
1870 for (i = 0; i < MAX_PIRQS; i++)
1871 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001872#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
1874 /*
1875 * The number of IO-APIC IRQ registers (== #pins):
1876 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001877 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001879 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001881 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1882 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001883 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001884 int pin;
1885 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001886 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001887 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001888 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001889
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001890 /* If the interrupt line is enabled and in ExtInt mode
1891 * I have found the pin where the i8259 is connected.
1892 */
1893 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1894 ioapic_i8259.apic = apic;
1895 ioapic_i8259.pin = pin;
1896 goto found_i8259;
1897 }
1898 }
1899 }
1900 found_i8259:
1901 /* Look to see what if the MP table has reported the ExtINT */
1902 /* If we could not find the appropriate pin by looking at the ioapic
1903 * the i8259 probably is not connected the ioapic but give the
1904 * mptable a chance anyway.
1905 */
1906 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1907 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1908 /* Trust the MP table if nothing is setup in the hardware */
1909 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1910 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1911 ioapic_i8259.pin = i8259_pin;
1912 ioapic_i8259.apic = i8259_apic;
1913 }
1914 /* Complain if the MP table and the hardware disagree */
1915 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1916 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1917 {
1918 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
1921 /*
1922 * Do not trust the IO-APIC being empty at bootup
1923 */
1924 clear_IO_APIC();
1925}
1926
1927/*
1928 * Not an __init, needed by the reboot code
1929 */
1930void disable_IO_APIC(void)
1931{
1932 /*
1933 * Clear the IO-APIC before rebooting:
1934 */
1935 clear_IO_APIC();
1936
Eric W. Biederman650927e2005-06-25 14:57:44 -07001937 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001938 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001939 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001940 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001941 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001942 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001943 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001944
1945 memset(&entry, 0, sizeof(entry));
1946 entry.mask = 0; /* Enabled */
1947 entry.trigger = 0; /* Edge */
1948 entry.irr = 0;
1949 entry.polarity = 0; /* High */
1950 entry.delivery_status = 0;
1951 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001952 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001953 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001954 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001955
1956 /*
1957 * Add it to the IO-APIC irq-routing table:
1958 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001959 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001960 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001961
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001962 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Ingo Molnar54168ed2008-08-20 09:07:45 +02001965#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966/*
1967 * function to set the IO-APIC physical IDs based on the
1968 * values stored in the MPC table.
1969 *
1970 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1971 */
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973static void __init setup_ioapic_ids_from_mpc(void)
1974{
1975 union IO_APIC_reg_00 reg_00;
1976 physid_mask_t phys_id_present_map;
1977 int apic;
1978 int i;
1979 unsigned char old_id;
1980 unsigned long flags;
1981
Yinghai Lua4dbc342008-07-25 02:14:28 -07001982 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001983 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001984
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001986 * Don't check I/O APIC IDs for xAPIC systems. They have
1987 * no meaning without the serial APIC bus.
1988 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001989 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1990 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001991 return;
1992 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 * This is broken; anything with a real cpu count has to
1994 * circumvent this idiocy regardless.
1995 */
1996 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1997
1998 /*
1999 * Set the IOAPIC ID to the value stored in the MPC table.
2000 */
2001 for (apic = 0; apic < nr_ioapics; apic++) {
2002
2003 /* Read the register 0 value */
2004 spin_lock_irqsave(&ioapic_lock, flags);
2005 reg_00.raw = io_apic_read(apic, 0);
2006 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002007
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002008 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002010 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002012 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2014 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002015 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /*
2019 * Sanity check, is the ID really free? Every APIC in a
2020 * system must have a unique ID or we get lots of nice
2021 * 'stuck on smp_invalidate_needed IPI wait' messages.
2022 */
2023 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002024 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002026 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 for (i = 0; i < get_physical_broadcast(); i++)
2028 if (!physid_isset(i, phys_id_present_map))
2029 break;
2030 if (i >= get_physical_broadcast())
2031 panic("Max APIC ID exceeded!\n");
2032 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2033 i);
2034 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002035 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 } else {
2037 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002038 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 apic_printk(APIC_VERBOSE, "Setting %d in the "
2040 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002041 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2043 }
2044
2045
2046 /*
2047 * We need to adjust the IRQ routing table
2048 * if the ID changed.
2049 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002050 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002052 if (mp_irqs[i].mp_dstapic == old_id)
2053 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002054 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 /*
2057 * Read the right value from the MPC table and
2058 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002059 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 apic_printk(APIC_VERBOSE, KERN_INFO
2061 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002062 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002064 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002066 io_apic_write(apic, 0, reg_00.raw);
2067 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 /*
2070 * Sanity check
2071 */
2072 spin_lock_irqsave(&ioapic_lock, flags);
2073 reg_00.raw = io_apic_read(apic, 0);
2074 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002075 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 printk("could not set ID!\n");
2077 else
2078 apic_printk(APIC_VERBOSE, " ok.\n");
2079 }
2080}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002081#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002083int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002084
2085static int __init notimercheck(char *s)
2086{
2087 no_timer_check = 1;
2088 return 1;
2089}
2090__setup("no_timer_check", notimercheck);
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092/*
2093 * There is a nasty bug in some older SMP boards, their mptable lies
2094 * about the timer IRQ. We do the following to work around the situation:
2095 *
2096 * - timer IRQ defaults to IO-APIC IRQ
2097 * - if this function detects that timer IRQs are defunct, then we fall
2098 * back to ISA timer IRQs
2099 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002100static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101{
2102 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002103 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Zachary Amsden8542b202006-12-07 02:14:09 +01002105 if (no_timer_check)
2106 return 1;
2107
Ingo Molnar4aae0702007-12-18 18:05:58 +01002108 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 local_irq_enable();
2110 /* Let ten ticks pass... */
2111 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002112 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 /*
2115 * Expect a few ticks at least, to be sure some possible
2116 * glue logic does not lock up after one or two first
2117 * ticks in a non-ExtINT mode. Also the local APIC
2118 * might have cached one ExtINT interrupt. Finally, at
2119 * least one tick may be lost due to delays.
2120 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002121
2122 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002123 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 return 0;
2126}
2127
2128/*
2129 * In the SMP+IOAPIC case it might happen that there are an unspecified
2130 * number of pending IRQ events unhandled. These cases are very rare,
2131 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2132 * better to do it this way as thus we do not have to be aware of
2133 * 'pending' interrupts in the IRQ path, except at this point.
2134 */
2135/*
2136 * Edge triggered needs to resend any interrupt
2137 * that was delayed but this is now handled in the device
2138 * independent code.
2139 */
2140
2141/*
2142 * Starting up a edge-triggered IO-APIC interrupt is
2143 * nasty - we need to make sure that we get the edge.
2144 * If it is already asserted for some reason, we need
2145 * return 1 to indicate that is was pending.
2146 *
2147 * This is not complete - we should be able to fake
2148 * an edge even if it isn't on the 8259A...
2149 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002150
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002151static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 int was_pending = 0;
2154 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002155 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002158 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 disable_8259A_irq(irq);
2160 if (i8259A_irq_pending(irq))
2161 was_pending = 1;
2162 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002163 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002164 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 spin_unlock_irqrestore(&ioapic_lock, flags);
2166
2167 return was_pending;
2168}
2169
Ingo Molnar54168ed2008-08-20 09:07:45 +02002170#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002171static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002173
2174 struct irq_cfg *cfg = irq_cfg(irq);
2175 unsigned long flags;
2176
2177 spin_lock_irqsave(&vector_lock, flags);
Mike Travis22f65d32008-12-16 17:33:56 -08002178 send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002179 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002180
2181 return 1;
2182}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002183#else
2184static int ioapic_retrigger_irq(unsigned int irq)
2185{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002186 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002187
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002188 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002189}
2190#endif
2191
2192/*
2193 * Level and edge triggered IO-APIC interrupts need different handling,
2194 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2195 * handled with the level-triggered descriptor, but that one has slightly
2196 * more overhead. Level-triggered interrupts cannot be handled with the
2197 * edge-triggered handler, without risking IRQ storms and other ugly
2198 * races.
2199 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002200
Yinghai Lu497c9a12008-08-19 20:50:28 -07002201#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002202
2203#ifdef CONFIG_INTR_REMAP
2204static void ir_irq_migration(struct work_struct *work);
2205
2206static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2207
2208/*
2209 * Migrate the IO-APIC irq in the presence of intr-remapping.
2210 *
2211 * For edge triggered, irq migration is a simple atomic update(of vector
2212 * and cpu destination) of IRTE and flush the hardware cache.
2213 *
2214 * For level triggered, we need to modify the io-apic RTE aswell with the update
2215 * vector information, along with modifying IRTE with vector and destination.
2216 * So irq migration for level triggered is little bit more complex compared to
2217 * edge triggered migration. But the good news is, we use the same algorithm
2218 * for level triggered migration as we have today, only difference being,
2219 * we now initiate the irq migration from process context instead of the
2220 * interrupt context.
2221 *
2222 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2223 * suppression) to the IO-APIC, level triggered irq migration will also be
2224 * as simple as edge triggered migration and we can do the irq migration
2225 * with a simple atomic update to IO-APIC RTE.
2226 */
Mike Travise7986732008-12-16 17:33:52 -08002227static void
2228migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002229{
2230 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002231 struct irte irte;
2232 int modify_ioapic_rte;
2233 unsigned int dest;
2234 unsigned long flags;
Yinghai Lu3145e942008-12-05 18:58:34 -08002235 unsigned int irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002236
Mike Travis22f65d32008-12-16 17:33:56 -08002237 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002238 return;
2239
Yinghai Lu3145e942008-12-05 18:58:34 -08002240 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002241 if (get_irte(irq, &irte))
2242 return;
2243
Yinghai Lu3145e942008-12-05 18:58:34 -08002244 cfg = desc->chip_data;
2245 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002246 return;
2247
Yinghai Lu3145e942008-12-05 18:58:34 -08002248 set_extra_move_desc(desc, mask);
2249
Mike Travis22f65d32008-12-16 17:33:56 -08002250 dest = cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002251
Ingo Molnar54168ed2008-08-20 09:07:45 +02002252 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2253 if (modify_ioapic_rte) {
2254 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08002255 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002256 spin_unlock_irqrestore(&ioapic_lock, flags);
2257 }
2258
2259 irte.vector = cfg->vector;
2260 irte.dest_id = IRTE_DEST(dest);
2261
2262 /*
2263 * Modified the IRTE and flushes the Interrupt entry cache.
2264 */
2265 modify_irte(irq, &irte);
2266
Mike Travis22f65d32008-12-16 17:33:56 -08002267 if (cfg->move_in_progress)
2268 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002269
Mike Travis22f65d32008-12-16 17:33:56 -08002270 cpumask_copy(&desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002271}
2272
Yinghai Lu3145e942008-12-05 18:58:34 -08002273static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002274{
2275 int ret = -1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002276 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002277
Yinghai Lu3145e942008-12-05 18:58:34 -08002278 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002279
Yinghai Lu3145e942008-12-05 18:58:34 -08002280 if (io_apic_level_ack_pending(cfg)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002281 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002282 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002283 * vector information in the IO-APIC RTE and that will confuse
2284 * the EOI broadcast performed by cpu.
2285 * So, delay the irq migration to the next instance.
2286 */
2287 schedule_delayed_work(&ir_migration_work, 1);
2288 goto unmask;
2289 }
2290
2291 /* everthing is clear. we have right of way */
Mike Travise7986732008-12-16 17:33:52 -08002292 migrate_ioapic_irq_desc(desc, &desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002293
2294 ret = 0;
2295 desc->status &= ~IRQ_MOVE_PENDING;
Mike Travis22f65d32008-12-16 17:33:56 -08002296 cpumask_clear(&desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002297
2298unmask:
Yinghai Lu3145e942008-12-05 18:58:34 -08002299 unmask_IO_APIC_irq_desc(desc);
2300
Ingo Molnar54168ed2008-08-20 09:07:45 +02002301 return ret;
2302}
2303
2304static void ir_irq_migration(struct work_struct *work)
2305{
2306 unsigned int irq;
2307 struct irq_desc *desc;
2308
2309 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002310 if (!desc)
2311 continue;
2312
Ingo Molnar54168ed2008-08-20 09:07:45 +02002313 if (desc->status & IRQ_MOVE_PENDING) {
2314 unsigned long flags;
2315
2316 spin_lock_irqsave(&desc->lock, flags);
2317 if (!desc->chip->set_affinity ||
2318 !(desc->status & IRQ_MOVE_PENDING)) {
2319 desc->status &= ~IRQ_MOVE_PENDING;
2320 spin_unlock_irqrestore(&desc->lock, flags);
2321 continue;
2322 }
2323
Rusty Russell0de26522008-12-13 21:20:26 +10302324 desc->chip->set_affinity(irq, &desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002325 spin_unlock_irqrestore(&desc->lock, flags);
2326 }
2327 }
2328}
2329
2330/*
2331 * Migrates the IRQ destination in the process context.
2332 */
Rusty Russell968ea6d2008-12-13 21:55:51 +10302333static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2334 const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -08002335{
2336 if (desc->status & IRQ_LEVEL) {
2337 desc->status |= IRQ_MOVE_PENDING;
Rusty Russell968ea6d2008-12-13 21:55:51 +10302338 cpumask_copy(&desc->pending_mask, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002339 migrate_irq_remapped_level_desc(desc);
2340 return;
2341 }
2342
Mike Travise7986732008-12-16 17:33:52 -08002343 migrate_ioapic_irq_desc(desc, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002344}
Rusty Russell0de26522008-12-13 21:20:26 +10302345static void set_ir_ioapic_affinity_irq(unsigned int irq,
2346 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002347{
2348 struct irq_desc *desc = irq_to_desc(irq);
2349
Yinghai Lu3145e942008-12-05 18:58:34 -08002350 set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002351}
2352#endif
2353
Yinghai Lu497c9a12008-08-19 20:50:28 -07002354asmlinkage void smp_irq_move_cleanup_interrupt(void)
2355{
2356 unsigned vector, me;
2357 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002358#ifdef CONFIG_X86_64
2359 exit_idle();
2360#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002361 irq_enter();
2362
2363 me = smp_processor_id();
2364 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2365 unsigned int irq;
2366 struct irq_desc *desc;
2367 struct irq_cfg *cfg;
2368 irq = __get_cpu_var(vector_irq)[vector];
2369
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002370 if (irq == -1)
2371 continue;
2372
Yinghai Lu497c9a12008-08-19 20:50:28 -07002373 desc = irq_to_desc(irq);
2374 if (!desc)
2375 continue;
2376
2377 cfg = irq_cfg(irq);
2378 spin_lock(&desc->lock);
2379 if (!cfg->move_cleanup_count)
2380 goto unlock;
2381
Mike Travis22f65d32008-12-16 17:33:56 -08002382 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002383 goto unlock;
2384
2385 __get_cpu_var(vector_irq)[vector] = -1;
2386 cfg->move_cleanup_count--;
2387unlock:
2388 spin_unlock(&desc->lock);
2389 }
2390
2391 irq_exit();
2392}
2393
Yinghai Lu3145e942008-12-05 18:58:34 -08002394static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002395{
Yinghai Lu3145e942008-12-05 18:58:34 -08002396 struct irq_desc *desc = *descp;
2397 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002398 unsigned vector, me;
2399
2400 if (likely(!cfg->move_in_progress))
2401 return;
2402
2403 vector = ~get_irq_regs()->orig_ax;
2404 me = smp_processor_id();
Mike Travis22f65d32008-12-16 17:33:56 -08002405 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2406 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002407}
2408#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002409static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002410#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002411
Ingo Molnar54168ed2008-08-20 09:07:45 +02002412#ifdef CONFIG_INTR_REMAP
2413static void ack_x2apic_level(unsigned int irq)
2414{
2415 ack_x2APIC_irq();
2416}
2417
2418static void ack_x2apic_edge(unsigned int irq)
2419{
2420 ack_x2APIC_irq();
2421}
Yinghai Lu3145e942008-12-05 18:58:34 -08002422
Ingo Molnar54168ed2008-08-20 09:07:45 +02002423#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002424
Yinghai Lu1d025192008-08-19 20:50:34 -07002425static void ack_apic_edge(unsigned int irq)
2426{
Yinghai Lu3145e942008-12-05 18:58:34 -08002427 struct irq_desc *desc = irq_to_desc(irq);
2428
2429 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002430 move_native_irq(irq);
2431 ack_APIC_irq();
2432}
2433
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002434atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002435
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002436static void ack_apic_level(unsigned int irq)
2437{
Yinghai Lu3145e942008-12-05 18:58:34 -08002438 struct irq_desc *desc = irq_to_desc(irq);
2439
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002440#ifdef CONFIG_X86_32
2441 unsigned long v;
2442 int i;
2443#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002444 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002445 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002446
Yinghai Lu3145e942008-12-05 18:58:34 -08002447 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002448#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002449 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002450 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002451 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002452 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002453 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002454#endif
2455
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002456#ifdef CONFIG_X86_32
2457 /*
2458 * It appears there is an erratum which affects at least version 0x11
2459 * of I/O APIC (that's the 82093AA and cores integrated into various
2460 * chipsets). Under certain conditions a level-triggered interrupt is
2461 * erroneously delivered as edge-triggered one but the respective IRR
2462 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2463 * message but it will never arrive and further interrupts are blocked
2464 * from the source. The exact reason is so far unknown, but the
2465 * phenomenon was observed when two consecutive interrupt requests
2466 * from a given source get delivered to the same CPU and the source is
2467 * temporarily disabled in between.
2468 *
2469 * A workaround is to simulate an EOI message manually. We achieve it
2470 * by setting the trigger mode to edge and then to level when the edge
2471 * trigger mode gets detected in the TMR of a local APIC for a
2472 * level-triggered interrupt. We mask the source for the time of the
2473 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2474 * The idea is from Manfred Spraul. --macro
2475 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002476 cfg = desc->chip_data;
2477 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002478
2479 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2480#endif
2481
Ingo Molnar54168ed2008-08-20 09:07:45 +02002482 /*
2483 * We must acknowledge the irq before we move it or the acknowledge will
2484 * not propagate properly.
2485 */
2486 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002487
Ingo Molnar54168ed2008-08-20 09:07:45 +02002488 /* Now we can move and renable the irq */
2489 if (unlikely(do_unmask_irq)) {
2490 /* Only migrate the irq if the ack has been received.
2491 *
2492 * On rare occasions the broadcast level triggered ack gets
2493 * delayed going to ioapics, and if we reprogram the
2494 * vector while Remote IRR is still set the irq will never
2495 * fire again.
2496 *
2497 * To prevent this scenario we read the Remote IRR bit
2498 * of the ioapic. This has two effects.
2499 * - On any sane system the read of the ioapic will
2500 * flush writes (and acks) going to the ioapic from
2501 * this cpu.
2502 * - We get to see if the ACK has actually been delivered.
2503 *
2504 * Based on failed experiments of reprogramming the
2505 * ioapic entry from outside of irq context starting
2506 * with masking the ioapic entry and then polling until
2507 * Remote IRR was clear before reprogramming the
2508 * ioapic I don't trust the Remote IRR bit to be
2509 * completey accurate.
2510 *
2511 * However there appears to be no other way to plug
2512 * this race, so if the Remote IRR bit is not
2513 * accurate and is causing problems then it is a hardware bug
2514 * and you can go talk to the chipset vendor about it.
2515 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002516 cfg = desc->chip_data;
2517 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002518 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002519 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002520 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002521
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002522#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002523 if (!(v & (1 << (i & 0x1f)))) {
2524 atomic_inc(&irq_mis_count);
2525 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002526 __mask_and_edge_IO_APIC_irq(cfg);
2527 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002528 spin_unlock(&ioapic_lock);
2529 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002530#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002531}
Yinghai Lu1d025192008-08-19 20:50:34 -07002532
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002533static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002534 .name = "IO-APIC",
2535 .startup = startup_ioapic_irq,
2536 .mask = mask_IO_APIC_irq,
2537 .unmask = unmask_IO_APIC_irq,
2538 .ack = ack_apic_edge,
2539 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002540#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002541 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002542#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002543 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544};
2545
Ingo Molnar54168ed2008-08-20 09:07:45 +02002546#ifdef CONFIG_INTR_REMAP
2547static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002548 .name = "IR-IO-APIC",
2549 .startup = startup_ioapic_irq,
2550 .mask = mask_IO_APIC_irq,
2551 .unmask = unmask_IO_APIC_irq,
2552 .ack = ack_x2apic_edge,
2553 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002554#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002555 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002556#endif
2557 .retrigger = ioapic_retrigger_irq,
2558};
2559#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561static inline void init_IO_APIC_traps(void)
2562{
2563 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002564 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002565 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 /*
2568 * NOTE! The local APIC isn't very good at handling
2569 * multiple interrupts at the same interrupt level.
2570 * As the interrupt level is determined by taking the
2571 * vector number and shifting that right by 4, we
2572 * want to spread these out a bit so that they don't
2573 * all fall in the same interrupt level.
2574 *
2575 * Also, we've got to be careful not to trash gate
2576 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2577 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002578 for_each_irq_desc(irq, desc) {
2579 if (!desc)
2580 continue;
2581
2582 cfg = desc->chip_data;
2583 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 /*
2585 * Hmm.. We don't have an entry for this,
2586 * so default to an old-fashioned 8259
2587 * interrupt if we can..
2588 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002589 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002591 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002593 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 }
2595 }
2596}
2597
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002598/*
2599 * The local APIC irq-chip implementation:
2600 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002602static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
2604 unsigned long v;
2605
2606 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002607 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002610static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002612 unsigned long v;
2613
2614 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002615 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
Yinghai Lu3145e942008-12-05 18:58:34 -08002618static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002619{
2620 ack_APIC_irq();
2621}
2622
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002623static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002624 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002625 .mask = mask_lapic_irq,
2626 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002627 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628};
2629
Yinghai Lu3145e942008-12-05 18:58:34 -08002630static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002631{
Yinghai Lu08678b02008-08-19 20:50:05 -07002632 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002633 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2634 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002635}
2636
Jan Beuliche9427102008-01-30 13:31:24 +01002637static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
2639 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002640 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 * We put the 8259A master into AEOI mode and
2642 * unmask on all local APICs LVT0 as NMI.
2643 *
2644 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2645 * is from Maciej W. Rozycki - so we do not have to EOI from
2646 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2649
Jan Beuliche9427102008-01-30 13:31:24 +01002650 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 apic_printk(APIC_VERBOSE, " done.\n");
2653}
2654
2655/*
2656 * This looks a bit hackish but it's about the only one way of sending
2657 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2658 * not support the ExtINT mode, unfortunately. We need to send these
2659 * cycles as some i82489DX-based boards have glue logic that keeps the
2660 * 8259A interrupt line asserted until INTA. --macro
2661 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002662static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002664 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 struct IO_APIC_route_entry entry0, entry1;
2666 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002668 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002669 if (pin == -1) {
2670 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002672 }
2673 apic = find_isa_irq_apic(8, mp_INT);
2674 if (apic == -1) {
2675 WARN_ON_ONCE(1);
2676 return;
2677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Andi Kleencf4c6a22006-09-26 10:52:30 +02002679 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002680 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 memset(&entry1, 0, sizeof(entry1));
2683
2684 entry1.dest_mode = 0; /* physical delivery */
2685 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002686 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 entry1.delivery_mode = dest_ExtINT;
2688 entry1.polarity = entry0.polarity;
2689 entry1.trigger = 0;
2690 entry1.vector = 0;
2691
Andi Kleencf4c6a22006-09-26 10:52:30 +02002692 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
2694 save_control = CMOS_READ(RTC_CONTROL);
2695 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2696 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2697 RTC_FREQ_SELECT);
2698 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2699
2700 i = 100;
2701 while (i-- > 0) {
2702 mdelay(10);
2703 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2704 i -= 10;
2705 }
2706
2707 CMOS_WRITE(save_control, RTC_CONTROL);
2708 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002709 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Andi Kleencf4c6a22006-09-26 10:52:30 +02002711 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712}
2713
Yinghai Luefa25592008-08-19 20:50:36 -07002714static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002715/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002716static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002717{
2718 disable_timer_pin_1 = 1;
2719 return 0;
2720}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002721early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002722
2723int timer_through_8259 __initdata;
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725/*
2726 * This code may look a bit paranoid, but it's supposed to cooperate with
2727 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2728 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2729 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002730 *
2731 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002733static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Yinghai Lu3145e942008-12-05 18:58:34 -08002735 struct irq_desc *desc = irq_to_desc(0);
2736 struct irq_cfg *cfg = desc->chip_data;
2737 int cpu = boot_cpu_id;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002738 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002739 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002740 unsigned int ver;
2741 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002742
2743 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002744
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002745 ver = apic_read(APIC_LVR);
2746 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 /*
2749 * get/set the timer IRQ vector:
2750 */
2751 disable_8259A_irq(0);
Yinghai Lu3145e942008-12-05 18:58:34 -08002752 assign_irq_vector(0, cfg, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
2754 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002755 * As IRQ0 is to be enabled in the 8259A, the virtual
2756 * wire has to be disabled in the local APIC. Also
2757 * timer interrupts need to be acknowledged manually in
2758 * the 8259A for the i82489DX when using the NMI
2759 * watchdog as that APIC treats NMIs as level-triggered.
2760 * The AEOI mode will finish them in the 8259A
2761 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002763 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002765#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002766 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002767#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002769 pin1 = find_isa_irq_pin(0, mp_INT);
2770 apic1 = find_isa_irq_apic(0, mp_INT);
2771 pin2 = ioapic_i8259.pin;
2772 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002774 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2775 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002776 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002778 /*
2779 * Some BIOS writers are clueless and report the ExtINTA
2780 * I/O APIC input from the cascaded 8259A as the timer
2781 * interrupt input. So just in case, if only one pin
2782 * was found above, try it both directly and through the
2783 * 8259A.
2784 */
2785 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002786#ifdef CONFIG_INTR_REMAP
2787 if (intr_remapping_enabled)
2788 panic("BIOS bug: timer not connected to IO-APIC");
2789#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002790 pin1 = pin2;
2791 apic1 = apic2;
2792 no_pin1 = 1;
2793 } else if (pin2 == -1) {
2794 pin2 = pin1;
2795 apic2 = apic1;
2796 }
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 if (pin1 != -1) {
2799 /*
2800 * Ok, does IRQ0 through the IOAPIC work?
2801 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002802 if (no_pin1) {
Yinghai Lu3145e942008-12-05 18:58:34 -08002803 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002804 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002805 }
Yinghai Lu3145e942008-12-05 18:58:34 -08002806 unmask_IO_APIC_irq_desc(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 if (timer_irq_works()) {
2808 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 setup_nmi();
2810 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002812 if (disable_timer_pin_1 > 0)
2813 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002814 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002816#ifdef CONFIG_INTR_REMAP
2817 if (intr_remapping_enabled)
2818 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2819#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002820 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002821 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002822 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2823 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002825 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2826 "(IRQ0) through the 8259A ...\n");
2827 apic_printk(APIC_QUIET, KERN_INFO
2828 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 /*
2830 * legacy devices should be connected to IO APIC #0
2831 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002832 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002833 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Yinghai Lu3145e942008-12-05 18:58:34 -08002834 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002835 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002837 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002838 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002840 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002842 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846 /*
2847 * Cleanup, just in case ...
2848 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002849 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002850 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002851 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002855 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2856 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002857 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002859#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002860 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002861#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002863 apic_printk(APIC_QUIET, KERN_INFO
2864 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Yinghai Lu3145e942008-12-05 18:58:34 -08002866 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002867 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 enable_8259A_irq(0);
2869
2870 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002871 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002874 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002875 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002876 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002878 apic_printk(APIC_QUIET, KERN_INFO
2879 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 init_8259A(0);
2882 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002883 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 unlock_ExtINT_logic();
2886
2887 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002888 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002889 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002891 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002893 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002894out:
2895 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
2898/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002899 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2900 * to devices. However there may be an I/O APIC pin available for
2901 * this interrupt regardless. The pin may be left unconnected, but
2902 * typically it will be reused as an ExtINT cascade interrupt for
2903 * the master 8259A. In the MPS case such a pin will normally be
2904 * reported as an ExtINT interrupt in the MP table. With ACPI
2905 * there is no provision for ExtINT interrupts, and in the absence
2906 * of an override it would be treated as an ordinary ISA I/O APIC
2907 * interrupt, that is edge-triggered and unmasked by default. We
2908 * used to do this, but it caused problems on some systems because
2909 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2910 * the same ExtINT cascade interrupt to drive the local APIC of the
2911 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2912 * the I/O APIC in all cases now. No actual device should request
2913 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 */
2915#define PIC_IRQS (1 << PIC_CASCADE_IR)
2916
2917void __init setup_IO_APIC(void)
2918{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002919
2920#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002922#else
2923 /*
2924 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2925 */
2926#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002928 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Ingo Molnar54168ed2008-08-20 09:07:45 +02002930 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002931 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002932 * Set up IO-APIC IRQ routing.
2933 */
2934#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002935 if (!acpi_ioapic)
2936 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002937#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 sync_Arb_IDs();
2939 setup_IO_APIC_irqs();
2940 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002941 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942}
2943
2944/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02002945 * Called after all the initialization is done. If we didnt find any
2946 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949static int __init io_apic_bug_finalize(void)
2950{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002951 if (sis_apic_bug == -1)
2952 sis_apic_bug = 0;
2953 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
2955
2956late_initcall(io_apic_bug_finalize);
2957
2958struct sysfs_ioapic_data {
2959 struct sys_device dev;
2960 struct IO_APIC_route_entry entry[0];
2961};
Ingo Molnar54168ed2008-08-20 09:07:45 +02002962static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Pavel Machek438510f2005-04-16 15:25:24 -07002964static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965{
2966 struct IO_APIC_route_entry *entry;
2967 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 data = container_of(dev, struct sysfs_ioapic_data, dev);
2971 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002972 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2973 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 return 0;
2976}
2977
2978static int ioapic_resume(struct sys_device *dev)
2979{
2980 struct IO_APIC_route_entry *entry;
2981 struct sysfs_ioapic_data *data;
2982 unsigned long flags;
2983 union IO_APIC_reg_00 reg_00;
2984 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 data = container_of(dev, struct sysfs_ioapic_data, dev);
2987 entry = data->entry;
2988
2989 spin_lock_irqsave(&ioapic_lock, flags);
2990 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002991 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2992 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 io_apic_write(dev->id, 0, reg_00.raw);
2994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002996 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002997 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 return 0;
3000}
3001
3002static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003003 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 .suspend = ioapic_suspend,
3005 .resume = ioapic_resume,
3006};
3007
3008static int __init ioapic_init_sysfs(void)
3009{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003010 struct sys_device * dev;
3011 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 error = sysdev_class_register(&ioapic_sysdev_class);
3014 if (error)
3015 return error;
3016
Ingo Molnar54168ed2008-08-20 09:07:45 +02003017 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003018 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003020 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 if (!mp_ioapic_data[i]) {
3022 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3023 continue;
3024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003026 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 dev->cls = &ioapic_sysdev_class;
3028 error = sysdev_register(dev);
3029 if (error) {
3030 kfree(mp_ioapic_data[i]);
3031 mp_ioapic_data[i] = NULL;
3032 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3033 continue;
3034 }
3035 }
3036
3037 return 0;
3038}
3039
3040device_initcall(ioapic_init_sysfs);
3041
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003042/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003043 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003044 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003045unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003046{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003047 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003048 unsigned int irq;
3049 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003050 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003051 struct irq_cfg *cfg_new = NULL;
3052 int cpu = boot_cpu_id;
3053 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003054
3055 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003056 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lube5d5352008-12-05 18:58:33 -08003057 for (new = irq_want; new < NR_IRQS; new++) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003058 if (platform_legacy_irq(new))
3059 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003060
3061 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3062 if (!desc_new) {
3063 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003064 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003065 }
3066 cfg_new = desc_new->chip_data;
3067
3068 if (cfg_new->vector != 0)
3069 continue;
Yinghai Lu3145e942008-12-05 18:58:34 -08003070 if (__assign_irq_vector(new, cfg_new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003071 irq = new;
3072 break;
3073 }
3074 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003075
Yinghai Lu199751d2008-08-19 20:50:27 -07003076 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003077 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003078 /* restore it, in case dynamic_irq_init clear it */
3079 if (desc_new)
3080 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003081 }
3082 return irq;
3083}
3084
Yinghai Lube5d5352008-12-05 18:58:33 -08003085static int nr_irqs_gsi = NR_IRQS_LEGACY;
Yinghai Lu199751d2008-08-19 20:50:27 -07003086int create_irq(void)
3087{
Yinghai Lube5d5352008-12-05 18:58:33 -08003088 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003089 int irq;
3090
Yinghai Lube5d5352008-12-05 18:58:33 -08003091 irq_want = nr_irqs_gsi;
3092 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003093
3094 if (irq == 0)
3095 irq = -1;
3096
3097 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003098}
3099
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003100void destroy_irq(unsigned int irq)
3101{
3102 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003103 struct irq_cfg *cfg;
3104 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003105
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003106 /* store it, in case dynamic_irq_cleanup clear it */
3107 desc = irq_to_desc(irq);
3108 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003109 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003110 /* connect back irq_cfg */
3111 if (desc)
3112 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003113
Ingo Molnar54168ed2008-08-20 09:07:45 +02003114#ifdef CONFIG_INTR_REMAP
3115 free_irte(irq);
3116#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003117 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003118 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003119 spin_unlock_irqrestore(&vector_lock, flags);
3120}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003121
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003122/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003123 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003124 */
3125#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003126static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003127{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003128 struct irq_cfg *cfg;
3129 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003130 unsigned dest;
3131
Yinghai Lu3145e942008-12-05 18:58:34 -08003132 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08003133 err = assign_irq_vector(irq, cfg, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003134 if (err)
3135 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003136
Mike Travis22f65d32008-12-16 17:33:56 -08003137 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003138
Ingo Molnar54168ed2008-08-20 09:07:45 +02003139#ifdef CONFIG_INTR_REMAP
3140 if (irq_remapped(irq)) {
3141 struct irte irte;
3142 int ir_index;
3143 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003144
Ingo Molnar54168ed2008-08-20 09:07:45 +02003145 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3146 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003147
Ingo Molnar54168ed2008-08-20 09:07:45 +02003148 memset (&irte, 0, sizeof(irte));
3149
3150 irte.present = 1;
3151 irte.dst_mode = INT_DEST_MODE;
3152 irte.trigger_mode = 0; /* edge */
3153 irte.dlvry_mode = INT_DELIVERY_MODE;
3154 irte.vector = cfg->vector;
3155 irte.dest_id = IRTE_DEST(dest);
3156
3157 modify_irte(irq, &irte);
3158
3159 msg->address_hi = MSI_ADDR_BASE_HI;
3160 msg->data = sub_handle;
3161 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3162 MSI_ADDR_IR_SHV |
3163 MSI_ADDR_IR_INDEX1(ir_index) |
3164 MSI_ADDR_IR_INDEX2(ir_index);
3165 } else
3166#endif
3167 {
3168 msg->address_hi = MSI_ADDR_BASE_HI;
3169 msg->address_lo =
3170 MSI_ADDR_BASE_LO |
3171 ((INT_DEST_MODE == 0) ?
3172 MSI_ADDR_DEST_MODE_PHYSICAL:
3173 MSI_ADDR_DEST_MODE_LOGICAL) |
3174 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3175 MSI_ADDR_REDIRECTION_CPU:
3176 MSI_ADDR_REDIRECTION_LOWPRI) |
3177 MSI_ADDR_DEST_ID(dest);
3178
3179 msg->data =
3180 MSI_DATA_TRIGGER_EDGE |
3181 MSI_DATA_LEVEL_ASSERT |
3182 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3183 MSI_DATA_DELIVERY_FIXED:
3184 MSI_DATA_DELIVERY_LOWPRI) |
3185 MSI_DATA_VECTOR(cfg->vector);
3186 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003187 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003188}
3189
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003190#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303191static void set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003192{
Yinghai Lu3145e942008-12-05 18:58:34 -08003193 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003194 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003195 struct msi_msg msg;
3196 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003197
Mike Travis22f65d32008-12-16 17:33:56 -08003198 dest = set_desc_affinity(desc, mask);
3199 if (dest == BAD_APICID)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003200 return;
3201
Yinghai Lu3145e942008-12-05 18:58:34 -08003202 cfg = desc->chip_data;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003203
Yinghai Lu3145e942008-12-05 18:58:34 -08003204 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003205
3206 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003207 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003208 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3209 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3210
Yinghai Lu3145e942008-12-05 18:58:34 -08003211 write_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003212}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003213#ifdef CONFIG_INTR_REMAP
3214/*
3215 * Migrate the MSI irq to another cpumask. This migration is
3216 * done in the process context using interrupt-remapping hardware.
3217 */
Mike Travise7986732008-12-16 17:33:52 -08003218static void
3219ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003220{
Yinghai Lu3145e942008-12-05 18:58:34 -08003221 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003222 struct irq_cfg *cfg;
3223 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003224 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003225
Ingo Molnar54168ed2008-08-20 09:07:45 +02003226 if (get_irte(irq, &irte))
3227 return;
3228
Mike Travis22f65d32008-12-16 17:33:56 -08003229 dest = set_desc_affinity(desc, mask);
3230 if (dest == BAD_APICID)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003231 return;
3232
Ingo Molnar54168ed2008-08-20 09:07:45 +02003233 irte.vector = cfg->vector;
3234 irte.dest_id = IRTE_DEST(dest);
3235
3236 /*
3237 * atomically update the IRTE with the new destination and vector.
3238 */
3239 modify_irte(irq, &irte);
3240
3241 /*
3242 * After this point, all the interrupts will start arriving
3243 * at the new destination. So, time to cleanup the previous
3244 * vector allocation.
3245 */
Mike Travis22f65d32008-12-16 17:33:56 -08003246 if (cfg->move_in_progress)
3247 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003248}
Yinghai Lu3145e942008-12-05 18:58:34 -08003249
Ingo Molnar54168ed2008-08-20 09:07:45 +02003250#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003251#endif /* CONFIG_SMP */
3252
3253/*
3254 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3255 * which implement the MSI or MSI-X Capability Structure.
3256 */
3257static struct irq_chip msi_chip = {
3258 .name = "PCI-MSI",
3259 .unmask = unmask_msi_irq,
3260 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003261 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003262#ifdef CONFIG_SMP
3263 .set_affinity = set_msi_irq_affinity,
3264#endif
3265 .retrigger = ioapic_retrigger_irq,
3266};
3267
Ingo Molnar54168ed2008-08-20 09:07:45 +02003268#ifdef CONFIG_INTR_REMAP
3269static struct irq_chip msi_ir_chip = {
3270 .name = "IR-PCI-MSI",
3271 .unmask = unmask_msi_irq,
3272 .mask = mask_msi_irq,
3273 .ack = ack_x2apic_edge,
3274#ifdef CONFIG_SMP
3275 .set_affinity = ir_set_msi_irq_affinity,
3276#endif
3277 .retrigger = ioapic_retrigger_irq,
3278};
3279
3280/*
3281 * Map the PCI dev to the corresponding remapping hardware unit
3282 * and allocate 'nvec' consecutive interrupt-remapping table entries
3283 * in it.
3284 */
3285static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3286{
3287 struct intel_iommu *iommu;
3288 int index;
3289
3290 iommu = map_dev_to_ir(dev);
3291 if (!iommu) {
3292 printk(KERN_ERR
3293 "Unable to map PCI %s to iommu\n", pci_name(dev));
3294 return -ENOENT;
3295 }
3296
3297 index = alloc_irte(iommu, irq, nvec);
3298 if (index < 0) {
3299 printk(KERN_ERR
3300 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003301 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003302 return -ENOSPC;
3303 }
3304 return index;
3305}
3306#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003307
Yinghai Lu3145e942008-12-05 18:58:34 -08003308static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003309{
3310 int ret;
3311 struct msi_msg msg;
3312
3313 ret = msi_compose_msg(dev, irq, &msg);
3314 if (ret < 0)
3315 return ret;
3316
Yinghai Lu3145e942008-12-05 18:58:34 -08003317 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003318 write_msi_msg(irq, &msg);
3319
Ingo Molnar54168ed2008-08-20 09:07:45 +02003320#ifdef CONFIG_INTR_REMAP
3321 if (irq_remapped(irq)) {
3322 struct irq_desc *desc = irq_to_desc(irq);
3323 /*
3324 * irq migration in process context
3325 */
3326 desc->status |= IRQ_MOVE_PCNTXT;
3327 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3328 } else
3329#endif
3330 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003331
Yinghai Luc81bba42008-09-25 11:53:11 -07003332 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3333
Yinghai Lu1d025192008-08-19 20:50:34 -07003334 return 0;
3335}
3336
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003337int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003338{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003339 unsigned int irq;
3340 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003341 unsigned int irq_want;
3342
Yinghai Lube5d5352008-12-05 18:58:33 -08003343 irq_want = nr_irqs_gsi;
Yinghai Lu199751d2008-08-19 20:50:27 -07003344 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003345 if (irq == 0)
3346 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003347
Ingo Molnar54168ed2008-08-20 09:07:45 +02003348#ifdef CONFIG_INTR_REMAP
3349 if (!intr_remapping_enabled)
3350 goto no_ir;
3351
3352 ret = msi_alloc_irte(dev, irq, 1);
3353 if (ret < 0)
3354 goto error;
3355no_ir:
3356#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003357 ret = setup_msi_irq(dev, msidesc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003358 if (ret < 0) {
3359 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003360 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003361 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003362 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003363
3364#ifdef CONFIG_INTR_REMAP
3365error:
3366 destroy_irq(irq);
3367 return ret;
3368#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003369}
3370
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003371int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3372{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003373 unsigned int irq;
3374 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003375 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003376 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003377
Ingo Molnar54168ed2008-08-20 09:07:45 +02003378#ifdef CONFIG_INTR_REMAP
3379 struct intel_iommu *iommu = 0;
3380 int index = 0;
3381#endif
3382
Yinghai Lube5d5352008-12-05 18:58:33 -08003383 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003384 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003385 list_for_each_entry(msidesc, &dev->msi_list, list) {
3386 irq = create_irq_nr(irq_want);
Yinghai Lube5d5352008-12-05 18:58:33 -08003387 irq_want++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003388 if (irq == 0)
3389 return -1;
3390#ifdef CONFIG_INTR_REMAP
3391 if (!intr_remapping_enabled)
3392 goto no_ir;
3393
3394 if (!sub_handle) {
3395 /*
3396 * allocate the consecutive block of IRTE's
3397 * for 'nvec'
3398 */
3399 index = msi_alloc_irte(dev, irq, nvec);
3400 if (index < 0) {
3401 ret = index;
3402 goto error;
3403 }
3404 } else {
3405 iommu = map_dev_to_ir(dev);
3406 if (!iommu) {
3407 ret = -ENOENT;
3408 goto error;
3409 }
3410 /*
3411 * setup the mapping between the irq and the IRTE
3412 * base index, the sub_handle pointing to the
3413 * appropriate interrupt remap table entry.
3414 */
3415 set_irte_irq(irq, iommu, index, sub_handle);
3416 }
3417no_ir:
3418#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003419 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003420 if (ret < 0)
3421 goto error;
3422 sub_handle++;
3423 }
3424 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003425
3426error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003427 destroy_irq(irq);
3428 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003429}
3430
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003431void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003432{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003433 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003434}
3435
Ingo Molnar54168ed2008-08-20 09:07:45 +02003436#ifdef CONFIG_DMAR
3437#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -08003438static void dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003439{
Yinghai Lu3145e942008-12-05 18:58:34 -08003440 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003441 struct irq_cfg *cfg;
3442 struct msi_msg msg;
3443 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003444
Mike Travis22f65d32008-12-16 17:33:56 -08003445 dest = set_desc_affinity(desc, mask);
3446 if (dest == BAD_APICID)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003447 return;
3448
Yinghai Lu3145e942008-12-05 18:58:34 -08003449 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003450
3451 dmar_msi_read(irq, &msg);
3452
3453 msg.data &= ~MSI_DATA_VECTOR_MASK;
3454 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3455 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3456 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3457
3458 dmar_msi_write(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003459}
Yinghai Lu3145e942008-12-05 18:58:34 -08003460
Ingo Molnar54168ed2008-08-20 09:07:45 +02003461#endif /* CONFIG_SMP */
3462
3463struct irq_chip dmar_msi_type = {
3464 .name = "DMAR_MSI",
3465 .unmask = dmar_msi_unmask,
3466 .mask = dmar_msi_mask,
3467 .ack = ack_apic_edge,
3468#ifdef CONFIG_SMP
3469 .set_affinity = dmar_msi_set_affinity,
3470#endif
3471 .retrigger = ioapic_retrigger_irq,
3472};
3473
3474int arch_setup_dmar_msi(unsigned int irq)
3475{
3476 int ret;
3477 struct msi_msg msg;
3478
3479 ret = msi_compose_msg(NULL, irq, &msg);
3480 if (ret < 0)
3481 return ret;
3482 dmar_msi_write(irq, &msg);
3483 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3484 "edge");
3485 return 0;
3486}
3487#endif
3488
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003489#ifdef CONFIG_HPET_TIMER
3490
3491#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -08003492static void hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003493{
Yinghai Lu3145e942008-12-05 18:58:34 -08003494 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003495 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003496 struct msi_msg msg;
3497 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003498
Mike Travis22f65d32008-12-16 17:33:56 -08003499 dest = set_desc_affinity(desc, mask);
3500 if (dest == BAD_APICID)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003501 return;
3502
Yinghai Lu3145e942008-12-05 18:58:34 -08003503 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003504
3505 hpet_msi_read(irq, &msg);
3506
3507 msg.data &= ~MSI_DATA_VECTOR_MASK;
3508 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3509 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3510 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3511
3512 hpet_msi_write(irq, &msg);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003513}
Yinghai Lu3145e942008-12-05 18:58:34 -08003514
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003515#endif /* CONFIG_SMP */
3516
3517struct irq_chip hpet_msi_type = {
3518 .name = "HPET_MSI",
3519 .unmask = hpet_msi_unmask,
3520 .mask = hpet_msi_mask,
3521 .ack = ack_apic_edge,
3522#ifdef CONFIG_SMP
3523 .set_affinity = hpet_msi_set_affinity,
3524#endif
3525 .retrigger = ioapic_retrigger_irq,
3526};
3527
3528int arch_setup_hpet_msi(unsigned int irq)
3529{
3530 int ret;
3531 struct msi_msg msg;
3532
3533 ret = msi_compose_msg(NULL, irq, &msg);
3534 if (ret < 0)
3535 return ret;
3536
3537 hpet_msi_write(irq, &msg);
3538 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3539 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003540
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003541 return 0;
3542}
3543#endif
3544
Ingo Molnar54168ed2008-08-20 09:07:45 +02003545#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003546/*
3547 * Hypertransport interrupt support
3548 */
3549#ifdef CONFIG_HT_IRQ
3550
3551#ifdef CONFIG_SMP
3552
Yinghai Lu497c9a12008-08-19 20:50:28 -07003553static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003554{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003555 struct ht_irq_msg msg;
3556 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003557
Yinghai Lu497c9a12008-08-19 20:50:28 -07003558 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003559 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003560
Yinghai Lu497c9a12008-08-19 20:50:28 -07003561 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003562 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003563
Eric W. Biedermanec683072006-11-08 17:44:57 -08003564 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003565}
3566
Mike Travis22f65d32008-12-16 17:33:56 -08003567static void set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003568{
Yinghai Lu3145e942008-12-05 18:58:34 -08003569 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003570 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003571 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003572
Mike Travis22f65d32008-12-16 17:33:56 -08003573 dest = set_desc_affinity(desc, mask);
3574 if (dest == BAD_APICID)
Yinghai Lu497c9a12008-08-19 20:50:28 -07003575 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003576
Yinghai Lu3145e942008-12-05 18:58:34 -08003577 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003578
Yinghai Lu497c9a12008-08-19 20:50:28 -07003579 target_ht_irq(irq, dest, cfg->vector);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003580}
Yinghai Lu3145e942008-12-05 18:58:34 -08003581
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003582#endif
3583
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003584static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003585 .name = "PCI-HT",
3586 .mask = mask_ht_irq,
3587 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003588 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003589#ifdef CONFIG_SMP
3590 .set_affinity = set_ht_irq_affinity,
3591#endif
3592 .retrigger = ioapic_retrigger_irq,
3593};
3594
3595int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3596{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003597 struct irq_cfg *cfg;
3598 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003599
Yinghai Lu3145e942008-12-05 18:58:34 -08003600 cfg = irq_cfg(irq);
Mike Travise7986732008-12-16 17:33:52 -08003601 err = assign_irq_vector(irq, cfg, TARGET_CPUS);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003602 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003603 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003604 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003605
Mike Travis22f65d32008-12-16 17:33:56 -08003606 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003607
Eric W. Biedermanec683072006-11-08 17:44:57 -08003608 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003609
Eric W. Biedermanec683072006-11-08 17:44:57 -08003610 msg.address_lo =
3611 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003612 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003613 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003614 ((INT_DEST_MODE == 0) ?
3615 HT_IRQ_LOW_DM_PHYSICAL :
3616 HT_IRQ_LOW_DM_LOGICAL) |
3617 HT_IRQ_LOW_RQEOI_EDGE |
3618 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3619 HT_IRQ_LOW_MT_FIXED :
3620 HT_IRQ_LOW_MT_ARBITRATED) |
3621 HT_IRQ_LOW_IRQ_MASKED;
3622
Eric W. Biedermanec683072006-11-08 17:44:57 -08003623 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003624
Ingo Molnara460e742006-10-17 00:10:03 -07003625 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3626 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003627
3628 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003629 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003630 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003631}
3632#endif /* CONFIG_HT_IRQ */
3633
Dean Nelson4173a0e2008-10-02 12:18:21 -05003634#ifdef CONFIG_X86_64
3635/*
3636 * Re-target the irq to the specified CPU and enable the specified MMR located
3637 * on the specified blade to allow the sending of MSIs to the specified CPU.
3638 */
3639int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3640 unsigned long mmr_offset)
3641{
Mike Travis22f65d32008-12-16 17:33:56 -08003642 const struct cpumask *eligible_cpu = cpumask_of(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003643 struct irq_cfg *cfg;
3644 int mmr_pnode;
3645 unsigned long mmr_value;
3646 struct uv_IO_APIC_route_entry *entry;
3647 unsigned long flags;
3648 int err;
3649
Yinghai Lu3145e942008-12-05 18:58:34 -08003650 cfg = irq_cfg(irq);
3651
Mike Travise7986732008-12-16 17:33:52 -08003652 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003653 if (err != 0)
3654 return err;
3655
3656 spin_lock_irqsave(&vector_lock, flags);
3657 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3658 irq_name);
3659 spin_unlock_irqrestore(&vector_lock, flags);
3660
Dean Nelson4173a0e2008-10-02 12:18:21 -05003661 mmr_value = 0;
3662 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3663 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3664
3665 entry->vector = cfg->vector;
3666 entry->delivery_mode = INT_DELIVERY_MODE;
3667 entry->dest_mode = INT_DEST_MODE;
3668 entry->polarity = 0;
3669 entry->trigger = 0;
3670 entry->mask = 0;
Mike Travise7986732008-12-16 17:33:52 -08003671 entry->dest = cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003672
3673 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3674 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3675
3676 return irq;
3677}
3678
3679/*
3680 * Disable the specified MMR located on the specified blade so that MSIs are
3681 * longer allowed to be sent.
3682 */
3683void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3684{
3685 unsigned long mmr_value;
3686 struct uv_IO_APIC_route_entry *entry;
3687 int mmr_pnode;
3688
3689 mmr_value = 0;
3690 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3691 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3692
3693 entry->mask = 1;
3694
3695 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3696 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3697}
3698#endif /* CONFIG_X86_64 */
3699
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003700int __init io_apic_get_redir_entries (int ioapic)
3701{
3702 union IO_APIC_reg_01 reg_01;
3703 unsigned long flags;
3704
3705 spin_lock_irqsave(&ioapic_lock, flags);
3706 reg_01.raw = io_apic_read(ioapic, 1);
3707 spin_unlock_irqrestore(&ioapic_lock, flags);
3708
3709 return reg_01.bits.entries;
3710}
3711
Yinghai Lube5d5352008-12-05 18:58:33 -08003712void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003713{
Yinghai Lube5d5352008-12-05 18:58:33 -08003714 int idx;
3715 int nr = 0;
3716
3717 for (idx = 0; idx < nr_ioapics; idx++)
3718 nr += io_apic_get_redir_entries(idx) + 1;
3719
3720 if (nr > nr_irqs_gsi)
3721 nr_irqs_gsi = nr;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003722}
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003725 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 -------------------------------------------------------------------------- */
3727
Len Brown888ba6c2005-08-24 12:07:20 -04003728#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Ingo Molnar54168ed2008-08-20 09:07:45 +02003730#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003731int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
3733 union IO_APIC_reg_00 reg_00;
3734 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3735 physid_mask_t tmp;
3736 unsigned long flags;
3737 int i = 0;
3738
3739 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003740 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3741 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003743 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3745 * advantage of new APIC bus architecture.
3746 */
3747
3748 if (physids_empty(apic_id_map))
3749 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3750
3751 spin_lock_irqsave(&ioapic_lock, flags);
3752 reg_00.raw = io_apic_read(ioapic, 0);
3753 spin_unlock_irqrestore(&ioapic_lock, flags);
3754
3755 if (apic_id >= get_physical_broadcast()) {
3756 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3757 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3758 apic_id = reg_00.bits.ID;
3759 }
3760
3761 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003762 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 * 'stuck on smp_invalidate_needed IPI wait' messages.
3764 */
3765 if (check_apicid_used(apic_id_map, apic_id)) {
3766
3767 for (i = 0; i < get_physical_broadcast(); i++) {
3768 if (!check_apicid_used(apic_id_map, i))
3769 break;
3770 }
3771
3772 if (i == get_physical_broadcast())
3773 panic("Max apic_id exceeded!\n");
3774
3775 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3776 "trying %d\n", ioapic, apic_id, i);
3777
3778 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
3781 tmp = apicid_to_cpu_present(apic_id);
3782 physids_or(apic_id_map, apic_id_map, tmp);
3783
3784 if (reg_00.bits.ID != apic_id) {
3785 reg_00.bits.ID = apic_id;
3786
3787 spin_lock_irqsave(&ioapic_lock, flags);
3788 io_apic_write(ioapic, 0, reg_00.raw);
3789 reg_00.raw = io_apic_read(ioapic, 0);
3790 spin_unlock_irqrestore(&ioapic_lock, flags);
3791
3792 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003793 if (reg_00.bits.ID != apic_id) {
3794 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3795 return -1;
3796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
3798
3799 apic_printk(APIC_VERBOSE, KERN_INFO
3800 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3801
3802 return apic_id;
3803}
3804
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003805int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
3807 union IO_APIC_reg_01 reg_01;
3808 unsigned long flags;
3809
3810 spin_lock_irqsave(&ioapic_lock, flags);
3811 reg_01.raw = io_apic_read(ioapic, 1);
3812 spin_unlock_irqrestore(&ioapic_lock, flags);
3813
3814 return reg_01.bits.version;
3815}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003816#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Ingo Molnar54168ed2008-08-20 09:07:45 +02003818int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003820 struct irq_desc *desc;
3821 struct irq_cfg *cfg;
3822 int cpu = boot_cpu_id;
3823
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003825 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 ioapic);
3827 return -EINVAL;
3828 }
3829
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003830 desc = irq_to_desc_alloc_cpu(irq, cpu);
3831 if (!desc) {
3832 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3833 return 0;
3834 }
3835
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 * IRQs < 16 are already in the irq_2_pin[] map
3838 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003839 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003840 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003841 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Yinghai Lu3145e942008-12-05 18:58:34 -08003844 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
3846 return 0;
3847}
3848
Ingo Molnar54168ed2008-08-20 09:07:45 +02003849
Shaohua Li61fd47e2007-11-17 01:05:28 -05003850int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3851{
3852 int i;
3853
3854 if (skip_ioapic_setup)
3855 return -1;
3856
3857 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003858 if (mp_irqs[i].mp_irqtype == mp_INT &&
3859 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003860 break;
3861 if (i >= mp_irq_entries)
3862 return -1;
3863
3864 *trigger = irq_trigger(i);
3865 *polarity = irq_polarity(i);
3866 return 0;
3867}
3868
Len Brown888ba6c2005-08-24 12:07:20 -04003869#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02003870
Yinghai Lu497c9a12008-08-19 20:50:28 -07003871/*
3872 * This function currently is only a helper for the i386 smp boot process where
3873 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3874 * so mask in all cases should simply be TARGET_CPUS
3875 */
3876#ifdef CONFIG_SMP
3877void __init setup_ioapic_dest(void)
3878{
3879 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003880 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003881 struct irq_cfg *cfg;
Mike Travis22f65d32008-12-16 17:33:56 -08003882 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003883
3884 if (skip_ioapic_setup == 1)
3885 return;
3886
3887 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3888 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3889 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3890 if (irq_entry == -1)
3891 continue;
3892 irq = pin_2_irq(irq_entry, ioapic, pin);
3893
3894 /* setup_IO_APIC_irqs could fail to get vector for some device
3895 * when you have too many devices, because at that time only boot
3896 * cpu is online.
3897 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003898 desc = irq_to_desc(irq);
3899 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003900 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08003901 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07003902 irq_trigger(irq_entry),
3903 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003904 continue;
3905
3906 }
3907
3908 /*
3909 * Honour affinities which have been set in early boot
3910 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003911 if (desc->status &
3912 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
Mike Travise7986732008-12-16 17:33:52 -08003913 mask = &desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003914 else
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003915 mask = TARGET_CPUS;
3916
3917#ifdef CONFIG_INTR_REMAP
3918 if (intr_remapping_enabled)
Mike Travise7986732008-12-16 17:33:52 -08003919 set_ir_ioapic_affinity_irq_desc(desc, mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01003920 else
3921#endif
Mike Travise7986732008-12-16 17:33:52 -08003922 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003923 }
3924
3925 }
3926}
3927#endif
3928
Ingo Molnar54168ed2008-08-20 09:07:45 +02003929#define IOAPIC_RESOURCE_NAME_SIZE 11
3930
3931static struct resource *ioapic_resources;
3932
3933static struct resource * __init ioapic_setup_resources(void)
3934{
3935 unsigned long n;
3936 struct resource *res;
3937 char *mem;
3938 int i;
3939
3940 if (nr_ioapics <= 0)
3941 return NULL;
3942
3943 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3944 n *= nr_ioapics;
3945
3946 mem = alloc_bootmem(n);
3947 res = (void *)mem;
3948
3949 if (mem != NULL) {
3950 mem += sizeof(struct resource) * nr_ioapics;
3951
3952 for (i = 0; i < nr_ioapics; i++) {
3953 res[i].name = mem;
3954 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3955 sprintf(mem, "IOAPIC %u", i);
3956 mem += IOAPIC_RESOURCE_NAME_SIZE;
3957 }
3958 }
3959
3960 ioapic_resources = res;
3961
3962 return res;
3963}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003964
Yinghai Luf3294a32008-06-27 01:41:56 -07003965void __init ioapic_init_mappings(void)
3966{
3967 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003968 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003969 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07003970
Ingo Molnar54168ed2008-08-20 09:07:45 +02003971 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07003972 for (i = 0; i < nr_ioapics; i++) {
3973 if (smp_found_config) {
3974 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003975#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003976 if (!ioapic_phys) {
3977 printk(KERN_ERR
3978 "WARNING: bogus zero IO-APIC "
3979 "address found in MPTABLE, "
3980 "disabling IO/APIC support!\n");
3981 smp_found_config = 0;
3982 skip_ioapic_setup = 1;
3983 goto fake_ioapic_page;
3984 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02003985#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003986 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003987#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07003988fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003989#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07003990 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003991 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07003992 ioapic_phys = __pa(ioapic_phys);
3993 }
3994 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003995 apic_printk(APIC_VERBOSE,
3996 "mapped IOAPIC to %08lx (%08lx)\n",
3997 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07003998 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003999
Ingo Molnar54168ed2008-08-20 09:07:45 +02004000 if (ioapic_res != NULL) {
4001 ioapic_res->start = ioapic_phys;
4002 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4003 ioapic_res++;
4004 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004005 }
4006}
4007
Ingo Molnar54168ed2008-08-20 09:07:45 +02004008static int __init ioapic_insert_resources(void)
4009{
4010 int i;
4011 struct resource *r = ioapic_resources;
4012
4013 if (!r) {
4014 printk(KERN_ERR
4015 "IO APIC resources could be not be allocated.\n");
4016 return -1;
4017 }
4018
4019 for (i = 0; i < nr_ioapics; i++) {
4020 insert_resource(&iomem_resource, r);
4021 r++;
4022 }
4023
4024 return 0;
4025}
4026
4027/* Insert the IO APIC resources after PCI initialization has occured to handle
4028 * IO APICS that are mapped in on a BAR in PCI space. */
4029late_initcall(ioapic_insert_resources);