amdgpu_ctx.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_device *adev;
  30. unsigned i, j;
  31. ctx = container_of(ref, struct amdgpu_ctx, refcount);
  32. adev = ctx->adev;
  33. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  34. for (j = 0; j < AMDGPU_CTX_MAX_CS_PENDING; ++j)
  35. fence_put(ctx->rings[i].fences[j]);
  36. if (amdgpu_enable_scheduler) {
  37. for (i = 0; i < adev->num_rings; i++)
  38. amd_context_entity_fini(adev->rings[i]->scheduler,
  39. &ctx->rings[i].c_entity);
  40. }
  41. kfree(ctx);
  42. }
  43. int amdgpu_ctx_alloc(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv,
  44. uint32_t *id)
  45. {
  46. struct amdgpu_ctx *ctx;
  47. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  48. int i, j, r;
  49. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  50. if (!ctx)
  51. return -ENOMEM;
  52. mutex_lock(&mgr->lock);
  53. r = idr_alloc(&mgr->ctx_handles, ctx, 0, 0, GFP_KERNEL);
  54. if (r < 0) {
  55. mutex_unlock(&mgr->lock);
  56. kfree(ctx);
  57. return r;
  58. }
  59. *id = (uint32_t)r;
  60. memset(ctx, 0, sizeof(*ctx));
  61. ctx->adev = adev;
  62. kref_init(&ctx->refcount);
  63. spin_lock_init(&ctx->ring_lock);
  64. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  65. ctx->rings[i].sequence = 1;
  66. mutex_unlock(&mgr->lock);
  67. if (amdgpu_enable_scheduler) {
  68. /* create context entity for each ring */
  69. for (i = 0; i < adev->num_rings; i++) {
  70. struct amd_run_queue *rq;
  71. if (fpriv)
  72. rq = &adev->rings[i]->scheduler->sched_rq;
  73. else
  74. rq = &adev->rings[i]->scheduler->kernel_rq;
  75. r = amd_context_entity_init(adev->rings[i]->scheduler,
  76. &ctx->rings[i].c_entity,
  77. NULL, rq, *id);
  78. if (r)
  79. break;
  80. }
  81. if (i < adev->num_rings) {
  82. for (j = 0; j < i; j++)
  83. amd_context_entity_fini(adev->rings[j]->scheduler,
  84. &ctx->rings[j].c_entity);
  85. kfree(ctx);
  86. return -EINVAL;
  87. }
  88. }
  89. return 0;
  90. }
  91. int amdgpu_ctx_free(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv, uint32_t id)
  92. {
  93. struct amdgpu_ctx *ctx;
  94. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  95. mutex_lock(&mgr->lock);
  96. ctx = idr_find(&mgr->ctx_handles, id);
  97. if (ctx) {
  98. idr_remove(&mgr->ctx_handles, id);
  99. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  100. mutex_unlock(&mgr->lock);
  101. return 0;
  102. }
  103. mutex_unlock(&mgr->lock);
  104. return -EINVAL;
  105. }
  106. static int amdgpu_ctx_query(struct amdgpu_device *adev,
  107. struct amdgpu_fpriv *fpriv, uint32_t id,
  108. union drm_amdgpu_ctx_out *out)
  109. {
  110. struct amdgpu_ctx *ctx;
  111. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  112. unsigned reset_counter;
  113. mutex_lock(&mgr->lock);
  114. ctx = idr_find(&mgr->ctx_handles, id);
  115. if (!ctx) {
  116. mutex_unlock(&mgr->lock);
  117. return -EINVAL;
  118. }
  119. /* TODO: these two are always zero */
  120. out->state.flags = 0x0;
  121. out->state.hangs = 0x0;
  122. /* determine if a GPU reset has occured since the last call */
  123. reset_counter = atomic_read(&adev->gpu_reset_counter);
  124. /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
  125. if (ctx->reset_counter == reset_counter)
  126. out->state.reset_status = AMDGPU_CTX_NO_RESET;
  127. else
  128. out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  129. ctx->reset_counter = reset_counter;
  130. mutex_unlock(&mgr->lock);
  131. return 0;
  132. }
  133. void amdgpu_ctx_fini(struct amdgpu_fpriv *fpriv)
  134. {
  135. struct idr *idp;
  136. struct amdgpu_ctx *ctx;
  137. uint32_t id;
  138. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  139. idp = &mgr->ctx_handles;
  140. idr_for_each_entry(idp,ctx,id) {
  141. if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
  142. DRM_ERROR("ctx %p is still alive\n", ctx);
  143. }
  144. idr_destroy(&mgr->ctx_handles);
  145. mutex_destroy(&mgr->lock);
  146. }
  147. int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  148. struct drm_file *filp)
  149. {
  150. int r;
  151. uint32_t id;
  152. union drm_amdgpu_ctx *args = data;
  153. struct amdgpu_device *adev = dev->dev_private;
  154. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  155. r = 0;
  156. id = args->in.ctx_id;
  157. switch (args->in.op) {
  158. case AMDGPU_CTX_OP_ALLOC_CTX:
  159. r = amdgpu_ctx_alloc(adev, fpriv, &id);
  160. args->out.alloc.ctx_id = id;
  161. break;
  162. case AMDGPU_CTX_OP_FREE_CTX:
  163. r = amdgpu_ctx_free(adev, fpriv, id);
  164. break;
  165. case AMDGPU_CTX_OP_QUERY_STATE:
  166. r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
  167. break;
  168. default:
  169. return -EINVAL;
  170. }
  171. return r;
  172. }
  173. struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
  174. {
  175. struct amdgpu_ctx *ctx;
  176. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  177. mutex_lock(&mgr->lock);
  178. ctx = idr_find(&mgr->ctx_handles, id);
  179. if (ctx)
  180. kref_get(&ctx->refcount);
  181. mutex_unlock(&mgr->lock);
  182. return ctx;
  183. }
  184. int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
  185. {
  186. if (ctx == NULL)
  187. return -EINVAL;
  188. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  189. return 0;
  190. }
  191. uint64_t amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
  192. struct fence *fence)
  193. {
  194. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  195. uint64_t seq = 0;
  196. unsigned idx = 0;
  197. struct fence *other = NULL;
  198. if (amdgpu_enable_scheduler)
  199. seq = atomic64_read(&cring->c_entity.last_queued_v_seq);
  200. else
  201. seq = cring->sequence;
  202. idx = seq % AMDGPU_CTX_MAX_CS_PENDING;
  203. other = cring->fences[idx];
  204. if (other) {
  205. signed long r;
  206. r = fence_wait_timeout(other, false, MAX_SCHEDULE_TIMEOUT);
  207. if (r < 0)
  208. DRM_ERROR("Error (%ld) waiting for fence!\n", r);
  209. }
  210. fence_get(fence);
  211. spin_lock(&ctx->ring_lock);
  212. cring->fences[idx] = fence;
  213. if (!amdgpu_enable_scheduler)
  214. cring->sequence++;
  215. spin_unlock(&ctx->ring_lock);
  216. fence_put(other);
  217. return seq;
  218. }
  219. struct fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  220. struct amdgpu_ring *ring, uint64_t seq)
  221. {
  222. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  223. struct fence *fence;
  224. uint64_t queued_seq;
  225. spin_lock(&ctx->ring_lock);
  226. if (amdgpu_enable_scheduler)
  227. queued_seq = atomic64_read(&cring->c_entity.last_queued_v_seq) + 1;
  228. else
  229. queued_seq = cring->sequence;
  230. if (seq >= queued_seq) {
  231. spin_unlock(&ctx->ring_lock);
  232. return ERR_PTR(-EINVAL);
  233. }
  234. if (seq + AMDGPU_CTX_MAX_CS_PENDING < queued_seq) {
  235. spin_unlock(&ctx->ring_lock);
  236. return NULL;
  237. }
  238. fence = fence_get(cring->fences[seq % AMDGPU_CTX_MAX_CS_PENDING]);
  239. spin_unlock(&ctx->ring_lock);
  240. return fence;
  241. }