irqdomain.c 24 KB

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