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 int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
29 unsigned int nr_irqs, int node, void *arg,
30 bool realloc, const struct irq_affinity_desc *affinity);
31 static void irq_domain_check_hierarchy(struct irq_domain *domain);
34 struct fwnode_handle fwnode;
40 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
41 static void debugfs_add_domain_dir(struct irq_domain *d);
42 static void debugfs_remove_domain_dir(struct irq_domain *d);
44 static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
45 static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
48 static const char *irqchip_fwnode_get_name(const struct fwnode_handle *fwnode)
50 struct irqchip_fwid *fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
55 const struct fwnode_operations irqchip_fwnode_ops = {
56 .get_name = irqchip_fwnode_get_name,
58 EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
61 * __irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
62 * identifying an irq domain
63 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
64 * @id: Optional user provided id if name != NULL
65 * @name: Optional user provided domain name
66 * @pa: Optional user-provided physical address
68 * Allocate a struct irqchip_fwid, and return a pointer to the embedded
69 * fwnode_handle (or NULL on failure).
71 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
72 * solely to transport name information to irqdomain creation code. The
73 * node is not stored. For other types the pointer is kept in the irq
76 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
80 struct irqchip_fwid *fwid;
83 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
86 case IRQCHIP_FWNODE_NAMED:
87 n = kasprintf(GFP_KERNEL, "%s", name);
89 case IRQCHIP_FWNODE_NAMED_ID:
90 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
93 n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
106 fwnode_init(&fwid->fwnode, &irqchip_fwnode_ops);
107 return &fwid->fwnode;
109 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
112 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
114 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
116 void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
118 struct irqchip_fwid *fwid;
120 if (!fwnode || WARN_ON(!is_fwnode_irqchip(fwnode)))
123 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
127 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
129 static struct irq_domain *__irq_domain_create(struct fwnode_handle *fwnode,
131 irq_hw_number_t hwirq_max,
133 const struct irq_domain_ops *ops,
136 struct irqchip_fwid *fwid;
137 struct irq_domain *domain;
139 static atomic_t unknown_domains;
141 if (WARN_ON((size && direct_max) ||
142 (!IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) && direct_max) ||
143 (direct_max && (direct_max != hwirq_max))))
146 domain = kzalloc_node(struct_size(domain, revmap, size),
147 GFP_KERNEL, of_node_to_nid(to_of_node(fwnode)));
151 if (is_fwnode_irqchip(fwnode)) {
152 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
154 switch (fwid->type) {
155 case IRQCHIP_FWNODE_NAMED:
156 case IRQCHIP_FWNODE_NAMED_ID:
157 domain->fwnode = fwnode;
158 domain->name = kstrdup(fwid->name, GFP_KERNEL);
163 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
166 domain->fwnode = fwnode;
167 domain->name = fwid->name;
170 } else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
171 is_software_node(fwnode)) {
175 * fwnode paths contain '/', which debugfs is legitimately
176 * unhappy about. Replace them with ':', which does
177 * the trick and is not as offensive as '\'...
179 name = kasprintf(GFP_KERNEL, "%pfw", fwnode);
185 strreplace(name, '/', ':');
188 domain->fwnode = fwnode;
189 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
194 pr_err("Invalid fwnode type for irqdomain\n");
195 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
196 atomic_inc_return(&unknown_domains));
201 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
204 fwnode_handle_get(fwnode);
205 fwnode_dev_initialized(fwnode, true);
208 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
210 domain->host_data = host_data;
211 domain->hwirq_max = hwirq_max;
214 domain->flags |= IRQ_DOMAIN_FLAG_NO_MAP;
216 domain->revmap_size = size;
219 * Hierarchical domains use the domain lock of the root domain
220 * (innermost domain).
222 * For non-hierarchical domains (as for root domains), the root
223 * pointer is set to the domain itself so that &domain->root->mutex
224 * always points to the right lock.
226 mutex_init(&domain->mutex);
227 domain->root = domain;
229 irq_domain_check_hierarchy(domain);
234 static void __irq_domain_publish(struct irq_domain *domain)
236 mutex_lock(&irq_domain_mutex);
237 debugfs_add_domain_dir(domain);
238 list_add(&domain->link, &irq_domain_list);
239 mutex_unlock(&irq_domain_mutex);
241 pr_debug("Added domain %s\n", domain->name);
245 * __irq_domain_add() - Allocate a new irq_domain data structure
246 * @fwnode: firmware node for the interrupt controller
247 * @size: Size of linear map; 0 for radix mapping only
248 * @hwirq_max: Maximum number of interrupts supported by controller
249 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
251 * @ops: domain callbacks
252 * @host_data: Controller private data pointer
254 * Allocates and initializes an irq_domain structure.
255 * Returns pointer to IRQ domain, or NULL on failure.
257 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, unsigned int size,
258 irq_hw_number_t hwirq_max, int direct_max,
259 const struct irq_domain_ops *ops,
262 struct irq_domain *domain;
264 domain = __irq_domain_create(fwnode, size, hwirq_max, direct_max,
267 __irq_domain_publish(domain);
271 EXPORT_SYMBOL_GPL(__irq_domain_add);
274 * irq_domain_remove() - Remove an irq domain.
275 * @domain: domain to remove
277 * This routine is used to remove an irq domain. The caller must ensure
278 * that all mappings within the domain have been disposed of prior to
279 * use, depending on the revmap type.
281 void irq_domain_remove(struct irq_domain *domain)
283 mutex_lock(&irq_domain_mutex);
284 debugfs_remove_domain_dir(domain);
286 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
288 list_del(&domain->link);
291 * If the going away domain is the default one, reset it.
293 if (unlikely(irq_default_domain == domain))
294 irq_set_default_host(NULL);
296 mutex_unlock(&irq_domain_mutex);
298 pr_debug("Removed domain %s\n", domain->name);
300 fwnode_dev_initialized(domain->fwnode, false);
301 fwnode_handle_put(domain->fwnode);
302 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
306 EXPORT_SYMBOL_GPL(irq_domain_remove);
308 void irq_domain_update_bus_token(struct irq_domain *domain,
309 enum irq_domain_bus_token bus_token)
313 if (domain->bus_token == bus_token)
316 mutex_lock(&irq_domain_mutex);
318 domain->bus_token = bus_token;
320 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
322 mutex_unlock(&irq_domain_mutex);
326 debugfs_remove_domain_dir(domain);
328 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
331 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
334 debugfs_add_domain_dir(domain);
336 mutex_unlock(&irq_domain_mutex);
338 EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
341 * irq_domain_create_simple() - Register an irq_domain and optionally map a range of irqs
342 * @fwnode: firmware node for the interrupt controller
343 * @size: total number of irqs in mapping
344 * @first_irq: first number of irq block assigned to the domain,
345 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
346 * pre-map all of the irqs in the domain to virqs starting at first_irq.
347 * @ops: domain callbacks
348 * @host_data: Controller private data pointer
350 * Allocates an irq_domain, and optionally if first_irq is positive then also
351 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
353 * This is intended to implement the expected behaviour for most
354 * interrupt controllers. If device tree is used, then first_irq will be 0 and
355 * irqs get mapped dynamically on the fly. However, if the controller requires
356 * static virq assignments (non-DT boot) then it will set that up correctly.
358 struct irq_domain *irq_domain_create_simple(struct fwnode_handle *fwnode,
360 unsigned int first_irq,
361 const struct irq_domain_ops *ops,
364 struct irq_domain *domain;
366 domain = __irq_domain_add(fwnode, size, size, 0, ops, host_data);
371 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
372 /* attempt to allocated irq_descs */
373 int rc = irq_alloc_descs(first_irq, first_irq, size,
374 of_node_to_nid(to_of_node(fwnode)));
376 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
379 irq_domain_associate_many(domain, first_irq, 0, size);
384 EXPORT_SYMBOL_GPL(irq_domain_create_simple);
387 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
388 * @of_node: pointer to interrupt controller's device tree node.
389 * @size: total number of irqs in legacy mapping
390 * @first_irq: first number of irq block assigned to the domain
391 * @first_hwirq: first hwirq number to use for the translation. Should normally
392 * be '0', but a positive integer can be used if the effective
393 * hwirqs numbering does not begin at zero.
394 * @ops: map/unmap domain callbacks
395 * @host_data: Controller private data pointer
397 * Note: the map() callback will be called before this function returns
398 * for all legacy interrupts except 0 (which is always the invalid irq for
399 * a legacy controller).
401 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
403 unsigned int first_irq,
404 irq_hw_number_t first_hwirq,
405 const struct irq_domain_ops *ops,
408 return irq_domain_create_legacy(of_node_to_fwnode(of_node), size,
409 first_irq, first_hwirq, ops, host_data);
411 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
413 struct irq_domain *irq_domain_create_legacy(struct fwnode_handle *fwnode,
415 unsigned int first_irq,
416 irq_hw_number_t first_hwirq,
417 const struct irq_domain_ops *ops,
420 struct irq_domain *domain;
422 domain = __irq_domain_add(fwnode, first_hwirq + size, first_hwirq + size, 0, ops, host_data);
424 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
428 EXPORT_SYMBOL_GPL(irq_domain_create_legacy);
431 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
432 * @fwspec: FW specifier for an interrupt
433 * @bus_token: domain-specific data
435 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
436 enum irq_domain_bus_token bus_token)
438 struct irq_domain *h, *found = NULL;
439 struct fwnode_handle *fwnode = fwspec->fwnode;
442 /* We might want to match the legacy controller last since
443 * it might potentially be set to match all interrupts in
444 * the absence of a device node. This isn't a problem so far
447 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
448 * values must generate an exact match for the domain to be
451 mutex_lock(&irq_domain_mutex);
452 list_for_each_entry(h, &irq_domain_list, link) {
453 if (h->ops->select && fwspec->param_count)
454 rc = h->ops->select(h, fwspec, bus_token);
455 else if (h->ops->match)
456 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
458 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
459 ((bus_token == DOMAIN_BUS_ANY) ||
460 (h->bus_token == bus_token)));
467 mutex_unlock(&irq_domain_mutex);
470 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
473 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
476 * Return: false if any MSI irq domain does not support IRQ remapping,
477 * true otherwise (including if there is no MSI irq domain)
479 bool irq_domain_check_msi_remap(void)
481 struct irq_domain *h;
484 mutex_lock(&irq_domain_mutex);
485 list_for_each_entry(h, &irq_domain_list, link) {
486 if (irq_domain_is_msi(h) &&
487 !irq_domain_hierarchical_is_msi_remap(h)) {
492 mutex_unlock(&irq_domain_mutex);
495 EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
498 * irq_set_default_host() - Set a "default" irq domain
499 * @domain: default domain pointer
501 * For convenience, it's possible to set a "default" domain that will be used
502 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
503 * platforms that want to manipulate a few hard coded interrupt numbers that
504 * aren't properly represented in the device-tree.
506 void irq_set_default_host(struct irq_domain *domain)
508 pr_debug("Default domain set to @0x%p\n", domain);
510 irq_default_domain = domain;
512 EXPORT_SYMBOL_GPL(irq_set_default_host);
515 * irq_get_default_host() - Retrieve the "default" irq domain
517 * Returns: the default domain, if any.
519 * Modern code should never use this. This should only be used on
520 * systems that cannot implement a firmware->fwnode mapping (which
521 * both DT and ACPI provide).
523 struct irq_domain *irq_get_default_host(void)
525 return irq_default_domain;
527 EXPORT_SYMBOL_GPL(irq_get_default_host);
529 static bool irq_domain_is_nomap(struct irq_domain *domain)
531 return IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) &&
532 (domain->flags & IRQ_DOMAIN_FLAG_NO_MAP);
535 static void irq_domain_clear_mapping(struct irq_domain *domain,
536 irq_hw_number_t hwirq)
538 lockdep_assert_held(&domain->root->mutex);
540 if (irq_domain_is_nomap(domain))
543 if (hwirq < domain->revmap_size)
544 rcu_assign_pointer(domain->revmap[hwirq], NULL);
546 radix_tree_delete(&domain->revmap_tree, hwirq);
549 static void irq_domain_set_mapping(struct irq_domain *domain,
550 irq_hw_number_t hwirq,
551 struct irq_data *irq_data)
554 * This also makes sure that all domains point to the same root when
555 * called from irq_domain_insert_irq() for each domain in a hierarchy.
557 lockdep_assert_held(&domain->root->mutex);
559 if (irq_domain_is_nomap(domain))
562 if (hwirq < domain->revmap_size)
563 rcu_assign_pointer(domain->revmap[hwirq], irq_data);
565 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
568 static void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
570 struct irq_data *irq_data = irq_get_irq_data(irq);
571 irq_hw_number_t hwirq;
573 if (WARN(!irq_data || irq_data->domain != domain,
574 "virq%i doesn't exist; cannot disassociate\n", irq))
577 hwirq = irq_data->hwirq;
579 mutex_lock(&domain->root->mutex);
581 irq_set_status_flags(irq, IRQ_NOREQUEST);
583 /* remove chip and handler */
584 irq_set_chip_and_handler(irq, NULL, NULL);
586 /* Make sure it's completed */
587 synchronize_irq(irq);
589 /* Tell the PIC about it */
590 if (domain->ops->unmap)
591 domain->ops->unmap(domain, irq);
594 irq_data->domain = NULL;
598 /* Clear reverse map for this hwirq */
599 irq_domain_clear_mapping(domain, hwirq);
601 mutex_unlock(&domain->root->mutex);
604 static int irq_domain_associate_locked(struct irq_domain *domain, unsigned int virq,
605 irq_hw_number_t hwirq)
607 struct irq_data *irq_data = irq_get_irq_data(virq);
610 if (WARN(hwirq >= domain->hwirq_max,
611 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
613 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
615 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
618 irq_data->hwirq = hwirq;
619 irq_data->domain = domain;
620 if (domain->ops->map) {
621 ret = domain->ops->map(domain, virq, hwirq);
624 * If map() returns -EPERM, this interrupt is protected
625 * by the firmware or some other service and shall not
626 * be mapped. Don't bother telling the user about it.
629 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
630 domain->name, hwirq, virq, ret);
632 irq_data->domain = NULL;
639 irq_domain_set_mapping(domain, hwirq, irq_data);
641 irq_clear_status_flags(virq, IRQ_NOREQUEST);
646 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
647 irq_hw_number_t hwirq)
651 mutex_lock(&domain->root->mutex);
652 ret = irq_domain_associate_locked(domain, virq, hwirq);
653 mutex_unlock(&domain->root->mutex);
657 EXPORT_SYMBOL_GPL(irq_domain_associate);
659 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
660 irq_hw_number_t hwirq_base, int count)
662 struct device_node *of_node;
665 of_node = irq_domain_get_of_node(domain);
666 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
667 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
669 for (i = 0; i < count; i++)
670 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
672 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
674 #ifdef CONFIG_IRQ_DOMAIN_NOMAP
676 * irq_create_direct_mapping() - Allocate an irq for direct mapping
677 * @domain: domain to allocate the irq for or NULL for default domain
679 * This routine is used for irq controllers which can choose the hardware
680 * interrupt numbers they generate. In such a case it's simplest to use
681 * the linux irq as the hardware interrupt number. It still uses the linear
682 * or radix tree to store the mapping, but the irq controller can optimize
683 * the revmap path by using the hwirq directly.
685 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
687 struct device_node *of_node;
691 domain = irq_default_domain;
693 of_node = irq_domain_get_of_node(domain);
694 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
696 pr_debug("create_direct virq allocation failed\n");
699 if (virq >= domain->hwirq_max) {
700 pr_err("ERROR: no free irqs available below %lu maximum\n",
705 pr_debug("create_direct obtained virq %d\n", virq);
707 if (irq_domain_associate(domain, virq, virq)) {
714 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
717 static unsigned int irq_create_mapping_affinity_locked(struct irq_domain *domain,
718 irq_hw_number_t hwirq,
719 const struct irq_affinity_desc *affinity)
721 struct device_node *of_node = irq_domain_get_of_node(domain);
724 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
726 /* Allocate a virtual interrupt number */
727 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
730 pr_debug("-> virq allocation failed\n");
734 if (irq_domain_associate_locked(domain, virq, hwirq)) {
739 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
740 hwirq, of_node_full_name(of_node), virq);
746 * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
747 * @domain: domain owning this hardware interrupt or NULL for default domain
748 * @hwirq: hardware irq number in that domain space
749 * @affinity: irq affinity
751 * Only one mapping per hardware interrupt is permitted. Returns a linux
753 * If the sense/trigger is to be specified, set_irq_type() should be called
754 * on the number returned from that call.
756 unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
757 irq_hw_number_t hwirq,
758 const struct irq_affinity_desc *affinity)
762 /* Look for default domain if necessary */
764 domain = irq_default_domain;
765 if (domain == NULL) {
766 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
770 mutex_lock(&domain->root->mutex);
772 /* Check if mapping already exists */
773 virq = irq_find_mapping(domain, hwirq);
775 pr_debug("existing mapping on virq %d\n", virq);
779 virq = irq_create_mapping_affinity_locked(domain, hwirq, affinity);
781 mutex_unlock(&domain->root->mutex);
785 EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
787 static int irq_domain_translate(struct irq_domain *d,
788 struct irq_fwspec *fwspec,
789 irq_hw_number_t *hwirq, unsigned int *type)
791 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
792 if (d->ops->translate)
793 return d->ops->translate(d, fwspec, hwirq, type);
796 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
797 fwspec->param, fwspec->param_count,
800 /* If domain has no translation, then we assume interrupt line */
801 *hwirq = fwspec->param[0];
805 void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
806 unsigned int count, struct irq_fwspec *fwspec)
810 fwspec->fwnode = of_node_to_fwnode(np);
811 fwspec->param_count = count;
813 for (i = 0; i < count; i++)
814 fwspec->param[i] = args[i];
816 EXPORT_SYMBOL_GPL(of_phandle_args_to_fwspec);
818 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
820 struct irq_domain *domain;
821 struct irq_data *irq_data;
822 irq_hw_number_t hwirq;
823 unsigned int type = IRQ_TYPE_NONE;
826 if (fwspec->fwnode) {
827 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
829 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
831 domain = irq_default_domain;
835 pr_warn("no irq domain found for %s !\n",
836 of_node_full_name(to_of_node(fwspec->fwnode)));
840 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
844 * WARN if the irqchip returns a type with bits
845 * outside the sense mask set and clear these bits.
847 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
848 type &= IRQ_TYPE_SENSE_MASK;
850 mutex_lock(&domain->root->mutex);
853 * If we've already configured this interrupt,
854 * don't do it again, or hell will break loose.
856 virq = irq_find_mapping(domain, hwirq);
859 * If the trigger type is not specified or matches the
860 * current trigger type then we are done so return the
863 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
867 * If the trigger type has not been set yet, then set
868 * it now and return the interrupt number.
870 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
871 irq_data = irq_get_irq_data(virq);
877 irqd_set_trigger_type(irq_data, type);
881 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
882 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
887 if (irq_domain_is_hierarchy(domain)) {
888 virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE,
889 fwspec, false, NULL);
896 virq = irq_create_mapping_affinity_locked(domain, hwirq, NULL);
901 irq_data = irq_get_irq_data(virq);
902 if (WARN_ON(!irq_data)) {
907 /* Store trigger type */
908 irqd_set_trigger_type(irq_data, type);
910 mutex_unlock(&domain->root->mutex);
914 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
916 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
918 struct irq_fwspec fwspec;
920 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
921 irq_data->args_count, &fwspec);
923 return irq_create_fwspec_mapping(&fwspec);
925 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
928 * irq_dispose_mapping() - Unmap an interrupt
929 * @virq: linux irq number of the interrupt to unmap
931 void irq_dispose_mapping(unsigned int virq)
933 struct irq_data *irq_data = irq_get_irq_data(virq);
934 struct irq_domain *domain;
936 if (!virq || !irq_data)
939 domain = irq_data->domain;
940 if (WARN_ON(domain == NULL))
943 if (irq_domain_is_hierarchy(domain)) {
944 irq_domain_free_irqs(virq, 1);
946 irq_domain_disassociate(domain, virq);
950 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
953 * __irq_resolve_mapping() - Find a linux irq from a hw irq number.
954 * @domain: domain owning this hardware interrupt
955 * @hwirq: hardware irq number in that domain space
956 * @irq: optional pointer to return the Linux irq if required
958 * Returns the interrupt descriptor.
960 struct irq_desc *__irq_resolve_mapping(struct irq_domain *domain,
961 irq_hw_number_t hwirq,
964 struct irq_desc *desc = NULL;
965 struct irq_data *data;
967 /* Look for default domain if necessary */
969 domain = irq_default_domain;
973 if (irq_domain_is_nomap(domain)) {
974 if (hwirq < domain->hwirq_max) {
975 data = irq_domain_get_irq_data(domain, hwirq);
976 if (data && data->hwirq == hwirq)
977 desc = irq_data_to_desc(data);
986 /* Check if the hwirq is in the linear revmap. */
987 if (hwirq < domain->revmap_size)
988 data = rcu_dereference(domain->revmap[hwirq]);
990 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
993 desc = irq_data_to_desc(data);
1001 EXPORT_SYMBOL_GPL(__irq_resolve_mapping);
1004 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
1006 * Device Tree IRQ specifier translation function which works with one cell
1007 * bindings where the cell value maps directly to the hwirq number.
1009 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
1010 const u32 *intspec, unsigned int intsize,
1011 unsigned long *out_hwirq, unsigned int *out_type)
1013 if (WARN_ON(intsize < 1))
1015 *out_hwirq = intspec[0];
1016 *out_type = IRQ_TYPE_NONE;
1019 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
1022 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
1024 * Device Tree IRQ specifier translation function which works with two cell
1025 * bindings where the cell values map directly to the hwirq number
1026 * and linux irq flags.
1028 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
1029 const u32 *intspec, unsigned int intsize,
1030 irq_hw_number_t *out_hwirq, unsigned int *out_type)
1032 struct irq_fwspec fwspec;
1034 of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
1035 return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
1037 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
1040 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
1042 * Device Tree IRQ specifier translation function which works with either one
1043 * or two cell bindings where the cell values map directly to the hwirq number
1044 * and linux irq flags.
1046 * Note: don't use this function unless your interrupt controller explicitly
1047 * supports both one and two cell bindings. For the majority of controllers
1048 * the _onecell() or _twocell() variants above should be used.
1050 int irq_domain_xlate_onetwocell(struct irq_domain *d,
1051 struct device_node *ctrlr,
1052 const u32 *intspec, unsigned int intsize,
1053 unsigned long *out_hwirq, unsigned int *out_type)
1055 if (WARN_ON(intsize < 1))
1057 *out_hwirq = intspec[0];
1059 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1061 *out_type = IRQ_TYPE_NONE;
1064 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
1066 const struct irq_domain_ops irq_domain_simple_ops = {
1067 .xlate = irq_domain_xlate_onetwocell,
1069 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1072 * irq_domain_translate_onecell() - Generic translate for direct one cell
1075 int irq_domain_translate_onecell(struct irq_domain *d,
1076 struct irq_fwspec *fwspec,
1077 unsigned long *out_hwirq,
1078 unsigned int *out_type)
1080 if (WARN_ON(fwspec->param_count < 1))
1082 *out_hwirq = fwspec->param[0];
1083 *out_type = IRQ_TYPE_NONE;
1086 EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
1089 * irq_domain_translate_twocell() - Generic translate for direct two cell
1092 * Device Tree IRQ specifier translation function which works with two cell
1093 * bindings where the cell values map directly to the hwirq number
1094 * and linux irq flags.
1096 int irq_domain_translate_twocell(struct irq_domain *d,
1097 struct irq_fwspec *fwspec,
1098 unsigned long *out_hwirq,
1099 unsigned int *out_type)
1101 if (WARN_ON(fwspec->param_count < 2))
1103 *out_hwirq = fwspec->param[0];
1104 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1107 EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1109 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1110 int node, const struct irq_affinity_desc *affinity)
1115 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1118 hint = hwirq % nr_irqs;
1121 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1123 if (virq <= 0 && hint > 1) {
1124 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1133 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1134 * @irq_data: The pointer to irq_data
1136 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1138 irq_data->hwirq = 0;
1139 irq_data->chip = &no_irq_chip;
1140 irq_data->chip_data = NULL;
1142 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1144 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1146 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1147 * @parent: Parent irq domain to associate with the new domain
1148 * @flags: Irq domain flags associated to the domain
1149 * @size: Size of the domain. See below
1150 * @fwnode: Optional fwnode of the interrupt controller
1151 * @ops: Pointer to the interrupt domain callbacks
1152 * @host_data: Controller private data pointer
1154 * If @size is 0 a tree domain is created, otherwise a linear domain.
1156 * If successful the parent is associated to the new domain and the
1157 * domain flags are set.
1158 * Returns pointer to IRQ domain, or NULL on failure.
1160 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1163 struct fwnode_handle *fwnode,
1164 const struct irq_domain_ops *ops,
1167 struct irq_domain *domain;
1170 domain = __irq_domain_create(fwnode, size, size, 0, ops, host_data);
1172 domain = __irq_domain_create(fwnode, 0, ~0, 0, ops, host_data);
1175 domain->root = parent->root;
1176 domain->parent = parent;
1177 domain->flags |= flags;
1179 __irq_domain_publish(domain);
1184 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1186 static void irq_domain_insert_irq(int virq)
1188 struct irq_data *data;
1190 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1191 struct irq_domain *domain = data->domain;
1194 irq_domain_set_mapping(domain, data->hwirq, data);
1197 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1200 static void irq_domain_remove_irq(int virq)
1202 struct irq_data *data;
1204 irq_set_status_flags(virq, IRQ_NOREQUEST);
1205 irq_set_chip_and_handler(virq, NULL, NULL);
1206 synchronize_irq(virq);
1209 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1210 struct irq_domain *domain = data->domain;
1211 irq_hw_number_t hwirq = data->hwirq;
1214 irq_domain_clear_mapping(domain, hwirq);
1218 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1219 struct irq_data *child)
1221 struct irq_data *irq_data;
1223 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1224 irq_data_get_node(child));
1226 child->parent_data = irq_data;
1227 irq_data->irq = child->irq;
1228 irq_data->common = child->common;
1229 irq_data->domain = domain;
1235 static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
1237 struct irq_data *tmp;
1241 irq_data = irq_data->parent_data;
1246 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1248 struct irq_data *irq_data, *tmp;
1251 for (i = 0; i < nr_irqs; i++) {
1252 irq_data = irq_get_irq_data(virq + i);
1253 tmp = irq_data->parent_data;
1254 irq_data->parent_data = NULL;
1255 irq_data->domain = NULL;
1257 __irq_domain_free_hierarchy(tmp);
1262 * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
1263 * @domain: IRQ domain from which the hierarchy is to be disconnected
1264 * @virq: IRQ number where the hierarchy is to be trimmed
1266 * Marks the @virq level belonging to @domain as disconnected.
1267 * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
1270 * Its only use is to be able to trim levels of hierarchy that do not
1271 * have any real meaning for this interrupt, and that the driver marks
1272 * as such from its .alloc() callback.
1274 int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
1277 struct irq_data *irqd;
1279 irqd = irq_domain_get_irq_data(domain, virq);
1283 irqd->chip = ERR_PTR(-ENOTCONN);
1286 EXPORT_SYMBOL_GPL(irq_domain_disconnect_hierarchy);
1288 static int irq_domain_trim_hierarchy(unsigned int virq)
1290 struct irq_data *tail, *irqd, *irq_data;
1292 irq_data = irq_get_irq_data(virq);
1295 /* The first entry must have a valid irqchip */
1296 if (!irq_data->chip || IS_ERR(irq_data->chip))
1300 * Validate that the irq_data chain is sane in the presence of
1301 * a hierarchy trimming marker.
1303 for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
1304 /* Can't have a valid irqchip after a trim marker */
1305 if (irqd->chip && tail)
1308 /* Can't have an empty irqchip before a trim marker */
1309 if (!irqd->chip && !tail)
1312 if (IS_ERR(irqd->chip)) {
1313 /* Only -ENOTCONN is a valid trim marker */
1314 if (PTR_ERR(irqd->chip) != -ENOTCONN)
1321 /* No trim marker, nothing to do */
1325 pr_info("IRQ%d: trimming hierarchy from %s\n",
1326 virq, tail->parent_data->domain->name);
1328 /* Sever the inner part of the hierarchy... */
1330 tail = tail->parent_data;
1331 irqd->parent_data = NULL;
1332 __irq_domain_free_hierarchy(tail);
1337 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1338 unsigned int virq, unsigned int nr_irqs)
1340 struct irq_data *irq_data;
1341 struct irq_domain *parent;
1344 /* The outermost irq_data is embedded in struct irq_desc */
1345 for (i = 0; i < nr_irqs; i++) {
1346 irq_data = irq_get_irq_data(virq + i);
1347 irq_data->domain = domain;
1349 for (parent = domain->parent; parent; parent = parent->parent) {
1350 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1352 irq_domain_free_irq_data(virq, i + 1);
1362 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1363 * @domain: domain to match
1364 * @virq: IRQ number to get irq_data
1366 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1369 struct irq_data *irq_data;
1371 for (irq_data = irq_get_irq_data(virq); irq_data;
1372 irq_data = irq_data->parent_data)
1373 if (irq_data->domain == domain)
1378 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1381 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1382 * @domain: Interrupt domain to match
1384 * @hwirq: The hwirq number
1385 * @chip: The associated interrupt chip
1386 * @chip_data: The associated chip data
1388 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1389 irq_hw_number_t hwirq,
1390 const struct irq_chip *chip,
1393 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1398 irq_data->hwirq = hwirq;
1399 irq_data->chip = (struct irq_chip *)(chip ? chip : &no_irq_chip);
1400 irq_data->chip_data = chip_data;
1404 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1407 * irq_domain_set_info - Set the complete data for a @virq in @domain
1408 * @domain: Interrupt domain to match
1410 * @hwirq: The hardware interrupt number
1411 * @chip: The associated interrupt chip
1412 * @chip_data: The associated interrupt chip data
1413 * @handler: The interrupt flow handler
1414 * @handler_data: The interrupt flow handler data
1415 * @handler_name: The interrupt handler name
1417 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1418 irq_hw_number_t hwirq, const struct irq_chip *chip,
1419 void *chip_data, irq_flow_handler_t handler,
1420 void *handler_data, const char *handler_name)
1422 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1423 __irq_set_handler(virq, handler, 0, handler_name);
1424 irq_set_handler_data(virq, handler_data);
1426 EXPORT_SYMBOL(irq_domain_set_info);
1429 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1430 * @domain: Interrupt domain to match
1431 * @virq: IRQ number to start with
1432 * @nr_irqs: The number of irqs to free
1434 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1435 unsigned int nr_irqs)
1437 struct irq_data *irq_data;
1440 for (i = 0; i < nr_irqs; i++) {
1441 irq_data = irq_domain_get_irq_data(domain, virq + i);
1443 irq_domain_reset_irq_data(irq_data);
1445 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1447 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1450 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1451 * @domain: Interrupt domain to match
1452 * @virq: IRQ number to start with
1453 * @nr_irqs: The number of irqs to free
1455 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1456 unsigned int nr_irqs)
1460 for (i = 0; i < nr_irqs; i++) {
1461 irq_set_handler_data(virq + i, NULL);
1462 irq_set_handler(virq + i, NULL);
1464 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1467 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1468 unsigned int irq_base,
1469 unsigned int nr_irqs)
1473 if (!domain->ops->free)
1476 for (i = 0; i < nr_irqs; i++) {
1477 if (irq_domain_get_irq_data(domain, irq_base + i))
1478 domain->ops->free(domain, irq_base + i, 1);
1482 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1483 unsigned int irq_base,
1484 unsigned int nr_irqs, void *arg)
1486 if (!domain->ops->alloc) {
1487 pr_debug("domain->ops->alloc() is NULL\n");
1491 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1494 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1495 unsigned int nr_irqs, int node, void *arg,
1496 bool realloc, const struct irq_affinity_desc *affinity)
1500 if (realloc && irq_base >= 0) {
1503 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1506 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1512 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1513 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1518 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1520 goto out_free_irq_data;
1522 for (i = 0; i < nr_irqs; i++) {
1523 ret = irq_domain_trim_hierarchy(virq + i);
1525 goto out_free_irq_data;
1528 for (i = 0; i < nr_irqs; i++)
1529 irq_domain_insert_irq(virq + i);
1534 irq_domain_free_irq_data(virq, nr_irqs);
1536 irq_free_descs(virq, nr_irqs);
1541 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1542 * @domain: domain to allocate from
1543 * @irq_base: allocate specified IRQ number if irq_base >= 0
1544 * @nr_irqs: number of IRQs to allocate
1545 * @node: NUMA node id for memory allocation
1546 * @arg: domain specific argument
1547 * @realloc: IRQ descriptors have already been allocated if true
1548 * @affinity: Optional irq affinity mask for multiqueue devices
1550 * Allocate IRQ numbers and initialized all data structures to support
1551 * hierarchy IRQ domains.
1552 * Parameter @realloc is mainly to support legacy IRQs.
1553 * Returns error code or allocated IRQ number
1555 * The whole process to setup an IRQ has been split into two steps.
1556 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1557 * descriptor and required hardware resources. The second step,
1558 * irq_domain_activate_irq(), is to program the hardware with preallocated
1559 * resources. In this way, it's easier to rollback when failing to
1560 * allocate resources.
1562 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1563 unsigned int nr_irqs, int node, void *arg,
1564 bool realloc, const struct irq_affinity_desc *affinity)
1568 if (domain == NULL) {
1569 domain = irq_default_domain;
1570 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1574 mutex_lock(&domain->root->mutex);
1575 ret = irq_domain_alloc_irqs_locked(domain, irq_base, nr_irqs, node, arg,
1577 mutex_unlock(&domain->root->mutex);
1581 EXPORT_SYMBOL_GPL(__irq_domain_alloc_irqs);
1583 /* The irq_data was moved, fix the revmap to refer to the new location */
1584 static void irq_domain_fix_revmap(struct irq_data *d)
1588 lockdep_assert_held(&d->domain->root->mutex);
1590 if (irq_domain_is_nomap(d->domain))
1593 /* Fix up the revmap. */
1594 if (d->hwirq < d->domain->revmap_size) {
1595 /* Not using radix tree */
1596 rcu_assign_pointer(d->domain->revmap[d->hwirq], d);
1598 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1600 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1605 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1606 * @domain: Domain to push.
1607 * @virq: Irq to push the domain in to.
1608 * @arg: Passed to the irq_domain_ops alloc() function.
1610 * For an already existing irqdomain hierarchy, as might be obtained
1611 * via a call to pci_enable_msix(), add an additional domain to the
1612 * head of the processing chain. Must be called before request_irq()
1615 int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1617 struct irq_data *irq_data = irq_get_irq_data(virq);
1618 struct irq_data *parent_irq_data;
1619 struct irq_desc *desc;
1623 * Check that no action has been set, which indicates the virq
1624 * is in a state where this function doesn't have to deal with
1625 * races between interrupt handling and maintaining the
1626 * hierarchy. This will catch gross misuse. Attempting to
1627 * make the check race free would require holding locks across
1628 * calls to struct irq_domain_ops->alloc(), which could lead
1629 * to deadlock, so we just do a simple check before starting.
1631 desc = irq_to_desc(virq);
1634 if (WARN_ON(desc->action))
1640 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1646 if (domain->parent != irq_data->domain)
1649 parent_irq_data = kzalloc_node(sizeof(*parent_irq_data), GFP_KERNEL,
1650 irq_data_get_node(irq_data));
1651 if (!parent_irq_data)
1654 mutex_lock(&domain->root->mutex);
1656 /* Copy the original irq_data. */
1657 *parent_irq_data = *irq_data;
1660 * Overwrite the irq_data, which is embedded in struct irq_desc, with
1661 * values for this domain.
1663 irq_data->parent_data = parent_irq_data;
1664 irq_data->domain = domain;
1666 irq_data->hwirq = 0;
1667 irq_data->chip = NULL;
1668 irq_data->chip_data = NULL;
1670 /* May (probably does) set hwirq, chip, etc. */
1671 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1673 /* Restore the original irq_data. */
1674 *irq_data = *parent_irq_data;
1675 kfree(parent_irq_data);
1679 irq_domain_fix_revmap(parent_irq_data);
1680 irq_domain_set_mapping(domain, irq_data->hwirq, irq_data);
1682 mutex_unlock(&domain->root->mutex);
1686 EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1689 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1690 * @domain: Domain to remove.
1691 * @virq: Irq to remove the domain from.
1693 * Undo the effects of a call to irq_domain_push_irq(). Must be
1694 * called either before request_irq() or after free_irq().
1696 int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1698 struct irq_data *irq_data = irq_get_irq_data(virq);
1699 struct irq_data *parent_irq_data;
1700 struct irq_data *tmp_irq_data;
1701 struct irq_desc *desc;
1704 * Check that no action is set, which indicates the virq is in
1705 * a state where this function doesn't have to deal with races
1706 * between interrupt handling and maintaining the hierarchy.
1707 * This will catch gross misuse. Attempting to make the check
1708 * race free would require holding locks across calls to
1709 * struct irq_domain_ops->free(), which could lead to
1710 * deadlock, so we just do a simple check before starting.
1712 desc = irq_to_desc(virq);
1715 if (WARN_ON(desc->action))
1724 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1726 /* We can only "pop" if this domain is at the top of the list */
1727 if (WARN_ON(irq_data != tmp_irq_data))
1730 if (WARN_ON(irq_data->domain != domain))
1733 parent_irq_data = irq_data->parent_data;
1734 if (WARN_ON(!parent_irq_data))
1737 mutex_lock(&domain->root->mutex);
1739 irq_data->parent_data = NULL;
1741 irq_domain_clear_mapping(domain, irq_data->hwirq);
1742 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1744 /* Restore the original irq_data. */
1745 *irq_data = *parent_irq_data;
1747 irq_domain_fix_revmap(irq_data);
1749 mutex_unlock(&domain->root->mutex);
1751 kfree(parent_irq_data);
1755 EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1758 * irq_domain_free_irqs - Free IRQ number and associated data structures
1759 * @virq: base IRQ number
1760 * @nr_irqs: number of IRQs to free
1762 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1764 struct irq_data *data = irq_get_irq_data(virq);
1765 struct irq_domain *domain;
1768 if (WARN(!data || !data->domain || !data->domain->ops->free,
1769 "NULL pointer, cannot free irq\n"))
1772 domain = data->domain;
1774 mutex_lock(&domain->root->mutex);
1775 for (i = 0; i < nr_irqs; i++)
1776 irq_domain_remove_irq(virq + i);
1777 irq_domain_free_irqs_hierarchy(domain, virq, nr_irqs);
1778 mutex_unlock(&domain->root->mutex);
1780 irq_domain_free_irq_data(virq, nr_irqs);
1781 irq_free_descs(virq, nr_irqs);
1785 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1786 * @domain: Domain below which interrupts must be allocated
1787 * @irq_base: Base IRQ number
1788 * @nr_irqs: Number of IRQs to allocate
1789 * @arg: Allocation data (arch/domain specific)
1791 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1792 unsigned int irq_base, unsigned int nr_irqs,
1795 if (!domain->parent)
1798 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1801 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1804 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1805 * @domain: Domain below which interrupts must be freed
1806 * @irq_base: Base IRQ number
1807 * @nr_irqs: Number of IRQs to free
1809 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1810 unsigned int irq_base, unsigned int nr_irqs)
1812 if (!domain->parent)
1815 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1817 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1819 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1821 if (irq_data && irq_data->domain) {
1822 struct irq_domain *domain = irq_data->domain;
1824 if (domain->ops->deactivate)
1825 domain->ops->deactivate(domain, irq_data);
1826 if (irq_data->parent_data)
1827 __irq_domain_deactivate_irq(irq_data->parent_data);
1831 static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1835 if (irqd && irqd->domain) {
1836 struct irq_domain *domain = irqd->domain;
1838 if (irqd->parent_data)
1839 ret = __irq_domain_activate_irq(irqd->parent_data,
1841 if (!ret && domain->ops->activate) {
1842 ret = domain->ops->activate(domain, irqd, reserve);
1843 /* Rollback in case of error */
1844 if (ret && irqd->parent_data)
1845 __irq_domain_deactivate_irq(irqd->parent_data);
1852 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1854 * @irq_data: Outermost irq_data associated with interrupt
1855 * @reserve: If set only reserve an interrupt vector instead of assigning one
1857 * This is the second step to call domain_ops->activate to program interrupt
1858 * controllers, so the interrupt could actually get delivered.
1860 int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1864 if (!irqd_is_activated(irq_data))
1865 ret = __irq_domain_activate_irq(irq_data, reserve);
1867 irqd_set_activated(irq_data);
1872 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1873 * deactivate interrupt
1874 * @irq_data: outermost irq_data associated with interrupt
1876 * It calls domain_ops->deactivate to program interrupt controllers to disable
1877 * interrupt delivery.
1879 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1881 if (irqd_is_activated(irq_data)) {
1882 __irq_domain_deactivate_irq(irq_data);
1883 irqd_clr_activated(irq_data);
1887 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1889 /* Hierarchy irq_domains must implement callback alloc() */
1890 if (domain->ops->alloc)
1891 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1895 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1896 * parent has MSI remapping support
1897 * @domain: domain pointer
1899 bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1901 for (; domain; domain = domain->parent) {
1902 if (irq_domain_is_msi_remap(domain))
1907 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1909 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1910 * @domain: domain to match
1911 * @virq: IRQ number to get irq_data
1913 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1916 struct irq_data *irq_data = irq_get_irq_data(virq);
1918 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1920 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1923 * irq_domain_set_info - Set the complete data for a @virq in @domain
1924 * @domain: Interrupt domain to match
1926 * @hwirq: The hardware interrupt number
1927 * @chip: The associated interrupt chip
1928 * @chip_data: The associated interrupt chip data
1929 * @handler: The interrupt flow handler
1930 * @handler_data: The interrupt flow handler data
1931 * @handler_name: The interrupt handler name
1933 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1934 irq_hw_number_t hwirq, const struct irq_chip *chip,
1935 void *chip_data, irq_flow_handler_t handler,
1936 void *handler_data, const char *handler_name)
1938 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1939 irq_set_chip_data(virq, chip_data);
1940 irq_set_handler_data(virq, handler_data);
1943 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1944 unsigned int nr_irqs, int node, void *arg,
1945 bool realloc, const struct irq_affinity_desc *affinity)
1950 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1953 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1955 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1956 static struct dentry *domain_dir;
1959 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1961 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1962 seq_printf(m, "%*ssize: %u\n", ind + 1, "", d->revmap_size);
1963 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1964 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1965 if (d->ops && d->ops->debug_show)
1966 d->ops->debug_show(m, d, NULL, ind + 1);
1967 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1970 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1971 irq_domain_debug_show_one(m, d->parent, ind + 4);
1975 static int irq_domain_debug_show(struct seq_file *m, void *p)
1977 struct irq_domain *d = m->private;
1979 /* Default domain? Might be NULL */
1981 if (!irq_default_domain)
1983 d = irq_default_domain;
1985 irq_domain_debug_show_one(m, d, 0);
1988 DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1990 static void debugfs_add_domain_dir(struct irq_domain *d)
1992 if (!d->name || !domain_dir)
1994 debugfs_create_file(d->name, 0444, domain_dir, d,
1995 &irq_domain_debug_fops);
1998 static void debugfs_remove_domain_dir(struct irq_domain *d)
2000 debugfs_lookup_and_remove(d->name, domain_dir);
2003 void __init irq_domain_debugfs_init(struct dentry *root)
2005 struct irq_domain *d;
2007 domain_dir = debugfs_create_dir("domains", root);
2009 debugfs_create_file("default", 0444, domain_dir, NULL,
2010 &irq_domain_debug_fops);
2011 mutex_lock(&irq_domain_mutex);
2012 list_for_each_entry(d, &irq_domain_list, link)
2013 debugfs_add_domain_dir(d);
2014 mutex_unlock(&irq_domain_mutex);