irqdomain.c 26 KB

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