vce_v1_0.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * Copyright 2013 Advanced Micro Devices, Inc.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sub license, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  16. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  17. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  18. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  19. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. * The above copyright notice and this permission notice (including the
  22. * next paragraph) shall be included in all copies or substantial portions
  23. * of the Software.
  24. *
  25. * Authors: Christian König <christian.koenig@amd.com>
  26. */
  27. #include <linux/firmware.h>
  28. #include <drm/drmP.h>
  29. #include "radeon.h"
  30. #include "radeon_asic.h"
  31. #include "sid.h"
  32. #define VCE_V1_0_FW_SIZE (256 * 1024)
  33. #define VCE_V1_0_STACK_SIZE (64 * 1024)
  34. #define VCE_V1_0_DATA_SIZE (7808 * (RADEON_MAX_VCE_HANDLES + 1))
  35. struct vce_v1_0_fw_signature
  36. {
  37. int32_t off;
  38. uint32_t len;
  39. int32_t num;
  40. struct {
  41. uint32_t chip_id;
  42. uint32_t keyselect;
  43. uint32_t nonce[4];
  44. uint32_t sigval[4];
  45. } val[8];
  46. };
  47. /**
  48. * vce_v1_0_get_rptr - get read pointer
  49. *
  50. * @rdev: radeon_device pointer
  51. * @ring: radeon_ring pointer
  52. *
  53. * Returns the current hardware read pointer
  54. */
  55. uint32_t vce_v1_0_get_rptr(struct radeon_device *rdev,
  56. struct radeon_ring *ring)
  57. {
  58. if (ring->idx == TN_RING_TYPE_VCE1_INDEX)
  59. return RREG32(VCE_RB_RPTR);
  60. else
  61. return RREG32(VCE_RB_RPTR2);
  62. }
  63. /**
  64. * vce_v1_0_get_wptr - get write pointer
  65. *
  66. * @rdev: radeon_device pointer
  67. * @ring: radeon_ring pointer
  68. *
  69. * Returns the current hardware write pointer
  70. */
  71. uint32_t vce_v1_0_get_wptr(struct radeon_device *rdev,
  72. struct radeon_ring *ring)
  73. {
  74. if (ring->idx == TN_RING_TYPE_VCE1_INDEX)
  75. return RREG32(VCE_RB_WPTR);
  76. else
  77. return RREG32(VCE_RB_WPTR2);
  78. }
  79. /**
  80. * vce_v1_0_set_wptr - set write pointer
  81. *
  82. * @rdev: radeon_device pointer
  83. * @ring: radeon_ring pointer
  84. *
  85. * Commits the write pointer to the hardware
  86. */
  87. void vce_v1_0_set_wptr(struct radeon_device *rdev,
  88. struct radeon_ring *ring)
  89. {
  90. if (ring->idx == TN_RING_TYPE_VCE1_INDEX)
  91. WREG32(VCE_RB_WPTR, ring->wptr);
  92. else
  93. WREG32(VCE_RB_WPTR2, ring->wptr);
  94. }
  95. void vce_v1_0_enable_mgcg(struct radeon_device *rdev, bool enable)
  96. {
  97. u32 tmp;
  98. if (enable && (rdev->cg_flags & RADEON_CG_SUPPORT_VCE_MGCG)) {
  99. tmp = RREG32(VCE_CLOCK_GATING_A);
  100. tmp |= CGC_DYN_CLOCK_MODE;
  101. WREG32(VCE_CLOCK_GATING_A, tmp);
  102. tmp = RREG32(VCE_UENC_CLOCK_GATING);
  103. tmp &= ~0x1ff000;
  104. tmp |= 0xff800000;
  105. WREG32(VCE_UENC_CLOCK_GATING, tmp);
  106. tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
  107. tmp &= ~0x3ff;
  108. WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
  109. } else {
  110. tmp = RREG32(VCE_CLOCK_GATING_A);
  111. tmp &= ~CGC_DYN_CLOCK_MODE;
  112. WREG32(VCE_CLOCK_GATING_A, tmp);
  113. tmp = RREG32(VCE_UENC_CLOCK_GATING);
  114. tmp |= 0x1ff000;
  115. tmp &= ~0xff800000;
  116. WREG32(VCE_UENC_CLOCK_GATING, tmp);
  117. tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
  118. tmp |= 0x3ff;
  119. WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
  120. }
  121. }
  122. static void vce_v1_0_init_cg(struct radeon_device *rdev)
  123. {
  124. u32 tmp;
  125. tmp = RREG32(VCE_CLOCK_GATING_A);
  126. tmp |= CGC_DYN_CLOCK_MODE;
  127. WREG32(VCE_CLOCK_GATING_A, tmp);
  128. tmp = RREG32(VCE_CLOCK_GATING_B);
  129. tmp |= 0x1e;
  130. tmp &= ~0xe100e1;
  131. WREG32(VCE_CLOCK_GATING_B, tmp);
  132. tmp = RREG32(VCE_UENC_CLOCK_GATING);
  133. tmp &= ~0xff9ff000;
  134. WREG32(VCE_UENC_CLOCK_GATING, tmp);
  135. tmp = RREG32(VCE_UENC_REG_CLOCK_GATING);
  136. tmp &= ~0x3ff;
  137. WREG32(VCE_UENC_REG_CLOCK_GATING, tmp);
  138. }
  139. int vce_v1_0_load_fw(struct radeon_device *rdev, uint32_t *data)
  140. {
  141. struct vce_v1_0_fw_signature *sign = (void*)rdev->vce_fw->data;
  142. uint32_t chip_id;
  143. int i;
  144. switch (rdev->family) {
  145. case CHIP_TAHITI:
  146. chip_id = 0x01000014;
  147. break;
  148. case CHIP_VERDE:
  149. chip_id = 0x01000015;
  150. break;
  151. case CHIP_PITCAIRN:
  152. case CHIP_OLAND:
  153. chip_id = 0x01000016;
  154. break;
  155. case CHIP_ARUBA:
  156. chip_id = 0x01000017;
  157. break;
  158. default:
  159. return -EINVAL;
  160. }
  161. for (i = 0; i < le32_to_cpu(sign->num); ++i) {
  162. if (le32_to_cpu(sign->val[i].chip_id) == chip_id)
  163. break;
  164. }
  165. if (i == le32_to_cpu(sign->num))
  166. return -EINVAL;
  167. data += (256 - 64) / 4;
  168. data[0] = sign->val[i].nonce[0];
  169. data[1] = sign->val[i].nonce[1];
  170. data[2] = sign->val[i].nonce[2];
  171. data[3] = sign->val[i].nonce[3];
  172. data[4] = cpu_to_le32(le32_to_cpu(sign->len) + 64);
  173. memset(&data[5], 0, 44);
  174. memcpy(&data[16], &sign[1], rdev->vce_fw->size - sizeof(*sign));
  175. data += le32_to_cpu(data[4]) / 4;
  176. data[0] = sign->val[i].sigval[0];
  177. data[1] = sign->val[i].sigval[1];
  178. data[2] = sign->val[i].sigval[2];
  179. data[3] = sign->val[i].sigval[3];
  180. rdev->vce.keyselect = le32_to_cpu(sign->val[i].keyselect);
  181. return 0;
  182. }
  183. unsigned vce_v1_0_bo_size(struct radeon_device *rdev)
  184. {
  185. WARN_ON(VCE_V1_0_FW_SIZE < rdev->vce_fw->size);
  186. return VCE_V1_0_FW_SIZE + VCE_V1_0_STACK_SIZE + VCE_V1_0_DATA_SIZE;
  187. }
  188. int vce_v1_0_resume(struct radeon_device *rdev)
  189. {
  190. uint64_t addr = rdev->vce.gpu_addr;
  191. uint32_t size;
  192. int i;
  193. WREG32_P(VCE_CLOCK_GATING_A, 0, ~(1 << 16));
  194. WREG32_P(VCE_UENC_CLOCK_GATING, 0x1FF000, ~0xFF9FF000);
  195. WREG32_P(VCE_UENC_REG_CLOCK_GATING, 0x3F, ~0x3F);
  196. WREG32(VCE_CLOCK_GATING_B, 0);
  197. WREG32_P(VCE_LMI_FW_PERIODIC_CTRL, 0x4, ~0x4);
  198. WREG32(VCE_LMI_CTRL, 0x00398000);
  199. WREG32_P(VCE_LMI_CACHE_CTRL, 0x0, ~0x1);
  200. WREG32(VCE_LMI_SWAP_CNTL, 0);
  201. WREG32(VCE_LMI_SWAP_CNTL1, 0);
  202. WREG32(VCE_LMI_VM_CTRL, 0);
  203. WREG32(VCE_VCPU_SCRATCH7, RADEON_MAX_VCE_HANDLES);
  204. addr += 256;
  205. size = VCE_V1_0_FW_SIZE;
  206. WREG32(VCE_VCPU_CACHE_OFFSET0, addr & 0x7fffffff);
  207. WREG32(VCE_VCPU_CACHE_SIZE0, size);
  208. addr += size;
  209. size = VCE_V1_0_STACK_SIZE;
  210. WREG32(VCE_VCPU_CACHE_OFFSET1, addr & 0x7fffffff);
  211. WREG32(VCE_VCPU_CACHE_SIZE1, size);
  212. addr += size;
  213. size = VCE_V1_0_DATA_SIZE;
  214. WREG32(VCE_VCPU_CACHE_OFFSET2, addr & 0x7fffffff);
  215. WREG32(VCE_VCPU_CACHE_SIZE2, size);
  216. WREG32_P(VCE_LMI_CTRL2, 0x0, ~0x100);
  217. WREG32(VCE_LMI_FW_START_KEYSEL, rdev->vce.keyselect);
  218. for (i = 0; i < 10; ++i) {
  219. mdelay(10);
  220. if (RREG32(VCE_FW_REG_STATUS) & VCE_FW_REG_STATUS_DONE)
  221. break;
  222. }
  223. if (i == 10)
  224. return -ETIMEDOUT;
  225. if (!(RREG32(VCE_FW_REG_STATUS) & VCE_FW_REG_STATUS_PASS))
  226. return -EINVAL;
  227. for (i = 0; i < 10; ++i) {
  228. mdelay(10);
  229. if (!(RREG32(VCE_FW_REG_STATUS) & VCE_FW_REG_STATUS_BUSY))
  230. break;
  231. }
  232. if (i == 10)
  233. return -ETIMEDOUT;
  234. vce_v1_0_init_cg(rdev);
  235. return 0;
  236. }
  237. /**
  238. * vce_v1_0_start - start VCE block
  239. *
  240. * @rdev: radeon_device pointer
  241. *
  242. * Setup and start the VCE block
  243. */
  244. int vce_v1_0_start(struct radeon_device *rdev)
  245. {
  246. struct radeon_ring *ring;
  247. int i, j, r;
  248. /* set BUSY flag */
  249. WREG32_P(VCE_STATUS, 1, ~1);
  250. ring = &rdev->ring[TN_RING_TYPE_VCE1_INDEX];
  251. WREG32(VCE_RB_RPTR, ring->wptr);
  252. WREG32(VCE_RB_WPTR, ring->wptr);
  253. WREG32(VCE_RB_BASE_LO, ring->gpu_addr);
  254. WREG32(VCE_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
  255. WREG32(VCE_RB_SIZE, ring->ring_size / 4);
  256. ring = &rdev->ring[TN_RING_TYPE_VCE2_INDEX];
  257. WREG32(VCE_RB_RPTR2, ring->wptr);
  258. WREG32(VCE_RB_WPTR2, ring->wptr);
  259. WREG32(VCE_RB_BASE_LO2, ring->gpu_addr);
  260. WREG32(VCE_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
  261. WREG32(VCE_RB_SIZE2, ring->ring_size / 4);
  262. WREG32_P(VCE_VCPU_CNTL, VCE_CLK_EN, ~VCE_CLK_EN);
  263. WREG32_P(VCE_SOFT_RESET,
  264. VCE_ECPU_SOFT_RESET |
  265. VCE_FME_SOFT_RESET, ~(
  266. VCE_ECPU_SOFT_RESET |
  267. VCE_FME_SOFT_RESET));
  268. mdelay(100);
  269. WREG32_P(VCE_SOFT_RESET, 0, ~(
  270. VCE_ECPU_SOFT_RESET |
  271. VCE_FME_SOFT_RESET));
  272. for (i = 0; i < 10; ++i) {
  273. uint32_t status;
  274. for (j = 0; j < 100; ++j) {
  275. status = RREG32(VCE_STATUS);
  276. if (status & 2)
  277. break;
  278. mdelay(10);
  279. }
  280. r = 0;
  281. if (status & 2)
  282. break;
  283. DRM_ERROR("VCE not responding, trying to reset the ECPU!!!\n");
  284. WREG32_P(VCE_SOFT_RESET, VCE_ECPU_SOFT_RESET, ~VCE_ECPU_SOFT_RESET);
  285. mdelay(10);
  286. WREG32_P(VCE_SOFT_RESET, 0, ~VCE_ECPU_SOFT_RESET);
  287. mdelay(10);
  288. r = -1;
  289. }
  290. /* clear BUSY flag */
  291. WREG32_P(VCE_STATUS, 0, ~1);
  292. if (r) {
  293. DRM_ERROR("VCE not responding, giving up!!!\n");
  294. return r;
  295. }
  296. return 0;
  297. }
  298. int vce_v1_0_init(struct radeon_device *rdev)
  299. {
  300. struct radeon_ring *ring;
  301. int r;
  302. r = vce_v1_0_start(rdev);
  303. if (r)
  304. return r;
  305. ring = &rdev->ring[TN_RING_TYPE_VCE1_INDEX];
  306. ring->ready = true;
  307. r = radeon_ring_test(rdev, TN_RING_TYPE_VCE1_INDEX, ring);
  308. if (r) {
  309. ring->ready = false;
  310. return r;
  311. }
  312. ring = &rdev->ring[TN_RING_TYPE_VCE2_INDEX];
  313. ring->ready = true;
  314. r = radeon_ring_test(rdev, TN_RING_TYPE_VCE2_INDEX, ring);
  315. if (r) {
  316. ring->ready = false;
  317. return r;
  318. }
  319. DRM_INFO("VCE initialized successfully.\n");
  320. return 0;
  321. }