xref: /openbmc/linux/drivers/of/irq.c (revision 17a70355)
1e3873444SGrant Likely /*
2e3873444SGrant Likely  *  Derived from arch/i386/kernel/irq.c
3e3873444SGrant Likely  *    Copyright (C) 1992 Linus Torvalds
4e3873444SGrant Likely  *  Adapted from arch/i386 by Gary Thomas
5e3873444SGrant Likely  *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6e3873444SGrant Likely  *  Updated and modified by Cort Dougan <cort@fsmlabs.com>
7e3873444SGrant Likely  *    Copyright (C) 1996-2001 Cort Dougan
8e3873444SGrant Likely  *  Adapted for Power Macintosh by Paul Mackerras
9e3873444SGrant Likely  *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
10e3873444SGrant Likely  *
11e3873444SGrant Likely  * This program is free software; you can redistribute it and/or
12e3873444SGrant Likely  * modify it under the terms of the GNU General Public License
13e3873444SGrant Likely  * as published by the Free Software Foundation; either version
14e3873444SGrant Likely  * 2 of the License, or (at your option) any later version.
15e3873444SGrant Likely  *
16e3873444SGrant Likely  * This file contains the code used to make IRQ descriptions in the
17e3873444SGrant Likely  * device tree to actual irq numbers on an interrupt controller
18e3873444SGrant Likely  * driver.
19e3873444SGrant Likely  */
20e3873444SGrant Likely 
21606ad42aSRob Herring #define pr_fmt(fmt)	"OF: " fmt
22606ad42aSRob Herring 
23c706c239SMarc Zyngier #include <linux/device.h>
24e3873444SGrant Likely #include <linux/errno.h>
25c71a54b0SRob Herring #include <linux/list.h>
26e3873444SGrant Likely #include <linux/module.h>
27e3873444SGrant Likely #include <linux/of.h>
28e3873444SGrant Likely #include <linux/of_irq.h>
29987068fcSRobin Murphy #include <linux/of_pci.h>
30e3873444SGrant Likely #include <linux/string.h>
31c71a54b0SRob Herring #include <linux/slab.h>
32e3873444SGrant Likely 
33e3873444SGrant Likely /**
34e3873444SGrant Likely  * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
35d84ff46aSYijing Wang  * @dev: Device node of the device whose interrupt is to be mapped
36e3873444SGrant Likely  * @index: Index of the interrupt to map
37e3873444SGrant Likely  *
380c02c800SGrant Likely  * This function is a wrapper that chains of_irq_parse_one() and
39e3873444SGrant Likely  * irq_create_of_mapping() to make things easier to callers
40e3873444SGrant Likely  */
41e3873444SGrant Likely unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
42e3873444SGrant Likely {
43530210c7SGrant Likely 	struct of_phandle_args oirq;
44e3873444SGrant Likely 
450c02c800SGrant Likely 	if (of_irq_parse_one(dev, index, &oirq))
4677a7300aSAnton Vorontsov 		return 0;
47e3873444SGrant Likely 
48e6d30ab1SGrant Likely 	return irq_create_of_mapping(&oirq);
49e3873444SGrant Likely }
50e3873444SGrant Likely EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
517dc2e113SGrant Likely 
527dc2e113SGrant Likely /**
537dc2e113SGrant Likely  * of_irq_find_parent - Given a device node, find its interrupt parent node
547dc2e113SGrant Likely  * @child: pointer to device node
557dc2e113SGrant Likely  *
567dc2e113SGrant Likely  * Returns a pointer to the interrupt parent node, or NULL if the interrupt
577dc2e113SGrant Likely  * parent could not be determined.
587dc2e113SGrant Likely  */
594c3141e0SCarlo Caione struct device_node *of_irq_find_parent(struct device_node *child)
607dc2e113SGrant Likely {
61b4bbb029SLinus Torvalds 	struct device_node *p;
629a6b2e58SGrant Likely 	const __be32 *parp;
637dc2e113SGrant Likely 
64b4bbb029SLinus Torvalds 	if (!of_node_get(child))
657dc2e113SGrant Likely 		return NULL;
667dc2e113SGrant Likely 
677dc2e113SGrant Likely 	do {
68b4bbb029SLinus Torvalds 		parp = of_get_property(child, "interrupt-parent", NULL);
697dc2e113SGrant Likely 		if (parp == NULL)
70b4bbb029SLinus Torvalds 			p = of_get_parent(child);
717dc2e113SGrant Likely 		else {
727dc2e113SGrant Likely 			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
737dc2e113SGrant Likely 				p = of_node_get(of_irq_dflt_pic);
747dc2e113SGrant Likely 			else
759a6b2e58SGrant Likely 				p = of_find_node_by_phandle(be32_to_cpup(parp));
767dc2e113SGrant Likely 		}
77b4bbb029SLinus Torvalds 		of_node_put(child);
78b4bbb029SLinus Torvalds 		child = p;
797dc2e113SGrant Likely 	} while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
807dc2e113SGrant Likely 
81b4bbb029SLinus Torvalds 	return p;
827dc2e113SGrant Likely }
834c3141e0SCarlo Caione EXPORT_SYMBOL_GPL(of_irq_find_parent);
847dc2e113SGrant Likely 
857dc2e113SGrant Likely /**
860c02c800SGrant Likely  * of_irq_parse_raw - Low level interrupt tree parsing
877dc2e113SGrant Likely  * @parent:	the device interrupt parent
8823616132SGrant Likely  * @addr:	address specifier (start of "reg" property of the device) in be32 format
8923616132SGrant Likely  * @out_irq:	structure of_irq updated by this function
907dc2e113SGrant Likely  *
917dc2e113SGrant Likely  * Returns 0 on success and a negative number on error
927dc2e113SGrant Likely  *
937dc2e113SGrant Likely  * This function is a low-level interrupt tree walking function. It
947dc2e113SGrant Likely  * can be used to do a partial walk with synthetized reg and interrupts
957dc2e113SGrant Likely  * properties, for example when resolving PCI interrupts when no device
9623616132SGrant Likely  * node exist for the parent. It takes an interrupt specifier structure as
9723616132SGrant Likely  * input, walks the tree looking for any interrupt-map properties, translates
9823616132SGrant Likely  * the specifier for each map, and then returns the translated map.
997dc2e113SGrant Likely  */
10023616132SGrant Likely int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
1017dc2e113SGrant Likely {
1027dc2e113SGrant Likely 	struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
103355e62f5SGrant Likely 	__be32 initial_match_array[MAX_PHANDLE_ARGS];
10423616132SGrant Likely 	const __be32 *match_array = initial_match_array;
10517a70355SRob Herring 	const __be32 *tmp, *imap, *imask, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) };
1067dc2e113SGrant Likely 	u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
107f1aa5484SGuilherme G. Piccoli 	int imaplen, match, i, rc = -EINVAL;
1087dc2e113SGrant Likely 
109624cfca5SGrant Likely #ifdef DEBUG
110624cfca5SGrant Likely 	of_print_phandle_args("of_irq_parse_raw: ", out_irq);
111624cfca5SGrant Likely #endif
1127dc2e113SGrant Likely 
11323616132SGrant Likely 	ipar = of_node_get(out_irq->np);
1147dc2e113SGrant Likely 
1157dc2e113SGrant Likely 	/* First get the #interrupt-cells property of the current cursor
1167dc2e113SGrant Likely 	 * that tells us how to interpret the passed-in intspec. If there
1177dc2e113SGrant Likely 	 * is none, we are nice and just walk up the tree
1187dc2e113SGrant Likely 	 */
1197dc2e113SGrant Likely 	do {
1207dc2e113SGrant Likely 		tmp = of_get_property(ipar, "#interrupt-cells", NULL);
1217dc2e113SGrant Likely 		if (tmp != NULL) {
122a7c194b0SRob Herring 			intsize = be32_to_cpu(*tmp);
1237dc2e113SGrant Likely 			break;
1247dc2e113SGrant Likely 		}
1257dc2e113SGrant Likely 		tnode = ipar;
1267dc2e113SGrant Likely 		ipar = of_irq_find_parent(ipar);
1277dc2e113SGrant Likely 		of_node_put(tnode);
1287dc2e113SGrant Likely 	} while (ipar);
1297dc2e113SGrant Likely 	if (ipar == NULL) {
1307dc2e113SGrant Likely 		pr_debug(" -> no parent found !\n");
1317dc2e113SGrant Likely 		goto fail;
1327dc2e113SGrant Likely 	}
1337dc2e113SGrant Likely 
1340c02c800SGrant Likely 	pr_debug("of_irq_parse_raw: ipar=%s, size=%d\n", of_node_full_name(ipar), intsize);
1357dc2e113SGrant Likely 
13623616132SGrant Likely 	if (out_irq->args_count != intsize)
137f1aa5484SGuilherme G. Piccoli 		goto fail;
1387dc2e113SGrant Likely 
1397dc2e113SGrant Likely 	/* Look for this #address-cells. We have to implement the old linux
1407dc2e113SGrant Likely 	 * trick of looking for the parent here as some device-trees rely on it
1417dc2e113SGrant Likely 	 */
1427dc2e113SGrant Likely 	old = of_node_get(ipar);
1437dc2e113SGrant Likely 	do {
1447dc2e113SGrant Likely 		tmp = of_get_property(old, "#address-cells", NULL);
1457dc2e113SGrant Likely 		tnode = of_get_parent(old);
1467dc2e113SGrant Likely 		of_node_put(old);
1477dc2e113SGrant Likely 		old = tnode;
1487dc2e113SGrant Likely 	} while (old && tmp == NULL);
1497dc2e113SGrant Likely 	of_node_put(old);
1507dc2e113SGrant Likely 	old = NULL;
151a7c194b0SRob Herring 	addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
1527dc2e113SGrant Likely 
1537dc2e113SGrant Likely 	pr_debug(" -> addrsize=%d\n", addrsize);
1547dc2e113SGrant Likely 
155355e62f5SGrant Likely 	/* Range check so that the temporary buffer doesn't overflow */
156f1aa5484SGuilherme G. Piccoli 	if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) {
157f1aa5484SGuilherme G. Piccoli 		rc = -EFAULT;
158355e62f5SGrant Likely 		goto fail;
159f1aa5484SGuilherme G. Piccoli 	}
160355e62f5SGrant Likely 
16123616132SGrant Likely 	/* Precalculate the match array - this simplifies match loop */
16223616132SGrant Likely 	for (i = 0; i < addrsize; i++)
16378119fd1SGrant Likely 		initial_match_array[i] = addr ? addr[i] : 0;
16423616132SGrant Likely 	for (i = 0; i < intsize; i++)
16523616132SGrant Likely 		initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);
16623616132SGrant Likely 
1677dc2e113SGrant Likely 	/* Now start the actual "proper" walk of the interrupt tree */
1687dc2e113SGrant Likely 	while (ipar != NULL) {
1697dc2e113SGrant Likely 		/* Now check if cursor is an interrupt-controller and if it is
1707dc2e113SGrant Likely 		 * then we are done
1717dc2e113SGrant Likely 		 */
1727dc2e113SGrant Likely 		if (of_get_property(ipar, "interrupt-controller", NULL) !=
1737dc2e113SGrant Likely 				NULL) {
1747dc2e113SGrant Likely 			pr_debug(" -> got it !\n");
1757dc2e113SGrant Likely 			return 0;
1767dc2e113SGrant Likely 		}
1777dc2e113SGrant Likely 
17878119fd1SGrant Likely 		/*
17978119fd1SGrant Likely 		 * interrupt-map parsing does not work without a reg
18078119fd1SGrant Likely 		 * property when #address-cells != 0
18178119fd1SGrant Likely 		 */
18278119fd1SGrant Likely 		if (addrsize && !addr) {
18378119fd1SGrant Likely 			pr_debug(" -> no reg passed in when needed !\n");
18478119fd1SGrant Likely 			goto fail;
18578119fd1SGrant Likely 		}
18678119fd1SGrant Likely 
1877dc2e113SGrant Likely 		/* Now look for an interrupt-map */
1887dc2e113SGrant Likely 		imap = of_get_property(ipar, "interrupt-map", &imaplen);
1897dc2e113SGrant Likely 		/* No interrupt map, check for an interrupt parent */
1907dc2e113SGrant Likely 		if (imap == NULL) {
1917dc2e113SGrant Likely 			pr_debug(" -> no map, getting parent\n");
1927dc2e113SGrant Likely 			newpar = of_irq_find_parent(ipar);
1937dc2e113SGrant Likely 			goto skiplevel;
1947dc2e113SGrant Likely 		}
1957dc2e113SGrant Likely 		imaplen /= sizeof(u32);
1967dc2e113SGrant Likely 
1977dc2e113SGrant Likely 		/* Look for a mask */
1987dc2e113SGrant Likely 		imask = of_get_property(ipar, "interrupt-map-mask", NULL);
19923616132SGrant Likely 		if (!imask)
20023616132SGrant Likely 			imask = dummy_imask;
2017dc2e113SGrant Likely 
2027dc2e113SGrant Likely 		/* Parse interrupt-map */
2037dc2e113SGrant Likely 		match = 0;
2047dc2e113SGrant Likely 		while (imaplen > (addrsize + intsize + 1) && !match) {
2057dc2e113SGrant Likely 			/* Compare specifiers */
2067dc2e113SGrant Likely 			match = 1;
20723616132SGrant Likely 			for (i = 0; i < (addrsize + intsize); i++, imaplen--)
20874dac2edSTomasz Figa 				match &= !((match_array[i] ^ *imap++) & imask[i]);
2097dc2e113SGrant Likely 
2107dc2e113SGrant Likely 			pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
2117dc2e113SGrant Likely 
2127dc2e113SGrant Likely 			/* Get the interrupt parent */
2137dc2e113SGrant Likely 			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
2147dc2e113SGrant Likely 				newpar = of_node_get(of_irq_dflt_pic);
2157dc2e113SGrant Likely 			else
2169a6b2e58SGrant Likely 				newpar = of_find_node_by_phandle(be32_to_cpup(imap));
2177dc2e113SGrant Likely 			imap++;
2187dc2e113SGrant Likely 			--imaplen;
2197dc2e113SGrant Likely 
2207dc2e113SGrant Likely 			/* Check if not found */
2217dc2e113SGrant Likely 			if (newpar == NULL) {
2227dc2e113SGrant Likely 				pr_debug(" -> imap parent not found !\n");
2237dc2e113SGrant Likely 				goto fail;
2247dc2e113SGrant Likely 			}
2257dc2e113SGrant Likely 
2261ca56e7dSPeter Crosthwaite 			if (!of_device_is_available(newpar))
2271ca56e7dSPeter Crosthwaite 				match = 0;
2281ca56e7dSPeter Crosthwaite 
2297dc2e113SGrant Likely 			/* Get #interrupt-cells and #address-cells of new
2307dc2e113SGrant Likely 			 * parent
2317dc2e113SGrant Likely 			 */
2327dc2e113SGrant Likely 			tmp = of_get_property(newpar, "#interrupt-cells", NULL);
2337dc2e113SGrant Likely 			if (tmp == NULL) {
2347dc2e113SGrant Likely 				pr_debug(" -> parent lacks #interrupt-cells!\n");
2357dc2e113SGrant Likely 				goto fail;
2367dc2e113SGrant Likely 			}
237a7c194b0SRob Herring 			newintsize = be32_to_cpu(*tmp);
2387dc2e113SGrant Likely 			tmp = of_get_property(newpar, "#address-cells", NULL);
239a7c194b0SRob Herring 			newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
2407dc2e113SGrant Likely 
2417dc2e113SGrant Likely 			pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
2427dc2e113SGrant Likely 			    newintsize, newaddrsize);
2437dc2e113SGrant Likely 
2447dc2e113SGrant Likely 			/* Check for malformed properties */
245f1aa5484SGuilherme G. Piccoli 			if (WARN_ON(newaddrsize + newintsize > MAX_PHANDLE_ARGS)
246f1aa5484SGuilherme G. Piccoli 			    || (imaplen < (newaddrsize + newintsize))) {
247f1aa5484SGuilherme G. Piccoli 				rc = -EFAULT;
248355e62f5SGrant Likely 				goto fail;
249f1aa5484SGuilherme G. Piccoli 			}
2507dc2e113SGrant Likely 
2517dc2e113SGrant Likely 			imap += newaddrsize + newintsize;
2527dc2e113SGrant Likely 			imaplen -= newaddrsize + newintsize;
2537dc2e113SGrant Likely 
2547dc2e113SGrant Likely 			pr_debug(" -> imaplen=%d\n", imaplen);
2557dc2e113SGrant Likely 		}
2567dc2e113SGrant Likely 		if (!match)
2577dc2e113SGrant Likely 			goto fail;
2587dc2e113SGrant Likely 
25923616132SGrant Likely 		/*
26023616132SGrant Likely 		 * Successfully parsed an interrrupt-map translation; copy new
26123616132SGrant Likely 		 * interrupt specifier into the out_irq structure
26223616132SGrant Likely 		 */
26323616132SGrant Likely 		match_array = imap - newaddrsize - newintsize;
26423616132SGrant Likely 		for (i = 0; i < newintsize; i++)
26523616132SGrant Likely 			out_irq->args[i] = be32_to_cpup(imap - newintsize + i);
26623616132SGrant Likely 		out_irq->args_count = intsize = newintsize;
2677dc2e113SGrant Likely 		addrsize = newaddrsize;
2687dc2e113SGrant Likely 
2697dc2e113SGrant Likely 	skiplevel:
2707dc2e113SGrant Likely 		/* Iterate again with new parent */
271d23b2516SJeremy Linton 		out_irq->np = newpar;
27274a7f084SGrant Likely 		pr_debug(" -> new parent: %s\n", of_node_full_name(newpar));
2737dc2e113SGrant Likely 		of_node_put(ipar);
2747dc2e113SGrant Likely 		ipar = newpar;
2757dc2e113SGrant Likely 		newpar = NULL;
2767dc2e113SGrant Likely 	}
277f1aa5484SGuilherme G. Piccoli 	rc = -ENOENT; /* No interrupt-map found */
278f1aa5484SGuilherme G. Piccoli 
2797dc2e113SGrant Likely  fail:
2807dc2e113SGrant Likely 	of_node_put(ipar);
2817dc2e113SGrant Likely 	of_node_put(newpar);
2827dc2e113SGrant Likely 
283f1aa5484SGuilherme G. Piccoli 	return rc;
2847dc2e113SGrant Likely }
2850c02c800SGrant Likely EXPORT_SYMBOL_GPL(of_irq_parse_raw);
2867dc2e113SGrant Likely 
2877dc2e113SGrant Likely /**
2880c02c800SGrant Likely  * of_irq_parse_one - Resolve an interrupt for a device
2897dc2e113SGrant Likely  * @device: the device whose interrupt is to be resolved
2907dc2e113SGrant Likely  * @index: index of the interrupt to resolve
2917dc2e113SGrant Likely  * @out_irq: structure of_irq filled by this function
2927dc2e113SGrant Likely  *
29323616132SGrant Likely  * This function resolves an interrupt for a node by walking the interrupt tree,
29423616132SGrant Likely  * finding which interrupt controller node it is attached to, and returning the
29523616132SGrant Likely  * interrupt specifier that can be used to retrieve a Linux IRQ number.
2967dc2e113SGrant Likely  */
297530210c7SGrant Likely int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
2987dc2e113SGrant Likely {
2997dc2e113SGrant Likely 	struct device_node *p;
300d2f71839SGrant Likely 	const __be32 *intspec, *tmp, *addr;
3017dc2e113SGrant Likely 	u32 intsize, intlen;
302d7c14605SLaurent Pinchart 	int i, res;
3037dc2e113SGrant Likely 
3040c02c800SGrant Likely 	pr_debug("of_irq_parse_one: dev=%s, index=%d\n", of_node_full_name(device), index);
3057dc2e113SGrant Likely 
3067dc2e113SGrant Likely 	/* OldWorld mac stuff is "special", handle out of line */
3077dc2e113SGrant Likely 	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
3080c02c800SGrant Likely 		return of_irq_parse_oldworld(device, index, out_irq);
3097dc2e113SGrant Likely 
31079d97015SGrant Likely 	/* Get the reg property (if any) */
31179d97015SGrant Likely 	addr = of_get_property(device, "reg", NULL);
31279d97015SGrant Likely 
313a9ecdc0fSFlorian Fainelli 	/* Try the new-style interrupts-extended first */
31479d97015SGrant Likely 	res = of_parse_phandle_with_args(device, "interrupts-extended",
31579d97015SGrant Likely 					"#interrupt-cells", index, out_irq);
316a9ecdc0fSFlorian Fainelli 	if (!res)
31779d97015SGrant Likely 		return of_irq_parse_raw(addr, out_irq);
318a9ecdc0fSFlorian Fainelli 
319a9ecdc0fSFlorian Fainelli 	/* Get the interrupts property */
320a9ecdc0fSFlorian Fainelli 	intspec = of_get_property(device, "interrupts", &intlen);
321a9ecdc0fSFlorian Fainelli 	if (intspec == NULL)
322a9ecdc0fSFlorian Fainelli 		return -EINVAL;
323a9ecdc0fSFlorian Fainelli 
324d2f71839SGrant Likely 	intlen /= sizeof(*intspec);
3257dc2e113SGrant Likely 
326d2f71839SGrant Likely 	pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
3277dc2e113SGrant Likely 
3287dc2e113SGrant Likely 	/* Look for the interrupt parent. */
3297dc2e113SGrant Likely 	p = of_irq_find_parent(device);
3307dc2e113SGrant Likely 	if (p == NULL)
3317dc2e113SGrant Likely 		return -EINVAL;
3327dc2e113SGrant Likely 
3337dc2e113SGrant Likely 	/* Get size of interrupt specifier */
3347dc2e113SGrant Likely 	tmp = of_get_property(p, "#interrupt-cells", NULL);
335d7c14605SLaurent Pinchart 	if (tmp == NULL) {
336d7c14605SLaurent Pinchart 		res = -EINVAL;
3377dc2e113SGrant Likely 		goto out;
338d7c14605SLaurent Pinchart 	}
339a7c194b0SRob Herring 	intsize = be32_to_cpu(*tmp);
3407dc2e113SGrant Likely 
3417dc2e113SGrant Likely 	pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
3427dc2e113SGrant Likely 
3437dc2e113SGrant Likely 	/* Check index */
344d7c14605SLaurent Pinchart 	if ((index + 1) * intsize > intlen) {
345d7c14605SLaurent Pinchart 		res = -EINVAL;
3467dc2e113SGrant Likely 		goto out;
347d7c14605SLaurent Pinchart 	}
3487dc2e113SGrant Likely 
34923616132SGrant Likely 	/* Copy intspec into irq structure */
35023616132SGrant Likely 	intspec += index * intsize;
35123616132SGrant Likely 	out_irq->np = p;
35223616132SGrant Likely 	out_irq->args_count = intsize;
35323616132SGrant Likely 	for (i = 0; i < intsize; i++)
35423616132SGrant Likely 		out_irq->args[i] = be32_to_cpup(intspec++);
35523616132SGrant Likely 
35623616132SGrant Likely 	/* Check if there are any interrupt-map translations to process */
35723616132SGrant Likely 	res = of_irq_parse_raw(addr, out_irq);
3587dc2e113SGrant Likely  out:
3597dc2e113SGrant Likely 	of_node_put(p);
3607dc2e113SGrant Likely 	return res;
3617dc2e113SGrant Likely }
3620c02c800SGrant Likely EXPORT_SYMBOL_GPL(of_irq_parse_one);
3637dc2e113SGrant Likely 
3647dc2e113SGrant Likely /**
3657dc2e113SGrant Likely  * of_irq_to_resource - Decode a node's IRQ and return it as a resource
3667dc2e113SGrant Likely  * @dev: pointer to device tree node
3677dc2e113SGrant Likely  * @index: zero-based index of the irq
3687dc2e113SGrant Likely  * @r: pointer to resource structure to return result into.
3697dc2e113SGrant Likely  */
3707dc2e113SGrant Likely int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
3717dc2e113SGrant Likely {
3727dc2e113SGrant Likely 	int irq = irq_of_parse_and_map(dev, index);
3737dc2e113SGrant Likely 
3747dc2e113SGrant Likely 	/* Only dereference the resource if both the
3757dc2e113SGrant Likely 	 * resource and the irq are valid. */
37677a7300aSAnton Vorontsov 	if (r && irq) {
377661db794SBenoit Cousson 		const char *name = NULL;
378661db794SBenoit Cousson 
379cf9e2368SSebastian Andrzej Siewior 		memset(r, 0, sizeof(*r));
380661db794SBenoit Cousson 		/*
381ae107d06SGeert Uytterhoeven 		 * Get optional "interrupt-names" property to add a name
382661db794SBenoit Cousson 		 * to the resource.
383661db794SBenoit Cousson 		 */
384661db794SBenoit Cousson 		of_property_read_string_index(dev, "interrupt-names", index,
385661db794SBenoit Cousson 					      &name);
386661db794SBenoit Cousson 
3877dc2e113SGrant Likely 		r->start = r->end = irq;
3884a43d686STomasz Figa 		r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
3898804827bSGrant Likely 		r->name = name ? name : of_node_full_name(dev);
3907dc2e113SGrant Likely 	}
3917dc2e113SGrant Likely 
3927dc2e113SGrant Likely 	return irq;
3937dc2e113SGrant Likely }
3947dc2e113SGrant Likely EXPORT_SYMBOL_GPL(of_irq_to_resource);
39552f6537cSAndres Salomon 
39652f6537cSAndres Salomon /**
39739935466SSergei Shtylyov  * of_irq_get - Decode a node's IRQ and return it as a Linux IRQ number
3989ec36cafSRob Herring  * @dev: pointer to device tree node
39939935466SSergei Shtylyov  * @index: zero-based index of the IRQ
4009ec36cafSRob Herring  *
40139935466SSergei Shtylyov  * Returns Linux IRQ number on success, or 0 on the IRQ mapping failure, or
40239935466SSergei Shtylyov  * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
40339935466SSergei Shtylyov  * of any other failure.
4049ec36cafSRob Herring  */
4059ec36cafSRob Herring int of_irq_get(struct device_node *dev, int index)
4069ec36cafSRob Herring {
4079ec36cafSRob Herring 	int rc;
4089ec36cafSRob Herring 	struct of_phandle_args oirq;
4099ec36cafSRob Herring 	struct irq_domain *domain;
4109ec36cafSRob Herring 
4119ec36cafSRob Herring 	rc = of_irq_parse_one(dev, index, &oirq);
4129ec36cafSRob Herring 	if (rc)
4139ec36cafSRob Herring 		return rc;
4149ec36cafSRob Herring 
4159ec36cafSRob Herring 	domain = irq_find_host(oirq.np);
4169ec36cafSRob Herring 	if (!domain)
4179ec36cafSRob Herring 		return -EPROBE_DEFER;
4189ec36cafSRob Herring 
4199ec36cafSRob Herring 	return irq_create_of_mapping(&oirq);
4209ec36cafSRob Herring }
4219eb08fb3SLaurent Pinchart EXPORT_SYMBOL_GPL(of_irq_get);
4229ec36cafSRob Herring 
4239ec36cafSRob Herring /**
42439935466SSergei Shtylyov  * of_irq_get_byname - Decode a node's IRQ and return it as a Linux IRQ number
425ad69674eSGrygorii Strashko  * @dev: pointer to device tree node
42639935466SSergei Shtylyov  * @name: IRQ name
427ad69674eSGrygorii Strashko  *
42839935466SSergei Shtylyov  * Returns Linux IRQ number on success, or 0 on the IRQ mapping failure, or
42939935466SSergei Shtylyov  * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
43039935466SSergei Shtylyov  * of any other failure.
431ad69674eSGrygorii Strashko  */
432ad69674eSGrygorii Strashko int of_irq_get_byname(struct device_node *dev, const char *name)
433ad69674eSGrygorii Strashko {
434ad69674eSGrygorii Strashko 	int index;
435ad69674eSGrygorii Strashko 
436ad69674eSGrygorii Strashko 	if (unlikely(!name))
437ad69674eSGrygorii Strashko 		return -EINVAL;
438ad69674eSGrygorii Strashko 
439ad69674eSGrygorii Strashko 	index = of_property_match_string(dev, "interrupt-names", name);
440ad69674eSGrygorii Strashko 	if (index < 0)
441ad69674eSGrygorii Strashko 		return index;
442ad69674eSGrygorii Strashko 
443ad69674eSGrygorii Strashko 	return of_irq_get(dev, index);
444ad69674eSGrygorii Strashko }
4456602c452SDmitry Torokhov EXPORT_SYMBOL_GPL(of_irq_get_byname);
446ad69674eSGrygorii Strashko 
447ad69674eSGrygorii Strashko /**
44852f6537cSAndres Salomon  * of_irq_count - Count the number of IRQs a node uses
44952f6537cSAndres Salomon  * @dev: pointer to device tree node
45052f6537cSAndres Salomon  */
45152f6537cSAndres Salomon int of_irq_count(struct device_node *dev)
45252f6537cSAndres Salomon {
4533da52787SThierry Reding 	struct of_phandle_args irq;
45452f6537cSAndres Salomon 	int nr = 0;
45552f6537cSAndres Salomon 
4563da52787SThierry Reding 	while (of_irq_parse_one(dev, nr, &irq) == 0)
45752f6537cSAndres Salomon 		nr++;
45852f6537cSAndres Salomon 
45952f6537cSAndres Salomon 	return nr;
46052f6537cSAndres Salomon }
46152f6537cSAndres Salomon 
46252f6537cSAndres Salomon /**
46352f6537cSAndres Salomon  * of_irq_to_resource_table - Fill in resource table with node's IRQ info
46452f6537cSAndres Salomon  * @dev: pointer to device tree node
46552f6537cSAndres Salomon  * @res: array of resources to fill in
46652f6537cSAndres Salomon  * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
46752f6537cSAndres Salomon  *
46852f6537cSAndres Salomon  * Returns the size of the filled in table (up to @nr_irqs).
46952f6537cSAndres Salomon  */
47052f6537cSAndres Salomon int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
47152f6537cSAndres Salomon 		int nr_irqs)
47252f6537cSAndres Salomon {
47352f6537cSAndres Salomon 	int i;
47452f6537cSAndres Salomon 
47552f6537cSAndres Salomon 	for (i = 0; i < nr_irqs; i++, res++)
47677a7300aSAnton Vorontsov 		if (!of_irq_to_resource(dev, i, res))
47752f6537cSAndres Salomon 			break;
47852f6537cSAndres Salomon 
47952f6537cSAndres Salomon 	return i;
48052f6537cSAndres Salomon }
481a4f8bf22SJohn Crispin EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
482c71a54b0SRob Herring 
48348a9b733SGeert Uytterhoeven struct of_intc_desc {
484c71a54b0SRob Herring 	struct list_head	list;
485264041e3SMasahiro Yamada 	of_irq_init_cb_t	irq_init_cb;
486c71a54b0SRob Herring 	struct device_node	*dev;
487c71a54b0SRob Herring 	struct device_node	*interrupt_parent;
488c71a54b0SRob Herring };
489c71a54b0SRob Herring 
490c71a54b0SRob Herring /**
491c71a54b0SRob Herring  * of_irq_init - Scan and init matching interrupt controllers in DT
492c71a54b0SRob Herring  * @matches: 0 terminated array of nodes to match and init function to call
493c71a54b0SRob Herring  *
494c71a54b0SRob Herring  * This function scans the device tree for matching interrupt controller nodes,
495c71a54b0SRob Herring  * and calls their initialization functions in order with parents first.
496c71a54b0SRob Herring  */
497c71a54b0SRob Herring void __init of_irq_init(const struct of_device_id *matches)
498c71a54b0SRob Herring {
499264041e3SMasahiro Yamada 	const struct of_device_id *match;
500c71a54b0SRob Herring 	struct device_node *np, *parent = NULL;
50148a9b733SGeert Uytterhoeven 	struct of_intc_desc *desc, *temp_desc;
502c71a54b0SRob Herring 	struct list_head intc_desc_list, intc_parent_list;
503c71a54b0SRob Herring 
504c71a54b0SRob Herring 	INIT_LIST_HEAD(&intc_desc_list);
505c71a54b0SRob Herring 	INIT_LIST_HEAD(&intc_parent_list);
506c71a54b0SRob Herring 
507264041e3SMasahiro Yamada 	for_each_matching_node_and_match(np, matches, &match) {
508bf49be02SPeter Crosthwaite 		if (!of_find_property(np, "interrupt-controller", NULL) ||
509bf49be02SPeter Crosthwaite 				!of_device_is_available(np))
510c71a54b0SRob Herring 			continue;
511264041e3SMasahiro Yamada 
512264041e3SMasahiro Yamada 		if (WARN(!match->data, "of_irq_init: no init function for %s\n",
513264041e3SMasahiro Yamada 			 match->compatible))
514264041e3SMasahiro Yamada 			continue;
515264041e3SMasahiro Yamada 
516c71a54b0SRob Herring 		/*
51748a9b733SGeert Uytterhoeven 		 * Here, we allocate and populate an of_intc_desc with the node
518c71a54b0SRob Herring 		 * pointer, interrupt-parent device_node etc.
519c71a54b0SRob Herring 		 */
520c71a54b0SRob Herring 		desc = kzalloc(sizeof(*desc), GFP_KERNEL);
5218363ccb9SJulia Lawall 		if (WARN_ON(!desc)) {
5228363ccb9SJulia Lawall 			of_node_put(np);
523c71a54b0SRob Herring 			goto err;
5248363ccb9SJulia Lawall 		}
525c71a54b0SRob Herring 
526264041e3SMasahiro Yamada 		desc->irq_init_cb = match->data;
5278363ccb9SJulia Lawall 		desc->dev = of_node_get(np);
528c71a54b0SRob Herring 		desc->interrupt_parent = of_irq_find_parent(np);
529d7fb6d0aSRob Herring 		if (desc->interrupt_parent == np)
530d7fb6d0aSRob Herring 			desc->interrupt_parent = NULL;
531c71a54b0SRob Herring 		list_add_tail(&desc->list, &intc_desc_list);
532c71a54b0SRob Herring 	}
533c71a54b0SRob Herring 
534c71a54b0SRob Herring 	/*
535c71a54b0SRob Herring 	 * The root irq controller is the one without an interrupt-parent.
536c71a54b0SRob Herring 	 * That one goes first, followed by the controllers that reference it,
537c71a54b0SRob Herring 	 * followed by the ones that reference the 2nd level controllers, etc.
538c71a54b0SRob Herring 	 */
539c71a54b0SRob Herring 	while (!list_empty(&intc_desc_list)) {
540c71a54b0SRob Herring 		/*
541c71a54b0SRob Herring 		 * Process all controllers with the current 'parent'.
542c71a54b0SRob Herring 		 * First pass will be looking for NULL as the parent.
543c71a54b0SRob Herring 		 * The assumption is that NULL parent means a root controller.
544c71a54b0SRob Herring 		 */
545c71a54b0SRob Herring 		list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
546c71a54b0SRob Herring 			int ret;
547c71a54b0SRob Herring 
548c71a54b0SRob Herring 			if (desc->interrupt_parent != parent)
549c71a54b0SRob Herring 				continue;
550c71a54b0SRob Herring 
551c71a54b0SRob Herring 			list_del(&desc->list);
552c71a54b0SRob Herring 
553e55aeb6bSPhilipp Zabel 			of_node_set_flag(desc->dev, OF_POPULATED);
554e55aeb6bSPhilipp Zabel 
555264041e3SMasahiro Yamada 			pr_debug("of_irq_init: init %s (%p), parent %p\n",
556264041e3SMasahiro Yamada 				 desc->dev->full_name,
557c71a54b0SRob Herring 				 desc->dev, desc->interrupt_parent);
558264041e3SMasahiro Yamada 			ret = desc->irq_init_cb(desc->dev,
559264041e3SMasahiro Yamada 						desc->interrupt_parent);
560c71a54b0SRob Herring 			if (ret) {
561e55aeb6bSPhilipp Zabel 				of_node_clear_flag(desc->dev, OF_POPULATED);
562c71a54b0SRob Herring 				kfree(desc);
563c71a54b0SRob Herring 				continue;
564c71a54b0SRob Herring 			}
565c71a54b0SRob Herring 
566c71a54b0SRob Herring 			/*
567c71a54b0SRob Herring 			 * This one is now set up; add it to the parent list so
568c71a54b0SRob Herring 			 * its children can get processed in a subsequent pass.
569c71a54b0SRob Herring 			 */
570c71a54b0SRob Herring 			list_add_tail(&desc->list, &intc_parent_list);
571c71a54b0SRob Herring 		}
572c71a54b0SRob Herring 
573c71a54b0SRob Herring 		/* Get the next pending parent that might have children */
574c0cdfaa0SAxel Lin 		desc = list_first_entry_or_null(&intc_parent_list,
575c0cdfaa0SAxel Lin 						typeof(*desc), list);
576c0cdfaa0SAxel Lin 		if (!desc) {
577c71a54b0SRob Herring 			pr_err("of_irq_init: children remain, but no parents\n");
578c71a54b0SRob Herring 			break;
579c71a54b0SRob Herring 		}
580c71a54b0SRob Herring 		list_del(&desc->list);
581c71a54b0SRob Herring 		parent = desc->dev;
582c71a54b0SRob Herring 		kfree(desc);
583c71a54b0SRob Herring 	}
584c71a54b0SRob Herring 
585c71a54b0SRob Herring 	list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
586c71a54b0SRob Herring 		list_del(&desc->list);
587c71a54b0SRob Herring 		kfree(desc);
588c71a54b0SRob Herring 	}
589c71a54b0SRob Herring err:
590c71a54b0SRob Herring 	list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
591c71a54b0SRob Herring 		list_del(&desc->list);
5928363ccb9SJulia Lawall 		of_node_put(desc->dev);
593c71a54b0SRob Herring 		kfree(desc);
594c71a54b0SRob Herring 	}
595c71a54b0SRob Herring }
596c706c239SMarc Zyngier 
597a251b263SMarc Zyngier static u32 __of_msi_map_rid(struct device *dev, struct device_node **np,
598a251b263SMarc Zyngier 			    u32 rid_in)
5998db02d8bSDavid Daney {
6008db02d8bSDavid Daney 	struct device *parent_dev;
6018db02d8bSDavid Daney 	u32 rid_out = rid_in;
6028db02d8bSDavid Daney 
6038db02d8bSDavid Daney 	/*
6048db02d8bSDavid Daney 	 * Walk up the device parent links looking for one with a
6058db02d8bSDavid Daney 	 * "msi-map" property.
6068db02d8bSDavid Daney 	 */
607987068fcSRobin Murphy 	for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
608987068fcSRobin Murphy 		if (!of_pci_map_rid(parent_dev->of_node, rid_in, "msi-map",
609987068fcSRobin Murphy 				    "msi-map-mask", np, &rid_out))
6108db02d8bSDavid Daney 			break;
6118db02d8bSDavid Daney 	return rid_out;
6128db02d8bSDavid Daney }
61348ae34fbSMarc Zyngier 
614a251b263SMarc Zyngier /**
615a251b263SMarc Zyngier  * of_msi_map_rid - Map a MSI requester ID for a device.
616a251b263SMarc Zyngier  * @dev: device for which the mapping is to be done.
617a251b263SMarc Zyngier  * @msi_np: device node of the expected msi controller.
618a251b263SMarc Zyngier  * @rid_in: unmapped MSI requester ID for the device.
619a251b263SMarc Zyngier  *
620a251b263SMarc Zyngier  * Walk up the device hierarchy looking for devices with a "msi-map"
621a251b263SMarc Zyngier  * property.  If found, apply the mapping to @rid_in.
622a251b263SMarc Zyngier  *
623a251b263SMarc Zyngier  * Returns the mapped MSI requester ID.
624a251b263SMarc Zyngier  */
625a251b263SMarc Zyngier u32 of_msi_map_rid(struct device *dev, struct device_node *msi_np, u32 rid_in)
626a251b263SMarc Zyngier {
627a251b263SMarc Zyngier 	return __of_msi_map_rid(dev, &msi_np, rid_in);
628a251b263SMarc Zyngier }
629a251b263SMarc Zyngier 
63048ae34fbSMarc Zyngier /**
63182b9b424SMarc Zyngier  * of_msi_map_get_device_domain - Use msi-map to find the relevant MSI domain
63282b9b424SMarc Zyngier  * @dev: device for which the mapping is to be done.
63382b9b424SMarc Zyngier  * @rid: Requester ID for the device.
63482b9b424SMarc Zyngier  *
63582b9b424SMarc Zyngier  * Walk up the device hierarchy looking for devices with a "msi-map"
63682b9b424SMarc Zyngier  * property.
63782b9b424SMarc Zyngier  *
63882b9b424SMarc Zyngier  * Returns: the MSI domain for this device (or NULL on failure)
63982b9b424SMarc Zyngier  */
64082b9b424SMarc Zyngier struct irq_domain *of_msi_map_get_device_domain(struct device *dev, u32 rid)
64182b9b424SMarc Zyngier {
64282b9b424SMarc Zyngier 	struct device_node *np = NULL;
64382b9b424SMarc Zyngier 
64482b9b424SMarc Zyngier 	__of_msi_map_rid(dev, &np, rid);
64514a0db3cSMarc Zyngier 	return irq_find_matching_host(np, DOMAIN_BUS_PCI_MSI);
64682b9b424SMarc Zyngier }
64782b9b424SMarc Zyngier 
64882b9b424SMarc Zyngier /**
64948ae34fbSMarc Zyngier  * of_msi_get_domain - Use msi-parent to find the relevant MSI domain
65048ae34fbSMarc Zyngier  * @dev: device for which the domain is requested
65148ae34fbSMarc Zyngier  * @np: device node for @dev
65248ae34fbSMarc Zyngier  * @token: bus type for this domain
65348ae34fbSMarc Zyngier  *
65448ae34fbSMarc Zyngier  * Parse the msi-parent property (both the simple and the complex
65548ae34fbSMarc Zyngier  * versions), and returns the corresponding MSI domain.
65648ae34fbSMarc Zyngier  *
65748ae34fbSMarc Zyngier  * Returns: the MSI domain for this device (or NULL on failure).
65848ae34fbSMarc Zyngier  */
65948ae34fbSMarc Zyngier struct irq_domain *of_msi_get_domain(struct device *dev,
66048ae34fbSMarc Zyngier 				     struct device_node *np,
66148ae34fbSMarc Zyngier 				     enum irq_domain_bus_token token)
66248ae34fbSMarc Zyngier {
66348ae34fbSMarc Zyngier 	struct device_node *msi_np;
66448ae34fbSMarc Zyngier 	struct irq_domain *d;
66548ae34fbSMarc Zyngier 
66648ae34fbSMarc Zyngier 	/* Check for a single msi-parent property */
66748ae34fbSMarc Zyngier 	msi_np = of_parse_phandle(np, "msi-parent", 0);
66848ae34fbSMarc Zyngier 	if (msi_np && !of_property_read_bool(msi_np, "#msi-cells")) {
66914a0db3cSMarc Zyngier 		d = irq_find_matching_host(msi_np, token);
67048ae34fbSMarc Zyngier 		if (!d)
67148ae34fbSMarc Zyngier 			of_node_put(msi_np);
67248ae34fbSMarc Zyngier 		return d;
67348ae34fbSMarc Zyngier 	}
67448ae34fbSMarc Zyngier 
67548ae34fbSMarc Zyngier 	if (token == DOMAIN_BUS_PLATFORM_MSI) {
67648ae34fbSMarc Zyngier 		/* Check for the complex msi-parent version */
67748ae34fbSMarc Zyngier 		struct of_phandle_args args;
67848ae34fbSMarc Zyngier 		int index = 0;
67948ae34fbSMarc Zyngier 
68048ae34fbSMarc Zyngier 		while (!of_parse_phandle_with_args(np, "msi-parent",
68148ae34fbSMarc Zyngier 						   "#msi-cells",
68248ae34fbSMarc Zyngier 						   index, &args)) {
68314a0db3cSMarc Zyngier 			d = irq_find_matching_host(args.np, token);
68448ae34fbSMarc Zyngier 			if (d)
68548ae34fbSMarc Zyngier 				return d;
68648ae34fbSMarc Zyngier 
68748ae34fbSMarc Zyngier 			of_node_put(args.np);
68848ae34fbSMarc Zyngier 			index++;
68948ae34fbSMarc Zyngier 		}
69048ae34fbSMarc Zyngier 	}
69148ae34fbSMarc Zyngier 
69248ae34fbSMarc Zyngier 	return NULL;
69348ae34fbSMarc Zyngier }
69461c08240SMarc Zyngier 
69561c08240SMarc Zyngier /**
69661c08240SMarc Zyngier  * of_msi_configure - Set the msi_domain field of a device
69761c08240SMarc Zyngier  * @dev: device structure to associate with an MSI irq domain
69861c08240SMarc Zyngier  * @np: device node for that device
69961c08240SMarc Zyngier  */
70061c08240SMarc Zyngier void of_msi_configure(struct device *dev, struct device_node *np)
70161c08240SMarc Zyngier {
70261c08240SMarc Zyngier 	dev_set_msi_domain(dev,
70361c08240SMarc Zyngier 			   of_msi_get_domain(dev, np, DOMAIN_BUS_PLATFORM_MSI));
70461c08240SMarc Zyngier }
7055282c181SSinan Kaya EXPORT_SYMBOL_GPL(of_msi_configure);
706