blob: 5a83d7f5b14717cd7b54276f1589cd47010e70aa [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
51#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020052#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +010054#define __apicdebuginit(type) static type __init
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056int (*ioapic_renumber_irq)(int ioapic, int irq);
57atomic_t irq_mis_count;
58
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -080059/* Where if anywhere is the i8259 connect in external int mode */
60static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static DEFINE_SPINLOCK(ioapic_lock);
Eric W. Biedermand388e5f2008-08-09 15:09:02 -070063DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Maciej W. Rozycki35542c52008-05-21 22:10:22 +010065int timer_through_8259 __initdata;
Andi Kleenf9262c12006-03-08 17:57:25 -080066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
68 * Is the SiS APIC rmw bug present ?
69 * -1 = don't know, 0 = no, 1 = yes
70 */
71int sis_apic_bug = -1;
72
Yinghai Lu301e6192008-08-19 20:50:02 -070073int first_free_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
75 * # of IRQ routing registers
76 */
77int nr_ioapic_registers[MAX_IO_APICS];
78
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040079/* I/O APIC entries */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +040080struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040081int nr_ioapics;
82
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040083/* MP IRQ source entries */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +040084struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040085
86/* # of MP IRQ source entries */
87int mp_irq_entries;
88
Alexey Starikovskiy8732fc42008-05-19 19:47:16 +040089#if defined (CONFIG_MCA) || defined (CONFIG_EISA)
90int mp_bus_id_to_type[MAX_MP_BUSSES];
91#endif
92
93DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
94
Rusty Russell1a3f2392006-09-26 10:52:32 +020095static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020096
Yinghai Luda51a822008-08-19 20:50:25 -070097struct irq_cfg;
98
Yinghai Lua1420f32008-08-19 20:50:24 -070099struct irq_cfg {
Yinghai Luda51a822008-08-19 20:50:25 -0700100 unsigned int irq;
101 struct irq_cfg *next;
Yinghai Lua1420f32008-08-19 20:50:24 -0700102 u8 vector;
103};
104
105
106/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
107static struct irq_cfg irq_cfg_legacy[] __initdata = {
Yinghai Luda51a822008-08-19 20:50:25 -0700108 [0] = { .irq = 0, .vector = IRQ0_VECTOR, },
109 [1] = { .irq = 1, .vector = IRQ1_VECTOR, },
110 [2] = { .irq = 2, .vector = IRQ2_VECTOR, },
111 [3] = { .irq = 3, .vector = IRQ3_VECTOR, },
112 [4] = { .irq = 4, .vector = IRQ4_VECTOR, },
113 [5] = { .irq = 5, .vector = IRQ5_VECTOR, },
114 [6] = { .irq = 6, .vector = IRQ6_VECTOR, },
115 [7] = { .irq = 7, .vector = IRQ7_VECTOR, },
116 [8] = { .irq = 8, .vector = IRQ8_VECTOR, },
117 [9] = { .irq = 9, .vector = IRQ9_VECTOR, },
118 [10] = { .irq = 10, .vector = IRQ10_VECTOR, },
119 [11] = { .irq = 11, .vector = IRQ11_VECTOR, },
120 [12] = { .irq = 12, .vector = IRQ12_VECTOR, },
121 [13] = { .irq = 13, .vector = IRQ13_VECTOR, },
122 [14] = { .irq = 14, .vector = IRQ14_VECTOR, },
123 [15] = { .irq = 15, .vector = IRQ15_VECTOR, },
Yinghai Lua1420f32008-08-19 20:50:24 -0700124};
125
Yinghai Luda51a822008-08-19 20:50:25 -0700126static struct irq_cfg irq_cfg_init = { .irq = -1U, };
127/* need to be biger than size of irq_cfg_legacy */
128static int nr_irq_cfg = 32;
129
130static int __init parse_nr_irq_cfg(char *arg)
Yinghai Lua1420f32008-08-19 20:50:24 -0700131{
Yinghai Luda51a822008-08-19 20:50:25 -0700132 if (arg) {
133 nr_irq_cfg = simple_strtoul(arg, NULL, 0);
134 if (nr_irq_cfg < 32)
135 nr_irq_cfg = 32;
136 }
137 return 0;
138}
Yinghai Lua1420f32008-08-19 20:50:24 -0700139
Yinghai Luda51a822008-08-19 20:50:25 -0700140early_param("nr_irq_cfg", parse_nr_irq_cfg);
Yinghai Lua1420f32008-08-19 20:50:24 -0700141
Yinghai Luda51a822008-08-19 20:50:25 -0700142static void init_one_irq_cfg(struct irq_cfg *cfg)
143{
144 memcpy(cfg, &irq_cfg_init, sizeof(struct irq_cfg));
Yinghai Lua1420f32008-08-19 20:50:24 -0700145}
146
147static struct irq_cfg *irq_cfgx;
Yinghai Luda51a822008-08-19 20:50:25 -0700148static struct irq_cfg *irq_cfgx_free;
149static void __init init_work(void *data)
150{
151 struct dyn_array *da = data;
152 struct irq_cfg *cfg;
153 int legacy_count;
154 int i;
155
156 cfg = *da->name;
157
158 memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
159
160 legacy_count = sizeof(irq_cfg_legacy)/sizeof(irq_cfg_legacy[0]);
161 for (i = legacy_count; i < *da->nr; i++)
162 init_one_irq_cfg(&cfg[i]);
163
164 for (i = 1; i < *da->nr; i++)
165 cfg[i-1].next = &cfg[i];
166
167 irq_cfgx_free = &irq_cfgx[legacy_count];
168 irq_cfgx[legacy_count - 1].next = NULL;
169}
170
171#define for_each_irq_cfg(cfg) \
172 for (cfg = irq_cfgx; cfg; cfg = cfg->next)
173
174DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irq_cfg, PAGE_SIZE, init_work);
Yinghai Lua1420f32008-08-19 20:50:24 -0700175
176static struct irq_cfg *irq_cfg(unsigned int irq)
177{
Yinghai Luda51a822008-08-19 20:50:25 -0700178 struct irq_cfg *cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700179
Yinghai Luda51a822008-08-19 20:50:25 -0700180 cfg = irq_cfgx;
181 while (cfg) {
182 if (cfg->irq == irq)
183 return cfg;
184
185 cfg = cfg->next;
186 }
187
188 return NULL;
189}
190
191static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
192{
193 struct irq_cfg *cfg, *cfg_pri;
194 int i;
195 int count = 0;
196
197 cfg_pri = cfg = irq_cfgx;
198 while (cfg) {
199 if (cfg->irq == irq)
200 return cfg;
201
202 cfg_pri = cfg;
203 cfg = cfg->next;
204 count++;
205 }
206
207 if (!irq_cfgx_free) {
208 unsigned long phys;
209 unsigned long total_bytes;
210 /*
211 * we run out of pre-allocate ones, allocate more
212 */
213 printk(KERN_DEBUG "try to get more irq_cfg %d\n", nr_irq_cfg);
214
215 total_bytes = sizeof(struct irq_cfg) * nr_irq_cfg;
216 if (after_bootmem)
217 cfg = kzalloc(total_bytes, GFP_ATOMIC);
218 else
219 cfg = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
220
221 if (!cfg)
222 panic("please boot with nr_irq_cfg= %d\n", count * 2);
223
224 phys = __pa(cfg);
225 printk(KERN_DEBUG "irq_irq ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
226
227 for (i = 0; i < nr_irq_cfg; i++)
228 init_one_irq_cfg(&cfg[i]);
229
230 for (i = 1; i < nr_irq_cfg; i++)
231 cfg[i-1].next = &cfg[i];
232
233 irq_cfgx_free = cfg;
234 }
235
236 cfg = irq_cfgx_free;
237 irq_cfgx_free = irq_cfgx_free->next;
238 cfg->next = NULL;
239 if (cfg_pri)
240 cfg_pri->next = cfg;
241 else
242 irq_cfgx = cfg;
243 cfg->irq = irq;
244 printk(KERN_DEBUG "found new irq_cfg for irq %d\n", cfg->irq);
245
246#ifdef CONFIG_HAVE_SPARSE_IRQ_DEBUG
247 {
248 /* dump the results */
249 struct irq_cfg *cfg;
250 unsigned long phys;
251 unsigned long bytes = sizeof(struct irq_cfg);
252
253 printk(KERN_DEBUG "=========================== %d\n", irq);
254 printk(KERN_DEBUG "irq_cfg dump after get that for %d\n", irq);
255 for_each_irq_cfg(cfg) {
256 phys = __pa(cfg);
257 printk(KERN_DEBUG "irq_cfg %d ==> [%#lx - %#lx]\n", cfg->irq, phys, phys + bytes);
258 }
259 printk(KERN_DEBUG "===========================\n");
260 }
261#endif
262 return cfg;
Yinghai Lua1420f32008-08-19 20:50:24 -0700263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/*
266 * Rough estimation of how many shared IRQs there are, can
267 * be changed anytime.
268 */
Yinghai Lu301e6192008-08-19 20:50:02 -0700269int pin_map_size;
Yinghai Lu0799e432008-08-19 20:49:48 -0700270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/*
272 * This is performance-critical, we want to do it O(1)
273 *
274 * the indexing order of this array favors 1:1 mappings
275 * between pins and IRQs.
276 */
277
278static struct irq_pin_list {
279 int apic, pin, next;
Yinghai Lu301e6192008-08-19 20:50:02 -0700280} *irq_2_pin;
281
282DEFINE_DYN_ARRAY(irq_2_pin, sizeof(struct irq_pin_list), pin_map_size, 16, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds130fe052006-11-01 09:11:00 -0800284struct io_apic {
285 unsigned int index;
286 unsigned int unused[3];
287 unsigned int data;
288};
289
290static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
291{
292 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +0400293 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
Linus Torvalds130fe052006-11-01 09:11:00 -0800294}
295
296static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
297{
298 struct io_apic __iomem *io_apic = io_apic_base(apic);
299 writel(reg, &io_apic->index);
300 return readl(&io_apic->data);
301}
302
303static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
304{
305 struct io_apic __iomem *io_apic = io_apic_base(apic);
306 writel(reg, &io_apic->index);
307 writel(value, &io_apic->data);
308}
309
310/*
311 * Re-write a value: to be used for read-modify-write
312 * cycles where the read already set up the index register.
313 *
314 * Older SiS APIC requires we rewrite the index register
315 */
316static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
317{
Al Virocb468982007-02-09 16:39:25 +0000318 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800319 if (sis_apic_bug)
320 writel(reg, &io_apic->index);
321 writel(value, &io_apic->data);
322}
323
Andi Kleencf4c6a22006-09-26 10:52:30 +0200324union entry_union {
325 struct { u32 w1, w2; };
326 struct IO_APIC_route_entry entry;
327};
328
329static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
330{
331 union entry_union eu;
332 unsigned long flags;
333 spin_lock_irqsave(&ioapic_lock, flags);
334 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
335 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
336 spin_unlock_irqrestore(&ioapic_lock, flags);
337 return eu.entry;
338}
339
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800340/*
341 * When we write a new IO APIC routing entry, we need to write the high
342 * word first! If the mask bit in the low word is clear, we will enable
343 * the interrupt, and we need to make sure the entry is fully populated
344 * before that happens.
345 */
Andi Kleend15512f2006-12-07 02:14:07 +0100346static void
347__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
348{
349 union entry_union eu;
350 eu.entry = e;
351 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
352 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
353}
354
Andi Kleencf4c6a22006-09-26 10:52:30 +0200355static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
356{
357 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200358 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100359 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800360 spin_unlock_irqrestore(&ioapic_lock, flags);
361}
362
363/*
364 * When we mask an IO APIC routing entry, we need to write the low
365 * word first, in order to set the mask bit before we change the
366 * high bits!
367 */
368static void ioapic_mask_entry(int apic, int pin)
369{
370 unsigned long flags;
371 union entry_union eu = { .entry.mask = 1 };
372
373 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200374 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
375 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
376 spin_unlock_irqrestore(&ioapic_lock, flags);
377}
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/*
380 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
381 * shared ISA-space IRQs, so we have to support them. We are super
382 * fast in the common case, and fast for shared ISA-space IRQs.
383 */
384static void add_pin_to_irq(unsigned int irq, int apic, int pin)
385{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct irq_pin_list *entry = irq_2_pin + irq;
387
Yinghai Luda51a822008-08-19 20:50:25 -0700388 irq_cfg_alloc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 while (entry->next)
390 entry = irq_2_pin + entry->next;
391
392 if (entry->pin != -1) {
393 entry->next = first_free_entry;
394 entry = irq_2_pin + entry->next;
Yinghai Lu0799e432008-08-19 20:49:48 -0700395 if (++first_free_entry >= pin_map_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 panic("io_apic.c: whoops");
397 }
398 entry->apic = apic;
399 entry->pin = pin;
400}
401
402/*
403 * Reroute an IRQ to a different pin.
404 */
405static void __init replace_pin_at_irq(unsigned int irq,
406 int oldapic, int oldpin,
407 int newapic, int newpin)
408{
409 struct irq_pin_list *entry = irq_2_pin + irq;
410
411 while (1) {
412 if (entry->apic == oldapic && entry->pin == oldpin) {
413 entry->apic = newapic;
414 entry->pin = newpin;
415 }
416 if (!entry->next)
417 break;
418 entry = irq_2_pin + entry->next;
419 }
420}
421
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200422static void __modify_IO_APIC_irq(unsigned int irq, unsigned long enable, unsigned long disable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct irq_pin_list *entry = irq_2_pin + irq;
425 unsigned int pin, reg;
426
427 for (;;) {
428 pin = entry->pin;
429 if (pin == -1)
430 break;
431 reg = io_apic_read(entry->apic, 0x10 + pin*2);
432 reg &= ~disable;
433 reg |= enable;
434 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
435 if (!entry->next)
436 break;
437 entry = irq_2_pin + entry->next;
438 }
439}
440
441/* mask = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200442static void __mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400444 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/* mask = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200448static void __unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400450 __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/* mask = 1, trigger = 0 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200454static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400456 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
457 IO_APIC_REDIR_LEVEL_TRIGGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/* mask = 0, trigger = 1 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200461static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400463 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
464 IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200467static void mask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 unsigned long flags;
470
471 spin_lock_irqsave(&ioapic_lock, flags);
472 __mask_IO_APIC_irq(irq);
473 spin_unlock_irqrestore(&ioapic_lock, flags);
474}
475
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200476static void unmask_IO_APIC_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 unsigned long flags;
479
480 spin_lock_irqsave(&ioapic_lock, flags);
481 __unmask_IO_APIC_irq(irq);
482 spin_unlock_irqrestore(&ioapic_lock, flags);
483}
484
485static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
486{
487 struct IO_APIC_route_entry entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200490 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (entry.delivery_mode == dest_SMI)
492 return;
493
494 /*
495 * Disable it in the IO-APIC irq-routing table:
496 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800497 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200500static void clear_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 int apic, pin;
503
504 for (apic = 0; apic < nr_ioapics; apic++)
505 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
506 clear_IO_APIC_pin(apic, pin);
507}
508
Ashok Raj54d5d422005-09-06 15:16:15 -0700509#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
511{
512 unsigned long flags;
513 int pin;
514 struct irq_pin_list *entry = irq_2_pin + irq;
515 unsigned int apicid_value;
Ashok Raj54d5d422005-09-06 15:16:15 -0700516 cpumask_t tmp;
Yinghai Lu08678b02008-08-19 20:50:05 -0700517 struct irq_desc *desc;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200518
Ashok Raj54d5d422005-09-06 15:16:15 -0700519 cpus_and(tmp, cpumask, cpu_online_map);
520 if (cpus_empty(tmp))
521 tmp = TARGET_CPUS;
522
523 cpus_and(cpumask, tmp, CPU_MASK_ALL);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 apicid_value = cpu_mask_to_apicid(cpumask);
526 /* Prepare to do the io_apic_write */
527 apicid_value = apicid_value << 24;
528 spin_lock_irqsave(&ioapic_lock, flags);
529 for (;;) {
530 pin = entry->pin;
531 if (pin == -1)
532 break;
533 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
534 if (!entry->next)
535 break;
536 entry = irq_2_pin + entry->next;
537 }
Yinghai Lu08678b02008-08-19 20:50:05 -0700538 desc = irq_to_desc(irq);
539 desc->affinity = cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 spin_unlock_irqrestore(&ioapic_lock, flags);
541}
542
Ashok Raj54d5d422005-09-06 15:16:15 -0700543#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545#ifndef CONFIG_SMP
Harvey Harrison75604d72008-01-30 13:31:17 +0100546void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 unsigned int cfg;
549
550 /*
551 * Wait for idle.
552 */
553 apic_wait_icr_idle();
554 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
555 /*
556 * Send the IPI. The write to APIC_ICR fires this off.
557 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100558 apic_write(APIC_ICR, cfg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560#endif /* !CONFIG_SMP */
561
562
563/*
564 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
565 * specific CPU-side IRQs.
566 */
567
568#define MAX_PIRQS 8
569static int pirq_entries [MAX_PIRQS];
570static int pirqs_enabled;
571int skip_ioapic_setup;
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573static int __init ioapic_pirq_setup(char *str)
574{
575 int i, max;
576 int ints[MAX_PIRQS+1];
577
578 get_options(str, ARRAY_SIZE(ints), ints);
579
580 for (i = 0; i < MAX_PIRQS; i++)
581 pirq_entries[i] = -1;
582
583 pirqs_enabled = 1;
584 apic_printk(APIC_VERBOSE, KERN_INFO
585 "PIRQ redirection, working around broken MP-BIOS.\n");
586 max = MAX_PIRQS;
587 if (ints[0] < MAX_PIRQS)
588 max = ints[0];
589
590 for (i = 0; i < max; i++) {
591 apic_printk(APIC_VERBOSE, KERN_DEBUG
592 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
593 /*
594 * PIRQs are mapped upside down, usually.
595 */
596 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
597 }
598 return 1;
599}
600
601__setup("pirq=", ioapic_pirq_setup);
602
603/*
604 * Find the IRQ entry number of a certain pin.
605 */
606static int find_irq_entry(int apic, int pin, int type)
607{
608 int i;
609
610 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400611 if (mp_irqs[i].mp_irqtype == type &&
612 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
613 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
614 mp_irqs[i].mp_dstirq == pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return i;
616
617 return -1;
618}
619
620/*
621 * Find the pin to which IRQ[irq] (ISA) is connected
622 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800623static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 int i;
626
627 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400628 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300630 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400631 (mp_irqs[i].mp_irqtype == type) &&
632 (mp_irqs[i].mp_srcbusirq == irq))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400634 return mp_irqs[i].mp_dstirq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636 return -1;
637}
638
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800639static int __init find_isa_irq_apic(int irq, int type)
640{
641 int i;
642
643 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400644 int lbus = mp_irqs[i].mp_srcbus;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800645
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300646 if (test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400647 (mp_irqs[i].mp_irqtype == type) &&
648 (mp_irqs[i].mp_srcbusirq == irq))
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800649 break;
650 }
651 if (i < mp_irq_entries) {
652 int apic;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200653 for (apic = 0; apic < nr_ioapics; apic++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400654 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800655 return apic;
656 }
657 }
658
659 return -1;
660}
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/*
663 * Find a specific PCI IRQ entry.
664 * Not an __init, possibly needed by modules
665 */
666static int pin_2_irq(int idx, int apic, int pin);
667
668int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
669{
670 int apic, i, best_guess = -1;
671
672 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
673 "slot:%d, pin:%d.\n", bus, slot, pin);
Alexey Starikovskiyce6444d2008-05-19 19:47:09 +0400674 if (test_bit(bus, mp_bus_not_pci)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
676 return -1;
677 }
678 for (i = 0; i < mp_irq_entries; i++) {
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400679 int lbus = mp_irqs[i].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 for (apic = 0; apic < nr_ioapics; apic++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400682 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
683 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 break;
685
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300686 if (!test_bit(lbus, mp_bus_not_pci) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400687 !mp_irqs[i].mp_irqtype &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 (bus == lbus) &&
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400689 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
Ingo Molnar3de352b2008-07-08 11:14:58 +0200690 int irq = pin_2_irq(i, apic, mp_irqs[i].mp_dstirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (!(apic || IO_APIC_IRQ(irq)))
693 continue;
694
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400695 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return irq;
697 /*
698 * Use the first all-but-pin matching entry as a
699 * best-guess fuzzy result for broken mptables.
700 */
701 if (best_guess < 0)
702 best_guess = irq;
703 }
704 }
705 return best_guess;
706}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700707EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709/*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200710 * This function currently is only a helper for the i386 smp boot process where
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 * we need to reprogram the ioredtbls to cater for the cpus which have come online
712 * so mask in all cases should simply be TARGET_CPUS
713 */
Ashok Raj54d5d422005-09-06 15:16:15 -0700714#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715void __init setup_ioapic_dest(void)
716{
717 int pin, ioapic, irq, irq_entry;
718
719 if (skip_ioapic_setup == 1)
720 return;
721
722 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
723 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
724 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
725 if (irq_entry == -1)
726 continue;
727 irq = pin_2_irq(irq_entry, ioapic, pin);
728 set_ioapic_affinity_irq(irq, TARGET_CPUS);
729 }
730
731 }
732}
Ashok Raj54d5d422005-09-06 15:16:15 -0700733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300735#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
737 * EISA Edge/Level control register, ELCR
738 */
739static int EISA_ELCR(unsigned int irq)
740{
741 if (irq < 16) {
742 unsigned int port = 0x4d0 + (irq >> 3);
743 return (inb(port) >> (irq & 7)) & 1;
744 }
745 apic_printk(APIC_VERBOSE, KERN_INFO
746 "Broken MPtable reports ISA irq %d\n", irq);
747 return 0;
748}
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300749#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300751/* ISA interrupts are always polarity zero edge triggered,
752 * when listed as conforming in the MP table. */
753
754#define default_ISA_trigger(idx) (0)
755#define default_ISA_polarity(idx) (0)
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/* EISA interrupts are always polarity zero and can be edge or level
758 * trigger depending on the ELCR value. If an interrupt is listed as
759 * EISA conforming in the MP table, that means its trigger type must
760 * be read in from the ELCR */
761
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400762#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300763#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765/* PCI interrupts are always polarity one level triggered,
766 * when listed as conforming in the MP table. */
767
768#define default_PCI_trigger(idx) (1)
769#define default_PCI_polarity(idx) (1)
770
771/* MCA interrupts are always polarity zero level triggered,
772 * when listed as conforming in the MP table. */
773
774#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300775#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Shaohua Li61fd47e2007-11-17 01:05:28 -0500777static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400779 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 int polarity;
781
782 /*
783 * Determine IRQ line polarity (high active or low active):
784 */
Ingo Molnar3de352b2008-07-08 11:14:58 +0200785 switch (mp_irqs[idx].mp_irqflag & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200786 case 0: /* conforms, ie. bus-type dependent polarity */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200788 polarity = test_bit(bus, mp_bus_not_pci)?
789 default_ISA_polarity(idx):
790 default_PCI_polarity(idx);
791 break;
792 }
793 case 1: /* high active */
794 {
795 polarity = 0;
796 break;
797 }
798 case 2: /* reserved */
799 {
800 printk(KERN_WARNING "broken BIOS!!\n");
801 polarity = 1;
802 break;
803 }
804 case 3: /* low active */
805 {
806 polarity = 1;
807 break;
808 }
809 default: /* invalid */
810 {
811 printk(KERN_WARNING "broken BIOS!!\n");
812 polarity = 1;
813 break;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 return polarity;
817}
818
819static int MPBIOS_trigger(int idx)
820{
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400821 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 int trigger;
823
824 /*
825 * Determine IRQ trigger mode (edge or level sensitive):
826 */
Ingo Molnar3de352b2008-07-08 11:14:58 +0200827 switch ((mp_irqs[idx].mp_irqflag>>2) & 3) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200828 case 0: /* conforms, ie. bus-type dependent */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200830 trigger = test_bit(bus, mp_bus_not_pci)?
831 default_ISA_trigger(idx):
832 default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300833#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200834 switch (mp_bus_id_to_type[bus]) {
835 case MP_BUS_ISA: /* ISA pin */
836 {
837 /* set before the switch */
838 break;
839 }
840 case MP_BUS_EISA: /* EISA pin */
841 {
842 trigger = default_EISA_trigger(idx);
843 break;
844 }
845 case MP_BUS_PCI: /* PCI pin */
846 {
847 /* set before the switch */
848 break;
849 }
850 case MP_BUS_MCA: /* MCA pin */
851 {
852 trigger = default_MCA_trigger(idx);
853 break;
854 }
855 default:
856 {
857 printk(KERN_WARNING "broken BIOS!!\n");
858 trigger = 1;
859 break;
860 }
861 }
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300862#endif
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200863 break;
864 }
865 case 1: /* edge */
866 {
867 trigger = 0;
868 break;
869 }
870 case 2: /* reserved */
871 {
872 printk(KERN_WARNING "broken BIOS!!\n");
873 trigger = 1;
874 break;
875 }
876 case 3: /* level */
877 {
878 trigger = 1;
879 break;
880 }
881 default: /* invalid */
882 {
883 printk(KERN_WARNING "broken BIOS!!\n");
884 trigger = 0;
885 break;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 return trigger;
889}
890
891static inline int irq_polarity(int idx)
892{
893 return MPBIOS_polarity(idx);
894}
895
896static inline int irq_trigger(int idx)
897{
898 return MPBIOS_trigger(idx);
899}
900
901static int pin_2_irq(int idx, int apic, int pin)
902{
903 int irq, i;
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400904 int bus = mp_irqs[idx].mp_srcbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /*
907 * Debugging check, we are in big trouble if this message pops up!
908 */
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400909 if (mp_irqs[idx].mp_dstirq != pin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
911
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300912 if (test_bit(bus, mp_bus_not_pci))
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +0400913 irq = mp_irqs[idx].mp_srcbusirq;
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300914 else {
915 /*
916 * PCI IRQs are mapped in order
917 */
918 i = irq = 0;
919 while (i < apic)
920 irq += nr_ioapic_registers[i++];
921 irq += pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Alexey Starikovskiy643befe2008-03-20 14:54:49 +0300923 /*
924 * For MPS mode, so far only needed by ES7000 platform
925 */
926 if (ioapic_renumber_irq)
927 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
930 /*
931 * PCI IRQ command line redirection. Yes, limits are hardcoded.
932 */
933 if ((pin >= 16) && (pin <= 23)) {
934 if (pirq_entries[pin-16] != -1) {
935 if (!pirq_entries[pin-16]) {
936 apic_printk(APIC_VERBOSE, KERN_DEBUG
937 "disabling PIRQ%d\n", pin-16);
938 } else {
939 irq = pirq_entries[pin-16];
940 apic_printk(APIC_VERBOSE, KERN_DEBUG
941 "using PIRQ%d -> IRQ %d\n",
942 pin-16, irq);
943 }
944 }
945 }
946 return irq;
947}
948
949static inline int IO_APIC_irq_trigger(int irq)
950{
951 int apic, idx, pin;
952
953 for (apic = 0; apic < nr_ioapics; apic++) {
954 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200955 idx = find_irq_entry(apic, pin, mp_INT);
956 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return irq_trigger(idx);
958 }
959 }
960 /*
961 * nonexistent IRQs are edge default
962 */
963 return 0;
964}
965
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Eric W. Biedermanace80ab2006-10-04 02:16:47 -0700967static int __assign_irq_vector(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +0200969 static int current_vector = FIRST_DEVICE_VECTOR, current_offset;
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200970 int vector, offset;
Yinghai Luda51a822008-08-19 20:50:25 -0700971 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Yinghai Lu301e6192008-08-19 20:50:02 -0700973 BUG_ON((unsigned)irq >= nr_irqs);
Jan Beulich0a1ad602006-06-26 13:56:43 +0200974
Yinghai Luda51a822008-08-19 20:50:25 -0700975 cfg = irq_cfg(irq);
976 if (cfg->vector > 0)
977 return cfg->vector;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -0700978
Jan Beulich0a1ad602006-06-26 13:56:43 +0200979 vector = current_vector;
Eric W. Biederman8339f002007-01-29 13:19:05 -0700980 offset = current_offset;
981next:
982 vector += 8;
Alan Mayer305b92a2008-04-15 15:36:56 -0500983 if (vector >= first_system_vector) {
Eric W. Biederman8339f002007-01-29 13:19:05 -0700984 offset = (offset + 1) % 8;
985 vector = FIRST_DEVICE_VECTOR + offset;
986 }
987 if (vector == current_vector)
988 return -ENOSPC;
Rusty Russelldbeb2be2007-10-19 20:35:03 +0200989 if (test_and_set_bit(vector, used_vectors))
Eric W. Biederman8339f002007-01-29 13:19:05 -0700990 goto next;
Eric W. Biederman8339f002007-01-29 13:19:05 -0700991
992 current_vector = vector;
993 current_offset = offset;
Yinghai Luda51a822008-08-19 20:50:25 -0700994 cfg->vector = vector;
Jan Beulich0a1ad602006-06-26 13:56:43 +0200995
996 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Eric W. Biedermanace80ab2006-10-04 02:16:47 -0700999static int assign_irq_vector(int irq)
1000{
1001 unsigned long flags;
1002 int vector;
1003
1004 spin_lock_irqsave(&vector_lock, flags);
1005 vector = __assign_irq_vector(irq);
1006 spin_unlock_irqrestore(&vector_lock, flags);
1007
1008 return vector;
1009}
Glauber Costa3fde6902008-05-28 20:34:19 -07001010
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001011static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
1013#define IOAPIC_AUTO -1
1014#define IOAPIC_EDGE 0
1015#define IOAPIC_LEVEL 1
1016
Ingo Molnard1bef4e2006-06-29 02:24:36 -07001017static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Yinghai Lu08678b02008-08-19 20:50:05 -07001019 struct irq_desc *desc;
1020
1021 desc = irq_to_desc(irq);
Jan Beulich6ebcc002006-06-26 13:56:46 +02001022 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001023 trigger == IOAPIC_LEVEL) {
Yinghai Lu08678b02008-08-19 20:50:05 -07001024 desc->status |= IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001025 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1026 handle_fasteoi_irq, "fasteoi");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001027 } else {
Yinghai Lu08678b02008-08-19 20:50:05 -07001028 desc->status &= ~IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001029 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1030 handle_edge_irq, "edge");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001031 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001032 set_intr_gate(vector, interrupt[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035static void __init setup_IO_APIC_irqs(void)
1036{
1037 struct IO_APIC_route_entry entry;
1038 int apic, pin, idx, irq, first_notcon = 1, vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1041
1042 for (apic = 0; apic < nr_ioapics; apic++) {
1043 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1044
1045 /*
1046 * add it to the IO-APIC irq-routing table:
1047 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001048 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 entry.delivery_mode = INT_DELIVERY_MODE;
1051 entry.dest_mode = INT_DEST_MODE;
1052 entry.mask = 0; /* enable IRQ */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001053 entry.dest.logical.logical_dest =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 cpu_mask_to_apicid(TARGET_CPUS);
1055
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001056 idx = find_irq_entry(apic, pin, mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (idx == -1) {
1058 if (first_notcon) {
1059 apic_printk(APIC_VERBOSE, KERN_DEBUG
1060 " IO-APIC (apicid-pin) %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001061 mp_ioapics[apic].mp_apicid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 pin);
1063 first_notcon = 0;
1064 } else
1065 apic_printk(APIC_VERBOSE, ", %d-%d",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001066 mp_ioapics[apic].mp_apicid, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 continue;
1068 }
1069
Yinghai Lu20d225b2007-10-17 18:04:41 +02001070 if (!first_notcon) {
1071 apic_printk(APIC_VERBOSE, " not connected.\n");
1072 first_notcon = 1;
1073 }
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 entry.trigger = irq_trigger(idx);
1076 entry.polarity = irq_polarity(idx);
1077
1078 if (irq_trigger(idx)) {
1079 entry.trigger = 1;
1080 entry.mask = 1;
1081 }
1082
1083 irq = pin_2_irq(idx, apic, pin);
1084 /*
1085 * skip adding the timer int on secondary nodes, which causes
1086 * a small but painful rift in the time-space continuum
1087 */
1088 if (multi_timer_check(apic, irq))
1089 continue;
1090 else
1091 add_pin_to_irq(irq, apic, pin);
1092
1093 if (!apic && !IO_APIC_IRQ(irq))
1094 continue;
1095
1096 if (IO_APIC_IRQ(irq)) {
1097 vector = assign_irq_vector(irq);
1098 entry.vector = vector;
1099 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (!apic && (irq < 16))
1102 disable_8259A_irq(irq);
1103 }
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09001104 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106 }
1107
1108 if (!first_notcon)
1109 apic_printk(APIC_VERBOSE, " not connected.\n");
1110}
1111
1112/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001113 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001115static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1116 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
1118 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001120 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 /*
1123 * We use logical delivery to get the timer IRQ
1124 * to the first CPU.
1125 */
1126 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001127 entry.mask = 1; /* mask IRQ now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1129 entry.delivery_mode = INT_DELIVERY_MODE;
1130 entry.polarity = 0;
1131 entry.trigger = 0;
1132 entry.vector = vector;
1133
1134 /*
1135 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001136 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 */
Maciej W. Rozyckif0825262008-05-27 21:19:16 +01001138 ioapic_register_intr(0, vector, IOAPIC_EDGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 /*
1141 * Add it to the IO-APIC irq-routing table:
1142 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001143 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001146
1147__apicdebuginit(void) print_IO_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148{
1149 int apic, i;
1150 union IO_APIC_reg_00 reg_00;
1151 union IO_APIC_reg_01 reg_01;
1152 union IO_APIC_reg_02 reg_02;
1153 union IO_APIC_reg_03 reg_03;
1154 unsigned long flags;
1155
1156 if (apic_verbosity == APIC_QUIET)
1157 return;
1158
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001159 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 for (i = 0; i < nr_ioapics; i++)
1161 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001162 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /*
1165 * We are a bit conservative about what we expect. We have to
1166 * know about every hardware change ASAP.
1167 */
1168 printk(KERN_INFO "testing the IO APIC.......................\n");
1169
1170 for (apic = 0; apic < nr_ioapics; apic++) {
1171
1172 spin_lock_irqsave(&ioapic_lock, flags);
1173 reg_00.raw = io_apic_read(apic, 0);
1174 reg_01.raw = io_apic_read(apic, 1);
1175 if (reg_01.bits.version >= 0x10)
1176 reg_02.raw = io_apic_read(apic, 2);
1177 if (reg_01.bits.version >= 0x20)
1178 reg_03.raw = io_apic_read(apic, 3);
1179 spin_unlock_irqrestore(&ioapic_lock, flags);
1180
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001181 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1183 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1184 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1185 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1188 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1191 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /*
1194 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1195 * but the value of reg_02 is read as the previous read register
1196 * value, so ignore it if reg_02 == reg_01.
1197 */
1198 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1199 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1200 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202
1203 /*
1204 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1205 * or reg_03, but the value of reg_0[23] is read as the previous read
1206 * register value, so ignore it if reg_03 == reg_0[12].
1207 */
1208 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1209 reg_03.raw != reg_01.raw) {
1210 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1211 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
1214 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1215
1216 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1217 " Stat Dest Deli Vect: \n");
1218
1219 for (i = 0; i <= reg_01.bits.entries; i++) {
1220 struct IO_APIC_route_entry entry;
1221
Andi Kleencf4c6a22006-09-26 10:52:30 +02001222 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 printk(KERN_DEBUG " %02x %03X %02X ",
1225 i,
1226 entry.dest.logical.logical_dest,
1227 entry.dest.physical.physical_dest
1228 );
1229
1230 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1231 entry.mask,
1232 entry.trigger,
1233 entry.irr,
1234 entry.polarity,
1235 entry.delivery_status,
1236 entry.dest_mode,
1237 entry.delivery_mode,
1238 entry.vector
1239 );
1240 }
1241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 printk(KERN_DEBUG "IRQ to pin mappings:\n");
Yinghai Lu0799e432008-08-19 20:49:48 -07001243 for (i = 0; i < nr_irqs; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct irq_pin_list *entry = irq_2_pin + i;
1245 if (entry->pin < 0)
1246 continue;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001247 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 for (;;) {
1249 printk("-> %d:%d", entry->apic, entry->pin);
1250 if (!entry->next)
1251 break;
1252 entry = irq_2_pin + entry->next;
1253 }
1254 printk("\n");
1255 }
1256
1257 printk(KERN_INFO ".................................... done.\n");
1258
1259 return;
1260}
1261
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001262__apicdebuginit(void) print_APIC_bitfield(int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 unsigned int v;
1265 int i, j;
1266
1267 if (apic_verbosity == APIC_QUIET)
1268 return;
1269
1270 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1271 for (i = 0; i < 8; i++) {
1272 v = apic_read(base + i*0x10);
1273 for (j = 0; j < 32; j++) {
1274 if (v & (1<<j))
1275 printk("1");
1276 else
1277 printk("0");
1278 }
1279 printk("\n");
1280 }
1281}
1282
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001283__apicdebuginit(void) print_local_APIC(void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 unsigned int v, ver, maxlvt;
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001286 u64 icr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 if (apic_verbosity == APIC_QUIET)
1289 return;
1290
1291 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1292 smp_processor_id(), hard_smp_processor_id());
Andreas Herrmann66823112008-06-05 16:35:10 +02001293 v = apic_read(APIC_ID);
Jack Steiner05f2d122008-03-28 14:12:02 -05001294 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001295 GET_APIC_ID(v));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 v = apic_read(APIC_LVR);
1297 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1298 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001299 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 v = apic_read(APIC_TASKPRI);
1302 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1303
1304 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1305 v = apic_read(APIC_ARBPRI);
1306 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1307 v & APIC_ARBPRI_MASK);
1308 v = apic_read(APIC_PROCPRI);
1309 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1310 }
1311
1312 v = apic_read(APIC_EOI);
1313 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1314 v = apic_read(APIC_RRR);
1315 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1316 v = apic_read(APIC_LDR);
1317 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1318 v = apic_read(APIC_DFR);
1319 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1320 v = apic_read(APIC_SPIV);
1321 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1322
1323 printk(KERN_DEBUG "... APIC ISR field:\n");
1324 print_APIC_bitfield(APIC_ISR);
1325 printk(KERN_DEBUG "... APIC TMR field:\n");
1326 print_APIC_bitfield(APIC_TMR);
1327 printk(KERN_DEBUG "... APIC IRR field:\n");
1328 print_APIC_bitfield(APIC_IRR);
1329
1330 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1331 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1332 apic_write(APIC_ESR, 0);
1333 v = apic_read(APIC_ESR);
1334 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1335 }
1336
Hiroshi Shimamoto7ab6af72008-07-30 17:36:48 -07001337 icr = apic_icr_read();
1338 printk(KERN_DEBUG "... APIC ICR: %08x\n", icr);
1339 printk(KERN_DEBUG "... APIC ICR2: %08x\n", icr >> 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 v = apic_read(APIC_LVTT);
1342 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1343
1344 if (maxlvt > 3) { /* PC is LVT#4. */
1345 v = apic_read(APIC_LVTPC);
1346 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1347 }
1348 v = apic_read(APIC_LVT0);
1349 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1350 v = apic_read(APIC_LVT1);
1351 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1352
1353 if (maxlvt > 2) { /* ERR is LVT#3. */
1354 v = apic_read(APIC_LVTERR);
1355 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1356 }
1357
1358 v = apic_read(APIC_TMICT);
1359 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1360 v = apic_read(APIC_TMCCT);
1361 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1362 v = apic_read(APIC_TDCR);
1363 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1364 printk("\n");
1365}
1366
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001367__apicdebuginit(void) print_all_local_APICs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368{
Jens Axboe15c8b6c2008-05-09 09:39:44 +02001369 on_each_cpu(print_local_APIC, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370}
1371
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001372__apicdebuginit(void) print_PIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 unsigned int v;
1375 unsigned long flags;
1376
1377 if (apic_verbosity == APIC_QUIET)
1378 return;
1379
1380 printk(KERN_DEBUG "\nprinting PIC contents\n");
1381
1382 spin_lock_irqsave(&i8259A_lock, flags);
1383
1384 v = inb(0xa1) << 8 | inb(0x21);
1385 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1386
1387 v = inb(0xa0) << 8 | inb(0x20);
1388 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1389
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001390 outb(0x0b, 0xa0);
1391 outb(0x0b, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 v = inb(0xa0) << 8 | inb(0x20);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001393 outb(0x0a, 0xa0);
1394 outb(0x0a, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 spin_unlock_irqrestore(&i8259A_lock, flags);
1397
1398 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1399
1400 v = inb(0x4d1) << 8 | inb(0x4d0);
1401 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1402}
1403
Maciej W. Rozycki32f71af2008-07-21 00:52:49 +01001404__apicdebuginit(int) print_all_ICs(void)
1405{
1406 print_PIC();
1407 print_all_local_APICs();
1408 print_IO_APIC();
1409
1410 return 0;
1411}
1412
1413fs_initcall(print_all_ICs);
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
1416static void __init enable_IO_APIC(void)
1417{
1418 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001419 int i8259_apic, i8259_pin;
1420 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 unsigned long flags;
1422
Yinghai Lu0799e432008-08-19 20:49:48 -07001423 for (i = 0; i < pin_map_size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 irq_2_pin[i].pin = -1;
1425 irq_2_pin[i].next = 0;
1426 }
1427 if (!pirqs_enabled)
1428 for (i = 0; i < MAX_PIRQS; i++)
1429 pirq_entries[i] = -1;
1430
1431 /*
1432 * The number of IO-APIC IRQ registers (== #pins):
1433 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001434 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001436 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001438 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1439 }
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001440 for (apic = 0; apic < nr_ioapics; apic++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001441 int pin;
1442 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001443 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001444 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001445 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001446
1447
1448 /* If the interrupt line is enabled and in ExtInt mode
1449 * I have found the pin where the i8259 is connected.
1450 */
1451 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1452 ioapic_i8259.apic = apic;
1453 ioapic_i8259.pin = pin;
1454 goto found_i8259;
1455 }
1456 }
1457 }
1458 found_i8259:
1459 /* Look to see what if the MP table has reported the ExtINT */
1460 /* If we could not find the appropriate pin by looking at the ioapic
1461 * the i8259 probably is not connected the ioapic but give the
1462 * mptable a chance anyway.
1463 */
1464 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1465 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1466 /* Trust the MP table if nothing is setup in the hardware */
1467 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1468 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1469 ioapic_i8259.pin = i8259_pin;
1470 ioapic_i8259.apic = i8259_apic;
1471 }
1472 /* Complain if the MP table and the hardware disagree */
1473 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1474 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1475 {
1476 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
1479 /*
1480 * Do not trust the IO-APIC being empty at bootup
1481 */
1482 clear_IO_APIC();
1483}
1484
1485/*
1486 * Not an __init, needed by the reboot code
1487 */
1488void disable_IO_APIC(void)
1489{
1490 /*
1491 * Clear the IO-APIC before rebooting:
1492 */
1493 clear_IO_APIC();
1494
Eric W. Biederman650927e2005-06-25 14:57:44 -07001495 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001496 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001497 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001498 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001499 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001500 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001501 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001502
1503 memset(&entry, 0, sizeof(entry));
1504 entry.mask = 0; /* Enabled */
1505 entry.trigger = 0; /* Edge */
1506 entry.irr = 0;
1507 entry.polarity = 0; /* High */
1508 entry.delivery_status = 0;
1509 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001510 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001511 entry.vector = 0;
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001512 entry.dest.physical.physical_dest = read_apic_id();
Eric W. Biederman650927e2005-06-25 14:57:44 -07001513
1514 /*
1515 * Add it to the IO-APIC irq-routing table:
1516 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001517 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001518 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001519 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520}
1521
1522/*
1523 * function to set the IO-APIC physical IDs based on the
1524 * values stored in the MPC table.
1525 *
1526 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1527 */
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529static void __init setup_ioapic_ids_from_mpc(void)
1530{
1531 union IO_APIC_reg_00 reg_00;
1532 physid_mask_t phys_id_present_map;
1533 int apic;
1534 int i;
1535 unsigned char old_id;
1536 unsigned long flags;
1537
Yinghai Lua4dbc342008-07-25 02:14:28 -07001538 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
Yinghai Lud49c4282008-06-08 18:31:54 -07001539 return;
Yinghai Lud49c4282008-06-08 18:31:54 -07001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001542 * Don't check I/O APIC IDs for xAPIC systems. They have
1543 * no meaning without the serial APIC bus.
1544 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001545 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1546 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001547 return;
1548 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 * This is broken; anything with a real cpu count has to
1550 * circumvent this idiocy regardless.
1551 */
1552 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1553
1554 /*
1555 * Set the IOAPIC ID to the value stored in the MPC table.
1556 */
1557 for (apic = 0; apic < nr_ioapics; apic++) {
1558
1559 /* Read the register 0 value */
1560 spin_lock_irqsave(&ioapic_lock, flags);
1561 reg_00.raw = io_apic_read(apic, 0);
1562 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001563
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001564 old_id = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001566 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001568 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1570 reg_00.bits.ID);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001571 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 /*
1575 * Sanity check, is the ID really free? Every APIC in a
1576 * system must have a unique ID or we get lots of nice
1577 * 'stuck on smp_invalidate_needed IPI wait' messages.
1578 */
1579 if (check_apicid_used(phys_id_present_map,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001580 mp_ioapics[apic].mp_apicid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001582 apic, mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 for (i = 0; i < get_physical_broadcast(); i++)
1584 if (!physid_isset(i, phys_id_present_map))
1585 break;
1586 if (i >= get_physical_broadcast())
1587 panic("Max APIC ID exceeded!\n");
1588 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1589 i);
1590 physid_set(i, phys_id_present_map);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001591 mp_ioapics[apic].mp_apicid = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 } else {
1593 physid_mask_t tmp;
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001594 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 apic_printk(APIC_VERBOSE, "Setting %d in the "
1596 "phys_id_present_map\n",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001597 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1599 }
1600
1601
1602 /*
1603 * We need to adjust the IRQ routing table
1604 * if the ID changed.
1605 */
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001606 if (old_id != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04001608 if (mp_irqs[i].mp_dstapic == old_id)
1609 mp_irqs[i].mp_dstapic
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001610 = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 /*
1613 * Read the right value from the MPC table and
1614 * write it into the ID register.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001615 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 apic_printk(APIC_VERBOSE, KERN_INFO
1617 "...changing IO-APIC physical APIC ID to %d ...",
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001618 mp_ioapics[apic].mp_apicid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001620 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 spin_lock_irqsave(&ioapic_lock, flags);
1622 io_apic_write(apic, 0, reg_00.raw);
1623 spin_unlock_irqrestore(&ioapic_lock, flags);
1624
1625 /*
1626 * Sanity check
1627 */
1628 spin_lock_irqsave(&ioapic_lock, flags);
1629 reg_00.raw = io_apic_read(apic, 0);
1630 spin_unlock_irqrestore(&ioapic_lock, flags);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001631 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 printk("could not set ID!\n");
1633 else
1634 apic_printk(APIC_VERBOSE, " ok.\n");
1635 }
1636}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001638int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001639
1640static int __init notimercheck(char *s)
1641{
1642 no_timer_check = 1;
1643 return 1;
1644}
1645__setup("no_timer_check", notimercheck);
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647/*
1648 * There is a nasty bug in some older SMP boards, their mptable lies
1649 * about the timer IRQ. We do the following to work around the situation:
1650 *
1651 * - timer IRQ defaults to IO-APIC IRQ
1652 * - if this function detects that timer IRQs are defunct, then we fall
1653 * back to ISA timer IRQs
1654 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001655static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656{
1657 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001658 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Zachary Amsden8542b202006-12-07 02:14:09 +01001660 if (no_timer_check)
1661 return 1;
1662
Ingo Molnar4aae0702007-12-18 18:05:58 +01001663 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 local_irq_enable();
1665 /* Let ten ticks pass... */
1666 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001667 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 /*
1670 * Expect a few ticks at least, to be sure some possible
1671 * glue logic does not lock up after one or two first
1672 * ticks in a non-ExtINT mode. Also the local APIC
1673 * might have cached one ExtINT interrupt. Finally, at
1674 * least one tick may be lost due to delays.
1675 */
Julia Lawall1d16b532008-01-30 13:32:19 +01001676 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return 1;
1678
1679 return 0;
1680}
1681
1682/*
1683 * In the SMP+IOAPIC case it might happen that there are an unspecified
1684 * number of pending IRQ events unhandled. These cases are very rare,
1685 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1686 * better to do it this way as thus we do not have to be aware of
1687 * 'pending' interrupts in the IRQ path, except at this point.
1688 */
1689/*
1690 * Edge triggered needs to resend any interrupt
1691 * that was delayed but this is now handled in the device
1692 * independent code.
1693 */
1694
1695/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001696 * Startup quirk:
1697 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 * Starting up a edge-triggered IO-APIC interrupt is
1699 * nasty - we need to make sure that we get the edge.
1700 * If it is already asserted for some reason, we need
1701 * return 1 to indicate that is was pending.
1702 *
1703 * This is not complete - we should be able to fake
1704 * an edge even if it isn't on the 8259A...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001705 *
1706 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001708static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
1710 int was_pending = 0;
1711 unsigned long flags;
1712
1713 spin_lock_irqsave(&ioapic_lock, flags);
1714 if (irq < 16) {
1715 disable_8259A_irq(irq);
1716 if (i8259A_irq_pending(irq))
1717 was_pending = 1;
1718 }
1719 __unmask_IO_APIC_irq(irq);
1720 spin_unlock_irqrestore(&ioapic_lock, flags);
1721
1722 return was_pending;
1723}
1724
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001725static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001727 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 ack_APIC_irq();
1729}
1730
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001731static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
1733 unsigned long v;
1734 int i;
1735
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001736 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737/*
1738 * It appears there is an erratum which affects at least version 0x11
1739 * of I/O APIC (that's the 82093AA and cores integrated into various
1740 * chipsets). Under certain conditions a level-triggered interrupt is
1741 * erroneously delivered as edge-triggered one but the respective IRR
1742 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1743 * message but it will never arrive and further interrupts are blocked
1744 * from the source. The exact reason is so far unknown, but the
1745 * phenomenon was observed when two consecutive interrupt requests
1746 * from a given source get delivered to the same CPU and the source is
1747 * temporarily disabled in between.
1748 *
1749 * A workaround is to simulate an EOI message manually. We achieve it
1750 * by setting the trigger mode to edge and then to level when the edge
1751 * trigger mode gets detected in the TMR of a local APIC for a
1752 * level-triggered interrupt. We mask the source for the time of the
1753 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1754 * The idea is from Manfred Spraul. --macro
1755 */
Yinghai Lua1420f32008-08-19 20:50:24 -07001756 i = irq_cfg(irq)->vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1759
1760 ack_APIC_irq();
1761
1762 if (!(v & (1 << (i & 0x1f)))) {
1763 atomic_inc(&irq_mis_count);
1764 spin_lock(&ioapic_lock);
1765 __mask_and_edge_IO_APIC_irq(irq);
1766 __unmask_and_level_IO_APIC_irq(irq);
1767 spin_unlock(&ioapic_lock);
1768 }
1769}
1770
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001771static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Yinghai Lua1420f32008-08-19 20:50:24 -07001773 send_IPI_self(irq_cfg(irq)->vector);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07001774
1775 return 1;
1776}
1777
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001778static struct irq_chip ioapic_chip __read_mostly = {
1779 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001780 .startup = startup_ioapic_irq,
1781 .mask = mask_IO_APIC_irq,
1782 .unmask = unmask_IO_APIC_irq,
1783 .ack = ack_ioapic_irq,
1784 .eoi = ack_ioapic_quirk_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001785#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001786 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001787#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001788 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789};
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792static inline void init_IO_APIC_traps(void)
1793{
1794 int irq;
Yinghai Lu08678b02008-08-19 20:50:05 -07001795 struct irq_desc *desc;
Yinghai Luda51a822008-08-19 20:50:25 -07001796 struct irq_cfg *cfg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /*
1799 * NOTE! The local APIC isn't very good at handling
1800 * multiple interrupts at the same interrupt level.
1801 * As the interrupt level is determined by taking the
1802 * vector number and shifting that right by 4, we
1803 * want to spread these out a bit so that they don't
1804 * all fall in the same interrupt level.
1805 *
1806 * Also, we've got to be careful not to trash gate
1807 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1808 */
Yinghai Luda51a822008-08-19 20:50:25 -07001809 for_each_irq_cfg(cfg) {
1810 irq = cfg->irq;
1811 if (IO_APIC_IRQ(irq) && !cfg->vector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * Hmm.. We don't have an entry for this,
1814 * so default to an old-fashioned 8259
1815 * interrupt if we can..
1816 */
1817 if (irq < 16)
1818 make_8259A_irq(irq);
Yinghai Lu08678b02008-08-19 20:50:05 -07001819 else {
1820 desc = irq_to_desc(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 /* Strange. Oh, well.. */
Yinghai Lu08678b02008-08-19 20:50:05 -07001822 desc->chip = &no_irq_chip;
1823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 }
1825 }
1826}
1827
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001828/*
1829 * The local APIC irq-chip implementation:
1830 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001832static void ack_lapic_irq(unsigned int irq)
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001833{
1834 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
1836
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001837static void mask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
1839 unsigned long v;
1840
1841 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001842 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001845static void unmask_lapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001847 unsigned long v;
1848
1849 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001850 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001853static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01001854 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001855 .mask = mask_lapic_irq,
1856 .unmask = unmask_lapic_irq,
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001857 .ack = ack_lapic_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858};
1859
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001860static void lapic_register_intr(int irq, int vector)
1861{
Yinghai Lu08678b02008-08-19 20:50:05 -07001862 struct irq_desc *desc;
1863
1864 desc = irq_to_desc(irq);
1865 desc->status &= ~IRQ_LEVEL;
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01001866 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
1867 "edge");
1868 set_intr_gate(vector, interrupt[irq]);
1869}
1870
Jan Beuliche9427102008-01-30 13:31:24 +01001871static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
1873 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001874 * Dirty trick to enable the NMI watchdog ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * We put the 8259A master into AEOI mode and
1876 * unmask on all local APICs LVT0 as NMI.
1877 *
1878 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1879 * is from Maciej W. Rozycki - so we do not have to EOI from
1880 * the NMI handler or the timer interrupt.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02001881 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
1883
Jan Beuliche9427102008-01-30 13:31:24 +01001884 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 apic_printk(APIC_VERBOSE, " done.\n");
1887}
1888
1889/*
1890 * This looks a bit hackish but it's about the only one way of sending
1891 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1892 * not support the ExtINT mode, unfortunately. We need to send these
1893 * cycles as some i82489DX-based boards have glue logic that keeps the
1894 * 8259A interrupt line asserted until INTA. --macro
1895 */
Jacek Luczak28acf282008-04-12 17:41:12 +02001896static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001898 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct IO_APIC_route_entry entry0, entry1;
1900 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001902 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01001903 if (pin == -1) {
1904 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01001906 }
1907 apic = find_isa_irq_apic(8, mp_INT);
1908 if (apic == -1) {
1909 WARN_ON_ONCE(1);
1910 return;
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Andi Kleencf4c6a22006-09-26 10:52:30 +02001913 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001914 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 memset(&entry1, 0, sizeof(entry1));
1917
1918 entry1.dest_mode = 0; /* physical delivery */
1919 entry1.mask = 0; /* unmask IRQ now */
1920 entry1.dest.physical.physical_dest = hard_smp_processor_id();
1921 entry1.delivery_mode = dest_ExtINT;
1922 entry1.polarity = entry0.polarity;
1923 entry1.trigger = 0;
1924 entry1.vector = 0;
1925
Andi Kleencf4c6a22006-09-26 10:52:30 +02001926 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 save_control = CMOS_READ(RTC_CONTROL);
1929 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1930 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1931 RTC_FREQ_SELECT);
1932 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1933
1934 i = 100;
1935 while (i-- > 0) {
1936 mdelay(10);
1937 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1938 i -= 10;
1939 }
1940
1941 CMOS_WRITE(save_control, RTC_CONTROL);
1942 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001943 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Andi Kleencf4c6a22006-09-26 10:52:30 +02001945 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
1948/*
1949 * This code may look a bit paranoid, but it's supposed to cooperate with
1950 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1951 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1952 * fanatically on his truly buggy board.
1953 */
Zachary Amsden8542b202006-12-07 02:14:09 +01001954static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001956 int apic1, pin1, apic2, pin2;
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01001957 int no_pin1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 int vector;
Ingo Molnar6e908942008-03-21 14:32:36 +01001959 unsigned int ver;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001960 unsigned long flags;
1961
1962 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01001963
Ingo Molnar6e908942008-03-21 14:32:36 +01001964 ver = apic_read(APIC_LVR);
1965 ver = GET_APIC_VERSION(ver);
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /*
1968 * get/set the timer IRQ vector:
1969 */
1970 disable_8259A_irq(0);
1971 vector = assign_irq_vector(0);
1972 set_intr_gate(vector, interrupt[0]);
1973
1974 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01001975 * As IRQ0 is to be enabled in the 8259A, the virtual
1976 * wire has to be disabled in the local APIC. Also
1977 * timer interrupts need to be acknowledged manually in
1978 * the 8259A for the i82489DX when using the NMI
1979 * watchdog as that APIC treats NMIs as level-triggered.
1980 * The AEOI mode will finish them in the 8259A
1981 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001983 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 init_8259A(1);
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01001985 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001987 pin1 = find_isa_irq_pin(0, mp_INT);
1988 apic1 = find_isa_irq_apic(0, mp_INT);
1989 pin2 = ioapic_i8259.pin;
1990 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01001992 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
1993 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
1994 vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01001996 /*
1997 * Some BIOS writers are clueless and report the ExtINTA
1998 * I/O APIC input from the cascaded 8259A as the timer
1999 * interrupt input. So just in case, if only one pin
2000 * was found above, try it both directly and through the
2001 * 8259A.
2002 */
2003 if (pin1 == -1) {
2004 pin1 = pin2;
2005 apic1 = apic2;
2006 no_pin1 = 1;
2007 } else if (pin2 == -1) {
2008 pin2 = pin1;
2009 apic2 = apic1;
2010 }
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (pin1 != -1) {
2013 /*
2014 * Ok, does IRQ0 through the IOAPIC work?
2015 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002016 if (no_pin1) {
2017 add_pin_to_irq(0, apic1, pin1);
2018 setup_timer_IRQ0_pin(apic1, pin1, vector);
2019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 unmask_IO_APIC_irq(0);
2021 if (timer_irq_works()) {
2022 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 setup_nmi();
2024 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002026 if (disable_timer_pin_1 > 0)
2027 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002028 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002030 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002031 if (!no_pin1)
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002032 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2033 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002035 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2036 "(IRQ0) through the 8259A ...\n");
2037 apic_printk(APIC_QUIET, KERN_INFO
2038 "..... (found apic %d pin %d) ...\n", apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /*
2040 * legacy devices should be connected to IO APIC #0
2041 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002042 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01002043 setup_timer_IRQ0_pin(apic2, pin2, vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002044 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002045 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002047 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002048 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002050 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002052 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002054 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 /*
2057 * Cleanup, just in case ...
2058 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002059 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002060 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002061 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002065 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2066 "through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002067 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002069 timer_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002071 apic_printk(APIC_QUIET, KERN_INFO
2072 "...trying to set up timer as Virtual Wire IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
Maciej W. Rozyckic88ac1d2008-07-11 19:35:17 +01002074 lapic_register_intr(0, vector);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002075 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 enable_8259A_irq(0);
2077
2078 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002079 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002080 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002082 disable_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002083 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002084 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002086 apic_printk(APIC_QUIET, KERN_INFO
2087 "...trying to set up timer as ExtINT IRQ...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 init_8259A(0);
2090 make_8259A_irq(0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01002091 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 unlock_ExtINT_logic();
2094
2095 if (timer_irq_works()) {
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002096 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002097 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002099 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
Maciej W. Rozycki49a66a0b2008-07-14 19:08:13 +01002101 "report. Then try booting with the 'noapic' option.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002102out:
2103 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
2106/*
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002107 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2108 * to devices. However there may be an I/O APIC pin available for
2109 * this interrupt regardless. The pin may be left unconnected, but
2110 * typically it will be reused as an ExtINT cascade interrupt for
2111 * the master 8259A. In the MPS case such a pin will normally be
2112 * reported as an ExtINT interrupt in the MP table. With ACPI
2113 * there is no provision for ExtINT interrupts, and in the absence
2114 * of an override it would be treated as an ordinary ISA I/O APIC
2115 * interrupt, that is edge-triggered and unmasked by default. We
2116 * used to do this, but it caused problems on some systems because
2117 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2118 * the same ExtINT cascade interrupt to drive the local APIC of the
2119 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2120 * the I/O APIC in all cases now. No actual device should request
2121 * it anyway. --macro
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 */
2123#define PIC_IRQS (1 << PIC_CASCADE_IR)
2124
2125void __init setup_IO_APIC(void)
2126{
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002127 int i;
2128
2129 /* Reserve all the system vectors. */
Alan Mayer305b92a2008-04-15 15:36:56 -05002130 for (i = first_system_vector; i < NR_VECTORS; i++)
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002131 set_bit(i, used_vectors);
2132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 enable_IO_APIC();
2134
Maciej W. Rozyckiaf174782008-07-11 19:35:23 +01002135 io_apic_irqs = ~PIC_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 printk("ENABLING IO-APIC IRQs\n");
2138
2139 /*
2140 * Set up IO-APIC IRQ routing.
2141 */
2142 if (!acpi_ioapic)
2143 setup_ioapic_ids_from_mpc();
2144 sync_Arb_IDs();
2145 setup_IO_APIC_irqs();
2146 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002147 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/*
2151 * Called after all the initialization is done. If we didnt find any
2152 * APIC bugs then we can allow the modify fast path
2153 */
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155static int __init io_apic_bug_finalize(void)
2156{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002157 if (sis_apic_bug == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 sis_apic_bug = 0;
2159 return 0;
2160}
2161
2162late_initcall(io_apic_bug_finalize);
2163
2164struct sysfs_ioapic_data {
2165 struct sys_device dev;
2166 struct IO_APIC_route_entry entry[0];
2167};
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002168static struct sysfs_ioapic_data *mp_ioapic_data[MAX_IO_APICS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Pavel Machek438510f2005-04-16 15:25:24 -07002170static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 struct IO_APIC_route_entry *entry;
2173 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 data = container_of(dev, struct sysfs_ioapic_data, dev);
2177 entry = data->entry;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002178 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002179 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 return 0;
2182}
2183
2184static int ioapic_resume(struct sys_device *dev)
2185{
2186 struct IO_APIC_route_entry *entry;
2187 struct sysfs_ioapic_data *data;
2188 unsigned long flags;
2189 union IO_APIC_reg_00 reg_00;
2190 int i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 data = container_of(dev, struct sysfs_ioapic_data, dev);
2193 entry = data->entry;
2194
2195 spin_lock_irqsave(&ioapic_lock, flags);
2196 reg_00.raw = io_apic_read(dev->id, 0);
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002197 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
2198 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 io_apic_write(dev->id, 0, reg_00.raw);
2200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 spin_unlock_irqrestore(&ioapic_lock, flags);
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002202 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
Andi Kleencf4c6a22006-09-26 10:52:30 +02002203 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 return 0;
2206}
2207
2208static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002209 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 .suspend = ioapic_suspend,
2211 .resume = ioapic_resume,
2212};
2213
2214static int __init ioapic_init_sysfs(void)
2215{
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002216 struct sys_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 int i, size, error = 0;
2218
2219 error = sysdev_class_register(&ioapic_sysdev_class);
2220 if (error)
2221 return error;
2222
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002223 for (i = 0; i < nr_ioapics; i++) {
2224 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 * sizeof(struct IO_APIC_route_entry);
Christophe Jaillet25556c12008-06-22 22:13:48 +02002226 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if (!mp_ioapic_data[i]) {
2228 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2229 continue;
2230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 dev = &mp_ioapic_data[i]->dev;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002232 dev->id = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 dev->cls = &ioapic_sysdev_class;
2234 error = sysdev_register(dev);
2235 if (error) {
2236 kfree(mp_ioapic_data[i]);
2237 mp_ioapic_data[i] = NULL;
2238 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2239 continue;
2240 }
2241 }
2242
2243 return 0;
2244}
2245
2246device_initcall(ioapic_init_sysfs);
2247
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002248/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002249 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002250 */
2251int create_irq(void)
2252{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002253 /* Allocate an unused irq */
Andi Kleen306a22c2006-12-09 21:33:36 +01002254 int irq, new, vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002255 unsigned long flags;
Yinghai Luda51a822008-08-19 20:50:25 -07002256 struct irq_cfg *cfg_new;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002257
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002258 irq = -ENOSPC;
2259 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu0799e432008-08-19 20:49:48 -07002260 for (new = (nr_irqs - 1); new >= 0; new--) {
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002261 if (platform_legacy_irq(new))
2262 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07002263 cfg_new = irq_cfg(new);
2264 if (cfg_new && cfg_new->vector != 0)
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002265 continue;
Yinghai Luda51a822008-08-19 20:50:25 -07002266 if (!cfg_new)
2267 cfg_new = irq_cfg_alloc(new);
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002268 vector = __assign_irq_vector(new);
2269 if (likely(vector > 0))
2270 irq = new;
2271 break;
2272 }
2273 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002274
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002275 if (irq >= 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002276 set_intr_gate(vector, interrupt[irq]);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002277 dynamic_irq_init(irq);
2278 }
2279 return irq;
2280}
2281
2282void destroy_irq(unsigned int irq)
2283{
2284 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002285
2286 dynamic_irq_cleanup(irq);
2287
2288 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lua1420f32008-08-19 20:50:24 -07002289 clear_bit(irq_cfg(irq)->vector, used_vectors);
2290 irq_cfg(irq)->vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002291 spin_unlock_irqrestore(&vector_lock, flags);
2292}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002293
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002294/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002295 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002296 */
2297#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002298static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002299{
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002300 int vector;
2301 unsigned dest;
2302
2303 vector = assign_irq_vector(irq);
2304 if (vector >= 0) {
2305 dest = cpu_mask_to_apicid(TARGET_CPUS);
2306
2307 msg->address_hi = MSI_ADDR_BASE_HI;
2308 msg->address_lo =
2309 MSI_ADDR_BASE_LO |
2310 ((INT_DEST_MODE == 0) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002311MSI_ADDR_DEST_MODE_PHYSICAL:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002312 MSI_ADDR_DEST_MODE_LOGICAL) |
2313 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2314 MSI_ADDR_REDIRECTION_CPU:
2315 MSI_ADDR_REDIRECTION_LOWPRI) |
2316 MSI_ADDR_DEST_ID(dest);
2317
2318 msg->data =
2319 MSI_DATA_TRIGGER_EDGE |
2320 MSI_DATA_LEVEL_ASSERT |
2321 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002322MSI_DATA_DELIVERY_FIXED:
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002323 MSI_DATA_DELIVERY_LOWPRI) |
2324 MSI_DATA_VECTOR(vector);
2325 }
2326 return vector;
2327}
2328
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002329#ifdef CONFIG_SMP
2330static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2331{
2332 struct msi_msg msg;
2333 unsigned int dest;
2334 cpumask_t tmp;
2335 int vector;
Yinghai Lu08678b02008-08-19 20:50:05 -07002336 struct irq_desc *desc;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002337
2338 cpus_and(tmp, mask, cpu_online_map);
2339 if (cpus_empty(tmp))
2340 tmp = TARGET_CPUS;
2341
2342 vector = assign_irq_vector(irq);
2343 if (vector < 0)
2344 return;
2345
2346 dest = cpu_mask_to_apicid(mask);
2347
2348 read_msi_msg(irq, &msg);
2349
2350 msg.data &= ~MSI_DATA_VECTOR_MASK;
2351 msg.data |= MSI_DATA_VECTOR(vector);
2352 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2353 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2354
2355 write_msi_msg(irq, &msg);
Yinghai Lu08678b02008-08-19 20:50:05 -07002356 desc = irq_to_desc(irq);
2357 desc->affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002358}
2359#endif /* CONFIG_SMP */
2360
2361/*
2362 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2363 * which implement the MSI or MSI-X Capability Structure.
2364 */
2365static struct irq_chip msi_chip = {
2366 .name = "PCI-MSI",
2367 .unmask = unmask_msi_irq,
2368 .mask = mask_msi_irq,
2369 .ack = ack_ioapic_irq,
2370#ifdef CONFIG_SMP
2371 .set_affinity = set_msi_irq_affinity,
2372#endif
2373 .retrigger = ioapic_retrigger_irq,
2374};
2375
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002376int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002377{
2378 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002379 int irq, ret;
2380 irq = create_irq();
2381 if (irq < 0)
2382 return irq;
2383
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002384 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002385 if (ret < 0) {
2386 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002387 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002388 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002389
Michael Ellerman7fe37302007-04-18 19:39:21 +10002390 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002391 write_msi_msg(irq, &msg);
2392
Ingo Molnara460e742006-10-17 00:10:03 -07002393 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2394 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002395
Michael Ellerman7fe37302007-04-18 19:39:21 +10002396 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002397}
2398
2399void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002400{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002401 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002402}
2403
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002404#endif /* CONFIG_PCI_MSI */
2405
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002406/*
2407 * Hypertransport interrupt support
2408 */
2409#ifdef CONFIG_HT_IRQ
2410
2411#ifdef CONFIG_SMP
2412
2413static void target_ht_irq(unsigned int irq, unsigned int dest)
2414{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002415 struct ht_irq_msg msg;
2416 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002417
Eric W. Biedermanec683072006-11-08 17:44:57 -08002418 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2419 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002420
Eric W. Biedermanec683072006-11-08 17:44:57 -08002421 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2422 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002423
Eric W. Biedermanec683072006-11-08 17:44:57 -08002424 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002425}
2426
2427static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2428{
2429 unsigned int dest;
2430 cpumask_t tmp;
Yinghai Lu08678b02008-08-19 20:50:05 -07002431 struct irq_desc *desc;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002432
2433 cpus_and(tmp, mask, cpu_online_map);
2434 if (cpus_empty(tmp))
2435 tmp = TARGET_CPUS;
2436
2437 cpus_and(mask, tmp, CPU_MASK_ALL);
2438
2439 dest = cpu_mask_to_apicid(mask);
2440
2441 target_ht_irq(irq, dest);
Yinghai Lu08678b02008-08-19 20:50:05 -07002442 desc = irq_to_desc(irq);
2443 desc->affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002444}
2445#endif
2446
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002447static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002448 .name = "PCI-HT",
2449 .mask = mask_ht_irq,
2450 .unmask = unmask_ht_irq,
2451 .ack = ack_ioapic_irq,
2452#ifdef CONFIG_SMP
2453 .set_affinity = set_ht_irq_affinity,
2454#endif
2455 .retrigger = ioapic_retrigger_irq,
2456};
2457
2458int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2459{
2460 int vector;
2461
2462 vector = assign_irq_vector(irq);
2463 if (vector >= 0) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002464 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002465 unsigned dest;
2466 cpumask_t tmp;
2467
2468 cpus_clear(tmp);
2469 cpu_set(vector >> 8, tmp);
2470 dest = cpu_mask_to_apicid(tmp);
2471
Eric W. Biedermanec683072006-11-08 17:44:57 -08002472 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002473
Eric W. Biedermanec683072006-11-08 17:44:57 -08002474 msg.address_lo =
2475 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002476 HT_IRQ_LOW_DEST_ID(dest) |
2477 HT_IRQ_LOW_VECTOR(vector) |
2478 ((INT_DEST_MODE == 0) ?
2479 HT_IRQ_LOW_DM_PHYSICAL :
2480 HT_IRQ_LOW_DM_LOGICAL) |
2481 HT_IRQ_LOW_RQEOI_EDGE |
2482 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2483 HT_IRQ_LOW_MT_FIXED :
2484 HT_IRQ_LOW_MT_ARBITRATED) |
2485 HT_IRQ_LOW_IRQ_MASKED;
2486
Eric W. Biedermanec683072006-11-08 17:44:57 -08002487 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002488
Ingo Molnara460e742006-10-17 00:10:03 -07002489 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2490 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002491 }
2492 return vector;
2493}
2494#endif /* CONFIG_HT_IRQ */
2495
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496/* --------------------------------------------------------------------------
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002497 ACPI-based IOAPIC Configuration
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 -------------------------------------------------------------------------- */
2499
Len Brown888ba6c2005-08-24 12:07:20 -04002500#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002502int __init io_apic_get_unique_id(int ioapic, int apic_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 union IO_APIC_reg_00 reg_00;
2505 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2506 physid_mask_t tmp;
2507 unsigned long flags;
2508 int i = 0;
2509
2510 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002511 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2512 * buses (one for LAPICs, one for IOAPICs), where predecessors only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 * supports up to 16 on one shared APIC bus.
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002514 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2516 * advantage of new APIC bus architecture.
2517 */
2518
2519 if (physids_empty(apic_id_map))
2520 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2521
2522 spin_lock_irqsave(&ioapic_lock, flags);
2523 reg_00.raw = io_apic_read(ioapic, 0);
2524 spin_unlock_irqrestore(&ioapic_lock, flags);
2525
2526 if (apic_id >= get_physical_broadcast()) {
2527 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2528 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2529 apic_id = reg_00.bits.ID;
2530 }
2531
2532 /*
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002533 * Every APIC in a system must have a unique ID or we get lots of nice
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 * 'stuck on smp_invalidate_needed IPI wait' messages.
2535 */
2536 if (check_apicid_used(apic_id_map, apic_id)) {
2537
2538 for (i = 0; i < get_physical_broadcast(); i++) {
2539 if (!check_apicid_used(apic_id_map, i))
2540 break;
2541 }
2542
2543 if (i == get_physical_broadcast())
2544 panic("Max apic_id exceeded!\n");
2545
2546 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2547 "trying %d\n", ioapic, apic_id, i);
2548
2549 apic_id = i;
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 tmp = apicid_to_cpu_present(apic_id);
2553 physids_or(apic_id_map, apic_id_map, tmp);
2554
2555 if (reg_00.bits.ID != apic_id) {
2556 reg_00.bits.ID = apic_id;
2557
2558 spin_lock_irqsave(&ioapic_lock, flags);
2559 io_apic_write(ioapic, 0, reg_00.raw);
2560 reg_00.raw = io_apic_read(ioapic, 0);
2561 spin_unlock_irqrestore(&ioapic_lock, flags);
2562
2563 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002564 if (reg_00.bits.ID != apic_id) {
2565 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2566 return -1;
2567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
2569
2570 apic_printk(APIC_VERBOSE, KERN_INFO
2571 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2572
2573 return apic_id;
2574}
2575
2576
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002577int __init io_apic_get_version(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 union IO_APIC_reg_01 reg_01;
2580 unsigned long flags;
2581
2582 spin_lock_irqsave(&ioapic_lock, flags);
2583 reg_01.raw = io_apic_read(ioapic, 1);
2584 spin_unlock_irqrestore(&ioapic_lock, flags);
2585
2586 return reg_01.bits.version;
2587}
2588
2589
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002590int __init io_apic_get_redir_entries(int ioapic)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
2592 union IO_APIC_reg_01 reg_01;
2593 unsigned long flags;
2594
2595 spin_lock_irqsave(&ioapic_lock, flags);
2596 reg_01.raw = io_apic_read(ioapic, 1);
2597 spin_unlock_irqrestore(&ioapic_lock, flags);
2598
2599 return reg_01.bits.entries;
2600}
2601
2602
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002603int io_apic_set_pci_routing(int ioapic, int pin, int irq, int edge_level, int active_high_low)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
2605 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
2607 if (!IO_APIC_IRQ(irq)) {
2608 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2609 ioapic);
2610 return -EINVAL;
2611 }
2612
2613 /*
2614 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2615 * Note that we mask (disable) IRQs now -- these get enabled when the
2616 * corresponding device driver registers for this IRQ.
2617 */
2618
Paolo Ciarrocchi36062442008-06-08 13:07:18 +02002619 memset(&entry, 0, sizeof(entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621 entry.delivery_mode = INT_DELIVERY_MODE;
2622 entry.dest_mode = INT_DEST_MODE;
2623 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2624 entry.trigger = edge_level;
2625 entry.polarity = active_high_low;
2626 entry.mask = 1;
2627
2628 /*
2629 * IRQs < 16 are already in the irq_2_pin[] map
2630 */
2631 if (irq >= 16)
2632 add_pin_to_irq(irq, ioapic, pin);
2633
2634 entry.vector = assign_irq_vector(irq);
2635
2636 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2637 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04002638 mp_ioapics[ioapic].mp_apicid, pin, entry.vector, irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 edge_level, active_high_low);
2640
2641 ioapic_register_intr(irq, entry.vector, edge_level);
2642
2643 if (!ioapic && (irq < 16))
2644 disable_8259A_irq(irq);
2645
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09002646 ioapic_write_entry(ioapic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
2648 return 0;
2649}
2650
Shaohua Li61fd47e2007-11-17 01:05:28 -05002651int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2652{
2653 int i;
2654
2655 if (skip_ioapic_setup)
2656 return -1;
2657
2658 for (i = 0; i < mp_irq_entries; i++)
Alexey Starikovskiy2fddb6e282008-05-14 19:03:17 +04002659 if (mp_irqs[i].mp_irqtype == mp_INT &&
2660 mp_irqs[i].mp_srcbusirq == bus_irq)
Shaohua Li61fd47e2007-11-17 01:05:28 -05002661 break;
2662 if (i >= mp_irq_entries)
2663 return -1;
2664
2665 *trigger = irq_trigger(i);
2666 *polarity = irq_polarity(i);
2667 return 0;
2668}
2669
Len Brown888ba6c2005-08-24 12:07:20 -04002670#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002671
2672static int __init parse_disable_timer_pin_1(char *arg)
2673{
2674 disable_timer_pin_1 = 1;
2675 return 0;
2676}
2677early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2678
2679static int __init parse_enable_timer_pin_1(char *arg)
2680{
2681 disable_timer_pin_1 = -1;
2682 return 0;
2683}
2684early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2685
2686static int __init parse_noapic(char *arg)
2687{
2688 /* disable IO-APIC */
2689 disable_ioapic_setup();
2690 return 0;
2691}
2692early_param("noapic", parse_noapic);
Yinghai Luf3294a32008-06-27 01:41:56 -07002693
2694void __init ioapic_init_mappings(void)
2695{
2696 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
2697 int i;
2698
2699 for (i = 0; i < nr_ioapics; i++) {
2700 if (smp_found_config) {
2701 ioapic_phys = mp_ioapics[i].mp_apicaddr;
2702 if (!ioapic_phys) {
2703 printk(KERN_ERR
2704 "WARNING: bogus zero IO-APIC "
2705 "address found in MPTABLE, "
2706 "disabling IO/APIC support!\n");
2707 smp_found_config = 0;
2708 skip_ioapic_setup = 1;
2709 goto fake_ioapic_page;
2710 }
2711 } else {
2712fake_ioapic_page:
2713 ioapic_phys = (unsigned long)
2714 alloc_bootmem_pages(PAGE_SIZE);
2715 ioapic_phys = __pa(ioapic_phys);
2716 }
2717 set_fixmap_nocache(idx, ioapic_phys);
2718 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
2719 __fix_to_virt(idx), ioapic_phys);
2720 idx++;
2721 }
2722}
2723