vcn_v1_0.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /*
  2. * Copyright 2016 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. */
  23. #include <linux/firmware.h>
  24. #include <drm/drmP.h>
  25. #include "amdgpu.h"
  26. #include "amdgpu_vcn.h"
  27. #include "soc15d.h"
  28. #include "soc15_common.h"
  29. #include "vega10/soc15ip.h"
  30. #include "raven1/VCN/vcn_1_0_offset.h"
  31. #include "raven1/VCN/vcn_1_0_sh_mask.h"
  32. #include "vega10/HDP/hdp_4_0_offset.h"
  33. #include "raven1/MMHUB/mmhub_9_1_offset.h"
  34. #include "raven1/MMHUB/mmhub_9_1_sh_mask.h"
  35. static int vcn_v1_0_start(struct amdgpu_device *adev);
  36. static int vcn_v1_0_stop(struct amdgpu_device *adev);
  37. static void vcn_v1_0_set_dec_ring_funcs(struct amdgpu_device *adev);
  38. static void vcn_v1_0_set_enc_ring_funcs(struct amdgpu_device *adev);
  39. static void vcn_v1_0_set_irq_funcs(struct amdgpu_device *adev);
  40. /**
  41. * vcn_v1_0_early_init - set function pointers
  42. *
  43. * @handle: amdgpu_device pointer
  44. *
  45. * Set ring and irq function pointers
  46. */
  47. static int vcn_v1_0_early_init(void *handle)
  48. {
  49. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  50. adev->vcn.num_enc_rings = 2;
  51. vcn_v1_0_set_dec_ring_funcs(adev);
  52. vcn_v1_0_set_enc_ring_funcs(adev);
  53. vcn_v1_0_set_irq_funcs(adev);
  54. return 0;
  55. }
  56. /**
  57. * vcn_v1_0_sw_init - sw init for VCN block
  58. *
  59. * @handle: amdgpu_device pointer
  60. *
  61. * Load firmware and sw initialization
  62. */
  63. static int vcn_v1_0_sw_init(void *handle)
  64. {
  65. struct amdgpu_ring *ring;
  66. int i, r;
  67. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  68. /* VCN DEC TRAP */
  69. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_VCN, 124, &adev->vcn.irq);
  70. if (r)
  71. return r;
  72. /* VCN ENC TRAP */
  73. for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
  74. r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_VCN, i + 119,
  75. &adev->vcn.irq);
  76. if (r)
  77. return r;
  78. }
  79. r = amdgpu_vcn_sw_init(adev);
  80. if (r)
  81. return r;
  82. if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
  83. const struct common_firmware_header *hdr;
  84. hdr = (const struct common_firmware_header *)adev->vcn.fw->data;
  85. adev->firmware.ucode[AMDGPU_UCODE_ID_VCN].ucode_id = AMDGPU_UCODE_ID_VCN;
  86. adev->firmware.ucode[AMDGPU_UCODE_ID_VCN].fw = adev->vcn.fw;
  87. adev->firmware.fw_size +=
  88. ALIGN(le32_to_cpu(hdr->ucode_size_bytes), PAGE_SIZE);
  89. DRM_INFO("PSP loading VCN firmware\n");
  90. }
  91. r = amdgpu_vcn_resume(adev);
  92. if (r)
  93. return r;
  94. ring = &adev->vcn.ring_dec;
  95. sprintf(ring->name, "vcn_dec");
  96. r = amdgpu_ring_init(adev, ring, 512, &adev->vcn.irq, 0);
  97. if (r)
  98. return r;
  99. for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
  100. ring = &adev->vcn.ring_enc[i];
  101. sprintf(ring->name, "vcn_enc%d", i);
  102. r = amdgpu_ring_init(adev, ring, 512, &adev->vcn.irq, 0);
  103. if (r)
  104. return r;
  105. }
  106. return r;
  107. }
  108. /**
  109. * vcn_v1_0_sw_fini - sw fini for VCN block
  110. *
  111. * @handle: amdgpu_device pointer
  112. *
  113. * VCN suspend and free up sw allocation
  114. */
  115. static int vcn_v1_0_sw_fini(void *handle)
  116. {
  117. int r;
  118. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  119. r = amdgpu_vcn_suspend(adev);
  120. if (r)
  121. return r;
  122. r = amdgpu_vcn_sw_fini(adev);
  123. return r;
  124. }
  125. /**
  126. * vcn_v1_0_hw_init - start and test VCN block
  127. *
  128. * @handle: amdgpu_device pointer
  129. *
  130. * Initialize the hardware, boot up the VCPU and do some testing
  131. */
  132. static int vcn_v1_0_hw_init(void *handle)
  133. {
  134. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  135. struct amdgpu_ring *ring = &adev->vcn.ring_dec;
  136. int i, r;
  137. r = vcn_v1_0_start(adev);
  138. if (r)
  139. goto done;
  140. ring->ready = true;
  141. r = amdgpu_ring_test_ring(ring);
  142. if (r) {
  143. ring->ready = false;
  144. goto done;
  145. }
  146. for (i = 0; i < adev->vcn.num_enc_rings; ++i) {
  147. ring = &adev->vcn.ring_enc[i];
  148. ring->ready = true;
  149. r = amdgpu_ring_test_ring(ring);
  150. if (r) {
  151. ring->ready = false;
  152. goto done;
  153. }
  154. }
  155. done:
  156. if (!r)
  157. DRM_INFO("VCN decode and encode initialized successfully.\n");
  158. return r;
  159. }
  160. /**
  161. * vcn_v1_0_hw_fini - stop the hardware block
  162. *
  163. * @handle: amdgpu_device pointer
  164. *
  165. * Stop the VCN block, mark ring as not ready any more
  166. */
  167. static int vcn_v1_0_hw_fini(void *handle)
  168. {
  169. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  170. struct amdgpu_ring *ring = &adev->vcn.ring_dec;
  171. int r;
  172. r = vcn_v1_0_stop(adev);
  173. if (r)
  174. return r;
  175. ring->ready = false;
  176. return 0;
  177. }
  178. /**
  179. * vcn_v1_0_suspend - suspend VCN block
  180. *
  181. * @handle: amdgpu_device pointer
  182. *
  183. * HW fini and suspend VCN block
  184. */
  185. static int vcn_v1_0_suspend(void *handle)
  186. {
  187. int r;
  188. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  189. r = vcn_v1_0_hw_fini(adev);
  190. if (r)
  191. return r;
  192. r = amdgpu_vcn_suspend(adev);
  193. return r;
  194. }
  195. /**
  196. * vcn_v1_0_resume - resume VCN block
  197. *
  198. * @handle: amdgpu_device pointer
  199. *
  200. * Resume firmware and hw init VCN block
  201. */
  202. static int vcn_v1_0_resume(void *handle)
  203. {
  204. int r;
  205. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  206. r = amdgpu_vcn_resume(adev);
  207. if (r)
  208. return r;
  209. r = vcn_v1_0_hw_init(adev);
  210. return r;
  211. }
  212. /**
  213. * vcn_v1_0_mc_resume - memory controller programming
  214. *
  215. * @adev: amdgpu_device pointer
  216. *
  217. * Let the VCN memory controller know it's offsets
  218. */
  219. static void vcn_v1_0_mc_resume(struct amdgpu_device *adev)
  220. {
  221. uint32_t size = AMDGPU_GPU_PAGE_ALIGN(adev->vcn.fw->size + 4);
  222. uint32_t offset;
  223. if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
  224. WREG32_SOC15(UVD, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
  225. (adev->firmware.ucode[AMDGPU_UCODE_ID_VCN].tmr_mc_addr_lo));
  226. WREG32_SOC15(UVD, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
  227. (adev->firmware.ucode[AMDGPU_UCODE_ID_VCN].tmr_mc_addr_hi));
  228. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CACHE_OFFSET0, 0);
  229. offset = 0;
  230. } else {
  231. WREG32_SOC15(UVD, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_LOW,
  232. lower_32_bits(adev->vcn.gpu_addr));
  233. WREG32_SOC15(UVD, 0, mmUVD_LMI_VCPU_CACHE_64BIT_BAR_HIGH,
  234. upper_32_bits(adev->vcn.gpu_addr));
  235. offset = size;
  236. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CACHE_OFFSET0,
  237. AMDGPU_UVD_FIRMWARE_OFFSET >> 3);
  238. }
  239. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CACHE_SIZE0, size);
  240. WREG32_SOC15(UVD, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_LOW,
  241. lower_32_bits(adev->vcn.gpu_addr + offset));
  242. WREG32_SOC15(UVD, 0, mmUVD_LMI_VCPU_CACHE1_64BIT_BAR_HIGH,
  243. upper_32_bits(adev->vcn.gpu_addr + offset));
  244. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CACHE_OFFSET1, 0);
  245. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CACHE_SIZE1, AMDGPU_VCN_HEAP_SIZE);
  246. WREG32_SOC15(UVD, 0, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_LOW,
  247. lower_32_bits(adev->vcn.gpu_addr + offset + AMDGPU_VCN_HEAP_SIZE));
  248. WREG32_SOC15(UVD, 0, mmUVD_LMI_VCPU_CACHE2_64BIT_BAR_HIGH,
  249. upper_32_bits(adev->vcn.gpu_addr + offset + AMDGPU_VCN_HEAP_SIZE));
  250. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CACHE_OFFSET2, 0);
  251. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CACHE_SIZE2,
  252. AMDGPU_VCN_STACK_SIZE + (AMDGPU_VCN_SESSION_SIZE * 40));
  253. WREG32_SOC15(UVD, 0, mmUVD_UDEC_ADDR_CONFIG,
  254. adev->gfx.config.gb_addr_config);
  255. WREG32_SOC15(UVD, 0, mmUVD_UDEC_DB_ADDR_CONFIG,
  256. adev->gfx.config.gb_addr_config);
  257. WREG32_SOC15(UVD, 0, mmUVD_UDEC_DBW_ADDR_CONFIG,
  258. adev->gfx.config.gb_addr_config);
  259. }
  260. /**
  261. * vcn_v1_0_disable_clock_gating - disable VCN clock gating
  262. *
  263. * @adev: amdgpu_device pointer
  264. * @sw: enable SW clock gating
  265. *
  266. * Disable clock gating for VCN block
  267. */
  268. static void vcn_v1_0_disable_clock_gating(struct amdgpu_device *adev, bool sw)
  269. {
  270. uint32_t data;
  271. /* JPEG disable CGC */
  272. data = RREG32_SOC15(VCN, 0, mmJPEG_CGC_CTRL);
  273. if (sw)
  274. data |= 1 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
  275. else
  276. data &= ~JPEG_CGC_CTRL__DYN_CLOCK_MODE_MASK;
  277. data |= 1 << JPEG_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
  278. data |= 4 << JPEG_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
  279. WREG32_SOC15(VCN, 0, mmJPEG_CGC_CTRL, data);
  280. data = RREG32_SOC15(VCN, 0, mmJPEG_CGC_GATE);
  281. data &= ~(JPEG_CGC_GATE__JPEG_MASK | JPEG_CGC_GATE__JPEG2_MASK);
  282. WREG32_SOC15(VCN, 0, mmJPEG_CGC_GATE, data);
  283. /* UVD disable CGC */
  284. data = RREG32_SOC15(VCN, 0, mmUVD_CGC_CTRL);
  285. if (sw)
  286. data |= 1 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
  287. else
  288. data &= ~ UVD_CGC_CTRL__DYN_CLOCK_MODE_MASK;
  289. data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
  290. data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
  291. WREG32_SOC15(VCN, 0, mmUVD_CGC_CTRL, data);
  292. data = RREG32_SOC15(VCN, 0, mmUVD_CGC_GATE);
  293. data &= ~(UVD_CGC_GATE__SYS_MASK
  294. | UVD_CGC_GATE__UDEC_MASK
  295. | UVD_CGC_GATE__MPEG2_MASK
  296. | UVD_CGC_GATE__REGS_MASK
  297. | UVD_CGC_GATE__RBC_MASK
  298. | UVD_CGC_GATE__LMI_MC_MASK
  299. | UVD_CGC_GATE__LMI_UMC_MASK
  300. | UVD_CGC_GATE__IDCT_MASK
  301. | UVD_CGC_GATE__MPRD_MASK
  302. | UVD_CGC_GATE__MPC_MASK
  303. | UVD_CGC_GATE__LBSI_MASK
  304. | UVD_CGC_GATE__LRBBM_MASK
  305. | UVD_CGC_GATE__UDEC_RE_MASK
  306. | UVD_CGC_GATE__UDEC_CM_MASK
  307. | UVD_CGC_GATE__UDEC_IT_MASK
  308. | UVD_CGC_GATE__UDEC_DB_MASK
  309. | UVD_CGC_GATE__UDEC_MP_MASK
  310. | UVD_CGC_GATE__WCB_MASK
  311. | UVD_CGC_GATE__VCPU_MASK
  312. | UVD_CGC_GATE__SCPU_MASK);
  313. WREG32_SOC15(VCN, 0, mmUVD_CGC_GATE, data);
  314. data = RREG32_SOC15(VCN, 0, mmUVD_CGC_CTRL);
  315. data &= ~(UVD_CGC_CTRL__UDEC_RE_MODE_MASK
  316. | UVD_CGC_CTRL__UDEC_CM_MODE_MASK
  317. | UVD_CGC_CTRL__UDEC_IT_MODE_MASK
  318. | UVD_CGC_CTRL__UDEC_DB_MODE_MASK
  319. | UVD_CGC_CTRL__UDEC_MP_MODE_MASK
  320. | UVD_CGC_CTRL__SYS_MODE_MASK
  321. | UVD_CGC_CTRL__UDEC_MODE_MASK
  322. | UVD_CGC_CTRL__MPEG2_MODE_MASK
  323. | UVD_CGC_CTRL__REGS_MODE_MASK
  324. | UVD_CGC_CTRL__RBC_MODE_MASK
  325. | UVD_CGC_CTRL__LMI_MC_MODE_MASK
  326. | UVD_CGC_CTRL__LMI_UMC_MODE_MASK
  327. | UVD_CGC_CTRL__IDCT_MODE_MASK
  328. | UVD_CGC_CTRL__MPRD_MODE_MASK
  329. | UVD_CGC_CTRL__MPC_MODE_MASK
  330. | UVD_CGC_CTRL__LBSI_MODE_MASK
  331. | UVD_CGC_CTRL__LRBBM_MODE_MASK
  332. | UVD_CGC_CTRL__WCB_MODE_MASK
  333. | UVD_CGC_CTRL__VCPU_MODE_MASK
  334. | UVD_CGC_CTRL__SCPU_MODE_MASK);
  335. WREG32_SOC15(VCN, 0, mmUVD_CGC_CTRL, data);
  336. /* turn on */
  337. data = RREG32_SOC15(VCN, 0, mmUVD_SUVD_CGC_GATE);
  338. data |= (UVD_SUVD_CGC_GATE__SRE_MASK
  339. | UVD_SUVD_CGC_GATE__SIT_MASK
  340. | UVD_SUVD_CGC_GATE__SMP_MASK
  341. | UVD_SUVD_CGC_GATE__SCM_MASK
  342. | UVD_SUVD_CGC_GATE__SDB_MASK
  343. | UVD_SUVD_CGC_GATE__SRE_H264_MASK
  344. | UVD_SUVD_CGC_GATE__SRE_HEVC_MASK
  345. | UVD_SUVD_CGC_GATE__SIT_H264_MASK
  346. | UVD_SUVD_CGC_GATE__SIT_HEVC_MASK
  347. | UVD_SUVD_CGC_GATE__SCM_H264_MASK
  348. | UVD_SUVD_CGC_GATE__SCM_HEVC_MASK
  349. | UVD_SUVD_CGC_GATE__SDB_H264_MASK
  350. | UVD_SUVD_CGC_GATE__SDB_HEVC_MASK
  351. | UVD_SUVD_CGC_GATE__SCLR_MASK
  352. | UVD_SUVD_CGC_GATE__UVD_SC_MASK
  353. | UVD_SUVD_CGC_GATE__ENT_MASK
  354. | UVD_SUVD_CGC_GATE__SIT_HEVC_DEC_MASK
  355. | UVD_SUVD_CGC_GATE__SIT_HEVC_ENC_MASK
  356. | UVD_SUVD_CGC_GATE__SITE_MASK
  357. | UVD_SUVD_CGC_GATE__SRE_VP9_MASK
  358. | UVD_SUVD_CGC_GATE__SCM_VP9_MASK
  359. | UVD_SUVD_CGC_GATE__SIT_VP9_DEC_MASK
  360. | UVD_SUVD_CGC_GATE__SDB_VP9_MASK
  361. | UVD_SUVD_CGC_GATE__IME_HEVC_MASK);
  362. WREG32_SOC15(VCN, 0, mmUVD_SUVD_CGC_GATE, data);
  363. data = RREG32_SOC15(VCN, 0, mmUVD_SUVD_CGC_CTRL);
  364. data &= ~(UVD_SUVD_CGC_CTRL__SRE_MODE_MASK
  365. | UVD_SUVD_CGC_CTRL__SIT_MODE_MASK
  366. | UVD_SUVD_CGC_CTRL__SMP_MODE_MASK
  367. | UVD_SUVD_CGC_CTRL__SCM_MODE_MASK
  368. | UVD_SUVD_CGC_CTRL__SDB_MODE_MASK
  369. | UVD_SUVD_CGC_CTRL__SCLR_MODE_MASK
  370. | UVD_SUVD_CGC_CTRL__UVD_SC_MODE_MASK
  371. | UVD_SUVD_CGC_CTRL__ENT_MODE_MASK
  372. | UVD_SUVD_CGC_CTRL__IME_MODE_MASK
  373. | UVD_SUVD_CGC_CTRL__SITE_MODE_MASK);
  374. WREG32_SOC15(VCN, 0, mmUVD_SUVD_CGC_CTRL, data);
  375. }
  376. /**
  377. * vcn_v1_0_enable_clock_gating - enable VCN clock gating
  378. *
  379. * @adev: amdgpu_device pointer
  380. * @sw: enable SW clock gating
  381. *
  382. * Enable clock gating for VCN block
  383. */
  384. static void vcn_v1_0_enable_clock_gating(struct amdgpu_device *adev, bool sw)
  385. {
  386. uint32_t data = 0;
  387. /* enable JPEG CGC */
  388. data = RREG32_SOC15(VCN, 0, mmJPEG_CGC_CTRL);
  389. if (sw)
  390. data |= 1 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
  391. else
  392. data |= 0 << JPEG_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
  393. data |= 1 << JPEG_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
  394. data |= 4 << JPEG_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
  395. WREG32_SOC15(VCN, 0, mmJPEG_CGC_CTRL, data);
  396. data = RREG32_SOC15(VCN, 0, mmJPEG_CGC_GATE);
  397. data |= (JPEG_CGC_GATE__JPEG_MASK | JPEG_CGC_GATE__JPEG2_MASK);
  398. WREG32_SOC15(VCN, 0, mmJPEG_CGC_GATE, data);
  399. /* enable UVD CGC */
  400. data = RREG32_SOC15(VCN, 0, mmUVD_CGC_CTRL);
  401. if (sw)
  402. data |= 1 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
  403. else
  404. data |= 0 << UVD_CGC_CTRL__DYN_CLOCK_MODE__SHIFT;
  405. data |= 1 << UVD_CGC_CTRL__CLK_GATE_DLY_TIMER__SHIFT;
  406. data |= 4 << UVD_CGC_CTRL__CLK_OFF_DELAY__SHIFT;
  407. WREG32_SOC15(VCN, 0, mmUVD_CGC_CTRL, data);
  408. data = RREG32_SOC15(VCN, 0, mmUVD_CGC_CTRL);
  409. data |= (UVD_CGC_CTRL__UDEC_RE_MODE_MASK
  410. | UVD_CGC_CTRL__UDEC_CM_MODE_MASK
  411. | UVD_CGC_CTRL__UDEC_IT_MODE_MASK
  412. | UVD_CGC_CTRL__UDEC_DB_MODE_MASK
  413. | UVD_CGC_CTRL__UDEC_MP_MODE_MASK
  414. | UVD_CGC_CTRL__SYS_MODE_MASK
  415. | UVD_CGC_CTRL__UDEC_MODE_MASK
  416. | UVD_CGC_CTRL__MPEG2_MODE_MASK
  417. | UVD_CGC_CTRL__REGS_MODE_MASK
  418. | UVD_CGC_CTRL__RBC_MODE_MASK
  419. | UVD_CGC_CTRL__LMI_MC_MODE_MASK
  420. | UVD_CGC_CTRL__LMI_UMC_MODE_MASK
  421. | UVD_CGC_CTRL__IDCT_MODE_MASK
  422. | UVD_CGC_CTRL__MPRD_MODE_MASK
  423. | UVD_CGC_CTRL__MPC_MODE_MASK
  424. | UVD_CGC_CTRL__LBSI_MODE_MASK
  425. | UVD_CGC_CTRL__LRBBM_MODE_MASK
  426. | UVD_CGC_CTRL__WCB_MODE_MASK
  427. | UVD_CGC_CTRL__VCPU_MODE_MASK
  428. | UVD_CGC_CTRL__SCPU_MODE_MASK);
  429. WREG32_SOC15(VCN, 0, mmUVD_CGC_CTRL, data);
  430. data = RREG32_SOC15(VCN, 0, mmUVD_SUVD_CGC_CTRL);
  431. data |= (UVD_SUVD_CGC_CTRL__SRE_MODE_MASK
  432. | UVD_SUVD_CGC_CTRL__SIT_MODE_MASK
  433. | UVD_SUVD_CGC_CTRL__SMP_MODE_MASK
  434. | UVD_SUVD_CGC_CTRL__SCM_MODE_MASK
  435. | UVD_SUVD_CGC_CTRL__SDB_MODE_MASK
  436. | UVD_SUVD_CGC_CTRL__SCLR_MODE_MASK
  437. | UVD_SUVD_CGC_CTRL__UVD_SC_MODE_MASK
  438. | UVD_SUVD_CGC_CTRL__ENT_MODE_MASK
  439. | UVD_SUVD_CGC_CTRL__IME_MODE_MASK
  440. | UVD_SUVD_CGC_CTRL__SITE_MODE_MASK);
  441. WREG32_SOC15(VCN, 0, mmUVD_SUVD_CGC_CTRL, data);
  442. }
  443. /**
  444. * vcn_v1_0_start - start VCN block
  445. *
  446. * @adev: amdgpu_device pointer
  447. *
  448. * Setup and start the VCN block
  449. */
  450. static int vcn_v1_0_start(struct amdgpu_device *adev)
  451. {
  452. struct amdgpu_ring *ring = &adev->vcn.ring_dec;
  453. uint32_t rb_bufsz, tmp;
  454. uint32_t lmi_swap_cntl;
  455. int i, j, r;
  456. /* disable byte swapping */
  457. lmi_swap_cntl = 0;
  458. vcn_v1_0_mc_resume(adev);
  459. /* disable clock gating */
  460. vcn_v1_0_disable_clock_gating(adev, true);
  461. /* disable interupt */
  462. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_MASTINT_EN), 0,
  463. ~UVD_MASTINT_EN__VCPU_EN_MASK);
  464. /* stall UMC and register bus before resetting VCPU */
  465. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_LMI_CTRL2),
  466. UVD_LMI_CTRL2__STALL_ARB_UMC_MASK,
  467. ~UVD_LMI_CTRL2__STALL_ARB_UMC_MASK);
  468. mdelay(1);
  469. /* put LMI, VCPU, RBC etc... into reset */
  470. WREG32_SOC15(UVD, 0, mmUVD_SOFT_RESET,
  471. UVD_SOFT_RESET__LMI_SOFT_RESET_MASK |
  472. UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK |
  473. UVD_SOFT_RESET__LBSI_SOFT_RESET_MASK |
  474. UVD_SOFT_RESET__RBC_SOFT_RESET_MASK |
  475. UVD_SOFT_RESET__CSM_SOFT_RESET_MASK |
  476. UVD_SOFT_RESET__CXW_SOFT_RESET_MASK |
  477. UVD_SOFT_RESET__TAP_SOFT_RESET_MASK |
  478. UVD_SOFT_RESET__LMI_UMC_SOFT_RESET_MASK);
  479. mdelay(5);
  480. /* initialize VCN memory controller */
  481. WREG32_SOC15(UVD, 0, mmUVD_LMI_CTRL,
  482. (0x40 << UVD_LMI_CTRL__WRITE_CLEAN_TIMER__SHIFT) |
  483. UVD_LMI_CTRL__WRITE_CLEAN_TIMER_EN_MASK |
  484. UVD_LMI_CTRL__DATA_COHERENCY_EN_MASK |
  485. UVD_LMI_CTRL__VCPU_DATA_COHERENCY_EN_MASK |
  486. UVD_LMI_CTRL__REQ_MODE_MASK |
  487. 0x00100000L);
  488. #ifdef __BIG_ENDIAN
  489. /* swap (8 in 32) RB and IB */
  490. lmi_swap_cntl = 0xa;
  491. #endif
  492. WREG32_SOC15(UVD, 0, mmUVD_LMI_SWAP_CNTL, lmi_swap_cntl);
  493. WREG32_SOC15(UVD, 0, mmUVD_MPC_SET_MUXA0, 0x40c2040);
  494. WREG32_SOC15(UVD, 0, mmUVD_MPC_SET_MUXA1, 0x0);
  495. WREG32_SOC15(UVD, 0, mmUVD_MPC_SET_MUXB0, 0x40c2040);
  496. WREG32_SOC15(UVD, 0, mmUVD_MPC_SET_MUXB1, 0x0);
  497. WREG32_SOC15(UVD, 0, mmUVD_MPC_SET_ALU, 0);
  498. WREG32_SOC15(UVD, 0, mmUVD_MPC_SET_MUX, 0x88);
  499. /* take all subblocks out of reset, except VCPU */
  500. WREG32_SOC15(UVD, 0, mmUVD_SOFT_RESET,
  501. UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  502. mdelay(5);
  503. /* enable VCPU clock */
  504. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CNTL,
  505. UVD_VCPU_CNTL__CLK_EN_MASK);
  506. /* enable UMC */
  507. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_LMI_CTRL2), 0,
  508. ~UVD_LMI_CTRL2__STALL_ARB_UMC_MASK);
  509. /* boot up the VCPU */
  510. WREG32_SOC15(UVD, 0, mmUVD_SOFT_RESET, 0);
  511. mdelay(10);
  512. for (i = 0; i < 10; ++i) {
  513. uint32_t status;
  514. for (j = 0; j < 100; ++j) {
  515. status = RREG32_SOC15(UVD, 0, mmUVD_STATUS);
  516. if (status & 2)
  517. break;
  518. mdelay(10);
  519. }
  520. r = 0;
  521. if (status & 2)
  522. break;
  523. DRM_ERROR("VCN decode not responding, trying to reset the VCPU!!!\n");
  524. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_SOFT_RESET),
  525. UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK,
  526. ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  527. mdelay(10);
  528. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_SOFT_RESET), 0,
  529. ~UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  530. mdelay(10);
  531. r = -1;
  532. }
  533. if (r) {
  534. DRM_ERROR("VCN decode not responding, giving up!!!\n");
  535. return r;
  536. }
  537. /* enable master interrupt */
  538. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_MASTINT_EN),
  539. (UVD_MASTINT_EN__VCPU_EN_MASK|UVD_MASTINT_EN__SYS_EN_MASK),
  540. ~(UVD_MASTINT_EN__VCPU_EN_MASK|UVD_MASTINT_EN__SYS_EN_MASK));
  541. /* clear the bit 4 of VCN_STATUS */
  542. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_STATUS), 0,
  543. ~(2 << UVD_STATUS__VCPU_REPORT__SHIFT));
  544. /* force RBC into idle state */
  545. rb_bufsz = order_base_2(ring->ring_size);
  546. tmp = REG_SET_FIELD(0, UVD_RBC_RB_CNTL, RB_BUFSZ, rb_bufsz);
  547. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_BLKSZ, 1);
  548. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_FETCH, 1);
  549. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_WPTR_POLL_EN, 0);
  550. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_NO_UPDATE, 1);
  551. tmp = REG_SET_FIELD(tmp, UVD_RBC_RB_CNTL, RB_RPTR_WR_EN, 1);
  552. WREG32_SOC15(UVD, 0, mmUVD_RBC_RB_CNTL, tmp);
  553. /* set the write pointer delay */
  554. WREG32_SOC15(UVD, 0, mmUVD_RBC_RB_WPTR_CNTL, 0);
  555. /* set the wb address */
  556. WREG32_SOC15(UVD, 0, mmUVD_RBC_RB_RPTR_ADDR,
  557. (upper_32_bits(ring->gpu_addr) >> 2));
  558. /* programm the RB_BASE for ring buffer */
  559. WREG32_SOC15(UVD, 0, mmUVD_LMI_RBC_RB_64BIT_BAR_LOW,
  560. lower_32_bits(ring->gpu_addr));
  561. WREG32_SOC15(UVD, 0, mmUVD_LMI_RBC_RB_64BIT_BAR_HIGH,
  562. upper_32_bits(ring->gpu_addr));
  563. /* Initialize the ring buffer's read and write pointers */
  564. WREG32_SOC15(UVD, 0, mmUVD_RBC_RB_RPTR, 0);
  565. ring->wptr = RREG32_SOC15(UVD, 0, mmUVD_RBC_RB_RPTR);
  566. WREG32_SOC15(UVD, 0, mmUVD_RBC_RB_WPTR,
  567. lower_32_bits(ring->wptr));
  568. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_RBC_RB_CNTL), 0,
  569. ~UVD_RBC_RB_CNTL__RB_NO_FETCH_MASK);
  570. ring = &adev->vcn.ring_enc[0];
  571. WREG32_SOC15(UVD, 0, mmUVD_RB_RPTR, lower_32_bits(ring->wptr));
  572. WREG32_SOC15(UVD, 0, mmUVD_RB_WPTR, lower_32_bits(ring->wptr));
  573. WREG32_SOC15(UVD, 0, mmUVD_RB_BASE_LO, ring->gpu_addr);
  574. WREG32_SOC15(UVD, 0, mmUVD_RB_BASE_HI, upper_32_bits(ring->gpu_addr));
  575. WREG32_SOC15(UVD, 0, mmUVD_RB_SIZE, ring->ring_size / 4);
  576. ring = &adev->vcn.ring_enc[1];
  577. WREG32_SOC15(UVD, 0, mmUVD_RB_RPTR2, lower_32_bits(ring->wptr));
  578. WREG32_SOC15(UVD, 0, mmUVD_RB_WPTR2, lower_32_bits(ring->wptr));
  579. WREG32_SOC15(UVD, 0, mmUVD_RB_BASE_LO2, ring->gpu_addr);
  580. WREG32_SOC15(UVD, 0, mmUVD_RB_BASE_HI2, upper_32_bits(ring->gpu_addr));
  581. WREG32_SOC15(UVD, 0, mmUVD_RB_SIZE2, ring->ring_size / 4);
  582. return 0;
  583. }
  584. /**
  585. * vcn_v1_0_stop - stop VCN block
  586. *
  587. * @adev: amdgpu_device pointer
  588. *
  589. * stop the VCN block
  590. */
  591. static int vcn_v1_0_stop(struct amdgpu_device *adev)
  592. {
  593. /* force RBC into idle state */
  594. WREG32_SOC15(UVD, 0, mmUVD_RBC_RB_CNTL, 0x11010101);
  595. /* Stall UMC and register bus before resetting VCPU */
  596. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_LMI_CTRL2),
  597. UVD_LMI_CTRL2__STALL_ARB_UMC_MASK,
  598. ~UVD_LMI_CTRL2__STALL_ARB_UMC_MASK);
  599. mdelay(1);
  600. /* put VCPU into reset */
  601. WREG32_SOC15(UVD, 0, mmUVD_SOFT_RESET,
  602. UVD_SOFT_RESET__VCPU_SOFT_RESET_MASK);
  603. mdelay(5);
  604. /* disable VCPU clock */
  605. WREG32_SOC15(UVD, 0, mmUVD_VCPU_CNTL, 0x0);
  606. /* Unstall UMC and register bus */
  607. WREG32_P(SOC15_REG_OFFSET(UVD, 0, mmUVD_LMI_CTRL2), 0,
  608. ~UVD_LMI_CTRL2__STALL_ARB_UMC_MASK);
  609. /* enable clock gating */
  610. vcn_v1_0_enable_clock_gating(adev, true);
  611. return 0;
  612. }
  613. static int vcn_v1_0_set_clockgating_state(void *handle,
  614. enum amd_clockgating_state state)
  615. {
  616. /* needed for driver unload*/
  617. return 0;
  618. }
  619. /**
  620. * vcn_v1_0_dec_ring_get_rptr - get read pointer
  621. *
  622. * @ring: amdgpu_ring pointer
  623. *
  624. * Returns the current hardware read pointer
  625. */
  626. static uint64_t vcn_v1_0_dec_ring_get_rptr(struct amdgpu_ring *ring)
  627. {
  628. struct amdgpu_device *adev = ring->adev;
  629. return RREG32_SOC15(UVD, 0, mmUVD_RBC_RB_RPTR);
  630. }
  631. /**
  632. * vcn_v1_0_dec_ring_get_wptr - get write pointer
  633. *
  634. * @ring: amdgpu_ring pointer
  635. *
  636. * Returns the current hardware write pointer
  637. */
  638. static uint64_t vcn_v1_0_dec_ring_get_wptr(struct amdgpu_ring *ring)
  639. {
  640. struct amdgpu_device *adev = ring->adev;
  641. return RREG32_SOC15(UVD, 0, mmUVD_RBC_RB_WPTR);
  642. }
  643. /**
  644. * vcn_v1_0_dec_ring_set_wptr - set write pointer
  645. *
  646. * @ring: amdgpu_ring pointer
  647. *
  648. * Commits the write pointer to the hardware
  649. */
  650. static void vcn_v1_0_dec_ring_set_wptr(struct amdgpu_ring *ring)
  651. {
  652. struct amdgpu_device *adev = ring->adev;
  653. WREG32_SOC15(UVD, 0, mmUVD_RBC_RB_WPTR, lower_32_bits(ring->wptr));
  654. }
  655. /**
  656. * vcn_v1_0_dec_ring_insert_start - insert a start command
  657. *
  658. * @ring: amdgpu_ring pointer
  659. *
  660. * Write a start command to the ring.
  661. */
  662. static void vcn_v1_0_dec_ring_insert_start(struct amdgpu_ring *ring)
  663. {
  664. amdgpu_ring_write(ring,
  665. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA0), 0));
  666. amdgpu_ring_write(ring, 0);
  667. amdgpu_ring_write(ring,
  668. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0));
  669. amdgpu_ring_write(ring, VCN_DEC_CMD_PACKET_START << 1);
  670. }
  671. /**
  672. * vcn_v1_0_dec_ring_insert_end - insert a end command
  673. *
  674. * @ring: amdgpu_ring pointer
  675. *
  676. * Write a end command to the ring.
  677. */
  678. static void vcn_v1_0_dec_ring_insert_end(struct amdgpu_ring *ring)
  679. {
  680. amdgpu_ring_write(ring,
  681. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0));
  682. amdgpu_ring_write(ring, VCN_DEC_CMD_PACKET_END << 1);
  683. }
  684. /**
  685. * vcn_v1_0_dec_ring_emit_fence - emit an fence & trap command
  686. *
  687. * @ring: amdgpu_ring pointer
  688. * @fence: fence to emit
  689. *
  690. * Write a fence and a trap command to the ring.
  691. */
  692. static void vcn_v1_0_dec_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
  693. unsigned flags)
  694. {
  695. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  696. amdgpu_ring_write(ring,
  697. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_CONTEXT_ID), 0));
  698. amdgpu_ring_write(ring, seq);
  699. amdgpu_ring_write(ring,
  700. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA0), 0));
  701. amdgpu_ring_write(ring, addr & 0xffffffff);
  702. amdgpu_ring_write(ring,
  703. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA1), 0));
  704. amdgpu_ring_write(ring, upper_32_bits(addr) & 0xff);
  705. amdgpu_ring_write(ring,
  706. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0));
  707. amdgpu_ring_write(ring, VCN_DEC_CMD_FENCE << 1);
  708. amdgpu_ring_write(ring,
  709. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA0), 0));
  710. amdgpu_ring_write(ring, 0);
  711. amdgpu_ring_write(ring,
  712. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA1), 0));
  713. amdgpu_ring_write(ring, 0);
  714. amdgpu_ring_write(ring,
  715. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0));
  716. amdgpu_ring_write(ring, VCN_DEC_CMD_TRAP << 1);
  717. }
  718. /**
  719. * vcn_v1_0_dec_ring_hdp_invalidate - emit an hdp invalidate
  720. *
  721. * @ring: amdgpu_ring pointer
  722. *
  723. * Emits an hdp invalidate.
  724. */
  725. static void vcn_v1_0_dec_ring_emit_hdp_invalidate(struct amdgpu_ring *ring)
  726. {
  727. amdgpu_ring_write(ring, PACKET0(SOC15_REG_OFFSET(HDP, 0, mmHDP_DEBUG0), 0));
  728. amdgpu_ring_write(ring, 1);
  729. }
  730. /**
  731. * vcn_v1_0_dec_ring_emit_ib - execute indirect buffer
  732. *
  733. * @ring: amdgpu_ring pointer
  734. * @ib: indirect buffer to execute
  735. *
  736. * Write ring commands to execute the indirect buffer
  737. */
  738. static void vcn_v1_0_dec_ring_emit_ib(struct amdgpu_ring *ring,
  739. struct amdgpu_ib *ib,
  740. unsigned vm_id, bool ctx_switch)
  741. {
  742. amdgpu_ring_write(ring,
  743. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_LMI_RBC_IB_VMID), 0));
  744. amdgpu_ring_write(ring, vm_id);
  745. amdgpu_ring_write(ring,
  746. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_LMI_RBC_IB_64BIT_BAR_LOW), 0));
  747. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  748. amdgpu_ring_write(ring,
  749. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_LMI_RBC_IB_64BIT_BAR_HIGH), 0));
  750. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  751. amdgpu_ring_write(ring,
  752. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_RBC_IB_SIZE), 0));
  753. amdgpu_ring_write(ring, ib->length_dw);
  754. }
  755. static void vcn_v1_0_dec_vm_reg_write(struct amdgpu_ring *ring,
  756. uint32_t data0, uint32_t data1)
  757. {
  758. amdgpu_ring_write(ring,
  759. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA0), 0));
  760. amdgpu_ring_write(ring, data0);
  761. amdgpu_ring_write(ring,
  762. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA1), 0));
  763. amdgpu_ring_write(ring, data1);
  764. amdgpu_ring_write(ring,
  765. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0));
  766. amdgpu_ring_write(ring, VCN_DEC_CMD_WRITE_REG << 1);
  767. }
  768. static void vcn_v1_0_dec_vm_reg_wait(struct amdgpu_ring *ring,
  769. uint32_t data0, uint32_t data1, uint32_t mask)
  770. {
  771. amdgpu_ring_write(ring,
  772. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA0), 0));
  773. amdgpu_ring_write(ring, data0);
  774. amdgpu_ring_write(ring,
  775. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_DATA1), 0));
  776. amdgpu_ring_write(ring, data1);
  777. amdgpu_ring_write(ring,
  778. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GP_SCRATCH8), 0));
  779. amdgpu_ring_write(ring, mask);
  780. amdgpu_ring_write(ring,
  781. PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_GPCOM_VCPU_CMD), 0));
  782. amdgpu_ring_write(ring, VCN_DEC_CMD_REG_READ_COND_WAIT << 1);
  783. }
  784. static void vcn_v1_0_dec_ring_emit_vm_flush(struct amdgpu_ring *ring,
  785. unsigned vm_id, uint64_t pd_addr)
  786. {
  787. struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->funcs->vmhub];
  788. uint32_t req = ring->adev->gart.gart_funcs->get_invalidate_req(vm_id);
  789. uint32_t data0, data1, mask;
  790. unsigned eng = ring->vm_inv_eng;
  791. pd_addr = amdgpu_gart_get_vm_pde(ring->adev, pd_addr);
  792. pd_addr |= AMDGPU_PTE_VALID;
  793. data0 = (hub->ctx0_ptb_addr_hi32 + vm_id * 2) << 2;
  794. data1 = upper_32_bits(pd_addr);
  795. vcn_v1_0_dec_vm_reg_write(ring, data0, data1);
  796. data0 = (hub->ctx0_ptb_addr_lo32 + vm_id * 2) << 2;
  797. data1 = lower_32_bits(pd_addr);
  798. vcn_v1_0_dec_vm_reg_write(ring, data0, data1);
  799. data0 = (hub->ctx0_ptb_addr_lo32 + vm_id * 2) << 2;
  800. data1 = lower_32_bits(pd_addr);
  801. mask = 0xffffffff;
  802. vcn_v1_0_dec_vm_reg_wait(ring, data0, data1, mask);
  803. /* flush TLB */
  804. data0 = (hub->vm_inv_eng0_req + eng) << 2;
  805. data1 = req;
  806. vcn_v1_0_dec_vm_reg_write(ring, data0, data1);
  807. /* wait for flush */
  808. data0 = (hub->vm_inv_eng0_ack + eng) << 2;
  809. data1 = 1 << vm_id;
  810. mask = 1 << vm_id;
  811. vcn_v1_0_dec_vm_reg_wait(ring, data0, data1, mask);
  812. }
  813. /**
  814. * vcn_v1_0_enc_ring_get_rptr - get enc read pointer
  815. *
  816. * @ring: amdgpu_ring pointer
  817. *
  818. * Returns the current hardware enc read pointer
  819. */
  820. static uint64_t vcn_v1_0_enc_ring_get_rptr(struct amdgpu_ring *ring)
  821. {
  822. struct amdgpu_device *adev = ring->adev;
  823. if (ring == &adev->vcn.ring_enc[0])
  824. return RREG32_SOC15(UVD, 0, mmUVD_RB_RPTR);
  825. else
  826. return RREG32_SOC15(UVD, 0, mmUVD_RB_RPTR2);
  827. }
  828. /**
  829. * vcn_v1_0_enc_ring_get_wptr - get enc write pointer
  830. *
  831. * @ring: amdgpu_ring pointer
  832. *
  833. * Returns the current hardware enc write pointer
  834. */
  835. static uint64_t vcn_v1_0_enc_ring_get_wptr(struct amdgpu_ring *ring)
  836. {
  837. struct amdgpu_device *adev = ring->adev;
  838. if (ring == &adev->vcn.ring_enc[0])
  839. return RREG32_SOC15(UVD, 0, mmUVD_RB_WPTR);
  840. else
  841. return RREG32_SOC15(UVD, 0, mmUVD_RB_WPTR2);
  842. }
  843. /**
  844. * vcn_v1_0_enc_ring_set_wptr - set enc write pointer
  845. *
  846. * @ring: amdgpu_ring pointer
  847. *
  848. * Commits the enc write pointer to the hardware
  849. */
  850. static void vcn_v1_0_enc_ring_set_wptr(struct amdgpu_ring *ring)
  851. {
  852. struct amdgpu_device *adev = ring->adev;
  853. if (ring == &adev->vcn.ring_enc[0])
  854. WREG32_SOC15(UVD, 0, mmUVD_RB_WPTR,
  855. lower_32_bits(ring->wptr));
  856. else
  857. WREG32_SOC15(UVD, 0, mmUVD_RB_WPTR2,
  858. lower_32_bits(ring->wptr));
  859. }
  860. /**
  861. * vcn_v1_0_enc_ring_emit_fence - emit an enc fence & trap command
  862. *
  863. * @ring: amdgpu_ring pointer
  864. * @fence: fence to emit
  865. *
  866. * Write enc a fence and a trap command to the ring.
  867. */
  868. static void vcn_v1_0_enc_ring_emit_fence(struct amdgpu_ring *ring, u64 addr,
  869. u64 seq, unsigned flags)
  870. {
  871. WARN_ON(flags & AMDGPU_FENCE_FLAG_64BIT);
  872. amdgpu_ring_write(ring, VCN_ENC_CMD_FENCE);
  873. amdgpu_ring_write(ring, addr);
  874. amdgpu_ring_write(ring, upper_32_bits(addr));
  875. amdgpu_ring_write(ring, seq);
  876. amdgpu_ring_write(ring, VCN_ENC_CMD_TRAP);
  877. }
  878. static void vcn_v1_0_enc_ring_insert_end(struct amdgpu_ring *ring)
  879. {
  880. amdgpu_ring_write(ring, VCN_ENC_CMD_END);
  881. }
  882. /**
  883. * vcn_v1_0_enc_ring_emit_ib - enc execute indirect buffer
  884. *
  885. * @ring: amdgpu_ring pointer
  886. * @ib: indirect buffer to execute
  887. *
  888. * Write enc ring commands to execute the indirect buffer
  889. */
  890. static void vcn_v1_0_enc_ring_emit_ib(struct amdgpu_ring *ring,
  891. struct amdgpu_ib *ib, unsigned int vm_id, bool ctx_switch)
  892. {
  893. amdgpu_ring_write(ring, VCN_ENC_CMD_IB);
  894. amdgpu_ring_write(ring, vm_id);
  895. amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr));
  896. amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
  897. amdgpu_ring_write(ring, ib->length_dw);
  898. }
  899. static void vcn_v1_0_enc_ring_emit_vm_flush(struct amdgpu_ring *ring,
  900. unsigned int vm_id, uint64_t pd_addr)
  901. {
  902. struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->funcs->vmhub];
  903. uint32_t req = ring->adev->gart.gart_funcs->get_invalidate_req(vm_id);
  904. unsigned eng = ring->vm_inv_eng;
  905. pd_addr = amdgpu_gart_get_vm_pde(ring->adev, pd_addr);
  906. pd_addr |= AMDGPU_PTE_VALID;
  907. amdgpu_ring_write(ring, VCN_ENC_CMD_REG_WRITE);
  908. amdgpu_ring_write(ring,
  909. (hub->ctx0_ptb_addr_hi32 + vm_id * 2) << 2);
  910. amdgpu_ring_write(ring, upper_32_bits(pd_addr));
  911. amdgpu_ring_write(ring, VCN_ENC_CMD_REG_WRITE);
  912. amdgpu_ring_write(ring,
  913. (hub->ctx0_ptb_addr_lo32 + vm_id * 2) << 2);
  914. amdgpu_ring_write(ring, lower_32_bits(pd_addr));
  915. amdgpu_ring_write(ring, VCN_ENC_CMD_REG_WAIT);
  916. amdgpu_ring_write(ring,
  917. (hub->ctx0_ptb_addr_lo32 + vm_id * 2) << 2);
  918. amdgpu_ring_write(ring, 0xffffffff);
  919. amdgpu_ring_write(ring, lower_32_bits(pd_addr));
  920. /* flush TLB */
  921. amdgpu_ring_write(ring, VCN_ENC_CMD_REG_WRITE);
  922. amdgpu_ring_write(ring, (hub->vm_inv_eng0_req + eng) << 2);
  923. amdgpu_ring_write(ring, req);
  924. /* wait for flush */
  925. amdgpu_ring_write(ring, VCN_ENC_CMD_REG_WAIT);
  926. amdgpu_ring_write(ring, (hub->vm_inv_eng0_ack + eng) << 2);
  927. amdgpu_ring_write(ring, 1 << vm_id);
  928. amdgpu_ring_write(ring, 1 << vm_id);
  929. }
  930. static int vcn_v1_0_set_interrupt_state(struct amdgpu_device *adev,
  931. struct amdgpu_irq_src *source,
  932. unsigned type,
  933. enum amdgpu_interrupt_state state)
  934. {
  935. return 0;
  936. }
  937. static int vcn_v1_0_process_interrupt(struct amdgpu_device *adev,
  938. struct amdgpu_irq_src *source,
  939. struct amdgpu_iv_entry *entry)
  940. {
  941. DRM_DEBUG("IH: VCN TRAP\n");
  942. switch (entry->src_id) {
  943. case 124:
  944. amdgpu_fence_process(&adev->vcn.ring_dec);
  945. break;
  946. case 119:
  947. amdgpu_fence_process(&adev->vcn.ring_enc[0]);
  948. break;
  949. case 120:
  950. amdgpu_fence_process(&adev->vcn.ring_enc[1]);
  951. break;
  952. default:
  953. DRM_ERROR("Unhandled interrupt: %d %d\n",
  954. entry->src_id, entry->src_data[0]);
  955. break;
  956. }
  957. return 0;
  958. }
  959. static const struct amd_ip_funcs vcn_v1_0_ip_funcs = {
  960. .name = "vcn_v1_0",
  961. .early_init = vcn_v1_0_early_init,
  962. .late_init = NULL,
  963. .sw_init = vcn_v1_0_sw_init,
  964. .sw_fini = vcn_v1_0_sw_fini,
  965. .hw_init = vcn_v1_0_hw_init,
  966. .hw_fini = vcn_v1_0_hw_fini,
  967. .suspend = vcn_v1_0_suspend,
  968. .resume = vcn_v1_0_resume,
  969. .is_idle = NULL /* vcn_v1_0_is_idle */,
  970. .wait_for_idle = NULL /* vcn_v1_0_wait_for_idle */,
  971. .check_soft_reset = NULL /* vcn_v1_0_check_soft_reset */,
  972. .pre_soft_reset = NULL /* vcn_v1_0_pre_soft_reset */,
  973. .soft_reset = NULL /* vcn_v1_0_soft_reset */,
  974. .post_soft_reset = NULL /* vcn_v1_0_post_soft_reset */,
  975. .set_clockgating_state = vcn_v1_0_set_clockgating_state,
  976. .set_powergating_state = NULL /* vcn_v1_0_set_powergating_state */,
  977. };
  978. static const struct amdgpu_ring_funcs vcn_v1_0_dec_ring_vm_funcs = {
  979. .type = AMDGPU_RING_TYPE_VCN_DEC,
  980. .align_mask = 0xf,
  981. .nop = PACKET0(SOC15_REG_OFFSET(UVD, 0, mmUVD_NO_OP), 0),
  982. .support_64bit_ptrs = false,
  983. .vmhub = AMDGPU_MMHUB,
  984. .get_rptr = vcn_v1_0_dec_ring_get_rptr,
  985. .get_wptr = vcn_v1_0_dec_ring_get_wptr,
  986. .set_wptr = vcn_v1_0_dec_ring_set_wptr,
  987. .emit_frame_size =
  988. 2 + /* vcn_v1_0_dec_ring_emit_hdp_invalidate */
  989. 34 + /* vcn_v1_0_dec_ring_emit_vm_flush */
  990. 14 + 14 + /* vcn_v1_0_dec_ring_emit_fence x2 vm fence */
  991. 6,
  992. .emit_ib_size = 8, /* vcn_v1_0_dec_ring_emit_ib */
  993. .emit_ib = vcn_v1_0_dec_ring_emit_ib,
  994. .emit_fence = vcn_v1_0_dec_ring_emit_fence,
  995. .emit_vm_flush = vcn_v1_0_dec_ring_emit_vm_flush,
  996. .emit_hdp_invalidate = vcn_v1_0_dec_ring_emit_hdp_invalidate,
  997. .test_ring = amdgpu_vcn_dec_ring_test_ring,
  998. .test_ib = amdgpu_vcn_dec_ring_test_ib,
  999. .insert_nop = amdgpu_ring_insert_nop,
  1000. .insert_start = vcn_v1_0_dec_ring_insert_start,
  1001. .insert_end = vcn_v1_0_dec_ring_insert_end,
  1002. .pad_ib = amdgpu_ring_generic_pad_ib,
  1003. .begin_use = amdgpu_vcn_ring_begin_use,
  1004. .end_use = amdgpu_vcn_ring_end_use,
  1005. };
  1006. static const struct amdgpu_ring_funcs vcn_v1_0_enc_ring_vm_funcs = {
  1007. .type = AMDGPU_RING_TYPE_VCN_ENC,
  1008. .align_mask = 0x3f,
  1009. .nop = VCN_ENC_CMD_NO_OP,
  1010. .support_64bit_ptrs = false,
  1011. .vmhub = AMDGPU_MMHUB,
  1012. .get_rptr = vcn_v1_0_enc_ring_get_rptr,
  1013. .get_wptr = vcn_v1_0_enc_ring_get_wptr,
  1014. .set_wptr = vcn_v1_0_enc_ring_set_wptr,
  1015. .emit_frame_size =
  1016. 17 + /* vcn_v1_0_enc_ring_emit_vm_flush */
  1017. 5 + 5 + /* vcn_v1_0_enc_ring_emit_fence x2 vm fence */
  1018. 1, /* vcn_v1_0_enc_ring_insert_end */
  1019. .emit_ib_size = 5, /* vcn_v1_0_enc_ring_emit_ib */
  1020. .emit_ib = vcn_v1_0_enc_ring_emit_ib,
  1021. .emit_fence = vcn_v1_0_enc_ring_emit_fence,
  1022. .emit_vm_flush = vcn_v1_0_enc_ring_emit_vm_flush,
  1023. .test_ring = amdgpu_vcn_enc_ring_test_ring,
  1024. .test_ib = amdgpu_vcn_enc_ring_test_ib,
  1025. .insert_nop = amdgpu_ring_insert_nop,
  1026. .insert_end = vcn_v1_0_enc_ring_insert_end,
  1027. .pad_ib = amdgpu_ring_generic_pad_ib,
  1028. .begin_use = amdgpu_vcn_ring_begin_use,
  1029. .end_use = amdgpu_vcn_ring_end_use,
  1030. };
  1031. static void vcn_v1_0_set_dec_ring_funcs(struct amdgpu_device *adev)
  1032. {
  1033. adev->vcn.ring_dec.funcs = &vcn_v1_0_dec_ring_vm_funcs;
  1034. DRM_INFO("VCN decode is enabled in VM mode\n");
  1035. }
  1036. static void vcn_v1_0_set_enc_ring_funcs(struct amdgpu_device *adev)
  1037. {
  1038. int i;
  1039. for (i = 0; i < adev->vcn.num_enc_rings; ++i)
  1040. adev->vcn.ring_enc[i].funcs = &vcn_v1_0_enc_ring_vm_funcs;
  1041. DRM_INFO("VCN encode is enabled in VM mode\n");
  1042. }
  1043. static const struct amdgpu_irq_src_funcs vcn_v1_0_irq_funcs = {
  1044. .set = vcn_v1_0_set_interrupt_state,
  1045. .process = vcn_v1_0_process_interrupt,
  1046. };
  1047. static void vcn_v1_0_set_irq_funcs(struct amdgpu_device *adev)
  1048. {
  1049. adev->vcn.irq.num_types = adev->vcn.num_enc_rings + 1;
  1050. adev->vcn.irq.funcs = &vcn_v1_0_irq_funcs;
  1051. }
  1052. const struct amdgpu_ip_block_version vcn_v1_0_ip_block =
  1053. {
  1054. .type = AMD_IP_BLOCK_TYPE_VCN,
  1055. .major = 1,
  1056. .minor = 0,
  1057. .rev = 0,
  1058. .funcs = &vcn_v1_0_ip_funcs,
  1059. };