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);
55 static int psp_ring_init(struct psp_context *psp,
56 enum psp_ring_type ring_type)
59 struct psp_ring *ring;
60 struct amdgpu_device *adev = psp->adev;
64 ring->ring_type = ring_type;
66 /* allocate 4k Page of Local Frame Buffer memory for ring */
67 ring->ring_size = 0x1000;
68 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
69 AMDGPU_GEM_DOMAIN_VRAM,
71 &ring->ring_mem_mc_addr,
72 (void **)&ring->ring_mem);
82 * Due to DF Cstate management centralized to PMFW, the firmware
83 * loading sequence will be updated as below:
89 * - Load other non-psp fw
91 * - Load XGMI/RAS/HDCP/DTM TA if any
93 * This new sequence is required for
94 * - Arcturus and onwards
96 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
98 struct amdgpu_device *adev = psp->adev;
100 if (amdgpu_sriov_vf(adev)) {
101 psp->pmfw_centralized_cstate_management = false;
105 switch (adev->ip_versions[MP0_HWIP][0]) {
106 case IP_VERSION(11, 0, 0):
107 case IP_VERSION(11, 0, 4):
108 case IP_VERSION(11, 0, 5):
109 case IP_VERSION(11, 0, 7):
110 case IP_VERSION(11, 0, 9):
111 case IP_VERSION(11, 0, 11):
112 case IP_VERSION(11, 0, 12):
113 case IP_VERSION(11, 0, 13):
114 case IP_VERSION(13, 0, 0):
115 case IP_VERSION(13, 0, 2):
116 case IP_VERSION(13, 0, 7):
117 psp->pmfw_centralized_cstate_management = true;
120 psp->pmfw_centralized_cstate_management = false;
125 static int psp_early_init(void *handle)
127 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
128 struct psp_context *psp = &adev->psp;
130 switch (adev->ip_versions[MP0_HWIP][0]) {
131 case IP_VERSION(9, 0, 0):
132 psp_v3_1_set_psp_funcs(psp);
133 psp->autoload_supported = false;
135 case IP_VERSION(10, 0, 0):
136 case IP_VERSION(10, 0, 1):
137 psp_v10_0_set_psp_funcs(psp);
138 psp->autoload_supported = false;
140 case IP_VERSION(11, 0, 2):
141 case IP_VERSION(11, 0, 4):
142 psp_v11_0_set_psp_funcs(psp);
143 psp->autoload_supported = false;
145 case IP_VERSION(11, 0, 0):
146 case IP_VERSION(11, 0, 5):
147 case IP_VERSION(11, 0, 9):
148 case IP_VERSION(11, 0, 7):
149 case IP_VERSION(11, 0, 11):
150 case IP_VERSION(11, 5, 0):
151 case IP_VERSION(11, 0, 12):
152 case IP_VERSION(11, 0, 13):
153 psp_v11_0_set_psp_funcs(psp);
154 psp->autoload_supported = true;
156 case IP_VERSION(11, 0, 3):
157 case IP_VERSION(12, 0, 1):
158 psp_v12_0_set_psp_funcs(psp);
160 case IP_VERSION(13, 0, 2):
161 psp_v13_0_set_psp_funcs(psp);
163 case IP_VERSION(13, 0, 1):
164 case IP_VERSION(13, 0, 3):
165 case IP_VERSION(13, 0, 5):
166 case IP_VERSION(13, 0, 8):
167 case IP_VERSION(13, 0, 10):
168 psp_v13_0_set_psp_funcs(psp);
169 psp->autoload_supported = true;
171 case IP_VERSION(11, 0, 8):
172 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
173 psp_v11_0_8_set_psp_funcs(psp);
174 psp->autoload_supported = false;
177 case IP_VERSION(13, 0, 0):
178 case IP_VERSION(13, 0, 7):
179 psp_v13_0_set_psp_funcs(psp);
180 psp->autoload_supported = true;
182 case IP_VERSION(13, 0, 4):
183 psp_v13_0_4_set_psp_funcs(psp);
184 psp->autoload_supported = true;
192 psp_check_pmfw_centralized_cstate_management(psp);
197 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
199 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
200 &mem_ctx->shared_buf);
203 static void psp_free_shared_bufs(struct psp_context *psp)
208 /* free TMR memory buffer */
209 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
210 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
212 /* free xgmi shared memory */
213 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
215 /* free ras shared memory */
216 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
218 /* free hdcp shared memory */
219 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
221 /* free dtm shared memory */
222 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
224 /* free rap shared memory */
225 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
227 /* free securedisplay shared memory */
228 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
233 static void psp_memory_training_fini(struct psp_context *psp)
235 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
237 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
238 kfree(ctx->sys_cache);
239 ctx->sys_cache = NULL;
242 static int psp_memory_training_init(struct psp_context *psp)
245 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
247 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
248 DRM_DEBUG("memory training is not supported!\n");
252 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
253 if (ctx->sys_cache == NULL) {
254 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
259 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
260 ctx->train_data_size,
261 ctx->p2c_train_data_offset,
262 ctx->c2p_train_data_offset);
263 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
267 psp_memory_training_fini(psp);
272 * Helper funciton to query psp runtime database entry
274 * @adev: amdgpu_device pointer
275 * @entry_type: the type of psp runtime database entry
276 * @db_entry: runtime database entry pointer
278 * Return false if runtime database doesn't exit or entry is invalid
279 * or true if the specific database entry is found, and copy to @db_entry
281 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
282 enum psp_runtime_entry_type entry_type,
285 uint64_t db_header_pos, db_dir_pos;
286 struct psp_runtime_data_header db_header = {0};
287 struct psp_runtime_data_directory db_dir = {0};
291 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
292 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
294 /* read runtime db header from vram */
295 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
296 sizeof(struct psp_runtime_data_header), false);
298 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
299 /* runtime db doesn't exist, exit */
300 dev_warn(adev->dev, "PSP runtime database doesn't exist\n");
304 /* read runtime database entry from vram */
305 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
306 sizeof(struct psp_runtime_data_directory), false);
308 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
309 /* invalid db entry count, exit */
310 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
314 /* look up for requested entry type */
315 for (i = 0; i < db_dir.entry_count && !ret; i++) {
316 if (db_dir.entry_list[i].entry_type == entry_type) {
317 switch (entry_type) {
318 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
319 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
320 /* invalid db entry size */
321 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
324 /* read runtime database entry */
325 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
326 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
329 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
330 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
331 /* invalid db entry size */
332 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
335 /* read runtime database entry */
336 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
337 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
350 static int psp_init_sriov_microcode(struct psp_context *psp)
352 struct amdgpu_device *adev = psp->adev;
355 switch (adev->ip_versions[MP0_HWIP][0]) {
356 case IP_VERSION(9, 0, 0):
357 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
358 ret = psp_init_cap_microcode(psp, "vega10");
360 case IP_VERSION(11, 0, 9):
361 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
362 ret = psp_init_cap_microcode(psp, "navi12");
364 case IP_VERSION(11, 0, 7):
365 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
366 ret = psp_init_cap_microcode(psp, "sienna_cichlid");
368 case IP_VERSION(13, 0, 2):
369 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
370 ret = psp_init_cap_microcode(psp, "aldebaran");
371 ret &= psp_init_ta_microcode(psp, "aldebaran");
373 case IP_VERSION(13, 0, 0):
374 adev->virt.autoload_ucode_id = 0;
376 case IP_VERSION(13, 0, 10):
377 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
386 static int psp_sw_init(void *handle)
388 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
389 struct psp_context *psp = &adev->psp;
391 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
392 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
393 struct psp_runtime_scpm_entry scpm_entry;
395 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
397 DRM_ERROR("Failed to allocate memory to command buffer!\n");
401 if (amdgpu_sriov_vf(adev))
402 ret = psp_init_sriov_microcode(psp);
404 ret = psp_init_microcode(psp);
406 DRM_ERROR("Failed to load psp firmware!\n");
410 adev->psp.xgmi_context.supports_extended_data =
411 !adev->gmc.xgmi.connected_to_cpu &&
412 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
414 memset(&scpm_entry, 0, sizeof(scpm_entry));
415 if ((psp_get_runtime_db_entry(adev,
416 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
418 (SCPM_DISABLE != scpm_entry.scpm_status)) {
419 adev->scpm_enabled = true;
420 adev->scpm_status = scpm_entry.scpm_status;
422 adev->scpm_enabled = false;
423 adev->scpm_status = SCPM_DISABLE;
426 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
428 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
429 if (psp_get_runtime_db_entry(adev,
430 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
432 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
433 if ((psp->boot_cfg_bitmask) &
434 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
435 /* If psp runtime database exists, then
436 * only enable two stage memory training
437 * when TWO_STAGE_DRAM_TRAINING bit is set
438 * in runtime database */
439 mem_training_ctx->enable_mem_training = true;
443 /* If psp runtime database doesn't exist or
444 * is invalid, force enable two stage memory
446 mem_training_ctx->enable_mem_training = true;
449 if (mem_training_ctx->enable_mem_training) {
450 ret = psp_memory_training_init(psp);
452 DRM_ERROR("Failed to initialize memory training!\n");
456 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
458 DRM_ERROR("Failed to process memory training!\n");
463 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
464 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
465 ret= psp_sysfs_init(adev);
471 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
472 amdgpu_sriov_vf(adev) ?
473 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
475 &psp->fw_pri_mc_addr,
480 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
481 AMDGPU_GEM_DOMAIN_VRAM,
483 &psp->fence_buf_mc_addr,
488 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
489 AMDGPU_GEM_DOMAIN_VRAM,
490 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
491 (void **)&psp->cmd_buf_mem);
498 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
499 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
501 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
502 &psp->fence_buf_mc_addr, &psp->fence_buf);
506 static int psp_sw_fini(void *handle)
508 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
509 struct psp_context *psp = &adev->psp;
510 struct psp_gfx_cmd_resp *cmd = psp->cmd;
512 psp_memory_training_fini(psp);
514 release_firmware(psp->sos_fw);
518 release_firmware(psp->asd_fw);
522 release_firmware(psp->ta_fw);
526 release_firmware(psp->cap_fw);
530 release_firmware(psp->toc_fw);
533 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
534 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
535 psp_sysfs_fini(adev);
540 if (psp->km_ring.ring_mem)
541 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
542 &psp->km_ring.ring_mem_mc_addr,
543 (void **)&psp->km_ring.ring_mem);
545 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
546 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
547 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
548 &psp->fence_buf_mc_addr, &psp->fence_buf);
549 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
550 (void **)&psp->cmd_buf_mem);
555 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
556 uint32_t reg_val, uint32_t mask, bool check_changed)
560 struct amdgpu_device *adev = psp->adev;
562 if (psp->adev->no_hw_access)
565 for (i = 0; i < adev->usec_timeout; i++) {
566 val = RREG32(reg_index);
571 if ((val & mask) == reg_val)
580 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
583 case GFX_CMD_ID_LOAD_TA:
585 case GFX_CMD_ID_UNLOAD_TA:
587 case GFX_CMD_ID_INVOKE_CMD:
589 case GFX_CMD_ID_LOAD_ASD:
591 case GFX_CMD_ID_SETUP_TMR:
593 case GFX_CMD_ID_LOAD_IP_FW:
595 case GFX_CMD_ID_DESTROY_TMR:
596 return "DESTROY_TMR";
597 case GFX_CMD_ID_SAVE_RESTORE:
598 return "SAVE_RESTORE_IP_FW";
599 case GFX_CMD_ID_SETUP_VMR:
601 case GFX_CMD_ID_DESTROY_VMR:
602 return "DESTROY_VMR";
603 case GFX_CMD_ID_PROG_REG:
605 case GFX_CMD_ID_GET_FW_ATTESTATION:
606 return "GET_FW_ATTESTATION";
607 case GFX_CMD_ID_LOAD_TOC:
608 return "ID_LOAD_TOC";
609 case GFX_CMD_ID_AUTOLOAD_RLC:
610 return "AUTOLOAD_RLC";
611 case GFX_CMD_ID_BOOT_CFG:
614 return "UNKNOWN CMD";
619 psp_cmd_submit_buf(struct psp_context *psp,
620 struct amdgpu_firmware_info *ucode,
621 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
626 bool ras_intr = false;
627 bool skip_unsupport = false;
629 if (psp->adev->no_hw_access)
632 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
635 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
637 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
639 index = atomic_inc_return(&psp->fence_value);
640 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
642 atomic_dec(&psp->fence_value);
646 amdgpu_device_invalidate_hdp(psp->adev, NULL);
647 while (*((unsigned int *)psp->fence_buf) != index) {
651 * Shouldn't wait for timeout when err_event_athub occurs,
652 * because gpu reset thread triggered and lock resource should
653 * be released for psp resume sequence.
655 ras_intr = amdgpu_ras_intr_triggered();
658 usleep_range(10, 100);
659 amdgpu_device_invalidate_hdp(psp->adev, NULL);
662 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
663 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
664 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
666 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
668 /* In some cases, psp response status is not 0 even there is no
669 * problem while the command is submitted. Some version of PSP FW
670 * doesn't write 0 to that field.
671 * So here we would like to only print a warning instead of an error
672 * during psp initialization to avoid breaking hw_init and it doesn't
675 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
677 DRM_WARN("failed to load ucode %s(0x%X) ",
678 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
679 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
680 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
681 psp->cmd_buf_mem->resp.status);
682 /* If any firmware (including CAP) load fails under SRIOV, it should
683 * return failure to stop the VF from initializing.
684 * Also return failure in case of timeout
686 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
693 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
694 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
702 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
704 struct psp_gfx_cmd_resp *cmd = psp->cmd;
706 mutex_lock(&psp->mutex);
708 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
713 static void release_psp_cmd_buf(struct psp_context *psp)
715 mutex_unlock(&psp->mutex);
718 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
719 struct psp_gfx_cmd_resp *cmd,
720 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
722 struct amdgpu_device *adev = psp->adev;
723 uint32_t size = amdgpu_bo_size(tmr_bo);
724 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
726 if (amdgpu_sriov_vf(psp->adev))
727 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
729 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
730 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
731 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
732 cmd->cmd.cmd_setup_tmr.buf_size = size;
733 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
734 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
735 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
738 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
739 uint64_t pri_buf_mc, uint32_t size)
741 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
742 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
743 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
744 cmd->cmd.cmd_load_toc.toc_size = size;
747 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
748 static int psp_load_toc(struct psp_context *psp,
752 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
754 /* Copy toc to psp firmware private buffer */
755 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
757 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
759 ret = psp_cmd_submit_buf(psp, NULL, cmd,
760 psp->fence_buf_mc_addr);
762 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
764 release_psp_cmd_buf(psp);
769 /* Set up Trusted Memory Region */
770 static int psp_tmr_init(struct psp_context *psp)
778 * According to HW engineer, they prefer the TMR address be "naturally
779 * aligned" , e.g. the start address be an integer divide of TMR size.
781 * Note: this memory need be reserved till the driver
784 tmr_size = PSP_TMR_SIZE(psp->adev);
786 /* For ASICs support RLC autoload, psp will parse the toc
787 * and calculate the total size of TMR needed */
788 if (!amdgpu_sriov_vf(psp->adev) &&
789 psp->toc.start_addr &&
790 psp->toc.size_bytes &&
792 ret = psp_load_toc(psp, &tmr_size);
794 DRM_ERROR("Failed to load toc\n");
799 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
800 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_ALIGNMENT,
801 AMDGPU_GEM_DOMAIN_VRAM,
802 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
807 static bool psp_skip_tmr(struct psp_context *psp)
809 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
810 case IP_VERSION(11, 0, 9):
811 case IP_VERSION(11, 0, 7):
812 case IP_VERSION(13, 0, 2):
813 case IP_VERSION(13, 0, 10):
820 static int psp_tmr_load(struct psp_context *psp)
823 struct psp_gfx_cmd_resp *cmd;
825 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
826 * Already set up by host driver.
828 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
831 cmd = acquire_psp_cmd_buf(psp);
833 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
834 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
835 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
837 ret = psp_cmd_submit_buf(psp, NULL, cmd,
838 psp->fence_buf_mc_addr);
840 release_psp_cmd_buf(psp);
845 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
846 struct psp_gfx_cmd_resp *cmd)
848 if (amdgpu_sriov_vf(psp->adev))
849 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
851 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
854 static int psp_tmr_unload(struct psp_context *psp)
857 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
859 psp_prep_tmr_unload_cmd_buf(psp, cmd);
860 dev_info(psp->adev->dev, "free PSP TMR buffer\n");
862 ret = psp_cmd_submit_buf(psp, NULL, cmd,
863 psp->fence_buf_mc_addr);
865 release_psp_cmd_buf(psp);
870 static int psp_tmr_terminate(struct psp_context *psp)
872 return psp_tmr_unload(psp);
875 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
876 uint64_t *output_ptr)
879 struct psp_gfx_cmd_resp *cmd;
884 if (amdgpu_sriov_vf(psp->adev))
887 cmd = acquire_psp_cmd_buf(psp);
889 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
891 ret = psp_cmd_submit_buf(psp, NULL, cmd,
892 psp->fence_buf_mc_addr);
895 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
896 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
899 release_psp_cmd_buf(psp);
904 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
906 struct psp_context *psp = &adev->psp;
907 struct psp_gfx_cmd_resp *cmd;
910 if (amdgpu_sriov_vf(adev))
913 cmd = acquire_psp_cmd_buf(psp);
915 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
916 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
918 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
921 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
924 release_psp_cmd_buf(psp);
929 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
932 struct psp_context *psp = &adev->psp;
933 struct psp_gfx_cmd_resp *cmd;
935 if (amdgpu_sriov_vf(adev))
938 cmd = acquire_psp_cmd_buf(psp);
940 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
941 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
942 cmd->cmd.boot_cfg.boot_config = boot_cfg;
943 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
945 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
947 release_psp_cmd_buf(psp);
952 static int psp_rl_load(struct amdgpu_device *adev)
955 struct psp_context *psp = &adev->psp;
956 struct psp_gfx_cmd_resp *cmd;
958 if (!is_psp_fw_valid(psp->rl))
961 cmd = acquire_psp_cmd_buf(psp);
963 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
964 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
966 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
967 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
968 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
969 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
970 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
972 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
974 release_psp_cmd_buf(psp);
979 static int psp_asd_initialize(struct psp_context *psp)
983 /* If PSP version doesn't match ASD version, asd loading will be failed.
984 * add workaround to bypass it for sriov now.
985 * TODO: add version check to make it common
987 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
990 psp->asd_context.mem_context.shared_mc_addr = 0;
991 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
992 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
994 ret = psp_ta_load(psp, &psp->asd_context);
996 psp->asd_context.initialized = true;
1001 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1002 uint32_t session_id)
1004 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1005 cmd->cmd.cmd_unload_ta.session_id = session_id;
1008 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1011 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1013 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1015 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1017 context->resp_status = cmd->resp.status;
1019 release_psp_cmd_buf(psp);
1024 static int psp_asd_terminate(struct psp_context *psp)
1028 if (amdgpu_sriov_vf(psp->adev))
1031 if (!psp->asd_context.initialized)
1034 ret = psp_ta_unload(psp, &psp->asd_context);
1036 psp->asd_context.initialized = false;
1041 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1042 uint32_t id, uint32_t value)
1044 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1045 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1046 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1049 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1052 struct psp_gfx_cmd_resp *cmd;
1055 if (reg >= PSP_REG_LAST)
1058 cmd = acquire_psp_cmd_buf(psp);
1060 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1061 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1063 DRM_ERROR("PSP failed to program reg id %d", reg);
1065 release_psp_cmd_buf(psp);
1070 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1072 struct ta_context *context)
1074 cmd->cmd_id = context->ta_load_type;
1075 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1076 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1077 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1079 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1080 lower_32_bits(context->mem_context.shared_mc_addr);
1081 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1082 upper_32_bits(context->mem_context.shared_mc_addr);
1083 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1086 int psp_ta_init_shared_buf(struct psp_context *psp,
1087 struct ta_mem_context *mem_ctx)
1090 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1091 * physical) for ta to host memory
1093 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1094 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1095 &mem_ctx->shared_bo,
1096 &mem_ctx->shared_mc_addr,
1097 &mem_ctx->shared_buf);
1100 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1102 uint32_t session_id)
1104 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1105 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1106 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1109 int psp_ta_invoke(struct psp_context *psp,
1111 struct ta_context *context)
1114 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1116 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1118 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1119 psp->fence_buf_mc_addr);
1121 context->resp_status = cmd->resp.status;
1123 release_psp_cmd_buf(psp);
1128 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1131 struct psp_gfx_cmd_resp *cmd;
1133 cmd = acquire_psp_cmd_buf(psp);
1135 psp_copy_fw(psp, context->bin_desc.start_addr,
1136 context->bin_desc.size_bytes);
1138 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1140 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1141 psp->fence_buf_mc_addr);
1143 context->resp_status = cmd->resp.status;
1146 context->session_id = cmd->resp.session_id;
1149 release_psp_cmd_buf(psp);
1154 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1156 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1159 int psp_xgmi_terminate(struct psp_context *psp)
1162 struct amdgpu_device *adev = psp->adev;
1164 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1165 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1166 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1167 adev->gmc.xgmi.connected_to_cpu))
1170 if (!psp->xgmi_context.context.initialized)
1173 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1175 psp->xgmi_context.context.initialized = false;
1180 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1182 struct ta_xgmi_shared_memory *xgmi_cmd;
1186 !psp->xgmi_context.context.bin_desc.size_bytes ||
1187 !psp->xgmi_context.context.bin_desc.start_addr)
1193 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1194 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1196 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1197 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1203 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1205 psp->xgmi_context.context.initialized = true;
1210 /* Initialize XGMI session */
1211 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1212 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1213 xgmi_cmd->flag_extend_link_record = set_extended_data;
1214 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1216 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1221 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1223 struct ta_xgmi_shared_memory *xgmi_cmd;
1226 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1227 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1229 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1231 /* Invoke xgmi ta to get hive id */
1232 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1236 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1241 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1243 struct ta_xgmi_shared_memory *xgmi_cmd;
1246 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1247 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1249 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1251 /* Invoke xgmi ta to get the node id */
1252 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1256 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1261 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1263 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1264 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1268 * Chips that support extended topology information require the driver to
1269 * reflect topology information in the opposite direction. This is
1270 * because the TA has already exceeded its link record limit and if the
1271 * TA holds bi-directional information, the driver would have to do
1272 * multiple fetches instead of just two.
1274 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1275 struct psp_xgmi_node_info node_info)
1277 struct amdgpu_device *mirror_adev;
1278 struct amdgpu_hive_info *hive;
1279 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1280 uint64_t dst_node_id = node_info.node_id;
1281 uint8_t dst_num_hops = node_info.num_hops;
1282 uint8_t dst_num_links = node_info.num_links;
1284 hive = amdgpu_get_xgmi_hive(psp->adev);
1285 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1286 struct psp_xgmi_topology_info *mirror_top_info;
1289 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1292 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1293 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1294 if (mirror_top_info->nodes[j].node_id != src_node_id)
1297 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1299 * prevent 0 num_links value re-reflection since reflection
1300 * criteria is based on num_hops (direct or indirect).
1304 mirror_top_info->nodes[j].num_links = dst_num_links;
1312 amdgpu_put_xgmi_hive(hive);
1315 int psp_xgmi_get_topology_info(struct psp_context *psp,
1317 struct psp_xgmi_topology_info *topology,
1318 bool get_extended_data)
1320 struct ta_xgmi_shared_memory *xgmi_cmd;
1321 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1322 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1326 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1329 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1330 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1331 xgmi_cmd->flag_extend_link_record = get_extended_data;
1333 /* Fill in the shared memory with topology information as input */
1334 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1335 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1336 topology_info_input->num_nodes = number_devices;
1338 for (i = 0; i < topology_info_input->num_nodes; i++) {
1339 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1340 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1341 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1342 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1345 /* Invoke xgmi ta to get the topology information */
1346 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1350 /* Read the output topology information from the shared memory */
1351 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1352 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1353 for (i = 0; i < topology->num_nodes; i++) {
1354 /* extended data will either be 0 or equal to non-extended data */
1355 if (topology_info_output->nodes[i].num_hops)
1356 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1358 /* non-extended data gets everything here so no need to update */
1359 if (!get_extended_data) {
1360 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1361 topology->nodes[i].is_sharing_enabled =
1362 topology_info_output->nodes[i].is_sharing_enabled;
1363 topology->nodes[i].sdma_engine =
1364 topology_info_output->nodes[i].sdma_engine;
1369 /* Invoke xgmi ta again to get the link information */
1370 if (psp_xgmi_peer_link_info_supported(psp)) {
1371 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1373 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1375 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1380 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1381 for (i = 0; i < topology->num_nodes; i++) {
1382 /* accumulate num_links on extended data */
1383 topology->nodes[i].num_links = get_extended_data ?
1384 topology->nodes[i].num_links +
1385 link_info_output->nodes[i].num_links :
1386 link_info_output->nodes[i].num_links;
1388 /* reflect the topology information for bi-directionality */
1389 if (psp->xgmi_context.supports_extended_data &&
1390 get_extended_data && topology->nodes[i].num_hops)
1391 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1398 int psp_xgmi_set_topology_info(struct psp_context *psp,
1400 struct psp_xgmi_topology_info *topology)
1402 struct ta_xgmi_shared_memory *xgmi_cmd;
1403 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1406 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1409 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1410 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1412 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1413 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1414 topology_info_input->num_nodes = number_devices;
1416 for (i = 0; i < topology_info_input->num_nodes; i++) {
1417 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1418 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1419 topology_info_input->nodes[i].is_sharing_enabled = 1;
1420 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1423 /* Invoke xgmi ta to set topology information */
1424 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1428 static void psp_ras_ta_check_status(struct psp_context *psp)
1430 struct ta_ras_shared_memory *ras_cmd =
1431 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1433 switch (ras_cmd->ras_status) {
1434 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1435 dev_warn(psp->adev->dev,
1436 "RAS WARNING: cmd failed due to unsupported ip\n");
1438 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1439 dev_warn(psp->adev->dev,
1440 "RAS WARNING: cmd failed due to unsupported error injection\n");
1442 case TA_RAS_STATUS__SUCCESS:
1444 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1445 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1446 dev_warn(psp->adev->dev,
1447 "RAS WARNING: Inject error to critical region is not allowed\n");
1450 dev_warn(psp->adev->dev,
1451 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1456 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1458 struct ta_ras_shared_memory *ras_cmd;
1461 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1464 * TODO: bypass the loading in sriov for now
1466 if (amdgpu_sriov_vf(psp->adev))
1469 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1471 if (amdgpu_ras_intr_triggered())
1474 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1476 DRM_WARN("RAS: Unsupported Interface");
1481 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1482 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1484 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1486 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1487 dev_warn(psp->adev->dev,
1488 "RAS internal register access blocked\n");
1490 psp_ras_ta_check_status(psp);
1496 int psp_ras_enable_features(struct psp_context *psp,
1497 union ta_ras_cmd_input *info, bool enable)
1499 struct ta_ras_shared_memory *ras_cmd;
1502 if (!psp->ras_context.context.initialized)
1505 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1506 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1509 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1511 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1513 ras_cmd->ras_in_message = *info;
1515 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1522 int psp_ras_terminate(struct psp_context *psp)
1527 * TODO: bypass the terminate in sriov for now
1529 if (amdgpu_sriov_vf(psp->adev))
1532 if (!psp->ras_context.context.initialized)
1535 ret = psp_ta_unload(psp, &psp->ras_context.context);
1537 psp->ras_context.context.initialized = false;
1542 int psp_ras_initialize(struct psp_context *psp)
1545 uint32_t boot_cfg = 0xFF;
1546 struct amdgpu_device *adev = psp->adev;
1547 struct ta_ras_shared_memory *ras_cmd;
1550 * TODO: bypass the initialize in sriov for now
1552 if (amdgpu_sriov_vf(adev))
1555 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1556 !adev->psp.ras_context.context.bin_desc.start_addr) {
1557 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1561 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1562 /* query GECC enablement status from boot config
1563 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1565 ret = psp_boot_config_get(adev, &boot_cfg);
1567 dev_warn(adev->dev, "PSP get boot config failed\n");
1569 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1571 dev_info(adev->dev, "GECC is disabled\n");
1573 /* disable GECC in next boot cycle if ras is
1574 * disabled by module parameter amdgpu_ras_enable
1575 * and/or amdgpu_ras_mask, or boot_config_get call
1578 ret = psp_boot_config_set(adev, 0);
1580 dev_warn(adev->dev, "PSP set boot config failed\n");
1582 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1583 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1586 if (1 == boot_cfg) {
1587 dev_info(adev->dev, "GECC is enabled\n");
1589 /* enable GECC in next boot cycle if it is disabled
1590 * in boot config, or force enable GECC if failed to
1591 * get boot configuration
1593 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1595 dev_warn(adev->dev, "PSP set boot config failed\n");
1597 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1602 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1603 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1605 if (!psp->ras_context.context.mem_context.shared_buf) {
1606 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1611 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1612 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1614 if (amdgpu_ras_is_poison_mode_supported(adev))
1615 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1616 if (!adev->gmc.xgmi.connected_to_cpu)
1617 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1619 ret = psp_ta_load(psp, &psp->ras_context.context);
1621 if (!ret && !ras_cmd->ras_status)
1622 psp->ras_context.context.initialized = true;
1624 if (ras_cmd->ras_status)
1625 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1627 /* fail to load RAS TA */
1628 psp->ras_context.context.initialized = false;
1634 int psp_ras_trigger_error(struct psp_context *psp,
1635 struct ta_ras_trigger_error_input *info)
1637 struct ta_ras_shared_memory *ras_cmd;
1640 if (!psp->ras_context.context.initialized)
1643 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1644 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1646 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1647 ras_cmd->ras_in_message.trigger_error = *info;
1649 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1653 /* If err_event_athub occurs error inject was successful, however
1654 return status from TA is no long reliable */
1655 if (amdgpu_ras_intr_triggered())
1658 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1660 else if (ras_cmd->ras_status)
1668 static int psp_hdcp_initialize(struct psp_context *psp)
1673 * TODO: bypass the initialize in sriov for now
1675 if (amdgpu_sriov_vf(psp->adev))
1678 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1679 !psp->hdcp_context.context.bin_desc.start_addr) {
1680 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1684 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1685 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1687 if (!psp->hdcp_context.context.initialized) {
1688 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1693 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1695 psp->hdcp_context.context.initialized = true;
1696 mutex_init(&psp->hdcp_context.mutex);
1702 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1705 * TODO: bypass the loading in sriov for now
1707 if (amdgpu_sriov_vf(psp->adev))
1710 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1713 static int psp_hdcp_terminate(struct psp_context *psp)
1718 * TODO: bypass the terminate in sriov for now
1720 if (amdgpu_sriov_vf(psp->adev))
1723 if (!psp->hdcp_context.context.initialized)
1726 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1728 psp->hdcp_context.context.initialized = false;
1735 static int psp_dtm_initialize(struct psp_context *psp)
1740 * TODO: bypass the initialize in sriov for now
1742 if (amdgpu_sriov_vf(psp->adev))
1745 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1746 !psp->dtm_context.context.bin_desc.start_addr) {
1747 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1751 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1752 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1754 if (!psp->dtm_context.context.initialized) {
1755 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1760 ret = psp_ta_load(psp, &psp->dtm_context.context);
1762 psp->dtm_context.context.initialized = true;
1763 mutex_init(&psp->dtm_context.mutex);
1769 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1772 * TODO: bypass the loading in sriov for now
1774 if (amdgpu_sriov_vf(psp->adev))
1777 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1780 static int psp_dtm_terminate(struct psp_context *psp)
1785 * TODO: bypass the terminate in sriov for now
1787 if (amdgpu_sriov_vf(psp->adev))
1790 if (!psp->dtm_context.context.initialized)
1793 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1795 psp->dtm_context.context.initialized = false;
1802 static int psp_rap_initialize(struct psp_context *psp)
1805 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1808 * TODO: bypass the initialize in sriov for now
1810 if (amdgpu_sriov_vf(psp->adev))
1813 if (!psp->rap_context.context.bin_desc.size_bytes ||
1814 !psp->rap_context.context.bin_desc.start_addr) {
1815 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1819 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1820 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1822 if (!psp->rap_context.context.initialized) {
1823 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1828 ret = psp_ta_load(psp, &psp->rap_context.context);
1830 psp->rap_context.context.initialized = true;
1831 mutex_init(&psp->rap_context.mutex);
1835 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1836 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1837 psp_rap_terminate(psp);
1838 /* free rap shared memory */
1839 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1841 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1850 static int psp_rap_terminate(struct psp_context *psp)
1854 if (!psp->rap_context.context.initialized)
1857 ret = psp_ta_unload(psp, &psp->rap_context.context);
1859 psp->rap_context.context.initialized = false;
1864 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1866 struct ta_rap_shared_memory *rap_cmd;
1869 if (!psp->rap_context.context.initialized)
1872 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1873 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1876 mutex_lock(&psp->rap_context.mutex);
1878 rap_cmd = (struct ta_rap_shared_memory *)
1879 psp->rap_context.context.mem_context.shared_buf;
1880 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1882 rap_cmd->cmd_id = ta_cmd_id;
1883 rap_cmd->validation_method_id = METHOD_A;
1885 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1890 *status = rap_cmd->rap_status;
1893 mutex_unlock(&psp->rap_context.mutex);
1899 /* securedisplay start */
1900 static int psp_securedisplay_initialize(struct psp_context *psp)
1903 struct securedisplay_cmd *securedisplay_cmd;
1906 * TODO: bypass the initialize in sriov for now
1908 if (amdgpu_sriov_vf(psp->adev))
1911 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1912 !psp->securedisplay_context.context.bin_desc.start_addr) {
1913 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1917 psp->securedisplay_context.context.mem_context.shared_mem_size =
1918 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1919 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1921 if (!psp->securedisplay_context.context.initialized) {
1922 ret = psp_ta_init_shared_buf(psp,
1923 &psp->securedisplay_context.context.mem_context);
1928 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1930 psp->securedisplay_context.context.initialized = true;
1931 mutex_init(&psp->securedisplay_context.mutex);
1935 mutex_lock(&psp->securedisplay_context.mutex);
1937 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1938 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1940 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1942 mutex_unlock(&psp->securedisplay_context.mutex);
1945 psp_securedisplay_terminate(psp);
1946 /* free securedisplay shared memory */
1947 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1948 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1952 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1953 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1954 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1955 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1961 static int psp_securedisplay_terminate(struct psp_context *psp)
1966 * TODO:bypass the terminate in sriov for now
1968 if (amdgpu_sriov_vf(psp->adev))
1971 if (!psp->securedisplay_context.context.initialized)
1974 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1976 psp->securedisplay_context.context.initialized = false;
1981 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1985 if (!psp->securedisplay_context.context.initialized)
1988 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1989 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1992 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1996 /* SECUREDISPLAY end */
1998 static int psp_hw_start(struct psp_context *psp)
2000 struct amdgpu_device *adev = psp->adev;
2003 if (!amdgpu_sriov_vf(adev)) {
2004 if ((is_psp_fw_valid(psp->kdb)) &&
2005 (psp->funcs->bootloader_load_kdb != NULL)) {
2006 ret = psp_bootloader_load_kdb(psp);
2008 DRM_ERROR("PSP load kdb failed!\n");
2013 if ((is_psp_fw_valid(psp->spl)) &&
2014 (psp->funcs->bootloader_load_spl != NULL)) {
2015 ret = psp_bootloader_load_spl(psp);
2017 DRM_ERROR("PSP load spl failed!\n");
2022 if ((is_psp_fw_valid(psp->sys)) &&
2023 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2024 ret = psp_bootloader_load_sysdrv(psp);
2026 DRM_ERROR("PSP load sys drv failed!\n");
2031 if ((is_psp_fw_valid(psp->soc_drv)) &&
2032 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2033 ret = psp_bootloader_load_soc_drv(psp);
2035 DRM_ERROR("PSP load soc drv failed!\n");
2040 if ((is_psp_fw_valid(psp->intf_drv)) &&
2041 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2042 ret = psp_bootloader_load_intf_drv(psp);
2044 DRM_ERROR("PSP load intf drv failed!\n");
2049 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2050 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2051 ret = psp_bootloader_load_dbg_drv(psp);
2053 DRM_ERROR("PSP load dbg drv failed!\n");
2058 if ((is_psp_fw_valid(psp->ras_drv)) &&
2059 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2060 ret = psp_bootloader_load_ras_drv(psp);
2062 DRM_ERROR("PSP load ras_drv failed!\n");
2067 if ((is_psp_fw_valid(psp->sos)) &&
2068 (psp->funcs->bootloader_load_sos != NULL)) {
2069 ret = psp_bootloader_load_sos(psp);
2071 DRM_ERROR("PSP load sos failed!\n");
2077 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2079 DRM_ERROR("PSP create ring failed!\n");
2083 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2086 ret = psp_tmr_init(psp);
2088 DRM_ERROR("PSP tmr init failed!\n");
2094 * For ASICs with DF Cstate management centralized
2095 * to PMFW, TMR setup should be performed after PMFW
2096 * loaded and before other non-psp firmware loaded.
2098 if (psp->pmfw_centralized_cstate_management) {
2099 ret = psp_load_smu_fw(psp);
2104 ret = psp_tmr_load(psp);
2106 DRM_ERROR("PSP load tmr failed!\n");
2113 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2114 enum psp_gfx_fw_type *type)
2116 switch (ucode->ucode_id) {
2117 case AMDGPU_UCODE_ID_CAP:
2118 *type = GFX_FW_TYPE_CAP;
2120 case AMDGPU_UCODE_ID_SDMA0:
2121 *type = GFX_FW_TYPE_SDMA0;
2123 case AMDGPU_UCODE_ID_SDMA1:
2124 *type = GFX_FW_TYPE_SDMA1;
2126 case AMDGPU_UCODE_ID_SDMA2:
2127 *type = GFX_FW_TYPE_SDMA2;
2129 case AMDGPU_UCODE_ID_SDMA3:
2130 *type = GFX_FW_TYPE_SDMA3;
2132 case AMDGPU_UCODE_ID_SDMA4:
2133 *type = GFX_FW_TYPE_SDMA4;
2135 case AMDGPU_UCODE_ID_SDMA5:
2136 *type = GFX_FW_TYPE_SDMA5;
2138 case AMDGPU_UCODE_ID_SDMA6:
2139 *type = GFX_FW_TYPE_SDMA6;
2141 case AMDGPU_UCODE_ID_SDMA7:
2142 *type = GFX_FW_TYPE_SDMA7;
2144 case AMDGPU_UCODE_ID_CP_MES:
2145 *type = GFX_FW_TYPE_CP_MES;
2147 case AMDGPU_UCODE_ID_CP_MES_DATA:
2148 *type = GFX_FW_TYPE_MES_STACK;
2150 case AMDGPU_UCODE_ID_CP_MES1:
2151 *type = GFX_FW_TYPE_CP_MES_KIQ;
2153 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2154 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2156 case AMDGPU_UCODE_ID_CP_CE:
2157 *type = GFX_FW_TYPE_CP_CE;
2159 case AMDGPU_UCODE_ID_CP_PFP:
2160 *type = GFX_FW_TYPE_CP_PFP;
2162 case AMDGPU_UCODE_ID_CP_ME:
2163 *type = GFX_FW_TYPE_CP_ME;
2165 case AMDGPU_UCODE_ID_CP_MEC1:
2166 *type = GFX_FW_TYPE_CP_MEC;
2168 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2169 *type = GFX_FW_TYPE_CP_MEC_ME1;
2171 case AMDGPU_UCODE_ID_CP_MEC2:
2172 *type = GFX_FW_TYPE_CP_MEC;
2174 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2175 *type = GFX_FW_TYPE_CP_MEC_ME2;
2177 case AMDGPU_UCODE_ID_RLC_P:
2178 *type = GFX_FW_TYPE_RLC_P;
2180 case AMDGPU_UCODE_ID_RLC_V:
2181 *type = GFX_FW_TYPE_RLC_V;
2183 case AMDGPU_UCODE_ID_RLC_G:
2184 *type = GFX_FW_TYPE_RLC_G;
2186 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2187 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2189 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2190 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2192 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2193 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2195 case AMDGPU_UCODE_ID_RLC_IRAM:
2196 *type = GFX_FW_TYPE_RLC_IRAM;
2198 case AMDGPU_UCODE_ID_RLC_DRAM:
2199 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2201 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2202 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2204 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2205 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2207 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2208 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2210 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2211 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2213 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2214 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2216 case AMDGPU_UCODE_ID_SMC:
2217 *type = GFX_FW_TYPE_SMU;
2219 case AMDGPU_UCODE_ID_PPTABLE:
2220 *type = GFX_FW_TYPE_PPTABLE;
2222 case AMDGPU_UCODE_ID_UVD:
2223 *type = GFX_FW_TYPE_UVD;
2225 case AMDGPU_UCODE_ID_UVD1:
2226 *type = GFX_FW_TYPE_UVD1;
2228 case AMDGPU_UCODE_ID_VCE:
2229 *type = GFX_FW_TYPE_VCE;
2231 case AMDGPU_UCODE_ID_VCN:
2232 *type = GFX_FW_TYPE_VCN;
2234 case AMDGPU_UCODE_ID_VCN1:
2235 *type = GFX_FW_TYPE_VCN1;
2237 case AMDGPU_UCODE_ID_DMCU_ERAM:
2238 *type = GFX_FW_TYPE_DMCU_ERAM;
2240 case AMDGPU_UCODE_ID_DMCU_INTV:
2241 *type = GFX_FW_TYPE_DMCU_ISR;
2243 case AMDGPU_UCODE_ID_VCN0_RAM:
2244 *type = GFX_FW_TYPE_VCN0_RAM;
2246 case AMDGPU_UCODE_ID_VCN1_RAM:
2247 *type = GFX_FW_TYPE_VCN1_RAM;
2249 case AMDGPU_UCODE_ID_DMCUB:
2250 *type = GFX_FW_TYPE_DMUB;
2252 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2253 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2255 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2256 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2258 case AMDGPU_UCODE_ID_IMU_I:
2259 *type = GFX_FW_TYPE_IMU_I;
2261 case AMDGPU_UCODE_ID_IMU_D:
2262 *type = GFX_FW_TYPE_IMU_D;
2264 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2265 *type = GFX_FW_TYPE_RS64_PFP;
2267 case AMDGPU_UCODE_ID_CP_RS64_ME:
2268 *type = GFX_FW_TYPE_RS64_ME;
2270 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2271 *type = GFX_FW_TYPE_RS64_MEC;
2273 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2274 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2276 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2277 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2279 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2280 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2282 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2283 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2285 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2286 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2288 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2289 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2291 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2292 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2294 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2295 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2297 case AMDGPU_UCODE_ID_MAXIMUM:
2305 static void psp_print_fw_hdr(struct psp_context *psp,
2306 struct amdgpu_firmware_info *ucode)
2308 struct amdgpu_device *adev = psp->adev;
2309 struct common_firmware_header *hdr;
2311 switch (ucode->ucode_id) {
2312 case AMDGPU_UCODE_ID_SDMA0:
2313 case AMDGPU_UCODE_ID_SDMA1:
2314 case AMDGPU_UCODE_ID_SDMA2:
2315 case AMDGPU_UCODE_ID_SDMA3:
2316 case AMDGPU_UCODE_ID_SDMA4:
2317 case AMDGPU_UCODE_ID_SDMA5:
2318 case AMDGPU_UCODE_ID_SDMA6:
2319 case AMDGPU_UCODE_ID_SDMA7:
2320 hdr = (struct common_firmware_header *)
2321 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2322 amdgpu_ucode_print_sdma_hdr(hdr);
2324 case AMDGPU_UCODE_ID_CP_CE:
2325 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2326 amdgpu_ucode_print_gfx_hdr(hdr);
2328 case AMDGPU_UCODE_ID_CP_PFP:
2329 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2330 amdgpu_ucode_print_gfx_hdr(hdr);
2332 case AMDGPU_UCODE_ID_CP_ME:
2333 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2334 amdgpu_ucode_print_gfx_hdr(hdr);
2336 case AMDGPU_UCODE_ID_CP_MEC1:
2337 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2338 amdgpu_ucode_print_gfx_hdr(hdr);
2340 case AMDGPU_UCODE_ID_RLC_G:
2341 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2342 amdgpu_ucode_print_rlc_hdr(hdr);
2344 case AMDGPU_UCODE_ID_SMC:
2345 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2346 amdgpu_ucode_print_smc_hdr(hdr);
2353 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2354 struct psp_gfx_cmd_resp *cmd)
2357 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2359 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2360 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2361 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2362 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2364 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2366 DRM_ERROR("Unknown firmware type\n");
2371 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2372 struct amdgpu_firmware_info *ucode)
2375 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2377 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2379 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2380 psp->fence_buf_mc_addr);
2383 release_psp_cmd_buf(psp);
2388 static int psp_load_smu_fw(struct psp_context *psp)
2391 struct amdgpu_device *adev = psp->adev;
2392 struct amdgpu_firmware_info *ucode =
2393 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2394 struct amdgpu_ras *ras = psp->ras_context.ras;
2397 * Skip SMU FW reloading in case of using BACO for runpm only,
2398 * as SMU is always alive.
2400 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2403 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2406 if ((amdgpu_in_reset(adev) &&
2407 ras && adev->ras_enabled &&
2408 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2409 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2410 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2412 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2416 ret = psp_execute_non_psp_fw_load(psp, ucode);
2419 DRM_ERROR("PSP load smu failed!\n");
2424 static bool fw_load_skip_check(struct psp_context *psp,
2425 struct amdgpu_firmware_info *ucode)
2427 if (!ucode->fw || !ucode->ucode_size)
2430 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2431 (psp_smu_reload_quirk(psp) ||
2432 psp->autoload_supported ||
2433 psp->pmfw_centralized_cstate_management))
2436 if (amdgpu_sriov_vf(psp->adev) &&
2437 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2440 if (psp->autoload_supported &&
2441 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2442 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2443 /* skip mec JT when autoload is enabled */
2449 int psp_load_fw_list(struct psp_context *psp,
2450 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2453 struct amdgpu_firmware_info *ucode;
2455 for (i = 0; i < ucode_count; ++i) {
2456 ucode = ucode_list[i];
2457 psp_print_fw_hdr(psp, ucode);
2458 ret = psp_execute_non_psp_fw_load(psp, ucode);
2465 static int psp_load_non_psp_fw(struct psp_context *psp)
2468 struct amdgpu_firmware_info *ucode;
2469 struct amdgpu_device *adev = psp->adev;
2471 if (psp->autoload_supported &&
2472 !psp->pmfw_centralized_cstate_management) {
2473 ret = psp_load_smu_fw(psp);
2478 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2479 ucode = &adev->firmware.ucode[i];
2481 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2482 !fw_load_skip_check(psp, ucode)) {
2483 ret = psp_load_smu_fw(psp);
2489 if (fw_load_skip_check(psp, ucode))
2492 if (psp->autoload_supported &&
2493 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2494 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2495 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2496 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2497 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2498 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2499 /* PSP only receive one SDMA fw for sienna_cichlid,
2500 * as all four sdma fw are same */
2503 psp_print_fw_hdr(psp, ucode);
2505 ret = psp_execute_non_psp_fw_load(psp, ucode);
2509 /* Start rlc autoload after psp recieved all the gfx firmware */
2510 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2511 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2512 ret = psp_rlc_autoload_start(psp);
2514 DRM_ERROR("Failed to start rlc autoload\n");
2523 static int psp_load_fw(struct amdgpu_device *adev)
2526 struct psp_context *psp = &adev->psp;
2528 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2529 /* should not destroy ring, only stop */
2530 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2532 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2534 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2536 DRM_ERROR("PSP ring init failed!\n");
2541 ret = psp_hw_start(psp);
2545 ret = psp_load_non_psp_fw(psp);
2549 ret = psp_asd_initialize(psp);
2551 DRM_ERROR("PSP load asd failed!\n");
2555 ret = psp_rl_load(adev);
2557 DRM_ERROR("PSP load RL failed!\n");
2561 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2562 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2563 ret = psp_xgmi_initialize(psp, false, true);
2564 /* Warning the XGMI seesion initialize failure
2565 * Instead of stop driver initialization
2568 dev_err(psp->adev->dev,
2569 "XGMI: Failed to initialize XGMI session\n");
2574 ret = psp_ras_initialize(psp);
2576 dev_err(psp->adev->dev,
2577 "RAS: Failed to initialize RAS\n");
2579 ret = psp_hdcp_initialize(psp);
2581 dev_err(psp->adev->dev,
2582 "HDCP: Failed to initialize HDCP\n");
2584 ret = psp_dtm_initialize(psp);
2586 dev_err(psp->adev->dev,
2587 "DTM: Failed to initialize DTM\n");
2589 ret = psp_rap_initialize(psp);
2591 dev_err(psp->adev->dev,
2592 "RAP: Failed to initialize RAP\n");
2594 ret = psp_securedisplay_initialize(psp);
2596 dev_err(psp->adev->dev,
2597 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2603 psp_free_shared_bufs(psp);
2606 * all cleanup jobs (xgmi terminate, ras terminate,
2607 * ring destroy, cmd/fence/fw buffers destory,
2608 * psp->cmd destory) are delayed to psp_hw_fini
2610 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2614 static int psp_hw_init(void *handle)
2617 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2619 mutex_lock(&adev->firmware.mutex);
2621 * This sequence is just used on hw_init only once, no need on
2624 ret = amdgpu_ucode_init_bo(adev);
2628 ret = psp_load_fw(adev);
2630 DRM_ERROR("PSP firmware loading failed\n");
2634 mutex_unlock(&adev->firmware.mutex);
2638 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2639 mutex_unlock(&adev->firmware.mutex);
2643 static int psp_hw_fini(void *handle)
2645 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2646 struct psp_context *psp = &adev->psp;
2649 psp_ras_terminate(psp);
2650 psp_securedisplay_terminate(psp);
2651 psp_rap_terminate(psp);
2652 psp_dtm_terminate(psp);
2653 psp_hdcp_terminate(psp);
2655 if (adev->gmc.xgmi.num_physical_nodes > 1)
2656 psp_xgmi_terminate(psp);
2659 psp_asd_terminate(psp);
2660 psp_tmr_terminate(psp);
2662 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2664 psp_free_shared_bufs(psp);
2669 static int psp_suspend(void *handle)
2672 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2673 struct psp_context *psp = &adev->psp;
2675 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2676 psp->xgmi_context.context.initialized) {
2677 ret = psp_xgmi_terminate(psp);
2679 DRM_ERROR("Failed to terminate xgmi ta\n");
2685 ret = psp_ras_terminate(psp);
2687 DRM_ERROR("Failed to terminate ras ta\n");
2690 ret = psp_hdcp_terminate(psp);
2692 DRM_ERROR("Failed to terminate hdcp ta\n");
2695 ret = psp_dtm_terminate(psp);
2697 DRM_ERROR("Failed to terminate dtm ta\n");
2700 ret = psp_rap_terminate(psp);
2702 DRM_ERROR("Failed to terminate rap ta\n");
2705 ret = psp_securedisplay_terminate(psp);
2707 DRM_ERROR("Failed to terminate securedisplay ta\n");
2712 ret = psp_asd_terminate(psp);
2714 DRM_ERROR("Failed to terminate asd\n");
2718 ret = psp_tmr_terminate(psp);
2720 DRM_ERROR("Failed to terminate tmr\n");
2724 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2726 DRM_ERROR("PSP ring stop failed\n");
2730 psp_free_shared_bufs(psp);
2735 static int psp_resume(void *handle)
2738 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2739 struct psp_context *psp = &adev->psp;
2741 DRM_INFO("PSP is resuming...\n");
2743 if (psp->mem_train_ctx.enable_mem_training) {
2744 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2746 DRM_ERROR("Failed to process memory training!\n");
2751 mutex_lock(&adev->firmware.mutex);
2753 ret = psp_hw_start(psp);
2757 ret = psp_load_non_psp_fw(psp);
2761 ret = psp_asd_initialize(psp);
2763 DRM_ERROR("PSP load asd failed!\n");
2767 ret = psp_rl_load(adev);
2769 dev_err(adev->dev, "PSP load RL failed!\n");
2773 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2774 ret = psp_xgmi_initialize(psp, false, true);
2775 /* Warning the XGMI seesion initialize failure
2776 * Instead of stop driver initialization
2779 dev_err(psp->adev->dev,
2780 "XGMI: Failed to initialize XGMI session\n");
2784 ret = psp_ras_initialize(psp);
2786 dev_err(psp->adev->dev,
2787 "RAS: Failed to initialize RAS\n");
2789 ret = psp_hdcp_initialize(psp);
2791 dev_err(psp->adev->dev,
2792 "HDCP: Failed to initialize HDCP\n");
2794 ret = psp_dtm_initialize(psp);
2796 dev_err(psp->adev->dev,
2797 "DTM: Failed to initialize DTM\n");
2799 ret = psp_rap_initialize(psp);
2801 dev_err(psp->adev->dev,
2802 "RAP: Failed to initialize RAP\n");
2804 ret = psp_securedisplay_initialize(psp);
2806 dev_err(psp->adev->dev,
2807 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2810 mutex_unlock(&adev->firmware.mutex);
2815 DRM_ERROR("PSP resume failed\n");
2816 mutex_unlock(&adev->firmware.mutex);
2820 int psp_gpu_reset(struct amdgpu_device *adev)
2824 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2827 mutex_lock(&adev->psp.mutex);
2828 ret = psp_mode1_reset(&adev->psp);
2829 mutex_unlock(&adev->psp.mutex);
2834 int psp_rlc_autoload_start(struct psp_context *psp)
2837 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2839 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2841 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2842 psp->fence_buf_mc_addr);
2844 release_psp_cmd_buf(psp);
2849 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2850 uint64_t cmd_gpu_addr, int cmd_size)
2852 struct amdgpu_firmware_info ucode = {0};
2854 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2855 AMDGPU_UCODE_ID_VCN0_RAM;
2856 ucode.mc_addr = cmd_gpu_addr;
2857 ucode.ucode_size = cmd_size;
2859 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2862 int psp_ring_cmd_submit(struct psp_context *psp,
2863 uint64_t cmd_buf_mc_addr,
2864 uint64_t fence_mc_addr,
2867 unsigned int psp_write_ptr_reg = 0;
2868 struct psp_gfx_rb_frame *write_frame;
2869 struct psp_ring *ring = &psp->km_ring;
2870 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2871 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2872 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2873 struct amdgpu_device *adev = psp->adev;
2874 uint32_t ring_size_dw = ring->ring_size / 4;
2875 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2877 /* KM (GPCOM) prepare write pointer */
2878 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2880 /* Update KM RB frame pointer to new frame */
2881 /* write_frame ptr increments by size of rb_frame in bytes */
2882 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2883 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2884 write_frame = ring_buffer_start;
2886 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2887 /* Check invalid write_frame ptr address */
2888 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2889 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2890 ring_buffer_start, ring_buffer_end, write_frame);
2891 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2895 /* Initialize KM RB frame */
2896 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2898 /* Update KM RB frame */
2899 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2900 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2901 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2902 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2903 write_frame->fence_value = index;
2904 amdgpu_device_flush_hdp(adev, NULL);
2906 /* Update the write Pointer in DWORDs */
2907 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2908 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2912 int psp_init_asd_microcode(struct psp_context *psp,
2913 const char *chip_name)
2915 struct amdgpu_device *adev = psp->adev;
2916 char fw_name[PSP_FW_NAME_LEN];
2917 const struct psp_firmware_header_v1_0 *asd_hdr;
2921 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2925 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2926 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2930 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2934 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2935 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2936 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2937 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2938 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2939 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2942 dev_err(adev->dev, "fail to initialize asd microcode\n");
2943 release_firmware(adev->psp.asd_fw);
2944 adev->psp.asd_fw = NULL;
2948 int psp_init_toc_microcode(struct psp_context *psp,
2949 const char *chip_name)
2951 struct amdgpu_device *adev = psp->adev;
2952 char fw_name[PSP_FW_NAME_LEN];
2953 const struct psp_firmware_header_v1_0 *toc_hdr;
2957 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2961 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2962 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2966 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2970 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2971 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2972 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2973 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2974 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2975 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2978 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2979 release_firmware(adev->psp.toc_fw);
2980 adev->psp.toc_fw = NULL;
2984 static int parse_sos_bin_descriptor(struct psp_context *psp,
2985 const struct psp_fw_bin_desc *desc,
2986 const struct psp_firmware_header_v2_0 *sos_hdr)
2988 uint8_t *ucode_start_addr = NULL;
2990 if (!psp || !desc || !sos_hdr)
2993 ucode_start_addr = (uint8_t *)sos_hdr +
2994 le32_to_cpu(desc->offset_bytes) +
2995 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2997 switch (desc->fw_type) {
2998 case PSP_FW_TYPE_PSP_SOS:
2999 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3000 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3001 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3002 psp->sos.start_addr = ucode_start_addr;
3004 case PSP_FW_TYPE_PSP_SYS_DRV:
3005 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3006 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3007 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3008 psp->sys.start_addr = ucode_start_addr;
3010 case PSP_FW_TYPE_PSP_KDB:
3011 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3012 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3013 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3014 psp->kdb.start_addr = ucode_start_addr;
3016 case PSP_FW_TYPE_PSP_TOC:
3017 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3018 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3019 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3020 psp->toc.start_addr = ucode_start_addr;
3022 case PSP_FW_TYPE_PSP_SPL:
3023 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3024 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3025 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3026 psp->spl.start_addr = ucode_start_addr;
3028 case PSP_FW_TYPE_PSP_RL:
3029 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3030 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3031 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3032 psp->rl.start_addr = ucode_start_addr;
3034 case PSP_FW_TYPE_PSP_SOC_DRV:
3035 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3036 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3037 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3038 psp->soc_drv.start_addr = ucode_start_addr;
3040 case PSP_FW_TYPE_PSP_INTF_DRV:
3041 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3042 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3043 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3044 psp->intf_drv.start_addr = ucode_start_addr;
3046 case PSP_FW_TYPE_PSP_DBG_DRV:
3047 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3048 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3049 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3050 psp->dbg_drv.start_addr = ucode_start_addr;
3052 case PSP_FW_TYPE_PSP_RAS_DRV:
3053 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3054 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3055 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3056 psp->ras_drv.start_addr = ucode_start_addr;
3059 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3066 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3068 const struct psp_firmware_header_v1_0 *sos_hdr;
3069 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3070 uint8_t *ucode_array_start_addr;
3072 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3073 ucode_array_start_addr = (uint8_t *)sos_hdr +
3074 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3076 if (adev->gmc.xgmi.connected_to_cpu ||
3077 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3078 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3079 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3081 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3082 adev->psp.sys.start_addr = ucode_array_start_addr;
3084 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3085 adev->psp.sos.start_addr = ucode_array_start_addr +
3086 le32_to_cpu(sos_hdr->sos.offset_bytes);
3088 /* Load alternate PSP SOS FW */
3089 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3091 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3092 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3094 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3095 adev->psp.sys.start_addr = ucode_array_start_addr +
3096 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3098 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3099 adev->psp.sos.start_addr = ucode_array_start_addr +
3100 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3103 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3104 dev_warn(adev->dev, "PSP SOS FW not available");
3111 int psp_init_sos_microcode(struct psp_context *psp,
3112 const char *chip_name)
3114 struct amdgpu_device *adev = psp->adev;
3115 char fw_name[PSP_FW_NAME_LEN];
3116 const struct psp_firmware_header_v1_0 *sos_hdr;
3117 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3118 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3119 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3120 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3122 uint8_t *ucode_array_start_addr;
3126 dev_err(adev->dev, "invalid chip name for sos microcode\n");
3130 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3131 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
3135 err = amdgpu_ucode_validate(adev->psp.sos_fw);
3139 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3140 ucode_array_start_addr = (uint8_t *)sos_hdr +
3141 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3142 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3144 switch (sos_hdr->header.header_version_major) {
3146 err = psp_init_sos_base_fw(adev);
3150 if (sos_hdr->header.header_version_minor == 1) {
3151 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3152 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3153 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3154 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3155 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3156 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3157 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3159 if (sos_hdr->header.header_version_minor == 2) {
3160 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3161 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3162 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3163 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3165 if (sos_hdr->header.header_version_minor == 3) {
3166 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3167 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3168 adev->psp.toc.start_addr = ucode_array_start_addr +
3169 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3170 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3171 adev->psp.kdb.start_addr = ucode_array_start_addr +
3172 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3173 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3174 adev->psp.spl.start_addr = ucode_array_start_addr +
3175 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3176 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3177 adev->psp.rl.start_addr = ucode_array_start_addr +
3178 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3182 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3184 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3185 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3190 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3191 err = parse_sos_bin_descriptor(psp,
3192 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3200 "unsupported psp sos firmware\n");
3208 "failed to init sos firmware\n");
3209 release_firmware(adev->psp.sos_fw);
3210 adev->psp.sos_fw = NULL;
3215 static int parse_ta_bin_descriptor(struct psp_context *psp,
3216 const struct psp_fw_bin_desc *desc,
3217 const struct ta_firmware_header_v2_0 *ta_hdr)
3219 uint8_t *ucode_start_addr = NULL;
3221 if (!psp || !desc || !ta_hdr)
3224 ucode_start_addr = (uint8_t *)ta_hdr +
3225 le32_to_cpu(desc->offset_bytes) +
3226 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3228 switch (desc->fw_type) {
3229 case TA_FW_TYPE_PSP_ASD:
3230 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3231 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3232 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3233 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3235 case TA_FW_TYPE_PSP_XGMI:
3236 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3237 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3238 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3240 case TA_FW_TYPE_PSP_RAS:
3241 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3242 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3243 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3245 case TA_FW_TYPE_PSP_HDCP:
3246 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3247 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3248 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3250 case TA_FW_TYPE_PSP_DTM:
3251 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3252 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3253 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3255 case TA_FW_TYPE_PSP_RAP:
3256 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3257 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3258 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3260 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3261 psp->securedisplay_context.context.bin_desc.fw_version =
3262 le32_to_cpu(desc->fw_version);
3263 psp->securedisplay_context.context.bin_desc.size_bytes =
3264 le32_to_cpu(desc->size_bytes);
3265 psp->securedisplay_context.context.bin_desc.start_addr =
3269 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3276 int psp_init_ta_microcode(struct psp_context *psp,
3277 const char *chip_name)
3279 struct amdgpu_device *adev = psp->adev;
3280 char fw_name[PSP_FW_NAME_LEN];
3281 const struct ta_firmware_header_v2_0 *ta_hdr;
3286 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3290 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3291 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3295 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3299 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3301 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3302 dev_err(adev->dev, "unsupported TA header version\n");
3307 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3308 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3313 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3314 err = parse_ta_bin_descriptor(psp,
3315 &ta_hdr->ta_fw_bin[ta_index],
3323 dev_err(adev->dev, "fail to initialize ta microcode\n");
3324 release_firmware(adev->psp.ta_fw);
3325 adev->psp.ta_fw = NULL;
3329 int psp_init_cap_microcode(struct psp_context *psp,
3330 const char *chip_name)
3332 struct amdgpu_device *adev = psp->adev;
3333 char fw_name[PSP_FW_NAME_LEN];
3334 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3335 struct amdgpu_firmware_info *info = NULL;
3339 dev_err(adev->dev, "invalid chip name for cap microcode\n");
3343 if (!amdgpu_sriov_vf(adev)) {
3344 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3348 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3349 err = request_firmware(&adev->psp.cap_fw, fw_name, adev->dev);
3351 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3356 err = amdgpu_ucode_validate(adev->psp.cap_fw);
3358 dev_err(adev->dev, "fail to initialize cap microcode\n");
3362 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3363 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3364 info->fw = adev->psp.cap_fw;
3365 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3366 adev->psp.cap_fw->data;
3367 adev->firmware.fw_size += ALIGN(
3368 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3369 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3370 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3371 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3376 release_firmware(adev->psp.cap_fw);
3377 adev->psp.cap_fw = NULL;
3381 static int psp_set_clockgating_state(void *handle,
3382 enum amd_clockgating_state state)
3387 static int psp_set_powergating_state(void *handle,
3388 enum amd_powergating_state state)
3393 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3394 struct device_attribute *attr,
3397 struct drm_device *ddev = dev_get_drvdata(dev);
3398 struct amdgpu_device *adev = drm_to_adev(ddev);
3402 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3403 DRM_INFO("PSP block is not ready yet.");
3407 mutex_lock(&adev->psp.mutex);
3408 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3409 mutex_unlock(&adev->psp.mutex);
3412 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3416 return sysfs_emit(buf, "%x\n", fw_ver);
3419 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3420 struct device_attribute *attr,
3424 struct drm_device *ddev = dev_get_drvdata(dev);
3425 struct amdgpu_device *adev = drm_to_adev(ddev);
3428 const struct firmware *usbc_pd_fw;
3429 struct amdgpu_bo *fw_buf_bo = NULL;
3430 uint64_t fw_pri_mc_addr;
3431 void *fw_pri_cpu_addr;
3433 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3434 DRM_INFO("PSP block is not ready yet.");
3438 if (!drm_dev_enter(ddev, &idx))
3441 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3442 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3446 /* LFB address which is aligned to 1MB boundary per PSP request */
3447 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3448 AMDGPU_GEM_DOMAIN_VRAM,
3455 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3457 mutex_lock(&adev->psp.mutex);
3458 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3459 mutex_unlock(&adev->psp.mutex);
3461 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3464 release_firmware(usbc_pd_fw);
3467 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3475 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3479 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3482 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3483 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3488 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3489 psp_usbc_pd_fw_sysfs_read,
3490 psp_usbc_pd_fw_sysfs_write);
3492 int is_psp_fw_valid(struct psp_bin_desc bin)
3494 return bin.size_bytes;
3497 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3498 struct bin_attribute *bin_attr,
3499 char *buffer, loff_t pos, size_t count)
3501 struct device *dev = kobj_to_dev(kobj);
3502 struct drm_device *ddev = dev_get_drvdata(dev);
3503 struct amdgpu_device *adev = drm_to_adev(ddev);
3505 adev->psp.vbflash_done = false;
3507 /* Safeguard against memory drain */
3508 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3509 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3510 kvfree(adev->psp.vbflash_tmp_buf);
3511 adev->psp.vbflash_tmp_buf = NULL;
3512 adev->psp.vbflash_image_size = 0;
3516 /* TODO Just allocate max for now and optimize to realloc later if needed */
3517 if (!adev->psp.vbflash_tmp_buf) {
3518 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3519 if (!adev->psp.vbflash_tmp_buf)
3523 mutex_lock(&adev->psp.mutex);
3524 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3525 adev->psp.vbflash_image_size += count;
3526 mutex_unlock(&adev->psp.mutex);
3528 dev_info(adev->dev, "VBIOS flash write PSP done");
3533 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3534 struct bin_attribute *bin_attr, char *buffer,
3535 loff_t pos, size_t count)
3537 struct device *dev = kobj_to_dev(kobj);
3538 struct drm_device *ddev = dev_get_drvdata(dev);
3539 struct amdgpu_device *adev = drm_to_adev(ddev);
3540 struct amdgpu_bo *fw_buf_bo = NULL;
3541 uint64_t fw_pri_mc_addr;
3542 void *fw_pri_cpu_addr;
3545 dev_info(adev->dev, "VBIOS flash to PSP started");
3547 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3548 AMDGPU_GPU_PAGE_SIZE,
3549 AMDGPU_GEM_DOMAIN_VRAM,
3556 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3558 mutex_lock(&adev->psp.mutex);
3559 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3560 mutex_unlock(&adev->psp.mutex);
3562 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3565 kvfree(adev->psp.vbflash_tmp_buf);
3566 adev->psp.vbflash_tmp_buf = NULL;
3567 adev->psp.vbflash_image_size = 0;
3570 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3574 dev_info(adev->dev, "VBIOS flash to PSP done");
3578 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3579 struct device_attribute *attr,
3582 struct drm_device *ddev = dev_get_drvdata(dev);
3583 struct amdgpu_device *adev = drm_to_adev(ddev);
3584 uint32_t vbflash_status;
3586 vbflash_status = psp_vbflash_status(&adev->psp);
3587 if (!adev->psp.vbflash_done)
3589 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3592 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3595 static const struct bin_attribute psp_vbflash_bin_attr = {
3596 .attr = {.name = "psp_vbflash", .mode = 0664},
3598 .write = amdgpu_psp_vbflash_write,
3599 .read = amdgpu_psp_vbflash_read,
3602 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3604 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3607 struct psp_context *psp = &adev->psp;
3609 if (amdgpu_sriov_vf(adev))
3612 switch (adev->ip_versions[MP0_HWIP][0]) {
3613 case IP_VERSION(13, 0, 0):
3614 case IP_VERSION(13, 0, 7):
3617 psp_v13_0_set_psp_funcs(psp);
3619 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3621 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3622 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3624 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3631 const struct amd_ip_funcs psp_ip_funcs = {
3633 .early_init = psp_early_init,
3635 .sw_init = psp_sw_init,
3636 .sw_fini = psp_sw_fini,
3637 .hw_init = psp_hw_init,
3638 .hw_fini = psp_hw_fini,
3639 .suspend = psp_suspend,
3640 .resume = psp_resume,
3642 .check_soft_reset = NULL,
3643 .wait_for_idle = NULL,
3645 .set_clockgating_state = psp_set_clockgating_state,
3646 .set_powergating_state = psp_set_powergating_state,
3649 static int psp_sysfs_init(struct amdgpu_device *adev)
3651 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3654 DRM_ERROR("Failed to create USBC PD FW control file!");
3659 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3661 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3662 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3665 static void psp_sysfs_fini(struct amdgpu_device *adev)
3667 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3670 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3672 .type = AMD_IP_BLOCK_TYPE_PSP,
3676 .funcs = &psp_ip_funcs,
3679 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3681 .type = AMD_IP_BLOCK_TYPE_PSP,
3685 .funcs = &psp_ip_funcs,
3688 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3690 .type = AMD_IP_BLOCK_TYPE_PSP,
3694 .funcs = &psp_ip_funcs,
3697 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3698 .type = AMD_IP_BLOCK_TYPE_PSP,
3702 .funcs = &psp_ip_funcs,
3705 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3707 .type = AMD_IP_BLOCK_TYPE_PSP,
3711 .funcs = &psp_ip_funcs,
3714 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3715 .type = AMD_IP_BLOCK_TYPE_PSP,
3719 .funcs = &psp_ip_funcs,
3722 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3723 .type = AMD_IP_BLOCK_TYPE_PSP,
3727 .funcs = &psp_ip_funcs,