xref: /openbmc/linux/drivers/of/irq.c (revision d7fb6d0a)
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 
21e3873444SGrant Likely #include <linux/errno.h>
22c71a54b0SRob Herring #include <linux/list.h>
23e3873444SGrant Likely #include <linux/module.h>
24e3873444SGrant Likely #include <linux/of.h>
25e3873444SGrant Likely #include <linux/of_irq.h>
26e3873444SGrant Likely #include <linux/string.h>
27c71a54b0SRob Herring #include <linux/slab.h>
28e3873444SGrant Likely 
2952f6537cSAndres Salomon /* For archs that don't support NO_IRQ (such as x86), provide a dummy value */
3052f6537cSAndres Salomon #ifndef NO_IRQ
3152f6537cSAndres Salomon #define NO_IRQ 0
3252f6537cSAndres Salomon #endif
3352f6537cSAndres Salomon 
34e3873444SGrant Likely /**
35e3873444SGrant Likely  * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
36e3873444SGrant Likely  * @device: Device node of the device whose interrupt is to be mapped
37e3873444SGrant Likely  * @index: Index of the interrupt to map
38e3873444SGrant Likely  *
39e3873444SGrant Likely  * This function is a wrapper that chains of_irq_map_one() and
40e3873444SGrant Likely  * irq_create_of_mapping() to make things easier to callers
41e3873444SGrant Likely  */
42e3873444SGrant Likely unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
43e3873444SGrant Likely {
44e3873444SGrant Likely 	struct of_irq oirq;
45e3873444SGrant Likely 
46e3873444SGrant Likely 	if (of_irq_map_one(dev, index, &oirq))
47e3873444SGrant Likely 		return NO_IRQ;
48e3873444SGrant Likely 
49e3873444SGrant Likely 	return irq_create_of_mapping(oirq.controller, oirq.specifier,
50e3873444SGrant Likely 				     oirq.size);
51e3873444SGrant Likely }
52e3873444SGrant Likely EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
537dc2e113SGrant Likely 
547dc2e113SGrant Likely /**
557dc2e113SGrant Likely  * of_irq_find_parent - Given a device node, find its interrupt parent node
567dc2e113SGrant Likely  * @child: pointer to device node
577dc2e113SGrant Likely  *
587dc2e113SGrant Likely  * Returns a pointer to the interrupt parent node, or NULL if the interrupt
597dc2e113SGrant Likely  * parent could not be determined.
607dc2e113SGrant Likely  */
610b2e9a8eSMichael Ellerman struct device_node *of_irq_find_parent(struct device_node *child)
627dc2e113SGrant Likely {
63b4bbb029SLinus Torvalds 	struct device_node *p;
649a6b2e58SGrant Likely 	const __be32 *parp;
657dc2e113SGrant Likely 
66b4bbb029SLinus Torvalds 	if (!of_node_get(child))
677dc2e113SGrant Likely 		return NULL;
687dc2e113SGrant Likely 
697dc2e113SGrant Likely 	do {
70b4bbb029SLinus Torvalds 		parp = of_get_property(child, "interrupt-parent", NULL);
717dc2e113SGrant Likely 		if (parp == NULL)
72b4bbb029SLinus Torvalds 			p = of_get_parent(child);
737dc2e113SGrant Likely 		else {
747dc2e113SGrant Likely 			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
757dc2e113SGrant Likely 				p = of_node_get(of_irq_dflt_pic);
767dc2e113SGrant Likely 			else
779a6b2e58SGrant Likely 				p = of_find_node_by_phandle(be32_to_cpup(parp));
787dc2e113SGrant Likely 		}
79b4bbb029SLinus Torvalds 		of_node_put(child);
80b4bbb029SLinus Torvalds 		child = p;
817dc2e113SGrant Likely 	} while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
827dc2e113SGrant Likely 
83b4bbb029SLinus Torvalds 	return p;
847dc2e113SGrant Likely }
857dc2e113SGrant Likely 
867dc2e113SGrant Likely /**
877dc2e113SGrant Likely  * of_irq_map_raw - Low level interrupt tree parsing
887dc2e113SGrant Likely  * @parent:	the device interrupt parent
897dc2e113SGrant Likely  * @intspec:	interrupt specifier ("interrupts" property of the device)
907dc2e113SGrant Likely  * @ointsize:   size of the passed in interrupt specifier
917dc2e113SGrant Likely  * @addr:	address specifier (start of "reg" property of the device)
927dc2e113SGrant Likely  * @out_irq:	structure of_irq filled by this function
937dc2e113SGrant Likely  *
947dc2e113SGrant Likely  * Returns 0 on success and a negative number on error
957dc2e113SGrant Likely  *
967dc2e113SGrant Likely  * This function is a low-level interrupt tree walking function. It
977dc2e113SGrant Likely  * can be used to do a partial walk with synthetized reg and interrupts
987dc2e113SGrant Likely  * properties, for example when resolving PCI interrupts when no device
997dc2e113SGrant Likely  * node exist for the parent.
1007dc2e113SGrant Likely  */
101d2f71839SGrant Likely int of_irq_map_raw(struct device_node *parent, const __be32 *intspec,
102d2f71839SGrant Likely 		   u32 ointsize, const __be32 *addr, struct of_irq *out_irq)
1037dc2e113SGrant Likely {
1047dc2e113SGrant Likely 	struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
105a7c194b0SRob Herring 	const __be32 *tmp, *imap, *imask;
1067dc2e113SGrant Likely 	u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
1077dc2e113SGrant Likely 	int imaplen, match, i;
1087dc2e113SGrant Likely 
1097dc2e113SGrant Likely 	pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
110d2f71839SGrant Likely 		 parent->full_name, be32_to_cpup(intspec),
111d2f71839SGrant Likely 		 be32_to_cpup(intspec + 1), ointsize);
1127dc2e113SGrant Likely 
1137dc2e113SGrant Likely 	ipar = of_node_get(parent);
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 
1347dc2e113SGrant Likely 	pr_debug("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
1357dc2e113SGrant Likely 
1367dc2e113SGrant Likely 	if (ointsize != intsize)
1377dc2e113SGrant Likely 		return -EINVAL;
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 
1557dc2e113SGrant Likely 	/* Now start the actual "proper" walk of the interrupt tree */
1567dc2e113SGrant Likely 	while (ipar != NULL) {
1577dc2e113SGrant Likely 		/* Now check if cursor is an interrupt-controller and if it is
1587dc2e113SGrant Likely 		 * then we are done
1597dc2e113SGrant Likely 		 */
1607dc2e113SGrant Likely 		if (of_get_property(ipar, "interrupt-controller", NULL) !=
1617dc2e113SGrant Likely 				NULL) {
1627dc2e113SGrant Likely 			pr_debug(" -> got it !\n");
163a7c194b0SRob Herring 			for (i = 0; i < intsize; i++)
164a7c194b0SRob Herring 				out_irq->specifier[i] =
165a7c194b0SRob Herring 						of_read_number(intspec +i, 1);
1667dc2e113SGrant Likely 			out_irq->size = intsize;
1677dc2e113SGrant Likely 			out_irq->controller = ipar;
1687dc2e113SGrant Likely 			of_node_put(old);
1697dc2e113SGrant Likely 			return 0;
1707dc2e113SGrant Likely 		}
1717dc2e113SGrant Likely 
1727dc2e113SGrant Likely 		/* Now look for an interrupt-map */
1737dc2e113SGrant Likely 		imap = of_get_property(ipar, "interrupt-map", &imaplen);
1747dc2e113SGrant Likely 		/* No interrupt map, check for an interrupt parent */
1757dc2e113SGrant Likely 		if (imap == NULL) {
1767dc2e113SGrant Likely 			pr_debug(" -> no map, getting parent\n");
1777dc2e113SGrant Likely 			newpar = of_irq_find_parent(ipar);
1787dc2e113SGrant Likely 			goto skiplevel;
1797dc2e113SGrant Likely 		}
1807dc2e113SGrant Likely 		imaplen /= sizeof(u32);
1817dc2e113SGrant Likely 
1827dc2e113SGrant Likely 		/* Look for a mask */
1837dc2e113SGrant Likely 		imask = of_get_property(ipar, "interrupt-map-mask", NULL);
1847dc2e113SGrant Likely 
1857dc2e113SGrant Likely 		/* If we were passed no "reg" property and we attempt to parse
1867dc2e113SGrant Likely 		 * an interrupt-map, then #address-cells must be 0.
1877dc2e113SGrant Likely 		 * Fail if it's not.
1887dc2e113SGrant Likely 		 */
1897dc2e113SGrant Likely 		if (addr == NULL && addrsize != 0) {
1907dc2e113SGrant Likely 			pr_debug(" -> no reg passed in when needed !\n");
1917dc2e113SGrant Likely 			goto fail;
1927dc2e113SGrant Likely 		}
1937dc2e113SGrant Likely 
1947dc2e113SGrant Likely 		/* Parse interrupt-map */
1957dc2e113SGrant Likely 		match = 0;
1967dc2e113SGrant Likely 		while (imaplen > (addrsize + intsize + 1) && !match) {
1977dc2e113SGrant Likely 			/* Compare specifiers */
1987dc2e113SGrant Likely 			match = 1;
1997dc2e113SGrant Likely 			for (i = 0; i < addrsize && match; ++i) {
2007dc2e113SGrant Likely 				u32 mask = imask ? imask[i] : 0xffffffffu;
2017dc2e113SGrant Likely 				match = ((addr[i] ^ imap[i]) & mask) == 0;
2027dc2e113SGrant Likely 			}
2037dc2e113SGrant Likely 			for (; i < (addrsize + intsize) && match; ++i) {
2047dc2e113SGrant Likely 				u32 mask = imask ? imask[i] : 0xffffffffu;
2057dc2e113SGrant Likely 				match =
2067dc2e113SGrant Likely 				   ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
2077dc2e113SGrant Likely 			}
2087dc2e113SGrant Likely 			imap += addrsize + intsize;
2097dc2e113SGrant Likely 			imaplen -= addrsize + intsize;
2107dc2e113SGrant Likely 
2117dc2e113SGrant Likely 			pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
2127dc2e113SGrant Likely 
2137dc2e113SGrant Likely 			/* Get the interrupt parent */
2147dc2e113SGrant Likely 			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
2157dc2e113SGrant Likely 				newpar = of_node_get(of_irq_dflt_pic);
2167dc2e113SGrant Likely 			else
2179a6b2e58SGrant Likely 				newpar = of_find_node_by_phandle(be32_to_cpup(imap));
2187dc2e113SGrant Likely 			imap++;
2197dc2e113SGrant Likely 			--imaplen;
2207dc2e113SGrant Likely 
2217dc2e113SGrant Likely 			/* Check if not found */
2227dc2e113SGrant Likely 			if (newpar == NULL) {
2237dc2e113SGrant Likely 				pr_debug(" -> imap parent not found !\n");
2247dc2e113SGrant Likely 				goto fail;
2257dc2e113SGrant Likely 			}
2267dc2e113SGrant Likely 
2277dc2e113SGrant Likely 			/* Get #interrupt-cells and #address-cells of new
2287dc2e113SGrant Likely 			 * parent
2297dc2e113SGrant Likely 			 */
2307dc2e113SGrant Likely 			tmp = of_get_property(newpar, "#interrupt-cells", NULL);
2317dc2e113SGrant Likely 			if (tmp == NULL) {
2327dc2e113SGrant Likely 				pr_debug(" -> parent lacks #interrupt-cells!\n");
2337dc2e113SGrant Likely 				goto fail;
2347dc2e113SGrant Likely 			}
235a7c194b0SRob Herring 			newintsize = be32_to_cpu(*tmp);
2367dc2e113SGrant Likely 			tmp = of_get_property(newpar, "#address-cells", NULL);
237a7c194b0SRob Herring 			newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
2387dc2e113SGrant Likely 
2397dc2e113SGrant Likely 			pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
2407dc2e113SGrant Likely 			    newintsize, newaddrsize);
2417dc2e113SGrant Likely 
2427dc2e113SGrant Likely 			/* Check for malformed properties */
2437dc2e113SGrant Likely 			if (imaplen < (newaddrsize + newintsize))
2447dc2e113SGrant Likely 				goto fail;
2457dc2e113SGrant Likely 
2467dc2e113SGrant Likely 			imap += newaddrsize + newintsize;
2477dc2e113SGrant Likely 			imaplen -= newaddrsize + newintsize;
2487dc2e113SGrant Likely 
2497dc2e113SGrant Likely 			pr_debug(" -> imaplen=%d\n", imaplen);
2507dc2e113SGrant Likely 		}
2517dc2e113SGrant Likely 		if (!match)
2527dc2e113SGrant Likely 			goto fail;
2537dc2e113SGrant Likely 
2547dc2e113SGrant Likely 		of_node_put(old);
2557dc2e113SGrant Likely 		old = of_node_get(newpar);
2567dc2e113SGrant Likely 		addrsize = newaddrsize;
2577dc2e113SGrant Likely 		intsize = newintsize;
2587dc2e113SGrant Likely 		intspec = imap - intsize;
2597dc2e113SGrant Likely 		addr = intspec - addrsize;
2607dc2e113SGrant Likely 
2617dc2e113SGrant Likely 	skiplevel:
2627dc2e113SGrant Likely 		/* Iterate again with new parent */
2637dc2e113SGrant Likely 		pr_debug(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
2647dc2e113SGrant Likely 		of_node_put(ipar);
2657dc2e113SGrant Likely 		ipar = newpar;
2667dc2e113SGrant Likely 		newpar = NULL;
2677dc2e113SGrant Likely 	}
2687dc2e113SGrant Likely  fail:
2697dc2e113SGrant Likely 	of_node_put(ipar);
2707dc2e113SGrant Likely 	of_node_put(old);
2717dc2e113SGrant Likely 	of_node_put(newpar);
2727dc2e113SGrant Likely 
2737dc2e113SGrant Likely 	return -EINVAL;
2747dc2e113SGrant Likely }
2757dc2e113SGrant Likely EXPORT_SYMBOL_GPL(of_irq_map_raw);
2767dc2e113SGrant Likely 
2777dc2e113SGrant Likely /**
2787dc2e113SGrant Likely  * of_irq_map_one - Resolve an interrupt for a device
2797dc2e113SGrant Likely  * @device: the device whose interrupt is to be resolved
2807dc2e113SGrant Likely  * @index: index of the interrupt to resolve
2817dc2e113SGrant Likely  * @out_irq: structure of_irq filled by this function
2827dc2e113SGrant Likely  *
2837dc2e113SGrant Likely  * This function resolves an interrupt, walking the tree, for a given
2847dc2e113SGrant Likely  * device-tree node. It's the high level pendant to of_irq_map_raw().
2857dc2e113SGrant Likely  */
2867dc2e113SGrant Likely int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
2877dc2e113SGrant Likely {
2887dc2e113SGrant Likely 	struct device_node *p;
289d2f71839SGrant Likely 	const __be32 *intspec, *tmp, *addr;
2907dc2e113SGrant Likely 	u32 intsize, intlen;
2917dc2e113SGrant Likely 	int res = -EINVAL;
2927dc2e113SGrant Likely 
2937dc2e113SGrant Likely 	pr_debug("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
2947dc2e113SGrant Likely 
2957dc2e113SGrant Likely 	/* OldWorld mac stuff is "special", handle out of line */
2967dc2e113SGrant Likely 	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
2977dc2e113SGrant Likely 		return of_irq_map_oldworld(device, index, out_irq);
2987dc2e113SGrant Likely 
2997dc2e113SGrant Likely 	/* Get the interrupts property */
3007dc2e113SGrant Likely 	intspec = of_get_property(device, "interrupts", &intlen);
3017dc2e113SGrant Likely 	if (intspec == NULL)
3027dc2e113SGrant Likely 		return -EINVAL;
303d2f71839SGrant Likely 	intlen /= sizeof(*intspec);
3047dc2e113SGrant Likely 
305d2f71839SGrant Likely 	pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
3067dc2e113SGrant Likely 
3077dc2e113SGrant Likely 	/* Get the reg property (if any) */
3087dc2e113SGrant Likely 	addr = of_get_property(device, "reg", NULL);
3097dc2e113SGrant Likely 
3107dc2e113SGrant Likely 	/* Look for the interrupt parent. */
3117dc2e113SGrant Likely 	p = of_irq_find_parent(device);
3127dc2e113SGrant Likely 	if (p == NULL)
3137dc2e113SGrant Likely 		return -EINVAL;
3147dc2e113SGrant Likely 
3157dc2e113SGrant Likely 	/* Get size of interrupt specifier */
3167dc2e113SGrant Likely 	tmp = of_get_property(p, "#interrupt-cells", NULL);
3177dc2e113SGrant Likely 	if (tmp == NULL)
3187dc2e113SGrant Likely 		goto out;
319a7c194b0SRob Herring 	intsize = be32_to_cpu(*tmp);
3207dc2e113SGrant Likely 
3217dc2e113SGrant Likely 	pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
3227dc2e113SGrant Likely 
3237dc2e113SGrant Likely 	/* Check index */
3247dc2e113SGrant Likely 	if ((index + 1) * intsize > intlen)
3257dc2e113SGrant Likely 		goto out;
3267dc2e113SGrant Likely 
3277dc2e113SGrant Likely 	/* Get new specifier and map it */
3287dc2e113SGrant Likely 	res = of_irq_map_raw(p, intspec + index * intsize, intsize,
3297dc2e113SGrant Likely 			     addr, out_irq);
3307dc2e113SGrant Likely  out:
3317dc2e113SGrant Likely 	of_node_put(p);
3327dc2e113SGrant Likely 	return res;
3337dc2e113SGrant Likely }
3347dc2e113SGrant Likely EXPORT_SYMBOL_GPL(of_irq_map_one);
3357dc2e113SGrant Likely 
3367dc2e113SGrant Likely /**
3377dc2e113SGrant Likely  * of_irq_to_resource - Decode a node's IRQ and return it as a resource
3387dc2e113SGrant Likely  * @dev: pointer to device tree node
3397dc2e113SGrant Likely  * @index: zero-based index of the irq
3407dc2e113SGrant Likely  * @r: pointer to resource structure to return result into.
3417dc2e113SGrant Likely  */
3427dc2e113SGrant Likely int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
3437dc2e113SGrant Likely {
3447dc2e113SGrant Likely 	int irq = irq_of_parse_and_map(dev, index);
3457dc2e113SGrant Likely 
3467dc2e113SGrant Likely 	/* Only dereference the resource if both the
3477dc2e113SGrant Likely 	 * resource and the irq are valid. */
3487dc2e113SGrant Likely 	if (r && irq != NO_IRQ) {
3497dc2e113SGrant Likely 		r->start = r->end = irq;
3507dc2e113SGrant Likely 		r->flags = IORESOURCE_IRQ;
351d3571c3aSGrant Likely 		r->name = dev->full_name;
3527dc2e113SGrant Likely 	}
3537dc2e113SGrant Likely 
3547dc2e113SGrant Likely 	return irq;
3557dc2e113SGrant Likely }
3567dc2e113SGrant Likely EXPORT_SYMBOL_GPL(of_irq_to_resource);
35752f6537cSAndres Salomon 
35852f6537cSAndres Salomon /**
35952f6537cSAndres Salomon  * of_irq_count - Count the number of IRQs a node uses
36052f6537cSAndres Salomon  * @dev: pointer to device tree node
36152f6537cSAndres Salomon  */
36252f6537cSAndres Salomon int of_irq_count(struct device_node *dev)
36352f6537cSAndres Salomon {
36452f6537cSAndres Salomon 	int nr = 0;
36552f6537cSAndres Salomon 
36652f6537cSAndres Salomon 	while (of_irq_to_resource(dev, nr, NULL) != NO_IRQ)
36752f6537cSAndres Salomon 		nr++;
36852f6537cSAndres Salomon 
36952f6537cSAndres Salomon 	return nr;
37052f6537cSAndres Salomon }
37152f6537cSAndres Salomon 
37252f6537cSAndres Salomon /**
37352f6537cSAndres Salomon  * of_irq_to_resource_table - Fill in resource table with node's IRQ info
37452f6537cSAndres Salomon  * @dev: pointer to device tree node
37552f6537cSAndres Salomon  * @res: array of resources to fill in
37652f6537cSAndres Salomon  * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
37752f6537cSAndres Salomon  *
37852f6537cSAndres Salomon  * Returns the size of the filled in table (up to @nr_irqs).
37952f6537cSAndres Salomon  */
38052f6537cSAndres Salomon int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
38152f6537cSAndres Salomon 		int nr_irqs)
38252f6537cSAndres Salomon {
38352f6537cSAndres Salomon 	int i;
38452f6537cSAndres Salomon 
38552f6537cSAndres Salomon 	for (i = 0; i < nr_irqs; i++, res++)
38652f6537cSAndres Salomon 		if (of_irq_to_resource(dev, i, res) == NO_IRQ)
38752f6537cSAndres Salomon 			break;
38852f6537cSAndres Salomon 
38952f6537cSAndres Salomon 	return i;
39052f6537cSAndres Salomon }
391c71a54b0SRob Herring 
392c71a54b0SRob Herring struct intc_desc {
393c71a54b0SRob Herring 	struct list_head	list;
394c71a54b0SRob Herring 	struct device_node	*dev;
395c71a54b0SRob Herring 	struct device_node	*interrupt_parent;
396c71a54b0SRob Herring };
397c71a54b0SRob Herring 
398c71a54b0SRob Herring /**
399c71a54b0SRob Herring  * of_irq_init - Scan and init matching interrupt controllers in DT
400c71a54b0SRob Herring  * @matches: 0 terminated array of nodes to match and init function to call
401c71a54b0SRob Herring  *
402c71a54b0SRob Herring  * This function scans the device tree for matching interrupt controller nodes,
403c71a54b0SRob Herring  * and calls their initialization functions in order with parents first.
404c71a54b0SRob Herring  */
405c71a54b0SRob Herring void __init of_irq_init(const struct of_device_id *matches)
406c71a54b0SRob Herring {
407c71a54b0SRob Herring 	struct device_node *np, *parent = NULL;
408c71a54b0SRob Herring 	struct intc_desc *desc, *temp_desc;
409c71a54b0SRob Herring 	struct list_head intc_desc_list, intc_parent_list;
410c71a54b0SRob Herring 
411c71a54b0SRob Herring 	INIT_LIST_HEAD(&intc_desc_list);
412c71a54b0SRob Herring 	INIT_LIST_HEAD(&intc_parent_list);
413c71a54b0SRob Herring 
414c71a54b0SRob Herring 	for_each_matching_node(np, matches) {
415c71a54b0SRob Herring 		if (!of_find_property(np, "interrupt-controller", NULL))
416c71a54b0SRob Herring 			continue;
417c71a54b0SRob Herring 		/*
418c71a54b0SRob Herring 		 * Here, we allocate and populate an intc_desc with the node
419c71a54b0SRob Herring 		 * pointer, interrupt-parent device_node etc.
420c71a54b0SRob Herring 		 */
421c71a54b0SRob Herring 		desc = kzalloc(sizeof(*desc), GFP_KERNEL);
422c71a54b0SRob Herring 		if (WARN_ON(!desc))
423c71a54b0SRob Herring 			goto err;
424c71a54b0SRob Herring 
425c71a54b0SRob Herring 		desc->dev = np;
426c71a54b0SRob Herring 		desc->interrupt_parent = of_irq_find_parent(np);
427d7fb6d0aSRob Herring 		if (desc->interrupt_parent == np)
428d7fb6d0aSRob Herring 			desc->interrupt_parent = NULL;
429c71a54b0SRob Herring 		list_add_tail(&desc->list, &intc_desc_list);
430c71a54b0SRob Herring 	}
431c71a54b0SRob Herring 
432c71a54b0SRob Herring 	/*
433c71a54b0SRob Herring 	 * The root irq controller is the one without an interrupt-parent.
434c71a54b0SRob Herring 	 * That one goes first, followed by the controllers that reference it,
435c71a54b0SRob Herring 	 * followed by the ones that reference the 2nd level controllers, etc.
436c71a54b0SRob Herring 	 */
437c71a54b0SRob Herring 	while (!list_empty(&intc_desc_list)) {
438c71a54b0SRob Herring 		/*
439c71a54b0SRob Herring 		 * Process all controllers with the current 'parent'.
440c71a54b0SRob Herring 		 * First pass will be looking for NULL as the parent.
441c71a54b0SRob Herring 		 * The assumption is that NULL parent means a root controller.
442c71a54b0SRob Herring 		 */
443c71a54b0SRob Herring 		list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
444c71a54b0SRob Herring 			const struct of_device_id *match;
445c71a54b0SRob Herring 			int ret;
446c71a54b0SRob Herring 			of_irq_init_cb_t irq_init_cb;
447c71a54b0SRob Herring 
448c71a54b0SRob Herring 			if (desc->interrupt_parent != parent)
449c71a54b0SRob Herring 				continue;
450c71a54b0SRob Herring 
451c71a54b0SRob Herring 			list_del(&desc->list);
452c71a54b0SRob Herring 			match = of_match_node(matches, desc->dev);
453c71a54b0SRob Herring 			if (WARN(!match->data,
454c71a54b0SRob Herring 			    "of_irq_init: no init function for %s\n",
455c71a54b0SRob Herring 			    match->compatible)) {
456c71a54b0SRob Herring 				kfree(desc);
457c71a54b0SRob Herring 				continue;
458c71a54b0SRob Herring 			}
459c71a54b0SRob Herring 
460c71a54b0SRob Herring 			pr_debug("of_irq_init: init %s @ %p, parent %p\n",
461c71a54b0SRob Herring 				 match->compatible,
462c71a54b0SRob Herring 				 desc->dev, desc->interrupt_parent);
463c71a54b0SRob Herring 			irq_init_cb = match->data;
464c71a54b0SRob Herring 			ret = irq_init_cb(desc->dev, desc->interrupt_parent);
465c71a54b0SRob Herring 			if (ret) {
466c71a54b0SRob Herring 				kfree(desc);
467c71a54b0SRob Herring 				continue;
468c71a54b0SRob Herring 			}
469c71a54b0SRob Herring 
470c71a54b0SRob Herring 			/*
471c71a54b0SRob Herring 			 * This one is now set up; add it to the parent list so
472c71a54b0SRob Herring 			 * its children can get processed in a subsequent pass.
473c71a54b0SRob Herring 			 */
474c71a54b0SRob Herring 			list_add_tail(&desc->list, &intc_parent_list);
475c71a54b0SRob Herring 		}
476c71a54b0SRob Herring 
477c71a54b0SRob Herring 		/* Get the next pending parent that might have children */
478c71a54b0SRob Herring 		desc = list_first_entry(&intc_parent_list, typeof(*desc), list);
479c71a54b0SRob Herring 		if (list_empty(&intc_parent_list) || !desc) {
480c71a54b0SRob Herring 			pr_err("of_irq_init: children remain, but no parents\n");
481c71a54b0SRob Herring 			break;
482c71a54b0SRob Herring 		}
483c71a54b0SRob Herring 		list_del(&desc->list);
484c71a54b0SRob Herring 		parent = desc->dev;
485c71a54b0SRob Herring 		kfree(desc);
486c71a54b0SRob Herring 	}
487c71a54b0SRob Herring 
488c71a54b0SRob Herring 	list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
489c71a54b0SRob Herring 		list_del(&desc->list);
490c71a54b0SRob Herring 		kfree(desc);
491c71a54b0SRob Herring 	}
492c71a54b0SRob Herring err:
493c71a54b0SRob Herring 	list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
494c71a54b0SRob Herring 		list_del(&desc->list);
495c71a54b0SRob Herring 		kfree(desc);
496c71a54b0SRob Herring 	}
497c71a54b0SRob Herring }
498