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 const struct fwnode_operations irqchip_fwnode_ops;
49 EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
52 * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
53 * identifying an irq domain
54 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
55 * @name: Optional user provided domain name
56 * @id: Optional user provided id if name != NULL
57 * @pa: Optional user-provided physical address
59 * Allocate a struct irqchip_fwid, and return a poiner to the embedded
60 * fwnode_handle (or NULL on failure).
62 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
63 * solely to transport name information to irqdomain creation code. The
64 * node is not stored. For other types the pointer is kept in the irq
67 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
71 struct irqchip_fwid *fwid;
74 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
77 case IRQCHIP_FWNODE_NAMED:
78 n = kasprintf(GFP_KERNEL, "%s", name);
80 case IRQCHIP_FWNODE_NAMED_ID:
81 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
84 n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
97 fwid->fwnode.ops = &irqchip_fwnode_ops;
100 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
103 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
105 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
107 void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
109 struct irqchip_fwid *fwid;
111 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
114 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
118 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
120 static struct irq_domain *__irq_domain_create(struct fwnode_handle *fwnode,
122 irq_hw_number_t hwirq_max,
124 const struct irq_domain_ops *ops,
127 struct device_node *of_node = to_of_node(fwnode);
128 struct irqchip_fwid *fwid;
129 struct irq_domain *domain;
131 static atomic_t unknown_domains;
133 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
134 GFP_KERNEL, of_node_to_nid(of_node));
138 if (fwnode && is_fwnode_irqchip(fwnode)) {
139 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
141 switch (fwid->type) {
142 case IRQCHIP_FWNODE_NAMED:
143 case IRQCHIP_FWNODE_NAMED_ID:
144 domain->fwnode = fwnode;
145 domain->name = kstrdup(fwid->name, GFP_KERNEL);
150 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
153 domain->fwnode = fwnode;
154 domain->name = fwid->name;
158 } else if (is_acpi_device_node(fwnode)) {
159 struct acpi_buffer buf = {
160 .length = ACPI_ALLOCATE_BUFFER,
164 handle = acpi_device_handle(to_acpi_device_node(fwnode));
165 if (acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf) == AE_OK) {
166 domain->name = buf.pointer;
167 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
170 domain->fwnode = fwnode;
172 } else if (of_node) {
176 * DT paths contain '/', which debugfs is legitimately
177 * unhappy about. Replace them with ':', which does
178 * the trick and is not as offensive as '\'...
180 name = kasprintf(GFP_KERNEL, "%pOF", of_node);
186 strreplace(name, '/', ':');
189 domain->fwnode = fwnode;
190 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
195 pr_err("Invalid fwnode type for irqdomain\n");
196 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
197 atomic_inc_return(&unknown_domains));
202 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
205 of_node_get(of_node);
208 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
209 mutex_init(&domain->revmap_tree_mutex);
211 domain->host_data = host_data;
212 domain->hwirq_max = hwirq_max;
213 domain->revmap_size = size;
214 domain->revmap_direct_max_irq = direct_max;
215 irq_domain_check_hierarchy(domain);
220 static void __irq_domain_publish(struct irq_domain *domain)
222 mutex_lock(&irq_domain_mutex);
223 debugfs_add_domain_dir(domain);
224 list_add(&domain->link, &irq_domain_list);
225 mutex_unlock(&irq_domain_mutex);
227 pr_debug("Added domain %s\n", domain->name);
231 * __irq_domain_add() - Allocate a new irq_domain data structure
232 * @fwnode: firmware node for the interrupt controller
233 * @size: Size of linear map; 0 for radix mapping only
234 * @hwirq_max: Maximum number of interrupts supported by controller
235 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
237 * @ops: domain callbacks
238 * @host_data: Controller private data pointer
240 * Allocates and initializes an irq_domain structure.
241 * Returns pointer to IRQ domain, or NULL on failure.
243 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, unsigned int size,
244 irq_hw_number_t hwirq_max, int direct_max,
245 const struct irq_domain_ops *ops,
248 struct irq_domain *domain;
250 domain = __irq_domain_create(fwnode, size, hwirq_max, direct_max,
253 __irq_domain_publish(domain);
257 EXPORT_SYMBOL_GPL(__irq_domain_add);
260 * irq_domain_remove() - Remove an irq domain.
261 * @domain: domain to remove
263 * This routine is used to remove an irq domain. The caller must ensure
264 * that all mappings within the domain have been disposed of prior to
265 * use, depending on the revmap type.
267 void irq_domain_remove(struct irq_domain *domain)
269 mutex_lock(&irq_domain_mutex);
270 debugfs_remove_domain_dir(domain);
272 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
274 list_del(&domain->link);
277 * If the going away domain is the default one, reset it.
279 if (unlikely(irq_default_domain == domain))
280 irq_set_default_host(NULL);
282 mutex_unlock(&irq_domain_mutex);
284 pr_debug("Removed domain %s\n", domain->name);
286 of_node_put(irq_domain_get_of_node(domain));
287 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
291 EXPORT_SYMBOL_GPL(irq_domain_remove);
293 void irq_domain_update_bus_token(struct irq_domain *domain,
294 enum irq_domain_bus_token bus_token)
298 if (domain->bus_token == bus_token)
301 mutex_lock(&irq_domain_mutex);
303 domain->bus_token = bus_token;
305 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
307 mutex_unlock(&irq_domain_mutex);
311 debugfs_remove_domain_dir(domain);
313 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
316 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
319 debugfs_add_domain_dir(domain);
321 mutex_unlock(&irq_domain_mutex);
325 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
326 * @of_node: pointer to interrupt controller's device tree node.
327 * @size: total number of irqs in mapping
328 * @first_irq: first number of irq block assigned to the domain,
329 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
330 * pre-map all of the irqs in the domain to virqs starting at first_irq.
331 * @ops: domain callbacks
332 * @host_data: Controller private data pointer
334 * Allocates an irq_domain, and optionally if first_irq is positive then also
335 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
337 * This is intended to implement the expected behaviour for most
338 * interrupt controllers. If device tree is used, then first_irq will be 0 and
339 * irqs get mapped dynamically on the fly. However, if the controller requires
340 * static virq assignments (non-DT boot) then it will set that up correctly.
342 struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
344 unsigned int first_irq,
345 const struct irq_domain_ops *ops,
348 struct irq_domain *domain;
350 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
355 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
356 /* attempt to allocated irq_descs */
357 int rc = irq_alloc_descs(first_irq, first_irq, size,
358 of_node_to_nid(of_node));
360 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
363 irq_domain_associate_many(domain, first_irq, 0, size);
368 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
371 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
372 * @of_node: pointer to interrupt controller's device tree node.
373 * @size: total number of irqs in legacy mapping
374 * @first_irq: first number of irq block assigned to the domain
375 * @first_hwirq: first hwirq number to use for the translation. Should normally
376 * be '0', but a positive integer can be used if the effective
377 * hwirqs numbering does not begin at zero.
378 * @ops: map/unmap domain callbacks
379 * @host_data: Controller private data pointer
381 * Note: the map() callback will be called before this function returns
382 * for all legacy interrupts except 0 (which is always the invalid irq for
383 * a legacy controller).
385 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
387 unsigned int first_irq,
388 irq_hw_number_t first_hwirq,
389 const struct irq_domain_ops *ops,
392 struct irq_domain *domain;
394 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
395 first_hwirq + size, 0, ops, host_data);
397 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
401 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
404 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
405 * @fwspec: FW specifier for an interrupt
406 * @bus_token: domain-specific data
408 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
409 enum irq_domain_bus_token bus_token)
411 struct irq_domain *h, *found = NULL;
412 struct fwnode_handle *fwnode = fwspec->fwnode;
415 /* We might want to match the legacy controller last since
416 * it might potentially be set to match all interrupts in
417 * the absence of a device node. This isn't a problem so far
420 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
421 * values must generate an exact match for the domain to be
424 mutex_lock(&irq_domain_mutex);
425 list_for_each_entry(h, &irq_domain_list, link) {
426 if (h->ops->select && fwspec->param_count)
427 rc = h->ops->select(h, fwspec, bus_token);
428 else if (h->ops->match)
429 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
431 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
432 ((bus_token == DOMAIN_BUS_ANY) ||
433 (h->bus_token == bus_token)));
440 mutex_unlock(&irq_domain_mutex);
443 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
446 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
449 * Return: false if any MSI irq domain does not support IRQ remapping,
450 * true otherwise (including if there is no MSI irq domain)
452 bool irq_domain_check_msi_remap(void)
454 struct irq_domain *h;
457 mutex_lock(&irq_domain_mutex);
458 list_for_each_entry(h, &irq_domain_list, link) {
459 if (irq_domain_is_msi(h) &&
460 !irq_domain_hierarchical_is_msi_remap(h)) {
465 mutex_unlock(&irq_domain_mutex);
468 EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
471 * irq_set_default_host() - Set a "default" irq domain
472 * @domain: default domain pointer
474 * For convenience, it's possible to set a "default" domain that will be used
475 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
476 * platforms that want to manipulate a few hard coded interrupt numbers that
477 * aren't properly represented in the device-tree.
479 void irq_set_default_host(struct irq_domain *domain)
481 pr_debug("Default domain set to @0x%p\n", domain);
483 irq_default_domain = domain;
485 EXPORT_SYMBOL_GPL(irq_set_default_host);
488 * irq_get_default_host() - Retrieve the "default" irq domain
490 * Returns: the default domain, if any.
492 * Modern code should never use this. This should only be used on
493 * systems that cannot implement a firmware->fwnode mapping (which
494 * both DT and ACPI provide).
496 struct irq_domain *irq_get_default_host(void)
498 return irq_default_domain;
501 static void irq_domain_clear_mapping(struct irq_domain *domain,
502 irq_hw_number_t hwirq)
504 if (hwirq < domain->revmap_size) {
505 domain->linear_revmap[hwirq] = 0;
507 mutex_lock(&domain->revmap_tree_mutex);
508 radix_tree_delete(&domain->revmap_tree, hwirq);
509 mutex_unlock(&domain->revmap_tree_mutex);
513 static void irq_domain_set_mapping(struct irq_domain *domain,
514 irq_hw_number_t hwirq,
515 struct irq_data *irq_data)
517 if (hwirq < domain->revmap_size) {
518 domain->linear_revmap[hwirq] = irq_data->irq;
520 mutex_lock(&domain->revmap_tree_mutex);
521 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
522 mutex_unlock(&domain->revmap_tree_mutex);
526 void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
528 struct irq_data *irq_data = irq_get_irq_data(irq);
529 irq_hw_number_t hwirq;
531 if (WARN(!irq_data || irq_data->domain != domain,
532 "virq%i doesn't exist; cannot disassociate\n", irq))
535 hwirq = irq_data->hwirq;
537 mutex_lock(&irq_domain_mutex);
539 irq_set_status_flags(irq, IRQ_NOREQUEST);
541 /* remove chip and handler */
542 irq_set_chip_and_handler(irq, NULL, NULL);
544 /* Make sure it's completed */
545 synchronize_irq(irq);
547 /* Tell the PIC about it */
548 if (domain->ops->unmap)
549 domain->ops->unmap(domain, irq);
552 irq_data->domain = NULL;
556 /* Clear reverse map for this hwirq */
557 irq_domain_clear_mapping(domain, hwirq);
559 mutex_unlock(&irq_domain_mutex);
562 static int irq_domain_associate_locked(struct irq_domain *domain, unsigned int virq,
563 irq_hw_number_t hwirq)
565 struct irq_data *irq_data = irq_get_irq_data(virq);
568 if (WARN(hwirq >= domain->hwirq_max,
569 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
571 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
573 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
576 irq_data->hwirq = hwirq;
577 irq_data->domain = domain;
578 if (domain->ops->map) {
579 ret = domain->ops->map(domain, virq, hwirq);
582 * If map() returns -EPERM, this interrupt is protected
583 * by the firmware or some other service and shall not
584 * be mapped. Don't bother telling the user about it.
587 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
588 domain->name, hwirq, virq, ret);
590 irq_data->domain = NULL;
595 /* If not already assigned, give the domain the chip's name */
596 if (!domain->name && irq_data->chip)
597 domain->name = irq_data->chip->name;
601 irq_domain_set_mapping(domain, hwirq, irq_data);
603 irq_clear_status_flags(virq, IRQ_NOREQUEST);
608 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
609 irq_hw_number_t hwirq)
613 mutex_lock(&irq_domain_mutex);
614 ret = irq_domain_associate_locked(domain, virq, hwirq);
615 mutex_unlock(&irq_domain_mutex);
619 EXPORT_SYMBOL_GPL(irq_domain_associate);
621 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
622 irq_hw_number_t hwirq_base, int count)
624 struct device_node *of_node;
627 of_node = irq_domain_get_of_node(domain);
628 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
629 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
631 for (i = 0; i < count; i++) {
632 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
635 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
638 * irq_create_direct_mapping() - Allocate an irq for direct mapping
639 * @domain: domain to allocate the irq for or NULL for default domain
641 * This routine is used for irq controllers which can choose the hardware
642 * interrupt numbers they generate. In such a case it's simplest to use
643 * the linux irq as the hardware interrupt number. It still uses the linear
644 * or radix tree to store the mapping, but the irq controller can optimize
645 * the revmap path by using the hwirq directly.
647 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
649 struct device_node *of_node;
653 domain = irq_default_domain;
655 of_node = irq_domain_get_of_node(domain);
656 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
658 pr_debug("create_direct virq allocation failed\n");
661 if (virq >= domain->revmap_direct_max_irq) {
662 pr_err("ERROR: no free irqs available below %i maximum\n",
663 domain->revmap_direct_max_irq);
667 pr_debug("create_direct obtained virq %d\n", virq);
669 if (irq_domain_associate(domain, virq, virq)) {
676 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
678 static unsigned int irq_create_mapping_affinity_locked(struct irq_domain *domain,
679 irq_hw_number_t hwirq,
680 const struct irq_affinity_desc *affinity)
682 struct device_node *of_node = irq_domain_get_of_node(domain);
685 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
687 /* Allocate a virtual interrupt number */
688 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
691 pr_debug("-> virq allocation failed\n");
695 if (irq_domain_associate_locked(domain, virq, hwirq)) {
700 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
701 hwirq, of_node_full_name(of_node), virq);
707 * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
708 * @domain: domain owning this hardware interrupt or NULL for default domain
709 * @hwirq: hardware irq number in that domain space
710 * @affinity: irq affinity
712 * Only one mapping per hardware interrupt is permitted. Returns a linux
714 * If the sense/trigger is to be specified, set_irq_type() should be called
715 * on the number returned from that call.
717 unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
718 irq_hw_number_t hwirq,
719 const struct irq_affinity_desc *affinity)
723 /* Look for default domain if necessary */
725 domain = irq_default_domain;
726 if (domain == NULL) {
727 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
731 mutex_lock(&irq_domain_mutex);
733 /* Check if mapping already exists */
734 virq = irq_find_mapping(domain, hwirq);
736 pr_debug("existing mapping on virq %d\n", virq);
740 virq = irq_create_mapping_affinity_locked(domain, hwirq, affinity);
742 mutex_unlock(&irq_domain_mutex);
746 EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
749 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
750 * @domain: domain owning the interrupt range
751 * @irq_base: beginning of linux IRQ range
752 * @hwirq_base: beginning of hardware IRQ range
753 * @count: Number of interrupts to map
755 * This routine is used for allocating and mapping a range of hardware
756 * irqs to linux irqs where the linux irq numbers are at pre-defined
757 * locations. For use by controllers that already have static mappings
758 * to insert in to the domain.
760 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
763 * 0 is returned upon success, while any failure to establish a static
764 * mapping is treated as an error.
766 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
767 irq_hw_number_t hwirq_base, int count)
769 struct device_node *of_node;
772 of_node = irq_domain_get_of_node(domain);
773 ret = irq_alloc_descs(irq_base, irq_base, count,
774 of_node_to_nid(of_node));
775 if (unlikely(ret < 0))
778 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
781 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
783 static int irq_domain_translate(struct irq_domain *d,
784 struct irq_fwspec *fwspec,
785 irq_hw_number_t *hwirq, unsigned int *type)
787 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
788 if (d->ops->translate)
789 return d->ops->translate(d, fwspec, hwirq, type);
792 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
793 fwspec->param, fwspec->param_count,
796 /* If domain has no translation, then we assume interrupt line */
797 *hwirq = fwspec->param[0];
801 static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
803 struct irq_fwspec *fwspec)
807 fwspec->fwnode = np ? &np->fwnode : NULL;
808 fwspec->param_count = count;
810 for (i = 0; i < count; i++)
811 fwspec->param[i] = args[i];
814 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
816 struct irq_domain *domain;
817 struct irq_data *irq_data;
818 irq_hw_number_t hwirq;
819 unsigned int type = IRQ_TYPE_NONE;
822 if (fwspec->fwnode) {
823 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
825 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
827 domain = irq_default_domain;
831 pr_warn("no irq domain found for %s !\n",
832 of_node_full_name(to_of_node(fwspec->fwnode)));
836 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
840 * WARN if the irqchip returns a type with bits
841 * outside the sense mask set and clear these bits.
843 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
844 type &= IRQ_TYPE_SENSE_MASK;
846 mutex_lock(&irq_domain_mutex);
849 * If we've already configured this interrupt,
850 * don't do it again, or hell will break loose.
852 virq = irq_find_mapping(domain, hwirq);
855 * If the trigger type is not specified or matches the
856 * current trigger type then we are done so return the
859 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
863 * If the trigger type has not been set yet, then set
864 * it now and return the interrupt number.
866 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
867 irq_data = irq_get_irq_data(virq);
873 irqd_set_trigger_type(irq_data, type);
877 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
878 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
883 if (irq_domain_is_hierarchy(domain)) {
884 virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE,
885 fwspec, false, NULL);
892 virq = irq_create_mapping_affinity_locked(domain, hwirq, NULL);
897 irq_data = irq_get_irq_data(virq);
898 if (WARN_ON(!irq_data)) {
903 /* Store trigger type */
904 irqd_set_trigger_type(irq_data, type);
906 mutex_unlock(&irq_domain_mutex);
910 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
912 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
914 struct irq_fwspec fwspec;
916 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
917 irq_data->args_count, &fwspec);
919 return irq_create_fwspec_mapping(&fwspec);
921 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
924 * irq_dispose_mapping() - Unmap an interrupt
925 * @virq: linux irq number of the interrupt to unmap
927 void irq_dispose_mapping(unsigned int virq)
929 struct irq_data *irq_data = irq_get_irq_data(virq);
930 struct irq_domain *domain;
932 if (!virq || !irq_data)
935 domain = irq_data->domain;
936 if (WARN_ON(domain == NULL))
939 if (irq_domain_is_hierarchy(domain)) {
940 irq_domain_free_irqs(virq, 1);
942 irq_domain_disassociate(domain, virq);
946 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
949 * irq_find_mapping() - Find a linux irq from a hw irq number.
950 * @domain: domain owning this hardware interrupt
951 * @hwirq: hardware irq number in that domain space
953 unsigned int irq_find_mapping(struct irq_domain *domain,
954 irq_hw_number_t hwirq)
956 struct irq_data *data;
958 /* Look for default domain if nececssary */
960 domain = irq_default_domain;
964 if (hwirq < domain->revmap_direct_max_irq) {
965 data = irq_domain_get_irq_data(domain, hwirq);
966 if (data && data->hwirq == hwirq)
970 /* Check if the hwirq is in the linear revmap. */
971 if (hwirq < domain->revmap_size)
972 return domain->linear_revmap[hwirq];
975 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
977 return data ? data->irq : 0;
979 EXPORT_SYMBOL_GPL(irq_find_mapping);
982 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
984 * Device Tree IRQ specifier translation function which works with one cell
985 * bindings where the cell value maps directly to the hwirq number.
987 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
988 const u32 *intspec, unsigned int intsize,
989 unsigned long *out_hwirq, unsigned int *out_type)
991 if (WARN_ON(intsize < 1))
993 *out_hwirq = intspec[0];
994 *out_type = IRQ_TYPE_NONE;
997 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
1000 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
1002 * Device Tree IRQ specifier translation function which works with two cell
1003 * bindings where the cell values map directly to the hwirq number
1004 * and linux irq flags.
1006 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
1007 const u32 *intspec, unsigned int intsize,
1008 irq_hw_number_t *out_hwirq, unsigned int *out_type)
1010 struct irq_fwspec fwspec;
1012 of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
1013 return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
1015 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
1018 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
1020 * Device Tree IRQ specifier translation function which works with either one
1021 * or two cell bindings where the cell values map directly to the hwirq number
1022 * and linux irq flags.
1024 * Note: don't use this function unless your interrupt controller explicitly
1025 * supports both one and two cell bindings. For the majority of controllers
1026 * the _onecell() or _twocell() variants above should be used.
1028 int irq_domain_xlate_onetwocell(struct irq_domain *d,
1029 struct device_node *ctrlr,
1030 const u32 *intspec, unsigned int intsize,
1031 unsigned long *out_hwirq, unsigned int *out_type)
1033 if (WARN_ON(intsize < 1))
1035 *out_hwirq = intspec[0];
1037 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1039 *out_type = IRQ_TYPE_NONE;
1042 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
1044 const struct irq_domain_ops irq_domain_simple_ops = {
1045 .xlate = irq_domain_xlate_onetwocell,
1047 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1050 * irq_domain_translate_twocell() - Generic translate for direct two cell
1053 * Device Tree IRQ specifier translation function which works with two cell
1054 * bindings where the cell values map directly to the hwirq number
1055 * and linux irq flags.
1057 int irq_domain_translate_twocell(struct irq_domain *d,
1058 struct irq_fwspec *fwspec,
1059 unsigned long *out_hwirq,
1060 unsigned int *out_type)
1062 if (WARN_ON(fwspec->param_count < 2))
1064 *out_hwirq = fwspec->param[0];
1065 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1068 EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1070 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1071 int node, const struct irq_affinity_desc *affinity)
1076 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1079 hint = hwirq % nr_irqs;
1082 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1084 if (virq <= 0 && hint > 1) {
1085 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1093 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1095 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1096 * @parent: Parent irq domain to associate with the new domain
1097 * @flags: Irq domain flags associated to the domain
1098 * @size: Size of the domain. See below
1099 * @fwnode: Optional fwnode of the interrupt controller
1100 * @ops: Pointer to the interrupt domain callbacks
1101 * @host_data: Controller private data pointer
1103 * If @size is 0 a tree domain is created, otherwise a linear domain.
1105 * If successful the parent is associated to the new domain and the
1106 * domain flags are set.
1107 * Returns pointer to IRQ domain, or NULL on failure.
1109 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1112 struct fwnode_handle *fwnode,
1113 const struct irq_domain_ops *ops,
1116 struct irq_domain *domain;
1119 domain = __irq_domain_create(fwnode, size, size, 0, ops, host_data);
1121 domain = __irq_domain_create(fwnode, 0, ~0, 0, ops, host_data);
1124 domain->parent = parent;
1125 domain->flags |= flags;
1127 __irq_domain_publish(domain);
1132 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1134 static void irq_domain_insert_irq(int virq)
1136 struct irq_data *data;
1138 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1139 struct irq_domain *domain = data->domain;
1142 irq_domain_set_mapping(domain, data->hwirq, data);
1144 /* If not already assigned, give the domain the chip's name */
1145 if (!domain->name && data->chip)
1146 domain->name = data->chip->name;
1149 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1152 static void irq_domain_remove_irq(int virq)
1154 struct irq_data *data;
1156 irq_set_status_flags(virq, IRQ_NOREQUEST);
1157 irq_set_chip_and_handler(virq, NULL, NULL);
1158 synchronize_irq(virq);
1161 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1162 struct irq_domain *domain = data->domain;
1163 irq_hw_number_t hwirq = data->hwirq;
1166 irq_domain_clear_mapping(domain, hwirq);
1170 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1171 struct irq_data *child)
1173 struct irq_data *irq_data;
1175 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1176 irq_data_get_node(child));
1178 child->parent_data = irq_data;
1179 irq_data->irq = child->irq;
1180 irq_data->common = child->common;
1181 irq_data->domain = domain;
1187 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1189 struct irq_data *irq_data, *tmp;
1192 for (i = 0; i < nr_irqs; i++) {
1193 irq_data = irq_get_irq_data(virq + i);
1194 tmp = irq_data->parent_data;
1195 irq_data->parent_data = NULL;
1196 irq_data->domain = NULL;
1200 tmp = tmp->parent_data;
1206 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1207 unsigned int virq, unsigned int nr_irqs)
1209 struct irq_data *irq_data;
1210 struct irq_domain *parent;
1213 /* The outermost irq_data is embedded in struct irq_desc */
1214 for (i = 0; i < nr_irqs; i++) {
1215 irq_data = irq_get_irq_data(virq + i);
1216 irq_data->domain = domain;
1218 for (parent = domain->parent; parent; parent = parent->parent) {
1219 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1221 irq_domain_free_irq_data(virq, i + 1);
1231 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1232 * @domain: domain to match
1233 * @virq: IRQ number to get irq_data
1235 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1238 struct irq_data *irq_data;
1240 for (irq_data = irq_get_irq_data(virq); irq_data;
1241 irq_data = irq_data->parent_data)
1242 if (irq_data->domain == domain)
1247 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1250 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1251 * @domain: Interrupt domain to match
1253 * @hwirq: The hwirq number
1254 * @chip: The associated interrupt chip
1255 * @chip_data: The associated chip data
1257 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1258 irq_hw_number_t hwirq, struct irq_chip *chip,
1261 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1266 irq_data->hwirq = hwirq;
1267 irq_data->chip = chip ? chip : &no_irq_chip;
1268 irq_data->chip_data = chip_data;
1272 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1275 * irq_domain_set_info - Set the complete data for a @virq in @domain
1276 * @domain: Interrupt domain to match
1278 * @hwirq: The hardware interrupt number
1279 * @chip: The associated interrupt chip
1280 * @chip_data: The associated interrupt chip data
1281 * @handler: The interrupt flow handler
1282 * @handler_data: The interrupt flow handler data
1283 * @handler_name: The interrupt handler name
1285 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1286 irq_hw_number_t hwirq, struct irq_chip *chip,
1287 void *chip_data, irq_flow_handler_t handler,
1288 void *handler_data, const char *handler_name)
1290 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1291 __irq_set_handler(virq, handler, 0, handler_name);
1292 irq_set_handler_data(virq, handler_data);
1294 EXPORT_SYMBOL(irq_domain_set_info);
1297 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1298 * @irq_data: The pointer to irq_data
1300 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1302 irq_data->hwirq = 0;
1303 irq_data->chip = &no_irq_chip;
1304 irq_data->chip_data = NULL;
1306 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1309 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1310 * @domain: Interrupt domain to match
1311 * @virq: IRQ number to start with
1312 * @nr_irqs: The number of irqs to free
1314 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1315 unsigned int nr_irqs)
1317 struct irq_data *irq_data;
1320 for (i = 0; i < nr_irqs; i++) {
1321 irq_data = irq_domain_get_irq_data(domain, virq + i);
1323 irq_domain_reset_irq_data(irq_data);
1325 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1327 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1330 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1331 * @domain: Interrupt domain to match
1332 * @virq: IRQ number to start with
1333 * @nr_irqs: The number of irqs to free
1335 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1336 unsigned int nr_irqs)
1340 for (i = 0; i < nr_irqs; i++) {
1341 irq_set_handler_data(virq + i, NULL);
1342 irq_set_handler(virq + i, NULL);
1344 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1347 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1348 unsigned int irq_base,
1349 unsigned int nr_irqs)
1353 if (!domain->ops->free)
1356 for (i = 0; i < nr_irqs; i++) {
1357 if (irq_domain_get_irq_data(domain, irq_base + i))
1358 domain->ops->free(domain, irq_base + i, 1);
1362 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1363 unsigned int irq_base,
1364 unsigned int nr_irqs, void *arg)
1366 if (!domain->ops->alloc) {
1367 pr_debug("domain->ops->alloc() is NULL\n");
1371 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1374 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1375 unsigned int nr_irqs, int node, void *arg,
1376 bool realloc, const struct irq_affinity_desc *affinity)
1380 if (realloc && irq_base >= 0) {
1383 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1386 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1392 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1393 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1398 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1400 goto out_free_irq_data;
1401 for (i = 0; i < nr_irqs; i++)
1402 irq_domain_insert_irq(virq + i);
1407 irq_domain_free_irq_data(virq, nr_irqs);
1409 irq_free_descs(virq, nr_irqs);
1414 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1415 * @domain: domain to allocate from
1416 * @irq_base: allocate specified IRQ number if irq_base >= 0
1417 * @nr_irqs: number of IRQs to allocate
1418 * @node: NUMA node id for memory allocation
1419 * @arg: domain specific argument
1420 * @realloc: IRQ descriptors have already been allocated if true
1421 * @affinity: Optional irq affinity mask for multiqueue devices
1423 * Allocate IRQ numbers and initialized all data structures to support
1424 * hierarchy IRQ domains.
1425 * Parameter @realloc is mainly to support legacy IRQs.
1426 * Returns error code or allocated IRQ number
1428 * The whole process to setup an IRQ has been split into two steps.
1429 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1430 * descriptor and required hardware resources. The second step,
1431 * irq_domain_activate_irq(), is to program hardwares with preallocated
1432 * resources. In this way, it's easier to rollback when failing to
1433 * allocate resources.
1435 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1436 unsigned int nr_irqs, int node, void *arg,
1437 bool realloc, const struct irq_affinity_desc *affinity)
1441 if (domain == NULL) {
1442 domain = irq_default_domain;
1443 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1447 mutex_lock(&irq_domain_mutex);
1448 ret = irq_domain_alloc_irqs_locked(domain, irq_base, nr_irqs, node, arg,
1450 mutex_unlock(&irq_domain_mutex);
1455 /* The irq_data was moved, fix the revmap to refer to the new location */
1456 static void irq_domain_fix_revmap(struct irq_data *d)
1460 if (d->hwirq < d->domain->revmap_size)
1461 return; /* Not using radix tree. */
1463 /* Fix up the revmap. */
1464 mutex_lock(&d->domain->revmap_tree_mutex);
1465 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1467 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1468 mutex_unlock(&d->domain->revmap_tree_mutex);
1472 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1473 * @domain: Domain to push.
1474 * @virq: Irq to push the domain in to.
1475 * @arg: Passed to the irq_domain_ops alloc() function.
1477 * For an already existing irqdomain hierarchy, as might be obtained
1478 * via a call to pci_enable_msix(), add an additional domain to the
1479 * head of the processing chain. Must be called before request_irq()
1482 int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1484 struct irq_data *child_irq_data;
1485 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1486 struct irq_desc *desc;
1490 * Check that no action has been set, which indicates the virq
1491 * is in a state where this function doesn't have to deal with
1492 * races between interrupt handling and maintaining the
1493 * hierarchy. This will catch gross misuse. Attempting to
1494 * make the check race free would require holding locks across
1495 * calls to struct irq_domain_ops->alloc(), which could lead
1496 * to deadlock, so we just do a simple check before starting.
1498 desc = irq_to_desc(virq);
1501 if (WARN_ON(desc->action))
1507 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1513 if (domain->parent != root_irq_data->domain)
1516 child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
1517 irq_data_get_node(root_irq_data));
1518 if (!child_irq_data)
1521 mutex_lock(&irq_domain_mutex);
1523 /* Copy the original irq_data. */
1524 *child_irq_data = *root_irq_data;
1527 * Overwrite the root_irq_data, which is embedded in struct
1528 * irq_desc, with values for this domain.
1530 root_irq_data->parent_data = child_irq_data;
1531 root_irq_data->domain = domain;
1532 root_irq_data->mask = 0;
1533 root_irq_data->hwirq = 0;
1534 root_irq_data->chip = NULL;
1535 root_irq_data->chip_data = NULL;
1537 /* May (probably does) set hwirq, chip, etc. */
1538 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1540 /* Restore the original irq_data. */
1541 *root_irq_data = *child_irq_data;
1542 kfree(child_irq_data);
1546 irq_domain_fix_revmap(child_irq_data);
1547 irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
1550 mutex_unlock(&irq_domain_mutex);
1554 EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1557 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1558 * @domain: Domain to remove.
1559 * @virq: Irq to remove the domain from.
1561 * Undo the effects of a call to irq_domain_push_irq(). Must be
1562 * called either before request_irq() or after free_irq().
1564 int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1566 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1567 struct irq_data *child_irq_data;
1568 struct irq_data *tmp_irq_data;
1569 struct irq_desc *desc;
1572 * Check that no action is set, which indicates the virq is in
1573 * a state where this function doesn't have to deal with races
1574 * between interrupt handling and maintaining the hierarchy.
1575 * This will catch gross misuse. Attempting to make the check
1576 * race free would require holding locks across calls to
1577 * struct irq_domain_ops->free(), which could lead to
1578 * deadlock, so we just do a simple check before starting.
1580 desc = irq_to_desc(virq);
1583 if (WARN_ON(desc->action))
1592 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1594 /* We can only "pop" if this domain is at the top of the list */
1595 if (WARN_ON(root_irq_data != tmp_irq_data))
1598 if (WARN_ON(root_irq_data->domain != domain))
1601 child_irq_data = root_irq_data->parent_data;
1602 if (WARN_ON(!child_irq_data))
1605 mutex_lock(&irq_domain_mutex);
1607 root_irq_data->parent_data = NULL;
1609 irq_domain_clear_mapping(domain, root_irq_data->hwirq);
1610 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1612 /* Restore the original irq_data. */
1613 *root_irq_data = *child_irq_data;
1615 irq_domain_fix_revmap(root_irq_data);
1617 mutex_unlock(&irq_domain_mutex);
1619 kfree(child_irq_data);
1623 EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1626 * irq_domain_free_irqs - Free IRQ number and associated data structures
1627 * @virq: base IRQ number
1628 * @nr_irqs: number of IRQs to free
1630 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1632 struct irq_data *data = irq_get_irq_data(virq);
1635 if (WARN(!data || !data->domain || !data->domain->ops->free,
1636 "NULL pointer, cannot free irq\n"))
1639 mutex_lock(&irq_domain_mutex);
1640 for (i = 0; i < nr_irqs; i++)
1641 irq_domain_remove_irq(virq + i);
1642 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
1643 mutex_unlock(&irq_domain_mutex);
1645 irq_domain_free_irq_data(virq, nr_irqs);
1646 irq_free_descs(virq, nr_irqs);
1650 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1651 * @irq_base: Base IRQ number
1652 * @nr_irqs: Number of IRQs to allocate
1653 * @arg: Allocation data (arch/domain specific)
1655 * Check whether the domain has been setup recursive. If not allocate
1656 * through the parent domain.
1658 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1659 unsigned int irq_base, unsigned int nr_irqs,
1662 if (!domain->parent)
1665 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1668 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1671 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1672 * @irq_base: Base IRQ number
1673 * @nr_irqs: Number of IRQs to free
1675 * Check whether the domain has been setup recursive. If not free
1676 * through the parent domain.
1678 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1679 unsigned int irq_base, unsigned int nr_irqs)
1681 if (!domain->parent)
1684 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1686 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1688 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1690 if (irq_data && irq_data->domain) {
1691 struct irq_domain *domain = irq_data->domain;
1693 if (domain->ops->deactivate)
1694 domain->ops->deactivate(domain, irq_data);
1695 if (irq_data->parent_data)
1696 __irq_domain_deactivate_irq(irq_data->parent_data);
1700 static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1704 if (irqd && irqd->domain) {
1705 struct irq_domain *domain = irqd->domain;
1707 if (irqd->parent_data)
1708 ret = __irq_domain_activate_irq(irqd->parent_data,
1710 if (!ret && domain->ops->activate) {
1711 ret = domain->ops->activate(domain, irqd, reserve);
1712 /* Rollback in case of error */
1713 if (ret && irqd->parent_data)
1714 __irq_domain_deactivate_irq(irqd->parent_data);
1721 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1723 * @irq_data: Outermost irq_data associated with interrupt
1724 * @reserve: If set only reserve an interrupt vector instead of assigning one
1726 * This is the second step to call domain_ops->activate to program interrupt
1727 * controllers, so the interrupt could actually get delivered.
1729 int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1733 if (!irqd_is_activated(irq_data))
1734 ret = __irq_domain_activate_irq(irq_data, reserve);
1736 irqd_set_activated(irq_data);
1741 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1742 * deactivate interrupt
1743 * @irq_data: outermost irq_data associated with interrupt
1745 * It calls domain_ops->deactivate to program interrupt controllers to disable
1746 * interrupt delivery.
1748 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1750 if (irqd_is_activated(irq_data)) {
1751 __irq_domain_deactivate_irq(irq_data);
1752 irqd_clr_activated(irq_data);
1756 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1758 /* Hierarchy irq_domains must implement callback alloc() */
1759 if (domain->ops->alloc)
1760 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1764 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1765 * parent has MSI remapping support
1766 * @domain: domain pointer
1768 bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1770 for (; domain; domain = domain->parent) {
1771 if (irq_domain_is_msi_remap(domain))
1776 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1778 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1779 * @domain: domain to match
1780 * @virq: IRQ number to get irq_data
1782 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1785 struct irq_data *irq_data = irq_get_irq_data(virq);
1787 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1789 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1792 * irq_domain_set_info - Set the complete data for a @virq in @domain
1793 * @domain: Interrupt domain to match
1795 * @hwirq: The hardware interrupt number
1796 * @chip: The associated interrupt chip
1797 * @chip_data: The associated interrupt chip data
1798 * @handler: The interrupt flow handler
1799 * @handler_data: The interrupt flow handler data
1800 * @handler_name: The interrupt handler name
1802 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1803 irq_hw_number_t hwirq, struct irq_chip *chip,
1804 void *chip_data, irq_flow_handler_t handler,
1805 void *handler_data, const char *handler_name)
1807 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1808 irq_set_chip_data(virq, chip_data);
1809 irq_set_handler_data(virq, handler_data);
1812 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1813 unsigned int nr_irqs, int node, void *arg,
1814 bool realloc, const struct irq_affinity_desc *affinity)
1819 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1822 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1824 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1825 static struct dentry *domain_dir;
1828 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1830 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1831 seq_printf(m, "%*ssize: %u\n", ind + 1, "",
1832 d->revmap_size + d->revmap_direct_max_irq);
1833 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1834 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1835 if (d->ops && d->ops->debug_show)
1836 d->ops->debug_show(m, d, NULL, ind + 1);
1837 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1840 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1841 irq_domain_debug_show_one(m, d->parent, ind + 4);
1845 static int irq_domain_debug_show(struct seq_file *m, void *p)
1847 struct irq_domain *d = m->private;
1849 /* Default domain? Might be NULL */
1851 if (!irq_default_domain)
1853 d = irq_default_domain;
1855 irq_domain_debug_show_one(m, d, 0);
1858 DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1860 static void debugfs_add_domain_dir(struct irq_domain *d)
1862 if (!d->name || !domain_dir || d->debugfs_file)
1864 d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
1865 &irq_domain_debug_fops);
1868 static void debugfs_remove_domain_dir(struct irq_domain *d)
1870 debugfs_remove(d->debugfs_file);
1871 d->debugfs_file = NULL;
1874 void __init irq_domain_debugfs_init(struct dentry *root)
1876 struct irq_domain *d;
1878 domain_dir = debugfs_create_dir("domains", root);
1880 debugfs_create_file("default", 0444, domain_dir, NULL,
1881 &irq_domain_debug_fops);
1882 mutex_lock(&irq_domain_mutex);
1883 list_for_each_entry(d, &irq_domain_list, link)
1884 debugfs_add_domain_dir(d);
1885 mutex_unlock(&irq_domain_mutex);