irqdomain.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. #define pr_fmt(fmt) "irq: " fmt
  2. #include <linux/debugfs.h>
  3. #include <linux/hardirq.h>
  4. #include <linux/interrupt.h>
  5. #include <linux/irq.h>
  6. #include <linux/irqdesc.h>
  7. #include <linux/irqdomain.h>
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <linux/topology.h>
  13. #include <linux/seq_file.h>
  14. #include <linux/slab.h>
  15. #include <linux/smp.h>
  16. #include <linux/fs.h>
  17. static LIST_HEAD(irq_domain_list);
  18. static DEFINE_MUTEX(irq_domain_mutex);
  19. static DEFINE_MUTEX(revmap_trees_mutex);
  20. static struct irq_domain *irq_default_domain;
  21. /**
  22. * irq_domain_alloc() - Allocate a new irq_domain data structure
  23. * @of_node: optional device-tree node of the interrupt controller
  24. * @ops: map/unmap domain callbacks
  25. * @host_data: Controller private data pointer
  26. *
  27. * Allocates and initialize and irq_domain structure. Caller is expected to
  28. * register allocated irq_domain with irq_domain_register(). Returns pointer
  29. * to IRQ domain, or NULL on failure.
  30. */
  31. static struct irq_domain *irq_domain_alloc(struct device_node *of_node,
  32. int size,
  33. const struct irq_domain_ops *ops,
  34. void *host_data)
  35. {
  36. struct irq_domain *domain;
  37. domain = kzalloc_node(sizeof(*domain) + (sizeof(unsigned int) * size),
  38. GFP_KERNEL, of_node_to_nid(of_node));
  39. if (WARN_ON(!domain))
  40. return NULL;
  41. /* Fill structure */
  42. INIT_RADIX_TREE(&domain->revmap_tree, GFP_KERNEL);
  43. domain->ops = ops;
  44. domain->host_data = host_data;
  45. domain->of_node = of_node_get(of_node);
  46. domain->revmap_size = size;
  47. return domain;
  48. }
  49. static void irq_domain_free(struct irq_domain *domain)
  50. {
  51. of_node_put(domain->of_node);
  52. kfree(domain);
  53. }
  54. static void irq_domain_add(struct irq_domain *domain)
  55. {
  56. mutex_lock(&irq_domain_mutex);
  57. list_add(&domain->link, &irq_domain_list);
  58. mutex_unlock(&irq_domain_mutex);
  59. pr_debug("Added domain %s\n", domain->name);
  60. }
  61. /**
  62. * irq_domain_remove() - Remove an irq domain.
  63. * @domain: domain to remove
  64. *
  65. * This routine is used to remove an irq domain. The caller must ensure
  66. * that all mappings within the domain have been disposed of prior to
  67. * use, depending on the revmap type.
  68. */
  69. void irq_domain_remove(struct irq_domain *domain)
  70. {
  71. mutex_lock(&irq_domain_mutex);
  72. /*
  73. * radix_tree_delete() takes care of destroying the root
  74. * node when all entries are removed. Shout if there are
  75. * any mappings left.
  76. */
  77. WARN_ON(domain->revmap_tree.height);
  78. list_del(&domain->link);
  79. /*
  80. * If the going away domain is the default one, reset it.
  81. */
  82. if (unlikely(irq_default_domain == domain))
  83. irq_set_default_host(NULL);
  84. mutex_unlock(&irq_domain_mutex);
  85. pr_debug("Removed domain %s\n", domain->name);
  86. irq_domain_free(domain);
  87. }
  88. EXPORT_SYMBOL_GPL(irq_domain_remove);
  89. /**
  90. * irq_domain_add_simple() - Allocate and register a simple irq_domain.
  91. * @of_node: pointer to interrupt controller's device tree node.
  92. * @size: total number of irqs in mapping
  93. * @first_irq: first number of irq block assigned to the domain,
  94. * pass zero to assign irqs on-the-fly. This will result in a
  95. * linear IRQ domain so it is important to use irq_create_mapping()
  96. * for each used IRQ, especially when SPARSE_IRQ is enabled.
  97. * @ops: map/unmap domain callbacks
  98. * @host_data: Controller private data pointer
  99. *
  100. * Allocates a legacy irq_domain if irq_base is positive or a linear
  101. * domain otherwise. For the legacy domain, IRQ descriptors will also
  102. * be allocated.
  103. *
  104. * This is intended to implement the expected behaviour for most
  105. * interrupt controllers which is that a linear mapping should
  106. * normally be used unless the system requires a legacy mapping in
  107. * order to support supplying interrupt numbers during non-DT
  108. * registration of devices.
  109. */
  110. struct irq_domain *irq_domain_add_simple(struct device_node *of_node,
  111. unsigned int size,
  112. unsigned int first_irq,
  113. const struct irq_domain_ops *ops,
  114. void *host_data)
  115. {
  116. if (first_irq > 0) {
  117. int irq_base;
  118. if (IS_ENABLED(CONFIG_SPARSE_IRQ)) {
  119. /*
  120. * Set the descriptor allocator to search for a
  121. * 1-to-1 mapping, such as irq_alloc_desc_at().
  122. * Use of_node_to_nid() which is defined to
  123. * numa_node_id() on platforms that have no custom
  124. * implementation.
  125. */
  126. irq_base = irq_alloc_descs(first_irq, first_irq, size,
  127. of_node_to_nid(of_node));
  128. if (irq_base < 0) {
  129. pr_info("Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
  130. first_irq);
  131. irq_base = first_irq;
  132. }
  133. } else
  134. irq_base = first_irq;
  135. return irq_domain_add_legacy(of_node, size, irq_base, 0,
  136. ops, host_data);
  137. }
  138. /* A linear domain is the default */
  139. return irq_domain_add_linear(of_node, size, ops, host_data);
  140. }
  141. EXPORT_SYMBOL_GPL(irq_domain_add_simple);
  142. /**
  143. * irq_domain_add_legacy() - Allocate and register a legacy revmap irq_domain.
  144. * @of_node: pointer to interrupt controller's device tree node.
  145. * @size: total number of irqs in legacy mapping
  146. * @first_irq: first number of irq block assigned to the domain
  147. * @first_hwirq: first hwirq number to use for the translation. Should normally
  148. * be '0', but a positive integer can be used if the effective
  149. * hwirqs numbering does not begin at zero.
  150. * @ops: map/unmap domain callbacks
  151. * @host_data: Controller private data pointer
  152. *
  153. * Note: the map() callback will be called before this function returns
  154. * for all legacy interrupts except 0 (which is always the invalid irq for
  155. * a legacy controller).
  156. */
  157. struct irq_domain *irq_domain_add_legacy(struct device_node *of_node,
  158. unsigned int size,
  159. unsigned int first_irq,
  160. irq_hw_number_t first_hwirq,
  161. const struct irq_domain_ops *ops,
  162. void *host_data)
  163. {
  164. struct irq_domain *domain;
  165. pr_debug("Setting up legacy domain virq[%i:%i] ==> hwirq[%i:%i]\n",
  166. first_irq, first_irq + size - 1,
  167. (int)first_hwirq, (int)first_hwirq + size -1);
  168. domain = irq_domain_add_linear(of_node, first_hwirq + size, ops, host_data);
  169. if (!domain)
  170. return NULL;
  171. WARN_ON(irq_domain_associate_many(domain, first_irq, first_hwirq, size));
  172. return domain;
  173. }
  174. EXPORT_SYMBOL_GPL(irq_domain_add_legacy);
  175. /**
  176. * irq_domain_add_linear() - Allocate and register a linear revmap irq_domain.
  177. * @of_node: pointer to interrupt controller's device tree node.
  178. * @size: Number of interrupts in the domain.
  179. * @ops: map/unmap domain callbacks
  180. * @host_data: Controller private data pointer
  181. */
  182. struct irq_domain *irq_domain_add_linear(struct device_node *of_node,
  183. unsigned int size,
  184. const struct irq_domain_ops *ops,
  185. void *host_data)
  186. {
  187. struct irq_domain *domain;
  188. domain = irq_domain_alloc(of_node, size, ops, host_data);
  189. if (!domain)
  190. return NULL;
  191. irq_domain_add(domain);
  192. return domain;
  193. }
  194. EXPORT_SYMBOL_GPL(irq_domain_add_linear);
  195. struct irq_domain *irq_domain_add_nomap(struct device_node *of_node,
  196. unsigned int max_irq,
  197. const struct irq_domain_ops *ops,
  198. void *host_data)
  199. {
  200. struct irq_domain *domain = irq_domain_alloc(of_node, 0, ops, host_data);
  201. if (domain) {
  202. domain->revmap_direct_max_irq = max_irq ? max_irq : ~0;
  203. irq_domain_add(domain);
  204. }
  205. return domain;
  206. }
  207. EXPORT_SYMBOL_GPL(irq_domain_add_nomap);
  208. /**
  209. * irq_find_host() - Locates a domain for a given device node
  210. * @node: device-tree node of the interrupt controller
  211. */
  212. struct irq_domain *irq_find_host(struct device_node *node)
  213. {
  214. struct irq_domain *h, *found = NULL;
  215. int rc;
  216. /* We might want to match the legacy controller last since
  217. * it might potentially be set to match all interrupts in
  218. * the absence of a device node. This isn't a problem so far
  219. * yet though...
  220. */
  221. mutex_lock(&irq_domain_mutex);
  222. list_for_each_entry(h, &irq_domain_list, link) {
  223. if (h->ops->match)
  224. rc = h->ops->match(h, node);
  225. else
  226. rc = (h->of_node != NULL) && (h->of_node == node);
  227. if (rc) {
  228. found = h;
  229. break;
  230. }
  231. }
  232. mutex_unlock(&irq_domain_mutex);
  233. return found;
  234. }
  235. EXPORT_SYMBOL_GPL(irq_find_host);
  236. /**
  237. * irq_set_default_host() - Set a "default" irq domain
  238. * @domain: default domain pointer
  239. *
  240. * For convenience, it's possible to set a "default" domain that will be used
  241. * whenever NULL is passed to irq_create_mapping(). It makes life easier for
  242. * platforms that want to manipulate a few hard coded interrupt numbers that
  243. * aren't properly represented in the device-tree.
  244. */
  245. void irq_set_default_host(struct irq_domain *domain)
  246. {
  247. pr_debug("Default domain set to @0x%p\n", domain);
  248. irq_default_domain = domain;
  249. }
  250. EXPORT_SYMBOL_GPL(irq_set_default_host);
  251. static void irq_domain_disassociate_many(struct irq_domain *domain,
  252. unsigned int irq_base, int count)
  253. {
  254. /*
  255. * disassociate in reverse order;
  256. * not strictly necessary, but nice for unwinding
  257. */
  258. while (count--) {
  259. int irq = irq_base + count;
  260. struct irq_data *irq_data = irq_get_irq_data(irq);
  261. irq_hw_number_t hwirq;
  262. if (WARN_ON(!irq_data || irq_data->domain != domain))
  263. continue;
  264. hwirq = irq_data->hwirq;
  265. irq_set_status_flags(irq, IRQ_NOREQUEST);
  266. /* remove chip and handler */
  267. irq_set_chip_and_handler(irq, NULL, NULL);
  268. /* Make sure it's completed */
  269. synchronize_irq(irq);
  270. /* Tell the PIC about it */
  271. if (domain->ops->unmap)
  272. domain->ops->unmap(domain, irq);
  273. smp_mb();
  274. irq_data->domain = NULL;
  275. irq_data->hwirq = 0;
  276. /* Clear reverse map for this hwirq */
  277. if (hwirq < domain->revmap_size) {
  278. domain->linear_revmap[hwirq] = 0;
  279. } else {
  280. mutex_lock(&revmap_trees_mutex);
  281. radix_tree_delete(&domain->revmap_tree, hwirq);
  282. mutex_unlock(&revmap_trees_mutex);
  283. }
  284. }
  285. }
  286. int irq_domain_associate_many(struct irq_domain *domain, unsigned int irq_base,
  287. irq_hw_number_t hwirq_base, int count)
  288. {
  289. unsigned int virq = irq_base;
  290. irq_hw_number_t hwirq = hwirq_base;
  291. int i, ret;
  292. pr_debug("%s(%s, irqbase=%i, hwbase=%i, count=%i)\n", __func__,
  293. of_node_full_name(domain->of_node), irq_base, (int)hwirq_base, count);
  294. for (i = 0; i < count; i++) {
  295. struct irq_data *irq_data = irq_get_irq_data(virq + i);
  296. if (WARN(!irq_data, "error: irq_desc not allocated; "
  297. "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i))
  298. return -EINVAL;
  299. if (WARN(irq_data->domain, "error: irq_desc already associated; "
  300. "irq=%i hwirq=0x%x\n", virq + i, (int)hwirq + i))
  301. return -EINVAL;
  302. };
  303. for (i = 0; i < count; i++, virq++, hwirq++) {
  304. struct irq_data *irq_data = irq_get_irq_data(virq);
  305. irq_data->hwirq = hwirq;
  306. irq_data->domain = domain;
  307. if (domain->ops->map) {
  308. ret = domain->ops->map(domain, virq, hwirq);
  309. if (ret != 0) {
  310. /*
  311. * If map() returns -EPERM, this interrupt is protected
  312. * by the firmware or some other service and shall not
  313. * be mapped. Don't bother telling the user about it.
  314. */
  315. if (ret != -EPERM) {
  316. pr_info("%s didn't like hwirq-0x%lx to VIRQ%i mapping (rc=%d)\n",
  317. domain->name, hwirq, virq, ret);
  318. }
  319. irq_data->domain = NULL;
  320. irq_data->hwirq = 0;
  321. continue;
  322. }
  323. /* If not already assigned, give the domain the chip's name */
  324. if (!domain->name && irq_data->chip)
  325. domain->name = irq_data->chip->name;
  326. }
  327. if (hwirq < domain->revmap_size) {
  328. domain->linear_revmap[hwirq] = virq;
  329. } else {
  330. mutex_lock(&revmap_trees_mutex);
  331. radix_tree_insert(&domain->revmap_tree, hwirq, irq_data);
  332. mutex_unlock(&revmap_trees_mutex);
  333. }
  334. irq_clear_status_flags(virq, IRQ_NOREQUEST);
  335. }
  336. return 0;
  337. }
  338. EXPORT_SYMBOL_GPL(irq_domain_associate_many);
  339. /**
  340. * irq_create_direct_mapping() - Allocate an irq for direct mapping
  341. * @domain: domain to allocate the irq for or NULL for default domain
  342. *
  343. * This routine is used for irq controllers which can choose the hardware
  344. * interrupt numbers they generate. In such a case it's simplest to use
  345. * the linux irq as the hardware interrupt number. It still uses the linear
  346. * or radix tree to store the mapping, but the irq controller can optimize
  347. * the revmap path by using the hwirq directly.
  348. */
  349. unsigned int irq_create_direct_mapping(struct irq_domain *domain)
  350. {
  351. unsigned int virq;
  352. if (domain == NULL)
  353. domain = irq_default_domain;
  354. virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
  355. if (!virq) {
  356. pr_debug("create_direct virq allocation failed\n");
  357. return 0;
  358. }
  359. if (virq >= domain->revmap_direct_max_irq) {
  360. pr_err("ERROR: no free irqs available below %i maximum\n",
  361. domain->revmap_direct_max_irq);
  362. irq_free_desc(virq);
  363. return 0;
  364. }
  365. pr_debug("create_direct obtained virq %d\n", virq);
  366. if (irq_domain_associate(domain, virq, virq)) {
  367. irq_free_desc(virq);
  368. return 0;
  369. }
  370. return virq;
  371. }
  372. EXPORT_SYMBOL_GPL(irq_create_direct_mapping);
  373. /**
  374. * irq_create_mapping() - Map a hardware interrupt into linux irq space
  375. * @domain: domain owning this hardware interrupt or NULL for default domain
  376. * @hwirq: hardware irq number in that domain space
  377. *
  378. * Only one mapping per hardware interrupt is permitted. Returns a linux
  379. * irq number.
  380. * If the sense/trigger is to be specified, set_irq_type() should be called
  381. * on the number returned from that call.
  382. */
  383. unsigned int irq_create_mapping(struct irq_domain *domain,
  384. irq_hw_number_t hwirq)
  385. {
  386. unsigned int hint;
  387. int virq;
  388. pr_debug("irq_create_mapping(0x%p, 0x%lx)\n", domain, hwirq);
  389. /* Look for default domain if nececssary */
  390. if (domain == NULL)
  391. domain = irq_default_domain;
  392. if (domain == NULL) {
  393. pr_warning("irq_create_mapping called for"
  394. " NULL domain, hwirq=%lx\n", hwirq);
  395. WARN_ON(1);
  396. return 0;
  397. }
  398. pr_debug("-> using domain @%p\n", domain);
  399. /* Check if mapping already exists */
  400. virq = irq_find_mapping(domain, hwirq);
  401. if (virq) {
  402. pr_debug("-> existing mapping on virq %d\n", virq);
  403. return virq;
  404. }
  405. /* Allocate a virtual interrupt number */
  406. hint = hwirq % nr_irqs;
  407. if (hint == 0)
  408. hint++;
  409. virq = irq_alloc_desc_from(hint, of_node_to_nid(domain->of_node));
  410. if (virq <= 0)
  411. virq = irq_alloc_desc_from(1, of_node_to_nid(domain->of_node));
  412. if (virq <= 0) {
  413. pr_debug("-> virq allocation failed\n");
  414. return 0;
  415. }
  416. if (irq_domain_associate(domain, virq, hwirq)) {
  417. irq_free_desc(virq);
  418. return 0;
  419. }
  420. pr_debug("irq %lu on domain %s mapped to virtual irq %u\n",
  421. hwirq, of_node_full_name(domain->of_node), virq);
  422. return virq;
  423. }
  424. EXPORT_SYMBOL_GPL(irq_create_mapping);
  425. /**
  426. * irq_create_strict_mappings() - Map a range of hw irqs to fixed linux irqs
  427. * @domain: domain owning the interrupt range
  428. * @irq_base: beginning of linux IRQ range
  429. * @hwirq_base: beginning of hardware IRQ range
  430. * @count: Number of interrupts to map
  431. *
  432. * This routine is used for allocating and mapping a range of hardware
  433. * irqs to linux irqs where the linux irq numbers are at pre-defined
  434. * locations. For use by controllers that already have static mappings
  435. * to insert in to the domain.
  436. *
  437. * Non-linear users can use irq_create_identity_mapping() for IRQ-at-a-time
  438. * domain insertion.
  439. *
  440. * 0 is returned upon success, while any failure to establish a static
  441. * mapping is treated as an error.
  442. */
  443. int irq_create_strict_mappings(struct irq_domain *domain, unsigned int irq_base,
  444. irq_hw_number_t hwirq_base, int count)
  445. {
  446. int ret;
  447. ret = irq_alloc_descs(irq_base, irq_base, count,
  448. of_node_to_nid(domain->of_node));
  449. if (unlikely(ret < 0))
  450. return ret;
  451. ret = irq_domain_associate_many(domain, irq_base, hwirq_base, count);
  452. if (unlikely(ret < 0)) {
  453. irq_free_descs(irq_base, count);
  454. return ret;
  455. }
  456. return 0;
  457. }
  458. EXPORT_SYMBOL_GPL(irq_create_strict_mappings);
  459. unsigned int irq_create_of_mapping(struct device_node *controller,
  460. const u32 *intspec, unsigned int intsize)
  461. {
  462. struct irq_domain *domain;
  463. irq_hw_number_t hwirq;
  464. unsigned int type = IRQ_TYPE_NONE;
  465. unsigned int virq;
  466. domain = controller ? irq_find_host(controller) : irq_default_domain;
  467. if (!domain) {
  468. #ifdef CONFIG_MIPS
  469. /*
  470. * Workaround to avoid breaking interrupt controller drivers
  471. * that don't yet register an irq_domain. This is temporary
  472. * code. ~~~gcl, Feb 24, 2012
  473. *
  474. * Scheduled for removal in Linux v3.6. That should be enough
  475. * time.
  476. */
  477. if (intsize > 0)
  478. return intspec[0];
  479. #endif
  480. pr_warning("no irq domain found for %s !\n",
  481. of_node_full_name(controller));
  482. return 0;
  483. }
  484. /* If domain has no translation, then we assume interrupt line */
  485. if (domain->ops->xlate == NULL)
  486. hwirq = intspec[0];
  487. else {
  488. if (domain->ops->xlate(domain, controller, intspec, intsize,
  489. &hwirq, &type))
  490. return 0;
  491. }
  492. /* Create mapping */
  493. virq = irq_create_mapping(domain, hwirq);
  494. if (!virq)
  495. return virq;
  496. /* Set type if specified and different than the current one */
  497. if (type != IRQ_TYPE_NONE &&
  498. type != (irqd_get_trigger_type(irq_get_irq_data(virq))))
  499. irq_set_irq_type(virq, type);
  500. return virq;
  501. }
  502. EXPORT_SYMBOL_GPL(irq_create_of_mapping);
  503. /**
  504. * irq_dispose_mapping() - Unmap an interrupt
  505. * @virq: linux irq number of the interrupt to unmap
  506. */
  507. void irq_dispose_mapping(unsigned int virq)
  508. {
  509. struct irq_data *irq_data = irq_get_irq_data(virq);
  510. struct irq_domain *domain;
  511. if (!virq || !irq_data)
  512. return;
  513. domain = irq_data->domain;
  514. if (WARN_ON(domain == NULL))
  515. return;
  516. irq_domain_disassociate_many(domain, virq, 1);
  517. irq_free_desc(virq);
  518. }
  519. EXPORT_SYMBOL_GPL(irq_dispose_mapping);
  520. /**
  521. * irq_find_mapping() - Find a linux irq from an hw irq number.
  522. * @domain: domain owning this hardware interrupt
  523. * @hwirq: hardware irq number in that domain space
  524. */
  525. unsigned int irq_find_mapping(struct irq_domain *domain,
  526. irq_hw_number_t hwirq)
  527. {
  528. struct irq_data *data;
  529. /* Look for default domain if nececssary */
  530. if (domain == NULL)
  531. domain = irq_default_domain;
  532. if (domain == NULL)
  533. return 0;
  534. if (hwirq < domain->revmap_direct_max_irq) {
  535. data = irq_get_irq_data(hwirq);
  536. if (data && (data->domain == domain) && (data->hwirq == hwirq))
  537. return hwirq;
  538. }
  539. return irq_linear_revmap(domain, hwirq);
  540. }
  541. EXPORT_SYMBOL_GPL(irq_find_mapping);
  542. /**
  543. * irq_linear_revmap() - Find a linux irq from a hw irq number.
  544. * @domain: domain owning this hardware interrupt
  545. * @hwirq: hardware irq number in that domain space
  546. *
  547. * This is a fast path that can be called directly by irq controller code to
  548. * save a handful of instructions.
  549. */
  550. unsigned int irq_linear_revmap(struct irq_domain *domain,
  551. irq_hw_number_t hwirq)
  552. {
  553. struct irq_data *data;
  554. /* Check revmap bounds; complain if exceeded */
  555. if (hwirq >= domain->revmap_size) {
  556. rcu_read_lock();
  557. data = radix_tree_lookup(&domain->revmap_tree, hwirq);
  558. rcu_read_unlock();
  559. return data ? data->irq : 0;
  560. }
  561. return domain->linear_revmap[hwirq];
  562. }
  563. EXPORT_SYMBOL_GPL(irq_linear_revmap);
  564. #ifdef CONFIG_IRQ_DOMAIN_DEBUG
  565. static int virq_debug_show(struct seq_file *m, void *private)
  566. {
  567. unsigned long flags;
  568. struct irq_desc *desc;
  569. void *data;
  570. int i;
  571. seq_printf(m, "%-5s %-7s %-15s %-*s %s\n", "irq", "hwirq",
  572. "chip name", (int)(2 * sizeof(void *) + 2), "chip data",
  573. "domain name");
  574. for (i = 1; i < nr_irqs; i++) {
  575. desc = irq_to_desc(i);
  576. if (!desc)
  577. continue;
  578. raw_spin_lock_irqsave(&desc->lock, flags);
  579. if (desc->action && desc->action->handler) {
  580. struct irq_chip *chip;
  581. seq_printf(m, "%5d ", i);
  582. seq_printf(m, "0x%05lx ", desc->irq_data.hwirq);
  583. chip = irq_desc_get_chip(desc);
  584. seq_printf(m, "%-15s ", (chip && chip->name) ? chip->name : "none");
  585. data = irq_desc_get_chip_data(desc);
  586. seq_printf(m, data ? "0x%p " : " %p ", data);
  587. seq_printf(m, "%s\n", desc->irq_data.domain->name);
  588. }
  589. raw_spin_unlock_irqrestore(&desc->lock, flags);
  590. }
  591. return 0;
  592. }
  593. static int virq_debug_open(struct inode *inode, struct file *file)
  594. {
  595. return single_open(file, virq_debug_show, inode->i_private);
  596. }
  597. static const struct file_operations virq_debug_fops = {
  598. .open = virq_debug_open,
  599. .read = seq_read,
  600. .llseek = seq_lseek,
  601. .release = single_release,
  602. };
  603. static int __init irq_debugfs_init(void)
  604. {
  605. if (debugfs_create_file("irq_domain_mapping", S_IRUGO, NULL,
  606. NULL, &virq_debug_fops) == NULL)
  607. return -ENOMEM;
  608. return 0;
  609. }
  610. __initcall(irq_debugfs_init);
  611. #endif /* CONFIG_IRQ_DOMAIN_DEBUG */
  612. /**
  613. * irq_domain_xlate_onecell() - Generic xlate for direct one cell bindings
  614. *
  615. * Device Tree IRQ specifier translation function which works with one cell
  616. * bindings where the cell value maps directly to the hwirq number.
  617. */
  618. int irq_domain_xlate_onecell(struct irq_domain *d, struct device_node *ctrlr,
  619. const u32 *intspec, unsigned int intsize,
  620. unsigned long *out_hwirq, unsigned int *out_type)
  621. {
  622. if (WARN_ON(intsize < 1))
  623. return -EINVAL;
  624. *out_hwirq = intspec[0];
  625. *out_type = IRQ_TYPE_NONE;
  626. return 0;
  627. }
  628. EXPORT_SYMBOL_GPL(irq_domain_xlate_onecell);
  629. /**
  630. * irq_domain_xlate_twocell() - Generic xlate for direct two cell bindings
  631. *
  632. * Device Tree IRQ specifier translation function which works with two cell
  633. * bindings where the cell values map directly to the hwirq number
  634. * and linux irq flags.
  635. */
  636. int irq_domain_xlate_twocell(struct irq_domain *d, struct device_node *ctrlr,
  637. const u32 *intspec, unsigned int intsize,
  638. irq_hw_number_t *out_hwirq, unsigned int *out_type)
  639. {
  640. if (WARN_ON(intsize < 2))
  641. return -EINVAL;
  642. *out_hwirq = intspec[0];
  643. *out_type = intspec[1] & IRQ_TYPE_SENSE_MASK;
  644. return 0;
  645. }
  646. EXPORT_SYMBOL_GPL(irq_domain_xlate_twocell);
  647. /**
  648. * irq_domain_xlate_onetwocell() - Generic xlate for one or two cell bindings
  649. *
  650. * Device Tree IRQ specifier translation function which works with either one
  651. * or two cell bindings where the cell values map directly to the hwirq number
  652. * and linux irq flags.
  653. *
  654. * Note: don't use this function unless your interrupt controller explicitly
  655. * supports both one and two cell bindings. For the majority of controllers
  656. * the _onecell() or _twocell() variants above should be used.
  657. */
  658. int irq_domain_xlate_onetwocell(struct irq_domain *d,
  659. struct device_node *ctrlr,
  660. const u32 *intspec, unsigned int intsize,
  661. unsigned long *out_hwirq, unsigned int *out_type)
  662. {
  663. if (WARN_ON(intsize < 1))
  664. return -EINVAL;
  665. *out_hwirq = intspec[0];
  666. *out_type = (intsize > 1) ? intspec[1] : IRQ_TYPE_NONE;
  667. return 0;
  668. }
  669. EXPORT_SYMBOL_GPL(irq_domain_xlate_onetwocell);
  670. const struct irq_domain_ops irq_domain_simple_ops = {
  671. .xlate = irq_domain_xlate_onetwocell,
  672. };
  673. EXPORT_SYMBOL_GPL(irq_domain_simple_ops);
  674. #ifdef CONFIG_OF_IRQ
  675. void irq_domain_generate_simple(const struct of_device_id *match,
  676. u64 phys_base, unsigned int irq_start)
  677. {
  678. struct device_node *node;
  679. pr_debug("looking for phys_base=%llx, irq_start=%i\n",
  680. (unsigned long long) phys_base, (int) irq_start);
  681. node = of_find_matching_node_by_address(NULL, match, phys_base);
  682. if (node)
  683. irq_domain_add_legacy(node, 32, irq_start, 0,
  684. &irq_domain_simple_ops, NULL);
  685. }
  686. EXPORT_SYMBOL_GPL(irq_domain_generate_simple);
  687. #endif