1 // SPDX-License-Identifier: GPL-2.0
3 #define pr_fmt(fmt) "irq: " fmt
5 #include <linux/acpi.h>
6 #include <linux/debugfs.h>
7 #include <linux/hardirq.h>
8 #include <linux/interrupt.h>
10 #include <linux/irqdesc.h>
11 #include <linux/irqdomain.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/topology.h>
18 #include <linux/seq_file.h>
19 #include <linux/slab.h>
20 #include <linux/smp.h>
23 static LIST_HEAD(irq_domain_list);
24 static DEFINE_MUTEX(irq_domain_mutex);
26 static struct irq_domain *irq_default_domain;
28 static void irq_domain_check_hierarchy(struct irq_domain *domain);
31 struct fwnode_handle fwnode;
37 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
38 static void debugfs_add_domain_dir(struct irq_domain *d);
39 static void debugfs_remove_domain_dir(struct irq_domain *d);
41 static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
42 static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
45 static const char *irqchip_fwnode_get_name(const struct fwnode_handle *fwnode)
47 struct irqchip_fwid *fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
52 const struct fwnode_operations irqchip_fwnode_ops = {
53 .get_name = irqchip_fwnode_get_name,
55 EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
58 * __irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
59 * identifying an irq domain
60 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
61 * @id: Optional user provided id if name != NULL
62 * @name: Optional user provided domain name
63 * @pa: Optional user-provided physical address
65 * Allocate a struct irqchip_fwid, and return a pointer to the embedded
66 * fwnode_handle (or NULL on failure).
68 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
69 * solely to transport name information to irqdomain creation code. The
70 * node is not stored. For other types the pointer is kept in the irq
73 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
77 struct irqchip_fwid *fwid;
80 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
83 case IRQCHIP_FWNODE_NAMED:
84 n = kasprintf(GFP_KERNEL, "%s", name);
86 case IRQCHIP_FWNODE_NAMED_ID:
87 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
90 n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
103 fwnode_init(&fwid->fwnode, &irqchip_fwnode_ops);
104 return &fwid->fwnode;
106 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
109 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
111 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
113 void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
115 struct irqchip_fwid *fwid;
117 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
120 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
124 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
127 * __irq_domain_add() - Allocate a new irq_domain data structure
128 * @fwnode: firmware node for the interrupt controller
129 * @size: Size of linear map; 0 for radix mapping only
130 * @hwirq_max: Maximum number of interrupts supported by controller
131 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
133 * @ops: domain callbacks
134 * @host_data: Controller private data pointer
136 * Allocates and initializes an irq_domain structure.
137 * Returns pointer to IRQ domain, or NULL on failure.
139 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
140 irq_hw_number_t hwirq_max, int direct_max,
141 const struct irq_domain_ops *ops,
144 struct irqchip_fwid *fwid;
145 struct irq_domain *domain;
147 static atomic_t unknown_domains;
149 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
150 GFP_KERNEL, of_node_to_nid(to_of_node(fwnode)));
154 if (is_fwnode_irqchip(fwnode)) {
155 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
157 switch (fwid->type) {
158 case IRQCHIP_FWNODE_NAMED:
159 case IRQCHIP_FWNODE_NAMED_ID:
160 domain->fwnode = fwnode;
161 domain->name = kstrdup(fwid->name, GFP_KERNEL);
166 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
169 domain->fwnode = fwnode;
170 domain->name = fwid->name;
173 } else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
174 is_software_node(fwnode)) {
178 * fwnode paths contain '/', which debugfs is legitimately
179 * unhappy about. Replace them with ':', which does
180 * the trick and is not as offensive as '\'...
182 name = kasprintf(GFP_KERNEL, "%pfw", fwnode);
188 strreplace(name, '/', ':');
191 domain->fwnode = fwnode;
192 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
197 pr_err("Invalid fwnode type for irqdomain\n");
198 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
199 atomic_inc_return(&unknown_domains));
204 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
207 fwnode_handle_get(fwnode);
208 fwnode_dev_initialized(fwnode, true);
211 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
212 mutex_init(&domain->revmap_tree_mutex);
214 domain->host_data = host_data;
215 domain->hwirq_max = hwirq_max;
216 domain->revmap_size = size;
217 domain->revmap_direct_max_irq = direct_max;
218 irq_domain_check_hierarchy(domain);
220 mutex_lock(&irq_domain_mutex);
221 debugfs_add_domain_dir(domain);
222 list_add(&domain->link, &irq_domain_list);
223 mutex_unlock(&irq_domain_mutex);
225 pr_debug("Added domain %s\n", domain->name);
228 EXPORT_SYMBOL_GPL(__irq_domain_add);
231 * irq_domain_remove() - Remove an irq domain.
232 * @domain: domain to remove
234 * This routine is used to remove an irq domain. The caller must ensure
235 * that all mappings within the domain have been disposed of prior to
236 * use, depending on the revmap type.
238 void irq_domain_remove(struct irq_domain *domain)
240 mutex_lock(&irq_domain_mutex);
241 debugfs_remove_domain_dir(domain);
243 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
245 list_del(&domain->link);
248 * If the going away domain is the default one, reset it.
250 if (unlikely(irq_default_domain == domain))
251 irq_set_default_host(NULL);
253 mutex_unlock(&irq_domain_mutex);
255 pr_debug("Removed domain %s\n", domain->name);
257 fwnode_dev_initialized(domain->fwnode, false);
258 fwnode_handle_put(domain->fwnode);
259 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
263 EXPORT_SYMBOL_GPL(irq_domain_remove);
265 void irq_domain_update_bus_token(struct irq_domain *domain,
266 enum irq_domain_bus_token bus_token)
270 if (domain->bus_token == bus_token)
273 mutex_lock(&irq_domain_mutex);
275 domain->bus_token = bus_token;
277 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
279 mutex_unlock(&irq_domain_mutex);
283 debugfs_remove_domain_dir(domain);
285 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
288 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
291 debugfs_add_domain_dir(domain);
293 mutex_unlock(&irq_domain_mutex);
295 EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
298 * irq_domain_create_simple() - Register an irq_domain and optionally map a range of irqs
299 * @fwnode: firmware node for the interrupt controller
300 * @size: total number of irqs in mapping
301 * @first_irq: first number of irq block assigned to the domain,
302 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
303 * pre-map all of the irqs in the domain to virqs starting at first_irq.
304 * @ops: domain callbacks
305 * @host_data: Controller private data pointer
307 * Allocates an irq_domain, and optionally if first_irq is positive then also
308 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
310 * This is intended to implement the expected behaviour for most
311 * interrupt controllers. If device tree is used, then first_irq will be 0 and
312 * irqs get mapped dynamically on the fly. However, if the controller requires
313 * static virq assignments (non-DT boot) then it will set that up correctly.
315 struct irq_domain *irq_domain_create_simple(struct fwnode_handle *fwnode,
317 unsigned int first_irq,
318 const struct irq_domain_ops *ops,
321 struct irq_domain *domain;
323 domain = __irq_domain_add(fwnode, size, size, 0, ops, host_data);
328 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
329 /* attempt to allocated irq_descs */
330 int rc = irq_alloc_descs(first_irq, first_irq, size,
331 of_node_to_nid(to_of_node(fwnode)));
333 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
336 irq_domain_associate_many(domain, first_irq, 0, size);
341 EXPORT_SYMBOL_GPL(irq_domain_create_simple);
344 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
345 * @of_node: pointer to interrupt controller's device tree node.
346 * @size: total number of irqs in legacy mapping
347 * @first_irq: first number of irq block assigned to the domain
348 * @first_hwirq: first hwirq number to use for the translation. Should normally
349 * be '0', but a positive integer can be used if the effective
350 * hwirqs numbering does not begin at zero.
351 * @ops: map/unmap domain callbacks
352 * @host_data: Controller private data pointer
354 * Note: the map() callback will be called before this function returns
355 * for all legacy interrupts except 0 (which is always the invalid irq for
356 * a legacy controller).
358 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
360 unsigned int first_irq,
361 irq_hw_number_t first_hwirq,
362 const struct irq_domain_ops *ops,
365 return irq_domain_create_legacy(of_node_to_fwnode(of_node), size,
366 first_irq, first_hwirq, ops, host_data);
368 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
370 struct irq_domain *irq_domain_create_legacy(struct fwnode_handle *fwnode,
372 unsigned int first_irq,
373 irq_hw_number_t first_hwirq,
374 const struct irq_domain_ops *ops,
377 struct irq_domain *domain;
379 domain = __irq_domain_add(fwnode, first_hwirq + size, first_hwirq + size, 0, ops, host_data);
381 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
385 EXPORT_SYMBOL_GPL(irq_domain_create_legacy);
388 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
389 * @fwspec: FW specifier for an interrupt
390 * @bus_token: domain-specific data
392 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
393 enum irq_domain_bus_token bus_token)
395 struct irq_domain *h, *found = NULL;
396 struct fwnode_handle *fwnode = fwspec->fwnode;
399 /* We might want to match the legacy controller last since
400 * it might potentially be set to match all interrupts in
401 * the absence of a device node. This isn't a problem so far
404 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
405 * values must generate an exact match for the domain to be
408 mutex_lock(&irq_domain_mutex);
409 list_for_each_entry(h, &irq_domain_list, link) {
410 if (h->ops->select && fwspec->param_count)
411 rc = h->ops->select(h, fwspec, bus_token);
412 else if (h->ops->match)
413 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
415 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
416 ((bus_token == DOMAIN_BUS_ANY) ||
417 (h->bus_token == bus_token)));
424 mutex_unlock(&irq_domain_mutex);
427 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
430 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
433 * Return: false if any MSI irq domain does not support IRQ remapping,
434 * true otherwise (including if there is no MSI irq domain)
436 bool irq_domain_check_msi_remap(void)
438 struct irq_domain *h;
441 mutex_lock(&irq_domain_mutex);
442 list_for_each_entry(h, &irq_domain_list, link) {
443 if (irq_domain_is_msi(h) &&
444 !irq_domain_hierarchical_is_msi_remap(h)) {
449 mutex_unlock(&irq_domain_mutex);
452 EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
455 * irq_set_default_host() - Set a "default" irq domain
456 * @domain: default domain pointer
458 * For convenience, it's possible to set a "default" domain that will be used
459 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
460 * platforms that want to manipulate a few hard coded interrupt numbers that
461 * aren't properly represented in the device-tree.
463 void irq_set_default_host(struct irq_domain *domain)
465 pr_debug("Default domain set to @0x%p\n", domain);
467 irq_default_domain = domain;
469 EXPORT_SYMBOL_GPL(irq_set_default_host);
472 * irq_get_default_host() - Retrieve the "default" irq domain
474 * Returns: the default domain, if any.
476 * Modern code should never use this. This should only be used on
477 * systems that cannot implement a firmware->fwnode mapping (which
478 * both DT and ACPI provide).
480 struct irq_domain *irq_get_default_host(void)
482 return irq_default_domain;
485 static void irq_domain_clear_mapping(struct irq_domain *domain,
486 irq_hw_number_t hwirq)
488 if (hwirq < domain->revmap_size) {
489 domain->linear_revmap[hwirq] = 0;
491 mutex_lock(&domain->revmap_tree_mutex);
492 radix_tree_delete(&domain->revmap_tree, hwirq);
493 mutex_unlock(&domain->revmap_tree_mutex);
497 static void irq_domain_set_mapping(struct irq_domain *domain,
498 irq_hw_number_t hwirq,
499 struct irq_data *irq_data)
501 if (hwirq < domain->revmap_size) {
502 domain->linear_revmap[hwirq] = irq_data->irq;
504 mutex_lock(&domain->revmap_tree_mutex);
505 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
506 mutex_unlock(&domain->revmap_tree_mutex);
510 static void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
512 struct irq_data *irq_data = irq_get_irq_data(irq);
513 irq_hw_number_t hwirq;
515 if (WARN(!irq_data || irq_data->domain != domain,
516 "virq%i doesn't exist; cannot disassociate\n", irq))
519 hwirq = irq_data->hwirq;
520 irq_set_status_flags(irq, IRQ_NOREQUEST);
522 /* remove chip and handler */
523 irq_set_chip_and_handler(irq, NULL, NULL);
525 /* Make sure it's completed */
526 synchronize_irq(irq);
528 /* Tell the PIC about it */
529 if (domain->ops->unmap)
530 domain->ops->unmap(domain, irq);
533 irq_data->domain = NULL;
537 /* Clear reverse map for this hwirq */
538 irq_domain_clear_mapping(domain, hwirq);
541 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
542 irq_hw_number_t hwirq)
544 struct irq_data *irq_data = irq_get_irq_data(virq);
547 if (WARN(hwirq >= domain->hwirq_max,
548 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
550 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
552 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
555 mutex_lock(&irq_domain_mutex);
556 irq_data->hwirq = hwirq;
557 irq_data->domain = domain;
558 if (domain->ops->map) {
559 ret = domain->ops->map(domain, virq, hwirq);
562 * If map() returns -EPERM, this interrupt is protected
563 * by the firmware or some other service and shall not
564 * be mapped. Don't bother telling the user about it.
567 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
568 domain->name, hwirq, virq, ret);
570 irq_data->domain = NULL;
572 mutex_unlock(&irq_domain_mutex);
576 /* If not already assigned, give the domain the chip's name */
577 if (!domain->name && irq_data->chip)
578 domain->name = irq_data->chip->name;
582 irq_domain_set_mapping(domain, hwirq, irq_data);
583 mutex_unlock(&irq_domain_mutex);
585 irq_clear_status_flags(virq, IRQ_NOREQUEST);
589 EXPORT_SYMBOL_GPL(irq_domain_associate);
591 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
592 irq_hw_number_t hwirq_base, int count)
594 struct device_node *of_node;
597 of_node = irq_domain_get_of_node(domain);
598 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
599 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
601 for (i = 0; i < count; i++) {
602 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
605 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
608 * irq_create_direct_mapping() - Allocate an irq for direct mapping
609 * @domain: domain to allocate the irq for or NULL for default domain
611 * This routine is used for irq controllers which can choose the hardware
612 * interrupt numbers they generate. In such a case it's simplest to use
613 * the linux irq as the hardware interrupt number. It still uses the linear
614 * or radix tree to store the mapping, but the irq controller can optimize
615 * the revmap path by using the hwirq directly.
617 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
619 struct device_node *of_node;
623 domain = irq_default_domain;
625 of_node = irq_domain_get_of_node(domain);
626 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
628 pr_debug("create_direct virq allocation failed\n");
631 if (virq >= domain->revmap_direct_max_irq) {
632 pr_err("ERROR: no free irqs available below %i maximum\n",
633 domain->revmap_direct_max_irq);
637 pr_debug("create_direct obtained virq %d\n", virq);
639 if (irq_domain_associate(domain, virq, virq)) {
646 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
649 * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
650 * @domain: domain owning this hardware interrupt or NULL for default domain
651 * @hwirq: hardware irq number in that domain space
652 * @affinity: irq affinity
654 * Only one mapping per hardware interrupt is permitted. Returns a linux
656 * If the sense/trigger is to be specified, set_irq_type() should be called
657 * on the number returned from that call.
659 unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
660 irq_hw_number_t hwirq,
661 const struct irq_affinity_desc *affinity)
663 struct device_node *of_node;
666 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
668 /* Look for default domain if necessary */
670 domain = irq_default_domain;
671 if (domain == NULL) {
672 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
675 pr_debug("-> using domain @%p\n", domain);
677 of_node = irq_domain_get_of_node(domain);
679 /* Check if mapping already exists */
680 virq = irq_find_mapping(domain, hwirq);
682 pr_debug("-> existing mapping on virq %d\n", virq);
686 /* Allocate a virtual interrupt number */
687 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
690 pr_debug("-> virq allocation failed\n");
694 if (irq_domain_associate(domain, virq, hwirq)) {
699 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
700 hwirq, of_node_full_name(of_node), virq);
704 EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
706 static int irq_domain_translate(struct irq_domain *d,
707 struct irq_fwspec *fwspec,
708 irq_hw_number_t *hwirq, unsigned int *type)
710 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
711 if (d->ops->translate)
712 return d->ops->translate(d, fwspec, hwirq, type);
715 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
716 fwspec->param, fwspec->param_count,
719 /* If domain has no translation, then we assume interrupt line */
720 *hwirq = fwspec->param[0];
724 static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
726 struct irq_fwspec *fwspec)
730 fwspec->fwnode = of_node_to_fwnode(np);
731 fwspec->param_count = count;
733 for (i = 0; i < count; i++)
734 fwspec->param[i] = args[i];
737 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
739 struct irq_domain *domain;
740 struct irq_data *irq_data;
741 irq_hw_number_t hwirq;
742 unsigned int type = IRQ_TYPE_NONE;
745 if (fwspec->fwnode) {
746 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
748 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
750 domain = irq_default_domain;
754 pr_warn("no irq domain found for %s !\n",
755 of_node_full_name(to_of_node(fwspec->fwnode)));
759 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
763 * WARN if the irqchip returns a type with bits
764 * outside the sense mask set and clear these bits.
766 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
767 type &= IRQ_TYPE_SENSE_MASK;
770 * If we've already configured this interrupt,
771 * don't do it again, or hell will break loose.
773 virq = irq_find_mapping(domain, hwirq);
776 * If the trigger type is not specified or matches the
777 * current trigger type then we are done so return the
780 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
784 * If the trigger type has not been set yet, then set
785 * it now and return the interrupt number.
787 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
788 irq_data = irq_get_irq_data(virq);
792 irqd_set_trigger_type(irq_data, type);
796 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
797 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
801 if (irq_domain_is_hierarchy(domain)) {
802 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
807 virq = irq_create_mapping(domain, hwirq);
812 irq_data = irq_get_irq_data(virq);
814 if (irq_domain_is_hierarchy(domain))
815 irq_domain_free_irqs(virq, 1);
817 irq_dispose_mapping(virq);
821 /* Store trigger type */
822 irqd_set_trigger_type(irq_data, type);
826 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
828 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
830 struct irq_fwspec fwspec;
832 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
833 irq_data->args_count, &fwspec);
835 return irq_create_fwspec_mapping(&fwspec);
837 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
840 * irq_dispose_mapping() - Unmap an interrupt
841 * @virq: linux irq number of the interrupt to unmap
843 void irq_dispose_mapping(unsigned int virq)
845 struct irq_data *irq_data = irq_get_irq_data(virq);
846 struct irq_domain *domain;
848 if (!virq || !irq_data)
851 domain = irq_data->domain;
852 if (WARN_ON(domain == NULL))
855 if (irq_domain_is_hierarchy(domain)) {
856 irq_domain_free_irqs(virq, 1);
858 irq_domain_disassociate(domain, virq);
862 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
865 * irq_find_mapping() - Find a linux irq from a hw irq number.
866 * @domain: domain owning this hardware interrupt
867 * @hwirq: hardware irq number in that domain space
869 unsigned int irq_find_mapping(struct irq_domain *domain,
870 irq_hw_number_t hwirq)
872 struct irq_data *data;
874 /* Look for default domain if necessary */
876 domain = irq_default_domain;
880 if (hwirq < domain->revmap_direct_max_irq) {
881 data = irq_domain_get_irq_data(domain, hwirq);
882 if (data && data->hwirq == hwirq)
886 /* Check if the hwirq is in the linear revmap. */
887 if (hwirq < domain->revmap_size)
888 return domain->linear_revmap[hwirq];
891 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
893 return data ? data->irq : 0;
895 EXPORT_SYMBOL_GPL(irq_find_mapping);
898 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
900 * Device Tree IRQ specifier translation function which works with one cell
901 * bindings where the cell value maps directly to the hwirq number.
903 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
904 const u32 *intspec, unsigned int intsize,
905 unsigned long *out_hwirq, unsigned int *out_type)
907 if (WARN_ON(intsize < 1))
909 *out_hwirq = intspec[0];
910 *out_type = IRQ_TYPE_NONE;
913 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
916 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
918 * Device Tree IRQ specifier translation function which works with two cell
919 * bindings where the cell values map directly to the hwirq number
920 * and linux irq flags.
922 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
923 const u32 *intspec, unsigned int intsize,
924 irq_hw_number_t *out_hwirq, unsigned int *out_type)
926 struct irq_fwspec fwspec;
928 of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
929 return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
931 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
934 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
936 * Device Tree IRQ specifier translation function which works with either one
937 * or two cell bindings where the cell values map directly to the hwirq number
938 * and linux irq flags.
940 * Note: don't use this function unless your interrupt controller explicitly
941 * supports both one and two cell bindings. For the majority of controllers
942 * the _onecell() or _twocell() variants above should be used.
944 int irq_domain_xlate_onetwocell(struct irq_domain *d,
945 struct device_node *ctrlr,
946 const u32 *intspec, unsigned int intsize,
947 unsigned long *out_hwirq, unsigned int *out_type)
949 if (WARN_ON(intsize < 1))
951 *out_hwirq = intspec[0];
953 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
955 *out_type = IRQ_TYPE_NONE;
958 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
960 const struct irq_domain_ops irq_domain_simple_ops = {
961 .xlate = irq_domain_xlate_onetwocell,
963 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
966 * irq_domain_translate_onecell() - Generic translate for direct one cell
969 int irq_domain_translate_onecell(struct irq_domain *d,
970 struct irq_fwspec *fwspec,
971 unsigned long *out_hwirq,
972 unsigned int *out_type)
974 if (WARN_ON(fwspec->param_count < 1))
976 *out_hwirq = fwspec->param[0];
977 *out_type = IRQ_TYPE_NONE;
980 EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
983 * irq_domain_translate_twocell() - Generic translate for direct two cell
986 * Device Tree IRQ specifier translation function which works with two cell
987 * bindings where the cell values map directly to the hwirq number
988 * and linux irq flags.
990 int irq_domain_translate_twocell(struct irq_domain *d,
991 struct irq_fwspec *fwspec,
992 unsigned long *out_hwirq,
993 unsigned int *out_type)
995 if (WARN_ON(fwspec->param_count < 2))
997 *out_hwirq = fwspec->param[0];
998 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1001 EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1003 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1004 int node, const struct irq_affinity_desc *affinity)
1009 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1012 hint = hwirq % nr_irqs;
1015 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1017 if (virq <= 0 && hint > 1) {
1018 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1027 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1028 * @irq_data: The pointer to irq_data
1030 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1032 irq_data->hwirq = 0;
1033 irq_data->chip = &no_irq_chip;
1034 irq_data->chip_data = NULL;
1036 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1038 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1040 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1041 * @parent: Parent irq domain to associate with the new domain
1042 * @flags: Irq domain flags associated to the domain
1043 * @size: Size of the domain. See below
1044 * @fwnode: Optional fwnode of the interrupt controller
1045 * @ops: Pointer to the interrupt domain callbacks
1046 * @host_data: Controller private data pointer
1048 * If @size is 0 a tree domain is created, otherwise a linear domain.
1050 * If successful the parent is associated to the new domain and the
1051 * domain flags are set.
1052 * Returns pointer to IRQ domain, or NULL on failure.
1054 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1057 struct fwnode_handle *fwnode,
1058 const struct irq_domain_ops *ops,
1061 struct irq_domain *domain;
1064 domain = irq_domain_create_linear(fwnode, size, ops, host_data);
1066 domain = irq_domain_create_tree(fwnode, ops, host_data);
1068 domain->parent = parent;
1069 domain->flags |= flags;
1074 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1076 static void irq_domain_insert_irq(int virq)
1078 struct irq_data *data;
1080 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1081 struct irq_domain *domain = data->domain;
1084 irq_domain_set_mapping(domain, data->hwirq, data);
1086 /* If not already assigned, give the domain the chip's name */
1087 if (!domain->name && data->chip)
1088 domain->name = data->chip->name;
1091 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1094 static void irq_domain_remove_irq(int virq)
1096 struct irq_data *data;
1098 irq_set_status_flags(virq, IRQ_NOREQUEST);
1099 irq_set_chip_and_handler(virq, NULL, NULL);
1100 synchronize_irq(virq);
1103 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1104 struct irq_domain *domain = data->domain;
1105 irq_hw_number_t hwirq = data->hwirq;
1108 irq_domain_clear_mapping(domain, hwirq);
1112 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1113 struct irq_data *child)
1115 struct irq_data *irq_data;
1117 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1118 irq_data_get_node(child));
1120 child->parent_data = irq_data;
1121 irq_data->irq = child->irq;
1122 irq_data->common = child->common;
1123 irq_data->domain = domain;
1129 static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
1131 struct irq_data *tmp;
1135 irq_data = irq_data->parent_data;
1140 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1142 struct irq_data *irq_data, *tmp;
1145 for (i = 0; i < nr_irqs; i++) {
1146 irq_data = irq_get_irq_data(virq + i);
1147 tmp = irq_data->parent_data;
1148 irq_data->parent_data = NULL;
1149 irq_data->domain = NULL;
1151 __irq_domain_free_hierarchy(tmp);
1156 * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
1157 * @domain: IRQ domain from which the hierarchy is to be disconnected
1158 * @virq: IRQ number where the hierarchy is to be trimmed
1160 * Marks the @virq level belonging to @domain as disconnected.
1161 * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
1164 * Its only use is to be able to trim levels of hierarchy that do not
1165 * have any real meaning for this interrupt, and that the driver marks
1166 * as such from its .alloc() callback.
1168 int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
1171 struct irq_data *irqd;
1173 irqd = irq_domain_get_irq_data(domain, virq);
1177 irqd->chip = ERR_PTR(-ENOTCONN);
1181 static int irq_domain_trim_hierarchy(unsigned int virq)
1183 struct irq_data *tail, *irqd, *irq_data;
1185 irq_data = irq_get_irq_data(virq);
1188 /* The first entry must have a valid irqchip */
1189 if (!irq_data->chip || IS_ERR(irq_data->chip))
1193 * Validate that the irq_data chain is sane in the presence of
1194 * a hierarchy trimming marker.
1196 for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
1197 /* Can't have a valid irqchip after a trim marker */
1198 if (irqd->chip && tail)
1201 /* Can't have an empty irqchip before a trim marker */
1202 if (!irqd->chip && !tail)
1205 if (IS_ERR(irqd->chip)) {
1206 /* Only -ENOTCONN is a valid trim marker */
1207 if (PTR_ERR(irqd->chip) != -ENOTCONN)
1214 /* No trim marker, nothing to do */
1218 pr_info("IRQ%d: trimming hierarchy from %s\n",
1219 virq, tail->parent_data->domain->name);
1221 /* Sever the inner part of the hierarchy... */
1223 tail = tail->parent_data;
1224 irqd->parent_data = NULL;
1225 __irq_domain_free_hierarchy(tail);
1230 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1231 unsigned int virq, unsigned int nr_irqs)
1233 struct irq_data *irq_data;
1234 struct irq_domain *parent;
1237 /* The outermost irq_data is embedded in struct irq_desc */
1238 for (i = 0; i < nr_irqs; i++) {
1239 irq_data = irq_get_irq_data(virq + i);
1240 irq_data->domain = domain;
1242 for (parent = domain->parent; parent; parent = parent->parent) {
1243 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1245 irq_domain_free_irq_data(virq, i + 1);
1255 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1256 * @domain: domain to match
1257 * @virq: IRQ number to get irq_data
1259 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1262 struct irq_data *irq_data;
1264 for (irq_data = irq_get_irq_data(virq); irq_data;
1265 irq_data = irq_data->parent_data)
1266 if (irq_data->domain == domain)
1271 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1274 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1275 * @domain: Interrupt domain to match
1277 * @hwirq: The hwirq number
1278 * @chip: The associated interrupt chip
1279 * @chip_data: The associated chip data
1281 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1282 irq_hw_number_t hwirq, struct irq_chip *chip,
1285 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1290 irq_data->hwirq = hwirq;
1291 irq_data->chip = chip ? chip : &no_irq_chip;
1292 irq_data->chip_data = chip_data;
1296 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1299 * irq_domain_set_info - Set the complete data for a @virq in @domain
1300 * @domain: Interrupt domain to match
1302 * @hwirq: The hardware interrupt number
1303 * @chip: The associated interrupt chip
1304 * @chip_data: The associated interrupt chip data
1305 * @handler: The interrupt flow handler
1306 * @handler_data: The interrupt flow handler data
1307 * @handler_name: The interrupt handler name
1309 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1310 irq_hw_number_t hwirq, struct irq_chip *chip,
1311 void *chip_data, irq_flow_handler_t handler,
1312 void *handler_data, const char *handler_name)
1314 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1315 __irq_set_handler(virq, handler, 0, handler_name);
1316 irq_set_handler_data(virq, handler_data);
1318 EXPORT_SYMBOL(irq_domain_set_info);
1321 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1322 * @domain: Interrupt domain to match
1323 * @virq: IRQ number to start with
1324 * @nr_irqs: The number of irqs to free
1326 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1327 unsigned int nr_irqs)
1329 struct irq_data *irq_data;
1332 for (i = 0; i < nr_irqs; i++) {
1333 irq_data = irq_domain_get_irq_data(domain, virq + i);
1335 irq_domain_reset_irq_data(irq_data);
1337 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1339 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1342 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1343 * @domain: Interrupt domain to match
1344 * @virq: IRQ number to start with
1345 * @nr_irqs: The number of irqs to free
1347 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1348 unsigned int nr_irqs)
1352 for (i = 0; i < nr_irqs; i++) {
1353 irq_set_handler_data(virq + i, NULL);
1354 irq_set_handler(virq + i, NULL);
1356 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1359 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1360 unsigned int irq_base,
1361 unsigned int nr_irqs)
1365 if (!domain->ops->free)
1368 for (i = 0; i < nr_irqs; i++) {
1369 if (irq_domain_get_irq_data(domain, irq_base + i))
1370 domain->ops->free(domain, irq_base + i, 1);
1374 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1375 unsigned int irq_base,
1376 unsigned int nr_irqs, void *arg)
1378 if (!domain->ops->alloc) {
1379 pr_debug("domain->ops->alloc() is NULL\n");
1383 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1387 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1388 * @domain: domain to allocate from
1389 * @irq_base: allocate specified IRQ number if irq_base >= 0
1390 * @nr_irqs: number of IRQs to allocate
1391 * @node: NUMA node id for memory allocation
1392 * @arg: domain specific argument
1393 * @realloc: IRQ descriptors have already been allocated if true
1394 * @affinity: Optional irq affinity mask for multiqueue devices
1396 * Allocate IRQ numbers and initialized all data structures to support
1397 * hierarchy IRQ domains.
1398 * Parameter @realloc is mainly to support legacy IRQs.
1399 * Returns error code or allocated IRQ number
1401 * The whole process to setup an IRQ has been split into two steps.
1402 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1403 * descriptor and required hardware resources. The second step,
1404 * irq_domain_activate_irq(), is to program the hardware with preallocated
1405 * resources. In this way, it's easier to rollback when failing to
1406 * allocate resources.
1408 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1409 unsigned int nr_irqs, int node, void *arg,
1410 bool realloc, const struct irq_affinity_desc *affinity)
1414 if (domain == NULL) {
1415 domain = irq_default_domain;
1416 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1420 if (realloc && irq_base >= 0) {
1423 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1426 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1432 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1433 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1438 mutex_lock(&irq_domain_mutex);
1439 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1441 mutex_unlock(&irq_domain_mutex);
1442 goto out_free_irq_data;
1445 for (i = 0; i < nr_irqs; i++) {
1446 ret = irq_domain_trim_hierarchy(virq + i);
1448 mutex_unlock(&irq_domain_mutex);
1449 goto out_free_irq_data;
1453 for (i = 0; i < nr_irqs; i++)
1454 irq_domain_insert_irq(virq + i);
1455 mutex_unlock(&irq_domain_mutex);
1460 irq_domain_free_irq_data(virq, nr_irqs);
1462 irq_free_descs(virq, nr_irqs);
1466 /* The irq_data was moved, fix the revmap to refer to the new location */
1467 static void irq_domain_fix_revmap(struct irq_data *d)
1471 if (d->hwirq < d->domain->revmap_size)
1472 return; /* Not using radix tree. */
1474 /* Fix up the revmap. */
1475 mutex_lock(&d->domain->revmap_tree_mutex);
1476 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1478 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1479 mutex_unlock(&d->domain->revmap_tree_mutex);
1483 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1484 * @domain: Domain to push.
1485 * @virq: Irq to push the domain in to.
1486 * @arg: Passed to the irq_domain_ops alloc() function.
1488 * For an already existing irqdomain hierarchy, as might be obtained
1489 * via a call to pci_enable_msix(), add an additional domain to the
1490 * head of the processing chain. Must be called before request_irq()
1493 int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1495 struct irq_data *child_irq_data;
1496 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1497 struct irq_desc *desc;
1501 * Check that no action has been set, which indicates the virq
1502 * is in a state where this function doesn't have to deal with
1503 * races between interrupt handling and maintaining the
1504 * hierarchy. This will catch gross misuse. Attempting to
1505 * make the check race free would require holding locks across
1506 * calls to struct irq_domain_ops->alloc(), which could lead
1507 * to deadlock, so we just do a simple check before starting.
1509 desc = irq_to_desc(virq);
1512 if (WARN_ON(desc->action))
1518 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1524 if (domain->parent != root_irq_data->domain)
1527 child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
1528 irq_data_get_node(root_irq_data));
1529 if (!child_irq_data)
1532 mutex_lock(&irq_domain_mutex);
1534 /* Copy the original irq_data. */
1535 *child_irq_data = *root_irq_data;
1538 * Overwrite the root_irq_data, which is embedded in struct
1539 * irq_desc, with values for this domain.
1541 root_irq_data->parent_data = child_irq_data;
1542 root_irq_data->domain = domain;
1543 root_irq_data->mask = 0;
1544 root_irq_data->hwirq = 0;
1545 root_irq_data->chip = NULL;
1546 root_irq_data->chip_data = NULL;
1548 /* May (probably does) set hwirq, chip, etc. */
1549 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1551 /* Restore the original irq_data. */
1552 *root_irq_data = *child_irq_data;
1553 kfree(child_irq_data);
1557 irq_domain_fix_revmap(child_irq_data);
1558 irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
1561 mutex_unlock(&irq_domain_mutex);
1565 EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1568 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1569 * @domain: Domain to remove.
1570 * @virq: Irq to remove the domain from.
1572 * Undo the effects of a call to irq_domain_push_irq(). Must be
1573 * called either before request_irq() or after free_irq().
1575 int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1577 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1578 struct irq_data *child_irq_data;
1579 struct irq_data *tmp_irq_data;
1580 struct irq_desc *desc;
1583 * Check that no action is set, which indicates the virq is in
1584 * a state where this function doesn't have to deal with races
1585 * between interrupt handling and maintaining the hierarchy.
1586 * This will catch gross misuse. Attempting to make the check
1587 * race free would require holding locks across calls to
1588 * struct irq_domain_ops->free(), which could lead to
1589 * deadlock, so we just do a simple check before starting.
1591 desc = irq_to_desc(virq);
1594 if (WARN_ON(desc->action))
1603 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1605 /* We can only "pop" if this domain is at the top of the list */
1606 if (WARN_ON(root_irq_data != tmp_irq_data))
1609 if (WARN_ON(root_irq_data->domain != domain))
1612 child_irq_data = root_irq_data->parent_data;
1613 if (WARN_ON(!child_irq_data))
1616 mutex_lock(&irq_domain_mutex);
1618 root_irq_data->parent_data = NULL;
1620 irq_domain_clear_mapping(domain, root_irq_data->hwirq);
1621 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1623 /* Restore the original irq_data. */
1624 *root_irq_data = *child_irq_data;
1626 irq_domain_fix_revmap(root_irq_data);
1628 mutex_unlock(&irq_domain_mutex);
1630 kfree(child_irq_data);
1634 EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1637 * irq_domain_free_irqs - Free IRQ number and associated data structures
1638 * @virq: base IRQ number
1639 * @nr_irqs: number of IRQs to free
1641 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1643 struct irq_data *data = irq_get_irq_data(virq);
1646 if (WARN(!data || !data->domain || !data->domain->ops->free,
1647 "NULL pointer, cannot free irq\n"))
1650 mutex_lock(&irq_domain_mutex);
1651 for (i = 0; i < nr_irqs; i++)
1652 irq_domain_remove_irq(virq + i);
1653 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
1654 mutex_unlock(&irq_domain_mutex);
1656 irq_domain_free_irq_data(virq, nr_irqs);
1657 irq_free_descs(virq, nr_irqs);
1661 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1662 * @domain: Domain below which interrupts must be allocated
1663 * @irq_base: Base IRQ number
1664 * @nr_irqs: Number of IRQs to allocate
1665 * @arg: Allocation data (arch/domain specific)
1667 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1668 unsigned int irq_base, unsigned int nr_irqs,
1671 if (!domain->parent)
1674 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1677 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1680 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1681 * @domain: Domain below which interrupts must be freed
1682 * @irq_base: Base IRQ number
1683 * @nr_irqs: Number of IRQs to free
1685 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1686 unsigned int irq_base, unsigned int nr_irqs)
1688 if (!domain->parent)
1691 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1693 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1695 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1697 if (irq_data && irq_data->domain) {
1698 struct irq_domain *domain = irq_data->domain;
1700 if (domain->ops->deactivate)
1701 domain->ops->deactivate(domain, irq_data);
1702 if (irq_data->parent_data)
1703 __irq_domain_deactivate_irq(irq_data->parent_data);
1707 static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1711 if (irqd && irqd->domain) {
1712 struct irq_domain *domain = irqd->domain;
1714 if (irqd->parent_data)
1715 ret = __irq_domain_activate_irq(irqd->parent_data,
1717 if (!ret && domain->ops->activate) {
1718 ret = domain->ops->activate(domain, irqd, reserve);
1719 /* Rollback in case of error */
1720 if (ret && irqd->parent_data)
1721 __irq_domain_deactivate_irq(irqd->parent_data);
1728 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1730 * @irq_data: Outermost irq_data associated with interrupt
1731 * @reserve: If set only reserve an interrupt vector instead of assigning one
1733 * This is the second step to call domain_ops->activate to program interrupt
1734 * controllers, so the interrupt could actually get delivered.
1736 int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1740 if (!irqd_is_activated(irq_data))
1741 ret = __irq_domain_activate_irq(irq_data, reserve);
1743 irqd_set_activated(irq_data);
1748 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1749 * deactivate interrupt
1750 * @irq_data: outermost irq_data associated with interrupt
1752 * It calls domain_ops->deactivate to program interrupt controllers to disable
1753 * interrupt delivery.
1755 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1757 if (irqd_is_activated(irq_data)) {
1758 __irq_domain_deactivate_irq(irq_data);
1759 irqd_clr_activated(irq_data);
1763 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1765 /* Hierarchy irq_domains must implement callback alloc() */
1766 if (domain->ops->alloc)
1767 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1771 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1772 * parent has MSI remapping support
1773 * @domain: domain pointer
1775 bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1777 for (; domain; domain = domain->parent) {
1778 if (irq_domain_is_msi_remap(domain))
1783 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1785 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1786 * @domain: domain to match
1787 * @virq: IRQ number to get irq_data
1789 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1792 struct irq_data *irq_data = irq_get_irq_data(virq);
1794 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1796 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1799 * irq_domain_set_info - Set the complete data for a @virq in @domain
1800 * @domain: Interrupt domain to match
1802 * @hwirq: The hardware interrupt number
1803 * @chip: The associated interrupt chip
1804 * @chip_data: The associated interrupt chip data
1805 * @handler: The interrupt flow handler
1806 * @handler_data: The interrupt flow handler data
1807 * @handler_name: The interrupt handler name
1809 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1810 irq_hw_number_t hwirq, struct irq_chip *chip,
1811 void *chip_data, irq_flow_handler_t handler,
1812 void *handler_data, const char *handler_name)
1814 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1815 irq_set_chip_data(virq, chip_data);
1816 irq_set_handler_data(virq, handler_data);
1819 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1822 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1824 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1825 static struct dentry *domain_dir;
1828 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1830 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1831 seq_printf(m, "%*ssize: %u\n", ind + 1, "",
1832 d->revmap_size + d->revmap_direct_max_irq);
1833 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1834 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1835 if (d->ops && d->ops->debug_show)
1836 d->ops->debug_show(m, d, NULL, ind + 1);
1837 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1840 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1841 irq_domain_debug_show_one(m, d->parent, ind + 4);
1845 static int irq_domain_debug_show(struct seq_file *m, void *p)
1847 struct irq_domain *d = m->private;
1849 /* Default domain? Might be NULL */
1851 if (!irq_default_domain)
1853 d = irq_default_domain;
1855 irq_domain_debug_show_one(m, d, 0);
1858 DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1860 static void debugfs_add_domain_dir(struct irq_domain *d)
1862 if (!d->name || !domain_dir)
1864 debugfs_create_file(d->name, 0444, domain_dir, d,
1865 &irq_domain_debug_fops);
1868 static void debugfs_remove_domain_dir(struct irq_domain *d)
1870 debugfs_remove(debugfs_lookup(d->name, domain_dir));
1873 void __init irq_domain_debugfs_init(struct dentry *root)
1875 struct irq_domain *d;
1877 domain_dir = debugfs_create_dir("domains", root);
1879 debugfs_create_file("default", 0444, domain_dir, NULL,
1880 &irq_domain_debug_fops);
1881 mutex_lock(&irq_domain_mutex);
1882 list_for_each_entry(d, &irq_domain_list, link)
1883 debugfs_add_domain_dir(d);
1884 mutex_unlock(&irq_domain_mutex);