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 |
70 AMDGPU_GEM_DOMAIN_GTT,
72 &ring->ring_mem_mc_addr,
73 (void **)&ring->ring_mem);
83 * Due to DF Cstate management centralized to PMFW, the firmware
84 * loading sequence will be updated as below:
90 * - Load other non-psp fw
92 * - Load XGMI/RAS/HDCP/DTM TA if any
94 * This new sequence is required for
95 * - Arcturus and onwards
97 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
99 struct amdgpu_device *adev = psp->adev;
101 if (amdgpu_sriov_vf(adev)) {
102 psp->pmfw_centralized_cstate_management = false;
106 switch (adev->ip_versions[MP0_HWIP][0]) {
107 case IP_VERSION(11, 0, 0):
108 case IP_VERSION(11, 0, 4):
109 case IP_VERSION(11, 0, 5):
110 case IP_VERSION(11, 0, 7):
111 case IP_VERSION(11, 0, 9):
112 case IP_VERSION(11, 0, 11):
113 case IP_VERSION(11, 0, 12):
114 case IP_VERSION(11, 0, 13):
115 case IP_VERSION(13, 0, 0):
116 case IP_VERSION(13, 0, 2):
117 case IP_VERSION(13, 0, 7):
118 psp->pmfw_centralized_cstate_management = true;
121 psp->pmfw_centralized_cstate_management = false;
126 static int psp_early_init(void *handle)
128 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
129 struct psp_context *psp = &adev->psp;
131 switch (adev->ip_versions[MP0_HWIP][0]) {
132 case IP_VERSION(9, 0, 0):
133 psp_v3_1_set_psp_funcs(psp);
134 psp->autoload_supported = false;
136 case IP_VERSION(10, 0, 0):
137 case IP_VERSION(10, 0, 1):
138 psp_v10_0_set_psp_funcs(psp);
139 psp->autoload_supported = false;
141 case IP_VERSION(11, 0, 2):
142 case IP_VERSION(11, 0, 4):
143 psp_v11_0_set_psp_funcs(psp);
144 psp->autoload_supported = false;
146 case IP_VERSION(11, 0, 0):
147 case IP_VERSION(11, 0, 5):
148 case IP_VERSION(11, 0, 9):
149 case IP_VERSION(11, 0, 7):
150 case IP_VERSION(11, 0, 11):
151 case IP_VERSION(11, 5, 0):
152 case IP_VERSION(11, 0, 12):
153 case IP_VERSION(11, 0, 13):
154 psp_v11_0_set_psp_funcs(psp);
155 psp->autoload_supported = true;
157 case IP_VERSION(11, 0, 3):
158 case IP_VERSION(12, 0, 1):
159 psp_v12_0_set_psp_funcs(psp);
161 case IP_VERSION(13, 0, 2):
162 psp_v13_0_set_psp_funcs(psp);
164 case IP_VERSION(13, 0, 1):
165 case IP_VERSION(13, 0, 3):
166 case IP_VERSION(13, 0, 5):
167 case IP_VERSION(13, 0, 8):
168 case IP_VERSION(13, 0, 10):
169 case IP_VERSION(13, 0, 11):
170 psp_v13_0_set_psp_funcs(psp);
171 psp->autoload_supported = true;
173 case IP_VERSION(11, 0, 8):
174 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
175 psp_v11_0_8_set_psp_funcs(psp);
176 psp->autoload_supported = false;
179 case IP_VERSION(13, 0, 0):
180 case IP_VERSION(13, 0, 7):
181 psp_v13_0_set_psp_funcs(psp);
182 psp->autoload_supported = true;
184 case IP_VERSION(13, 0, 4):
185 psp_v13_0_4_set_psp_funcs(psp);
186 psp->autoload_supported = true;
194 psp_check_pmfw_centralized_cstate_management(psp);
199 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
201 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
202 &mem_ctx->shared_buf);
203 mem_ctx->shared_bo = NULL;
206 static void psp_free_shared_bufs(struct psp_context *psp)
211 /* free TMR memory buffer */
212 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
213 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
216 /* free xgmi shared memory */
217 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
219 /* free ras shared memory */
220 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
222 /* free hdcp shared memory */
223 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
225 /* free dtm shared memory */
226 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
228 /* free rap shared memory */
229 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
231 /* free securedisplay shared memory */
232 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
237 static void psp_memory_training_fini(struct psp_context *psp)
239 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
241 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
242 kfree(ctx->sys_cache);
243 ctx->sys_cache = NULL;
246 static int psp_memory_training_init(struct psp_context *psp)
249 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
251 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
252 DRM_DEBUG("memory training is not supported!\n");
256 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
257 if (ctx->sys_cache == NULL) {
258 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
263 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
264 ctx->train_data_size,
265 ctx->p2c_train_data_offset,
266 ctx->c2p_train_data_offset);
267 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
271 psp_memory_training_fini(psp);
276 * Helper funciton to query psp runtime database entry
278 * @adev: amdgpu_device pointer
279 * @entry_type: the type of psp runtime database entry
280 * @db_entry: runtime database entry pointer
282 * Return false if runtime database doesn't exit or entry is invalid
283 * or true if the specific database entry is found, and copy to @db_entry
285 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
286 enum psp_runtime_entry_type entry_type,
289 uint64_t db_header_pos, db_dir_pos;
290 struct psp_runtime_data_header db_header = {0};
291 struct psp_runtime_data_directory db_dir = {0};
295 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
296 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
298 /* read runtime db header from vram */
299 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
300 sizeof(struct psp_runtime_data_header), false);
302 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
303 /* runtime db doesn't exist, exit */
304 dev_warn(adev->dev, "PSP runtime database doesn't exist\n");
308 /* read runtime database entry from vram */
309 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
310 sizeof(struct psp_runtime_data_directory), false);
312 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
313 /* invalid db entry count, exit */
314 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
318 /* look up for requested entry type */
319 for (i = 0; i < db_dir.entry_count && !ret; i++) {
320 if (db_dir.entry_list[i].entry_type == entry_type) {
321 switch (entry_type) {
322 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
323 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
324 /* invalid db entry size */
325 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
328 /* read runtime database entry */
329 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
330 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
333 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
334 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
335 /* invalid db entry size */
336 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
339 /* read runtime database entry */
340 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
341 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
354 static int psp_init_sriov_microcode(struct psp_context *psp)
356 struct amdgpu_device *adev = psp->adev;
359 switch (adev->ip_versions[MP0_HWIP][0]) {
360 case IP_VERSION(9, 0, 0):
361 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
362 ret = psp_init_cap_microcode(psp, "vega10");
364 case IP_VERSION(11, 0, 9):
365 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
366 ret = psp_init_cap_microcode(psp, "navi12");
368 case IP_VERSION(11, 0, 7):
369 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
370 ret = psp_init_cap_microcode(psp, "sienna_cichlid");
372 case IP_VERSION(13, 0, 2):
373 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
374 ret = psp_init_cap_microcode(psp, "aldebaran");
375 ret &= psp_init_ta_microcode(psp, "aldebaran");
377 case IP_VERSION(13, 0, 0):
378 adev->virt.autoload_ucode_id = 0;
380 case IP_VERSION(13, 0, 10):
381 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
390 static int psp_sw_init(void *handle)
392 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
393 struct psp_context *psp = &adev->psp;
395 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
396 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
397 struct psp_runtime_scpm_entry scpm_entry;
399 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
401 DRM_ERROR("Failed to allocate memory to command buffer!\n");
405 if (amdgpu_sriov_vf(adev))
406 ret = psp_init_sriov_microcode(psp);
408 ret = psp_init_microcode(psp);
410 DRM_ERROR("Failed to load psp firmware!\n");
414 adev->psp.xgmi_context.supports_extended_data =
415 !adev->gmc.xgmi.connected_to_cpu &&
416 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
418 memset(&scpm_entry, 0, sizeof(scpm_entry));
419 if ((psp_get_runtime_db_entry(adev,
420 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
422 (SCPM_DISABLE != scpm_entry.scpm_status)) {
423 adev->scpm_enabled = true;
424 adev->scpm_status = scpm_entry.scpm_status;
426 adev->scpm_enabled = false;
427 adev->scpm_status = SCPM_DISABLE;
430 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
432 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
433 if (psp_get_runtime_db_entry(adev,
434 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
436 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
437 if ((psp->boot_cfg_bitmask) &
438 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
439 /* If psp runtime database exists, then
440 * only enable two stage memory training
441 * when TWO_STAGE_DRAM_TRAINING bit is set
442 * in runtime database */
443 mem_training_ctx->enable_mem_training = true;
447 /* If psp runtime database doesn't exist or
448 * is invalid, force enable two stage memory
450 mem_training_ctx->enable_mem_training = true;
453 if (mem_training_ctx->enable_mem_training) {
454 ret = psp_memory_training_init(psp);
456 DRM_ERROR("Failed to initialize memory training!\n");
460 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
462 DRM_ERROR("Failed to process memory training!\n");
467 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
468 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
469 ret= psp_sysfs_init(adev);
475 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
476 amdgpu_sriov_vf(adev) ?
477 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
479 &psp->fw_pri_mc_addr,
484 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
485 AMDGPU_GEM_DOMAIN_VRAM,
487 &psp->fence_buf_mc_addr,
492 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
493 AMDGPU_GEM_DOMAIN_VRAM,
494 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
495 (void **)&psp->cmd_buf_mem);
502 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
503 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
505 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
506 &psp->fence_buf_mc_addr, &psp->fence_buf);
510 static int psp_sw_fini(void *handle)
512 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
513 struct psp_context *psp = &adev->psp;
514 struct psp_gfx_cmd_resp *cmd = psp->cmd;
516 psp_memory_training_fini(psp);
518 release_firmware(psp->sos_fw);
521 release_firmware(psp->asd_fw);
524 release_firmware(psp->ta_fw);
527 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");
800 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
801 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
803 AMDGPU_HAS_VRAM(psp->adev) ?
804 AMDGPU_GEM_DOMAIN_VRAM :
805 AMDGPU_GEM_DOMAIN_GTT,
806 &psp->tmr_bo, &psp->tmr_mc_addr,
813 static bool psp_skip_tmr(struct psp_context *psp)
815 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
816 case IP_VERSION(11, 0, 9):
817 case IP_VERSION(11, 0, 7):
818 case IP_VERSION(13, 0, 2):
819 case IP_VERSION(13, 0, 10):
826 static int psp_tmr_load(struct psp_context *psp)
829 struct psp_gfx_cmd_resp *cmd;
831 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
832 * Already set up by host driver.
834 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
837 cmd = acquire_psp_cmd_buf(psp);
839 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
840 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
841 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
843 ret = psp_cmd_submit_buf(psp, NULL, cmd,
844 psp->fence_buf_mc_addr);
846 release_psp_cmd_buf(psp);
851 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
852 struct psp_gfx_cmd_resp *cmd)
854 if (amdgpu_sriov_vf(psp->adev))
855 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
857 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
860 static int psp_tmr_unload(struct psp_context *psp)
863 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
865 psp_prep_tmr_unload_cmd_buf(psp, cmd);
866 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
868 ret = psp_cmd_submit_buf(psp, NULL, cmd,
869 psp->fence_buf_mc_addr);
871 release_psp_cmd_buf(psp);
876 static int psp_tmr_terminate(struct psp_context *psp)
878 return psp_tmr_unload(psp);
881 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
882 uint64_t *output_ptr)
885 struct psp_gfx_cmd_resp *cmd;
890 if (amdgpu_sriov_vf(psp->adev))
893 cmd = acquire_psp_cmd_buf(psp);
895 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
897 ret = psp_cmd_submit_buf(psp, NULL, cmd,
898 psp->fence_buf_mc_addr);
901 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
902 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
905 release_psp_cmd_buf(psp);
910 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
912 struct psp_context *psp = &adev->psp;
913 struct psp_gfx_cmd_resp *cmd;
916 if (amdgpu_sriov_vf(adev))
919 cmd = acquire_psp_cmd_buf(psp);
921 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
922 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
924 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
927 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
930 release_psp_cmd_buf(psp);
935 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
938 struct psp_context *psp = &adev->psp;
939 struct psp_gfx_cmd_resp *cmd;
941 if (amdgpu_sriov_vf(adev))
944 cmd = acquire_psp_cmd_buf(psp);
946 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
947 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
948 cmd->cmd.boot_cfg.boot_config = boot_cfg;
949 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
951 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
953 release_psp_cmd_buf(psp);
958 static int psp_rl_load(struct amdgpu_device *adev)
961 struct psp_context *psp = &adev->psp;
962 struct psp_gfx_cmd_resp *cmd;
964 if (!is_psp_fw_valid(psp->rl))
967 cmd = acquire_psp_cmd_buf(psp);
969 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
970 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
972 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
973 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
974 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
975 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
976 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
978 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
980 release_psp_cmd_buf(psp);
985 static int psp_asd_initialize(struct psp_context *psp)
989 /* If PSP version doesn't match ASD version, asd loading will be failed.
990 * add workaround to bypass it for sriov now.
991 * TODO: add version check to make it common
993 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
996 psp->asd_context.mem_context.shared_mc_addr = 0;
997 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
998 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
1000 ret = psp_ta_load(psp, &psp->asd_context);
1002 psp->asd_context.initialized = true;
1007 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1008 uint32_t session_id)
1010 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1011 cmd->cmd.cmd_unload_ta.session_id = session_id;
1014 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1017 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1019 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1021 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1023 context->resp_status = cmd->resp.status;
1025 release_psp_cmd_buf(psp);
1030 static int psp_asd_terminate(struct psp_context *psp)
1034 if (amdgpu_sriov_vf(psp->adev))
1037 if (!psp->asd_context.initialized)
1040 ret = psp_ta_unload(psp, &psp->asd_context);
1042 psp->asd_context.initialized = false;
1047 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1048 uint32_t id, uint32_t value)
1050 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1051 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1052 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1055 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1058 struct psp_gfx_cmd_resp *cmd;
1061 if (reg >= PSP_REG_LAST)
1064 cmd = acquire_psp_cmd_buf(psp);
1066 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1067 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1069 DRM_ERROR("PSP failed to program reg id %d", reg);
1071 release_psp_cmd_buf(psp);
1076 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1078 struct ta_context *context)
1080 cmd->cmd_id = context->ta_load_type;
1081 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1082 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1083 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1085 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1086 lower_32_bits(context->mem_context.shared_mc_addr);
1087 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1088 upper_32_bits(context->mem_context.shared_mc_addr);
1089 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1092 int psp_ta_init_shared_buf(struct psp_context *psp,
1093 struct ta_mem_context *mem_ctx)
1096 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1097 * physical) for ta to host memory
1099 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1100 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1101 AMDGPU_GEM_DOMAIN_GTT,
1102 &mem_ctx->shared_bo,
1103 &mem_ctx->shared_mc_addr,
1104 &mem_ctx->shared_buf);
1107 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1109 uint32_t session_id)
1111 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1112 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1113 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1116 int psp_ta_invoke(struct psp_context *psp,
1118 struct ta_context *context)
1121 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1123 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1125 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1126 psp->fence_buf_mc_addr);
1128 context->resp_status = cmd->resp.status;
1130 release_psp_cmd_buf(psp);
1135 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1138 struct psp_gfx_cmd_resp *cmd;
1140 cmd = acquire_psp_cmd_buf(psp);
1142 psp_copy_fw(psp, context->bin_desc.start_addr,
1143 context->bin_desc.size_bytes);
1145 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1147 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1148 psp->fence_buf_mc_addr);
1150 context->resp_status = cmd->resp.status;
1153 context->session_id = cmd->resp.session_id;
1156 release_psp_cmd_buf(psp);
1161 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1163 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1166 int psp_xgmi_terminate(struct psp_context *psp)
1169 struct amdgpu_device *adev = psp->adev;
1171 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1172 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1173 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1174 adev->gmc.xgmi.connected_to_cpu))
1177 if (!psp->xgmi_context.context.initialized)
1180 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1182 psp->xgmi_context.context.initialized = false;
1187 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1189 struct ta_xgmi_shared_memory *xgmi_cmd;
1193 !psp->xgmi_context.context.bin_desc.size_bytes ||
1194 !psp->xgmi_context.context.bin_desc.start_addr)
1200 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1201 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1203 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1204 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1210 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1212 psp->xgmi_context.context.initialized = true;
1217 /* Initialize XGMI session */
1218 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1219 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1220 xgmi_cmd->flag_extend_link_record = set_extended_data;
1221 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1223 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1228 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1230 struct ta_xgmi_shared_memory *xgmi_cmd;
1233 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1234 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1236 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1238 /* Invoke xgmi ta to get hive id */
1239 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1243 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1248 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1250 struct ta_xgmi_shared_memory *xgmi_cmd;
1253 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1254 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1256 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1258 /* Invoke xgmi ta to get the node id */
1259 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1263 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1268 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1270 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1271 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1275 * Chips that support extended topology information require the driver to
1276 * reflect topology information in the opposite direction. This is
1277 * because the TA has already exceeded its link record limit and if the
1278 * TA holds bi-directional information, the driver would have to do
1279 * multiple fetches instead of just two.
1281 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1282 struct psp_xgmi_node_info node_info)
1284 struct amdgpu_device *mirror_adev;
1285 struct amdgpu_hive_info *hive;
1286 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1287 uint64_t dst_node_id = node_info.node_id;
1288 uint8_t dst_num_hops = node_info.num_hops;
1289 uint8_t dst_num_links = node_info.num_links;
1291 hive = amdgpu_get_xgmi_hive(psp->adev);
1292 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1293 struct psp_xgmi_topology_info *mirror_top_info;
1296 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1299 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1300 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1301 if (mirror_top_info->nodes[j].node_id != src_node_id)
1304 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1306 * prevent 0 num_links value re-reflection since reflection
1307 * criteria is based on num_hops (direct or indirect).
1311 mirror_top_info->nodes[j].num_links = dst_num_links;
1319 amdgpu_put_xgmi_hive(hive);
1322 int psp_xgmi_get_topology_info(struct psp_context *psp,
1324 struct psp_xgmi_topology_info *topology,
1325 bool get_extended_data)
1327 struct ta_xgmi_shared_memory *xgmi_cmd;
1328 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1329 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1333 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1336 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1337 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1338 xgmi_cmd->flag_extend_link_record = get_extended_data;
1340 /* Fill in the shared memory with topology information as input */
1341 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1342 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1343 topology_info_input->num_nodes = number_devices;
1345 for (i = 0; i < topology_info_input->num_nodes; i++) {
1346 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1347 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1348 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1349 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1352 /* Invoke xgmi ta to get the topology information */
1353 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1357 /* Read the output topology information from the shared memory */
1358 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1359 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1360 for (i = 0; i < topology->num_nodes; i++) {
1361 /* extended data will either be 0 or equal to non-extended data */
1362 if (topology_info_output->nodes[i].num_hops)
1363 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1365 /* non-extended data gets everything here so no need to update */
1366 if (!get_extended_data) {
1367 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1368 topology->nodes[i].is_sharing_enabled =
1369 topology_info_output->nodes[i].is_sharing_enabled;
1370 topology->nodes[i].sdma_engine =
1371 topology_info_output->nodes[i].sdma_engine;
1376 /* Invoke xgmi ta again to get the link information */
1377 if (psp_xgmi_peer_link_info_supported(psp)) {
1378 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1380 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1382 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1387 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1388 for (i = 0; i < topology->num_nodes; i++) {
1389 /* accumulate num_links on extended data */
1390 topology->nodes[i].num_links = get_extended_data ?
1391 topology->nodes[i].num_links +
1392 link_info_output->nodes[i].num_links :
1393 link_info_output->nodes[i].num_links;
1395 /* reflect the topology information for bi-directionality */
1396 if (psp->xgmi_context.supports_extended_data &&
1397 get_extended_data && topology->nodes[i].num_hops)
1398 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1405 int psp_xgmi_set_topology_info(struct psp_context *psp,
1407 struct psp_xgmi_topology_info *topology)
1409 struct ta_xgmi_shared_memory *xgmi_cmd;
1410 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1413 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1416 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1417 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1419 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1420 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1421 topology_info_input->num_nodes = number_devices;
1423 for (i = 0; i < topology_info_input->num_nodes; i++) {
1424 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1425 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1426 topology_info_input->nodes[i].is_sharing_enabled = 1;
1427 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1430 /* Invoke xgmi ta to set topology information */
1431 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1435 static void psp_ras_ta_check_status(struct psp_context *psp)
1437 struct ta_ras_shared_memory *ras_cmd =
1438 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1440 switch (ras_cmd->ras_status) {
1441 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1442 dev_warn(psp->adev->dev,
1443 "RAS WARNING: cmd failed due to unsupported ip\n");
1445 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1446 dev_warn(psp->adev->dev,
1447 "RAS WARNING: cmd failed due to unsupported error injection\n");
1449 case TA_RAS_STATUS__SUCCESS:
1451 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1452 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1453 dev_warn(psp->adev->dev,
1454 "RAS WARNING: Inject error to critical region is not allowed\n");
1457 dev_warn(psp->adev->dev,
1458 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1463 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1465 struct ta_ras_shared_memory *ras_cmd;
1468 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1471 * TODO: bypass the loading in sriov for now
1473 if (amdgpu_sriov_vf(psp->adev))
1476 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1478 if (amdgpu_ras_intr_triggered())
1481 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1483 DRM_WARN("RAS: Unsupported Interface");
1488 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1489 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1491 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1493 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1494 dev_warn(psp->adev->dev,
1495 "RAS internal register access blocked\n");
1497 psp_ras_ta_check_status(psp);
1503 int psp_ras_enable_features(struct psp_context *psp,
1504 union ta_ras_cmd_input *info, bool enable)
1506 struct ta_ras_shared_memory *ras_cmd;
1509 if (!psp->ras_context.context.initialized)
1512 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1513 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1516 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1518 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1520 ras_cmd->ras_in_message = *info;
1522 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1529 int psp_ras_terminate(struct psp_context *psp)
1534 * TODO: bypass the terminate in sriov for now
1536 if (amdgpu_sriov_vf(psp->adev))
1539 if (!psp->ras_context.context.initialized)
1542 ret = psp_ta_unload(psp, &psp->ras_context.context);
1544 psp->ras_context.context.initialized = false;
1549 int psp_ras_initialize(struct psp_context *psp)
1552 uint32_t boot_cfg = 0xFF;
1553 struct amdgpu_device *adev = psp->adev;
1554 struct ta_ras_shared_memory *ras_cmd;
1557 * TODO: bypass the initialize in sriov for now
1559 if (amdgpu_sriov_vf(adev))
1562 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1563 !adev->psp.ras_context.context.bin_desc.start_addr) {
1564 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1568 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1569 /* query GECC enablement status from boot config
1570 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1572 ret = psp_boot_config_get(adev, &boot_cfg);
1574 dev_warn(adev->dev, "PSP get boot config failed\n");
1576 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1578 dev_info(adev->dev, "GECC is disabled\n");
1580 /* disable GECC in next boot cycle if ras is
1581 * disabled by module parameter amdgpu_ras_enable
1582 * and/or amdgpu_ras_mask, or boot_config_get call
1585 ret = psp_boot_config_set(adev, 0);
1587 dev_warn(adev->dev, "PSP set boot config failed\n");
1589 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1590 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1593 if (1 == boot_cfg) {
1594 dev_info(adev->dev, "GECC is enabled\n");
1596 /* enable GECC in next boot cycle if it is disabled
1597 * in boot config, or force enable GECC if failed to
1598 * get boot configuration
1600 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1602 dev_warn(adev->dev, "PSP set boot config failed\n");
1604 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1609 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1610 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1612 if (!psp->ras_context.context.mem_context.shared_buf) {
1613 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1618 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1619 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1621 if (amdgpu_ras_is_poison_mode_supported(adev))
1622 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1623 if (!adev->gmc.xgmi.connected_to_cpu)
1624 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1626 ret = psp_ta_load(psp, &psp->ras_context.context);
1628 if (!ret && !ras_cmd->ras_status)
1629 psp->ras_context.context.initialized = true;
1631 if (ras_cmd->ras_status)
1632 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1634 /* fail to load RAS TA */
1635 psp->ras_context.context.initialized = false;
1641 int psp_ras_trigger_error(struct psp_context *psp,
1642 struct ta_ras_trigger_error_input *info)
1644 struct ta_ras_shared_memory *ras_cmd;
1647 if (!psp->ras_context.context.initialized)
1650 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1651 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1653 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1654 ras_cmd->ras_in_message.trigger_error = *info;
1656 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1660 /* If err_event_athub occurs error inject was successful, however
1661 return status from TA is no long reliable */
1662 if (amdgpu_ras_intr_triggered())
1665 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1667 else if (ras_cmd->ras_status)
1675 static int psp_hdcp_initialize(struct psp_context *psp)
1680 * TODO: bypass the initialize in sriov for now
1682 if (amdgpu_sriov_vf(psp->adev))
1685 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1686 !psp->hdcp_context.context.bin_desc.start_addr) {
1687 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1691 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1692 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1694 if (!psp->hdcp_context.context.initialized) {
1695 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1700 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1702 psp->hdcp_context.context.initialized = true;
1703 mutex_init(&psp->hdcp_context.mutex);
1709 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1712 * TODO: bypass the loading in sriov for now
1714 if (amdgpu_sriov_vf(psp->adev))
1717 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1720 static int psp_hdcp_terminate(struct psp_context *psp)
1725 * TODO: bypass the terminate in sriov for now
1727 if (amdgpu_sriov_vf(psp->adev))
1730 if (!psp->hdcp_context.context.initialized)
1733 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1735 psp->hdcp_context.context.initialized = false;
1742 static int psp_dtm_initialize(struct psp_context *psp)
1747 * TODO: bypass the initialize in sriov for now
1749 if (amdgpu_sriov_vf(psp->adev))
1752 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1753 !psp->dtm_context.context.bin_desc.start_addr) {
1754 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1758 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1759 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1761 if (!psp->dtm_context.context.initialized) {
1762 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1767 ret = psp_ta_load(psp, &psp->dtm_context.context);
1769 psp->dtm_context.context.initialized = true;
1770 mutex_init(&psp->dtm_context.mutex);
1776 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1779 * TODO: bypass the loading in sriov for now
1781 if (amdgpu_sriov_vf(psp->adev))
1784 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1787 static int psp_dtm_terminate(struct psp_context *psp)
1792 * TODO: bypass the terminate in sriov for now
1794 if (amdgpu_sriov_vf(psp->adev))
1797 if (!psp->dtm_context.context.initialized)
1800 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1802 psp->dtm_context.context.initialized = false;
1809 static int psp_rap_initialize(struct psp_context *psp)
1812 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1815 * TODO: bypass the initialize in sriov for now
1817 if (amdgpu_sriov_vf(psp->adev))
1820 if (!psp->rap_context.context.bin_desc.size_bytes ||
1821 !psp->rap_context.context.bin_desc.start_addr) {
1822 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1826 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1827 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1829 if (!psp->rap_context.context.initialized) {
1830 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1835 ret = psp_ta_load(psp, &psp->rap_context.context);
1837 psp->rap_context.context.initialized = true;
1838 mutex_init(&psp->rap_context.mutex);
1842 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1843 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1844 psp_rap_terminate(psp);
1845 /* free rap shared memory */
1846 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1848 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1857 static int psp_rap_terminate(struct psp_context *psp)
1861 if (!psp->rap_context.context.initialized)
1864 ret = psp_ta_unload(psp, &psp->rap_context.context);
1866 psp->rap_context.context.initialized = false;
1871 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1873 struct ta_rap_shared_memory *rap_cmd;
1876 if (!psp->rap_context.context.initialized)
1879 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1880 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1883 mutex_lock(&psp->rap_context.mutex);
1885 rap_cmd = (struct ta_rap_shared_memory *)
1886 psp->rap_context.context.mem_context.shared_buf;
1887 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1889 rap_cmd->cmd_id = ta_cmd_id;
1890 rap_cmd->validation_method_id = METHOD_A;
1892 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1897 *status = rap_cmd->rap_status;
1900 mutex_unlock(&psp->rap_context.mutex);
1906 /* securedisplay start */
1907 static int psp_securedisplay_initialize(struct psp_context *psp)
1910 struct ta_securedisplay_cmd *securedisplay_cmd;
1913 * TODO: bypass the initialize in sriov for now
1915 if (amdgpu_sriov_vf(psp->adev))
1918 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1919 !psp->securedisplay_context.context.bin_desc.start_addr) {
1920 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1924 psp->securedisplay_context.context.mem_context.shared_mem_size =
1925 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1926 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1928 if (!psp->securedisplay_context.context.initialized) {
1929 ret = psp_ta_init_shared_buf(psp,
1930 &psp->securedisplay_context.context.mem_context);
1935 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1937 psp->securedisplay_context.context.initialized = true;
1938 mutex_init(&psp->securedisplay_context.mutex);
1942 mutex_lock(&psp->securedisplay_context.mutex);
1944 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1945 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1947 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1949 mutex_unlock(&psp->securedisplay_context.mutex);
1952 psp_securedisplay_terminate(psp);
1953 /* free securedisplay shared memory */
1954 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1955 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1959 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1960 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1961 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1962 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1968 static int psp_securedisplay_terminate(struct psp_context *psp)
1973 * TODO:bypass the terminate in sriov for now
1975 if (amdgpu_sriov_vf(psp->adev))
1978 if (!psp->securedisplay_context.context.initialized)
1981 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1983 psp->securedisplay_context.context.initialized = false;
1988 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1992 if (!psp->securedisplay_context.context.initialized)
1995 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1996 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1999 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2003 /* SECUREDISPLAY end */
2005 static int psp_hw_start(struct psp_context *psp)
2007 struct amdgpu_device *adev = psp->adev;
2010 if (!amdgpu_sriov_vf(adev)) {
2011 if ((is_psp_fw_valid(psp->kdb)) &&
2012 (psp->funcs->bootloader_load_kdb != NULL)) {
2013 ret = psp_bootloader_load_kdb(psp);
2015 DRM_ERROR("PSP load kdb failed!\n");
2020 if ((is_psp_fw_valid(psp->spl)) &&
2021 (psp->funcs->bootloader_load_spl != NULL)) {
2022 ret = psp_bootloader_load_spl(psp);
2024 DRM_ERROR("PSP load spl failed!\n");
2029 if ((is_psp_fw_valid(psp->sys)) &&
2030 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2031 ret = psp_bootloader_load_sysdrv(psp);
2033 DRM_ERROR("PSP load sys drv failed!\n");
2038 if ((is_psp_fw_valid(psp->soc_drv)) &&
2039 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2040 ret = psp_bootloader_load_soc_drv(psp);
2042 DRM_ERROR("PSP load soc drv failed!\n");
2047 if ((is_psp_fw_valid(psp->intf_drv)) &&
2048 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2049 ret = psp_bootloader_load_intf_drv(psp);
2051 DRM_ERROR("PSP load intf drv failed!\n");
2056 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2057 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2058 ret = psp_bootloader_load_dbg_drv(psp);
2060 DRM_ERROR("PSP load dbg drv failed!\n");
2065 if ((is_psp_fw_valid(psp->ras_drv)) &&
2066 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2067 ret = psp_bootloader_load_ras_drv(psp);
2069 DRM_ERROR("PSP load ras_drv failed!\n");
2074 if ((is_psp_fw_valid(psp->sos)) &&
2075 (psp->funcs->bootloader_load_sos != NULL)) {
2076 ret = psp_bootloader_load_sos(psp);
2078 DRM_ERROR("PSP load sos failed!\n");
2084 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2086 DRM_ERROR("PSP create ring failed!\n");
2090 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2093 ret = psp_tmr_init(psp);
2095 DRM_ERROR("PSP tmr init failed!\n");
2101 * For ASICs with DF Cstate management centralized
2102 * to PMFW, TMR setup should be performed after PMFW
2103 * loaded and before other non-psp firmware loaded.
2105 if (psp->pmfw_centralized_cstate_management) {
2106 ret = psp_load_smu_fw(psp);
2111 ret = psp_tmr_load(psp);
2113 DRM_ERROR("PSP load tmr failed!\n");
2120 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2121 enum psp_gfx_fw_type *type)
2123 switch (ucode->ucode_id) {
2124 case AMDGPU_UCODE_ID_CAP:
2125 *type = GFX_FW_TYPE_CAP;
2127 case AMDGPU_UCODE_ID_SDMA0:
2128 *type = GFX_FW_TYPE_SDMA0;
2130 case AMDGPU_UCODE_ID_SDMA1:
2131 *type = GFX_FW_TYPE_SDMA1;
2133 case AMDGPU_UCODE_ID_SDMA2:
2134 *type = GFX_FW_TYPE_SDMA2;
2136 case AMDGPU_UCODE_ID_SDMA3:
2137 *type = GFX_FW_TYPE_SDMA3;
2139 case AMDGPU_UCODE_ID_SDMA4:
2140 *type = GFX_FW_TYPE_SDMA4;
2142 case AMDGPU_UCODE_ID_SDMA5:
2143 *type = GFX_FW_TYPE_SDMA5;
2145 case AMDGPU_UCODE_ID_SDMA6:
2146 *type = GFX_FW_TYPE_SDMA6;
2148 case AMDGPU_UCODE_ID_SDMA7:
2149 *type = GFX_FW_TYPE_SDMA7;
2151 case AMDGPU_UCODE_ID_CP_MES:
2152 *type = GFX_FW_TYPE_CP_MES;
2154 case AMDGPU_UCODE_ID_CP_MES_DATA:
2155 *type = GFX_FW_TYPE_MES_STACK;
2157 case AMDGPU_UCODE_ID_CP_MES1:
2158 *type = GFX_FW_TYPE_CP_MES_KIQ;
2160 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2161 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2163 case AMDGPU_UCODE_ID_CP_CE:
2164 *type = GFX_FW_TYPE_CP_CE;
2166 case AMDGPU_UCODE_ID_CP_PFP:
2167 *type = GFX_FW_TYPE_CP_PFP;
2169 case AMDGPU_UCODE_ID_CP_ME:
2170 *type = GFX_FW_TYPE_CP_ME;
2172 case AMDGPU_UCODE_ID_CP_MEC1:
2173 *type = GFX_FW_TYPE_CP_MEC;
2175 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2176 *type = GFX_FW_TYPE_CP_MEC_ME1;
2178 case AMDGPU_UCODE_ID_CP_MEC2:
2179 *type = GFX_FW_TYPE_CP_MEC;
2181 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2182 *type = GFX_FW_TYPE_CP_MEC_ME2;
2184 case AMDGPU_UCODE_ID_RLC_P:
2185 *type = GFX_FW_TYPE_RLC_P;
2187 case AMDGPU_UCODE_ID_RLC_V:
2188 *type = GFX_FW_TYPE_RLC_V;
2190 case AMDGPU_UCODE_ID_RLC_G:
2191 *type = GFX_FW_TYPE_RLC_G;
2193 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2194 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2196 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2197 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2199 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2200 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2202 case AMDGPU_UCODE_ID_RLC_IRAM:
2203 *type = GFX_FW_TYPE_RLC_IRAM;
2205 case AMDGPU_UCODE_ID_RLC_DRAM:
2206 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2208 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2209 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2211 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2212 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2214 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2215 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2217 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2218 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2220 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2221 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2223 case AMDGPU_UCODE_ID_SMC:
2224 *type = GFX_FW_TYPE_SMU;
2226 case AMDGPU_UCODE_ID_PPTABLE:
2227 *type = GFX_FW_TYPE_PPTABLE;
2229 case AMDGPU_UCODE_ID_UVD:
2230 *type = GFX_FW_TYPE_UVD;
2232 case AMDGPU_UCODE_ID_UVD1:
2233 *type = GFX_FW_TYPE_UVD1;
2235 case AMDGPU_UCODE_ID_VCE:
2236 *type = GFX_FW_TYPE_VCE;
2238 case AMDGPU_UCODE_ID_VCN:
2239 *type = GFX_FW_TYPE_VCN;
2241 case AMDGPU_UCODE_ID_VCN1:
2242 *type = GFX_FW_TYPE_VCN1;
2244 case AMDGPU_UCODE_ID_DMCU_ERAM:
2245 *type = GFX_FW_TYPE_DMCU_ERAM;
2247 case AMDGPU_UCODE_ID_DMCU_INTV:
2248 *type = GFX_FW_TYPE_DMCU_ISR;
2250 case AMDGPU_UCODE_ID_VCN0_RAM:
2251 *type = GFX_FW_TYPE_VCN0_RAM;
2253 case AMDGPU_UCODE_ID_VCN1_RAM:
2254 *type = GFX_FW_TYPE_VCN1_RAM;
2256 case AMDGPU_UCODE_ID_DMCUB:
2257 *type = GFX_FW_TYPE_DMUB;
2259 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2260 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2262 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2263 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2265 case AMDGPU_UCODE_ID_IMU_I:
2266 *type = GFX_FW_TYPE_IMU_I;
2268 case AMDGPU_UCODE_ID_IMU_D:
2269 *type = GFX_FW_TYPE_IMU_D;
2271 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2272 *type = GFX_FW_TYPE_RS64_PFP;
2274 case AMDGPU_UCODE_ID_CP_RS64_ME:
2275 *type = GFX_FW_TYPE_RS64_ME;
2277 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2278 *type = GFX_FW_TYPE_RS64_MEC;
2280 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2281 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2283 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2284 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2286 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2287 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2289 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2290 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2292 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2293 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2295 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2296 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2298 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2299 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2301 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2302 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2304 case AMDGPU_UCODE_ID_MAXIMUM:
2312 static void psp_print_fw_hdr(struct psp_context *psp,
2313 struct amdgpu_firmware_info *ucode)
2315 struct amdgpu_device *adev = psp->adev;
2316 struct common_firmware_header *hdr;
2318 switch (ucode->ucode_id) {
2319 case AMDGPU_UCODE_ID_SDMA0:
2320 case AMDGPU_UCODE_ID_SDMA1:
2321 case AMDGPU_UCODE_ID_SDMA2:
2322 case AMDGPU_UCODE_ID_SDMA3:
2323 case AMDGPU_UCODE_ID_SDMA4:
2324 case AMDGPU_UCODE_ID_SDMA5:
2325 case AMDGPU_UCODE_ID_SDMA6:
2326 case AMDGPU_UCODE_ID_SDMA7:
2327 hdr = (struct common_firmware_header *)
2328 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2329 amdgpu_ucode_print_sdma_hdr(hdr);
2331 case AMDGPU_UCODE_ID_CP_CE:
2332 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2333 amdgpu_ucode_print_gfx_hdr(hdr);
2335 case AMDGPU_UCODE_ID_CP_PFP:
2336 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2337 amdgpu_ucode_print_gfx_hdr(hdr);
2339 case AMDGPU_UCODE_ID_CP_ME:
2340 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2341 amdgpu_ucode_print_gfx_hdr(hdr);
2343 case AMDGPU_UCODE_ID_CP_MEC1:
2344 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2345 amdgpu_ucode_print_gfx_hdr(hdr);
2347 case AMDGPU_UCODE_ID_RLC_G:
2348 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2349 amdgpu_ucode_print_rlc_hdr(hdr);
2351 case AMDGPU_UCODE_ID_SMC:
2352 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2353 amdgpu_ucode_print_smc_hdr(hdr);
2360 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2361 struct psp_gfx_cmd_resp *cmd)
2364 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2366 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2367 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2368 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2369 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2371 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2373 DRM_ERROR("Unknown firmware type\n");
2378 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2379 struct amdgpu_firmware_info *ucode)
2382 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2384 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2386 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2387 psp->fence_buf_mc_addr);
2390 release_psp_cmd_buf(psp);
2395 static int psp_load_smu_fw(struct psp_context *psp)
2398 struct amdgpu_device *adev = psp->adev;
2399 struct amdgpu_firmware_info *ucode =
2400 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2401 struct amdgpu_ras *ras = psp->ras_context.ras;
2404 * Skip SMU FW reloading in case of using BACO for runpm only,
2405 * as SMU is always alive.
2407 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2410 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2413 if ((amdgpu_in_reset(adev) &&
2414 ras && adev->ras_enabled &&
2415 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2416 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2417 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2419 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2423 ret = psp_execute_non_psp_fw_load(psp, ucode);
2426 DRM_ERROR("PSP load smu failed!\n");
2431 static bool fw_load_skip_check(struct psp_context *psp,
2432 struct amdgpu_firmware_info *ucode)
2434 if (!ucode->fw || !ucode->ucode_size)
2437 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2438 (psp_smu_reload_quirk(psp) ||
2439 psp->autoload_supported ||
2440 psp->pmfw_centralized_cstate_management))
2443 if (amdgpu_sriov_vf(psp->adev) &&
2444 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2447 if (psp->autoload_supported &&
2448 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2449 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2450 /* skip mec JT when autoload is enabled */
2456 int psp_load_fw_list(struct psp_context *psp,
2457 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2460 struct amdgpu_firmware_info *ucode;
2462 for (i = 0; i < ucode_count; ++i) {
2463 ucode = ucode_list[i];
2464 psp_print_fw_hdr(psp, ucode);
2465 ret = psp_execute_non_psp_fw_load(psp, ucode);
2472 static int psp_load_non_psp_fw(struct psp_context *psp)
2475 struct amdgpu_firmware_info *ucode;
2476 struct amdgpu_device *adev = psp->adev;
2478 if (psp->autoload_supported &&
2479 !psp->pmfw_centralized_cstate_management) {
2480 ret = psp_load_smu_fw(psp);
2485 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2486 ucode = &adev->firmware.ucode[i];
2488 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2489 !fw_load_skip_check(psp, ucode)) {
2490 ret = psp_load_smu_fw(psp);
2496 if (fw_load_skip_check(psp, ucode))
2499 if (psp->autoload_supported &&
2500 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2501 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2502 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2503 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2504 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2505 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2506 /* PSP only receive one SDMA fw for sienna_cichlid,
2507 * as all four sdma fw are same */
2510 psp_print_fw_hdr(psp, ucode);
2512 ret = psp_execute_non_psp_fw_load(psp, ucode);
2516 /* Start rlc autoload after psp recieved all the gfx firmware */
2517 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2518 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2519 ret = psp_rlc_autoload_start(psp);
2521 DRM_ERROR("Failed to start rlc autoload\n");
2530 static int psp_load_fw(struct amdgpu_device *adev)
2533 struct psp_context *psp = &adev->psp;
2535 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2536 /* should not destroy ring, only stop */
2537 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2539 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2541 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2543 DRM_ERROR("PSP ring init failed!\n");
2548 ret = psp_hw_start(psp);
2552 ret = psp_load_non_psp_fw(psp);
2556 ret = psp_asd_initialize(psp);
2558 DRM_ERROR("PSP load asd failed!\n");
2562 ret = psp_rl_load(adev);
2564 DRM_ERROR("PSP load RL failed!\n");
2568 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2569 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2570 ret = psp_xgmi_initialize(psp, false, true);
2571 /* Warning the XGMI seesion initialize failure
2572 * Instead of stop driver initialization
2575 dev_err(psp->adev->dev,
2576 "XGMI: Failed to initialize XGMI session\n");
2581 ret = psp_ras_initialize(psp);
2583 dev_err(psp->adev->dev,
2584 "RAS: Failed to initialize RAS\n");
2586 ret = psp_hdcp_initialize(psp);
2588 dev_err(psp->adev->dev,
2589 "HDCP: Failed to initialize HDCP\n");
2591 ret = psp_dtm_initialize(psp);
2593 dev_err(psp->adev->dev,
2594 "DTM: Failed to initialize DTM\n");
2596 ret = psp_rap_initialize(psp);
2598 dev_err(psp->adev->dev,
2599 "RAP: Failed to initialize RAP\n");
2601 ret = psp_securedisplay_initialize(psp);
2603 dev_err(psp->adev->dev,
2604 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2610 psp_free_shared_bufs(psp);
2613 * all cleanup jobs (xgmi terminate, ras terminate,
2614 * ring destroy, cmd/fence/fw buffers destory,
2615 * psp->cmd destory) are delayed to psp_hw_fini
2617 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2621 static int psp_hw_init(void *handle)
2624 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2626 mutex_lock(&adev->firmware.mutex);
2628 * This sequence is just used on hw_init only once, no need on
2631 ret = amdgpu_ucode_init_bo(adev);
2635 ret = psp_load_fw(adev);
2637 DRM_ERROR("PSP firmware loading failed\n");
2641 mutex_unlock(&adev->firmware.mutex);
2645 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2646 mutex_unlock(&adev->firmware.mutex);
2650 static int psp_hw_fini(void *handle)
2652 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2653 struct psp_context *psp = &adev->psp;
2656 psp_ras_terminate(psp);
2657 psp_securedisplay_terminate(psp);
2658 psp_rap_terminate(psp);
2659 psp_dtm_terminate(psp);
2660 psp_hdcp_terminate(psp);
2662 if (adev->gmc.xgmi.num_physical_nodes > 1)
2663 psp_xgmi_terminate(psp);
2666 psp_asd_terminate(psp);
2667 psp_tmr_terminate(psp);
2669 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2671 psp_free_shared_bufs(psp);
2676 static int psp_suspend(void *handle)
2679 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2680 struct psp_context *psp = &adev->psp;
2682 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2683 psp->xgmi_context.context.initialized) {
2684 ret = psp_xgmi_terminate(psp);
2686 DRM_ERROR("Failed to terminate xgmi ta\n");
2692 ret = psp_ras_terminate(psp);
2694 DRM_ERROR("Failed to terminate ras ta\n");
2697 ret = psp_hdcp_terminate(psp);
2699 DRM_ERROR("Failed to terminate hdcp ta\n");
2702 ret = psp_dtm_terminate(psp);
2704 DRM_ERROR("Failed to terminate dtm ta\n");
2707 ret = psp_rap_terminate(psp);
2709 DRM_ERROR("Failed to terminate rap ta\n");
2712 ret = psp_securedisplay_terminate(psp);
2714 DRM_ERROR("Failed to terminate securedisplay ta\n");
2719 ret = psp_asd_terminate(psp);
2721 DRM_ERROR("Failed to terminate asd\n");
2725 ret = psp_tmr_terminate(psp);
2727 DRM_ERROR("Failed to terminate tmr\n");
2731 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2733 DRM_ERROR("PSP ring stop failed\n");
2740 static int psp_resume(void *handle)
2743 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2744 struct psp_context *psp = &adev->psp;
2746 DRM_INFO("PSP is resuming...\n");
2748 if (psp->mem_train_ctx.enable_mem_training) {
2749 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2751 DRM_ERROR("Failed to process memory training!\n");
2756 mutex_lock(&adev->firmware.mutex);
2758 ret = psp_hw_start(psp);
2762 ret = psp_load_non_psp_fw(psp);
2766 ret = psp_asd_initialize(psp);
2768 DRM_ERROR("PSP load asd failed!\n");
2772 ret = psp_rl_load(adev);
2774 dev_err(adev->dev, "PSP load RL failed!\n");
2778 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2779 ret = psp_xgmi_initialize(psp, false, true);
2780 /* Warning the XGMI seesion initialize failure
2781 * Instead of stop driver initialization
2784 dev_err(psp->adev->dev,
2785 "XGMI: Failed to initialize XGMI session\n");
2789 ret = psp_ras_initialize(psp);
2791 dev_err(psp->adev->dev,
2792 "RAS: Failed to initialize RAS\n");
2794 ret = psp_hdcp_initialize(psp);
2796 dev_err(psp->adev->dev,
2797 "HDCP: Failed to initialize HDCP\n");
2799 ret = psp_dtm_initialize(psp);
2801 dev_err(psp->adev->dev,
2802 "DTM: Failed to initialize DTM\n");
2804 ret = psp_rap_initialize(psp);
2806 dev_err(psp->adev->dev,
2807 "RAP: Failed to initialize RAP\n");
2809 ret = psp_securedisplay_initialize(psp);
2811 dev_err(psp->adev->dev,
2812 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2815 mutex_unlock(&adev->firmware.mutex);
2820 DRM_ERROR("PSP resume failed\n");
2821 mutex_unlock(&adev->firmware.mutex);
2825 int psp_gpu_reset(struct amdgpu_device *adev)
2829 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2832 mutex_lock(&adev->psp.mutex);
2833 ret = psp_mode1_reset(&adev->psp);
2834 mutex_unlock(&adev->psp.mutex);
2839 int psp_rlc_autoload_start(struct psp_context *psp)
2842 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2844 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2846 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2847 psp->fence_buf_mc_addr);
2849 release_psp_cmd_buf(psp);
2854 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2855 uint64_t cmd_gpu_addr, int cmd_size)
2857 struct amdgpu_firmware_info ucode = {0};
2859 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2860 AMDGPU_UCODE_ID_VCN0_RAM;
2861 ucode.mc_addr = cmd_gpu_addr;
2862 ucode.ucode_size = cmd_size;
2864 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2867 int psp_ring_cmd_submit(struct psp_context *psp,
2868 uint64_t cmd_buf_mc_addr,
2869 uint64_t fence_mc_addr,
2872 unsigned int psp_write_ptr_reg = 0;
2873 struct psp_gfx_rb_frame *write_frame;
2874 struct psp_ring *ring = &psp->km_ring;
2875 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2876 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2877 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2878 struct amdgpu_device *adev = psp->adev;
2879 uint32_t ring_size_dw = ring->ring_size / 4;
2880 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2882 /* KM (GPCOM) prepare write pointer */
2883 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2885 /* Update KM RB frame pointer to new frame */
2886 /* write_frame ptr increments by size of rb_frame in bytes */
2887 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2888 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2889 write_frame = ring_buffer_start;
2891 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2892 /* Check invalid write_frame ptr address */
2893 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2894 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2895 ring_buffer_start, ring_buffer_end, write_frame);
2896 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2900 /* Initialize KM RB frame */
2901 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2903 /* Update KM RB frame */
2904 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2905 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2906 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2907 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2908 write_frame->fence_value = index;
2909 amdgpu_device_flush_hdp(adev, NULL);
2911 /* Update the write Pointer in DWORDs */
2912 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2913 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2917 int psp_init_asd_microcode(struct psp_context *psp,
2918 const char *chip_name)
2920 struct amdgpu_device *adev = psp->adev;
2921 char fw_name[PSP_FW_NAME_LEN];
2922 const struct psp_firmware_header_v1_0 *asd_hdr;
2926 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2930 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2931 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2935 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2939 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2940 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2941 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2942 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2943 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2944 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2947 dev_err(adev->dev, "fail to initialize asd microcode\n");
2948 release_firmware(adev->psp.asd_fw);
2949 adev->psp.asd_fw = NULL;
2953 int psp_init_toc_microcode(struct psp_context *psp,
2954 const char *chip_name)
2956 struct amdgpu_device *adev = psp->adev;
2957 char fw_name[PSP_FW_NAME_LEN];
2958 const struct psp_firmware_header_v1_0 *toc_hdr;
2962 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2966 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2967 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2971 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2975 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2976 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2977 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2978 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2979 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2980 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2983 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2984 release_firmware(adev->psp.toc_fw);
2985 adev->psp.toc_fw = NULL;
2989 static int parse_sos_bin_descriptor(struct psp_context *psp,
2990 const struct psp_fw_bin_desc *desc,
2991 const struct psp_firmware_header_v2_0 *sos_hdr)
2993 uint8_t *ucode_start_addr = NULL;
2995 if (!psp || !desc || !sos_hdr)
2998 ucode_start_addr = (uint8_t *)sos_hdr +
2999 le32_to_cpu(desc->offset_bytes) +
3000 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3002 switch (desc->fw_type) {
3003 case PSP_FW_TYPE_PSP_SOS:
3004 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3005 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3006 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3007 psp->sos.start_addr = ucode_start_addr;
3009 case PSP_FW_TYPE_PSP_SYS_DRV:
3010 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3011 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3012 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3013 psp->sys.start_addr = ucode_start_addr;
3015 case PSP_FW_TYPE_PSP_KDB:
3016 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3017 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3018 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3019 psp->kdb.start_addr = ucode_start_addr;
3021 case PSP_FW_TYPE_PSP_TOC:
3022 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3023 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3024 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3025 psp->toc.start_addr = ucode_start_addr;
3027 case PSP_FW_TYPE_PSP_SPL:
3028 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3029 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3030 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3031 psp->spl.start_addr = ucode_start_addr;
3033 case PSP_FW_TYPE_PSP_RL:
3034 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3035 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3036 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3037 psp->rl.start_addr = ucode_start_addr;
3039 case PSP_FW_TYPE_PSP_SOC_DRV:
3040 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3041 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3042 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3043 psp->soc_drv.start_addr = ucode_start_addr;
3045 case PSP_FW_TYPE_PSP_INTF_DRV:
3046 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3047 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3048 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3049 psp->intf_drv.start_addr = ucode_start_addr;
3051 case PSP_FW_TYPE_PSP_DBG_DRV:
3052 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3053 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3054 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3055 psp->dbg_drv.start_addr = ucode_start_addr;
3057 case PSP_FW_TYPE_PSP_RAS_DRV:
3058 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3059 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3060 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3061 psp->ras_drv.start_addr = ucode_start_addr;
3064 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3071 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3073 const struct psp_firmware_header_v1_0 *sos_hdr;
3074 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3075 uint8_t *ucode_array_start_addr;
3077 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3078 ucode_array_start_addr = (uint8_t *)sos_hdr +
3079 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3081 if (adev->gmc.xgmi.connected_to_cpu ||
3082 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3083 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3084 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3086 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3087 adev->psp.sys.start_addr = ucode_array_start_addr;
3089 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3090 adev->psp.sos.start_addr = ucode_array_start_addr +
3091 le32_to_cpu(sos_hdr->sos.offset_bytes);
3093 /* Load alternate PSP SOS FW */
3094 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3096 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3097 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3099 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3100 adev->psp.sys.start_addr = ucode_array_start_addr +
3101 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3103 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3104 adev->psp.sos.start_addr = ucode_array_start_addr +
3105 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3108 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3109 dev_warn(adev->dev, "PSP SOS FW not available");
3116 int psp_init_sos_microcode(struct psp_context *psp,
3117 const char *chip_name)
3119 struct amdgpu_device *adev = psp->adev;
3120 char fw_name[PSP_FW_NAME_LEN];
3121 const struct psp_firmware_header_v1_0 *sos_hdr;
3122 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3123 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3124 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3125 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3127 uint8_t *ucode_array_start_addr;
3131 dev_err(adev->dev, "invalid chip name for sos microcode\n");
3135 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3136 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
3140 err = amdgpu_ucode_validate(adev->psp.sos_fw);
3144 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3145 ucode_array_start_addr = (uint8_t *)sos_hdr +
3146 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3147 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3149 switch (sos_hdr->header.header_version_major) {
3151 err = psp_init_sos_base_fw(adev);
3155 if (sos_hdr->header.header_version_minor == 1) {
3156 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3157 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3158 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3159 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3160 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3161 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3162 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3164 if (sos_hdr->header.header_version_minor == 2) {
3165 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3166 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3167 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3168 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3170 if (sos_hdr->header.header_version_minor == 3) {
3171 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3172 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3173 adev->psp.toc.start_addr = ucode_array_start_addr +
3174 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3175 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3176 adev->psp.kdb.start_addr = ucode_array_start_addr +
3177 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3178 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3179 adev->psp.spl.start_addr = ucode_array_start_addr +
3180 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3181 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3182 adev->psp.rl.start_addr = ucode_array_start_addr +
3183 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3187 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3189 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3190 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3195 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3196 err = parse_sos_bin_descriptor(psp,
3197 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3205 "unsupported psp sos firmware\n");
3213 "failed to init sos firmware\n");
3214 release_firmware(adev->psp.sos_fw);
3215 adev->psp.sos_fw = NULL;
3220 static int parse_ta_bin_descriptor(struct psp_context *psp,
3221 const struct psp_fw_bin_desc *desc,
3222 const struct ta_firmware_header_v2_0 *ta_hdr)
3224 uint8_t *ucode_start_addr = NULL;
3226 if (!psp || !desc || !ta_hdr)
3229 ucode_start_addr = (uint8_t *)ta_hdr +
3230 le32_to_cpu(desc->offset_bytes) +
3231 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3233 switch (desc->fw_type) {
3234 case TA_FW_TYPE_PSP_ASD:
3235 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3236 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3237 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3238 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3240 case TA_FW_TYPE_PSP_XGMI:
3241 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3242 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3243 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3245 case TA_FW_TYPE_PSP_RAS:
3246 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3247 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3248 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3250 case TA_FW_TYPE_PSP_HDCP:
3251 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3252 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3253 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3255 case TA_FW_TYPE_PSP_DTM:
3256 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3257 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3258 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3260 case TA_FW_TYPE_PSP_RAP:
3261 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3262 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3263 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3265 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3266 psp->securedisplay_context.context.bin_desc.fw_version =
3267 le32_to_cpu(desc->fw_version);
3268 psp->securedisplay_context.context.bin_desc.size_bytes =
3269 le32_to_cpu(desc->size_bytes);
3270 psp->securedisplay_context.context.bin_desc.start_addr =
3274 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3281 int psp_init_ta_microcode(struct psp_context *psp,
3282 const char *chip_name)
3284 struct amdgpu_device *adev = psp->adev;
3285 char fw_name[PSP_FW_NAME_LEN];
3286 const struct ta_firmware_header_v2_0 *ta_hdr;
3291 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3295 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3296 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3300 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3304 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3306 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3307 dev_err(adev->dev, "unsupported TA header version\n");
3312 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3313 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3318 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3319 err = parse_ta_bin_descriptor(psp,
3320 &ta_hdr->ta_fw_bin[ta_index],
3328 dev_err(adev->dev, "fail to initialize ta microcode\n");
3329 release_firmware(adev->psp.ta_fw);
3330 adev->psp.ta_fw = NULL;
3334 int psp_init_cap_microcode(struct psp_context *psp,
3335 const char *chip_name)
3337 struct amdgpu_device *adev = psp->adev;
3338 char fw_name[PSP_FW_NAME_LEN];
3339 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3340 struct amdgpu_firmware_info *info = NULL;
3344 dev_err(adev->dev, "invalid chip name for cap microcode\n");
3348 if (!amdgpu_sriov_vf(adev)) {
3349 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3353 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3354 err = request_firmware(&adev->psp.cap_fw, fw_name, adev->dev);
3356 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3361 err = amdgpu_ucode_validate(adev->psp.cap_fw);
3363 dev_err(adev->dev, "fail to initialize cap microcode\n");
3367 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3368 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3369 info->fw = adev->psp.cap_fw;
3370 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3371 adev->psp.cap_fw->data;
3372 adev->firmware.fw_size += ALIGN(
3373 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3374 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3375 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3376 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3381 release_firmware(adev->psp.cap_fw);
3382 adev->psp.cap_fw = NULL;
3386 static int psp_set_clockgating_state(void *handle,
3387 enum amd_clockgating_state state)
3392 static int psp_set_powergating_state(void *handle,
3393 enum amd_powergating_state state)
3398 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3399 struct device_attribute *attr,
3402 struct drm_device *ddev = dev_get_drvdata(dev);
3403 struct amdgpu_device *adev = drm_to_adev(ddev);
3407 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3408 DRM_INFO("PSP block is not ready yet.");
3412 mutex_lock(&adev->psp.mutex);
3413 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3414 mutex_unlock(&adev->psp.mutex);
3417 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3421 return sysfs_emit(buf, "%x\n", fw_ver);
3424 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3425 struct device_attribute *attr,
3429 struct drm_device *ddev = dev_get_drvdata(dev);
3430 struct amdgpu_device *adev = drm_to_adev(ddev);
3433 const struct firmware *usbc_pd_fw;
3434 struct amdgpu_bo *fw_buf_bo = NULL;
3435 uint64_t fw_pri_mc_addr;
3436 void *fw_pri_cpu_addr;
3438 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3439 DRM_INFO("PSP block is not ready yet.");
3443 if (!drm_dev_enter(ddev, &idx))
3446 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3447 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3451 /* LFB address which is aligned to 1MB boundary per PSP request */
3452 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3453 AMDGPU_GEM_DOMAIN_VRAM |
3454 AMDGPU_GEM_DOMAIN_GTT,
3455 &fw_buf_bo, &fw_pri_mc_addr,
3460 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3462 mutex_lock(&adev->psp.mutex);
3463 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3464 mutex_unlock(&adev->psp.mutex);
3466 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3469 release_firmware(usbc_pd_fw);
3472 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3480 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3484 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3487 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3488 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3493 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3494 psp_usbc_pd_fw_sysfs_read,
3495 psp_usbc_pd_fw_sysfs_write);
3497 int is_psp_fw_valid(struct psp_bin_desc bin)
3499 return bin.size_bytes;
3502 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3503 struct bin_attribute *bin_attr,
3504 char *buffer, loff_t pos, size_t count)
3506 struct device *dev = kobj_to_dev(kobj);
3507 struct drm_device *ddev = dev_get_drvdata(dev);
3508 struct amdgpu_device *adev = drm_to_adev(ddev);
3510 adev->psp.vbflash_done = false;
3512 /* Safeguard against memory drain */
3513 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3514 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3515 kvfree(adev->psp.vbflash_tmp_buf);
3516 adev->psp.vbflash_tmp_buf = NULL;
3517 adev->psp.vbflash_image_size = 0;
3521 /* TODO Just allocate max for now and optimize to realloc later if needed */
3522 if (!adev->psp.vbflash_tmp_buf) {
3523 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3524 if (!adev->psp.vbflash_tmp_buf)
3528 mutex_lock(&adev->psp.mutex);
3529 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3530 adev->psp.vbflash_image_size += count;
3531 mutex_unlock(&adev->psp.mutex);
3533 dev_info(adev->dev, "VBIOS flash write PSP done");
3538 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3539 struct bin_attribute *bin_attr, char *buffer,
3540 loff_t pos, size_t count)
3542 struct device *dev = kobj_to_dev(kobj);
3543 struct drm_device *ddev = dev_get_drvdata(dev);
3544 struct amdgpu_device *adev = drm_to_adev(ddev);
3545 struct amdgpu_bo *fw_buf_bo = NULL;
3546 uint64_t fw_pri_mc_addr;
3547 void *fw_pri_cpu_addr;
3550 dev_info(adev->dev, "VBIOS flash to PSP started");
3552 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3553 AMDGPU_GPU_PAGE_SIZE,
3554 AMDGPU_GEM_DOMAIN_VRAM,
3561 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3563 mutex_lock(&adev->psp.mutex);
3564 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3565 mutex_unlock(&adev->psp.mutex);
3567 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3570 kvfree(adev->psp.vbflash_tmp_buf);
3571 adev->psp.vbflash_tmp_buf = NULL;
3572 adev->psp.vbflash_image_size = 0;
3575 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3579 dev_info(adev->dev, "VBIOS flash to PSP done");
3583 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3584 struct device_attribute *attr,
3587 struct drm_device *ddev = dev_get_drvdata(dev);
3588 struct amdgpu_device *adev = drm_to_adev(ddev);
3589 uint32_t vbflash_status;
3591 vbflash_status = psp_vbflash_status(&adev->psp);
3592 if (!adev->psp.vbflash_done)
3594 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3597 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3600 static const struct bin_attribute psp_vbflash_bin_attr = {
3601 .attr = {.name = "psp_vbflash", .mode = 0664},
3603 .write = amdgpu_psp_vbflash_write,
3604 .read = amdgpu_psp_vbflash_read,
3607 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3609 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3612 struct psp_context *psp = &adev->psp;
3614 if (amdgpu_sriov_vf(adev))
3617 switch (adev->ip_versions[MP0_HWIP][0]) {
3618 case IP_VERSION(13, 0, 0):
3619 case IP_VERSION(13, 0, 7):
3622 psp_v13_0_set_psp_funcs(psp);
3624 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3626 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3627 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3629 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3636 const struct amd_ip_funcs psp_ip_funcs = {
3638 .early_init = psp_early_init,
3640 .sw_init = psp_sw_init,
3641 .sw_fini = psp_sw_fini,
3642 .hw_init = psp_hw_init,
3643 .hw_fini = psp_hw_fini,
3644 .suspend = psp_suspend,
3645 .resume = psp_resume,
3647 .check_soft_reset = NULL,
3648 .wait_for_idle = NULL,
3650 .set_clockgating_state = psp_set_clockgating_state,
3651 .set_powergating_state = psp_set_powergating_state,
3654 static int psp_sysfs_init(struct amdgpu_device *adev)
3656 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3659 DRM_ERROR("Failed to create USBC PD FW control file!");
3664 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3666 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3667 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3670 static void psp_sysfs_fini(struct amdgpu_device *adev)
3672 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3675 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3677 .type = AMD_IP_BLOCK_TYPE_PSP,
3681 .funcs = &psp_ip_funcs,
3684 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3686 .type = AMD_IP_BLOCK_TYPE_PSP,
3690 .funcs = &psp_ip_funcs,
3693 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3695 .type = AMD_IP_BLOCK_TYPE_PSP,
3699 .funcs = &psp_ip_funcs,
3702 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3703 .type = AMD_IP_BLOCK_TYPE_PSP,
3707 .funcs = &psp_ip_funcs,
3710 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3712 .type = AMD_IP_BLOCK_TYPE_PSP,
3716 .funcs = &psp_ip_funcs,
3719 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3720 .type = AMD_IP_BLOCK_TYPE_PSP,
3724 .funcs = &psp_ip_funcs,
3727 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3728 .type = AMD_IP_BLOCK_TYPE_PSP,
3732 .funcs = &psp_ip_funcs,