xref: /openbmc/linux/kernel/irq/irqdomain.c (revision 7effbd18)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #define pr_fmt(fmt)  "irq: " fmt
4 
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>
22 
23 static LIST_HEAD(irq_domain_list);
24 static DEFINE_MUTEX(irq_domain_mutex);
25 
26 static struct irq_domain *irq_default_domain;
27 
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);
32 
33 struct irqchip_fwid {
34 	struct fwnode_handle	fwnode;
35 	unsigned int		type;
36 	char			*name;
37 	phys_addr_t		*pa;
38 };
39 
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);
43 #else
44 static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
45 static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
46 #endif
47 
48 static const char *irqchip_fwnode_get_name(const struct fwnode_handle *fwnode)
49 {
50 	struct irqchip_fwid *fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
51 
52 	return fwid->name;
53 }
54 
55 const struct fwnode_operations irqchip_fwnode_ops = {
56 	.get_name = irqchip_fwnode_get_name,
57 };
58 EXPORT_SYMBOL_GPL(irqchip_fwnode_ops);
59 
60 /**
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
67  *
68  * Allocate a struct irqchip_fwid, and return a pointer to the embedded
69  * fwnode_handle (or NULL on failure).
70  *
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
74  * domain struct.
75  */
76 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
77 						const char *name,
78 						phys_addr_t *pa)
79 {
80 	struct irqchip_fwid *fwid;
81 	char *n;
82 
83 	fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
84 
85 	switch (type) {
86 	case IRQCHIP_FWNODE_NAMED:
87 		n = kasprintf(GFP_KERNEL, "%s", name);
88 		break;
89 	case IRQCHIP_FWNODE_NAMED_ID:
90 		n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
91 		break;
92 	default:
93 		n = kasprintf(GFP_KERNEL, "irqchip@%pa", pa);
94 		break;
95 	}
96 
97 	if (!fwid || !n) {
98 		kfree(fwid);
99 		kfree(n);
100 		return NULL;
101 	}
102 
103 	fwid->type = type;
104 	fwid->name = n;
105 	fwid->pa = pa;
106 	fwnode_init(&fwid->fwnode, &irqchip_fwnode_ops);
107 	return &fwid->fwnode;
108 }
109 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
110 
111 /**
112  * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
113  *
114  * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
115  */
116 void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
117 {
118 	struct irqchip_fwid *fwid;
119 
120 	if (!fwnode || WARN_ON(!is_fwnode_irqchip(fwnode)))
121 		return;
122 
123 	fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
124 	kfree(fwid->name);
125 	kfree(fwid);
126 }
127 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
128 
129 static struct irq_domain *__irq_domain_create(struct fwnode_handle *fwnode,
130 					      unsigned int size,
131 					      irq_hw_number_t hwirq_max,
132 					      int direct_max,
133 					      const struct irq_domain_ops *ops,
134 					      void *host_data)
135 {
136 	struct irqchip_fwid *fwid;
137 	struct irq_domain *domain;
138 
139 	static atomic_t unknown_domains;
140 
141 	if (WARN_ON((size && direct_max) ||
142 		    (!IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) && direct_max) ||
143 		    (direct_max && (direct_max != hwirq_max))))
144 		return NULL;
145 
146 	domain = kzalloc_node(struct_size(domain, revmap, size),
147 			      GFP_KERNEL, of_node_to_nid(to_of_node(fwnode)));
148 	if (!domain)
149 		return NULL;
150 
151 	if (is_fwnode_irqchip(fwnode)) {
152 		fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
153 
154 		switch (fwid->type) {
155 		case IRQCHIP_FWNODE_NAMED:
156 		case IRQCHIP_FWNODE_NAMED_ID:
157 			domain->fwnode = fwnode;
158 			domain->name = kstrdup(fwid->name, GFP_KERNEL);
159 			if (!domain->name) {
160 				kfree(domain);
161 				return NULL;
162 			}
163 			domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
164 			break;
165 		default:
166 			domain->fwnode = fwnode;
167 			domain->name = fwid->name;
168 			break;
169 		}
170 	} else if (is_of_node(fwnode) || is_acpi_device_node(fwnode) ||
171 		   is_software_node(fwnode)) {
172 		char *name;
173 
174 		/*
175 		 * fwnode paths contain '/', which debugfs is legitimately
176 		 * unhappy about. Replace them with ':', which does
177 		 * the trick and is not as offensive as '\'...
178 		 */
179 		name = kasprintf(GFP_KERNEL, "%pfw", fwnode);
180 		if (!name) {
181 			kfree(domain);
182 			return NULL;
183 		}
184 
185 		strreplace(name, '/', ':');
186 
187 		domain->name = name;
188 		domain->fwnode = fwnode;
189 		domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
190 	}
191 
192 	if (!domain->name) {
193 		if (fwnode)
194 			pr_err("Invalid fwnode type for irqdomain\n");
195 		domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
196 					 atomic_inc_return(&unknown_domains));
197 		if (!domain->name) {
198 			kfree(domain);
199 			return NULL;
200 		}
201 		domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
202 	}
203 
204 	fwnode_handle_get(fwnode);
205 	fwnode_dev_initialized(fwnode, true);
206 
207 	/* Fill structure */
208 	INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
209 	domain->ops = ops;
210 	domain->host_data = host_data;
211 	domain->hwirq_max = hwirq_max;
212 
213 	if (direct_max)
214 		domain->flags |= IRQ_DOMAIN_FLAG_NO_MAP;
215 
216 	domain->revmap_size = size;
217 
218 	/*
219 	 * Hierarchical domains use the domain lock of the root domain
220 	 * (innermost domain).
221 	 *
222 	 * For non-hierarchical domains (as for root domains), the root
223 	 * pointer is set to the domain itself so that &domain->root->mutex
224 	 * always points to the right lock.
225 	 */
226 	mutex_init(&domain->mutex);
227 	domain->root = domain;
228 
229 	irq_domain_check_hierarchy(domain);
230 
231 	return domain;
232 }
233 
234 static void __irq_domain_publish(struct irq_domain *domain)
235 {
236 	mutex_lock(&irq_domain_mutex);
237 	debugfs_add_domain_dir(domain);
238 	list_add(&domain->link, &irq_domain_list);
239 	mutex_unlock(&irq_domain_mutex);
240 
241 	pr_debug("Added domain %s\n", domain->name);
242 }
243 
244 /**
245  * __irq_domain_add() - Allocate a new irq_domain data structure
246  * @fwnode: firmware node for the interrupt controller
247  * @size: Size of linear map; 0 for radix mapping only
248  * @hwirq_max: Maximum number of interrupts supported by controller
249  * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
250  *              direct mapping
251  * @ops: domain callbacks
252  * @host_data: Controller private data pointer
253  *
254  * Allocates and initializes an irq_domain structure.
255  * Returns pointer to IRQ domain, or NULL on failure.
256  */
257 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, unsigned int size,
258 				    irq_hw_number_t hwirq_max, int direct_max,
259 				    const struct irq_domain_ops *ops,
260 				    void *host_data)
261 {
262 	struct irq_domain *domain;
263 
264 	domain = __irq_domain_create(fwnode, size, hwirq_max, direct_max,
265 				     ops, host_data);
266 	if (domain)
267 		__irq_domain_publish(domain);
268 
269 	return domain;
270 }
271 EXPORT_SYMBOL_GPL(__irq_domain_add);
272 
273 /**
274  * irq_domain_remove() - Remove an irq domain.
275  * @domain: domain to remove
276  *
277  * This routine is used to remove an irq domain. The caller must ensure
278  * that all mappings within the domain have been disposed of prior to
279  * use, depending on the revmap type.
280  */
281 void irq_domain_remove(struct irq_domain *domain)
282 {
283 	mutex_lock(&irq_domain_mutex);
284 	debugfs_remove_domain_dir(domain);
285 
286 	WARN_ON(!radix_tree_empty(&domain->revmap_tree));
287 
288 	list_del(&domain->link);
289 
290 	/*
291 	 * If the going away domain is the default one, reset it.
292 	 */
293 	if (unlikely(irq_default_domain == domain))
294 		irq_set_default_host(NULL);
295 
296 	mutex_unlock(&irq_domain_mutex);
297 
298 	pr_debug("Removed domain %s\n", domain->name);
299 
300 	fwnode_dev_initialized(domain->fwnode, false);
301 	fwnode_handle_put(domain->fwnode);
302 	if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
303 		kfree(domain->name);
304 	kfree(domain);
305 }
306 EXPORT_SYMBOL_GPL(irq_domain_remove);
307 
308 void irq_domain_update_bus_token(struct irq_domain *domain,
309 				 enum irq_domain_bus_token bus_token)
310 {
311 	char *name;
312 
313 	if (domain->bus_token == bus_token)
314 		return;
315 
316 	mutex_lock(&irq_domain_mutex);
317 
318 	domain->bus_token = bus_token;
319 
320 	name = kasprintf(GFP_KERNEL, "%s-%d", domain->name, bus_token);
321 	if (!name) {
322 		mutex_unlock(&irq_domain_mutex);
323 		return;
324 	}
325 
326 	debugfs_remove_domain_dir(domain);
327 
328 	if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
329 		kfree(domain->name);
330 	else
331 		domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
332 
333 	domain->name = name;
334 	debugfs_add_domain_dir(domain);
335 
336 	mutex_unlock(&irq_domain_mutex);
337 }
338 EXPORT_SYMBOL_GPL(irq_domain_update_bus_token);
339 
340 /**
341  * irq_domain_create_simple() - Register an irq_domain and optionally map a range of irqs
342  * @fwnode: firmware node for the interrupt controller
343  * @size: total number of irqs in mapping
344  * @first_irq: first number of irq block assigned to the domain,
345  *	pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
346  *	pre-map all of the irqs in the domain to virqs starting at first_irq.
347  * @ops: domain callbacks
348  * @host_data: Controller private data pointer
349  *
350  * Allocates an irq_domain, and optionally if first_irq is positive then also
351  * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
352  *
353  * This is intended to implement the expected behaviour for most
354  * interrupt controllers. If device tree is used, then first_irq will be 0 and
355  * irqs get mapped dynamically on the fly. However, if the controller requires
356  * static virq assignments (non-DT boot) then it will set that up correctly.
357  */
358 struct irq_domain *irq_domain_create_simple(struct fwnode_handle *fwnode,
359 					    unsigned int size,
360 					    unsigned int first_irq,
361 					    const struct irq_domain_ops *ops,
362 					    void *host_data)
363 {
364 	struct irq_domain *domain;
365 
366 	domain = __irq_domain_add(fwnode, size, size, 0, ops, host_data);
367 	if (!domain)
368 		return NULL;
369 
370 	if (first_irq > 0) {
371 		if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
372 			/* attempt to allocated irq_descs */
373 			int rc = irq_alloc_descs(first_irq, first_irq, size,
374 						 of_node_to_nid(to_of_node(fwnode)));
375 			if (rc < 0)
376 				pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
377 					first_irq);
378 		}
379 		irq_domain_associate_many(domain, first_irq, 0, size);
380 	}
381 
382 	return domain;
383 }
384 EXPORT_SYMBOL_GPL(irq_domain_create_simple);
385 
386 /**
387  * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
388  * @of_node: pointer to interrupt controller's device tree node.
389  * @size: total number of irqs in legacy mapping
390  * @first_irq: first number of irq block assigned to the domain
391  * @first_hwirq: first hwirq number to use for the translation. Should normally
392  *               be '0', but a positive integer can be used if the effective
393  *               hwirqs numbering does not begin at zero.
394  * @ops: map/unmap domain callbacks
395  * @host_data: Controller private data pointer
396  *
397  * Note: the map() callback will be called before this function returns
398  * for all legacy interrupts except 0 (which is always the invalid irq for
399  * a legacy controller).
400  */
401 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
402 					 unsigned int size,
403 					 unsigned int first_irq,
404 					 irq_hw_number_t first_hwirq,
405 					 const struct irq_domain_ops *ops,
406 					 void *host_data)
407 {
408 	return irq_domain_create_legacy(of_node_to_fwnode(of_node), size,
409 					first_irq, first_hwirq, ops, host_data);
410 }
411 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
412 
413 struct irq_domain *irq_domain_create_legacy(struct fwnode_handle *fwnode,
414 					 unsigned int size,
415 					 unsigned int first_irq,
416 					 irq_hw_number_t first_hwirq,
417 					 const struct irq_domain_ops *ops,
418 					 void *host_data)
419 {
420 	struct irq_domain *domain;
421 
422 	domain = __irq_domain_add(fwnode, first_hwirq + size, first_hwirq + size, 0, ops, host_data);
423 	if (domain)
424 		irq_domain_associate_many(domain, first_irq, first_hwirq, size);
425 
426 	return domain;
427 }
428 EXPORT_SYMBOL_GPL(irq_domain_create_legacy);
429 
430 /**
431  * irq_find_matching_fwspec() - Locates a domain for a given fwspec
432  * @fwspec: FW specifier for an interrupt
433  * @bus_token: domain-specific data
434  */
435 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
436 					    enum irq_domain_bus_token bus_token)
437 {
438 	struct irq_domain *h, *found = NULL;
439 	struct fwnode_handle *fwnode = fwspec->fwnode;
440 	int rc;
441 
442 	/* We might want to match the legacy controller last since
443 	 * it might potentially be set to match all interrupts in
444 	 * the absence of a device node. This isn't a problem so far
445 	 * yet though...
446 	 *
447 	 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
448 	 * values must generate an exact match for the domain to be
449 	 * selected.
450 	 */
451 	mutex_lock(&irq_domain_mutex);
452 	list_for_each_entry(h, &irq_domain_list, link) {
453 		if (h->ops->select && fwspec->param_count)
454 			rc = h->ops->select(h, fwspec, bus_token);
455 		else if (h->ops->match)
456 			rc = h->ops->match(h, to_of_node(fwnode), bus_token);
457 		else
458 			rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
459 			      ((bus_token == DOMAIN_BUS_ANY) ||
460 			       (h->bus_token == bus_token)));
461 
462 		if (rc) {
463 			found = h;
464 			break;
465 		}
466 	}
467 	mutex_unlock(&irq_domain_mutex);
468 	return found;
469 }
470 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
471 
472 /**
473  * irq_set_default_host() - Set a "default" irq domain
474  * @domain: default domain pointer
475  *
476  * For convenience, it's possible to set a "default" domain that will be used
477  * whenever NULL is passed to irq_create_mapping(). It makes life easier for
478  * platforms that want to manipulate a few hard coded interrupt numbers that
479  * aren't properly represented in the device-tree.
480  */
481 void irq_set_default_host(struct irq_domain *domain)
482 {
483 	pr_debug("Default domain set to @0x%p\n", domain);
484 
485 	irq_default_domain = domain;
486 }
487 EXPORT_SYMBOL_GPL(irq_set_default_host);
488 
489 /**
490  * irq_get_default_host() - Retrieve the "default" irq domain
491  *
492  * Returns: the default domain, if any.
493  *
494  * Modern code should never use this. This should only be used on
495  * systems that cannot implement a firmware->fwnode mapping (which
496  * both DT and ACPI provide).
497  */
498 struct irq_domain *irq_get_default_host(void)
499 {
500 	return irq_default_domain;
501 }
502 EXPORT_SYMBOL_GPL(irq_get_default_host);
503 
504 static bool irq_domain_is_nomap(struct irq_domain *domain)
505 {
506 	return IS_ENABLED(CONFIG_IRQ_DOMAIN_NOMAP) &&
507 	       (domain->flags & IRQ_DOMAIN_FLAG_NO_MAP);
508 }
509 
510 static void irq_domain_clear_mapping(struct irq_domain *domain,
511 				     irq_hw_number_t hwirq)
512 {
513 	lockdep_assert_held(&domain->root->mutex);
514 
515 	if (irq_domain_is_nomap(domain))
516 		return;
517 
518 	if (hwirq < domain->revmap_size)
519 		rcu_assign_pointer(domain->revmap[hwirq], NULL);
520 	else
521 		radix_tree_delete(&domain->revmap_tree, hwirq);
522 }
523 
524 static void irq_domain_set_mapping(struct irq_domain *domain,
525 				   irq_hw_number_t hwirq,
526 				   struct irq_data *irq_data)
527 {
528 	/*
529 	 * This also makes sure that all domains point to the same root when
530 	 * called from irq_domain_insert_irq() for each domain in a hierarchy.
531 	 */
532 	lockdep_assert_held(&domain->root->mutex);
533 
534 	if (irq_domain_is_nomap(domain))
535 		return;
536 
537 	if (hwirq < domain->revmap_size)
538 		rcu_assign_pointer(domain->revmap[hwirq], irq_data);
539 	else
540 		radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
541 }
542 
543 static void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
544 {
545 	struct irq_data *irq_data = irq_get_irq_data(irq);
546 	irq_hw_number_t hwirq;
547 
548 	if (WARN(!irq_data || irq_data->domain != domain,
549 		 "virq%i doesn't exist; cannot disassociate\n", irq))
550 		return;
551 
552 	hwirq = irq_data->hwirq;
553 
554 	mutex_lock(&domain->root->mutex);
555 
556 	irq_set_status_flags(irq, IRQ_NOREQUEST);
557 
558 	/* remove chip and handler */
559 	irq_set_chip_and_handler(irq, NULL, NULL);
560 
561 	/* Make sure it's completed */
562 	synchronize_irq(irq);
563 
564 	/* Tell the PIC about it */
565 	if (domain->ops->unmap)
566 		domain->ops->unmap(domain, irq);
567 	smp_mb();
568 
569 	irq_data->domain = NULL;
570 	irq_data->hwirq = 0;
571 	domain->mapcount--;
572 
573 	/* Clear reverse map for this hwirq */
574 	irq_domain_clear_mapping(domain, hwirq);
575 
576 	mutex_unlock(&domain->root->mutex);
577 }
578 
579 static int irq_domain_associate_locked(struct irq_domain *domain, unsigned int virq,
580 				       irq_hw_number_t hwirq)
581 {
582 	struct irq_data *irq_data = irq_get_irq_data(virq);
583 	int ret;
584 
585 	if (WARN(hwirq >= domain->hwirq_max,
586 		 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
587 		return -EINVAL;
588 	if (WARN(!irq_data, "error: virq%i is not allocated", virq))
589 		return -EINVAL;
590 	if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
591 		return -EINVAL;
592 
593 	irq_data->hwirq = hwirq;
594 	irq_data->domain = domain;
595 	if (domain->ops->map) {
596 		ret = domain->ops->map(domain, virq, hwirq);
597 		if (ret != 0) {
598 			/*
599 			 * If map() returns -EPERM, this interrupt is protected
600 			 * by the firmware or some other service and shall not
601 			 * be mapped. Don't bother telling the user about it.
602 			 */
603 			if (ret != -EPERM) {
604 				pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
605 				       domain->name, hwirq, virq, ret);
606 			}
607 			irq_data->domain = NULL;
608 			irq_data->hwirq = 0;
609 			return ret;
610 		}
611 	}
612 
613 	domain->mapcount++;
614 	irq_domain_set_mapping(domain, hwirq, irq_data);
615 
616 	irq_clear_status_flags(virq, IRQ_NOREQUEST);
617 
618 	return 0;
619 }
620 
621 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
622 			 irq_hw_number_t hwirq)
623 {
624 	int ret;
625 
626 	mutex_lock(&domain->root->mutex);
627 	ret = irq_domain_associate_locked(domain, virq, hwirq);
628 	mutex_unlock(&domain->root->mutex);
629 
630 	return ret;
631 }
632 EXPORT_SYMBOL_GPL(irq_domain_associate);
633 
634 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
635 			       irq_hw_number_t hwirq_base, int count)
636 {
637 	struct device_node *of_node;
638 	int i;
639 
640 	of_node = irq_domain_get_of_node(domain);
641 	pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
642 		of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
643 
644 	for (i = 0; i < count; i++)
645 		irq_domain_associate(domain, irq_base + i, hwirq_base + i);
646 }
647 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
648 
649 #ifdef CONFIG_IRQ_DOMAIN_NOMAP
650 /**
651  * irq_create_direct_mapping() - Allocate an irq for direct mapping
652  * @domain: domain to allocate the irq for or NULL for default domain
653  *
654  * This routine is used for irq controllers which can choose the hardware
655  * interrupt numbers they generate. In such a case it's simplest to use
656  * the linux irq as the hardware interrupt number. It still uses the linear
657  * or radix tree to store the mapping, but the irq controller can optimize
658  * the revmap path by using the hwirq directly.
659  */
660 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
661 {
662 	struct device_node *of_node;
663 	unsigned int virq;
664 
665 	if (domain == NULL)
666 		domain = irq_default_domain;
667 
668 	of_node = irq_domain_get_of_node(domain);
669 	virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
670 	if (!virq) {
671 		pr_debug("create_direct virq allocation failed\n");
672 		return 0;
673 	}
674 	if (virq >= domain->hwirq_max) {
675 		pr_err("ERROR: no free irqs available below %lu maximum\n",
676 			domain->hwirq_max);
677 		irq_free_desc(virq);
678 		return 0;
679 	}
680 	pr_debug("create_direct obtained virq %d\n", virq);
681 
682 	if (irq_domain_associate(domain, virq, virq)) {
683 		irq_free_desc(virq);
684 		return 0;
685 	}
686 
687 	return virq;
688 }
689 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
690 #endif
691 
692 static unsigned int irq_create_mapping_affinity_locked(struct irq_domain *domain,
693 						       irq_hw_number_t hwirq,
694 						       const struct irq_affinity_desc *affinity)
695 {
696 	struct device_node *of_node = irq_domain_get_of_node(domain);
697 	int virq;
698 
699 	pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
700 
701 	/* Allocate a virtual interrupt number */
702 	virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node),
703 				      affinity);
704 	if (virq <= 0) {
705 		pr_debug("-> virq allocation failed\n");
706 		return 0;
707 	}
708 
709 	if (irq_domain_associate_locked(domain, virq, hwirq)) {
710 		irq_free_desc(virq);
711 		return 0;
712 	}
713 
714 	pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
715 		hwirq, of_node_full_name(of_node), virq);
716 
717 	return virq;
718 }
719 
720 /**
721  * irq_create_mapping_affinity() - Map a hardware interrupt into linux irq space
722  * @domain: domain owning this hardware interrupt or NULL for default domain
723  * @hwirq: hardware irq number in that domain space
724  * @affinity: irq affinity
725  *
726  * Only one mapping per hardware interrupt is permitted. Returns a linux
727  * irq number.
728  * If the sense/trigger is to be specified, set_irq_type() should be called
729  * on the number returned from that call.
730  */
731 unsigned int irq_create_mapping_affinity(struct irq_domain *domain,
732 					 irq_hw_number_t hwirq,
733 					 const struct irq_affinity_desc *affinity)
734 {
735 	int virq;
736 
737 	/* Look for default domain if necessary */
738 	if (domain == NULL)
739 		domain = irq_default_domain;
740 	if (domain == NULL) {
741 		WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
742 		return 0;
743 	}
744 
745 	mutex_lock(&domain->root->mutex);
746 
747 	/* Check if mapping already exists */
748 	virq = irq_find_mapping(domain, hwirq);
749 	if (virq) {
750 		pr_debug("existing mapping on virq %d\n", virq);
751 		goto out;
752 	}
753 
754 	virq = irq_create_mapping_affinity_locked(domain, hwirq, affinity);
755 out:
756 	mutex_unlock(&domain->root->mutex);
757 
758 	return virq;
759 }
760 EXPORT_SYMBOL_GPL(irq_create_mapping_affinity);
761 
762 static int irq_domain_translate(struct irq_domain *d,
763 				struct irq_fwspec *fwspec,
764 				irq_hw_number_t *hwirq, unsigned int *type)
765 {
766 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
767 	if (d->ops->translate)
768 		return d->ops->translate(d, fwspec, hwirq, type);
769 #endif
770 	if (d->ops->xlate)
771 		return d->ops->xlate(d, to_of_node(fwspec->fwnode),
772 				     fwspec->param, fwspec->param_count,
773 				     hwirq, type);
774 
775 	/* If domain has no translation, then we assume interrupt line */
776 	*hwirq = fwspec->param[0];
777 	return 0;
778 }
779 
780 void of_phandle_args_to_fwspec(struct device_node *np, const u32 *args,
781 			       unsigned int count, struct irq_fwspec *fwspec)
782 {
783 	int i;
784 
785 	fwspec->fwnode = of_node_to_fwnode(np);
786 	fwspec->param_count = count;
787 
788 	for (i = 0; i < count; i++)
789 		fwspec->param[i] = args[i];
790 }
791 EXPORT_SYMBOL_GPL(of_phandle_args_to_fwspec);
792 
793 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
794 {
795 	struct irq_domain *domain;
796 	struct irq_data *irq_data;
797 	irq_hw_number_t hwirq;
798 	unsigned int type = IRQ_TYPE_NONE;
799 	int virq;
800 
801 	if (fwspec->fwnode) {
802 		domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
803 		if (!domain)
804 			domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
805 	} else {
806 		domain = irq_default_domain;
807 	}
808 
809 	if (!domain) {
810 		pr_warn("no irq domain found for %s !\n",
811 			of_node_full_name(to_of_node(fwspec->fwnode)));
812 		return 0;
813 	}
814 
815 	if (irq_domain_translate(domain, fwspec, &hwirq, &type))
816 		return 0;
817 
818 	/*
819 	 * WARN if the irqchip returns a type with bits
820 	 * outside the sense mask set and clear these bits.
821 	 */
822 	if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
823 		type &= IRQ_TYPE_SENSE_MASK;
824 
825 	mutex_lock(&domain->root->mutex);
826 
827 	/*
828 	 * If we've already configured this interrupt,
829 	 * don't do it again, or hell will break loose.
830 	 */
831 	virq = irq_find_mapping(domain, hwirq);
832 	if (virq) {
833 		/*
834 		 * If the trigger type is not specified or matches the
835 		 * current trigger type then we are done so return the
836 		 * interrupt number.
837 		 */
838 		if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
839 			goto out;
840 
841 		/*
842 		 * If the trigger type has not been set yet, then set
843 		 * it now and return the interrupt number.
844 		 */
845 		if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
846 			irq_data = irq_get_irq_data(virq);
847 			if (!irq_data) {
848 				virq = 0;
849 				goto out;
850 			}
851 
852 			irqd_set_trigger_type(irq_data, type);
853 			goto out;
854 		}
855 
856 		pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
857 			hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
858 		virq = 0;
859 		goto out;
860 	}
861 
862 	if (irq_domain_is_hierarchy(domain)) {
863 		virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE,
864 						    fwspec, false, NULL);
865 		if (virq <= 0) {
866 			virq = 0;
867 			goto out;
868 		}
869 	} else {
870 		/* Create mapping */
871 		virq = irq_create_mapping_affinity_locked(domain, hwirq, NULL);
872 		if (!virq)
873 			goto out;
874 	}
875 
876 	irq_data = irq_get_irq_data(virq);
877 	if (WARN_ON(!irq_data)) {
878 		virq = 0;
879 		goto out;
880 	}
881 
882 	/* Store trigger type */
883 	irqd_set_trigger_type(irq_data, type);
884 out:
885 	mutex_unlock(&domain->root->mutex);
886 
887 	return virq;
888 }
889 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
890 
891 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
892 {
893 	struct irq_fwspec fwspec;
894 
895 	of_phandle_args_to_fwspec(irq_data->np, irq_data->args,
896 				  irq_data->args_count, &fwspec);
897 
898 	return irq_create_fwspec_mapping(&fwspec);
899 }
900 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
901 
902 /**
903  * irq_dispose_mapping() - Unmap an interrupt
904  * @virq: linux irq number of the interrupt to unmap
905  */
906 void irq_dispose_mapping(unsigned int virq)
907 {
908 	struct irq_data *irq_data = irq_get_irq_data(virq);
909 	struct irq_domain *domain;
910 
911 	if (!virq || !irq_data)
912 		return;
913 
914 	domain = irq_data->domain;
915 	if (WARN_ON(domain == NULL))
916 		return;
917 
918 	if (irq_domain_is_hierarchy(domain)) {
919 		irq_domain_free_irqs(virq, 1);
920 	} else {
921 		irq_domain_disassociate(domain, virq);
922 		irq_free_desc(virq);
923 	}
924 }
925 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
926 
927 /**
928  * __irq_resolve_mapping() - Find a linux irq from a hw irq number.
929  * @domain: domain owning this hardware interrupt
930  * @hwirq: hardware irq number in that domain space
931  * @irq: optional pointer to return the Linux irq if required
932  *
933  * Returns the interrupt descriptor.
934  */
935 struct irq_desc *__irq_resolve_mapping(struct irq_domain *domain,
936 				       irq_hw_number_t hwirq,
937 				       unsigned int *irq)
938 {
939 	struct irq_desc *desc = NULL;
940 	struct irq_data *data;
941 
942 	/* Look for default domain if necessary */
943 	if (domain == NULL)
944 		domain = irq_default_domain;
945 	if (domain == NULL)
946 		return desc;
947 
948 	if (irq_domain_is_nomap(domain)) {
949 		if (hwirq < domain->hwirq_max) {
950 			data = irq_domain_get_irq_data(domain, hwirq);
951 			if (data && data->hwirq == hwirq)
952 				desc = irq_data_to_desc(data);
953 			if (irq && desc)
954 				*irq = hwirq;
955 		}
956 
957 		return desc;
958 	}
959 
960 	rcu_read_lock();
961 	/* Check if the hwirq is in the linear revmap. */
962 	if (hwirq < domain->revmap_size)
963 		data = rcu_dereference(domain->revmap[hwirq]);
964 	else
965 		data = radix_tree_lookup(&domain->revmap_tree, hwirq);
966 
967 	if (likely(data)) {
968 		desc = irq_data_to_desc(data);
969 		if (irq)
970 			*irq = data->irq;
971 	}
972 
973 	rcu_read_unlock();
974 	return desc;
975 }
976 EXPORT_SYMBOL_GPL(__irq_resolve_mapping);
977 
978 /**
979  * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
980  *
981  * Device Tree IRQ specifier translation function which works with one cell
982  * bindings where the cell value maps directly to the hwirq number.
983  */
984 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
985 			     const u32 *intspec, unsigned int intsize,
986 			     unsigned long *out_hwirq, unsigned int *out_type)
987 {
988 	if (WARN_ON(intsize < 1))
989 		return -EINVAL;
990 	*out_hwirq = intspec[0];
991 	*out_type = IRQ_TYPE_NONE;
992 	return 0;
993 }
994 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
995 
996 /**
997  * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
998  *
999  * Device Tree IRQ specifier translation function which works with two cell
1000  * bindings where the cell values map directly to the hwirq number
1001  * and linux irq flags.
1002  */
1003 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
1004 			const u32 *intspec, unsigned int intsize,
1005 			irq_hw_number_t *out_hwirq, unsigned int *out_type)
1006 {
1007 	struct irq_fwspec fwspec;
1008 
1009 	of_phandle_args_to_fwspec(ctrlr, intspec, intsize, &fwspec);
1010 	return irq_domain_translate_twocell(d, &fwspec, out_hwirq, out_type);
1011 }
1012 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
1013 
1014 /**
1015  * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
1016  *
1017  * Device Tree IRQ specifier translation function which works with either one
1018  * or two cell bindings where the cell values map directly to the hwirq number
1019  * and linux irq flags.
1020  *
1021  * Note: don't use this function unless your interrupt controller explicitly
1022  * supports both one and two cell bindings.  For the majority of controllers
1023  * the _onecell() or _twocell() variants above should be used.
1024  */
1025 int irq_domain_xlate_onetwocell(struct irq_domain *d,
1026 				struct device_node *ctrlr,
1027 				const u32 *intspec, unsigned int intsize,
1028 				unsigned long *out_hwirq, unsigned int *out_type)
1029 {
1030 	if (WARN_ON(intsize < 1))
1031 		return -EINVAL;
1032 	*out_hwirq = intspec[0];
1033 	if (intsize > 1)
1034 		*out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1035 	else
1036 		*out_type = IRQ_TYPE_NONE;
1037 	return 0;
1038 }
1039 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
1040 
1041 const struct irq_domain_ops irq_domain_simple_ops = {
1042 	.xlate = irq_domain_xlate_onetwocell,
1043 };
1044 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1045 
1046 /**
1047  * irq_domain_translate_onecell() - Generic translate for direct one cell
1048  * bindings
1049  */
1050 int irq_domain_translate_onecell(struct irq_domain *d,
1051 				 struct irq_fwspec *fwspec,
1052 				 unsigned long *out_hwirq,
1053 				 unsigned int *out_type)
1054 {
1055 	if (WARN_ON(fwspec->param_count < 1))
1056 		return -EINVAL;
1057 	*out_hwirq = fwspec->param[0];
1058 	*out_type = IRQ_TYPE_NONE;
1059 	return 0;
1060 }
1061 EXPORT_SYMBOL_GPL(irq_domain_translate_onecell);
1062 
1063 /**
1064  * irq_domain_translate_twocell() - Generic translate for direct two cell
1065  * bindings
1066  *
1067  * Device Tree IRQ specifier translation function which works with two cell
1068  * bindings where the cell values map directly to the hwirq number
1069  * and linux irq flags.
1070  */
1071 int irq_domain_translate_twocell(struct irq_domain *d,
1072 				 struct irq_fwspec *fwspec,
1073 				 unsigned long *out_hwirq,
1074 				 unsigned int *out_type)
1075 {
1076 	if (WARN_ON(fwspec->param_count < 2))
1077 		return -EINVAL;
1078 	*out_hwirq = fwspec->param[0];
1079 	*out_type = fwspec->param[1] & IRQ_TYPE_SENSE_MASK;
1080 	return 0;
1081 }
1082 EXPORT_SYMBOL_GPL(irq_domain_translate_twocell);
1083 
1084 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1085 			   int node, const struct irq_affinity_desc *affinity)
1086 {
1087 	unsigned int hint;
1088 
1089 	if (virq >= 0) {
1090 		virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1091 					 affinity);
1092 	} else {
1093 		hint = hwirq % nr_irqs;
1094 		if (hint == 0)
1095 			hint++;
1096 		virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1097 					 affinity);
1098 		if (virq <= 0 && hint > 1) {
1099 			virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1100 						 affinity);
1101 		}
1102 	}
1103 
1104 	return virq;
1105 }
1106 
1107 /**
1108  * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1109  * @irq_data:	The pointer to irq_data
1110  */
1111 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1112 {
1113 	irq_data->hwirq = 0;
1114 	irq_data->chip = &no_irq_chip;
1115 	irq_data->chip_data = NULL;
1116 }
1117 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1118 
1119 #ifdef	CONFIG_IRQ_DOMAIN_HIERARCHY
1120 /**
1121  * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1122  * @parent:	Parent irq domain to associate with the new domain
1123  * @flags:	Irq domain flags associated to the domain
1124  * @size:	Size of the domain. See below
1125  * @fwnode:	Optional fwnode of the interrupt controller
1126  * @ops:	Pointer to the interrupt domain callbacks
1127  * @host_data:	Controller private data pointer
1128  *
1129  * If @size is 0 a tree domain is created, otherwise a linear domain.
1130  *
1131  * If successful the parent is associated to the new domain and the
1132  * domain flags are set.
1133  * Returns pointer to IRQ domain, or NULL on failure.
1134  */
1135 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1136 					    unsigned int flags,
1137 					    unsigned int size,
1138 					    struct fwnode_handle *fwnode,
1139 					    const struct irq_domain_ops *ops,
1140 					    void *host_data)
1141 {
1142 	struct irq_domain *domain;
1143 
1144 	if (size)
1145 		domain = __irq_domain_create(fwnode, size, size, 0, ops, host_data);
1146 	else
1147 		domain = __irq_domain_create(fwnode, 0, ~0, 0, ops, host_data);
1148 
1149 	if (domain) {
1150 		if (parent)
1151 			domain->root = parent->root;
1152 		domain->parent = parent;
1153 		domain->flags |= flags;
1154 
1155 		__irq_domain_publish(domain);
1156 	}
1157 
1158 	return domain;
1159 }
1160 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1161 
1162 static void irq_domain_insert_irq(int virq)
1163 {
1164 	struct irq_data *data;
1165 
1166 	for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1167 		struct irq_domain *domain = data->domain;
1168 
1169 		domain->mapcount++;
1170 		irq_domain_set_mapping(domain, data->hwirq, data);
1171 	}
1172 
1173 	irq_clear_status_flags(virq, IRQ_NOREQUEST);
1174 }
1175 
1176 static void irq_domain_remove_irq(int virq)
1177 {
1178 	struct irq_data *data;
1179 
1180 	irq_set_status_flags(virq, IRQ_NOREQUEST);
1181 	irq_set_chip_and_handler(virq, NULL, NULL);
1182 	synchronize_irq(virq);
1183 	smp_mb();
1184 
1185 	for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1186 		struct irq_domain *domain = data->domain;
1187 		irq_hw_number_t hwirq = data->hwirq;
1188 
1189 		domain->mapcount--;
1190 		irq_domain_clear_mapping(domain, hwirq);
1191 	}
1192 }
1193 
1194 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1195 						   struct irq_data *child)
1196 {
1197 	struct irq_data *irq_data;
1198 
1199 	irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1200 				irq_data_get_node(child));
1201 	if (irq_data) {
1202 		child->parent_data = irq_data;
1203 		irq_data->irq = child->irq;
1204 		irq_data->common = child->common;
1205 		irq_data->domain = domain;
1206 	}
1207 
1208 	return irq_data;
1209 }
1210 
1211 static void __irq_domain_free_hierarchy(struct irq_data *irq_data)
1212 {
1213 	struct irq_data *tmp;
1214 
1215 	while (irq_data) {
1216 		tmp = irq_data;
1217 		irq_data = irq_data->parent_data;
1218 		kfree(tmp);
1219 	}
1220 }
1221 
1222 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1223 {
1224 	struct irq_data *irq_data, *tmp;
1225 	int i;
1226 
1227 	for (i = 0; i < nr_irqs; i++) {
1228 		irq_data = irq_get_irq_data(virq + i);
1229 		tmp = irq_data->parent_data;
1230 		irq_data->parent_data = NULL;
1231 		irq_data->domain = NULL;
1232 
1233 		__irq_domain_free_hierarchy(tmp);
1234 	}
1235 }
1236 
1237 /**
1238  * irq_domain_disconnect_hierarchy - Mark the first unused level of a hierarchy
1239  * @domain:	IRQ domain from which the hierarchy is to be disconnected
1240  * @virq:	IRQ number where the hierarchy is to be trimmed
1241  *
1242  * Marks the @virq level belonging to @domain as disconnected.
1243  * Returns -EINVAL if @virq doesn't have a valid irq_data pointing
1244  * to @domain.
1245  *
1246  * Its only use is to be able to trim levels of hierarchy that do not
1247  * have any real meaning for this interrupt, and that the driver marks
1248  * as such from its .alloc() callback.
1249  */
1250 int irq_domain_disconnect_hierarchy(struct irq_domain *domain,
1251 				    unsigned int virq)
1252 {
1253 	struct irq_data *irqd;
1254 
1255 	irqd = irq_domain_get_irq_data(domain, virq);
1256 	if (!irqd)
1257 		return -EINVAL;
1258 
1259 	irqd->chip = ERR_PTR(-ENOTCONN);
1260 	return 0;
1261 }
1262 EXPORT_SYMBOL_GPL(irq_domain_disconnect_hierarchy);
1263 
1264 static int irq_domain_trim_hierarchy(unsigned int virq)
1265 {
1266 	struct irq_data *tail, *irqd, *irq_data;
1267 
1268 	irq_data = irq_get_irq_data(virq);
1269 	tail = NULL;
1270 
1271 	/* The first entry must have a valid irqchip */
1272 	if (!irq_data->chip || IS_ERR(irq_data->chip))
1273 		return -EINVAL;
1274 
1275 	/*
1276 	 * Validate that the irq_data chain is sane in the presence of
1277 	 * a hierarchy trimming marker.
1278 	 */
1279 	for (irqd = irq_data->parent_data; irqd; irq_data = irqd, irqd = irqd->parent_data) {
1280 		/* Can't have a valid irqchip after a trim marker */
1281 		if (irqd->chip && tail)
1282 			return -EINVAL;
1283 
1284 		/* Can't have an empty irqchip before a trim marker */
1285 		if (!irqd->chip && !tail)
1286 			return -EINVAL;
1287 
1288 		if (IS_ERR(irqd->chip)) {
1289 			/* Only -ENOTCONN is a valid trim marker */
1290 			if (PTR_ERR(irqd->chip) != -ENOTCONN)
1291 				return -EINVAL;
1292 
1293 			tail = irq_data;
1294 		}
1295 	}
1296 
1297 	/* No trim marker, nothing to do */
1298 	if (!tail)
1299 		return 0;
1300 
1301 	pr_info("IRQ%d: trimming hierarchy from %s\n",
1302 		virq, tail->parent_data->domain->name);
1303 
1304 	/* Sever the inner part of the hierarchy...  */
1305 	irqd = tail;
1306 	tail = tail->parent_data;
1307 	irqd->parent_data = NULL;
1308 	__irq_domain_free_hierarchy(tail);
1309 
1310 	return 0;
1311 }
1312 
1313 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1314 				     unsigned int virq, unsigned int nr_irqs)
1315 {
1316 	struct irq_data *irq_data;
1317 	struct irq_domain *parent;
1318 	int i;
1319 
1320 	/* The outermost irq_data is embedded in struct irq_desc */
1321 	for (i = 0; i < nr_irqs; i++) {
1322 		irq_data = irq_get_irq_data(virq + i);
1323 		irq_data->domain = domain;
1324 
1325 		for (parent = domain->parent; parent; parent = parent->parent) {
1326 			irq_data = irq_domain_insert_irq_data(parent, irq_data);
1327 			if (!irq_data) {
1328 				irq_domain_free_irq_data(virq, i + 1);
1329 				return -ENOMEM;
1330 			}
1331 		}
1332 	}
1333 
1334 	return 0;
1335 }
1336 
1337 /**
1338  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1339  * @domain:	domain to match
1340  * @virq:	IRQ number to get irq_data
1341  */
1342 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1343 					 unsigned int virq)
1344 {
1345 	struct irq_data *irq_data;
1346 
1347 	for (irq_data = irq_get_irq_data(virq); irq_data;
1348 	     irq_data = irq_data->parent_data)
1349 		if (irq_data->domain == domain)
1350 			return irq_data;
1351 
1352 	return NULL;
1353 }
1354 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1355 
1356 /**
1357  * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1358  * @domain:	Interrupt domain to match
1359  * @virq:	IRQ number
1360  * @hwirq:	The hwirq number
1361  * @chip:	The associated interrupt chip
1362  * @chip_data:	The associated chip data
1363  */
1364 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1365 				  irq_hw_number_t hwirq,
1366 				  const struct irq_chip *chip,
1367 				  void *chip_data)
1368 {
1369 	struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1370 
1371 	if (!irq_data)
1372 		return -ENOENT;
1373 
1374 	irq_data->hwirq = hwirq;
1375 	irq_data->chip = (struct irq_chip *)(chip ? chip : &no_irq_chip);
1376 	irq_data->chip_data = chip_data;
1377 
1378 	return 0;
1379 }
1380 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1381 
1382 /**
1383  * irq_domain_set_info - Set the complete data for a @virq in @domain
1384  * @domain:		Interrupt domain to match
1385  * @virq:		IRQ number
1386  * @hwirq:		The hardware interrupt number
1387  * @chip:		The associated interrupt chip
1388  * @chip_data:		The associated interrupt chip data
1389  * @handler:		The interrupt flow handler
1390  * @handler_data:	The interrupt flow handler data
1391  * @handler_name:	The interrupt handler name
1392  */
1393 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1394 			 irq_hw_number_t hwirq, const struct irq_chip *chip,
1395 			 void *chip_data, irq_flow_handler_t handler,
1396 			 void *handler_data, const char *handler_name)
1397 {
1398 	irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1399 	__irq_set_handler(virq, handler, 0, handler_name);
1400 	irq_set_handler_data(virq, handler_data);
1401 }
1402 EXPORT_SYMBOL(irq_domain_set_info);
1403 
1404 /**
1405  * irq_domain_free_irqs_common - Clear irq_data and free the parent
1406  * @domain:	Interrupt domain to match
1407  * @virq:	IRQ number to start with
1408  * @nr_irqs:	The number of irqs to free
1409  */
1410 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1411 				 unsigned int nr_irqs)
1412 {
1413 	struct irq_data *irq_data;
1414 	int i;
1415 
1416 	for (i = 0; i < nr_irqs; i++) {
1417 		irq_data = irq_domain_get_irq_data(domain, virq + i);
1418 		if (irq_data)
1419 			irq_domain_reset_irq_data(irq_data);
1420 	}
1421 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1422 }
1423 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1424 
1425 /**
1426  * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1427  * @domain:	Interrupt domain to match
1428  * @virq:	IRQ number to start with
1429  * @nr_irqs:	The number of irqs to free
1430  */
1431 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1432 			      unsigned int nr_irqs)
1433 {
1434 	int i;
1435 
1436 	for (i = 0; i < nr_irqs; i++) {
1437 		irq_set_handler_data(virq + i, NULL);
1438 		irq_set_handler(virq + i, NULL);
1439 	}
1440 	irq_domain_free_irqs_common(domain, virq, nr_irqs);
1441 }
1442 
1443 static void irq_domain_free_irqs_hierarchy(struct irq_domain *domain,
1444 					   unsigned int irq_base,
1445 					   unsigned int nr_irqs)
1446 {
1447 	unsigned int i;
1448 
1449 	if (!domain->ops->free)
1450 		return;
1451 
1452 	for (i = 0; i < nr_irqs; i++) {
1453 		if (irq_domain_get_irq_data(domain, irq_base + i))
1454 			domain->ops->free(domain, irq_base + i, 1);
1455 	}
1456 }
1457 
1458 int irq_domain_alloc_irqs_hierarchy(struct irq_domain *domain,
1459 				    unsigned int irq_base,
1460 				    unsigned int nr_irqs, void *arg)
1461 {
1462 	if (!domain->ops->alloc) {
1463 		pr_debug("domain->ops->alloc() is NULL\n");
1464 		return -ENOSYS;
1465 	}
1466 
1467 	return domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1468 }
1469 
1470 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1471 					unsigned int nr_irqs, int node, void *arg,
1472 					bool realloc, const struct irq_affinity_desc *affinity)
1473 {
1474 	int i, ret, virq;
1475 
1476 	if (realloc && irq_base >= 0) {
1477 		virq = irq_base;
1478 	} else {
1479 		virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1480 					      affinity);
1481 		if (virq < 0) {
1482 			pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1483 				 irq_base, nr_irqs);
1484 			return virq;
1485 		}
1486 	}
1487 
1488 	if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1489 		pr_debug("cannot allocate memory for IRQ%d\n", virq);
1490 		ret = -ENOMEM;
1491 		goto out_free_desc;
1492 	}
1493 
1494 	ret = irq_domain_alloc_irqs_hierarchy(domain, virq, nr_irqs, arg);
1495 	if (ret < 0)
1496 		goto out_free_irq_data;
1497 
1498 	for (i = 0; i < nr_irqs; i++) {
1499 		ret = irq_domain_trim_hierarchy(virq + i);
1500 		if (ret)
1501 			goto out_free_irq_data;
1502 	}
1503 
1504 	for (i = 0; i < nr_irqs; i++)
1505 		irq_domain_insert_irq(virq + i);
1506 
1507 	return virq;
1508 
1509 out_free_irq_data:
1510 	irq_domain_free_irq_data(virq, nr_irqs);
1511 out_free_desc:
1512 	irq_free_descs(virq, nr_irqs);
1513 	return ret;
1514 }
1515 
1516 /**
1517  * __irq_domain_alloc_irqs - Allocate IRQs from domain
1518  * @domain:	domain to allocate from
1519  * @irq_base:	allocate specified IRQ number if irq_base >= 0
1520  * @nr_irqs:	number of IRQs to allocate
1521  * @node:	NUMA node id for memory allocation
1522  * @arg:	domain specific argument
1523  * @realloc:	IRQ descriptors have already been allocated if true
1524  * @affinity:	Optional irq affinity mask for multiqueue devices
1525  *
1526  * Allocate IRQ numbers and initialized all data structures to support
1527  * hierarchy IRQ domains.
1528  * Parameter @realloc is mainly to support legacy IRQs.
1529  * Returns error code or allocated IRQ number
1530  *
1531  * The whole process to setup an IRQ has been split into two steps.
1532  * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1533  * descriptor and required hardware resources. The second step,
1534  * irq_domain_activate_irq(), is to program the hardware with preallocated
1535  * resources. In this way, it's easier to rollback when failing to
1536  * allocate resources.
1537  */
1538 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1539 			    unsigned int nr_irqs, int node, void *arg,
1540 			    bool realloc, const struct irq_affinity_desc *affinity)
1541 {
1542 	int ret;
1543 
1544 	if (domain == NULL) {
1545 		domain = irq_default_domain;
1546 		if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1547 			return -EINVAL;
1548 	}
1549 
1550 	mutex_lock(&domain->root->mutex);
1551 	ret = irq_domain_alloc_irqs_locked(domain, irq_base, nr_irqs, node, arg,
1552 					   realloc, affinity);
1553 	mutex_unlock(&domain->root->mutex);
1554 
1555 	return ret;
1556 }
1557 EXPORT_SYMBOL_GPL(__irq_domain_alloc_irqs);
1558 
1559 /* The irq_data was moved, fix the revmap to refer to the new location */
1560 static void irq_domain_fix_revmap(struct irq_data *d)
1561 {
1562 	void __rcu **slot;
1563 
1564 	lockdep_assert_held(&d->domain->root->mutex);
1565 
1566 	if (irq_domain_is_nomap(d->domain))
1567 		return;
1568 
1569 	/* Fix up the revmap. */
1570 	if (d->hwirq < d->domain->revmap_size) {
1571 		/* Not using radix tree */
1572 		rcu_assign_pointer(d->domain->revmap[d->hwirq], d);
1573 	} else {
1574 		slot = radix_tree_lookup_slot(&d->domain->revmap_tree, d->hwirq);
1575 		if (slot)
1576 			radix_tree_replace_slot(&d->domain->revmap_tree, slot, d);
1577 	}
1578 }
1579 
1580 /**
1581  * irq_domain_push_irq() - Push a domain in to the top of a hierarchy.
1582  * @domain:	Domain to push.
1583  * @virq:	Irq to push the domain in to.
1584  * @arg:	Passed to the irq_domain_ops alloc() function.
1585  *
1586  * For an already existing irqdomain hierarchy, as might be obtained
1587  * via a call to pci_enable_msix(), add an additional domain to the
1588  * head of the processing chain.  Must be called before request_irq()
1589  * has been called.
1590  */
1591 int irq_domain_push_irq(struct irq_domain *domain, int virq, void *arg)
1592 {
1593 	struct irq_data *irq_data = irq_get_irq_data(virq);
1594 	struct irq_data *parent_irq_data;
1595 	struct irq_desc *desc;
1596 	int rv = 0;
1597 
1598 	/*
1599 	 * Check that no action has been set, which indicates the virq
1600 	 * is in a state where this function doesn't have to deal with
1601 	 * races between interrupt handling and maintaining the
1602 	 * hierarchy.  This will catch gross misuse.  Attempting to
1603 	 * make the check race free would require holding locks across
1604 	 * calls to struct irq_domain_ops->alloc(), which could lead
1605 	 * to deadlock, so we just do a simple check before starting.
1606 	 */
1607 	desc = irq_to_desc(virq);
1608 	if (!desc)
1609 		return -EINVAL;
1610 	if (WARN_ON(desc->action))
1611 		return -EBUSY;
1612 
1613 	if (domain == NULL)
1614 		return -EINVAL;
1615 
1616 	if (WARN_ON(!irq_domain_is_hierarchy(domain)))
1617 		return -EINVAL;
1618 
1619 	if (!irq_data)
1620 		return -EINVAL;
1621 
1622 	if (domain->parent != irq_data->domain)
1623 		return -EINVAL;
1624 
1625 	parent_irq_data = kzalloc_node(sizeof(*parent_irq_data), GFP_KERNEL,
1626 				       irq_data_get_node(irq_data));
1627 	if (!parent_irq_data)
1628 		return -ENOMEM;
1629 
1630 	mutex_lock(&domain->root->mutex);
1631 
1632 	/* Copy the original irq_data. */
1633 	*parent_irq_data = *irq_data;
1634 
1635 	/*
1636 	 * Overwrite the irq_data, which is embedded in struct irq_desc, with
1637 	 * values for this domain.
1638 	 */
1639 	irq_data->parent_data = parent_irq_data;
1640 	irq_data->domain = domain;
1641 	irq_data->mask = 0;
1642 	irq_data->hwirq = 0;
1643 	irq_data->chip = NULL;
1644 	irq_data->chip_data = NULL;
1645 
1646 	/* May (probably does) set hwirq, chip, etc. */
1647 	rv = irq_domain_alloc_irqs_hierarchy(domain, virq, 1, arg);
1648 	if (rv) {
1649 		/* Restore the original irq_data. */
1650 		*irq_data = *parent_irq_data;
1651 		kfree(parent_irq_data);
1652 		goto error;
1653 	}
1654 
1655 	irq_domain_fix_revmap(parent_irq_data);
1656 	irq_domain_set_mapping(domain, irq_data->hwirq, irq_data);
1657 error:
1658 	mutex_unlock(&domain->root->mutex);
1659 
1660 	return rv;
1661 }
1662 EXPORT_SYMBOL_GPL(irq_domain_push_irq);
1663 
1664 /**
1665  * irq_domain_pop_irq() - Remove a domain from the top of a hierarchy.
1666  * @domain:	Domain to remove.
1667  * @virq:	Irq to remove the domain from.
1668  *
1669  * Undo the effects of a call to irq_domain_push_irq().  Must be
1670  * called either before request_irq() or after free_irq().
1671  */
1672 int irq_domain_pop_irq(struct irq_domain *domain, int virq)
1673 {
1674 	struct irq_data *irq_data = irq_get_irq_data(virq);
1675 	struct irq_data *parent_irq_data;
1676 	struct irq_data *tmp_irq_data;
1677 	struct irq_desc *desc;
1678 
1679 	/*
1680 	 * Check that no action is set, which indicates the virq is in
1681 	 * a state where this function doesn't have to deal with races
1682 	 * between interrupt handling and maintaining the hierarchy.
1683 	 * This will catch gross misuse.  Attempting to make the check
1684 	 * race free would require holding locks across calls to
1685 	 * struct irq_domain_ops->free(), which could lead to
1686 	 * deadlock, so we just do a simple check before starting.
1687 	 */
1688 	desc = irq_to_desc(virq);
1689 	if (!desc)
1690 		return -EINVAL;
1691 	if (WARN_ON(desc->action))
1692 		return -EBUSY;
1693 
1694 	if (domain == NULL)
1695 		return -EINVAL;
1696 
1697 	if (!irq_data)
1698 		return -EINVAL;
1699 
1700 	tmp_irq_data = irq_domain_get_irq_data(domain, virq);
1701 
1702 	/* We can only "pop" if this domain is at the top of the list */
1703 	if (WARN_ON(irq_data != tmp_irq_data))
1704 		return -EINVAL;
1705 
1706 	if (WARN_ON(irq_data->domain != domain))
1707 		return -EINVAL;
1708 
1709 	parent_irq_data = irq_data->parent_data;
1710 	if (WARN_ON(!parent_irq_data))
1711 		return -EINVAL;
1712 
1713 	mutex_lock(&domain->root->mutex);
1714 
1715 	irq_data->parent_data = NULL;
1716 
1717 	irq_domain_clear_mapping(domain, irq_data->hwirq);
1718 	irq_domain_free_irqs_hierarchy(domain, virq, 1);
1719 
1720 	/* Restore the original irq_data. */
1721 	*irq_data = *parent_irq_data;
1722 
1723 	irq_domain_fix_revmap(irq_data);
1724 
1725 	mutex_unlock(&domain->root->mutex);
1726 
1727 	kfree(parent_irq_data);
1728 
1729 	return 0;
1730 }
1731 EXPORT_SYMBOL_GPL(irq_domain_pop_irq);
1732 
1733 /**
1734  * irq_domain_free_irqs - Free IRQ number and associated data structures
1735  * @virq:	base IRQ number
1736  * @nr_irqs:	number of IRQs to free
1737  */
1738 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1739 {
1740 	struct irq_data *data = irq_get_irq_data(virq);
1741 	struct irq_domain *domain;
1742 	int i;
1743 
1744 	if (WARN(!data || !data->domain || !data->domain->ops->free,
1745 		 "NULL pointer, cannot free irq\n"))
1746 		return;
1747 
1748 	domain = data->domain;
1749 
1750 	mutex_lock(&domain->root->mutex);
1751 	for (i = 0; i < nr_irqs; i++)
1752 		irq_domain_remove_irq(virq + i);
1753 	irq_domain_free_irqs_hierarchy(domain, virq, nr_irqs);
1754 	mutex_unlock(&domain->root->mutex);
1755 
1756 	irq_domain_free_irq_data(virq, nr_irqs);
1757 	irq_free_descs(virq, nr_irqs);
1758 }
1759 
1760 /**
1761  * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1762  * @domain:	Domain below which interrupts must be allocated
1763  * @irq_base:	Base IRQ number
1764  * @nr_irqs:	Number of IRQs to allocate
1765  * @arg:	Allocation data (arch/domain specific)
1766  */
1767 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1768 				 unsigned int irq_base, unsigned int nr_irqs,
1769 				 void *arg)
1770 {
1771 	if (!domain->parent)
1772 		return -ENOSYS;
1773 
1774 	return irq_domain_alloc_irqs_hierarchy(domain->parent, irq_base,
1775 					       nr_irqs, arg);
1776 }
1777 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1778 
1779 /**
1780  * irq_domain_free_irqs_parent - Free interrupts from parent domain
1781  * @domain:	Domain below which interrupts must be freed
1782  * @irq_base:	Base IRQ number
1783  * @nr_irqs:	Number of IRQs to free
1784  */
1785 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1786 				 unsigned int irq_base, unsigned int nr_irqs)
1787 {
1788 	if (!domain->parent)
1789 		return;
1790 
1791 	irq_domain_free_irqs_hierarchy(domain->parent, irq_base, nr_irqs);
1792 }
1793 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1794 
1795 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1796 {
1797 	if (irq_data && irq_data->domain) {
1798 		struct irq_domain *domain = irq_data->domain;
1799 
1800 		if (domain->ops->deactivate)
1801 			domain->ops->deactivate(domain, irq_data);
1802 		if (irq_data->parent_data)
1803 			__irq_domain_deactivate_irq(irq_data->parent_data);
1804 	}
1805 }
1806 
1807 static int __irq_domain_activate_irq(struct irq_data *irqd, bool reserve)
1808 {
1809 	int ret = 0;
1810 
1811 	if (irqd && irqd->domain) {
1812 		struct irq_domain *domain = irqd->domain;
1813 
1814 		if (irqd->parent_data)
1815 			ret = __irq_domain_activate_irq(irqd->parent_data,
1816 							reserve);
1817 		if (!ret && domain->ops->activate) {
1818 			ret = domain->ops->activate(domain, irqd, reserve);
1819 			/* Rollback in case of error */
1820 			if (ret && irqd->parent_data)
1821 				__irq_domain_deactivate_irq(irqd->parent_data);
1822 		}
1823 	}
1824 	return ret;
1825 }
1826 
1827 /**
1828  * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1829  *			     interrupt
1830  * @irq_data:	Outermost irq_data associated with interrupt
1831  * @reserve:	If set only reserve an interrupt vector instead of assigning one
1832  *
1833  * This is the second step to call domain_ops->activate to program interrupt
1834  * controllers, so the interrupt could actually get delivered.
1835  */
1836 int irq_domain_activate_irq(struct irq_data *irq_data, bool reserve)
1837 {
1838 	int ret = 0;
1839 
1840 	if (!irqd_is_activated(irq_data))
1841 		ret = __irq_domain_activate_irq(irq_data, reserve);
1842 	if (!ret)
1843 		irqd_set_activated(irq_data);
1844 	return ret;
1845 }
1846 
1847 /**
1848  * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1849  *			       deactivate interrupt
1850  * @irq_data: outermost irq_data associated with interrupt
1851  *
1852  * It calls domain_ops->deactivate to program interrupt controllers to disable
1853  * interrupt delivery.
1854  */
1855 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1856 {
1857 	if (irqd_is_activated(irq_data)) {
1858 		__irq_domain_deactivate_irq(irq_data);
1859 		irqd_clr_activated(irq_data);
1860 	}
1861 }
1862 
1863 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1864 {
1865 	/* Hierarchy irq_domains must implement callback alloc() */
1866 	if (domain->ops->alloc)
1867 		domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1868 }
1869 #else	/* CONFIG_IRQ_DOMAIN_HIERARCHY */
1870 /**
1871  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1872  * @domain:	domain to match
1873  * @virq:	IRQ number to get irq_data
1874  */
1875 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1876 					 unsigned int virq)
1877 {
1878 	struct irq_data *irq_data = irq_get_irq_data(virq);
1879 
1880 	return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1881 }
1882 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1883 
1884 /**
1885  * irq_domain_set_info - Set the complete data for a @virq in @domain
1886  * @domain:		Interrupt domain to match
1887  * @virq:		IRQ number
1888  * @hwirq:		The hardware interrupt number
1889  * @chip:		The associated interrupt chip
1890  * @chip_data:		The associated interrupt chip data
1891  * @handler:		The interrupt flow handler
1892  * @handler_data:	The interrupt flow handler data
1893  * @handler_name:	The interrupt handler name
1894  */
1895 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1896 			 irq_hw_number_t hwirq, const struct irq_chip *chip,
1897 			 void *chip_data, irq_flow_handler_t handler,
1898 			 void *handler_data, const char *handler_name)
1899 {
1900 	irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1901 	irq_set_chip_data(virq, chip_data);
1902 	irq_set_handler_data(virq, handler_data);
1903 }
1904 
1905 static int irq_domain_alloc_irqs_locked(struct irq_domain *domain, int irq_base,
1906 					unsigned int nr_irqs, int node, void *arg,
1907 					bool realloc, const struct irq_affinity_desc *affinity)
1908 {
1909 	return -EINVAL;
1910 }
1911 
1912 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1913 {
1914 }
1915 #endif	/* CONFIG_IRQ_DOMAIN_HIERARCHY */
1916 
1917 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1918 static struct dentry *domain_dir;
1919 
1920 static void
1921 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1922 {
1923 	seq_printf(m, "%*sname:   %s\n", ind, "", d->name);
1924 	seq_printf(m, "%*ssize:   %u\n", ind + 1, "", d->revmap_size);
1925 	seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1926 	seq_printf(m, "%*sflags:  0x%08x\n", ind +1 , "", d->flags);
1927 	if (d->ops && d->ops->debug_show)
1928 		d->ops->debug_show(m, d, NULL, ind + 1);
1929 #ifdef	CONFIG_IRQ_DOMAIN_HIERARCHY
1930 	if (!d->parent)
1931 		return;
1932 	seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1933 	irq_domain_debug_show_one(m, d->parent, ind + 4);
1934 #endif
1935 }
1936 
1937 static int irq_domain_debug_show(struct seq_file *m, void *p)
1938 {
1939 	struct irq_domain *d = m->private;
1940 
1941 	/* Default domain? Might be NULL */
1942 	if (!d) {
1943 		if (!irq_default_domain)
1944 			return 0;
1945 		d = irq_default_domain;
1946 	}
1947 	irq_domain_debug_show_one(m, d, 0);
1948 	return 0;
1949 }
1950 DEFINE_SHOW_ATTRIBUTE(irq_domain_debug);
1951 
1952 static void debugfs_add_domain_dir(struct irq_domain *d)
1953 {
1954 	if (!d->name || !domain_dir)
1955 		return;
1956 	debugfs_create_file(d->name, 0444, domain_dir, d,
1957 			    &irq_domain_debug_fops);
1958 }
1959 
1960 static void debugfs_remove_domain_dir(struct irq_domain *d)
1961 {
1962 	debugfs_lookup_and_remove(d->name, domain_dir);
1963 }
1964 
1965 void __init irq_domain_debugfs_init(struct dentry *root)
1966 {
1967 	struct irq_domain *d;
1968 
1969 	domain_dir = debugfs_create_dir("domains", root);
1970 
1971 	debugfs_create_file("default", 0444, domain_dir, NULL,
1972 			    &irq_domain_debug_fops);
1973 	mutex_lock(&irq_domain_mutex);
1974 	list_for_each_entry(d, &irq_domain_list, link)
1975 		debugfs_add_domain_dir(d);
1976 	mutex_unlock(&irq_domain_mutex);
1977 }
1978 #endif
1979