amdgpu_ctx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. #define to_amdgpu_ctx_entity(e) \
  29. container_of((e), struct amdgpu_ctx_entity, entity)
  30. const unsigned int amdgpu_ctx_num_entities[AMDGPU_HW_IP_NUM] = {
  31. [AMDGPU_HW_IP_GFX] = 1,
  32. [AMDGPU_HW_IP_COMPUTE] = 4,
  33. [AMDGPU_HW_IP_DMA] = 2,
  34. [AMDGPU_HW_IP_UVD] = 1,
  35. [AMDGPU_HW_IP_VCE] = 1,
  36. [AMDGPU_HW_IP_UVD_ENC] = 1,
  37. [AMDGPU_HW_IP_VCN_DEC] = 1,
  38. [AMDGPU_HW_IP_VCN_ENC] = 1,
  39. };
  40. static int amdgput_ctx_total_num_entities(void)
  41. {
  42. unsigned i, num_entities = 0;
  43. for (i = 0; i < AMDGPU_HW_IP_NUM; ++i)
  44. num_entities += amdgpu_ctx_num_entities[i];
  45. return num_entities;
  46. }
  47. static int amdgpu_ctx_priority_permit(struct drm_file *filp,
  48. enum drm_sched_priority priority)
  49. {
  50. /* NORMAL and below are accessible by everyone */
  51. if (priority <= DRM_SCHED_PRIORITY_NORMAL)
  52. return 0;
  53. if (capable(CAP_SYS_NICE))
  54. return 0;
  55. if (drm_is_current_master(filp))
  56. return 0;
  57. return -EACCES;
  58. }
  59. static int amdgpu_ctx_init(struct amdgpu_device *adev,
  60. enum drm_sched_priority priority,
  61. struct drm_file *filp,
  62. struct amdgpu_ctx *ctx)
  63. {
  64. unsigned num_entities = amdgput_ctx_total_num_entities();
  65. unsigned i, j;
  66. int r;
  67. if (priority < 0 || priority >= DRM_SCHED_PRIORITY_MAX)
  68. return -EINVAL;
  69. r = amdgpu_ctx_priority_permit(filp, priority);
  70. if (r)
  71. return r;
  72. memset(ctx, 0, sizeof(*ctx));
  73. ctx->adev = adev;
  74. ctx->fences = kcalloc(amdgpu_sched_jobs * num_entities,
  75. sizeof(struct dma_fence*), GFP_KERNEL);
  76. if (!ctx->fences)
  77. return -ENOMEM;
  78. ctx->entities[0] = kcalloc(num_entities,
  79. sizeof(struct amdgpu_ctx_entity),
  80. GFP_KERNEL);
  81. if (!ctx->entities[0]) {
  82. r = -ENOMEM;
  83. goto error_free_fences;
  84. }
  85. for (i = 0; i < num_entities; ++i) {
  86. struct amdgpu_ctx_entity *entity = &ctx->entities[0][i];
  87. entity->sequence = 1;
  88. entity->fences = &ctx->fences[amdgpu_sched_jobs * i];
  89. }
  90. for (i = 1; i < AMDGPU_HW_IP_NUM; ++i)
  91. ctx->entities[i] = ctx->entities[i - 1] +
  92. amdgpu_ctx_num_entities[i - 1];
  93. kref_init(&ctx->refcount);
  94. spin_lock_init(&ctx->ring_lock);
  95. mutex_init(&ctx->lock);
  96. ctx->reset_counter = atomic_read(&adev->gpu_reset_counter);
  97. ctx->reset_counter_query = ctx->reset_counter;
  98. ctx->vram_lost_counter = atomic_read(&adev->vram_lost_counter);
  99. ctx->init_priority = priority;
  100. ctx->override_priority = DRM_SCHED_PRIORITY_UNSET;
  101. for (i = 0; i < AMDGPU_HW_IP_NUM; ++i) {
  102. struct amdgpu_ring *rings[AMDGPU_MAX_RINGS];
  103. struct drm_sched_rq *rqs[AMDGPU_MAX_RINGS];
  104. unsigned num_rings;
  105. switch (i) {
  106. case AMDGPU_HW_IP_GFX:
  107. rings[0] = &adev->gfx.gfx_ring[0];
  108. num_rings = 1;
  109. break;
  110. case AMDGPU_HW_IP_COMPUTE:
  111. for (j = 0; j < adev->gfx.num_compute_rings; ++j)
  112. rings[j] = &adev->gfx.compute_ring[j];
  113. num_rings = adev->gfx.num_compute_rings;
  114. break;
  115. case AMDGPU_HW_IP_DMA:
  116. for (j = 0; j < adev->sdma.num_instances; ++j)
  117. rings[j] = &adev->sdma.instance[j].ring;
  118. num_rings = adev->sdma.num_instances;
  119. break;
  120. case AMDGPU_HW_IP_UVD:
  121. rings[0] = &adev->uvd.inst[0].ring;
  122. num_rings = 1;
  123. break;
  124. case AMDGPU_HW_IP_VCE:
  125. rings[0] = &adev->vce.ring[0];
  126. num_rings = 1;
  127. break;
  128. case AMDGPU_HW_IP_UVD_ENC:
  129. rings[0] = &adev->uvd.inst[0].ring_enc[0];
  130. num_rings = 1;
  131. break;
  132. case AMDGPU_HW_IP_VCN_DEC:
  133. rings[0] = &adev->vcn.ring_dec;
  134. num_rings = 1;
  135. break;
  136. case AMDGPU_HW_IP_VCN_ENC:
  137. rings[0] = &adev->vcn.ring_enc[0];
  138. num_rings = 1;
  139. break;
  140. case AMDGPU_HW_IP_VCN_JPEG:
  141. rings[0] = &adev->vcn.ring_jpeg;
  142. num_rings = 1;
  143. break;
  144. }
  145. for (j = 0; j < num_rings; ++j)
  146. rqs[j] = &rings[j]->sched.sched_rq[priority];
  147. for (j = 0; j < amdgpu_ctx_num_entities[i]; ++j)
  148. r = drm_sched_entity_init(&ctx->entities[i][j].entity,
  149. rqs, num_rings, &ctx->guilty);
  150. if (r)
  151. goto error_cleanup_entities;
  152. }
  153. return 0;
  154. error_cleanup_entities:
  155. for (i = 0; i < num_entities; ++i)
  156. drm_sched_entity_destroy(&ctx->entities[0][i].entity);
  157. kfree(ctx->entities[0]);
  158. error_free_fences:
  159. kfree(ctx->fences);
  160. ctx->fences = NULL;
  161. return r;
  162. }
  163. static void amdgpu_ctx_fini(struct kref *ref)
  164. {
  165. struct amdgpu_ctx *ctx = container_of(ref, struct amdgpu_ctx, refcount);
  166. unsigned num_entities = amdgput_ctx_total_num_entities();
  167. struct amdgpu_device *adev = ctx->adev;
  168. unsigned i, j;
  169. if (!adev)
  170. return;
  171. for (i = 0; i < num_entities; ++i)
  172. for (j = 0; j < amdgpu_sched_jobs; ++j)
  173. dma_fence_put(ctx->entities[0][i].fences[j]);
  174. kfree(ctx->fences);
  175. kfree(ctx->entities[0]);
  176. mutex_destroy(&ctx->lock);
  177. kfree(ctx);
  178. }
  179. int amdgpu_ctx_get_entity(struct amdgpu_ctx *ctx, u32 hw_ip, u32 instance,
  180. u32 ring, struct drm_sched_entity **entity)
  181. {
  182. if (hw_ip >= AMDGPU_HW_IP_NUM) {
  183. DRM_ERROR("unknown HW IP type: %d\n", hw_ip);
  184. return -EINVAL;
  185. }
  186. /* Right now all IPs have only one instance - multiple rings. */
  187. if (instance != 0) {
  188. DRM_DEBUG("invalid ip instance: %d\n", instance);
  189. return -EINVAL;
  190. }
  191. if (ring >= amdgpu_ctx_num_entities[hw_ip]) {
  192. DRM_DEBUG("invalid ring: %d %d\n", hw_ip, ring);
  193. return -EINVAL;
  194. }
  195. *entity = &ctx->entities[hw_ip][ring].entity;
  196. return 0;
  197. }
  198. static int amdgpu_ctx_alloc(struct amdgpu_device *adev,
  199. struct amdgpu_fpriv *fpriv,
  200. struct drm_file *filp,
  201. enum drm_sched_priority priority,
  202. uint32_t *id)
  203. {
  204. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  205. struct amdgpu_ctx *ctx;
  206. int r;
  207. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  208. if (!ctx)
  209. return -ENOMEM;
  210. mutex_lock(&mgr->lock);
  211. r = idr_alloc(&mgr->ctx_handles, ctx, 1, 0, GFP_KERNEL);
  212. if (r < 0) {
  213. mutex_unlock(&mgr->lock);
  214. kfree(ctx);
  215. return r;
  216. }
  217. *id = (uint32_t)r;
  218. r = amdgpu_ctx_init(adev, priority, filp, ctx);
  219. if (r) {
  220. idr_remove(&mgr->ctx_handles, *id);
  221. *id = 0;
  222. kfree(ctx);
  223. }
  224. mutex_unlock(&mgr->lock);
  225. return r;
  226. }
  227. static void amdgpu_ctx_do_release(struct kref *ref)
  228. {
  229. struct amdgpu_ctx *ctx;
  230. unsigned num_entities;
  231. u32 i;
  232. ctx = container_of(ref, struct amdgpu_ctx, refcount);
  233. num_entities = 0;
  234. for (i = 0; i < AMDGPU_HW_IP_NUM; i++)
  235. num_entities += amdgpu_ctx_num_entities[i];
  236. for (i = 0; i < num_entities; i++)
  237. drm_sched_entity_destroy(&ctx->entities[0][i].entity);
  238. amdgpu_ctx_fini(ref);
  239. }
  240. static int amdgpu_ctx_free(struct amdgpu_fpriv *fpriv, uint32_t id)
  241. {
  242. struct amdgpu_ctx_mgr *mgr = &fpriv->ctx_mgr;
  243. struct amdgpu_ctx *ctx;
  244. mutex_lock(&mgr->lock);
  245. ctx = idr_remove(&mgr->ctx_handles, id);
  246. if (ctx)
  247. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  248. mutex_unlock(&mgr->lock);
  249. return ctx ? 0 : -EINVAL;
  250. }
  251. static int amdgpu_ctx_query(struct amdgpu_device *adev,
  252. struct amdgpu_fpriv *fpriv, uint32_t id,
  253. union drm_amdgpu_ctx_out *out)
  254. {
  255. struct amdgpu_ctx *ctx;
  256. struct amdgpu_ctx_mgr *mgr;
  257. unsigned reset_counter;
  258. if (!fpriv)
  259. return -EINVAL;
  260. mgr = &fpriv->ctx_mgr;
  261. mutex_lock(&mgr->lock);
  262. ctx = idr_find(&mgr->ctx_handles, id);
  263. if (!ctx) {
  264. mutex_unlock(&mgr->lock);
  265. return -EINVAL;
  266. }
  267. /* TODO: these two are always zero */
  268. out->state.flags = 0x0;
  269. out->state.hangs = 0x0;
  270. /* determine if a GPU reset has occured since the last call */
  271. reset_counter = atomic_read(&adev->gpu_reset_counter);
  272. /* TODO: this should ideally return NO, GUILTY, or INNOCENT. */
  273. if (ctx->reset_counter_query == reset_counter)
  274. out->state.reset_status = AMDGPU_CTX_NO_RESET;
  275. else
  276. out->state.reset_status = AMDGPU_CTX_UNKNOWN_RESET;
  277. ctx->reset_counter_query = reset_counter;
  278. mutex_unlock(&mgr->lock);
  279. return 0;
  280. }
  281. static int amdgpu_ctx_query2(struct amdgpu_device *adev,
  282. struct amdgpu_fpriv *fpriv, uint32_t id,
  283. union drm_amdgpu_ctx_out *out)
  284. {
  285. struct amdgpu_ctx *ctx;
  286. struct amdgpu_ctx_mgr *mgr;
  287. if (!fpriv)
  288. return -EINVAL;
  289. mgr = &fpriv->ctx_mgr;
  290. mutex_lock(&mgr->lock);
  291. ctx = idr_find(&mgr->ctx_handles, id);
  292. if (!ctx) {
  293. mutex_unlock(&mgr->lock);
  294. return -EINVAL;
  295. }
  296. out->state.flags = 0x0;
  297. out->state.hangs = 0x0;
  298. if (ctx->reset_counter != atomic_read(&adev->gpu_reset_counter))
  299. out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_RESET;
  300. if (ctx->vram_lost_counter != atomic_read(&adev->vram_lost_counter))
  301. out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_VRAMLOST;
  302. if (atomic_read(&ctx->guilty))
  303. out->state.flags |= AMDGPU_CTX_QUERY2_FLAGS_GUILTY;
  304. mutex_unlock(&mgr->lock);
  305. return 0;
  306. }
  307. int amdgpu_ctx_ioctl(struct drm_device *dev, void *data,
  308. struct drm_file *filp)
  309. {
  310. int r;
  311. uint32_t id;
  312. enum drm_sched_priority priority;
  313. union drm_amdgpu_ctx *args = data;
  314. struct amdgpu_device *adev = dev->dev_private;
  315. struct amdgpu_fpriv *fpriv = filp->driver_priv;
  316. r = 0;
  317. id = args->in.ctx_id;
  318. priority = amdgpu_to_sched_priority(args->in.priority);
  319. /* For backwards compatibility reasons, we need to accept
  320. * ioctls with garbage in the priority field */
  321. if (priority == DRM_SCHED_PRIORITY_INVALID)
  322. priority = DRM_SCHED_PRIORITY_NORMAL;
  323. switch (args->in.op) {
  324. case AMDGPU_CTX_OP_ALLOC_CTX:
  325. r = amdgpu_ctx_alloc(adev, fpriv, filp, priority, &id);
  326. args->out.alloc.ctx_id = id;
  327. break;
  328. case AMDGPU_CTX_OP_FREE_CTX:
  329. r = amdgpu_ctx_free(fpriv, id);
  330. break;
  331. case AMDGPU_CTX_OP_QUERY_STATE:
  332. r = amdgpu_ctx_query(adev, fpriv, id, &args->out);
  333. break;
  334. case AMDGPU_CTX_OP_QUERY_STATE2:
  335. r = amdgpu_ctx_query2(adev, fpriv, id, &args->out);
  336. break;
  337. default:
  338. return -EINVAL;
  339. }
  340. return r;
  341. }
  342. struct amdgpu_ctx *amdgpu_ctx_get(struct amdgpu_fpriv *fpriv, uint32_t id)
  343. {
  344. struct amdgpu_ctx *ctx;
  345. struct amdgpu_ctx_mgr *mgr;
  346. if (!fpriv)
  347. return NULL;
  348. mgr = &fpriv->ctx_mgr;
  349. mutex_lock(&mgr->lock);
  350. ctx = idr_find(&mgr->ctx_handles, id);
  351. if (ctx)
  352. kref_get(&ctx->refcount);
  353. mutex_unlock(&mgr->lock);
  354. return ctx;
  355. }
  356. int amdgpu_ctx_put(struct amdgpu_ctx *ctx)
  357. {
  358. if (ctx == NULL)
  359. return -EINVAL;
  360. kref_put(&ctx->refcount, amdgpu_ctx_do_release);
  361. return 0;
  362. }
  363. void amdgpu_ctx_add_fence(struct amdgpu_ctx *ctx,
  364. struct drm_sched_entity *entity,
  365. struct dma_fence *fence, uint64_t* handle)
  366. {
  367. struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
  368. uint64_t seq = centity->sequence;
  369. struct dma_fence *other = NULL;
  370. unsigned idx = 0;
  371. idx = seq & (amdgpu_sched_jobs - 1);
  372. other = centity->fences[idx];
  373. if (other)
  374. BUG_ON(!dma_fence_is_signaled(other));
  375. dma_fence_get(fence);
  376. spin_lock(&ctx->ring_lock);
  377. centity->fences[idx] = fence;
  378. centity->sequence++;
  379. spin_unlock(&ctx->ring_lock);
  380. dma_fence_put(other);
  381. if (handle)
  382. *handle = seq;
  383. }
  384. struct dma_fence *amdgpu_ctx_get_fence(struct amdgpu_ctx *ctx,
  385. struct drm_sched_entity *entity,
  386. uint64_t seq)
  387. {
  388. struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
  389. struct dma_fence *fence;
  390. spin_lock(&ctx->ring_lock);
  391. if (seq == ~0ull)
  392. seq = centity->sequence - 1;
  393. if (seq >= centity->sequence) {
  394. spin_unlock(&ctx->ring_lock);
  395. return ERR_PTR(-EINVAL);
  396. }
  397. if (seq + amdgpu_sched_jobs < centity->sequence) {
  398. spin_unlock(&ctx->ring_lock);
  399. return NULL;
  400. }
  401. fence = dma_fence_get(centity->fences[seq & (amdgpu_sched_jobs - 1)]);
  402. spin_unlock(&ctx->ring_lock);
  403. return fence;
  404. }
  405. void amdgpu_ctx_priority_override(struct amdgpu_ctx *ctx,
  406. enum drm_sched_priority priority)
  407. {
  408. unsigned num_entities = amdgput_ctx_total_num_entities();
  409. enum drm_sched_priority ctx_prio;
  410. unsigned i;
  411. ctx->override_priority = priority;
  412. ctx_prio = (ctx->override_priority == DRM_SCHED_PRIORITY_UNSET) ?
  413. ctx->init_priority : ctx->override_priority;
  414. for (i = 0; i < num_entities; i++) {
  415. struct drm_sched_entity *entity = &ctx->entities[0][i].entity;
  416. drm_sched_entity_set_priority(entity, ctx_prio);
  417. }
  418. }
  419. int amdgpu_ctx_wait_prev_fence(struct amdgpu_ctx *ctx,
  420. struct drm_sched_entity *entity)
  421. {
  422. struct amdgpu_ctx_entity *centity = to_amdgpu_ctx_entity(entity);
  423. unsigned idx = centity->sequence & (amdgpu_sched_jobs - 1);
  424. struct dma_fence *other = centity->fences[idx];
  425. if (other) {
  426. signed long r;
  427. r = dma_fence_wait(other, true);
  428. if (r < 0) {
  429. if (r != -ERESTARTSYS)
  430. DRM_ERROR("Error (%ld) waiting for fence!\n", r);
  431. return r;
  432. }
  433. }
  434. return 0;
  435. }
  436. void amdgpu_ctx_mgr_init(struct amdgpu_ctx_mgr *mgr)
  437. {
  438. mutex_init(&mgr->lock);
  439. idr_init(&mgr->ctx_handles);
  440. }
  441. void amdgpu_ctx_mgr_entity_flush(struct amdgpu_ctx_mgr *mgr)
  442. {
  443. unsigned num_entities = amdgput_ctx_total_num_entities();
  444. struct amdgpu_ctx *ctx;
  445. struct idr *idp;
  446. uint32_t id, i;
  447. long max_wait = MAX_WAIT_SCHED_ENTITY_Q_EMPTY;
  448. idp = &mgr->ctx_handles;
  449. mutex_lock(&mgr->lock);
  450. idr_for_each_entry(idp, ctx, id) {
  451. if (!ctx->adev) {
  452. mutex_unlock(&mgr->lock);
  453. return;
  454. }
  455. for (i = 0; i < num_entities; i++) {
  456. struct drm_sched_entity *entity;
  457. entity = &ctx->entities[0][i].entity;
  458. max_wait = drm_sched_entity_flush(entity, max_wait);
  459. }
  460. }
  461. mutex_unlock(&mgr->lock);
  462. }
  463. void amdgpu_ctx_mgr_entity_fini(struct amdgpu_ctx_mgr *mgr)
  464. {
  465. unsigned num_entities = amdgput_ctx_total_num_entities();
  466. struct amdgpu_ctx *ctx;
  467. struct idr *idp;
  468. uint32_t id, i;
  469. idp = &mgr->ctx_handles;
  470. idr_for_each_entry(idp, ctx, id) {
  471. if (!ctx->adev)
  472. return;
  473. if (kref_read(&ctx->refcount) != 1) {
  474. DRM_ERROR("ctx %p is still alive\n", ctx);
  475. continue;
  476. }
  477. for (i = 0; i < num_entities; i++)
  478. drm_sched_entity_fini(&ctx->entities[0][i].entity);
  479. }
  480. }
  481. void amdgpu_ctx_mgr_fini(struct amdgpu_ctx_mgr *mgr)
  482. {
  483. struct amdgpu_ctx *ctx;
  484. struct idr *idp;
  485. uint32_t id;
  486. amdgpu_ctx_mgr_entity_fini(mgr);
  487. idp = &mgr->ctx_handles;
  488. idr_for_each_entry(idp, ctx, id) {
  489. if (kref_put(&ctx->refcount, amdgpu_ctx_fini) != 1)
  490. DRM_ERROR("ctx %p is still alive\n", ctx);
  491. }
  492. idr_destroy(&mgr->ctx_handles);
  493. mutex_destroy(&mgr->lock);
  494. }