irq.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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, 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. 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. /**
  138. * of_irq_parse_raw - Low level interrupt tree parsing
  139. * @addr: address specifier (start of "reg" property of the device) in be32 format
  140. * @out_irq: structure of_phandle_args updated by this function
  141. *
  142. * This function is a low-level interrupt tree walking function. It
  143. * can be used to do a partial walk with synthetized reg and interrupts
  144. * properties, for example when resolving PCI interrupts when no device
  145. * node exist for the parent. It takes an interrupt specifier structure as
  146. * input, walks the tree looking for any interrupt-map properties, translates
  147. * the specifier for each map, and then returns the translated map.
  148. *
  149. * Return: 0 on success and a negative number on error
  150. *
  151. * Note: refcount of node @out_irq->np is increased by 1 on success.
  152. */
  153. int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
  154. {
  155. struct device_node *ipar, *tnode, *old = NULL;
  156. __be32 initial_match_array[MAX_PHANDLE_ARGS];
  157. const __be32 *match_array = initial_match_array;
  158. const __be32 *tmp, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) };
  159. u32 intsize = 1, addrsize;
  160. int i, rc = -EINVAL;
  161. #ifdef DEBUG
  162. of_print_phandle_args("of_irq_parse_raw: ", out_irq);
  163. #endif
  164. ipar = of_node_get(out_irq->np);
  165. /* First get the #interrupt-cells property of the current cursor
  166. * that tells us how to interpret the passed-in intspec. If there
  167. * is none, we are nice and just walk up the tree
  168. */
  169. do {
  170. if (!of_property_read_u32(ipar, "#interrupt-cells", &intsize))
  171. break;
  172. tnode = ipar;
  173. ipar = of_irq_find_parent(ipar);
  174. of_node_put(tnode);
  175. } while (ipar);
  176. if (ipar == NULL) {
  177. pr_debug(" -> no parent found !\n");
  178. goto fail;
  179. }
  180. pr_debug("of_irq_parse_raw: ipar=%pOF, size=%d\n", ipar, intsize);
  181. if (out_irq->args_count != intsize)
  182. goto fail;
  183. /* Look for this #address-cells. We have to implement the old linux
  184. * trick of looking for the parent here as some device-trees rely on it
  185. */
  186. old = of_node_get(ipar);
  187. do {
  188. tmp = of_get_property(old, "#address-cells", NULL);
  189. tnode = of_get_parent(old);
  190. of_node_put(old);
  191. old = tnode;
  192. } while (old && tmp == NULL);
  193. of_node_put(old);
  194. old = NULL;
  195. addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
  196. pr_debug(" -> addrsize=%d\n", addrsize);
  197. /* Range check so that the temporary buffer doesn't overflow */
  198. if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) {
  199. rc = -EFAULT;
  200. goto fail;
  201. }
  202. /* Precalculate the match array - this simplifies match loop */
  203. for (i = 0; i < addrsize; i++)
  204. initial_match_array[i] = addr ? addr[i] : 0;
  205. for (i = 0; i < intsize; i++)
  206. initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);
  207. /* Now start the actual "proper" walk of the interrupt tree */
  208. while (ipar != NULL) {
  209. int imaplen, match;
  210. const __be32 *imap, *oldimap, *imask;
  211. struct device_node *newpar;
  212. /*
  213. * Now check if cursor is an interrupt-controller and
  214. * if it is then we are done, unless there is an
  215. * interrupt-map which takes precedence except on one
  216. * of these broken platforms that want to parse
  217. * interrupt-map themselves for $reason.
  218. */
  219. bool intc = of_property_read_bool(ipar, "interrupt-controller");
  220. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  221. if (intc &&
  222. (!imap || of_device_compatible_match(ipar, of_irq_imap_abusers))) {
  223. pr_debug(" -> got it !\n");
  224. return 0;
  225. }
  226. /*
  227. * interrupt-map parsing does not work without a reg
  228. * property when #address-cells != 0
  229. */
  230. if (addrsize && !addr) {
  231. pr_debug(" -> no reg passed in when needed !\n");
  232. goto fail;
  233. }
  234. /* No interrupt map, check for an interrupt parent */
  235. if (imap == NULL) {
  236. pr_debug(" -> no map, getting parent\n");
  237. newpar = of_irq_find_parent(ipar);
  238. goto skiplevel;
  239. }
  240. imaplen /= sizeof(u32);
  241. /* Look for a mask */
  242. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  243. if (!imask)
  244. imask = dummy_imask;
  245. /* Parse interrupt-map */
  246. match = 0;
  247. while (imaplen > (addrsize + intsize + 1)) {
  248. /* Compare specifiers */
  249. match = 1;
  250. for (i = 0; i < (addrsize + intsize); i++, imaplen--)
  251. match &= !((match_array[i] ^ *imap++) & imask[i]);
  252. pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
  253. oldimap = imap;
  254. imap = of_irq_parse_imap_parent(oldimap, imaplen, out_irq);
  255. if (!imap)
  256. goto fail;
  257. match &= of_device_is_available(out_irq->np);
  258. if (match)
  259. break;
  260. of_node_put(out_irq->np);
  261. imaplen -= imap - oldimap;
  262. pr_debug(" -> imaplen=%d\n", imaplen);
  263. }
  264. if (!match)
  265. goto fail;
  266. /*
  267. * Successfully parsed an interrupt-map translation; copy new
  268. * interrupt specifier into the out_irq structure
  269. */
  270. match_array = oldimap + 1;
  271. newpar = out_irq->np;
  272. intsize = out_irq->args_count;
  273. addrsize = (imap - match_array) - intsize;
  274. if (ipar == newpar) {
  275. /*
  276. * We got @ipar's refcount, but the refcount was
  277. * gotten again by of_irq_parse_imap_parent() via its
  278. * alias @newpar.
  279. */
  280. of_node_put(ipar);
  281. pr_debug("%pOF interrupt-map entry to self\n", ipar);
  282. return 0;
  283. }
  284. skiplevel:
  285. /* Iterate again with new parent */
  286. pr_debug(" -> new parent: %pOF\n", newpar);
  287. of_node_put(ipar);
  288. ipar = newpar;
  289. newpar = NULL;
  290. }
  291. rc = -ENOENT; /* No interrupt-map found */
  292. fail:
  293. of_node_put(ipar);
  294. return rc;
  295. }
  296. EXPORT_SYMBOL_GPL(of_irq_parse_raw);
  297. /**
  298. * of_irq_parse_one - Resolve an interrupt for a device
  299. * @device: the device whose interrupt is to be resolved
  300. * @index: index of the interrupt to resolve
  301. * @out_irq: structure of_phandle_args filled by this function
  302. *
  303. * This function resolves an interrupt for a node by walking the interrupt tree,
  304. * finding which interrupt controller node it is attached to, and returning the
  305. * interrupt specifier that can be used to retrieve a Linux IRQ number.
  306. *
  307. * Note: refcount of node @out_irq->np is increased by 1 on success.
  308. */
  309. int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
  310. {
  311. struct device_node __free(device_node) *p = NULL;
  312. const __be32 *addr;
  313. u32 intsize;
  314. int i, res, addr_len;
  315. __be32 addr_buf[3] = { 0 };
  316. pr_debug("of_irq_parse_one: dev=%pOF, index=%d\n", device, index);
  317. /* OldWorld mac stuff is "special", handle out of line */
  318. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  319. return of_irq_parse_oldworld(device, index, out_irq);
  320. /* Get the reg property (if any) */
  321. addr_len = 0;
  322. addr = of_get_property(device, "reg", &addr_len);
  323. /* Prevent out-of-bounds read in case of longer interrupt parent address size */
  324. if (addr_len > sizeof(addr_buf))
  325. addr_len = sizeof(addr_buf);
  326. if (addr)
  327. memcpy(addr_buf, addr, addr_len);
  328. /* Try the new-style interrupts-extended first */
  329. res = of_parse_phandle_with_args(device, "interrupts-extended",
  330. "#interrupt-cells", index, out_irq);
  331. if (!res) {
  332. p = out_irq->np;
  333. } else {
  334. /* Look for the interrupt parent. */
  335. p = of_irq_find_parent(device);
  336. /* Get size of interrupt specifier */
  337. if (!p || of_property_read_u32(p, "#interrupt-cells", &intsize))
  338. return -EINVAL;
  339. pr_debug(" parent=%pOF, intsize=%d\n", p, intsize);
  340. /* Copy intspec into irq structure */
  341. out_irq->np = p;
  342. out_irq->args_count = intsize;
  343. for (i = 0; i < intsize; i++) {
  344. res = of_property_read_u32_index(device, "interrupts",
  345. (index * intsize) + i,
  346. out_irq->args + i);
  347. if (res)
  348. return res;
  349. }
  350. pr_debug(" intspec=%d\n", *out_irq->args);
  351. }
  352. /* Check if there are any interrupt-map translations to process */
  353. return of_irq_parse_raw(addr_buf, out_irq);
  354. }
  355. EXPORT_SYMBOL_GPL(of_irq_parse_one);
  356. /**
  357. * of_irq_to_resource - Decode a node's IRQ and return it as a resource
  358. * @dev: pointer to device tree node
  359. * @index: zero-based index of the irq
  360. * @r: pointer to resource structure to return result into.
  361. */
  362. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  363. {
  364. int irq = of_irq_get(dev, index);
  365. if (irq < 0)
  366. return irq;
  367. /* Only dereference the resource if both the
  368. * resource and the irq are valid. */
  369. if (r && irq) {
  370. const char *name = NULL;
  371. memset(r, 0, sizeof(*r));
  372. /*
  373. * Get optional "interrupt-names" property to add a name
  374. * to the resource.
  375. */
  376. of_property_read_string_index(dev, "interrupt-names", index,
  377. &name);
  378. *r = DEFINE_RES_IRQ_NAMED(irq, name ?: of_node_full_name(dev));
  379. r->flags |= irq_get_trigger_type(irq);
  380. }
  381. return irq;
  382. }
  383. EXPORT_SYMBOL_GPL(of_irq_to_resource);
  384. /**
  385. * of_irq_get - Decode a node's IRQ and return it as a Linux IRQ number
  386. * @dev: pointer to device tree node
  387. * @index: zero-based index of the IRQ
  388. *
  389. * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
  390. * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
  391. * of any other failure.
  392. */
  393. int of_irq_get(struct device_node *dev, int index)
  394. {
  395. int rc;
  396. struct of_phandle_args oirq;
  397. struct irq_domain *domain;
  398. rc = of_irq_parse_one(dev, index, &oirq);
  399. if (rc)
  400. return rc;
  401. domain = irq_find_host(oirq.np);
  402. if (!domain) {
  403. rc = -EPROBE_DEFER;
  404. goto out;
  405. }
  406. rc = irq_create_of_mapping(&oirq);
  407. out:
  408. of_node_put(oirq.np);
  409. return rc;
  410. }
  411. EXPORT_SYMBOL_GPL(of_irq_get);
  412. /**
  413. * of_irq_get_byname - Decode a node's IRQ and return it as a Linux IRQ number
  414. * @dev: pointer to device tree node
  415. * @name: IRQ name
  416. *
  417. * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
  418. * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
  419. * of any other failure.
  420. */
  421. int of_irq_get_byname(struct device_node *dev, const char *name)
  422. {
  423. int index;
  424. if (unlikely(!name))
  425. return -EINVAL;
  426. index = of_property_match_string(dev, "interrupt-names", name);
  427. if (index < 0)
  428. return index;
  429. return of_irq_get(dev, index);
  430. }
  431. EXPORT_SYMBOL_GPL(of_irq_get_byname);
  432. /**
  433. * of_irq_count - Count the number of IRQs a node uses
  434. * @dev: pointer to device tree node
  435. */
  436. int of_irq_count(struct device_node *dev)
  437. {
  438. struct of_phandle_args irq;
  439. int nr = 0;
  440. while (of_irq_parse_one(dev, nr, &irq) == 0) {
  441. of_node_put(irq.np);
  442. nr++;
  443. }
  444. return nr;
  445. }
  446. /**
  447. * of_irq_to_resource_table - Fill in resource table with node's IRQ info
  448. * @dev: pointer to device tree node
  449. * @res: array of resources to fill in
  450. * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
  451. *
  452. * Return: The size of the filled in table (up to @nr_irqs).
  453. */
  454. int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
  455. int nr_irqs)
  456. {
  457. int i;
  458. for (i = 0; i < nr_irqs; i++, res++)
  459. if (of_irq_to_resource(dev, i, res) <= 0)
  460. break;
  461. return i;
  462. }
  463. EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
  464. struct of_intc_desc {
  465. struct list_head list;
  466. of_irq_init_cb_t irq_init_cb;
  467. struct device_node *dev;
  468. struct device_node *interrupt_parent;
  469. };
  470. /**
  471. * of_irq_init - Scan and init matching interrupt controllers in DT
  472. * @matches: 0 terminated array of nodes to match and init function to call
  473. *
  474. * This function scans the device tree for matching interrupt controller nodes,
  475. * and calls their initialization functions in order with parents first.
  476. */
  477. void __init of_irq_init(const struct of_device_id *matches)
  478. {
  479. const struct of_device_id *match;
  480. struct device_node *np, *parent = NULL;
  481. struct of_intc_desc *desc, *temp_desc;
  482. struct list_head intc_desc_list, intc_parent_list;
  483. INIT_LIST_HEAD(&intc_desc_list);
  484. INIT_LIST_HEAD(&intc_parent_list);
  485. for_each_matching_node_and_match(np, matches, &match) {
  486. if (!of_property_read_bool(np, "interrupt-controller") ||
  487. !of_device_is_available(np))
  488. continue;
  489. if (WARN(!match->data, "of_irq_init: no init function for %s\n",
  490. match->compatible))
  491. continue;
  492. /*
  493. * Here, we allocate and populate an of_intc_desc with the node
  494. * pointer, interrupt-parent device_node etc.
  495. */
  496. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  497. if (!desc) {
  498. of_node_put(np);
  499. goto err;
  500. }
  501. desc->irq_init_cb = match->data;
  502. desc->dev = of_node_get(np);
  503. /*
  504. * interrupts-extended can reference multiple parent domains.
  505. * Arbitrarily pick the first one; assume any other parents
  506. * are the same distance away from the root irq controller.
  507. */
  508. desc->interrupt_parent = of_parse_phandle(np, "interrupts-extended", 0);
  509. if (!desc->interrupt_parent)
  510. desc->interrupt_parent = of_irq_find_parent(np);
  511. if (desc->interrupt_parent == np) {
  512. of_node_put(desc->interrupt_parent);
  513. desc->interrupt_parent = NULL;
  514. }
  515. list_add_tail(&desc->list, &intc_desc_list);
  516. }
  517. /*
  518. * The root irq controller is the one without an interrupt-parent.
  519. * That one goes first, followed by the controllers that reference it,
  520. * followed by the ones that reference the 2nd level controllers, etc.
  521. */
  522. while (!list_empty(&intc_desc_list)) {
  523. /*
  524. * Process all controllers with the current 'parent'.
  525. * First pass will be looking for NULL as the parent.
  526. * The assumption is that NULL parent means a root controller.
  527. */
  528. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  529. int ret;
  530. if (desc->interrupt_parent != parent)
  531. continue;
  532. list_del(&desc->list);
  533. of_node_set_flag(desc->dev, OF_POPULATED);
  534. pr_debug("of_irq_init: init %pOF (%p), parent %p\n",
  535. desc->dev,
  536. desc->dev, desc->interrupt_parent);
  537. ret = desc->irq_init_cb(desc->dev,
  538. desc->interrupt_parent);
  539. if (ret) {
  540. pr_err("%s: Failed to init %pOF (%p), parent %p\n",
  541. __func__, desc->dev, desc->dev,
  542. desc->interrupt_parent);
  543. of_node_clear_flag(desc->dev, OF_POPULATED);
  544. of_node_put(desc->interrupt_parent);
  545. of_node_put(desc->dev);
  546. kfree(desc);
  547. continue;
  548. }
  549. /*
  550. * This one is now set up; add it to the parent list so
  551. * its children can get processed in a subsequent pass.
  552. */
  553. list_add_tail(&desc->list, &intc_parent_list);
  554. }
  555. /* Get the next pending parent that might have children */
  556. desc = list_first_entry_or_null(&intc_parent_list,
  557. typeof(*desc), list);
  558. if (!desc) {
  559. pr_err("of_irq_init: children remain, but no parents\n");
  560. break;
  561. }
  562. list_del(&desc->list);
  563. parent = desc->dev;
  564. kfree(desc);
  565. }
  566. list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
  567. list_del(&desc->list);
  568. kfree(desc);
  569. }
  570. err:
  571. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  572. list_del(&desc->list);
  573. of_node_put(desc->interrupt_parent);
  574. of_node_put(desc->dev);
  575. kfree(desc);
  576. }
  577. }
  578. static u32 __of_msi_map_id(struct device *dev, struct device_node **np,
  579. u32 id_in)
  580. {
  581. struct device *parent_dev;
  582. u32 id_out = id_in;
  583. /*
  584. * Walk up the device parent links looking for one with a
  585. * "msi-map" property.
  586. */
  587. for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
  588. if (!of_map_id(parent_dev->of_node, id_in, "msi-map",
  589. "msi-map-mask", np, &id_out))
  590. break;
  591. return id_out;
  592. }
  593. /**
  594. * of_msi_map_id - Map a MSI ID for a device.
  595. * @dev: device for which the mapping is to be done.
  596. * @msi_np: device node of the expected msi controller.
  597. * @id_in: unmapped MSI ID for the device.
  598. *
  599. * Walk up the device hierarchy looking for devices with a "msi-map"
  600. * property. If found, apply the mapping to @id_in.
  601. *
  602. * Return: The mapped MSI ID.
  603. */
  604. u32 of_msi_map_id(struct device *dev, struct device_node *msi_np, u32 id_in)
  605. {
  606. return __of_msi_map_id(dev, &msi_np, id_in);
  607. }
  608. /**
  609. * of_msi_map_get_device_domain - Use msi-map to find the relevant MSI domain
  610. * @dev: device for which the mapping is to be done.
  611. * @id: Device ID.
  612. * @bus_token: Bus token
  613. *
  614. * Walk up the device hierarchy looking for devices with a "msi-map"
  615. * property.
  616. *
  617. * Returns: the MSI domain for this device (or NULL on failure)
  618. */
  619. struct irq_domain *of_msi_map_get_device_domain(struct device *dev, u32 id,
  620. u32 bus_token)
  621. {
  622. struct device_node *np = NULL;
  623. __of_msi_map_id(dev, &np, id);
  624. return irq_find_matching_host(np, bus_token);
  625. }
  626. /**
  627. * of_msi_get_domain - Use msi-parent to find the relevant MSI domain
  628. * @dev: device for which the domain is requested
  629. * @np: device node for @dev
  630. * @token: bus type for this domain
  631. *
  632. * Parse the msi-parent property and returns the corresponding MSI domain.
  633. *
  634. * Returns: the MSI domain for this device (or NULL on failure).
  635. */
  636. struct irq_domain *of_msi_get_domain(struct device *dev,
  637. struct device_node *np,
  638. enum irq_domain_bus_token token)
  639. {
  640. struct of_phandle_iterator it;
  641. struct irq_domain *d;
  642. int err;
  643. of_for_each_phandle(&it, err, np, "msi-parent", "#msi-cells", 0) {
  644. d = irq_find_matching_host(it.node, token);
  645. if (d)
  646. return d;
  647. }
  648. return NULL;
  649. }
  650. EXPORT_SYMBOL_GPL(of_msi_get_domain);
  651. /**
  652. * of_msi_configure - Set the msi_domain field of a device
  653. * @dev: device structure to associate with an MSI irq domain
  654. * @np: device node for that device
  655. */
  656. void of_msi_configure(struct device *dev, struct device_node *np)
  657. {
  658. dev_set_msi_domain(dev,
  659. of_msi_get_domain(dev, np, DOMAIN_BUS_PLATFORM_MSI));
  660. }
  661. EXPORT_SYMBOL_GPL(of_msi_configure);