amdgpu_psp.c 13 KB

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