amdgpu_ctx.c 4.5 KB

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