uvd_v1_0.c 13 KB

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