irqdomain.c 26 KB

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