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