2 * Derived from arch/i386/kernel/irq.c
3 * Copyright (C) 1992 Linus Torvalds
4 * Adapted from arch/i386 by Gary Thomas
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 * Updated and modified by Cort Dougan <cort@fsmlabs.com>
7 * Copyright (C) 1996-2001 Cort Dougan
8 * Adapted for Power Macintosh by Paul Mackerras
9 * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
10 * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
17 * This file contains the code used by various IRQ handling routines:
18 * asking for different IRQ's should be done through these routines
19 * instead of just grabbing them. Thus setups with different IRQ numbers
20 * shouldn't result in any weird surprises, and installing new handlers
23 * The MPC8xx has an interrupt mask in the SIU. If a bit is set, the
24 * interrupt is _enabled_. As expected, IRQ0 is bit 0 in the 32-bit
25 * mask register (of which only 16 are defined), hence the weird shifting
26 * and complement of the cached_irq_mask. I want to be able to stuff
27 * this right into the SIU SMASK register.
28 * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
29 * to reduce code space and undefined function references.
34 #include <linux/module.h>
35 #include <linux/threads.h>
36 #include <linux/kernel_stat.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 #include <linux/ptrace.h>
40 #include <linux/ioport.h>
41 #include <linux/interrupt.h>
42 #include <linux/timex.h>
43 #include <linux/init.h>
44 #include <linux/slab.h>
45 #include <linux/delay.h>
46 #include <linux/irq.h>
47 #include <linux/seq_file.h>
48 #include <linux/cpumask.h>
49 #include <linux/profile.h>
50 #include <linux/bitops.h>
51 #include <linux/list.h>
52 #include <linux/radix-tree.h>
53 #include <linux/mutex.h>
54 #include <linux/bootmem.h>
55 #include <linux/pci.h>
57 #include <asm/uaccess.h>
58 #include <asm/system.h>
60 #include <asm/pgtable.h>
62 #include <asm/cache.h>
64 #include <asm/ptrace.h>
65 #include <asm/machdep.h>
67 #ifdef CONFIG_PPC_ISERIES
71 int __irq_offset_value;
72 static int ppc_spurious_interrupts;
75 EXPORT_SYMBOL(__irq_offset_value);
76 atomic_t ppc_n_lost_interrupts;
78 #ifndef CONFIG_PPC_MERGE
79 #define NR_MASK_WORDS ((NR_IRQS + 31) / 32)
80 unsigned long ppc_cached_irq_mask[NR_MASK_WORDS];
84 extern int tau_initialized;
85 extern int tau_interrupts(int);
87 #endif /* CONFIG_PPC32 */
89 #if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
90 extern atomic_t ipi_recv;
91 extern atomic_t ipi_sent;
95 EXPORT_SYMBOL(irq_desc);
97 int distribute_irqs = 1;
98 #endif /* CONFIG_PPC64 */
100 int show_interrupts(struct seq_file *p, void *v)
102 int i = *(loff_t *)v, j;
103 struct irqaction *action;
109 for_each_online_cpu(j)
110 seq_printf(p, "CPU%d ", j);
115 desc = get_irq_desc(i);
116 spin_lock_irqsave(&desc->lock, flags);
117 action = desc->action;
118 if (!action || !action->handler)
120 seq_printf(p, "%3d: ", i);
122 for_each_online_cpu(j)
123 seq_printf(p, "%10u ", kstat_cpu(j).irqs[i]);
125 seq_printf(p, "%10u ", kstat_irqs(i));
126 #endif /* CONFIG_SMP */
128 seq_printf(p, " %s ", desc->chip->typename);
130 seq_puts(p, " None ");
131 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge ");
132 seq_printf(p, " %s", action->name);
133 for (action = action->next; action; action = action->next)
134 seq_printf(p, ", %s", action->name);
137 spin_unlock_irqrestore(&desc->lock, flags);
138 } else if (i == NR_IRQS) {
140 #ifdef CONFIG_TAU_INT
141 if (tau_initialized){
142 seq_puts(p, "TAU: ");
143 for_each_online_cpu(j)
144 seq_printf(p, "%10u ", tau_interrupts(j));
145 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
148 #if defined(CONFIG_SMP) && !defined(CONFIG_PPC_MERGE)
149 /* should this be per processor send/receive? */
150 seq_printf(p, "IPI (recv/sent): %10u/%u\n",
151 atomic_read(&ipi_recv), atomic_read(&ipi_sent));
153 #endif /* CONFIG_PPC32 */
154 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
159 #ifdef CONFIG_HOTPLUG_CPU
160 void fixup_irqs(cpumask_t map)
168 if (irq_desc[irq].status & IRQ_PER_CPU)
171 cpus_and(mask, irq_desc[irq].affinity, map);
172 if (any_online_cpu(mask) == NR_CPUS) {
173 printk("Breaking affinity for irq %i\n", irq);
176 if (irq_desc[irq].chip->set_affinity)
177 irq_desc[irq].chip->set_affinity(irq, mask);
178 else if (irq_desc[irq].action && !(warned++))
179 printk("Cannot set affinity for irq %i\n", irq);
188 void do_IRQ(struct pt_regs *regs)
190 struct pt_regs *old_regs = set_irq_regs(regs);
192 #ifdef CONFIG_IRQSTACKS
193 struct thread_info *curtp, *irqtp;
198 #ifdef CONFIG_DEBUG_STACKOVERFLOW
199 /* Debugging check for stack overflow: is there less than 2KB free? */
203 sp = __get_SP() & (THREAD_SIZE-1);
205 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
206 printk("do_IRQ: stack overflow: %ld\n",
207 sp - sizeof(struct thread_info));
214 * Every platform is required to implement ppc_md.get_irq.
215 * This function will either return an irq number or -1 to
216 * indicate there are no more pending.
217 * The value -2 is for buggy hardware and means that this IRQ
218 * has already been handled. -- Tom
220 irq = ppc_md.get_irq();
222 if (irq != NO_IRQ && irq != NO_IRQ_IGNORE) {
223 #ifdef CONFIG_IRQSTACKS
224 /* Switch to the irq stack to handle this */
225 curtp = current_thread_info();
226 irqtp = hardirq_ctx[smp_processor_id()];
227 if (curtp != irqtp) {
228 struct irq_desc *desc = irq_desc + irq;
229 void *handler = desc->handle_irq;
232 irqtp->task = curtp->task;
234 call_handle_irq(irq, desc, irqtp, handler);
237 set_bits(irqtp->flags, &curtp->flags);
240 generic_handle_irq(irq);
241 } else if (irq != NO_IRQ_IGNORE)
242 /* That's not SMP safe ... but who cares ? */
243 ppc_spurious_interrupts++;
246 set_irq_regs(old_regs);
248 #ifdef CONFIG_PPC_ISERIES
249 if (get_lppaca()->int_dword.fields.decr_int) {
250 get_lppaca()->int_dword.fields.decr_int = 0;
251 /* Signal a fake decrementer interrupt */
252 timer_interrupt(regs);
257 void __init init_IRQ(void)
266 #ifdef CONFIG_IRQSTACKS
267 struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
268 struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
270 void irq_ctx_init(void)
272 struct thread_info *tp;
275 for_each_possible_cpu(i) {
276 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
279 tp->preempt_count = SOFTIRQ_OFFSET;
281 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
284 tp->preempt_count = HARDIRQ_OFFSET;
288 static inline void do_softirq_onstack(void)
290 struct thread_info *curtp, *irqtp;
292 curtp = current_thread_info();
293 irqtp = softirq_ctx[smp_processor_id()];
294 irqtp->task = curtp->task;
295 call_do_softirq(irqtp);
300 #define do_softirq_onstack() __do_softirq()
301 #endif /* CONFIG_IRQSTACKS */
303 void do_softirq(void)
310 local_irq_save(flags);
312 if (local_softirq_pending())
313 do_softirq_onstack();
315 local_irq_restore(flags);
317 EXPORT_SYMBOL(do_softirq);
321 * IRQ controller and virtual interrupts
324 #ifdef CONFIG_PPC_MERGE
326 static LIST_HEAD(irq_hosts);
327 static spinlock_t irq_big_lock = SPIN_LOCK_UNLOCKED;
328 static DEFINE_PER_CPU(unsigned int, irq_radix_reader);
329 static unsigned int irq_radix_writer;
330 struct irq_map_entry irq_map[NR_IRQS];
331 static unsigned int irq_virq_count = NR_IRQS;
332 static struct irq_host *irq_default_host;
334 struct irq_host *irq_alloc_host(unsigned int revmap_type,
335 unsigned int revmap_arg,
336 struct irq_host_ops *ops,
337 irq_hw_number_t inval_irq)
339 struct irq_host *host;
340 unsigned int size = sizeof(struct irq_host);
345 /* Allocate structure and revmap table if using linear mapping */
346 if (revmap_type == IRQ_HOST_MAP_LINEAR)
347 size += revmap_arg * sizeof(unsigned int);
349 host = kzalloc(size, GFP_KERNEL);
351 host = alloc_bootmem(size);
353 memset(host, 0, size);
359 host->revmap_type = revmap_type;
360 host->inval_irq = inval_irq;
363 spin_lock_irqsave(&irq_big_lock, flags);
365 /* If it's a legacy controller, check for duplicates and
366 * mark it as allocated (we use irq 0 host pointer for that
368 if (revmap_type == IRQ_HOST_MAP_LEGACY) {
369 if (irq_map[0].host != NULL) {
370 spin_unlock_irqrestore(&irq_big_lock, flags);
371 /* If we are early boot, we can't free the structure,
373 * this will be fixed once slab is made available early
374 * instead of the current cruft
380 irq_map[0].host = host;
383 list_add(&host->link, &irq_hosts);
384 spin_unlock_irqrestore(&irq_big_lock, flags);
386 /* Additional setups per revmap type */
387 switch(revmap_type) {
388 case IRQ_HOST_MAP_LEGACY:
389 /* 0 is always the invalid number for legacy */
391 /* setup us as the host for all legacy interrupts */
392 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
393 irq_map[i].hwirq = 0;
395 irq_map[i].host = host;
398 /* Clear norequest flags */
399 get_irq_desc(i)->status &= ~IRQ_NOREQUEST;
401 /* Legacy flags are left to default at this point,
402 * one can then use irq_create_mapping() to
403 * explicitely change them
405 ops->map(host, i, i);
408 case IRQ_HOST_MAP_LINEAR:
409 rmap = (unsigned int *)(host + 1);
410 for (i = 0; i < revmap_arg; i++)
412 host->revmap_data.linear.size = revmap_arg;
414 host->revmap_data.linear.revmap = rmap;
420 pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
425 struct irq_host *irq_find_host(struct device_node *node)
427 struct irq_host *h, *found = NULL;
430 /* We might want to match the legacy controller last since
431 * it might potentially be set to match all interrupts in
432 * the absence of a device node. This isn't a problem so far
435 spin_lock_irqsave(&irq_big_lock, flags);
436 list_for_each_entry(h, &irq_hosts, link)
437 if (h->ops->match == NULL || h->ops->match(h, node)) {
441 spin_unlock_irqrestore(&irq_big_lock, flags);
444 EXPORT_SYMBOL_GPL(irq_find_host);
446 void irq_set_default_host(struct irq_host *host)
448 pr_debug("irq: Default host set to @0x%p\n", host);
450 irq_default_host = host;
453 void irq_set_virq_count(unsigned int count)
455 pr_debug("irq: Trying to set virq count to %d\n", count);
457 BUG_ON(count < NUM_ISA_INTERRUPTS);
459 irq_virq_count = count;
462 /* radix tree not lockless safe ! we use a brlock-type mecanism
463 * for now, until we can use a lockless radix tree
465 static void irq_radix_wrlock(unsigned long *flags)
467 unsigned int cpu, ok;
469 spin_lock_irqsave(&irq_big_lock, *flags);
470 irq_radix_writer = 1;
475 for_each_possible_cpu(cpu) {
476 if (per_cpu(irq_radix_reader, cpu)) {
486 static void irq_radix_wrunlock(unsigned long flags)
489 irq_radix_writer = 0;
490 spin_unlock_irqrestore(&irq_big_lock, flags);
493 static void irq_radix_rdlock(unsigned long *flags)
495 local_irq_save(*flags);
496 __get_cpu_var(irq_radix_reader) = 1;
498 if (likely(irq_radix_writer == 0))
500 __get_cpu_var(irq_radix_reader) = 0;
502 spin_lock(&irq_big_lock);
503 __get_cpu_var(irq_radix_reader) = 1;
504 spin_unlock(&irq_big_lock);
507 static void irq_radix_rdunlock(unsigned long flags)
509 __get_cpu_var(irq_radix_reader) = 0;
510 local_irq_restore(flags);
514 unsigned int irq_create_mapping(struct irq_host *host,
515 irq_hw_number_t hwirq)
517 unsigned int virq, hint;
519 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
521 /* Look for default host if nececssary */
523 host = irq_default_host;
525 printk(KERN_WARNING "irq_create_mapping called for"
526 " NULL host, hwirq=%lx\n", hwirq);
530 pr_debug("irq: -> using host @%p\n", host);
532 /* Check if mapping already exist, if it does, call
533 * host->ops->map() to update the flags
535 virq = irq_find_mapping(host, hwirq);
536 if (virq != IRQ_NONE) {
537 pr_debug("irq: -> existing mapping on virq %d\n", virq);
541 /* Get a virtual interrupt number */
542 if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
544 virq = (unsigned int)hwirq;
545 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
549 /* Allocate a virtual interrupt number */
550 hint = hwirq % irq_virq_count;
551 virq = irq_alloc_virt(host, 1, hint);
552 if (virq == NO_IRQ) {
553 pr_debug("irq: -> virq allocation failed\n");
557 pr_debug("irq: -> obtained virq %d\n", virq);
559 /* Clear IRQ_NOREQUEST flag */
560 get_irq_desc(virq)->status &= ~IRQ_NOREQUEST;
564 irq_map[virq].hwirq = hwirq;
566 if (host->ops->map(host, virq, hwirq)) {
567 pr_debug("irq: -> mapping failed, freeing\n");
568 irq_free_virt(virq, 1);
573 EXPORT_SYMBOL_GPL(irq_create_mapping);
575 extern unsigned int irq_create_of_mapping(struct device_node *controller,
576 u32 *intspec, unsigned int intsize)
578 struct irq_host *host;
579 irq_hw_number_t hwirq;
580 unsigned int type = IRQ_TYPE_NONE;
583 if (controller == NULL)
584 host = irq_default_host;
586 host = irq_find_host(controller);
588 printk(KERN_WARNING "irq: no irq host found for %s !\n",
589 controller->full_name);
593 /* If host has no translation, then we assume interrupt line */
594 if (host->ops->xlate == NULL)
597 if (host->ops->xlate(host, controller, intspec, intsize,
603 virq = irq_create_mapping(host, hwirq);
607 /* Set type if specified and different than the current one */
608 if (type != IRQ_TYPE_NONE &&
609 type != (get_irq_desc(virq)->status & IRQF_TRIGGER_MASK))
610 set_irq_type(virq, type);
613 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
615 unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
619 if (of_irq_map_one(dev, index, &oirq))
622 return irq_create_of_mapping(oirq.controller, oirq.specifier,
625 EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
627 void irq_dispose_mapping(unsigned int virq)
629 struct irq_host *host = irq_map[virq].host;
630 irq_hw_number_t hwirq;
633 WARN_ON (host == NULL);
637 /* Never unmap legacy interrupts */
638 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
641 /* remove chip and handler */
642 set_irq_chip_and_handler(virq, NULL, NULL);
644 /* Make sure it's completed */
645 synchronize_irq(virq);
647 /* Tell the PIC about it */
648 if (host->ops->unmap)
649 host->ops->unmap(host, virq);
652 /* Clear reverse map */
653 hwirq = irq_map[virq].hwirq;
654 switch(host->revmap_type) {
655 case IRQ_HOST_MAP_LINEAR:
656 if (hwirq < host->revmap_data.linear.size)
657 host->revmap_data.linear.revmap[hwirq] = IRQ_NONE;
659 case IRQ_HOST_MAP_TREE:
660 /* Check if radix tree allocated yet */
661 if (host->revmap_data.tree.gfp_mask == 0)
663 irq_radix_wrlock(&flags);
664 radix_tree_delete(&host->revmap_data.tree, hwirq);
665 irq_radix_wrunlock(flags);
671 irq_map[virq].hwirq = host->inval_irq;
674 get_irq_desc(virq)->status |= IRQ_NOREQUEST;
677 irq_free_virt(virq, 1);
679 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
681 unsigned int irq_find_mapping(struct irq_host *host,
682 irq_hw_number_t hwirq)
685 unsigned int hint = hwirq % irq_virq_count;
687 /* Look for default host if nececssary */
689 host = irq_default_host;
693 /* legacy -> bail early */
694 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
697 /* Slow path does a linear search of the map */
698 if (hint < NUM_ISA_INTERRUPTS)
699 hint = NUM_ISA_INTERRUPTS;
702 if (irq_map[i].host == host &&
703 irq_map[i].hwirq == hwirq)
706 if (i >= irq_virq_count)
707 i = NUM_ISA_INTERRUPTS;
711 EXPORT_SYMBOL_GPL(irq_find_mapping);
714 unsigned int irq_radix_revmap(struct irq_host *host,
715 irq_hw_number_t hwirq)
717 struct radix_tree_root *tree;
718 struct irq_map_entry *ptr;
722 WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
724 /* Check if the radix tree exist yet. We test the value of
725 * the gfp_mask for that. Sneaky but saves another int in the
726 * structure. If not, we fallback to slow mode
728 tree = &host->revmap_data.tree;
729 if (tree->gfp_mask == 0)
730 return irq_find_mapping(host, hwirq);
732 /* Now try to resolve */
733 irq_radix_rdlock(&flags);
734 ptr = radix_tree_lookup(tree, hwirq);
735 irq_radix_rdunlock(flags);
737 /* Found it, return */
739 virq = ptr - irq_map;
743 /* If not there, try to insert it */
744 virq = irq_find_mapping(host, hwirq);
745 if (virq != NO_IRQ) {
746 irq_radix_wrlock(&flags);
747 radix_tree_insert(tree, hwirq, &irq_map[virq]);
748 irq_radix_wrunlock(flags);
753 unsigned int irq_linear_revmap(struct irq_host *host,
754 irq_hw_number_t hwirq)
756 unsigned int *revmap;
758 WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
760 /* Check revmap bounds */
761 if (unlikely(hwirq >= host->revmap_data.linear.size))
762 return irq_find_mapping(host, hwirq);
764 /* Check if revmap was allocated */
765 revmap = host->revmap_data.linear.revmap;
766 if (unlikely(revmap == NULL))
767 return irq_find_mapping(host, hwirq);
769 /* Fill up revmap with slow path if no mapping found */
770 if (unlikely(revmap[hwirq] == NO_IRQ))
771 revmap[hwirq] = irq_find_mapping(host, hwirq);
773 return revmap[hwirq];
776 unsigned int irq_alloc_virt(struct irq_host *host,
781 unsigned int i, j, found = NO_IRQ;
783 if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
786 spin_lock_irqsave(&irq_big_lock, flags);
788 /* Use hint for 1 interrupt if any */
789 if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
790 hint < irq_virq_count && irq_map[hint].host == NULL) {
795 /* Look for count consecutive numbers in the allocatable
798 for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
799 if (irq_map[i].host != NULL)
805 found = i - count + 1;
809 if (found == NO_IRQ) {
810 spin_unlock_irqrestore(&irq_big_lock, flags);
814 for (i = found; i < (found + count); i++) {
815 irq_map[i].hwirq = host->inval_irq;
817 irq_map[i].host = host;
819 spin_unlock_irqrestore(&irq_big_lock, flags);
823 void irq_free_virt(unsigned int virq, unsigned int count)
828 WARN_ON (virq < NUM_ISA_INTERRUPTS);
829 WARN_ON (count == 0 || (virq + count) > irq_virq_count);
831 spin_lock_irqsave(&irq_big_lock, flags);
832 for (i = virq; i < (virq + count); i++) {
833 struct irq_host *host;
835 if (i < NUM_ISA_INTERRUPTS ||
836 (virq + count) > irq_virq_count)
839 host = irq_map[i].host;
840 irq_map[i].hwirq = host->inval_irq;
842 irq_map[i].host = NULL;
844 spin_unlock_irqrestore(&irq_big_lock, flags);
847 void irq_early_init(void)
851 for (i = 0; i < NR_IRQS; i++)
852 get_irq_desc(i)->status |= IRQ_NOREQUEST;
855 /* We need to create the radix trees late */
856 static int irq_late_init(void)
861 irq_radix_wrlock(&flags);
862 list_for_each_entry(h, &irq_hosts, link) {
863 if (h->revmap_type == IRQ_HOST_MAP_TREE)
864 INIT_RADIX_TREE(&h->revmap_data.tree, GFP_ATOMIC);
866 irq_radix_wrunlock(flags);
870 arch_initcall(irq_late_init);
872 #endif /* CONFIG_PPC_MERGE */
874 #ifdef CONFIG_PCI_MSI
875 int pci_enable_msi(struct pci_dev * pdev)
877 if (ppc_md.enable_msi)
878 return ppc_md.enable_msi(pdev);
882 EXPORT_SYMBOL(pci_enable_msi);
884 void pci_disable_msi(struct pci_dev * pdev)
886 if (ppc_md.disable_msi)
887 ppc_md.disable_msi(pdev);
889 EXPORT_SYMBOL(pci_disable_msi);
891 void pci_scan_msi_device(struct pci_dev *dev) {}
892 int pci_enable_msix(struct pci_dev* dev, struct msix_entry *entries, int nvec) {return -1;}
893 void pci_disable_msix(struct pci_dev *dev) {}
894 void msi_remove_pci_irq_vectors(struct pci_dev *dev) {}
895 void disable_msi_mode(struct pci_dev *dev, int pos, int type) {}
896 void pci_no_msi(void) {}
897 EXPORT_SYMBOL(pci_enable_msix);
898 EXPORT_SYMBOL(pci_disable_msix);
903 static int __init setup_noirqdistrib(char *str)
909 __setup("noirqdistrib", setup_noirqdistrib);
910 #endif /* CONFIG_PPC64 */