amdgpu_psp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. /*
  131. * Allocate 3M memory aligned to 1M from Frame Buffer (local
  132. * physical).
  133. *
  134. * Note: this memory need be reserved till the driver
  135. * uninitializes.
  136. */
  137. ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
  138. AMDGPU_GEM_DOMAIN_VRAM,
  139. &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  140. return ret;
  141. }
  142. static int psp_tmr_load(struct psp_context *psp)
  143. {
  144. int ret;
  145. struct psp_gfx_cmd_resp *cmd;
  146. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  147. if (!cmd)
  148. return -ENOMEM;
  149. psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
  150. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  151. psp->fence_buf_mc_addr, 1);
  152. if (ret)
  153. goto failed;
  154. kfree(cmd);
  155. return 0;
  156. failed:
  157. kfree(cmd);
  158. return ret;
  159. }
  160. static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  161. uint64_t asd_mc, uint64_t asd_mc_shared,
  162. uint32_t size, uint32_t shared_size)
  163. {
  164. cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
  165. cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
  166. cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
  167. cmd->cmd.cmd_load_ta.app_len = size;
  168. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
  169. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
  170. cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
  171. }
  172. static int psp_asd_load(struct psp_context *psp)
  173. {
  174. int ret;
  175. struct amdgpu_bo *asd_bo, *asd_shared_bo;
  176. uint64_t asd_mc_addr, asd_shared_mc_addr;
  177. void *asd_buf, *asd_shared_buf;
  178. struct psp_gfx_cmd_resp *cmd;
  179. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  180. if (!cmd)
  181. return -ENOMEM;
  182. /*
  183. * Allocate 16k memory aligned to 4k from Frame Buffer (local
  184. * physical) for shared ASD <-> Driver
  185. */
  186. ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE, PAGE_SIZE,
  187. AMDGPU_GEM_DOMAIN_VRAM,
  188. &asd_shared_bo, &asd_shared_mc_addr, &asd_buf);
  189. if (ret)
  190. goto failed;
  191. /*
  192. * Allocate 256k memory aligned to 4k from Frame Buffer (local
  193. * physical) for ASD firmware
  194. */
  195. ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_BIN_SIZE, PAGE_SIZE,
  196. AMDGPU_GEM_DOMAIN_VRAM,
  197. &asd_bo, &asd_mc_addr, &asd_buf);
  198. if (ret)
  199. goto failed_mem;
  200. memcpy(asd_buf, psp->asd_start_addr, psp->asd_ucode_size);
  201. psp_prep_asd_cmd_buf(cmd, asd_mc_addr, asd_shared_mc_addr,
  202. psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
  203. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  204. psp->fence_buf_mc_addr, 2);
  205. if (ret)
  206. goto failed_mem1;
  207. amdgpu_bo_free_kernel(&asd_bo, &asd_mc_addr, &asd_buf);
  208. amdgpu_bo_free_kernel(&asd_shared_bo, &asd_shared_mc_addr, &asd_shared_buf);
  209. kfree(cmd);
  210. return 0;
  211. failed_mem1:
  212. amdgpu_bo_free_kernel(&asd_bo, &asd_mc_addr, &asd_buf);
  213. failed_mem:
  214. amdgpu_bo_free_kernel(&asd_shared_bo, &asd_shared_mc_addr, &asd_shared_buf);
  215. failed:
  216. kfree(cmd);
  217. return ret;
  218. }
  219. static int psp_load_fw(struct amdgpu_device *adev)
  220. {
  221. int ret;
  222. struct psp_gfx_cmd_resp *cmd;
  223. int i;
  224. struct amdgpu_firmware_info *ucode;
  225. struct psp_context *psp = &adev->psp;
  226. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  227. if (!cmd)
  228. return -ENOMEM;
  229. ret = psp_bootloader_load_sysdrv(psp);
  230. if (ret)
  231. goto failed;
  232. ret = psp_bootloader_load_sos(psp);
  233. if (ret)
  234. goto failed;
  235. ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
  236. if (ret)
  237. goto failed;
  238. ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
  239. AMDGPU_GEM_DOMAIN_VRAM,
  240. &psp->fence_buf_bo,
  241. &psp->fence_buf_mc_addr,
  242. &psp->fence_buf);
  243. if (ret)
  244. goto failed;
  245. memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
  246. ret = psp_tmr_init(psp);
  247. if (ret)
  248. goto failed_mem;
  249. ret = psp_tmr_load(psp);
  250. if (ret)
  251. goto failed_mem;
  252. ret = psp_asd_load(psp);
  253. if (ret)
  254. goto failed_mem;
  255. for (i = 0; i < adev->firmware.max_ucodes; i++) {
  256. ucode = &adev->firmware.ucode[i];
  257. if (!ucode->fw)
  258. continue;
  259. if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
  260. psp_smu_reload_quirk(psp))
  261. continue;
  262. ret = psp_prep_cmd_buf(ucode, cmd);
  263. if (ret)
  264. goto failed_mem;
  265. ret = psp_cmd_submit_buf(psp, ucode, cmd,
  266. psp->fence_buf_mc_addr, i + 3);
  267. if (ret)
  268. goto failed_mem;
  269. #if 0
  270. /* check if firmware loaded sucessfully */
  271. if (!amdgpu_psp_check_fw_loading_status(adev, i))
  272. return -EINVAL;
  273. #endif
  274. }
  275. amdgpu_bo_free_kernel(&psp->fence_buf_bo,
  276. &psp->fence_buf_mc_addr, &psp->fence_buf);
  277. kfree(cmd);
  278. return 0;
  279. failed_mem:
  280. amdgpu_bo_free_kernel(&psp->fence_buf_bo,
  281. &psp->fence_buf_mc_addr, &psp->fence_buf);
  282. failed:
  283. kfree(cmd);
  284. return ret;
  285. }
  286. static int psp_hw_init(void *handle)
  287. {
  288. int ret;
  289. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  290. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  291. return 0;
  292. mutex_lock(&adev->firmware.mutex);
  293. /*
  294. * This sequence is just used on hw_init only once, no need on
  295. * resume.
  296. */
  297. ret = amdgpu_ucode_init_bo(adev);
  298. if (ret)
  299. goto failed;
  300. ret = psp_load_fw(adev);
  301. if (ret) {
  302. DRM_ERROR("PSP firmware loading failed\n");
  303. goto failed;
  304. }
  305. mutex_unlock(&adev->firmware.mutex);
  306. return 0;
  307. failed:
  308. adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
  309. mutex_unlock(&adev->firmware.mutex);
  310. return -EINVAL;
  311. }
  312. static int psp_hw_fini(void *handle)
  313. {
  314. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  315. struct psp_context *psp = &adev->psp;
  316. if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP)
  317. amdgpu_ucode_fini_bo(adev);
  318. if (psp->tmr_buf)
  319. amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  320. return 0;
  321. }
  322. static int psp_suspend(void *handle)
  323. {
  324. return 0;
  325. }
  326. static int psp_resume(void *handle)
  327. {
  328. int ret;
  329. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  330. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  331. return 0;
  332. mutex_lock(&adev->firmware.mutex);
  333. ret = psp_load_fw(adev);
  334. if (ret)
  335. DRM_ERROR("PSP resume failed\n");
  336. mutex_unlock(&adev->firmware.mutex);
  337. return ret;
  338. }
  339. static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
  340. enum AMDGPU_UCODE_ID ucode_type)
  341. {
  342. struct amdgpu_firmware_info *ucode = NULL;
  343. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
  344. DRM_INFO("firmware is not loaded by PSP\n");
  345. return true;
  346. }
  347. if (!adev->firmware.fw_size)
  348. return false;
  349. ucode = &adev->firmware.ucode[ucode_type];
  350. if (!ucode->fw || !ucode->ucode_size)
  351. return false;
  352. return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
  353. }
  354. static int psp_set_clockgating_state(void *handle,
  355. enum amd_clockgating_state state)
  356. {
  357. return 0;
  358. }
  359. static int psp_set_powergating_state(void *handle,
  360. enum amd_powergating_state state)
  361. {
  362. return 0;
  363. }
  364. const struct amd_ip_funcs psp_ip_funcs = {
  365. .name = "psp",
  366. .early_init = psp_early_init,
  367. .late_init = NULL,
  368. .sw_init = psp_sw_init,
  369. .sw_fini = psp_sw_fini,
  370. .hw_init = psp_hw_init,
  371. .hw_fini = psp_hw_fini,
  372. .suspend = psp_suspend,
  373. .resume = psp_resume,
  374. .is_idle = NULL,
  375. .wait_for_idle = NULL,
  376. .soft_reset = NULL,
  377. .set_clockgating_state = psp_set_clockgating_state,
  378. .set_powergating_state = psp_set_powergating_state,
  379. };
  380. static const struct amdgpu_psp_funcs psp_funcs = {
  381. .check_fw_loading_status = psp_check_fw_loading_status,
  382. };
  383. static void psp_set_funcs(struct amdgpu_device *adev)
  384. {
  385. if (NULL == adev->firmware.funcs)
  386. adev->firmware.funcs = &psp_funcs;
  387. }
  388. const struct amdgpu_ip_block_version psp_v3_1_ip_block =
  389. {
  390. .type = AMD_IP_BLOCK_TYPE_PSP,
  391. .major = 3,
  392. .minor = 1,
  393. .rev = 0,
  394. .funcs = &psp_ip_funcs,
  395. };