uvd_v5_0.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * Copyright 2014 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 "vid.h"
  29. #include "uvd/uvd_5_0_d.h"
  30. #include "uvd/uvd_5_0_sh_mask.h"
  31. #include "oss/oss_2_0_d.h"
  32. #include "oss/oss_2_0_sh_mask.h"
  33. static void uvd_v5_0_set_ring_funcs(struct amdgpu_device *adev);
  34. static void uvd_v5_0_set_irq_funcs(struct amdgpu_device *adev);
  35. static int uvd_v5_0_start(struct amdgpu_device *adev);
  36. static void uvd_v5_0_stop(struct amdgpu_device *adev);
  37. /**
  38. * uvd_v5_0_ring_get_rptr - get read pointer
  39. *
  40. * @ring: amdgpu_ring pointer
  41. *
  42. * Returns the current hardware read pointer
  43. */
  44. static uint32_t uvd_v5_0_ring_get_rptr(struct amdgpu_ring *ring)
  45. {
  46. struct amdgpu_device *adev = ring->adev;
  47. return RREG32(mmUVD_RBC_RB_RPTR);
  48. }
  49. /**
  50. * uvd_v5_0_ring_get_wptr - get write pointer
  51. *
  52. * @ring: amdgpu_ring pointer
  53. *
  54. * Returns the current hardware write pointer
  55. */
  56. static uint32_t uvd_v5_0_ring_get_wptr(struct amdgpu_ring *ring)
  57. {
  58. struct amdgpu_device *adev = ring->adev;
  59. return RREG32(mmUVD_RBC_RB_WPTR);
  60. }
  61. /**
  62. * uvd_v5_0_ring_set_wptr - set write pointer
  63. *
  64. * @ring: amdgpu_ring pointer
  65. *
  66. * Commits the write pointer to the hardware
  67. */
  68. static void uvd_v5_0_ring_set_wptr(struct amdgpu_ring *ring)
  69. {
  70. struct amdgpu_device *adev = ring->adev;
  71. WREG32(mmUVD_RBC_RB_WPTR, ring->wptr);
  72. }
  73. static int uvd_v5_0_early_init(void *handle)
  74. {
  75. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  76. uvd_v5_0_set_ring_funcs(adev);
  77. uvd_v5_0_set_irq_funcs(adev);
  78. return 0;
  79. }
  80. static int uvd_v5_0_sw_init(void *handle)
  81. {
  82. struct amdgpu_ring *ring;
  83. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  84. int r;
  85. /* UVD TRAP */
  86. r = amdgpu_irq_add_id(adev, 124, &adev->uvd.irq);
  87. if (r)
  88. return r;
  89. r = amdgpu_uvd_sw_init(adev);
  90. if (r)
  91. return r;
  92. r = amdgpu_uvd_resume(adev);
  93. if (r)
  94. return r;
  95. ring = &adev->uvd.ring;
  96. sprintf(ring->name, "uvd");
  97. r = amdgpu_ring_init(adev, ring, 4096, CP_PACKET2, 0xf,
  98. &adev->uvd.irq, 0, AMDGPU_RING_TYPE_UVD);
  99. return r;
  100. }
  101. static int uvd_v5_0_sw_fini(void *handle)
  102. {
  103. int r;
  104. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  105. r = amdgpu_uvd_suspend(adev);
  106. if (r)
  107. return r;
  108. r = amdgpu_uvd_sw_fini(adev);
  109. if (r)
  110. return r;
  111. return r;
  112. }
  113. /**
  114. * uvd_v5_0_hw_init - start and test UVD block
  115. *
  116. * @adev: amdgpu_device pointer
  117. *
  118. * Initialize the hardware, boot up the VCPU and do some testing
  119. */
  120. static int uvd_v5_0_hw_init(void *handle)
  121. {
  122. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  123. struct amdgpu_ring *ring = &adev->uvd.ring;
  124. uint32_t tmp;
  125. int r;
  126. /* raise clocks while booting up the VCPU */
  127. amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  128. r = uvd_v5_0_start(adev);
  129. if (r)
  130. goto done;
  131. ring->ready = true;
  132. r = amdgpu_ring_test_ring(ring);
  133. if (r) {
  134. ring->ready = false;
  135. goto done;
  136. }
  137. r = amdgpu_ring_lock(ring, 10);
  138. if (r) {
  139. DRM_ERROR("amdgpu: ring failed to lock UVD ring (%d).\n", r);
  140. goto done;
  141. }
  142. tmp = PACKET0(mmUVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL, 0);
  143. amdgpu_ring_write(ring, tmp);
  144. amdgpu_ring_write(ring, 0xFFFFF);
  145. tmp = PACKET0(mmUVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL, 0);
  146. amdgpu_ring_write(ring, tmp);
  147. amdgpu_ring_write(ring, 0xFFFFF);
  148. tmp = PACKET0(mmUVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL, 0);
  149. amdgpu_ring_write(ring, tmp);
  150. amdgpu_ring_write(ring, 0xFFFFF);
  151. /* Clear timeout status bits */
  152. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_TIMEOUT_STATUS, 0));
  153. amdgpu_ring_write(ring, 0x8);
  154. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_CNTL, 0));
  155. amdgpu_ring_write(ring, 3);
  156. amdgpu_ring_unlock_commit(ring);
  157. done:
  158. /* lower clocks again */
  159. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  160. if (!r)
  161. DRM_INFO("UVD initialized successfully.\n");
  162. return r;
  163. }
  164. /**
  165. * uvd_v5_0_hw_fini - stop the hardware block
  166. *
  167. * @adev: amdgpu_device pointer
  168. *
  169. * Stop the UVD block, mark ring as not ready any more
  170. */
  171. static int uvd_v5_0_hw_fini(void *handle)
  172. {
  173. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  174. struct amdgpu_ring *ring = &adev->uvd.ring;
  175. uvd_v5_0_stop(adev);
  176. ring->ready = false;
  177. return 0;
  178. }
  179. static int uvd_v5_0_suspend(void *handle)
  180. {
  181. int r;
  182. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  183. r = amdgpu_uvd_suspend(adev);
  184. if (r)
  185. return r;
  186. r = uvd_v5_0_hw_fini(adev);
  187. if (r)
  188. return r;
  189. return r;
  190. }
  191. static int uvd_v5_0_resume(void *handle)
  192. {
  193. int r;
  194. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  195. r = amdgpu_uvd_resume(adev);
  196. if (r)
  197. return r;
  198. r = uvd_v5_0_hw_init(adev);
  199. if (r)
  200. return r;
  201. return r;
  202. }
  203. /**
  204. * uvd_v5_0_mc_resume - memory controller programming
  205. *
  206. * @adev: amdgpu_device pointer
  207. *
  208. * Let the UVD memory controller know it's offsets
  209. */
  210. static void uvd_v5_0_mc_resume(struct amdgpu_device *adev)
  211. {
  212. uint64_t offset;
  213. uint32_t size;
  214. /* programm memory controller bits 0-27 */
  215. WREG32(mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
  216. lower_32_bits(adev->uvd.gpu_addr));
  217. WREG32(mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
  218. upper_32_bits(adev->uvd.gpu_addr));
  219. offset = AMDGPU_UVD_FIRMWARE_OFFSET;
  220. size = AMDGPU_GPU_PAGE_ALIGN(adev->uvd.fw->size + 4);
  221. WREG32(mmUVD_VCPU_CACHE_OFFSET0, offset >> 3);
  222. WREG32(mmUVD_VCPU_CACHE_SIZE0, size);
  223. offset += size;
  224. size = AMDGPU_UVD_STACK_SIZE;
  225. WREG32(mmUVD_VCPU_CACHE_OFFSET1, offset >> 3);
  226. WREG32(mmUVD_VCPU_CACHE_SIZE1, size);
  227. offset += size;
  228. size = AMDGPU_UVD_HEAP_SIZE;
  229. WREG32(mmUVD_VCPU_CACHE_OFFSET2, offset >> 3);
  230. WREG32(mmUVD_VCPU_CACHE_SIZE2, size);
  231. }
  232. /**
  233. * uvd_v5_0_start - start UVD block
  234. *
  235. * @adev: amdgpu_device pointer
  236. *
  237. * Setup and start the UVD block
  238. */
  239. static int uvd_v5_0_start(struct amdgpu_device *adev)
  240. {
  241. struct amdgpu_ring *ring = &adev->uvd.ring;
  242. uint32_t rb_bufsz, tmp;
  243. uint32_t lmi_swap_cntl;
  244. uint32_t mp_swap_cntl;
  245. int i, j, r;
  246. /*disable DPG */
  247. WREG32_P(mmUVD_POWER_STATUS, 0, ~(1 << 2));
  248. /* disable byte swapping */
  249. lmi_swap_cntl = 0;
  250. mp_swap_cntl = 0;
  251. uvd_v5_0_mc_resume(adev);
  252. /* disable clock gating */
  253. WREG32(mmUVD_CGC_GATE, 0);
  254. /* disable interupt */
  255. WREG32_P(mmUVD_MASTINT_EN, 0, ~(1 << 1));
  256. /* stall UMC and register bus before resetting VCPU */
  257. WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
  258. mdelay(1);
  259. /* put LMI, VCPU, RBC etc... into reset */
  260. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__LMI_SOFT_RESET_MASK |
  261. UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK | UVD_SOFT_RESET__LBSI_SOFT_RESET_MASK |
  262. UVD_SOFT_RESET__RBC_SOFT_RESET_MASK | UVD_SOFT_RESET__CSM_SOFT_RESET_MASK |
  263. UVD_SOFT_RESET__CXW_SOFT_RESET_MASK | UVD_SOFT_RESET__TAP_SOFT_RESET_MASK |
  264. UVD_SOFT_RESET__LMI_UMC_SOFT_RESET_MASK);
  265. mdelay(5);
  266. /* take UVD block out of reset */
  267. WREG32_P(mmSRBM_SOFT_RESET, 0, ~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
  268. mdelay(5);
  269. /* initialize UVD memory controller */
  270. WREG32(mmUVD_LMI_CTRL, 0x40 | (1 << 8) | (1 << 13) |
  271. (1 << 21) | (1 << 9) | (1 << 20));
  272. #ifdef __BIG_ENDIAN
  273. /* swap (8 in 32) RB and IB */
  274. lmi_swap_cntl = 0xa;
  275. mp_swap_cntl = 0;
  276. #endif
  277. WREG32(mmUVD_LMI_SWAP_CNTL, lmi_swap_cntl);
  278. WREG32(mmUVD_MP_SWAP_CNTL, mp_swap_cntl);
  279. WREG32(mmUVD_MPC_SET_MUXA0, 0x40c2040);
  280. WREG32(mmUVD_MPC_SET_MUXA1, 0x0);
  281. WREG32(mmUVD_MPC_SET_MUXB0, 0x40c2040);
  282. WREG32(mmUVD_MPC_SET_MUXB1, 0x0);
  283. WREG32(mmUVD_MPC_SET_ALU, 0);
  284. WREG32(mmUVD_MPC_SET_MUX, 0x88);
  285. /* take all subblocks out of reset, except VCPU */
  286. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  287. mdelay(5);
  288. /* enable VCPU clock */
  289. WREG32(mmUVD_VCPU_CNTL, 1 << 9);
  290. /* enable UMC */
  291. WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
  292. /* boot up the VCPU */
  293. WREG32(mmUVD_SOFT_RESET, 0);
  294. mdelay(10);
  295. for (i = 0; i < 10; ++i) {
  296. uint32_t status;
  297. for (j = 0; j < 100; ++j) {
  298. status = RREG32(mmUVD_STATUS);
  299. if (status & 2)
  300. break;
  301. mdelay(10);
  302. }
  303. r = 0;
  304. if (status & 2)
  305. break;
  306. DRM_ERROR("UVD not responding, trying to reset the VCPU!!!\n");
  307. WREG32_P(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK,
  308. ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  309. mdelay(10);
  310. WREG32_P(mmUVD_SOFT_RESET, 0, ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  311. mdelay(10);
  312. r = -1;
  313. }
  314. if (r) {
  315. DRM_ERROR("UVD not responding, giving up!!!\n");
  316. return r;
  317. }
  318. /* enable master interrupt */
  319. WREG32_P(mmUVD_MASTINT_EN, 3 << 1, ~(3 << 1));
  320. /* clear the bit 4 of UVD_STATUS */
  321. WREG32_P(mmUVD_STATUS, 0, ~(2 << 1));
  322. rb_bufsz = order_base_2(ring->ring_size);
  323. tmp = 0;
  324. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
  325. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
  326. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
  327. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_WPTR_POLL_EN, 0);
  328. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
  329. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
  330. /* force RBC into idle state */
  331. WREG32(mmUVD_RBC_RB_CNTL, tmp);
  332. /* set the write pointer delay */
  333. WREG32(mmUVD_RBC_RB_WPTR_CNTL, 0);
  334. /* set the wb address */
  335. WREG32(mmUVD_RBC_RB_RPTR_ADDR, (upper_32_bits(ring->gpu_addr) >> 2));
  336. /* programm the RB_BASE for ring buffer */
  337. WREG32(mmUVD_LMI_RBC_RB_64BIT_BAR_LOW,
  338. lower_32_bits(ring->gpu_addr));
  339. WREG32(mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH,
  340. upper_32_bits(ring->gpu_addr));
  341. /* Initialize the ring buffer's read and write pointers */
  342. WREG32(mmUVD_RBC_RB_RPTR, 0);
  343. ring->wptr = RREG32(mmUVD_RBC_RB_RPTR);
  344. WREG32(mmUVD_RBC_RB_WPTR, ring->wptr);
  345. WREG32_P(mmUVD_RBC_RB_CNTL, 0, ~UVD_RBC_RB_CNTL__RB_NO_FETCH_MASK);
  346. return 0;
  347. }
  348. /**
  349. * uvd_v5_0_stop - stop UVD block
  350. *
  351. * @adev: amdgpu_device pointer
  352. *
  353. * stop the UVD block
  354. */
  355. static void uvd_v5_0_stop(struct amdgpu_device *adev)
  356. {
  357. /* force RBC into idle state */
  358. WREG32(mmUVD_RBC_RB_CNTL, 0x11010101);
  359. /* Stall UMC and register bus before resetting VCPU */
  360. WREG32_P(mmUVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
  361. mdelay(1);
  362. /* put VCPU into reset */
  363. WREG32(mmUVD_SOFT_RESET, UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  364. mdelay(5);
  365. /* disable VCPU clock */
  366. WREG32(mmUVD_VCPU_CNTL, 0x0);
  367. /* Unstall UMC and register bus */
  368. WREG32_P(mmUVD_LMI_CTRL2, 0, ~(1 << 8));
  369. }
  370. /**
  371. * uvd_v5_0_ring_emit_fence - emit an fence & trap command
  372. *
  373. * @ring: amdgpu_ring pointer
  374. * @fence: fence to emit
  375. *
  376. * Write a fence and a trap command to the ring.
  377. */
  378. static void uvd_v5_0_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  379. unsigned flags)
  380. {
  381. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  382. amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
  383. amdgpu_ring_write(ring, seq);
  384. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
  385. amdgpu_ring_write(ring, addr & 0xffffffff);
  386. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
  387. amdgpu_ring_write(ring, upper_32_bits(addr) & 0xff);
  388. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
  389. amdgpu_ring_write(ring, 0);
  390. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA0, 0));
  391. amdgpu_ring_write(ring, 0);
  392. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_DATA1, 0));
  393. amdgpu_ring_write(ring, 0);
  394. amdgpu_ring_write(ring, PACKET0(mmUVD_GPCOM_VCPU_CMD, 0));
  395. amdgpu_ring_write(ring, 2);
  396. }
  397. /**
  398. * uvd_v5_0_ring_emit_semaphore - emit semaphore command
  399. *
  400. * @ring: amdgpu_ring pointer
  401. * @semaphore: semaphore to emit commands for
  402. * @emit_wait: true if we should emit a wait command
  403. *
  404. * Emit a semaphore command (either wait or signal) to the UVD ring.
  405. */
  406. static bool uvd_v5_0_ring_emit_semaphore(struct amdgpu_ring *ring,
  407. struct amdgpu_semaphore *semaphore,
  408. bool emit_wait)
  409. {
  410. uint64_t addr = semaphore->gpu_addr;
  411. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_ADDR_LOW, 0));
  412. amdgpu_ring_write(ring, (addr >> 3) & 0x000FFFFF);
  413. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_ADDR_HIGH, 0));
  414. amdgpu_ring_write(ring, (addr >> 23) & 0x000FFFFF);
  415. amdgpu_ring_write(ring, PACKET0(mmUVD_SEMA_CMD, 0));
  416. amdgpu_ring_write(ring, 0x80 | (emit_wait ? 1 : 0));
  417. return true;
  418. }
  419. /**
  420. * uvd_v5_0_ring_test_ring - register write test
  421. *
  422. * @ring: amdgpu_ring pointer
  423. *
  424. * Test if we can successfully write to the context register
  425. */
  426. static int uvd_v5_0_ring_test_ring(struct amdgpu_ring *ring)
  427. {
  428. struct amdgpu_device *adev = ring->adev;
  429. uint32_t tmp = 0;
  430. unsigned i;
  431. int r;
  432. WREG32(mmUVD_CONTEXT_ID, 0xCAFEDEAD);
  433. r = amdgpu_ring_lock(ring, 3);
  434. if (r) {
  435. DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
  436. ring->idx, r);
  437. return r;
  438. }
  439. amdgpu_ring_write(ring, PACKET0(mmUVD_CONTEXT_ID, 0));
  440. amdgpu_ring_write(ring, 0xDEADBEEF);
  441. amdgpu_ring_unlock_commit(ring);
  442. for (i = 0; i < adev->usec_timeout; i++) {
  443. tmp = RREG32(mmUVD_CONTEXT_ID);
  444. if (tmp == 0xDEADBEEF)
  445. break;
  446. DRM_UDELAY(1);
  447. }
  448. if (i < adev->usec_timeout) {
  449. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  450. ring->idx, i);
  451. } else {
  452. DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
  453. ring->idx, tmp);
  454. r = -EINVAL;
  455. }
  456. return r;
  457. }
  458. /**
  459. * uvd_v5_0_ring_emit_ib - execute indirect buffer
  460. *
  461. * @ring: amdgpu_ring pointer
  462. * @ib: indirect buffer to execute
  463. *
  464. * Write ring commands to execute the indirect buffer
  465. */
  466. static void uvd_v5_0_ring_emit_ib(struct amdgpu_ring *ring,
  467. struct amdgpu_ib *ib)
  468. {
  469. amdgpu_ring_write(ring, PACKET0(mmUVD_LMI_RBC_IB_64BIT_BAR_LOW, 0));
  470. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  471. amdgpu_ring_write(ring, PACKET0(mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH, 0));
  472. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  473. amdgpu_ring_write(ring, PACKET0(mmUVD_RBC_IB_SIZE, 0));
  474. amdgpu_ring_write(ring, ib->length_dw);
  475. }
  476. /**
  477. * uvd_v5_0_ring_test_ib - test ib execution
  478. *
  479. * @ring: amdgpu_ring pointer
  480. *
  481. * Test if we can successfully execute an IB
  482. */
  483. static int uvd_v5_0_ring_test_ib(struct amdgpu_ring *ring)
  484. {
  485. struct amdgpu_device *adev = ring->adev;
  486. struct fence *fence = NULL;
  487. int r;
  488. r = amdgpu_asic_set_uvd_clocks(adev, 53300, 40000);
  489. if (r) {
  490. DRM_ERROR("amdgpu: failed to raise UVD clocks (%d).\n", r);
  491. return r;
  492. }
  493. r = amdgpu_uvd_get_create_msg(ring, 1, NULL);
  494. if (r) {
  495. DRM_ERROR("amdgpu: failed to get create msg (%d).\n", r);
  496. goto error;
  497. }
  498. r = amdgpu_uvd_get_destroy_msg(ring, 1, &fence);
  499. if (r) {
  500. DRM_ERROR("amdgpu: failed to get destroy ib (%d).\n", r);
  501. goto error;
  502. }
  503. r = fence_wait(fence, false);
  504. if (r) {
  505. DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
  506. goto error;
  507. }
  508. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  509. error:
  510. fence_put(fence);
  511. amdgpu_asic_set_uvd_clocks(adev, 0, 0);
  512. return r;
  513. }
  514. static bool uvd_v5_0_is_idle(void *handle)
  515. {
  516. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  517. return !(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK);
  518. }
  519. static int uvd_v5_0_wait_for_idle(void *handle)
  520. {
  521. unsigned i;
  522. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  523. for (i = 0; i < adev->usec_timeout; i++) {
  524. if (!(RREG32(mmSRBM_STATUS) & SRBM_STATUS__UVD_BUSY_MASK))
  525. return 0;
  526. }
  527. return -ETIMEDOUT;
  528. }
  529. static int uvd_v5_0_soft_reset(void *handle)
  530. {
  531. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  532. uvd_v5_0_stop(adev);
  533. WREG32_P(mmSRBM_SOFT_RESET, SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK,
  534. ~SRBM_SOFT_RESET__SOFT_RESET_UVD_MASK);
  535. mdelay(5);
  536. return uvd_v5_0_start(adev);
  537. }
  538. static void uvd_v5_0_print_status(void *handle)
  539. {
  540. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  541. dev_info(adev->dev, "UVD 5.0 registers\n");
  542. dev_info(adev->dev, " UVD_SEMA_ADDR_LOW=0x%08X\n",
  543. RREG32(mmUVD_SEMA_ADDR_LOW));
  544. dev_info(adev->dev, " UVD_SEMA_ADDR_HIGH=0x%08X\n",
  545. RREG32(mmUVD_SEMA_ADDR_HIGH));
  546. dev_info(adev->dev, " UVD_SEMA_CMD=0x%08X\n",
  547. RREG32(mmUVD_SEMA_CMD));
  548. dev_info(adev->dev, " UVD_GPCOM_VCPU_CMD=0x%08X\n",
  549. RREG32(mmUVD_GPCOM_VCPU_CMD));
  550. dev_info(adev->dev, " UVD_GPCOM_VCPU_DATA0=0x%08X\n",
  551. RREG32(mmUVD_GPCOM_VCPU_DATA0));
  552. dev_info(adev->dev, " UVD_GPCOM_VCPU_DATA1=0x%08X\n",
  553. RREG32(mmUVD_GPCOM_VCPU_DATA1));
  554. dev_info(adev->dev, " UVD_ENGINE_CNTL=0x%08X\n",
  555. RREG32(mmUVD_ENGINE_CNTL));
  556. dev_info(adev->dev, " UVD_UDEC_ADDR_CONFIG=0x%08X\n",
  557. RREG32(mmUVD_UDEC_ADDR_CONFIG));
  558. dev_info(adev->dev, " UVD_UDEC_DB_ADDR_CONFIG=0x%08X\n",
  559. RREG32(mmUVD_UDEC_DB_ADDR_CONFIG));
  560. dev_info(adev->dev, " UVD_UDEC_DBW_ADDR_CONFIG=0x%08X\n",
  561. RREG32(mmUVD_UDEC_DBW_ADDR_CONFIG));
  562. dev_info(adev->dev, " UVD_SEMA_CNTL=0x%08X\n",
  563. RREG32(mmUVD_SEMA_CNTL));
  564. dev_info(adev->dev, " UVD_LMI_EXT40_ADDR=0x%08X\n",
  565. RREG32(mmUVD_LMI_EXT40_ADDR));
  566. dev_info(adev->dev, " UVD_CTX_INDEX=0x%08X\n",
  567. RREG32(mmUVD_CTX_INDEX));
  568. dev_info(adev->dev, " UVD_CTX_DATA=0x%08X\n",
  569. RREG32(mmUVD_CTX_DATA));
  570. dev_info(adev->dev, " UVD_CGC_GATE=0x%08X\n",
  571. RREG32(mmUVD_CGC_GATE));
  572. dev_info(adev->dev, " UVD_CGC_CTRL=0x%08X\n",
  573. RREG32(mmUVD_CGC_CTRL));
  574. dev_info(adev->dev, " UVD_LMI_CTRL2=0x%08X\n",
  575. RREG32(mmUVD_LMI_CTRL2));
  576. dev_info(adev->dev, " UVD_MASTINT_EN=0x%08X\n",
  577. RREG32(mmUVD_MASTINT_EN));
  578. dev_info(adev->dev, " UVD_LMI_ADDR_EXT=0x%08X\n",
  579. RREG32(mmUVD_LMI_ADDR_EXT));
  580. dev_info(adev->dev, " UVD_LMI_CTRL=0x%08X\n",
  581. RREG32(mmUVD_LMI_CTRL));
  582. dev_info(adev->dev, " UVD_LMI_SWAP_CNTL=0x%08X\n",
  583. RREG32(mmUVD_LMI_SWAP_CNTL));
  584. dev_info(adev->dev, " UVD_MP_SWAP_CNTL=0x%08X\n",
  585. RREG32(mmUVD_MP_SWAP_CNTL));
  586. dev_info(adev->dev, " UVD_MPC_SET_MUXA0=0x%08X\n",
  587. RREG32(mmUVD_MPC_SET_MUXA0));
  588. dev_info(adev->dev, " UVD_MPC_SET_MUXA1=0x%08X\n",
  589. RREG32(mmUVD_MPC_SET_MUXA1));
  590. dev_info(adev->dev, " UVD_MPC_SET_MUXB0=0x%08X\n",
  591. RREG32(mmUVD_MPC_SET_MUXB0));
  592. dev_info(adev->dev, " UVD_MPC_SET_MUXB1=0x%08X\n",
  593. RREG32(mmUVD_MPC_SET_MUXB1));
  594. dev_info(adev->dev, " UVD_MPC_SET_MUX=0x%08X\n",
  595. RREG32(mmUVD_MPC_SET_MUX));
  596. dev_info(adev->dev, " UVD_MPC_SET_ALU=0x%08X\n",
  597. RREG32(mmUVD_MPC_SET_ALU));
  598. dev_info(adev->dev, " UVD_VCPU_CACHE_OFFSET0=0x%08X\n",
  599. RREG32(mmUVD_VCPU_CACHE_OFFSET0));
  600. dev_info(adev->dev, " UVD_VCPU_CACHE_SIZE0=0x%08X\n",
  601. RREG32(mmUVD_VCPU_CACHE_SIZE0));
  602. dev_info(adev->dev, " UVD_VCPU_CACHE_OFFSET1=0x%08X\n",
  603. RREG32(mmUVD_VCPU_CACHE_OFFSET1));
  604. dev_info(adev->dev, " UVD_VCPU_CACHE_SIZE1=0x%08X\n",
  605. RREG32(mmUVD_VCPU_CACHE_SIZE1));
  606. dev_info(adev->dev, " UVD_VCPU_CACHE_OFFSET2=0x%08X\n",
  607. RREG32(mmUVD_VCPU_CACHE_OFFSET2));
  608. dev_info(adev->dev, " UVD_VCPU_CACHE_SIZE2=0x%08X\n",
  609. RREG32(mmUVD_VCPU_CACHE_SIZE2));
  610. dev_info(adev->dev, " UVD_VCPU_CNTL=0x%08X\n",
  611. RREG32(mmUVD_VCPU_CNTL));
  612. dev_info(adev->dev, " UVD_SOFT_RESET=0x%08X\n",
  613. RREG32(mmUVD_SOFT_RESET));
  614. dev_info(adev->dev, " UVD_LMI_RBC_IB_64BIT_BAR_LOW=0x%08X\n",
  615. RREG32(mmUVD_LMI_RBC_IB_64BIT_BAR_LOW));
  616. dev_info(adev->dev, " UVD_LMI_RBC_IB_64BIT_BAR_HIGH=0x%08X\n",
  617. RREG32(mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH));
  618. dev_info(adev->dev, " UVD_RBC_IB_SIZE=0x%08X\n",
  619. RREG32(mmUVD_RBC_IB_SIZE));
  620. dev_info(adev->dev, " UVD_LMI_RBC_RB_64BIT_BAR_LOW=0x%08X\n",
  621. RREG32(mmUVD_LMI_RBC_RB_64BIT_BAR_LOW));
  622. dev_info(adev->dev, " UVD_LMI_RBC_RB_64BIT_BAR_HIGH=0x%08X\n",
  623. RREG32(mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH));
  624. dev_info(adev->dev, " UVD_RBC_RB_RPTR=0x%08X\n",
  625. RREG32(mmUVD_RBC_RB_RPTR));
  626. dev_info(adev->dev, " UVD_RBC_RB_WPTR=0x%08X\n",
  627. RREG32(mmUVD_RBC_RB_WPTR));
  628. dev_info(adev->dev, " UVD_RBC_RB_WPTR_CNTL=0x%08X\n",
  629. RREG32(mmUVD_RBC_RB_WPTR_CNTL));
  630. dev_info(adev->dev, " UVD_RBC_RB_CNTL=0x%08X\n",
  631. RREG32(mmUVD_RBC_RB_CNTL));
  632. dev_info(adev->dev, " UVD_STATUS=0x%08X\n",
  633. RREG32(mmUVD_STATUS));
  634. dev_info(adev->dev, " UVD_SEMA_TIMEOUT_STATUS=0x%08X\n",
  635. RREG32(mmUVD_SEMA_TIMEOUT_STATUS));
  636. dev_info(adev->dev, " UVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL=0x%08X\n",
  637. RREG32(mmUVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL));
  638. dev_info(adev->dev, " UVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL=0x%08X\n",
  639. RREG32(mmUVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL));
  640. dev_info(adev->dev, " UVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL=0x%08X\n",
  641. RREG32(mmUVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL));
  642. dev_info(adev->dev, " UVD_CONTEXT_ID=0x%08X\n",
  643. RREG32(mmUVD_CONTEXT_ID));
  644. }
  645. static int uvd_v5_0_set_interrupt_state(struct amdgpu_device *adev,
  646. struct amdgpu_irq_src *source,
  647. unsigned type,
  648. enum amdgpu_interrupt_state state)
  649. {
  650. // TODO
  651. return 0;
  652. }
  653. static int uvd_v5_0_process_interrupt(struct amdgpu_device *adev,
  654. struct amdgpu_irq_src *source,
  655. struct amdgpu_iv_entry *entry)
  656. {
  657. DRM_DEBUG("IH: UVD TRAP\n");
  658. amdgpu_fence_process(&adev->uvd.ring);
  659. return 0;
  660. }
  661. static int uvd_v5_0_set_clockgating_state(void *handle,
  662. enum amd_clockgating_state state)
  663. {
  664. return 0;
  665. }
  666. static int uvd_v5_0_set_powergating_state(void *handle,
  667. enum amd_powergating_state state)
  668. {
  669. /* This doesn't actually powergate the UVD block.
  670. * That's done in the dpm code via the SMC. This
  671. * just re-inits the block as necessary. The actual
  672. * gating still happens in the dpm code. We should
  673. * revisit this when there is a cleaner line between
  674. * the smc and the hw blocks
  675. */
  676. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  677. if (state == AMD_PG_STATE_GATE) {
  678. uvd_v5_0_stop(adev);
  679. return 0;
  680. } else {
  681. return uvd_v5_0_start(adev);
  682. }
  683. }
  684. const struct amd_ip_funcs uvd_v5_0_ip_funcs = {
  685. .early_init = uvd_v5_0_early_init,
  686. .late_init = NULL,
  687. .sw_init = uvd_v5_0_sw_init,
  688. .sw_fini = uvd_v5_0_sw_fini,
  689. .hw_init = uvd_v5_0_hw_init,
  690. .hw_fini = uvd_v5_0_hw_fini,
  691. .suspend = uvd_v5_0_suspend,
  692. .resume = uvd_v5_0_resume,
  693. .is_idle = uvd_v5_0_is_idle,
  694. .wait_for_idle = uvd_v5_0_wait_for_idle,
  695. .soft_reset = uvd_v5_0_soft_reset,
  696. .print_status = uvd_v5_0_print_status,
  697. .set_clockgating_state = uvd_v5_0_set_clockgating_state,
  698. .set_powergating_state = uvd_v5_0_set_powergating_state,
  699. };
  700. static const struct amdgpu_ring_funcs uvd_v5_0_ring_funcs = {
  701. .get_rptr = uvd_v5_0_ring_get_rptr,
  702. .get_wptr = uvd_v5_0_ring_get_wptr,
  703. .set_wptr = uvd_v5_0_ring_set_wptr,
  704. .parse_cs = amdgpu_uvd_ring_parse_cs,
  705. .emit_ib = uvd_v5_0_ring_emit_ib,
  706. .emit_fence = uvd_v5_0_ring_emit_fence,
  707. .emit_semaphore = uvd_v5_0_ring_emit_semaphore,
  708. .test_ring = uvd_v5_0_ring_test_ring,
  709. .test_ib = uvd_v5_0_ring_test_ib,
  710. .insert_nop = amdgpu_ring_insert_nop,
  711. };
  712. static void uvd_v5_0_set_ring_funcs(struct amdgpu_device *adev)
  713. {
  714. adev->uvd.ring.funcs = &uvd_v5_0_ring_funcs;
  715. }
  716. static const struct amdgpu_irq_src_funcs uvd_v5_0_irq_funcs = {
  717. .set = uvd_v5_0_set_interrupt_state,
  718. .process = uvd_v5_0_process_interrupt,
  719. };
  720. static void uvd_v5_0_set_irq_funcs(struct amdgpu_device *adev)
  721. {
  722. adev->uvd.irq.num_types = 1;
  723. adev->uvd.irq.funcs = &uvd_v5_0_irq_funcs;
  724. }