2 * Copyright 2016 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <linux/firmware.h>
29 #include "amdgpu_psp.h"
30 #include "amdgpu_ucode.h"
31 #include "soc15_common.h"
33 #include "psp_v10_0.h"
35 static void psp_set_funcs(struct amdgpu_device *adev);
37 static int psp_early_init(void *handle)
39 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
46 static int psp_sw_init(void *handle)
48 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
49 struct psp_context *psp = &adev->psp;
52 switch (adev->asic_type) {
55 psp_v3_1_set_psp_funcs(psp);
58 psp_v10_0_set_psp_funcs(psp);
66 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
69 ret = psp_init_microcode(psp);
71 DRM_ERROR("Failed to load psp firmware!\n");
78 static int psp_sw_fini(void *handle)
80 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
82 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
85 release_firmware(adev->psp.sos_fw);
86 adev->psp.sos_fw = NULL;
87 release_firmware(adev->psp.asd_fw);
88 adev->psp.asd_fw = NULL;
92 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
93 uint32_t reg_val, uint32_t mask, bool check_changed)
97 struct amdgpu_device *adev = psp->adev;
99 for (i = 0; i < adev->usec_timeout; i++) {
100 val = RREG32(reg_index);
105 if ((val & mask) == reg_val)
115 psp_cmd_submit_buf(struct psp_context *psp,
116 struct amdgpu_firmware_info *ucode,
117 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
122 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
124 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
126 ret = psp_cmd_submit(psp, ucode, psp->cmd_buf_mc_addr,
127 fence_mc_addr, index);
129 while (*((unsigned int *)psp->fence_buf) != index) {
136 static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
137 uint64_t tmr_mc, uint32_t size)
139 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
140 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
141 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
142 cmd->cmd.cmd_setup_tmr.buf_size = size;
145 /* Set up Trusted Memory Region */
146 static int psp_tmr_init(struct psp_context *psp)
151 * Allocate 3M memory aligned to 1M from Frame Buffer (local
154 * Note: this memory need be reserved till the driver
157 ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
158 AMDGPU_GEM_DOMAIN_VRAM,
159 &psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
164 static int psp_tmr_load(struct psp_context *psp)
167 struct psp_gfx_cmd_resp *cmd;
169 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
173 psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
175 ret = psp_cmd_submit_buf(psp, NULL, cmd,
176 psp->fence_buf_mc_addr, 1);
189 static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
190 uint64_t asd_mc, uint64_t asd_mc_shared,
191 uint32_t size, uint32_t shared_size)
193 cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
194 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
195 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
196 cmd->cmd.cmd_load_ta.app_len = size;
198 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
199 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
200 cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
203 static int psp_asd_init(struct psp_context *psp)
208 * Allocate 16k memory aligned to 4k from Frame Buffer (local
209 * physical) for shared ASD <-> Driver
211 ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
212 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
214 &psp->asd_shared_mc_addr,
215 &psp->asd_shared_buf);
220 static int psp_asd_load(struct psp_context *psp)
223 struct psp_gfx_cmd_resp *cmd;
225 /* If PSP version doesn't match ASD version, asd loading will be failed.
226 * add workaround to bypass it for sriov now.
227 * TODO: add version check to make it common
229 if (amdgpu_sriov_vf(psp->adev))
232 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
236 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
237 memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
239 psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
240 psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
242 ret = psp_cmd_submit_buf(psp, NULL, cmd,
243 psp->fence_buf_mc_addr, 2);
250 static int psp_hw_start(struct psp_context *psp)
252 struct amdgpu_device *adev = psp->adev;
255 if (!amdgpu_sriov_vf(adev) || !adev->in_gpu_reset) {
256 ret = psp_bootloader_load_sysdrv(psp);
260 ret = psp_bootloader_load_sos(psp);
265 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
269 ret = psp_tmr_load(psp);
273 ret = psp_asd_load(psp);
280 static int psp_np_fw_load(struct psp_context *psp)
283 struct amdgpu_firmware_info *ucode;
284 struct amdgpu_device* adev = psp->adev;
286 for (i = 0; i < adev->firmware.max_ucodes; i++) {
287 ucode = &adev->firmware.ucode[i];
291 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
292 psp_smu_reload_quirk(psp))
294 if (amdgpu_sriov_vf(adev) &&
295 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
296 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
297 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
298 /*skip ucode loading in SRIOV VF */
301 ret = psp_prep_cmd_buf(ucode, psp->cmd);
305 ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
306 psp->fence_buf_mc_addr, i + 3);
311 /* check if firmware loaded sucessfully */
312 if (!amdgpu_psp_check_fw_loading_status(adev, i))
320 static int psp_load_fw(struct amdgpu_device *adev)
323 struct psp_context *psp = &adev->psp;
325 if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset != 0)
328 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
332 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
333 AMDGPU_GEM_DOMAIN_GTT,
335 &psp->fw_pri_mc_addr,
340 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
341 AMDGPU_GEM_DOMAIN_VRAM,
343 &psp->fence_buf_mc_addr,
348 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
349 AMDGPU_GEM_DOMAIN_VRAM,
350 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
351 (void **)&psp->cmd_buf_mem);
355 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
357 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
361 ret = psp_tmr_init(psp);
365 ret = psp_asd_init(psp);
370 ret = psp_hw_start(psp);
374 ret = psp_np_fw_load(psp);
381 amdgpu_bo_free_kernel(&psp->cmd_buf_bo,
382 &psp->cmd_buf_mc_addr,
383 (void **)&psp->cmd_buf_mem);
385 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
386 &psp->fence_buf_mc_addr, &psp->fence_buf);
388 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
389 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
396 static int psp_hw_init(void *handle)
399 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
402 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
405 mutex_lock(&adev->firmware.mutex);
407 * This sequence is just used on hw_init only once, no need on
410 ret = amdgpu_ucode_init_bo(adev);
414 ret = psp_load_fw(adev);
416 DRM_ERROR("PSP firmware loading failed\n");
420 mutex_unlock(&adev->firmware.mutex);
424 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
425 mutex_unlock(&adev->firmware.mutex);
429 static int psp_hw_fini(void *handle)
431 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
432 struct psp_context *psp = &adev->psp;
434 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
437 amdgpu_ucode_fini_bo(adev);
439 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
441 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, &psp->tmr_buf);
442 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
443 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
444 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
445 &psp->fence_buf_mc_addr, &psp->fence_buf);
446 amdgpu_bo_free_kernel(&psp->asd_shared_bo, &psp->asd_shared_mc_addr,
447 &psp->asd_shared_buf);
448 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
449 (void **)&psp->cmd_buf_mem);
457 static int psp_suspend(void *handle)
460 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
461 struct psp_context *psp = &adev->psp;
463 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
466 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
468 DRM_ERROR("PSP ring stop failed\n");
475 static int psp_resume(void *handle)
478 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
479 struct psp_context *psp = &adev->psp;
481 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
484 DRM_INFO("PSP is resuming...\n");
486 mutex_lock(&adev->firmware.mutex);
488 ret = psp_hw_start(psp);
492 ret = psp_np_fw_load(psp);
496 mutex_unlock(&adev->firmware.mutex);
501 DRM_ERROR("PSP resume failed\n");
502 mutex_unlock(&adev->firmware.mutex);
506 int psp_gpu_reset(struct amdgpu_device *adev)
508 return psp_mode1_reset(&adev->psp);
511 static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
512 enum AMDGPU_UCODE_ID ucode_type)
514 struct amdgpu_firmware_info *ucode = NULL;
516 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
517 DRM_INFO("firmware is not loaded by PSP\n");
521 if (!adev->firmware.fw_size)
524 ucode = &adev->firmware.ucode[ucode_type];
525 if (!ucode->fw || !ucode->ucode_size)
528 return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
531 static int psp_set_clockgating_state(void *handle,
532 enum amd_clockgating_state state)
537 static int psp_set_powergating_state(void *handle,
538 enum amd_powergating_state state)
543 const struct amd_ip_funcs psp_ip_funcs = {
545 .early_init = psp_early_init,
547 .sw_init = psp_sw_init,
548 .sw_fini = psp_sw_fini,
549 .hw_init = psp_hw_init,
550 .hw_fini = psp_hw_fini,
551 .suspend = psp_suspend,
552 .resume = psp_resume,
554 .check_soft_reset = NULL,
555 .wait_for_idle = NULL,
557 .set_clockgating_state = psp_set_clockgating_state,
558 .set_powergating_state = psp_set_powergating_state,
561 static const struct amdgpu_psp_funcs psp_funcs = {
562 .check_fw_loading_status = psp_check_fw_loading_status,
565 static void psp_set_funcs(struct amdgpu_device *adev)
567 if (NULL == adev->firmware.funcs)
568 adev->firmware.funcs = &psp_funcs;
571 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
573 .type = AMD_IP_BLOCK_TYPE_PSP,
577 .funcs = &psp_ip_funcs,
580 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
582 .type = AMD_IP_BLOCK_TYPE_PSP,
586 .funcs = &psp_ip_funcs,