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