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 #define IRQ_DOMAIN_MAP_LEGACY 0 /* driver allocated fixed range of irqs.
17 * ie. legacy 8259, gets irqs 1..15 */
18 #define IRQ_DOMAIN_MAP_NOMAP 1 /* no fast reverse mapping */
19 #define IRQ_DOMAIN_MAP_LINEAR 2 /* linear map of interrupts */
20 #define IRQ_DOMAIN_MAP_TREE 3 /* radix tree */
22 static LIST_HEAD(irq_domain_list);
23 static DEFINE_MUTEX(irq_domain_mutex);
25 static DEFINE_MUTEX(revmap_trees_mutex);
26 static struct irq_domain *irq_default_domain;
29 * irq_domain_alloc() - Allocate a new irq_domain data structure
30 * @of_node: optional device-tree node of the interrupt controller
31 * @revmap_type: type of reverse mapping to use
32 * @ops: map/unmap domain callbacks
33 * @host_data: Controller private data pointer
35 * Allocates and initialize and irq_domain structure. Caller is expected to
36 * register allocated irq_domain with irq_domain_register(). Returns pointer
37 * to IRQ domain, or NULL on failure.
39 static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
40 unsigned int revmap_type,
41 const struct irq_domain_ops *ops,
44 struct irq_domain *domain;
46 domain = kzalloc(sizeof(*domain), GFP_KERNEL);
51 domain->revmap_type = revmap_type;
53 domain->host_data = host_data;
54 domain->of_node = of_node_get(of_node);
59 static void irq_domain_free(struct irq_domain *domain)
61 of_node_put(domain->of_node);
65 static void irq_domain_add(struct irq_domain *domain)
67 mutex_lock(&irq_domain_mutex);
68 list_add(&domain->link, &irq_domain_list);
69 mutex_unlock(&irq_domain_mutex);
70 pr_debug("irq: Allocated domain of type %d @0x%p\n",
71 domain->revmap_type, domain);
75 * irq_domain_remove() - Remove an irq domain.
76 * @domain: domain to remove
78 * This routine is used to remove an irq domain. The caller must ensure
79 * that all mappings within the domain have been disposed of prior to
80 * use, depending on the revmap type.
82 void irq_domain_remove(struct irq_domain *domain)
84 mutex_lock(&irq_domain_mutex);
86 switch (domain->revmap_type) {
87 case IRQ_DOMAIN_MAP_LEGACY:
89 * Legacy domains don't manage their own irq_desc
90 * allocations, we expect the caller to handle irq_desc
91 * freeing on their own.
94 case IRQ_DOMAIN_MAP_TREE:
96 * radix_tree_delete() takes care of destroying the root
97 * node when all entries are removed. Shout if there are
100 WARN_ON(domain->revmap_data.tree.height);
102 case IRQ_DOMAIN_MAP_LINEAR:
103 kfree(domain->revmap_data.linear.revmap);
104 domain->revmap_data.linear.size = 0;
106 case IRQ_DOMAIN_MAP_NOMAP:
110 list_del(&domain->link);
113 * If the going away domain is the default one, reset it.
115 if (unlikely(irq_default_domain == domain))
116 irq_set_default_host(NULL);
118 mutex_unlock(&irq_domain_mutex);
120 pr_debug("irq: Removed domain of type %d @0x%p\n",
121 domain->revmap_type, domain);
123 irq_domain_free(domain);
126 static unsigned int irq_domain_legacy_revmap(struct irq_domain *domain,
127 irq_hw_number_t hwirq)
129 irq_hw_number_t first_hwirq = domain->revmap_data.legacy.first_hwirq;
130 int size = domain->revmap_data.legacy.size;
132 if (WARN_ON(hwirq < first_hwirq || hwirq >= first_hwirq + size))
134 return hwirq - first_hwirq + domain->revmap_data.legacy.first_irq;
138 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
139 * @of_node: pointer to interrupt controller's device tree node.
140 * @size: total number of irqs in legacy mapping
141 * @first_irq: first number of irq block assigned to the domain
142 * @first_hwirq: first hwirq number to use for the translation. Should normally
143 * be '0', but a positive integer can be used if the effective
144 * hwirqs numbering does not begin at zero.
145 * @ops: map/unmap domain callbacks
146 * @host_data: Controller private data pointer
148 * Note: the map() callback will be called before this function returns
149 * for all legacy interrupts except 0 (which is always the invalid irq for
150 * a legacy controller).
152 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
154 unsigned int first_irq,
155 irq_hw_number_t first_hwirq,
156 const struct irq_domain_ops *ops,
159 struct irq_domain *domain;
162 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LEGACY, ops, host_data);
166 domain->revmap_data.legacy.first_irq = first_irq;
167 domain->revmap_data.legacy.first_hwirq = first_hwirq;
168 domain->revmap_data.legacy.size = size;
170 mutex_lock(&irq_domain_mutex);
171 /* Verify that all the irqs are available */
172 for (i = 0; i < size; i++) {
173 int irq = first_irq + i;
174 struct irq_data *irq_data = irq_get_irq_data(irq);
176 if (WARN_ON(!irq_data || irq_data->domain)) {
177 mutex_unlock(&irq_domain_mutex);
178 irq_domain_free(domain);
183 /* Claim all of the irqs before registering a legacy domain */
184 for (i = 0; i < size; i++) {
185 struct irq_data *irq_data = irq_get_irq_data(first_irq + i);
186 irq_data->hwirq = first_hwirq + i;
187 irq_data->domain = domain;
189 mutex_unlock(&irq_domain_mutex);
191 for (i = 0; i < size; i++) {
192 int irq = first_irq + i;
193 int hwirq = first_hwirq + i;
195 /* IRQ0 gets ignored */
199 /* Legacy flags are left to default at this point,
200 * one can then use irq_create_mapping() to
201 * explicitly change them
203 ops->map(domain, irq, hwirq);
205 /* Clear norequest flags */
206 irq_clear_status_flags(irq, IRQ_NOREQUEST);
209 irq_domain_add(domain);
214 * irq_domain_add_linear() - Allocate and register a legacy revmap irq_domain.
215 * @of_node: pointer to interrupt controller's device tree node.
216 * @ops: map/unmap domain callbacks
217 * @host_data: Controller private data pointer
219 struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
221 const struct irq_domain_ops *ops,
224 struct irq_domain *domain;
225 unsigned int *revmap;
227 revmap = kzalloc(sizeof(*revmap) * size, GFP_KERNEL);
228 if (WARN_ON(!revmap))
231 domain = irq_domain_alloc(of_node, IRQ_DOMAIN_MAP_LINEAR, ops, host_data);
236 domain->revmap_data.linear.size = size;
237 domain->revmap_data.linear.revmap = revmap;
238 irq_domain_add(domain);
242 struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
243 unsigned int max_irq,
244 const struct irq_domain_ops *ops,
247 struct irq_domain *domain = irq_domain_alloc(of_node,
248 IRQ_DOMAIN_MAP_NOMAP, ops, host_data);
250 domain->revmap_data.nomap.max_irq = max_irq ? max_irq : ~0;
251 irq_domain_add(domain);
257 * irq_domain_add_tree()
258 * @of_node: pointer to interrupt controller's device tree node.
259 * @ops: map/unmap domain callbacks
261 * Note: The radix tree will be allocated later during boot automatically
262 * (the reverse mapping will use the slow path until that happens).
264 struct irq_domain *irq_domain_add_tree(struct device_node *of_node,
265 const struct irq_domain_ops *ops,
268 struct irq_domain *domain = irq_domain_alloc(of_node,
269 IRQ_DOMAIN_MAP_TREE, ops, host_data);
271 INIT_RADIX_TREE(&domain->revmap_data.tree, GFP_KERNEL);
272 irq_domain_add(domain);
278 * irq_find_host() - Locates a domain for a given device node
279 * @node: device-tree node of the interrupt controller
281 struct irq_domain *irq_find_host(struct device_node *node)
283 struct irq_domain *h, *found = NULL;
286 /* We might want to match the legacy controller last since
287 * it might potentially be set to match all interrupts in
288 * the absence of a device node. This isn't a problem so far
291 mutex_lock(&irq_domain_mutex);
292 list_for_each_entry(h, &irq_domain_list, link) {
294 rc = h->ops->match(h, node);
296 rc = (h->of_node != NULL) && (h->of_node == node);
303 mutex_unlock(&irq_domain_mutex);
306 EXPORT_SYMBOL_GPL(irq_find_host);
309 * irq_set_default_host() - Set a "default" irq domain
310 * @domain: default domain pointer
312 * For convenience, it's possible to set a "default" domain that will be used
313 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
314 * platforms that want to manipulate a few hard coded interrupt numbers that
315 * aren't properly represented in the device-tree.
317 void irq_set_default_host(struct irq_domain *domain)
319 pr_debug("irq: Default domain set to @0x%p\n", domain);
321 irq_default_domain = domain;
324 static int irq_setup_virq(struct irq_domain *domain, unsigned int virq,
325 irq_hw_number_t hwirq)
327 struct irq_data *irq_data = irq_get_irq_data(virq);
329 irq_data->hwirq = hwirq;
330 irq_data->domain = domain;
331 if (domain->ops->map(domain, virq, hwirq)) {
332 pr_debug("irq: -> mapping failed, freeing\n");
333 irq_data->domain = NULL;
338 irq_clear_status_flags(virq, IRQ_NOREQUEST);
344 * irq_create_direct_mapping() - Allocate an irq for direct mapping
345 * @domain: domain to allocate the irq for or NULL for default domain
347 * This routine is used for irq controllers which can choose the hardware
348 * interrupt numbers they generate. In such a case it's simplest to use
349 * the linux irq as the hardware interrupt number.
351 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
356 domain = irq_default_domain;
358 BUG_ON(domain == NULL);
359 WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_NOMAP);
361 virq = irq_alloc_desc_from(1, 0);
363 pr_debug("irq: create_direct virq allocation failed\n");
366 if (virq >= domain->revmap_data.nomap.max_irq) {
367 pr_err("ERROR: no free irqs available below %i maximum\n",
368 domain->revmap_data.nomap.max_irq);
372 pr_debug("irq: create_direct obtained virq %d\n", virq);
374 if (irq_setup_virq(domain, virq, virq)) {
383 * irq_create_mapping() - Map a hardware interrupt into linux irq space
384 * @domain: domain owning this hardware interrupt or NULL for default domain
385 * @hwirq: hardware irq number in that domain space
387 * Only one mapping per hardware interrupt is permitted. Returns a linux
389 * If the sense/trigger is to be specified, set_irq_type() should be called
390 * on the number returned from that call.
392 unsigned int irq_create_mapping(struct irq_domain *domain,
393 irq_hw_number_t hwirq)
398 pr_debug("irq: irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
400 /* Look for default domain if nececssary */
402 domain = irq_default_domain;
403 if (domain == NULL) {
404 printk(KERN_WARNING "irq_create_mapping called for"
405 " NULL domain, hwirq=%lx\n", hwirq);
409 pr_debug("irq: -> using domain @%p\n", domain);
411 /* Check if mapping already exists */
412 virq = irq_find_mapping(domain, hwirq);
414 pr_debug("irq: -> existing mapping on virq %d\n", virq);
418 /* Get a virtual interrupt number */
419 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
420 return irq_domain_legacy_revmap(domain, hwirq);
422 /* Allocate a virtual interrupt number */
423 hint = hwirq % nr_irqs;
426 virq = irq_alloc_desc_from(hint, 0);
428 virq = irq_alloc_desc_from(1, 0);
430 pr_debug("irq: -> virq allocation failed\n");
434 if (irq_setup_virq(domain, virq, hwirq)) {
435 if (domain->revmap_type != IRQ_DOMAIN_MAP_LEGACY)
440 pr_debug("irq: irq %lu on domain %s mapped to virtual irq %u\n",
441 hwirq, domain->of_node ? domain->of_node->full_name : "null", virq);
445 EXPORT_SYMBOL_GPL(irq_create_mapping);
447 unsigned int irq_create_of_mapping(struct device_node *controller,
448 const u32 *intspec, unsigned int intsize)
450 struct irq_domain *domain;
451 irq_hw_number_t hwirq;
452 unsigned int type = IRQ_TYPE_NONE;
455 domain = controller ? irq_find_host(controller) : irq_default_domain;
459 * Workaround to avoid breaking interrupt controller drivers
460 * that don't yet register an irq_domain. This is temporary
461 * code. ~~~gcl, Feb 24, 2012
463 * Scheduled for removal in Linux v3.6. That should be enough
469 printk(KERN_WARNING "irq: no irq domain found for %s !\n",
470 controller->full_name);
474 /* If domain has no translation, then we assume interrupt line */
475 if (domain->ops->xlate == NULL)
478 if (domain->ops->xlate(domain, controller, intspec, intsize,
484 virq = irq_create_mapping(domain, hwirq);
488 /* Set type if specified and different than the current one */
489 if (type != IRQ_TYPE_NONE &&
490 type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
491 irq_set_irq_type(virq, type);
494 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
497 * irq_dispose_mapping() - Unmap an interrupt
498 * @virq: linux irq number of the interrupt to unmap
500 void irq_dispose_mapping(unsigned int virq)
502 struct irq_data *irq_data = irq_get_irq_data(virq);
503 struct irq_domain *domain;
504 irq_hw_number_t hwirq;
506 if (!virq || !irq_data)
509 domain = irq_data->domain;
510 if (WARN_ON(domain == NULL))
513 /* Never unmap legacy interrupts */
514 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
517 irq_set_status_flags(virq, IRQ_NOREQUEST);
519 /* remove chip and handler */
520 irq_set_chip_and_handler(virq, NULL, NULL);
522 /* Make sure it's completed */
523 synchronize_irq(virq);
525 /* Tell the PIC about it */
526 if (domain->ops->unmap)
527 domain->ops->unmap(domain, virq);
530 /* Clear reverse map */
531 hwirq = irq_data->hwirq;
532 switch(domain->revmap_type) {
533 case IRQ_DOMAIN_MAP_LINEAR:
534 if (hwirq < domain->revmap_data.linear.size)
535 domain->revmap_data.linear.revmap[hwirq] = 0;
537 case IRQ_DOMAIN_MAP_TREE:
538 mutex_lock(&revmap_trees_mutex);
539 radix_tree_delete(&domain->revmap_data.tree, hwirq);
540 mutex_unlock(&revmap_trees_mutex);
546 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
549 * irq_find_mapping() - Find a linux irq from an hw irq number.
550 * @domain: domain owning this hardware interrupt
551 * @hwirq: hardware irq number in that domain space
553 * This is a slow path, for use by generic code. It's expected that an
554 * irq controller implementation directly calls the appropriate low level
557 unsigned int irq_find_mapping(struct irq_domain *domain,
558 irq_hw_number_t hwirq)
561 unsigned int hint = hwirq % nr_irqs;
563 /* Look for default domain if nececssary */
565 domain = irq_default_domain;
569 /* legacy -> bail early */
570 if (domain->revmap_type == IRQ_DOMAIN_MAP_LEGACY)
571 return irq_domain_legacy_revmap(domain, hwirq);
573 /* Slow path does a linear search of the map */
578 struct irq_data *data = irq_get_irq_data(i);
579 if (data && (data->domain == domain) && (data->hwirq == hwirq))
587 EXPORT_SYMBOL_GPL(irq_find_mapping);
590 * irq_radix_revmap_lookup() - Find a linux irq from a hw irq number.
591 * @domain: domain owning this hardware interrupt
592 * @hwirq: hardware irq number in that domain space
594 * This is a fast path, for use by irq controller code that uses radix tree
597 unsigned int irq_radix_revmap_lookup(struct irq_domain *domain,
598 irq_hw_number_t hwirq)
600 struct irq_data *irq_data;
602 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
603 return irq_find_mapping(domain, hwirq);
606 * Freeing an irq can delete nodes along the path to
607 * do the lookup via call_rcu.
610 irq_data = radix_tree_lookup(&domain->revmap_data.tree, hwirq);
614 * If found in radix tree, then fine.
615 * Else fallback to linear lookup - this should not happen in practice
616 * as it means that we failed to insert the node in the radix tree.
618 return irq_data ? irq_data->irq : irq_find_mapping(domain, hwirq);
622 * irq_radix_revmap_insert() - Insert a hw irq to linux irq number mapping.
623 * @domain: domain owning this hardware interrupt
624 * @virq: linux irq number
625 * @hwirq: hardware irq number in that domain space
627 * This is for use by irq controllers that use a radix tree reverse
628 * mapping for fast lookup.
630 void irq_radix_revmap_insert(struct irq_domain *domain, unsigned int virq,
631 irq_hw_number_t hwirq)
633 struct irq_data *irq_data = irq_get_irq_data(virq);
635 if (WARN_ON(domain->revmap_type != IRQ_DOMAIN_MAP_TREE))
639 mutex_lock(&revmap_trees_mutex);
640 radix_tree_insert(&domain->revmap_data.tree, hwirq, irq_data);
641 mutex_unlock(&revmap_trees_mutex);
646 * irq_linear_revmap() - Find a linux irq from a hw irq number.
647 * @domain: domain owning this hardware interrupt
648 * @hwirq: hardware irq number in that domain space
650 * This is a fast path, for use by irq controller code that uses linear
651 * revmaps. It does fallback to the slow path if the revmap doesn't exist
652 * yet and will create the revmap entry with appropriate locking
654 unsigned int irq_linear_revmap(struct irq_domain *domain,
655 irq_hw_number_t hwirq)
657 unsigned int *revmap;
659 if (WARN_ON_ONCE(domain->revmap_type != IRQ_DOMAIN_MAP_LINEAR))
660 return irq_find_mapping(domain, hwirq);
662 /* Check revmap bounds */
663 if (unlikely(hwirq >= domain->revmap_data.linear.size))
664 return irq_find_mapping(domain, hwirq);
666 /* Check if revmap was allocated */
667 revmap = domain->revmap_data.linear.revmap;
668 if (unlikely(revmap == NULL))
669 return irq_find_mapping(domain, hwirq);
671 /* Fill up revmap with slow path if no mapping found */
672 if (unlikely(!revmap[hwirq]))
673 revmap[hwirq] = irq_find_mapping(domain, hwirq);
675 return revmap[hwirq];
678 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
679 static int virq_debug_show(struct seq_file *m, void *private)
682 struct irq_desc *desc;
684 static const char none[] = "none";
688 seq_printf(m, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq",
689 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
692 for (i = 1; i < nr_irqs; i++) {
693 desc = irq_to_desc(i);
697 raw_spin_lock_irqsave(&desc->lock, flags);
699 if (desc->action && desc->action->handler) {
700 struct irq_chip *chip;
702 seq_printf(m, "%5d ", i);
703 seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
705 chip = irq_desc_get_chip(desc);
706 if (chip && chip->name)
710 seq_printf(m, "%-15s ", p);
712 data = irq_desc_get_chip_data(desc);
713 seq_printf(m, data ? "0x%p " : " %p ", data);
715 if (desc->irq_data.domain && desc->irq_data.domain->of_node)
716 p = desc->irq_data.domain->of_node->full_name;
719 seq_printf(m, "%s\n", p);
722 raw_spin_unlock_irqrestore(&desc->lock, flags);
728 static int virq_debug_open(struct inode *inode, struct file *file)
730 return single_open(file, virq_debug_show, inode->i_private);
733 static const struct file_operations virq_debug_fops = {
734 .open = virq_debug_open,
737 .release = single_release,
740 static int __init irq_debugfs_init(void)
742 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
743 NULL, &virq_debug_fops) == NULL)
748 __initcall(irq_debugfs_init);
749 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
751 int irq_domain_simple_map(struct irq_domain *d, unsigned int irq,
752 irq_hw_number_t hwirq)
758 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
760 * Device Tree IRQ specifier translation function which works with one cell
761 * bindings where the cell value maps directly to the hwirq number.
763 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
764 const u32 *intspec, unsigned int intsize,
765 unsigned long *out_hwirq, unsigned int *out_type)
767 if (WARN_ON(intsize < 1))
769 *out_hwirq = intspec[0];
770 *out_type = IRQ_TYPE_NONE;
773 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
776 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
778 * Device Tree IRQ specifier translation function which works with two cell
779 * bindings where the cell values map directly to the hwirq number
780 * and linux irq flags.
782 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
783 const u32 *intspec, unsigned int intsize,
784 irq_hw_number_t *out_hwirq, unsigned int *out_type)
786 if (WARN_ON(intsize < 2))
788 *out_hwirq = intspec[0];
789 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
792 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
795 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
797 * Device Tree IRQ specifier translation function which works with either one
798 * or two cell bindings where the cell values map directly to the hwirq number
799 * and linux irq flags.
801 * Note: don't use this function unless your interrupt controller explicitly
802 * supports both one and two cell bindings. For the majority of controllers
803 * the _onecell() or _twocell() variants above should be used.
805 int irq_domain_xlate_onetwocell(struct irq_domain *d,
806 struct device_node *ctrlr,
807 const u32 *intspec, unsigned int intsize,
808 unsigned long *out_hwirq, unsigned int *out_type)
810 if (WARN_ON(intsize < 1))
812 *out_hwirq = intspec[0];
813 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
816 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
818 const struct irq_domain_ops irq_domain_simple_ops = {
819 .map = irq_domain_simple_map,
820 .xlate = irq_domain_xlate_onetwocell,
822 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
825 void irq_domain_generate_simple(const struct of_device_id *match,
826 u64 phys_base, unsigned int irq_start)
828 struct device_node *node;
829 pr_debug("looking for phys_base=%llx, irq_start=%i\n",
830 (unsigned long long) phys_base, (int) irq_start);
831 node = of_find_matching_node_by_address(NULL, match, phys_base);
833 irq_domain_add_legacy(node, 32, irq_start, 0,
834 &irq_domain_simple_ops, NULL);
836 EXPORT_SYMBOL_GPL(irq_domain_generate_simple);