amdgpu_ctx.c 7.8 KB

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