amdgpu_psp.c 12 KB

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