irqdomain.c 23 KB

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