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 <drm/drm_drv.h>
30 #include "amdgpu_psp.h"
31 #include "amdgpu_ucode.h"
32 #include "amdgpu_xgmi.h"
33 #include "soc15_common.h"
35 #include "psp_v10_0.h"
36 #include "psp_v11_0.h"
37 #include "psp_v11_0_8.h"
38 #include "psp_v12_0.h"
39 #include "psp_v13_0.h"
41 #include "amdgpu_ras.h"
42 #include "amdgpu_securedisplay.h"
43 #include "amdgpu_atomfirmware.h"
45 static int psp_sysfs_init(struct amdgpu_device *adev);
46 static void psp_sysfs_fini(struct amdgpu_device *adev);
48 static int psp_load_smu_fw(struct psp_context *psp);
49 static int psp_ta_unload(struct psp_context *psp, struct ta_context *context);
50 static int psp_ta_load(struct psp_context *psp, struct ta_context *context);
51 static int psp_rap_terminate(struct psp_context *psp);
52 static int psp_securedisplay_terminate(struct psp_context *psp);
55 * Due to DF Cstate management centralized to PMFW, the firmware
56 * loading sequence will be updated as below:
62 * - Load other non-psp fw
64 * - Load XGMI/RAS/HDCP/DTM TA if any
66 * This new sequence is required for
67 * - Arcturus and onwards
68 * - Navi12 and onwards
70 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
72 struct amdgpu_device *adev = psp->adev;
74 if (amdgpu_sriov_vf(adev)) {
75 psp->pmfw_centralized_cstate_management = false;
79 switch (adev->ip_versions[MP0_HWIP][0]) {
80 case IP_VERSION(11, 0, 4):
81 case IP_VERSION(11, 0, 7):
82 case IP_VERSION(11, 0, 9):
83 case IP_VERSION(11, 0, 11):
84 case IP_VERSION(11, 0, 12):
85 case IP_VERSION(11, 0, 13):
86 case IP_VERSION(13, 0, 2):
87 psp->pmfw_centralized_cstate_management = true;
90 psp->pmfw_centralized_cstate_management = false;
95 static int psp_early_init(void *handle)
97 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
98 struct psp_context *psp = &adev->psp;
100 switch (adev->ip_versions[MP0_HWIP][0]) {
101 case IP_VERSION(9, 0, 0):
102 psp_v3_1_set_psp_funcs(psp);
103 psp->autoload_supported = false;
105 case IP_VERSION(10, 0, 0):
106 case IP_VERSION(10, 0, 1):
107 psp_v10_0_set_psp_funcs(psp);
108 psp->autoload_supported = false;
110 case IP_VERSION(11, 0, 2):
111 case IP_VERSION(11, 0, 4):
112 psp_v11_0_set_psp_funcs(psp);
113 psp->autoload_supported = false;
115 case IP_VERSION(11, 0, 0):
116 case IP_VERSION(11, 0, 5):
117 case IP_VERSION(11, 0, 9):
118 case IP_VERSION(11, 0, 7):
119 case IP_VERSION(11, 0, 11):
120 case IP_VERSION(11, 5, 0):
121 case IP_VERSION(11, 0, 12):
122 case IP_VERSION(11, 0, 13):
123 psp_v11_0_set_psp_funcs(psp);
124 psp->autoload_supported = true;
126 case IP_VERSION(11, 0, 3):
127 case IP_VERSION(12, 0, 1):
128 psp_v12_0_set_psp_funcs(psp);
130 case IP_VERSION(13, 0, 2):
131 psp_v13_0_set_psp_funcs(psp);
133 case IP_VERSION(13, 0, 1):
134 case IP_VERSION(13, 0, 3):
135 psp_v13_0_set_psp_funcs(psp);
136 psp->autoload_supported = true;
138 case IP_VERSION(11, 0, 8):
139 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
140 psp_v11_0_8_set_psp_funcs(psp);
141 psp->autoload_supported = false;
150 psp_check_pmfw_centralized_cstate_management(psp);
155 static void psp_memory_training_fini(struct psp_context *psp)
157 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
159 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
160 kfree(ctx->sys_cache);
161 ctx->sys_cache = NULL;
164 static int psp_memory_training_init(struct psp_context *psp)
167 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
169 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
170 DRM_DEBUG("memory training is not supported!\n");
174 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
175 if (ctx->sys_cache == NULL) {
176 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
181 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
182 ctx->train_data_size,
183 ctx->p2c_train_data_offset,
184 ctx->c2p_train_data_offset);
185 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
189 psp_memory_training_fini(psp);
194 * Helper funciton to query psp runtime database entry
196 * @adev: amdgpu_device pointer
197 * @entry_type: the type of psp runtime database entry
198 * @db_entry: runtime database entry pointer
200 * Return false if runtime database doesn't exit or entry is invalid
201 * or true if the specific database entry is found, and copy to @db_entry
203 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
204 enum psp_runtime_entry_type entry_type,
207 uint64_t db_header_pos, db_dir_pos;
208 struct psp_runtime_data_header db_header = {0};
209 struct psp_runtime_data_directory db_dir = {0};
213 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
214 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
216 /* read runtime db header from vram */
217 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
218 sizeof(struct psp_runtime_data_header), false);
220 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
221 /* runtime db doesn't exist, exit */
222 dev_warn(adev->dev, "PSP runtime database doesn't exist\n");
226 /* read runtime database entry from vram */
227 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
228 sizeof(struct psp_runtime_data_directory), false);
230 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
231 /* invalid db entry count, exit */
232 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
236 /* look up for requested entry type */
237 for (i = 0; i < db_dir.entry_count && !ret; i++) {
238 if (db_dir.entry_list[i].entry_type == entry_type) {
239 switch (entry_type) {
240 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
241 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
242 /* invalid db entry size */
243 dev_warn(adev->dev, "Invalid PSP runtime database entry size\n");
246 /* read runtime database entry */
247 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
248 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
261 static int psp_sw_init(void *handle)
263 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
264 struct psp_context *psp = &adev->psp;
266 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
267 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
269 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
271 DRM_ERROR("Failed to allocate memory to command buffer!\n");
275 if (!amdgpu_sriov_vf(adev)) {
276 ret = psp_init_microcode(psp);
278 DRM_ERROR("Failed to load psp firmware!\n");
281 } else if (amdgpu_sriov_vf(adev) &&
282 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2)) {
283 ret = psp_init_ta_microcode(psp, "aldebaran");
285 DRM_ERROR("Failed to initialize ta microcode!\n");
290 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
291 if (psp_get_runtime_db_entry(adev,
292 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
294 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
295 if ((psp->boot_cfg_bitmask) &
296 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
297 /* If psp runtime database exists, then
298 * only enable two stage memory training
299 * when TWO_STAGE_DRAM_TRAINING bit is set
300 * in runtime database */
301 mem_training_ctx->enable_mem_training = true;
305 /* If psp runtime database doesn't exist or
306 * is invalid, force enable two stage memory
308 mem_training_ctx->enable_mem_training = true;
311 if (mem_training_ctx->enable_mem_training) {
312 ret = psp_memory_training_init(psp);
314 DRM_ERROR("Failed to initialize memory training!\n");
318 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
320 DRM_ERROR("Failed to process memory training!\n");
325 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
326 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
327 ret= psp_sysfs_init(adev);
336 static int psp_sw_fini(void *handle)
338 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
339 struct psp_context *psp = &adev->psp;
340 struct psp_gfx_cmd_resp *cmd = psp->cmd;
342 psp_memory_training_fini(psp);
344 release_firmware(psp->sos_fw);
348 release_firmware(psp->asd_fw);
352 release_firmware(psp->ta_fw);
356 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
357 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
358 psp_sysfs_fini(adev);
366 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
367 uint32_t reg_val, uint32_t mask, bool check_changed)
371 struct amdgpu_device *adev = psp->adev;
373 if (psp->adev->no_hw_access)
376 for (i = 0; i < adev->usec_timeout; i++) {
377 val = RREG32(reg_index);
382 if ((val & mask) == reg_val)
391 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
394 case GFX_CMD_ID_LOAD_TA:
396 case GFX_CMD_ID_UNLOAD_TA:
398 case GFX_CMD_ID_INVOKE_CMD:
400 case GFX_CMD_ID_LOAD_ASD:
402 case GFX_CMD_ID_SETUP_TMR:
404 case GFX_CMD_ID_LOAD_IP_FW:
406 case GFX_CMD_ID_DESTROY_TMR:
407 return "DESTROY_TMR";
408 case GFX_CMD_ID_SAVE_RESTORE:
409 return "SAVE_RESTORE_IP_FW";
410 case GFX_CMD_ID_SETUP_VMR:
412 case GFX_CMD_ID_DESTROY_VMR:
413 return "DESTROY_VMR";
414 case GFX_CMD_ID_PROG_REG:
416 case GFX_CMD_ID_GET_FW_ATTESTATION:
417 return "GET_FW_ATTESTATION";
418 case GFX_CMD_ID_LOAD_TOC:
419 return "ID_LOAD_TOC";
420 case GFX_CMD_ID_AUTOLOAD_RLC:
421 return "AUTOLOAD_RLC";
422 case GFX_CMD_ID_BOOT_CFG:
425 return "UNKNOWN CMD";
430 psp_cmd_submit_buf(struct psp_context *psp,
431 struct amdgpu_firmware_info *ucode,
432 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
437 bool ras_intr = false;
438 bool skip_unsupport = false;
440 if (psp->adev->no_hw_access)
443 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
446 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
448 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
450 index = atomic_inc_return(&psp->fence_value);
451 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
453 atomic_dec(&psp->fence_value);
457 amdgpu_device_invalidate_hdp(psp->adev, NULL);
458 while (*((unsigned int *)psp->fence_buf) != index) {
462 * Shouldn't wait for timeout when err_event_athub occurs,
463 * because gpu reset thread triggered and lock resource should
464 * be released for psp resume sequence.
466 ras_intr = amdgpu_ras_intr_triggered();
469 usleep_range(10, 100);
470 amdgpu_device_invalidate_hdp(psp->adev, NULL);
473 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
474 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
475 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
477 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
479 /* In some cases, psp response status is not 0 even there is no
480 * problem while the command is submitted. Some version of PSP FW
481 * doesn't write 0 to that field.
482 * So here we would like to only print a warning instead of an error
483 * during psp initialization to avoid breaking hw_init and it doesn't
486 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
488 DRM_WARN("failed to load ucode %s(0x%X) ",
489 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
490 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
491 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
492 psp->cmd_buf_mem->resp.status);
500 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
501 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
509 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
511 struct psp_gfx_cmd_resp *cmd = psp->cmd;
513 mutex_lock(&psp->mutex);
515 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
520 void release_psp_cmd_buf(struct psp_context *psp)
522 mutex_unlock(&psp->mutex);
525 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
526 struct psp_gfx_cmd_resp *cmd,
527 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
529 struct amdgpu_device *adev = psp->adev;
530 uint32_t size = amdgpu_bo_size(tmr_bo);
531 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
533 if (amdgpu_sriov_vf(psp->adev))
534 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
536 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
537 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
538 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
539 cmd->cmd.cmd_setup_tmr.buf_size = size;
540 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
541 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
542 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
545 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
546 uint64_t pri_buf_mc, uint32_t size)
548 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
549 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
550 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
551 cmd->cmd.cmd_load_toc.toc_size = size;
554 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
555 static int psp_load_toc(struct psp_context *psp,
559 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
561 /* Copy toc to psp firmware private buffer */
562 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
564 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
566 ret = psp_cmd_submit_buf(psp, NULL, cmd,
567 psp->fence_buf_mc_addr);
569 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
571 release_psp_cmd_buf(psp);
576 /* Set up Trusted Memory Region */
577 static int psp_tmr_init(struct psp_context *psp)
585 * According to HW engineer, they prefer the TMR address be "naturally
586 * aligned" , e.g. the start address be an integer divide of TMR size.
588 * Note: this memory need be reserved till the driver
591 tmr_size = PSP_TMR_SIZE(psp->adev);
593 /* For ASICs support RLC autoload, psp will parse the toc
594 * and calculate the total size of TMR needed */
595 if (!amdgpu_sriov_vf(psp->adev) &&
596 psp->toc.start_addr &&
597 psp->toc.size_bytes &&
599 ret = psp_load_toc(psp, &tmr_size);
601 DRM_ERROR("Failed to load toc\n");
606 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
607 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_SIZE(psp->adev),
608 AMDGPU_GEM_DOMAIN_VRAM,
609 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
614 static bool psp_skip_tmr(struct psp_context *psp)
616 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
617 case IP_VERSION(11, 0, 9):
618 case IP_VERSION(11, 0, 7):
619 case IP_VERSION(13, 0, 2):
626 static int psp_tmr_load(struct psp_context *psp)
629 struct psp_gfx_cmd_resp *cmd;
631 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
632 * Already set up by host driver.
634 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
637 cmd = acquire_psp_cmd_buf(psp);
639 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
640 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
641 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
643 ret = psp_cmd_submit_buf(psp, NULL, cmd,
644 psp->fence_buf_mc_addr);
646 release_psp_cmd_buf(psp);
651 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
652 struct psp_gfx_cmd_resp *cmd)
654 if (amdgpu_sriov_vf(psp->adev))
655 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
657 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
660 static int psp_tmr_unload(struct psp_context *psp)
663 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
665 psp_prep_tmr_unload_cmd_buf(psp, cmd);
666 DRM_INFO("free PSP TMR buffer\n");
668 ret = psp_cmd_submit_buf(psp, NULL, cmd,
669 psp->fence_buf_mc_addr);
671 release_psp_cmd_buf(psp);
676 static int psp_tmr_terminate(struct psp_context *psp)
682 ret = psp_tmr_unload(psp);
686 /* free TMR memory buffer */
687 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
688 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
693 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
694 uint64_t *output_ptr)
697 struct psp_gfx_cmd_resp *cmd;
702 if (amdgpu_sriov_vf(psp->adev))
705 cmd = acquire_psp_cmd_buf(psp);
707 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
709 ret = psp_cmd_submit_buf(psp, NULL, cmd,
710 psp->fence_buf_mc_addr);
713 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
714 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
717 release_psp_cmd_buf(psp);
722 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
724 struct psp_context *psp = &adev->psp;
725 struct psp_gfx_cmd_resp *cmd;
728 if (amdgpu_sriov_vf(adev))
731 cmd = acquire_psp_cmd_buf(psp);
733 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
734 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
736 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
739 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
742 release_psp_cmd_buf(psp);
747 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
750 struct psp_context *psp = &adev->psp;
751 struct psp_gfx_cmd_resp *cmd;
753 if (amdgpu_sriov_vf(adev))
756 cmd = acquire_psp_cmd_buf(psp);
758 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
759 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
760 cmd->cmd.boot_cfg.boot_config = boot_cfg;
761 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
763 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
765 release_psp_cmd_buf(psp);
770 static int psp_rl_load(struct amdgpu_device *adev)
773 struct psp_context *psp = &adev->psp;
774 struct psp_gfx_cmd_resp *cmd;
776 if (!is_psp_fw_valid(psp->rl))
779 cmd = acquire_psp_cmd_buf(psp);
781 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
782 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
784 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
785 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
786 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
787 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
788 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
790 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
792 release_psp_cmd_buf(psp);
797 static int psp_asd_load(struct psp_context *psp)
799 return psp_ta_load(psp, &psp->asd_context);
802 static int psp_asd_initialize(struct psp_context *psp)
806 /* If PSP version doesn't match ASD version, asd loading will be failed.
807 * add workaround to bypass it for sriov now.
808 * TODO: add version check to make it common
810 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
813 psp->asd_context.mem_context.shared_mc_addr = 0;
814 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
815 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
817 ret = psp_asd_load(psp);
819 psp->asd_context.initialized = true;
824 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
827 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
828 cmd->cmd.cmd_unload_ta.session_id = session_id;
831 static int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
834 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
836 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
838 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
840 release_psp_cmd_buf(psp);
845 static int psp_asd_unload(struct psp_context *psp)
847 return psp_ta_unload(psp, &psp->asd_context);
850 static int psp_asd_terminate(struct psp_context *psp)
854 if (amdgpu_sriov_vf(psp->adev))
857 if (!psp->asd_context.initialized)
860 ret = psp_asd_unload(psp);
863 psp->asd_context.initialized = false;
868 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
869 uint32_t id, uint32_t value)
871 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
872 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
873 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
876 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
879 struct psp_gfx_cmd_resp *cmd;
882 if (reg >= PSP_REG_LAST)
885 cmd = acquire_psp_cmd_buf(psp);
887 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
888 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
890 DRM_ERROR("PSP failed to program reg id %d", reg);
892 release_psp_cmd_buf(psp);
897 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
899 struct ta_context *context)
901 cmd->cmd_id = context->ta_load_type;
902 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
903 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
904 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
906 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
907 lower_32_bits(context->mem_context.shared_mc_addr);
908 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
909 upper_32_bits(context->mem_context.shared_mc_addr);
910 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
913 static int psp_ta_init_shared_buf(struct psp_context *psp,
914 struct ta_mem_context *mem_ctx)
919 * Allocate 16k memory aligned to 4k from Frame Buffer (local
920 * physical) for ta to host memory
922 ret = amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
923 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
925 &mem_ctx->shared_mc_addr,
926 &mem_ctx->shared_buf);
931 static void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
933 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
934 &mem_ctx->shared_buf);
937 static int psp_xgmi_init_shared_buf(struct psp_context *psp)
939 return psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
942 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
946 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
947 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
948 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
951 static int psp_ta_invoke(struct psp_context *psp,
953 struct ta_context *context)
956 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
958 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
960 ret = psp_cmd_submit_buf(psp, NULL, cmd,
961 psp->fence_buf_mc_addr);
963 release_psp_cmd_buf(psp);
968 static int psp_ta_load(struct psp_context *psp, struct ta_context *context)
971 struct psp_gfx_cmd_resp *cmd;
973 cmd = acquire_psp_cmd_buf(psp);
975 psp_copy_fw(psp, context->bin_desc.start_addr,
976 context->bin_desc.size_bytes);
978 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
980 ret = psp_cmd_submit_buf(psp, NULL, cmd,
981 psp->fence_buf_mc_addr);
984 context->session_id = cmd->resp.session_id;
987 release_psp_cmd_buf(psp);
992 static int psp_xgmi_load(struct psp_context *psp)
994 return psp_ta_load(psp, &psp->xgmi_context.context);
997 static int psp_xgmi_unload(struct psp_context *psp)
999 return psp_ta_unload(psp, &psp->xgmi_context.context);
1002 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1004 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1007 int psp_xgmi_terminate(struct psp_context *psp)
1010 struct amdgpu_device *adev = psp->adev;
1012 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1013 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1014 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1015 adev->gmc.xgmi.connected_to_cpu))
1018 if (!psp->xgmi_context.context.initialized)
1021 ret = psp_xgmi_unload(psp);
1025 psp->xgmi_context.context.initialized = false;
1027 /* free xgmi shared memory */
1028 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
1033 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1035 struct ta_xgmi_shared_memory *xgmi_cmd;
1039 !psp->xgmi_context.context.bin_desc.size_bytes ||
1040 !psp->xgmi_context.context.bin_desc.start_addr)
1046 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1047 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1049 if (!psp->xgmi_context.context.initialized) {
1050 ret = psp_xgmi_init_shared_buf(psp);
1056 ret = psp_xgmi_load(psp);
1058 psp->xgmi_context.context.initialized = true;
1063 /* Initialize XGMI session */
1064 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1065 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1066 xgmi_cmd->flag_extend_link_record = set_extended_data;
1067 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1069 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1074 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1076 struct ta_xgmi_shared_memory *xgmi_cmd;
1079 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1080 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1082 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1084 /* Invoke xgmi ta to get hive id */
1085 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1089 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1094 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1096 struct ta_xgmi_shared_memory *xgmi_cmd;
1099 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1100 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1102 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1104 /* Invoke xgmi ta to get the node id */
1105 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1109 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1114 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1116 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1117 psp->xgmi_context.context.bin_desc.feature_version >= 0x2000000b;
1121 * Chips that support extended topology information require the driver to
1122 * reflect topology information in the opposite direction. This is
1123 * because the TA has already exceeded its link record limit and if the
1124 * TA holds bi-directional information, the driver would have to do
1125 * multiple fetches instead of just two.
1127 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1128 struct psp_xgmi_node_info node_info)
1130 struct amdgpu_device *mirror_adev;
1131 struct amdgpu_hive_info *hive;
1132 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1133 uint64_t dst_node_id = node_info.node_id;
1134 uint8_t dst_num_hops = node_info.num_hops;
1135 uint8_t dst_num_links = node_info.num_links;
1137 hive = amdgpu_get_xgmi_hive(psp->adev);
1138 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1139 struct psp_xgmi_topology_info *mirror_top_info;
1142 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1145 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1146 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1147 if (mirror_top_info->nodes[j].node_id != src_node_id)
1150 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1152 * prevent 0 num_links value re-reflection since reflection
1153 * criteria is based on num_hops (direct or indirect).
1157 mirror_top_info->nodes[j].num_links = dst_num_links;
1166 int psp_xgmi_get_topology_info(struct psp_context *psp,
1168 struct psp_xgmi_topology_info *topology,
1169 bool get_extended_data)
1171 struct ta_xgmi_shared_memory *xgmi_cmd;
1172 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1173 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1177 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1180 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1181 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1182 xgmi_cmd->flag_extend_link_record = get_extended_data;
1184 /* Fill in the shared memory with topology information as input */
1185 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1186 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1187 topology_info_input->num_nodes = number_devices;
1189 for (i = 0; i < topology_info_input->num_nodes; i++) {
1190 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1191 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1192 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1193 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1196 /* Invoke xgmi ta to get the topology information */
1197 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1201 /* Read the output topology information from the shared memory */
1202 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1203 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1204 for (i = 0; i < topology->num_nodes; i++) {
1205 /* extended data will either be 0 or equal to non-extended data */
1206 if (topology_info_output->nodes[i].num_hops)
1207 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1209 /* non-extended data gets everything here so no need to update */
1210 if (!get_extended_data) {
1211 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1212 topology->nodes[i].is_sharing_enabled =
1213 topology_info_output->nodes[i].is_sharing_enabled;
1214 topology->nodes[i].sdma_engine =
1215 topology_info_output->nodes[i].sdma_engine;
1220 /* Invoke xgmi ta again to get the link information */
1221 if (psp_xgmi_peer_link_info_supported(psp)) {
1222 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1224 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1226 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1231 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1232 for (i = 0; i < topology->num_nodes; i++) {
1233 /* accumulate num_links on extended data */
1234 topology->nodes[i].num_links = get_extended_data ?
1235 topology->nodes[i].num_links +
1236 link_info_output->nodes[i].num_links :
1237 link_info_output->nodes[i].num_links;
1239 /* reflect the topology information for bi-directionality */
1240 if (psp->xgmi_context.supports_extended_data &&
1241 get_extended_data && topology->nodes[i].num_hops)
1242 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1249 int psp_xgmi_set_topology_info(struct psp_context *psp,
1251 struct psp_xgmi_topology_info *topology)
1253 struct ta_xgmi_shared_memory *xgmi_cmd;
1254 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1257 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1260 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1261 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1263 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1264 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1265 topology_info_input->num_nodes = number_devices;
1267 for (i = 0; i < topology_info_input->num_nodes; i++) {
1268 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1269 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1270 topology_info_input->nodes[i].is_sharing_enabled = 1;
1271 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1274 /* Invoke xgmi ta to set topology information */
1275 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1279 static int psp_ras_init_shared_buf(struct psp_context *psp)
1281 return psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1284 static int psp_ras_load(struct psp_context *psp)
1286 return psp_ta_load(psp, &psp->ras_context.context);
1289 static int psp_ras_unload(struct psp_context *psp)
1291 return psp_ta_unload(psp, &psp->ras_context.context);
1294 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1296 struct ta_ras_shared_memory *ras_cmd;
1299 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1302 * TODO: bypass the loading in sriov for now
1304 if (amdgpu_sriov_vf(psp->adev))
1307 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1309 if (amdgpu_ras_intr_triggered())
1312 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1314 DRM_WARN("RAS: Unsupported Interface");
1319 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1320 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1322 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1324 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1325 dev_warn(psp->adev->dev,
1326 "RAS internal register access blocked\n");
1328 if (ras_cmd->ras_status == TA_RAS_STATUS__ERROR_UNSUPPORTED_IP)
1329 dev_warn(psp->adev->dev, "RAS WARNING: cmd failed due to unsupported ip\n");
1330 else if (ras_cmd->ras_status)
1331 dev_warn(psp->adev->dev, "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1337 int psp_ras_enable_features(struct psp_context *psp,
1338 union ta_ras_cmd_input *info, bool enable)
1340 struct ta_ras_shared_memory *ras_cmd;
1343 if (!psp->ras_context.context.initialized)
1346 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1347 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1350 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1352 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1354 ras_cmd->ras_in_message = *info;
1356 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1363 static int psp_ras_terminate(struct psp_context *psp)
1368 * TODO: bypass the terminate in sriov for now
1370 if (amdgpu_sriov_vf(psp->adev))
1373 if (!psp->ras_context.context.initialized)
1376 ret = psp_ras_unload(psp);
1380 psp->ras_context.context.initialized = false;
1382 /* free ras shared memory */
1383 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
1388 static int psp_ras_initialize(struct psp_context *psp)
1391 uint32_t boot_cfg = 0xFF;
1392 struct amdgpu_device *adev = psp->adev;
1393 struct ta_ras_shared_memory *ras_cmd;
1396 * TODO: bypass the initialize in sriov for now
1398 if (amdgpu_sriov_vf(adev))
1401 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1402 !adev->psp.ras_context.context.bin_desc.start_addr) {
1403 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1407 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1408 /* query GECC enablement status from boot config
1409 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1411 ret = psp_boot_config_get(adev, &boot_cfg);
1413 dev_warn(adev->dev, "PSP get boot config failed\n");
1415 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1417 dev_info(adev->dev, "GECC is disabled\n");
1419 /* disable GECC in next boot cycle if ras is
1420 * disabled by module parameter amdgpu_ras_enable
1421 * and/or amdgpu_ras_mask, or boot_config_get call
1424 ret = psp_boot_config_set(adev, 0);
1426 dev_warn(adev->dev, "PSP set boot config failed\n");
1428 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1429 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1432 if (1 == boot_cfg) {
1433 dev_info(adev->dev, "GECC is enabled\n");
1435 /* enable GECC in next boot cycle if it is disabled
1436 * in boot config, or force enable GECC if failed to
1437 * get boot configuration
1439 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1441 dev_warn(adev->dev, "PSP set boot config failed\n");
1443 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1448 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1449 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1451 if (!psp->ras_context.context.initialized) {
1452 ret = psp_ras_init_shared_buf(psp);
1457 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1458 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1460 if (amdgpu_ras_is_poison_mode_supported(adev))
1461 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1462 if (!adev->gmc.xgmi.connected_to_cpu)
1463 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1465 ret = psp_ras_load(psp);
1467 if (!ret && !ras_cmd->ras_status)
1468 psp->ras_context.context.initialized = true;
1470 if (ras_cmd->ras_status)
1471 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1472 amdgpu_ras_fini(psp->adev);
1478 int psp_ras_trigger_error(struct psp_context *psp,
1479 struct ta_ras_trigger_error_input *info)
1481 struct ta_ras_shared_memory *ras_cmd;
1484 if (!psp->ras_context.context.initialized)
1487 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1488 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1490 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1491 ras_cmd->ras_in_message.trigger_error = *info;
1493 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1497 /* If err_event_athub occurs error inject was successful, however
1498 return status from TA is no long reliable */
1499 if (amdgpu_ras_intr_triggered())
1502 if (ras_cmd->ras_status)
1510 static int psp_hdcp_init_shared_buf(struct psp_context *psp)
1512 return psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1515 static int psp_hdcp_load(struct psp_context *psp)
1517 return psp_ta_load(psp, &psp->hdcp_context.context);
1520 static int psp_hdcp_initialize(struct psp_context *psp)
1525 * TODO: bypass the initialize in sriov for now
1527 if (amdgpu_sriov_vf(psp->adev))
1530 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1531 !psp->hdcp_context.context.bin_desc.start_addr) {
1532 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1536 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1537 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1539 if (!psp->hdcp_context.context.initialized) {
1540 ret = psp_hdcp_init_shared_buf(psp);
1545 ret = psp_hdcp_load(psp);
1547 psp->hdcp_context.context.initialized = true;
1548 mutex_init(&psp->hdcp_context.mutex);
1554 static int psp_hdcp_unload(struct psp_context *psp)
1556 return psp_ta_unload(psp, &psp->hdcp_context.context);
1559 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1562 * TODO: bypass the loading in sriov for now
1564 if (amdgpu_sriov_vf(psp->adev))
1567 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1570 static int psp_hdcp_terminate(struct psp_context *psp)
1575 * TODO: bypass the terminate in sriov for now
1577 if (amdgpu_sriov_vf(psp->adev))
1580 if (!psp->hdcp_context.context.initialized) {
1581 if (psp->hdcp_context.context.mem_context.shared_buf)
1587 ret = psp_hdcp_unload(psp);
1591 psp->hdcp_context.context.initialized = false;
1594 /* free hdcp shared memory */
1595 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
1602 static int psp_dtm_init_shared_buf(struct psp_context *psp)
1604 return psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1607 static int psp_dtm_load(struct psp_context *psp)
1609 return psp_ta_load(psp, &psp->dtm_context.context);
1612 static int psp_dtm_initialize(struct psp_context *psp)
1617 * TODO: bypass the initialize in sriov for now
1619 if (amdgpu_sriov_vf(psp->adev))
1622 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1623 !psp->dtm_context.context.bin_desc.start_addr) {
1624 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1628 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1629 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1631 if (!psp->dtm_context.context.initialized) {
1632 ret = psp_dtm_init_shared_buf(psp);
1637 ret = psp_dtm_load(psp);
1639 psp->dtm_context.context.initialized = true;
1640 mutex_init(&psp->dtm_context.mutex);
1646 static int psp_dtm_unload(struct psp_context *psp)
1648 return psp_ta_unload(psp, &psp->dtm_context.context);
1651 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1654 * TODO: bypass the loading in sriov for now
1656 if (amdgpu_sriov_vf(psp->adev))
1659 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1662 static int psp_dtm_terminate(struct psp_context *psp)
1667 * TODO: bypass the terminate in sriov for now
1669 if (amdgpu_sriov_vf(psp->adev))
1672 if (!psp->dtm_context.context.initialized) {
1673 if (psp->dtm_context.context.mem_context.shared_buf)
1679 ret = psp_dtm_unload(psp);
1683 psp->dtm_context.context.initialized = false;
1686 /* free dtm shared memory */
1687 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
1694 static int psp_rap_init_shared_buf(struct psp_context *psp)
1696 return psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1699 static int psp_rap_load(struct psp_context *psp)
1701 return psp_ta_load(psp, &psp->rap_context.context);
1704 static int psp_rap_unload(struct psp_context *psp)
1706 return psp_ta_unload(psp, &psp->rap_context.context);
1709 static int psp_rap_initialize(struct psp_context *psp)
1712 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1715 * TODO: bypass the initialize in sriov for now
1717 if (amdgpu_sriov_vf(psp->adev))
1720 if (!psp->rap_context.context.bin_desc.size_bytes ||
1721 !psp->rap_context.context.bin_desc.start_addr) {
1722 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1726 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1727 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1729 if (!psp->rap_context.context.initialized) {
1730 ret = psp_rap_init_shared_buf(psp);
1735 ret = psp_rap_load(psp);
1737 psp->rap_context.context.initialized = true;
1738 mutex_init(&psp->rap_context.mutex);
1742 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1743 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1744 psp_rap_terminate(psp);
1746 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1755 static int psp_rap_terminate(struct psp_context *psp)
1759 if (!psp->rap_context.context.initialized)
1762 ret = psp_rap_unload(psp);
1764 psp->rap_context.context.initialized = false;
1766 /* free rap shared memory */
1767 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1772 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1774 struct ta_rap_shared_memory *rap_cmd;
1777 if (!psp->rap_context.context.initialized)
1780 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1781 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1784 mutex_lock(&psp->rap_context.mutex);
1786 rap_cmd = (struct ta_rap_shared_memory *)
1787 psp->rap_context.context.mem_context.shared_buf;
1788 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1790 rap_cmd->cmd_id = ta_cmd_id;
1791 rap_cmd->validation_method_id = METHOD_A;
1793 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1798 *status = rap_cmd->rap_status;
1801 mutex_unlock(&psp->rap_context.mutex);
1807 /* securedisplay start */
1808 static int psp_securedisplay_init_shared_buf(struct psp_context *psp)
1810 return psp_ta_init_shared_buf(
1811 psp, &psp->securedisplay_context.context.mem_context);
1814 static int psp_securedisplay_load(struct psp_context *psp)
1816 return psp_ta_load(psp, &psp->securedisplay_context.context);
1819 static int psp_securedisplay_unload(struct psp_context *psp)
1821 return psp_ta_unload(psp, &psp->securedisplay_context.context);
1824 static int psp_securedisplay_initialize(struct psp_context *psp)
1827 struct securedisplay_cmd *securedisplay_cmd;
1830 * TODO: bypass the initialize in sriov for now
1832 if (amdgpu_sriov_vf(psp->adev))
1835 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1836 !psp->securedisplay_context.context.bin_desc.start_addr) {
1837 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1841 psp->securedisplay_context.context.mem_context.shared_mem_size =
1842 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1843 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1845 if (!psp->securedisplay_context.context.initialized) {
1846 ret = psp_securedisplay_init_shared_buf(psp);
1851 ret = psp_securedisplay_load(psp);
1853 psp->securedisplay_context.context.initialized = true;
1854 mutex_init(&psp->securedisplay_context.mutex);
1858 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1859 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1861 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1863 psp_securedisplay_terminate(psp);
1864 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1868 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1869 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1870 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1871 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1877 static int psp_securedisplay_terminate(struct psp_context *psp)
1882 * TODO:bypass the terminate in sriov for now
1884 if (amdgpu_sriov_vf(psp->adev))
1887 if (!psp->securedisplay_context.context.initialized)
1890 ret = psp_securedisplay_unload(psp);
1894 psp->securedisplay_context.context.initialized = false;
1896 /* free securedisplay shared memory */
1897 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1902 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1906 if (!psp->securedisplay_context.context.initialized)
1909 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1910 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1913 mutex_lock(&psp->securedisplay_context.mutex);
1915 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1917 mutex_unlock(&psp->securedisplay_context.mutex);
1921 /* SECUREDISPLAY end */
1923 static int psp_hw_start(struct psp_context *psp)
1925 struct amdgpu_device *adev = psp->adev;
1928 if (!amdgpu_sriov_vf(adev)) {
1929 if ((is_psp_fw_valid(psp->kdb)) &&
1930 (psp->funcs->bootloader_load_kdb != NULL)) {
1931 ret = psp_bootloader_load_kdb(psp);
1933 DRM_ERROR("PSP load kdb failed!\n");
1938 if ((is_psp_fw_valid(psp->spl)) &&
1939 (psp->funcs->bootloader_load_spl != NULL)) {
1940 ret = psp_bootloader_load_spl(psp);
1942 DRM_ERROR("PSP load spl failed!\n");
1947 if ((is_psp_fw_valid(psp->sys)) &&
1948 (psp->funcs->bootloader_load_sysdrv != NULL)) {
1949 ret = psp_bootloader_load_sysdrv(psp);
1951 DRM_ERROR("PSP load sys drv failed!\n");
1956 if ((is_psp_fw_valid(psp->soc_drv)) &&
1957 (psp->funcs->bootloader_load_soc_drv != NULL)) {
1958 ret = psp_bootloader_load_soc_drv(psp);
1960 DRM_ERROR("PSP load soc drv failed!\n");
1965 if ((is_psp_fw_valid(psp->intf_drv)) &&
1966 (psp->funcs->bootloader_load_intf_drv != NULL)) {
1967 ret = psp_bootloader_load_intf_drv(psp);
1969 DRM_ERROR("PSP load intf drv failed!\n");
1974 if ((is_psp_fw_valid(psp->dbg_drv)) &&
1975 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
1976 ret = psp_bootloader_load_dbg_drv(psp);
1978 DRM_ERROR("PSP load dbg drv failed!\n");
1983 if ((is_psp_fw_valid(psp->sos)) &&
1984 (psp->funcs->bootloader_load_sos != NULL)) {
1985 ret = psp_bootloader_load_sos(psp);
1987 DRM_ERROR("PSP load sos failed!\n");
1993 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
1995 DRM_ERROR("PSP create ring failed!\n");
1999 ret = psp_tmr_init(psp);
2001 DRM_ERROR("PSP tmr init failed!\n");
2006 * For ASICs with DF Cstate management centralized
2007 * to PMFW, TMR setup should be performed after PMFW
2008 * loaded and before other non-psp firmware loaded.
2010 if (psp->pmfw_centralized_cstate_management) {
2011 ret = psp_load_smu_fw(psp);
2016 ret = psp_tmr_load(psp);
2018 DRM_ERROR("PSP load tmr failed!\n");
2025 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2026 enum psp_gfx_fw_type *type)
2028 switch (ucode->ucode_id) {
2029 case AMDGPU_UCODE_ID_SDMA0:
2030 *type = GFX_FW_TYPE_SDMA0;
2032 case AMDGPU_UCODE_ID_SDMA1:
2033 *type = GFX_FW_TYPE_SDMA1;
2035 case AMDGPU_UCODE_ID_SDMA2:
2036 *type = GFX_FW_TYPE_SDMA2;
2038 case AMDGPU_UCODE_ID_SDMA3:
2039 *type = GFX_FW_TYPE_SDMA3;
2041 case AMDGPU_UCODE_ID_SDMA4:
2042 *type = GFX_FW_TYPE_SDMA4;
2044 case AMDGPU_UCODE_ID_SDMA5:
2045 *type = GFX_FW_TYPE_SDMA5;
2047 case AMDGPU_UCODE_ID_SDMA6:
2048 *type = GFX_FW_TYPE_SDMA6;
2050 case AMDGPU_UCODE_ID_SDMA7:
2051 *type = GFX_FW_TYPE_SDMA7;
2053 case AMDGPU_UCODE_ID_CP_MES:
2054 *type = GFX_FW_TYPE_CP_MES;
2056 case AMDGPU_UCODE_ID_CP_MES_DATA:
2057 *type = GFX_FW_TYPE_MES_STACK;
2059 case AMDGPU_UCODE_ID_CP_CE:
2060 *type = GFX_FW_TYPE_CP_CE;
2062 case AMDGPU_UCODE_ID_CP_PFP:
2063 *type = GFX_FW_TYPE_CP_PFP;
2065 case AMDGPU_UCODE_ID_CP_ME:
2066 *type = GFX_FW_TYPE_CP_ME;
2068 case AMDGPU_UCODE_ID_CP_MEC1:
2069 *type = GFX_FW_TYPE_CP_MEC;
2071 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2072 *type = GFX_FW_TYPE_CP_MEC_ME1;
2074 case AMDGPU_UCODE_ID_CP_MEC2:
2075 *type = GFX_FW_TYPE_CP_MEC;
2077 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2078 *type = GFX_FW_TYPE_CP_MEC_ME2;
2080 case AMDGPU_UCODE_ID_RLC_G:
2081 *type = GFX_FW_TYPE_RLC_G;
2083 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2084 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2086 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2087 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2089 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2090 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2092 case AMDGPU_UCODE_ID_RLC_IRAM:
2093 *type = GFX_FW_TYPE_RLC_IRAM;
2095 case AMDGPU_UCODE_ID_RLC_DRAM:
2096 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2098 case AMDGPU_UCODE_ID_SMC:
2099 *type = GFX_FW_TYPE_SMU;
2101 case AMDGPU_UCODE_ID_UVD:
2102 *type = GFX_FW_TYPE_UVD;
2104 case AMDGPU_UCODE_ID_UVD1:
2105 *type = GFX_FW_TYPE_UVD1;
2107 case AMDGPU_UCODE_ID_VCE:
2108 *type = GFX_FW_TYPE_VCE;
2110 case AMDGPU_UCODE_ID_VCN:
2111 *type = GFX_FW_TYPE_VCN;
2113 case AMDGPU_UCODE_ID_VCN1:
2114 *type = GFX_FW_TYPE_VCN1;
2116 case AMDGPU_UCODE_ID_DMCU_ERAM:
2117 *type = GFX_FW_TYPE_DMCU_ERAM;
2119 case AMDGPU_UCODE_ID_DMCU_INTV:
2120 *type = GFX_FW_TYPE_DMCU_ISR;
2122 case AMDGPU_UCODE_ID_VCN0_RAM:
2123 *type = GFX_FW_TYPE_VCN0_RAM;
2125 case AMDGPU_UCODE_ID_VCN1_RAM:
2126 *type = GFX_FW_TYPE_VCN1_RAM;
2128 case AMDGPU_UCODE_ID_DMCUB:
2129 *type = GFX_FW_TYPE_DMUB;
2131 case AMDGPU_UCODE_ID_MAXIMUM:
2139 static void psp_print_fw_hdr(struct psp_context *psp,
2140 struct amdgpu_firmware_info *ucode)
2142 struct amdgpu_device *adev = psp->adev;
2143 struct common_firmware_header *hdr;
2145 switch (ucode->ucode_id) {
2146 case AMDGPU_UCODE_ID_SDMA0:
2147 case AMDGPU_UCODE_ID_SDMA1:
2148 case AMDGPU_UCODE_ID_SDMA2:
2149 case AMDGPU_UCODE_ID_SDMA3:
2150 case AMDGPU_UCODE_ID_SDMA4:
2151 case AMDGPU_UCODE_ID_SDMA5:
2152 case AMDGPU_UCODE_ID_SDMA6:
2153 case AMDGPU_UCODE_ID_SDMA7:
2154 hdr = (struct common_firmware_header *)
2155 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2156 amdgpu_ucode_print_sdma_hdr(hdr);
2158 case AMDGPU_UCODE_ID_CP_CE:
2159 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2160 amdgpu_ucode_print_gfx_hdr(hdr);
2162 case AMDGPU_UCODE_ID_CP_PFP:
2163 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2164 amdgpu_ucode_print_gfx_hdr(hdr);
2166 case AMDGPU_UCODE_ID_CP_ME:
2167 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2168 amdgpu_ucode_print_gfx_hdr(hdr);
2170 case AMDGPU_UCODE_ID_CP_MEC1:
2171 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2172 amdgpu_ucode_print_gfx_hdr(hdr);
2174 case AMDGPU_UCODE_ID_RLC_G:
2175 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2176 amdgpu_ucode_print_rlc_hdr(hdr);
2178 case AMDGPU_UCODE_ID_SMC:
2179 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2180 amdgpu_ucode_print_smc_hdr(hdr);
2187 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2188 struct psp_gfx_cmd_resp *cmd)
2191 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2193 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2194 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2195 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2196 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2198 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2200 DRM_ERROR("Unknown firmware type\n");
2205 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2206 struct amdgpu_firmware_info *ucode)
2209 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2211 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2213 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2214 psp->fence_buf_mc_addr);
2217 release_psp_cmd_buf(psp);
2222 static int psp_load_smu_fw(struct psp_context *psp)
2225 struct amdgpu_device *adev = psp->adev;
2226 struct amdgpu_firmware_info *ucode =
2227 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2228 struct amdgpu_ras *ras = psp->ras_context.ras;
2230 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2233 if ((amdgpu_in_reset(adev) &&
2234 ras && adev->ras_enabled &&
2235 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2236 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2237 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2239 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2243 ret = psp_execute_non_psp_fw_load(psp, ucode);
2246 DRM_ERROR("PSP load smu failed!\n");
2251 static bool fw_load_skip_check(struct psp_context *psp,
2252 struct amdgpu_firmware_info *ucode)
2257 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2258 (psp_smu_reload_quirk(psp) ||
2259 psp->autoload_supported ||
2260 psp->pmfw_centralized_cstate_management))
2263 if (amdgpu_sriov_vf(psp->adev) &&
2264 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
2265 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
2266 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2
2267 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3
2268 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA4
2269 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA5
2270 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA6
2271 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA7
2272 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G
2273 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
2274 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
2275 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
2276 || ucode->ucode_id == AMDGPU_UCODE_ID_SMC))
2277 /*skip ucode loading in SRIOV VF */
2280 if (psp->autoload_supported &&
2281 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2282 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2283 /* skip mec JT when autoload is enabled */
2289 int psp_load_fw_list(struct psp_context *psp,
2290 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2293 struct amdgpu_firmware_info *ucode;
2295 for (i = 0; i < ucode_count; ++i) {
2296 ucode = ucode_list[i];
2297 psp_print_fw_hdr(psp, ucode);
2298 ret = psp_execute_non_psp_fw_load(psp, ucode);
2305 static int psp_load_non_psp_fw(struct psp_context *psp)
2308 struct amdgpu_firmware_info *ucode;
2309 struct amdgpu_device *adev = psp->adev;
2311 if (psp->autoload_supported &&
2312 !psp->pmfw_centralized_cstate_management) {
2313 ret = psp_load_smu_fw(psp);
2318 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2319 ucode = &adev->firmware.ucode[i];
2321 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2322 !fw_load_skip_check(psp, ucode)) {
2323 ret = psp_load_smu_fw(psp);
2329 if (fw_load_skip_check(psp, ucode))
2332 if (psp->autoload_supported &&
2333 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2334 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2335 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2336 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2337 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2338 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2339 /* PSP only receive one SDMA fw for sienna_cichlid,
2340 * as all four sdma fw are same */
2343 psp_print_fw_hdr(psp, ucode);
2345 ret = psp_execute_non_psp_fw_load(psp, ucode);
2349 /* Start rlc autoload after psp recieved all the gfx firmware */
2350 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2351 AMDGPU_UCODE_ID_CP_MEC2 : AMDGPU_UCODE_ID_RLC_G)) {
2352 ret = psp_rlc_autoload_start(psp);
2354 DRM_ERROR("Failed to start rlc autoload\n");
2363 static int psp_load_fw(struct amdgpu_device *adev)
2366 struct psp_context *psp = &adev->psp;
2368 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2369 psp_ring_stop(psp, PSP_RING_TYPE__KM); /* should not destroy ring, only stop */
2373 if (amdgpu_sriov_vf(adev)) {
2374 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
2375 AMDGPU_GEM_DOMAIN_VRAM,
2377 &psp->fw_pri_mc_addr,
2380 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
2381 AMDGPU_GEM_DOMAIN_GTT,
2383 &psp->fw_pri_mc_addr,
2390 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
2391 AMDGPU_GEM_DOMAIN_VRAM,
2393 &psp->fence_buf_mc_addr,
2398 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
2399 AMDGPU_GEM_DOMAIN_VRAM,
2400 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
2401 (void **)&psp->cmd_buf_mem);
2405 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2407 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2409 DRM_ERROR("PSP ring init failed!\n");
2414 ret = psp_hw_start(psp);
2418 ret = psp_load_non_psp_fw(psp);
2422 ret = psp_asd_initialize(psp);
2424 DRM_ERROR("PSP load asd failed!\n");
2428 ret = psp_rl_load(adev);
2430 DRM_ERROR("PSP load RL failed!\n");
2435 ret = psp_ras_initialize(psp);
2437 dev_err(psp->adev->dev,
2438 "RAS: Failed to initialize RAS\n");
2440 ret = psp_hdcp_initialize(psp);
2442 dev_err(psp->adev->dev,
2443 "HDCP: Failed to initialize HDCP\n");
2445 ret = psp_dtm_initialize(psp);
2447 dev_err(psp->adev->dev,
2448 "DTM: Failed to initialize DTM\n");
2450 ret = psp_rap_initialize(psp);
2452 dev_err(psp->adev->dev,
2453 "RAP: Failed to initialize RAP\n");
2455 ret = psp_securedisplay_initialize(psp);
2457 dev_err(psp->adev->dev,
2458 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2465 * all cleanup jobs (xgmi terminate, ras terminate,
2466 * ring destroy, cmd/fence/fw buffers destory,
2467 * psp->cmd destory) are delayed to psp_hw_fini
2472 static int psp_hw_init(void *handle)
2475 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2477 mutex_lock(&adev->firmware.mutex);
2479 * This sequence is just used on hw_init only once, no need on
2482 ret = amdgpu_ucode_init_bo(adev);
2486 ret = psp_load_fw(adev);
2488 DRM_ERROR("PSP firmware loading failed\n");
2492 mutex_unlock(&adev->firmware.mutex);
2496 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2497 mutex_unlock(&adev->firmware.mutex);
2501 static int psp_hw_fini(void *handle)
2503 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2504 struct psp_context *psp = &adev->psp;
2507 psp_ras_terminate(psp);
2508 psp_securedisplay_terminate(psp);
2509 psp_rap_terminate(psp);
2510 psp_dtm_terminate(psp);
2511 psp_hdcp_terminate(psp);
2514 psp_asd_terminate(psp);
2516 psp_tmr_terminate(psp);
2517 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2519 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
2520 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
2521 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
2522 &psp->fence_buf_mc_addr, &psp->fence_buf);
2523 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
2524 (void **)&psp->cmd_buf_mem);
2529 static int psp_suspend(void *handle)
2532 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2533 struct psp_context *psp = &adev->psp;
2535 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2536 psp->xgmi_context.context.initialized) {
2537 ret = psp_xgmi_terminate(psp);
2539 DRM_ERROR("Failed to terminate xgmi ta\n");
2545 ret = psp_ras_terminate(psp);
2547 DRM_ERROR("Failed to terminate ras ta\n");
2550 ret = psp_hdcp_terminate(psp);
2552 DRM_ERROR("Failed to terminate hdcp ta\n");
2555 ret = psp_dtm_terminate(psp);
2557 DRM_ERROR("Failed to terminate dtm ta\n");
2560 ret = psp_rap_terminate(psp);
2562 DRM_ERROR("Failed to terminate rap ta\n");
2565 ret = psp_securedisplay_terminate(psp);
2567 DRM_ERROR("Failed to terminate securedisplay ta\n");
2572 ret = psp_asd_terminate(psp);
2574 DRM_ERROR("Failed to terminate asd\n");
2578 ret = psp_tmr_terminate(psp);
2580 DRM_ERROR("Failed to terminate tmr\n");
2584 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2586 DRM_ERROR("PSP ring stop failed\n");
2593 static int psp_resume(void *handle)
2596 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2597 struct psp_context *psp = &adev->psp;
2599 DRM_INFO("PSP is resuming...\n");
2601 if (psp->mem_train_ctx.enable_mem_training) {
2602 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2604 DRM_ERROR("Failed to process memory training!\n");
2609 mutex_lock(&adev->firmware.mutex);
2611 ret = psp_hw_start(psp);
2615 ret = psp_load_non_psp_fw(psp);
2619 ret = psp_asd_initialize(psp);
2621 DRM_ERROR("PSP load asd failed!\n");
2625 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2626 ret = psp_xgmi_initialize(psp, false, true);
2627 /* Warning the XGMI seesion initialize failure
2628 * Instead of stop driver initialization
2631 dev_err(psp->adev->dev,
2632 "XGMI: Failed to initialize XGMI session\n");
2636 ret = psp_ras_initialize(psp);
2638 dev_err(psp->adev->dev,
2639 "RAS: Failed to initialize RAS\n");
2641 ret = psp_hdcp_initialize(psp);
2643 dev_err(psp->adev->dev,
2644 "HDCP: Failed to initialize HDCP\n");
2646 ret = psp_dtm_initialize(psp);
2648 dev_err(psp->adev->dev,
2649 "DTM: Failed to initialize DTM\n");
2651 ret = psp_rap_initialize(psp);
2653 dev_err(psp->adev->dev,
2654 "RAP: Failed to initialize RAP\n");
2656 ret = psp_securedisplay_initialize(psp);
2658 dev_err(psp->adev->dev,
2659 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2662 mutex_unlock(&adev->firmware.mutex);
2667 DRM_ERROR("PSP resume failed\n");
2668 mutex_unlock(&adev->firmware.mutex);
2672 int psp_gpu_reset(struct amdgpu_device *adev)
2676 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2679 mutex_lock(&adev->psp.mutex);
2680 ret = psp_mode1_reset(&adev->psp);
2681 mutex_unlock(&adev->psp.mutex);
2686 int psp_rlc_autoload_start(struct psp_context *psp)
2689 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2691 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2693 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2694 psp->fence_buf_mc_addr);
2696 release_psp_cmd_buf(psp);
2701 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2702 uint64_t cmd_gpu_addr, int cmd_size)
2704 struct amdgpu_firmware_info ucode = {0};
2706 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2707 AMDGPU_UCODE_ID_VCN0_RAM;
2708 ucode.mc_addr = cmd_gpu_addr;
2709 ucode.ucode_size = cmd_size;
2711 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2714 int psp_ring_cmd_submit(struct psp_context *psp,
2715 uint64_t cmd_buf_mc_addr,
2716 uint64_t fence_mc_addr,
2719 unsigned int psp_write_ptr_reg = 0;
2720 struct psp_gfx_rb_frame *write_frame;
2721 struct psp_ring *ring = &psp->km_ring;
2722 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2723 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2724 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2725 struct amdgpu_device *adev = psp->adev;
2726 uint32_t ring_size_dw = ring->ring_size / 4;
2727 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2729 /* KM (GPCOM) prepare write pointer */
2730 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2732 /* Update KM RB frame pointer to new frame */
2733 /* write_frame ptr increments by size of rb_frame in bytes */
2734 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2735 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2736 write_frame = ring_buffer_start;
2738 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2739 /* Check invalid write_frame ptr address */
2740 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2741 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2742 ring_buffer_start, ring_buffer_end, write_frame);
2743 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2747 /* Initialize KM RB frame */
2748 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2750 /* Update KM RB frame */
2751 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2752 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2753 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2754 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2755 write_frame->fence_value = index;
2756 amdgpu_device_flush_hdp(adev, NULL);
2758 /* Update the write Pointer in DWORDs */
2759 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2760 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2764 int psp_init_asd_microcode(struct psp_context *psp,
2765 const char *chip_name)
2767 struct amdgpu_device *adev = psp->adev;
2768 char fw_name[PSP_FW_NAME_LEN];
2769 const struct psp_firmware_header_v1_0 *asd_hdr;
2773 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2777 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2778 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2782 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2786 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2787 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2788 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2789 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2790 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2791 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2794 dev_err(adev->dev, "fail to initialize asd microcode\n");
2795 release_firmware(adev->psp.asd_fw);
2796 adev->psp.asd_fw = NULL;
2800 int psp_init_toc_microcode(struct psp_context *psp,
2801 const char *chip_name)
2803 struct amdgpu_device *adev = psp->adev;
2804 char fw_name[PSP_FW_NAME_LEN];
2805 const struct psp_firmware_header_v1_0 *toc_hdr;
2809 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2813 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2814 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2818 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2822 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2823 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2824 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2825 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2826 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2827 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2830 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2831 release_firmware(adev->psp.toc_fw);
2832 adev->psp.toc_fw = NULL;
2836 static int parse_sos_bin_descriptor(struct psp_context *psp,
2837 const struct psp_fw_bin_desc *desc,
2838 const struct psp_firmware_header_v2_0 *sos_hdr)
2840 uint8_t *ucode_start_addr = NULL;
2842 if (!psp || !desc || !sos_hdr)
2845 ucode_start_addr = (uint8_t *)sos_hdr +
2846 le32_to_cpu(desc->offset_bytes) +
2847 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2849 switch (desc->fw_type) {
2850 case PSP_FW_TYPE_PSP_SOS:
2851 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
2852 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
2853 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
2854 psp->sos.start_addr = ucode_start_addr;
2856 case PSP_FW_TYPE_PSP_SYS_DRV:
2857 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
2858 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
2859 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
2860 psp->sys.start_addr = ucode_start_addr;
2862 case PSP_FW_TYPE_PSP_KDB:
2863 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
2864 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
2865 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
2866 psp->kdb.start_addr = ucode_start_addr;
2868 case PSP_FW_TYPE_PSP_TOC:
2869 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
2870 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
2871 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
2872 psp->toc.start_addr = ucode_start_addr;
2874 case PSP_FW_TYPE_PSP_SPL:
2875 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
2876 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
2877 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
2878 psp->spl.start_addr = ucode_start_addr;
2880 case PSP_FW_TYPE_PSP_RL:
2881 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
2882 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
2883 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
2884 psp->rl.start_addr = ucode_start_addr;
2886 case PSP_FW_TYPE_PSP_SOC_DRV:
2887 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
2888 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
2889 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2890 psp->soc_drv.start_addr = ucode_start_addr;
2892 case PSP_FW_TYPE_PSP_INTF_DRV:
2893 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
2894 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
2895 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2896 psp->intf_drv.start_addr = ucode_start_addr;
2898 case PSP_FW_TYPE_PSP_DBG_DRV:
2899 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
2900 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
2901 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2902 psp->dbg_drv.start_addr = ucode_start_addr;
2905 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
2912 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
2914 const struct psp_firmware_header_v1_0 *sos_hdr;
2915 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
2916 uint8_t *ucode_array_start_addr;
2918 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
2919 ucode_array_start_addr = (uint8_t *)sos_hdr +
2920 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2922 if (adev->gmc.xgmi.connected_to_cpu ||
2923 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
2924 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
2925 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
2927 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
2928 adev->psp.sys.start_addr = ucode_array_start_addr;
2930 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
2931 adev->psp.sos.start_addr = ucode_array_start_addr +
2932 le32_to_cpu(sos_hdr->sos.offset_bytes);
2933 adev->psp.xgmi_context.supports_extended_data = false;
2935 /* Load alternate PSP SOS FW */
2936 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
2938 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
2939 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
2941 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
2942 adev->psp.sys.start_addr = ucode_array_start_addr +
2943 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
2945 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
2946 adev->psp.sos.start_addr = ucode_array_start_addr +
2947 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
2948 adev->psp.xgmi_context.supports_extended_data = true;
2951 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
2952 dev_warn(adev->dev, "PSP SOS FW not available");
2959 int psp_init_sos_microcode(struct psp_context *psp,
2960 const char *chip_name)
2962 struct amdgpu_device *adev = psp->adev;
2963 char fw_name[PSP_FW_NAME_LEN];
2964 const struct psp_firmware_header_v1_0 *sos_hdr;
2965 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
2966 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
2967 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
2968 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
2970 uint8_t *ucode_array_start_addr;
2974 dev_err(adev->dev, "invalid chip name for sos microcode\n");
2978 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
2979 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
2983 err = amdgpu_ucode_validate(adev->psp.sos_fw);
2987 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
2988 ucode_array_start_addr = (uint8_t *)sos_hdr +
2989 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2990 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
2992 switch (sos_hdr->header.header_version_major) {
2994 err = psp_init_sos_base_fw(adev);
2998 if (sos_hdr->header.header_version_minor == 1) {
2999 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3000 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3001 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3002 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3003 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3004 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3005 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3007 if (sos_hdr->header.header_version_minor == 2) {
3008 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3009 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3010 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3011 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3013 if (sos_hdr->header.header_version_minor == 3) {
3014 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3015 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3016 adev->psp.toc.start_addr = ucode_array_start_addr +
3017 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3018 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3019 adev->psp.kdb.start_addr = ucode_array_start_addr +
3020 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3021 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3022 adev->psp.spl.start_addr = ucode_array_start_addr +
3023 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3024 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3025 adev->psp.rl.start_addr = ucode_array_start_addr +
3026 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3030 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3032 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3033 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3038 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3039 err = parse_sos_bin_descriptor(psp,
3040 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3048 "unsupported psp sos firmware\n");
3056 "failed to init sos firmware\n");
3057 release_firmware(adev->psp.sos_fw);
3058 adev->psp.sos_fw = NULL;
3063 static int parse_ta_bin_descriptor(struct psp_context *psp,
3064 const struct psp_fw_bin_desc *desc,
3065 const struct ta_firmware_header_v2_0 *ta_hdr)
3067 uint8_t *ucode_start_addr = NULL;
3069 if (!psp || !desc || !ta_hdr)
3072 ucode_start_addr = (uint8_t *)ta_hdr +
3073 le32_to_cpu(desc->offset_bytes) +
3074 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3076 switch (desc->fw_type) {
3077 case TA_FW_TYPE_PSP_ASD:
3078 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3079 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3080 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3081 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3083 case TA_FW_TYPE_PSP_XGMI:
3084 psp->xgmi_context.context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3085 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3086 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3088 case TA_FW_TYPE_PSP_RAS:
3089 psp->ras_context.context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3090 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3091 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3093 case TA_FW_TYPE_PSP_HDCP:
3094 psp->hdcp_context.context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3095 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3096 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3098 case TA_FW_TYPE_PSP_DTM:
3099 psp->dtm_context.context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3100 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3101 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3103 case TA_FW_TYPE_PSP_RAP:
3104 psp->rap_context.context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3105 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3106 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3108 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3109 psp->securedisplay_context.context.bin_desc.feature_version =
3110 le32_to_cpu(desc->fw_version);
3111 psp->securedisplay_context.context.bin_desc.size_bytes =
3112 le32_to_cpu(desc->size_bytes);
3113 psp->securedisplay_context.context.bin_desc.start_addr =
3117 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3124 int psp_init_ta_microcode(struct psp_context *psp,
3125 const char *chip_name)
3127 struct amdgpu_device *adev = psp->adev;
3128 char fw_name[PSP_FW_NAME_LEN];
3129 const struct ta_firmware_header_v2_0 *ta_hdr;
3134 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3138 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3139 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3143 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3147 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3149 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3150 dev_err(adev->dev, "unsupported TA header version\n");
3155 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3156 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3161 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3162 err = parse_ta_bin_descriptor(psp,
3163 &ta_hdr->ta_fw_bin[ta_index],
3171 dev_err(adev->dev, "fail to initialize ta microcode\n");
3172 release_firmware(adev->psp.ta_fw);
3173 adev->psp.ta_fw = NULL;
3177 static int psp_set_clockgating_state(void *handle,
3178 enum amd_clockgating_state state)
3183 static int psp_set_powergating_state(void *handle,
3184 enum amd_powergating_state state)
3189 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3190 struct device_attribute *attr,
3193 struct drm_device *ddev = dev_get_drvdata(dev);
3194 struct amdgpu_device *adev = drm_to_adev(ddev);
3198 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3199 DRM_INFO("PSP block is not ready yet.");
3203 mutex_lock(&adev->psp.mutex);
3204 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3205 mutex_unlock(&adev->psp.mutex);
3208 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3212 return sysfs_emit(buf, "%x\n", fw_ver);
3215 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3216 struct device_attribute *attr,
3220 struct drm_device *ddev = dev_get_drvdata(dev);
3221 struct amdgpu_device *adev = drm_to_adev(ddev);
3224 const struct firmware *usbc_pd_fw;
3225 struct amdgpu_bo *fw_buf_bo = NULL;
3226 uint64_t fw_pri_mc_addr;
3227 void *fw_pri_cpu_addr;
3229 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3230 DRM_INFO("PSP block is not ready yet.");
3234 if (!drm_dev_enter(ddev, &idx))
3237 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3238 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3242 /* LFB address which is aligned to 1MB boundary per PSP request */
3243 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3244 AMDGPU_GEM_DOMAIN_VRAM,
3251 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3253 mutex_lock(&adev->psp.mutex);
3254 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3255 mutex_unlock(&adev->psp.mutex);
3257 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3260 release_firmware(usbc_pd_fw);
3263 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3271 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3275 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3278 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3279 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3284 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3285 psp_usbc_pd_fw_sysfs_read,
3286 psp_usbc_pd_fw_sysfs_write);
3288 int is_psp_fw_valid(struct psp_bin_desc bin)
3290 return bin.size_bytes;
3293 const struct amd_ip_funcs psp_ip_funcs = {
3295 .early_init = psp_early_init,
3297 .sw_init = psp_sw_init,
3298 .sw_fini = psp_sw_fini,
3299 .hw_init = psp_hw_init,
3300 .hw_fini = psp_hw_fini,
3301 .suspend = psp_suspend,
3302 .resume = psp_resume,
3304 .check_soft_reset = NULL,
3305 .wait_for_idle = NULL,
3307 .set_clockgating_state = psp_set_clockgating_state,
3308 .set_powergating_state = psp_set_powergating_state,
3311 static int psp_sysfs_init(struct amdgpu_device *adev)
3313 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3316 DRM_ERROR("Failed to create USBC PD FW control file!");
3321 static void psp_sysfs_fini(struct amdgpu_device *adev)
3323 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3326 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3328 .type = AMD_IP_BLOCK_TYPE_PSP,
3332 .funcs = &psp_ip_funcs,
3335 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3337 .type = AMD_IP_BLOCK_TYPE_PSP,
3341 .funcs = &psp_ip_funcs,
3344 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3346 .type = AMD_IP_BLOCK_TYPE_PSP,
3350 .funcs = &psp_ip_funcs,
3353 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3354 .type = AMD_IP_BLOCK_TYPE_PSP,
3358 .funcs = &psp_ip_funcs,
3361 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3363 .type = AMD_IP_BLOCK_TYPE_PSP,
3367 .funcs = &psp_ip_funcs,
3370 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3371 .type = AMD_IP_BLOCK_TYPE_PSP,
3375 .funcs = &psp_ip_funcs,