amdgpu_ctx.c 7.8 KB

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