amdgpu_ctx.c 7.5 KB

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