amdgpu_ctx.c 7.2 KB

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