uvd_v4_2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. /*
  2. * Copyright 2013 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: Christian König <christian.koenig@amd.com>
  23. */
  24. #include <linux/firmware.h>
  25. #include <drm/drmP.h>
  26. #include "amdgpu.h"
  27. #include "amdgpu_uvd.h"
  28. #include "cikd.h"
  29. #include "uvd/uvd_4_2_d.h"
  30. #include "uvd/uvd_4_2_sh_mask.h"
  31. #include "oss/oss_2_0_d.h"
  32. #include "oss/oss_2_0_sh_mask.h"
  33. static void uvd_v4_2_mc_resume(struct amdgpu_device *adev);
  34. static void uvd_v4_2_init_cg(struct amdgpu_device *adev);
  35. static void uvd_v4_2_set_ring_funcs(struct amdgpu_device *adev);
  36. static void uvd_v4_2_set_irq_funcs(struct amdgpu_device *adev);
  37. static int uvd_v4_2_start(struct amdgpu_device *adev);
  38. static void uvd_v4_2_stop(struct amdgpu_device *adev);
  39. /**
  40. * uvd_v4_2_ring_get_rptr - get read pointer
  41. *
  42. * @ring: amdgpu_ring pointer
  43. *
  44. * Returns the current hardware read pointer
  45. */
  46. static uint32_t uvd_v4_2_ring_get_rptr(struct amdgpu_ring *ring)
  47. {
  48. struct amdgpu_device *adev = ring->adev;
  49. return RREG32(mmUVD_RBC_RB_RPTR);
  50. }
  51. /**
  52. * uvd_v4_2_ring_get_wptr - get write pointer
  53. *
  54. * @ring: amdgpu_ring pointer
  55. *
  56. * Returns the current hardware write pointer
  57. */
  58. static uint32_t uvd_v4_2_ring_get_wptr(struct amdgpu_ring *ring)
  59. {
  60. struct amdgpu_device *adev = ring->adev;
  61. return RREG32(mmUVD_RBC_RB_WPTR);
  62. }
  63. /**
  64. * uvd_v4_2_ring_set_wptr - set write pointer
  65. *
  66. * @ring: amdgpu_ring pointer
  67. *
  68. * Commits the write pointer to the hardware
  69. */
  70. static void uvd_v4_2_ring_set_wptr(struct amdgpu_ring *ring)
  71. {
  72. struct amdgpu_device *adev = ring->adev;
  73. WREG32(mmUVD_RBC_RB_WPTR, ring->wptr);
  74. }
  75. static int uvd_v4_2_early_init(void *handle)
  76. {
  77. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  78. uvd_v4_2_set_ring_funcs(adev);
  79. uvd_v4_2_set_irq_funcs(adev);
  80. return 0;
  81. }
  82. static int uvd_v4_2_sw_init(void *handle)
  83. {
  84. struct amdgpu_ring *ring;
  85. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  86. int r;
  87. /* UVD TRAP */
  88. r = amdgpu_irq_add_id(adev, 124, &adev->uvd.irq);
  89. if (r)
  90. return r;
  91. r = amdgpu_uvd_sw_init(adev);
  92. if (r)
  93. return r;
  94. r = amdgpu_uvd_resume(adev);
  95. if (r)
  96. return r;
  97. ring = &adev->uvd.ring;
  98. sprintf(ring->name, "uvd");
  99. r = amdgpu_ring_init(adev, ring, 512, CP_PACKET2, 0xf,
  100. &adev->uvd.irq, 0, AMDGPU_RING_TYPE_UVD);
  101. return r;
  102. }
  103. static int uvd_v4_2_sw_fini(void *handle)
  104. {
  105. int r;
  106. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  107. r = amdgpu_uvd_suspend(adev);
  108. if (r)
  109. return r;
  110. r = amdgpu_uvd_sw_fini(adev);
  111. if (r)
  112. return r;
  113. return r;
  114. }
  115. /**
  116. * uvd_v4_2_hw_init - start and test UVD block
  117. *
  118. * @adev: amdgpu_device pointer
  119. *
  120. * Initialize the hardware, boot up the VCPU and do some testing
  121. */
  122. static int uvd_v4_2_hw_init(void *handle)
  123. {
  124. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  125. struct amdgpu_ring *ring = &adev->uvd.ring;
  126. uint32_t tmp;
  127. int r;
  128. /* raise clocks while booting up the VCPU */
  129. amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  130. r = uvd_v4_2_start(adev);
  131. if (r)
  132. goto done;
  133. ring->ready = true;
  134. r = amdgpu_ring_test_ring(ring);
  135. if (r) {
  136. ring->ready = false;
  137. goto done;
  138. }
  139. r = amdgpu_ring_alloc(ring, 10);
  140. if (r) {
  141. DRM_ERROR("amdgpu: ring failed to lock UVD ring (%d).\n", r);
  142. goto done;
  143. }
  144. tmp = PACKET0(mmUVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL, 0);
  145. amdgpu_ring_write(ring, tmp);
  146. amdgpu_ring_write(ring, 0xFFFFF);
  147. tmp = PACKET0(mmUVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL, 0);
  148. amdgpu_ring_write(ring, tmp);
  149. amdgpu_ring_write(ring, 0xFFFFF);
  150. tmp = PACKET0(mmUVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL, 0);
  151. amdgpu_ring_write(ring, tmp);
  152. amdgpu_ring_write(ring, 0xFFFFF);
  153. /* Clear timeout status bits */
  154. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_TIMEOUT_STATUS, 0));
  155. amdgpu_ring_write(ring, 0x8);
  156. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_CNTL, 0));
  157. amdgpu_ring_write(ring, 3);
  158. amdgpu_ring_commit(ring);
  159. done:
  160. /* lower clocks again */
  161. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  162. if (!r)
  163. DRM_INFO("UVD initialized successfully.\n");
  164. return r;
  165. }
  166. /**
  167. * uvd_v4_2_hw_fini - stop the hardware block
  168. *
  169. * @adev: amdgpu_device pointer
  170. *
  171. * Stop the UVD block, mark ring as not ready any more
  172. */
  173. static int uvd_v4_2_hw_fini(void *handle)
  174. {
  175. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  176. struct amdgpu_ring *ring = &adev->uvd.ring;
  177. uvd_v4_2_stop(adev);
  178. ring->ready = false;
  179. return 0;
  180. }
  181. static int uvd_v4_2_suspend(void *handle)
  182. {
  183. int r;
  184. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  185. r = uvd_v4_2_hw_fini(adev);
  186. if (r)
  187. return r;
  188. r = amdgpu_uvd_suspend(adev);
  189. if (r)
  190. return r;
  191. return r;
  192. }
  193. static int uvd_v4_2_resume(void *handle)
  194. {
  195. int r;
  196. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  197. r = amdgpu_uvd_resume(adev);
  198. if (r)
  199. return r;
  200. r = uvd_v4_2_hw_init(adev);
  201. if (r)
  202. return r;
  203. return r;
  204. }
  205. /**
  206. * uvd_v4_2_start - start UVD block
  207. *
  208. * @adev: amdgpu_device pointer
  209. *
  210. * Setup and start the UVD block
  211. */
  212. static int uvd_v4_2_start(struct amdgpu_device *adev)
  213. {
  214. struct amdgpu_ring *ring = &adev->uvd.ring;
  215. uint32_t rb_bufsz;
  216. int i, j, r;
  217. /* disable byte swapping */
  218. u32 lmi_swap_cntl = 0;
  219. u32 mp_swap_cntl = 0;
  220. uvd_v4_2_mc_resume(adev);
  221. /* disable clock gating */
  222. WREG32(mmUVD_CGC_GATE, 0);
  223. /* disable interupt */
  224. WREG32_P(mmUVD_MASTINT_EN, 0, ~(1 << 1));
  225. /* Stall UMC and register bus before resetting VCPU */
  226. WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
  227. mdelay(1);
  228. /* put LMI, VCPU, RBC etc... into reset */
  229. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__LMI_SOFT_RESET_MASK |
  230. UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK | UVD_SOFT_RESET__LBSI_SOFT_RESET_MASK |
  231. UVD_SOFT_RESET__RBC_SOFT_RESET_MASK | UVD_SOFT_RESET__CSM_SOFT_RESET_MASK |
  232. UVD_SOFT_RESET__CXW_SOFT_RESET_MASK | UVD_SOFT_RESET__TAP_SOFT_RESET_MASK |
  233. UVD_SOFT_RESET__LMI_UMC_SOFT_RESET_MASK);
  234. mdelay(5);
  235. /* take UVD block out of reset */
  236. WREG32_P(mmSRBM_SOFT_RESET, 0, ~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
  237. mdelay(5);
  238. /* initialize UVD memory controller */
  239. WREG32(mmUVD_LMI_CTRL, 0x40 | (1 << 8) | (1 << 13) |
  240. (1 << 21) | (1 << 9) | (1 << 20));
  241. #ifdef __BIG_ENDIAN
  242. /* swap (8 in 32) RB and IB */
  243. lmi_swap_cntl = 0xa;
  244. mp_swap_cntl = 0;
  245. #endif
  246. WREG32(mmUVD_LMI_SWAP_CNTL, lmi_swap_cntl);
  247. WREG32(mmUVD_MP_SWAP_CNTL, mp_swap_cntl);
  248. WREG32(mmUVD_MPC_SET_MUXA0, 0x40c2040);
  249. WREG32(mmUVD_MPC_SET_MUXA1, 0x0);
  250. WREG32(mmUVD_MPC_SET_MUXB0, 0x40c2040);
  251. WREG32(mmUVD_MPC_SET_MUXB1, 0x0);
  252. WREG32(mmUVD_MPC_SET_ALU, 0);
  253. WREG32(mmUVD_MPC_SET_MUX, 0x88);
  254. /* take all subblocks out of reset, except VCPU */
  255. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  256. mdelay(5);
  257. /* enable VCPU clock */
  258. WREG32(mmUVD_VCPU_CNTL, 1 << 9);
  259. /* enable UMC */
  260. WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
  261. /* boot up the VCPU */
  262. WREG32(mmUVD_SOFT_RESET, 0);
  263. mdelay(10);
  264. for (i = 0; i < 10; ++i) {
  265. uint32_t status;
  266. for (j = 0; j < 100; ++j) {
  267. status = RREG32(mmUVD_STATUS);
  268. if (status & 2)
  269. break;
  270. mdelay(10);
  271. }
  272. r = 0;
  273. if (status & 2)
  274. break;
  275. DRM_ERROR("UVD not responding, trying to reset the VCPU!!!\n");
  276. WREG32_P(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK,
  277. ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  278. mdelay(10);
  279. WREG32_P(mmUVD_SOFT_RESET, 0, ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  280. mdelay(10);
  281. r = -1;
  282. }
  283. if (r) {
  284. DRM_ERROR("UVD not responding, giving up!!!\n");
  285. return r;
  286. }
  287. /* enable interupt */
  288. WREG32_P(mmUVD_MASTINT_EN, 3<<1, ~(3 << 1));
  289. /* force RBC into idle state */
  290. WREG32(mmUVD_RBC_RB_CNTL, 0x11010101);
  291. /* Set the write pointer delay */
  292. WREG32(mmUVD_RBC_RB_WPTR_CNTL, 0);
  293. /* programm the 4GB memory segment for rptr and ring buffer */
  294. WREG32(mmUVD_LMI_EXT40_ADDR, upper_32_bits(ring->gpu_addr) |
  295. (0x7 << 16) | (0x1 << 31));
  296. /* Initialize the ring buffer's read and write pointers */
  297. WREG32(mmUVD_RBC_RB_RPTR, 0x0);
  298. ring->wptr = RREG32(mmUVD_RBC_RB_RPTR);
  299. WREG32(mmUVD_RBC_RB_WPTR, ring->wptr);
  300. /* set the ring address */
  301. WREG32(mmUVD_RBC_RB_BASE, ring->gpu_addr);
  302. /* Set ring buffer size */
  303. rb_bufsz = order_base_2(ring->ring_size);
  304. rb_bufsz = (0x1 << 8) | rb_bufsz;
  305. WREG32_P(mmUVD_RBC_RB_CNTL, rb_bufsz, ~0x11f1f);
  306. return 0;
  307. }
  308. /**
  309. * uvd_v4_2_stop - stop UVD block
  310. *
  311. * @adev: amdgpu_device pointer
  312. *
  313. * stop the UVD block
  314. */
  315. static void uvd_v4_2_stop(struct amdgpu_device *adev)
  316. {
  317. /* force RBC into idle state */
  318. WREG32(mmUVD_RBC_RB_CNTL, 0x11010101);
  319. /* Stall UMC and register bus before resetting VCPU */
  320. WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
  321. mdelay(1);
  322. /* put VCPU into reset */
  323. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  324. mdelay(5);
  325. /* disable VCPU clock */
  326. WREG32(mmUVD_VCPU_CNTL, 0x0);
  327. /* Unstall UMC and register bus */
  328. WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
  329. }
  330. /**
  331. * uvd_v4_2_ring_emit_fence - emit an fence & trap command
  332. *
  333. * @ring: amdgpu_ring pointer
  334. * @fence: fence to emit
  335. *
  336. * Write a fence and a trap command to the ring.
  337. */
  338. static void uvd_v4_2_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  339. unsigned flags)
  340. {
  341. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  342. amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
  343. amdgpu_ring_write(ring, seq);
  344. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
  345. amdgpu_ring_write(ring, addr & 0xffffffff);
  346. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
  347. amdgpu_ring_write(ring, upper_32_bits(addr) & 0xff);
  348. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
  349. amdgpu_ring_write(ring, 0);
  350. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
  351. amdgpu_ring_write(ring, 0);
  352. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
  353. amdgpu_ring_write(ring, 0);
  354. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
  355. amdgpu_ring_write(ring, 2);
  356. }
  357. /**
  358. * uvd_v4_2_ring_test_ring - register write test
  359. *
  360. * @ring: amdgpu_ring pointer
  361. *
  362. * Test if we can successfully write to the context register
  363. */
  364. static int uvd_v4_2_ring_test_ring(struct amdgpu_ring *ring)
  365. {
  366. struct amdgpu_device *adev = ring->adev;
  367. uint32_t tmp = 0;
  368. unsigned i;
  369. int r;
  370. WREG32(mmUVD_CONTEXT_ID, 0xCAFEDEAD);
  371. r = amdgpu_ring_alloc(ring, 3);
  372. if (r) {
  373. DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
  374. ring->idx, r);
  375. return r;
  376. }
  377. amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
  378. amdgpu_ring_write(ring, 0xDEADBEEF);
  379. amdgpu_ring_commit(ring);
  380. for (i = 0; i < adev->usec_timeout; i++) {
  381. tmp = RREG32(mmUVD_CONTEXT_ID);
  382. if (tmp == 0xDEADBEEF)
  383. break;
  384. DRM_UDELAY(1);
  385. }
  386. if (i < adev->usec_timeout) {
  387. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  388. ring->idx, i);
  389. } else {
  390. DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
  391. ring->idx, tmp);
  392. r = -EINVAL;
  393. }
  394. return r;
  395. }
  396. /**
  397. * uvd_v4_2_ring_emit_ib - execute indirect buffer
  398. *
  399. * @ring: amdgpu_ring pointer
  400. * @ib: indirect buffer to execute
  401. *
  402. * Write ring commands to execute the indirect buffer
  403. */
  404. static void uvd_v4_2_ring_emit_ib(struct amdgpu_ring *ring,
  405. struct amdgpu_ib *ib)
  406. {
  407. amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_BASE, 0));
  408. amdgpu_ring_write(ring, ib->gpu_addr);
  409. amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_SIZE, 0));
  410. amdgpu_ring_write(ring, ib->length_dw);
  411. }
  412. /**
  413. * uvd_v4_2_ring_test_ib - test ib execution
  414. *
  415. * @ring: amdgpu_ring pointer
  416. *
  417. * Test if we can successfully execute an IB
  418. */
  419. static int uvd_v4_2_ring_test_ib(struct amdgpu_ring *ring)
  420. {
  421. struct amdgpu_device *adev = ring->adev;
  422. struct fence *fence = NULL;
  423. int r;
  424. r = amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  425. if (r) {
  426. DRM_ERROR("amdgpu: failed to raise UVD clocks (%d).\n", r);
  427. return r;
  428. }
  429. r = amdgpu_uvd_get_create_msg(ring, 1, NULL);
  430. if (r) {
  431. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  432. goto error;
  433. }
  434. r = amdgpu_uvd_get_destroy_msg(ring, 1, true, &fence);
  435. if (r) {
  436. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  437. goto error;
  438. }
  439. r = fence_wait(fence, false);
  440. if (r) {
  441. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  442. goto error;
  443. }
  444. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  445. error:
  446. fence_put(fence);
  447. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  448. return r;
  449. }
  450. /**
  451. * uvd_v4_2_mc_resume - memory controller programming
  452. *
  453. * @adev: amdgpu_device pointer
  454. *
  455. * Let the UVD memory controller know it's offsets
  456. */
  457. static void uvd_v4_2_mc_resume(struct amdgpu_device *adev)
  458. {
  459. uint64_t addr;
  460. uint32_t size;
  461. /* programm the VCPU memory controller bits 0-27 */
  462. addr = (adev->uvd.gpu_addr + AMDGPU_UVD_FIRMWARE_OFFSET) >> 3;
  463. size = AMDGPU_GPU_PAGE_ALIGN(adev->uvd.fw->size + 4) >> 3;
  464. WREG32(mmUVD_VCPU_CACHE_OFFSET0, addr);
  465. WREG32(mmUVD_VCPU_CACHE_SIZE0, size);
  466. addr += size;
  467. size = AMDGPU_UVD_HEAP_SIZE >> 3;
  468. WREG32(mmUVD_VCPU_CACHE_OFFSET1, addr);
  469. WREG32(mmUVD_VCPU_CACHE_SIZE1, size);
  470. addr += size;
  471. size = (AMDGPU_UVD_STACK_SIZE +
  472. (AMDGPU_UVD_SESSION_SIZE * adev->uvd.max_handles)) >> 3;
  473. WREG32(mmUVD_VCPU_CACHE_OFFSET2, addr);
  474. WREG32(mmUVD_VCPU_CACHE_SIZE2, size);
  475. /* bits 28-31 */
  476. addr = (adev->uvd.gpu_addr >> 28) & 0xF;
  477. WREG32(mmUVD_LMI_ADDR_EXT, (addr << 12) | (addr << 0));
  478. /* bits 32-39 */
  479. addr = (adev->uvd.gpu_addr >> 32) & 0xFF;
  480. WREG32(mmUVD_LMI_EXT40_ADDR, addr | (0x9 << 16) | (0x1 << 31));
  481. WREG32(mmUVD_UDEC_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
  482. WREG32(mmUVD_UDEC_DB_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
  483. WREG32(mmUVD_UDEC_DBW_ADDR_CONFIG, adev->gfx.config.gb_addr_config);
  484. uvd_v4_2_init_cg(adev);
  485. }
  486. static void uvd_v4_2_enable_mgcg(struct amdgpu_device *adev,
  487. bool enable)
  488. {
  489. u32 orig, data;
  490. if (enable && (adev->cg_flags & AMD_CG_SUPPORT_UVD_MGCG)) {
  491. data = RREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL);
  492. data = 0xfff;
  493. WREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL, data);
  494. orig = data = RREG32(mmUVD_CGC_CTRL);
  495. data |= UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
  496. if (orig != data)
  497. WREG32(mmUVD_CGC_CTRL, data);
  498. } else {
  499. data = RREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL);
  500. data &= ~0xfff;
  501. WREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL, data);
  502. orig = data = RREG32(mmUVD_CGC_CTRL);
  503. data &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
  504. if (orig != data)
  505. WREG32(mmUVD_CGC_CTRL, data);
  506. }
  507. }
  508. static void uvd_v4_2_set_dcm(struct amdgpu_device *adev,
  509. bool sw_mode)
  510. {
  511. u32 tmp, tmp2;
  512. tmp = RREG32(mmUVD_CGC_CTRL);
  513. tmp &= ~(UVD_CGC_CTRL__CLK_OFF_DELAY_MASK | UVD_CGC_CTRL__CLK_GATE_DLY_TIMER_MASK);
  514. tmp |= UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK |
  515. (1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT) |
  516. (4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT);
  517. if (sw_mode) {
  518. tmp &= ~0x7ffff800;
  519. tmp2 = UVD_CGC_CTRL2__DYN_OCLK_RAMP_EN_MASK |
  520. UVD_CGC_CTRL2__DYN_RCLK_RAMP_EN_MASK |
  521. (7 << UVD_CGC_CTRL2__GATER_DIV_ID__SHIFT);
  522. } else {
  523. tmp |= 0x7ffff800;
  524. tmp2 = 0;
  525. }
  526. WREG32(mmUVD_CGC_CTRL, tmp);
  527. WREG32_UVD_CTX(ixUVD_CGC_CTRL2, tmp2);
  528. }
  529. static void uvd_v4_2_init_cg(struct amdgpu_device *adev)
  530. {
  531. bool hw_mode = true;
  532. if (hw_mode) {
  533. uvd_v4_2_set_dcm(adev, false);
  534. } else {
  535. u32 tmp = RREG32(mmUVD_CGC_CTRL);
  536. tmp &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
  537. WREG32(mmUVD_CGC_CTRL, tmp);
  538. }
  539. }
  540. static bool uvd_v4_2_is_idle(void *handle)
  541. {
  542. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  543. return !(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK);
  544. }
  545. static int uvd_v4_2_wait_for_idle(void *handle)
  546. {
  547. unsigned i;
  548. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  549. for (i = 0; i < adev->usec_timeout; i++) {
  550. if (!(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK))
  551. return 0;
  552. }
  553. return -ETIMEDOUT;
  554. }
  555. static int uvd_v4_2_soft_reset(void *handle)
  556. {
  557. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  558. uvd_v4_2_stop(adev);
  559. WREG32_P(mmSRBM_SOFT_RESET, SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK,
  560. ~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
  561. mdelay(5);
  562. return uvd_v4_2_start(adev);
  563. }
  564. static int uvd_v4_2_set_interrupt_state(struct amdgpu_device *adev,
  565. struct amdgpu_irq_src *source,
  566. unsigned type,
  567. enum amdgpu_interrupt_state state)
  568. {
  569. // TODO
  570. return 0;
  571. }
  572. static int uvd_v4_2_process_interrupt(struct amdgpu_device *adev,
  573. struct amdgpu_irq_src *source,
  574. struct amdgpu_iv_entry *entry)
  575. {
  576. DRM_DEBUG("IH: UVD TRAP\n");
  577. amdgpu_fence_process(&adev->uvd.ring);
  578. return 0;
  579. }
  580. static int uvd_v4_2_set_clockgating_state(void *handle,
  581. enum amd_clockgating_state state)
  582. {
  583. bool gate = false;
  584. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  585. if (!(adev->cg_flags & AMD_CG_SUPPORT_UVD_MGCG))
  586. return 0;
  587. if (state == AMD_CG_STATE_GATE)
  588. gate = true;
  589. uvd_v4_2_enable_mgcg(adev, gate);
  590. return 0;
  591. }
  592. static int uvd_v4_2_set_powergating_state(void *handle,
  593. enum amd_powergating_state state)
  594. {
  595. /* This doesn't actually powergate the UVD block.
  596. * That's done in the dpm code via the SMC. This
  597. * just re-inits the block as necessary. The actual
  598. * gating still happens in the dpm code. We should
  599. * revisit this when there is a cleaner line between
  600. * the smc and the hw blocks
  601. */
  602. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  603. if (!(adev->pg_flags & AMD_PG_SUPPORT_UVD))
  604. return 0;
  605. if (state == AMD_PG_STATE_GATE) {
  606. uvd_v4_2_stop(adev);
  607. return 0;
  608. } else {
  609. return uvd_v4_2_start(adev);
  610. }
  611. }
  612. const struct amd_ip_funcs uvd_v4_2_ip_funcs = {
  613. .early_init = uvd_v4_2_early_init,
  614. .late_init = NULL,
  615. .sw_init = uvd_v4_2_sw_init,
  616. .sw_fini = uvd_v4_2_sw_fini,
  617. .hw_init = uvd_v4_2_hw_init,
  618. .hw_fini = uvd_v4_2_hw_fini,
  619. .suspend = uvd_v4_2_suspend,
  620. .resume = uvd_v4_2_resume,
  621. .is_idle = uvd_v4_2_is_idle,
  622. .wait_for_idle = uvd_v4_2_wait_for_idle,
  623. .soft_reset = uvd_v4_2_soft_reset,
  624. .set_clockgating_state = uvd_v4_2_set_clockgating_state,
  625. .set_powergating_state = uvd_v4_2_set_powergating_state,
  626. };
  627. static const struct amdgpu_ring_funcs uvd_v4_2_ring_funcs = {
  628. .get_rptr = uvd_v4_2_ring_get_rptr,
  629. .get_wptr = uvd_v4_2_ring_get_wptr,
  630. .set_wptr = uvd_v4_2_ring_set_wptr,
  631. .parse_cs = amdgpu_uvd_ring_parse_cs,
  632. .emit_ib = uvd_v4_2_ring_emit_ib,
  633. .emit_fence = uvd_v4_2_ring_emit_fence,
  634. .test_ring = uvd_v4_2_ring_test_ring,
  635. .test_ib = uvd_v4_2_ring_test_ib,
  636. .insert_nop = amdgpu_ring_insert_nop,
  637. .pad_ib = amdgpu_ring_generic_pad_ib,
  638. };
  639. static void uvd_v4_2_set_ring_funcs(struct amdgpu_device *adev)
  640. {
  641. adev->uvd.ring.funcs = &uvd_v4_2_ring_funcs;
  642. }
  643. static const struct amdgpu_irq_src_funcs uvd_v4_2_irq_funcs = {
  644. .set = uvd_v4_2_set_interrupt_state,
  645. .process = uvd_v4_2_process_interrupt,
  646. };
  647. static void uvd_v4_2_set_irq_funcs(struct amdgpu_device *adev)
  648. {
  649. adev->uvd.irq.num_types = 1;
  650. adev->uvd.irq.funcs = &uvd_v4_2_irq_funcs;
  651. }