irq.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Derived from arch/i386/kernel/irq.c
  4. * Copyright (C) 1992 Linus Torvalds
  5. * Adapted from arch/i386 by Gary Thomas
  6. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  7. * Updated and modified by Cort Dougan <cort@fsmlabs.com>
  8. * Copyright (C) 1996-2001 Cort Dougan
  9. * Adapted for Power Macintosh by Paul Mackerras
  10. * Copyright (C) 1996 Paul Mackerras (paulus@cs.anu.edu.au)
  11. *
  12. * This file contains the code used to make IRQ descriptions in the
  13. * device tree to actual irq numbers on an interrupt controller
  14. * driver.
  15. */
  16. #define pr_fmt(fmt) "OF: " fmt
  17. #include <linux/cleanup.h>
  18. #include <linux/device.h>
  19. #include <linux/errno.h>
  20. #include <linux/list.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/string.h>
  25. #include <linux/slab.h>
  26. #include "of_private.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. unsigned int ret;
  39. if (of_irq_parse_one(dev, index, &oirq))
  40. return 0;
  41. ret = irq_create_of_mapping(&oirq);
  42. of_node_put(oirq.np);
  43. return ret;
  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. * Return: A pointer to the interrupt parent node with refcount increased
  51. * or NULL if the interrupt 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. phandle parent;
  57. if (!of_node_get(child))
  58. return NULL;
  59. do {
  60. if (of_property_read_u32(child, "interrupt-parent", &parent)) {
  61. p = of_get_parent(child);
  62. } else {
  63. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  64. p = of_node_get(of_irq_dflt_pic);
  65. else
  66. p = of_find_node_by_phandle(parent);
  67. }
  68. of_node_put(child);
  69. child = p;
  70. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  71. return p;
  72. }
  73. EXPORT_SYMBOL_GPL(of_irq_find_parent);
  74. /*
  75. * These interrupt controllers abuse interrupt-map for unspeakable
  76. * reasons and rely on the core code to *ignore* it (the drivers do
  77. * their own parsing of the property). The PAsemi entry covers a
  78. * non-sensical interrupt-map that is better left ignored.
  79. *
  80. * If you think of adding to the list for something *new*, think
  81. * again. There is a high chance that you will be sent back to the
  82. * drawing board.
  83. */
  84. static const char * const of_irq_imap_abusers[] = {
  85. "CBEA,platform-spider-pic",
  86. "sti,platform-spider-pic",
  87. "realtek,rtl-intc",
  88. "fsl,ls1021a-extirq",
  89. "fsl,ls1043a-extirq",
  90. "fsl,ls1088a-extirq",
  91. "renesas,rza1-irqc",
  92. "pasemi,rootbus",
  93. NULL,
  94. };
  95. const __be32 *of_irq_parse_imap_parent(const __be32 *imap, int len, struct of_phandle_args *out_irq)
  96. {
  97. u32 intsize, addrsize;
  98. struct device_node *np;
  99. /* Get the interrupt parent */
  100. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  101. np = of_node_get(of_irq_dflt_pic);
  102. else
  103. np = of_find_node_by_phandle(be32_to_cpup(imap));
  104. imap++;
  105. len--;
  106. /* Check if not found */
  107. if (!np) {
  108. pr_debug(" -> imap parent not found !\n");
  109. return NULL;
  110. }
  111. /* Get #interrupt-cells and #address-cells of new parent */
  112. if (of_property_read_u32(np, "#interrupt-cells",
  113. &intsize)) {
  114. pr_debug(" -> parent lacks #interrupt-cells!\n");
  115. of_node_put(np);
  116. return NULL;
  117. }
  118. if (of_property_read_u32(np, "#address-cells",
  119. &addrsize))
  120. addrsize = 0;
  121. pr_debug(" -> intsize=%d, addrsize=%d\n",
  122. intsize, addrsize);
  123. /* Check for malformed properties */
  124. if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)
  125. || (len < (addrsize + intsize))) {
  126. of_node_put(np);
  127. return NULL;
  128. }
  129. pr_debug(" -> imaplen=%d\n", len);
  130. imap += addrsize + intsize;
  131. out_irq->np = np;
  132. for (int i = 0; i < intsize; i++)
  133. out_irq->args[i] = be32_to_cpup(imap - intsize + i);
  134. out_irq->args_count = intsize;
  135. return imap;
  136. }
  137. int of_imap_parser_init(struct of_imap_parser *parser, struct device_node *node,
  138. struct of_imap_item *item)
  139. {
  140. int imaplen;
  141. u32 tmp;
  142. int ret;
  143. /*
  144. * parent_offset is the offset where the parent part is starting.
  145. * In other words, the offset where the parent interrupt controller
  146. * phandle is present.
  147. *
  148. * Compute this offset (child #interrupt-cells + child #address-cells)
  149. */
  150. parser->parent_offset = of_bus_n_addr_cells(node);
  151. ret = of_property_read_u32(node, "#interrupt-cells", &tmp);
  152. if (ret)
  153. return ret;
  154. parser->parent_offset += tmp;
  155. if (WARN(parser->parent_offset > ARRAY_SIZE(item->child_imap),
  156. "child part size = %u, cannot fit in array of %zu items",
  157. parser->parent_offset, ARRAY_SIZE(item->child_imap)))
  158. return -EINVAL;
  159. parser->imap = of_get_property(node, "interrupt-map", &imaplen);
  160. if (!parser->imap)
  161. return -ENOENT;
  162. imaplen /= sizeof(*parser->imap);
  163. parser->imap_end = parser->imap + imaplen;
  164. memset(item, 0, sizeof(*item));
  165. item->child_imap_count = parser->parent_offset;
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(of_imap_parser_init);
  169. struct of_imap_item *of_imap_parser_one(struct of_imap_parser *parser,
  170. struct of_imap_item *item)
  171. {
  172. const __be32 *imap_parent, *imap_next;
  173. int i;
  174. /* Release previously get parent node */
  175. of_node_put(item->parent_args.np);
  176. if (parser->imap + parser->parent_offset + 1 >= parser->imap_end)
  177. return NULL;
  178. imap_parent = parser->imap + parser->parent_offset;
  179. imap_next = of_irq_parse_imap_parent(imap_parent,
  180. parser->imap_end - imap_parent,
  181. &item->parent_args);
  182. if (!imap_next)
  183. return NULL;
  184. for (i = 0; i < parser->parent_offset; i++)
  185. item->child_imap[i] = be32_to_cpu(*(parser->imap + i));
  186. parser->imap = imap_next;
  187. return item;
  188. }
  189. EXPORT_SYMBOL_GPL(of_imap_parser_one);
  190. /**
  191. * of_irq_parse_raw - Low level interrupt tree parsing
  192. * @addr: address specifier (start of "reg" property of the device) in be32 format
  193. * @out_irq: structure of_phandle_args updated by this function
  194. *
  195. * This function is a low-level interrupt tree walking function. It
  196. * can be used to do a partial walk with synthesized reg and interrupts
  197. * properties, for example when resolving PCI interrupts when no device
  198. * node exist for the parent. It takes an interrupt specifier structure as
  199. * input, walks the tree looking for any interrupt-map properties, translates
  200. * the specifier for each map, and then returns the translated map.
  201. *
  202. * Return: 0 on success and a negative number on error
  203. *
  204. * Note: refcount of node @out_irq->np is increased by 1 on success.
  205. */
  206. int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
  207. {
  208. struct device_node *ipar, *tnode, *old = NULL;
  209. __be32 initial_match_array[MAX_PHANDLE_ARGS];
  210. const __be32 *match_array = initial_match_array;
  211. const __be32 *tmp, dummy_imask[] = { [0 ... (MAX_PHANDLE_ARGS - 1)] = cpu_to_be32(~0) };
  212. u32 intsize = 1, addrsize;
  213. int i, rc = -EINVAL;
  214. #ifdef DEBUG
  215. of_print_phandle_args("of_irq_parse_raw: ", out_irq);
  216. #endif
  217. ipar = of_node_get(out_irq->np);
  218. /* First get the #interrupt-cells property of the current cursor
  219. * that tells us how to interpret the passed-in intspec. If there
  220. * is none, we are nice and just walk up the tree
  221. */
  222. do {
  223. if (!of_property_read_u32(ipar, "#interrupt-cells", &intsize))
  224. break;
  225. tnode = ipar;
  226. ipar = of_irq_find_parent(ipar);
  227. of_node_put(tnode);
  228. } while (ipar);
  229. if (ipar == NULL) {
  230. pr_debug(" -> no parent found !\n");
  231. goto fail;
  232. }
  233. pr_debug("of_irq_parse_raw: ipar=%pOF, size=%d\n", ipar, intsize);
  234. if (out_irq->args_count != intsize)
  235. goto fail;
  236. /* Look for this #address-cells. We have to implement the old linux
  237. * trick of looking for the parent here as some device-trees rely on it
  238. */
  239. old = of_node_get(ipar);
  240. do {
  241. tmp = of_get_property(old, "#address-cells", NULL);
  242. tnode = of_get_parent(old);
  243. of_node_put(old);
  244. old = tnode;
  245. } while (old && tmp == NULL);
  246. of_node_put(old);
  247. old = NULL;
  248. addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
  249. pr_debug(" -> addrsize=%d\n", addrsize);
  250. /* Range check so that the temporary buffer doesn't overflow */
  251. if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) {
  252. rc = -EFAULT;
  253. goto fail;
  254. }
  255. /* Precalculate the match array - this simplifies match loop */
  256. for (i = 0; i < addrsize; i++)
  257. initial_match_array[i] = addr ? addr[i] : 0;
  258. for (i = 0; i < intsize; i++)
  259. initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);
  260. /* Now start the actual "proper" walk of the interrupt tree */
  261. while (ipar != NULL) {
  262. int imaplen, match;
  263. const __be32 *imap, *oldimap, *imask;
  264. struct device_node *newpar;
  265. /*
  266. * Now check if cursor is an interrupt-controller and
  267. * if it is then we are done, unless there is an
  268. * interrupt-map which takes precedence except on one
  269. * of these broken platforms that want to parse
  270. * interrupt-map themselves for $reason.
  271. */
  272. bool intc = of_property_read_bool(ipar, "interrupt-controller");
  273. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  274. if (intc &&
  275. (!imap || of_device_compatible_match(ipar, of_irq_imap_abusers))) {
  276. pr_debug(" -> got it !\n");
  277. return 0;
  278. }
  279. /*
  280. * interrupt-map parsing does not work without a reg
  281. * property when #address-cells != 0
  282. */
  283. if (addrsize && !addr) {
  284. pr_debug(" -> no reg passed in when needed !\n");
  285. goto fail;
  286. }
  287. /* No interrupt map, check for an interrupt parent */
  288. if (imap == NULL) {
  289. pr_debug(" -> no map, getting parent\n");
  290. newpar = of_irq_find_parent(ipar);
  291. goto skiplevel;
  292. }
  293. imaplen /= sizeof(u32);
  294. /* Look for a mask */
  295. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  296. if (!imask)
  297. imask = dummy_imask;
  298. /* Parse interrupt-map */
  299. match = 0;
  300. while (imaplen > (addrsize + intsize + 1)) {
  301. /* Compare specifiers */
  302. match = 1;
  303. for (i = 0; i < (addrsize + intsize); i++, imaplen--)
  304. match &= !((match_array[i] ^ *imap++) & imask[i]);
  305. pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
  306. oldimap = imap;
  307. imap = of_irq_parse_imap_parent(oldimap, imaplen, out_irq);
  308. if (!imap)
  309. goto fail;
  310. match &= of_device_is_available(out_irq->np);
  311. if (match)
  312. break;
  313. of_node_put(out_irq->np);
  314. imaplen -= imap - oldimap;
  315. pr_debug(" -> imaplen=%d\n", imaplen);
  316. }
  317. if (!match)
  318. goto fail;
  319. /*
  320. * Successfully parsed an interrupt-map translation; copy new
  321. * interrupt specifier into the out_irq structure
  322. */
  323. match_array = oldimap + 1;
  324. newpar = out_irq->np;
  325. intsize = out_irq->args_count;
  326. addrsize = (imap - match_array) - intsize;
  327. if (ipar == newpar) {
  328. /*
  329. * We got @ipar's refcount, but the refcount was
  330. * gotten again by of_irq_parse_imap_parent() via its
  331. * alias @newpar.
  332. */
  333. of_node_put(ipar);
  334. pr_debug("%pOF interrupt-map entry to self\n", ipar);
  335. return 0;
  336. }
  337. skiplevel:
  338. /* Iterate again with new parent */
  339. pr_debug(" -> new parent: %pOF\n", newpar);
  340. of_node_put(ipar);
  341. ipar = newpar;
  342. newpar = NULL;
  343. }
  344. rc = -ENOENT; /* No interrupt-map found */
  345. fail:
  346. of_node_put(ipar);
  347. return rc;
  348. }
  349. EXPORT_SYMBOL_GPL(of_irq_parse_raw);
  350. /**
  351. * of_irq_parse_one - Resolve an interrupt for a device
  352. * @device: the device whose interrupt is to be resolved
  353. * @index: index of the interrupt to resolve
  354. * @out_irq: structure of_phandle_args filled by this function
  355. *
  356. * This function resolves an interrupt for a node by walking the interrupt tree,
  357. * finding which interrupt controller node it is attached to, and returning the
  358. * interrupt specifier that can be used to retrieve a Linux IRQ number.
  359. *
  360. * Note: refcount of node @out_irq->np is increased by 1 on success.
  361. */
  362. int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
  363. {
  364. struct device_node __free(device_node) *p = NULL;
  365. const __be32 *addr;
  366. u32 intsize;
  367. int i, res, addr_len;
  368. __be32 addr_buf[3] = { 0 };
  369. pr_debug("of_irq_parse_one: dev=%pOF, index=%d\n", device, index);
  370. /* OldWorld mac stuff is "special", handle out of line */
  371. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  372. return of_irq_parse_oldworld(device, index, out_irq);
  373. /* Get the reg property (if any) */
  374. addr_len = 0;
  375. addr = of_get_property(device, "reg", &addr_len);
  376. /* Prevent out-of-bounds read in case of longer interrupt parent address size */
  377. if (addr_len > sizeof(addr_buf))
  378. addr_len = sizeof(addr_buf);
  379. if (addr)
  380. memcpy(addr_buf, addr, addr_len);
  381. /* Try the new-style interrupts-extended first */
  382. res = of_parse_phandle_with_args(device, "interrupts-extended",
  383. "#interrupt-cells", index, out_irq);
  384. if (!res) {
  385. p = out_irq->np;
  386. } else {
  387. /* Look for the interrupt parent. */
  388. p = of_irq_find_parent(device);
  389. /* Get size of interrupt specifier */
  390. if (!p || of_property_read_u32(p, "#interrupt-cells", &intsize))
  391. return -EINVAL;
  392. pr_debug(" parent=%pOF, intsize=%d\n", p, intsize);
  393. /* Copy intspec into irq structure */
  394. out_irq->np = p;
  395. out_irq->args_count = intsize;
  396. for (i = 0; i < intsize; i++) {
  397. res = of_property_read_u32_index(device, "interrupts",
  398. (index * intsize) + i,
  399. out_irq->args + i);
  400. if (res)
  401. return res;
  402. }
  403. pr_debug(" intspec=%d\n", *out_irq->args);
  404. }
  405. /* Check if there are any interrupt-map translations to process */
  406. return of_irq_parse_raw(addr_buf, out_irq);
  407. }
  408. EXPORT_SYMBOL_GPL(of_irq_parse_one);
  409. /**
  410. * of_irq_to_resource - Decode a node's IRQ and return it as a resource
  411. * @dev: pointer to device tree node
  412. * @index: zero-based index of the irq
  413. * @r: pointer to resource structure to return result into.
  414. */
  415. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  416. {
  417. int irq = of_irq_get(dev, index);
  418. if (irq < 0)
  419. return irq;
  420. /* Only dereference the resource if both the
  421. * resource and the irq are valid. */
  422. if (r && irq) {
  423. const char *name = NULL;
  424. memset(r, 0, sizeof(*r));
  425. /*
  426. * Get optional "interrupt-names" property to add a name
  427. * to the resource.
  428. */
  429. of_property_read_string_index(dev, "interrupt-names", index,
  430. &name);
  431. *r = DEFINE_RES_IRQ_NAMED(irq, name ?: of_node_full_name(dev));
  432. r->flags |= irq_get_trigger_type(irq);
  433. }
  434. return irq;
  435. }
  436. EXPORT_SYMBOL_GPL(of_irq_to_resource);
  437. /**
  438. * of_irq_get - Decode a node's IRQ and return it as a Linux IRQ number
  439. * @dev: pointer to device tree node
  440. * @index: zero-based index of the IRQ
  441. *
  442. * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
  443. * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
  444. * of any other failure.
  445. */
  446. int of_irq_get(struct device_node *dev, int index)
  447. {
  448. int rc;
  449. struct of_phandle_args oirq;
  450. struct irq_domain *domain;
  451. rc = of_irq_parse_one(dev, index, &oirq);
  452. if (rc)
  453. return rc;
  454. domain = irq_find_host(oirq.np);
  455. if (!domain) {
  456. rc = -EPROBE_DEFER;
  457. goto out;
  458. }
  459. rc = irq_create_of_mapping(&oirq);
  460. out:
  461. of_node_put(oirq.np);
  462. return rc;
  463. }
  464. EXPORT_SYMBOL_GPL(of_irq_get);
  465. const struct cpumask *of_irq_get_affinity(struct device_node *dev, int index)
  466. {
  467. struct of_phandle_args oirq;
  468. struct irq_fwspec_info info;
  469. struct irq_fwspec fwspec;
  470. int rc;
  471. rc = of_irq_parse_one(dev, index, &oirq);
  472. if (rc)
  473. return NULL;
  474. of_phandle_args_to_fwspec(oirq.np, oirq.args, oirq.args_count,
  475. &fwspec);
  476. if (irq_populate_fwspec_info(&fwspec, &info))
  477. return NULL;
  478. return info.affinity;
  479. }
  480. /**
  481. * of_irq_get_byname - Decode a node's IRQ and return it as a Linux IRQ number
  482. * @dev: pointer to device tree node
  483. * @name: IRQ name
  484. *
  485. * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
  486. * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
  487. * of any other failure.
  488. */
  489. int of_irq_get_byname(struct device_node *dev, const char *name)
  490. {
  491. int index;
  492. if (unlikely(!name))
  493. return -EINVAL;
  494. index = of_property_match_string(dev, "interrupt-names", name);
  495. if (index < 0)
  496. return index;
  497. return of_irq_get(dev, index);
  498. }
  499. EXPORT_SYMBOL_GPL(of_irq_get_byname);
  500. /**
  501. * of_irq_count - Count the number of IRQs a node uses
  502. * @dev: pointer to device tree node
  503. */
  504. int of_irq_count(struct device_node *dev)
  505. {
  506. struct of_phandle_args irq;
  507. int nr = 0;
  508. while (of_irq_parse_one(dev, nr, &irq) == 0) {
  509. of_node_put(irq.np);
  510. nr++;
  511. }
  512. return nr;
  513. }
  514. EXPORT_SYMBOL_GPL(of_irq_count);
  515. /**
  516. * of_irq_to_resource_table - Fill in resource table with node's IRQ info
  517. * @dev: pointer to device tree node
  518. * @res: array of resources to fill in
  519. * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
  520. *
  521. * Return: The size of the filled in table (up to @nr_irqs).
  522. */
  523. int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
  524. int nr_irqs)
  525. {
  526. int i;
  527. for (i = 0; i < nr_irqs; i++, res++)
  528. if (of_irq_to_resource(dev, i, res) <= 0)
  529. break;
  530. return i;
  531. }
  532. EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
  533. struct of_intc_desc {
  534. struct list_head list;
  535. of_irq_init_cb_t irq_init_cb;
  536. struct device_node *dev;
  537. struct device_node *interrupt_parent;
  538. };
  539. /**
  540. * of_irq_init - Scan and init matching interrupt controllers in DT
  541. * @matches: 0 terminated array of nodes to match and init function to call
  542. *
  543. * This function scans the device tree for matching interrupt controller nodes,
  544. * and calls their initialization functions in order with parents first.
  545. */
  546. void __init of_irq_init(const struct of_device_id *matches)
  547. {
  548. const struct of_device_id *match;
  549. struct device_node *np, *parent = NULL;
  550. struct of_intc_desc *desc, *temp_desc;
  551. struct list_head intc_desc_list, intc_parent_list;
  552. INIT_LIST_HEAD(&intc_desc_list);
  553. INIT_LIST_HEAD(&intc_parent_list);
  554. for_each_matching_node_and_match(np, matches, &match) {
  555. if (!of_property_read_bool(np, "interrupt-controller") ||
  556. !of_device_is_available(np))
  557. continue;
  558. if (WARN(!match->data, "of_irq_init: no init function for %s\n",
  559. match->compatible))
  560. continue;
  561. /*
  562. * Here, we allocate and populate an of_intc_desc with the node
  563. * pointer, interrupt-parent device_node etc.
  564. */
  565. desc = kzalloc_obj(*desc);
  566. if (!desc) {
  567. of_node_put(np);
  568. goto err;
  569. }
  570. desc->irq_init_cb = match->data;
  571. desc->dev = of_node_get(np);
  572. /*
  573. * interrupts-extended can reference multiple parent domains.
  574. * Arbitrarily pick the first one; assume any other parents
  575. * are the same distance away from the root irq controller.
  576. */
  577. desc->interrupt_parent = of_parse_phandle(np, "interrupts-extended", 0);
  578. if (!desc->interrupt_parent && of_property_present(np, "interrupts"))
  579. desc->interrupt_parent = of_irq_find_parent(np);
  580. else if (!desc->interrupt_parent)
  581. desc->interrupt_parent = of_parse_phandle(np, "interrupt-parent", 0);
  582. if (desc->interrupt_parent == np) {
  583. of_node_put(desc->interrupt_parent);
  584. desc->interrupt_parent = NULL;
  585. }
  586. list_add_tail(&desc->list, &intc_desc_list);
  587. }
  588. /*
  589. * The root irq controller is the one without an interrupt-parent.
  590. * That one goes first, followed by the controllers that reference it,
  591. * followed by the ones that reference the 2nd level controllers, etc.
  592. */
  593. while (!list_empty(&intc_desc_list)) {
  594. /*
  595. * Process all controllers with the current 'parent'.
  596. * First pass will be looking for NULL as the parent.
  597. * The assumption is that NULL parent means a root controller.
  598. */
  599. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  600. int ret;
  601. if (desc->interrupt_parent != parent)
  602. continue;
  603. list_del(&desc->list);
  604. of_node_set_flag(desc->dev, OF_POPULATED);
  605. pr_debug("of_irq_init: init %pOF (%p), parent %p\n",
  606. desc->dev,
  607. desc->dev, desc->interrupt_parent);
  608. ret = desc->irq_init_cb(desc->dev,
  609. desc->interrupt_parent);
  610. if (ret) {
  611. pr_err("%s: Failed to init %pOF (%p), parent %p\n",
  612. __func__, desc->dev, desc->dev,
  613. desc->interrupt_parent);
  614. of_node_clear_flag(desc->dev, OF_POPULATED);
  615. of_node_put(desc->interrupt_parent);
  616. of_node_put(desc->dev);
  617. kfree(desc);
  618. continue;
  619. }
  620. /*
  621. * This one is now set up; add it to the parent list so
  622. * its children can get processed in a subsequent pass.
  623. */
  624. list_add_tail(&desc->list, &intc_parent_list);
  625. }
  626. /* Get the next pending parent that might have children */
  627. desc = list_first_entry_or_null(&intc_parent_list,
  628. typeof(*desc), list);
  629. if (!desc) {
  630. pr_err("of_irq_init: children remain, but no parents\n");
  631. break;
  632. }
  633. list_del(&desc->list);
  634. parent = desc->dev;
  635. kfree(desc);
  636. }
  637. list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
  638. list_del(&desc->list);
  639. kfree(desc);
  640. }
  641. err:
  642. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  643. list_del(&desc->list);
  644. of_node_put(desc->interrupt_parent);
  645. of_node_put(desc->dev);
  646. kfree(desc);
  647. }
  648. }
  649. static int of_check_msi_parent(struct device_node *dev_node, struct device_node **msi_node)
  650. {
  651. struct of_phandle_args msi_spec;
  652. int ret;
  653. /*
  654. * An msi-parent phandle with a missing or == 0 #msi-cells
  655. * property identifies a 1:1 ID translation mapping.
  656. *
  657. * Set the msi controller node if the firmware matches this
  658. * condition.
  659. */
  660. ret = of_parse_phandle_with_optional_args(dev_node, "msi-parent", "#msi-cells",
  661. 0, &msi_spec);
  662. if (ret)
  663. return ret;
  664. if ((*msi_node && *msi_node != msi_spec.np) || msi_spec.args_count != 0)
  665. ret = -EINVAL;
  666. if (!ret) {
  667. /* Return with a node reference held */
  668. *msi_node = msi_spec.np;
  669. return 0;
  670. }
  671. of_node_put(msi_spec.np);
  672. return ret;
  673. }
  674. /**
  675. * of_msi_xlate - map a MSI ID and find relevant MSI controller node
  676. * @dev: device for which the mapping is to be done.
  677. * @msi_np: Pointer to target MSI controller node
  678. * @id_in: Device ID.
  679. *
  680. * Walk up the device hierarchy looking for devices with a "msi-map"
  681. * or "msi-parent" property. If found, apply the mapping to @id_in.
  682. * If @msi_np points to a non-NULL device node pointer, only entries targeting
  683. * that node will be matched; if it points to a NULL value, it will receive the
  684. * device node of the first matching target phandle, with a reference held.
  685. *
  686. * Returns: The mapped MSI id.
  687. */
  688. u32 of_msi_xlate(struct device *dev, struct device_node **msi_np, u32 id_in)
  689. {
  690. struct device *parent_dev;
  691. u32 id_out = id_in;
  692. /*
  693. * Walk up the device parent links looking for one with a
  694. * "msi-map" or an "msi-parent" property.
  695. */
  696. for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent) {
  697. if (!of_map_id(parent_dev->of_node, id_in, "msi-map",
  698. "msi-map-mask", msi_np, &id_out))
  699. break;
  700. if (!of_check_msi_parent(parent_dev->of_node, msi_np))
  701. break;
  702. }
  703. return id_out;
  704. }
  705. EXPORT_SYMBOL_GPL(of_msi_xlate);
  706. /**
  707. * of_msi_map_get_device_domain - Use msi-map to find the relevant MSI domain
  708. * @dev: device for which the mapping is to be done.
  709. * @id: Device ID.
  710. * @bus_token: Bus token
  711. *
  712. * Walk up the device hierarchy looking for devices with a "msi-map"
  713. * property.
  714. *
  715. * Returns: the MSI domain for this device (or NULL on failure)
  716. */
  717. struct irq_domain *of_msi_map_get_device_domain(struct device *dev, u32 id,
  718. u32 bus_token)
  719. {
  720. struct device_node *np = NULL;
  721. of_msi_xlate(dev, &np, id);
  722. return irq_find_matching_host(np, bus_token);
  723. }
  724. /**
  725. * of_msi_get_domain - Use msi-parent to find the relevant MSI domain
  726. * @dev: device for which the domain is requested
  727. * @np: device node for @dev
  728. * @token: bus type for this domain
  729. *
  730. * Parse the msi-parent property and returns the corresponding MSI domain.
  731. *
  732. * Returns: the MSI domain for this device (or NULL on failure).
  733. */
  734. struct irq_domain *of_msi_get_domain(struct device *dev,
  735. const struct device_node *np,
  736. enum irq_domain_bus_token token)
  737. {
  738. struct of_phandle_iterator it;
  739. struct irq_domain *d;
  740. int err;
  741. of_for_each_phandle(&it, err, np, "msi-parent", "#msi-cells", 0) {
  742. d = irq_find_matching_host(it.node, token);
  743. if (d) {
  744. of_node_put(it.node);
  745. return d;
  746. }
  747. }
  748. return NULL;
  749. }
  750. EXPORT_SYMBOL_GPL(of_msi_get_domain);
  751. /**
  752. * of_msi_configure - Set the msi_domain field of a device
  753. * @dev: device structure to associate with an MSI irq domain
  754. * @np: device node for that device
  755. */
  756. void of_msi_configure(struct device *dev, const struct device_node *np)
  757. {
  758. dev_set_msi_domain(dev,
  759. of_msi_get_domain(dev, np, DOMAIN_BUS_PLATFORM_MSI));
  760. }
  761. EXPORT_SYMBOL_GPL(of_msi_configure);