blob: 3001924bdd36fa10540b08d2e8016b49b95e7872 [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 Luf3294a32008-06-27 01:41:56 -070028#include <linux/bootmem.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. Biederman2d3fcc12006-10-04 02:16:43 -070034#include <linux/pci.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070035#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070036#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020038#include <linux/kthread.h>
Julia Lawall1d16b532008-01-30 13:32:19 +010039#include <linux/jiffies.h> /* time_after() */
Ashok Raj54d5d422005-09-06 15:16:15 -070040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/io.h>
42#include <asm/smp.h>
43#include <asm/desc.h>
44#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070045#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020046#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070047#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070048#include <asm/hypertransport.h>
Yinghai Lua4dbc342008-07-25 02:14:28 -070049#include <asm/setup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Yinghai Lu497c9a12008-08-19 20:50:28 -070051#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020053#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010055#define __apicdebuginit(type) static type __init
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057int (*ioapic_renumber_irq)(int ioapic, int irq);
58atomic_t irq_mis_count;
59
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -080060/* Where if anywhere is the i8259 connect in external int mode */
61static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static DEFINE_SPINLOCK(ioapic_lock);
Yinghai Lu497c9a12008-08-19 20:50:28 -070064static DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Maciej W. Rozycki35542c52008-05-21 22:10:22 +010066int timer_through_8259 __initdata;
Andi Kleenf9262c12006-03-08 17:57:25 -080067
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * Is the SiS APIC rmw bug present ?
70 * -1 = don't know, 0 = no, 1 = yes
71 */
72int sis_apic_bug = -1;
73
Yinghai Lu301e6192008-08-19 20:50:02 -070074int first_free_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075/*
76 * # of IRQ routing registers
77 */
78int nr_ioapic_registers[MAX_IO_APICS];
79
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040080/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040081struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040082int nr_ioapics;
83
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040084/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040085struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040086
87/* # of MP IRQ source entries */
88int mp_irq_entries;
89
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040090#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
91int mp_bus_id_to_type[MAX_MP_BUSSES];
92#endif
93
94DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
95
Rusty Russell1a3f2392006-09-26 10:52:32 +020096static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020097
Yinghai Luda51a822008-08-19 20:50:25 -070098struct irq_cfg;
Yinghai Lu0f978f42008-08-19 20:50:26 -070099struct irq_pin_list;
Yinghai Lua1420f32008-08-19 20:50:24 -0700100struct irq_cfg {
Yinghai Luda51a822008-08-19 20:50:25 -0700101 unsigned int irq;
102 struct irq_cfg *next;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700103 struct irq_pin_list *irq_2_pin;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700104 cpumask_t domain;
105 cpumask_t old_domain;
106 unsigned move_cleanup_count;
Yinghai Lua1420f32008-08-19 20:50:24 -0700107 u8 vector;
Yinghai Lu497c9a12008-08-19 20:50:28 -0700108 u8 move_in_progress : 1;
Yinghai Lua1420f32008-08-19 20:50:24 -0700109};
110
111
112/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
113static struct irq_cfg irq_cfg_legacy[] __initdata = {
Yinghai Lu497c9a12008-08-19 20:50:28 -0700114 [0] = { .irq = 0, .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
115 [1] = { .irq = 1, .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
116 [2] = { .irq = 2, .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
117 [3] = { .irq = 3, .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
118 [4] = { .irq = 4, .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
119 [5] = { .irq = 5, .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
120 [6] = { .irq = 6, .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
121 [7] = { .irq = 7, .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
122 [8] = { .irq = 8, .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
123 [9] = { .irq = 9, .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
124 [10] = { .irq = 10, .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
125 [11] = { .irq = 11, .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
126 [12] = { .irq = 12, .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
127 [13] = { .irq = 13, .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
128 [14] = { .irq = 14, .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
129 [15] = { .irq = 15, .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
Yinghai Lua1420f32008-08-19 20:50:24 -0700130};
131
Yinghai Luda51a822008-08-19 20:50:25 -0700132static struct irq_cfg irq_cfg_init = { .irq = -1U, };
133/* need to be biger than size of irq_cfg_legacy */
134static int nr_irq_cfg = 32;
135
136static int __init parse_nr_irq_cfg(char *arg)
Yinghai Lua1420f32008-08-19 20:50:24 -0700137{
Yinghai Luda51a822008-08-19 20:50:25 -0700138 if (arg) {
139 nr_irq_cfg = simple_strtoul(arg, NULL, 0);
140 if (nr_irq_cfg < 32)
141 nr_irq_cfg = 32;
142 }
143 return 0;
144}
Yinghai Lua1420f32008-08-19 20:50:24 -0700145
Yinghai Luda51a822008-08-19 20:50:25 -0700146early_param("nr_irq_cfg", parse_nr_irq_cfg);
Yinghai Lua1420f32008-08-19 20:50:24 -0700147
Yinghai Luda51a822008-08-19 20:50:25 -0700148static void init_one_irq_cfg(struct irq_cfg *cfg)
149{
150 memcpy(cfg, &irq_cfg_init, sizeof(struct irq_cfg));
Yinghai Lua1420f32008-08-19 20:50:24 -0700151}
152
153static struct irq_cfg *irq_cfgx;
Yinghai Luda51a822008-08-19 20:50:25 -0700154static struct irq_cfg *irq_cfgx_free;
155static void __init init_work(void *data)
156{
157 struct dyn_array *da = data;
158 struct irq_cfg *cfg;
159 int legacy_count;
160 int i;
161
162 cfg = *da->name;
163
164 memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
165
166 legacy_count = sizeof(irq_cfg_legacy)/sizeof(irq_cfg_legacy[0]);
167 for (i = legacy_count; i < *da->nr; i++)
168 init_one_irq_cfg(&cfg[i]);
169
170 for (i = 1; i < *da->nr; i++)
171 cfg[i-1].next = &cfg[i];
172
173 irq_cfgx_free = &irq_cfgx[legacy_count];
174 irq_cfgx[legacy_count - 1].next = NULL;
175}
176
177#define for_each_irq_cfg(cfg) \
178 for (cfg = irq_cfgx; cfg; cfg = cfg->next)
179
180DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irq_cfg, PAGE_SIZE, init_work);
Yinghai Lua1420f32008-08-19 20:50:24 -0700181
182static struct irq_cfg *irq_cfg(unsigned int irq)
183{
Yinghai Luda51a822008-08-19 20:50:25 -0700184 struct irq_cfg *cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700185
Yinghai Luda51a822008-08-19 20:50:25 -0700186 cfg = irq_cfgx;
187 while (cfg) {
188 if (cfg->irq == irq)
189 return cfg;
190
191 cfg = cfg->next;
192 }
193
194 return NULL;
195}
196
197static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
198{
199 struct irq_cfg *cfg, *cfg_pri;
200 int i;
201 int count = 0;
202
203 cfg_pri = cfg = irq_cfgx;
204 while (cfg) {
205 if (cfg->irq == irq)
206 return cfg;
207
208 cfg_pri = cfg;
209 cfg = cfg->next;
210 count++;
211 }
212
213 if (!irq_cfgx_free) {
214 unsigned long phys;
215 unsigned long total_bytes;
216 /*
217 * we run out of pre-allocate ones, allocate more
218 */
219 printk(KERN_DEBUG "try to get more irq_cfg %d\n", nr_irq_cfg);
220
221 total_bytes = sizeof(struct irq_cfg) * nr_irq_cfg;
222 if (after_bootmem)
223 cfg = kzalloc(total_bytes, GFP_ATOMIC);
224 else
225 cfg = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
226
227 if (!cfg)
228 panic("please boot with nr_irq_cfg= %d\n", count * 2);
229
230 phys = __pa(cfg);
231 printk(KERN_DEBUG "irq_irq ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
232
233 for (i = 0; i < nr_irq_cfg; i++)
234 init_one_irq_cfg(&cfg[i]);
235
236 for (i = 1; i < nr_irq_cfg; i++)
237 cfg[i-1].next = &cfg[i];
238
239 irq_cfgx_free = cfg;
240 }
241
242 cfg = irq_cfgx_free;
243 irq_cfgx_free = irq_cfgx_free->next;
244 cfg->next = NULL;
245 if (cfg_pri)
246 cfg_pri->next = cfg;
247 else
248 irq_cfgx = cfg;
249 cfg->irq = irq;
250 printk(KERN_DEBUG "found new irq_cfg for irq %d\n", cfg->irq);
251
252#ifdef CONFIG_HAVE_SPARSE_IRQ_DEBUG
253 {
254 /* dump the results */
255 struct irq_cfg *cfg;
256 unsigned long phys;
257 unsigned long bytes = sizeof(struct irq_cfg);
258
259 printk(KERN_DEBUG "=========================== %d\n", irq);
260 printk(KERN_DEBUG "irq_cfg dump after get that for %d\n", irq);
261 for_each_irq_cfg(cfg) {
262 phys = __pa(cfg);
263 printk(KERN_DEBUG "irq_cfg %d ==> [%#lx - %#lx]\n", cfg->irq, phys, phys + bytes);
264 }
265 printk(KERN_DEBUG "===========================\n");
266 }
267#endif
268 return cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700269}
270
Yinghai Lu497c9a12008-08-19 20:50:28 -0700271static int assign_irq_vector(int irq, cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
273 * Rough estimation of how many shared IRQs there are, can
274 * be changed anytime.
275 */
Yinghai Lu301e6192008-08-19 20:50:02 -0700276int pin_map_size;
Yinghai Lu0799e432008-08-19 20:49:48 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/*
279 * This is performance-critical, we want to do it O(1)
280 *
281 * the indexing order of this array favors 1:1 mappings
282 * between pins and IRQs.
283 */
284
Yinghai Lu0f978f42008-08-19 20:50:26 -0700285struct irq_pin_list {
286 int apic, pin;
287 struct irq_pin_list *next;
288};
Yinghai Lu301e6192008-08-19 20:50:02 -0700289
Yinghai Lu0f978f42008-08-19 20:50:26 -0700290static struct irq_pin_list *irq_2_pin_head;
291/* fill one page ? */
292static int nr_irq_2_pin = 0x100;
293static struct irq_pin_list *irq_2_pin_ptr;
294static void __init irq_2_pin_init_work(void *data)
295{
296 struct dyn_array *da = data;
297 struct irq_pin_list *pin;
298 int i;
299
300 pin = *da->name;
301
302 for (i = 1; i < *da->nr; i++)
303 pin[i-1].next = &pin[i];
304
305 irq_2_pin_ptr = &pin[0];
306}
307DEFINE_DYN_ARRAY(irq_2_pin_head, sizeof(struct irq_pin_list), nr_irq_2_pin, PAGE_SIZE, irq_2_pin_init_work);
308
309static struct irq_pin_list *get_one_free_irq_2_pin(void)
310{
311 struct irq_pin_list *pin;
312 int i;
313
314 pin = irq_2_pin_ptr;
315
316 if (pin) {
317 irq_2_pin_ptr = pin->next;
318 pin->next = NULL;
319 return pin;
320 }
321
322 /*
323 * we run out of pre-allocate ones, allocate more
324 */
325 printk(KERN_DEBUG "try to get more irq_2_pin %d\n", nr_irq_2_pin);
326
327 if (after_bootmem)
328 pin = kzalloc(sizeof(struct irq_pin_list)*nr_irq_2_pin,
329 GFP_ATOMIC);
330 else
331 pin = __alloc_bootmem_nopanic(sizeof(struct irq_pin_list) *
332 nr_irq_2_pin, PAGE_SIZE, 0);
333
334 if (!pin)
335 panic("can not get more irq_2_pin\n");
336
337 for (i = 1; i < nr_irq_2_pin; i++)
338 pin[i-1].next = &pin[i];
339
340 irq_2_pin_ptr = pin->next;
341 pin->next = NULL;
342
343 return pin;
344}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Linus Torvalds130fe052006-11-01 09:11:00 -0800346struct io_apic {
347 unsigned int index;
348 unsigned int unused[3];
349 unsigned int data;
350};
351
352static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
353{
354 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400355 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800356}
357
358static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
359{
360 struct io_apic __iomem *io_apic = io_apic_base(apic);
361 writel(reg, &io_apic->index);
362 return readl(&io_apic->data);
363}
364
365static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
366{
367 struct io_apic __iomem *io_apic = io_apic_base(apic);
368 writel(reg, &io_apic->index);
369 writel(value, &io_apic->data);
370}
371
372/*
373 * Re-write a value: to be used for read-modify-write
374 * cycles where the read already set up the index register.
375 *
376 * Older SiS APIC requires we rewrite the index register
377 */
378static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
379{
Al Virocb468982007-02-09 16:39:25 +0000380 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800381 if (sis_apic_bug)
382 writel(reg, &io_apic->index);
383 writel(value, &io_apic->data);
384}
385
Andi Kleencf4c6a22006-09-26 10:52:30 +0200386union entry_union {
387 struct { u32 w1, w2; };
388 struct IO_APIC_route_entry entry;
389};
390
391static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
392{
393 union entry_union eu;
394 unsigned long flags;
395 spin_lock_irqsave(&ioapic_lock, flags);
396 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
397 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
398 spin_unlock_irqrestore(&ioapic_lock, flags);
399 return eu.entry;
400}
401
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800402/*
403 * When we write a new IO APIC routing entry, we need to write the high
404 * word first! If the mask bit in the low word is clear, we will enable
405 * the interrupt, and we need to make sure the entry is fully populated
406 * before that happens.
407 */
Andi Kleend15512f2006-12-07 02:14:07 +0100408static void
409__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
410{
411 union entry_union eu;
412 eu.entry = e;
413 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
414 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
415}
416
Andi Kleencf4c6a22006-09-26 10:52:30 +0200417static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
418{
419 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200420 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100421 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800422 spin_unlock_irqrestore(&ioapic_lock, flags);
423}
424
425/*
426 * When we mask an IO APIC routing entry, we need to write the low
427 * word first, in order to set the mask bit before we change the
428 * high bits!
429 */
430static void ioapic_mask_entry(int apic, int pin)
431{
432 unsigned long flags;
433 union entry_union eu = { .entry.mask = 1 };
434
435 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200436 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
437 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
438 spin_unlock_irqrestore(&ioapic_lock, flags);
439}
440
Yinghai Lu497c9a12008-08-19 20:50:28 -0700441#ifdef CONFIG_SMP
442static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
443{
444 int apic, pin;
445 struct irq_cfg *cfg;
446 struct irq_pin_list *entry;
447
448 cfg = irq_cfg(irq);
449 entry = cfg->irq_2_pin;
450 for (;;) {
451 unsigned int reg;
452
453 if (!entry)
454 break;
455
456 apic = entry->apic;
457 pin = entry->pin;
458 io_apic_write(apic, 0x11 + pin*2, dest);
459 reg = io_apic_read(apic, 0x10 + pin*2);
460 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
461 reg |= vector;
462 io_apic_modify(apic, 0x10 + pin *2, reg);
463 if (!entry->next)
464 break;
465 entry = entry->next;
466 }
467}
468static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
469{
470 struct irq_cfg *cfg;
471 unsigned long flags;
472 unsigned int dest;
473 cpumask_t tmp;
474
475 cfg = irq_cfg(irq);
476
477 cpus_and(tmp, mask, cpu_online_map);
478 if (cpus_empty(tmp))
479 return;
480
481 if (assign_irq_vector(irq, mask))
482 return;
483
484 cpus_and(tmp, cfg->domain, mask);
485
486 dest = cpu_mask_to_apicid(tmp);
487 /*
488 * Only the high 8 bits are valid.
489 */
490 dest = SET_APIC_LOGICAL_ID(dest);
491
492 spin_lock_irqsave(&ioapic_lock, flags);
493 __target_IO_APIC_irq(irq, dest, cfg->vector);
494 irq_to_desc(irq)->affinity = mask;
495 spin_unlock_irqrestore(&ioapic_lock, flags);
496}
497
498#endif /* CONFIG_SMP */
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
501 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
502 * shared ISA-space IRQs, so we have to support them. We are super
503 * fast in the common case, and fast for shared ISA-space IRQs.
504 */
505static void add_pin_to_irq(unsigned int irq, int apic, int pin)
506{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700507 struct irq_cfg *cfg;
508 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Yinghai Lu0f978f42008-08-19 20:50:26 -0700510 /* first time to refer irq_cfg, so with new */
511 cfg = irq_cfg_alloc(irq);
512 entry = cfg->irq_2_pin;
513 if (!entry) {
514 entry = get_one_free_irq_2_pin();
515 cfg->irq_2_pin = entry;
516 entry->apic = apic;
517 entry->pin = pin;
518 printk(KERN_DEBUG " 0 add_pin_to_irq: irq %d --> apic %d pin %d\n", irq, apic, pin);
519 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700521
522 while (entry->next) {
523 /* not again, please */
524 if (entry->apic == apic && entry->pin == pin)
525 return;
526
527 entry = entry->next;
528 }
529
530 entry->next = get_one_free_irq_2_pin();
531 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 entry->apic = apic;
533 entry->pin = pin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700534 printk(KERN_DEBUG " x add_pin_to_irq: irq %d --> apic %d pin %d\n", irq, apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537/*
538 * Reroute an IRQ to a different pin.
539 */
540static void __init replace_pin_at_irq(unsigned int irq,
541 int oldapic, int oldpin,
542 int newapic, int newpin)
543{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700544 struct irq_cfg *cfg = irq_cfg(irq);
545 struct irq_pin_list *entry = cfg->irq_2_pin;
546 int replaced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Yinghai Lu0f978f42008-08-19 20:50:26 -0700548 while (entry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (entry->apic == oldapic && entry->pin == oldpin) {
550 entry->apic = newapic;
551 entry->pin = newpin;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700552 replaced = 1;
553 /* every one is different, right? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700555 }
556 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Yinghai Lu0f978f42008-08-19 20:50:26 -0700558
559 /* why? call replace before add? */
560 if (!replaced)
561 add_pin_to_irq(irq, newapic, newpin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200564static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Yinghai Lu0f978f42008-08-19 20:50:26 -0700566 struct irq_cfg *cfg;
567 struct irq_pin_list *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned int pin, reg;
569
Yinghai Lu0f978f42008-08-19 20:50:26 -0700570 cfg = irq_cfg(irq);
571 entry = cfg->irq_2_pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 for (;;) {
Yinghai Lu0f978f42008-08-19 20:50:26 -0700573 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700575 pin = entry->pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 reg = io_apic_read(entry->apic, 0x10 + pin*2);
577 reg &= ~disable;
578 reg |= enable;
579 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
580 if (!entry->next)
581 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -0700582 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584}
585
586/* mask = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200587static void __mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400589 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592/* mask = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200593static void __unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400595 __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598/* mask = 1, trigger = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200599static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400601 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
602 IO_APIC_REDIR_LEVEL_TRIGGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/* mask = 0, trigger = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200606static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400608 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
609 IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200612static void mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 unsigned long flags;
615
616 spin_lock_irqsave(&ioapic_lock, flags);
617 __mask_IO_APIC_irq(irq);
618 spin_unlock_irqrestore(&ioapic_lock, flags);
619}
620
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200621static void unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 unsigned long flags;
624
625 spin_lock_irqsave(&ioapic_lock, flags);
626 __unmask_IO_APIC_irq(irq);
627 spin_unlock_irqrestore(&ioapic_lock, flags);
628}
629
630static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
631{
632 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200635 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 if (entry.delivery_mode == dest_SMI)
637 return;
638
639 /*
640 * Disable it in the IO-APIC irq-routing table:
641 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800642 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200645static void clear_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 int apic, pin;
648
649 for (apic = 0; apic < nr_ioapics; apic++)
650 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
651 clear_IO_APIC_pin(apic, pin);
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654#ifndef CONFIG_SMP
Harvey Harrison75604d72008-01-30 13:31:17 +0100655void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 unsigned int cfg;
658
659 /*
660 * Wait for idle.
661 */
662 apic_wait_icr_idle();
663 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
664 /*
665 * Send the IPI. The write to APIC_ICR fires this off.
666 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100667 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669#endif /* !CONFIG_SMP */
670
671
672/*
673 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
674 * specific CPU-side IRQs.
675 */
676
677#define MAX_PIRQS 8
678static int pirq_entries [MAX_PIRQS];
679static int pirqs_enabled;
680int skip_ioapic_setup;
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static int __init ioapic_pirq_setup(char *str)
683{
684 int i, max;
685 int ints[MAX_PIRQS+1];
686
687 get_options(str, ARRAY_SIZE(ints), ints);
688
689 for (i = 0; i < MAX_PIRQS; i++)
690 pirq_entries[i] = -1;
691
692 pirqs_enabled = 1;
693 apic_printk(APIC_VERBOSE, KERN_INFO
694 "PIRQ redirection, working around broken MP-BIOS.\n");
695 max = MAX_PIRQS;
696 if (ints[0] < MAX_PIRQS)
697 max = ints[0];
698
699 for (i = 0; i < max; i++) {
700 apic_printk(APIC_VERBOSE, KERN_DEBUG
701 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
702 /*
703 * PIRQs are mapped upside down, usually.
704 */
705 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
706 }
707 return 1;
708}
709
710__setup("pirq=", ioapic_pirq_setup);
711
712/*
713 * Find the IRQ entry number of a certain pin.
714 */
715static int find_irq_entry(int apic, int pin, int type)
716{
717 int i;
718
719 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400720 if (mp_irqs[i].mp_irqtype == type &&
721 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
722 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
723 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 return i;
725
726 return -1;
727}
728
729/*
730 * Find the pin to which IRQ[irq] (ISA) is connected
731 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800732static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 int i;
735
736 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400737 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300739 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400740 (mp_irqs[i].mp_irqtype == type) &&
741 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400743 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745 return -1;
746}
747
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800748static int __init find_isa_irq_apic(int irq, int type)
749{
750 int i;
751
752 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400753 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800754
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300755 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400756 (mp_irqs[i].mp_irqtype == type) &&
757 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800758 break;
759 }
760 if (i < mp_irq_entries) {
761 int apic;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200762 for (apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400763 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800764 return apic;
765 }
766 }
767
768 return -1;
769}
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/*
772 * Find a specific PCI IRQ entry.
773 * Not an __init, possibly needed by modules
774 */
775static int pin_2_irq(int idx, int apic, int pin);
776
777int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
778{
779 int apic, i, best_guess = -1;
780
781 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
782 "slot:%d, pin:%d.\n", bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400783 if (test_bit(bus, mp_bus_not_pci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
785 return -1;
786 }
787 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400788 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400791 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
792 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 break;
794
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300795 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400796 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400798 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar3de352b2008-07-08 11:14:58 +0200799 int irq = pin_2_irq(i, apic, mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (!(apic || IO_APIC_IRQ(irq)))
802 continue;
803
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400804 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return irq;
806 /*
807 * Use the first all-but-pin matching entry as a
808 * best-guess fuzzy result for broken mptables.
809 */
810 if (best_guess < 0)
811 best_guess = irq;
812 }
813 }
814 return best_guess;
815}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700816EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300818#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819/*
820 * EISA Edge/Level control register, ELCR
821 */
822static int EISA_ELCR(unsigned int irq)
823{
824 if (irq < 16) {
825 unsigned int port = 0x4d0 + (irq >> 3);
826 return (inb(port) >> (irq & 7)) & 1;
827 }
828 apic_printk(APIC_VERBOSE, KERN_INFO
829 "Broken MPtable reports ISA irq %d\n", irq);
830 return 0;
831}
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300832#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300834/* ISA interrupts are always polarity zero edge triggered,
835 * when listed as conforming in the MP table. */
836
837#define default_ISA_trigger(idx) (0)
838#define default_ISA_polarity(idx) (0)
839
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840/* EISA interrupts are always polarity zero and can be edge or level
841 * trigger depending on the ELCR value. If an interrupt is listed as
842 * EISA conforming in the MP table, that means its trigger type must
843 * be read in from the ELCR */
844
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400845#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300846#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848/* PCI interrupts are always polarity one level triggered,
849 * when listed as conforming in the MP table. */
850
851#define default_PCI_trigger(idx) (1)
852#define default_PCI_polarity(idx) (1)
853
854/* MCA interrupts are always polarity zero level triggered,
855 * when listed as conforming in the MP table. */
856
857#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300858#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Shaohua Li61fd47e2007-11-17 01:05:28 -0500860static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400862 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 int polarity;
864
865 /*
866 * Determine IRQ line polarity (high active or low active):
867 */
Ingo Molnar3de352b2008-07-08 11:14:58 +0200868 switch (mp_irqs[idx].mp_irqflag & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200869 case 0: /* conforms, ie. bus-type dependent polarity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200871 polarity = test_bit(bus, mp_bus_not_pci)?
872 default_ISA_polarity(idx):
873 default_PCI_polarity(idx);
874 break;
875 }
876 case 1: /* high active */
877 {
878 polarity = 0;
879 break;
880 }
881 case 2: /* reserved */
882 {
883 printk(KERN_WARNING "broken BIOS!!\n");
884 polarity = 1;
885 break;
886 }
887 case 3: /* low active */
888 {
889 polarity = 1;
890 break;
891 }
892 default: /* invalid */
893 {
894 printk(KERN_WARNING "broken BIOS!!\n");
895 polarity = 1;
896 break;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899 return polarity;
900}
901
902static int MPBIOS_trigger(int idx)
903{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400904 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 int trigger;
906
907 /*
908 * Determine IRQ trigger mode (edge or level sensitive):
909 */
Ingo Molnar3de352b2008-07-08 11:14:58 +0200910 switch ((mp_irqs[idx].mp_irqflag>>2) & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200911 case 0: /* conforms, ie. bus-type dependent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200913 trigger = test_bit(bus, mp_bus_not_pci)?
914 default_ISA_trigger(idx):
915 default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300916#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200917 switch (mp_bus_id_to_type[bus]) {
918 case MP_BUS_ISA: /* ISA pin */
919 {
920 /* set before the switch */
921 break;
922 }
923 case MP_BUS_EISA: /* EISA pin */
924 {
925 trigger = default_EISA_trigger(idx);
926 break;
927 }
928 case MP_BUS_PCI: /* PCI pin */
929 {
930 /* set before the switch */
931 break;
932 }
933 case MP_BUS_MCA: /* MCA pin */
934 {
935 trigger = default_MCA_trigger(idx);
936 break;
937 }
938 default:
939 {
940 printk(KERN_WARNING "broken BIOS!!\n");
941 trigger = 1;
942 break;
943 }
944 }
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300945#endif
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200946 break;
947 }
948 case 1: /* edge */
949 {
950 trigger = 0;
951 break;
952 }
953 case 2: /* reserved */
954 {
955 printk(KERN_WARNING "broken BIOS!!\n");
956 trigger = 1;
957 break;
958 }
959 case 3: /* level */
960 {
961 trigger = 1;
962 break;
963 }
964 default: /* invalid */
965 {
966 printk(KERN_WARNING "broken BIOS!!\n");
967 trigger = 0;
968 break;
969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 }
971 return trigger;
972}
973
974static inline int irq_polarity(int idx)
975{
976 return MPBIOS_polarity(idx);
977}
978
979static inline int irq_trigger(int idx)
980{
981 return MPBIOS_trigger(idx);
982}
983
984static int pin_2_irq(int idx, int apic, int pin)
985{
986 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400987 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989 /*
990 * Debugging check, we are in big trouble if this message pops up!
991 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400992 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
994
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300995 if (test_bit(bus, mp_bus_not_pci))
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400996 irq = mp_irqs[idx].mp_srcbusirq;
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300997 else {
998 /*
999 * PCI IRQs are mapped in order
1000 */
1001 i = irq = 0;
1002 while (i < apic)
1003 irq += nr_ioapic_registers[i++];
1004 irq += pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001006 /*
1007 * For MPS mode, so far only needed by ES7000 platform
1008 */
1009 if (ioapic_renumber_irq)
1010 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
1013 /*
1014 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1015 */
1016 if ((pin >= 16) && (pin <= 23)) {
1017 if (pirq_entries[pin-16] != -1) {
1018 if (!pirq_entries[pin-16]) {
1019 apic_printk(APIC_VERBOSE, KERN_DEBUG
1020 "disabling PIRQ%d\n", pin-16);
1021 } else {
1022 irq = pirq_entries[pin-16];
1023 apic_printk(APIC_VERBOSE, KERN_DEBUG
1024 "using PIRQ%d -> IRQ %d\n",
1025 pin-16, irq);
1026 }
1027 }
1028 }
1029 return irq;
1030}
1031
1032static inline int IO_APIC_irq_trigger(int irq)
1033{
1034 int apic, idx, pin;
1035
1036 for (apic = 0; apic < nr_ioapics; apic++) {
1037 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001038 idx = find_irq_entry(apic, pin, mp_INT);
1039 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return irq_trigger(idx);
1041 }
1042 }
1043 /*
1044 * nonexistent IRQs are edge default
1045 */
1046 return 0;
1047}
1048
Yinghai Lu497c9a12008-08-19 20:50:28 -07001049void lock_vector_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001051 /* Used to the online set of cpus does not change
1052 * during assign_irq_vector.
1053 */
1054 spin_lock(&vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Yinghai Lu497c9a12008-08-19 20:50:28 -07001057void unlock_vector_lock(void)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001058{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001059 spin_unlock(&vector_lock);
1060}
1061
1062static int __assign_irq_vector(int irq, cpumask_t mask)
1063{
1064 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1065 unsigned int old_vector;
1066 int cpu;
1067 struct irq_cfg *cfg;
1068
1069 cfg = irq_cfg(irq);
1070
1071 /* Only try and allocate irqs on cpus that are present */
1072 cpus_and(mask, mask, cpu_online_map);
1073
1074 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1075 return -EBUSY;
1076
1077 old_vector = cfg->vector;
1078 if (old_vector) {
1079 cpumask_t tmp;
1080 cpus_and(tmp, cfg->domain, mask);
1081 if (!cpus_empty(tmp))
1082 return 0;
1083 }
1084
1085 for_each_cpu_mask_nr(cpu, mask) {
1086 cpumask_t domain, new_mask;
1087 int new_cpu;
1088 int vector, offset;
1089
1090 domain = vector_allocation_domain(cpu);
1091 cpus_and(new_mask, domain, cpu_online_map);
1092
1093 vector = current_vector;
1094 offset = current_offset;
1095next:
1096 vector += 8;
1097 if (vector >= first_system_vector) {
1098 /* If we run out of vectors on large boxen, must share them. */
1099 offset = (offset + 1) % 8;
1100 vector = FIRST_DEVICE_VECTOR + offset;
1101 }
1102 if (unlikely(current_vector == vector))
1103 continue;
1104 if (vector == SYSCALL_VECTOR)
1105 goto next;
1106
1107 for_each_cpu_mask_nr(new_cpu, new_mask)
1108 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1109 goto next;
1110 /* Found one! */
1111 current_vector = vector;
1112 current_offset = offset;
1113 if (old_vector) {
1114 cfg->move_in_progress = 1;
1115 cfg->old_domain = cfg->domain;
1116 }
Yinghai Lu7a959cf2008-08-19 20:50:32 -07001117 printk(KERN_DEBUG "assign_irq_vector: irq %d vector %#x cpu ", irq, vector);
1118 for_each_cpu_mask_nr(new_cpu, new_mask) {
1119 per_cpu(vector_irq, new_cpu)[vector] = irq;
1120 printk(KERN_CONT " %d ", new_cpu);
1121 }
1122 printk(KERN_CONT "\n");
Yinghai Lu497c9a12008-08-19 20:50:28 -07001123 cfg->vector = vector;
1124 cfg->domain = domain;
1125 return 0;
1126 }
1127 return -ENOSPC;
1128}
1129
1130static int assign_irq_vector(int irq, cpumask_t mask)
1131{
1132 int err;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001133 unsigned long flags;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001134
1135 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07001136 err = __assign_irq_vector(irq, mask);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001137 spin_unlock_irqrestore(&vector_lock, flags);
1138
Yinghai Lu497c9a12008-08-19 20:50:28 -07001139 return err;
1140}
1141
1142static void __clear_irq_vector(int irq)
1143{
1144 struct irq_cfg *cfg;
1145 cpumask_t mask;
1146 int cpu, vector;
1147
1148 cfg = irq_cfg(irq);
1149 BUG_ON(!cfg->vector);
1150
1151 vector = cfg->vector;
1152 cpus_and(mask, cfg->domain, cpu_online_map);
1153 for_each_cpu_mask_nr(cpu, mask)
1154 per_cpu(vector_irq, cpu)[vector] = -1;
1155
1156 cfg->vector = 0;
1157 cpus_clear(cfg->domain);
1158}
1159
1160void __setup_vector_irq(int cpu)
1161{
1162 /* Initialize vector_irq on a new cpu */
1163 /* This function must be called with vector_lock held */
1164 int irq, vector;
1165 struct irq_cfg *cfg;
1166
1167 /* Mark the inuse vectors */
1168 for_each_irq_cfg(cfg) {
1169 if (!cpu_isset(cpu, cfg->domain))
1170 continue;
1171 vector = cfg->vector;
1172 irq = cfg->irq;
1173 per_cpu(vector_irq, cpu)[vector] = irq;
1174 }
1175 /* Mark the free vectors */
1176 for (vector = 0; vector < NR_VECTORS; ++vector) {
1177 irq = per_cpu(vector_irq, cpu)[vector];
1178 if (irq < 0)
1179 continue;
1180
1181 cfg = irq_cfg(irq);
1182 if (!cpu_isset(cpu, cfg->domain))
1183 per_cpu(vector_irq, cpu)[vector] = -1;
1184 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001185}
Glauber Costa3fde6902008-05-28 20:34:19 -07001186
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001187static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189#define IOAPIC_AUTO -1
1190#define IOAPIC_EDGE 0
1191#define IOAPIC_LEVEL 1
1192
Yinghai Lu497c9a12008-08-19 20:50:28 -07001193static void ioapic_register_intr(int irq, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
Yinghai Lu08678b02008-08-19 20:50:05 -07001195 struct irq_desc *desc;
1196
Yinghai Lu199751d2008-08-19 20:50:27 -07001197 /* first time to use this irq_desc */
1198 if (irq < 16)
1199 desc = irq_to_desc(irq);
1200 else
1201 desc = irq_to_desc_alloc(irq);
1202
Jan Beulich6ebcc002006-06-26 13:56:46 +02001203 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001204 trigger == IOAPIC_LEVEL) {
Yinghai Lu08678b02008-08-19 20:50:05 -07001205 desc->status |= IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001206 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1207 handle_fasteoi_irq, "fasteoi");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001208 } else {
Yinghai Lu08678b02008-08-19 20:50:05 -07001209 desc->status &= ~IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001210 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1211 handle_edge_irq, "edge");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001212 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07001213}
1214
1215static int setup_ioapic_entry(int apic, int irq,
1216 struct IO_APIC_route_entry *entry,
1217 unsigned int destination, int trigger,
1218 int polarity, int vector)
1219{
1220 /*
1221 * add it to the IO-APIC irq-routing table:
1222 */
1223 memset(entry,0,sizeof(*entry));
1224
1225 entry->delivery_mode = INT_DELIVERY_MODE;
1226 entry->dest_mode = INT_DEST_MODE;
1227 entry->dest.logical.logical_dest = destination;
1228
1229 entry->mask = 0; /* enable IRQ */
1230 entry->trigger = trigger;
1231 entry->polarity = polarity;
1232 entry->vector = vector;
1233
1234 /* Mask level triggered irqs.
1235 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1236 */
1237 if (trigger)
1238 entry->mask = 1;
1239
1240 return 0;
1241}
1242
1243static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
1244 int trigger, int polarity)
1245{
1246 struct irq_cfg *cfg;
1247 struct IO_APIC_route_entry entry;
1248 cpumask_t mask;
1249
1250 if (!IO_APIC_IRQ(irq))
1251 return;
1252
1253 cfg = irq_cfg(irq);
1254
1255 mask = TARGET_CPUS;
1256 if (assign_irq_vector(irq, mask))
1257 return;
1258
1259 cpus_and(mask, cfg->domain, mask);
1260
1261 apic_printk(APIC_VERBOSE,KERN_DEBUG
1262 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1263 "IRQ %d Mode:%i Active:%i)\n",
1264 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1265 irq, trigger, polarity);
1266
1267
1268 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1269 cpu_mask_to_apicid(mask), trigger, polarity,
1270 cfg->vector)) {
1271 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1272 mp_ioapics[apic].mp_apicid, pin);
1273 __clear_irq_vector(irq);
1274 return;
1275 }
1276
1277 ioapic_register_intr(irq, trigger);
1278 if (irq < 16)
1279 disable_8259A_irq(irq);
1280
1281 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
1284static void __init setup_IO_APIC_irqs(void)
1285{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001286 int apic, pin, idx, irq, first_notcon = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1289
1290 for (apic = 0; apic < nr_ioapics; apic++) {
1291 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1292
Yinghai Lu497c9a12008-08-19 20:50:28 -07001293 idx = find_irq_entry(apic,pin,mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (idx == -1) {
1295 if (first_notcon) {
Yinghai Lu497c9a12008-08-19 20:50:28 -07001296 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 first_notcon = 0;
1298 } else
Yinghai Lu497c9a12008-08-19 20:50:28 -07001299 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 continue;
1301 }
Yinghai Lu20d225b2007-10-17 18:04:41 +02001302 if (!first_notcon) {
1303 apic_printk(APIC_VERBOSE, " not connected.\n");
1304 first_notcon = 1;
1305 }
1306
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 irq = pin_2_irq(idx, apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Yinghai Lu497c9a12008-08-19 20:50:28 -07001309 if (multi_timer_check(apic, irq))
1310 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Yinghai Lu497c9a12008-08-19 20:50:28 -07001312 add_pin_to_irq(irq, apic, pin);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001313
Yinghai Lu497c9a12008-08-19 20:50:28 -07001314 setup_IO_APIC_irq(apic, pin, irq,
1315 irq_trigger(idx), irq_polarity(idx));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 }
1318
1319 if (!first_notcon)
1320 apic_printk(APIC_VERBOSE, " not connected.\n");
1321}
1322
1323/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001324 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001326static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1327 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328{
1329 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001331 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 /*
1334 * We use logical delivery to get the timer IRQ
1335 * to the first CPU.
1336 */
1337 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001338 entry.mask = 1; /* mask IRQ now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1340 entry.delivery_mode = INT_DELIVERY_MODE;
1341 entry.polarity = 0;
1342 entry.trigger = 0;
1343 entry.vector = vector;
1344
1345 /*
1346 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001347 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 */
Yinghai Lu497c9a12008-08-19 20:50:28 -07001349 ioapic_register_intr(0, IOAPIC_EDGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /*
1352 * Add it to the IO-APIC irq-routing table:
1353 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001354 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355}
1356
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001357
1358__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
1360 int apic, i;
1361 union IO_APIC_reg_00 reg_00;
1362 union IO_APIC_reg_01 reg_01;
1363 union IO_APIC_reg_02 reg_02;
1364 union IO_APIC_reg_03 reg_03;
1365 unsigned long flags;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001366 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 if (apic_verbosity == APIC_QUIET)
1369 return;
1370
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001371 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 for (i = 0; i < nr_ioapics; i++)
1373 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001374 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /*
1377 * We are a bit conservative about what we expect. We have to
1378 * know about every hardware change ASAP.
1379 */
1380 printk(KERN_INFO "testing the IO APIC.......................\n");
1381
1382 for (apic = 0; apic < nr_ioapics; apic++) {
1383
1384 spin_lock_irqsave(&ioapic_lock, flags);
1385 reg_00.raw = io_apic_read(apic, 0);
1386 reg_01.raw = io_apic_read(apic, 1);
1387 if (reg_01.bits.version >= 0x10)
1388 reg_02.raw = io_apic_read(apic, 2);
1389 if (reg_01.bits.version >= 0x20)
1390 reg_03.raw = io_apic_read(apic, 3);
1391 spin_unlock_irqrestore(&ioapic_lock, flags);
1392
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001393 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1395 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1396 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1397 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
1399 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1400 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1403 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /*
1406 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1407 * but the value of reg_02 is read as the previous read register
1408 * value, so ignore it if reg_02 == reg_01.
1409 */
1410 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1411 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1412 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
1415 /*
1416 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1417 * or reg_03, but the value of reg_0[23] is read as the previous read
1418 * register value, so ignore it if reg_03 == reg_0[12].
1419 */
1420 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1421 reg_03.raw != reg_01.raw) {
1422 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1423 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425
1426 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1427
1428 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1429 " Stat Dest Deli Vect: \n");
1430
1431 for (i = 0; i <= reg_01.bits.entries; i++) {
1432 struct IO_APIC_route_entry entry;
1433
Andi Kleencf4c6a22006-09-26 10:52:30 +02001434 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 printk(KERN_DEBUG " %02x %03X %02X ",
1437 i,
1438 entry.dest.logical.logical_dest,
1439 entry.dest.physical.physical_dest
1440 );
1441
1442 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1443 entry.mask,
1444 entry.trigger,
1445 entry.irr,
1446 entry.polarity,
1447 entry.delivery_status,
1448 entry.dest_mode,
1449 entry.delivery_mode,
1450 entry.vector
1451 );
1452 }
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0f978f42008-08-19 20:50:26 -07001455 for_each_irq_cfg(cfg) {
1456 struct irq_pin_list *entry = cfg->irq_2_pin;
1457 if (!entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 continue;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001459 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 for (;;) {
1461 printk("-> %d:%d", entry->apic, entry->pin);
1462 if (!entry->next)
1463 break;
Yinghai Lu0f978f42008-08-19 20:50:26 -07001464 entry = entry->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 }
1466 printk("\n");
1467 }
1468
1469 printk(KERN_INFO ".................................... done.\n");
1470
1471 return;
1472}
1473
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001474__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
1476 unsigned int v;
1477 int i, j;
1478
1479 if (apic_verbosity == APIC_QUIET)
1480 return;
1481
1482 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1483 for (i = 0; i < 8; i++) {
1484 v = apic_read(base + i*0x10);
1485 for (j = 0; j < 32; j++) {
1486 if (v & (1<<j))
1487 printk("1");
1488 else
1489 printk("0");
1490 }
1491 printk("\n");
1492 }
1493}
1494
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001495__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001498 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 if (apic_verbosity == APIC_QUIET)
1501 return;
1502
1503 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1504 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001505 v = apic_read(APIC_ID);
Jack Steiner05f2d122008-03-28 14:12:02 -05001506 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001507 GET_APIC_ID(v));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 v = apic_read(APIC_LVR);
1509 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1510 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001511 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 v = apic_read(APIC_TASKPRI);
1514 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1515
1516 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1517 v = apic_read(APIC_ARBPRI);
1518 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1519 v & APIC_ARBPRI_MASK);
1520 v = apic_read(APIC_PROCPRI);
1521 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1522 }
1523
1524 v = apic_read(APIC_EOI);
1525 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1526 v = apic_read(APIC_RRR);
1527 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1528 v = apic_read(APIC_LDR);
1529 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1530 v = apic_read(APIC_DFR);
1531 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1532 v = apic_read(APIC_SPIV);
1533 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1534
1535 printk(KERN_DEBUG "... APIC ISR field:\n");
1536 print_APIC_bitfield(APIC_ISR);
1537 printk(KERN_DEBUG "... APIC TMR field:\n");
1538 print_APIC_bitfield(APIC_TMR);
1539 printk(KERN_DEBUG "... APIC IRR field:\n");
1540 print_APIC_bitfield(APIC_IRR);
1541
1542 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1543 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1544 apic_write(APIC_ESR, 0);
1545 v = apic_read(APIC_ESR);
1546 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1547 }
1548
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001549 icr = apic_icr_read();
1550 printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
1551 printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
1553 v = apic_read(APIC_LVTT);
1554 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1555
1556 if (maxlvt > 3) { /* PC is LVT#4. */
1557 v = apic_read(APIC_LVTPC);
1558 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1559 }
1560 v = apic_read(APIC_LVT0);
1561 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1562 v = apic_read(APIC_LVT1);
1563 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1564
1565 if (maxlvt > 2) { /* ERR is LVT#3. */
1566 v = apic_read(APIC_LVTERR);
1567 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1568 }
1569
1570 v = apic_read(APIC_TMICT);
1571 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1572 v = apic_read(APIC_TMCCT);
1573 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1574 v = apic_read(APIC_TDCR);
1575 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1576 printk("\n");
1577}
1578
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001579__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001581 on_each_cpu(print_local_APIC, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001584__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 unsigned int v;
1587 unsigned long flags;
1588
1589 if (apic_verbosity == APIC_QUIET)
1590 return;
1591
1592 printk(KERN_DEBUG "\nprinting PIC contents\n");
1593
1594 spin_lock_irqsave(&i8259A_lock, flags);
1595
1596 v = inb(0xa1) << 8 | inb(0x21);
1597 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1598
1599 v = inb(0xa0) << 8 | inb(0x20);
1600 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1601
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001602 outb(0x0b, 0xa0);
1603 outb(0x0b, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 v = inb(0xa0) << 8 | inb(0x20);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001605 outb(0x0a, 0xa0);
1606 outb(0x0a, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 spin_unlock_irqrestore(&i8259A_lock, flags);
1609
1610 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1611
1612 v = inb(0x4d1) << 8 | inb(0x4d0);
1613 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1614}
1615
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001616__apicdebuginit(int) print_all_ICs(void)
1617{
1618 print_PIC();
1619 print_all_local_APICs();
1620 print_IO_APIC();
1621
1622 return 0;
1623}
1624
1625fs_initcall(print_all_ICs);
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628static void __init enable_IO_APIC(void)
1629{
1630 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001631 int i8259_apic, i8259_pin;
1632 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 unsigned long flags;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (!pirqs_enabled)
1636 for (i = 0; i < MAX_PIRQS; i++)
1637 pirq_entries[i] = -1;
1638
1639 /*
1640 * The number of IO-APIC IRQ registers (== #pins):
1641 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001642 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001644 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001646 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1647 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001648 for (apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001649 int pin;
1650 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001651 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001652 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001653 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001654
1655
1656 /* If the interrupt line is enabled and in ExtInt mode
1657 * I have found the pin where the i8259 is connected.
1658 */
1659 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1660 ioapic_i8259.apic = apic;
1661 ioapic_i8259.pin = pin;
1662 goto found_i8259;
1663 }
1664 }
1665 }
1666 found_i8259:
1667 /* Look to see what if the MP table has reported the ExtINT */
1668 /* If we could not find the appropriate pin by looking at the ioapic
1669 * the i8259 probably is not connected the ioapic but give the
1670 * mptable a chance anyway.
1671 */
1672 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1673 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1674 /* Trust the MP table if nothing is setup in the hardware */
1675 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1676 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1677 ioapic_i8259.pin = i8259_pin;
1678 ioapic_i8259.apic = i8259_apic;
1679 }
1680 /* Complain if the MP table and the hardware disagree */
1681 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1682 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1683 {
1684 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
1687 /*
1688 * Do not trust the IO-APIC being empty at bootup
1689 */
1690 clear_IO_APIC();
1691}
1692
1693/*
1694 * Not an __init, needed by the reboot code
1695 */
1696void disable_IO_APIC(void)
1697{
1698 /*
1699 * Clear the IO-APIC before rebooting:
1700 */
1701 clear_IO_APIC();
1702
Eric W. Biederman650927e2005-06-25 14:57:44 -07001703 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001704 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001705 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001706 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001707 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001708 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001709 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001710
1711 memset(&entry, 0, sizeof(entry));
1712 entry.mask = 0; /* Enabled */
1713 entry.trigger = 0; /* Edge */
1714 entry.irr = 0;
1715 entry.polarity = 0; /* High */
1716 entry.delivery_status = 0;
1717 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001718 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001719 entry.vector = 0;
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001720 entry.dest.physical.physical_dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001721
1722 /*
1723 * Add it to the IO-APIC irq-routing table:
1724 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001725 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001726 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001727 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730/*
1731 * function to set the IO-APIC physical IDs based on the
1732 * values stored in the MPC table.
1733 *
1734 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1735 */
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737static void __init setup_ioapic_ids_from_mpc(void)
1738{
1739 union IO_APIC_reg_00 reg_00;
1740 physid_mask_t phys_id_present_map;
1741 int apic;
1742 int i;
1743 unsigned char old_id;
1744 unsigned long flags;
1745
Yinghai Lua4dbc342008-07-25 02:14:28 -07001746 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001747 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001750 * Don't check I/O APIC IDs for xAPIC systems. They have
1751 * no meaning without the serial APIC bus.
1752 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001753 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1754 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001755 return;
1756 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 * This is broken; anything with a real cpu count has to
1758 * circumvent this idiocy regardless.
1759 */
1760 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1761
1762 /*
1763 * Set the IOAPIC ID to the value stored in the MPC table.
1764 */
1765 for (apic = 0; apic < nr_ioapics; apic++) {
1766
1767 /* Read the register 0 value */
1768 spin_lock_irqsave(&ioapic_lock, flags);
1769 reg_00.raw = io_apic_read(apic, 0);
1770 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001771
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001772 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001774 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001776 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1778 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001779 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 /*
1783 * Sanity check, is the ID really free? Every APIC in a
1784 * system must have a unique ID or we get lots of nice
1785 * 'stuck on smp_invalidate_needed IPI wait' messages.
1786 */
1787 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001788 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001790 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 for (i = 0; i < get_physical_broadcast(); i++)
1792 if (!physid_isset(i, phys_id_present_map))
1793 break;
1794 if (i >= get_physical_broadcast())
1795 panic("Max APIC ID exceeded!\n");
1796 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1797 i);
1798 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001799 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 } else {
1801 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001802 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 apic_printk(APIC_VERBOSE, "Setting %d in the "
1804 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001805 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1807 }
1808
1809
1810 /*
1811 * We need to adjust the IRQ routing table
1812 * if the ID changed.
1813 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001814 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001816 if (mp_irqs[i].mp_dstapic == old_id)
1817 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001818 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820 /*
1821 * Read the right value from the MPC table and
1822 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 apic_printk(APIC_VERBOSE, KERN_INFO
1825 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001826 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001828 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 spin_lock_irqsave(&ioapic_lock, flags);
1830 io_apic_write(apic, 0, reg_00.raw);
1831 spin_unlock_irqrestore(&ioapic_lock, flags);
1832
1833 /*
1834 * Sanity check
1835 */
1836 spin_lock_irqsave(&ioapic_lock, flags);
1837 reg_00.raw = io_apic_read(apic, 0);
1838 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001839 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 printk("could not set ID!\n");
1841 else
1842 apic_printk(APIC_VERBOSE, " ok.\n");
1843 }
1844}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001846int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001847
1848static int __init notimercheck(char *s)
1849{
1850 no_timer_check = 1;
1851 return 1;
1852}
1853__setup("no_timer_check", notimercheck);
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855/*
1856 * There is a nasty bug in some older SMP boards, their mptable lies
1857 * about the timer IRQ. We do the following to work around the situation:
1858 *
1859 * - timer IRQ defaults to IO-APIC IRQ
1860 * - if this function detects that timer IRQs are defunct, then we fall
1861 * back to ISA timer IRQs
1862 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001863static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864{
1865 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001866 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Zachary Amsden8542b202006-12-07 02:14:09 +01001868 if (no_timer_check)
1869 return 1;
1870
Ingo Molnar4aae0702007-12-18 18:05:58 +01001871 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 local_irq_enable();
1873 /* Let ten ticks pass... */
1874 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001875 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
1877 /*
1878 * Expect a few ticks at least, to be sure some possible
1879 * glue logic does not lock up after one or two first
1880 * ticks in a non-ExtINT mode. Also the local APIC
1881 * might have cached one ExtINT interrupt. Finally, at
1882 * least one tick may be lost due to delays.
1883 */
Julia Lawall1d16b532008-01-30 13:32:19 +01001884 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 return 1;
1886
1887 return 0;
1888}
1889
1890/*
1891 * In the SMP+IOAPIC case it might happen that there are an unspecified
1892 * number of pending IRQ events unhandled. These cases are very rare,
1893 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1894 * better to do it this way as thus we do not have to be aware of
1895 * 'pending' interrupts in the IRQ path, except at this point.
1896 */
1897/*
1898 * Edge triggered needs to resend any interrupt
1899 * that was delayed but this is now handled in the device
1900 * independent code.
1901 */
1902
1903/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001904 * Startup quirk:
1905 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 * Starting up a edge-triggered IO-APIC interrupt is
1907 * nasty - we need to make sure that we get the edge.
1908 * If it is already asserted for some reason, we need
1909 * return 1 to indicate that is was pending.
1910 *
1911 * This is not complete - we should be able to fake
1912 * an edge even if it isn't on the 8259A...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001913 *
1914 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001916static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 int was_pending = 0;
1919 unsigned long flags;
1920
1921 spin_lock_irqsave(&ioapic_lock, flags);
1922 if (irq < 16) {
1923 disable_8259A_irq(irq);
1924 if (i8259A_irq_pending(irq))
1925 was_pending = 1;
1926 }
1927 __unmask_IO_APIC_irq(irq);
1928 spin_unlock_irqrestore(&ioapic_lock, flags);
1929
1930 return was_pending;
1931}
1932
Yinghai Lu497c9a12008-08-19 20:50:28 -07001933static void irq_complete_move(unsigned int irq);
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001934static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Yinghai Lu497c9a12008-08-19 20:50:28 -07001936 irq_complete_move(irq);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001937 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 ack_APIC_irq();
1939}
1940
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001941static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
1943 unsigned long v;
1944 int i;
1945
Yinghai Lu497c9a12008-08-19 20:50:28 -07001946 irq_complete_move(irq);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001947 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948/*
1949 * It appears there is an erratum which affects at least version 0x11
1950 * of I/O APIC (that's the 82093AA and cores integrated into various
1951 * chipsets). Under certain conditions a level-triggered interrupt is
1952 * erroneously delivered as edge-triggered one but the respective IRR
1953 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1954 * message but it will never arrive and further interrupts are blocked
1955 * from the source. The exact reason is so far unknown, but the
1956 * phenomenon was observed when two consecutive interrupt requests
1957 * from a given source get delivered to the same CPU and the source is
1958 * temporarily disabled in between.
1959 *
1960 * A workaround is to simulate an EOI message manually. We achieve it
1961 * by setting the trigger mode to edge and then to level when the edge
1962 * trigger mode gets detected in the TMR of a local APIC for a
1963 * level-triggered interrupt. We mask the source for the time of the
1964 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1965 * The idea is from Manfred Spraul. --macro
1966 */
Yinghai Lua1420f32008-08-19 20:50:24 -07001967 i = irq_cfg(irq)->vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1970
1971 ack_APIC_irq();
1972
1973 if (!(v & (1 << (i & 0x1f)))) {
1974 atomic_inc(&irq_mis_count);
1975 spin_lock(&ioapic_lock);
1976 __mask_and_edge_IO_APIC_irq(irq);
1977 __unmask_and_level_IO_APIC_irq(irq);
1978 spin_unlock(&ioapic_lock);
1979 }
1980}
1981
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001982static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Yinghai Lua1420f32008-08-19 20:50:24 -07001984 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07001985
1986 return 1;
1987}
1988
Yinghai Lu497c9a12008-08-19 20:50:28 -07001989#ifdef CONFIG_SMP
1990asmlinkage void smp_irq_move_cleanup_interrupt(void)
1991{
1992 unsigned vector, me;
1993 ack_APIC_irq();
1994 irq_enter();
1995
1996 me = smp_processor_id();
1997 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1998 unsigned int irq;
1999 struct irq_desc *desc;
2000 struct irq_cfg *cfg;
2001 irq = __get_cpu_var(vector_irq)[vector];
2002
2003 desc = irq_to_desc(irq);
2004 if (!desc)
2005 continue;
2006
2007 cfg = irq_cfg(irq);
2008 spin_lock(&desc->lock);
2009 if (!cfg->move_cleanup_count)
2010 goto unlock;
2011
2012 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2013 goto unlock;
2014
2015 __get_cpu_var(vector_irq)[vector] = -1;
2016 cfg->move_cleanup_count--;
2017unlock:
2018 spin_unlock(&desc->lock);
2019 }
2020
2021 irq_exit();
2022}
2023
2024static void irq_complete_move(unsigned int irq)
2025{
2026 struct irq_cfg *cfg = irq_cfg(irq);
2027 unsigned vector, me;
2028
2029 if (likely(!cfg->move_in_progress))
2030 return;
2031
2032 vector = ~get_irq_regs()->orig_ax;
2033 me = smp_processor_id();
2034 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2035 cpumask_t cleanup_mask;
2036
2037 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2038 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2039 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2040 cfg->move_in_progress = 0;
2041 }
2042}
2043#else
2044static inline void irq_complete_move(unsigned int irq) {}
2045#endif
2046
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002047static struct irq_chip ioapic_chip __read_mostly = {
2048 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002049 .startup = startup_ioapic_irq,
2050 .mask = mask_IO_APIC_irq,
2051 .unmask = unmask_IO_APIC_irq,
2052 .ack = ack_ioapic_irq,
2053 .eoi = ack_ioapic_quirk_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002054#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002055 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07002056#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002057 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058};
2059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061static inline void init_IO_APIC_traps(void)
2062{
2063 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07002064 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07002065 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067 /*
2068 * NOTE! The local APIC isn't very good at handling
2069 * multiple interrupts at the same interrupt level.
2070 * As the interrupt level is determined by taking the
2071 * vector number and shifting that right by 4, we
2072 * want to spread these out a bit so that they don't
2073 * all fall in the same interrupt level.
2074 *
2075 * Also, we've got to be careful not to trash gate
2076 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2077 */
Yinghai Luda51a822008-08-19 20:50:25 -07002078 for_each_irq_cfg(cfg) {
2079 irq = cfg->irq;
2080 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /*
2082 * Hmm.. We don't have an entry for this,
2083 * so default to an old-fashioned 8259
2084 * interrupt if we can..
2085 */
2086 if (irq < 16)
2087 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07002088 else {
2089 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07002091 desc->chip = &no_irq_chip;
2092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094 }
2095}
2096
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002097/*
2098 * The local APIC irq-chip implementation:
2099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002101static void ack_lapic_irq(unsigned int irq)
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002102{
2103 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002106static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107{
2108 unsigned long v;
2109
2110 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002111 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002114static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002116 unsigned long v;
2117
2118 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002119 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002122static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002123 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002124 .mask = mask_lapic_irq,
2125 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002126 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127};
2128
Yinghai Lu497c9a12008-08-19 20:50:28 -07002129static void lapic_register_intr(int irq)
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002130{
Yinghai Lu08678b02008-08-19 20:50:05 -07002131 struct irq_desc *desc;
2132
2133 desc = irq_to_desc(irq);
2134 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002135 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2136 "edge");
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002137}
2138
Jan Beuliche9427102008-01-30 13:31:24 +01002139static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002142 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 * We put the 8259A master into AEOI mode and
2144 * unmask on all local APICs LVT0 as NMI.
2145 *
2146 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2147 * is from Maciej W. Rozycki - so we do not have to EOI from
2148 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2151
Jan Beuliche9427102008-01-30 13:31:24 +01002152 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
2154 apic_printk(APIC_VERBOSE, " done.\n");
2155}
2156
2157/*
2158 * This looks a bit hackish but it's about the only one way of sending
2159 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2160 * not support the ExtINT mode, unfortunately. We need to send these
2161 * cycles as some i82489DX-based boards have glue logic that keeps the
2162 * 8259A interrupt line asserted until INTA. --macro
2163 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002164static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002166 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 struct IO_APIC_route_entry entry0, entry1;
2168 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002170 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002171 if (pin == -1) {
2172 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002174 }
2175 apic = find_isa_irq_apic(8, mp_INT);
2176 if (apic == -1) {
2177 WARN_ON_ONCE(1);
2178 return;
2179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Andi Kleencf4c6a22006-09-26 10:52:30 +02002181 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002182 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 memset(&entry1, 0, sizeof(entry1));
2185
2186 entry1.dest_mode = 0; /* physical delivery */
2187 entry1.mask = 0; /* unmask IRQ now */
2188 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2189 entry1.delivery_mode = dest_ExtINT;
2190 entry1.polarity = entry0.polarity;
2191 entry1.trigger = 0;
2192 entry1.vector = 0;
2193
Andi Kleencf4c6a22006-09-26 10:52:30 +02002194 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 save_control = CMOS_READ(RTC_CONTROL);
2197 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2198 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2199 RTC_FREQ_SELECT);
2200 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2201
2202 i = 100;
2203 while (i-- > 0) {
2204 mdelay(10);
2205 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2206 i -= 10;
2207 }
2208
2209 CMOS_WRITE(save_control, RTC_CONTROL);
2210 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002211 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
Andi Kleencf4c6a22006-09-26 10:52:30 +02002213 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
2216/*
2217 * This code may look a bit paranoid, but it's supposed to cooperate with
2218 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2219 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2220 * fanatically on his truly buggy board.
2221 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002222static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002224 struct irq_cfg *cfg = irq_cfg(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002225 int apic1, pin1, apic2, pin2;
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002226 int no_pin1 = 0;
Ingo Molnar6e908942008-03-21 14:32:36 +01002227 unsigned int ver;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002228 unsigned long flags;
2229
2230 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002231
Ingo Molnar6e908942008-03-21 14:32:36 +01002232 ver = apic_read(APIC_LVR);
2233 ver = GET_APIC_VERSION(ver);
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /*
2236 * get/set the timer IRQ vector:
2237 */
2238 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002239 assign_irq_vector(0, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002242 * As IRQ0 is to be enabled in the 8259A, the virtual
2243 * wire has to be disabled in the local APIC. Also
2244 * timer interrupts need to be acknowledged manually in
2245 * the 8259A for the i82489DX when using the NMI
2246 * watchdog as that APIC treats NMIs as level-triggered.
2247 * The AEOI mode will finish them in the 8259A
2248 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002250 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 init_8259A(1);
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002252 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002254 pin1 = find_isa_irq_pin(0, mp_INT);
2255 apic1 = find_isa_irq_apic(0, mp_INT);
2256 pin2 = ioapic_i8259.pin;
2257 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002259 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2260 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
Yinghai Lu497c9a12008-08-19 20:50:28 -07002261 cfg->vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002263 /*
2264 * Some BIOS writers are clueless and report the ExtINTA
2265 * I/O APIC input from the cascaded 8259A as the timer
2266 * interrupt input. So just in case, if only one pin
2267 * was found above, try it both directly and through the
2268 * 8259A.
2269 */
2270 if (pin1 == -1) {
2271 pin1 = pin2;
2272 apic1 = apic2;
2273 no_pin1 = 1;
2274 } else if (pin2 == -1) {
2275 pin2 = pin1;
2276 apic2 = apic1;
2277 }
2278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (pin1 != -1) {
2280 /*
2281 * Ok, does IRQ0 through the IOAPIC work?
2282 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002283 if (no_pin1) {
2284 add_pin_to_irq(0, apic1, pin1);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002285 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 unmask_IO_APIC_irq(0);
2288 if (timer_irq_works()) {
2289 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 setup_nmi();
2291 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002293 if (disable_timer_pin_1 > 0)
2294 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002297 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002298 if (!no_pin1)
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002299 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2300 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002302 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2303 "(IRQ0) through the 8259A ...\n");
2304 apic_printk(APIC_QUIET, KERN_INFO
2305 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 /*
2307 * legacy devices should be connected to IO APIC #0
2308 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002309 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002310 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002311 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002312 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002314 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002315 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002317 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002319 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002321 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 }
2323 /*
2324 * Cleanup, just in case ...
2325 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002326 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002327 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002328 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
2331 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002332 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2333 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002334 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002336 timer_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002338 apic_printk(APIC_QUIET, KERN_INFO
2339 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Yinghai Lu497c9a12008-08-19 20:50:28 -07002341 lapic_register_intr(0);
2342 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 enable_8259A_irq(0);
2344
2345 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002346 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002347 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002349 disable_8259A_irq(0);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002350 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002351 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002353 apic_printk(APIC_QUIET, KERN_INFO
2354 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 init_8259A(0);
2357 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002358 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 unlock_ExtINT_logic();
2361
2362 if (timer_irq_works()) {
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002363 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002364 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 }
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002366 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a02008-07-14 19:08:13 +01002368 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002369out:
2370 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371}
2372
2373/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002374 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2375 * to devices. However there may be an I/O APIC pin available for
2376 * this interrupt regardless. The pin may be left unconnected, but
2377 * typically it will be reused as an ExtINT cascade interrupt for
2378 * the master 8259A. In the MPS case such a pin will normally be
2379 * reported as an ExtINT interrupt in the MP table. With ACPI
2380 * there is no provision for ExtINT interrupts, and in the absence
2381 * of an override it would be treated as an ordinary ISA I/O APIC
2382 * interrupt, that is edge-triggered and unmasked by default. We
2383 * used to do this, but it caused problems on some systems because
2384 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2385 * the same ExtINT cascade interrupt to drive the local APIC of the
2386 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2387 * the I/O APIC in all cases now. No actual device should request
2388 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 */
2390#define PIC_IRQS (1 << PIC_CASCADE_IR)
2391
2392void __init setup_IO_APIC(void)
2393{
2394 enable_IO_APIC();
2395
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002396 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
2398 printk("ENABLING IO-APIC IRQs\n");
2399
2400 /*
2401 * Set up IO-APIC IRQ routing.
2402 */
2403 if (!acpi_ioapic)
2404 setup_ioapic_ids_from_mpc();
2405 sync_Arb_IDs();
2406 setup_IO_APIC_irqs();
2407 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002408 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
2411/*
2412 * Called after all the initialization is done. If we didnt find any
2413 * APIC bugs then we can allow the modify fast path
2414 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416static int __init io_apic_bug_finalize(void)
2417{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002418 if (sis_apic_bug == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 sis_apic_bug = 0;
2420 return 0;
2421}
2422
2423late_initcall(io_apic_bug_finalize);
2424
2425struct sysfs_ioapic_data {
2426 struct sys_device dev;
2427 struct IO_APIC_route_entry entry[0];
2428};
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002429static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Pavel Machek438510f2005-04-16 15:25:24 -07002431static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 struct IO_APIC_route_entry *entry;
2434 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 data = container_of(dev, struct sysfs_ioapic_data, dev);
2438 entry = data->entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002439 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002440 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 return 0;
2443}
2444
2445static int ioapic_resume(struct sys_device *dev)
2446{
2447 struct IO_APIC_route_entry *entry;
2448 struct sysfs_ioapic_data *data;
2449 unsigned long flags;
2450 union IO_APIC_reg_00 reg_00;
2451 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 data = container_of(dev, struct sysfs_ioapic_data, dev);
2454 entry = data->entry;
2455
2456 spin_lock_irqsave(&ioapic_lock, flags);
2457 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002458 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2459 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 io_apic_write(dev->id, 0, reg_00.raw);
2461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002463 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002464 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 return 0;
2467}
2468
2469static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002470 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 .suspend = ioapic_suspend,
2472 .resume = ioapic_resume,
2473};
2474
2475static int __init ioapic_init_sysfs(void)
2476{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002477 struct sys_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 int i, size, error = 0;
2479
2480 error = sysdev_class_register(&ioapic_sysdev_class);
2481 if (error)
2482 return error;
2483
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002484 for (i = 0; i < nr_ioapics; i++) {
2485 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002487 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (!mp_ioapic_data[i]) {
2489 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2490 continue;
2491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002493 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 dev->cls = &ioapic_sysdev_class;
2495 error = sysdev_register(dev);
2496 if (error) {
2497 kfree(mp_ioapic_data[i]);
2498 mp_ioapic_data[i] = NULL;
2499 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2500 continue;
2501 }
2502 }
2503
2504 return 0;
2505}
2506
2507device_initcall(ioapic_init_sysfs);
2508
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002509/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002510 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002511 */
Yinghai Lu199751d2008-08-19 20:50:27 -07002512unsigned int create_irq_nr(unsigned int irq_want)
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002513{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002514 /* Allocate an unused irq */
Yinghai Lu497c9a12008-08-19 20:50:28 -07002515 unsigned int irq, new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002516 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07002517 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002518
Yinghai Lu497c9a12008-08-19 20:50:28 -07002519#ifndef CONFIG_HAVE_SPARSE_IRQ
Yinghai Lu199751d2008-08-19 20:50:27 -07002520 /* only can use bus/dev/fn.. when per_cpu vector is used */
2521 irq_want = nr_irqs - 1;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002522#endif
Yinghai Lu199751d2008-08-19 20:50:27 -07002523
2524 irq = 0;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002525 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu199751d2008-08-19 20:50:27 -07002526 for (new = (nr_irqs - 1); new > 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002527 if (platform_legacy_irq(new))
2528 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07002529 cfg_new = irq_cfg(new);
2530 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002531 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07002532 if (!cfg_new)
2533 cfg_new = irq_cfg_alloc(new);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002534 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002535 irq = new;
2536 break;
2537 }
2538 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002539
Yinghai Lu199751d2008-08-19 20:50:27 -07002540 if (irq > 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002541 dynamic_irq_init(irq);
2542 }
2543 return irq;
2544}
2545
Yinghai Lu199751d2008-08-19 20:50:27 -07002546int create_irq(void)
2547{
2548 return create_irq_nr(nr_irqs - 1);
2549}
2550
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002551void destroy_irq(unsigned int irq)
2552{
2553 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002554
2555 dynamic_irq_cleanup(irq);
2556
2557 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu497c9a12008-08-19 20:50:28 -07002558 __clear_irq_vector(irq);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002559 spin_unlock_irqrestore(&vector_lock, flags);
2560}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002561
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002562/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002563 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002564 */
2565#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002566static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002567{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002568 struct irq_cfg *cfg;
2569 int err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002570 unsigned dest;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002571 cpumask_t tmp;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002572
Yinghai Lu497c9a12008-08-19 20:50:28 -07002573 tmp = TARGET_CPUS;
2574 err = assign_irq_vector(irq, tmp);
2575 if (err)
2576 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002577
Yinghai Lu497c9a12008-08-19 20:50:28 -07002578 cfg = irq_cfg(irq);
2579 cpus_and(tmp, cfg->domain, tmp);
2580 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002581
Yinghai Lu497c9a12008-08-19 20:50:28 -07002582 msg->address_hi = MSI_ADDR_BASE_HI;
2583 msg->address_lo =
2584 MSI_ADDR_BASE_LO |
2585 ((INT_DEST_MODE == 0) ?
2586 MSI_ADDR_DEST_MODE_PHYSICAL:
2587 MSI_ADDR_DEST_MODE_LOGICAL) |
2588 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2589 MSI_ADDR_REDIRECTION_CPU:
2590 MSI_ADDR_REDIRECTION_LOWPRI) |
2591 MSI_ADDR_DEST_ID(dest);
2592
2593 msg->data =
2594 MSI_DATA_TRIGGER_EDGE |
2595 MSI_DATA_LEVEL_ASSERT |
2596 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2597 MSI_DATA_DELIVERY_FIXED:
2598 MSI_DATA_DELIVERY_LOWPRI) |
2599 MSI_DATA_VECTOR(cfg->vector);
2600
2601 return err;
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002602}
2603
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002604#ifdef CONFIG_SMP
2605static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2606{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002607 struct irq_cfg *cfg;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002608 struct msi_msg msg;
2609 unsigned int dest;
2610 cpumask_t tmp;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002611
2612 cpus_and(tmp, mask, cpu_online_map);
2613 if (cpus_empty(tmp))
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002614 return;
2615
Yinghai Lu497c9a12008-08-19 20:50:28 -07002616 if (assign_irq_vector(irq, mask))
2617 return;
2618
2619 cfg = irq_cfg(irq);
2620 cpus_and(tmp, cfg->domain, mask);
2621 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002622
2623 read_msi_msg(irq, &msg);
2624
2625 msg.data &= ~MSI_DATA_VECTOR_MASK;
Yinghai Lu497c9a12008-08-19 20:50:28 -07002626 msg.data |= MSI_DATA_VECTOR(cfg->vector);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002627 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2628 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2629
2630 write_msi_msg(irq, &msg);
Yinghai Lu199751d2008-08-19 20:50:27 -07002631 irq_to_desc(irq)->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002632}
2633#endif /* CONFIG_SMP */
2634
2635/*
2636 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2637 * which implement the MSI or MSI-X Capability Structure.
2638 */
2639static struct irq_chip msi_chip = {
2640 .name = "PCI-MSI",
2641 .unmask = unmask_msi_irq,
2642 .mask = mask_msi_irq,
2643 .ack = ack_ioapic_irq,
2644#ifdef CONFIG_SMP
2645 .set_affinity = set_msi_irq_affinity,
2646#endif
2647 .retrigger = ioapic_retrigger_irq,
2648};
2649
Yinghai Lu199751d2008-08-19 20:50:27 -07002650static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
2651{
2652 unsigned int irq;
2653
2654 irq = dev->bus->number;
2655 irq <<= 8;
2656 irq |= dev->devfn;
2657 irq <<= 12;
2658
2659 return irq;
2660}
2661
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002662int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002663{
2664 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002665 int irq, ret;
Yinghai Lu199751d2008-08-19 20:50:27 -07002666
2667 unsigned int irq_want;
2668
2669 irq_want = build_irq_for_pci_dev(dev) + 0x100;
2670
2671 irq = create_irq_nr(irq_want);
2672
2673 if (irq == 0)
2674 return -1;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002675
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002676 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002677 if (ret < 0) {
2678 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002679 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002680 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002681
Michael Ellerman7fe37302007-04-18 19:39:21 +10002682 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002683 write_msi_msg(irq, &msg);
2684
Ingo Molnara460e742006-10-17 00:10:03 -07002685 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2686 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002687
Michael Ellerman7fe37302007-04-18 19:39:21 +10002688 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002689}
2690
2691void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002692{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002693 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002694}
2695
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002696#endif /* CONFIG_PCI_MSI */
2697
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002698/*
2699 * Hypertransport interrupt support
2700 */
2701#ifdef CONFIG_HT_IRQ
2702
2703#ifdef CONFIG_SMP
2704
Yinghai Lu497c9a12008-08-19 20:50:28 -07002705static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002706{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002707 struct ht_irq_msg msg;
2708 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002709
Yinghai Lu497c9a12008-08-19 20:50:28 -07002710 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
Eric W. Biedermanec683072006-11-08 17:44:57 -08002711 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002712
Yinghai Lu497c9a12008-08-19 20:50:28 -07002713 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
Eric W. Biedermanec683072006-11-08 17:44:57 -08002714 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002715
Eric W. Biedermanec683072006-11-08 17:44:57 -08002716 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002717}
2718
2719static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2720{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002721 struct irq_cfg *cfg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002722 unsigned int dest;
2723 cpumask_t tmp;
2724
2725 cpus_and(tmp, mask, cpu_online_map);
2726 if (cpus_empty(tmp))
Yinghai Lu497c9a12008-08-19 20:50:28 -07002727 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002728
Yinghai Lu497c9a12008-08-19 20:50:28 -07002729 if (assign_irq_vector(irq, mask))
2730 return;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002731
Yinghai Lu497c9a12008-08-19 20:50:28 -07002732 cfg = irq_cfg(irq);
2733 cpus_and(tmp, cfg->domain, mask);
2734 dest = cpu_mask_to_apicid(tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002735
Yinghai Lu497c9a12008-08-19 20:50:28 -07002736 target_ht_irq(irq, dest, cfg->vector);
Yinghai Lu199751d2008-08-19 20:50:27 -07002737 irq_to_desc(irq)->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002738}
2739#endif
2740
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002741static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002742 .name = "PCI-HT",
2743 .mask = mask_ht_irq,
2744 .unmask = unmask_ht_irq,
2745 .ack = ack_ioapic_irq,
2746#ifdef CONFIG_SMP
2747 .set_affinity = set_ht_irq_affinity,
2748#endif
2749 .retrigger = ioapic_retrigger_irq,
2750};
2751
2752int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2753{
Yinghai Lu497c9a12008-08-19 20:50:28 -07002754 struct irq_cfg *cfg;
2755 int err;
2756 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002757
Yinghai Lu497c9a12008-08-19 20:50:28 -07002758 tmp = TARGET_CPUS;
2759 err = assign_irq_vector(irq, tmp);
2760 if ( !err) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002761 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002762 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002763
Yinghai Lu497c9a12008-08-19 20:50:28 -07002764 cfg = irq_cfg(irq);
2765 cpus_and(tmp, cfg->domain, tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002766 dest = cpu_mask_to_apicid(tmp);
2767
Eric W. Biedermanec683072006-11-08 17:44:57 -08002768 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002769
Eric W. Biedermanec683072006-11-08 17:44:57 -08002770 msg.address_lo =
2771 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002772 HT_IRQ_LOW_DEST_ID(dest) |
Yinghai Lu497c9a12008-08-19 20:50:28 -07002773 HT_IRQ_LOW_VECTOR(cfg->vector) |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002774 ((INT_DEST_MODE == 0) ?
2775 HT_IRQ_LOW_DM_PHYSICAL :
2776 HT_IRQ_LOW_DM_LOGICAL) |
2777 HT_IRQ_LOW_RQEOI_EDGE |
2778 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2779 HT_IRQ_LOW_MT_FIXED :
2780 HT_IRQ_LOW_MT_ARBITRATED) |
2781 HT_IRQ_LOW_IRQ_MASKED;
2782
Eric W. Biedermanec683072006-11-08 17:44:57 -08002783 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002784
Ingo Molnara460e742006-10-17 00:10:03 -07002785 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2786 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002787 }
Yinghai Lu497c9a12008-08-19 20:50:28 -07002788 return err;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002789}
2790#endif /* CONFIG_HT_IRQ */
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792/* --------------------------------------------------------------------------
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002793 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 -------------------------------------------------------------------------- */
2795
Len Brown888ba6c2005-08-24 12:07:20 -04002796#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002798int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 union IO_APIC_reg_00 reg_00;
2801 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2802 physid_mask_t tmp;
2803 unsigned long flags;
2804 int i = 0;
2805
2806 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002807 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2808 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2812 * advantage of new APIC bus architecture.
2813 */
2814
2815 if (physids_empty(apic_id_map))
2816 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2817
2818 spin_lock_irqsave(&ioapic_lock, flags);
2819 reg_00.raw = io_apic_read(ioapic, 0);
2820 spin_unlock_irqrestore(&ioapic_lock, flags);
2821
2822 if (apic_id >= get_physical_broadcast()) {
2823 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2824 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2825 apic_id = reg_00.bits.ID;
2826 }
2827
2828 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002829 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 * 'stuck on smp_invalidate_needed IPI wait' messages.
2831 */
2832 if (check_apicid_used(apic_id_map, apic_id)) {
2833
2834 for (i = 0; i < get_physical_broadcast(); i++) {
2835 if (!check_apicid_used(apic_id_map, i))
2836 break;
2837 }
2838
2839 if (i == get_physical_broadcast())
2840 panic("Max apic_id exceeded!\n");
2841
2842 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2843 "trying %d\n", ioapic, apic_id, i);
2844
2845 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
2848 tmp = apicid_to_cpu_present(apic_id);
2849 physids_or(apic_id_map, apic_id_map, tmp);
2850
2851 if (reg_00.bits.ID != apic_id) {
2852 reg_00.bits.ID = apic_id;
2853
2854 spin_lock_irqsave(&ioapic_lock, flags);
2855 io_apic_write(ioapic, 0, reg_00.raw);
2856 reg_00.raw = io_apic_read(ioapic, 0);
2857 spin_unlock_irqrestore(&ioapic_lock, flags);
2858
2859 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002860 if (reg_00.bits.ID != apic_id) {
2861 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2862 return -1;
2863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865
2866 apic_printk(APIC_VERBOSE, KERN_INFO
2867 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2868
2869 return apic_id;
2870}
2871
2872
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002873int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
2875 union IO_APIC_reg_01 reg_01;
2876 unsigned long flags;
2877
2878 spin_lock_irqsave(&ioapic_lock, flags);
2879 reg_01.raw = io_apic_read(ioapic, 1);
2880 spin_unlock_irqrestore(&ioapic_lock, flags);
2881
2882 return reg_01.bits.version;
2883}
2884
2885
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002886int __init io_apic_get_redir_entries(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887{
2888 union IO_APIC_reg_01 reg_01;
2889 unsigned long flags;
2890
2891 spin_lock_irqsave(&ioapic_lock, flags);
2892 reg_01.raw = io_apic_read(ioapic, 1);
2893 spin_unlock_irqrestore(&ioapic_lock, flags);
2894
2895 return reg_01.bits.entries;
2896}
2897
2898
Yinghai Lu497c9a12008-08-19 20:50:28 -07002899int io_apic_set_pci_routing(int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (!IO_APIC_IRQ(irq)) {
2902 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2903 ioapic);
2904 return -EINVAL;
2905 }
2906
2907 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 * IRQs < 16 are already in the irq_2_pin[] map
2909 */
2910 if (irq >= 16)
2911 add_pin_to_irq(irq, ioapic, pin);
2912
Yinghai Lu497c9a12008-08-19 20:50:28 -07002913 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915 return 0;
2916}
2917
Shaohua Li61fd47e2007-11-17 01:05:28 -05002918int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2919{
2920 int i;
2921
2922 if (skip_ioapic_setup)
2923 return -1;
2924
2925 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002926 if (mp_irqs[i].mp_irqtype == mp_INT &&
2927 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002928 break;
2929 if (i >= mp_irq_entries)
2930 return -1;
2931
2932 *trigger = irq_trigger(i);
2933 *polarity = irq_polarity(i);
2934 return 0;
2935}
2936
Len Brown888ba6c2005-08-24 12:07:20 -04002937#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002938
Yinghai Lu497c9a12008-08-19 20:50:28 -07002939/*
2940 * This function currently is only a helper for the i386 smp boot process where
2941 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2942 * so mask in all cases should simply be TARGET_CPUS
2943 */
2944#ifdef CONFIG_SMP
2945void __init setup_ioapic_dest(void)
2946{
2947 int pin, ioapic, irq, irq_entry;
2948 struct irq_cfg *cfg;
2949 struct irq_desc *desc;
2950
2951 if (skip_ioapic_setup == 1)
2952 return;
2953
2954 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2955 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2956 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2957 if (irq_entry == -1)
2958 continue;
2959 irq = pin_2_irq(irq_entry, ioapic, pin);
2960
2961 /* setup_IO_APIC_irqs could fail to get vector for some device
2962 * when you have too many devices, because at that time only boot
2963 * cpu is online.
2964 */
2965 cfg = irq_cfg(irq);
2966 if (!cfg->vector)
2967 setup_IO_APIC_irq(ioapic, pin, irq,
2968 irq_trigger(irq_entry),
2969 irq_polarity(irq_entry));
2970 else {
2971 desc = irq_to_desc(irq);
2972 set_ioapic_affinity_irq(irq, TARGET_CPUS);
2973 }
2974 }
2975
2976 }
2977}
2978#endif
2979
Rusty Russell1a3f2392006-09-26 10:52:32 +02002980static int __init parse_disable_timer_pin_1(char *arg)
2981{
2982 disable_timer_pin_1 = 1;
2983 return 0;
2984}
2985early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2986
2987static int __init parse_enable_timer_pin_1(char *arg)
2988{
2989 disable_timer_pin_1 = -1;
2990 return 0;
2991}
2992early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2993
2994static int __init parse_noapic(char *arg)
2995{
2996 /* disable IO-APIC */
2997 disable_ioapic_setup();
2998 return 0;
2999}
3000early_param("noapic", parse_noapic);
Yinghai Luf3294a32008-06-27 01:41:56 -07003001
3002void __init ioapic_init_mappings(void)
3003{
3004 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3005 int i;
3006
3007 for (i = 0; i < nr_ioapics; i++) {
3008 if (smp_found_config) {
3009 ioapic_phys = mp_ioapics[i].mp_apicaddr;
3010 if (!ioapic_phys) {
3011 printk(KERN_ERR
3012 "WARNING: bogus zero IO-APIC "
3013 "address found in MPTABLE, "
3014 "disabling IO/APIC support!\n");
3015 smp_found_config = 0;
3016 skip_ioapic_setup = 1;
3017 goto fake_ioapic_page;
3018 }
3019 } else {
3020fake_ioapic_page:
3021 ioapic_phys = (unsigned long)
3022 alloc_bootmem_pages(PAGE_SIZE);
3023 ioapic_phys = __pa(ioapic_phys);
3024 }
3025 set_fixmap_nocache(idx, ioapic_phys);
3026 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
3027 __fix_to_virt(idx), ioapic_phys);
3028 idx++;
3029 }
3030}
3031