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);
177 static void psp_free_shared_bufs(struct psp_context *psp)
182 /* free TMR memory buffer */
183 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
184 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
186 /* free xgmi shared memory */
187 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
189 /* free ras shared memory */
190 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
192 /* free hdcp shared memory */
193 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
195 /* free dtm shared memory */
196 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
198 /* free rap shared memory */
199 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
201 /* free securedisplay shared memory */
202 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
207 static void psp_memory_training_fini(struct psp_context *psp)
209 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
211 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
212 kfree(ctx->sys_cache);
213 ctx->sys_cache = NULL;
216 static int psp_memory_training_init(struct psp_context *psp)
219 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
221 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
222 DRM_DEBUG("memory training is not supported!\n");
226 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
227 if (ctx->sys_cache == NULL) {
228 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
233 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
234 ctx->train_data_size,
235 ctx->p2c_train_data_offset,
236 ctx->c2p_train_data_offset);
237 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
241 psp_memory_training_fini(psp);
246 * Helper funciton to query psp runtime database entry
248 * @adev: amdgpu_device pointer
249 * @entry_type: the type of psp runtime database entry
250 * @db_entry: runtime database entry pointer
252 * Return false if runtime database doesn't exit or entry is invalid
253 * or true if the specific database entry is found, and copy to @db_entry
255 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
256 enum psp_runtime_entry_type entry_type,
259 uint64_t db_header_pos, db_dir_pos;
260 struct psp_runtime_data_header db_header = {0};
261 struct psp_runtime_data_directory db_dir = {0};
265 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
266 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
268 /* read runtime db header from vram */
269 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
270 sizeof(struct psp_runtime_data_header), false);
272 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
273 /* runtime db doesn't exist, exit */
274 dev_warn(adev->dev, "PSP runtime database doesn't exist\n");
278 /* read runtime database entry from vram */
279 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
280 sizeof(struct psp_runtime_data_directory), false);
282 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
283 /* invalid db entry count, exit */
284 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
288 /* look up for requested entry type */
289 for (i = 0; i < db_dir.entry_count && !ret; i++) {
290 if (db_dir.entry_list[i].entry_type == entry_type) {
291 switch (entry_type) {
292 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
293 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
294 /* invalid db entry size */
295 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
298 /* read runtime database entry */
299 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
300 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
303 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
304 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
305 /* invalid db entry size */
306 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
309 /* read runtime database entry */
310 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
311 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
324 static int psp_init_sriov_microcode(struct psp_context *psp)
326 struct amdgpu_device *adev = psp->adev;
329 switch (adev->ip_versions[MP0_HWIP][0]) {
330 case IP_VERSION(9, 0, 0):
331 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
332 ret = psp_init_cap_microcode(psp, "vega10");
334 case IP_VERSION(11, 0, 9):
335 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
336 ret = psp_init_cap_microcode(psp, "navi12");
338 case IP_VERSION(11, 0, 7):
339 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
340 ret = psp_init_cap_microcode(psp, "sienna_cichlid");
342 case IP_VERSION(13, 0, 2):
343 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
344 ret = psp_init_cap_microcode(psp, "aldebaran");
345 ret &= psp_init_ta_microcode(psp, "aldebaran");
347 case IP_VERSION(13, 0, 0):
348 adev->virt.autoload_ucode_id = 0;
350 case IP_VERSION(13, 0, 10):
351 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
360 static int psp_sw_init(void *handle)
362 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
363 struct psp_context *psp = &adev->psp;
365 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
366 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
367 struct psp_runtime_scpm_entry scpm_entry;
369 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
371 DRM_ERROR("Failed to allocate memory to command buffer!\n");
375 if (amdgpu_sriov_vf(adev))
376 ret = psp_init_sriov_microcode(psp);
378 ret = psp_init_microcode(psp);
380 DRM_ERROR("Failed to load psp firmware!\n");
384 adev->psp.xgmi_context.supports_extended_data =
385 !adev->gmc.xgmi.connected_to_cpu &&
386 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
388 memset(&scpm_entry, 0, sizeof(scpm_entry));
389 if ((psp_get_runtime_db_entry(adev,
390 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
392 (SCPM_DISABLE != scpm_entry.scpm_status)) {
393 adev->scpm_enabled = true;
394 adev->scpm_status = scpm_entry.scpm_status;
396 adev->scpm_enabled = false;
397 adev->scpm_status = SCPM_DISABLE;
400 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
402 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
403 if (psp_get_runtime_db_entry(adev,
404 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
406 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
407 if ((psp->boot_cfg_bitmask) &
408 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
409 /* If psp runtime database exists, then
410 * only enable two stage memory training
411 * when TWO_STAGE_DRAM_TRAINING bit is set
412 * in runtime database */
413 mem_training_ctx->enable_mem_training = true;
417 /* If psp runtime database doesn't exist or
418 * is invalid, force enable two stage memory
420 mem_training_ctx->enable_mem_training = true;
423 if (mem_training_ctx->enable_mem_training) {
424 ret = psp_memory_training_init(psp);
426 DRM_ERROR("Failed to initialize memory training!\n");
430 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
432 DRM_ERROR("Failed to process memory training!\n");
437 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
438 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
439 ret= psp_sysfs_init(adev);
445 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
446 amdgpu_sriov_vf(adev) ?
447 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
449 &psp->fw_pri_mc_addr,
454 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
455 AMDGPU_GEM_DOMAIN_VRAM,
457 &psp->fence_buf_mc_addr,
462 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
463 AMDGPU_GEM_DOMAIN_VRAM,
464 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
465 (void **)&psp->cmd_buf_mem);
472 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
473 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
475 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
476 &psp->fence_buf_mc_addr, &psp->fence_buf);
480 static int psp_sw_fini(void *handle)
482 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
483 struct psp_context *psp = &adev->psp;
484 struct psp_gfx_cmd_resp *cmd = psp->cmd;
486 psp_memory_training_fini(psp);
488 release_firmware(psp->sos_fw);
492 release_firmware(psp->asd_fw);
496 release_firmware(psp->ta_fw);
500 release_firmware(psp->cap_fw);
504 release_firmware(psp->toc_fw);
507 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
508 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
509 psp_sysfs_fini(adev);
514 if (psp->km_ring.ring_mem)
515 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
516 &psp->km_ring.ring_mem_mc_addr,
517 (void **)&psp->km_ring.ring_mem);
519 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
520 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
521 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
522 &psp->fence_buf_mc_addr, &psp->fence_buf);
523 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
524 (void **)&psp->cmd_buf_mem);
529 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
530 uint32_t reg_val, uint32_t mask, bool check_changed)
534 struct amdgpu_device *adev = psp->adev;
536 if (psp->adev->no_hw_access)
539 for (i = 0; i < adev->usec_timeout; i++) {
540 val = RREG32(reg_index);
545 if ((val & mask) == reg_val)
554 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
557 case GFX_CMD_ID_LOAD_TA:
559 case GFX_CMD_ID_UNLOAD_TA:
561 case GFX_CMD_ID_INVOKE_CMD:
563 case GFX_CMD_ID_LOAD_ASD:
565 case GFX_CMD_ID_SETUP_TMR:
567 case GFX_CMD_ID_LOAD_IP_FW:
569 case GFX_CMD_ID_DESTROY_TMR:
570 return "DESTROY_TMR";
571 case GFX_CMD_ID_SAVE_RESTORE:
572 return "SAVE_RESTORE_IP_FW";
573 case GFX_CMD_ID_SETUP_VMR:
575 case GFX_CMD_ID_DESTROY_VMR:
576 return "DESTROY_VMR";
577 case GFX_CMD_ID_PROG_REG:
579 case GFX_CMD_ID_GET_FW_ATTESTATION:
580 return "GET_FW_ATTESTATION";
581 case GFX_CMD_ID_LOAD_TOC:
582 return "ID_LOAD_TOC";
583 case GFX_CMD_ID_AUTOLOAD_RLC:
584 return "AUTOLOAD_RLC";
585 case GFX_CMD_ID_BOOT_CFG:
588 return "UNKNOWN CMD";
593 psp_cmd_submit_buf(struct psp_context *psp,
594 struct amdgpu_firmware_info *ucode,
595 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
600 bool ras_intr = false;
601 bool skip_unsupport = false;
603 if (psp->adev->no_hw_access)
606 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
609 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
611 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
613 index = atomic_inc_return(&psp->fence_value);
614 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
616 atomic_dec(&psp->fence_value);
620 amdgpu_device_invalidate_hdp(psp->adev, NULL);
621 while (*((unsigned int *)psp->fence_buf) != index) {
625 * Shouldn't wait for timeout when err_event_athub occurs,
626 * because gpu reset thread triggered and lock resource should
627 * be released for psp resume sequence.
629 ras_intr = amdgpu_ras_intr_triggered();
632 usleep_range(10, 100);
633 amdgpu_device_invalidate_hdp(psp->adev, NULL);
636 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
637 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
638 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
640 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
642 /* In some cases, psp response status is not 0 even there is no
643 * problem while the command is submitted. Some version of PSP FW
644 * doesn't write 0 to that field.
645 * So here we would like to only print a warning instead of an error
646 * during psp initialization to avoid breaking hw_init and it doesn't
649 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
651 DRM_WARN("failed to load ucode %s(0x%X) ",
652 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
653 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
654 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
655 psp->cmd_buf_mem->resp.status);
656 /* If any firmware (including CAP) load fails under SRIOV, it should
657 * return failure to stop the VF from initializing.
658 * Also return failure in case of timeout
660 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
667 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
668 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
676 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
678 struct psp_gfx_cmd_resp *cmd = psp->cmd;
680 mutex_lock(&psp->mutex);
682 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
687 static void release_psp_cmd_buf(struct psp_context *psp)
689 mutex_unlock(&psp->mutex);
692 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
693 struct psp_gfx_cmd_resp *cmd,
694 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
696 struct amdgpu_device *adev = psp->adev;
697 uint32_t size = amdgpu_bo_size(tmr_bo);
698 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
700 if (amdgpu_sriov_vf(psp->adev))
701 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
703 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
704 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
705 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
706 cmd->cmd.cmd_setup_tmr.buf_size = size;
707 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
708 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
709 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
712 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
713 uint64_t pri_buf_mc, uint32_t size)
715 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
716 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
717 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
718 cmd->cmd.cmd_load_toc.toc_size = size;
721 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
722 static int psp_load_toc(struct psp_context *psp,
726 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
728 /* Copy toc to psp firmware private buffer */
729 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
731 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
733 ret = psp_cmd_submit_buf(psp, NULL, cmd,
734 psp->fence_buf_mc_addr);
736 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
738 release_psp_cmd_buf(psp);
743 /* Set up Trusted Memory Region */
744 static int psp_tmr_init(struct psp_context *psp)
752 * According to HW engineer, they prefer the TMR address be "naturally
753 * aligned" , e.g. the start address be an integer divide of TMR size.
755 * Note: this memory need be reserved till the driver
758 tmr_size = PSP_TMR_SIZE(psp->adev);
760 /* For ASICs support RLC autoload, psp will parse the toc
761 * and calculate the total size of TMR needed */
762 if (!amdgpu_sriov_vf(psp->adev) &&
763 psp->toc.start_addr &&
764 psp->toc.size_bytes &&
766 ret = psp_load_toc(psp, &tmr_size);
768 DRM_ERROR("Failed to load toc\n");
773 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
774 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_ALIGNMENT,
775 AMDGPU_GEM_DOMAIN_VRAM,
776 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
781 static bool psp_skip_tmr(struct psp_context *psp)
783 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
784 case IP_VERSION(11, 0, 9):
785 case IP_VERSION(11, 0, 7):
786 case IP_VERSION(13, 0, 2):
787 case IP_VERSION(13, 0, 10):
794 static int psp_tmr_load(struct psp_context *psp)
797 struct psp_gfx_cmd_resp *cmd;
799 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
800 * Already set up by host driver.
802 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
805 cmd = acquire_psp_cmd_buf(psp);
807 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
808 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
809 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
811 ret = psp_cmd_submit_buf(psp, NULL, cmd,
812 psp->fence_buf_mc_addr);
814 release_psp_cmd_buf(psp);
819 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
820 struct psp_gfx_cmd_resp *cmd)
822 if (amdgpu_sriov_vf(psp->adev))
823 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
825 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
828 static int psp_tmr_unload(struct psp_context *psp)
831 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
833 psp_prep_tmr_unload_cmd_buf(psp, cmd);
834 dev_info(psp->adev->dev, "free PSP TMR buffer\n");
836 ret = psp_cmd_submit_buf(psp, NULL, cmd,
837 psp->fence_buf_mc_addr);
839 release_psp_cmd_buf(psp);
844 static int psp_tmr_terminate(struct psp_context *psp)
846 return psp_tmr_unload(psp);
849 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
850 uint64_t *output_ptr)
853 struct psp_gfx_cmd_resp *cmd;
858 if (amdgpu_sriov_vf(psp->adev))
861 cmd = acquire_psp_cmd_buf(psp);
863 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
865 ret = psp_cmd_submit_buf(psp, NULL, cmd,
866 psp->fence_buf_mc_addr);
869 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
870 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
873 release_psp_cmd_buf(psp);
878 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
880 struct psp_context *psp = &adev->psp;
881 struct psp_gfx_cmd_resp *cmd;
884 if (amdgpu_sriov_vf(adev))
887 cmd = acquire_psp_cmd_buf(psp);
889 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
890 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
892 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
895 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
898 release_psp_cmd_buf(psp);
903 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
906 struct psp_context *psp = &adev->psp;
907 struct psp_gfx_cmd_resp *cmd;
909 if (amdgpu_sriov_vf(adev))
912 cmd = acquire_psp_cmd_buf(psp);
914 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
915 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
916 cmd->cmd.boot_cfg.boot_config = boot_cfg;
917 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
919 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
921 release_psp_cmd_buf(psp);
926 static int psp_rl_load(struct amdgpu_device *adev)
929 struct psp_context *psp = &adev->psp;
930 struct psp_gfx_cmd_resp *cmd;
932 if (!is_psp_fw_valid(psp->rl))
935 cmd = acquire_psp_cmd_buf(psp);
937 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
938 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
940 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
941 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
942 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
943 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
944 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
946 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
948 release_psp_cmd_buf(psp);
953 static int psp_asd_initialize(struct psp_context *psp)
957 /* If PSP version doesn't match ASD version, asd loading will be failed.
958 * add workaround to bypass it for sriov now.
959 * TODO: add version check to make it common
961 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
964 psp->asd_context.mem_context.shared_mc_addr = 0;
965 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
966 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
968 ret = psp_ta_load(psp, &psp->asd_context);
970 psp->asd_context.initialized = true;
975 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
978 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
979 cmd->cmd.cmd_unload_ta.session_id = session_id;
982 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
985 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
987 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
989 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
991 release_psp_cmd_buf(psp);
996 static int psp_asd_terminate(struct psp_context *psp)
1000 if (amdgpu_sriov_vf(psp->adev))
1003 if (!psp->asd_context.initialized)
1006 ret = psp_ta_unload(psp, &psp->asd_context);
1008 psp->asd_context.initialized = false;
1013 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1014 uint32_t id, uint32_t value)
1016 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1017 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1018 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1021 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1024 struct psp_gfx_cmd_resp *cmd;
1027 if (reg >= PSP_REG_LAST)
1030 cmd = acquire_psp_cmd_buf(psp);
1032 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1033 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1035 DRM_ERROR("PSP failed to program reg id %d", reg);
1037 release_psp_cmd_buf(psp);
1042 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1044 struct ta_context *context)
1046 cmd->cmd_id = context->ta_load_type;
1047 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1048 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1049 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1051 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1052 lower_32_bits(context->mem_context.shared_mc_addr);
1053 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1054 upper_32_bits(context->mem_context.shared_mc_addr);
1055 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1058 int psp_ta_init_shared_buf(struct psp_context *psp,
1059 struct ta_mem_context *mem_ctx)
1062 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1063 * physical) for ta to host memory
1065 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1066 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1067 &mem_ctx->shared_bo,
1068 &mem_ctx->shared_mc_addr,
1069 &mem_ctx->shared_buf);
1072 static void psp_prep_ta_invoke_indirect_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1074 struct ta_context *context)
1076 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1077 cmd->cmd.cmd_invoke_cmd.session_id = context->session_id;
1078 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1080 cmd->cmd.cmd_invoke_cmd.buf.num_desc = 1;
1081 cmd->cmd.cmd_invoke_cmd.buf.total_size = context->mem_context.shared_mem_size;
1082 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_size = context->mem_context.shared_mem_size;
1083 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_phy_addr_lo =
1084 lower_32_bits(context->mem_context.shared_mc_addr);
1085 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_phy_addr_hi =
1086 upper_32_bits(context->mem_context.shared_mc_addr);
1089 int psp_ta_invoke_indirect(struct psp_context *psp,
1091 struct ta_context *context)
1094 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1096 psp_prep_ta_invoke_indirect_cmd_buf(cmd, ta_cmd_id, context);
1098 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1099 psp->fence_buf_mc_addr);
1101 context->resp_status = cmd->resp.status;
1103 release_psp_cmd_buf(psp);
1108 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1110 uint32_t session_id)
1112 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1113 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1114 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1117 int psp_ta_invoke(struct psp_context *psp,
1119 struct ta_context *context)
1122 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1124 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1126 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1127 psp->fence_buf_mc_addr);
1129 context->resp_status = cmd->resp.status;
1131 release_psp_cmd_buf(psp);
1136 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1139 struct psp_gfx_cmd_resp *cmd;
1141 cmd = acquire_psp_cmd_buf(psp);
1143 psp_copy_fw(psp, context->bin_desc.start_addr,
1144 context->bin_desc.size_bytes);
1146 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1148 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1149 psp->fence_buf_mc_addr);
1151 context->resp_status = cmd->resp.status;
1154 context->session_id = cmd->resp.session_id;
1157 release_psp_cmd_buf(psp);
1162 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1164 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1167 int psp_xgmi_terminate(struct psp_context *psp)
1170 struct amdgpu_device *adev = psp->adev;
1172 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1173 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1174 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1175 adev->gmc.xgmi.connected_to_cpu))
1178 if (!psp->xgmi_context.context.initialized)
1181 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1183 psp->xgmi_context.context.initialized = false;
1188 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1190 struct ta_xgmi_shared_memory *xgmi_cmd;
1194 !psp->xgmi_context.context.bin_desc.size_bytes ||
1195 !psp->xgmi_context.context.bin_desc.start_addr)
1201 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1202 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1204 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1205 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1211 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1213 psp->xgmi_context.context.initialized = true;
1218 /* Initialize XGMI session */
1219 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1220 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1221 xgmi_cmd->flag_extend_link_record = set_extended_data;
1222 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1224 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1229 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1231 struct ta_xgmi_shared_memory *xgmi_cmd;
1234 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1235 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1237 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1239 /* Invoke xgmi ta to get hive id */
1240 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1244 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1249 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1251 struct ta_xgmi_shared_memory *xgmi_cmd;
1254 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1255 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1257 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1259 /* Invoke xgmi ta to get the node id */
1260 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1264 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1269 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1271 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1272 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1276 * Chips that support extended topology information require the driver to
1277 * reflect topology information in the opposite direction. This is
1278 * because the TA has already exceeded its link record limit and if the
1279 * TA holds bi-directional information, the driver would have to do
1280 * multiple fetches instead of just two.
1282 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1283 struct psp_xgmi_node_info node_info)
1285 struct amdgpu_device *mirror_adev;
1286 struct amdgpu_hive_info *hive;
1287 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1288 uint64_t dst_node_id = node_info.node_id;
1289 uint8_t dst_num_hops = node_info.num_hops;
1290 uint8_t dst_num_links = node_info.num_links;
1292 hive = amdgpu_get_xgmi_hive(psp->adev);
1293 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1294 struct psp_xgmi_topology_info *mirror_top_info;
1297 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1300 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1301 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1302 if (mirror_top_info->nodes[j].node_id != src_node_id)
1305 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1307 * prevent 0 num_links value re-reflection since reflection
1308 * criteria is based on num_hops (direct or indirect).
1312 mirror_top_info->nodes[j].num_links = dst_num_links;
1320 amdgpu_put_xgmi_hive(hive);
1323 int psp_xgmi_get_topology_info(struct psp_context *psp,
1325 struct psp_xgmi_topology_info *topology,
1326 bool get_extended_data)
1328 struct ta_xgmi_shared_memory *xgmi_cmd;
1329 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1330 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1334 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1337 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1338 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1339 xgmi_cmd->flag_extend_link_record = get_extended_data;
1341 /* Fill in the shared memory with topology information as input */
1342 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1343 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1344 topology_info_input->num_nodes = number_devices;
1346 for (i = 0; i < topology_info_input->num_nodes; i++) {
1347 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1348 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1349 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1350 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1353 /* Invoke xgmi ta to get the topology information */
1354 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1358 /* Read the output topology information from the shared memory */
1359 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1360 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1361 for (i = 0; i < topology->num_nodes; i++) {
1362 /* extended data will either be 0 or equal to non-extended data */
1363 if (topology_info_output->nodes[i].num_hops)
1364 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1366 /* non-extended data gets everything here so no need to update */
1367 if (!get_extended_data) {
1368 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1369 topology->nodes[i].is_sharing_enabled =
1370 topology_info_output->nodes[i].is_sharing_enabled;
1371 topology->nodes[i].sdma_engine =
1372 topology_info_output->nodes[i].sdma_engine;
1377 /* Invoke xgmi ta again to get the link information */
1378 if (psp_xgmi_peer_link_info_supported(psp)) {
1379 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1381 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1383 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1388 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1389 for (i = 0; i < topology->num_nodes; i++) {
1390 /* accumulate num_links on extended data */
1391 topology->nodes[i].num_links = get_extended_data ?
1392 topology->nodes[i].num_links +
1393 link_info_output->nodes[i].num_links :
1394 link_info_output->nodes[i].num_links;
1396 /* reflect the topology information for bi-directionality */
1397 if (psp->xgmi_context.supports_extended_data &&
1398 get_extended_data && topology->nodes[i].num_hops)
1399 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1406 int psp_xgmi_set_topology_info(struct psp_context *psp,
1408 struct psp_xgmi_topology_info *topology)
1410 struct ta_xgmi_shared_memory *xgmi_cmd;
1411 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1414 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1417 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1418 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1420 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1421 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1422 topology_info_input->num_nodes = number_devices;
1424 for (i = 0; i < topology_info_input->num_nodes; i++) {
1425 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1426 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1427 topology_info_input->nodes[i].is_sharing_enabled = 1;
1428 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1431 /* Invoke xgmi ta to set topology information */
1432 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1436 static void psp_ras_ta_check_status(struct psp_context *psp)
1438 struct ta_ras_shared_memory *ras_cmd =
1439 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1441 switch (ras_cmd->ras_status) {
1442 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1443 dev_warn(psp->adev->dev,
1444 "RAS WARNING: cmd failed due to unsupported ip\n");
1446 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1447 dev_warn(psp->adev->dev,
1448 "RAS WARNING: cmd failed due to unsupported error injection\n");
1450 case TA_RAS_STATUS__SUCCESS:
1452 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1453 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1454 dev_warn(psp->adev->dev,
1455 "RAS WARNING: Inject error to critical region is not allowed\n");
1458 dev_warn(psp->adev->dev,
1459 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1464 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1466 struct ta_ras_shared_memory *ras_cmd;
1469 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1472 * TODO: bypass the loading in sriov for now
1474 if (amdgpu_sriov_vf(psp->adev))
1477 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1479 if (amdgpu_ras_intr_triggered())
1482 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1484 DRM_WARN("RAS: Unsupported Interface");
1489 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1490 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1492 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1494 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1495 dev_warn(psp->adev->dev,
1496 "RAS internal register access blocked\n");
1498 psp_ras_ta_check_status(psp);
1504 int psp_ras_enable_features(struct psp_context *psp,
1505 union ta_ras_cmd_input *info, bool enable)
1507 struct ta_ras_shared_memory *ras_cmd;
1510 if (!psp->ras_context.context.initialized)
1513 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1514 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1517 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1519 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1521 ras_cmd->ras_in_message = *info;
1523 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1530 int psp_ras_terminate(struct psp_context *psp)
1535 * TODO: bypass the terminate in sriov for now
1537 if (amdgpu_sriov_vf(psp->adev))
1540 if (!psp->ras_context.context.initialized)
1543 ret = psp_ta_unload(psp, &psp->ras_context.context);
1545 psp->ras_context.context.initialized = false;
1550 static int psp_ras_initialize(struct psp_context *psp)
1553 uint32_t boot_cfg = 0xFF;
1554 struct amdgpu_device *adev = psp->adev;
1555 struct ta_ras_shared_memory *ras_cmd;
1558 * TODO: bypass the initialize in sriov for now
1560 if (amdgpu_sriov_vf(adev))
1563 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1564 !adev->psp.ras_context.context.bin_desc.start_addr) {
1565 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1569 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1570 /* query GECC enablement status from boot config
1571 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1573 ret = psp_boot_config_get(adev, &boot_cfg);
1575 dev_warn(adev->dev, "PSP get boot config failed\n");
1577 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1579 dev_info(adev->dev, "GECC is disabled\n");
1581 /* disable GECC in next boot cycle if ras is
1582 * disabled by module parameter amdgpu_ras_enable
1583 * and/or amdgpu_ras_mask, or boot_config_get call
1586 ret = psp_boot_config_set(adev, 0);
1588 dev_warn(adev->dev, "PSP set boot config failed\n");
1590 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1591 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1594 if (1 == boot_cfg) {
1595 dev_info(adev->dev, "GECC is enabled\n");
1597 /* enable GECC in next boot cycle if it is disabled
1598 * in boot config, or force enable GECC if failed to
1599 * get boot configuration
1601 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1603 dev_warn(adev->dev, "PSP set boot config failed\n");
1605 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1610 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1611 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1613 if (!psp->ras_context.context.initialized) {
1614 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1619 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1620 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1622 if (amdgpu_ras_is_poison_mode_supported(adev))
1623 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1624 if (!adev->gmc.xgmi.connected_to_cpu)
1625 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1627 ret = psp_ta_load(psp, &psp->ras_context.context);
1629 if (!ret && !ras_cmd->ras_status)
1630 psp->ras_context.context.initialized = true;
1632 if (ras_cmd->ras_status)
1633 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1634 amdgpu_ras_fini(psp->adev);
1640 int psp_ras_trigger_error(struct psp_context *psp,
1641 struct ta_ras_trigger_error_input *info)
1643 struct ta_ras_shared_memory *ras_cmd;
1646 if (!psp->ras_context.context.initialized)
1649 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1650 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1652 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1653 ras_cmd->ras_in_message.trigger_error = *info;
1655 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1659 /* If err_event_athub occurs error inject was successful, however
1660 return status from TA is no long reliable */
1661 if (amdgpu_ras_intr_triggered())
1664 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1666 else if (ras_cmd->ras_status)
1674 static int psp_hdcp_initialize(struct psp_context *psp)
1679 * TODO: bypass the initialize in sriov for now
1681 if (amdgpu_sriov_vf(psp->adev))
1684 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1685 !psp->hdcp_context.context.bin_desc.start_addr) {
1686 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1690 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1691 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1693 if (!psp->hdcp_context.context.initialized) {
1694 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1699 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1701 psp->hdcp_context.context.initialized = true;
1702 mutex_init(&psp->hdcp_context.mutex);
1708 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1711 * TODO: bypass the loading in sriov for now
1713 if (amdgpu_sriov_vf(psp->adev))
1716 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1719 static int psp_hdcp_terminate(struct psp_context *psp)
1724 * TODO: bypass the terminate in sriov for now
1726 if (amdgpu_sriov_vf(psp->adev))
1729 if (!psp->hdcp_context.context.initialized)
1732 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1734 psp->hdcp_context.context.initialized = false;
1741 static int psp_dtm_initialize(struct psp_context *psp)
1746 * TODO: bypass the initialize in sriov for now
1748 if (amdgpu_sriov_vf(psp->adev))
1751 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1752 !psp->dtm_context.context.bin_desc.start_addr) {
1753 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1757 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1758 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1760 if (!psp->dtm_context.context.initialized) {
1761 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1766 ret = psp_ta_load(psp, &psp->dtm_context.context);
1768 psp->dtm_context.context.initialized = true;
1769 mutex_init(&psp->dtm_context.mutex);
1775 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1778 * TODO: bypass the loading in sriov for now
1780 if (amdgpu_sriov_vf(psp->adev))
1783 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1786 static int psp_dtm_terminate(struct psp_context *psp)
1791 * TODO: bypass the terminate in sriov for now
1793 if (amdgpu_sriov_vf(psp->adev))
1796 if (!psp->dtm_context.context.initialized)
1799 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1801 psp->dtm_context.context.initialized = false;
1808 static int psp_rap_initialize(struct psp_context *psp)
1811 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1814 * TODO: bypass the initialize in sriov for now
1816 if (amdgpu_sriov_vf(psp->adev))
1819 if (!psp->rap_context.context.bin_desc.size_bytes ||
1820 !psp->rap_context.context.bin_desc.start_addr) {
1821 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1825 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1826 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1828 if (!psp->rap_context.context.initialized) {
1829 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1834 ret = psp_ta_load(psp, &psp->rap_context.context);
1836 psp->rap_context.context.initialized = true;
1837 mutex_init(&psp->rap_context.mutex);
1841 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1842 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1843 psp_rap_terminate(psp);
1844 /* free rap shared memory */
1845 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1847 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1856 static int psp_rap_terminate(struct psp_context *psp)
1860 if (!psp->rap_context.context.initialized)
1863 ret = psp_ta_unload(psp, &psp->rap_context.context);
1865 psp->rap_context.context.initialized = false;
1870 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1872 struct ta_rap_shared_memory *rap_cmd;
1875 if (!psp->rap_context.context.initialized)
1878 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1879 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1882 mutex_lock(&psp->rap_context.mutex);
1884 rap_cmd = (struct ta_rap_shared_memory *)
1885 psp->rap_context.context.mem_context.shared_buf;
1886 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1888 rap_cmd->cmd_id = ta_cmd_id;
1889 rap_cmd->validation_method_id = METHOD_A;
1891 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1896 *status = rap_cmd->rap_status;
1899 mutex_unlock(&psp->rap_context.mutex);
1905 /* securedisplay start */
1906 static int psp_securedisplay_initialize(struct psp_context *psp)
1909 struct securedisplay_cmd *securedisplay_cmd;
1912 * TODO: bypass the initialize in sriov for now
1914 if (amdgpu_sriov_vf(psp->adev))
1917 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1918 !psp->securedisplay_context.context.bin_desc.start_addr) {
1919 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1923 psp->securedisplay_context.context.mem_context.shared_mem_size =
1924 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1925 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1927 if (!psp->securedisplay_context.context.initialized) {
1928 ret = psp_ta_init_shared_buf(psp,
1929 &psp->securedisplay_context.context.mem_context);
1934 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1936 psp->securedisplay_context.context.initialized = true;
1937 mutex_init(&psp->securedisplay_context.mutex);
1941 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1942 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1944 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1946 psp_securedisplay_terminate(psp);
1947 /* free securedisplay shared memory */
1948 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1949 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1953 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1954 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1955 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1956 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1962 static int psp_securedisplay_terminate(struct psp_context *psp)
1967 * TODO:bypass the terminate in sriov for now
1969 if (amdgpu_sriov_vf(psp->adev))
1972 if (!psp->securedisplay_context.context.initialized)
1975 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1977 psp->securedisplay_context.context.initialized = false;
1982 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1986 if (!psp->securedisplay_context.context.initialized)
1989 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1990 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1993 mutex_lock(&psp->securedisplay_context.mutex);
1995 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1997 mutex_unlock(&psp->securedisplay_context.mutex);
2001 /* SECUREDISPLAY end */
2003 static int psp_hw_start(struct psp_context *psp)
2005 struct amdgpu_device *adev = psp->adev;
2008 if (!amdgpu_sriov_vf(adev)) {
2009 if ((is_psp_fw_valid(psp->kdb)) &&
2010 (psp->funcs->bootloader_load_kdb != NULL)) {
2011 ret = psp_bootloader_load_kdb(psp);
2013 DRM_ERROR("PSP load kdb failed!\n");
2018 if ((is_psp_fw_valid(psp->spl)) &&
2019 (psp->funcs->bootloader_load_spl != NULL)) {
2020 ret = psp_bootloader_load_spl(psp);
2022 DRM_ERROR("PSP load spl failed!\n");
2027 if ((is_psp_fw_valid(psp->sys)) &&
2028 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2029 ret = psp_bootloader_load_sysdrv(psp);
2031 DRM_ERROR("PSP load sys drv failed!\n");
2036 if ((is_psp_fw_valid(psp->soc_drv)) &&
2037 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2038 ret = psp_bootloader_load_soc_drv(psp);
2040 DRM_ERROR("PSP load soc drv failed!\n");
2045 if ((is_psp_fw_valid(psp->intf_drv)) &&
2046 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2047 ret = psp_bootloader_load_intf_drv(psp);
2049 DRM_ERROR("PSP load intf drv failed!\n");
2054 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2055 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2056 ret = psp_bootloader_load_dbg_drv(psp);
2058 DRM_ERROR("PSP load dbg drv failed!\n");
2063 if ((is_psp_fw_valid(psp->ras_drv)) &&
2064 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2065 ret = psp_bootloader_load_ras_drv(psp);
2067 DRM_ERROR("PSP load ras_drv failed!\n");
2072 if ((is_psp_fw_valid(psp->sos)) &&
2073 (psp->funcs->bootloader_load_sos != NULL)) {
2074 ret = psp_bootloader_load_sos(psp);
2076 DRM_ERROR("PSP load sos failed!\n");
2082 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2084 DRM_ERROR("PSP create ring failed!\n");
2088 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2091 ret = psp_tmr_init(psp);
2093 DRM_ERROR("PSP tmr init failed!\n");
2099 * For ASICs with DF Cstate management centralized
2100 * to PMFW, TMR setup should be performed after PMFW
2101 * loaded and before other non-psp firmware loaded.
2103 if (psp->pmfw_centralized_cstate_management) {
2104 ret = psp_load_smu_fw(psp);
2109 ret = psp_tmr_load(psp);
2111 DRM_ERROR("PSP load tmr failed!\n");
2118 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2119 enum psp_gfx_fw_type *type)
2121 switch (ucode->ucode_id) {
2122 case AMDGPU_UCODE_ID_CAP:
2123 *type = GFX_FW_TYPE_CAP;
2125 case AMDGPU_UCODE_ID_SDMA0:
2126 *type = GFX_FW_TYPE_SDMA0;
2128 case AMDGPU_UCODE_ID_SDMA1:
2129 *type = GFX_FW_TYPE_SDMA1;
2131 case AMDGPU_UCODE_ID_SDMA2:
2132 *type = GFX_FW_TYPE_SDMA2;
2134 case AMDGPU_UCODE_ID_SDMA3:
2135 *type = GFX_FW_TYPE_SDMA3;
2137 case AMDGPU_UCODE_ID_SDMA4:
2138 *type = GFX_FW_TYPE_SDMA4;
2140 case AMDGPU_UCODE_ID_SDMA5:
2141 *type = GFX_FW_TYPE_SDMA5;
2143 case AMDGPU_UCODE_ID_SDMA6:
2144 *type = GFX_FW_TYPE_SDMA6;
2146 case AMDGPU_UCODE_ID_SDMA7:
2147 *type = GFX_FW_TYPE_SDMA7;
2149 case AMDGPU_UCODE_ID_CP_MES:
2150 *type = GFX_FW_TYPE_CP_MES;
2152 case AMDGPU_UCODE_ID_CP_MES_DATA:
2153 *type = GFX_FW_TYPE_MES_STACK;
2155 case AMDGPU_UCODE_ID_CP_MES1:
2156 *type = GFX_FW_TYPE_CP_MES_KIQ;
2158 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2159 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2161 case AMDGPU_UCODE_ID_CP_CE:
2162 *type = GFX_FW_TYPE_CP_CE;
2164 case AMDGPU_UCODE_ID_CP_PFP:
2165 *type = GFX_FW_TYPE_CP_PFP;
2167 case AMDGPU_UCODE_ID_CP_ME:
2168 *type = GFX_FW_TYPE_CP_ME;
2170 case AMDGPU_UCODE_ID_CP_MEC1:
2171 *type = GFX_FW_TYPE_CP_MEC;
2173 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2174 *type = GFX_FW_TYPE_CP_MEC_ME1;
2176 case AMDGPU_UCODE_ID_CP_MEC2:
2177 *type = GFX_FW_TYPE_CP_MEC;
2179 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2180 *type = GFX_FW_TYPE_CP_MEC_ME2;
2182 case AMDGPU_UCODE_ID_RLC_P:
2183 *type = GFX_FW_TYPE_RLC_P;
2185 case AMDGPU_UCODE_ID_RLC_V:
2186 *type = GFX_FW_TYPE_RLC_V;
2188 case AMDGPU_UCODE_ID_RLC_G:
2189 *type = GFX_FW_TYPE_RLC_G;
2191 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2192 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2194 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2195 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2197 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2198 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2200 case AMDGPU_UCODE_ID_RLC_IRAM:
2201 *type = GFX_FW_TYPE_RLC_IRAM;
2203 case AMDGPU_UCODE_ID_RLC_DRAM:
2204 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2206 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2207 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2209 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2210 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2212 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2213 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2215 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2216 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2218 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2219 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2221 case AMDGPU_UCODE_ID_SMC:
2222 *type = GFX_FW_TYPE_SMU;
2224 case AMDGPU_UCODE_ID_PPTABLE:
2225 *type = GFX_FW_TYPE_PPTABLE;
2227 case AMDGPU_UCODE_ID_UVD:
2228 *type = GFX_FW_TYPE_UVD;
2230 case AMDGPU_UCODE_ID_UVD1:
2231 *type = GFX_FW_TYPE_UVD1;
2233 case AMDGPU_UCODE_ID_VCE:
2234 *type = GFX_FW_TYPE_VCE;
2236 case AMDGPU_UCODE_ID_VCN:
2237 *type = GFX_FW_TYPE_VCN;
2239 case AMDGPU_UCODE_ID_VCN1:
2240 *type = GFX_FW_TYPE_VCN1;
2242 case AMDGPU_UCODE_ID_DMCU_ERAM:
2243 *type = GFX_FW_TYPE_DMCU_ERAM;
2245 case AMDGPU_UCODE_ID_DMCU_INTV:
2246 *type = GFX_FW_TYPE_DMCU_ISR;
2248 case AMDGPU_UCODE_ID_VCN0_RAM:
2249 *type = GFX_FW_TYPE_VCN0_RAM;
2251 case AMDGPU_UCODE_ID_VCN1_RAM:
2252 *type = GFX_FW_TYPE_VCN1_RAM;
2254 case AMDGPU_UCODE_ID_DMCUB:
2255 *type = GFX_FW_TYPE_DMUB;
2257 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2258 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2260 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2261 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2263 case AMDGPU_UCODE_ID_IMU_I:
2264 *type = GFX_FW_TYPE_IMU_I;
2266 case AMDGPU_UCODE_ID_IMU_D:
2267 *type = GFX_FW_TYPE_IMU_D;
2269 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2270 *type = GFX_FW_TYPE_RS64_PFP;
2272 case AMDGPU_UCODE_ID_CP_RS64_ME:
2273 *type = GFX_FW_TYPE_RS64_ME;
2275 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2276 *type = GFX_FW_TYPE_RS64_MEC;
2278 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2279 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2281 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2282 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2284 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2285 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2287 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2288 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2290 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2291 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2293 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2294 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2296 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2297 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2299 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2300 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2302 case AMDGPU_UCODE_ID_MAXIMUM:
2310 static void psp_print_fw_hdr(struct psp_context *psp,
2311 struct amdgpu_firmware_info *ucode)
2313 struct amdgpu_device *adev = psp->adev;
2314 struct common_firmware_header *hdr;
2316 switch (ucode->ucode_id) {
2317 case AMDGPU_UCODE_ID_SDMA0:
2318 case AMDGPU_UCODE_ID_SDMA1:
2319 case AMDGPU_UCODE_ID_SDMA2:
2320 case AMDGPU_UCODE_ID_SDMA3:
2321 case AMDGPU_UCODE_ID_SDMA4:
2322 case AMDGPU_UCODE_ID_SDMA5:
2323 case AMDGPU_UCODE_ID_SDMA6:
2324 case AMDGPU_UCODE_ID_SDMA7:
2325 hdr = (struct common_firmware_header *)
2326 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2327 amdgpu_ucode_print_sdma_hdr(hdr);
2329 case AMDGPU_UCODE_ID_CP_CE:
2330 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2331 amdgpu_ucode_print_gfx_hdr(hdr);
2333 case AMDGPU_UCODE_ID_CP_PFP:
2334 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2335 amdgpu_ucode_print_gfx_hdr(hdr);
2337 case AMDGPU_UCODE_ID_CP_ME:
2338 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2339 amdgpu_ucode_print_gfx_hdr(hdr);
2341 case AMDGPU_UCODE_ID_CP_MEC1:
2342 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2343 amdgpu_ucode_print_gfx_hdr(hdr);
2345 case AMDGPU_UCODE_ID_RLC_G:
2346 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2347 amdgpu_ucode_print_rlc_hdr(hdr);
2349 case AMDGPU_UCODE_ID_SMC:
2350 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2351 amdgpu_ucode_print_smc_hdr(hdr);
2358 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2359 struct psp_gfx_cmd_resp *cmd)
2362 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2364 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2365 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2366 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2367 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2369 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2371 DRM_ERROR("Unknown firmware type\n");
2376 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2377 struct amdgpu_firmware_info *ucode)
2380 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2382 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2384 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2385 psp->fence_buf_mc_addr);
2388 release_psp_cmd_buf(psp);
2393 static int psp_load_smu_fw(struct psp_context *psp)
2396 struct amdgpu_device *adev = psp->adev;
2397 struct amdgpu_firmware_info *ucode =
2398 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2399 struct amdgpu_ras *ras = psp->ras_context.ras;
2402 * Skip SMU FW reloading in case of using BACO for runpm only,
2403 * as SMU is always alive.
2405 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2408 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2411 if ((amdgpu_in_reset(adev) &&
2412 ras && adev->ras_enabled &&
2413 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2414 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2415 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2417 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2421 ret = psp_execute_non_psp_fw_load(psp, ucode);
2424 DRM_ERROR("PSP load smu failed!\n");
2429 static bool fw_load_skip_check(struct psp_context *psp,
2430 struct amdgpu_firmware_info *ucode)
2432 if (!ucode->fw || !ucode->ucode_size)
2435 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2436 (psp_smu_reload_quirk(psp) ||
2437 psp->autoload_supported ||
2438 psp->pmfw_centralized_cstate_management))
2441 if (amdgpu_sriov_vf(psp->adev) &&
2442 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2445 if (psp->autoload_supported &&
2446 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2447 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2448 /* skip mec JT when autoload is enabled */
2454 int psp_load_fw_list(struct psp_context *psp,
2455 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2458 struct amdgpu_firmware_info *ucode;
2460 for (i = 0; i < ucode_count; ++i) {
2461 ucode = ucode_list[i];
2462 psp_print_fw_hdr(psp, ucode);
2463 ret = psp_execute_non_psp_fw_load(psp, ucode);
2470 static int psp_load_non_psp_fw(struct psp_context *psp)
2473 struct amdgpu_firmware_info *ucode;
2474 struct amdgpu_device *adev = psp->adev;
2476 if (psp->autoload_supported &&
2477 !psp->pmfw_centralized_cstate_management) {
2478 ret = psp_load_smu_fw(psp);
2483 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2484 ucode = &adev->firmware.ucode[i];
2486 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2487 !fw_load_skip_check(psp, ucode)) {
2488 ret = psp_load_smu_fw(psp);
2494 if (fw_load_skip_check(psp, ucode))
2497 if (psp->autoload_supported &&
2498 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2499 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2500 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2501 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2502 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2503 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2504 /* PSP only receive one SDMA fw for sienna_cichlid,
2505 * as all four sdma fw are same */
2508 psp_print_fw_hdr(psp, ucode);
2510 ret = psp_execute_non_psp_fw_load(psp, ucode);
2514 /* Start rlc autoload after psp recieved all the gfx firmware */
2515 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2516 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2517 ret = psp_rlc_autoload_start(psp);
2519 DRM_ERROR("Failed to start rlc autoload\n");
2528 static int psp_load_fw(struct amdgpu_device *adev)
2531 struct psp_context *psp = &adev->psp;
2533 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2534 /* should not destroy ring, only stop */
2535 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2537 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2539 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2541 DRM_ERROR("PSP ring init failed!\n");
2546 ret = psp_hw_start(psp);
2550 ret = psp_load_non_psp_fw(psp);
2554 ret = psp_asd_initialize(psp);
2556 DRM_ERROR("PSP load asd failed!\n");
2560 ret = psp_rl_load(adev);
2562 DRM_ERROR("PSP load RL failed!\n");
2566 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2567 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2568 ret = psp_xgmi_initialize(psp, false, true);
2569 /* Warning the XGMI seesion initialize failure
2570 * Instead of stop driver initialization
2573 dev_err(psp->adev->dev,
2574 "XGMI: Failed to initialize XGMI session\n");
2579 ret = psp_ras_initialize(psp);
2581 dev_err(psp->adev->dev,
2582 "RAS: Failed to initialize RAS\n");
2584 ret = psp_hdcp_initialize(psp);
2586 dev_err(psp->adev->dev,
2587 "HDCP: Failed to initialize HDCP\n");
2589 ret = psp_dtm_initialize(psp);
2591 dev_err(psp->adev->dev,
2592 "DTM: Failed to initialize DTM\n");
2594 ret = psp_rap_initialize(psp);
2596 dev_err(psp->adev->dev,
2597 "RAP: Failed to initialize RAP\n");
2599 ret = psp_securedisplay_initialize(psp);
2601 dev_err(psp->adev->dev,
2602 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2608 psp_free_shared_bufs(psp);
2611 * all cleanup jobs (xgmi terminate, ras terminate,
2612 * ring destroy, cmd/fence/fw buffers destory,
2613 * psp->cmd destory) are delayed to psp_hw_fini
2615 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2619 static int psp_hw_init(void *handle)
2622 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2624 mutex_lock(&adev->firmware.mutex);
2626 * This sequence is just used on hw_init only once, no need on
2629 ret = amdgpu_ucode_init_bo(adev);
2633 ret = psp_load_fw(adev);
2635 DRM_ERROR("PSP firmware loading failed\n");
2639 mutex_unlock(&adev->firmware.mutex);
2643 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2644 mutex_unlock(&adev->firmware.mutex);
2648 static int psp_hw_fini(void *handle)
2650 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2651 struct psp_context *psp = &adev->psp;
2654 psp_ras_terminate(psp);
2655 psp_securedisplay_terminate(psp);
2656 psp_rap_terminate(psp);
2657 psp_dtm_terminate(psp);
2658 psp_hdcp_terminate(psp);
2660 if (adev->gmc.xgmi.num_physical_nodes > 1)
2661 psp_xgmi_terminate(psp);
2664 psp_asd_terminate(psp);
2665 psp_tmr_terminate(psp);
2667 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2669 psp_free_shared_bufs(psp);
2674 static int psp_suspend(void *handle)
2677 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2678 struct psp_context *psp = &adev->psp;
2680 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2681 psp->xgmi_context.context.initialized) {
2682 ret = psp_xgmi_terminate(psp);
2684 DRM_ERROR("Failed to terminate xgmi ta\n");
2690 ret = psp_ras_terminate(psp);
2692 DRM_ERROR("Failed to terminate ras ta\n");
2695 ret = psp_hdcp_terminate(psp);
2697 DRM_ERROR("Failed to terminate hdcp ta\n");
2700 ret = psp_dtm_terminate(psp);
2702 DRM_ERROR("Failed to terminate dtm ta\n");
2705 ret = psp_rap_terminate(psp);
2707 DRM_ERROR("Failed to terminate rap ta\n");
2710 ret = psp_securedisplay_terminate(psp);
2712 DRM_ERROR("Failed to terminate securedisplay ta\n");
2717 ret = psp_asd_terminate(psp);
2719 DRM_ERROR("Failed to terminate asd\n");
2723 ret = psp_tmr_terminate(psp);
2725 DRM_ERROR("Failed to terminate tmr\n");
2729 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2731 DRM_ERROR("PSP ring stop failed\n");
2735 psp_free_shared_bufs(psp);
2740 static int psp_resume(void *handle)
2743 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2744 struct psp_context *psp = &adev->psp;
2746 DRM_INFO("PSP is resuming...\n");
2748 if (psp->mem_train_ctx.enable_mem_training) {
2749 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2751 DRM_ERROR("Failed to process memory training!\n");
2756 mutex_lock(&adev->firmware.mutex);
2758 ret = psp_hw_start(psp);
2762 ret = psp_load_non_psp_fw(psp);
2766 ret = psp_asd_initialize(psp);
2768 DRM_ERROR("PSP load asd failed!\n");
2772 ret = psp_rl_load(adev);
2774 dev_err(adev->dev, "PSP load RL failed!\n");
2778 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2779 ret = psp_xgmi_initialize(psp, false, true);
2780 /* Warning the XGMI seesion initialize failure
2781 * Instead of stop driver initialization
2784 dev_err(psp->adev->dev,
2785 "XGMI: Failed to initialize XGMI session\n");
2789 ret = psp_ras_initialize(psp);
2791 dev_err(psp->adev->dev,
2792 "RAS: Failed to initialize RAS\n");
2794 ret = psp_hdcp_initialize(psp);
2796 dev_err(psp->adev->dev,
2797 "HDCP: Failed to initialize HDCP\n");
2799 ret = psp_dtm_initialize(psp);
2801 dev_err(psp->adev->dev,
2802 "DTM: Failed to initialize DTM\n");
2804 ret = psp_rap_initialize(psp);
2806 dev_err(psp->adev->dev,
2807 "RAP: Failed to initialize RAP\n");
2809 ret = psp_securedisplay_initialize(psp);
2811 dev_err(psp->adev->dev,
2812 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2815 mutex_unlock(&adev->firmware.mutex);
2820 DRM_ERROR("PSP resume failed\n");
2821 mutex_unlock(&adev->firmware.mutex);
2825 int psp_gpu_reset(struct amdgpu_device *adev)
2829 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2832 mutex_lock(&adev->psp.mutex);
2833 ret = psp_mode1_reset(&adev->psp);
2834 mutex_unlock(&adev->psp.mutex);
2839 int psp_rlc_autoload_start(struct psp_context *psp)
2842 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2844 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2846 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2847 psp->fence_buf_mc_addr);
2849 release_psp_cmd_buf(psp);
2854 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2855 uint64_t cmd_gpu_addr, int cmd_size)
2857 struct amdgpu_firmware_info ucode = {0};
2859 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2860 AMDGPU_UCODE_ID_VCN0_RAM;
2861 ucode.mc_addr = cmd_gpu_addr;
2862 ucode.ucode_size = cmd_size;
2864 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2867 int psp_ring_cmd_submit(struct psp_context *psp,
2868 uint64_t cmd_buf_mc_addr,
2869 uint64_t fence_mc_addr,
2872 unsigned int psp_write_ptr_reg = 0;
2873 struct psp_gfx_rb_frame *write_frame;
2874 struct psp_ring *ring = &psp->km_ring;
2875 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2876 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2877 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2878 struct amdgpu_device *adev = psp->adev;
2879 uint32_t ring_size_dw = ring->ring_size / 4;
2880 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2882 /* KM (GPCOM) prepare write pointer */
2883 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2885 /* Update KM RB frame pointer to new frame */
2886 /* write_frame ptr increments by size of rb_frame in bytes */
2887 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2888 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2889 write_frame = ring_buffer_start;
2891 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2892 /* Check invalid write_frame ptr address */
2893 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2894 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2895 ring_buffer_start, ring_buffer_end, write_frame);
2896 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2900 /* Initialize KM RB frame */
2901 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2903 /* Update KM RB frame */
2904 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2905 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2906 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2907 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2908 write_frame->fence_value = index;
2909 amdgpu_device_flush_hdp(adev, NULL);
2911 /* Update the write Pointer in DWORDs */
2912 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2913 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2917 int psp_init_asd_microcode(struct psp_context *psp,
2918 const char *chip_name)
2920 struct amdgpu_device *adev = psp->adev;
2921 char fw_name[PSP_FW_NAME_LEN];
2922 const struct psp_firmware_header_v1_0 *asd_hdr;
2926 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2930 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2931 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2935 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2939 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2940 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2941 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2942 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2943 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2944 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2947 dev_err(adev->dev, "fail to initialize asd microcode\n");
2948 release_firmware(adev->psp.asd_fw);
2949 adev->psp.asd_fw = NULL;
2953 int psp_init_toc_microcode(struct psp_context *psp,
2954 const char *chip_name)
2956 struct amdgpu_device *adev = psp->adev;
2957 char fw_name[PSP_FW_NAME_LEN];
2958 const struct psp_firmware_header_v1_0 *toc_hdr;
2962 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2966 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2967 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2971 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2975 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2976 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2977 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2978 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2979 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2980 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2983 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2984 release_firmware(adev->psp.toc_fw);
2985 adev->psp.toc_fw = NULL;
2989 static int parse_sos_bin_descriptor(struct psp_context *psp,
2990 const struct psp_fw_bin_desc *desc,
2991 const struct psp_firmware_header_v2_0 *sos_hdr)
2993 uint8_t *ucode_start_addr = NULL;
2995 if (!psp || !desc || !sos_hdr)
2998 ucode_start_addr = (uint8_t *)sos_hdr +
2999 le32_to_cpu(desc->offset_bytes) +
3000 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3002 switch (desc->fw_type) {
3003 case PSP_FW_TYPE_PSP_SOS:
3004 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3005 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3006 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3007 psp->sos.start_addr = ucode_start_addr;
3009 case PSP_FW_TYPE_PSP_SYS_DRV:
3010 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3011 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3012 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3013 psp->sys.start_addr = ucode_start_addr;
3015 case PSP_FW_TYPE_PSP_KDB:
3016 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3017 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3018 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3019 psp->kdb.start_addr = ucode_start_addr;
3021 case PSP_FW_TYPE_PSP_TOC:
3022 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3023 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3024 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3025 psp->toc.start_addr = ucode_start_addr;
3027 case PSP_FW_TYPE_PSP_SPL:
3028 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3029 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3030 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3031 psp->spl.start_addr = ucode_start_addr;
3033 case PSP_FW_TYPE_PSP_RL:
3034 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3035 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3036 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3037 psp->rl.start_addr = ucode_start_addr;
3039 case PSP_FW_TYPE_PSP_SOC_DRV:
3040 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3041 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3042 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3043 psp->soc_drv.start_addr = ucode_start_addr;
3045 case PSP_FW_TYPE_PSP_INTF_DRV:
3046 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3047 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3048 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3049 psp->intf_drv.start_addr = ucode_start_addr;
3051 case PSP_FW_TYPE_PSP_DBG_DRV:
3052 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3053 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3054 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3055 psp->dbg_drv.start_addr = ucode_start_addr;
3057 case PSP_FW_TYPE_PSP_RAS_DRV:
3058 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3059 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3060 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3061 psp->ras_drv.start_addr = ucode_start_addr;
3064 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3071 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3073 const struct psp_firmware_header_v1_0 *sos_hdr;
3074 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3075 uint8_t *ucode_array_start_addr;
3077 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3078 ucode_array_start_addr = (uint8_t *)sos_hdr +
3079 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3081 if (adev->gmc.xgmi.connected_to_cpu ||
3082 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3083 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3084 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3086 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3087 adev->psp.sys.start_addr = ucode_array_start_addr;
3089 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3090 adev->psp.sos.start_addr = ucode_array_start_addr +
3091 le32_to_cpu(sos_hdr->sos.offset_bytes);
3093 /* Load alternate PSP SOS FW */
3094 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3096 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3097 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3099 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3100 adev->psp.sys.start_addr = ucode_array_start_addr +
3101 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3103 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3104 adev->psp.sos.start_addr = ucode_array_start_addr +
3105 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3108 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3109 dev_warn(adev->dev, "PSP SOS FW not available");
3116 int psp_init_sos_microcode(struct psp_context *psp,
3117 const char *chip_name)
3119 struct amdgpu_device *adev = psp->adev;
3120 char fw_name[PSP_FW_NAME_LEN];
3121 const struct psp_firmware_header_v1_0 *sos_hdr;
3122 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3123 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3124 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3125 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3127 uint8_t *ucode_array_start_addr;
3131 dev_err(adev->dev, "invalid chip name for sos microcode\n");
3135 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3136 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
3140 err = amdgpu_ucode_validate(adev->psp.sos_fw);
3144 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3145 ucode_array_start_addr = (uint8_t *)sos_hdr +
3146 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3147 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3149 switch (sos_hdr->header.header_version_major) {
3151 err = psp_init_sos_base_fw(adev);
3155 if (sos_hdr->header.header_version_minor == 1) {
3156 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3157 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3158 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3159 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3160 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3161 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3162 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3164 if (sos_hdr->header.header_version_minor == 2) {
3165 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3166 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3167 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3168 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3170 if (sos_hdr->header.header_version_minor == 3) {
3171 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3172 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3173 adev->psp.toc.start_addr = ucode_array_start_addr +
3174 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3175 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3176 adev->psp.kdb.start_addr = ucode_array_start_addr +
3177 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3178 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3179 adev->psp.spl.start_addr = ucode_array_start_addr +
3180 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3181 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3182 adev->psp.rl.start_addr = ucode_array_start_addr +
3183 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3187 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3189 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3190 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3195 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3196 err = parse_sos_bin_descriptor(psp,
3197 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3205 "unsupported psp sos firmware\n");
3213 "failed to init sos firmware\n");
3214 release_firmware(adev->psp.sos_fw);
3215 adev->psp.sos_fw = NULL;
3220 static int parse_ta_bin_descriptor(struct psp_context *psp,
3221 const struct psp_fw_bin_desc *desc,
3222 const struct ta_firmware_header_v2_0 *ta_hdr)
3224 uint8_t *ucode_start_addr = NULL;
3226 if (!psp || !desc || !ta_hdr)
3229 ucode_start_addr = (uint8_t *)ta_hdr +
3230 le32_to_cpu(desc->offset_bytes) +
3231 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3233 switch (desc->fw_type) {
3234 case TA_FW_TYPE_PSP_ASD:
3235 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3236 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3237 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3238 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3240 case TA_FW_TYPE_PSP_XGMI:
3241 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3242 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3243 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3245 case TA_FW_TYPE_PSP_RAS:
3246 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3247 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3248 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3250 case TA_FW_TYPE_PSP_HDCP:
3251 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3252 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3253 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3255 case TA_FW_TYPE_PSP_DTM:
3256 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3257 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3258 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3260 case TA_FW_TYPE_PSP_RAP:
3261 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3262 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3263 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3265 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3266 psp->securedisplay_context.context.bin_desc.fw_version =
3267 le32_to_cpu(desc->fw_version);
3268 psp->securedisplay_context.context.bin_desc.size_bytes =
3269 le32_to_cpu(desc->size_bytes);
3270 psp->securedisplay_context.context.bin_desc.start_addr =
3274 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3281 int psp_init_ta_microcode(struct psp_context *psp,
3282 const char *chip_name)
3284 struct amdgpu_device *adev = psp->adev;
3285 char fw_name[PSP_FW_NAME_LEN];
3286 const struct ta_firmware_header_v2_0 *ta_hdr;
3291 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3295 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3296 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3300 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3304 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3306 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3307 dev_err(adev->dev, "unsupported TA header version\n");
3312 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3313 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3318 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3319 err = parse_ta_bin_descriptor(psp,
3320 &ta_hdr->ta_fw_bin[ta_index],
3328 dev_err(adev->dev, "fail to initialize ta microcode\n");
3329 release_firmware(adev->psp.ta_fw);
3330 adev->psp.ta_fw = NULL;
3334 int psp_init_cap_microcode(struct psp_context *psp,
3335 const char *chip_name)
3337 struct amdgpu_device *adev = psp->adev;
3338 char fw_name[PSP_FW_NAME_LEN];
3339 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3340 struct amdgpu_firmware_info *info = NULL;
3344 dev_err(adev->dev, "invalid chip name for cap microcode\n");
3348 if (!amdgpu_sriov_vf(adev)) {
3349 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3353 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3354 err = request_firmware(&adev->psp.cap_fw, fw_name, adev->dev);
3356 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3361 err = amdgpu_ucode_validate(adev->psp.cap_fw);
3363 dev_err(adev->dev, "fail to initialize cap microcode\n");
3367 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3368 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3369 info->fw = adev->psp.cap_fw;
3370 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3371 adev->psp.cap_fw->data;
3372 adev->firmware.fw_size += ALIGN(
3373 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3374 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3375 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3376 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3381 release_firmware(adev->psp.cap_fw);
3382 adev->psp.cap_fw = NULL;
3386 static int psp_set_clockgating_state(void *handle,
3387 enum amd_clockgating_state state)
3392 static int psp_set_powergating_state(void *handle,
3393 enum amd_powergating_state state)
3398 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3399 struct device_attribute *attr,
3402 struct drm_device *ddev = dev_get_drvdata(dev);
3403 struct amdgpu_device *adev = drm_to_adev(ddev);
3407 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3408 DRM_INFO("PSP block is not ready yet.");
3412 mutex_lock(&adev->psp.mutex);
3413 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3414 mutex_unlock(&adev->psp.mutex);
3417 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3421 return sysfs_emit(buf, "%x\n", fw_ver);
3424 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3425 struct device_attribute *attr,
3429 struct drm_device *ddev = dev_get_drvdata(dev);
3430 struct amdgpu_device *adev = drm_to_adev(ddev);
3433 const struct firmware *usbc_pd_fw;
3434 struct amdgpu_bo *fw_buf_bo = NULL;
3435 uint64_t fw_pri_mc_addr;
3436 void *fw_pri_cpu_addr;
3438 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3439 DRM_INFO("PSP block is not ready yet.");
3443 if (!drm_dev_enter(ddev, &idx))
3446 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3447 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3451 /* LFB address which is aligned to 1MB boundary per PSP request */
3452 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3453 AMDGPU_GEM_DOMAIN_VRAM,
3460 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3462 mutex_lock(&adev->psp.mutex);
3463 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3464 mutex_unlock(&adev->psp.mutex);
3466 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3469 release_firmware(usbc_pd_fw);
3472 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3480 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3484 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3487 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3488 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3493 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3494 psp_usbc_pd_fw_sysfs_read,
3495 psp_usbc_pd_fw_sysfs_write);
3497 int is_psp_fw_valid(struct psp_bin_desc bin)
3499 return bin.size_bytes;
3502 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3503 struct bin_attribute *bin_attr,
3504 char *buffer, loff_t pos, size_t count)
3506 struct device *dev = kobj_to_dev(kobj);
3507 struct drm_device *ddev = dev_get_drvdata(dev);
3508 struct amdgpu_device *adev = drm_to_adev(ddev);
3510 adev->psp.vbflash_done = false;
3512 /* Safeguard against memory drain */
3513 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3514 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3515 kvfree(adev->psp.vbflash_tmp_buf);
3516 adev->psp.vbflash_tmp_buf = NULL;
3517 adev->psp.vbflash_image_size = 0;
3521 /* TODO Just allocate max for now and optimize to realloc later if needed */
3522 if (!adev->psp.vbflash_tmp_buf) {
3523 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3524 if (!adev->psp.vbflash_tmp_buf)
3528 mutex_lock(&adev->psp.mutex);
3529 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3530 adev->psp.vbflash_image_size += count;
3531 mutex_unlock(&adev->psp.mutex);
3533 dev_info(adev->dev, "VBIOS flash write PSP done");
3538 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3539 struct bin_attribute *bin_attr, char *buffer,
3540 loff_t pos, size_t count)
3542 struct device *dev = kobj_to_dev(kobj);
3543 struct drm_device *ddev = dev_get_drvdata(dev);
3544 struct amdgpu_device *adev = drm_to_adev(ddev);
3545 struct amdgpu_bo *fw_buf_bo = NULL;
3546 uint64_t fw_pri_mc_addr;
3547 void *fw_pri_cpu_addr;
3550 dev_info(adev->dev, "VBIOS flash to PSP started");
3552 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3553 AMDGPU_GPU_PAGE_SIZE,
3554 AMDGPU_GEM_DOMAIN_VRAM,
3561 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3563 mutex_lock(&adev->psp.mutex);
3564 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3565 mutex_unlock(&adev->psp.mutex);
3567 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3570 kvfree(adev->psp.vbflash_tmp_buf);
3571 adev->psp.vbflash_tmp_buf = NULL;
3572 adev->psp.vbflash_image_size = 0;
3575 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3579 dev_info(adev->dev, "VBIOS flash to PSP done");
3583 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3584 struct device_attribute *attr,
3587 struct drm_device *ddev = dev_get_drvdata(dev);
3588 struct amdgpu_device *adev = drm_to_adev(ddev);
3589 uint32_t vbflash_status;
3591 vbflash_status = psp_vbflash_status(&adev->psp);
3592 if (!adev->psp.vbflash_done)
3594 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3597 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3600 static const struct bin_attribute psp_vbflash_bin_attr = {
3601 .attr = {.name = "psp_vbflash", .mode = 0664},
3603 .write = amdgpu_psp_vbflash_write,
3604 .read = amdgpu_psp_vbflash_read,
3607 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3609 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3612 struct psp_context *psp = &adev->psp;
3614 if (amdgpu_sriov_vf(adev))
3617 switch (adev->ip_versions[MP0_HWIP][0]) {
3618 case IP_VERSION(13, 0, 0):
3619 case IP_VERSION(13, 0, 7):
3622 psp_v13_0_set_psp_funcs(psp);
3624 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3626 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3627 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3629 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3636 const struct amd_ip_funcs psp_ip_funcs = {
3638 .early_init = psp_early_init,
3640 .sw_init = psp_sw_init,
3641 .sw_fini = psp_sw_fini,
3642 .hw_init = psp_hw_init,
3643 .hw_fini = psp_hw_fini,
3644 .suspend = psp_suspend,
3645 .resume = psp_resume,
3647 .check_soft_reset = NULL,
3648 .wait_for_idle = NULL,
3650 .set_clockgating_state = psp_set_clockgating_state,
3651 .set_powergating_state = psp_set_powergating_state,
3654 static int psp_sysfs_init(struct amdgpu_device *adev)
3656 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3659 DRM_ERROR("Failed to create USBC PD FW control file!");
3664 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3666 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3667 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3670 static void psp_sysfs_fini(struct amdgpu_device *adev)
3672 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3675 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3677 .type = AMD_IP_BLOCK_TYPE_PSP,
3681 .funcs = &psp_ip_funcs,
3684 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3686 .type = AMD_IP_BLOCK_TYPE_PSP,
3690 .funcs = &psp_ip_funcs,
3693 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3695 .type = AMD_IP_BLOCK_TYPE_PSP,
3699 .funcs = &psp_ip_funcs,
3702 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3703 .type = AMD_IP_BLOCK_TYPE_PSP,
3707 .funcs = &psp_ip_funcs,
3710 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3712 .type = AMD_IP_BLOCK_TYPE_PSP,
3716 .funcs = &psp_ip_funcs,
3719 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3720 .type = AMD_IP_BLOCK_TYPE_PSP,
3724 .funcs = &psp_ip_funcs,
3727 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3728 .type = AMD_IP_BLOCK_TYPE_PSP,
3732 .funcs = &psp_ip_funcs,