amdgpu_psp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "drmP.h"
  27. #include "amdgpu.h"
  28. #include "amdgpu_psp.h"
  29. #include "amdgpu_ucode.h"
  30. #include "soc15_common.h"
  31. #include "psp_v3_1.h"
  32. static void psp_set_funcs(struct amdgpu_device *adev);
  33. static int psp_early_init(void *handle)
  34. {
  35. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  36. psp_set_funcs(adev);
  37. return 0;
  38. }
  39. static int psp_sw_init(void *handle)
  40. {
  41. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  42. struct psp_context *psp = &adev->psp;
  43. int ret;
  44. switch (adev->asic_type) {
  45. case CHIP_VEGA10:
  46. psp->init_microcode = psp_v3_1_init_microcode;
  47. psp->bootloader_load_sysdrv = psp_v3_1_bootloader_load_sysdrv;
  48. psp->bootloader_load_sos = psp_v3_1_bootloader_load_sos;
  49. psp->prep_cmd_buf = psp_v3_1_prep_cmd_buf;
  50. psp->ring_init = psp_v3_1_ring_init;
  51. psp->cmd_submit = psp_v3_1_cmd_submit;
  52. psp->compare_sram_data = psp_v3_1_compare_sram_data;
  53. psp->smu_reload_quirk = psp_v3_1_smu_reload_quirk;
  54. break;
  55. default:
  56. return -EINVAL;
  57. }
  58. psp->adev = adev;
  59. ret = psp_init_microcode(psp);
  60. if (ret) {
  61. DRM_ERROR("Failed to load psp firmware!\n");
  62. return ret;
  63. }
  64. return 0;
  65. }
  66. static int psp_sw_fini(void *handle)
  67. {
  68. return 0;
  69. }
  70. int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
  71. uint32_t reg_val, uint32_t mask, bool check_changed)
  72. {
  73. uint32_t val;
  74. int i;
  75. struct amdgpu_device *adev = psp->adev;
  76. val = RREG32(reg_index);
  77. for (i = 0; i < adev->usec_timeout; i++) {
  78. if (check_changed) {
  79. if (val != reg_val)
  80. return 0;
  81. } else {
  82. if ((val & mask) == reg_val)
  83. return 0;
  84. }
  85. udelay(1);
  86. }
  87. return -ETIME;
  88. }
  89. static int
  90. psp_cmd_submit_buf(struct psp_context *psp,
  91. struct amdgpu_firmware_info *ucode,
  92. struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
  93. int index)
  94. {
  95. int ret;
  96. struct amdgpu_bo *cmd_buf_bo;
  97. uint64_t cmd_buf_mc_addr;
  98. struct psp_gfx_cmd_resp *cmd_buf_mem;
  99. struct amdgpu_device *adev = psp->adev;
  100. ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
  101. AMDGPU_GEM_DOMAIN_VRAM,
  102. &cmd_buf_bo, &cmd_buf_mc_addr,
  103. (void **)&cmd_buf_mem);
  104. if (ret)
  105. return ret;
  106. memset(cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
  107. memcpy(cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
  108. ret = psp_cmd_submit(psp, ucode, cmd_buf_mc_addr,
  109. fence_mc_addr, index);
  110. while (*((unsigned int *)psp->fence_buf) != index) {
  111. msleep(1);
  112. };
  113. amdgpu_bo_free_kernel(&cmd_buf_bo,
  114. &cmd_buf_mc_addr,
  115. (void **)&cmd_buf_mem);
  116. return ret;
  117. }
  118. static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  119. uint64_t tmr_mc, uint32_t size)
  120. {
  121. cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
  122. cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = (uint32_t)tmr_mc;
  123. cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = (uint32_t)(tmr_mc >> 32);
  124. cmd->cmd.cmd_setup_tmr.buf_size = size;
  125. }
  126. /* Set up Trusted Memory Region */
  127. static int psp_tmr_init(struct psp_context *psp)
  128. {
  129. int ret;
  130. struct psp_gfx_cmd_resp *cmd;
  131. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  132. if (!cmd)
  133. return -ENOMEM;
  134. /*
  135. * Allocate 3M memory aligned to 1M from Frame Buffer (local
  136. * physical).
  137. *
  138. * Note: this memory need be reserved till the driver
  139. * uninitializes.
  140. */
  141. ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
  142. AMDGPU_GEM_DOMAIN_VRAM,
  143. &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  144. if (ret)
  145. goto failed;
  146. psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
  147. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  148. psp->fence_buf_mc_addr, 1);
  149. if (ret)
  150. goto failed_mem;
  151. kfree(cmd);
  152. return 0;
  153. failed_mem:
  154. amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  155. failed:
  156. kfree(cmd);
  157. return ret;
  158. }
  159. static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  160. uint64_t asd_mc, uint64_t asd_mc_shared,
  161. uint32_t size, uint32_t shared_size)
  162. {
  163. cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
  164. cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
  165. cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
  166. cmd->cmd.cmd_load_ta.app_len = size;
  167. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
  168. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
  169. cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
  170. }
  171. static int psp_asd_load(struct psp_context *psp)
  172. {
  173. int ret;
  174. struct amdgpu_bo *asd_bo, *asd_shared_bo;
  175. uint64_t asd_mc_addr, asd_shared_mc_addr;
  176. void *asd_buf, *asd_shared_buf;
  177. struct psp_gfx_cmd_resp *cmd;
  178. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  179. if (!cmd)
  180. return -ENOMEM;
  181. /*
  182. * Allocate 16k memory aligned to 4k from Frame Buffer (local
  183. * physical) for shared ASD <-> Driver
  184. */
  185. ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE, PAGE_SIZE,
  186. AMDGPU_GEM_DOMAIN_VRAM,
  187. &asd_shared_bo, &asd_shared_mc_addr, &asd_buf);
  188. if (ret)
  189. goto failed;
  190. /*
  191. * Allocate 256k memory aligned to 4k from Frame Buffer (local
  192. * physical) for ASD firmware
  193. */
  194. ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_BIN_SIZE, PAGE_SIZE,
  195. AMDGPU_GEM_DOMAIN_VRAM,
  196. &asd_bo, &asd_mc_addr, &asd_buf);
  197. if (ret)
  198. goto failed_mem;
  199. memcpy(asd_buf, psp->asd_start_addr, psp->asd_ucode_size);
  200. psp_prep_asd_cmd_buf(cmd, asd_mc_addr, asd_shared_mc_addr,
  201. psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
  202. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  203. psp->fence_buf_mc_addr, 2);
  204. if (ret)
  205. goto failed_mem1;
  206. amdgpu_bo_free_kernel(&asd_bo, &asd_mc_addr, &asd_buf);
  207. amdgpu_bo_free_kernel(&asd_shared_bo, &asd_shared_mc_addr, &asd_shared_buf);
  208. kfree(cmd);
  209. return 0;
  210. failed_mem1:
  211. amdgpu_bo_free_kernel(&asd_bo, &asd_mc_addr, &asd_buf);
  212. failed_mem:
  213. amdgpu_bo_free_kernel(&asd_shared_bo, &asd_shared_mc_addr, &asd_shared_buf);
  214. failed:
  215. kfree(cmd);
  216. return ret;
  217. }
  218. static int psp_load_fw(struct amdgpu_device *adev)
  219. {
  220. int ret;
  221. struct psp_gfx_cmd_resp *cmd;
  222. int i;
  223. struct amdgpu_firmware_info *ucode;
  224. struct psp_context *psp = &adev->psp;
  225. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  226. if (!cmd)
  227. return -ENOMEM;
  228. ret = psp_bootloader_load_sysdrv(psp);
  229. if (ret)
  230. goto failed;
  231. ret = psp_bootloader_load_sos(psp);
  232. if (ret)
  233. goto failed;
  234. ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
  235. if (ret)
  236. goto failed;
  237. ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
  238. AMDGPU_GEM_DOMAIN_VRAM,
  239. &psp->fence_buf_bo,
  240. &psp->fence_buf_mc_addr,
  241. &psp->fence_buf);
  242. if (ret)
  243. goto failed;
  244. memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
  245. ret = psp_tmr_init(psp);
  246. if (ret)
  247. goto failed_mem;
  248. ret = psp_asd_load(psp);
  249. if (ret)
  250. goto failed_mem;
  251. for (i = 0; i < adev->firmware.max_ucodes; i++) {
  252. ucode = &adev->firmware.ucode[i];
  253. if (!ucode->fw)
  254. continue;
  255. if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
  256. psp_smu_reload_quirk(psp))
  257. continue;
  258. ret = psp_prep_cmd_buf(ucode, cmd);
  259. if (ret)
  260. goto failed_mem;
  261. ret = psp_cmd_submit_buf(psp, ucode, cmd,
  262. psp->fence_buf_mc_addr, i + 3);
  263. if (ret)
  264. goto failed_mem;
  265. #if 0
  266. /* check if firmware loaded sucessfully */
  267. if (!amdgpu_psp_check_fw_loading_status(adev, i))
  268. return -EINVAL;
  269. #endif
  270. }
  271. amdgpu_bo_free_kernel(&psp->fence_buf_bo,
  272. &psp->fence_buf_mc_addr, &psp->fence_buf);
  273. kfree(cmd);
  274. return 0;
  275. failed_mem:
  276. amdgpu_bo_free_kernel(&psp->fence_buf_bo,
  277. &psp->fence_buf_mc_addr, &psp->fence_buf);
  278. failed:
  279. kfree(cmd);
  280. return ret;
  281. }
  282. static int psp_hw_init(void *handle)
  283. {
  284. int ret;
  285. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  286. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  287. return 0;
  288. mutex_lock(&adev->firmware.mutex);
  289. /*
  290. * This sequence is just used on hw_init only once, no need on
  291. * resume.
  292. */
  293. ret = amdgpu_ucode_init_bo(adev);
  294. if (ret)
  295. goto failed;
  296. ret = psp_load_fw(adev);
  297. if (ret) {
  298. DRM_ERROR("PSP firmware loading failed\n");
  299. goto failed;
  300. }
  301. mutex_unlock(&adev->firmware.mutex);
  302. return 0;
  303. failed:
  304. adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
  305. mutex_unlock(&adev->firmware.mutex);
  306. return -EINVAL;
  307. }
  308. static int psp_hw_fini(void *handle)
  309. {
  310. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  311. struct psp_context *psp = &adev->psp;
  312. if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
  313. amdgpu_ucode_fini_bo(adev);
  314. if (psp->tmr_buf)
  315. amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  316. return 0;
  317. }
  318. static int psp_suspend(void *handle)
  319. {
  320. return 0;
  321. }
  322. static int psp_resume(void *handle)
  323. {
  324. int ret;
  325. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  326. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  327. return 0;
  328. mutex_lock(&adev->firmware.mutex);
  329. ret = psp_load_fw(adev);
  330. if (ret)
  331. DRM_ERROR("PSP resume failed\n");
  332. mutex_unlock(&adev->firmware.mutex);
  333. return ret;
  334. }
  335. static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
  336. enum AMDGPU_UCODE_ID ucode_type)
  337. {
  338. struct amdgpu_firmware_info *ucode = NULL;
  339. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
  340. DRM_INFO("firmware is not loaded by PSP\n");
  341. return true;
  342. }
  343. if (!adev->firmware.fw_size)
  344. return false;
  345. ucode = &adev->firmware.ucode[ucode_type];
  346. if (!ucode->fw || !ucode->ucode_size)
  347. return false;
  348. return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
  349. }
  350. static int psp_set_clockgating_state(void *handle,
  351. enum amd_clockgating_state state)
  352. {
  353. return 0;
  354. }
  355. static int psp_set_powergating_state(void *handle,
  356. enum amd_powergating_state state)
  357. {
  358. return 0;
  359. }
  360. const struct amd_ip_funcs psp_ip_funcs = {
  361. .name = "psp",
  362. .early_init = psp_early_init,
  363. .late_init = NULL,
  364. .sw_init = psp_sw_init,
  365. .sw_fini = psp_sw_fini,
  366. .hw_init = psp_hw_init,
  367. .hw_fini = psp_hw_fini,
  368. .suspend = psp_suspend,
  369. .resume = psp_resume,
  370. .is_idle = NULL,
  371. .wait_for_idle = NULL,
  372. .soft_reset = NULL,
  373. .set_clockgating_state = psp_set_clockgating_state,
  374. .set_powergating_state = psp_set_powergating_state,
  375. };
  376. static const struct amdgpu_psp_funcs psp_funcs = {
  377. .check_fw_loading_status = psp_check_fw_loading_status,
  378. };
  379. static void psp_set_funcs(struct amdgpu_device *adev)
  380. {
  381. if (NULL == adev->firmware.funcs)
  382. adev->firmware.funcs = &psp_funcs;
  383. }
  384. const struct amdgpu_ip_block_version psp_v3_1_ip_block =
  385. {
  386. .type = AMD_IP_BLOCK_TYPE_PSP,
  387. .major = 3,
  388. .minor = 1,
  389. .rev = 0,
  390. .funcs = &psp_ip_funcs,
  391. };