irqdomain.c 23 KB

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