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
69 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
71 struct amdgpu_device *adev = psp->adev;
73 if (amdgpu_sriov_vf(adev)) {
74 psp->pmfw_centralized_cstate_management = false;
78 switch (adev->ip_versions[MP0_HWIP][0]) {
79 case IP_VERSION(11, 0, 0):
80 case IP_VERSION(11, 0, 4):
81 case IP_VERSION(11, 0, 5):
82 case IP_VERSION(11, 0, 7):
83 case IP_VERSION(11, 0, 9):
84 case IP_VERSION(11, 0, 11):
85 case IP_VERSION(11, 0, 12):
86 case IP_VERSION(11, 0, 13):
87 case IP_VERSION(13, 0, 2):
88 psp->pmfw_centralized_cstate_management = true;
91 psp->pmfw_centralized_cstate_management = false;
96 static int psp_early_init(void *handle)
98 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
99 struct psp_context *psp = &adev->psp;
101 switch (adev->ip_versions[MP0_HWIP][0]) {
102 case IP_VERSION(9, 0, 0):
103 psp_v3_1_set_psp_funcs(psp);
104 psp->autoload_supported = false;
106 case IP_VERSION(10, 0, 0):
107 case IP_VERSION(10, 0, 1):
108 psp_v10_0_set_psp_funcs(psp);
109 psp->autoload_supported = false;
111 case IP_VERSION(11, 0, 2):
112 case IP_VERSION(11, 0, 4):
113 psp_v11_0_set_psp_funcs(psp);
114 psp->autoload_supported = false;
116 case IP_VERSION(11, 0, 0):
117 case IP_VERSION(11, 0, 5):
118 case IP_VERSION(11, 0, 9):
119 case IP_VERSION(11, 0, 7):
120 case IP_VERSION(11, 0, 11):
121 case IP_VERSION(11, 5, 0):
122 case IP_VERSION(11, 0, 12):
123 case IP_VERSION(11, 0, 13):
124 psp_v11_0_set_psp_funcs(psp);
125 psp->autoload_supported = true;
127 case IP_VERSION(11, 0, 3):
128 case IP_VERSION(12, 0, 1):
129 psp_v12_0_set_psp_funcs(psp);
131 case IP_VERSION(13, 0, 2):
132 psp_v13_0_set_psp_funcs(psp);
134 case IP_VERSION(13, 0, 1):
135 case IP_VERSION(13, 0, 3):
136 case IP_VERSION(13, 0, 5):
137 case IP_VERSION(13, 0, 8):
138 psp_v13_0_set_psp_funcs(psp);
139 psp->autoload_supported = true;
141 case IP_VERSION(11, 0, 8):
142 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
143 psp_v11_0_8_set_psp_funcs(psp);
144 psp->autoload_supported = false;
153 psp_check_pmfw_centralized_cstate_management(psp);
158 static void psp_memory_training_fini(struct psp_context *psp)
160 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
162 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
163 kfree(ctx->sys_cache);
164 ctx->sys_cache = NULL;
167 static int psp_memory_training_init(struct psp_context *psp)
170 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
172 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
173 DRM_DEBUG("memory training is not supported!\n");
177 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
178 if (ctx->sys_cache == NULL) {
179 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
184 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
185 ctx->train_data_size,
186 ctx->p2c_train_data_offset,
187 ctx->c2p_train_data_offset);
188 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
192 psp_memory_training_fini(psp);
197 * Helper funciton to query psp runtime database entry
199 * @adev: amdgpu_device pointer
200 * @entry_type: the type of psp runtime database entry
201 * @db_entry: runtime database entry pointer
203 * Return false if runtime database doesn't exit or entry is invalid
204 * or true if the specific database entry is found, and copy to @db_entry
206 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
207 enum psp_runtime_entry_type entry_type,
210 uint64_t db_header_pos, db_dir_pos;
211 struct psp_runtime_data_header db_header = {0};
212 struct psp_runtime_data_directory db_dir = {0};
216 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
217 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
219 /* read runtime db header from vram */
220 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
221 sizeof(struct psp_runtime_data_header), false);
223 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
224 /* runtime db doesn't exist, exit */
225 dev_warn(adev->dev, "PSP runtime database doesn't exist\n");
229 /* read runtime database entry from vram */
230 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
231 sizeof(struct psp_runtime_data_directory), false);
233 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
234 /* invalid db entry count, exit */
235 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
239 /* look up for requested entry type */
240 for (i = 0; i < db_dir.entry_count && !ret; i++) {
241 if (db_dir.entry_list[i].entry_type == entry_type) {
242 switch (entry_type) {
243 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
244 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
245 /* invalid db entry size */
246 dev_warn(adev->dev, "Invalid PSP runtime database entry size\n");
249 /* read runtime database entry */
250 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
251 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
264 static int psp_init_sriov_microcode(struct psp_context *psp)
266 struct amdgpu_device *adev = psp->adev;
269 switch (adev->ip_versions[MP0_HWIP][0]) {
270 case IP_VERSION(9, 0, 0):
271 ret = psp_init_cap_microcode(psp, "vega10");
273 case IP_VERSION(11, 0, 9):
274 ret = psp_init_cap_microcode(psp, "navi12");
276 case IP_VERSION(11, 0, 7):
277 ret = psp_init_cap_microcode(psp, "sienna_cichlid");
279 case IP_VERSION(13, 0, 2):
280 ret = psp_init_cap_microcode(psp, "aldebaran");
290 static int psp_sw_init(void *handle)
292 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
293 struct psp_context *psp = &adev->psp;
295 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
296 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
298 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
300 DRM_ERROR("Failed to allocate memory to command buffer!\n");
304 if (amdgpu_sriov_vf(adev))
305 ret = psp_init_sriov_microcode(psp);
307 ret = psp_init_microcode(psp);
309 DRM_ERROR("Failed to load psp firmware!\n");
313 adev->psp.xgmi_context.supports_extended_data =
314 !adev->gmc.xgmi.connected_to_cpu &&
315 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
317 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
318 if (psp_get_runtime_db_entry(adev,
319 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
321 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
322 if ((psp->boot_cfg_bitmask) &
323 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
324 /* If psp runtime database exists, then
325 * only enable two stage memory training
326 * when TWO_STAGE_DRAM_TRAINING bit is set
327 * in runtime database */
328 mem_training_ctx->enable_mem_training = true;
332 /* If psp runtime database doesn't exist or
333 * is invalid, force enable two stage memory
335 mem_training_ctx->enable_mem_training = true;
338 if (mem_training_ctx->enable_mem_training) {
339 ret = psp_memory_training_init(psp);
341 DRM_ERROR("Failed to initialize memory training!\n");
345 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
347 DRM_ERROR("Failed to process memory training!\n");
352 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
353 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
354 ret= psp_sysfs_init(adev);
363 static int psp_sw_fini(void *handle)
365 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
366 struct psp_context *psp = &adev->psp;
367 struct psp_gfx_cmd_resp *cmd = psp->cmd;
369 psp_memory_training_fini(psp);
371 release_firmware(psp->sos_fw);
375 release_firmware(psp->asd_fw);
379 release_firmware(psp->ta_fw);
382 if (adev->psp.cap_fw) {
383 release_firmware(psp->cap_fw);
387 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
388 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
389 psp_sysfs_fini(adev);
397 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
398 uint32_t reg_val, uint32_t mask, bool check_changed)
402 struct amdgpu_device *adev = psp->adev;
404 if (psp->adev->no_hw_access)
407 for (i = 0; i < adev->usec_timeout; i++) {
408 val = RREG32(reg_index);
413 if ((val & mask) == reg_val)
422 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
425 case GFX_CMD_ID_LOAD_TA:
427 case GFX_CMD_ID_UNLOAD_TA:
429 case GFX_CMD_ID_INVOKE_CMD:
431 case GFX_CMD_ID_LOAD_ASD:
433 case GFX_CMD_ID_SETUP_TMR:
435 case GFX_CMD_ID_LOAD_IP_FW:
437 case GFX_CMD_ID_DESTROY_TMR:
438 return "DESTROY_TMR";
439 case GFX_CMD_ID_SAVE_RESTORE:
440 return "SAVE_RESTORE_IP_FW";
441 case GFX_CMD_ID_SETUP_VMR:
443 case GFX_CMD_ID_DESTROY_VMR:
444 return "DESTROY_VMR";
445 case GFX_CMD_ID_PROG_REG:
447 case GFX_CMD_ID_GET_FW_ATTESTATION:
448 return "GET_FW_ATTESTATION";
449 case GFX_CMD_ID_LOAD_TOC:
450 return "ID_LOAD_TOC";
451 case GFX_CMD_ID_AUTOLOAD_RLC:
452 return "AUTOLOAD_RLC";
453 case GFX_CMD_ID_BOOT_CFG:
456 return "UNKNOWN CMD";
461 psp_cmd_submit_buf(struct psp_context *psp,
462 struct amdgpu_firmware_info *ucode,
463 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
468 bool ras_intr = false;
469 bool skip_unsupport = false;
471 if (psp->adev->no_hw_access)
474 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
477 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
479 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
481 index = atomic_inc_return(&psp->fence_value);
482 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
484 atomic_dec(&psp->fence_value);
488 amdgpu_device_invalidate_hdp(psp->adev, NULL);
489 while (*((unsigned int *)psp->fence_buf) != index) {
493 * Shouldn't wait for timeout when err_event_athub occurs,
494 * because gpu reset thread triggered and lock resource should
495 * be released for psp resume sequence.
497 ras_intr = amdgpu_ras_intr_triggered();
500 usleep_range(10, 100);
501 amdgpu_device_invalidate_hdp(psp->adev, NULL);
504 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
505 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
506 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
508 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
510 /* In some cases, psp response status is not 0 even there is no
511 * problem while the command is submitted. Some version of PSP FW
512 * doesn't write 0 to that field.
513 * So here we would like to only print a warning instead of an error
514 * during psp initialization to avoid breaking hw_init and it doesn't
517 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
519 DRM_WARN("failed to load ucode %s(0x%X) ",
520 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
521 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
522 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
523 psp->cmd_buf_mem->resp.status);
524 /* If we load CAP FW, PSP must return 0 under SRIOV
525 * also return failure in case of timeout
527 if ((ucode && (ucode->ucode_id == AMDGPU_UCODE_ID_CAP)) || !timeout) {
534 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
535 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
543 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
545 struct psp_gfx_cmd_resp *cmd = psp->cmd;
547 mutex_lock(&psp->mutex);
549 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
554 static void release_psp_cmd_buf(struct psp_context *psp)
556 mutex_unlock(&psp->mutex);
559 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
560 struct psp_gfx_cmd_resp *cmd,
561 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
563 struct amdgpu_device *adev = psp->adev;
564 uint32_t size = amdgpu_bo_size(tmr_bo);
565 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
567 if (amdgpu_sriov_vf(psp->adev))
568 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
570 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
571 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
572 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
573 cmd->cmd.cmd_setup_tmr.buf_size = size;
574 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
575 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
576 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
579 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
580 uint64_t pri_buf_mc, uint32_t size)
582 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
583 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
584 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
585 cmd->cmd.cmd_load_toc.toc_size = size;
588 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
589 static int psp_load_toc(struct psp_context *psp,
593 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
595 /* Copy toc to psp firmware private buffer */
596 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
598 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
600 ret = psp_cmd_submit_buf(psp, NULL, cmd,
601 psp->fence_buf_mc_addr);
603 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
605 release_psp_cmd_buf(psp);
610 /* Set up Trusted Memory Region */
611 static int psp_tmr_init(struct psp_context *psp)
619 * According to HW engineer, they prefer the TMR address be "naturally
620 * aligned" , e.g. the start address be an integer divide of TMR size.
622 * Note: this memory need be reserved till the driver
625 tmr_size = PSP_TMR_SIZE(psp->adev);
627 /* For ASICs support RLC autoload, psp will parse the toc
628 * and calculate the total size of TMR needed */
629 if (!amdgpu_sriov_vf(psp->adev) &&
630 psp->toc.start_addr &&
631 psp->toc.size_bytes &&
633 ret = psp_load_toc(psp, &tmr_size);
635 DRM_ERROR("Failed to load toc\n");
640 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
641 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_SIZE(psp->adev),
642 AMDGPU_GEM_DOMAIN_VRAM,
643 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
648 static bool psp_skip_tmr(struct psp_context *psp)
650 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
651 case IP_VERSION(11, 0, 9):
652 case IP_VERSION(11, 0, 7):
653 case IP_VERSION(13, 0, 2):
660 static int psp_tmr_load(struct psp_context *psp)
663 struct psp_gfx_cmd_resp *cmd;
665 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
666 * Already set up by host driver.
668 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
671 cmd = acquire_psp_cmd_buf(psp);
673 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
674 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
675 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
677 ret = psp_cmd_submit_buf(psp, NULL, cmd,
678 psp->fence_buf_mc_addr);
680 release_psp_cmd_buf(psp);
685 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
686 struct psp_gfx_cmd_resp *cmd)
688 if (amdgpu_sriov_vf(psp->adev))
689 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
691 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
694 static int psp_tmr_unload(struct psp_context *psp)
697 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
699 psp_prep_tmr_unload_cmd_buf(psp, cmd);
700 DRM_INFO("free PSP TMR buffer\n");
702 ret = psp_cmd_submit_buf(psp, NULL, cmd,
703 psp->fence_buf_mc_addr);
705 release_psp_cmd_buf(psp);
710 static int psp_tmr_terminate(struct psp_context *psp)
716 ret = psp_tmr_unload(psp);
720 /* free TMR memory buffer */
721 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
722 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
727 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
728 uint64_t *output_ptr)
731 struct psp_gfx_cmd_resp *cmd;
736 if (amdgpu_sriov_vf(psp->adev))
739 cmd = acquire_psp_cmd_buf(psp);
741 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
743 ret = psp_cmd_submit_buf(psp, NULL, cmd,
744 psp->fence_buf_mc_addr);
747 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
748 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
751 release_psp_cmd_buf(psp);
756 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
758 struct psp_context *psp = &adev->psp;
759 struct psp_gfx_cmd_resp *cmd;
762 if (amdgpu_sriov_vf(adev))
765 cmd = acquire_psp_cmd_buf(psp);
767 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
768 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
770 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
773 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
776 release_psp_cmd_buf(psp);
781 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
784 struct psp_context *psp = &adev->psp;
785 struct psp_gfx_cmd_resp *cmd;
787 if (amdgpu_sriov_vf(adev))
790 cmd = acquire_psp_cmd_buf(psp);
792 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
793 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
794 cmd->cmd.boot_cfg.boot_config = boot_cfg;
795 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
797 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
799 release_psp_cmd_buf(psp);
804 static int psp_rl_load(struct amdgpu_device *adev)
807 struct psp_context *psp = &adev->psp;
808 struct psp_gfx_cmd_resp *cmd;
810 if (!is_psp_fw_valid(psp->rl))
813 cmd = acquire_psp_cmd_buf(psp);
815 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
816 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
818 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
819 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
820 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
821 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
822 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
824 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
826 release_psp_cmd_buf(psp);
831 static int psp_asd_load(struct psp_context *psp)
833 return psp_ta_load(psp, &psp->asd_context);
836 static int psp_asd_initialize(struct psp_context *psp)
840 /* If PSP version doesn't match ASD version, asd loading will be failed.
841 * add workaround to bypass it for sriov now.
842 * TODO: add version check to make it common
844 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
847 psp->asd_context.mem_context.shared_mc_addr = 0;
848 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
849 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
851 ret = psp_asd_load(psp);
853 psp->asd_context.initialized = true;
858 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
861 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
862 cmd->cmd.cmd_unload_ta.session_id = session_id;
865 static int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
868 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
870 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
872 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
874 release_psp_cmd_buf(psp);
879 static int psp_asd_unload(struct psp_context *psp)
881 return psp_ta_unload(psp, &psp->asd_context);
884 static int psp_asd_terminate(struct psp_context *psp)
888 if (amdgpu_sriov_vf(psp->adev))
891 if (!psp->asd_context.initialized)
894 ret = psp_asd_unload(psp);
897 psp->asd_context.initialized = false;
902 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
903 uint32_t id, uint32_t value)
905 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
906 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
907 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
910 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
913 struct psp_gfx_cmd_resp *cmd;
916 if (reg >= PSP_REG_LAST)
919 cmd = acquire_psp_cmd_buf(psp);
921 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
922 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
924 DRM_ERROR("PSP failed to program reg id %d", reg);
926 release_psp_cmd_buf(psp);
931 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
933 struct ta_context *context)
935 cmd->cmd_id = context->ta_load_type;
936 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
937 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
938 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
940 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
941 lower_32_bits(context->mem_context.shared_mc_addr);
942 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
943 upper_32_bits(context->mem_context.shared_mc_addr);
944 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
947 static int psp_ta_init_shared_buf(struct psp_context *psp,
948 struct ta_mem_context *mem_ctx)
951 * Allocate 16k memory aligned to 4k from Frame Buffer (local
952 * physical) for ta to host memory
954 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
955 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
957 &mem_ctx->shared_mc_addr,
958 &mem_ctx->shared_buf);
961 static void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
963 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
964 &mem_ctx->shared_buf);
967 static int psp_xgmi_init_shared_buf(struct psp_context *psp)
969 return psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
972 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
976 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
977 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
978 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
981 static int psp_ta_invoke(struct psp_context *psp,
983 struct ta_context *context)
986 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
988 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
990 ret = psp_cmd_submit_buf(psp, NULL, cmd,
991 psp->fence_buf_mc_addr);
993 release_psp_cmd_buf(psp);
998 static int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1001 struct psp_gfx_cmd_resp *cmd;
1003 cmd = acquire_psp_cmd_buf(psp);
1005 psp_copy_fw(psp, context->bin_desc.start_addr,
1006 context->bin_desc.size_bytes);
1008 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1010 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1011 psp->fence_buf_mc_addr);
1014 context->session_id = cmd->resp.session_id;
1017 release_psp_cmd_buf(psp);
1022 static int psp_xgmi_load(struct psp_context *psp)
1024 return psp_ta_load(psp, &psp->xgmi_context.context);
1027 static int psp_xgmi_unload(struct psp_context *psp)
1029 return psp_ta_unload(psp, &psp->xgmi_context.context);
1032 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1034 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1037 int psp_xgmi_terminate(struct psp_context *psp)
1040 struct amdgpu_device *adev = psp->adev;
1042 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1043 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1044 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1045 adev->gmc.xgmi.connected_to_cpu))
1048 if (!psp->xgmi_context.context.initialized)
1051 ret = psp_xgmi_unload(psp);
1055 psp->xgmi_context.context.initialized = false;
1057 /* free xgmi shared memory */
1058 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
1063 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1065 struct ta_xgmi_shared_memory *xgmi_cmd;
1069 !psp->xgmi_context.context.bin_desc.size_bytes ||
1070 !psp->xgmi_context.context.bin_desc.start_addr)
1076 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1077 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1079 if (!psp->xgmi_context.context.initialized) {
1080 ret = psp_xgmi_init_shared_buf(psp);
1086 ret = psp_xgmi_load(psp);
1088 psp->xgmi_context.context.initialized = true;
1093 /* Initialize XGMI session */
1094 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1095 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1096 xgmi_cmd->flag_extend_link_record = set_extended_data;
1097 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1099 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1104 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1106 struct ta_xgmi_shared_memory *xgmi_cmd;
1109 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1110 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1112 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1114 /* Invoke xgmi ta to get hive id */
1115 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1119 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1124 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1126 struct ta_xgmi_shared_memory *xgmi_cmd;
1129 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1130 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1132 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1134 /* Invoke xgmi ta to get the node id */
1135 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1139 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1144 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1146 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1147 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1151 * Chips that support extended topology information require the driver to
1152 * reflect topology information in the opposite direction. This is
1153 * because the TA has already exceeded its link record limit and if the
1154 * TA holds bi-directional information, the driver would have to do
1155 * multiple fetches instead of just two.
1157 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1158 struct psp_xgmi_node_info node_info)
1160 struct amdgpu_device *mirror_adev;
1161 struct amdgpu_hive_info *hive;
1162 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1163 uint64_t dst_node_id = node_info.node_id;
1164 uint8_t dst_num_hops = node_info.num_hops;
1165 uint8_t dst_num_links = node_info.num_links;
1167 hive = amdgpu_get_xgmi_hive(psp->adev);
1168 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1169 struct psp_xgmi_topology_info *mirror_top_info;
1172 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1175 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1176 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1177 if (mirror_top_info->nodes[j].node_id != src_node_id)
1180 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1182 * prevent 0 num_links value re-reflection since reflection
1183 * criteria is based on num_hops (direct or indirect).
1187 mirror_top_info->nodes[j].num_links = dst_num_links;
1196 int psp_xgmi_get_topology_info(struct psp_context *psp,
1198 struct psp_xgmi_topology_info *topology,
1199 bool get_extended_data)
1201 struct ta_xgmi_shared_memory *xgmi_cmd;
1202 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1203 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1207 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1210 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1211 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1212 xgmi_cmd->flag_extend_link_record = get_extended_data;
1214 /* Fill in the shared memory with topology information as input */
1215 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1216 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1217 topology_info_input->num_nodes = number_devices;
1219 for (i = 0; i < topology_info_input->num_nodes; i++) {
1220 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1221 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1222 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1223 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1226 /* Invoke xgmi ta to get the topology information */
1227 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1231 /* Read the output topology information from the shared memory */
1232 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1233 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1234 for (i = 0; i < topology->num_nodes; i++) {
1235 /* extended data will either be 0 or equal to non-extended data */
1236 if (topology_info_output->nodes[i].num_hops)
1237 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1239 /* non-extended data gets everything here so no need to update */
1240 if (!get_extended_data) {
1241 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1242 topology->nodes[i].is_sharing_enabled =
1243 topology_info_output->nodes[i].is_sharing_enabled;
1244 topology->nodes[i].sdma_engine =
1245 topology_info_output->nodes[i].sdma_engine;
1250 /* Invoke xgmi ta again to get the link information */
1251 if (psp_xgmi_peer_link_info_supported(psp)) {
1252 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1254 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1256 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1261 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1262 for (i = 0; i < topology->num_nodes; i++) {
1263 /* accumulate num_links on extended data */
1264 topology->nodes[i].num_links = get_extended_data ?
1265 topology->nodes[i].num_links +
1266 link_info_output->nodes[i].num_links :
1267 link_info_output->nodes[i].num_links;
1269 /* reflect the topology information for bi-directionality */
1270 if (psp->xgmi_context.supports_extended_data &&
1271 get_extended_data && topology->nodes[i].num_hops)
1272 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1279 int psp_xgmi_set_topology_info(struct psp_context *psp,
1281 struct psp_xgmi_topology_info *topology)
1283 struct ta_xgmi_shared_memory *xgmi_cmd;
1284 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1287 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1290 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1291 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1293 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1294 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1295 topology_info_input->num_nodes = number_devices;
1297 for (i = 0; i < topology_info_input->num_nodes; i++) {
1298 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1299 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1300 topology_info_input->nodes[i].is_sharing_enabled = 1;
1301 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1304 /* Invoke xgmi ta to set topology information */
1305 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1309 static int psp_ras_init_shared_buf(struct psp_context *psp)
1311 return psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1314 static int psp_ras_load(struct psp_context *psp)
1316 return psp_ta_load(psp, &psp->ras_context.context);
1319 static int psp_ras_unload(struct psp_context *psp)
1321 return psp_ta_unload(psp, &psp->ras_context.context);
1324 static void psp_ras_ta_check_status(struct psp_context *psp)
1326 struct ta_ras_shared_memory *ras_cmd =
1327 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1329 switch (ras_cmd->ras_status) {
1330 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1331 dev_warn(psp->adev->dev,
1332 "RAS WARNING: cmd failed due to unsupported ip\n");
1334 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1335 dev_warn(psp->adev->dev,
1336 "RAS WARNING: cmd failed due to unsupported error injection\n");
1338 case TA_RAS_STATUS__SUCCESS:
1340 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1341 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1342 dev_warn(psp->adev->dev,
1343 "RAS WARNING: Inject error to critical region is not allowed\n");
1346 dev_warn(psp->adev->dev,
1347 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1352 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1354 struct ta_ras_shared_memory *ras_cmd;
1357 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1360 * TODO: bypass the loading in sriov for now
1362 if (amdgpu_sriov_vf(psp->adev))
1365 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1367 if (amdgpu_ras_intr_triggered())
1370 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1372 DRM_WARN("RAS: Unsupported Interface");
1377 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1378 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1380 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1382 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1383 dev_warn(psp->adev->dev,
1384 "RAS internal register access blocked\n");
1386 psp_ras_ta_check_status(psp);
1392 int psp_ras_enable_features(struct psp_context *psp,
1393 union ta_ras_cmd_input *info, bool enable)
1395 struct ta_ras_shared_memory *ras_cmd;
1398 if (!psp->ras_context.context.initialized)
1401 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1402 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1405 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1407 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1409 ras_cmd->ras_in_message = *info;
1411 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1418 static int psp_ras_terminate(struct psp_context *psp)
1423 * TODO: bypass the terminate in sriov for now
1425 if (amdgpu_sriov_vf(psp->adev))
1428 if (!psp->ras_context.context.initialized)
1431 ret = psp_ras_unload(psp);
1435 psp->ras_context.context.initialized = false;
1437 /* free ras shared memory */
1438 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
1443 static int psp_ras_initialize(struct psp_context *psp)
1446 uint32_t boot_cfg = 0xFF;
1447 struct amdgpu_device *adev = psp->adev;
1448 struct ta_ras_shared_memory *ras_cmd;
1451 * TODO: bypass the initialize in sriov for now
1453 if (amdgpu_sriov_vf(adev))
1456 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1457 !adev->psp.ras_context.context.bin_desc.start_addr) {
1458 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1462 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1463 /* query GECC enablement status from boot config
1464 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1466 ret = psp_boot_config_get(adev, &boot_cfg);
1468 dev_warn(adev->dev, "PSP get boot config failed\n");
1470 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1472 dev_info(adev->dev, "GECC is disabled\n");
1474 /* disable GECC in next boot cycle if ras is
1475 * disabled by module parameter amdgpu_ras_enable
1476 * and/or amdgpu_ras_mask, or boot_config_get call
1479 ret = psp_boot_config_set(adev, 0);
1481 dev_warn(adev->dev, "PSP set boot config failed\n");
1483 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1484 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1487 if (1 == boot_cfg) {
1488 dev_info(adev->dev, "GECC is enabled\n");
1490 /* enable GECC in next boot cycle if it is disabled
1491 * in boot config, or force enable GECC if failed to
1492 * get boot configuration
1494 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1496 dev_warn(adev->dev, "PSP set boot config failed\n");
1498 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1503 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1504 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1506 if (!psp->ras_context.context.initialized) {
1507 ret = psp_ras_init_shared_buf(psp);
1512 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1513 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1515 if (amdgpu_ras_is_poison_mode_supported(adev))
1516 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1517 if (!adev->gmc.xgmi.connected_to_cpu)
1518 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1520 ret = psp_ras_load(psp);
1522 if (!ret && !ras_cmd->ras_status)
1523 psp->ras_context.context.initialized = true;
1525 if (ras_cmd->ras_status)
1526 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1527 amdgpu_ras_fini(psp->adev);
1533 int psp_ras_trigger_error(struct psp_context *psp,
1534 struct ta_ras_trigger_error_input *info)
1536 struct ta_ras_shared_memory *ras_cmd;
1539 if (!psp->ras_context.context.initialized)
1542 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1543 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1545 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1546 ras_cmd->ras_in_message.trigger_error = *info;
1548 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1552 /* If err_event_athub occurs error inject was successful, however
1553 return status from TA is no long reliable */
1554 if (amdgpu_ras_intr_triggered())
1557 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1559 else if (ras_cmd->ras_status)
1567 static int psp_hdcp_init_shared_buf(struct psp_context *psp)
1569 return psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1572 static int psp_hdcp_load(struct psp_context *psp)
1574 return psp_ta_load(psp, &psp->hdcp_context.context);
1577 static int psp_hdcp_initialize(struct psp_context *psp)
1582 * TODO: bypass the initialize in sriov for now
1584 if (amdgpu_sriov_vf(psp->adev))
1587 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1588 !psp->hdcp_context.context.bin_desc.start_addr) {
1589 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1593 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1594 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1596 if (!psp->hdcp_context.context.initialized) {
1597 ret = psp_hdcp_init_shared_buf(psp);
1602 ret = psp_hdcp_load(psp);
1604 psp->hdcp_context.context.initialized = true;
1605 mutex_init(&psp->hdcp_context.mutex);
1611 static int psp_hdcp_unload(struct psp_context *psp)
1613 return psp_ta_unload(psp, &psp->hdcp_context.context);
1616 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1619 * TODO: bypass the loading in sriov for now
1621 if (amdgpu_sriov_vf(psp->adev))
1624 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1627 static int psp_hdcp_terminate(struct psp_context *psp)
1632 * TODO: bypass the terminate in sriov for now
1634 if (amdgpu_sriov_vf(psp->adev))
1637 if (!psp->hdcp_context.context.initialized) {
1638 if (psp->hdcp_context.context.mem_context.shared_buf)
1644 ret = psp_hdcp_unload(psp);
1648 psp->hdcp_context.context.initialized = false;
1651 /* free hdcp shared memory */
1652 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
1659 static int psp_dtm_init_shared_buf(struct psp_context *psp)
1661 return psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1664 static int psp_dtm_load(struct psp_context *psp)
1666 return psp_ta_load(psp, &psp->dtm_context.context);
1669 static int psp_dtm_initialize(struct psp_context *psp)
1674 * TODO: bypass the initialize in sriov for now
1676 if (amdgpu_sriov_vf(psp->adev))
1679 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1680 !psp->dtm_context.context.bin_desc.start_addr) {
1681 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1685 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1686 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1688 if (!psp->dtm_context.context.initialized) {
1689 ret = psp_dtm_init_shared_buf(psp);
1694 ret = psp_dtm_load(psp);
1696 psp->dtm_context.context.initialized = true;
1697 mutex_init(&psp->dtm_context.mutex);
1703 static int psp_dtm_unload(struct psp_context *psp)
1705 return psp_ta_unload(psp, &psp->dtm_context.context);
1708 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1711 * TODO: bypass the loading in sriov for now
1713 if (amdgpu_sriov_vf(psp->adev))
1716 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1719 static int psp_dtm_terminate(struct psp_context *psp)
1724 * TODO: bypass the terminate in sriov for now
1726 if (amdgpu_sriov_vf(psp->adev))
1729 if (!psp->dtm_context.context.initialized) {
1730 if (psp->dtm_context.context.mem_context.shared_buf)
1736 ret = psp_dtm_unload(psp);
1740 psp->dtm_context.context.initialized = false;
1743 /* free dtm shared memory */
1744 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
1751 static int psp_rap_init_shared_buf(struct psp_context *psp)
1753 return psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1756 static int psp_rap_load(struct psp_context *psp)
1758 return psp_ta_load(psp, &psp->rap_context.context);
1761 static int psp_rap_unload(struct psp_context *psp)
1763 return psp_ta_unload(psp, &psp->rap_context.context);
1766 static int psp_rap_initialize(struct psp_context *psp)
1769 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1772 * TODO: bypass the initialize in sriov for now
1774 if (amdgpu_sriov_vf(psp->adev))
1777 if (!psp->rap_context.context.bin_desc.size_bytes ||
1778 !psp->rap_context.context.bin_desc.start_addr) {
1779 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1783 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1784 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1786 if (!psp->rap_context.context.initialized) {
1787 ret = psp_rap_init_shared_buf(psp);
1792 ret = psp_rap_load(psp);
1794 psp->rap_context.context.initialized = true;
1795 mutex_init(&psp->rap_context.mutex);
1799 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1800 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1801 psp_rap_terminate(psp);
1803 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1812 static int psp_rap_terminate(struct psp_context *psp)
1816 if (!psp->rap_context.context.initialized)
1819 ret = psp_rap_unload(psp);
1821 psp->rap_context.context.initialized = false;
1823 /* free rap shared memory */
1824 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1829 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1831 struct ta_rap_shared_memory *rap_cmd;
1834 if (!psp->rap_context.context.initialized)
1837 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1838 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1841 mutex_lock(&psp->rap_context.mutex);
1843 rap_cmd = (struct ta_rap_shared_memory *)
1844 psp->rap_context.context.mem_context.shared_buf;
1845 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1847 rap_cmd->cmd_id = ta_cmd_id;
1848 rap_cmd->validation_method_id = METHOD_A;
1850 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1855 *status = rap_cmd->rap_status;
1858 mutex_unlock(&psp->rap_context.mutex);
1864 /* securedisplay start */
1865 static int psp_securedisplay_init_shared_buf(struct psp_context *psp)
1867 return psp_ta_init_shared_buf(
1868 psp, &psp->securedisplay_context.context.mem_context);
1871 static int psp_securedisplay_load(struct psp_context *psp)
1873 return psp_ta_load(psp, &psp->securedisplay_context.context);
1876 static int psp_securedisplay_unload(struct psp_context *psp)
1878 return psp_ta_unload(psp, &psp->securedisplay_context.context);
1881 static int psp_securedisplay_initialize(struct psp_context *psp)
1884 struct securedisplay_cmd *securedisplay_cmd;
1887 * TODO: bypass the initialize in sriov for now
1889 if (amdgpu_sriov_vf(psp->adev))
1892 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1893 !psp->securedisplay_context.context.bin_desc.start_addr) {
1894 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1898 psp->securedisplay_context.context.mem_context.shared_mem_size =
1899 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1900 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1902 if (!psp->securedisplay_context.context.initialized) {
1903 ret = psp_securedisplay_init_shared_buf(psp);
1908 ret = psp_securedisplay_load(psp);
1910 psp->securedisplay_context.context.initialized = true;
1911 mutex_init(&psp->securedisplay_context.mutex);
1915 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1916 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1918 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1920 psp_securedisplay_terminate(psp);
1921 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1925 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1926 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1927 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1928 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1934 static int psp_securedisplay_terminate(struct psp_context *psp)
1939 * TODO:bypass the terminate in sriov for now
1941 if (amdgpu_sriov_vf(psp->adev))
1944 if (!psp->securedisplay_context.context.initialized)
1947 ret = psp_securedisplay_unload(psp);
1951 psp->securedisplay_context.context.initialized = false;
1953 /* free securedisplay shared memory */
1954 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1959 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1963 if (!psp->securedisplay_context.context.initialized)
1966 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1967 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1970 mutex_lock(&psp->securedisplay_context.mutex);
1972 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1974 mutex_unlock(&psp->securedisplay_context.mutex);
1978 /* SECUREDISPLAY end */
1980 static int psp_hw_start(struct psp_context *psp)
1982 struct amdgpu_device *adev = psp->adev;
1985 if (!amdgpu_sriov_vf(adev)) {
1986 if ((is_psp_fw_valid(psp->kdb)) &&
1987 (psp->funcs->bootloader_load_kdb != NULL)) {
1988 ret = psp_bootloader_load_kdb(psp);
1990 DRM_ERROR("PSP load kdb failed!\n");
1995 if ((is_psp_fw_valid(psp->spl)) &&
1996 (psp->funcs->bootloader_load_spl != NULL)) {
1997 ret = psp_bootloader_load_spl(psp);
1999 DRM_ERROR("PSP load spl failed!\n");
2004 if ((is_psp_fw_valid(psp->sys)) &&
2005 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2006 ret = psp_bootloader_load_sysdrv(psp);
2008 DRM_ERROR("PSP load sys drv failed!\n");
2013 if ((is_psp_fw_valid(psp->soc_drv)) &&
2014 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2015 ret = psp_bootloader_load_soc_drv(psp);
2017 DRM_ERROR("PSP load soc drv failed!\n");
2022 if ((is_psp_fw_valid(psp->intf_drv)) &&
2023 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2024 ret = psp_bootloader_load_intf_drv(psp);
2026 DRM_ERROR("PSP load intf drv failed!\n");
2031 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2032 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2033 ret = psp_bootloader_load_dbg_drv(psp);
2035 DRM_ERROR("PSP load dbg drv failed!\n");
2040 if ((is_psp_fw_valid(psp->sos)) &&
2041 (psp->funcs->bootloader_load_sos != NULL)) {
2042 ret = psp_bootloader_load_sos(psp);
2044 DRM_ERROR("PSP load sos failed!\n");
2050 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2052 DRM_ERROR("PSP create ring failed!\n");
2056 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2059 ret = psp_tmr_init(psp);
2061 DRM_ERROR("PSP tmr init failed!\n");
2067 * For ASICs with DF Cstate management centralized
2068 * to PMFW, TMR setup should be performed after PMFW
2069 * loaded and before other non-psp firmware loaded.
2071 if (psp->pmfw_centralized_cstate_management) {
2072 ret = psp_load_smu_fw(psp);
2077 ret = psp_tmr_load(psp);
2079 DRM_ERROR("PSP load tmr failed!\n");
2086 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2087 enum psp_gfx_fw_type *type)
2089 switch (ucode->ucode_id) {
2090 case AMDGPU_UCODE_ID_CAP:
2091 *type = GFX_FW_TYPE_CAP;
2093 case AMDGPU_UCODE_ID_SDMA0:
2094 *type = GFX_FW_TYPE_SDMA0;
2096 case AMDGPU_UCODE_ID_SDMA1:
2097 *type = GFX_FW_TYPE_SDMA1;
2099 case AMDGPU_UCODE_ID_SDMA2:
2100 *type = GFX_FW_TYPE_SDMA2;
2102 case AMDGPU_UCODE_ID_SDMA3:
2103 *type = GFX_FW_TYPE_SDMA3;
2105 case AMDGPU_UCODE_ID_SDMA4:
2106 *type = GFX_FW_TYPE_SDMA4;
2108 case AMDGPU_UCODE_ID_SDMA5:
2109 *type = GFX_FW_TYPE_SDMA5;
2111 case AMDGPU_UCODE_ID_SDMA6:
2112 *type = GFX_FW_TYPE_SDMA6;
2114 case AMDGPU_UCODE_ID_SDMA7:
2115 *type = GFX_FW_TYPE_SDMA7;
2117 case AMDGPU_UCODE_ID_CP_MES:
2118 *type = GFX_FW_TYPE_CP_MES;
2120 case AMDGPU_UCODE_ID_CP_MES_DATA:
2121 *type = GFX_FW_TYPE_MES_STACK;
2123 case AMDGPU_UCODE_ID_CP_CE:
2124 *type = GFX_FW_TYPE_CP_CE;
2126 case AMDGPU_UCODE_ID_CP_PFP:
2127 *type = GFX_FW_TYPE_CP_PFP;
2129 case AMDGPU_UCODE_ID_CP_ME:
2130 *type = GFX_FW_TYPE_CP_ME;
2132 case AMDGPU_UCODE_ID_CP_MEC1:
2133 *type = GFX_FW_TYPE_CP_MEC;
2135 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2136 *type = GFX_FW_TYPE_CP_MEC_ME1;
2138 case AMDGPU_UCODE_ID_CP_MEC2:
2139 *type = GFX_FW_TYPE_CP_MEC;
2141 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2142 *type = GFX_FW_TYPE_CP_MEC_ME2;
2144 case AMDGPU_UCODE_ID_RLC_G:
2145 *type = GFX_FW_TYPE_RLC_G;
2147 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2148 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2150 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2151 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2153 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2154 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2156 case AMDGPU_UCODE_ID_RLC_IRAM:
2157 *type = GFX_FW_TYPE_RLC_IRAM;
2159 case AMDGPU_UCODE_ID_RLC_DRAM:
2160 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2162 case AMDGPU_UCODE_ID_SMC:
2163 *type = GFX_FW_TYPE_SMU;
2165 case AMDGPU_UCODE_ID_UVD:
2166 *type = GFX_FW_TYPE_UVD;
2168 case AMDGPU_UCODE_ID_UVD1:
2169 *type = GFX_FW_TYPE_UVD1;
2171 case AMDGPU_UCODE_ID_VCE:
2172 *type = GFX_FW_TYPE_VCE;
2174 case AMDGPU_UCODE_ID_VCN:
2175 *type = GFX_FW_TYPE_VCN;
2177 case AMDGPU_UCODE_ID_VCN1:
2178 *type = GFX_FW_TYPE_VCN1;
2180 case AMDGPU_UCODE_ID_DMCU_ERAM:
2181 *type = GFX_FW_TYPE_DMCU_ERAM;
2183 case AMDGPU_UCODE_ID_DMCU_INTV:
2184 *type = GFX_FW_TYPE_DMCU_ISR;
2186 case AMDGPU_UCODE_ID_VCN0_RAM:
2187 *type = GFX_FW_TYPE_VCN0_RAM;
2189 case AMDGPU_UCODE_ID_VCN1_RAM:
2190 *type = GFX_FW_TYPE_VCN1_RAM;
2192 case AMDGPU_UCODE_ID_DMCUB:
2193 *type = GFX_FW_TYPE_DMUB;
2195 case AMDGPU_UCODE_ID_MAXIMUM:
2203 static void psp_print_fw_hdr(struct psp_context *psp,
2204 struct amdgpu_firmware_info *ucode)
2206 struct amdgpu_device *adev = psp->adev;
2207 struct common_firmware_header *hdr;
2209 switch (ucode->ucode_id) {
2210 case AMDGPU_UCODE_ID_SDMA0:
2211 case AMDGPU_UCODE_ID_SDMA1:
2212 case AMDGPU_UCODE_ID_SDMA2:
2213 case AMDGPU_UCODE_ID_SDMA3:
2214 case AMDGPU_UCODE_ID_SDMA4:
2215 case AMDGPU_UCODE_ID_SDMA5:
2216 case AMDGPU_UCODE_ID_SDMA6:
2217 case AMDGPU_UCODE_ID_SDMA7:
2218 hdr = (struct common_firmware_header *)
2219 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2220 amdgpu_ucode_print_sdma_hdr(hdr);
2222 case AMDGPU_UCODE_ID_CP_CE:
2223 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2224 amdgpu_ucode_print_gfx_hdr(hdr);
2226 case AMDGPU_UCODE_ID_CP_PFP:
2227 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2228 amdgpu_ucode_print_gfx_hdr(hdr);
2230 case AMDGPU_UCODE_ID_CP_ME:
2231 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2232 amdgpu_ucode_print_gfx_hdr(hdr);
2234 case AMDGPU_UCODE_ID_CP_MEC1:
2235 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2236 amdgpu_ucode_print_gfx_hdr(hdr);
2238 case AMDGPU_UCODE_ID_RLC_G:
2239 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2240 amdgpu_ucode_print_rlc_hdr(hdr);
2242 case AMDGPU_UCODE_ID_SMC:
2243 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2244 amdgpu_ucode_print_smc_hdr(hdr);
2251 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2252 struct psp_gfx_cmd_resp *cmd)
2255 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2257 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2258 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2259 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2260 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2262 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2264 DRM_ERROR("Unknown firmware type\n");
2269 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2270 struct amdgpu_firmware_info *ucode)
2273 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2275 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2277 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2278 psp->fence_buf_mc_addr);
2281 release_psp_cmd_buf(psp);
2286 static int psp_load_smu_fw(struct psp_context *psp)
2289 struct amdgpu_device *adev = psp->adev;
2290 struct amdgpu_firmware_info *ucode =
2291 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2292 struct amdgpu_ras *ras = psp->ras_context.ras;
2294 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2297 if ((amdgpu_in_reset(adev) &&
2298 ras && adev->ras_enabled &&
2299 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2300 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2301 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2303 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2307 ret = psp_execute_non_psp_fw_load(psp, ucode);
2310 DRM_ERROR("PSP load smu failed!\n");
2315 static bool fw_load_skip_check(struct psp_context *psp,
2316 struct amdgpu_firmware_info *ucode)
2321 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2322 (psp_smu_reload_quirk(psp) ||
2323 psp->autoload_supported ||
2324 psp->pmfw_centralized_cstate_management))
2327 if (amdgpu_sriov_vf(psp->adev) &&
2328 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
2329 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
2330 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2
2331 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3
2332 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA4
2333 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA5
2334 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA6
2335 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA7
2336 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G
2337 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
2338 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
2339 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
2340 || ucode->ucode_id == AMDGPU_UCODE_ID_SMC))
2341 /*skip ucode loading in SRIOV VF */
2344 if (psp->autoload_supported &&
2345 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2346 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2347 /* skip mec JT when autoload is enabled */
2353 int psp_load_fw_list(struct psp_context *psp,
2354 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2357 struct amdgpu_firmware_info *ucode;
2359 for (i = 0; i < ucode_count; ++i) {
2360 ucode = ucode_list[i];
2361 psp_print_fw_hdr(psp, ucode);
2362 ret = psp_execute_non_psp_fw_load(psp, ucode);
2369 static int psp_load_non_psp_fw(struct psp_context *psp)
2372 struct amdgpu_firmware_info *ucode;
2373 struct amdgpu_device *adev = psp->adev;
2375 if (psp->autoload_supported &&
2376 !psp->pmfw_centralized_cstate_management) {
2377 ret = psp_load_smu_fw(psp);
2382 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2383 ucode = &adev->firmware.ucode[i];
2385 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2386 !fw_load_skip_check(psp, ucode)) {
2387 ret = psp_load_smu_fw(psp);
2393 if (fw_load_skip_check(psp, ucode))
2396 if (psp->autoload_supported &&
2397 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2398 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2399 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2400 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2401 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2402 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2403 /* PSP only receive one SDMA fw for sienna_cichlid,
2404 * as all four sdma fw are same */
2407 psp_print_fw_hdr(psp, ucode);
2409 ret = psp_execute_non_psp_fw_load(psp, ucode);
2413 /* Start rlc autoload after psp recieved all the gfx firmware */
2414 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2415 AMDGPU_UCODE_ID_CP_MEC2 : AMDGPU_UCODE_ID_RLC_G)) {
2416 ret = psp_rlc_autoload_start(psp);
2418 DRM_ERROR("Failed to start rlc autoload\n");
2427 static int psp_load_fw(struct amdgpu_device *adev)
2430 struct psp_context *psp = &adev->psp;
2432 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2433 psp_ring_stop(psp, PSP_RING_TYPE__KM); /* should not destroy ring, only stop */
2437 if (amdgpu_sriov_vf(adev)) {
2438 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
2439 AMDGPU_GEM_DOMAIN_VRAM,
2441 &psp->fw_pri_mc_addr,
2444 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
2445 AMDGPU_GEM_DOMAIN_GTT,
2447 &psp->fw_pri_mc_addr,
2454 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
2455 AMDGPU_GEM_DOMAIN_VRAM,
2457 &psp->fence_buf_mc_addr,
2462 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
2463 AMDGPU_GEM_DOMAIN_VRAM,
2464 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
2465 (void **)&psp->cmd_buf_mem);
2469 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2471 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2473 DRM_ERROR("PSP ring init failed!\n");
2478 ret = psp_hw_start(psp);
2482 ret = psp_load_non_psp_fw(psp);
2486 ret = psp_asd_initialize(psp);
2488 DRM_ERROR("PSP load asd failed!\n");
2492 ret = psp_rl_load(adev);
2494 DRM_ERROR("PSP load RL failed!\n");
2498 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2499 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2500 ret = psp_xgmi_initialize(psp, false, true);
2501 /* Warning the XGMI seesion initialize failure
2502 * Instead of stop driver initialization
2505 dev_err(psp->adev->dev,
2506 "XGMI: Failed to initialize XGMI session\n");
2511 ret = psp_ras_initialize(psp);
2513 dev_err(psp->adev->dev,
2514 "RAS: Failed to initialize RAS\n");
2516 ret = psp_hdcp_initialize(psp);
2518 dev_err(psp->adev->dev,
2519 "HDCP: Failed to initialize HDCP\n");
2521 ret = psp_dtm_initialize(psp);
2523 dev_err(psp->adev->dev,
2524 "DTM: Failed to initialize DTM\n");
2526 ret = psp_rap_initialize(psp);
2528 dev_err(psp->adev->dev,
2529 "RAP: Failed to initialize RAP\n");
2531 ret = psp_securedisplay_initialize(psp);
2533 dev_err(psp->adev->dev,
2534 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2541 * all cleanup jobs (xgmi terminate, ras terminate,
2542 * ring destroy, cmd/fence/fw buffers destory,
2543 * psp->cmd destory) are delayed to psp_hw_fini
2548 static int psp_hw_init(void *handle)
2551 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2553 mutex_lock(&adev->firmware.mutex);
2555 * This sequence is just used on hw_init only once, no need on
2558 ret = amdgpu_ucode_init_bo(adev);
2562 ret = psp_load_fw(adev);
2564 DRM_ERROR("PSP firmware loading failed\n");
2568 mutex_unlock(&adev->firmware.mutex);
2572 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2573 mutex_unlock(&adev->firmware.mutex);
2577 static int psp_hw_fini(void *handle)
2579 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2580 struct psp_context *psp = &adev->psp;
2583 psp_ras_terminate(psp);
2584 psp_securedisplay_terminate(psp);
2585 psp_rap_terminate(psp);
2586 psp_dtm_terminate(psp);
2587 psp_hdcp_terminate(psp);
2590 psp_asd_terminate(psp);
2592 psp_tmr_terminate(psp);
2593 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2595 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
2596 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
2597 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
2598 &psp->fence_buf_mc_addr, &psp->fence_buf);
2599 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
2600 (void **)&psp->cmd_buf_mem);
2605 static int psp_suspend(void *handle)
2608 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2609 struct psp_context *psp = &adev->psp;
2611 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2612 psp->xgmi_context.context.initialized) {
2613 ret = psp_xgmi_terminate(psp);
2615 DRM_ERROR("Failed to terminate xgmi ta\n");
2621 ret = psp_ras_terminate(psp);
2623 DRM_ERROR("Failed to terminate ras ta\n");
2626 ret = psp_hdcp_terminate(psp);
2628 DRM_ERROR("Failed to terminate hdcp ta\n");
2631 ret = psp_dtm_terminate(psp);
2633 DRM_ERROR("Failed to terminate dtm ta\n");
2636 ret = psp_rap_terminate(psp);
2638 DRM_ERROR("Failed to terminate rap ta\n");
2641 ret = psp_securedisplay_terminate(psp);
2643 DRM_ERROR("Failed to terminate securedisplay ta\n");
2648 ret = psp_asd_terminate(psp);
2650 DRM_ERROR("Failed to terminate asd\n");
2654 ret = psp_tmr_terminate(psp);
2656 DRM_ERROR("Failed to terminate tmr\n");
2660 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2662 DRM_ERROR("PSP ring stop failed\n");
2669 static int psp_resume(void *handle)
2672 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2673 struct psp_context *psp = &adev->psp;
2675 DRM_INFO("PSP is resuming...\n");
2677 if (psp->mem_train_ctx.enable_mem_training) {
2678 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2680 DRM_ERROR("Failed to process memory training!\n");
2685 mutex_lock(&adev->firmware.mutex);
2687 ret = psp_hw_start(psp);
2691 ret = psp_load_non_psp_fw(psp);
2695 ret = psp_asd_initialize(psp);
2697 DRM_ERROR("PSP load asd failed!\n");
2701 ret = psp_rl_load(adev);
2703 dev_err(adev->dev, "PSP load RL failed!\n");
2707 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2708 ret = psp_xgmi_initialize(psp, false, true);
2709 /* Warning the XGMI seesion initialize failure
2710 * Instead of stop driver initialization
2713 dev_err(psp->adev->dev,
2714 "XGMI: Failed to initialize XGMI session\n");
2718 ret = psp_ras_initialize(psp);
2720 dev_err(psp->adev->dev,
2721 "RAS: Failed to initialize RAS\n");
2723 ret = psp_hdcp_initialize(psp);
2725 dev_err(psp->adev->dev,
2726 "HDCP: Failed to initialize HDCP\n");
2728 ret = psp_dtm_initialize(psp);
2730 dev_err(psp->adev->dev,
2731 "DTM: Failed to initialize DTM\n");
2733 ret = psp_rap_initialize(psp);
2735 dev_err(psp->adev->dev,
2736 "RAP: Failed to initialize RAP\n");
2738 ret = psp_securedisplay_initialize(psp);
2740 dev_err(psp->adev->dev,
2741 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2744 mutex_unlock(&adev->firmware.mutex);
2749 DRM_ERROR("PSP resume failed\n");
2750 mutex_unlock(&adev->firmware.mutex);
2754 int psp_gpu_reset(struct amdgpu_device *adev)
2758 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2761 mutex_lock(&adev->psp.mutex);
2762 ret = psp_mode1_reset(&adev->psp);
2763 mutex_unlock(&adev->psp.mutex);
2768 int psp_rlc_autoload_start(struct psp_context *psp)
2771 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2773 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2775 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2776 psp->fence_buf_mc_addr);
2778 release_psp_cmd_buf(psp);
2783 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2784 uint64_t cmd_gpu_addr, int cmd_size)
2786 struct amdgpu_firmware_info ucode = {0};
2788 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2789 AMDGPU_UCODE_ID_VCN0_RAM;
2790 ucode.mc_addr = cmd_gpu_addr;
2791 ucode.ucode_size = cmd_size;
2793 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2796 int psp_ring_cmd_submit(struct psp_context *psp,
2797 uint64_t cmd_buf_mc_addr,
2798 uint64_t fence_mc_addr,
2801 unsigned int psp_write_ptr_reg = 0;
2802 struct psp_gfx_rb_frame *write_frame;
2803 struct psp_ring *ring = &psp->km_ring;
2804 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2805 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2806 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2807 struct amdgpu_device *adev = psp->adev;
2808 uint32_t ring_size_dw = ring->ring_size / 4;
2809 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2811 /* KM (GPCOM) prepare write pointer */
2812 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2814 /* Update KM RB frame pointer to new frame */
2815 /* write_frame ptr increments by size of rb_frame in bytes */
2816 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2817 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2818 write_frame = ring_buffer_start;
2820 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2821 /* Check invalid write_frame ptr address */
2822 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2823 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2824 ring_buffer_start, ring_buffer_end, write_frame);
2825 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2829 /* Initialize KM RB frame */
2830 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2832 /* Update KM RB frame */
2833 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2834 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2835 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2836 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2837 write_frame->fence_value = index;
2838 amdgpu_device_flush_hdp(adev, NULL);
2840 /* Update the write Pointer in DWORDs */
2841 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2842 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2846 int psp_init_asd_microcode(struct psp_context *psp,
2847 const char *chip_name)
2849 struct amdgpu_device *adev = psp->adev;
2850 char fw_name[PSP_FW_NAME_LEN];
2851 const struct psp_firmware_header_v1_0 *asd_hdr;
2855 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2859 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2860 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2864 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2868 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2869 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2870 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2871 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2872 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2873 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2876 dev_err(adev->dev, "fail to initialize asd microcode\n");
2877 release_firmware(adev->psp.asd_fw);
2878 adev->psp.asd_fw = NULL;
2882 int psp_init_toc_microcode(struct psp_context *psp,
2883 const char *chip_name)
2885 struct amdgpu_device *adev = psp->adev;
2886 char fw_name[PSP_FW_NAME_LEN];
2887 const struct psp_firmware_header_v1_0 *toc_hdr;
2891 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2895 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2896 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2900 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2904 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2905 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2906 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2907 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2908 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2909 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2912 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2913 release_firmware(adev->psp.toc_fw);
2914 adev->psp.toc_fw = NULL;
2918 static int parse_sos_bin_descriptor(struct psp_context *psp,
2919 const struct psp_fw_bin_desc *desc,
2920 const struct psp_firmware_header_v2_0 *sos_hdr)
2922 uint8_t *ucode_start_addr = NULL;
2924 if (!psp || !desc || !sos_hdr)
2927 ucode_start_addr = (uint8_t *)sos_hdr +
2928 le32_to_cpu(desc->offset_bytes) +
2929 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2931 switch (desc->fw_type) {
2932 case PSP_FW_TYPE_PSP_SOS:
2933 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
2934 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
2935 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
2936 psp->sos.start_addr = ucode_start_addr;
2938 case PSP_FW_TYPE_PSP_SYS_DRV:
2939 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
2940 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
2941 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
2942 psp->sys.start_addr = ucode_start_addr;
2944 case PSP_FW_TYPE_PSP_KDB:
2945 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
2946 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
2947 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
2948 psp->kdb.start_addr = ucode_start_addr;
2950 case PSP_FW_TYPE_PSP_TOC:
2951 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
2952 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
2953 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
2954 psp->toc.start_addr = ucode_start_addr;
2956 case PSP_FW_TYPE_PSP_SPL:
2957 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
2958 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
2959 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
2960 psp->spl.start_addr = ucode_start_addr;
2962 case PSP_FW_TYPE_PSP_RL:
2963 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
2964 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
2965 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
2966 psp->rl.start_addr = ucode_start_addr;
2968 case PSP_FW_TYPE_PSP_SOC_DRV:
2969 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
2970 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
2971 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2972 psp->soc_drv.start_addr = ucode_start_addr;
2974 case PSP_FW_TYPE_PSP_INTF_DRV:
2975 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
2976 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
2977 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2978 psp->intf_drv.start_addr = ucode_start_addr;
2980 case PSP_FW_TYPE_PSP_DBG_DRV:
2981 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
2982 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
2983 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2984 psp->dbg_drv.start_addr = ucode_start_addr;
2987 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
2994 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
2996 const struct psp_firmware_header_v1_0 *sos_hdr;
2997 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
2998 uint8_t *ucode_array_start_addr;
3000 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3001 ucode_array_start_addr = (uint8_t *)sos_hdr +
3002 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3004 if (adev->gmc.xgmi.connected_to_cpu ||
3005 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3006 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3007 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3009 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3010 adev->psp.sys.start_addr = ucode_array_start_addr;
3012 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3013 adev->psp.sos.start_addr = ucode_array_start_addr +
3014 le32_to_cpu(sos_hdr->sos.offset_bytes);
3016 /* Load alternate PSP SOS FW */
3017 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3019 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3020 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3022 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3023 adev->psp.sys.start_addr = ucode_array_start_addr +
3024 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3026 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3027 adev->psp.sos.start_addr = ucode_array_start_addr +
3028 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3031 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3032 dev_warn(adev->dev, "PSP SOS FW not available");
3039 int psp_init_sos_microcode(struct psp_context *psp,
3040 const char *chip_name)
3042 struct amdgpu_device *adev = psp->adev;
3043 char fw_name[PSP_FW_NAME_LEN];
3044 const struct psp_firmware_header_v1_0 *sos_hdr;
3045 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3046 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3047 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3048 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3050 uint8_t *ucode_array_start_addr;
3054 dev_err(adev->dev, "invalid chip name for sos microcode\n");
3058 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3059 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
3063 err = amdgpu_ucode_validate(adev->psp.sos_fw);
3067 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3068 ucode_array_start_addr = (uint8_t *)sos_hdr +
3069 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3070 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3072 switch (sos_hdr->header.header_version_major) {
3074 err = psp_init_sos_base_fw(adev);
3078 if (sos_hdr->header.header_version_minor == 1) {
3079 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3080 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3081 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3082 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3083 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3084 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3085 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3087 if (sos_hdr->header.header_version_minor == 2) {
3088 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3089 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3090 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3091 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3093 if (sos_hdr->header.header_version_minor == 3) {
3094 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3095 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3096 adev->psp.toc.start_addr = ucode_array_start_addr +
3097 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3098 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3099 adev->psp.kdb.start_addr = ucode_array_start_addr +
3100 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3101 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3102 adev->psp.spl.start_addr = ucode_array_start_addr +
3103 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3104 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3105 adev->psp.rl.start_addr = ucode_array_start_addr +
3106 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3110 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3112 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3113 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3118 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3119 err = parse_sos_bin_descriptor(psp,
3120 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3128 "unsupported psp sos firmware\n");
3136 "failed to init sos firmware\n");
3137 release_firmware(adev->psp.sos_fw);
3138 adev->psp.sos_fw = NULL;
3143 static int parse_ta_bin_descriptor(struct psp_context *psp,
3144 const struct psp_fw_bin_desc *desc,
3145 const struct ta_firmware_header_v2_0 *ta_hdr)
3147 uint8_t *ucode_start_addr = NULL;
3149 if (!psp || !desc || !ta_hdr)
3152 ucode_start_addr = (uint8_t *)ta_hdr +
3153 le32_to_cpu(desc->offset_bytes) +
3154 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3156 switch (desc->fw_type) {
3157 case TA_FW_TYPE_PSP_ASD:
3158 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3159 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3160 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3161 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3163 case TA_FW_TYPE_PSP_XGMI:
3164 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3165 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3166 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3168 case TA_FW_TYPE_PSP_RAS:
3169 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3170 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3171 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3173 case TA_FW_TYPE_PSP_HDCP:
3174 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3175 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3176 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3178 case TA_FW_TYPE_PSP_DTM:
3179 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3180 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3181 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3183 case TA_FW_TYPE_PSP_RAP:
3184 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3185 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3186 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3188 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3189 psp->securedisplay_context.context.bin_desc.fw_version =
3190 le32_to_cpu(desc->fw_version);
3191 psp->securedisplay_context.context.bin_desc.size_bytes =
3192 le32_to_cpu(desc->size_bytes);
3193 psp->securedisplay_context.context.bin_desc.start_addr =
3197 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3204 int psp_init_ta_microcode(struct psp_context *psp,
3205 const char *chip_name)
3207 struct amdgpu_device *adev = psp->adev;
3208 char fw_name[PSP_FW_NAME_LEN];
3209 const struct ta_firmware_header_v2_0 *ta_hdr;
3214 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3218 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3219 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3223 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3227 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3229 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3230 dev_err(adev->dev, "unsupported TA header version\n");
3235 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3236 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3241 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3242 err = parse_ta_bin_descriptor(psp,
3243 &ta_hdr->ta_fw_bin[ta_index],
3251 dev_err(adev->dev, "fail to initialize ta microcode\n");
3252 release_firmware(adev->psp.ta_fw);
3253 adev->psp.ta_fw = NULL;
3257 int psp_init_cap_microcode(struct psp_context *psp,
3258 const char *chip_name)
3260 struct amdgpu_device *adev = psp->adev;
3261 char fw_name[PSP_FW_NAME_LEN];
3262 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3263 struct amdgpu_firmware_info *info = NULL;
3267 dev_err(adev->dev, "invalid chip name for cap microcode\n");
3271 if (!amdgpu_sriov_vf(adev)) {
3272 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3276 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3277 err = request_firmware(&adev->psp.cap_fw, fw_name, adev->dev);
3279 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3284 err = amdgpu_ucode_validate(adev->psp.cap_fw);
3286 dev_err(adev->dev, "fail to initialize cap microcode\n");
3290 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3291 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3292 info->fw = adev->psp.cap_fw;
3293 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3294 adev->psp.cap_fw->data;
3295 adev->firmware.fw_size += ALIGN(
3296 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3297 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3298 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3299 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3304 release_firmware(adev->psp.cap_fw);
3305 adev->psp.cap_fw = NULL;
3309 static int psp_set_clockgating_state(void *handle,
3310 enum amd_clockgating_state state)
3315 static int psp_set_powergating_state(void *handle,
3316 enum amd_powergating_state state)
3321 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3322 struct device_attribute *attr,
3325 struct drm_device *ddev = dev_get_drvdata(dev);
3326 struct amdgpu_device *adev = drm_to_adev(ddev);
3330 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3331 DRM_INFO("PSP block is not ready yet.");
3335 mutex_lock(&adev->psp.mutex);
3336 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3337 mutex_unlock(&adev->psp.mutex);
3340 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3344 return sysfs_emit(buf, "%x\n", fw_ver);
3347 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3348 struct device_attribute *attr,
3352 struct drm_device *ddev = dev_get_drvdata(dev);
3353 struct amdgpu_device *adev = drm_to_adev(ddev);
3356 const struct firmware *usbc_pd_fw;
3357 struct amdgpu_bo *fw_buf_bo = NULL;
3358 uint64_t fw_pri_mc_addr;
3359 void *fw_pri_cpu_addr;
3361 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3362 DRM_INFO("PSP block is not ready yet.");
3366 if (!drm_dev_enter(ddev, &idx))
3369 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3370 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3374 /* LFB address which is aligned to 1MB boundary per PSP request */
3375 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3376 AMDGPU_GEM_DOMAIN_VRAM,
3383 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3385 mutex_lock(&adev->psp.mutex);
3386 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3387 mutex_unlock(&adev->psp.mutex);
3389 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3392 release_firmware(usbc_pd_fw);
3395 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3403 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3407 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3410 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3411 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3416 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3417 psp_usbc_pd_fw_sysfs_read,
3418 psp_usbc_pd_fw_sysfs_write);
3420 int is_psp_fw_valid(struct psp_bin_desc bin)
3422 return bin.size_bytes;
3425 const struct amd_ip_funcs psp_ip_funcs = {
3427 .early_init = psp_early_init,
3429 .sw_init = psp_sw_init,
3430 .sw_fini = psp_sw_fini,
3431 .hw_init = psp_hw_init,
3432 .hw_fini = psp_hw_fini,
3433 .suspend = psp_suspend,
3434 .resume = psp_resume,
3436 .check_soft_reset = NULL,
3437 .wait_for_idle = NULL,
3439 .set_clockgating_state = psp_set_clockgating_state,
3440 .set_powergating_state = psp_set_powergating_state,
3443 static int psp_sysfs_init(struct amdgpu_device *adev)
3445 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3448 DRM_ERROR("Failed to create USBC PD FW control file!");
3453 static void psp_sysfs_fini(struct amdgpu_device *adev)
3455 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3458 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3460 .type = AMD_IP_BLOCK_TYPE_PSP,
3464 .funcs = &psp_ip_funcs,
3467 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3469 .type = AMD_IP_BLOCK_TYPE_PSP,
3473 .funcs = &psp_ip_funcs,
3476 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3478 .type = AMD_IP_BLOCK_TYPE_PSP,
3482 .funcs = &psp_ip_funcs,
3485 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3486 .type = AMD_IP_BLOCK_TYPE_PSP,
3490 .funcs = &psp_ip_funcs,
3493 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3495 .type = AMD_IP_BLOCK_TYPE_PSP,
3499 .funcs = &psp_ip_funcs,
3502 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3503 .type = AMD_IP_BLOCK_TYPE_PSP,
3507 .funcs = &psp_ip_funcs,