genirq: mark io_apic level interrupts to avoid resend
[deliverable/linux.git] / arch / x86_64 / kernel / io_apic.c
CommitLineData
1da177e4
LT
1/*
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>
1da177e4
LT
24#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
589e367f 28#include <linux/pci.h>
1da177e4
LT
29#include <linux/mc146818rtc.h>
30#include <linux/acpi.h>
31#include <linux/sysdev.h>
3b7d1921 32#include <linux/msi.h>
95d77884 33#include <linux/htirq.h>
ab688059
AK
34#ifdef CONFIG_ACPI
35#include <acpi/acpi_bus.h>
36#endif
1da177e4 37
61014292 38#include <asm/idle.h>
1da177e4
LT
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>
8d916406 44#include <asm/acpi.h>
ca8642f6 45#include <asm/dma.h>
3e4ff115 46#include <asm/nmi.h>
589e367f 47#include <asm/msidef.h>
8b955b0d 48#include <asm/hypertransport.h>
1da177e4 49
13a79503
EB
50struct irq_cfg {
51 cpumask_t domain;
61014292
EB
52 cpumask_t old_domain;
53 unsigned move_cleanup_count;
13a79503 54 u8 vector;
61014292 55 u8 move_in_progress : 1;
13a79503
EB
56};
57
58/* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
59struct irq_cfg irq_cfg[NR_IRQS] __read_mostly = {
bc5e81a1
EB
60 [0] = { .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
61 [1] = { .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
62 [2] = { .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
63 [3] = { .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
64 [4] = { .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
65 [5] = { .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
66 [6] = { .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
67 [7] = { .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
68 [8] = { .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
69 [9] = { .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
70 [10] = { .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
71 [11] = { .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
72 [12] = { .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
73 [13] = { .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
74 [14] = { .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
75 [15] = { .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
13a79503
EB
76};
77
dfbffdd8 78static int assign_irq_vector(int irq, cpumask_t mask);
04b9267b 79
1da177e4
LT
80#define __apicdebuginit __init
81
82int sis_apic_bug; /* not actually supported, dummy for compile */
83
14d98cad
AK
84static int no_timer_check;
85
fea5f1e1
LT
86static int disable_timer_pin_1 __initdata;
87
88int timer_over_8254 __initdata = 1;
89
1008fddc
EB
90/* Where if anywhere is the i8259 connect in external int mode */
91static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
92
1da177e4 93static DEFINE_SPINLOCK(ioapic_lock);
70a0a535 94DEFINE_SPINLOCK(vector_lock);
1da177e4
LT
95
96/*
97 * # of IRQ routing registers
98 */
99int nr_ioapic_registers[MAX_IO_APICS];
100
101/*
102 * Rough estimation of how many shared IRQs there are, can
103 * be changed anytime.
104 */
e273d140 105#define MAX_PLUS_SHARED_IRQS NR_IRQS
1da177e4
LT
106#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
107
108/*
109 * This is performance-critical, we want to do it O(1)
110 *
111 * the indexing order of this array favors 1:1 mappings
112 * between pins and IRQs.
113 */
114
115static struct irq_pin_list {
116 short apic, pin, next;
117} irq_2_pin[PIN_MAP_SIZE];
118
6c0ffb9d
LT
119struct io_apic {
120 unsigned int index;
121 unsigned int unused[3];
122 unsigned int data;
123};
124
125static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
126{
127 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
128 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
129}
130
131static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
132{
133 struct io_apic __iomem *io_apic = io_apic_base(apic);
134 writel(reg, &io_apic->index);
135 return readl(&io_apic->data);
136}
137
138static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
139{
140 struct io_apic __iomem *io_apic = io_apic_base(apic);
141 writel(reg, &io_apic->index);
142 writel(value, &io_apic->data);
143}
144
145/*
146 * Re-write a value: to be used for read-modify-write
147 * cycles where the read already set up the index register.
148 */
149static inline void io_apic_modify(unsigned int apic, unsigned int value)
150{
151 struct io_apic __iomem *io_apic = io_apic_base(apic);
152 writel(value, &io_apic->data);
153}
154
ef3e28c5
EB
155static int io_apic_level_ack_pending(unsigned int irq)
156{
157 struct irq_pin_list *entry;
158 unsigned long flags;
159 int pending = 0;
160
161 spin_lock_irqsave(&ioapic_lock, flags);
162 entry = irq_2_pin + irq;
163 for (;;) {
164 unsigned int reg;
165 int pin;
166
167 pin = entry->pin;
168 if (pin == -1)
169 break;
170 reg = io_apic_read(entry->apic, 0x10 + pin*2);
171 /* Is the remote IRR bit set? */
172 pending |= (reg >> 14) & 1;
173 if (!entry->next)
174 break;
175 entry = irq_2_pin + entry->next;
176 }
177 spin_unlock_irqrestore(&ioapic_lock, flags);
178 return pending;
179}
180
6c0ffb9d
LT
181/*
182 * Synchronize the IO-APIC and the CPU by doing
183 * a dummy read from the IO-APIC
184 */
185static inline void io_apic_sync(unsigned int apic)
186{
187 struct io_apic __iomem *io_apic = io_apic_base(apic);
188 readl(&io_apic->data);
189}
190
54d5d424
AR
191#define __DO_ACTION(R, ACTION, FINAL) \
192 \
193{ \
194 int pin; \
195 struct irq_pin_list *entry = irq_2_pin + irq; \
196 \
6004e1b7 197 BUG_ON(irq >= NR_IRQS); \
54d5d424
AR
198 for (;;) { \
199 unsigned int reg; \
200 pin = entry->pin; \
201 if (pin == -1) \
202 break; \
203 reg = io_apic_read(entry->apic, 0x10 + R + pin*2); \
204 reg ACTION; \
205 io_apic_modify(entry->apic, reg); \
f45bcd70 206 FINAL; \
54d5d424
AR
207 if (!entry->next) \
208 break; \
209 entry = irq_2_pin + entry->next; \
210 } \
54d5d424
AR
211}
212
eea0e11c
AK
213union entry_union {
214 struct { u32 w1, w2; };
215 struct IO_APIC_route_entry entry;
216};
217
218static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
219{
220 union entry_union eu;
221 unsigned long flags;
222 spin_lock_irqsave(&ioapic_lock, flags);
223 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
224 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
225 spin_unlock_irqrestore(&ioapic_lock, flags);
226 return eu.entry;
227}
228
48797ebd
LT
229/*
230 * When we write a new IO APIC routing entry, we need to write the high
231 * word first! If the mask bit in the low word is clear, we will enable
232 * the interrupt, and we need to make sure the entry is fully populated
233 * before that happens.
234 */
516d2836
AK
235static void
236__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
eea0e11c 237{
eea0e11c
AK
238 union entry_union eu;
239 eu.entry = e;
48797ebd
LT
240 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
241 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
516d2836
AK
242}
243
244static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
245{
246 unsigned long flags;
247 spin_lock_irqsave(&ioapic_lock, flags);
248 __ioapic_write_entry(apic, pin, e);
48797ebd
LT
249 spin_unlock_irqrestore(&ioapic_lock, flags);
250}
251
252/*
253 * When we mask an IO APIC routing entry, we need to write the low
254 * word first, in order to set the mask bit before we change the
255 * high bits!
256 */
257static void ioapic_mask_entry(int apic, int pin)
258{
259 unsigned long flags;
260 union entry_union eu = { .entry.mask = 1 };
261
eea0e11c
AK
262 spin_lock_irqsave(&ioapic_lock, flags);
263 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
264 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
265 spin_unlock_irqrestore(&ioapic_lock, flags);
266}
267
54d5d424 268#ifdef CONFIG_SMP
550f2299
EB
269static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
270{
271 int apic, pin;
272 struct irq_pin_list *entry = irq_2_pin + irq;
273
274 BUG_ON(irq >= NR_IRQS);
275 for (;;) {
276 unsigned int reg;
277 apic = entry->apic;
278 pin = entry->pin;
279 if (pin == -1)
280 break;
281 io_apic_write(apic, 0x11 + pin*2, dest);
282 reg = io_apic_read(apic, 0x10 + pin*2);
283 reg &= ~0x000000ff;
284 reg |= vector;
285 io_apic_modify(apic, reg);
286 if (!entry->next)
287 break;
288 entry = irq_2_pin + entry->next;
289 }
290}
291
54d5d424
AR
292static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
293{
dfbffdd8 294 struct irq_cfg *cfg = irq_cfg + irq;
54d5d424
AR
295 unsigned long flags;
296 unsigned int dest;
297 cpumask_t tmp;
298
299 cpus_and(tmp, mask, cpu_online_map);
300 if (cpus_empty(tmp))
5ff5115e 301 return;
54d5d424 302
dfbffdd8 303 if (assign_irq_vector(irq, mask))
550f2299
EB
304 return;
305
dfbffdd8 306 cpus_and(tmp, cfg->domain, mask);
550f2299 307 dest = cpu_mask_to_apicid(tmp);
54d5d424
AR
308
309 /*
310 * Only the high 8 bits are valid.
311 */
312 dest = SET_APIC_LOGICAL_ID(dest);
313
314 spin_lock_irqsave(&ioapic_lock, flags);
dfbffdd8 315 __target_IO_APIC_irq(irq, dest, cfg->vector);
9f0a5ba5 316 irq_desc[irq].affinity = mask;
54d5d424
AR
317 spin_unlock_irqrestore(&ioapic_lock, flags);
318}
319#endif
320
1da177e4
LT
321/*
322 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
323 * shared ISA-space IRQs, so we have to support them. We are super
324 * fast in the common case, and fast for shared ISA-space IRQs.
325 */
326static void add_pin_to_irq(unsigned int irq, int apic, int pin)
327{
328 static int first_free_entry = NR_IRQS;
329 struct irq_pin_list *entry = irq_2_pin + irq;
330
6004e1b7 331 BUG_ON(irq >= NR_IRQS);
1da177e4
LT
332 while (entry->next)
333 entry = irq_2_pin + entry->next;
334
335 if (entry->pin != -1) {
336 entry->next = first_free_entry;
337 entry = irq_2_pin + entry->next;
338 if (++first_free_entry >= PIN_MAP_SIZE)
6004e1b7 339 panic("io_apic.c: ran out of irq_2_pin entries!");
1da177e4
LT
340 }
341 entry->apic = apic;
342 entry->pin = pin;
343}
344
1da177e4
LT
345
346#define DO_ACTION(name,R,ACTION, FINAL) \
347 \
348 static void name##_IO_APIC_irq (unsigned int irq) \
349 __DO_ACTION(R, ACTION, FINAL)
350
351DO_ACTION( __mask, 0, |= 0x00010000, io_apic_sync(entry->apic) )
352 /* mask = 1 */
353DO_ACTION( __unmask, 0, &= 0xfffeffff, )
354 /* mask = 0 */
355
356static void mask_IO_APIC_irq (unsigned int irq)
357{
358 unsigned long flags;
359
360 spin_lock_irqsave(&ioapic_lock, flags);
361 __mask_IO_APIC_irq(irq);
362 spin_unlock_irqrestore(&ioapic_lock, flags);
363}
364
365static void unmask_IO_APIC_irq (unsigned int irq)
366{
367 unsigned long flags;
368
369 spin_lock_irqsave(&ioapic_lock, flags);
370 __unmask_IO_APIC_irq(irq);
371 spin_unlock_irqrestore(&ioapic_lock, flags);
372}
373
374static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
375{
376 struct IO_APIC_route_entry entry;
1da177e4
LT
377
378 /* Check delivery_mode to be sure we're not clearing an SMI pin */
eea0e11c 379 entry = ioapic_read_entry(apic, pin);
1da177e4
LT
380 if (entry.delivery_mode == dest_SMI)
381 return;
382 /*
383 * Disable it in the IO-APIC irq-routing table:
384 */
48797ebd 385 ioapic_mask_entry(apic, pin);
1da177e4
LT
386}
387
388static void clear_IO_APIC (void)
389{
390 int apic, pin;
391
392 for (apic = 0; apic < nr_ioapics; apic++)
393 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
394 clear_IO_APIC_pin(apic, pin);
395}
396
1da177e4
LT
397int skip_ioapic_setup;
398int ioapic_force;
399
400/* dummy parsing: see setup.c */
401
402static int __init disable_ioapic_setup(char *str)
403{
404 skip_ioapic_setup = 1;
2c8c0e6b 405 return 0;
1da177e4 406}
2c8c0e6b 407early_param("noapic", disable_ioapic_setup);
1da177e4 408
fea5f1e1
LT
409/* Actually the next is obsolete, but keep it for paranoid reasons -AK */
410static int __init disable_timer_pin_setup(char *arg)
411{
412 disable_timer_pin_1 = 1;
413 return 1;
414}
415__setup("disable_timer_pin_1", disable_timer_pin_setup);
416
417static int __init setup_disable_8254_timer(char *s)
418{
419 timer_over_8254 = -1;
420 return 1;
421}
422static int __init setup_enable_8254_timer(char *s)
423{
424 timer_over_8254 = 2;
425 return 1;
426}
427
428__setup("disable_8254_timer", setup_disable_8254_timer);
429__setup("enable_8254_timer", setup_enable_8254_timer);
430
431
1da177e4
LT
432/*
433 * Find the IRQ entry number of a certain pin.
434 */
435static int find_irq_entry(int apic, int pin, int type)
436{
437 int i;
438
439 for (i = 0; i < mp_irq_entries; i++)
440 if (mp_irqs[i].mpc_irqtype == type &&
441 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
442 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
443 mp_irqs[i].mpc_dstirq == pin)
444 return i;
445
446 return -1;
447}
448
449/*
450 * Find the pin to which IRQ[irq] (ISA) is connected
451 */
1008fddc 452static int __init find_isa_irq_pin(int irq, int type)
1da177e4
LT
453{
454 int i;
455
456 for (i = 0; i < mp_irq_entries; i++) {
457 int lbus = mp_irqs[i].mpc_srcbus;
458
55f05ffa 459 if (test_bit(lbus, mp_bus_not_pci) &&
1da177e4
LT
460 (mp_irqs[i].mpc_irqtype == type) &&
461 (mp_irqs[i].mpc_srcbusirq == irq))
462
463 return mp_irqs[i].mpc_dstirq;
464 }
465 return -1;
466}
467
1008fddc
EB
468static int __init find_isa_irq_apic(int irq, int type)
469{
470 int i;
471
472 for (i = 0; i < mp_irq_entries; i++) {
473 int lbus = mp_irqs[i].mpc_srcbus;
474
55f05ffa 475 if (test_bit(lbus, mp_bus_not_pci) &&
1008fddc
EB
476 (mp_irqs[i].mpc_irqtype == type) &&
477 (mp_irqs[i].mpc_srcbusirq == irq))
478 break;
479 }
480 if (i < mp_irq_entries) {
481 int apic;
482 for(apic = 0; apic < nr_ioapics; apic++) {
483 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
484 return apic;
485 }
486 }
487
488 return -1;
489}
490
1da177e4
LT
491/*
492 * Find a specific PCI IRQ entry.
493 * Not an __init, possibly needed by modules
494 */
495static int pin_2_irq(int idx, int apic, int pin);
496
497int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
498{
499 int apic, i, best_guess = -1;
500
501 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
502 bus, slot, pin);
503 if (mp_bus_id_to_pci_bus[bus] == -1) {
504 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
505 return -1;
506 }
507 for (i = 0; i < mp_irq_entries; i++) {
508 int lbus = mp_irqs[i].mpc_srcbus;
509
510 for (apic = 0; apic < nr_ioapics; apic++)
511 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
512 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
513 break;
514
55f05ffa 515 if (!test_bit(lbus, mp_bus_not_pci) &&
1da177e4
LT
516 !mp_irqs[i].mpc_irqtype &&
517 (bus == lbus) &&
518 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
519 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
520
521 if (!(apic || IO_APIC_IRQ(irq)))
522 continue;
523
524 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
525 return irq;
526 /*
527 * Use the first all-but-pin matching entry as a
528 * best-guess fuzzy result for broken mptables.
529 */
530 if (best_guess < 0)
531 best_guess = irq;
532 }
533 }
6004e1b7 534 BUG_ON(best_guess >= NR_IRQS);
1da177e4
LT
535 return best_guess;
536}
537
1da177e4
LT
538/* ISA interrupts are always polarity zero edge triggered,
539 * when listed as conforming in the MP table. */
540
541#define default_ISA_trigger(idx) (0)
542#define default_ISA_polarity(idx) (0)
543
544/* PCI interrupts are always polarity one level triggered,
545 * when listed as conforming in the MP table. */
546
547#define default_PCI_trigger(idx) (1)
548#define default_PCI_polarity(idx) (1)
549
1da177e4
LT
550static int __init MPBIOS_polarity(int idx)
551{
552 int bus = mp_irqs[idx].mpc_srcbus;
553 int polarity;
554
555 /*
556 * Determine IRQ line polarity (high active or low active):
557 */
558 switch (mp_irqs[idx].mpc_irqflag & 3)
559 {
560 case 0: /* conforms, ie. bus-type dependent polarity */
55f05ffa
AK
561 if (test_bit(bus, mp_bus_not_pci))
562 polarity = default_ISA_polarity(idx);
563 else
564 polarity = default_PCI_polarity(idx);
1da177e4 565 break;
1da177e4
LT
566 case 1: /* high active */
567 {
568 polarity = 0;
569 break;
570 }
571 case 2: /* reserved */
572 {
573 printk(KERN_WARNING "broken BIOS!!\n");
574 polarity = 1;
575 break;
576 }
577 case 3: /* low active */
578 {
579 polarity = 1;
580 break;
581 }
582 default: /* invalid */
583 {
584 printk(KERN_WARNING "broken BIOS!!\n");
585 polarity = 1;
586 break;
587 }
588 }
589 return polarity;
590}
591
592static int MPBIOS_trigger(int idx)
593{
594 int bus = mp_irqs[idx].mpc_srcbus;
595 int trigger;
596
597 /*
598 * Determine IRQ trigger mode (edge or level sensitive):
599 */
600 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
601 {
602 case 0: /* conforms, ie. bus-type dependent */
55f05ffa
AK
603 if (test_bit(bus, mp_bus_not_pci))
604 trigger = default_ISA_trigger(idx);
605 else
606 trigger = default_PCI_trigger(idx);
1da177e4 607 break;
1da177e4
LT
608 case 1: /* edge */
609 {
610 trigger = 0;
611 break;
612 }
613 case 2: /* reserved */
614 {
615 printk(KERN_WARNING "broken BIOS!!\n");
616 trigger = 1;
617 break;
618 }
619 case 3: /* level */
620 {
621 trigger = 1;
622 break;
623 }
624 default: /* invalid */
625 {
626 printk(KERN_WARNING "broken BIOS!!\n");
627 trigger = 0;
628 break;
629 }
630 }
631 return trigger;
632}
633
634static inline int irq_polarity(int idx)
635{
636 return MPBIOS_polarity(idx);
637}
638
639static inline int irq_trigger(int idx)
640{
641 return MPBIOS_trigger(idx);
642}
643
644static int pin_2_irq(int idx, int apic, int pin)
645{
646 int irq, i;
647 int bus = mp_irqs[idx].mpc_srcbus;
648
649 /*
650 * Debugging check, we are in big trouble if this message pops up!
651 */
652 if (mp_irqs[idx].mpc_dstirq != pin)
653 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
654
55f05ffa
AK
655 if (test_bit(bus, mp_bus_not_pci)) {
656 irq = mp_irqs[idx].mpc_srcbusirq;
657 } else {
658 /*
659 * PCI IRQs are mapped in order
660 */
661 i = irq = 0;
662 while (i < apic)
663 irq += nr_ioapic_registers[i++];
664 irq += pin;
1da177e4 665 }
6004e1b7 666 BUG_ON(irq >= NR_IRQS);
1da177e4
LT
667 return irq;
668}
669
dfbffdd8 670static int __assign_irq_vector(int irq, cpumask_t mask)
1da177e4 671{
550f2299
EB
672 /*
673 * NOTE! The local APIC isn't very good at handling
674 * multiple interrupts at the same interrupt level.
675 * As the interrupt level is determined by taking the
676 * vector number and shifting that right by 4, we
677 * want to spread these out a bit so that they don't
678 * all fall in the same interrupt level.
679 *
680 * Also, we've got to be careful not to trash gate
681 * 0x80, because int 0x80 is hm, kind of importantish. ;)
682 */
d1752aa8 683 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
dfbffdd8 684 unsigned int old_vector;
550f2299 685 int cpu;
13a79503 686 struct irq_cfg *cfg;
1da177e4 687
e273d140 688 BUG_ON((unsigned)irq >= NR_IRQS);
13a79503 689 cfg = &irq_cfg[irq];
0a1ad60d 690
70a0a535
EB
691 /* Only try and allocate irqs on cpus that are present */
692 cpus_and(mask, mask, cpu_online_map);
693
61014292
EB
694 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
695 return -EBUSY;
696
dfbffdd8
EB
697 old_vector = cfg->vector;
698 if (old_vector) {
699 cpumask_t tmp;
700 cpus_and(tmp, cfg->domain, mask);
701 if (!cpus_empty(tmp))
702 return 0;
0a1ad60d 703 }
550f2299
EB
704
705 for_each_cpu_mask(cpu, mask) {
70a0a535 706 cpumask_t domain, new_mask;
61014292 707 int new_cpu;
550f2299 708 int vector, offset;
c7111c13
EB
709
710 domain = vector_allocation_domain(cpu);
70a0a535 711 cpus_and(new_mask, domain, cpu_online_map);
c7111c13 712
d1752aa8
EB
713 vector = current_vector;
714 offset = current_offset;
1da177e4 715next:
550f2299
EB
716 vector += 8;
717 if (vector >= FIRST_SYSTEM_VECTOR) {
718 /* If we run out of vectors on large boxen, must share them. */
719 offset = (offset + 1) % 8;
720 vector = FIRST_DEVICE_VECTOR + offset;
721 }
d1752aa8 722 if (unlikely(current_vector == vector))
550f2299
EB
723 continue;
724 if (vector == IA32_SYSCALL_VECTOR)
725 goto next;
70a0a535 726 for_each_cpu_mask(new_cpu, new_mask)
45edfd1d 727 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
c7111c13 728 goto next;
550f2299 729 /* Found one! */
d1752aa8
EB
730 current_vector = vector;
731 current_offset = offset;
61014292
EB
732 if (old_vector) {
733 cfg->move_in_progress = 1;
734 cfg->old_domain = cfg->domain;
735 }
70a0a535 736 for_each_cpu_mask(new_cpu, new_mask)
c7111c13 737 per_cpu(vector_irq, new_cpu)[vector] = irq;
13a79503
EB
738 cfg->vector = vector;
739 cfg->domain = domain;
dfbffdd8 740 return 0;
1da177e4 741 }
550f2299 742 return -ENOSPC;
04b9267b
EB
743}
744
dfbffdd8 745static int assign_irq_vector(int irq, cpumask_t mask)
04b9267b 746{
dfbffdd8 747 int err;
04b9267b 748 unsigned long flags;
0a1ad60d 749
04b9267b 750 spin_lock_irqsave(&vector_lock, flags);
dfbffdd8 751 err = __assign_irq_vector(irq, mask);
26a3c49c 752 spin_unlock_irqrestore(&vector_lock, flags);
dfbffdd8 753 return err;
1da177e4
LT
754}
755
5df0287e
YL
756static void __clear_irq_vector(int irq)
757{
13a79503 758 struct irq_cfg *cfg;
5df0287e
YL
759 cpumask_t mask;
760 int cpu, vector;
761
13a79503
EB
762 BUG_ON((unsigned)irq >= NR_IRQS);
763 cfg = &irq_cfg[irq];
764 BUG_ON(!cfg->vector);
5df0287e 765
13a79503
EB
766 vector = cfg->vector;
767 cpus_and(mask, cfg->domain, cpu_online_map);
5df0287e
YL
768 for_each_cpu_mask(cpu, mask)
769 per_cpu(vector_irq, cpu)[vector] = -1;
770
13a79503
EB
771 cfg->vector = 0;
772 cfg->domain = CPU_MASK_NONE;
5df0287e
YL
773}
774
70a0a535
EB
775void __setup_vector_irq(int cpu)
776{
777 /* Initialize vector_irq on a new cpu */
778 /* This function must be called with vector_lock held */
70a0a535
EB
779 int irq, vector;
780
70a0a535 781 /* Mark the inuse vectors */
e273d140 782 for (irq = 0; irq < NR_IRQS; ++irq) {
13a79503 783 if (!cpu_isset(cpu, irq_cfg[irq].domain))
70a0a535 784 continue;
13a79503 785 vector = irq_cfg[irq].vector;
70a0a535
EB
786 per_cpu(vector_irq, cpu)[vector] = irq;
787 }
788 /* Mark the free vectors */
789 for (vector = 0; vector < NR_VECTORS; ++vector) {
790 irq = per_cpu(vector_irq, cpu)[vector];
791 if (irq < 0)
792 continue;
13a79503 793 if (!cpu_isset(cpu, irq_cfg[irq].domain))
70a0a535
EB
794 per_cpu(vector_irq, cpu)[vector] = -1;
795 }
796}
797
798
f29bd1ba 799static struct irq_chip ioapic_chip;
1da177e4 800
a27bc06d 801static void ioapic_register_intr(int irq, unsigned long trigger)
1da177e4 802{
cc75b92d
TG
803 if (trigger) {
804 irq_desc[irq].status |= IRQ_LEVEL;
a460e745
IM
805 set_irq_chip_and_handler_name(irq, &ioapic_chip,
806 handle_fasteoi_irq, "fasteoi");
cc75b92d
TG
807 } else {
808 irq_desc[irq].status &= ~IRQ_LEVEL;
a460e745
IM
809 set_irq_chip_and_handler_name(irq, &ioapic_chip,
810 handle_edge_irq, "edge");
cc75b92d 811 }
1da177e4 812}
a8c8a367
EB
813
814static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
815 int trigger, int polarity)
1da177e4 816{
dfbffdd8 817 struct irq_cfg *cfg = irq_cfg + irq;
1da177e4 818 struct IO_APIC_route_entry entry;
a8c8a367 819 cpumask_t mask;
1da177e4 820
a8c8a367
EB
821 if (!IO_APIC_IRQ(irq))
822 return;
823
dfbffdd8
EB
824 mask = TARGET_CPUS;
825 if (assign_irq_vector(irq, mask))
a8c8a367
EB
826 return;
827
dfbffdd8
EB
828 cpus_and(mask, cfg->domain, mask);
829
a8c8a367
EB
830 apic_printk(APIC_VERBOSE,KERN_DEBUG
831 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
832 "IRQ %d Mode:%i Active:%i)\n",
dfbffdd8 833 apic, mp_ioapics[apic].mpc_apicid, pin, cfg->vector,
a8c8a367 834 irq, trigger, polarity);
1da177e4 835
ad892f5e
YL
836 /*
837 * add it to the IO-APIC irq-routing table:
838 */
839 memset(&entry,0,sizeof(entry));
1da177e4 840
ad892f5e
YL
841 entry.delivery_mode = INT_DELIVERY_MODE;
842 entry.dest_mode = INT_DEST_MODE;
a8c8a367 843 entry.dest = cpu_mask_to_apicid(mask);
ad892f5e 844 entry.mask = 0; /* enable IRQ */
a8c8a367
EB
845 entry.trigger = trigger;
846 entry.polarity = polarity;
dfbffdd8 847 entry.vector = cfg->vector;
1da177e4 848
a8c8a367
EB
849 /* Mask level triggered irqs.
850 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
851 */
852 if (trigger)
ad892f5e 853 entry.mask = 1;
ad892f5e 854
a8c8a367
EB
855 ioapic_register_intr(irq, trigger);
856 if (irq < 16)
857 disable_8259A_irq(irq);
ad892f5e
YL
858
859 ioapic_write_entry(apic, pin, entry);
ad892f5e
YL
860}
861
862static void __init setup_IO_APIC_irqs(void)
863{
864 int apic, pin, idx, irq, first_notcon = 1;
865
866 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
867
868 for (apic = 0; apic < nr_ioapics; apic++) {
869 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1da177e4
LT
870
871 idx = find_irq_entry(apic,pin,mp_INT);
872 if (idx == -1) {
873 if (first_notcon) {
874 apic_printk(APIC_VERBOSE, KERN_DEBUG " IO-APIC (apicid-pin) %d-%d", mp_ioapics[apic].mpc_apicid, pin);
875 first_notcon = 0;
876 } else
877 apic_printk(APIC_VERBOSE, ", %d-%d", mp_ioapics[apic].mpc_apicid, pin);
878 continue;
879 }
880
1da177e4
LT
881 irq = pin_2_irq(idx, apic, pin);
882 add_pin_to_irq(irq, apic, pin);
883
a8c8a367
EB
884 setup_IO_APIC_irq(apic, pin, irq,
885 irq_trigger(idx), irq_polarity(idx));
1da177e4
LT
886 }
887 }
888
889 if (!first_notcon)
890 apic_printk(APIC_VERBOSE," not connected.\n");
891}
892
893/*
894 * Set up the 8259A-master output pin as broadcast to all
895 * CPUs.
896 */
1008fddc 897static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
1da177e4
LT
898{
899 struct IO_APIC_route_entry entry;
900 unsigned long flags;
901
902 memset(&entry,0,sizeof(entry));
903
904 disable_8259A_irq(0);
905
906 /* mask LVT0 */
11a8e778 907 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1da177e4
LT
908
909 /*
910 * We use logical delivery to get the timer IRQ
911 * to the first CPU.
912 */
913 entry.dest_mode = INT_DEST_MODE;
914 entry.mask = 0; /* unmask IRQ now */
ee4eff6f 915 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
1da177e4
LT
916 entry.delivery_mode = INT_DELIVERY_MODE;
917 entry.polarity = 0;
918 entry.trigger = 0;
919 entry.vector = vector;
920
921 /*
922 * The timer IRQ doesn't have to know that behind the
923 * scene we have a 8259A-master in AEOI mode ...
924 */
a460e745 925 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1da177e4
LT
926
927 /*
928 * Add it to the IO-APIC irq-routing table:
929 */
930 spin_lock_irqsave(&ioapic_lock, flags);
1008fddc
EB
931 io_apic_write(apic, 0x11+2*pin, *(((int *)&entry)+1));
932 io_apic_write(apic, 0x10+2*pin, *(((int *)&entry)+0));
1da177e4
LT
933 spin_unlock_irqrestore(&ioapic_lock, flags);
934
935 enable_8259A_irq(0);
936}
937
1da177e4
LT
938void __apicdebuginit print_IO_APIC(void)
939{
940 int apic, i;
941 union IO_APIC_reg_00 reg_00;
942 union IO_APIC_reg_01 reg_01;
943 union IO_APIC_reg_02 reg_02;
944 unsigned long flags;
945
946 if (apic_verbosity == APIC_QUIET)
947 return;
948
949 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
950 for (i = 0; i < nr_ioapics; i++)
951 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
952 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
953
954 /*
955 * We are a bit conservative about what we expect. We have to
956 * know about every hardware change ASAP.
957 */
958 printk(KERN_INFO "testing the IO APIC.......................\n");
959
960 for (apic = 0; apic < nr_ioapics; apic++) {
961
962 spin_lock_irqsave(&ioapic_lock, flags);
963 reg_00.raw = io_apic_read(apic, 0);
964 reg_01.raw = io_apic_read(apic, 1);
965 if (reg_01.bits.version >= 0x10)
966 reg_02.raw = io_apic_read(apic, 2);
967 spin_unlock_irqrestore(&ioapic_lock, flags);
968
969 printk("\n");
970 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
971 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
972 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1da177e4
LT
973
974 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
975 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1da177e4
LT
976
977 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
978 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1da177e4
LT
979
980 if (reg_01.bits.version >= 0x10) {
981 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
982 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1da177e4
LT
983 }
984
985 printk(KERN_DEBUG ".... IRQ redirection table:\n");
986
ee4eff6f
BR
987 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
988 " Stat Dmod Deli Vect: \n");
1da177e4
LT
989
990 for (i = 0; i <= reg_01.bits.entries; i++) {
991 struct IO_APIC_route_entry entry;
992
eea0e11c 993 entry = ioapic_read_entry(apic, i);
1da177e4 994
ee4eff6f 995 printk(KERN_DEBUG " %02x %03X ",
1da177e4 996 i,
ee4eff6f 997 entry.dest
1da177e4
LT
998 );
999
1000 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1001 entry.mask,
1002 entry.trigger,
1003 entry.irr,
1004 entry.polarity,
1005 entry.delivery_status,
1006 entry.dest_mode,
1007 entry.delivery_mode,
1008 entry.vector
1009 );
1010 }
1011 }
1da177e4
LT
1012 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1013 for (i = 0; i < NR_IRQS; i++) {
1014 struct irq_pin_list *entry = irq_2_pin + i;
1015 if (entry->pin < 0)
1016 continue;
04b9267b 1017 printk(KERN_DEBUG "IRQ%d ", i);
1da177e4
LT
1018 for (;;) {
1019 printk("-> %d:%d", entry->apic, entry->pin);
1020 if (!entry->next)
1021 break;
1022 entry = irq_2_pin + entry->next;
1023 }
1024 printk("\n");
1025 }
1026
1027 printk(KERN_INFO ".................................... done.\n");
1028
1029 return;
1030}
1031
1032#if 0
1033
1034static __apicdebuginit void print_APIC_bitfield (int base)
1035{
1036 unsigned int v;
1037 int i, j;
1038
1039 if (apic_verbosity == APIC_QUIET)
1040 return;
1041
1042 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1043 for (i = 0; i < 8; i++) {
1044 v = apic_read(base + i*0x10);
1045 for (j = 0; j < 32; j++) {
1046 if (v & (1<<j))
1047 printk("1");
1048 else
1049 printk("0");
1050 }
1051 printk("\n");
1052 }
1053}
1054
1055void __apicdebuginit print_local_APIC(void * dummy)
1056{
1057 unsigned int v, ver, maxlvt;
1058
1059 if (apic_verbosity == APIC_QUIET)
1060 return;
1061
1062 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1063 smp_processor_id(), hard_smp_processor_id());
1064 v = apic_read(APIC_ID);
1065 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, GET_APIC_ID(v));
1066 v = apic_read(APIC_LVR);
1067 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1068 ver = GET_APIC_VERSION(v);
1069 maxlvt = get_maxlvt();
1070
1071 v = apic_read(APIC_TASKPRI);
1072 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1073
5a40b7c2
AK
1074 v = apic_read(APIC_ARBPRI);
1075 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1076 v & APIC_ARBPRI_MASK);
1077 v = apic_read(APIC_PROCPRI);
1078 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1da177e4
LT
1079
1080 v = apic_read(APIC_EOI);
1081 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1082 v = apic_read(APIC_RRR);
1083 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1084 v = apic_read(APIC_LDR);
1085 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1086 v = apic_read(APIC_DFR);
1087 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1088 v = apic_read(APIC_SPIV);
1089 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1090
1091 printk(KERN_DEBUG "... APIC ISR field:\n");
1092 print_APIC_bitfield(APIC_ISR);
1093 printk(KERN_DEBUG "... APIC TMR field:\n");
1094 print_APIC_bitfield(APIC_TMR);
1095 printk(KERN_DEBUG "... APIC IRR field:\n");
1096 print_APIC_bitfield(APIC_IRR);
1097
5a40b7c2
AK
1098 v = apic_read(APIC_ESR);
1099 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1da177e4
LT
1100
1101 v = apic_read(APIC_ICR);
1102 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1103 v = apic_read(APIC_ICR2);
1104 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1105
1106 v = apic_read(APIC_LVTT);
1107 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1108
1109 if (maxlvt > 3) { /* PC is LVT#4. */
1110 v = apic_read(APIC_LVTPC);
1111 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1112 }
1113 v = apic_read(APIC_LVT0);
1114 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1115 v = apic_read(APIC_LVT1);
1116 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1117
1118 if (maxlvt > 2) { /* ERR is LVT#3. */
1119 v = apic_read(APIC_LVTERR);
1120 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1121 }
1122
1123 v = apic_read(APIC_TMICT);
1124 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1125 v = apic_read(APIC_TMCCT);
1126 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1127 v = apic_read(APIC_TDCR);
1128 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1129 printk("\n");
1130}
1131
1132void print_all_local_APICs (void)
1133{
1134 on_each_cpu(print_local_APIC, NULL, 1, 1);
1135}
1136
1137void __apicdebuginit print_PIC(void)
1138{
1da177e4
LT
1139 unsigned int v;
1140 unsigned long flags;
1141
1142 if (apic_verbosity == APIC_QUIET)
1143 return;
1144
1145 printk(KERN_DEBUG "\nprinting PIC contents\n");
1146
1147 spin_lock_irqsave(&i8259A_lock, flags);
1148
1149 v = inb(0xa1) << 8 | inb(0x21);
1150 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1151
1152 v = inb(0xa0) << 8 | inb(0x20);
1153 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1154
1155 outb(0x0b,0xa0);
1156 outb(0x0b,0x20);
1157 v = inb(0xa0) << 8 | inb(0x20);
1158 outb(0x0a,0xa0);
1159 outb(0x0a,0x20);
1160
1161 spin_unlock_irqrestore(&i8259A_lock, flags);
1162
1163 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1164
1165 v = inb(0x4d1) << 8 | inb(0x4d0);
1166 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1167}
1168
1169#endif /* 0 */
1170
1171static void __init enable_IO_APIC(void)
1172{
1173 union IO_APIC_reg_01 reg_01;
1008fddc
EB
1174 int i8259_apic, i8259_pin;
1175 int i, apic;
1da177e4
LT
1176 unsigned long flags;
1177
1178 for (i = 0; i < PIN_MAP_SIZE; i++) {
1179 irq_2_pin[i].pin = -1;
1180 irq_2_pin[i].next = 0;
1181 }
1da177e4
LT
1182
1183 /*
1184 * The number of IO-APIC IRQ registers (== #pins):
1185 */
1008fddc 1186 for (apic = 0; apic < nr_ioapics; apic++) {
1da177e4 1187 spin_lock_irqsave(&ioapic_lock, flags);
1008fddc 1188 reg_01.raw = io_apic_read(apic, 1);
1da177e4 1189 spin_unlock_irqrestore(&ioapic_lock, flags);
1008fddc
EB
1190 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1191 }
1192 for(apic = 0; apic < nr_ioapics; apic++) {
1193 int pin;
1194 /* See if any of the pins is in ExtINT mode */
1195 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1196 struct IO_APIC_route_entry entry;
eea0e11c 1197 entry = ioapic_read_entry(apic, pin);
1008fddc
EB
1198
1199 /* If the interrupt line is enabled and in ExtInt mode
1200 * I have found the pin where the i8259 is connected.
1201 */
1202 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1203 ioapic_i8259.apic = apic;
1204 ioapic_i8259.pin = pin;
1205 goto found_i8259;
1206 }
1207 }
1208 }
1209 found_i8259:
1210 /* Look to see what if the MP table has reported the ExtINT */
1211 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1212 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1213 /* Trust the MP table if nothing is setup in the hardware */
1214 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1215 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1216 ioapic_i8259.pin = i8259_pin;
1217 ioapic_i8259.apic = i8259_apic;
1218 }
1219 /* Complain if the MP table and the hardware disagree */
1220 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1221 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1222 {
1223 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1da177e4
LT
1224 }
1225
1226 /*
1227 * Do not trust the IO-APIC being empty at bootup
1228 */
1229 clear_IO_APIC();
1230}
1231
1232/*
1233 * Not an __init, needed by the reboot code
1234 */
1235void disable_IO_APIC(void)
1236{
1237 /*
1238 * Clear the IO-APIC before rebooting:
1239 */
1240 clear_IO_APIC();
1241
208fb931 1242 /*
0b968d23 1243 * If the i8259 is routed through an IOAPIC
208fb931 1244 * Put that IOAPIC in virtual wire mode
0b968d23 1245 * so legacy interrupts can be delivered.
208fb931 1246 */
1008fddc 1247 if (ioapic_i8259.pin != -1) {
208fb931 1248 struct IO_APIC_route_entry entry;
208fb931
EB
1249
1250 memset(&entry, 0, sizeof(entry));
1251 entry.mask = 0; /* Enabled */
1252 entry.trigger = 0; /* Edge */
1253 entry.irr = 0;
1254 entry.polarity = 0; /* High */
1255 entry.delivery_status = 0;
1256 entry.dest_mode = 0; /* Physical */
1008fddc 1257 entry.delivery_mode = dest_ExtINT; /* ExtInt */
208fb931 1258 entry.vector = 0;
ee4eff6f 1259 entry.dest = GET_APIC_ID(apic_read(APIC_ID));
208fb931 1260
208fb931
EB
1261 /*
1262 * Add it to the IO-APIC irq-routing table:
1263 */
eea0e11c 1264 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
208fb931
EB
1265 }
1266
1008fddc 1267 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1da177e4
LT
1268}
1269
1da177e4
LT
1270/*
1271 * There is a nasty bug in some older SMP boards, their mptable lies
1272 * about the timer IRQ. We do the following to work around the situation:
1273 *
1274 * - timer IRQ defaults to IO-APIC IRQ
1275 * - if this function detects that timer IRQs are defunct, then we fall
1276 * back to ISA timer IRQs
1277 */
1278static int __init timer_irq_works(void)
1279{
1280 unsigned long t1 = jiffies;
1281
1282 local_irq_enable();
1283 /* Let ten ticks pass... */
1284 mdelay((10 * 1000) / HZ);
1285
1286 /*
1287 * Expect a few ticks at least, to be sure some possible
1288 * glue logic does not lock up after one or two first
1289 * ticks in a non-ExtINT mode. Also the local APIC
1290 * might have cached one ExtINT interrupt. Finally, at
1291 * least one tick may be lost due to delays.
1292 */
1293
1294 /* jiffies wrap? */
1295 if (jiffies - t1 > 4)
1296 return 1;
1297 return 0;
1298}
1299
1300/*
1301 * In the SMP+IOAPIC case it might happen that there are an unspecified
1302 * number of pending IRQ events unhandled. These cases are very rare,
1303 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1304 * better to do it this way as thus we do not have to be aware of
1305 * 'pending' interrupts in the IRQ path, except at this point.
1306 */
1307/*
1308 * Edge triggered needs to resend any interrupt
1309 * that was delayed but this is now handled in the device
1310 * independent code.
1311 */
1312
1313/*
1314 * Starting up a edge-triggered IO-APIC interrupt is
1315 * nasty - we need to make sure that we get the edge.
1316 * If it is already asserted for some reason, we need
1317 * return 1 to indicate that is was pending.
1318 *
1319 * This is not complete - we should be able to fake
1320 * an edge even if it isn't on the 8259A...
1321 */
1322
f29bd1ba 1323static unsigned int startup_ioapic_irq(unsigned int irq)
1da177e4
LT
1324{
1325 int was_pending = 0;
1326 unsigned long flags;
1327
1328 spin_lock_irqsave(&ioapic_lock, flags);
1329 if (irq < 16) {
1330 disable_8259A_irq(irq);
1331 if (i8259A_irq_pending(irq))
1332 was_pending = 1;
1333 }
1334 __unmask_IO_APIC_irq(irq);
1335 spin_unlock_irqrestore(&ioapic_lock, flags);
1336
1337 return was_pending;
1338}
1339
04b9267b 1340static int ioapic_retrigger_irq(unsigned int irq)
c0ad90a3 1341{
13a79503 1342 struct irq_cfg *cfg = &irq_cfg[irq];
550f2299 1343 cpumask_t mask;
6bf2dafa 1344 unsigned long flags;
550f2299 1345
6bf2dafa 1346 spin_lock_irqsave(&vector_lock, flags);
550f2299 1347 cpus_clear(mask);
13a79503 1348 cpu_set(first_cpu(cfg->domain), mask);
550f2299 1349
13a79503 1350 send_IPI_mask(mask, cfg->vector);
6bf2dafa 1351 spin_unlock_irqrestore(&vector_lock, flags);
c0ad90a3
IM
1352
1353 return 1;
1354}
1355
1da177e4
LT
1356/*
1357 * Level and edge triggered IO-APIC interrupts need different handling,
1358 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1359 * handled with the level-triggered descriptor, but that one has slightly
1360 * more overhead. Level-triggered interrupts cannot be handled with the
1361 * edge-triggered handler, without risking IRQ storms and other ugly
1362 * races.
1363 */
1364
61014292
EB
1365#ifdef CONFIG_SMP
1366asmlinkage void smp_irq_move_cleanup_interrupt(void)
1367{
1368 unsigned vector, me;
1369 ack_APIC_irq();
1370 exit_idle();
1371 irq_enter();
1372
1373 me = smp_processor_id();
1374 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1375 unsigned int irq;
1376 struct irq_desc *desc;
1377 struct irq_cfg *cfg;
1378 irq = __get_cpu_var(vector_irq)[vector];
1379 if (irq >= NR_IRQS)
1380 continue;
1381
1382 desc = irq_desc + irq;
1383 cfg = irq_cfg + irq;
1384 spin_lock(&desc->lock);
1385 if (!cfg->move_cleanup_count)
1386 goto unlock;
1387
1388 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
1389 goto unlock;
1390
1391 __get_cpu_var(vector_irq)[vector] = -1;
1392 cfg->move_cleanup_count--;
1393unlock:
1394 spin_unlock(&desc->lock);
1395 }
1396
1397 irq_exit();
1398}
1399
1400static void irq_complete_move(unsigned int irq)
1401{
1402 struct irq_cfg *cfg = irq_cfg + irq;
1403 unsigned vector, me;
1404
1405 if (likely(!cfg->move_in_progress))
1406 return;
1407
1408 vector = ~get_irq_regs()->orig_rax;
1409 me = smp_processor_id();
f0e13ae7 1410 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
61014292
EB
1411 cpumask_t cleanup_mask;
1412
1413 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
1414 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
1415 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
1416 cfg->move_in_progress = 0;
1417 }
1418}
1419#else
1420static inline void irq_complete_move(unsigned int irq) {}
1421#endif
1422
0be6652f
EB
1423static void ack_apic_edge(unsigned int irq)
1424{
61014292 1425 irq_complete_move(irq);
0be6652f
EB
1426 move_native_irq(irq);
1427 ack_APIC_irq();
1428}
1429
1430static void ack_apic_level(unsigned int irq)
1431{
1432 int do_unmask_irq = 0;
1433
61014292 1434 irq_complete_move(irq);
0be6652f
EB
1435#if defined(CONFIG_GENERIC_PENDING_IRQ) || defined(CONFIG_IRQBALANCE)
1436 /* If we are moving the irq we need to mask it */
1437 if (unlikely(irq_desc[irq].status & IRQ_MOVE_PENDING)) {
1438 do_unmask_irq = 1;
1439 mask_IO_APIC_irq(irq);
1440 }
1441#endif
1442
1443 /*
1444 * We must acknowledge the irq before we move it or the acknowledge will
beb7dd86 1445 * not propagate properly.
0be6652f
EB
1446 */
1447 ack_APIC_irq();
1448
1449 /* Now we can move and renable the irq */
ef3e28c5
EB
1450 if (unlikely(do_unmask_irq)) {
1451 /* Only migrate the irq if the ack has been received.
1452 *
1453 * On rare occasions the broadcast level triggered ack gets
1454 * delayed going to ioapics, and if we reprogram the
1455 * vector while Remote IRR is still set the irq will never
1456 * fire again.
1457 *
1458 * To prevent this scenario we read the Remote IRR bit
1459 * of the ioapic. This has two effects.
1460 * - On any sane system the read of the ioapic will
1461 * flush writes (and acks) going to the ioapic from
1462 * this cpu.
1463 * - We get to see if the ACK has actually been delivered.
1464 *
1465 * Based on failed experiments of reprogramming the
1466 * ioapic entry from outside of irq context starting
1467 * with masking the ioapic entry and then polling until
1468 * Remote IRR was clear before reprogramming the
1469 * ioapic I don't trust the Remote IRR bit to be
1470 * completey accurate.
1471 *
1472 * However there appears to be no other way to plug
1473 * this race, so if the Remote IRR bit is not
1474 * accurate and is causing problems then it is a hardware bug
1475 * and you can go talk to the chipset vendor about it.
1476 */
1477 if (!io_apic_level_ack_pending(irq))
1478 move_masked_irq(irq);
0be6652f 1479 unmask_IO_APIC_irq(irq);
ef3e28c5 1480 }
0be6652f
EB
1481}
1482
f29bd1ba
IM
1483static struct irq_chip ioapic_chip __read_mostly = {
1484 .name = "IO-APIC",
04b9267b
EB
1485 .startup = startup_ioapic_irq,
1486 .mask = mask_IO_APIC_irq,
1487 .unmask = unmask_IO_APIC_irq,
0be6652f
EB
1488 .ack = ack_apic_edge,
1489 .eoi = ack_apic_level,
54d5d424 1490#ifdef CONFIG_SMP
04b9267b 1491 .set_affinity = set_ioapic_affinity_irq,
54d5d424 1492#endif
04b9267b 1493 .retrigger = ioapic_retrigger_irq,
1da177e4
LT
1494};
1495
1496static inline void init_IO_APIC_traps(void)
1497{
1498 int irq;
1499
1500 /*
1501 * NOTE! The local APIC isn't very good at handling
1502 * multiple interrupts at the same interrupt level.
1503 * As the interrupt level is determined by taking the
1504 * vector number and shifting that right by 4, we
1505 * want to spread these out a bit so that they don't
1506 * all fall in the same interrupt level.
1507 *
1508 * Also, we've got to be careful not to trash gate
1509 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1510 */
1511 for (irq = 0; irq < NR_IRQS ; irq++) {
1512 int tmp = irq;
13a79503 1513 if (IO_APIC_IRQ(tmp) && !irq_cfg[tmp].vector) {
1da177e4
LT
1514 /*
1515 * Hmm.. We don't have an entry for this,
1516 * so default to an old-fashioned 8259
1517 * interrupt if we can..
1518 */
1519 if (irq < 16)
1520 make_8259A_irq(irq);
1521 else
1522 /* Strange. Oh, well.. */
f29bd1ba 1523 irq_desc[irq].chip = &no_irq_chip;
1da177e4
LT
1524 }
1525 }
1526}
1527
1528static void enable_lapic_irq (unsigned int irq)
1529{
1530 unsigned long v;
1531
1532 v = apic_read(APIC_LVT0);
11a8e778 1533 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1da177e4
LT
1534}
1535
1536static void disable_lapic_irq (unsigned int irq)
1537{
1538 unsigned long v;
1539
1540 v = apic_read(APIC_LVT0);
11a8e778 1541 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1da177e4
LT
1542}
1543
1544static void ack_lapic_irq (unsigned int irq)
1545{
1546 ack_APIC_irq();
1547}
1548
1549static void end_lapic_irq (unsigned int i) { /* nothing */ }
1550
6c231b7b 1551static struct hw_interrupt_type lapic_irq_type __read_mostly = {
c47e285d 1552 .name = "local-APIC",
1da177e4
LT
1553 .typename = "local-APIC-edge",
1554 .startup = NULL, /* startup_irq() not used for IRQ0 */
1555 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1556 .enable = enable_lapic_irq,
1557 .disable = disable_lapic_irq,
1558 .ack = ack_lapic_irq,
1559 .end = end_lapic_irq,
1560};
1561
1562static void setup_nmi (void)
1563{
1564 /*
1565 * Dirty trick to enable the NMI watchdog ...
1566 * We put the 8259A master into AEOI mode and
1567 * unmask on all local APICs LVT0 as NMI.
1568 *
1569 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
1570 * is from Maciej W. Rozycki - so we do not have to EOI from
1571 * the NMI handler or the timer interrupt.
1572 */
1573 printk(KERN_INFO "activating NMI Watchdog ...");
1574
1575 enable_NMI_through_LVT0(NULL);
1576
1577 printk(" done.\n");
1578}
1579
1580/*
1581 * This looks a bit hackish but it's about the only one way of sending
1582 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1583 * not support the ExtINT mode, unfortunately. We need to send these
1584 * cycles as some i82489DX-based boards have glue logic that keeps the
1585 * 8259A interrupt line asserted until INTA. --macro
1586 */
1587static inline void unlock_ExtINT_logic(void)
1588{
1008fddc 1589 int apic, pin, i;
1da177e4
LT
1590 struct IO_APIC_route_entry entry0, entry1;
1591 unsigned char save_control, save_freq_select;
1592 unsigned long flags;
1593
1008fddc
EB
1594 pin = find_isa_irq_pin(8, mp_INT);
1595 apic = find_isa_irq_apic(8, mp_INT);
1da177e4
LT
1596 if (pin == -1)
1597 return;
1598
1599 spin_lock_irqsave(&ioapic_lock, flags);
1008fddc
EB
1600 *(((int *)&entry0) + 1) = io_apic_read(apic, 0x11 + 2 * pin);
1601 *(((int *)&entry0) + 0) = io_apic_read(apic, 0x10 + 2 * pin);
1da177e4 1602 spin_unlock_irqrestore(&ioapic_lock, flags);
1008fddc 1603 clear_IO_APIC_pin(apic, pin);
1da177e4
LT
1604
1605 memset(&entry1, 0, sizeof(entry1));
1606
1607 entry1.dest_mode = 0; /* physical delivery */
1608 entry1.mask = 0; /* unmask IRQ now */
ee4eff6f 1609 entry1.dest = hard_smp_processor_id();
1da177e4
LT
1610 entry1.delivery_mode = dest_ExtINT;
1611 entry1.polarity = entry0.polarity;
1612 entry1.trigger = 0;
1613 entry1.vector = 0;
1614
1615 spin_lock_irqsave(&ioapic_lock, flags);
1008fddc
EB
1616 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry1) + 1));
1617 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry1) + 0));
1da177e4
LT
1618 spin_unlock_irqrestore(&ioapic_lock, flags);
1619
1620 save_control = CMOS_READ(RTC_CONTROL);
1621 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1622 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1623 RTC_FREQ_SELECT);
1624 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1625
1626 i = 100;
1627 while (i-- > 0) {
1628 mdelay(10);
1629 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1630 i -= 10;
1631 }
1632
1633 CMOS_WRITE(save_control, RTC_CONTROL);
1634 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1008fddc 1635 clear_IO_APIC_pin(apic, pin);
1da177e4
LT
1636
1637 spin_lock_irqsave(&ioapic_lock, flags);
1008fddc
EB
1638 io_apic_write(apic, 0x11 + 2 * pin, *(((int *)&entry0) + 1));
1639 io_apic_write(apic, 0x10 + 2 * pin, *(((int *)&entry0) + 0));
1da177e4
LT
1640 spin_unlock_irqrestore(&ioapic_lock, flags);
1641}
1642
1643/*
1644 * This code may look a bit paranoid, but it's supposed to cooperate with
1645 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1646 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1647 * fanatically on his truly buggy board.
fea5f1e1
LT
1648 *
1649 * FIXME: really need to revamp this for modern platforms only.
1da177e4 1650 */
fea5f1e1 1651static inline void check_timer(void)
1da177e4 1652{
dfbffdd8 1653 struct irq_cfg *cfg = irq_cfg + 0;
1008fddc 1654 int apic1, pin1, apic2, pin2;
1da177e4
LT
1655
1656 /*
1657 * get/set the timer IRQ vector:
1658 */
1659 disable_8259A_irq(0);
dfbffdd8 1660 assign_irq_vector(0, TARGET_CPUS);
1da177e4
LT
1661
1662 /*
1663 * Subtle, code in do_timer_interrupt() expects an AEOI
1664 * mode for the 8259A whenever interrupts are routed
1665 * through I/O APICs. Also IRQ0 has to be enabled in
1666 * the 8259A which implies the virtual wire has to be
1667 * disabled in the local APIC.
1668 */
11a8e778 1669 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1da177e4 1670 init_8259A(1);
fea5f1e1
LT
1671 if (timer_over_8254 > 0)
1672 enable_8259A_irq(0);
1da177e4 1673
1008fddc
EB
1674 pin1 = find_isa_irq_pin(0, mp_INT);
1675 apic1 = find_isa_irq_apic(0, mp_INT);
1676 pin2 = ioapic_i8259.pin;
1677 apic2 = ioapic_i8259.apic;
1da177e4 1678
fea5f1e1 1679 apic_printk(APIC_VERBOSE,KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
dfbffdd8 1680 cfg->vector, apic1, pin1, apic2, pin2);
b0268726 1681
fea5f1e1
LT
1682 if (pin1 != -1) {
1683 /*
1684 * Ok, does IRQ0 through the IOAPIC work?
1685 */
1686 unmask_IO_APIC_irq(0);
1687 if (!no_timer_check && timer_irq_works()) {
1688 nmi_watchdog_default();
1689 if (nmi_watchdog == NMI_IO_APIC) {
1690 disable_8259A_irq(0);
1691 setup_nmi();
1692 enable_8259A_irq(0);
1693 }
1694 if (disable_timer_pin_1 > 0)
1695 clear_IO_APIC_pin(0, pin1);
1696 return;
1697 }
1698 clear_IO_APIC_pin(apic1, pin1);
1699 apic_printk(APIC_QUIET,KERN_ERR "..MP-BIOS bug: 8254 timer not "
1700 "connected to IO-APIC\n");
1701 }
1da177e4 1702
fea5f1e1
LT
1703 apic_printk(APIC_VERBOSE,KERN_INFO "...trying to set up timer (IRQ0) "
1704 "through the 8259A ... ");
1da177e4 1705 if (pin2 != -1) {
fea5f1e1
LT
1706 apic_printk(APIC_VERBOSE,"\n..... (found apic %d pin %d) ...",
1707 apic2, pin2);
1708 /*
1709 * legacy devices should be connected to IO APIC #0
1710 */
dfbffdd8 1711 setup_ExtINT_IRQ0_pin(apic2, pin2, cfg->vector);
fea5f1e1
LT
1712 if (timer_irq_works()) {
1713 apic_printk(APIC_VERBOSE," works.\n");
1714 nmi_watchdog_default();
1715 if (nmi_watchdog == NMI_IO_APIC) {
1716 setup_nmi();
1717 }
1da177e4 1718 return;
fea5f1e1
LT
1719 }
1720 /*
1721 * Cleanup, just in case ...
1722 */
1723 clear_IO_APIC_pin(apic2, pin2);
1da177e4 1724 }
fea5f1e1 1725 apic_printk(APIC_VERBOSE," failed.\n");
1da177e4 1726
1f992153 1727 if (nmi_watchdog == NMI_IO_APIC) {
1da177e4
LT
1728 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1729 nmi_watchdog = 0;
1730 }
1731
1732 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1733
1734 disable_8259A_irq(0);
d1bef4ed 1735 irq_desc[0].chip = &lapic_irq_type;
dfbffdd8 1736 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
1da177e4
LT
1737 enable_8259A_irq(0);
1738
1739 if (timer_irq_works()) {
5b922cd4 1740 apic_printk(APIC_VERBOSE," works.\n");
1da177e4
LT
1741 return;
1742 }
dfbffdd8 1743 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
1da177e4
LT
1744 apic_printk(APIC_VERBOSE," failed.\n");
1745
1746 apic_printk(APIC_VERBOSE, KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1747
1748 init_8259A(0);
1749 make_8259A_irq(0);
11a8e778 1750 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4
LT
1751
1752 unlock_ExtINT_logic();
1753
1754 if (timer_irq_works()) {
1755 apic_printk(APIC_VERBOSE," works.\n");
1756 return;
1757 }
1758 apic_printk(APIC_VERBOSE," failed :(.\n");
1759 panic("IO-APIC + timer doesn't work! Try using the 'noapic' kernel parameter\n");
1760}
1761
14d98cad
AK
1762static int __init notimercheck(char *s)
1763{
1764 no_timer_check = 1;
1765 return 1;
1766}
1767__setup("no_timer_check", notimercheck);
1768
1da177e4
LT
1769/*
1770 *
1771 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
1772 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
1773 * Linux doesn't really care, as it's not actually used
1774 * for any interrupt handling anyway.
1775 */
1776#define PIC_IRQS (1<<2)
1777
1778void __init setup_IO_APIC(void)
1779{
1780 enable_IO_APIC();
1781
1782 if (acpi_ioapic)
1783 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
1784 else
1785 io_apic_irqs = ~PIC_IRQS;
1786
1787 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
1788
1da177e4
LT
1789 sync_Arb_IDs();
1790 setup_IO_APIC_irqs();
1791 init_IO_APIC_traps();
1792 check_timer();
1793 if (!acpi_ioapic)
1794 print_IO_APIC();
1795}
1796
1797struct sysfs_ioapic_data {
1798 struct sys_device dev;
1799 struct IO_APIC_route_entry entry[0];
1800};
1801static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
1802
0b9c33a7 1803static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
1804{
1805 struct IO_APIC_route_entry *entry;
1806 struct sysfs_ioapic_data *data;
1da177e4
LT
1807 int i;
1808
1809 data = container_of(dev, struct sysfs_ioapic_data, dev);
1810 entry = data->entry;
eea0e11c
AK
1811 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
1812 *entry = ioapic_read_entry(dev->id, i);
1da177e4
LT
1813
1814 return 0;
1815}
1816
1817static int ioapic_resume(struct sys_device *dev)
1818{
1819 struct IO_APIC_route_entry *entry;
1820 struct sysfs_ioapic_data *data;
1821 unsigned long flags;
1822 union IO_APIC_reg_00 reg_00;
1823 int i;
1824
1825 data = container_of(dev, struct sysfs_ioapic_data, dev);
1826 entry = data->entry;
1827
1828 spin_lock_irqsave(&ioapic_lock, flags);
1829 reg_00.raw = io_apic_read(dev->id, 0);
1830 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
1831 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
1832 io_apic_write(dev->id, 0, reg_00.raw);
1833 }
1da177e4 1834 spin_unlock_irqrestore(&ioapic_lock, flags);
eea0e11c
AK
1835 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
1836 ioapic_write_entry(dev->id, i, entry[i]);
1da177e4
LT
1837
1838 return 0;
1839}
1840
1841static struct sysdev_class ioapic_sysdev_class = {
1842 set_kset_name("ioapic"),
1843 .suspend = ioapic_suspend,
1844 .resume = ioapic_resume,
1845};
1846
1847static int __init ioapic_init_sysfs(void)
1848{
1849 struct sys_device * dev;
1850 int i, size, error = 0;
1851
1852 error = sysdev_class_register(&ioapic_sysdev_class);
1853 if (error)
1854 return error;
1855
1856 for (i = 0; i < nr_ioapics; i++ ) {
1857 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
1858 * sizeof(struct IO_APIC_route_entry);
1859 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
1860 if (!mp_ioapic_data[i]) {
1861 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1862 continue;
1863 }
1864 memset(mp_ioapic_data[i], 0, size);
1865 dev = &mp_ioapic_data[i]->dev;
1866 dev->id = i;
1867 dev->cls = &ioapic_sysdev_class;
1868 error = sysdev_register(dev);
1869 if (error) {
1870 kfree(mp_ioapic_data[i]);
1871 mp_ioapic_data[i] = NULL;
1872 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
1873 continue;
1874 }
1875 }
1876
1877 return 0;
1878}
1879
1880device_initcall(ioapic_init_sysfs);
1881
c4fa0bbf 1882/*
04b9267b 1883 * Dynamic irq allocate and deallocation
c4fa0bbf
EB
1884 */
1885int create_irq(void)
1886{
04b9267b
EB
1887 /* Allocate an unused irq */
1888 int irq;
1889 int new;
c4fa0bbf 1890 unsigned long flags;
c4fa0bbf 1891
04b9267b
EB
1892 irq = -ENOSPC;
1893 spin_lock_irqsave(&vector_lock, flags);
1894 for (new = (NR_IRQS - 1); new >= 0; new--) {
1895 if (platform_legacy_irq(new))
1896 continue;
13a79503 1897 if (irq_cfg[new].vector != 0)
04b9267b 1898 continue;
dfbffdd8 1899 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
04b9267b
EB
1900 irq = new;
1901 break;
1902 }
1903 spin_unlock_irqrestore(&vector_lock, flags);
c4fa0bbf 1904
04b9267b 1905 if (irq >= 0) {
c4fa0bbf
EB
1906 dynamic_irq_init(irq);
1907 }
1908 return irq;
1909}
1910
1911void destroy_irq(unsigned int irq)
1912{
1913 unsigned long flags;
c4fa0bbf
EB
1914
1915 dynamic_irq_cleanup(irq);
1916
1917 spin_lock_irqsave(&vector_lock, flags);
5df0287e 1918 __clear_irq_vector(irq);
c4fa0bbf
EB
1919 spin_unlock_irqrestore(&vector_lock, flags);
1920}
c4fa0bbf 1921
589e367f
EB
1922/*
1923 * MSI mesage composition
1924 */
1925#ifdef CONFIG_PCI_MSI
3b7d1921 1926static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
589e367f 1927{
dfbffdd8
EB
1928 struct irq_cfg *cfg = irq_cfg + irq;
1929 int err;
589e367f 1930 unsigned dest;
c7111c13 1931 cpumask_t tmp;
589e367f 1932
dfbffdd8
EB
1933 tmp = TARGET_CPUS;
1934 err = assign_irq_vector(irq, tmp);
1935 if (!err) {
1936 cpus_and(tmp, cfg->domain, tmp);
589e367f
EB
1937 dest = cpu_mask_to_apicid(tmp);
1938
1939 msg->address_hi = MSI_ADDR_BASE_HI;
1940 msg->address_lo =
1941 MSI_ADDR_BASE_LO |
1942 ((INT_DEST_MODE == 0) ?
1943 MSI_ADDR_DEST_MODE_PHYSICAL:
1944 MSI_ADDR_DEST_MODE_LOGICAL) |
1945 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1946 MSI_ADDR_REDIRECTION_CPU:
1947 MSI_ADDR_REDIRECTION_LOWPRI) |
1948 MSI_ADDR_DEST_ID(dest);
1949
1950 msg->data =
1951 MSI_DATA_TRIGGER_EDGE |
1952 MSI_DATA_LEVEL_ASSERT |
1953 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
1954 MSI_DATA_DELIVERY_FIXED:
1955 MSI_DATA_DELIVERY_LOWPRI) |
dfbffdd8 1956 MSI_DATA_VECTOR(cfg->vector);
589e367f 1957 }
dfbffdd8 1958 return err;
589e367f
EB
1959}
1960
3b7d1921
EB
1961#ifdef CONFIG_SMP
1962static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
589e367f 1963{
dfbffdd8 1964 struct irq_cfg *cfg = irq_cfg + irq;
3b7d1921
EB
1965 struct msi_msg msg;
1966 unsigned int dest;
1967 cpumask_t tmp;
3b7d1921
EB
1968
1969 cpus_and(tmp, mask, cpu_online_map);
1970 if (cpus_empty(tmp))
5ff5115e 1971 return;
589e367f 1972
dfbffdd8 1973 if (assign_irq_vector(irq, mask))
3b7d1921 1974 return;
550f2299 1975
dfbffdd8 1976 cpus_and(tmp, cfg->domain, mask);
3b7d1921 1977 dest = cpu_mask_to_apicid(tmp);
589e367f 1978
3b7d1921
EB
1979 read_msi_msg(irq, &msg);
1980
1981 msg.data &= ~MSI_DATA_VECTOR_MASK;
dfbffdd8 1982 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3b7d1921
EB
1983 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
1984 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
1985
1986 write_msi_msg(irq, &msg);
9f0a5ba5 1987 irq_desc[irq].affinity = mask;
589e367f 1988}
3b7d1921 1989#endif /* CONFIG_SMP */
589e367f 1990
3b7d1921
EB
1991/*
1992 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
1993 * which implement the MSI or MSI-X Capability Structure.
1994 */
1995static struct irq_chip msi_chip = {
1996 .name = "PCI-MSI",
1997 .unmask = unmask_msi_irq,
1998 .mask = mask_msi_irq,
1999 .ack = ack_apic_edge,
2000#ifdef CONFIG_SMP
2001 .set_affinity = set_msi_irq_affinity,
2002#endif
2003 .retrigger = ioapic_retrigger_irq,
589e367f
EB
2004};
2005
f7feaca7 2006int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
3b7d1921
EB
2007{
2008 struct msi_msg msg;
f7feaca7
EB
2009 int irq, ret;
2010 irq = create_irq();
2011 if (irq < 0)
2012 return irq;
2013
3b7d1921 2014 ret = msi_compose_msg(dev, irq, &msg);
f7feaca7
EB
2015 if (ret < 0) {
2016 destroy_irq(irq);
3b7d1921 2017 return ret;
f7feaca7 2018 }
3b7d1921 2019
7fe3730d 2020 set_irq_msi(irq, desc);
3b7d1921
EB
2021 write_msi_msg(irq, &msg);
2022
a460e745 2023 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
3b7d1921 2024
7fe3730d 2025 return 0;
3b7d1921
EB
2026}
2027
2028void arch_teardown_msi_irq(unsigned int irq)
2029{
f7feaca7 2030 destroy_irq(irq);
3b7d1921
EB
2031}
2032
2033#endif /* CONFIG_PCI_MSI */
589e367f 2034
8b955b0d
EB
2035/*
2036 * Hypertransport interrupt support
2037 */
2038#ifdef CONFIG_HT_IRQ
2039
2040#ifdef CONFIG_SMP
2041
2042static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
2043{
ec68307c
EB
2044 struct ht_irq_msg msg;
2045 fetch_ht_irq_msg(irq, &msg);
8b955b0d 2046
ec68307c
EB
2047 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
2048 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
8b955b0d 2049
ec68307c
EB
2050 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
2051 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
8b955b0d 2052
ec68307c 2053 write_ht_irq_msg(irq, &msg);
8b955b0d
EB
2054}
2055
2056static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2057{
dfbffdd8 2058 struct irq_cfg *cfg = irq_cfg + irq;
8b955b0d
EB
2059 unsigned int dest;
2060 cpumask_t tmp;
8b955b0d
EB
2061
2062 cpus_and(tmp, mask, cpu_online_map);
2063 if (cpus_empty(tmp))
5ff5115e 2064 return;
8b955b0d 2065
dfbffdd8 2066 if (assign_irq_vector(irq, mask))
8b955b0d
EB
2067 return;
2068
dfbffdd8 2069 cpus_and(tmp, cfg->domain, mask);
8b955b0d
EB
2070 dest = cpu_mask_to_apicid(tmp);
2071
dfbffdd8 2072 target_ht_irq(irq, dest, cfg->vector);
9f0a5ba5 2073 irq_desc[irq].affinity = mask;
8b955b0d
EB
2074}
2075#endif
2076
c37e108d 2077static struct irq_chip ht_irq_chip = {
8b955b0d
EB
2078 .name = "PCI-HT",
2079 .mask = mask_ht_irq,
2080 .unmask = unmask_ht_irq,
2081 .ack = ack_apic_edge,
2082#ifdef CONFIG_SMP
2083 .set_affinity = set_ht_irq_affinity,
2084#endif
2085 .retrigger = ioapic_retrigger_irq,
2086};
2087
2088int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2089{
dfbffdd8
EB
2090 struct irq_cfg *cfg = irq_cfg + irq;
2091 int err;
c7111c13 2092 cpumask_t tmp;
8b955b0d 2093
dfbffdd8
EB
2094 tmp = TARGET_CPUS;
2095 err = assign_irq_vector(irq, tmp);
2096 if (!err) {
ec68307c 2097 struct ht_irq_msg msg;
8b955b0d 2098 unsigned dest;
8b955b0d 2099
dfbffdd8 2100 cpus_and(tmp, cfg->domain, tmp);
8b955b0d
EB
2101 dest = cpu_mask_to_apicid(tmp);
2102
ec68307c 2103 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
8b955b0d 2104
ec68307c
EB
2105 msg.address_lo =
2106 HT_IRQ_LOW_BASE |
8b955b0d 2107 HT_IRQ_LOW_DEST_ID(dest) |
dfbffdd8 2108 HT_IRQ_LOW_VECTOR(cfg->vector) |
8b955b0d
EB
2109 ((INT_DEST_MODE == 0) ?
2110 HT_IRQ_LOW_DM_PHYSICAL :
2111 HT_IRQ_LOW_DM_LOGICAL) |
2112 HT_IRQ_LOW_RQEOI_EDGE |
2113 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2114 HT_IRQ_LOW_MT_FIXED :
ec68307c
EB
2115 HT_IRQ_LOW_MT_ARBITRATED) |
2116 HT_IRQ_LOW_IRQ_MASKED;
8b955b0d 2117
ec68307c 2118 write_ht_irq_msg(irq, &msg);
8b955b0d 2119
a460e745
IM
2120 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2121 handle_edge_irq, "edge");
8b955b0d 2122 }
dfbffdd8 2123 return err;
8b955b0d
EB
2124}
2125#endif /* CONFIG_HT_IRQ */
2126
1da177e4
LT
2127/* --------------------------------------------------------------------------
2128 ACPI-based IOAPIC Configuration
2129 -------------------------------------------------------------------------- */
2130
888ba6c6 2131#ifdef CONFIG_ACPI
1da177e4
LT
2132
2133#define IO_APIC_MAX_ID 0xFE
2134
1da177e4
LT
2135int __init io_apic_get_redir_entries (int ioapic)
2136{
2137 union IO_APIC_reg_01 reg_01;
2138 unsigned long flags;
2139
2140 spin_lock_irqsave(&ioapic_lock, flags);
2141 reg_01.raw = io_apic_read(ioapic, 1);
2142 spin_unlock_irqrestore(&ioapic_lock, flags);
2143
2144 return reg_01.bits.entries;
2145}
2146
2147
50eca3eb 2148int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
1da177e4 2149{
1da177e4
LT
2150 if (!IO_APIC_IRQ(irq)) {
2151 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2152 ioapic);
2153 return -EINVAL;
2154 }
2155
550f2299
EB
2156 /*
2157 * IRQs < 16 are already in the irq_2_pin[] map
2158 */
2159 if (irq >= 16)
2160 add_pin_to_irq(irq, ioapic, pin);
2161
a8c8a367 2162 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
1da177e4
LT
2163
2164 return 0;
2165}
2166
888ba6c6 2167#endif /* CONFIG_ACPI */
1da177e4
LT
2168
2169
2170/*
2171 * This function currently is only a helper for the i386 smp boot process where
2172 * we need to reprogram the ioredtbls to cater for the cpus which have come online
2173 * so mask in all cases should simply be TARGET_CPUS
2174 */
54d5d424 2175#ifdef CONFIG_SMP
1da177e4
LT
2176void __init setup_ioapic_dest(void)
2177{
2178 int pin, ioapic, irq, irq_entry;
2179
2180 if (skip_ioapic_setup == 1)
2181 return;
2182
2183 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
2184 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
2185 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
2186 if (irq_entry == -1)
2187 continue;
2188 irq = pin_2_irq(irq_entry, ioapic, pin);
ad892f5e
YL
2189
2190 /* setup_IO_APIC_irqs could fail to get vector for some device
2191 * when you have too many devices, because at that time only boot
2192 * cpu is online.
2193 */
13a79503 2194 if (!irq_cfg[irq].vector)
a8c8a367
EB
2195 setup_IO_APIC_irq(ioapic, pin, irq,
2196 irq_trigger(irq_entry),
2197 irq_polarity(irq_entry));
ad892f5e
YL
2198 else
2199 set_ioapic_affinity_irq(irq, TARGET_CPUS);
1da177e4
LT
2200 }
2201
2202 }
2203}
54d5d424 2204#endif
This page took 0.432226 seconds and 5 git commands to generate.