PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/of/irq.c

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