amdgpu_ctx.c 9.1 KB

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