uvd_v4_2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  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, 4096, 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_lock(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_unlock_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 = amdgpu_uvd_suspend(adev);
  186. if (r)
  187. return r;
  188. r = uvd_v4_2_hw_fini(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_emit_semaphore - emit semaphore command
  359. *
  360. * @ring: amdgpu_ring pointer
  361. * @semaphore: semaphore to emit commands for
  362. * @emit_wait: true if we should emit a wait command
  363. *
  364. * Emit a semaphore command (either wait or signal) to the UVD ring.
  365. */
  366. static bool uvd_v4_2_ring_emit_semaphore(struct amdgpu_ring *ring,
  367. struct amdgpu_semaphore *semaphore,
  368. bool emit_wait)
  369. {
  370. uint64_t addr = semaphore->gpu_addr;
  371. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_ADDR_LOW, 0));
  372. amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF);
  373. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_ADDR_HIGH, 0));
  374. amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF);
  375. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_CMD, 0));
  376. amdgpu_ring_write(ring, 0x80 | (emit_wait ? 1 : 0));
  377. return true;
  378. }
  379. /**
  380. * uvd_v4_2_ring_test_ring - register write test
  381. *
  382. * @ring: amdgpu_ring pointer
  383. *
  384. * Test if we can successfully write to the context register
  385. */
  386. static int uvd_v4_2_ring_test_ring(struct amdgpu_ring *ring)
  387. {
  388. struct amdgpu_device *adev = ring->adev;
  389. uint32_t tmp = 0;
  390. unsigned i;
  391. int r;
  392. WREG32(mmUVD_CONTEXT_ID, 0xCAFEDEAD);
  393. r = amdgpu_ring_lock(ring, 3);
  394. if (r) {
  395. DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
  396. ring->idx, r);
  397. return r;
  398. }
  399. amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
  400. amdgpu_ring_write(ring, 0xDEADBEEF);
  401. amdgpu_ring_unlock_commit(ring);
  402. for (i = 0; i < adev->usec_timeout; i++) {
  403. tmp = RREG32(mmUVD_CONTEXT_ID);
  404. if (tmp == 0xDEADBEEF)
  405. break;
  406. DRM_UDELAY(1);
  407. }
  408. if (i < adev->usec_timeout) {
  409. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  410. ring->idx, i);
  411. } else {
  412. DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
  413. ring->idx, tmp);
  414. r = -EINVAL;
  415. }
  416. return r;
  417. }
  418. /**
  419. * uvd_v4_2_ring_emit_ib - execute indirect buffer
  420. *
  421. * @ring: amdgpu_ring pointer
  422. * @ib: indirect buffer to execute
  423. *
  424. * Write ring commands to execute the indirect buffer
  425. */
  426. static void uvd_v4_2_ring_emit_ib(struct amdgpu_ring *ring,
  427. struct amdgpu_ib *ib)
  428. {
  429. amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_BASE, 0));
  430. amdgpu_ring_write(ring, ib->gpu_addr);
  431. amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_SIZE, 0));
  432. amdgpu_ring_write(ring, ib->length_dw);
  433. }
  434. /**
  435. * uvd_v4_2_ring_test_ib - test ib execution
  436. *
  437. * @ring: amdgpu_ring pointer
  438. *
  439. * Test if we can successfully execute an IB
  440. */
  441. static int uvd_v4_2_ring_test_ib(struct amdgpu_ring *ring)
  442. {
  443. struct amdgpu_device *adev = ring->adev;
  444. struct fence *fence = NULL;
  445. int r;
  446. r = amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  447. if (r) {
  448. DRM_ERROR("amdgpu: failed to raise UVD clocks (%d).\n", r);
  449. return r;
  450. }
  451. r = amdgpu_uvd_get_create_msg(ring, 1, NULL);
  452. if (r) {
  453. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  454. goto error;
  455. }
  456. r = amdgpu_uvd_get_destroy_msg(ring, 1, &fence);
  457. if (r) {
  458. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  459. goto error;
  460. }
  461. r = fence_wait(fence, false);
  462. if (r) {
  463. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  464. goto error;
  465. }
  466. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  467. error:
  468. fence_put(fence);
  469. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  470. return r;
  471. }
  472. /**
  473. * uvd_v4_2_mc_resume - memory controller programming
  474. *
  475. * @adev: amdgpu_device pointer
  476. *
  477. * Let the UVD memory controller know it's offsets
  478. */
  479. static void uvd_v4_2_mc_resume(struct amdgpu_device *adev)
  480. {
  481. uint64_t addr;
  482. uint32_t size;
  483. /* programm the VCPU memory controller bits 0-27 */
  484. addr = (adev->uvd.gpu_addr + AMDGPU_UVD_FIRMWARE_OFFSET) >> 3;
  485. size = AMDGPU_GPU_PAGE_ALIGN(adev->uvd.fw->size + 4) >> 3;
  486. WREG32(mmUVD_VCPU_CACHE_OFFSET0, addr);
  487. WREG32(mmUVD_VCPU_CACHE_SIZE0, size);
  488. addr += size;
  489. size = AMDGPU_UVD_STACK_SIZE >> 3;
  490. WREG32(mmUVD_VCPU_CACHE_OFFSET1, addr);
  491. WREG32(mmUVD_VCPU_CACHE_SIZE1, size);
  492. addr += size;
  493. size = AMDGPU_UVD_HEAP_SIZE >> 3;
  494. WREG32(mmUVD_VCPU_CACHE_OFFSET2, addr);
  495. WREG32(mmUVD_VCPU_CACHE_SIZE2, size);
  496. /* bits 28-31 */
  497. addr = (adev->uvd.gpu_addr >> 28) & 0xF;
  498. WREG32(mmUVD_LMI_ADDR_EXT, (addr << 12) | (addr << 0));
  499. /* bits 32-39 */
  500. addr = (adev->uvd.gpu_addr >> 32) & 0xFF;
  501. WREG32(mmUVD_LMI_EXT40_ADDR, addr | (0x9 << 16) | (0x1 << 31));
  502. uvd_v4_2_init_cg(adev);
  503. }
  504. static void uvd_v4_2_enable_mgcg(struct amdgpu_device *adev,
  505. bool enable)
  506. {
  507. u32 orig, data;
  508. if (enable && (adev->cg_flags & AMDGPU_CG_SUPPORT_UVD_MGCG)) {
  509. data = RREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL);
  510. data = 0xfff;
  511. WREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL, data);
  512. orig = data = RREG32(mmUVD_CGC_CTRL);
  513. data |= UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
  514. if (orig != data)
  515. WREG32(mmUVD_CGC_CTRL, data);
  516. } else {
  517. data = RREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL);
  518. data &= ~0xfff;
  519. WREG32_UVD_CTX(ixUVD_CGC_MEM_CTRL, data);
  520. orig = data = RREG32(mmUVD_CGC_CTRL);
  521. data &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
  522. if (orig != data)
  523. WREG32(mmUVD_CGC_CTRL, data);
  524. }
  525. }
  526. static void uvd_v4_2_set_dcm(struct amdgpu_device *adev,
  527. bool sw_mode)
  528. {
  529. u32 tmp, tmp2;
  530. tmp = RREG32(mmUVD_CGC_CTRL);
  531. tmp &= ~(UVD_CGC_CTRL__CLK_OFF_DELAY_MASK | UVD_CGC_CTRL__CLK_GATE_DLY_TIMER_MASK);
  532. tmp |= UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK |
  533. (1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT) |
  534. (4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT);
  535. if (sw_mode) {
  536. tmp &= ~0x7ffff800;
  537. tmp2 = UVD_CGC_CTRL2__DYN_OCLK_RAMP_EN_MASK |
  538. UVD_CGC_CTRL2__DYN_RCLK_RAMP_EN_MASK |
  539. (7 << UVD_CGC_CTRL2__GATER_DIV_ID__SHIFT);
  540. } else {
  541. tmp |= 0x7ffff800;
  542. tmp2 = 0;
  543. }
  544. WREG32(mmUVD_CGC_CTRL, tmp);
  545. WREG32_UVD_CTX(ixUVD_CGC_CTRL2, tmp2);
  546. }
  547. static void uvd_v4_2_init_cg(struct amdgpu_device *adev)
  548. {
  549. bool hw_mode = true;
  550. if (hw_mode) {
  551. uvd_v4_2_set_dcm(adev, false);
  552. } else {
  553. u32 tmp = RREG32(mmUVD_CGC_CTRL);
  554. tmp &= ~UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
  555. WREG32(mmUVD_CGC_CTRL, tmp);
  556. }
  557. }
  558. static bool uvd_v4_2_is_idle(void *handle)
  559. {
  560. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  561. return !(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK);
  562. }
  563. static int uvd_v4_2_wait_for_idle(void *handle)
  564. {
  565. unsigned i;
  566. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  567. for (i = 0; i < adev->usec_timeout; i++) {
  568. if (!(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK))
  569. return 0;
  570. }
  571. return -ETIMEDOUT;
  572. }
  573. static int uvd_v4_2_soft_reset(void *handle)
  574. {
  575. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  576. uvd_v4_2_stop(adev);
  577. WREG32_P(mmSRBM_SOFT_RESET, SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK,
  578. ~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
  579. mdelay(5);
  580. return uvd_v4_2_start(adev);
  581. }
  582. static void uvd_v4_2_print_status(void *handle)
  583. {
  584. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  585. dev_info(adev->dev, "UVD 4.2 registers\n");
  586. dev_info(adev->dev, " UVD_SEMA_ADDR_LOW=0x%08X\n",
  587. RREG32(mmUVD_SEMA_ADDR_LOW));
  588. dev_info(adev->dev, " UVD_SEMA_ADDR_HIGH=0x%08X\n",
  589. RREG32(mmUVD_SEMA_ADDR_HIGH));
  590. dev_info(adev->dev, " UVD_SEMA_CMD=0x%08X\n",
  591. RREG32(mmUVD_SEMA_CMD));
  592. dev_info(adev->dev, " UVD_GPCOM_VCPU_CMD=0x%08X\n",
  593. RREG32(mmUVD_GPCOM_VCPU_CMD));
  594. dev_info(adev->dev, " UVD_GPCOM_VCPU_DATA0=0x%08X\n",
  595. RREG32(mmUVD_GPCOM_VCPU_DATA0));
  596. dev_info(adev->dev, " UVD_GPCOM_VCPU_DATA1=0x%08X\n",
  597. RREG32(mmUVD_GPCOM_VCPU_DATA1));
  598. dev_info(adev->dev, " UVD_ENGINE_CNTL=0x%08X\n",
  599. RREG32(mmUVD_ENGINE_CNTL));
  600. dev_info(adev->dev, " UVD_UDEC_ADDR_CONFIG=0x%08X\n",
  601. RREG32(mmUVD_UDEC_ADDR_CONFIG));
  602. dev_info(adev->dev, " UVD_UDEC_DB_ADDR_CONFIG=0x%08X\n",
  603. RREG32(mmUVD_UDEC_DB_ADDR_CONFIG));
  604. dev_info(adev->dev, " UVD_UDEC_DBW_ADDR_CONFIG=0x%08X\n",
  605. RREG32(mmUVD_UDEC_DBW_ADDR_CONFIG));
  606. dev_info(adev->dev, " UVD_SEMA_CNTL=0x%08X\n",
  607. RREG32(mmUVD_SEMA_CNTL));
  608. dev_info(adev->dev, " UVD_LMI_EXT40_ADDR=0x%08X\n",
  609. RREG32(mmUVD_LMI_EXT40_ADDR));
  610. dev_info(adev->dev, " UVD_CTX_INDEX=0x%08X\n",
  611. RREG32(mmUVD_CTX_INDEX));
  612. dev_info(adev->dev, " UVD_CTX_DATA=0x%08X\n",
  613. RREG32(mmUVD_CTX_DATA));
  614. dev_info(adev->dev, " UVD_CGC_GATE=0x%08X\n",
  615. RREG32(mmUVD_CGC_GATE));
  616. dev_info(adev->dev, " UVD_CGC_CTRL=0x%08X\n",
  617. RREG32(mmUVD_CGC_CTRL));
  618. dev_info(adev->dev, " UVD_LMI_CTRL2=0x%08X\n",
  619. RREG32(mmUVD_LMI_CTRL2));
  620. dev_info(adev->dev, " UVD_MASTINT_EN=0x%08X\n",
  621. RREG32(mmUVD_MASTINT_EN));
  622. dev_info(adev->dev, " UVD_LMI_ADDR_EXT=0x%08X\n",
  623. RREG32(mmUVD_LMI_ADDR_EXT));
  624. dev_info(adev->dev, " UVD_LMI_CTRL=0x%08X\n",
  625. RREG32(mmUVD_LMI_CTRL));
  626. dev_info(adev->dev, " UVD_LMI_SWAP_CNTL=0x%08X\n",
  627. RREG32(mmUVD_LMI_SWAP_CNTL));
  628. dev_info(adev->dev, " UVD_MP_SWAP_CNTL=0x%08X\n",
  629. RREG32(mmUVD_MP_SWAP_CNTL));
  630. dev_info(adev->dev, " UVD_MPC_SET_MUXA0=0x%08X\n",
  631. RREG32(mmUVD_MPC_SET_MUXA0));
  632. dev_info(adev->dev, " UVD_MPC_SET_MUXA1=0x%08X\n",
  633. RREG32(mmUVD_MPC_SET_MUXA1));
  634. dev_info(adev->dev, " UVD_MPC_SET_MUXB0=0x%08X\n",
  635. RREG32(mmUVD_MPC_SET_MUXB0));
  636. dev_info(adev->dev, " UVD_MPC_SET_MUXB1=0x%08X\n",
  637. RREG32(mmUVD_MPC_SET_MUXB1));
  638. dev_info(adev->dev, " UVD_MPC_SET_MUX=0x%08X\n",
  639. RREG32(mmUVD_MPC_SET_MUX));
  640. dev_info(adev->dev, " UVD_MPC_SET_ALU=0x%08X\n",
  641. RREG32(mmUVD_MPC_SET_ALU));
  642. dev_info(adev->dev, " UVD_VCPU_CACHE_OFFSET0=0x%08X\n",
  643. RREG32(mmUVD_VCPU_CACHE_OFFSET0));
  644. dev_info(adev->dev, " UVD_VCPU_CACHE_SIZE0=0x%08X\n",
  645. RREG32(mmUVD_VCPU_CACHE_SIZE0));
  646. dev_info(adev->dev, " UVD_VCPU_CACHE_OFFSET1=0x%08X\n",
  647. RREG32(mmUVD_VCPU_CACHE_OFFSET1));
  648. dev_info(adev->dev, " UVD_VCPU_CACHE_SIZE1=0x%08X\n",
  649. RREG32(mmUVD_VCPU_CACHE_SIZE1));
  650. dev_info(adev->dev, " UVD_VCPU_CACHE_OFFSET2=0x%08X\n",
  651. RREG32(mmUVD_VCPU_CACHE_OFFSET2));
  652. dev_info(adev->dev, " UVD_VCPU_CACHE_SIZE2=0x%08X\n",
  653. RREG32(mmUVD_VCPU_CACHE_SIZE2));
  654. dev_info(adev->dev, " UVD_VCPU_CNTL=0x%08X\n",
  655. RREG32(mmUVD_VCPU_CNTL));
  656. dev_info(adev->dev, " UVD_SOFT_RESET=0x%08X\n",
  657. RREG32(mmUVD_SOFT_RESET));
  658. dev_info(adev->dev, " UVD_RBC_IB_BASE=0x%08X\n",
  659. RREG32(mmUVD_RBC_IB_BASE));
  660. dev_info(adev->dev, " UVD_RBC_IB_SIZE=0x%08X\n",
  661. RREG32(mmUVD_RBC_IB_SIZE));
  662. dev_info(adev->dev, " UVD_RBC_RB_BASE=0x%08X\n",
  663. RREG32(mmUVD_RBC_RB_BASE));
  664. dev_info(adev->dev, " UVD_RBC_RB_RPTR=0x%08X\n",
  665. RREG32(mmUVD_RBC_RB_RPTR));
  666. dev_info(adev->dev, " UVD_RBC_RB_WPTR=0x%08X\n",
  667. RREG32(mmUVD_RBC_RB_WPTR));
  668. dev_info(adev->dev, " UVD_RBC_RB_WPTR_CNTL=0x%08X\n",
  669. RREG32(mmUVD_RBC_RB_WPTR_CNTL));
  670. dev_info(adev->dev, " UVD_RBC_RB_CNTL=0x%08X\n",
  671. RREG32(mmUVD_RBC_RB_CNTL));
  672. dev_info(adev->dev, " UVD_STATUS=0x%08X\n",
  673. RREG32(mmUVD_STATUS));
  674. dev_info(adev->dev, " UVD_SEMA_TIMEOUT_STATUS=0x%08X\n",
  675. RREG32(mmUVD_SEMA_TIMEOUT_STATUS));
  676. dev_info(adev->dev, " UVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL=0x%08X\n",
  677. RREG32(mmUVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL));
  678. dev_info(adev->dev, " UVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL=0x%08X\n",
  679. RREG32(mmUVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL));
  680. dev_info(adev->dev, " UVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL=0x%08X\n",
  681. RREG32(mmUVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL));
  682. dev_info(adev->dev, " UVD_CONTEXT_ID=0x%08X\n",
  683. RREG32(mmUVD_CONTEXT_ID));
  684. }
  685. static int uvd_v4_2_set_interrupt_state(struct amdgpu_device *adev,
  686. struct amdgpu_irq_src *source,
  687. unsigned type,
  688. enum amdgpu_interrupt_state state)
  689. {
  690. // TODO
  691. return 0;
  692. }
  693. static int uvd_v4_2_process_interrupt(struct amdgpu_device *adev,
  694. struct amdgpu_irq_src *source,
  695. struct amdgpu_iv_entry *entry)
  696. {
  697. DRM_DEBUG("IH: UVD TRAP\n");
  698. amdgpu_fence_process(&adev->uvd.ring);
  699. return 0;
  700. }
  701. static int uvd_v4_2_set_clockgating_state(void *handle,
  702. enum amd_clockgating_state state)
  703. {
  704. bool gate = false;
  705. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  706. if (state == AMD_CG_STATE_GATE)
  707. gate = true;
  708. uvd_v4_2_enable_mgcg(adev, gate);
  709. return 0;
  710. }
  711. static int uvd_v4_2_set_powergating_state(void *handle,
  712. enum amd_powergating_state state)
  713. {
  714. /* This doesn't actually powergate the UVD block.
  715. * That's done in the dpm code via the SMC. This
  716. * just re-inits the block as necessary. The actual
  717. * gating still happens in the dpm code. We should
  718. * revisit this when there is a cleaner line between
  719. * the smc and the hw blocks
  720. */
  721. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  722. if (state == AMD_PG_STATE_GATE) {
  723. uvd_v4_2_stop(adev);
  724. return 0;
  725. } else {
  726. return uvd_v4_2_start(adev);
  727. }
  728. }
  729. const struct amd_ip_funcs uvd_v4_2_ip_funcs = {
  730. .early_init = uvd_v4_2_early_init,
  731. .late_init = NULL,
  732. .sw_init = uvd_v4_2_sw_init,
  733. .sw_fini = uvd_v4_2_sw_fini,
  734. .hw_init = uvd_v4_2_hw_init,
  735. .hw_fini = uvd_v4_2_hw_fini,
  736. .suspend = uvd_v4_2_suspend,
  737. .resume = uvd_v4_2_resume,
  738. .is_idle = uvd_v4_2_is_idle,
  739. .wait_for_idle = uvd_v4_2_wait_for_idle,
  740. .soft_reset = uvd_v4_2_soft_reset,
  741. .print_status = uvd_v4_2_print_status,
  742. .set_clockgating_state = uvd_v4_2_set_clockgating_state,
  743. .set_powergating_state = uvd_v4_2_set_powergating_state,
  744. };
  745. static const struct amdgpu_ring_funcs uvd_v4_2_ring_funcs = {
  746. .get_rptr = uvd_v4_2_ring_get_rptr,
  747. .get_wptr = uvd_v4_2_ring_get_wptr,
  748. .set_wptr = uvd_v4_2_ring_set_wptr,
  749. .parse_cs = amdgpu_uvd_ring_parse_cs,
  750. .emit_ib = uvd_v4_2_ring_emit_ib,
  751. .emit_fence = uvd_v4_2_ring_emit_fence,
  752. .emit_semaphore = uvd_v4_2_ring_emit_semaphore,
  753. .test_ring = uvd_v4_2_ring_test_ring,
  754. .test_ib = uvd_v4_2_ring_test_ib,
  755. .insert_nop = amdgpu_ring_insert_nop,
  756. };
  757. static void uvd_v4_2_set_ring_funcs(struct amdgpu_device *adev)
  758. {
  759. adev->uvd.ring.funcs = &uvd_v4_2_ring_funcs;
  760. }
  761. static const struct amdgpu_irq_src_funcs uvd_v4_2_irq_funcs = {
  762. .set = uvd_v4_2_set_interrupt_state,
  763. .process = uvd_v4_2_process_interrupt,
  764. };
  765. static void uvd_v4_2_set_irq_funcs(struct amdgpu_device *adev)
  766. {
  767. adev->uvd.irq.num_types = 1;
  768. adev->uvd.irq.funcs = &uvd_v4_2_irq_funcs;
  769. }