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