amdgpu_vcn.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * Copyright 2016 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. */
  26. #include <linux/firmware.h>
  27. #include <linux/module.h>
  28. #include <drm/drmP.h>
  29. #include <drm/drm.h>
  30. #include "amdgpu.h"
  31. #include "amdgpu_pm.h"
  32. #include "amdgpu_vcn.h"
  33. #include "soc15d.h"
  34. #include "soc15_common.h"
  35. #include "vega10/soc15ip.h"
  36. #include "raven1/VCN/vcn_1_0_offset.h"
  37. /* 1 second timeout */
  38. #define VCN_IDLE_TIMEOUT msecs_to_jiffies(1000)
  39. /* Firmware Names */
  40. #define FIRMWARE_RAVEN "amdgpu/raven_vcn.bin"
  41. MODULE_FIRMWARE(FIRMWARE_RAVEN);
  42. static void amdgpu_vcn_idle_work_handler(struct work_struct *work);
  43. int amdgpu_vcn_sw_init(struct amdgpu_device *adev)
  44. {
  45. struct amdgpu_ring *ring;
  46. struct amd_sched_rq *rq;
  47. unsigned long bo_size;
  48. const char *fw_name;
  49. const struct common_firmware_header *hdr;
  50. unsigned version_major, version_minor, family_id;
  51. int r;
  52. INIT_DELAYED_WORK(&adev->vcn.idle_work, amdgpu_vcn_idle_work_handler);
  53. switch (adev->asic_type) {
  54. case CHIP_RAVEN:
  55. fw_name = FIRMWARE_RAVEN;
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. r = request_firmware(&adev->vcn.fw, fw_name, adev->dev);
  61. if (r) {
  62. dev_err(adev->dev, "amdgpu_vcn: Can't load firmware \"%s\"\n",
  63. fw_name);
  64. return r;
  65. }
  66. r = amdgpu_ucode_validate(adev->vcn.fw);
  67. if (r) {
  68. dev_err(adev->dev, "amdgpu_vcn: Can't validate firmware \"%s\"\n",
  69. fw_name);
  70. release_firmware(adev->vcn.fw);
  71. adev->vcn.fw = NULL;
  72. return r;
  73. }
  74. hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
  75. adev->vcn.fw_version = le32_to_cpu(hdr->ucode_version);
  76. family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
  77. version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
  78. version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
  79. DRM_INFO("Found VCN firmware Version: %hu.%hu Family ID: %hu\n",
  80. version_major, version_minor, family_id);
  81. bo_size = AMDGPU_VCN_STACK_SIZE + AMDGPU_VCN_HEAP_SIZE
  82. + AMDGPU_VCN_SESSION_SIZE * 40;
  83. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  84. bo_size += AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8);
  85. r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
  86. AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.vcpu_bo,
  87. &adev->vcn.gpu_addr, &adev->vcn.cpu_addr);
  88. if (r) {
  89. dev_err(adev->dev, "(%d) failed to allocate vcn bo\n", r);
  90. return r;
  91. }
  92. ring = &adev->vcn.ring_dec;
  93. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
  94. r = amd_sched_entity_init(&ring->sched, &adev->vcn.entity_dec,
  95. rq, amdgpu_sched_jobs);
  96. if (r != 0) {
  97. DRM_ERROR("Failed setting up VCN dec run queue.\n");
  98. return r;
  99. }
  100. ring = &adev->vcn.ring_enc[0];
  101. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
  102. r = amd_sched_entity_init(&ring->sched, &adev->vcn.entity_enc,
  103. rq, amdgpu_sched_jobs);
  104. if (r != 0) {
  105. DRM_ERROR("Failed setting up VCN enc run queue.\n");
  106. return r;
  107. }
  108. return 0;
  109. }
  110. int amdgpu_vcn_sw_fini(struct amdgpu_device *adev)
  111. {
  112. int i;
  113. kfree(adev->vcn.saved_bo);
  114. amd_sched_entity_fini(&adev->vcn.ring_dec.sched, &adev->vcn.entity_dec);
  115. amd_sched_entity_fini(&adev->vcn.ring_enc[0].sched, &adev->vcn.entity_enc);
  116. amdgpu_bo_free_kernel(&adev->vcn.vcpu_bo,
  117. &adev->vcn.gpu_addr,
  118. (void **)&adev->vcn.cpu_addr);
  119. amdgpu_ring_fini(&adev->vcn.ring_dec);
  120. for (i = 0; i < adev->vcn.num_enc_rings; ++i)
  121. amdgpu_ring_fini(&adev->vcn.ring_enc[i]);
  122. release_firmware(adev->vcn.fw);
  123. return 0;
  124. }
  125. int amdgpu_vcn_suspend(struct amdgpu_device *adev)
  126. {
  127. unsigned size;
  128. void *ptr;
  129. cancel_delayed_work_sync(&adev->vcn.idle_work);
  130. if (adev->vcn.vcpu_bo == NULL)
  131. return 0;
  132. size = amdgpu_bo_size(adev->vcn.vcpu_bo);
  133. ptr = adev->vcn.cpu_addr;
  134. adev->vcn.saved_bo = kmalloc(size, GFP_KERNEL);
  135. if (!adev->vcn.saved_bo)
  136. return -ENOMEM;
  137. memcpy_fromio(adev->vcn.saved_bo, ptr, size);
  138. return 0;
  139. }
  140. int amdgpu_vcn_resume(struct amdgpu_device *adev)
  141. {
  142. unsigned size;
  143. void *ptr;
  144. if (adev->vcn.vcpu_bo == NULL)
  145. return -EINVAL;
  146. size = amdgpu_bo_size(adev->vcn.vcpu_bo);
  147. ptr = adev->vcn.cpu_addr;
  148. if (adev->vcn.saved_bo != NULL) {
  149. memcpy_toio(ptr, adev->vcn.saved_bo, size);
  150. kfree(adev->vcn.saved_bo);
  151. adev->vcn.saved_bo = NULL;
  152. } else {
  153. const struct common_firmware_header *hdr;
  154. unsigned offset;
  155. hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
  156. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
  157. offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
  158. memcpy_toio(adev->vcn.cpu_addr, adev->vcn.fw->data + offset,
  159. le32_to_cpu(hdr->ucode_size_bytes));
  160. size -= le32_to_cpu(hdr->ucode_size_bytes);
  161. ptr += le32_to_cpu(hdr->ucode_size_bytes);
  162. }
  163. memset_io(ptr, 0, size);
  164. }
  165. return 0;
  166. }
  167. static void amdgpu_vcn_idle_work_handler(struct work_struct *work)
  168. {
  169. struct amdgpu_device *adev =
  170. container_of(work, struct amdgpu_device, vcn.idle_work.work);
  171. unsigned fences = amdgpu_fence_count_emitted(&adev->vcn.ring_dec);
  172. if (fences == 0) {
  173. if (adev->pm.dpm_enabled) {
  174. /* might be used when with pg/cg
  175. amdgpu_dpm_enable_uvd(adev, false);
  176. */
  177. }
  178. } else {
  179. schedule_delayed_work(&adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
  180. }
  181. }
  182. void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
  183. {
  184. struct amdgpu_device *adev = ring->adev;
  185. bool set_clocks = !cancel_delayed_work_sync(&adev->vcn.idle_work);
  186. if (set_clocks && adev->pm.dpm_enabled) {
  187. /* might be used when with pg/cg
  188. amdgpu_dpm_enable_uvd(adev, true);
  189. */
  190. }
  191. }
  192. void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)
  193. {
  194. schedule_delayed_work(&ring->adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
  195. }
  196. int amdgpu_vcn_dec_ring_test_ring(struct amdgpu_ring *ring)
  197. {
  198. struct amdgpu_device *adev = ring->adev;
  199. uint32_t tmp = 0;
  200. unsigned i;
  201. int r;
  202. WREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_CONTEXT_ID), 0xCAFEDEAD);
  203. r = amdgpu_ring_alloc(ring, 3);
  204. if (r) {
  205. DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
  206. ring->idx, r);
  207. return r;
  208. }
  209. amdgpu_ring_write(ring,
  210. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_CONTEXT_ID), 0));
  211. amdgpu_ring_write(ring, 0xDEADBEEF);
  212. amdgpu_ring_commit(ring);
  213. for (i = 0; i < adev->usec_timeout; i++) {
  214. tmp = RREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_CONTEXT_ID));
  215. if (tmp == 0xDEADBEEF)
  216. break;
  217. DRM_UDELAY(1);
  218. }
  219. if (i < adev->usec_timeout) {
  220. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  221. ring->idx, i);
  222. } else {
  223. DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
  224. ring->idx, tmp);
  225. r = -EINVAL;
  226. }
  227. return r;
  228. }
  229. static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
  230. bool direct, struct dma_fence **fence)
  231. {
  232. struct ttm_validate_buffer tv;
  233. struct ww_acquire_ctx ticket;
  234. struct list_head head;
  235. struct amdgpu_job *job;
  236. struct amdgpu_ib *ib;
  237. struct dma_fence *f = NULL;
  238. struct amdgpu_device *adev = ring->adev;
  239. uint64_t addr;
  240. int i, r;
  241. memset(&tv, 0, sizeof(tv));
  242. tv.bo = &bo->tbo;
  243. INIT_LIST_HEAD(&head);
  244. list_add(&tv.head, &head);
  245. r = ttm_eu_reserve_buffers(&ticket, &head, true, NULL);
  246. if (r)
  247. return r;
  248. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  249. if (r)
  250. goto err;
  251. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  252. if (r)
  253. goto err;
  254. ib = &job->ibs[0];
  255. addr = amdgpu_bo_gpu_offset(bo);
  256. ib->ptr[0] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA0), 0);
  257. ib->ptr[1] = addr;
  258. ib->ptr[2] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA1), 0);
  259. ib->ptr[3] = addr >> 32;
  260. ib->ptr[4] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0);
  261. ib->ptr[5] = 0;
  262. for (i = 6; i < 16; i += 2) {
  263. ib->ptr[i] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_NO_OP), 0);
  264. ib->ptr[i+1] = 0;
  265. }
  266. ib->length_dw = 16;
  267. if (direct) {
  268. r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
  269. job->fence = dma_fence_get(f);
  270. if (r)
  271. goto err_free;
  272. amdgpu_job_free(job);
  273. } else {
  274. r = amdgpu_job_submit(job, ring, &adev->vcn.entity_dec,
  275. AMDGPU_FENCE_OWNER_UNDEFINED, &f);
  276. if (r)
  277. goto err_free;
  278. }
  279. ttm_eu_fence_buffer_objects(&ticket, &head, f);
  280. if (fence)
  281. *fence = dma_fence_get(f);
  282. amdgpu_bo_unref(&bo);
  283. dma_fence_put(f);
  284. return 0;
  285. err_free:
  286. amdgpu_job_free(job);
  287. err:
  288. ttm_eu_backoff_reservation(&ticket, &head);
  289. return r;
  290. }
  291. static int amdgpu_vcn_dec_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  292. struct dma_fence **fence)
  293. {
  294. struct amdgpu_device *adev = ring->adev;
  295. struct amdgpu_bo *bo;
  296. uint32_t *msg;
  297. int r, i;
  298. r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
  299. AMDGPU_GEM_DOMAIN_VRAM,
  300. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  301. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  302. NULL, NULL, 0, &bo);
  303. if (r)
  304. return r;
  305. r = amdgpu_bo_reserve(bo, false);
  306. if (r) {
  307. amdgpu_bo_unref(&bo);
  308. return r;
  309. }
  310. r = amdgpu_bo_kmap(bo, (void **)&msg);
  311. if (r) {
  312. amdgpu_bo_unreserve(bo);
  313. amdgpu_bo_unref(&bo);
  314. return r;
  315. }
  316. msg[0] = cpu_to_le32(0x00000028);
  317. msg[1] = cpu_to_le32(0x00000038);
  318. msg[2] = cpu_to_le32(0x00000001);
  319. msg[3] = cpu_to_le32(0x00000000);
  320. msg[4] = cpu_to_le32(handle);
  321. msg[5] = cpu_to_le32(0x00000000);
  322. msg[6] = cpu_to_le32(0x00000001);
  323. msg[7] = cpu_to_le32(0x00000028);
  324. msg[8] = cpu_to_le32(0x00000010);
  325. msg[9] = cpu_to_le32(0x00000000);
  326. msg[10] = cpu_to_le32(0x00000007);
  327. msg[11] = cpu_to_le32(0x00000000);
  328. msg[12] = cpu_to_le32(0x00000780);
  329. msg[13] = cpu_to_le32(0x00000440);
  330. for (i = 14; i < 1024; ++i)
  331. msg[i] = cpu_to_le32(0x0);
  332. amdgpu_bo_kunmap(bo);
  333. amdgpu_bo_unreserve(bo);
  334. return amdgpu_vcn_dec_send_msg(ring, bo, true, fence);
  335. }
  336. static int amdgpu_vcn_dec_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  337. bool direct, struct dma_fence **fence)
  338. {
  339. struct amdgpu_device *adev = ring->adev;
  340. struct amdgpu_bo *bo;
  341. uint32_t *msg;
  342. int r, i;
  343. r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
  344. AMDGPU_GEM_DOMAIN_VRAM,
  345. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  346. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  347. NULL, NULL, 0, &bo);
  348. if (r)
  349. return r;
  350. r = amdgpu_bo_reserve(bo, false);
  351. if (r) {
  352. amdgpu_bo_unref(&bo);
  353. return r;
  354. }
  355. r = amdgpu_bo_kmap(bo, (void **)&msg);
  356. if (r) {
  357. amdgpu_bo_unreserve(bo);
  358. amdgpu_bo_unref(&bo);
  359. return r;
  360. }
  361. msg[0] = cpu_to_le32(0x00000028);
  362. msg[1] = cpu_to_le32(0x00000018);
  363. msg[2] = cpu_to_le32(0x00000000);
  364. msg[3] = cpu_to_le32(0x00000002);
  365. msg[4] = cpu_to_le32(handle);
  366. msg[5] = cpu_to_le32(0x00000000);
  367. for (i = 6; i < 1024; ++i)
  368. msg[i] = cpu_to_le32(0x0);
  369. amdgpu_bo_kunmap(bo);
  370. amdgpu_bo_unreserve(bo);
  371. return amdgpu_vcn_dec_send_msg(ring, bo, direct, fence);
  372. }
  373. int amdgpu_vcn_dec_ring_test_ib(struct amdgpu_ring *ring, long timeout)
  374. {
  375. struct dma_fence *fence;
  376. long r;
  377. r = amdgpu_vcn_dec_get_create_msg(ring, 1, NULL);
  378. if (r) {
  379. DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
  380. goto error;
  381. }
  382. r = amdgpu_vcn_dec_get_destroy_msg(ring, 1, true, &fence);
  383. if (r) {
  384. DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
  385. goto error;
  386. }
  387. r = dma_fence_wait_timeout(fence, false, timeout);
  388. if (r == 0) {
  389. DRM_ERROR("amdgpu: IB test timed out.\n");
  390. r = -ETIMEDOUT;
  391. } else if (r < 0) {
  392. DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
  393. } else {
  394. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  395. r = 0;
  396. }
  397. dma_fence_put(fence);
  398. error:
  399. return r;
  400. }
  401. int amdgpu_vcn_enc_ring_test_ring(struct amdgpu_ring *ring)
  402. {
  403. struct amdgpu_device *adev = ring->adev;
  404. uint32_t rptr = amdgpu_ring_get_rptr(ring);
  405. unsigned i;
  406. int r;
  407. r = amdgpu_ring_alloc(ring, 16);
  408. if (r) {
  409. DRM_ERROR("amdgpu: vcn enc failed to lock ring %d (%d).\n",
  410. ring->idx, r);
  411. return r;
  412. }
  413. amdgpu_ring_write(ring, VCN_ENC_CMD_END);
  414. amdgpu_ring_commit(ring);
  415. for (i = 0; i < adev->usec_timeout; i++) {
  416. if (amdgpu_ring_get_rptr(ring) != rptr)
  417. break;
  418. DRM_UDELAY(1);
  419. }
  420. if (i < adev->usec_timeout) {
  421. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  422. ring->idx, i);
  423. } else {
  424. DRM_ERROR("amdgpu: ring %d test failed\n",
  425. ring->idx);
  426. r = -ETIMEDOUT;
  427. }
  428. return r;
  429. }
  430. static int amdgpu_vcn_enc_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  431. struct dma_fence **fence)
  432. {
  433. const unsigned ib_size_dw = 16;
  434. struct amdgpu_job *job;
  435. struct amdgpu_ib *ib;
  436. struct dma_fence *f = NULL;
  437. uint64_t dummy;
  438. int i, r;
  439. r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
  440. if (r)
  441. return r;
  442. ib = &job->ibs[0];
  443. dummy = ib->gpu_addr + 1024;
  444. ib->length_dw = 0;
  445. ib->ptr[ib->length_dw++] = 0x00000018;
  446. ib->ptr[ib->length_dw++] = 0x00000001; /* session info */
  447. ib->ptr[ib->length_dw++] = handle;
  448. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  449. ib->ptr[ib->length_dw++] = dummy;
  450. ib->ptr[ib->length_dw++] = 0x0000000b;
  451. ib->ptr[ib->length_dw++] = 0x00000014;
  452. ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
  453. ib->ptr[ib->length_dw++] = 0x0000001c;
  454. ib->ptr[ib->length_dw++] = 0x00000000;
  455. ib->ptr[ib->length_dw++] = 0x00000000;
  456. ib->ptr[ib->length_dw++] = 0x00000008;
  457. ib->ptr[ib->length_dw++] = 0x08000001; /* op initialize */
  458. for (i = ib->length_dw; i < ib_size_dw; ++i)
  459. ib->ptr[i] = 0x0;
  460. r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
  461. job->fence = dma_fence_get(f);
  462. if (r)
  463. goto err;
  464. amdgpu_job_free(job);
  465. if (fence)
  466. *fence = dma_fence_get(f);
  467. dma_fence_put(f);
  468. return 0;
  469. err:
  470. amdgpu_job_free(job);
  471. return r;
  472. }
  473. static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  474. struct dma_fence **fence)
  475. {
  476. const unsigned ib_size_dw = 16;
  477. struct amdgpu_job *job;
  478. struct amdgpu_ib *ib;
  479. struct dma_fence *f = NULL;
  480. uint64_t dummy;
  481. int i, r;
  482. r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
  483. if (r)
  484. return r;
  485. ib = &job->ibs[0];
  486. dummy = ib->gpu_addr + 1024;
  487. ib->length_dw = 0;
  488. ib->ptr[ib->length_dw++] = 0x00000018;
  489. ib->ptr[ib->length_dw++] = 0x00000001;
  490. ib->ptr[ib->length_dw++] = handle;
  491. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  492. ib->ptr[ib->length_dw++] = dummy;
  493. ib->ptr[ib->length_dw++] = 0x0000000b;
  494. ib->ptr[ib->length_dw++] = 0x00000014;
  495. ib->ptr[ib->length_dw++] = 0x00000002;
  496. ib->ptr[ib->length_dw++] = 0x0000001c;
  497. ib->ptr[ib->length_dw++] = 0x00000000;
  498. ib->ptr[ib->length_dw++] = 0x00000000;
  499. ib->ptr[ib->length_dw++] = 0x00000008;
  500. ib->ptr[ib->length_dw++] = 0x08000002; /* op close session */
  501. for (i = ib->length_dw; i < ib_size_dw; ++i)
  502. ib->ptr[i] = 0x0;
  503. r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
  504. job->fence = dma_fence_get(f);
  505. if (r)
  506. goto err;
  507. amdgpu_job_free(job);
  508. if (fence)
  509. *fence = dma_fence_get(f);
  510. dma_fence_put(f);
  511. return 0;
  512. err:
  513. amdgpu_job_free(job);
  514. return r;
  515. }
  516. int amdgpu_vcn_enc_ring_test_ib(struct amdgpu_ring *ring, long timeout)
  517. {
  518. struct dma_fence *fence = NULL;
  519. long r;
  520. r = amdgpu_vcn_enc_get_create_msg(ring, 1, NULL);
  521. if (r) {
  522. DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
  523. goto error;
  524. }
  525. r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, &fence);
  526. if (r) {
  527. DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
  528. goto error;
  529. }
  530. r = dma_fence_wait_timeout(fence, false, timeout);
  531. if (r == 0) {
  532. DRM_ERROR("amdgpu: IB test timed out.\n");
  533. r = -ETIMEDOUT;
  534. } else if (r < 0) {
  535. DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
  536. } else {
  537. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  538. r = 0;
  539. }
  540. error:
  541. dma_fence_put(fence);
  542. return r;
  543. }