amdgpu_psp.c 14 KB

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