blob: 40747e58f3056e78f7228bcad6ae9eb550388bc8 [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);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700132
Yinghai Lu0f978f42008-08-19 20:50:26 -0700133 return pin;
134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800136struct irq_cfg {
137 struct irq_pin_list *irq_2_pin;
Mike Travis22f65d32008-12-16 17:33:56 -0800138 cpumask_var_t domain;
139 cpumask_var_t old_domain;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800140 unsigned move_cleanup_count;
141 u8 vector;
142 u8 move_in_progress : 1;
Yinghai Lu48a1b102008-12-11 00:15:01 -0800143#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
144 u8 move_desc_pending : 1;
145#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800146};
147
148/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
149#ifdef CONFIG_SPARSE_IRQ
150static struct irq_cfg irq_cfgx[] = {
151#else
152static struct irq_cfg irq_cfgx[NR_IRQS] = {
153#endif
Mike Travis22f65d32008-12-16 17:33:56 -0800154 [0] = { .vector = IRQ0_VECTOR, },
155 [1] = { .vector = IRQ1_VECTOR, },
156 [2] = { .vector = IRQ2_VECTOR, },
157 [3] = { .vector = IRQ3_VECTOR, },
158 [4] = { .vector = IRQ4_VECTOR, },
159 [5] = { .vector = IRQ5_VECTOR, },
160 [6] = { .vector = IRQ6_VECTOR, },
161 [7] = { .vector = IRQ7_VECTOR, },
162 [8] = { .vector = IRQ8_VECTOR, },
163 [9] = { .vector = IRQ9_VECTOR, },
164 [10] = { .vector = IRQ10_VECTOR, },
165 [11] = { .vector = IRQ11_VECTOR, },
166 [12] = { .vector = IRQ12_VECTOR, },
167 [13] = { .vector = IRQ13_VECTOR, },
168 [14] = { .vector = IRQ14_VECTOR, },
169 [15] = { .vector = IRQ15_VECTOR, },
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800170};
171
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800172int __init arch_early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800173{
174 struct irq_cfg *cfg;
175 struct irq_desc *desc;
176 int count;
177 int i;
178
179 cfg = irq_cfgx;
180 count = ARRAY_SIZE(irq_cfgx);
181
182 for (i = 0; i < count; i++) {
183 desc = irq_to_desc(i);
184 desc->chip_data = &cfg[i];
Mike Travis22f65d32008-12-16 17:33:56 -0800185 alloc_bootmem_cpumask_var(&cfg[i].domain);
186 alloc_bootmem_cpumask_var(&cfg[i].old_domain);
187 if (i < NR_IRQS_LEGACY)
188 cpumask_setall(cfg[i].domain);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800189 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800190
191 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800192}
193
194#ifdef CONFIG_SPARSE_IRQ
195static struct irq_cfg *irq_cfg(unsigned int irq)
196{
197 struct irq_cfg *cfg = NULL;
198 struct irq_desc *desc;
199
200 desc = irq_to_desc(irq);
201 if (desc)
202 cfg = desc->chip_data;
203
204 return cfg;
205}
206
207static struct irq_cfg *get_one_free_irq_cfg(int cpu)
208{
209 struct irq_cfg *cfg;
210 int node;
211
212 node = cpu_to_node(cpu);
213
214 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
Mike Travis22f65d32008-12-16 17:33:56 -0800215 if (cfg) {
Mike Travis80855f72008-12-31 18:08:47 -0800216 if (!alloc_cpumask_var_node(&cfg->domain, GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800217 kfree(cfg);
218 cfg = NULL;
Mike Travis80855f72008-12-31 18:08:47 -0800219 } else if (!alloc_cpumask_var_node(&cfg->old_domain,
220 GFP_ATOMIC, node)) {
Mike Travis22f65d32008-12-16 17:33:56 -0800221 free_cpumask_var(cfg->domain);
222 kfree(cfg);
223 cfg = NULL;
224 } else {
225 cpumask_clear(cfg->domain);
226 cpumask_clear(cfg->old_domain);
227 }
228 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800229
230 return cfg;
231}
232
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800233int arch_init_chip_data(struct irq_desc *desc, int cpu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800234{
235 struct irq_cfg *cfg;
236
237 cfg = desc->chip_data;
238 if (!cfg) {
239 desc->chip_data = get_one_free_irq_cfg(cpu);
240 if (!desc->chip_data) {
241 printk(KERN_ERR "can not alloc irq_cfg\n");
242 BUG_ON(1);
243 }
244 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800245
246 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800247}
248
Yinghai Lu48a1b102008-12-11 00:15:01 -0800249#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
250
251static void
252init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int cpu)
253{
254 struct irq_pin_list *old_entry, *head, *tail, *entry;
255
256 cfg->irq_2_pin = NULL;
257 old_entry = old_cfg->irq_2_pin;
258 if (!old_entry)
259 return;
260
261 entry = get_one_free_irq_2_pin(cpu);
262 if (!entry)
263 return;
264
265 entry->apic = old_entry->apic;
266 entry->pin = old_entry->pin;
267 head = entry;
268 tail = entry;
269 old_entry = old_entry->next;
270 while (old_entry) {
271 entry = get_one_free_irq_2_pin(cpu);
272 if (!entry) {
273 entry = head;
274 while (entry) {
275 head = entry->next;
276 kfree(entry);
277 entry = head;
278 }
279 /* still use the old one */
280 return;
281 }
282 entry->apic = old_entry->apic;
283 entry->pin = old_entry->pin;
284 tail->next = entry;
285 tail = entry;
286 old_entry = old_entry->next;
287 }
288
289 tail->next = NULL;
290 cfg->irq_2_pin = head;
291}
292
293static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
294{
295 struct irq_pin_list *entry, *next;
296
297 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
298 return;
299
300 entry = old_cfg->irq_2_pin;
301
302 while (entry) {
303 next = entry->next;
304 kfree(entry);
305 entry = next;
306 }
307 old_cfg->irq_2_pin = NULL;
308}
309
310void arch_init_copy_chip_data(struct irq_desc *old_desc,
311 struct irq_desc *desc, int cpu)
312{
313 struct irq_cfg *cfg;
314 struct irq_cfg *old_cfg;
315
316 cfg = get_one_free_irq_cfg(cpu);
317
318 if (!cfg)
319 return;
320
321 desc->chip_data = cfg;
322
323 old_cfg = old_desc->chip_data;
324
325 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
326
327 init_copy_irq_2_pin(old_cfg, cfg, cpu);
328}
329
330static void free_irq_cfg(struct irq_cfg *old_cfg)
331{
332 kfree(old_cfg);
333}
334
335void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
336{
337 struct irq_cfg *old_cfg, *cfg;
338
339 old_cfg = old_desc->chip_data;
340 cfg = desc->chip_data;
341
342 if (old_cfg == cfg)
343 return;
344
345 if (old_cfg) {
346 free_irq_2_pin(old_cfg, cfg);
347 free_irq_cfg(old_cfg);
348 old_desc->chip_data = NULL;
349 }
350}
351
Ingo Molnard733e002008-12-17 13:35:51 +0100352static void
353set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu48a1b102008-12-11 00:15:01 -0800354{
355 struct irq_cfg *cfg = desc->chip_data;
356
357 if (!cfg->move_in_progress) {
358 /* it means that domain is not changed */
Ingo Molnard733e002008-12-17 13:35:51 +0100359 if (!cpumask_intersects(&desc->affinity, mask))
Yinghai Lu48a1b102008-12-11 00:15:01 -0800360 cfg->move_desc_pending = 1;
361 }
362}
363#endif
364
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800365#else
366static struct irq_cfg *irq_cfg(unsigned int irq)
367{
368 return irq < nr_irqs ? irq_cfgx + irq : NULL;
369}
370
371#endif
372
Yinghai Lu48a1b102008-12-11 00:15:01 -0800373#ifndef CONFIG_NUMA_MIGRATE_IRQ_DESC
Mike Travise7986732008-12-16 17:33:52 -0800374static inline void
375set_extra_move_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800376{
377}
Yinghai Lu48a1b102008-12-11 00:15:01 -0800378#endif
Yinghai Lu3145e942008-12-05 18:58:34 -0800379
Linus Torvalds130fe052006-11-01 09:11:00 -0800380struct io_apic {
381 unsigned int index;
382 unsigned int unused[3];
383 unsigned int data;
384};
385
386static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
387{
388 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400389 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800390}
391
392static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
393{
394 struct io_apic __iomem *io_apic = io_apic_base(apic);
395 writel(reg, &io_apic->index);
396 return readl(&io_apic->data);
397}
398
399static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
400{
401 struct io_apic __iomem *io_apic = io_apic_base(apic);
402 writel(reg, &io_apic->index);
403 writel(value, &io_apic->data);
404}
405
406/*
407 * Re-write a value: to be used for read-modify-write
408 * cycles where the read already set up the index register.
409 *
410 * Older SiS APIC requires we rewrite the index register
411 */
412static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
413{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200414 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200415
416 if (sis_apic_bug)
417 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800418 writel(value, &io_apic->data);
419}
420
Yinghai Lu3145e942008-12-05 18:58:34 -0800421static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700422{
423 struct irq_pin_list *entry;
424 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700425
426 spin_lock_irqsave(&ioapic_lock, flags);
427 entry = cfg->irq_2_pin;
428 for (;;) {
429 unsigned int reg;
430 int pin;
431
432 if (!entry)
433 break;
434 pin = entry->pin;
435 reg = io_apic_read(entry->apic, 0x10 + pin*2);
436 /* Is the remote IRR bit set? */
437 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
438 spin_unlock_irqrestore(&ioapic_lock, flags);
439 return true;
440 }
441 if (!entry->next)
442 break;
443 entry = entry->next;
444 }
445 spin_unlock_irqrestore(&ioapic_lock, flags);
446
447 return false;
448}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700449
Andi Kleencf4c6a22006-09-26 10:52:30 +0200450union entry_union {
451 struct { u32 w1, w2; };
452 struct IO_APIC_route_entry entry;
453};
454
455static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
456{
457 union entry_union eu;
458 unsigned long flags;
459 spin_lock_irqsave(&ioapic_lock, flags);
460 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
461 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
462 spin_unlock_irqrestore(&ioapic_lock, flags);
463 return eu.entry;
464}
465
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800466/*
467 * When we write a new IO APIC routing entry, we need to write the high
468 * word first! If the mask bit in the low word is clear, we will enable
469 * the interrupt, and we need to make sure the entry is fully populated
470 * before that happens.
471 */
Andi Kleend15512f2006-12-07 02:14:07 +0100472static void
473__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
474{
475 union entry_union eu;
476 eu.entry = e;
477 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
478 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
479}
480
Andi Kleencf4c6a22006-09-26 10:52:30 +0200481static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
482{
483 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200484 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100485 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800486 spin_unlock_irqrestore(&ioapic_lock, flags);
487}
488
489/*
490 * When we mask an IO APIC routing entry, we need to write the low
491 * word first, in order to set the mask bit before we change the
492 * high bits!
493 */
494static void ioapic_mask_entry(int apic, int pin)
495{
496 unsigned long flags;
497 union entry_union eu = { .entry.mask = 1 };
498
499 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200500 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
501 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
502 spin_unlock_irqrestore(&ioapic_lock, flags);
503}
504
Yinghai Lu497c9a12008-08-19 20:50:28 -0700505#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -0800506static void send_cleanup_vector(struct irq_cfg *cfg)
507{
508 cpumask_var_t cleanup_mask;
509
510 if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
511 unsigned int i;
512 cfg->move_cleanup_count = 0;
513 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
514 cfg->move_cleanup_count++;
515 for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
516 send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
517 } else {
518 cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
519 cfg->move_cleanup_count = cpumask_weight(cleanup_mask);
520 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
521 free_cpumask_var(cleanup_mask);
522 }
523 cfg->move_in_progress = 0;
524}
525
Yinghai Lu3145e942008-12-05 18:58:34 -0800526static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700527{
528 int apic, pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700529 struct irq_pin_list *entry;
Yinghai Lu3145e942008-12-05 18:58:34 -0800530 u8 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700531
Yinghai Lu497c9a12008-08-19 20:50:28 -0700532 entry = cfg->irq_2_pin;
533 for (;;) {
534 unsigned int reg;
535
536 if (!entry)
537 break;
538
539 apic = entry->apic;
540 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200541#ifdef CONFIG_INTR_REMAP
542 /*
543 * With interrupt-remapping, destination information comes
544 * from interrupt-remapping table entry.
545 */
546 if (!irq_remapped(irq))
547 io_apic_write(apic, 0x11 + pin*2, dest);
548#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700549 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200550#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700551 reg = io_apic_read(apic, 0x10 + pin*2);
552 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
553 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200554 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700555 if (!entry->next)
556 break;
557 entry = entry->next;
558 }
559}
Yinghai Luefa25592008-08-19 20:50:36 -0700560
Mike Travise7986732008-12-16 17:33:52 -0800561static int
562assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask);
Yinghai Luefa25592008-08-19 20:50:36 -0700563
Mike Travis22f65d32008-12-16 17:33:56 -0800564/*
565 * Either sets desc->affinity to a valid value, and returns cpu_mask_to_apicid
566 * of that, or returns BAD_APICID and leaves desc->affinity untouched.
567 */
568static unsigned int
569set_desc_affinity(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700570{
571 struct irq_cfg *cfg;
Yinghai Lu3145e942008-12-05 18:58:34 -0800572 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700573
Rusty Russell0de26522008-12-13 21:20:26 +1030574 if (!cpumask_intersects(mask, cpu_online_mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800575 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700576
Yinghai Lu3145e942008-12-05 18:58:34 -0800577 irq = desc->irq;
578 cfg = desc->chip_data;
579 if (assign_irq_vector(irq, cfg, mask))
Mike Travis22f65d32008-12-16 17:33:56 -0800580 return BAD_APICID;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700581
Mike Travis22f65d32008-12-16 17:33:56 -0800582 cpumask_and(&desc->affinity, cfg->domain, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -0800583 set_extra_move_desc(desc, mask);
Mike Travis22f65d32008-12-16 17:33:56 -0800584 return cpu_mask_to_apicid_and(&desc->affinity, cpu_online_mask);
585}
Yinghai Lu3145e942008-12-05 18:58:34 -0800586
Mike Travis22f65d32008-12-16 17:33:56 -0800587static void
588set_ioapic_affinity_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700589{
590 struct irq_cfg *cfg;
591 unsigned long flags;
592 unsigned int dest;
Mike Travis22f65d32008-12-16 17:33:56 -0800593 unsigned int irq;
594
595 irq = desc->irq;
596 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700597
598 spin_lock_irqsave(&ioapic_lock, flags);
Mike Travis22f65d32008-12-16 17:33:56 -0800599 dest = set_desc_affinity(desc, mask);
600 if (dest != BAD_APICID) {
601 /* Only the high 8 bits are valid. */
602 dest = SET_APIC_LOGICAL_ID(dest);
603 __target_IO_APIC_irq(irq, dest, cfg);
604 }
Yinghai Lu497c9a12008-08-19 20:50:28 -0700605 spin_unlock_irqrestore(&ioapic_lock, flags);
606}
Yinghai Lu3145e942008-12-05 18:58:34 -0800607
Mike Travis22f65d32008-12-16 17:33:56 -0800608static void
609set_ioapic_affinity_irq(unsigned int irq, const struct cpumask *mask)
Yinghai Lu3145e942008-12-05 18:58:34 -0800610{
Yinghai Lu497c9a12008-08-19 20:50:28 -0700611 struct irq_desc *desc;
612
Yinghai Lu497c9a12008-08-19 20:50:28 -0700613 desc = irq_to_desc(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -0800614
615 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700616}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700617#endif /* CONFIG_SMP */
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
620 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
621 * shared ISA-space IRQs, so we have to support them. We are super
622 * fast in the common case, and fast for shared ISA-space IRQs.
623 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800624static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700626 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Yinghai Lu0f978f42008-08-19 20:50:26 -0700628 entry = cfg->irq_2_pin;
629 if (!entry) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800630 entry = get_one_free_irq_2_pin(cpu);
631 if (!entry) {
632 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
633 apic, pin);
634 return;
635 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700636 cfg->irq_2_pin = entry;
637 entry->apic = apic;
638 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700639 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700641
642 while (entry->next) {
643 /* not again, please */
644 if (entry->apic == apic && entry->pin == pin)
645 return;
646
647 entry = entry->next;
648 }
649
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800650 entry->next = get_one_free_irq_2_pin(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700651 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 entry->apic = apic;
653 entry->pin = pin;
654}
655
656/*
657 * Reroute an IRQ to a different pin.
658 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800659static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 int oldapic, int oldpin,
661 int newapic, int newpin)
662{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700663 struct irq_pin_list *entry = cfg->irq_2_pin;
664 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Yinghai Lu0f978f42008-08-19 20:50:26 -0700666 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (entry->apic == oldapic && entry->pin == oldpin) {
668 entry->apic = newapic;
669 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700670 replaced = 1;
671 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700673 }
674 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700676
677 /* why? call replace before add? */
678 if (!replaced)
Yinghai Lu3145e942008-12-05 18:58:34 -0800679 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Yinghai Lu3145e942008-12-05 18:58:34 -0800682static inline void io_apic_modify_irq(struct irq_cfg *cfg,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400683 int mask_and, int mask_or,
684 void (*final)(struct irq_pin_list *entry))
685{
686 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400687 struct irq_pin_list *entry;
688
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400689 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
690 unsigned int reg;
691 pin = entry->pin;
692 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
693 reg &= mask_and;
694 reg |= mask_or;
695 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
696 if (final)
697 final(entry);
698 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700699}
700
Yinghai Lu3145e942008-12-05 18:58:34 -0800701static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400702{
Yinghai Lu3145e942008-12-05 18:58:34 -0800703 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400704}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700705
706#ifdef CONFIG_X86_64
Jaswinder Singh Rajput7f3e6322008-12-29 20:34:35 +0530707static void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700708{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400709 /*
710 * Synchronize the IO-APIC and the CPU by doing
711 * a dummy read from the IO-APIC
712 */
713 struct io_apic __iomem *io_apic;
714 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700715 readl(&io_apic->data);
716}
717
Yinghai Lu3145e942008-12-05 18:58:34 -0800718static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400719{
Yinghai Lu3145e942008-12-05 18:58:34 -0800720 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400721}
722#else /* CONFIG_X86_32 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800723static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400724{
Yinghai Lu3145e942008-12-05 18:58:34 -0800725 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400726}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700727
Yinghai Lu3145e942008-12-05 18:58:34 -0800728static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400729{
Yinghai Lu3145e942008-12-05 18:58:34 -0800730 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400731 IO_APIC_REDIR_MASKED, NULL);
732}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700733
Yinghai Lu3145e942008-12-05 18:58:34 -0800734static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400735{
Yinghai Lu3145e942008-12-05 18:58:34 -0800736 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400737 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
738}
739#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700740
Yinghai Lu3145e942008-12-05 18:58:34 -0800741static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Yinghai Lu3145e942008-12-05 18:58:34 -0800743 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 unsigned long flags;
745
Yinghai Lu3145e942008-12-05 18:58:34 -0800746 BUG_ON(!cfg);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800749 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 spin_unlock_irqrestore(&ioapic_lock, flags);
751}
752
Yinghai Lu3145e942008-12-05 18:58:34 -0800753static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Yinghai Lu3145e942008-12-05 18:58:34 -0800755 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 unsigned long flags;
757
758 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800759 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 spin_unlock_irqrestore(&ioapic_lock, flags);
761}
762
Yinghai Lu3145e942008-12-05 18:58:34 -0800763static void mask_IO_APIC_irq(unsigned int irq)
764{
765 struct irq_desc *desc = irq_to_desc(irq);
766
767 mask_IO_APIC_irq_desc(desc);
768}
769static void unmask_IO_APIC_irq(unsigned int irq)
770{
771 struct irq_desc *desc = irq_to_desc(irq);
772
773 unmask_IO_APIC_irq_desc(desc);
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
777{
778 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200781 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (entry.delivery_mode == dest_SMI)
783 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 /*
785 * Disable it in the IO-APIC irq-routing table:
786 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800787 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Ingo Molnar54168ed2008-08-20 09:07:45 +0200790static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 int apic, pin;
793
794 for (apic = 0; apic < nr_ioapics; apic++)
795 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
796 clear_IO_APIC_pin(apic, pin);
797}
798
Ingo Molnar54168ed2008-08-20 09:07:45 +0200799#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100800void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 unsigned int cfg;
803
804 /*
805 * Wait for idle.
806 */
807 apic_wait_icr_idle();
808 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
809 /*
810 * Send the IPI. The write to APIC_ICR fires this off.
811 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100812 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200814#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Ingo Molnar54168ed2008-08-20 09:07:45 +0200816#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817/*
818 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
819 * specific CPU-side IRQs.
820 */
821
822#define MAX_PIRQS 8
823static int pirq_entries [MAX_PIRQS];
824static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826static int __init ioapic_pirq_setup(char *str)
827{
828 int i, max;
829 int ints[MAX_PIRQS+1];
830
831 get_options(str, ARRAY_SIZE(ints), ints);
832
833 for (i = 0; i < MAX_PIRQS; i++)
834 pirq_entries[i] = -1;
835
836 pirqs_enabled = 1;
837 apic_printk(APIC_VERBOSE, KERN_INFO
838 "PIRQ redirection, working around broken MP-BIOS.\n");
839 max = MAX_PIRQS;
840 if (ints[0] < MAX_PIRQS)
841 max = ints[0];
842
843 for (i = 0; i < max; i++) {
844 apic_printk(APIC_VERBOSE, KERN_DEBUG
845 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
846 /*
847 * PIRQs are mapped upside down, usually.
848 */
849 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
850 }
851 return 1;
852}
853
854__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200855#endif /* CONFIG_X86_32 */
856
857#ifdef CONFIG_INTR_REMAP
858/* I/O APIC RTE contents at the OS boot up */
859static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
860
861/*
862 * Saves and masks all the unmasked IO-APIC RTE's
863 */
864int save_mask_IO_APIC_setup(void)
865{
866 union IO_APIC_reg_01 reg_01;
867 unsigned long flags;
868 int apic, pin;
869
870 /*
871 * The number of IO-APIC IRQ registers (== #pins):
872 */
873 for (apic = 0; apic < nr_ioapics; apic++) {
874 spin_lock_irqsave(&ioapic_lock, flags);
875 reg_01.raw = io_apic_read(apic, 1);
876 spin_unlock_irqrestore(&ioapic_lock, flags);
877 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
878 }
879
880 for (apic = 0; apic < nr_ioapics; apic++) {
881 early_ioapic_entries[apic] =
882 kzalloc(sizeof(struct IO_APIC_route_entry) *
883 nr_ioapic_registers[apic], GFP_KERNEL);
884 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400885 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200886 }
887
888 for (apic = 0; apic < nr_ioapics; apic++)
889 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
890 struct IO_APIC_route_entry entry;
891
892 entry = early_ioapic_entries[apic][pin] =
893 ioapic_read_entry(apic, pin);
894 if (!entry.mask) {
895 entry.mask = 1;
896 ioapic_write_entry(apic, pin, entry);
897 }
898 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400899
Ingo Molnar54168ed2008-08-20 09:07:45 +0200900 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400901
902nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400903 while (apic >= 0)
904 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400905 memset(early_ioapic_entries, 0,
906 ARRAY_SIZE(early_ioapic_entries));
907
908 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200909}
910
911void restore_IO_APIC_setup(void)
912{
913 int apic, pin;
914
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400915 for (apic = 0; apic < nr_ioapics; apic++) {
916 if (!early_ioapic_entries[apic])
917 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200918 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
919 ioapic_write_entry(apic, pin,
920 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400921 kfree(early_ioapic_entries[apic]);
922 early_ioapic_entries[apic] = NULL;
923 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200924}
925
926void reinit_intr_remapped_IO_APIC(int intr_remapping)
927{
928 /*
929 * for now plain restore of previous settings.
930 * TBD: In the case of OS enabling interrupt-remapping,
931 * IO-APIC RTE's need to be setup to point to interrupt-remapping
932 * table entries. for now, do a plain restore, and wait for
933 * the setup_IO_APIC_irqs() to do proper initialization.
934 */
935 restore_IO_APIC_setup();
936}
937#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939/*
940 * Find the IRQ entry number of a certain pin.
941 */
942static int find_irq_entry(int apic, int pin, int type)
943{
944 int i;
945
946 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400947 if (mp_irqs[i].mp_irqtype == type &&
948 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
949 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
950 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 return i;
952
953 return -1;
954}
955
956/*
957 * Find the pin to which IRQ[irq] (ISA) is connected
958 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800959static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 int i;
962
963 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400964 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300966 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400967 (mp_irqs[i].mp_irqtype == type) &&
968 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400970 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972 return -1;
973}
974
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800975static int __init find_isa_irq_apic(int irq, int type)
976{
977 int i;
978
979 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400980 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800981
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300982 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400983 (mp_irqs[i].mp_irqtype == type) &&
984 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800985 break;
986 }
987 if (i < mp_irq_entries) {
988 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200989 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400990 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800991 return apic;
992 }
993 }
994
995 return -1;
996}
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998/*
999 * Find a specific PCI IRQ entry.
1000 * Not an __init, possibly needed by modules
1001 */
1002static int pin_2_irq(int idx, int apic, int pin);
1003
1004int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
1005{
1006 int apic, i, best_guess = -1;
1007
Ingo Molnar54168ed2008-08-20 09:07:45 +02001008 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1009 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +04001010 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001011 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return -1;
1013 }
1014 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001015 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001018 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
1019 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 break;
1021
Alexey Starikovskiy47cab822008-03-20 14:54:30 +03001022 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001023 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001025 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001026 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (!(apic || IO_APIC_IRQ(irq)))
1029 continue;
1030
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001031 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return irq;
1033 /*
1034 * Use the first all-but-pin matching entry as a
1035 * best-guess fuzzy result for broken mptables.
1036 */
1037 if (best_guess < 0)
1038 best_guess = irq;
1039 }
1040 }
1041 return best_guess;
1042}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001043
Alexey Dobriyan129f6942005-06-23 00:08:33 -07001044EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001046#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047/*
1048 * EISA Edge/Level control register, ELCR
1049 */
1050static int EISA_ELCR(unsigned int irq)
1051{
Yinghai Lu99d093d2008-12-05 18:58:32 -08001052 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 unsigned int port = 0x4d0 + (irq >> 3);
1054 return (inb(port) >> (irq & 7)) & 1;
1055 }
1056 apic_printk(APIC_VERBOSE, KERN_INFO
1057 "Broken MPtable reports ISA irq %d\n", irq);
1058 return 0;
1059}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001060
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001061#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001063/* ISA interrupts are always polarity zero edge triggered,
1064 * when listed as conforming in the MP table. */
1065
1066#define default_ISA_trigger(idx) (0)
1067#define default_ISA_polarity(idx) (0)
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069/* EISA interrupts are always polarity zero and can be edge or level
1070 * trigger depending on the ELCR value. If an interrupt is listed as
1071 * EISA conforming in the MP table, that means its trigger type must
1072 * be read in from the ELCR */
1073
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001074#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001075#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077/* PCI interrupts are always polarity one level triggered,
1078 * when listed as conforming in the MP table. */
1079
1080#define default_PCI_trigger(idx) (1)
1081#define default_PCI_polarity(idx) (1)
1082
1083/* MCA interrupts are always polarity zero level triggered,
1084 * when listed as conforming in the MP table. */
1085
1086#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001087#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Shaohua Li61fd47e2007-11-17 01:05:28 -05001089static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001091 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 int polarity;
1093
1094 /*
1095 * Determine IRQ line polarity (high active or low active):
1096 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001097 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001099 case 0: /* conforms, ie. bus-type dependent polarity */
1100 if (test_bit(bus, mp_bus_not_pci))
1101 polarity = default_ISA_polarity(idx);
1102 else
1103 polarity = default_PCI_polarity(idx);
1104 break;
1105 case 1: /* high active */
1106 {
1107 polarity = 0;
1108 break;
1109 }
1110 case 2: /* reserved */
1111 {
1112 printk(KERN_WARNING "broken BIOS!!\n");
1113 polarity = 1;
1114 break;
1115 }
1116 case 3: /* low active */
1117 {
1118 polarity = 1;
1119 break;
1120 }
1121 default: /* invalid */
1122 {
1123 printk(KERN_WARNING "broken BIOS!!\n");
1124 polarity = 1;
1125 break;
1126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128 return polarity;
1129}
1130
1131static int MPBIOS_trigger(int idx)
1132{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001133 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 int trigger;
1135
1136 /*
1137 * Determine IRQ trigger mode (edge or level sensitive):
1138 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001139 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001141 case 0: /* conforms, ie. bus-type dependent */
1142 if (test_bit(bus, mp_bus_not_pci))
1143 trigger = default_ISA_trigger(idx);
1144 else
1145 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001146#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001147 switch (mp_bus_id_to_type[bus]) {
1148 case MP_BUS_ISA: /* ISA pin */
1149 {
1150 /* set before the switch */
1151 break;
1152 }
1153 case MP_BUS_EISA: /* EISA pin */
1154 {
1155 trigger = default_EISA_trigger(idx);
1156 break;
1157 }
1158 case MP_BUS_PCI: /* PCI pin */
1159 {
1160 /* set before the switch */
1161 break;
1162 }
1163 case MP_BUS_MCA: /* MCA pin */
1164 {
1165 trigger = default_MCA_trigger(idx);
1166 break;
1167 }
1168 default:
1169 {
1170 printk(KERN_WARNING "broken BIOS!!\n");
1171 trigger = 1;
1172 break;
1173 }
1174 }
1175#endif
1176 break;
1177 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001178 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001179 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001180 break;
1181 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001182 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001183 {
1184 printk(KERN_WARNING "broken BIOS!!\n");
1185 trigger = 1;
1186 break;
1187 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001188 case 3: /* level */
1189 {
1190 trigger = 1;
1191 break;
1192 }
1193 default: /* invalid */
1194 {
1195 printk(KERN_WARNING "broken BIOS!!\n");
1196 trigger = 0;
1197 break;
1198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
1200 return trigger;
1201}
1202
1203static inline int irq_polarity(int idx)
1204{
1205 return MPBIOS_polarity(idx);
1206}
1207
1208static inline int irq_trigger(int idx)
1209{
1210 return MPBIOS_trigger(idx);
1211}
1212
Yinghai Luefa25592008-08-19 20:50:36 -07001213int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214static int pin_2_irq(int idx, int apic, int pin)
1215{
1216 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001217 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
1220 * Debugging check, we are in big trouble if this message pops up!
1221 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001222 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1224
Ingo Molnar54168ed2008-08-20 09:07:45 +02001225 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001226 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001227 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001228 /*
1229 * PCI IRQs are mapped in order
1230 */
1231 i = irq = 0;
1232 while (i < apic)
1233 irq += nr_ioapic_registers[i++];
1234 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001235 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001236 * For MPS mode, so far only needed by ES7000 platform
1237 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001238 if (ioapic_renumber_irq)
1239 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241
Ingo Molnar54168ed2008-08-20 09:07:45 +02001242#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1245 */
1246 if ((pin >= 16) && (pin <= 23)) {
1247 if (pirq_entries[pin-16] != -1) {
1248 if (!pirq_entries[pin-16]) {
1249 apic_printk(APIC_VERBOSE, KERN_DEBUG
1250 "disabling PIRQ%d\n", pin-16);
1251 } else {
1252 irq = pirq_entries[pin-16];
1253 apic_printk(APIC_VERBOSE, KERN_DEBUG
1254 "using PIRQ%d -> IRQ %d\n",
1255 pin-16, irq);
1256 }
1257 }
1258 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001259#endif
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return irq;
1262}
1263
Yinghai Lu497c9a12008-08-19 20:50:28 -07001264void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001266 /* Used to the online set of cpus does not change
1267 * during assign_irq_vector.
1268 */
1269 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Yinghai Lu497c9a12008-08-19 20:50:28 -07001272void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001273{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001274 spin_unlock(&vector_lock);
1275}
1276
Mike Travise7986732008-12-16 17:33:52 -08001277static int
1278__assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001279{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001280 /*
1281 * NOTE! The local APIC isn't very good at handling
1282 * multiple interrupts at the same interrupt level.
1283 * As the interrupt level is determined by taking the
1284 * vector number and shifting that right by 4, we
1285 * want to spread these out a bit so that they don't
1286 * all fall in the same interrupt level.
1287 *
1288 * Also, we've got to be careful not to trash gate
1289 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1290 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001291 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1292 unsigned int old_vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001293 int cpu, err;
1294 cpumask_var_t tmp_mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001295
Ingo Molnar54168ed2008-08-20 09:07:45 +02001296 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1297 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001298
Mike Travis22f65d32008-12-16 17:33:56 -08001299 if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1300 return -ENOMEM;
Yinghai Lu3145e942008-12-05 18:58:34 -08001301
Ingo Molnar54168ed2008-08-20 09:07:45 +02001302 old_vector = cfg->vector;
1303 if (old_vector) {
Mike Travis22f65d32008-12-16 17:33:56 -08001304 cpumask_and(tmp_mask, mask, cpu_online_mask);
1305 cpumask_and(tmp_mask, cfg->domain, tmp_mask);
1306 if (!cpumask_empty(tmp_mask)) {
1307 free_cpumask_var(tmp_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001308 return 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001309 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001310 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001311
Mike Travise7986732008-12-16 17:33:52 -08001312 /* Only try and allocate irqs on cpus that are present */
Mike Travis22f65d32008-12-16 17:33:56 -08001313 err = -ENOSPC;
1314 for_each_cpu_and(cpu, mask, cpu_online_mask) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001315 int new_cpu;
1316 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001317
Mike Travis22f65d32008-12-16 17:33:56 -08001318 vector_allocation_domain(cpu, tmp_mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001319
Ingo Molnar54168ed2008-08-20 09:07:45 +02001320 vector = current_vector;
1321 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001322next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001323 vector += 8;
1324 if (vector >= first_system_vector) {
Mike Travise7986732008-12-16 17:33:52 -08001325 /* If out of vectors on large boxen, must share them. */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001326 offset = (offset + 1) % 8;
1327 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001328 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001329 if (unlikely(current_vector == vector))
1330 continue;
Yinghai Lub77b8812008-12-19 15:23:44 -08001331
1332 if (test_bit(vector, used_vectors))
Ingo Molnar54168ed2008-08-20 09:07:45 +02001333 goto next;
Yinghai Lub77b8812008-12-19 15:23:44 -08001334
Mike Travis22f65d32008-12-16 17:33:56 -08001335 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001336 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1337 goto next;
1338 /* Found one! */
1339 current_vector = vector;
1340 current_offset = offset;
1341 if (old_vector) {
1342 cfg->move_in_progress = 1;
Mike Travis22f65d32008-12-16 17:33:56 -08001343 cpumask_copy(cfg->old_domain, cfg->domain);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001344 }
Mike Travis22f65d32008-12-16 17:33:56 -08001345 for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001346 per_cpu(vector_irq, new_cpu)[vector] = irq;
1347 cfg->vector = vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001348 cpumask_copy(cfg->domain, tmp_mask);
1349 err = 0;
1350 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001351 }
Mike Travis22f65d32008-12-16 17:33:56 -08001352 free_cpumask_var(tmp_mask);
1353 return err;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001354}
1355
Mike Travise7986732008-12-16 17:33:52 -08001356static int
1357assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001358{
1359 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001360 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001361
1362 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001363 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001364 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001365 return err;
1366}
1367
Yinghai Lu3145e942008-12-05 18:58:34 -08001368static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001369{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001370 int cpu, vector;
1371
Yinghai Lu497c9a12008-08-19 20:50:28 -07001372 BUG_ON(!cfg->vector);
1373
1374 vector = cfg->vector;
Mike Travis22f65d32008-12-16 17:33:56 -08001375 for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001376 per_cpu(vector_irq, cpu)[vector] = -1;
1377
1378 cfg->vector = 0;
Mike Travis22f65d32008-12-16 17:33:56 -08001379 cpumask_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001380
1381 if (likely(!cfg->move_in_progress))
1382 return;
Mike Travis22f65d32008-12-16 17:33:56 -08001383 for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001384 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1385 vector++) {
1386 if (per_cpu(vector_irq, cpu)[vector] != irq)
1387 continue;
1388 per_cpu(vector_irq, cpu)[vector] = -1;
1389 break;
1390 }
1391 }
1392 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001393}
1394
1395void __setup_vector_irq(int cpu)
1396{
1397 /* Initialize vector_irq on a new cpu */
1398 /* This function must be called with vector_lock held */
1399 int irq, vector;
1400 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001401 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001402
1403 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001404 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001405 cfg = desc->chip_data;
Mike Travis22f65d32008-12-16 17:33:56 -08001406 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001407 continue;
1408 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001409 per_cpu(vector_irq, cpu)[vector] = irq;
1410 }
1411 /* Mark the free vectors */
1412 for (vector = 0; vector < NR_VECTORS; ++vector) {
1413 irq = per_cpu(vector_irq, cpu)[vector];
1414 if (irq < 0)
1415 continue;
1416
1417 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08001418 if (!cpumask_test_cpu(cpu, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001419 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001420 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001421}
Glauber Costa3fde6902008-05-28 20:34:19 -07001422
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001423static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001424#ifdef CONFIG_INTR_REMAP
1425static struct irq_chip ir_ioapic_chip;
1426#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Ingo Molnar54168ed2008-08-20 09:07:45 +02001428#define IOAPIC_AUTO -1
1429#define IOAPIC_EDGE 0
1430#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001432#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001433static inline int IO_APIC_irq_trigger(int irq)
1434{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001435 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001436
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001437 for (apic = 0; apic < nr_ioapics; apic++) {
1438 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1439 idx = find_irq_entry(apic, pin, mp_INT);
1440 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1441 return irq_trigger(idx);
1442 }
1443 }
1444 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001445 * nonexistent IRQs are edge default
1446 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001447 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001448}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001449#else
1450static inline int IO_APIC_irq_trigger(int irq)
1451{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001452 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001453}
1454#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001455
Yinghai Lu3145e942008-12-05 18:58:34 -08001456static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Yinghai Lu199751d2008-08-19 20:50:27 -07001458
Jan Beulich6ebcc002006-06-26 13:56:46 +02001459 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001460 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001461 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001462 else
1463 desc->status &= ~IRQ_LEVEL;
1464
Ingo Molnar54168ed2008-08-20 09:07:45 +02001465#ifdef CONFIG_INTR_REMAP
1466 if (irq_remapped(irq)) {
1467 desc->status |= IRQ_MOVE_PCNTXT;
1468 if (trigger)
1469 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1470 handle_fasteoi_irq,
1471 "fasteoi");
1472 else
1473 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1474 handle_edge_irq, "edge");
1475 return;
1476 }
1477#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001478 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1479 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001480 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001481 handle_fasteoi_irq,
1482 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001483 else
Ingo Molnara460e742006-10-17 00:10:03 -07001484 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001485 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001486}
1487
1488static int setup_ioapic_entry(int apic, int irq,
1489 struct IO_APIC_route_entry *entry,
1490 unsigned int destination, int trigger,
1491 int polarity, int vector)
1492{
1493 /*
1494 * add it to the IO-APIC irq-routing table:
1495 */
1496 memset(entry,0,sizeof(*entry));
1497
Ingo Molnar54168ed2008-08-20 09:07:45 +02001498#ifdef CONFIG_INTR_REMAP
1499 if (intr_remapping_enabled) {
1500 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1501 struct irte irte;
1502 struct IR_IO_APIC_route_entry *ir_entry =
1503 (struct IR_IO_APIC_route_entry *) entry;
1504 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001505
Ingo Molnar54168ed2008-08-20 09:07:45 +02001506 if (!iommu)
1507 panic("No mapping iommu for ioapic %d\n", apic);
1508
1509 index = alloc_irte(iommu, irq, 1);
1510 if (index < 0)
1511 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1512
1513 memset(&irte, 0, sizeof(irte));
1514
1515 irte.present = 1;
1516 irte.dst_mode = INT_DEST_MODE;
1517 irte.trigger_mode = trigger;
1518 irte.dlvry_mode = INT_DELIVERY_MODE;
1519 irte.vector = vector;
1520 irte.dest_id = IRTE_DEST(destination);
1521
1522 modify_irte(irq, &irte);
1523
1524 ir_entry->index2 = (index >> 15) & 0x1;
1525 ir_entry->zero = 0;
1526 ir_entry->format = 1;
1527 ir_entry->index = (index & 0x7fff);
1528 } else
1529#endif
1530 {
1531 entry->delivery_mode = INT_DELIVERY_MODE;
1532 entry->dest_mode = INT_DEST_MODE;
1533 entry->dest = destination;
1534 }
1535
1536 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001537 entry->trigger = trigger;
1538 entry->polarity = polarity;
1539 entry->vector = vector;
1540
1541 /* Mask level triggered irqs.
1542 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1543 */
1544 if (trigger)
1545 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001546 return 0;
1547}
1548
Yinghai Lu3145e942008-12-05 18:58:34 -08001549static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001550 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001551{
1552 struct irq_cfg *cfg;
1553 struct IO_APIC_route_entry entry;
Mike Travis22f65d32008-12-16 17:33:56 -08001554 unsigned int dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001555
1556 if (!IO_APIC_IRQ(irq))
1557 return;
1558
Yinghai Lu3145e942008-12-05 18:58:34 -08001559 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001560
Mike Travis22f65d32008-12-16 17:33:56 -08001561 if (assign_irq_vector(irq, cfg, TARGET_CPUS))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001562 return;
1563
Mike Travis22f65d32008-12-16 17:33:56 -08001564 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001565
1566 apic_printk(APIC_VERBOSE,KERN_DEBUG
1567 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1568 "IRQ %d Mode:%i Active:%i)\n",
1569 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1570 irq, trigger, polarity);
1571
1572
1573 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
Mike Travis22f65d32008-12-16 17:33:56 -08001574 dest, trigger, polarity, cfg->vector)) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001575 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1576 mp_ioapics[apic].mp_apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001577 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001578 return;
1579 }
1580
Yinghai Lu3145e942008-12-05 18:58:34 -08001581 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001582 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001583 disable_8259A_irq(irq);
1584
1585 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
1588static void __init setup_IO_APIC_irqs(void)
1589{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001590 int apic, pin, idx, irq;
1591 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001592 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001593 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001594 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
1596 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1597
1598 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001599 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001601 idx = find_irq_entry(apic, pin, mp_INT);
1602 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001603 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001604 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001605 apic_printk(APIC_VERBOSE,
1606 KERN_DEBUG " %d-%d",
1607 mp_ioapics[apic].mp_apicid,
1608 pin);
1609 } else
1610 apic_printk(APIC_VERBOSE, " %d-%d",
1611 mp_ioapics[apic].mp_apicid,
1612 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001613 continue;
1614 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001615 if (notcon) {
1616 apic_printk(APIC_VERBOSE,
1617 " (apicid-pin) not connected\n");
1618 notcon = 0;
1619 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001620
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001621 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001622#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001623 if (multi_timer_check(apic, irq))
1624 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001625#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001626 desc = irq_to_desc_alloc_cpu(irq, cpu);
1627 if (!desc) {
1628 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1629 continue;
1630 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001631 cfg = desc->chip_data;
1632 add_pin_to_irq_cpu(cfg, cpu, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001633
Yinghai Lu3145e942008-12-05 18:58:34 -08001634 setup_IO_APIC_irq(apic, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001635 irq_trigger(idx), irq_polarity(idx));
1636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001639 if (notcon)
1640 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001641 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
1644/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001645 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001647static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1648 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
1650 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Ingo Molnar54168ed2008-08-20 09:07:45 +02001652#ifdef CONFIG_INTR_REMAP
1653 if (intr_remapping_enabled)
1654 return;
1655#endif
1656
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001657 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /*
1660 * We use logical delivery to get the timer IRQ
1661 * to the first CPU.
1662 */
1663 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001664 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001665 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 entry.delivery_mode = INT_DELIVERY_MODE;
1667 entry.polarity = 0;
1668 entry.trigger = 0;
1669 entry.vector = vector;
1670
1671 /*
1672 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001673 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001675 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /*
1678 * Add it to the IO-APIC irq-routing table:
1679 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001680 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001683
1684__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 int apic, i;
1687 union IO_APIC_reg_00 reg_00;
1688 union IO_APIC_reg_01 reg_01;
1689 union IO_APIC_reg_02 reg_02;
1690 union IO_APIC_reg_03 reg_03;
1691 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001692 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001693 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001694 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 if (apic_verbosity == APIC_QUIET)
1697 return;
1698
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001699 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 for (i = 0; i < nr_ioapics; i++)
1701 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001702 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /*
1705 * We are a bit conservative about what we expect. We have to
1706 * know about every hardware change ASAP.
1707 */
1708 printk(KERN_INFO "testing the IO APIC.......................\n");
1709
1710 for (apic = 0; apic < nr_ioapics; apic++) {
1711
1712 spin_lock_irqsave(&ioapic_lock, flags);
1713 reg_00.raw = io_apic_read(apic, 0);
1714 reg_01.raw = io_apic_read(apic, 1);
1715 if (reg_01.bits.version >= 0x10)
1716 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001717 if (reg_01.bits.version >= 0x20)
1718 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 spin_unlock_irqrestore(&ioapic_lock, flags);
1720
Ingo Molnar54168ed2008-08-20 09:07:45 +02001721 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001722 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1724 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1725 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1726 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Ingo Molnar54168ed2008-08-20 09:07:45 +02001728 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1732 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /*
1735 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1736 * but the value of reg_02 is read as the previous read register
1737 * value, so ignore it if reg_02 == reg_01.
1738 */
1739 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1740 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1741 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 }
1743
1744 /*
1745 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1746 * or reg_03, but the value of reg_0[23] is read as the previous read
1747 * register value, so ignore it if reg_03 == reg_0[12].
1748 */
1749 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1750 reg_03.raw != reg_01.raw) {
1751 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1752 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
1755 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1756
Yinghai Lud83e94a2008-08-19 20:50:33 -07001757 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1758 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 for (i = 0; i <= reg_01.bits.entries; i++) {
1761 struct IO_APIC_route_entry entry;
1762
Andi Kleencf4c6a22006-09-26 10:52:30 +02001763 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Ingo Molnar54168ed2008-08-20 09:07:45 +02001765 printk(KERN_DEBUG " %02x %03X ",
1766 i,
1767 entry.dest
1768 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1771 entry.mask,
1772 entry.trigger,
1773 entry.irr,
1774 entry.polarity,
1775 entry.delivery_status,
1776 entry.dest_mode,
1777 entry.delivery_mode,
1778 entry.vector
1779 );
1780 }
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001783 for_each_irq_desc(irq, desc) {
1784 struct irq_pin_list *entry;
1785
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001786 cfg = desc->chip_data;
1787 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001788 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001790 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 for (;;) {
1792 printk("-> %d:%d", entry->apic, entry->pin);
1793 if (!entry->next)
1794 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001795 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797 printk("\n");
1798 }
1799
1800 printk(KERN_INFO ".................................... done.\n");
1801
1802 return;
1803}
1804
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001805__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 unsigned int v;
1808 int i, j;
1809
1810 if (apic_verbosity == APIC_QUIET)
1811 return;
1812
1813 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1814 for (i = 0; i < 8; i++) {
1815 v = apic_read(base + i*0x10);
1816 for (j = 0; j < 32; j++) {
1817 if (v & (1<<j))
1818 printk("1");
1819 else
1820 printk("0");
1821 }
1822 printk("\n");
1823 }
1824}
1825
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001826__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
1828 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001829 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 if (apic_verbosity == APIC_QUIET)
1832 return;
1833
1834 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1835 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001836 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001837 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 v = apic_read(APIC_LVR);
1839 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1840 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001841 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
1843 v = apic_read(APIC_TASKPRI);
1844 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1845
Ingo Molnar54168ed2008-08-20 09:07:45 +02001846 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001847 if (!APIC_XAPIC(ver)) {
1848 v = apic_read(APIC_ARBPRI);
1849 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1850 v & APIC_ARBPRI_MASK);
1851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 v = apic_read(APIC_PROCPRI);
1853 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1854 }
1855
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001856 /*
1857 * Remote read supported only in the 82489DX and local APIC for
1858 * Pentium processors.
1859 */
1860 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1861 v = apic_read(APIC_RRR);
1862 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1863 }
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 v = apic_read(APIC_LDR);
1866 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001867 if (!x2apic_enabled()) {
1868 v = apic_read(APIC_DFR);
1869 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 v = apic_read(APIC_SPIV);
1872 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1873
1874 printk(KERN_DEBUG "... APIC ISR field:\n");
1875 print_APIC_bitfield(APIC_ISR);
1876 printk(KERN_DEBUG "... APIC TMR field:\n");
1877 print_APIC_bitfield(APIC_TMR);
1878 printk(KERN_DEBUG "... APIC IRR field:\n");
1879 print_APIC_bitfield(APIC_IRR);
1880
Ingo Molnar54168ed2008-08-20 09:07:45 +02001881 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1882 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 v = apic_read(APIC_ESR);
1886 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1887 }
1888
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001889 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001890 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1891 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
1893 v = apic_read(APIC_LVTT);
1894 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1895
1896 if (maxlvt > 3) { /* PC is LVT#4. */
1897 v = apic_read(APIC_LVTPC);
1898 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1899 }
1900 v = apic_read(APIC_LVT0);
1901 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1902 v = apic_read(APIC_LVT1);
1903 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1904
1905 if (maxlvt > 2) { /* ERR is LVT#3. */
1906 v = apic_read(APIC_LVTERR);
1907 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1908 }
1909
1910 v = apic_read(APIC_TMICT);
1911 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1912 v = apic_read(APIC_TMCCT);
1913 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1914 v = apic_read(APIC_TDCR);
1915 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1916 printk("\n");
1917}
1918
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001919__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001921 int cpu;
1922
1923 preempt_disable();
1924 for_each_online_cpu(cpu)
1925 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1926 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927}
1928
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001929__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 unsigned int v;
1932 unsigned long flags;
1933
1934 if (apic_verbosity == APIC_QUIET)
1935 return;
1936
1937 printk(KERN_DEBUG "\nprinting PIC contents\n");
1938
1939 spin_lock_irqsave(&i8259A_lock, flags);
1940
1941 v = inb(0xa1) << 8 | inb(0x21);
1942 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1943
1944 v = inb(0xa0) << 8 | inb(0x20);
1945 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1946
Ingo Molnar54168ed2008-08-20 09:07:45 +02001947 outb(0x0b,0xa0);
1948 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001950 outb(0x0a,0xa0);
1951 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 spin_unlock_irqrestore(&i8259A_lock, flags);
1954
1955 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1956
1957 v = inb(0x4d1) << 8 | inb(0x4d0);
1958 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1959}
1960
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001961__apicdebuginit(int) print_all_ICs(void)
1962{
1963 print_PIC();
1964 print_all_local_APICs();
1965 print_IO_APIC();
1966
1967 return 0;
1968}
1969
1970fs_initcall(print_all_ICs);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Yinghai Luefa25592008-08-19 20:50:36 -07001973/* Where if anywhere is the i8259 connect in external int mode */
1974static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1975
Ingo Molnar54168ed2008-08-20 09:07:45 +02001976void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001979 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001980 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 unsigned long flags;
1982
Ingo Molnar54168ed2008-08-20 09:07:45 +02001983#ifdef CONFIG_X86_32
1984 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 if (!pirqs_enabled)
1986 for (i = 0; i < MAX_PIRQS; i++)
1987 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001988#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 /*
1991 * The number of IO-APIC IRQ registers (== #pins):
1992 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001993 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001995 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001997 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1998 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001999 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002000 int pin;
2001 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01002002 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002003 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02002004 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002005
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002006 /* If the interrupt line is enabled and in ExtInt mode
2007 * I have found the pin where the i8259 is connected.
2008 */
2009 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
2010 ioapic_i8259.apic = apic;
2011 ioapic_i8259.pin = pin;
2012 goto found_i8259;
2013 }
2014 }
2015 }
2016 found_i8259:
2017 /* Look to see what if the MP table has reported the ExtINT */
2018 /* If we could not find the appropriate pin by looking at the ioapic
2019 * the i8259 probably is not connected the ioapic but give the
2020 * mptable a chance anyway.
2021 */
2022 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
2023 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
2024 /* Trust the MP table if nothing is setup in the hardware */
2025 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
2026 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
2027 ioapic_i8259.pin = i8259_pin;
2028 ioapic_i8259.apic = i8259_apic;
2029 }
2030 /* Complain if the MP table and the hardware disagree */
2031 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
2032 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
2033 {
2034 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 }
2036
2037 /*
2038 * Do not trust the IO-APIC being empty at bootup
2039 */
2040 clear_IO_APIC();
2041}
2042
2043/*
2044 * Not an __init, needed by the reboot code
2045 */
2046void disable_IO_APIC(void)
2047{
2048 /*
2049 * Clear the IO-APIC before rebooting:
2050 */
2051 clear_IO_APIC();
2052
Eric W. Biederman650927e2005-06-25 14:57:44 -07002053 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02002054 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07002055 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02002056 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07002057 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002058 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07002059 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07002060
2061 memset(&entry, 0, sizeof(entry));
2062 entry.mask = 0; /* Enabled */
2063 entry.trigger = 0; /* Edge */
2064 entry.irr = 0;
2065 entry.polarity = 0; /* High */
2066 entry.delivery_status = 0;
2067 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002068 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07002069 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002070 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07002071
2072 /*
2073 * Add it to the IO-APIC irq-routing table:
2074 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02002075 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07002076 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002077
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002078 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079}
2080
Ingo Molnar54168ed2008-08-20 09:07:45 +02002081#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082/*
2083 * function to set the IO-APIC physical IDs based on the
2084 * values stored in the MPC table.
2085 *
2086 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2087 */
2088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089static void __init setup_ioapic_ids_from_mpc(void)
2090{
2091 union IO_APIC_reg_00 reg_00;
2092 physid_mask_t phys_id_present_map;
2093 int apic;
2094 int i;
2095 unsigned char old_id;
2096 unsigned long flags;
2097
Yinghai Lua4dbc342008-07-25 02:14:28 -07002098 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07002099 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07002100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002102 * Don't check I/O APIC IDs for xAPIC systems. They have
2103 * no meaning without the serial APIC bus.
2104 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002105 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2106 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002107 return;
2108 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 * This is broken; anything with a real cpu count has to
2110 * circumvent this idiocy regardless.
2111 */
2112 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2113
2114 /*
2115 * Set the IOAPIC ID to the value stored in the MPC table.
2116 */
2117 for (apic = 0; apic < nr_ioapics; apic++) {
2118
2119 /* Read the register 0 value */
2120 spin_lock_irqsave(&ioapic_lock, flags);
2121 reg_00.raw = io_apic_read(apic, 0);
2122 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002123
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002124 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002126 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002128 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2130 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002131 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /*
2135 * Sanity check, is the ID really free? Every APIC in a
2136 * system must have a unique ID or we get lots of nice
2137 * 'stuck on smp_invalidate_needed IPI wait' messages.
2138 */
2139 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002140 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002142 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 for (i = 0; i < get_physical_broadcast(); i++)
2144 if (!physid_isset(i, phys_id_present_map))
2145 break;
2146 if (i >= get_physical_broadcast())
2147 panic("Max APIC ID exceeded!\n");
2148 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2149 i);
2150 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002151 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 } else {
2153 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002154 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 apic_printk(APIC_VERBOSE, "Setting %d in the "
2156 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002157 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2159 }
2160
2161
2162 /*
2163 * We need to adjust the IRQ routing table
2164 * if the ID changed.
2165 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002166 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002168 if (mp_irqs[i].mp_dstapic == old_id)
2169 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002170 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
2172 /*
2173 * Read the right value from the MPC table and
2174 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 apic_printk(APIC_VERBOSE, KERN_INFO
2177 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002178 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002180 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002182 io_apic_write(apic, 0, reg_00.raw);
2183 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 /*
2186 * Sanity check
2187 */
2188 spin_lock_irqsave(&ioapic_lock, flags);
2189 reg_00.raw = io_apic_read(apic, 0);
2190 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002191 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 printk("could not set ID!\n");
2193 else
2194 apic_printk(APIC_VERBOSE, " ok.\n");
2195 }
2196}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002199int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002200
2201static int __init notimercheck(char *s)
2202{
2203 no_timer_check = 1;
2204 return 1;
2205}
2206__setup("no_timer_check", notimercheck);
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208/*
2209 * There is a nasty bug in some older SMP boards, their mptable lies
2210 * about the timer IRQ. We do the following to work around the situation:
2211 *
2212 * - timer IRQ defaults to IO-APIC IRQ
2213 * - if this function detects that timer IRQs are defunct, then we fall
2214 * back to ISA timer IRQs
2215 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002216static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002219 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Zachary Amsden8542b202006-12-07 02:14:09 +01002221 if (no_timer_check)
2222 return 1;
2223
Ingo Molnar4aae0702007-12-18 18:05:58 +01002224 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 local_irq_enable();
2226 /* Let ten ticks pass... */
2227 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002228 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
2230 /*
2231 * Expect a few ticks at least, to be sure some possible
2232 * glue logic does not lock up after one or two first
2233 * ticks in a non-ExtINT mode. Also the local APIC
2234 * might have cached one ExtINT interrupt. Finally, at
2235 * least one tick may be lost due to delays.
2236 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002237
2238 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002239 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return 0;
2242}
2243
2244/*
2245 * In the SMP+IOAPIC case it might happen that there are an unspecified
2246 * number of pending IRQ events unhandled. These cases are very rare,
2247 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2248 * better to do it this way as thus we do not have to be aware of
2249 * 'pending' interrupts in the IRQ path, except at this point.
2250 */
2251/*
2252 * Edge triggered needs to resend any interrupt
2253 * that was delayed but this is now handled in the device
2254 * independent code.
2255 */
2256
2257/*
2258 * Starting up a edge-triggered IO-APIC interrupt is
2259 * nasty - we need to make sure that we get the edge.
2260 * If it is already asserted for some reason, we need
2261 * return 1 to indicate that is was pending.
2262 *
2263 * This is not complete - we should be able to fake
2264 * an edge even if it isn't on the 8259A...
2265 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002266
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002267static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 int was_pending = 0;
2270 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002271 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002274 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 disable_8259A_irq(irq);
2276 if (i8259A_irq_pending(irq))
2277 was_pending = 1;
2278 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002279 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002280 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 spin_unlock_irqrestore(&ioapic_lock, flags);
2282
2283 return was_pending;
2284}
2285
Ingo Molnar54168ed2008-08-20 09:07:45 +02002286#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002287static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002289
2290 struct irq_cfg *cfg = irq_cfg(irq);
2291 unsigned long flags;
2292
2293 spin_lock_irqsave(&vector_lock, flags);
Mike Travis22f65d32008-12-16 17:33:56 -08002294 send_IPI_mask(cpumask_of(cpumask_first(cfg->domain)), cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002295 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002296
2297 return 1;
2298}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002299#else
2300static int ioapic_retrigger_irq(unsigned int irq)
2301{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002302 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002303
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002304 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002305}
2306#endif
2307
2308/*
2309 * Level and edge triggered IO-APIC interrupts need different handling,
2310 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2311 * handled with the level-triggered descriptor, but that one has slightly
2312 * more overhead. Level-triggered interrupts cannot be handled with the
2313 * edge-triggered handler, without risking IRQ storms and other ugly
2314 * races.
2315 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002316
Yinghai Lu497c9a12008-08-19 20:50:28 -07002317#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002318
2319#ifdef CONFIG_INTR_REMAP
2320static void ir_irq_migration(struct work_struct *work);
2321
2322static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2323
2324/*
2325 * Migrate the IO-APIC irq in the presence of intr-remapping.
2326 *
2327 * For edge triggered, irq migration is a simple atomic update(of vector
2328 * and cpu destination) of IRTE and flush the hardware cache.
2329 *
2330 * For level triggered, we need to modify the io-apic RTE aswell with the update
2331 * vector information, along with modifying IRTE with vector and destination.
2332 * So irq migration for level triggered is little bit more complex compared to
2333 * edge triggered migration. But the good news is, we use the same algorithm
2334 * for level triggered migration as we have today, only difference being,
2335 * we now initiate the irq migration from process context instead of the
2336 * interrupt context.
2337 *
2338 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2339 * suppression) to the IO-APIC, level triggered irq migration will also be
2340 * as simple as edge triggered migration and we can do the irq migration
2341 * with a simple atomic update to IO-APIC RTE.
2342 */
Mike Travise7986732008-12-16 17:33:52 -08002343static void
2344migrate_ioapic_irq_desc(struct irq_desc *desc, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002345{
2346 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002347 struct irte irte;
2348 int modify_ioapic_rte;
2349 unsigned int dest;
2350 unsigned long flags;
Yinghai Lu3145e942008-12-05 18:58:34 -08002351 unsigned int irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002352
Mike Travis22f65d32008-12-16 17:33:56 -08002353 if (!cpumask_intersects(mask, cpu_online_mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002354 return;
2355
Yinghai Lu3145e942008-12-05 18:58:34 -08002356 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002357 if (get_irte(irq, &irte))
2358 return;
2359
Yinghai Lu3145e942008-12-05 18:58:34 -08002360 cfg = desc->chip_data;
2361 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002362 return;
2363
Yinghai Lu3145e942008-12-05 18:58:34 -08002364 set_extra_move_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002365
Mike Travis22f65d32008-12-16 17:33:56 -08002366 dest = cpu_mask_to_apicid_and(cfg->domain, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002367
Ingo Molnar54168ed2008-08-20 09:07:45 +02002368 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2369 if (modify_ioapic_rte) {
2370 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08002371 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002372 spin_unlock_irqrestore(&ioapic_lock, flags);
2373 }
2374
2375 irte.vector = cfg->vector;
2376 irte.dest_id = IRTE_DEST(dest);
2377
2378 /*
2379 * Modified the IRTE and flushes the Interrupt entry cache.
2380 */
2381 modify_irte(irq, &irte);
2382
Mike Travis22f65d32008-12-16 17:33:56 -08002383 if (cfg->move_in_progress)
2384 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002385
Mike Travis22f65d32008-12-16 17:33:56 -08002386 cpumask_copy(&desc->affinity, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002387}
2388
Yinghai Lu3145e942008-12-05 18:58:34 -08002389static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002390{
2391 int ret = -1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002392 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002393
Yinghai Lu3145e942008-12-05 18:58:34 -08002394 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002395
Yinghai Lu3145e942008-12-05 18:58:34 -08002396 if (io_apic_level_ack_pending(cfg)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002397 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002398 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002399 * vector information in the IO-APIC RTE and that will confuse
2400 * the EOI broadcast performed by cpu.
2401 * So, delay the irq migration to the next instance.
2402 */
2403 schedule_delayed_work(&ir_migration_work, 1);
2404 goto unmask;
2405 }
2406
2407 /* everthing is clear. we have right of way */
Mike Travise7986732008-12-16 17:33:52 -08002408 migrate_ioapic_irq_desc(desc, &desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002409
2410 ret = 0;
2411 desc->status &= ~IRQ_MOVE_PENDING;
Mike Travis22f65d32008-12-16 17:33:56 -08002412 cpumask_clear(&desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002413
2414unmask:
Yinghai Lu3145e942008-12-05 18:58:34 -08002415 unmask_IO_APIC_irq_desc(desc);
2416
Ingo Molnar54168ed2008-08-20 09:07:45 +02002417 return ret;
2418}
2419
2420static void ir_irq_migration(struct work_struct *work)
2421{
2422 unsigned int irq;
2423 struct irq_desc *desc;
2424
2425 for_each_irq_desc(irq, desc) {
2426 if (desc->status & IRQ_MOVE_PENDING) {
2427 unsigned long flags;
2428
2429 spin_lock_irqsave(&desc->lock, flags);
2430 if (!desc->chip->set_affinity ||
2431 !(desc->status & IRQ_MOVE_PENDING)) {
2432 desc->status &= ~IRQ_MOVE_PENDING;
2433 spin_unlock_irqrestore(&desc->lock, flags);
2434 continue;
2435 }
2436
Rusty Russell0de26522008-12-13 21:20:26 +10302437 desc->chip->set_affinity(irq, &desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002438 spin_unlock_irqrestore(&desc->lock, flags);
2439 }
2440 }
2441}
2442
2443/*
2444 * Migrates the IRQ destination in the process context.
2445 */
Rusty Russell968ea6d2008-12-13 21:55:51 +10302446static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc,
2447 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002448{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002449 if (desc->status & IRQ_LEVEL) {
2450 desc->status |= IRQ_MOVE_PENDING;
Rusty Russell968ea6d2008-12-13 21:55:51 +10302451 cpumask_copy(&desc->pending_mask, mask);
Yinghai Lu3145e942008-12-05 18:58:34 -08002452 migrate_irq_remapped_level_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002453 return;
2454 }
2455
Yinghai Lu3145e942008-12-05 18:58:34 -08002456 migrate_ioapic_irq_desc(desc, mask);
2457}
Rusty Russell0de26522008-12-13 21:20:26 +10302458static void set_ir_ioapic_affinity_irq(unsigned int irq,
2459 const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002460{
2461 struct irq_desc *desc = irq_to_desc(irq);
2462
Yinghai Lu3145e942008-12-05 18:58:34 -08002463 set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002464}
2465#endif
2466
Yinghai Lu497c9a12008-08-19 20:50:28 -07002467asmlinkage void smp_irq_move_cleanup_interrupt(void)
2468{
2469 unsigned vector, me;
Hiroshi Shimamoto8f2466f2008-12-08 19:19:07 -08002470
Yinghai Lu497c9a12008-08-19 20:50:28 -07002471 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002472 exit_idle();
Yinghai Lu497c9a12008-08-19 20:50:28 -07002473 irq_enter();
2474
2475 me = smp_processor_id();
2476 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2477 unsigned int irq;
2478 struct irq_desc *desc;
2479 struct irq_cfg *cfg;
2480 irq = __get_cpu_var(vector_irq)[vector];
2481
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002482 if (irq == -1)
2483 continue;
2484
Yinghai Lu497c9a12008-08-19 20:50:28 -07002485 desc = irq_to_desc(irq);
2486 if (!desc)
2487 continue;
2488
2489 cfg = irq_cfg(irq);
2490 spin_lock(&desc->lock);
2491 if (!cfg->move_cleanup_count)
2492 goto unlock;
2493
Mike Travis22f65d32008-12-16 17:33:56 -08002494 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002495 goto unlock;
2496
2497 __get_cpu_var(vector_irq)[vector] = -1;
2498 cfg->move_cleanup_count--;
2499unlock:
2500 spin_unlock(&desc->lock);
2501 }
2502
2503 irq_exit();
2504}
2505
Yinghai Lu3145e942008-12-05 18:58:34 -08002506static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002507{
Yinghai Lu3145e942008-12-05 18:58:34 -08002508 struct irq_desc *desc = *descp;
2509 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002510 unsigned vector, me;
2511
Yinghai Lu48a1b102008-12-11 00:15:01 -08002512 if (likely(!cfg->move_in_progress)) {
2513#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2514 if (likely(!cfg->move_desc_pending))
2515 return;
2516
Yinghai Lub9098952008-12-19 13:48:34 -08002517 /* domain has not changed, but affinity did */
Yinghai Lu48a1b102008-12-11 00:15:01 -08002518 me = smp_processor_id();
2519 if (cpu_isset(me, desc->affinity)) {
2520 *descp = desc = move_irq_desc(desc, me);
2521 /* get the new one */
2522 cfg = desc->chip_data;
2523 cfg->move_desc_pending = 0;
2524 }
2525#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002526 return;
Yinghai Lu48a1b102008-12-11 00:15:01 -08002527 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002528
2529 vector = ~get_irq_regs()->orig_ax;
2530 me = smp_processor_id();
Yinghai Lu48a1b102008-12-11 00:15:01 -08002531#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2532 *descp = desc = move_irq_desc(desc, me);
2533 /* get the new one */
2534 cfg = desc->chip_data;
2535#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002536
Mike Travis22f65d32008-12-16 17:33:56 -08002537 if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2538 send_cleanup_vector(cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002539}
2540#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002541static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002542#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002543
Ingo Molnar54168ed2008-08-20 09:07:45 +02002544#ifdef CONFIG_INTR_REMAP
2545static void ack_x2apic_level(unsigned int irq)
2546{
2547 ack_x2APIC_irq();
2548}
2549
2550static void ack_x2apic_edge(unsigned int irq)
2551{
2552 ack_x2APIC_irq();
2553}
Yinghai Lu3145e942008-12-05 18:58:34 -08002554
Ingo Molnar54168ed2008-08-20 09:07:45 +02002555#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002556
Yinghai Lu1d025192008-08-19 20:50:34 -07002557static void ack_apic_edge(unsigned int irq)
2558{
Yinghai Lu3145e942008-12-05 18:58:34 -08002559 struct irq_desc *desc = irq_to_desc(irq);
2560
2561 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002562 move_native_irq(irq);
2563 ack_APIC_irq();
2564}
2565
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002566atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002567
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002568static void ack_apic_level(unsigned int irq)
2569{
Yinghai Lu3145e942008-12-05 18:58:34 -08002570 struct irq_desc *desc = irq_to_desc(irq);
2571
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002572#ifdef CONFIG_X86_32
2573 unsigned long v;
2574 int i;
2575#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002576 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002577 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002578
Yinghai Lu3145e942008-12-05 18:58:34 -08002579 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002580#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002581 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002582 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002583 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002584 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002585 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002586#endif
2587
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002588#ifdef CONFIG_X86_32
2589 /*
2590 * It appears there is an erratum which affects at least version 0x11
2591 * of I/O APIC (that's the 82093AA and cores integrated into various
2592 * chipsets). Under certain conditions a level-triggered interrupt is
2593 * erroneously delivered as edge-triggered one but the respective IRR
2594 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2595 * message but it will never arrive and further interrupts are blocked
2596 * from the source. The exact reason is so far unknown, but the
2597 * phenomenon was observed when two consecutive interrupt requests
2598 * from a given source get delivered to the same CPU and the source is
2599 * temporarily disabled in between.
2600 *
2601 * A workaround is to simulate an EOI message manually. We achieve it
2602 * by setting the trigger mode to edge and then to level when the edge
2603 * trigger mode gets detected in the TMR of a local APIC for a
2604 * level-triggered interrupt. We mask the source for the time of the
2605 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2606 * The idea is from Manfred Spraul. --macro
2607 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002608 cfg = desc->chip_data;
2609 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002610
2611 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2612#endif
2613
Ingo Molnar54168ed2008-08-20 09:07:45 +02002614 /*
2615 * We must acknowledge the irq before we move it or the acknowledge will
2616 * not propagate properly.
2617 */
2618 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002619
Ingo Molnar54168ed2008-08-20 09:07:45 +02002620 /* Now we can move and renable the irq */
2621 if (unlikely(do_unmask_irq)) {
2622 /* Only migrate the irq if the ack has been received.
2623 *
2624 * On rare occasions the broadcast level triggered ack gets
2625 * delayed going to ioapics, and if we reprogram the
2626 * vector while Remote IRR is still set the irq will never
2627 * fire again.
2628 *
2629 * To prevent this scenario we read the Remote IRR bit
2630 * of the ioapic. This has two effects.
2631 * - On any sane system the read of the ioapic will
2632 * flush writes (and acks) going to the ioapic from
2633 * this cpu.
2634 * - We get to see if the ACK has actually been delivered.
2635 *
2636 * Based on failed experiments of reprogramming the
2637 * ioapic entry from outside of irq context starting
2638 * with masking the ioapic entry and then polling until
2639 * Remote IRR was clear before reprogramming the
2640 * ioapic I don't trust the Remote IRR bit to be
2641 * completey accurate.
2642 *
2643 * However there appears to be no other way to plug
2644 * this race, so if the Remote IRR bit is not
2645 * accurate and is causing problems then it is a hardware bug
2646 * and you can go talk to the chipset vendor about it.
2647 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002648 cfg = desc->chip_data;
2649 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002650 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002651 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002652 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002653
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002654#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002655 if (!(v & (1 << (i & 0x1f)))) {
2656 atomic_inc(&irq_mis_count);
2657 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002658 __mask_and_edge_IO_APIC_irq(cfg);
2659 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002660 spin_unlock(&ioapic_lock);
2661 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002662#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002663}
Yinghai Lu1d025192008-08-19 20:50:34 -07002664
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002665static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002666 .name = "IO-APIC",
2667 .startup = startup_ioapic_irq,
2668 .mask = mask_IO_APIC_irq,
2669 .unmask = unmask_IO_APIC_irq,
2670 .ack = ack_apic_edge,
2671 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002672#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002673 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002674#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002675 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676};
2677
Ingo Molnar54168ed2008-08-20 09:07:45 +02002678#ifdef CONFIG_INTR_REMAP
2679static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002680 .name = "IR-IO-APIC",
2681 .startup = startup_ioapic_irq,
2682 .mask = mask_IO_APIC_irq,
2683 .unmask = unmask_IO_APIC_irq,
2684 .ack = ack_x2apic_edge,
2685 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002686#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002687 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002688#endif
2689 .retrigger = ioapic_retrigger_irq,
2690};
2691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
2693static inline void init_IO_APIC_traps(void)
2694{
2695 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002696 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002697 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
2699 /*
2700 * NOTE! The local APIC isn't very good at handling
2701 * multiple interrupts at the same interrupt level.
2702 * As the interrupt level is determined by taking the
2703 * vector number and shifting that right by 4, we
2704 * want to spread these out a bit so that they don't
2705 * all fall in the same interrupt level.
2706 *
2707 * Also, we've got to be careful not to trash gate
2708 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2709 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002710 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002711 cfg = desc->chip_data;
2712 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /*
2714 * Hmm.. We don't have an entry for this,
2715 * so default to an old-fashioned 8259
2716 * interrupt if we can..
2717 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002718 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002720 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002722 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724 }
2725}
2726
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002727/*
2728 * The local APIC irq-chip implementation:
2729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002731static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 unsigned long v;
2734
2735 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002736 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737}
2738
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002739static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002741 unsigned long v;
2742
2743 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002744 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
Yinghai Lu3145e942008-12-05 18:58:34 -08002747static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002748{
2749 ack_APIC_irq();
2750}
2751
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002752static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002753 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002754 .mask = mask_lapic_irq,
2755 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002756 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757};
2758
Yinghai Lu3145e942008-12-05 18:58:34 -08002759static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002760{
Yinghai Lu08678b02008-08-19 20:50:05 -07002761 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002762 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2763 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002764}
2765
Jan Beuliche9427102008-01-30 13:31:24 +01002766static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
2768 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002769 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 * We put the 8259A master into AEOI mode and
2771 * unmask on all local APICs LVT0 as NMI.
2772 *
2773 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2774 * is from Maciej W. Rozycki - so we do not have to EOI from
2775 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002776 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2778
Jan Beuliche9427102008-01-30 13:31:24 +01002779 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 apic_printk(APIC_VERBOSE, " done.\n");
2782}
2783
2784/*
2785 * This looks a bit hackish but it's about the only one way of sending
2786 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2787 * not support the ExtINT mode, unfortunately. We need to send these
2788 * cycles as some i82489DX-based boards have glue logic that keeps the
2789 * 8259A interrupt line asserted until INTA. --macro
2790 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002791static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002793 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct IO_APIC_route_entry entry0, entry1;
2795 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002797 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002798 if (pin == -1) {
2799 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002801 }
2802 apic = find_isa_irq_apic(8, mp_INT);
2803 if (apic == -1) {
2804 WARN_ON_ONCE(1);
2805 return;
2806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Andi Kleencf4c6a22006-09-26 10:52:30 +02002808 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002809 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 memset(&entry1, 0, sizeof(entry1));
2812
2813 entry1.dest_mode = 0; /* physical delivery */
2814 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002815 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 entry1.delivery_mode = dest_ExtINT;
2817 entry1.polarity = entry0.polarity;
2818 entry1.trigger = 0;
2819 entry1.vector = 0;
2820
Andi Kleencf4c6a22006-09-26 10:52:30 +02002821 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 save_control = CMOS_READ(RTC_CONTROL);
2824 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2825 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2826 RTC_FREQ_SELECT);
2827 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2828
2829 i = 100;
2830 while (i-- > 0) {
2831 mdelay(10);
2832 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2833 i -= 10;
2834 }
2835
2836 CMOS_WRITE(save_control, RTC_CONTROL);
2837 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002838 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Andi Kleencf4c6a22006-09-26 10:52:30 +02002840 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841}
2842
Yinghai Luefa25592008-08-19 20:50:36 -07002843static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002844/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002845static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002846{
2847 disable_timer_pin_1 = 1;
2848 return 0;
2849}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002850early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002851
2852int timer_through_8259 __initdata;
2853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854/*
2855 * This code may look a bit paranoid, but it's supposed to cooperate with
2856 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2857 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2858 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002859 *
2860 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002862static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Yinghai Lu3145e942008-12-05 18:58:34 -08002864 struct irq_desc *desc = irq_to_desc(0);
2865 struct irq_cfg *cfg = desc->chip_data;
2866 int cpu = boot_cpu_id;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002867 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002868 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002869 unsigned int ver;
2870 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002871
2872 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002873
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002874 ver = apic_read(APIC_LVR);
2875 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 /*
2878 * get/set the timer IRQ vector:
2879 */
2880 disable_8259A_irq(0);
Yinghai Lu3145e942008-12-05 18:58:34 -08002881 assign_irq_vector(0, cfg, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002884 * As IRQ0 is to be enabled in the 8259A, the virtual
2885 * wire has to be disabled in the local APIC. Also
2886 * timer interrupts need to be acknowledged manually in
2887 * the 8259A for the i82489DX when using the NMI
2888 * watchdog as that APIC treats NMIs as level-triggered.
2889 * The AEOI mode will finish them in the 8259A
2890 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002892 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002894#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002895 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002898 pin1 = find_isa_irq_pin(0, mp_INT);
2899 apic1 = find_isa_irq_apic(0, mp_INT);
2900 pin2 = ioapic_i8259.pin;
2901 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002903 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2904 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002905 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002907 /*
2908 * Some BIOS writers are clueless and report the ExtINTA
2909 * I/O APIC input from the cascaded 8259A as the timer
2910 * interrupt input. So just in case, if only one pin
2911 * was found above, try it both directly and through the
2912 * 8259A.
2913 */
2914 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002915#ifdef CONFIG_INTR_REMAP
2916 if (intr_remapping_enabled)
2917 panic("BIOS bug: timer not connected to IO-APIC");
2918#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002919 pin1 = pin2;
2920 apic1 = apic2;
2921 no_pin1 = 1;
2922 } else if (pin2 == -1) {
2923 pin2 = pin1;
2924 apic2 = apic1;
2925 }
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (pin1 != -1) {
2928 /*
2929 * Ok, does IRQ0 through the IOAPIC work?
2930 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002931 if (no_pin1) {
Yinghai Lu3145e942008-12-05 18:58:34 -08002932 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002933 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002934 }
Yinghai Lu3145e942008-12-05 18:58:34 -08002935 unmask_IO_APIC_irq_desc(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (timer_irq_works()) {
2937 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 setup_nmi();
2939 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002941 if (disable_timer_pin_1 > 0)
2942 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002943 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002945#ifdef CONFIG_INTR_REMAP
2946 if (intr_remapping_enabled)
2947 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2948#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002949 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002950 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002951 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2952 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002954 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2955 "(IRQ0) through the 8259A ...\n");
2956 apic_printk(APIC_QUIET, KERN_INFO
2957 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /*
2959 * legacy devices should be connected to IO APIC #0
2960 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002961 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002962 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Yinghai Lu3145e942008-12-05 18:58:34 -08002963 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002964 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002966 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002967 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002969 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002971 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975 /*
2976 * Cleanup, just in case ...
2977 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002978 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002979 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002980 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
2983 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002984 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2985 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002986 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002988#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002989 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002990#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002992 apic_printk(APIC_QUIET, KERN_INFO
2993 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Yinghai Lu3145e942008-12-05 18:58:34 -08002995 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002996 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 enable_8259A_irq(0);
2998
2999 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003000 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01003003 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003004 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003005 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003007 apic_printk(APIC_QUIET, KERN_INFO
3008 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 init_8259A(0);
3011 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01003012 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
3014 unlock_ExtINT_logic();
3015
3016 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003017 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003018 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003020 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01003022 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01003023out:
3024 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025}
3026
3027/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003028 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
3029 * to devices. However there may be an I/O APIC pin available for
3030 * this interrupt regardless. The pin may be left unconnected, but
3031 * typically it will be reused as an ExtINT cascade interrupt for
3032 * the master 8259A. In the MPS case such a pin will normally be
3033 * reported as an ExtINT interrupt in the MP table. With ACPI
3034 * there is no provision for ExtINT interrupts, and in the absence
3035 * of an override it would be treated as an ordinary ISA I/O APIC
3036 * interrupt, that is edge-triggered and unmasked by default. We
3037 * used to do this, but it caused problems on some systems because
3038 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
3039 * the same ExtINT cascade interrupt to drive the local APIC of the
3040 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3041 * the I/O APIC in all cases now. No actual device should request
3042 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 */
3044#define PIC_IRQS (1 << PIC_CASCADE_IR)
3045
3046void __init setup_IO_APIC(void)
3047{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003048
3049#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003051#else
3052 /*
3053 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3054 */
3055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003057 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Ingo Molnar54168ed2008-08-20 09:07:45 +02003059 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003060 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003061 * Set up IO-APIC IRQ routing.
3062 */
3063#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003064 if (!acpi_ioapic)
3065 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 sync_Arb_IDs();
3068 setup_IO_APIC_irqs();
3069 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08003070 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
3073/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003074 * Called after all the initialization is done. If we didnt find any
3075 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078static int __init io_apic_bug_finalize(void)
3079{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003080 if (sis_apic_bug == -1)
3081 sis_apic_bug = 0;
3082 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083}
3084
3085late_initcall(io_apic_bug_finalize);
3086
3087struct sysfs_ioapic_data {
3088 struct sys_device dev;
3089 struct IO_APIC_route_entry entry[0];
3090};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003091static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
Pavel Machek438510f2005-04-16 15:25:24 -07003093static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct IO_APIC_route_entry *entry;
3096 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003098
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 data = container_of(dev, struct sysfs_ioapic_data, dev);
3100 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003101 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3102 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
3104 return 0;
3105}
3106
3107static int ioapic_resume(struct sys_device *dev)
3108{
3109 struct IO_APIC_route_entry *entry;
3110 struct sysfs_ioapic_data *data;
3111 unsigned long flags;
3112 union IO_APIC_reg_00 reg_00;
3113 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003114
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 data = container_of(dev, struct sysfs_ioapic_data, dev);
3116 entry = data->entry;
3117
3118 spin_lock_irqsave(&ioapic_lock, flags);
3119 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04003120 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
3121 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 io_apic_write(dev->id, 0, reg_00.raw);
3123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003125 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003126 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
3128 return 0;
3129}
3130
3131static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003132 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 .suspend = ioapic_suspend,
3134 .resume = ioapic_resume,
3135};
3136
3137static int __init ioapic_init_sysfs(void)
3138{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003139 struct sys_device * dev;
3140 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 error = sysdev_class_register(&ioapic_sysdev_class);
3143 if (error)
3144 return error;
3145
Ingo Molnar54168ed2008-08-20 09:07:45 +02003146 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003147 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003149 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 if (!mp_ioapic_data[i]) {
3151 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3152 continue;
3153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003155 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 dev->cls = &ioapic_sysdev_class;
3157 error = sysdev_register(dev);
3158 if (error) {
3159 kfree(mp_ioapic_data[i]);
3160 mp_ioapic_data[i] = NULL;
3161 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3162 continue;
3163 }
3164 }
3165
3166 return 0;
3167}
3168
3169device_initcall(ioapic_init_sysfs);
3170
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003171/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003172 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003173 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003174unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003175{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003176 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003177 unsigned int irq;
3178 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003179 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003180 struct irq_cfg *cfg_new = NULL;
3181 int cpu = boot_cpu_id;
3182 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003183
3184 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003185 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lube5d5352008-12-05 18:58:33 -08003186 for (new = irq_want; new < NR_IRQS; new++) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003187 if (platform_legacy_irq(new))
3188 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003189
3190 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3191 if (!desc_new) {
3192 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003193 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003194 }
3195 cfg_new = desc_new->chip_data;
3196
3197 if (cfg_new->vector != 0)
3198 continue;
Yinghai Lu3145e942008-12-05 18:58:34 -08003199 if (__assign_irq_vector(new, cfg_new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003200 irq = new;
3201 break;
3202 }
3203 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003204
Yinghai Lu199751d2008-08-19 20:50:27 -07003205 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003206 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003207 /* restore it, in case dynamic_irq_init clear it */
3208 if (desc_new)
3209 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003210 }
3211 return irq;
3212}
3213
Yinghai Lube5d5352008-12-05 18:58:33 -08003214static int nr_irqs_gsi = NR_IRQS_LEGACY;
Yinghai Lu199751d2008-08-19 20:50:27 -07003215int create_irq(void)
3216{
Yinghai Lube5d5352008-12-05 18:58:33 -08003217 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003218 int irq;
3219
Yinghai Lube5d5352008-12-05 18:58:33 -08003220 irq_want = nr_irqs_gsi;
3221 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003222
3223 if (irq == 0)
3224 irq = -1;
3225
3226 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003227}
3228
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003229void destroy_irq(unsigned int irq)
3230{
3231 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003232 struct irq_cfg *cfg;
3233 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003234
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003235 /* store it, in case dynamic_irq_cleanup clear it */
3236 desc = irq_to_desc(irq);
3237 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003238 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003239 /* connect back irq_cfg */
3240 if (desc)
3241 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003242
Ingo Molnar54168ed2008-08-20 09:07:45 +02003243#ifdef CONFIG_INTR_REMAP
3244 free_irte(irq);
3245#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003246 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003247 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003248 spin_unlock_irqrestore(&vector_lock, flags);
3249}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003250
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003251/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003252 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003253 */
3254#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003255static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003256{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003257 struct irq_cfg *cfg;
3258 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003259 unsigned dest;
3260
Jan Beulichf1182632009-01-14 12:27:35 +00003261 if (disable_apic)
3262 return -ENXIO;
3263
Yinghai Lu3145e942008-12-05 18:58:34 -08003264 cfg = irq_cfg(irq);
Mike Travis22f65d32008-12-16 17:33:56 -08003265 err = assign_irq_vector(irq, cfg, TARGET_CPUS);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003266 if (err)
3267 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003268
Mike Travis22f65d32008-12-16 17:33:56 -08003269 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003270
Ingo Molnar54168ed2008-08-20 09:07:45 +02003271#ifdef CONFIG_INTR_REMAP
3272 if (irq_remapped(irq)) {
3273 struct irte irte;
3274 int ir_index;
3275 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003276
Ingo Molnar54168ed2008-08-20 09:07:45 +02003277 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3278 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003279
Ingo Molnar54168ed2008-08-20 09:07:45 +02003280 memset (&irte, 0, sizeof(irte));
3281
3282 irte.present = 1;
3283 irte.dst_mode = INT_DEST_MODE;
3284 irte.trigger_mode = 0; /* edge */
3285 irte.dlvry_mode = INT_DELIVERY_MODE;
3286 irte.vector = cfg->vector;
3287 irte.dest_id = IRTE_DEST(dest);
3288
3289 modify_irte(irq, &irte);
3290
3291 msg->address_hi = MSI_ADDR_BASE_HI;
3292 msg->data = sub_handle;
3293 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3294 MSI_ADDR_IR_SHV |
3295 MSI_ADDR_IR_INDEX1(ir_index) |
3296 MSI_ADDR_IR_INDEX2(ir_index);
3297 } else
3298#endif
3299 {
3300 msg->address_hi = MSI_ADDR_BASE_HI;
3301 msg->address_lo =
3302 MSI_ADDR_BASE_LO |
3303 ((INT_DEST_MODE == 0) ?
3304 MSI_ADDR_DEST_MODE_PHYSICAL:
3305 MSI_ADDR_DEST_MODE_LOGICAL) |
3306 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3307 MSI_ADDR_REDIRECTION_CPU:
3308 MSI_ADDR_REDIRECTION_LOWPRI) |
3309 MSI_ADDR_DEST_ID(dest);
3310
3311 msg->data =
3312 MSI_DATA_TRIGGER_EDGE |
3313 MSI_DATA_LEVEL_ASSERT |
3314 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3315 MSI_DATA_DELIVERY_FIXED:
3316 MSI_DATA_DELIVERY_LOWPRI) |
3317 MSI_DATA_VECTOR(cfg->vector);
3318 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003319 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003320}
3321
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003322#ifdef CONFIG_SMP
Rusty Russell0de26522008-12-13 21:20:26 +10303323static void set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003324{
Yinghai Lu3145e942008-12-05 18:58:34 -08003325 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003326 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003327 struct msi_msg msg;
3328 unsigned int dest;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003329
Mike Travis22f65d32008-12-16 17:33:56 -08003330 dest = set_desc_affinity(desc, mask);
3331 if (dest == BAD_APICID)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003332 return;
3333
Yinghai Lu3145e942008-12-05 18:58:34 -08003334 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003335
Yinghai Lu3145e942008-12-05 18:58:34 -08003336 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003337
3338 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003339 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003340 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3341 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3342
Yinghai Lu3145e942008-12-05 18:58:34 -08003343 write_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003344}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003345#ifdef CONFIG_INTR_REMAP
3346/*
3347 * Migrate the MSI irq to another cpumask. This migration is
3348 * done in the process context using interrupt-remapping hardware.
3349 */
Mike Travise7986732008-12-16 17:33:52 -08003350static void
3351ir_set_msi_irq_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003352{
Yinghai Lu3145e942008-12-05 18:58:34 -08003353 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnara7883de2008-12-19 00:59:09 +01003354 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003355 unsigned int dest;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003356 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003357
3358 if (get_irte(irq, &irte))
3359 return;
3360
Mike Travis22f65d32008-12-16 17:33:56 -08003361 dest = set_desc_affinity(desc, mask);
3362 if (dest == BAD_APICID)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003363 return;
3364
Ingo Molnar54168ed2008-08-20 09:07:45 +02003365 irte.vector = cfg->vector;
3366 irte.dest_id = IRTE_DEST(dest);
3367
3368 /*
3369 * atomically update the IRTE with the new destination and vector.
3370 */
3371 modify_irte(irq, &irte);
3372
3373 /*
3374 * After this point, all the interrupts will start arriving
3375 * at the new destination. So, time to cleanup the previous
3376 * vector allocation.
3377 */
Mike Travis22f65d32008-12-16 17:33:56 -08003378 if (cfg->move_in_progress)
3379 send_cleanup_vector(cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003380}
Yinghai Lu3145e942008-12-05 18:58:34 -08003381
Ingo Molnar54168ed2008-08-20 09:07:45 +02003382#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003383#endif /* CONFIG_SMP */
3384
3385/*
3386 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3387 * which implement the MSI or MSI-X Capability Structure.
3388 */
3389static struct irq_chip msi_chip = {
3390 .name = "PCI-MSI",
3391 .unmask = unmask_msi_irq,
3392 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003393 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003394#ifdef CONFIG_SMP
3395 .set_affinity = set_msi_irq_affinity,
3396#endif
3397 .retrigger = ioapic_retrigger_irq,
3398};
3399
Ingo Molnar54168ed2008-08-20 09:07:45 +02003400#ifdef CONFIG_INTR_REMAP
3401static struct irq_chip msi_ir_chip = {
3402 .name = "IR-PCI-MSI",
3403 .unmask = unmask_msi_irq,
3404 .mask = mask_msi_irq,
3405 .ack = ack_x2apic_edge,
3406#ifdef CONFIG_SMP
3407 .set_affinity = ir_set_msi_irq_affinity,
3408#endif
3409 .retrigger = ioapic_retrigger_irq,
3410};
3411
3412/*
3413 * Map the PCI dev to the corresponding remapping hardware unit
3414 * and allocate 'nvec' consecutive interrupt-remapping table entries
3415 * in it.
3416 */
3417static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3418{
3419 struct intel_iommu *iommu;
3420 int index;
3421
3422 iommu = map_dev_to_ir(dev);
3423 if (!iommu) {
3424 printk(KERN_ERR
3425 "Unable to map PCI %s to iommu\n", pci_name(dev));
3426 return -ENOENT;
3427 }
3428
3429 index = alloc_irte(iommu, irq, nvec);
3430 if (index < 0) {
3431 printk(KERN_ERR
3432 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003433 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003434 return -ENOSPC;
3435 }
3436 return index;
3437}
3438#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003439
Yinghai Lu3145e942008-12-05 18:58:34 -08003440static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003441{
3442 int ret;
3443 struct msi_msg msg;
3444
3445 ret = msi_compose_msg(dev, irq, &msg);
3446 if (ret < 0)
3447 return ret;
3448
Yinghai Lu3145e942008-12-05 18:58:34 -08003449 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003450 write_msi_msg(irq, &msg);
3451
Ingo Molnar54168ed2008-08-20 09:07:45 +02003452#ifdef CONFIG_INTR_REMAP
3453 if (irq_remapped(irq)) {
3454 struct irq_desc *desc = irq_to_desc(irq);
3455 /*
3456 * irq migration in process context
3457 */
3458 desc->status |= IRQ_MOVE_PCNTXT;
3459 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3460 } else
3461#endif
3462 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003463
Yinghai Luc81bba42008-09-25 11:53:11 -07003464 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3465
Yinghai Lu1d025192008-08-19 20:50:34 -07003466 return 0;
3467}
3468
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003469int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003470{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003471 unsigned int irq;
3472 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003473 unsigned int irq_want;
3474
Yinghai Lube5d5352008-12-05 18:58:33 -08003475 irq_want = nr_irqs_gsi;
Yinghai Lu199751d2008-08-19 20:50:27 -07003476 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003477 if (irq == 0)
3478 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003479
Ingo Molnar54168ed2008-08-20 09:07:45 +02003480#ifdef CONFIG_INTR_REMAP
3481 if (!intr_remapping_enabled)
3482 goto no_ir;
3483
3484 ret = msi_alloc_irte(dev, irq, 1);
3485 if (ret < 0)
3486 goto error;
3487no_ir:
3488#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003489 ret = setup_msi_irq(dev, msidesc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003490 if (ret < 0) {
3491 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003492 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003493 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003494 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003495
3496#ifdef CONFIG_INTR_REMAP
3497error:
3498 destroy_irq(irq);
3499 return ret;
3500#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003501}
3502
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003503int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3504{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003505 unsigned int irq;
3506 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003507 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003508 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003509
Ingo Molnar54168ed2008-08-20 09:07:45 +02003510#ifdef CONFIG_INTR_REMAP
3511 struct intel_iommu *iommu = 0;
3512 int index = 0;
3513#endif
3514
Yinghai Lube5d5352008-12-05 18:58:33 -08003515 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003516 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003517 list_for_each_entry(msidesc, &dev->msi_list, list) {
3518 irq = create_irq_nr(irq_want);
Yinghai Lube5d5352008-12-05 18:58:33 -08003519 irq_want++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003520 if (irq == 0)
3521 return -1;
3522#ifdef CONFIG_INTR_REMAP
3523 if (!intr_remapping_enabled)
3524 goto no_ir;
3525
3526 if (!sub_handle) {
3527 /*
3528 * allocate the consecutive block of IRTE's
3529 * for 'nvec'
3530 */
3531 index = msi_alloc_irte(dev, irq, nvec);
3532 if (index < 0) {
3533 ret = index;
3534 goto error;
3535 }
3536 } else {
3537 iommu = map_dev_to_ir(dev);
3538 if (!iommu) {
3539 ret = -ENOENT;
3540 goto error;
3541 }
3542 /*
3543 * setup the mapping between the irq and the IRTE
3544 * base index, the sub_handle pointing to the
3545 * appropriate interrupt remap table entry.
3546 */
3547 set_irte_irq(irq, iommu, index, sub_handle);
3548 }
3549no_ir:
3550#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003551 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003552 if (ret < 0)
3553 goto error;
3554 sub_handle++;
3555 }
3556 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003557
3558error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003559 destroy_irq(irq);
3560 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003561}
3562
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003563void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003564{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003565 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003566}
3567
Ingo Molnar54168ed2008-08-20 09:07:45 +02003568#ifdef CONFIG_DMAR
3569#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -08003570static void dmar_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003571{
Yinghai Lu3145e942008-12-05 18:58:34 -08003572 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003573 struct irq_cfg *cfg;
3574 struct msi_msg msg;
3575 unsigned int dest;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003576
Mike Travis22f65d32008-12-16 17:33:56 -08003577 dest = set_desc_affinity(desc, mask);
3578 if (dest == BAD_APICID)
Ingo Molnar54168ed2008-08-20 09:07:45 +02003579 return;
3580
Yinghai Lu3145e942008-12-05 18:58:34 -08003581 cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003582
3583 dmar_msi_read(irq, &msg);
3584
3585 msg.data &= ~MSI_DATA_VECTOR_MASK;
3586 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3587 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3588 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3589
3590 dmar_msi_write(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003591}
Yinghai Lu3145e942008-12-05 18:58:34 -08003592
Ingo Molnar54168ed2008-08-20 09:07:45 +02003593#endif /* CONFIG_SMP */
3594
3595struct irq_chip dmar_msi_type = {
3596 .name = "DMAR_MSI",
3597 .unmask = dmar_msi_unmask,
3598 .mask = dmar_msi_mask,
3599 .ack = ack_apic_edge,
3600#ifdef CONFIG_SMP
3601 .set_affinity = dmar_msi_set_affinity,
3602#endif
3603 .retrigger = ioapic_retrigger_irq,
3604};
3605
3606int arch_setup_dmar_msi(unsigned int irq)
3607{
3608 int ret;
3609 struct msi_msg msg;
3610
3611 ret = msi_compose_msg(NULL, irq, &msg);
3612 if (ret < 0)
3613 return ret;
3614 dmar_msi_write(irq, &msg);
3615 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3616 "edge");
3617 return 0;
3618}
3619#endif
3620
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003621#ifdef CONFIG_HPET_TIMER
3622
3623#ifdef CONFIG_SMP
Mike Travis22f65d32008-12-16 17:33:56 -08003624static void hpet_msi_set_affinity(unsigned int irq, const struct cpumask *mask)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003625{
Yinghai Lu3145e942008-12-05 18:58:34 -08003626 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003627 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003628 struct msi_msg msg;
3629 unsigned int dest;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003630
Mike Travis22f65d32008-12-16 17:33:56 -08003631 dest = set_desc_affinity(desc, mask);
3632 if (dest == BAD_APICID)
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003633 return;
3634
Yinghai Lu3145e942008-12-05 18:58:34 -08003635 cfg = desc->chip_data;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003636
3637 hpet_msi_read(irq, &msg);
3638
3639 msg.data &= ~MSI_DATA_VECTOR_MASK;
3640 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3641 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3642 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3643
3644 hpet_msi_write(irq, &msg);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003645}
Yinghai Lu3145e942008-12-05 18:58:34 -08003646
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003647#endif /* CONFIG_SMP */
3648
3649struct irq_chip hpet_msi_type = {
3650 .name = "HPET_MSI",
3651 .unmask = hpet_msi_unmask,
3652 .mask = hpet_msi_mask,
3653 .ack = ack_apic_edge,
3654#ifdef CONFIG_SMP
3655 .set_affinity = hpet_msi_set_affinity,
3656#endif
3657 .retrigger = ioapic_retrigger_irq,
3658};
3659
3660int arch_setup_hpet_msi(unsigned int irq)
3661{
3662 int ret;
3663 struct msi_msg msg;
3664
3665 ret = msi_compose_msg(NULL, irq, &msg);
3666 if (ret < 0)
3667 return ret;
3668
3669 hpet_msi_write(irq, &msg);
3670 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3671 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003672
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003673 return 0;
3674}
3675#endif
3676
Ingo Molnar54168ed2008-08-20 09:07:45 +02003677#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003678/*
3679 * Hypertransport interrupt support
3680 */
3681#ifdef CONFIG_HT_IRQ
3682
3683#ifdef CONFIG_SMP
3684
Yinghai Lu497c9a12008-08-19 20:50:28 -07003685static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003686{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003687 struct ht_irq_msg msg;
3688 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003689
Yinghai Lu497c9a12008-08-19 20:50:28 -07003690 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003691 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003692
Yinghai Lu497c9a12008-08-19 20:50:28 -07003693 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003694 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003695
Eric W. Biedermanec683072006-11-08 17:44:57 -08003696 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003697}
3698
Mike Travis22f65d32008-12-16 17:33:56 -08003699static void set_ht_irq_affinity(unsigned int irq, const struct cpumask *mask)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003700{
Yinghai Lu3145e942008-12-05 18:58:34 -08003701 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003702 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003703 unsigned int dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003704
Mike Travis22f65d32008-12-16 17:33:56 -08003705 dest = set_desc_affinity(desc, mask);
3706 if (dest == BAD_APICID)
Yinghai Lu497c9a12008-08-19 20:50:28 -07003707 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003708
Yinghai Lu3145e942008-12-05 18:58:34 -08003709 cfg = desc->chip_data;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003710
Yinghai Lu497c9a12008-08-19 20:50:28 -07003711 target_ht_irq(irq, dest, cfg->vector);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003712}
Yinghai Lu3145e942008-12-05 18:58:34 -08003713
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003714#endif
3715
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003716static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003717 .name = "PCI-HT",
3718 .mask = mask_ht_irq,
3719 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003720 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003721#ifdef CONFIG_SMP
3722 .set_affinity = set_ht_irq_affinity,
3723#endif
3724 .retrigger = ioapic_retrigger_irq,
3725};
3726
3727int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3728{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003729 struct irq_cfg *cfg;
3730 int err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003731
Jan Beulichf1182632009-01-14 12:27:35 +00003732 if (disable_apic)
3733 return -ENXIO;
3734
Yinghai Lu3145e942008-12-05 18:58:34 -08003735 cfg = irq_cfg(irq);
Mike Travise7986732008-12-16 17:33:52 -08003736 err = assign_irq_vector(irq, cfg, TARGET_CPUS);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003737 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003738 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003739 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003740
Mike Travis22f65d32008-12-16 17:33:56 -08003741 dest = cpu_mask_to_apicid_and(cfg->domain, TARGET_CPUS);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003742
Eric W. Biedermanec683072006-11-08 17:44:57 -08003743 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003744
Eric W. Biedermanec683072006-11-08 17:44:57 -08003745 msg.address_lo =
3746 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003747 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003748 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003749 ((INT_DEST_MODE == 0) ?
3750 HT_IRQ_LOW_DM_PHYSICAL :
3751 HT_IRQ_LOW_DM_LOGICAL) |
3752 HT_IRQ_LOW_RQEOI_EDGE |
3753 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3754 HT_IRQ_LOW_MT_FIXED :
3755 HT_IRQ_LOW_MT_ARBITRATED) |
3756 HT_IRQ_LOW_IRQ_MASKED;
3757
Eric W. Biedermanec683072006-11-08 17:44:57 -08003758 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003759
Ingo Molnara460e742006-10-17 00:10:03 -07003760 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3761 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003762
3763 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003764 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003765 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003766}
3767#endif /* CONFIG_HT_IRQ */
3768
Dean Nelson4173a0e2008-10-02 12:18:21 -05003769#ifdef CONFIG_X86_64
3770/*
3771 * Re-target the irq to the specified CPU and enable the specified MMR located
3772 * on the specified blade to allow the sending of MSIs to the specified CPU.
3773 */
3774int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3775 unsigned long mmr_offset)
3776{
Mike Travis22f65d32008-12-16 17:33:56 -08003777 const struct cpumask *eligible_cpu = cpumask_of(cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003778 struct irq_cfg *cfg;
3779 int mmr_pnode;
3780 unsigned long mmr_value;
3781 struct uv_IO_APIC_route_entry *entry;
3782 unsigned long flags;
3783 int err;
3784
Yinghai Lu3145e942008-12-05 18:58:34 -08003785 cfg = irq_cfg(irq);
3786
Mike Travise7986732008-12-16 17:33:52 -08003787 err = assign_irq_vector(irq, cfg, eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003788 if (err != 0)
3789 return err;
3790
3791 spin_lock_irqsave(&vector_lock, flags);
3792 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3793 irq_name);
3794 spin_unlock_irqrestore(&vector_lock, flags);
3795
Dean Nelson4173a0e2008-10-02 12:18:21 -05003796 mmr_value = 0;
3797 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3798 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3799
3800 entry->vector = cfg->vector;
3801 entry->delivery_mode = INT_DELIVERY_MODE;
3802 entry->dest_mode = INT_DEST_MODE;
3803 entry->polarity = 0;
3804 entry->trigger = 0;
3805 entry->mask = 0;
Mike Travise7986732008-12-16 17:33:52 -08003806 entry->dest = cpu_mask_to_apicid(eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003807
3808 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3809 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3810
3811 return irq;
3812}
3813
3814/*
3815 * Disable the specified MMR located on the specified blade so that MSIs are
3816 * longer allowed to be sent.
3817 */
3818void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3819{
3820 unsigned long mmr_value;
3821 struct uv_IO_APIC_route_entry *entry;
3822 int mmr_pnode;
3823
3824 mmr_value = 0;
3825 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3826 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3827
3828 entry->mask = 1;
3829
3830 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3831 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3832}
3833#endif /* CONFIG_X86_64 */
3834
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003835int __init io_apic_get_redir_entries (int ioapic)
3836{
3837 union IO_APIC_reg_01 reg_01;
3838 unsigned long flags;
3839
3840 spin_lock_irqsave(&ioapic_lock, flags);
3841 reg_01.raw = io_apic_read(ioapic, 1);
3842 spin_unlock_irqrestore(&ioapic_lock, flags);
3843
3844 return reg_01.bits.entries;
3845}
3846
Yinghai Lube5d5352008-12-05 18:58:33 -08003847void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003848{
Yinghai Lube5d5352008-12-05 18:58:33 -08003849 int idx;
3850 int nr = 0;
3851
3852 for (idx = 0; idx < nr_ioapics; idx++)
3853 nr += io_apic_get_redir_entries(idx) + 1;
3854
3855 if (nr > nr_irqs_gsi)
3856 nr_irqs_gsi = nr;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003857}
3858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003860 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 -------------------------------------------------------------------------- */
3862
Len Brown888ba6c2005-08-24 12:07:20 -04003863#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Ingo Molnar54168ed2008-08-20 09:07:45 +02003865#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003866int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
3868 union IO_APIC_reg_00 reg_00;
3869 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3870 physid_mask_t tmp;
3871 unsigned long flags;
3872 int i = 0;
3873
3874 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003875 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3876 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003878 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3880 * advantage of new APIC bus architecture.
3881 */
3882
3883 if (physids_empty(apic_id_map))
3884 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3885
3886 spin_lock_irqsave(&ioapic_lock, flags);
3887 reg_00.raw = io_apic_read(ioapic, 0);
3888 spin_unlock_irqrestore(&ioapic_lock, flags);
3889
3890 if (apic_id >= get_physical_broadcast()) {
3891 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3892 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3893 apic_id = reg_00.bits.ID;
3894 }
3895
3896 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003897 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 * 'stuck on smp_invalidate_needed IPI wait' messages.
3899 */
3900 if (check_apicid_used(apic_id_map, apic_id)) {
3901
3902 for (i = 0; i < get_physical_broadcast(); i++) {
3903 if (!check_apicid_used(apic_id_map, i))
3904 break;
3905 }
3906
3907 if (i == get_physical_broadcast())
3908 panic("Max apic_id exceeded!\n");
3909
3910 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3911 "trying %d\n", ioapic, apic_id, i);
3912
3913 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
3916 tmp = apicid_to_cpu_present(apic_id);
3917 physids_or(apic_id_map, apic_id_map, tmp);
3918
3919 if (reg_00.bits.ID != apic_id) {
3920 reg_00.bits.ID = apic_id;
3921
3922 spin_lock_irqsave(&ioapic_lock, flags);
3923 io_apic_write(ioapic, 0, reg_00.raw);
3924 reg_00.raw = io_apic_read(ioapic, 0);
3925 spin_unlock_irqrestore(&ioapic_lock, flags);
3926
3927 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003928 if (reg_00.bits.ID != apic_id) {
3929 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3930 return -1;
3931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 }
3933
3934 apic_printk(APIC_VERBOSE, KERN_INFO
3935 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3936
3937 return apic_id;
3938}
3939
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003940int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
3942 union IO_APIC_reg_01 reg_01;
3943 unsigned long flags;
3944
3945 spin_lock_irqsave(&ioapic_lock, flags);
3946 reg_01.raw = io_apic_read(ioapic, 1);
3947 spin_unlock_irqrestore(&ioapic_lock, flags);
3948
3949 return reg_01.bits.version;
3950}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Ingo Molnar54168ed2008-08-20 09:07:45 +02003953int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003955 struct irq_desc *desc;
3956 struct irq_cfg *cfg;
3957 int cpu = boot_cpu_id;
3958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003960 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 ioapic);
3962 return -EINVAL;
3963 }
3964
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003965 desc = irq_to_desc_alloc_cpu(irq, cpu);
3966 if (!desc) {
3967 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3968 return 0;
3969 }
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 * IRQs < 16 are already in the irq_2_pin[] map
3973 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003974 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003975 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003976 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Yinghai Lu3145e942008-12-05 18:58:34 -08003979 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
3981 return 0;
3982}
3983
Ingo Molnar54168ed2008-08-20 09:07:45 +02003984
Shaohua Li61fd47e2007-11-17 01:05:28 -05003985int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3986{
3987 int i;
3988
3989 if (skip_ioapic_setup)
3990 return -1;
3991
3992 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04003993 if (mp_irqs[i].mp_irqtype == mp_INT &&
3994 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05003995 break;
3996 if (i >= mp_irq_entries)
3997 return -1;
3998
3999 *trigger = irq_trigger(i);
4000 *polarity = irq_polarity(i);
4001 return 0;
4002}
4003
Len Brown888ba6c2005-08-24 12:07:20 -04004004#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02004005
Yinghai Lu497c9a12008-08-19 20:50:28 -07004006/*
4007 * This function currently is only a helper for the i386 smp boot process where
4008 * we need to reprogram the ioredtbls to cater for the cpus which have come online
4009 * so mask in all cases should simply be TARGET_CPUS
4010 */
4011#ifdef CONFIG_SMP
4012void __init setup_ioapic_dest(void)
4013{
4014 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004015 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004016 struct irq_cfg *cfg;
Mike Travis22f65d32008-12-16 17:33:56 -08004017 const struct cpumask *mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004018
4019 if (skip_ioapic_setup == 1)
4020 return;
4021
4022 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
4023 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4024 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4025 if (irq_entry == -1)
4026 continue;
4027 irq = pin_2_irq(irq_entry, ioapic, pin);
4028
4029 /* setup_IO_APIC_irqs could fail to get vector for some device
4030 * when you have too many devices, because at that time only boot
4031 * cpu is online.
4032 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08004033 desc = irq_to_desc(irq);
4034 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004035 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08004036 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07004037 irq_trigger(irq_entry),
4038 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004039 continue;
4040
4041 }
4042
4043 /*
4044 * Honour affinities which have been set in early boot
4045 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004046 if (desc->status &
4047 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
Mike Travise7986732008-12-16 17:33:52 -08004048 mask = &desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004049 else
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004050 mask = TARGET_CPUS;
4051
4052#ifdef CONFIG_INTR_REMAP
4053 if (intr_remapping_enabled)
Yinghai Lu3145e942008-12-05 18:58:34 -08004054 set_ir_ioapic_affinity_irq_desc(desc, mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004055 else
4056#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08004057 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07004058 }
4059
4060 }
4061}
4062#endif
4063
Ingo Molnar54168ed2008-08-20 09:07:45 +02004064#define IOAPIC_RESOURCE_NAME_SIZE 11
4065
4066static struct resource *ioapic_resources;
4067
4068static struct resource * __init ioapic_setup_resources(void)
4069{
4070 unsigned long n;
4071 struct resource *res;
4072 char *mem;
4073 int i;
4074
4075 if (nr_ioapics <= 0)
4076 return NULL;
4077
4078 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4079 n *= nr_ioapics;
4080
4081 mem = alloc_bootmem(n);
4082 res = (void *)mem;
4083
4084 if (mem != NULL) {
4085 mem += sizeof(struct resource) * nr_ioapics;
4086
4087 for (i = 0; i < nr_ioapics; i++) {
4088 res[i].name = mem;
4089 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4090 sprintf(mem, "IOAPIC %u", i);
4091 mem += IOAPIC_RESOURCE_NAME_SIZE;
4092 }
4093 }
4094
4095 ioapic_resources = res;
4096
4097 return res;
4098}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004099
Yinghai Luf3294a32008-06-27 01:41:56 -07004100void __init ioapic_init_mappings(void)
4101{
4102 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004103 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004104 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004105
Ingo Molnar54168ed2008-08-20 09:07:45 +02004106 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07004107 for (i = 0; i < nr_ioapics; i++) {
4108 if (smp_found_config) {
4109 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004110#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004111 if (!ioapic_phys) {
4112 printk(KERN_ERR
4113 "WARNING: bogus zero IO-APIC "
4114 "address found in MPTABLE, "
4115 "disabling IO/APIC support!\n");
4116 smp_found_config = 0;
4117 skip_ioapic_setup = 1;
4118 goto fake_ioapic_page;
4119 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004120#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004121 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004122#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004123fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004124#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004125 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004126 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004127 ioapic_phys = __pa(ioapic_phys);
4128 }
4129 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004130 apic_printk(APIC_VERBOSE,
4131 "mapped IOAPIC to %08lx (%08lx)\n",
4132 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004133 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004134
Ingo Molnar54168ed2008-08-20 09:07:45 +02004135 if (ioapic_res != NULL) {
4136 ioapic_res->start = ioapic_phys;
4137 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4138 ioapic_res++;
4139 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004140 }
4141}
4142
Ingo Molnar54168ed2008-08-20 09:07:45 +02004143static int __init ioapic_insert_resources(void)
4144{
4145 int i;
4146 struct resource *r = ioapic_resources;
4147
4148 if (!r) {
4149 printk(KERN_ERR
4150 "IO APIC resources could be not be allocated.\n");
4151 return -1;
4152 }
4153
4154 for (i = 0; i < nr_ioapics; i++) {
4155 insert_resource(&iomem_resource, r);
4156 r++;
4157 }
4158
4159 return 0;
4160}
4161
4162/* Insert the IO APIC resources after PCI initialization has occured to handle
4163 * IO APICS that are mapped in on a BAR in PCI space. */
4164late_initcall(ioapic_insert_resources);