psp_v10_0.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. * Author: Huang Rui
  23. *
  24. */
  25. #include <linux/firmware.h>
  26. #include "amdgpu.h"
  27. #include "amdgpu_psp.h"
  28. #include "amdgpu_ucode.h"
  29. #include "soc15_common.h"
  30. #include "psp_v10_0.h"
  31. #include "vega10/soc15ip.h"
  32. #include "raven1/MP/mp_10_0_offset.h"
  33. #include "raven1/GC/gc_9_1_offset.h"
  34. #include "raven1/SDMA0/sdma0_4_1_offset.h"
  35. static int
  36. psp_v10_0_get_fw_type(struct amdgpu_firmware_info *ucode, enum psp_gfx_fw_type *type)
  37. {
  38. switch(ucode->ucode_id) {
  39. case AMDGPU_UCODE_ID_SDMA0:
  40. *type = GFX_FW_TYPE_SDMA0;
  41. break;
  42. case AMDGPU_UCODE_ID_SDMA1:
  43. *type = GFX_FW_TYPE_SDMA1;
  44. break;
  45. case AMDGPU_UCODE_ID_CP_CE:
  46. *type = GFX_FW_TYPE_CP_CE;
  47. break;
  48. case AMDGPU_UCODE_ID_CP_PFP:
  49. *type = GFX_FW_TYPE_CP_PFP;
  50. break;
  51. case AMDGPU_UCODE_ID_CP_ME:
  52. *type = GFX_FW_TYPE_CP_ME;
  53. break;
  54. case AMDGPU_UCODE_ID_CP_MEC1:
  55. *type = GFX_FW_TYPE_CP_MEC;
  56. break;
  57. case AMDGPU_UCODE_ID_CP_MEC1_JT:
  58. *type = GFX_FW_TYPE_CP_MEC_ME1;
  59. break;
  60. case AMDGPU_UCODE_ID_CP_MEC2:
  61. *type = GFX_FW_TYPE_CP_MEC;
  62. break;
  63. case AMDGPU_UCODE_ID_CP_MEC2_JT:
  64. *type = GFX_FW_TYPE_CP_MEC_ME2;
  65. break;
  66. case AMDGPU_UCODE_ID_RLC_G:
  67. *type = GFX_FW_TYPE_RLC_G;
  68. break;
  69. case AMDGPU_UCODE_ID_SMC:
  70. *type = GFX_FW_TYPE_SMU;
  71. break;
  72. case AMDGPU_UCODE_ID_UVD:
  73. *type = GFX_FW_TYPE_UVD;
  74. break;
  75. case AMDGPU_UCODE_ID_VCE:
  76. *type = GFX_FW_TYPE_VCE;
  77. break;
  78. case AMDGPU_UCODE_ID_VCN:
  79. *type = GFX_FW_TYPE_VCN;
  80. break;
  81. case AMDGPU_UCODE_ID_MAXIMUM:
  82. default:
  83. return -EINVAL;
  84. }
  85. return 0;
  86. }
  87. int psp_v10_0_init_microcode(struct psp_context *psp)
  88. {
  89. struct amdgpu_device *adev = psp->adev;
  90. const char *chip_name;
  91. char fw_name[30];
  92. int err = 0;
  93. const struct psp_firmware_header_v1_0 *hdr;
  94. DRM_DEBUG("\n");
  95. switch (adev->asic_type) {
  96. case CHIP_RAVEN:
  97. chip_name = "raven";
  98. break;
  99. default: BUG();
  100. }
  101. snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
  102. err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
  103. if (err)
  104. goto out;
  105. err = amdgpu_ucode_validate(adev->psp.asd_fw);
  106. if (err)
  107. goto out;
  108. hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
  109. adev->psp.asd_fw_version = le32_to_cpu(hdr->header.ucode_version);
  110. adev->psp.asd_feature_version = le32_to_cpu(hdr->ucode_feature_version);
  111. adev->psp.asd_ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  112. adev->psp.asd_start_addr = (uint8_t *)hdr +
  113. le32_to_cpu(hdr->header.ucode_array_offset_bytes);
  114. return 0;
  115. out:
  116. if (err) {
  117. dev_err(adev->dev,
  118. "psp v10.0: Failed to load firmware \"%s\"\n",
  119. fw_name);
  120. release_firmware(adev->psp.asd_fw);
  121. adev->psp.asd_fw = NULL;
  122. }
  123. return err;
  124. }
  125. int psp_v10_0_prep_cmd_buf(struct amdgpu_firmware_info *ucode, struct psp_gfx_cmd_resp *cmd)
  126. {
  127. int ret;
  128. uint64_t fw_mem_mc_addr = ucode->mc_addr;
  129. struct common_firmware_header *header;
  130. memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
  131. header = (struct common_firmware_header *)ucode->fw;
  132. cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
  133. cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
  134. cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
  135. cmd->cmd.cmd_load_ip_fw.fw_size = le32_to_cpu(header->ucode_size_bytes);
  136. ret = psp_v10_0_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
  137. if (ret)
  138. DRM_ERROR("Unknown firmware type\n");
  139. return ret;
  140. }
  141. int psp_v10_0_ring_init(struct psp_context *psp, enum psp_ring_type ring_type)
  142. {
  143. int ret = 0;
  144. struct psp_ring *ring;
  145. struct amdgpu_device *adev = psp->adev;
  146. ring = &psp->km_ring;
  147. ring->ring_type = ring_type;
  148. /* allocate 4k Page of Local Frame Buffer memory for ring */
  149. ring->ring_size = 0x1000;
  150. ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
  151. AMDGPU_GEM_DOMAIN_VRAM,
  152. &adev->firmware.rbuf,
  153. &ring->ring_mem_mc_addr,
  154. (void **)&ring->ring_mem);
  155. if (ret) {
  156. ring->ring_size = 0;
  157. return ret;
  158. }
  159. return 0;
  160. }
  161. int psp_v10_0_ring_create(struct psp_context *psp, enum psp_ring_type ring_type)
  162. {
  163. int ret = 0;
  164. unsigned int psp_ring_reg = 0;
  165. struct psp_ring *ring = &psp->km_ring;
  166. struct amdgpu_device *adev = psp->adev;
  167. /* Write low address of the ring to C2PMSG_69 */
  168. psp_ring_reg = lower_32_bits(ring->ring_mem_mc_addr);
  169. WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_69, psp_ring_reg);
  170. /* Write high address of the ring to C2PMSG_70 */
  171. psp_ring_reg = upper_32_bits(ring->ring_mem_mc_addr);
  172. WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_70, psp_ring_reg);
  173. /* Write size of ring to C2PMSG_71 */
  174. psp_ring_reg = ring->ring_size;
  175. WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_71, psp_ring_reg);
  176. /* Write the ring initialization command to C2PMSG_64 */
  177. psp_ring_reg = ring_type;
  178. psp_ring_reg = psp_ring_reg << 16;
  179. WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_64, psp_ring_reg);
  180. /* There might be handshake issue with hardware which needs delay */
  181. mdelay(20);
  182. /* Wait for response flag (bit 31) in C2PMSG_64 */
  183. ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, mmMP0_SMN_C2PMSG_64),
  184. 0x80000000, 0x8000FFFF, false);
  185. return ret;
  186. }
  187. int psp_v10_0_ring_destroy(struct psp_context *psp, enum psp_ring_type ring_type)
  188. {
  189. int ret = 0;
  190. struct psp_ring *ring;
  191. unsigned int psp_ring_reg = 0;
  192. struct amdgpu_device *adev = psp->adev;
  193. ring = &psp->km_ring;
  194. /* Write the ring destroy command to C2PMSG_64 */
  195. psp_ring_reg = 3 << 16;
  196. WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_64, psp_ring_reg);
  197. /* There might be handshake issue with hardware which needs delay */
  198. mdelay(20);
  199. /* Wait for response flag (bit 31) in C2PMSG_64 */
  200. ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, mmMP0_SMN_C2PMSG_64),
  201. 0x80000000, 0x80000000, false);
  202. amdgpu_bo_free_kernel(&adev->firmware.rbuf,
  203. &ring->ring_mem_mc_addr,
  204. (void **)&ring->ring_mem);
  205. return ret;
  206. }
  207. int psp_v10_0_cmd_submit(struct psp_context *psp,
  208. struct amdgpu_firmware_info *ucode,
  209. uint64_t cmd_buf_mc_addr, uint64_t fence_mc_addr,
  210. int index)
  211. {
  212. unsigned int psp_write_ptr_reg = 0;
  213. struct psp_gfx_rb_frame * write_frame = psp->km_ring.ring_mem;
  214. struct psp_ring *ring = &psp->km_ring;
  215. struct amdgpu_device *adev = psp->adev;
  216. /* KM (GPCOM) prepare write pointer */
  217. psp_write_ptr_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_67);
  218. /* Update KM RB frame pointer to new frame */
  219. if ((psp_write_ptr_reg % ring->ring_size) == 0)
  220. write_frame = ring->ring_mem;
  221. else
  222. write_frame = ring->ring_mem + (psp_write_ptr_reg / (sizeof(struct psp_gfx_rb_frame) / 4));
  223. /* Update KM RB frame */
  224. write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
  225. write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
  226. write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
  227. write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
  228. write_frame->fence_value = index;
  229. /* Update the write Pointer in DWORDs */
  230. psp_write_ptr_reg += sizeof(struct psp_gfx_rb_frame) / 4;
  231. psp_write_ptr_reg = (psp_write_ptr_reg >= ring->ring_size) ? 0 : psp_write_ptr_reg;
  232. WREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_67, psp_write_ptr_reg);
  233. return 0;
  234. }
  235. static int
  236. psp_v10_0_sram_map(unsigned int *sram_offset, unsigned int *sram_addr_reg_offset,
  237. unsigned int *sram_data_reg_offset,
  238. enum AMDGPU_UCODE_ID ucode_id)
  239. {
  240. int ret = 0;
  241. switch(ucode_id) {
  242. /* TODO: needs to confirm */
  243. #if 0
  244. case AMDGPU_UCODE_ID_SMC:
  245. *sram_offset = 0;
  246. *sram_addr_reg_offset = 0;
  247. *sram_data_reg_offset = 0;
  248. break;
  249. #endif
  250. case AMDGPU_UCODE_ID_CP_CE:
  251. *sram_offset = 0x0;
  252. *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_CE_UCODE_ADDR);
  253. *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_CE_UCODE_DATA);
  254. break;
  255. case AMDGPU_UCODE_ID_CP_PFP:
  256. *sram_offset = 0x0;
  257. *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_PFP_UCODE_ADDR);
  258. *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_PFP_UCODE_DATA);
  259. break;
  260. case AMDGPU_UCODE_ID_CP_ME:
  261. *sram_offset = 0x0;
  262. *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_HYP_ME_UCODE_ADDR);
  263. *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_HYP_ME_UCODE_DATA);
  264. break;
  265. case AMDGPU_UCODE_ID_CP_MEC1:
  266. *sram_offset = 0x10000;
  267. *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_MEC_ME1_UCODE_ADDR);
  268. *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_MEC_ME1_UCODE_DATA);
  269. break;
  270. case AMDGPU_UCODE_ID_CP_MEC2:
  271. *sram_offset = 0x10000;
  272. *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_HYP_MEC2_UCODE_ADDR);
  273. *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmCP_HYP_MEC2_UCODE_DATA);
  274. break;
  275. case AMDGPU_UCODE_ID_RLC_G:
  276. *sram_offset = 0x2000;
  277. *sram_addr_reg_offset = SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_UCODE_ADDR);
  278. *sram_data_reg_offset = SOC15_REG_OFFSET(GC, 0, mmRLC_GPM_UCODE_DATA);
  279. break;
  280. case AMDGPU_UCODE_ID_SDMA0:
  281. *sram_offset = 0x0;
  282. *sram_addr_reg_offset = SOC15_REG_OFFSET(SDMA0, 0, mmSDMA0_UCODE_ADDR);
  283. *sram_data_reg_offset = SOC15_REG_OFFSET(SDMA0, 0, mmSDMA0_UCODE_DATA);
  284. break;
  285. /* TODO: needs to confirm */
  286. #if 0
  287. case AMDGPU_UCODE_ID_SDMA1:
  288. *sram_offset = ;
  289. *sram_addr_reg_offset = ;
  290. break;
  291. case AMDGPU_UCODE_ID_UVD:
  292. *sram_offset = ;
  293. *sram_addr_reg_offset = ;
  294. break;
  295. case AMDGPU_UCODE_ID_VCE:
  296. *sram_offset = ;
  297. *sram_addr_reg_offset = ;
  298. break;
  299. #endif
  300. case AMDGPU_UCODE_ID_MAXIMUM:
  301. default:
  302. ret = -EINVAL;
  303. break;
  304. }
  305. return ret;
  306. }
  307. bool psp_v10_0_compare_sram_data(struct psp_context *psp,
  308. struct amdgpu_firmware_info *ucode,
  309. enum AMDGPU_UCODE_ID ucode_type)
  310. {
  311. int err = 0;
  312. unsigned int fw_sram_reg_val = 0;
  313. unsigned int fw_sram_addr_reg_offset = 0;
  314. unsigned int fw_sram_data_reg_offset = 0;
  315. unsigned int ucode_size;
  316. uint32_t *ucode_mem = NULL;
  317. struct amdgpu_device *adev = psp->adev;
  318. err = psp_v10_0_sram_map(&fw_sram_reg_val, &fw_sram_addr_reg_offset,
  319. &fw_sram_data_reg_offset, ucode_type);
  320. if (err)
  321. return false;
  322. WREG32(fw_sram_addr_reg_offset, fw_sram_reg_val);
  323. ucode_size = ucode->ucode_size;
  324. ucode_mem = (uint32_t *)ucode->kaddr;
  325. while (!ucode_size) {
  326. fw_sram_reg_val = RREG32(fw_sram_data_reg_offset);
  327. if (*ucode_mem != fw_sram_reg_val)
  328. return false;
  329. ucode_mem++;
  330. /* 4 bytes */
  331. ucode_size -= 4;
  332. }
  333. return true;
  334. }