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>
27 #include <linux/dma-mapping.h>
28 #include <drm/drm_drv.h>
31 #include "amdgpu_psp.h"
32 #include "amdgpu_ucode.h"
33 #include "soc15_common.h"
35 #include "psp_v10_0.h"
36 #include "psp_v11_0.h"
37 #include "psp_v12_0.h"
38 #include "psp_v13_0.h"
40 #include "amdgpu_ras.h"
41 #include "amdgpu_securedisplay.h"
42 #include "amdgpu_atomfirmware.h"
44 #include <drm/drm_drv.h>
46 static int psp_sysfs_init(struct amdgpu_device *adev);
47 static void psp_sysfs_fini(struct amdgpu_device *adev);
49 static int psp_load_smu_fw(struct psp_context *psp);
52 * Due to DF Cstate management centralized to PMFW, the firmware
53 * loading sequence will be updated as below:
59 * - Load other non-psp fw
61 * - Load XGMI/RAS/HDCP/DTM TA if any
63 * This new sequence is required for
64 * - Arcturus and onwards
65 * - Navi12 and onwards
67 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
69 struct amdgpu_device *adev = psp->adev;
71 psp->pmfw_centralized_cstate_management = false;
73 if (amdgpu_sriov_vf(adev))
76 if (adev->flags & AMD_IS_APU)
79 if ((adev->asic_type >= CHIP_ARCTURUS) ||
80 (adev->asic_type >= CHIP_NAVI12))
81 psp->pmfw_centralized_cstate_management = true;
84 static int psp_early_init(void *handle)
86 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
87 struct psp_context *psp = &adev->psp;
89 switch (adev->asic_type) {
92 psp_v3_1_set_psp_funcs(psp);
93 psp->autoload_supported = false;
96 psp_v10_0_set_psp_funcs(psp);
97 psp->autoload_supported = false;
101 psp_v11_0_set_psp_funcs(psp);
102 psp->autoload_supported = false;
107 case CHIP_SIENNA_CICHLID:
108 case CHIP_NAVY_FLOUNDER:
110 case CHIP_DIMGREY_CAVEFISH:
111 case CHIP_BEIGE_GOBY:
112 psp_v11_0_set_psp_funcs(psp);
113 psp->autoload_supported = true;
116 psp_v12_0_set_psp_funcs(psp);
119 psp_v13_0_set_psp_funcs(psp);
121 case CHIP_YELLOW_CARP:
122 psp_v13_0_set_psp_funcs(psp);
123 psp->autoload_supported = true;
131 psp_check_pmfw_centralized_cstate_management(psp);
136 static void psp_memory_training_fini(struct psp_context *psp)
138 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
140 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
141 kfree(ctx->sys_cache);
142 ctx->sys_cache = NULL;
145 static int psp_memory_training_init(struct psp_context *psp)
148 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
150 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
151 DRM_DEBUG("memory training is not supported!\n");
155 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
156 if (ctx->sys_cache == NULL) {
157 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
162 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
163 ctx->train_data_size,
164 ctx->p2c_train_data_offset,
165 ctx->c2p_train_data_offset);
166 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
170 psp_memory_training_fini(psp);
174 static int psp_sw_init(void *handle)
176 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
177 struct psp_context *psp = &adev->psp;
180 if (!amdgpu_sriov_vf(adev)) {
181 ret = psp_init_microcode(psp);
183 DRM_ERROR("Failed to load psp firmware!\n");
186 } else if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_ALDEBARAN) {
187 ret = psp_init_ta_microcode(psp, "aldebaran");
189 DRM_ERROR("Failed to initialize ta microcode!\n");
194 ret = psp_memory_training_init(psp);
196 DRM_ERROR("Failed to initialize memory training!\n");
199 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
201 DRM_ERROR("Failed to process memory training!\n");
205 if (adev->asic_type == CHIP_NAVI10 || adev->asic_type == CHIP_SIENNA_CICHLID) {
206 ret= psp_sysfs_init(adev);
215 static int psp_sw_fini(void *handle)
217 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
219 psp_memory_training_fini(&adev->psp);
220 if (adev->psp.sos_fw) {
221 release_firmware(adev->psp.sos_fw);
222 adev->psp.sos_fw = NULL;
224 if (adev->psp.asd_fw) {
225 release_firmware(adev->psp.asd_fw);
226 adev->psp.asd_fw = NULL;
228 if (adev->psp.ta_fw) {
229 release_firmware(adev->psp.ta_fw);
230 adev->psp.ta_fw = NULL;
233 if (adev->asic_type == CHIP_NAVI10 ||
234 adev->asic_type == CHIP_SIENNA_CICHLID)
235 psp_sysfs_fini(adev);
240 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
241 uint32_t reg_val, uint32_t mask, bool check_changed)
245 struct amdgpu_device *adev = psp->adev;
247 if (psp->adev->no_hw_access)
250 for (i = 0; i < adev->usec_timeout; i++) {
251 val = RREG32(reg_index);
256 if ((val & mask) == reg_val)
266 psp_cmd_submit_buf(struct psp_context *psp,
267 struct amdgpu_firmware_info *ucode,
268 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
273 bool ras_intr = false;
274 bool skip_unsupport = false;
276 if (psp->adev->no_hw_access)
279 if (!drm_dev_enter(&psp->adev->ddev, &idx))
282 mutex_lock(&psp->mutex);
284 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
286 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
288 index = atomic_inc_return(&psp->fence_value);
289 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
291 atomic_dec(&psp->fence_value);
295 amdgpu_device_invalidate_hdp(psp->adev, NULL);
296 while (*((unsigned int *)psp->fence_buf) != index) {
300 * Shouldn't wait for timeout when err_event_athub occurs,
301 * because gpu reset thread triggered and lock resource should
302 * be released for psp resume sequence.
304 ras_intr = amdgpu_ras_intr_triggered();
307 usleep_range(10, 100);
308 amdgpu_device_invalidate_hdp(psp->adev, NULL);
311 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
312 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
313 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
315 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
317 /* In some cases, psp response status is not 0 even there is no
318 * problem while the command is submitted. Some version of PSP FW
319 * doesn't write 0 to that field.
320 * So here we would like to only print a warning instead of an error
321 * during psp initialization to avoid breaking hw_init and it doesn't
324 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
326 DRM_WARN("failed to load ucode id (%d) ",
328 DRM_WARN("psp command (0x%X) failed and response status is (0x%X)\n",
329 psp->cmd_buf_mem->cmd_id,
330 psp->cmd_buf_mem->resp.status);
338 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
339 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
343 mutex_unlock(&psp->mutex);
348 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
349 struct psp_gfx_cmd_resp *cmd,
350 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
352 struct amdgpu_device *adev = psp->adev;
353 uint32_t size = amdgpu_bo_size(tmr_bo);
354 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
356 if (amdgpu_sriov_vf(psp->adev))
357 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
359 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
360 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
361 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
362 cmd->cmd.cmd_setup_tmr.buf_size = size;
363 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
364 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
365 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
368 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
369 uint64_t pri_buf_mc, uint32_t size)
371 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
372 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
373 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
374 cmd->cmd.cmd_load_toc.toc_size = size;
377 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
378 static int psp_load_toc(struct psp_context *psp,
382 struct psp_gfx_cmd_resp *cmd;
384 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
387 /* Copy toc to psp firmware private buffer */
388 psp_copy_fw(psp, psp->toc_start_addr, psp->toc_bin_size);
390 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size);
392 ret = psp_cmd_submit_buf(psp, NULL, cmd,
393 psp->fence_buf_mc_addr);
395 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
400 /* Set up Trusted Memory Region */
401 static int psp_tmr_init(struct psp_context *psp)
409 * According to HW engineer, they prefer the TMR address be "naturally
410 * aligned" , e.g. the start address be an integer divide of TMR size.
412 * Note: this memory need be reserved till the driver
415 tmr_size = PSP_TMR_SIZE(psp->adev);
417 /* For ASICs support RLC autoload, psp will parse the toc
418 * and calculate the total size of TMR needed */
419 if (!amdgpu_sriov_vf(psp->adev) &&
420 psp->toc_start_addr &&
423 ret = psp_load_toc(psp, &tmr_size);
425 DRM_ERROR("Failed to load toc\n");
430 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
431 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_SIZE(psp->adev),
432 AMDGPU_GEM_DOMAIN_VRAM,
433 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
438 static bool psp_skip_tmr(struct psp_context *psp)
440 switch (psp->adev->asic_type) {
442 case CHIP_SIENNA_CICHLID:
450 static int psp_tmr_load(struct psp_context *psp)
453 struct psp_gfx_cmd_resp *cmd;
455 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
456 * Already set up by host driver.
458 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
461 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
465 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
466 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
467 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
469 ret = psp_cmd_submit_buf(psp, NULL, cmd,
470 psp->fence_buf_mc_addr);
477 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
478 struct psp_gfx_cmd_resp *cmd)
480 if (amdgpu_sriov_vf(psp->adev))
481 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
483 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
486 static int psp_tmr_unload(struct psp_context *psp)
489 struct psp_gfx_cmd_resp *cmd;
491 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
495 psp_prep_tmr_unload_cmd_buf(psp, cmd);
496 DRM_INFO("free PSP TMR buffer\n");
498 ret = psp_cmd_submit_buf(psp, NULL, cmd,
499 psp->fence_buf_mc_addr);
506 static int psp_tmr_terminate(struct psp_context *psp)
512 ret = psp_tmr_unload(psp);
516 /* free TMR memory buffer */
517 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
518 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
523 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
524 uint64_t *output_ptr)
527 struct psp_gfx_cmd_resp *cmd;
532 if (amdgpu_sriov_vf(psp->adev))
535 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
539 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
541 ret = psp_cmd_submit_buf(psp, NULL, cmd,
542 psp->fence_buf_mc_addr);
545 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
546 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
554 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
556 struct psp_context *psp = &adev->psp;
557 struct psp_gfx_cmd_resp *cmd = psp->cmd;
560 if (amdgpu_sriov_vf(adev))
563 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
565 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
566 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
568 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
571 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
577 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
579 struct psp_context *psp = &adev->psp;
580 struct psp_gfx_cmd_resp *cmd = psp->cmd;
582 if (amdgpu_sriov_vf(adev))
585 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
587 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
588 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
589 cmd->cmd.boot_cfg.boot_config = boot_cfg;
590 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
592 return psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
595 static int psp_rl_load(struct amdgpu_device *adev)
597 struct psp_context *psp = &adev->psp;
598 struct psp_gfx_cmd_resp *cmd = psp->cmd;
600 if (psp->rl_bin_size == 0)
603 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
604 memcpy(psp->fw_pri_buf, psp->rl_start_addr, psp->rl_bin_size);
606 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
608 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
609 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
610 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
611 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl_bin_size;
612 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
614 return psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
617 static void psp_prep_asd_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
618 uint64_t asd_mc, uint32_t size)
620 cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
621 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
622 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
623 cmd->cmd.cmd_load_ta.app_len = size;
625 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = 0;
626 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = 0;
627 cmd->cmd.cmd_load_ta.cmd_buf_len = 0;
630 static int psp_asd_load(struct psp_context *psp)
633 struct psp_gfx_cmd_resp *cmd;
635 /* If PSP version doesn't match ASD version, asd loading will be failed.
636 * add workaround to bypass it for sriov now.
637 * TODO: add version check to make it common
639 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_ucode_size)
642 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
646 psp_copy_fw(psp, psp->asd_start_addr, psp->asd_ucode_size);
648 psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr,
649 psp->asd_ucode_size);
651 ret = psp_cmd_submit_buf(psp, NULL, cmd,
652 psp->fence_buf_mc_addr);
654 psp->asd_context.asd_initialized = true;
655 psp->asd_context.session_id = cmd->resp.session_id;
663 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
666 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
667 cmd->cmd.cmd_unload_ta.session_id = session_id;
670 static int psp_asd_unload(struct psp_context *psp)
673 struct psp_gfx_cmd_resp *cmd;
675 if (amdgpu_sriov_vf(psp->adev))
678 if (!psp->asd_context.asd_initialized)
681 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
685 psp_prep_ta_unload_cmd_buf(cmd, psp->asd_context.session_id);
687 ret = psp_cmd_submit_buf(psp, NULL, cmd,
688 psp->fence_buf_mc_addr);
690 psp->asd_context.asd_initialized = false;
697 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
698 uint32_t id, uint32_t value)
700 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
701 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
702 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
705 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
708 struct psp_gfx_cmd_resp *cmd = NULL;
711 if (reg >= PSP_REG_LAST)
714 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
718 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
719 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
721 DRM_ERROR("PSP failed to program reg id %d", reg);
727 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
729 uint32_t ta_bin_size,
730 uint64_t ta_shared_mc,
731 uint32_t ta_shared_size)
733 cmd->cmd_id = GFX_CMD_ID_LOAD_TA;
734 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
735 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
736 cmd->cmd.cmd_load_ta.app_len = ta_bin_size;
738 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(ta_shared_mc);
739 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(ta_shared_mc);
740 cmd->cmd.cmd_load_ta.cmd_buf_len = ta_shared_size;
743 static int psp_xgmi_init_shared_buf(struct psp_context *psp)
748 * Allocate 16k memory aligned to 4k from Frame Buffer (local
749 * physical) for xgmi ta <-> Driver
751 ret = amdgpu_bo_create_kernel(psp->adev, PSP_XGMI_SHARED_MEM_SIZE,
752 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
753 &psp->xgmi_context.xgmi_shared_bo,
754 &psp->xgmi_context.xgmi_shared_mc_addr,
755 &psp->xgmi_context.xgmi_shared_buf);
760 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
764 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
765 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
766 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
769 static int psp_ta_invoke(struct psp_context *psp,
774 struct psp_gfx_cmd_resp *cmd;
776 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
780 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, session_id);
782 ret = psp_cmd_submit_buf(psp, NULL, cmd,
783 psp->fence_buf_mc_addr);
790 static int psp_xgmi_load(struct psp_context *psp)
793 struct psp_gfx_cmd_resp *cmd;
796 * TODO: bypass the loading in sriov for now
799 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
803 psp_copy_fw(psp, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
805 psp_prep_ta_load_cmd_buf(cmd,
807 psp->ta_xgmi_ucode_size,
808 psp->xgmi_context.xgmi_shared_mc_addr,
809 PSP_XGMI_SHARED_MEM_SIZE);
811 ret = psp_cmd_submit_buf(psp, NULL, cmd,
812 psp->fence_buf_mc_addr);
815 psp->xgmi_context.initialized = 1;
816 psp->xgmi_context.session_id = cmd->resp.session_id;
824 static int psp_xgmi_unload(struct psp_context *psp)
827 struct psp_gfx_cmd_resp *cmd;
828 struct amdgpu_device *adev = psp->adev;
830 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
831 if (adev->asic_type == CHIP_ARCTURUS ||
832 (adev->asic_type == CHIP_ALDEBARAN && adev->gmc.xgmi.connected_to_cpu))
836 * TODO: bypass the unloading in sriov for now
839 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
843 psp_prep_ta_unload_cmd_buf(cmd, psp->xgmi_context.session_id);
845 ret = psp_cmd_submit_buf(psp, NULL, cmd,
846 psp->fence_buf_mc_addr);
853 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
855 return psp_ta_invoke(psp, ta_cmd_id, psp->xgmi_context.session_id);
858 int psp_xgmi_terminate(struct psp_context *psp)
862 if (!psp->xgmi_context.initialized)
865 ret = psp_xgmi_unload(psp);
869 psp->xgmi_context.initialized = 0;
871 /* free xgmi shared memory */
872 amdgpu_bo_free_kernel(&psp->xgmi_context.xgmi_shared_bo,
873 &psp->xgmi_context.xgmi_shared_mc_addr,
874 &psp->xgmi_context.xgmi_shared_buf);
879 int psp_xgmi_initialize(struct psp_context *psp)
881 struct ta_xgmi_shared_memory *xgmi_cmd;
884 if (!psp->adev->psp.ta_fw ||
885 !psp->adev->psp.ta_xgmi_ucode_size ||
886 !psp->adev->psp.ta_xgmi_start_addr)
889 if (!psp->xgmi_context.initialized) {
890 ret = psp_xgmi_init_shared_buf(psp);
896 ret = psp_xgmi_load(psp);
900 /* Initialize XGMI session */
901 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.xgmi_shared_buf);
902 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
903 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
905 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
910 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
912 struct ta_xgmi_shared_memory *xgmi_cmd;
915 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.xgmi_shared_buf;
916 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
918 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
920 /* Invoke xgmi ta to get hive id */
921 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
925 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
930 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
932 struct ta_xgmi_shared_memory *xgmi_cmd;
935 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.xgmi_shared_buf;
936 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
938 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
940 /* Invoke xgmi ta to get the node id */
941 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
945 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
950 int psp_xgmi_get_topology_info(struct psp_context *psp,
952 struct psp_xgmi_topology_info *topology)
954 struct ta_xgmi_shared_memory *xgmi_cmd;
955 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
956 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
960 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
963 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.xgmi_shared_buf;
964 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
966 /* Fill in the shared memory with topology information as input */
967 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
968 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
969 topology_info_input->num_nodes = number_devices;
971 for (i = 0; i < topology_info_input->num_nodes; i++) {
972 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
973 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
974 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
975 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
978 /* Invoke xgmi ta to get the topology information */
979 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
983 /* Read the output topology information from the shared memory */
984 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
985 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
986 for (i = 0; i < topology->num_nodes; i++) {
987 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
988 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
989 topology->nodes[i].is_sharing_enabled = topology_info_output->nodes[i].is_sharing_enabled;
990 topology->nodes[i].sdma_engine = topology_info_output->nodes[i].sdma_engine;
996 int psp_xgmi_set_topology_info(struct psp_context *psp,
998 struct psp_xgmi_topology_info *topology)
1000 struct ta_xgmi_shared_memory *xgmi_cmd;
1001 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1004 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1007 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.xgmi_shared_buf;
1008 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1010 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1011 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1012 topology_info_input->num_nodes = number_devices;
1014 for (i = 0; i < topology_info_input->num_nodes; i++) {
1015 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1016 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1017 topology_info_input->nodes[i].is_sharing_enabled = 1;
1018 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1021 /* Invoke xgmi ta to set topology information */
1022 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1026 static int psp_ras_init_shared_buf(struct psp_context *psp)
1031 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1032 * physical) for ras ta <-> Driver
1034 ret = amdgpu_bo_create_kernel(psp->adev, PSP_RAS_SHARED_MEM_SIZE,
1035 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1036 &psp->ras.ras_shared_bo,
1037 &psp->ras.ras_shared_mc_addr,
1038 &psp->ras.ras_shared_buf);
1043 static int psp_ras_load(struct psp_context *psp)
1046 struct psp_gfx_cmd_resp *cmd;
1047 struct ta_ras_shared_memory *ras_cmd;
1050 * TODO: bypass the loading in sriov for now
1052 if (amdgpu_sriov_vf(psp->adev))
1055 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1059 psp_copy_fw(psp, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
1061 ras_cmd = (struct ta_ras_shared_memory *)psp->ras.ras_shared_buf;
1063 if (psp->adev->gmc.xgmi.connected_to_cpu)
1064 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1066 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1068 psp_prep_ta_load_cmd_buf(cmd,
1069 psp->fw_pri_mc_addr,
1070 psp->ta_ras_ucode_size,
1071 psp->ras.ras_shared_mc_addr,
1072 PSP_RAS_SHARED_MEM_SIZE);
1074 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1075 psp->fence_buf_mc_addr);
1078 psp->ras.session_id = cmd->resp.session_id;
1080 if (!ras_cmd->ras_status)
1081 psp->ras.ras_initialized = true;
1083 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1086 if (ret || ras_cmd->ras_status)
1087 amdgpu_ras_fini(psp->adev);
1094 static int psp_ras_unload(struct psp_context *psp)
1097 struct psp_gfx_cmd_resp *cmd;
1100 * TODO: bypass the unloading in sriov for now
1102 if (amdgpu_sriov_vf(psp->adev))
1105 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1109 psp_prep_ta_unload_cmd_buf(cmd, psp->ras.session_id);
1111 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1112 psp->fence_buf_mc_addr);
1119 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1121 struct ta_ras_shared_memory *ras_cmd;
1124 ras_cmd = (struct ta_ras_shared_memory *)psp->ras.ras_shared_buf;
1127 * TODO: bypass the loading in sriov for now
1129 if (amdgpu_sriov_vf(psp->adev))
1132 ret = psp_ta_invoke(psp, ta_cmd_id, psp->ras.session_id);
1134 if (amdgpu_ras_intr_triggered())
1137 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1139 DRM_WARN("RAS: Unsupported Interface");
1144 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1145 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1147 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1149 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1150 dev_warn(psp->adev->dev,
1151 "RAS internal register access blocked\n");
1157 static int psp_ras_status_to_errno(struct amdgpu_device *adev,
1158 enum ta_ras_status ras_status)
1162 switch (ras_status) {
1163 case TA_RAS_STATUS__SUCCESS:
1166 case TA_RAS_STATUS__RESET_NEEDED:
1169 case TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE:
1170 dev_warn(adev->dev, "RAS WARN: ras function unavailable\n");
1172 case TA_RAS_STATUS__ERROR_ASD_READ_WRITE:
1173 dev_warn(adev->dev, "RAS WARN: asd read or write failed\n");
1176 dev_err(adev->dev, "RAS ERROR: ras function failed ret 0x%X\n", ret);
1182 int psp_ras_enable_features(struct psp_context *psp,
1183 union ta_ras_cmd_input *info, bool enable)
1185 struct ta_ras_shared_memory *ras_cmd;
1188 if (!psp->ras.ras_initialized)
1191 ras_cmd = (struct ta_ras_shared_memory *)psp->ras.ras_shared_buf;
1192 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1195 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1197 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1199 ras_cmd->ras_in_message = *info;
1201 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1205 return psp_ras_status_to_errno(psp->adev, ras_cmd->ras_status);
1208 static int psp_ras_terminate(struct psp_context *psp)
1213 * TODO: bypass the terminate in sriov for now
1215 if (amdgpu_sriov_vf(psp->adev))
1218 if (!psp->ras.ras_initialized)
1221 ret = psp_ras_unload(psp);
1225 psp->ras.ras_initialized = false;
1227 /* free ras shared memory */
1228 amdgpu_bo_free_kernel(&psp->ras.ras_shared_bo,
1229 &psp->ras.ras_shared_mc_addr,
1230 &psp->ras.ras_shared_buf);
1235 static int psp_ras_initialize(struct psp_context *psp)
1238 uint32_t boot_cfg = 0xFF;
1239 struct amdgpu_device *adev = psp->adev;
1242 * TODO: bypass the initialize in sriov for now
1244 if (amdgpu_sriov_vf(adev))
1247 if (!adev->psp.ta_ras_ucode_size ||
1248 !adev->psp.ta_ras_start_addr) {
1249 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1253 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1254 /* query GECC enablement status from boot config
1255 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1257 ret = psp_boot_config_get(adev, &boot_cfg);
1259 dev_warn(adev->dev, "PSP get boot config failed\n");
1261 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1263 dev_info(adev->dev, "GECC is disabled\n");
1265 /* disable GECC in next boot cycle if ras is
1266 * disabled by module parameter amdgpu_ras_enable
1267 * and/or amdgpu_ras_mask, or boot_config_get call
1270 ret = psp_boot_config_set(adev, 0);
1272 dev_warn(adev->dev, "PSP set boot config failed\n");
1274 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1275 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1278 if (1 == boot_cfg) {
1279 dev_info(adev->dev, "GECC is enabled\n");
1281 /* enable GECC in next boot cycle if it is disabled
1282 * in boot config, or force enable GECC if failed to
1283 * get boot configuration
1285 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1287 dev_warn(adev->dev, "PSP set boot config failed\n");
1289 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1294 if (!psp->ras.ras_initialized) {
1295 ret = psp_ras_init_shared_buf(psp);
1300 ret = psp_ras_load(psp);
1307 int psp_ras_trigger_error(struct psp_context *psp,
1308 struct ta_ras_trigger_error_input *info)
1310 struct ta_ras_shared_memory *ras_cmd;
1313 if (!psp->ras.ras_initialized)
1316 ras_cmd = (struct ta_ras_shared_memory *)psp->ras.ras_shared_buf;
1317 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1319 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1320 ras_cmd->ras_in_message.trigger_error = *info;
1322 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1326 /* If err_event_athub occurs error inject was successful, however
1327 return status from TA is no long reliable */
1328 if (amdgpu_ras_intr_triggered())
1331 return psp_ras_status_to_errno(psp->adev, ras_cmd->ras_status);
1336 static int psp_hdcp_init_shared_buf(struct psp_context *psp)
1341 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1342 * physical) for hdcp ta <-> Driver
1344 ret = amdgpu_bo_create_kernel(psp->adev, PSP_HDCP_SHARED_MEM_SIZE,
1345 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1346 &psp->hdcp_context.hdcp_shared_bo,
1347 &psp->hdcp_context.hdcp_shared_mc_addr,
1348 &psp->hdcp_context.hdcp_shared_buf);
1353 static int psp_hdcp_load(struct psp_context *psp)
1356 struct psp_gfx_cmd_resp *cmd;
1359 * TODO: bypass the loading in sriov for now
1361 if (amdgpu_sriov_vf(psp->adev))
1364 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1368 psp_copy_fw(psp, psp->ta_hdcp_start_addr,
1369 psp->ta_hdcp_ucode_size);
1371 psp_prep_ta_load_cmd_buf(cmd,
1372 psp->fw_pri_mc_addr,
1373 psp->ta_hdcp_ucode_size,
1374 psp->hdcp_context.hdcp_shared_mc_addr,
1375 PSP_HDCP_SHARED_MEM_SIZE);
1377 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1380 psp->hdcp_context.hdcp_initialized = true;
1381 psp->hdcp_context.session_id = cmd->resp.session_id;
1382 mutex_init(&psp->hdcp_context.mutex);
1389 static int psp_hdcp_initialize(struct psp_context *psp)
1394 * TODO: bypass the initialize in sriov for now
1396 if (amdgpu_sriov_vf(psp->adev))
1399 if (!psp->adev->psp.ta_hdcp_ucode_size ||
1400 !psp->adev->psp.ta_hdcp_start_addr) {
1401 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1405 if (!psp->hdcp_context.hdcp_initialized) {
1406 ret = psp_hdcp_init_shared_buf(psp);
1411 ret = psp_hdcp_load(psp);
1418 static int psp_hdcp_unload(struct psp_context *psp)
1421 struct psp_gfx_cmd_resp *cmd;
1424 * TODO: bypass the unloading in sriov for now
1426 if (amdgpu_sriov_vf(psp->adev))
1429 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1433 psp_prep_ta_unload_cmd_buf(cmd, psp->hdcp_context.session_id);
1435 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1442 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1445 * TODO: bypass the loading in sriov for now
1447 if (amdgpu_sriov_vf(psp->adev))
1450 return psp_ta_invoke(psp, ta_cmd_id, psp->hdcp_context.session_id);
1453 static int psp_hdcp_terminate(struct psp_context *psp)
1458 * TODO: bypass the terminate in sriov for now
1460 if (amdgpu_sriov_vf(psp->adev))
1463 if (!psp->hdcp_context.hdcp_initialized) {
1464 if (psp->hdcp_context.hdcp_shared_buf)
1470 ret = psp_hdcp_unload(psp);
1474 psp->hdcp_context.hdcp_initialized = false;
1477 /* free hdcp shared memory */
1478 amdgpu_bo_free_kernel(&psp->hdcp_context.hdcp_shared_bo,
1479 &psp->hdcp_context.hdcp_shared_mc_addr,
1480 &psp->hdcp_context.hdcp_shared_buf);
1487 static int psp_dtm_init_shared_buf(struct psp_context *psp)
1492 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1493 * physical) for dtm ta <-> Driver
1495 ret = amdgpu_bo_create_kernel(psp->adev, PSP_DTM_SHARED_MEM_SIZE,
1496 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1497 &psp->dtm_context.dtm_shared_bo,
1498 &psp->dtm_context.dtm_shared_mc_addr,
1499 &psp->dtm_context.dtm_shared_buf);
1504 static int psp_dtm_load(struct psp_context *psp)
1507 struct psp_gfx_cmd_resp *cmd;
1510 * TODO: bypass the loading in sriov for now
1512 if (amdgpu_sriov_vf(psp->adev))
1515 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1519 psp_copy_fw(psp, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
1521 psp_prep_ta_load_cmd_buf(cmd,
1522 psp->fw_pri_mc_addr,
1523 psp->ta_dtm_ucode_size,
1524 psp->dtm_context.dtm_shared_mc_addr,
1525 PSP_DTM_SHARED_MEM_SIZE);
1527 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1530 psp->dtm_context.dtm_initialized = true;
1531 psp->dtm_context.session_id = cmd->resp.session_id;
1532 mutex_init(&psp->dtm_context.mutex);
1540 static int psp_dtm_initialize(struct psp_context *psp)
1545 * TODO: bypass the initialize in sriov for now
1547 if (amdgpu_sriov_vf(psp->adev))
1550 if (!psp->adev->psp.ta_dtm_ucode_size ||
1551 !psp->adev->psp.ta_dtm_start_addr) {
1552 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1556 if (!psp->dtm_context.dtm_initialized) {
1557 ret = psp_dtm_init_shared_buf(psp);
1562 ret = psp_dtm_load(psp);
1569 static int psp_dtm_unload(struct psp_context *psp)
1572 struct psp_gfx_cmd_resp *cmd;
1575 * TODO: bypass the unloading in sriov for now
1577 if (amdgpu_sriov_vf(psp->adev))
1580 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1584 psp_prep_ta_unload_cmd_buf(cmd, psp->dtm_context.session_id);
1586 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1593 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1596 * TODO: bypass the loading in sriov for now
1598 if (amdgpu_sriov_vf(psp->adev))
1601 return psp_ta_invoke(psp, ta_cmd_id, psp->dtm_context.session_id);
1604 static int psp_dtm_terminate(struct psp_context *psp)
1609 * TODO: bypass the terminate in sriov for now
1611 if (amdgpu_sriov_vf(psp->adev))
1614 if (!psp->dtm_context.dtm_initialized) {
1615 if (psp->dtm_context.dtm_shared_buf)
1621 ret = psp_dtm_unload(psp);
1625 psp->dtm_context.dtm_initialized = false;
1628 /* free hdcp shared memory */
1629 amdgpu_bo_free_kernel(&psp->dtm_context.dtm_shared_bo,
1630 &psp->dtm_context.dtm_shared_mc_addr,
1631 &psp->dtm_context.dtm_shared_buf);
1638 static int psp_rap_init_shared_buf(struct psp_context *psp)
1643 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1644 * physical) for rap ta <-> Driver
1646 ret = amdgpu_bo_create_kernel(psp->adev, PSP_RAP_SHARED_MEM_SIZE,
1647 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1648 &psp->rap_context.rap_shared_bo,
1649 &psp->rap_context.rap_shared_mc_addr,
1650 &psp->rap_context.rap_shared_buf);
1655 static int psp_rap_load(struct psp_context *psp)
1658 struct psp_gfx_cmd_resp *cmd;
1660 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1664 psp_copy_fw(psp, psp->ta_rap_start_addr, psp->ta_rap_ucode_size);
1666 psp_prep_ta_load_cmd_buf(cmd,
1667 psp->fw_pri_mc_addr,
1668 psp->ta_rap_ucode_size,
1669 psp->rap_context.rap_shared_mc_addr,
1670 PSP_RAP_SHARED_MEM_SIZE);
1672 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1675 psp->rap_context.rap_initialized = true;
1676 psp->rap_context.session_id = cmd->resp.session_id;
1677 mutex_init(&psp->rap_context.mutex);
1685 static int psp_rap_unload(struct psp_context *psp)
1688 struct psp_gfx_cmd_resp *cmd;
1690 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1694 psp_prep_ta_unload_cmd_buf(cmd, psp->rap_context.session_id);
1696 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1703 static int psp_rap_initialize(struct psp_context *psp)
1706 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1709 * TODO: bypass the initialize in sriov for now
1711 if (amdgpu_sriov_vf(psp->adev))
1714 if (!psp->adev->psp.ta_rap_ucode_size ||
1715 !psp->adev->psp.ta_rap_start_addr) {
1716 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1720 if (!psp->rap_context.rap_initialized) {
1721 ret = psp_rap_init_shared_buf(psp);
1726 ret = psp_rap_load(psp);
1730 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1731 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1732 psp_rap_unload(psp);
1734 amdgpu_bo_free_kernel(&psp->rap_context.rap_shared_bo,
1735 &psp->rap_context.rap_shared_mc_addr,
1736 &psp->rap_context.rap_shared_buf);
1738 psp->rap_context.rap_initialized = false;
1740 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1749 static int psp_rap_terminate(struct psp_context *psp)
1753 if (!psp->rap_context.rap_initialized)
1756 ret = psp_rap_unload(psp);
1758 psp->rap_context.rap_initialized = false;
1760 /* free rap shared memory */
1761 amdgpu_bo_free_kernel(&psp->rap_context.rap_shared_bo,
1762 &psp->rap_context.rap_shared_mc_addr,
1763 &psp->rap_context.rap_shared_buf);
1768 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1770 struct ta_rap_shared_memory *rap_cmd;
1773 if (!psp->rap_context.rap_initialized)
1776 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1777 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1780 mutex_lock(&psp->rap_context.mutex);
1782 rap_cmd = (struct ta_rap_shared_memory *)
1783 psp->rap_context.rap_shared_buf;
1784 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1786 rap_cmd->cmd_id = ta_cmd_id;
1787 rap_cmd->validation_method_id = METHOD_A;
1789 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, psp->rap_context.session_id);
1794 *status = rap_cmd->rap_status;
1797 mutex_unlock(&psp->rap_context.mutex);
1803 /* securedisplay start */
1804 static int psp_securedisplay_init_shared_buf(struct psp_context *psp)
1809 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1810 * physical) for sa ta <-> Driver
1812 ret = amdgpu_bo_create_kernel(psp->adev, PSP_SECUREDISPLAY_SHARED_MEM_SIZE,
1813 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1814 &psp->securedisplay_context.securedisplay_shared_bo,
1815 &psp->securedisplay_context.securedisplay_shared_mc_addr,
1816 &psp->securedisplay_context.securedisplay_shared_buf);
1821 static int psp_securedisplay_load(struct psp_context *psp)
1824 struct psp_gfx_cmd_resp *cmd;
1826 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1830 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
1831 memcpy(psp->fw_pri_buf, psp->ta_securedisplay_start_addr, psp->ta_securedisplay_ucode_size);
1833 psp_prep_ta_load_cmd_buf(cmd,
1834 psp->fw_pri_mc_addr,
1835 psp->ta_securedisplay_ucode_size,
1836 psp->securedisplay_context.securedisplay_shared_mc_addr,
1837 PSP_SECUREDISPLAY_SHARED_MEM_SIZE);
1839 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1844 psp->securedisplay_context.securedisplay_initialized = true;
1845 psp->securedisplay_context.session_id = cmd->resp.session_id;
1846 mutex_init(&psp->securedisplay_context.mutex);
1853 static int psp_securedisplay_unload(struct psp_context *psp)
1856 struct psp_gfx_cmd_resp *cmd;
1858 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1862 psp_prep_ta_unload_cmd_buf(cmd, psp->securedisplay_context.session_id);
1864 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1871 static int psp_securedisplay_initialize(struct psp_context *psp)
1874 struct securedisplay_cmd *securedisplay_cmd;
1877 * TODO: bypass the initialize in sriov for now
1879 if (amdgpu_sriov_vf(psp->adev))
1882 if (!psp->adev->psp.ta_securedisplay_ucode_size ||
1883 !psp->adev->psp.ta_securedisplay_start_addr) {
1884 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1888 if (!psp->securedisplay_context.securedisplay_initialized) {
1889 ret = psp_securedisplay_init_shared_buf(psp);
1894 ret = psp_securedisplay_load(psp);
1898 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1899 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1901 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1903 psp_securedisplay_unload(psp);
1905 amdgpu_bo_free_kernel(&psp->securedisplay_context.securedisplay_shared_bo,
1906 &psp->securedisplay_context.securedisplay_shared_mc_addr,
1907 &psp->securedisplay_context.securedisplay_shared_buf);
1909 psp->securedisplay_context.securedisplay_initialized = false;
1911 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1915 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1916 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1917 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1918 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1924 static int psp_securedisplay_terminate(struct psp_context *psp)
1929 * TODO:bypass the terminate in sriov for now
1931 if (amdgpu_sriov_vf(psp->adev))
1934 if (!psp->securedisplay_context.securedisplay_initialized)
1937 ret = psp_securedisplay_unload(psp);
1941 psp->securedisplay_context.securedisplay_initialized = false;
1943 /* free securedisplay shared memory */
1944 amdgpu_bo_free_kernel(&psp->securedisplay_context.securedisplay_shared_bo,
1945 &psp->securedisplay_context.securedisplay_shared_mc_addr,
1946 &psp->securedisplay_context.securedisplay_shared_buf);
1951 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1955 if (!psp->securedisplay_context.securedisplay_initialized)
1958 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1959 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1962 mutex_lock(&psp->securedisplay_context.mutex);
1964 ret = psp_ta_invoke(psp, ta_cmd_id, psp->securedisplay_context.session_id);
1966 mutex_unlock(&psp->securedisplay_context.mutex);
1970 /* SECUREDISPLAY end */
1972 static int psp_hw_start(struct psp_context *psp)
1974 struct amdgpu_device *adev = psp->adev;
1977 if (!amdgpu_sriov_vf(adev)) {
1978 if (psp->kdb_bin_size &&
1979 (psp->funcs->bootloader_load_kdb != NULL)) {
1980 ret = psp_bootloader_load_kdb(psp);
1982 DRM_ERROR("PSP load kdb failed!\n");
1987 if (psp->spl_bin_size) {
1988 ret = psp_bootloader_load_spl(psp);
1990 DRM_ERROR("PSP load spl failed!\n");
1995 ret = psp_bootloader_load_sysdrv(psp);
1997 DRM_ERROR("PSP load sysdrv failed!\n");
2001 ret = psp_bootloader_load_sos(psp);
2003 DRM_ERROR("PSP load sos failed!\n");
2008 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2010 DRM_ERROR("PSP create ring failed!\n");
2014 ret = psp_tmr_init(psp);
2016 DRM_ERROR("PSP tmr init failed!\n");
2021 * For ASICs with DF Cstate management centralized
2022 * to PMFW, TMR setup should be performed after PMFW
2023 * loaded and before other non-psp firmware loaded.
2025 if (psp->pmfw_centralized_cstate_management) {
2026 ret = psp_load_smu_fw(psp);
2031 ret = psp_tmr_load(psp);
2033 DRM_ERROR("PSP load tmr failed!\n");
2040 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2041 enum psp_gfx_fw_type *type)
2043 switch (ucode->ucode_id) {
2044 case AMDGPU_UCODE_ID_SDMA0:
2045 *type = GFX_FW_TYPE_SDMA0;
2047 case AMDGPU_UCODE_ID_SDMA1:
2048 *type = GFX_FW_TYPE_SDMA1;
2050 case AMDGPU_UCODE_ID_SDMA2:
2051 *type = GFX_FW_TYPE_SDMA2;
2053 case AMDGPU_UCODE_ID_SDMA3:
2054 *type = GFX_FW_TYPE_SDMA3;
2056 case AMDGPU_UCODE_ID_SDMA4:
2057 *type = GFX_FW_TYPE_SDMA4;
2059 case AMDGPU_UCODE_ID_SDMA5:
2060 *type = GFX_FW_TYPE_SDMA5;
2062 case AMDGPU_UCODE_ID_SDMA6:
2063 *type = GFX_FW_TYPE_SDMA6;
2065 case AMDGPU_UCODE_ID_SDMA7:
2066 *type = GFX_FW_TYPE_SDMA7;
2068 case AMDGPU_UCODE_ID_CP_MES:
2069 *type = GFX_FW_TYPE_CP_MES;
2071 case AMDGPU_UCODE_ID_CP_MES_DATA:
2072 *type = GFX_FW_TYPE_MES_STACK;
2074 case AMDGPU_UCODE_ID_CP_CE:
2075 *type = GFX_FW_TYPE_CP_CE;
2077 case AMDGPU_UCODE_ID_CP_PFP:
2078 *type = GFX_FW_TYPE_CP_PFP;
2080 case AMDGPU_UCODE_ID_CP_ME:
2081 *type = GFX_FW_TYPE_CP_ME;
2083 case AMDGPU_UCODE_ID_CP_MEC1:
2084 *type = GFX_FW_TYPE_CP_MEC;
2086 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2087 *type = GFX_FW_TYPE_CP_MEC_ME1;
2089 case AMDGPU_UCODE_ID_CP_MEC2:
2090 *type = GFX_FW_TYPE_CP_MEC;
2092 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2093 *type = GFX_FW_TYPE_CP_MEC_ME2;
2095 case AMDGPU_UCODE_ID_RLC_G:
2096 *type = GFX_FW_TYPE_RLC_G;
2098 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2099 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2101 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2102 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2104 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2105 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2107 case AMDGPU_UCODE_ID_RLC_IRAM:
2108 *type = GFX_FW_TYPE_RLC_IRAM;
2110 case AMDGPU_UCODE_ID_RLC_DRAM:
2111 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2113 case AMDGPU_UCODE_ID_SMC:
2114 *type = GFX_FW_TYPE_SMU;
2116 case AMDGPU_UCODE_ID_UVD:
2117 *type = GFX_FW_TYPE_UVD;
2119 case AMDGPU_UCODE_ID_UVD1:
2120 *type = GFX_FW_TYPE_UVD1;
2122 case AMDGPU_UCODE_ID_VCE:
2123 *type = GFX_FW_TYPE_VCE;
2125 case AMDGPU_UCODE_ID_VCN:
2126 *type = GFX_FW_TYPE_VCN;
2128 case AMDGPU_UCODE_ID_VCN1:
2129 *type = GFX_FW_TYPE_VCN1;
2131 case AMDGPU_UCODE_ID_DMCU_ERAM:
2132 *type = GFX_FW_TYPE_DMCU_ERAM;
2134 case AMDGPU_UCODE_ID_DMCU_INTV:
2135 *type = GFX_FW_TYPE_DMCU_ISR;
2137 case AMDGPU_UCODE_ID_VCN0_RAM:
2138 *type = GFX_FW_TYPE_VCN0_RAM;
2140 case AMDGPU_UCODE_ID_VCN1_RAM:
2141 *type = GFX_FW_TYPE_VCN1_RAM;
2143 case AMDGPU_UCODE_ID_DMCUB:
2144 *type = GFX_FW_TYPE_DMUB;
2146 case AMDGPU_UCODE_ID_MAXIMUM:
2154 static void psp_print_fw_hdr(struct psp_context *psp,
2155 struct amdgpu_firmware_info *ucode)
2157 struct amdgpu_device *adev = psp->adev;
2158 struct common_firmware_header *hdr;
2160 switch (ucode->ucode_id) {
2161 case AMDGPU_UCODE_ID_SDMA0:
2162 case AMDGPU_UCODE_ID_SDMA1:
2163 case AMDGPU_UCODE_ID_SDMA2:
2164 case AMDGPU_UCODE_ID_SDMA3:
2165 case AMDGPU_UCODE_ID_SDMA4:
2166 case AMDGPU_UCODE_ID_SDMA5:
2167 case AMDGPU_UCODE_ID_SDMA6:
2168 case AMDGPU_UCODE_ID_SDMA7:
2169 hdr = (struct common_firmware_header *)
2170 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2171 amdgpu_ucode_print_sdma_hdr(hdr);
2173 case AMDGPU_UCODE_ID_CP_CE:
2174 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2175 amdgpu_ucode_print_gfx_hdr(hdr);
2177 case AMDGPU_UCODE_ID_CP_PFP:
2178 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2179 amdgpu_ucode_print_gfx_hdr(hdr);
2181 case AMDGPU_UCODE_ID_CP_ME:
2182 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2183 amdgpu_ucode_print_gfx_hdr(hdr);
2185 case AMDGPU_UCODE_ID_CP_MEC1:
2186 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2187 amdgpu_ucode_print_gfx_hdr(hdr);
2189 case AMDGPU_UCODE_ID_RLC_G:
2190 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2191 amdgpu_ucode_print_rlc_hdr(hdr);
2193 case AMDGPU_UCODE_ID_SMC:
2194 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2195 amdgpu_ucode_print_smc_hdr(hdr);
2202 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2203 struct psp_gfx_cmd_resp *cmd)
2206 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2208 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
2210 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2211 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2212 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2213 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2215 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2217 DRM_ERROR("Unknown firmware type\n");
2222 static int psp_execute_np_fw_load(struct psp_context *psp,
2223 struct amdgpu_firmware_info *ucode)
2227 ret = psp_prep_load_ip_fw_cmd_buf(ucode, psp->cmd);
2231 ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
2232 psp->fence_buf_mc_addr);
2237 static int psp_load_smu_fw(struct psp_context *psp)
2240 struct amdgpu_device *adev = psp->adev;
2241 struct amdgpu_firmware_info *ucode =
2242 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2243 struct amdgpu_ras *ras = psp->ras.ras;
2245 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2248 if ((amdgpu_in_reset(adev) &&
2249 ras && adev->ras_enabled &&
2250 (adev->asic_type == CHIP_ARCTURUS ||
2251 adev->asic_type == CHIP_VEGA20))) {
2252 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2254 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2258 ret = psp_execute_np_fw_load(psp, ucode);
2261 DRM_ERROR("PSP load smu failed!\n");
2266 static bool fw_load_skip_check(struct psp_context *psp,
2267 struct amdgpu_firmware_info *ucode)
2272 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2273 (psp_smu_reload_quirk(psp) ||
2274 psp->autoload_supported ||
2275 psp->pmfw_centralized_cstate_management))
2278 if (amdgpu_sriov_vf(psp->adev) &&
2279 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
2280 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
2281 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2
2282 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3
2283 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA4
2284 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA5
2285 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA6
2286 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA7
2287 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G
2288 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
2289 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
2290 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
2291 || ucode->ucode_id == AMDGPU_UCODE_ID_SMC))
2292 /*skip ucode loading in SRIOV VF */
2295 if (psp->autoload_supported &&
2296 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2297 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2298 /* skip mec JT when autoload is enabled */
2304 int psp_load_fw_list(struct psp_context *psp,
2305 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2308 struct amdgpu_firmware_info *ucode;
2310 for (i = 0; i < ucode_count; ++i) {
2311 ucode = ucode_list[i];
2312 psp_print_fw_hdr(psp, ucode);
2313 ret = psp_execute_np_fw_load(psp, ucode);
2320 static int psp_np_fw_load(struct psp_context *psp)
2323 struct amdgpu_firmware_info *ucode;
2324 struct amdgpu_device *adev = psp->adev;
2326 if (psp->autoload_supported &&
2327 !psp->pmfw_centralized_cstate_management) {
2328 ret = psp_load_smu_fw(psp);
2333 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2334 ucode = &adev->firmware.ucode[i];
2336 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2337 !fw_load_skip_check(psp, ucode)) {
2338 ret = psp_load_smu_fw(psp);
2344 if (fw_load_skip_check(psp, ucode))
2347 if (psp->autoload_supported &&
2348 (adev->asic_type >= CHIP_SIENNA_CICHLID &&
2349 adev->asic_type <= CHIP_DIMGREY_CAVEFISH) &&
2350 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2351 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2352 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2353 /* PSP only receive one SDMA fw for sienna_cichlid,
2354 * as all four sdma fw are same */
2357 psp_print_fw_hdr(psp, ucode);
2359 ret = psp_execute_np_fw_load(psp, ucode);
2363 /* Start rlc autoload after psp recieved all the gfx firmware */
2364 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2365 AMDGPU_UCODE_ID_CP_MEC2 : AMDGPU_UCODE_ID_RLC_G)) {
2366 ret = psp_rlc_autoload_start(psp);
2368 DRM_ERROR("Failed to start rlc autoload\n");
2377 static int psp_load_fw(struct amdgpu_device *adev)
2380 struct psp_context *psp = &adev->psp;
2382 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2383 psp_ring_stop(psp, PSP_RING_TYPE__KM); /* should not destroy ring, only stop */
2387 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
2391 if (amdgpu_sriov_vf(adev)) {
2392 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
2393 AMDGPU_GEM_DOMAIN_VRAM,
2395 &psp->fw_pri_mc_addr,
2398 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
2399 AMDGPU_GEM_DOMAIN_GTT,
2401 &psp->fw_pri_mc_addr,
2408 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
2409 AMDGPU_GEM_DOMAIN_VRAM,
2411 &psp->fence_buf_mc_addr,
2416 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
2417 AMDGPU_GEM_DOMAIN_VRAM,
2418 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
2419 (void **)&psp->cmd_buf_mem);
2423 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2425 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2427 DRM_ERROR("PSP ring init failed!\n");
2432 ret = psp_hw_start(psp);
2436 ret = psp_np_fw_load(psp);
2440 ret = psp_asd_load(psp);
2442 DRM_ERROR("PSP load asd failed!\n");
2446 ret = psp_rl_load(adev);
2448 DRM_ERROR("PSP load RL failed!\n");
2452 if (psp->adev->psp.ta_fw) {
2453 ret = psp_ras_initialize(psp);
2455 dev_err(psp->adev->dev,
2456 "RAS: Failed to initialize RAS\n");
2458 ret = psp_hdcp_initialize(psp);
2460 dev_err(psp->adev->dev,
2461 "HDCP: Failed to initialize HDCP\n");
2463 ret = psp_dtm_initialize(psp);
2465 dev_err(psp->adev->dev,
2466 "DTM: Failed to initialize DTM\n");
2468 ret = psp_rap_initialize(psp);
2470 dev_err(psp->adev->dev,
2471 "RAP: Failed to initialize RAP\n");
2473 ret = psp_securedisplay_initialize(psp);
2475 dev_err(psp->adev->dev,
2476 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2483 * all cleanup jobs (xgmi terminate, ras terminate,
2484 * ring destroy, cmd/fence/fw buffers destory,
2485 * psp->cmd destory) are delayed to psp_hw_fini
2490 static int psp_hw_init(void *handle)
2493 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2495 mutex_lock(&adev->firmware.mutex);
2497 * This sequence is just used on hw_init only once, no need on
2500 ret = amdgpu_ucode_init_bo(adev);
2504 ret = psp_load_fw(adev);
2506 DRM_ERROR("PSP firmware loading failed\n");
2510 mutex_unlock(&adev->firmware.mutex);
2514 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2515 mutex_unlock(&adev->firmware.mutex);
2519 static int psp_hw_fini(void *handle)
2521 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2522 struct psp_context *psp = &adev->psp;
2524 if (psp->adev->psp.ta_fw) {
2525 psp_ras_terminate(psp);
2526 psp_securedisplay_terminate(psp);
2527 psp_rap_terminate(psp);
2528 psp_dtm_terminate(psp);
2529 psp_hdcp_terminate(psp);
2532 psp_asd_unload(psp);
2534 psp_tmr_terminate(psp);
2535 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2537 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
2538 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
2539 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
2540 &psp->fence_buf_mc_addr, &psp->fence_buf);
2541 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
2542 (void **)&psp->cmd_buf_mem);
2550 static int psp_suspend(void *handle)
2553 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2554 struct psp_context *psp = &adev->psp;
2556 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2557 psp->xgmi_context.initialized == 1) {
2558 ret = psp_xgmi_terminate(psp);
2560 DRM_ERROR("Failed to terminate xgmi ta\n");
2565 if (psp->adev->psp.ta_fw) {
2566 ret = psp_ras_terminate(psp);
2568 DRM_ERROR("Failed to terminate ras ta\n");
2571 ret = psp_hdcp_terminate(psp);
2573 DRM_ERROR("Failed to terminate hdcp ta\n");
2576 ret = psp_dtm_terminate(psp);
2578 DRM_ERROR("Failed to terminate dtm ta\n");
2581 ret = psp_rap_terminate(psp);
2583 DRM_ERROR("Failed to terminate rap ta\n");
2586 ret = psp_securedisplay_terminate(psp);
2588 DRM_ERROR("Failed to terminate securedisplay ta\n");
2593 ret = psp_asd_unload(psp);
2595 DRM_ERROR("Failed to unload asd\n");
2599 ret = psp_tmr_terminate(psp);
2601 DRM_ERROR("Failed to terminate tmr\n");
2605 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2607 DRM_ERROR("PSP ring stop failed\n");
2614 static int psp_resume(void *handle)
2617 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2618 struct psp_context *psp = &adev->psp;
2620 DRM_INFO("PSP is resuming...\n");
2622 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2624 DRM_ERROR("Failed to process memory training!\n");
2628 mutex_lock(&adev->firmware.mutex);
2630 ret = psp_hw_start(psp);
2634 ret = psp_np_fw_load(psp);
2638 ret = psp_asd_load(psp);
2640 DRM_ERROR("PSP load asd failed!\n");
2644 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2645 ret = psp_xgmi_initialize(psp);
2646 /* Warning the XGMI seesion initialize failure
2647 * Instead of stop driver initialization
2650 dev_err(psp->adev->dev,
2651 "XGMI: Failed to initialize XGMI session\n");
2654 if (psp->adev->psp.ta_fw) {
2655 ret = psp_ras_initialize(psp);
2657 dev_err(psp->adev->dev,
2658 "RAS: Failed to initialize RAS\n");
2660 ret = psp_hdcp_initialize(psp);
2662 dev_err(psp->adev->dev,
2663 "HDCP: Failed to initialize HDCP\n");
2665 ret = psp_dtm_initialize(psp);
2667 dev_err(psp->adev->dev,
2668 "DTM: Failed to initialize DTM\n");
2670 ret = psp_rap_initialize(psp);
2672 dev_err(psp->adev->dev,
2673 "RAP: Failed to initialize RAP\n");
2675 ret = psp_securedisplay_initialize(psp);
2677 dev_err(psp->adev->dev,
2678 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2681 mutex_unlock(&adev->firmware.mutex);
2686 DRM_ERROR("PSP resume failed\n");
2687 mutex_unlock(&adev->firmware.mutex);
2691 int psp_gpu_reset(struct amdgpu_device *adev)
2695 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2698 mutex_lock(&adev->psp.mutex);
2699 ret = psp_mode1_reset(&adev->psp);
2700 mutex_unlock(&adev->psp.mutex);
2705 int psp_rlc_autoload_start(struct psp_context *psp)
2708 struct psp_gfx_cmd_resp *cmd;
2710 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
2714 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2716 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2717 psp->fence_buf_mc_addr);
2722 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2723 uint64_t cmd_gpu_addr, int cmd_size)
2725 struct amdgpu_firmware_info ucode = {0};
2727 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2728 AMDGPU_UCODE_ID_VCN0_RAM;
2729 ucode.mc_addr = cmd_gpu_addr;
2730 ucode.ucode_size = cmd_size;
2732 return psp_execute_np_fw_load(&adev->psp, &ucode);
2735 int psp_ring_cmd_submit(struct psp_context *psp,
2736 uint64_t cmd_buf_mc_addr,
2737 uint64_t fence_mc_addr,
2740 unsigned int psp_write_ptr_reg = 0;
2741 struct psp_gfx_rb_frame *write_frame;
2742 struct psp_ring *ring = &psp->km_ring;
2743 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2744 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2745 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2746 struct amdgpu_device *adev = psp->adev;
2747 uint32_t ring_size_dw = ring->ring_size / 4;
2748 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2750 /* KM (GPCOM) prepare write pointer */
2751 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2753 /* Update KM RB frame pointer to new frame */
2754 /* write_frame ptr increments by size of rb_frame in bytes */
2755 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2756 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2757 write_frame = ring_buffer_start;
2759 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2760 /* Check invalid write_frame ptr address */
2761 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2762 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2763 ring_buffer_start, ring_buffer_end, write_frame);
2764 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2768 /* Initialize KM RB frame */
2769 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2771 /* Update KM RB frame */
2772 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2773 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2774 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2775 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2776 write_frame->fence_value = index;
2777 amdgpu_device_flush_hdp(adev, NULL);
2779 /* Update the write Pointer in DWORDs */
2780 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2781 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2785 int psp_init_asd_microcode(struct psp_context *psp,
2786 const char *chip_name)
2788 struct amdgpu_device *adev = psp->adev;
2789 char fw_name[PSP_FW_NAME_LEN];
2790 const struct psp_firmware_header_v1_0 *asd_hdr;
2794 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2798 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2799 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2803 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2807 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2808 adev->psp.asd_fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2809 adev->psp.asd_feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2810 adev->psp.asd_ucode_size = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2811 adev->psp.asd_start_addr = (uint8_t *)asd_hdr +
2812 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2815 dev_err(adev->dev, "fail to initialize asd microcode\n");
2816 release_firmware(adev->psp.asd_fw);
2817 adev->psp.asd_fw = NULL;
2821 int psp_init_toc_microcode(struct psp_context *psp,
2822 const char *chip_name)
2824 struct amdgpu_device *adev = psp->adev;
2826 const struct psp_firmware_header_v1_0 *toc_hdr;
2830 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2834 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2835 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2839 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2843 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2844 adev->psp.toc_fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2845 adev->psp.toc_feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2846 adev->psp.toc_bin_size = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2847 adev->psp.toc_start_addr = (uint8_t *)toc_hdr +
2848 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2851 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2852 release_firmware(adev->psp.toc_fw);
2853 adev->psp.toc_fw = NULL;
2857 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
2859 const struct psp_firmware_header_v1_0 *sos_hdr;
2860 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
2862 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
2864 if (adev->gmc.xgmi.connected_to_cpu || (adev->asic_type != CHIP_ALDEBARAN)) {
2865 adev->psp.sos_fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
2866 adev->psp.sos_feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
2868 adev->psp.sys_bin_size = le32_to_cpu(sos_hdr->sos.offset_bytes);
2869 adev->psp.sys_start_addr = (uint8_t *)sos_hdr +
2870 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2872 adev->psp.sos_bin_size = le32_to_cpu(sos_hdr->sos.size_bytes);
2873 adev->psp.sos_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2874 le32_to_cpu(sos_hdr->sos.offset_bytes);
2876 /* Load alternate PSP SOS FW */
2877 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
2879 adev->psp.sos_fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
2880 adev->psp.sos_feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
2882 adev->psp.sys_bin_size = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
2883 adev->psp.sys_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2884 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
2886 adev->psp.sos_bin_size = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
2887 adev->psp.sos_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2888 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
2891 if ((adev->psp.sys_bin_size == 0) || (adev->psp.sos_bin_size == 0)) {
2892 dev_warn(adev->dev, "PSP SOS FW not available");
2899 int psp_init_sos_microcode(struct psp_context *psp,
2900 const char *chip_name)
2902 struct amdgpu_device *adev = psp->adev;
2903 char fw_name[PSP_FW_NAME_LEN];
2904 const struct psp_firmware_header_v1_0 *sos_hdr;
2905 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
2906 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
2907 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
2911 dev_err(adev->dev, "invalid chip name for sos microcode\n");
2915 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
2916 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
2920 err = amdgpu_ucode_validate(adev->psp.sos_fw);
2924 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
2925 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
2927 switch (sos_hdr->header.header_version_major) {
2929 err = psp_init_sos_base_fw(adev);
2933 if (sos_hdr->header.header_version_minor == 1) {
2934 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
2935 adev->psp.toc_bin_size = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
2936 adev->psp.toc_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2937 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
2938 adev->psp.kdb_bin_size = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
2939 adev->psp.kdb_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2940 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
2942 if (sos_hdr->header.header_version_minor == 2) {
2943 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
2944 adev->psp.kdb_bin_size = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
2945 adev->psp.kdb_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2946 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
2948 if (sos_hdr->header.header_version_minor == 3) {
2949 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
2950 adev->psp.toc_bin_size = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
2951 adev->psp.toc_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2952 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
2953 adev->psp.kdb_bin_size = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
2954 adev->psp.kdb_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2955 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
2956 adev->psp.spl_bin_size = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
2957 adev->psp.spl_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2958 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
2959 adev->psp.rl_bin_size = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
2960 adev->psp.rl_start_addr = (uint8_t *)adev->psp.sys_start_addr +
2961 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
2966 "unsupported psp sos firmware\n");
2974 "failed to init sos firmware\n");
2975 release_firmware(adev->psp.sos_fw);
2976 adev->psp.sos_fw = NULL;
2981 static int parse_ta_bin_descriptor(struct psp_context *psp,
2982 const struct ta_fw_bin_desc *desc,
2983 const struct ta_firmware_header_v2_0 *ta_hdr)
2985 uint8_t *ucode_start_addr = NULL;
2987 if (!psp || !desc || !ta_hdr)
2990 ucode_start_addr = (uint8_t *)ta_hdr +
2991 le32_to_cpu(desc->offset_bytes) +
2992 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
2994 switch (desc->fw_type) {
2995 case TA_FW_TYPE_PSP_ASD:
2996 psp->asd_fw_version = le32_to_cpu(desc->fw_version);
2997 psp->asd_feature_version = le32_to_cpu(desc->fw_version);
2998 psp->asd_ucode_size = le32_to_cpu(desc->size_bytes);
2999 psp->asd_start_addr = ucode_start_addr;
3001 case TA_FW_TYPE_PSP_XGMI:
3002 psp->ta_xgmi_ucode_version = le32_to_cpu(desc->fw_version);
3003 psp->ta_xgmi_ucode_size = le32_to_cpu(desc->size_bytes);
3004 psp->ta_xgmi_start_addr = ucode_start_addr;
3006 case TA_FW_TYPE_PSP_RAS:
3007 psp->ta_ras_ucode_version = le32_to_cpu(desc->fw_version);
3008 psp->ta_ras_ucode_size = le32_to_cpu(desc->size_bytes);
3009 psp->ta_ras_start_addr = ucode_start_addr;
3011 case TA_FW_TYPE_PSP_HDCP:
3012 psp->ta_hdcp_ucode_version = le32_to_cpu(desc->fw_version);
3013 psp->ta_hdcp_ucode_size = le32_to_cpu(desc->size_bytes);
3014 psp->ta_hdcp_start_addr = ucode_start_addr;
3016 case TA_FW_TYPE_PSP_DTM:
3017 psp->ta_dtm_ucode_version = le32_to_cpu(desc->fw_version);
3018 psp->ta_dtm_ucode_size = le32_to_cpu(desc->size_bytes);
3019 psp->ta_dtm_start_addr = ucode_start_addr;
3021 case TA_FW_TYPE_PSP_RAP:
3022 psp->ta_rap_ucode_version = le32_to_cpu(desc->fw_version);
3023 psp->ta_rap_ucode_size = le32_to_cpu(desc->size_bytes);
3024 psp->ta_rap_start_addr = ucode_start_addr;
3026 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3027 psp->ta_securedisplay_ucode_version = le32_to_cpu(desc->fw_version);
3028 psp->ta_securedisplay_ucode_size = le32_to_cpu(desc->size_bytes);
3029 psp->ta_securedisplay_start_addr = ucode_start_addr;
3032 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3039 int psp_init_ta_microcode(struct psp_context *psp,
3040 const char *chip_name)
3042 struct amdgpu_device *adev = psp->adev;
3043 char fw_name[PSP_FW_NAME_LEN];
3044 const struct ta_firmware_header_v2_0 *ta_hdr;
3049 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3053 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3054 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3058 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3062 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3064 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3065 dev_err(adev->dev, "unsupported TA header version\n");
3070 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_TA_PACKAGING) {
3071 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3076 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3077 err = parse_ta_bin_descriptor(psp,
3078 &ta_hdr->ta_fw_bin[ta_index],
3086 dev_err(adev->dev, "fail to initialize ta microcode\n");
3087 release_firmware(adev->psp.ta_fw);
3088 adev->psp.ta_fw = NULL;
3092 static int psp_set_clockgating_state(void *handle,
3093 enum amd_clockgating_state state)
3098 static int psp_set_powergating_state(void *handle,
3099 enum amd_powergating_state state)
3104 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3105 struct device_attribute *attr,
3108 struct drm_device *ddev = dev_get_drvdata(dev);
3109 struct amdgpu_device *adev = drm_to_adev(ddev);
3113 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3114 DRM_INFO("PSP block is not ready yet.");
3118 mutex_lock(&adev->psp.mutex);
3119 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3120 mutex_unlock(&adev->psp.mutex);
3123 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3127 return sysfs_emit(buf, "%x\n", fw_ver);
3130 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3131 struct device_attribute *attr,
3135 struct drm_device *ddev = dev_get_drvdata(dev);
3136 struct amdgpu_device *adev = drm_to_adev(ddev);
3138 dma_addr_t dma_addr;
3141 const struct firmware *usbc_pd_fw;
3143 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3144 DRM_INFO("PSP block is not ready yet.");
3148 if (!drm_dev_enter(ddev, &idx))
3151 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3152 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3156 /* We need contiguous physical mem to place the FW for psp to access */
3157 cpu_addr = dma_alloc_coherent(adev->dev, usbc_pd_fw->size, &dma_addr, GFP_KERNEL);
3159 ret = dma_mapping_error(adev->dev, dma_addr);
3163 memcpy_toio(cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3166 * x86 specific workaround.
3167 * Without it the buffer is invisible in PSP.
3169 * TODO Remove once PSP starts snooping CPU cache
3172 clflush_cache_range(cpu_addr, (usbc_pd_fw->size & ~(L1_CACHE_BYTES - 1)));
3175 mutex_lock(&adev->psp.mutex);
3176 ret = psp_load_usbc_pd_fw(&adev->psp, dma_addr);
3177 mutex_unlock(&adev->psp.mutex);
3180 dma_free_coherent(adev->dev, usbc_pd_fw->size, cpu_addr, dma_addr);
3181 release_firmware(usbc_pd_fw);
3184 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3192 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3196 if (!drm_dev_enter(&psp->adev->ddev, &idx))
3199 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3200 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3205 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3206 psp_usbc_pd_fw_sysfs_read,
3207 psp_usbc_pd_fw_sysfs_write);
3211 const struct amd_ip_funcs psp_ip_funcs = {
3213 .early_init = psp_early_init,
3215 .sw_init = psp_sw_init,
3216 .sw_fini = psp_sw_fini,
3217 .hw_init = psp_hw_init,
3218 .hw_fini = psp_hw_fini,
3219 .suspend = psp_suspend,
3220 .resume = psp_resume,
3222 .check_soft_reset = NULL,
3223 .wait_for_idle = NULL,
3225 .set_clockgating_state = psp_set_clockgating_state,
3226 .set_powergating_state = psp_set_powergating_state,
3229 static int psp_sysfs_init(struct amdgpu_device *adev)
3231 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3234 DRM_ERROR("Failed to create USBC PD FW control file!");
3239 static void psp_sysfs_fini(struct amdgpu_device *adev)
3241 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3244 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3246 .type = AMD_IP_BLOCK_TYPE_PSP,
3250 .funcs = &psp_ip_funcs,
3253 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3255 .type = AMD_IP_BLOCK_TYPE_PSP,
3259 .funcs = &psp_ip_funcs,
3262 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3264 .type = AMD_IP_BLOCK_TYPE_PSP,
3268 .funcs = &psp_ip_funcs,
3271 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3273 .type = AMD_IP_BLOCK_TYPE_PSP,
3277 .funcs = &psp_ip_funcs,
3280 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3281 .type = AMD_IP_BLOCK_TYPE_PSP,
3285 .funcs = &psp_ip_funcs,