amdgpu_ctx.c 14 KB

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