amdgpu_ctx.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. struct amdgpu_ctx_mgr *mgr;
  30. ctx = container_of(ref, struct amdgpu_ctx, refcount);
  31. mgr = &ctx->fpriv->ctx_mgr;
  32. idr_remove(&mgr->ctx_handles, ctx->id);
  33. kfree(ctx);
  34. }
  35. int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t *id, uint32_t flags)
  36. {
  37. int r;
  38. struct amdgpu_ctx *ctx;
  39. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  40. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  41. if (!ctx)
  42. return -ENOMEM;
  43. mutex_lock(&mgr->lock);
  44. r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
  45. if (r < 0) {
  46. mutex_unlock(&mgr->lock);
  47. kfree(ctx);
  48. return r;
  49. }
  50. *id = (uint32_t)r;
  51. memset(ctx, 0, sizeof(*ctx));
  52. ctx->id = *id;
  53. ctx->fpriv = fpriv;
  54. kref_init(&ctx->refcount);
  55. mutex_unlock(&mgr->lock);
  56. return 0;
  57. }
  58. int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
  59. {
  60. struct amdgpu_ctx *ctx;
  61. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  62. mutex_lock(&mgr->lock);
  63. ctx = idr_find(&mgr->ctx_handles, id);
  64. if (ctx) {
  65. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  66. mutex_unlock(&mgr->lock);
  67. return 0;
  68. }
  69. mutex_unlock(&mgr->lock);
  70. return -EINVAL;
  71. }
  72. static int amdgpu_ctx_query(struct amdgpu_device *adev,
  73. struct amdgpu_fpriv *fpriv, uint32_t id,
  74. union drm_amdgpu_ctx_out *out)
  75. {
  76. struct amdgpu_ctx *ctx;
  77. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  78. unsigned reset_counter;
  79. mutex_lock(&mgr->lock);
  80. ctx = idr_find(&mgr->ctx_handles, id);
  81. if (!ctx) {
  82. mutex_unlock(&mgr->lock);
  83. return -EINVAL;
  84. }
  85. /* TODO: these two are always zero */
  86. out->state.flags = ctx->state.flags;
  87. out->state.hangs = ctx->state.hangs;
  88. /* determine if a GPU reset has occured since the last call */
  89. reset_counter = atomic_read(&adev->gpu_reset_counter);
  90. /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
  91. if (ctx->reset_counter == reset_counter)
  92. out->state.reset_status = AMDGPU_CTX_NO_RESET;
  93. else
  94. out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  95. ctx->reset_counter = reset_counter;
  96. mutex_unlock(&mgr->lock);
  97. return 0;
  98. }
  99. void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
  100. {
  101. struct idr *idp;
  102. struct amdgpu_ctx *ctx;
  103. uint32_t id;
  104. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  105. idp = &mgr->ctx_handles;
  106. idr_for_each_entry(idp,ctx,id) {
  107. if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
  108. DRM_ERROR("ctx (id=%ul) is still alive\n",ctx->id);
  109. }
  110. mutex_destroy(&mgr->lock);
  111. }
  112. int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  113. struct drm_file *filp)
  114. {
  115. int r;
  116. uint32_t id;
  117. uint32_t flags;
  118. union drm_amdgpu_ctx *args = data;
  119. struct amdgpu_device *adev = dev->dev_private;
  120. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  121. r = 0;
  122. id = args->in.ctx_id;
  123. flags = args->in.flags;
  124. switch (args->in.op) {
  125. case AMDGPU_CTX_OP_ALLOC_CTX:
  126. r = amdgpu_ctx_alloc(adev, fpriv, &id, flags);
  127. args->out.alloc.ctx_id = id;
  128. break;
  129. case AMDGPU_CTX_OP_FREE_CTX:
  130. r = amdgpu_ctx_free(adev, fpriv, id);
  131. break;
  132. case AMDGPU_CTX_OP_QUERY_STATE:
  133. r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
  134. break;
  135. default:
  136. return -EINVAL;
  137. }
  138. return r;
  139. }
  140. struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
  141. {
  142. struct amdgpu_ctx *ctx;
  143. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  144. mutex_lock(&mgr->lock);
  145. ctx = idr_find(&mgr->ctx_handles, id);
  146. if (ctx)
  147. kref_get(&ctx->refcount);
  148. mutex_unlock(&mgr->lock);
  149. return ctx;
  150. }
  151. int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
  152. {
  153. struct amdgpu_fpriv *fpriv;
  154. struct amdgpu_ctx_mgr *mgr;
  155. if (ctx == NULL)
  156. return -EINVAL;
  157. fpriv = ctx->fpriv;
  158. mgr = &fpriv->ctx_mgr;
  159. mutex_lock(&mgr->lock);
  160. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  161. mutex_unlock(&mgr->lock);
  162. return 0;
  163. }