amdgpu_psp.c 14 KB

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