amdgpu_psp.c 13 KB

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