amdgpu_ctx.c 7.3 KB

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