1 #define pr_fmt(fmt) "irq: " fmt
3 #include <linux/acpi.h>
4 #include <linux/debugfs.h>
5 #include <linux/hardirq.h>
6 #include <linux/interrupt.h>
8 #include <linux/irqdesc.h>
9 #include <linux/irqdomain.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/topology.h>
16 #include <linux/seq_file.h>
17 #include <linux/slab.h>
18 #include <linux/smp.h>
21 static LIST_HEAD(irq_domain_list);
22 static DEFINE_MUTEX(irq_domain_mutex);
24 static DEFINE_MUTEX(revmap_trees_mutex);
25 static struct irq_domain *irq_default_domain;
27 static void irq_domain_check_hierarchy(struct irq_domain *domain);
30 struct fwnode_handle fwnode;
36 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
37 static void debugfs_add_domain_dir(struct irq_domain *d);
38 static void debugfs_remove_domain_dir(struct irq_domain *d);
40 static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
41 static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
44 const struct fwnode_operations irqchip_fwnode_ops;
45 EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
48 * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
49 * identifying an irq domain
50 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
51 * @name: Optional user provided domain name
52 * @id: Optional user provided id if name != NULL
53 * @data: Optional user-provided data
55 * Allocate a struct irqchip_fwid, and return a poiner to the embedded
56 * fwnode_handle (or NULL on failure).
58 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
59 * solely to transport name information to irqdomain creation code. The
60 * node is not stored. For other types the pointer is kept in the irq
63 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
64 const char *name, void *data)
66 struct irqchip_fwid *fwid;
69 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
72 case IRQCHIP_FWNODE_NAMED:
73 n = kasprintf(GFP_KERNEL, "%s", name);
75 case IRQCHIP_FWNODE_NAMED_ID:
76 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
79 n = kasprintf(GFP_KERNEL, "irqchip@%p", data);
92 fwid->fwnode.ops = &irqchip_fwnode_ops;
95 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
98 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
100 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
102 void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
104 struct irqchip_fwid *fwid;
106 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
109 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
113 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
116 * __irq_domain_add() - Allocate a new irq_domain data structure
117 * @fwnode: firmware node for the interrupt controller
118 * @size: Size of linear map; 0 for radix mapping only
119 * @hwirq_max: Maximum number of interrupts supported by controller
120 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
122 * @ops: domain callbacks
123 * @host_data: Controller private data pointer
125 * Allocates and initialize and irq_domain structure.
126 * Returns pointer to IRQ domain, or NULL on failure.
128 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
129 irq_hw_number_t hwirq_max, int direct_max,
130 const struct irq_domain_ops *ops,
133 struct device_node *of_node = to_of_node(fwnode);
134 struct irqchip_fwid *fwid;
135 struct irq_domain *domain;
137 static atomic_t unknown_domains;
139 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
140 GFP_KERNEL, of_node_to_nid(of_node));
141 if (WARN_ON(!domain))
144 if (fwnode && is_fwnode_irqchip(fwnode)) {
145 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
147 switch (fwid->type) {
148 case IRQCHIP_FWNODE_NAMED:
149 case IRQCHIP_FWNODE_NAMED_ID:
150 domain->fwnode = fwnode;
151 domain->name = kstrdup(fwid->name, GFP_KERNEL);
156 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
159 domain->fwnode = fwnode;
160 domain->name = fwid->name;
164 } else if (is_acpi_device_node(fwnode)) {
165 struct acpi_buffer buf = {
166 .length = ACPI_ALLOCATE_BUFFER,
170 handle = acpi_device_handle(to_acpi_device_node(fwnode));
171 if (acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf) == AE_OK) {
172 domain->name = buf.pointer;
173 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
176 domain->fwnode = fwnode;
178 } else if (of_node) {
182 * DT paths contain '/', which debugfs is legitimately
183 * unhappy about. Replace them with ':', which does
184 * the trick and is not as offensive as '\'...
186 name = kstrdup(of_node_full_name(of_node), GFP_KERNEL);
192 strreplace(name, '/', ':');
195 domain->fwnode = fwnode;
196 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
201 pr_err("Invalid fwnode type for irqdomain\n");
202 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
203 atomic_inc_return(&unknown_domains));
208 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
211 of_node_get(of_node);
214 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
216 domain->host_data = host_data;
217 domain->hwirq_max = hwirq_max;
218 domain->revmap_size = size;
219 domain->revmap_direct_max_irq = direct_max;
220 irq_domain_check_hierarchy(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);
230 EXPORT_SYMBOL_GPL(__irq_domain_add);
233 * irq_domain_remove() - Remove an irq domain.
234 * @domain: domain to remove
236 * This routine is used to remove an irq domain. The caller must ensure
237 * that all mappings within the domain have been disposed of prior to
238 * use, depending on the revmap type.
240 void irq_domain_remove(struct irq_domain *domain)
242 mutex_lock(&irq_domain_mutex);
243 debugfs_remove_domain_dir(domain);
245 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
247 list_del(&domain->link);
250 * If the going away domain is the default one, reset it.
252 if (unlikely(irq_default_domain == domain))
253 irq_set_default_host(NULL);
255 mutex_unlock(&irq_domain_mutex);
257 pr_debug("Removed domain %s\n", domain->name);
259 of_node_put(irq_domain_get_of_node(domain));
260 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
264 EXPORT_SYMBOL_GPL(irq_domain_remove);
266 void irq_domain_update_bus_token(struct irq_domain *domain,
267 enum irq_domain_bus_token bus_token)
271 if (domain->bus_token == bus_token)
274 mutex_lock(&irq_domain_mutex);
276 domain->bus_token = bus_token;
278 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
280 mutex_unlock(&irq_domain_mutex);
284 debugfs_remove_domain_dir(domain);
286 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
289 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
292 debugfs_add_domain_dir(domain);
294 mutex_unlock(&irq_domain_mutex);
298 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
299 * @of_node: pointer to interrupt controller's device tree node.
300 * @size: total number of irqs in mapping
301 * @first_irq: first number of irq block assigned to the domain,
302 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
303 * pre-map all of the irqs in the domain to virqs starting at first_irq.
304 * @ops: domain callbacks
305 * @host_data: Controller private data pointer
307 * Allocates an irq_domain, and optionally if first_irq is positive then also
308 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
310 * This is intended to implement the expected behaviour for most
311 * interrupt controllers. If device tree is used, then first_irq will be 0 and
312 * irqs get mapped dynamically on the fly. However, if the controller requires
313 * static virq assignments (non-DT boot) then it will set that up correctly.
315 struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
317 unsigned int first_irq,
318 const struct irq_domain_ops *ops,
321 struct irq_domain *domain;
323 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
328 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
329 /* attempt to allocated irq_descs */
330 int rc = irq_alloc_descs(first_irq, first_irq, size,
331 of_node_to_nid(of_node));
333 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
336 irq_domain_associate_many(domain, first_irq, 0, size);
341 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
344 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
345 * @of_node: pointer to interrupt controller's device tree node.
346 * @size: total number of irqs in legacy mapping
347 * @first_irq: first number of irq block assigned to the domain
348 * @first_hwirq: first hwirq number to use for the translation. Should normally
349 * be '0', but a positive integer can be used if the effective
350 * hwirqs numbering does not begin at zero.
351 * @ops: map/unmap domain callbacks
352 * @host_data: Controller private data pointer
354 * Note: the map() callback will be called before this function returns
355 * for all legacy interrupts except 0 (which is always the invalid irq for
356 * a legacy controller).
358 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
360 unsigned int first_irq,
361 irq_hw_number_t first_hwirq,
362 const struct irq_domain_ops *ops,
365 struct irq_domain *domain;
367 domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
368 first_hwirq + size, 0, ops, host_data);
370 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
374 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
377 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
378 * @fwspec: FW specifier for an interrupt
379 * @bus_token: domain-specific data
381 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
382 enum irq_domain_bus_token bus_token)
384 struct irq_domain *h, *found = NULL;
385 struct fwnode_handle *fwnode = fwspec->fwnode;
388 /* We might want to match the legacy controller last since
389 * it might potentially be set to match all interrupts in
390 * the absence of a device node. This isn't a problem so far
393 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
394 * values must generate an exact match for the domain to be
397 mutex_lock(&irq_domain_mutex);
398 list_for_each_entry(h, &irq_domain_list, link) {
399 if (h->ops->select && fwspec->param_count)
400 rc = h->ops->select(h, fwspec, bus_token);
401 else if (h->ops->match)
402 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
404 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
405 ((bus_token == DOMAIN_BUS_ANY) ||
406 (h->bus_token == bus_token)));
413 mutex_unlock(&irq_domain_mutex);
416 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
419 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
422 * Return: false if any MSI irq domain does not support IRQ remapping,
423 * true otherwise (including if there is no MSI irq domain)
425 bool irq_domain_check_msi_remap(void)
427 struct irq_domain *h;
430 mutex_lock(&irq_domain_mutex);
431 list_for_each_entry(h, &irq_domain_list, link) {
432 if (irq_domain_is_msi(h) &&
433 !irq_domain_hierarchical_is_msi_remap(h)) {
438 mutex_unlock(&irq_domain_mutex);
441 EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
444 * irq_set_default_host() - Set a "default" irq domain
445 * @domain: default domain pointer
447 * For convenience, it's possible to set a "default" domain that will be used
448 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
449 * platforms that want to manipulate a few hard coded interrupt numbers that
450 * aren't properly represented in the device-tree.
452 void irq_set_default_host(struct irq_domain *domain)
454 pr_debug("Default domain set to @0x%p\n", domain);
456 irq_default_domain = domain;
458 EXPORT_SYMBOL_GPL(irq_set_default_host);
460 static void irq_domain_clear_mapping(struct irq_domain *domain,
461 irq_hw_number_t hwirq)
463 if (hwirq < domain->revmap_size) {
464 domain->linear_revmap[hwirq] = 0;
466 mutex_lock(&revmap_trees_mutex);
467 radix_tree_delete(&domain->revmap_tree, hwirq);
468 mutex_unlock(&revmap_trees_mutex);
472 static void irq_domain_set_mapping(struct irq_domain *domain,
473 irq_hw_number_t hwirq,
474 struct irq_data *irq_data)
476 if (hwirq < domain->revmap_size) {
477 domain->linear_revmap[hwirq] = irq_data->irq;
479 mutex_lock(&revmap_trees_mutex);
480 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
481 mutex_unlock(&revmap_trees_mutex);
485 void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
487 struct irq_data *irq_data = irq_get_irq_data(irq);
488 irq_hw_number_t hwirq;
490 if (WARN(!irq_data || irq_data->domain != domain,
491 "virq%i doesn't exist; cannot disassociate\n", irq))
494 hwirq = irq_data->hwirq;
496 mutex_lock(&irq_domain_mutex);
498 irq_set_status_flags(irq, IRQ_NOREQUEST);
500 /* remove chip and handler */
501 irq_set_chip_and_handler(irq, NULL, NULL);
503 /* Make sure it's completed */
504 synchronize_irq(irq);
506 /* Tell the PIC about it */
507 if (domain->ops->unmap)
508 domain->ops->unmap(domain, irq);
511 irq_data->domain = NULL;
515 /* Clear reverse map for this hwirq */
516 irq_domain_clear_mapping(domain, hwirq);
518 mutex_unlock(&irq_domain_mutex);
521 static int irq_domain_associate_locked(struct irq_domain *domain, unsigned int virq,
522 irq_hw_number_t hwirq)
524 struct irq_data *irq_data = irq_get_irq_data(virq);
527 if (WARN(hwirq >= domain->hwirq_max,
528 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
530 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
532 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
535 irq_data->hwirq = hwirq;
536 irq_data->domain = domain;
537 if (domain->ops->map) {
538 ret = domain->ops->map(domain, virq, hwirq);
541 * If map() returns -EPERM, this interrupt is protected
542 * by the firmware or some other service and shall not
543 * be mapped. Don't bother telling the user about it.
546 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
547 domain->name, hwirq, virq, ret);
549 irq_data->domain = NULL;
554 /* If not already assigned, give the domain the chip's name */
555 if (!domain->name && irq_data->chip)
556 domain->name = irq_data->chip->name;
560 irq_domain_set_mapping(domain, hwirq, irq_data);
562 irq_clear_status_flags(virq, IRQ_NOREQUEST);
567 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
568 irq_hw_number_t hwirq)
572 mutex_lock(&irq_domain_mutex);
573 ret = irq_domain_associate_locked(domain, virq, hwirq);
574 mutex_unlock(&irq_domain_mutex);
578 EXPORT_SYMBOL_GPL(irq_domain_associate);
580 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
581 irq_hw_number_t hwirq_base, int count)
583 struct device_node *of_node;
586 of_node = irq_domain_get_of_node(domain);
587 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
588 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
590 for (i = 0; i < count; i++) {
591 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
594 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
597 * irq_create_direct_mapping() - Allocate an irq for direct mapping
598 * @domain: domain to allocate the irq for or NULL for default domain
600 * This routine is used for irq controllers which can choose the hardware
601 * interrupt numbers they generate. In such a case it's simplest to use
602 * the linux irq as the hardware interrupt number. It still uses the linear
603 * or radix tree to store the mapping, but the irq controller can optimize
604 * the revmap path by using the hwirq directly.
606 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
608 struct device_node *of_node;
612 domain = irq_default_domain;
614 of_node = irq_domain_get_of_node(domain);
615 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
617 pr_debug("create_direct virq allocation failed\n");
620 if (virq >= domain->revmap_direct_max_irq) {
621 pr_err("ERROR: no free irqs available below %i maximum\n",
622 domain->revmap_direct_max_irq);
626 pr_debug("create_direct obtained virq %d\n", virq);
628 if (irq_domain_associate(domain, virq, virq)) {
635 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
638 * irq_create_mapping() - Map a hardware interrupt into linux irq space
639 * @domain: domain owning this hardware interrupt or NULL for default domain
640 * @hwirq: hardware irq number in that domain space
642 * Only one mapping per hardware interrupt is permitted. Returns a linux
644 * If the sense/trigger is to be specified, set_irq_type() should be called
645 * on the number returned from that call.
647 unsigned int irq_create_mapping(struct irq_domain *domain,
648 irq_hw_number_t hwirq)
650 struct device_node *of_node;
653 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
655 /* Look for default domain if nececssary */
657 domain = irq_default_domain;
658 if (domain == NULL) {
659 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
662 pr_debug("-> using domain @%p\n", domain);
664 of_node = irq_domain_get_of_node(domain);
666 /* Check if mapping already exists */
667 virq = irq_find_mapping(domain, hwirq);
669 pr_debug("-> existing mapping on virq %d\n", virq);
673 /* Allocate a virtual interrupt number */
674 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL);
676 pr_debug("-> virq allocation failed\n");
680 if (irq_domain_associate(domain, virq, hwirq)) {
685 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
686 hwirq, of_node_full_name(of_node), virq);
690 EXPORT_SYMBOL_GPL(irq_create_mapping);
693 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
694 * @domain: domain owning the interrupt range
695 * @irq_base: beginning of linux IRQ range
696 * @hwirq_base: beginning of hardware IRQ range
697 * @count: Number of interrupts to map
699 * This routine is used for allocating and mapping a range of hardware
700 * irqs to linux irqs where the linux irq numbers are at pre-defined
701 * locations. For use by controllers that already have static mappings
702 * to insert in to the domain.
704 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
707 * 0 is returned upon success, while any failure to establish a static
708 * mapping is treated as an error.
710 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
711 irq_hw_number_t hwirq_base, int count)
713 struct device_node *of_node;
716 of_node = irq_domain_get_of_node(domain);
717 ret = irq_alloc_descs(irq_base, irq_base, count,
718 of_node_to_nid(of_node));
719 if (unlikely(ret < 0))
722 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
725 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
727 static int irq_domain_translate(struct irq_domain *d,
728 struct irq_fwspec *fwspec,
729 irq_hw_number_t *hwirq, unsigned int *type)
731 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
732 if (d->ops->translate)
733 return d->ops->translate(d, fwspec, hwirq, type);
736 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
737 fwspec->param, fwspec->param_count,
740 /* If domain has no translation, then we assume interrupt line */
741 *hwirq = fwspec->param[0];
745 static void of_phandle_args_to_fwspec(struct of_phandle_args *irq_data,
746 struct irq_fwspec *fwspec)
750 fwspec->fwnode = irq_data->np ? &irq_data->np->fwnode : NULL;
751 fwspec->param_count = irq_data->args_count;
753 for (i = 0; i < irq_data->args_count; i++)
754 fwspec->param[i] = irq_data->args[i];
757 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
759 struct irq_domain *domain;
760 struct irq_data *irq_data;
761 irq_hw_number_t hwirq;
762 unsigned int type = IRQ_TYPE_NONE;
765 if (fwspec->fwnode) {
766 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
768 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
770 domain = irq_default_domain;
774 pr_warn("no irq domain found for %s !\n",
775 of_node_full_name(to_of_node(fwspec->fwnode)));
779 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
783 * WARN if the irqchip returns a type with bits
784 * outside the sense mask set and clear these bits.
786 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
787 type &= IRQ_TYPE_SENSE_MASK;
790 * If we've already configured this interrupt,
791 * don't do it again, or hell will break loose.
793 virq = irq_find_mapping(domain, hwirq);
796 * If the trigger type is not specified or matches the
797 * current trigger type then we are done so return the
800 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
804 * If the trigger type has not been set yet, then set
805 * it now and return the interrupt number.
807 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
808 irq_data = irq_get_irq_data(virq);
812 irqd_set_trigger_type(irq_data, type);
816 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
817 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
821 if (irq_domain_is_hierarchy(domain)) {
822 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
827 virq = irq_create_mapping(domain, hwirq);
832 irq_data = irq_get_irq_data(virq);
833 if (WARN_ON(!irq_data))
836 /* Store trigger type */
837 irqd_set_trigger_type(irq_data, type);
841 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
843 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
845 struct irq_fwspec fwspec;
847 of_phandle_args_to_fwspec(irq_data, &fwspec);
848 return irq_create_fwspec_mapping(&fwspec);
850 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
853 * irq_dispose_mapping() - Unmap an interrupt
854 * @virq: linux irq number of the interrupt to unmap
856 void irq_dispose_mapping(unsigned int virq)
858 struct irq_data *irq_data = irq_get_irq_data(virq);
859 struct irq_domain *domain;
861 if (!virq || !irq_data)
864 domain = irq_data->domain;
865 if (WARN_ON(domain == NULL))
868 if (irq_domain_is_hierarchy(domain)) {
869 irq_domain_free_irqs(virq, 1);
871 irq_domain_disassociate(domain, virq);
875 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
878 * irq_find_mapping() - Find a linux irq from an hw irq number.
879 * @domain: domain owning this hardware interrupt
880 * @hwirq: hardware irq number in that domain space
882 unsigned int irq_find_mapping(struct irq_domain *domain,
883 irq_hw_number_t hwirq)
885 struct irq_data *data;
887 /* Look for default domain if nececssary */
889 domain = irq_default_domain;
893 if (hwirq < domain->revmap_direct_max_irq) {
894 data = irq_domain_get_irq_data(domain, hwirq);
895 if (data && data->hwirq == hwirq)
899 /* Check if the hwirq is in the linear revmap. */
900 if (hwirq < domain->revmap_size)
901 return domain->linear_revmap[hwirq];
904 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
906 return data ? data->irq : 0;
908 EXPORT_SYMBOL_GPL(irq_find_mapping);
910 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
911 static void virq_debug_show_one(struct seq_file *m, struct irq_desc *desc)
913 struct irq_domain *domain;
914 struct irq_data *data;
916 domain = desc->irq_data.domain;
917 data = &desc->irq_data;
920 unsigned int irq = data->irq;
921 unsigned long hwirq = data->hwirq;
922 struct irq_chip *chip;
925 if (data == &desc->irq_data)
926 seq_printf(m, "%5d ", irq);
928 seq_printf(m, "%5d+ ", irq);
929 seq_printf(m, "0x%05lx ", hwirq);
931 chip = irq_data_get_irq_chip(data);
932 seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
934 seq_printf(m, data ? "0x%p " : " %p ",
935 irq_data_get_irq_chip_data(data));
937 seq_printf(m, " %c ", (desc->action && desc->action->handler) ? '*' : ' ');
938 direct = (irq == hwirq) && (irq < domain->revmap_direct_max_irq);
939 seq_printf(m, "%6s%-8s ",
940 (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
941 direct ? "(DIRECT)" : "");
942 seq_printf(m, "%s\n", domain->name);
943 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
944 domain = domain->parent;
945 data = data->parent_data;
952 static int virq_debug_show(struct seq_file *m, void *private)
955 struct irq_desc *desc;
956 struct irq_domain *domain;
957 struct radix_tree_iter iter;
961 seq_printf(m, " %-16s %-6s %-10s %-10s %s\n",
962 "name", "mapped", "linear-max", "direct-max", "devtree-node");
963 mutex_lock(&irq_domain_mutex);
964 list_for_each_entry(domain, &irq_domain_list, link) {
965 struct device_node *of_node;
970 of_node = irq_domain_get_of_node(domain);
972 name = of_node_full_name(of_node);
973 else if (is_fwnode_irqchip(domain->fwnode))
974 name = container_of(domain->fwnode, struct irqchip_fwid,
979 radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
981 seq_printf(m, "%c%-16s %6u %10u %10u %s\n",
982 domain == irq_default_domain ? '*' : ' ', domain->name,
983 domain->revmap_size + count, domain->revmap_size,
984 domain->revmap_direct_max_irq,
987 mutex_unlock(&irq_domain_mutex);
989 seq_printf(m, "%-5s %-7s %-15s %-*s %6s %-14s %s\n", "irq", "hwirq",
990 "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
991 "active", "type", "domain");
993 for (i = 1; i < nr_irqs; i++) {
994 desc = irq_to_desc(i);
998 raw_spin_lock_irqsave(&desc->lock, flags);
999 virq_debug_show_one(m, desc);
1000 raw_spin_unlock_irqrestore(&desc->lock, flags);
1006 static int virq_debug_open(struct inode *inode, struct file *file)
1008 return single_open(file, virq_debug_show, inode->i_private);
1011 static const struct file_operations virq_debug_fops = {
1012 .open = virq_debug_open,
1014 .llseek = seq_lseek,
1015 .release = single_release,
1018 static int __init irq_debugfs_init(void)
1020 if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
1021 NULL, &virq_debug_fops) == NULL)
1026 __initcall(irq_debugfs_init);
1027 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
1030 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
1032 * Device Tree IRQ specifier translation function which works with one cell
1033 * bindings where the cell value maps directly to the hwirq number.
1035 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
1036 const u32 *intspec, unsigned int intsize,
1037 unsigned long *out_hwirq, unsigned int *out_type)
1039 if (WARN_ON(intsize < 1))
1041 *out_hwirq = intspec[0];
1042 *out_type = IRQ_TYPE_NONE;
1045 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
1048 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
1050 * Device Tree IRQ specifier translation function which works with two cell
1051 * bindings where the cell values map directly to the hwirq number
1052 * and linux irq flags.
1054 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
1055 const u32 *intspec, unsigned int intsize,
1056 irq_hw_number_t *out_hwirq, unsigned int *out_type)
1058 if (WARN_ON(intsize < 2))
1060 *out_hwirq = intspec[0];
1061 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1064 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
1067 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
1069 * Device Tree IRQ specifier translation function which works with either one
1070 * or two cell bindings where the cell values map directly to the hwirq number
1071 * and linux irq flags.
1073 * Note: don't use this function unless your interrupt controller explicitly
1074 * supports both one and two cell bindings. For the majority of controllers
1075 * the _onecell() or _twocell() variants above should be used.
1077 int irq_domain_xlate_onetwocell(struct irq_domain *d,
1078 struct device_node *ctrlr,
1079 const u32 *intspec, unsigned int intsize,
1080 unsigned long *out_hwirq, unsigned int *out_type)
1082 if (WARN_ON(intsize < 1))
1084 *out_hwirq = intspec[0];
1086 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1088 *out_type = IRQ_TYPE_NONE;
1091 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
1093 const struct irq_domain_ops irq_domain_simple_ops = {
1094 .xlate = irq_domain_xlate_onetwocell,
1096 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1098 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1099 int node, const struct cpumask *affinity)
1104 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1107 hint = hwirq % nr_irqs;
1110 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1112 if (virq <= 0 && hint > 1) {
1113 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1121 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1123 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1124 * @parent: Parent irq domain to associate with the new domain
1125 * @flags: Irq domain flags associated to the domain
1126 * @size: Size of the domain. See below
1127 * @fwnode: Optional fwnode of the interrupt controller
1128 * @ops: Pointer to the interrupt domain callbacks
1129 * @host_data: Controller private data pointer
1131 * If @size is 0 a tree domain is created, otherwise a linear domain.
1133 * If successful the parent is associated to the new domain and the
1134 * domain flags are set.
1135 * Returns pointer to IRQ domain, or NULL on failure.
1137 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1140 struct fwnode_handle *fwnode,
1141 const struct irq_domain_ops *ops,
1144 struct irq_domain *domain;
1147 domain = irq_domain_create_linear(fwnode, size, ops, host_data);
1149 domain = irq_domain_create_tree(fwnode, ops, host_data);
1151 domain->parent = parent;
1152 domain->flags |= flags;
1157 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1159 static void irq_domain_insert_irq(int virq)
1161 struct irq_data *data;
1163 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1164 struct irq_domain *domain = data->domain;
1167 irq_domain_set_mapping(domain, data->hwirq, data);
1169 /* If not already assigned, give the domain the chip's name */
1170 if (!domain->name && data->chip)
1171 domain->name = data->chip->name;
1174 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1177 static void irq_domain_remove_irq(int virq)
1179 struct irq_data *data;
1181 irq_set_status_flags(virq, IRQ_NOREQUEST);
1182 irq_set_chip_and_handler(virq, NULL, NULL);
1183 synchronize_irq(virq);
1186 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1187 struct irq_domain *domain = data->domain;
1188 irq_hw_number_t hwirq = data->hwirq;
1191 irq_domain_clear_mapping(domain, hwirq);
1195 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1196 struct irq_data *child)
1198 struct irq_data *irq_data;
1200 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1201 irq_data_get_node(child));
1203 child->parent_data = irq_data;
1204 irq_data->irq = child->irq;
1205 irq_data->common = child->common;
1206 irq_data->domain = domain;
1212 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1214 struct irq_data *irq_data, *tmp;
1217 for (i = 0; i < nr_irqs; i++) {
1218 irq_data = irq_get_irq_data(virq + i);
1219 tmp = irq_data->parent_data;
1220 irq_data->parent_data = NULL;
1221 irq_data->domain = NULL;
1225 tmp = tmp->parent_data;
1231 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1232 unsigned int virq, unsigned int nr_irqs)
1234 struct irq_data *irq_data;
1235 struct irq_domain *parent;
1238 /* The outermost irq_data is embedded in struct irq_desc */
1239 for (i = 0; i < nr_irqs; i++) {
1240 irq_data = irq_get_irq_data(virq + i);
1241 irq_data->domain = domain;
1243 for (parent = domain->parent; parent; parent = parent->parent) {
1244 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1246 irq_domain_free_irq_data(virq, i + 1);
1256 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1257 * @domain: domain to match
1258 * @virq: IRQ number to get irq_data
1260 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1263 struct irq_data *irq_data;
1265 for (irq_data = irq_get_irq_data(virq); irq_data;
1266 irq_data = irq_data->parent_data)
1267 if (irq_data->domain == domain)
1272 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1275 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1276 * @domain: Interrupt domain to match
1278 * @hwirq: The hwirq number
1279 * @chip: The associated interrupt chip
1280 * @chip_data: The associated chip data
1282 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1283 irq_hw_number_t hwirq, struct irq_chip *chip,
1286 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1291 irq_data->hwirq = hwirq;
1292 irq_data->chip = chip ? chip : &no_irq_chip;
1293 irq_data->chip_data = chip_data;
1297 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1300 * irq_domain_set_info - Set the complete data for a @virq in @domain
1301 * @domain: Interrupt domain to match
1303 * @hwirq: The hardware interrupt number
1304 * @chip: The associated interrupt chip
1305 * @chip_data: The associated interrupt chip data
1306 * @handler: The interrupt flow handler
1307 * @handler_data: The interrupt flow handler data
1308 * @handler_name: The interrupt handler name
1310 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1311 irq_hw_number_t hwirq, struct irq_chip *chip,
1312 void *chip_data, irq_flow_handler_t handler,
1313 void *handler_data, const char *handler_name)
1315 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1316 __irq_set_handler(virq, handler, 0, handler_name);
1317 irq_set_handler_data(virq, handler_data);
1319 EXPORT_SYMBOL(irq_domain_set_info);
1322 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1323 * @irq_data: The pointer to irq_data
1325 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1327 irq_data->hwirq = 0;
1328 irq_data->chip = &no_irq_chip;
1329 irq_data->chip_data = NULL;
1331 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1334 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1335 * @domain: Interrupt domain to match
1336 * @virq: IRQ number to start with
1337 * @nr_irqs: The number of irqs to free
1339 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1340 unsigned int nr_irqs)
1342 struct irq_data *irq_data;
1345 for (i = 0; i < nr_irqs; i++) {
1346 irq_data = irq_domain_get_irq_data(domain, virq + i);
1348 irq_domain_reset_irq_data(irq_data);
1350 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1352 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1355 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1356 * @domain: Interrupt domain to match
1357 * @virq: IRQ number to start with
1358 * @nr_irqs: The number of irqs to free
1360 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1361 unsigned int nr_irqs)
1365 for (i = 0; i < nr_irqs; i++) {
1366 irq_set_handler_data(virq + i, NULL);
1367 irq_set_handler(virq + i, NULL);
1369 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1372 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1373 unsigned int irq_base,
1374 unsigned int nr_irqs)
1378 if (!domain->ops->free)
1381 for (i = 0; i < nr_irqs; i++) {
1382 if (irq_domain_get_irq_data(domain, irq_base + i))
1383 domain->ops->free(domain, irq_base + i, 1);
1387 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1388 unsigned int irq_base,
1389 unsigned int nr_irqs, void *arg)
1391 if (!domain->ops->alloc) {
1392 pr_debug("domain->ops->alloc() is NULL\n");
1396 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1400 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1401 * @domain: domain to allocate from
1402 * @irq_base: allocate specified IRQ nubmer if irq_base >= 0
1403 * @nr_irqs: number of IRQs to allocate
1404 * @node: NUMA node id for memory allocation
1405 * @arg: domain specific argument
1406 * @realloc: IRQ descriptors have already been allocated if true
1407 * @affinity: Optional irq affinity mask for multiqueue devices
1409 * Allocate IRQ numbers and initialized all data structures to support
1410 * hierarchy IRQ domains.
1411 * Parameter @realloc is mainly to support legacy IRQs.
1412 * Returns error code or allocated IRQ number
1414 * The whole process to setup an IRQ has been split into two steps.
1415 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1416 * descriptor and required hardware resources. The second step,
1417 * irq_domain_activate_irq(), is to program hardwares with preallocated
1418 * resources. In this way, it's easier to rollback when failing to
1419 * allocate resources.
1421 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1422 unsigned int nr_irqs, int node, void *arg,
1423 bool realloc, const struct cpumask *affinity)
1427 if (domain == NULL) {
1428 domain = irq_default_domain;
1429 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1433 if (realloc && irq_base >= 0) {
1436 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1439 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1445 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1446 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1451 mutex_lock(&irq_domain_mutex);
1452 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1454 mutex_unlock(&irq_domain_mutex);
1455 goto out_free_irq_data;
1457 for (i = 0; i < nr_irqs; i++)
1458 irq_domain_insert_irq(virq + i);
1459 mutex_unlock(&irq_domain_mutex);
1464 irq_domain_free_irq_data(virq, nr_irqs);
1466 irq_free_descs(virq, nr_irqs);
1470 /* The irq_data was moved, fix the revmap to refer to the new location */
1471 static void irq_domain_fix_revmap(struct irq_data *d)
1475 if (d->hwirq < d->domain->revmap_size)
1476 return; /* Not using radix tree. */
1478 /* Fix up the revmap. */
1479 mutex_lock(&revmap_trees_mutex);
1480 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1482 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1483 mutex_unlock(&revmap_trees_mutex);
1487 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1488 * @domain: Domain to push.
1489 * @virq: Irq to push the domain in to.
1490 * @arg: Passed to the irq_domain_ops alloc() function.
1492 * For an already existing irqdomain hierarchy, as might be obtained
1493 * via a call to pci_enable_msix(), add an additional domain to the
1494 * head of the processing chain. Must be called before request_irq()
1497 int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1499 struct irq_data *child_irq_data;
1500 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1501 struct irq_desc *desc;
1505 * Check that no action has been set, which indicates the virq
1506 * is in a state where this function doesn't have to deal with
1507 * races between interrupt handling and maintaining the
1508 * hierarchy. This will catch gross misuse. Attempting to
1509 * make the check race free would require holding locks across
1510 * calls to struct irq_domain_ops->alloc(), which could lead
1511 * to deadlock, so we just do a simple check before starting.
1513 desc = irq_to_desc(virq);
1516 if (WARN_ON(desc->action))
1522 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1528 if (domain->parent != root_irq_data->domain)
1531 child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
1532 irq_data_get_node(root_irq_data));
1533 if (!child_irq_data)
1536 mutex_lock(&irq_domain_mutex);
1538 /* Copy the original irq_data. */
1539 *child_irq_data = *root_irq_data;
1542 * Overwrite the root_irq_data, which is embedded in struct
1543 * irq_desc, with values for this domain.
1545 root_irq_data->parent_data = child_irq_data;
1546 root_irq_data->domain = domain;
1547 root_irq_data->mask = 0;
1548 root_irq_data->hwirq = 0;
1549 root_irq_data->chip = NULL;
1550 root_irq_data->chip_data = NULL;
1552 /* May (probably does) set hwirq, chip, etc. */
1553 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1555 /* Restore the original irq_data. */
1556 *root_irq_data = *child_irq_data;
1557 kfree(child_irq_data);
1561 irq_domain_fix_revmap(child_irq_data);
1562 irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
1565 mutex_unlock(&irq_domain_mutex);
1569 EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1572 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1573 * @domain: Domain to remove.
1574 * @virq: Irq to remove the domain from.
1576 * Undo the effects of a call to irq_domain_push_irq(). Must be
1577 * called either before request_irq() or after free_irq().
1579 int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1581 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1582 struct irq_data *child_irq_data;
1583 struct irq_data *tmp_irq_data;
1584 struct irq_desc *desc;
1587 * Check that no action is set, which indicates the virq is in
1588 * a state where this function doesn't have to deal with races
1589 * between interrupt handling and maintaining the hierarchy.
1590 * This will catch gross misuse. Attempting to make the check
1591 * race free would require holding locks across calls to
1592 * struct irq_domain_ops->free(), which could lead to
1593 * deadlock, so we just do a simple check before starting.
1595 desc = irq_to_desc(virq);
1598 if (WARN_ON(desc->action))
1607 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1609 /* We can only "pop" if this domain is at the top of the list */
1610 if (WARN_ON(root_irq_data != tmp_irq_data))
1613 if (WARN_ON(root_irq_data->domain != domain))
1616 child_irq_data = root_irq_data->parent_data;
1617 if (WARN_ON(!child_irq_data))
1620 mutex_lock(&irq_domain_mutex);
1622 root_irq_data->parent_data = NULL;
1624 irq_domain_clear_mapping(domain, root_irq_data->hwirq);
1625 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1627 /* Restore the original irq_data. */
1628 *root_irq_data = *child_irq_data;
1630 irq_domain_fix_revmap(root_irq_data);
1632 mutex_unlock(&irq_domain_mutex);
1634 kfree(child_irq_data);
1638 EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1641 * irq_domain_free_irqs - Free IRQ number and associated data structures
1642 * @virq: base IRQ number
1643 * @nr_irqs: number of IRQs to free
1645 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1647 struct irq_data *data = irq_get_irq_data(virq);
1650 if (WARN(!data || !data->domain || !data->domain->ops->free,
1651 "NULL pointer, cannot free irq\n"))
1654 mutex_lock(&irq_domain_mutex);
1655 for (i = 0; i < nr_irqs; i++)
1656 irq_domain_remove_irq(virq + i);
1657 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
1658 mutex_unlock(&irq_domain_mutex);
1660 irq_domain_free_irq_data(virq, nr_irqs);
1661 irq_free_descs(virq, nr_irqs);
1665 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1666 * @irq_base: Base IRQ number
1667 * @nr_irqs: Number of IRQs to allocate
1668 * @arg: Allocation data (arch/domain specific)
1670 * Check whether the domain has been setup recursive. If not allocate
1671 * through the parent domain.
1673 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1674 unsigned int irq_base, unsigned int nr_irqs,
1677 if (!domain->parent)
1680 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1683 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1686 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1687 * @irq_base: Base IRQ number
1688 * @nr_irqs: Number of IRQs to free
1690 * Check whether the domain has been setup recursive. If not free
1691 * through the parent domain.
1693 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1694 unsigned int irq_base, unsigned int nr_irqs)
1696 if (!domain->parent)
1699 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1701 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1703 static void __irq_domain_activate_irq(struct irq_data *irq_data)
1705 if (irq_data && irq_data->domain) {
1706 struct irq_domain *domain = irq_data->domain;
1708 if (irq_data->parent_data)
1709 __irq_domain_activate_irq(irq_data->parent_data);
1710 if (domain->ops->activate)
1711 domain->ops->activate(domain, irq_data);
1715 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1717 if (irq_data && irq_data->domain) {
1718 struct irq_domain *domain = irq_data->domain;
1720 if (domain->ops->deactivate)
1721 domain->ops->deactivate(domain, irq_data);
1722 if (irq_data->parent_data)
1723 __irq_domain_deactivate_irq(irq_data->parent_data);
1728 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1730 * @irq_data: outermost irq_data associated with interrupt
1732 * This is the second step to call domain_ops->activate to program interrupt
1733 * controllers, so the interrupt could actually get delivered.
1735 void irq_domain_activate_irq(struct irq_data *irq_data)
1737 if (!irqd_is_activated(irq_data)) {
1738 __irq_domain_activate_irq(irq_data);
1739 irqd_set_activated(irq_data);
1744 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1745 * deactivate interrupt
1746 * @irq_data: outermost irq_data associated with interrupt
1748 * It calls domain_ops->deactivate to program interrupt controllers to disable
1749 * interrupt delivery.
1751 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1753 if (irqd_is_activated(irq_data)) {
1754 __irq_domain_deactivate_irq(irq_data);
1755 irqd_clr_activated(irq_data);
1759 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1761 /* Hierarchy irq_domains must implement callback alloc() */
1762 if (domain->ops->alloc)
1763 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1767 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1768 * parent has MSI remapping support
1769 * @domain: domain pointer
1771 bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1773 for (; domain; domain = domain->parent) {
1774 if (irq_domain_is_msi_remap(domain))
1779 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1781 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1782 * @domain: domain to match
1783 * @virq: IRQ number to get irq_data
1785 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1788 struct irq_data *irq_data = irq_get_irq_data(virq);
1790 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1792 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1795 * irq_domain_set_info - Set the complete data for a @virq in @domain
1796 * @domain: Interrupt domain to match
1798 * @hwirq: The hardware interrupt number
1799 * @chip: The associated interrupt chip
1800 * @chip_data: The associated interrupt chip data
1801 * @handler: The interrupt flow handler
1802 * @handler_data: The interrupt flow handler data
1803 * @handler_name: The interrupt handler name
1805 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1806 irq_hw_number_t hwirq, struct irq_chip *chip,
1807 void *chip_data, irq_flow_handler_t handler,
1808 void *handler_data, const char *handler_name)
1810 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1811 irq_set_chip_data(virq, chip_data);
1812 irq_set_handler_data(virq, handler_data);
1815 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1818 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1820 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1821 static struct dentry *domain_dir;
1824 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1826 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1827 seq_printf(m, "%*ssize: %u\n", ind + 1, "",
1828 d->revmap_size + d->revmap_direct_max_irq);
1829 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1830 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1831 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1834 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1835 irq_domain_debug_show_one(m, d->parent, ind + 4);
1839 static int irq_domain_debug_show(struct seq_file *m, void *p)
1841 struct irq_domain *d = m->private;
1843 /* Default domain? Might be NULL */
1845 if (!irq_default_domain)
1847 d = irq_default_domain;
1849 irq_domain_debug_show_one(m, d, 0);
1853 static int irq_domain_debug_open(struct inode *inode, struct file *file)
1855 return single_open(file, irq_domain_debug_show, inode->i_private);
1858 static const struct file_operations dfs_domain_ops = {
1859 .open = irq_domain_debug_open,
1861 .llseek = seq_lseek,
1862 .release = single_release,
1865 static void debugfs_add_domain_dir(struct irq_domain *d)
1867 if (!d->name || !domain_dir || d->debugfs_file)
1869 d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
1873 static void debugfs_remove_domain_dir(struct irq_domain *d)
1875 debugfs_remove(d->debugfs_file);
1878 void __init irq_domain_debugfs_init(struct dentry *root)
1880 struct irq_domain *d;
1882 domain_dir = debugfs_create_dir("domains", root);
1886 debugfs_create_file("default", 0444, domain_dir, NULL, &dfs_domain_ops);
1887 mutex_lock(&irq_domain_mutex);
1888 list_for_each_entry(d, &irq_domain_list, link)
1889 debugfs_add_domain_dir(d);
1890 mutex_unlock(&irq_domain_mutex);