xref: /openbmc/linux/drivers/of/irq.c (revision eb2b4ecf)
1af6074fcSRob Herring // SPDX-License-Identifier: GPL-2.0+
2e3873444SGrant Likely /*
3e3873444SGrant Likely  *  Derived from arch/i386/kernel/irq.c
4e3873444SGrant Likely  *    Copyright (C) 1992 Linus Torvalds
5e3873444SGrant Likely  *  Adapted from arch/i386 by Gary Thomas
6e3873444SGrant Likely  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
7e3873444SGrant Likely  *  Updated and modified by Cort Dougan <cort@fsmlabs.com>
8e3873444SGrant Likely  *    Copyright (C) 1996-2001 Cort Dougan
9e3873444SGrant Likely  *  Adapted for Power Macintosh by Paul Mackerras
10e3873444SGrant Likely  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
11e3873444SGrant Likely  *
12e3873444SGrant Likely  * This file contains the code used to make IRQ descriptions in the
13e3873444SGrant Likely  * device tree to actual irq numbers on an interrupt controller
14e3873444SGrant Likely  * driver.
15e3873444SGrant Likely  */
16e3873444SGrant Likely 
17606ad42aSRob Herring #define pr_fmt(fmt)	"OF: " fmt
18606ad42aSRob Herring 
19c706c239SMarc Zyngier #include <linux/device.h>
20e3873444SGrant Likely #include <linux/errno.h>
21c71a54b0SRob Herring #include <linux/list.h>
22e3873444SGrant Likely #include <linux/module.h>
23e3873444SGrant Likely #include <linux/of.h>
24e3873444SGrant Likely #include <linux/of_irq.h>
25e3873444SGrant Likely #include <linux/string.h>
26c71a54b0SRob Herring #include <linux/slab.h>
27e3873444SGrant Likely 
28e3873444SGrant Likely /**
29e3873444SGrant Likely  * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
30d84ff46aSYijing Wang  * @dev: Device node of the device whose interrupt is to be mapped
31e3873444SGrant Likely  * @index: Index of the interrupt to map
32e3873444SGrant Likely  *
330c02c800SGrant Likely  * This function is a wrapper that chains of_irq_parse_one() and
34e3873444SGrant Likely  * irq_create_of_mapping() to make things easier to callers
35e3873444SGrant Likely  */
irq_of_parse_and_map(struct device_node * dev,int index)36e3873444SGrant Likely unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
37e3873444SGrant Likely {
38530210c7SGrant Likely 	struct of_phandle_args oirq;
39e3873444SGrant Likely 
400c02c800SGrant Likely 	if (of_irq_parse_one(dev, index, &oirq))
4177a7300aSAnton Vorontsov 		return 0;
42e3873444SGrant Likely 
43e6d30ab1SGrant Likely 	return irq_create_of_mapping(&oirq);
44e3873444SGrant Likely }
45e3873444SGrant Likely EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
467dc2e113SGrant Likely 
477dc2e113SGrant Likely /**
487dc2e113SGrant Likely  * of_irq_find_parent - Given a device node, find its interrupt parent node
497dc2e113SGrant Likely  * @child: pointer to device node
507dc2e113SGrant Likely  *
518c8239c2SRob Herring  * Return: A pointer to the interrupt parent node, or NULL if the interrupt
527dc2e113SGrant Likely  * parent could not be determined.
537dc2e113SGrant Likely  */
of_irq_find_parent(struct device_node * child)544c3141e0SCarlo Caione struct device_node *of_irq_find_parent(struct device_node *child)
557dc2e113SGrant Likely {
56b4bbb029SLinus Torvalds 	struct device_node *p;
57fa976ff7SSergei Shtylyov 	phandle parent;
587dc2e113SGrant Likely 
59b4bbb029SLinus Torvalds 	if (!of_node_get(child))
607dc2e113SGrant Likely 		return NULL;
617dc2e113SGrant Likely 
627dc2e113SGrant Likely 	do {
63fa976ff7SSergei Shtylyov 		if (of_property_read_u32(child, "interrupt-parent", &parent)) {
64b4bbb029SLinus Torvalds 			p = of_get_parent(child);
65fa976ff7SSergei Shtylyov 		} else	{
667dc2e113SGrant Likely 			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
677dc2e113SGrant Likely 				p = of_node_get(of_irq_dflt_pic);
687dc2e113SGrant Likely 			else
69fa976ff7SSergei Shtylyov 				p = of_find_node_by_phandle(parent);
707dc2e113SGrant Likely 		}
71b4bbb029SLinus Torvalds 		of_node_put(child);
72b4bbb029SLinus Torvalds 		child = p;
737dc2e113SGrant Likely 	} while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
747dc2e113SGrant Likely 
75b4bbb029SLinus Torvalds 	return p;
767dc2e113SGrant Likely }
774c3141e0SCarlo Caione EXPORT_SYMBOL_GPL(of_irq_find_parent);
787dc2e113SGrant Likely 
79de4adddcSMarc Zyngier /*
80de4adddcSMarc Zyngier  * These interrupt controllers abuse interrupt-map for unspeakable
81de4adddcSMarc Zyngier  * reasons and rely on the core code to *ignore* it (the drivers do
82de4adddcSMarc Zyngier  * their own parsing of the property).
83de4adddcSMarc Zyngier  *
84de4adddcSMarc Zyngier  * If you think of adding to the list for something *new*, think
85de4adddcSMarc Zyngier  * again. There is a high chance that you will be sent back to the
86de4adddcSMarc Zyngier  * drawing board.
87de4adddcSMarc Zyngier  */
88de4adddcSMarc Zyngier static const char * const of_irq_imap_abusers[] = {
89de4adddcSMarc Zyngier 	"CBEA,platform-spider-pic",
90de4adddcSMarc Zyngier 	"sti,platform-spider-pic",
91de4adddcSMarc Zyngier 	"realtek,rtl-intc",
92de4adddcSMarc Zyngier 	"fsl,ls1021a-extirq",
93de4adddcSMarc Zyngier 	"fsl,ls1043a-extirq",
94de4adddcSMarc Zyngier 	"fsl,ls1088a-extirq",
95de4adddcSMarc Zyngier 	"renesas,rza1-irqc",
96de4adddcSMarc Zyngier 	NULL,
97de4adddcSMarc Zyngier };
98de4adddcSMarc Zyngier 
997dc2e113SGrant Likely /**
1000c02c800SGrant Likely  * of_irq_parse_raw - Low level interrupt tree parsing
10123616132SGrant Likely  * @addr:	address specifier (start of "reg" property of the device) in be32 format
102fae3b9cdSVasyl Gomonovych  * @out_irq:	structure of_phandle_args updated by this function
1037dc2e113SGrant Likely  *
1047dc2e113SGrant Likely  * This function is a low-level interrupt tree walking function. It
1057dc2e113SGrant Likely  * can be used to do a partial walk with synthetized reg and interrupts
1067dc2e113SGrant Likely  * properties, for example when resolving PCI interrupts when no device
10723616132SGrant Likely  * node exist for the parent. It takes an interrupt specifier structure as
10823616132SGrant Likely  * input, walks the tree looking for any interrupt-map properties, translates
10923616132SGrant Likely  * the specifier for each map, and then returns the translated map.
1108c8239c2SRob Herring  *
1118c8239c2SRob Herring  * Return: 0 on success and a negative number on error
1127dc2e113SGrant Likely  */
of_irq_parse_raw(const __be32 * addr,struct of_phandle_args * out_irq)11323616132SGrant Likely int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
1147dc2e113SGrant Likely {
1157dc2e113SGrant Likely 	struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
116355e62f5SGrant Likely 	__be32 initial_match_array[MAX_PHANDLE_ARGS];
11723616132SGrant Likely 	const __be32 *match_array = initial_match_array;
11817a70355SRob Herring 	const __be32 *tmp, *imap, *imask, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) };
1197dc2e113SGrant Likely 	u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
120f1aa5484SGuilherme G. Piccoli 	int imaplen, match, i, rc = -EINVAL;
1217dc2e113SGrant Likely 
122624cfca5SGrant Likely #ifdef DEBUG
123624cfca5SGrant Likely 	of_print_phandle_args("of_irq_parse_raw: ", out_irq);
124624cfca5SGrant Likely #endif
1257dc2e113SGrant Likely 
12623616132SGrant Likely 	ipar = of_node_get(out_irq->np);
1277dc2e113SGrant Likely 
1287dc2e113SGrant Likely 	/* First get the #interrupt-cells property of the current cursor
1297dc2e113SGrant Likely 	 * that tells us how to interpret the passed-in intspec. If there
1307dc2e113SGrant Likely 	 * is none, we are nice and just walk up the tree
1317dc2e113SGrant Likely 	 */
1327dc2e113SGrant Likely 	do {
133fa976ff7SSergei Shtylyov 		if (!of_property_read_u32(ipar, "#interrupt-cells", &intsize))
1347dc2e113SGrant Likely 			break;
1357dc2e113SGrant Likely 		tnode = ipar;
1367dc2e113SGrant Likely 		ipar = of_irq_find_parent(ipar);
1377dc2e113SGrant Likely 		of_node_put(tnode);
1387dc2e113SGrant Likely 	} while (ipar);
1397dc2e113SGrant Likely 	if (ipar == NULL) {
1407dc2e113SGrant Likely 		pr_debug(" -> no parent found !\n");
1417dc2e113SGrant Likely 		goto fail;
1427dc2e113SGrant Likely 	}
1437dc2e113SGrant Likely 
1440d638a07SRob Herring 	pr_debug("of_irq_parse_raw: ipar=%pOF, size=%d\n", ipar, intsize);
1457dc2e113SGrant Likely 
14623616132SGrant Likely 	if (out_irq->args_count != intsize)
147f1aa5484SGuilherme G. Piccoli 		goto fail;
1487dc2e113SGrant Likely 
1497dc2e113SGrant Likely 	/* Look for this #address-cells. We have to implement the old linux
1507dc2e113SGrant Likely 	 * trick of looking for the parent here as some device-trees rely on it
1517dc2e113SGrant Likely 	 */
1527dc2e113SGrant Likely 	old = of_node_get(ipar);
1537dc2e113SGrant Likely 	do {
1547dc2e113SGrant Likely 		tmp = of_get_property(old, "#address-cells", NULL);
1557dc2e113SGrant Likely 		tnode = of_get_parent(old);
1567dc2e113SGrant Likely 		of_node_put(old);
1577dc2e113SGrant Likely 		old = tnode;
1587dc2e113SGrant Likely 	} while (old && tmp == NULL);
1597dc2e113SGrant Likely 	of_node_put(old);
1607dc2e113SGrant Likely 	old = NULL;
161a7c194b0SRob Herring 	addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
1627dc2e113SGrant Likely 
1637dc2e113SGrant Likely 	pr_debug(" -> addrsize=%d\n", addrsize);
1647dc2e113SGrant Likely 
165355e62f5SGrant Likely 	/* Range check so that the temporary buffer doesn't overflow */
166f1aa5484SGuilherme G. Piccoli 	if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) {
167f1aa5484SGuilherme G. Piccoli 		rc = -EFAULT;
168355e62f5SGrant Likely 		goto fail;
169f1aa5484SGuilherme G. Piccoli 	}
170355e62f5SGrant Likely 
17123616132SGrant Likely 	/* Precalculate the match array - this simplifies match loop */
17223616132SGrant Likely 	for (i = 0; i < addrsize; i++)
17378119fd1SGrant Likely 		initial_match_array[i] = addr ? addr[i] : 0;
17423616132SGrant Likely 	for (i = 0; i < intsize; i++)
17523616132SGrant Likely 		initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);
17623616132SGrant Likely 
1777dc2e113SGrant Likely 	/* Now start the actual "proper" walk of the interrupt tree */
1787dc2e113SGrant Likely 	while (ipar != NULL) {
17904128418SMarc Zyngier 		/*
18004128418SMarc Zyngier 		 * Now check if cursor is an interrupt-controller and
18104128418SMarc Zyngier 		 * if it is then we are done, unless there is an
182de4adddcSMarc Zyngier 		 * interrupt-map which takes precedence except on one
183de4adddcSMarc Zyngier 		 * of these broken platforms that want to parse
184de4adddcSMarc Zyngier 		 * interrupt-map themselves for $reason.
1857dc2e113SGrant Likely 		 */
18610a20b34SMarc Zyngier 		bool intc = of_property_read_bool(ipar, "interrupt-controller");
18710a20b34SMarc Zyngier 
18804128418SMarc Zyngier 		imap = of_get_property(ipar, "interrupt-map", &imaplen);
189de4adddcSMarc Zyngier 		if (intc &&
190de4adddcSMarc Zyngier 		    (!imap || of_device_compatible_match(ipar, of_irq_imap_abusers))) {
1917dc2e113SGrant Likely 			pr_debug(" -> got it !\n");
1927dc2e113SGrant Likely 			return 0;
1937dc2e113SGrant Likely 		}
1947dc2e113SGrant Likely 
19578119fd1SGrant Likely 		/*
19678119fd1SGrant Likely 		 * interrupt-map parsing does not work without a reg
19778119fd1SGrant Likely 		 * property when #address-cells != 0
19878119fd1SGrant Likely 		 */
19978119fd1SGrant Likely 		if (addrsize && !addr) {
20078119fd1SGrant Likely 			pr_debug(" -> no reg passed in when needed !\n");
20178119fd1SGrant Likely 			goto fail;
20278119fd1SGrant Likely 		}
20378119fd1SGrant Likely 
2047dc2e113SGrant Likely 		/* No interrupt map, check for an interrupt parent */
2057dc2e113SGrant Likely 		if (imap == NULL) {
2067dc2e113SGrant Likely 			pr_debug(" -> no map, getting parent\n");
2077dc2e113SGrant Likely 			newpar = of_irq_find_parent(ipar);
2087dc2e113SGrant Likely 			goto skiplevel;
2097dc2e113SGrant Likely 		}
2107dc2e113SGrant Likely 		imaplen /= sizeof(u32);
2117dc2e113SGrant Likely 
2127dc2e113SGrant Likely 		/* Look for a mask */
2137dc2e113SGrant Likely 		imask = of_get_property(ipar, "interrupt-map-mask", NULL);
21423616132SGrant Likely 		if (!imask)
21523616132SGrant Likely 			imask = dummy_imask;
2167dc2e113SGrant Likely 
2177dc2e113SGrant Likely 		/* Parse interrupt-map */
2187dc2e113SGrant Likely 		match = 0;
2197dc2e113SGrant Likely 		while (imaplen > (addrsize + intsize + 1) && !match) {
2207dc2e113SGrant Likely 			/* Compare specifiers */
2217dc2e113SGrant Likely 			match = 1;
22223616132SGrant Likely 			for (i = 0; i < (addrsize + intsize); i++, imaplen--)
22374dac2edSTomasz Figa 				match &= !((match_array[i] ^ *imap++) & imask[i]);
2247dc2e113SGrant Likely 
2257dc2e113SGrant Likely 			pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
2267dc2e113SGrant Likely 
2277dc2e113SGrant Likely 			/* Get the interrupt parent */
2287dc2e113SGrant Likely 			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
2297dc2e113SGrant Likely 				newpar = of_node_get(of_irq_dflt_pic);
2307dc2e113SGrant Likely 			else
2319a6b2e58SGrant Likely 				newpar = of_find_node_by_phandle(be32_to_cpup(imap));
2327dc2e113SGrant Likely 			imap++;
2337dc2e113SGrant Likely 			--imaplen;
2347dc2e113SGrant Likely 
2357dc2e113SGrant Likely 			/* Check if not found */
2367dc2e113SGrant Likely 			if (newpar == NULL) {
2377dc2e113SGrant Likely 				pr_debug(" -> imap parent not found !\n");
2387dc2e113SGrant Likely 				goto fail;
2397dc2e113SGrant Likely 			}
2407dc2e113SGrant Likely 
2411ca56e7dSPeter Crosthwaite 			if (!of_device_is_available(newpar))
2421ca56e7dSPeter Crosthwaite 				match = 0;
2431ca56e7dSPeter Crosthwaite 
2447dc2e113SGrant Likely 			/* Get #interrupt-cells and #address-cells of new
2457dc2e113SGrant Likely 			 * parent
2467dc2e113SGrant Likely 			 */
247fa976ff7SSergei Shtylyov 			if (of_property_read_u32(newpar, "#interrupt-cells",
248fa976ff7SSergei Shtylyov 						 &newintsize)) {
2497dc2e113SGrant Likely 				pr_debug(" -> parent lacks #interrupt-cells!\n");
2507dc2e113SGrant Likely 				goto fail;
2517dc2e113SGrant Likely 			}
252fa976ff7SSergei Shtylyov 			if (of_property_read_u32(newpar, "#address-cells",
253fa976ff7SSergei Shtylyov 						 &newaddrsize))
254fa976ff7SSergei Shtylyov 				newaddrsize = 0;
2557dc2e113SGrant Likely 
2567dc2e113SGrant Likely 			pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
2577dc2e113SGrant Likely 			    newintsize, newaddrsize);
2587dc2e113SGrant Likely 
2597dc2e113SGrant Likely 			/* Check for malformed properties */
260f1aa5484SGuilherme G. Piccoli 			if (WARN_ON(newaddrsize + newintsize > MAX_PHANDLE_ARGS)
261f1aa5484SGuilherme G. Piccoli 			    || (imaplen < (newaddrsize + newintsize))) {
262f1aa5484SGuilherme G. Piccoli 				rc = -EFAULT;
263355e62f5SGrant Likely 				goto fail;
264f1aa5484SGuilherme G. Piccoli 			}
2657dc2e113SGrant Likely 
2667dc2e113SGrant Likely 			imap += newaddrsize + newintsize;
2677dc2e113SGrant Likely 			imaplen -= newaddrsize + newintsize;
2687dc2e113SGrant Likely 
2697dc2e113SGrant Likely 			pr_debug(" -> imaplen=%d\n", imaplen);
2707dc2e113SGrant Likely 		}
27110a20b34SMarc Zyngier 		if (!match) {
27210a20b34SMarc Zyngier 			if (intc) {
27310a20b34SMarc Zyngier 				/*
27410a20b34SMarc Zyngier 				 * The PASEMI Nemo is a known offender, so
27510a20b34SMarc Zyngier 				 * let's only warn for anyone else.
27610a20b34SMarc Zyngier 				 */
27710a20b34SMarc Zyngier 				WARN(!IS_ENABLED(CONFIG_PPC_PASEMI),
27810a20b34SMarc Zyngier 				     "%pOF interrupt-map failed, using interrupt-controller\n",
27910a20b34SMarc Zyngier 				     ipar);
28010a20b34SMarc Zyngier 				return 0;
28110a20b34SMarc Zyngier 			}
28210a20b34SMarc Zyngier 
2837dc2e113SGrant Likely 			goto fail;
28410a20b34SMarc Zyngier 		}
2857dc2e113SGrant Likely 
28623616132SGrant Likely 		/*
287d036d915SJulia Lawall 		 * Successfully parsed an interrupt-map translation; copy new
28823616132SGrant Likely 		 * interrupt specifier into the out_irq structure
28923616132SGrant Likely 		 */
29023616132SGrant Likely 		match_array = imap - newaddrsize - newintsize;
29123616132SGrant Likely 		for (i = 0; i < newintsize; i++)
29223616132SGrant Likely 			out_irq->args[i] = be32_to_cpup(imap - newintsize + i);
29323616132SGrant Likely 		out_irq->args_count = intsize = newintsize;
2947dc2e113SGrant Likely 		addrsize = newaddrsize;
2957dc2e113SGrant Likely 
29604128418SMarc Zyngier 		if (ipar == newpar) {
29704128418SMarc Zyngier 			pr_debug("%pOF interrupt-map entry to self\n", ipar);
29804128418SMarc Zyngier 			return 0;
29904128418SMarc Zyngier 		}
30004128418SMarc Zyngier 
3017dc2e113SGrant Likely 	skiplevel:
3027dc2e113SGrant Likely 		/* Iterate again with new parent */
303d23b2516SJeremy Linton 		out_irq->np = newpar;
3040d638a07SRob Herring 		pr_debug(" -> new parent: %pOF\n", newpar);
3057dc2e113SGrant Likely 		of_node_put(ipar);
3067dc2e113SGrant Likely 		ipar = newpar;
3077dc2e113SGrant Likely 		newpar = NULL;
3087dc2e113SGrant Likely 	}
309f1aa5484SGuilherme G. Piccoli 	rc = -ENOENT; /* No interrupt-map found */
310f1aa5484SGuilherme G. Piccoli 
3117dc2e113SGrant Likely  fail:
3127dc2e113SGrant Likely 	of_node_put(ipar);
3137dc2e113SGrant Likely 	of_node_put(newpar);
3147dc2e113SGrant Likely 
315f1aa5484SGuilherme G. Piccoli 	return rc;
3167dc2e113SGrant Likely }
3170c02c800SGrant Likely EXPORT_SYMBOL_GPL(of_irq_parse_raw);
3187dc2e113SGrant Likely 
3197dc2e113SGrant Likely /**
3200c02c800SGrant Likely  * of_irq_parse_one - Resolve an interrupt for a device
3217dc2e113SGrant Likely  * @device: the device whose interrupt is to be resolved
3227dc2e113SGrant Likely  * @index: index of the interrupt to resolve
32383f82d7aSLubomir Rintel  * @out_irq: structure of_phandle_args filled by this function
3247dc2e113SGrant Likely  *
32523616132SGrant Likely  * This function resolves an interrupt for a node by walking the interrupt tree,
32623616132SGrant Likely  * finding which interrupt controller node it is attached to, and returning the
32723616132SGrant Likely  * interrupt specifier that can be used to retrieve a Linux IRQ number.
3287dc2e113SGrant Likely  */
of_irq_parse_one(struct device_node * device,int index,struct of_phandle_args * out_irq)329530210c7SGrant Likely int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
3307dc2e113SGrant Likely {
3317dc2e113SGrant Likely 	struct device_node *p;
332b47fe22dSRob Herring 	const __be32 *addr;
333b47fe22dSRob Herring 	u32 intsize;
334d7c14605SLaurent Pinchart 	int i, res;
3357dc2e113SGrant Likely 
3360d638a07SRob Herring 	pr_debug("of_irq_parse_one: dev=%pOF, index=%d\n", device, index);
3377dc2e113SGrant Likely 
3387dc2e113SGrant Likely 	/* OldWorld mac stuff is "special", handle out of line */
3397dc2e113SGrant Likely 	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
3400c02c800SGrant Likely 		return of_irq_parse_oldworld(device, index, out_irq);
3417dc2e113SGrant Likely 
34279d97015SGrant Likely 	/* Get the reg property (if any) */
34379d97015SGrant Likely 	addr = of_get_property(device, "reg", NULL);
34479d97015SGrant Likely 
345a9ecdc0fSFlorian Fainelli 	/* Try the new-style interrupts-extended first */
34679d97015SGrant Likely 	res = of_parse_phandle_with_args(device, "interrupts-extended",
34779d97015SGrant Likely 					"#interrupt-cells", index, out_irq);
348a9ecdc0fSFlorian Fainelli 	if (!res)
34979d97015SGrant Likely 		return of_irq_parse_raw(addr, out_irq);
350a9ecdc0fSFlorian Fainelli 
3517dc2e113SGrant Likely 	/* Look for the interrupt parent. */
3527dc2e113SGrant Likely 	p = of_irq_find_parent(device);
3537dc2e113SGrant Likely 	if (p == NULL)
3547dc2e113SGrant Likely 		return -EINVAL;
3557dc2e113SGrant Likely 
3567dc2e113SGrant Likely 	/* Get size of interrupt specifier */
357fa976ff7SSergei Shtylyov 	if (of_property_read_u32(p, "#interrupt-cells", &intsize)) {
358d7c14605SLaurent Pinchart 		res = -EINVAL;
3597dc2e113SGrant Likely 		goto out;
360d7c14605SLaurent Pinchart 	}
3617dc2e113SGrant Likely 
362b47fe22dSRob Herring 	pr_debug(" parent=%pOF, intsize=%d\n", p, intsize);
3637dc2e113SGrant Likely 
36423616132SGrant Likely 	/* Copy intspec into irq structure */
36523616132SGrant Likely 	out_irq->np = p;
36623616132SGrant Likely 	out_irq->args_count = intsize;
367b47fe22dSRob Herring 	for (i = 0; i < intsize; i++) {
368b47fe22dSRob Herring 		res = of_property_read_u32_index(device, "interrupts",
369b47fe22dSRob Herring 						 (index * intsize) + i,
370b47fe22dSRob Herring 						 out_irq->args + i);
371b47fe22dSRob Herring 		if (res)
37223616132SGrant Likely 			goto out;
37323616132SGrant Likely 	}
37423616132SGrant Likely 
375b47fe22dSRob Herring 	pr_debug(" intspec=%d\n", *out_irq->args);
376b47fe22dSRob Herring 
37723616132SGrant Likely 
37823616132SGrant Likely 	/* Check if there are any interrupt-map translations to process */
37923616132SGrant Likely 	res = of_irq_parse_raw(addr, out_irq);
3807dc2e113SGrant Likely  out:
3817dc2e113SGrant Likely 	of_node_put(p);
3827dc2e113SGrant Likely 	return res;
3837dc2e113SGrant Likely }
3840c02c800SGrant Likely EXPORT_SYMBOL_GPL(of_irq_parse_one);
3857dc2e113SGrant Likely 
3867dc2e113SGrant Likely /**
3877dc2e113SGrant Likely  * of_irq_to_resource - Decode a node's IRQ and return it as a resource
3887dc2e113SGrant Likely  * @dev: pointer to device tree node
3897dc2e113SGrant Likely  * @index: zero-based index of the irq
3907dc2e113SGrant Likely  * @r: pointer to resource structure to return result into.
3917dc2e113SGrant Likely  */
of_irq_to_resource(struct device_node * dev,int index,struct resource * r)3927dc2e113SGrant Likely int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
3937dc2e113SGrant Likely {
3947a4228bbSThomas Petazzoni 	int irq = of_irq_get(dev, index);
3957a4228bbSThomas Petazzoni 
3967a4228bbSThomas Petazzoni 	if (irq < 0)
3977a4228bbSThomas Petazzoni 		return irq;
3987dc2e113SGrant Likely 
3997dc2e113SGrant Likely 	/* Only dereference the resource if both the
4007dc2e113SGrant Likely 	 * resource and the irq are valid. */
40177a7300aSAnton Vorontsov 	if (r && irq) {
402661db794SBenoit Cousson 		const char *name = NULL;
403661db794SBenoit Cousson 
404cf9e2368SSebastian Andrzej Siewior 		memset(r, 0, sizeof(*r));
405661db794SBenoit Cousson 		/*
406ae107d06SGeert Uytterhoeven 		 * Get optional "interrupt-names" property to add a name
407661db794SBenoit Cousson 		 * to the resource.
408661db794SBenoit Cousson 		 */
409661db794SBenoit Cousson 		of_property_read_string_index(dev, "interrupt-names", index,
410661db794SBenoit Cousson 					      &name);
411661db794SBenoit Cousson 
4127dc2e113SGrant Likely 		r->start = r->end = irq;
4134a43d686STomasz Figa 		r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
4148804827bSGrant Likely 		r->name = name ? name : of_node_full_name(dev);
4157dc2e113SGrant Likely 	}
4167dc2e113SGrant Likely 
4177dc2e113SGrant Likely 	return irq;
4187dc2e113SGrant Likely }
4197dc2e113SGrant Likely EXPORT_SYMBOL_GPL(of_irq_to_resource);
42052f6537cSAndres Salomon 
42152f6537cSAndres Salomon /**
42239935466SSergei Shtylyov  * of_irq_get - Decode a node's IRQ and return it as a Linux IRQ number
4239ec36cafSRob Herring  * @dev: pointer to device tree node
42439935466SSergei Shtylyov  * @index: zero-based index of the IRQ
4259ec36cafSRob Herring  *
4268c8239c2SRob Herring  * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
42739935466SSergei Shtylyov  * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
42839935466SSergei Shtylyov  * of any other failure.
4299ec36cafSRob Herring  */
of_irq_get(struct device_node * dev,int index)4309ec36cafSRob Herring int of_irq_get(struct device_node *dev, int index)
4319ec36cafSRob Herring {
4329ec36cafSRob Herring 	int rc;
4339ec36cafSRob Herring 	struct of_phandle_args oirq;
4349ec36cafSRob Herring 	struct irq_domain *domain;
4359ec36cafSRob Herring 
4369ec36cafSRob Herring 	rc = of_irq_parse_one(dev, index, &oirq);
4379ec36cafSRob Herring 	if (rc)
4389ec36cafSRob Herring 		return rc;
4399ec36cafSRob Herring 
4409ec36cafSRob Herring 	domain = irq_find_host(oirq.np);
441*eb2b4ecfSClément Léger 	if (!domain) {
442*eb2b4ecfSClément Léger 		rc = -EPROBE_DEFER;
443*eb2b4ecfSClément Léger 		goto out;
444*eb2b4ecfSClément Léger 	}
4459ec36cafSRob Herring 
446*eb2b4ecfSClément Léger 	rc = irq_create_of_mapping(&oirq);
447*eb2b4ecfSClément Léger out:
448*eb2b4ecfSClément Léger 	of_node_put(oirq.np);
449*eb2b4ecfSClément Léger 
450*eb2b4ecfSClément Léger 	return rc;
4519ec36cafSRob Herring }
4529eb08fb3SLaurent Pinchart EXPORT_SYMBOL_GPL(of_irq_get);
4539ec36cafSRob Herring 
4549ec36cafSRob Herring /**
45539935466SSergei Shtylyov  * of_irq_get_byname - Decode a node's IRQ and return it as a Linux IRQ number
456ad69674eSGrygorii Strashko  * @dev: pointer to device tree node
45739935466SSergei Shtylyov  * @name: IRQ name
458ad69674eSGrygorii Strashko  *
4598c8239c2SRob Herring  * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
46039935466SSergei Shtylyov  * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
46139935466SSergei Shtylyov  * of any other failure.
462ad69674eSGrygorii Strashko  */
of_irq_get_byname(struct device_node * dev,const char * name)463ad69674eSGrygorii Strashko int of_irq_get_byname(struct device_node *dev, const char *name)
464ad69674eSGrygorii Strashko {
465ad69674eSGrygorii Strashko 	int index;
466ad69674eSGrygorii Strashko 
467ad69674eSGrygorii Strashko 	if (unlikely(!name))
468ad69674eSGrygorii Strashko 		return -EINVAL;
469ad69674eSGrygorii Strashko 
470ad69674eSGrygorii Strashko 	index = of_property_match_string(dev, "interrupt-names", name);
471ad69674eSGrygorii Strashko 	if (index < 0)
472ad69674eSGrygorii Strashko 		return index;
473ad69674eSGrygorii Strashko 
474ad69674eSGrygorii Strashko 	return of_irq_get(dev, index);
475ad69674eSGrygorii Strashko }
4766602c452SDmitry Torokhov EXPORT_SYMBOL_GPL(of_irq_get_byname);
477ad69674eSGrygorii Strashko 
478ad69674eSGrygorii Strashko /**
47952f6537cSAndres Salomon  * of_irq_count - Count the number of IRQs a node uses
48052f6537cSAndres Salomon  * @dev: pointer to device tree node
48152f6537cSAndres Salomon  */
of_irq_count(struct device_node * dev)48252f6537cSAndres Salomon int of_irq_count(struct device_node *dev)
48352f6537cSAndres Salomon {
4843da52787SThierry Reding 	struct of_phandle_args irq;
48552f6537cSAndres Salomon 	int nr = 0;
48652f6537cSAndres Salomon 
4873da52787SThierry Reding 	while (of_irq_parse_one(dev, nr, &irq) == 0)
48852f6537cSAndres Salomon 		nr++;
48952f6537cSAndres Salomon 
49052f6537cSAndres Salomon 	return nr;
49152f6537cSAndres Salomon }
49252f6537cSAndres Salomon 
49352f6537cSAndres Salomon /**
49452f6537cSAndres Salomon  * of_irq_to_resource_table - Fill in resource table with node's IRQ info
49552f6537cSAndres Salomon  * @dev: pointer to device tree node
49652f6537cSAndres Salomon  * @res: array of resources to fill in
49752f6537cSAndres Salomon  * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
49852f6537cSAndres Salomon  *
4998c8239c2SRob Herring  * Return: The size of the filled in table (up to @nr_irqs).
50052f6537cSAndres Salomon  */
of_irq_to_resource_table(struct device_node * dev,struct resource * res,int nr_irqs)50152f6537cSAndres Salomon int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
50252f6537cSAndres Salomon 		int nr_irqs)
50352f6537cSAndres Salomon {
50452f6537cSAndres Salomon 	int i;
50552f6537cSAndres Salomon 
50652f6537cSAndres Salomon 	for (i = 0; i < nr_irqs; i++, res++)
507531da740SSergei Shtylyov 		if (of_irq_to_resource(dev, i, res) <= 0)
50852f6537cSAndres Salomon 			break;
50952f6537cSAndres Salomon 
51052f6537cSAndres Salomon 	return i;
51152f6537cSAndres Salomon }
512a4f8bf22SJohn Crispin EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
513c71a54b0SRob Herring 
51448a9b733SGeert Uytterhoeven struct of_intc_desc {
515c71a54b0SRob Herring 	struct list_head	list;
516264041e3SMasahiro Yamada 	of_irq_init_cb_t	irq_init_cb;
517c71a54b0SRob Herring 	struct device_node	*dev;
518c71a54b0SRob Herring 	struct device_node	*interrupt_parent;
519c71a54b0SRob Herring };
520c71a54b0SRob Herring 
521c71a54b0SRob Herring /**
522c71a54b0SRob Herring  * of_irq_init - Scan and init matching interrupt controllers in DT
523c71a54b0SRob Herring  * @matches: 0 terminated array of nodes to match and init function to call
524c71a54b0SRob Herring  *
525c71a54b0SRob Herring  * This function scans the device tree for matching interrupt controller nodes,
526c71a54b0SRob Herring  * and calls their initialization functions in order with parents first.
527c71a54b0SRob Herring  */
of_irq_init(const struct of_device_id * matches)528c71a54b0SRob Herring void __init of_irq_init(const struct of_device_id *matches)
529c71a54b0SRob Herring {
530264041e3SMasahiro Yamada 	const struct of_device_id *match;
531c71a54b0SRob Herring 	struct device_node *np, *parent = NULL;
53248a9b733SGeert Uytterhoeven 	struct of_intc_desc *desc, *temp_desc;
533c71a54b0SRob Herring 	struct list_head intc_desc_list, intc_parent_list;
534c71a54b0SRob Herring 
535c71a54b0SRob Herring 	INIT_LIST_HEAD(&intc_desc_list);
536c71a54b0SRob Herring 	INIT_LIST_HEAD(&intc_parent_list);
537c71a54b0SRob Herring 
538264041e3SMasahiro Yamada 	for_each_matching_node_and_match(np, matches, &match) {
5396a245d95SSergei Shtylyov 		if (!of_property_read_bool(np, "interrupt-controller") ||
540bf49be02SPeter Crosthwaite 				!of_device_is_available(np))
541c71a54b0SRob Herring 			continue;
542264041e3SMasahiro Yamada 
543264041e3SMasahiro Yamada 		if (WARN(!match->data, "of_irq_init: no init function for %s\n",
544264041e3SMasahiro Yamada 			 match->compatible))
545264041e3SMasahiro Yamada 			continue;
546264041e3SMasahiro Yamada 
547c71a54b0SRob Herring 		/*
54848a9b733SGeert Uytterhoeven 		 * Here, we allocate and populate an of_intc_desc with the node
549c71a54b0SRob Herring 		 * pointer, interrupt-parent device_node etc.
550c71a54b0SRob Herring 		 */
551c71a54b0SRob Herring 		desc = kzalloc(sizeof(*desc), GFP_KERNEL);
5526f7dc9a3SGeert Uytterhoeven 		if (!desc) {
5538363ccb9SJulia Lawall 			of_node_put(np);
554c71a54b0SRob Herring 			goto err;
5558363ccb9SJulia Lawall 		}
556c71a54b0SRob Herring 
557264041e3SMasahiro Yamada 		desc->irq_init_cb = match->data;
5588363ccb9SJulia Lawall 		desc->dev = of_node_get(np);
559e9103362SSamuel Holland 		/*
560e9103362SSamuel Holland 		 * interrupts-extended can reference multiple parent domains.
561e9103362SSamuel Holland 		 * Arbitrarily pick the first one; assume any other parents
562e9103362SSamuel Holland 		 * are the same distance away from the root irq controller.
563e9103362SSamuel Holland 		 */
564e9103362SSamuel Holland 		desc->interrupt_parent = of_parse_phandle(np, "interrupts-extended", 0);
565e9103362SSamuel Holland 		if (!desc->interrupt_parent)
566c71a54b0SRob Herring 			desc->interrupt_parent = of_irq_find_parent(np);
567e9103362SSamuel Holland 		if (desc->interrupt_parent == np) {
568e9103362SSamuel Holland 			of_node_put(desc->interrupt_parent);
569d7fb6d0aSRob Herring 			desc->interrupt_parent = NULL;
570e9103362SSamuel Holland 		}
571c71a54b0SRob Herring 		list_add_tail(&desc->list, &intc_desc_list);
572c71a54b0SRob Herring 	}
573c71a54b0SRob Herring 
574c71a54b0SRob Herring 	/*
575c71a54b0SRob Herring 	 * The root irq controller is the one without an interrupt-parent.
576c71a54b0SRob Herring 	 * That one goes first, followed by the controllers that reference it,
577c71a54b0SRob Herring 	 * followed by the ones that reference the 2nd level controllers, etc.
578c71a54b0SRob Herring 	 */
579c71a54b0SRob Herring 	while (!list_empty(&intc_desc_list)) {
580c71a54b0SRob Herring 		/*
581c71a54b0SRob Herring 		 * Process all controllers with the current 'parent'.
582c71a54b0SRob Herring 		 * First pass will be looking for NULL as the parent.
583c71a54b0SRob Herring 		 * The assumption is that NULL parent means a root controller.
584c71a54b0SRob Herring 		 */
585c71a54b0SRob Herring 		list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
586c71a54b0SRob Herring 			int ret;
587c71a54b0SRob Herring 
588c71a54b0SRob Herring 			if (desc->interrupt_parent != parent)
589c71a54b0SRob Herring 				continue;
590c71a54b0SRob Herring 
591c71a54b0SRob Herring 			list_del(&desc->list);
592c71a54b0SRob Herring 
593e55aeb6bSPhilipp Zabel 			of_node_set_flag(desc->dev, OF_POPULATED);
594e55aeb6bSPhilipp Zabel 
5950d638a07SRob Herring 			pr_debug("of_irq_init: init %pOF (%p), parent %p\n",
5960d638a07SRob Herring 				 desc->dev,
597c71a54b0SRob Herring 				 desc->dev, desc->interrupt_parent);
598264041e3SMasahiro Yamada 			ret = desc->irq_init_cb(desc->dev,
599264041e3SMasahiro Yamada 						desc->interrupt_parent);
600c71a54b0SRob Herring 			if (ret) {
60127244cbdSAlexander Sverdlin 				pr_err("%s: Failed to init %pOF (%p), parent %p\n",
60227244cbdSAlexander Sverdlin 				       __func__, desc->dev, desc->dev,
60327244cbdSAlexander Sverdlin 				       desc->interrupt_parent);
604e55aeb6bSPhilipp Zabel 				of_node_clear_flag(desc->dev, OF_POPULATED);
605c71a54b0SRob Herring 				kfree(desc);
606c71a54b0SRob Herring 				continue;
607c71a54b0SRob Herring 			}
608c71a54b0SRob Herring 
609c71a54b0SRob Herring 			/*
610c71a54b0SRob Herring 			 * This one is now set up; add it to the parent list so
611c71a54b0SRob Herring 			 * its children can get processed in a subsequent pass.
612c71a54b0SRob Herring 			 */
613c71a54b0SRob Herring 			list_add_tail(&desc->list, &intc_parent_list);
614c71a54b0SRob Herring 		}
615c71a54b0SRob Herring 
616c71a54b0SRob Herring 		/* Get the next pending parent that might have children */
617c0cdfaa0SAxel Lin 		desc = list_first_entry_or_null(&intc_parent_list,
618c0cdfaa0SAxel Lin 						typeof(*desc), list);
619c0cdfaa0SAxel Lin 		if (!desc) {
620c71a54b0SRob Herring 			pr_err("of_irq_init: children remain, but no parents\n");
621c71a54b0SRob Herring 			break;
622c71a54b0SRob Herring 		}
623c71a54b0SRob Herring 		list_del(&desc->list);
624c71a54b0SRob Herring 		parent = desc->dev;
625c71a54b0SRob Herring 		kfree(desc);
626c71a54b0SRob Herring 	}
627c71a54b0SRob Herring 
628c71a54b0SRob Herring 	list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
629c71a54b0SRob Herring 		list_del(&desc->list);
630c71a54b0SRob Herring 		kfree(desc);
631c71a54b0SRob Herring 	}
632c71a54b0SRob Herring err:
633c71a54b0SRob Herring 	list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
634c71a54b0SRob Herring 		list_del(&desc->list);
6358363ccb9SJulia Lawall 		of_node_put(desc->dev);
636c71a54b0SRob Herring 		kfree(desc);
637c71a54b0SRob Herring 	}
638c71a54b0SRob Herring }
639c706c239SMarc Zyngier 
__of_msi_map_id(struct device * dev,struct device_node ** np,u32 id_in)6402bcdd8f2SLorenzo Pieralisi static u32 __of_msi_map_id(struct device *dev, struct device_node **np,
6412bcdd8f2SLorenzo Pieralisi 			    u32 id_in)
6428db02d8bSDavid Daney {
6438db02d8bSDavid Daney 	struct device *parent_dev;
6442bcdd8f2SLorenzo Pieralisi 	u32 id_out = id_in;
6458db02d8bSDavid Daney 
6468db02d8bSDavid Daney 	/*
6478db02d8bSDavid Daney 	 * Walk up the device parent links looking for one with a
6488db02d8bSDavid Daney 	 * "msi-map" property.
6498db02d8bSDavid Daney 	 */
650987068fcSRobin Murphy 	for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
6512bcdd8f2SLorenzo Pieralisi 		if (!of_map_id(parent_dev->of_node, id_in, "msi-map",
6522bcdd8f2SLorenzo Pieralisi 				"msi-map-mask", np, &id_out))
6538db02d8bSDavid Daney 			break;
6542bcdd8f2SLorenzo Pieralisi 	return id_out;
6558db02d8bSDavid Daney }
65648ae34fbSMarc Zyngier 
657a251b263SMarc Zyngier /**
6582bcdd8f2SLorenzo Pieralisi  * of_msi_map_id - Map a MSI ID for a device.
659a251b263SMarc Zyngier  * @dev: device for which the mapping is to be done.
660a251b263SMarc Zyngier  * @msi_np: device node of the expected msi controller.
6612bcdd8f2SLorenzo Pieralisi  * @id_in: unmapped MSI ID for the device.
662a251b263SMarc Zyngier  *
663a251b263SMarc Zyngier  * Walk up the device hierarchy looking for devices with a "msi-map"
6642bcdd8f2SLorenzo Pieralisi  * property.  If found, apply the mapping to @id_in.
665a251b263SMarc Zyngier  *
6668c8239c2SRob Herring  * Return: The mapped MSI ID.
667a251b263SMarc Zyngier  */
of_msi_map_id(struct device * dev,struct device_node * msi_np,u32 id_in)6682bcdd8f2SLorenzo Pieralisi u32 of_msi_map_id(struct device *dev, struct device_node *msi_np, u32 id_in)
669a251b263SMarc Zyngier {
6702bcdd8f2SLorenzo Pieralisi 	return __of_msi_map_id(dev, &msi_np, id_in);
671a251b263SMarc Zyngier }
672a251b263SMarc Zyngier 
67348ae34fbSMarc Zyngier /**
67482b9b424SMarc Zyngier  * of_msi_map_get_device_domain - Use msi-map to find the relevant MSI domain
67582b9b424SMarc Zyngier  * @dev: device for which the mapping is to be done.
6762bcdd8f2SLorenzo Pieralisi  * @id: Device ID.
6776f881abaSDiana Craciun  * @bus_token: Bus token
67882b9b424SMarc Zyngier  *
67982b9b424SMarc Zyngier  * Walk up the device hierarchy looking for devices with a "msi-map"
68082b9b424SMarc Zyngier  * property.
68182b9b424SMarc Zyngier  *
68282b9b424SMarc Zyngier  * Returns: the MSI domain for this device (or NULL on failure)
68382b9b424SMarc Zyngier  */
of_msi_map_get_device_domain(struct device * dev,u32 id,u32 bus_token)6846f881abaSDiana Craciun struct irq_domain *of_msi_map_get_device_domain(struct device *dev, u32 id,
6856f881abaSDiana Craciun 						u32 bus_token)
68682b9b424SMarc Zyngier {
68782b9b424SMarc Zyngier 	struct device_node *np = NULL;
68882b9b424SMarc Zyngier 
6892bcdd8f2SLorenzo Pieralisi 	__of_msi_map_id(dev, &np, id);
6906f881abaSDiana Craciun 	return irq_find_matching_host(np, bus_token);
69182b9b424SMarc Zyngier }
69282b9b424SMarc Zyngier 
69382b9b424SMarc Zyngier /**
69448ae34fbSMarc Zyngier  * of_msi_get_domain - Use msi-parent to find the relevant MSI domain
69548ae34fbSMarc Zyngier  * @dev: device for which the domain is requested
69648ae34fbSMarc Zyngier  * @np: device node for @dev
69748ae34fbSMarc Zyngier  * @token: bus type for this domain
69848ae34fbSMarc Zyngier  *
69948ae34fbSMarc Zyngier  * Parse the msi-parent property (both the simple and the complex
70048ae34fbSMarc Zyngier  * versions), and returns the corresponding MSI domain.
70148ae34fbSMarc Zyngier  *
70248ae34fbSMarc Zyngier  * Returns: the MSI domain for this device (or NULL on failure).
70348ae34fbSMarc Zyngier  */
of_msi_get_domain(struct device * dev,struct device_node * np,enum irq_domain_bus_token token)70448ae34fbSMarc Zyngier struct irq_domain *of_msi_get_domain(struct device *dev,
70548ae34fbSMarc Zyngier 				     struct device_node *np,
70648ae34fbSMarc Zyngier 				     enum irq_domain_bus_token token)
70748ae34fbSMarc Zyngier {
70848ae34fbSMarc Zyngier 	struct device_node *msi_np;
70948ae34fbSMarc Zyngier 	struct irq_domain *d;
71048ae34fbSMarc Zyngier 
71148ae34fbSMarc Zyngier 	/* Check for a single msi-parent property */
71248ae34fbSMarc Zyngier 	msi_np = of_parse_phandle(np, "msi-parent", 0);
71348ae34fbSMarc Zyngier 	if (msi_np && !of_property_read_bool(msi_np, "#msi-cells")) {
71414a0db3cSMarc Zyngier 		d = irq_find_matching_host(msi_np, token);
71548ae34fbSMarc Zyngier 		if (!d)
71648ae34fbSMarc Zyngier 			of_node_put(msi_np);
71748ae34fbSMarc Zyngier 		return d;
71848ae34fbSMarc Zyngier 	}
71948ae34fbSMarc Zyngier 
72048ae34fbSMarc Zyngier 	if (token == DOMAIN_BUS_PLATFORM_MSI) {
72148ae34fbSMarc Zyngier 		/* Check for the complex msi-parent version */
72248ae34fbSMarc Zyngier 		struct of_phandle_args args;
72348ae34fbSMarc Zyngier 		int index = 0;
72448ae34fbSMarc Zyngier 
72548ae34fbSMarc Zyngier 		while (!of_parse_phandle_with_args(np, "msi-parent",
72648ae34fbSMarc Zyngier 						   "#msi-cells",
72748ae34fbSMarc Zyngier 						   index, &args)) {
72814a0db3cSMarc Zyngier 			d = irq_find_matching_host(args.np, token);
72948ae34fbSMarc Zyngier 			if (d)
73048ae34fbSMarc Zyngier 				return d;
73148ae34fbSMarc Zyngier 
73248ae34fbSMarc Zyngier 			of_node_put(args.np);
73348ae34fbSMarc Zyngier 			index++;
73448ae34fbSMarc Zyngier 		}
73548ae34fbSMarc Zyngier 	}
73648ae34fbSMarc Zyngier 
73748ae34fbSMarc Zyngier 	return NULL;
73848ae34fbSMarc Zyngier }
739beb6f649SKevin Hilman EXPORT_SYMBOL_GPL(of_msi_get_domain);
74061c08240SMarc Zyngier 
74161c08240SMarc Zyngier /**
74261c08240SMarc Zyngier  * of_msi_configure - Set the msi_domain field of a device
74361c08240SMarc Zyngier  * @dev: device structure to associate with an MSI irq domain
74461c08240SMarc Zyngier  * @np: device node for that device
74561c08240SMarc Zyngier  */
of_msi_configure(struct device * dev,struct device_node * np)74661c08240SMarc Zyngier void of_msi_configure(struct device *dev, struct device_node *np)
74761c08240SMarc Zyngier {
74861c08240SMarc Zyngier 	dev_set_msi_domain(dev,
74961c08240SMarc Zyngier 			   of_msi_get_domain(dev, np, DOMAIN_BUS_PLATFORM_MSI));
75061c08240SMarc Zyngier }
7515282c181SSinan Kaya EXPORT_SYMBOL_GPL(of_msi_configure);
752