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_rap_terminate(struct psp_context *psp);
50 static int psp_securedisplay_terminate(struct psp_context *psp);
53 * Due to DF Cstate management centralized to PMFW, the firmware
54 * loading sequence will be updated as below:
60 * - Load other non-psp fw
62 * - Load XGMI/RAS/HDCP/DTM TA if any
64 * This new sequence is required for
65 * - Arcturus and onwards
67 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
69 struct amdgpu_device *adev = psp->adev;
71 if (amdgpu_sriov_vf(adev)) {
72 psp->pmfw_centralized_cstate_management = false;
76 switch (adev->ip_versions[MP0_HWIP][0]) {
77 case IP_VERSION(11, 0, 0):
78 case IP_VERSION(11, 0, 4):
79 case IP_VERSION(11, 0, 5):
80 case IP_VERSION(11, 0, 7):
81 case IP_VERSION(11, 0, 9):
82 case IP_VERSION(11, 0, 11):
83 case IP_VERSION(11, 0, 12):
84 case IP_VERSION(11, 0, 13):
85 case IP_VERSION(13, 0, 2):
86 psp->pmfw_centralized_cstate_management = true;
89 psp->pmfw_centralized_cstate_management = false;
94 static int psp_early_init(void *handle)
96 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
97 struct psp_context *psp = &adev->psp;
99 switch (adev->ip_versions[MP0_HWIP][0]) {
100 case IP_VERSION(9, 0, 0):
101 psp_v3_1_set_psp_funcs(psp);
102 psp->autoload_supported = false;
104 case IP_VERSION(10, 0, 0):
105 case IP_VERSION(10, 0, 1):
106 psp_v10_0_set_psp_funcs(psp);
107 psp->autoload_supported = false;
109 case IP_VERSION(11, 0, 2):
110 case IP_VERSION(11, 0, 4):
111 psp_v11_0_set_psp_funcs(psp);
112 psp->autoload_supported = false;
114 case IP_VERSION(11, 0, 0):
115 case IP_VERSION(11, 0, 5):
116 case IP_VERSION(11, 0, 9):
117 case IP_VERSION(11, 0, 7):
118 case IP_VERSION(11, 0, 11):
119 case IP_VERSION(11, 5, 0):
120 case IP_VERSION(11, 0, 12):
121 case IP_VERSION(11, 0, 13):
122 psp_v11_0_set_psp_funcs(psp);
123 psp->autoload_supported = true;
125 case IP_VERSION(11, 0, 3):
126 case IP_VERSION(12, 0, 1):
127 psp_v12_0_set_psp_funcs(psp);
129 case IP_VERSION(13, 0, 2):
130 psp_v13_0_set_psp_funcs(psp);
132 case IP_VERSION(13, 0, 1):
133 case IP_VERSION(13, 0, 3):
134 case IP_VERSION(13, 0, 5):
135 case IP_VERSION(13, 0, 8):
136 psp_v13_0_set_psp_funcs(psp);
137 psp->autoload_supported = true;
139 case IP_VERSION(11, 0, 8):
140 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
141 psp_v11_0_8_set_psp_funcs(psp);
142 psp->autoload_supported = false;
151 psp_check_pmfw_centralized_cstate_management(psp);
156 static void psp_memory_training_fini(struct psp_context *psp)
158 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
160 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
161 kfree(ctx->sys_cache);
162 ctx->sys_cache = NULL;
165 static int psp_memory_training_init(struct psp_context *psp)
168 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
170 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
171 DRM_DEBUG("memory training is not supported!\n");
175 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
176 if (ctx->sys_cache == NULL) {
177 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
182 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
183 ctx->train_data_size,
184 ctx->p2c_train_data_offset,
185 ctx->c2p_train_data_offset);
186 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
190 psp_memory_training_fini(psp);
195 * Helper funciton to query psp runtime database entry
197 * @adev: amdgpu_device pointer
198 * @entry_type: the type of psp runtime database entry
199 * @db_entry: runtime database entry pointer
201 * Return false if runtime database doesn't exit or entry is invalid
202 * or true if the specific database entry is found, and copy to @db_entry
204 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
205 enum psp_runtime_entry_type entry_type,
208 uint64_t db_header_pos, db_dir_pos;
209 struct psp_runtime_data_header db_header = {0};
210 struct psp_runtime_data_directory db_dir = {0};
214 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
215 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
217 /* read runtime db header from vram */
218 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
219 sizeof(struct psp_runtime_data_header), false);
221 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
222 /* runtime db doesn't exist, exit */
223 dev_warn(adev->dev, "PSP runtime database doesn't exist\n");
227 /* read runtime database entry from vram */
228 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
229 sizeof(struct psp_runtime_data_directory), false);
231 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
232 /* invalid db entry count, exit */
233 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
237 /* look up for requested entry type */
238 for (i = 0; i < db_dir.entry_count && !ret; i++) {
239 if (db_dir.entry_list[i].entry_type == entry_type) {
240 switch (entry_type) {
241 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
242 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
243 /* invalid db entry size */
244 dev_warn(adev->dev, "Invalid PSP runtime database entry size\n");
247 /* read runtime database entry */
248 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
249 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
262 static int psp_init_sriov_microcode(struct psp_context *psp)
264 struct amdgpu_device *adev = psp->adev;
267 switch (adev->ip_versions[MP0_HWIP][0]) {
268 case IP_VERSION(9, 0, 0):
269 ret = psp_init_cap_microcode(psp, "vega10");
271 case IP_VERSION(11, 0, 9):
272 ret = psp_init_cap_microcode(psp, "navi12");
274 case IP_VERSION(11, 0, 7):
275 ret = psp_init_cap_microcode(psp, "sienna_cichlid");
277 case IP_VERSION(13, 0, 2):
278 ret = psp_init_cap_microcode(psp, "aldebaran");
279 ret &= psp_init_ta_microcode(psp, "aldebaran");
289 static int psp_sw_init(void *handle)
291 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
292 struct psp_context *psp = &adev->psp;
294 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
295 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
297 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
299 DRM_ERROR("Failed to allocate memory to command buffer!\n");
303 if (amdgpu_sriov_vf(adev))
304 ret = psp_init_sriov_microcode(psp);
306 ret = psp_init_microcode(psp);
308 DRM_ERROR("Failed to load psp firmware!\n");
312 adev->psp.xgmi_context.supports_extended_data =
313 !adev->gmc.xgmi.connected_to_cpu &&
314 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
316 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
317 if (psp_get_runtime_db_entry(adev,
318 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
320 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
321 if ((psp->boot_cfg_bitmask) &
322 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
323 /* If psp runtime database exists, then
324 * only enable two stage memory training
325 * when TWO_STAGE_DRAM_TRAINING bit is set
326 * in runtime database */
327 mem_training_ctx->enable_mem_training = true;
331 /* If psp runtime database doesn't exist or
332 * is invalid, force enable two stage memory
334 mem_training_ctx->enable_mem_training = true;
337 if (mem_training_ctx->enable_mem_training) {
338 ret = psp_memory_training_init(psp);
340 DRM_ERROR("Failed to initialize memory training!\n");
344 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
346 DRM_ERROR("Failed to process memory training!\n");
351 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
352 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
353 ret= psp_sysfs_init(adev);
359 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
360 amdgpu_sriov_vf(adev) ?
361 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
363 &psp->fw_pri_mc_addr,
368 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
369 AMDGPU_GEM_DOMAIN_VRAM,
371 &psp->fence_buf_mc_addr,
376 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
377 AMDGPU_GEM_DOMAIN_VRAM,
378 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
379 (void **)&psp->cmd_buf_mem);
386 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
387 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
389 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
390 &psp->fence_buf_mc_addr, &psp->fence_buf);
394 static int psp_sw_fini(void *handle)
396 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
397 struct psp_context *psp = &adev->psp;
398 struct psp_gfx_cmd_resp *cmd = psp->cmd;
400 psp_memory_training_fini(psp);
402 release_firmware(psp->sos_fw);
406 release_firmware(psp->asd_fw);
410 release_firmware(psp->ta_fw);
413 if (adev->psp.cap_fw) {
414 release_firmware(psp->cap_fw);
418 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
419 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
420 psp_sysfs_fini(adev);
425 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
426 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
427 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
428 &psp->fence_buf_mc_addr, &psp->fence_buf);
429 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
430 (void **)&psp->cmd_buf_mem);
435 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
436 uint32_t reg_val, uint32_t mask, bool check_changed)
440 struct amdgpu_device *adev = psp->adev;
442 if (psp->adev->no_hw_access)
445 for (i = 0; i < adev->usec_timeout; i++) {
446 val = RREG32(reg_index);
451 if ((val & mask) == reg_val)
460 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
463 case GFX_CMD_ID_LOAD_TA:
465 case GFX_CMD_ID_UNLOAD_TA:
467 case GFX_CMD_ID_INVOKE_CMD:
469 case GFX_CMD_ID_LOAD_ASD:
471 case GFX_CMD_ID_SETUP_TMR:
473 case GFX_CMD_ID_LOAD_IP_FW:
475 case GFX_CMD_ID_DESTROY_TMR:
476 return "DESTROY_TMR";
477 case GFX_CMD_ID_SAVE_RESTORE:
478 return "SAVE_RESTORE_IP_FW";
479 case GFX_CMD_ID_SETUP_VMR:
481 case GFX_CMD_ID_DESTROY_VMR:
482 return "DESTROY_VMR";
483 case GFX_CMD_ID_PROG_REG:
485 case GFX_CMD_ID_GET_FW_ATTESTATION:
486 return "GET_FW_ATTESTATION";
487 case GFX_CMD_ID_LOAD_TOC:
488 return "ID_LOAD_TOC";
489 case GFX_CMD_ID_AUTOLOAD_RLC:
490 return "AUTOLOAD_RLC";
491 case GFX_CMD_ID_BOOT_CFG:
494 return "UNKNOWN CMD";
499 psp_cmd_submit_buf(struct psp_context *psp,
500 struct amdgpu_firmware_info *ucode,
501 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
506 bool ras_intr = false;
507 bool skip_unsupport = false;
509 if (psp->adev->no_hw_access)
512 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
515 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
517 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
519 index = atomic_inc_return(&psp->fence_value);
520 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
522 atomic_dec(&psp->fence_value);
526 amdgpu_device_invalidate_hdp(psp->adev, NULL);
527 while (*((unsigned int *)psp->fence_buf) != index) {
531 * Shouldn't wait for timeout when err_event_athub occurs,
532 * because gpu reset thread triggered and lock resource should
533 * be released for psp resume sequence.
535 ras_intr = amdgpu_ras_intr_triggered();
538 usleep_range(10, 100);
539 amdgpu_device_invalidate_hdp(psp->adev, NULL);
542 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
543 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
544 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
546 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
548 /* In some cases, psp response status is not 0 even there is no
549 * problem while the command is submitted. Some version of PSP FW
550 * doesn't write 0 to that field.
551 * So here we would like to only print a warning instead of an error
552 * during psp initialization to avoid breaking hw_init and it doesn't
555 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
557 DRM_WARN("failed to load ucode %s(0x%X) ",
558 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
559 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
560 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
561 psp->cmd_buf_mem->resp.status);
562 /* If we load CAP FW, PSP must return 0 under SRIOV
563 * also return failure in case of timeout
565 if ((ucode && (ucode->ucode_id == AMDGPU_UCODE_ID_CAP)) || !timeout) {
572 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
573 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
581 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
583 struct psp_gfx_cmd_resp *cmd = psp->cmd;
585 mutex_lock(&psp->mutex);
587 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
592 static void release_psp_cmd_buf(struct psp_context *psp)
594 mutex_unlock(&psp->mutex);
597 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
598 struct psp_gfx_cmd_resp *cmd,
599 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
601 struct amdgpu_device *adev = psp->adev;
602 uint32_t size = amdgpu_bo_size(tmr_bo);
603 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
605 if (amdgpu_sriov_vf(psp->adev))
606 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
608 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
609 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
610 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
611 cmd->cmd.cmd_setup_tmr.buf_size = size;
612 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
613 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
614 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
617 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
618 uint64_t pri_buf_mc, uint32_t size)
620 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
621 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
622 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
623 cmd->cmd.cmd_load_toc.toc_size = size;
626 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
627 static int psp_load_toc(struct psp_context *psp,
631 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
633 /* Copy toc to psp firmware private buffer */
634 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
636 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
638 ret = psp_cmd_submit_buf(psp, NULL, cmd,
639 psp->fence_buf_mc_addr);
641 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
643 release_psp_cmd_buf(psp);
648 /* Set up Trusted Memory Region */
649 static int psp_tmr_init(struct psp_context *psp)
657 * According to HW engineer, they prefer the TMR address be "naturally
658 * aligned" , e.g. the start address be an integer divide of TMR size.
660 * Note: this memory need be reserved till the driver
663 tmr_size = PSP_TMR_SIZE(psp->adev);
665 /* For ASICs support RLC autoload, psp will parse the toc
666 * and calculate the total size of TMR needed */
667 if (!amdgpu_sriov_vf(psp->adev) &&
668 psp->toc.start_addr &&
669 psp->toc.size_bytes &&
671 ret = psp_load_toc(psp, &tmr_size);
673 DRM_ERROR("Failed to load toc\n");
678 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
679 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_SIZE(psp->adev),
680 AMDGPU_GEM_DOMAIN_VRAM,
681 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
686 static bool psp_skip_tmr(struct psp_context *psp)
688 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
689 case IP_VERSION(11, 0, 9):
690 case IP_VERSION(11, 0, 7):
691 case IP_VERSION(13, 0, 2):
698 static int psp_tmr_load(struct psp_context *psp)
701 struct psp_gfx_cmd_resp *cmd;
703 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
704 * Already set up by host driver.
706 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
709 cmd = acquire_psp_cmd_buf(psp);
711 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
712 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
713 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
715 ret = psp_cmd_submit_buf(psp, NULL, cmd,
716 psp->fence_buf_mc_addr);
718 release_psp_cmd_buf(psp);
723 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
724 struct psp_gfx_cmd_resp *cmd)
726 if (amdgpu_sriov_vf(psp->adev))
727 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
729 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
732 static int psp_tmr_unload(struct psp_context *psp)
735 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
737 psp_prep_tmr_unload_cmd_buf(psp, cmd);
738 DRM_INFO("free PSP TMR buffer\n");
740 ret = psp_cmd_submit_buf(psp, NULL, cmd,
741 psp->fence_buf_mc_addr);
743 release_psp_cmd_buf(psp);
748 static int psp_tmr_terminate(struct psp_context *psp)
754 ret = psp_tmr_unload(psp);
756 /* free TMR memory buffer */
757 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
758 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
763 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
764 uint64_t *output_ptr)
767 struct psp_gfx_cmd_resp *cmd;
772 if (amdgpu_sriov_vf(psp->adev))
775 cmd = acquire_psp_cmd_buf(psp);
777 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
779 ret = psp_cmd_submit_buf(psp, NULL, cmd,
780 psp->fence_buf_mc_addr);
783 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
784 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
787 release_psp_cmd_buf(psp);
792 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
794 struct psp_context *psp = &adev->psp;
795 struct psp_gfx_cmd_resp *cmd;
798 if (amdgpu_sriov_vf(adev))
801 cmd = acquire_psp_cmd_buf(psp);
803 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
804 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
806 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
809 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
812 release_psp_cmd_buf(psp);
817 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
820 struct psp_context *psp = &adev->psp;
821 struct psp_gfx_cmd_resp *cmd;
823 if (amdgpu_sriov_vf(adev))
826 cmd = acquire_psp_cmd_buf(psp);
828 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
829 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
830 cmd->cmd.boot_cfg.boot_config = boot_cfg;
831 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
833 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
835 release_psp_cmd_buf(psp);
840 static int psp_rl_load(struct amdgpu_device *adev)
843 struct psp_context *psp = &adev->psp;
844 struct psp_gfx_cmd_resp *cmd;
846 if (!is_psp_fw_valid(psp->rl))
849 cmd = acquire_psp_cmd_buf(psp);
851 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
852 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
854 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
855 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
856 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
857 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
858 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
860 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
862 release_psp_cmd_buf(psp);
867 static int psp_asd_initialize(struct psp_context *psp)
871 /* If PSP version doesn't match ASD version, asd loading will be failed.
872 * add workaround to bypass it for sriov now.
873 * TODO: add version check to make it common
875 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
878 psp->asd_context.mem_context.shared_mc_addr = 0;
879 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
880 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
882 ret = psp_ta_load(psp, &psp->asd_context);
884 psp->asd_context.initialized = true;
889 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
892 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
893 cmd->cmd.cmd_unload_ta.session_id = session_id;
896 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
899 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
901 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
903 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
905 release_psp_cmd_buf(psp);
910 static int psp_asd_terminate(struct psp_context *psp)
914 if (amdgpu_sriov_vf(psp->adev))
917 if (!psp->asd_context.initialized)
920 ret = psp_ta_unload(psp, &psp->asd_context);
922 psp->asd_context.initialized = false;
927 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
928 uint32_t id, uint32_t value)
930 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
931 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
932 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
935 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
938 struct psp_gfx_cmd_resp *cmd;
941 if (reg >= PSP_REG_LAST)
944 cmd = acquire_psp_cmd_buf(psp);
946 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
947 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
949 DRM_ERROR("PSP failed to program reg id %d", reg);
951 release_psp_cmd_buf(psp);
956 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
958 struct ta_context *context)
960 cmd->cmd_id = context->ta_load_type;
961 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
962 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
963 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
965 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
966 lower_32_bits(context->mem_context.shared_mc_addr);
967 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
968 upper_32_bits(context->mem_context.shared_mc_addr);
969 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
972 int psp_ta_init_shared_buf(struct psp_context *psp,
973 struct ta_mem_context *mem_ctx)
976 * Allocate 16k memory aligned to 4k from Frame Buffer (local
977 * physical) for ta to host memory
979 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
980 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
982 &mem_ctx->shared_mc_addr,
983 &mem_ctx->shared_buf);
986 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
988 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
989 &mem_ctx->shared_buf);
992 static void psp_prep_ta_invoke_indirect_cmd_buf(struct psp_gfx_cmd_resp *cmd,
994 struct ta_context *context)
996 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
997 cmd->cmd.cmd_invoke_cmd.session_id = context->session_id;
998 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1000 cmd->cmd.cmd_invoke_cmd.buf.num_desc = 1;
1001 cmd->cmd.cmd_invoke_cmd.buf.total_size = context->mem_context.shared_mem_size;
1002 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_size = context->mem_context.shared_mem_size;
1003 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_phy_addr_lo =
1004 lower_32_bits(context->mem_context.shared_mc_addr);
1005 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_phy_addr_hi =
1006 upper_32_bits(context->mem_context.shared_mc_addr);
1009 int psp_ta_invoke_indirect(struct psp_context *psp,
1011 struct ta_context *context)
1014 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1016 psp_prep_ta_invoke_indirect_cmd_buf(cmd, ta_cmd_id, context);
1018 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1019 psp->fence_buf_mc_addr);
1021 context->resp_status = cmd->resp.status;
1023 release_psp_cmd_buf(psp);
1028 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1030 uint32_t session_id)
1032 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1033 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1034 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1037 int psp_ta_invoke(struct psp_context *psp,
1039 struct ta_context *context)
1042 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1044 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1046 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1047 psp->fence_buf_mc_addr);
1049 context->resp_status = cmd->resp.status;
1051 release_psp_cmd_buf(psp);
1056 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1059 struct psp_gfx_cmd_resp *cmd;
1061 cmd = acquire_psp_cmd_buf(psp);
1063 psp_copy_fw(psp, context->bin_desc.start_addr,
1064 context->bin_desc.size_bytes);
1066 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1068 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1069 psp->fence_buf_mc_addr);
1071 context->resp_status = cmd->resp.status;
1074 context->session_id = cmd->resp.session_id;
1077 release_psp_cmd_buf(psp);
1082 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1084 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1087 int psp_xgmi_terminate(struct psp_context *psp)
1090 struct amdgpu_device *adev = psp->adev;
1092 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1093 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1094 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1095 adev->gmc.xgmi.connected_to_cpu))
1098 if (!psp->xgmi_context.context.initialized)
1101 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1103 psp->xgmi_context.context.initialized = false;
1105 /* free xgmi shared memory */
1106 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
1111 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1113 struct ta_xgmi_shared_memory *xgmi_cmd;
1117 !psp->xgmi_context.context.bin_desc.size_bytes ||
1118 !psp->xgmi_context.context.bin_desc.start_addr)
1124 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1125 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1127 if (!psp->xgmi_context.context.initialized) {
1128 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1134 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1136 psp->xgmi_context.context.initialized = true;
1141 /* Initialize XGMI session */
1142 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1143 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1144 xgmi_cmd->flag_extend_link_record = set_extended_data;
1145 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1147 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1152 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1154 struct ta_xgmi_shared_memory *xgmi_cmd;
1157 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1158 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1160 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1162 /* Invoke xgmi ta to get hive id */
1163 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1167 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1172 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1174 struct ta_xgmi_shared_memory *xgmi_cmd;
1177 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1178 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1180 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1182 /* Invoke xgmi ta to get the node id */
1183 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1187 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1192 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1194 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1195 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1199 * Chips that support extended topology information require the driver to
1200 * reflect topology information in the opposite direction. This is
1201 * because the TA has already exceeded its link record limit and if the
1202 * TA holds bi-directional information, the driver would have to do
1203 * multiple fetches instead of just two.
1205 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1206 struct psp_xgmi_node_info node_info)
1208 struct amdgpu_device *mirror_adev;
1209 struct amdgpu_hive_info *hive;
1210 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1211 uint64_t dst_node_id = node_info.node_id;
1212 uint8_t dst_num_hops = node_info.num_hops;
1213 uint8_t dst_num_links = node_info.num_links;
1215 hive = amdgpu_get_xgmi_hive(psp->adev);
1216 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1217 struct psp_xgmi_topology_info *mirror_top_info;
1220 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1223 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1224 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1225 if (mirror_top_info->nodes[j].node_id != src_node_id)
1228 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1230 * prevent 0 num_links value re-reflection since reflection
1231 * criteria is based on num_hops (direct or indirect).
1235 mirror_top_info->nodes[j].num_links = dst_num_links;
1244 int psp_xgmi_get_topology_info(struct psp_context *psp,
1246 struct psp_xgmi_topology_info *topology,
1247 bool get_extended_data)
1249 struct ta_xgmi_shared_memory *xgmi_cmd;
1250 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1251 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1255 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1258 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1259 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1260 xgmi_cmd->flag_extend_link_record = get_extended_data;
1262 /* Fill in the shared memory with topology information as input */
1263 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1264 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_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 = topology->nodes[i].is_sharing_enabled;
1271 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1274 /* Invoke xgmi ta to get the topology information */
1275 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1279 /* Read the output topology information from the shared memory */
1280 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1281 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1282 for (i = 0; i < topology->num_nodes; i++) {
1283 /* extended data will either be 0 or equal to non-extended data */
1284 if (topology_info_output->nodes[i].num_hops)
1285 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1287 /* non-extended data gets everything here so no need to update */
1288 if (!get_extended_data) {
1289 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1290 topology->nodes[i].is_sharing_enabled =
1291 topology_info_output->nodes[i].is_sharing_enabled;
1292 topology->nodes[i].sdma_engine =
1293 topology_info_output->nodes[i].sdma_engine;
1298 /* Invoke xgmi ta again to get the link information */
1299 if (psp_xgmi_peer_link_info_supported(psp)) {
1300 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1302 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1304 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1309 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1310 for (i = 0; i < topology->num_nodes; i++) {
1311 /* accumulate num_links on extended data */
1312 topology->nodes[i].num_links = get_extended_data ?
1313 topology->nodes[i].num_links +
1314 link_info_output->nodes[i].num_links :
1315 link_info_output->nodes[i].num_links;
1317 /* reflect the topology information for bi-directionality */
1318 if (psp->xgmi_context.supports_extended_data &&
1319 get_extended_data && topology->nodes[i].num_hops)
1320 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1327 int psp_xgmi_set_topology_info(struct psp_context *psp,
1329 struct psp_xgmi_topology_info *topology)
1331 struct ta_xgmi_shared_memory *xgmi_cmd;
1332 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1335 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1338 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1339 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1341 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1342 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1343 topology_info_input->num_nodes = number_devices;
1345 for (i = 0; i < topology_info_input->num_nodes; i++) {
1346 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1347 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1348 topology_info_input->nodes[i].is_sharing_enabled = 1;
1349 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1352 /* Invoke xgmi ta to set topology information */
1353 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1357 static void psp_ras_ta_check_status(struct psp_context *psp)
1359 struct ta_ras_shared_memory *ras_cmd =
1360 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1362 switch (ras_cmd->ras_status) {
1363 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1364 dev_warn(psp->adev->dev,
1365 "RAS WARNING: cmd failed due to unsupported ip\n");
1367 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1368 dev_warn(psp->adev->dev,
1369 "RAS WARNING: cmd failed due to unsupported error injection\n");
1371 case TA_RAS_STATUS__SUCCESS:
1373 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1374 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1375 dev_warn(psp->adev->dev,
1376 "RAS WARNING: Inject error to critical region is not allowed\n");
1379 dev_warn(psp->adev->dev,
1380 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1385 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1387 struct ta_ras_shared_memory *ras_cmd;
1390 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1393 * TODO: bypass the loading in sriov for now
1395 if (amdgpu_sriov_vf(psp->adev))
1398 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1400 if (amdgpu_ras_intr_triggered())
1403 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1405 DRM_WARN("RAS: Unsupported Interface");
1410 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1411 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1413 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1415 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1416 dev_warn(psp->adev->dev,
1417 "RAS internal register access blocked\n");
1419 psp_ras_ta_check_status(psp);
1425 int psp_ras_enable_features(struct psp_context *psp,
1426 union ta_ras_cmd_input *info, bool enable)
1428 struct ta_ras_shared_memory *ras_cmd;
1431 if (!psp->ras_context.context.initialized)
1434 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1435 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1438 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1440 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1442 ras_cmd->ras_in_message = *info;
1444 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1451 int psp_ras_terminate(struct psp_context *psp)
1456 * TODO: bypass the terminate in sriov for now
1458 if (amdgpu_sriov_vf(psp->adev))
1461 if (!psp->ras_context.context.initialized)
1464 ret = psp_ta_unload(psp, &psp->ras_context.context);
1466 psp->ras_context.context.initialized = false;
1468 /* free ras shared memory */
1469 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
1474 static int psp_ras_initialize(struct psp_context *psp)
1477 uint32_t boot_cfg = 0xFF;
1478 struct amdgpu_device *adev = psp->adev;
1479 struct ta_ras_shared_memory *ras_cmd;
1482 * TODO: bypass the initialize in sriov for now
1484 if (amdgpu_sriov_vf(adev))
1487 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1488 !adev->psp.ras_context.context.bin_desc.start_addr) {
1489 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1493 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1494 /* query GECC enablement status from boot config
1495 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1497 ret = psp_boot_config_get(adev, &boot_cfg);
1499 dev_warn(adev->dev, "PSP get boot config failed\n");
1501 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1503 dev_info(adev->dev, "GECC is disabled\n");
1505 /* disable GECC in next boot cycle if ras is
1506 * disabled by module parameter amdgpu_ras_enable
1507 * and/or amdgpu_ras_mask, or boot_config_get call
1510 ret = psp_boot_config_set(adev, 0);
1512 dev_warn(adev->dev, "PSP set boot config failed\n");
1514 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1515 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1518 if (1 == boot_cfg) {
1519 dev_info(adev->dev, "GECC is enabled\n");
1521 /* enable GECC in next boot cycle if it is disabled
1522 * in boot config, or force enable GECC if failed to
1523 * get boot configuration
1525 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1527 dev_warn(adev->dev, "PSP set boot config failed\n");
1529 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1534 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1535 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1537 if (!psp->ras_context.context.initialized) {
1538 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1543 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1544 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1546 if (amdgpu_ras_is_poison_mode_supported(adev))
1547 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1548 if (!adev->gmc.xgmi.connected_to_cpu)
1549 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1551 ret = psp_ta_load(psp, &psp->ras_context.context);
1553 if (!ret && !ras_cmd->ras_status)
1554 psp->ras_context.context.initialized = true;
1556 if (ras_cmd->ras_status)
1557 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1558 amdgpu_ras_fini(psp->adev);
1564 int psp_ras_trigger_error(struct psp_context *psp,
1565 struct ta_ras_trigger_error_input *info)
1567 struct ta_ras_shared_memory *ras_cmd;
1570 if (!psp->ras_context.context.initialized)
1573 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1574 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1576 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1577 ras_cmd->ras_in_message.trigger_error = *info;
1579 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1583 /* If err_event_athub occurs error inject was successful, however
1584 return status from TA is no long reliable */
1585 if (amdgpu_ras_intr_triggered())
1588 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1590 else if (ras_cmd->ras_status)
1598 static int psp_hdcp_initialize(struct psp_context *psp)
1603 * TODO: bypass the initialize in sriov for now
1605 if (amdgpu_sriov_vf(psp->adev))
1608 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1609 !psp->hdcp_context.context.bin_desc.start_addr) {
1610 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1614 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1615 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1617 if (!psp->hdcp_context.context.initialized) {
1618 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1623 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1625 psp->hdcp_context.context.initialized = true;
1626 mutex_init(&psp->hdcp_context.mutex);
1632 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1635 * TODO: bypass the loading in sriov for now
1637 if (amdgpu_sriov_vf(psp->adev))
1640 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1643 static int psp_hdcp_terminate(struct psp_context *psp)
1648 * TODO: bypass the terminate in sriov for now
1650 if (amdgpu_sriov_vf(psp->adev))
1653 if (!psp->hdcp_context.context.initialized) {
1654 if (psp->hdcp_context.context.mem_context.shared_buf) {
1662 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1664 psp->hdcp_context.context.initialized = false;
1667 /* free hdcp shared memory */
1668 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
1675 static int psp_dtm_initialize(struct psp_context *psp)
1680 * TODO: bypass the initialize in sriov for now
1682 if (amdgpu_sriov_vf(psp->adev))
1685 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1686 !psp->dtm_context.context.bin_desc.start_addr) {
1687 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1691 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1692 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1694 if (!psp->dtm_context.context.initialized) {
1695 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1700 ret = psp_ta_load(psp, &psp->dtm_context.context);
1702 psp->dtm_context.context.initialized = true;
1703 mutex_init(&psp->dtm_context.mutex);
1709 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1712 * TODO: bypass the loading in sriov for now
1714 if (amdgpu_sriov_vf(psp->adev))
1717 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1720 static int psp_dtm_terminate(struct psp_context *psp)
1725 * TODO: bypass the terminate in sriov for now
1727 if (amdgpu_sriov_vf(psp->adev))
1730 if (!psp->dtm_context.context.initialized) {
1731 if (psp->dtm_context.context.mem_context.shared_buf) {
1739 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1741 psp->dtm_context.context.initialized = false;
1744 /* free dtm shared memory */
1745 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
1752 static int psp_rap_initialize(struct psp_context *psp)
1755 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1758 * TODO: bypass the initialize in sriov for now
1760 if (amdgpu_sriov_vf(psp->adev))
1763 if (!psp->rap_context.context.bin_desc.size_bytes ||
1764 !psp->rap_context.context.bin_desc.start_addr) {
1765 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1769 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1770 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1772 if (!psp->rap_context.context.initialized) {
1773 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1778 ret = psp_ta_load(psp, &psp->rap_context.context);
1780 psp->rap_context.context.initialized = true;
1781 mutex_init(&psp->rap_context.mutex);
1785 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1786 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1787 psp_rap_terminate(psp);
1789 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1798 static int psp_rap_terminate(struct psp_context *psp)
1802 if (!psp->rap_context.context.initialized)
1805 ret = psp_ta_unload(psp, &psp->rap_context.context);
1807 psp->rap_context.context.initialized = false;
1809 /* free rap shared memory */
1810 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1815 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1817 struct ta_rap_shared_memory *rap_cmd;
1820 if (!psp->rap_context.context.initialized)
1823 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1824 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1827 mutex_lock(&psp->rap_context.mutex);
1829 rap_cmd = (struct ta_rap_shared_memory *)
1830 psp->rap_context.context.mem_context.shared_buf;
1831 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1833 rap_cmd->cmd_id = ta_cmd_id;
1834 rap_cmd->validation_method_id = METHOD_A;
1836 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1841 *status = rap_cmd->rap_status;
1844 mutex_unlock(&psp->rap_context.mutex);
1850 /* securedisplay start */
1851 static int psp_securedisplay_initialize(struct psp_context *psp)
1854 struct securedisplay_cmd *securedisplay_cmd;
1857 * TODO: bypass the initialize in sriov for now
1859 if (amdgpu_sriov_vf(psp->adev))
1862 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1863 !psp->securedisplay_context.context.bin_desc.start_addr) {
1864 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1868 psp->securedisplay_context.context.mem_context.shared_mem_size =
1869 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1870 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1872 if (!psp->securedisplay_context.context.initialized) {
1873 ret = psp_ta_init_shared_buf(psp,
1874 &psp->securedisplay_context.context.mem_context);
1879 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1881 psp->securedisplay_context.context.initialized = true;
1882 mutex_init(&psp->securedisplay_context.mutex);
1886 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1887 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1889 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1891 psp_securedisplay_terminate(psp);
1892 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1896 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1897 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1898 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1899 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1905 static int psp_securedisplay_terminate(struct psp_context *psp)
1910 * TODO:bypass the terminate in sriov for now
1912 if (amdgpu_sriov_vf(psp->adev))
1915 if (!psp->securedisplay_context.context.initialized)
1918 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1920 psp->securedisplay_context.context.initialized = false;
1922 /* free securedisplay shared memory */
1923 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1928 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1932 if (!psp->securedisplay_context.context.initialized)
1935 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1936 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1939 mutex_lock(&psp->securedisplay_context.mutex);
1941 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1943 mutex_unlock(&psp->securedisplay_context.mutex);
1947 /* SECUREDISPLAY end */
1949 static int psp_hw_start(struct psp_context *psp)
1951 struct amdgpu_device *adev = psp->adev;
1954 if (!amdgpu_sriov_vf(adev)) {
1955 if ((is_psp_fw_valid(psp->kdb)) &&
1956 (psp->funcs->bootloader_load_kdb != NULL)) {
1957 ret = psp_bootloader_load_kdb(psp);
1959 DRM_ERROR("PSP load kdb failed!\n");
1964 if ((is_psp_fw_valid(psp->spl)) &&
1965 (psp->funcs->bootloader_load_spl != NULL)) {
1966 ret = psp_bootloader_load_spl(psp);
1968 DRM_ERROR("PSP load spl failed!\n");
1973 if ((is_psp_fw_valid(psp->sys)) &&
1974 (psp->funcs->bootloader_load_sysdrv != NULL)) {
1975 ret = psp_bootloader_load_sysdrv(psp);
1977 DRM_ERROR("PSP load sys drv failed!\n");
1982 if ((is_psp_fw_valid(psp->soc_drv)) &&
1983 (psp->funcs->bootloader_load_soc_drv != NULL)) {
1984 ret = psp_bootloader_load_soc_drv(psp);
1986 DRM_ERROR("PSP load soc drv failed!\n");
1991 if ((is_psp_fw_valid(psp->intf_drv)) &&
1992 (psp->funcs->bootloader_load_intf_drv != NULL)) {
1993 ret = psp_bootloader_load_intf_drv(psp);
1995 DRM_ERROR("PSP load intf drv failed!\n");
2000 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2001 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2002 ret = psp_bootloader_load_dbg_drv(psp);
2004 DRM_ERROR("PSP load dbg drv failed!\n");
2009 if ((is_psp_fw_valid(psp->sos)) &&
2010 (psp->funcs->bootloader_load_sos != NULL)) {
2011 ret = psp_bootloader_load_sos(psp);
2013 DRM_ERROR("PSP load sos failed!\n");
2019 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2021 DRM_ERROR("PSP create ring failed!\n");
2025 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2028 ret = psp_tmr_init(psp);
2030 DRM_ERROR("PSP tmr init failed!\n");
2036 * For ASICs with DF Cstate management centralized
2037 * to PMFW, TMR setup should be performed after PMFW
2038 * loaded and before other non-psp firmware loaded.
2040 if (psp->pmfw_centralized_cstate_management) {
2041 ret = psp_load_smu_fw(psp);
2046 ret = psp_tmr_load(psp);
2048 DRM_ERROR("PSP load tmr failed!\n");
2055 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2056 enum psp_gfx_fw_type *type)
2058 switch (ucode->ucode_id) {
2059 case AMDGPU_UCODE_ID_CAP:
2060 *type = GFX_FW_TYPE_CAP;
2062 case AMDGPU_UCODE_ID_SDMA0:
2063 *type = GFX_FW_TYPE_SDMA0;
2065 case AMDGPU_UCODE_ID_SDMA1:
2066 *type = GFX_FW_TYPE_SDMA1;
2068 case AMDGPU_UCODE_ID_SDMA2:
2069 *type = GFX_FW_TYPE_SDMA2;
2071 case AMDGPU_UCODE_ID_SDMA3:
2072 *type = GFX_FW_TYPE_SDMA3;
2074 case AMDGPU_UCODE_ID_SDMA4:
2075 *type = GFX_FW_TYPE_SDMA4;
2077 case AMDGPU_UCODE_ID_SDMA5:
2078 *type = GFX_FW_TYPE_SDMA5;
2080 case AMDGPU_UCODE_ID_SDMA6:
2081 *type = GFX_FW_TYPE_SDMA6;
2083 case AMDGPU_UCODE_ID_SDMA7:
2084 *type = GFX_FW_TYPE_SDMA7;
2086 case AMDGPU_UCODE_ID_CP_MES:
2087 *type = GFX_FW_TYPE_CP_MES;
2089 case AMDGPU_UCODE_ID_CP_MES_DATA:
2090 *type = GFX_FW_TYPE_MES_STACK;
2092 case AMDGPU_UCODE_ID_CP_CE:
2093 *type = GFX_FW_TYPE_CP_CE;
2095 case AMDGPU_UCODE_ID_CP_PFP:
2096 *type = GFX_FW_TYPE_CP_PFP;
2098 case AMDGPU_UCODE_ID_CP_ME:
2099 *type = GFX_FW_TYPE_CP_ME;
2101 case AMDGPU_UCODE_ID_CP_MEC1:
2102 *type = GFX_FW_TYPE_CP_MEC;
2104 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2105 *type = GFX_FW_TYPE_CP_MEC_ME1;
2107 case AMDGPU_UCODE_ID_CP_MEC2:
2108 *type = GFX_FW_TYPE_CP_MEC;
2110 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2111 *type = GFX_FW_TYPE_CP_MEC_ME2;
2113 case AMDGPU_UCODE_ID_RLC_G:
2114 *type = GFX_FW_TYPE_RLC_G;
2116 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2117 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2119 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2120 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2122 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2123 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2125 case AMDGPU_UCODE_ID_RLC_IRAM:
2126 *type = GFX_FW_TYPE_RLC_IRAM;
2128 case AMDGPU_UCODE_ID_RLC_DRAM:
2129 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2131 case AMDGPU_UCODE_ID_SMC:
2132 *type = GFX_FW_TYPE_SMU;
2134 case AMDGPU_UCODE_ID_UVD:
2135 *type = GFX_FW_TYPE_UVD;
2137 case AMDGPU_UCODE_ID_UVD1:
2138 *type = GFX_FW_TYPE_UVD1;
2140 case AMDGPU_UCODE_ID_VCE:
2141 *type = GFX_FW_TYPE_VCE;
2143 case AMDGPU_UCODE_ID_VCN:
2144 *type = GFX_FW_TYPE_VCN;
2146 case AMDGPU_UCODE_ID_VCN1:
2147 *type = GFX_FW_TYPE_VCN1;
2149 case AMDGPU_UCODE_ID_DMCU_ERAM:
2150 *type = GFX_FW_TYPE_DMCU_ERAM;
2152 case AMDGPU_UCODE_ID_DMCU_INTV:
2153 *type = GFX_FW_TYPE_DMCU_ISR;
2155 case AMDGPU_UCODE_ID_VCN0_RAM:
2156 *type = GFX_FW_TYPE_VCN0_RAM;
2158 case AMDGPU_UCODE_ID_VCN1_RAM:
2159 *type = GFX_FW_TYPE_VCN1_RAM;
2161 case AMDGPU_UCODE_ID_DMCUB:
2162 *type = GFX_FW_TYPE_DMUB;
2164 case AMDGPU_UCODE_ID_MAXIMUM:
2172 static void psp_print_fw_hdr(struct psp_context *psp,
2173 struct amdgpu_firmware_info *ucode)
2175 struct amdgpu_device *adev = psp->adev;
2176 struct common_firmware_header *hdr;
2178 switch (ucode->ucode_id) {
2179 case AMDGPU_UCODE_ID_SDMA0:
2180 case AMDGPU_UCODE_ID_SDMA1:
2181 case AMDGPU_UCODE_ID_SDMA2:
2182 case AMDGPU_UCODE_ID_SDMA3:
2183 case AMDGPU_UCODE_ID_SDMA4:
2184 case AMDGPU_UCODE_ID_SDMA5:
2185 case AMDGPU_UCODE_ID_SDMA6:
2186 case AMDGPU_UCODE_ID_SDMA7:
2187 hdr = (struct common_firmware_header *)
2188 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2189 amdgpu_ucode_print_sdma_hdr(hdr);
2191 case AMDGPU_UCODE_ID_CP_CE:
2192 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2193 amdgpu_ucode_print_gfx_hdr(hdr);
2195 case AMDGPU_UCODE_ID_CP_PFP:
2196 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2197 amdgpu_ucode_print_gfx_hdr(hdr);
2199 case AMDGPU_UCODE_ID_CP_ME:
2200 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2201 amdgpu_ucode_print_gfx_hdr(hdr);
2203 case AMDGPU_UCODE_ID_CP_MEC1:
2204 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2205 amdgpu_ucode_print_gfx_hdr(hdr);
2207 case AMDGPU_UCODE_ID_RLC_G:
2208 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2209 amdgpu_ucode_print_rlc_hdr(hdr);
2211 case AMDGPU_UCODE_ID_SMC:
2212 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2213 amdgpu_ucode_print_smc_hdr(hdr);
2220 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2221 struct psp_gfx_cmd_resp *cmd)
2224 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2226 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2227 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2228 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2229 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2231 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2233 DRM_ERROR("Unknown firmware type\n");
2238 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2239 struct amdgpu_firmware_info *ucode)
2242 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2244 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2246 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2247 psp->fence_buf_mc_addr);
2250 release_psp_cmd_buf(psp);
2255 static int psp_load_smu_fw(struct psp_context *psp)
2258 struct amdgpu_device *adev = psp->adev;
2259 struct amdgpu_firmware_info *ucode =
2260 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2261 struct amdgpu_ras *ras = psp->ras_context.ras;
2263 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2266 if ((amdgpu_in_reset(adev) &&
2267 ras && adev->ras_enabled &&
2268 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2269 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2270 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2272 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2276 ret = psp_execute_non_psp_fw_load(psp, ucode);
2279 DRM_ERROR("PSP load smu failed!\n");
2284 static bool fw_load_skip_check(struct psp_context *psp,
2285 struct amdgpu_firmware_info *ucode)
2290 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2291 (psp_smu_reload_quirk(psp) ||
2292 psp->autoload_supported ||
2293 psp->pmfw_centralized_cstate_management))
2296 if (amdgpu_sriov_vf(psp->adev) &&
2297 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
2298 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
2299 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2
2300 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3
2301 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA4
2302 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA5
2303 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA6
2304 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA7
2305 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G
2306 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
2307 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
2308 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
2309 || ucode->ucode_id == AMDGPU_UCODE_ID_SMC))
2310 /*skip ucode loading in SRIOV VF */
2313 if (psp->autoload_supported &&
2314 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2315 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2316 /* skip mec JT when autoload is enabled */
2322 int psp_load_fw_list(struct psp_context *psp,
2323 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2326 struct amdgpu_firmware_info *ucode;
2328 for (i = 0; i < ucode_count; ++i) {
2329 ucode = ucode_list[i];
2330 psp_print_fw_hdr(psp, ucode);
2331 ret = psp_execute_non_psp_fw_load(psp, ucode);
2338 static int psp_load_non_psp_fw(struct psp_context *psp)
2341 struct amdgpu_firmware_info *ucode;
2342 struct amdgpu_device *adev = psp->adev;
2344 if (psp->autoload_supported &&
2345 !psp->pmfw_centralized_cstate_management) {
2346 ret = psp_load_smu_fw(psp);
2351 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2352 ucode = &adev->firmware.ucode[i];
2354 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2355 !fw_load_skip_check(psp, ucode)) {
2356 ret = psp_load_smu_fw(psp);
2362 if (fw_load_skip_check(psp, ucode))
2365 if (psp->autoload_supported &&
2366 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2367 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2368 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2369 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2370 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2371 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2372 /* PSP only receive one SDMA fw for sienna_cichlid,
2373 * as all four sdma fw are same */
2376 psp_print_fw_hdr(psp, ucode);
2378 ret = psp_execute_non_psp_fw_load(psp, ucode);
2382 /* Start rlc autoload after psp recieved all the gfx firmware */
2383 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2384 AMDGPU_UCODE_ID_CP_MEC2 : AMDGPU_UCODE_ID_RLC_G)) {
2385 ret = psp_rlc_autoload_start(psp);
2387 DRM_ERROR("Failed to start rlc autoload\n");
2396 static int psp_load_fw(struct amdgpu_device *adev)
2399 struct psp_context *psp = &adev->psp;
2401 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2402 /* should not destroy ring, only stop */
2403 psp_ring_stop(psp, PSP_RING_TYPE__KM);
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");
2434 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2435 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2436 ret = psp_xgmi_initialize(psp, false, true);
2437 /* Warning the XGMI seesion initialize failure
2438 * Instead of stop driver initialization
2441 dev_err(psp->adev->dev,
2442 "XGMI: Failed to initialize XGMI session\n");
2447 ret = psp_ras_initialize(psp);
2449 dev_err(psp->adev->dev,
2450 "RAS: Failed to initialize RAS\n");
2452 ret = psp_hdcp_initialize(psp);
2454 dev_err(psp->adev->dev,
2455 "HDCP: Failed to initialize HDCP\n");
2457 ret = psp_dtm_initialize(psp);
2459 dev_err(psp->adev->dev,
2460 "DTM: Failed to initialize DTM\n");
2462 ret = psp_rap_initialize(psp);
2464 dev_err(psp->adev->dev,
2465 "RAP: Failed to initialize RAP\n");
2467 ret = psp_securedisplay_initialize(psp);
2469 dev_err(psp->adev->dev,
2470 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2477 * all cleanup jobs (xgmi terminate, ras terminate,
2478 * ring destroy, cmd/fence/fw buffers destory,
2479 * psp->cmd destory) are delayed to psp_hw_fini
2484 static int psp_hw_init(void *handle)
2487 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2489 mutex_lock(&adev->firmware.mutex);
2491 * This sequence is just used on hw_init only once, no need on
2494 ret = amdgpu_ucode_init_bo(adev);
2498 ret = psp_load_fw(adev);
2500 DRM_ERROR("PSP firmware loading failed\n");
2504 mutex_unlock(&adev->firmware.mutex);
2508 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2509 mutex_unlock(&adev->firmware.mutex);
2513 static int psp_hw_fini(void *handle)
2515 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2516 struct psp_context *psp = &adev->psp;
2519 psp_ras_terminate(psp);
2520 psp_securedisplay_terminate(psp);
2521 psp_rap_terminate(psp);
2522 psp_dtm_terminate(psp);
2523 psp_hdcp_terminate(psp);
2526 psp_asd_terminate(psp);
2528 psp_tmr_terminate(psp);
2529 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2534 static int psp_suspend(void *handle)
2537 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2538 struct psp_context *psp = &adev->psp;
2540 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2541 psp->xgmi_context.context.initialized) {
2542 ret = psp_xgmi_terminate(psp);
2544 DRM_ERROR("Failed to terminate xgmi ta\n");
2550 ret = psp_ras_terminate(psp);
2552 DRM_ERROR("Failed to terminate ras ta\n");
2555 ret = psp_hdcp_terminate(psp);
2557 DRM_ERROR("Failed to terminate hdcp ta\n");
2560 ret = psp_dtm_terminate(psp);
2562 DRM_ERROR("Failed to terminate dtm ta\n");
2565 ret = psp_rap_terminate(psp);
2567 DRM_ERROR("Failed to terminate rap ta\n");
2570 ret = psp_securedisplay_terminate(psp);
2572 DRM_ERROR("Failed to terminate securedisplay ta\n");
2577 ret = psp_asd_terminate(psp);
2579 DRM_ERROR("Failed to terminate asd\n");
2583 ret = psp_tmr_terminate(psp);
2585 DRM_ERROR("Failed to terminate tmr\n");
2589 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2591 DRM_ERROR("PSP ring stop failed\n");
2598 static int psp_resume(void *handle)
2601 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2602 struct psp_context *psp = &adev->psp;
2604 DRM_INFO("PSP is resuming...\n");
2606 if (psp->mem_train_ctx.enable_mem_training) {
2607 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2609 DRM_ERROR("Failed to process memory training!\n");
2614 mutex_lock(&adev->firmware.mutex);
2616 ret = psp_hw_start(psp);
2620 ret = psp_load_non_psp_fw(psp);
2624 ret = psp_asd_initialize(psp);
2626 DRM_ERROR("PSP load asd failed!\n");
2630 ret = psp_rl_load(adev);
2632 dev_err(adev->dev, "PSP load RL failed!\n");
2636 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2637 ret = psp_xgmi_initialize(psp, false, true);
2638 /* Warning the XGMI seesion initialize failure
2639 * Instead of stop driver initialization
2642 dev_err(psp->adev->dev,
2643 "XGMI: Failed to initialize XGMI session\n");
2647 ret = psp_ras_initialize(psp);
2649 dev_err(psp->adev->dev,
2650 "RAS: Failed to initialize RAS\n");
2652 ret = psp_hdcp_initialize(psp);
2654 dev_err(psp->adev->dev,
2655 "HDCP: Failed to initialize HDCP\n");
2657 ret = psp_dtm_initialize(psp);
2659 dev_err(psp->adev->dev,
2660 "DTM: Failed to initialize DTM\n");
2662 ret = psp_rap_initialize(psp);
2664 dev_err(psp->adev->dev,
2665 "RAP: Failed to initialize RAP\n");
2667 ret = psp_securedisplay_initialize(psp);
2669 dev_err(psp->adev->dev,
2670 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2673 mutex_unlock(&adev->firmware.mutex);
2678 DRM_ERROR("PSP resume failed\n");
2679 mutex_unlock(&adev->firmware.mutex);
2683 int psp_gpu_reset(struct amdgpu_device *adev)
2687 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2690 mutex_lock(&adev->psp.mutex);
2691 ret = psp_mode1_reset(&adev->psp);
2692 mutex_unlock(&adev->psp.mutex);
2697 int psp_rlc_autoload_start(struct psp_context *psp)
2700 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2702 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2704 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2705 psp->fence_buf_mc_addr);
2707 release_psp_cmd_buf(psp);
2712 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2713 uint64_t cmd_gpu_addr, int cmd_size)
2715 struct amdgpu_firmware_info ucode = {0};
2717 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2718 AMDGPU_UCODE_ID_VCN0_RAM;
2719 ucode.mc_addr = cmd_gpu_addr;
2720 ucode.ucode_size = cmd_size;
2722 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2725 int psp_ring_cmd_submit(struct psp_context *psp,
2726 uint64_t cmd_buf_mc_addr,
2727 uint64_t fence_mc_addr,
2730 unsigned int psp_write_ptr_reg = 0;
2731 struct psp_gfx_rb_frame *write_frame;
2732 struct psp_ring *ring = &psp->km_ring;
2733 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2734 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2735 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2736 struct amdgpu_device *adev = psp->adev;
2737 uint32_t ring_size_dw = ring->ring_size / 4;
2738 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2740 /* KM (GPCOM) prepare write pointer */
2741 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2743 /* Update KM RB frame pointer to new frame */
2744 /* write_frame ptr increments by size of rb_frame in bytes */
2745 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2746 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2747 write_frame = ring_buffer_start;
2749 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2750 /* Check invalid write_frame ptr address */
2751 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2752 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2753 ring_buffer_start, ring_buffer_end, write_frame);
2754 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2758 /* Initialize KM RB frame */
2759 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2761 /* Update KM RB frame */
2762 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2763 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2764 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2765 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2766 write_frame->fence_value = index;
2767 amdgpu_device_flush_hdp(adev, NULL);
2769 /* Update the write Pointer in DWORDs */
2770 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2771 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2775 int psp_init_asd_microcode(struct psp_context *psp,
2776 const char *chip_name)
2778 struct amdgpu_device *adev = psp->adev;
2779 char fw_name[PSP_FW_NAME_LEN];
2780 const struct psp_firmware_header_v1_0 *asd_hdr;
2784 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2788 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2789 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2793 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2797 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2798 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2799 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2800 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2801 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2802 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2805 dev_err(adev->dev, "fail to initialize asd microcode\n");
2806 release_firmware(adev->psp.asd_fw);
2807 adev->psp.asd_fw = NULL;
2811 int psp_init_toc_microcode(struct psp_context *psp,
2812 const char *chip_name)
2814 struct amdgpu_device *adev = psp->adev;
2815 char fw_name[PSP_FW_NAME_LEN];
2816 const struct psp_firmware_header_v1_0 *toc_hdr;
2820 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2824 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2825 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2829 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2833 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2834 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2835 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2836 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2837 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2838 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2841 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2842 release_firmware(adev->psp.toc_fw);
2843 adev->psp.toc_fw = NULL;
2847 static int parse_sos_bin_descriptor(struct psp_context *psp,
2848 const struct psp_fw_bin_desc *desc,
2849 const struct psp_firmware_header_v2_0 *sos_hdr)
2851 uint8_t *ucode_start_addr = NULL;
2853 if (!psp || !desc || !sos_hdr)
2856 ucode_start_addr = (uint8_t *)sos_hdr +
2857 le32_to_cpu(desc->offset_bytes) +
2858 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2860 switch (desc->fw_type) {
2861 case PSP_FW_TYPE_PSP_SOS:
2862 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
2863 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
2864 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
2865 psp->sos.start_addr = ucode_start_addr;
2867 case PSP_FW_TYPE_PSP_SYS_DRV:
2868 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
2869 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
2870 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
2871 psp->sys.start_addr = ucode_start_addr;
2873 case PSP_FW_TYPE_PSP_KDB:
2874 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
2875 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
2876 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
2877 psp->kdb.start_addr = ucode_start_addr;
2879 case PSP_FW_TYPE_PSP_TOC:
2880 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
2881 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
2882 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
2883 psp->toc.start_addr = ucode_start_addr;
2885 case PSP_FW_TYPE_PSP_SPL:
2886 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
2887 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
2888 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
2889 psp->spl.start_addr = ucode_start_addr;
2891 case PSP_FW_TYPE_PSP_RL:
2892 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
2893 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
2894 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
2895 psp->rl.start_addr = ucode_start_addr;
2897 case PSP_FW_TYPE_PSP_SOC_DRV:
2898 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
2899 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
2900 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2901 psp->soc_drv.start_addr = ucode_start_addr;
2903 case PSP_FW_TYPE_PSP_INTF_DRV:
2904 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
2905 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
2906 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2907 psp->intf_drv.start_addr = ucode_start_addr;
2909 case PSP_FW_TYPE_PSP_DBG_DRV:
2910 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
2911 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
2912 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2913 psp->dbg_drv.start_addr = ucode_start_addr;
2916 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
2923 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
2925 const struct psp_firmware_header_v1_0 *sos_hdr;
2926 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
2927 uint8_t *ucode_array_start_addr;
2929 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
2930 ucode_array_start_addr = (uint8_t *)sos_hdr +
2931 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2933 if (adev->gmc.xgmi.connected_to_cpu ||
2934 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
2935 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
2936 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
2938 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
2939 adev->psp.sys.start_addr = ucode_array_start_addr;
2941 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
2942 adev->psp.sos.start_addr = ucode_array_start_addr +
2943 le32_to_cpu(sos_hdr->sos.offset_bytes);
2945 /* Load alternate PSP SOS FW */
2946 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
2948 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
2949 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
2951 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
2952 adev->psp.sys.start_addr = ucode_array_start_addr +
2953 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
2955 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
2956 adev->psp.sos.start_addr = ucode_array_start_addr +
2957 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
2960 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
2961 dev_warn(adev->dev, "PSP SOS FW not available");
2968 int psp_init_sos_microcode(struct psp_context *psp,
2969 const char *chip_name)
2971 struct amdgpu_device *adev = psp->adev;
2972 char fw_name[PSP_FW_NAME_LEN];
2973 const struct psp_firmware_header_v1_0 *sos_hdr;
2974 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
2975 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
2976 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
2977 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
2979 uint8_t *ucode_array_start_addr;
2983 dev_err(adev->dev, "invalid chip name for sos microcode\n");
2987 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
2988 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
2992 err = amdgpu_ucode_validate(adev->psp.sos_fw);
2996 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
2997 ucode_array_start_addr = (uint8_t *)sos_hdr +
2998 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2999 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3001 switch (sos_hdr->header.header_version_major) {
3003 err = psp_init_sos_base_fw(adev);
3007 if (sos_hdr->header.header_version_minor == 1) {
3008 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3009 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3010 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3011 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3012 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3013 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3014 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3016 if (sos_hdr->header.header_version_minor == 2) {
3017 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3018 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3019 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3020 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3022 if (sos_hdr->header.header_version_minor == 3) {
3023 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3024 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3025 adev->psp.toc.start_addr = ucode_array_start_addr +
3026 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3027 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3028 adev->psp.kdb.start_addr = ucode_array_start_addr +
3029 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3030 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3031 adev->psp.spl.start_addr = ucode_array_start_addr +
3032 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3033 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3034 adev->psp.rl.start_addr = ucode_array_start_addr +
3035 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3039 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3041 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3042 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3047 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3048 err = parse_sos_bin_descriptor(psp,
3049 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3057 "unsupported psp sos firmware\n");
3065 "failed to init sos firmware\n");
3066 release_firmware(adev->psp.sos_fw);
3067 adev->psp.sos_fw = NULL;
3072 static int parse_ta_bin_descriptor(struct psp_context *psp,
3073 const struct psp_fw_bin_desc *desc,
3074 const struct ta_firmware_header_v2_0 *ta_hdr)
3076 uint8_t *ucode_start_addr = NULL;
3078 if (!psp || !desc || !ta_hdr)
3081 ucode_start_addr = (uint8_t *)ta_hdr +
3082 le32_to_cpu(desc->offset_bytes) +
3083 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3085 switch (desc->fw_type) {
3086 case TA_FW_TYPE_PSP_ASD:
3087 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3088 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3089 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3090 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3092 case TA_FW_TYPE_PSP_XGMI:
3093 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3094 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3095 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3097 case TA_FW_TYPE_PSP_RAS:
3098 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3099 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3100 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3102 case TA_FW_TYPE_PSP_HDCP:
3103 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3104 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3105 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3107 case TA_FW_TYPE_PSP_DTM:
3108 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3109 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3110 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3112 case TA_FW_TYPE_PSP_RAP:
3113 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3114 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3115 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3117 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3118 psp->securedisplay_context.context.bin_desc.fw_version =
3119 le32_to_cpu(desc->fw_version);
3120 psp->securedisplay_context.context.bin_desc.size_bytes =
3121 le32_to_cpu(desc->size_bytes);
3122 psp->securedisplay_context.context.bin_desc.start_addr =
3126 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3133 int psp_init_ta_microcode(struct psp_context *psp,
3134 const char *chip_name)
3136 struct amdgpu_device *adev = psp->adev;
3137 char fw_name[PSP_FW_NAME_LEN];
3138 const struct ta_firmware_header_v2_0 *ta_hdr;
3143 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3147 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3148 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3152 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3156 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3158 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3159 dev_err(adev->dev, "unsupported TA header version\n");
3164 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3165 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3170 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3171 err = parse_ta_bin_descriptor(psp,
3172 &ta_hdr->ta_fw_bin[ta_index],
3180 dev_err(adev->dev, "fail to initialize ta microcode\n");
3181 release_firmware(adev->psp.ta_fw);
3182 adev->psp.ta_fw = NULL;
3186 int psp_init_cap_microcode(struct psp_context *psp,
3187 const char *chip_name)
3189 struct amdgpu_device *adev = psp->adev;
3190 char fw_name[PSP_FW_NAME_LEN];
3191 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3192 struct amdgpu_firmware_info *info = NULL;
3196 dev_err(adev->dev, "invalid chip name for cap microcode\n");
3200 if (!amdgpu_sriov_vf(adev)) {
3201 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3205 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3206 err = request_firmware(&adev->psp.cap_fw, fw_name, adev->dev);
3208 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3213 err = amdgpu_ucode_validate(adev->psp.cap_fw);
3215 dev_err(adev->dev, "fail to initialize cap microcode\n");
3219 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3220 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3221 info->fw = adev->psp.cap_fw;
3222 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3223 adev->psp.cap_fw->data;
3224 adev->firmware.fw_size += ALIGN(
3225 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3226 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3227 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3228 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3233 release_firmware(adev->psp.cap_fw);
3234 adev->psp.cap_fw = NULL;
3238 static int psp_set_clockgating_state(void *handle,
3239 enum amd_clockgating_state state)
3244 static int psp_set_powergating_state(void *handle,
3245 enum amd_powergating_state state)
3250 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3251 struct device_attribute *attr,
3254 struct drm_device *ddev = dev_get_drvdata(dev);
3255 struct amdgpu_device *adev = drm_to_adev(ddev);
3259 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3260 DRM_INFO("PSP block is not ready yet.");
3264 mutex_lock(&adev->psp.mutex);
3265 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3266 mutex_unlock(&adev->psp.mutex);
3269 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3273 return sysfs_emit(buf, "%x\n", fw_ver);
3276 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3277 struct device_attribute *attr,
3281 struct drm_device *ddev = dev_get_drvdata(dev);
3282 struct amdgpu_device *adev = drm_to_adev(ddev);
3285 const struct firmware *usbc_pd_fw;
3286 struct amdgpu_bo *fw_buf_bo = NULL;
3287 uint64_t fw_pri_mc_addr;
3288 void *fw_pri_cpu_addr;
3290 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3291 DRM_INFO("PSP block is not ready yet.");
3295 if (!drm_dev_enter(ddev, &idx))
3298 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3299 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3303 /* LFB address which is aligned to 1MB boundary per PSP request */
3304 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3305 AMDGPU_GEM_DOMAIN_VRAM,
3312 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3314 mutex_lock(&adev->psp.mutex);
3315 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3316 mutex_unlock(&adev->psp.mutex);
3318 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3321 release_firmware(usbc_pd_fw);
3324 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3332 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3336 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3339 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3340 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3345 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3346 psp_usbc_pd_fw_sysfs_read,
3347 psp_usbc_pd_fw_sysfs_write);
3349 int is_psp_fw_valid(struct psp_bin_desc bin)
3351 return bin.size_bytes;
3354 const struct amd_ip_funcs psp_ip_funcs = {
3356 .early_init = psp_early_init,
3358 .sw_init = psp_sw_init,
3359 .sw_fini = psp_sw_fini,
3360 .hw_init = psp_hw_init,
3361 .hw_fini = psp_hw_fini,
3362 .suspend = psp_suspend,
3363 .resume = psp_resume,
3365 .check_soft_reset = NULL,
3366 .wait_for_idle = NULL,
3368 .set_clockgating_state = psp_set_clockgating_state,
3369 .set_powergating_state = psp_set_powergating_state,
3372 static int psp_sysfs_init(struct amdgpu_device *adev)
3374 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3377 DRM_ERROR("Failed to create USBC PD FW control file!");
3382 static void psp_sysfs_fini(struct amdgpu_device *adev)
3384 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3387 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3389 .type = AMD_IP_BLOCK_TYPE_PSP,
3393 .funcs = &psp_ip_funcs,
3396 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3398 .type = AMD_IP_BLOCK_TYPE_PSP,
3402 .funcs = &psp_ip_funcs,
3405 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3407 .type = AMD_IP_BLOCK_TYPE_PSP,
3411 .funcs = &psp_ip_funcs,
3414 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3415 .type = AMD_IP_BLOCK_TYPE_PSP,
3419 .funcs = &psp_ip_funcs,
3422 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3424 .type = AMD_IP_BLOCK_TYPE_PSP,
3428 .funcs = &psp_ip_funcs,
3431 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3432 .type = AMD_IP_BLOCK_TYPE_PSP,
3436 .funcs = &psp_ip_funcs,