amdgpu_psp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. psp->init_microcode = psp_v3_1_init_microcode;
  42. psp->bootloader_load_sysdrv = psp_v3_1_bootloader_load_sysdrv;
  43. psp->bootloader_load_sos = psp_v3_1_bootloader_load_sos;
  44. psp->prep_cmd_buf = psp_v3_1_prep_cmd_buf;
  45. psp->ring_init = psp_v3_1_ring_init;
  46. psp->ring_create = psp_v3_1_ring_create;
  47. psp->ring_destroy = psp_v3_1_ring_destroy;
  48. psp->cmd_submit = psp_v3_1_cmd_submit;
  49. psp->compare_sram_data = psp_v3_1_compare_sram_data;
  50. psp->smu_reload_quirk = psp_v3_1_smu_reload_quirk;
  51. break;
  52. case CHIP_RAVEN:
  53. #if 0
  54. psp->init_microcode = psp_v10_0_init_microcode;
  55. #endif
  56. psp->prep_cmd_buf = psp_v10_0_prep_cmd_buf;
  57. psp->ring_init = psp_v10_0_ring_init;
  58. psp->ring_create = psp_v10_0_ring_create;
  59. psp->ring_destroy = psp_v10_0_ring_destroy;
  60. psp->cmd_submit = psp_v10_0_cmd_submit;
  61. psp->compare_sram_data = psp_v10_0_compare_sram_data;
  62. break;
  63. default:
  64. return -EINVAL;
  65. }
  66. psp->adev = adev;
  67. return 0;
  68. }
  69. static int psp_sw_init(void *handle)
  70. {
  71. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  72. struct psp_context *psp = &adev->psp;
  73. int ret;
  74. ret = psp_init_microcode(psp);
  75. if (ret) {
  76. DRM_ERROR("Failed to load psp firmware!\n");
  77. return ret;
  78. }
  79. return 0;
  80. }
  81. static int psp_sw_fini(void *handle)
  82. {
  83. return 0;
  84. }
  85. int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
  86. uint32_t reg_val, uint32_t mask, bool check_changed)
  87. {
  88. uint32_t val;
  89. int i;
  90. struct amdgpu_device *adev = psp->adev;
  91. for (i = 0; i < adev->usec_timeout; i++) {
  92. val = RREG32(reg_index);
  93. if (check_changed) {
  94. if (val != reg_val)
  95. return 0;
  96. } else {
  97. if ((val & mask) == reg_val)
  98. return 0;
  99. }
  100. udelay(1);
  101. }
  102. return -ETIME;
  103. }
  104. static int
  105. psp_cmd_submit_buf(struct psp_context *psp,
  106. struct amdgpu_firmware_info *ucode,
  107. struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
  108. int index)
  109. {
  110. int ret;
  111. memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
  112. memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
  113. ret = psp_cmd_submit(psp, ucode, psp->cmd_buf_mc_addr,
  114. fence_mc_addr, index);
  115. while (*((unsigned int *)psp->fence_buf) != index) {
  116. msleep(1);
  117. }
  118. if (ucode) {
  119. ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
  120. ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
  121. }
  122. return ret;
  123. }
  124. static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  125. uint64_t tmr_mc, uint32_t size)
  126. {
  127. cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
  128. cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
  129. cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
  130. cmd->cmd.cmd_setup_tmr.buf_size = size;
  131. }
  132. /* Set up Trusted Memory Region */
  133. static int psp_tmr_init(struct psp_context *psp)
  134. {
  135. int ret;
  136. /*
  137. * Allocate 3M memory aligned to 1M from Frame Buffer (local
  138. * physical).
  139. *
  140. * Note: this memory need be reserved till the driver
  141. * uninitializes.
  142. */
  143. ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
  144. AMDGPU_GEM_DOMAIN_VRAM,
  145. &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  146. return ret;
  147. }
  148. static int psp_tmr_load(struct psp_context *psp)
  149. {
  150. int ret;
  151. struct psp_gfx_cmd_resp *cmd;
  152. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  153. if (!cmd)
  154. return -ENOMEM;
  155. psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
  156. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  157. psp->fence_buf_mc_addr, 1);
  158. if (ret)
  159. goto failed;
  160. kfree(cmd);
  161. return 0;
  162. failed:
  163. kfree(cmd);
  164. return ret;
  165. }
  166. static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  167. uint64_t asd_mc, uint64_t asd_mc_shared,
  168. uint32_t size, uint32_t shared_size)
  169. {
  170. cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
  171. cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
  172. cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
  173. cmd->cmd.cmd_load_ta.app_len = size;
  174. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
  175. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
  176. cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
  177. }
  178. static int psp_asd_init(struct psp_context *psp)
  179. {
  180. int ret;
  181. /*
  182. * Allocate 16k memory aligned to 4k from Frame Buffer (local
  183. * physical) for shared ASD <-> Driver
  184. */
  185. ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
  186. PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
  187. &psp->asd_shared_bo,
  188. &psp->asd_shared_mc_addr,
  189. &psp->asd_shared_buf);
  190. return ret;
  191. }
  192. static int psp_asd_load(struct psp_context *psp)
  193. {
  194. int ret;
  195. struct psp_gfx_cmd_resp *cmd;
  196. /* If PSP version doesn't match ASD version, asd loading will be failed.
  197. * add workaround to bypass it for sriov now.
  198. * TODO: add version check to make it common
  199. */
  200. if (amdgpu_sriov_vf(psp->adev))
  201. return 0;
  202. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  203. if (!cmd)
  204. return -ENOMEM;
  205. memset(psp->fw_pri_buf, 0, PSP_1_MEG);
  206. memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
  207. psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
  208. psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
  209. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  210. psp->fence_buf_mc_addr, 2);
  211. kfree(cmd);
  212. return ret;
  213. }
  214. static int psp_hw_start(struct psp_context *psp)
  215. {
  216. int ret;
  217. ret = psp_bootloader_load_sysdrv(psp);
  218. if (ret)
  219. return ret;
  220. ret = psp_bootloader_load_sos(psp);
  221. if (ret)
  222. return ret;
  223. ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
  224. if (ret)
  225. return ret;
  226. ret = psp_tmr_load(psp);
  227. if (ret)
  228. return ret;
  229. ret = psp_asd_load(psp);
  230. if (ret)
  231. return ret;
  232. return 0;
  233. }
  234. static int psp_np_fw_load(struct psp_context *psp)
  235. {
  236. int i, ret;
  237. struct amdgpu_firmware_info *ucode;
  238. struct amdgpu_device* adev = psp->adev;
  239. for (i = 0; i < adev->firmware.max_ucodes; i++) {
  240. ucode = &adev->firmware.ucode[i];
  241. if (!ucode->fw)
  242. continue;
  243. if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
  244. psp_smu_reload_quirk(psp))
  245. continue;
  246. if (amdgpu_sriov_vf(adev) &&
  247. (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
  248. || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
  249. || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
  250. /*skip ucode loading in SRIOV VF */
  251. continue;
  252. ret = psp_prep_cmd_buf(ucode, psp->cmd);
  253. if (ret)
  254. return ret;
  255. ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
  256. psp->fence_buf_mc_addr, i + 3);
  257. if (ret)
  258. return ret;
  259. #if 0
  260. /* check if firmware loaded sucessfully */
  261. if (!amdgpu_psp_check_fw_loading_status(adev, i))
  262. return -EINVAL;
  263. #endif
  264. }
  265. return 0;
  266. }
  267. static int psp_load_fw(struct amdgpu_device *adev)
  268. {
  269. int ret;
  270. struct psp_context *psp = &adev->psp;
  271. psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  272. if (!psp->cmd)
  273. return -ENOMEM;
  274. ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
  275. AMDGPU_GEM_DOMAIN_GTT,
  276. &psp->fw_pri_bo,
  277. &psp->fw_pri_mc_addr,
  278. &psp->fw_pri_buf);
  279. if (ret)
  280. goto failed;
  281. ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
  282. AMDGPU_GEM_DOMAIN_VRAM,
  283. &psp->fence_buf_bo,
  284. &psp->fence_buf_mc_addr,
  285. &psp->fence_buf);
  286. if (ret)
  287. goto failed_mem2;
  288. ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
  289. AMDGPU_GEM_DOMAIN_VRAM,
  290. &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
  291. (void **)&psp->cmd_buf_mem);
  292. if (ret)
  293. goto failed_mem1;
  294. memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
  295. ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
  296. if (ret)
  297. goto failed_mem;
  298. ret = psp_tmr_init(psp);
  299. if (ret)
  300. goto failed_mem;
  301. ret = psp_asd_init(psp);
  302. if (ret)
  303. goto failed_mem;
  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. return 0;
  376. }
  377. static int psp_resume(void *handle)
  378. {
  379. int ret;
  380. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  381. struct psp_context *psp = &adev->psp;
  382. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  383. return 0;
  384. DRM_INFO("PSP is resuming...\n");
  385. mutex_lock(&adev->firmware.mutex);
  386. ret = psp_hw_start(psp);
  387. if (ret)
  388. goto failed;
  389. ret = psp_np_fw_load(psp);
  390. if (ret)
  391. goto failed;
  392. mutex_unlock(&adev->firmware.mutex);
  393. return 0;
  394. failed:
  395. DRM_ERROR("PSP resume failed\n");
  396. mutex_unlock(&adev->firmware.mutex);
  397. return ret;
  398. }
  399. static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
  400. enum AMDGPU_UCODE_ID ucode_type)
  401. {
  402. struct amdgpu_firmware_info *ucode = NULL;
  403. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
  404. DRM_INFO("firmware is not loaded by PSP\n");
  405. return true;
  406. }
  407. if (!adev->firmware.fw_size)
  408. return false;
  409. ucode = &adev->firmware.ucode[ucode_type];
  410. if (!ucode->fw || !ucode->ucode_size)
  411. return false;
  412. return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
  413. }
  414. static int psp_set_clockgating_state(void *handle,
  415. enum amd_clockgating_state state)
  416. {
  417. return 0;
  418. }
  419. static int psp_set_powergating_state(void *handle,
  420. enum amd_powergating_state state)
  421. {
  422. return 0;
  423. }
  424. const struct amd_ip_funcs psp_ip_funcs = {
  425. .name = "psp",
  426. .early_init = psp_early_init,
  427. .late_init = NULL,
  428. .sw_init = psp_sw_init,
  429. .sw_fini = psp_sw_fini,
  430. .hw_init = psp_hw_init,
  431. .hw_fini = psp_hw_fini,
  432. .suspend = psp_suspend,
  433. .resume = psp_resume,
  434. .is_idle = NULL,
  435. .wait_for_idle = NULL,
  436. .soft_reset = NULL,
  437. .set_clockgating_state = psp_set_clockgating_state,
  438. .set_powergating_state = psp_set_powergating_state,
  439. };
  440. static const struct amdgpu_psp_funcs psp_funcs = {
  441. .check_fw_loading_status = psp_check_fw_loading_status,
  442. };
  443. static void psp_set_funcs(struct amdgpu_device *adev)
  444. {
  445. if (NULL == adev->firmware.funcs)
  446. adev->firmware.funcs = &psp_funcs;
  447. }
  448. const struct amdgpu_ip_block_version psp_v3_1_ip_block =
  449. {
  450. .type = AMD_IP_BLOCK_TYPE_PSP,
  451. .major = 3,
  452. .minor = 1,
  453. .rev = 0,
  454. .funcs = &psp_ip_funcs,
  455. };
  456. const struct amdgpu_ip_block_version psp_v10_0_ip_block =
  457. {
  458. .type = AMD_IP_BLOCK_TYPE_PSP,
  459. .major = 10,
  460. .minor = 0,
  461. .rev = 0,
  462. .funcs = &psp_ip_funcs,
  463. };