Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / kernel / irq / irqdomain.c
blob6aacd342cd14cb88a6e305f4d2473df1bcc5de3b
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>
9 #include <linux/irq.h>
10 #include <linux/irqdesc.h>
11 #include <linux/irqdomain.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
14 #include <linux/of.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>
21 #include <linux/fs.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 void irq_domain_check_hierarchy(struct irq_domain *domain);
30 struct irqchip_fwid {
31 struct fwnode_handle fwnode;
32 unsigned int type;
33 char *name;
34 phys_addr_t *pa;
37 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
38 static void debugfs_add_domain_dir(struct irq_domain *d);
39 static void debugfs_remove_domain_dir(struct irq_domain *d);
40 #else
41 static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
42 static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
43 #endif
45 static const char *irqchip_fwnode_get_name(const struct fwnode_handle *fwnode)
47 struct irqchip_fwid *fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
49 return fwid->name;
52 const struct fwnode_operations irqchip_fwnode_ops = {
53 .get_name = irqchip_fwnode_get_name,
55 EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
57 /**
58 * __irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
59 * identifying an irq domain
60 * @type: Type of irqchip_fwnode. See linux/irqdomain.h
61 * @id: Optional user provided id if name != NULL
62 * @name: Optional user provided domain name
63 * @pa: Optional user-provided physical address
65 * Allocate a struct irqchip_fwid, and return a poiner to the embedded
66 * fwnode_handle (or NULL on failure).
68 * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
69 * solely to transport name information to irqdomain creation code. The
70 * node is not stored. For other types the pointer is kept in the irq
71 * domain struct.
73 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
74 const char *name,
75 phys_addr_t *pa)
77 struct irqchip_fwid *fwid;
78 char *n;
80 fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
82 switch (type) {
83 case IRQCHIP_FWNODE_NAMED:
84 n = kasprintf(GFP_KERNEL, "%s", name);
85 break;
86 case IRQCHIP_FWNODE_NAMED_ID:
87 n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
88 break;
89 default:
90 n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
91 break;
94 if (!fwid || !n) {
95 kfree(fwid);
96 kfree(n);
97 return NULL;
100 fwid->type = type;
101 fwid->name = n;
102 fwid->pa = pa;
103 fwnode_init(&fwid->fwnode, &irqchip_fwnode_ops);
104 return &fwid->fwnode;
106 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
109 * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
111 * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
113 void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
115 struct irqchip_fwid *fwid;
117 if (WARN_ON(!is_fwnode_irqchip(fwnode)))
118 return;
120 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
121 kfree(fwid->name);
122 kfree(fwid);
124 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
127 * __irq_domain_add() - Allocate a new irq_domain data structure
128 * @fwnode: firmware node for the interrupt controller
129 * @size: Size of linear map; 0 for radix mapping only
130 * @hwirq_max: Maximum number of interrupts supported by controller
131 * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
132 * direct mapping
133 * @ops: domain callbacks
134 * @host_data: Controller private data pointer
136 * Allocates and initializes an irq_domain structure.
137 * Returns pointer to IRQ domain, or NULL on failure.
139 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
140 irq_hw_number_t hwirq_max, int direct_max,
141 const struct irq_domain_ops *ops,
142 void *host_data)
144 struct irqchip_fwid *fwid;
145 struct irq_domain *domain;
147 static atomic_t unknown_domains;
149 domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
150 GFP_KERNEL, of_node_to_nid(to_of_node(fwnode)));
151 if (!domain)
152 return NULL;
154 if (is_fwnode_irqchip(fwnode)) {
155 fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
157 switch (fwid->type) {
158 case IRQCHIP_FWNODE_NAMED:
159 case IRQCHIP_FWNODE_NAMED_ID:
160 domain->fwnode = fwnode;
161 domain->name = kstrdup(fwid->name, GFP_KERNEL);
162 if (!domain->name) {
163 kfree(domain);
164 return NULL;
166 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
167 break;
168 default:
169 domain->fwnode = fwnode;
170 domain->name = fwid->name;
171 break;
173 } else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
174 is_software_node(fwnode)) {
175 char *name;
178 * fwnode paths contain '/', which debugfs is legitimately
179 * unhappy about. Replace them with ':', which does
180 * the trick and is not as offensive as '\'...
182 name = kasprintf(GFP_KERNEL, "%pfw", fwnode);
183 if (!name) {
184 kfree(domain);
185 return NULL;
188 strreplace(name, '/', ':');
190 domain->name = name;
191 domain->fwnode = fwnode;
192 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
195 if (!domain->name) {
196 if (fwnode)
197 pr_err("Invalid fwnode type for irqdomain\n");
198 domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
199 atomic_inc_return(&unknown_domains));
200 if (!domain->name) {
201 kfree(domain);
202 return NULL;
204 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
207 fwnode_handle_get(fwnode);
209 /* Fill structure */
210 INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
211 mutex_init(&domain->revmap_tree_mutex);
212 domain->ops = ops;
213 domain->host_data = host_data;
214 domain->hwirq_max = hwirq_max;
215 domain->revmap_size = size;
216 domain->revmap_direct_max_irq = direct_max;
217 irq_domain_check_hierarchy(domain);
219 mutex_lock(&irq_domain_mutex);
220 debugfs_add_domain_dir(domain);
221 list_add(&domain->link, &irq_domain_list);
222 mutex_unlock(&irq_domain_mutex);
224 pr_debug("Added domain %s\n", domain->name);
225 return domain;
227 EXPORT_SYMBOL_GPL(__irq_domain_add);
230 * irq_domain_remove() - Remove an irq domain.
231 * @domain: domain to remove
233 * This routine is used to remove an irq domain. The caller must ensure
234 * that all mappings within the domain have been disposed of prior to
235 * use, depending on the revmap type.
237 void irq_domain_remove(struct irq_domain *domain)
239 mutex_lock(&irq_domain_mutex);
240 debugfs_remove_domain_dir(domain);
242 WARN_ON(!radix_tree_empty(&domain->revmap_tree));
244 list_del(&domain->link);
247 * If the going away domain is the default one, reset it.
249 if (unlikely(irq_default_domain == domain))
250 irq_set_default_host(NULL);
252 mutex_unlock(&irq_domain_mutex);
254 pr_debug("Removed domain %s\n", domain->name);
256 fwnode_handle_put(domain->fwnode);
257 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
258 kfree(domain->name);
259 kfree(domain);
261 EXPORT_SYMBOL_GPL(irq_domain_remove);
263 void irq_domain_update_bus_token(struct irq_domain *domain,
264 enum irq_domain_bus_token bus_token)
266 char *name;
268 if (domain->bus_token == bus_token)
269 return;
271 mutex_lock(&irq_domain_mutex);
273 domain->bus_token = bus_token;
275 name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
276 if (!name) {
277 mutex_unlock(&irq_domain_mutex);
278 return;
281 debugfs_remove_domain_dir(domain);
283 if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
284 kfree(domain->name);
285 else
286 domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
288 domain->name = name;
289 debugfs_add_domain_dir(domain);
291 mutex_unlock(&irq_domain_mutex);
293 EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
296 * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
297 * @of_node: pointer to interrupt controller's device tree node.
298 * @size: total number of irqs in mapping
299 * @first_irq: first number of irq block assigned to the domain,
300 * pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
301 * pre-map all of the irqs in the domain to virqs starting at first_irq.
302 * @ops: domain callbacks
303 * @host_data: Controller private data pointer
305 * Allocates an irq_domain, and optionally if first_irq is positive then also
306 * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
308 * This is intended to implement the expected behaviour for most
309 * interrupt controllers. If device tree is used, then first_irq will be 0 and
310 * irqs get mapped dynamically on the fly. However, if the controller requires
311 * static virq assignments (non-DT boot) then it will set that up correctly.
313 struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
314 unsigned int size,
315 unsigned int first_irq,
316 const struct irq_domain_ops *ops,
317 void *host_data)
319 struct irq_domain *domain;
321 domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
322 if (!domain)
323 return NULL;
325 if (first_irq > 0) {
326 if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
327 /* attempt to allocated irq_descs */
328 int rc = irq_alloc_descs(first_irq, first_irq, size,
329 of_node_to_nid(of_node));
330 if (rc < 0)
331 pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
332 first_irq);
334 irq_domain_associate_many(domain, first_irq, 0, size);
337 return domain;
339 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
342 * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
343 * @of_node: pointer to interrupt controller's device tree node.
344 * @size: total number of irqs in legacy mapping
345 * @first_irq: first number of irq block assigned to the domain
346 * @first_hwirq: first hwirq number to use for the translation. Should normally
347 * be '0', but a positive integer can be used if the effective
348 * hwirqs numbering does not begin at zero.
349 * @ops: map/unmap domain callbacks
350 * @host_data: Controller private data pointer
352 * Note: the map() callback will be called before this function returns
353 * for all legacy interrupts except 0 (which is always the invalid irq for
354 * a legacy controller).
356 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
357 unsigned int size,
358 unsigned int first_irq,
359 irq_hw_number_t first_hwirq,
360 const struct irq_domain_ops *ops,
361 void *host_data)
363 return irq_domain_create_legacy(of_node_to_fwnode(of_node), size,
364 first_irq, first_hwirq, ops, host_data);
366 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
368 struct irq_domain *irq_domain_create_legacy(struct fwnode_handle *fwnode,
369 unsigned int size,
370 unsigned int first_irq,
371 irq_hw_number_t first_hwirq,
372 const struct irq_domain_ops *ops,
373 void *host_data)
375 struct irq_domain *domain;
377 domain = __irq_domain_add(fwnode, first_hwirq + size, first_hwirq + size, 0, ops, host_data);
378 if (domain)
379 irq_domain_associate_many(domain, first_irq, first_hwirq, size);
381 return domain;
383 EXPORT_SYMBOL_GPL(irq_domain_create_legacy);
386 * irq_find_matching_fwspec() - Locates a domain for a given fwspec
387 * @fwspec: FW specifier for an interrupt
388 * @bus_token: domain-specific data
390 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
391 enum irq_domain_bus_token bus_token)
393 struct irq_domain *h, *found = NULL;
394 struct fwnode_handle *fwnode = fwspec->fwnode;
395 int rc;
397 /* We might want to match the legacy controller last since
398 * it might potentially be set to match all interrupts in
399 * the absence of a device node. This isn't a problem so far
400 * yet though...
402 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
403 * values must generate an exact match for the domain to be
404 * selected.
406 mutex_lock(&irq_domain_mutex);
407 list_for_each_entry(h, &irq_domain_list, link) {
408 if (h->ops->select && fwspec->param_count)
409 rc = h->ops->select(h, fwspec, bus_token);
410 else if (h->ops->match)
411 rc = h->ops->match(h, to_of_node(fwnode), bus_token);
412 else
413 rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
414 ((bus_token == DOMAIN_BUS_ANY) ||
415 (h->bus_token == bus_token)));
417 if (rc) {
418 found = h;
419 break;
422 mutex_unlock(&irq_domain_mutex);
423 return found;
425 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
428 * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
429 * IRQ remapping
431 * Return: false if any MSI irq domain does not support IRQ remapping,
432 * true otherwise (including if there is no MSI irq domain)
434 bool irq_domain_check_msi_remap(void)
436 struct irq_domain *h;
437 bool ret = true;
439 mutex_lock(&irq_domain_mutex);
440 list_for_each_entry(h, &irq_domain_list, link) {
441 if (irq_domain_is_msi(h) &&
442 !irq_domain_hierarchical_is_msi_remap(h)) {
443 ret = false;
444 break;
447 mutex_unlock(&irq_domain_mutex);
448 return ret;
450 EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
453 * irq_set_default_host() - Set a "default" irq domain
454 * @domain: default domain pointer
456 * For convenience, it's possible to set a "default" domain that will be used
457 * whenever NULL is passed to irq_create_mapping(). It makes life easier for
458 * platforms that want to manipulate a few hard coded interrupt numbers that
459 * aren't properly represented in the device-tree.
461 void irq_set_default_host(struct irq_domain *domain)
463 pr_debug("Default domain set to @0x%p\n", domain);
465 irq_default_domain = domain;
467 EXPORT_SYMBOL_GPL(irq_set_default_host);
470 * irq_get_default_host() - Retrieve the "default" irq domain
472 * Returns: the default domain, if any.
474 * Modern code should never use this. This should only be used on
475 * systems that cannot implement a firmware->fwnode mapping (which
476 * both DT and ACPI provide).
478 struct irq_domain *irq_get_default_host(void)
480 return irq_default_domain;
483 static void irq_domain_clear_mapping(struct irq_domain *domain,
484 irq_hw_number_t hwirq)
486 if (hwirq < domain->revmap_size) {
487 domain->linear_revmap[hwirq] = 0;
488 } else {
489 mutex_lock(&domain->revmap_tree_mutex);
490 radix_tree_delete(&domain->revmap_tree, hwirq);
491 mutex_unlock(&domain->revmap_tree_mutex);
495 static void irq_domain_set_mapping(struct irq_domain *domain,
496 irq_hw_number_t hwirq,
497 struct irq_data *irq_data)
499 if (hwirq < domain->revmap_size) {
500 domain->linear_revmap[hwirq] = irq_data->irq;
501 } else {
502 mutex_lock(&domain->revmap_tree_mutex);
503 radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
504 mutex_unlock(&domain->revmap_tree_mutex);
508 static void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
510 struct irq_data *irq_data = irq_get_irq_data(irq);
511 irq_hw_number_t hwirq;
513 if (WARN(!irq_data || irq_data->domain != domain,
514 "virq%i doesn't exist; cannot disassociate\n", irq))
515 return;
517 hwirq = irq_data->hwirq;
518 irq_set_status_flags(irq, IRQ_NOREQUEST);
520 /* remove chip and handler */
521 irq_set_chip_and_handler(irq, NULL, NULL);
523 /* Make sure it's completed */
524 synchronize_irq(irq);
526 /* Tell the PIC about it */
527 if (domain->ops->unmap)
528 domain->ops->unmap(domain, irq);
529 smp_mb();
531 irq_data->domain = NULL;
532 irq_data->hwirq = 0;
533 domain->mapcount--;
535 /* Clear reverse map for this hwirq */
536 irq_domain_clear_mapping(domain, hwirq);
539 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
540 irq_hw_number_t hwirq)
542 struct irq_data *irq_data = irq_get_irq_data(virq);
543 int ret;
545 if (WARN(hwirq >= domain->hwirq_max,
546 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
547 return -EINVAL;
548 if (WARN(!irq_data, "error: virq%i is not allocated", virq))
549 return -EINVAL;
550 if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
551 return -EINVAL;
553 mutex_lock(&irq_domain_mutex);
554 irq_data->hwirq = hwirq;
555 irq_data->domain = domain;
556 if (domain->ops->map) {
557 ret = domain->ops->map(domain, virq, hwirq);
558 if (ret != 0) {
560 * If map() returns -EPERM, this interrupt is protected
561 * by the firmware or some other service and shall not
562 * be mapped. Don't bother telling the user about it.
564 if (ret != -EPERM) {
565 pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
566 domain->name, hwirq, virq, ret);
568 irq_data->domain = NULL;
569 irq_data->hwirq = 0;
570 mutex_unlock(&irq_domain_mutex);
571 return ret;
574 /* If not already assigned, give the domain the chip's name */
575 if (!domain->name && irq_data->chip)
576 domain->name = irq_data->chip->name;
579 domain->mapcount++;
580 irq_domain_set_mapping(domain, hwirq, irq_data);
581 mutex_unlock(&irq_domain_mutex);
583 irq_clear_status_flags(virq, IRQ_NOREQUEST);
585 return 0;
587 EXPORT_SYMBOL_GPL(irq_domain_associate);
589 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
590 irq_hw_number_t hwirq_base, int count)
592 struct device_node *of_node;
593 int i;
595 of_node = irq_domain_get_of_node(domain);
596 pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
597 of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
599 for (i = 0; i < count; i++) {
600 irq_domain_associate(domain, irq_base + i, hwirq_base + i);
603 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
606 * irq_create_direct_mapping() - Allocate an irq for direct mapping
607 * @domain: domain to allocate the irq for or NULL for default domain
609 * This routine is used for irq controllers which can choose the hardware
610 * interrupt numbers they generate. In such a case it's simplest to use
611 * the linux irq as the hardware interrupt number. It still uses the linear
612 * or radix tree to store the mapping, but the irq controller can optimize
613 * the revmap path by using the hwirq directly.
615 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
617 struct device_node *of_node;
618 unsigned int virq;
620 if (domain == NULL)
621 domain = irq_default_domain;
623 of_node = irq_domain_get_of_node(domain);
624 virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
625 if (!virq) {
626 pr_debug("create_direct virq allocation failed\n");
627 return 0;
629 if (virq >= domain->revmap_direct_max_irq) {
630 pr_err("ERROR: no free irqs available below %i maximum\n",
631 domain->revmap_direct_max_irq);
632 irq_free_desc(virq);
633 return 0;
635 pr_debug("create_direct obtained virq %d\n", virq);
637 if (irq_domain_associate(domain, virq, virq)) {
638 irq_free_desc(virq);
639 return 0;
642 return virq;
644 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
647 * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
648 * @domain: domain owning this hardware interrupt or NULL for default domain
649 * @hwirq: hardware irq number in that domain space
650 * @affinity: irq affinity
652 * Only one mapping per hardware interrupt is permitted. Returns a linux
653 * irq number.
654 * If the sense/trigger is to be specified, set_irq_type() should be called
655 * on the number returned from that call.
657 unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
658 irq_hw_number_t hwirq,
659 const struct irq_affinity_desc *affinity)
661 struct device_node *of_node;
662 int virq;
664 pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
666 /* Look for default domain if nececssary */
667 if (domain == NULL)
668 domain = irq_default_domain;
669 if (domain == NULL) {
670 WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
671 return 0;
673 pr_debug("-> using domain @%p\n", domain);
675 of_node = irq_domain_get_of_node(domain);
677 /* Check if mapping already exists */
678 virq = irq_find_mapping(domain, hwirq);
679 if (virq) {
680 pr_debug("-> existing mapping on virq %d\n", virq);
681 return virq;
684 /* Allocate a virtual interrupt number */
685 virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
686 affinity);
687 if (virq <= 0) {
688 pr_debug("-> virq allocation failed\n");
689 return 0;
692 if (irq_domain_associate(domain, virq, hwirq)) {
693 irq_free_desc(virq);
694 return 0;
697 pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
698 hwirq, of_node_full_name(of_node), virq);
700 return virq;
702 EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
705 * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
706 * @domain: domain owning the interrupt range
707 * @irq_base: beginning of linux IRQ range
708 * @hwirq_base: beginning of hardware IRQ range
709 * @count: Number of interrupts to map
711 * This routine is used for allocating and mapping a range of hardware
712 * irqs to linux irqs where the linux irq numbers are at pre-defined
713 * locations. For use by controllers that already have static mappings
714 * to insert in to the domain.
716 * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
717 * domain insertion.
719 * 0 is returned upon success, while any failure to establish a static
720 * mapping is treated as an error.
722 int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
723 irq_hw_number_t hwirq_base, int count)
725 struct device_node *of_node;
726 int ret;
728 of_node = irq_domain_get_of_node(domain);
729 ret = irq_alloc_descs(irq_base, irq_base, count,
730 of_node_to_nid(of_node));
731 if (unlikely(ret < 0))
732 return ret;
734 irq_domain_associate_many(domain, irq_base, hwirq_base, count);
735 return 0;
737 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
739 static int irq_domain_translate(struct irq_domain *d,
740 struct irq_fwspec *fwspec,
741 irq_hw_number_t *hwirq, unsigned int *type)
743 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
744 if (d->ops->translate)
745 return d->ops->translate(d, fwspec, hwirq, type);
746 #endif
747 if (d->ops->xlate)
748 return d->ops->xlate(d, to_of_node(fwspec->fwnode),
749 fwspec->param, fwspec->param_count,
750 hwirq, type);
752 /* If domain has no translation, then we assume interrupt line */
753 *hwirq = fwspec->param[0];
754 return 0;
757 static void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
758 unsigned int count,
759 struct irq_fwspec *fwspec)
761 int i;
763 fwspec->fwnode = of_node_to_fwnode(np);
764 fwspec->param_count = count;
766 for (i = 0; i < count; i++)
767 fwspec->param[i] = args[i];
770 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
772 struct irq_domain *domain;
773 struct irq_data *irq_data;
774 irq_hw_number_t hwirq;
775 unsigned int type = IRQ_TYPE_NONE;
776 int virq;
778 if (fwspec->fwnode) {
779 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
780 if (!domain)
781 domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
782 } else {
783 domain = irq_default_domain;
786 if (!domain) {
787 pr_warn("no irq domain found for %s !\n",
788 of_node_full_name(to_of_node(fwspec->fwnode)));
789 return 0;
792 if (irq_domain_translate(domain, fwspec, &hwirq, &type))
793 return 0;
796 * WARN if the irqchip returns a type with bits
797 * outside the sense mask set and clear these bits.
799 if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
800 type &= IRQ_TYPE_SENSE_MASK;
803 * If we've already configured this interrupt,
804 * don't do it again, or hell will break loose.
806 virq = irq_find_mapping(domain, hwirq);
807 if (virq) {
809 * If the trigger type is not specified or matches the
810 * current trigger type then we are done so return the
811 * interrupt number.
813 if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
814 return virq;
817 * If the trigger type has not been set yet, then set
818 * it now and return the interrupt number.
820 if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
821 irq_data = irq_get_irq_data(virq);
822 if (!irq_data)
823 return 0;
825 irqd_set_trigger_type(irq_data, type);
826 return virq;
829 pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
830 hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
831 return 0;
834 if (irq_domain_is_hierarchy(domain)) {
835 virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
836 if (virq <= 0)
837 return 0;
838 } else {
839 /* Create mapping */
840 virq = irq_create_mapping(domain, hwirq);
841 if (!virq)
842 return virq;
845 irq_data = irq_get_irq_data(virq);
846 if (!irq_data) {
847 if (irq_domain_is_hierarchy(domain))
848 irq_domain_free_irqs(virq, 1);
849 else
850 irq_dispose_mapping(virq);
851 return 0;
854 /* Store trigger type */
855 irqd_set_trigger_type(irq_data, type);
857 return virq;
859 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
861 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
863 struct irq_fwspec fwspec;
865 of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
866 irq_data->args_count, &fwspec);
868 return irq_create_fwspec_mapping(&fwspec);
870 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
873 * irq_dispose_mapping() - Unmap an interrupt
874 * @virq: linux irq number of the interrupt to unmap
876 void irq_dispose_mapping(unsigned int virq)
878 struct irq_data *irq_data = irq_get_irq_data(virq);
879 struct irq_domain *domain;
881 if (!virq || !irq_data)
882 return;
884 domain = irq_data->domain;
885 if (WARN_ON(domain == NULL))
886 return;
888 if (irq_domain_is_hierarchy(domain)) {
889 irq_domain_free_irqs(virq, 1);
890 } else {
891 irq_domain_disassociate(domain, virq);
892 irq_free_desc(virq);
895 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
898 * irq_find_mapping() - Find a linux irq from a hw irq number.
899 * @domain: domain owning this hardware interrupt
900 * @hwirq: hardware irq number in that domain space
902 unsigned int irq_find_mapping(struct irq_domain *domain,
903 irq_hw_number_t hwirq)
905 struct irq_data *data;
907 /* Look for default domain if nececssary */
908 if (domain == NULL)
909 domain = irq_default_domain;
910 if (domain == NULL)
911 return 0;
913 if (hwirq < domain->revmap_direct_max_irq) {
914 data = irq_domain_get_irq_data(domain, hwirq);
915 if (data && data->hwirq == hwirq)
916 return hwirq;
919 /* Check if the hwirq is in the linear revmap. */
920 if (hwirq < domain->revmap_size)
921 return domain->linear_revmap[hwirq];
923 rcu_read_lock();
924 data = radix_tree_lookup(&domain->revmap_tree, hwirq);
925 rcu_read_unlock();
926 return data ? data->irq : 0;
928 EXPORT_SYMBOL_GPL(irq_find_mapping);
931 * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
933 * Device Tree IRQ specifier translation function which works with one cell
934 * bindings where the cell value maps directly to the hwirq number.
936 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
937 const u32 *intspec, unsigned int intsize,
938 unsigned long *out_hwirq, unsigned int *out_type)
940 if (WARN_ON(intsize < 1))
941 return -EINVAL;
942 *out_hwirq = intspec[0];
943 *out_type = IRQ_TYPE_NONE;
944 return 0;
946 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
949 * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
951 * Device Tree IRQ specifier translation function which works with two cell
952 * bindings where the cell values map directly to the hwirq number
953 * and linux irq flags.
955 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
956 const u32 *intspec, unsigned int intsize,
957 irq_hw_number_t *out_hwirq, unsigned int *out_type)
959 struct irq_fwspec fwspec;
961 of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
962 return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
964 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
967 * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
969 * Device Tree IRQ specifier translation function which works with either one
970 * or two cell bindings where the cell values map directly to the hwirq number
971 * and linux irq flags.
973 * Note: don't use this function unless your interrupt controller explicitly
974 * supports both one and two cell bindings. For the majority of controllers
975 * the _onecell() or _twocell() variants above should be used.
977 int irq_domain_xlate_onetwocell(struct irq_domain *d,
978 struct device_node *ctrlr,
979 const u32 *intspec, unsigned int intsize,
980 unsigned long *out_hwirq, unsigned int *out_type)
982 if (WARN_ON(intsize < 1))
983 return -EINVAL;
984 *out_hwirq = intspec[0];
985 if (intsize > 1)
986 *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
987 else
988 *out_type = IRQ_TYPE_NONE;
989 return 0;
991 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
993 const struct irq_domain_ops irq_domain_simple_ops = {
994 .xlate = irq_domain_xlate_onetwocell,
996 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
999 * irq_domain_translate_onecell() - Generic translate for direct one cell
1000 * bindings
1002 int irq_domain_translate_onecell(struct irq_domain *d,
1003 struct irq_fwspec *fwspec,
1004 unsigned long *out_hwirq,
1005 unsigned int *out_type)
1007 if (WARN_ON(fwspec->param_count < 1))
1008 return -EINVAL;
1009 *out_hwirq = fwspec->param[0];
1010 *out_type = IRQ_TYPE_NONE;
1011 return 0;
1013 EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
1016 * irq_domain_translate_twocell() - Generic translate for direct two cell
1017 * bindings
1019 * Device Tree IRQ specifier translation function which works with two cell
1020 * bindings where the cell values map directly to the hwirq number
1021 * and linux irq flags.
1023 int irq_domain_translate_twocell(struct irq_domain *d,
1024 struct irq_fwspec *fwspec,
1025 unsigned long *out_hwirq,
1026 unsigned int *out_type)
1028 if (WARN_ON(fwspec->param_count < 2))
1029 return -EINVAL;
1030 *out_hwirq = fwspec->param[0];
1031 *out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1032 return 0;
1034 EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1036 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1037 int node, const struct irq_affinity_desc *affinity)
1039 unsigned int hint;
1041 if (virq >= 0) {
1042 virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1043 affinity);
1044 } else {
1045 hint = hwirq % nr_irqs;
1046 if (hint == 0)
1047 hint++;
1048 virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1049 affinity);
1050 if (virq <= 0 && hint > 1) {
1051 virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1052 affinity);
1056 return virq;
1060 * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1061 * @irq_data: The pointer to irq_data
1063 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1065 irq_data->hwirq = 0;
1066 irq_data->chip = &no_irq_chip;
1067 irq_data->chip_data = NULL;
1069 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1071 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1073 * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1074 * @parent: Parent irq domain to associate with the new domain
1075 * @flags: Irq domain flags associated to the domain
1076 * @size: Size of the domain. See below
1077 * @fwnode: Optional fwnode of the interrupt controller
1078 * @ops: Pointer to the interrupt domain callbacks
1079 * @host_data: Controller private data pointer
1081 * If @size is 0 a tree domain is created, otherwise a linear domain.
1083 * If successful the parent is associated to the new domain and the
1084 * domain flags are set.
1085 * Returns pointer to IRQ domain, or NULL on failure.
1087 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1088 unsigned int flags,
1089 unsigned int size,
1090 struct fwnode_handle *fwnode,
1091 const struct irq_domain_ops *ops,
1092 void *host_data)
1094 struct irq_domain *domain;
1096 if (size)
1097 domain = irq_domain_create_linear(fwnode, size, ops, host_data);
1098 else
1099 domain = irq_domain_create_tree(fwnode, ops, host_data);
1100 if (domain) {
1101 domain->parent = parent;
1102 domain->flags |= flags;
1105 return domain;
1107 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1109 static void irq_domain_insert_irq(int virq)
1111 struct irq_data *data;
1113 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1114 struct irq_domain *domain = data->domain;
1116 domain->mapcount++;
1117 irq_domain_set_mapping(domain, data->hwirq, data);
1119 /* If not already assigned, give the domain the chip's name */
1120 if (!domain->name && data->chip)
1121 domain->name = data->chip->name;
1124 irq_clear_status_flags(virq, IRQ_NOREQUEST);
1127 static void irq_domain_remove_irq(int virq)
1129 struct irq_data *data;
1131 irq_set_status_flags(virq, IRQ_NOREQUEST);
1132 irq_set_chip_and_handler(virq, NULL, NULL);
1133 synchronize_irq(virq);
1134 smp_mb();
1136 for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1137 struct irq_domain *domain = data->domain;
1138 irq_hw_number_t hwirq = data->hwirq;
1140 domain->mapcount--;
1141 irq_domain_clear_mapping(domain, hwirq);
1145 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1146 struct irq_data *child)
1148 struct irq_data *irq_data;
1150 irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1151 irq_data_get_node(child));
1152 if (irq_data) {
1153 child->parent_data = irq_data;
1154 irq_data->irq = child->irq;
1155 irq_data->common = child->common;
1156 irq_data->domain = domain;
1159 return irq_data;
1162 static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
1164 struct irq_data *tmp;
1166 while (irq_data) {
1167 tmp = irq_data;
1168 irq_data = irq_data->parent_data;
1169 kfree(tmp);
1173 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1175 struct irq_data *irq_data, *tmp;
1176 int i;
1178 for (i = 0; i < nr_irqs; i++) {
1179 irq_data = irq_get_irq_data(virq + i);
1180 tmp = irq_data->parent_data;
1181 irq_data->parent_data = NULL;
1182 irq_data->domain = NULL;
1184 __irq_domain_free_hierarchy(tmp);
1189 * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
1190 * @domain: IRQ domain from which the hierarchy is to be disconnected
1191 * @virq: IRQ number where the hierarchy is to be trimmed
1193 * Marks the @virq level belonging to @domain as disconnected.
1194 * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
1195 * to @domain.
1197 * Its only use is to be able to trim levels of hierarchy that do not
1198 * have any real meaning for this interrupt, and that the driver marks
1199 * as such from its .alloc() callback.
1201 int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
1202 unsigned int virq)
1204 struct irq_data *irqd;
1206 irqd = irq_domain_get_irq_data(domain, virq);
1207 if (!irqd)
1208 return -EINVAL;
1210 irqd->chip = ERR_PTR(-ENOTCONN);
1211 return 0;
1214 static int irq_domain_trim_hierarchy(unsigned int virq)
1216 struct irq_data *tail, *irqd, *irq_data;
1218 irq_data = irq_get_irq_data(virq);
1219 tail = NULL;
1221 /* The first entry must have a valid irqchip */
1222 if (!irq_data->chip || IS_ERR(irq_data->chip))
1223 return -EINVAL;
1226 * Validate that the irq_data chain is sane in the presence of
1227 * a hierarchy trimming marker.
1229 for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
1230 /* Can't have a valid irqchip after a trim marker */
1231 if (irqd->chip && tail)
1232 return -EINVAL;
1234 /* Can't have an empty irqchip before a trim marker */
1235 if (!irqd->chip && !tail)
1236 return -EINVAL;
1238 if (IS_ERR(irqd->chip)) {
1239 /* Only -ENOTCONN is a valid trim marker */
1240 if (PTR_ERR(irqd->chip) != -ENOTCONN)
1241 return -EINVAL;
1243 tail = irq_data;
1247 /* No trim marker, nothing to do */
1248 if (!tail)
1249 return 0;
1251 pr_info("IRQ%d: trimming hierarchy from %s\n",
1252 virq, tail->parent_data->domain->name);
1254 /* Sever the inner part of the hierarchy... */
1255 irqd = tail;
1256 tail = tail->parent_data;
1257 irqd->parent_data = NULL;
1258 __irq_domain_free_hierarchy(tail);
1260 return 0;
1263 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1264 unsigned int virq, unsigned int nr_irqs)
1266 struct irq_data *irq_data;
1267 struct irq_domain *parent;
1268 int i;
1270 /* The outermost irq_data is embedded in struct irq_desc */
1271 for (i = 0; i < nr_irqs; i++) {
1272 irq_data = irq_get_irq_data(virq + i);
1273 irq_data->domain = domain;
1275 for (parent = domain->parent; parent; parent = parent->parent) {
1276 irq_data = irq_domain_insert_irq_data(parent, irq_data);
1277 if (!irq_data) {
1278 irq_domain_free_irq_data(virq, i + 1);
1279 return -ENOMEM;
1284 return 0;
1288 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1289 * @domain: domain to match
1290 * @virq: IRQ number to get irq_data
1292 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1293 unsigned int virq)
1295 struct irq_data *irq_data;
1297 for (irq_data = irq_get_irq_data(virq); irq_data;
1298 irq_data = irq_data->parent_data)
1299 if (irq_data->domain == domain)
1300 return irq_data;
1302 return NULL;
1304 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1307 * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1308 * @domain: Interrupt domain to match
1309 * @virq: IRQ number
1310 * @hwirq: The hwirq number
1311 * @chip: The associated interrupt chip
1312 * @chip_data: The associated chip data
1314 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1315 irq_hw_number_t hwirq, struct irq_chip *chip,
1316 void *chip_data)
1318 struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1320 if (!irq_data)
1321 return -ENOENT;
1323 irq_data->hwirq = hwirq;
1324 irq_data->chip = chip ? chip : &no_irq_chip;
1325 irq_data->chip_data = chip_data;
1327 return 0;
1329 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1332 * irq_domain_set_info - Set the complete data for a @virq in @domain
1333 * @domain: Interrupt domain to match
1334 * @virq: IRQ number
1335 * @hwirq: The hardware interrupt number
1336 * @chip: The associated interrupt chip
1337 * @chip_data: The associated interrupt chip data
1338 * @handler: The interrupt flow handler
1339 * @handler_data: The interrupt flow handler data
1340 * @handler_name: The interrupt handler name
1342 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1343 irq_hw_number_t hwirq, struct irq_chip *chip,
1344 void *chip_data, irq_flow_handler_t handler,
1345 void *handler_data, const char *handler_name)
1347 irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1348 __irq_set_handler(virq, handler, 0, handler_name);
1349 irq_set_handler_data(virq, handler_data);
1351 EXPORT_SYMBOL(irq_domain_set_info);
1354 * irq_domain_free_irqs_common - Clear irq_data and free the parent
1355 * @domain: Interrupt domain to match
1356 * @virq: IRQ number to start with
1357 * @nr_irqs: The number of irqs to free
1359 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1360 unsigned int nr_irqs)
1362 struct irq_data *irq_data;
1363 int i;
1365 for (i = 0; i < nr_irqs; i++) {
1366 irq_data = irq_domain_get_irq_data(domain, virq + i);
1367 if (irq_data)
1368 irq_domain_reset_irq_data(irq_data);
1370 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1372 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1375 * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1376 * @domain: Interrupt domain to match
1377 * @virq: IRQ number to start with
1378 * @nr_irqs: The number of irqs to free
1380 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1381 unsigned int nr_irqs)
1383 int i;
1385 for (i = 0; i < nr_irqs; i++) {
1386 irq_set_handler_data(virq + i, NULL);
1387 irq_set_handler(virq + i, NULL);
1389 irq_domain_free_irqs_common(domain, virq, nr_irqs);
1392 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1393 unsigned int irq_base,
1394 unsigned int nr_irqs)
1396 unsigned int i;
1398 if (!domain->ops->free)
1399 return;
1401 for (i = 0; i < nr_irqs; i++) {
1402 if (irq_domain_get_irq_data(domain, irq_base + i))
1403 domain->ops->free(domain, irq_base + i, 1);
1407 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1408 unsigned int irq_base,
1409 unsigned int nr_irqs, void *arg)
1411 if (!domain->ops->alloc) {
1412 pr_debug("domain->ops->alloc() is NULL\n");
1413 return -ENOSYS;
1416 return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1420 * __irq_domain_alloc_irqs - Allocate IRQs from domain
1421 * @domain: domain to allocate from
1422 * @irq_base: allocate specified IRQ number if irq_base >= 0
1423 * @nr_irqs: number of IRQs to allocate
1424 * @node: NUMA node id for memory allocation
1425 * @arg: domain specific argument
1426 * @realloc: IRQ descriptors have already been allocated if true
1427 * @affinity: Optional irq affinity mask for multiqueue devices
1429 * Allocate IRQ numbers and initialized all data structures to support
1430 * hierarchy IRQ domains.
1431 * Parameter @realloc is mainly to support legacy IRQs.
1432 * Returns error code or allocated IRQ number
1434 * The whole process to setup an IRQ has been split into two steps.
1435 * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1436 * descriptor and required hardware resources. The second step,
1437 * irq_domain_activate_irq(), is to program hardwares with preallocated
1438 * resources. In this way, it's easier to rollback when failing to
1439 * allocate resources.
1441 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1442 unsigned int nr_irqs, int node, void *arg,
1443 bool realloc, const struct irq_affinity_desc *affinity)
1445 int i, ret, virq;
1447 if (domain == NULL) {
1448 domain = irq_default_domain;
1449 if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1450 return -EINVAL;
1453 if (realloc && irq_base >= 0) {
1454 virq = irq_base;
1455 } else {
1456 virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1457 affinity);
1458 if (virq < 0) {
1459 pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1460 irq_base, nr_irqs);
1461 return virq;
1465 if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1466 pr_debug("cannot allocate memory for IRQ%d\n", virq);
1467 ret = -ENOMEM;
1468 goto out_free_desc;
1471 mutex_lock(&irq_domain_mutex);
1472 ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1473 if (ret < 0) {
1474 mutex_unlock(&irq_domain_mutex);
1475 goto out_free_irq_data;
1478 for (i = 0; i < nr_irqs; i++) {
1479 ret = irq_domain_trim_hierarchy(virq + i);
1480 if (ret) {
1481 mutex_unlock(&irq_domain_mutex);
1482 goto out_free_irq_data;
1486 for (i = 0; i < nr_irqs; i++)
1487 irq_domain_insert_irq(virq + i);
1488 mutex_unlock(&irq_domain_mutex);
1490 return virq;
1492 out_free_irq_data:
1493 irq_domain_free_irq_data(virq, nr_irqs);
1494 out_free_desc:
1495 irq_free_descs(virq, nr_irqs);
1496 return ret;
1499 /* The irq_data was moved, fix the revmap to refer to the new location */
1500 static void irq_domain_fix_revmap(struct irq_data *d)
1502 void __rcu **slot;
1504 if (d->hwirq < d->domain->revmap_size)
1505 return; /* Not using radix tree. */
1507 /* Fix up the revmap. */
1508 mutex_lock(&d->domain->revmap_tree_mutex);
1509 slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1510 if (slot)
1511 radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1512 mutex_unlock(&d->domain->revmap_tree_mutex);
1516 * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1517 * @domain: Domain to push.
1518 * @virq: Irq to push the domain in to.
1519 * @arg: Passed to the irq_domain_ops alloc() function.
1521 * For an already existing irqdomain hierarchy, as might be obtained
1522 * via a call to pci_enable_msix(), add an additional domain to the
1523 * head of the processing chain. Must be called before request_irq()
1524 * has been called.
1526 int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1528 struct irq_data *child_irq_data;
1529 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1530 struct irq_desc *desc;
1531 int rv = 0;
1534 * Check that no action has been set, which indicates the virq
1535 * is in a state where this function doesn't have to deal with
1536 * races between interrupt handling and maintaining the
1537 * hierarchy. This will catch gross misuse. Attempting to
1538 * make the check race free would require holding locks across
1539 * calls to struct irq_domain_ops->alloc(), which could lead
1540 * to deadlock, so we just do a simple check before starting.
1542 desc = irq_to_desc(virq);
1543 if (!desc)
1544 return -EINVAL;
1545 if (WARN_ON(desc->action))
1546 return -EBUSY;
1548 if (domain == NULL)
1549 return -EINVAL;
1551 if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1552 return -EINVAL;
1554 if (!root_irq_data)
1555 return -EINVAL;
1557 if (domain->parent != root_irq_data->domain)
1558 return -EINVAL;
1560 child_irq_data = kzalloc_node(sizeof(*child_irq_data), GFP_KERNEL,
1561 irq_data_get_node(root_irq_data));
1562 if (!child_irq_data)
1563 return -ENOMEM;
1565 mutex_lock(&irq_domain_mutex);
1567 /* Copy the original irq_data. */
1568 *child_irq_data = *root_irq_data;
1571 * Overwrite the root_irq_data, which is embedded in struct
1572 * irq_desc, with values for this domain.
1574 root_irq_data->parent_data = child_irq_data;
1575 root_irq_data->domain = domain;
1576 root_irq_data->mask = 0;
1577 root_irq_data->hwirq = 0;
1578 root_irq_data->chip = NULL;
1579 root_irq_data->chip_data = NULL;
1581 /* May (probably does) set hwirq, chip, etc. */
1582 rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1583 if (rv) {
1584 /* Restore the original irq_data. */
1585 *root_irq_data = *child_irq_data;
1586 kfree(child_irq_data);
1587 goto error;
1590 irq_domain_fix_revmap(child_irq_data);
1591 irq_domain_set_mapping(domain, root_irq_data->hwirq, root_irq_data);
1593 error:
1594 mutex_unlock(&irq_domain_mutex);
1596 return rv;
1598 EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1601 * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1602 * @domain: Domain to remove.
1603 * @virq: Irq to remove the domain from.
1605 * Undo the effects of a call to irq_domain_push_irq(). Must be
1606 * called either before request_irq() or after free_irq().
1608 int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1610 struct irq_data *root_irq_data = irq_get_irq_data(virq);
1611 struct irq_data *child_irq_data;
1612 struct irq_data *tmp_irq_data;
1613 struct irq_desc *desc;
1616 * Check that no action is set, which indicates the virq is in
1617 * a state where this function doesn't have to deal with races
1618 * between interrupt handling and maintaining the hierarchy.
1619 * This will catch gross misuse. Attempting to make the check
1620 * race free would require holding locks across calls to
1621 * struct irq_domain_ops->free(), which could lead to
1622 * deadlock, so we just do a simple check before starting.
1624 desc = irq_to_desc(virq);
1625 if (!desc)
1626 return -EINVAL;
1627 if (WARN_ON(desc->action))
1628 return -EBUSY;
1630 if (domain == NULL)
1631 return -EINVAL;
1633 if (!root_irq_data)
1634 return -EINVAL;
1636 tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1638 /* We can only "pop" if this domain is at the top of the list */
1639 if (WARN_ON(root_irq_data != tmp_irq_data))
1640 return -EINVAL;
1642 if (WARN_ON(root_irq_data->domain != domain))
1643 return -EINVAL;
1645 child_irq_data = root_irq_data->parent_data;
1646 if (WARN_ON(!child_irq_data))
1647 return -EINVAL;
1649 mutex_lock(&irq_domain_mutex);
1651 root_irq_data->parent_data = NULL;
1653 irq_domain_clear_mapping(domain, root_irq_data->hwirq);
1654 irq_domain_free_irqs_hierarchy(domain, virq, 1);
1656 /* Restore the original irq_data. */
1657 *root_irq_data = *child_irq_data;
1659 irq_domain_fix_revmap(root_irq_data);
1661 mutex_unlock(&irq_domain_mutex);
1663 kfree(child_irq_data);
1665 return 0;
1667 EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1670 * irq_domain_free_irqs - Free IRQ number and associated data structures
1671 * @virq: base IRQ number
1672 * @nr_irqs: number of IRQs to free
1674 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1676 struct irq_data *data = irq_get_irq_data(virq);
1677 int i;
1679 if (WARN(!data || !data->domain || !data->domain->ops->free,
1680 "NULL pointer, cannot free irq\n"))
1681 return;
1683 mutex_lock(&irq_domain_mutex);
1684 for (i = 0; i < nr_irqs; i++)
1685 irq_domain_remove_irq(virq + i);
1686 irq_domain_free_irqs_hierarchy(data->domain, virq, nr_irqs);
1687 mutex_unlock(&irq_domain_mutex);
1689 irq_domain_free_irq_data(virq, nr_irqs);
1690 irq_free_descs(virq, nr_irqs);
1694 * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1695 * @irq_base: Base IRQ number
1696 * @nr_irqs: Number of IRQs to allocate
1697 * @arg: Allocation data (arch/domain specific)
1699 * Check whether the domain has been setup recursive. If not allocate
1700 * through the parent domain.
1702 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1703 unsigned int irq_base, unsigned int nr_irqs,
1704 void *arg)
1706 if (!domain->parent)
1707 return -ENOSYS;
1709 return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1710 nr_irqs, arg);
1712 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1715 * irq_domain_free_irqs_parent - Free interrupts from parent domain
1716 * @irq_base: Base IRQ number
1717 * @nr_irqs: Number of IRQs to free
1719 * Check whether the domain has been setup recursive. If not free
1720 * through the parent domain.
1722 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1723 unsigned int irq_base, unsigned int nr_irqs)
1725 if (!domain->parent)
1726 return;
1728 irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1730 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1732 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1734 if (irq_data && irq_data->domain) {
1735 struct irq_domain *domain = irq_data->domain;
1737 if (domain->ops->deactivate)
1738 domain->ops->deactivate(domain, irq_data);
1739 if (irq_data->parent_data)
1740 __irq_domain_deactivate_irq(irq_data->parent_data);
1744 static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1746 int ret = 0;
1748 if (irqd && irqd->domain) {
1749 struct irq_domain *domain = irqd->domain;
1751 if (irqd->parent_data)
1752 ret = __irq_domain_activate_irq(irqd->parent_data,
1753 reserve);
1754 if (!ret && domain->ops->activate) {
1755 ret = domain->ops->activate(domain, irqd, reserve);
1756 /* Rollback in case of error */
1757 if (ret && irqd->parent_data)
1758 __irq_domain_deactivate_irq(irqd->parent_data);
1761 return ret;
1765 * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1766 * interrupt
1767 * @irq_data: Outermost irq_data associated with interrupt
1768 * @reserve: If set only reserve an interrupt vector instead of assigning one
1770 * This is the second step to call domain_ops->activate to program interrupt
1771 * controllers, so the interrupt could actually get delivered.
1773 int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1775 int ret = 0;
1777 if (!irqd_is_activated(irq_data))
1778 ret = __irq_domain_activate_irq(irq_data, reserve);
1779 if (!ret)
1780 irqd_set_activated(irq_data);
1781 return ret;
1785 * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1786 * deactivate interrupt
1787 * @irq_data: outermost irq_data associated with interrupt
1789 * It calls domain_ops->deactivate to program interrupt controllers to disable
1790 * interrupt delivery.
1792 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1794 if (irqd_is_activated(irq_data)) {
1795 __irq_domain_deactivate_irq(irq_data);
1796 irqd_clr_activated(irq_data);
1800 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1802 /* Hierarchy irq_domains must implement callback alloc() */
1803 if (domain->ops->alloc)
1804 domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1808 * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1809 * parent has MSI remapping support
1810 * @domain: domain pointer
1812 bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1814 for (; domain; domain = domain->parent) {
1815 if (irq_domain_is_msi_remap(domain))
1816 return true;
1818 return false;
1820 #else /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1822 * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1823 * @domain: domain to match
1824 * @virq: IRQ number to get irq_data
1826 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1827 unsigned int virq)
1829 struct irq_data *irq_data = irq_get_irq_data(virq);
1831 return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1833 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1836 * irq_domain_set_info - Set the complete data for a @virq in @domain
1837 * @domain: Interrupt domain to match
1838 * @virq: IRQ number
1839 * @hwirq: The hardware interrupt number
1840 * @chip: The associated interrupt chip
1841 * @chip_data: The associated interrupt chip data
1842 * @handler: The interrupt flow handler
1843 * @handler_data: The interrupt flow handler data
1844 * @handler_name: The interrupt handler name
1846 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1847 irq_hw_number_t hwirq, struct irq_chip *chip,
1848 void *chip_data, irq_flow_handler_t handler,
1849 void *handler_data, const char *handler_name)
1851 irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1852 irq_set_chip_data(virq, chip_data);
1853 irq_set_handler_data(virq, handler_data);
1856 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1859 #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
1861 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1862 static struct dentry *domain_dir;
1864 static void
1865 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1867 seq_printf(m, "%*sname: %s\n", ind, "", d->name);
1868 seq_printf(m, "%*ssize: %u\n", ind + 1, "",
1869 d->revmap_size + d->revmap_direct_max_irq);
1870 seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1871 seq_printf(m, "%*sflags: 0x%08x\n", ind +1 , "", d->flags);
1872 if (d->ops && d->ops->debug_show)
1873 d->ops->debug_show(m, d, NULL, ind + 1);
1874 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
1875 if (!d->parent)
1876 return;
1877 seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1878 irq_domain_debug_show_one(m, d->parent, ind + 4);
1879 #endif
1882 static int irq_domain_debug_show(struct seq_file *m, void *p)
1884 struct irq_domain *d = m->private;
1886 /* Default domain? Might be NULL */
1887 if (!d) {
1888 if (!irq_default_domain)
1889 return 0;
1890 d = irq_default_domain;
1892 irq_domain_debug_show_one(m, d, 0);
1893 return 0;
1895 DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1897 static void debugfs_add_domain_dir(struct irq_domain *d)
1899 if (!d->name || !domain_dir || d->debugfs_file)
1900 return;
1901 d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
1902 &irq_domain_debug_fops);
1905 static void debugfs_remove_domain_dir(struct irq_domain *d)
1907 debugfs_remove(d->debugfs_file);
1908 d->debugfs_file = NULL;
1911 void __init irq_domain_debugfs_init(struct dentry *root)
1913 struct irq_domain *d;
1915 domain_dir = debugfs_create_dir("domains", root);
1917 debugfs_create_file("default", 0444, domain_dir, NULL,
1918 &irq_domain_debug_fops);
1919 mutex_lock(&irq_domain_mutex);
1920 list_for_each_entry(d, &irq_domain_list, link)
1921 debugfs_add_domain_dir(d);
1922 mutex_unlock(&irq_domain_mutex);
1924 #endif