amdgpu_psp.c 13 KB

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