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