irq.c 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *  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)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * This file contains the code used to make IRQ descriptions in the
 * device tree to actual irq numbers on an interrupt controller
 * driver.
 */

#include <linux/errno.h>
R
Rob Herring 已提交
22
#include <linux/list.h>
23 24 25 26
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_irq.h>
#include <linux/string.h>
R
Rob Herring 已提交
27
#include <linux/slab.h>
28 29 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 33
 * @index: Index of the interrupt to map
 *
34
 * This function is a wrapper that chains of_irq_parse_one() and
35 36 37 38
 * irq_create_of_mapping() to make things easier to callers
 */
unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
{
39
	struct of_phandle_args oirq;
40

41
	if (of_irq_parse_one(dev, index, &oirq))
A
Anton Vorontsov 已提交
42
		return 0;
43

44
	return irq_create_of_mapping(&oirq);
45 46
}
EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
G
Grant Likely 已提交
47 48 49 50 51 52 53 54

/**
 * 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.
 */
55
struct device_node *of_irq_find_parent(struct device_node *child)
G
Grant Likely 已提交
56
{
57
	struct device_node *p;
G
Grant Likely 已提交
58
	const __be32 *parp;
G
Grant Likely 已提交
59

60
	if (!of_node_get(child))
G
Grant Likely 已提交
61 62 63
		return NULL;

	do {
64
		parp = of_get_property(child, "interrupt-parent", NULL);
G
Grant Likely 已提交
65
		if (parp == NULL)
66
			p = of_get_parent(child);
G
Grant Likely 已提交
67 68 69 70
		else {
			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
				p = of_node_get(of_irq_dflt_pic);
			else
G
Grant Likely 已提交
71
				p = of_find_node_by_phandle(be32_to_cpup(parp));
G
Grant Likely 已提交
72
		}
73 74
		of_node_put(child);
		child = p;
G
Grant Likely 已提交
75 76
	} while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);

77
	return p;
G
Grant Likely 已提交
78 79 80
}

/**
81
 * of_irq_parse_raw - Low level interrupt tree parsing
G
Grant Likely 已提交
82
 * @parent:	the device interrupt parent
83 84
 * @addr:	address specifier (start of "reg" property of the device) in be32 format
 * @out_irq:	structure of_irq updated by this function
G
Grant Likely 已提交
85 86 87 88 89 90
 *
 * 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
91 92 93
 * 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.
G
Grant Likely 已提交
94
 */
95
int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
G
Grant Likely 已提交
96 97
{
	struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
98
	__be32 initial_match_array[MAX_PHANDLE_ARGS];
99
	const __be32 *match_array = initial_match_array;
100
	const __be32 *tmp, *imap, *imask, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
G
Grant Likely 已提交
101 102 103
	u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
	int imaplen, match, i;

104 105 106
#ifdef DEBUG
	of_print_phandle_args("of_irq_parse_raw: ", out_irq);
#endif
G
Grant Likely 已提交
107

108
	ipar = of_node_get(out_irq->np);
G
Grant Likely 已提交
109 110 111 112 113 114 115 116

	/* 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 {
		tmp = of_get_property(ipar, "#interrupt-cells", NULL);
		if (tmp != NULL) {
R
Rob Herring 已提交
117
			intsize = be32_to_cpu(*tmp);
G
Grant Likely 已提交
118 119 120 121 122 123 124 125 126 127 128
			break;
		}
		tnode = ipar;
		ipar = of_irq_find_parent(ipar);
		of_node_put(tnode);
	} while (ipar);
	if (ipar == NULL) {
		pr_debug(" -> no parent found !\n");
		goto fail;
	}

129
	pr_debug("of_irq_parse_raw: ipar=%s, size=%d\n", of_node_full_name(ipar), intsize);
G
Grant Likely 已提交
130

131
	if (out_irq->args_count != intsize)
G
Grant Likely 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145
		return -EINVAL;

	/* 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;
R
Rob Herring 已提交
146
	addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
G
Grant Likely 已提交
147 148 149

	pr_debug(" -> addrsize=%d\n", addrsize);

150 151 152 153
	/* Range check so that the temporary buffer doesn't overflow */
	if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS))
		goto fail;

154 155
	/* Precalculate the match array - this simplifies match loop */
	for (i = 0; i < addrsize; i++)
156
		initial_match_array[i] = addr ? addr[i] : 0;
157 158 159
	for (i = 0; i < intsize; i++)
		initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);

G
Grant Likely 已提交
160 161 162 163 164 165 166 167 168 169 170
	/* 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
		 */
		if (of_get_property(ipar, "interrupt-controller", NULL) !=
				NULL) {
			pr_debug(" -> got it !\n");
			return 0;
		}

171 172 173 174 175 176 177 178 179
		/*
		 * 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 !\n");
			goto fail;
		}

G
Grant Likely 已提交
180 181 182 183 184 185 186 187 188 189 190 191
		/* 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\n");
			newpar = of_irq_find_parent(ipar);
			goto skiplevel;
		}
		imaplen /= sizeof(u32);

		/* Look for a mask */
		imask = of_get_property(ipar, "interrupt-map-mask", NULL);
192 193
		if (!imask)
			imask = dummy_imask;
G
Grant Likely 已提交
194 195 196 197 198 199

		/* Parse interrupt-map */
		match = 0;
		while (imaplen > (addrsize + intsize + 1) && !match) {
			/* Compare specifiers */
			match = 1;
200
			for (i = 0; i < (addrsize + intsize); i++, imaplen--)
201
				match &= !((match_array[i] ^ *imap++) & imask[i]);
G
Grant Likely 已提交
202 203 204 205 206 207 208

			pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);

			/* Get the interrupt parent */
			if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
				newpar = of_node_get(of_irq_dflt_pic);
			else
G
Grant Likely 已提交
209
				newpar = of_find_node_by_phandle(be32_to_cpup(imap));
G
Grant Likely 已提交
210 211 212 213 214 215 216 217 218
			imap++;
			--imaplen;

			/* Check if not found */
			if (newpar == NULL) {
				pr_debug(" -> imap parent not found !\n");
				goto fail;
			}

219 220 221
			if (!of_device_is_available(newpar))
				match = 0;

G
Grant Likely 已提交
222 223 224 225 226 227 228 229
			/* Get #interrupt-cells and #address-cells of new
			 * parent
			 */
			tmp = of_get_property(newpar, "#interrupt-cells", NULL);
			if (tmp == NULL) {
				pr_debug(" -> parent lacks #interrupt-cells!\n");
				goto fail;
			}
R
Rob Herring 已提交
230
			newintsize = be32_to_cpu(*tmp);
G
Grant Likely 已提交
231
			tmp = of_get_property(newpar, "#address-cells", NULL);
R
Rob Herring 已提交
232
			newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
G
Grant Likely 已提交
233 234 235 236 237

			pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
			    newintsize, newaddrsize);

			/* Check for malformed properties */
238 239
			if (WARN_ON(newaddrsize + newintsize > MAX_PHANDLE_ARGS))
				goto fail;
G
Grant Likely 已提交
240 241 242 243 244 245 246 247 248 249 250
			if (imaplen < (newaddrsize + newintsize))
				goto fail;

			imap += newaddrsize + newintsize;
			imaplen -= newaddrsize + newintsize;

			pr_debug(" -> imaplen=%d\n", imaplen);
		}
		if (!match)
			goto fail;

251 252 253 254
		/*
		 * Successfully parsed an interrrupt-map translation; copy new
		 * interrupt specifier into the out_irq structure
		 */
255
		out_irq->np = newpar;
256 257 258 259 260

		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;
G
Grant Likely 已提交
261 262 263 264
		addrsize = newaddrsize;

	skiplevel:
		/* Iterate again with new parent */
265
		pr_debug(" -> new parent: %s\n", of_node_full_name(newpar));
G
Grant Likely 已提交
266 267 268 269 270 271 272 273 274 275
		of_node_put(ipar);
		ipar = newpar;
		newpar = NULL;
	}
 fail:
	of_node_put(ipar);
	of_node_put(newpar);

	return -EINVAL;
}
276
EXPORT_SYMBOL_GPL(of_irq_parse_raw);
G
Grant Likely 已提交
277 278

/**
279
 * of_irq_parse_one - Resolve an interrupt for a device
G
Grant Likely 已提交
280 281 282 283
 * @device: the device whose interrupt is to be resolved
 * @index: index of the interrupt to resolve
 * @out_irq: structure of_irq filled by this function
 *
284 285 286
 * 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.
G
Grant Likely 已提交
287
 */
288
int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
G
Grant Likely 已提交
289 290
{
	struct device_node *p;
291
	const __be32 *intspec, *tmp, *addr;
G
Grant Likely 已提交
292
	u32 intsize, intlen;
293
	int i, res = -EINVAL;
G
Grant Likely 已提交
294

295
	pr_debug("of_irq_parse_one: dev=%s, index=%d\n", of_node_full_name(device), index);
G
Grant Likely 已提交
296 297 298

	/* OldWorld mac stuff is "special", handle out of line */
	if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
299
		return of_irq_parse_oldworld(device, index, out_irq);
G
Grant Likely 已提交
300

301 302 303
	/* Get the reg property (if any) */
	addr = of_get_property(device, "reg", NULL);

G
Grant Likely 已提交
304 305
	/* Get the interrupts property */
	intspec = of_get_property(device, "interrupts", &intlen);
306 307 308 309 310 311 312 313
	if (intspec == NULL) {
		/* Try the new-style interrupts-extended */
		res = of_parse_phandle_with_args(device, "interrupts-extended",
						"#interrupt-cells", index, out_irq);
		if (res)
			return -EINVAL;
		return of_irq_parse_raw(addr, out_irq);
	}
314
	intlen /= sizeof(*intspec);
G
Grant Likely 已提交
315

316
	pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
G
Grant Likely 已提交
317 318 319 320 321 322 323 324 325 326

	/* Look for the interrupt parent. */
	p = of_irq_find_parent(device);
	if (p == NULL)
		return -EINVAL;

	/* Get size of interrupt specifier */
	tmp = of_get_property(p, "#interrupt-cells", NULL);
	if (tmp == NULL)
		goto out;
R
Rob Herring 已提交
327
	intsize = be32_to_cpu(*tmp);
G
Grant Likely 已提交
328 329 330 331 332 333 334

	pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);

	/* Check index */
	if ((index + 1) * intsize > intlen)
		goto out;

335 336 337 338 339 340 341 342 343
	/* Copy intspec into irq structure */
	intspec += index * intsize;
	out_irq->np = p;
	out_irq->args_count = intsize;
	for (i = 0; i < intsize; i++)
		out_irq->args[i] = be32_to_cpup(intspec++);

	/* Check if there are any interrupt-map translations to process */
	res = of_irq_parse_raw(addr, out_irq);
G
Grant Likely 已提交
344 345 346 347
 out:
	of_node_put(p);
	return res;
}
348
EXPORT_SYMBOL_GPL(of_irq_parse_one);
G
Grant Likely 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361

/**
 * 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)
{
	int irq = irq_of_parse_and_map(dev, index);

	/* Only dereference the resource if both the
	 * resource and the irq are valid. */
A
Anton Vorontsov 已提交
362
	if (r && irq) {
363 364
		const char *name = NULL;

365
		memset(r, 0, sizeof(*r));
366 367 368 369 370 371 372
		/*
		 * Get optional "interrupts-names" property to add a name
		 * to the resource.
		 */
		of_property_read_string_index(dev, "interrupt-names", index,
					      &name);

G
Grant Likely 已提交
373
		r->start = r->end = irq;
374
		r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
375
		r->name = name ? name : of_node_full_name(dev);
G
Grant Likely 已提交
376 377 378 379 380
	}

	return irq;
}
EXPORT_SYMBOL_GPL(of_irq_to_resource);
381 382 383 384 385 386 387

/**
 * 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)
{
T
Thierry Reding 已提交
388
	struct of_phandle_args irq;
389 390
	int nr = 0;

T
Thierry Reding 已提交
391
	while (of_irq_parse_one(dev, nr, &irq) == 0)
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
		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++)
A
Anton Vorontsov 已提交
411
		if (!of_irq_to_resource(dev, i, res))
412 413 414 415
			break;

	return i;
}
416
EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
R
Rob Herring 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

struct intc_desc {
	struct list_head	list;
	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)
{
	struct device_node *np, *parent = NULL;
	struct intc_desc *desc, *temp_desc;
	struct list_head intc_desc_list, intc_parent_list;

	INIT_LIST_HEAD(&intc_desc_list);
	INIT_LIST_HEAD(&intc_parent_list);

	for_each_matching_node(np, matches) {
441 442
		if (!of_find_property(np, "interrupt-controller", NULL) ||
				!of_device_is_available(np))
R
Rob Herring 已提交
443 444 445 446 447 448 449 450 451 452 453
			continue;
		/*
		 * Here, we allocate and populate an intc_desc with the node
		 * pointer, interrupt-parent device_node etc.
		 */
		desc = kzalloc(sizeof(*desc), GFP_KERNEL);
		if (WARN_ON(!desc))
			goto err;

		desc->dev = np;
		desc->interrupt_parent = of_irq_find_parent(np);
454 455
		if (desc->interrupt_parent == np)
			desc->interrupt_parent = NULL;
R
Rob Herring 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
		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) {
			const struct of_device_id *match;
			int ret;
			of_irq_init_cb_t irq_init_cb;

			if (desc->interrupt_parent != parent)
				continue;

			list_del(&desc->list);
			match = of_match_node(matches, desc->dev);
			if (WARN(!match->data,
			    "of_irq_init: no init function for %s\n",
			    match->compatible)) {
				kfree(desc);
				continue;
			}

			pr_debug("of_irq_init: init %s @ %p, parent %p\n",
				 match->compatible,
				 desc->dev, desc->interrupt_parent);
K
Kim Phillips 已提交
490
			irq_init_cb = (of_irq_init_cb_t)match->data;
R
Rob Herring 已提交
491 492 493 494 495 496 497 498 499 500 501 502 503 504
			ret = irq_init_cb(desc->dev, desc->interrupt_parent);
			if (ret) {
				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 */
505 506 507
		desc = list_first_entry_or_null(&intc_parent_list,
						typeof(*desc), list);
		if (!desc) {
R
Rob Herring 已提交
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
			pr_err("of_irq_init: children remain, but no parents\n");
			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);
		kfree(desc);
	}
}