amdgpu_ctx.c 7.4 KB

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