PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/gpu/drm/amd/amdgpu/amdgpu_psp.c

https://github.com/tklauser/linux-nios2
C | 544 lines | 392 code | 110 blank | 42 comment | 61 complexity | 33ec68699049ca3c6846b7fd48559f01 MD5 | raw file
  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 "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. static void psp_set_funcs(struct amdgpu_device *adev);
  33. static int psp_early_init(void *handle)
  34. {
  35. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  36. psp_set_funcs(adev);
  37. return 0;
  38. }
  39. static int psp_sw_init(void *handle)
  40. {
  41. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  42. struct psp_context *psp = &adev->psp;
  43. int ret;
  44. switch (adev->asic_type) {
  45. case CHIP_VEGA10:
  46. psp->init_microcode = psp_v3_1_init_microcode;
  47. psp->bootloader_load_sysdrv = psp_v3_1_bootloader_load_sysdrv;
  48. psp->bootloader_load_sos = psp_v3_1_bootloader_load_sos;
  49. psp->prep_cmd_buf = psp_v3_1_prep_cmd_buf;
  50. psp->ring_init = psp_v3_1_ring_init;
  51. psp->ring_create = psp_v3_1_ring_create;
  52. psp->ring_destroy = psp_v3_1_ring_destroy;
  53. psp->cmd_submit = psp_v3_1_cmd_submit;
  54. psp->compare_sram_data = psp_v3_1_compare_sram_data;
  55. psp->smu_reload_quirk = psp_v3_1_smu_reload_quirk;
  56. break;
  57. default:
  58. return -EINVAL;
  59. }
  60. psp->adev = adev;
  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. 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. val = RREG32(reg_index);
  79. for (i = 0; i < adev->usec_timeout; i++) {
  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. struct amdgpu_bo *cmd_buf_bo;
  99. uint64_t cmd_buf_mc_addr;
  100. struct psp_gfx_cmd_resp *cmd_buf_mem;
  101. struct amdgpu_device *adev = psp->adev;
  102. ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
  103. AMDGPU_GEM_DOMAIN_VRAM,
  104. &cmd_buf_bo, &cmd_buf_mc_addr,
  105. (void **)&cmd_buf_mem);
  106. if (ret)
  107. return ret;
  108. memset(cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
  109. memcpy(cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
  110. ret = psp_cmd_submit(psp, ucode, cmd_buf_mc_addr,
  111. fence_mc_addr, index);
  112. while (*((unsigned int *)psp->fence_buf) != index) {
  113. msleep(1);
  114. }
  115. amdgpu_bo_free_kernel(&cmd_buf_bo,
  116. &cmd_buf_mc_addr,
  117. (void **)&cmd_buf_mem);
  118. return ret;
  119. }
  120. static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  121. uint64_t tmr_mc, uint32_t size)
  122. {
  123. cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
  124. cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = (uint32_t)tmr_mc;
  125. cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = (uint32_t)(tmr_mc >> 32);
  126. cmd->cmd.cmd_setup_tmr.buf_size = size;
  127. }
  128. /* Set up Trusted Memory Region */
  129. static int psp_tmr_init(struct psp_context *psp)
  130. {
  131. int ret;
  132. /*
  133. * Allocate 3M memory aligned to 1M from Frame Buffer (local
  134. * physical).
  135. *
  136. * Note: this memory need be reserved till the driver
  137. * uninitializes.
  138. */
  139. ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
  140. AMDGPU_GEM_DOMAIN_VRAM,
  141. &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  142. return ret;
  143. }
  144. static int psp_tmr_load(struct psp_context *psp)
  145. {
  146. int ret;
  147. struct psp_gfx_cmd_resp *cmd;
  148. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  149. if (!cmd)
  150. return -ENOMEM;
  151. psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
  152. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  153. psp->fence_buf_mc_addr, 1);
  154. if (ret)
  155. goto failed;
  156. kfree(cmd);
  157. return 0;
  158. failed:
  159. kfree(cmd);
  160. return ret;
  161. }
  162. static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
  163. uint64_t asd_mc, uint64_t asd_mc_shared,
  164. uint32_t size, uint32_t shared_size)
  165. {
  166. cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
  167. cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
  168. cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
  169. cmd->cmd.cmd_load_ta.app_len = size;
  170. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
  171. cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
  172. cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
  173. }
  174. static int psp_asd_init(struct psp_context *psp)
  175. {
  176. int ret;
  177. /*
  178. * Allocate 16k memory aligned to 4k from Frame Buffer (local
  179. * physical) for shared ASD <-> Driver
  180. */
  181. ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
  182. PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
  183. &psp->asd_shared_bo,
  184. &psp->asd_shared_mc_addr,
  185. &psp->asd_shared_buf);
  186. return ret;
  187. }
  188. static int psp_asd_load(struct psp_context *psp)
  189. {
  190. int ret;
  191. struct psp_gfx_cmd_resp *cmd;
  192. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  193. if (!cmd)
  194. return -ENOMEM;
  195. memset(psp->fw_pri_buf, 0, PSP_1_MEG);
  196. memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
  197. psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
  198. psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
  199. ret = psp_cmd_submit_buf(psp, NULL, cmd,
  200. psp->fence_buf_mc_addr, 2);
  201. kfree(cmd);
  202. return ret;
  203. }
  204. static int psp_hw_start(struct psp_context *psp)
  205. {
  206. int ret;
  207. ret = psp_bootloader_load_sysdrv(psp);
  208. if (ret)
  209. return ret;
  210. ret = psp_bootloader_load_sos(psp);
  211. if (ret)
  212. return ret;
  213. ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
  214. if (ret)
  215. return ret;
  216. ret = psp_tmr_load(psp);
  217. if (ret)
  218. return ret;
  219. ret = psp_asd_load(psp);
  220. if (ret)
  221. return ret;
  222. return 0;
  223. }
  224. static int psp_np_fw_load(struct psp_context *psp)
  225. {
  226. int i, ret;
  227. struct amdgpu_firmware_info *ucode;
  228. struct amdgpu_device* adev = psp->adev;
  229. for (i = 0; i < adev->firmware.max_ucodes; i++) {
  230. ucode = &adev->firmware.ucode[i];
  231. if (!ucode->fw)
  232. continue;
  233. if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
  234. psp_smu_reload_quirk(psp))
  235. continue;
  236. if (amdgpu_sriov_vf(adev) &&
  237. (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
  238. || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
  239. || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
  240. /*skip ucode loading in SRIOV VF */
  241. continue;
  242. ret = psp_prep_cmd_buf(ucode, psp->cmd);
  243. if (ret)
  244. return ret;
  245. ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
  246. psp->fence_buf_mc_addr, i + 3);
  247. if (ret)
  248. return ret;
  249. #if 0
  250. /* check if firmware loaded sucessfully */
  251. if (!amdgpu_psp_check_fw_loading_status(adev, i))
  252. return -EINVAL;
  253. #endif
  254. }
  255. return 0;
  256. }
  257. static int psp_load_fw(struct amdgpu_device *adev)
  258. {
  259. int ret;
  260. struct psp_context *psp = &adev->psp;
  261. struct psp_gfx_cmd_resp *cmd;
  262. cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
  263. if (!cmd)
  264. return -ENOMEM;
  265. psp->cmd = cmd;
  266. ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
  267. AMDGPU_GEM_DOMAIN_GTT,
  268. &psp->fw_pri_bo,
  269. &psp->fw_pri_mc_addr,
  270. &psp->fw_pri_buf);
  271. if (ret)
  272. goto failed;
  273. ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
  274. AMDGPU_GEM_DOMAIN_VRAM,
  275. &psp->fence_buf_bo,
  276. &psp->fence_buf_mc_addr,
  277. &psp->fence_buf);
  278. if (ret)
  279. goto failed_mem1;
  280. memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
  281. ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
  282. if (ret)
  283. goto failed_mem1;
  284. ret = psp_tmr_init(psp);
  285. if (ret)
  286. goto failed_mem;
  287. ret = psp_asd_init(psp);
  288. if (ret)
  289. goto failed_mem;
  290. ret = psp_hw_start(psp);
  291. if (ret)
  292. goto failed_mem;
  293. ret = psp_np_fw_load(psp);
  294. if (ret)
  295. goto failed_mem;
  296. kfree(cmd);
  297. return 0;
  298. failed_mem:
  299. amdgpu_bo_free_kernel(&psp->fence_buf_bo,
  300. &psp->fence_buf_mc_addr, &psp->fence_buf);
  301. failed_mem1:
  302. amdgpu_bo_free_kernel(&psp->fw_pri_bo,
  303. &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
  304. failed:
  305. kfree(cmd);
  306. return ret;
  307. }
  308. static int psp_hw_init(void *handle)
  309. {
  310. int ret;
  311. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  312. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  313. return 0;
  314. mutex_lock(&adev->firmware.mutex);
  315. /*
  316. * This sequence is just used on hw_init only once, no need on
  317. * resume.
  318. */
  319. ret = amdgpu_ucode_init_bo(adev);
  320. if (ret)
  321. goto failed;
  322. ret = psp_load_fw(adev);
  323. if (ret) {
  324. DRM_ERROR("PSP firmware loading failed\n");
  325. goto failed;
  326. }
  327. mutex_unlock(&adev->firmware.mutex);
  328. return 0;
  329. failed:
  330. adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
  331. mutex_unlock(&adev->firmware.mutex);
  332. return -EINVAL;
  333. }
  334. static int psp_hw_fini(void *handle)
  335. {
  336. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  337. struct psp_context *psp = &adev->psp;
  338. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  339. return 0;
  340. amdgpu_ucode_fini_bo(adev);
  341. psp_ring_destroy(psp, PSP_RING_TYPE__KM);
  342. if (psp->tmr_buf)
  343. amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
  344. if (psp->fw_pri_buf)
  345. amdgpu_bo_free_kernel(&psp->fw_pri_bo,
  346. &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
  347. if (psp->fence_buf_bo)
  348. amdgpu_bo_free_kernel(&psp->fence_buf_bo,
  349. &psp->fence_buf_mc_addr, &psp->fence_buf);
  350. return 0;
  351. }
  352. static int psp_suspend(void *handle)
  353. {
  354. return 0;
  355. }
  356. static int psp_resume(void *handle)
  357. {
  358. int ret;
  359. struct amdgpu_device *adev = (struct amdgpu_device *)handle;
  360. struct psp_context *psp = &adev->psp;
  361. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
  362. return 0;
  363. DRM_INFO("PSP is resuming...\n");
  364. mutex_lock(&adev->firmware.mutex);
  365. ret = psp_hw_start(psp);
  366. if (ret)
  367. goto failed;
  368. ret = psp_np_fw_load(psp);
  369. if (ret)
  370. goto failed;
  371. mutex_unlock(&adev->firmware.mutex);
  372. return 0;
  373. failed:
  374. DRM_ERROR("PSP resume failed\n");
  375. mutex_unlock(&adev->firmware.mutex);
  376. return ret;
  377. }
  378. static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
  379. enum AMDGPU_UCODE_ID ucode_type)
  380. {
  381. struct amdgpu_firmware_info *ucode = NULL;
  382. if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
  383. DRM_INFO("firmware is not loaded by PSP\n");
  384. return true;
  385. }
  386. if (!adev->firmware.fw_size)
  387. return false;
  388. ucode = &adev->firmware.ucode[ucode_type];
  389. if (!ucode->fw || !ucode->ucode_size)
  390. return false;
  391. return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
  392. }
  393. static int psp_set_clockgating_state(void *handle,
  394. enum amd_clockgating_state state)
  395. {
  396. return 0;
  397. }
  398. static int psp_set_powergating_state(void *handle,
  399. enum amd_powergating_state state)
  400. {
  401. return 0;
  402. }
  403. const struct amd_ip_funcs psp_ip_funcs = {
  404. .name = "psp",
  405. .early_init = psp_early_init,
  406. .late_init = NULL,
  407. .sw_init = psp_sw_init,
  408. .sw_fini = psp_sw_fini,
  409. .hw_init = psp_hw_init,
  410. .hw_fini = psp_hw_fini,
  411. .suspend = psp_suspend,
  412. .resume = psp_resume,
  413. .is_idle = NULL,
  414. .wait_for_idle = NULL,
  415. .soft_reset = NULL,
  416. .set_clockgating_state = psp_set_clockgating_state,
  417. .set_powergating_state = psp_set_powergating_state,
  418. };
  419. static const struct amdgpu_psp_funcs psp_funcs = {
  420. .check_fw_loading_status = psp_check_fw_loading_status,
  421. };
  422. static void psp_set_funcs(struct amdgpu_device *adev)
  423. {
  424. if (NULL == adev->firmware.funcs)
  425. adev->firmware.funcs = &psp_funcs;
  426. }
  427. const struct amdgpu_ip_block_version psp_v3_1_ip_block =
  428. {
  429. .type = AMD_IP_BLOCK_TYPE_PSP,
  430. .major = 3,
  431. .minor = 1,
  432. .rev = 0,
  433. .funcs = &psp_ip_funcs,
  434. };