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