irq.c 14 KB

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