blob: 976039377846bc62cba1d1fd840a000a4db17ec3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070028#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mc146818rtc.h>
30#include <linux/compiler.h>
31#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/sysdev.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Ingo Molnar54168ed2008-08-20 09:07:45 +020038#include <linux/jiffies.h> /* time_after() */
Yinghai Lud4057bd2008-08-19 20:50:38 -070039#ifdef CONFIG_ACPI
40#include <acpi/acpi_bus.h>
41#endif
42#include <linux/bootmem.h>
43#include <linux/dmar.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070044#include <linux/hpet.h>
Ashok Raj54d5d422005-09-06 15:16:15 -070045
Yinghai Lud4057bd2008-08-19 20:50:38 -070046#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/io.h>
48#include <asm/smp.h>
49#include <asm/desc.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070050#include <asm/proto.h>
51#include <asm/acpi.h>
52#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070054#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020055#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070056#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070057#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070058#include <asm/setup.h>
Yinghai Lud4057bd2008-08-19 20:50:38 -070059#include <asm/irq_remapping.h>
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -070060#include <asm/hpet.h>
Dean Nelson4173a0e2008-10-02 12:18:21 -050061#include <asm/uv/uv_hub.h>
62#include <asm/uv/uv_irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Yinghai Lu497c9a12008-08-19 20:50:28 -070064#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020066#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010068#define __apicdebuginit(type) static type __init
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/*
Ingo Molnar54168ed2008-08-20 09:07:45 +020071 * Is the SiS APIC rmw bug present ?
72 * -1 = don't know, 0 = no, 1 = yes
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74int sis_apic_bug = -1;
75
Yinghai Luefa25592008-08-19 20:50:36 -070076static DEFINE_SPINLOCK(ioapic_lock);
77static DEFINE_SPINLOCK(vector_lock);
78
Yinghai Luefa25592008-08-19 20:50:36 -070079/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * # of IRQ routing registers
81 */
82int nr_ioapic_registers[MAX_IO_APICS];
83
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040084/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040085struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040086int nr_ioapics;
87
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040088/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040089struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040090
91/* # of MP IRQ source entries */
92int mp_irq_entries;
93
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040094#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
95int mp_bus_id_to_type[MAX_MP_BUSSES];
96#endif
97
98DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
99
Yinghai Luefa25592008-08-19 20:50:36 -0700100int skip_ioapic_setup;
101
Ingo Molnar54168ed2008-08-20 09:07:45 +0200102static int __init parse_noapic(char *str)
Yinghai Luefa25592008-08-19 20:50:36 -0700103{
104 /* disable IO-APIC */
105 disable_ioapic_setup();
106 return 0;
107}
108early_param("noapic", parse_noapic);
Chuck Ebbert66759a02005-09-12 18:49:25 +0200109
Yinghai Lu0f978f42008-08-19 20:50:26 -0700110struct irq_pin_list;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200111
112/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * This is performance-critical, we want to do it O(1)
114 *
115 * the indexing order of this array favors 1:1 mappings
116 * between pins and IRQs.
117 */
118
Yinghai Lu0f978f42008-08-19 20:50:26 -0700119struct irq_pin_list {
120 int apic, pin;
121 struct irq_pin_list *next;
122};
Yinghai Lu301e6192008-08-19 20:50:02 -0700123
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800124static struct irq_pin_list *get_one_free_irq_2_pin(int cpu)
Yinghai Lu0f978f42008-08-19 20:50:26 -0700125{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800126 struct irq_pin_list *pin;
127 int node;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700128
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800129 node = cpu_to_node(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700130
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800131 pin = kzalloc_node(sizeof(*pin), GFP_ATOMIC, node);
132 printk(KERN_DEBUG " alloc irq_2_pin on cpu %d node %d\n", cpu, node);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700133
Yinghai Lu0f978f42008-08-19 20:50:26 -0700134 return pin;
135}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800137struct irq_cfg {
138 struct irq_pin_list *irq_2_pin;
139 cpumask_t domain;
140 cpumask_t old_domain;
141 unsigned move_cleanup_count;
142 u8 vector;
143 u8 move_in_progress : 1;
Yinghai Lu48a1b102008-12-11 00:15:01 -0800144#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
145 u8 move_desc_pending : 1;
146#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800147};
148
149/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
150#ifdef CONFIG_SPARSE_IRQ
151static struct irq_cfg irq_cfgx[] = {
152#else
153static struct irq_cfg irq_cfgx[NR_IRQS] = {
154#endif
155 [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
156 [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
157 [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
158 [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
159 [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
160 [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
161 [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
162 [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
163 [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
164 [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
165 [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
166 [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
167 [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
168 [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
169 [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
170 [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
171};
172
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800173int __init arch_early_irq_init(void)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800174{
175 struct irq_cfg *cfg;
176 struct irq_desc *desc;
177 int count;
178 int i;
179
180 cfg = irq_cfgx;
181 count = ARRAY_SIZE(irq_cfgx);
182
183 for (i = 0; i < count; i++) {
184 desc = irq_to_desc(i);
185 desc->chip_data = &cfg[i];
186 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800187
188 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800189}
190
191#ifdef CONFIG_SPARSE_IRQ
192static struct irq_cfg *irq_cfg(unsigned int irq)
193{
194 struct irq_cfg *cfg = NULL;
195 struct irq_desc *desc;
196
197 desc = irq_to_desc(irq);
198 if (desc)
199 cfg = desc->chip_data;
200
201 return cfg;
202}
203
204static struct irq_cfg *get_one_free_irq_cfg(int cpu)
205{
206 struct irq_cfg *cfg;
207 int node;
208
209 node = cpu_to_node(cpu);
210
211 cfg = kzalloc_node(sizeof(*cfg), GFP_ATOMIC, node);
212 printk(KERN_DEBUG " alloc irq_cfg on cpu %d node %d\n", cpu, node);
213
214 return cfg;
215}
216
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800217int arch_init_chip_data(struct irq_desc *desc, int cpu)
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800218{
219 struct irq_cfg *cfg;
220
221 cfg = desc->chip_data;
222 if (!cfg) {
223 desc->chip_data = get_one_free_irq_cfg(cpu);
224 if (!desc->chip_data) {
225 printk(KERN_ERR "can not alloc irq_cfg\n");
226 BUG_ON(1);
227 }
228 }
Yinghai Lu13a0c3c2008-12-26 02:05:47 -0800229
230 return 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800231}
232
Yinghai Lu48a1b102008-12-11 00:15:01 -0800233#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
234
235static void
236init_copy_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg, int cpu)
237{
238 struct irq_pin_list *old_entry, *head, *tail, *entry;
239
240 cfg->irq_2_pin = NULL;
241 old_entry = old_cfg->irq_2_pin;
242 if (!old_entry)
243 return;
244
245 entry = get_one_free_irq_2_pin(cpu);
246 if (!entry)
247 return;
248
249 entry->apic = old_entry->apic;
250 entry->pin = old_entry->pin;
251 head = entry;
252 tail = entry;
253 old_entry = old_entry->next;
254 while (old_entry) {
255 entry = get_one_free_irq_2_pin(cpu);
256 if (!entry) {
257 entry = head;
258 while (entry) {
259 head = entry->next;
260 kfree(entry);
261 entry = head;
262 }
263 /* still use the old one */
264 return;
265 }
266 entry->apic = old_entry->apic;
267 entry->pin = old_entry->pin;
268 tail->next = entry;
269 tail = entry;
270 old_entry = old_entry->next;
271 }
272
273 tail->next = NULL;
274 cfg->irq_2_pin = head;
275}
276
277static void free_irq_2_pin(struct irq_cfg *old_cfg, struct irq_cfg *cfg)
278{
279 struct irq_pin_list *entry, *next;
280
281 if (old_cfg->irq_2_pin == cfg->irq_2_pin)
282 return;
283
284 entry = old_cfg->irq_2_pin;
285
286 while (entry) {
287 next = entry->next;
288 kfree(entry);
289 entry = next;
290 }
291 old_cfg->irq_2_pin = NULL;
292}
293
294void arch_init_copy_chip_data(struct irq_desc *old_desc,
295 struct irq_desc *desc, int cpu)
296{
297 struct irq_cfg *cfg;
298 struct irq_cfg *old_cfg;
299
300 cfg = get_one_free_irq_cfg(cpu);
301
302 if (!cfg)
303 return;
304
305 desc->chip_data = cfg;
306
307 old_cfg = old_desc->chip_data;
308
309 memcpy(cfg, old_cfg, sizeof(struct irq_cfg));
310
311 init_copy_irq_2_pin(old_cfg, cfg, cpu);
312}
313
314static void free_irq_cfg(struct irq_cfg *old_cfg)
315{
316 kfree(old_cfg);
317}
318
319void arch_free_chip_data(struct irq_desc *old_desc, struct irq_desc *desc)
320{
321 struct irq_cfg *old_cfg, *cfg;
322
323 old_cfg = old_desc->chip_data;
324 cfg = desc->chip_data;
325
326 if (old_cfg == cfg)
327 return;
328
329 if (old_cfg) {
330 free_irq_2_pin(old_cfg, cfg);
331 free_irq_cfg(old_cfg);
332 old_desc->chip_data = NULL;
333 }
334}
335
336static void set_extra_move_desc(struct irq_desc *desc, cpumask_t mask)
337{
338 struct irq_cfg *cfg = desc->chip_data;
339
340 if (!cfg->move_in_progress) {
341 /* it means that domain is not changed */
342 if (!cpus_intersects(desc->affinity, mask))
343 cfg->move_desc_pending = 1;
344 }
345}
346#endif
347
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800348#else
349static struct irq_cfg *irq_cfg(unsigned int irq)
350{
351 return irq < nr_irqs ? irq_cfgx + irq : NULL;
352}
353
354#endif
355
Yinghai Lu48a1b102008-12-11 00:15:01 -0800356#ifndef CONFIG_NUMA_MIGRATE_IRQ_DESC
Yinghai Lu3145e942008-12-05 18:58:34 -0800357static inline void set_extra_move_desc(struct irq_desc *desc, cpumask_t mask)
358{
359}
Yinghai Lu48a1b102008-12-11 00:15:01 -0800360#endif
Yinghai Lu3145e942008-12-05 18:58:34 -0800361
Linus Torvalds130fe052006-11-01 09:11:00 -0800362struct io_apic {
363 unsigned int index;
364 unsigned int unused[3];
365 unsigned int data;
366};
367
368static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
369{
370 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400371 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800372}
373
374static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
375{
376 struct io_apic __iomem *io_apic = io_apic_base(apic);
377 writel(reg, &io_apic->index);
378 return readl(&io_apic->data);
379}
380
381static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
382{
383 struct io_apic __iomem *io_apic = io_apic_base(apic);
384 writel(reg, &io_apic->index);
385 writel(value, &io_apic->data);
386}
387
388/*
389 * Re-write a value: to be used for read-modify-write
390 * cycles where the read already set up the index register.
391 *
392 * Older SiS APIC requires we rewrite the index register
393 */
394static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
395{
Ingo Molnar54168ed2008-08-20 09:07:45 +0200396 struct io_apic __iomem *io_apic = io_apic_base(apic);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +0200397
398 if (sis_apic_bug)
399 writel(reg, &io_apic->index);
Linus Torvalds130fe052006-11-01 09:11:00 -0800400 writel(value, &io_apic->data);
401}
402
Yinghai Lu3145e942008-12-05 18:58:34 -0800403static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700404{
405 struct irq_pin_list *entry;
406 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700407
408 spin_lock_irqsave(&ioapic_lock, flags);
409 entry = cfg->irq_2_pin;
410 for (;;) {
411 unsigned int reg;
412 int pin;
413
414 if (!entry)
415 break;
416 pin = entry->pin;
417 reg = io_apic_read(entry->apic, 0x10 + pin*2);
418 /* Is the remote IRR bit set? */
419 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
420 spin_unlock_irqrestore(&ioapic_lock, flags);
421 return true;
422 }
423 if (!entry->next)
424 break;
425 entry = entry->next;
426 }
427 spin_unlock_irqrestore(&ioapic_lock, flags);
428
429 return false;
430}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700431
Andi Kleencf4c6a22006-09-26 10:52:30 +0200432union entry_union {
433 struct { u32 w1, w2; };
434 struct IO_APIC_route_entry entry;
435};
436
437static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
438{
439 union entry_union eu;
440 unsigned long flags;
441 spin_lock_irqsave(&ioapic_lock, flags);
442 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
443 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
444 spin_unlock_irqrestore(&ioapic_lock, flags);
445 return eu.entry;
446}
447
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800448/*
449 * When we write a new IO APIC routing entry, we need to write the high
450 * word first! If the mask bit in the low word is clear, we will enable
451 * the interrupt, and we need to make sure the entry is fully populated
452 * before that happens.
453 */
Andi Kleend15512f2006-12-07 02:14:07 +0100454static void
455__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
456{
457 union entry_union eu;
458 eu.entry = e;
459 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
460 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
461}
462
Andi Kleencf4c6a22006-09-26 10:52:30 +0200463static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
464{
465 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200466 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100467 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800468 spin_unlock_irqrestore(&ioapic_lock, flags);
469}
470
471/*
472 * When we mask an IO APIC routing entry, we need to write the low
473 * word first, in order to set the mask bit before we change the
474 * high bits!
475 */
476static void ioapic_mask_entry(int apic, int pin)
477{
478 unsigned long flags;
479 union entry_union eu = { .entry.mask = 1 };
480
481 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200482 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
483 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
484 spin_unlock_irqrestore(&ioapic_lock, flags);
485}
486
Yinghai Lu497c9a12008-08-19 20:50:28 -0700487#ifdef CONFIG_SMP
Yinghai Lu3145e942008-12-05 18:58:34 -0800488static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700489{
490 int apic, pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700491 struct irq_pin_list *entry;
Yinghai Lu3145e942008-12-05 18:58:34 -0800492 u8 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700493
Yinghai Lu497c9a12008-08-19 20:50:28 -0700494 entry = cfg->irq_2_pin;
495 for (;;) {
496 unsigned int reg;
497
498 if (!entry)
499 break;
500
501 apic = entry->apic;
502 pin = entry->pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200503#ifdef CONFIG_INTR_REMAP
504 /*
505 * With interrupt-remapping, destination information comes
506 * from interrupt-remapping table entry.
507 */
508 if (!irq_remapped(irq))
509 io_apic_write(apic, 0x11 + pin*2, dest);
510#else
Yinghai Lu497c9a12008-08-19 20:50:28 -0700511 io_apic_write(apic, 0x11 + pin*2, dest);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200512#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -0700513 reg = io_apic_read(apic, 0x10 + pin*2);
514 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
515 reg |= vector;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200516 io_apic_modify(apic, 0x10 + pin*2, reg);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700517 if (!entry->next)
518 break;
519 entry = entry->next;
520 }
521}
Yinghai Luefa25592008-08-19 20:50:36 -0700522
Yinghai Lu3145e942008-12-05 18:58:34 -0800523static int assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask);
Yinghai Luefa25592008-08-19 20:50:36 -0700524
Yinghai Lu3145e942008-12-05 18:58:34 -0800525static void set_ioapic_affinity_irq_desc(struct irq_desc *desc, cpumask_t mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -0700526{
527 struct irq_cfg *cfg;
528 unsigned long flags;
529 unsigned int dest;
530 cpumask_t tmp;
Yinghai Lu3145e942008-12-05 18:58:34 -0800531 unsigned int irq;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700532
Yinghai Lu497c9a12008-08-19 20:50:28 -0700533 cpus_and(tmp, mask, cpu_online_map);
534 if (cpus_empty(tmp))
535 return;
536
Yinghai Lu3145e942008-12-05 18:58:34 -0800537 irq = desc->irq;
538 cfg = desc->chip_data;
539 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -0700540 return;
541
Yinghai Lu3145e942008-12-05 18:58:34 -0800542 set_extra_move_desc(desc, mask);
543
Yinghai Lu497c9a12008-08-19 20:50:28 -0700544 cpus_and(tmp, cfg->domain, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -0700545 dest = cpu_mask_to_apicid(tmp);
546 /*
547 * Only the high 8 bits are valid.
548 */
549 dest = SET_APIC_LOGICAL_ID(dest);
550
551 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800552 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200553 desc->affinity = mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700554 spin_unlock_irqrestore(&ioapic_lock, flags);
555}
Yinghai Lu3145e942008-12-05 18:58:34 -0800556
557static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
558{
559 struct irq_desc *desc;
560
561 desc = irq_to_desc(irq);
562
563 set_ioapic_affinity_irq_desc(desc, mask);
564}
Yinghai Lu497c9a12008-08-19 20:50:28 -0700565#endif /* CONFIG_SMP */
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567/*
568 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
569 * shared ISA-space IRQs, so we have to support them. We are super
570 * fast in the common case, and fast for shared ISA-space IRQs.
571 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800572static void add_pin_to_irq_cpu(struct irq_cfg *cfg, int cpu, int apic, int pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700574 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Yinghai Lu0f978f42008-08-19 20:50:26 -0700576 entry = cfg->irq_2_pin;
577 if (!entry) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800578 entry = get_one_free_irq_2_pin(cpu);
579 if (!entry) {
580 printk(KERN_ERR "can not alloc irq_2_pin to add %d - %d\n",
581 apic, pin);
582 return;
583 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700584 cfg->irq_2_pin = entry;
585 entry->apic = apic;
586 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700587 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700589
590 while (entry->next) {
591 /* not again, please */
592 if (entry->apic == apic && entry->pin == pin)
593 return;
594
595 entry = entry->next;
596 }
597
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -0800598 entry->next = get_one_free_irq_2_pin(cpu);
Yinghai Lu0f978f42008-08-19 20:50:26 -0700599 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 entry->apic = apic;
601 entry->pin = pin;
602}
603
604/*
605 * Reroute an IRQ to a different pin.
606 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800607static void __init replace_pin_at_irq_cpu(struct irq_cfg *cfg, int cpu,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 int oldapic, int oldpin,
609 int newapic, int newpin)
610{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700611 struct irq_pin_list *entry = cfg->irq_2_pin;
612 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Yinghai Lu0f978f42008-08-19 20:50:26 -0700614 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (entry->apic == oldapic && entry->pin == oldpin) {
616 entry->apic = newapic;
617 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700618 replaced = 1;
619 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700621 }
622 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700624
625 /* why? call replace before add? */
626 if (!replaced)
Yinghai Lu3145e942008-12-05 18:58:34 -0800627 add_pin_to_irq_cpu(cfg, cpu, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Yinghai Lu3145e942008-12-05 18:58:34 -0800630static inline void io_apic_modify_irq(struct irq_cfg *cfg,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400631 int mask_and, int mask_or,
632 void (*final)(struct irq_pin_list *entry))
633{
634 int pin;
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400635 struct irq_pin_list *entry;
636
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400637 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
638 unsigned int reg;
639 pin = entry->pin;
640 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
641 reg &= mask_and;
642 reg |= mask_or;
643 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
644 if (final)
645 final(entry);
646 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700647}
648
Yinghai Lu3145e942008-12-05 18:58:34 -0800649static void __unmask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400650{
Yinghai Lu3145e942008-12-05 18:58:34 -0800651 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400652}
Yinghai Lu4e738e22008-08-19 20:50:47 -0700653
654#ifdef CONFIG_X86_64
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400655void io_apic_sync(struct irq_pin_list *entry)
Yinghai Lu4e738e22008-08-19 20:50:47 -0700656{
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400657 /*
658 * Synchronize the IO-APIC and the CPU by doing
659 * a dummy read from the IO-APIC
660 */
661 struct io_apic __iomem *io_apic;
662 io_apic = io_apic_base(entry->apic);
Yinghai Lu4e738e22008-08-19 20:50:47 -0700663 readl(&io_apic->data);
664}
665
Yinghai Lu3145e942008-12-05 18:58:34 -0800666static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400667{
Yinghai Lu3145e942008-12-05 18:58:34 -0800668 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400669}
670#else /* CONFIG_X86_32 */
Yinghai Lu3145e942008-12-05 18:58:34 -0800671static void __mask_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400672{
Yinghai Lu3145e942008-12-05 18:58:34 -0800673 io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, NULL);
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400674}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700675
Yinghai Lu3145e942008-12-05 18:58:34 -0800676static void __mask_and_edge_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400677{
Yinghai Lu3145e942008-12-05 18:58:34 -0800678 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_LEVEL_TRIGGER,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400679 IO_APIC_REDIR_MASKED, NULL);
680}
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700681
Yinghai Lu3145e942008-12-05 18:58:34 -0800682static void __unmask_and_level_IO_APIC_irq(struct irq_cfg *cfg)
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400683{
Yinghai Lu3145e942008-12-05 18:58:34 -0800684 io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED,
Cyrill Gorcunov87783be2008-09-10 22:19:50 +0400685 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
686}
687#endif /* CONFIG_X86_32 */
Yinghai Lu047c8fd2008-08-19 20:50:41 -0700688
Yinghai Lu3145e942008-12-05 18:58:34 -0800689static void mask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Yinghai Lu3145e942008-12-05 18:58:34 -0800691 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 unsigned long flags;
693
Yinghai Lu3145e942008-12-05 18:58:34 -0800694 BUG_ON(!cfg);
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800697 __mask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 spin_unlock_irqrestore(&ioapic_lock, flags);
699}
700
Yinghai Lu3145e942008-12-05 18:58:34 -0800701static void unmask_IO_APIC_irq_desc(struct irq_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Yinghai Lu3145e942008-12-05 18:58:34 -0800703 struct irq_cfg *cfg = desc->chip_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 unsigned long flags;
705
706 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -0800707 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 spin_unlock_irqrestore(&ioapic_lock, flags);
709}
710
Yinghai Lu3145e942008-12-05 18:58:34 -0800711static void mask_IO_APIC_irq(unsigned int irq)
712{
713 struct irq_desc *desc = irq_to_desc(irq);
714
715 mask_IO_APIC_irq_desc(desc);
716}
717static void unmask_IO_APIC_irq(unsigned int irq)
718{
719 struct irq_desc *desc = irq_to_desc(irq);
720
721 unmask_IO_APIC_irq_desc(desc);
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
725{
726 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200729 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 if (entry.delivery_mode == dest_SMI)
731 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /*
733 * Disable it in the IO-APIC irq-routing table:
734 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800735 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Ingo Molnar54168ed2008-08-20 09:07:45 +0200738static void clear_IO_APIC (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
740 int apic, pin;
741
742 for (apic = 0; apic < nr_ioapics; apic++)
743 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
744 clear_IO_APIC_pin(apic, pin);
745}
746
Ingo Molnar54168ed2008-08-20 09:07:45 +0200747#if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
Harvey Harrison75604d72008-01-30 13:31:17 +0100748void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
750 unsigned int cfg;
751
752 /*
753 * Wait for idle.
754 */
755 apic_wait_icr_idle();
756 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
757 /*
758 * Send the IPI. The write to APIC_ICR fires this off.
759 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100760 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200762#endif /* !CONFIG_SMP && CONFIG_X86_32*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Ingo Molnar54168ed2008-08-20 09:07:45 +0200764#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765/*
766 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
767 * specific CPU-side IRQs.
768 */
769
770#define MAX_PIRQS 8
771static int pirq_entries [MAX_PIRQS];
772static int pirqs_enabled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774static int __init ioapic_pirq_setup(char *str)
775{
776 int i, max;
777 int ints[MAX_PIRQS+1];
778
779 get_options(str, ARRAY_SIZE(ints), ints);
780
781 for (i = 0; i < MAX_PIRQS; i++)
782 pirq_entries[i] = -1;
783
784 pirqs_enabled = 1;
785 apic_printk(APIC_VERBOSE, KERN_INFO
786 "PIRQ redirection, working around broken MP-BIOS.\n");
787 max = MAX_PIRQS;
788 if (ints[0] < MAX_PIRQS)
789 max = ints[0];
790
791 for (i = 0; i < max; i++) {
792 apic_printk(APIC_VERBOSE, KERN_DEBUG
793 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
794 /*
795 * PIRQs are mapped upside down, usually.
796 */
797 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
798 }
799 return 1;
800}
801
802__setup("pirq=", ioapic_pirq_setup);
Ingo Molnar54168ed2008-08-20 09:07:45 +0200803#endif /* CONFIG_X86_32 */
804
805#ifdef CONFIG_INTR_REMAP
806/* I/O APIC RTE contents at the OS boot up */
807static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
808
809/*
810 * Saves and masks all the unmasked IO-APIC RTE's
811 */
812int save_mask_IO_APIC_setup(void)
813{
814 union IO_APIC_reg_01 reg_01;
815 unsigned long flags;
816 int apic, pin;
817
818 /*
819 * The number of IO-APIC IRQ registers (== #pins):
820 */
821 for (apic = 0; apic < nr_ioapics; apic++) {
822 spin_lock_irqsave(&ioapic_lock, flags);
823 reg_01.raw = io_apic_read(apic, 1);
824 spin_unlock_irqrestore(&ioapic_lock, flags);
825 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
826 }
827
828 for (apic = 0; apic < nr_ioapics; apic++) {
829 early_ioapic_entries[apic] =
830 kzalloc(sizeof(struct IO_APIC_route_entry) *
831 nr_ioapic_registers[apic], GFP_KERNEL);
832 if (!early_ioapic_entries[apic])
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400833 goto nomem;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200834 }
835
836 for (apic = 0; apic < nr_ioapics; apic++)
837 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
838 struct IO_APIC_route_entry entry;
839
840 entry = early_ioapic_entries[apic][pin] =
841 ioapic_read_entry(apic, pin);
842 if (!entry.mask) {
843 entry.mask = 1;
844 ioapic_write_entry(apic, pin, entry);
845 }
846 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400847
Ingo Molnar54168ed2008-08-20 09:07:45 +0200848 return 0;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400849
850nomem:
Cyrill Gorcunovc1370b42008-09-23 23:00:02 +0400851 while (apic >= 0)
852 kfree(early_ioapic_entries[apic--]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400853 memset(early_ioapic_entries, 0,
854 ARRAY_SIZE(early_ioapic_entries));
855
856 return -ENOMEM;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200857}
858
859void restore_IO_APIC_setup(void)
860{
861 int apic, pin;
862
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400863 for (apic = 0; apic < nr_ioapics; apic++) {
864 if (!early_ioapic_entries[apic])
865 break;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200866 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
867 ioapic_write_entry(apic, pin,
868 early_ioapic_entries[apic][pin]);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +0400869 kfree(early_ioapic_entries[apic]);
870 early_ioapic_entries[apic] = NULL;
871 }
Ingo Molnar54168ed2008-08-20 09:07:45 +0200872}
873
874void reinit_intr_remapped_IO_APIC(int intr_remapping)
875{
876 /*
877 * for now plain restore of previous settings.
878 * TBD: In the case of OS enabling interrupt-remapping,
879 * IO-APIC RTE's need to be setup to point to interrupt-remapping
880 * table entries. for now, do a plain restore, and wait for
881 * the setup_IO_APIC_irqs() to do proper initialization.
882 */
883 restore_IO_APIC_setup();
884}
885#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887/*
888 * Find the IRQ entry number of a certain pin.
889 */
890static int find_irq_entry(int apic, int pin, int type)
891{
892 int i;
893
894 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400895 if (mp_irqs[i].mp_irqtype == type &&
896 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
897 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
898 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return i;
900
901 return -1;
902}
903
904/*
905 * Find the pin to which IRQ[irq] (ISA) is connected
906 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800907static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
909 int i;
910
911 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400912 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300914 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400915 (mp_irqs[i].mp_irqtype == type) &&
916 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400918 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 return -1;
921}
922
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800923static int __init find_isa_irq_apic(int irq, int type)
924{
925 int i;
926
927 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400928 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800929
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300930 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400931 (mp_irqs[i].mp_irqtype == type) &&
932 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800933 break;
934 }
935 if (i < mp_irq_entries) {
936 int apic;
Ingo Molnar54168ed2008-08-20 09:07:45 +0200937 for(apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400938 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800939 return apic;
940 }
941 }
942
943 return -1;
944}
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946/*
947 * Find a specific PCI IRQ entry.
948 * Not an __init, possibly needed by modules
949 */
950static int pin_2_irq(int idx, int apic, int pin);
951
952int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
953{
954 int apic, i, best_guess = -1;
955
Ingo Molnar54168ed2008-08-20 09:07:45 +0200956 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
957 bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400958 if (test_bit(bus, mp_bus_not_pci)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200959 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return -1;
961 }
962 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400963 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400966 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
967 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 break;
969
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300970 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400971 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400973 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar54168ed2008-08-20 09:07:45 +0200974 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 if (!(apic || IO_APIC_IRQ(irq)))
977 continue;
978
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400979 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return irq;
981 /*
982 * Use the first all-but-pin matching entry as a
983 * best-guess fuzzy result for broken mptables.
984 */
985 if (best_guess < 0)
986 best_guess = irq;
987 }
988 }
989 return best_guess;
990}
Ingo Molnar54168ed2008-08-20 09:07:45 +0200991
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700992EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300994#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995/*
996 * EISA Edge/Level control register, ELCR
997 */
998static int EISA_ELCR(unsigned int irq)
999{
Yinghai Lu99d093d2008-12-05 18:58:32 -08001000 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 unsigned int port = 0x4d0 + (irq >> 3);
1002 return (inb(port) >> (irq & 7)) & 1;
1003 }
1004 apic_printk(APIC_VERBOSE, KERN_INFO
1005 "Broken MPtable reports ISA irq %d\n", irq);
1006 return 0;
1007}
Ingo Molnar54168ed2008-08-20 09:07:45 +02001008
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001009#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001011/* ISA interrupts are always polarity zero edge triggered,
1012 * when listed as conforming in the MP table. */
1013
1014#define default_ISA_trigger(idx) (0)
1015#define default_ISA_polarity(idx) (0)
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/* EISA interrupts are always polarity zero and can be edge or level
1018 * trigger depending on the ELCR value. If an interrupt is listed as
1019 * EISA conforming in the MP table, that means its trigger type must
1020 * be read in from the ELCR */
1021
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001022#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001023#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025/* PCI interrupts are always polarity one level triggered,
1026 * when listed as conforming in the MP table. */
1027
1028#define default_PCI_trigger(idx) (1)
1029#define default_PCI_polarity(idx) (1)
1030
1031/* MCA interrupts are always polarity zero level triggered,
1032 * when listed as conforming in the MP table. */
1033
1034#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +03001035#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Shaohua Li61fd47e2007-11-17 01:05:28 -05001037static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001039 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int polarity;
1041
1042 /*
1043 * Determine IRQ line polarity (high active or low active):
1044 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001045 switch (mp_irqs[idx].mp_irqflag & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001047 case 0: /* conforms, ie. bus-type dependent polarity */
1048 if (test_bit(bus, mp_bus_not_pci))
1049 polarity = default_ISA_polarity(idx);
1050 else
1051 polarity = default_PCI_polarity(idx);
1052 break;
1053 case 1: /* high active */
1054 {
1055 polarity = 0;
1056 break;
1057 }
1058 case 2: /* reserved */
1059 {
1060 printk(KERN_WARNING "broken BIOS!!\n");
1061 polarity = 1;
1062 break;
1063 }
1064 case 3: /* low active */
1065 {
1066 polarity = 1;
1067 break;
1068 }
1069 default: /* invalid */
1070 {
1071 printk(KERN_WARNING "broken BIOS!!\n");
1072 polarity = 1;
1073 break;
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076 return polarity;
1077}
1078
1079static int MPBIOS_trigger(int idx)
1080{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001081 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 int trigger;
1083
1084 /*
1085 * Determine IRQ trigger mode (edge or level sensitive):
1086 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001087 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001089 case 0: /* conforms, ie. bus-type dependent */
1090 if (test_bit(bus, mp_bus_not_pci))
1091 trigger = default_ISA_trigger(idx);
1092 else
1093 trigger = default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001094#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Ingo Molnar54168ed2008-08-20 09:07:45 +02001095 switch (mp_bus_id_to_type[bus]) {
1096 case MP_BUS_ISA: /* ISA pin */
1097 {
1098 /* set before the switch */
1099 break;
1100 }
1101 case MP_BUS_EISA: /* EISA pin */
1102 {
1103 trigger = default_EISA_trigger(idx);
1104 break;
1105 }
1106 case MP_BUS_PCI: /* PCI pin */
1107 {
1108 /* set before the switch */
1109 break;
1110 }
1111 case MP_BUS_MCA: /* MCA pin */
1112 {
1113 trigger = default_MCA_trigger(idx);
1114 break;
1115 }
1116 default:
1117 {
1118 printk(KERN_WARNING "broken BIOS!!\n");
1119 trigger = 1;
1120 break;
1121 }
1122 }
1123#endif
1124 break;
1125 case 1: /* edge */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001126 {
Ingo Molnar54168ed2008-08-20 09:07:45 +02001127 trigger = 0;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001128 break;
1129 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001130 case 2: /* reserved */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001131 {
1132 printk(KERN_WARNING "broken BIOS!!\n");
1133 trigger = 1;
1134 break;
1135 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001136 case 3: /* level */
1137 {
1138 trigger = 1;
1139 break;
1140 }
1141 default: /* invalid */
1142 {
1143 printk(KERN_WARNING "broken BIOS!!\n");
1144 trigger = 0;
1145 break;
1146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148 return trigger;
1149}
1150
1151static inline int irq_polarity(int idx)
1152{
1153 return MPBIOS_polarity(idx);
1154}
1155
1156static inline int irq_trigger(int idx)
1157{
1158 return MPBIOS_trigger(idx);
1159}
1160
Yinghai Luefa25592008-08-19 20:50:36 -07001161int (*ioapic_renumber_irq)(int ioapic, int irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162static int pin_2_irq(int idx, int apic, int pin)
1163{
1164 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001165 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 /*
1168 * Debugging check, we are in big trouble if this message pops up!
1169 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001170 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1172
Ingo Molnar54168ed2008-08-20 09:07:45 +02001173 if (test_bit(bus, mp_bus_not_pci)) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001174 irq = mp_irqs[idx].mp_srcbusirq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001175 } else {
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001176 /*
1177 * PCI IRQs are mapped in order
1178 */
1179 i = irq = 0;
1180 while (i < apic)
1181 irq += nr_ioapic_registers[i++];
1182 irq += pin;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001183 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001184 * For MPS mode, so far only needed by ES7000 platform
1185 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001186 if (ioapic_renumber_irq)
1187 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Ingo Molnar54168ed2008-08-20 09:07:45 +02001190#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /*
1192 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1193 */
1194 if ((pin >= 16) && (pin <= 23)) {
1195 if (pirq_entries[pin-16] != -1) {
1196 if (!pirq_entries[pin-16]) {
1197 apic_printk(APIC_VERBOSE, KERN_DEBUG
1198 "disabling PIRQ%d\n", pin-16);
1199 } else {
1200 irq = pirq_entries[pin-16];
1201 apic_printk(APIC_VERBOSE, KERN_DEBUG
1202 "using PIRQ%d -> IRQ %d\n",
1203 pin-16, irq);
1204 }
1205 }
1206 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001207#endif
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return irq;
1210}
1211
Yinghai Lu497c9a12008-08-19 20:50:28 -07001212void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001214 /* Used to the online set of cpus does not change
1215 * during assign_irq_vector.
1216 */
1217 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
Yinghai Lu497c9a12008-08-19 20:50:28 -07001220void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001221{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001222 spin_unlock(&vector_lock);
1223}
1224
Yinghai Lu3145e942008-12-05 18:58:34 -08001225static int __assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001226{
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001227 /*
1228 * NOTE! The local APIC isn't very good at handling
1229 * multiple interrupts at the same interrupt level.
1230 * As the interrupt level is determined by taking the
1231 * vector number and shifting that right by 4, we
1232 * want to spread these out a bit so that they don't
1233 * all fall in the same interrupt level.
1234 *
1235 * Also, we've got to be careful not to trash gate
1236 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1237 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001238 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1239 unsigned int old_vector;
1240 int cpu;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001241
Ingo Molnar54168ed2008-08-20 09:07:45 +02001242 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1243 return -EBUSY;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001244
Yinghai Lu3145e942008-12-05 18:58:34 -08001245 /* Only try and allocate irqs on cpus that are present */
1246 cpus_and(mask, mask, cpu_online_map);
1247
Ingo Molnar54168ed2008-08-20 09:07:45 +02001248 old_vector = cfg->vector;
1249 if (old_vector) {
1250 cpumask_t tmp;
1251 cpus_and(tmp, cfg->domain, mask);
1252 if (!cpus_empty(tmp))
1253 return 0;
1254 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001255
Ingo Molnar54168ed2008-08-20 09:07:45 +02001256 for_each_cpu_mask_nr(cpu, mask) {
1257 cpumask_t domain, new_mask;
1258 int new_cpu;
1259 int vector, offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001260
Ingo Molnar54168ed2008-08-20 09:07:45 +02001261 domain = vector_allocation_domain(cpu);
1262 cpus_and(new_mask, domain, cpu_online_map);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001263
Ingo Molnar54168ed2008-08-20 09:07:45 +02001264 vector = current_vector;
1265 offset = current_offset;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001266next:
Ingo Molnar54168ed2008-08-20 09:07:45 +02001267 vector += 8;
1268 if (vector >= first_system_vector) {
1269 /* If we run out of vectors on large boxen, must share them. */
1270 offset = (offset + 1) % 8;
1271 vector = FIRST_DEVICE_VECTOR + offset;
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001272 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001273 if (unlikely(current_vector == vector))
1274 continue;
1275#ifdef CONFIG_X86_64
1276 if (vector == IA32_SYSCALL_VECTOR)
1277 goto next;
1278#else
1279 if (vector == SYSCALL_VECTOR)
1280 goto next;
1281#endif
1282 for_each_cpu_mask_nr(new_cpu, new_mask)
1283 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1284 goto next;
1285 /* Found one! */
1286 current_vector = vector;
1287 current_offset = offset;
1288 if (old_vector) {
1289 cfg->move_in_progress = 1;
1290 cfg->old_domain = cfg->domain;
1291 }
1292 for_each_cpu_mask_nr(new_cpu, new_mask)
1293 per_cpu(vector_irq, new_cpu)[vector] = irq;
1294 cfg->vector = vector;
1295 cfg->domain = domain;
1296 return 0;
1297 }
1298 return -ENOSPC;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001299}
1300
Yinghai Lu3145e942008-12-05 18:58:34 -08001301static int assign_irq_vector(int irq, struct irq_cfg *cfg, cpumask_t mask)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001302{
1303 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001304 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001305
1306 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08001307 err = __assign_irq_vector(irq, cfg, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001308 spin_unlock_irqrestore(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001309 return err;
1310}
1311
Yinghai Lu3145e942008-12-05 18:58:34 -08001312static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001313{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001314 cpumask_t mask;
1315 int cpu, vector;
1316
Yinghai Lu497c9a12008-08-19 20:50:28 -07001317 BUG_ON(!cfg->vector);
1318
1319 vector = cfg->vector;
1320 cpus_and(mask, cfg->domain, cpu_online_map);
1321 for_each_cpu_mask_nr(cpu, mask)
1322 per_cpu(vector_irq, cpu)[vector] = -1;
1323
1324 cfg->vector = 0;
1325 cpus_clear(cfg->domain);
Matthew Wilcox0ca4b6b2008-11-20 14:09:33 -07001326
1327 if (likely(!cfg->move_in_progress))
1328 return;
1329 cpus_and(mask, cfg->old_domain, cpu_online_map);
1330 for_each_cpu_mask_nr(cpu, mask) {
1331 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS;
1332 vector++) {
1333 if (per_cpu(vector_irq, cpu)[vector] != irq)
1334 continue;
1335 per_cpu(vector_irq, cpu)[vector] = -1;
1336 break;
1337 }
1338 }
1339 cfg->move_in_progress = 0;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001340}
1341
1342void __setup_vector_irq(int cpu)
1343{
1344 /* Initialize vector_irq on a new cpu */
1345 /* This function must be called with vector_lock held */
1346 int irq, vector;
1347 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001348 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001349
1350 /* Mark the inuse vectors */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001351 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001352 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001353 if (!cpu_isset(cpu, cfg->domain))
1354 continue;
1355 vector = cfg->vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001356 per_cpu(vector_irq, cpu)[vector] = irq;
1357 }
1358 /* Mark the free vectors */
1359 for (vector = 0; vector < NR_VECTORS; ++vector) {
1360 irq = per_cpu(vector_irq, cpu)[vector];
1361 if (irq < 0)
1362 continue;
1363
1364 cfg = irq_cfg(irq);
1365 if (!cpu_isset(cpu, cfg->domain))
1366 per_cpu(vector_irq, cpu)[vector] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001367 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001368}
Glauber Costa3fde6902008-05-28 20:34:19 -07001369
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001370static struct irq_chip ioapic_chip;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001371#ifdef CONFIG_INTR_REMAP
1372static struct irq_chip ir_ioapic_chip;
1373#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
Ingo Molnar54168ed2008-08-20 09:07:45 +02001375#define IOAPIC_AUTO -1
1376#define IOAPIC_EDGE 0
1377#define IOAPIC_LEVEL 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001379#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07001380static inline int IO_APIC_irq_trigger(int irq)
1381{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001382 int apic, idx, pin;
Yinghai Lu1d025192008-08-19 20:50:34 -07001383
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001384 for (apic = 0; apic < nr_ioapics; apic++) {
1385 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1386 idx = find_irq_entry(apic, pin, mp_INT);
1387 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1388 return irq_trigger(idx);
1389 }
1390 }
1391 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02001392 * nonexistent IRQs are edge default
1393 */
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001394 return 0;
Yinghai Lu1d025192008-08-19 20:50:34 -07001395}
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001396#else
1397static inline int IO_APIC_irq_trigger(int irq)
1398{
Ingo Molnar54168ed2008-08-20 09:07:45 +02001399 return 1;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001400}
1401#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07001402
Yinghai Lu3145e942008-12-05 18:58:34 -08001403static void ioapic_register_intr(int irq, struct irq_desc *desc, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Yinghai Lu199751d2008-08-19 20:50:27 -07001405
Jan Beulich6ebcc002006-06-26 13:56:46 +02001406 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001407 trigger == IOAPIC_LEVEL)
Yinghai Lu08678b02008-08-19 20:50:05 -07001408 desc->status |= IRQ_LEVEL;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001409 else
1410 desc->status &= ~IRQ_LEVEL;
1411
Ingo Molnar54168ed2008-08-20 09:07:45 +02001412#ifdef CONFIG_INTR_REMAP
1413 if (irq_remapped(irq)) {
1414 desc->status |= IRQ_MOVE_PCNTXT;
1415 if (trigger)
1416 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1417 handle_fasteoi_irq,
1418 "fasteoi");
1419 else
1420 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1421 handle_edge_irq, "edge");
1422 return;
1423 }
1424#endif
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001425 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1426 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -07001427 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001428 handle_fasteoi_irq,
1429 "fasteoi");
Yinghai Lu047c8fd2008-08-19 20:50:41 -07001430 else
Ingo Molnara460e742006-10-17 00:10:03 -07001431 set_irq_chip_and_handler_name(irq, &ioapic_chip,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001432 handle_edge_irq, "edge");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001433}
1434
1435static int setup_ioapic_entry(int apic, int irq,
1436 struct IO_APIC_route_entry *entry,
1437 unsigned int destination, int trigger,
1438 int polarity, int vector)
1439{
1440 /*
1441 * add it to the IO-APIC irq-routing table:
1442 */
1443 memset(entry,0,sizeof(*entry));
1444
Ingo Molnar54168ed2008-08-20 09:07:45 +02001445#ifdef CONFIG_INTR_REMAP
1446 if (intr_remapping_enabled) {
1447 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1448 struct irte irte;
1449 struct IR_IO_APIC_route_entry *ir_entry =
1450 (struct IR_IO_APIC_route_entry *) entry;
1451 int index;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001452
Ingo Molnar54168ed2008-08-20 09:07:45 +02001453 if (!iommu)
1454 panic("No mapping iommu for ioapic %d\n", apic);
1455
1456 index = alloc_irte(iommu, irq, 1);
1457 if (index < 0)
1458 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1459
1460 memset(&irte, 0, sizeof(irte));
1461
1462 irte.present = 1;
1463 irte.dst_mode = INT_DEST_MODE;
1464 irte.trigger_mode = trigger;
1465 irte.dlvry_mode = INT_DELIVERY_MODE;
1466 irte.vector = vector;
1467 irte.dest_id = IRTE_DEST(destination);
1468
1469 modify_irte(irq, &irte);
1470
1471 ir_entry->index2 = (index >> 15) & 0x1;
1472 ir_entry->zero = 0;
1473 ir_entry->format = 1;
1474 ir_entry->index = (index & 0x7fff);
1475 } else
1476#endif
1477 {
1478 entry->delivery_mode = INT_DELIVERY_MODE;
1479 entry->dest_mode = INT_DEST_MODE;
1480 entry->dest = destination;
1481 }
1482
1483 entry->mask = 0; /* enable IRQ */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001484 entry->trigger = trigger;
1485 entry->polarity = polarity;
1486 entry->vector = vector;
1487
1488 /* Mask level triggered irqs.
1489 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1490 */
1491 if (trigger)
1492 entry->mask = 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001493 return 0;
1494}
1495
Yinghai Lu3145e942008-12-05 18:58:34 -08001496static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq, struct irq_desc *desc,
Ingo Molnar54168ed2008-08-20 09:07:45 +02001497 int trigger, int polarity)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001498{
1499 struct irq_cfg *cfg;
1500 struct IO_APIC_route_entry entry;
1501 cpumask_t mask;
1502
1503 if (!IO_APIC_IRQ(irq))
1504 return;
1505
Yinghai Lu3145e942008-12-05 18:58:34 -08001506 cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07001507
1508 mask = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08001509 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07001510 return;
1511
1512 cpus_and(mask, cfg->domain, mask);
1513
1514 apic_printk(APIC_VERBOSE,KERN_DEBUG
1515 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1516 "IRQ %d Mode:%i Active:%i)\n",
1517 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1518 irq, trigger, polarity);
1519
1520
1521 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1522 cpu_mask_to_apicid(mask), trigger, polarity,
1523 cfg->vector)) {
1524 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1525 mp_ioapics[apic].mp_apicid, pin);
Yinghai Lu3145e942008-12-05 18:58:34 -08001526 __clear_irq_vector(irq, cfg);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001527 return;
1528 }
1529
Yinghai Lu3145e942008-12-05 18:58:34 -08001530 ioapic_register_intr(irq, desc, trigger);
Yinghai Lu99d093d2008-12-05 18:58:32 -08001531 if (irq < NR_IRQS_LEGACY)
Yinghai Lu497c9a12008-08-19 20:50:28 -07001532 disable_8259A_irq(irq);
1533
1534 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535}
1536
1537static void __init setup_IO_APIC_irqs(void)
1538{
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001539 int apic, pin, idx, irq;
1540 int notcon = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001541 struct irq_desc *desc;
Yinghai Lu3145e942008-12-05 18:58:34 -08001542 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001543 int cpu = boot_cpu_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1546
1547 for (apic = 0; apic < nr_ioapics; apic++) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001548 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001550 idx = find_irq_entry(apic, pin, mp_INT);
1551 if (idx == -1) {
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001552 if (!notcon) {
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001553 notcon = 1;
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001554 apic_printk(APIC_VERBOSE,
1555 KERN_DEBUG " %d-%d",
1556 mp_ioapics[apic].mp_apicid,
1557 pin);
1558 } else
1559 apic_printk(APIC_VERBOSE, " %d-%d",
1560 mp_ioapics[apic].mp_apicid,
1561 pin);
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001562 continue;
1563 }
Cyrill Gorcunov56ffa1a2008-09-13 13:11:16 +04001564 if (notcon) {
1565 apic_printk(APIC_VERBOSE,
1566 " (apicid-pin) not connected\n");
1567 notcon = 0;
1568 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001569
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001570 irq = pin_2_irq(idx, apic, pin);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001571#ifdef CONFIG_X86_32
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001572 if (multi_timer_check(apic, irq))
1573 continue;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001574#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001575 desc = irq_to_desc_alloc_cpu(irq, cpu);
1576 if (!desc) {
1577 printk(KERN_INFO "can not get irq_desc for %d\n", irq);
1578 continue;
1579 }
Yinghai Lu3145e942008-12-05 18:58:34 -08001580 cfg = desc->chip_data;
1581 add_pin_to_irq_cpu(cfg, cpu, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001582
Yinghai Lu3145e942008-12-05 18:58:34 -08001583 setup_IO_APIC_irq(apic, pin, irq, desc,
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001584 irq_trigger(idx), irq_polarity(idx));
1585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
Cyrill Gorcunov3c2cbd22008-09-06 14:15:33 +04001588 if (notcon)
1589 apic_printk(APIC_VERBOSE,
Cyrill Gorcunov2a554fb2008-09-08 19:38:06 +04001590 " (apicid-pin) not connected\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001594 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001596static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1597 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598{
1599 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Ingo Molnar54168ed2008-08-20 09:07:45 +02001601#ifdef CONFIG_INTR_REMAP
1602 if (intr_remapping_enabled)
1603 return;
1604#endif
1605
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001606 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 /*
1609 * We use logical delivery to get the timer IRQ
1610 * to the first CPU.
1611 */
1612 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001613 entry.mask = 1; /* mask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07001614 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 entry.delivery_mode = INT_DELIVERY_MODE;
1616 entry.polarity = 0;
1617 entry.trigger = 0;
1618 entry.vector = vector;
1619
1620 /*
1621 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001622 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02001624 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626 /*
1627 * Add it to the IO-APIC irq-routing table:
1628 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001629 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001632
1633__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 int apic, i;
1636 union IO_APIC_reg_00 reg_00;
1637 union IO_APIC_reg_01 reg_01;
1638 union IO_APIC_reg_02 reg_02;
1639 union IO_APIC_reg_03 reg_03;
1640 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001641 struct irq_cfg *cfg;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001642 struct irq_desc *desc;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001643 unsigned int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
1645 if (apic_verbosity == APIC_QUIET)
1646 return;
1647
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001648 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 for (i = 0; i < nr_ioapics; i++)
1650 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001651 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 /*
1654 * We are a bit conservative about what we expect. We have to
1655 * know about every hardware change ASAP.
1656 */
1657 printk(KERN_INFO "testing the IO APIC.......................\n");
1658
1659 for (apic = 0; apic < nr_ioapics; apic++) {
1660
1661 spin_lock_irqsave(&ioapic_lock, flags);
1662 reg_00.raw = io_apic_read(apic, 0);
1663 reg_01.raw = io_apic_read(apic, 1);
1664 if (reg_01.bits.version >= 0x10)
1665 reg_02.raw = io_apic_read(apic, 2);
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02001666 if (reg_01.bits.version >= 0x20)
1667 reg_03.raw = io_apic_read(apic, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 spin_unlock_irqrestore(&ioapic_lock, flags);
1669
Ingo Molnar54168ed2008-08-20 09:07:45 +02001670 printk("\n");
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001671 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1673 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1674 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1675 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Ingo Molnar54168ed2008-08-20 09:07:45 +02001677 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1681 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
1683 /*
1684 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1685 * but the value of reg_02 is read as the previous read register
1686 * value, so ignore it if reg_02 == reg_01.
1687 */
1688 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1689 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1690 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692
1693 /*
1694 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1695 * or reg_03, but the value of reg_0[23] is read as the previous read
1696 * register value, so ignore it if reg_03 == reg_0[12].
1697 */
1698 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1699 reg_03.raw != reg_01.raw) {
1700 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1701 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
1704 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1705
Yinghai Lud83e94a2008-08-19 20:50:33 -07001706 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1707 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 for (i = 0; i <= reg_01.bits.entries; i++) {
1710 struct IO_APIC_route_entry entry;
1711
Andi Kleencf4c6a22006-09-26 10:52:30 +02001712 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Ingo Molnar54168ed2008-08-20 09:07:45 +02001714 printk(KERN_DEBUG " %02x %03X ",
1715 i,
1716 entry.dest
1717 );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1720 entry.mask,
1721 entry.trigger,
1722 entry.irr,
1723 entry.polarity,
1724 entry.delivery_status,
1725 entry.dest_mode,
1726 entry.delivery_mode,
1727 entry.vector
1728 );
1729 }
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001732 for_each_irq_desc(irq, desc) {
1733 struct irq_pin_list *entry;
1734
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08001735 cfg = desc->chip_data;
1736 entry = cfg->irq_2_pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001737 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 continue;
Yinghai Lu8f09cd22008-08-19 20:50:51 -07001739 printk(KERN_DEBUG "IRQ%d ", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 for (;;) {
1741 printk("-> %d:%d", entry->apic, entry->pin);
1742 if (!entry->next)
1743 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001744 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746 printk("\n");
1747 }
1748
1749 printk(KERN_INFO ".................................... done.\n");
1750
1751 return;
1752}
1753
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001754__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 unsigned int v;
1757 int i, j;
1758
1759 if (apic_verbosity == APIC_QUIET)
1760 return;
1761
1762 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1763 for (i = 0; i < 8; i++) {
1764 v = apic_read(base + i*0x10);
1765 for (j = 0; j < 32; j++) {
1766 if (v & (1<<j))
1767 printk("1");
1768 else
1769 printk("0");
1770 }
1771 printk("\n");
1772 }
1773}
1774
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001775__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
1777 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001778 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 if (apic_verbosity == APIC_QUIET)
1781 return;
1782
1783 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1784 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001785 v = apic_read(APIC_ID);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001786 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 v = apic_read(APIC_LVR);
1788 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1789 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001790 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 v = apic_read(APIC_TASKPRI);
1793 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1794
Ingo Molnar54168ed2008-08-20 09:07:45 +02001795 if (APIC_INTEGRATED(ver)) { /* !82489DX */
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001796 if (!APIC_XAPIC(ver)) {
1797 v = apic_read(APIC_ARBPRI);
1798 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1799 v & APIC_ARBPRI_MASK);
1800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 v = apic_read(APIC_PROCPRI);
1802 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1803 }
1804
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001805 /*
1806 * Remote read supported only in the 82489DX and local APIC for
1807 * Pentium processors.
1808 */
1809 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1810 v = apic_read(APIC_RRR);
1811 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1812 }
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 v = apic_read(APIC_LDR);
1815 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
Yinghai Lua11b5ab2008-09-03 16:58:31 -07001816 if (!x2apic_enabled()) {
1817 v = apic_read(APIC_DFR);
1818 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 v = apic_read(APIC_SPIV);
1821 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1822
1823 printk(KERN_DEBUG "... APIC ISR field:\n");
1824 print_APIC_bitfield(APIC_ISR);
1825 printk(KERN_DEBUG "... APIC TMR field:\n");
1826 print_APIC_bitfield(APIC_TMR);
1827 printk(KERN_DEBUG "... APIC IRR field:\n");
1828 print_APIC_bitfield(APIC_IRR);
1829
Ingo Molnar54168ed2008-08-20 09:07:45 +02001830 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1831 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 apic_write(APIC_ESR, 0);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 v = apic_read(APIC_ESR);
1835 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1836 }
1837
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001838 icr = apic_icr_read();
Ingo Molnar0c425ce2008-08-18 13:04:26 +02001839 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1840 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 v = apic_read(APIC_LVTT);
1843 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1844
1845 if (maxlvt > 3) { /* PC is LVT#4. */
1846 v = apic_read(APIC_LVTPC);
1847 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1848 }
1849 v = apic_read(APIC_LVT0);
1850 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1851 v = apic_read(APIC_LVT1);
1852 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1853
1854 if (maxlvt > 2) { /* ERR is LVT#3. */
1855 v = apic_read(APIC_LVTERR);
1856 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1857 }
1858
1859 v = apic_read(APIC_TMICT);
1860 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1861 v = apic_read(APIC_TMCCT);
1862 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1863 v = apic_read(APIC_TDCR);
1864 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1865 printk("\n");
1866}
1867
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001868__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Yinghai Luffd5aae2008-08-19 20:50:50 -07001870 int cpu;
1871
1872 preempt_disable();
1873 for_each_online_cpu(cpu)
1874 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1875 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876}
1877
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001878__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 unsigned int v;
1881 unsigned long flags;
1882
1883 if (apic_verbosity == APIC_QUIET)
1884 return;
1885
1886 printk(KERN_DEBUG "\nprinting PIC contents\n");
1887
1888 spin_lock_irqsave(&i8259A_lock, flags);
1889
1890 v = inb(0xa1) << 8 | inb(0x21);
1891 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1892
1893 v = inb(0xa0) << 8 | inb(0x20);
1894 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1895
Ingo Molnar54168ed2008-08-20 09:07:45 +02001896 outb(0x0b,0xa0);
1897 outb(0x0b,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 v = inb(0xa0) << 8 | inb(0x20);
Ingo Molnar54168ed2008-08-20 09:07:45 +02001899 outb(0x0a,0xa0);
1900 outb(0x0a,0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
1902 spin_unlock_irqrestore(&i8259A_lock, flags);
1903
1904 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1905
1906 v = inb(0x4d1) << 8 | inb(0x4d0);
1907 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1908}
1909
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001910__apicdebuginit(int) print_all_ICs(void)
1911{
1912 print_PIC();
1913 print_all_local_APICs();
1914 print_IO_APIC();
1915
1916 return 0;
1917}
1918
1919fs_initcall(print_all_ICs);
1920
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Yinghai Luefa25592008-08-19 20:50:36 -07001922/* Where if anywhere is the i8259 connect in external int mode */
1923static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1924
Ingo Molnar54168ed2008-08-20 09:07:45 +02001925void __init enable_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
1927 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001928 int i8259_apic, i8259_pin;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001929 int apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 unsigned long flags;
1931
Ingo Molnar54168ed2008-08-20 09:07:45 +02001932#ifdef CONFIG_X86_32
1933 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 if (!pirqs_enabled)
1935 for (i = 0; i < MAX_PIRQS; i++)
1936 pirq_entries[i] = -1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02001937#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 /*
1940 * The number of IO-APIC IRQ registers (== #pins):
1941 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001942 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001944 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001946 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1947 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02001948 for(apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001949 int pin;
1950 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001951 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001952 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001953 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001954
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001955 /* If the interrupt line is enabled and in ExtInt mode
1956 * I have found the pin where the i8259 is connected.
1957 */
1958 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1959 ioapic_i8259.apic = apic;
1960 ioapic_i8259.pin = pin;
1961 goto found_i8259;
1962 }
1963 }
1964 }
1965 found_i8259:
1966 /* Look to see what if the MP table has reported the ExtINT */
1967 /* If we could not find the appropriate pin by looking at the ioapic
1968 * the i8259 probably is not connected the ioapic but give the
1969 * mptable a chance anyway.
1970 */
1971 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1972 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1973 /* Trust the MP table if nothing is setup in the hardware */
1974 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1975 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1976 ioapic_i8259.pin = i8259_pin;
1977 ioapic_i8259.apic = i8259_apic;
1978 }
1979 /* Complain if the MP table and the hardware disagree */
1980 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1981 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1982 {
1983 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
1985
1986 /*
1987 * Do not trust the IO-APIC being empty at bootup
1988 */
1989 clear_IO_APIC();
1990}
1991
1992/*
1993 * Not an __init, needed by the reboot code
1994 */
1995void disable_IO_APIC(void)
1996{
1997 /*
1998 * Clear the IO-APIC before rebooting:
1999 */
2000 clear_IO_APIC();
2001
Eric W. Biederman650927e2005-06-25 14:57:44 -07002002 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02002003 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07002004 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02002005 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07002006 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002007 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07002008 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07002009
2010 memset(&entry, 0, sizeof(entry));
2011 entry.mask = 0; /* Enabled */
2012 entry.trigger = 0; /* Edge */
2013 entry.irr = 0;
2014 entry.polarity = 0; /* High */
2015 entry.delivery_status = 0;
2016 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002017 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07002018 entry.vector = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002019 entry.dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07002020
2021 /*
2022 * Add it to the IO-APIC irq-routing table:
2023 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02002024 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07002025 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002026
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002027 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Ingo Molnar54168ed2008-08-20 09:07:45 +02002030#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031/*
2032 * function to set the IO-APIC physical IDs based on the
2033 * values stored in the MPC table.
2034 *
2035 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2036 */
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038static void __init setup_ioapic_ids_from_mpc(void)
2039{
2040 union IO_APIC_reg_00 reg_00;
2041 physid_mask_t phys_id_present_map;
2042 int apic;
2043 int i;
2044 unsigned char old_id;
2045 unsigned long flags;
2046
Yinghai Lua4dbc342008-07-25 02:14:28 -07002047 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07002048 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002051 * Don't check I/O APIC IDs for xAPIC systems. They have
2052 * no meaning without the serial APIC bus.
2053 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08002054 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2055 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07002056 return;
2057 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 * This is broken; anything with a real cpu count has to
2059 * circumvent this idiocy regardless.
2060 */
2061 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2062
2063 /*
2064 * Set the IOAPIC ID to the value stored in the MPC table.
2065 */
2066 for (apic = 0; apic < nr_ioapics; apic++) {
2067
2068 /* Read the register 0 value */
2069 spin_lock_irqsave(&ioapic_lock, flags);
2070 reg_00.raw = io_apic_read(apic, 0);
2071 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002072
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002073 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002075 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002077 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2079 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002080 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /*
2084 * Sanity check, is the ID really free? Every APIC in a
2085 * system must have a unique ID or we get lots of nice
2086 * 'stuck on smp_invalidate_needed IPI wait' messages.
2087 */
2088 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002089 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002091 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 for (i = 0; i < get_physical_broadcast(); i++)
2093 if (!physid_isset(i, phys_id_present_map))
2094 break;
2095 if (i >= get_physical_broadcast())
2096 panic("Max APIC ID exceeded!\n");
2097 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2098 i);
2099 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002100 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 } else {
2102 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002103 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 apic_printk(APIC_VERBOSE, "Setting %d in the "
2105 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002106 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2108 }
2109
2110
2111 /*
2112 * We need to adjust the IRQ routing table
2113 * if the ID changed.
2114 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002115 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002117 if (mp_irqs[i].mp_dstapic == old_id)
2118 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002119 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /*
2122 * Read the right value from the MPC table and
2123 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002124 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 apic_printk(APIC_VERBOSE, KERN_INFO
2126 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002127 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002129 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lua2d332f2008-08-21 12:56:32 -07002131 io_apic_write(apic, 0, reg_00.raw);
2132 spin_unlock_irqrestore(&ioapic_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /*
2135 * Sanity check
2136 */
2137 spin_lock_irqsave(&ioapic_lock, flags);
2138 reg_00.raw = io_apic_read(apic, 0);
2139 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002140 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 printk("could not set ID!\n");
2142 else
2143 apic_printk(APIC_VERBOSE, " ok.\n");
2144 }
2145}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002146#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01002148int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01002149
2150static int __init notimercheck(char *s)
2151{
2152 no_timer_check = 1;
2153 return 1;
2154}
2155__setup("no_timer_check", notimercheck);
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157/*
2158 * There is a nasty bug in some older SMP boards, their mptable lies
2159 * about the timer IRQ. We do the following to work around the situation:
2160 *
2161 * - timer IRQ defaults to IO-APIC IRQ
2162 * - if this function detects that timer IRQs are defunct, then we fall
2163 * back to ISA timer IRQs
2164 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02002165static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002168 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Zachary Amsden8542b202006-12-07 02:14:09 +01002170 if (no_timer_check)
2171 return 1;
2172
Ingo Molnar4aae0702007-12-18 18:05:58 +01002173 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 local_irq_enable();
2175 /* Let ten ticks pass... */
2176 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002177 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 /*
2180 * Expect a few ticks at least, to be sure some possible
2181 * glue logic does not lock up after one or two first
2182 * ticks in a non-ExtINT mode. Also the local APIC
2183 * might have cached one ExtINT interrupt. Finally, at
2184 * least one tick may be lost due to delays.
2185 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002186
2187 /* jiffies wrap? */
Julia Lawall1d16b532008-01-30 13:32:19 +01002188 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return 0;
2191}
2192
2193/*
2194 * In the SMP+IOAPIC case it might happen that there are an unspecified
2195 * number of pending IRQ events unhandled. These cases are very rare,
2196 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2197 * better to do it this way as thus we do not have to be aware of
2198 * 'pending' interrupts in the IRQ path, except at this point.
2199 */
2200/*
2201 * Edge triggered needs to resend any interrupt
2202 * that was delayed but this is now handled in the device
2203 * independent code.
2204 */
2205
2206/*
2207 * Starting up a edge-triggered IO-APIC interrupt is
2208 * nasty - we need to make sure that we get the edge.
2209 * If it is already asserted for some reason, we need
2210 * return 1 to indicate that is was pending.
2211 *
2212 * This is not complete - we should be able to fake
2213 * an edge even if it isn't on the 8259A...
2214 */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002215
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002216static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 int was_pending = 0;
2219 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002220 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu99d093d2008-12-05 18:58:32 -08002223 if (irq < NR_IRQS_LEGACY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 disable_8259A_irq(irq);
2225 if (i8259A_irq_pending(irq))
2226 was_pending = 1;
2227 }
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002228 cfg = irq_cfg(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002229 __unmask_IO_APIC_irq(cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 spin_unlock_irqrestore(&ioapic_lock, flags);
2231
2232 return was_pending;
2233}
2234
Ingo Molnar54168ed2008-08-20 09:07:45 +02002235#ifdef CONFIG_X86_64
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002236static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Ingo Molnar54168ed2008-08-20 09:07:45 +02002238
2239 struct irq_cfg *cfg = irq_cfg(irq);
2240 unsigned long flags;
2241
2242 spin_lock_irqsave(&vector_lock, flags);
2243 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2244 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002245
2246 return 1;
2247}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002248#else
2249static int ioapic_retrigger_irq(unsigned int irq)
2250{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002251 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002252
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002253 return 1;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002254}
2255#endif
2256
2257/*
2258 * Level and edge triggered IO-APIC interrupts need different handling,
2259 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2260 * handled with the level-triggered descriptor, but that one has slightly
2261 * more overhead. Level-triggered interrupts cannot be handled with the
2262 * edge-triggered handler, without risking IRQ storms and other ugly
2263 * races.
2264 */
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07002265
Yinghai Lu497c9a12008-08-19 20:50:28 -07002266#ifdef CONFIG_SMP
Ingo Molnar54168ed2008-08-20 09:07:45 +02002267
2268#ifdef CONFIG_INTR_REMAP
2269static void ir_irq_migration(struct work_struct *work);
2270
2271static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2272
2273/*
2274 * Migrate the IO-APIC irq in the presence of intr-remapping.
2275 *
2276 * For edge triggered, irq migration is a simple atomic update(of vector
2277 * and cpu destination) of IRTE and flush the hardware cache.
2278 *
2279 * For level triggered, we need to modify the io-apic RTE aswell with the update
2280 * vector information, along with modifying IRTE with vector and destination.
2281 * So irq migration for level triggered is little bit more complex compared to
2282 * edge triggered migration. But the good news is, we use the same algorithm
2283 * for level triggered migration as we have today, only difference being,
2284 * we now initiate the irq migration from process context instead of the
2285 * interrupt context.
2286 *
2287 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2288 * suppression) to the IO-APIC, level triggered irq migration will also be
2289 * as simple as edge triggered migration and we can do the irq migration
2290 * with a simple atomic update to IO-APIC RTE.
2291 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002292static void migrate_ioapic_irq_desc(struct irq_desc *desc, cpumask_t mask)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002293{
2294 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002295 cpumask_t tmp, cleanup_mask;
2296 struct irte irte;
2297 int modify_ioapic_rte;
2298 unsigned int dest;
2299 unsigned long flags;
Yinghai Lu3145e942008-12-05 18:58:34 -08002300 unsigned int irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002301
2302 cpus_and(tmp, mask, cpu_online_map);
2303 if (cpus_empty(tmp))
2304 return;
2305
Yinghai Lu3145e942008-12-05 18:58:34 -08002306 irq = desc->irq;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002307 if (get_irte(irq, &irte))
2308 return;
2309
Yinghai Lu3145e942008-12-05 18:58:34 -08002310 cfg = desc->chip_data;
2311 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002312 return;
2313
Yinghai Lu3145e942008-12-05 18:58:34 -08002314 set_extra_move_desc(desc, mask);
2315
Ingo Molnar54168ed2008-08-20 09:07:45 +02002316 cpus_and(tmp, cfg->domain, mask);
2317 dest = cpu_mask_to_apicid(tmp);
2318
Ingo Molnar54168ed2008-08-20 09:07:45 +02002319 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2320 if (modify_ioapic_rte) {
2321 spin_lock_irqsave(&ioapic_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08002322 __target_IO_APIC_irq(irq, dest, cfg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002323 spin_unlock_irqrestore(&ioapic_lock, flags);
2324 }
2325
2326 irte.vector = cfg->vector;
2327 irte.dest_id = IRTE_DEST(dest);
2328
2329 /*
2330 * Modified the IRTE and flushes the Interrupt entry cache.
2331 */
2332 modify_irte(irq, &irte);
2333
2334 if (cfg->move_in_progress) {
2335 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2336 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2337 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2338 cfg->move_in_progress = 0;
2339 }
2340
2341 desc->affinity = mask;
2342}
2343
Yinghai Lu3145e942008-12-05 18:58:34 -08002344static int migrate_irq_remapped_level_desc(struct irq_desc *desc)
Ingo Molnar54168ed2008-08-20 09:07:45 +02002345{
2346 int ret = -1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002347 struct irq_cfg *cfg = desc->chip_data;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002348
Yinghai Lu3145e942008-12-05 18:58:34 -08002349 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002350
Yinghai Lu3145e942008-12-05 18:58:34 -08002351 if (io_apic_level_ack_pending(cfg)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002352 /*
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002353 * Interrupt in progress. Migrating irq now will change the
Ingo Molnar54168ed2008-08-20 09:07:45 +02002354 * vector information in the IO-APIC RTE and that will confuse
2355 * the EOI broadcast performed by cpu.
2356 * So, delay the irq migration to the next instance.
2357 */
2358 schedule_delayed_work(&ir_migration_work, 1);
2359 goto unmask;
2360 }
2361
2362 /* everthing is clear. we have right of way */
Yinghai Lu3145e942008-12-05 18:58:34 -08002363 migrate_ioapic_irq_desc(desc, desc->pending_mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002364
2365 ret = 0;
2366 desc->status &= ~IRQ_MOVE_PENDING;
2367 cpus_clear(desc->pending_mask);
2368
2369unmask:
Yinghai Lu3145e942008-12-05 18:58:34 -08002370 unmask_IO_APIC_irq_desc(desc);
2371
Ingo Molnar54168ed2008-08-20 09:07:45 +02002372 return ret;
2373}
2374
2375static void ir_irq_migration(struct work_struct *work)
2376{
2377 unsigned int irq;
2378 struct irq_desc *desc;
2379
2380 for_each_irq_desc(irq, desc) {
2381 if (desc->status & IRQ_MOVE_PENDING) {
2382 unsigned long flags;
2383
2384 spin_lock_irqsave(&desc->lock, flags);
2385 if (!desc->chip->set_affinity ||
2386 !(desc->status & IRQ_MOVE_PENDING)) {
2387 desc->status &= ~IRQ_MOVE_PENDING;
2388 spin_unlock_irqrestore(&desc->lock, flags);
2389 continue;
2390 }
2391
2392 desc->chip->set_affinity(irq, desc->pending_mask);
2393 spin_unlock_irqrestore(&desc->lock, flags);
2394 }
2395 }
2396}
2397
2398/*
2399 * Migrates the IRQ destination in the process context.
2400 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002401static void set_ir_ioapic_affinity_irq_desc(struct irq_desc *desc, cpumask_t mask)
2402{
2403 if (desc->status & IRQ_LEVEL) {
2404 desc->status |= IRQ_MOVE_PENDING;
2405 desc->pending_mask = mask;
2406 migrate_irq_remapped_level_desc(desc);
2407 return;
2408 }
2409
2410 migrate_ioapic_irq_desc(desc, mask);
2411}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002412static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2413{
2414 struct irq_desc *desc = irq_to_desc(irq);
2415
Yinghai Lu3145e942008-12-05 18:58:34 -08002416 set_ir_ioapic_affinity_irq_desc(desc, mask);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002417}
2418#endif
2419
Yinghai Lu497c9a12008-08-19 20:50:28 -07002420asmlinkage void smp_irq_move_cleanup_interrupt(void)
2421{
2422 unsigned vector, me;
2423 ack_APIC_irq();
Ingo Molnar54168ed2008-08-20 09:07:45 +02002424#ifdef CONFIG_X86_64
2425 exit_idle();
2426#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002427 irq_enter();
2428
2429 me = smp_processor_id();
2430 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2431 unsigned int irq;
2432 struct irq_desc *desc;
2433 struct irq_cfg *cfg;
2434 irq = __get_cpu_var(vector_irq)[vector];
2435
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002436 if (irq == -1)
2437 continue;
2438
Yinghai Lu497c9a12008-08-19 20:50:28 -07002439 desc = irq_to_desc(irq);
2440 if (!desc)
2441 continue;
2442
2443 cfg = irq_cfg(irq);
2444 spin_lock(&desc->lock);
2445 if (!cfg->move_cleanup_count)
2446 goto unlock;
2447
2448 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2449 goto unlock;
2450
2451 __get_cpu_var(vector_irq)[vector] = -1;
2452 cfg->move_cleanup_count--;
2453unlock:
2454 spin_unlock(&desc->lock);
2455 }
2456
2457 irq_exit();
2458}
2459
Yinghai Lu3145e942008-12-05 18:58:34 -08002460static void irq_complete_move(struct irq_desc **descp)
Yinghai Lu497c9a12008-08-19 20:50:28 -07002461{
Yinghai Lu3145e942008-12-05 18:58:34 -08002462 struct irq_desc *desc = *descp;
2463 struct irq_cfg *cfg = desc->chip_data;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002464 unsigned vector, me;
2465
Yinghai Lu48a1b102008-12-11 00:15:01 -08002466 if (likely(!cfg->move_in_progress)) {
2467#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2468 if (likely(!cfg->move_desc_pending))
2469 return;
2470
Yinghai Lub9098952008-12-19 13:48:34 -08002471 /* domain has not changed, but affinity did */
Yinghai Lu48a1b102008-12-11 00:15:01 -08002472 me = smp_processor_id();
2473 if (cpu_isset(me, desc->affinity)) {
2474 *descp = desc = move_irq_desc(desc, me);
2475 /* get the new one */
2476 cfg = desc->chip_data;
2477 cfg->move_desc_pending = 0;
2478 }
2479#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002480 return;
Yinghai Lu48a1b102008-12-11 00:15:01 -08002481 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002482
2483 vector = ~get_irq_regs()->orig_ax;
2484 me = smp_processor_id();
2485 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2486 cpumask_t cleanup_mask;
2487
Yinghai Lu48a1b102008-12-11 00:15:01 -08002488#ifdef CONFIG_NUMA_MIGRATE_IRQ_DESC
2489 *descp = desc = move_irq_desc(desc, me);
2490 /* get the new one */
2491 cfg = desc->chip_data;
2492#endif
2493
Yinghai Lu497c9a12008-08-19 20:50:28 -07002494 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2495 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2496 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2497 cfg->move_in_progress = 0;
2498 }
2499}
2500#else
Yinghai Lu3145e942008-12-05 18:58:34 -08002501static inline void irq_complete_move(struct irq_desc **descp) {}
Yinghai Lu497c9a12008-08-19 20:50:28 -07002502#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002503
Ingo Molnar54168ed2008-08-20 09:07:45 +02002504#ifdef CONFIG_INTR_REMAP
2505static void ack_x2apic_level(unsigned int irq)
2506{
2507 ack_x2APIC_irq();
2508}
2509
2510static void ack_x2apic_edge(unsigned int irq)
2511{
2512 ack_x2APIC_irq();
2513}
Yinghai Lu3145e942008-12-05 18:58:34 -08002514
Ingo Molnar54168ed2008-08-20 09:07:45 +02002515#endif
Yinghai Lu497c9a12008-08-19 20:50:28 -07002516
Yinghai Lu1d025192008-08-19 20:50:34 -07002517static void ack_apic_edge(unsigned int irq)
2518{
Yinghai Lu3145e942008-12-05 18:58:34 -08002519 struct irq_desc *desc = irq_to_desc(irq);
2520
2521 irq_complete_move(&desc);
Yinghai Lu1d025192008-08-19 20:50:34 -07002522 move_native_irq(irq);
2523 ack_APIC_irq();
2524}
2525
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002526atomic_t irq_mis_count;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002527
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002528static void ack_apic_level(unsigned int irq)
2529{
Yinghai Lu3145e942008-12-05 18:58:34 -08002530 struct irq_desc *desc = irq_to_desc(irq);
2531
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002532#ifdef CONFIG_X86_32
2533 unsigned long v;
2534 int i;
2535#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08002536 struct irq_cfg *cfg;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002537 int do_unmask_irq = 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002538
Yinghai Lu3145e942008-12-05 18:58:34 -08002539 irq_complete_move(&desc);
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002540#ifdef CONFIG_GENERIC_PENDING_IRQ
Ingo Molnar54168ed2008-08-20 09:07:45 +02002541 /* If we are moving the irq we need to mask it */
Yinghai Lu3145e942008-12-05 18:58:34 -08002542 if (unlikely(desc->status & IRQ_MOVE_PENDING)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002543 do_unmask_irq = 1;
Yinghai Lu3145e942008-12-05 18:58:34 -08002544 mask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002545 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002546#endif
2547
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002548#ifdef CONFIG_X86_32
2549 /*
2550 * It appears there is an erratum which affects at least version 0x11
2551 * of I/O APIC (that's the 82093AA and cores integrated into various
2552 * chipsets). Under certain conditions a level-triggered interrupt is
2553 * erroneously delivered as edge-triggered one but the respective IRR
2554 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2555 * message but it will never arrive and further interrupts are blocked
2556 * from the source. The exact reason is so far unknown, but the
2557 * phenomenon was observed when two consecutive interrupt requests
2558 * from a given source get delivered to the same CPU and the source is
2559 * temporarily disabled in between.
2560 *
2561 * A workaround is to simulate an EOI message manually. We achieve it
2562 * by setting the trigger mode to edge and then to level when the edge
2563 * trigger mode gets detected in the TMR of a local APIC for a
2564 * level-triggered interrupt. We mask the source for the time of the
2565 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2566 * The idea is from Manfred Spraul. --macro
2567 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002568 cfg = desc->chip_data;
2569 i = cfg->vector;
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002570
2571 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2572#endif
2573
Ingo Molnar54168ed2008-08-20 09:07:45 +02002574 /*
2575 * We must acknowledge the irq before we move it or the acknowledge will
2576 * not propagate properly.
2577 */
2578 ack_APIC_irq();
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002579
Ingo Molnar54168ed2008-08-20 09:07:45 +02002580 /* Now we can move and renable the irq */
2581 if (unlikely(do_unmask_irq)) {
2582 /* Only migrate the irq if the ack has been received.
2583 *
2584 * On rare occasions the broadcast level triggered ack gets
2585 * delayed going to ioapics, and if we reprogram the
2586 * vector while Remote IRR is still set the irq will never
2587 * fire again.
2588 *
2589 * To prevent this scenario we read the Remote IRR bit
2590 * of the ioapic. This has two effects.
2591 * - On any sane system the read of the ioapic will
2592 * flush writes (and acks) going to the ioapic from
2593 * this cpu.
2594 * - We get to see if the ACK has actually been delivered.
2595 *
2596 * Based on failed experiments of reprogramming the
2597 * ioapic entry from outside of irq context starting
2598 * with masking the ioapic entry and then polling until
2599 * Remote IRR was clear before reprogramming the
2600 * ioapic I don't trust the Remote IRR bit to be
2601 * completey accurate.
2602 *
2603 * However there appears to be no other way to plug
2604 * this race, so if the Remote IRR bit is not
2605 * accurate and is causing problems then it is a hardware bug
2606 * and you can go talk to the chipset vendor about it.
2607 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002608 cfg = desc->chip_data;
2609 if (!io_apic_level_ack_pending(cfg))
Ingo Molnar54168ed2008-08-20 09:07:45 +02002610 move_masked_irq(irq);
Yinghai Lu3145e942008-12-05 18:58:34 -08002611 unmask_IO_APIC_irq_desc(desc);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002612 }
Yinghai Lu1d025192008-08-19 20:50:34 -07002613
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002614#ifdef CONFIG_X86_32
Yinghai Lu1d025192008-08-19 20:50:34 -07002615 if (!(v & (1 << (i & 0x1f)))) {
2616 atomic_inc(&irq_mis_count);
2617 spin_lock(&ioapic_lock);
Yinghai Lu3145e942008-12-05 18:58:34 -08002618 __mask_and_edge_IO_APIC_irq(cfg);
2619 __unmask_and_level_IO_APIC_irq(cfg);
Yinghai Lu1d025192008-08-19 20:50:34 -07002620 spin_unlock(&ioapic_lock);
2621 }
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002622#endif
Yinghai Lu3eb2cce2008-08-19 20:50:48 -07002623}
Yinghai Lu1d025192008-08-19 20:50:34 -07002624
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002625static struct irq_chip ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002626 .name = "IO-APIC",
2627 .startup = startup_ioapic_irq,
2628 .mask = mask_IO_APIC_irq,
2629 .unmask = unmask_IO_APIC_irq,
2630 .ack = ack_apic_edge,
2631 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07002632#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002633 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002634#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002635 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636};
2637
Ingo Molnar54168ed2008-08-20 09:07:45 +02002638#ifdef CONFIG_INTR_REMAP
2639static struct irq_chip ir_ioapic_chip __read_mostly = {
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002640 .name = "IR-IO-APIC",
2641 .startup = startup_ioapic_irq,
2642 .mask = mask_IO_APIC_irq,
2643 .unmask = unmask_IO_APIC_irq,
2644 .ack = ack_x2apic_edge,
2645 .eoi = ack_x2apic_level,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002646#ifdef CONFIG_SMP
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002647 .set_affinity = set_ir_ioapic_affinity_irq,
Ingo Molnar54168ed2008-08-20 09:07:45 +02002648#endif
2649 .retrigger = ioapic_retrigger_irq,
2650};
2651#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
2653static inline void init_IO_APIC_traps(void)
2654{
2655 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002656 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002657 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 /*
2660 * NOTE! The local APIC isn't very good at handling
2661 * multiple interrupts at the same interrupt level.
2662 * As the interrupt level is determined by taking the
2663 * vector number and shifting that right by 4, we
2664 * want to spread these out a bit so that they don't
2665 * all fall in the same interrupt level.
2666 *
2667 * Also, we've got to be careful not to trash gate
2668 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2669 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002670 for_each_irq_desc(irq, desc) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002671 cfg = desc->chip_data;
2672 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /*
2674 * Hmm.. We don't have an entry for this,
2675 * so default to an old-fashioned 8259
2676 * interrupt if we can..
2677 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08002678 if (irq < NR_IRQS_LEGACY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 make_8259A_irq(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08002680 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002682 desc->chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
2684 }
2685}
2686
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002687/*
2688 * The local APIC irq-chip implementation:
2689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002691static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
2693 unsigned long v;
2694
2695 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002696 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002699static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002701 unsigned long v;
2702
2703 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002704 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705}
2706
Yinghai Lu3145e942008-12-05 18:58:34 -08002707static void ack_lapic_irq(unsigned int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07002708{
2709 ack_APIC_irq();
2710}
2711
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002712static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002713 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002714 .mask = mask_lapic_irq,
2715 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002716 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717};
2718
Yinghai Lu3145e942008-12-05 18:58:34 -08002719static void lapic_register_intr(int irq, struct irq_desc *desc)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002720{
Yinghai Lu08678b02008-08-19 20:50:05 -07002721 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002722 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2723 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002724}
2725
Jan Beuliche9427102008-01-30 13:31:24 +01002726static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
2728 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002729 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 * We put the 8259A master into AEOI mode and
2731 * unmask on all local APICs LVT0 as NMI.
2732 *
2733 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2734 * is from Maciej W. Rozycki - so we do not have to EOI from
2735 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2738
Jan Beuliche9427102008-01-30 13:31:24 +01002739 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
2741 apic_printk(APIC_VERBOSE, " done.\n");
2742}
2743
2744/*
2745 * This looks a bit hackish but it's about the only one way of sending
2746 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2747 * not support the ExtINT mode, unfortunately. We need to send these
2748 * cycles as some i82489DX-based boards have glue logic that keeps the
2749 * 8259A interrupt line asserted until INTA. --macro
2750 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002751static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002753 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 struct IO_APIC_route_entry entry0, entry1;
2755 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002757 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002758 if (pin == -1) {
2759 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002761 }
2762 apic = find_isa_irq_apic(8, mp_INT);
2763 if (apic == -1) {
2764 WARN_ON_ONCE(1);
2765 return;
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Andi Kleencf4c6a22006-09-26 10:52:30 +02002768 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002769 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 memset(&entry1, 0, sizeof(entry1));
2772
2773 entry1.dest_mode = 0; /* physical delivery */
2774 entry1.mask = 0; /* unmask IRQ now */
Yinghai Lud83e94a2008-08-19 20:50:33 -07002775 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 entry1.delivery_mode = dest_ExtINT;
2777 entry1.polarity = entry0.polarity;
2778 entry1.trigger = 0;
2779 entry1.vector = 0;
2780
Andi Kleencf4c6a22006-09-26 10:52:30 +02002781 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 save_control = CMOS_READ(RTC_CONTROL);
2784 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2785 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2786 RTC_FREQ_SELECT);
2787 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2788
2789 i = 100;
2790 while (i-- > 0) {
2791 mdelay(10);
2792 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2793 i -= 10;
2794 }
2795
2796 CMOS_WRITE(save_control, RTC_CONTROL);
2797 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002798 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Andi Kleencf4c6a22006-09-26 10:52:30 +02002800 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Yinghai Luefa25592008-08-19 20:50:36 -07002803static int disable_timer_pin_1 __initdata;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002804/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
Ingo Molnar54168ed2008-08-20 09:07:45 +02002805static int __init disable_timer_pin_setup(char *arg)
Yinghai Luefa25592008-08-19 20:50:36 -07002806{
2807 disable_timer_pin_1 = 1;
2808 return 0;
2809}
Ingo Molnar54168ed2008-08-20 09:07:45 +02002810early_param("disable_timer_pin_1", disable_timer_pin_setup);
Yinghai Luefa25592008-08-19 20:50:36 -07002811
2812int timer_through_8259 __initdata;
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814/*
2815 * This code may look a bit paranoid, but it's supposed to cooperate with
2816 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2817 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2818 * fanatically on his truly buggy board.
Ingo Molnar54168ed2008-08-20 09:07:45 +02002819 *
2820 * FIXME: really need to revamp this for all platforms.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002822static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Yinghai Lu3145e942008-12-05 18:58:34 -08002824 struct irq_desc *desc = irq_to_desc(0);
2825 struct irq_cfg *cfg = desc->chip_data;
2826 int cpu = boot_cpu_id;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002827 int apic1, pin1, apic2, pin2;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002828 unsigned long flags;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07002829 unsigned int ver;
2830 int no_pin1 = 0;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002831
2832 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002833
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02002834 ver = apic_read(APIC_LVR);
2835 ver = GET_APIC_VERSION(ver);
Ingo Molnar6e908942008-03-21 14:32:36 +01002836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 /*
2838 * get/set the timer IRQ vector:
2839 */
2840 disable_8259A_irq(0);
Yinghai Lu3145e942008-12-05 18:58:34 -08002841 assign_irq_vector(0, cfg, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002844 * As IRQ0 is to be enabled in the 8259A, the virtual
2845 * wire has to be disabled in the local APIC. Also
2846 * timer interrupts need to be acknowledged manually in
2847 * the 8259A for the i82489DX when using the NMI
2848 * watchdog as that APIC treats NMIs as level-triggered.
2849 * The AEOI mode will finish them in the 8259A
2850 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002852 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 init_8259A(1);
Ingo Molnar54168ed2008-08-20 09:07:45 +02002854#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002855 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Ingo Molnar54168ed2008-08-20 09:07:45 +02002856#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002858 pin1 = find_isa_irq_pin(0, mp_INT);
2859 apic1 = find_isa_irq_apic(0, mp_INT);
2860 pin2 = ioapic_i8259.pin;
2861 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002863 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2864 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002865 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002867 /*
2868 * Some BIOS writers are clueless and report the ExtINTA
2869 * I/O APIC input from the cascaded 8259A as the timer
2870 * interrupt input. So just in case, if only one pin
2871 * was found above, try it both directly and through the
2872 * 8259A.
2873 */
2874 if (pin1 == -1) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02002875#ifdef CONFIG_INTR_REMAP
2876 if (intr_remapping_enabled)
2877 panic("BIOS bug: timer not connected to IO-APIC");
2878#endif
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002879 pin1 = pin2;
2880 apic1 = apic2;
2881 no_pin1 = 1;
2882 } else if (pin2 == -1) {
2883 pin2 = pin1;
2884 apic2 = apic1;
2885 }
2886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (pin1 != -1) {
2888 /*
2889 * Ok, does IRQ0 through the IOAPIC work?
2890 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002891 if (no_pin1) {
Yinghai Lu3145e942008-12-05 18:58:34 -08002892 add_pin_to_irq_cpu(cfg, cpu, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002893 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002894 }
Yinghai Lu3145e942008-12-05 18:58:34 -08002895 unmask_IO_APIC_irq_desc(desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if (timer_irq_works()) {
2897 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 setup_nmi();
2899 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002901 if (disable_timer_pin_1 > 0)
2902 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002903 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002905#ifdef CONFIG_INTR_REMAP
2906 if (intr_remapping_enabled)
2907 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2908#endif
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002909 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002910 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002911 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2912 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002914 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2915 "(IRQ0) through the 8259A ...\n");
2916 apic_printk(APIC_QUIET, KERN_INFO
2917 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 /*
2919 * legacy devices should be connected to IO APIC #0
2920 */
Yinghai Lu3145e942008-12-05 18:58:34 -08002921 replace_pin_at_irq_cpu(cfg, cpu, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002922 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Yinghai Lu3145e942008-12-05 18:58:34 -08002923 unmask_IO_APIC_irq_desc(desc);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002924 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002926 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002927 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002929 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002931 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 }
2935 /*
2936 * Cleanup, just in case ...
2937 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002938 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002939 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002940 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
2943 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002944 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2945 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002946 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02002948#ifdef CONFIG_X86_32
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002949 timer_ack = 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02002950#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002952 apic_printk(APIC_QUIET, KERN_INFO
2953 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Yinghai Lu3145e942008-12-05 18:58:34 -08002955 lapic_register_intr(0, desc);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002956 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 enable_8259A_irq(0);
2958
2959 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002960 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002961 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002963 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002964 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002965 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002967 apic_printk(APIC_QUIET, KERN_INFO
2968 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 init_8259A(0);
2971 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002972 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
2974 unlock_ExtINT_logic();
2975
2976 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002977 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002978 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002980 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002982 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002983out:
2984 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
2987/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002988 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2989 * to devices. However there may be an I/O APIC pin available for
2990 * this interrupt regardless. The pin may be left unconnected, but
2991 * typically it will be reused as an ExtINT cascade interrupt for
2992 * the master 8259A. In the MPS case such a pin will normally be
2993 * reported as an ExtINT interrupt in the MP table. With ACPI
2994 * there is no provision for ExtINT interrupts, and in the absence
2995 * of an override it would be treated as an ordinary ISA I/O APIC
2996 * interrupt, that is edge-triggered and unmasked by default. We
2997 * used to do this, but it caused problems on some systems because
2998 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2999 * the same ExtINT cascade interrupt to drive the local APIC of the
3000 * bootstrap processor. Therefore we refrain from routing IRQ2 to
3001 * the I/O APIC in all cases now. No actual device should request
3002 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 */
3004#define PIC_IRQS (1 << PIC_CASCADE_IR)
3005
3006void __init setup_IO_APIC(void)
3007{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003008
3009#ifdef CONFIG_X86_32
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 enable_IO_APIC();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003011#else
3012 /*
3013 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
3014 */
3015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01003017 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Ingo Molnar54168ed2008-08-20 09:07:45 +02003019 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003020 /*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003021 * Set up IO-APIC IRQ routing.
3022 */
3023#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003024 if (!acpi_ioapic)
3025 setup_ioapic_ids_from_mpc();
Ingo Molnar54168ed2008-08-20 09:07:45 +02003026#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 sync_Arb_IDs();
3028 setup_IO_APIC_irqs();
3029 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08003030 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
3033/*
Ingo Molnar54168ed2008-08-20 09:07:45 +02003034 * Called after all the initialization is done. If we didnt find any
3035 * APIC bugs then we can allow the modify fast path
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038static int __init io_apic_bug_finalize(void)
3039{
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003040 if (sis_apic_bug == -1)
3041 sis_apic_bug = 0;
3042 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
3045late_initcall(io_apic_bug_finalize);
3046
3047struct sysfs_ioapic_data {
3048 struct sys_device dev;
3049 struct IO_APIC_route_entry entry[0];
3050};
Ingo Molnar54168ed2008-08-20 09:07:45 +02003051static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Pavel Machek438510f2005-04-16 15:25:24 -07003053static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 struct IO_APIC_route_entry *entry;
3056 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 data = container_of(dev, struct sysfs_ioapic_data, dev);
3060 entry = data->entry;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003061 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
3062 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064 return 0;
3065}
3066
3067static int ioapic_resume(struct sys_device *dev)
3068{
3069 struct IO_APIC_route_entry *entry;
3070 struct sysfs_ioapic_data *data;
3071 unsigned long flags;
3072 union IO_APIC_reg_00 reg_00;
3073 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 data = container_of(dev, struct sysfs_ioapic_data, dev);
3076 entry = data->entry;
3077
3078 spin_lock_irqsave(&ioapic_lock, flags);
3079 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04003080 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
3081 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 io_apic_write(dev->id, 0, reg_00.raw);
3083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003085 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02003086 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 return 0;
3089}
3090
3091static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01003092 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 .suspend = ioapic_suspend,
3094 .resume = ioapic_resume,
3095};
3096
3097static int __init ioapic_init_sysfs(void)
3098{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003099 struct sys_device * dev;
3100 int i, size, error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101
3102 error = sysdev_class_register(&ioapic_sysdev_class);
3103 if (error)
3104 return error;
3105
Ingo Molnar54168ed2008-08-20 09:07:45 +02003106 for (i = 0; i < nr_ioapics; i++ ) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003107 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02003109 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (!mp_ioapic_data[i]) {
3111 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3112 continue;
3113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003115 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 dev->cls = &ioapic_sysdev_class;
3117 error = sysdev_register(dev);
3118 if (error) {
3119 kfree(mp_ioapic_data[i]);
3120 mp_ioapic_data[i] = NULL;
3121 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3122 continue;
3123 }
3124 }
3125
3126 return 0;
3127}
3128
3129device_initcall(ioapic_init_sysfs);
3130
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003131/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07003132 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003133 */
Yinghai Lu199751d2008-08-19 20:50:27 -07003134unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003135{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003136 /* Allocate an unused irq */
Ingo Molnar54168ed2008-08-20 09:07:45 +02003137 unsigned int irq;
3138 unsigned int new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003139 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003140 struct irq_cfg *cfg_new = NULL;
3141 int cpu = boot_cpu_id;
3142 struct irq_desc *desc_new = NULL;
Yinghai Lu199751d2008-08-19 20:50:27 -07003143
3144 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003145 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lube5d5352008-12-05 18:58:33 -08003146 for (new = irq_want; new < NR_IRQS; new++) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003147 if (platform_legacy_irq(new))
3148 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003149
3150 desc_new = irq_to_desc_alloc_cpu(new, cpu);
3151 if (!desc_new) {
3152 printk(KERN_INFO "can not get irq_desc for %d\n", new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003153 continue;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003154 }
3155 cfg_new = desc_new->chip_data;
3156
3157 if (cfg_new->vector != 0)
3158 continue;
Yinghai Lu3145e942008-12-05 18:58:34 -08003159 if (__assign_irq_vector(new, cfg_new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07003160 irq = new;
3161 break;
3162 }
3163 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003164
Yinghai Lu199751d2008-08-19 20:50:27 -07003165 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003166 dynamic_irq_init(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003167 /* restore it, in case dynamic_irq_init clear it */
3168 if (desc_new)
3169 desc_new->chip_data = cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003170 }
3171 return irq;
3172}
3173
Yinghai Lube5d5352008-12-05 18:58:33 -08003174static int nr_irqs_gsi = NR_IRQS_LEGACY;
Yinghai Lu199751d2008-08-19 20:50:27 -07003175int create_irq(void)
3176{
Yinghai Lube5d5352008-12-05 18:58:33 -08003177 unsigned int irq_want;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003178 int irq;
3179
Yinghai Lube5d5352008-12-05 18:58:33 -08003180 irq_want = nr_irqs_gsi;
3181 irq = create_irq_nr(irq_want);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003182
3183 if (irq == 0)
3184 irq = -1;
3185
3186 return irq;
Yinghai Lu199751d2008-08-19 20:50:27 -07003187}
3188
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003189void destroy_irq(unsigned int irq)
3190{
3191 unsigned long flags;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003192 struct irq_cfg *cfg;
3193 struct irq_desc *desc;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003194
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003195 /* store it, in case dynamic_irq_cleanup clear it */
3196 desc = irq_to_desc(irq);
3197 cfg = desc->chip_data;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003198 dynamic_irq_cleanup(irq);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003199 /* connect back irq_cfg */
3200 if (desc)
3201 desc->chip_data = cfg;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003202
Ingo Molnar54168ed2008-08-20 09:07:45 +02003203#ifdef CONFIG_INTR_REMAP
3204 free_irte(irq);
3205#endif
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003206 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu3145e942008-12-05 18:58:34 -08003207 __clear_irq_vector(irq, cfg);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003208 spin_unlock_irqrestore(&vector_lock, flags);
3209}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07003210
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003211/*
Simon Arlott27b46d72007-10-20 01:13:56 +02003212 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003213 */
3214#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003215static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003216{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003217 struct irq_cfg *cfg;
3218 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003219 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003220 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003221
Yinghai Lu3145e942008-12-05 18:58:34 -08003222 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003223 tmp = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08003224 err = assign_irq_vector(irq, cfg, tmp);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003225 if (err)
3226 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003227
Yinghai Lu497c9a12008-08-19 20:50:28 -07003228 cpus_and(tmp, cfg->domain, tmp);
3229 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003230
Ingo Molnar54168ed2008-08-20 09:07:45 +02003231#ifdef CONFIG_INTR_REMAP
3232 if (irq_remapped(irq)) {
3233 struct irte irte;
3234 int ir_index;
3235 u16 sub_handle;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003236
Ingo Molnar54168ed2008-08-20 09:07:45 +02003237 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3238 BUG_ON(ir_index == -1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003239
Ingo Molnar54168ed2008-08-20 09:07:45 +02003240 memset (&irte, 0, sizeof(irte));
3241
3242 irte.present = 1;
3243 irte.dst_mode = INT_DEST_MODE;
3244 irte.trigger_mode = 0; /* edge */
3245 irte.dlvry_mode = INT_DELIVERY_MODE;
3246 irte.vector = cfg->vector;
3247 irte.dest_id = IRTE_DEST(dest);
3248
3249 modify_irte(irq, &irte);
3250
3251 msg->address_hi = MSI_ADDR_BASE_HI;
3252 msg->data = sub_handle;
3253 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3254 MSI_ADDR_IR_SHV |
3255 MSI_ADDR_IR_INDEX1(ir_index) |
3256 MSI_ADDR_IR_INDEX2(ir_index);
3257 } else
3258#endif
3259 {
3260 msg->address_hi = MSI_ADDR_BASE_HI;
3261 msg->address_lo =
3262 MSI_ADDR_BASE_LO |
3263 ((INT_DEST_MODE == 0) ?
3264 MSI_ADDR_DEST_MODE_PHYSICAL:
3265 MSI_ADDR_DEST_MODE_LOGICAL) |
3266 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3267 MSI_ADDR_REDIRECTION_CPU:
3268 MSI_ADDR_REDIRECTION_LOWPRI) |
3269 MSI_ADDR_DEST_ID(dest);
3270
3271 msg->data =
3272 MSI_DATA_TRIGGER_EDGE |
3273 MSI_DATA_LEVEL_ASSERT |
3274 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3275 MSI_DATA_DELIVERY_FIXED:
3276 MSI_DATA_DELIVERY_LOWPRI) |
3277 MSI_DATA_VECTOR(cfg->vector);
3278 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003279 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003280}
3281
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003282#ifdef CONFIG_SMP
3283static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3284{
Yinghai Lu3145e942008-12-05 18:58:34 -08003285 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003286 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003287 struct msi_msg msg;
3288 unsigned int dest;
3289 cpumask_t tmp;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003290
3291 cpus_and(tmp, mask, cpu_online_map);
3292 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003293 return;
3294
Yinghai Lu3145e942008-12-05 18:58:34 -08003295 cfg = desc->chip_data;
3296 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003297 return;
3298
Yinghai Lu3145e942008-12-05 18:58:34 -08003299 set_extra_move_desc(desc, mask);
3300
Yinghai Lu497c9a12008-08-19 20:50:28 -07003301 cpus_and(tmp, cfg->domain, mask);
3302 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003303
Yinghai Lu3145e942008-12-05 18:58:34 -08003304 read_msi_msg_desc(desc, &msg);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003305
3306 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07003307 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003308 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3309 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3310
Yinghai Lu3145e942008-12-05 18:58:34 -08003311 write_msi_msg_desc(desc, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003312 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003313}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003314#ifdef CONFIG_INTR_REMAP
3315/*
3316 * Migrate the MSI irq to another cpumask. This migration is
3317 * done in the process context using interrupt-remapping hardware.
3318 */
3319static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3320{
Yinghai Lu3145e942008-12-05 18:58:34 -08003321 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003322 struct irq_cfg *cfg;
3323 unsigned int dest;
3324 cpumask_t tmp, cleanup_mask;
3325 struct irte irte;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003326
3327 cpus_and(tmp, mask, cpu_online_map);
3328 if (cpus_empty(tmp))
3329 return;
3330
3331 if (get_irte(irq, &irte))
3332 return;
3333
Yinghai Lu3145e942008-12-05 18:58:34 -08003334 cfg = desc->chip_data;
3335 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003336 return;
3337
Yinghai Lu3145e942008-12-05 18:58:34 -08003338 set_extra_move_desc(desc, mask);
3339
Ingo Molnar54168ed2008-08-20 09:07:45 +02003340 cpus_and(tmp, cfg->domain, mask);
3341 dest = cpu_mask_to_apicid(tmp);
3342
3343 irte.vector = cfg->vector;
3344 irte.dest_id = IRTE_DEST(dest);
3345
3346 /*
3347 * atomically update the IRTE with the new destination and vector.
3348 */
3349 modify_irte(irq, &irte);
3350
3351 /*
3352 * After this point, all the interrupts will start arriving
3353 * at the new destination. So, time to cleanup the previous
3354 * vector allocation.
3355 */
3356 if (cfg->move_in_progress) {
3357 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3358 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3359 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3360 cfg->move_in_progress = 0;
3361 }
3362
Ingo Molnar54168ed2008-08-20 09:07:45 +02003363 desc->affinity = mask;
3364}
Yinghai Lu3145e942008-12-05 18:58:34 -08003365
Ingo Molnar54168ed2008-08-20 09:07:45 +02003366#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003367#endif /* CONFIG_SMP */
3368
3369/*
3370 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3371 * which implement the MSI or MSI-X Capability Structure.
3372 */
3373static struct irq_chip msi_chip = {
3374 .name = "PCI-MSI",
3375 .unmask = unmask_msi_irq,
3376 .mask = mask_msi_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003377 .ack = ack_apic_edge,
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003378#ifdef CONFIG_SMP
3379 .set_affinity = set_msi_irq_affinity,
3380#endif
3381 .retrigger = ioapic_retrigger_irq,
3382};
3383
Ingo Molnar54168ed2008-08-20 09:07:45 +02003384#ifdef CONFIG_INTR_REMAP
3385static struct irq_chip msi_ir_chip = {
3386 .name = "IR-PCI-MSI",
3387 .unmask = unmask_msi_irq,
3388 .mask = mask_msi_irq,
3389 .ack = ack_x2apic_edge,
3390#ifdef CONFIG_SMP
3391 .set_affinity = ir_set_msi_irq_affinity,
3392#endif
3393 .retrigger = ioapic_retrigger_irq,
3394};
3395
3396/*
3397 * Map the PCI dev to the corresponding remapping hardware unit
3398 * and allocate 'nvec' consecutive interrupt-remapping table entries
3399 * in it.
3400 */
3401static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3402{
3403 struct intel_iommu *iommu;
3404 int index;
3405
3406 iommu = map_dev_to_ir(dev);
3407 if (!iommu) {
3408 printk(KERN_ERR
3409 "Unable to map PCI %s to iommu\n", pci_name(dev));
3410 return -ENOENT;
3411 }
3412
3413 index = alloc_irte(iommu, irq, nvec);
3414 if (index < 0) {
3415 printk(KERN_ERR
3416 "Unable to allocate %d IRTE for PCI %s\n", nvec,
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02003417 pci_name(dev));
Ingo Molnar54168ed2008-08-20 09:07:45 +02003418 return -ENOSPC;
3419 }
3420 return index;
3421}
3422#endif
Yinghai Lu1d025192008-08-19 20:50:34 -07003423
Yinghai Lu3145e942008-12-05 18:58:34 -08003424static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc, int irq)
Yinghai Lu1d025192008-08-19 20:50:34 -07003425{
3426 int ret;
3427 struct msi_msg msg;
3428
3429 ret = msi_compose_msg(dev, irq, &msg);
3430 if (ret < 0)
3431 return ret;
3432
Yinghai Lu3145e942008-12-05 18:58:34 -08003433 set_irq_msi(irq, msidesc);
Yinghai Lu1d025192008-08-19 20:50:34 -07003434 write_msi_msg(irq, &msg);
3435
Ingo Molnar54168ed2008-08-20 09:07:45 +02003436#ifdef CONFIG_INTR_REMAP
3437 if (irq_remapped(irq)) {
3438 struct irq_desc *desc = irq_to_desc(irq);
3439 /*
3440 * irq migration in process context
3441 */
3442 desc->status |= IRQ_MOVE_PCNTXT;
3443 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3444 } else
3445#endif
3446 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Yinghai Lu1d025192008-08-19 20:50:34 -07003447
Yinghai Luc81bba42008-09-25 11:53:11 -07003448 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3449
Yinghai Lu1d025192008-08-19 20:50:34 -07003450 return 0;
3451}
3452
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003453int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003454{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003455 unsigned int irq;
3456 int ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07003457 unsigned int irq_want;
3458
Yinghai Lube5d5352008-12-05 18:58:33 -08003459 irq_want = nr_irqs_gsi;
Yinghai Lu199751d2008-08-19 20:50:27 -07003460 irq = create_irq_nr(irq_want);
Yinghai Lu199751d2008-08-19 20:50:27 -07003461 if (irq == 0)
3462 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003463
Ingo Molnar54168ed2008-08-20 09:07:45 +02003464#ifdef CONFIG_INTR_REMAP
3465 if (!intr_remapping_enabled)
3466 goto no_ir;
3467
3468 ret = msi_alloc_irte(dev, irq, 1);
3469 if (ret < 0)
3470 goto error;
3471no_ir:
3472#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003473 ret = setup_msi_irq(dev, msidesc, irq);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003474 if (ret < 0) {
3475 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003476 return ret;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003477 }
Michael Ellerman7fe37302007-04-18 19:39:21 +10003478 return 0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003479
3480#ifdef CONFIG_INTR_REMAP
3481error:
3482 destroy_irq(irq);
3483 return ret;
3484#endif
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003485}
3486
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003487int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3488{
Ingo Molnar54168ed2008-08-20 09:07:45 +02003489 unsigned int irq;
3490 int ret, sub_handle;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003491 struct msi_desc *msidesc;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003492 unsigned int irq_want;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003493
Ingo Molnar54168ed2008-08-20 09:07:45 +02003494#ifdef CONFIG_INTR_REMAP
3495 struct intel_iommu *iommu = 0;
3496 int index = 0;
3497#endif
3498
Yinghai Lube5d5352008-12-05 18:58:33 -08003499 irq_want = nr_irqs_gsi;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003500 sub_handle = 0;
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003501 list_for_each_entry(msidesc, &dev->msi_list, list) {
3502 irq = create_irq_nr(irq_want);
Yinghai Lube5d5352008-12-05 18:58:33 -08003503 irq_want++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02003504 if (irq == 0)
3505 return -1;
3506#ifdef CONFIG_INTR_REMAP
3507 if (!intr_remapping_enabled)
3508 goto no_ir;
3509
3510 if (!sub_handle) {
3511 /*
3512 * allocate the consecutive block of IRTE's
3513 * for 'nvec'
3514 */
3515 index = msi_alloc_irte(dev, irq, nvec);
3516 if (index < 0) {
3517 ret = index;
3518 goto error;
3519 }
3520 } else {
3521 iommu = map_dev_to_ir(dev);
3522 if (!iommu) {
3523 ret = -ENOENT;
3524 goto error;
3525 }
3526 /*
3527 * setup the mapping between the irq and the IRTE
3528 * base index, the sub_handle pointing to the
3529 * appropriate interrupt remap table entry.
3530 */
3531 set_irte_irq(irq, iommu, index, sub_handle);
3532 }
3533no_ir:
3534#endif
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003535 ret = setup_msi_irq(dev, msidesc, irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003536 if (ret < 0)
3537 goto error;
3538 sub_handle++;
3539 }
3540 return 0;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003541
3542error:
Ingo Molnar54168ed2008-08-20 09:07:45 +02003543 destroy_irq(irq);
3544 return ret;
Yinghai Lu047c8fd2008-08-19 20:50:41 -07003545}
3546
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07003547void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003548{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07003549 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003550}
3551
Ingo Molnar54168ed2008-08-20 09:07:45 +02003552#ifdef CONFIG_DMAR
3553#ifdef CONFIG_SMP
3554static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3555{
Yinghai Lu3145e942008-12-05 18:58:34 -08003556 struct irq_desc *desc = irq_to_desc(irq);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003557 struct irq_cfg *cfg;
3558 struct msi_msg msg;
3559 unsigned int dest;
3560 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07003561
Ingo Molnar54168ed2008-08-20 09:07:45 +02003562 cpus_and(tmp, mask, cpu_online_map);
3563 if (cpus_empty(tmp))
3564 return;
3565
Yinghai Lu3145e942008-12-05 18:58:34 -08003566 cfg = desc->chip_data;
3567 if (assign_irq_vector(irq, cfg, mask))
Ingo Molnar54168ed2008-08-20 09:07:45 +02003568 return;
3569
Yinghai Lu3145e942008-12-05 18:58:34 -08003570 set_extra_move_desc(desc, mask);
3571
Ingo Molnar54168ed2008-08-20 09:07:45 +02003572 cpus_and(tmp, cfg->domain, mask);
3573 dest = cpu_mask_to_apicid(tmp);
3574
3575 dmar_msi_read(irq, &msg);
3576
3577 msg.data &= ~MSI_DATA_VECTOR_MASK;
3578 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3579 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3580 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3581
3582 dmar_msi_write(irq, &msg);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003583 desc->affinity = mask;
3584}
Yinghai Lu3145e942008-12-05 18:58:34 -08003585
Ingo Molnar54168ed2008-08-20 09:07:45 +02003586#endif /* CONFIG_SMP */
3587
3588struct irq_chip dmar_msi_type = {
3589 .name = "DMAR_MSI",
3590 .unmask = dmar_msi_unmask,
3591 .mask = dmar_msi_mask,
3592 .ack = ack_apic_edge,
3593#ifdef CONFIG_SMP
3594 .set_affinity = dmar_msi_set_affinity,
3595#endif
3596 .retrigger = ioapic_retrigger_irq,
3597};
3598
3599int arch_setup_dmar_msi(unsigned int irq)
3600{
3601 int ret;
3602 struct msi_msg msg;
3603
3604 ret = msi_compose_msg(NULL, irq, &msg);
3605 if (ret < 0)
3606 return ret;
3607 dmar_msi_write(irq, &msg);
3608 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3609 "edge");
3610 return 0;
3611}
3612#endif
3613
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003614#ifdef CONFIG_HPET_TIMER
3615
3616#ifdef CONFIG_SMP
3617static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
3618{
Yinghai Lu3145e942008-12-05 18:58:34 -08003619 struct irq_desc *desc = irq_to_desc(irq);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003620 struct irq_cfg *cfg;
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003621 struct msi_msg msg;
3622 unsigned int dest;
3623 cpumask_t tmp;
3624
3625 cpus_and(tmp, mask, cpu_online_map);
3626 if (cpus_empty(tmp))
3627 return;
3628
Yinghai Lu3145e942008-12-05 18:58:34 -08003629 cfg = desc->chip_data;
3630 if (assign_irq_vector(irq, cfg, mask))
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003631 return;
3632
Yinghai Lu3145e942008-12-05 18:58:34 -08003633 set_extra_move_desc(desc, mask);
3634
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003635 cpus_and(tmp, cfg->domain, mask);
3636 dest = cpu_mask_to_apicid(tmp);
3637
3638 hpet_msi_read(irq, &msg);
3639
3640 msg.data &= ~MSI_DATA_VECTOR_MASK;
3641 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3642 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3643 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3644
3645 hpet_msi_write(irq, &msg);
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003646 desc->affinity = mask;
3647}
Yinghai Lu3145e942008-12-05 18:58:34 -08003648
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003649#endif /* CONFIG_SMP */
3650
3651struct irq_chip hpet_msi_type = {
3652 .name = "HPET_MSI",
3653 .unmask = hpet_msi_unmask,
3654 .mask = hpet_msi_mask,
3655 .ack = ack_apic_edge,
3656#ifdef CONFIG_SMP
3657 .set_affinity = hpet_msi_set_affinity,
3658#endif
3659 .retrigger = ioapic_retrigger_irq,
3660};
3661
3662int arch_setup_hpet_msi(unsigned int irq)
3663{
3664 int ret;
3665 struct msi_msg msg;
3666
3667 ret = msi_compose_msg(NULL, irq, &msg);
3668 if (ret < 0)
3669 return ret;
3670
3671 hpet_msi_write(irq, &msg);
3672 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3673 "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003674
venkatesh.pallipadi@intel.com58ac1e72008-09-05 18:02:17 -07003675 return 0;
3676}
3677#endif
3678
Ingo Molnar54168ed2008-08-20 09:07:45 +02003679#endif /* CONFIG_PCI_MSI */
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003680/*
3681 * Hypertransport interrupt support
3682 */
3683#ifdef CONFIG_HT_IRQ
3684
3685#ifdef CONFIG_SMP
3686
Yinghai Lu497c9a12008-08-19 20:50:28 -07003687static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003688{
Eric W. Biedermanec683072006-11-08 17:44:57 -08003689 struct ht_irq_msg msg;
3690 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003691
Yinghai Lu497c9a12008-08-19 20:50:28 -07003692 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003693 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003694
Yinghai Lu497c9a12008-08-19 20:50:28 -07003695 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08003696 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003697
Eric W. Biedermanec683072006-11-08 17:44:57 -08003698 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003699}
3700
3701static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3702{
Yinghai Lu3145e942008-12-05 18:58:34 -08003703 struct irq_desc *desc = irq_to_desc(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003704 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003705 unsigned int dest;
3706 cpumask_t tmp;
3707
3708 cpus_and(tmp, mask, cpu_online_map);
3709 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003710 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003711
Yinghai Lu3145e942008-12-05 18:58:34 -08003712 cfg = desc->chip_data;
3713 if (assign_irq_vector(irq, cfg, mask))
Yinghai Lu497c9a12008-08-19 20:50:28 -07003714 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003715
Yinghai Lu3145e942008-12-05 18:58:34 -08003716 set_extra_move_desc(desc, mask);
3717
Yinghai Lu497c9a12008-08-19 20:50:28 -07003718 cpus_and(tmp, cfg->domain, mask);
3719 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003720
Yinghai Lu497c9a12008-08-19 20:50:28 -07003721 target_ht_irq(irq, dest, cfg->vector);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003722 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003723}
Yinghai Lu3145e942008-12-05 18:58:34 -08003724
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003725#endif
3726
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07003727static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003728 .name = "PCI-HT",
3729 .mask = mask_ht_irq,
3730 .unmask = unmask_ht_irq,
Yinghai Lu1d025192008-08-19 20:50:34 -07003731 .ack = ack_apic_edge,
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003732#ifdef CONFIG_SMP
3733 .set_affinity = set_ht_irq_affinity,
3734#endif
3735 .retrigger = ioapic_retrigger_irq,
3736};
3737
3738int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3739{
Yinghai Lu497c9a12008-08-19 20:50:28 -07003740 struct irq_cfg *cfg;
3741 int err;
3742 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003743
Yinghai Lu3145e942008-12-05 18:58:34 -08003744 cfg = irq_cfg(irq);
Yinghai Lu497c9a12008-08-19 20:50:28 -07003745 tmp = TARGET_CPUS;
Yinghai Lu3145e942008-12-05 18:58:34 -08003746 err = assign_irq_vector(irq, cfg, tmp);
Ingo Molnar54168ed2008-08-20 09:07:45 +02003747 if (!err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08003748 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003749 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003750
Yinghai Lu497c9a12008-08-19 20:50:28 -07003751 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003752 dest = cpu_mask_to_apicid(tmp);
3753
Eric W. Biedermanec683072006-11-08 17:44:57 -08003754 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003755
Eric W. Biedermanec683072006-11-08 17:44:57 -08003756 msg.address_lo =
3757 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003758 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07003759 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003760 ((INT_DEST_MODE == 0) ?
3761 HT_IRQ_LOW_DM_PHYSICAL :
3762 HT_IRQ_LOW_DM_LOGICAL) |
3763 HT_IRQ_LOW_RQEOI_EDGE |
3764 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3765 HT_IRQ_LOW_MT_FIXED :
3766 HT_IRQ_LOW_MT_ARBITRATED) |
3767 HT_IRQ_LOW_IRQ_MASKED;
3768
Eric W. Biedermanec683072006-11-08 17:44:57 -08003769 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003770
Ingo Molnara460e742006-10-17 00:10:03 -07003771 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3772 handle_edge_irq, "edge");
Yinghai Luc81bba42008-09-25 11:53:11 -07003773
3774 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003775 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07003776 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07003777}
3778#endif /* CONFIG_HT_IRQ */
3779
Dean Nelson4173a0e2008-10-02 12:18:21 -05003780#ifdef CONFIG_X86_64
3781/*
3782 * Re-target the irq to the specified CPU and enable the specified MMR located
3783 * on the specified blade to allow the sending of MSIs to the specified CPU.
3784 */
3785int arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
3786 unsigned long mmr_offset)
3787{
3788 const cpumask_t *eligible_cpu = get_cpu_mask(cpu);
3789 struct irq_cfg *cfg;
3790 int mmr_pnode;
3791 unsigned long mmr_value;
3792 struct uv_IO_APIC_route_entry *entry;
3793 unsigned long flags;
3794 int err;
3795
Yinghai Lu3145e942008-12-05 18:58:34 -08003796 cfg = irq_cfg(irq);
3797
3798 err = assign_irq_vector(irq, cfg, *eligible_cpu);
Dean Nelson4173a0e2008-10-02 12:18:21 -05003799 if (err != 0)
3800 return err;
3801
3802 spin_lock_irqsave(&vector_lock, flags);
3803 set_irq_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
3804 irq_name);
3805 spin_unlock_irqrestore(&vector_lock, flags);
3806
Dean Nelson4173a0e2008-10-02 12:18:21 -05003807 mmr_value = 0;
3808 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3809 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3810
3811 entry->vector = cfg->vector;
3812 entry->delivery_mode = INT_DELIVERY_MODE;
3813 entry->dest_mode = INT_DEST_MODE;
3814 entry->polarity = 0;
3815 entry->trigger = 0;
3816 entry->mask = 0;
3817 entry->dest = cpu_mask_to_apicid(*eligible_cpu);
3818
3819 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3820 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3821
3822 return irq;
3823}
3824
3825/*
3826 * Disable the specified MMR located on the specified blade so that MSIs are
3827 * longer allowed to be sent.
3828 */
3829void arch_disable_uv_irq(int mmr_blade, unsigned long mmr_offset)
3830{
3831 unsigned long mmr_value;
3832 struct uv_IO_APIC_route_entry *entry;
3833 int mmr_pnode;
3834
3835 mmr_value = 0;
3836 entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
3837 BUG_ON(sizeof(struct uv_IO_APIC_route_entry) != sizeof(unsigned long));
3838
3839 entry->mask = 1;
3840
3841 mmr_pnode = uv_blade_to_pnode(mmr_blade);
3842 uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
3843}
3844#endif /* CONFIG_X86_64 */
3845
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003846int __init io_apic_get_redir_entries (int ioapic)
3847{
3848 union IO_APIC_reg_01 reg_01;
3849 unsigned long flags;
3850
3851 spin_lock_irqsave(&ioapic_lock, flags);
3852 reg_01.raw = io_apic_read(ioapic, 1);
3853 spin_unlock_irqrestore(&ioapic_lock, flags);
3854
3855 return reg_01.bits.entries;
3856}
3857
Yinghai Lube5d5352008-12-05 18:58:33 -08003858void __init probe_nr_irqs_gsi(void)
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003859{
Yinghai Lube5d5352008-12-05 18:58:33 -08003860 int idx;
3861 int nr = 0;
3862
3863 for (idx = 0; idx < nr_ioapics; idx++)
3864 nr += io_apic_get_redir_entries(idx) + 1;
3865
3866 if (nr > nr_irqs_gsi)
3867 nr_irqs_gsi = nr;
Yinghai Lu9d6a4d02008-08-19 20:50:52 -07003868}
3869
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870/* --------------------------------------------------------------------------
Ingo Molnar54168ed2008-08-20 09:07:45 +02003871 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 -------------------------------------------------------------------------- */
3873
Len Brown888ba6c2005-08-24 12:07:20 -04003874#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Ingo Molnar54168ed2008-08-20 09:07:45 +02003876#ifdef CONFIG_X86_32
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003877int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878{
3879 union IO_APIC_reg_00 reg_00;
3880 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3881 physid_mask_t tmp;
3882 unsigned long flags;
3883 int i = 0;
3884
3885 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003886 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3887 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003889 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3891 * advantage of new APIC bus architecture.
3892 */
3893
3894 if (physids_empty(apic_id_map))
3895 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3896
3897 spin_lock_irqsave(&ioapic_lock, flags);
3898 reg_00.raw = io_apic_read(ioapic, 0);
3899 spin_unlock_irqrestore(&ioapic_lock, flags);
3900
3901 if (apic_id >= get_physical_broadcast()) {
3902 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3903 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3904 apic_id = reg_00.bits.ID;
3905 }
3906
3907 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003908 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 * 'stuck on smp_invalidate_needed IPI wait' messages.
3910 */
3911 if (check_apicid_used(apic_id_map, apic_id)) {
3912
3913 for (i = 0; i < get_physical_broadcast(); i++) {
3914 if (!check_apicid_used(apic_id_map, i))
3915 break;
3916 }
3917
3918 if (i == get_physical_broadcast())
3919 panic("Max apic_id exceeded!\n");
3920
3921 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3922 "trying %d\n", ioapic, apic_id, i);
3923
3924 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927 tmp = apicid_to_cpu_present(apic_id);
3928 physids_or(apic_id_map, apic_id_map, tmp);
3929
3930 if (reg_00.bits.ID != apic_id) {
3931 reg_00.bits.ID = apic_id;
3932
3933 spin_lock_irqsave(&ioapic_lock, flags);
3934 io_apic_write(ioapic, 0, reg_00.raw);
3935 reg_00.raw = io_apic_read(ioapic, 0);
3936 spin_unlock_irqrestore(&ioapic_lock, flags);
3937
3938 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01003939 if (reg_00.bits.ID != apic_id) {
3940 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3941 return -1;
3942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 }
3944
3945 apic_printk(APIC_VERBOSE, KERN_INFO
3946 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3947
3948 return apic_id;
3949}
3950
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02003951int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952{
3953 union IO_APIC_reg_01 reg_01;
3954 unsigned long flags;
3955
3956 spin_lock_irqsave(&ioapic_lock, flags);
3957 reg_01.raw = io_apic_read(ioapic, 1);
3958 spin_unlock_irqrestore(&ioapic_lock, flags);
3959
3960 return reg_01.bits.version;
3961}
Ingo Molnar54168ed2008-08-20 09:07:45 +02003962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Ingo Molnar54168ed2008-08-20 09:07:45 +02003964int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965{
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003966 struct irq_desc *desc;
3967 struct irq_cfg *cfg;
3968 int cpu = boot_cpu_id;
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 if (!IO_APIC_IRQ(irq)) {
Ingo Molnar54168ed2008-08-20 09:07:45 +02003971 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 ioapic);
3973 return -EINVAL;
3974 }
3975
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003976 desc = irq_to_desc_alloc_cpu(irq, cpu);
3977 if (!desc) {
3978 printk(KERN_INFO "can not get irq_desc %d\n", irq);
3979 return 0;
3980 }
3981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 * IRQs < 16 are already in the irq_2_pin[] map
3984 */
Yinghai Lu99d093d2008-12-05 18:58:32 -08003985 if (irq >= NR_IRQS_LEGACY) {
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003986 cfg = desc->chip_data;
Yinghai Lu3145e942008-12-05 18:58:34 -08003987 add_pin_to_irq_cpu(cfg, cpu, ioapic, pin);
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08003988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Yinghai Lu3145e942008-12-05 18:58:34 -08003990 setup_IO_APIC_irq(ioapic, pin, irq, desc, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 return 0;
3993}
3994
Ingo Molnar54168ed2008-08-20 09:07:45 +02003995
Shaohua Li61fd47e2007-11-17 01:05:28 -05003996int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3997{
3998 int i;
3999
4000 if (skip_ioapic_setup)
4001 return -1;
4002
4003 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04004004 if (mp_irqs[i].mp_irqtype == mp_INT &&
4005 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05004006 break;
4007 if (i >= mp_irq_entries)
4008 return -1;
4009
4010 *trigger = irq_trigger(i);
4011 *polarity = irq_polarity(i);
4012 return 0;
4013}
4014
Len Brown888ba6c2005-08-24 12:07:20 -04004015#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02004016
Yinghai Lu497c9a12008-08-19 20:50:28 -07004017/*
4018 * This function currently is only a helper for the i386 smp boot process where
4019 * we need to reprogram the ioredtbls to cater for the cpus which have come online
4020 * so mask in all cases should simply be TARGET_CPUS
4021 */
4022#ifdef CONFIG_SMP
4023void __init setup_ioapic_dest(void)
4024{
4025 int pin, ioapic, irq, irq_entry;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004026 struct irq_desc *desc;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004027 struct irq_cfg *cfg;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004028 cpumask_t mask;
Yinghai Lu497c9a12008-08-19 20:50:28 -07004029
4030 if (skip_ioapic_setup == 1)
4031 return;
4032
4033 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
4034 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
4035 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
4036 if (irq_entry == -1)
4037 continue;
4038 irq = pin_2_irq(irq_entry, ioapic, pin);
4039
4040 /* setup_IO_APIC_irqs could fail to get vector for some device
4041 * when you have too many devices, because at that time only boot
4042 * cpu is online.
4043 */
Yinghai Lu0b8f1ef2008-12-05 18:58:31 -08004044 desc = irq_to_desc(irq);
4045 cfg = desc->chip_data;
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004046 if (!cfg->vector) {
Yinghai Lu3145e942008-12-05 18:58:34 -08004047 setup_IO_APIC_irq(ioapic, pin, irq, desc,
Yinghai Lu497c9a12008-08-19 20:50:28 -07004048 irq_trigger(irq_entry),
4049 irq_polarity(irq_entry));
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004050 continue;
4051
4052 }
4053
4054 /*
4055 * Honour affinities which have been set in early boot
4056 */
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004057 if (desc->status &
4058 (IRQ_NO_BALANCING | IRQ_AFFINITY_SET))
4059 mask = desc->affinity;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004060 else
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004061 mask = TARGET_CPUS;
4062
4063#ifdef CONFIG_INTR_REMAP
4064 if (intr_remapping_enabled)
Yinghai Lu3145e942008-12-05 18:58:34 -08004065 set_ir_ioapic_affinity_irq_desc(desc, mask);
Thomas Gleixner6c2e9402008-11-07 12:33:49 +01004066 else
4067#endif
Yinghai Lu3145e942008-12-05 18:58:34 -08004068 set_ioapic_affinity_irq_desc(desc, mask);
Yinghai Lu497c9a12008-08-19 20:50:28 -07004069 }
4070
4071 }
4072}
4073#endif
4074
Ingo Molnar54168ed2008-08-20 09:07:45 +02004075#define IOAPIC_RESOURCE_NAME_SIZE 11
4076
4077static struct resource *ioapic_resources;
4078
4079static struct resource * __init ioapic_setup_resources(void)
4080{
4081 unsigned long n;
4082 struct resource *res;
4083 char *mem;
4084 int i;
4085
4086 if (nr_ioapics <= 0)
4087 return NULL;
4088
4089 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
4090 n *= nr_ioapics;
4091
4092 mem = alloc_bootmem(n);
4093 res = (void *)mem;
4094
4095 if (mem != NULL) {
4096 mem += sizeof(struct resource) * nr_ioapics;
4097
4098 for (i = 0; i < nr_ioapics; i++) {
4099 res[i].name = mem;
4100 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
4101 sprintf(mem, "IOAPIC %u", i);
4102 mem += IOAPIC_RESOURCE_NAME_SIZE;
4103 }
4104 }
4105
4106 ioapic_resources = res;
4107
4108 return res;
4109}
Ingo Molnar54168ed2008-08-20 09:07:45 +02004110
Yinghai Luf3294a32008-06-27 01:41:56 -07004111void __init ioapic_init_mappings(void)
4112{
4113 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004114 struct resource *ioapic_res;
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004115 int i;
Yinghai Luf3294a32008-06-27 01:41:56 -07004116
Ingo Molnar54168ed2008-08-20 09:07:45 +02004117 ioapic_res = ioapic_setup_resources();
Yinghai Luf3294a32008-06-27 01:41:56 -07004118 for (i = 0; i < nr_ioapics; i++) {
4119 if (smp_found_config) {
4120 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004121#ifdef CONFIG_X86_32
Thomas Gleixnerd6c88a52008-10-15 15:27:23 +02004122 if (!ioapic_phys) {
4123 printk(KERN_ERR
4124 "WARNING: bogus zero IO-APIC "
4125 "address found in MPTABLE, "
4126 "disabling IO/APIC support!\n");
4127 smp_found_config = 0;
4128 skip_ioapic_setup = 1;
4129 goto fake_ioapic_page;
4130 }
Ingo Molnar54168ed2008-08-20 09:07:45 +02004131#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004132 } else {
Ingo Molnar54168ed2008-08-20 09:07:45 +02004133#ifdef CONFIG_X86_32
Yinghai Luf3294a32008-06-27 01:41:56 -07004134fake_ioapic_page:
Ingo Molnar54168ed2008-08-20 09:07:45 +02004135#endif
Yinghai Luf3294a32008-06-27 01:41:56 -07004136 ioapic_phys = (unsigned long)
Ingo Molnar54168ed2008-08-20 09:07:45 +02004137 alloc_bootmem_pages(PAGE_SIZE);
Yinghai Luf3294a32008-06-27 01:41:56 -07004138 ioapic_phys = __pa(ioapic_phys);
4139 }
4140 set_fixmap_nocache(idx, ioapic_phys);
Ingo Molnar54168ed2008-08-20 09:07:45 +02004141 apic_printk(APIC_VERBOSE,
4142 "mapped IOAPIC to %08lx (%08lx)\n",
4143 __fix_to_virt(idx), ioapic_phys);
Yinghai Luf3294a32008-06-27 01:41:56 -07004144 idx++;
Ingo Molnar54168ed2008-08-20 09:07:45 +02004145
Ingo Molnar54168ed2008-08-20 09:07:45 +02004146 if (ioapic_res != NULL) {
4147 ioapic_res->start = ioapic_phys;
4148 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
4149 ioapic_res++;
4150 }
Yinghai Luf3294a32008-06-27 01:41:56 -07004151 }
4152}
4153
Ingo Molnar54168ed2008-08-20 09:07:45 +02004154static int __init ioapic_insert_resources(void)
4155{
4156 int i;
4157 struct resource *r = ioapic_resources;
4158
4159 if (!r) {
4160 printk(KERN_ERR
4161 "IO APIC resources could be not be allocated.\n");
4162 return -1;
4163 }
4164
4165 for (i = 0; i < nr_ioapics; i++) {
4166 insert_resource(&iomem_resource, r);
4167 r++;
4168 }
4169
4170 return 0;
4171}
4172
4173/* Insert the IO APIC resources after PCI initialization has occured to handle
4174 * IO APICS that are mapped in on a BAR in PCI space. */
4175late_initcall(ioapic_insert_resources);