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"
40 #include "psp_v13_0_4.h"
42 #include "amdgpu_ras.h"
43 #include "amdgpu_securedisplay.h"
44 #include "amdgpu_atomfirmware.h"
46 #define AMD_VBIOS_FILE_MAX_SIZE_B (1024*1024*3)
48 static int psp_sysfs_init(struct amdgpu_device *adev);
49 static void psp_sysfs_fini(struct amdgpu_device *adev);
51 static int psp_load_smu_fw(struct psp_context *psp);
52 static int psp_rap_terminate(struct psp_context *psp);
53 static int psp_securedisplay_terminate(struct psp_context *psp);
56 * Due to DF Cstate management centralized to PMFW, the firmware
57 * loading sequence will be updated as below:
63 * - Load other non-psp fw
65 * - Load XGMI/RAS/HDCP/DTM TA if any
67 * This new sequence is required for
68 * - Arcturus and onwards
70 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
72 struct amdgpu_device *adev = psp->adev;
74 if (amdgpu_sriov_vf(adev)) {
75 psp->pmfw_centralized_cstate_management = false;
79 switch (adev->ip_versions[MP0_HWIP][0]) {
80 case IP_VERSION(11, 0, 0):
81 case IP_VERSION(11, 0, 4):
82 case IP_VERSION(11, 0, 5):
83 case IP_VERSION(11, 0, 7):
84 case IP_VERSION(11, 0, 9):
85 case IP_VERSION(11, 0, 11):
86 case IP_VERSION(11, 0, 12):
87 case IP_VERSION(11, 0, 13):
88 case IP_VERSION(13, 0, 0):
89 case IP_VERSION(13, 0, 2):
90 case IP_VERSION(13, 0, 7):
91 psp->pmfw_centralized_cstate_management = true;
94 psp->pmfw_centralized_cstate_management = false;
99 static int psp_early_init(void *handle)
101 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
102 struct psp_context *psp = &adev->psp;
104 switch (adev->ip_versions[MP0_HWIP][0]) {
105 case IP_VERSION(9, 0, 0):
106 psp_v3_1_set_psp_funcs(psp);
107 psp->autoload_supported = false;
109 case IP_VERSION(10, 0, 0):
110 case IP_VERSION(10, 0, 1):
111 psp_v10_0_set_psp_funcs(psp);
112 psp->autoload_supported = false;
114 case IP_VERSION(11, 0, 2):
115 case IP_VERSION(11, 0, 4):
116 psp_v11_0_set_psp_funcs(psp);
117 psp->autoload_supported = false;
119 case IP_VERSION(11, 0, 0):
120 case IP_VERSION(11, 0, 5):
121 case IP_VERSION(11, 0, 9):
122 case IP_VERSION(11, 0, 7):
123 case IP_VERSION(11, 0, 11):
124 case IP_VERSION(11, 5, 0):
125 case IP_VERSION(11, 0, 12):
126 case IP_VERSION(11, 0, 13):
127 psp_v11_0_set_psp_funcs(psp);
128 psp->autoload_supported = true;
130 case IP_VERSION(11, 0, 3):
131 case IP_VERSION(12, 0, 1):
132 psp_v12_0_set_psp_funcs(psp);
134 case IP_VERSION(13, 0, 2):
135 psp_v13_0_set_psp_funcs(psp);
137 case IP_VERSION(13, 0, 1):
138 case IP_VERSION(13, 0, 3):
139 case IP_VERSION(13, 0, 5):
140 case IP_VERSION(13, 0, 8):
141 case IP_VERSION(13, 0, 10):
142 psp_v13_0_set_psp_funcs(psp);
143 psp->autoload_supported = true;
145 case IP_VERSION(11, 0, 8):
146 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
147 psp_v11_0_8_set_psp_funcs(psp);
148 psp->autoload_supported = false;
151 case IP_VERSION(13, 0, 0):
152 case IP_VERSION(13, 0, 7):
153 psp_v13_0_set_psp_funcs(psp);
154 psp->autoload_supported = true;
156 case IP_VERSION(13, 0, 4):
157 psp_v13_0_4_set_psp_funcs(psp);
158 psp->autoload_supported = true;
166 psp_check_pmfw_centralized_cstate_management(psp);
171 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
173 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
174 &mem_ctx->shared_buf);
175 mem_ctx->shared_bo = NULL;
178 static void psp_free_shared_bufs(struct psp_context *psp)
183 /* free TMR memory buffer */
184 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
185 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
188 /* free xgmi shared memory */
189 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
191 /* free ras shared memory */
192 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
194 /* free hdcp shared memory */
195 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
197 /* free dtm shared memory */
198 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
200 /* free rap shared memory */
201 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
203 /* free securedisplay shared memory */
204 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
209 static void psp_memory_training_fini(struct psp_context *psp)
211 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
213 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
214 kfree(ctx->sys_cache);
215 ctx->sys_cache = NULL;
218 static int psp_memory_training_init(struct psp_context *psp)
221 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
223 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
224 DRM_DEBUG("memory training is not supported!\n");
228 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
229 if (ctx->sys_cache == NULL) {
230 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
235 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
236 ctx->train_data_size,
237 ctx->p2c_train_data_offset,
238 ctx->c2p_train_data_offset);
239 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
243 psp_memory_training_fini(psp);
248 * Helper funciton to query psp runtime database entry
250 * @adev: amdgpu_device pointer
251 * @entry_type: the type of psp runtime database entry
252 * @db_entry: runtime database entry pointer
254 * Return false if runtime database doesn't exit or entry is invalid
255 * or true if the specific database entry is found, and copy to @db_entry
257 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
258 enum psp_runtime_entry_type entry_type,
261 uint64_t db_header_pos, db_dir_pos;
262 struct psp_runtime_data_header db_header = {0};
263 struct psp_runtime_data_directory db_dir = {0};
267 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
268 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
270 /* read runtime db header from vram */
271 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
272 sizeof(struct psp_runtime_data_header), false);
274 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
275 /* runtime db doesn't exist, exit */
276 dev_warn(adev->dev, "PSP runtime database doesn't exist\n");
280 /* read runtime database entry from vram */
281 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
282 sizeof(struct psp_runtime_data_directory), false);
284 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
285 /* invalid db entry count, exit */
286 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
290 /* look up for requested entry type */
291 for (i = 0; i < db_dir.entry_count && !ret; i++) {
292 if (db_dir.entry_list[i].entry_type == entry_type) {
293 switch (entry_type) {
294 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
295 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
296 /* invalid db entry size */
297 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
300 /* read runtime database entry */
301 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
302 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
305 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
306 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
307 /* invalid db entry size */
308 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
311 /* read runtime database entry */
312 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
313 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
326 static int psp_init_sriov_microcode(struct psp_context *psp)
328 struct amdgpu_device *adev = psp->adev;
331 switch (adev->ip_versions[MP0_HWIP][0]) {
332 case IP_VERSION(9, 0, 0):
333 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
334 ret = psp_init_cap_microcode(psp, "vega10");
336 case IP_VERSION(11, 0, 9):
337 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
338 ret = psp_init_cap_microcode(psp, "navi12");
340 case IP_VERSION(11, 0, 7):
341 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
342 ret = psp_init_cap_microcode(psp, "sienna_cichlid");
344 case IP_VERSION(13, 0, 2):
345 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
346 ret = psp_init_cap_microcode(psp, "aldebaran");
347 ret &= psp_init_ta_microcode(psp, "aldebaran");
349 case IP_VERSION(13, 0, 0):
350 adev->virt.autoload_ucode_id = 0;
352 case IP_VERSION(13, 0, 10):
353 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
362 static int psp_sw_init(void *handle)
364 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
365 struct psp_context *psp = &adev->psp;
367 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
368 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
369 struct psp_runtime_scpm_entry scpm_entry;
371 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
373 DRM_ERROR("Failed to allocate memory to command buffer!\n");
377 if (amdgpu_sriov_vf(adev))
378 ret = psp_init_sriov_microcode(psp);
380 ret = psp_init_microcode(psp);
382 DRM_ERROR("Failed to load psp firmware!\n");
386 adev->psp.xgmi_context.supports_extended_data =
387 !adev->gmc.xgmi.connected_to_cpu &&
388 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
390 memset(&scpm_entry, 0, sizeof(scpm_entry));
391 if ((psp_get_runtime_db_entry(adev,
392 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
394 (SCPM_DISABLE != scpm_entry.scpm_status)) {
395 adev->scpm_enabled = true;
396 adev->scpm_status = scpm_entry.scpm_status;
398 adev->scpm_enabled = false;
399 adev->scpm_status = SCPM_DISABLE;
402 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
404 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
405 if (psp_get_runtime_db_entry(adev,
406 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
408 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
409 if ((psp->boot_cfg_bitmask) &
410 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
411 /* If psp runtime database exists, then
412 * only enable two stage memory training
413 * when TWO_STAGE_DRAM_TRAINING bit is set
414 * in runtime database */
415 mem_training_ctx->enable_mem_training = true;
419 /* If psp runtime database doesn't exist or
420 * is invalid, force enable two stage memory
422 mem_training_ctx->enable_mem_training = true;
425 if (mem_training_ctx->enable_mem_training) {
426 ret = psp_memory_training_init(psp);
428 DRM_ERROR("Failed to initialize memory training!\n");
432 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
434 DRM_ERROR("Failed to process memory training!\n");
439 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
440 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
441 ret= psp_sysfs_init(adev);
447 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
448 amdgpu_sriov_vf(adev) ?
449 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
451 &psp->fw_pri_mc_addr,
456 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
457 AMDGPU_GEM_DOMAIN_VRAM,
459 &psp->fence_buf_mc_addr,
464 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
465 AMDGPU_GEM_DOMAIN_VRAM,
466 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
467 (void **)&psp->cmd_buf_mem);
474 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
475 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
477 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
478 &psp->fence_buf_mc_addr, &psp->fence_buf);
482 static int psp_sw_fini(void *handle)
484 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
485 struct psp_context *psp = &adev->psp;
486 struct psp_gfx_cmd_resp *cmd = psp->cmd;
488 psp_memory_training_fini(psp);
490 release_firmware(psp->sos_fw);
494 release_firmware(psp->asd_fw);
498 release_firmware(psp->ta_fw);
502 release_firmware(psp->cap_fw);
506 release_firmware(psp->toc_fw);
509 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
510 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
511 psp_sysfs_fini(adev);
516 if (psp->km_ring.ring_mem)
517 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
518 &psp->km_ring.ring_mem_mc_addr,
519 (void **)&psp->km_ring.ring_mem);
521 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
522 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
523 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
524 &psp->fence_buf_mc_addr, &psp->fence_buf);
525 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
526 (void **)&psp->cmd_buf_mem);
531 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
532 uint32_t reg_val, uint32_t mask, bool check_changed)
536 struct amdgpu_device *adev = psp->adev;
538 if (psp->adev->no_hw_access)
541 for (i = 0; i < adev->usec_timeout; i++) {
542 val = RREG32(reg_index);
547 if ((val & mask) == reg_val)
556 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
559 case GFX_CMD_ID_LOAD_TA:
561 case GFX_CMD_ID_UNLOAD_TA:
563 case GFX_CMD_ID_INVOKE_CMD:
565 case GFX_CMD_ID_LOAD_ASD:
567 case GFX_CMD_ID_SETUP_TMR:
569 case GFX_CMD_ID_LOAD_IP_FW:
571 case GFX_CMD_ID_DESTROY_TMR:
572 return "DESTROY_TMR";
573 case GFX_CMD_ID_SAVE_RESTORE:
574 return "SAVE_RESTORE_IP_FW";
575 case GFX_CMD_ID_SETUP_VMR:
577 case GFX_CMD_ID_DESTROY_VMR:
578 return "DESTROY_VMR";
579 case GFX_CMD_ID_PROG_REG:
581 case GFX_CMD_ID_GET_FW_ATTESTATION:
582 return "GET_FW_ATTESTATION";
583 case GFX_CMD_ID_LOAD_TOC:
584 return "ID_LOAD_TOC";
585 case GFX_CMD_ID_AUTOLOAD_RLC:
586 return "AUTOLOAD_RLC";
587 case GFX_CMD_ID_BOOT_CFG:
590 return "UNKNOWN CMD";
595 psp_cmd_submit_buf(struct psp_context *psp,
596 struct amdgpu_firmware_info *ucode,
597 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
602 bool ras_intr = false;
603 bool skip_unsupport = false;
605 if (psp->adev->no_hw_access)
608 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
611 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
613 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
615 index = atomic_inc_return(&psp->fence_value);
616 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
618 atomic_dec(&psp->fence_value);
622 amdgpu_device_invalidate_hdp(psp->adev, NULL);
623 while (*((unsigned int *)psp->fence_buf) != index) {
627 * Shouldn't wait for timeout when err_event_athub occurs,
628 * because gpu reset thread triggered and lock resource should
629 * be released for psp resume sequence.
631 ras_intr = amdgpu_ras_intr_triggered();
634 usleep_range(10, 100);
635 amdgpu_device_invalidate_hdp(psp->adev, NULL);
638 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
639 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
640 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
642 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
644 /* In some cases, psp response status is not 0 even there is no
645 * problem while the command is submitted. Some version of PSP FW
646 * doesn't write 0 to that field.
647 * So here we would like to only print a warning instead of an error
648 * during psp initialization to avoid breaking hw_init and it doesn't
651 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
653 DRM_WARN("failed to load ucode %s(0x%X) ",
654 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
655 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
656 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
657 psp->cmd_buf_mem->resp.status);
658 /* If any firmware (including CAP) load fails under SRIOV, it should
659 * return failure to stop the VF from initializing.
660 * Also return failure in case of timeout
662 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
669 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
670 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
678 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
680 struct psp_gfx_cmd_resp *cmd = psp->cmd;
682 mutex_lock(&psp->mutex);
684 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
689 static void release_psp_cmd_buf(struct psp_context *psp)
691 mutex_unlock(&psp->mutex);
694 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
695 struct psp_gfx_cmd_resp *cmd,
696 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
698 struct amdgpu_device *adev = psp->adev;
699 uint32_t size = amdgpu_bo_size(tmr_bo);
700 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
702 if (amdgpu_sriov_vf(psp->adev))
703 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
705 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
706 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
707 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
708 cmd->cmd.cmd_setup_tmr.buf_size = size;
709 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
710 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
711 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
714 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
715 uint64_t pri_buf_mc, uint32_t size)
717 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
718 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
719 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
720 cmd->cmd.cmd_load_toc.toc_size = size;
723 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
724 static int psp_load_toc(struct psp_context *psp,
728 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
730 /* Copy toc to psp firmware private buffer */
731 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
733 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
735 ret = psp_cmd_submit_buf(psp, NULL, cmd,
736 psp->fence_buf_mc_addr);
738 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
740 release_psp_cmd_buf(psp);
745 /* Set up Trusted Memory Region */
746 static int psp_tmr_init(struct psp_context *psp)
754 * According to HW engineer, they prefer the TMR address be "naturally
755 * aligned" , e.g. the start address be an integer divide of TMR size.
757 * Note: this memory need be reserved till the driver
760 tmr_size = PSP_TMR_SIZE(psp->adev);
762 /* For ASICs support RLC autoload, psp will parse the toc
763 * and calculate the total size of TMR needed */
764 if (!amdgpu_sriov_vf(psp->adev) &&
765 psp->toc.start_addr &&
766 psp->toc.size_bytes &&
768 ret = psp_load_toc(psp, &tmr_size);
770 DRM_ERROR("Failed to load toc\n");
776 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
777 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_ALIGNMENT,
778 AMDGPU_GEM_DOMAIN_VRAM,
779 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
785 static bool psp_skip_tmr(struct psp_context *psp)
787 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
788 case IP_VERSION(11, 0, 9):
789 case IP_VERSION(11, 0, 7):
790 case IP_VERSION(13, 0, 2):
791 case IP_VERSION(13, 0, 10):
798 static int psp_tmr_load(struct psp_context *psp)
801 struct psp_gfx_cmd_resp *cmd;
803 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
804 * Already set up by host driver.
806 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
809 cmd = acquire_psp_cmd_buf(psp);
811 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
812 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
813 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
815 ret = psp_cmd_submit_buf(psp, NULL, cmd,
816 psp->fence_buf_mc_addr);
818 release_psp_cmd_buf(psp);
823 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
824 struct psp_gfx_cmd_resp *cmd)
826 if (amdgpu_sriov_vf(psp->adev))
827 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
829 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
832 static int psp_tmr_unload(struct psp_context *psp)
835 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
837 psp_prep_tmr_unload_cmd_buf(psp, cmd);
838 dev_info(psp->adev->dev, "free PSP TMR buffer\n");
840 ret = psp_cmd_submit_buf(psp, NULL, cmd,
841 psp->fence_buf_mc_addr);
843 release_psp_cmd_buf(psp);
848 static int psp_tmr_terminate(struct psp_context *psp)
850 return psp_tmr_unload(psp);
853 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
854 uint64_t *output_ptr)
857 struct psp_gfx_cmd_resp *cmd;
862 if (amdgpu_sriov_vf(psp->adev))
865 cmd = acquire_psp_cmd_buf(psp);
867 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
869 ret = psp_cmd_submit_buf(psp, NULL, cmd,
870 psp->fence_buf_mc_addr);
873 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
874 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
877 release_psp_cmd_buf(psp);
882 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
884 struct psp_context *psp = &adev->psp;
885 struct psp_gfx_cmd_resp *cmd;
888 if (amdgpu_sriov_vf(adev))
891 cmd = acquire_psp_cmd_buf(psp);
893 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
894 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
896 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
899 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
902 release_psp_cmd_buf(psp);
907 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
910 struct psp_context *psp = &adev->psp;
911 struct psp_gfx_cmd_resp *cmd;
913 if (amdgpu_sriov_vf(adev))
916 cmd = acquire_psp_cmd_buf(psp);
918 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
919 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
920 cmd->cmd.boot_cfg.boot_config = boot_cfg;
921 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
923 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
925 release_psp_cmd_buf(psp);
930 static int psp_rl_load(struct amdgpu_device *adev)
933 struct psp_context *psp = &adev->psp;
934 struct psp_gfx_cmd_resp *cmd;
936 if (!is_psp_fw_valid(psp->rl))
939 cmd = acquire_psp_cmd_buf(psp);
941 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
942 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
944 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
945 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
946 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
947 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
948 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
950 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
952 release_psp_cmd_buf(psp);
957 static int psp_asd_initialize(struct psp_context *psp)
961 /* If PSP version doesn't match ASD version, asd loading will be failed.
962 * add workaround to bypass it for sriov now.
963 * TODO: add version check to make it common
965 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
968 psp->asd_context.mem_context.shared_mc_addr = 0;
969 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
970 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
972 ret = psp_ta_load(psp, &psp->asd_context);
974 psp->asd_context.initialized = true;
979 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
982 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
983 cmd->cmd.cmd_unload_ta.session_id = session_id;
986 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
989 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
991 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
993 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
995 release_psp_cmd_buf(psp);
1000 static int psp_asd_terminate(struct psp_context *psp)
1004 if (amdgpu_sriov_vf(psp->adev))
1007 if (!psp->asd_context.initialized)
1010 ret = psp_ta_unload(psp, &psp->asd_context);
1012 psp->asd_context.initialized = false;
1017 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1018 uint32_t id, uint32_t value)
1020 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1021 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1022 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1025 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1028 struct psp_gfx_cmd_resp *cmd;
1031 if (reg >= PSP_REG_LAST)
1034 cmd = acquire_psp_cmd_buf(psp);
1036 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1037 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1039 DRM_ERROR("PSP failed to program reg id %d", reg);
1041 release_psp_cmd_buf(psp);
1046 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1048 struct ta_context *context)
1050 cmd->cmd_id = context->ta_load_type;
1051 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1052 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1053 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1055 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1056 lower_32_bits(context->mem_context.shared_mc_addr);
1057 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1058 upper_32_bits(context->mem_context.shared_mc_addr);
1059 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1062 int psp_ta_init_shared_buf(struct psp_context *psp,
1063 struct ta_mem_context *mem_ctx)
1066 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1067 * physical) for ta to host memory
1069 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1070 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1071 &mem_ctx->shared_bo,
1072 &mem_ctx->shared_mc_addr,
1073 &mem_ctx->shared_buf);
1076 static void psp_prep_ta_invoke_indirect_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1078 struct ta_context *context)
1080 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1081 cmd->cmd.cmd_invoke_cmd.session_id = context->session_id;
1082 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1084 cmd->cmd.cmd_invoke_cmd.buf.num_desc = 1;
1085 cmd->cmd.cmd_invoke_cmd.buf.total_size = context->mem_context.shared_mem_size;
1086 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_size = context->mem_context.shared_mem_size;
1087 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_phy_addr_lo =
1088 lower_32_bits(context->mem_context.shared_mc_addr);
1089 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_phy_addr_hi =
1090 upper_32_bits(context->mem_context.shared_mc_addr);
1093 int psp_ta_invoke_indirect(struct psp_context *psp,
1095 struct ta_context *context)
1098 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1100 psp_prep_ta_invoke_indirect_cmd_buf(cmd, ta_cmd_id, context);
1102 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1103 psp->fence_buf_mc_addr);
1105 context->resp_status = cmd->resp.status;
1107 release_psp_cmd_buf(psp);
1112 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1114 uint32_t session_id)
1116 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1117 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1118 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1121 int psp_ta_invoke(struct psp_context *psp,
1123 struct ta_context *context)
1126 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1128 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1130 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1131 psp->fence_buf_mc_addr);
1133 context->resp_status = cmd->resp.status;
1135 release_psp_cmd_buf(psp);
1140 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1143 struct psp_gfx_cmd_resp *cmd;
1145 cmd = acquire_psp_cmd_buf(psp);
1147 psp_copy_fw(psp, context->bin_desc.start_addr,
1148 context->bin_desc.size_bytes);
1150 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1152 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1153 psp->fence_buf_mc_addr);
1155 context->resp_status = cmd->resp.status;
1158 context->session_id = cmd->resp.session_id;
1161 release_psp_cmd_buf(psp);
1166 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1168 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1171 int psp_xgmi_terminate(struct psp_context *psp)
1174 struct amdgpu_device *adev = psp->adev;
1176 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1177 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1178 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1179 adev->gmc.xgmi.connected_to_cpu))
1182 if (!psp->xgmi_context.context.initialized)
1185 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1187 psp->xgmi_context.context.initialized = false;
1192 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1194 struct ta_xgmi_shared_memory *xgmi_cmd;
1198 !psp->xgmi_context.context.bin_desc.size_bytes ||
1199 !psp->xgmi_context.context.bin_desc.start_addr)
1205 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1206 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1208 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1209 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1215 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1217 psp->xgmi_context.context.initialized = true;
1222 /* Initialize XGMI session */
1223 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1224 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1225 xgmi_cmd->flag_extend_link_record = set_extended_data;
1226 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1228 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1233 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1235 struct ta_xgmi_shared_memory *xgmi_cmd;
1238 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1239 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1241 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1243 /* Invoke xgmi ta to get hive id */
1244 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1248 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1253 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1255 struct ta_xgmi_shared_memory *xgmi_cmd;
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));
1261 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1263 /* Invoke xgmi ta to get the node id */
1264 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1268 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1273 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1275 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1276 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1280 * Chips that support extended topology information require the driver to
1281 * reflect topology information in the opposite direction. This is
1282 * because the TA has already exceeded its link record limit and if the
1283 * TA holds bi-directional information, the driver would have to do
1284 * multiple fetches instead of just two.
1286 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1287 struct psp_xgmi_node_info node_info)
1289 struct amdgpu_device *mirror_adev;
1290 struct amdgpu_hive_info *hive;
1291 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1292 uint64_t dst_node_id = node_info.node_id;
1293 uint8_t dst_num_hops = node_info.num_hops;
1294 uint8_t dst_num_links = node_info.num_links;
1296 hive = amdgpu_get_xgmi_hive(psp->adev);
1297 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1298 struct psp_xgmi_topology_info *mirror_top_info;
1301 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1304 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1305 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1306 if (mirror_top_info->nodes[j].node_id != src_node_id)
1309 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1311 * prevent 0 num_links value re-reflection since reflection
1312 * criteria is based on num_hops (direct or indirect).
1316 mirror_top_info->nodes[j].num_links = dst_num_links;
1324 amdgpu_put_xgmi_hive(hive);
1327 int psp_xgmi_get_topology_info(struct psp_context *psp,
1329 struct psp_xgmi_topology_info *topology,
1330 bool get_extended_data)
1332 struct ta_xgmi_shared_memory *xgmi_cmd;
1333 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1334 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1338 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1341 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1342 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1343 xgmi_cmd->flag_extend_link_record = get_extended_data;
1345 /* Fill in the shared memory with topology information as input */
1346 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1347 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1348 topology_info_input->num_nodes = number_devices;
1350 for (i = 0; i < topology_info_input->num_nodes; i++) {
1351 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1352 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1353 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1354 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1357 /* Invoke xgmi ta to get the topology information */
1358 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1362 /* Read the output topology information from the shared memory */
1363 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1364 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1365 for (i = 0; i < topology->num_nodes; i++) {
1366 /* extended data will either be 0 or equal to non-extended data */
1367 if (topology_info_output->nodes[i].num_hops)
1368 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1370 /* non-extended data gets everything here so no need to update */
1371 if (!get_extended_data) {
1372 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1373 topology->nodes[i].is_sharing_enabled =
1374 topology_info_output->nodes[i].is_sharing_enabled;
1375 topology->nodes[i].sdma_engine =
1376 topology_info_output->nodes[i].sdma_engine;
1381 /* Invoke xgmi ta again to get the link information */
1382 if (psp_xgmi_peer_link_info_supported(psp)) {
1383 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1385 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1387 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1392 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1393 for (i = 0; i < topology->num_nodes; i++) {
1394 /* accumulate num_links on extended data */
1395 topology->nodes[i].num_links = get_extended_data ?
1396 topology->nodes[i].num_links +
1397 link_info_output->nodes[i].num_links :
1398 link_info_output->nodes[i].num_links;
1400 /* reflect the topology information for bi-directionality */
1401 if (psp->xgmi_context.supports_extended_data &&
1402 get_extended_data && topology->nodes[i].num_hops)
1403 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1410 int psp_xgmi_set_topology_info(struct psp_context *psp,
1412 struct psp_xgmi_topology_info *topology)
1414 struct ta_xgmi_shared_memory *xgmi_cmd;
1415 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1418 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1421 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1422 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1424 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1425 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1426 topology_info_input->num_nodes = number_devices;
1428 for (i = 0; i < topology_info_input->num_nodes; i++) {
1429 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1430 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1431 topology_info_input->nodes[i].is_sharing_enabled = 1;
1432 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1435 /* Invoke xgmi ta to set topology information */
1436 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1440 static void psp_ras_ta_check_status(struct psp_context *psp)
1442 struct ta_ras_shared_memory *ras_cmd =
1443 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1445 switch (ras_cmd->ras_status) {
1446 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1447 dev_warn(psp->adev->dev,
1448 "RAS WARNING: cmd failed due to unsupported ip\n");
1450 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1451 dev_warn(psp->adev->dev,
1452 "RAS WARNING: cmd failed due to unsupported error injection\n");
1454 case TA_RAS_STATUS__SUCCESS:
1456 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1457 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1458 dev_warn(psp->adev->dev,
1459 "RAS WARNING: Inject error to critical region is not allowed\n");
1462 dev_warn(psp->adev->dev,
1463 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1468 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1470 struct ta_ras_shared_memory *ras_cmd;
1473 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1476 * TODO: bypass the loading in sriov for now
1478 if (amdgpu_sriov_vf(psp->adev))
1481 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1483 if (amdgpu_ras_intr_triggered())
1486 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1488 DRM_WARN("RAS: Unsupported Interface");
1493 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1494 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1496 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1498 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1499 dev_warn(psp->adev->dev,
1500 "RAS internal register access blocked\n");
1502 psp_ras_ta_check_status(psp);
1508 int psp_ras_enable_features(struct psp_context *psp,
1509 union ta_ras_cmd_input *info, bool enable)
1511 struct ta_ras_shared_memory *ras_cmd;
1514 if (!psp->ras_context.context.initialized)
1517 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1518 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1521 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1523 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1525 ras_cmd->ras_in_message = *info;
1527 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1534 int psp_ras_terminate(struct psp_context *psp)
1539 * TODO: bypass the terminate in sriov for now
1541 if (amdgpu_sriov_vf(psp->adev))
1544 if (!psp->ras_context.context.initialized)
1547 ret = psp_ta_unload(psp, &psp->ras_context.context);
1549 psp->ras_context.context.initialized = false;
1554 static int psp_ras_initialize(struct psp_context *psp)
1557 uint32_t boot_cfg = 0xFF;
1558 struct amdgpu_device *adev = psp->adev;
1559 struct ta_ras_shared_memory *ras_cmd;
1562 * TODO: bypass the initialize in sriov for now
1564 if (amdgpu_sriov_vf(adev))
1567 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1568 !adev->psp.ras_context.context.bin_desc.start_addr) {
1569 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1573 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1574 /* query GECC enablement status from boot config
1575 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1577 ret = psp_boot_config_get(adev, &boot_cfg);
1579 dev_warn(adev->dev, "PSP get boot config failed\n");
1581 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1583 dev_info(adev->dev, "GECC is disabled\n");
1585 /* disable GECC in next boot cycle if ras is
1586 * disabled by module parameter amdgpu_ras_enable
1587 * and/or amdgpu_ras_mask, or boot_config_get call
1590 ret = psp_boot_config_set(adev, 0);
1592 dev_warn(adev->dev, "PSP set boot config failed\n");
1594 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1595 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1598 if (1 == boot_cfg) {
1599 dev_info(adev->dev, "GECC is enabled\n");
1601 /* enable GECC in next boot cycle if it is disabled
1602 * in boot config, or force enable GECC if failed to
1603 * get boot configuration
1605 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1607 dev_warn(adev->dev, "PSP set boot config failed\n");
1609 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1614 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1615 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1617 if (!psp->ras_context.context.initialized) {
1618 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1623 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1624 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1626 if (amdgpu_ras_is_poison_mode_supported(adev))
1627 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1628 if (!adev->gmc.xgmi.connected_to_cpu)
1629 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1631 ret = psp_ta_load(psp, &psp->ras_context.context);
1633 if (!ret && !ras_cmd->ras_status)
1634 psp->ras_context.context.initialized = true;
1636 if (ras_cmd->ras_status)
1637 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1638 amdgpu_ras_fini(psp->adev);
1644 int psp_ras_trigger_error(struct psp_context *psp,
1645 struct ta_ras_trigger_error_input *info)
1647 struct ta_ras_shared_memory *ras_cmd;
1650 if (!psp->ras_context.context.initialized)
1653 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1654 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1656 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1657 ras_cmd->ras_in_message.trigger_error = *info;
1659 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1663 /* If err_event_athub occurs error inject was successful, however
1664 return status from TA is no long reliable */
1665 if (amdgpu_ras_intr_triggered())
1668 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1670 else if (ras_cmd->ras_status)
1678 static int psp_hdcp_initialize(struct psp_context *psp)
1683 * TODO: bypass the initialize in sriov for now
1685 if (amdgpu_sriov_vf(psp->adev))
1688 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1689 !psp->hdcp_context.context.bin_desc.start_addr) {
1690 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1694 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1695 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1697 if (!psp->hdcp_context.context.initialized) {
1698 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1703 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1705 psp->hdcp_context.context.initialized = true;
1706 mutex_init(&psp->hdcp_context.mutex);
1712 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1715 * TODO: bypass the loading in sriov for now
1717 if (amdgpu_sriov_vf(psp->adev))
1720 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1723 static int psp_hdcp_terminate(struct psp_context *psp)
1728 * TODO: bypass the terminate in sriov for now
1730 if (amdgpu_sriov_vf(psp->adev))
1733 if (!psp->hdcp_context.context.initialized)
1736 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1738 psp->hdcp_context.context.initialized = false;
1745 static int psp_dtm_initialize(struct psp_context *psp)
1750 * TODO: bypass the initialize in sriov for now
1752 if (amdgpu_sriov_vf(psp->adev))
1755 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1756 !psp->dtm_context.context.bin_desc.start_addr) {
1757 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1761 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1762 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1764 if (!psp->dtm_context.context.initialized) {
1765 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1770 ret = psp_ta_load(psp, &psp->dtm_context.context);
1772 psp->dtm_context.context.initialized = true;
1773 mutex_init(&psp->dtm_context.mutex);
1779 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1782 * TODO: bypass the loading in sriov for now
1784 if (amdgpu_sriov_vf(psp->adev))
1787 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1790 static int psp_dtm_terminate(struct psp_context *psp)
1795 * TODO: bypass the terminate in sriov for now
1797 if (amdgpu_sriov_vf(psp->adev))
1800 if (!psp->dtm_context.context.initialized)
1803 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1805 psp->dtm_context.context.initialized = false;
1812 static int psp_rap_initialize(struct psp_context *psp)
1815 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1818 * TODO: bypass the initialize in sriov for now
1820 if (amdgpu_sriov_vf(psp->adev))
1823 if (!psp->rap_context.context.bin_desc.size_bytes ||
1824 !psp->rap_context.context.bin_desc.start_addr) {
1825 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1829 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1830 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1832 if (!psp->rap_context.context.initialized) {
1833 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1838 ret = psp_ta_load(psp, &psp->rap_context.context);
1840 psp->rap_context.context.initialized = true;
1841 mutex_init(&psp->rap_context.mutex);
1845 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1846 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1847 psp_rap_terminate(psp);
1848 /* free rap shared memory */
1849 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1851 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1860 static int psp_rap_terminate(struct psp_context *psp)
1864 if (!psp->rap_context.context.initialized)
1867 ret = psp_ta_unload(psp, &psp->rap_context.context);
1869 psp->rap_context.context.initialized = false;
1874 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1876 struct ta_rap_shared_memory *rap_cmd;
1879 if (!psp->rap_context.context.initialized)
1882 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1883 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1886 mutex_lock(&psp->rap_context.mutex);
1888 rap_cmd = (struct ta_rap_shared_memory *)
1889 psp->rap_context.context.mem_context.shared_buf;
1890 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1892 rap_cmd->cmd_id = ta_cmd_id;
1893 rap_cmd->validation_method_id = METHOD_A;
1895 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1900 *status = rap_cmd->rap_status;
1903 mutex_unlock(&psp->rap_context.mutex);
1909 /* securedisplay start */
1910 static int psp_securedisplay_initialize(struct psp_context *psp)
1913 struct securedisplay_cmd *securedisplay_cmd;
1916 * TODO: bypass the initialize in sriov for now
1918 if (amdgpu_sriov_vf(psp->adev))
1921 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1922 !psp->securedisplay_context.context.bin_desc.start_addr) {
1923 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1927 psp->securedisplay_context.context.mem_context.shared_mem_size =
1928 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1929 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1931 if (!psp->securedisplay_context.context.initialized) {
1932 ret = psp_ta_init_shared_buf(psp,
1933 &psp->securedisplay_context.context.mem_context);
1938 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1940 psp->securedisplay_context.context.initialized = true;
1941 mutex_init(&psp->securedisplay_context.mutex);
1945 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1946 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1948 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1950 psp_securedisplay_terminate(psp);
1951 /* free securedisplay shared memory */
1952 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1953 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1957 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1958 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1959 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1960 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1966 static int psp_securedisplay_terminate(struct psp_context *psp)
1971 * TODO:bypass the terminate in sriov for now
1973 if (amdgpu_sriov_vf(psp->adev))
1976 if (!psp->securedisplay_context.context.initialized)
1979 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1981 psp->securedisplay_context.context.initialized = false;
1986 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1990 if (!psp->securedisplay_context.context.initialized)
1993 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1994 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1997 mutex_lock(&psp->securedisplay_context.mutex);
1999 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2001 mutex_unlock(&psp->securedisplay_context.mutex);
2005 /* SECUREDISPLAY end */
2007 static int psp_hw_start(struct psp_context *psp)
2009 struct amdgpu_device *adev = psp->adev;
2012 if (!amdgpu_sriov_vf(adev)) {
2013 if ((is_psp_fw_valid(psp->kdb)) &&
2014 (psp->funcs->bootloader_load_kdb != NULL)) {
2015 ret = psp_bootloader_load_kdb(psp);
2017 DRM_ERROR("PSP load kdb failed!\n");
2022 if ((is_psp_fw_valid(psp->spl)) &&
2023 (psp->funcs->bootloader_load_spl != NULL)) {
2024 ret = psp_bootloader_load_spl(psp);
2026 DRM_ERROR("PSP load spl failed!\n");
2031 if ((is_psp_fw_valid(psp->sys)) &&
2032 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2033 ret = psp_bootloader_load_sysdrv(psp);
2035 DRM_ERROR("PSP load sys drv failed!\n");
2040 if ((is_psp_fw_valid(psp->soc_drv)) &&
2041 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2042 ret = psp_bootloader_load_soc_drv(psp);
2044 DRM_ERROR("PSP load soc drv failed!\n");
2049 if ((is_psp_fw_valid(psp->intf_drv)) &&
2050 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2051 ret = psp_bootloader_load_intf_drv(psp);
2053 DRM_ERROR("PSP load intf drv failed!\n");
2058 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2059 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2060 ret = psp_bootloader_load_dbg_drv(psp);
2062 DRM_ERROR("PSP load dbg drv failed!\n");
2067 if ((is_psp_fw_valid(psp->ras_drv)) &&
2068 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2069 ret = psp_bootloader_load_ras_drv(psp);
2071 DRM_ERROR("PSP load ras_drv failed!\n");
2076 if ((is_psp_fw_valid(psp->sos)) &&
2077 (psp->funcs->bootloader_load_sos != NULL)) {
2078 ret = psp_bootloader_load_sos(psp);
2080 DRM_ERROR("PSP load sos failed!\n");
2086 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2088 DRM_ERROR("PSP create ring failed!\n");
2092 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2095 ret = psp_tmr_init(psp);
2097 DRM_ERROR("PSP tmr init failed!\n");
2103 * For ASICs with DF Cstate management centralized
2104 * to PMFW, TMR setup should be performed after PMFW
2105 * loaded and before other non-psp firmware loaded.
2107 if (psp->pmfw_centralized_cstate_management) {
2108 ret = psp_load_smu_fw(psp);
2113 ret = psp_tmr_load(psp);
2115 DRM_ERROR("PSP load tmr failed!\n");
2122 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2123 enum psp_gfx_fw_type *type)
2125 switch (ucode->ucode_id) {
2126 case AMDGPU_UCODE_ID_CAP:
2127 *type = GFX_FW_TYPE_CAP;
2129 case AMDGPU_UCODE_ID_SDMA0:
2130 *type = GFX_FW_TYPE_SDMA0;
2132 case AMDGPU_UCODE_ID_SDMA1:
2133 *type = GFX_FW_TYPE_SDMA1;
2135 case AMDGPU_UCODE_ID_SDMA2:
2136 *type = GFX_FW_TYPE_SDMA2;
2138 case AMDGPU_UCODE_ID_SDMA3:
2139 *type = GFX_FW_TYPE_SDMA3;
2141 case AMDGPU_UCODE_ID_SDMA4:
2142 *type = GFX_FW_TYPE_SDMA4;
2144 case AMDGPU_UCODE_ID_SDMA5:
2145 *type = GFX_FW_TYPE_SDMA5;
2147 case AMDGPU_UCODE_ID_SDMA6:
2148 *type = GFX_FW_TYPE_SDMA6;
2150 case AMDGPU_UCODE_ID_SDMA7:
2151 *type = GFX_FW_TYPE_SDMA7;
2153 case AMDGPU_UCODE_ID_CP_MES:
2154 *type = GFX_FW_TYPE_CP_MES;
2156 case AMDGPU_UCODE_ID_CP_MES_DATA:
2157 *type = GFX_FW_TYPE_MES_STACK;
2159 case AMDGPU_UCODE_ID_CP_MES1:
2160 *type = GFX_FW_TYPE_CP_MES_KIQ;
2162 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2163 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2165 case AMDGPU_UCODE_ID_CP_CE:
2166 *type = GFX_FW_TYPE_CP_CE;
2168 case AMDGPU_UCODE_ID_CP_PFP:
2169 *type = GFX_FW_TYPE_CP_PFP;
2171 case AMDGPU_UCODE_ID_CP_ME:
2172 *type = GFX_FW_TYPE_CP_ME;
2174 case AMDGPU_UCODE_ID_CP_MEC1:
2175 *type = GFX_FW_TYPE_CP_MEC;
2177 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2178 *type = GFX_FW_TYPE_CP_MEC_ME1;
2180 case AMDGPU_UCODE_ID_CP_MEC2:
2181 *type = GFX_FW_TYPE_CP_MEC;
2183 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2184 *type = GFX_FW_TYPE_CP_MEC_ME2;
2186 case AMDGPU_UCODE_ID_RLC_P:
2187 *type = GFX_FW_TYPE_RLC_P;
2189 case AMDGPU_UCODE_ID_RLC_V:
2190 *type = GFX_FW_TYPE_RLC_V;
2192 case AMDGPU_UCODE_ID_RLC_G:
2193 *type = GFX_FW_TYPE_RLC_G;
2195 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2196 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2198 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2199 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2201 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2202 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2204 case AMDGPU_UCODE_ID_RLC_IRAM:
2205 *type = GFX_FW_TYPE_RLC_IRAM;
2207 case AMDGPU_UCODE_ID_RLC_DRAM:
2208 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2210 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2211 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2213 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2214 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2216 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2217 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2219 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2220 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2222 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2223 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2225 case AMDGPU_UCODE_ID_SMC:
2226 *type = GFX_FW_TYPE_SMU;
2228 case AMDGPU_UCODE_ID_PPTABLE:
2229 *type = GFX_FW_TYPE_PPTABLE;
2231 case AMDGPU_UCODE_ID_UVD:
2232 *type = GFX_FW_TYPE_UVD;
2234 case AMDGPU_UCODE_ID_UVD1:
2235 *type = GFX_FW_TYPE_UVD1;
2237 case AMDGPU_UCODE_ID_VCE:
2238 *type = GFX_FW_TYPE_VCE;
2240 case AMDGPU_UCODE_ID_VCN:
2241 *type = GFX_FW_TYPE_VCN;
2243 case AMDGPU_UCODE_ID_VCN1:
2244 *type = GFX_FW_TYPE_VCN1;
2246 case AMDGPU_UCODE_ID_DMCU_ERAM:
2247 *type = GFX_FW_TYPE_DMCU_ERAM;
2249 case AMDGPU_UCODE_ID_DMCU_INTV:
2250 *type = GFX_FW_TYPE_DMCU_ISR;
2252 case AMDGPU_UCODE_ID_VCN0_RAM:
2253 *type = GFX_FW_TYPE_VCN0_RAM;
2255 case AMDGPU_UCODE_ID_VCN1_RAM:
2256 *type = GFX_FW_TYPE_VCN1_RAM;
2258 case AMDGPU_UCODE_ID_DMCUB:
2259 *type = GFX_FW_TYPE_DMUB;
2261 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2262 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2264 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2265 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2267 case AMDGPU_UCODE_ID_IMU_I:
2268 *type = GFX_FW_TYPE_IMU_I;
2270 case AMDGPU_UCODE_ID_IMU_D:
2271 *type = GFX_FW_TYPE_IMU_D;
2273 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2274 *type = GFX_FW_TYPE_RS64_PFP;
2276 case AMDGPU_UCODE_ID_CP_RS64_ME:
2277 *type = GFX_FW_TYPE_RS64_ME;
2279 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2280 *type = GFX_FW_TYPE_RS64_MEC;
2282 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2283 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2285 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2286 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2288 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2289 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2291 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2292 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2294 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2295 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2297 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2298 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2300 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2301 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2303 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2304 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2306 case AMDGPU_UCODE_ID_MAXIMUM:
2314 static void psp_print_fw_hdr(struct psp_context *psp,
2315 struct amdgpu_firmware_info *ucode)
2317 struct amdgpu_device *adev = psp->adev;
2318 struct common_firmware_header *hdr;
2320 switch (ucode->ucode_id) {
2321 case AMDGPU_UCODE_ID_SDMA0:
2322 case AMDGPU_UCODE_ID_SDMA1:
2323 case AMDGPU_UCODE_ID_SDMA2:
2324 case AMDGPU_UCODE_ID_SDMA3:
2325 case AMDGPU_UCODE_ID_SDMA4:
2326 case AMDGPU_UCODE_ID_SDMA5:
2327 case AMDGPU_UCODE_ID_SDMA6:
2328 case AMDGPU_UCODE_ID_SDMA7:
2329 hdr = (struct common_firmware_header *)
2330 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2331 amdgpu_ucode_print_sdma_hdr(hdr);
2333 case AMDGPU_UCODE_ID_CP_CE:
2334 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2335 amdgpu_ucode_print_gfx_hdr(hdr);
2337 case AMDGPU_UCODE_ID_CP_PFP:
2338 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2339 amdgpu_ucode_print_gfx_hdr(hdr);
2341 case AMDGPU_UCODE_ID_CP_ME:
2342 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2343 amdgpu_ucode_print_gfx_hdr(hdr);
2345 case AMDGPU_UCODE_ID_CP_MEC1:
2346 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2347 amdgpu_ucode_print_gfx_hdr(hdr);
2349 case AMDGPU_UCODE_ID_RLC_G:
2350 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2351 amdgpu_ucode_print_rlc_hdr(hdr);
2353 case AMDGPU_UCODE_ID_SMC:
2354 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2355 amdgpu_ucode_print_smc_hdr(hdr);
2362 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2363 struct psp_gfx_cmd_resp *cmd)
2366 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2368 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2369 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2370 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2371 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2373 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2375 DRM_ERROR("Unknown firmware type\n");
2380 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2381 struct amdgpu_firmware_info *ucode)
2384 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2386 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2388 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2389 psp->fence_buf_mc_addr);
2392 release_psp_cmd_buf(psp);
2397 static int psp_load_smu_fw(struct psp_context *psp)
2400 struct amdgpu_device *adev = psp->adev;
2401 struct amdgpu_firmware_info *ucode =
2402 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2403 struct amdgpu_ras *ras = psp->ras_context.ras;
2406 * Skip SMU FW reloading in case of using BACO for runpm only,
2407 * as SMU is always alive.
2409 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2412 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2415 if ((amdgpu_in_reset(adev) &&
2416 ras && adev->ras_enabled &&
2417 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2418 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2419 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2421 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2425 ret = psp_execute_non_psp_fw_load(psp, ucode);
2428 DRM_ERROR("PSP load smu failed!\n");
2433 static bool fw_load_skip_check(struct psp_context *psp,
2434 struct amdgpu_firmware_info *ucode)
2436 if (!ucode->fw || !ucode->ucode_size)
2439 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2440 (psp_smu_reload_quirk(psp) ||
2441 psp->autoload_supported ||
2442 psp->pmfw_centralized_cstate_management))
2445 if (amdgpu_sriov_vf(psp->adev) &&
2446 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2449 if (psp->autoload_supported &&
2450 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2451 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2452 /* skip mec JT when autoload is enabled */
2458 int psp_load_fw_list(struct psp_context *psp,
2459 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2462 struct amdgpu_firmware_info *ucode;
2464 for (i = 0; i < ucode_count; ++i) {
2465 ucode = ucode_list[i];
2466 psp_print_fw_hdr(psp, ucode);
2467 ret = psp_execute_non_psp_fw_load(psp, ucode);
2474 static int psp_load_non_psp_fw(struct psp_context *psp)
2477 struct amdgpu_firmware_info *ucode;
2478 struct amdgpu_device *adev = psp->adev;
2480 if (psp->autoload_supported &&
2481 !psp->pmfw_centralized_cstate_management) {
2482 ret = psp_load_smu_fw(psp);
2487 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2488 ucode = &adev->firmware.ucode[i];
2490 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2491 !fw_load_skip_check(psp, ucode)) {
2492 ret = psp_load_smu_fw(psp);
2498 if (fw_load_skip_check(psp, ucode))
2501 if (psp->autoload_supported &&
2502 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2503 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2504 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2505 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2506 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2507 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2508 /* PSP only receive one SDMA fw for sienna_cichlid,
2509 * as all four sdma fw are same */
2512 psp_print_fw_hdr(psp, ucode);
2514 ret = psp_execute_non_psp_fw_load(psp, ucode);
2518 /* Start rlc autoload after psp recieved all the gfx firmware */
2519 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2520 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2521 ret = psp_rlc_autoload_start(psp);
2523 DRM_ERROR("Failed to start rlc autoload\n");
2532 static int psp_load_fw(struct amdgpu_device *adev)
2535 struct psp_context *psp = &adev->psp;
2537 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2538 /* should not destroy ring, only stop */
2539 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2541 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2543 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2545 DRM_ERROR("PSP ring init failed!\n");
2550 ret = psp_hw_start(psp);
2554 ret = psp_load_non_psp_fw(psp);
2558 ret = psp_asd_initialize(psp);
2560 DRM_ERROR("PSP load asd failed!\n");
2564 ret = psp_rl_load(adev);
2566 DRM_ERROR("PSP load RL failed!\n");
2570 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2571 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2572 ret = psp_xgmi_initialize(psp, false, true);
2573 /* Warning the XGMI seesion initialize failure
2574 * Instead of stop driver initialization
2577 dev_err(psp->adev->dev,
2578 "XGMI: Failed to initialize XGMI session\n");
2583 ret = psp_ras_initialize(psp);
2585 dev_err(psp->adev->dev,
2586 "RAS: Failed to initialize RAS\n");
2588 ret = psp_hdcp_initialize(psp);
2590 dev_err(psp->adev->dev,
2591 "HDCP: Failed to initialize HDCP\n");
2593 ret = psp_dtm_initialize(psp);
2595 dev_err(psp->adev->dev,
2596 "DTM: Failed to initialize DTM\n");
2598 ret = psp_rap_initialize(psp);
2600 dev_err(psp->adev->dev,
2601 "RAP: Failed to initialize RAP\n");
2603 ret = psp_securedisplay_initialize(psp);
2605 dev_err(psp->adev->dev,
2606 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2612 psp_free_shared_bufs(psp);
2615 * all cleanup jobs (xgmi terminate, ras terminate,
2616 * ring destroy, cmd/fence/fw buffers destory,
2617 * psp->cmd destory) are delayed to psp_hw_fini
2619 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2623 static int psp_hw_init(void *handle)
2626 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2628 mutex_lock(&adev->firmware.mutex);
2630 * This sequence is just used on hw_init only once, no need on
2633 ret = amdgpu_ucode_init_bo(adev);
2637 ret = psp_load_fw(adev);
2639 DRM_ERROR("PSP firmware loading failed\n");
2643 mutex_unlock(&adev->firmware.mutex);
2647 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2648 mutex_unlock(&adev->firmware.mutex);
2652 static int psp_hw_fini(void *handle)
2654 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2655 struct psp_context *psp = &adev->psp;
2658 psp_ras_terminate(psp);
2659 psp_securedisplay_terminate(psp);
2660 psp_rap_terminate(psp);
2661 psp_dtm_terminate(psp);
2662 psp_hdcp_terminate(psp);
2664 if (adev->gmc.xgmi.num_physical_nodes > 1)
2665 psp_xgmi_terminate(psp);
2668 psp_asd_terminate(psp);
2669 psp_tmr_terminate(psp);
2671 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2673 psp_free_shared_bufs(psp);
2678 static int psp_suspend(void *handle)
2681 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2682 struct psp_context *psp = &adev->psp;
2684 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2685 psp->xgmi_context.context.initialized) {
2686 ret = psp_xgmi_terminate(psp);
2688 DRM_ERROR("Failed to terminate xgmi ta\n");
2694 ret = psp_ras_terminate(psp);
2696 DRM_ERROR("Failed to terminate ras ta\n");
2699 ret = psp_hdcp_terminate(psp);
2701 DRM_ERROR("Failed to terminate hdcp ta\n");
2704 ret = psp_dtm_terminate(psp);
2706 DRM_ERROR("Failed to terminate dtm ta\n");
2709 ret = psp_rap_terminate(psp);
2711 DRM_ERROR("Failed to terminate rap ta\n");
2714 ret = psp_securedisplay_terminate(psp);
2716 DRM_ERROR("Failed to terminate securedisplay ta\n");
2721 ret = psp_asd_terminate(psp);
2723 DRM_ERROR("Failed to terminate asd\n");
2727 ret = psp_tmr_terminate(psp);
2729 DRM_ERROR("Failed to terminate tmr\n");
2733 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2735 DRM_ERROR("PSP ring stop failed\n");
2742 static int psp_resume(void *handle)
2745 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2746 struct psp_context *psp = &adev->psp;
2748 DRM_INFO("PSP is resuming...\n");
2750 if (psp->mem_train_ctx.enable_mem_training) {
2751 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2753 DRM_ERROR("Failed to process memory training!\n");
2758 mutex_lock(&adev->firmware.mutex);
2760 ret = psp_hw_start(psp);
2764 ret = psp_load_non_psp_fw(psp);
2768 ret = psp_asd_initialize(psp);
2770 DRM_ERROR("PSP load asd failed!\n");
2774 ret = psp_rl_load(adev);
2776 dev_err(adev->dev, "PSP load RL failed!\n");
2780 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2781 ret = psp_xgmi_initialize(psp, false, true);
2782 /* Warning the XGMI seesion initialize failure
2783 * Instead of stop driver initialization
2786 dev_err(psp->adev->dev,
2787 "XGMI: Failed to initialize XGMI session\n");
2791 ret = psp_ras_initialize(psp);
2793 dev_err(psp->adev->dev,
2794 "RAS: Failed to initialize RAS\n");
2796 ret = psp_hdcp_initialize(psp);
2798 dev_err(psp->adev->dev,
2799 "HDCP: Failed to initialize HDCP\n");
2801 ret = psp_dtm_initialize(psp);
2803 dev_err(psp->adev->dev,
2804 "DTM: Failed to initialize DTM\n");
2806 ret = psp_rap_initialize(psp);
2808 dev_err(psp->adev->dev,
2809 "RAP: Failed to initialize RAP\n");
2811 ret = psp_securedisplay_initialize(psp);
2813 dev_err(psp->adev->dev,
2814 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2817 mutex_unlock(&adev->firmware.mutex);
2822 DRM_ERROR("PSP resume failed\n");
2823 mutex_unlock(&adev->firmware.mutex);
2827 int psp_gpu_reset(struct amdgpu_device *adev)
2831 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2834 mutex_lock(&adev->psp.mutex);
2835 ret = psp_mode1_reset(&adev->psp);
2836 mutex_unlock(&adev->psp.mutex);
2841 int psp_rlc_autoload_start(struct psp_context *psp)
2844 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2846 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2848 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2849 psp->fence_buf_mc_addr);
2851 release_psp_cmd_buf(psp);
2856 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2857 uint64_t cmd_gpu_addr, int cmd_size)
2859 struct amdgpu_firmware_info ucode = {0};
2861 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2862 AMDGPU_UCODE_ID_VCN0_RAM;
2863 ucode.mc_addr = cmd_gpu_addr;
2864 ucode.ucode_size = cmd_size;
2866 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2869 int psp_ring_cmd_submit(struct psp_context *psp,
2870 uint64_t cmd_buf_mc_addr,
2871 uint64_t fence_mc_addr,
2874 unsigned int psp_write_ptr_reg = 0;
2875 struct psp_gfx_rb_frame *write_frame;
2876 struct psp_ring *ring = &psp->km_ring;
2877 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2878 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2879 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2880 struct amdgpu_device *adev = psp->adev;
2881 uint32_t ring_size_dw = ring->ring_size / 4;
2882 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2884 /* KM (GPCOM) prepare write pointer */
2885 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2887 /* Update KM RB frame pointer to new frame */
2888 /* write_frame ptr increments by size of rb_frame in bytes */
2889 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2890 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2891 write_frame = ring_buffer_start;
2893 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2894 /* Check invalid write_frame ptr address */
2895 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2896 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2897 ring_buffer_start, ring_buffer_end, write_frame);
2898 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2902 /* Initialize KM RB frame */
2903 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2905 /* Update KM RB frame */
2906 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2907 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2908 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2909 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2910 write_frame->fence_value = index;
2911 amdgpu_device_flush_hdp(adev, NULL);
2913 /* Update the write Pointer in DWORDs */
2914 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2915 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2919 int psp_init_asd_microcode(struct psp_context *psp,
2920 const char *chip_name)
2922 struct amdgpu_device *adev = psp->adev;
2923 char fw_name[PSP_FW_NAME_LEN];
2924 const struct psp_firmware_header_v1_0 *asd_hdr;
2928 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2932 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2933 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2937 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2941 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2942 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2943 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2944 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2945 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2946 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2949 dev_err(adev->dev, "fail to initialize asd microcode\n");
2950 release_firmware(adev->psp.asd_fw);
2951 adev->psp.asd_fw = NULL;
2955 int psp_init_toc_microcode(struct psp_context *psp,
2956 const char *chip_name)
2958 struct amdgpu_device *adev = psp->adev;
2959 char fw_name[PSP_FW_NAME_LEN];
2960 const struct psp_firmware_header_v1_0 *toc_hdr;
2964 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2968 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2969 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2973 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2977 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2978 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2979 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2980 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2981 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2982 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2985 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2986 release_firmware(adev->psp.toc_fw);
2987 adev->psp.toc_fw = NULL;
2991 static int parse_sos_bin_descriptor(struct psp_context *psp,
2992 const struct psp_fw_bin_desc *desc,
2993 const struct psp_firmware_header_v2_0 *sos_hdr)
2995 uint8_t *ucode_start_addr = NULL;
2997 if (!psp || !desc || !sos_hdr)
3000 ucode_start_addr = (uint8_t *)sos_hdr +
3001 le32_to_cpu(desc->offset_bytes) +
3002 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3004 switch (desc->fw_type) {
3005 case PSP_FW_TYPE_PSP_SOS:
3006 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3007 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3008 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3009 psp->sos.start_addr = ucode_start_addr;
3011 case PSP_FW_TYPE_PSP_SYS_DRV:
3012 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3013 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3014 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3015 psp->sys.start_addr = ucode_start_addr;
3017 case PSP_FW_TYPE_PSP_KDB:
3018 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3019 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3020 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3021 psp->kdb.start_addr = ucode_start_addr;
3023 case PSP_FW_TYPE_PSP_TOC:
3024 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3025 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3026 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3027 psp->toc.start_addr = ucode_start_addr;
3029 case PSP_FW_TYPE_PSP_SPL:
3030 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3031 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3032 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3033 psp->spl.start_addr = ucode_start_addr;
3035 case PSP_FW_TYPE_PSP_RL:
3036 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3037 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3038 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3039 psp->rl.start_addr = ucode_start_addr;
3041 case PSP_FW_TYPE_PSP_SOC_DRV:
3042 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3043 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3044 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3045 psp->soc_drv.start_addr = ucode_start_addr;
3047 case PSP_FW_TYPE_PSP_INTF_DRV:
3048 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3049 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3050 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3051 psp->intf_drv.start_addr = ucode_start_addr;
3053 case PSP_FW_TYPE_PSP_DBG_DRV:
3054 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3055 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3056 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3057 psp->dbg_drv.start_addr = ucode_start_addr;
3059 case PSP_FW_TYPE_PSP_RAS_DRV:
3060 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3061 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3062 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3063 psp->ras_drv.start_addr = ucode_start_addr;
3066 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3073 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3075 const struct psp_firmware_header_v1_0 *sos_hdr;
3076 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3077 uint8_t *ucode_array_start_addr;
3079 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3080 ucode_array_start_addr = (uint8_t *)sos_hdr +
3081 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3083 if (adev->gmc.xgmi.connected_to_cpu ||
3084 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3085 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3086 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3088 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3089 adev->psp.sys.start_addr = ucode_array_start_addr;
3091 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3092 adev->psp.sos.start_addr = ucode_array_start_addr +
3093 le32_to_cpu(sos_hdr->sos.offset_bytes);
3095 /* Load alternate PSP SOS FW */
3096 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3098 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3099 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3101 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3102 adev->psp.sys.start_addr = ucode_array_start_addr +
3103 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3105 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3106 adev->psp.sos.start_addr = ucode_array_start_addr +
3107 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3110 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3111 dev_warn(adev->dev, "PSP SOS FW not available");
3118 int psp_init_sos_microcode(struct psp_context *psp,
3119 const char *chip_name)
3121 struct amdgpu_device *adev = psp->adev;
3122 char fw_name[PSP_FW_NAME_LEN];
3123 const struct psp_firmware_header_v1_0 *sos_hdr;
3124 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3125 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3126 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3127 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3129 uint8_t *ucode_array_start_addr;
3133 dev_err(adev->dev, "invalid chip name for sos microcode\n");
3137 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3138 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
3142 err = amdgpu_ucode_validate(adev->psp.sos_fw);
3146 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3147 ucode_array_start_addr = (uint8_t *)sos_hdr +
3148 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3149 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3151 switch (sos_hdr->header.header_version_major) {
3153 err = psp_init_sos_base_fw(adev);
3157 if (sos_hdr->header.header_version_minor == 1) {
3158 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3159 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3160 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3161 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3162 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3163 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3164 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3166 if (sos_hdr->header.header_version_minor == 2) {
3167 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3168 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3169 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3170 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3172 if (sos_hdr->header.header_version_minor == 3) {
3173 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3174 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3175 adev->psp.toc.start_addr = ucode_array_start_addr +
3176 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3177 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3178 adev->psp.kdb.start_addr = ucode_array_start_addr +
3179 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3180 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3181 adev->psp.spl.start_addr = ucode_array_start_addr +
3182 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3183 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3184 adev->psp.rl.start_addr = ucode_array_start_addr +
3185 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3189 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3191 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3192 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3197 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3198 err = parse_sos_bin_descriptor(psp,
3199 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3207 "unsupported psp sos firmware\n");
3215 "failed to init sos firmware\n");
3216 release_firmware(adev->psp.sos_fw);
3217 adev->psp.sos_fw = NULL;
3222 static int parse_ta_bin_descriptor(struct psp_context *psp,
3223 const struct psp_fw_bin_desc *desc,
3224 const struct ta_firmware_header_v2_0 *ta_hdr)
3226 uint8_t *ucode_start_addr = NULL;
3228 if (!psp || !desc || !ta_hdr)
3231 ucode_start_addr = (uint8_t *)ta_hdr +
3232 le32_to_cpu(desc->offset_bytes) +
3233 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3235 switch (desc->fw_type) {
3236 case TA_FW_TYPE_PSP_ASD:
3237 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3238 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3239 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3240 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3242 case TA_FW_TYPE_PSP_XGMI:
3243 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3244 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3245 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3247 case TA_FW_TYPE_PSP_RAS:
3248 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3249 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3250 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3252 case TA_FW_TYPE_PSP_HDCP:
3253 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3254 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3255 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3257 case TA_FW_TYPE_PSP_DTM:
3258 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3259 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3260 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3262 case TA_FW_TYPE_PSP_RAP:
3263 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3264 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3265 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3267 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3268 psp->securedisplay_context.context.bin_desc.fw_version =
3269 le32_to_cpu(desc->fw_version);
3270 psp->securedisplay_context.context.bin_desc.size_bytes =
3271 le32_to_cpu(desc->size_bytes);
3272 psp->securedisplay_context.context.bin_desc.start_addr =
3276 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3283 int psp_init_ta_microcode(struct psp_context *psp,
3284 const char *chip_name)
3286 struct amdgpu_device *adev = psp->adev;
3287 char fw_name[PSP_FW_NAME_LEN];
3288 const struct ta_firmware_header_v2_0 *ta_hdr;
3293 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3297 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3298 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3302 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3306 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3308 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3309 dev_err(adev->dev, "unsupported TA header version\n");
3314 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3315 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3320 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3321 err = parse_ta_bin_descriptor(psp,
3322 &ta_hdr->ta_fw_bin[ta_index],
3330 dev_err(adev->dev, "fail to initialize ta microcode\n");
3331 release_firmware(adev->psp.ta_fw);
3332 adev->psp.ta_fw = NULL;
3336 int psp_init_cap_microcode(struct psp_context *psp,
3337 const char *chip_name)
3339 struct amdgpu_device *adev = psp->adev;
3340 char fw_name[PSP_FW_NAME_LEN];
3341 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3342 struct amdgpu_firmware_info *info = NULL;
3346 dev_err(adev->dev, "invalid chip name for cap microcode\n");
3350 if (!amdgpu_sriov_vf(adev)) {
3351 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3355 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3356 err = request_firmware(&adev->psp.cap_fw, fw_name, adev->dev);
3358 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3363 err = amdgpu_ucode_validate(adev->psp.cap_fw);
3365 dev_err(adev->dev, "fail to initialize cap microcode\n");
3369 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3370 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3371 info->fw = adev->psp.cap_fw;
3372 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3373 adev->psp.cap_fw->data;
3374 adev->firmware.fw_size += ALIGN(
3375 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3376 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3377 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3378 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3383 release_firmware(adev->psp.cap_fw);
3384 adev->psp.cap_fw = NULL;
3388 static int psp_set_clockgating_state(void *handle,
3389 enum amd_clockgating_state state)
3394 static int psp_set_powergating_state(void *handle,
3395 enum amd_powergating_state state)
3400 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3401 struct device_attribute *attr,
3404 struct drm_device *ddev = dev_get_drvdata(dev);
3405 struct amdgpu_device *adev = drm_to_adev(ddev);
3409 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3410 DRM_INFO("PSP block is not ready yet.");
3414 mutex_lock(&adev->psp.mutex);
3415 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3416 mutex_unlock(&adev->psp.mutex);
3419 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3423 return sysfs_emit(buf, "%x\n", fw_ver);
3426 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3427 struct device_attribute *attr,
3431 struct drm_device *ddev = dev_get_drvdata(dev);
3432 struct amdgpu_device *adev = drm_to_adev(ddev);
3435 const struct firmware *usbc_pd_fw;
3436 struct amdgpu_bo *fw_buf_bo = NULL;
3437 uint64_t fw_pri_mc_addr;
3438 void *fw_pri_cpu_addr;
3440 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3441 DRM_INFO("PSP block is not ready yet.");
3445 if (!drm_dev_enter(ddev, &idx))
3448 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3449 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3453 /* LFB address which is aligned to 1MB boundary per PSP request */
3454 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3455 AMDGPU_GEM_DOMAIN_VRAM,
3462 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3464 mutex_lock(&adev->psp.mutex);
3465 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3466 mutex_unlock(&adev->psp.mutex);
3468 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3471 release_firmware(usbc_pd_fw);
3474 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3482 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3486 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3489 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3490 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3495 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3496 psp_usbc_pd_fw_sysfs_read,
3497 psp_usbc_pd_fw_sysfs_write);
3499 int is_psp_fw_valid(struct psp_bin_desc bin)
3501 return bin.size_bytes;
3504 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3505 struct bin_attribute *bin_attr,
3506 char *buffer, loff_t pos, size_t count)
3508 struct device *dev = kobj_to_dev(kobj);
3509 struct drm_device *ddev = dev_get_drvdata(dev);
3510 struct amdgpu_device *adev = drm_to_adev(ddev);
3512 adev->psp.vbflash_done = false;
3514 /* Safeguard against memory drain */
3515 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3516 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3517 kvfree(adev->psp.vbflash_tmp_buf);
3518 adev->psp.vbflash_tmp_buf = NULL;
3519 adev->psp.vbflash_image_size = 0;
3523 /* TODO Just allocate max for now and optimize to realloc later if needed */
3524 if (!adev->psp.vbflash_tmp_buf) {
3525 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3526 if (!adev->psp.vbflash_tmp_buf)
3530 mutex_lock(&adev->psp.mutex);
3531 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3532 adev->psp.vbflash_image_size += count;
3533 mutex_unlock(&adev->psp.mutex);
3535 dev_info(adev->dev, "VBIOS flash write PSP done");
3540 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3541 struct bin_attribute *bin_attr, char *buffer,
3542 loff_t pos, size_t count)
3544 struct device *dev = kobj_to_dev(kobj);
3545 struct drm_device *ddev = dev_get_drvdata(dev);
3546 struct amdgpu_device *adev = drm_to_adev(ddev);
3547 struct amdgpu_bo *fw_buf_bo = NULL;
3548 uint64_t fw_pri_mc_addr;
3549 void *fw_pri_cpu_addr;
3552 dev_info(adev->dev, "VBIOS flash to PSP started");
3554 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3555 AMDGPU_GPU_PAGE_SIZE,
3556 AMDGPU_GEM_DOMAIN_VRAM,
3563 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3565 mutex_lock(&adev->psp.mutex);
3566 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3567 mutex_unlock(&adev->psp.mutex);
3569 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3572 kvfree(adev->psp.vbflash_tmp_buf);
3573 adev->psp.vbflash_tmp_buf = NULL;
3574 adev->psp.vbflash_image_size = 0;
3577 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3581 dev_info(adev->dev, "VBIOS flash to PSP done");
3585 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3586 struct device_attribute *attr,
3589 struct drm_device *ddev = dev_get_drvdata(dev);
3590 struct amdgpu_device *adev = drm_to_adev(ddev);
3591 uint32_t vbflash_status;
3593 vbflash_status = psp_vbflash_status(&adev->psp);
3594 if (!adev->psp.vbflash_done)
3596 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3599 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3602 static const struct bin_attribute psp_vbflash_bin_attr = {
3603 .attr = {.name = "psp_vbflash", .mode = 0664},
3605 .write = amdgpu_psp_vbflash_write,
3606 .read = amdgpu_psp_vbflash_read,
3609 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3611 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3614 struct psp_context *psp = &adev->psp;
3616 if (amdgpu_sriov_vf(adev))
3619 switch (adev->ip_versions[MP0_HWIP][0]) {
3620 case IP_VERSION(13, 0, 0):
3621 case IP_VERSION(13, 0, 7):
3624 psp_v13_0_set_psp_funcs(psp);
3626 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3628 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3629 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3631 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3638 const struct amd_ip_funcs psp_ip_funcs = {
3640 .early_init = psp_early_init,
3642 .sw_init = psp_sw_init,
3643 .sw_fini = psp_sw_fini,
3644 .hw_init = psp_hw_init,
3645 .hw_fini = psp_hw_fini,
3646 .suspend = psp_suspend,
3647 .resume = psp_resume,
3649 .check_soft_reset = NULL,
3650 .wait_for_idle = NULL,
3652 .set_clockgating_state = psp_set_clockgating_state,
3653 .set_powergating_state = psp_set_powergating_state,
3656 static int psp_sysfs_init(struct amdgpu_device *adev)
3658 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3661 DRM_ERROR("Failed to create USBC PD FW control file!");
3666 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3668 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3669 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3672 static void psp_sysfs_fini(struct amdgpu_device *adev)
3674 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3677 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3679 .type = AMD_IP_BLOCK_TYPE_PSP,
3683 .funcs = &psp_ip_funcs,
3686 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3688 .type = AMD_IP_BLOCK_TYPE_PSP,
3692 .funcs = &psp_ip_funcs,
3695 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3697 .type = AMD_IP_BLOCK_TYPE_PSP,
3701 .funcs = &psp_ip_funcs,
3704 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3705 .type = AMD_IP_BLOCK_TYPE_PSP,
3709 .funcs = &psp_ip_funcs,
3712 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3714 .type = AMD_IP_BLOCK_TYPE_PSP,
3718 .funcs = &psp_ip_funcs,
3721 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3722 .type = AMD_IP_BLOCK_TYPE_PSP,
3726 .funcs = &psp_ip_funcs,
3729 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3730 .type = AMD_IP_BLOCK_TYPE_PSP,
3734 .funcs = &psp_ip_funcs,