amdgpu_ctx.c 7.9 KB

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