powerpc: Reduce footprint of irq_stat
[linux-2.6/kvm.git] / arch / powerpc / kernel / irq.c
blobc6ac5583672a7b73f3733cc5f389688ad4985296
1 /*
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)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
16 * This file contains the code used by various IRQ handling routines:
17 * asking for different IRQ's should be done through these routines
18 * instead of just grabbing them. Thus setups with different IRQ numbers
19 * shouldn't result in any weird surprises, and installing new handlers
20 * should be easier.
22 * The MPC8xx has an interrupt mask in the SIU. If a bit is set, the
23 * interrupt is _enabled_. As expected, IRQ0 is bit 0 in the 32-bit
24 * mask register (of which only 16 are defined), hence the weird shifting
25 * and complement of the cached_irq_mask. I want to be able to stuff
26 * this right into the SIU SMASK register.
27 * Many of the prep/chrp functions are conditional compiled on CONFIG_8xx
28 * to reduce code space and undefined function references.
31 #undef DEBUG
33 #include <linux/module.h>
34 #include <linux/threads.h>
35 #include <linux/kernel_stat.h>
36 #include <linux/signal.h>
37 #include <linux/sched.h>
38 #include <linux/ptrace.h>
39 #include <linux/ioport.h>
40 #include <linux/interrupt.h>
41 #include <linux/timex.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/delay.h>
45 #include <linux/irq.h>
46 #include <linux/seq_file.h>
47 #include <linux/cpumask.h>
48 #include <linux/profile.h>
49 #include <linux/bitops.h>
50 #include <linux/list.h>
51 #include <linux/radix-tree.h>
52 #include <linux/mutex.h>
53 #include <linux/bootmem.h>
54 #include <linux/pci.h>
55 #include <linux/debugfs.h>
56 #include <linux/perf_event.h>
58 #include <asm/uaccess.h>
59 #include <asm/system.h>
60 #include <asm/io.h>
61 #include <asm/pgtable.h>
62 #include <asm/irq.h>
63 #include <asm/cache.h>
64 #include <asm/prom.h>
65 #include <asm/ptrace.h>
66 #include <asm/machdep.h>
67 #include <asm/udbg.h>
68 #ifdef CONFIG_PPC64
69 #include <asm/paca.h>
70 #include <asm/firmware.h>
71 #include <asm/lv1call.h>
72 #endif
73 #define CREATE_TRACE_POINTS
74 #include <asm/trace.h>
76 DEFINE_PER_CPU_SHARED_ALIGNED(irq_cpustat_t, irq_stat);
77 EXPORT_PER_CPU_SYMBOL(irq_stat);
79 int __irq_offset_value;
80 static int ppc_spurious_interrupts;
82 #ifdef CONFIG_PPC32
83 EXPORT_SYMBOL(__irq_offset_value);
84 atomic_t ppc_n_lost_interrupts;
86 #ifdef CONFIG_TAU_INT
87 extern int tau_initialized;
88 extern int tau_interrupts(int);
89 #endif
90 #endif /* CONFIG_PPC32 */
92 #ifdef CONFIG_PPC64
94 #ifndef CONFIG_SPARSE_IRQ
95 EXPORT_SYMBOL(irq_desc);
96 #endif
98 int distribute_irqs = 1;
100 static inline notrace unsigned long get_hard_enabled(void)
102 unsigned long enabled;
104 __asm__ __volatile__("lbz %0,%1(13)"
105 : "=r" (enabled) : "i" (offsetof(struct paca_struct, hard_enabled)));
107 return enabled;
110 static inline notrace void set_soft_enabled(unsigned long enable)
112 __asm__ __volatile__("stb %0,%1(13)"
113 : : "r" (enable), "i" (offsetof(struct paca_struct, soft_enabled)));
116 notrace void raw_local_irq_restore(unsigned long en)
119 * get_paca()->soft_enabled = en;
120 * Is it ever valid to use local_irq_restore(0) when soft_enabled is 1?
121 * That was allowed before, and in such a case we do need to take care
122 * that gcc will set soft_enabled directly via r13, not choose to use
123 * an intermediate register, lest we're preempted to a different cpu.
125 set_soft_enabled(en);
126 if (!en)
127 return;
129 #ifdef CONFIG_PPC_STD_MMU_64
130 if (firmware_has_feature(FW_FEATURE_ISERIES)) {
132 * Do we need to disable preemption here? Not really: in the
133 * unlikely event that we're preempted to a different cpu in
134 * between getting r13, loading its lppaca_ptr, and loading
135 * its any_int, we might call iseries_handle_interrupts without
136 * an interrupt pending on the new cpu, but that's no disaster,
137 * is it? And the business of preempting us off the old cpu
138 * would itself involve a local_irq_restore which handles the
139 * interrupt to that cpu.
141 * But use "local_paca->lppaca_ptr" instead of "get_lppaca()"
142 * to avoid any preemption checking added into get_paca().
144 if (local_paca->lppaca_ptr->int_dword.any_int)
145 iseries_handle_interrupts();
147 #endif /* CONFIG_PPC_STD_MMU_64 */
149 if (test_perf_event_pending()) {
150 clear_perf_event_pending();
151 perf_event_do_pending();
155 * if (get_paca()->hard_enabled) return;
156 * But again we need to take care that gcc gets hard_enabled directly
157 * via r13, not choose to use an intermediate register, lest we're
158 * preempted to a different cpu in between the two instructions.
160 if (get_hard_enabled())
161 return;
164 * Need to hard-enable interrupts here. Since currently disabled,
165 * no need to take further asm precautions against preemption; but
166 * use local_paca instead of get_paca() to avoid preemption checking.
168 local_paca->hard_enabled = en;
169 if ((int)mfspr(SPRN_DEC) < 0)
170 mtspr(SPRN_DEC, 1);
173 * Force the delivery of pending soft-disabled interrupts on PS3.
174 * Any HV call will have this side effect.
176 if (firmware_has_feature(FW_FEATURE_PS3_LV1)) {
177 u64 tmp;
178 lv1_get_version_info(&tmp);
181 __hard_irq_enable();
183 EXPORT_SYMBOL(raw_local_irq_restore);
184 #endif /* CONFIG_PPC64 */
186 int show_interrupts(struct seq_file *p, void *v)
188 int i = *(loff_t *)v, j;
189 struct irqaction *action;
190 struct irq_desc *desc;
191 unsigned long flags;
193 if (i == 0) {
194 seq_puts(p, " ");
195 for_each_online_cpu(j)
196 seq_printf(p, "CPU%d ", j);
197 seq_putc(p, '\n');
198 } else if (i == nr_irqs) {
199 #if defined(CONFIG_PPC32) && defined(CONFIG_TAU_INT)
200 if (tau_initialized){
201 seq_puts(p, "TAU: ");
202 for_each_online_cpu(j)
203 seq_printf(p, "%10u ", tau_interrupts(j));
204 seq_puts(p, " PowerPC Thermal Assist (cpu temp)\n");
206 #endif /* CONFIG_PPC32 && CONFIG_TAU_INT*/
207 seq_printf(p, "BAD: %10u\n", ppc_spurious_interrupts);
209 return 0;
212 desc = irq_to_desc(i);
213 if (!desc)
214 return 0;
216 raw_spin_lock_irqsave(&desc->lock, flags);
218 action = desc->action;
219 if (!action || !action->handler)
220 goto skip;
222 seq_printf(p, "%3d: ", i);
223 #ifdef CONFIG_SMP
224 for_each_online_cpu(j)
225 seq_printf(p, "%10u ", kstat_irqs_cpu(i, j));
226 #else
227 seq_printf(p, "%10u ", kstat_irqs(i));
228 #endif /* CONFIG_SMP */
230 if (desc->chip)
231 seq_printf(p, " %s ", desc->chip->name);
232 else
233 seq_puts(p, " None ");
235 seq_printf(p, "%s", (desc->status & IRQ_LEVEL) ? "Level " : "Edge ");
236 seq_printf(p, " %s", action->name);
238 for (action = action->next; action; action = action->next)
239 seq_printf(p, ", %s", action->name);
240 seq_putc(p, '\n');
242 skip:
243 raw_spin_unlock_irqrestore(&desc->lock, flags);
245 return 0;
248 #ifdef CONFIG_HOTPLUG_CPU
249 void fixup_irqs(cpumask_t map)
251 struct irq_desc *desc;
252 unsigned int irq;
253 static int warned;
255 for_each_irq(irq) {
256 cpumask_t mask;
258 desc = irq_to_desc(irq);
259 if (desc && desc->status & IRQ_PER_CPU)
260 continue;
262 cpumask_and(&mask, desc->affinity, &map);
263 if (any_online_cpu(mask) == NR_CPUS) {
264 printk("Breaking affinity for irq %i\n", irq);
265 mask = map;
267 if (desc->chip->set_affinity)
268 desc->chip->set_affinity(irq, &mask);
269 else if (desc->action && !(warned++))
270 printk("Cannot set affinity for irq %i\n", irq);
273 local_irq_enable();
274 mdelay(1);
275 local_irq_disable();
277 #endif
279 #ifdef CONFIG_IRQSTACKS
280 static inline void handle_one_irq(unsigned int irq)
282 struct thread_info *curtp, *irqtp;
283 unsigned long saved_sp_limit;
284 struct irq_desc *desc;
286 /* Switch to the irq stack to handle this */
287 curtp = current_thread_info();
288 irqtp = hardirq_ctx[smp_processor_id()];
290 if (curtp == irqtp) {
291 /* We're already on the irq stack, just handle it */
292 generic_handle_irq(irq);
293 return;
296 desc = irq_to_desc(irq);
297 saved_sp_limit = current->thread.ksp_limit;
299 irqtp->task = curtp->task;
300 irqtp->flags = 0;
302 /* Copy the softirq bits in preempt_count so that the
303 * softirq checks work in the hardirq context. */
304 irqtp->preempt_count = (irqtp->preempt_count & ~SOFTIRQ_MASK) |
305 (curtp->preempt_count & SOFTIRQ_MASK);
307 current->thread.ksp_limit = (unsigned long)irqtp +
308 _ALIGN_UP(sizeof(struct thread_info), 16);
310 call_handle_irq(irq, desc, irqtp, desc->handle_irq);
311 current->thread.ksp_limit = saved_sp_limit;
312 irqtp->task = NULL;
314 /* Set any flag that may have been set on the
315 * alternate stack
317 if (irqtp->flags)
318 set_bits(irqtp->flags, &curtp->flags);
320 #else
321 static inline void handle_one_irq(unsigned int irq)
323 generic_handle_irq(irq);
325 #endif
327 static inline void check_stack_overflow(void)
329 #ifdef CONFIG_DEBUG_STACKOVERFLOW
330 long sp;
332 sp = __get_SP() & (THREAD_SIZE-1);
334 /* check for stack overflow: is there less than 2KB free? */
335 if (unlikely(sp < (sizeof(struct thread_info) + 2048))) {
336 printk("do_IRQ: stack overflow: %ld\n",
337 sp - sizeof(struct thread_info));
338 dump_stack();
340 #endif
343 void do_IRQ(struct pt_regs *regs)
345 struct pt_regs *old_regs = set_irq_regs(regs);
346 unsigned int irq;
348 trace_irq_entry(regs);
350 irq_enter();
352 check_stack_overflow();
354 irq = ppc_md.get_irq();
356 if (irq != NO_IRQ && irq != NO_IRQ_IGNORE)
357 handle_one_irq(irq);
358 else if (irq != NO_IRQ_IGNORE)
359 /* That's not SMP safe ... but who cares ? */
360 ppc_spurious_interrupts++;
362 irq_exit();
363 set_irq_regs(old_regs);
365 #ifdef CONFIG_PPC_ISERIES
366 if (firmware_has_feature(FW_FEATURE_ISERIES) &&
367 get_lppaca()->int_dword.fields.decr_int) {
368 get_lppaca()->int_dword.fields.decr_int = 0;
369 /* Signal a fake decrementer interrupt */
370 timer_interrupt(regs);
372 #endif
374 trace_irq_exit(regs);
377 void __init init_IRQ(void)
379 if (ppc_md.init_IRQ)
380 ppc_md.init_IRQ();
382 exc_lvl_ctx_init();
384 irq_ctx_init();
387 #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
388 struct thread_info *critirq_ctx[NR_CPUS] __read_mostly;
389 struct thread_info *dbgirq_ctx[NR_CPUS] __read_mostly;
390 struct thread_info *mcheckirq_ctx[NR_CPUS] __read_mostly;
392 void exc_lvl_ctx_init(void)
394 struct thread_info *tp;
395 int i;
397 for_each_possible_cpu(i) {
398 memset((void *)critirq_ctx[i], 0, THREAD_SIZE);
399 tp = critirq_ctx[i];
400 tp->cpu = i;
401 tp->preempt_count = 0;
403 #ifdef CONFIG_BOOKE
404 memset((void *)dbgirq_ctx[i], 0, THREAD_SIZE);
405 tp = dbgirq_ctx[i];
406 tp->cpu = i;
407 tp->preempt_count = 0;
409 memset((void *)mcheckirq_ctx[i], 0, THREAD_SIZE);
410 tp = mcheckirq_ctx[i];
411 tp->cpu = i;
412 tp->preempt_count = HARDIRQ_OFFSET;
413 #endif
416 #endif
418 #ifdef CONFIG_IRQSTACKS
419 struct thread_info *softirq_ctx[NR_CPUS] __read_mostly;
420 struct thread_info *hardirq_ctx[NR_CPUS] __read_mostly;
422 void irq_ctx_init(void)
424 struct thread_info *tp;
425 int i;
427 for_each_possible_cpu(i) {
428 memset((void *)softirq_ctx[i], 0, THREAD_SIZE);
429 tp = softirq_ctx[i];
430 tp->cpu = i;
431 tp->preempt_count = 0;
433 memset((void *)hardirq_ctx[i], 0, THREAD_SIZE);
434 tp = hardirq_ctx[i];
435 tp->cpu = i;
436 tp->preempt_count = HARDIRQ_OFFSET;
440 static inline void do_softirq_onstack(void)
442 struct thread_info *curtp, *irqtp;
443 unsigned long saved_sp_limit = current->thread.ksp_limit;
445 curtp = current_thread_info();
446 irqtp = softirq_ctx[smp_processor_id()];
447 irqtp->task = curtp->task;
448 current->thread.ksp_limit = (unsigned long)irqtp +
449 _ALIGN_UP(sizeof(struct thread_info), 16);
450 call_do_softirq(irqtp);
451 current->thread.ksp_limit = saved_sp_limit;
452 irqtp->task = NULL;
455 #else
456 #define do_softirq_onstack() __do_softirq()
457 #endif /* CONFIG_IRQSTACKS */
459 void do_softirq(void)
461 unsigned long flags;
463 if (in_interrupt())
464 return;
466 local_irq_save(flags);
468 if (local_softirq_pending())
469 do_softirq_onstack();
471 local_irq_restore(flags);
476 * IRQ controller and virtual interrupts
479 static LIST_HEAD(irq_hosts);
480 static DEFINE_SPINLOCK(irq_big_lock);
481 static unsigned int revmap_trees_allocated;
482 static DEFINE_MUTEX(revmap_trees_mutex);
483 struct irq_map_entry irq_map[NR_IRQS];
484 static unsigned int irq_virq_count = NR_IRQS;
485 static struct irq_host *irq_default_host;
487 irq_hw_number_t virq_to_hw(unsigned int virq)
489 return irq_map[virq].hwirq;
491 EXPORT_SYMBOL_GPL(virq_to_hw);
493 static int default_irq_host_match(struct irq_host *h, struct device_node *np)
495 return h->of_node != NULL && h->of_node == np;
498 struct irq_host *irq_alloc_host(struct device_node *of_node,
499 unsigned int revmap_type,
500 unsigned int revmap_arg,
501 struct irq_host_ops *ops,
502 irq_hw_number_t inval_irq)
504 struct irq_host *host;
505 unsigned int size = sizeof(struct irq_host);
506 unsigned int i;
507 unsigned int *rmap;
508 unsigned long flags;
510 /* Allocate structure and revmap table if using linear mapping */
511 if (revmap_type == IRQ_HOST_MAP_LINEAR)
512 size += revmap_arg * sizeof(unsigned int);
513 host = zalloc_maybe_bootmem(size, GFP_KERNEL);
514 if (host == NULL)
515 return NULL;
517 /* Fill structure */
518 host->revmap_type = revmap_type;
519 host->inval_irq = inval_irq;
520 host->ops = ops;
521 host->of_node = of_node_get(of_node);
523 if (host->ops->match == NULL)
524 host->ops->match = default_irq_host_match;
526 spin_lock_irqsave(&irq_big_lock, flags);
528 /* If it's a legacy controller, check for duplicates and
529 * mark it as allocated (we use irq 0 host pointer for that
531 if (revmap_type == IRQ_HOST_MAP_LEGACY) {
532 if (irq_map[0].host != NULL) {
533 spin_unlock_irqrestore(&irq_big_lock, flags);
534 /* If we are early boot, we can't free the structure,
535 * too bad...
536 * this will be fixed once slab is made available early
537 * instead of the current cruft
539 if (mem_init_done)
540 kfree(host);
541 return NULL;
543 irq_map[0].host = host;
546 list_add(&host->link, &irq_hosts);
547 spin_unlock_irqrestore(&irq_big_lock, flags);
549 /* Additional setups per revmap type */
550 switch(revmap_type) {
551 case IRQ_HOST_MAP_LEGACY:
552 /* 0 is always the invalid number for legacy */
553 host->inval_irq = 0;
554 /* setup us as the host for all legacy interrupts */
555 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
556 irq_map[i].hwirq = i;
557 smp_wmb();
558 irq_map[i].host = host;
559 smp_wmb();
561 /* Clear norequest flags */
562 irq_to_desc(i)->status &= ~IRQ_NOREQUEST;
564 /* Legacy flags are left to default at this point,
565 * one can then use irq_create_mapping() to
566 * explicitly change them
568 ops->map(host, i, i);
570 break;
571 case IRQ_HOST_MAP_LINEAR:
572 rmap = (unsigned int *)(host + 1);
573 for (i = 0; i < revmap_arg; i++)
574 rmap[i] = NO_IRQ;
575 host->revmap_data.linear.size = revmap_arg;
576 smp_wmb();
577 host->revmap_data.linear.revmap = rmap;
578 break;
579 default:
580 break;
583 pr_debug("irq: Allocated host of type %d @0x%p\n", revmap_type, host);
585 return host;
588 struct irq_host *irq_find_host(struct device_node *node)
590 struct irq_host *h, *found = NULL;
591 unsigned long flags;
593 /* We might want to match the legacy controller last since
594 * it might potentially be set to match all interrupts in
595 * the absence of a device node. This isn't a problem so far
596 * yet though...
598 spin_lock_irqsave(&irq_big_lock, flags);
599 list_for_each_entry(h, &irq_hosts, link)
600 if (h->ops->match(h, node)) {
601 found = h;
602 break;
604 spin_unlock_irqrestore(&irq_big_lock, flags);
605 return found;
607 EXPORT_SYMBOL_GPL(irq_find_host);
609 void irq_set_default_host(struct irq_host *host)
611 pr_debug("irq: Default host set to @0x%p\n", host);
613 irq_default_host = host;
616 void irq_set_virq_count(unsigned int count)
618 pr_debug("irq: Trying to set virq count to %d\n", count);
620 BUG_ON(count < NUM_ISA_INTERRUPTS);
621 if (count < NR_IRQS)
622 irq_virq_count = count;
625 static int irq_setup_virq(struct irq_host *host, unsigned int virq,
626 irq_hw_number_t hwirq)
628 struct irq_desc *desc;
630 desc = irq_to_desc_alloc_node(virq, 0);
631 if (!desc) {
632 pr_debug("irq: -> allocating desc failed\n");
633 goto error;
636 /* Clear IRQ_NOREQUEST flag */
637 desc->status &= ~IRQ_NOREQUEST;
639 /* map it */
640 smp_wmb();
641 irq_map[virq].hwirq = hwirq;
642 smp_mb();
644 if (host->ops->map(host, virq, hwirq)) {
645 pr_debug("irq: -> mapping failed, freeing\n");
646 goto error;
649 return 0;
651 error:
652 irq_free_virt(virq, 1);
653 return -1;
656 unsigned int irq_create_direct_mapping(struct irq_host *host)
658 unsigned int virq;
660 if (host == NULL)
661 host = irq_default_host;
663 BUG_ON(host == NULL);
664 WARN_ON(host->revmap_type != IRQ_HOST_MAP_NOMAP);
666 virq = irq_alloc_virt(host, 1, 0);
667 if (virq == NO_IRQ) {
668 pr_debug("irq: create_direct virq allocation failed\n");
669 return NO_IRQ;
672 pr_debug("irq: create_direct obtained virq %d\n", virq);
674 if (irq_setup_virq(host, virq, virq))
675 return NO_IRQ;
677 return virq;
680 unsigned int irq_create_mapping(struct irq_host *host,
681 irq_hw_number_t hwirq)
683 unsigned int virq, hint;
685 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", host, hwirq);
687 /* Look for default host if nececssary */
688 if (host == NULL)
689 host = irq_default_host;
690 if (host == NULL) {
691 printk(KERN_WARNING "irq_create_mapping called for"
692 " NULL host, hwirq=%lx\n", hwirq);
693 WARN_ON(1);
694 return NO_IRQ;
696 pr_debug("irq: -> using host @%p\n", host);
698 /* Check if mapping already exist, if it does, call
699 * host->ops->map() to update the flags
701 virq = irq_find_mapping(host, hwirq);
702 if (virq != NO_IRQ) {
703 if (host->ops->remap)
704 host->ops->remap(host, virq, hwirq);
705 pr_debug("irq: -> existing mapping on virq %d\n", virq);
706 return virq;
709 /* Get a virtual interrupt number */
710 if (host->revmap_type == IRQ_HOST_MAP_LEGACY) {
711 /* Handle legacy */
712 virq = (unsigned int)hwirq;
713 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
714 return NO_IRQ;
715 return virq;
716 } else {
717 /* Allocate a virtual interrupt number */
718 hint = hwirq % irq_virq_count;
719 virq = irq_alloc_virt(host, 1, hint);
720 if (virq == NO_IRQ) {
721 pr_debug("irq: -> virq allocation failed\n");
722 return NO_IRQ;
726 if (irq_setup_virq(host, virq, hwirq))
727 return NO_IRQ;
729 printk(KERN_DEBUG "irq: irq %lu on host %s mapped to virtual irq %u\n",
730 hwirq, host->of_node ? host->of_node->full_name : "null", virq);
732 return virq;
734 EXPORT_SYMBOL_GPL(irq_create_mapping);
736 unsigned int irq_create_of_mapping(struct device_node *controller,
737 const u32 *intspec, unsigned int intsize)
739 struct irq_host *host;
740 irq_hw_number_t hwirq;
741 unsigned int type = IRQ_TYPE_NONE;
742 unsigned int virq;
744 if (controller == NULL)
745 host = irq_default_host;
746 else
747 host = irq_find_host(controller);
748 if (host == NULL) {
749 printk(KERN_WARNING "irq: no irq host found for %s !\n",
750 controller->full_name);
751 return NO_IRQ;
754 /* If host has no translation, then we assume interrupt line */
755 if (host->ops->xlate == NULL)
756 hwirq = intspec[0];
757 else {
758 if (host->ops->xlate(host, controller, intspec, intsize,
759 &hwirq, &type))
760 return NO_IRQ;
763 /* Create mapping */
764 virq = irq_create_mapping(host, hwirq);
765 if (virq == NO_IRQ)
766 return virq;
768 /* Set type if specified and different than the current one */
769 if (type != IRQ_TYPE_NONE &&
770 type != (irq_to_desc(virq)->status & IRQF_TRIGGER_MASK))
771 set_irq_type(virq, type);
772 return virq;
774 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
776 unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
778 struct of_irq oirq;
780 if (of_irq_map_one(dev, index, &oirq))
781 return NO_IRQ;
783 return irq_create_of_mapping(oirq.controller, oirq.specifier,
784 oirq.size);
786 EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
788 void irq_dispose_mapping(unsigned int virq)
790 struct irq_host *host;
791 irq_hw_number_t hwirq;
793 if (virq == NO_IRQ)
794 return;
796 host = irq_map[virq].host;
797 WARN_ON (host == NULL);
798 if (host == NULL)
799 return;
801 /* Never unmap legacy interrupts */
802 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
803 return;
805 /* remove chip and handler */
806 set_irq_chip_and_handler(virq, NULL, NULL);
808 /* Make sure it's completed */
809 synchronize_irq(virq);
811 /* Tell the PIC about it */
812 if (host->ops->unmap)
813 host->ops->unmap(host, virq);
814 smp_mb();
816 /* Clear reverse map */
817 hwirq = irq_map[virq].hwirq;
818 switch(host->revmap_type) {
819 case IRQ_HOST_MAP_LINEAR:
820 if (hwirq < host->revmap_data.linear.size)
821 host->revmap_data.linear.revmap[hwirq] = NO_IRQ;
822 break;
823 case IRQ_HOST_MAP_TREE:
825 * Check if radix tree allocated yet, if not then nothing to
826 * remove.
828 smp_rmb();
829 if (revmap_trees_allocated < 1)
830 break;
831 mutex_lock(&revmap_trees_mutex);
832 radix_tree_delete(&host->revmap_data.tree, hwirq);
833 mutex_unlock(&revmap_trees_mutex);
834 break;
837 /* Destroy map */
838 smp_mb();
839 irq_map[virq].hwirq = host->inval_irq;
841 /* Set some flags */
842 irq_to_desc(virq)->status |= IRQ_NOREQUEST;
844 /* Free it */
845 irq_free_virt(virq, 1);
847 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
849 unsigned int irq_find_mapping(struct irq_host *host,
850 irq_hw_number_t hwirq)
852 unsigned int i;
853 unsigned int hint = hwirq % irq_virq_count;
855 /* Look for default host if nececssary */
856 if (host == NULL)
857 host = irq_default_host;
858 if (host == NULL)
859 return NO_IRQ;
861 /* legacy -> bail early */
862 if (host->revmap_type == IRQ_HOST_MAP_LEGACY)
863 return hwirq;
865 /* Slow path does a linear search of the map */
866 if (hint < NUM_ISA_INTERRUPTS)
867 hint = NUM_ISA_INTERRUPTS;
868 i = hint;
869 do {
870 if (irq_map[i].host == host &&
871 irq_map[i].hwirq == hwirq)
872 return i;
873 i++;
874 if (i >= irq_virq_count)
875 i = NUM_ISA_INTERRUPTS;
876 } while(i != hint);
877 return NO_IRQ;
879 EXPORT_SYMBOL_GPL(irq_find_mapping);
882 unsigned int irq_radix_revmap_lookup(struct irq_host *host,
883 irq_hw_number_t hwirq)
885 struct irq_map_entry *ptr;
886 unsigned int virq;
888 WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
891 * Check if the radix tree exists and has bee initialized.
892 * If not, we fallback to slow mode
894 if (revmap_trees_allocated < 2)
895 return irq_find_mapping(host, hwirq);
897 /* Now try to resolve */
899 * No rcu_read_lock(ing) needed, the ptr returned can't go under us
900 * as it's referencing an entry in the static irq_map table.
902 ptr = radix_tree_lookup(&host->revmap_data.tree, hwirq);
905 * If found in radix tree, then fine.
906 * Else fallback to linear lookup - this should not happen in practice
907 * as it means that we failed to insert the node in the radix tree.
909 if (ptr)
910 virq = ptr - irq_map;
911 else
912 virq = irq_find_mapping(host, hwirq);
914 return virq;
917 void irq_radix_revmap_insert(struct irq_host *host, unsigned int virq,
918 irq_hw_number_t hwirq)
921 WARN_ON(host->revmap_type != IRQ_HOST_MAP_TREE);
924 * Check if the radix tree exists yet.
925 * If not, then the irq will be inserted into the tree when it gets
926 * initialized.
928 smp_rmb();
929 if (revmap_trees_allocated < 1)
930 return;
932 if (virq != NO_IRQ) {
933 mutex_lock(&revmap_trees_mutex);
934 radix_tree_insert(&host->revmap_data.tree, hwirq,
935 &irq_map[virq]);
936 mutex_unlock(&revmap_trees_mutex);
940 unsigned int irq_linear_revmap(struct irq_host *host,
941 irq_hw_number_t hwirq)
943 unsigned int *revmap;
945 WARN_ON(host->revmap_type != IRQ_HOST_MAP_LINEAR);
947 /* Check revmap bounds */
948 if (unlikely(hwirq >= host->revmap_data.linear.size))
949 return irq_find_mapping(host, hwirq);
951 /* Check if revmap was allocated */
952 revmap = host->revmap_data.linear.revmap;
953 if (unlikely(revmap == NULL))
954 return irq_find_mapping(host, hwirq);
956 /* Fill up revmap with slow path if no mapping found */
957 if (unlikely(revmap[hwirq] == NO_IRQ))
958 revmap[hwirq] = irq_find_mapping(host, hwirq);
960 return revmap[hwirq];
963 unsigned int irq_alloc_virt(struct irq_host *host,
964 unsigned int count,
965 unsigned int hint)
967 unsigned long flags;
968 unsigned int i, j, found = NO_IRQ;
970 if (count == 0 || count > (irq_virq_count - NUM_ISA_INTERRUPTS))
971 return NO_IRQ;
973 spin_lock_irqsave(&irq_big_lock, flags);
975 /* Use hint for 1 interrupt if any */
976 if (count == 1 && hint >= NUM_ISA_INTERRUPTS &&
977 hint < irq_virq_count && irq_map[hint].host == NULL) {
978 found = hint;
979 goto hint_found;
982 /* Look for count consecutive numbers in the allocatable
983 * (non-legacy) space
985 for (i = NUM_ISA_INTERRUPTS, j = 0; i < irq_virq_count; i++) {
986 if (irq_map[i].host != NULL)
987 j = 0;
988 else
989 j++;
991 if (j == count) {
992 found = i - count + 1;
993 break;
996 if (found == NO_IRQ) {
997 spin_unlock_irqrestore(&irq_big_lock, flags);
998 return NO_IRQ;
1000 hint_found:
1001 for (i = found; i < (found + count); i++) {
1002 irq_map[i].hwirq = host->inval_irq;
1003 smp_wmb();
1004 irq_map[i].host = host;
1006 spin_unlock_irqrestore(&irq_big_lock, flags);
1007 return found;
1010 void irq_free_virt(unsigned int virq, unsigned int count)
1012 unsigned long flags;
1013 unsigned int i;
1015 WARN_ON (virq < NUM_ISA_INTERRUPTS);
1016 WARN_ON (count == 0 || (virq + count) > irq_virq_count);
1018 spin_lock_irqsave(&irq_big_lock, flags);
1019 for (i = virq; i < (virq + count); i++) {
1020 struct irq_host *host;
1022 if (i < NUM_ISA_INTERRUPTS ||
1023 (virq + count) > irq_virq_count)
1024 continue;
1026 host = irq_map[i].host;
1027 irq_map[i].hwirq = host->inval_irq;
1028 smp_wmb();
1029 irq_map[i].host = NULL;
1031 spin_unlock_irqrestore(&irq_big_lock, flags);
1034 int arch_early_irq_init(void)
1036 struct irq_desc *desc;
1037 int i;
1039 for (i = 0; i < NR_IRQS; i++) {
1040 desc = irq_to_desc(i);
1041 if (desc)
1042 desc->status |= IRQ_NOREQUEST;
1045 return 0;
1048 int arch_init_chip_data(struct irq_desc *desc, int node)
1050 desc->status |= IRQ_NOREQUEST;
1051 return 0;
1054 /* We need to create the radix trees late */
1055 static int irq_late_init(void)
1057 struct irq_host *h;
1058 unsigned int i;
1061 * No mutual exclusion with respect to accessors of the tree is needed
1062 * here as the synchronization is done via the state variable
1063 * revmap_trees_allocated.
1065 list_for_each_entry(h, &irq_hosts, link) {
1066 if (h->revmap_type == IRQ_HOST_MAP_TREE)
1067 INIT_RADIX_TREE(&h->revmap_data.tree, GFP_KERNEL);
1071 * Make sure the radix trees inits are visible before setting
1072 * the flag
1074 smp_wmb();
1075 revmap_trees_allocated = 1;
1078 * Insert the reverse mapping for those interrupts already present
1079 * in irq_map[].
1081 mutex_lock(&revmap_trees_mutex);
1082 for (i = 0; i < irq_virq_count; i++) {
1083 if (irq_map[i].host &&
1084 (irq_map[i].host->revmap_type == IRQ_HOST_MAP_TREE))
1085 radix_tree_insert(&irq_map[i].host->revmap_data.tree,
1086 irq_map[i].hwirq, &irq_map[i]);
1088 mutex_unlock(&revmap_trees_mutex);
1091 * Make sure the radix trees insertions are visible before setting
1092 * the flag
1094 smp_wmb();
1095 revmap_trees_allocated = 2;
1097 return 0;
1099 arch_initcall(irq_late_init);
1101 #ifdef CONFIG_VIRQ_DEBUG
1102 static int virq_debug_show(struct seq_file *m, void *private)
1104 unsigned long flags;
1105 struct irq_desc *desc;
1106 const char *p;
1107 char none[] = "none";
1108 int i;
1110 seq_printf(m, "%-5s %-7s %-15s %s\n", "virq", "hwirq",
1111 "chip name", "host name");
1113 for (i = 1; i < nr_irqs; i++) {
1114 desc = irq_to_desc(i);
1115 if (!desc)
1116 continue;
1118 raw_spin_lock_irqsave(&desc->lock, flags);
1120 if (desc->action && desc->action->handler) {
1121 seq_printf(m, "%5d ", i);
1122 seq_printf(m, "0x%05lx ", virq_to_hw(i));
1124 if (desc->chip && desc->chip->name)
1125 p = desc->chip->name;
1126 else
1127 p = none;
1128 seq_printf(m, "%-15s ", p);
1130 if (irq_map[i].host && irq_map[i].host->of_node)
1131 p = irq_map[i].host->of_node->full_name;
1132 else
1133 p = none;
1134 seq_printf(m, "%s\n", p);
1137 raw_spin_unlock_irqrestore(&desc->lock, flags);
1140 return 0;
1143 static int virq_debug_open(struct inode *inode, struct file *file)
1145 return single_open(file, virq_debug_show, inode->i_private);
1148 static const struct file_operations virq_debug_fops = {
1149 .open = virq_debug_open,
1150 .read = seq_read,
1151 .llseek = seq_lseek,
1152 .release = single_release,
1155 static int __init irq_debugfs_init(void)
1157 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
1158 NULL, &virq_debug_fops) == NULL)
1159 return -ENOMEM;
1161 return 0;
1163 __initcall(irq_debugfs_init);
1164 #endif /* CONFIG_VIRQ_DEBUG */
1166 #ifdef CONFIG_PPC64
1167 static int __init setup_noirqdistrib(char *str)
1169 distribute_irqs = 0;
1170 return 1;
1173 __setup("noirqdistrib", setup_noirqdistrib);
1174 #endif /* CONFIG_PPC64 */