amdgpu_ctx.c 7.1 KB

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