Blame view

drivers/of/irq.c 18.6 KB
af6074fc9   Rob Herring   of: Use SPDX lice...
1
  // SPDX-License-Identifier: GPL-2.0+
e38734449   Grant Likely   of/irq: Move irq_...
2
3
4
5
6
7
8
9
10
11
  /*
   *  Derived from arch/i386/kernel/irq.c
   *    Copyright (C) 1992 Linus Torvalds
   *  Adapted from arch/i386 by Gary Thomas
   *    Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
   *  Updated and modified by Cort Dougan <cort@fsmlabs.com>
   *    Copyright (C) 1996-2001 Cort Dougan
   *  Adapted for Power Macintosh by Paul Mackerras
   *    Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
   *
e38734449   Grant Likely   of/irq: Move irq_...
12
13
14
15
   * This file contains the code used to make IRQ descriptions in the
   * device tree to actual irq numbers on an interrupt controller
   * driver.
   */
606ad42aa   Rob Herring   of: use pr_fmt pr...
16
  #define pr_fmt(fmt)	"OF: " fmt
c706c239a   Marc Zyngier   of/platform: Assi...
17
  #include <linux/device.h>
e38734449   Grant Likely   of/irq: Move irq_...
18
  #include <linux/errno.h>
c71a54b08   Rob Herring   of/irq: introduce...
19
  #include <linux/list.h>
e38734449   Grant Likely   of/irq: Move irq_...
20
21
22
23
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/of_irq.h>
  #include <linux/string.h>
c71a54b08   Rob Herring   of/irq: introduce...
24
  #include <linux/slab.h>
e38734449   Grant Likely   of/irq: Move irq_...
25
26
27
  
  /**
   * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
d84ff46a9   Yijing Wang   irq/of: Fix comme...
28
   * @dev: Device node of the device whose interrupt is to be mapped
e38734449   Grant Likely   of/irq: Move irq_...
29
30
   * @index: Index of the interrupt to map
   *
0c02c8007   Grant Likely   of/irq: Rename of...
31
   * This function is a wrapper that chains of_irq_parse_one() and
e38734449   Grant Likely   of/irq: Move irq_...
32
33
34
35
   * irq_create_of_mapping() to make things easier to callers
   */
  unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  {
530210c78   Grant Likely   of/irq: Replace o...
36
  	struct of_phandle_args oirq;
e38734449   Grant Likely   of/irq: Move irq_...
37

0c02c8007   Grant Likely   of/irq: Rename of...
38
  	if (of_irq_parse_one(dev, index, &oirq))
77a7300ab   Anton Vorontsov   of/irq: Get rid o...
39
  		return 0;
e38734449   Grant Likely   of/irq: Move irq_...
40

e6d30ab1e   Grant Likely   of/irq: simplify ...
41
  	return irq_create_of_mapping(&oirq);
e38734449   Grant Likely   of/irq: Move irq_...
42
43
  }
  EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
7dc2e1134   Grant Likely   of/irq: merge irq...
44
45
46
47
48
49
50
51
  
  /**
   * of_irq_find_parent - Given a device node, find its interrupt parent node
   * @child: pointer to device node
   *
   * Returns a pointer to the interrupt parent node, or NULL if the interrupt
   * parent could not be determined.
   */
4c3141e09   Carlo Caione   of/irq: Export of...
52
  struct device_node *of_irq_find_parent(struct device_node *child)
7dc2e1134   Grant Likely   of/irq: merge irq...
53
  {
b4bbb0293   Linus Torvalds   Revert "of/irq: o...
54
  	struct device_node *p;
fa976ff7b   Sergei Shtylyov   of: irq: use of_p...
55
  	phandle parent;
7dc2e1134   Grant Likely   of/irq: merge irq...
56

b4bbb0293   Linus Torvalds   Revert "of/irq: o...
57
  	if (!of_node_get(child))
7dc2e1134   Grant Likely   of/irq: merge irq...
58
59
60
  		return NULL;
  
  	do {
fa976ff7b   Sergei Shtylyov   of: irq: use of_p...
61
  		if (of_property_read_u32(child, "interrupt-parent", &parent)) {
b4bbb0293   Linus Torvalds   Revert "of/irq: o...
62
  			p = of_get_parent(child);
fa976ff7b   Sergei Shtylyov   of: irq: use of_p...
63
  		} else	{
7dc2e1134   Grant Likely   of/irq: merge irq...
64
65
66
  			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  				p = of_node_get(of_irq_dflt_pic);
  			else
fa976ff7b   Sergei Shtylyov   of: irq: use of_p...
67
  				p = of_find_node_by_phandle(parent);
7dc2e1134   Grant Likely   of/irq: merge irq...
68
  		}
b4bbb0293   Linus Torvalds   Revert "of/irq: o...
69
70
  		of_node_put(child);
  		child = p;
7dc2e1134   Grant Likely   of/irq: merge irq...
71
  	} while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
b4bbb0293   Linus Torvalds   Revert "of/irq: o...
72
  	return p;
7dc2e1134   Grant Likely   of/irq: merge irq...
73
  }
4c3141e09   Carlo Caione   of/irq: Export of...
74
  EXPORT_SYMBOL_GPL(of_irq_find_parent);
7dc2e1134   Grant Likely   of/irq: merge irq...
75
76
  
  /**
0c02c8007   Grant Likely   of/irq: Rename of...
77
   * of_irq_parse_raw - Low level interrupt tree parsing
236161320   Grant Likely   of/irq: Refactor ...
78
   * @addr:	address specifier (start of "reg" property of the device) in be32 format
fae3b9cd7   Vasyl Gomonovych   of: irq: Fix func...
79
   * @out_irq:	structure of_phandle_args updated by this function
7dc2e1134   Grant Likely   of/irq: merge irq...
80
81
82
83
84
85
   *
   * Returns 0 on success and a negative number on error
   *
   * This function is a low-level interrupt tree walking function. It
   * can be used to do a partial walk with synthetized reg and interrupts
   * properties, for example when resolving PCI interrupts when no device
236161320   Grant Likely   of/irq: Refactor ...
86
87
88
   * node exist for the parent. It takes an interrupt specifier structure as
   * input, walks the tree looking for any interrupt-map properties, translates
   * the specifier for each map, and then returns the translated map.
7dc2e1134   Grant Likely   of/irq: merge irq...
89
   */
236161320   Grant Likely   of/irq: Refactor ...
90
  int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
7dc2e1134   Grant Likely   of/irq: merge irq...
91
92
  {
  	struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
355e62f5a   Grant Likely   of/irq: Fix poten...
93
  	__be32 initial_match_array[MAX_PHANDLE_ARGS];
236161320   Grant Likely   of/irq: Refactor ...
94
  	const __be32 *match_array = initial_match_array;
17a70355e   Rob Herring   of: fix sparse wa...
95
  	const __be32 *tmp, *imap, *imask, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) };
7dc2e1134   Grant Likely   of/irq: merge irq...
96
  	u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
f1aa54840   Guilherme G. Piccoli   of/irq: improve e...
97
  	int imaplen, match, i, rc = -EINVAL;
7dc2e1134   Grant Likely   of/irq: merge irq...
98

624cfca53   Grant Likely   of: Add helper fo...
99
100
101
  #ifdef DEBUG
  	of_print_phandle_args("of_irq_parse_raw: ", out_irq);
  #endif
7dc2e1134   Grant Likely   of/irq: merge irq...
102

236161320   Grant Likely   of/irq: Refactor ...
103
  	ipar = of_node_get(out_irq->np);
7dc2e1134   Grant Likely   of/irq: merge irq...
104
105
106
107
108
109
  
  	/* First get the #interrupt-cells property of the current cursor
  	 * that tells us how to interpret the passed-in intspec. If there
  	 * is none, we are nice and just walk up the tree
  	 */
  	do {
fa976ff7b   Sergei Shtylyov   of: irq: use of_p...
110
  		if (!of_property_read_u32(ipar, "#interrupt-cells", &intsize))
7dc2e1134   Grant Likely   of/irq: merge irq...
111
  			break;
7dc2e1134   Grant Likely   of/irq: merge irq...
112
113
114
115
116
117
118
119
120
  		tnode = ipar;
  		ipar = of_irq_find_parent(ipar);
  		of_node_put(tnode);
  	} while (ipar);
  	if (ipar == NULL) {
  		pr_debug(" -> no parent found !
  ");
  		goto fail;
  	}
0d638a07d   Rob Herring   of: Convert to us...
121
122
  	pr_debug("of_irq_parse_raw: ipar=%pOF, size=%d
  ", ipar, intsize);
7dc2e1134   Grant Likely   of/irq: merge irq...
123

236161320   Grant Likely   of/irq: Refactor ...
124
  	if (out_irq->args_count != intsize)
f1aa54840   Guilherme G. Piccoli   of/irq: improve e...
125
  		goto fail;
7dc2e1134   Grant Likely   of/irq: merge irq...
126
127
128
129
130
131
132
133
134
135
136
137
138
  
  	/* Look for this #address-cells. We have to implement the old linux
  	 * trick of looking for the parent here as some device-trees rely on it
  	 */
  	old = of_node_get(ipar);
  	do {
  		tmp = of_get_property(old, "#address-cells", NULL);
  		tnode = of_get_parent(old);
  		of_node_put(old);
  		old = tnode;
  	} while (old && tmp == NULL);
  	of_node_put(old);
  	old = NULL;
a7c194b00   Rob Herring   of/irq: little en...
139
  	addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
7dc2e1134   Grant Likely   of/irq: merge irq...
140
141
142
  
  	pr_debug(" -> addrsize=%d
  ", addrsize);
355e62f5a   Grant Likely   of/irq: Fix poten...
143
  	/* Range check so that the temporary buffer doesn't overflow */
f1aa54840   Guilherme G. Piccoli   of/irq: improve e...
144
145
  	if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) {
  		rc = -EFAULT;
355e62f5a   Grant Likely   of/irq: Fix poten...
146
  		goto fail;
f1aa54840   Guilherme G. Piccoli   of/irq: improve e...
147
  	}
355e62f5a   Grant Likely   of/irq: Fix poten...
148

236161320   Grant Likely   of/irq: Refactor ...
149
150
  	/* Precalculate the match array - this simplifies match loop */
  	for (i = 0; i < addrsize; i++)
78119fd10   Grant Likely   of/irq: Fix bug i...
151
  		initial_match_array[i] = addr ? addr[i] : 0;
236161320   Grant Likely   of/irq: Refactor ...
152
153
  	for (i = 0; i < intsize; i++)
  		initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);
7dc2e1134   Grant Likely   of/irq: merge irq...
154
155
156
157
158
  	/* Now start the actual "proper" walk of the interrupt tree */
  	while (ipar != NULL) {
  		/* Now check if cursor is an interrupt-controller and if it is
  		 * then we are done
  		 */
6a245d959   Sergei Shtylyov   of: irq: use of_p...
159
  		if (of_property_read_bool(ipar, "interrupt-controller")) {
7dc2e1134   Grant Likely   of/irq: merge irq...
160
161
  			pr_debug(" -> got it !
  ");
7dc2e1134   Grant Likely   of/irq: merge irq...
162
163
  			return 0;
  		}
78119fd10   Grant Likely   of/irq: Fix bug i...
164
165
166
167
168
169
170
171
172
  		/*
  		 * interrupt-map parsing does not work without a reg
  		 * property when #address-cells != 0
  		 */
  		if (addrsize && !addr) {
  			pr_debug(" -> no reg passed in when needed !
  ");
  			goto fail;
  		}
7dc2e1134   Grant Likely   of/irq: merge irq...
173
174
175
176
177
178
179
180
181
182
183
184
185
  		/* Now look for an interrupt-map */
  		imap = of_get_property(ipar, "interrupt-map", &imaplen);
  		/* No interrupt map, check for an interrupt parent */
  		if (imap == NULL) {
  			pr_debug(" -> no map, getting parent
  ");
  			newpar = of_irq_find_parent(ipar);
  			goto skiplevel;
  		}
  		imaplen /= sizeof(u32);
  
  		/* Look for a mask */
  		imask = of_get_property(ipar, "interrupt-map-mask", NULL);
236161320   Grant Likely   of/irq: Refactor ...
186
187
  		if (!imask)
  			imask = dummy_imask;
7dc2e1134   Grant Likely   of/irq: merge irq...
188
189
190
191
192
193
  
  		/* Parse interrupt-map */
  		match = 0;
  		while (imaplen > (addrsize + intsize + 1) && !match) {
  			/* Compare specifiers */
  			match = 1;
236161320   Grant Likely   of/irq: Refactor ...
194
  			for (i = 0; i < (addrsize + intsize); i++, imaplen--)
74dac2ed6   Tomasz Figa   of: irq: Fix inte...
195
  				match &= !((match_array[i] ^ *imap++) & imask[i]);
7dc2e1134   Grant Likely   of/irq: merge irq...
196
197
198
199
200
201
202
203
  
  			pr_debug(" -> match=%d (imaplen=%d)
  ", match, imaplen);
  
  			/* Get the interrupt parent */
  			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  				newpar = of_node_get(of_irq_dflt_pic);
  			else
9a6b2e588   Grant Likely   of: Fix phandle e...
204
  				newpar = of_find_node_by_phandle(be32_to_cpup(imap));
7dc2e1134   Grant Likely   of/irq: merge irq...
205
206
207
208
209
210
211
212
213
  			imap++;
  			--imaplen;
  
  			/* Check if not found */
  			if (newpar == NULL) {
  				pr_debug(" -> imap parent not found !
  ");
  				goto fail;
  			}
1ca56e7dc   Peter Crosthwaite   of: irq: Ignore d...
214
215
  			if (!of_device_is_available(newpar))
  				match = 0;
7dc2e1134   Grant Likely   of/irq: merge irq...
216
217
218
  			/* Get #interrupt-cells and #address-cells of new
  			 * parent
  			 */
fa976ff7b   Sergei Shtylyov   of: irq: use of_p...
219
220
  			if (of_property_read_u32(newpar, "#interrupt-cells",
  						 &newintsize)) {
7dc2e1134   Grant Likely   of/irq: merge irq...
221
222
223
224
  				pr_debug(" -> parent lacks #interrupt-cells!
  ");
  				goto fail;
  			}
fa976ff7b   Sergei Shtylyov   of: irq: use of_p...
225
226
227
  			if (of_property_read_u32(newpar, "#address-cells",
  						 &newaddrsize))
  				newaddrsize = 0;
7dc2e1134   Grant Likely   of/irq: merge irq...
228
229
230
231
232
233
  
  			pr_debug(" -> newintsize=%d, newaddrsize=%d
  ",
  			    newintsize, newaddrsize);
  
  			/* Check for malformed properties */
f1aa54840   Guilherme G. Piccoli   of/irq: improve e...
234
235
236
  			if (WARN_ON(newaddrsize + newintsize > MAX_PHANDLE_ARGS)
  			    || (imaplen < (newaddrsize + newintsize))) {
  				rc = -EFAULT;
7dc2e1134   Grant Likely   of/irq: merge irq...
237
  				goto fail;
f1aa54840   Guilherme G. Piccoli   of/irq: improve e...
238
  			}
7dc2e1134   Grant Likely   of/irq: merge irq...
239
240
241
242
243
244
245
246
247
  
  			imap += newaddrsize + newintsize;
  			imaplen -= newaddrsize + newintsize;
  
  			pr_debug(" -> imaplen=%d
  ", imaplen);
  		}
  		if (!match)
  			goto fail;
236161320   Grant Likely   of/irq: Refactor ...
248
249
250
251
  		/*
  		 * Successfully parsed an interrrupt-map translation; copy new
  		 * interrupt specifier into the out_irq structure
  		 */
236161320   Grant Likely   of/irq: Refactor ...
252
253
254
255
  		match_array = imap - newaddrsize - newintsize;
  		for (i = 0; i < newintsize; i++)
  			out_irq->args[i] = be32_to_cpup(imap - newintsize + i);
  		out_irq->args_count = intsize = newintsize;
7dc2e1134   Grant Likely   of/irq: merge irq...
256
  		addrsize = newaddrsize;
7dc2e1134   Grant Likely   of/irq: merge irq...
257
258
259
  
  	skiplevel:
  		/* Iterate again with new parent */
d23b25166   Jeremy Linton   of/irq: Fix pSeri...
260
  		out_irq->np = newpar;
0d638a07d   Rob Herring   of: Convert to us...
261
262
  		pr_debug(" -> new parent: %pOF
  ", newpar);
7dc2e1134   Grant Likely   of/irq: merge irq...
263
264
265
266
  		of_node_put(ipar);
  		ipar = newpar;
  		newpar = NULL;
  	}
f1aa54840   Guilherme G. Piccoli   of/irq: improve e...
267
  	rc = -ENOENT; /* No interrupt-map found */
7dc2e1134   Grant Likely   of/irq: merge irq...
268
269
   fail:
  	of_node_put(ipar);
7dc2e1134   Grant Likely   of/irq: merge irq...
270
  	of_node_put(newpar);
f1aa54840   Guilherme G. Piccoli   of/irq: improve e...
271
  	return rc;
7dc2e1134   Grant Likely   of/irq: merge irq...
272
  }
0c02c8007   Grant Likely   of/irq: Rename of...
273
  EXPORT_SYMBOL_GPL(of_irq_parse_raw);
7dc2e1134   Grant Likely   of/irq: merge irq...
274
275
  
  /**
0c02c8007   Grant Likely   of/irq: Rename of...
276
   * of_irq_parse_one - Resolve an interrupt for a device
7dc2e1134   Grant Likely   of/irq: merge irq...
277
278
   * @device: the device whose interrupt is to be resolved
   * @index: index of the interrupt to resolve
83f82d7a4   Lubomir Rintel   of: irq: fix a tr...
279
   * @out_irq: structure of_phandle_args filled by this function
7dc2e1134   Grant Likely   of/irq: merge irq...
280
   *
236161320   Grant Likely   of/irq: Refactor ...
281
282
283
   * This function resolves an interrupt for a node by walking the interrupt tree,
   * finding which interrupt controller node it is attached to, and returning the
   * interrupt specifier that can be used to retrieve a Linux IRQ number.
7dc2e1134   Grant Likely   of/irq: merge irq...
284
   */
530210c78   Grant Likely   of/irq: Replace o...
285
  int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
7dc2e1134   Grant Likely   of/irq: merge irq...
286
287
  {
  	struct device_node *p;
b47fe22d8   Rob Herring   of/irq: use of_pr...
288
289
  	const __be32 *addr;
  	u32 intsize;
d7c146053   Laurent Pinchart   of/irq: Fix of_ir...
290
  	int i, res;
7dc2e1134   Grant Likely   of/irq: merge irq...
291

0d638a07d   Rob Herring   of: Convert to us...
292
293
  	pr_debug("of_irq_parse_one: dev=%pOF, index=%d
  ", device, index);
7dc2e1134   Grant Likely   of/irq: merge irq...
294
295
296
  
  	/* OldWorld mac stuff is "special", handle out of line */
  	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
0c02c8007   Grant Likely   of/irq: Rename of...
297
  		return of_irq_parse_oldworld(device, index, out_irq);
7dc2e1134   Grant Likely   of/irq: merge irq...
298

79d970155   Grant Likely   of/irq: create in...
299
300
  	/* Get the reg property (if any) */
  	addr = of_get_property(device, "reg", NULL);
a9ecdc0fd   Florian Fainelli   of/irq: Fix looku...
301
302
303
304
305
  	/* Try the new-style interrupts-extended first */
  	res = of_parse_phandle_with_args(device, "interrupts-extended",
  					"#interrupt-cells", index, out_irq);
  	if (!res)
  		return of_irq_parse_raw(addr, out_irq);
7dc2e1134   Grant Likely   of/irq: merge irq...
306
307
308
309
310
311
  	/* Look for the interrupt parent. */
  	p = of_irq_find_parent(device);
  	if (p == NULL)
  		return -EINVAL;
  
  	/* Get size of interrupt specifier */
fa976ff7b   Sergei Shtylyov   of: irq: use of_p...
312
  	if (of_property_read_u32(p, "#interrupt-cells", &intsize)) {
d7c146053   Laurent Pinchart   of/irq: Fix of_ir...
313
  		res = -EINVAL;
7dc2e1134   Grant Likely   of/irq: merge irq...
314
  		goto out;
d7c146053   Laurent Pinchart   of/irq: Fix of_ir...
315
  	}
7dc2e1134   Grant Likely   of/irq: merge irq...
316

b47fe22d8   Rob Herring   of/irq: use of_pr...
317
318
  	pr_debug(" parent=%pOF, intsize=%d
  ", p, intsize);
7dc2e1134   Grant Likely   of/irq: merge irq...
319

236161320   Grant Likely   of/irq: Refactor ...
320
  	/* Copy intspec into irq structure */
236161320   Grant Likely   of/irq: Refactor ...
321
322
  	out_irq->np = p;
  	out_irq->args_count = intsize;
b47fe22d8   Rob Herring   of/irq: use of_pr...
323
324
325
326
327
328
329
330
331
332
  	for (i = 0; i < intsize; i++) {
  		res = of_property_read_u32_index(device, "interrupts",
  						 (index * intsize) + i,
  						 out_irq->args + i);
  		if (res)
  			goto out;
  	}
  
  	pr_debug(" intspec=%d
  ", *out_irq->args);
236161320   Grant Likely   of/irq: Refactor ...
333
334
335
  
  	/* Check if there are any interrupt-map translations to process */
  	res = of_irq_parse_raw(addr, out_irq);
7dc2e1134   Grant Likely   of/irq: merge irq...
336
337
338
339
   out:
  	of_node_put(p);
  	return res;
  }
0c02c8007   Grant Likely   of/irq: Rename of...
340
  EXPORT_SYMBOL_GPL(of_irq_parse_one);
7dc2e1134   Grant Likely   of/irq: merge irq...
341
342
343
344
345
346
347
348
349
  
  /**
   * of_irq_to_resource - Decode a node's IRQ and return it as a resource
   * @dev: pointer to device tree node
   * @index: zero-based index of the irq
   * @r: pointer to resource structure to return result into.
   */
  int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  {
7a4228bbf   Thomas Petazzoni   of: irq: use of_i...
350
351
352
353
  	int irq = of_irq_get(dev, index);
  
  	if (irq < 0)
  		return irq;
7dc2e1134   Grant Likely   of/irq: merge irq...
354
355
356
  
  	/* Only dereference the resource if both the
  	 * resource and the irq are valid. */
77a7300ab   Anton Vorontsov   of/irq: Get rid o...
357
  	if (r && irq) {
661db794e   Benoit Cousson   of/irq: Add inter...
358
  		const char *name = NULL;
cf9e23686   Sebastian Andrzej Siewior   of/irq: init stru...
359
  		memset(r, 0, sizeof(*r));
661db794e   Benoit Cousson   of/irq: Add inter...
360
  		/*
ae107d061   Geert Uytterhoeven   dt: Fix binding t...
361
  		 * Get optional "interrupt-names" property to add a name
661db794e   Benoit Cousson   of/irq: Add inter...
362
363
364
365
  		 * to the resource.
  		 */
  		of_property_read_string_index(dev, "interrupt-names", index,
  					      &name);
7dc2e1134   Grant Likely   of/irq: merge irq...
366
  		r->start = r->end = irq;
4a43d686f   Tomasz Figa   of/irq: Pass trig...
367
  		r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
8804827b3   Grant Likely   of: Fix dereferen...
368
  		r->name = name ? name : of_node_full_name(dev);
7dc2e1134   Grant Likely   of/irq: merge irq...
369
370
371
372
373
  	}
  
  	return irq;
  }
  EXPORT_SYMBOL_GPL(of_irq_to_resource);
52f6537cb   Andres Salomon   of/irq: remove re...
374
375
  
  /**
399354664   Sergei Shtylyov   of: irq: fix of_i...
376
   * of_irq_get - Decode a node's IRQ and return it as a Linux IRQ number
9ec36cafe   Rob Herring   of/irq: do irq re...
377
   * @dev: pointer to device tree node
399354664   Sergei Shtylyov   of: irq: fix of_i...
378
   * @index: zero-based index of the IRQ
9ec36cafe   Rob Herring   of/irq: do irq re...
379
   *
399354664   Sergei Shtylyov   of: irq: fix of_i...
380
381
382
   * Returns Linux IRQ number on success, or 0 on the IRQ mapping failure, or
   * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
   * of any other failure.
9ec36cafe   Rob Herring   of/irq: do irq re...
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
   */
  int of_irq_get(struct device_node *dev, int index)
  {
  	int rc;
  	struct of_phandle_args oirq;
  	struct irq_domain *domain;
  
  	rc = of_irq_parse_one(dev, index, &oirq);
  	if (rc)
  		return rc;
  
  	domain = irq_find_host(oirq.np);
  	if (!domain)
  		return -EPROBE_DEFER;
  
  	return irq_create_of_mapping(&oirq);
  }
9eb08fb3d   Laurent Pinchart   of/irq: Export of...
400
  EXPORT_SYMBOL_GPL(of_irq_get);
9ec36cafe   Rob Herring   of/irq: do irq re...
401
402
  
  /**
399354664   Sergei Shtylyov   of: irq: fix of_i...
403
   * of_irq_get_byname - Decode a node's IRQ and return it as a Linux IRQ number
ad69674e7   Grygorii Strashko   of/irq: do irq re...
404
   * @dev: pointer to device tree node
399354664   Sergei Shtylyov   of: irq: fix of_i...
405
   * @name: IRQ name
ad69674e7   Grygorii Strashko   of/irq: do irq re...
406
   *
399354664   Sergei Shtylyov   of: irq: fix of_i...
407
408
409
   * Returns Linux IRQ number on success, or 0 on the IRQ mapping failure, or
   * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
   * of any other failure.
ad69674e7   Grygorii Strashko   of/irq: do irq re...
410
411
412
413
414
415
416
417
418
419
420
421
422
423
   */
  int of_irq_get_byname(struct device_node *dev, const char *name)
  {
  	int index;
  
  	if (unlikely(!name))
  		return -EINVAL;
  
  	index = of_property_match_string(dev, "interrupt-names", name);
  	if (index < 0)
  		return index;
  
  	return of_irq_get(dev, index);
  }
6602c452c   Dmitry Torokhov   of/irq: export of...
424
  EXPORT_SYMBOL_GPL(of_irq_get_byname);
ad69674e7   Grygorii Strashko   of/irq: do irq re...
425
426
  
  /**
52f6537cb   Andres Salomon   of/irq: remove re...
427
428
429
430
431
   * of_irq_count - Count the number of IRQs a node uses
   * @dev: pointer to device tree node
   */
  int of_irq_count(struct device_node *dev)
  {
3da527872   Thierry Reding   of/irq: Rework of...
432
  	struct of_phandle_args irq;
52f6537cb   Andres Salomon   of/irq: remove re...
433
  	int nr = 0;
3da527872   Thierry Reding   of/irq: Rework of...
434
  	while (of_irq_parse_one(dev, nr, &irq) == 0)
52f6537cb   Andres Salomon   of/irq: remove re...
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  		nr++;
  
  	return nr;
  }
  
  /**
   * of_irq_to_resource_table - Fill in resource table with node's IRQ info
   * @dev: pointer to device tree node
   * @res: array of resources to fill in
   * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
   *
   * Returns the size of the filled in table (up to @nr_irqs).
   */
  int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
  		int nr_irqs)
  {
  	int i;
  
  	for (i = 0; i < nr_irqs; i++, res++)
531da7407   Sergei Shtylyov   of: irq: fix of_i...
454
  		if (of_irq_to_resource(dev, i, res) <= 0)
52f6537cb   Andres Salomon   of/irq: remove re...
455
456
457
458
  			break;
  
  	return i;
  }
a4f8bf220   John Crispin   DT: export of_irq...
459
  EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
c71a54b08   Rob Herring   of/irq: introduce...
460

48a9b733e   Geert Uytterhoeven   of/irq: Rename "i...
461
  struct of_intc_desc {
c71a54b08   Rob Herring   of/irq: introduce...
462
  	struct list_head	list;
264041e37   Masahiro Yamada   of/irq: optimize ...
463
  	of_irq_init_cb_t	irq_init_cb;
c71a54b08   Rob Herring   of/irq: introduce...
464
465
466
467
468
469
470
471
472
473
474
475
476
  	struct device_node	*dev;
  	struct device_node	*interrupt_parent;
  };
  
  /**
   * of_irq_init - Scan and init matching interrupt controllers in DT
   * @matches: 0 terminated array of nodes to match and init function to call
   *
   * This function scans the device tree for matching interrupt controller nodes,
   * and calls their initialization functions in order with parents first.
   */
  void __init of_irq_init(const struct of_device_id *matches)
  {
264041e37   Masahiro Yamada   of/irq: optimize ...
477
  	const struct of_device_id *match;
c71a54b08   Rob Herring   of/irq: introduce...
478
  	struct device_node *np, *parent = NULL;
48a9b733e   Geert Uytterhoeven   of/irq: Rename "i...
479
  	struct of_intc_desc *desc, *temp_desc;
c71a54b08   Rob Herring   of/irq: introduce...
480
481
482
483
  	struct list_head intc_desc_list, intc_parent_list;
  
  	INIT_LIST_HEAD(&intc_desc_list);
  	INIT_LIST_HEAD(&intc_parent_list);
264041e37   Masahiro Yamada   of/irq: optimize ...
484
  	for_each_matching_node_and_match(np, matches, &match) {
6a245d959   Sergei Shtylyov   of: irq: use of_p...
485
  		if (!of_property_read_bool(np, "interrupt-controller") ||
bf49be02d   Peter Crosthwaite   of: irq: Ignore d...
486
  				!of_device_is_available(np))
c71a54b08   Rob Herring   of/irq: introduce...
487
  			continue;
264041e37   Masahiro Yamada   of/irq: optimize ...
488
489
490
491
492
  
  		if (WARN(!match->data, "of_irq_init: no init function for %s
  ",
  			 match->compatible))
  			continue;
c71a54b08   Rob Herring   of/irq: introduce...
493
  		/*
48a9b733e   Geert Uytterhoeven   of/irq: Rename "i...
494
  		 * Here, we allocate and populate an of_intc_desc with the node
c71a54b08   Rob Herring   of/irq: introduce...
495
496
497
  		 * pointer, interrupt-parent device_node etc.
  		 */
  		desc = kzalloc(sizeof(*desc), GFP_KERNEL);
6f7dc9a37   Geert Uytterhoeven   of: irq: Remove W...
498
  		if (!desc) {
8363ccb91   Julia Lawall   of/irq: add missi...
499
  			of_node_put(np);
c71a54b08   Rob Herring   of/irq: introduce...
500
  			goto err;
8363ccb91   Julia Lawall   of/irq: add missi...
501
  		}
c71a54b08   Rob Herring   of/irq: introduce...
502

264041e37   Masahiro Yamada   of/irq: optimize ...
503
  		desc->irq_init_cb = match->data;
8363ccb91   Julia Lawall   of/irq: add missi...
504
  		desc->dev = of_node_get(np);
c71a54b08   Rob Herring   of/irq: introduce...
505
  		desc->interrupt_parent = of_irq_find_parent(np);
d7fb6d0ad   Rob Herring   of/irq: of_irq_in...
506
507
  		if (desc->interrupt_parent == np)
  			desc->interrupt_parent = NULL;
c71a54b08   Rob Herring   of/irq: introduce...
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  		list_add_tail(&desc->list, &intc_desc_list);
  	}
  
  	/*
  	 * The root irq controller is the one without an interrupt-parent.
  	 * That one goes first, followed by the controllers that reference it,
  	 * followed by the ones that reference the 2nd level controllers, etc.
  	 */
  	while (!list_empty(&intc_desc_list)) {
  		/*
  		 * Process all controllers with the current 'parent'.
  		 * First pass will be looking for NULL as the parent.
  		 * The assumption is that NULL parent means a root controller.
  		 */
  		list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
c71a54b08   Rob Herring   of/irq: introduce...
523
  			int ret;
c71a54b08   Rob Herring   of/irq: introduce...
524
525
526
527
528
  
  			if (desc->interrupt_parent != parent)
  				continue;
  
  			list_del(&desc->list);
c71a54b08   Rob Herring   of/irq: introduce...
529

e55aeb6ba   Philipp Zabel   of/irq: Mark inte...
530
  			of_node_set_flag(desc->dev, OF_POPULATED);
0d638a07d   Rob Herring   of: Convert to us...
531
532
533
  			pr_debug("of_irq_init: init %pOF (%p), parent %p
  ",
  				 desc->dev,
c71a54b08   Rob Herring   of/irq: introduce...
534
  				 desc->dev, desc->interrupt_parent);
264041e37   Masahiro Yamada   of/irq: optimize ...
535
536
  			ret = desc->irq_init_cb(desc->dev,
  						desc->interrupt_parent);
c71a54b08   Rob Herring   of/irq: introduce...
537
  			if (ret) {
e55aeb6ba   Philipp Zabel   of/irq: Mark inte...
538
  				of_node_clear_flag(desc->dev, OF_POPULATED);
c71a54b08   Rob Herring   of/irq: introduce...
539
540
541
542
543
544
545
546
547
548
549
550
  				kfree(desc);
  				continue;
  			}
  
  			/*
  			 * This one is now set up; add it to the parent list so
  			 * its children can get processed in a subsequent pass.
  			 */
  			list_add_tail(&desc->list, &intc_parent_list);
  		}
  
  		/* Get the next pending parent that might have children */
c0cdfaa0a   Axel Lin   of/irq: Avoid cal...
551
552
553
  		desc = list_first_entry_or_null(&intc_parent_list,
  						typeof(*desc), list);
  		if (!desc) {
c71a54b08   Rob Herring   of/irq: introduce...
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
  			pr_err("of_irq_init: children remain, but no parents
  ");
  			break;
  		}
  		list_del(&desc->list);
  		parent = desc->dev;
  		kfree(desc);
  	}
  
  	list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
  		list_del(&desc->list);
  		kfree(desc);
  	}
  err:
  	list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  		list_del(&desc->list);
8363ccb91   Julia Lawall   of/irq: add missi...
570
  		of_node_put(desc->dev);
c71a54b08   Rob Herring   of/irq: introduce...
571
572
573
  		kfree(desc);
  	}
  }
c706c239a   Marc Zyngier   of/platform: Assi...
574

2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
575
576
  static u32 __of_msi_map_id(struct device *dev, struct device_node **np,
  			    u32 id_in)
8db02d8b4   David Daney   of/irq: Add new f...
577
578
  {
  	struct device *parent_dev;
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
579
  	u32 id_out = id_in;
8db02d8b4   David Daney   of/irq: Add new f...
580
581
582
583
584
  
  	/*
  	 * Walk up the device parent links looking for one with a
  	 * "msi-map" property.
  	 */
987068fcb   Robin Murphy   of/irq: Break out...
585
  	for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
586
587
  		if (!of_map_id(parent_dev->of_node, id_in, "msi-map",
  				"msi-map-mask", np, &id_out))
a251b2633   Marc Zyngier   of/irq: Split of_...
588
  			break;
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
589
  	return id_out;
8db02d8b4   David Daney   of/irq: Add new f...
590
  }
48ae34fb3   Marc Zyngier   of/irq: Add suppo...
591

a251b2633   Marc Zyngier   of/irq: Split of_...
592
  /**
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
593
   * of_msi_map_id - Map a MSI ID for a device.
a251b2633   Marc Zyngier   of/irq: Split of_...
594
595
   * @dev: device for which the mapping is to be done.
   * @msi_np: device node of the expected msi controller.
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
596
   * @id_in: unmapped MSI ID for the device.
a251b2633   Marc Zyngier   of/irq: Split of_...
597
598
   *
   * Walk up the device hierarchy looking for devices with a "msi-map"
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
599
   * property.  If found, apply the mapping to @id_in.
a251b2633   Marc Zyngier   of/irq: Split of_...
600
   *
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
601
   * Returns the mapped MSI ID.
a251b2633   Marc Zyngier   of/irq: Split of_...
602
   */
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
603
  u32 of_msi_map_id(struct device *dev, struct device_node *msi_np, u32 id_in)
a251b2633   Marc Zyngier   of/irq: Split of_...
604
  {
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
605
  	return __of_msi_map_id(dev, &msi_np, id_in);
a251b2633   Marc Zyngier   of/irq: Split of_...
606
  }
48ae34fb3   Marc Zyngier   of/irq: Add suppo...
607
  /**
82b9b4243   Marc Zyngier   of/irq: Use the m...
608
609
   * of_msi_map_get_device_domain - Use msi-map to find the relevant MSI domain
   * @dev: device for which the mapping is to be done.
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
610
   * @id: Device ID.
6f881aba0   Diana Craciun   of/irq: make of_m...
611
   * @bus_token: Bus token
82b9b4243   Marc Zyngier   of/irq: Use the m...
612
613
614
615
616
617
   *
   * Walk up the device hierarchy looking for devices with a "msi-map"
   * property.
   *
   * Returns: the MSI domain for this device (or NULL on failure)
   */
6f881aba0   Diana Craciun   of/irq: make of_m...
618
619
  struct irq_domain *of_msi_map_get_device_domain(struct device *dev, u32 id,
  						u32 bus_token)
82b9b4243   Marc Zyngier   of/irq: Use the m...
620
621
  {
  	struct device_node *np = NULL;
2bcdd8f2c   Lorenzo Pieralisi   of/irq: Make of_m...
622
  	__of_msi_map_id(dev, &np, id);
6f881aba0   Diana Craciun   of/irq: make of_m...
623
  	return irq_find_matching_host(np, bus_token);
82b9b4243   Marc Zyngier   of/irq: Use the m...
624
625
626
  }
  
  /**
48ae34fb3   Marc Zyngier   of/irq: Add suppo...
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
   * of_msi_get_domain - Use msi-parent to find the relevant MSI domain
   * @dev: device for which the domain is requested
   * @np: device node for @dev
   * @token: bus type for this domain
   *
   * Parse the msi-parent property (both the simple and the complex
   * versions), and returns the corresponding MSI domain.
   *
   * Returns: the MSI domain for this device (or NULL on failure).
   */
  struct irq_domain *of_msi_get_domain(struct device *dev,
  				     struct device_node *np,
  				     enum irq_domain_bus_token token)
  {
  	struct device_node *msi_np;
  	struct irq_domain *d;
  
  	/* Check for a single msi-parent property */
  	msi_np = of_parse_phandle(np, "msi-parent", 0);
  	if (msi_np && !of_property_read_bool(msi_np, "#msi-cells")) {
14a0db3cd   Marc Zyngier   of: MSI: Simplify...
647
  		d = irq_find_matching_host(msi_np, token);
48ae34fb3   Marc Zyngier   of/irq: Add suppo...
648
649
650
651
652
653
654
655
656
657
658
659
660
  		if (!d)
  			of_node_put(msi_np);
  		return d;
  	}
  
  	if (token == DOMAIN_BUS_PLATFORM_MSI) {
  		/* Check for the complex msi-parent version */
  		struct of_phandle_args args;
  		int index = 0;
  
  		while (!of_parse_phandle_with_args(np, "msi-parent",
  						   "#msi-cells",
  						   index, &args)) {
14a0db3cd   Marc Zyngier   of: MSI: Simplify...
661
  			d = irq_find_matching_host(args.np, token);
48ae34fb3   Marc Zyngier   of/irq: Add suppo...
662
663
664
665
666
667
668
669
670
671
  			if (d)
  				return d;
  
  			of_node_put(args.np);
  			index++;
  		}
  	}
  
  	return NULL;
  }
61c08240a   Marc Zyngier   of/irq: Use of_ms...
672
673
674
675
676
677
678
679
680
681
682
  
  /**
   * of_msi_configure - Set the msi_domain field of a device
   * @dev: device structure to associate with an MSI irq domain
   * @np: device node for that device
   */
  void of_msi_configure(struct device *dev, struct device_node *np)
  {
  	dev_set_msi_domain(dev,
  			   of_msi_get_domain(dev, np, DOMAIN_BUS_PLATFORM_MSI));
  }
5282c1816   Sinan Kaya   of: irq: make of_...
683
  EXPORT_SYMBOL_GPL(of_msi_configure);