xref: /linux/drivers/of/irq.c (revision af8e51644a70f612974a6e767fa7d896d3c23f88)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Derived from arch/i386/kernel/irq.c
4  *    Copyright (C) 1992 Linus Torvalds
5  *  Adapted from arch/i386 by Gary Thomas
6  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7  *  Updated and modified by Cort Dougan <cort@fsmlabs.com>
8  *    Copyright (C) 1996-2001 Cort Dougan
9  *  Adapted for Power Macintosh by Paul Mackerras
10  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
11  *
12  * This file contains the code used to make IRQ descriptions in the
13  * device tree to actual irq numbers on an interrupt controller
14  * driver.
15  */
16 
17 #define pr_fmt(fmt)	"OF: " fmt
18 
19 #include <linux/device.h>
20 #include <linux/errno.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_irq.h>
25 #include <linux/string.h>
26 #include <linux/slab.h>
27 
28 #include "of_private.h"
29 
30 /**
31  * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
32  * @dev: Device node of the device whose interrupt is to be mapped
33  * @index: Index of the interrupt to map
34  *
35  * This function is a wrapper that chains of_irq_parse_one() and
36  * irq_create_of_mapping() to make things easier to callers
37  */
38 unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
39 {
40 	struct of_phandle_args oirq;
41 
42 	if (of_irq_parse_one(dev, index, &oirq))
43 		return 0;
44 
45 	return irq_create_of_mapping(&oirq);
46 }
47 EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
48 
49 /**
50  * of_irq_find_parent - Given a device node, find its interrupt parent node
51  * @child: pointer to device node
52  *
53  * Return: A pointer to the interrupt parent node, or NULL if the interrupt
54  * parent could not be determined.
55  */
56 struct device_node *of_irq_find_parent(struct device_node *child)
57 {
58 	struct device_node *p;
59 	phandle parent;
60 
61 	if (!of_node_get(child))
62 		return NULL;
63 
64 	do {
65 		if (of_property_read_u32(child, "interrupt-parent", &parent)) {
66 			p = of_get_parent(child);
67 		} else	{
68 			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
69 				p = of_node_get(of_irq_dflt_pic);
70 			else
71 				p = of_find_node_by_phandle(parent);
72 		}
73 		of_node_put(child);
74 		child = p;
75 	} while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
76 
77 	return p;
78 }
79 EXPORT_SYMBOL_GPL(of_irq_find_parent);
80 
81 /*
82  * These interrupt controllers abuse interrupt-map for unspeakable
83  * reasons and rely on the core code to *ignore* it (the drivers do
84  * their own parsing of the property).
85  *
86  * If you think of adding to the list for something *new*, think
87  * again. There is a high chance that you will be sent back to the
88  * drawing board.
89  */
90 static const char * const of_irq_imap_abusers[] = {
91 	"CBEA,platform-spider-pic",
92 	"sti,platform-spider-pic",
93 	"realtek,rtl-intc",
94 	"fsl,ls1021a-extirq",
95 	"fsl,ls1043a-extirq",
96 	"fsl,ls1088a-extirq",
97 	"renesas,rza1-irqc",
98 	NULL,
99 };
100 
101 const __be32 *of_irq_parse_imap_parent(const __be32 *imap, int len, struct of_phandle_args *out_irq)
102 {
103 	u32 intsize, addrsize;
104 	struct device_node *np;
105 
106 	/* Get the interrupt parent */
107 	if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
108 		np = of_node_get(of_irq_dflt_pic);
109 	else
110 		np = of_find_node_by_phandle(be32_to_cpup(imap));
111 	imap++;
112 
113 	/* Check if not found */
114 	if (!np) {
115 		pr_debug(" -> imap parent not found !\n");
116 		return NULL;
117 	}
118 
119 	/* Get #interrupt-cells and #address-cells of new parent */
120 	if (of_property_read_u32(np, "#interrupt-cells",
121 					&intsize)) {
122 		pr_debug(" -> parent lacks #interrupt-cells!\n");
123 		of_node_put(np);
124 		return NULL;
125 	}
126 	if (of_property_read_u32(np, "#address-cells",
127 					&addrsize))
128 		addrsize = 0;
129 
130 	pr_debug(" -> intsize=%d, addrsize=%d\n",
131 		intsize, addrsize);
132 
133 	/* Check for malformed properties */
134 	if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)
135 		|| (len < (addrsize + intsize))) {
136 		of_node_put(np);
137 		return NULL;
138 	}
139 
140 	pr_debug(" -> imaplen=%d\n", len);
141 
142 	imap += addrsize + intsize;
143 
144 	out_irq->np = np;
145 	for (int i = 0; i < intsize; i++)
146 		out_irq->args[i] = be32_to_cpup(imap - intsize + i);
147 	out_irq->args_count = intsize;
148 
149 	return imap;
150 }
151 
152 /**
153  * of_irq_parse_raw - Low level interrupt tree parsing
154  * @addr:	address specifier (start of "reg" property of the device) in be32 format
155  * @out_irq:	structure of_phandle_args updated by this function
156  *
157  * This function is a low-level interrupt tree walking function. It
158  * can be used to do a partial walk with synthetized reg and interrupts
159  * properties, for example when resolving PCI interrupts when no device
160  * node exist for the parent. It takes an interrupt specifier structure as
161  * input, walks the tree looking for any interrupt-map properties, translates
162  * the specifier for each map, and then returns the translated map.
163  *
164  * Return: 0 on success and a negative number on error
165  */
166 int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
167 {
168 	struct device_node *ipar, *tnode, *old = NULL;
169 	__be32 initial_match_array[MAX_PHANDLE_ARGS];
170 	const __be32 *match_array = initial_match_array;
171 	const __be32 *tmp, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) };
172 	u32 intsize = 1, addrsize;
173 	int i, rc = -EINVAL;
174 
175 #ifdef DEBUG
176 	of_print_phandle_args("of_irq_parse_raw: ", out_irq);
177 #endif
178 
179 	ipar = of_node_get(out_irq->np);
180 
181 	/* First get the #interrupt-cells property of the current cursor
182 	 * that tells us how to interpret the passed-in intspec. If there
183 	 * is none, we are nice and just walk up the tree
184 	 */
185 	do {
186 		if (!of_property_read_u32(ipar, "#interrupt-cells", &intsize))
187 			break;
188 		tnode = ipar;
189 		ipar = of_irq_find_parent(ipar);
190 		of_node_put(tnode);
191 	} while (ipar);
192 	if (ipar == NULL) {
193 		pr_debug(" -> no parent found !\n");
194 		goto fail;
195 	}
196 
197 	pr_debug("of_irq_parse_raw: ipar=%pOF, size=%d\n", ipar, intsize);
198 
199 	if (out_irq->args_count != intsize)
200 		goto fail;
201 
202 	/* Look for this #address-cells. We have to implement the old linux
203 	 * trick of looking for the parent here as some device-trees rely on it
204 	 */
205 	old = of_node_get(ipar);
206 	do {
207 		tmp = of_get_property(old, "#address-cells", NULL);
208 		tnode = of_get_parent(old);
209 		of_node_put(old);
210 		old = tnode;
211 	} while (old && tmp == NULL);
212 	of_node_put(old);
213 	old = NULL;
214 	addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
215 
216 	pr_debug(" -> addrsize=%d\n", addrsize);
217 
218 	/* Range check so that the temporary buffer doesn't overflow */
219 	if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) {
220 		rc = -EFAULT;
221 		goto fail;
222 	}
223 
224 	/* Precalculate the match array - this simplifies match loop */
225 	for (i = 0; i < addrsize; i++)
226 		initial_match_array[i] = addr ? addr[i] : 0;
227 	for (i = 0; i < intsize; i++)
228 		initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);
229 
230 	/* Now start the actual "proper" walk of the interrupt tree */
231 	while (ipar != NULL) {
232 		int imaplen, match;
233 		const __be32 *imap, *oldimap, *imask;
234 		struct device_node *newpar;
235 		/*
236 		 * Now check if cursor is an interrupt-controller and
237 		 * if it is then we are done, unless there is an
238 		 * interrupt-map which takes precedence except on one
239 		 * of these broken platforms that want to parse
240 		 * interrupt-map themselves for $reason.
241 		 */
242 		bool intc = of_property_read_bool(ipar, "interrupt-controller");
243 
244 		imap = of_get_property(ipar, "interrupt-map", &imaplen);
245 		if (intc &&
246 		    (!imap || of_device_compatible_match(ipar, of_irq_imap_abusers))) {
247 			pr_debug(" -> got it !\n");
248 			return 0;
249 		}
250 
251 		/*
252 		 * interrupt-map parsing does not work without a reg
253 		 * property when #address-cells != 0
254 		 */
255 		if (addrsize && !addr) {
256 			pr_debug(" -> no reg passed in when needed !\n");
257 			goto fail;
258 		}
259 
260 		/* No interrupt map, check for an interrupt parent */
261 		if (imap == NULL) {
262 			pr_debug(" -> no map, getting parent\n");
263 			newpar = of_irq_find_parent(ipar);
264 			goto skiplevel;
265 		}
266 		imaplen /= sizeof(u32);
267 
268 		/* Look for a mask */
269 		imask = of_get_property(ipar, "interrupt-map-mask", NULL);
270 		if (!imask)
271 			imask = dummy_imask;
272 
273 		/* Parse interrupt-map */
274 		match = 0;
275 		while (imaplen > (addrsize + intsize + 1)) {
276 			/* Compare specifiers */
277 			match = 1;
278 			for (i = 0; i < (addrsize + intsize); i++, imaplen--)
279 				match &= !((match_array[i] ^ *imap++) & imask[i]);
280 
281 			pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
282 
283 			oldimap = imap;
284 			imap = of_irq_parse_imap_parent(oldimap, imaplen, out_irq);
285 			if (!imap)
286 				goto fail;
287 
288 			match &= of_device_is_available(out_irq->np);
289 			if (match)
290 				break;
291 
292 			of_node_put(out_irq->np);
293 			imaplen -= imap - oldimap;
294 			pr_debug(" -> imaplen=%d\n", imaplen);
295 		}
296 		if (!match) {
297 			if (intc) {
298 				/*
299 				 * The PASEMI Nemo is a known offender, so
300 				 * let's only warn for anyone else.
301 				 */
302 				WARN(!IS_ENABLED(CONFIG_PPC_PASEMI),
303 				     "%pOF interrupt-map failed, using interrupt-controller\n",
304 				     ipar);
305 				return 0;
306 			}
307 
308 			goto fail;
309 		}
310 
311 		/*
312 		 * Successfully parsed an interrupt-map translation; copy new
313 		 * interrupt specifier into the out_irq structure
314 		 */
315 		match_array = oldimap + 1;
316 
317 		newpar = out_irq->np;
318 		intsize = out_irq->args_count;
319 		addrsize = (imap - match_array) - intsize;
320 
321 		if (ipar == newpar) {
322 			pr_debug("%pOF interrupt-map entry to self\n", ipar);
323 			return 0;
324 		}
325 
326 	skiplevel:
327 		/* Iterate again with new parent */
328 		pr_debug(" -> new parent: %pOF\n", newpar);
329 		of_node_put(ipar);
330 		ipar = newpar;
331 		newpar = NULL;
332 	}
333 	rc = -ENOENT; /* No interrupt-map found */
334 
335  fail:
336 	of_node_put(ipar);
337 
338 	return rc;
339 }
340 EXPORT_SYMBOL_GPL(of_irq_parse_raw);
341 
342 /**
343  * of_irq_parse_one - Resolve an interrupt for a device
344  * @device: the device whose interrupt is to be resolved
345  * @index: index of the interrupt to resolve
346  * @out_irq: structure of_phandle_args filled by this function
347  *
348  * This function resolves an interrupt for a node by walking the interrupt tree,
349  * finding which interrupt controller node it is attached to, and returning the
350  * interrupt specifier that can be used to retrieve a Linux IRQ number.
351  */
352 int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
353 {
354 	struct device_node *p;
355 	const __be32 *addr;
356 	u32 intsize;
357 	int i, res;
358 
359 	pr_debug("of_irq_parse_one: dev=%pOF, index=%d\n", device, index);
360 
361 	/* OldWorld mac stuff is "special", handle out of line */
362 	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
363 		return of_irq_parse_oldworld(device, index, out_irq);
364 
365 	/* Get the reg property (if any) */
366 	addr = of_get_property(device, "reg", NULL);
367 
368 	/* Try the new-style interrupts-extended first */
369 	res = of_parse_phandle_with_args(device, "interrupts-extended",
370 					"#interrupt-cells", index, out_irq);
371 	if (!res)
372 		return of_irq_parse_raw(addr, out_irq);
373 
374 	/* Look for the interrupt parent. */
375 	p = of_irq_find_parent(device);
376 	if (p == NULL)
377 		return -EINVAL;
378 
379 	/* Get size of interrupt specifier */
380 	if (of_property_read_u32(p, "#interrupt-cells", &intsize)) {
381 		res = -EINVAL;
382 		goto out;
383 	}
384 
385 	pr_debug(" parent=%pOF, intsize=%d\n", p, intsize);
386 
387 	/* Copy intspec into irq structure */
388 	out_irq->np = p;
389 	out_irq->args_count = intsize;
390 	for (i = 0; i < intsize; i++) {
391 		res = of_property_read_u32_index(device, "interrupts",
392 						 (index * intsize) + i,
393 						 out_irq->args + i);
394 		if (res)
395 			goto out;
396 	}
397 
398 	pr_debug(" intspec=%d\n", *out_irq->args);
399 
400 
401 	/* Check if there are any interrupt-map translations to process */
402 	res = of_irq_parse_raw(addr, out_irq);
403  out:
404 	of_node_put(p);
405 	return res;
406 }
407 EXPORT_SYMBOL_GPL(of_irq_parse_one);
408 
409 /**
410  * of_irq_to_resource - Decode a node's IRQ and return it as a resource
411  * @dev: pointer to device tree node
412  * @index: zero-based index of the irq
413  * @r: pointer to resource structure to return result into.
414  */
415 int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
416 {
417 	int irq = of_irq_get(dev, index);
418 
419 	if (irq < 0)
420 		return irq;
421 
422 	/* Only dereference the resource if both the
423 	 * resource and the irq are valid. */
424 	if (r && irq) {
425 		const char *name = NULL;
426 
427 		memset(r, 0, sizeof(*r));
428 		/*
429 		 * Get optional "interrupt-names" property to add a name
430 		 * to the resource.
431 		 */
432 		of_property_read_string_index(dev, "interrupt-names", index,
433 					      &name);
434 
435 		r->start = r->end = irq;
436 		r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
437 		r->name = name ? name : of_node_full_name(dev);
438 	}
439 
440 	return irq;
441 }
442 EXPORT_SYMBOL_GPL(of_irq_to_resource);
443 
444 /**
445  * of_irq_get - Decode a node's IRQ and return it as a Linux IRQ number
446  * @dev: pointer to device tree node
447  * @index: zero-based index of the IRQ
448  *
449  * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
450  * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
451  * of any other failure.
452  */
453 int of_irq_get(struct device_node *dev, int index)
454 {
455 	int rc;
456 	struct of_phandle_args oirq;
457 	struct irq_domain *domain;
458 
459 	rc = of_irq_parse_one(dev, index, &oirq);
460 	if (rc)
461 		return rc;
462 
463 	domain = irq_find_host(oirq.np);
464 	if (!domain) {
465 		rc = -EPROBE_DEFER;
466 		goto out;
467 	}
468 
469 	rc = irq_create_of_mapping(&oirq);
470 out:
471 	of_node_put(oirq.np);
472 
473 	return rc;
474 }
475 EXPORT_SYMBOL_GPL(of_irq_get);
476 
477 /**
478  * of_irq_get_byname - Decode a node's IRQ and return it as a Linux IRQ number
479  * @dev: pointer to device tree node
480  * @name: IRQ name
481  *
482  * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
483  * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
484  * of any other failure.
485  */
486 int of_irq_get_byname(struct device_node *dev, const char *name)
487 {
488 	int index;
489 
490 	if (unlikely(!name))
491 		return -EINVAL;
492 
493 	index = of_property_match_string(dev, "interrupt-names", name);
494 	if (index < 0)
495 		return index;
496 
497 	return of_irq_get(dev, index);
498 }
499 EXPORT_SYMBOL_GPL(of_irq_get_byname);
500 
501 /**
502  * of_irq_count - Count the number of IRQs a node uses
503  * @dev: pointer to device tree node
504  */
505 int of_irq_count(struct device_node *dev)
506 {
507 	struct of_phandle_args irq;
508 	int nr = 0;
509 
510 	while (of_irq_parse_one(dev, nr, &irq) == 0)
511 		nr++;
512 
513 	return nr;
514 }
515 
516 /**
517  * of_irq_to_resource_table - Fill in resource table with node's IRQ info
518  * @dev: pointer to device tree node
519  * @res: array of resources to fill in
520  * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
521  *
522  * Return: The size of the filled in table (up to @nr_irqs).
523  */
524 int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
525 		int nr_irqs)
526 {
527 	int i;
528 
529 	for (i = 0; i < nr_irqs; i++, res++)
530 		if (of_irq_to_resource(dev, i, res) <= 0)
531 			break;
532 
533 	return i;
534 }
535 EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
536 
537 struct of_intc_desc {
538 	struct list_head	list;
539 	of_irq_init_cb_t	irq_init_cb;
540 	struct device_node	*dev;
541 	struct device_node	*interrupt_parent;
542 };
543 
544 /**
545  * of_irq_init - Scan and init matching interrupt controllers in DT
546  * @matches: 0 terminated array of nodes to match and init function to call
547  *
548  * This function scans the device tree for matching interrupt controller nodes,
549  * and calls their initialization functions in order with parents first.
550  */
551 void __init of_irq_init(const struct of_device_id *matches)
552 {
553 	const struct of_device_id *match;
554 	struct device_node *np, *parent = NULL;
555 	struct of_intc_desc *desc, *temp_desc;
556 	struct list_head intc_desc_list, intc_parent_list;
557 
558 	INIT_LIST_HEAD(&intc_desc_list);
559 	INIT_LIST_HEAD(&intc_parent_list);
560 
561 	for_each_matching_node_and_match(np, matches, &match) {
562 		if (!of_property_read_bool(np, "interrupt-controller") ||
563 				!of_device_is_available(np))
564 			continue;
565 
566 		if (WARN(!match->data, "of_irq_init: no init function for %s\n",
567 			 match->compatible))
568 			continue;
569 
570 		/*
571 		 * Here, we allocate and populate an of_intc_desc with the node
572 		 * pointer, interrupt-parent device_node etc.
573 		 */
574 		desc = kzalloc(sizeof(*desc), GFP_KERNEL);
575 		if (!desc) {
576 			of_node_put(np);
577 			goto err;
578 		}
579 
580 		desc->irq_init_cb = match->data;
581 		desc->dev = of_node_get(np);
582 		/*
583 		 * interrupts-extended can reference multiple parent domains.
584 		 * Arbitrarily pick the first one; assume any other parents
585 		 * are the same distance away from the root irq controller.
586 		 */
587 		desc->interrupt_parent = of_parse_phandle(np, "interrupts-extended", 0);
588 		if (!desc->interrupt_parent)
589 			desc->interrupt_parent = of_irq_find_parent(np);
590 		if (desc->interrupt_parent == np) {
591 			of_node_put(desc->interrupt_parent);
592 			desc->interrupt_parent = NULL;
593 		}
594 		list_add_tail(&desc->list, &intc_desc_list);
595 	}
596 
597 	/*
598 	 * The root irq controller is the one without an interrupt-parent.
599 	 * That one goes first, followed by the controllers that reference it,
600 	 * followed by the ones that reference the 2nd level controllers, etc.
601 	 */
602 	while (!list_empty(&intc_desc_list)) {
603 		/*
604 		 * Process all controllers with the current 'parent'.
605 		 * First pass will be looking for NULL as the parent.
606 		 * The assumption is that NULL parent means a root controller.
607 		 */
608 		list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
609 			int ret;
610 
611 			if (desc->interrupt_parent != parent)
612 				continue;
613 
614 			list_del(&desc->list);
615 
616 			of_node_set_flag(desc->dev, OF_POPULATED);
617 
618 			pr_debug("of_irq_init: init %pOF (%p), parent %p\n",
619 				 desc->dev,
620 				 desc->dev, desc->interrupt_parent);
621 			ret = desc->irq_init_cb(desc->dev,
622 						desc->interrupt_parent);
623 			if (ret) {
624 				pr_err("%s: Failed to init %pOF (%p), parent %p\n",
625 				       __func__, desc->dev, desc->dev,
626 				       desc->interrupt_parent);
627 				of_node_clear_flag(desc->dev, OF_POPULATED);
628 				kfree(desc);
629 				continue;
630 			}
631 
632 			/*
633 			 * This one is now set up; add it to the parent list so
634 			 * its children can get processed in a subsequent pass.
635 			 */
636 			list_add_tail(&desc->list, &intc_parent_list);
637 		}
638 
639 		/* Get the next pending parent that might have children */
640 		desc = list_first_entry_or_null(&intc_parent_list,
641 						typeof(*desc), list);
642 		if (!desc) {
643 			pr_err("of_irq_init: children remain, but no parents\n");
644 			break;
645 		}
646 		list_del(&desc->list);
647 		parent = desc->dev;
648 		kfree(desc);
649 	}
650 
651 	list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
652 		list_del(&desc->list);
653 		kfree(desc);
654 	}
655 err:
656 	list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
657 		list_del(&desc->list);
658 		of_node_put(desc->dev);
659 		kfree(desc);
660 	}
661 }
662 
663 static u32 __of_msi_map_id(struct device *dev, struct device_node **np,
664 			    u32 id_in)
665 {
666 	struct device *parent_dev;
667 	u32 id_out = id_in;
668 
669 	/*
670 	 * Walk up the device parent links looking for one with a
671 	 * "msi-map" property.
672 	 */
673 	for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
674 		if (!of_map_id(parent_dev->of_node, id_in, "msi-map",
675 				"msi-map-mask", np, &id_out))
676 			break;
677 	return id_out;
678 }
679 
680 /**
681  * of_msi_map_id - Map a MSI ID for a device.
682  * @dev: device for which the mapping is to be done.
683  * @msi_np: device node of the expected msi controller.
684  * @id_in: unmapped MSI ID for the device.
685  *
686  * Walk up the device hierarchy looking for devices with a "msi-map"
687  * property.  If found, apply the mapping to @id_in.
688  *
689  * Return: The mapped MSI ID.
690  */
691 u32 of_msi_map_id(struct device *dev, struct device_node *msi_np, u32 id_in)
692 {
693 	return __of_msi_map_id(dev, &msi_np, id_in);
694 }
695 
696 /**
697  * of_msi_map_get_device_domain - Use msi-map to find the relevant MSI domain
698  * @dev: device for which the mapping is to be done.
699  * @id: Device ID.
700  * @bus_token: Bus token
701  *
702  * Walk up the device hierarchy looking for devices with a "msi-map"
703  * property.
704  *
705  * Returns: the MSI domain for this device (or NULL on failure)
706  */
707 struct irq_domain *of_msi_map_get_device_domain(struct device *dev, u32 id,
708 						u32 bus_token)
709 {
710 	struct device_node *np = NULL;
711 
712 	__of_msi_map_id(dev, &np, id);
713 	return irq_find_matching_host(np, bus_token);
714 }
715 
716 /**
717  * of_msi_get_domain - Use msi-parent to find the relevant MSI domain
718  * @dev: device for which the domain is requested
719  * @np: device node for @dev
720  * @token: bus type for this domain
721  *
722  * Parse the msi-parent property (both the simple and the complex
723  * versions), and returns the corresponding MSI domain.
724  *
725  * Returns: the MSI domain for this device (or NULL on failure).
726  */
727 struct irq_domain *of_msi_get_domain(struct device *dev,
728 				     struct device_node *np,
729 				     enum irq_domain_bus_token token)
730 {
731 	struct device_node *msi_np;
732 	struct irq_domain *d;
733 
734 	/* Check for a single msi-parent property */
735 	msi_np = of_parse_phandle(np, "msi-parent", 0);
736 	if (msi_np && !of_property_read_bool(msi_np, "#msi-cells")) {
737 		d = irq_find_matching_host(msi_np, token);
738 		if (!d)
739 			of_node_put(msi_np);
740 		return d;
741 	}
742 
743 	if (token == DOMAIN_BUS_PLATFORM_MSI) {
744 		/* Check for the complex msi-parent version */
745 		struct of_phandle_args args;
746 		int index = 0;
747 
748 		while (!of_parse_phandle_with_args(np, "msi-parent",
749 						   "#msi-cells",
750 						   index, &args)) {
751 			d = irq_find_matching_host(args.np, token);
752 			if (d)
753 				return d;
754 
755 			of_node_put(args.np);
756 			index++;
757 		}
758 	}
759 
760 	return NULL;
761 }
762 EXPORT_SYMBOL_GPL(of_msi_get_domain);
763 
764 /**
765  * of_msi_configure - Set the msi_domain field of a device
766  * @dev: device structure to associate with an MSI irq domain
767  * @np: device node for that device
768  */
769 void of_msi_configure(struct device *dev, struct device_node *np)
770 {
771 	dev_set_msi_domain(dev,
772 			   of_msi_get_domain(dev, np, DOMAIN_BUS_PLATFORM_MSI));
773 }
774 EXPORT_SYMBOL_GPL(of_msi_configure);
775