amdgpu_ctx.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 <drm/drm_auth.h>
  26. #include "amdgpu.h"
  27. #include "amdgpu_sched.h"
  28. static int amdgpu_ctx_priority_permit(struct drm_file *filp,
  29. enum amd_sched_priority priority)
  30. {
  31. /* NORMAL and below are accessible by everyone */
  32. if (priority <= AMD_SCHED_PRIORITY_NORMAL)
  33. return 0;
  34. if (capable(CAP_SYS_NICE))
  35. return 0;
  36. if (drm_is_current_master(filp))
  37. return 0;
  38. return -EACCES;
  39. }
  40. static int amdgpu_ctx_init(struct amdgpu_device *adev,
  41. enum amd_sched_priority priority,
  42. struct drm_file *filp,
  43. struct amdgpu_ctx *ctx)
  44. {
  45. unsigned i, j;
  46. int r;
  47. if (priority < 0 || priority >= AMD_SCHED_PRIORITY_MAX)
  48. return -EINVAL;
  49. r = amdgpu_ctx_priority_permit(filp, priority);
  50. if (r)
  51. return r;
  52. memset(ctx, 0, sizeof(*ctx));
  53. ctx->adev = adev;
  54. kref_init(&ctx->refcount);
  55. spin_lock_init(&ctx->ring_lock);
  56. ctx->fences = kcalloc(amdgpu_sched_jobs * AMDGPU_MAX_RINGS,
  57. sizeof(struct dma_fence*), GFP_KERNEL);
  58. if (!ctx->fences)
  59. return -ENOMEM;
  60. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  61. ctx->rings[i].sequence = 1;
  62. ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
  63. }
  64. ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
  65. ctx->init_priority = priority;
  66. ctx->override_priority = AMD_SCHED_PRIORITY_UNSET;
  67. /* create context entity for each ring */
  68. for (i = 0; i < adev->num_rings; i++) {
  69. struct amdgpu_ring *ring = adev->rings[i];
  70. struct amd_sched_rq *rq;
  71. rq = &ring->sched.sched_rq[priority];
  72. if (ring == &adev->gfx.kiq.ring)
  73. continue;
  74. r = amd_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
  75. rq, amdgpu_sched_jobs);
  76. if (r)
  77. goto failed;
  78. }
  79. r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
  80. if (r)
  81. goto failed;
  82. return 0;
  83. failed:
  84. for (j = 0; j < i; j++)
  85. amd_sched_entity_fini(&adev->rings[j]->sched,
  86. &ctx->rings[j].entity);
  87. kfree(ctx->fences);
  88. ctx->fences = NULL;
  89. return r;
  90. }
  91. static void amdgpu_ctx_fini(struct amdgpu_ctx *ctx)
  92. {
  93. struct amdgpu_device *adev = ctx->adev;
  94. unsigned i, j;
  95. if (!adev)
  96. return;
  97. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  98. for (j = 0; j < amdgpu_sched_jobs; ++j)
  99. dma_fence_put(ctx->rings[i].fences[j]);
  100. kfree(ctx->fences);
  101. ctx->fences = NULL;
  102. for (i = 0; i < adev->num_rings; i++)
  103. amd_sched_entity_fini(&adev->rings[i]->sched,
  104. &ctx->rings[i].entity);
  105. amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
  106. }
  107. static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
  108. struct amdgpu_fpriv *fpriv,
  109. struct drm_file *filp,
  110. enum amd_sched_priority priority,
  111. uint32_t *id)
  112. {
  113. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  114. struct amdgpu_ctx *ctx;
  115. int r;
  116. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  117. if (!ctx)
  118. return -ENOMEM;
  119. mutex_lock(&mgr->lock);
  120. r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
  121. if (r < 0) {
  122. mutex_unlock(&mgr->lock);
  123. kfree(ctx);
  124. return r;
  125. }
  126. *id = (uint32_t)r;
  127. r = amdgpu_ctx_init(adev, priority, filp, ctx);
  128. if (r) {
  129. idr_remove(&mgr->ctx_handles, *id);
  130. *id = 0;
  131. kfree(ctx);
  132. }
  133. mutex_unlock(&mgr->lock);
  134. return r;
  135. }
  136. static void amdgpu_ctx_do_release(struct kref *ref)
  137. {
  138. struct amdgpu_ctx *ctx;
  139. ctx = container_of(ref, struct amdgpu_ctx, refcount);
  140. amdgpu_ctx_fini(ctx);
  141. kfree(ctx);
  142. }
  143. static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
  144. {
  145. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  146. struct amdgpu_ctx *ctx;
  147. mutex_lock(&mgr->lock);
  148. ctx = idr_remove(&mgr->ctx_handles, id);
  149. if (ctx)
  150. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  151. mutex_unlock(&mgr->lock);
  152. return ctx ? 0 : -EINVAL;
  153. }
  154. static int amdgpu_ctx_query(struct amdgpu_device *adev,
  155. struct amdgpu_fpriv *fpriv, uint32_t id,
  156. union drm_amdgpu_ctx_out *out)
  157. {
  158. struct amdgpu_ctx *ctx;
  159. struct amdgpu_ctx_mgr *mgr;
  160. unsigned reset_counter;
  161. if (!fpriv)
  162. return -EINVAL;
  163. mgr = &fpriv->ctx_mgr;
  164. mutex_lock(&mgr->lock);
  165. ctx = idr_find(&mgr->ctx_handles, id);
  166. if (!ctx) {
  167. mutex_unlock(&mgr->lock);
  168. return -EINVAL;
  169. }
  170. /* TODO: these two are always zero */
  171. out->state.flags = 0x0;
  172. out->state.hangs = 0x0;
  173. /* determine if a GPU reset has occured since the last call */
  174. reset_counter = atomic_read(&adev->gpu_reset_counter);
  175. /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
  176. if (ctx->reset_counter == reset_counter)
  177. out->state.reset_status = AMDGPU_CTX_NO_RESET;
  178. else
  179. out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  180. ctx->reset_counter = reset_counter;
  181. mutex_unlock(&mgr->lock);
  182. return 0;
  183. }
  184. int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  185. struct drm_file *filp)
  186. {
  187. int r;
  188. uint32_t id;
  189. enum amd_sched_priority priority;
  190. union drm_amdgpu_ctx *args = data;
  191. struct amdgpu_device *adev = dev->dev_private;
  192. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  193. r = 0;
  194. id = args->in.ctx_id;
  195. priority = amdgpu_to_sched_priority(args->in.priority);
  196. /* For backwards compatibility reasons, we need to accept
  197. * ioctls with garbage in the priority field */
  198. if (priority == AMD_SCHED_PRIORITY_INVALID)
  199. priority = AMD_SCHED_PRIORITY_NORMAL;
  200. switch (args->in.op) {
  201. case AMDGPU_CTX_OP_ALLOC_CTX:
  202. r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
  203. args->out.alloc.ctx_id = id;
  204. break;
  205. case AMDGPU_CTX_OP_FREE_CTX:
  206. r = amdgpu_ctx_free(fpriv, id);
  207. break;
  208. case AMDGPU_CTX_OP_QUERY_STATE:
  209. r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
  210. break;
  211. default:
  212. return -EINVAL;
  213. }
  214. return r;
  215. }
  216. struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
  217. {
  218. struct amdgpu_ctx *ctx;
  219. struct amdgpu_ctx_mgr *mgr;
  220. if (!fpriv)
  221. return NULL;
  222. mgr = &fpriv->ctx_mgr;
  223. mutex_lock(&mgr->lock);
  224. ctx = idr_find(&mgr->ctx_handles, id);
  225. if (ctx)
  226. kref_get(&ctx->refcount);
  227. mutex_unlock(&mgr->lock);
  228. return ctx;
  229. }
  230. int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
  231. {
  232. if (ctx == NULL)
  233. return -EINVAL;
  234. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  235. return 0;
  236. }
  237. int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
  238. struct dma_fence *fence, uint64_t* handler)
  239. {
  240. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  241. uint64_t seq = cring->sequence;
  242. unsigned idx = 0;
  243. struct dma_fence *other = NULL;
  244. idx = seq & (amdgpu_sched_jobs - 1);
  245. other = cring->fences[idx];
  246. if (other) {
  247. signed long r;
  248. r = dma_fence_wait_timeout(other, true, MAX_SCHEDULE_TIMEOUT);
  249. if (r < 0)
  250. return r;
  251. }
  252. dma_fence_get(fence);
  253. spin_lock(&ctx->ring_lock);
  254. cring->fences[idx] = fence;
  255. cring->sequence++;
  256. spin_unlock(&ctx->ring_lock);
  257. dma_fence_put(other);
  258. if (handler)
  259. *handler = seq;
  260. return 0;
  261. }
  262. struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  263. struct amdgpu_ring *ring, uint64_t seq)
  264. {
  265. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  266. struct dma_fence *fence;
  267. spin_lock(&ctx->ring_lock);
  268. if (seq == ~0ull)
  269. seq = ctx->rings[ring->idx].sequence - 1;
  270. if (seq >= cring->sequence) {
  271. spin_unlock(&ctx->ring_lock);
  272. return ERR_PTR(-EINVAL);
  273. }
  274. if (seq + amdgpu_sched_jobs < cring->sequence) {
  275. spin_unlock(&ctx->ring_lock);
  276. return NULL;
  277. }
  278. fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
  279. spin_unlock(&ctx->ring_lock);
  280. return fence;
  281. }
  282. void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
  283. enum amd_sched_priority priority)
  284. {
  285. int i;
  286. struct amdgpu_device *adev = ctx->adev;
  287. struct amd_sched_rq *rq;
  288. struct amd_sched_entity *entity;
  289. struct amdgpu_ring *ring;
  290. enum amd_sched_priority ctx_prio;
  291. ctx->override_priority = priority;
  292. ctx_prio = (ctx->override_priority == AMD_SCHED_PRIORITY_UNSET) ?
  293. ctx->init_priority : ctx->override_priority;
  294. for (i = 0; i < adev->num_rings; i++) {
  295. ring = adev->rings[i];
  296. entity = &ctx->rings[i].entity;
  297. rq = &ring->sched.sched_rq[ctx_prio];
  298. if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
  299. continue;
  300. amd_sched_entity_set_rq(entity, rq);
  301. }
  302. }
  303. void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
  304. {
  305. mutex_init(&mgr->lock);
  306. idr_init(&mgr->ctx_handles);
  307. }
  308. void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
  309. {
  310. struct amdgpu_ctx *ctx;
  311. struct idr *idp;
  312. uint32_t id;
  313. idp = &mgr->ctx_handles;
  314. idr_for_each_entry(idp, ctx, id) {
  315. if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
  316. DRM_ERROR("ctx %p is still alive\n", ctx);
  317. }
  318. idr_destroy(&mgr->ctx_handles);
  319. mutex_destroy(&mgr->lock);
  320. }