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