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