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