2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/firmware.h>
27 #include <drm/drm_drv.h>
30 #include "amdgpu_psp.h"
31 #include "amdgpu_ucode.h"
32 #include "amdgpu_xgmi.h"
33 #include "soc15_common.h"
35 #include "psp_v10_0.h"
36 #include "psp_v11_0.h"
37 #include "psp_v11_0_8.h"
38 #include "psp_v12_0.h"
39 #include "psp_v13_0.h"
40 #include "psp_v13_0_4.h"
42 #include "amdgpu_ras.h"
43 #include "amdgpu_securedisplay.h"
44 #include "amdgpu_atomfirmware.h"
46 #define AMD_VBIOS_FILE_MAX_SIZE_B (1024*1024*3)
48 static int psp_sysfs_init(struct amdgpu_device *adev);
49 static void psp_sysfs_fini(struct amdgpu_device *adev);
51 static int psp_load_smu_fw(struct psp_context *psp);
52 static int psp_rap_terminate(struct psp_context *psp);
53 static int psp_securedisplay_terminate(struct psp_context *psp);
55 static int psp_ring_init(struct psp_context *psp,
56 enum psp_ring_type ring_type)
59 struct psp_ring *ring;
60 struct amdgpu_device *adev = psp->adev;
64 ring->ring_type = ring_type;
66 /* allocate 4k Page of Local Frame Buffer memory for ring */
67 ring->ring_size = 0x1000;
68 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
69 AMDGPU_GEM_DOMAIN_VRAM |
70 AMDGPU_GEM_DOMAIN_GTT,
72 &ring->ring_mem_mc_addr,
73 (void **)&ring->ring_mem);
83 * Due to DF Cstate management centralized to PMFW, the firmware
84 * loading sequence will be updated as below:
90 * - Load other non-psp fw
92 * - Load XGMI/RAS/HDCP/DTM TA if any
94 * This new sequence is required for
95 * - Arcturus and onwards
97 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
99 struct amdgpu_device *adev = psp->adev;
101 if (amdgpu_sriov_vf(adev)) {
102 psp->pmfw_centralized_cstate_management = false;
106 switch (adev->ip_versions[MP0_HWIP][0]) {
107 case IP_VERSION(11, 0, 0):
108 case IP_VERSION(11, 0, 4):
109 case IP_VERSION(11, 0, 5):
110 case IP_VERSION(11, 0, 7):
111 case IP_VERSION(11, 0, 9):
112 case IP_VERSION(11, 0, 11):
113 case IP_VERSION(11, 0, 12):
114 case IP_VERSION(11, 0, 13):
115 case IP_VERSION(13, 0, 0):
116 case IP_VERSION(13, 0, 2):
117 case IP_VERSION(13, 0, 7):
118 psp->pmfw_centralized_cstate_management = true;
121 psp->pmfw_centralized_cstate_management = false;
126 static int psp_init_sriov_microcode(struct psp_context *psp)
128 struct amdgpu_device *adev = psp->adev;
129 char ucode_prefix[30];
132 amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
134 switch (adev->ip_versions[MP0_HWIP][0]) {
135 case IP_VERSION(9, 0, 0):
136 case IP_VERSION(11, 0, 7):
137 case IP_VERSION(11, 0, 9):
138 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
139 ret = psp_init_cap_microcode(psp, ucode_prefix);
141 case IP_VERSION(13, 0, 2):
142 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
143 ret = psp_init_cap_microcode(psp, ucode_prefix);
144 ret &= psp_init_ta_microcode(psp, ucode_prefix);
146 case IP_VERSION(13, 0, 0):
147 adev->virt.autoload_ucode_id = 0;
149 case IP_VERSION(13, 0, 10):
150 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
158 static int psp_early_init(void *handle)
160 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
161 struct psp_context *psp = &adev->psp;
163 switch (adev->ip_versions[MP0_HWIP][0]) {
164 case IP_VERSION(9, 0, 0):
165 psp_v3_1_set_psp_funcs(psp);
166 psp->autoload_supported = false;
168 case IP_VERSION(10, 0, 0):
169 case IP_VERSION(10, 0, 1):
170 psp_v10_0_set_psp_funcs(psp);
171 psp->autoload_supported = false;
173 case IP_VERSION(11, 0, 2):
174 case IP_VERSION(11, 0, 4):
175 psp_v11_0_set_psp_funcs(psp);
176 psp->autoload_supported = false;
178 case IP_VERSION(11, 0, 0):
179 case IP_VERSION(11, 0, 5):
180 case IP_VERSION(11, 0, 9):
181 case IP_VERSION(11, 0, 7):
182 case IP_VERSION(11, 0, 11):
183 case IP_VERSION(11, 5, 0):
184 case IP_VERSION(11, 0, 12):
185 case IP_VERSION(11, 0, 13):
186 psp_v11_0_set_psp_funcs(psp);
187 psp->autoload_supported = true;
189 case IP_VERSION(11, 0, 3):
190 case IP_VERSION(12, 0, 1):
191 psp_v12_0_set_psp_funcs(psp);
193 case IP_VERSION(13, 0, 2):
194 psp_v13_0_set_psp_funcs(psp);
196 case IP_VERSION(13, 0, 1):
197 case IP_VERSION(13, 0, 3):
198 case IP_VERSION(13, 0, 5):
199 case IP_VERSION(13, 0, 8):
200 case IP_VERSION(13, 0, 10):
201 case IP_VERSION(13, 0, 11):
202 psp_v13_0_set_psp_funcs(psp);
203 psp->autoload_supported = true;
205 case IP_VERSION(11, 0, 8):
206 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
207 psp_v11_0_8_set_psp_funcs(psp);
208 psp->autoload_supported = false;
211 case IP_VERSION(13, 0, 0):
212 case IP_VERSION(13, 0, 7):
213 psp_v13_0_set_psp_funcs(psp);
214 psp->autoload_supported = true;
216 case IP_VERSION(13, 0, 4):
217 psp_v13_0_4_set_psp_funcs(psp);
218 psp->autoload_supported = true;
226 psp_check_pmfw_centralized_cstate_management(psp);
228 if (amdgpu_sriov_vf(adev))
229 return psp_init_sriov_microcode(psp);
231 return psp_init_microcode(psp);
234 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
236 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
237 &mem_ctx->shared_buf);
238 mem_ctx->shared_bo = NULL;
241 static void psp_free_shared_bufs(struct psp_context *psp)
246 /* free TMR memory buffer */
247 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
248 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
251 /* free xgmi shared memory */
252 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
254 /* free ras shared memory */
255 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
257 /* free hdcp shared memory */
258 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
260 /* free dtm shared memory */
261 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
263 /* free rap shared memory */
264 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
266 /* free securedisplay shared memory */
267 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
272 static void psp_memory_training_fini(struct psp_context *psp)
274 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
276 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
277 kfree(ctx->sys_cache);
278 ctx->sys_cache = NULL;
281 static int psp_memory_training_init(struct psp_context *psp)
284 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
286 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
287 DRM_DEBUG("memory training is not supported!\n");
291 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
292 if (ctx->sys_cache == NULL) {
293 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
298 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
299 ctx->train_data_size,
300 ctx->p2c_train_data_offset,
301 ctx->c2p_train_data_offset);
302 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
306 psp_memory_training_fini(psp);
311 * Helper funciton to query psp runtime database entry
313 * @adev: amdgpu_device pointer
314 * @entry_type: the type of psp runtime database entry
315 * @db_entry: runtime database entry pointer
317 * Return false if runtime database doesn't exit or entry is invalid
318 * or true if the specific database entry is found, and copy to @db_entry
320 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
321 enum psp_runtime_entry_type entry_type,
324 uint64_t db_header_pos, db_dir_pos;
325 struct psp_runtime_data_header db_header = {0};
326 struct psp_runtime_data_directory db_dir = {0};
330 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
331 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
333 /* read runtime db header from vram */
334 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
335 sizeof(struct psp_runtime_data_header), false);
337 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
338 /* runtime db doesn't exist, exit */
339 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
343 /* read runtime database entry from vram */
344 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
345 sizeof(struct psp_runtime_data_directory), false);
347 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
348 /* invalid db entry count, exit */
349 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
353 /* look up for requested entry type */
354 for (i = 0; i < db_dir.entry_count && !ret; i++) {
355 if (db_dir.entry_list[i].entry_type == entry_type) {
356 switch (entry_type) {
357 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
358 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
359 /* invalid db entry size */
360 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
363 /* read runtime database entry */
364 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
365 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
368 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
369 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
370 /* invalid db entry size */
371 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
374 /* read runtime database entry */
375 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
376 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
389 static int psp_sw_init(void *handle)
391 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
392 struct psp_context *psp = &adev->psp;
394 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
395 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
396 struct psp_runtime_scpm_entry scpm_entry;
398 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
400 DRM_ERROR("Failed to allocate memory to command buffer!\n");
404 adev->psp.xgmi_context.supports_extended_data =
405 !adev->gmc.xgmi.connected_to_cpu &&
406 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
408 memset(&scpm_entry, 0, sizeof(scpm_entry));
409 if ((psp_get_runtime_db_entry(adev,
410 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
412 (SCPM_DISABLE != scpm_entry.scpm_status)) {
413 adev->scpm_enabled = true;
414 adev->scpm_status = scpm_entry.scpm_status;
416 adev->scpm_enabled = false;
417 adev->scpm_status = SCPM_DISABLE;
420 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
422 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
423 if (psp_get_runtime_db_entry(adev,
424 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
426 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
427 if ((psp->boot_cfg_bitmask) &
428 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
429 /* If psp runtime database exists, then
430 * only enable two stage memory training
431 * when TWO_STAGE_DRAM_TRAINING bit is set
432 * in runtime database */
433 mem_training_ctx->enable_mem_training = true;
437 /* If psp runtime database doesn't exist or
438 * is invalid, force enable two stage memory
440 mem_training_ctx->enable_mem_training = true;
443 if (mem_training_ctx->enable_mem_training) {
444 ret = psp_memory_training_init(psp);
446 DRM_ERROR("Failed to initialize memory training!\n");
450 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
452 DRM_ERROR("Failed to process memory training!\n");
457 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
458 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
459 ret= psp_sysfs_init(adev);
465 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
466 amdgpu_sriov_vf(adev) ?
467 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
469 &psp->fw_pri_mc_addr,
474 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
475 AMDGPU_GEM_DOMAIN_VRAM,
477 &psp->fence_buf_mc_addr,
482 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
483 AMDGPU_GEM_DOMAIN_VRAM,
484 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
485 (void **)&psp->cmd_buf_mem);
492 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
493 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
495 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
496 &psp->fence_buf_mc_addr, &psp->fence_buf);
500 static int psp_sw_fini(void *handle)
502 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
503 struct psp_context *psp = &adev->psp;
504 struct psp_gfx_cmd_resp *cmd = psp->cmd;
506 psp_memory_training_fini(psp);
508 amdgpu_ucode_release(&psp->sos_fw);
509 amdgpu_ucode_release(&psp->asd_fw);
510 amdgpu_ucode_release(&psp->ta_fw);
511 amdgpu_ucode_release(&psp->cap_fw);
512 amdgpu_ucode_release(&psp->toc_fw);
514 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
515 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
516 psp_sysfs_fini(adev);
521 if (psp->km_ring.ring_mem)
522 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
523 &psp->km_ring.ring_mem_mc_addr,
524 (void **)&psp->km_ring.ring_mem);
526 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
527 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
528 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
529 &psp->fence_buf_mc_addr, &psp->fence_buf);
530 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
531 (void **)&psp->cmd_buf_mem);
536 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
537 uint32_t reg_val, uint32_t mask, bool check_changed)
541 struct amdgpu_device *adev = psp->adev;
543 if (psp->adev->no_hw_access)
546 for (i = 0; i < adev->usec_timeout; i++) {
547 val = RREG32(reg_index);
552 if ((val & mask) == reg_val)
561 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
564 case GFX_CMD_ID_LOAD_TA:
566 case GFX_CMD_ID_UNLOAD_TA:
568 case GFX_CMD_ID_INVOKE_CMD:
570 case GFX_CMD_ID_LOAD_ASD:
572 case GFX_CMD_ID_SETUP_TMR:
574 case GFX_CMD_ID_LOAD_IP_FW:
576 case GFX_CMD_ID_DESTROY_TMR:
577 return "DESTROY_TMR";
578 case GFX_CMD_ID_SAVE_RESTORE:
579 return "SAVE_RESTORE_IP_FW";
580 case GFX_CMD_ID_SETUP_VMR:
582 case GFX_CMD_ID_DESTROY_VMR:
583 return "DESTROY_VMR";
584 case GFX_CMD_ID_PROG_REG:
586 case GFX_CMD_ID_GET_FW_ATTESTATION:
587 return "GET_FW_ATTESTATION";
588 case GFX_CMD_ID_LOAD_TOC:
589 return "ID_LOAD_TOC";
590 case GFX_CMD_ID_AUTOLOAD_RLC:
591 return "AUTOLOAD_RLC";
592 case GFX_CMD_ID_BOOT_CFG:
595 return "UNKNOWN CMD";
600 psp_cmd_submit_buf(struct psp_context *psp,
601 struct amdgpu_firmware_info *ucode,
602 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
607 bool ras_intr = false;
608 bool skip_unsupport = false;
611 if (psp->adev->no_hw_access)
614 dev_entered = drm_dev_enter(adev_to_drm(psp->adev), &idx);
616 * We allow sending PSP messages LOAD_ASD and UNLOAD_TA without acquiring
617 * a lock in drm_dev_enter during driver unload because we must call
618 * drm_dev_unplug as the beginning of unload driver sequence . It is very
619 * crucial that userspace can't access device instances anymore.
622 WARN_ON(psp->cmd_buf_mem->cmd_id != GFX_CMD_ID_LOAD_ASD &&
623 psp->cmd_buf_mem->cmd_id != GFX_CMD_ID_UNLOAD_TA &&
624 psp->cmd_buf_mem->cmd_id != GFX_CMD_ID_INVOKE_CMD);
626 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
628 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
630 index = atomic_inc_return(&psp->fence_value);
631 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
633 atomic_dec(&psp->fence_value);
637 amdgpu_device_invalidate_hdp(psp->adev, NULL);
638 while (*((unsigned int *)psp->fence_buf) != index) {
642 * Shouldn't wait for timeout when err_event_athub occurs,
643 * because gpu reset thread triggered and lock resource should
644 * be released for psp resume sequence.
646 ras_intr = amdgpu_ras_intr_triggered();
649 usleep_range(10, 100);
650 amdgpu_device_invalidate_hdp(psp->adev, NULL);
653 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
654 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
655 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
657 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
659 /* In some cases, psp response status is not 0 even there is no
660 * problem while the command is submitted. Some version of PSP FW
661 * doesn't write 0 to that field.
662 * So here we would like to only print a warning instead of an error
663 * during psp initialization to avoid breaking hw_init and it doesn't
666 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
668 DRM_WARN("failed to load ucode %s(0x%X) ",
669 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
670 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
671 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
672 psp->cmd_buf_mem->resp.status);
673 /* If any firmware (including CAP) load fails under SRIOV, it should
674 * return failure to stop the VF from initializing.
675 * Also return failure in case of timeout
677 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
684 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
685 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
694 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
696 struct psp_gfx_cmd_resp *cmd = psp->cmd;
698 mutex_lock(&psp->mutex);
700 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
705 static void release_psp_cmd_buf(struct psp_context *psp)
707 mutex_unlock(&psp->mutex);
710 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
711 struct psp_gfx_cmd_resp *cmd,
712 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
714 struct amdgpu_device *adev = psp->adev;
715 uint32_t size = amdgpu_bo_size(tmr_bo);
716 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
718 if (amdgpu_sriov_vf(psp->adev))
719 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
721 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
722 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
723 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
724 cmd->cmd.cmd_setup_tmr.buf_size = size;
725 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
726 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
727 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
730 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
731 uint64_t pri_buf_mc, uint32_t size)
733 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
734 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
735 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
736 cmd->cmd.cmd_load_toc.toc_size = size;
739 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
740 static int psp_load_toc(struct psp_context *psp,
744 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
746 /* Copy toc to psp firmware private buffer */
747 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
749 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
751 ret = psp_cmd_submit_buf(psp, NULL, cmd,
752 psp->fence_buf_mc_addr);
754 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
756 release_psp_cmd_buf(psp);
761 /* Set up Trusted Memory Region */
762 static int psp_tmr_init(struct psp_context *psp)
770 * According to HW engineer, they prefer the TMR address be "naturally
771 * aligned" , e.g. the start address be an integer divide of TMR size.
773 * Note: this memory need be reserved till the driver
776 tmr_size = PSP_TMR_SIZE(psp->adev);
778 /* For ASICs support RLC autoload, psp will parse the toc
779 * and calculate the total size of TMR needed */
780 if (!amdgpu_sriov_vf(psp->adev) &&
781 psp->toc.start_addr &&
782 psp->toc.size_bytes &&
784 ret = psp_load_toc(psp, &tmr_size);
786 DRM_ERROR("Failed to load toc\n");
792 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
793 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
795 AMDGPU_HAS_VRAM(psp->adev) ?
796 AMDGPU_GEM_DOMAIN_VRAM :
797 AMDGPU_GEM_DOMAIN_GTT,
798 &psp->tmr_bo, &psp->tmr_mc_addr,
805 static bool psp_skip_tmr(struct psp_context *psp)
807 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
808 case IP_VERSION(11, 0, 9):
809 case IP_VERSION(11, 0, 7):
810 case IP_VERSION(13, 0, 2):
811 case IP_VERSION(13, 0, 10):
818 static int psp_tmr_load(struct psp_context *psp)
821 struct psp_gfx_cmd_resp *cmd;
823 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
824 * Already set up by host driver.
826 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
829 cmd = acquire_psp_cmd_buf(psp);
831 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
832 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
833 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
835 ret = psp_cmd_submit_buf(psp, NULL, cmd,
836 psp->fence_buf_mc_addr);
838 release_psp_cmd_buf(psp);
843 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
844 struct psp_gfx_cmd_resp *cmd)
846 if (amdgpu_sriov_vf(psp->adev))
847 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
849 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
852 static int psp_tmr_unload(struct psp_context *psp)
855 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
857 psp_prep_tmr_unload_cmd_buf(psp, cmd);
858 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
860 ret = psp_cmd_submit_buf(psp, NULL, cmd,
861 psp->fence_buf_mc_addr);
863 release_psp_cmd_buf(psp);
868 static int psp_tmr_terminate(struct psp_context *psp)
870 return psp_tmr_unload(psp);
873 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
874 uint64_t *output_ptr)
877 struct psp_gfx_cmd_resp *cmd;
882 if (amdgpu_sriov_vf(psp->adev))
885 cmd = acquire_psp_cmd_buf(psp);
887 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
889 ret = psp_cmd_submit_buf(psp, NULL, cmd,
890 psp->fence_buf_mc_addr);
893 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
894 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
897 release_psp_cmd_buf(psp);
902 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
904 struct psp_context *psp = &adev->psp;
905 struct psp_gfx_cmd_resp *cmd;
908 if (amdgpu_sriov_vf(adev))
911 cmd = acquire_psp_cmd_buf(psp);
913 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
914 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
916 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
919 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
922 release_psp_cmd_buf(psp);
927 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
930 struct psp_context *psp = &adev->psp;
931 struct psp_gfx_cmd_resp *cmd;
933 if (amdgpu_sriov_vf(adev))
936 cmd = acquire_psp_cmd_buf(psp);
938 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
939 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
940 cmd->cmd.boot_cfg.boot_config = boot_cfg;
941 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
943 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
945 release_psp_cmd_buf(psp);
950 static int psp_rl_load(struct amdgpu_device *adev)
953 struct psp_context *psp = &adev->psp;
954 struct psp_gfx_cmd_resp *cmd;
956 if (!is_psp_fw_valid(psp->rl))
959 cmd = acquire_psp_cmd_buf(psp);
961 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
962 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
964 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
965 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
966 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
967 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
968 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
970 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
972 release_psp_cmd_buf(psp);
977 static int psp_asd_initialize(struct psp_context *psp)
981 /* If PSP version doesn't match ASD version, asd loading will be failed.
982 * add workaround to bypass it for sriov now.
983 * TODO: add version check to make it common
985 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
988 psp->asd_context.mem_context.shared_mc_addr = 0;
989 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
990 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
992 ret = psp_ta_load(psp, &psp->asd_context);
994 psp->asd_context.initialized = true;
999 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1000 uint32_t session_id)
1002 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1003 cmd->cmd.cmd_unload_ta.session_id = session_id;
1006 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1009 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1011 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1013 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1015 context->resp_status = cmd->resp.status;
1017 release_psp_cmd_buf(psp);
1022 static int psp_asd_terminate(struct psp_context *psp)
1026 if (amdgpu_sriov_vf(psp->adev))
1029 if (!psp->asd_context.initialized)
1032 ret = psp_ta_unload(psp, &psp->asd_context);
1034 psp->asd_context.initialized = false;
1039 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1040 uint32_t id, uint32_t value)
1042 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1043 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1044 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1047 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1050 struct psp_gfx_cmd_resp *cmd;
1053 if (reg >= PSP_REG_LAST)
1056 cmd = acquire_psp_cmd_buf(psp);
1058 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1059 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1061 DRM_ERROR("PSP failed to program reg id %d", reg);
1063 release_psp_cmd_buf(psp);
1068 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1070 struct ta_context *context)
1072 cmd->cmd_id = context->ta_load_type;
1073 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1074 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1075 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1077 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1078 lower_32_bits(context->mem_context.shared_mc_addr);
1079 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1080 upper_32_bits(context->mem_context.shared_mc_addr);
1081 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1084 int psp_ta_init_shared_buf(struct psp_context *psp,
1085 struct ta_mem_context *mem_ctx)
1088 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1089 * physical) for ta to host memory
1091 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1092 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1093 AMDGPU_GEM_DOMAIN_GTT,
1094 &mem_ctx->shared_bo,
1095 &mem_ctx->shared_mc_addr,
1096 &mem_ctx->shared_buf);
1099 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1101 uint32_t session_id)
1103 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1104 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1105 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1108 int psp_ta_invoke(struct psp_context *psp,
1110 struct ta_context *context)
1113 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1115 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1117 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1118 psp->fence_buf_mc_addr);
1120 context->resp_status = cmd->resp.status;
1122 release_psp_cmd_buf(psp);
1127 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1130 struct psp_gfx_cmd_resp *cmd;
1132 cmd = acquire_psp_cmd_buf(psp);
1134 psp_copy_fw(psp, context->bin_desc.start_addr,
1135 context->bin_desc.size_bytes);
1137 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1139 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1140 psp->fence_buf_mc_addr);
1142 context->resp_status = cmd->resp.status;
1145 context->session_id = cmd->resp.session_id;
1148 release_psp_cmd_buf(psp);
1153 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1155 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1158 int psp_xgmi_terminate(struct psp_context *psp)
1161 struct amdgpu_device *adev = psp->adev;
1163 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1164 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1165 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1166 adev->gmc.xgmi.connected_to_cpu))
1169 if (!psp->xgmi_context.context.initialized)
1172 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1174 psp->xgmi_context.context.initialized = false;
1179 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1181 struct ta_xgmi_shared_memory *xgmi_cmd;
1185 !psp->xgmi_context.context.bin_desc.size_bytes ||
1186 !psp->xgmi_context.context.bin_desc.start_addr)
1192 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1193 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1195 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1196 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1202 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1204 psp->xgmi_context.context.initialized = true;
1209 /* Initialize XGMI session */
1210 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1211 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1212 xgmi_cmd->flag_extend_link_record = set_extended_data;
1213 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1215 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1220 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1222 struct ta_xgmi_shared_memory *xgmi_cmd;
1225 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1226 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1228 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1230 /* Invoke xgmi ta to get hive id */
1231 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1235 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1240 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1242 struct ta_xgmi_shared_memory *xgmi_cmd;
1245 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1246 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1248 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1250 /* Invoke xgmi ta to get the node id */
1251 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1255 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1260 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1262 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1263 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1267 * Chips that support extended topology information require the driver to
1268 * reflect topology information in the opposite direction. This is
1269 * because the TA has already exceeded its link record limit and if the
1270 * TA holds bi-directional information, the driver would have to do
1271 * multiple fetches instead of just two.
1273 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1274 struct psp_xgmi_node_info node_info)
1276 struct amdgpu_device *mirror_adev;
1277 struct amdgpu_hive_info *hive;
1278 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1279 uint64_t dst_node_id = node_info.node_id;
1280 uint8_t dst_num_hops = node_info.num_hops;
1281 uint8_t dst_num_links = node_info.num_links;
1283 hive = amdgpu_get_xgmi_hive(psp->adev);
1284 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1285 struct psp_xgmi_topology_info *mirror_top_info;
1288 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1291 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1292 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1293 if (mirror_top_info->nodes[j].node_id != src_node_id)
1296 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1298 * prevent 0 num_links value re-reflection since reflection
1299 * criteria is based on num_hops (direct or indirect).
1303 mirror_top_info->nodes[j].num_links = dst_num_links;
1311 amdgpu_put_xgmi_hive(hive);
1314 int psp_xgmi_get_topology_info(struct psp_context *psp,
1316 struct psp_xgmi_topology_info *topology,
1317 bool get_extended_data)
1319 struct ta_xgmi_shared_memory *xgmi_cmd;
1320 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1321 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1325 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1328 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1329 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1330 xgmi_cmd->flag_extend_link_record = get_extended_data;
1332 /* Fill in the shared memory with topology information as input */
1333 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1334 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1335 topology_info_input->num_nodes = number_devices;
1337 for (i = 0; i < topology_info_input->num_nodes; i++) {
1338 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1339 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1340 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1341 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1344 /* Invoke xgmi ta to get the topology information */
1345 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1349 /* Read the output topology information from the shared memory */
1350 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1351 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1352 for (i = 0; i < topology->num_nodes; i++) {
1353 /* extended data will either be 0 or equal to non-extended data */
1354 if (topology_info_output->nodes[i].num_hops)
1355 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1357 /* non-extended data gets everything here so no need to update */
1358 if (!get_extended_data) {
1359 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1360 topology->nodes[i].is_sharing_enabled =
1361 topology_info_output->nodes[i].is_sharing_enabled;
1362 topology->nodes[i].sdma_engine =
1363 topology_info_output->nodes[i].sdma_engine;
1368 /* Invoke xgmi ta again to get the link information */
1369 if (psp_xgmi_peer_link_info_supported(psp)) {
1370 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1372 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1374 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1379 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1380 for (i = 0; i < topology->num_nodes; i++) {
1381 /* accumulate num_links on extended data */
1382 topology->nodes[i].num_links = get_extended_data ?
1383 topology->nodes[i].num_links +
1384 link_info_output->nodes[i].num_links :
1385 link_info_output->nodes[i].num_links;
1387 /* reflect the topology information for bi-directionality */
1388 if (psp->xgmi_context.supports_extended_data &&
1389 get_extended_data && topology->nodes[i].num_hops)
1390 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1397 int psp_xgmi_set_topology_info(struct psp_context *psp,
1399 struct psp_xgmi_topology_info *topology)
1401 struct ta_xgmi_shared_memory *xgmi_cmd;
1402 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1405 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1408 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1409 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1411 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1412 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1413 topology_info_input->num_nodes = number_devices;
1415 for (i = 0; i < topology_info_input->num_nodes; i++) {
1416 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1417 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1418 topology_info_input->nodes[i].is_sharing_enabled = 1;
1419 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1422 /* Invoke xgmi ta to set topology information */
1423 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1427 static void psp_ras_ta_check_status(struct psp_context *psp)
1429 struct ta_ras_shared_memory *ras_cmd =
1430 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1432 switch (ras_cmd->ras_status) {
1433 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1434 dev_warn(psp->adev->dev,
1435 "RAS WARNING: cmd failed due to unsupported ip\n");
1437 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1438 dev_warn(psp->adev->dev,
1439 "RAS WARNING: cmd failed due to unsupported error injection\n");
1441 case TA_RAS_STATUS__SUCCESS:
1443 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1444 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1445 dev_warn(psp->adev->dev,
1446 "RAS WARNING: Inject error to critical region is not allowed\n");
1449 dev_warn(psp->adev->dev,
1450 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1455 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1457 struct ta_ras_shared_memory *ras_cmd;
1460 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1463 * TODO: bypass the loading in sriov for now
1465 if (amdgpu_sriov_vf(psp->adev))
1468 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1470 if (amdgpu_ras_intr_triggered())
1473 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1475 DRM_WARN("RAS: Unsupported Interface");
1480 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1481 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1483 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1485 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1486 dev_warn(psp->adev->dev,
1487 "RAS internal register access blocked\n");
1489 psp_ras_ta_check_status(psp);
1495 int psp_ras_enable_features(struct psp_context *psp,
1496 union ta_ras_cmd_input *info, bool enable)
1498 struct ta_ras_shared_memory *ras_cmd;
1501 if (!psp->ras_context.context.initialized)
1504 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1505 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1508 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1510 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1512 ras_cmd->ras_in_message = *info;
1514 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1521 int psp_ras_terminate(struct psp_context *psp)
1526 * TODO: bypass the terminate in sriov for now
1528 if (amdgpu_sriov_vf(psp->adev))
1531 if (!psp->ras_context.context.initialized)
1534 ret = psp_ta_unload(psp, &psp->ras_context.context);
1536 psp->ras_context.context.initialized = false;
1541 int psp_ras_initialize(struct psp_context *psp)
1544 uint32_t boot_cfg = 0xFF;
1545 struct amdgpu_device *adev = psp->adev;
1546 struct ta_ras_shared_memory *ras_cmd;
1549 * TODO: bypass the initialize in sriov for now
1551 if (amdgpu_sriov_vf(adev))
1554 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1555 !adev->psp.ras_context.context.bin_desc.start_addr) {
1556 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1560 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1561 /* query GECC enablement status from boot config
1562 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1564 ret = psp_boot_config_get(adev, &boot_cfg);
1566 dev_warn(adev->dev, "PSP get boot config failed\n");
1568 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1570 dev_info(adev->dev, "GECC is disabled\n");
1572 /* disable GECC in next boot cycle if ras is
1573 * disabled by module parameter amdgpu_ras_enable
1574 * and/or amdgpu_ras_mask, or boot_config_get call
1577 ret = psp_boot_config_set(adev, 0);
1579 dev_warn(adev->dev, "PSP set boot config failed\n");
1581 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1582 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1585 if (1 == boot_cfg) {
1586 dev_info(adev->dev, "GECC is enabled\n");
1588 /* enable GECC in next boot cycle if it is disabled
1589 * in boot config, or force enable GECC if failed to
1590 * get boot configuration
1592 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1594 dev_warn(adev->dev, "PSP set boot config failed\n");
1596 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1601 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1602 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1604 if (!psp->ras_context.context.mem_context.shared_buf) {
1605 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1610 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1611 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1613 if (amdgpu_ras_is_poison_mode_supported(adev))
1614 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1615 if (!adev->gmc.xgmi.connected_to_cpu)
1616 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1618 ret = psp_ta_load(psp, &psp->ras_context.context);
1620 if (!ret && !ras_cmd->ras_status)
1621 psp->ras_context.context.initialized = true;
1623 if (ras_cmd->ras_status)
1624 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1626 /* fail to load RAS TA */
1627 psp->ras_context.context.initialized = false;
1633 int psp_ras_trigger_error(struct psp_context *psp,
1634 struct ta_ras_trigger_error_input *info)
1636 struct ta_ras_shared_memory *ras_cmd;
1639 if (!psp->ras_context.context.initialized)
1642 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1643 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1645 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1646 ras_cmd->ras_in_message.trigger_error = *info;
1648 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1652 /* If err_event_athub occurs error inject was successful, however
1653 return status from TA is no long reliable */
1654 if (amdgpu_ras_intr_triggered())
1657 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1659 else if (ras_cmd->ras_status)
1667 static int psp_hdcp_initialize(struct psp_context *psp)
1672 * TODO: bypass the initialize in sriov for now
1674 if (amdgpu_sriov_vf(psp->adev))
1677 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1678 !psp->hdcp_context.context.bin_desc.start_addr) {
1679 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1683 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1684 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1686 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1687 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1692 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1694 psp->hdcp_context.context.initialized = true;
1695 mutex_init(&psp->hdcp_context.mutex);
1701 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1704 * TODO: bypass the loading in sriov for now
1706 if (amdgpu_sriov_vf(psp->adev))
1709 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1712 static int psp_hdcp_terminate(struct psp_context *psp)
1717 * TODO: bypass the terminate in sriov for now
1719 if (amdgpu_sriov_vf(psp->adev))
1722 if (!psp->hdcp_context.context.initialized)
1725 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1727 psp->hdcp_context.context.initialized = false;
1734 static int psp_dtm_initialize(struct psp_context *psp)
1739 * TODO: bypass the initialize in sriov for now
1741 if (amdgpu_sriov_vf(psp->adev))
1744 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1745 !psp->dtm_context.context.bin_desc.start_addr) {
1746 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1750 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1751 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1753 if (!psp->dtm_context.context.mem_context.shared_buf) {
1754 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1759 ret = psp_ta_load(psp, &psp->dtm_context.context);
1761 psp->dtm_context.context.initialized = true;
1762 mutex_init(&psp->dtm_context.mutex);
1768 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1771 * TODO: bypass the loading in sriov for now
1773 if (amdgpu_sriov_vf(psp->adev))
1776 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1779 static int psp_dtm_terminate(struct psp_context *psp)
1784 * TODO: bypass the terminate in sriov for now
1786 if (amdgpu_sriov_vf(psp->adev))
1789 if (!psp->dtm_context.context.initialized)
1792 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1794 psp->dtm_context.context.initialized = false;
1801 static int psp_rap_initialize(struct psp_context *psp)
1804 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1807 * TODO: bypass the initialize in sriov for now
1809 if (amdgpu_sriov_vf(psp->adev))
1812 if (!psp->rap_context.context.bin_desc.size_bytes ||
1813 !psp->rap_context.context.bin_desc.start_addr) {
1814 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1818 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1819 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1821 if (!psp->rap_context.context.mem_context.shared_buf) {
1822 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1827 ret = psp_ta_load(psp, &psp->rap_context.context);
1829 psp->rap_context.context.initialized = true;
1830 mutex_init(&psp->rap_context.mutex);
1834 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1835 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1836 psp_rap_terminate(psp);
1837 /* free rap shared memory */
1838 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1840 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1849 static int psp_rap_terminate(struct psp_context *psp)
1853 if (!psp->rap_context.context.initialized)
1856 ret = psp_ta_unload(psp, &psp->rap_context.context);
1858 psp->rap_context.context.initialized = false;
1863 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1865 struct ta_rap_shared_memory *rap_cmd;
1868 if (!psp->rap_context.context.initialized)
1871 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1872 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1875 mutex_lock(&psp->rap_context.mutex);
1877 rap_cmd = (struct ta_rap_shared_memory *)
1878 psp->rap_context.context.mem_context.shared_buf;
1879 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1881 rap_cmd->cmd_id = ta_cmd_id;
1882 rap_cmd->validation_method_id = METHOD_A;
1884 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1889 *status = rap_cmd->rap_status;
1892 mutex_unlock(&psp->rap_context.mutex);
1898 /* securedisplay start */
1899 static int psp_securedisplay_initialize(struct psp_context *psp)
1902 struct ta_securedisplay_cmd *securedisplay_cmd;
1905 * TODO: bypass the initialize in sriov for now
1907 if (amdgpu_sriov_vf(psp->adev))
1910 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1911 !psp->securedisplay_context.context.bin_desc.start_addr) {
1912 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1916 psp->securedisplay_context.context.mem_context.shared_mem_size =
1917 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1918 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1920 if (!psp->securedisplay_context.context.initialized) {
1921 ret = psp_ta_init_shared_buf(psp,
1922 &psp->securedisplay_context.context.mem_context);
1927 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1929 psp->securedisplay_context.context.initialized = true;
1930 mutex_init(&psp->securedisplay_context.mutex);
1934 mutex_lock(&psp->securedisplay_context.mutex);
1936 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1937 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1939 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1941 mutex_unlock(&psp->securedisplay_context.mutex);
1944 psp_securedisplay_terminate(psp);
1945 /* free securedisplay shared memory */
1946 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1947 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1951 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1952 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1953 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1954 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1960 static int psp_securedisplay_terminate(struct psp_context *psp)
1965 * TODO:bypass the terminate in sriov for now
1967 if (amdgpu_sriov_vf(psp->adev))
1970 if (!psp->securedisplay_context.context.initialized)
1973 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1975 psp->securedisplay_context.context.initialized = false;
1980 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1984 if (!psp->securedisplay_context.context.initialized)
1987 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1988 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1991 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1995 /* SECUREDISPLAY end */
1997 static int psp_hw_start(struct psp_context *psp)
1999 struct amdgpu_device *adev = psp->adev;
2002 if (!amdgpu_sriov_vf(adev)) {
2003 if ((is_psp_fw_valid(psp->kdb)) &&
2004 (psp->funcs->bootloader_load_kdb != NULL)) {
2005 ret = psp_bootloader_load_kdb(psp);
2007 DRM_ERROR("PSP load kdb failed!\n");
2012 if ((is_psp_fw_valid(psp->spl)) &&
2013 (psp->funcs->bootloader_load_spl != NULL)) {
2014 ret = psp_bootloader_load_spl(psp);
2016 DRM_ERROR("PSP load spl failed!\n");
2021 if ((is_psp_fw_valid(psp->sys)) &&
2022 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2023 ret = psp_bootloader_load_sysdrv(psp);
2025 DRM_ERROR("PSP load sys drv failed!\n");
2030 if ((is_psp_fw_valid(psp->soc_drv)) &&
2031 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2032 ret = psp_bootloader_load_soc_drv(psp);
2034 DRM_ERROR("PSP load soc drv failed!\n");
2039 if ((is_psp_fw_valid(psp->intf_drv)) &&
2040 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2041 ret = psp_bootloader_load_intf_drv(psp);
2043 DRM_ERROR("PSP load intf drv failed!\n");
2048 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2049 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2050 ret = psp_bootloader_load_dbg_drv(psp);
2052 DRM_ERROR("PSP load dbg drv failed!\n");
2057 if ((is_psp_fw_valid(psp->ras_drv)) &&
2058 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2059 ret = psp_bootloader_load_ras_drv(psp);
2061 DRM_ERROR("PSP load ras_drv failed!\n");
2066 if ((is_psp_fw_valid(psp->sos)) &&
2067 (psp->funcs->bootloader_load_sos != NULL)) {
2068 ret = psp_bootloader_load_sos(psp);
2070 DRM_ERROR("PSP load sos failed!\n");
2076 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2078 DRM_ERROR("PSP create ring failed!\n");
2082 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2085 ret = psp_tmr_init(psp);
2087 DRM_ERROR("PSP tmr init failed!\n");
2093 * For ASICs with DF Cstate management centralized
2094 * to PMFW, TMR setup should be performed after PMFW
2095 * loaded and before other non-psp firmware loaded.
2097 if (psp->pmfw_centralized_cstate_management) {
2098 ret = psp_load_smu_fw(psp);
2103 ret = psp_tmr_load(psp);
2105 DRM_ERROR("PSP load tmr failed!\n");
2112 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2113 enum psp_gfx_fw_type *type)
2115 switch (ucode->ucode_id) {
2116 case AMDGPU_UCODE_ID_CAP:
2117 *type = GFX_FW_TYPE_CAP;
2119 case AMDGPU_UCODE_ID_SDMA0:
2120 *type = GFX_FW_TYPE_SDMA0;
2122 case AMDGPU_UCODE_ID_SDMA1:
2123 *type = GFX_FW_TYPE_SDMA1;
2125 case AMDGPU_UCODE_ID_SDMA2:
2126 *type = GFX_FW_TYPE_SDMA2;
2128 case AMDGPU_UCODE_ID_SDMA3:
2129 *type = GFX_FW_TYPE_SDMA3;
2131 case AMDGPU_UCODE_ID_SDMA4:
2132 *type = GFX_FW_TYPE_SDMA4;
2134 case AMDGPU_UCODE_ID_SDMA5:
2135 *type = GFX_FW_TYPE_SDMA5;
2137 case AMDGPU_UCODE_ID_SDMA6:
2138 *type = GFX_FW_TYPE_SDMA6;
2140 case AMDGPU_UCODE_ID_SDMA7:
2141 *type = GFX_FW_TYPE_SDMA7;
2143 case AMDGPU_UCODE_ID_CP_MES:
2144 *type = GFX_FW_TYPE_CP_MES;
2146 case AMDGPU_UCODE_ID_CP_MES_DATA:
2147 *type = GFX_FW_TYPE_MES_STACK;
2149 case AMDGPU_UCODE_ID_CP_MES1:
2150 *type = GFX_FW_TYPE_CP_MES_KIQ;
2152 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2153 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2155 case AMDGPU_UCODE_ID_CP_CE:
2156 *type = GFX_FW_TYPE_CP_CE;
2158 case AMDGPU_UCODE_ID_CP_PFP:
2159 *type = GFX_FW_TYPE_CP_PFP;
2161 case AMDGPU_UCODE_ID_CP_ME:
2162 *type = GFX_FW_TYPE_CP_ME;
2164 case AMDGPU_UCODE_ID_CP_MEC1:
2165 *type = GFX_FW_TYPE_CP_MEC;
2167 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2168 *type = GFX_FW_TYPE_CP_MEC_ME1;
2170 case AMDGPU_UCODE_ID_CP_MEC2:
2171 *type = GFX_FW_TYPE_CP_MEC;
2173 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2174 *type = GFX_FW_TYPE_CP_MEC_ME2;
2176 case AMDGPU_UCODE_ID_RLC_P:
2177 *type = GFX_FW_TYPE_RLC_P;
2179 case AMDGPU_UCODE_ID_RLC_V:
2180 *type = GFX_FW_TYPE_RLC_V;
2182 case AMDGPU_UCODE_ID_RLC_G:
2183 *type = GFX_FW_TYPE_RLC_G;
2185 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2186 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2188 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2189 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2191 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2192 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2194 case AMDGPU_UCODE_ID_RLC_IRAM:
2195 *type = GFX_FW_TYPE_RLC_IRAM;
2197 case AMDGPU_UCODE_ID_RLC_DRAM:
2198 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2200 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2201 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2203 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2204 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2206 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2207 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2209 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2210 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2212 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2213 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2215 case AMDGPU_UCODE_ID_SMC:
2216 *type = GFX_FW_TYPE_SMU;
2218 case AMDGPU_UCODE_ID_PPTABLE:
2219 *type = GFX_FW_TYPE_PPTABLE;
2221 case AMDGPU_UCODE_ID_UVD:
2222 *type = GFX_FW_TYPE_UVD;
2224 case AMDGPU_UCODE_ID_UVD1:
2225 *type = GFX_FW_TYPE_UVD1;
2227 case AMDGPU_UCODE_ID_VCE:
2228 *type = GFX_FW_TYPE_VCE;
2230 case AMDGPU_UCODE_ID_VCN:
2231 *type = GFX_FW_TYPE_VCN;
2233 case AMDGPU_UCODE_ID_VCN1:
2234 *type = GFX_FW_TYPE_VCN1;
2236 case AMDGPU_UCODE_ID_DMCU_ERAM:
2237 *type = GFX_FW_TYPE_DMCU_ERAM;
2239 case AMDGPU_UCODE_ID_DMCU_INTV:
2240 *type = GFX_FW_TYPE_DMCU_ISR;
2242 case AMDGPU_UCODE_ID_VCN0_RAM:
2243 *type = GFX_FW_TYPE_VCN0_RAM;
2245 case AMDGPU_UCODE_ID_VCN1_RAM:
2246 *type = GFX_FW_TYPE_VCN1_RAM;
2248 case AMDGPU_UCODE_ID_DMCUB:
2249 *type = GFX_FW_TYPE_DMUB;
2251 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2252 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2254 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2255 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2257 case AMDGPU_UCODE_ID_IMU_I:
2258 *type = GFX_FW_TYPE_IMU_I;
2260 case AMDGPU_UCODE_ID_IMU_D:
2261 *type = GFX_FW_TYPE_IMU_D;
2263 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2264 *type = GFX_FW_TYPE_RS64_PFP;
2266 case AMDGPU_UCODE_ID_CP_RS64_ME:
2267 *type = GFX_FW_TYPE_RS64_ME;
2269 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2270 *type = GFX_FW_TYPE_RS64_MEC;
2272 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2273 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2275 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2276 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2278 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2279 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2281 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2282 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2284 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2285 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2287 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2288 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2290 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2291 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2293 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2294 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2296 case AMDGPU_UCODE_ID_MAXIMUM:
2304 static void psp_print_fw_hdr(struct psp_context *psp,
2305 struct amdgpu_firmware_info *ucode)
2307 struct amdgpu_device *adev = psp->adev;
2308 struct common_firmware_header *hdr;
2310 switch (ucode->ucode_id) {
2311 case AMDGPU_UCODE_ID_SDMA0:
2312 case AMDGPU_UCODE_ID_SDMA1:
2313 case AMDGPU_UCODE_ID_SDMA2:
2314 case AMDGPU_UCODE_ID_SDMA3:
2315 case AMDGPU_UCODE_ID_SDMA4:
2316 case AMDGPU_UCODE_ID_SDMA5:
2317 case AMDGPU_UCODE_ID_SDMA6:
2318 case AMDGPU_UCODE_ID_SDMA7:
2319 hdr = (struct common_firmware_header *)
2320 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2321 amdgpu_ucode_print_sdma_hdr(hdr);
2323 case AMDGPU_UCODE_ID_CP_CE:
2324 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2325 amdgpu_ucode_print_gfx_hdr(hdr);
2327 case AMDGPU_UCODE_ID_CP_PFP:
2328 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2329 amdgpu_ucode_print_gfx_hdr(hdr);
2331 case AMDGPU_UCODE_ID_CP_ME:
2332 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2333 amdgpu_ucode_print_gfx_hdr(hdr);
2335 case AMDGPU_UCODE_ID_CP_MEC1:
2336 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2337 amdgpu_ucode_print_gfx_hdr(hdr);
2339 case AMDGPU_UCODE_ID_RLC_G:
2340 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2341 amdgpu_ucode_print_rlc_hdr(hdr);
2343 case AMDGPU_UCODE_ID_SMC:
2344 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2345 amdgpu_ucode_print_smc_hdr(hdr);
2352 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2353 struct psp_gfx_cmd_resp *cmd)
2356 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2358 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2359 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2360 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2361 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2363 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2365 DRM_ERROR("Unknown firmware type\n");
2370 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2371 struct amdgpu_firmware_info *ucode)
2374 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2376 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2378 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2379 psp->fence_buf_mc_addr);
2382 release_psp_cmd_buf(psp);
2387 static int psp_load_smu_fw(struct psp_context *psp)
2390 struct amdgpu_device *adev = psp->adev;
2391 struct amdgpu_firmware_info *ucode =
2392 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2393 struct amdgpu_ras *ras = psp->ras_context.ras;
2396 * Skip SMU FW reloading in case of using BACO for runpm only,
2397 * as SMU is always alive.
2399 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2402 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2405 if ((amdgpu_in_reset(adev) &&
2406 ras && adev->ras_enabled &&
2407 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2408 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2409 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2411 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2415 ret = psp_execute_non_psp_fw_load(psp, ucode);
2418 DRM_ERROR("PSP load smu failed!\n");
2423 static bool fw_load_skip_check(struct psp_context *psp,
2424 struct amdgpu_firmware_info *ucode)
2426 if (!ucode->fw || !ucode->ucode_size)
2429 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2430 (psp_smu_reload_quirk(psp) ||
2431 psp->autoload_supported ||
2432 psp->pmfw_centralized_cstate_management))
2435 if (amdgpu_sriov_vf(psp->adev) &&
2436 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2439 if (psp->autoload_supported &&
2440 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2441 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2442 /* skip mec JT when autoload is enabled */
2448 int psp_load_fw_list(struct psp_context *psp,
2449 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2452 struct amdgpu_firmware_info *ucode;
2454 for (i = 0; i < ucode_count; ++i) {
2455 ucode = ucode_list[i];
2456 psp_print_fw_hdr(psp, ucode);
2457 ret = psp_execute_non_psp_fw_load(psp, ucode);
2464 static int psp_load_non_psp_fw(struct psp_context *psp)
2467 struct amdgpu_firmware_info *ucode;
2468 struct amdgpu_device *adev = psp->adev;
2470 if (psp->autoload_supported &&
2471 !psp->pmfw_centralized_cstate_management) {
2472 ret = psp_load_smu_fw(psp);
2477 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2478 ucode = &adev->firmware.ucode[i];
2480 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2481 !fw_load_skip_check(psp, ucode)) {
2482 ret = psp_load_smu_fw(psp);
2488 if (fw_load_skip_check(psp, ucode))
2491 if (psp->autoload_supported &&
2492 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2493 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2494 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2495 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2496 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2497 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2498 /* PSP only receive one SDMA fw for sienna_cichlid,
2499 * as all four sdma fw are same */
2502 psp_print_fw_hdr(psp, ucode);
2504 ret = psp_execute_non_psp_fw_load(psp, ucode);
2508 /* Start rlc autoload after psp recieved all the gfx firmware */
2509 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2510 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2511 ret = psp_rlc_autoload_start(psp);
2513 DRM_ERROR("Failed to start rlc autoload\n");
2522 static int psp_load_fw(struct amdgpu_device *adev)
2525 struct psp_context *psp = &adev->psp;
2527 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2528 /* should not destroy ring, only stop */
2529 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2531 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2533 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2535 DRM_ERROR("PSP ring init failed!\n");
2540 ret = psp_hw_start(psp);
2544 ret = psp_load_non_psp_fw(psp);
2548 ret = psp_asd_initialize(psp);
2550 DRM_ERROR("PSP load asd failed!\n");
2554 ret = psp_rl_load(adev);
2556 DRM_ERROR("PSP load RL failed!\n");
2560 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2561 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2562 ret = psp_xgmi_initialize(psp, false, true);
2563 /* Warning the XGMI seesion initialize failure
2564 * Instead of stop driver initialization
2567 dev_err(psp->adev->dev,
2568 "XGMI: Failed to initialize XGMI session\n");
2573 ret = psp_ras_initialize(psp);
2575 dev_err(psp->adev->dev,
2576 "RAS: Failed to initialize RAS\n");
2578 ret = psp_hdcp_initialize(psp);
2580 dev_err(psp->adev->dev,
2581 "HDCP: Failed to initialize HDCP\n");
2583 ret = psp_dtm_initialize(psp);
2585 dev_err(psp->adev->dev,
2586 "DTM: Failed to initialize DTM\n");
2588 ret = psp_rap_initialize(psp);
2590 dev_err(psp->adev->dev,
2591 "RAP: Failed to initialize RAP\n");
2593 ret = psp_securedisplay_initialize(psp);
2595 dev_err(psp->adev->dev,
2596 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2602 psp_free_shared_bufs(psp);
2605 * all cleanup jobs (xgmi terminate, ras terminate,
2606 * ring destroy, cmd/fence/fw buffers destory,
2607 * psp->cmd destory) are delayed to psp_hw_fini
2609 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2613 static int psp_hw_init(void *handle)
2616 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2618 mutex_lock(&adev->firmware.mutex);
2620 * This sequence is just used on hw_init only once, no need on
2623 ret = amdgpu_ucode_init_bo(adev);
2627 ret = psp_load_fw(adev);
2629 DRM_ERROR("PSP firmware loading failed\n");
2633 mutex_unlock(&adev->firmware.mutex);
2637 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2638 mutex_unlock(&adev->firmware.mutex);
2642 static int psp_hw_fini(void *handle)
2644 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2645 struct psp_context *psp = &adev->psp;
2648 psp_ras_terminate(psp);
2649 psp_securedisplay_terminate(psp);
2650 psp_rap_terminate(psp);
2651 psp_dtm_terminate(psp);
2652 psp_hdcp_terminate(psp);
2654 if (adev->gmc.xgmi.num_physical_nodes > 1)
2655 psp_xgmi_terminate(psp);
2658 psp_asd_terminate(psp);
2659 psp_tmr_terminate(psp);
2661 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2663 psp_free_shared_bufs(psp);
2668 static int psp_suspend(void *handle)
2671 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2672 struct psp_context *psp = &adev->psp;
2674 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2675 psp->xgmi_context.context.initialized) {
2676 ret = psp_xgmi_terminate(psp);
2678 DRM_ERROR("Failed to terminate xgmi ta\n");
2684 ret = psp_ras_terminate(psp);
2686 DRM_ERROR("Failed to terminate ras ta\n");
2689 ret = psp_hdcp_terminate(psp);
2691 DRM_ERROR("Failed to terminate hdcp ta\n");
2694 ret = psp_dtm_terminate(psp);
2696 DRM_ERROR("Failed to terminate dtm ta\n");
2699 ret = psp_rap_terminate(psp);
2701 DRM_ERROR("Failed to terminate rap ta\n");
2704 ret = psp_securedisplay_terminate(psp);
2706 DRM_ERROR("Failed to terminate securedisplay ta\n");
2711 ret = psp_asd_terminate(psp);
2713 DRM_ERROR("Failed to terminate asd\n");
2717 ret = psp_tmr_terminate(psp);
2719 DRM_ERROR("Failed to terminate tmr\n");
2723 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2725 DRM_ERROR("PSP ring stop failed\n");
2732 static int psp_resume(void *handle)
2735 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2736 struct psp_context *psp = &adev->psp;
2738 DRM_INFO("PSP is resuming...\n");
2740 if (psp->mem_train_ctx.enable_mem_training) {
2741 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2743 DRM_ERROR("Failed to process memory training!\n");
2748 mutex_lock(&adev->firmware.mutex);
2750 ret = psp_hw_start(psp);
2754 ret = psp_load_non_psp_fw(psp);
2758 ret = psp_asd_initialize(psp);
2760 DRM_ERROR("PSP load asd failed!\n");
2764 ret = psp_rl_load(adev);
2766 dev_err(adev->dev, "PSP load RL failed!\n");
2770 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2771 ret = psp_xgmi_initialize(psp, false, true);
2772 /* Warning the XGMI seesion initialize failure
2773 * Instead of stop driver initialization
2776 dev_err(psp->adev->dev,
2777 "XGMI: Failed to initialize XGMI session\n");
2781 ret = psp_ras_initialize(psp);
2783 dev_err(psp->adev->dev,
2784 "RAS: Failed to initialize RAS\n");
2786 ret = psp_hdcp_initialize(psp);
2788 dev_err(psp->adev->dev,
2789 "HDCP: Failed to initialize HDCP\n");
2791 ret = psp_dtm_initialize(psp);
2793 dev_err(psp->adev->dev,
2794 "DTM: Failed to initialize DTM\n");
2796 ret = psp_rap_initialize(psp);
2798 dev_err(psp->adev->dev,
2799 "RAP: Failed to initialize RAP\n");
2801 ret = psp_securedisplay_initialize(psp);
2803 dev_err(psp->adev->dev,
2804 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2807 mutex_unlock(&adev->firmware.mutex);
2812 DRM_ERROR("PSP resume failed\n");
2813 mutex_unlock(&adev->firmware.mutex);
2817 int psp_gpu_reset(struct amdgpu_device *adev)
2821 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2824 mutex_lock(&adev->psp.mutex);
2825 ret = psp_mode1_reset(&adev->psp);
2826 mutex_unlock(&adev->psp.mutex);
2831 int psp_rlc_autoload_start(struct psp_context *psp)
2834 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2836 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2838 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2839 psp->fence_buf_mc_addr);
2841 release_psp_cmd_buf(psp);
2846 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2847 uint64_t cmd_gpu_addr, int cmd_size)
2849 struct amdgpu_firmware_info ucode = {0};
2851 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2852 AMDGPU_UCODE_ID_VCN0_RAM;
2853 ucode.mc_addr = cmd_gpu_addr;
2854 ucode.ucode_size = cmd_size;
2856 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2859 int psp_ring_cmd_submit(struct psp_context *psp,
2860 uint64_t cmd_buf_mc_addr,
2861 uint64_t fence_mc_addr,
2864 unsigned int psp_write_ptr_reg = 0;
2865 struct psp_gfx_rb_frame *write_frame;
2866 struct psp_ring *ring = &psp->km_ring;
2867 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2868 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2869 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2870 struct amdgpu_device *adev = psp->adev;
2871 uint32_t ring_size_dw = ring->ring_size / 4;
2872 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2874 /* KM (GPCOM) prepare write pointer */
2875 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2877 /* Update KM RB frame pointer to new frame */
2878 /* write_frame ptr increments by size of rb_frame in bytes */
2879 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2880 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2881 write_frame = ring_buffer_start;
2883 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2884 /* Check invalid write_frame ptr address */
2885 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2886 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2887 ring_buffer_start, ring_buffer_end, write_frame);
2888 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2892 /* Initialize KM RB frame */
2893 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2895 /* Update KM RB frame */
2896 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2897 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2898 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2899 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2900 write_frame->fence_value = index;
2901 amdgpu_device_flush_hdp(adev, NULL);
2903 /* Update the write Pointer in DWORDs */
2904 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2905 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2909 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2911 struct amdgpu_device *adev = psp->adev;
2912 char fw_name[PSP_FW_NAME_LEN];
2913 const struct psp_firmware_header_v1_0 *asd_hdr;
2916 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2917 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2921 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2922 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2923 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2924 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2925 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2926 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2929 amdgpu_ucode_release(&adev->psp.asd_fw);
2933 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
2935 struct amdgpu_device *adev = psp->adev;
2936 char fw_name[PSP_FW_NAME_LEN];
2937 const struct psp_firmware_header_v1_0 *toc_hdr;
2940 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2941 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
2945 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2946 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2947 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2948 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2949 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2950 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2953 amdgpu_ucode_release(&adev->psp.toc_fw);
2957 static int parse_sos_bin_descriptor(struct psp_context *psp,
2958 const struct psp_fw_bin_desc *desc,
2959 const struct psp_firmware_header_v2_0 *sos_hdr)
2961 uint8_t *ucode_start_addr = NULL;
2963 if (!psp || !desc || !sos_hdr)
2966 ucode_start_addr = (uint8_t *)sos_hdr +
2967 le32_to_cpu(desc->offset_bytes) +
2968 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2970 switch (desc->fw_type) {
2971 case PSP_FW_TYPE_PSP_SOS:
2972 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
2973 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
2974 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
2975 psp->sos.start_addr = ucode_start_addr;
2977 case PSP_FW_TYPE_PSP_SYS_DRV:
2978 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
2979 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
2980 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
2981 psp->sys.start_addr = ucode_start_addr;
2983 case PSP_FW_TYPE_PSP_KDB:
2984 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
2985 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
2986 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
2987 psp->kdb.start_addr = ucode_start_addr;
2989 case PSP_FW_TYPE_PSP_TOC:
2990 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
2991 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
2992 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
2993 psp->toc.start_addr = ucode_start_addr;
2995 case PSP_FW_TYPE_PSP_SPL:
2996 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
2997 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
2998 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
2999 psp->spl.start_addr = ucode_start_addr;
3001 case PSP_FW_TYPE_PSP_RL:
3002 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3003 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3004 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3005 psp->rl.start_addr = ucode_start_addr;
3007 case PSP_FW_TYPE_PSP_SOC_DRV:
3008 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3009 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3010 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3011 psp->soc_drv.start_addr = ucode_start_addr;
3013 case PSP_FW_TYPE_PSP_INTF_DRV:
3014 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3015 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3016 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3017 psp->intf_drv.start_addr = ucode_start_addr;
3019 case PSP_FW_TYPE_PSP_DBG_DRV:
3020 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3021 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3022 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3023 psp->dbg_drv.start_addr = ucode_start_addr;
3025 case PSP_FW_TYPE_PSP_RAS_DRV:
3026 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3027 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3028 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3029 psp->ras_drv.start_addr = ucode_start_addr;
3032 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3039 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3041 const struct psp_firmware_header_v1_0 *sos_hdr;
3042 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3043 uint8_t *ucode_array_start_addr;
3045 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3046 ucode_array_start_addr = (uint8_t *)sos_hdr +
3047 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3049 if (adev->gmc.xgmi.connected_to_cpu ||
3050 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3051 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3052 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3054 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3055 adev->psp.sys.start_addr = ucode_array_start_addr;
3057 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3058 adev->psp.sos.start_addr = ucode_array_start_addr +
3059 le32_to_cpu(sos_hdr->sos.offset_bytes);
3061 /* Load alternate PSP SOS FW */
3062 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3064 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3065 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3067 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3068 adev->psp.sys.start_addr = ucode_array_start_addr +
3069 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3071 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3072 adev->psp.sos.start_addr = ucode_array_start_addr +
3073 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3076 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3077 dev_warn(adev->dev, "PSP SOS FW not available");
3084 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3086 struct amdgpu_device *adev = psp->adev;
3087 char fw_name[PSP_FW_NAME_LEN];
3088 const struct psp_firmware_header_v1_0 *sos_hdr;
3089 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3090 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3091 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3092 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3094 uint8_t *ucode_array_start_addr;
3097 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3098 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3102 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3103 ucode_array_start_addr = (uint8_t *)sos_hdr +
3104 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3105 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3107 switch (sos_hdr->header.header_version_major) {
3109 err = psp_init_sos_base_fw(adev);
3113 if (sos_hdr->header.header_version_minor == 1) {
3114 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3115 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3116 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3117 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3118 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3119 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3120 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3122 if (sos_hdr->header.header_version_minor == 2) {
3123 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3124 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3125 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3126 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3128 if (sos_hdr->header.header_version_minor == 3) {
3129 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3130 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3131 adev->psp.toc.start_addr = ucode_array_start_addr +
3132 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3133 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3134 adev->psp.kdb.start_addr = ucode_array_start_addr +
3135 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3136 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3137 adev->psp.spl.start_addr = ucode_array_start_addr +
3138 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3139 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3140 adev->psp.rl.start_addr = ucode_array_start_addr +
3141 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3145 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3147 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3148 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3153 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3154 err = parse_sos_bin_descriptor(psp,
3155 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3163 "unsupported psp sos firmware\n");
3170 amdgpu_ucode_release(&adev->psp.sos_fw);
3175 static int parse_ta_bin_descriptor(struct psp_context *psp,
3176 const struct psp_fw_bin_desc *desc,
3177 const struct ta_firmware_header_v2_0 *ta_hdr)
3179 uint8_t *ucode_start_addr = NULL;
3181 if (!psp || !desc || !ta_hdr)
3184 ucode_start_addr = (uint8_t *)ta_hdr +
3185 le32_to_cpu(desc->offset_bytes) +
3186 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3188 switch (desc->fw_type) {
3189 case TA_FW_TYPE_PSP_ASD:
3190 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3191 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3192 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3193 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3195 case TA_FW_TYPE_PSP_XGMI:
3196 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3197 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3198 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3200 case TA_FW_TYPE_PSP_RAS:
3201 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3202 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3203 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3205 case TA_FW_TYPE_PSP_HDCP:
3206 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3207 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3208 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3210 case TA_FW_TYPE_PSP_DTM:
3211 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3212 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3213 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3215 case TA_FW_TYPE_PSP_RAP:
3216 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3217 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3218 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3220 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3221 psp->securedisplay_context.context.bin_desc.fw_version =
3222 le32_to_cpu(desc->fw_version);
3223 psp->securedisplay_context.context.bin_desc.size_bytes =
3224 le32_to_cpu(desc->size_bytes);
3225 psp->securedisplay_context.context.bin_desc.start_addr =
3229 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3236 static int parse_ta_v1_microcode(struct psp_context *psp)
3238 const struct ta_firmware_header_v1_0 *ta_hdr;
3239 struct amdgpu_device *adev = psp->adev;
3241 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3243 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3246 adev->psp.xgmi_context.context.bin_desc.fw_version =
3247 le32_to_cpu(ta_hdr->xgmi.fw_version);
3248 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3249 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3250 adev->psp.xgmi_context.context.bin_desc.start_addr =
3252 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3254 adev->psp.ras_context.context.bin_desc.fw_version =
3255 le32_to_cpu(ta_hdr->ras.fw_version);
3256 adev->psp.ras_context.context.bin_desc.size_bytes =
3257 le32_to_cpu(ta_hdr->ras.size_bytes);
3258 adev->psp.ras_context.context.bin_desc.start_addr =
3259 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3260 le32_to_cpu(ta_hdr->ras.offset_bytes);
3262 adev->psp.hdcp_context.context.bin_desc.fw_version =
3263 le32_to_cpu(ta_hdr->hdcp.fw_version);
3264 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3265 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3266 adev->psp.hdcp_context.context.bin_desc.start_addr =
3268 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3270 adev->psp.dtm_context.context.bin_desc.fw_version =
3271 le32_to_cpu(ta_hdr->dtm.fw_version);
3272 adev->psp.dtm_context.context.bin_desc.size_bytes =
3273 le32_to_cpu(ta_hdr->dtm.size_bytes);
3274 adev->psp.dtm_context.context.bin_desc.start_addr =
3275 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3276 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3278 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3279 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3280 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3281 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3282 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3283 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3284 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3286 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3291 static int parse_ta_v2_microcode(struct psp_context *psp)
3293 const struct ta_firmware_header_v2_0 *ta_hdr;
3294 struct amdgpu_device *adev = psp->adev;
3298 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3300 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3303 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3304 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3308 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3309 err = parse_ta_bin_descriptor(psp,
3310 &ta_hdr->ta_fw_bin[ta_index],
3319 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3321 const struct common_firmware_header *hdr;
3322 struct amdgpu_device *adev = psp->adev;
3323 char fw_name[PSP_FW_NAME_LEN];
3326 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3327 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3331 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3332 switch (le16_to_cpu(hdr->header_version_major)) {
3334 err = parse_ta_v1_microcode(psp);
3337 err = parse_ta_v2_microcode(psp);
3340 dev_err(adev->dev, "unsupported TA header version\n");
3345 amdgpu_ucode_release(&adev->psp.ta_fw);
3350 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3352 struct amdgpu_device *adev = psp->adev;
3353 char fw_name[PSP_FW_NAME_LEN];
3354 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3355 struct amdgpu_firmware_info *info = NULL;
3358 if (!amdgpu_sriov_vf(adev)) {
3359 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3363 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3364 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3366 if (err == -ENODEV) {
3367 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3371 dev_err(adev->dev, "fail to initialize cap microcode\n");
3374 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3375 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3376 info->fw = adev->psp.cap_fw;
3377 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3378 adev->psp.cap_fw->data;
3379 adev->firmware.fw_size += ALIGN(
3380 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3381 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3382 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3383 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3388 amdgpu_ucode_release(&adev->psp.cap_fw);
3392 static int psp_set_clockgating_state(void *handle,
3393 enum amd_clockgating_state state)
3398 static int psp_set_powergating_state(void *handle,
3399 enum amd_powergating_state state)
3404 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3405 struct device_attribute *attr,
3408 struct drm_device *ddev = dev_get_drvdata(dev);
3409 struct amdgpu_device *adev = drm_to_adev(ddev);
3413 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3414 DRM_INFO("PSP block is not ready yet.");
3418 mutex_lock(&adev->psp.mutex);
3419 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3420 mutex_unlock(&adev->psp.mutex);
3423 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3427 return sysfs_emit(buf, "%x\n", fw_ver);
3430 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3431 struct device_attribute *attr,
3435 struct drm_device *ddev = dev_get_drvdata(dev);
3436 struct amdgpu_device *adev = drm_to_adev(ddev);
3439 const struct firmware *usbc_pd_fw;
3440 struct amdgpu_bo *fw_buf_bo = NULL;
3441 uint64_t fw_pri_mc_addr;
3442 void *fw_pri_cpu_addr;
3444 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3445 DRM_INFO("PSP block is not ready yet.");
3449 if (!drm_dev_enter(ddev, &idx))
3452 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3453 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3457 /* LFB address which is aligned to 1MB boundary per PSP request */
3458 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3459 AMDGPU_GEM_DOMAIN_VRAM |
3460 AMDGPU_GEM_DOMAIN_GTT,
3461 &fw_buf_bo, &fw_pri_mc_addr,
3466 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3468 mutex_lock(&adev->psp.mutex);
3469 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3470 mutex_unlock(&adev->psp.mutex);
3472 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3475 release_firmware(usbc_pd_fw);
3478 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3486 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3490 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3493 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3494 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3499 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3500 psp_usbc_pd_fw_sysfs_read,
3501 psp_usbc_pd_fw_sysfs_write);
3503 int is_psp_fw_valid(struct psp_bin_desc bin)
3505 return bin.size_bytes;
3508 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3509 struct bin_attribute *bin_attr,
3510 char *buffer, loff_t pos, size_t count)
3512 struct device *dev = kobj_to_dev(kobj);
3513 struct drm_device *ddev = dev_get_drvdata(dev);
3514 struct amdgpu_device *adev = drm_to_adev(ddev);
3516 adev->psp.vbflash_done = false;
3518 /* Safeguard against memory drain */
3519 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3520 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3521 kvfree(adev->psp.vbflash_tmp_buf);
3522 adev->psp.vbflash_tmp_buf = NULL;
3523 adev->psp.vbflash_image_size = 0;
3527 /* TODO Just allocate max for now and optimize to realloc later if needed */
3528 if (!adev->psp.vbflash_tmp_buf) {
3529 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3530 if (!adev->psp.vbflash_tmp_buf)
3534 mutex_lock(&adev->psp.mutex);
3535 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3536 adev->psp.vbflash_image_size += count;
3537 mutex_unlock(&adev->psp.mutex);
3539 dev_info(adev->dev, "VBIOS flash write PSP done");
3544 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3545 struct bin_attribute *bin_attr, char *buffer,
3546 loff_t pos, size_t count)
3548 struct device *dev = kobj_to_dev(kobj);
3549 struct drm_device *ddev = dev_get_drvdata(dev);
3550 struct amdgpu_device *adev = drm_to_adev(ddev);
3551 struct amdgpu_bo *fw_buf_bo = NULL;
3552 uint64_t fw_pri_mc_addr;
3553 void *fw_pri_cpu_addr;
3556 dev_info(adev->dev, "VBIOS flash to PSP started");
3558 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3559 AMDGPU_GPU_PAGE_SIZE,
3560 AMDGPU_GEM_DOMAIN_VRAM,
3567 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3569 mutex_lock(&adev->psp.mutex);
3570 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3571 mutex_unlock(&adev->psp.mutex);
3573 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3576 kvfree(adev->psp.vbflash_tmp_buf);
3577 adev->psp.vbflash_tmp_buf = NULL;
3578 adev->psp.vbflash_image_size = 0;
3581 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3585 dev_info(adev->dev, "VBIOS flash to PSP done");
3589 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3590 struct device_attribute *attr,
3593 struct drm_device *ddev = dev_get_drvdata(dev);
3594 struct amdgpu_device *adev = drm_to_adev(ddev);
3595 uint32_t vbflash_status;
3597 vbflash_status = psp_vbflash_status(&adev->psp);
3598 if (!adev->psp.vbflash_done)
3600 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3603 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3606 static const struct bin_attribute psp_vbflash_bin_attr = {
3607 .attr = {.name = "psp_vbflash", .mode = 0664},
3609 .write = amdgpu_psp_vbflash_write,
3610 .read = amdgpu_psp_vbflash_read,
3613 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3615 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3618 struct psp_context *psp = &adev->psp;
3620 if (amdgpu_sriov_vf(adev))
3623 switch (adev->ip_versions[MP0_HWIP][0]) {
3624 case IP_VERSION(13, 0, 0):
3625 case IP_VERSION(13, 0, 7):
3628 psp_v13_0_set_psp_funcs(psp);
3630 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3632 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3633 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3635 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3642 const struct amd_ip_funcs psp_ip_funcs = {
3644 .early_init = psp_early_init,
3646 .sw_init = psp_sw_init,
3647 .sw_fini = psp_sw_fini,
3648 .hw_init = psp_hw_init,
3649 .hw_fini = psp_hw_fini,
3650 .suspend = psp_suspend,
3651 .resume = psp_resume,
3653 .check_soft_reset = NULL,
3654 .wait_for_idle = NULL,
3656 .set_clockgating_state = psp_set_clockgating_state,
3657 .set_powergating_state = psp_set_powergating_state,
3660 static int psp_sysfs_init(struct amdgpu_device *adev)
3662 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3665 DRM_ERROR("Failed to create USBC PD FW control file!");
3670 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3672 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3673 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3676 static void psp_sysfs_fini(struct amdgpu_device *adev)
3678 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3681 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3683 .type = AMD_IP_BLOCK_TYPE_PSP,
3687 .funcs = &psp_ip_funcs,
3690 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3692 .type = AMD_IP_BLOCK_TYPE_PSP,
3696 .funcs = &psp_ip_funcs,
3699 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3701 .type = AMD_IP_BLOCK_TYPE_PSP,
3705 .funcs = &psp_ip_funcs,
3708 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3709 .type = AMD_IP_BLOCK_TYPE_PSP,
3713 .funcs = &psp_ip_funcs,
3716 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3718 .type = AMD_IP_BLOCK_TYPE_PSP,
3722 .funcs = &psp_ip_funcs,
3725 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3726 .type = AMD_IP_BLOCK_TYPE_PSP,
3730 .funcs = &psp_ip_funcs,
3733 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3734 .type = AMD_IP_BLOCK_TYPE_PSP,
3738 .funcs = &psp_ip_funcs,