x86: print out irq nr for msi/ht, v3
[linux-2.6/mini2440.git] / arch / x86 / kernel / io_apic.c
blob4ee270d30358a894a3bbd70c432766d199d230f5
1 /*
2 * Intel IO-APIC support for multi-Pentium hosts.
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
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>
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
23 #include <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/pci.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/compiler.h>
31 #include <linux/acpi.h>
32 #include <linux/module.h>
33 #include <linux/sysdev.h>
34 #include <linux/msi.h>
35 #include <linux/htirq.h>
36 #include <linux/freezer.h>
37 #include <linux/kthread.h>
38 #include <linux/jiffies.h> /* time_after() */
39 #ifdef CONFIG_ACPI
40 #include <acpi/acpi_bus.h>
41 #endif
42 #include <linux/bootmem.h>
43 #include <linux/dmar.h>
44 #include <linux/hpet.h>
46 #include <asm/idle.h>
47 #include <asm/io.h>
48 #include <asm/smp.h>
49 #include <asm/desc.h>
50 #include <asm/proto.h>
51 #include <asm/acpi.h>
52 #include <asm/dma.h>
53 #include <asm/timer.h>
54 #include <asm/i8259.h>
55 #include <asm/nmi.h>
56 #include <asm/msidef.h>
57 #include <asm/hypertransport.h>
58 #include <asm/setup.h>
59 #include <asm/irq_remapping.h>
60 #include <asm/hpet.h>
62 #include <mach_ipi.h>
63 #include <mach_apic.h>
64 #include <mach_apicdef.h>
66 #define __apicdebuginit(type) static type __init
69 * Is the SiS APIC rmw bug present ?
70 * -1 = don't know, 0 = no, 1 = yes
72 int sis_apic_bug = -1;
74 static DEFINE_SPINLOCK(ioapic_lock);
75 static DEFINE_SPINLOCK(vector_lock);
78 * # of IRQ routing registers
80 int nr_ioapic_registers[MAX_IO_APICS];
82 /* I/O APIC entries */
83 struct mp_config_ioapic mp_ioapics[MAX_IO_APICS];
84 int nr_ioapics;
86 /* MP IRQ source entries */
87 struct mp_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
89 /* # of MP IRQ source entries */
90 int mp_irq_entries;
92 #if defined (CONFIG_MCA) || defined (CONFIG_EISA)
93 int mp_bus_id_to_type[MAX_MP_BUSSES];
94 #endif
96 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
98 int skip_ioapic_setup;
100 static int __init parse_noapic(char *str)
102 /* disable IO-APIC */
103 disable_ioapic_setup();
104 return 0;
106 early_param("noapic", parse_noapic);
108 struct irq_cfg;
109 struct irq_pin_list;
110 struct irq_cfg {
111 unsigned int irq;
112 #ifdef CONFIG_HAVE_SPARSE_IRQ
113 struct irq_cfg *next;
114 #endif
115 struct irq_pin_list *irq_2_pin;
116 cpumask_t domain;
117 cpumask_t old_domain;
118 unsigned move_cleanup_count;
119 u8 vector;
120 u8 move_in_progress : 1;
123 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
124 static struct irq_cfg irq_cfg_legacy[] __initdata = {
125 [0] = { .irq = 0, .domain = CPU_MASK_ALL, .vector = IRQ0_VECTOR, },
126 [1] = { .irq = 1, .domain = CPU_MASK_ALL, .vector = IRQ1_VECTOR, },
127 [2] = { .irq = 2, .domain = CPU_MASK_ALL, .vector = IRQ2_VECTOR, },
128 [3] = { .irq = 3, .domain = CPU_MASK_ALL, .vector = IRQ3_VECTOR, },
129 [4] = { .irq = 4, .domain = CPU_MASK_ALL, .vector = IRQ4_VECTOR, },
130 [5] = { .irq = 5, .domain = CPU_MASK_ALL, .vector = IRQ5_VECTOR, },
131 [6] = { .irq = 6, .domain = CPU_MASK_ALL, .vector = IRQ6_VECTOR, },
132 [7] = { .irq = 7, .domain = CPU_MASK_ALL, .vector = IRQ7_VECTOR, },
133 [8] = { .irq = 8, .domain = CPU_MASK_ALL, .vector = IRQ8_VECTOR, },
134 [9] = { .irq = 9, .domain = CPU_MASK_ALL, .vector = IRQ9_VECTOR, },
135 [10] = { .irq = 10, .domain = CPU_MASK_ALL, .vector = IRQ10_VECTOR, },
136 [11] = { .irq = 11, .domain = CPU_MASK_ALL, .vector = IRQ11_VECTOR, },
137 [12] = { .irq = 12, .domain = CPU_MASK_ALL, .vector = IRQ12_VECTOR, },
138 [13] = { .irq = 13, .domain = CPU_MASK_ALL, .vector = IRQ13_VECTOR, },
139 [14] = { .irq = 14, .domain = CPU_MASK_ALL, .vector = IRQ14_VECTOR, },
140 [15] = { .irq = 15, .domain = CPU_MASK_ALL, .vector = IRQ15_VECTOR, },
143 static struct irq_cfg irq_cfg_init = { .irq = -1U, };
145 static void init_one_irq_cfg(struct irq_cfg *cfg)
147 memcpy(cfg, &irq_cfg_init, sizeof(struct irq_cfg));
150 static struct irq_cfg *irq_cfgx;
152 #ifdef CONFIG_HAVE_SPARSE_IRQ
154 * Protect the irq_cfgx_free freelist:
156 static DEFINE_SPINLOCK(irq_cfg_lock);
158 static struct irq_cfg *irq_cfgx_free;
159 #endif
161 static void __init init_work(void *data)
163 struct dyn_array *da = data;
164 struct irq_cfg *cfg;
165 int legacy_count;
166 int i;
168 cfg = *da->name;
170 memcpy(cfg, irq_cfg_legacy, sizeof(irq_cfg_legacy));
172 legacy_count = ARRAY_SIZE(irq_cfg_legacy);
173 for (i = legacy_count; i < *da->nr; i++)
174 init_one_irq_cfg(&cfg[i]);
176 #ifdef CONFIG_HAVE_SPARSE_IRQ
177 for (i = 1; i < *da->nr; i++)
178 cfg[i-1].next = &cfg[i];
180 irq_cfgx_free = &irq_cfgx[legacy_count];
181 irq_cfgx[legacy_count - 1].next = NULL;
182 #endif
185 #ifdef CONFIG_HAVE_SPARSE_IRQ
186 /* need to be biger than size of irq_cfg_legacy */
187 static int nr_irq_cfg = 32;
189 static int __init parse_nr_irq_cfg(char *arg)
191 if (arg) {
192 nr_irq_cfg = simple_strtoul(arg, NULL, 0);
193 if (nr_irq_cfg < 32)
194 nr_irq_cfg = 32;
196 return 0;
199 early_param("nr_irq_cfg", parse_nr_irq_cfg);
201 #define for_each_irq_cfg(irqX, cfg) \
202 for (cfg = irq_cfgx, irqX = cfg->irq; cfg; cfg = cfg->next, irqX = cfg ? cfg->irq : -1U)
205 DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irq_cfg, PAGE_SIZE, init_work);
207 static struct irq_cfg *irq_cfg(unsigned int irq)
209 struct irq_cfg *cfg;
211 cfg = irq_cfgx;
212 while (cfg) {
213 if (cfg->irq == irq)
214 return cfg;
216 cfg = cfg->next;
219 return NULL;
222 static struct irq_cfg *irq_cfg_alloc(unsigned int irq)
224 struct irq_cfg *cfg, *cfg_pri;
225 unsigned long flags;
226 int count = 0;
227 int i;
229 cfg_pri = cfg = irq_cfgx;
230 while (cfg) {
231 if (cfg->irq == irq)
232 return cfg;
234 cfg_pri = cfg;
235 cfg = cfg->next;
236 count++;
239 spin_lock_irqsave(&irq_cfg_lock, flags);
240 if (!irq_cfgx_free) {
241 unsigned long phys;
242 unsigned long total_bytes;
244 * we run out of pre-allocate ones, allocate more
246 printk(KERN_DEBUG "try to get more irq_cfg %d\n", nr_irq_cfg);
248 total_bytes = sizeof(struct irq_cfg) * nr_irq_cfg;
249 if (after_bootmem)
250 cfg = kzalloc(total_bytes, GFP_ATOMIC);
251 else
252 cfg = __alloc_bootmem_nopanic(total_bytes, PAGE_SIZE, 0);
254 if (!cfg)
255 panic("please boot with nr_irq_cfg= %d\n", count * 2);
257 phys = __pa(cfg);
258 printk(KERN_DEBUG "irq_irq ==> [%#lx - %#lx]\n", phys, phys + total_bytes);
260 for (i = 0; i < nr_irq_cfg; i++)
261 init_one_irq_cfg(&cfg[i]);
263 for (i = 1; i < nr_irq_cfg; i++)
264 cfg[i-1].next = &cfg[i];
266 irq_cfgx_free = cfg;
269 cfg = irq_cfgx_free;
270 irq_cfgx_free = irq_cfgx_free->next;
271 cfg->next = NULL;
272 if (cfg_pri)
273 cfg_pri->next = cfg;
274 else
275 irq_cfgx = cfg;
276 cfg->irq = irq;
278 spin_unlock_irqrestore(&irq_cfg_lock, flags);
280 return cfg;
282 #else
284 #define for_each_irq_cfg(irq, cfg) \
285 for (irq = 0, cfg = &irq_cfgx[irq]; irq < nr_irqs; irq++, cfg = &irq_cfgx[irq])
287 DEFINE_DYN_ARRAY(irq_cfgx, sizeof(struct irq_cfg), nr_irqs, PAGE_SIZE, init_work);
289 struct irq_cfg *irq_cfg(unsigned int irq)
291 if (irq < nr_irqs)
292 return &irq_cfgx[irq];
294 return NULL;
296 struct irq_cfg *irq_cfg_alloc(unsigned int irq)
298 return irq_cfg(irq);
301 #endif
303 * This is performance-critical, we want to do it O(1)
305 * the indexing order of this array favors 1:1 mappings
306 * between pins and IRQs.
309 struct irq_pin_list {
310 int apic, pin;
311 struct irq_pin_list *next;
314 static struct irq_pin_list *irq_2_pin_head;
315 /* fill one page ? */
316 static int nr_irq_2_pin = 0x100;
317 static struct irq_pin_list *irq_2_pin_ptr;
318 static void __init irq_2_pin_init_work(void *data)
320 struct dyn_array *da = data;
321 struct irq_pin_list *pin;
322 int i;
324 pin = *da->name;
326 for (i = 1; i < *da->nr; i++)
327 pin[i-1].next = &pin[i];
329 irq_2_pin_ptr = &pin[0];
331 DEFINE_DYN_ARRAY(irq_2_pin_head, sizeof(struct irq_pin_list), nr_irq_2_pin, PAGE_SIZE, irq_2_pin_init_work);
333 static struct irq_pin_list *get_one_free_irq_2_pin(void)
335 struct irq_pin_list *pin;
336 int i;
338 pin = irq_2_pin_ptr;
340 if (pin) {
341 irq_2_pin_ptr = pin->next;
342 pin->next = NULL;
343 return pin;
347 * we run out of pre-allocate ones, allocate more
349 printk(KERN_DEBUG "try to get more irq_2_pin %d\n", nr_irq_2_pin);
351 if (after_bootmem)
352 pin = kzalloc(sizeof(struct irq_pin_list)*nr_irq_2_pin,
353 GFP_ATOMIC);
354 else
355 pin = __alloc_bootmem_nopanic(sizeof(struct irq_pin_list) *
356 nr_irq_2_pin, PAGE_SIZE, 0);
358 if (!pin)
359 panic("can not get more irq_2_pin\n");
361 for (i = 1; i < nr_irq_2_pin; i++)
362 pin[i-1].next = &pin[i];
364 irq_2_pin_ptr = pin->next;
365 pin->next = NULL;
367 return pin;
370 struct io_apic {
371 unsigned int index;
372 unsigned int unused[3];
373 unsigned int data;
376 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
378 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
379 + (mp_ioapics[idx].mp_apicaddr & ~PAGE_MASK);
382 static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
384 struct io_apic __iomem *io_apic = io_apic_base(apic);
385 writel(reg, &io_apic->index);
386 return readl(&io_apic->data);
389 static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
391 struct io_apic __iomem *io_apic = io_apic_base(apic);
392 writel(reg, &io_apic->index);
393 writel(value, &io_apic->data);
397 * Re-write a value: to be used for read-modify-write
398 * cycles where the read already set up the index register.
400 * Older SiS APIC requires we rewrite the index register
402 static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
404 struct io_apic __iomem *io_apic = io_apic_base(apic);
405 if (sis_apic_bug)
406 writel(reg, &io_apic->index);
407 writel(value, &io_apic->data);
410 static bool io_apic_level_ack_pending(unsigned int irq)
412 struct irq_pin_list *entry;
413 unsigned long flags;
414 struct irq_cfg *cfg = irq_cfg(irq);
416 spin_lock_irqsave(&ioapic_lock, flags);
417 entry = cfg->irq_2_pin;
418 for (;;) {
419 unsigned int reg;
420 int pin;
422 if (!entry)
423 break;
424 pin = entry->pin;
425 reg = io_apic_read(entry->apic, 0x10 + pin*2);
426 /* Is the remote IRR bit set? */
427 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
428 spin_unlock_irqrestore(&ioapic_lock, flags);
429 return true;
431 if (!entry->next)
432 break;
433 entry = entry->next;
435 spin_unlock_irqrestore(&ioapic_lock, flags);
437 return false;
440 union entry_union {
441 struct { u32 w1, w2; };
442 struct IO_APIC_route_entry entry;
445 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
447 union entry_union eu;
448 unsigned long flags;
449 spin_lock_irqsave(&ioapic_lock, flags);
450 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
451 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
452 spin_unlock_irqrestore(&ioapic_lock, flags);
453 return eu.entry;
457 * When we write a new IO APIC routing entry, we need to write the high
458 * word first! If the mask bit in the low word is clear, we will enable
459 * the interrupt, and we need to make sure the entry is fully populated
460 * before that happens.
462 static void
463 __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
465 union entry_union eu;
466 eu.entry = e;
467 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
468 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
471 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
473 unsigned long flags;
474 spin_lock_irqsave(&ioapic_lock, flags);
475 __ioapic_write_entry(apic, pin, e);
476 spin_unlock_irqrestore(&ioapic_lock, flags);
480 * When we mask an IO APIC routing entry, we need to write the low
481 * word first, in order to set the mask bit before we change the
482 * high bits!
484 static void ioapic_mask_entry(int apic, int pin)
486 unsigned long flags;
487 union entry_union eu = { .entry.mask = 1 };
489 spin_lock_irqsave(&ioapic_lock, flags);
490 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
491 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
492 spin_unlock_irqrestore(&ioapic_lock, flags);
495 #ifdef CONFIG_SMP
496 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, u8 vector)
498 int apic, pin;
499 struct irq_cfg *cfg;
500 struct irq_pin_list *entry;
502 cfg = irq_cfg(irq);
503 entry = cfg->irq_2_pin;
504 for (;;) {
505 unsigned int reg;
507 if (!entry)
508 break;
510 apic = entry->apic;
511 pin = entry->pin;
512 #ifdef CONFIG_INTR_REMAP
514 * With interrupt-remapping, destination information comes
515 * from interrupt-remapping table entry.
517 if (!irq_remapped(irq))
518 io_apic_write(apic, 0x11 + pin*2, dest);
519 #else
520 io_apic_write(apic, 0x11 + pin*2, dest);
521 #endif
522 reg = io_apic_read(apic, 0x10 + pin*2);
523 reg &= ~IO_APIC_REDIR_VECTOR_MASK;
524 reg |= vector;
525 io_apic_modify(apic, 0x10 + pin*2, reg);
526 if (!entry->next)
527 break;
528 entry = entry->next;
532 static int assign_irq_vector(int irq, cpumask_t mask);
534 static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
536 struct irq_cfg *cfg;
537 unsigned long flags;
538 unsigned int dest;
539 cpumask_t tmp;
540 struct irq_desc *desc;
542 cpus_and(tmp, mask, cpu_online_map);
543 if (cpus_empty(tmp))
544 return;
546 cfg = irq_cfg(irq);
547 if (assign_irq_vector(irq, mask))
548 return;
550 cpus_and(tmp, cfg->domain, mask);
551 dest = cpu_mask_to_apicid(tmp);
553 * Only the high 8 bits are valid.
555 dest = SET_APIC_LOGICAL_ID(dest);
557 desc = irq_to_desc(irq);
558 spin_lock_irqsave(&ioapic_lock, flags);
559 __target_IO_APIC_irq(irq, dest, cfg->vector);
560 desc->affinity = mask;
561 spin_unlock_irqrestore(&ioapic_lock, flags);
563 #endif /* CONFIG_SMP */
566 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
567 * shared ISA-space IRQs, so we have to support them. We are super
568 * fast in the common case, and fast for shared ISA-space IRQs.
570 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
572 struct irq_cfg *cfg;
573 struct irq_pin_list *entry;
575 /* first time to refer irq_cfg, so with new */
576 cfg = irq_cfg_alloc(irq);
577 entry = cfg->irq_2_pin;
578 if (!entry) {
579 entry = get_one_free_irq_2_pin();
580 cfg->irq_2_pin = entry;
581 entry->apic = apic;
582 entry->pin = pin;
583 return;
586 while (entry->next) {
587 /* not again, please */
588 if (entry->apic == apic && entry->pin == pin)
589 return;
591 entry = entry->next;
594 entry->next = get_one_free_irq_2_pin();
595 entry = entry->next;
596 entry->apic = apic;
597 entry->pin = pin;
601 * Reroute an IRQ to a different pin.
603 static void __init replace_pin_at_irq(unsigned int irq,
604 int oldapic, int oldpin,
605 int newapic, int newpin)
607 struct irq_cfg *cfg = irq_cfg(irq);
608 struct irq_pin_list *entry = cfg->irq_2_pin;
609 int replaced = 0;
611 while (entry) {
612 if (entry->apic == oldapic && entry->pin == oldpin) {
613 entry->apic = newapic;
614 entry->pin = newpin;
615 replaced = 1;
616 /* every one is different, right? */
617 break;
619 entry = entry->next;
622 /* why? call replace before add? */
623 if (!replaced)
624 add_pin_to_irq(irq, newapic, newpin);
627 static inline void io_apic_modify_irq(unsigned int irq,
628 int mask_and, int mask_or,
629 void (*final)(struct irq_pin_list *entry))
631 int pin;
632 struct irq_cfg *cfg;
633 struct irq_pin_list *entry;
635 cfg = irq_cfg(irq);
636 for (entry = cfg->irq_2_pin; entry != NULL; entry = entry->next) {
637 unsigned int reg;
638 pin = entry->pin;
639 reg = io_apic_read(entry->apic, 0x10 + pin * 2);
640 reg &= mask_and;
641 reg |= mask_or;
642 io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
643 if (final)
644 final(entry);
648 static void __unmask_IO_APIC_irq(unsigned int irq)
650 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED, 0, NULL);
653 #ifdef CONFIG_X86_64
654 void io_apic_sync(struct irq_pin_list *entry)
657 * Synchronize the IO-APIC and the CPU by doing
658 * a dummy read from the IO-APIC
660 struct io_apic __iomem *io_apic;
661 io_apic = io_apic_base(entry->apic);
662 readl(&io_apic->data);
665 static void __mask_IO_APIC_irq(unsigned int irq)
667 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
669 #else /* CONFIG_X86_32 */
670 static void __mask_IO_APIC_irq(unsigned int irq)
672 io_apic_modify_irq(irq, ~0, IO_APIC_REDIR_MASKED, NULL);
675 static void __mask_and_edge_IO_APIC_irq(unsigned int irq)
677 io_apic_modify_irq(irq, ~IO_APIC_REDIR_LEVEL_TRIGGER,
678 IO_APIC_REDIR_MASKED, NULL);
681 static void __unmask_and_level_IO_APIC_irq(unsigned int irq)
683 io_apic_modify_irq(irq, ~IO_APIC_REDIR_MASKED,
684 IO_APIC_REDIR_LEVEL_TRIGGER, NULL);
686 #endif /* CONFIG_X86_32 */
688 static void mask_IO_APIC_irq (unsigned int irq)
690 unsigned long flags;
692 spin_lock_irqsave(&ioapic_lock, flags);
693 __mask_IO_APIC_irq(irq);
694 spin_unlock_irqrestore(&ioapic_lock, flags);
697 static void unmask_IO_APIC_irq (unsigned int irq)
699 unsigned long flags;
701 spin_lock_irqsave(&ioapic_lock, flags);
702 __unmask_IO_APIC_irq(irq);
703 spin_unlock_irqrestore(&ioapic_lock, flags);
706 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
708 struct IO_APIC_route_entry entry;
710 /* Check delivery_mode to be sure we're not clearing an SMI pin */
711 entry = ioapic_read_entry(apic, pin);
712 if (entry.delivery_mode == dest_SMI)
713 return;
715 * Disable it in the IO-APIC irq-routing table:
717 ioapic_mask_entry(apic, pin);
720 static void clear_IO_APIC (void)
722 int apic, pin;
724 for (apic = 0; apic < nr_ioapics; apic++)
725 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
726 clear_IO_APIC_pin(apic, pin);
729 #if !defined(CONFIG_SMP) && defined(CONFIG_X86_32)
730 void send_IPI_self(int vector)
732 unsigned int cfg;
735 * Wait for idle.
737 apic_wait_icr_idle();
738 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
740 * Send the IPI. The write to APIC_ICR fires this off.
742 apic_write(APIC_ICR, cfg);
744 #endif /* !CONFIG_SMP && CONFIG_X86_32*/
746 #ifdef CONFIG_X86_32
748 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
749 * specific CPU-side IRQs.
752 #define MAX_PIRQS 8
753 static int pirq_entries [MAX_PIRQS];
754 static int pirqs_enabled;
756 static int __init ioapic_pirq_setup(char *str)
758 int i, max;
759 int ints[MAX_PIRQS+1];
761 get_options(str, ARRAY_SIZE(ints), ints);
763 for (i = 0; i < MAX_PIRQS; i++)
764 pirq_entries[i] = -1;
766 pirqs_enabled = 1;
767 apic_printk(APIC_VERBOSE, KERN_INFO
768 "PIRQ redirection, working around broken MP-BIOS.\n");
769 max = MAX_PIRQS;
770 if (ints[0] < MAX_PIRQS)
771 max = ints[0];
773 for (i = 0; i < max; i++) {
774 apic_printk(APIC_VERBOSE, KERN_DEBUG
775 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
777 * PIRQs are mapped upside down, usually.
779 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
781 return 1;
784 __setup("pirq=", ioapic_pirq_setup);
785 #endif /* CONFIG_X86_32 */
787 #ifdef CONFIG_INTR_REMAP
788 /* I/O APIC RTE contents at the OS boot up */
789 static struct IO_APIC_route_entry *early_ioapic_entries[MAX_IO_APICS];
792 * Saves and masks all the unmasked IO-APIC RTE's
794 int save_mask_IO_APIC_setup(void)
796 union IO_APIC_reg_01 reg_01;
797 unsigned long flags;
798 int apic, pin;
801 * The number of IO-APIC IRQ registers (== #pins):
803 for (apic = 0; apic < nr_ioapics; apic++) {
804 spin_lock_irqsave(&ioapic_lock, flags);
805 reg_01.raw = io_apic_read(apic, 1);
806 spin_unlock_irqrestore(&ioapic_lock, flags);
807 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
810 for (apic = 0; apic < nr_ioapics; apic++) {
811 early_ioapic_entries[apic] =
812 kzalloc(sizeof(struct IO_APIC_route_entry) *
813 nr_ioapic_registers[apic], GFP_KERNEL);
814 if (!early_ioapic_entries[apic])
815 goto nomem;
818 for (apic = 0; apic < nr_ioapics; apic++)
819 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
820 struct IO_APIC_route_entry entry;
822 entry = early_ioapic_entries[apic][pin] =
823 ioapic_read_entry(apic, pin);
824 if (!entry.mask) {
825 entry.mask = 1;
826 ioapic_write_entry(apic, pin, entry);
830 return 0;
832 nomem:
833 while (apic >= 0)
834 kfree(early_ioapic_entries[apic--]);
835 memset(early_ioapic_entries, 0,
836 ARRAY_SIZE(early_ioapic_entries));
838 return -ENOMEM;
841 void restore_IO_APIC_setup(void)
843 int apic, pin;
845 for (apic = 0; apic < nr_ioapics; apic++) {
846 if (!early_ioapic_entries[apic])
847 break;
848 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
849 ioapic_write_entry(apic, pin,
850 early_ioapic_entries[apic][pin]);
851 kfree(early_ioapic_entries[apic]);
852 early_ioapic_entries[apic] = NULL;
856 void reinit_intr_remapped_IO_APIC(int intr_remapping)
859 * for now plain restore of previous settings.
860 * TBD: In the case of OS enabling interrupt-remapping,
861 * IO-APIC RTE's need to be setup to point to interrupt-remapping
862 * table entries. for now, do a plain restore, and wait for
863 * the setup_IO_APIC_irqs() to do proper initialization.
865 restore_IO_APIC_setup();
867 #endif
870 * Find the IRQ entry number of a certain pin.
872 static int find_irq_entry(int apic, int pin, int type)
874 int i;
876 for (i = 0; i < mp_irq_entries; i++)
877 if (mp_irqs[i].mp_irqtype == type &&
878 (mp_irqs[i].mp_dstapic == mp_ioapics[apic].mp_apicid ||
879 mp_irqs[i].mp_dstapic == MP_APIC_ALL) &&
880 mp_irqs[i].mp_dstirq == pin)
881 return i;
883 return -1;
887 * Find the pin to which IRQ[irq] (ISA) is connected
889 static int __init find_isa_irq_pin(int irq, int type)
891 int i;
893 for (i = 0; i < mp_irq_entries; i++) {
894 int lbus = mp_irqs[i].mp_srcbus;
896 if (test_bit(lbus, mp_bus_not_pci) &&
897 (mp_irqs[i].mp_irqtype == type) &&
898 (mp_irqs[i].mp_srcbusirq == irq))
900 return mp_irqs[i].mp_dstirq;
902 return -1;
905 static int __init find_isa_irq_apic(int irq, int type)
907 int i;
909 for (i = 0; i < mp_irq_entries; i++) {
910 int lbus = mp_irqs[i].mp_srcbus;
912 if (test_bit(lbus, mp_bus_not_pci) &&
913 (mp_irqs[i].mp_irqtype == type) &&
914 (mp_irqs[i].mp_srcbusirq == irq))
915 break;
917 if (i < mp_irq_entries) {
918 int apic;
919 for(apic = 0; apic < nr_ioapics; apic++) {
920 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic)
921 return apic;
925 return -1;
929 * Find a specific PCI IRQ entry.
930 * Not an __init, possibly needed by modules
932 static int pin_2_irq(int idx, int apic, int pin);
934 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
936 int apic, i, best_guess = -1;
938 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
939 bus, slot, pin);
940 if (test_bit(bus, mp_bus_not_pci)) {
941 apic_printk(APIC_VERBOSE, "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
942 return -1;
944 for (i = 0; i < mp_irq_entries; i++) {
945 int lbus = mp_irqs[i].mp_srcbus;
947 for (apic = 0; apic < nr_ioapics; apic++)
948 if (mp_ioapics[apic].mp_apicid == mp_irqs[i].mp_dstapic ||
949 mp_irqs[i].mp_dstapic == MP_APIC_ALL)
950 break;
952 if (!test_bit(lbus, mp_bus_not_pci) &&
953 !mp_irqs[i].mp_irqtype &&
954 (bus == lbus) &&
955 (slot == ((mp_irqs[i].mp_srcbusirq >> 2) & 0x1f))) {
956 int irq = pin_2_irq(i,apic,mp_irqs[i].mp_dstirq);
958 if (!(apic || IO_APIC_IRQ(irq)))
959 continue;
961 if (pin == (mp_irqs[i].mp_srcbusirq & 3))
962 return irq;
964 * Use the first all-but-pin matching entry as a
965 * best-guess fuzzy result for broken mptables.
967 if (best_guess < 0)
968 best_guess = irq;
971 return best_guess;
974 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
976 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
978 * EISA Edge/Level control register, ELCR
980 static int EISA_ELCR(unsigned int irq)
982 if (irq < 16) {
983 unsigned int port = 0x4d0 + (irq >> 3);
984 return (inb(port) >> (irq & 7)) & 1;
986 apic_printk(APIC_VERBOSE, KERN_INFO
987 "Broken MPtable reports ISA irq %d\n", irq);
988 return 0;
991 #endif
993 /* ISA interrupts are always polarity zero edge triggered,
994 * when listed as conforming in the MP table. */
996 #define default_ISA_trigger(idx) (0)
997 #define default_ISA_polarity(idx) (0)
999 /* EISA interrupts are always polarity zero and can be edge or level
1000 * trigger depending on the ELCR value. If an interrupt is listed as
1001 * EISA conforming in the MP table, that means its trigger type must
1002 * be read in from the ELCR */
1004 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mp_srcbusirq))
1005 #define default_EISA_polarity(idx) default_ISA_polarity(idx)
1007 /* PCI interrupts are always polarity one level triggered,
1008 * when listed as conforming in the MP table. */
1010 #define default_PCI_trigger(idx) (1)
1011 #define default_PCI_polarity(idx) (1)
1013 /* MCA interrupts are always polarity zero level triggered,
1014 * when listed as conforming in the MP table. */
1016 #define default_MCA_trigger(idx) (1)
1017 #define default_MCA_polarity(idx) default_ISA_polarity(idx)
1019 static int MPBIOS_polarity(int idx)
1021 int bus = mp_irqs[idx].mp_srcbus;
1022 int polarity;
1025 * Determine IRQ line polarity (high active or low active):
1027 switch (mp_irqs[idx].mp_irqflag & 3)
1029 case 0: /* conforms, ie. bus-type dependent polarity */
1030 if (test_bit(bus, mp_bus_not_pci))
1031 polarity = default_ISA_polarity(idx);
1032 else
1033 polarity = default_PCI_polarity(idx);
1034 break;
1035 case 1: /* high active */
1037 polarity = 0;
1038 break;
1040 case 2: /* reserved */
1042 printk(KERN_WARNING "broken BIOS!!\n");
1043 polarity = 1;
1044 break;
1046 case 3: /* low active */
1048 polarity = 1;
1049 break;
1051 default: /* invalid */
1053 printk(KERN_WARNING "broken BIOS!!\n");
1054 polarity = 1;
1055 break;
1058 return polarity;
1061 static int MPBIOS_trigger(int idx)
1063 int bus = mp_irqs[idx].mp_srcbus;
1064 int trigger;
1067 * Determine IRQ trigger mode (edge or level sensitive):
1069 switch ((mp_irqs[idx].mp_irqflag>>2) & 3)
1071 case 0: /* conforms, ie. bus-type dependent */
1072 if (test_bit(bus, mp_bus_not_pci))
1073 trigger = default_ISA_trigger(idx);
1074 else
1075 trigger = default_PCI_trigger(idx);
1076 #if defined(CONFIG_EISA) || defined(CONFIG_MCA)
1077 switch (mp_bus_id_to_type[bus]) {
1078 case MP_BUS_ISA: /* ISA pin */
1080 /* set before the switch */
1081 break;
1083 case MP_BUS_EISA: /* EISA pin */
1085 trigger = default_EISA_trigger(idx);
1086 break;
1088 case MP_BUS_PCI: /* PCI pin */
1090 /* set before the switch */
1091 break;
1093 case MP_BUS_MCA: /* MCA pin */
1095 trigger = default_MCA_trigger(idx);
1096 break;
1098 default:
1100 printk(KERN_WARNING "broken BIOS!!\n");
1101 trigger = 1;
1102 break;
1105 #endif
1106 break;
1107 case 1: /* edge */
1109 trigger = 0;
1110 break;
1112 case 2: /* reserved */
1114 printk(KERN_WARNING "broken BIOS!!\n");
1115 trigger = 1;
1116 break;
1118 case 3: /* level */
1120 trigger = 1;
1121 break;
1123 default: /* invalid */
1125 printk(KERN_WARNING "broken BIOS!!\n");
1126 trigger = 0;
1127 break;
1130 return trigger;
1133 static inline int irq_polarity(int idx)
1135 return MPBIOS_polarity(idx);
1138 static inline int irq_trigger(int idx)
1140 return MPBIOS_trigger(idx);
1143 int (*ioapic_renumber_irq)(int ioapic, int irq);
1144 static int pin_2_irq(int idx, int apic, int pin)
1146 int irq, i;
1147 int bus = mp_irqs[idx].mp_srcbus;
1150 * Debugging check, we are in big trouble if this message pops up!
1152 if (mp_irqs[idx].mp_dstirq != pin)
1153 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1155 if (test_bit(bus, mp_bus_not_pci)) {
1156 irq = mp_irqs[idx].mp_srcbusirq;
1157 } else {
1159 * PCI IRQs are mapped in order
1161 i = irq = 0;
1162 while (i < apic)
1163 irq += nr_ioapic_registers[i++];
1164 irq += pin;
1166 * For MPS mode, so far only needed by ES7000 platform
1168 if (ioapic_renumber_irq)
1169 irq = ioapic_renumber_irq(apic, irq);
1172 #ifdef CONFIG_X86_32
1174 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1176 if ((pin >= 16) && (pin <= 23)) {
1177 if (pirq_entries[pin-16] != -1) {
1178 if (!pirq_entries[pin-16]) {
1179 apic_printk(APIC_VERBOSE, KERN_DEBUG
1180 "disabling PIRQ%d\n", pin-16);
1181 } else {
1182 irq = pirq_entries[pin-16];
1183 apic_printk(APIC_VERBOSE, KERN_DEBUG
1184 "using PIRQ%d -> IRQ %d\n",
1185 pin-16, irq);
1189 #endif
1191 return irq;
1194 void lock_vector_lock(void)
1196 /* Used to the online set of cpus does not change
1197 * during assign_irq_vector.
1199 spin_lock(&vector_lock);
1202 void unlock_vector_lock(void)
1204 spin_unlock(&vector_lock);
1207 static int __assign_irq_vector(int irq, cpumask_t mask)
1210 * NOTE! The local APIC isn't very good at handling
1211 * multiple interrupts at the same interrupt level.
1212 * As the interrupt level is determined by taking the
1213 * vector number and shifting that right by 4, we
1214 * want to spread these out a bit so that they don't
1215 * all fall in the same interrupt level.
1217 * Also, we've got to be careful not to trash gate
1218 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1220 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
1221 unsigned int old_vector;
1222 int cpu;
1223 struct irq_cfg *cfg;
1225 cfg = irq_cfg(irq);
1227 /* Only try and allocate irqs on cpus that are present */
1228 cpus_and(mask, mask, cpu_online_map);
1230 if ((cfg->move_in_progress) || cfg->move_cleanup_count)
1231 return -EBUSY;
1233 old_vector = cfg->vector;
1234 if (old_vector) {
1235 cpumask_t tmp;
1236 cpus_and(tmp, cfg->domain, mask);
1237 if (!cpus_empty(tmp))
1238 return 0;
1241 for_each_cpu_mask_nr(cpu, mask) {
1242 cpumask_t domain, new_mask;
1243 int new_cpu;
1244 int vector, offset;
1246 domain = vector_allocation_domain(cpu);
1247 cpus_and(new_mask, domain, cpu_online_map);
1249 vector = current_vector;
1250 offset = current_offset;
1251 next:
1252 vector += 8;
1253 if (vector >= first_system_vector) {
1254 /* If we run out of vectors on large boxen, must share them. */
1255 offset = (offset + 1) % 8;
1256 vector = FIRST_DEVICE_VECTOR + offset;
1258 if (unlikely(current_vector == vector))
1259 continue;
1260 #ifdef CONFIG_X86_64
1261 if (vector == IA32_SYSCALL_VECTOR)
1262 goto next;
1263 #else
1264 if (vector == SYSCALL_VECTOR)
1265 goto next;
1266 #endif
1267 for_each_cpu_mask_nr(new_cpu, new_mask)
1268 if (per_cpu(vector_irq, new_cpu)[vector] != -1)
1269 goto next;
1270 /* Found one! */
1271 current_vector = vector;
1272 current_offset = offset;
1273 if (old_vector) {
1274 cfg->move_in_progress = 1;
1275 cfg->old_domain = cfg->domain;
1277 for_each_cpu_mask_nr(new_cpu, new_mask)
1278 per_cpu(vector_irq, new_cpu)[vector] = irq;
1279 cfg->vector = vector;
1280 cfg->domain = domain;
1281 return 0;
1283 return -ENOSPC;
1286 static int assign_irq_vector(int irq, cpumask_t mask)
1288 int err;
1289 unsigned long flags;
1291 spin_lock_irqsave(&vector_lock, flags);
1292 err = __assign_irq_vector(irq, mask);
1293 spin_unlock_irqrestore(&vector_lock, flags);
1294 return err;
1297 static void __clear_irq_vector(int irq)
1299 struct irq_cfg *cfg;
1300 cpumask_t mask;
1301 int cpu, vector;
1303 cfg = irq_cfg(irq);
1304 BUG_ON(!cfg->vector);
1306 vector = cfg->vector;
1307 cpus_and(mask, cfg->domain, cpu_online_map);
1308 for_each_cpu_mask_nr(cpu, mask)
1309 per_cpu(vector_irq, cpu)[vector] = -1;
1311 cfg->vector = 0;
1312 cpus_clear(cfg->domain);
1315 void __setup_vector_irq(int cpu)
1317 /* Initialize vector_irq on a new cpu */
1318 /* This function must be called with vector_lock held */
1319 int irq, vector;
1320 struct irq_cfg *cfg;
1322 /* Mark the inuse vectors */
1323 for_each_irq_cfg(irq, cfg) {
1324 if (!cpu_isset(cpu, cfg->domain))
1325 continue;
1326 vector = cfg->vector;
1327 per_cpu(vector_irq, cpu)[vector] = irq;
1329 /* Mark the free vectors */
1330 for (vector = 0; vector < NR_VECTORS; ++vector) {
1331 irq = per_cpu(vector_irq, cpu)[vector];
1332 if (irq < 0)
1333 continue;
1335 cfg = irq_cfg(irq);
1336 if (!cpu_isset(cpu, cfg->domain))
1337 per_cpu(vector_irq, cpu)[vector] = -1;
1341 static struct irq_chip ioapic_chip;
1342 #ifdef CONFIG_INTR_REMAP
1343 static struct irq_chip ir_ioapic_chip;
1344 #endif
1346 #define IOAPIC_AUTO -1
1347 #define IOAPIC_EDGE 0
1348 #define IOAPIC_LEVEL 1
1350 #ifdef CONFIG_X86_32
1351 static inline int IO_APIC_irq_trigger(int irq)
1353 int apic, idx, pin;
1355 for (apic = 0; apic < nr_ioapics; apic++) {
1356 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1357 idx = find_irq_entry(apic, pin, mp_INT);
1358 if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1359 return irq_trigger(idx);
1363 * nonexistent IRQs are edge default
1365 return 0;
1367 #else
1368 static inline int IO_APIC_irq_trigger(int irq)
1370 return 1;
1372 #endif
1374 static void ioapic_register_intr(int irq, unsigned long trigger)
1376 struct irq_desc *desc;
1378 /* first time to use this irq_desc */
1379 if (irq < 16)
1380 desc = irq_to_desc(irq);
1381 else
1382 desc = irq_to_desc_alloc(irq);
1384 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1385 trigger == IOAPIC_LEVEL)
1386 desc->status |= IRQ_LEVEL;
1387 else
1388 desc->status &= ~IRQ_LEVEL;
1390 #ifdef CONFIG_INTR_REMAP
1391 if (irq_remapped(irq)) {
1392 desc->status |= IRQ_MOVE_PCNTXT;
1393 if (trigger)
1394 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1395 handle_fasteoi_irq,
1396 "fasteoi");
1397 else
1398 set_irq_chip_and_handler_name(irq, &ir_ioapic_chip,
1399 handle_edge_irq, "edge");
1400 return;
1402 #endif
1403 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1404 trigger == IOAPIC_LEVEL)
1405 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1406 handle_fasteoi_irq,
1407 "fasteoi");
1408 else
1409 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1410 handle_edge_irq, "edge");
1413 static int setup_ioapic_entry(int apic, int irq,
1414 struct IO_APIC_route_entry *entry,
1415 unsigned int destination, int trigger,
1416 int polarity, int vector)
1419 * add it to the IO-APIC irq-routing table:
1421 memset(entry,0,sizeof(*entry));
1423 #ifdef CONFIG_INTR_REMAP
1424 if (intr_remapping_enabled) {
1425 struct intel_iommu *iommu = map_ioapic_to_ir(apic);
1426 struct irte irte;
1427 struct IR_IO_APIC_route_entry *ir_entry =
1428 (struct IR_IO_APIC_route_entry *) entry;
1429 int index;
1431 if (!iommu)
1432 panic("No mapping iommu for ioapic %d\n", apic);
1434 index = alloc_irte(iommu, irq, 1);
1435 if (index < 0)
1436 panic("Failed to allocate IRTE for ioapic %d\n", apic);
1438 memset(&irte, 0, sizeof(irte));
1440 irte.present = 1;
1441 irte.dst_mode = INT_DEST_MODE;
1442 irte.trigger_mode = trigger;
1443 irte.dlvry_mode = INT_DELIVERY_MODE;
1444 irte.vector = vector;
1445 irte.dest_id = IRTE_DEST(destination);
1447 modify_irte(irq, &irte);
1449 ir_entry->index2 = (index >> 15) & 0x1;
1450 ir_entry->zero = 0;
1451 ir_entry->format = 1;
1452 ir_entry->index = (index & 0x7fff);
1453 } else
1454 #endif
1456 entry->delivery_mode = INT_DELIVERY_MODE;
1457 entry->dest_mode = INT_DEST_MODE;
1458 entry->dest = destination;
1461 entry->mask = 0; /* enable IRQ */
1462 entry->trigger = trigger;
1463 entry->polarity = polarity;
1464 entry->vector = vector;
1466 /* Mask level triggered irqs.
1467 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1469 if (trigger)
1470 entry->mask = 1;
1471 return 0;
1474 static void setup_IO_APIC_irq(int apic, int pin, unsigned int irq,
1475 int trigger, int polarity)
1477 struct irq_cfg *cfg;
1478 struct IO_APIC_route_entry entry;
1479 cpumask_t mask;
1481 if (!IO_APIC_IRQ(irq))
1482 return;
1484 cfg = irq_cfg(irq);
1486 mask = TARGET_CPUS;
1487 if (assign_irq_vector(irq, mask))
1488 return;
1490 cpus_and(mask, cfg->domain, mask);
1492 apic_printk(APIC_VERBOSE,KERN_DEBUG
1493 "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1494 "IRQ %d Mode:%i Active:%i)\n",
1495 apic, mp_ioapics[apic].mp_apicid, pin, cfg->vector,
1496 irq, trigger, polarity);
1499 if (setup_ioapic_entry(mp_ioapics[apic].mp_apicid, irq, &entry,
1500 cpu_mask_to_apicid(mask), trigger, polarity,
1501 cfg->vector)) {
1502 printk("Failed to setup ioapic entry for ioapic %d, pin %d\n",
1503 mp_ioapics[apic].mp_apicid, pin);
1504 __clear_irq_vector(irq);
1505 return;
1508 ioapic_register_intr(irq, trigger);
1509 if (irq < 16)
1510 disable_8259A_irq(irq);
1512 ioapic_write_entry(apic, pin, entry);
1515 static void __init setup_IO_APIC_irqs(void)
1517 int apic, pin, idx, irq;
1518 int notcon = 0;
1520 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1522 for (apic = 0; apic < nr_ioapics; apic++) {
1523 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1525 idx = find_irq_entry(apic, pin, mp_INT);
1526 if (idx == -1) {
1527 if (!notcon) {
1528 notcon = 1;
1529 apic_printk(APIC_VERBOSE,
1530 KERN_DEBUG " %d-%d",
1531 mp_ioapics[apic].mp_apicid,
1532 pin);
1533 } else
1534 apic_printk(APIC_VERBOSE, " %d-%d",
1535 mp_ioapics[apic].mp_apicid,
1536 pin);
1537 continue;
1539 if (notcon) {
1540 apic_printk(APIC_VERBOSE,
1541 " (apicid-pin) not connected\n");
1542 notcon = 0;
1545 irq = pin_2_irq(idx, apic, pin);
1546 #ifdef CONFIG_X86_32
1547 if (multi_timer_check(apic, irq))
1548 continue;
1549 #endif
1550 add_pin_to_irq(irq, apic, pin);
1552 setup_IO_APIC_irq(apic, pin, irq,
1553 irq_trigger(idx), irq_polarity(idx));
1557 if (notcon)
1558 apic_printk(APIC_VERBOSE,
1559 " (apicid-pin) not connected\n");
1563 * Set up the timer pin, possibly with the 8259A-master behind.
1565 static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1566 int vector)
1568 struct IO_APIC_route_entry entry;
1570 #ifdef CONFIG_INTR_REMAP
1571 if (intr_remapping_enabled)
1572 return;
1573 #endif
1575 memset(&entry, 0, sizeof(entry));
1578 * We use logical delivery to get the timer IRQ
1579 * to the first CPU.
1581 entry.dest_mode = INT_DEST_MODE;
1582 entry.mask = 1; /* mask IRQ now */
1583 entry.dest = cpu_mask_to_apicid(TARGET_CPUS);
1584 entry.delivery_mode = INT_DELIVERY_MODE;
1585 entry.polarity = 0;
1586 entry.trigger = 0;
1587 entry.vector = vector;
1590 * The timer IRQ doesn't have to know that behind the
1591 * scene we may have a 8259A-master in AEOI mode ...
1593 set_irq_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq, "edge");
1596 * Add it to the IO-APIC irq-routing table:
1598 ioapic_write_entry(apic, pin, entry);
1602 __apicdebuginit(void) print_IO_APIC(void)
1604 int apic, i;
1605 union IO_APIC_reg_00 reg_00;
1606 union IO_APIC_reg_01 reg_01;
1607 union IO_APIC_reg_02 reg_02;
1608 union IO_APIC_reg_03 reg_03;
1609 unsigned long flags;
1610 struct irq_cfg *cfg;
1611 unsigned int irq;
1613 if (apic_verbosity == APIC_QUIET)
1614 return;
1616 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1617 for (i = 0; i < nr_ioapics; i++)
1618 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1619 mp_ioapics[i].mp_apicid, nr_ioapic_registers[i]);
1622 * We are a bit conservative about what we expect. We have to
1623 * know about every hardware change ASAP.
1625 printk(KERN_INFO "testing the IO APIC.......................\n");
1627 for (apic = 0; apic < nr_ioapics; apic++) {
1629 spin_lock_irqsave(&ioapic_lock, flags);
1630 reg_00.raw = io_apic_read(apic, 0);
1631 reg_01.raw = io_apic_read(apic, 1);
1632 if (reg_01.bits.version >= 0x10)
1633 reg_02.raw = io_apic_read(apic, 2);
1634 if (reg_01.bits.version >= 0x20)
1635 reg_03.raw = io_apic_read(apic, 3);
1636 spin_unlock_irqrestore(&ioapic_lock, flags);
1638 printk("\n");
1639 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mp_apicid);
1640 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1641 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1642 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1643 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1645 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1646 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1648 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1649 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1652 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1653 * but the value of reg_02 is read as the previous read register
1654 * value, so ignore it if reg_02 == reg_01.
1656 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1657 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1658 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1662 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1663 * or reg_03, but the value of reg_0[23] is read as the previous read
1664 * register value, so ignore it if reg_03 == reg_0[12].
1666 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1667 reg_03.raw != reg_01.raw) {
1668 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1669 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1672 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1674 printk(KERN_DEBUG " NR Dst Mask Trig IRR Pol"
1675 " Stat Dmod Deli Vect: \n");
1677 for (i = 0; i <= reg_01.bits.entries; i++) {
1678 struct IO_APIC_route_entry entry;
1680 entry = ioapic_read_entry(apic, i);
1682 printk(KERN_DEBUG " %02x %03X ",
1684 entry.dest
1687 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1688 entry.mask,
1689 entry.trigger,
1690 entry.irr,
1691 entry.polarity,
1692 entry.delivery_status,
1693 entry.dest_mode,
1694 entry.delivery_mode,
1695 entry.vector
1699 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1700 for_each_irq_cfg(irq, cfg) {
1701 struct irq_pin_list *entry = cfg->irq_2_pin;
1702 if (!entry)
1703 continue;
1704 printk(KERN_DEBUG "IRQ%d ", irq);
1705 for (;;) {
1706 printk("-> %d:%d", entry->apic, entry->pin);
1707 if (!entry->next)
1708 break;
1709 entry = entry->next;
1711 printk("\n");
1714 printk(KERN_INFO ".................................... done.\n");
1716 return;
1719 __apicdebuginit(void) print_APIC_bitfield(int base)
1721 unsigned int v;
1722 int i, j;
1724 if (apic_verbosity == APIC_QUIET)
1725 return;
1727 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1728 for (i = 0; i < 8; i++) {
1729 v = apic_read(base + i*0x10);
1730 for (j = 0; j < 32; j++) {
1731 if (v & (1<<j))
1732 printk("1");
1733 else
1734 printk("0");
1736 printk("\n");
1740 __apicdebuginit(void) print_local_APIC(void *dummy)
1742 unsigned int v, ver, maxlvt;
1743 u64 icr;
1745 if (apic_verbosity == APIC_QUIET)
1746 return;
1748 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1749 smp_processor_id(), hard_smp_processor_id());
1750 v = apic_read(APIC_ID);
1751 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v, read_apic_id());
1752 v = apic_read(APIC_LVR);
1753 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1754 ver = GET_APIC_VERSION(v);
1755 maxlvt = lapic_get_maxlvt();
1757 v = apic_read(APIC_TASKPRI);
1758 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1760 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1761 if (!APIC_XAPIC(ver)) {
1762 v = apic_read(APIC_ARBPRI);
1763 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1764 v & APIC_ARBPRI_MASK);
1766 v = apic_read(APIC_PROCPRI);
1767 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1771 * Remote read supported only in the 82489DX and local APIC for
1772 * Pentium processors.
1774 if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1775 v = apic_read(APIC_RRR);
1776 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1779 v = apic_read(APIC_LDR);
1780 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1781 if (!x2apic_enabled()) {
1782 v = apic_read(APIC_DFR);
1783 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1785 v = apic_read(APIC_SPIV);
1786 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1788 printk(KERN_DEBUG "... APIC ISR field:\n");
1789 print_APIC_bitfield(APIC_ISR);
1790 printk(KERN_DEBUG "... APIC TMR field:\n");
1791 print_APIC_bitfield(APIC_TMR);
1792 printk(KERN_DEBUG "... APIC IRR field:\n");
1793 print_APIC_bitfield(APIC_IRR);
1795 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1796 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1797 apic_write(APIC_ESR, 0);
1799 v = apic_read(APIC_ESR);
1800 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1803 icr = apic_icr_read();
1804 printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1805 printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
1807 v = apic_read(APIC_LVTT);
1808 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1810 if (maxlvt > 3) { /* PC is LVT#4. */
1811 v = apic_read(APIC_LVTPC);
1812 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1814 v = apic_read(APIC_LVT0);
1815 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1816 v = apic_read(APIC_LVT1);
1817 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1819 if (maxlvt > 2) { /* ERR is LVT#3. */
1820 v = apic_read(APIC_LVTERR);
1821 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1824 v = apic_read(APIC_TMICT);
1825 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1826 v = apic_read(APIC_TMCCT);
1827 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1828 v = apic_read(APIC_TDCR);
1829 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1830 printk("\n");
1833 __apicdebuginit(void) print_all_local_APICs(void)
1835 int cpu;
1837 preempt_disable();
1838 for_each_online_cpu(cpu)
1839 smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1840 preempt_enable();
1843 __apicdebuginit(void) print_PIC(void)
1845 unsigned int v;
1846 unsigned long flags;
1848 if (apic_verbosity == APIC_QUIET)
1849 return;
1851 printk(KERN_DEBUG "\nprinting PIC contents\n");
1853 spin_lock_irqsave(&i8259A_lock, flags);
1855 v = inb(0xa1) << 8 | inb(0x21);
1856 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1858 v = inb(0xa0) << 8 | inb(0x20);
1859 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1861 outb(0x0b,0xa0);
1862 outb(0x0b,0x20);
1863 v = inb(0xa0) << 8 | inb(0x20);
1864 outb(0x0a,0xa0);
1865 outb(0x0a,0x20);
1867 spin_unlock_irqrestore(&i8259A_lock, flags);
1869 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1871 v = inb(0x4d1) << 8 | inb(0x4d0);
1872 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1875 __apicdebuginit(int) print_all_ICs(void)
1877 print_PIC();
1878 print_all_local_APICs();
1879 print_IO_APIC();
1881 return 0;
1884 fs_initcall(print_all_ICs);
1887 /* Where if anywhere is the i8259 connect in external int mode */
1888 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1890 void __init enable_IO_APIC(void)
1892 union IO_APIC_reg_01 reg_01;
1893 int i8259_apic, i8259_pin;
1894 int apic;
1895 unsigned long flags;
1897 #ifdef CONFIG_X86_32
1898 int i;
1899 if (!pirqs_enabled)
1900 for (i = 0; i < MAX_PIRQS; i++)
1901 pirq_entries[i] = -1;
1902 #endif
1905 * The number of IO-APIC IRQ registers (== #pins):
1907 for (apic = 0; apic < nr_ioapics; apic++) {
1908 spin_lock_irqsave(&ioapic_lock, flags);
1909 reg_01.raw = io_apic_read(apic, 1);
1910 spin_unlock_irqrestore(&ioapic_lock, flags);
1911 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1913 for(apic = 0; apic < nr_ioapics; apic++) {
1914 int pin;
1915 /* See if any of the pins is in ExtINT mode */
1916 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1917 struct IO_APIC_route_entry entry;
1918 entry = ioapic_read_entry(apic, pin);
1920 /* If the interrupt line is enabled and in ExtInt mode
1921 * I have found the pin where the i8259 is connected.
1923 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1924 ioapic_i8259.apic = apic;
1925 ioapic_i8259.pin = pin;
1926 goto found_i8259;
1930 found_i8259:
1931 /* Look to see what if the MP table has reported the ExtINT */
1932 /* If we could not find the appropriate pin by looking at the ioapic
1933 * the i8259 probably is not connected the ioapic but give the
1934 * mptable a chance anyway.
1936 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1937 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1938 /* Trust the MP table if nothing is setup in the hardware */
1939 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1940 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1941 ioapic_i8259.pin = i8259_pin;
1942 ioapic_i8259.apic = i8259_apic;
1944 /* Complain if the MP table and the hardware disagree */
1945 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1946 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1948 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1952 * Do not trust the IO-APIC being empty at bootup
1954 clear_IO_APIC();
1958 * Not an __init, needed by the reboot code
1960 void disable_IO_APIC(void)
1963 * Clear the IO-APIC before rebooting:
1965 clear_IO_APIC();
1968 * If the i8259 is routed through an IOAPIC
1969 * Put that IOAPIC in virtual wire mode
1970 * so legacy interrupts can be delivered.
1972 if (ioapic_i8259.pin != -1) {
1973 struct IO_APIC_route_entry entry;
1975 memset(&entry, 0, sizeof(entry));
1976 entry.mask = 0; /* Enabled */
1977 entry.trigger = 0; /* Edge */
1978 entry.irr = 0;
1979 entry.polarity = 0; /* High */
1980 entry.delivery_status = 0;
1981 entry.dest_mode = 0; /* Physical */
1982 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1983 entry.vector = 0;
1984 entry.dest = read_apic_id();
1987 * Add it to the IO-APIC irq-routing table:
1989 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1992 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1995 #ifdef CONFIG_X86_32
1997 * function to set the IO-APIC physical IDs based on the
1998 * values stored in the MPC table.
2000 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
2003 static void __init setup_ioapic_ids_from_mpc(void)
2005 union IO_APIC_reg_00 reg_00;
2006 physid_mask_t phys_id_present_map;
2007 int apic;
2008 int i;
2009 unsigned char old_id;
2010 unsigned long flags;
2012 if (x86_quirks->setup_ioapic_ids && x86_quirks->setup_ioapic_ids())
2013 return;
2016 * Don't check I/O APIC IDs for xAPIC systems. They have
2017 * no meaning without the serial APIC bus.
2019 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2020 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
2021 return;
2023 * This is broken; anything with a real cpu count has to
2024 * circumvent this idiocy regardless.
2026 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
2029 * Set the IOAPIC ID to the value stored in the MPC table.
2031 for (apic = 0; apic < nr_ioapics; apic++) {
2033 /* Read the register 0 value */
2034 spin_lock_irqsave(&ioapic_lock, flags);
2035 reg_00.raw = io_apic_read(apic, 0);
2036 spin_unlock_irqrestore(&ioapic_lock, flags);
2038 old_id = mp_ioapics[apic].mp_apicid;
2040 if (mp_ioapics[apic].mp_apicid >= get_physical_broadcast()) {
2041 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
2042 apic, mp_ioapics[apic].mp_apicid);
2043 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2044 reg_00.bits.ID);
2045 mp_ioapics[apic].mp_apicid = reg_00.bits.ID;
2049 * Sanity check, is the ID really free? Every APIC in a
2050 * system must have a unique ID or we get lots of nice
2051 * 'stuck on smp_invalidate_needed IPI wait' messages.
2053 if (check_apicid_used(phys_id_present_map,
2054 mp_ioapics[apic].mp_apicid)) {
2055 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
2056 apic, mp_ioapics[apic].mp_apicid);
2057 for (i = 0; i < get_physical_broadcast(); i++)
2058 if (!physid_isset(i, phys_id_present_map))
2059 break;
2060 if (i >= get_physical_broadcast())
2061 panic("Max APIC ID exceeded!\n");
2062 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2064 physid_set(i, phys_id_present_map);
2065 mp_ioapics[apic].mp_apicid = i;
2066 } else {
2067 physid_mask_t tmp;
2068 tmp = apicid_to_cpu_present(mp_ioapics[apic].mp_apicid);
2069 apic_printk(APIC_VERBOSE, "Setting %d in the "
2070 "phys_id_present_map\n",
2071 mp_ioapics[apic].mp_apicid);
2072 physids_or(phys_id_present_map, phys_id_present_map, tmp);
2077 * We need to adjust the IRQ routing table
2078 * if the ID changed.
2080 if (old_id != mp_ioapics[apic].mp_apicid)
2081 for (i = 0; i < mp_irq_entries; i++)
2082 if (mp_irqs[i].mp_dstapic == old_id)
2083 mp_irqs[i].mp_dstapic
2084 = mp_ioapics[apic].mp_apicid;
2087 * Read the right value from the MPC table and
2088 * write it into the ID register.
2090 apic_printk(APIC_VERBOSE, KERN_INFO
2091 "...changing IO-APIC physical APIC ID to %d ...",
2092 mp_ioapics[apic].mp_apicid);
2094 reg_00.bits.ID = mp_ioapics[apic].mp_apicid;
2095 spin_lock_irqsave(&ioapic_lock, flags);
2096 io_apic_write(apic, 0, reg_00.raw);
2097 spin_unlock_irqrestore(&ioapic_lock, flags);
2100 * Sanity check
2102 spin_lock_irqsave(&ioapic_lock, flags);
2103 reg_00.raw = io_apic_read(apic, 0);
2104 spin_unlock_irqrestore(&ioapic_lock, flags);
2105 if (reg_00.bits.ID != mp_ioapics[apic].mp_apicid)
2106 printk("could not set ID!\n");
2107 else
2108 apic_printk(APIC_VERBOSE, " ok.\n");
2111 #endif
2113 int no_timer_check __initdata;
2115 static int __init notimercheck(char *s)
2117 no_timer_check = 1;
2118 return 1;
2120 __setup("no_timer_check", notimercheck);
2123 * There is a nasty bug in some older SMP boards, their mptable lies
2124 * about the timer IRQ. We do the following to work around the situation:
2126 * - timer IRQ defaults to IO-APIC IRQ
2127 * - if this function detects that timer IRQs are defunct, then we fall
2128 * back to ISA timer IRQs
2130 static int __init timer_irq_works(void)
2132 unsigned long t1 = jiffies;
2133 unsigned long flags;
2135 if (no_timer_check)
2136 return 1;
2138 local_save_flags(flags);
2139 local_irq_enable();
2140 /* Let ten ticks pass... */
2141 mdelay((10 * 1000) / HZ);
2142 local_irq_restore(flags);
2145 * Expect a few ticks at least, to be sure some possible
2146 * glue logic does not lock up after one or two first
2147 * ticks in a non-ExtINT mode. Also the local APIC
2148 * might have cached one ExtINT interrupt. Finally, at
2149 * least one tick may be lost due to delays.
2152 /* jiffies wrap? */
2153 if (time_after(jiffies, t1 + 4))
2154 return 1;
2155 return 0;
2159 * In the SMP+IOAPIC case it might happen that there are an unspecified
2160 * number of pending IRQ events unhandled. These cases are very rare,
2161 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2162 * better to do it this way as thus we do not have to be aware of
2163 * 'pending' interrupts in the IRQ path, except at this point.
2166 * Edge triggered needs to resend any interrupt
2167 * that was delayed but this is now handled in the device
2168 * independent code.
2172 * Starting up a edge-triggered IO-APIC interrupt is
2173 * nasty - we need to make sure that we get the edge.
2174 * If it is already asserted for some reason, we need
2175 * return 1 to indicate that is was pending.
2177 * This is not complete - we should be able to fake
2178 * an edge even if it isn't on the 8259A...
2181 static unsigned int startup_ioapic_irq(unsigned int irq)
2183 int was_pending = 0;
2184 unsigned long flags;
2186 spin_lock_irqsave(&ioapic_lock, flags);
2187 if (irq < 16) {
2188 disable_8259A_irq(irq);
2189 if (i8259A_irq_pending(irq))
2190 was_pending = 1;
2192 __unmask_IO_APIC_irq(irq);
2193 spin_unlock_irqrestore(&ioapic_lock, flags);
2195 return was_pending;
2198 #ifdef CONFIG_X86_64
2199 static int ioapic_retrigger_irq(unsigned int irq)
2202 struct irq_cfg *cfg = irq_cfg(irq);
2203 unsigned long flags;
2205 spin_lock_irqsave(&vector_lock, flags);
2206 send_IPI_mask(cpumask_of_cpu(first_cpu(cfg->domain)), cfg->vector);
2207 spin_unlock_irqrestore(&vector_lock, flags);
2209 return 1;
2211 #else
2212 static int ioapic_retrigger_irq(unsigned int irq)
2214 send_IPI_self(irq_cfg(irq)->vector);
2216 return 1;
2218 #endif
2221 * Level and edge triggered IO-APIC interrupts need different handling,
2222 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2223 * handled with the level-triggered descriptor, but that one has slightly
2224 * more overhead. Level-triggered interrupts cannot be handled with the
2225 * edge-triggered handler, without risking IRQ storms and other ugly
2226 * races.
2229 #ifdef CONFIG_SMP
2231 #ifdef CONFIG_INTR_REMAP
2232 static void ir_irq_migration(struct work_struct *work);
2234 static DECLARE_DELAYED_WORK(ir_migration_work, ir_irq_migration);
2237 * Migrate the IO-APIC irq in the presence of intr-remapping.
2239 * For edge triggered, irq migration is a simple atomic update(of vector
2240 * and cpu destination) of IRTE and flush the hardware cache.
2242 * For level triggered, we need to modify the io-apic RTE aswell with the update
2243 * vector information, along with modifying IRTE with vector and destination.
2244 * So irq migration for level triggered is little bit more complex compared to
2245 * edge triggered migration. But the good news is, we use the same algorithm
2246 * for level triggered migration as we have today, only difference being,
2247 * we now initiate the irq migration from process context instead of the
2248 * interrupt context.
2250 * In future, when we do a directed EOI (combined with cpu EOI broadcast
2251 * suppression) to the IO-APIC, level triggered irq migration will also be
2252 * as simple as edge triggered migration and we can do the irq migration
2253 * with a simple atomic update to IO-APIC RTE.
2255 static void migrate_ioapic_irq(int irq, cpumask_t mask)
2257 struct irq_cfg *cfg;
2258 struct irq_desc *desc;
2259 cpumask_t tmp, cleanup_mask;
2260 struct irte irte;
2261 int modify_ioapic_rte;
2262 unsigned int dest;
2263 unsigned long flags;
2265 cpus_and(tmp, mask, cpu_online_map);
2266 if (cpus_empty(tmp))
2267 return;
2269 if (get_irte(irq, &irte))
2270 return;
2272 if (assign_irq_vector(irq, mask))
2273 return;
2275 cfg = irq_cfg(irq);
2276 cpus_and(tmp, cfg->domain, mask);
2277 dest = cpu_mask_to_apicid(tmp);
2279 desc = irq_to_desc(irq);
2280 modify_ioapic_rte = desc->status & IRQ_LEVEL;
2281 if (modify_ioapic_rte) {
2282 spin_lock_irqsave(&ioapic_lock, flags);
2283 __target_IO_APIC_irq(irq, dest, cfg->vector);
2284 spin_unlock_irqrestore(&ioapic_lock, flags);
2287 irte.vector = cfg->vector;
2288 irte.dest_id = IRTE_DEST(dest);
2291 * Modified the IRTE and flushes the Interrupt entry cache.
2293 modify_irte(irq, &irte);
2295 if (cfg->move_in_progress) {
2296 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2297 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2298 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2299 cfg->move_in_progress = 0;
2302 desc->affinity = mask;
2305 static int migrate_irq_remapped_level(int irq)
2307 int ret = -1;
2308 struct irq_desc *desc = irq_to_desc(irq);
2310 mask_IO_APIC_irq(irq);
2312 if (io_apic_level_ack_pending(irq)) {
2314 * Interrupt in progress. Migrating irq now will change the
2315 * vector information in the IO-APIC RTE and that will confuse
2316 * the EOI broadcast performed by cpu.
2317 * So, delay the irq migration to the next instance.
2319 schedule_delayed_work(&ir_migration_work, 1);
2320 goto unmask;
2323 /* everthing is clear. we have right of way */
2324 migrate_ioapic_irq(irq, desc->pending_mask);
2326 ret = 0;
2327 desc->status &= ~IRQ_MOVE_PENDING;
2328 cpus_clear(desc->pending_mask);
2330 unmask:
2331 unmask_IO_APIC_irq(irq);
2332 return ret;
2335 static void ir_irq_migration(struct work_struct *work)
2337 unsigned int irq;
2338 struct irq_desc *desc;
2340 for_each_irq_desc(irq, desc) {
2341 if (desc->status & IRQ_MOVE_PENDING) {
2342 unsigned long flags;
2344 spin_lock_irqsave(&desc->lock, flags);
2345 if (!desc->chip->set_affinity ||
2346 !(desc->status & IRQ_MOVE_PENDING)) {
2347 desc->status &= ~IRQ_MOVE_PENDING;
2348 spin_unlock_irqrestore(&desc->lock, flags);
2349 continue;
2352 desc->chip->set_affinity(irq, desc->pending_mask);
2353 spin_unlock_irqrestore(&desc->lock, flags);
2359 * Migrates the IRQ destination in the process context.
2361 static void set_ir_ioapic_affinity_irq(unsigned int irq, cpumask_t mask)
2363 struct irq_desc *desc = irq_to_desc(irq);
2365 if (desc->status & IRQ_LEVEL) {
2366 desc->status |= IRQ_MOVE_PENDING;
2367 desc->pending_mask = mask;
2368 migrate_irq_remapped_level(irq);
2369 return;
2372 migrate_ioapic_irq(irq, mask);
2374 #endif
2376 asmlinkage void smp_irq_move_cleanup_interrupt(void)
2378 unsigned vector, me;
2379 ack_APIC_irq();
2380 #ifdef CONFIG_X86_64
2381 exit_idle();
2382 #endif
2383 irq_enter();
2385 me = smp_processor_id();
2386 for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2387 unsigned int irq;
2388 struct irq_desc *desc;
2389 struct irq_cfg *cfg;
2390 irq = __get_cpu_var(vector_irq)[vector];
2392 desc = irq_to_desc(irq);
2393 if (!desc)
2394 continue;
2396 cfg = irq_cfg(irq);
2397 spin_lock(&desc->lock);
2398 if (!cfg->move_cleanup_count)
2399 goto unlock;
2401 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain))
2402 goto unlock;
2404 __get_cpu_var(vector_irq)[vector] = -1;
2405 cfg->move_cleanup_count--;
2406 unlock:
2407 spin_unlock(&desc->lock);
2410 irq_exit();
2413 static void irq_complete_move(unsigned int irq)
2415 struct irq_cfg *cfg = irq_cfg(irq);
2416 unsigned vector, me;
2418 if (likely(!cfg->move_in_progress))
2419 return;
2421 vector = ~get_irq_regs()->orig_ax;
2422 me = smp_processor_id();
2423 if ((vector == cfg->vector) && cpu_isset(me, cfg->domain)) {
2424 cpumask_t cleanup_mask;
2426 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
2427 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
2428 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2429 cfg->move_in_progress = 0;
2432 #else
2433 static inline void irq_complete_move(unsigned int irq) {}
2434 #endif
2435 #ifdef CONFIG_INTR_REMAP
2436 static void ack_x2apic_level(unsigned int irq)
2438 ack_x2APIC_irq();
2441 static void ack_x2apic_edge(unsigned int irq)
2443 ack_x2APIC_irq();
2445 #endif
2447 static void ack_apic_edge(unsigned int irq)
2449 irq_complete_move(irq);
2450 move_native_irq(irq);
2451 ack_APIC_irq();
2454 #ifdef CONFIG_X86_32
2455 atomic_t irq_mis_count;
2456 #endif
2458 static void ack_apic_level(unsigned int irq)
2460 #ifdef CONFIG_X86_32
2461 unsigned long v;
2462 int i;
2463 #endif
2464 int do_unmask_irq = 0;
2466 irq_complete_move(irq);
2467 #ifdef CONFIG_GENERIC_PENDING_IRQ
2468 /* If we are moving the irq we need to mask it */
2469 if (unlikely(irq_to_desc(irq)->status & IRQ_MOVE_PENDING)) {
2470 do_unmask_irq = 1;
2471 mask_IO_APIC_irq(irq);
2473 #endif
2475 #ifdef CONFIG_X86_32
2477 * It appears there is an erratum which affects at least version 0x11
2478 * of I/O APIC (that's the 82093AA and cores integrated into various
2479 * chipsets). Under certain conditions a level-triggered interrupt is
2480 * erroneously delivered as edge-triggered one but the respective IRR
2481 * bit gets set nevertheless. As a result the I/O unit expects an EOI
2482 * message but it will never arrive and further interrupts are blocked
2483 * from the source. The exact reason is so far unknown, but the
2484 * phenomenon was observed when two consecutive interrupt requests
2485 * from a given source get delivered to the same CPU and the source is
2486 * temporarily disabled in between.
2488 * A workaround is to simulate an EOI message manually. We achieve it
2489 * by setting the trigger mode to edge and then to level when the edge
2490 * trigger mode gets detected in the TMR of a local APIC for a
2491 * level-triggered interrupt. We mask the source for the time of the
2492 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2493 * The idea is from Manfred Spraul. --macro
2495 i = irq_cfg(irq)->vector;
2497 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2498 #endif
2501 * We must acknowledge the irq before we move it or the acknowledge will
2502 * not propagate properly.
2504 ack_APIC_irq();
2506 /* Now we can move and renable the irq */
2507 if (unlikely(do_unmask_irq)) {
2508 /* Only migrate the irq if the ack has been received.
2510 * On rare occasions the broadcast level triggered ack gets
2511 * delayed going to ioapics, and if we reprogram the
2512 * vector while Remote IRR is still set the irq will never
2513 * fire again.
2515 * To prevent this scenario we read the Remote IRR bit
2516 * of the ioapic. This has two effects.
2517 * - On any sane system the read of the ioapic will
2518 * flush writes (and acks) going to the ioapic from
2519 * this cpu.
2520 * - We get to see if the ACK has actually been delivered.
2522 * Based on failed experiments of reprogramming the
2523 * ioapic entry from outside of irq context starting
2524 * with masking the ioapic entry and then polling until
2525 * Remote IRR was clear before reprogramming the
2526 * ioapic I don't trust the Remote IRR bit to be
2527 * completey accurate.
2529 * However there appears to be no other way to plug
2530 * this race, so if the Remote IRR bit is not
2531 * accurate and is causing problems then it is a hardware bug
2532 * and you can go talk to the chipset vendor about it.
2534 if (!io_apic_level_ack_pending(irq))
2535 move_masked_irq(irq);
2536 unmask_IO_APIC_irq(irq);
2539 #ifdef CONFIG_X86_32
2540 if (!(v & (1 << (i & 0x1f)))) {
2541 atomic_inc(&irq_mis_count);
2542 spin_lock(&ioapic_lock);
2543 __mask_and_edge_IO_APIC_irq(irq);
2544 __unmask_and_level_IO_APIC_irq(irq);
2545 spin_unlock(&ioapic_lock);
2547 #endif
2550 static struct irq_chip ioapic_chip __read_mostly = {
2551 .name = "IO-APIC",
2552 .startup = startup_ioapic_irq,
2553 .mask = mask_IO_APIC_irq,
2554 .unmask = unmask_IO_APIC_irq,
2555 .ack = ack_apic_edge,
2556 .eoi = ack_apic_level,
2557 #ifdef CONFIG_SMP
2558 .set_affinity = set_ioapic_affinity_irq,
2559 #endif
2560 .retrigger = ioapic_retrigger_irq,
2563 #ifdef CONFIG_INTR_REMAP
2564 static struct irq_chip ir_ioapic_chip __read_mostly = {
2565 .name = "IR-IO-APIC",
2566 .startup = startup_ioapic_irq,
2567 .mask = mask_IO_APIC_irq,
2568 .unmask = unmask_IO_APIC_irq,
2569 .ack = ack_x2apic_edge,
2570 .eoi = ack_x2apic_level,
2571 #ifdef CONFIG_SMP
2572 .set_affinity = set_ir_ioapic_affinity_irq,
2573 #endif
2574 .retrigger = ioapic_retrigger_irq,
2576 #endif
2578 static inline void init_IO_APIC_traps(void)
2580 int irq;
2581 struct irq_desc *desc;
2582 struct irq_cfg *cfg;
2585 * NOTE! The local APIC isn't very good at handling
2586 * multiple interrupts at the same interrupt level.
2587 * As the interrupt level is determined by taking the
2588 * vector number and shifting that right by 4, we
2589 * want to spread these out a bit so that they don't
2590 * all fall in the same interrupt level.
2592 * Also, we've got to be careful not to trash gate
2593 * 0x80, because int 0x80 is hm, kind of importantish. ;)
2595 for_each_irq_cfg(irq, cfg) {
2596 if (IO_APIC_IRQ(irq) && !cfg->vector) {
2598 * Hmm.. We don't have an entry for this,
2599 * so default to an old-fashioned 8259
2600 * interrupt if we can..
2602 if (irq < 16)
2603 make_8259A_irq(irq);
2604 else {
2605 desc = irq_to_desc(irq);
2606 /* Strange. Oh, well.. */
2607 desc->chip = &no_irq_chip;
2614 * The local APIC irq-chip implementation:
2617 static void mask_lapic_irq(unsigned int irq)
2619 unsigned long v;
2621 v = apic_read(APIC_LVT0);
2622 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2625 static void unmask_lapic_irq(unsigned int irq)
2627 unsigned long v;
2629 v = apic_read(APIC_LVT0);
2630 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2633 static void ack_lapic_irq (unsigned int irq)
2635 ack_APIC_irq();
2638 static struct irq_chip lapic_chip __read_mostly = {
2639 .name = "local-APIC",
2640 .mask = mask_lapic_irq,
2641 .unmask = unmask_lapic_irq,
2642 .ack = ack_lapic_irq,
2645 static void lapic_register_intr(int irq)
2647 struct irq_desc *desc;
2649 desc = irq_to_desc(irq);
2650 desc->status &= ~IRQ_LEVEL;
2651 set_irq_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2652 "edge");
2655 static void __init setup_nmi(void)
2658 * Dirty trick to enable the NMI watchdog ...
2659 * We put the 8259A master into AEOI mode and
2660 * unmask on all local APICs LVT0 as NMI.
2662 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2663 * is from Maciej W. Rozycki - so we do not have to EOI from
2664 * the NMI handler or the timer interrupt.
2666 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2668 enable_NMI_through_LVT0();
2670 apic_printk(APIC_VERBOSE, " done.\n");
2674 * This looks a bit hackish but it's about the only one way of sending
2675 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2676 * not support the ExtINT mode, unfortunately. We need to send these
2677 * cycles as some i82489DX-based boards have glue logic that keeps the
2678 * 8259A interrupt line asserted until INTA. --macro
2680 static inline void __init unlock_ExtINT_logic(void)
2682 int apic, pin, i;
2683 struct IO_APIC_route_entry entry0, entry1;
2684 unsigned char save_control, save_freq_select;
2686 pin = find_isa_irq_pin(8, mp_INT);
2687 if (pin == -1) {
2688 WARN_ON_ONCE(1);
2689 return;
2691 apic = find_isa_irq_apic(8, mp_INT);
2692 if (apic == -1) {
2693 WARN_ON_ONCE(1);
2694 return;
2697 entry0 = ioapic_read_entry(apic, pin);
2698 clear_IO_APIC_pin(apic, pin);
2700 memset(&entry1, 0, sizeof(entry1));
2702 entry1.dest_mode = 0; /* physical delivery */
2703 entry1.mask = 0; /* unmask IRQ now */
2704 entry1.dest = hard_smp_processor_id();
2705 entry1.delivery_mode = dest_ExtINT;
2706 entry1.polarity = entry0.polarity;
2707 entry1.trigger = 0;
2708 entry1.vector = 0;
2710 ioapic_write_entry(apic, pin, entry1);
2712 save_control = CMOS_READ(RTC_CONTROL);
2713 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2714 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2715 RTC_FREQ_SELECT);
2716 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2718 i = 100;
2719 while (i-- > 0) {
2720 mdelay(10);
2721 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2722 i -= 10;
2725 CMOS_WRITE(save_control, RTC_CONTROL);
2726 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2727 clear_IO_APIC_pin(apic, pin);
2729 ioapic_write_entry(apic, pin, entry0);
2732 static int disable_timer_pin_1 __initdata;
2733 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
2734 static int __init disable_timer_pin_setup(char *arg)
2736 disable_timer_pin_1 = 1;
2737 return 0;
2739 early_param("disable_timer_pin_1", disable_timer_pin_setup);
2741 int timer_through_8259 __initdata;
2744 * This code may look a bit paranoid, but it's supposed to cooperate with
2745 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2746 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2747 * fanatically on his truly buggy board.
2749 * FIXME: really need to revamp this for all platforms.
2751 static inline void __init check_timer(void)
2753 struct irq_cfg *cfg = irq_cfg(0);
2754 int apic1, pin1, apic2, pin2;
2755 unsigned long flags;
2756 unsigned int ver;
2757 int no_pin1 = 0;
2759 local_irq_save(flags);
2761 ver = apic_read(APIC_LVR);
2762 ver = GET_APIC_VERSION(ver);
2765 * get/set the timer IRQ vector:
2767 disable_8259A_irq(0);
2768 assign_irq_vector(0, TARGET_CPUS);
2771 * As IRQ0 is to be enabled in the 8259A, the virtual
2772 * wire has to be disabled in the local APIC. Also
2773 * timer interrupts need to be acknowledged manually in
2774 * the 8259A for the i82489DX when using the NMI
2775 * watchdog as that APIC treats NMIs as level-triggered.
2776 * The AEOI mode will finish them in the 8259A
2777 * automatically.
2779 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2780 init_8259A(1);
2781 #ifdef CONFIG_X86_32
2782 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
2783 #endif
2785 pin1 = find_isa_irq_pin(0, mp_INT);
2786 apic1 = find_isa_irq_apic(0, mp_INT);
2787 pin2 = ioapic_i8259.pin;
2788 apic2 = ioapic_i8259.apic;
2790 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2791 "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2792 cfg->vector, apic1, pin1, apic2, pin2);
2795 * Some BIOS writers are clueless and report the ExtINTA
2796 * I/O APIC input from the cascaded 8259A as the timer
2797 * interrupt input. So just in case, if only one pin
2798 * was found above, try it both directly and through the
2799 * 8259A.
2801 if (pin1 == -1) {
2802 #ifdef CONFIG_INTR_REMAP
2803 if (intr_remapping_enabled)
2804 panic("BIOS bug: timer not connected to IO-APIC");
2805 #endif
2806 pin1 = pin2;
2807 apic1 = apic2;
2808 no_pin1 = 1;
2809 } else if (pin2 == -1) {
2810 pin2 = pin1;
2811 apic2 = apic1;
2814 if (pin1 != -1) {
2816 * Ok, does IRQ0 through the IOAPIC work?
2818 if (no_pin1) {
2819 add_pin_to_irq(0, apic1, pin1);
2820 setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2822 unmask_IO_APIC_irq(0);
2823 if (timer_irq_works()) {
2824 if (nmi_watchdog == NMI_IO_APIC) {
2825 setup_nmi();
2826 enable_8259A_irq(0);
2828 if (disable_timer_pin_1 > 0)
2829 clear_IO_APIC_pin(0, pin1);
2830 goto out;
2832 #ifdef CONFIG_INTR_REMAP
2833 if (intr_remapping_enabled)
2834 panic("timer doesn't work through Interrupt-remapped IO-APIC");
2835 #endif
2836 clear_IO_APIC_pin(apic1, pin1);
2837 if (!no_pin1)
2838 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2839 "8254 timer not connected to IO-APIC\n");
2841 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2842 "(IRQ0) through the 8259A ...\n");
2843 apic_printk(APIC_QUIET, KERN_INFO
2844 "..... (found apic %d pin %d) ...\n", apic2, pin2);
2846 * legacy devices should be connected to IO APIC #0
2848 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
2849 setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2850 unmask_IO_APIC_irq(0);
2851 enable_8259A_irq(0);
2852 if (timer_irq_works()) {
2853 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2854 timer_through_8259 = 1;
2855 if (nmi_watchdog == NMI_IO_APIC) {
2856 disable_8259A_irq(0);
2857 setup_nmi();
2858 enable_8259A_irq(0);
2860 goto out;
2863 * Cleanup, just in case ...
2865 disable_8259A_irq(0);
2866 clear_IO_APIC_pin(apic2, pin2);
2867 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2870 if (nmi_watchdog == NMI_IO_APIC) {
2871 apic_printk(APIC_QUIET, KERN_WARNING "timer doesn't work "
2872 "through the IO-APIC - disabling NMI Watchdog!\n");
2873 nmi_watchdog = NMI_NONE;
2875 #ifdef CONFIG_X86_32
2876 timer_ack = 0;
2877 #endif
2879 apic_printk(APIC_QUIET, KERN_INFO
2880 "...trying to set up timer as Virtual Wire IRQ...\n");
2882 lapic_register_intr(0);
2883 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */
2884 enable_8259A_irq(0);
2886 if (timer_irq_works()) {
2887 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2888 goto out;
2890 disable_8259A_irq(0);
2891 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2892 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2894 apic_printk(APIC_QUIET, KERN_INFO
2895 "...trying to set up timer as ExtINT IRQ...\n");
2897 init_8259A(0);
2898 make_8259A_irq(0);
2899 apic_write(APIC_LVT0, APIC_DM_EXTINT);
2901 unlock_ExtINT_logic();
2903 if (timer_irq_works()) {
2904 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2905 goto out;
2907 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2908 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2909 "report. Then try booting with the 'noapic' option.\n");
2910 out:
2911 local_irq_restore(flags);
2915 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2916 * to devices. However there may be an I/O APIC pin available for
2917 * this interrupt regardless. The pin may be left unconnected, but
2918 * typically it will be reused as an ExtINT cascade interrupt for
2919 * the master 8259A. In the MPS case such a pin will normally be
2920 * reported as an ExtINT interrupt in the MP table. With ACPI
2921 * there is no provision for ExtINT interrupts, and in the absence
2922 * of an override it would be treated as an ordinary ISA I/O APIC
2923 * interrupt, that is edge-triggered and unmasked by default. We
2924 * used to do this, but it caused problems on some systems because
2925 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2926 * the same ExtINT cascade interrupt to drive the local APIC of the
2927 * bootstrap processor. Therefore we refrain from routing IRQ2 to
2928 * the I/O APIC in all cases now. No actual device should request
2929 * it anyway. --macro
2931 #define PIC_IRQS (1 << PIC_CASCADE_IR)
2933 void __init setup_IO_APIC(void)
2936 #ifdef CONFIG_X86_32
2937 enable_IO_APIC();
2938 #else
2940 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2942 #endif
2944 io_apic_irqs = ~PIC_IRQS;
2946 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2948 * Set up IO-APIC IRQ routing.
2950 #ifdef CONFIG_X86_32
2951 if (!acpi_ioapic)
2952 setup_ioapic_ids_from_mpc();
2953 #endif
2954 sync_Arb_IDs();
2955 setup_IO_APIC_irqs();
2956 init_IO_APIC_traps();
2957 check_timer();
2961 * Called after all the initialization is done. If we didnt find any
2962 * APIC bugs then we can allow the modify fast path
2965 static int __init io_apic_bug_finalize(void)
2967 if (sis_apic_bug == -1)
2968 sis_apic_bug = 0;
2969 return 0;
2972 late_initcall(io_apic_bug_finalize);
2974 struct sysfs_ioapic_data {
2975 struct sys_device dev;
2976 struct IO_APIC_route_entry entry[0];
2978 static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2980 static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
2982 struct IO_APIC_route_entry *entry;
2983 struct sysfs_ioapic_data *data;
2984 int i;
2986 data = container_of(dev, struct sysfs_ioapic_data, dev);
2987 entry = data->entry;
2988 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++, entry ++ )
2989 *entry = ioapic_read_entry(dev->id, i);
2991 return 0;
2994 static int ioapic_resume(struct sys_device *dev)
2996 struct IO_APIC_route_entry *entry;
2997 struct sysfs_ioapic_data *data;
2998 unsigned long flags;
2999 union IO_APIC_reg_00 reg_00;
3000 int i;
3002 data = container_of(dev, struct sysfs_ioapic_data, dev);
3003 entry = data->entry;
3005 spin_lock_irqsave(&ioapic_lock, flags);
3006 reg_00.raw = io_apic_read(dev->id, 0);
3007 if (reg_00.bits.ID != mp_ioapics[dev->id].mp_apicid) {
3008 reg_00.bits.ID = mp_ioapics[dev->id].mp_apicid;
3009 io_apic_write(dev->id, 0, reg_00.raw);
3011 spin_unlock_irqrestore(&ioapic_lock, flags);
3012 for (i = 0; i < nr_ioapic_registers[dev->id]; i++)
3013 ioapic_write_entry(dev->id, i, entry[i]);
3015 return 0;
3018 static struct sysdev_class ioapic_sysdev_class = {
3019 .name = "ioapic",
3020 .suspend = ioapic_suspend,
3021 .resume = ioapic_resume,
3024 static int __init ioapic_init_sysfs(void)
3026 struct sys_device * dev;
3027 int i, size, error;
3029 error = sysdev_class_register(&ioapic_sysdev_class);
3030 if (error)
3031 return error;
3033 for (i = 0; i < nr_ioapics; i++ ) {
3034 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
3035 * sizeof(struct IO_APIC_route_entry);
3036 mp_ioapic_data[i] = kzalloc(size, GFP_KERNEL);
3037 if (!mp_ioapic_data[i]) {
3038 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3039 continue;
3041 dev = &mp_ioapic_data[i]->dev;
3042 dev->id = i;
3043 dev->cls = &ioapic_sysdev_class;
3044 error = sysdev_register(dev);
3045 if (error) {
3046 kfree(mp_ioapic_data[i]);
3047 mp_ioapic_data[i] = NULL;
3048 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
3049 continue;
3053 return 0;
3056 device_initcall(ioapic_init_sysfs);
3059 * Dynamic irq allocate and deallocation
3061 unsigned int create_irq_nr(unsigned int irq_want)
3063 /* Allocate an unused irq */
3064 unsigned int irq;
3065 unsigned int new;
3066 unsigned long flags;
3067 struct irq_cfg *cfg_new;
3069 #ifndef CONFIG_HAVE_SPARSE_IRQ
3070 irq_want = nr_irqs - 1;
3071 #endif
3073 irq = 0;
3074 spin_lock_irqsave(&vector_lock, flags);
3075 for (new = irq_want; new > 0; new--) {
3076 if (platform_legacy_irq(new))
3077 continue;
3078 cfg_new = irq_cfg(new);
3079 if (cfg_new && cfg_new->vector != 0)
3080 continue;
3081 /* check if need to create one */
3082 if (!cfg_new)
3083 cfg_new = irq_cfg_alloc(new);
3084 if (__assign_irq_vector(new, TARGET_CPUS) == 0)
3085 irq = new;
3086 break;
3088 spin_unlock_irqrestore(&vector_lock, flags);
3090 if (irq > 0) {
3091 dynamic_irq_init(irq);
3093 return irq;
3096 int create_irq(void)
3098 int irq;
3100 irq = create_irq_nr(nr_irqs - 1);
3102 if (irq == 0)
3103 irq = -1;
3105 return irq;
3108 void destroy_irq(unsigned int irq)
3110 unsigned long flags;
3112 dynamic_irq_cleanup(irq);
3114 #ifdef CONFIG_INTR_REMAP
3115 free_irte(irq);
3116 #endif
3117 spin_lock_irqsave(&vector_lock, flags);
3118 __clear_irq_vector(irq);
3119 spin_unlock_irqrestore(&vector_lock, flags);
3123 * MSI message composition
3125 #ifdef CONFIG_PCI_MSI
3126 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
3128 struct irq_cfg *cfg;
3129 int err;
3130 unsigned dest;
3131 cpumask_t tmp;
3133 tmp = TARGET_CPUS;
3134 err = assign_irq_vector(irq, tmp);
3135 if (err)
3136 return err;
3138 cfg = irq_cfg(irq);
3139 cpus_and(tmp, cfg->domain, tmp);
3140 dest = cpu_mask_to_apicid(tmp);
3142 #ifdef CONFIG_INTR_REMAP
3143 if (irq_remapped(irq)) {
3144 struct irte irte;
3145 int ir_index;
3146 u16 sub_handle;
3148 ir_index = map_irq_to_irte_handle(irq, &sub_handle);
3149 BUG_ON(ir_index == -1);
3151 memset (&irte, 0, sizeof(irte));
3153 irte.present = 1;
3154 irte.dst_mode = INT_DEST_MODE;
3155 irte.trigger_mode = 0; /* edge */
3156 irte.dlvry_mode = INT_DELIVERY_MODE;
3157 irte.vector = cfg->vector;
3158 irte.dest_id = IRTE_DEST(dest);
3160 modify_irte(irq, &irte);
3162 msg->address_hi = MSI_ADDR_BASE_HI;
3163 msg->data = sub_handle;
3164 msg->address_lo = MSI_ADDR_BASE_LO | MSI_ADDR_IR_EXT_INT |
3165 MSI_ADDR_IR_SHV |
3166 MSI_ADDR_IR_INDEX1(ir_index) |
3167 MSI_ADDR_IR_INDEX2(ir_index);
3168 } else
3169 #endif
3171 msg->address_hi = MSI_ADDR_BASE_HI;
3172 msg->address_lo =
3173 MSI_ADDR_BASE_LO |
3174 ((INT_DEST_MODE == 0) ?
3175 MSI_ADDR_DEST_MODE_PHYSICAL:
3176 MSI_ADDR_DEST_MODE_LOGICAL) |
3177 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3178 MSI_ADDR_REDIRECTION_CPU:
3179 MSI_ADDR_REDIRECTION_LOWPRI) |
3180 MSI_ADDR_DEST_ID(dest);
3182 msg->data =
3183 MSI_DATA_TRIGGER_EDGE |
3184 MSI_DATA_LEVEL_ASSERT |
3185 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3186 MSI_DATA_DELIVERY_FIXED:
3187 MSI_DATA_DELIVERY_LOWPRI) |
3188 MSI_DATA_VECTOR(cfg->vector);
3190 return err;
3193 #ifdef CONFIG_SMP
3194 static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3196 struct irq_cfg *cfg;
3197 struct msi_msg msg;
3198 unsigned int dest;
3199 cpumask_t tmp;
3200 struct irq_desc *desc;
3202 cpus_and(tmp, mask, cpu_online_map);
3203 if (cpus_empty(tmp))
3204 return;
3206 if (assign_irq_vector(irq, mask))
3207 return;
3209 cfg = irq_cfg(irq);
3210 cpus_and(tmp, cfg->domain, mask);
3211 dest = cpu_mask_to_apicid(tmp);
3213 read_msi_msg(irq, &msg);
3215 msg.data &= ~MSI_DATA_VECTOR_MASK;
3216 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3217 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3218 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3220 write_msi_msg(irq, &msg);
3221 desc = irq_to_desc(irq);
3222 desc->affinity = mask;
3225 #ifdef CONFIG_INTR_REMAP
3227 * Migrate the MSI irq to another cpumask. This migration is
3228 * done in the process context using interrupt-remapping hardware.
3230 static void ir_set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
3232 struct irq_cfg *cfg;
3233 unsigned int dest;
3234 cpumask_t tmp, cleanup_mask;
3235 struct irte irte;
3236 struct irq_desc *desc;
3238 cpus_and(tmp, mask, cpu_online_map);
3239 if (cpus_empty(tmp))
3240 return;
3242 if (get_irte(irq, &irte))
3243 return;
3245 if (assign_irq_vector(irq, mask))
3246 return;
3248 cfg = irq_cfg(irq);
3249 cpus_and(tmp, cfg->domain, mask);
3250 dest = cpu_mask_to_apicid(tmp);
3252 irte.vector = cfg->vector;
3253 irte.dest_id = IRTE_DEST(dest);
3256 * atomically update the IRTE with the new destination and vector.
3258 modify_irte(irq, &irte);
3261 * After this point, all the interrupts will start arriving
3262 * at the new destination. So, time to cleanup the previous
3263 * vector allocation.
3265 if (cfg->move_in_progress) {
3266 cpus_and(cleanup_mask, cfg->old_domain, cpu_online_map);
3267 cfg->move_cleanup_count = cpus_weight(cleanup_mask);
3268 send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
3269 cfg->move_in_progress = 0;
3272 desc = irq_to_desc(irq);
3273 desc->affinity = mask;
3275 #endif
3276 #endif /* CONFIG_SMP */
3279 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3280 * which implement the MSI or MSI-X Capability Structure.
3282 static struct irq_chip msi_chip = {
3283 .name = "PCI-MSI",
3284 .unmask = unmask_msi_irq,
3285 .mask = mask_msi_irq,
3286 .ack = ack_apic_edge,
3287 #ifdef CONFIG_SMP
3288 .set_affinity = set_msi_irq_affinity,
3289 #endif
3290 .retrigger = ioapic_retrigger_irq,
3293 #ifdef CONFIG_INTR_REMAP
3294 static struct irq_chip msi_ir_chip = {
3295 .name = "IR-PCI-MSI",
3296 .unmask = unmask_msi_irq,
3297 .mask = mask_msi_irq,
3298 .ack = ack_x2apic_edge,
3299 #ifdef CONFIG_SMP
3300 .set_affinity = ir_set_msi_irq_affinity,
3301 #endif
3302 .retrigger = ioapic_retrigger_irq,
3306 * Map the PCI dev to the corresponding remapping hardware unit
3307 * and allocate 'nvec' consecutive interrupt-remapping table entries
3308 * in it.
3310 static int msi_alloc_irte(struct pci_dev *dev, int irq, int nvec)
3312 struct intel_iommu *iommu;
3313 int index;
3315 iommu = map_dev_to_ir(dev);
3316 if (!iommu) {
3317 printk(KERN_ERR
3318 "Unable to map PCI %s to iommu\n", pci_name(dev));
3319 return -ENOENT;
3322 index = alloc_irte(iommu, irq, nvec);
3323 if (index < 0) {
3324 printk(KERN_ERR
3325 "Unable to allocate %d IRTE for PCI %s\n", nvec,
3326 pci_name(dev));
3327 return -ENOSPC;
3329 return index;
3331 #endif
3333 static int setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc, int irq)
3335 int ret;
3336 struct msi_msg msg;
3338 ret = msi_compose_msg(dev, irq, &msg);
3339 if (ret < 0)
3340 return ret;
3342 set_irq_msi(irq, desc);
3343 write_msi_msg(irq, &msg);
3345 #ifdef CONFIG_INTR_REMAP
3346 if (irq_remapped(irq)) {
3347 struct irq_desc *desc = irq_to_desc(irq);
3349 * irq migration in process context
3351 desc->status |= IRQ_MOVE_PCNTXT;
3352 set_irq_chip_and_handler_name(irq, &msi_ir_chip, handle_edge_irq, "edge");
3353 } else
3354 #endif
3355 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq, "edge");
3357 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3359 return 0;
3362 static unsigned int build_irq_for_pci_dev(struct pci_dev *dev)
3364 unsigned int irq;
3366 irq = dev->bus->number;
3367 irq <<= 8;
3368 irq |= dev->devfn;
3369 irq <<= 12;
3371 return irq;
3374 int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
3376 unsigned int irq;
3377 int ret;
3378 unsigned int irq_want;
3380 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3382 irq = create_irq_nr(irq_want);
3383 if (irq == 0)
3384 return -1;
3386 #ifdef CONFIG_INTR_REMAP
3387 if (!intr_remapping_enabled)
3388 goto no_ir;
3390 ret = msi_alloc_irte(dev, irq, 1);
3391 if (ret < 0)
3392 goto error;
3393 no_ir:
3394 #endif
3395 ret = setup_msi_irq(dev, desc, irq);
3396 if (ret < 0) {
3397 destroy_irq(irq);
3398 return ret;
3400 return 0;
3402 #ifdef CONFIG_INTR_REMAP
3403 error:
3404 destroy_irq(irq);
3405 return ret;
3406 #endif
3409 int arch_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3411 unsigned int irq;
3412 int ret, sub_handle;
3413 struct msi_desc *desc;
3414 unsigned int irq_want;
3416 #ifdef CONFIG_INTR_REMAP
3417 struct intel_iommu *iommu = 0;
3418 int index = 0;
3419 #endif
3421 irq_want = build_irq_for_pci_dev(dev) + 0x100;
3422 sub_handle = 0;
3423 list_for_each_entry(desc, &dev->msi_list, list) {
3424 irq = create_irq_nr(irq_want--);
3425 if (irq == 0)
3426 return -1;
3427 #ifdef CONFIG_INTR_REMAP
3428 if (!intr_remapping_enabled)
3429 goto no_ir;
3431 if (!sub_handle) {
3433 * allocate the consecutive block of IRTE's
3434 * for 'nvec'
3436 index = msi_alloc_irte(dev, irq, nvec);
3437 if (index < 0) {
3438 ret = index;
3439 goto error;
3441 } else {
3442 iommu = map_dev_to_ir(dev);
3443 if (!iommu) {
3444 ret = -ENOENT;
3445 goto error;
3448 * setup the mapping between the irq and the IRTE
3449 * base index, the sub_handle pointing to the
3450 * appropriate interrupt remap table entry.
3452 set_irte_irq(irq, iommu, index, sub_handle);
3454 no_ir:
3455 #endif
3456 ret = setup_msi_irq(dev, desc, irq);
3457 if (ret < 0)
3458 goto error;
3459 sub_handle++;
3461 return 0;
3463 error:
3464 destroy_irq(irq);
3465 return ret;
3468 void arch_teardown_msi_irq(unsigned int irq)
3470 destroy_irq(irq);
3473 #ifdef CONFIG_DMAR
3474 #ifdef CONFIG_SMP
3475 static void dmar_msi_set_affinity(unsigned int irq, cpumask_t mask)
3477 struct irq_cfg *cfg;
3478 struct msi_msg msg;
3479 unsigned int dest;
3480 cpumask_t tmp;
3481 struct irq_desc *desc;
3483 cpus_and(tmp, mask, cpu_online_map);
3484 if (cpus_empty(tmp))
3485 return;
3487 if (assign_irq_vector(irq, mask))
3488 return;
3490 cfg = irq_cfg(irq);
3491 cpus_and(tmp, cfg->domain, mask);
3492 dest = cpu_mask_to_apicid(tmp);
3494 dmar_msi_read(irq, &msg);
3496 msg.data &= ~MSI_DATA_VECTOR_MASK;
3497 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3498 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3499 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3501 dmar_msi_write(irq, &msg);
3502 desc = irq_to_desc(irq);
3503 desc->affinity = mask;
3505 #endif /* CONFIG_SMP */
3507 struct irq_chip dmar_msi_type = {
3508 .name = "DMAR_MSI",
3509 .unmask = dmar_msi_unmask,
3510 .mask = dmar_msi_mask,
3511 .ack = ack_apic_edge,
3512 #ifdef CONFIG_SMP
3513 .set_affinity = dmar_msi_set_affinity,
3514 #endif
3515 .retrigger = ioapic_retrigger_irq,
3518 int arch_setup_dmar_msi(unsigned int irq)
3520 int ret;
3521 struct msi_msg msg;
3523 ret = msi_compose_msg(NULL, irq, &msg);
3524 if (ret < 0)
3525 return ret;
3526 dmar_msi_write(irq, &msg);
3527 set_irq_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3528 "edge");
3529 return 0;
3531 #endif
3533 #ifdef CONFIG_HPET_TIMER
3535 #ifdef CONFIG_SMP
3536 static void hpet_msi_set_affinity(unsigned int irq, cpumask_t mask)
3538 struct irq_cfg *cfg;
3539 struct irq_desc *desc;
3540 struct msi_msg msg;
3541 unsigned int dest;
3542 cpumask_t tmp;
3544 cpus_and(tmp, mask, cpu_online_map);
3545 if (cpus_empty(tmp))
3546 return;
3548 if (assign_irq_vector(irq, mask))
3549 return;
3551 cfg = irq_cfg(irq);
3552 cpus_and(tmp, cfg->domain, mask);
3553 dest = cpu_mask_to_apicid(tmp);
3555 hpet_msi_read(irq, &msg);
3557 msg.data &= ~MSI_DATA_VECTOR_MASK;
3558 msg.data |= MSI_DATA_VECTOR(cfg->vector);
3559 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3560 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3562 hpet_msi_write(irq, &msg);
3563 desc = irq_to_desc(irq);
3564 desc->affinity = mask;
3566 #endif /* CONFIG_SMP */
3568 struct irq_chip hpet_msi_type = {
3569 .name = "HPET_MSI",
3570 .unmask = hpet_msi_unmask,
3571 .mask = hpet_msi_mask,
3572 .ack = ack_apic_edge,
3573 #ifdef CONFIG_SMP
3574 .set_affinity = hpet_msi_set_affinity,
3575 #endif
3576 .retrigger = ioapic_retrigger_irq,
3579 int arch_setup_hpet_msi(unsigned int irq)
3581 int ret;
3582 struct msi_msg msg;
3584 ret = msi_compose_msg(NULL, irq, &msg);
3585 if (ret < 0)
3586 return ret;
3588 hpet_msi_write(irq, &msg);
3589 set_irq_chip_and_handler_name(irq, &hpet_msi_type, handle_edge_irq,
3590 "edge");
3592 return 0;
3594 #endif
3596 #endif /* CONFIG_PCI_MSI */
3598 * Hypertransport interrupt support
3600 #ifdef CONFIG_HT_IRQ
3602 #ifdef CONFIG_SMP
3604 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
3606 struct ht_irq_msg msg;
3607 fetch_ht_irq_msg(irq, &msg);
3609 msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
3610 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
3612 msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
3613 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
3615 write_ht_irq_msg(irq, &msg);
3618 static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
3620 struct irq_cfg *cfg;
3621 unsigned int dest;
3622 cpumask_t tmp;
3623 struct irq_desc *desc;
3625 cpus_and(tmp, mask, cpu_online_map);
3626 if (cpus_empty(tmp))
3627 return;
3629 if (assign_irq_vector(irq, mask))
3630 return;
3632 cfg = irq_cfg(irq);
3633 cpus_and(tmp, cfg->domain, mask);
3634 dest = cpu_mask_to_apicid(tmp);
3636 target_ht_irq(irq, dest, cfg->vector);
3637 desc = irq_to_desc(irq);
3638 desc->affinity = mask;
3640 #endif
3642 static struct irq_chip ht_irq_chip = {
3643 .name = "PCI-HT",
3644 .mask = mask_ht_irq,
3645 .unmask = unmask_ht_irq,
3646 .ack = ack_apic_edge,
3647 #ifdef CONFIG_SMP
3648 .set_affinity = set_ht_irq_affinity,
3649 #endif
3650 .retrigger = ioapic_retrigger_irq,
3653 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3655 struct irq_cfg *cfg;
3656 int err;
3657 cpumask_t tmp;
3659 tmp = TARGET_CPUS;
3660 err = assign_irq_vector(irq, tmp);
3661 if (!err) {
3662 struct ht_irq_msg msg;
3663 unsigned dest;
3665 cfg = irq_cfg(irq);
3666 cpus_and(tmp, cfg->domain, tmp);
3667 dest = cpu_mask_to_apicid(tmp);
3669 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
3671 msg.address_lo =
3672 HT_IRQ_LOW_BASE |
3673 HT_IRQ_LOW_DEST_ID(dest) |
3674 HT_IRQ_LOW_VECTOR(cfg->vector) |
3675 ((INT_DEST_MODE == 0) ?
3676 HT_IRQ_LOW_DM_PHYSICAL :
3677 HT_IRQ_LOW_DM_LOGICAL) |
3678 HT_IRQ_LOW_RQEOI_EDGE |
3679 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
3680 HT_IRQ_LOW_MT_FIXED :
3681 HT_IRQ_LOW_MT_ARBITRATED) |
3682 HT_IRQ_LOW_IRQ_MASKED;
3684 write_ht_irq_msg(irq, &msg);
3686 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
3687 handle_edge_irq, "edge");
3689 dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
3691 return err;
3693 #endif /* CONFIG_HT_IRQ */
3695 int __init io_apic_get_redir_entries (int ioapic)
3697 union IO_APIC_reg_01 reg_01;
3698 unsigned long flags;
3700 spin_lock_irqsave(&ioapic_lock, flags);
3701 reg_01.raw = io_apic_read(ioapic, 1);
3702 spin_unlock_irqrestore(&ioapic_lock, flags);
3704 return reg_01.bits.entries;
3707 int __init probe_nr_irqs(void)
3709 int idx;
3710 int nr = 0;
3711 #ifndef CONFIG_XEN
3712 int nr_min = 32;
3713 #else
3714 int nr_min = NR_IRQS;
3715 #endif
3717 for (idx = 0; idx < nr_ioapics; idx++)
3718 nr += io_apic_get_redir_entries(idx) + 1;
3720 /* double it for hotplug and msi and nmi */
3721 nr <<= 1;
3723 /* something wrong ? */
3724 if (nr < nr_min)
3725 nr = nr_min;
3727 return nr;
3730 /* --------------------------------------------------------------------------
3731 ACPI-based IOAPIC Configuration
3732 -------------------------------------------------------------------------- */
3734 #ifdef CONFIG_ACPI
3736 #ifdef CONFIG_X86_32
3737 int __init io_apic_get_unique_id(int ioapic, int apic_id)
3739 union IO_APIC_reg_00 reg_00;
3740 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3741 physid_mask_t tmp;
3742 unsigned long flags;
3743 int i = 0;
3746 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3747 * buses (one for LAPICs, one for IOAPICs), where predecessors only
3748 * supports up to 16 on one shared APIC bus.
3750 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3751 * advantage of new APIC bus architecture.
3754 if (physids_empty(apic_id_map))
3755 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
3757 spin_lock_irqsave(&ioapic_lock, flags);
3758 reg_00.raw = io_apic_read(ioapic, 0);
3759 spin_unlock_irqrestore(&ioapic_lock, flags);
3761 if (apic_id >= get_physical_broadcast()) {
3762 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3763 "%d\n", ioapic, apic_id, reg_00.bits.ID);
3764 apic_id = reg_00.bits.ID;
3768 * Every APIC in a system must have a unique ID or we get lots of nice
3769 * 'stuck on smp_invalidate_needed IPI wait' messages.
3771 if (check_apicid_used(apic_id_map, apic_id)) {
3773 for (i = 0; i < get_physical_broadcast(); i++) {
3774 if (!check_apicid_used(apic_id_map, i))
3775 break;
3778 if (i == get_physical_broadcast())
3779 panic("Max apic_id exceeded!\n");
3781 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3782 "trying %d\n", ioapic, apic_id, i);
3784 apic_id = i;
3787 tmp = apicid_to_cpu_present(apic_id);
3788 physids_or(apic_id_map, apic_id_map, tmp);
3790 if (reg_00.bits.ID != apic_id) {
3791 reg_00.bits.ID = apic_id;
3793 spin_lock_irqsave(&ioapic_lock, flags);
3794 io_apic_write(ioapic, 0, reg_00.raw);
3795 reg_00.raw = io_apic_read(ioapic, 0);
3796 spin_unlock_irqrestore(&ioapic_lock, flags);
3798 /* Sanity check */
3799 if (reg_00.bits.ID != apic_id) {
3800 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
3801 return -1;
3805 apic_printk(APIC_VERBOSE, KERN_INFO
3806 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3808 return apic_id;
3811 int __init io_apic_get_version(int ioapic)
3813 union IO_APIC_reg_01 reg_01;
3814 unsigned long flags;
3816 spin_lock_irqsave(&ioapic_lock, flags);
3817 reg_01.raw = io_apic_read(ioapic, 1);
3818 spin_unlock_irqrestore(&ioapic_lock, flags);
3820 return reg_01.bits.version;
3822 #endif
3824 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int triggering, int polarity)
3826 if (!IO_APIC_IRQ(irq)) {
3827 apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3828 ioapic);
3829 return -EINVAL;
3833 * IRQs < 16 are already in the irq_2_pin[] map
3835 if (irq >= 16)
3836 add_pin_to_irq(irq, ioapic, pin);
3838 setup_IO_APIC_irq(ioapic, pin, irq, triggering, polarity);
3840 return 0;
3844 int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
3846 int i;
3848 if (skip_ioapic_setup)
3849 return -1;
3851 for (i = 0; i < mp_irq_entries; i++)
3852 if (mp_irqs[i].mp_irqtype == mp_INT &&
3853 mp_irqs[i].mp_srcbusirq == bus_irq)
3854 break;
3855 if (i >= mp_irq_entries)
3856 return -1;
3858 *trigger = irq_trigger(i);
3859 *polarity = irq_polarity(i);
3860 return 0;
3863 #endif /* CONFIG_ACPI */
3866 * This function currently is only a helper for the i386 smp boot process where
3867 * we need to reprogram the ioredtbls to cater for the cpus which have come online
3868 * so mask in all cases should simply be TARGET_CPUS
3870 #ifdef CONFIG_SMP
3871 void __init setup_ioapic_dest(void)
3873 int pin, ioapic, irq, irq_entry;
3874 struct irq_cfg *cfg;
3876 if (skip_ioapic_setup == 1)
3877 return;
3879 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
3880 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
3881 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3882 if (irq_entry == -1)
3883 continue;
3884 irq = pin_2_irq(irq_entry, ioapic, pin);
3886 /* setup_IO_APIC_irqs could fail to get vector for some device
3887 * when you have too many devices, because at that time only boot
3888 * cpu is online.
3890 cfg = irq_cfg(irq);
3891 if (!cfg->vector)
3892 setup_IO_APIC_irq(ioapic, pin, irq,
3893 irq_trigger(irq_entry),
3894 irq_polarity(irq_entry));
3895 #ifdef CONFIG_INTR_REMAP
3896 else if (intr_remapping_enabled)
3897 set_ir_ioapic_affinity_irq(irq, TARGET_CPUS);
3898 #endif
3899 else
3900 set_ioapic_affinity_irq(irq, TARGET_CPUS);
3905 #endif
3907 #define IOAPIC_RESOURCE_NAME_SIZE 11
3909 static struct resource *ioapic_resources;
3911 static struct resource * __init ioapic_setup_resources(void)
3913 unsigned long n;
3914 struct resource *res;
3915 char *mem;
3916 int i;
3918 if (nr_ioapics <= 0)
3919 return NULL;
3921 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3922 n *= nr_ioapics;
3924 mem = alloc_bootmem(n);
3925 res = (void *)mem;
3927 if (mem != NULL) {
3928 mem += sizeof(struct resource) * nr_ioapics;
3930 for (i = 0; i < nr_ioapics; i++) {
3931 res[i].name = mem;
3932 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3933 sprintf(mem, "IOAPIC %u", i);
3934 mem += IOAPIC_RESOURCE_NAME_SIZE;
3938 ioapic_resources = res;
3940 return res;
3943 void __init ioapic_init_mappings(void)
3945 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3946 int i;
3947 struct resource *ioapic_res;
3949 ioapic_res = ioapic_setup_resources();
3950 for (i = 0; i < nr_ioapics; i++) {
3951 if (smp_found_config) {
3952 ioapic_phys = mp_ioapics[i].mp_apicaddr;
3953 #ifdef CONFIG_X86_32
3954 if (!ioapic_phys) {
3955 printk(KERN_ERR
3956 "WARNING: bogus zero IO-APIC "
3957 "address found in MPTABLE, "
3958 "disabling IO/APIC support!\n");
3959 smp_found_config = 0;
3960 skip_ioapic_setup = 1;
3961 goto fake_ioapic_page;
3963 #endif
3964 } else {
3965 #ifdef CONFIG_X86_32
3966 fake_ioapic_page:
3967 #endif
3968 ioapic_phys = (unsigned long)
3969 alloc_bootmem_pages(PAGE_SIZE);
3970 ioapic_phys = __pa(ioapic_phys);
3972 set_fixmap_nocache(idx, ioapic_phys);
3973 apic_printk(APIC_VERBOSE,
3974 "mapped IOAPIC to %08lx (%08lx)\n",
3975 __fix_to_virt(idx), ioapic_phys);
3976 idx++;
3978 if (ioapic_res != NULL) {
3979 ioapic_res->start = ioapic_phys;
3980 ioapic_res->end = ioapic_phys + (4 * 1024) - 1;
3981 ioapic_res++;
3986 static int __init ioapic_insert_resources(void)
3988 int i;
3989 struct resource *r = ioapic_resources;
3991 if (!r) {
3992 printk(KERN_ERR
3993 "IO APIC resources could be not be allocated.\n");
3994 return -1;
3997 for (i = 0; i < nr_ioapics; i++) {
3998 insert_resource(&iomem_resource, r);
3999 r++;
4002 return 0;
4005 /* Insert the IO APIC resources after PCI initialization has occured to handle
4006 * IO APICS that are mapped in on a BAR in PCI space. */
4007 late_initcall(ioapic_insert_resources);