amdgpu_psp.c 13 KB

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