irq.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/string.h>
  25. /**
  26. * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
  27. * @device: Device node of the device whose interrupt is to be mapped
  28. * @index: Index of the interrupt to map
  29. *
  30. * This function is a wrapper that chains of_irq_map_one() and
  31. * irq_create_of_mapping() to make things easier to callers
  32. */
  33. unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  34. {
  35. struct of_irq oirq;
  36. if (of_irq_map_one(dev, index, &oirq))
  37. return NO_IRQ;
  38. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  39. oirq.size);
  40. }
  41. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  42. /**
  43. * of_irq_find_parent - Given a device node, find its interrupt parent node
  44. * @child: pointer to device node
  45. *
  46. * Returns a pointer to the interrupt parent node, or NULL if the interrupt
  47. * parent could not be determined.
  48. */
  49. static struct device_node *of_irq_find_parent(struct device_node *child)
  50. {
  51. struct device_node *p;
  52. const __be32 *parp;
  53. if (!of_node_get(child))
  54. return NULL;
  55. do {
  56. parp = of_get_property(child, "interrupt-parent", NULL);
  57. if (parp == NULL)
  58. p = of_get_parent(child);
  59. else {
  60. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  61. p = of_node_get(of_irq_dflt_pic);
  62. else
  63. p = of_find_node_by_phandle(be32_to_cpup(parp));
  64. }
  65. of_node_put(child);
  66. child = p;
  67. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  68. return p;
  69. }
  70. /**
  71. * of_irq_map_raw - Low level interrupt tree parsing
  72. * @parent: the device interrupt parent
  73. * @intspec: interrupt specifier ("interrupts" property of the device)
  74. * @ointsize: size of the passed in interrupt specifier
  75. * @addr: address specifier (start of "reg" property of the device)
  76. * @out_irq: structure of_irq filled by this function
  77. *
  78. * Returns 0 on success and a negative number on error
  79. *
  80. * This function is a low-level interrupt tree walking function. It
  81. * can be used to do a partial walk with synthetized reg and interrupts
  82. * properties, for example when resolving PCI interrupts when no device
  83. * node exist for the parent.
  84. */
  85. int of_irq_map_raw(struct device_node *parent, const __be32 *intspec,
  86. u32 ointsize, const __be32 *addr, struct of_irq *out_irq)
  87. {
  88. struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
  89. const __be32 *tmp, *imap, *imask;
  90. u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
  91. int imaplen, match, i;
  92. pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
  93. parent->full_name, be32_to_cpup(intspec),
  94. be32_to_cpup(intspec + 1), ointsize);
  95. ipar = of_node_get(parent);
  96. /* First get the #interrupt-cells property of the current cursor
  97. * that tells us how to interpret the passed-in intspec. If there
  98. * is none, we are nice and just walk up the tree
  99. */
  100. do {
  101. tmp = of_get_property(ipar, "#interrupt-cells", NULL);
  102. if (tmp != NULL) {
  103. intsize = be32_to_cpu(*tmp);
  104. break;
  105. }
  106. tnode = ipar;
  107. ipar = of_irq_find_parent(ipar);
  108. of_node_put(tnode);
  109. } while (ipar);
  110. if (ipar == NULL) {
  111. pr_debug(" -> no parent found !\n");
  112. goto fail;
  113. }
  114. pr_debug("of_irq_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
  115. if (ointsize != intsize)
  116. return -EINVAL;
  117. /* Look for this #address-cells. We have to implement the old linux
  118. * trick of looking for the parent here as some device-trees rely on it
  119. */
  120. old = of_node_get(ipar);
  121. do {
  122. tmp = of_get_property(old, "#address-cells", NULL);
  123. tnode = of_get_parent(old);
  124. of_node_put(old);
  125. old = tnode;
  126. } while (old && tmp == NULL);
  127. of_node_put(old);
  128. old = NULL;
  129. addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
  130. pr_debug(" -> addrsize=%d\n", addrsize);
  131. /* Now start the actual "proper" walk of the interrupt tree */
  132. while (ipar != NULL) {
  133. /* Now check if cursor is an interrupt-controller and if it is
  134. * then we are done
  135. */
  136. if (of_get_property(ipar, "interrupt-controller", NULL) !=
  137. NULL) {
  138. pr_debug(" -> got it !\n");
  139. for (i = 0; i < intsize; i++)
  140. out_irq->specifier[i] =
  141. of_read_number(intspec +i, 1);
  142. out_irq->size = intsize;
  143. out_irq->controller = ipar;
  144. of_node_put(old);
  145. return 0;
  146. }
  147. /* Now look for an interrupt-map */
  148. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  149. /* No interrupt map, check for an interrupt parent */
  150. if (imap == NULL) {
  151. pr_debug(" -> no map, getting parent\n");
  152. newpar = of_irq_find_parent(ipar);
  153. goto skiplevel;
  154. }
  155. imaplen /= sizeof(u32);
  156. /* Look for a mask */
  157. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  158. /* If we were passed no "reg" property and we attempt to parse
  159. * an interrupt-map, then #address-cells must be 0.
  160. * Fail if it's not.
  161. */
  162. if (addr == NULL && addrsize != 0) {
  163. pr_debug(" -> no reg passed in when needed !\n");
  164. goto fail;
  165. }
  166. /* Parse interrupt-map */
  167. match = 0;
  168. while (imaplen > (addrsize + intsize + 1) && !match) {
  169. /* Compare specifiers */
  170. match = 1;
  171. for (i = 0; i < addrsize && match; ++i) {
  172. u32 mask = imask ? imask[i] : 0xffffffffu;
  173. match = ((addr[i] ^ imap[i]) & mask) == 0;
  174. }
  175. for (; i < (addrsize + intsize) && match; ++i) {
  176. u32 mask = imask ? imask[i] : 0xffffffffu;
  177. match =
  178. ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
  179. }
  180. imap += addrsize + intsize;
  181. imaplen -= addrsize + intsize;
  182. pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
  183. /* Get the interrupt parent */
  184. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  185. newpar = of_node_get(of_irq_dflt_pic);
  186. else
  187. newpar = of_find_node_by_phandle(be32_to_cpup(imap));
  188. imap++;
  189. --imaplen;
  190. /* Check if not found */
  191. if (newpar == NULL) {
  192. pr_debug(" -> imap parent not found !\n");
  193. goto fail;
  194. }
  195. /* Get #interrupt-cells and #address-cells of new
  196. * parent
  197. */
  198. tmp = of_get_property(newpar, "#interrupt-cells", NULL);
  199. if (tmp == NULL) {
  200. pr_debug(" -> parent lacks #interrupt-cells!\n");
  201. goto fail;
  202. }
  203. newintsize = be32_to_cpu(*tmp);
  204. tmp = of_get_property(newpar, "#address-cells", NULL);
  205. newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
  206. pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
  207. newintsize, newaddrsize);
  208. /* Check for malformed properties */
  209. if (imaplen < (newaddrsize + newintsize))
  210. goto fail;
  211. imap += newaddrsize + newintsize;
  212. imaplen -= newaddrsize + newintsize;
  213. pr_debug(" -> imaplen=%d\n", imaplen);
  214. }
  215. if (!match)
  216. goto fail;
  217. of_node_put(old);
  218. old = of_node_get(newpar);
  219. addrsize = newaddrsize;
  220. intsize = newintsize;
  221. intspec = imap - intsize;
  222. addr = intspec - addrsize;
  223. skiplevel:
  224. /* Iterate again with new parent */
  225. pr_debug(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
  226. of_node_put(ipar);
  227. ipar = newpar;
  228. newpar = NULL;
  229. }
  230. fail:
  231. of_node_put(ipar);
  232. of_node_put(old);
  233. of_node_put(newpar);
  234. return -EINVAL;
  235. }
  236. EXPORT_SYMBOL_GPL(of_irq_map_raw);
  237. /**
  238. * of_irq_map_one - Resolve an interrupt for a device
  239. * @device: the device whose interrupt is to be resolved
  240. * @index: index of the interrupt to resolve
  241. * @out_irq: structure of_irq filled by this function
  242. *
  243. * This function resolves an interrupt, walking the tree, for a given
  244. * device-tree node. It's the high level pendant to of_irq_map_raw().
  245. */
  246. int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
  247. {
  248. struct device_node *p;
  249. const __be32 *intspec, *tmp, *addr;
  250. u32 intsize, intlen;
  251. int res = -EINVAL;
  252. pr_debug("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
  253. /* OldWorld mac stuff is "special", handle out of line */
  254. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  255. return of_irq_map_oldworld(device, index, out_irq);
  256. /* Get the interrupts property */
  257. intspec = of_get_property(device, "interrupts", &intlen);
  258. if (intspec == NULL)
  259. return -EINVAL;
  260. intlen /= sizeof(*intspec);
  261. pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
  262. /* Get the reg property (if any) */
  263. addr = of_get_property(device, "reg", NULL);
  264. /* Look for the interrupt parent. */
  265. p = of_irq_find_parent(device);
  266. if (p == NULL)
  267. return -EINVAL;
  268. /* Get size of interrupt specifier */
  269. tmp = of_get_property(p, "#interrupt-cells", NULL);
  270. if (tmp == NULL)
  271. goto out;
  272. intsize = be32_to_cpu(*tmp);
  273. pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
  274. /* Check index */
  275. if ((index + 1) * intsize > intlen)
  276. goto out;
  277. /* Get new specifier and map it */
  278. res = of_irq_map_raw(p, intspec + index * intsize, intsize,
  279. addr, out_irq);
  280. out:
  281. of_node_put(p);
  282. return res;
  283. }
  284. EXPORT_SYMBOL_GPL(of_irq_map_one);
  285. /**
  286. * of_irq_to_resource - Decode a node's IRQ and return it as a resource
  287. * @dev: pointer to device tree node
  288. * @index: zero-based index of the irq
  289. * @r: pointer to resource structure to return result into.
  290. */
  291. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  292. {
  293. int irq = irq_of_parse_and_map(dev, index);
  294. /* Only dereference the resource if both the
  295. * resource and the irq are valid. */
  296. if (r && irq != NO_IRQ) {
  297. r->start = r->end = irq;
  298. r->flags = IORESOURCE_IRQ;
  299. r->name = dev->full_name;
  300. }
  301. return irq;
  302. }
  303. EXPORT_SYMBOL_GPL(of_irq_to_resource);