amdgpu_ctx.c 7.4 KB

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