irq.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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 ([email protected])
  7. * Updated and modified by Cort Dougan <[email protected]>
  8. * Copyright (C) 1996-2001 Cort Dougan
  9. * Adapted for Power Macintosh by Paul Mackerras
  10. * Copyright (C) 1996 Paul Mackerras ([email protected])
  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/device.h>
  18. #include <linux/errno.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/string.h>
  24. #include <linux/slab.h>
  25. /**
  26. * irq_of_parse_and_map - Parse and map an interrupt into linux virq space
  27. * @dev: Device node of the device whose interrupt is to be mapped
  28. * @index: Index of the interrupt to map
  29. *
  30. * This function is a wrapper that chains of_irq_parse_one() and
  31. * irq_create_of_mapping() to make things easier to callers
  32. */
  33. unsigned int irq_of_parse_and_map(struct device_node *dev, int index)
  34. {
  35. struct of_phandle_args oirq;
  36. if (of_irq_parse_one(dev, index, &oirq))
  37. return 0;
  38. return irq_create_of_mapping(&oirq);
  39. }
  40. EXPORT_SYMBOL_GPL(irq_of_parse_and_map);
  41. /**
  42. * of_irq_find_parent - Given a device node, find its interrupt parent node
  43. * @child: pointer to device node
  44. *
  45. * Return: A pointer to the interrupt parent node, or NULL if the interrupt
  46. * parent could not be determined.
  47. */
  48. struct device_node *of_irq_find_parent(struct device_node *child)
  49. {
  50. struct device_node *p;
  51. phandle parent;
  52. if (!of_node_get(child))
  53. return NULL;
  54. do {
  55. if (of_property_read_u32(child, "interrupt-parent", &parent)) {
  56. p = of_get_parent(child);
  57. } else {
  58. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  59. p = of_node_get(of_irq_dflt_pic);
  60. else
  61. p = of_find_node_by_phandle(parent);
  62. }
  63. of_node_put(child);
  64. child = p;
  65. } while (p && of_get_property(p, "#interrupt-cells", NULL) == NULL);
  66. return p;
  67. }
  68. EXPORT_SYMBOL_GPL(of_irq_find_parent);
  69. /*
  70. * These interrupt controllers abuse interrupt-map for unspeakable
  71. * reasons and rely on the core code to *ignore* it (the drivers do
  72. * their own parsing of the property).
  73. *
  74. * If you think of adding to the list for something *new*, think
  75. * again. There is a high chance that you will be sent back to the
  76. * drawing board.
  77. */
  78. static const char * const of_irq_imap_abusers[] = {
  79. "CBEA,platform-spider-pic",
  80. "sti,platform-spider-pic",
  81. "realtek,rtl-intc",
  82. "fsl,ls1021a-extirq",
  83. "fsl,ls1043a-extirq",
  84. "fsl,ls1088a-extirq",
  85. "renesas,rza1-irqc",
  86. NULL,
  87. };
  88. /**
  89. * of_irq_parse_raw - Low level interrupt tree parsing
  90. * @addr: address specifier (start of "reg" property of the device) in be32 format
  91. * @out_irq: structure of_phandle_args updated by this function
  92. *
  93. * This function is a low-level interrupt tree walking function. It
  94. * can be used to do a partial walk with synthetized reg and interrupts
  95. * properties, for example when resolving PCI interrupts when no device
  96. * node exist for the parent. It takes an interrupt specifier structure as
  97. * input, walks the tree looking for any interrupt-map properties, translates
  98. * the specifier for each map, and then returns the translated map.
  99. *
  100. * Return: 0 on success and a negative number on error
  101. */
  102. int of_irq_parse_raw(const __be32 *addr, struct of_phandle_args *out_irq)
  103. {
  104. struct device_node *ipar, *tnode, *old = NULL, *newpar = NULL;
  105. __be32 initial_match_array[MAX_PHANDLE_ARGS];
  106. const __be32 *match_array = initial_match_array;
  107. const __be32 *tmp, *imap, *imask, dummy_imask[] = { [0 ... MAX_PHANDLE_ARGS] = cpu_to_be32(~0) };
  108. u32 intsize = 1, addrsize, newintsize = 0, newaddrsize = 0;
  109. int imaplen, match, i, rc = -EINVAL;
  110. #ifdef DEBUG
  111. of_print_phandle_args("of_irq_parse_raw: ", out_irq);
  112. #endif
  113. ipar = of_node_get(out_irq->np);
  114. /* First get the #interrupt-cells property of the current cursor
  115. * that tells us how to interpret the passed-in intspec. If there
  116. * is none, we are nice and just walk up the tree
  117. */
  118. do {
  119. if (!of_property_read_u32(ipar, "#interrupt-cells", &intsize))
  120. break;
  121. tnode = ipar;
  122. ipar = of_irq_find_parent(ipar);
  123. of_node_put(tnode);
  124. } while (ipar);
  125. if (ipar == NULL) {
  126. pr_debug(" -> no parent found !\n");
  127. goto fail;
  128. }
  129. pr_debug("of_irq_parse_raw: ipar=%pOF, size=%d\n", ipar, intsize);
  130. if (out_irq->args_count != intsize)
  131. goto fail;
  132. /* Look for this #address-cells. We have to implement the old linux
  133. * trick of looking for the parent here as some device-trees rely on it
  134. */
  135. old = of_node_get(ipar);
  136. do {
  137. tmp = of_get_property(old, "#address-cells", NULL);
  138. tnode = of_get_parent(old);
  139. of_node_put(old);
  140. old = tnode;
  141. } while (old && tmp == NULL);
  142. of_node_put(old);
  143. old = NULL;
  144. addrsize = (tmp == NULL) ? 2 : be32_to_cpu(*tmp);
  145. pr_debug(" -> addrsize=%d\n", addrsize);
  146. /* Range check so that the temporary buffer doesn't overflow */
  147. if (WARN_ON(addrsize + intsize > MAX_PHANDLE_ARGS)) {
  148. rc = -EFAULT;
  149. goto fail;
  150. }
  151. /* Precalculate the match array - this simplifies match loop */
  152. for (i = 0; i < addrsize; i++)
  153. initial_match_array[i] = addr ? addr[i] : 0;
  154. for (i = 0; i < intsize; i++)
  155. initial_match_array[addrsize + i] = cpu_to_be32(out_irq->args[i]);
  156. /* Now start the actual "proper" walk of the interrupt tree */
  157. while (ipar != NULL) {
  158. /*
  159. * Now check if cursor is an interrupt-controller and
  160. * if it is then we are done, unless there is an
  161. * interrupt-map which takes precedence except on one
  162. * of these broken platforms that want to parse
  163. * interrupt-map themselves for $reason.
  164. */
  165. bool intc = of_property_read_bool(ipar, "interrupt-controller");
  166. imap = of_get_property(ipar, "interrupt-map", &imaplen);
  167. if (intc &&
  168. (!imap || of_device_compatible_match(ipar, of_irq_imap_abusers))) {
  169. pr_debug(" -> got it !\n");
  170. return 0;
  171. }
  172. /*
  173. * interrupt-map parsing does not work without a reg
  174. * property when #address-cells != 0
  175. */
  176. if (addrsize && !addr) {
  177. pr_debug(" -> no reg passed in when needed !\n");
  178. goto fail;
  179. }
  180. /* No interrupt map, check for an interrupt parent */
  181. if (imap == NULL) {
  182. pr_debug(" -> no map, getting parent\n");
  183. newpar = of_irq_find_parent(ipar);
  184. goto skiplevel;
  185. }
  186. imaplen /= sizeof(u32);
  187. /* Look for a mask */
  188. imask = of_get_property(ipar, "interrupt-map-mask", NULL);
  189. if (!imask)
  190. imask = dummy_imask;
  191. /* Parse interrupt-map */
  192. match = 0;
  193. while (imaplen > (addrsize + intsize + 1) && !match) {
  194. /* Compare specifiers */
  195. match = 1;
  196. for (i = 0; i < (addrsize + intsize); i++, imaplen--)
  197. match &= !((match_array[i] ^ *imap++) & imask[i]);
  198. pr_debug(" -> match=%d (imaplen=%d)\n", match, imaplen);
  199. /* Get the interrupt parent */
  200. if (of_irq_workarounds & OF_IMAP_NO_PHANDLE)
  201. newpar = of_node_get(of_irq_dflt_pic);
  202. else
  203. newpar = of_find_node_by_phandle(be32_to_cpup(imap));
  204. imap++;
  205. --imaplen;
  206. /* Check if not found */
  207. if (newpar == NULL) {
  208. pr_debug(" -> imap parent not found !\n");
  209. goto fail;
  210. }
  211. if (!of_device_is_available(newpar))
  212. match = 0;
  213. /* Get #interrupt-cells and #address-cells of new
  214. * parent
  215. */
  216. if (of_property_read_u32(newpar, "#interrupt-cells",
  217. &newintsize)) {
  218. pr_debug(" -> parent lacks #interrupt-cells!\n");
  219. goto fail;
  220. }
  221. if (of_property_read_u32(newpar, "#address-cells",
  222. &newaddrsize))
  223. newaddrsize = 0;
  224. pr_debug(" -> newintsize=%d, newaddrsize=%d\n",
  225. newintsize, newaddrsize);
  226. /* Check for malformed properties */
  227. if (WARN_ON(newaddrsize + newintsize > MAX_PHANDLE_ARGS)
  228. || (imaplen < (newaddrsize + newintsize))) {
  229. rc = -EFAULT;
  230. goto fail;
  231. }
  232. imap += newaddrsize + newintsize;
  233. imaplen -= newaddrsize + newintsize;
  234. pr_debug(" -> imaplen=%d\n", imaplen);
  235. }
  236. if (!match) {
  237. if (intc) {
  238. /*
  239. * The PASEMI Nemo is a known offender, so
  240. * let's only warn for anyone else.
  241. */
  242. WARN(!IS_ENABLED(CONFIG_PPC_PASEMI),
  243. "%pOF interrupt-map failed, using interrupt-controller\n",
  244. ipar);
  245. return 0;
  246. }
  247. goto fail;
  248. }
  249. /*
  250. * Successfully parsed an interrupt-map translation; copy new
  251. * interrupt specifier into the out_irq structure
  252. */
  253. match_array = imap - newaddrsize - newintsize;
  254. for (i = 0; i < newintsize; i++)
  255. out_irq->args[i] = be32_to_cpup(imap - newintsize + i);
  256. out_irq->args_count = intsize = newintsize;
  257. addrsize = newaddrsize;
  258. if (ipar == newpar) {
  259. pr_debug("%pOF interrupt-map entry to self\n", ipar);
  260. return 0;
  261. }
  262. skiplevel:
  263. /* Iterate again with new parent */
  264. out_irq->np = newpar;
  265. pr_debug(" -> new parent: %pOF\n", newpar);
  266. of_node_put(ipar);
  267. ipar = newpar;
  268. newpar = NULL;
  269. }
  270. rc = -ENOENT; /* No interrupt-map found */
  271. fail:
  272. of_node_put(ipar);
  273. of_node_put(newpar);
  274. return rc;
  275. }
  276. EXPORT_SYMBOL_GPL(of_irq_parse_raw);
  277. /**
  278. * of_irq_parse_one - Resolve an interrupt for a device
  279. * @device: the device whose interrupt is to be resolved
  280. * @index: index of the interrupt to resolve
  281. * @out_irq: structure of_phandle_args filled by this function
  282. *
  283. * This function resolves an interrupt for a node by walking the interrupt tree,
  284. * finding which interrupt controller node it is attached to, and returning the
  285. * interrupt specifier that can be used to retrieve a Linux IRQ number.
  286. */
  287. int of_irq_parse_one(struct device_node *device, int index, struct of_phandle_args *out_irq)
  288. {
  289. struct device_node *p;
  290. const __be32 *addr;
  291. u32 intsize;
  292. int i, res;
  293. pr_debug("of_irq_parse_one: dev=%pOF, index=%d\n", device, index);
  294. /* OldWorld mac stuff is "special", handle out of line */
  295. if (of_irq_workarounds & OF_IMAP_OLDWORLD_MAC)
  296. return of_irq_parse_oldworld(device, index, out_irq);
  297. /* Get the reg property (if any) */
  298. addr = of_get_property(device, "reg", NULL);
  299. /* Try the new-style interrupts-extended first */
  300. res = of_parse_phandle_with_args(device, "interrupts-extended",
  301. "#interrupt-cells", index, out_irq);
  302. if (!res)
  303. return of_irq_parse_raw(addr, out_irq);
  304. /* Look for the interrupt parent. */
  305. p = of_irq_find_parent(device);
  306. if (p == NULL)
  307. return -EINVAL;
  308. /* Get size of interrupt specifier */
  309. if (of_property_read_u32(p, "#interrupt-cells", &intsize)) {
  310. res = -EINVAL;
  311. goto out;
  312. }
  313. pr_debug(" parent=%pOF, intsize=%d\n", p, intsize);
  314. /* Copy intspec into irq structure */
  315. out_irq->np = p;
  316. out_irq->args_count = intsize;
  317. for (i = 0; i < intsize; i++) {
  318. res = of_property_read_u32_index(device, "interrupts",
  319. (index * intsize) + i,
  320. out_irq->args + i);
  321. if (res)
  322. goto out;
  323. }
  324. pr_debug(" intspec=%d\n", *out_irq->args);
  325. /* Check if there are any interrupt-map translations to process */
  326. res = of_irq_parse_raw(addr, out_irq);
  327. out:
  328. of_node_put(p);
  329. return res;
  330. }
  331. EXPORT_SYMBOL_GPL(of_irq_parse_one);
  332. /**
  333. * of_irq_to_resource - Decode a node's IRQ and return it as a resource
  334. * @dev: pointer to device tree node
  335. * @index: zero-based index of the irq
  336. * @r: pointer to resource structure to return result into.
  337. */
  338. int of_irq_to_resource(struct device_node *dev, int index, struct resource *r)
  339. {
  340. int irq = of_irq_get(dev, index);
  341. if (irq < 0)
  342. return irq;
  343. /* Only dereference the resource if both the
  344. * resource and the irq are valid. */
  345. if (r && irq) {
  346. const char *name = NULL;
  347. memset(r, 0, sizeof(*r));
  348. /*
  349. * Get optional "interrupt-names" property to add a name
  350. * to the resource.
  351. */
  352. of_property_read_string_index(dev, "interrupt-names", index,
  353. &name);
  354. r->start = r->end = irq;
  355. r->flags = IORESOURCE_IRQ | irqd_get_trigger_type(irq_get_irq_data(irq));
  356. r->name = name ? name : of_node_full_name(dev);
  357. }
  358. return irq;
  359. }
  360. EXPORT_SYMBOL_GPL(of_irq_to_resource);
  361. /**
  362. * of_irq_get - Decode a node's IRQ and return it as a Linux IRQ number
  363. * @dev: pointer to device tree node
  364. * @index: zero-based index of the IRQ
  365. *
  366. * Return: Linux IRQ number on success, or 0 on the IRQ mapping failure, or
  367. * -EPROBE_DEFER if the IRQ domain is not yet created, or error code in case
  368. * of any other failure.
  369. */
  370. int of_irq_get(struct device_node *dev, int index)
  371. {
  372. int rc;
  373. struct of_phandle_args oirq;
  374. struct irq_domain *domain;
  375. rc = of_irq_parse_one(dev, index, &oirq);
  376. if (rc)
  377. return rc;
  378. domain = irq_find_host(oirq.np);
  379. if (!domain)
  380. return -EPROBE_DEFER;
  381. return irq_create_of_mapping(&oirq);
  382. }
  383. EXPORT_SYMBOL_GPL(of_irq_get);
  384. /**
  385. * of_irq_get_byname - Decode a node's IRQ and return it as a Linux IRQ number
  386. * @dev: pointer to device tree node
  387. * @name: IRQ name
  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_byname(struct device_node *dev, const char *name)
  394. {
  395. int index;
  396. if (unlikely(!name))
  397. return -EINVAL;
  398. index = of_property_match_string(dev, "interrupt-names", name);
  399. if (index < 0)
  400. return index;
  401. return of_irq_get(dev, index);
  402. }
  403. EXPORT_SYMBOL_GPL(of_irq_get_byname);
  404. /**
  405. * of_irq_count - Count the number of IRQs a node uses
  406. * @dev: pointer to device tree node
  407. */
  408. int of_irq_count(struct device_node *dev)
  409. {
  410. struct of_phandle_args irq;
  411. int nr = 0;
  412. while (of_irq_parse_one(dev, nr, &irq) == 0)
  413. nr++;
  414. return nr;
  415. }
  416. /**
  417. * of_irq_to_resource_table - Fill in resource table with node's IRQ info
  418. * @dev: pointer to device tree node
  419. * @res: array of resources to fill in
  420. * @nr_irqs: the number of IRQs (and upper bound for num of @res elements)
  421. *
  422. * Return: The size of the filled in table (up to @nr_irqs).
  423. */
  424. int of_irq_to_resource_table(struct device_node *dev, struct resource *res,
  425. int nr_irqs)
  426. {
  427. int i;
  428. for (i = 0; i < nr_irqs; i++, res++)
  429. if (of_irq_to_resource(dev, i, res) <= 0)
  430. break;
  431. return i;
  432. }
  433. EXPORT_SYMBOL_GPL(of_irq_to_resource_table);
  434. struct of_intc_desc {
  435. struct list_head list;
  436. of_irq_init_cb_t irq_init_cb;
  437. struct device_node *dev;
  438. struct device_node *interrupt_parent;
  439. };
  440. /**
  441. * of_irq_init - Scan and init matching interrupt controllers in DT
  442. * @matches: 0 terminated array of nodes to match and init function to call
  443. *
  444. * This function scans the device tree for matching interrupt controller nodes,
  445. * and calls their initialization functions in order with parents first.
  446. */
  447. void __init of_irq_init(const struct of_device_id *matches)
  448. {
  449. const struct of_device_id *match;
  450. struct device_node *np, *parent = NULL;
  451. struct of_intc_desc *desc, *temp_desc;
  452. struct list_head intc_desc_list, intc_parent_list;
  453. INIT_LIST_HEAD(&intc_desc_list);
  454. INIT_LIST_HEAD(&intc_parent_list);
  455. for_each_matching_node_and_match(np, matches, &match) {
  456. if (!of_property_read_bool(np, "interrupt-controller") ||
  457. !of_device_is_available(np))
  458. continue;
  459. if (WARN(!match->data, "of_irq_init: no init function for %s\n",
  460. match->compatible))
  461. continue;
  462. /*
  463. * Here, we allocate and populate an of_intc_desc with the node
  464. * pointer, interrupt-parent device_node etc.
  465. */
  466. desc = kzalloc(sizeof(*desc), GFP_KERNEL);
  467. if (!desc) {
  468. of_node_put(np);
  469. goto err;
  470. }
  471. desc->irq_init_cb = match->data;
  472. desc->dev = of_node_get(np);
  473. /*
  474. * interrupts-extended can reference multiple parent domains.
  475. * Arbitrarily pick the first one; assume any other parents
  476. * are the same distance away from the root irq controller.
  477. */
  478. desc->interrupt_parent = of_parse_phandle(np, "interrupts-extended", 0);
  479. if (!desc->interrupt_parent)
  480. desc->interrupt_parent = of_irq_find_parent(np);
  481. if (desc->interrupt_parent == np) {
  482. of_node_put(desc->interrupt_parent);
  483. desc->interrupt_parent = NULL;
  484. }
  485. list_add_tail(&desc->list, &intc_desc_list);
  486. }
  487. /*
  488. * The root irq controller is the one without an interrupt-parent.
  489. * That one goes first, followed by the controllers that reference it,
  490. * followed by the ones that reference the 2nd level controllers, etc.
  491. */
  492. while (!list_empty(&intc_desc_list)) {
  493. /*
  494. * Process all controllers with the current 'parent'.
  495. * First pass will be looking for NULL as the parent.
  496. * The assumption is that NULL parent means a root controller.
  497. */
  498. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  499. int ret;
  500. if (desc->interrupt_parent != parent)
  501. continue;
  502. list_del(&desc->list);
  503. of_node_set_flag(desc->dev, OF_POPULATED);
  504. pr_debug("of_irq_init: init %pOF (%p), parent %p\n",
  505. desc->dev,
  506. desc->dev, desc->interrupt_parent);
  507. ret = desc->irq_init_cb(desc->dev,
  508. desc->interrupt_parent);
  509. if (ret) {
  510. pr_err("%s: Failed to init %pOF (%p), parent %p\n",
  511. __func__, desc->dev, desc->dev,
  512. desc->interrupt_parent);
  513. of_node_clear_flag(desc->dev, OF_POPULATED);
  514. kfree(desc);
  515. continue;
  516. }
  517. /*
  518. * This one is now set up; add it to the parent list so
  519. * its children can get processed in a subsequent pass.
  520. */
  521. list_add_tail(&desc->list, &intc_parent_list);
  522. }
  523. /* Get the next pending parent that might have children */
  524. desc = list_first_entry_or_null(&intc_parent_list,
  525. typeof(*desc), list);
  526. if (!desc) {
  527. pr_err("of_irq_init: children remain, but no parents\n");
  528. break;
  529. }
  530. list_del(&desc->list);
  531. parent = desc->dev;
  532. kfree(desc);
  533. }
  534. list_for_each_entry_safe(desc, temp_desc, &intc_parent_list, list) {
  535. list_del(&desc->list);
  536. kfree(desc);
  537. }
  538. err:
  539. list_for_each_entry_safe(desc, temp_desc, &intc_desc_list, list) {
  540. list_del(&desc->list);
  541. of_node_put(desc->dev);
  542. kfree(desc);
  543. }
  544. }
  545. static u32 __of_msi_map_id(struct device *dev, struct device_node **np,
  546. u32 id_in)
  547. {
  548. struct device *parent_dev;
  549. u32 id_out = id_in;
  550. /*
  551. * Walk up the device parent links looking for one with a
  552. * "msi-map" property.
  553. */
  554. for (parent_dev = dev; parent_dev; parent_dev = parent_dev->parent)
  555. if (!of_map_id(parent_dev->of_node, id_in, "msi-map",
  556. "msi-map-mask", np, &id_out))
  557. break;
  558. return id_out;
  559. }
  560. /**
  561. * of_msi_map_id - Map a MSI ID for a device.
  562. * @dev: device for which the mapping is to be done.
  563. * @msi_np: device node of the expected msi controller.
  564. * @id_in: unmapped MSI ID for the device.
  565. *
  566. * Walk up the device hierarchy looking for devices with a "msi-map"
  567. * property. If found, apply the mapping to @id_in.
  568. *
  569. * Return: The mapped MSI ID.
  570. */
  571. u32 of_msi_map_id(struct device *dev, struct device_node *msi_np, u32 id_in)
  572. {
  573. return __of_msi_map_id(dev, &msi_np, id_in);
  574. }
  575. /**
  576. * of_msi_map_get_device_domain - Use msi-map to find the relevant MSI domain
  577. * @dev: device for which the mapping is to be done.
  578. * @id: Device ID.
  579. * @bus_token: Bus token
  580. *
  581. * Walk up the device hierarchy looking for devices with a "msi-map"
  582. * property.
  583. *
  584. * Returns: the MSI domain for this device (or NULL on failure)
  585. */
  586. struct irq_domain *of_msi_map_get_device_domain(struct device *dev, u32 id,
  587. u32 bus_token)
  588. {
  589. struct device_node *np = NULL;
  590. __of_msi_map_id(dev, &np, id);
  591. return irq_find_matching_host(np, bus_token);
  592. }
  593. /**
  594. * of_msi_get_domain - Use msi-parent to find the relevant MSI domain
  595. * @dev: device for which the domain is requested
  596. * @np: device node for @dev
  597. * @token: bus type for this domain
  598. *
  599. * Parse the msi-parent property (both the simple and the complex
  600. * versions), and returns the corresponding MSI domain.
  601. *
  602. * Returns: the MSI domain for this device (or NULL on failure).
  603. */
  604. struct irq_domain *of_msi_get_domain(struct device *dev,
  605. struct device_node *np,
  606. enum irq_domain_bus_token token)
  607. {
  608. struct device_node *msi_np;
  609. struct irq_domain *d;
  610. /* Check for a single msi-parent property */
  611. msi_np = of_parse_phandle(np, "msi-parent", 0);
  612. if (msi_np && !of_property_read_bool(msi_np, "#msi-cells")) {
  613. d = irq_find_matching_host(msi_np, token);
  614. if (!d)
  615. of_node_put(msi_np);
  616. return d;
  617. }
  618. if (token == DOMAIN_BUS_PLATFORM_MSI) {
  619. /* Check for the complex msi-parent version */
  620. struct of_phandle_args args;
  621. int index = 0;
  622. while (!of_parse_phandle_with_args(np, "msi-parent",
  623. "#msi-cells",
  624. index, &args)) {
  625. d = irq_find_matching_host(args.np, token);
  626. if (d)
  627. return d;
  628. of_node_put(args.np);
  629. index++;
  630. }
  631. }
  632. return NULL;
  633. }
  634. /**
  635. * of_msi_configure - Set the msi_domain field of a device
  636. * @dev: device structure to associate with an MSI irq domain
  637. * @np: device node for that device
  638. */
  639. void of_msi_configure(struct device *dev, struct device_node *np)
  640. {
  641. dev_set_msi_domain(dev,
  642. of_msi_get_domain(dev, np, DOMAIN_BUS_PLATFORM_MSI));
  643. }
  644. EXPORT_SYMBOL_GPL(of_msi_configure);