uvd_v4_2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  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. #include "bif/bif_4_1_d.h"
  34. static void uvd_v4_2_mc_resume(struct amdgpu_device *adev);
  35. static void uvd_v4_2_init_cg(struct amdgpu_device *adev);
  36. static void uvd_v4_2_set_ring_funcs(struct amdgpu_device *adev);
  37. static void uvd_v4_2_set_irq_funcs(struct amdgpu_device *adev);
  38. static int uvd_v4_2_start(struct amdgpu_device *adev);
  39. static void uvd_v4_2_stop(struct amdgpu_device *adev);
  40. /**
  41. * uvd_v4_2_ring_get_rptr - get read pointer
  42. *
  43. * @ring: amdgpu_ring pointer
  44. *
  45. * Returns the current hardware read pointer
  46. */
  47. static uint32_t uvd_v4_2_ring_get_rptr(struct amdgpu_ring *ring)
  48. {
  49. struct amdgpu_device *adev = ring->adev;
  50. return RREG32(mmUVD_RBC_RB_RPTR);
  51. }
  52. /**
  53. * uvd_v4_2_ring_get_wptr - get write pointer
  54. *
  55. * @ring: amdgpu_ring pointer
  56. *
  57. * Returns the current hardware write pointer
  58. */
  59. static uint32_t uvd_v4_2_ring_get_wptr(struct amdgpu_ring *ring)
  60. {
  61. struct amdgpu_device *adev = ring->adev;
  62. return RREG32(mmUVD_RBC_RB_WPTR);
  63. }
  64. /**
  65. * uvd_v4_2_ring_set_wptr - set write pointer
  66. *
  67. * @ring: amdgpu_ring pointer
  68. *
  69. * Commits the write pointer to the hardware
  70. */
  71. static void uvd_v4_2_ring_set_wptr(struct amdgpu_ring *ring)
  72. {
  73. struct amdgpu_device *adev = ring->adev;
  74. WREG32(mmUVD_RBC_RB_WPTR, ring->wptr);
  75. }
  76. static int uvd_v4_2_early_init(void *handle)
  77. {
  78. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  79. uvd_v4_2_set_ring_funcs(adev);
  80. uvd_v4_2_set_irq_funcs(adev);
  81. return 0;
  82. }
  83. static int uvd_v4_2_sw_init(void *handle)
  84. {
  85. struct amdgpu_ring *ring;
  86. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  87. int r;
  88. /* UVD TRAP */
  89. r = amdgpu_irq_add_id(adev, 124, &adev->uvd.irq);
  90. if (r)
  91. return r;
  92. r = amdgpu_uvd_sw_init(adev);
  93. if (r)
  94. return r;
  95. r = amdgpu_uvd_resume(adev);
  96. if (r)
  97. return r;
  98. ring = &adev->uvd.ring;
  99. sprintf(ring->name, "uvd");
  100. r = amdgpu_ring_init(adev, ring, 512, PACKET0(mmUVD_NO_OP, 0), 0xf,
  101. &adev->uvd.irq, 0, AMDGPU_RING_TYPE_UVD);
  102. return r;
  103. }
  104. static int uvd_v4_2_sw_fini(void *handle)
  105. {
  106. int r;
  107. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  108. r = amdgpu_uvd_suspend(adev);
  109. if (r)
  110. return r;
  111. r = amdgpu_uvd_sw_fini(adev);
  112. if (r)
  113. return r;
  114. return r;
  115. }
  116. /**
  117. * uvd_v4_2_hw_init - start and test UVD block
  118. *
  119. * @adev: amdgpu_device pointer
  120. *
  121. * Initialize the hardware, boot up the VCPU and do some testing
  122. */
  123. static int uvd_v4_2_hw_init(void *handle)
  124. {
  125. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  126. struct amdgpu_ring *ring = &adev->uvd.ring;
  127. uint32_t tmp;
  128. int r;
  129. /* raise clocks while booting up the VCPU */
  130. amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  131. r = uvd_v4_2_start(adev);
  132. if (r)
  133. goto done;
  134. ring->ready = true;
  135. r = amdgpu_ring_test_ring(ring);
  136. if (r) {
  137. ring->ready = false;
  138. goto done;
  139. }
  140. r = amdgpu_ring_alloc(ring, 10);
  141. if (r) {
  142. DRM_ERROR("amdgpu: ring failed to lock UVD ring (%d).\n", r);
  143. goto done;
  144. }
  145. tmp = PACKET0(mmUVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL, 0);
  146. amdgpu_ring_write(ring, tmp);
  147. amdgpu_ring_write(ring, 0xFFFFF);
  148. tmp = PACKET0(mmUVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL, 0);
  149. amdgpu_ring_write(ring, tmp);
  150. amdgpu_ring_write(ring, 0xFFFFF);
  151. tmp = PACKET0(mmUVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL, 0);
  152. amdgpu_ring_write(ring, tmp);
  153. amdgpu_ring_write(ring, 0xFFFFF);
  154. /* Clear timeout status bits */
  155. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_TIMEOUT_STATUS, 0));
  156. amdgpu_ring_write(ring, 0x8);
  157. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_CNTL, 0));
  158. amdgpu_ring_write(ring, 3);
  159. amdgpu_ring_commit(ring);
  160. done:
  161. /* lower clocks again */
  162. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  163. if (!r)
  164. DRM_INFO("UVD initialized successfully.\n");
  165. return r;
  166. }
  167. /**
  168. * uvd_v4_2_hw_fini - stop the hardware block
  169. *
  170. * @adev: amdgpu_device pointer
  171. *
  172. * Stop the UVD block, mark ring as not ready any more
  173. */
  174. static int uvd_v4_2_hw_fini(void *handle)
  175. {
  176. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  177. struct amdgpu_ring *ring = &adev->uvd.ring;
  178. uvd_v4_2_stop(adev);
  179. ring->ready = false;
  180. return 0;
  181. }
  182. static int uvd_v4_2_suspend(void *handle)
  183. {
  184. int r;
  185. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  186. r = uvd_v4_2_hw_fini(adev);
  187. if (r)
  188. return r;
  189. r = amdgpu_uvd_suspend(adev);
  190. if (r)
  191. return r;
  192. return r;
  193. }
  194. static int uvd_v4_2_resume(void *handle)
  195. {
  196. int r;
  197. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  198. r = amdgpu_uvd_resume(adev);
  199. if (r)
  200. return r;
  201. r = uvd_v4_2_hw_init(adev);
  202. if (r)
  203. return r;
  204. return r;
  205. }
  206. /**
  207. * uvd_v4_2_start - start UVD block
  208. *
  209. * @adev: amdgpu_device pointer
  210. *
  211. * Setup and start the UVD block
  212. */
  213. static int uvd_v4_2_start(struct amdgpu_device *adev)
  214. {
  215. struct amdgpu_ring *ring = &adev->uvd.ring;
  216. uint32_t rb_bufsz;
  217. int i, j, r;
  218. /* disable byte swapping */
  219. u32 lmi_swap_cntl = 0;
  220. u32 mp_swap_cntl = 0;
  221. uvd_v4_2_mc_resume(adev);
  222. /* disable clock gating */
  223. WREG32(mmUVD_CGC_GATE, 0);
  224. /* disable interupt */
  225. WREG32_P(mmUVD_MASTINT_EN, 0, ~(1 << 1));
  226. /* Stall UMC and register bus before resetting VCPU */
  227. WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
  228. mdelay(1);
  229. /* put LMI, VCPU, RBC etc... into reset */
  230. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__LMI_SOFT_RESET_MASK |
  231. UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK | UVD_SOFT_RESET__LBSI_SOFT_RESET_MASK |
  232. UVD_SOFT_RESET__RBC_SOFT_RESET_MASK | UVD_SOFT_RESET__CSM_SOFT_RESET_MASK |
  233. UVD_SOFT_RESET__CXW_SOFT_RESET_MASK | UVD_SOFT_RESET__TAP_SOFT_RESET_MASK |
  234. UVD_SOFT_RESET__LMI_UMC_SOFT_RESET_MASK);
  235. mdelay(5);
  236. /* take UVD block out of reset */
  237. WREG32_P(mmSRBM_SOFT_RESET, 0, ~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
  238. mdelay(5);
  239. /* initialize UVD memory controller */
  240. WREG32(mmUVD_LMI_CTRL, 0x40 | (1 << 8) | (1 << 13) |
  241. (1 << 21) | (1 << 9) | (1 << 20));
  242. #ifdef __BIG_ENDIAN
  243. /* swap (8 in 32) RB and IB */
  244. lmi_swap_cntl = 0xa;
  245. mp_swap_cntl = 0;
  246. #endif
  247. WREG32(mmUVD_LMI_SWAP_CNTL, lmi_swap_cntl);
  248. WREG32(mmUVD_MP_SWAP_CNTL, mp_swap_cntl);
  249. WREG32(mmUVD_MPC_SET_MUXA0, 0x40c2040);
  250. WREG32(mmUVD_MPC_SET_MUXA1, 0x0);
  251. WREG32(mmUVD_MPC_SET_MUXB0, 0x40c2040);
  252. WREG32(mmUVD_MPC_SET_MUXB1, 0x0);
  253. WREG32(mmUVD_MPC_SET_ALU, 0);
  254. WREG32(mmUVD_MPC_SET_MUX, 0x88);
  255. /* take all subblocks out of reset, except VCPU */
  256. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  257. mdelay(5);
  258. /* enable VCPU clock */
  259. WREG32(mmUVD_VCPU_CNTL, 1 << 9);
  260. /* enable UMC */
  261. WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
  262. /* boot up the VCPU */
  263. WREG32(mmUVD_SOFT_RESET, 0);
  264. mdelay(10);
  265. for (i = 0; i < 10; ++i) {
  266. uint32_t status;
  267. for (j = 0; j < 100; ++j) {
  268. status = RREG32(mmUVD_STATUS);
  269. if (status & 2)
  270. break;
  271. mdelay(10);
  272. }
  273. r = 0;
  274. if (status & 2)
  275. break;
  276. DRM_ERROR("UVD not responding, trying to reset the VCPU!!!\n");
  277. WREG32_P(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK,
  278. ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  279. mdelay(10);
  280. WREG32_P(mmUVD_SOFT_RESET, 0, ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  281. mdelay(10);
  282. r = -1;
  283. }
  284. if (r) {
  285. DRM_ERROR("UVD not responding, giving up!!!\n");
  286. return r;
  287. }
  288. /* enable interupt */
  289. WREG32_P(mmUVD_MASTINT_EN, 3<<1, ~(3 << 1));
  290. /* force RBC into idle state */
  291. WREG32(mmUVD_RBC_RB_CNTL, 0x11010101);
  292. /* Set the write pointer delay */
  293. WREG32(mmUVD_RBC_RB_WPTR_CNTL, 0);
  294. /* programm the 4GB memory segment for rptr and ring buffer */
  295. WREG32(mmUVD_LMI_EXT40_ADDR, upper_32_bits(ring->gpu_addr) |
  296. (0x7 << 16) | (0x1 << 31));
  297. /* Initialize the ring buffer's read and write pointers */
  298. WREG32(mmUVD_RBC_RB_RPTR, 0x0);
  299. ring->wptr = RREG32(mmUVD_RBC_RB_RPTR);
  300. WREG32(mmUVD_RBC_RB_WPTR, ring->wptr);
  301. /* set the ring address */
  302. WREG32(mmUVD_RBC_RB_BASE, ring->gpu_addr);
  303. /* Set ring buffer size */
  304. rb_bufsz = order_base_2(ring->ring_size);
  305. rb_bufsz = (0x1 << 8) | rb_bufsz;
  306. WREG32_P(mmUVD_RBC_RB_CNTL, rb_bufsz, ~0x11f1f);
  307. return 0;
  308. }
  309. /**
  310. * uvd_v4_2_stop - stop UVD block
  311. *
  312. * @adev: amdgpu_device pointer
  313. *
  314. * stop the UVD block
  315. */
  316. static void uvd_v4_2_stop(struct amdgpu_device *adev)
  317. {
  318. /* force RBC into idle state */
  319. WREG32(mmUVD_RBC_RB_CNTL, 0x11010101);
  320. /* Stall UMC and register bus before resetting VCPU */
  321. WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
  322. mdelay(1);
  323. /* put VCPU into reset */
  324. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  325. mdelay(5);
  326. /* disable VCPU clock */
  327. WREG32(mmUVD_VCPU_CNTL, 0x0);
  328. /* Unstall UMC and register bus */
  329. WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
  330. }
  331. /**
  332. * uvd_v4_2_ring_emit_fence - emit an fence & trap command
  333. *
  334. * @ring: amdgpu_ring pointer
  335. * @fence: fence to emit
  336. *
  337. * Write a fence and a trap command to the ring.
  338. */
  339. static void uvd_v4_2_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  340. unsigned flags)
  341. {
  342. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  343. amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
  344. amdgpu_ring_write(ring, seq);
  345. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
  346. amdgpu_ring_write(ring, addr & 0xffffffff);
  347. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
  348. amdgpu_ring_write(ring, upper_32_bits(addr) & 0xff);
  349. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
  350. amdgpu_ring_write(ring, 0);
  351. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
  352. amdgpu_ring_write(ring, 0);
  353. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
  354. amdgpu_ring_write(ring, 0);
  355. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
  356. amdgpu_ring_write(ring, 2);
  357. }
  358. /**
  359. * uvd_v4_2_ring_emit_hdp_flush - emit an hdp flush
  360. *
  361. * @ring: amdgpu_ring pointer
  362. *
  363. * Emits an hdp flush.
  364. */
  365. static void uvd_v4_2_ring_emit_hdp_flush(struct amdgpu_ring *ring)
  366. {
  367. amdgpu_ring_write(ring, PACKET0(mmHDP_MEM_COHERENCY_FLUSH_CNTL, 0));
  368. amdgpu_ring_write(ring, 0);
  369. }
  370. /**
  371. * uvd_v4_2_ring_hdp_invalidate - emit an hdp invalidate
  372. *
  373. * @ring: amdgpu_ring pointer
  374. *
  375. * Emits an hdp invalidate.
  376. */
  377. static void uvd_v4_2_ring_emit_hdp_invalidate(struct amdgpu_ring *ring)
  378. {
  379. amdgpu_ring_write(ring, PACKET0(mmHDP_DEBUG0, 0));
  380. amdgpu_ring_write(ring, 1);
  381. }
  382. /**
  383. * uvd_v4_2_ring_test_ring - register write test
  384. *
  385. * @ring: amdgpu_ring pointer
  386. *
  387. * Test if we can successfully write to the context register
  388. */
  389. static int uvd_v4_2_ring_test_ring(struct amdgpu_ring *ring)
  390. {
  391. struct amdgpu_device *adev = ring->adev;
  392. uint32_t tmp = 0;
  393. unsigned i;
  394. int r;
  395. WREG32(mmUVD_CONTEXT_ID, 0xCAFEDEAD);
  396. r = amdgpu_ring_alloc(ring, 3);
  397. if (r) {
  398. DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
  399. ring->idx, r);
  400. return r;
  401. }
  402. amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
  403. amdgpu_ring_write(ring, 0xDEADBEEF);
  404. amdgpu_ring_commit(ring);
  405. for (i = 0; i < adev->usec_timeout; i++) {
  406. tmp = RREG32(mmUVD_CONTEXT_ID);
  407. if (tmp == 0xDEADBEEF)
  408. break;
  409. DRM_UDELAY(1);
  410. }
  411. if (i < adev->usec_timeout) {
  412. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  413. ring->idx, i);
  414. } else {
  415. DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
  416. ring->idx, tmp);
  417. r = -EINVAL;
  418. }
  419. return r;
  420. }
  421. /**
  422. * uvd_v4_2_ring_emit_ib - execute indirect buffer
  423. *
  424. * @ring: amdgpu_ring pointer
  425. * @ib: indirect buffer to execute
  426. *
  427. * Write ring commands to execute the indirect buffer
  428. */
  429. static void uvd_v4_2_ring_emit_ib(struct amdgpu_ring *ring,
  430. struct amdgpu_ib *ib,
  431. unsigned vm_id, bool ctx_switch)
  432. {
  433. amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_BASE, 0));
  434. amdgpu_ring_write(ring, ib->gpu_addr);
  435. amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_SIZE, 0));
  436. amdgpu_ring_write(ring, ib->length_dw);
  437. }
  438. static unsigned uvd_v4_2_ring_get_emit_ib_size(struct amdgpu_ring *ring)
  439. {
  440. return
  441. 4; /* uvd_v4_2_ring_emit_ib */
  442. }
  443. static unsigned uvd_v4_2_ring_get_dma_frame_size(struct amdgpu_ring *ring)
  444. {
  445. return
  446. 2 + /* uvd_v4_2_ring_emit_hdp_flush */
  447. 2 + /* uvd_v4_2_ring_emit_hdp_invalidate */
  448. 14; /* uvd_v4_2_ring_emit_fence x1 no user fence */
  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. .name = "uvd_v4_2",
  614. .early_init = uvd_v4_2_early_init,
  615. .late_init = NULL,
  616. .sw_init = uvd_v4_2_sw_init,
  617. .sw_fini = uvd_v4_2_sw_fini,
  618. .hw_init = uvd_v4_2_hw_init,
  619. .hw_fini = uvd_v4_2_hw_fini,
  620. .suspend = uvd_v4_2_suspend,
  621. .resume = uvd_v4_2_resume,
  622. .is_idle = uvd_v4_2_is_idle,
  623. .wait_for_idle = uvd_v4_2_wait_for_idle,
  624. .soft_reset = uvd_v4_2_soft_reset,
  625. .set_clockgating_state = uvd_v4_2_set_clockgating_state,
  626. .set_powergating_state = uvd_v4_2_set_powergating_state,
  627. };
  628. static const struct amdgpu_ring_funcs uvd_v4_2_ring_funcs = {
  629. .get_rptr = uvd_v4_2_ring_get_rptr,
  630. .get_wptr = uvd_v4_2_ring_get_wptr,
  631. .set_wptr = uvd_v4_2_ring_set_wptr,
  632. .parse_cs = amdgpu_uvd_ring_parse_cs,
  633. .emit_ib = uvd_v4_2_ring_emit_ib,
  634. .emit_fence = uvd_v4_2_ring_emit_fence,
  635. .emit_hdp_flush = uvd_v4_2_ring_emit_hdp_flush,
  636. .emit_hdp_invalidate = uvd_v4_2_ring_emit_hdp_invalidate,
  637. .test_ring = uvd_v4_2_ring_test_ring,
  638. .test_ib = amdgpu_uvd_ring_test_ib,
  639. .insert_nop = amdgpu_ring_insert_nop,
  640. .pad_ib = amdgpu_ring_generic_pad_ib,
  641. .begin_use = amdgpu_uvd_ring_begin_use,
  642. .end_use = amdgpu_uvd_ring_end_use,
  643. .get_emit_ib_size = uvd_v4_2_ring_get_emit_ib_size,
  644. .get_dma_frame_size = uvd_v4_2_ring_get_dma_frame_size,
  645. };
  646. static void uvd_v4_2_set_ring_funcs(struct amdgpu_device *adev)
  647. {
  648. adev->uvd.ring.funcs = &uvd_v4_2_ring_funcs;
  649. }
  650. static const struct amdgpu_irq_src_funcs uvd_v4_2_irq_funcs = {
  651. .set = uvd_v4_2_set_interrupt_state,
  652. .process = uvd_v4_2_process_interrupt,
  653. };
  654. static void uvd_v4_2_set_irq_funcs(struct amdgpu_device *adev)
  655. {
  656. adev->uvd.irq.num_types = 1;
  657. adev->uvd.irq.funcs = &uvd_v4_2_irq_funcs;
  658. }