amdgpu_ctx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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 drm_sched_priority priority)
  30. {
  31. /* NORMAL and below are accessible by everyone */
  32. if (priority <= DRM_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 drm_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 >= DRM_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. mutex_init(&ctx->lock);
  61. for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
  62. ctx->rings[i].sequence = 1;
  63. ctx->rings[i].fences = &ctx->fences[amdgpu_sched_jobs * i];
  64. }
  65. ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
  66. ctx->reset_counter_query = ctx->reset_counter;
  67. ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
  68. ctx->init_priority = priority;
  69. ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
  70. /* create context entity for each ring */
  71. for (i = 0; i < adev->num_rings; i++) {
  72. struct amdgpu_ring *ring = adev->rings[i];
  73. struct drm_sched_rq *rq;
  74. rq = &ring->sched.sched_rq[priority];
  75. if (ring == &adev->gfx.kiq.ring)
  76. continue;
  77. r = drm_sched_entity_init(&ring->sched, &ctx->rings[i].entity,
  78. rq, &ctx->guilty);
  79. if (r)
  80. goto failed;
  81. }
  82. r = amdgpu_queue_mgr_init(adev, &ctx->queue_mgr);
  83. if (r)
  84. goto failed;
  85. return 0;
  86. failed:
  87. for (j = 0; j < i; j++)
  88. drm_sched_entity_fini(&adev->rings[j]->sched,
  89. &ctx->rings[j].entity);
  90. kfree(ctx->fences);
  91. ctx->fences = NULL;
  92. return r;
  93. }
  94. static void amdgpu_ctx_fini(struct kref *ref)
  95. {
  96. struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
  97. struct amdgpu_device *adev = ctx->adev;
  98. unsigned i, j;
  99. if (!adev)
  100. return;
  101. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  102. for (j = 0; j < amdgpu_sched_jobs; ++j)
  103. dma_fence_put(ctx->rings[i].fences[j]);
  104. kfree(ctx->fences);
  105. ctx->fences = NULL;
  106. amdgpu_queue_mgr_fini(adev, &ctx->queue_mgr);
  107. mutex_destroy(&ctx->lock);
  108. kfree(ctx);
  109. }
  110. static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
  111. struct amdgpu_fpriv *fpriv,
  112. struct drm_file *filp,
  113. enum drm_sched_priority priority,
  114. uint32_t *id)
  115. {
  116. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  117. struct amdgpu_ctx *ctx;
  118. int r;
  119. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  120. if (!ctx)
  121. return -ENOMEM;
  122. mutex_lock(&mgr->lock);
  123. r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
  124. if (r < 0) {
  125. mutex_unlock(&mgr->lock);
  126. kfree(ctx);
  127. return r;
  128. }
  129. *id = (uint32_t)r;
  130. r = amdgpu_ctx_init(adev, priority, filp, ctx);
  131. if (r) {
  132. idr_remove(&mgr->ctx_handles, *id);
  133. *id = 0;
  134. kfree(ctx);
  135. }
  136. mutex_unlock(&mgr->lock);
  137. return r;
  138. }
  139. static void amdgpu_ctx_do_release(struct kref *ref)
  140. {
  141. struct amdgpu_ctx *ctx;
  142. u32 i;
  143. ctx = container_of(ref, struct amdgpu_ctx, refcount);
  144. for (i = 0; i < ctx->adev->num_rings; i++) {
  145. if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
  146. continue;
  147. drm_sched_entity_fini(&ctx->adev->rings[i]->sched,
  148. &ctx->rings[i].entity);
  149. }
  150. amdgpu_ctx_fini(ref);
  151. }
  152. static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
  153. {
  154. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  155. struct amdgpu_ctx *ctx;
  156. mutex_lock(&mgr->lock);
  157. ctx = idr_remove(&mgr->ctx_handles, id);
  158. if (ctx)
  159. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  160. mutex_unlock(&mgr->lock);
  161. return ctx ? 0 : -EINVAL;
  162. }
  163. static int amdgpu_ctx_query(struct amdgpu_device *adev,
  164. struct amdgpu_fpriv *fpriv, uint32_t id,
  165. union drm_amdgpu_ctx_out *out)
  166. {
  167. struct amdgpu_ctx *ctx;
  168. struct amdgpu_ctx_mgr *mgr;
  169. unsigned reset_counter;
  170. if (!fpriv)
  171. return -EINVAL;
  172. mgr = &fpriv->ctx_mgr;
  173. mutex_lock(&mgr->lock);
  174. ctx = idr_find(&mgr->ctx_handles, id);
  175. if (!ctx) {
  176. mutex_unlock(&mgr->lock);
  177. return -EINVAL;
  178. }
  179. /* TODO: these two are always zero */
  180. out->state.flags = 0x0;
  181. out->state.hangs = 0x0;
  182. /* determine if a GPU reset has occured since the last call */
  183. reset_counter = atomic_read(&adev->gpu_reset_counter);
  184. /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
  185. if (ctx->reset_counter_query == reset_counter)
  186. out->state.reset_status = AMDGPU_CTX_NO_RESET;
  187. else
  188. out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  189. ctx->reset_counter_query = reset_counter;
  190. mutex_unlock(&mgr->lock);
  191. return 0;
  192. }
  193. static int amdgpu_ctx_query2(struct amdgpu_device *adev,
  194. struct amdgpu_fpriv *fpriv, uint32_t id,
  195. union drm_amdgpu_ctx_out *out)
  196. {
  197. struct amdgpu_ctx *ctx;
  198. struct amdgpu_ctx_mgr *mgr;
  199. if (!fpriv)
  200. return -EINVAL;
  201. mgr = &fpriv->ctx_mgr;
  202. mutex_lock(&mgr->lock);
  203. ctx = idr_find(&mgr->ctx_handles, id);
  204. if (!ctx) {
  205. mutex_unlock(&mgr->lock);
  206. return -EINVAL;
  207. }
  208. out->state.flags = 0x0;
  209. out->state.hangs = 0x0;
  210. if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
  211. out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
  212. if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
  213. out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
  214. if (atomic_read(&ctx->guilty))
  215. out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
  216. mutex_unlock(&mgr->lock);
  217. return 0;
  218. }
  219. int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  220. struct drm_file *filp)
  221. {
  222. int r;
  223. uint32_t id;
  224. enum drm_sched_priority priority;
  225. union drm_amdgpu_ctx *args = data;
  226. struct amdgpu_device *adev = dev->dev_private;
  227. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  228. r = 0;
  229. id = args->in.ctx_id;
  230. priority = amdgpu_to_sched_priority(args->in.priority);
  231. /* For backwards compatibility reasons, we need to accept
  232. * ioctls with garbage in the priority field */
  233. if (priority == DRM_SCHED_PRIORITY_INVALID)
  234. priority = DRM_SCHED_PRIORITY_NORMAL;
  235. switch (args->in.op) {
  236. case AMDGPU_CTX_OP_ALLOC_CTX:
  237. r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
  238. args->out.alloc.ctx_id = id;
  239. break;
  240. case AMDGPU_CTX_OP_FREE_CTX:
  241. r = amdgpu_ctx_free(fpriv, id);
  242. break;
  243. case AMDGPU_CTX_OP_QUERY_STATE:
  244. r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
  245. break;
  246. case AMDGPU_CTX_OP_QUERY_STATE2:
  247. r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
  248. break;
  249. default:
  250. return -EINVAL;
  251. }
  252. return r;
  253. }
  254. struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
  255. {
  256. struct amdgpu_ctx *ctx;
  257. struct amdgpu_ctx_mgr *mgr;
  258. if (!fpriv)
  259. return NULL;
  260. mgr = &fpriv->ctx_mgr;
  261. mutex_lock(&mgr->lock);
  262. ctx = idr_find(&mgr->ctx_handles, id);
  263. if (ctx)
  264. kref_get(&ctx->refcount);
  265. mutex_unlock(&mgr->lock);
  266. return ctx;
  267. }
  268. int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
  269. {
  270. if (ctx == NULL)
  271. return -EINVAL;
  272. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  273. return 0;
  274. }
  275. int amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx, struct amdgpu_ring *ring,
  276. struct dma_fence *fence, uint64_t* handler)
  277. {
  278. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  279. uint64_t seq = cring->sequence;
  280. unsigned idx = 0;
  281. struct dma_fence *other = NULL;
  282. idx = seq & (amdgpu_sched_jobs - 1);
  283. other = cring->fences[idx];
  284. if (other)
  285. BUG_ON(!dma_fence_is_signaled(other));
  286. dma_fence_get(fence);
  287. spin_lock(&ctx->ring_lock);
  288. cring->fences[idx] = fence;
  289. cring->sequence++;
  290. spin_unlock(&ctx->ring_lock);
  291. dma_fence_put(other);
  292. if (handler)
  293. *handler = seq;
  294. return 0;
  295. }
  296. struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  297. struct amdgpu_ring *ring, uint64_t seq)
  298. {
  299. struct amdgpu_ctx_ring *cring = & ctx->rings[ring->idx];
  300. struct dma_fence *fence;
  301. spin_lock(&ctx->ring_lock);
  302. if (seq == ~0ull)
  303. seq = ctx->rings[ring->idx].sequence - 1;
  304. if (seq >= cring->sequence) {
  305. spin_unlock(&ctx->ring_lock);
  306. return ERR_PTR(-EINVAL);
  307. }
  308. if (seq + amdgpu_sched_jobs < cring->sequence) {
  309. spin_unlock(&ctx->ring_lock);
  310. return NULL;
  311. }
  312. fence = dma_fence_get(cring->fences[seq & (amdgpu_sched_jobs - 1)]);
  313. spin_unlock(&ctx->ring_lock);
  314. return fence;
  315. }
  316. void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
  317. enum drm_sched_priority priority)
  318. {
  319. int i;
  320. struct amdgpu_device *adev = ctx->adev;
  321. struct drm_sched_rq *rq;
  322. struct drm_sched_entity *entity;
  323. struct amdgpu_ring *ring;
  324. enum drm_sched_priority ctx_prio;
  325. ctx->override_priority = priority;
  326. ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
  327. ctx->init_priority : ctx->override_priority;
  328. for (i = 0; i < adev->num_rings; i++) {
  329. ring = adev->rings[i];
  330. entity = &ctx->rings[i].entity;
  331. rq = &ring->sched.sched_rq[ctx_prio];
  332. if (ring->funcs->type == AMDGPU_RING_TYPE_KIQ)
  333. continue;
  334. drm_sched_entity_set_rq(entity, rq);
  335. }
  336. }
  337. int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx, unsigned ring_id)
  338. {
  339. struct amdgpu_ctx_ring *cring = &ctx->rings[ring_id];
  340. unsigned idx = cring->sequence & (amdgpu_sched_jobs - 1);
  341. struct dma_fence *other = cring->fences[idx];
  342. if (other) {
  343. signed long r;
  344. r = dma_fence_wait(other, true);
  345. if (r < 0) {
  346. if (r != -ERESTARTSYS)
  347. DRM_ERROR("Error (%ld) waiting for fence!\n", r);
  348. return r;
  349. }
  350. }
  351. return 0;
  352. }
  353. void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
  354. {
  355. mutex_init(&mgr->lock);
  356. idr_init(&mgr->ctx_handles);
  357. }
  358. void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
  359. {
  360. struct amdgpu_ctx *ctx;
  361. struct idr *idp;
  362. uint32_t id, i;
  363. idp = &mgr->ctx_handles;
  364. idr_for_each_entry(idp, ctx, id) {
  365. if (!ctx->adev)
  366. return;
  367. for (i = 0; i < ctx->adev->num_rings; i++) {
  368. if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
  369. continue;
  370. if (kref_read(&ctx->refcount) == 1)
  371. drm_sched_entity_do_release(&ctx->adev->rings[i]->sched,
  372. &ctx->rings[i].entity);
  373. else
  374. DRM_ERROR("ctx %p is still alive\n", ctx);
  375. }
  376. }
  377. }
  378. void amdgpu_ctx_mgr_entity_cleanup(struct amdgpu_ctx_mgr *mgr)
  379. {
  380. struct amdgpu_ctx *ctx;
  381. struct idr *idp;
  382. uint32_t id, i;
  383. idp = &mgr->ctx_handles;
  384. idr_for_each_entry(idp, ctx, id) {
  385. if (!ctx->adev)
  386. return;
  387. for (i = 0; i < ctx->adev->num_rings; i++) {
  388. if (ctx->adev->rings[i] == &ctx->adev->gfx.kiq.ring)
  389. continue;
  390. if (kref_read(&ctx->refcount) == 1)
  391. drm_sched_entity_cleanup(&ctx->adev->rings[i]->sched,
  392. &ctx->rings[i].entity);
  393. else
  394. DRM_ERROR("ctx %p is still alive\n", ctx);
  395. }
  396. }
  397. }
  398. void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
  399. {
  400. struct amdgpu_ctx *ctx;
  401. struct idr *idp;
  402. uint32_t id;
  403. amdgpu_ctx_mgr_entity_cleanup(mgr);
  404. idp = &mgr->ctx_handles;
  405. idr_for_each_entry(idp, ctx, id) {
  406. if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
  407. DRM_ERROR("ctx %p is still alive\n", ctx);
  408. }
  409. idr_destroy(&mgr->ctx_handles);
  410. mutex_destroy(&mgr->lock);
  411. }