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