afu_irq.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2017 IBM Corp.
  3. #include <linux/interrupt.h>
  4. #include <asm/pnv-ocxl.h>
  5. #include <asm/xive.h>
  6. #include "ocxl_internal.h"
  7. #include "trace.h"
  8. struct afu_irq {
  9. int id;
  10. int hw_irq;
  11. unsigned int virq;
  12. char *name;
  13. irqreturn_t (*handler)(void *private);
  14. void (*free_private)(void *private);
  15. void *private;
  16. };
  17. int ocxl_irq_offset_to_id(struct ocxl_context *ctx, u64 offset)
  18. {
  19. return (offset - ctx->afu->irq_base_offset) >> PAGE_SHIFT;
  20. }
  21. u64 ocxl_irq_id_to_offset(struct ocxl_context *ctx, int irq_id)
  22. {
  23. return ctx->afu->irq_base_offset + (irq_id << PAGE_SHIFT);
  24. }
  25. int ocxl_irq_set_handler(struct ocxl_context *ctx, int irq_id,
  26. irqreturn_t (*handler)(void *private),
  27. void (*free_private)(void *private),
  28. void *private)
  29. {
  30. struct afu_irq *irq;
  31. int rc;
  32. mutex_lock(&ctx->irq_lock);
  33. irq = idr_find(&ctx->irq_idr, irq_id);
  34. if (!irq) {
  35. rc = -EINVAL;
  36. goto unlock;
  37. }
  38. irq->handler = handler;
  39. irq->private = private;
  40. irq->free_private = free_private;
  41. rc = 0;
  42. // Fall through to unlock
  43. unlock:
  44. mutex_unlock(&ctx->irq_lock);
  45. return rc;
  46. }
  47. EXPORT_SYMBOL_GPL(ocxl_irq_set_handler);
  48. static irqreturn_t afu_irq_handler(int virq, void *data)
  49. {
  50. struct afu_irq *irq = (struct afu_irq *) data;
  51. trace_ocxl_afu_irq_receive(virq);
  52. if (irq->handler)
  53. return irq->handler(irq->private);
  54. return IRQ_HANDLED; // Just drop it on the ground
  55. }
  56. static int setup_afu_irq(struct ocxl_context *ctx, struct afu_irq *irq)
  57. {
  58. int rc;
  59. irq->virq = irq_create_mapping(NULL, irq->hw_irq);
  60. if (!irq->virq) {
  61. pr_err("irq_create_mapping failed\n");
  62. return -ENOMEM;
  63. }
  64. pr_debug("hw_irq %d mapped to virq %u\n", irq->hw_irq, irq->virq);
  65. irq->name = kasprintf(GFP_KERNEL, "ocxl-afu-%u", irq->virq);
  66. if (!irq->name) {
  67. irq_dispose_mapping(irq->virq);
  68. return -ENOMEM;
  69. }
  70. rc = request_irq(irq->virq, afu_irq_handler, 0, irq->name, irq);
  71. if (rc) {
  72. kfree(irq->name);
  73. irq->name = NULL;
  74. irq_dispose_mapping(irq->virq);
  75. pr_err("request_irq failed: %d\n", rc);
  76. return rc;
  77. }
  78. return 0;
  79. }
  80. static void release_afu_irq(struct afu_irq *irq)
  81. {
  82. free_irq(irq->virq, irq);
  83. irq_dispose_mapping(irq->virq);
  84. kfree(irq->name);
  85. }
  86. int ocxl_afu_irq_alloc(struct ocxl_context *ctx, int *irq_id)
  87. {
  88. struct afu_irq *irq;
  89. int rc;
  90. irq = kzalloc(sizeof(struct afu_irq), GFP_KERNEL);
  91. if (!irq)
  92. return -ENOMEM;
  93. /*
  94. * We limit the number of afu irqs per context and per link to
  95. * avoid a single process or user depleting the pool of IPIs
  96. */
  97. mutex_lock(&ctx->irq_lock);
  98. irq->id = idr_alloc(&ctx->irq_idr, irq, 0, MAX_IRQ_PER_CONTEXT,
  99. GFP_KERNEL);
  100. if (irq->id < 0) {
  101. rc = -ENOSPC;
  102. goto err_unlock;
  103. }
  104. rc = ocxl_link_irq_alloc(ctx->afu->fn->link, &irq->hw_irq);
  105. if (rc)
  106. goto err_idr;
  107. rc = setup_afu_irq(ctx, irq);
  108. if (rc)
  109. goto err_alloc;
  110. trace_ocxl_afu_irq_alloc(ctx->pasid, irq->id, irq->virq, irq->hw_irq);
  111. mutex_unlock(&ctx->irq_lock);
  112. *irq_id = irq->id;
  113. return 0;
  114. err_alloc:
  115. ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
  116. err_idr:
  117. idr_remove(&ctx->irq_idr, irq->id);
  118. err_unlock:
  119. mutex_unlock(&ctx->irq_lock);
  120. kfree(irq);
  121. return rc;
  122. }
  123. EXPORT_SYMBOL_GPL(ocxl_afu_irq_alloc);
  124. static void afu_irq_free(struct afu_irq *irq, struct ocxl_context *ctx)
  125. {
  126. trace_ocxl_afu_irq_free(ctx->pasid, irq->id);
  127. if (ctx->mapping)
  128. unmap_mapping_range(ctx->mapping,
  129. ocxl_irq_id_to_offset(ctx, irq->id),
  130. 1 << PAGE_SHIFT, 1);
  131. release_afu_irq(irq);
  132. if (irq->free_private)
  133. irq->free_private(irq->private);
  134. ocxl_link_free_irq(ctx->afu->fn->link, irq->hw_irq);
  135. kfree(irq);
  136. }
  137. int ocxl_afu_irq_free(struct ocxl_context *ctx, int irq_id)
  138. {
  139. struct afu_irq *irq;
  140. mutex_lock(&ctx->irq_lock);
  141. irq = idr_find(&ctx->irq_idr, irq_id);
  142. if (!irq) {
  143. mutex_unlock(&ctx->irq_lock);
  144. return -EINVAL;
  145. }
  146. idr_remove(&ctx->irq_idr, irq->id);
  147. afu_irq_free(irq, ctx);
  148. mutex_unlock(&ctx->irq_lock);
  149. return 0;
  150. }
  151. EXPORT_SYMBOL_GPL(ocxl_afu_irq_free);
  152. void ocxl_afu_irq_free_all(struct ocxl_context *ctx)
  153. {
  154. struct afu_irq *irq;
  155. int id;
  156. mutex_lock(&ctx->irq_lock);
  157. idr_for_each_entry(&ctx->irq_idr, irq, id)
  158. afu_irq_free(irq, ctx);
  159. mutex_unlock(&ctx->irq_lock);
  160. }
  161. u64 ocxl_afu_irq_get_addr(struct ocxl_context *ctx, int irq_id)
  162. {
  163. struct xive_irq_data *xd;
  164. struct afu_irq *irq;
  165. u64 addr = 0;
  166. mutex_lock(&ctx->irq_lock);
  167. irq = idr_find(&ctx->irq_idr, irq_id);
  168. if (irq) {
  169. xd = irq_get_handler_data(irq->virq);
  170. addr = xd ? xd->trig_page : 0;
  171. }
  172. mutex_unlock(&ctx->irq_lock);
  173. return addr;
  174. }
  175. EXPORT_SYMBOL_GPL(ocxl_afu_irq_get_addr);