irq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. * @device: 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_map_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_irq oirq;
  38. if (of_irq_map_one(dev, index, &oirq))
  39. return 0;
  40. return irq_create_of_mapping(oirq.controller, oirq.specifier,
  41. oirq.size);
  42. }
  43. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  44. /**
  45. * of_irq_find_parent - Given a device node, find its interrupt parent node
  46. * @child: pointer to device node
  47. *
  48. * Returns a pointer to the interrupt parent node, or NULL if the interrupt
  49. * parent could not be determined.
  50. */
  51. struct device_node *of_irq_find_parent(struct device_node *child)
  52. {
  53. struct device_node *p;
  54. const __be32 *parp;
  55. if (!of_node_get(child))
  56. return NULL;
  57. do {
  58. parp = of_get_property(child, "interrupt-parent", NULL);
  59. if (parp == NULL)
  60. p = of_get_parent(child);
  61. else {
  62. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  63. p = of_node_get(of_irq_dflt_pic);
  64. else
  65. p = of_find_node_by_phandle(be32_to_cpup(parp));
  66. }
  67. of_node_put(child);
  68. child = p;
  69. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  70. return p;
  71. }
  72. /**
  73. * of_irq_map_raw - Low level interrupt tree parsing
  74. * @parent: the device interrupt parent
  75. * @intspec: interrupt specifier ("interrupts" property of the device)
  76. * @ointsize: size of the passed in interrupt specifier
  77. * @addr: address specifier (start of "reg" property of the device)
  78. * @out_irq: structure of_irq filled by this function
  79. *
  80. * Returns 0 on success and a negative number on error
  81. *
  82. * This function is a low-level interrupt tree walking function. It
  83. * can be used to do a partial walk with synthetized reg and interrupts
  84. * properties, for example when resolving PCI interrupts when no device
  85. * node exist for the parent.
  86. */
  87. int of_irq_map_raw(struct device_node *parent, const __be32 *intspec,
  88. u32 ointsize, const __be32 *addr, struct of_irq *out_irq)
  89. {
  90. struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
  91. const __be32 *tmp, *imap, *imask;
  92. u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
  93. int imaplen, match, i;
  94. pr_debug("of_irq_map_raw: par=%s,intspec=[0x%08x 0x%08x...],ointsize=%d\n",
  95. parent->full_name, be32_to_cpup(intspec),
  96. be32_to_cpup(intspec + 1), ointsize);
  97. ipar = of_node_get(parent);
  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_map_raw: ipar=%s, size=%d\n", ipar->full_name, intsize);
  117. if (ointsize != 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. /* Now start the actual "proper" walk of the interrupt tree */
  134. while (ipar != NULL) {
  135. /* Now check if cursor is an interrupt-controller and if it is
  136. * then we are done
  137. */
  138. if (of_get_property(ipar, "interrupt-controller", NULL) !=
  139. NULL) {
  140. pr_debug(" -> got it !\n");
  141. for (i = 0; i < intsize; i++)
  142. out_irq->specifier[i] =
  143. of_read_number(intspec +i, 1);
  144. out_irq->size = intsize;
  145. out_irq->controller = ipar;
  146. of_node_put(old);
  147. return 0;
  148. }
  149. /* Now look for an interrupt-map */
  150. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  151. /* No interrupt map, check for an interrupt parent */
  152. if (imap == NULL) {
  153. pr_debug(" -> no map, getting parent\n");
  154. newpar = of_irq_find_parent(ipar);
  155. goto skiplevel;
  156. }
  157. imaplen /= sizeof(u32);
  158. /* Look for a mask */
  159. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  160. /* If we were passed no "reg" property and we attempt to parse
  161. * an interrupt-map, then #address-cells must be 0.
  162. * Fail if it's not.
  163. */
  164. if (addr == NULL && addrsize != 0) {
  165. pr_debug(" -> no reg passed in when needed !\n");
  166. goto fail;
  167. }
  168. /* Parse interrupt-map */
  169. match = 0;
  170. while (imaplen > (addrsize + intsize + 1) && !match) {
  171. /* Compare specifiers */
  172. match = 1;
  173. for (i = 0; i < addrsize && match; ++i) {
  174. u32 mask = imask ? imask[i] : 0xffffffffu;
  175. match = ((addr[i] ^ imap[i]) & mask) == 0;
  176. }
  177. for (; i < (addrsize + intsize) && match; ++i) {
  178. u32 mask = imask ? imask[i] : 0xffffffffu;
  179. match =
  180. ((intspec[i-addrsize] ^ imap[i]) & mask) == 0;
  181. }
  182. imap += addrsize + intsize;
  183. imaplen -= addrsize + intsize;
  184. pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
  185. /* Get the interrupt parent */
  186. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  187. newpar = of_node_get(of_irq_dflt_pic);
  188. else
  189. newpar = of_find_node_by_phandle(be32_to_cpup(imap));
  190. imap++;
  191. --imaplen;
  192. /* Check if not found */
  193. if (newpar == NULL) {
  194. pr_debug(" -> imap parent not found !\n");
  195. goto fail;
  196. }
  197. /* Get #interrupt-cells and #address-cells of new
  198. * parent
  199. */
  200. tmp = of_get_property(newpar, "#interrupt-cells", NULL);
  201. if (tmp == NULL) {
  202. pr_debug(" -> parent lacks #interrupt-cells!\n");
  203. goto fail;
  204. }
  205. newintsize = be32_to_cpu(*tmp);
  206. tmp = of_get_property(newpar, "#address-cells", NULL);
  207. newaddrsize = (tmp == NULL) ? 0 : be32_to_cpu(*tmp);
  208. pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
  209. newintsize, newaddrsize);
  210. /* Check for malformed properties */
  211. if (imaplen < (newaddrsize + newintsize))
  212. goto fail;
  213. imap += newaddrsize + newintsize;
  214. imaplen -= newaddrsize + newintsize;
  215. pr_debug(" -> imaplen=%d\n", imaplen);
  216. }
  217. if (!match)
  218. goto fail;
  219. of_node_put(old);
  220. old = of_node_get(newpar);
  221. addrsize = newaddrsize;
  222. intsize = newintsize;
  223. intspec = imap - intsize;
  224. addr = intspec - addrsize;
  225. skiplevel:
  226. /* Iterate again with new parent */
  227. pr_debug(" -> new parent: %s\n", newpar ? newpar->full_name : "<>");
  228. of_node_put(ipar);
  229. ipar = newpar;
  230. newpar = NULL;
  231. }
  232. fail:
  233. of_node_put(ipar);
  234. of_node_put(old);
  235. of_node_put(newpar);
  236. return -EINVAL;
  237. }
  238. EXPORT_SYMBOL_GPL(of_irq_map_raw);
  239. /**
  240. * of_irq_map_one - Resolve an interrupt for a device
  241. * @device: the device whose interrupt is to be resolved
  242. * @index: index of the interrupt to resolve
  243. * @out_irq: structure of_irq filled by this function
  244. *
  245. * This function resolves an interrupt, walking the tree, for a given
  246. * device-tree node. It's the high level pendant to of_irq_map_raw().
  247. */
  248. int of_irq_map_one(struct device_node *device, int index, struct of_irq *out_irq)
  249. {
  250. struct device_node *p;
  251. const __be32 *intspec, *tmp, *addr;
  252. u32 intsize, intlen;
  253. int res = -EINVAL;
  254. pr_debug("of_irq_map_one: dev=%s, index=%d\n", device->full_name, index);
  255. /* OldWorld mac stuff is "special", handle out of line */
  256. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  257. return of_irq_map_oldworld(device, index, out_irq);
  258. /* Get the interrupts property */
  259. intspec = of_get_property(device, "interrupts", &intlen);
  260. if (intspec == NULL)
  261. return -EINVAL;
  262. intlen /= sizeof(*intspec);
  263. pr_debug(" intspec=%d intlen=%d\n", be32_to_cpup(intspec), intlen);
  264. /* Get the reg property (if any) */
  265. addr = of_get_property(device, "reg", NULL);
  266. /* Look for the interrupt parent. */
  267. p = of_irq_find_parent(device);
  268. if (p == NULL)
  269. return -EINVAL;
  270. /* Get size of interrupt specifier */
  271. tmp = of_get_property(p, "#interrupt-cells", NULL);
  272. if (tmp == NULL)
  273. goto out;
  274. intsize = be32_to_cpu(*tmp);
  275. pr_debug(" intsize=%d intlen=%d\n", intsize, intlen);
  276. /* Check index */
  277. if ((index + 1) * intsize > intlen)
  278. goto out;
  279. /* Get new specifier and map it */
  280. res = of_irq_map_raw(p, intspec + index * intsize, intsize,
  281. addr, out_irq);
  282. out:
  283. of_node_put(p);
  284. return res;
  285. }
  286. EXPORT_SYMBOL_GPL(of_irq_map_one);
  287. /**
  288. * of_irq_to_resource - Decode a node's IRQ and return it as a resource
  289. * @dev: pointer to device tree node
  290. * @index: zero-based index of the irq
  291. * @r: pointer to resource structure to return result into.
  292. */
  293. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  294. {
  295. int irq = irq_of_parse_and_map(dev, index);
  296. /* Only dereference the resource if both the
  297. * resource and the irq are valid. */
  298. if (r && irq) {
  299. const char *name = NULL;
  300. /*
  301. * Get optional "interrupts-names" property to add a name
  302. * to the resource.
  303. */
  304. of_property_read_string_index(dev, "interrupt-names", index,
  305. &name);
  306. r->start = r->end = irq;
  307. r->flags = IORESOURCE_IRQ;
  308. r->name = name ? name : dev->full_name;
  309. }
  310. return irq;
  311. }
  312. EXPORT_SYMBOL_GPL(of_irq_to_resource);
  313. /**
  314. * of_irq_count - Count the number of IRQs a node uses
  315. * @dev: pointer to device tree node
  316. */
  317. int of_irq_count(struct device_node *dev)
  318. {
  319. int nr = 0;
  320. while (of_irq_to_resource(dev, nr, NULL))
  321. nr++;
  322. return nr;
  323. }
  324. /**
  325. * of_irq_to_resource_table - Fill in resource table with node's IRQ info
  326. * @dev: pointer to device tree node
  327. * @res: array of resources to fill in
  328. * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
  329. *
  330. * Returns the size of the filled in table (up to @nr_irqs).
  331. */
  332. int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
  333. int nr_irqs)
  334. {
  335. int i;
  336. for (i = 0; i < nr_irqs; i++, res++)
  337. if (!of_irq_to_resource(dev, i, res))
  338. break;
  339. return i;
  340. }
  341. struct intc_desc {
  342. struct list_head list;
  343. struct device_node *dev;
  344. struct device_node *interrupt_parent;
  345. };
  346. /**
  347. * of_irq_init - Scan and init matching interrupt controllers in DT
  348. * @matches: 0 terminated array of nodes to match and init function to call
  349. *
  350. * This function scans the device tree for matching interrupt controller nodes,
  351. * and calls their initialization functions in order with parents first.
  352. */
  353. void __init of_irq_init(const struct of_device_id *matches)
  354. {
  355. struct device_node *np, *parent = NULL;
  356. struct intc_desc *desc, *temp_desc;
  357. struct list_head intc_desc_list, intc_parent_list;
  358. INIT_LIST_HEAD(&intc_desc_list);
  359. INIT_LIST_HEAD(&intc_parent_list);
  360. for_each_matching_node(np, matches) {
  361. if (!of_find_property(np, "interrupt-controller", NULL))
  362. continue;
  363. /*
  364. * Here, we allocate and populate an intc_desc with the node
  365. * pointer, interrupt-parent device_node etc.
  366. */
  367. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  368. if (WARN_ON(!desc))
  369. goto err;
  370. desc->dev = np;
  371. desc->interrupt_parent = of_irq_find_parent(np);
  372. if (desc->interrupt_parent == np)
  373. desc->interrupt_parent = NULL;
  374. list_add_tail(&desc->list, &intc_desc_list);
  375. }
  376. /*
  377. * The root irq controller is the one without an interrupt-parent.
  378. * That one goes first, followed by the controllers that reference it,
  379. * followed by the ones that reference the 2nd level controllers, etc.
  380. */
  381. while (!list_empty(&intc_desc_list)) {
  382. /*
  383. * Process all controllers with the current 'parent'.
  384. * First pass will be looking for NULL as the parent.
  385. * The assumption is that NULL parent means a root controller.
  386. */
  387. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  388. const struct of_device_id *match;
  389. int ret;
  390. of_irq_init_cb_t irq_init_cb;
  391. if (desc->interrupt_parent != parent)
  392. continue;
  393. list_del(&desc->list);
  394. match = of_match_node(matches, desc->dev);
  395. if (WARN(!match->data,
  396. "of_irq_init: no init function for %s\n",
  397. match->compatible)) {
  398. kfree(desc);
  399. continue;
  400. }
  401. pr_debug("of_irq_init: init %s @ %p, parent %p\n",
  402. match->compatible,
  403. desc->dev, desc->interrupt_parent);
  404. irq_init_cb = match->data;
  405. ret = irq_init_cb(desc->dev, desc->interrupt_parent);
  406. if (ret) {
  407. kfree(desc);
  408. continue;
  409. }
  410. /*
  411. * This one is now set up; add it to the parent list so
  412. * its children can get processed in a subsequent pass.
  413. */
  414. list_add_tail(&desc->list, &intc_parent_list);
  415. }
  416. /* Get the next pending parent that might have children */
  417. desc = list_first_entry(&intc_parent_list, typeof(*desc), list);
  418. if (list_empty(&intc_parent_list) || !desc) {
  419. pr_err("of_irq_init: children remain, but no parents\n");
  420. break;
  421. }
  422. list_del(&desc->list);
  423. parent = desc->dev;
  424. kfree(desc);
  425. }
  426. list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
  427. list_del(&desc->list);
  428. kfree(desc);
  429. }
  430. err:
  431. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  432. list_del(&desc->list);
  433. kfree(desc);
  434. }
  435. }