uvd_v4_2.c 20 KB

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