amdgpu_psp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 <drm/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. #include "psp_v10_0.h"
  33. static void psp_set_funcs(struct amdgpu_device *adev);
  34. static int psp_early_init(void *handle)
  35. {
  36. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  37. psp_set_funcs(adev);
  38. return 0;
  39. }
  40. static int psp_sw_init(void *handle)
  41. {
  42. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  43. struct psp_context *psp = &adev->psp;
  44. int ret;
  45. switch (adev->asic_type) {
  46. case CHIP_VEGA10:
  47. psp_v3_1_set_psp_funcs(psp);
  48. break;
  49. case CHIP_RAVEN:
  50. psp_v10_0_set_psp_funcs(psp);
  51. break;
  52. default:
  53. return -EINVAL;
  54. }
  55. psp->adev = adev;
  56. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  57. return 0;
  58. ret = psp_init_microcode(psp);
  59. if (ret) {
  60. DRM_ERROR("Failed to load psp firmware!\n");
  61. return ret;
  62. }
  63. return 0;
  64. }
  65. static int psp_sw_fini(void *handle)
  66. {
  67. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  68. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  69. return 0;
  70. release_firmware(adev->psp.sos_fw);
  71. adev->psp.sos_fw = NULL;
  72. release_firmware(adev->psp.asd_fw);
  73. adev->psp.asd_fw = NULL;
  74. return 0;
  75. }
  76. int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
  77. uint32_t reg_val, uint32_t mask, bool check_changed)
  78. {
  79. uint32_t val;
  80. int i;
  81. struct amdgpu_device *adev = psp->adev;
  82. for (i = 0; i < adev->usec_timeout; i++) {
  83. val = RREG32(reg_index);
  84. if (check_changed) {
  85. if (val != reg_val)
  86. return 0;
  87. } else {
  88. if ((val & mask) == reg_val)
  89. return 0;
  90. }
  91. udelay(1);
  92. }
  93. return -ETIME;
  94. }
  95. static int
  96. psp_cmd_submit_buf(struct psp_context *psp,
  97. struct amdgpu_firmware_info *ucode,
  98. struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
  99. int index)
  100. {
  101. int ret;
  102. memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
  103. memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
  104. ret = psp_cmd_submit(psp, ucode, psp->cmd_buf_mc_addr,
  105. fence_mc_addr, index);
  106. while (*((unsigned int *)psp->fence_buf) != index) {
  107. msleep(1);
  108. }
  109. return ret;
  110. }
  111. static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  112. uint64_t tmr_mc, uint32_t size)
  113. {
  114. cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
  115. cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
  116. cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
  117. cmd->cmd.cmd_setup_tmr.buf_size = size;
  118. }
  119. /* Set up Trusted Memory Region */
  120. static int psp_tmr_init(struct psp_context *psp)
  121. {
  122. int ret;
  123. /*
  124. * Allocate 3M memory aligned to 1M from Frame Buffer (local
  125. * physical).
  126. *
  127. * Note: this memory need be reserved till the driver
  128. * uninitializes.
  129. */
  130. ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
  131. AMDGPU_GEM_DOMAIN_VRAM,
  132. &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  133. return ret;
  134. }
  135. static int psp_tmr_load(struct psp_context *psp)
  136. {
  137. int ret;
  138. struct psp_gfx_cmd_resp *cmd;
  139. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  140. if (!cmd)
  141. return -ENOMEM;
  142. psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
  143. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  144. psp->fence_buf_mc_addr, 1);
  145. if (ret)
  146. goto failed;
  147. kfree(cmd);
  148. return 0;
  149. failed:
  150. kfree(cmd);
  151. return ret;
  152. }
  153. static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  154. uint64_t asd_mc, uint64_t asd_mc_shared,
  155. uint32_t size, uint32_t shared_size)
  156. {
  157. cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
  158. cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
  159. cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
  160. cmd->cmd.cmd_load_ta.app_len = size;
  161. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
  162. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
  163. cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
  164. }
  165. static int psp_asd_init(struct psp_context *psp)
  166. {
  167. int ret;
  168. /*
  169. * Allocate 16k memory aligned to 4k from Frame Buffer (local
  170. * physical) for shared ASD <-> Driver
  171. */
  172. ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
  173. PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
  174. &psp->asd_shared_bo,
  175. &psp->asd_shared_mc_addr,
  176. &psp->asd_shared_buf);
  177. return ret;
  178. }
  179. static int psp_asd_load(struct psp_context *psp)
  180. {
  181. int ret;
  182. struct psp_gfx_cmd_resp *cmd;
  183. /* If PSP version doesn't match ASD version, asd loading will be failed.
  184. * add workaround to bypass it for sriov now.
  185. * TODO: add version check to make it common
  186. */
  187. if (amdgpu_sriov_vf(psp->adev))
  188. return 0;
  189. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  190. if (!cmd)
  191. return -ENOMEM;
  192. memset(psp->fw_pri_buf, 0, PSP_1_MEG);
  193. memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
  194. psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
  195. psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
  196. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  197. psp->fence_buf_mc_addr, 2);
  198. kfree(cmd);
  199. return ret;
  200. }
  201. static int psp_hw_start(struct psp_context *psp)
  202. {
  203. struct amdgpu_device *adev = psp->adev;
  204. int ret;
  205. if (!amdgpu_sriov_vf(adev) || !adev->in_gpu_reset) {
  206. ret = psp_bootloader_load_sysdrv(psp);
  207. if (ret)
  208. return ret;
  209. ret = psp_bootloader_load_sos(psp);
  210. if (ret)
  211. return ret;
  212. }
  213. ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
  214. if (ret)
  215. return ret;
  216. ret = psp_tmr_load(psp);
  217. if (ret)
  218. return ret;
  219. ret = psp_asd_load(psp);
  220. if (ret)
  221. return ret;
  222. return 0;
  223. }
  224. static int psp_np_fw_load(struct psp_context *psp)
  225. {
  226. int i, ret;
  227. struct amdgpu_firmware_info *ucode;
  228. struct amdgpu_device* adev = psp->adev;
  229. for (i = 0; i < adev->firmware.max_ucodes; i++) {
  230. ucode = &adev->firmware.ucode[i];
  231. if (!ucode->fw)
  232. continue;
  233. if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
  234. psp_smu_reload_quirk(psp))
  235. continue;
  236. if (amdgpu_sriov_vf(adev) &&
  237. (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
  238. || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
  239. || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
  240. /*skip ucode loading in SRIOV VF */
  241. continue;
  242. ret = psp_prep_cmd_buf(ucode, psp->cmd);
  243. if (ret)
  244. return ret;
  245. ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
  246. psp->fence_buf_mc_addr, i + 3);
  247. if (ret)
  248. return ret;
  249. #if 0
  250. /* check if firmware loaded sucessfully */
  251. if (!amdgpu_psp_check_fw_loading_status(adev, i))
  252. return -EINVAL;
  253. #endif
  254. }
  255. return 0;
  256. }
  257. static int psp_load_fw(struct amdgpu_device *adev)
  258. {
  259. int ret;
  260. struct psp_context *psp = &adev->psp;
  261. if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset != 0)
  262. goto skip_memalloc;
  263. psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  264. if (!psp->cmd)
  265. return -ENOMEM;
  266. ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
  267. AMDGPU_GEM_DOMAIN_GTT,
  268. &psp->fw_pri_bo,
  269. &psp->fw_pri_mc_addr,
  270. &psp->fw_pri_buf);
  271. if (ret)
  272. goto failed;
  273. ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
  274. AMDGPU_GEM_DOMAIN_VRAM,
  275. &psp->fence_buf_bo,
  276. &psp->fence_buf_mc_addr,
  277. &psp->fence_buf);
  278. if (ret)
  279. goto failed_mem2;
  280. ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
  281. AMDGPU_GEM_DOMAIN_VRAM,
  282. &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
  283. (void **)&psp->cmd_buf_mem);
  284. if (ret)
  285. goto failed_mem1;
  286. memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
  287. ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
  288. if (ret)
  289. goto failed_mem;
  290. ret = psp_tmr_init(psp);
  291. if (ret)
  292. goto failed_mem;
  293. ret = psp_asd_init(psp);
  294. if (ret)
  295. goto failed_mem;
  296. skip_memalloc:
  297. ret = psp_hw_start(psp);
  298. if (ret)
  299. goto failed_mem;
  300. ret = psp_np_fw_load(psp);
  301. if (ret)
  302. goto failed_mem;
  303. return 0;
  304. failed_mem:
  305. amdgpu_bo_free_kernel(&psp->cmd_buf_bo,
  306. &psp->cmd_buf_mc_addr,
  307. (void **)&psp->cmd_buf_mem);
  308. failed_mem1:
  309. amdgpu_bo_free_kernel(&psp->fence_buf_bo,
  310. &psp->fence_buf_mc_addr, &psp->fence_buf);
  311. failed_mem2:
  312. amdgpu_bo_free_kernel(&psp->fw_pri_bo,
  313. &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
  314. failed:
  315. kfree(psp->cmd);
  316. psp->cmd = NULL;
  317. return ret;
  318. }
  319. static int psp_hw_init(void *handle)
  320. {
  321. int ret;
  322. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  323. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  324. return 0;
  325. mutex_lock(&adev->firmware.mutex);
  326. /*
  327. * This sequence is just used on hw_init only once, no need on
  328. * resume.
  329. */
  330. ret = amdgpu_ucode_init_bo(adev);
  331. if (ret)
  332. goto failed;
  333. ret = psp_load_fw(adev);
  334. if (ret) {
  335. DRM_ERROR("PSP firmware loading failed\n");
  336. goto failed;
  337. }
  338. mutex_unlock(&adev->firmware.mutex);
  339. return 0;
  340. failed:
  341. adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
  342. mutex_unlock(&adev->firmware.mutex);
  343. return -EINVAL;
  344. }
  345. static int psp_hw_fini(void *handle)
  346. {
  347. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  348. struct psp_context *psp = &adev->psp;
  349. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  350. return 0;
  351. amdgpu_ucode_fini_bo(adev);
  352. psp_ring_destroy(psp, PSP_RING_TYPE__KM);
  353. amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  354. amdgpu_bo_free_kernel(&psp->fw_pri_bo,
  355. &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
  356. amdgpu_bo_free_kernel(&psp->fence_buf_bo,
  357. &psp->fence_buf_mc_addr, &psp->fence_buf);
  358. amdgpu_bo_free_kernel(&psp->asd_shared_bo, &psp->asd_shared_mc_addr,
  359. &psp->asd_shared_buf);
  360. amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
  361. (void **)&psp->cmd_buf_mem);
  362. kfree(psp->cmd);
  363. psp->cmd = NULL;
  364. return 0;
  365. }
  366. static int psp_suspend(void *handle)
  367. {
  368. int ret;
  369. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  370. struct psp_context *psp = &adev->psp;
  371. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  372. return 0;
  373. ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
  374. if (ret) {
  375. DRM_ERROR("PSP ring stop failed\n");
  376. return ret;
  377. }
  378. return 0;
  379. }
  380. static int psp_resume(void *handle)
  381. {
  382. int ret;
  383. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  384. struct psp_context *psp = &adev->psp;
  385. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  386. return 0;
  387. DRM_INFO("PSP is resuming...\n");
  388. mutex_lock(&adev->firmware.mutex);
  389. ret = psp_hw_start(psp);
  390. if (ret)
  391. goto failed;
  392. ret = psp_np_fw_load(psp);
  393. if (ret)
  394. goto failed;
  395. mutex_unlock(&adev->firmware.mutex);
  396. return 0;
  397. failed:
  398. DRM_ERROR("PSP resume failed\n");
  399. mutex_unlock(&adev->firmware.mutex);
  400. return ret;
  401. }
  402. int psp_gpu_reset(struct amdgpu_device *adev)
  403. {
  404. return psp_mode1_reset(&adev->psp);
  405. }
  406. static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
  407. enum AMDGPU_UCODE_ID ucode_type)
  408. {
  409. struct amdgpu_firmware_info *ucode = NULL;
  410. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
  411. DRM_INFO("firmware is not loaded by PSP\n");
  412. return true;
  413. }
  414. if (!adev->firmware.fw_size)
  415. return false;
  416. ucode = &adev->firmware.ucode[ucode_type];
  417. if (!ucode->fw || !ucode->ucode_size)
  418. return false;
  419. return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
  420. }
  421. static int psp_set_clockgating_state(void *handle,
  422. enum amd_clockgating_state state)
  423. {
  424. return 0;
  425. }
  426. static int psp_set_powergating_state(void *handle,
  427. enum amd_powergating_state state)
  428. {
  429. return 0;
  430. }
  431. const struct amd_ip_funcs psp_ip_funcs = {
  432. .name = "psp",
  433. .early_init = psp_early_init,
  434. .late_init = NULL,
  435. .sw_init = psp_sw_init,
  436. .sw_fini = psp_sw_fini,
  437. .hw_init = psp_hw_init,
  438. .hw_fini = psp_hw_fini,
  439. .suspend = psp_suspend,
  440. .resume = psp_resume,
  441. .is_idle = NULL,
  442. .check_soft_reset = NULL,
  443. .wait_for_idle = NULL,
  444. .soft_reset = NULL,
  445. .set_clockgating_state = psp_set_clockgating_state,
  446. .set_powergating_state = psp_set_powergating_state,
  447. };
  448. static const struct amdgpu_psp_funcs psp_funcs = {
  449. .check_fw_loading_status = psp_check_fw_loading_status,
  450. };
  451. static void psp_set_funcs(struct amdgpu_device *adev)
  452. {
  453. if (NULL == adev->firmware.funcs)
  454. adev->firmware.funcs = &psp_funcs;
  455. }
  456. const struct amdgpu_ip_block_version psp_v3_1_ip_block =
  457. {
  458. .type = AMD_IP_BLOCK_TYPE_PSP,
  459. .major = 3,
  460. .minor = 1,
  461. .rev = 0,
  462. .funcs = &psp_ip_funcs,
  463. };
  464. const struct amdgpu_ip_block_version psp_v10_0_ip_block =
  465. {
  466. .type = AMD_IP_BLOCK_TYPE_PSP,
  467. .major = 10,
  468. .minor = 0,
  469. .rev = 0,
  470. .funcs = &psp_ip_funcs,
  471. };