blob: a69c38b626290cb44b198aede0e6ac5c84b34364 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/smp_lock.h>
Eric W. Biederman589e3672006-10-04 02:16:42 -070029#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/mc146818rtc.h>
31#include <linux/acpi.h>
32#include <linux/sysdev.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070033#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070034#include <linux/htirq.h>
Andi Kleenab688052006-02-16 23:42:04 +010035#ifdef CONFIG_ACPI
36#include <acpi/acpi_bus.h>
37#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/io.h>
40#include <asm/smp.h>
41#include <asm/desc.h>
42#include <asm/proto.h>
43#include <asm/mach_apic.h>
Andi Kleen8d916402005-05-31 14:39:26 -070044#include <asm/acpi.h>
Andi Kleenca8642f2006-01-11 22:44:27 +010045#include <asm/dma.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020046#include <asm/nmi.h>
Eric W. Biederman589e3672006-10-04 02:16:42 -070047#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070048#include <asm/hypertransport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Eric W. Biedermanc7111c132006-10-08 07:47:55 -060050static int assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result);
Eric W. Biederman04b92672006-10-04 02:16:46 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define __apicdebuginit __init
53
54int sis_apic_bug; /* not actually supported, dummy for compile */
55
Andi Kleen14d98ca2005-05-20 14:27:59 -070056static int no_timer_check;
57
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -080058static int disable_timer_pin_1 __initdata;
59
60int timer_over_8254 __initdata = 1;
61
Eric W. Biederman1008fdd2006-01-11 22:46:06 +010062/* Where if anywhere is the i8259 connect in external int mode */
63static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_SPINLOCK(ioapic_lock);
Eric W. Biederman70a0a532006-10-25 01:00:23 +020066DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68/*
69 * # of IRQ routing registers
70 */
71int nr_ioapic_registers[MAX_IO_APICS];
72
73/*
74 * Rough estimation of how many shared IRQs there are, can
75 * be changed anytime.
76 */
James Cleverdon6004e1b2005-11-05 17:25:53 +010077#define MAX_PLUS_SHARED_IRQS NR_IRQ_VECTORS
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
79
80/*
81 * This is performance-critical, we want to do it O(1)
82 *
83 * the indexing order of this array favors 1:1 mappings
84 * between pins and IRQs.
85 */
86
87static struct irq_pin_list {
88 short apic, pin, next;
89} irq_2_pin[PIN_MAP_SIZE];
90
Linus Torvalds6c0ffb92006-11-08 10:23:03 -080091struct io_apic {
92 unsigned int index;
93 unsigned int unused[3];
94 unsigned int data;
95};
96
97static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
98{
99 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
100 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
101}
102
103static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
104{
105 struct io_apic __iomem *io_apic = io_apic_base(apic);
106 writel(reg, &io_apic->index);
107 return readl(&io_apic->data);
108}
109
110static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
111{
112 struct io_apic __iomem *io_apic = io_apic_base(apic);
113 writel(reg, &io_apic->index);
114 writel(value, &io_apic->data);
115}
116
117/*
118 * Re-write a value: to be used for read-modify-write
119 * cycles where the read already set up the index register.
120 */
121static inline void io_apic_modify(unsigned int apic, unsigned int value)
122{
123 struct io_apic __iomem *io_apic = io_apic_base(apic);
124 writel(value, &io_apic->data);
125}
126
127/*
128 * Synchronize the IO-APIC and the CPU by doing
129 * a dummy read from the IO-APIC
130 */
131static inline void io_apic_sync(unsigned int apic)
132{
133 struct io_apic __iomem *io_apic = io_apic_base(apic);
134 readl(&io_apic->data);
135}
136
Ashok Raj54d5d422005-09-06 15:16:15 -0700137#define __DO_ACTION(R, ACTION, FINAL) \
138 \
139{ \
140 int pin; \
141 struct irq_pin_list *entry = irq_2_pin + irq; \
142 \
James Cleverdon6004e1b2005-11-05 17:25:53 +0100143 BUG_ON(irq >= NR_IRQS); \
Ashok Raj54d5d422005-09-06 15:16:15 -0700144 for (;;) { \
145 unsigned int reg; \
146 pin = entry->pin; \
147 if (pin == -1) \
148 break; \
149 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
150 reg ACTION; \
151 io_apic_modify(entry->apic, reg); \
152 if (!entry->next) \
153 break; \
154 entry = irq_2_pin + entry->next; \
155 } \
156 FINAL; \
157}
158
Andi Kleeneea0e112006-09-26 10:52:30 +0200159union entry_union {
160 struct { u32 w1, w2; };
161 struct IO_APIC_route_entry entry;
162};
163
164static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
165{
166 union entry_union eu;
167 unsigned long flags;
168 spin_lock_irqsave(&ioapic_lock, flags);
169 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
170 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
171 spin_unlock_irqrestore(&ioapic_lock, flags);
172 return eu.entry;
173}
174
Linus Torvalds48797eb2006-11-08 10:27:54 -0800175/*
176 * When we write a new IO APIC routing entry, we need to write the high
177 * word first! If the mask bit in the low word is clear, we will enable
178 * the interrupt, and we need to make sure the entry is fully populated
179 * before that happens.
180 */
Andi Kleen516d2832006-12-07 02:14:07 +0100181static void
182__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
183{
184 union entry_union eu;
185 eu.entry = e;
186 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
187 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
188}
189
Andi Kleeneea0e112006-09-26 10:52:30 +0200190static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
191{
192 unsigned long flags;
Andi Kleeneea0e112006-09-26 10:52:30 +0200193 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleen516d2832006-12-07 02:14:07 +0100194 __ioapic_write_entry(apic, pin, e);
Linus Torvalds48797eb2006-11-08 10:27:54 -0800195 spin_unlock_irqrestore(&ioapic_lock, flags);
196}
197
198/*
199 * When we mask an IO APIC routing entry, we need to write the low
200 * word first, in order to set the mask bit before we change the
201 * high bits!
202 */
203static void ioapic_mask_entry(int apic, int pin)
204{
205 unsigned long flags;
206 union entry_union eu = { .entry.mask = 1 };
207
208 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleeneea0e112006-09-26 10:52:30 +0200209 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
210 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
211 spin_unlock_irqrestore(&ioapic_lock, flags);
212}
213
Ashok Raj54d5d422005-09-06 15:16:15 -0700214#ifdef CONFIG_SMP
Eric W. Biederman550f2292006-10-04 02:16:51 -0700215static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
216{
217 int apic, pin;
218 struct irq_pin_list *entry = irq_2_pin + irq;
219
220 BUG_ON(irq >= NR_IRQS);
221 for (;;) {
222 unsigned int reg;
223 apic = entry->apic;
224 pin = entry->pin;
225 if (pin == -1)
226 break;
227 io_apic_write(apic, 0x11 + pin*2, dest);
228 reg = io_apic_read(apic, 0x10 + pin*2);
229 reg &= ~0x000000ff;
230 reg |= vector;
231 io_apic_modify(apic, reg);
232 if (!entry->next)
233 break;
234 entry = irq_2_pin + entry->next;
235 }
236}
237
Ashok Raj54d5d422005-09-06 15:16:15 -0700238static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
239{
240 unsigned long flags;
241 unsigned int dest;
242 cpumask_t tmp;
Eric W. Biederman550f2292006-10-04 02:16:51 -0700243 int vector;
Ashok Raj54d5d422005-09-06 15:16:15 -0700244
245 cpus_and(tmp, mask, cpu_online_map);
246 if (cpus_empty(tmp))
247 tmp = TARGET_CPUS;
248
249 cpus_and(mask, tmp, CPU_MASK_ALL);
250
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600251 vector = assign_irq_vector(irq, mask, &tmp);
Eric W. Biederman550f2292006-10-04 02:16:51 -0700252 if (vector < 0)
253 return;
254
Eric W. Biederman550f2292006-10-04 02:16:51 -0700255 dest = cpu_mask_to_apicid(tmp);
Ashok Raj54d5d422005-09-06 15:16:15 -0700256
257 /*
258 * Only the high 8 bits are valid.
259 */
260 dest = SET_APIC_LOGICAL_ID(dest);
261
262 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600263 __target_IO_APIC_irq(irq, dest, vector);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -0700264 irq_desc[irq].affinity = mask;
Ashok Raj54d5d422005-09-06 15:16:15 -0700265 spin_unlock_irqrestore(&ioapic_lock, flags);
266}
267#endif
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/*
270 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
271 * shared ISA-space IRQs, so we have to support them. We are super
272 * fast in the common case, and fast for shared ISA-space IRQs.
273 */
274static void add_pin_to_irq(unsigned int irq, int apic, int pin)
275{
276 static int first_free_entry = NR_IRQS;
277 struct irq_pin_list *entry = irq_2_pin + irq;
278
James Cleverdon6004e1b2005-11-05 17:25:53 +0100279 BUG_ON(irq >= NR_IRQS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 while (entry->next)
281 entry = irq_2_pin + entry->next;
282
283 if (entry->pin != -1) {
284 entry->next = first_free_entry;
285 entry = irq_2_pin + entry->next;
286 if (++first_free_entry >= PIN_MAP_SIZE)
James Cleverdon6004e1b2005-11-05 17:25:53 +0100287 panic("io_apic.c: ran out of irq_2_pin entries!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289 entry->apic = apic;
290 entry->pin = pin;
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294#define DO_ACTION(name,R,ACTION, FINAL) \
295 \
296 static void name##_IO_APIC_irq (unsigned int irq) \
297 __DO_ACTION(R, ACTION, FINAL)
298
299DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
300 /* mask = 1 */
301DO_ACTION( __unmask, 0, &= 0xfffeffff, )
302 /* mask = 0 */
303
304static void mask_IO_APIC_irq (unsigned int irq)
305{
306 unsigned long flags;
307
308 spin_lock_irqsave(&ioapic_lock, flags);
309 __mask_IO_APIC_irq(irq);
310 spin_unlock_irqrestore(&ioapic_lock, flags);
311}
312
313static void unmask_IO_APIC_irq (unsigned int irq)
314{
315 unsigned long flags;
316
317 spin_lock_irqsave(&ioapic_lock, flags);
318 __unmask_IO_APIC_irq(irq);
319 spin_unlock_irqrestore(&ioapic_lock, flags);
320}
321
322static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
323{
324 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleeneea0e112006-09-26 10:52:30 +0200327 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (entry.delivery_mode == dest_SMI)
329 return;
330 /*
331 * Disable it in the IO-APIC irq-routing table:
332 */
Linus Torvalds48797eb2006-11-08 10:27:54 -0800333 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
336static void clear_IO_APIC (void)
337{
338 int apic, pin;
339
340 for (apic = 0; apic < nr_ioapics; apic++)
341 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
342 clear_IO_APIC_pin(apic, pin);
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345int skip_ioapic_setup;
346int ioapic_force;
347
348/* dummy parsing: see setup.c */
349
350static int __init disable_ioapic_setup(char *str)
351{
352 skip_ioapic_setup = 1;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200355early_param("noapic", disable_ioapic_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -0800357/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
358static int __init disable_timer_pin_setup(char *arg)
359{
360 disable_timer_pin_1 = 1;
361 return 1;
362}
363__setup("disable_timer_pin_1", disable_timer_pin_setup);
364
365static int __init setup_disable_8254_timer(char *s)
366{
367 timer_over_8254 = -1;
368 return 1;
369}
370static int __init setup_enable_8254_timer(char *s)
371{
372 timer_over_8254 = 2;
373 return 1;
374}
375
376__setup("disable_8254_timer", setup_disable_8254_timer);
377__setup("enable_8254_timer", setup_enable_8254_timer);
378
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
381 * Find the IRQ entry number of a certain pin.
382 */
383static int find_irq_entry(int apic, int pin, int type)
384{
385 int i;
386
387 for (i = 0; i < mp_irq_entries; i++)
388 if (mp_irqs[i].mpc_irqtype == type &&
389 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
390 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
391 mp_irqs[i].mpc_dstirq == pin)
392 return i;
393
394 return -1;
395}
396
397/*
398 * Find the pin to which IRQ[irq] (ISA) is connected
399 */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +0100400static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 int i;
403
404 for (i = 0; i < mp_irq_entries; i++) {
405 int lbus = mp_irqs[i].mpc_srcbus;
406
Andi Kleen55f05ff2006-09-26 10:52:30 +0200407 if (test_bit(lbus, mp_bus_not_pci) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 (mp_irqs[i].mpc_irqtype == type) &&
409 (mp_irqs[i].mpc_srcbusirq == irq))
410
411 return mp_irqs[i].mpc_dstirq;
412 }
413 return -1;
414}
415
Eric W. Biederman1008fdd2006-01-11 22:46:06 +0100416static int __init find_isa_irq_apic(int irq, int type)
417{
418 int i;
419
420 for (i = 0; i < mp_irq_entries; i++) {
421 int lbus = mp_irqs[i].mpc_srcbus;
422
Andi Kleen55f05ff2006-09-26 10:52:30 +0200423 if (test_bit(lbus, mp_bus_not_pci) &&
Eric W. Biederman1008fdd2006-01-11 22:46:06 +0100424 (mp_irqs[i].mpc_irqtype == type) &&
425 (mp_irqs[i].mpc_srcbusirq == irq))
426 break;
427 }
428 if (i < mp_irq_entries) {
429 int apic;
430 for(apic = 0; apic < nr_ioapics; apic++) {
431 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
432 return apic;
433 }
434 }
435
436 return -1;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*
440 * Find a specific PCI IRQ entry.
441 * Not an __init, possibly needed by modules
442 */
443static int pin_2_irq(int idx, int apic, int pin);
444
445int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
446{
447 int apic, i, best_guess = -1;
448
449 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
450 bus, slot, pin);
451 if (mp_bus_id_to_pci_bus[bus] == -1) {
452 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
453 return -1;
454 }
455 for (i = 0; i < mp_irq_entries; i++) {
456 int lbus = mp_irqs[i].mpc_srcbus;
457
458 for (apic = 0; apic < nr_ioapics; apic++)
459 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
460 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
461 break;
462
Andi Kleen55f05ff2006-09-26 10:52:30 +0200463 if (!test_bit(lbus, mp_bus_not_pci) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 !mp_irqs[i].mpc_irqtype &&
465 (bus == lbus) &&
466 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
467 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
468
469 if (!(apic || IO_APIC_IRQ(irq)))
470 continue;
471
472 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
473 return irq;
474 /*
475 * Use the first all-but-pin matching entry as a
476 * best-guess fuzzy result for broken mptables.
477 */
478 if (best_guess < 0)
479 best_guess = irq;
480 }
481 }
James Cleverdon6004e1b2005-11-05 17:25:53 +0100482 BUG_ON(best_guess >= NR_IRQS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return best_guess;
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/* ISA interrupts are always polarity zero edge triggered,
487 * when listed as conforming in the MP table. */
488
489#define default_ISA_trigger(idx) (0)
490#define default_ISA_polarity(idx) (0)
491
492/* PCI interrupts are always polarity one level triggered,
493 * when listed as conforming in the MP table. */
494
495#define default_PCI_trigger(idx) (1)
496#define default_PCI_polarity(idx) (1)
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498static int __init MPBIOS_polarity(int idx)
499{
500 int bus = mp_irqs[idx].mpc_srcbus;
501 int polarity;
502
503 /*
504 * Determine IRQ line polarity (high active or low active):
505 */
506 switch (mp_irqs[idx].mpc_irqflag & 3)
507 {
508 case 0: /* conforms, ie. bus-type dependent polarity */
Andi Kleen55f05ff2006-09-26 10:52:30 +0200509 if (test_bit(bus, mp_bus_not_pci))
510 polarity = default_ISA_polarity(idx);
511 else
512 polarity = default_PCI_polarity(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 case 1: /* high active */
515 {
516 polarity = 0;
517 break;
518 }
519 case 2: /* reserved */
520 {
521 printk(KERN_WARNING "broken BIOS!!\n");
522 polarity = 1;
523 break;
524 }
525 case 3: /* low active */
526 {
527 polarity = 1;
528 break;
529 }
530 default: /* invalid */
531 {
532 printk(KERN_WARNING "broken BIOS!!\n");
533 polarity = 1;
534 break;
535 }
536 }
537 return polarity;
538}
539
540static int MPBIOS_trigger(int idx)
541{
542 int bus = mp_irqs[idx].mpc_srcbus;
543 int trigger;
544
545 /*
546 * Determine IRQ trigger mode (edge or level sensitive):
547 */
548 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
549 {
550 case 0: /* conforms, ie. bus-type dependent */
Andi Kleen55f05ff2006-09-26 10:52:30 +0200551 if (test_bit(bus, mp_bus_not_pci))
552 trigger = default_ISA_trigger(idx);
553 else
554 trigger = default_PCI_trigger(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 case 1: /* edge */
557 {
558 trigger = 0;
559 break;
560 }
561 case 2: /* reserved */
562 {
563 printk(KERN_WARNING "broken BIOS!!\n");
564 trigger = 1;
565 break;
566 }
567 case 3: /* level */
568 {
569 trigger = 1;
570 break;
571 }
572 default: /* invalid */
573 {
574 printk(KERN_WARNING "broken BIOS!!\n");
575 trigger = 0;
576 break;
577 }
578 }
579 return trigger;
580}
581
582static inline int irq_polarity(int idx)
583{
584 return MPBIOS_polarity(idx);
585}
586
587static inline int irq_trigger(int idx)
588{
589 return MPBIOS_trigger(idx);
590}
591
592static int pin_2_irq(int idx, int apic, int pin)
593{
594 int irq, i;
595 int bus = mp_irqs[idx].mpc_srcbus;
596
597 /*
598 * Debugging check, we are in big trouble if this message pops up!
599 */
600 if (mp_irqs[idx].mpc_dstirq != pin)
601 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
602
Andi Kleen55f05ff2006-09-26 10:52:30 +0200603 if (test_bit(bus, mp_bus_not_pci)) {
604 irq = mp_irqs[idx].mpc_srcbusirq;
605 } else {
606 /*
607 * PCI IRQs are mapped in order
608 */
609 i = irq = 0;
610 while (i < apic)
611 irq += nr_ioapic_registers[i++];
612 irq += pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
James Cleverdon6004e1b2005-11-05 17:25:53 +0100614 BUG_ON(irq >= NR_IRQS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 return irq;
616}
617
618static inline int IO_APIC_irq_trigger(int irq)
619{
620 int apic, idx, pin;
621
622 for (apic = 0; apic < nr_ioapics; apic++) {
623 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
624 idx = find_irq_entry(apic,pin,mp_INT);
625 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
626 return irq_trigger(idx);
627 }
628 }
629 /*
630 * nonexistent IRQs are edge default
631 */
632 return 0;
633}
634
635/* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600636static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = {
637 [0] = FIRST_EXTERNAL_VECTOR + 0,
638 [1] = FIRST_EXTERNAL_VECTOR + 1,
639 [2] = FIRST_EXTERNAL_VECTOR + 2,
640 [3] = FIRST_EXTERNAL_VECTOR + 3,
641 [4] = FIRST_EXTERNAL_VECTOR + 4,
642 [5] = FIRST_EXTERNAL_VECTOR + 5,
643 [6] = FIRST_EXTERNAL_VECTOR + 6,
644 [7] = FIRST_EXTERNAL_VECTOR + 7,
645 [8] = FIRST_EXTERNAL_VECTOR + 8,
646 [9] = FIRST_EXTERNAL_VECTOR + 9,
647 [10] = FIRST_EXTERNAL_VECTOR + 10,
648 [11] = FIRST_EXTERNAL_VECTOR + 11,
649 [12] = FIRST_EXTERNAL_VECTOR + 12,
650 [13] = FIRST_EXTERNAL_VECTOR + 13,
651 [14] = FIRST_EXTERNAL_VECTOR + 14,
652 [15] = FIRST_EXTERNAL_VECTOR + 15,
653};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600655static cpumask_t irq_domain[NR_IRQ_VECTORS] __read_mostly = {
656 [0] = CPU_MASK_ALL,
657 [1] = CPU_MASK_ALL,
658 [2] = CPU_MASK_ALL,
659 [3] = CPU_MASK_ALL,
660 [4] = CPU_MASK_ALL,
661 [5] = CPU_MASK_ALL,
662 [6] = CPU_MASK_ALL,
663 [7] = CPU_MASK_ALL,
664 [8] = CPU_MASK_ALL,
665 [9] = CPU_MASK_ALL,
666 [10] = CPU_MASK_ALL,
667 [11] = CPU_MASK_ALL,
668 [12] = CPU_MASK_ALL,
669 [13] = CPU_MASK_ALL,
670 [14] = CPU_MASK_ALL,
671 [15] = CPU_MASK_ALL,
672};
673
674static int __assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Eric W. Biederman550f2292006-10-04 02:16:51 -0700676 /*
677 * NOTE! The local APIC isn't very good at handling
678 * multiple interrupts at the same interrupt level.
679 * As the interrupt level is determined by taking the
680 * vector number and shifting that right by 4, we
681 * want to spread these out a bit so that they don't
682 * all fall in the same interrupt level.
683 *
684 * Also, we've got to be careful not to trash gate
685 * 0x80, because int 0x80 is hm, kind of importantish. ;)
686 */
Eric W. Biedermand1752aa2006-10-25 01:00:22 +0200687 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
Eric W. Biedermanfc5d56f2007-02-23 04:11:56 -0700688 cpumask_t old_mask = CPU_MASK_NONE;
Eric W. Biederman550f2292006-10-04 02:16:51 -0700689 int old_vector = -1;
690 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Eric W. Biederman04b92672006-10-04 02:16:46 -0700692 BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
Jan Beulich0a1ad602006-06-26 13:56:43 +0200693
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200694 /* Only try and allocate irqs on cpus that are present */
695 cpus_and(mask, mask, cpu_online_map);
696
Eric W. Biedermanb940d222006-10-08 07:43:46 -0600697 if (irq_vector[irq] > 0)
698 old_vector = irq_vector[irq];
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600699 if (old_vector > 0) {
700 cpus_and(*result, irq_domain[irq], mask);
701 if (!cpus_empty(*result))
702 return old_vector;
Eric W. Biedermanfc5d56f2007-02-23 04:11:56 -0700703 cpus_and(old_mask, irq_domain[irq], cpu_online_map);
Jan Beulich0a1ad602006-06-26 13:56:43 +0200704 }
Eric W. Biederman550f2292006-10-04 02:16:51 -0700705
706 for_each_cpu_mask(cpu, mask) {
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200707 cpumask_t domain, new_mask;
Eric W. Biedermanfc5d56f2007-02-23 04:11:56 -0700708 int new_cpu, old_cpu;
Eric W. Biederman550f2292006-10-04 02:16:51 -0700709 int vector, offset;
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600710
711 domain = vector_allocation_domain(cpu);
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200712 cpus_and(new_mask, domain, cpu_online_map);
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600713
Eric W. Biedermand1752aa2006-10-25 01:00:22 +0200714 vector = current_vector;
715 offset = current_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716next:
Eric W. Biederman550f2292006-10-04 02:16:51 -0700717 vector += 8;
718 if (vector >= FIRST_SYSTEM_VECTOR) {
719 /* If we run out of vectors on large boxen, must share them. */
720 offset = (offset + 1) % 8;
721 vector = FIRST_DEVICE_VECTOR + offset;
722 }
Eric W. Biedermand1752aa2006-10-25 01:00:22 +0200723 if (unlikely(current_vector == vector))
Eric W. Biederman550f2292006-10-04 02:16:51 -0700724 continue;
725 if (vector == IA32_SYSCALL_VECTOR)
726 goto next;
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200727 for_each_cpu_mask(new_cpu, new_mask)
Yinghai Lu45edfd12006-10-21 18:37:01 +0200728 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600729 goto next;
Eric W. Biederman550f2292006-10-04 02:16:51 -0700730 /* Found one! */
Eric W. Biedermand1752aa2006-10-25 01:00:22 +0200731 current_vector = vector;
732 current_offset = offset;
Eric W. Biedermanfc5d56f2007-02-23 04:11:56 -0700733 for_each_cpu_mask(old_cpu, old_mask)
734 per_cpu(vector_irq, old_cpu)[old_vector] = -1;
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200735 for_each_cpu_mask(new_cpu, new_mask)
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600736 per_cpu(vector_irq, new_cpu)[vector] = irq;
Eric W. Biedermanb940d222006-10-08 07:43:46 -0600737 irq_vector[irq] = vector;
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600738 irq_domain[irq] = domain;
739 cpus_and(*result, domain, mask);
Eric W. Biederman550f2292006-10-04 02:16:51 -0700740 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Eric W. Biederman550f2292006-10-04 02:16:51 -0700742 return -ENOSPC;
Eric W. Biederman04b92672006-10-04 02:16:46 -0700743}
744
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600745static int assign_irq_vector(int irq, cpumask_t mask, cpumask_t *result)
Eric W. Biederman04b92672006-10-04 02:16:46 -0700746{
747 int vector;
748 unsigned long flags;
749
750 spin_lock_irqsave(&vector_lock, flags);
Eric W. Biedermanc7111c132006-10-08 07:47:55 -0600751 vector = __assign_irq_vector(irq, mask, result);
Ingo Molnar26a3c492006-06-26 13:57:16 +0200752 spin_unlock_irqrestore(&vector_lock, flags);
Jan Beulich0a1ad602006-06-26 13:56:43 +0200753 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Yinghai Lu5df02872006-12-07 02:14:05 +0100756static void __clear_irq_vector(int irq)
757{
758 cpumask_t mask;
759 int cpu, vector;
760
761 BUG_ON(!irq_vector[irq]);
762
763 vector = irq_vector[irq];
764 cpus_and(mask, irq_domain[irq], cpu_online_map);
765 for_each_cpu_mask(cpu, mask)
766 per_cpu(vector_irq, cpu)[vector] = -1;
767
768 irq_vector[irq] = 0;
769 irq_domain[irq] = CPU_MASK_NONE;
770}
771
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200772void __setup_vector_irq(int cpu)
773{
774 /* Initialize vector_irq on a new cpu */
775 /* This function must be called with vector_lock held */
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200776 int irq, vector;
777
Eric W. Biederman70a0a532006-10-25 01:00:23 +0200778 /* Mark the inuse vectors */
779 for (irq = 0; irq < NR_IRQ_VECTORS; ++irq) {
780 if (!cpu_isset(cpu, irq_domain[irq]))
781 continue;
782 vector = irq_vector[irq];
783 per_cpu(vector_irq, cpu)[vector] = irq;
784 }
785 /* Mark the free vectors */
786 for (vector = 0; vector < NR_VECTORS; ++vector) {
787 irq = per_cpu(vector_irq, cpu)[vector];
788 if (irq < 0)
789 continue;
790 if (!cpu_isset(cpu, irq_domain[irq]))
791 per_cpu(vector_irq, cpu)[vector] = -1;
792 }
793}
794
795
Ingo Molnarf29bd1b2006-10-04 02:16:25 -0700796static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798#define IOAPIC_AUTO -1
799#define IOAPIC_EDGE 0
800#define IOAPIC_LEVEL 1
801
Eric W. Biedermana27bc062007-02-23 04:16:31 -0700802static void ioapic_register_intr(int irq, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Jan Beulich6ebcc002006-06-26 13:56:46 +0200804 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
805 trigger == IOAPIC_LEVEL)
Ingo Molnara460e742006-10-17 00:10:03 -0700806 set_irq_chip_and_handler_name(irq, &ioapic_chip,
807 handle_fasteoi_irq, "fasteoi");
Ingo Molnard7e25f32007-02-16 01:28:24 -0800808 else
Ingo Molnara460e742006-10-17 00:10:03 -0700809 set_irq_chip_and_handler_name(irq, &ioapic_chip,
810 handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811}
Yinghai Luad892f52006-12-07 02:14:19 +0100812static void __init setup_IO_APIC_irq(int apic, int pin, int idx, int irq)
813{
814 struct IO_APIC_route_entry entry;
815 int vector;
816 unsigned long flags;
817
818
819 /*
820 * add it to the IO-APIC irq-routing table:
821 */
822 memset(&entry,0,sizeof(entry));
823
824 entry.delivery_mode = INT_DELIVERY_MODE;
825 entry.dest_mode = INT_DEST_MODE;
826 entry.mask = 0; /* enable IRQ */
Benjamin Romeree4eff62007-02-13 13:26:25 +0100827 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Yinghai Luad892f52006-12-07 02:14:19 +0100828
829 entry.trigger = irq_trigger(idx);
830 entry.polarity = irq_polarity(idx);
831
832 if (irq_trigger(idx)) {
833 entry.trigger = 1;
834 entry.mask = 1;
Benjamin Romeree4eff62007-02-13 13:26:25 +0100835 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Yinghai Luad892f52006-12-07 02:14:19 +0100836 }
837
838 if (!apic && !IO_APIC_IRQ(irq))
839 return;
840
841 if (IO_APIC_IRQ(irq)) {
842 cpumask_t mask;
843 vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
844 if (vector < 0)
845 return;
846
Benjamin Romeree4eff62007-02-13 13:26:25 +0100847 entry.dest = cpu_mask_to_apicid(mask);
Yinghai Luad892f52006-12-07 02:14:19 +0100848 entry.vector = vector;
849
Eric W. Biedermana27bc062007-02-23 04:16:31 -0700850 ioapic_register_intr(irq, IOAPIC_AUTO);
Yinghai Luad892f52006-12-07 02:14:19 +0100851 if (!apic && (irq < 16))
852 disable_8259A_irq(irq);
853 }
854
855 ioapic_write_entry(apic, pin, entry);
856
857 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -0700858 irq_desc[irq].affinity = TARGET_CPUS;
Yinghai Luad892f52006-12-07 02:14:19 +0100859 spin_unlock_irqrestore(&ioapic_lock, flags);
860
861}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863static void __init setup_IO_APIC_irqs(void)
864{
Yinghai Luad892f52006-12-07 02:14:19 +0100865 int apic, pin, idx, irq, first_notcon = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
868
869 for (apic = 0; apic < nr_ioapics; apic++) {
870 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 idx = find_irq_entry(apic,pin,mp_INT);
873 if (idx == -1) {
874 if (first_notcon) {
875 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
876 first_notcon = 0;
877 } else
878 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
879 continue;
880 }
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 irq = pin_2_irq(idx, apic, pin);
883 add_pin_to_irq(irq, apic, pin);
884
Yinghai Luad892f52006-12-07 02:14:19 +0100885 setup_IO_APIC_irq(apic, pin, idx, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 }
888 }
889
890 if (!first_notcon)
891 apic_printk(APIC_VERBOSE," not connected.\n");
892}
893
894/*
895 * Set up the 8259A-master output pin as broadcast to all
896 * CPUs.
897 */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +0100898static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 struct IO_APIC_route_entry entry;
901 unsigned long flags;
902
903 memset(&entry,0,sizeof(entry));
904
905 disable_8259A_irq(0);
906
907 /* mask LVT0 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100908 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
910 /*
911 * We use logical delivery to get the timer IRQ
912 * to the first CPU.
913 */
914 entry.dest_mode = INT_DEST_MODE;
915 entry.mask = 0; /* unmask IRQ now */
Benjamin Romeree4eff62007-02-13 13:26:25 +0100916 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 entry.delivery_mode = INT_DELIVERY_MODE;
918 entry.polarity = 0;
919 entry.trigger = 0;
920 entry.vector = vector;
921
922 /*
923 * The timer IRQ doesn't have to know that behind the
924 * scene we have a 8259A-master in AEOI mode ...
925 */
Ingo Molnara460e742006-10-17 00:10:03 -0700926 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /*
929 * Add it to the IO-APIC irq-routing table:
930 */
931 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +0100932 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
933 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 spin_unlock_irqrestore(&ioapic_lock, flags);
935
936 enable_8259A_irq(0);
937}
938
939void __init UNEXPECTED_IO_APIC(void)
940{
941}
942
943void __apicdebuginit print_IO_APIC(void)
944{
945 int apic, i;
946 union IO_APIC_reg_00 reg_00;
947 union IO_APIC_reg_01 reg_01;
948 union IO_APIC_reg_02 reg_02;
949 unsigned long flags;
950
951 if (apic_verbosity == APIC_QUIET)
952 return;
953
954 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
955 for (i = 0; i < nr_ioapics; i++)
956 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
957 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
958
959 /*
960 * We are a bit conservative about what we expect. We have to
961 * know about every hardware change ASAP.
962 */
963 printk(KERN_INFO "testing the IO APIC.......................\n");
964
965 for (apic = 0; apic < nr_ioapics; apic++) {
966
967 spin_lock_irqsave(&ioapic_lock, flags);
968 reg_00.raw = io_apic_read(apic, 0);
969 reg_01.raw = io_apic_read(apic, 1);
970 if (reg_01.bits.version >= 0x10)
971 reg_02.raw = io_apic_read(apic, 2);
972 spin_unlock_irqrestore(&ioapic_lock, flags);
973
974 printk("\n");
975 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
976 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
977 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
978 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
979 UNEXPECTED_IO_APIC();
980
981 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
982 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
983 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
984 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
985 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
986 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
987 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
988 (reg_01.bits.entries != 0x2E) &&
989 (reg_01.bits.entries != 0x3F) &&
990 (reg_01.bits.entries != 0x03)
991 )
992 UNEXPECTED_IO_APIC();
993
994 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
995 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
996 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
997 (reg_01.bits.version != 0x02) && /* 82801BA IO-APICs (ICH2) */
998 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
999 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1000 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1001 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1002 )
1003 UNEXPECTED_IO_APIC();
1004 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1005 UNEXPECTED_IO_APIC();
1006
1007 if (reg_01.bits.version >= 0x10) {
1008 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1009 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1010 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1011 UNEXPECTED_IO_APIC();
1012 }
1013
1014 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1015
Benjamin Romeree4eff62007-02-13 13:26:25 +01001016 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1017 " Stat Dmod Deli Vect: \n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 for (i = 0; i <= reg_01.bits.entries; i++) {
1020 struct IO_APIC_route_entry entry;
1021
Andi Kleeneea0e112006-09-26 10:52:30 +02001022 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Benjamin Romeree4eff62007-02-13 13:26:25 +01001024 printk(KERN_DEBUG " %02x %03X ",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 i,
Benjamin Romeree4eff62007-02-13 13:26:25 +01001026 entry.dest
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 );
1028
1029 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1030 entry.mask,
1031 entry.trigger,
1032 entry.irr,
1033 entry.polarity,
1034 entry.delivery_status,
1035 entry.dest_mode,
1036 entry.delivery_mode,
1037 entry.vector
1038 );
1039 }
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1042 for (i = 0; i < NR_IRQS; i++) {
1043 struct irq_pin_list *entry = irq_2_pin + i;
1044 if (entry->pin < 0)
1045 continue;
Eric W. Biederman04b92672006-10-04 02:16:46 -07001046 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 for (;;) {
1048 printk("-> %d:%d", entry->apic, entry->pin);
1049 if (!entry->next)
1050 break;
1051 entry = irq_2_pin + entry->next;
1052 }
1053 printk("\n");
1054 }
1055
1056 printk(KERN_INFO ".................................... done.\n");
1057
1058 return;
1059}
1060
1061#if 0
1062
1063static __apicdebuginit void print_APIC_bitfield (int base)
1064{
1065 unsigned int v;
1066 int i, j;
1067
1068 if (apic_verbosity == APIC_QUIET)
1069 return;
1070
1071 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1072 for (i = 0; i < 8; i++) {
1073 v = apic_read(base + i*0x10);
1074 for (j = 0; j < 32; j++) {
1075 if (v & (1<<j))
1076 printk("1");
1077 else
1078 printk("0");
1079 }
1080 printk("\n");
1081 }
1082}
1083
1084void __apicdebuginit print_local_APIC(void * dummy)
1085{
1086 unsigned int v, ver, maxlvt;
1087
1088 if (apic_verbosity == APIC_QUIET)
1089 return;
1090
1091 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1092 smp_processor_id(), hard_smp_processor_id());
1093 v = apic_read(APIC_ID);
1094 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1095 v = apic_read(APIC_LVR);
1096 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1097 ver = GET_APIC_VERSION(v);
1098 maxlvt = get_maxlvt();
1099
1100 v = apic_read(APIC_TASKPRI);
1101 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1102
Andi Kleen5a40b7c2005-09-12 18:49:24 +02001103 v = apic_read(APIC_ARBPRI);
1104 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1105 v & APIC_ARBPRI_MASK);
1106 v = apic_read(APIC_PROCPRI);
1107 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 v = apic_read(APIC_EOI);
1110 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1111 v = apic_read(APIC_RRR);
1112 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1113 v = apic_read(APIC_LDR);
1114 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1115 v = apic_read(APIC_DFR);
1116 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1117 v = apic_read(APIC_SPIV);
1118 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1119
1120 printk(KERN_DEBUG "... APIC ISR field:\n");
1121 print_APIC_bitfield(APIC_ISR);
1122 printk(KERN_DEBUG "... APIC TMR field:\n");
1123 print_APIC_bitfield(APIC_TMR);
1124 printk(KERN_DEBUG "... APIC IRR field:\n");
1125 print_APIC_bitfield(APIC_IRR);
1126
Andi Kleen5a40b7c2005-09-12 18:49:24 +02001127 v = apic_read(APIC_ESR);
1128 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 v = apic_read(APIC_ICR);
1131 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1132 v = apic_read(APIC_ICR2);
1133 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1134
1135 v = apic_read(APIC_LVTT);
1136 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1137
1138 if (maxlvt > 3) { /* PC is LVT#4. */
1139 v = apic_read(APIC_LVTPC);
1140 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1141 }
1142 v = apic_read(APIC_LVT0);
1143 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1144 v = apic_read(APIC_LVT1);
1145 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1146
1147 if (maxlvt > 2) { /* ERR is LVT#3. */
1148 v = apic_read(APIC_LVTERR);
1149 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1150 }
1151
1152 v = apic_read(APIC_TMICT);
1153 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1154 v = apic_read(APIC_TMCCT);
1155 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1156 v = apic_read(APIC_TDCR);
1157 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1158 printk("\n");
1159}
1160
1161void print_all_local_APICs (void)
1162{
1163 on_each_cpu(print_local_APIC, NULL, 1, 1);
1164}
1165
1166void __apicdebuginit print_PIC(void)
1167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 unsigned int v;
1169 unsigned long flags;
1170
1171 if (apic_verbosity == APIC_QUIET)
1172 return;
1173
1174 printk(KERN_DEBUG "\nprinting PIC contents\n");
1175
1176 spin_lock_irqsave(&i8259A_lock, flags);
1177
1178 v = inb(0xa1) << 8 | inb(0x21);
1179 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1180
1181 v = inb(0xa0) << 8 | inb(0x20);
1182 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1183
1184 outb(0x0b,0xa0);
1185 outb(0x0b,0x20);
1186 v = inb(0xa0) << 8 | inb(0x20);
1187 outb(0x0a,0xa0);
1188 outb(0x0a,0x20);
1189
1190 spin_unlock_irqrestore(&i8259A_lock, flags);
1191
1192 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1193
1194 v = inb(0x4d1) << 8 | inb(0x4d0);
1195 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1196}
1197
1198#endif /* 0 */
1199
1200static void __init enable_IO_APIC(void)
1201{
1202 union IO_APIC_reg_01 reg_01;
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001203 int i8259_apic, i8259_pin;
1204 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 unsigned long flags;
1206
1207 for (i = 0; i < PIN_MAP_SIZE; i++) {
1208 irq_2_pin[i].pin = -1;
1209 irq_2_pin[i].next = 0;
1210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /*
1213 * The number of IO-APIC IRQ registers (== #pins):
1214 */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001215 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001217 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001219 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1220 }
1221 for(apic = 0; apic < nr_ioapics; apic++) {
1222 int pin;
1223 /* See if any of the pins is in ExtINT mode */
1224 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1225 struct IO_APIC_route_entry entry;
Andi Kleeneea0e112006-09-26 10:52:30 +02001226 entry = ioapic_read_entry(apic, pin);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001227
1228 /* If the interrupt line is enabled and in ExtInt mode
1229 * I have found the pin where the i8259 is connected.
1230 */
1231 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1232 ioapic_i8259.apic = apic;
1233 ioapic_i8259.pin = pin;
1234 goto found_i8259;
1235 }
1236 }
1237 }
1238 found_i8259:
1239 /* Look to see what if the MP table has reported the ExtINT */
1240 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1241 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1242 /* Trust the MP table if nothing is setup in the hardware */
1243 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1244 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1245 ioapic_i8259.pin = i8259_pin;
1246 ioapic_i8259.apic = i8259_apic;
1247 }
1248 /* Complain if the MP table and the hardware disagree */
1249 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1250 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1251 {
1252 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
1255 /*
1256 * Do not trust the IO-APIC being empty at bootup
1257 */
1258 clear_IO_APIC();
1259}
1260
1261/*
1262 * Not an __init, needed by the reboot code
1263 */
1264void disable_IO_APIC(void)
1265{
1266 /*
1267 * Clear the IO-APIC before rebooting:
1268 */
1269 clear_IO_APIC();
1270
Eric W. Biederman208fb932005-06-25 14:57:45 -07001271 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001272 * If the i8259 is routed through an IOAPIC
Eric W. Biederman208fb932005-06-25 14:57:45 -07001273 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001274 * so legacy interrupts can be delivered.
Eric W. Biederman208fb932005-06-25 14:57:45 -07001275 */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001276 if (ioapic_i8259.pin != -1) {
Eric W. Biederman208fb932005-06-25 14:57:45 -07001277 struct IO_APIC_route_entry entry;
Eric W. Biederman208fb932005-06-25 14:57:45 -07001278
1279 memset(&entry, 0, sizeof(entry));
1280 entry.mask = 0; /* Enabled */
1281 entry.trigger = 0; /* Edge */
1282 entry.irr = 0;
1283 entry.polarity = 0; /* High */
1284 entry.delivery_status = 0;
1285 entry.dest_mode = 0; /* Physical */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001286 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman208fb932005-06-25 14:57:45 -07001287 entry.vector = 0;
Benjamin Romeree4eff62007-02-13 13:26:25 +01001288 entry.dest = GET_APIC_ID(apic_read(APIC_ID));
Eric W. Biederman208fb932005-06-25 14:57:45 -07001289
Eric W. Biederman208fb932005-06-25 14:57:45 -07001290 /*
1291 * Add it to the IO-APIC irq-routing table:
1292 */
Andi Kleeneea0e112006-09-26 10:52:30 +02001293 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman208fb932005-06-25 14:57:45 -07001294 }
1295
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001296 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
1299/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 * There is a nasty bug in some older SMP boards, their mptable lies
1301 * about the timer IRQ. We do the following to work around the situation:
1302 *
1303 * - timer IRQ defaults to IO-APIC IRQ
1304 * - if this function detects that timer IRQs are defunct, then we fall
1305 * back to ISA timer IRQs
1306 */
1307static int __init timer_irq_works(void)
1308{
1309 unsigned long t1 = jiffies;
1310
1311 local_irq_enable();
1312 /* Let ten ticks pass... */
1313 mdelay((10 * 1000) / HZ);
1314
1315 /*
1316 * Expect a few ticks at least, to be sure some possible
1317 * glue logic does not lock up after one or two first
1318 * ticks in a non-ExtINT mode. Also the local APIC
1319 * might have cached one ExtINT interrupt. Finally, at
1320 * least one tick may be lost due to delays.
1321 */
1322
1323 /* jiffies wrap? */
1324 if (jiffies - t1 > 4)
1325 return 1;
1326 return 0;
1327}
1328
1329/*
1330 * In the SMP+IOAPIC case it might happen that there are an unspecified
1331 * number of pending IRQ events unhandled. These cases are very rare,
1332 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1333 * better to do it this way as thus we do not have to be aware of
1334 * 'pending' interrupts in the IRQ path, except at this point.
1335 */
1336/*
1337 * Edge triggered needs to resend any interrupt
1338 * that was delayed but this is now handled in the device
1339 * independent code.
1340 */
1341
1342/*
1343 * Starting up a edge-triggered IO-APIC interrupt is
1344 * nasty - we need to make sure that we get the edge.
1345 * If it is already asserted for some reason, we need
1346 * return 1 to indicate that is was pending.
1347 *
1348 * This is not complete - we should be able to fake
1349 * an edge even if it isn't on the 8259A...
1350 */
1351
Ingo Molnarf29bd1b2006-10-04 02:16:25 -07001352static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
1354 int was_pending = 0;
1355 unsigned long flags;
1356
1357 spin_lock_irqsave(&ioapic_lock, flags);
1358 if (irq < 16) {
1359 disable_8259A_irq(irq);
1360 if (i8259A_irq_pending(irq))
1361 was_pending = 1;
1362 }
1363 __unmask_IO_APIC_irq(irq);
1364 spin_unlock_irqrestore(&ioapic_lock, flags);
1365
1366 return was_pending;
1367}
1368
Eric W. Biederman04b92672006-10-04 02:16:46 -07001369static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Eric W. Biederman550f2292006-10-04 02:16:51 -07001371 cpumask_t mask;
1372 unsigned vector;
Eric W. Biederman6bf2daf2006-10-21 18:37:02 +02001373 unsigned long flags;
Eric W. Biederman550f2292006-10-04 02:16:51 -07001374
Eric W. Biederman6bf2daf2006-10-21 18:37:02 +02001375 spin_lock_irqsave(&vector_lock, flags);
Eric W. Biederman550f2292006-10-04 02:16:51 -07001376 vector = irq_vector[irq];
1377 cpus_clear(mask);
Eric W. Biederman6bf2daf2006-10-21 18:37:02 +02001378 cpu_set(first_cpu(irq_domain[irq]), mask);
Eric W. Biederman550f2292006-10-04 02:16:51 -07001379
Eric W. Biederman6bf2daf2006-10-21 18:37:02 +02001380 send_IPI_mask(mask, vector);
1381 spin_unlock_irqrestore(&vector_lock, flags);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07001382
1383 return 1;
1384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386/*
1387 * Level and edge triggered IO-APIC interrupts need different handling,
1388 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1389 * handled with the level-triggered descriptor, but that one has slightly
1390 * more overhead. Level-triggered interrupts cannot be handled with the
1391 * edge-triggered handler, without risking IRQ storms and other ugly
1392 * races.
1393 */
1394
Eric W. Biederman0be66522006-10-04 02:16:30 -07001395static void ack_apic_edge(unsigned int irq)
1396{
1397 move_native_irq(irq);
1398 ack_APIC_irq();
1399}
1400
1401static void ack_apic_level(unsigned int irq)
1402{
1403 int do_unmask_irq = 0;
1404
1405#if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
1406 /* If we are moving the irq we need to mask it */
1407 if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1408 do_unmask_irq = 1;
1409 mask_IO_APIC_irq(irq);
1410 }
1411#endif
1412
1413 /*
1414 * We must acknowledge the irq before we move it or the acknowledge will
1415 * not propogate properly.
1416 */
1417 ack_APIC_irq();
1418
1419 /* Now we can move and renable the irq */
1420 move_masked_irq(irq);
1421 if (unlikely(do_unmask_irq))
1422 unmask_IO_APIC_irq(irq);
1423}
1424
Ingo Molnarf29bd1b2006-10-04 02:16:25 -07001425static struct irq_chip ioapic_chip __read_mostly = {
1426 .name = "IO-APIC",
Eric W. Biederman04b92672006-10-04 02:16:46 -07001427 .startup = startup_ioapic_irq,
1428 .mask = mask_IO_APIC_irq,
1429 .unmask = unmask_IO_APIC_irq,
Eric W. Biederman0be66522006-10-04 02:16:30 -07001430 .ack = ack_apic_edge,
1431 .eoi = ack_apic_level,
Ashok Raj54d5d422005-09-06 15:16:15 -07001432#ifdef CONFIG_SMP
Eric W. Biederman04b92672006-10-04 02:16:46 -07001433 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001434#endif
Eric W. Biederman04b92672006-10-04 02:16:46 -07001435 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436};
1437
1438static inline void init_IO_APIC_traps(void)
1439{
1440 int irq;
1441
1442 /*
1443 * NOTE! The local APIC isn't very good at handling
1444 * multiple interrupts at the same interrupt level.
1445 * As the interrupt level is determined by taking the
1446 * vector number and shifting that right by 4, we
1447 * want to spread these out a bit so that they don't
1448 * all fall in the same interrupt level.
1449 *
1450 * Also, we've got to be careful not to trash gate
1451 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1452 */
1453 for (irq = 0; irq < NR_IRQS ; irq++) {
1454 int tmp = irq;
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001455 if (IO_APIC_IRQ(tmp) && !irq_vector[tmp]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /*
1457 * Hmm.. We don't have an entry for this,
1458 * so default to an old-fashioned 8259
1459 * interrupt if we can..
1460 */
1461 if (irq < 16)
1462 make_8259A_irq(irq);
1463 else
1464 /* Strange. Oh, well.. */
Ingo Molnarf29bd1b2006-10-04 02:16:25 -07001465 irq_desc[irq].chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 }
1468}
1469
1470static void enable_lapic_irq (unsigned int irq)
1471{
1472 unsigned long v;
1473
1474 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +01001475 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
1478static void disable_lapic_irq (unsigned int irq)
1479{
1480 unsigned long v;
1481
1482 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +01001483 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484}
1485
1486static void ack_lapic_irq (unsigned int irq)
1487{
1488 ack_APIC_irq();
1489}
1490
1491static void end_lapic_irq (unsigned int i) { /* nothing */ }
1492
Ravikiran G Thirumalai6c231b72005-09-06 15:17:45 -07001493static struct hw_interrupt_type lapic_irq_type __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 .typename = "local-APIC-edge",
1495 .startup = NULL, /* startup_irq() not used for IRQ0 */
1496 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1497 .enable = enable_lapic_irq,
1498 .disable = disable_lapic_irq,
1499 .ack = ack_lapic_irq,
1500 .end = end_lapic_irq,
1501};
1502
1503static void setup_nmi (void)
1504{
1505 /*
1506 * Dirty trick to enable the NMI watchdog ...
1507 * We put the 8259A master into AEOI mode and
1508 * unmask on all local APICs LVT0 as NMI.
1509 *
1510 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1511 * is from Maciej W. Rozycki - so we do not have to EOI from
1512 * the NMI handler or the timer interrupt.
1513 */
1514 printk(KERN_INFO "activating NMI Watchdog ...");
1515
1516 enable_NMI_through_LVT0(NULL);
1517
1518 printk(" done.\n");
1519}
1520
1521/*
1522 * This looks a bit hackish but it's about the only one way of sending
1523 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1524 * not support the ExtINT mode, unfortunately. We need to send these
1525 * cycles as some i82489DX-based boards have glue logic that keeps the
1526 * 8259A interrupt line asserted until INTA. --macro
1527 */
1528static inline void unlock_ExtINT_logic(void)
1529{
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001530 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 struct IO_APIC_route_entry entry0, entry1;
1532 unsigned char save_control, save_freq_select;
1533 unsigned long flags;
1534
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001535 pin = find_isa_irq_pin(8, mp_INT);
1536 apic = find_isa_irq_apic(8, mp_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (pin == -1)
1538 return;
1539
1540 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001541 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1542 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001544 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 memset(&entry1, 0, sizeof(entry1));
1547
1548 entry1.dest_mode = 0; /* physical delivery */
1549 entry1.mask = 0; /* unmask IRQ now */
Benjamin Romeree4eff62007-02-13 13:26:25 +01001550 entry1.dest = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 entry1.delivery_mode = dest_ExtINT;
1552 entry1.polarity = entry0.polarity;
1553 entry1.trigger = 0;
1554 entry1.vector = 0;
1555
1556 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001557 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1558 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 spin_unlock_irqrestore(&ioapic_lock, flags);
1560
1561 save_control = CMOS_READ(RTC_CONTROL);
1562 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1563 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1564 RTC_FREQ_SELECT);
1565 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1566
1567 i = 100;
1568 while (i-- > 0) {
1569 mdelay(10);
1570 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1571 i -= 10;
1572 }
1573
1574 CMOS_WRITE(save_control, RTC_CONTROL);
1575 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001576 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
1578 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001579 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1580 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 spin_unlock_irqrestore(&ioapic_lock, flags);
1582}
1583
1584/*
1585 * This code may look a bit paranoid, but it's supposed to cooperate with
1586 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1587 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1588 * fanatically on his truly buggy board.
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -08001589 *
1590 * FIXME: really need to revamp this for modern platforms only.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 */
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -08001592static inline void check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001594 int apic1, pin1, apic2, pin2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int vector;
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06001596 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 /*
1599 * get/set the timer IRQ vector:
1600 */
1601 disable_8259A_irq(0);
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06001602 vector = assign_irq_vector(0, TARGET_CPUS, &mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 /*
1605 * Subtle, code in do_timer_interrupt() expects an AEOI
1606 * mode for the 8259A whenever interrupts are routed
1607 * through I/O APICs. Also IRQ0 has to be enabled in
1608 * the 8259A which implies the virtual wire has to be
1609 * disabled in the local APIC.
1610 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001611 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 init_8259A(1);
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -08001613 if (timer_over_8254 > 0)
1614 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001616 pin1 = find_isa_irq_pin(0, mp_INT);
1617 apic1 = find_isa_irq_apic(0, mp_INT);
1618 pin2 = ioapic_i8259.pin;
1619 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -08001621 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1622 vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -08001624 if (pin1 != -1) {
1625 /*
1626 * Ok, does IRQ0 through the IOAPIC work?
1627 */
1628 unmask_IO_APIC_irq(0);
1629 if (!no_timer_check && timer_irq_works()) {
1630 nmi_watchdog_default();
1631 if (nmi_watchdog == NMI_IO_APIC) {
1632 disable_8259A_irq(0);
1633 setup_nmi();
1634 enable_8259A_irq(0);
1635 }
1636 if (disable_timer_pin_1 > 0)
1637 clear_IO_APIC_pin(0, pin1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return;
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -08001639 }
1640 clear_IO_APIC_pin(apic1, pin1);
1641 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1642 "connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
Andi Kleenb0268722006-12-07 02:14:06 +01001644
Linus Torvaldsfea5f1e2007-01-08 15:04:46 -08001645 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1646 "through the 8259A ... ");
1647 if (pin2 != -1) {
1648 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1649 apic2, pin2);
1650 /*
1651 * legacy devices should be connected to IO APIC #0
1652 */
1653 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1654 if (timer_irq_works()) {
1655 apic_printk(APIC_VERBOSE," works.\n");
1656 nmi_watchdog_default();
1657 if (nmi_watchdog == NMI_IO_APIC) {
1658 setup_nmi();
1659 }
1660 return;
1661 }
1662 /*
1663 * Cleanup, just in case ...
1664 */
1665 clear_IO_APIC_pin(apic2, pin2);
1666 }
1667 apic_printk(APIC_VERBOSE," failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Chris McDermott1f992152006-02-26 04:18:40 +01001669 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1671 nmi_watchdog = 0;
1672 }
1673
1674 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1675
1676 disable_8259A_irq(0);
Ingo Molnard1bef4e2006-06-29 02:24:36 -07001677 irq_desc[0].chip = &lapic_irq_type;
Andi Kleen11a8e772006-01-11 22:46:51 +01001678 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 enable_8259A_irq(0);
1680
1681 if (timer_irq_works()) {
Chuck Ebbert5b922cd2006-03-25 16:30:55 +01001682 apic_printk(APIC_VERBOSE," works.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return;
1684 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001685 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 apic_printk(APIC_VERBOSE," failed.\n");
1687
1688 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1689
1690 init_8259A(0);
1691 make_8259A_irq(0);
Andi Kleen11a8e772006-01-11 22:46:51 +01001692 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 unlock_ExtINT_logic();
1695
1696 if (timer_irq_works()) {
1697 apic_printk(APIC_VERBOSE," works.\n");
1698 return;
1699 }
1700 apic_printk(APIC_VERBOSE," failed :(.\n");
1701 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1702}
1703
Andi Kleen14d98ca2005-05-20 14:27:59 -07001704static int __init notimercheck(char *s)
1705{
1706 no_timer_check = 1;
1707 return 1;
1708}
1709__setup("no_timer_check", notimercheck);
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711/*
1712 *
1713 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1714 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1715 * Linux doesn't really care, as it's not actually used
1716 * for any interrupt handling anyway.
1717 */
1718#define PIC_IRQS (1<<2)
1719
1720void __init setup_IO_APIC(void)
1721{
1722 enable_IO_APIC();
1723
1724 if (acpi_ioapic)
1725 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1726 else
1727 io_apic_irqs = ~PIC_IRQS;
1728
1729 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 sync_Arb_IDs();
1732 setup_IO_APIC_irqs();
1733 init_IO_APIC_traps();
1734 check_timer();
1735 if (!acpi_ioapic)
1736 print_IO_APIC();
1737}
1738
1739struct sysfs_ioapic_data {
1740 struct sys_device dev;
1741 struct IO_APIC_route_entry entry[0];
1742};
1743static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1744
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001745static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 struct IO_APIC_route_entry *entry;
1748 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 int i;
1750
1751 data = container_of(dev, struct sysfs_ioapic_data, dev);
1752 entry = data->entry;
Andi Kleeneea0e112006-09-26 10:52:30 +02001753 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
1754 *entry = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
1756 return 0;
1757}
1758
1759static int ioapic_resume(struct sys_device *dev)
1760{
1761 struct IO_APIC_route_entry *entry;
1762 struct sysfs_ioapic_data *data;
1763 unsigned long flags;
1764 union IO_APIC_reg_00 reg_00;
1765 int i;
1766
1767 data = container_of(dev, struct sysfs_ioapic_data, dev);
1768 entry = data->entry;
1769
1770 spin_lock_irqsave(&ioapic_lock, flags);
1771 reg_00.raw = io_apic_read(dev->id, 0);
1772 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1773 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1774 io_apic_write(dev->id, 0, reg_00.raw);
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 spin_unlock_irqrestore(&ioapic_lock, flags);
Andi Kleeneea0e112006-09-26 10:52:30 +02001777 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
1778 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 return 0;
1781}
1782
1783static struct sysdev_class ioapic_sysdev_class = {
1784 set_kset_name("ioapic"),
1785 .suspend = ioapic_suspend,
1786 .resume = ioapic_resume,
1787};
1788
1789static int __init ioapic_init_sysfs(void)
1790{
1791 struct sys_device * dev;
1792 int i, size, error = 0;
1793
1794 error = sysdev_class_register(&ioapic_sysdev_class);
1795 if (error)
1796 return error;
1797
1798 for (i = 0; i < nr_ioapics; i++ ) {
1799 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1800 * sizeof(struct IO_APIC_route_entry);
1801 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1802 if (!mp_ioapic_data[i]) {
1803 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1804 continue;
1805 }
1806 memset(mp_ioapic_data[i], 0, size);
1807 dev = &mp_ioapic_data[i]->dev;
1808 dev->id = i;
1809 dev->cls = &ioapic_sysdev_class;
1810 error = sysdev_register(dev);
1811 if (error) {
1812 kfree(mp_ioapic_data[i]);
1813 mp_ioapic_data[i] = NULL;
1814 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1815 continue;
1816 }
1817 }
1818
1819 return 0;
1820}
1821
1822device_initcall(ioapic_init_sysfs);
1823
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001824/*
Eric W. Biederman04b92672006-10-04 02:16:46 -07001825 * Dynamic irq allocate and deallocation
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001826 */
1827int create_irq(void)
1828{
Eric W. Biederman04b92672006-10-04 02:16:46 -07001829 /* Allocate an unused irq */
1830 int irq;
1831 int new;
1832 int vector = 0;
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001833 unsigned long flags;
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06001834 cpumask_t mask;
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001835
Eric W. Biederman04b92672006-10-04 02:16:46 -07001836 irq = -ENOSPC;
1837 spin_lock_irqsave(&vector_lock, flags);
1838 for (new = (NR_IRQS - 1); new >= 0; new--) {
1839 if (platform_legacy_irq(new))
1840 continue;
1841 if (irq_vector[new] != 0)
1842 continue;
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06001843 vector = __assign_irq_vector(new, TARGET_CPUS, &mask);
Eric W. Biederman04b92672006-10-04 02:16:46 -07001844 if (likely(vector > 0))
1845 irq = new;
1846 break;
1847 }
1848 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001849
Eric W. Biederman04b92672006-10-04 02:16:46 -07001850 if (irq >= 0) {
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001851 dynamic_irq_init(irq);
1852 }
1853 return irq;
1854}
1855
1856void destroy_irq(unsigned int irq)
1857{
1858 unsigned long flags;
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001859
1860 dynamic_irq_cleanup(irq);
1861
1862 spin_lock_irqsave(&vector_lock, flags);
Yinghai Lu5df02872006-12-07 02:14:05 +01001863 __clear_irq_vector(irq);
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001864 spin_unlock_irqrestore(&vector_lock, flags);
1865}
Eric W. Biedermanc4fa0bbf2006-10-04 02:16:40 -07001866
Eric W. Biederman589e3672006-10-04 02:16:42 -07001867/*
1868 * MSI mesage composition
1869 */
1870#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001871static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman589e3672006-10-04 02:16:42 -07001872{
Eric W. Biederman589e3672006-10-04 02:16:42 -07001873 int vector;
1874 unsigned dest;
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06001875 cpumask_t tmp;
Eric W. Biederman589e3672006-10-04 02:16:42 -07001876
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06001877 vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
Eric W. Biederman589e3672006-10-04 02:16:42 -07001878 if (vector >= 0) {
Eric W. Biederman589e3672006-10-04 02:16:42 -07001879 dest = cpu_mask_to_apicid(tmp);
1880
1881 msg->address_hi = MSI_ADDR_BASE_HI;
1882 msg->address_lo =
1883 MSI_ADDR_BASE_LO |
1884 ((INT_DEST_MODE == 0) ?
1885 MSI_ADDR_DEST_MODE_PHYSICAL:
1886 MSI_ADDR_DEST_MODE_LOGICAL) |
1887 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1888 MSI_ADDR_REDIRECTION_CPU:
1889 MSI_ADDR_REDIRECTION_LOWPRI) |
1890 MSI_ADDR_DEST_ID(dest);
1891
1892 msg->data =
1893 MSI_DATA_TRIGGER_EDGE |
1894 MSI_DATA_LEVEL_ASSERT |
1895 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1896 MSI_DATA_DELIVERY_FIXED:
1897 MSI_DATA_DELIVERY_LOWPRI) |
1898 MSI_DATA_VECTOR(vector);
1899 }
1900 return vector;
1901}
1902
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001903#ifdef CONFIG_SMP
1904static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
1905{
1906 struct msi_msg msg;
1907 unsigned int dest;
1908 cpumask_t tmp;
1909 int vector;
1910
1911 cpus_and(tmp, mask, cpu_online_map);
1912 if (cpus_empty(tmp))
1913 tmp = TARGET_CPUS;
1914
1915 cpus_and(mask, tmp, CPU_MASK_ALL);
1916
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06001917 vector = assign_irq_vector(irq, mask, &tmp);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001918 if (vector < 0)
1919 return;
1920
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001921 dest = cpu_mask_to_apicid(tmp);
1922
1923 read_msi_msg(irq, &msg);
1924
1925 msg.data &= ~MSI_DATA_VECTOR_MASK;
1926 msg.data |= MSI_DATA_VECTOR(vector);
1927 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
1928 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
1929
1930 write_msi_msg(irq, &msg);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07001931 irq_desc[irq].affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001932}
1933#endif /* CONFIG_SMP */
1934
1935/*
1936 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
1937 * which implement the MSI or MSI-X Capability Structure.
1938 */
1939static struct irq_chip msi_chip = {
1940 .name = "PCI-MSI",
1941 .unmask = unmask_msi_irq,
1942 .mask = mask_msi_irq,
1943 .ack = ack_apic_edge,
1944#ifdef CONFIG_SMP
1945 .set_affinity = set_msi_irq_affinity,
1946#endif
1947 .retrigger = ioapic_retrigger_irq,
1948};
1949
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07001950int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001951{
1952 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07001953 int irq, ret;
1954 irq = create_irq();
1955 if (irq < 0)
1956 return irq;
1957
1958 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001959 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07001960 if (ret < 0) {
1961 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001962 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07001963 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001964
1965 write_msi_msg(irq, &msg);
1966
Ingo Molnara460e742006-10-17 00:10:03 -07001967 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001968
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07001969 return irq;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001970}
1971
1972void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman589e3672006-10-04 02:16:42 -07001973{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07001974 destroy_irq(irq);
Eric W. Biederman589e3672006-10-04 02:16:42 -07001975}
1976
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07001977#endif /* CONFIG_PCI_MSI */
Eric W. Biederman589e3672006-10-04 02:16:42 -07001978
Eric W. Biederman8b955b02006-10-04 02:16:55 -07001979/*
1980 * Hypertransport interrupt support
1981 */
1982#ifdef CONFIG_HT_IRQ
1983
1984#ifdef CONFIG_SMP
1985
1986static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
1987{
Eric W. Biedermanec683072006-11-08 17:44:57 -08001988 struct ht_irq_msg msg;
1989 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07001990
Eric W. Biedermanec683072006-11-08 17:44:57 -08001991 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
1992 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07001993
Eric W. Biedermanec683072006-11-08 17:44:57 -08001994 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
1995 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07001996
Eric W. Biedermanec683072006-11-08 17:44:57 -08001997 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07001998}
1999
2000static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2001{
2002 unsigned int dest;
2003 cpumask_t tmp;
2004 int vector;
2005
2006 cpus_and(tmp, mask, cpu_online_map);
2007 if (cpus_empty(tmp))
2008 tmp = TARGET_CPUS;
2009
2010 cpus_and(mask, tmp, CPU_MASK_ALL);
2011
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06002012 vector = assign_irq_vector(irq, mask, &tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002013 if (vector < 0)
2014 return;
2015
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002016 dest = cpu_mask_to_apicid(tmp);
2017
Eric W. Biedermanec683072006-11-08 17:44:57 -08002018 target_ht_irq(irq, dest, vector);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002019 irq_desc[irq].affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002020}
2021#endif
2022
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002023static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002024 .name = "PCI-HT",
2025 .mask = mask_ht_irq,
2026 .unmask = unmask_ht_irq,
2027 .ack = ack_apic_edge,
2028#ifdef CONFIG_SMP
2029 .set_affinity = set_ht_irq_affinity,
2030#endif
2031 .retrigger = ioapic_retrigger_irq,
2032};
2033
2034int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2035{
2036 int vector;
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06002037 cpumask_t tmp;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002038
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06002039 vector = assign_irq_vector(irq, TARGET_CPUS, &tmp);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002040 if (vector >= 0) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002041 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002042 unsigned dest;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002043
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002044 dest = cpu_mask_to_apicid(tmp);
2045
Eric W. Biedermanec683072006-11-08 17:44:57 -08002046 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002047
Eric W. Biedermanec683072006-11-08 17:44:57 -08002048 msg.address_lo =
2049 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002050 HT_IRQ_LOW_DEST_ID(dest) |
2051 HT_IRQ_LOW_VECTOR(vector) |
2052 ((INT_DEST_MODE == 0) ?
2053 HT_IRQ_LOW_DM_PHYSICAL :
2054 HT_IRQ_LOW_DM_LOGICAL) |
2055 HT_IRQ_LOW_RQEOI_EDGE |
2056 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2057 HT_IRQ_LOW_MT_FIXED :
Eric W. Biedermanec683072006-11-08 17:44:57 -08002058 HT_IRQ_LOW_MT_ARBITRATED) |
2059 HT_IRQ_LOW_IRQ_MASKED;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002060
Eric W. Biedermanec683072006-11-08 17:44:57 -08002061 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002062
Ingo Molnara460e742006-10-17 00:10:03 -07002063 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2064 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002065 }
2066 return vector;
2067}
2068#endif /* CONFIG_HT_IRQ */
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070/* --------------------------------------------------------------------------
2071 ACPI-based IOAPIC Configuration
2072 -------------------------------------------------------------------------- */
2073
Len Brown888ba6c2005-08-24 12:07:20 -04002074#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076#define IO_APIC_MAX_ID 0xFE
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078int __init io_apic_get_redir_entries (int ioapic)
2079{
2080 union IO_APIC_reg_01 reg_01;
2081 unsigned long flags;
2082
2083 spin_lock_irqsave(&ioapic_lock, flags);
2084 reg_01.raw = io_apic_read(ioapic, 1);
2085 spin_unlock_irqrestore(&ioapic_lock, flags);
2086
2087 return reg_01.bits.entries;
2088}
2089
2090
Bob Moore50eca3e2005-09-30 19:03:00 -04002091int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 struct IO_APIC_route_entry entry;
2094 unsigned long flags;
Eric W. Biederman550f2292006-10-04 02:16:51 -07002095 int vector;
2096 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 if (!IO_APIC_IRQ(irq)) {
2099 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2100 ioapic);
2101 return -EINVAL;
2102 }
2103
Eric W. Biederman550f2292006-10-04 02:16:51 -07002104 /*
2105 * IRQs < 16 are already in the irq_2_pin[] map
2106 */
2107 if (irq >= 16)
2108 add_pin_to_irq(irq, ioapic, pin);
2109
2110
Eric W. Biedermanc7111c132006-10-08 07:47:55 -06002111 vector = assign_irq_vector(irq, TARGET_CPUS, &mask);
Eric W. Biederman550f2292006-10-04 02:16:51 -07002112 if (vector < 0)
2113 return vector;
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 /*
2116 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2117 * Note that we mask (disable) IRQs now -- these get enabled when the
2118 * corresponding device driver registers for this IRQ.
2119 */
2120
2121 memset(&entry,0,sizeof(entry));
2122
2123 entry.delivery_mode = INT_DELIVERY_MODE;
2124 entry.dest_mode = INT_DEST_MODE;
Benjamin Romeree4eff62007-02-13 13:26:25 +01002125 entry.dest = cpu_mask_to_apicid(mask);
Bob Moore50eca3e2005-09-30 19:03:00 -04002126 entry.trigger = triggering;
2127 entry.polarity = polarity;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 entry.mask = 1; /* Disabled (masked) */
Eric W. Biederman550f2292006-10-04 02:16:51 -07002129 entry.vector = vector & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 apic_printk(APIC_VERBOSE,KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry (%d-%d -> 0x%x -> "
2132 "IRQ %d Mode:%i Active:%i)\n", ioapic,
2133 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
Bob Moore50eca3e2005-09-30 19:03:00 -04002134 triggering, polarity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Eric W. Biedermana27bc062007-02-23 04:16:31 -07002136 ioapic_register_intr(irq, triggering);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 if (!ioapic && (irq < 16))
2139 disable_8259A_irq(irq);
2140
Andi Kleeneea0e112006-09-26 10:52:30 +02002141 ioapic_write_entry(ioapic, pin, entry);
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002144 irq_desc[irq].affinity = TARGET_CPUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 spin_unlock_irqrestore(&ioapic_lock, flags);
2146
2147 return 0;
2148}
2149
Len Brown888ba6c2005-08-24 12:07:20 -04002150#endif /* CONFIG_ACPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152
2153/*
2154 * This function currently is only a helper for the i386 smp boot process where
2155 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2156 * so mask in all cases should simply be TARGET_CPUS
2157 */
Ashok Raj54d5d422005-09-06 15:16:15 -07002158#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159void __init setup_ioapic_dest(void)
2160{
2161 int pin, ioapic, irq, irq_entry;
2162
2163 if (skip_ioapic_setup == 1)
2164 return;
2165
2166 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2167 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2168 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2169 if (irq_entry == -1)
2170 continue;
2171 irq = pin_2_irq(irq_entry, ioapic, pin);
Yinghai Luad892f52006-12-07 02:14:19 +01002172
2173 /* setup_IO_APIC_irqs could fail to get vector for some device
2174 * when you have too many devices, because at that time only boot
2175 * cpu is online.
2176 */
2177 if(!irq_vector[irq])
2178 setup_IO_APIC_irq(ioapic, pin, irq_entry, irq);
2179 else
2180 set_ioapic_affinity_irq(irq, TARGET_CPUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182
2183 }
2184}
Ashok Raj54d5d422005-09-06 15:16:15 -07002185#endif