amdgpu_vcn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. family_id = le32_to_cpu(hdr->ucode_version) & 0xff;
  76. version_major = (le32_to_cpu(hdr->ucode_version) >> 24) & 0xff;
  77. version_minor = (le32_to_cpu(hdr->ucode_version) >> 8) & 0xff;
  78. DRM_INFO("Found VCN firmware Version: %hu.%hu Family ID: %hu\n",
  79. version_major, version_minor, family_id);
  80. bo_size = AMDGPU_GPU_PAGE_ALIGN(le32_to_cpu(hdr->ucode_size_bytes) + 8)
  81. + AMDGPU_VCN_STACK_SIZE + AMDGPU_VCN_HEAP_SIZE
  82. + AMDGPU_VCN_SESSION_SIZE * 40;
  83. r = amdgpu_bo_create_kernel(adev, bo_size, PAGE_SIZE,
  84. AMDGPU_GEM_DOMAIN_VRAM, &adev->vcn.vcpu_bo,
  85. &adev->vcn.gpu_addr, &adev->vcn.cpu_addr);
  86. if (r) {
  87. dev_err(adev->dev, "(%d) failed to allocate vcn bo\n", r);
  88. return r;
  89. }
  90. ring = &adev->vcn.ring_dec;
  91. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
  92. r = amd_sched_entity_init(&ring->sched, &adev->vcn.entity_dec,
  93. rq, amdgpu_sched_jobs);
  94. if (r != 0) {
  95. DRM_ERROR("Failed setting up VCN dec run queue.\n");
  96. return r;
  97. }
  98. ring = &adev->vcn.ring_enc[0];
  99. rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_NORMAL];
  100. r = amd_sched_entity_init(&ring->sched, &adev->vcn.entity_enc,
  101. rq, amdgpu_sched_jobs);
  102. if (r != 0) {
  103. DRM_ERROR("Failed setting up VCN enc run queue.\n");
  104. return r;
  105. }
  106. return 0;
  107. }
  108. int amdgpu_vcn_sw_fini(struct amdgpu_device *adev)
  109. {
  110. kfree(adev->vcn.saved_bo);
  111. amd_sched_entity_fini(&adev->vcn.ring_dec.sched, &adev->vcn.entity_dec);
  112. amd_sched_entity_fini(&adev->vcn.ring_enc[0].sched, &adev->vcn.entity_enc);
  113. amdgpu_bo_free_kernel(&adev->vcn.vcpu_bo,
  114. &adev->vcn.gpu_addr,
  115. (void **)&adev->vcn.cpu_addr);
  116. amdgpu_ring_fini(&adev->vcn.ring_dec);
  117. release_firmware(adev->vcn.fw);
  118. return 0;
  119. }
  120. int amdgpu_vcn_suspend(struct amdgpu_device *adev)
  121. {
  122. unsigned size;
  123. void *ptr;
  124. if (adev->vcn.vcpu_bo == NULL)
  125. return 0;
  126. cancel_delayed_work_sync(&adev->vcn.idle_work);
  127. size = amdgpu_bo_size(adev->vcn.vcpu_bo);
  128. ptr = adev->vcn.cpu_addr;
  129. adev->vcn.saved_bo = kmalloc(size, GFP_KERNEL);
  130. if (!adev->vcn.saved_bo)
  131. return -ENOMEM;
  132. memcpy_fromio(adev->vcn.saved_bo, ptr, size);
  133. return 0;
  134. }
  135. int amdgpu_vcn_resume(struct amdgpu_device *adev)
  136. {
  137. unsigned size;
  138. void *ptr;
  139. if (adev->vcn.vcpu_bo == NULL)
  140. return -EINVAL;
  141. size = amdgpu_bo_size(adev->vcn.vcpu_bo);
  142. ptr = adev->vcn.cpu_addr;
  143. if (adev->vcn.saved_bo != NULL) {
  144. memcpy_toio(ptr, adev->vcn.saved_bo, size);
  145. kfree(adev->vcn.saved_bo);
  146. adev->vcn.saved_bo = NULL;
  147. } else {
  148. const struct common_firmware_header *hdr;
  149. unsigned offset;
  150. hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
  151. offset = le32_to_cpu(hdr->ucode_array_offset_bytes);
  152. memcpy_toio(adev->vcn.cpu_addr, adev->vcn.fw->data + offset,
  153. le32_to_cpu(hdr->ucode_size_bytes));
  154. size -= le32_to_cpu(hdr->ucode_size_bytes);
  155. ptr += le32_to_cpu(hdr->ucode_size_bytes);
  156. memset_io(ptr, 0, size);
  157. }
  158. return 0;
  159. }
  160. static void amdgpu_vcn_idle_work_handler(struct work_struct *work)
  161. {
  162. struct amdgpu_device *adev =
  163. container_of(work, struct amdgpu_device, vcn.idle_work.work);
  164. unsigned fences = amdgpu_fence_count_emitted(&adev->vcn.ring_dec);
  165. if (fences == 0) {
  166. if (adev->pm.dpm_enabled) {
  167. amdgpu_dpm_enable_uvd(adev, false);
  168. } else {
  169. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  170. }
  171. } else {
  172. schedule_delayed_work(&adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
  173. }
  174. }
  175. void amdgpu_vcn_ring_begin_use(struct amdgpu_ring *ring)
  176. {
  177. struct amdgpu_device *adev = ring->adev;
  178. bool set_clocks = !cancel_delayed_work_sync(&adev->vcn.idle_work);
  179. if (set_clocks) {
  180. if (adev->pm.dpm_enabled) {
  181. amdgpu_dpm_enable_uvd(adev, true);
  182. } else {
  183. amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  184. }
  185. }
  186. }
  187. void amdgpu_vcn_ring_end_use(struct amdgpu_ring *ring)
  188. {
  189. schedule_delayed_work(&ring->adev->vcn.idle_work, VCN_IDLE_TIMEOUT);
  190. }
  191. int amdgpu_vcn_dec_ring_test_ring(struct amdgpu_ring *ring)
  192. {
  193. struct amdgpu_device *adev = ring->adev;
  194. uint32_t tmp = 0;
  195. unsigned i;
  196. int r;
  197. WREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_CONTEXT_ID), 0xCAFEDEAD);
  198. r = amdgpu_ring_alloc(ring, 3);
  199. if (r) {
  200. DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
  201. ring->idx, r);
  202. return r;
  203. }
  204. amdgpu_ring_write(ring,
  205. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_CONTEXT_ID), 0));
  206. amdgpu_ring_write(ring, 0xDEADBEEF);
  207. amdgpu_ring_commit(ring);
  208. for (i = 0; i < adev->usec_timeout; i++) {
  209. tmp = RREG32(SOC15_REG_OFFSET(UVD, 0, mmUVD_CONTEXT_ID));
  210. if (tmp == 0xDEADBEEF)
  211. break;
  212. DRM_UDELAY(1);
  213. }
  214. if (i < adev->usec_timeout) {
  215. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  216. ring->idx, i);
  217. } else {
  218. DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
  219. ring->idx, tmp);
  220. r = -EINVAL;
  221. }
  222. return r;
  223. }
  224. static int amdgpu_vcn_dec_send_msg(struct amdgpu_ring *ring, struct amdgpu_bo *bo,
  225. bool direct, struct dma_fence **fence)
  226. {
  227. struct ttm_validate_buffer tv;
  228. struct ww_acquire_ctx ticket;
  229. struct list_head head;
  230. struct amdgpu_job *job;
  231. struct amdgpu_ib *ib;
  232. struct dma_fence *f = NULL;
  233. struct amdgpu_device *adev = ring->adev;
  234. uint64_t addr;
  235. int i, r;
  236. memset(&tv, 0, sizeof(tv));
  237. tv.bo = &bo->tbo;
  238. INIT_LIST_HEAD(&head);
  239. list_add(&tv.head, &head);
  240. r = ttm_eu_reserve_buffers(&ticket, &head, true, NULL);
  241. if (r)
  242. return r;
  243. r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
  244. if (r)
  245. goto err;
  246. r = amdgpu_job_alloc_with_ib(adev, 64, &job);
  247. if (r)
  248. goto err;
  249. ib = &job->ibs[0];
  250. addr = amdgpu_bo_gpu_offset(bo);
  251. ib->ptr[0] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA0), 0);
  252. ib->ptr[1] = addr;
  253. ib->ptr[2] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA1), 0);
  254. ib->ptr[3] = addr >> 32;
  255. ib->ptr[4] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0);
  256. ib->ptr[5] = 0;
  257. for (i = 6; i < 16; i += 2) {
  258. ib->ptr[i] = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_NO_OP), 0);
  259. ib->ptr[i+1] = 0;
  260. }
  261. ib->length_dw = 16;
  262. if (direct) {
  263. r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
  264. job->fence = dma_fence_get(f);
  265. if (r)
  266. goto err_free;
  267. amdgpu_job_free(job);
  268. } else {
  269. r = amdgpu_job_submit(job, ring, &adev->vcn.entity_dec,
  270. AMDGPU_FENCE_OWNER_UNDEFINED, &f);
  271. if (r)
  272. goto err_free;
  273. }
  274. ttm_eu_fence_buffer_objects(&ticket, &head, f);
  275. if (fence)
  276. *fence = dma_fence_get(f);
  277. amdgpu_bo_unref(&bo);
  278. dma_fence_put(f);
  279. return 0;
  280. err_free:
  281. amdgpu_job_free(job);
  282. err:
  283. ttm_eu_backoff_reservation(&ticket, &head);
  284. return r;
  285. }
  286. static int amdgpu_vcn_dec_get_create_msg(struct amdgpu_ring *ring, uint32_t handle,
  287. struct dma_fence **fence)
  288. {
  289. struct amdgpu_device *adev = ring->adev;
  290. struct amdgpu_bo *bo;
  291. uint32_t *msg;
  292. int r, i;
  293. r = amdgpu_bo_create(adev, 1024, PAGE_SIZE, true,
  294. AMDGPU_GEM_DOMAIN_VRAM,
  295. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  296. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
  297. NULL, NULL, &bo);
  298. if (r)
  299. return r;
  300. r = amdgpu_bo_reserve(bo, false);
  301. if (r) {
  302. amdgpu_bo_unref(&bo);
  303. return r;
  304. }
  305. r = amdgpu_bo_kmap(bo, (void **)&msg);
  306. if (r) {
  307. amdgpu_bo_unreserve(bo);
  308. amdgpu_bo_unref(&bo);
  309. return r;
  310. }
  311. msg[0] = cpu_to_le32(0x00000028);
  312. msg[1] = cpu_to_le32(0x0000004c);
  313. msg[2] = cpu_to_le32(0x00000001);
  314. msg[3] = cpu_to_le32(0x00000000);
  315. msg[4] = cpu_to_le32(handle);
  316. msg[5] = cpu_to_le32(0x00000000);
  317. msg[6] = cpu_to_le32(0x00000001);
  318. msg[7] = cpu_to_le32(0x00000028);
  319. msg[8] = cpu_to_le32(0x00000024);
  320. msg[9] = cpu_to_le32(0x00000000);
  321. msg[10] = cpu_to_le32(0x00000007);
  322. msg[11] = cpu_to_le32(0x00000000);
  323. msg[12] = cpu_to_le32(0x00000000);
  324. msg[13] = cpu_to_le32(0x00000780);
  325. msg[14] = cpu_to_le32(0x00000440);
  326. msg[15] = cpu_to_le32(0x00000000);
  327. msg[16] = cpu_to_le32(0x01b37000);
  328. msg[17] = cpu_to_le32(0x00000000);
  329. msg[18] = cpu_to_le32(0x00000000);
  330. for (i = 19; 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, &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, VCE_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 = 1024;
  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. /* stitch together an VCN enc create msg */
  445. ib->length_dw = 0;
  446. ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
  447. ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
  448. ib->ptr[ib->length_dw++] = handle;
  449. ib->ptr[ib->length_dw++] = 0x00000040; /* len */
  450. ib->ptr[ib->length_dw++] = 0x01000001; /* create cmd */
  451. ib->ptr[ib->length_dw++] = 0x00000000;
  452. ib->ptr[ib->length_dw++] = 0x00000042;
  453. ib->ptr[ib->length_dw++] = 0x0000000a;
  454. ib->ptr[ib->length_dw++] = 0x00000001;
  455. ib->ptr[ib->length_dw++] = 0x00000080;
  456. ib->ptr[ib->length_dw++] = 0x00000060;
  457. ib->ptr[ib->length_dw++] = 0x00000100;
  458. ib->ptr[ib->length_dw++] = 0x00000100;
  459. ib->ptr[ib->length_dw++] = 0x0000000c;
  460. ib->ptr[ib->length_dw++] = 0x00000000;
  461. ib->ptr[ib->length_dw++] = 0x00000000;
  462. ib->ptr[ib->length_dw++] = 0x00000000;
  463. ib->ptr[ib->length_dw++] = 0x00000000;
  464. ib->ptr[ib->length_dw++] = 0x00000000;
  465. ib->ptr[ib->length_dw++] = 0x00000014; /* len */
  466. ib->ptr[ib->length_dw++] = 0x05000005; /* feedback buffer */
  467. ib->ptr[ib->length_dw++] = upper_32_bits(dummy);
  468. ib->ptr[ib->length_dw++] = dummy;
  469. ib->ptr[ib->length_dw++] = 0x00000001;
  470. for (i = ib->length_dw; i < ib_size_dw; ++i)
  471. ib->ptr[i] = 0x0;
  472. r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
  473. job->fence = dma_fence_get(f);
  474. if (r)
  475. goto err;
  476. amdgpu_job_free(job);
  477. if (fence)
  478. *fence = dma_fence_get(f);
  479. dma_fence_put(f);
  480. return 0;
  481. err:
  482. amdgpu_job_free(job);
  483. return r;
  484. }
  485. static int amdgpu_vcn_enc_get_destroy_msg(struct amdgpu_ring *ring, uint32_t handle,
  486. bool direct, struct dma_fence **fence)
  487. {
  488. const unsigned ib_size_dw = 1024;
  489. struct amdgpu_job *job;
  490. struct amdgpu_ib *ib;
  491. struct dma_fence *f = NULL;
  492. int i, r;
  493. r = amdgpu_job_alloc_with_ib(ring->adev, ib_size_dw * 4, &job);
  494. if (r)
  495. return r;
  496. ib = &job->ibs[0];
  497. /* stitch together an VCN enc destroy msg */
  498. ib->length_dw = 0;
  499. ib->ptr[ib->length_dw++] = 0x0000000c; /* len */
  500. ib->ptr[ib->length_dw++] = 0x00000001; /* session cmd */
  501. ib->ptr[ib->length_dw++] = handle;
  502. ib->ptr[ib->length_dw++] = 0x00000020; /* len */
  503. ib->ptr[ib->length_dw++] = 0x00000002; /* task info */
  504. ib->ptr[ib->length_dw++] = 0xffffffff; /* next task info, set to 0xffffffff if no */
  505. ib->ptr[ib->length_dw++] = 0x00000001; /* destroy session */
  506. ib->ptr[ib->length_dw++] = 0x00000000;
  507. ib->ptr[ib->length_dw++] = 0x00000000;
  508. ib->ptr[ib->length_dw++] = 0xffffffff; /* feedback is not needed, set to 0xffffffff and firmware will not output feedback */
  509. ib->ptr[ib->length_dw++] = 0x00000000;
  510. ib->ptr[ib->length_dw++] = 0x00000008; /* len */
  511. ib->ptr[ib->length_dw++] = 0x02000001; /* destroy cmd */
  512. for (i = ib->length_dw; i < ib_size_dw; ++i)
  513. ib->ptr[i] = 0x0;
  514. if (direct) {
  515. r = amdgpu_ib_schedule(ring, 1, ib, NULL, &f);
  516. job->fence = dma_fence_get(f);
  517. if (r)
  518. goto err;
  519. amdgpu_job_free(job);
  520. } else {
  521. r = amdgpu_job_submit(job, ring, &ring->adev->vcn.entity_enc,
  522. AMDGPU_FENCE_OWNER_UNDEFINED, &f);
  523. if (r)
  524. goto err;
  525. }
  526. if (fence)
  527. *fence = dma_fence_get(f);
  528. dma_fence_put(f);
  529. return 0;
  530. err:
  531. amdgpu_job_free(job);
  532. return r;
  533. }
  534. int amdgpu_vcn_enc_ring_test_ib(struct amdgpu_ring *ring, long timeout)
  535. {
  536. struct dma_fence *fence = NULL;
  537. long r;
  538. r = amdgpu_vcn_enc_get_create_msg(ring, 1, NULL);
  539. if (r) {
  540. DRM_ERROR("amdgpu: failed to get create msg (%ld).\n", r);
  541. goto error;
  542. }
  543. r = amdgpu_vcn_enc_get_destroy_msg(ring, 1, true, &fence);
  544. if (r) {
  545. DRM_ERROR("amdgpu: failed to get destroy ib (%ld).\n", r);
  546. goto error;
  547. }
  548. r = dma_fence_wait_timeout(fence, false, timeout);
  549. if (r == 0) {
  550. DRM_ERROR("amdgpu: IB test timed out.\n");
  551. r = -ETIMEDOUT;
  552. } else if (r < 0) {
  553. DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
  554. } else {
  555. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  556. r = 0;
  557. }
  558. error:
  559. dma_fence_put(fence);
  560. return r;
  561. }