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 fwnode_handle *fwnode, int size,
44 irq_hw_number_t hwirq_max, int direct_max,
45 const struct irq_domain_ops *ops,
48 struct irq_domain *domain;
49 struct device_node *of_node;
51 of_node = to_of_node(fwnode);
53 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
54 GFP_KERNEL, of_node_to_nid(of_node));
61 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
63 domain->host_data = host_data;
64 domain->fwnode = fwnode;
65 domain->hwirq_max = hwirq_max;
66 domain->revmap_size = size;
67 domain->revmap_direct_max_irq = direct_max;
68 irq_domain_check_hierarchy(domain);
70 mutex_lock(&irq_domain_mutex);
71 list_add(&domain->link, &irq_domain_list);
72 mutex_unlock(&irq_domain_mutex);
74 pr_debug("Added domain %s\n", domain->name);
77 EXPORT_SYMBOL_GPL(__irq_domain_add);
80 * irq_domain_remove() - Remove an irq domain.
81 * @domain: domain to remove
83 * This routine is used to remove an irq domain. The caller must ensure
84 * that all mappings within the domain have been disposed of prior to
85 * use, depending on the revmap type.
87 void irq_domain_remove(struct irq_domain *domain)
89 mutex_lock(&irq_domain_mutex);
92 * radix_tree_delete() takes care of destroying the root
93 * node when all entries are removed. Shout if there are
96 WARN_ON(domain->revmap_tree.height);
98 list_del(&domain->link);
101 * If the going away domain is the default one, reset it.
103 if (unlikely(irq_default_domain == domain))
104 irq_set_default_host(NULL);
106 mutex_unlock(&irq_domain_mutex);
108 pr_debug("Removed domain %s\n", domain->name);
110 of_node_put(irq_domain_get_of_node(domain));
113 EXPORT_SYMBOL_GPL(irq_domain_remove);
116 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
117 * @of_node: pointer to interrupt controller's device tree node.
118 * @size: total number of irqs in mapping
119 * @first_irq: first number of irq block assigned to the domain,
120 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
121 * pre-map all of the irqs in the domain to virqs starting at first_irq.
122 * @ops: domain callbacks
123 * @host_data: Controller private data pointer
125 * Allocates an irq_domain, and optionally if first_irq is positive then also
126 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
128 * This is intended to implement the expected behaviour for most
129 * interrupt controllers. If device tree is used, then first_irq will be 0 and
130 * irqs get mapped dynamically on the fly. However, if the controller requires
131 * static virq assignments (non-DT boot) then it will set that up correctly.
133 struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
135 unsigned int first_irq,
136 const struct irq_domain_ops *ops,
139 struct irq_domain *domain;
141 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
146 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
147 /* attempt to allocated irq_descs */
148 int rc = irq_alloc_descs(first_irq, first_irq, size,
149 of_node_to_nid(of_node));
151 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
154 irq_domain_associate_many(domain, first_irq, 0, size);
159 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
162 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
163 * @of_node: pointer to interrupt controller's device tree node.
164 * @size: total number of irqs in legacy mapping
165 * @first_irq: first number of irq block assigned to the domain
166 * @first_hwirq: first hwirq number to use for the translation. Should normally
167 * be '0', but a positive integer can be used if the effective
168 * hwirqs numbering does not begin at zero.
169 * @ops: map/unmap domain callbacks
170 * @host_data: Controller private data pointer
172 * Note: the map() callback will be called before this function returns
173 * for all legacy interrupts except 0 (which is always the invalid irq for
174 * a legacy controller).
176 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
178 unsigned int first_irq,
179 irq_hw_number_t first_hwirq,
180 const struct irq_domain_ops *ops,
183 struct irq_domain *domain;
185 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
186 first_hwirq + size, 0, ops, host_data);
188 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
192 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
195 * irq_find_matching_fwnode() - Locates a domain for a given fwnode
196 * @fwnode: FW descriptor of the interrupt controller
197 * @bus_token: domain-specific data
199 struct irq_domain *irq_find_matching_fwnode(struct fwnode_handle *fwnode,
200 enum irq_domain_bus_token bus_token)
202 struct irq_domain *h, *found = NULL;
205 /* We might want to match the legacy controller last since
206 * it might potentially be set to match all interrupts in
207 * the absence of a device node. This isn't a problem so far
210 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
211 * values must generate an exact match for the domain to be
214 mutex_lock(&irq_domain_mutex);
215 list_for_each_entry(h, &irq_domain_list, link) {
217 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
219 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
220 ((bus_token == DOMAIN_BUS_ANY) ||
221 (h->bus_token == bus_token)));
228 mutex_unlock(&irq_domain_mutex);
231 EXPORT_SYMBOL_GPL(irq_find_matching_fwnode);
234 * irq_set_default_host() - Set a "default" irq domain
235 * @domain: default domain pointer
237 * For convenience, it's possible to set a "default" domain that will be used
238 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
239 * platforms that want to manipulate a few hard coded interrupt numbers that
240 * aren't properly represented in the device-tree.
242 void irq_set_default_host(struct irq_domain *domain)
244 pr_debug("Default domain set to @0x%p\n", domain);
246 irq_default_domain = domain;
248 EXPORT_SYMBOL_GPL(irq_set_default_host);
250 void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
252 struct irq_data *irq_data = irq_get_irq_data(irq);
253 irq_hw_number_t hwirq;
255 if (WARN(!irq_data || irq_data->domain != domain,
256 "virq%i doesn't exist; cannot disassociate\n", irq))
259 hwirq = irq_data->hwirq;
260 irq_set_status_flags(irq, IRQ_NOREQUEST);
262 /* remove chip and handler */
263 irq_set_chip_and_handler(irq, NULL, NULL);
265 /* Make sure it's completed */
266 synchronize_irq(irq);
268 /* Tell the PIC about it */
269 if (domain->ops->unmap)
270 domain->ops->unmap(domain, irq);
273 irq_data->domain = NULL;
276 /* Clear reverse map for this hwirq */
277 if (hwirq < domain->revmap_size) {
278 domain->linear_revmap[hwirq] = 0;
280 mutex_lock(&revmap_trees_mutex);
281 radix_tree_delete(&domain->revmap_tree, hwirq);
282 mutex_unlock(&revmap_trees_mutex);
286 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
287 irq_hw_number_t hwirq)
289 struct irq_data *irq_data = irq_get_irq_data(virq);
292 if (WARN(hwirq >= domain->hwirq_max,
293 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
295 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
297 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
300 mutex_lock(&irq_domain_mutex);
301 irq_data->hwirq = hwirq;
302 irq_data->domain = domain;
303 if (domain->ops->map) {
304 ret = domain->ops->map(domain, virq, hwirq);
307 * If map() returns -EPERM, this interrupt is protected
308 * by the firmware or some other service and shall not
309 * be mapped. Don't bother telling the user about it.
312 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
313 domain->name, hwirq, virq, ret);
315 irq_data->domain = NULL;
317 mutex_unlock(&irq_domain_mutex);
321 /* If not already assigned, give the domain the chip's name */
322 if (!domain->name && irq_data->chip)
323 domain->name = irq_data->chip->name;
326 if (hwirq < domain->revmap_size) {
327 domain->linear_revmap[hwirq] = virq;
329 mutex_lock(&revmap_trees_mutex);
330 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
331 mutex_unlock(&revmap_trees_mutex);
333 mutex_unlock(&irq_domain_mutex);
335 irq_clear_status_flags(virq, IRQ_NOREQUEST);
339 EXPORT_SYMBOL_GPL(irq_domain_associate);
341 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
342 irq_hw_number_t hwirq_base, int count)
344 struct device_node *of_node;
347 of_node = irq_domain_get_of_node(domain);
348 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
349 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
351 for (i = 0; i < count; i++) {
352 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
355 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
358 * irq_create_direct_mapping() - Allocate an irq for direct mapping
359 * @domain: domain to allocate the irq for or NULL for default domain
361 * This routine is used for irq controllers which can choose the hardware
362 * interrupt numbers they generate. In such a case it's simplest to use
363 * the linux irq as the hardware interrupt number. It still uses the linear
364 * or radix tree to store the mapping, but the irq controller can optimize
365 * the revmap path by using the hwirq directly.
367 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
369 struct device_node *of_node;
373 domain = irq_default_domain;
375 of_node = irq_domain_get_of_node(domain);
376 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
378 pr_debug("create_direct virq allocation failed\n");
381 if (virq >= domain->revmap_direct_max_irq) {
382 pr_err("ERROR: no free irqs available below %i maximum\n",
383 domain->revmap_direct_max_irq);
387 pr_debug("create_direct obtained virq %d\n", virq);
389 if (irq_domain_associate(domain, virq, virq)) {
396 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
399 * irq_create_mapping() - Map a hardware interrupt into linux irq space
400 * @domain: domain owning this hardware interrupt or NULL for default domain
401 * @hwirq: hardware irq number in that domain space
403 * Only one mapping per hardware interrupt is permitted. Returns a linux
405 * If the sense/trigger is to be specified, set_irq_type() should be called
406 * on the number returned from that call.
408 unsigned int irq_create_mapping(struct irq_domain *domain,
409 irq_hw_number_t hwirq)
411 struct device_node *of_node;
414 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
416 /* Look for default domain if nececssary */
418 domain = irq_default_domain;
419 if (domain == NULL) {
420 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
423 pr_debug("-> using domain @%p\n", domain);
425 of_node = irq_domain_get_of_node(domain);
427 /* Check if mapping already exists */
428 virq = irq_find_mapping(domain, hwirq);
430 pr_debug("-> existing mapping on virq %d\n", virq);
434 /* Allocate a virtual interrupt number */
435 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node));
437 pr_debug("-> virq allocation failed\n");
441 if (irq_domain_associate(domain, virq, hwirq)) {
446 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
447 hwirq, of_node_full_name(of_node), virq);
451 EXPORT_SYMBOL_GPL(irq_create_mapping);
454 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
455 * @domain: domain owning the interrupt range
456 * @irq_base: beginning of linux IRQ range
457 * @hwirq_base: beginning of hardware IRQ range
458 * @count: Number of interrupts to map
460 * This routine is used for allocating and mapping a range of hardware
461 * irqs to linux irqs where the linux irq numbers are at pre-defined
462 * locations. For use by controllers that already have static mappings
463 * to insert in to the domain.
465 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
468 * 0 is returned upon success, while any failure to establish a static
469 * mapping is treated as an error.
471 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
472 irq_hw_number_t hwirq_base, int count)
474 struct device_node *of_node;
477 of_node = irq_domain_get_of_node(domain);
478 ret = irq_alloc_descs(irq_base, irq_base, count,
479 of_node_to_nid(of_node));
480 if (unlikely(ret < 0))
483 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
486 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
488 static int irq_domain_translate(struct irq_domain *d,
489 struct irq_fwspec *fwspec,
490 irq_hw_number_t *hwirq, unsigned int *type)
492 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
493 if (d->ops->translate)
494 return d->ops->translate(d, fwspec, hwirq, type);
497 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
498 fwspec->param, fwspec->param_count,
501 /* If domain has no translation, then we assume interrupt line */
502 *hwirq = fwspec->param[0];
506 static void of_phandle_args_to_fwspec(struct of_phandle_args *irq_data,
507 struct irq_fwspec *fwspec)
511 fwspec->fwnode = irq_data->np ? &irq_data->np->fwnode : NULL;
512 fwspec->param_count = irq_data->args_count;
514 for (i = 0; i < irq_data->args_count; i++)
515 fwspec->param[i] = irq_data->args[i];
518 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
520 struct irq_domain *domain;
521 irq_hw_number_t hwirq;
522 unsigned int type = IRQ_TYPE_NONE;
526 domain = irq_find_matching_fwnode(fwspec->fwnode, DOMAIN_BUS_ANY);
528 domain = irq_default_domain;
531 pr_warn("no irq domain found for %s !\n",
532 of_node_full_name(to_of_node(fwspec->fwnode)));
536 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
539 if (irq_domain_is_hierarchy(domain)) {
541 * If we've already configured this interrupt,
542 * don't do it again, or hell will break loose.
544 virq = irq_find_mapping(domain, hwirq);
548 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
553 virq = irq_create_mapping(domain, hwirq);
558 /* Set type if specified and different than the current one */
559 if (type != IRQ_TYPE_NONE &&
560 type != irq_get_trigger_type(virq))
561 irq_set_irq_type(virq, type);
564 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
566 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
568 struct irq_fwspec fwspec;
570 of_phandle_args_to_fwspec(irq_data, &fwspec);
571 return irq_create_fwspec_mapping(&fwspec);
573 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
576 * irq_dispose_mapping() - Unmap an interrupt
577 * @virq: linux irq number of the interrupt to unmap
579 void irq_dispose_mapping(unsigned int virq)
581 struct irq_data *irq_data = irq_get_irq_data(virq);
582 struct irq_domain *domain;
584 if (!virq || !irq_data)
587 domain = irq_data->domain;
588 if (WARN_ON(domain == NULL))
591 irq_domain_disassociate(domain, virq);
594 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
597 * irq_find_mapping() - Find a linux irq from an hw irq number.
598 * @domain: domain owning this hardware interrupt
599 * @hwirq: hardware irq number in that domain space
601 unsigned int irq_find_mapping(struct irq_domain *domain,
602 irq_hw_number_t hwirq)
604 struct irq_data *data;
606 /* Look for default domain if nececssary */
608 domain = irq_default_domain;
612 if (hwirq < domain->revmap_direct_max_irq) {
613 data = irq_domain_get_irq_data(domain, hwirq);
614 if (data && data->hwirq == hwirq)
618 /* Check if the hwirq is in the linear revmap. */
619 if (hwirq < domain->revmap_size)
620 return domain->linear_revmap[hwirq];
623 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
625 return data ? data->irq : 0;
627 EXPORT_SYMBOL_GPL(irq_find_mapping);
629 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
630 static int virq_debug_show(struct seq_file *m, void *private)
633 struct irq_desc *desc;
634 struct irq_domain *domain;
635 struct radix_tree_iter iter;
639 seq_printf(m, " %-16s %-6s %-10s %-10s %s\n",
640 "name", "mapped", "linear-max", "direct-max", "devtree-node");
641 mutex_lock(&irq_domain_mutex);
642 list_for_each_entry(domain, &irq_domain_list, link) {
643 struct device_node *of_node;
645 of_node = irq_domain_get_of_node(domain);
646 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
648 seq_printf(m, "%c%-16s %6u %10u %10u %s\n",
649 domain == irq_default_domain ? '*' : ' ', domain->name,
650 domain->revmap_size + count, domain->revmap_size,
651 domain->revmap_direct_max_irq,
652 of_node ? of_node_full_name(of_node) : "");
654 mutex_unlock(&irq_domain_mutex);
656 seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq",
657 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
658 "active", "type", "domain");
660 for (i = 1; i < nr_irqs; i++) {
661 desc = irq_to_desc(i);
665 raw_spin_lock_irqsave(&desc->lock, flags);
666 domain = desc->irq_data.domain;
669 struct irq_chip *chip;
670 int hwirq = desc->irq_data.hwirq;
673 seq_printf(m, "%5d ", i);
674 seq_printf(m, "0x%05x ", hwirq);
676 chip = irq_desc_get_chip(desc);
677 seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
679 data = irq_desc_get_chip_data(desc);
680 seq_printf(m, data ? "0x%p " : " %p ", data);
682 seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' ');
683 direct = (i == hwirq) && (i < domain->revmap_direct_max_irq);
684 seq_printf(m, "%6s%-8s ",
685 (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
686 direct ? "(DIRECT)" : "");
687 seq_printf(m, "%s\n", desc->irq_data.domain->name);
690 raw_spin_unlock_irqrestore(&desc->lock, flags);
696 static int virq_debug_open(struct inode *inode, struct file *file)
698 return single_open(file, virq_debug_show, inode->i_private);
701 static const struct file_operations virq_debug_fops = {
702 .open = virq_debug_open,
705 .release = single_release,
708 static int __init irq_debugfs_init(void)
710 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
711 NULL, &virq_debug_fops) == NULL)
716 __initcall(irq_debugfs_init);
717 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
720 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
722 * Device Tree IRQ specifier translation function which works with one cell
723 * bindings where the cell value maps directly to the hwirq number.
725 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
726 const u32 *intspec, unsigned int intsize,
727 unsigned long *out_hwirq, unsigned int *out_type)
729 if (WARN_ON(intsize < 1))
731 *out_hwirq = intspec[0];
732 *out_type = IRQ_TYPE_NONE;
735 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
738 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
740 * Device Tree IRQ specifier translation function which works with two cell
741 * bindings where the cell values map directly to the hwirq number
742 * and linux irq flags.
744 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
745 const u32 *intspec, unsigned int intsize,
746 irq_hw_number_t *out_hwirq, unsigned int *out_type)
748 if (WARN_ON(intsize < 2))
750 *out_hwirq = intspec[0];
751 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
754 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
757 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
759 * Device Tree IRQ specifier translation function which works with either one
760 * or two cell bindings where the cell values map directly to the hwirq number
761 * and linux irq flags.
763 * Note: don't use this function unless your interrupt controller explicitly
764 * supports both one and two cell bindings. For the majority of controllers
765 * the _onecell() or _twocell() variants above should be used.
767 int irq_domain_xlate_onetwocell(struct irq_domain *d,
768 struct device_node *ctrlr,
769 const u32 *intspec, unsigned int intsize,
770 unsigned long *out_hwirq, unsigned int *out_type)
772 if (WARN_ON(intsize < 1))
774 *out_hwirq = intspec[0];
775 *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
778 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
780 const struct irq_domain_ops irq_domain_simple_ops = {
781 .xlate = irq_domain_xlate_onetwocell,
783 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
785 static int irq_domain_alloc_descs(int virq, unsigned int cnt,
786 irq_hw_number_t hwirq, int node)
791 virq = irq_alloc_descs(virq, virq, cnt, node);
793 hint = hwirq % nr_irqs;
796 virq = irq_alloc_descs_from(hint, cnt, node);
797 if (virq <= 0 && hint > 1)
798 virq = irq_alloc_descs_from(1, cnt, node);
804 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
806 * irq_domain_add_hierarchy - Add a irqdomain into the hierarchy
807 * @parent: Parent irq domain to associate with the new domain
808 * @flags: Irq domain flags associated to the domain
809 * @size: Size of the domain. See below
810 * @node: Optional device-tree node of the interrupt controller
811 * @ops: Pointer to the interrupt domain callbacks
812 * @host_data: Controller private data pointer
814 * If @size is 0 a tree domain is created, otherwise a linear domain.
816 * If successful the parent is associated to the new domain and the
817 * domain flags are set.
818 * Returns pointer to IRQ domain, or NULL on failure.
820 struct irq_domain *irq_domain_add_hierarchy(struct irq_domain *parent,
823 struct device_node *node,
824 const struct irq_domain_ops *ops,
827 struct irq_domain *domain;
830 domain = irq_domain_add_linear(node, size, ops, host_data);
832 domain = irq_domain_add_tree(node, ops, host_data);
834 domain->parent = parent;
835 domain->flags |= flags;
841 static void irq_domain_insert_irq(int virq)
843 struct irq_data *data;
845 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
846 struct irq_domain *domain = data->domain;
847 irq_hw_number_t hwirq = data->hwirq;
849 if (hwirq < domain->revmap_size) {
850 domain->linear_revmap[hwirq] = virq;
852 mutex_lock(&revmap_trees_mutex);
853 radix_tree_insert(&domain->revmap_tree, hwirq, data);
854 mutex_unlock(&revmap_trees_mutex);
857 /* If not already assigned, give the domain the chip's name */
858 if (!domain->name && data->chip)
859 domain->name = data->chip->name;
862 irq_clear_status_flags(virq, IRQ_NOREQUEST);
865 static void irq_domain_remove_irq(int virq)
867 struct irq_data *data;
869 irq_set_status_flags(virq, IRQ_NOREQUEST);
870 irq_set_chip_and_handler(virq, NULL, NULL);
871 synchronize_irq(virq);
874 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
875 struct irq_domain *domain = data->domain;
876 irq_hw_number_t hwirq = data->hwirq;
878 if (hwirq < domain->revmap_size) {
879 domain->linear_revmap[hwirq] = 0;
881 mutex_lock(&revmap_trees_mutex);
882 radix_tree_delete(&domain->revmap_tree, hwirq);
883 mutex_unlock(&revmap_trees_mutex);
888 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
889 struct irq_data *child)
891 struct irq_data *irq_data;
893 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
894 irq_data_get_node(child));
896 child->parent_data = irq_data;
897 irq_data->irq = child->irq;
898 irq_data->common = child->common;
899 irq_data->domain = domain;
905 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
907 struct irq_data *irq_data, *tmp;
910 for (i = 0; i < nr_irqs; i++) {
911 irq_data = irq_get_irq_data(virq + i);
912 tmp = irq_data->parent_data;
913 irq_data->parent_data = NULL;
914 irq_data->domain = NULL;
918 tmp = tmp->parent_data;
924 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
925 unsigned int virq, unsigned int nr_irqs)
927 struct irq_data *irq_data;
928 struct irq_domain *parent;
931 /* The outermost irq_data is embedded in struct irq_desc */
932 for (i = 0; i < nr_irqs; i++) {
933 irq_data = irq_get_irq_data(virq + i);
934 irq_data->domain = domain;
936 for (parent = domain->parent; parent; parent = parent->parent) {
937 irq_data = irq_domain_insert_irq_data(parent, irq_data);
939 irq_domain_free_irq_data(virq, i + 1);
949 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
950 * @domain: domain to match
951 * @virq: IRQ number to get irq_data
953 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
956 struct irq_data *irq_data;
958 for (irq_data = irq_get_irq_data(virq); irq_data;
959 irq_data = irq_data->parent_data)
960 if (irq_data->domain == domain)
967 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
968 * @domain: Interrupt domain to match
970 * @hwirq: The hwirq number
971 * @chip: The associated interrupt chip
972 * @chip_data: The associated chip data
974 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
975 irq_hw_number_t hwirq, struct irq_chip *chip,
978 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
983 irq_data->hwirq = hwirq;
984 irq_data->chip = chip ? chip : &no_irq_chip;
985 irq_data->chip_data = chip_data;
991 * irq_domain_set_info - Set the complete data for a @virq in @domain
992 * @domain: Interrupt domain to match
994 * @hwirq: The hardware interrupt number
995 * @chip: The associated interrupt chip
996 * @chip_data: The associated interrupt chip data
997 * @handler: The interrupt flow handler
998 * @handler_data: The interrupt flow handler data
999 * @handler_name: The interrupt handler name
1001 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1002 irq_hw_number_t hwirq, struct irq_chip *chip,
1003 void *chip_data, irq_flow_handler_t handler,
1004 void *handler_data, const char *handler_name)
1006 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1007 __irq_set_handler(virq, handler, 0, handler_name);
1008 irq_set_handler_data(virq, handler_data);
1012 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1013 * @irq_data: The pointer to irq_data
1015 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1017 irq_data->hwirq = 0;
1018 irq_data->chip = &no_irq_chip;
1019 irq_data->chip_data = NULL;
1023 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1024 * @domain: Interrupt domain to match
1025 * @virq: IRQ number to start with
1026 * @nr_irqs: The number of irqs to free
1028 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1029 unsigned int nr_irqs)
1031 struct irq_data *irq_data;
1034 for (i = 0; i < nr_irqs; i++) {
1035 irq_data = irq_domain_get_irq_data(domain, virq + i);
1037 irq_domain_reset_irq_data(irq_data);
1039 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1043 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1044 * @domain: Interrupt domain to match
1045 * @virq: IRQ number to start with
1046 * @nr_irqs: The number of irqs to free
1048 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1049 unsigned int nr_irqs)
1053 for (i = 0; i < nr_irqs; i++) {
1054 irq_set_handler_data(virq + i, NULL);
1055 irq_set_handler(virq + i, NULL);
1057 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1060 static bool irq_domain_is_auto_recursive(struct irq_domain *domain)
1062 return domain->flags & IRQ_DOMAIN_FLAG_AUTO_RECURSIVE;
1065 static void irq_domain_free_irqs_recursive(struct irq_domain *domain,
1066 unsigned int irq_base,
1067 unsigned int nr_irqs)
1069 domain->ops->free(domain, irq_base, nr_irqs);
1070 if (irq_domain_is_auto_recursive(domain)) {
1071 BUG_ON(!domain->parent);
1072 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1077 static int irq_domain_alloc_irqs_recursive(struct irq_domain *domain,
1078 unsigned int irq_base,
1079 unsigned int nr_irqs, void *arg)
1082 struct irq_domain *parent = domain->parent;
1083 bool recursive = irq_domain_is_auto_recursive(domain);
1085 BUG_ON(recursive && !parent);
1087 ret = irq_domain_alloc_irqs_recursive(parent, irq_base,
1090 ret = domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1091 if (ret < 0 && recursive)
1092 irq_domain_free_irqs_recursive(parent, irq_base, nr_irqs);
1098 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1099 * @domain: domain to allocate from
1100 * @irq_base: allocate specified IRQ nubmer if irq_base >= 0
1101 * @nr_irqs: number of IRQs to allocate
1102 * @node: NUMA node id for memory allocation
1103 * @arg: domain specific argument
1104 * @realloc: IRQ descriptors have already been allocated if true
1106 * Allocate IRQ numbers and initialized all data structures to support
1107 * hierarchy IRQ domains.
1108 * Parameter @realloc is mainly to support legacy IRQs.
1109 * Returns error code or allocated IRQ number
1111 * The whole process to setup an IRQ has been split into two steps.
1112 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1113 * descriptor and required hardware resources. The second step,
1114 * irq_domain_activate_irq(), is to program hardwares with preallocated
1115 * resources. In this way, it's easier to rollback when failing to
1116 * allocate resources.
1118 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1119 unsigned int nr_irqs, int node, void *arg,
1124 if (domain == NULL) {
1125 domain = irq_default_domain;
1126 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1130 if (!domain->ops->alloc) {
1131 pr_debug("domain->ops->alloc() is NULL\n");
1135 if (realloc && irq_base >= 0) {
1138 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node);
1140 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1146 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1147 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1152 mutex_lock(&irq_domain_mutex);
1153 ret = irq_domain_alloc_irqs_recursive(domain, virq, nr_irqs, arg);
1155 mutex_unlock(&irq_domain_mutex);
1156 goto out_free_irq_data;
1158 for (i = 0; i < nr_irqs; i++)
1159 irq_domain_insert_irq(virq + i);
1160 mutex_unlock(&irq_domain_mutex);
1165 irq_domain_free_irq_data(virq, nr_irqs);
1167 irq_free_descs(virq, nr_irqs);
1172 * irq_domain_free_irqs - Free IRQ number and associated data structures
1173 * @virq: base IRQ number
1174 * @nr_irqs: number of IRQs to free
1176 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1178 struct irq_data *data = irq_get_irq_data(virq);
1181 if (WARN(!data || !data->domain || !data->domain->ops->free,
1182 "NULL pointer, cannot free irq\n"))
1185 mutex_lock(&irq_domain_mutex);
1186 for (i = 0; i < nr_irqs; i++)
1187 irq_domain_remove_irq(virq + i);
1188 irq_domain_free_irqs_recursive(data->domain, virq, nr_irqs);
1189 mutex_unlock(&irq_domain_mutex);
1191 irq_domain_free_irq_data(virq, nr_irqs);
1192 irq_free_descs(virq, nr_irqs);
1196 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1197 * @irq_base: Base IRQ number
1198 * @nr_irqs: Number of IRQs to allocate
1199 * @arg: Allocation data (arch/domain specific)
1201 * Check whether the domain has been setup recursive. If not allocate
1202 * through the parent domain.
1204 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1205 unsigned int irq_base, unsigned int nr_irqs,
1208 /* irq_domain_alloc_irqs_recursive() has called parent's alloc() */
1209 if (irq_domain_is_auto_recursive(domain))
1212 domain = domain->parent;
1214 return irq_domain_alloc_irqs_recursive(domain, irq_base,
1220 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1221 * @irq_base: Base IRQ number
1222 * @nr_irqs: Number of IRQs to free
1224 * Check whether the domain has been setup recursive. If not free
1225 * through the parent domain.
1227 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1228 unsigned int irq_base, unsigned int nr_irqs)
1230 /* irq_domain_free_irqs_recursive() will call parent's free */
1231 if (!irq_domain_is_auto_recursive(domain) && domain->parent)
1232 irq_domain_free_irqs_recursive(domain->parent, irq_base,
1237 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1239 * @irq_data: outermost irq_data associated with interrupt
1241 * This is the second step to call domain_ops->activate to program interrupt
1242 * controllers, so the interrupt could actually get delivered.
1244 void irq_domain_activate_irq(struct irq_data *irq_data)
1246 if (irq_data && irq_data->domain) {
1247 struct irq_domain *domain = irq_data->domain;
1249 if (irq_data->parent_data)
1250 irq_domain_activate_irq(irq_data->parent_data);
1251 if (domain->ops->activate)
1252 domain->ops->activate(domain, irq_data);
1257 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1258 * deactivate interrupt
1259 * @irq_data: outermost irq_data associated with interrupt
1261 * It calls domain_ops->deactivate to program interrupt controllers to disable
1262 * interrupt delivery.
1264 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1266 if (irq_data && irq_data->domain) {
1267 struct irq_domain *domain = irq_data->domain;
1269 if (domain->ops->deactivate)
1270 domain->ops->deactivate(domain, irq_data);
1271 if (irq_data->parent_data)
1272 irq_domain_deactivate_irq(irq_data->parent_data);
1276 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1278 /* Hierarchy irq_domains must implement callback alloc() */
1279 if (domain->ops->alloc)
1280 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1282 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1284 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1285 * @domain: domain to match
1286 * @virq: IRQ number to get irq_data
1288 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1291 struct irq_data *irq_data = irq_get_irq_data(virq);
1293 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1297 * irq_domain_set_info - Set the complete data for a @virq in @domain
1298 * @domain: Interrupt domain to match
1300 * @hwirq: The hardware interrupt number
1301 * @chip: The associated interrupt chip
1302 * @chip_data: The associated interrupt chip data
1303 * @handler: The interrupt flow handler
1304 * @handler_data: The interrupt flow handler data
1305 * @handler_name: The interrupt handler name
1307 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1308 irq_hw_number_t hwirq, struct irq_chip *chip,
1309 void *chip_data, irq_flow_handler_t handler,
1310 void *handler_data, const char *handler_name)
1312 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1313 irq_set_chip_data(virq, chip_data);
1314 irq_set_handler_data(virq, handler_data);
1317 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1320 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */