1 // SPDX-License-Identifier: GPL-2.0
3 #define pr_fmt(fmt) "irq: " fmt
5 #include <linux/acpi.h>
6 #include <linux/debugfs.h>
7 #include <linux/hardirq.h>
8 #include <linux/interrupt.h>
10 #include <linux/irqdesc.h>
11 #include <linux/irqdomain.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/topology.h>
18 #include <linux/seq_file.h>
19 #include <linux/slab.h>
20 #include <linux/smp.h>
23 static LIST_HEAD(irq_domain_list);
24 static DEFINE_MUTEX(irq_domain_mutex);
26 static struct irq_domain *irq_default_domain;
28 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
29 unsigned int nr_irqs, int node, void *arg,
30 bool realloc, const struct irq_affinity_desc *affinity);
31 static void irq_domain_check_hierarchy(struct irq_domain *domain);
34 struct fwnode_handle fwnode;
40 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
41 static void debugfs_add_domain_dir(struct irq_domain *d);
42 static void debugfs_remove_domain_dir(struct irq_domain *d);
44 static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
45 static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
48 static const char *irqchip_fwnode_get_name(const struct fwnode_handle *fwnode)
50 struct irqchip_fwid *fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
55 const struct fwnode_operations irqchip_fwnode_ops = {
56 .get_name = irqchip_fwnode_get_name,
58 EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
61 * __irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
62 * identifying an irq domain
63 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
64 * @id: Optional user provided id if name != NULL
65 * @name: Optional user provided domain name
66 * @pa: Optional user-provided physical address
68 * Allocate a struct irqchip_fwid, and return a pointer to the embedded
69 * fwnode_handle (or NULL on failure).
71 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
72 * solely to transport name information to irqdomain creation code. The
73 * node is not stored. For other types the pointer is kept in the irq
76 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
80 struct irqchip_fwid *fwid;
83 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
86 case IRQCHIP_FWNODE_NAMED:
87 n = kasprintf(GFP_KERNEL, "%s", name);
89 case IRQCHIP_FWNODE_NAMED_ID:
90 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
93 n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
106 fwnode_init(&fwid->fwnode, &irqchip_fwnode_ops);
107 return &fwid->fwnode;
109 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
112 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
114 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
116 void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
118 struct irqchip_fwid *fwid;
120 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
123 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
127 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
129 static struct irq_domain *__irq_domain_create(struct fwnode_handle *fwnode,
131 irq_hw_number_t hwirq_max,
133 const struct irq_domain_ops *ops,
136 struct irqchip_fwid *fwid;
137 struct irq_domain *domain;
139 static atomic_t unknown_domains;
141 if (WARN_ON((size && direct_max) ||
142 (!IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) && direct_max)))
145 domain = kzalloc_node(struct_size(domain, revmap, size),
146 GFP_KERNEL, of_node_to_nid(to_of_node(fwnode)));
150 if (is_fwnode_irqchip(fwnode)) {
151 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
153 switch (fwid->type) {
154 case IRQCHIP_FWNODE_NAMED:
155 case IRQCHIP_FWNODE_NAMED_ID:
156 domain->fwnode = fwnode;
157 domain->name = kstrdup(fwid->name, GFP_KERNEL);
162 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
165 domain->fwnode = fwnode;
166 domain->name = fwid->name;
169 } else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
170 is_software_node(fwnode)) {
174 * fwnode paths contain '/', which debugfs is legitimately
175 * unhappy about. Replace them with ':', which does
176 * the trick and is not as offensive as '\'...
178 name = kasprintf(GFP_KERNEL, "%pfw", fwnode);
184 strreplace(name, '/', ':');
187 domain->fwnode = fwnode;
188 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
193 pr_err("Invalid fwnode type for irqdomain\n");
194 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
195 atomic_inc_return(&unknown_domains));
200 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
203 fwnode_handle_get(fwnode);
204 fwnode_dev_initialized(fwnode, true);
207 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
208 mutex_init(&domain->revmap_mutex);
210 domain->host_data = host_data;
211 domain->hwirq_max = hwirq_max;
215 domain->flags |= IRQ_DOMAIN_FLAG_NO_MAP;
218 domain->revmap_size = size;
220 irq_domain_check_hierarchy(domain);
225 static void __irq_domain_publish(struct irq_domain *domain)
227 mutex_lock(&irq_domain_mutex);
228 debugfs_add_domain_dir(domain);
229 list_add(&domain->link, &irq_domain_list);
230 mutex_unlock(&irq_domain_mutex);
232 pr_debug("Added domain %s\n", domain->name);
236 * __irq_domain_add() - Allocate a new irq_domain data structure
237 * @fwnode: firmware node for the interrupt controller
238 * @size: Size of linear map; 0 for radix mapping only
239 * @hwirq_max: Maximum number of interrupts supported by controller
240 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
242 * @ops: domain callbacks
243 * @host_data: Controller private data pointer
245 * Allocates and initializes an irq_domain structure.
246 * Returns pointer to IRQ domain, or NULL on failure.
248 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, unsigned int size,
249 irq_hw_number_t hwirq_max, int direct_max,
250 const struct irq_domain_ops *ops,
253 struct irq_domain *domain;
255 domain = __irq_domain_create(fwnode, size, hwirq_max, direct_max,
258 __irq_domain_publish(domain);
262 EXPORT_SYMBOL_GPL(__irq_domain_add);
265 * irq_domain_remove() - Remove an irq domain.
266 * @domain: domain to remove
268 * This routine is used to remove an irq domain. The caller must ensure
269 * that all mappings within the domain have been disposed of prior to
270 * use, depending on the revmap type.
272 void irq_domain_remove(struct irq_domain *domain)
274 mutex_lock(&irq_domain_mutex);
275 debugfs_remove_domain_dir(domain);
277 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
279 list_del(&domain->link);
282 * If the going away domain is the default one, reset it.
284 if (unlikely(irq_default_domain == domain))
285 irq_set_default_host(NULL);
287 mutex_unlock(&irq_domain_mutex);
289 pr_debug("Removed domain %s\n", domain->name);
291 fwnode_dev_initialized(domain->fwnode, false);
292 fwnode_handle_put(domain->fwnode);
293 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
297 EXPORT_SYMBOL_GPL(irq_domain_remove);
299 void irq_domain_update_bus_token(struct irq_domain *domain,
300 enum irq_domain_bus_token bus_token)
304 if (domain->bus_token == bus_token)
307 mutex_lock(&irq_domain_mutex);
309 domain->bus_token = bus_token;
311 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
313 mutex_unlock(&irq_domain_mutex);
317 debugfs_remove_domain_dir(domain);
319 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
322 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
325 debugfs_add_domain_dir(domain);
327 mutex_unlock(&irq_domain_mutex);
329 EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
332 * irq_domain_create_simple() - Register an irq_domain and optionally map a range of irqs
333 * @fwnode: firmware node for the interrupt controller
334 * @size: total number of irqs in mapping
335 * @first_irq: first number of irq block assigned to the domain,
336 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
337 * pre-map all of the irqs in the domain to virqs starting at first_irq.
338 * @ops: domain callbacks
339 * @host_data: Controller private data pointer
341 * Allocates an irq_domain, and optionally if first_irq is positive then also
342 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
344 * This is intended to implement the expected behaviour for most
345 * interrupt controllers. If device tree is used, then first_irq will be 0 and
346 * irqs get mapped dynamically on the fly. However, if the controller requires
347 * static virq assignments (non-DT boot) then it will set that up correctly.
349 struct irq_domain *irq_domain_create_simple(struct fwnode_handle *fwnode,
351 unsigned int first_irq,
352 const struct irq_domain_ops *ops,
355 struct irq_domain *domain;
357 domain = __irq_domain_add(fwnode, size, size, 0, ops, host_data);
362 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
363 /* attempt to allocated irq_descs */
364 int rc = irq_alloc_descs(first_irq, first_irq, size,
365 of_node_to_nid(to_of_node(fwnode)));
367 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
370 irq_domain_associate_many(domain, first_irq, 0, size);
375 EXPORT_SYMBOL_GPL(irq_domain_create_simple);
378 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
379 * @of_node: pointer to interrupt controller's device tree node.
380 * @size: total number of irqs in legacy mapping
381 * @first_irq: first number of irq block assigned to the domain
382 * @first_hwirq: first hwirq number to use for the translation. Should normally
383 * be '0', but a positive integer can be used if the effective
384 * hwirqs numbering does not begin at zero.
385 * @ops: map/unmap domain callbacks
386 * @host_data: Controller private data pointer
388 * Note: the map() callback will be called before this function returns
389 * for all legacy interrupts except 0 (which is always the invalid irq for
390 * a legacy controller).
392 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
394 unsigned int first_irq,
395 irq_hw_number_t first_hwirq,
396 const struct irq_domain_ops *ops,
399 return irq_domain_create_legacy(of_node_to_fwnode(of_node), size,
400 first_irq, first_hwirq, ops, host_data);
402 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
404 struct irq_domain *irq_domain_create_legacy(struct fwnode_handle *fwnode,
406 unsigned int first_irq,
407 irq_hw_number_t first_hwirq,
408 const struct irq_domain_ops *ops,
411 struct irq_domain *domain;
413 domain = __irq_domain_add(fwnode, first_hwirq + size, first_hwirq + size, 0, ops, host_data);
415 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
419 EXPORT_SYMBOL_GPL(irq_domain_create_legacy);
422 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
423 * @fwspec: FW specifier for an interrupt
424 * @bus_token: domain-specific data
426 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
427 enum irq_domain_bus_token bus_token)
429 struct irq_domain *h, *found = NULL;
430 struct fwnode_handle *fwnode = fwspec->fwnode;
433 /* We might want to match the legacy controller last since
434 * it might potentially be set to match all interrupts in
435 * the absence of a device node. This isn't a problem so far
438 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
439 * values must generate an exact match for the domain to be
442 mutex_lock(&irq_domain_mutex);
443 list_for_each_entry(h, &irq_domain_list, link) {
444 if (h->ops->select && fwspec->param_count)
445 rc = h->ops->select(h, fwspec, bus_token);
446 else if (h->ops->match)
447 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
449 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
450 ((bus_token == DOMAIN_BUS_ANY) ||
451 (h->bus_token == bus_token)));
458 mutex_unlock(&irq_domain_mutex);
461 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
464 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
467 * Return: false if any MSI irq domain does not support IRQ remapping,
468 * true otherwise (including if there is no MSI irq domain)
470 bool irq_domain_check_msi_remap(void)
472 struct irq_domain *h;
475 mutex_lock(&irq_domain_mutex);
476 list_for_each_entry(h, &irq_domain_list, link) {
477 if (irq_domain_is_msi(h) &&
478 !irq_domain_hierarchical_is_msi_remap(h)) {
483 mutex_unlock(&irq_domain_mutex);
486 EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
489 * irq_set_default_host() - Set a "default" irq domain
490 * @domain: default domain pointer
492 * For convenience, it's possible to set a "default" domain that will be used
493 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
494 * platforms that want to manipulate a few hard coded interrupt numbers that
495 * aren't properly represented in the device-tree.
497 void irq_set_default_host(struct irq_domain *domain)
499 pr_debug("Default domain set to @0x%p\n", domain);
501 irq_default_domain = domain;
503 EXPORT_SYMBOL_GPL(irq_set_default_host);
506 * irq_get_default_host() - Retrieve the "default" irq domain
508 * Returns: the default domain, if any.
510 * Modern code should never use this. This should only be used on
511 * systems that cannot implement a firmware->fwnode mapping (which
512 * both DT and ACPI provide).
514 struct irq_domain *irq_get_default_host(void)
516 return irq_default_domain;
518 EXPORT_SYMBOL_GPL(irq_get_default_host);
520 static bool irq_domain_is_nomap(struct irq_domain *domain)
522 return IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) &&
523 (domain->flags & IRQ_DOMAIN_FLAG_NO_MAP);
526 static void irq_domain_clear_mapping(struct irq_domain *domain,
527 irq_hw_number_t hwirq)
529 if (irq_domain_is_nomap(domain))
532 mutex_lock(&domain->revmap_mutex);
533 if (hwirq < domain->revmap_size)
534 rcu_assign_pointer(domain->revmap[hwirq], NULL);
536 radix_tree_delete(&domain->revmap_tree, hwirq);
537 mutex_unlock(&domain->revmap_mutex);
540 static void irq_domain_set_mapping(struct irq_domain *domain,
541 irq_hw_number_t hwirq,
542 struct irq_data *irq_data)
544 if (irq_domain_is_nomap(domain))
547 mutex_lock(&domain->revmap_mutex);
548 if (hwirq < domain->revmap_size)
549 rcu_assign_pointer(domain->revmap[hwirq], irq_data);
551 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
552 mutex_unlock(&domain->revmap_mutex);
555 static void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
557 struct irq_data *irq_data = irq_get_irq_data(irq);
558 irq_hw_number_t hwirq;
560 if (WARN(!irq_data || irq_data->domain != domain,
561 "virq%i doesn't exist; cannot disassociate\n", irq))
564 hwirq = irq_data->hwirq;
566 mutex_lock(&irq_domain_mutex);
568 irq_set_status_flags(irq, IRQ_NOREQUEST);
570 /* remove chip and handler */
571 irq_set_chip_and_handler(irq, NULL, NULL);
573 /* Make sure it's completed */
574 synchronize_irq(irq);
576 /* Tell the PIC about it */
577 if (domain->ops->unmap)
578 domain->ops->unmap(domain, irq);
581 irq_data->domain = NULL;
585 /* Clear reverse map for this hwirq */
586 irq_domain_clear_mapping(domain, hwirq);
588 mutex_unlock(&irq_domain_mutex);
591 static int irq_domain_associate_locked(struct irq_domain *domain, unsigned int virq,
592 irq_hw_number_t hwirq)
594 struct irq_data *irq_data = irq_get_irq_data(virq);
597 if (WARN(hwirq >= domain->hwirq_max,
598 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
600 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
602 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
605 irq_data->hwirq = hwirq;
606 irq_data->domain = domain;
607 if (domain->ops->map) {
608 ret = domain->ops->map(domain, virq, hwirq);
611 * If map() returns -EPERM, this interrupt is protected
612 * by the firmware or some other service and shall not
613 * be mapped. Don't bother telling the user about it.
616 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
617 domain->name, hwirq, virq, ret);
619 irq_data->domain = NULL;
624 /* If not already assigned, give the domain the chip's name */
625 if (!domain->name && irq_data->chip)
626 domain->name = irq_data->chip->name;
630 irq_domain_set_mapping(domain, hwirq, irq_data);
632 irq_clear_status_flags(virq, IRQ_NOREQUEST);
637 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
638 irq_hw_number_t hwirq)
642 mutex_lock(&irq_domain_mutex);
643 ret = irq_domain_associate_locked(domain, virq, hwirq);
644 mutex_unlock(&irq_domain_mutex);
648 EXPORT_SYMBOL_GPL(irq_domain_associate);
650 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
651 irq_hw_number_t hwirq_base, int count)
653 struct device_node *of_node;
656 of_node = irq_domain_get_of_node(domain);
657 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
658 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
660 for (i = 0; i < count; i++) {
661 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
664 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
666 #ifdef CONFIG_IRQ_DOMAIN_NOMAP
668 * irq_create_direct_mapping() - Allocate an irq for direct mapping
669 * @domain: domain to allocate the irq for or NULL for default domain
671 * This routine is used for irq controllers which can choose the hardware
672 * interrupt numbers they generate. In such a case it's simplest to use
673 * the linux irq as the hardware interrupt number. It still uses the linear
674 * or radix tree to store the mapping, but the irq controller can optimize
675 * the revmap path by using the hwirq directly.
677 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
679 struct device_node *of_node;
683 domain = irq_default_domain;
685 of_node = irq_domain_get_of_node(domain);
686 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
688 pr_debug("create_direct virq allocation failed\n");
691 if (virq >= domain->revmap_size) {
692 pr_err("ERROR: no free irqs available below %i maximum\n",
693 domain->revmap_size);
697 pr_debug("create_direct obtained virq %d\n", virq);
699 if (irq_domain_associate(domain, virq, virq)) {
706 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
709 static unsigned int irq_create_mapping_affinity_locked(struct irq_domain *domain,
710 irq_hw_number_t hwirq,
711 const struct irq_affinity_desc *affinity)
713 struct device_node *of_node = irq_domain_get_of_node(domain);
716 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
718 /* Allocate a virtual interrupt number */
719 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
722 pr_debug("-> virq allocation failed\n");
726 if (irq_domain_associate_locked(domain, virq, hwirq)) {
731 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
732 hwirq, of_node_full_name(of_node), virq);
738 * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
739 * @domain: domain owning this hardware interrupt or NULL for default domain
740 * @hwirq: hardware irq number in that domain space
741 * @affinity: irq affinity
743 * Only one mapping per hardware interrupt is permitted. Returns a linux
745 * If the sense/trigger is to be specified, set_irq_type() should be called
746 * on the number returned from that call.
748 unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
749 irq_hw_number_t hwirq,
750 const struct irq_affinity_desc *affinity)
754 /* Look for default domain if necessary */
756 domain = irq_default_domain;
757 if (domain == NULL) {
758 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
762 mutex_lock(&irq_domain_mutex);
764 /* Check if mapping already exists */
765 virq = irq_find_mapping(domain, hwirq);
767 pr_debug("existing mapping on virq %d\n", virq);
771 virq = irq_create_mapping_affinity_locked(domain, hwirq, affinity);
773 mutex_unlock(&irq_domain_mutex);
777 EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
779 static int irq_domain_translate(struct irq_domain *d,
780 struct irq_fwspec *fwspec,
781 irq_hw_number_t *hwirq, unsigned int *type)
783 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
784 if (d->ops->translate)
785 return d->ops->translate(d, fwspec, hwirq, type);
788 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
789 fwspec->param, fwspec->param_count,
792 /* If domain has no translation, then we assume interrupt line */
793 *hwirq = fwspec->param[0];
797 static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
799 struct irq_fwspec *fwspec)
803 fwspec->fwnode = of_node_to_fwnode(np);
804 fwspec->param_count = count;
806 for (i = 0; i < count; i++)
807 fwspec->param[i] = args[i];
810 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
812 struct irq_domain *domain;
813 struct irq_data *irq_data;
814 irq_hw_number_t hwirq;
815 unsigned int type = IRQ_TYPE_NONE;
818 if (fwspec->fwnode) {
819 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
821 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
823 domain = irq_default_domain;
827 pr_warn("no irq domain found for %s !\n",
828 of_node_full_name(to_of_node(fwspec->fwnode)));
832 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
836 * WARN if the irqchip returns a type with bits
837 * outside the sense mask set and clear these bits.
839 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
840 type &= IRQ_TYPE_SENSE_MASK;
842 mutex_lock(&irq_domain_mutex);
845 * If we've already configured this interrupt,
846 * don't do it again, or hell will break loose.
848 virq = irq_find_mapping(domain, hwirq);
851 * If the trigger type is not specified or matches the
852 * current trigger type then we are done so return the
855 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
859 * If the trigger type has not been set yet, then set
860 * it now and return the interrupt number.
862 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
863 irq_data = irq_get_irq_data(virq);
869 irqd_set_trigger_type(irq_data, type);
873 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
874 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
879 if (irq_domain_is_hierarchy(domain)) {
880 virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE,
881 fwspec, false, NULL);
888 virq = irq_create_mapping_affinity_locked(domain, hwirq, NULL);
893 irq_data = irq_get_irq_data(virq);
894 if (WARN_ON(!irq_data)) {
899 /* Store trigger type */
900 irqd_set_trigger_type(irq_data, type);
902 mutex_unlock(&irq_domain_mutex);
906 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
908 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
910 struct irq_fwspec fwspec;
912 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
913 irq_data->args_count, &fwspec);
915 return irq_create_fwspec_mapping(&fwspec);
917 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
920 * irq_dispose_mapping() - Unmap an interrupt
921 * @virq: linux irq number of the interrupt to unmap
923 void irq_dispose_mapping(unsigned int virq)
925 struct irq_data *irq_data = irq_get_irq_data(virq);
926 struct irq_domain *domain;
928 if (!virq || !irq_data)
931 domain = irq_data->domain;
932 if (WARN_ON(domain == NULL))
935 if (irq_domain_is_hierarchy(domain)) {
936 irq_domain_free_irqs(virq, 1);
938 irq_domain_disassociate(domain, virq);
942 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
945 * __irq_resolve_mapping() - Find a linux irq from a hw irq number.
946 * @domain: domain owning this hardware interrupt
947 * @hwirq: hardware irq number in that domain space
948 * @irq: optional pointer to return the Linux irq if required
950 * Returns the interrupt descriptor.
952 struct irq_desc *__irq_resolve_mapping(struct irq_domain *domain,
953 irq_hw_number_t hwirq,
956 struct irq_desc *desc = NULL;
957 struct irq_data *data;
959 /* Look for default domain if necessary */
961 domain = irq_default_domain;
965 if (irq_domain_is_nomap(domain)) {
966 if (hwirq < domain->revmap_size) {
967 data = irq_domain_get_irq_data(domain, hwirq);
968 if (data && data->hwirq == hwirq)
969 desc = irq_data_to_desc(data);
978 /* Check if the hwirq is in the linear revmap. */
979 if (hwirq < domain->revmap_size)
980 data = rcu_dereference(domain->revmap[hwirq]);
982 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
985 desc = irq_data_to_desc(data);
993 EXPORT_SYMBOL_GPL(__irq_resolve_mapping);
996 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
998 * Device Tree IRQ specifier translation function which works with one cell
999 * bindings where the cell value maps directly to the hwirq number.
1001 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
1002 const u32 *intspec, unsigned int intsize,
1003 unsigned long *out_hwirq, unsigned int *out_type)
1005 if (WARN_ON(intsize < 1))
1007 *out_hwirq = intspec[0];
1008 *out_type = IRQ_TYPE_NONE;
1011 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
1014 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
1016 * Device Tree IRQ specifier translation function which works with two cell
1017 * bindings where the cell values map directly to the hwirq number
1018 * and linux irq flags.
1020 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
1021 const u32 *intspec, unsigned int intsize,
1022 irq_hw_number_t *out_hwirq, unsigned int *out_type)
1024 struct irq_fwspec fwspec;
1026 of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
1027 return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
1029 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
1032 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
1034 * Device Tree IRQ specifier translation function which works with either one
1035 * or two cell bindings where the cell values map directly to the hwirq number
1036 * and linux irq flags.
1038 * Note: don't use this function unless your interrupt controller explicitly
1039 * supports both one and two cell bindings. For the majority of controllers
1040 * the _onecell() or _twocell() variants above should be used.
1042 int irq_domain_xlate_onetwocell(struct irq_domain *d,
1043 struct device_node *ctrlr,
1044 const u32 *intspec, unsigned int intsize,
1045 unsigned long *out_hwirq, unsigned int *out_type)
1047 if (WARN_ON(intsize < 1))
1049 *out_hwirq = intspec[0];
1051 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1053 *out_type = IRQ_TYPE_NONE;
1056 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
1058 const struct irq_domain_ops irq_domain_simple_ops = {
1059 .xlate = irq_domain_xlate_onetwocell,
1061 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1064 * irq_domain_translate_onecell() - Generic translate for direct one cell
1067 int irq_domain_translate_onecell(struct irq_domain *d,
1068 struct irq_fwspec *fwspec,
1069 unsigned long *out_hwirq,
1070 unsigned int *out_type)
1072 if (WARN_ON(fwspec->param_count < 1))
1074 *out_hwirq = fwspec->param[0];
1075 *out_type = IRQ_TYPE_NONE;
1078 EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
1081 * irq_domain_translate_twocell() - Generic translate for direct two cell
1084 * Device Tree IRQ specifier translation function which works with two cell
1085 * bindings where the cell values map directly to the hwirq number
1086 * and linux irq flags.
1088 int irq_domain_translate_twocell(struct irq_domain *d,
1089 struct irq_fwspec *fwspec,
1090 unsigned long *out_hwirq,
1091 unsigned int *out_type)
1093 if (WARN_ON(fwspec->param_count < 2))
1095 *out_hwirq = fwspec->param[0];
1096 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1099 EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1101 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1102 int node, const struct irq_affinity_desc *affinity)
1107 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1110 hint = hwirq % nr_irqs;
1113 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1115 if (virq <= 0 && hint > 1) {
1116 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1125 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1126 * @irq_data: The pointer to irq_data
1128 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1130 irq_data->hwirq = 0;
1131 irq_data->chip = &no_irq_chip;
1132 irq_data->chip_data = NULL;
1134 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1136 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1138 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1139 * @parent: Parent irq domain to associate with the new domain
1140 * @flags: Irq domain flags associated to the domain
1141 * @size: Size of the domain. See below
1142 * @fwnode: Optional fwnode of the interrupt controller
1143 * @ops: Pointer to the interrupt domain callbacks
1144 * @host_data: Controller private data pointer
1146 * If @size is 0 a tree domain is created, otherwise a linear domain.
1148 * If successful the parent is associated to the new domain and the
1149 * domain flags are set.
1150 * Returns pointer to IRQ domain, or NULL on failure.
1152 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1155 struct fwnode_handle *fwnode,
1156 const struct irq_domain_ops *ops,
1159 struct irq_domain *domain;
1162 domain = __irq_domain_create(fwnode, size, size, 0, ops, host_data);
1164 domain = __irq_domain_create(fwnode, 0, ~0, 0, ops, host_data);
1167 domain->parent = parent;
1168 domain->flags |= flags;
1170 __irq_domain_publish(domain);
1175 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1177 static void irq_domain_insert_irq(int virq)
1179 struct irq_data *data;
1181 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1182 struct irq_domain *domain = data->domain;
1185 irq_domain_set_mapping(domain, data->hwirq, data);
1187 /* If not already assigned, give the domain the chip's name */
1188 if (!domain->name && data->chip)
1189 domain->name = data->chip->name;
1192 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1195 static void irq_domain_remove_irq(int virq)
1197 struct irq_data *data;
1199 irq_set_status_flags(virq, IRQ_NOREQUEST);
1200 irq_set_chip_and_handler(virq, NULL, NULL);
1201 synchronize_irq(virq);
1204 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1205 struct irq_domain *domain = data->domain;
1206 irq_hw_number_t hwirq = data->hwirq;
1209 irq_domain_clear_mapping(domain, hwirq);
1213 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1214 struct irq_data *child)
1216 struct irq_data *irq_data;
1218 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1219 irq_data_get_node(child));
1221 child->parent_data = irq_data;
1222 irq_data->irq = child->irq;
1223 irq_data->common = child->common;
1224 irq_data->domain = domain;
1230 static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
1232 struct irq_data *tmp;
1236 irq_data = irq_data->parent_data;
1241 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1243 struct irq_data *irq_data, *tmp;
1246 for (i = 0; i < nr_irqs; i++) {
1247 irq_data = irq_get_irq_data(virq + i);
1248 tmp = irq_data->parent_data;
1249 irq_data->parent_data = NULL;
1250 irq_data->domain = NULL;
1252 __irq_domain_free_hierarchy(tmp);
1257 * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
1258 * @domain: IRQ domain from which the hierarchy is to be disconnected
1259 * @virq: IRQ number where the hierarchy is to be trimmed
1261 * Marks the @virq level belonging to @domain as disconnected.
1262 * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
1265 * Its only use is to be able to trim levels of hierarchy that do not
1266 * have any real meaning for this interrupt, and that the driver marks
1267 * as such from its .alloc() callback.
1269 int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
1272 struct irq_data *irqd;
1274 irqd = irq_domain_get_irq_data(domain, virq);
1278 irqd->chip = ERR_PTR(-ENOTCONN);
1281 EXPORT_SYMBOL_GPL(irq_domain_disconnect_hierarchy);
1283 static int irq_domain_trim_hierarchy(unsigned int virq)
1285 struct irq_data *tail, *irqd, *irq_data;
1287 irq_data = irq_get_irq_data(virq);
1290 /* The first entry must have a valid irqchip */
1291 if (!irq_data->chip || IS_ERR(irq_data->chip))
1295 * Validate that the irq_data chain is sane in the presence of
1296 * a hierarchy trimming marker.
1298 for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
1299 /* Can't have a valid irqchip after a trim marker */
1300 if (irqd->chip && tail)
1303 /* Can't have an empty irqchip before a trim marker */
1304 if (!irqd->chip && !tail)
1307 if (IS_ERR(irqd->chip)) {
1308 /* Only -ENOTCONN is a valid trim marker */
1309 if (PTR_ERR(irqd->chip) != -ENOTCONN)
1316 /* No trim marker, nothing to do */
1320 pr_info("IRQ%d: trimming hierarchy from %s\n",
1321 virq, tail->parent_data->domain->name);
1323 /* Sever the inner part of the hierarchy... */
1325 tail = tail->parent_data;
1326 irqd->parent_data = NULL;
1327 __irq_domain_free_hierarchy(tail);
1332 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1333 unsigned int virq, unsigned int nr_irqs)
1335 struct irq_data *irq_data;
1336 struct irq_domain *parent;
1339 /* The outermost irq_data is embedded in struct irq_desc */
1340 for (i = 0; i < nr_irqs; i++) {
1341 irq_data = irq_get_irq_data(virq + i);
1342 irq_data->domain = domain;
1344 for (parent = domain->parent; parent; parent = parent->parent) {
1345 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1347 irq_domain_free_irq_data(virq, i + 1);
1357 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1358 * @domain: domain to match
1359 * @virq: IRQ number to get irq_data
1361 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1364 struct irq_data *irq_data;
1366 for (irq_data = irq_get_irq_data(virq); irq_data;
1367 irq_data = irq_data->parent_data)
1368 if (irq_data->domain == domain)
1373 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1376 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1377 * @domain: Interrupt domain to match
1379 * @hwirq: The hwirq number
1380 * @chip: The associated interrupt chip
1381 * @chip_data: The associated chip data
1383 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1384 irq_hw_number_t hwirq, struct irq_chip *chip,
1387 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1392 irq_data->hwirq = hwirq;
1393 irq_data->chip = chip ? chip : &no_irq_chip;
1394 irq_data->chip_data = chip_data;
1398 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1401 * irq_domain_set_info - Set the complete data for a @virq in @domain
1402 * @domain: Interrupt domain to match
1404 * @hwirq: The hardware interrupt number
1405 * @chip: The associated interrupt chip
1406 * @chip_data: The associated interrupt chip data
1407 * @handler: The interrupt flow handler
1408 * @handler_data: The interrupt flow handler data
1409 * @handler_name: The interrupt handler name
1411 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1412 irq_hw_number_t hwirq, struct irq_chip *chip,
1413 void *chip_data, irq_flow_handler_t handler,
1414 void *handler_data, const char *handler_name)
1416 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1417 __irq_set_handler(virq, handler, 0, handler_name);
1418 irq_set_handler_data(virq, handler_data);
1420 EXPORT_SYMBOL(irq_domain_set_info);
1423 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1424 * @domain: Interrupt domain to match
1425 * @virq: IRQ number to start with
1426 * @nr_irqs: The number of irqs to free
1428 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1429 unsigned int nr_irqs)
1431 struct irq_data *irq_data;
1434 for (i = 0; i < nr_irqs; i++) {
1435 irq_data = irq_domain_get_irq_data(domain, virq + i);
1437 irq_domain_reset_irq_data(irq_data);
1439 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1441 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1444 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1445 * @domain: Interrupt domain to match
1446 * @virq: IRQ number to start with
1447 * @nr_irqs: The number of irqs to free
1449 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1450 unsigned int nr_irqs)
1454 for (i = 0; i < nr_irqs; i++) {
1455 irq_set_handler_data(virq + i, NULL);
1456 irq_set_handler(virq + i, NULL);
1458 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1461 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1462 unsigned int irq_base,
1463 unsigned int nr_irqs)
1467 if (!domain->ops->free)
1470 for (i = 0; i < nr_irqs; i++) {
1471 if (irq_domain_get_irq_data(domain, irq_base + i))
1472 domain->ops->free(domain, irq_base + i, 1);
1476 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1477 unsigned int irq_base,
1478 unsigned int nr_irqs, void *arg)
1480 if (!domain->ops->alloc) {
1481 pr_debug("domain->ops->alloc() is NULL\n");
1485 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1488 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1489 unsigned int nr_irqs, int node, void *arg,
1490 bool realloc, const struct irq_affinity_desc *affinity)
1494 if (realloc && irq_base >= 0) {
1497 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1500 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1506 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1507 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1512 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1514 goto out_free_irq_data;
1516 for (i = 0; i < nr_irqs; i++) {
1517 ret = irq_domain_trim_hierarchy(virq + i);
1519 goto out_free_irq_data;
1522 for (i = 0; i < nr_irqs; i++)
1523 irq_domain_insert_irq(virq + i);
1528 irq_domain_free_irq_data(virq, nr_irqs);
1530 irq_free_descs(virq, nr_irqs);
1535 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1536 * @domain: domain to allocate from
1537 * @irq_base: allocate specified IRQ number if irq_base >= 0
1538 * @nr_irqs: number of IRQs to allocate
1539 * @node: NUMA node id for memory allocation
1540 * @arg: domain specific argument
1541 * @realloc: IRQ descriptors have already been allocated if true
1542 * @affinity: Optional irq affinity mask for multiqueue devices
1544 * Allocate IRQ numbers and initialized all data structures to support
1545 * hierarchy IRQ domains.
1546 * Parameter @realloc is mainly to support legacy IRQs.
1547 * Returns error code or allocated IRQ number
1549 * The whole process to setup an IRQ has been split into two steps.
1550 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1551 * descriptor and required hardware resources. The second step,
1552 * irq_domain_activate_irq(), is to program the hardware with preallocated
1553 * resources. In this way, it's easier to rollback when failing to
1554 * allocate resources.
1556 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1557 unsigned int nr_irqs, int node, void *arg,
1558 bool realloc, const struct irq_affinity_desc *affinity)
1562 if (domain == NULL) {
1563 domain = irq_default_domain;
1564 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1568 mutex_lock(&irq_domain_mutex);
1569 ret = irq_domain_alloc_irqs_locked(domain, irq_base, nr_irqs, node, arg,
1571 mutex_unlock(&irq_domain_mutex);
1576 /* The irq_data was moved, fix the revmap to refer to the new location */
1577 static void irq_domain_fix_revmap(struct irq_data *d)
1581 if (irq_domain_is_nomap(d->domain))
1584 /* Fix up the revmap. */
1585 mutex_lock(&d->domain->revmap_mutex);
1586 if (d->hwirq < d->domain->revmap_size) {
1587 /* Not using radix tree */
1588 rcu_assign_pointer(d->domain->revmap[d->hwirq], d);
1590 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1592 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1594 mutex_unlock(&d->domain->revmap_mutex);
1598 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1599 * @domain: Domain to push.
1600 * @virq: Irq to push the domain in to.
1601 * @arg: Passed to the irq_domain_ops alloc() function.
1603 * For an already existing irqdomain hierarchy, as might be obtained
1604 * via a call to pci_enable_msix(), add an additional domain to the
1605 * head of the processing chain. Must be called before request_irq()
1608 int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1610 struct irq_data *child_irq_data;
1611 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1612 struct irq_desc *desc;
1616 * Check that no action has been set, which indicates the virq
1617 * is in a state where this function doesn't have to deal with
1618 * races between interrupt handling and maintaining the
1619 * hierarchy. This will catch gross misuse. Attempting to
1620 * make the check race free would require holding locks across
1621 * calls to struct irq_domain_ops->alloc(), which could lead
1622 * to deadlock, so we just do a simple check before starting.
1624 desc = irq_to_desc(virq);
1627 if (WARN_ON(desc->action))
1633 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1639 if (domain->parent != root_irq_data->domain)
1642 child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
1643 irq_data_get_node(root_irq_data));
1644 if (!child_irq_data)
1647 mutex_lock(&irq_domain_mutex);
1649 /* Copy the original irq_data. */
1650 *child_irq_data = *root_irq_data;
1653 * Overwrite the root_irq_data, which is embedded in struct
1654 * irq_desc, with values for this domain.
1656 root_irq_data->parent_data = child_irq_data;
1657 root_irq_data->domain = domain;
1658 root_irq_data->mask = 0;
1659 root_irq_data->hwirq = 0;
1660 root_irq_data->chip = NULL;
1661 root_irq_data->chip_data = NULL;
1663 /* May (probably does) set hwirq, chip, etc. */
1664 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1666 /* Restore the original irq_data. */
1667 *root_irq_data = *child_irq_data;
1668 kfree(child_irq_data);
1672 irq_domain_fix_revmap(child_irq_data);
1673 irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
1676 mutex_unlock(&irq_domain_mutex);
1680 EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1683 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1684 * @domain: Domain to remove.
1685 * @virq: Irq to remove the domain from.
1687 * Undo the effects of a call to irq_domain_push_irq(). Must be
1688 * called either before request_irq() or after free_irq().
1690 int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1692 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1693 struct irq_data *child_irq_data;
1694 struct irq_data *tmp_irq_data;
1695 struct irq_desc *desc;
1698 * Check that no action is set, which indicates the virq is in
1699 * a state where this function doesn't have to deal with races
1700 * between interrupt handling and maintaining the hierarchy.
1701 * This will catch gross misuse. Attempting to make the check
1702 * race free would require holding locks across calls to
1703 * struct irq_domain_ops->free(), which could lead to
1704 * deadlock, so we just do a simple check before starting.
1706 desc = irq_to_desc(virq);
1709 if (WARN_ON(desc->action))
1718 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1720 /* We can only "pop" if this domain is at the top of the list */
1721 if (WARN_ON(root_irq_data != tmp_irq_data))
1724 if (WARN_ON(root_irq_data->domain != domain))
1727 child_irq_data = root_irq_data->parent_data;
1728 if (WARN_ON(!child_irq_data))
1731 mutex_lock(&irq_domain_mutex);
1733 root_irq_data->parent_data = NULL;
1735 irq_domain_clear_mapping(domain, root_irq_data->hwirq);
1736 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1738 /* Restore the original irq_data. */
1739 *root_irq_data = *child_irq_data;
1741 irq_domain_fix_revmap(root_irq_data);
1743 mutex_unlock(&irq_domain_mutex);
1745 kfree(child_irq_data);
1749 EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1752 * irq_domain_free_irqs - Free IRQ number and associated data structures
1753 * @virq: base IRQ number
1754 * @nr_irqs: number of IRQs to free
1756 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1758 struct irq_data *data = irq_get_irq_data(virq);
1761 if (WARN(!data || !data->domain || !data->domain->ops->free,
1762 "NULL pointer, cannot free irq\n"))
1765 mutex_lock(&irq_domain_mutex);
1766 for (i = 0; i < nr_irqs; i++)
1767 irq_domain_remove_irq(virq + i);
1768 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
1769 mutex_unlock(&irq_domain_mutex);
1771 irq_domain_free_irq_data(virq, nr_irqs);
1772 irq_free_descs(virq, nr_irqs);
1776 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1777 * @domain: Domain below which interrupts must be allocated
1778 * @irq_base: Base IRQ number
1779 * @nr_irqs: Number of IRQs to allocate
1780 * @arg: Allocation data (arch/domain specific)
1782 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1783 unsigned int irq_base, unsigned int nr_irqs,
1786 if (!domain->parent)
1789 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1792 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1795 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1796 * @domain: Domain below which interrupts must be freed
1797 * @irq_base: Base IRQ number
1798 * @nr_irqs: Number of IRQs to free
1800 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1801 unsigned int irq_base, unsigned int nr_irqs)
1803 if (!domain->parent)
1806 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1808 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1810 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1812 if (irq_data && irq_data->domain) {
1813 struct irq_domain *domain = irq_data->domain;
1815 if (domain->ops->deactivate)
1816 domain->ops->deactivate(domain, irq_data);
1817 if (irq_data->parent_data)
1818 __irq_domain_deactivate_irq(irq_data->parent_data);
1822 static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1826 if (irqd && irqd->domain) {
1827 struct irq_domain *domain = irqd->domain;
1829 if (irqd->parent_data)
1830 ret = __irq_domain_activate_irq(irqd->parent_data,
1832 if (!ret && domain->ops->activate) {
1833 ret = domain->ops->activate(domain, irqd, reserve);
1834 /* Rollback in case of error */
1835 if (ret && irqd->parent_data)
1836 __irq_domain_deactivate_irq(irqd->parent_data);
1843 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1845 * @irq_data: Outermost irq_data associated with interrupt
1846 * @reserve: If set only reserve an interrupt vector instead of assigning one
1848 * This is the second step to call domain_ops->activate to program interrupt
1849 * controllers, so the interrupt could actually get delivered.
1851 int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1855 if (!irqd_is_activated(irq_data))
1856 ret = __irq_domain_activate_irq(irq_data, reserve);
1858 irqd_set_activated(irq_data);
1863 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1864 * deactivate interrupt
1865 * @irq_data: outermost irq_data associated with interrupt
1867 * It calls domain_ops->deactivate to program interrupt controllers to disable
1868 * interrupt delivery.
1870 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1872 if (irqd_is_activated(irq_data)) {
1873 __irq_domain_deactivate_irq(irq_data);
1874 irqd_clr_activated(irq_data);
1878 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1880 /* Hierarchy irq_domains must implement callback alloc() */
1881 if (domain->ops->alloc)
1882 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1886 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1887 * parent has MSI remapping support
1888 * @domain: domain pointer
1890 bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1892 for (; domain; domain = domain->parent) {
1893 if (irq_domain_is_msi_remap(domain))
1898 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1900 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1901 * @domain: domain to match
1902 * @virq: IRQ number to get irq_data
1904 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1907 struct irq_data *irq_data = irq_get_irq_data(virq);
1909 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1911 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1914 * irq_domain_set_info - Set the complete data for a @virq in @domain
1915 * @domain: Interrupt domain to match
1917 * @hwirq: The hardware interrupt number
1918 * @chip: The associated interrupt chip
1919 * @chip_data: The associated interrupt chip data
1920 * @handler: The interrupt flow handler
1921 * @handler_data: The interrupt flow handler data
1922 * @handler_name: The interrupt handler name
1924 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1925 irq_hw_number_t hwirq, struct irq_chip *chip,
1926 void *chip_data, irq_flow_handler_t handler,
1927 void *handler_data, const char *handler_name)
1929 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1930 irq_set_chip_data(virq, chip_data);
1931 irq_set_handler_data(virq, handler_data);
1934 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1935 unsigned int nr_irqs, int node, void *arg,
1936 bool realloc, const struct irq_affinity_desc *affinity)
1941 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1944 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1946 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1947 static struct dentry *domain_dir;
1950 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1952 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1953 seq_printf(m, "%*ssize: %u\n", ind + 1, "", d->revmap_size);
1954 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1955 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1956 if (d->ops && d->ops->debug_show)
1957 d->ops->debug_show(m, d, NULL, ind + 1);
1958 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1961 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1962 irq_domain_debug_show_one(m, d->parent, ind + 4);
1966 static int irq_domain_debug_show(struct seq_file *m, void *p)
1968 struct irq_domain *d = m->private;
1970 /* Default domain? Might be NULL */
1972 if (!irq_default_domain)
1974 d = irq_default_domain;
1976 irq_domain_debug_show_one(m, d, 0);
1979 DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1981 static void debugfs_add_domain_dir(struct irq_domain *d)
1983 if (!d->name || !domain_dir)
1985 debugfs_create_file(d->name, 0444, domain_dir, d,
1986 &irq_domain_debug_fops);
1989 static void debugfs_remove_domain_dir(struct irq_domain *d)
1991 debugfs_lookup_and_remove(d->name, domain_dir);
1994 void __init irq_domain_debugfs_init(struct dentry *root)
1996 struct irq_domain *d;
1998 domain_dir = debugfs_create_dir("domains", root);
2000 debugfs_create_file("default", 0444, domain_dir, NULL,
2001 &irq_domain_debug_fops);
2002 mutex_lock(&irq_domain_mutex);
2003 list_for_each_entry(d, &irq_domain_list, link)
2004 debugfs_add_domain_dir(d);
2005 mutex_unlock(&irq_domain_mutex);