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