irq.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  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)
  10. *
  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.
  15. *
  16. * This file contains the code used to make IRQ descriptions in the
  17. * device tree to actual irq numbers on an interrupt controller
  18. * driver.
  19. */
  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. * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
  29. * @dev: Device node of the device whose interrupt is to be mapped
  30. * @index: Index of the interrupt to map
  31. *
  32. * This function is a wrapper that chains of_irq_parse_one() and
  33. * irq_create_of_mapping() to make things easier to callers
  34. */
  35. unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  36. {
  37. struct of_phandle_args oirq;
  38. if (of_irq_parse_one(dev, index, &oirq))
  39. return 0;
  40. return irq_create_of_mapping(&oirq);
  41. }
  42. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  43. /**
  44. * of_irq_find_parent - Given a device node, find its interrupt parent node
  45. * @child: pointer to device node
  46. *
  47. * Returns a pointer to the interrupt parent node, or NULL if the interrupt
  48. * parent could not be determined.
  49. */
  50. struct device_node *of_irq_find_parent(struct device_node *child)
  51. {
  52. struct device_node *p;
  53. const __be32 *parp;
  54. if (!of_node_get(child))
  55. return NULL;
  56. do {
  57. parp = of_get_property(child, "interrupt-parent", NULL);
  58. if (parp == NULL)
  59. p = of_get_parent(child);
  60. else {
  61. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  62. p = of_node_get(of_irq_dflt_pic);
  63. else
  64. p = of_find_node_by_phandle(be32_to_cpup(parp));
  65. }
  66. of_node_put(child);
  67. child = p;
  68. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  69. return p;
  70. }
  71. /**
  72. * of_irq_parse_raw - Low level interrupt tree parsing
  73. * @parent: the device interrupt parent
  74. * @addr: address specifier (start of "reg" property of the device) in be32 format
  75. * @out_irq: structure of_irq updated by this function
  76. *
  77. * Returns 0 on success and a negative number on error
  78. *
  79. * This function is a low-level interrupt tree walking function. It
  80. * can be used to do a partial walk with synthetized reg and interrupts
  81. * properties, for example when resolving PCI interrupts when no device
  82. * node exist for the parent. It takes an interrupt specifier structure as
  83. * input, walks the tree looking for any interrupt-map properties, translates
  84. * the specifier for each map, and then returns the translated map.
  85. */
  86. int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
  87. {
  88. struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
  89. __be32 initial_match_array[MAX_PHANDLE_ARGS];
  90. const __be32 *match_array = initial_match_array;
  91. const __be32 *tmp, *imap, *imask, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = ~0 };
  92. u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
  93. int imaplen, match, i;
  94. #ifdef DEBUG
  95. of_print_phandle_args("of_irq_parse_raw: ", out_irq);
  96. #endif
  97. ipar = of_node_get(out_irq->np);
  98. /* First get the #interrupt-cells property of the current cursor
  99. * that tells us how to interpret the passed-in intspec. If there
  100. * is none, we are nice and just walk up the tree
  101. */
  102. do {
  103. tmp = of_get_property(ipar, "#interrupt-cells", NULL);
  104. if (tmp != NULL) {
  105. intsize = be32_to_cpu(*tmp);
  106. break;
  107. }
  108. tnode = ipar;
  109. ipar = of_irq_find_parent(ipar);
  110. of_node_put(tnode);
  111. } while (ipar);
  112. if (ipar == NULL) {
  113. pr_debug(" -> no parent found !\n");
  114. goto fail;
  115. }
  116. pr_debug("of_irq_parse_raw: ipar=%s, size=%d\n", of_node_full_name(ipar), intsize);
  117. if (out_irq->args_count != intsize)
  118. return -EINVAL;
  119. /* Look for this #address-cells. We have to implement the old linux
  120. * trick of looking for the parent here as some device-trees rely on it
  121. */
  122. old = of_node_get(ipar);
  123. do {
  124. tmp = of_get_property(old, "#address-cells", NULL);
  125. tnode = of_get_parent(old);
  126. of_node_put(old);
  127. old = tnode;
  128. } while (old && tmp == NULL);
  129. of_node_put(old);
  130. old = NULL;
  131. addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
  132. pr_debug(" -> addrsize=%d\n", addrsize);
  133. /* Range check so that the temporary buffer doesn't overflow */
  134. if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS))
  135. goto fail;
  136. /* Precalculate the match array - this simplifies match loop */
  137. for (i = 0; i < addrsize; i++)
  138. initial_match_array[i] = addr ? addr[i] : 0;
  139. for (i = 0; i < intsize; i++)
  140. initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);
  141. /* Now start the actual "proper" walk of the interrupt tree */
  142. while (ipar != NULL) {
  143. /* Now check if cursor is an interrupt-controller and if it is
  144. * then we are done
  145. */
  146. if (of_get_property(ipar, "interrupt-controller", NULL) !=
  147. NULL) {
  148. pr_debug(" -> got it !\n");
  149. return 0;
  150. }
  151. /*
  152. * interrupt-map parsing does not work without a reg
  153. * property when #address-cells != 0
  154. */
  155. if (addrsize && !addr) {
  156. pr_debug(" -> no reg passed in when needed !\n");
  157. goto fail;
  158. }
  159. /* Now look for an interrupt-map */
  160. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  161. /* No interrupt map, check for an interrupt parent */
  162. if (imap == NULL) {
  163. pr_debug(" -> no map, getting parent\n");
  164. newpar = of_irq_find_parent(ipar);
  165. goto skiplevel;
  166. }
  167. imaplen /= sizeof(u32);
  168. /* Look for a mask */
  169. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  170. if (!imask)
  171. imask = dummy_imask;
  172. /* Parse interrupt-map */
  173. match = 0;
  174. while (imaplen > (addrsize + intsize + 1) && !match) {
  175. /* Compare specifiers */
  176. match = 1;
  177. for (i = 0; i < (addrsize + intsize); i++, imaplen--)
  178. match &= !((match_array[i] ^ *imap++) & imask[i]);
  179. pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
  180. /* Get the interrupt parent */
  181. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  182. newpar = of_node_get(of_irq_dflt_pic);
  183. else
  184. newpar = of_find_node_by_phandle(be32_to_cpup(imap));
  185. imap++;
  186. --imaplen;
  187. /* Check if not found */
  188. if (newpar == NULL) {
  189. pr_debug(" -> imap parent not found !\n");
  190. goto fail;
  191. }
  192. if (!of_device_is_available(newpar))
  193. match = 0;
  194. /* Get #interrupt-cells and #address-cells of new
  195. * parent
  196. */
  197. tmp = of_get_property(newpar, "#interrupt-cells", NULL);
  198. if (tmp == NULL) {
  199. pr_debug(" -> parent lacks #interrupt-cells!\n");
  200. goto fail;
  201. }
  202. newintsize = be32_to_cpu(*tmp);
  203. tmp = of_get_property(newpar, "#address-cells", NULL);
  204. newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
  205. pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
  206. newintsize, newaddrsize);
  207. /* Check for malformed properties */
  208. if (WARN_ON(newaddrsize + newintsize > MAX_PHANDLE_ARGS))
  209. goto fail;
  210. if (imaplen < (newaddrsize + newintsize))
  211. goto fail;
  212. imap += newaddrsize + newintsize;
  213. imaplen -= newaddrsize + newintsize;
  214. pr_debug(" -> imaplen=%d\n", imaplen);
  215. }
  216. if (!match)
  217. goto fail;
  218. /*
  219. * Successfully parsed an interrrupt-map translation; copy new
  220. * interrupt specifier into the out_irq structure
  221. */
  222. match_array = imap - newaddrsize - newintsize;
  223. for (i = 0; i < newintsize; i++)
  224. out_irq->args[i] = be32_to_cpup(imap - newintsize + i);
  225. out_irq->args_count = intsize = newintsize;
  226. addrsize = newaddrsize;
  227. skiplevel:
  228. /* Iterate again with new parent */
  229. out_irq->np = newpar;
  230. pr_debug(" -> new parent: %s\n", of_node_full_name(newpar));
  231. of_node_put(ipar);
  232. ipar = newpar;
  233. newpar = NULL;
  234. }
  235. fail:
  236. of_node_put(ipar);
  237. of_node_put(newpar);
  238. return -EINVAL;
  239. }
  240. EXPORT_SYMBOL_GPL(of_irq_parse_raw);
  241. /**
  242. * of_irq_parse_one - Resolve an interrupt for a device
  243. * @device: the device whose interrupt is to be resolved
  244. * @index: index of the interrupt to resolve
  245. * @out_irq: structure of_irq filled by this function
  246. *
  247. * This function resolves an interrupt for a node by walking the interrupt tree,
  248. * finding which interrupt controller node it is attached to, and returning the
  249. * interrupt specifier that can be used to retrieve a Linux IRQ number.
  250. */
  251. int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
  252. {
  253. struct device_node *p;
  254. const __be32 *intspec, *tmp, *addr;
  255. u32 intsize, intlen;
  256. int i, res;
  257. pr_debug("of_irq_parse_one: dev=%s, index=%d\n", of_node_full_name(device), index);
  258. /* OldWorld mac stuff is "special", handle out of line */
  259. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  260. return of_irq_parse_oldworld(device, index, out_irq);
  261. /* Get the reg property (if any) */
  262. addr = of_get_property(device, "reg", NULL);
  263. /* Try the new-style interrupts-extended first */
  264. res = of_parse_phandle_with_args(device, "interrupts-extended",
  265. "#interrupt-cells", index, out_irq);
  266. if (!res)
  267. return of_irq_parse_raw(addr, out_irq);
  268. /* Get the interrupts property */
  269. intspec = of_get_property(device, "interrupts", &intlen);
  270. if (intspec == NULL)
  271. return -EINVAL;
  272. intlen /= sizeof(*intspec);
  273. pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
  274. /* Look for the interrupt parent. */
  275. p = of_irq_find_parent(device);
  276. if (p == NULL)
  277. return -EINVAL;
  278. /* Get size of interrupt specifier */
  279. tmp = of_get_property(p, "#interrupt-cells", NULL);
  280. if (tmp == NULL) {
  281. res = -EINVAL;
  282. goto out;
  283. }
  284. intsize = be32_to_cpu(*tmp);
  285. pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
  286. /* Check index */
  287. if ((index + 1) * intsize > intlen) {
  288. res = -EINVAL;
  289. goto out;
  290. }
  291. /* Copy intspec into irq structure */
  292. intspec += index * intsize;
  293. out_irq->np = p;
  294. out_irq->args_count = intsize;
  295. for (i = 0; i < intsize; i++)
  296. out_irq->args[i] = be32_to_cpup(intspec++);
  297. /* Check if there are any interrupt-map translations to process */
  298. res = of_irq_parse_raw(addr, out_irq);
  299. out:
  300. of_node_put(p);
  301. return res;
  302. }
  303. EXPORT_SYMBOL_GPL(of_irq_parse_one);
  304. /**
  305. * of_irq_to_resource - Decode a node's IRQ and return it as a resource
  306. * @dev: pointer to device tree node
  307. * @index: zero-based index of the irq
  308. * @r: pointer to resource structure to return result into.
  309. */
  310. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  311. {
  312. int irq = irq_of_parse_and_map(dev, index);
  313. /* Only dereference the resource if both the
  314. * resource and the irq are valid. */
  315. if (r && irq) {
  316. const char *name = NULL;
  317. memset(r, 0, sizeof(*r));
  318. /*
  319. * Get optional "interrupt-names" property to add a name
  320. * to the resource.
  321. */
  322. of_property_read_string_index(dev, "interrupt-names", index,
  323. &name);
  324. r->start = r->end = irq;
  325. r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
  326. r->name = name ? name : of_node_full_name(dev);
  327. }
  328. return irq;
  329. }
  330. EXPORT_SYMBOL_GPL(of_irq_to_resource);
  331. /**
  332. * of_irq_get - Decode a node's IRQ and return it as a Linux irq number
  333. * @dev: pointer to device tree node
  334. * @index: zero-based index of the irq
  335. *
  336. * Returns Linux irq number on success, or -EPROBE_DEFER if the irq domain
  337. * is not yet created.
  338. *
  339. */
  340. int of_irq_get(struct device_node *dev, int index)
  341. {
  342. int rc;
  343. struct of_phandle_args oirq;
  344. struct irq_domain *domain;
  345. rc = of_irq_parse_one(dev, index, &oirq);
  346. if (rc)
  347. return rc;
  348. domain = irq_find_host(oirq.np);
  349. if (!domain)
  350. return -EPROBE_DEFER;
  351. return irq_create_of_mapping(&oirq);
  352. }
  353. EXPORT_SYMBOL_GPL(of_irq_get);
  354. /**
  355. * of_irq_get_byname - Decode a node's IRQ and return it as a Linux irq number
  356. * @dev: pointer to device tree node
  357. * @name: irq name
  358. *
  359. * Returns Linux irq number on success, or -EPROBE_DEFER if the irq domain
  360. * is not yet created, or error code in case of any other failure.
  361. */
  362. int of_irq_get_byname(struct device_node *dev, const char *name)
  363. {
  364. int index;
  365. if (unlikely(!name))
  366. return -EINVAL;
  367. index = of_property_match_string(dev, "interrupt-names", name);
  368. if (index < 0)
  369. return index;
  370. return of_irq_get(dev, index);
  371. }
  372. /**
  373. * of_irq_count - Count the number of IRQs a node uses
  374. * @dev: pointer to device tree node
  375. */
  376. int of_irq_count(struct device_node *dev)
  377. {
  378. struct of_phandle_args irq;
  379. int nr = 0;
  380. while (of_irq_parse_one(dev, nr, &irq) == 0)
  381. nr++;
  382. return nr;
  383. }
  384. /**
  385. * of_irq_to_resource_table - Fill in resource table with node's IRQ info
  386. * @dev: pointer to device tree node
  387. * @res: array of resources to fill in
  388. * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
  389. *
  390. * Returns the size of the filled in table (up to @nr_irqs).
  391. */
  392. int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
  393. int nr_irqs)
  394. {
  395. int i;
  396. for (i = 0; i < nr_irqs; i++, res++)
  397. if (!of_irq_to_resource(dev, i, res))
  398. break;
  399. return i;
  400. }
  401. EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
  402. struct of_intc_desc {
  403. struct list_head list;
  404. struct device_node *dev;
  405. struct device_node *interrupt_parent;
  406. };
  407. /**
  408. * of_irq_init - Scan and init matching interrupt controllers in DT
  409. * @matches: 0 terminated array of nodes to match and init function to call
  410. *
  411. * This function scans the device tree for matching interrupt controller nodes,
  412. * and calls their initialization functions in order with parents first.
  413. */
  414. void __init of_irq_init(const struct of_device_id *matches)
  415. {
  416. struct device_node *np, *parent = NULL;
  417. struct of_intc_desc *desc, *temp_desc;
  418. struct list_head intc_desc_list, intc_parent_list;
  419. INIT_LIST_HEAD(&intc_desc_list);
  420. INIT_LIST_HEAD(&intc_parent_list);
  421. for_each_matching_node(np, matches) {
  422. if (!of_find_property(np, "interrupt-controller", NULL) ||
  423. !of_device_is_available(np))
  424. continue;
  425. /*
  426. * Here, we allocate and populate an of_intc_desc with the node
  427. * pointer, interrupt-parent device_node etc.
  428. */
  429. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  430. if (WARN_ON(!desc))
  431. goto err;
  432. desc->dev = np;
  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);
  437. }
  438. /*
  439. * The root irq controller is the one without an interrupt-parent.
  440. * That one goes first, followed by the controllers that reference it,
  441. * followed by the ones that reference the 2nd level controllers, etc.
  442. */
  443. while (!list_empty(&intc_desc_list)) {
  444. /*
  445. * Process all controllers with the current 'parent'.
  446. * First pass will be looking for NULL as the parent.
  447. * The assumption is that NULL parent means a root controller.
  448. */
  449. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  450. const struct of_device_id *match;
  451. int ret;
  452. of_irq_init_cb_t irq_init_cb;
  453. if (desc->interrupt_parent != parent)
  454. continue;
  455. list_del(&desc->list);
  456. match = of_match_node(matches, desc->dev);
  457. if (WARN(!match->data,
  458. "of_irq_init: no init function for %s\n",
  459. match->compatible)) {
  460. kfree(desc);
  461. continue;
  462. }
  463. pr_debug("of_irq_init: init %s @ %p, parent %p\n",
  464. match->compatible,
  465. desc->dev, desc->interrupt_parent);
  466. irq_init_cb = (of_irq_init_cb_t)match->data;
  467. ret = irq_init_cb(desc->dev, desc->interrupt_parent);
  468. if (ret) {
  469. kfree(desc);
  470. continue;
  471. }
  472. /*
  473. * This one is now set up; add it to the parent list so
  474. * its children can get processed in a subsequent pass.
  475. */
  476. list_add_tail(&desc->list, &intc_parent_list);
  477. }
  478. /* Get the next pending parent that might have children */
  479. desc = list_first_entry_or_null(&intc_parent_list,
  480. typeof(*desc), list);
  481. if (!desc) {
  482. pr_err("of_irq_init: children remain, but no parents\n");
  483. break;
  484. }
  485. list_del(&desc->list);
  486. parent = desc->dev;
  487. kfree(desc);
  488. }
  489. list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
  490. list_del(&desc->list);
  491. kfree(desc);
  492. }
  493. err:
  494. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  495. list_del(&desc->list);
  496. kfree(desc);
  497. }
  498. }