1 #define pr_fmt(fmt) "irq: " fmt
3 #include <linux/debugfs.h>
4 #include <linux/hardirq.h>
5 #include <linux/interrupt.h>
7 #include <linux/irqdesc.h>
8 #include <linux/irqdomain.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/topology.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/smp.h>
20 static LIST_HEAD(irq_domain_list);
21 static DEFINE_MUTEX(irq_domain_mutex);
23 static DEFINE_MUTEX(revmap_trees_mutex);
24 static struct irq_domain *irq_default_domain;
26 static int irq_domain_alloc_descs(int virq, unsigned int nr_irqs,
27 irq_hw_number_t hwirq, int node);
28 static void irq_domain_check_hierarchy(struct irq_domain *domain);
31 * __irq_domain_add() - Allocate a new irq_domain data structure
32 * @of_node: optional device-tree node of the interrupt controller
33 * @size: Size of linear map; 0 for radix mapping only
34 * @hwirq_max: Maximum number of interrupts supported by controller
35 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
37 * @ops: domain callbacks
38 * @host_data: Controller private data pointer
40 * Allocates and initialize and irq_domain structure.
41 * Returns pointer to IRQ domain, or NULL on failure.
43 struct irq_domain *__irq_domain_add(struct device_node *of_node, int size,
44 irq_hw_number_t hwirq_max, int direct_max,
45 const struct irq_domain_ops *ops,
48 struct irq_domain *domain;
50 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
51 GFP_KERNEL, of_node_to_nid(of_node));
56 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
58 domain->host_data = host_data;
59 domain->of_node = of_node_get(of_node);
60 domain->hwirq_max = hwirq_max;
61 domain->revmap_size = size;
62 domain->revmap_direct_max_irq = direct_max;
63 irq_domain_check_hierarchy(domain);
65 mutex_lock(&irq_domain_mutex);
66 list_add(&domain->link, &irq_domain_list);
67 mutex_unlock(&irq_domain_mutex);
69 pr_debug("Added domain %s\n", domain->name);
72 EXPORT_SYMBOL_GPL(__irq_domain_add);
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);
87 * radix_tree_delete() takes care of destroying the root
88 * node when all entries are removed. Shout if there are
91 WARN_ON(domain->revmap_tree.height);
93 list_del(&domain->link);
96 * If the going away domain is the default one, reset it.
98 if (unlikely(irq_default_domain == domain))
99 irq_set_default_host(NULL);
101 mutex_unlock(&irq_domain_mutex);
103 pr_debug("Removed domain %s\n", domain->name);
105 of_node_put(irq_domain_get_of_node(domain));
108 EXPORT_SYMBOL_GPL(irq_domain_remove);
111 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
112 * @of_node: pointer to interrupt controller's device tree node.
113 * @size: total number of irqs in mapping
114 * @first_irq: first number of irq block assigned to the domain,
115 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
116 * pre-map all of the irqs in the domain to virqs starting at first_irq.
117 * @ops: domain callbacks
118 * @host_data: Controller private data pointer
120 * Allocates an irq_domain, and optionally if first_irq is positive then also
121 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
123 * This is intended to implement the expected behaviour for most
124 * interrupt controllers. If device tree is used, then first_irq will be 0 and
125 * irqs get mapped dynamically on the fly. However, if the controller requires
126 * static virq assignments (non-DT boot) then it will set that up correctly.
128 struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
130 unsigned int first_irq,
131 const struct irq_domain_ops *ops,
134 struct irq_domain *domain;
136 domain = __irq_domain_add(of_node, size, size, 0, ops, host_data);
141 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
142 /* attempt to allocated irq_descs */
143 int rc = irq_alloc_descs(first_irq, first_irq, size,
144 of_node_to_nid(of_node));
146 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
149 irq_domain_associate_many(domain, first_irq, 0, size);
154 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
157 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
158 * @of_node: pointer to interrupt controller's device tree node.
159 * @size: total number of irqs in legacy mapping
160 * @first_irq: first number of irq block assigned to the domain
161 * @first_hwirq: first hwirq number to use for the translation. Should normally
162 * be '0', but a positive integer can be used if the effective
163 * hwirqs numbering does not begin at zero.
164 * @ops: map/unmap domain callbacks
165 * @host_data: Controller private data pointer
167 * Note: the map() callback will be called before this function returns
168 * for all legacy interrupts except 0 (which is always the invalid irq for
169 * a legacy controller).
171 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
173 unsigned int first_irq,
174 irq_hw_number_t first_hwirq,
175 const struct irq_domain_ops *ops,
178 struct irq_domain *domain;
180 domain = __irq_domain_add(of_node, first_hwirq + size,
181 first_hwirq + size, 0, ops, host_data);
183 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
187 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
190 * irq_find_matching_host() - Locates a domain for a given device node
191 * @node: device-tree node of the interrupt controller
192 * @bus_token: domain-specific data
194 struct irq_domain *irq_find_matching_host(struct device_node *node,
195 enum irq_domain_bus_token bus_token)
197 struct irq_domain *h, *found = NULL;
200 /* We might want to match the legacy controller last since
201 * it might potentially be set to match all interrupts in
202 * the absence of a device node. This isn't a problem so far
205 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
206 * values must generate an exact match for the domain to be
209 mutex_lock(&irq_domain_mutex);
210 list_for_each_entry(h, &irq_domain_list, link) {
211 struct device_node *of_node;
212 of_node = irq_domain_get_of_node(h);
214 rc = h->ops->match(h, node, bus_token);
216 rc = ((of_node != NULL) && (of_node == node) &&
217 ((bus_token == DOMAIN_BUS_ANY) ||
218 (h->bus_token == bus_token)));
225 mutex_unlock(&irq_domain_mutex);
228 EXPORT_SYMBOL_GPL(irq_find_matching_host);
231 * irq_set_default_host() - Set a "default" irq domain
232 * @domain: default domain pointer
234 * For convenience, it's possible to set a "default" domain that will be used
235 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
236 * platforms that want to manipulate a few hard coded interrupt numbers that
237 * aren't properly represented in the device-tree.
239 void irq_set_default_host(struct irq_domain *domain)
241 pr_debug("Default domain set to @0x%p\n", domain);
243 irq_default_domain = domain;
245 EXPORT_SYMBOL_GPL(irq_set_default_host);
247 void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
249 struct irq_data *irq_data = irq_get_irq_data(irq);
250 irq_hw_number_t hwirq;
252 if (WARN(!irq_data || irq_data->domain != domain,
253 "virq%i doesn't exist; cannot disassociate\n", irq))
256 hwirq = irq_data->hwirq;
257 irq_set_status_flags(irq, IRQ_NOREQUEST);
259 /* remove chip and handler */
260 irq_set_chip_and_handler(irq, NULL, NULL);
262 /* Make sure it's completed */
263 synchronize_irq(irq);
265 /* Tell the PIC about it */
266 if (domain->ops->unmap)
267 domain->ops->unmap(domain, irq);
270 irq_data->domain = NULL;
273 /* Clear reverse map for this hwirq */
274 if (hwirq < domain->revmap_size) {
275 domain->linear_revmap[hwirq] = 0;
277 mutex_lock(&revmap_trees_mutex);
278 radix_tree_delete(&domain->revmap_tree, hwirq);
279 mutex_unlock(&revmap_trees_mutex);
283 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
284 irq_hw_number_t hwirq)
286 struct irq_data *irq_data = irq_get_irq_data(virq);
289 if (WARN(hwirq >= domain->hwirq_max,
290 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
292 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
294 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
297 mutex_lock(&irq_domain_mutex);
298 irq_data->hwirq = hwirq;
299 irq_data->domain = domain;
300 if (domain->ops->map) {
301 ret = domain->ops->map(domain, virq, hwirq);
304 * If map() returns -EPERM, this interrupt is protected
305 * by the firmware or some other service and shall not
306 * be mapped. Don't bother telling the user about it.
309 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
310 domain->name, hwirq, virq, ret);
312 irq_data->domain = NULL;
314 mutex_unlock(&irq_domain_mutex);
318 /* If not already assigned, give the domain the chip's name */
319 if (!domain->name && irq_data->chip)
320 domain->name = irq_data->chip->name;
323 if (hwirq < domain->revmap_size) {
324 domain->linear_revmap[hwirq] = virq;
326 mutex_lock(&revmap_trees_mutex);
327 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
328 mutex_unlock(&revmap_trees_mutex);
330 mutex_unlock(&irq_domain_mutex);
332 irq_clear_status_flags(virq, IRQ_NOREQUEST);
336 EXPORT_SYMBOL_GPL(irq_domain_associate);
338 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
339 irq_hw_number_t hwirq_base, int count)
341 struct device_node *of_node;
344 of_node = irq_domain_get_of_node(domain);
345 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
346 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
348 for (i = 0; i < count; i++) {
349 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
352 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
355 * irq_create_direct_mapping() - Allocate an irq for direct mapping
356 * @domain: domain to allocate the irq for or NULL for default domain
358 * This routine is used for irq controllers which can choose the hardware
359 * interrupt numbers they generate. In such a case it's simplest to use
360 * the linux irq as the hardware interrupt number. It still uses the linear
361 * or radix tree to store the mapping, but the irq controller can optimize
362 * the revmap path by using the hwirq directly.
364 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
366 struct device_node *of_node;
370 domain = irq_default_domain;
372 of_node = irq_domain_get_of_node(domain);
373 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
375 pr_debug("create_direct virq allocation failed\n");
378 if (virq >= domain->revmap_direct_max_irq) {
379 pr_err("ERROR: no free irqs available below %i maximum\n",
380 domain->revmap_direct_max_irq);
384 pr_debug("create_direct obtained virq %d\n", virq);
386 if (irq_domain_associate(domain, virq, virq)) {
393 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
396 * irq_create_mapping() - Map a hardware interrupt into linux irq space
397 * @domain: domain owning this hardware interrupt or NULL for default domain
398 * @hwirq: hardware irq number in that domain space
400 * Only one mapping per hardware interrupt is permitted. Returns a linux
402 * If the sense/trigger is to be specified, set_irq_type() should be called
403 * on the number returned from that call.
405 unsigned int irq_create_mapping(struct irq_domain *domain,
406 irq_hw_number_t hwirq)
408 struct device_node *of_node;
411 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
413 /* Look for default domain if nececssary */
415 domain = irq_default_domain;
416 if (domain == NULL) {
417 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
420 pr_debug("-> using domain @%p\n", domain);
422 of_node = irq_domain_get_of_node(domain);
424 /* Check if mapping already exists */
425 virq = irq_find_mapping(domain, hwirq);
427 pr_debug("-> existing mapping on virq %d\n", virq);
431 /* Allocate a virtual interrupt number */
432 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node));
434 pr_debug("-> virq allocation failed\n");
438 if (irq_domain_associate(domain, virq, hwirq)) {
443 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
444 hwirq, of_node_full_name(of_node), virq);
448 EXPORT_SYMBOL_GPL(irq_create_mapping);
451 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
452 * @domain: domain owning the interrupt range
453 * @irq_base: beginning of linux IRQ range
454 * @hwirq_base: beginning of hardware IRQ range
455 * @count: Number of interrupts to map
457 * This routine is used for allocating and mapping a range of hardware
458 * irqs to linux irqs where the linux irq numbers are at pre-defined
459 * locations. For use by controllers that already have static mappings
460 * to insert in to the domain.
462 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
465 * 0 is returned upon success, while any failure to establish a static
466 * mapping is treated as an error.
468 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
469 irq_hw_number_t hwirq_base, int count)
471 struct device_node *of_node;
474 of_node = irq_domain_get_of_node(domain);
475 ret = irq_alloc_descs(irq_base, irq_base, count,
476 of_node_to_nid(of_node));
477 if (unlikely(ret < 0))
480 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
483 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
485 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
487 struct irq_domain *domain;
488 irq_hw_number_t hwirq;
489 unsigned int type = IRQ_TYPE_NONE;
492 domain = irq_data->np ? irq_find_host(irq_data->np) : irq_default_domain;
494 pr_warn("no irq domain found for %s !\n",
495 of_node_full_name(irq_data->np));
499 /* If domain has no translation, then we assume interrupt line */
500 if (domain->ops->xlate == NULL)
501 hwirq = irq_data->args[0];
503 if (domain->ops->xlate(domain, irq_data->np, irq_data->args,
504 irq_data->args_count, &hwirq, &type))
508 if (irq_domain_is_hierarchy(domain)) {
510 * If we've already configured this interrupt,
511 * don't do it again, or hell will break loose.
513 virq = irq_find_mapping(domain, hwirq);
517 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, irq_data);
522 virq = irq_create_mapping(domain, hwirq);
527 /* Set type if specified and different than the current one */
528 if (type != IRQ_TYPE_NONE &&
529 type != irq_get_trigger_type(virq))
530 irq_set_irq_type(virq, type);
533 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
536 * irq_dispose_mapping() - Unmap an interrupt
537 * @virq: linux irq number of the interrupt to unmap
539 void irq_dispose_mapping(unsigned int virq)
541 struct irq_data *irq_data = irq_get_irq_data(virq);
542 struct irq_domain *domain;
544 if (!virq || !irq_data)
547 domain = irq_data->domain;
548 if (WARN_ON(domain == NULL))
551 irq_domain_disassociate(domain, virq);
554 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
557 * irq_find_mapping() - Find a linux irq from an hw irq number.
558 * @domain: domain owning this hardware interrupt
559 * @hwirq: hardware irq number in that domain space
561 unsigned int irq_find_mapping(struct irq_domain *domain,
562 irq_hw_number_t hwirq)
564 struct irq_data *data;
566 /* Look for default domain if nececssary */
568 domain = irq_default_domain;
572 if (hwirq < domain->revmap_direct_max_irq) {
573 data = irq_domain_get_irq_data(domain, hwirq);
574 if (data && data->hwirq == hwirq)
578 /* Check if the hwirq is in the linear revmap. */
579 if (hwirq < domain->revmap_size)
580 return domain->linear_revmap[hwirq];
583 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
585 return data ? data->irq : 0;
587 EXPORT_SYMBOL_GPL(irq_find_mapping);
589 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
590 static int virq_debug_show(struct seq_file *m, void *private)
593 struct irq_desc *desc;
594 struct irq_domain *domain;
595 struct radix_tree_iter iter;
599 seq_printf(m, " %-16s %-6s %-10s %-10s %s\n",
600 "name", "mapped", "linear-max", "direct-max", "devtree-node");
601 mutex_lock(&irq_domain_mutex);
602 list_for_each_entry(domain, &irq_domain_list, link) {
603 struct device_node *of_node;
605 of_node = irq_domain_get_of_node(domain);
606 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
608 seq_printf(m, "%c%-16s %6u %10u %10u %s\n",
609 domain == irq_default_domain ? '*' : ' ', domain->name,
610 domain->revmap_size + count, domain->revmap_size,
611 domain->revmap_direct_max_irq,
612 of_node ? of_node_full_name(of_node) : "");
614 mutex_unlock(&irq_domain_mutex);
616 seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq",
617 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
618 "active", "type", "domain");
620 for (i = 1; i < nr_irqs; i++) {
621 desc = irq_to_desc(i);
625 raw_spin_lock_irqsave(&desc->lock, flags);
626 domain = desc->irq_data.domain;
629 struct irq_chip *chip;
630 int hwirq = desc->irq_data.hwirq;
633 seq_printf(m, "%5d ", i);
634 seq_printf(m, "0x%05x ", hwirq);
636 chip = irq_desc_get_chip(desc);
637 seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
639 data = irq_desc_get_chip_data(desc);
640 seq_printf(m, data ? "0x%p " : " %p ", data);
642 seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' ');
643 direct = (i == hwirq) && (i < domain->revmap_direct_max_irq);
644 seq_printf(m, "%6s%-8s ",
645 (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
646 direct ? "(DIRECT)" : "");
647 seq_printf(m, "%s\n", desc->irq_data.domain->name);
650 raw_spin_unlock_irqrestore(&desc->lock, flags);
656 static int virq_debug_open(struct inode *inode, struct file *file)
658 return single_open(file, virq_debug_show, inode->i_private);
661 static const struct file_operations virq_debug_fops = {
662 .open = virq_debug_open,
665 .release = single_release,
668 static int __init irq_debugfs_init(void)
670 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
671 NULL, &virq_debug_fops) == NULL)
676 __initcall(irq_debugfs_init);
677 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
680 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
682 * Device Tree IRQ specifier translation function which works with one cell
683 * bindings where the cell value maps directly to the hwirq number.
685 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
686 const u32 *intspec, unsigned int intsize,
687 unsigned long *out_hwirq, unsigned int *out_type)
689 if (WARN_ON(intsize < 1))
691 *out_hwirq = intspec[0];
692 *out_type = IRQ_TYPE_NONE;
695 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
698 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
700 * Device Tree IRQ specifier translation function which works with two cell
701 * bindings where the cell values map directly to the hwirq number
702 * and linux irq flags.
704 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
705 const u32 *intspec, unsigned int intsize,
706 irq_hw_number_t *out_hwirq, unsigned int *out_type)
708 if (WARN_ON(intsize < 2))
710 *out_hwirq = intspec[0];
711 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
714 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
717 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
719 * Device Tree IRQ specifier translation function which works with either one
720 * or two cell bindings where the cell values map directly to the hwirq number
721 * and linux irq flags.
723 * Note: don't use this function unless your interrupt controller explicitly
724 * supports both one and two cell bindings. For the majority of controllers
725 * the _onecell() or _twocell() variants above should be used.
727 int irq_domain_xlate_onetwocell(struct irq_domain *d,
728 struct device_node *ctrlr,
729 const u32 *intspec, unsigned int intsize,
730 unsigned long *out_hwirq, unsigned int *out_type)
732 if (WARN_ON(intsize < 1))
734 *out_hwirq = intspec[0];
735 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
738 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
740 const struct irq_domain_ops irq_domain_simple_ops = {
741 .xlate = irq_domain_xlate_onetwocell,
743 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
745 static int irq_domain_alloc_descs(int virq, unsigned int cnt,
746 irq_hw_number_t hwirq, int node)
751 virq = irq_alloc_descs(virq, virq, cnt, node);
753 hint = hwirq % nr_irqs;
756 virq = irq_alloc_descs_from(hint, cnt, node);
757 if (virq <= 0 && hint > 1)
758 virq = irq_alloc_descs_from(1, cnt, node);
764 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
766 * irq_domain_add_hierarchy - Add a irqdomain into the hierarchy
767 * @parent: Parent irq domain to associate with the new domain
768 * @flags: Irq domain flags associated to the domain
769 * @size: Size of the domain. See below
770 * @node: Optional device-tree node of the interrupt controller
771 * @ops: Pointer to the interrupt domain callbacks
772 * @host_data: Controller private data pointer
774 * If @size is 0 a tree domain is created, otherwise a linear domain.
776 * If successful the parent is associated to the new domain and the
777 * domain flags are set.
778 * Returns pointer to IRQ domain, or NULL on failure.
780 struct irq_domain *irq_domain_add_hierarchy(struct irq_domain *parent,
783 struct device_node *node,
784 const struct irq_domain_ops *ops,
787 struct irq_domain *domain;
790 domain = irq_domain_add_linear(node, size, ops, host_data);
792 domain = irq_domain_add_tree(node, ops, host_data);
794 domain->parent = parent;
795 domain->flags |= flags;
801 static void irq_domain_insert_irq(int virq)
803 struct irq_data *data;
805 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
806 struct irq_domain *domain = data->domain;
807 irq_hw_number_t hwirq = data->hwirq;
809 if (hwirq < domain->revmap_size) {
810 domain->linear_revmap[hwirq] = virq;
812 mutex_lock(&revmap_trees_mutex);
813 radix_tree_insert(&domain->revmap_tree, hwirq, data);
814 mutex_unlock(&revmap_trees_mutex);
817 /* If not already assigned, give the domain the chip's name */
818 if (!domain->name && data->chip)
819 domain->name = data->chip->name;
822 irq_clear_status_flags(virq, IRQ_NOREQUEST);
825 static void irq_domain_remove_irq(int virq)
827 struct irq_data *data;
829 irq_set_status_flags(virq, IRQ_NOREQUEST);
830 irq_set_chip_and_handler(virq, NULL, NULL);
831 synchronize_irq(virq);
834 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
835 struct irq_domain *domain = data->domain;
836 irq_hw_number_t hwirq = data->hwirq;
838 if (hwirq < domain->revmap_size) {
839 domain->linear_revmap[hwirq] = 0;
841 mutex_lock(&revmap_trees_mutex);
842 radix_tree_delete(&domain->revmap_tree, hwirq);
843 mutex_unlock(&revmap_trees_mutex);
848 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
849 struct irq_data *child)
851 struct irq_data *irq_data;
853 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
854 irq_data_get_node(child));
856 child->parent_data = irq_data;
857 irq_data->irq = child->irq;
858 irq_data->common = child->common;
859 irq_data->domain = domain;
865 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
867 struct irq_data *irq_data, *tmp;
870 for (i = 0; i < nr_irqs; i++) {
871 irq_data = irq_get_irq_data(virq + i);
872 tmp = irq_data->parent_data;
873 irq_data->parent_data = NULL;
874 irq_data->domain = NULL;
878 tmp = tmp->parent_data;
884 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
885 unsigned int virq, unsigned int nr_irqs)
887 struct irq_data *irq_data;
888 struct irq_domain *parent;
891 /* The outermost irq_data is embedded in struct irq_desc */
892 for (i = 0; i < nr_irqs; i++) {
893 irq_data = irq_get_irq_data(virq + i);
894 irq_data->domain = domain;
896 for (parent = domain->parent; parent; parent = parent->parent) {
897 irq_data = irq_domain_insert_irq_data(parent, irq_data);
899 irq_domain_free_irq_data(virq, i + 1);
909 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
910 * @domain: domain to match
911 * @virq: IRQ number to get irq_data
913 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
916 struct irq_data *irq_data;
918 for (irq_data = irq_get_irq_data(virq); irq_data;
919 irq_data = irq_data->parent_data)
920 if (irq_data->domain == domain)
927 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
928 * @domain: Interrupt domain to match
930 * @hwirq: The hwirq number
931 * @chip: The associated interrupt chip
932 * @chip_data: The associated chip data
934 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
935 irq_hw_number_t hwirq, struct irq_chip *chip,
938 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
943 irq_data->hwirq = hwirq;
944 irq_data->chip = chip ? chip : &no_irq_chip;
945 irq_data->chip_data = chip_data;
951 * irq_domain_set_info - Set the complete data for a @virq in @domain
952 * @domain: Interrupt domain to match
954 * @hwirq: The hardware interrupt number
955 * @chip: The associated interrupt chip
956 * @chip_data: The associated interrupt chip data
957 * @handler: The interrupt flow handler
958 * @handler_data: The interrupt flow handler data
959 * @handler_name: The interrupt handler name
961 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
962 irq_hw_number_t hwirq, struct irq_chip *chip,
963 void *chip_data, irq_flow_handler_t handler,
964 void *handler_data, const char *handler_name)
966 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
967 __irq_set_handler(virq, handler, 0, handler_name);
968 irq_set_handler_data(virq, handler_data);
972 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
973 * @irq_data: The pointer to irq_data
975 void irq_domain_reset_irq_data(struct irq_data *irq_data)
978 irq_data->chip = &no_irq_chip;
979 irq_data->chip_data = NULL;
983 * irq_domain_free_irqs_common - Clear irq_data and free the parent
984 * @domain: Interrupt domain to match
985 * @virq: IRQ number to start with
986 * @nr_irqs: The number of irqs to free
988 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
989 unsigned int nr_irqs)
991 struct irq_data *irq_data;
994 for (i = 0; i < nr_irqs; i++) {
995 irq_data = irq_domain_get_irq_data(domain, virq + i);
997 irq_domain_reset_irq_data(irq_data);
999 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1003 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1004 * @domain: Interrupt domain to match
1005 * @virq: IRQ number to start with
1006 * @nr_irqs: The number of irqs to free
1008 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1009 unsigned int nr_irqs)
1013 for (i = 0; i < nr_irqs; i++) {
1014 irq_set_handler_data(virq + i, NULL);
1015 irq_set_handler(virq + i, NULL);
1017 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1020 static bool irq_domain_is_auto_recursive(struct irq_domain *domain)
1022 return domain->flags & IRQ_DOMAIN_FLAG_AUTO_RECURSIVE;
1025 static void irq_domain_free_irqs_recursive(struct irq_domain *domain,
1026 unsigned int irq_base,
1027 unsigned int nr_irqs)
1029 domain->ops->free(domain, irq_base, nr_irqs);
1030 if (irq_domain_is_auto_recursive(domain)) {
1031 BUG_ON(!domain->parent);
1032 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1037 static int irq_domain_alloc_irqs_recursive(struct irq_domain *domain,
1038 unsigned int irq_base,
1039 unsigned int nr_irqs, void *arg)
1042 struct irq_domain *parent = domain->parent;
1043 bool recursive = irq_domain_is_auto_recursive(domain);
1045 BUG_ON(recursive && !parent);
1047 ret = irq_domain_alloc_irqs_recursive(parent, irq_base,
1050 ret = domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1051 if (ret < 0 && recursive)
1052 irq_domain_free_irqs_recursive(parent, irq_base, nr_irqs);
1058 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1059 * @domain: domain to allocate from
1060 * @irq_base: allocate specified IRQ nubmer if irq_base >= 0
1061 * @nr_irqs: number of IRQs to allocate
1062 * @node: NUMA node id for memory allocation
1063 * @arg: domain specific argument
1064 * @realloc: IRQ descriptors have already been allocated if true
1066 * Allocate IRQ numbers and initialized all data structures to support
1067 * hierarchy IRQ domains.
1068 * Parameter @realloc is mainly to support legacy IRQs.
1069 * Returns error code or allocated IRQ number
1071 * The whole process to setup an IRQ has been split into two steps.
1072 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1073 * descriptor and required hardware resources. The second step,
1074 * irq_domain_activate_irq(), is to program hardwares with preallocated
1075 * resources. In this way, it's easier to rollback when failing to
1076 * allocate resources.
1078 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1079 unsigned int nr_irqs, int node, void *arg,
1084 if (domain == NULL) {
1085 domain = irq_default_domain;
1086 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1090 if (!domain->ops->alloc) {
1091 pr_debug("domain->ops->alloc() is NULL\n");
1095 if (realloc && irq_base >= 0) {
1098 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node);
1100 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1106 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1107 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1112 mutex_lock(&irq_domain_mutex);
1113 ret = irq_domain_alloc_irqs_recursive(domain, virq, nr_irqs, arg);
1115 mutex_unlock(&irq_domain_mutex);
1116 goto out_free_irq_data;
1118 for (i = 0; i < nr_irqs; i++)
1119 irq_domain_insert_irq(virq + i);
1120 mutex_unlock(&irq_domain_mutex);
1125 irq_domain_free_irq_data(virq, nr_irqs);
1127 irq_free_descs(virq, nr_irqs);
1132 * irq_domain_free_irqs - Free IRQ number and associated data structures
1133 * @virq: base IRQ number
1134 * @nr_irqs: number of IRQs to free
1136 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1138 struct irq_data *data = irq_get_irq_data(virq);
1141 if (WARN(!data || !data->domain || !data->domain->ops->free,
1142 "NULL pointer, cannot free irq\n"))
1145 mutex_lock(&irq_domain_mutex);
1146 for (i = 0; i < nr_irqs; i++)
1147 irq_domain_remove_irq(virq + i);
1148 irq_domain_free_irqs_recursive(data->domain, virq, nr_irqs);
1149 mutex_unlock(&irq_domain_mutex);
1151 irq_domain_free_irq_data(virq, nr_irqs);
1152 irq_free_descs(virq, nr_irqs);
1156 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1157 * @irq_base: Base IRQ number
1158 * @nr_irqs: Number of IRQs to allocate
1159 * @arg: Allocation data (arch/domain specific)
1161 * Check whether the domain has been setup recursive. If not allocate
1162 * through the parent domain.
1164 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1165 unsigned int irq_base, unsigned int nr_irqs,
1168 /* irq_domain_alloc_irqs_recursive() has called parent's alloc() */
1169 if (irq_domain_is_auto_recursive(domain))
1172 domain = domain->parent;
1174 return irq_domain_alloc_irqs_recursive(domain, irq_base,
1180 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1181 * @irq_base: Base IRQ number
1182 * @nr_irqs: Number of IRQs to free
1184 * Check whether the domain has been setup recursive. If not free
1185 * through the parent domain.
1187 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1188 unsigned int irq_base, unsigned int nr_irqs)
1190 /* irq_domain_free_irqs_recursive() will call parent's free */
1191 if (!irq_domain_is_auto_recursive(domain) && domain->parent)
1192 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1197 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1199 * @irq_data: outermost irq_data associated with interrupt
1201 * This is the second step to call domain_ops->activate to program interrupt
1202 * controllers, so the interrupt could actually get delivered.
1204 void irq_domain_activate_irq(struct irq_data *irq_data)
1206 if (irq_data && irq_data->domain) {
1207 struct irq_domain *domain = irq_data->domain;
1209 if (irq_data->parent_data)
1210 irq_domain_activate_irq(irq_data->parent_data);
1211 if (domain->ops->activate)
1212 domain->ops->activate(domain, irq_data);
1217 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1218 * deactivate interrupt
1219 * @irq_data: outermost irq_data associated with interrupt
1221 * It calls domain_ops->deactivate to program interrupt controllers to disable
1222 * interrupt delivery.
1224 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1226 if (irq_data && irq_data->domain) {
1227 struct irq_domain *domain = irq_data->domain;
1229 if (domain->ops->deactivate)
1230 domain->ops->deactivate(domain, irq_data);
1231 if (irq_data->parent_data)
1232 irq_domain_deactivate_irq(irq_data->parent_data);
1236 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1238 /* Hierarchy irq_domains must implement callback alloc() */
1239 if (domain->ops->alloc)
1240 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1242 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1244 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1245 * @domain: domain to match
1246 * @virq: IRQ number to get irq_data
1248 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1251 struct irq_data *irq_data = irq_get_irq_data(virq);
1253 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1257 * irq_domain_set_info - Set the complete data for a @virq in @domain
1258 * @domain: Interrupt domain to match
1260 * @hwirq: The hardware interrupt number
1261 * @chip: The associated interrupt chip
1262 * @chip_data: The associated interrupt chip data
1263 * @handler: The interrupt flow handler
1264 * @handler_data: The interrupt flow handler data
1265 * @handler_name: The interrupt handler name
1267 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1268 irq_hw_number_t hwirq, struct irq_chip *chip,
1269 void *chip_data, irq_flow_handler_t handler,
1270 void *handler_data, const char *handler_name)
1272 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1273 irq_set_chip_data(virq, chip_data);
1274 irq_set_handler_data(virq, handler_data);
1277 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1280 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */