amdgpu_psp.c 13 KB

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