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_set_default_host() - Set a "default" irq domain
474 * @domain: default domain pointer
476 * For convenience, it's possible to set a "default" domain that will be used
477 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
478 * platforms that want to manipulate a few hard coded interrupt numbers that
479 * aren't properly represented in the device-tree.
481 void irq_set_default_host(struct irq_domain *domain)
483 pr_debug("Default domain set to @0x%p\n", domain);
485 irq_default_domain = domain;
487 EXPORT_SYMBOL_GPL(irq_set_default_host);
490 * irq_get_default_host() - Retrieve the "default" irq domain
492 * Returns: the default domain, if any.
494 * Modern code should never use this. This should only be used on
495 * systems that cannot implement a firmware->fwnode mapping (which
496 * both DT and ACPI provide).
498 struct irq_domain *irq_get_default_host(void)
500 return irq_default_domain;
502 EXPORT_SYMBOL_GPL(irq_get_default_host);
504 static bool irq_domain_is_nomap(struct irq_domain *domain)
506 return IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) &&
507 (domain->flags & IRQ_DOMAIN_FLAG_NO_MAP);
510 static void irq_domain_clear_mapping(struct irq_domain *domain,
511 irq_hw_number_t hwirq)
513 lockdep_assert_held(&domain->root->mutex);
515 if (irq_domain_is_nomap(domain))
518 if (hwirq < domain->revmap_size)
519 rcu_assign_pointer(domain->revmap[hwirq], NULL);
521 radix_tree_delete(&domain->revmap_tree, hwirq);
524 static void irq_domain_set_mapping(struct irq_domain *domain,
525 irq_hw_number_t hwirq,
526 struct irq_data *irq_data)
529 * This also makes sure that all domains point to the same root when
530 * called from irq_domain_insert_irq() for each domain in a hierarchy.
532 lockdep_assert_held(&domain->root->mutex);
534 if (irq_domain_is_nomap(domain))
537 if (hwirq < domain->revmap_size)
538 rcu_assign_pointer(domain->revmap[hwirq], irq_data);
540 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
543 static void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
545 struct irq_data *irq_data = irq_get_irq_data(irq);
546 irq_hw_number_t hwirq;
548 if (WARN(!irq_data || irq_data->domain != domain,
549 "virq%i doesn't exist; cannot disassociate\n", irq))
552 hwirq = irq_data->hwirq;
554 mutex_lock(&domain->root->mutex);
556 irq_set_status_flags(irq, IRQ_NOREQUEST);
558 /* remove chip and handler */
559 irq_set_chip_and_handler(irq, NULL, NULL);
561 /* Make sure it's completed */
562 synchronize_irq(irq);
564 /* Tell the PIC about it */
565 if (domain->ops->unmap)
566 domain->ops->unmap(domain, irq);
569 irq_data->domain = NULL;
573 /* Clear reverse map for this hwirq */
574 irq_domain_clear_mapping(domain, hwirq);
576 mutex_unlock(&domain->root->mutex);
579 static int irq_domain_associate_locked(struct irq_domain *domain, unsigned int virq,
580 irq_hw_number_t hwirq)
582 struct irq_data *irq_data = irq_get_irq_data(virq);
585 if (WARN(hwirq >= domain->hwirq_max,
586 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
588 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
590 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
593 irq_data->hwirq = hwirq;
594 irq_data->domain = domain;
595 if (domain->ops->map) {
596 ret = domain->ops->map(domain, virq, hwirq);
599 * If map() returns -EPERM, this interrupt is protected
600 * by the firmware or some other service and shall not
601 * be mapped. Don't bother telling the user about it.
604 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
605 domain->name, hwirq, virq, ret);
607 irq_data->domain = NULL;
614 irq_domain_set_mapping(domain, hwirq, irq_data);
616 irq_clear_status_flags(virq, IRQ_NOREQUEST);
621 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
622 irq_hw_number_t hwirq)
626 mutex_lock(&domain->root->mutex);
627 ret = irq_domain_associate_locked(domain, virq, hwirq);
628 mutex_unlock(&domain->root->mutex);
632 EXPORT_SYMBOL_GPL(irq_domain_associate);
634 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
635 irq_hw_number_t hwirq_base, int count)
637 struct device_node *of_node;
640 of_node = irq_domain_get_of_node(domain);
641 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
642 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
644 for (i = 0; i < count; i++)
645 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
647 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
649 #ifdef CONFIG_IRQ_DOMAIN_NOMAP
651 * irq_create_direct_mapping() - Allocate an irq for direct mapping
652 * @domain: domain to allocate the irq for or NULL for default domain
654 * This routine is used for irq controllers which can choose the hardware
655 * interrupt numbers they generate. In such a case it's simplest to use
656 * the linux irq as the hardware interrupt number. It still uses the linear
657 * or radix tree to store the mapping, but the irq controller can optimize
658 * the revmap path by using the hwirq directly.
660 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
662 struct device_node *of_node;
666 domain = irq_default_domain;
668 of_node = irq_domain_get_of_node(domain);
669 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
671 pr_debug("create_direct virq allocation failed\n");
674 if (virq >= domain->hwirq_max) {
675 pr_err("ERROR: no free irqs available below %lu maximum\n",
680 pr_debug("create_direct obtained virq %d\n", virq);
682 if (irq_domain_associate(domain, virq, virq)) {
689 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
692 static unsigned int irq_create_mapping_affinity_locked(struct irq_domain *domain,
693 irq_hw_number_t hwirq,
694 const struct irq_affinity_desc *affinity)
696 struct device_node *of_node = irq_domain_get_of_node(domain);
699 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
701 /* Allocate a virtual interrupt number */
702 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
705 pr_debug("-> virq allocation failed\n");
709 if (irq_domain_associate_locked(domain, virq, hwirq)) {
714 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
715 hwirq, of_node_full_name(of_node), virq);
721 * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
722 * @domain: domain owning this hardware interrupt or NULL for default domain
723 * @hwirq: hardware irq number in that domain space
724 * @affinity: irq affinity
726 * Only one mapping per hardware interrupt is permitted. Returns a linux
728 * If the sense/trigger is to be specified, set_irq_type() should be called
729 * on the number returned from that call.
731 unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
732 irq_hw_number_t hwirq,
733 const struct irq_affinity_desc *affinity)
737 /* Look for default domain if necessary */
739 domain = irq_default_domain;
740 if (domain == NULL) {
741 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
745 mutex_lock(&domain->root->mutex);
747 /* Check if mapping already exists */
748 virq = irq_find_mapping(domain, hwirq);
750 pr_debug("existing mapping on virq %d\n", virq);
754 virq = irq_create_mapping_affinity_locked(domain, hwirq, affinity);
756 mutex_unlock(&domain->root->mutex);
760 EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
762 static int irq_domain_translate(struct irq_domain *d,
763 struct irq_fwspec *fwspec,
764 irq_hw_number_t *hwirq, unsigned int *type)
766 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
767 if (d->ops->translate)
768 return d->ops->translate(d, fwspec, hwirq, type);
771 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
772 fwspec->param, fwspec->param_count,
775 /* If domain has no translation, then we assume interrupt line */
776 *hwirq = fwspec->param[0];
780 void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
781 unsigned int count, struct irq_fwspec *fwspec)
785 fwspec->fwnode = of_node_to_fwnode(np);
786 fwspec->param_count = count;
788 for (i = 0; i < count; i++)
789 fwspec->param[i] = args[i];
791 EXPORT_SYMBOL_GPL(of_phandle_args_to_fwspec);
793 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
795 struct irq_domain *domain;
796 struct irq_data *irq_data;
797 irq_hw_number_t hwirq;
798 unsigned int type = IRQ_TYPE_NONE;
801 if (fwspec->fwnode) {
802 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
804 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
806 domain = irq_default_domain;
810 pr_warn("no irq domain found for %s !\n",
811 of_node_full_name(to_of_node(fwspec->fwnode)));
815 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
819 * WARN if the irqchip returns a type with bits
820 * outside the sense mask set and clear these bits.
822 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
823 type &= IRQ_TYPE_SENSE_MASK;
825 mutex_lock(&domain->root->mutex);
828 * If we've already configured this interrupt,
829 * don't do it again, or hell will break loose.
831 virq = irq_find_mapping(domain, hwirq);
834 * If the trigger type is not specified or matches the
835 * current trigger type then we are done so return the
838 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
842 * If the trigger type has not been set yet, then set
843 * it now and return the interrupt number.
845 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
846 irq_data = irq_get_irq_data(virq);
852 irqd_set_trigger_type(irq_data, type);
856 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
857 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
862 if (irq_domain_is_hierarchy(domain)) {
863 virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE,
864 fwspec, false, NULL);
871 virq = irq_create_mapping_affinity_locked(domain, hwirq, NULL);
876 irq_data = irq_get_irq_data(virq);
877 if (WARN_ON(!irq_data)) {
882 /* Store trigger type */
883 irqd_set_trigger_type(irq_data, type);
885 mutex_unlock(&domain->root->mutex);
889 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
891 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
893 struct irq_fwspec fwspec;
895 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
896 irq_data->args_count, &fwspec);
898 return irq_create_fwspec_mapping(&fwspec);
900 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
903 * irq_dispose_mapping() - Unmap an interrupt
904 * @virq: linux irq number of the interrupt to unmap
906 void irq_dispose_mapping(unsigned int virq)
908 struct irq_data *irq_data = irq_get_irq_data(virq);
909 struct irq_domain *domain;
911 if (!virq || !irq_data)
914 domain = irq_data->domain;
915 if (WARN_ON(domain == NULL))
918 if (irq_domain_is_hierarchy(domain)) {
919 irq_domain_free_irqs(virq, 1);
921 irq_domain_disassociate(domain, virq);
925 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
928 * __irq_resolve_mapping() - Find a linux irq from a hw irq number.
929 * @domain: domain owning this hardware interrupt
930 * @hwirq: hardware irq number in that domain space
931 * @irq: optional pointer to return the Linux irq if required
933 * Returns the interrupt descriptor.
935 struct irq_desc *__irq_resolve_mapping(struct irq_domain *domain,
936 irq_hw_number_t hwirq,
939 struct irq_desc *desc = NULL;
940 struct irq_data *data;
942 /* Look for default domain if necessary */
944 domain = irq_default_domain;
948 if (irq_domain_is_nomap(domain)) {
949 if (hwirq < domain->hwirq_max) {
950 data = irq_domain_get_irq_data(domain, hwirq);
951 if (data && data->hwirq == hwirq)
952 desc = irq_data_to_desc(data);
961 /* Check if the hwirq is in the linear revmap. */
962 if (hwirq < domain->revmap_size)
963 data = rcu_dereference(domain->revmap[hwirq]);
965 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
968 desc = irq_data_to_desc(data);
976 EXPORT_SYMBOL_GPL(__irq_resolve_mapping);
979 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
981 * Device Tree IRQ specifier translation function which works with one cell
982 * bindings where the cell value maps directly to the hwirq number.
984 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
985 const u32 *intspec, unsigned int intsize,
986 unsigned long *out_hwirq, unsigned int *out_type)
988 if (WARN_ON(intsize < 1))
990 *out_hwirq = intspec[0];
991 *out_type = IRQ_TYPE_NONE;
994 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
997 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
999 * Device Tree IRQ specifier translation function which works with two cell
1000 * bindings where the cell values map directly to the hwirq number
1001 * and linux irq flags.
1003 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
1004 const u32 *intspec, unsigned int intsize,
1005 irq_hw_number_t *out_hwirq, unsigned int *out_type)
1007 struct irq_fwspec fwspec;
1009 of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
1010 return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
1012 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
1015 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
1017 * Device Tree IRQ specifier translation function which works with either one
1018 * or two cell bindings where the cell values map directly to the hwirq number
1019 * and linux irq flags.
1021 * Note: don't use this function unless your interrupt controller explicitly
1022 * supports both one and two cell bindings. For the majority of controllers
1023 * the _onecell() or _twocell() variants above should be used.
1025 int irq_domain_xlate_onetwocell(struct irq_domain *d,
1026 struct device_node *ctrlr,
1027 const u32 *intspec, unsigned int intsize,
1028 unsigned long *out_hwirq, unsigned int *out_type)
1030 if (WARN_ON(intsize < 1))
1032 *out_hwirq = intspec[0];
1034 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1036 *out_type = IRQ_TYPE_NONE;
1039 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
1041 const struct irq_domain_ops irq_domain_simple_ops = {
1042 .xlate = irq_domain_xlate_onetwocell,
1044 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1047 * irq_domain_translate_onecell() - Generic translate for direct one cell
1050 int irq_domain_translate_onecell(struct irq_domain *d,
1051 struct irq_fwspec *fwspec,
1052 unsigned long *out_hwirq,
1053 unsigned int *out_type)
1055 if (WARN_ON(fwspec->param_count < 1))
1057 *out_hwirq = fwspec->param[0];
1058 *out_type = IRQ_TYPE_NONE;
1061 EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
1064 * irq_domain_translate_twocell() - Generic translate for direct two cell
1067 * Device Tree IRQ specifier translation function which works with two cell
1068 * bindings where the cell values map directly to the hwirq number
1069 * and linux irq flags.
1071 int irq_domain_translate_twocell(struct irq_domain *d,
1072 struct irq_fwspec *fwspec,
1073 unsigned long *out_hwirq,
1074 unsigned int *out_type)
1076 if (WARN_ON(fwspec->param_count < 2))
1078 *out_hwirq = fwspec->param[0];
1079 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1082 EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1084 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1085 int node, const struct irq_affinity_desc *affinity)
1090 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1093 hint = hwirq % nr_irqs;
1096 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1098 if (virq <= 0 && hint > 1) {
1099 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1108 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1109 * @irq_data: The pointer to irq_data
1111 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1113 irq_data->hwirq = 0;
1114 irq_data->chip = &no_irq_chip;
1115 irq_data->chip_data = NULL;
1117 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1119 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1121 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1122 * @parent: Parent irq domain to associate with the new domain
1123 * @flags: Irq domain flags associated to the domain
1124 * @size: Size of the domain. See below
1125 * @fwnode: Optional fwnode of the interrupt controller
1126 * @ops: Pointer to the interrupt domain callbacks
1127 * @host_data: Controller private data pointer
1129 * If @size is 0 a tree domain is created, otherwise a linear domain.
1131 * If successful the parent is associated to the new domain and the
1132 * domain flags are set.
1133 * Returns pointer to IRQ domain, or NULL on failure.
1135 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1138 struct fwnode_handle *fwnode,
1139 const struct irq_domain_ops *ops,
1142 struct irq_domain *domain;
1145 domain = __irq_domain_create(fwnode, size, size, 0, ops, host_data);
1147 domain = __irq_domain_create(fwnode, 0, ~0, 0, ops, host_data);
1151 domain->root = parent->root;
1152 domain->parent = parent;
1153 domain->flags |= flags;
1155 __irq_domain_publish(domain);
1160 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1162 static void irq_domain_insert_irq(int virq)
1164 struct irq_data *data;
1166 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1167 struct irq_domain *domain = data->domain;
1170 irq_domain_set_mapping(domain, data->hwirq, data);
1173 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1176 static void irq_domain_remove_irq(int virq)
1178 struct irq_data *data;
1180 irq_set_status_flags(virq, IRQ_NOREQUEST);
1181 irq_set_chip_and_handler(virq, NULL, NULL);
1182 synchronize_irq(virq);
1185 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1186 struct irq_domain *domain = data->domain;
1187 irq_hw_number_t hwirq = data->hwirq;
1190 irq_domain_clear_mapping(domain, hwirq);
1194 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1195 struct irq_data *child)
1197 struct irq_data *irq_data;
1199 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1200 irq_data_get_node(child));
1202 child->parent_data = irq_data;
1203 irq_data->irq = child->irq;
1204 irq_data->common = child->common;
1205 irq_data->domain = domain;
1211 static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
1213 struct irq_data *tmp;
1217 irq_data = irq_data->parent_data;
1222 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1224 struct irq_data *irq_data, *tmp;
1227 for (i = 0; i < nr_irqs; i++) {
1228 irq_data = irq_get_irq_data(virq + i);
1229 tmp = irq_data->parent_data;
1230 irq_data->parent_data = NULL;
1231 irq_data->domain = NULL;
1233 __irq_domain_free_hierarchy(tmp);
1238 * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
1239 * @domain: IRQ domain from which the hierarchy is to be disconnected
1240 * @virq: IRQ number where the hierarchy is to be trimmed
1242 * Marks the @virq level belonging to @domain as disconnected.
1243 * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
1246 * Its only use is to be able to trim levels of hierarchy that do not
1247 * have any real meaning for this interrupt, and that the driver marks
1248 * as such from its .alloc() callback.
1250 int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
1253 struct irq_data *irqd;
1255 irqd = irq_domain_get_irq_data(domain, virq);
1259 irqd->chip = ERR_PTR(-ENOTCONN);
1262 EXPORT_SYMBOL_GPL(irq_domain_disconnect_hierarchy);
1264 static int irq_domain_trim_hierarchy(unsigned int virq)
1266 struct irq_data *tail, *irqd, *irq_data;
1268 irq_data = irq_get_irq_data(virq);
1271 /* The first entry must have a valid irqchip */
1272 if (!irq_data->chip || IS_ERR(irq_data->chip))
1276 * Validate that the irq_data chain is sane in the presence of
1277 * a hierarchy trimming marker.
1279 for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
1280 /* Can't have a valid irqchip after a trim marker */
1281 if (irqd->chip && tail)
1284 /* Can't have an empty irqchip before a trim marker */
1285 if (!irqd->chip && !tail)
1288 if (IS_ERR(irqd->chip)) {
1289 /* Only -ENOTCONN is a valid trim marker */
1290 if (PTR_ERR(irqd->chip) != -ENOTCONN)
1297 /* No trim marker, nothing to do */
1301 pr_info("IRQ%d: trimming hierarchy from %s\n",
1302 virq, tail->parent_data->domain->name);
1304 /* Sever the inner part of the hierarchy... */
1306 tail = tail->parent_data;
1307 irqd->parent_data = NULL;
1308 __irq_domain_free_hierarchy(tail);
1313 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1314 unsigned int virq, unsigned int nr_irqs)
1316 struct irq_data *irq_data;
1317 struct irq_domain *parent;
1320 /* The outermost irq_data is embedded in struct irq_desc */
1321 for (i = 0; i < nr_irqs; i++) {
1322 irq_data = irq_get_irq_data(virq + i);
1323 irq_data->domain = domain;
1325 for (parent = domain->parent; parent; parent = parent->parent) {
1326 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1328 irq_domain_free_irq_data(virq, i + 1);
1338 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1339 * @domain: domain to match
1340 * @virq: IRQ number to get irq_data
1342 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1345 struct irq_data *irq_data;
1347 for (irq_data = irq_get_irq_data(virq); irq_data;
1348 irq_data = irq_data->parent_data)
1349 if (irq_data->domain == domain)
1354 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1357 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1358 * @domain: Interrupt domain to match
1360 * @hwirq: The hwirq number
1361 * @chip: The associated interrupt chip
1362 * @chip_data: The associated chip data
1364 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1365 irq_hw_number_t hwirq,
1366 const struct irq_chip *chip,
1369 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1374 irq_data->hwirq = hwirq;
1375 irq_data->chip = (struct irq_chip *)(chip ? chip : &no_irq_chip);
1376 irq_data->chip_data = chip_data;
1380 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1383 * irq_domain_set_info - Set the complete data for a @virq in @domain
1384 * @domain: Interrupt domain to match
1386 * @hwirq: The hardware interrupt number
1387 * @chip: The associated interrupt chip
1388 * @chip_data: The associated interrupt chip data
1389 * @handler: The interrupt flow handler
1390 * @handler_data: The interrupt flow handler data
1391 * @handler_name: The interrupt handler name
1393 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1394 irq_hw_number_t hwirq, const struct irq_chip *chip,
1395 void *chip_data, irq_flow_handler_t handler,
1396 void *handler_data, const char *handler_name)
1398 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1399 __irq_set_handler(virq, handler, 0, handler_name);
1400 irq_set_handler_data(virq, handler_data);
1402 EXPORT_SYMBOL(irq_domain_set_info);
1405 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1406 * @domain: Interrupt domain to match
1407 * @virq: IRQ number to start with
1408 * @nr_irqs: The number of irqs to free
1410 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1411 unsigned int nr_irqs)
1413 struct irq_data *irq_data;
1416 for (i = 0; i < nr_irqs; i++) {
1417 irq_data = irq_domain_get_irq_data(domain, virq + i);
1419 irq_domain_reset_irq_data(irq_data);
1421 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1423 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1426 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1427 * @domain: Interrupt domain to match
1428 * @virq: IRQ number to start with
1429 * @nr_irqs: The number of irqs to free
1431 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1432 unsigned int nr_irqs)
1436 for (i = 0; i < nr_irqs; i++) {
1437 irq_set_handler_data(virq + i, NULL);
1438 irq_set_handler(virq + i, NULL);
1440 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1443 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1444 unsigned int irq_base,
1445 unsigned int nr_irqs)
1449 if (!domain->ops->free)
1452 for (i = 0; i < nr_irqs; i++) {
1453 if (irq_domain_get_irq_data(domain, irq_base + i))
1454 domain->ops->free(domain, irq_base + i, 1);
1458 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1459 unsigned int irq_base,
1460 unsigned int nr_irqs, void *arg)
1462 if (!domain->ops->alloc) {
1463 pr_debug("domain->ops->alloc() is NULL\n");
1467 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1470 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1471 unsigned int nr_irqs, int node, void *arg,
1472 bool realloc, const struct irq_affinity_desc *affinity)
1476 if (realloc && irq_base >= 0) {
1479 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1482 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1488 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1489 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1494 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1496 goto out_free_irq_data;
1498 for (i = 0; i < nr_irqs; i++) {
1499 ret = irq_domain_trim_hierarchy(virq + i);
1501 goto out_free_irq_data;
1504 for (i = 0; i < nr_irqs; i++)
1505 irq_domain_insert_irq(virq + i);
1510 irq_domain_free_irq_data(virq, nr_irqs);
1512 irq_free_descs(virq, nr_irqs);
1517 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1518 * @domain: domain to allocate from
1519 * @irq_base: allocate specified IRQ number if irq_base >= 0
1520 * @nr_irqs: number of IRQs to allocate
1521 * @node: NUMA node id for memory allocation
1522 * @arg: domain specific argument
1523 * @realloc: IRQ descriptors have already been allocated if true
1524 * @affinity: Optional irq affinity mask for multiqueue devices
1526 * Allocate IRQ numbers and initialized all data structures to support
1527 * hierarchy IRQ domains.
1528 * Parameter @realloc is mainly to support legacy IRQs.
1529 * Returns error code or allocated IRQ number
1531 * The whole process to setup an IRQ has been split into two steps.
1532 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1533 * descriptor and required hardware resources. The second step,
1534 * irq_domain_activate_irq(), is to program the hardware with preallocated
1535 * resources. In this way, it's easier to rollback when failing to
1536 * allocate resources.
1538 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1539 unsigned int nr_irqs, int node, void *arg,
1540 bool realloc, const struct irq_affinity_desc *affinity)
1544 if (domain == NULL) {
1545 domain = irq_default_domain;
1546 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1550 mutex_lock(&domain->root->mutex);
1551 ret = irq_domain_alloc_irqs_locked(domain, irq_base, nr_irqs, node, arg,
1553 mutex_unlock(&domain->root->mutex);
1557 EXPORT_SYMBOL_GPL(__irq_domain_alloc_irqs);
1559 /* The irq_data was moved, fix the revmap to refer to the new location */
1560 static void irq_domain_fix_revmap(struct irq_data *d)
1564 lockdep_assert_held(&d->domain->root->mutex);
1566 if (irq_domain_is_nomap(d->domain))
1569 /* Fix up the revmap. */
1570 if (d->hwirq < d->domain->revmap_size) {
1571 /* Not using radix tree */
1572 rcu_assign_pointer(d->domain->revmap[d->hwirq], d);
1574 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1576 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1581 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1582 * @domain: Domain to push.
1583 * @virq: Irq to push the domain in to.
1584 * @arg: Passed to the irq_domain_ops alloc() function.
1586 * For an already existing irqdomain hierarchy, as might be obtained
1587 * via a call to pci_enable_msix(), add an additional domain to the
1588 * head of the processing chain. Must be called before request_irq()
1591 int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1593 struct irq_data *irq_data = irq_get_irq_data(virq);
1594 struct irq_data *parent_irq_data;
1595 struct irq_desc *desc;
1599 * Check that no action has been set, which indicates the virq
1600 * is in a state where this function doesn't have to deal with
1601 * races between interrupt handling and maintaining the
1602 * hierarchy. This will catch gross misuse. Attempting to
1603 * make the check race free would require holding locks across
1604 * calls to struct irq_domain_ops->alloc(), which could lead
1605 * to deadlock, so we just do a simple check before starting.
1607 desc = irq_to_desc(virq);
1610 if (WARN_ON(desc->action))
1616 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1622 if (domain->parent != irq_data->domain)
1625 parent_irq_data = kzalloc_node(sizeof(*parent_irq_data), GFP_KERNEL,
1626 irq_data_get_node(irq_data));
1627 if (!parent_irq_data)
1630 mutex_lock(&domain->root->mutex);
1632 /* Copy the original irq_data. */
1633 *parent_irq_data = *irq_data;
1636 * Overwrite the irq_data, which is embedded in struct irq_desc, with
1637 * values for this domain.
1639 irq_data->parent_data = parent_irq_data;
1640 irq_data->domain = domain;
1642 irq_data->hwirq = 0;
1643 irq_data->chip = NULL;
1644 irq_data->chip_data = NULL;
1646 /* May (probably does) set hwirq, chip, etc. */
1647 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1649 /* Restore the original irq_data. */
1650 *irq_data = *parent_irq_data;
1651 kfree(parent_irq_data);
1655 irq_domain_fix_revmap(parent_irq_data);
1656 irq_domain_set_mapping(domain, irq_data->hwirq, irq_data);
1658 mutex_unlock(&domain->root->mutex);
1662 EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1665 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1666 * @domain: Domain to remove.
1667 * @virq: Irq to remove the domain from.
1669 * Undo the effects of a call to irq_domain_push_irq(). Must be
1670 * called either before request_irq() or after free_irq().
1672 int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1674 struct irq_data *irq_data = irq_get_irq_data(virq);
1675 struct irq_data *parent_irq_data;
1676 struct irq_data *tmp_irq_data;
1677 struct irq_desc *desc;
1680 * Check that no action is set, which indicates the virq is in
1681 * a state where this function doesn't have to deal with races
1682 * between interrupt handling and maintaining the hierarchy.
1683 * This will catch gross misuse. Attempting to make the check
1684 * race free would require holding locks across calls to
1685 * struct irq_domain_ops->free(), which could lead to
1686 * deadlock, so we just do a simple check before starting.
1688 desc = irq_to_desc(virq);
1691 if (WARN_ON(desc->action))
1700 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1702 /* We can only "pop" if this domain is at the top of the list */
1703 if (WARN_ON(irq_data != tmp_irq_data))
1706 if (WARN_ON(irq_data->domain != domain))
1709 parent_irq_data = irq_data->parent_data;
1710 if (WARN_ON(!parent_irq_data))
1713 mutex_lock(&domain->root->mutex);
1715 irq_data->parent_data = NULL;
1717 irq_domain_clear_mapping(domain, irq_data->hwirq);
1718 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1720 /* Restore the original irq_data. */
1721 *irq_data = *parent_irq_data;
1723 irq_domain_fix_revmap(irq_data);
1725 mutex_unlock(&domain->root->mutex);
1727 kfree(parent_irq_data);
1731 EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1734 * irq_domain_free_irqs - Free IRQ number and associated data structures
1735 * @virq: base IRQ number
1736 * @nr_irqs: number of IRQs to free
1738 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1740 struct irq_data *data = irq_get_irq_data(virq);
1741 struct irq_domain *domain;
1744 if (WARN(!data || !data->domain || !data->domain->ops->free,
1745 "NULL pointer, cannot free irq\n"))
1748 domain = data->domain;
1750 mutex_lock(&domain->root->mutex);
1751 for (i = 0; i < nr_irqs; i++)
1752 irq_domain_remove_irq(virq + i);
1753 irq_domain_free_irqs_hierarchy(domain, virq, nr_irqs);
1754 mutex_unlock(&domain->root->mutex);
1756 irq_domain_free_irq_data(virq, nr_irqs);
1757 irq_free_descs(virq, nr_irqs);
1761 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1762 * @domain: Domain below which interrupts must be allocated
1763 * @irq_base: Base IRQ number
1764 * @nr_irqs: Number of IRQs to allocate
1765 * @arg: Allocation data (arch/domain specific)
1767 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1768 unsigned int irq_base, unsigned int nr_irqs,
1771 if (!domain->parent)
1774 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1777 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1780 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1781 * @domain: Domain below which interrupts must be freed
1782 * @irq_base: Base IRQ number
1783 * @nr_irqs: Number of IRQs to free
1785 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1786 unsigned int irq_base, unsigned int nr_irqs)
1788 if (!domain->parent)
1791 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1793 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1795 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1797 if (irq_data && irq_data->domain) {
1798 struct irq_domain *domain = irq_data->domain;
1800 if (domain->ops->deactivate)
1801 domain->ops->deactivate(domain, irq_data);
1802 if (irq_data->parent_data)
1803 __irq_domain_deactivate_irq(irq_data->parent_data);
1807 static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1811 if (irqd && irqd->domain) {
1812 struct irq_domain *domain = irqd->domain;
1814 if (irqd->parent_data)
1815 ret = __irq_domain_activate_irq(irqd->parent_data,
1817 if (!ret && domain->ops->activate) {
1818 ret = domain->ops->activate(domain, irqd, reserve);
1819 /* Rollback in case of error */
1820 if (ret && irqd->parent_data)
1821 __irq_domain_deactivate_irq(irqd->parent_data);
1828 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1830 * @irq_data: Outermost irq_data associated with interrupt
1831 * @reserve: If set only reserve an interrupt vector instead of assigning one
1833 * This is the second step to call domain_ops->activate to program interrupt
1834 * controllers, so the interrupt could actually get delivered.
1836 int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1840 if (!irqd_is_activated(irq_data))
1841 ret = __irq_domain_activate_irq(irq_data, reserve);
1843 irqd_set_activated(irq_data);
1848 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1849 * deactivate interrupt
1850 * @irq_data: outermost irq_data associated with interrupt
1852 * It calls domain_ops->deactivate to program interrupt controllers to disable
1853 * interrupt delivery.
1855 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1857 if (irqd_is_activated(irq_data)) {
1858 __irq_domain_deactivate_irq(irq_data);
1859 irqd_clr_activated(irq_data);
1863 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1865 /* Hierarchy irq_domains must implement callback alloc() */
1866 if (domain->ops->alloc)
1867 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1869 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1871 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1872 * @domain: domain to match
1873 * @virq: IRQ number to get irq_data
1875 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1878 struct irq_data *irq_data = irq_get_irq_data(virq);
1880 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1882 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1885 * irq_domain_set_info - Set the complete data for a @virq in @domain
1886 * @domain: Interrupt domain to match
1888 * @hwirq: The hardware interrupt number
1889 * @chip: The associated interrupt chip
1890 * @chip_data: The associated interrupt chip data
1891 * @handler: The interrupt flow handler
1892 * @handler_data: The interrupt flow handler data
1893 * @handler_name: The interrupt handler name
1895 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1896 irq_hw_number_t hwirq, const struct irq_chip *chip,
1897 void *chip_data, irq_flow_handler_t handler,
1898 void *handler_data, const char *handler_name)
1900 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1901 irq_set_chip_data(virq, chip_data);
1902 irq_set_handler_data(virq, handler_data);
1905 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1906 unsigned int nr_irqs, int node, void *arg,
1907 bool realloc, const struct irq_affinity_desc *affinity)
1912 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1915 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1917 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1918 static struct dentry *domain_dir;
1921 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1923 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1924 seq_printf(m, "%*ssize: %u\n", ind + 1, "", d->revmap_size);
1925 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1926 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1927 if (d->ops && d->ops->debug_show)
1928 d->ops->debug_show(m, d, NULL, ind + 1);
1929 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1932 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1933 irq_domain_debug_show_one(m, d->parent, ind + 4);
1937 static int irq_domain_debug_show(struct seq_file *m, void *p)
1939 struct irq_domain *d = m->private;
1941 /* Default domain? Might be NULL */
1943 if (!irq_default_domain)
1945 d = irq_default_domain;
1947 irq_domain_debug_show_one(m, d, 0);
1950 DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1952 static void debugfs_add_domain_dir(struct irq_domain *d)
1954 if (!d->name || !domain_dir)
1956 debugfs_create_file(d->name, 0444, domain_dir, d,
1957 &irq_domain_debug_fops);
1960 static void debugfs_remove_domain_dir(struct irq_domain *d)
1962 debugfs_lookup_and_remove(d->name, domain_dir);
1965 void __init irq_domain_debugfs_init(struct dentry *root)
1967 struct irq_domain *d;
1969 domain_dir = debugfs_create_dir("domains", root);
1971 debugfs_create_file("default", 0444, domain_dir, NULL,
1972 &irq_domain_debug_fops);
1973 mutex_lock(&irq_domain_mutex);
1974 list_for_each_entry(d, &irq_domain_list, link)
1975 debugfs_add_domain_dir(d);
1976 mutex_unlock(&irq_domain_mutex);