amdgpu_ctx.c 7.6 KB

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