amdgpu_psp.c 11 KB

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