irqdomain.c 23 KB

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