uvd_v1_0.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 "radeon.h"
  27. #include "radeon_asic.h"
  28. #include "r600d.h"
  29. /**
  30. * uvd_v1_0_get_rptr - get read pointer
  31. *
  32. * @rdev: radeon_device pointer
  33. * @ring: radeon_ring pointer
  34. *
  35. * Returns the current hardware read pointer
  36. */
  37. uint32_t uvd_v1_0_get_rptr(struct radeon_device *rdev,
  38. struct radeon_ring *ring)
  39. {
  40. return RREG32(UVD_RBC_RB_RPTR);
  41. }
  42. /**
  43. * uvd_v1_0_get_wptr - get write pointer
  44. *
  45. * @rdev: radeon_device pointer
  46. * @ring: radeon_ring pointer
  47. *
  48. * Returns the current hardware write pointer
  49. */
  50. uint32_t uvd_v1_0_get_wptr(struct radeon_device *rdev,
  51. struct radeon_ring *ring)
  52. {
  53. return RREG32(UVD_RBC_RB_WPTR);
  54. }
  55. /**
  56. * uvd_v1_0_set_wptr - set write pointer
  57. *
  58. * @rdev: radeon_device pointer
  59. * @ring: radeon_ring pointer
  60. *
  61. * Commits the write pointer to the hardware
  62. */
  63. void uvd_v1_0_set_wptr(struct radeon_device *rdev,
  64. struct radeon_ring *ring)
  65. {
  66. WREG32(UVD_RBC_RB_WPTR, ring->wptr);
  67. }
  68. /**
  69. * uvd_v1_0_fence_emit - emit an fence & trap command
  70. *
  71. * @rdev: radeon_device pointer
  72. * @fence: fence to emit
  73. *
  74. * Write a fence and a trap command to the ring.
  75. */
  76. void uvd_v1_0_fence_emit(struct radeon_device *rdev,
  77. struct radeon_fence *fence)
  78. {
  79. struct radeon_ring *ring = &rdev->ring[fence->ring];
  80. uint64_t addr = rdev->fence_drv[fence->ring].gpu_addr;
  81. radeon_ring_write(ring, PACKET0(UVD_GPCOM_VCPU_DATA0, 0));
  82. radeon_ring_write(ring, addr & 0xffffffff);
  83. radeon_ring_write(ring, PACKET0(UVD_GPCOM_VCPU_DATA1, 0));
  84. radeon_ring_write(ring, fence->seq);
  85. radeon_ring_write(ring, PACKET0(UVD_GPCOM_VCPU_CMD, 0));
  86. radeon_ring_write(ring, 0);
  87. radeon_ring_write(ring, PACKET0(UVD_GPCOM_VCPU_DATA0, 0));
  88. radeon_ring_write(ring, 0);
  89. radeon_ring_write(ring, PACKET0(UVD_GPCOM_VCPU_DATA1, 0));
  90. radeon_ring_write(ring, 0);
  91. radeon_ring_write(ring, PACKET0(UVD_GPCOM_VCPU_CMD, 0));
  92. radeon_ring_write(ring, 2);
  93. return;
  94. }
  95. /**
  96. * uvd_v1_0_resume - memory controller programming
  97. *
  98. * @rdev: radeon_device pointer
  99. *
  100. * Let the UVD memory controller know it's offsets
  101. */
  102. int uvd_v1_0_resume(struct radeon_device *rdev)
  103. {
  104. uint64_t addr;
  105. uint32_t size;
  106. int r;
  107. r = radeon_uvd_resume(rdev);
  108. if (r)
  109. return r;
  110. /* programm the VCPU memory controller bits 0-27 */
  111. addr = (rdev->uvd.gpu_addr >> 3) + 16;
  112. size = RADEON_GPU_PAGE_ALIGN(rdev->uvd_fw->size) >> 3;
  113. WREG32(UVD_VCPU_CACHE_OFFSET0, addr);
  114. WREG32(UVD_VCPU_CACHE_SIZE0, size);
  115. addr += size;
  116. size = RADEON_UVD_STACK_SIZE >> 3;
  117. WREG32(UVD_VCPU_CACHE_OFFSET1, addr);
  118. WREG32(UVD_VCPU_CACHE_SIZE1, size);
  119. addr += size;
  120. size = RADEON_UVD_HEAP_SIZE >> 3;
  121. WREG32(UVD_VCPU_CACHE_OFFSET2, addr);
  122. WREG32(UVD_VCPU_CACHE_SIZE2, size);
  123. /* bits 28-31 */
  124. addr = (rdev->uvd.gpu_addr >> 28) & 0xF;
  125. WREG32(UVD_LMI_ADDR_EXT, (addr << 12) | (addr << 0));
  126. /* bits 32-39 */
  127. addr = (rdev->uvd.gpu_addr >> 32) & 0xFF;
  128. WREG32(UVD_LMI_EXT40_ADDR, addr | (0x9 << 16) | (0x1 << 31));
  129. WREG32(UVD_FW_START, *((uint32_t*)rdev->uvd.cpu_addr));
  130. return 0;
  131. }
  132. /**
  133. * uvd_v1_0_init - start and test UVD block
  134. *
  135. * @rdev: radeon_device pointer
  136. *
  137. * Initialize the hardware, boot up the VCPU and do some testing
  138. */
  139. int uvd_v1_0_init(struct radeon_device *rdev)
  140. {
  141. struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_UVD_INDEX];
  142. uint32_t tmp;
  143. int r;
  144. /* raise clocks while booting up the VCPU */
  145. if (rdev->family < CHIP_RV740)
  146. radeon_set_uvd_clocks(rdev, 10000, 10000);
  147. else
  148. radeon_set_uvd_clocks(rdev, 53300, 40000);
  149. r = uvd_v1_0_start(rdev);
  150. if (r)
  151. goto done;
  152. ring->ready = true;
  153. r = radeon_ring_test(rdev, R600_RING_TYPE_UVD_INDEX, ring);
  154. if (r) {
  155. ring->ready = false;
  156. goto done;
  157. }
  158. r = radeon_ring_lock(rdev, ring, 10);
  159. if (r) {
  160. DRM_ERROR("radeon: ring failed to lock UVD ring (%d).\n", r);
  161. goto done;
  162. }
  163. tmp = PACKET0(UVD_SEMA_WAIT_FAULT_TIMEOUT_CNTL, 0);
  164. radeon_ring_write(ring, tmp);
  165. radeon_ring_write(ring, 0xFFFFF);
  166. tmp = PACKET0(UVD_SEMA_WAIT_INCOMPLETE_TIMEOUT_CNTL, 0);
  167. radeon_ring_write(ring, tmp);
  168. radeon_ring_write(ring, 0xFFFFF);
  169. tmp = PACKET0(UVD_SEMA_SIGNAL_INCOMPLETE_TIMEOUT_CNTL, 0);
  170. radeon_ring_write(ring, tmp);
  171. radeon_ring_write(ring, 0xFFFFF);
  172. /* Clear timeout status bits */
  173. radeon_ring_write(ring, PACKET0(UVD_SEMA_TIMEOUT_STATUS, 0));
  174. radeon_ring_write(ring, 0x8);
  175. radeon_ring_write(ring, PACKET0(UVD_SEMA_CNTL, 0));
  176. radeon_ring_write(ring, 3);
  177. radeon_ring_unlock_commit(rdev, ring, false);
  178. done:
  179. /* lower clocks again */
  180. radeon_set_uvd_clocks(rdev, 0, 0);
  181. if (!r) {
  182. switch (rdev->family) {
  183. case CHIP_RV610:
  184. case CHIP_RV630:
  185. case CHIP_RV620:
  186. /* 64byte granularity workaround */
  187. WREG32(MC_CONFIG, 0);
  188. WREG32(MC_CONFIG, 1 << 4);
  189. WREG32(RS_DQ_RD_RET_CONF, 0x3f);
  190. WREG32(MC_CONFIG, 0x1f);
  191. /* fall through */
  192. case CHIP_RV670:
  193. case CHIP_RV635:
  194. /* write clean workaround */
  195. WREG32_P(UVD_VCPU_CNTL, 0x10, ~0x10);
  196. break;
  197. default:
  198. /* TODO: Do we need more? */
  199. break;
  200. }
  201. DRM_INFO("UVD initialized successfully.\n");
  202. }
  203. return r;
  204. }
  205. /**
  206. * uvd_v1_0_fini - stop the hardware block
  207. *
  208. * @rdev: radeon_device pointer
  209. *
  210. * Stop the UVD block, mark ring as not ready any more
  211. */
  212. void uvd_v1_0_fini(struct radeon_device *rdev)
  213. {
  214. struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_UVD_INDEX];
  215. uvd_v1_0_stop(rdev);
  216. ring->ready = false;
  217. }
  218. /**
  219. * uvd_v1_0_start - start UVD block
  220. *
  221. * @rdev: radeon_device pointer
  222. *
  223. * Setup and start the UVD block
  224. */
  225. int uvd_v1_0_start(struct radeon_device *rdev)
  226. {
  227. struct radeon_ring *ring = &rdev->ring[R600_RING_TYPE_UVD_INDEX];
  228. uint32_t rb_bufsz;
  229. int i, j, r;
  230. /* disable byte swapping */
  231. u32 lmi_swap_cntl = 0;
  232. u32 mp_swap_cntl = 0;
  233. /* disable clock gating */
  234. WREG32(UVD_CGC_GATE, 0);
  235. /* disable interupt */
  236. WREG32_P(UVD_MASTINT_EN, 0, ~(1 << 1));
  237. /* Stall UMC and register bus before resetting VCPU */
  238. WREG32_P(UVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
  239. WREG32_P(UVD_RB_ARB_CTRL, 1 << 3, ~(1 << 3));
  240. mdelay(1);
  241. /* put LMI, VCPU, RBC etc... into reset */
  242. WREG32(UVD_SOFT_RESET, LMI_SOFT_RESET | VCPU_SOFT_RESET |
  243. LBSI_SOFT_RESET | RBC_SOFT_RESET | CSM_SOFT_RESET |
  244. CXW_SOFT_RESET | TAP_SOFT_RESET | LMI_UMC_SOFT_RESET);
  245. mdelay(5);
  246. /* take UVD block out of reset */
  247. WREG32_P(SRBM_SOFT_RESET, 0, ~SOFT_RESET_UVD);
  248. mdelay(5);
  249. /* initialize UVD memory controller */
  250. WREG32(UVD_LMI_CTRL, 0x40 | (1 << 8) | (1 << 13) |
  251. (1 << 21) | (1 << 9) | (1 << 20));
  252. #ifdef __BIG_ENDIAN
  253. /* swap (8 in 32) RB and IB */
  254. lmi_swap_cntl = 0xa;
  255. mp_swap_cntl = 0;
  256. #endif
  257. WREG32(UVD_LMI_SWAP_CNTL, lmi_swap_cntl);
  258. WREG32(UVD_MP_SWAP_CNTL, mp_swap_cntl);
  259. WREG32(UVD_MPC_SET_MUXA0, 0x40c2040);
  260. WREG32(UVD_MPC_SET_MUXA1, 0x0);
  261. WREG32(UVD_MPC_SET_MUXB0, 0x40c2040);
  262. WREG32(UVD_MPC_SET_MUXB1, 0x0);
  263. WREG32(UVD_MPC_SET_ALU, 0);
  264. WREG32(UVD_MPC_SET_MUX, 0x88);
  265. /* take all subblocks out of reset, except VCPU */
  266. WREG32(UVD_SOFT_RESET, VCPU_SOFT_RESET);
  267. mdelay(5);
  268. /* enable VCPU clock */
  269. WREG32(UVD_VCPU_CNTL, 1 << 9);
  270. /* enable UMC */
  271. WREG32_P(UVD_LMI_CTRL2, 0, ~(1 << 8));
  272. WREG32_P(UVD_RB_ARB_CTRL, 0, ~(1 << 3));
  273. /* boot up the VCPU */
  274. WREG32(UVD_SOFT_RESET, 0);
  275. mdelay(10);
  276. for (i = 0; i < 10; ++i) {
  277. uint32_t status;
  278. for (j = 0; j < 100; ++j) {
  279. status = RREG32(UVD_STATUS);
  280. if (status & 2)
  281. break;
  282. mdelay(10);
  283. }
  284. r = 0;
  285. if (status & 2)
  286. break;
  287. DRM_ERROR("UVD not responding, trying to reset the VCPU!!!\n");
  288. WREG32_P(UVD_SOFT_RESET, VCPU_SOFT_RESET, ~VCPU_SOFT_RESET);
  289. mdelay(10);
  290. WREG32_P(UVD_SOFT_RESET, 0, ~VCPU_SOFT_RESET);
  291. mdelay(10);
  292. r = -1;
  293. }
  294. if (r) {
  295. DRM_ERROR("UVD not responding, giving up!!!\n");
  296. return r;
  297. }
  298. /* enable interupt */
  299. WREG32_P(UVD_MASTINT_EN, 3<<1, ~(3 << 1));
  300. /* force RBC into idle state */
  301. WREG32(UVD_RBC_RB_CNTL, 0x11010101);
  302. /* Set the write pointer delay */
  303. WREG32(UVD_RBC_RB_WPTR_CNTL, 0);
  304. /* programm the 4GB memory segment for rptr and ring buffer */
  305. WREG32(UVD_LMI_EXT40_ADDR, upper_32_bits(ring->gpu_addr) |
  306. (0x7 << 16) | (0x1 << 31));
  307. /* Initialize the ring buffer's read and write pointers */
  308. WREG32(UVD_RBC_RB_RPTR, 0x0);
  309. ring->wptr = RREG32(UVD_RBC_RB_RPTR);
  310. WREG32(UVD_RBC_RB_WPTR, ring->wptr);
  311. /* set the ring address */
  312. WREG32(UVD_RBC_RB_BASE, ring->gpu_addr);
  313. /* Set ring buffer size */
  314. rb_bufsz = order_base_2(ring->ring_size);
  315. rb_bufsz = (0x1 << 8) | rb_bufsz;
  316. WREG32_P(UVD_RBC_RB_CNTL, rb_bufsz, ~0x11f1f);
  317. return 0;
  318. }
  319. /**
  320. * uvd_v1_0_stop - stop UVD block
  321. *
  322. * @rdev: radeon_device pointer
  323. *
  324. * stop the UVD block
  325. */
  326. void uvd_v1_0_stop(struct radeon_device *rdev)
  327. {
  328. /* force RBC into idle state */
  329. WREG32(UVD_RBC_RB_CNTL, 0x11010101);
  330. /* Stall UMC and register bus before resetting VCPU */
  331. WREG32_P(UVD_LMI_CTRL2, 1 << 8, ~(1 << 8));
  332. WREG32_P(UVD_RB_ARB_CTRL, 1 << 3, ~(1 << 3));
  333. mdelay(1);
  334. /* put VCPU into reset */
  335. WREG32(UVD_SOFT_RESET, VCPU_SOFT_RESET);
  336. mdelay(5);
  337. /* disable VCPU clock */
  338. WREG32(UVD_VCPU_CNTL, 0x0);
  339. /* Unstall UMC and register bus */
  340. WREG32_P(UVD_LMI_CTRL2, 0, ~(1 << 8));
  341. WREG32_P(UVD_RB_ARB_CTRL, 0, ~(1 << 3));
  342. }
  343. /**
  344. * uvd_v1_0_ring_test - register write test
  345. *
  346. * @rdev: radeon_device pointer
  347. * @ring: radeon_ring pointer
  348. *
  349. * Test if we can successfully write to the context register
  350. */
  351. int uvd_v1_0_ring_test(struct radeon_device *rdev, struct radeon_ring *ring)
  352. {
  353. uint32_t tmp = 0;
  354. unsigned i;
  355. int r;
  356. WREG32(UVD_CONTEXT_ID, 0xCAFEDEAD);
  357. r = radeon_ring_lock(rdev, ring, 3);
  358. if (r) {
  359. DRM_ERROR("radeon: cp failed to lock ring %d (%d).\n",
  360. ring->idx, r);
  361. return r;
  362. }
  363. radeon_ring_write(ring, PACKET0(UVD_CONTEXT_ID, 0));
  364. radeon_ring_write(ring, 0xDEADBEEF);
  365. radeon_ring_unlock_commit(rdev, ring, false);
  366. for (i = 0; i < rdev->usec_timeout; i++) {
  367. tmp = RREG32(UVD_CONTEXT_ID);
  368. if (tmp == 0xDEADBEEF)
  369. break;
  370. DRM_UDELAY(1);
  371. }
  372. if (i < rdev->usec_timeout) {
  373. DRM_INFO("ring test on %d succeeded in %d usecs\n",
  374. ring->idx, i);
  375. } else {
  376. DRM_ERROR("radeon: ring %d test failed (0x%08X)\n",
  377. ring->idx, tmp);
  378. r = -EINVAL;
  379. }
  380. return r;
  381. }
  382. /**
  383. * uvd_v1_0_semaphore_emit - emit semaphore command
  384. *
  385. * @rdev: radeon_device pointer
  386. * @ring: radeon_ring pointer
  387. * @semaphore: semaphore to emit commands for
  388. * @emit_wait: true if we should emit a wait command
  389. *
  390. * Emit a semaphore command (either wait or signal) to the UVD ring.
  391. */
  392. bool uvd_v1_0_semaphore_emit(struct radeon_device *rdev,
  393. struct radeon_ring *ring,
  394. struct radeon_semaphore *semaphore,
  395. bool emit_wait)
  396. {
  397. /* disable semaphores for UVD V1 hardware */
  398. return false;
  399. }
  400. /**
  401. * uvd_v1_0_ib_execute - execute indirect buffer
  402. *
  403. * @rdev: radeon_device pointer
  404. * @ib: indirect buffer to execute
  405. *
  406. * Write ring commands to execute the indirect buffer
  407. */
  408. void uvd_v1_0_ib_execute(struct radeon_device *rdev, struct radeon_ib *ib)
  409. {
  410. struct radeon_ring *ring = &rdev->ring[ib->ring];
  411. radeon_ring_write(ring, PACKET0(UVD_RBC_IB_BASE, 0));
  412. radeon_ring_write(ring, ib->gpu_addr);
  413. radeon_ring_write(ring, PACKET0(UVD_RBC_IB_SIZE, 0));
  414. radeon_ring_write(ring, ib->length_dw);
  415. }
  416. /**
  417. * uvd_v1_0_ib_test - test ib execution
  418. *
  419. * @rdev: radeon_device pointer
  420. * @ring: radeon_ring pointer
  421. *
  422. * Test if we can successfully execute an IB
  423. */
  424. int uvd_v1_0_ib_test(struct radeon_device *rdev, struct radeon_ring *ring)
  425. {
  426. struct radeon_fence *fence = NULL;
  427. int r;
  428. if (rdev->family < CHIP_RV740)
  429. r = radeon_set_uvd_clocks(rdev, 10000, 10000);
  430. else
  431. r = radeon_set_uvd_clocks(rdev, 53300, 40000);
  432. if (r) {
  433. DRM_ERROR("radeon: failed to raise UVD clocks (%d).\n", r);
  434. return r;
  435. }
  436. r = radeon_uvd_get_create_msg(rdev, ring->idx, 1, NULL);
  437. if (r) {
  438. DRM_ERROR("radeon: failed to get create msg (%d).\n", r);
  439. goto error;
  440. }
  441. r = radeon_uvd_get_destroy_msg(rdev, ring->idx, 1, &fence);
  442. if (r) {
  443. DRM_ERROR("radeon: failed to get destroy ib (%d).\n", r);
  444. goto error;
  445. }
  446. r = radeon_fence_wait(fence, false);
  447. if (r) {
  448. DRM_ERROR("radeon: fence wait failed (%d).\n", r);
  449. goto error;
  450. }
  451. DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
  452. error:
  453. radeon_fence_unref(&fence);
  454. radeon_set_uvd_clocks(rdev, 0, 0);
  455. return r;
  456. }