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