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_load_smu_fw(struct psp_context *psp);
49 static int psp_rap_terminate(struct psp_context *psp);
50 static int psp_securedisplay_terminate(struct psp_context *psp);
52 static int psp_ring_init(struct psp_context *psp,
53 enum psp_ring_type ring_type)
56 struct psp_ring *ring;
57 struct amdgpu_device *adev = psp->adev;
61 ring->ring_type = ring_type;
63 /* allocate 4k Page of Local Frame Buffer memory for ring */
64 ring->ring_size = 0x1000;
65 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
66 AMDGPU_GEM_DOMAIN_VRAM |
67 AMDGPU_GEM_DOMAIN_GTT,
69 &ring->ring_mem_mc_addr,
70 (void **)&ring->ring_mem);
80 * Due to DF Cstate management centralized to PMFW, the firmware
81 * loading sequence will be updated as below:
87 * - Load other non-psp fw
89 * - Load XGMI/RAS/HDCP/DTM TA if any
91 * This new sequence is required for
92 * - Arcturus and onwards
94 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
96 struct amdgpu_device *adev = psp->adev;
98 if (amdgpu_sriov_vf(adev)) {
99 psp->pmfw_centralized_cstate_management = false;
103 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
104 case IP_VERSION(11, 0, 0):
105 case IP_VERSION(11, 0, 4):
106 case IP_VERSION(11, 0, 5):
107 case IP_VERSION(11, 0, 7):
108 case IP_VERSION(11, 0, 9):
109 case IP_VERSION(11, 0, 11):
110 case IP_VERSION(11, 0, 12):
111 case IP_VERSION(11, 0, 13):
112 case IP_VERSION(13, 0, 0):
113 case IP_VERSION(13, 0, 2):
114 case IP_VERSION(13, 0, 7):
115 psp->pmfw_centralized_cstate_management = true;
118 psp->pmfw_centralized_cstate_management = false;
123 static int psp_init_sriov_microcode(struct psp_context *psp)
125 struct amdgpu_device *adev = psp->adev;
126 char ucode_prefix[30];
129 amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
131 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
132 case IP_VERSION(9, 0, 0):
133 case IP_VERSION(11, 0, 7):
134 case IP_VERSION(11, 0, 9):
135 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
136 ret = psp_init_cap_microcode(psp, ucode_prefix);
138 case IP_VERSION(13, 0, 2):
139 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
140 ret = psp_init_cap_microcode(psp, ucode_prefix);
141 ret &= psp_init_ta_microcode(psp, ucode_prefix);
143 case IP_VERSION(13, 0, 0):
144 adev->virt.autoload_ucode_id = 0;
146 case IP_VERSION(13, 0, 6):
147 ret = psp_init_cap_microcode(psp, ucode_prefix);
148 ret &= psp_init_ta_microcode(psp, ucode_prefix);
150 case IP_VERSION(13, 0, 10):
151 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
152 ret = psp_init_cap_microcode(psp, ucode_prefix);
160 static int psp_early_init(void *handle)
162 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
163 struct psp_context *psp = &adev->psp;
165 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
166 case IP_VERSION(9, 0, 0):
167 psp_v3_1_set_psp_funcs(psp);
168 psp->autoload_supported = false;
170 case IP_VERSION(10, 0, 0):
171 case IP_VERSION(10, 0, 1):
172 psp_v10_0_set_psp_funcs(psp);
173 psp->autoload_supported = false;
175 case IP_VERSION(11, 0, 2):
176 case IP_VERSION(11, 0, 4):
177 psp_v11_0_set_psp_funcs(psp);
178 psp->autoload_supported = false;
180 case IP_VERSION(11, 0, 0):
181 case IP_VERSION(11, 0, 7):
182 adev->psp.sup_pd_fw_up = !amdgpu_sriov_vf(adev);
184 case IP_VERSION(11, 0, 5):
185 case IP_VERSION(11, 0, 9):
186 case IP_VERSION(11, 0, 11):
187 case IP_VERSION(11, 5, 0):
188 case IP_VERSION(11, 0, 12):
189 case IP_VERSION(11, 0, 13):
190 psp_v11_0_set_psp_funcs(psp);
191 psp->autoload_supported = true;
193 case IP_VERSION(11, 0, 3):
194 case IP_VERSION(12, 0, 1):
195 psp_v12_0_set_psp_funcs(psp);
197 case IP_VERSION(13, 0, 2):
198 case IP_VERSION(13, 0, 6):
199 psp_v13_0_set_psp_funcs(psp);
201 case IP_VERSION(13, 0, 1):
202 case IP_VERSION(13, 0, 3):
203 case IP_VERSION(13, 0, 5):
204 case IP_VERSION(13, 0, 8):
205 case IP_VERSION(13, 0, 11):
206 case IP_VERSION(14, 0, 0):
207 psp_v13_0_set_psp_funcs(psp);
208 psp->autoload_supported = true;
210 case IP_VERSION(11, 0, 8):
211 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
212 psp_v11_0_8_set_psp_funcs(psp);
213 psp->autoload_supported = false;
216 case IP_VERSION(13, 0, 0):
217 case IP_VERSION(13, 0, 7):
218 case IP_VERSION(13, 0, 10):
219 psp_v13_0_set_psp_funcs(psp);
220 psp->autoload_supported = true;
221 adev->psp.sup_ifwi_up = !amdgpu_sriov_vf(adev);
223 case IP_VERSION(13, 0, 4):
224 psp_v13_0_4_set_psp_funcs(psp);
225 psp->autoload_supported = true;
233 psp_check_pmfw_centralized_cstate_management(psp);
235 if (amdgpu_sriov_vf(adev))
236 return psp_init_sriov_microcode(psp);
238 return psp_init_microcode(psp);
241 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
243 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
244 &mem_ctx->shared_buf);
245 mem_ctx->shared_bo = NULL;
248 static void psp_free_shared_bufs(struct psp_context *psp)
253 /* free TMR memory buffer */
254 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
255 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
258 /* free xgmi shared memory */
259 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
261 /* free ras shared memory */
262 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
264 /* free hdcp shared memory */
265 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
267 /* free dtm shared memory */
268 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
270 /* free rap shared memory */
271 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
273 /* free securedisplay shared memory */
274 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
279 static void psp_memory_training_fini(struct psp_context *psp)
281 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
283 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
284 kfree(ctx->sys_cache);
285 ctx->sys_cache = NULL;
288 static int psp_memory_training_init(struct psp_context *psp)
291 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
293 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
294 DRM_DEBUG("memory training is not supported!\n");
298 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
299 if (ctx->sys_cache == NULL) {
300 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
305 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
306 ctx->train_data_size,
307 ctx->p2c_train_data_offset,
308 ctx->c2p_train_data_offset);
309 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
313 psp_memory_training_fini(psp);
318 * Helper funciton to query psp runtime database entry
320 * @adev: amdgpu_device pointer
321 * @entry_type: the type of psp runtime database entry
322 * @db_entry: runtime database entry pointer
324 * Return false if runtime database doesn't exit or entry is invalid
325 * or true if the specific database entry is found, and copy to @db_entry
327 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
328 enum psp_runtime_entry_type entry_type,
331 uint64_t db_header_pos, db_dir_pos;
332 struct psp_runtime_data_header db_header = {0};
333 struct psp_runtime_data_directory db_dir = {0};
337 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 6))
340 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
341 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
343 /* read runtime db header from vram */
344 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
345 sizeof(struct psp_runtime_data_header), false);
347 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
348 /* runtime db doesn't exist, exit */
349 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
353 /* read runtime database entry from vram */
354 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
355 sizeof(struct psp_runtime_data_directory), false);
357 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
358 /* invalid db entry count, exit */
359 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
363 /* look up for requested entry type */
364 for (i = 0; i < db_dir.entry_count && !ret; i++) {
365 if (db_dir.entry_list[i].entry_type == entry_type) {
366 switch (entry_type) {
367 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
368 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
369 /* invalid db entry size */
370 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
373 /* read runtime database entry */
374 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
375 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
378 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
379 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
380 /* invalid db entry size */
381 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
384 /* read runtime database entry */
385 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
386 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
399 static int psp_sw_init(void *handle)
401 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
402 struct psp_context *psp = &adev->psp;
404 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
405 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
406 struct psp_runtime_scpm_entry scpm_entry;
408 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
410 DRM_ERROR("Failed to allocate memory to command buffer!\n");
414 adev->psp.xgmi_context.supports_extended_data =
415 !adev->gmc.xgmi.connected_to_cpu &&
416 amdgpu_ip_version(adev, 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_entry.scpm_status != SCPM_DISABLE)) {
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
444 mem_training_ctx->enable_mem_training = true;
448 /* If psp runtime database doesn't exist or is
449 * invalid, force enable two stage memory training
451 mem_training_ctx->enable_mem_training = true;
454 if (mem_training_ctx->enable_mem_training) {
455 ret = psp_memory_training_init(psp);
457 DRM_ERROR("Failed to initialize memory training!\n");
461 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
463 DRM_ERROR("Failed to process memory training!\n");
468 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
469 (amdgpu_sriov_vf(adev) || adev->debug_use_vram_fw_buf) ?
470 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
472 &psp->fw_pri_mc_addr,
477 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
478 AMDGPU_GEM_DOMAIN_VRAM |
479 AMDGPU_GEM_DOMAIN_GTT,
481 &psp->fence_buf_mc_addr,
486 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
487 AMDGPU_GEM_DOMAIN_VRAM |
488 AMDGPU_GEM_DOMAIN_GTT,
489 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
490 (void **)&psp->cmd_buf_mem);
497 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
498 &psp->fence_buf_mc_addr, &psp->fence_buf);
500 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
501 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
505 static int psp_sw_fini(void *handle)
507 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
508 struct psp_context *psp = &adev->psp;
509 struct psp_gfx_cmd_resp *cmd = psp->cmd;
511 psp_memory_training_fini(psp);
513 amdgpu_ucode_release(&psp->sos_fw);
514 amdgpu_ucode_release(&psp->asd_fw);
515 amdgpu_ucode_release(&psp->ta_fw);
516 amdgpu_ucode_release(&psp->cap_fw);
517 amdgpu_ucode_release(&psp->toc_fw);
522 psp_free_shared_bufs(psp);
524 if (psp->km_ring.ring_mem)
525 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
526 &psp->km_ring.ring_mem_mc_addr,
527 (void **)&psp->km_ring.ring_mem);
529 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
530 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
531 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
532 &psp->fence_buf_mc_addr, &psp->fence_buf);
533 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
534 (void **)&psp->cmd_buf_mem);
539 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
540 uint32_t reg_val, uint32_t mask, bool check_changed)
544 struct amdgpu_device *adev = psp->adev;
546 if (psp->adev->no_hw_access)
549 for (i = 0; i < adev->usec_timeout; i++) {
550 val = RREG32(reg_index);
555 if ((val & mask) == reg_val)
564 int psp_wait_for_spirom_update(struct psp_context *psp, uint32_t reg_index,
565 uint32_t reg_val, uint32_t mask, uint32_t msec_timeout)
569 struct amdgpu_device *adev = psp->adev;
571 if (psp->adev->no_hw_access)
574 for (i = 0; i < msec_timeout; i++) {
575 val = RREG32(reg_index);
576 if ((val & mask) == reg_val)
584 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
587 case GFX_CMD_ID_LOAD_TA:
589 case GFX_CMD_ID_UNLOAD_TA:
591 case GFX_CMD_ID_INVOKE_CMD:
593 case GFX_CMD_ID_LOAD_ASD:
595 case GFX_CMD_ID_SETUP_TMR:
597 case GFX_CMD_ID_LOAD_IP_FW:
599 case GFX_CMD_ID_DESTROY_TMR:
600 return "DESTROY_TMR";
601 case GFX_CMD_ID_SAVE_RESTORE:
602 return "SAVE_RESTORE_IP_FW";
603 case GFX_CMD_ID_SETUP_VMR:
605 case GFX_CMD_ID_DESTROY_VMR:
606 return "DESTROY_VMR";
607 case GFX_CMD_ID_PROG_REG:
609 case GFX_CMD_ID_GET_FW_ATTESTATION:
610 return "GET_FW_ATTESTATION";
611 case GFX_CMD_ID_LOAD_TOC:
612 return "ID_LOAD_TOC";
613 case GFX_CMD_ID_AUTOLOAD_RLC:
614 return "AUTOLOAD_RLC";
615 case GFX_CMD_ID_BOOT_CFG:
618 return "UNKNOWN CMD";
623 psp_cmd_submit_buf(struct psp_context *psp,
624 struct amdgpu_firmware_info *ucode,
625 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
630 bool ras_intr = false;
631 bool skip_unsupport = false;
633 if (psp->adev->no_hw_access)
636 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
638 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
640 index = atomic_inc_return(&psp->fence_value);
641 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
643 atomic_dec(&psp->fence_value);
647 amdgpu_device_invalidate_hdp(psp->adev, NULL);
648 while (*((unsigned int *)psp->fence_buf) != index) {
652 * Shouldn't wait for timeout when err_event_athub occurs,
653 * because gpu reset thread triggered and lock resource should
654 * be released for psp resume sequence.
656 ras_intr = amdgpu_ras_intr_triggered();
659 usleep_range(10, 100);
660 amdgpu_device_invalidate_hdp(psp->adev, NULL);
663 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
664 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
665 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
667 memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
669 /* In some cases, psp response status is not 0 even there is no
670 * problem while the command is submitted. Some version of PSP FW
671 * doesn't write 0 to that field.
672 * So here we would like to only print a warning instead of an error
673 * during psp initialization to avoid breaking hw_init and it doesn't
676 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
678 DRM_WARN("failed to load ucode %s(0x%X) ",
679 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
680 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
681 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
682 psp->cmd_buf_mem->resp.status);
683 /* If any firmware (including CAP) load fails under SRIOV, it should
684 * return failure to stop the VF from initializing.
685 * Also return failure in case of timeout
687 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
694 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
695 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;
727 size = amdgpu_bo_size(tmr_bo);
728 tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
731 if (amdgpu_sriov_vf(psp->adev))
732 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
734 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
735 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
736 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
737 cmd->cmd.cmd_setup_tmr.buf_size = size;
738 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
739 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
740 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
743 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
744 uint64_t pri_buf_mc, uint32_t size)
746 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
747 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
748 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
749 cmd->cmd.cmd_load_toc.toc_size = size;
752 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
753 static int psp_load_toc(struct psp_context *psp,
757 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
759 /* Copy toc to psp firmware private buffer */
760 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
762 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
764 ret = psp_cmd_submit_buf(psp, NULL, cmd,
765 psp->fence_buf_mc_addr);
767 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
769 release_psp_cmd_buf(psp);
774 static bool psp_boottime_tmr(struct psp_context *psp)
776 switch (amdgpu_ip_version(psp->adev, MP0_HWIP, 0)) {
777 case IP_VERSION(13, 0, 6):
784 /* Set up Trusted Memory Region */
785 static int psp_tmr_init(struct psp_context *psp)
793 * According to HW engineer, they prefer the TMR address be "naturally
794 * aligned" , e.g. the start address be an integer divide of TMR size.
796 * Note: this memory need be reserved till the driver
799 tmr_size = PSP_TMR_SIZE(psp->adev);
801 /* For ASICs support RLC autoload, psp will parse the toc
802 * and calculate the total size of TMR needed
804 if (!amdgpu_sriov_vf(psp->adev) &&
805 psp->toc.start_addr &&
806 psp->toc.size_bytes &&
808 ret = psp_load_toc(psp, &tmr_size);
810 DRM_ERROR("Failed to load toc\n");
816 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
817 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
819 AMDGPU_HAS_VRAM(psp->adev) ?
820 AMDGPU_GEM_DOMAIN_VRAM :
821 AMDGPU_GEM_DOMAIN_GTT,
822 &psp->tmr_bo, &psp->tmr_mc_addr,
829 static bool psp_skip_tmr(struct psp_context *psp)
831 switch (amdgpu_ip_version(psp->adev, MP0_HWIP, 0)) {
832 case IP_VERSION(11, 0, 9):
833 case IP_VERSION(11, 0, 7):
834 case IP_VERSION(13, 0, 2):
835 case IP_VERSION(13, 0, 6):
836 case IP_VERSION(13, 0, 10):
843 static int psp_tmr_load(struct psp_context *psp)
846 struct psp_gfx_cmd_resp *cmd;
848 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
849 * Already set up by host driver.
851 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
854 cmd = acquire_psp_cmd_buf(psp);
856 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
858 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
859 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
861 ret = psp_cmd_submit_buf(psp, NULL, cmd,
862 psp->fence_buf_mc_addr);
864 release_psp_cmd_buf(psp);
869 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
870 struct psp_gfx_cmd_resp *cmd)
872 if (amdgpu_sriov_vf(psp->adev))
873 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
875 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
878 static int psp_tmr_unload(struct psp_context *psp)
881 struct psp_gfx_cmd_resp *cmd;
883 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
884 * as TMR is not loaded at all
886 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
889 cmd = acquire_psp_cmd_buf(psp);
891 psp_prep_tmr_unload_cmd_buf(psp, cmd);
892 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
894 ret = psp_cmd_submit_buf(psp, NULL, cmd,
895 psp->fence_buf_mc_addr);
897 release_psp_cmd_buf(psp);
902 static int psp_tmr_terminate(struct psp_context *psp)
904 return psp_tmr_unload(psp);
907 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
908 uint64_t *output_ptr)
911 struct psp_gfx_cmd_resp *cmd;
916 if (amdgpu_sriov_vf(psp->adev))
919 cmd = acquire_psp_cmd_buf(psp);
921 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
923 ret = psp_cmd_submit_buf(psp, NULL, cmd,
924 psp->fence_buf_mc_addr);
927 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
928 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
931 release_psp_cmd_buf(psp);
936 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
938 struct psp_context *psp = &adev->psp;
939 struct psp_gfx_cmd_resp *cmd;
942 if (amdgpu_sriov_vf(adev))
945 cmd = acquire_psp_cmd_buf(psp);
947 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
948 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
950 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
953 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
956 release_psp_cmd_buf(psp);
961 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
964 struct psp_context *psp = &adev->psp;
965 struct psp_gfx_cmd_resp *cmd;
967 if (amdgpu_sriov_vf(adev))
970 cmd = acquire_psp_cmd_buf(psp);
972 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
973 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
974 cmd->cmd.boot_cfg.boot_config = boot_cfg;
975 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
977 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
979 release_psp_cmd_buf(psp);
984 static int psp_rl_load(struct amdgpu_device *adev)
987 struct psp_context *psp = &adev->psp;
988 struct psp_gfx_cmd_resp *cmd;
990 if (!is_psp_fw_valid(psp->rl))
993 cmd = acquire_psp_cmd_buf(psp);
995 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
996 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
998 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
999 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
1000 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
1001 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
1002 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
1004 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1006 release_psp_cmd_buf(psp);
1011 int psp_spatial_partition(struct psp_context *psp, int mode)
1013 struct psp_gfx_cmd_resp *cmd;
1016 if (amdgpu_sriov_vf(psp->adev))
1019 cmd = acquire_psp_cmd_buf(psp);
1021 cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1022 cmd->cmd.cmd_spatial_part.mode = mode;
1024 dev_info(psp->adev->dev, "Requesting %d partitions through PSP", mode);
1025 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1027 release_psp_cmd_buf(psp);
1032 static int psp_asd_initialize(struct psp_context *psp)
1036 /* If PSP version doesn't match ASD version, asd loading will be failed.
1037 * add workaround to bypass it for sriov now.
1038 * TODO: add version check to make it common
1040 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1043 psp->asd_context.mem_context.shared_mc_addr = 0;
1044 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
1045 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
1047 ret = psp_ta_load(psp, &psp->asd_context);
1049 psp->asd_context.initialized = true;
1054 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1055 uint32_t session_id)
1057 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1058 cmd->cmd.cmd_unload_ta.session_id = session_id;
1061 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1064 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1066 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1068 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1070 context->resp_status = cmd->resp.status;
1072 release_psp_cmd_buf(psp);
1077 static int psp_asd_terminate(struct psp_context *psp)
1081 if (amdgpu_sriov_vf(psp->adev))
1084 if (!psp->asd_context.initialized)
1087 ret = psp_ta_unload(psp, &psp->asd_context);
1089 psp->asd_context.initialized = false;
1094 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1095 uint32_t id, uint32_t value)
1097 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1098 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1099 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1102 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1105 struct psp_gfx_cmd_resp *cmd;
1108 if (reg >= PSP_REG_LAST)
1111 cmd = acquire_psp_cmd_buf(psp);
1113 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1114 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1116 DRM_ERROR("PSP failed to program reg id %d", reg);
1118 release_psp_cmd_buf(psp);
1123 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1125 struct ta_context *context)
1127 cmd->cmd_id = context->ta_load_type;
1128 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1129 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1130 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1132 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1133 lower_32_bits(context->mem_context.shared_mc_addr);
1134 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1135 upper_32_bits(context->mem_context.shared_mc_addr);
1136 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1139 int psp_ta_init_shared_buf(struct psp_context *psp,
1140 struct ta_mem_context *mem_ctx)
1143 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1144 * physical) for ta to host memory
1146 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1147 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1148 AMDGPU_GEM_DOMAIN_GTT,
1149 &mem_ctx->shared_bo,
1150 &mem_ctx->shared_mc_addr,
1151 &mem_ctx->shared_buf);
1154 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1156 uint32_t session_id)
1158 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1159 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1160 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1163 int psp_ta_invoke(struct psp_context *psp,
1165 struct ta_context *context)
1168 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1170 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1172 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1173 psp->fence_buf_mc_addr);
1175 context->resp_status = cmd->resp.status;
1177 release_psp_cmd_buf(psp);
1182 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1185 struct psp_gfx_cmd_resp *cmd;
1187 cmd = acquire_psp_cmd_buf(psp);
1189 psp_copy_fw(psp, context->bin_desc.start_addr,
1190 context->bin_desc.size_bytes);
1192 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1194 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1195 psp->fence_buf_mc_addr);
1197 context->resp_status = cmd->resp.status;
1200 context->session_id = cmd->resp.session_id;
1202 release_psp_cmd_buf(psp);
1207 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1209 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1212 int psp_xgmi_terminate(struct psp_context *psp)
1215 struct amdgpu_device *adev = psp->adev;
1217 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1218 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 4) ||
1219 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 2) &&
1220 adev->gmc.xgmi.connected_to_cpu))
1223 if (!psp->xgmi_context.context.initialized)
1226 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1228 psp->xgmi_context.context.initialized = false;
1233 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1235 struct ta_xgmi_shared_memory *xgmi_cmd;
1239 !psp->xgmi_context.context.bin_desc.size_bytes ||
1240 !psp->xgmi_context.context.bin_desc.start_addr)
1246 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1247 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1249 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1250 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1256 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1258 psp->xgmi_context.context.initialized = true;
1263 /* Initialize XGMI session */
1264 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1265 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1266 xgmi_cmd->flag_extend_link_record = set_extended_data;
1267 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1269 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1270 /* note down the capbility flag for XGMI TA */
1271 psp->xgmi_context.xgmi_ta_caps = xgmi_cmd->caps_flag;
1276 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1278 struct ta_xgmi_shared_memory *xgmi_cmd;
1281 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1282 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1284 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1286 /* Invoke xgmi ta to get hive id */
1287 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1291 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1296 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1298 struct ta_xgmi_shared_memory *xgmi_cmd;
1301 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1302 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1304 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1306 /* Invoke xgmi ta to get the node id */
1307 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1311 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1316 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1318 return (amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1319 IP_VERSION(13, 0, 2) &&
1320 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
1321 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) >=
1322 IP_VERSION(13, 0, 6);
1326 * Chips that support extended topology information require the driver to
1327 * reflect topology information in the opposite direction. This is
1328 * because the TA has already exceeded its link record limit and if the
1329 * TA holds bi-directional information, the driver would have to do
1330 * multiple fetches instead of just two.
1332 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1333 struct psp_xgmi_node_info node_info)
1335 struct amdgpu_device *mirror_adev;
1336 struct amdgpu_hive_info *hive;
1337 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1338 uint64_t dst_node_id = node_info.node_id;
1339 uint8_t dst_num_hops = node_info.num_hops;
1340 uint8_t dst_num_links = node_info.num_links;
1342 hive = amdgpu_get_xgmi_hive(psp->adev);
1343 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1344 struct psp_xgmi_topology_info *mirror_top_info;
1347 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1350 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1351 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1352 if (mirror_top_info->nodes[j].node_id != src_node_id)
1355 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1357 * prevent 0 num_links value re-reflection since reflection
1358 * criteria is based on num_hops (direct or indirect).
1362 mirror_top_info->nodes[j].num_links = dst_num_links;
1370 amdgpu_put_xgmi_hive(hive);
1373 int psp_xgmi_get_topology_info(struct psp_context *psp,
1375 struct psp_xgmi_topology_info *topology,
1376 bool get_extended_data)
1378 struct ta_xgmi_shared_memory *xgmi_cmd;
1379 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1380 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1384 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1387 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1388 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1389 xgmi_cmd->flag_extend_link_record = get_extended_data;
1391 /* Fill in the shared memory with topology information as input */
1392 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1393 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_TOPOLOGY_INFO;
1394 topology_info_input->num_nodes = number_devices;
1396 for (i = 0; i < topology_info_input->num_nodes; i++) {
1397 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1398 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1399 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1400 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1403 /* Invoke xgmi ta to get the topology information */
1404 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_TOPOLOGY_INFO);
1408 /* Read the output topology information from the shared memory */
1409 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1410 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1411 for (i = 0; i < topology->num_nodes; i++) {
1412 /* extended data will either be 0 or equal to non-extended data */
1413 if (topology_info_output->nodes[i].num_hops)
1414 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1416 /* non-extended data gets everything here so no need to update */
1417 if (!get_extended_data) {
1418 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1419 topology->nodes[i].is_sharing_enabled =
1420 topology_info_output->nodes[i].is_sharing_enabled;
1421 topology->nodes[i].sdma_engine =
1422 topology_info_output->nodes[i].sdma_engine;
1427 /* Invoke xgmi ta again to get the link information */
1428 if (psp_xgmi_peer_link_info_supported(psp)) {
1429 struct ta_xgmi_cmd_get_peer_link_info *link_info_output;
1430 struct ta_xgmi_cmd_get_extend_peer_link_info *link_extend_info_output;
1431 bool requires_reflection =
1432 (psp->xgmi_context.supports_extended_data &&
1433 get_extended_data) ||
1434 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1435 IP_VERSION(13, 0, 6);
1436 bool ta_port_num_support = amdgpu_sriov_vf(psp->adev) ? 0 :
1437 psp->xgmi_context.xgmi_ta_caps & EXTEND_PEER_LINK_INFO_CMD_FLAG;
1439 /* popluate the shared output buffer rather than the cmd input buffer
1440 * with node_ids as the input for GET_PEER_LINKS command execution.
1441 * This is required for GET_PEER_LINKS per xgmi ta implementation.
1442 * The same requirement for GET_EXTEND_PEER_LINKS command.
1444 if (ta_port_num_support) {
1445 link_extend_info_output = &xgmi_cmd->xgmi_out_message.get_extend_link_info;
1447 for (i = 0; i < topology->num_nodes; i++)
1448 link_extend_info_output->nodes[i].node_id = topology->nodes[i].node_id;
1450 link_extend_info_output->num_nodes = topology->num_nodes;
1451 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_EXTEND_PEER_LINKS;
1453 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1455 for (i = 0; i < topology->num_nodes; i++)
1456 link_info_output->nodes[i].node_id = topology->nodes[i].node_id;
1458 link_info_output->num_nodes = topology->num_nodes;
1459 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1462 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1466 for (i = 0; i < topology->num_nodes; i++) {
1467 uint8_t node_num_links = ta_port_num_support ?
1468 link_extend_info_output->nodes[i].num_links : link_info_output->nodes[i].num_links;
1469 /* accumulate num_links on extended data */
1470 if (get_extended_data) {
1471 topology->nodes[i].num_links = topology->nodes[i].num_links + node_num_links;
1473 topology->nodes[i].num_links = (requires_reflection && topology->nodes[i].num_links) ?
1474 topology->nodes[i].num_links : node_num_links;
1476 /* popluate the connected port num info if supported and available */
1477 if (ta_port_num_support && topology->nodes[i].num_links) {
1478 memcpy(topology->nodes[i].port_num, link_extend_info_output->nodes[i].port_num,
1479 sizeof(struct xgmi_connected_port_num) * TA_XGMI__MAX_PORT_NUM);
1482 /* reflect the topology information for bi-directionality */
1483 if (requires_reflection && topology->nodes[i].num_hops)
1484 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1491 int psp_xgmi_set_topology_info(struct psp_context *psp,
1493 struct psp_xgmi_topology_info *topology)
1495 struct ta_xgmi_shared_memory *xgmi_cmd;
1496 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1499 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1502 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1503 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1505 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1506 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1507 topology_info_input->num_nodes = number_devices;
1509 for (i = 0; i < topology_info_input->num_nodes; i++) {
1510 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1511 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1512 topology_info_input->nodes[i].is_sharing_enabled = 1;
1513 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1516 /* Invoke xgmi ta to set topology information */
1517 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1521 static void psp_ras_ta_check_status(struct psp_context *psp)
1523 struct ta_ras_shared_memory *ras_cmd =
1524 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1526 switch (ras_cmd->ras_status) {
1527 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1528 dev_warn(psp->adev->dev,
1529 "RAS WARNING: cmd failed due to unsupported ip\n");
1531 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1532 dev_warn(psp->adev->dev,
1533 "RAS WARNING: cmd failed due to unsupported error injection\n");
1535 case TA_RAS_STATUS__SUCCESS:
1537 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1538 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1539 dev_warn(psp->adev->dev,
1540 "RAS WARNING: Inject error to critical region is not allowed\n");
1543 dev_warn(psp->adev->dev,
1544 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1549 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1551 struct ta_ras_shared_memory *ras_cmd;
1554 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1557 * TODO: bypass the loading in sriov for now
1559 if (amdgpu_sriov_vf(psp->adev))
1562 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1564 if (amdgpu_ras_intr_triggered())
1567 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1568 DRM_WARN("RAS: Unsupported Interface");
1573 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1574 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1576 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1577 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1578 dev_warn(psp->adev->dev,
1579 "RAS internal register access blocked\n");
1581 psp_ras_ta_check_status(psp);
1587 int psp_ras_enable_features(struct psp_context *psp,
1588 union ta_ras_cmd_input *info, bool enable)
1590 struct ta_ras_shared_memory *ras_cmd;
1593 if (!psp->ras_context.context.initialized)
1596 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1597 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1600 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1602 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1604 ras_cmd->ras_in_message = *info;
1606 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1613 int psp_ras_terminate(struct psp_context *psp)
1618 * TODO: bypass the terminate in sriov for now
1620 if (amdgpu_sriov_vf(psp->adev))
1623 if (!psp->ras_context.context.initialized)
1626 ret = psp_ta_unload(psp, &psp->ras_context.context);
1628 psp->ras_context.context.initialized = false;
1633 int psp_ras_initialize(struct psp_context *psp)
1636 uint32_t boot_cfg = 0xFF;
1637 struct amdgpu_device *adev = psp->adev;
1638 struct ta_ras_shared_memory *ras_cmd;
1641 * TODO: bypass the initialize in sriov for now
1643 if (amdgpu_sriov_vf(adev))
1646 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1647 !adev->psp.ras_context.context.bin_desc.start_addr) {
1648 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1652 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1653 /* query GECC enablement status from boot config
1654 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1656 ret = psp_boot_config_get(adev, &boot_cfg);
1658 dev_warn(adev->dev, "PSP get boot config failed\n");
1660 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1662 dev_info(adev->dev, "GECC is disabled\n");
1664 /* disable GECC in next boot cycle if ras is
1665 * disabled by module parameter amdgpu_ras_enable
1666 * and/or amdgpu_ras_mask, or boot_config_get call
1669 ret = psp_boot_config_set(adev, 0);
1671 dev_warn(adev->dev, "PSP set boot config failed\n");
1673 dev_warn(adev->dev, "GECC will be disabled in next boot cycle if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1676 if (boot_cfg == 1) {
1677 dev_info(adev->dev, "GECC is enabled\n");
1679 /* enable GECC in next boot cycle if it is disabled
1680 * in boot config, or force enable GECC if failed to
1681 * get boot configuration
1683 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1685 dev_warn(adev->dev, "PSP set boot config failed\n");
1687 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1692 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1693 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1695 if (!psp->ras_context.context.mem_context.shared_buf) {
1696 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1701 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1702 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1704 if (amdgpu_ras_is_poison_mode_supported(adev))
1705 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1706 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu)
1707 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1708 ras_cmd->ras_in_message.init_flags.xcc_mask =
1710 ras_cmd->ras_in_message.init_flags.channel_dis_num = hweight32(adev->gmc.m_half_use) * 2;
1712 ret = psp_ta_load(psp, &psp->ras_context.context);
1714 if (!ret && !ras_cmd->ras_status)
1715 psp->ras_context.context.initialized = true;
1717 if (ras_cmd->ras_status)
1718 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1720 /* fail to load RAS TA */
1721 psp->ras_context.context.initialized = false;
1727 int psp_ras_trigger_error(struct psp_context *psp,
1728 struct ta_ras_trigger_error_input *info, uint32_t instance_mask)
1730 struct ta_ras_shared_memory *ras_cmd;
1731 struct amdgpu_device *adev = psp->adev;
1735 if (!psp->ras_context.context.initialized)
1738 switch (info->block_id) {
1739 case TA_RAS_BLOCK__GFX:
1740 dev_mask = GET_MASK(GC, instance_mask);
1742 case TA_RAS_BLOCK__SDMA:
1743 dev_mask = GET_MASK(SDMA0, instance_mask);
1745 case TA_RAS_BLOCK__VCN:
1746 case TA_RAS_BLOCK__JPEG:
1747 dev_mask = GET_MASK(VCN, instance_mask);
1750 dev_mask = instance_mask;
1754 /* reuse sub_block_index for backward compatibility */
1755 dev_mask <<= AMDGPU_RAS_INST_SHIFT;
1756 dev_mask &= AMDGPU_RAS_INST_MASK;
1757 info->sub_block_index |= dev_mask;
1759 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1760 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1762 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1763 ras_cmd->ras_in_message.trigger_error = *info;
1765 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1769 /* If err_event_athub occurs error inject was successful, however
1770 * return status from TA is no long reliable
1772 if (amdgpu_ras_intr_triggered())
1775 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1777 else if (ras_cmd->ras_status)
1785 static int psp_hdcp_initialize(struct psp_context *psp)
1790 * TODO: bypass the initialize in sriov for now
1792 if (amdgpu_sriov_vf(psp->adev))
1795 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1796 !psp->hdcp_context.context.bin_desc.start_addr) {
1797 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1801 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1802 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1804 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1805 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1810 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1812 psp->hdcp_context.context.initialized = true;
1813 mutex_init(&psp->hdcp_context.mutex);
1819 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1822 * TODO: bypass the loading in sriov for now
1824 if (amdgpu_sriov_vf(psp->adev))
1827 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1830 static int psp_hdcp_terminate(struct psp_context *psp)
1835 * TODO: bypass the terminate in sriov for now
1837 if (amdgpu_sriov_vf(psp->adev))
1840 if (!psp->hdcp_context.context.initialized)
1843 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1845 psp->hdcp_context.context.initialized = false;
1852 static int psp_dtm_initialize(struct psp_context *psp)
1857 * TODO: bypass the initialize in sriov for now
1859 if (amdgpu_sriov_vf(psp->adev))
1862 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1863 !psp->dtm_context.context.bin_desc.start_addr) {
1864 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1868 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1869 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1871 if (!psp->dtm_context.context.mem_context.shared_buf) {
1872 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1877 ret = psp_ta_load(psp, &psp->dtm_context.context);
1879 psp->dtm_context.context.initialized = true;
1880 mutex_init(&psp->dtm_context.mutex);
1886 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1889 * TODO: bypass the loading in sriov for now
1891 if (amdgpu_sriov_vf(psp->adev))
1894 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1897 static int psp_dtm_terminate(struct psp_context *psp)
1902 * TODO: bypass the terminate in sriov for now
1904 if (amdgpu_sriov_vf(psp->adev))
1907 if (!psp->dtm_context.context.initialized)
1910 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1912 psp->dtm_context.context.initialized = false;
1919 static int psp_rap_initialize(struct psp_context *psp)
1922 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1925 * TODO: bypass the initialize in sriov for now
1927 if (amdgpu_sriov_vf(psp->adev))
1930 if (!psp->rap_context.context.bin_desc.size_bytes ||
1931 !psp->rap_context.context.bin_desc.start_addr) {
1932 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1936 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1937 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1939 if (!psp->rap_context.context.mem_context.shared_buf) {
1940 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1945 ret = psp_ta_load(psp, &psp->rap_context.context);
1947 psp->rap_context.context.initialized = true;
1948 mutex_init(&psp->rap_context.mutex);
1952 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1953 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1954 psp_rap_terminate(psp);
1955 /* free rap shared memory */
1956 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1958 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1967 static int psp_rap_terminate(struct psp_context *psp)
1971 if (!psp->rap_context.context.initialized)
1974 ret = psp_ta_unload(psp, &psp->rap_context.context);
1976 psp->rap_context.context.initialized = false;
1981 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1983 struct ta_rap_shared_memory *rap_cmd;
1986 if (!psp->rap_context.context.initialized)
1989 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1990 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1993 mutex_lock(&psp->rap_context.mutex);
1995 rap_cmd = (struct ta_rap_shared_memory *)
1996 psp->rap_context.context.mem_context.shared_buf;
1997 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1999 rap_cmd->cmd_id = ta_cmd_id;
2000 rap_cmd->validation_method_id = METHOD_A;
2002 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
2007 *status = rap_cmd->rap_status;
2010 mutex_unlock(&psp->rap_context.mutex);
2016 /* securedisplay start */
2017 static int psp_securedisplay_initialize(struct psp_context *psp)
2020 struct ta_securedisplay_cmd *securedisplay_cmd;
2023 * TODO: bypass the initialize in sriov for now
2025 if (amdgpu_sriov_vf(psp->adev))
2028 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
2029 !psp->securedisplay_context.context.bin_desc.start_addr) {
2030 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
2034 psp->securedisplay_context.context.mem_context.shared_mem_size =
2035 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
2036 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
2038 if (!psp->securedisplay_context.context.initialized) {
2039 ret = psp_ta_init_shared_buf(psp,
2040 &psp->securedisplay_context.context.mem_context);
2045 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
2047 psp->securedisplay_context.context.initialized = true;
2048 mutex_init(&psp->securedisplay_context.mutex);
2052 mutex_lock(&psp->securedisplay_context.mutex);
2054 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
2055 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2057 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2059 mutex_unlock(&psp->securedisplay_context.mutex);
2062 psp_securedisplay_terminate(psp);
2063 /* free securedisplay shared memory */
2064 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
2065 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
2069 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
2070 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
2071 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
2072 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
2073 /* don't try again */
2074 psp->securedisplay_context.context.bin_desc.size_bytes = 0;
2080 static int psp_securedisplay_terminate(struct psp_context *psp)
2085 * TODO:bypass the terminate in sriov for now
2087 if (amdgpu_sriov_vf(psp->adev))
2090 if (!psp->securedisplay_context.context.initialized)
2093 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2095 psp->securedisplay_context.context.initialized = false;
2100 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2104 if (!psp->securedisplay_context.context.initialized)
2107 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2108 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2111 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2115 /* SECUREDISPLAY end */
2117 int amdgpu_psp_wait_for_bootloader(struct amdgpu_device *adev)
2119 struct psp_context *psp = &adev->psp;
2122 if (!amdgpu_sriov_vf(adev) && psp->funcs && psp->funcs->wait_for_bootloader != NULL)
2123 ret = psp->funcs->wait_for_bootloader(psp);
2128 int amdgpu_psp_query_boot_status(struct amdgpu_device *adev)
2130 struct psp_context *psp = &adev->psp;
2133 if (amdgpu_sriov_vf(adev) || (adev->flags & AMD_IS_APU))
2137 psp->funcs->query_boot_status)
2138 ret = psp->funcs->query_boot_status(psp);
2143 static int psp_hw_start(struct psp_context *psp)
2145 struct amdgpu_device *adev = psp->adev;
2148 if (!amdgpu_sriov_vf(adev)) {
2149 if ((is_psp_fw_valid(psp->kdb)) &&
2150 (psp->funcs->bootloader_load_kdb != NULL)) {
2151 ret = psp_bootloader_load_kdb(psp);
2153 DRM_ERROR("PSP load kdb failed!\n");
2158 if ((is_psp_fw_valid(psp->spl)) &&
2159 (psp->funcs->bootloader_load_spl != NULL)) {
2160 ret = psp_bootloader_load_spl(psp);
2162 DRM_ERROR("PSP load spl failed!\n");
2167 if ((is_psp_fw_valid(psp->sys)) &&
2168 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2169 ret = psp_bootloader_load_sysdrv(psp);
2171 DRM_ERROR("PSP load sys drv failed!\n");
2176 if ((is_psp_fw_valid(psp->soc_drv)) &&
2177 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2178 ret = psp_bootloader_load_soc_drv(psp);
2180 DRM_ERROR("PSP load soc drv failed!\n");
2185 if ((is_psp_fw_valid(psp->intf_drv)) &&
2186 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2187 ret = psp_bootloader_load_intf_drv(psp);
2189 DRM_ERROR("PSP load intf drv failed!\n");
2194 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2195 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2196 ret = psp_bootloader_load_dbg_drv(psp);
2198 DRM_ERROR("PSP load dbg drv failed!\n");
2203 if ((is_psp_fw_valid(psp->ras_drv)) &&
2204 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2205 ret = psp_bootloader_load_ras_drv(psp);
2207 DRM_ERROR("PSP load ras_drv failed!\n");
2212 if ((is_psp_fw_valid(psp->sos)) &&
2213 (psp->funcs->bootloader_load_sos != NULL)) {
2214 ret = psp_bootloader_load_sos(psp);
2216 DRM_ERROR("PSP load sos failed!\n");
2222 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2224 DRM_ERROR("PSP create ring failed!\n");
2228 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2231 if (!psp_boottime_tmr(psp)) {
2232 ret = psp_tmr_init(psp);
2234 DRM_ERROR("PSP tmr init failed!\n");
2241 * For ASICs with DF Cstate management centralized
2242 * to PMFW, TMR setup should be performed after PMFW
2243 * loaded and before other non-psp firmware loaded.
2245 if (psp->pmfw_centralized_cstate_management) {
2246 ret = psp_load_smu_fw(psp);
2251 ret = psp_tmr_load(psp);
2253 DRM_ERROR("PSP load tmr failed!\n");
2260 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2261 enum psp_gfx_fw_type *type)
2263 switch (ucode->ucode_id) {
2264 case AMDGPU_UCODE_ID_CAP:
2265 *type = GFX_FW_TYPE_CAP;
2267 case AMDGPU_UCODE_ID_SDMA0:
2268 *type = GFX_FW_TYPE_SDMA0;
2270 case AMDGPU_UCODE_ID_SDMA1:
2271 *type = GFX_FW_TYPE_SDMA1;
2273 case AMDGPU_UCODE_ID_SDMA2:
2274 *type = GFX_FW_TYPE_SDMA2;
2276 case AMDGPU_UCODE_ID_SDMA3:
2277 *type = GFX_FW_TYPE_SDMA3;
2279 case AMDGPU_UCODE_ID_SDMA4:
2280 *type = GFX_FW_TYPE_SDMA4;
2282 case AMDGPU_UCODE_ID_SDMA5:
2283 *type = GFX_FW_TYPE_SDMA5;
2285 case AMDGPU_UCODE_ID_SDMA6:
2286 *type = GFX_FW_TYPE_SDMA6;
2288 case AMDGPU_UCODE_ID_SDMA7:
2289 *type = GFX_FW_TYPE_SDMA7;
2291 case AMDGPU_UCODE_ID_CP_MES:
2292 *type = GFX_FW_TYPE_CP_MES;
2294 case AMDGPU_UCODE_ID_CP_MES_DATA:
2295 *type = GFX_FW_TYPE_MES_STACK;
2297 case AMDGPU_UCODE_ID_CP_MES1:
2298 *type = GFX_FW_TYPE_CP_MES_KIQ;
2300 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2301 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2303 case AMDGPU_UCODE_ID_CP_CE:
2304 *type = GFX_FW_TYPE_CP_CE;
2306 case AMDGPU_UCODE_ID_CP_PFP:
2307 *type = GFX_FW_TYPE_CP_PFP;
2309 case AMDGPU_UCODE_ID_CP_ME:
2310 *type = GFX_FW_TYPE_CP_ME;
2312 case AMDGPU_UCODE_ID_CP_MEC1:
2313 *type = GFX_FW_TYPE_CP_MEC;
2315 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2316 *type = GFX_FW_TYPE_CP_MEC_ME1;
2318 case AMDGPU_UCODE_ID_CP_MEC2:
2319 *type = GFX_FW_TYPE_CP_MEC;
2321 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2322 *type = GFX_FW_TYPE_CP_MEC_ME2;
2324 case AMDGPU_UCODE_ID_RLC_P:
2325 *type = GFX_FW_TYPE_RLC_P;
2327 case AMDGPU_UCODE_ID_RLC_V:
2328 *type = GFX_FW_TYPE_RLC_V;
2330 case AMDGPU_UCODE_ID_RLC_G:
2331 *type = GFX_FW_TYPE_RLC_G;
2333 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2334 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2336 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2337 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2339 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2340 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2342 case AMDGPU_UCODE_ID_RLC_IRAM:
2343 *type = GFX_FW_TYPE_RLC_IRAM;
2345 case AMDGPU_UCODE_ID_RLC_DRAM:
2346 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2348 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2349 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2351 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2352 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2354 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2355 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2357 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2358 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2360 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2361 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2363 case AMDGPU_UCODE_ID_SMC:
2364 *type = GFX_FW_TYPE_SMU;
2366 case AMDGPU_UCODE_ID_PPTABLE:
2367 *type = GFX_FW_TYPE_PPTABLE;
2369 case AMDGPU_UCODE_ID_UVD:
2370 *type = GFX_FW_TYPE_UVD;
2372 case AMDGPU_UCODE_ID_UVD1:
2373 *type = GFX_FW_TYPE_UVD1;
2375 case AMDGPU_UCODE_ID_VCE:
2376 *type = GFX_FW_TYPE_VCE;
2378 case AMDGPU_UCODE_ID_VCN:
2379 *type = GFX_FW_TYPE_VCN;
2381 case AMDGPU_UCODE_ID_VCN1:
2382 *type = GFX_FW_TYPE_VCN1;
2384 case AMDGPU_UCODE_ID_DMCU_ERAM:
2385 *type = GFX_FW_TYPE_DMCU_ERAM;
2387 case AMDGPU_UCODE_ID_DMCU_INTV:
2388 *type = GFX_FW_TYPE_DMCU_ISR;
2390 case AMDGPU_UCODE_ID_VCN0_RAM:
2391 *type = GFX_FW_TYPE_VCN0_RAM;
2393 case AMDGPU_UCODE_ID_VCN1_RAM:
2394 *type = GFX_FW_TYPE_VCN1_RAM;
2396 case AMDGPU_UCODE_ID_DMCUB:
2397 *type = GFX_FW_TYPE_DMUB;
2399 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2400 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2402 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2403 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2405 case AMDGPU_UCODE_ID_IMU_I:
2406 *type = GFX_FW_TYPE_IMU_I;
2408 case AMDGPU_UCODE_ID_IMU_D:
2409 *type = GFX_FW_TYPE_IMU_D;
2411 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2412 *type = GFX_FW_TYPE_RS64_PFP;
2414 case AMDGPU_UCODE_ID_CP_RS64_ME:
2415 *type = GFX_FW_TYPE_RS64_ME;
2417 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2418 *type = GFX_FW_TYPE_RS64_MEC;
2420 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2421 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2423 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2424 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2426 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2427 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2429 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2430 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2432 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2433 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2435 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2436 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2438 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2439 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2441 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2442 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2444 case AMDGPU_UCODE_ID_VPE_CTX:
2445 *type = GFX_FW_TYPE_VPEC_FW1;
2447 case AMDGPU_UCODE_ID_VPE_CTL:
2448 *type = GFX_FW_TYPE_VPEC_FW2;
2450 case AMDGPU_UCODE_ID_VPE:
2451 *type = GFX_FW_TYPE_VPE;
2453 case AMDGPU_UCODE_ID_UMSCH_MM_UCODE:
2454 *type = GFX_FW_TYPE_UMSCH_UCODE;
2456 case AMDGPU_UCODE_ID_UMSCH_MM_DATA:
2457 *type = GFX_FW_TYPE_UMSCH_DATA;
2459 case AMDGPU_UCODE_ID_UMSCH_MM_CMD_BUFFER:
2460 *type = GFX_FW_TYPE_UMSCH_CMD_BUFFER;
2462 case AMDGPU_UCODE_ID_P2S_TABLE:
2463 *type = GFX_FW_TYPE_P2S_TABLE;
2465 case AMDGPU_UCODE_ID_MAXIMUM:
2473 static void psp_print_fw_hdr(struct psp_context *psp,
2474 struct amdgpu_firmware_info *ucode)
2476 struct amdgpu_device *adev = psp->adev;
2477 struct common_firmware_header *hdr;
2479 switch (ucode->ucode_id) {
2480 case AMDGPU_UCODE_ID_SDMA0:
2481 case AMDGPU_UCODE_ID_SDMA1:
2482 case AMDGPU_UCODE_ID_SDMA2:
2483 case AMDGPU_UCODE_ID_SDMA3:
2484 case AMDGPU_UCODE_ID_SDMA4:
2485 case AMDGPU_UCODE_ID_SDMA5:
2486 case AMDGPU_UCODE_ID_SDMA6:
2487 case AMDGPU_UCODE_ID_SDMA7:
2488 hdr = (struct common_firmware_header *)
2489 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2490 amdgpu_ucode_print_sdma_hdr(hdr);
2492 case AMDGPU_UCODE_ID_CP_CE:
2493 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2494 amdgpu_ucode_print_gfx_hdr(hdr);
2496 case AMDGPU_UCODE_ID_CP_PFP:
2497 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2498 amdgpu_ucode_print_gfx_hdr(hdr);
2500 case AMDGPU_UCODE_ID_CP_ME:
2501 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2502 amdgpu_ucode_print_gfx_hdr(hdr);
2504 case AMDGPU_UCODE_ID_CP_MEC1:
2505 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2506 amdgpu_ucode_print_gfx_hdr(hdr);
2508 case AMDGPU_UCODE_ID_RLC_G:
2509 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2510 amdgpu_ucode_print_rlc_hdr(hdr);
2512 case AMDGPU_UCODE_ID_SMC:
2513 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2514 amdgpu_ucode_print_smc_hdr(hdr);
2521 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2522 struct psp_gfx_cmd_resp *cmd)
2525 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2527 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2528 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2529 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2530 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2532 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2534 DRM_ERROR("Unknown firmware type\n");
2539 int psp_execute_ip_fw_load(struct psp_context *psp,
2540 struct amdgpu_firmware_info *ucode)
2543 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2545 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2547 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2548 psp->fence_buf_mc_addr);
2551 release_psp_cmd_buf(psp);
2556 static int psp_load_p2s_table(struct psp_context *psp)
2559 struct amdgpu_device *adev = psp->adev;
2560 struct amdgpu_firmware_info *ucode =
2561 &adev->firmware.ucode[AMDGPU_UCODE_ID_P2S_TABLE];
2563 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2566 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 6)) {
2567 uint32_t supp_vers = adev->flags & AMD_IS_APU ? 0x0036013D :
2569 if (psp->sos.fw_version < supp_vers)
2573 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2576 ret = psp_execute_ip_fw_load(psp, ucode);
2581 static int psp_load_smu_fw(struct psp_context *psp)
2584 struct amdgpu_device *adev = psp->adev;
2585 struct amdgpu_firmware_info *ucode =
2586 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2587 struct amdgpu_ras *ras = psp->ras_context.ras;
2590 * Skip SMU FW reloading in case of using BACO for runpm only,
2591 * as SMU is always alive.
2593 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2596 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2599 if ((amdgpu_in_reset(adev) && ras && adev->ras_enabled &&
2600 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 4) ||
2601 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 2)))) {
2602 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2604 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2607 ret = psp_execute_ip_fw_load(psp, ucode);
2610 DRM_ERROR("PSP load smu failed!\n");
2615 static bool fw_load_skip_check(struct psp_context *psp,
2616 struct amdgpu_firmware_info *ucode)
2618 if (!ucode->fw || !ucode->ucode_size)
2621 if (ucode->ucode_id == AMDGPU_UCODE_ID_P2S_TABLE)
2624 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2625 (psp_smu_reload_quirk(psp) ||
2626 psp->autoload_supported ||
2627 psp->pmfw_centralized_cstate_management))
2630 if (amdgpu_sriov_vf(psp->adev) &&
2631 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2634 if (psp->autoload_supported &&
2635 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2636 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2637 /* skip mec JT when autoload is enabled */
2643 int psp_load_fw_list(struct psp_context *psp,
2644 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2647 struct amdgpu_firmware_info *ucode;
2649 for (i = 0; i < ucode_count; ++i) {
2650 ucode = ucode_list[i];
2651 psp_print_fw_hdr(psp, ucode);
2652 ret = psp_execute_ip_fw_load(psp, ucode);
2659 static int psp_load_non_psp_fw(struct psp_context *psp)
2662 struct amdgpu_firmware_info *ucode;
2663 struct amdgpu_device *adev = psp->adev;
2665 if (psp->autoload_supported &&
2666 !psp->pmfw_centralized_cstate_management) {
2667 ret = psp_load_smu_fw(psp);
2672 /* Load P2S table first if it's available */
2673 psp_load_p2s_table(psp);
2675 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2676 ucode = &adev->firmware.ucode[i];
2678 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2679 !fw_load_skip_check(psp, ucode)) {
2680 ret = psp_load_smu_fw(psp);
2686 if (fw_load_skip_check(psp, ucode))
2689 if (psp->autoload_supported &&
2690 (amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2691 IP_VERSION(11, 0, 7) ||
2692 amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2693 IP_VERSION(11, 0, 11) ||
2694 amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2695 IP_VERSION(11, 0, 12)) &&
2696 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2697 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2698 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2699 /* PSP only receive one SDMA fw for sienna_cichlid,
2700 * as all four sdma fw are same
2704 psp_print_fw_hdr(psp, ucode);
2706 ret = psp_execute_ip_fw_load(psp, ucode);
2710 /* Start rlc autoload after psp recieved all the gfx firmware */
2711 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2712 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2713 ret = psp_rlc_autoload_start(psp);
2715 DRM_ERROR("Failed to start rlc autoload\n");
2724 static int psp_load_fw(struct amdgpu_device *adev)
2727 struct psp_context *psp = &adev->psp;
2729 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2730 /* should not destroy ring, only stop */
2731 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2733 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2735 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2737 DRM_ERROR("PSP ring init failed!\n");
2742 ret = psp_hw_start(psp);
2746 ret = psp_load_non_psp_fw(psp);
2750 ret = psp_asd_initialize(psp);
2752 DRM_ERROR("PSP load asd failed!\n");
2756 ret = psp_rl_load(adev);
2758 DRM_ERROR("PSP load RL failed!\n");
2762 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2763 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2764 ret = psp_xgmi_initialize(psp, false, true);
2765 /* Warning the XGMI seesion initialize failure
2766 * Instead of stop driver initialization
2769 dev_err(psp->adev->dev,
2770 "XGMI: Failed to initialize XGMI session\n");
2775 ret = psp_ras_initialize(psp);
2777 dev_err(psp->adev->dev,
2778 "RAS: Failed to initialize RAS\n");
2780 ret = psp_hdcp_initialize(psp);
2782 dev_err(psp->adev->dev,
2783 "HDCP: Failed to initialize HDCP\n");
2785 ret = psp_dtm_initialize(psp);
2787 dev_err(psp->adev->dev,
2788 "DTM: Failed to initialize DTM\n");
2790 ret = psp_rap_initialize(psp);
2792 dev_err(psp->adev->dev,
2793 "RAP: Failed to initialize RAP\n");
2795 ret = psp_securedisplay_initialize(psp);
2797 dev_err(psp->adev->dev,
2798 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2804 psp_free_shared_bufs(psp);
2807 * all cleanup jobs (xgmi terminate, ras terminate,
2808 * ring destroy, cmd/fence/fw buffers destory,
2809 * psp->cmd destory) are delayed to psp_hw_fini
2811 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2815 static int psp_hw_init(void *handle)
2818 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2820 mutex_lock(&adev->firmware.mutex);
2822 * This sequence is just used on hw_init only once, no need on
2825 ret = amdgpu_ucode_init_bo(adev);
2829 ret = psp_load_fw(adev);
2831 DRM_ERROR("PSP firmware loading failed\n");
2835 mutex_unlock(&adev->firmware.mutex);
2839 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2840 mutex_unlock(&adev->firmware.mutex);
2844 static int psp_hw_fini(void *handle)
2846 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2847 struct psp_context *psp = &adev->psp;
2850 psp_ras_terminate(psp);
2851 psp_securedisplay_terminate(psp);
2852 psp_rap_terminate(psp);
2853 psp_dtm_terminate(psp);
2854 psp_hdcp_terminate(psp);
2856 if (adev->gmc.xgmi.num_physical_nodes > 1)
2857 psp_xgmi_terminate(psp);
2860 psp_asd_terminate(psp);
2861 psp_tmr_terminate(psp);
2863 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2868 static int psp_suspend(void *handle)
2871 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2872 struct psp_context *psp = &adev->psp;
2874 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2875 psp->xgmi_context.context.initialized) {
2876 ret = psp_xgmi_terminate(psp);
2878 DRM_ERROR("Failed to terminate xgmi ta\n");
2884 ret = psp_ras_terminate(psp);
2886 DRM_ERROR("Failed to terminate ras ta\n");
2889 ret = psp_hdcp_terminate(psp);
2891 DRM_ERROR("Failed to terminate hdcp ta\n");
2894 ret = psp_dtm_terminate(psp);
2896 DRM_ERROR("Failed to terminate dtm ta\n");
2899 ret = psp_rap_terminate(psp);
2901 DRM_ERROR("Failed to terminate rap ta\n");
2904 ret = psp_securedisplay_terminate(psp);
2906 DRM_ERROR("Failed to terminate securedisplay ta\n");
2911 ret = psp_asd_terminate(psp);
2913 DRM_ERROR("Failed to terminate asd\n");
2917 ret = psp_tmr_terminate(psp);
2919 DRM_ERROR("Failed to terminate tmr\n");
2923 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2925 DRM_ERROR("PSP ring stop failed\n");
2931 static int psp_resume(void *handle)
2934 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2935 struct psp_context *psp = &adev->psp;
2937 DRM_INFO("PSP is resuming...\n");
2939 if (psp->mem_train_ctx.enable_mem_training) {
2940 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2942 DRM_ERROR("Failed to process memory training!\n");
2947 mutex_lock(&adev->firmware.mutex);
2949 ret = psp_hw_start(psp);
2953 ret = psp_load_non_psp_fw(psp);
2957 ret = psp_asd_initialize(psp);
2959 DRM_ERROR("PSP load asd failed!\n");
2963 ret = psp_rl_load(adev);
2965 dev_err(adev->dev, "PSP load RL failed!\n");
2969 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2970 ret = psp_xgmi_initialize(psp, false, true);
2971 /* Warning the XGMI seesion initialize failure
2972 * Instead of stop driver initialization
2975 dev_err(psp->adev->dev,
2976 "XGMI: Failed to initialize XGMI session\n");
2980 ret = psp_ras_initialize(psp);
2982 dev_err(psp->adev->dev,
2983 "RAS: Failed to initialize RAS\n");
2985 ret = psp_hdcp_initialize(psp);
2987 dev_err(psp->adev->dev,
2988 "HDCP: Failed to initialize HDCP\n");
2990 ret = psp_dtm_initialize(psp);
2992 dev_err(psp->adev->dev,
2993 "DTM: Failed to initialize DTM\n");
2995 ret = psp_rap_initialize(psp);
2997 dev_err(psp->adev->dev,
2998 "RAP: Failed to initialize RAP\n");
3000 ret = psp_securedisplay_initialize(psp);
3002 dev_err(psp->adev->dev,
3003 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
3006 mutex_unlock(&adev->firmware.mutex);
3011 DRM_ERROR("PSP resume failed\n");
3012 mutex_unlock(&adev->firmware.mutex);
3016 int psp_gpu_reset(struct amdgpu_device *adev)
3020 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
3023 mutex_lock(&adev->psp.mutex);
3024 ret = psp_mode1_reset(&adev->psp);
3025 mutex_unlock(&adev->psp.mutex);
3030 int psp_rlc_autoload_start(struct psp_context *psp)
3033 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
3035 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
3037 ret = psp_cmd_submit_buf(psp, NULL, cmd,
3038 psp->fence_buf_mc_addr);
3040 release_psp_cmd_buf(psp);
3045 int psp_ring_cmd_submit(struct psp_context *psp,
3046 uint64_t cmd_buf_mc_addr,
3047 uint64_t fence_mc_addr,
3050 unsigned int psp_write_ptr_reg = 0;
3051 struct psp_gfx_rb_frame *write_frame;
3052 struct psp_ring *ring = &psp->km_ring;
3053 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
3054 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
3055 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
3056 struct amdgpu_device *adev = psp->adev;
3057 uint32_t ring_size_dw = ring->ring_size / 4;
3058 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
3060 /* KM (GPCOM) prepare write pointer */
3061 psp_write_ptr_reg = psp_ring_get_wptr(psp);
3063 /* Update KM RB frame pointer to new frame */
3064 /* write_frame ptr increments by size of rb_frame in bytes */
3065 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
3066 if ((psp_write_ptr_reg % ring_size_dw) == 0)
3067 write_frame = ring_buffer_start;
3069 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
3070 /* Check invalid write_frame ptr address */
3071 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
3072 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
3073 ring_buffer_start, ring_buffer_end, write_frame);
3074 DRM_ERROR("write_frame is pointing to address out of bounds\n");
3078 /* Initialize KM RB frame */
3079 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
3081 /* Update KM RB frame */
3082 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
3083 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
3084 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
3085 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
3086 write_frame->fence_value = index;
3087 amdgpu_device_flush_hdp(adev, NULL);
3089 /* Update the write Pointer in DWORDs */
3090 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
3091 psp_ring_set_wptr(psp, psp_write_ptr_reg);
3095 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
3097 struct amdgpu_device *adev = psp->adev;
3098 char fw_name[PSP_FW_NAME_LEN];
3099 const struct psp_firmware_header_v1_0 *asd_hdr;
3102 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
3103 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
3107 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
3108 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
3109 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
3110 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
3111 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
3112 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
3115 amdgpu_ucode_release(&adev->psp.asd_fw);
3119 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
3121 struct amdgpu_device *adev = psp->adev;
3122 char fw_name[PSP_FW_NAME_LEN];
3123 const struct psp_firmware_header_v1_0 *toc_hdr;
3126 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
3127 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
3131 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
3132 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
3133 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
3134 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
3135 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
3136 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
3139 amdgpu_ucode_release(&adev->psp.toc_fw);
3143 static int parse_sos_bin_descriptor(struct psp_context *psp,
3144 const struct psp_fw_bin_desc *desc,
3145 const struct psp_firmware_header_v2_0 *sos_hdr)
3147 uint8_t *ucode_start_addr = NULL;
3149 if (!psp || !desc || !sos_hdr)
3152 ucode_start_addr = (uint8_t *)sos_hdr +
3153 le32_to_cpu(desc->offset_bytes) +
3154 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3156 switch (desc->fw_type) {
3157 case PSP_FW_TYPE_PSP_SOS:
3158 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3159 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3160 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3161 psp->sos.start_addr = ucode_start_addr;
3163 case PSP_FW_TYPE_PSP_SYS_DRV:
3164 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3165 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3166 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3167 psp->sys.start_addr = ucode_start_addr;
3169 case PSP_FW_TYPE_PSP_KDB:
3170 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3171 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3172 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3173 psp->kdb.start_addr = ucode_start_addr;
3175 case PSP_FW_TYPE_PSP_TOC:
3176 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3177 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3178 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3179 psp->toc.start_addr = ucode_start_addr;
3181 case PSP_FW_TYPE_PSP_SPL:
3182 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3183 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3184 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3185 psp->spl.start_addr = ucode_start_addr;
3187 case PSP_FW_TYPE_PSP_RL:
3188 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3189 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3190 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3191 psp->rl.start_addr = ucode_start_addr;
3193 case PSP_FW_TYPE_PSP_SOC_DRV:
3194 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3195 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3196 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3197 psp->soc_drv.start_addr = ucode_start_addr;
3199 case PSP_FW_TYPE_PSP_INTF_DRV:
3200 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3201 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3202 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3203 psp->intf_drv.start_addr = ucode_start_addr;
3205 case PSP_FW_TYPE_PSP_DBG_DRV:
3206 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3207 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3208 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3209 psp->dbg_drv.start_addr = ucode_start_addr;
3211 case PSP_FW_TYPE_PSP_RAS_DRV:
3212 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3213 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3214 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3215 psp->ras_drv.start_addr = ucode_start_addr;
3218 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3225 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3227 const struct psp_firmware_header_v1_0 *sos_hdr;
3228 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3229 uint8_t *ucode_array_start_addr;
3231 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3232 ucode_array_start_addr = (uint8_t *)sos_hdr +
3233 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3235 if (adev->gmc.xgmi.connected_to_cpu ||
3236 (amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(13, 0, 2))) {
3237 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3238 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3240 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3241 adev->psp.sys.start_addr = ucode_array_start_addr;
3243 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3244 adev->psp.sos.start_addr = ucode_array_start_addr +
3245 le32_to_cpu(sos_hdr->sos.offset_bytes);
3247 /* Load alternate PSP SOS FW */
3248 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3250 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3251 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3253 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3254 adev->psp.sys.start_addr = ucode_array_start_addr +
3255 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3257 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3258 adev->psp.sos.start_addr = ucode_array_start_addr +
3259 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3262 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3263 dev_warn(adev->dev, "PSP SOS FW not available");
3270 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3272 struct amdgpu_device *adev = psp->adev;
3273 char fw_name[PSP_FW_NAME_LEN];
3274 const struct psp_firmware_header_v1_0 *sos_hdr;
3275 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3276 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3277 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3278 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3280 uint8_t *ucode_array_start_addr;
3283 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3284 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3288 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3289 ucode_array_start_addr = (uint8_t *)sos_hdr +
3290 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3291 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3293 switch (sos_hdr->header.header_version_major) {
3295 err = psp_init_sos_base_fw(adev);
3299 if (sos_hdr->header.header_version_minor == 1) {
3300 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3301 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3302 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3303 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3304 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3305 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3306 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3308 if (sos_hdr->header.header_version_minor == 2) {
3309 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3310 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3311 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3312 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3314 if (sos_hdr->header.header_version_minor == 3) {
3315 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3316 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3317 adev->psp.toc.start_addr = ucode_array_start_addr +
3318 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3319 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3320 adev->psp.kdb.start_addr = ucode_array_start_addr +
3321 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3322 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3323 adev->psp.spl.start_addr = ucode_array_start_addr +
3324 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3325 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3326 adev->psp.rl.start_addr = ucode_array_start_addr +
3327 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3331 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3333 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3334 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3339 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3340 err = parse_sos_bin_descriptor(psp,
3341 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3349 "unsupported psp sos firmware\n");
3356 amdgpu_ucode_release(&adev->psp.sos_fw);
3361 static int parse_ta_bin_descriptor(struct psp_context *psp,
3362 const struct psp_fw_bin_desc *desc,
3363 const struct ta_firmware_header_v2_0 *ta_hdr)
3365 uint8_t *ucode_start_addr = NULL;
3367 if (!psp || !desc || !ta_hdr)
3370 ucode_start_addr = (uint8_t *)ta_hdr +
3371 le32_to_cpu(desc->offset_bytes) +
3372 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3374 switch (desc->fw_type) {
3375 case TA_FW_TYPE_PSP_ASD:
3376 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3377 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3378 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3379 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3381 case TA_FW_TYPE_PSP_XGMI:
3382 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3383 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3384 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3386 case TA_FW_TYPE_PSP_RAS:
3387 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3388 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3389 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3391 case TA_FW_TYPE_PSP_HDCP:
3392 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3393 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3394 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3396 case TA_FW_TYPE_PSP_DTM:
3397 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3398 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3399 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3401 case TA_FW_TYPE_PSP_RAP:
3402 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3403 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3404 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3406 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3407 psp->securedisplay_context.context.bin_desc.fw_version =
3408 le32_to_cpu(desc->fw_version);
3409 psp->securedisplay_context.context.bin_desc.size_bytes =
3410 le32_to_cpu(desc->size_bytes);
3411 psp->securedisplay_context.context.bin_desc.start_addr =
3415 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3422 static int parse_ta_v1_microcode(struct psp_context *psp)
3424 const struct ta_firmware_header_v1_0 *ta_hdr;
3425 struct amdgpu_device *adev = psp->adev;
3427 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3429 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3432 adev->psp.xgmi_context.context.bin_desc.fw_version =
3433 le32_to_cpu(ta_hdr->xgmi.fw_version);
3434 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3435 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3436 adev->psp.xgmi_context.context.bin_desc.start_addr =
3438 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3440 adev->psp.ras_context.context.bin_desc.fw_version =
3441 le32_to_cpu(ta_hdr->ras.fw_version);
3442 adev->psp.ras_context.context.bin_desc.size_bytes =
3443 le32_to_cpu(ta_hdr->ras.size_bytes);
3444 adev->psp.ras_context.context.bin_desc.start_addr =
3445 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3446 le32_to_cpu(ta_hdr->ras.offset_bytes);
3448 adev->psp.hdcp_context.context.bin_desc.fw_version =
3449 le32_to_cpu(ta_hdr->hdcp.fw_version);
3450 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3451 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3452 adev->psp.hdcp_context.context.bin_desc.start_addr =
3454 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3456 adev->psp.dtm_context.context.bin_desc.fw_version =
3457 le32_to_cpu(ta_hdr->dtm.fw_version);
3458 adev->psp.dtm_context.context.bin_desc.size_bytes =
3459 le32_to_cpu(ta_hdr->dtm.size_bytes);
3460 adev->psp.dtm_context.context.bin_desc.start_addr =
3461 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3462 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3464 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3465 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3466 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3467 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3468 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3469 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3470 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3472 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3477 static int parse_ta_v2_microcode(struct psp_context *psp)
3479 const struct ta_firmware_header_v2_0 *ta_hdr;
3480 struct amdgpu_device *adev = psp->adev;
3484 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3486 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3489 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3490 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3494 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3495 err = parse_ta_bin_descriptor(psp,
3496 &ta_hdr->ta_fw_bin[ta_index],
3505 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3507 const struct common_firmware_header *hdr;
3508 struct amdgpu_device *adev = psp->adev;
3509 char fw_name[PSP_FW_NAME_LEN];
3512 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3513 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3517 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3518 switch (le16_to_cpu(hdr->header_version_major)) {
3520 err = parse_ta_v1_microcode(psp);
3523 err = parse_ta_v2_microcode(psp);
3526 dev_err(adev->dev, "unsupported TA header version\n");
3531 amdgpu_ucode_release(&adev->psp.ta_fw);
3536 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3538 struct amdgpu_device *adev = psp->adev;
3539 char fw_name[PSP_FW_NAME_LEN];
3540 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3541 struct amdgpu_firmware_info *info = NULL;
3544 if (!amdgpu_sriov_vf(adev)) {
3545 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3549 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3550 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3552 if (err == -ENODEV) {
3553 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3557 dev_err(adev->dev, "fail to initialize cap microcode\n");
3560 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3561 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3562 info->fw = adev->psp.cap_fw;
3563 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3564 adev->psp.cap_fw->data;
3565 adev->firmware.fw_size += ALIGN(
3566 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3567 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3568 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3569 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3574 amdgpu_ucode_release(&adev->psp.cap_fw);
3578 static int psp_set_clockgating_state(void *handle,
3579 enum amd_clockgating_state state)
3584 static int psp_set_powergating_state(void *handle,
3585 enum amd_powergating_state state)
3590 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3591 struct device_attribute *attr,
3594 struct drm_device *ddev = dev_get_drvdata(dev);
3595 struct amdgpu_device *adev = drm_to_adev(ddev);
3599 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3600 DRM_INFO("PSP block is not ready yet.");
3604 mutex_lock(&adev->psp.mutex);
3605 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3606 mutex_unlock(&adev->psp.mutex);
3609 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3613 return sysfs_emit(buf, "%x\n", fw_ver);
3616 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3617 struct device_attribute *attr,
3621 struct drm_device *ddev = dev_get_drvdata(dev);
3622 struct amdgpu_device *adev = drm_to_adev(ddev);
3625 const struct firmware *usbc_pd_fw;
3626 struct amdgpu_bo *fw_buf_bo = NULL;
3627 uint64_t fw_pri_mc_addr;
3628 void *fw_pri_cpu_addr;
3630 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3631 DRM_INFO("PSP block is not ready yet.");
3635 if (!drm_dev_enter(ddev, &idx))
3638 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3639 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3643 /* LFB address which is aligned to 1MB boundary per PSP request */
3644 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3645 AMDGPU_GEM_DOMAIN_VRAM |
3646 AMDGPU_GEM_DOMAIN_GTT,
3647 &fw_buf_bo, &fw_pri_mc_addr,
3652 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3654 mutex_lock(&adev->psp.mutex);
3655 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3656 mutex_unlock(&adev->psp.mutex);
3658 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3661 release_firmware(usbc_pd_fw);
3664 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3672 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3676 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3679 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3680 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3687 * Reading from this file will retrieve the USB-C PD firmware version. Writing to
3688 * this file will trigger the update process.
3690 static DEVICE_ATTR(usbc_pd_fw, 0644,
3691 psp_usbc_pd_fw_sysfs_read,
3692 psp_usbc_pd_fw_sysfs_write);
3694 int is_psp_fw_valid(struct psp_bin_desc bin)
3696 return bin.size_bytes;
3699 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3700 struct bin_attribute *bin_attr,
3701 char *buffer, loff_t pos, size_t count)
3703 struct device *dev = kobj_to_dev(kobj);
3704 struct drm_device *ddev = dev_get_drvdata(dev);
3705 struct amdgpu_device *adev = drm_to_adev(ddev);
3707 adev->psp.vbflash_done = false;
3709 /* Safeguard against memory drain */
3710 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3711 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3712 kvfree(adev->psp.vbflash_tmp_buf);
3713 adev->psp.vbflash_tmp_buf = NULL;
3714 adev->psp.vbflash_image_size = 0;
3718 /* TODO Just allocate max for now and optimize to realloc later if needed */
3719 if (!adev->psp.vbflash_tmp_buf) {
3720 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3721 if (!adev->psp.vbflash_tmp_buf)
3725 mutex_lock(&adev->psp.mutex);
3726 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3727 adev->psp.vbflash_image_size += count;
3728 mutex_unlock(&adev->psp.mutex);
3730 dev_dbg(adev->dev, "IFWI staged for update");
3735 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3736 struct bin_attribute *bin_attr, char *buffer,
3737 loff_t pos, size_t count)
3739 struct device *dev = kobj_to_dev(kobj);
3740 struct drm_device *ddev = dev_get_drvdata(dev);
3741 struct amdgpu_device *adev = drm_to_adev(ddev);
3742 struct amdgpu_bo *fw_buf_bo = NULL;
3743 uint64_t fw_pri_mc_addr;
3744 void *fw_pri_cpu_addr;
3747 if (adev->psp.vbflash_image_size == 0)
3750 dev_dbg(adev->dev, "PSP IFWI flash process initiated");
3752 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3753 AMDGPU_GPU_PAGE_SIZE,
3754 AMDGPU_GEM_DOMAIN_VRAM,
3761 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3763 mutex_lock(&adev->psp.mutex);
3764 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3765 mutex_unlock(&adev->psp.mutex);
3767 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3770 kvfree(adev->psp.vbflash_tmp_buf);
3771 adev->psp.vbflash_tmp_buf = NULL;
3772 adev->psp.vbflash_image_size = 0;
3775 dev_err(adev->dev, "Failed to load IFWI, err = %d", ret);
3779 dev_dbg(adev->dev, "PSP IFWI flash process done");
3785 * Writing to this file will stage an IFWI for update. Reading from this file
3786 * will trigger the update process.
3788 static struct bin_attribute psp_vbflash_bin_attr = {
3789 .attr = {.name = "psp_vbflash", .mode = 0660},
3791 .write = amdgpu_psp_vbflash_write,
3792 .read = amdgpu_psp_vbflash_read,
3796 * DOC: psp_vbflash_status
3797 * The status of the flash process.
3798 * 0: IFWI flash not complete.
3799 * 1: IFWI flash complete.
3801 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3802 struct device_attribute *attr,
3805 struct drm_device *ddev = dev_get_drvdata(dev);
3806 struct amdgpu_device *adev = drm_to_adev(ddev);
3807 uint32_t vbflash_status;
3809 vbflash_status = psp_vbflash_status(&adev->psp);
3810 if (!adev->psp.vbflash_done)
3812 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3815 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3817 static DEVICE_ATTR(psp_vbflash_status, 0440, amdgpu_psp_vbflash_status, NULL);
3819 static struct bin_attribute *bin_flash_attrs[] = {
3820 &psp_vbflash_bin_attr,
3824 static struct attribute *flash_attrs[] = {
3825 &dev_attr_psp_vbflash_status.attr,
3826 &dev_attr_usbc_pd_fw.attr,
3830 static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
3832 struct device *dev = kobj_to_dev(kobj);
3833 struct drm_device *ddev = dev_get_drvdata(dev);
3834 struct amdgpu_device *adev = drm_to_adev(ddev);
3836 if (attr == &dev_attr_usbc_pd_fw.attr)
3837 return adev->psp.sup_pd_fw_up ? 0660 : 0;
3839 return adev->psp.sup_ifwi_up ? 0440 : 0;
3842 static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
3843 struct bin_attribute *attr,
3846 struct device *dev = kobj_to_dev(kobj);
3847 struct drm_device *ddev = dev_get_drvdata(dev);
3848 struct amdgpu_device *adev = drm_to_adev(ddev);
3850 return adev->psp.sup_ifwi_up ? 0660 : 0;
3853 const struct attribute_group amdgpu_flash_attr_group = {
3854 .attrs = flash_attrs,
3855 .bin_attrs = bin_flash_attrs,
3856 .is_bin_visible = amdgpu_bin_flash_attr_is_visible,
3857 .is_visible = amdgpu_flash_attr_is_visible,
3860 const struct amd_ip_funcs psp_ip_funcs = {
3862 .early_init = psp_early_init,
3864 .sw_init = psp_sw_init,
3865 .sw_fini = psp_sw_fini,
3866 .hw_init = psp_hw_init,
3867 .hw_fini = psp_hw_fini,
3868 .suspend = psp_suspend,
3869 .resume = psp_resume,
3871 .check_soft_reset = NULL,
3872 .wait_for_idle = NULL,
3874 .set_clockgating_state = psp_set_clockgating_state,
3875 .set_powergating_state = psp_set_powergating_state,
3878 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3879 .type = AMD_IP_BLOCK_TYPE_PSP,
3883 .funcs = &psp_ip_funcs,
3886 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3887 .type = AMD_IP_BLOCK_TYPE_PSP,
3891 .funcs = &psp_ip_funcs,
3894 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3895 .type = AMD_IP_BLOCK_TYPE_PSP,
3899 .funcs = &psp_ip_funcs,
3902 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3903 .type = AMD_IP_BLOCK_TYPE_PSP,
3907 .funcs = &psp_ip_funcs,
3910 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
3911 .type = AMD_IP_BLOCK_TYPE_PSP,
3915 .funcs = &psp_ip_funcs,
3918 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3919 .type = AMD_IP_BLOCK_TYPE_PSP,
3923 .funcs = &psp_ip_funcs,
3926 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3927 .type = AMD_IP_BLOCK_TYPE_PSP,
3931 .funcs = &psp_ip_funcs,