2 * Derived from arch/i386/kernel/irq.c
3 * Copyright (C) 1992 Linus Torvalds
4 * Adapted from arch/i386 by Gary Thomas
5 * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
6 * Updated and modified by Cort Dougan <cort@fsmlabs.com>
7 * Copyright (C) 1996-2001 Cort Dougan
8 * Adapted for Power Macintosh by Paul Mackerras
9 * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
16 * This file contains the code used to make IRQ descriptions in the
17 * device tree to actual irq numbers on an interrupt controller
21 #include <linux/errno.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
25 #include <linux/of_irq.h>
26 #include <linux/string.h>
27 #include <linux/slab.h>
30 * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
31 * @dev: Device node of the device whose interrupt is to be mapped
32 * @index: Index of the interrupt to map
34 * This function is a wrapper that chains of_irq_parse_one() and
35 * irq_create_of_mapping() to make things easier to callers
37 unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
39 struct of_phandle_args oirq;
41 if (of_irq_parse_one(dev, index, &oirq))
44 return irq_create_of_mapping(&oirq);
46 EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
49 * of_irq_find_parent - Given a device node, find its interrupt parent node
50 * @child: pointer to device node
52 * Returns a pointer to the interrupt parent node, or NULL if the interrupt
53 * parent could not be determined.
55 struct device_node *of_irq_find_parent(struct device_node *child)
57 struct device_node *p;
60 if (!of_node_get(child))
64 parp = of_get_property(child, "interrupt-parent", NULL);
66 p = of_get_parent(child);
68 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
69 p = of_node_get(of_irq_dflt_pic);
71 p = of_find_node_by_phandle(be32_to_cpup(parp));
75 } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
81 * of_irq_parse_raw - Low level interrupt tree parsing
82 * @parent: the device interrupt parent
83 * @intspec: interrupt specifier ("interrupts" property of the device)
84 * @ointsize: size of the passed in interrupt specifier
85 * @addr: address specifier (start of "reg" property of the device)
86 * @out_irq: structure of_irq filled by this function
88 * Returns 0 on success and a negative number on error
90 * This function is a low-level interrupt tree walking function. It
91 * can be used to do a partial walk with synthetized reg and interrupts
92 * properties, for example when resolving PCI interrupts when no device
93 * node exist for the parent.
95 int of_irq_parse_raw(struct device_node *parent, const __be32 *intspec,
96 u32 ointsize, const __be32 *addr, struct of_phandle_args *out_irq)
98 struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
99 const __be32 *tmp, *imap, *imask;
100 u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
101 int imaplen, match, i;
103 pr_debug("of_irq_parse_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
104 of_node_full_name(parent), be32_to_cpup(intspec),
105 be32_to_cpup(intspec + 1), ointsize);
107 ipar = of_node_get(parent);
109 /* First get the #interrupt-cells property of the current cursor
110 * that tells us how to interpret the passed-in intspec. If there
111 * is none, we are nice and just walk up the tree
114 tmp = of_get_property(ipar, "#interrupt-cells", NULL);
116 intsize = be32_to_cpu(*tmp);
120 ipar = of_irq_find_parent(ipar);
124 pr_debug(" -> no parent found !\n");
128 pr_debug("of_irq_parse_raw: ipar=%s, size=%d\n", of_node_full_name(ipar), intsize);
130 if (ointsize != intsize)
133 /* Look for this #address-cells. We have to implement the old linux
134 * trick of looking for the parent here as some device-trees rely on it
136 old = of_node_get(ipar);
138 tmp = of_get_property(old, "#address-cells", NULL);
139 tnode = of_get_parent(old);
142 } while (old && tmp == NULL);
145 addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
147 pr_debug(" -> addrsize=%d\n", addrsize);
149 /* Now start the actual "proper" walk of the interrupt tree */
150 while (ipar != NULL) {
151 /* Now check if cursor is an interrupt-controller and if it is
154 if (of_get_property(ipar, "interrupt-controller", NULL) !=
156 pr_debug(" -> got it !\n");
157 for (i = 0; i < intsize; i++)
159 of_read_number(intspec +i, 1);
160 out_irq->args_count = intsize;
166 /* Now look for an interrupt-map */
167 imap = of_get_property(ipar, "interrupt-map", &imaplen);
168 /* No interrupt map, check for an interrupt parent */
170 pr_debug(" -> no map, getting parent\n");
171 newpar = of_irq_find_parent(ipar);
174 imaplen /= sizeof(u32);
176 /* Look for a mask */
177 imask = of_get_property(ipar, "interrupt-map-mask", NULL);
179 /* If we were passed no "reg" property and we attempt to parse
180 * an interrupt-map, then #address-cells must be 0.
183 if (addr == NULL && addrsize != 0) {
184 pr_debug(" -> no reg passed in when needed !\n");
188 /* Parse interrupt-map */
190 while (imaplen > (addrsize + intsize + 1) && !match) {
191 /* Compare specifiers */
193 for (i = 0; i < addrsize && match; ++i) {
194 __be32 mask = imask ? imask[i]
195 : cpu_to_be32(0xffffffffu);
196 match = ((addr[i] ^ imap[i]) & mask) == 0;
198 for (; i < (addrsize + intsize) && match; ++i) {
199 __be32 mask = imask ? imask[i]
200 : cpu_to_be32(0xffffffffu);
202 ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
204 imap += addrsize + intsize;
205 imaplen -= addrsize + intsize;
207 pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
209 /* Get the interrupt parent */
210 if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
211 newpar = of_node_get(of_irq_dflt_pic);
213 newpar = of_find_node_by_phandle(be32_to_cpup(imap));
217 /* Check if not found */
218 if (newpar == NULL) {
219 pr_debug(" -> imap parent not found !\n");
223 /* Get #interrupt-cells and #address-cells of new
226 tmp = of_get_property(newpar, "#interrupt-cells", NULL);
228 pr_debug(" -> parent lacks #interrupt-cells!\n");
231 newintsize = be32_to_cpu(*tmp);
232 tmp = of_get_property(newpar, "#address-cells", NULL);
233 newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
235 pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
236 newintsize, newaddrsize);
238 /* Check for malformed properties */
239 if (imaplen < (newaddrsize + newintsize))
242 imap += newaddrsize + newintsize;
243 imaplen -= newaddrsize + newintsize;
245 pr_debug(" -> imaplen=%d\n", imaplen);
251 old = of_node_get(newpar);
252 addrsize = newaddrsize;
253 intsize = newintsize;
254 intspec = imap - intsize;
255 addr = intspec - addrsize;
258 /* Iterate again with new parent */
259 pr_debug(" -> new parent: %s\n", of_node_full_name(newpar));
271 EXPORT_SYMBOL_GPL(of_irq_parse_raw);
274 * of_irq_parse_one - Resolve an interrupt for a device
275 * @device: the device whose interrupt is to be resolved
276 * @index: index of the interrupt to resolve
277 * @out_irq: structure of_irq filled by this function
279 * This function resolves an interrupt, walking the tree, for a given
280 * device-tree node. It's the high level pendant to of_irq_parse_raw().
282 int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
284 struct device_node *p;
285 const __be32 *intspec, *tmp, *addr;
289 pr_debug("of_irq_parse_one: dev=%s, index=%d\n", of_node_full_name(device), index);
291 /* OldWorld mac stuff is "special", handle out of line */
292 if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
293 return of_irq_parse_oldworld(device, index, out_irq);
295 /* Get the interrupts property */
296 intspec = of_get_property(device, "interrupts", &intlen);
299 intlen /= sizeof(*intspec);
301 pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
303 /* Get the reg property (if any) */
304 addr = of_get_property(device, "reg", NULL);
306 /* Look for the interrupt parent. */
307 p = of_irq_find_parent(device);
311 /* Get size of interrupt specifier */
312 tmp = of_get_property(p, "#interrupt-cells", NULL);
315 intsize = be32_to_cpu(*tmp);
317 pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
320 if ((index + 1) * intsize > intlen)
323 /* Get new specifier and map it */
324 res = of_irq_parse_raw(p, intspec + index * intsize, intsize,
330 EXPORT_SYMBOL_GPL(of_irq_parse_one);
333 * of_irq_to_resource - Decode a node's IRQ and return it as a resource
334 * @dev: pointer to device tree node
335 * @index: zero-based index of the irq
336 * @r: pointer to resource structure to return result into.
338 int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
340 int irq = irq_of_parse_and_map(dev, index);
342 /* Only dereference the resource if both the
343 * resource and the irq are valid. */
345 const char *name = NULL;
347 memset(r, 0, sizeof(*r));
349 * Get optional "interrupts-names" property to add a name
352 of_property_read_string_index(dev, "interrupt-names", index,
355 r->start = r->end = irq;
356 r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
357 r->name = name ? name : of_node_full_name(dev);
362 EXPORT_SYMBOL_GPL(of_irq_to_resource);
365 * of_irq_count - Count the number of IRQs a node uses
366 * @dev: pointer to device tree node
368 int of_irq_count(struct device_node *dev)
372 while (of_irq_to_resource(dev, nr, NULL))
379 * of_irq_to_resource_table - Fill in resource table with node's IRQ info
380 * @dev: pointer to device tree node
381 * @res: array of resources to fill in
382 * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
384 * Returns the size of the filled in table (up to @nr_irqs).
386 int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
391 for (i = 0; i < nr_irqs; i++, res++)
392 if (!of_irq_to_resource(dev, i, res))
397 EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
400 struct list_head list;
401 struct device_node *dev;
402 struct device_node *interrupt_parent;
406 * of_irq_init - Scan and init matching interrupt controllers in DT
407 * @matches: 0 terminated array of nodes to match and init function to call
409 * This function scans the device tree for matching interrupt controller nodes,
410 * and calls their initialization functions in order with parents first.
412 void __init of_irq_init(const struct of_device_id *matches)
414 struct device_node *np, *parent = NULL;
415 struct intc_desc *desc, *temp_desc;
416 struct list_head intc_desc_list, intc_parent_list;
418 INIT_LIST_HEAD(&intc_desc_list);
419 INIT_LIST_HEAD(&intc_parent_list);
421 for_each_matching_node(np, matches) {
422 if (!of_find_property(np, "interrupt-controller", NULL))
425 * Here, we allocate and populate an intc_desc with the node
426 * pointer, interrupt-parent device_node etc.
428 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
433 desc->interrupt_parent = of_irq_find_parent(np);
434 if (desc->interrupt_parent == np)
435 desc->interrupt_parent = NULL;
436 list_add_tail(&desc->list, &intc_desc_list);
440 * The root irq controller is the one without an interrupt-parent.
441 * That one goes first, followed by the controllers that reference it,
442 * followed by the ones that reference the 2nd level controllers, etc.
444 while (!list_empty(&intc_desc_list)) {
446 * Process all controllers with the current 'parent'.
447 * First pass will be looking for NULL as the parent.
448 * The assumption is that NULL parent means a root controller.
450 list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
451 const struct of_device_id *match;
453 of_irq_init_cb_t irq_init_cb;
455 if (desc->interrupt_parent != parent)
458 list_del(&desc->list);
459 match = of_match_node(matches, desc->dev);
460 if (WARN(!match->data,
461 "of_irq_init: no init function for %s\n",
462 match->compatible)) {
467 pr_debug("of_irq_init: init %s @ %p, parent %p\n",
469 desc->dev, desc->interrupt_parent);
470 irq_init_cb = (of_irq_init_cb_t)match->data;
471 ret = irq_init_cb(desc->dev, desc->interrupt_parent);
478 * This one is now set up; add it to the parent list so
479 * its children can get processed in a subsequent pass.
481 list_add_tail(&desc->list, &intc_parent_list);
484 /* Get the next pending parent that might have children */
485 desc = list_first_entry_or_null(&intc_parent_list,
486 typeof(*desc), list);
488 pr_err("of_irq_init: children remain, but no parents\n");
491 list_del(&desc->list);
496 list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
497 list_del(&desc->list);
501 list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
502 list_del(&desc->list);