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