1 #include <linux/debugfs.h>
2 #include <linux/hardirq.h>
3 #include <linux/interrupt.h>
5 #include <linux/irqdesc.h>
6 #include <linux/irqdomain.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
10 #include <linux/of_address.h>
11 #include <linux/seq_file.h>
12 #include <linux/slab.h>
13 #include <linux/smp.h>
16 static LIST_HEAD(irq_domain_list);
17 static DEFINE_MUTEX(irq_domain_mutex);
20 static DEFINE_MUTEX(revmap_trees_mutex);
21 static unsigned int irq_virq_count = NR_IRQS;
22 static struct irq_domain *irq_default_domain;
24 static int default_irq_domain_match(struct irq_domain *d, struct device_node *np)
26 return d->of_node != NULL && d->of_node == np;
30 * irq_alloc_host() - Allocate a new irq_domain data structure
31 * @of_node: optional device-tree node of the interrupt controller
32 * @revmap_type: type of reverse mapping to use
33 * @revmap_arg: for IRQ_DOMAIN_MAP_LINEAR linear only: size of the map
34 * @ops: map/unmap domain callbacks
35 * @inval_irq: provide a hw number in that domain space that is always invalid
37 * Allocates and initialize and irq_domain structure. Note that in the case of
38 * IRQ_DOMAIN_MAP_LEGACY, the map() callback will be called before this returns
39 * for all legacy interrupts except 0 (which is always the invalid irq for
40 * a legacy controller). For a IRQ_DOMAIN_MAP_LINEAR, the map is allocated by
41 * this call as well. For a IRQ_DOMAIN_MAP_TREE, the radix tree will be
42 * allocated later during boot automatically (the reverse mapping will use the
43 * slow path until that happens).
45 struct irq_domain *irq_alloc_host(struct device_node *of_node,
46 unsigned int revmap_type,
47 unsigned int revmap_arg,
48 struct irq_domain_ops *ops,
49 irq_hw_number_t inval_irq)
51 struct irq_domain *domain, *h;
52 unsigned int size = sizeof(struct irq_domain);
56 /* Allocate structure and revmap table if using linear mapping */
57 if (revmap_type == IRQ_DOMAIN_MAP_LINEAR)
58 size += revmap_arg * sizeof(unsigned int);
59 domain = kzalloc(size, GFP_KERNEL);
64 domain->revmap_type = revmap_type;
65 domain->inval_irq = inval_irq;
67 domain->of_node = of_node_get(of_node);
69 if (domain->ops->match == NULL)
70 domain->ops->match = default_irq_domain_match;
72 mutex_lock(&irq_domain_mutex);
73 /* Make sure only one legacy controller can be created */
74 if (revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
75 list_for_each_entry(h, &irq_domain_list, link) {
76 if (WARN_ON(h->revmap_type == IRQ_DOMAIN_MAP_LEGACY)) {
77 mutex_unlock(&irq_domain_mutex);
78 of_node_put(domain->of_node);
84 list_add(&domain->link, &irq_domain_list);
85 mutex_unlock(&irq_domain_mutex);
87 /* Additional setups per revmap type */
89 case IRQ_DOMAIN_MAP_LEGACY:
90 /* 0 is always the invalid number for legacy */
91 domain->inval_irq = 0;
92 /* setup us as the domain for all legacy interrupts */
93 for (i = 1; i < NUM_ISA_INTERRUPTS; i++) {
94 struct irq_data *irq_data = irq_get_irq_data(i);
96 irq_data->domain = domain;
98 /* Legacy flags are left to default at this point,
99 * one can then use irq_create_mapping() to
100 * explicitly change them
102 ops->map(domain, i, i);
104 /* Clear norequest flags */
105 irq_clear_status_flags(i, IRQ_NOREQUEST);
108 case IRQ_DOMAIN_MAP_LINEAR:
109 rmap = (unsigned int *)(domain + 1);
110 for (i = 0; i < revmap_arg; i++)
112 domain->revmap_data.linear.size = revmap_arg;
113 domain->revmap_data.linear.revmap = rmap;
115 case IRQ_DOMAIN_MAP_TREE:
116 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
122 pr_debug("irq: Allocated domain of type %d @0x%p\n", revmap_type, domain);
128 * irq_find_host() - Locates a domain for a given device node
129 * @node: device-tree node of the interrupt controller
131 struct irq_domain *irq_find_host(struct device_node *node)
133 struct irq_domain *h, *found = NULL;
135 /* We might want to match the legacy controller last since
136 * it might potentially be set to match all interrupts in
137 * the absence of a device node. This isn't a problem so far
140 mutex_lock(&irq_domain_mutex);
141 list_for_each_entry(h, &irq_domain_list, link)
142 if (h->ops->match(h, node)) {
146 mutex_unlock(&irq_domain_mutex);
149 EXPORT_SYMBOL_GPL(irq_find_host);
152 * irq_set_default_host() - Set a "default" irq domain
153 * @domain: default domain pointer
155 * For convenience, it's possible to set a "default" domain that will be used
156 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
157 * platforms that want to manipulate a few hard coded interrupt numbers that
158 * aren't properly represented in the device-tree.
160 void irq_set_default_host(struct irq_domain *domain)
162 pr_debug("irq: Default domain set to @0x%p\n", domain);
164 irq_default_domain = domain;
168 * irq_set_virq_count() - Set the maximum number of linux irqs
169 * @count: number of linux irqs, capped with NR_IRQS
171 * This is mainly for use by platforms like iSeries who want to program
172 * the virtual irq number in the controller to avoid the reverse mapping
174 void irq_set_virq_count(unsigned int count)
176 pr_debug("irq: Trying to set virq count to %d\n", count);
178 BUG_ON(count < NUM_ISA_INTERRUPTS);
180 irq_virq_count = count;
183 static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
184 irq_hw_number_t hwirq)
186 struct irq_data *irq_data = irq_get_irq_data(virq);
188 irq_data->hwirq = hwirq;
189 irq_data->domain = domain;
190 if (domain->ops->map(domain, virq, hwirq)) {
191 pr_debug("irq: -> mapping failed, freeing\n");
192 irq_data->domain = NULL;
197 irq_clear_status_flags(virq, IRQ_NOREQUEST);
203 * irq_create_direct_mapping() - Allocate an irq for direct mapping
204 * @domain: domain to allocate the irq for or NULL for default domain
206 * This routine is used for irq controllers which can choose the hardware
207 * interrupt numbers they generate. In such a case it's simplest to use
208 * the linux irq as the hardware interrupt number.
210 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
215 domain = irq_default_domain;
217 BUG_ON(domain == NULL);
218 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
220 virq = irq_alloc_desc_from(1, 0);
222 pr_debug("irq: create_direct virq allocation failed\n");
225 if (virq >= irq_virq_count) {
226 pr_err("ERROR: no free irqs available below %i maximum\n",
232 pr_debug("irq: create_direct obtained virq %d\n", virq);
234 if (irq_setup_virq(domain, virq, virq)) {
243 * irq_create_mapping() - Map a hardware interrupt into linux irq space
244 * @domain: domain owning this hardware interrupt or NULL for default domain
245 * @hwirq: hardware irq number in that domain space
247 * Only one mapping per hardware interrupt is permitted. Returns a linux
249 * If the sense/trigger is to be specified, set_irq_type() should be called
250 * on the number returned from that call.
252 unsigned int irq_create_mapping(struct irq_domain *domain,
253 irq_hw_number_t hwirq)
255 unsigned int virq, hint;
257 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
259 /* Look for default domain if nececssary */
261 domain = irq_default_domain;
262 if (domain == NULL) {
263 printk(KERN_WARNING "irq_create_mapping called for"
264 " NULL domain, hwirq=%lx\n", hwirq);
268 pr_debug("irq: -> using domain @%p\n", domain);
270 /* Check if mapping already exists */
271 virq = irq_find_mapping(domain, hwirq);
273 pr_debug("irq: -> existing mapping on virq %d\n", virq);
277 /* Get a virtual interrupt number */
278 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY) {
280 virq = (unsigned int)hwirq;
281 if (virq == 0 || virq >= NUM_ISA_INTERRUPTS)
285 /* Allocate a virtual interrupt number */
286 hint = hwirq % irq_virq_count;
289 virq = irq_alloc_desc_from(hint, 0);
291 virq = irq_alloc_desc_from(1, 0);
293 pr_debug("irq: -> virq allocation failed\n");
298 if (irq_setup_virq(domain, virq, hwirq)) {
299 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
304 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
305 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
309 EXPORT_SYMBOL_GPL(irq_create_mapping);
311 unsigned int irq_create_of_mapping(struct device_node *controller,
312 const u32 *intspec, unsigned int intsize)
314 struct irq_domain *domain;
315 irq_hw_number_t hwirq;
316 unsigned int type = IRQ_TYPE_NONE;
319 domain = controller ? irq_find_host(controller) : irq_default_domain;
321 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
322 controller->full_name);
326 /* If domain has no translation, then we assume interrupt line */
327 if (domain->ops->xlate == NULL)
330 if (domain->ops->xlate(domain, controller, intspec, intsize,
336 virq = irq_create_mapping(domain, hwirq);
340 /* Set type if specified and different than the current one */
341 if (type != IRQ_TYPE_NONE &&
342 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
343 irq_set_irq_type(virq, type);
346 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
349 * irq_dispose_mapping() - Unmap an interrupt
350 * @virq: linux irq number of the interrupt to unmap
352 void irq_dispose_mapping(unsigned int virq)
354 struct irq_data *irq_data = irq_get_irq_data(virq);
355 struct irq_domain *domain;
356 irq_hw_number_t hwirq;
358 if (!virq || !irq_data)
361 domain = irq_data->domain;
362 if (WARN_ON(domain == NULL))
365 /* Never unmap legacy interrupts */
366 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
369 irq_set_status_flags(virq, IRQ_NOREQUEST);
371 /* remove chip and handler */
372 irq_set_chip_and_handler(virq, NULL, NULL);
374 /* Make sure it's completed */
375 synchronize_irq(virq);
377 /* Tell the PIC about it */
378 if (domain->ops->unmap)
379 domain->ops->unmap(domain, virq);
382 /* Clear reverse map */
383 hwirq = irq_data->hwirq;
384 switch(domain->revmap_type) {
385 case IRQ_DOMAIN_MAP_LINEAR:
386 if (hwirq < domain->revmap_data.linear.size)
387 domain->revmap_data.linear.revmap[hwirq] = 0;
389 case IRQ_DOMAIN_MAP_TREE:
390 mutex_lock(&revmap_trees_mutex);
391 radix_tree_delete(&domain->revmap_data.tree, hwirq);
392 mutex_unlock(&revmap_trees_mutex);
397 irq_data->hwirq = domain->inval_irq;
401 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
404 * irq_find_mapping() - Find a linux irq from an hw irq number.
405 * @domain: domain owning this hardware interrupt
406 * @hwirq: hardware irq number in that domain space
408 * This is a slow path, for use by generic code. It's expected that an
409 * irq controller implementation directly calls the appropriate low level
412 unsigned int irq_find_mapping(struct irq_domain *domain,
413 irq_hw_number_t hwirq)
416 unsigned int hint = hwirq % irq_virq_count;
418 /* Look for default domain if nececssary */
420 domain = irq_default_domain;
424 /* legacy -> bail early */
425 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
428 /* Slow path does a linear search of the map */
433 struct irq_data *data = irq_get_irq_data(i);
434 if (data && (data->domain == domain) && (data->hwirq == hwirq))
437 if (i >= irq_virq_count)
442 EXPORT_SYMBOL_GPL(irq_find_mapping);
445 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
446 * @domain: domain owning this hardware interrupt
447 * @hwirq: hardware irq number in that domain space
449 * This is a fast path, for use by irq controller code that uses radix tree
452 unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
453 irq_hw_number_t hwirq)
455 struct irq_data *irq_data;
457 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
458 return irq_find_mapping(domain, hwirq);
461 * Freeing an irq can delete nodes along the path to
462 * do the lookup via call_rcu.
465 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
469 * If found in radix tree, then fine.
470 * Else fallback to linear lookup - this should not happen in practice
471 * as it means that we failed to insert the node in the radix tree.
473 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
477 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
478 * @domain: domain owning this hardware interrupt
479 * @virq: linux irq number
480 * @hwirq: hardware irq number in that domain space
482 * This is for use by irq controllers that use a radix tree reverse
483 * mapping for fast lookup.
485 void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
486 irq_hw_number_t hwirq)
488 struct irq_data *irq_data = irq_get_irq_data(virq);
490 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
494 mutex_lock(&revmap_trees_mutex);
495 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
496 mutex_unlock(&revmap_trees_mutex);
501 * irq_linear_revmap() - Find a linux irq from a hw irq number.
502 * @domain: domain owning this hardware interrupt
503 * @hwirq: hardware irq number in that domain space
505 * This is a fast path, for use by irq controller code that uses linear
506 * revmaps. It does fallback to the slow path if the revmap doesn't exist
507 * yet and will create the revmap entry with appropriate locking
509 unsigned int irq_linear_revmap(struct irq_domain *domain,
510 irq_hw_number_t hwirq)
512 unsigned int *revmap;
514 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
515 return irq_find_mapping(domain, hwirq);
517 /* Check revmap bounds */
518 if (unlikely(hwirq >= domain->revmap_data.linear.size))
519 return irq_find_mapping(domain, hwirq);
521 /* Check if revmap was allocated */
522 revmap = domain->revmap_data.linear.revmap;
523 if (unlikely(revmap == NULL))
524 return irq_find_mapping(domain, hwirq);
526 /* Fill up revmap with slow path if no mapping found */
527 if (unlikely(!revmap[hwirq]))
528 revmap[hwirq] = irq_find_mapping(domain, hwirq);
530 return revmap[hwirq];
533 #ifdef CONFIG_VIRQ_DEBUG
534 static int virq_debug_show(struct seq_file *m, void *private)
537 struct irq_desc *desc;
539 static const char none[] = "none";
543 seq_printf(m, "%-5s %-7s %-15s %-18s %s\n", "virq", "hwirq",
544 "chip name", "chip data", "domain name");
546 for (i = 1; i < nr_irqs; i++) {
547 desc = irq_to_desc(i);
551 raw_spin_lock_irqsave(&desc->lock, flags);
553 if (desc->action && desc->action->handler) {
554 struct irq_chip *chip;
556 seq_printf(m, "%5d ", i);
557 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
559 chip = irq_desc_get_chip(desc);
560 if (chip && chip->name)
564 seq_printf(m, "%-15s ", p);
566 data = irq_desc_get_chip_data(desc);
567 seq_printf(m, "0x%16p ", data);
569 if (desc->irq_data.domain->of_node)
570 p = desc->irq_data.domain->of_node->full_name;
573 seq_printf(m, "%s\n", p);
576 raw_spin_unlock_irqrestore(&desc->lock, flags);
582 static int virq_debug_open(struct inode *inode, struct file *file)
584 return single_open(file, virq_debug_show, inode->i_private);
587 static const struct file_operations virq_debug_fops = {
588 .open = virq_debug_open,
591 .release = single_release,
594 static int __init irq_debugfs_init(void)
596 if (debugfs_create_file("virq_mapping", S_IRUGO, powerpc_debugfs_root,
597 NULL, &virq_debug_fops) == NULL)
602 __initcall(irq_debugfs_init);
603 #endif /* CONFIG_VIRQ_DEBUG */
605 #else /* CONFIG_PPC */
608 * irq_domain_add() - Register an irq_domain
609 * @domain: ptr to initialized irq_domain structure
611 * Registers an irq_domain structure. The irq_domain must at a minimum be
612 * initialized with an ops structure pointer, and either a ->to_irq hook or
613 * a valid irq_base value. Everything else is optional.
615 void irq_domain_add(struct irq_domain *domain)
621 * This assumes that the irq_domain owner has already allocated
622 * the irq_descs. This block will be removed when support for dynamic
623 * allocation of irq_descs is added to irq_domain.
625 irq_domain_for_each_irq(domain, hwirq, irq) {
626 d = irq_get_irq_data(irq);
628 WARN(1, "error: assigning domain to non existant irq_desc");
632 /* things are broken; just report, don't clean up */
633 WARN(1, "error: irq_desc already assigned to a domain");
640 mutex_lock(&irq_domain_mutex);
641 list_add(&domain->link, &irq_domain_list);
642 mutex_unlock(&irq_domain_mutex);
646 * irq_domain_del() - Unregister an irq_domain
647 * @domain: ptr to registered irq_domain.
649 void irq_domain_del(struct irq_domain *domain)
654 mutex_lock(&irq_domain_mutex);
655 list_del(&domain->link);
656 mutex_unlock(&irq_domain_mutex);
658 /* Clear the irq_domain assignments */
659 irq_domain_for_each_irq(domain, hwirq, irq) {
660 d = irq_get_irq_data(irq);
665 #if defined(CONFIG_OF_IRQ)
667 * irq_create_of_mapping() - Map a linux irq number from a DT interrupt spec
669 * Used by the device tree interrupt mapping code to translate a device tree
670 * interrupt specifier to a valid linux irq number. Returns either a valid
671 * linux IRQ number or 0.
673 * When the caller no longer need the irq number returned by this function it
674 * should arrange to call irq_dispose_mapping().
676 unsigned int irq_create_of_mapping(struct device_node *controller,
677 const u32 *intspec, unsigned int intsize)
679 struct irq_domain *domain;
681 unsigned int irq, type;
684 /* Find a domain which can translate the irq spec */
685 mutex_lock(&irq_domain_mutex);
686 list_for_each_entry(domain, &irq_domain_list, link) {
687 if (!domain->ops->xlate)
689 rc = domain->ops->xlate(domain, controller,
690 intspec, intsize, &hwirq, &type);
694 mutex_unlock(&irq_domain_mutex);
699 irq = irq_domain_to_irq(domain, hwirq);
700 if (type != IRQ_TYPE_NONE)
701 irq_set_irq_type(irq, type);
702 pr_debug("%s: mapped hwirq=%i to irq=%i, flags=%x\n",
703 controller->full_name, (int)hwirq, irq, type);
706 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
709 * irq_dispose_mapping() - Discard a mapping created by irq_create_of_mapping()
710 * @irq: linux irq number to be discarded
712 * Calling this function indicates the caller no longer needs a reference to
713 * the linux irq number returned by a prior call to irq_create_of_mapping().
715 void irq_dispose_mapping(unsigned int irq)
718 * nothing yet; will be filled when support for dynamic allocation of
719 * irq_descs is added to irq_domain
722 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
724 int irq_domain_simple_xlate(struct irq_domain *d,
725 struct device_node *controller,
726 const u32 *intspec, unsigned int intsize,
727 unsigned long *out_hwirq, unsigned int *out_type)
729 if (d->of_node != controller)
733 if (d->nr_irq && ((intspec[0] < d->hwirq_base) ||
734 (intspec[0] >= d->hwirq_base + d->nr_irq)))
737 *out_hwirq = intspec[0];
738 *out_type = IRQ_TYPE_NONE;
740 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
745 * irq_domain_create_simple() - Set up a 'simple' translation range
747 void irq_domain_add_simple(struct device_node *controller, int irq_base)
749 struct irq_domain *domain;
751 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
757 domain->irq_base = irq_base;
758 domain->of_node = of_node_get(controller);
759 domain->ops = &irq_domain_simple_ops;
760 irq_domain_add(domain);
762 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
764 void irq_domain_generate_simple(const struct of_device_id *match,
765 u64 phys_base, unsigned int irq_start)
767 struct device_node *node;
768 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
769 (unsigned long long) phys_base, (int) irq_start);
770 node = of_find_matching_node_by_address(NULL, match, phys_base);
772 irq_domain_add_simple(node, irq_start);
774 EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
775 #endif /* CONFIG_OF_IRQ */
777 struct irq_domain_ops irq_domain_simple_ops = {
779 .xlate = irq_domain_simple_xlate,
780 #endif /* CONFIG_OF_IRQ */
782 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
784 #endif /* !CONFIG_PPC */