amdgpu_ctx.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. default:
  194. WARN(1, "Invalid context priority %d\n", amdgpu_priority);
  195. return AMD_SCHED_PRIORITY_NORMAL;
  196. }
  197. }
  198. int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  199. struct drm_file *filp)
  200. {
  201. int r;
  202. uint32_t id;
  203. enum amd_sched_priority priority;
  204. union drm_amdgpu_ctx *args = data;
  205. struct amdgpu_device *adev = dev->dev_private;
  206. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  207. r = 0;
  208. id = args->in.ctx_id;
  209. priority = amdgpu_to_sched_priority(args->in.priority);
  210. if (priority >= AMD_SCHED_PRIORITY_MAX)
  211. return -EINVAL;
  212. switch (args->in.op) {
  213. case AMDGPU_CTX_OP_ALLOC_CTX:
  214. r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
  215. args->out.alloc.ctx_id = id;
  216. break;
  217. case AMDGPU_CTX_OP_FREE_CTX:
  218. r = amdgpu_ctx_free(fpriv, id);
  219. break;
  220. case AMDGPU_CTX_OP_QUERY_STATE:
  221. r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
  222. break;
  223. default:
  224. return -EINVAL;
  225. }
  226. return r;
  227. }
  228. struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
  229. {
  230. struct amdgpu_ctx *ctx;
  231. struct amdgpu_ctx_mgr *mgr;
  232. if (!fpriv)
  233. return NULL;
  234. mgr = &fpriv->ctx_mgr;
  235. mutex_lock(&mgr->lock);
  236. ctx = idr_find(&mgr->ctx_handles, id);
  237. if (ctx)
  238. kref_get(&ctx->refcount);
  239. mutex_unlock(&mgr->lock);
  240. return ctx;
  241. }
  242. int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
  243. {
  244. if (ctx == NULL)
  245. return -EINVAL;
  246. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  247. return 0;
  248. }
  249. int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
  250. struct dma_fence *fence, uint64_t* handler)
  251. {
  252. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  253. uint64_t seq = cring->sequence;
  254. unsigned idx = 0;
  255. struct dma_fence *other = NULL;
  256. idx = seq & (amdgpu_sched_jobs - 1);
  257. other = cring->fences[idx];
  258. if (other) {
  259. signed long r;
  260. r = dma_fence_wait_timeout(other, true, MAX_SCHEDULE_TIMEOUT);
  261. if (r < 0)
  262. return r;
  263. }
  264. dma_fence_get(fence);
  265. spin_lock(&ctx->ring_lock);
  266. cring->fences[idx] = fence;
  267. cring->sequence++;
  268. spin_unlock(&ctx->ring_lock);
  269. dma_fence_put(other);
  270. if (handler)
  271. *handler = seq;
  272. return 0;
  273. }
  274. struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  275. struct amdgpu_ring *ring, uint64_t seq)
  276. {
  277. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  278. struct dma_fence *fence;
  279. spin_lock(&ctx->ring_lock);
  280. if (seq == ~0ull)
  281. seq = ctx->rings[ring->idx].sequence - 1;
  282. if (seq >= cring->sequence) {
  283. spin_unlock(&ctx->ring_lock);
  284. return ERR_PTR(-EINVAL);
  285. }
  286. if (seq + amdgpu_sched_jobs < cring->sequence) {
  287. spin_unlock(&ctx->ring_lock);
  288. return NULL;
  289. }
  290. fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
  291. spin_unlock(&ctx->ring_lock);
  292. return fence;
  293. }
  294. void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
  295. {
  296. mutex_init(&mgr->lock);
  297. idr_init(&mgr->ctx_handles);
  298. }
  299. void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
  300. {
  301. struct amdgpu_ctx *ctx;
  302. struct idr *idp;
  303. uint32_t id;
  304. idp = &mgr->ctx_handles;
  305. idr_for_each_entry(idp, ctx, id) {
  306. if (kref_put(&ctx->refcount, amdgpu_ctx_do_release) != 1)
  307. DRM_ERROR("ctx %p is still alive\n", ctx);
  308. }
  309. idr_destroy(&mgr->ctx_handles);
  310. mutex_destroy(&mgr->lock);
  311. }