xref: /linux/kernel/irq/irqdomain.c (revision 087cdfb662ae50e3826e7cd2e54b6519d07b60f0)
1 #define pr_fmt(fmt)  "irq: " fmt
2 
3 #include <linux/debugfs.h>
4 #include <linux/hardirq.h>
5 #include <linux/interrupt.h>
6 #include <linux/irq.h>
7 #include <linux/irqdesc.h>
8 #include <linux/irqdomain.h>
9 #include <linux/module.h>
10 #include <linux/mutex.h>
11 #include <linux/of.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/topology.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/smp.h>
18 #include <linux/fs.h>
19 
20 static LIST_HEAD(irq_domain_list);
21 static DEFINE_MUTEX(irq_domain_mutex);
22 
23 static DEFINE_MUTEX(revmap_trees_mutex);
24 static struct irq_domain *irq_default_domain;
25 
26 static void irq_domain_check_hierarchy(struct irq_domain *domain);
27 
28 struct irqchip_fwid {
29 	struct fwnode_handle	fwnode;
30 	unsigned int		type;
31 	char			*name;
32 	void *data;
33 };
34 
35 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
36 static void debugfs_add_domain_dir(struct irq_domain *d);
37 static void debugfs_remove_domain_dir(struct irq_domain *d);
38 #else
39 static inline void debugfs_add_domain_dir(struct irq_domain *d) { }
40 static inline void debugfs_remove_domain_dir(struct irq_domain *d) { }
41 #endif
42 
43 /**
44  * irq_domain_alloc_fwnode - Allocate a fwnode_handle suitable for
45  *                           identifying an irq domain
46  * @type:	Type of irqchip_fwnode. See linux/irqdomain.h
47  * @name:	Optional user provided domain name
48  * @id:		Optional user provided id if name != NULL
49  * @data:	Optional user-provided data
50  *
51  * Allocate a struct irqchip_fwid, and return a poiner to the embedded
52  * fwnode_handle (or NULL on failure).
53  *
54  * Note: The types IRQCHIP_FWNODE_NAMED and IRQCHIP_FWNODE_NAMED_ID are
55  * solely to transport name information to irqdomain creation code. The
56  * node is not stored. For other types the pointer is kept in the irq
57  * domain struct.
58  */
59 struct fwnode_handle *__irq_domain_alloc_fwnode(unsigned int type, int id,
60 						const char *name, void *data)
61 {
62 	struct irqchip_fwid *fwid;
63 	char *n;
64 
65 	fwid = kzalloc(sizeof(*fwid), GFP_KERNEL);
66 
67 	switch (type) {
68 	case IRQCHIP_FWNODE_NAMED:
69 		n = kasprintf(GFP_KERNEL, "%s", name);
70 		break;
71 	case IRQCHIP_FWNODE_NAMED_ID:
72 		n = kasprintf(GFP_KERNEL, "%s-%d", name, id);
73 		break;
74 	default:
75 		n = kasprintf(GFP_KERNEL, "irqchip@%p", data);
76 		break;
77 	}
78 
79 	if (!fwid || !n) {
80 		kfree(fwid);
81 		kfree(n);
82 		return NULL;
83 	}
84 
85 	fwid->type = type;
86 	fwid->name = n;
87 	fwid->data = data;
88 	fwid->fwnode.type = FWNODE_IRQCHIP;
89 	return &fwid->fwnode;
90 }
91 EXPORT_SYMBOL_GPL(__irq_domain_alloc_fwnode);
92 
93 /**
94  * irq_domain_free_fwnode - Free a non-OF-backed fwnode_handle
95  *
96  * Free a fwnode_handle allocated with irq_domain_alloc_fwnode.
97  */
98 void irq_domain_free_fwnode(struct fwnode_handle *fwnode)
99 {
100 	struct irqchip_fwid *fwid;
101 
102 	if (WARN_ON(!is_fwnode_irqchip(fwnode)))
103 		return;
104 
105 	fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
106 	kfree(fwid->name);
107 	kfree(fwid);
108 }
109 EXPORT_SYMBOL_GPL(irq_domain_free_fwnode);
110 
111 /**
112  * __irq_domain_add() - Allocate a new irq_domain data structure
113  * @fwnode: firmware node for the interrupt controller
114  * @size: Size of linear map; 0 for radix mapping only
115  * @hwirq_max: Maximum number of interrupts supported by controller
116  * @direct_max: Maximum value of direct maps; Use ~0 for no limit; 0 for no
117  *              direct mapping
118  * @ops: domain callbacks
119  * @host_data: Controller private data pointer
120  *
121  * Allocates and initialize and irq_domain structure.
122  * Returns pointer to IRQ domain, or NULL on failure.
123  */
124 struct irq_domain *__irq_domain_add(struct fwnode_handle *fwnode, int size,
125 				    irq_hw_number_t hwirq_max, int direct_max,
126 				    const struct irq_domain_ops *ops,
127 				    void *host_data)
128 {
129 	struct device_node *of_node = to_of_node(fwnode);
130 	struct irqchip_fwid *fwid;
131 	struct irq_domain *domain;
132 
133 	static atomic_t unknown_domains;
134 
135 	domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
136 			      GFP_KERNEL, of_node_to_nid(of_node));
137 	if (WARN_ON(!domain))
138 		return NULL;
139 
140 	if (fwnode && is_fwnode_irqchip(fwnode)) {
141 		fwid = container_of(fwnode, struct irqchip_fwid, fwnode);
142 
143 		switch (fwid->type) {
144 		case IRQCHIP_FWNODE_NAMED:
145 		case IRQCHIP_FWNODE_NAMED_ID:
146 			domain->name = kstrdup(fwid->name, GFP_KERNEL);
147 			if (!domain->name) {
148 				kfree(domain);
149 				return NULL;
150 			}
151 			domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
152 			break;
153 		default:
154 			domain->fwnode = fwnode;
155 			domain->name = fwid->name;
156 			break;
157 		}
158 	} else if (of_node) {
159 		char *name;
160 
161 		/*
162 		 * DT paths contain '/', which debugfs is legitimately
163 		 * unhappy about. Replace them with ':', which does
164 		 * the trick and is not as offensive as '\'...
165 		 */
166 		name = kstrdup(of_node_full_name(of_node), GFP_KERNEL);
167 		if (!name) {
168 			kfree(domain);
169 			return NULL;
170 		}
171 
172 		strreplace(name, '/', ':');
173 
174 		domain->name = name;
175 		domain->fwnode = fwnode;
176 		domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
177 	}
178 
179 	if (!domain->name) {
180 		if (fwnode) {
181 			pr_err("Invalid fwnode type (%d) for irqdomain\n",
182 			       fwnode->type);
183 		}
184 		domain->name = kasprintf(GFP_KERNEL, "unknown-%d",
185 					 atomic_inc_return(&unknown_domains));
186 		if (!domain->name) {
187 			kfree(domain);
188 			return NULL;
189 		}
190 		domain->flags |= IRQ_DOMAIN_NAME_ALLOCATED;
191 	}
192 
193 	of_node_get(of_node);
194 
195 	/* Fill structure */
196 	INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
197 	domain->ops = ops;
198 	domain->host_data = host_data;
199 	domain->hwirq_max = hwirq_max;
200 	domain->revmap_size = size;
201 	domain->revmap_direct_max_irq = direct_max;
202 	irq_domain_check_hierarchy(domain);
203 
204 	mutex_lock(&irq_domain_mutex);
205 	debugfs_add_domain_dir(domain);
206 	list_add(&domain->link, &irq_domain_list);
207 	mutex_unlock(&irq_domain_mutex);
208 
209 	pr_debug("Added domain %s\n", domain->name);
210 	return domain;
211 }
212 EXPORT_SYMBOL_GPL(__irq_domain_add);
213 
214 /**
215  * irq_domain_remove() - Remove an irq domain.
216  * @domain: domain to remove
217  *
218  * This routine is used to remove an irq domain. The caller must ensure
219  * that all mappings within the domain have been disposed of prior to
220  * use, depending on the revmap type.
221  */
222 void irq_domain_remove(struct irq_domain *domain)
223 {
224 	mutex_lock(&irq_domain_mutex);
225 	debugfs_remove_domain_dir(domain);
226 
227 	WARN_ON(!radix_tree_empty(&domain->revmap_tree));
228 
229 	list_del(&domain->link);
230 
231 	/*
232 	 * If the going away domain is the default one, reset it.
233 	 */
234 	if (unlikely(irq_default_domain == domain))
235 		irq_set_default_host(NULL);
236 
237 	mutex_unlock(&irq_domain_mutex);
238 
239 	pr_debug("Removed domain %s\n", domain->name);
240 
241 	of_node_put(irq_domain_get_of_node(domain));
242 	if (domain->flags & IRQ_DOMAIN_NAME_ALLOCATED)
243 		kfree(domain->name);
244 	kfree(domain);
245 }
246 EXPORT_SYMBOL_GPL(irq_domain_remove);
247 
248 /**
249  * irq_domain_add_simple() - Register an irq_domain and optionally map a range of irqs
250  * @of_node: pointer to interrupt controller's device tree node.
251  * @size: total number of irqs in mapping
252  * @first_irq: first number of irq block assigned to the domain,
253  *	pass zero to assign irqs on-the-fly. If first_irq is non-zero, then
254  *	pre-map all of the irqs in the domain to virqs starting at first_irq.
255  * @ops: domain callbacks
256  * @host_data: Controller private data pointer
257  *
258  * Allocates an irq_domain, and optionally if first_irq is positive then also
259  * allocate irq_descs and map all of the hwirqs to virqs starting at first_irq.
260  *
261  * This is intended to implement the expected behaviour for most
262  * interrupt controllers. If device tree is used, then first_irq will be 0 and
263  * irqs get mapped dynamically on the fly. However, if the controller requires
264  * static virq assignments (non-DT boot) then it will set that up correctly.
265  */
266 struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
267 					 unsigned int size,
268 					 unsigned int first_irq,
269 					 const struct irq_domain_ops *ops,
270 					 void *host_data)
271 {
272 	struct irq_domain *domain;
273 
274 	domain = __irq_domain_add(of_node_to_fwnode(of_node), size, size, 0, ops, host_data);
275 	if (!domain)
276 		return NULL;
277 
278 	if (first_irq > 0) {
279 		if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
280 			/* attempt to allocated irq_descs */
281 			int rc = irq_alloc_descs(first_irq, first_irq, size,
282 						 of_node_to_nid(of_node));
283 			if (rc < 0)
284 				pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
285 					first_irq);
286 		}
287 		irq_domain_associate_many(domain, first_irq, 0, size);
288 	}
289 
290 	return domain;
291 }
292 EXPORT_SYMBOL_GPL(irq_domain_add_simple);
293 
294 /**
295  * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
296  * @of_node: pointer to interrupt controller's device tree node.
297  * @size: total number of irqs in legacy mapping
298  * @first_irq: first number of irq block assigned to the domain
299  * @first_hwirq: first hwirq number to use for the translation. Should normally
300  *               be '0', but a positive integer can be used if the effective
301  *               hwirqs numbering does not begin at zero.
302  * @ops: map/unmap domain callbacks
303  * @host_data: Controller private data pointer
304  *
305  * Note: the map() callback will be called before this function returns
306  * for all legacy interrupts except 0 (which is always the invalid irq for
307  * a legacy controller).
308  */
309 struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
310 					 unsigned int size,
311 					 unsigned int first_irq,
312 					 irq_hw_number_t first_hwirq,
313 					 const struct irq_domain_ops *ops,
314 					 void *host_data)
315 {
316 	struct irq_domain *domain;
317 
318 	domain = __irq_domain_add(of_node_to_fwnode(of_node), first_hwirq + size,
319 				  first_hwirq + size, 0, ops, host_data);
320 	if (domain)
321 		irq_domain_associate_many(domain, first_irq, first_hwirq, size);
322 
323 	return domain;
324 }
325 EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
326 
327 /**
328  * irq_find_matching_fwspec() - Locates a domain for a given fwspec
329  * @fwspec: FW specifier for an interrupt
330  * @bus_token: domain-specific data
331  */
332 struct irq_domain *irq_find_matching_fwspec(struct irq_fwspec *fwspec,
333 					    enum irq_domain_bus_token bus_token)
334 {
335 	struct irq_domain *h, *found = NULL;
336 	struct fwnode_handle *fwnode = fwspec->fwnode;
337 	int rc;
338 
339 	/* We might want to match the legacy controller last since
340 	 * it might potentially be set to match all interrupts in
341 	 * the absence of a device node. This isn't a problem so far
342 	 * yet though...
343 	 *
344 	 * bus_token == DOMAIN_BUS_ANY matches any domain, any other
345 	 * values must generate an exact match for the domain to be
346 	 * selected.
347 	 */
348 	mutex_lock(&irq_domain_mutex);
349 	list_for_each_entry(h, &irq_domain_list, link) {
350 		if (h->ops->select && fwspec->param_count)
351 			rc = h->ops->select(h, fwspec, bus_token);
352 		else if (h->ops->match)
353 			rc = h->ops->match(h, to_of_node(fwnode), bus_token);
354 		else
355 			rc = ((fwnode != NULL) && (h->fwnode == fwnode) &&
356 			      ((bus_token == DOMAIN_BUS_ANY) ||
357 			       (h->bus_token == bus_token)));
358 
359 		if (rc) {
360 			found = h;
361 			break;
362 		}
363 	}
364 	mutex_unlock(&irq_domain_mutex);
365 	return found;
366 }
367 EXPORT_SYMBOL_GPL(irq_find_matching_fwspec);
368 
369 /**
370  * irq_domain_check_msi_remap - Check whether all MSI irq domains implement
371  * IRQ remapping
372  *
373  * Return: false if any MSI irq domain does not support IRQ remapping,
374  * true otherwise (including if there is no MSI irq domain)
375  */
376 bool irq_domain_check_msi_remap(void)
377 {
378 	struct irq_domain *h;
379 	bool ret = true;
380 
381 	mutex_lock(&irq_domain_mutex);
382 	list_for_each_entry(h, &irq_domain_list, link) {
383 		if (irq_domain_is_msi(h) &&
384 		    !irq_domain_hierarchical_is_msi_remap(h)) {
385 			ret = false;
386 			break;
387 		}
388 	}
389 	mutex_unlock(&irq_domain_mutex);
390 	return ret;
391 }
392 EXPORT_SYMBOL_GPL(irq_domain_check_msi_remap);
393 
394 /**
395  * irq_set_default_host() - Set a "default" irq domain
396  * @domain: default domain pointer
397  *
398  * For convenience, it's possible to set a "default" domain that will be used
399  * whenever NULL is passed to irq_create_mapping(). It makes life easier for
400  * platforms that want to manipulate a few hard coded interrupt numbers that
401  * aren't properly represented in the device-tree.
402  */
403 void irq_set_default_host(struct irq_domain *domain)
404 {
405 	pr_debug("Default domain set to @0x%p\n", domain);
406 
407 	irq_default_domain = domain;
408 }
409 EXPORT_SYMBOL_GPL(irq_set_default_host);
410 
411 void irq_domain_disassociate(struct irq_domain *domain, unsigned int irq)
412 {
413 	struct irq_data *irq_data = irq_get_irq_data(irq);
414 	irq_hw_number_t hwirq;
415 
416 	if (WARN(!irq_data || irq_data->domain != domain,
417 		 "virq%i doesn't exist; cannot disassociate\n", irq))
418 		return;
419 
420 	hwirq = irq_data->hwirq;
421 	irq_set_status_flags(irq, IRQ_NOREQUEST);
422 
423 	/* remove chip and handler */
424 	irq_set_chip_and_handler(irq, NULL, NULL);
425 
426 	/* Make sure it's completed */
427 	synchronize_irq(irq);
428 
429 	/* Tell the PIC about it */
430 	if (domain->ops->unmap)
431 		domain->ops->unmap(domain, irq);
432 	smp_mb();
433 
434 	irq_data->domain = NULL;
435 	irq_data->hwirq = 0;
436 	domain->mapcount--;
437 
438 	/* Clear reverse map for this hwirq */
439 	if (hwirq < domain->revmap_size) {
440 		domain->linear_revmap[hwirq] = 0;
441 	} else {
442 		mutex_lock(&revmap_trees_mutex);
443 		radix_tree_delete(&domain->revmap_tree, hwirq);
444 		mutex_unlock(&revmap_trees_mutex);
445 	}
446 }
447 
448 int irq_domain_associate(struct irq_domain *domain, unsigned int virq,
449 			 irq_hw_number_t hwirq)
450 {
451 	struct irq_data *irq_data = irq_get_irq_data(virq);
452 	int ret;
453 
454 	if (WARN(hwirq >= domain->hwirq_max,
455 		 "error: hwirq 0x%x is too large for %s\n", (int)hwirq, domain->name))
456 		return -EINVAL;
457 	if (WARN(!irq_data, "error: virq%i is not allocated", virq))
458 		return -EINVAL;
459 	if (WARN(irq_data->domain, "error: virq%i is already associated", virq))
460 		return -EINVAL;
461 
462 	mutex_lock(&irq_domain_mutex);
463 	irq_data->hwirq = hwirq;
464 	irq_data->domain = domain;
465 	if (domain->ops->map) {
466 		ret = domain->ops->map(domain, virq, hwirq);
467 		if (ret != 0) {
468 			/*
469 			 * If map() returns -EPERM, this interrupt is protected
470 			 * by the firmware or some other service and shall not
471 			 * be mapped. Don't bother telling the user about it.
472 			 */
473 			if (ret != -EPERM) {
474 				pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
475 				       domain->name, hwirq, virq, ret);
476 			}
477 			irq_data->domain = NULL;
478 			irq_data->hwirq = 0;
479 			mutex_unlock(&irq_domain_mutex);
480 			return ret;
481 		}
482 
483 		/* If not already assigned, give the domain the chip's name */
484 		if (!domain->name && irq_data->chip)
485 			domain->name = irq_data->chip->name;
486 	}
487 
488 	domain->mapcount++;
489 	if (hwirq < domain->revmap_size) {
490 		domain->linear_revmap[hwirq] = virq;
491 	} else {
492 		mutex_lock(&revmap_trees_mutex);
493 		radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
494 		mutex_unlock(&revmap_trees_mutex);
495 	}
496 	mutex_unlock(&irq_domain_mutex);
497 
498 	irq_clear_status_flags(virq, IRQ_NOREQUEST);
499 
500 	return 0;
501 }
502 EXPORT_SYMBOL_GPL(irq_domain_associate);
503 
504 void irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
505 			       irq_hw_number_t hwirq_base, int count)
506 {
507 	struct device_node *of_node;
508 	int i;
509 
510 	of_node = irq_domain_get_of_node(domain);
511 	pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
512 		of_node_full_name(of_node), irq_base, (int)hwirq_base, count);
513 
514 	for (i = 0; i < count; i++) {
515 		irq_domain_associate(domain, irq_base + i, hwirq_base + i);
516 	}
517 }
518 EXPORT_SYMBOL_GPL(irq_domain_associate_many);
519 
520 /**
521  * irq_create_direct_mapping() - Allocate an irq for direct mapping
522  * @domain: domain to allocate the irq for or NULL for default domain
523  *
524  * This routine is used for irq controllers which can choose the hardware
525  * interrupt numbers they generate. In such a case it's simplest to use
526  * the linux irq as the hardware interrupt number. It still uses the linear
527  * or radix tree to store the mapping, but the irq controller can optimize
528  * the revmap path by using the hwirq directly.
529  */
530 unsigned int irq_create_direct_mapping(struct irq_domain *domain)
531 {
532 	struct device_node *of_node;
533 	unsigned int virq;
534 
535 	if (domain == NULL)
536 		domain = irq_default_domain;
537 
538 	of_node = irq_domain_get_of_node(domain);
539 	virq = irq_alloc_desc_from(1, of_node_to_nid(of_node));
540 	if (!virq) {
541 		pr_debug("create_direct virq allocation failed\n");
542 		return 0;
543 	}
544 	if (virq >= domain->revmap_direct_max_irq) {
545 		pr_err("ERROR: no free irqs available below %i maximum\n",
546 			domain->revmap_direct_max_irq);
547 		irq_free_desc(virq);
548 		return 0;
549 	}
550 	pr_debug("create_direct obtained virq %d\n", virq);
551 
552 	if (irq_domain_associate(domain, virq, virq)) {
553 		irq_free_desc(virq);
554 		return 0;
555 	}
556 
557 	return virq;
558 }
559 EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
560 
561 /**
562  * irq_create_mapping() - Map a hardware interrupt into linux irq space
563  * @domain: domain owning this hardware interrupt or NULL for default domain
564  * @hwirq: hardware irq number in that domain space
565  *
566  * Only one mapping per hardware interrupt is permitted. Returns a linux
567  * irq number.
568  * If the sense/trigger is to be specified, set_irq_type() should be called
569  * on the number returned from that call.
570  */
571 unsigned int irq_create_mapping(struct irq_domain *domain,
572 				irq_hw_number_t hwirq)
573 {
574 	struct device_node *of_node;
575 	int virq;
576 
577 	pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
578 
579 	/* Look for default domain if nececssary */
580 	if (domain == NULL)
581 		domain = irq_default_domain;
582 	if (domain == NULL) {
583 		WARN(1, "%s(, %lx) called with NULL domain\n", __func__, hwirq);
584 		return 0;
585 	}
586 	pr_debug("-> using domain @%p\n", domain);
587 
588 	of_node = irq_domain_get_of_node(domain);
589 
590 	/* Check if mapping already exists */
591 	virq = irq_find_mapping(domain, hwirq);
592 	if (virq) {
593 		pr_debug("-> existing mapping on virq %d\n", virq);
594 		return virq;
595 	}
596 
597 	/* Allocate a virtual interrupt number */
598 	virq = irq_domain_alloc_descs(-1, 1, hwirq, of_node_to_nid(of_node), NULL);
599 	if (virq <= 0) {
600 		pr_debug("-> virq allocation failed\n");
601 		return 0;
602 	}
603 
604 	if (irq_domain_associate(domain, virq, hwirq)) {
605 		irq_free_desc(virq);
606 		return 0;
607 	}
608 
609 	pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
610 		hwirq, of_node_full_name(of_node), virq);
611 
612 	return virq;
613 }
614 EXPORT_SYMBOL_GPL(irq_create_mapping);
615 
616 /**
617  * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
618  * @domain: domain owning the interrupt range
619  * @irq_base: beginning of linux IRQ range
620  * @hwirq_base: beginning of hardware IRQ range
621  * @count: Number of interrupts to map
622  *
623  * This routine is used for allocating and mapping a range of hardware
624  * irqs to linux irqs where the linux irq numbers are at pre-defined
625  * locations. For use by controllers that already have static mappings
626  * to insert in to the domain.
627  *
628  * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
629  * domain insertion.
630  *
631  * 0 is returned upon success, while any failure to establish a static
632  * mapping is treated as an error.
633  */
634 int irq_create_strict_mappings(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 ret;
639 
640 	of_node = irq_domain_get_of_node(domain);
641 	ret = irq_alloc_descs(irq_base, irq_base, count,
642 			      of_node_to_nid(of_node));
643 	if (unlikely(ret < 0))
644 		return ret;
645 
646 	irq_domain_associate_many(domain, irq_base, hwirq_base, count);
647 	return 0;
648 }
649 EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
650 
651 static int irq_domain_translate(struct irq_domain *d,
652 				struct irq_fwspec *fwspec,
653 				irq_hw_number_t *hwirq, unsigned int *type)
654 {
655 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
656 	if (d->ops->translate)
657 		return d->ops->translate(d, fwspec, hwirq, type);
658 #endif
659 	if (d->ops->xlate)
660 		return d->ops->xlate(d, to_of_node(fwspec->fwnode),
661 				     fwspec->param, fwspec->param_count,
662 				     hwirq, type);
663 
664 	/* If domain has no translation, then we assume interrupt line */
665 	*hwirq = fwspec->param[0];
666 	return 0;
667 }
668 
669 static void of_phandle_args_to_fwspec(struct of_phandle_args *irq_data,
670 				      struct irq_fwspec *fwspec)
671 {
672 	int i;
673 
674 	fwspec->fwnode = irq_data->np ? &irq_data->np->fwnode : NULL;
675 	fwspec->param_count = irq_data->args_count;
676 
677 	for (i = 0; i < irq_data->args_count; i++)
678 		fwspec->param[i] = irq_data->args[i];
679 }
680 
681 unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
682 {
683 	struct irq_domain *domain;
684 	struct irq_data *irq_data;
685 	irq_hw_number_t hwirq;
686 	unsigned int type = IRQ_TYPE_NONE;
687 	int virq;
688 
689 	if (fwspec->fwnode) {
690 		domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_WIRED);
691 		if (!domain)
692 			domain = irq_find_matching_fwspec(fwspec, DOMAIN_BUS_ANY);
693 	} else {
694 		domain = irq_default_domain;
695 	}
696 
697 	if (!domain) {
698 		pr_warn("no irq domain found for %s !\n",
699 			of_node_full_name(to_of_node(fwspec->fwnode)));
700 		return 0;
701 	}
702 
703 	if (irq_domain_translate(domain, fwspec, &hwirq, &type))
704 		return 0;
705 
706 	/*
707 	 * WARN if the irqchip returns a type with bits
708 	 * outside the sense mask set and clear these bits.
709 	 */
710 	if (WARN_ON(type & ~IRQ_TYPE_SENSE_MASK))
711 		type &= IRQ_TYPE_SENSE_MASK;
712 
713 	/*
714 	 * If we've already configured this interrupt,
715 	 * don't do it again, or hell will break loose.
716 	 */
717 	virq = irq_find_mapping(domain, hwirq);
718 	if (virq) {
719 		/*
720 		 * If the trigger type is not specified or matches the
721 		 * current trigger type then we are done so return the
722 		 * interrupt number.
723 		 */
724 		if (type == IRQ_TYPE_NONE || type == irq_get_trigger_type(virq))
725 			return virq;
726 
727 		/*
728 		 * If the trigger type has not been set yet, then set
729 		 * it now and return the interrupt number.
730 		 */
731 		if (irq_get_trigger_type(virq) == IRQ_TYPE_NONE) {
732 			irq_data = irq_get_irq_data(virq);
733 			if (!irq_data)
734 				return 0;
735 
736 			irqd_set_trigger_type(irq_data, type);
737 			return virq;
738 		}
739 
740 		pr_warn("type mismatch, failed to map hwirq-%lu for %s!\n",
741 			hwirq, of_node_full_name(to_of_node(fwspec->fwnode)));
742 		return 0;
743 	}
744 
745 	if (irq_domain_is_hierarchy(domain)) {
746 		virq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, fwspec);
747 		if (virq <= 0)
748 			return 0;
749 	} else {
750 		/* Create mapping */
751 		virq = irq_create_mapping(domain, hwirq);
752 		if (!virq)
753 			return virq;
754 	}
755 
756 	irq_data = irq_get_irq_data(virq);
757 	if (!irq_data) {
758 		if (irq_domain_is_hierarchy(domain))
759 			irq_domain_free_irqs(virq, 1);
760 		else
761 			irq_dispose_mapping(virq);
762 		return 0;
763 	}
764 
765 	/* Store trigger type */
766 	irqd_set_trigger_type(irq_data, type);
767 
768 	return virq;
769 }
770 EXPORT_SYMBOL_GPL(irq_create_fwspec_mapping);
771 
772 unsigned int irq_create_of_mapping(struct of_phandle_args *irq_data)
773 {
774 	struct irq_fwspec fwspec;
775 
776 	of_phandle_args_to_fwspec(irq_data, &fwspec);
777 	return irq_create_fwspec_mapping(&fwspec);
778 }
779 EXPORT_SYMBOL_GPL(irq_create_of_mapping);
780 
781 /**
782  * irq_dispose_mapping() - Unmap an interrupt
783  * @virq: linux irq number of the interrupt to unmap
784  */
785 void irq_dispose_mapping(unsigned int virq)
786 {
787 	struct irq_data *irq_data = irq_get_irq_data(virq);
788 	struct irq_domain *domain;
789 
790 	if (!virq || !irq_data)
791 		return;
792 
793 	domain = irq_data->domain;
794 	if (WARN_ON(domain == NULL))
795 		return;
796 
797 	if (irq_domain_is_hierarchy(domain)) {
798 		irq_domain_free_irqs(virq, 1);
799 	} else {
800 		irq_domain_disassociate(domain, virq);
801 		irq_free_desc(virq);
802 	}
803 }
804 EXPORT_SYMBOL_GPL(irq_dispose_mapping);
805 
806 /**
807  * irq_find_mapping() - Find a linux irq from an hw irq number.
808  * @domain: domain owning this hardware interrupt
809  * @hwirq: hardware irq number in that domain space
810  */
811 unsigned int irq_find_mapping(struct irq_domain *domain,
812 			      irq_hw_number_t hwirq)
813 {
814 	struct irq_data *data;
815 
816 	/* Look for default domain if nececssary */
817 	if (domain == NULL)
818 		domain = irq_default_domain;
819 	if (domain == NULL)
820 		return 0;
821 
822 	if (hwirq < domain->revmap_direct_max_irq) {
823 		data = irq_domain_get_irq_data(domain, hwirq);
824 		if (data && data->hwirq == hwirq)
825 			return hwirq;
826 	}
827 
828 	/* Check if the hwirq is in the linear revmap. */
829 	if (hwirq < domain->revmap_size)
830 		return domain->linear_revmap[hwirq];
831 
832 	rcu_read_lock();
833 	data = radix_tree_lookup(&domain->revmap_tree, hwirq);
834 	rcu_read_unlock();
835 	return data ? data->irq : 0;
836 }
837 EXPORT_SYMBOL_GPL(irq_find_mapping);
838 
839 #ifdef CONFIG_IRQ_DOMAIN_DEBUG
840 static void virq_debug_show_one(struct seq_file *m, struct irq_desc *desc)
841 {
842 	struct irq_domain *domain;
843 	struct irq_data *data;
844 
845 	domain = desc->irq_data.domain;
846 	data = &desc->irq_data;
847 
848 	while (domain) {
849 		unsigned int irq = data->irq;
850 		unsigned long hwirq = data->hwirq;
851 		struct irq_chip *chip;
852 		bool direct;
853 
854 		if (data == &desc->irq_data)
855 			seq_printf(m, "%5d  ", irq);
856 		else
857 			seq_printf(m, "%5d+ ", irq);
858 		seq_printf(m, "0x%05lx  ", hwirq);
859 
860 		chip = irq_data_get_irq_chip(data);
861 		seq_printf(m, "%-15s  ", (chip && chip->name) ? chip->name : "none");
862 
863 		seq_printf(m, data ? "0x%p  " : "  %p  ",
864 			   irq_data_get_irq_chip_data(data));
865 
866 		seq_printf(m, "   %c    ", (desc->action && desc->action->handler) ? '*' : ' ');
867 		direct = (irq == hwirq) && (irq < domain->revmap_direct_max_irq);
868 		seq_printf(m, "%6s%-8s  ",
869 			   (hwirq < domain->revmap_size) ? "LINEAR" : "RADIX",
870 			   direct ? "(DIRECT)" : "");
871 		seq_printf(m, "%s\n", domain->name);
872 #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
873 		domain = domain->parent;
874 		data = data->parent_data;
875 #else
876 		domain = NULL;
877 #endif
878 	}
879 }
880 
881 static int virq_debug_show(struct seq_file *m, void *private)
882 {
883 	unsigned long flags;
884 	struct irq_desc *desc;
885 	struct irq_domain *domain;
886 	struct radix_tree_iter iter;
887 	void **slot;
888 	int i;
889 
890 	seq_printf(m, " %-16s  %-6s  %-10s  %-10s  %s\n",
891 		   "name", "mapped", "linear-max", "direct-max", "devtree-node");
892 	mutex_lock(&irq_domain_mutex);
893 	list_for_each_entry(domain, &irq_domain_list, link) {
894 		struct device_node *of_node;
895 		const char *name;
896 
897 		int count = 0;
898 
899 		of_node = irq_domain_get_of_node(domain);
900 		if (of_node)
901 			name = of_node_full_name(of_node);
902 		else if (is_fwnode_irqchip(domain->fwnode))
903 			name = container_of(domain->fwnode, struct irqchip_fwid,
904 					    fwnode)->name;
905 		else
906 			name = "";
907 
908 		radix_tree_for_each_slot(slot, &domain->revmap_tree, &iter, 0)
909 			count++;
910 		seq_printf(m, "%c%-16s  %6u  %10u  %10u  %s\n",
911 			   domain == irq_default_domain ? '*' : ' ', domain->name,
912 			   domain->revmap_size + count, domain->revmap_size,
913 			   domain->revmap_direct_max_irq,
914 			   name);
915 	}
916 	mutex_unlock(&irq_domain_mutex);
917 
918 	seq_printf(m, "%-5s  %-7s  %-15s  %-*s  %6s  %-14s  %s\n", "irq", "hwirq",
919 		      "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
920 		      "active", "type", "domain");
921 
922 	for (i = 1; i < nr_irqs; i++) {
923 		desc = irq_to_desc(i);
924 		if (!desc)
925 			continue;
926 
927 		raw_spin_lock_irqsave(&desc->lock, flags);
928 		virq_debug_show_one(m, desc);
929 		raw_spin_unlock_irqrestore(&desc->lock, flags);
930 	}
931 
932 	return 0;
933 }
934 
935 static int virq_debug_open(struct inode *inode, struct file *file)
936 {
937 	return single_open(file, virq_debug_show, inode->i_private);
938 }
939 
940 static const struct file_operations virq_debug_fops = {
941 	.open = virq_debug_open,
942 	.read = seq_read,
943 	.llseek = seq_lseek,
944 	.release = single_release,
945 };
946 
947 static int __init irq_debugfs_init(void)
948 {
949 	if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
950 				 NULL, &virq_debug_fops) == NULL)
951 		return -ENOMEM;
952 
953 	return 0;
954 }
955 __initcall(irq_debugfs_init);
956 #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
957 
958 /**
959  * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
960  *
961  * Device Tree IRQ specifier translation function which works with one cell
962  * bindings where the cell value maps directly to the hwirq number.
963  */
964 int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
965 			     const u32 *intspec, unsigned int intsize,
966 			     unsigned long *out_hwirq, unsigned int *out_type)
967 {
968 	if (WARN_ON(intsize < 1))
969 		return -EINVAL;
970 	*out_hwirq = intspec[0];
971 	*out_type = IRQ_TYPE_NONE;
972 	return 0;
973 }
974 EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
975 
976 /**
977  * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
978  *
979  * Device Tree IRQ specifier translation function which works with two cell
980  * bindings where the cell values map directly to the hwirq number
981  * and linux irq flags.
982  */
983 int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
984 			const u32 *intspec, unsigned int intsize,
985 			irq_hw_number_t *out_hwirq, unsigned int *out_type)
986 {
987 	if (WARN_ON(intsize < 2))
988 		return -EINVAL;
989 	*out_hwirq = intspec[0];
990 	*out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
991 	return 0;
992 }
993 EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
994 
995 /**
996  * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
997  *
998  * Device Tree IRQ specifier translation function which works with either one
999  * or two cell bindings where the cell values map directly to the hwirq number
1000  * and linux irq flags.
1001  *
1002  * Note: don't use this function unless your interrupt controller explicitly
1003  * supports both one and two cell bindings.  For the majority of controllers
1004  * the _onecell() or _twocell() variants above should be used.
1005  */
1006 int irq_domain_xlate_onetwocell(struct irq_domain *d,
1007 				struct device_node *ctrlr,
1008 				const u32 *intspec, unsigned int intsize,
1009 				unsigned long *out_hwirq, unsigned int *out_type)
1010 {
1011 	if (WARN_ON(intsize < 1))
1012 		return -EINVAL;
1013 	*out_hwirq = intspec[0];
1014 	if (intsize > 1)
1015 		*out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
1016 	else
1017 		*out_type = IRQ_TYPE_NONE;
1018 	return 0;
1019 }
1020 EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
1021 
1022 const struct irq_domain_ops irq_domain_simple_ops = {
1023 	.xlate = irq_domain_xlate_onetwocell,
1024 };
1025 EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
1026 
1027 int irq_domain_alloc_descs(int virq, unsigned int cnt, irq_hw_number_t hwirq,
1028 			   int node, const struct cpumask *affinity)
1029 {
1030 	unsigned int hint;
1031 
1032 	if (virq >= 0) {
1033 		virq = __irq_alloc_descs(virq, virq, cnt, node, THIS_MODULE,
1034 					 affinity);
1035 	} else {
1036 		hint = hwirq % nr_irqs;
1037 		if (hint == 0)
1038 			hint++;
1039 		virq = __irq_alloc_descs(-1, hint, cnt, node, THIS_MODULE,
1040 					 affinity);
1041 		if (virq <= 0 && hint > 1) {
1042 			virq = __irq_alloc_descs(-1, 1, cnt, node, THIS_MODULE,
1043 						 affinity);
1044 		}
1045 	}
1046 
1047 	return virq;
1048 }
1049 
1050 #ifdef	CONFIG_IRQ_DOMAIN_HIERARCHY
1051 /**
1052  * irq_domain_create_hierarchy - Add a irqdomain into the hierarchy
1053  * @parent:	Parent irq domain to associate with the new domain
1054  * @flags:	Irq domain flags associated to the domain
1055  * @size:	Size of the domain. See below
1056  * @fwnode:	Optional fwnode of the interrupt controller
1057  * @ops:	Pointer to the interrupt domain callbacks
1058  * @host_data:	Controller private data pointer
1059  *
1060  * If @size is 0 a tree domain is created, otherwise a linear domain.
1061  *
1062  * If successful the parent is associated to the new domain and the
1063  * domain flags are set.
1064  * Returns pointer to IRQ domain, or NULL on failure.
1065  */
1066 struct irq_domain *irq_domain_create_hierarchy(struct irq_domain *parent,
1067 					    unsigned int flags,
1068 					    unsigned int size,
1069 					    struct fwnode_handle *fwnode,
1070 					    const struct irq_domain_ops *ops,
1071 					    void *host_data)
1072 {
1073 	struct irq_domain *domain;
1074 
1075 	if (size)
1076 		domain = irq_domain_create_linear(fwnode, size, ops, host_data);
1077 	else
1078 		domain = irq_domain_create_tree(fwnode, ops, host_data);
1079 	if (domain) {
1080 		domain->parent = parent;
1081 		domain->flags |= flags;
1082 	}
1083 
1084 	return domain;
1085 }
1086 EXPORT_SYMBOL_GPL(irq_domain_create_hierarchy);
1087 
1088 static void irq_domain_insert_irq(int virq)
1089 {
1090 	struct irq_data *data;
1091 
1092 	for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1093 		struct irq_domain *domain = data->domain;
1094 		irq_hw_number_t hwirq = data->hwirq;
1095 
1096 		domain->mapcount++;
1097 		if (hwirq < domain->revmap_size) {
1098 			domain->linear_revmap[hwirq] = virq;
1099 		} else {
1100 			mutex_lock(&revmap_trees_mutex);
1101 			radix_tree_insert(&domain->revmap_tree, hwirq, data);
1102 			mutex_unlock(&revmap_trees_mutex);
1103 		}
1104 
1105 		/* If not already assigned, give the domain the chip's name */
1106 		if (!domain->name && data->chip)
1107 			domain->name = data->chip->name;
1108 	}
1109 
1110 	irq_clear_status_flags(virq, IRQ_NOREQUEST);
1111 }
1112 
1113 static void irq_domain_remove_irq(int virq)
1114 {
1115 	struct irq_data *data;
1116 
1117 	irq_set_status_flags(virq, IRQ_NOREQUEST);
1118 	irq_set_chip_and_handler(virq, NULL, NULL);
1119 	synchronize_irq(virq);
1120 	smp_mb();
1121 
1122 	for (data = irq_get_irq_data(virq); data; data = data->parent_data) {
1123 		struct irq_domain *domain = data->domain;
1124 		irq_hw_number_t hwirq = data->hwirq;
1125 
1126 		domain->mapcount--;
1127 		if (hwirq < domain->revmap_size) {
1128 			domain->linear_revmap[hwirq] = 0;
1129 		} else {
1130 			mutex_lock(&revmap_trees_mutex);
1131 			radix_tree_delete(&domain->revmap_tree, hwirq);
1132 			mutex_unlock(&revmap_trees_mutex);
1133 		}
1134 	}
1135 }
1136 
1137 static struct irq_data *irq_domain_insert_irq_data(struct irq_domain *domain,
1138 						   struct irq_data *child)
1139 {
1140 	struct irq_data *irq_data;
1141 
1142 	irq_data = kzalloc_node(sizeof(*irq_data), GFP_KERNEL,
1143 				irq_data_get_node(child));
1144 	if (irq_data) {
1145 		child->parent_data = irq_data;
1146 		irq_data->irq = child->irq;
1147 		irq_data->common = child->common;
1148 		irq_data->domain = domain;
1149 	}
1150 
1151 	return irq_data;
1152 }
1153 
1154 static void irq_domain_free_irq_data(unsigned int virq, unsigned int nr_irqs)
1155 {
1156 	struct irq_data *irq_data, *tmp;
1157 	int i;
1158 
1159 	for (i = 0; i < nr_irqs; i++) {
1160 		irq_data = irq_get_irq_data(virq + i);
1161 		tmp = irq_data->parent_data;
1162 		irq_data->parent_data = NULL;
1163 		irq_data->domain = NULL;
1164 
1165 		while (tmp) {
1166 			irq_data = tmp;
1167 			tmp = tmp->parent_data;
1168 			kfree(irq_data);
1169 		}
1170 	}
1171 }
1172 
1173 static int irq_domain_alloc_irq_data(struct irq_domain *domain,
1174 				     unsigned int virq, unsigned int nr_irqs)
1175 {
1176 	struct irq_data *irq_data;
1177 	struct irq_domain *parent;
1178 	int i;
1179 
1180 	/* The outermost irq_data is embedded in struct irq_desc */
1181 	for (i = 0; i < nr_irqs; i++) {
1182 		irq_data = irq_get_irq_data(virq + i);
1183 		irq_data->domain = domain;
1184 
1185 		for (parent = domain->parent; parent; parent = parent->parent) {
1186 			irq_data = irq_domain_insert_irq_data(parent, irq_data);
1187 			if (!irq_data) {
1188 				irq_domain_free_irq_data(virq, i + 1);
1189 				return -ENOMEM;
1190 			}
1191 		}
1192 	}
1193 
1194 	return 0;
1195 }
1196 
1197 /**
1198  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1199  * @domain:	domain to match
1200  * @virq:	IRQ number to get irq_data
1201  */
1202 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1203 					 unsigned int virq)
1204 {
1205 	struct irq_data *irq_data;
1206 
1207 	for (irq_data = irq_get_irq_data(virq); irq_data;
1208 	     irq_data = irq_data->parent_data)
1209 		if (irq_data->domain == domain)
1210 			return irq_data;
1211 
1212 	return NULL;
1213 }
1214 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1215 
1216 /**
1217  * irq_domain_set_hwirq_and_chip - Set hwirq and irqchip of @virq at @domain
1218  * @domain:	Interrupt domain to match
1219  * @virq:	IRQ number
1220  * @hwirq:	The hwirq number
1221  * @chip:	The associated interrupt chip
1222  * @chip_data:	The associated chip data
1223  */
1224 int irq_domain_set_hwirq_and_chip(struct irq_domain *domain, unsigned int virq,
1225 				  irq_hw_number_t hwirq, struct irq_chip *chip,
1226 				  void *chip_data)
1227 {
1228 	struct irq_data *irq_data = irq_domain_get_irq_data(domain, virq);
1229 
1230 	if (!irq_data)
1231 		return -ENOENT;
1232 
1233 	irq_data->hwirq = hwirq;
1234 	irq_data->chip = chip ? chip : &no_irq_chip;
1235 	irq_data->chip_data = chip_data;
1236 
1237 	return 0;
1238 }
1239 EXPORT_SYMBOL_GPL(irq_domain_set_hwirq_and_chip);
1240 
1241 /**
1242  * irq_domain_set_info - Set the complete data for a @virq in @domain
1243  * @domain:		Interrupt domain to match
1244  * @virq:		IRQ number
1245  * @hwirq:		The hardware interrupt number
1246  * @chip:		The associated interrupt chip
1247  * @chip_data:		The associated interrupt chip data
1248  * @handler:		The interrupt flow handler
1249  * @handler_data:	The interrupt flow handler data
1250  * @handler_name:	The interrupt handler name
1251  */
1252 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1253 			 irq_hw_number_t hwirq, struct irq_chip *chip,
1254 			 void *chip_data, irq_flow_handler_t handler,
1255 			 void *handler_data, const char *handler_name)
1256 {
1257 	irq_domain_set_hwirq_and_chip(domain, virq, hwirq, chip, chip_data);
1258 	__irq_set_handler(virq, handler, 0, handler_name);
1259 	irq_set_handler_data(virq, handler_data);
1260 }
1261 EXPORT_SYMBOL(irq_domain_set_info);
1262 
1263 /**
1264  * irq_domain_reset_irq_data - Clear hwirq, chip and chip_data in @irq_data
1265  * @irq_data:	The pointer to irq_data
1266  */
1267 void irq_domain_reset_irq_data(struct irq_data *irq_data)
1268 {
1269 	irq_data->hwirq = 0;
1270 	irq_data->chip = &no_irq_chip;
1271 	irq_data->chip_data = NULL;
1272 }
1273 EXPORT_SYMBOL_GPL(irq_domain_reset_irq_data);
1274 
1275 /**
1276  * irq_domain_free_irqs_common - Clear irq_data and free the parent
1277  * @domain:	Interrupt domain to match
1278  * @virq:	IRQ number to start with
1279  * @nr_irqs:	The number of irqs to free
1280  */
1281 void irq_domain_free_irqs_common(struct irq_domain *domain, unsigned int virq,
1282 				 unsigned int nr_irqs)
1283 {
1284 	struct irq_data *irq_data;
1285 	int i;
1286 
1287 	for (i = 0; i < nr_irqs; i++) {
1288 		irq_data = irq_domain_get_irq_data(domain, virq + i);
1289 		if (irq_data)
1290 			irq_domain_reset_irq_data(irq_data);
1291 	}
1292 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1293 }
1294 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_common);
1295 
1296 /**
1297  * irq_domain_free_irqs_top - Clear handler and handler data, clear irqdata and free parent
1298  * @domain:	Interrupt domain to match
1299  * @virq:	IRQ number to start with
1300  * @nr_irqs:	The number of irqs to free
1301  */
1302 void irq_domain_free_irqs_top(struct irq_domain *domain, unsigned int virq,
1303 			      unsigned int nr_irqs)
1304 {
1305 	int i;
1306 
1307 	for (i = 0; i < nr_irqs; i++) {
1308 		irq_set_handler_data(virq + i, NULL);
1309 		irq_set_handler(virq + i, NULL);
1310 	}
1311 	irq_domain_free_irqs_common(domain, virq, nr_irqs);
1312 }
1313 
1314 static bool irq_domain_is_auto_recursive(struct irq_domain *domain)
1315 {
1316 	return domain->flags & IRQ_DOMAIN_FLAG_AUTO_RECURSIVE;
1317 }
1318 
1319 static void irq_domain_free_irqs_recursive(struct irq_domain *domain,
1320 					   unsigned int irq_base,
1321 					   unsigned int nr_irqs)
1322 {
1323 	domain->ops->free(domain, irq_base, nr_irqs);
1324 	if (irq_domain_is_auto_recursive(domain)) {
1325 		BUG_ON(!domain->parent);
1326 		irq_domain_free_irqs_recursive(domain->parent, irq_base,
1327 					       nr_irqs);
1328 	}
1329 }
1330 
1331 int irq_domain_alloc_irqs_recursive(struct irq_domain *domain,
1332 				    unsigned int irq_base,
1333 				    unsigned int nr_irqs, void *arg)
1334 {
1335 	int ret = 0;
1336 	struct irq_domain *parent = domain->parent;
1337 	bool recursive = irq_domain_is_auto_recursive(domain);
1338 
1339 	BUG_ON(recursive && !parent);
1340 	if (recursive)
1341 		ret = irq_domain_alloc_irqs_recursive(parent, irq_base,
1342 						      nr_irqs, arg);
1343 	if (ret < 0)
1344 		return ret;
1345 
1346 	ret = domain->ops->alloc(domain, irq_base, nr_irqs, arg);
1347 	if (ret < 0 && recursive)
1348 		irq_domain_free_irqs_recursive(parent, irq_base, nr_irqs);
1349 
1350 	return ret;
1351 }
1352 
1353 /**
1354  * __irq_domain_alloc_irqs - Allocate IRQs from domain
1355  * @domain:	domain to allocate from
1356  * @irq_base:	allocate specified IRQ nubmer if irq_base >= 0
1357  * @nr_irqs:	number of IRQs to allocate
1358  * @node:	NUMA node id for memory allocation
1359  * @arg:	domain specific argument
1360  * @realloc:	IRQ descriptors have already been allocated if true
1361  * @affinity:	Optional irq affinity mask for multiqueue devices
1362  *
1363  * Allocate IRQ numbers and initialized all data structures to support
1364  * hierarchy IRQ domains.
1365  * Parameter @realloc is mainly to support legacy IRQs.
1366  * Returns error code or allocated IRQ number
1367  *
1368  * The whole process to setup an IRQ has been split into two steps.
1369  * The first step, __irq_domain_alloc_irqs(), is to allocate IRQ
1370  * descriptor and required hardware resources. The second step,
1371  * irq_domain_activate_irq(), is to program hardwares with preallocated
1372  * resources. In this way, it's easier to rollback when failing to
1373  * allocate resources.
1374  */
1375 int __irq_domain_alloc_irqs(struct irq_domain *domain, int irq_base,
1376 			    unsigned int nr_irqs, int node, void *arg,
1377 			    bool realloc, const struct cpumask *affinity)
1378 {
1379 	int i, ret, virq;
1380 
1381 	if (domain == NULL) {
1382 		domain = irq_default_domain;
1383 		if (WARN(!domain, "domain is NULL; cannot allocate IRQ\n"))
1384 			return -EINVAL;
1385 	}
1386 
1387 	if (!domain->ops->alloc) {
1388 		pr_debug("domain->ops->alloc() is NULL\n");
1389 		return -ENOSYS;
1390 	}
1391 
1392 	if (realloc && irq_base >= 0) {
1393 		virq = irq_base;
1394 	} else {
1395 		virq = irq_domain_alloc_descs(irq_base, nr_irqs, 0, node,
1396 					      affinity);
1397 		if (virq < 0) {
1398 			pr_debug("cannot allocate IRQ(base %d, count %d)\n",
1399 				 irq_base, nr_irqs);
1400 			return virq;
1401 		}
1402 	}
1403 
1404 	if (irq_domain_alloc_irq_data(domain, virq, nr_irqs)) {
1405 		pr_debug("cannot allocate memory for IRQ%d\n", virq);
1406 		ret = -ENOMEM;
1407 		goto out_free_desc;
1408 	}
1409 
1410 	mutex_lock(&irq_domain_mutex);
1411 	ret = irq_domain_alloc_irqs_recursive(domain, virq, nr_irqs, arg);
1412 	if (ret < 0) {
1413 		mutex_unlock(&irq_domain_mutex);
1414 		goto out_free_irq_data;
1415 	}
1416 	for (i = 0; i < nr_irqs; i++)
1417 		irq_domain_insert_irq(virq + i);
1418 	mutex_unlock(&irq_domain_mutex);
1419 
1420 	return virq;
1421 
1422 out_free_irq_data:
1423 	irq_domain_free_irq_data(virq, nr_irqs);
1424 out_free_desc:
1425 	irq_free_descs(virq, nr_irqs);
1426 	return ret;
1427 }
1428 
1429 /**
1430  * irq_domain_free_irqs - Free IRQ number and associated data structures
1431  * @virq:	base IRQ number
1432  * @nr_irqs:	number of IRQs to free
1433  */
1434 void irq_domain_free_irqs(unsigned int virq, unsigned int nr_irqs)
1435 {
1436 	struct irq_data *data = irq_get_irq_data(virq);
1437 	int i;
1438 
1439 	if (WARN(!data || !data->domain || !data->domain->ops->free,
1440 		 "NULL pointer, cannot free irq\n"))
1441 		return;
1442 
1443 	mutex_lock(&irq_domain_mutex);
1444 	for (i = 0; i < nr_irqs; i++)
1445 		irq_domain_remove_irq(virq + i);
1446 	irq_domain_free_irqs_recursive(data->domain, virq, nr_irqs);
1447 	mutex_unlock(&irq_domain_mutex);
1448 
1449 	irq_domain_free_irq_data(virq, nr_irqs);
1450 	irq_free_descs(virq, nr_irqs);
1451 }
1452 
1453 /**
1454  * irq_domain_alloc_irqs_parent - Allocate interrupts from parent domain
1455  * @irq_base:	Base IRQ number
1456  * @nr_irqs:	Number of IRQs to allocate
1457  * @arg:	Allocation data (arch/domain specific)
1458  *
1459  * Check whether the domain has been setup recursive. If not allocate
1460  * through the parent domain.
1461  */
1462 int irq_domain_alloc_irqs_parent(struct irq_domain *domain,
1463 				 unsigned int irq_base, unsigned int nr_irqs,
1464 				 void *arg)
1465 {
1466 	/* irq_domain_alloc_irqs_recursive() has called parent's alloc() */
1467 	if (irq_domain_is_auto_recursive(domain))
1468 		return 0;
1469 
1470 	domain = domain->parent;
1471 	if (domain)
1472 		return irq_domain_alloc_irqs_recursive(domain, irq_base,
1473 						       nr_irqs, arg);
1474 	return -ENOSYS;
1475 }
1476 EXPORT_SYMBOL_GPL(irq_domain_alloc_irqs_parent);
1477 
1478 /**
1479  * irq_domain_free_irqs_parent - Free interrupts from parent domain
1480  * @irq_base:	Base IRQ number
1481  * @nr_irqs:	Number of IRQs to free
1482  *
1483  * Check whether the domain has been setup recursive. If not free
1484  * through the parent domain.
1485  */
1486 void irq_domain_free_irqs_parent(struct irq_domain *domain,
1487 				 unsigned int irq_base, unsigned int nr_irqs)
1488 {
1489 	/* irq_domain_free_irqs_recursive() will call parent's free */
1490 	if (!irq_domain_is_auto_recursive(domain) && domain->parent)
1491 		irq_domain_free_irqs_recursive(domain->parent, irq_base,
1492 					       nr_irqs);
1493 }
1494 EXPORT_SYMBOL_GPL(irq_domain_free_irqs_parent);
1495 
1496 static void __irq_domain_activate_irq(struct irq_data *irq_data)
1497 {
1498 	if (irq_data && irq_data->domain) {
1499 		struct irq_domain *domain = irq_data->domain;
1500 
1501 		if (irq_data->parent_data)
1502 			__irq_domain_activate_irq(irq_data->parent_data);
1503 		if (domain->ops->activate)
1504 			domain->ops->activate(domain, irq_data);
1505 	}
1506 }
1507 
1508 static void __irq_domain_deactivate_irq(struct irq_data *irq_data)
1509 {
1510 	if (irq_data && irq_data->domain) {
1511 		struct irq_domain *domain = irq_data->domain;
1512 
1513 		if (domain->ops->deactivate)
1514 			domain->ops->deactivate(domain, irq_data);
1515 		if (irq_data->parent_data)
1516 			__irq_domain_deactivate_irq(irq_data->parent_data);
1517 	}
1518 }
1519 
1520 /**
1521  * irq_domain_activate_irq - Call domain_ops->activate recursively to activate
1522  *			     interrupt
1523  * @irq_data:	outermost irq_data associated with interrupt
1524  *
1525  * This is the second step to call domain_ops->activate to program interrupt
1526  * controllers, so the interrupt could actually get delivered.
1527  */
1528 void irq_domain_activate_irq(struct irq_data *irq_data)
1529 {
1530 	if (!irqd_is_activated(irq_data)) {
1531 		__irq_domain_activate_irq(irq_data);
1532 		irqd_set_activated(irq_data);
1533 	}
1534 }
1535 
1536 /**
1537  * irq_domain_deactivate_irq - Call domain_ops->deactivate recursively to
1538  *			       deactivate interrupt
1539  * @irq_data: outermost irq_data associated with interrupt
1540  *
1541  * It calls domain_ops->deactivate to program interrupt controllers to disable
1542  * interrupt delivery.
1543  */
1544 void irq_domain_deactivate_irq(struct irq_data *irq_data)
1545 {
1546 	if (irqd_is_activated(irq_data)) {
1547 		__irq_domain_deactivate_irq(irq_data);
1548 		irqd_clr_activated(irq_data);
1549 	}
1550 }
1551 
1552 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1553 {
1554 	/* Hierarchy irq_domains must implement callback alloc() */
1555 	if (domain->ops->alloc)
1556 		domain->flags |= IRQ_DOMAIN_FLAG_HIERARCHY;
1557 }
1558 
1559 /**
1560  * irq_domain_hierarchical_is_msi_remap - Check if the domain or any
1561  * parent has MSI remapping support
1562  * @domain: domain pointer
1563  */
1564 bool irq_domain_hierarchical_is_msi_remap(struct irq_domain *domain)
1565 {
1566 	for (; domain; domain = domain->parent) {
1567 		if (irq_domain_is_msi_remap(domain))
1568 			return true;
1569 	}
1570 	return false;
1571 }
1572 #else	/* CONFIG_IRQ_DOMAIN_HIERARCHY */
1573 /**
1574  * irq_domain_get_irq_data - Get irq_data associated with @virq and @domain
1575  * @domain:	domain to match
1576  * @virq:	IRQ number to get irq_data
1577  */
1578 struct irq_data *irq_domain_get_irq_data(struct irq_domain *domain,
1579 					 unsigned int virq)
1580 {
1581 	struct irq_data *irq_data = irq_get_irq_data(virq);
1582 
1583 	return (irq_data && irq_data->domain == domain) ? irq_data : NULL;
1584 }
1585 EXPORT_SYMBOL_GPL(irq_domain_get_irq_data);
1586 
1587 /**
1588  * irq_domain_set_info - Set the complete data for a @virq in @domain
1589  * @domain:		Interrupt domain to match
1590  * @virq:		IRQ number
1591  * @hwirq:		The hardware interrupt number
1592  * @chip:		The associated interrupt chip
1593  * @chip_data:		The associated interrupt chip data
1594  * @handler:		The interrupt flow handler
1595  * @handler_data:	The interrupt flow handler data
1596  * @handler_name:	The interrupt handler name
1597  */
1598 void irq_domain_set_info(struct irq_domain *domain, unsigned int virq,
1599 			 irq_hw_number_t hwirq, struct irq_chip *chip,
1600 			 void *chip_data, irq_flow_handler_t handler,
1601 			 void *handler_data, const char *handler_name)
1602 {
1603 	irq_set_chip_and_handler_name(virq, chip, handler, handler_name);
1604 	irq_set_chip_data(virq, chip_data);
1605 	irq_set_handler_data(virq, handler_data);
1606 }
1607 
1608 static void irq_domain_check_hierarchy(struct irq_domain *domain)
1609 {
1610 }
1611 #endif	/* CONFIG_IRQ_DOMAIN_HIERARCHY */
1612 
1613 #ifdef CONFIG_GENERIC_IRQ_DEBUGFS
1614 static struct dentry *domain_dir;
1615 
1616 static void
1617 irq_domain_debug_show_one(struct seq_file *m, struct irq_domain *d, int ind)
1618 {
1619 	seq_printf(m, "%*sname:   %s\n", ind, "", d->name);
1620 	seq_printf(m, "%*ssize:   %u\n", ind + 1, "",
1621 		   d->revmap_size + d->revmap_direct_max_irq);
1622 	seq_printf(m, "%*smapped: %u\n", ind + 1, "", d->mapcount);
1623 	seq_printf(m, "%*sflags:  0x%08x\n", ind +1 , "", d->flags);
1624 #ifdef	CONFIG_IRQ_DOMAIN_HIERARCHY
1625 	if (!d->parent)
1626 		return;
1627 	seq_printf(m, "%*sparent: %s\n", ind + 1, "", d->parent->name);
1628 	irq_domain_debug_show_one(m, d->parent, ind + 4);
1629 #endif
1630 }
1631 
1632 static int irq_domain_debug_show(struct seq_file *m, void *p)
1633 {
1634 	struct irq_domain *d = m->private;
1635 
1636 	/* Default domain? Might be NULL */
1637 	if (!d) {
1638 		if (!irq_default_domain)
1639 			return 0;
1640 		d = irq_default_domain;
1641 	}
1642 	irq_domain_debug_show_one(m, d, 0);
1643 	return 0;
1644 }
1645 
1646 static int irq_domain_debug_open(struct inode *inode, struct file *file)
1647 {
1648 	return single_open(file, irq_domain_debug_show, inode->i_private);
1649 }
1650 
1651 static const struct file_operations dfs_domain_ops = {
1652 	.open		= irq_domain_debug_open,
1653 	.read		= seq_read,
1654 	.llseek		= seq_lseek,
1655 	.release	= single_release,
1656 };
1657 
1658 static void debugfs_add_domain_dir(struct irq_domain *d)
1659 {
1660 	if (!d->name || !domain_dir || d->debugfs_file)
1661 		return;
1662 	d->debugfs_file = debugfs_create_file(d->name, 0444, domain_dir, d,
1663 					      &dfs_domain_ops);
1664 }
1665 
1666 static void debugfs_remove_domain_dir(struct irq_domain *d)
1667 {
1668 	if (d->debugfs_file)
1669 		debugfs_remove(d->debugfs_file);
1670 }
1671 
1672 void __init irq_domain_debugfs_init(struct dentry *root)
1673 {
1674 	struct irq_domain *d;
1675 
1676 	domain_dir = debugfs_create_dir("domains", root);
1677 	if (!domain_dir)
1678 		return;
1679 
1680 	debugfs_create_file("default", 0444, domain_dir, NULL, &dfs_domain_ops);
1681 	mutex_lock(&irq_domain_mutex);
1682 	list_for_each_entry(d, &irq_domain_list, link)
1683 		debugfs_add_domain_dir(d);
1684 	mutex_unlock(&irq_domain_mutex);
1685 }
1686 #endif
1687