amdgpu_ctx.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright 2015 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: monk liu <monk.liu@amd.com>
  23. */
  24. #include <drm/drmP.h>
  25. #include "amdgpu.h"
  26. static void amdgpu_ctx_do_release(struct kref *ref)
  27. {
  28. struct amdgpu_ctx *ctx;
  29. unsigned i, j;
  30. ctx = container_of(ref, struct amdgpu_ctx, refcount);
  31. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  32. for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
  33. fence_put(ctx->rings[i].fences[j]);
  34. kfree(ctx);
  35. }
  36. int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
  37. uint32_t *id)
  38. {
  39. struct amdgpu_ctx *ctx;
  40. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  41. int i, r;
  42. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  43. if (!ctx)
  44. return -ENOMEM;
  45. mutex_lock(&mgr->lock);
  46. r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
  47. if (r < 0) {
  48. mutex_unlock(&mgr->lock);
  49. kfree(ctx);
  50. return r;
  51. }
  52. *id = (uint32_t)r;
  53. memset(ctx, 0, sizeof(*ctx));
  54. kref_init(&ctx->refcount);
  55. spin_lock_init(&ctx->ring_lock);
  56. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  57. ctx->rings[i].sequence = 1;
  58. mutex_unlock(&mgr->lock);
  59. return 0;
  60. }
  61. int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
  62. {
  63. struct amdgpu_ctx *ctx;
  64. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  65. mutex_lock(&mgr->lock);
  66. ctx = idr_find(&mgr->ctx_handles, id);
  67. if (ctx) {
  68. idr_remove(&mgr->ctx_handles, id);
  69. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  70. mutex_unlock(&mgr->lock);
  71. return 0;
  72. }
  73. mutex_unlock(&mgr->lock);
  74. return -EINVAL;
  75. }
  76. static int amdgpu_ctx_query(struct amdgpu_device *adev,
  77. struct amdgpu_fpriv *fpriv, uint32_t id,
  78. union drm_amdgpu_ctx_out *out)
  79. {
  80. struct amdgpu_ctx *ctx;
  81. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  82. unsigned reset_counter;
  83. mutex_lock(&mgr->lock);
  84. ctx = idr_find(&mgr->ctx_handles, id);
  85. if (!ctx) {
  86. mutex_unlock(&mgr->lock);
  87. return -EINVAL;
  88. }
  89. /* TODO: these two are always zero */
  90. out->state.flags = 0x0;
  91. out->state.hangs = 0x0;
  92. /* determine if a GPU reset has occured since the last call */
  93. reset_counter = atomic_read(&adev->gpu_reset_counter);
  94. /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
  95. if (ctx->reset_counter == reset_counter)
  96. out->state.reset_status = AMDGPU_CTX_NO_RESET;
  97. else
  98. out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  99. ctx->reset_counter = reset_counter;
  100. mutex_unlock(&mgr->lock);
  101. return 0;
  102. }
  103. void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
  104. {
  105. struct idr *idp;
  106. struct amdgpu_ctx *ctx;
  107. uint32_t id;
  108. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  109. idp = &mgr->ctx_handles;
  110. idr_for_each_entry(idp,ctx,id) {
  111. if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
  112. DRM_ERROR("ctx %p is still alive\n", ctx);
  113. }
  114. idr_destroy(&mgr->ctx_handles);
  115. mutex_destroy(&mgr->lock);
  116. }
  117. int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  118. struct drm_file *filp)
  119. {
  120. int r;
  121. uint32_t id;
  122. union drm_amdgpu_ctx *args = data;
  123. struct amdgpu_device *adev = dev->dev_private;
  124. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  125. r = 0;
  126. id = args->in.ctx_id;
  127. switch (args->in.op) {
  128. case AMDGPU_CTX_OP_ALLOC_CTX:
  129. r = amdgpu_ctx_alloc(adev, fpriv, &id);
  130. args->out.alloc.ctx_id = id;
  131. break;
  132. case AMDGPU_CTX_OP_FREE_CTX:
  133. r = amdgpu_ctx_free(adev, fpriv, id);
  134. break;
  135. case AMDGPU_CTX_OP_QUERY_STATE:
  136. r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
  137. break;
  138. default:
  139. return -EINVAL;
  140. }
  141. return r;
  142. }
  143. struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
  144. {
  145. struct amdgpu_ctx *ctx;
  146. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  147. mutex_lock(&mgr->lock);
  148. ctx = idr_find(&mgr->ctx_handles, id);
  149. if (ctx)
  150. kref_get(&ctx->refcount);
  151. mutex_unlock(&mgr->lock);
  152. return ctx;
  153. }
  154. int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
  155. {
  156. if (ctx == NULL)
  157. return -EINVAL;
  158. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  159. return 0;
  160. }
  161. uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
  162. struct fence *fence)
  163. {
  164. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  165. uint64_t seq = cring->sequence;
  166. unsigned idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
  167. struct fence *other = cring->fences[idx];
  168. if (other) {
  169. signed long r;
  170. r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
  171. if (r < 0)
  172. DRM_ERROR("Error (%ld) waiting for fence!\n", r);
  173. }
  174. fence_get(fence);
  175. spin_lock(&ctx->ring_lock);
  176. cring->fences[idx] = fence;
  177. cring->sequence++;
  178. spin_unlock(&ctx->ring_lock);
  179. fence_put(other);
  180. return seq;
  181. }
  182. struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  183. struct amdgpu_ring *ring, uint64_t seq)
  184. {
  185. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  186. struct fence *fence;
  187. spin_lock(&ctx->ring_lock);
  188. if (seq >= cring->sequence) {
  189. spin_unlock(&ctx->ring_lock);
  190. return ERR_PTR(-EINVAL);
  191. }
  192. if (seq < cring->sequence - AMDGPU_CTX_MAX_CS_PENDING) {
  193. spin_unlock(&ctx->ring_lock);
  194. return NULL;
  195. }
  196. fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
  197. spin_unlock(&ctx->ring_lock);
  198. return fence;
  199. }