amdgpu_ctx.c 7.2 KB

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