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;
151 ret = psp_init_cap_microcode(psp, ucode_prefix);
159 static int psp_early_init(void *handle)
161 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
162 struct psp_context *psp = &adev->psp;
164 switch (adev->ip_versions[MP0_HWIP][0]) {
165 case IP_VERSION(9, 0, 0):
166 psp_v3_1_set_psp_funcs(psp);
167 psp->autoload_supported = false;
169 case IP_VERSION(10, 0, 0):
170 case IP_VERSION(10, 0, 1):
171 psp_v10_0_set_psp_funcs(psp);
172 psp->autoload_supported = false;
174 case IP_VERSION(11, 0, 2):
175 case IP_VERSION(11, 0, 4):
176 psp_v11_0_set_psp_funcs(psp);
177 psp->autoload_supported = false;
179 case IP_VERSION(11, 0, 0):
180 case IP_VERSION(11, 0, 5):
181 case IP_VERSION(11, 0, 9):
182 case IP_VERSION(11, 0, 7):
183 case IP_VERSION(11, 0, 11):
184 case IP_VERSION(11, 5, 0):
185 case IP_VERSION(11, 0, 12):
186 case IP_VERSION(11, 0, 13):
187 psp_v11_0_set_psp_funcs(psp);
188 psp->autoload_supported = true;
190 case IP_VERSION(11, 0, 3):
191 case IP_VERSION(12, 0, 1):
192 psp_v12_0_set_psp_funcs(psp);
194 case IP_VERSION(13, 0, 2):
195 case IP_VERSION(13, 0, 6):
196 psp_v13_0_set_psp_funcs(psp);
198 case IP_VERSION(13, 0, 1):
199 case IP_VERSION(13, 0, 3):
200 case IP_VERSION(13, 0, 5):
201 case IP_VERSION(13, 0, 8):
202 case IP_VERSION(13, 0, 10):
203 case IP_VERSION(13, 0, 11):
204 psp_v13_0_set_psp_funcs(psp);
205 psp->autoload_supported = true;
207 case IP_VERSION(11, 0, 8):
208 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
209 psp_v11_0_8_set_psp_funcs(psp);
210 psp->autoload_supported = false;
213 case IP_VERSION(13, 0, 0):
214 case IP_VERSION(13, 0, 7):
215 psp_v13_0_set_psp_funcs(psp);
216 psp->autoload_supported = true;
218 case IP_VERSION(13, 0, 4):
219 psp_v13_0_4_set_psp_funcs(psp);
220 psp->autoload_supported = true;
228 psp_check_pmfw_centralized_cstate_management(psp);
230 if (amdgpu_sriov_vf(adev))
231 return psp_init_sriov_microcode(psp);
233 return psp_init_microcode(psp);
236 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
238 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
239 &mem_ctx->shared_buf);
240 mem_ctx->shared_bo = NULL;
243 static void psp_free_shared_bufs(struct psp_context *psp)
248 /* free TMR memory buffer */
249 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
250 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
253 /* free xgmi shared memory */
254 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
256 /* free ras shared memory */
257 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
259 /* free hdcp shared memory */
260 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
262 /* free dtm shared memory */
263 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
265 /* free rap shared memory */
266 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
268 /* free securedisplay shared memory */
269 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
274 static void psp_memory_training_fini(struct psp_context *psp)
276 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
278 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
279 kfree(ctx->sys_cache);
280 ctx->sys_cache = NULL;
283 static int psp_memory_training_init(struct psp_context *psp)
286 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
288 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
289 DRM_DEBUG("memory training is not supported!\n");
293 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
294 if (ctx->sys_cache == NULL) {
295 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
300 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
301 ctx->train_data_size,
302 ctx->p2c_train_data_offset,
303 ctx->c2p_train_data_offset);
304 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
308 psp_memory_training_fini(psp);
313 * Helper funciton to query psp runtime database entry
315 * @adev: amdgpu_device pointer
316 * @entry_type: the type of psp runtime database entry
317 * @db_entry: runtime database entry pointer
319 * Return false if runtime database doesn't exit or entry is invalid
320 * or true if the specific database entry is found, and copy to @db_entry
322 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
323 enum psp_runtime_entry_type entry_type,
326 uint64_t db_header_pos, db_dir_pos;
327 struct psp_runtime_data_header db_header = {0};
328 struct psp_runtime_data_directory db_dir = {0};
332 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
333 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
335 /* read runtime db header from vram */
336 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
337 sizeof(struct psp_runtime_data_header), false);
339 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
340 /* runtime db doesn't exist, exit */
341 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
345 /* read runtime database entry from vram */
346 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
347 sizeof(struct psp_runtime_data_directory), false);
349 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
350 /* invalid db entry count, exit */
351 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
355 /* look up for requested entry type */
356 for (i = 0; i < db_dir.entry_count && !ret; i++) {
357 if (db_dir.entry_list[i].entry_type == entry_type) {
358 switch (entry_type) {
359 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
360 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
361 /* invalid db entry size */
362 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
365 /* read runtime database entry */
366 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
367 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
370 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
371 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
372 /* invalid db entry size */
373 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
376 /* read runtime database entry */
377 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
378 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
391 static int psp_sw_init(void *handle)
393 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
394 struct psp_context *psp = &adev->psp;
396 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
397 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
398 struct psp_runtime_scpm_entry scpm_entry;
400 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
402 DRM_ERROR("Failed to allocate memory to command buffer!\n");
406 adev->psp.xgmi_context.supports_extended_data =
407 !adev->gmc.xgmi.connected_to_cpu &&
408 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
410 memset(&scpm_entry, 0, sizeof(scpm_entry));
411 if ((psp_get_runtime_db_entry(adev,
412 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
414 (scpm_entry.scpm_status != SCPM_DISABLE)) {
415 adev->scpm_enabled = true;
416 adev->scpm_status = scpm_entry.scpm_status;
418 adev->scpm_enabled = false;
419 adev->scpm_status = SCPM_DISABLE;
422 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
424 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
425 if (psp_get_runtime_db_entry(adev,
426 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
428 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
429 if ((psp->boot_cfg_bitmask) &
430 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
431 /* If psp runtime database exists, then
432 * only enable two stage memory training
433 * when TWO_STAGE_DRAM_TRAINING bit is set
434 * in runtime database */
435 mem_training_ctx->enable_mem_training = true;
439 /* If psp runtime database doesn't exist or
440 * is invalid, force enable two stage memory
442 mem_training_ctx->enable_mem_training = true;
445 if (mem_training_ctx->enable_mem_training) {
446 ret = psp_memory_training_init(psp);
448 DRM_ERROR("Failed to initialize memory training!\n");
452 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
454 DRM_ERROR("Failed to process memory training!\n");
459 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
460 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
461 ret = psp_sysfs_init(adev);
466 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
467 amdgpu_sriov_vf(adev) ?
468 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
470 &psp->fw_pri_mc_addr,
475 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
476 AMDGPU_GEM_DOMAIN_VRAM,
478 &psp->fence_buf_mc_addr,
483 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
484 AMDGPU_GEM_DOMAIN_VRAM,
485 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
486 (void **)&psp->cmd_buf_mem);
493 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
494 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
496 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
497 &psp->fence_buf_mc_addr, &psp->fence_buf);
501 static int psp_sw_fini(void *handle)
503 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
504 struct psp_context *psp = &adev->psp;
505 struct psp_gfx_cmd_resp *cmd = psp->cmd;
507 psp_memory_training_fini(psp);
509 amdgpu_ucode_release(&psp->sos_fw);
510 amdgpu_ucode_release(&psp->asd_fw);
511 amdgpu_ucode_release(&psp->ta_fw);
512 amdgpu_ucode_release(&psp->cap_fw);
513 amdgpu_ucode_release(&psp->toc_fw);
515 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
516 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
517 psp_sysfs_fini(adev);
522 psp_free_shared_bufs(psp);
524 if (psp->km_ring.ring_mem)
525 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
526 &psp->km_ring.ring_mem_mc_addr,
527 (void **)&psp->km_ring.ring_mem);
529 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
530 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
531 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
532 &psp->fence_buf_mc_addr, &psp->fence_buf);
533 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
534 (void **)&psp->cmd_buf_mem);
539 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
540 uint32_t reg_val, uint32_t mask, bool check_changed)
544 struct amdgpu_device *adev = psp->adev;
546 if (psp->adev->no_hw_access)
549 for (i = 0; i < adev->usec_timeout; i++) {
550 val = RREG32(reg_index);
555 if ((val & mask) == reg_val)
564 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
567 case GFX_CMD_ID_LOAD_TA:
569 case GFX_CMD_ID_UNLOAD_TA:
571 case GFX_CMD_ID_INVOKE_CMD:
573 case GFX_CMD_ID_LOAD_ASD:
575 case GFX_CMD_ID_SETUP_TMR:
577 case GFX_CMD_ID_LOAD_IP_FW:
579 case GFX_CMD_ID_DESTROY_TMR:
580 return "DESTROY_TMR";
581 case GFX_CMD_ID_SAVE_RESTORE:
582 return "SAVE_RESTORE_IP_FW";
583 case GFX_CMD_ID_SETUP_VMR:
585 case GFX_CMD_ID_DESTROY_VMR:
586 return "DESTROY_VMR";
587 case GFX_CMD_ID_PROG_REG:
589 case GFX_CMD_ID_GET_FW_ATTESTATION:
590 return "GET_FW_ATTESTATION";
591 case GFX_CMD_ID_LOAD_TOC:
592 return "ID_LOAD_TOC";
593 case GFX_CMD_ID_AUTOLOAD_RLC:
594 return "AUTOLOAD_RLC";
595 case GFX_CMD_ID_BOOT_CFG:
598 return "UNKNOWN CMD";
603 psp_cmd_submit_buf(struct psp_context *psp,
604 struct amdgpu_firmware_info *ucode,
605 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
610 bool ras_intr = false;
611 bool skip_unsupport = false;
613 if (psp->adev->no_hw_access)
616 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
618 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
620 index = atomic_inc_return(&psp->fence_value);
621 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
623 atomic_dec(&psp->fence_value);
627 amdgpu_device_invalidate_hdp(psp->adev, NULL);
628 while (*((unsigned int *)psp->fence_buf) != index) {
632 * Shouldn't wait for timeout when err_event_athub occurs,
633 * because gpu reset thread triggered and lock resource should
634 * be released for psp resume sequence.
636 ras_intr = amdgpu_ras_intr_triggered();
639 usleep_range(10, 100);
640 amdgpu_device_invalidate_hdp(psp->adev, NULL);
643 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
644 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
645 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
647 memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
649 /* In some cases, psp response status is not 0 even there is no
650 * problem while the command is submitted. Some version of PSP FW
651 * doesn't write 0 to that field.
652 * So here we would like to only print a warning instead of an error
653 * during psp initialization to avoid breaking hw_init and it doesn't
656 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
658 DRM_WARN("failed to load ucode %s(0x%X) ",
659 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
660 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
661 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
662 psp->cmd_buf_mem->resp.status);
663 /* If any firmware (including CAP) load fails under SRIOV, it should
664 * return failure to stop the VF from initializing.
665 * Also return failure in case of timeout
667 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
674 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
675 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
682 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
684 struct psp_gfx_cmd_resp *cmd = psp->cmd;
686 mutex_lock(&psp->mutex);
688 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
693 static void release_psp_cmd_buf(struct psp_context *psp)
695 mutex_unlock(&psp->mutex);
698 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
699 struct psp_gfx_cmd_resp *cmd,
700 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
702 struct amdgpu_device *adev = psp->adev;
703 uint32_t size = amdgpu_bo_size(tmr_bo);
704 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
706 if (amdgpu_sriov_vf(psp->adev))
707 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
709 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
710 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
711 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
712 cmd->cmd.cmd_setup_tmr.buf_size = size;
713 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
714 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
715 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
718 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
719 uint64_t pri_buf_mc, uint32_t size)
721 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
722 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
723 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
724 cmd->cmd.cmd_load_toc.toc_size = size;
727 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
728 static int psp_load_toc(struct psp_context *psp,
732 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
734 /* Copy toc to psp firmware private buffer */
735 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
737 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
739 ret = psp_cmd_submit_buf(psp, NULL, cmd,
740 psp->fence_buf_mc_addr);
742 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
744 release_psp_cmd_buf(psp);
749 /* Set up Trusted Memory Region */
750 static int psp_tmr_init(struct psp_context *psp)
758 * According to HW engineer, they prefer the TMR address be "naturally
759 * aligned" , e.g. the start address be an integer divide of TMR size.
761 * Note: this memory need be reserved till the driver
764 tmr_size = PSP_TMR_SIZE(psp->adev);
766 /* For ASICs support RLC autoload, psp will parse the toc
767 * and calculate the total size of TMR needed */
768 if (!amdgpu_sriov_vf(psp->adev) &&
769 psp->toc.start_addr &&
770 psp->toc.size_bytes &&
772 ret = psp_load_toc(psp, &tmr_size);
774 DRM_ERROR("Failed to load toc\n");
780 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
781 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
783 AMDGPU_HAS_VRAM(psp->adev) ?
784 AMDGPU_GEM_DOMAIN_VRAM :
785 AMDGPU_GEM_DOMAIN_GTT,
786 &psp->tmr_bo, &psp->tmr_mc_addr,
793 static bool psp_skip_tmr(struct psp_context *psp)
795 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
796 case IP_VERSION(11, 0, 9):
797 case IP_VERSION(11, 0, 7):
798 case IP_VERSION(13, 0, 2):
799 case IP_VERSION(13, 0, 10):
806 static int psp_tmr_load(struct psp_context *psp)
809 struct psp_gfx_cmd_resp *cmd;
811 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
812 * Already set up by host driver.
814 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
817 cmd = acquire_psp_cmd_buf(psp);
819 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
820 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
821 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
823 ret = psp_cmd_submit_buf(psp, NULL, cmd,
824 psp->fence_buf_mc_addr);
826 release_psp_cmd_buf(psp);
831 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
832 struct psp_gfx_cmd_resp *cmd)
834 if (amdgpu_sriov_vf(psp->adev))
835 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
837 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
840 static int psp_tmr_unload(struct psp_context *psp)
843 struct psp_gfx_cmd_resp *cmd;
845 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
846 * as TMR is not loaded at all
848 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
851 cmd = acquire_psp_cmd_buf(psp);
853 psp_prep_tmr_unload_cmd_buf(psp, cmd);
854 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
856 ret = psp_cmd_submit_buf(psp, NULL, cmd,
857 psp->fence_buf_mc_addr);
859 release_psp_cmd_buf(psp);
864 static int psp_tmr_terminate(struct psp_context *psp)
866 return psp_tmr_unload(psp);
869 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
870 uint64_t *output_ptr)
873 struct psp_gfx_cmd_resp *cmd;
878 if (amdgpu_sriov_vf(psp->adev))
881 cmd = acquire_psp_cmd_buf(psp);
883 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
885 ret = psp_cmd_submit_buf(psp, NULL, cmd,
886 psp->fence_buf_mc_addr);
889 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
890 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
893 release_psp_cmd_buf(psp);
898 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
900 struct psp_context *psp = &adev->psp;
901 struct psp_gfx_cmd_resp *cmd;
904 if (amdgpu_sriov_vf(adev))
907 cmd = acquire_psp_cmd_buf(psp);
909 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
910 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
912 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
915 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
918 release_psp_cmd_buf(psp);
923 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
926 struct psp_context *psp = &adev->psp;
927 struct psp_gfx_cmd_resp *cmd;
929 if (amdgpu_sriov_vf(adev))
932 cmd = acquire_psp_cmd_buf(psp);
934 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
935 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
936 cmd->cmd.boot_cfg.boot_config = boot_cfg;
937 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
939 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
941 release_psp_cmd_buf(psp);
946 static int psp_rl_load(struct amdgpu_device *adev)
949 struct psp_context *psp = &adev->psp;
950 struct psp_gfx_cmd_resp *cmd;
952 if (!is_psp_fw_valid(psp->rl))
955 cmd = acquire_psp_cmd_buf(psp);
957 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
958 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
960 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
961 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
962 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
963 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
964 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
966 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
968 release_psp_cmd_buf(psp);
973 static int psp_asd_initialize(struct psp_context *psp)
977 /* If PSP version doesn't match ASD version, asd loading will be failed.
978 * add workaround to bypass it for sriov now.
979 * TODO: add version check to make it common
981 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
984 psp->asd_context.mem_context.shared_mc_addr = 0;
985 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
986 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
988 ret = psp_ta_load(psp, &psp->asd_context);
990 psp->asd_context.initialized = true;
995 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
998 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
999 cmd->cmd.cmd_unload_ta.session_id = session_id;
1002 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1005 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1007 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1009 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1011 context->resp_status = cmd->resp.status;
1013 release_psp_cmd_buf(psp);
1018 static int psp_asd_terminate(struct psp_context *psp)
1022 if (amdgpu_sriov_vf(psp->adev))
1025 if (!psp->asd_context.initialized)
1028 ret = psp_ta_unload(psp, &psp->asd_context);
1030 psp->asd_context.initialized = false;
1035 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1036 uint32_t id, uint32_t value)
1038 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1039 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1040 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1043 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1046 struct psp_gfx_cmd_resp *cmd;
1049 if (reg >= PSP_REG_LAST)
1052 cmd = acquire_psp_cmd_buf(psp);
1054 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1055 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1057 DRM_ERROR("PSP failed to program reg id %d", reg);
1059 release_psp_cmd_buf(psp);
1064 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1066 struct ta_context *context)
1068 cmd->cmd_id = context->ta_load_type;
1069 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1070 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1071 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1073 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1074 lower_32_bits(context->mem_context.shared_mc_addr);
1075 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1076 upper_32_bits(context->mem_context.shared_mc_addr);
1077 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1080 int psp_ta_init_shared_buf(struct psp_context *psp,
1081 struct ta_mem_context *mem_ctx)
1084 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1085 * physical) for ta to host memory
1087 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1088 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1089 AMDGPU_GEM_DOMAIN_GTT,
1090 &mem_ctx->shared_bo,
1091 &mem_ctx->shared_mc_addr,
1092 &mem_ctx->shared_buf);
1095 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1097 uint32_t session_id)
1099 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1100 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1101 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1104 int psp_ta_invoke(struct psp_context *psp,
1106 struct ta_context *context)
1109 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1111 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1113 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1114 psp->fence_buf_mc_addr);
1116 context->resp_status = cmd->resp.status;
1118 release_psp_cmd_buf(psp);
1123 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1126 struct psp_gfx_cmd_resp *cmd;
1128 cmd = acquire_psp_cmd_buf(psp);
1130 psp_copy_fw(psp, context->bin_desc.start_addr,
1131 context->bin_desc.size_bytes);
1133 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1135 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1136 psp->fence_buf_mc_addr);
1138 context->resp_status = cmd->resp.status;
1141 context->session_id = cmd->resp.session_id;
1143 release_psp_cmd_buf(psp);
1148 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1150 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1153 int psp_xgmi_terminate(struct psp_context *psp)
1156 struct amdgpu_device *adev = psp->adev;
1158 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1159 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1160 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1161 adev->gmc.xgmi.connected_to_cpu))
1164 if (!psp->xgmi_context.context.initialized)
1167 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1169 psp->xgmi_context.context.initialized = false;
1174 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1176 struct ta_xgmi_shared_memory *xgmi_cmd;
1180 !psp->xgmi_context.context.bin_desc.size_bytes ||
1181 !psp->xgmi_context.context.bin_desc.start_addr)
1187 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1188 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1190 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1191 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1197 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1199 psp->xgmi_context.context.initialized = true;
1204 /* Initialize XGMI session */
1205 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1206 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1207 xgmi_cmd->flag_extend_link_record = set_extended_data;
1208 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1210 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1215 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1217 struct ta_xgmi_shared_memory *xgmi_cmd;
1220 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1221 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1223 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1225 /* Invoke xgmi ta to get hive id */
1226 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1230 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1235 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1237 struct ta_xgmi_shared_memory *xgmi_cmd;
1240 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1241 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1243 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1245 /* Invoke xgmi ta to get the node id */
1246 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1250 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1255 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1257 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1258 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1262 * Chips that support extended topology information require the driver to
1263 * reflect topology information in the opposite direction. This is
1264 * because the TA has already exceeded its link record limit and if the
1265 * TA holds bi-directional information, the driver would have to do
1266 * multiple fetches instead of just two.
1268 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1269 struct psp_xgmi_node_info node_info)
1271 struct amdgpu_device *mirror_adev;
1272 struct amdgpu_hive_info *hive;
1273 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1274 uint64_t dst_node_id = node_info.node_id;
1275 uint8_t dst_num_hops = node_info.num_hops;
1276 uint8_t dst_num_links = node_info.num_links;
1278 hive = amdgpu_get_xgmi_hive(psp->adev);
1279 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1280 struct psp_xgmi_topology_info *mirror_top_info;
1283 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1286 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1287 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1288 if (mirror_top_info->nodes[j].node_id != src_node_id)
1291 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1293 * prevent 0 num_links value re-reflection since reflection
1294 * criteria is based on num_hops (direct or indirect).
1298 mirror_top_info->nodes[j].num_links = dst_num_links;
1306 amdgpu_put_xgmi_hive(hive);
1309 int psp_xgmi_get_topology_info(struct psp_context *psp,
1311 struct psp_xgmi_topology_info *topology,
1312 bool get_extended_data)
1314 struct ta_xgmi_shared_memory *xgmi_cmd;
1315 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1316 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1320 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1323 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1324 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1325 xgmi_cmd->flag_extend_link_record = get_extended_data;
1327 /* Fill in the shared memory with topology information as input */
1328 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1329 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1330 topology_info_input->num_nodes = number_devices;
1332 for (i = 0; i < topology_info_input->num_nodes; i++) {
1333 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1334 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1335 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1336 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1339 /* Invoke xgmi ta to get the topology information */
1340 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1344 /* Read the output topology information from the shared memory */
1345 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1346 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1347 for (i = 0; i < topology->num_nodes; i++) {
1348 /* extended data will either be 0 or equal to non-extended data */
1349 if (topology_info_output->nodes[i].num_hops)
1350 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1352 /* non-extended data gets everything here so no need to update */
1353 if (!get_extended_data) {
1354 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1355 topology->nodes[i].is_sharing_enabled =
1356 topology_info_output->nodes[i].is_sharing_enabled;
1357 topology->nodes[i].sdma_engine =
1358 topology_info_output->nodes[i].sdma_engine;
1363 /* Invoke xgmi ta again to get the link information */
1364 if (psp_xgmi_peer_link_info_supported(psp)) {
1365 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1367 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1369 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1374 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1375 for (i = 0; i < topology->num_nodes; i++) {
1376 /* accumulate num_links on extended data */
1377 topology->nodes[i].num_links = get_extended_data ?
1378 topology->nodes[i].num_links +
1379 link_info_output->nodes[i].num_links :
1380 link_info_output->nodes[i].num_links;
1382 /* reflect the topology information for bi-directionality */
1383 if (psp->xgmi_context.supports_extended_data &&
1384 get_extended_data && topology->nodes[i].num_hops)
1385 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1392 int psp_xgmi_set_topology_info(struct psp_context *psp,
1394 struct psp_xgmi_topology_info *topology)
1396 struct ta_xgmi_shared_memory *xgmi_cmd;
1397 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1400 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1403 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1404 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1406 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1407 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1408 topology_info_input->num_nodes = number_devices;
1410 for (i = 0; i < topology_info_input->num_nodes; i++) {
1411 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1412 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1413 topology_info_input->nodes[i].is_sharing_enabled = 1;
1414 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1417 /* Invoke xgmi ta to set topology information */
1418 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1422 static void psp_ras_ta_check_status(struct psp_context *psp)
1424 struct ta_ras_shared_memory *ras_cmd =
1425 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1427 switch (ras_cmd->ras_status) {
1428 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1429 dev_warn(psp->adev->dev,
1430 "RAS WARNING: cmd failed due to unsupported ip\n");
1432 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1433 dev_warn(psp->adev->dev,
1434 "RAS WARNING: cmd failed due to unsupported error injection\n");
1436 case TA_RAS_STATUS__SUCCESS:
1438 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1439 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1440 dev_warn(psp->adev->dev,
1441 "RAS WARNING: Inject error to critical region is not allowed\n");
1444 dev_warn(psp->adev->dev,
1445 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1450 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1452 struct ta_ras_shared_memory *ras_cmd;
1455 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1458 * TODO: bypass the loading in sriov for now
1460 if (amdgpu_sriov_vf(psp->adev))
1463 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1465 if (amdgpu_ras_intr_triggered())
1468 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1469 DRM_WARN("RAS: Unsupported Interface");
1474 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1475 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1477 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1478 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1479 dev_warn(psp->adev->dev,
1480 "RAS internal register access blocked\n");
1482 psp_ras_ta_check_status(psp);
1488 int psp_ras_enable_features(struct psp_context *psp,
1489 union ta_ras_cmd_input *info, bool enable)
1491 struct ta_ras_shared_memory *ras_cmd;
1494 if (!psp->ras_context.context.initialized)
1497 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1498 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1501 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1503 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1505 ras_cmd->ras_in_message = *info;
1507 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1514 int psp_ras_terminate(struct psp_context *psp)
1519 * TODO: bypass the terminate in sriov for now
1521 if (amdgpu_sriov_vf(psp->adev))
1524 if (!psp->ras_context.context.initialized)
1527 ret = psp_ta_unload(psp, &psp->ras_context.context);
1529 psp->ras_context.context.initialized = false;
1534 int psp_ras_initialize(struct psp_context *psp)
1537 uint32_t boot_cfg = 0xFF;
1538 struct amdgpu_device *adev = psp->adev;
1539 struct ta_ras_shared_memory *ras_cmd;
1542 * TODO: bypass the initialize in sriov for now
1544 if (amdgpu_sriov_vf(adev))
1547 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1548 !adev->psp.ras_context.context.bin_desc.start_addr) {
1549 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1553 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1554 /* query GECC enablement status from boot config
1555 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1557 ret = psp_boot_config_get(adev, &boot_cfg);
1559 dev_warn(adev->dev, "PSP get boot config failed\n");
1561 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1563 dev_info(adev->dev, "GECC is disabled\n");
1565 /* disable GECC in next boot cycle if ras is
1566 * disabled by module parameter amdgpu_ras_enable
1567 * and/or amdgpu_ras_mask, or boot_config_get call
1570 ret = psp_boot_config_set(adev, 0);
1572 dev_warn(adev->dev, "PSP set boot config failed\n");
1574 dev_warn(adev->dev, "GECC will be disabled in next boot cycle if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1577 if (boot_cfg == 1) {
1578 dev_info(adev->dev, "GECC is enabled\n");
1580 /* enable GECC in next boot cycle if it is disabled
1581 * in boot config, or force enable GECC if failed to
1582 * get boot configuration
1584 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1586 dev_warn(adev->dev, "PSP set boot config failed\n");
1588 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1593 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1594 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1596 if (!psp->ras_context.context.mem_context.shared_buf) {
1597 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1602 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1603 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1605 if (amdgpu_ras_is_poison_mode_supported(adev))
1606 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1607 if (!adev->gmc.xgmi.connected_to_cpu)
1608 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1610 ret = psp_ta_load(psp, &psp->ras_context.context);
1612 if (!ret && !ras_cmd->ras_status)
1613 psp->ras_context.context.initialized = true;
1615 if (ras_cmd->ras_status)
1616 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1618 /* fail to load RAS TA */
1619 psp->ras_context.context.initialized = false;
1625 int psp_ras_trigger_error(struct psp_context *psp,
1626 struct ta_ras_trigger_error_input *info)
1628 struct ta_ras_shared_memory *ras_cmd;
1631 if (!psp->ras_context.context.initialized)
1634 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1635 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1637 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1638 ras_cmd->ras_in_message.trigger_error = *info;
1640 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1644 /* If err_event_athub occurs error inject was successful, however
1645 return status from TA is no long reliable */
1646 if (amdgpu_ras_intr_triggered())
1649 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1651 else if (ras_cmd->ras_status)
1659 static int psp_hdcp_initialize(struct psp_context *psp)
1664 * TODO: bypass the initialize in sriov for now
1666 if (amdgpu_sriov_vf(psp->adev))
1669 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1670 !psp->hdcp_context.context.bin_desc.start_addr) {
1671 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1675 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1676 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1678 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1679 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1684 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1686 psp->hdcp_context.context.initialized = true;
1687 mutex_init(&psp->hdcp_context.mutex);
1693 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1696 * TODO: bypass the loading in sriov for now
1698 if (amdgpu_sriov_vf(psp->adev))
1701 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1704 static int psp_hdcp_terminate(struct psp_context *psp)
1709 * TODO: bypass the terminate in sriov for now
1711 if (amdgpu_sriov_vf(psp->adev))
1714 if (!psp->hdcp_context.context.initialized)
1717 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1719 psp->hdcp_context.context.initialized = false;
1726 static int psp_dtm_initialize(struct psp_context *psp)
1731 * TODO: bypass the initialize in sriov for now
1733 if (amdgpu_sriov_vf(psp->adev))
1736 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1737 !psp->dtm_context.context.bin_desc.start_addr) {
1738 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1742 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1743 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1745 if (!psp->dtm_context.context.mem_context.shared_buf) {
1746 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1751 ret = psp_ta_load(psp, &psp->dtm_context.context);
1753 psp->dtm_context.context.initialized = true;
1754 mutex_init(&psp->dtm_context.mutex);
1760 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1763 * TODO: bypass the loading in sriov for now
1765 if (amdgpu_sriov_vf(psp->adev))
1768 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1771 static int psp_dtm_terminate(struct psp_context *psp)
1776 * TODO: bypass the terminate in sriov for now
1778 if (amdgpu_sriov_vf(psp->adev))
1781 if (!psp->dtm_context.context.initialized)
1784 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1786 psp->dtm_context.context.initialized = false;
1793 static int psp_rap_initialize(struct psp_context *psp)
1796 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1799 * TODO: bypass the initialize in sriov for now
1801 if (amdgpu_sriov_vf(psp->adev))
1804 if (!psp->rap_context.context.bin_desc.size_bytes ||
1805 !psp->rap_context.context.bin_desc.start_addr) {
1806 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1810 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1811 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1813 if (!psp->rap_context.context.mem_context.shared_buf) {
1814 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1819 ret = psp_ta_load(psp, &psp->rap_context.context);
1821 psp->rap_context.context.initialized = true;
1822 mutex_init(&psp->rap_context.mutex);
1826 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1827 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1828 psp_rap_terminate(psp);
1829 /* free rap shared memory */
1830 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1832 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1841 static int psp_rap_terminate(struct psp_context *psp)
1845 if (!psp->rap_context.context.initialized)
1848 ret = psp_ta_unload(psp, &psp->rap_context.context);
1850 psp->rap_context.context.initialized = false;
1855 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1857 struct ta_rap_shared_memory *rap_cmd;
1860 if (!psp->rap_context.context.initialized)
1863 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1864 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1867 mutex_lock(&psp->rap_context.mutex);
1869 rap_cmd = (struct ta_rap_shared_memory *)
1870 psp->rap_context.context.mem_context.shared_buf;
1871 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1873 rap_cmd->cmd_id = ta_cmd_id;
1874 rap_cmd->validation_method_id = METHOD_A;
1876 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1881 *status = rap_cmd->rap_status;
1884 mutex_unlock(&psp->rap_context.mutex);
1890 /* securedisplay start */
1891 static int psp_securedisplay_initialize(struct psp_context *psp)
1894 struct ta_securedisplay_cmd *securedisplay_cmd;
1897 * TODO: bypass the initialize in sriov for now
1899 if (amdgpu_sriov_vf(psp->adev))
1902 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1903 !psp->securedisplay_context.context.bin_desc.start_addr) {
1904 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1908 psp->securedisplay_context.context.mem_context.shared_mem_size =
1909 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1910 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1912 if (!psp->securedisplay_context.context.initialized) {
1913 ret = psp_ta_init_shared_buf(psp,
1914 &psp->securedisplay_context.context.mem_context);
1919 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1921 psp->securedisplay_context.context.initialized = true;
1922 mutex_init(&psp->securedisplay_context.mutex);
1926 mutex_lock(&psp->securedisplay_context.mutex);
1928 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1929 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1931 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1933 mutex_unlock(&psp->securedisplay_context.mutex);
1936 psp_securedisplay_terminate(psp);
1937 /* free securedisplay shared memory */
1938 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1939 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1943 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1944 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1945 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1946 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1952 static int psp_securedisplay_terminate(struct psp_context *psp)
1957 * TODO:bypass the terminate in sriov for now
1959 if (amdgpu_sriov_vf(psp->adev))
1962 if (!psp->securedisplay_context.context.initialized)
1965 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1967 psp->securedisplay_context.context.initialized = false;
1972 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1976 if (!psp->securedisplay_context.context.initialized)
1979 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1980 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1983 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1987 /* SECUREDISPLAY end */
1989 static int psp_hw_start(struct psp_context *psp)
1991 struct amdgpu_device *adev = psp->adev;
1994 if (!amdgpu_sriov_vf(adev)) {
1995 if ((is_psp_fw_valid(psp->kdb)) &&
1996 (psp->funcs->bootloader_load_kdb != NULL)) {
1997 ret = psp_bootloader_load_kdb(psp);
1999 DRM_ERROR("PSP load kdb failed!\n");
2004 if ((is_psp_fw_valid(psp->spl)) &&
2005 (psp->funcs->bootloader_load_spl != NULL)) {
2006 ret = psp_bootloader_load_spl(psp);
2008 DRM_ERROR("PSP load spl failed!\n");
2013 if ((is_psp_fw_valid(psp->sys)) &&
2014 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2015 ret = psp_bootloader_load_sysdrv(psp);
2017 DRM_ERROR("PSP load sys drv failed!\n");
2022 if ((is_psp_fw_valid(psp->soc_drv)) &&
2023 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2024 ret = psp_bootloader_load_soc_drv(psp);
2026 DRM_ERROR("PSP load soc drv failed!\n");
2031 if ((is_psp_fw_valid(psp->intf_drv)) &&
2032 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2033 ret = psp_bootloader_load_intf_drv(psp);
2035 DRM_ERROR("PSP load intf drv failed!\n");
2040 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2041 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2042 ret = psp_bootloader_load_dbg_drv(psp);
2044 DRM_ERROR("PSP load dbg drv failed!\n");
2049 if ((is_psp_fw_valid(psp->ras_drv)) &&
2050 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2051 ret = psp_bootloader_load_ras_drv(psp);
2053 DRM_ERROR("PSP load ras_drv failed!\n");
2058 if ((is_psp_fw_valid(psp->sos)) &&
2059 (psp->funcs->bootloader_load_sos != NULL)) {
2060 ret = psp_bootloader_load_sos(psp);
2062 DRM_ERROR("PSP load sos failed!\n");
2068 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2070 DRM_ERROR("PSP create ring failed!\n");
2074 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2077 ret = psp_tmr_init(psp);
2079 DRM_ERROR("PSP tmr init failed!\n");
2085 * For ASICs with DF Cstate management centralized
2086 * to PMFW, TMR setup should be performed after PMFW
2087 * loaded and before other non-psp firmware loaded.
2089 if (psp->pmfw_centralized_cstate_management) {
2090 ret = psp_load_smu_fw(psp);
2095 ret = psp_tmr_load(psp);
2097 DRM_ERROR("PSP load tmr failed!\n");
2104 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2105 enum psp_gfx_fw_type *type)
2107 switch (ucode->ucode_id) {
2108 case AMDGPU_UCODE_ID_CAP:
2109 *type = GFX_FW_TYPE_CAP;
2111 case AMDGPU_UCODE_ID_SDMA0:
2112 *type = GFX_FW_TYPE_SDMA0;
2114 case AMDGPU_UCODE_ID_SDMA1:
2115 *type = GFX_FW_TYPE_SDMA1;
2117 case AMDGPU_UCODE_ID_SDMA2:
2118 *type = GFX_FW_TYPE_SDMA2;
2120 case AMDGPU_UCODE_ID_SDMA3:
2121 *type = GFX_FW_TYPE_SDMA3;
2123 case AMDGPU_UCODE_ID_SDMA4:
2124 *type = GFX_FW_TYPE_SDMA4;
2126 case AMDGPU_UCODE_ID_SDMA5:
2127 *type = GFX_FW_TYPE_SDMA5;
2129 case AMDGPU_UCODE_ID_SDMA6:
2130 *type = GFX_FW_TYPE_SDMA6;
2132 case AMDGPU_UCODE_ID_SDMA7:
2133 *type = GFX_FW_TYPE_SDMA7;
2135 case AMDGPU_UCODE_ID_CP_MES:
2136 *type = GFX_FW_TYPE_CP_MES;
2138 case AMDGPU_UCODE_ID_CP_MES_DATA:
2139 *type = GFX_FW_TYPE_MES_STACK;
2141 case AMDGPU_UCODE_ID_CP_MES1:
2142 *type = GFX_FW_TYPE_CP_MES_KIQ;
2144 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2145 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2147 case AMDGPU_UCODE_ID_CP_CE:
2148 *type = GFX_FW_TYPE_CP_CE;
2150 case AMDGPU_UCODE_ID_CP_PFP:
2151 *type = GFX_FW_TYPE_CP_PFP;
2153 case AMDGPU_UCODE_ID_CP_ME:
2154 *type = GFX_FW_TYPE_CP_ME;
2156 case AMDGPU_UCODE_ID_CP_MEC1:
2157 *type = GFX_FW_TYPE_CP_MEC;
2159 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2160 *type = GFX_FW_TYPE_CP_MEC_ME1;
2162 case AMDGPU_UCODE_ID_CP_MEC2:
2163 *type = GFX_FW_TYPE_CP_MEC;
2165 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2166 *type = GFX_FW_TYPE_CP_MEC_ME2;
2168 case AMDGPU_UCODE_ID_RLC_P:
2169 *type = GFX_FW_TYPE_RLC_P;
2171 case AMDGPU_UCODE_ID_RLC_V:
2172 *type = GFX_FW_TYPE_RLC_V;
2174 case AMDGPU_UCODE_ID_RLC_G:
2175 *type = GFX_FW_TYPE_RLC_G;
2177 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2178 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2180 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2181 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2183 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2184 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2186 case AMDGPU_UCODE_ID_RLC_IRAM:
2187 *type = GFX_FW_TYPE_RLC_IRAM;
2189 case AMDGPU_UCODE_ID_RLC_DRAM:
2190 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2192 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2193 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2195 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2196 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2198 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2199 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2201 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2202 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2204 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2205 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2207 case AMDGPU_UCODE_ID_SMC:
2208 *type = GFX_FW_TYPE_SMU;
2210 case AMDGPU_UCODE_ID_PPTABLE:
2211 *type = GFX_FW_TYPE_PPTABLE;
2213 case AMDGPU_UCODE_ID_UVD:
2214 *type = GFX_FW_TYPE_UVD;
2216 case AMDGPU_UCODE_ID_UVD1:
2217 *type = GFX_FW_TYPE_UVD1;
2219 case AMDGPU_UCODE_ID_VCE:
2220 *type = GFX_FW_TYPE_VCE;
2222 case AMDGPU_UCODE_ID_VCN:
2223 *type = GFX_FW_TYPE_VCN;
2225 case AMDGPU_UCODE_ID_VCN1:
2226 *type = GFX_FW_TYPE_VCN1;
2228 case AMDGPU_UCODE_ID_DMCU_ERAM:
2229 *type = GFX_FW_TYPE_DMCU_ERAM;
2231 case AMDGPU_UCODE_ID_DMCU_INTV:
2232 *type = GFX_FW_TYPE_DMCU_ISR;
2234 case AMDGPU_UCODE_ID_VCN0_RAM:
2235 *type = GFX_FW_TYPE_VCN0_RAM;
2237 case AMDGPU_UCODE_ID_VCN1_RAM:
2238 *type = GFX_FW_TYPE_VCN1_RAM;
2240 case AMDGPU_UCODE_ID_DMCUB:
2241 *type = GFX_FW_TYPE_DMUB;
2243 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2244 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2246 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2247 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2249 case AMDGPU_UCODE_ID_IMU_I:
2250 *type = GFX_FW_TYPE_IMU_I;
2252 case AMDGPU_UCODE_ID_IMU_D:
2253 *type = GFX_FW_TYPE_IMU_D;
2255 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2256 *type = GFX_FW_TYPE_RS64_PFP;
2258 case AMDGPU_UCODE_ID_CP_RS64_ME:
2259 *type = GFX_FW_TYPE_RS64_ME;
2261 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2262 *type = GFX_FW_TYPE_RS64_MEC;
2264 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2265 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2267 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2268 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2270 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2271 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2273 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2274 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2276 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2277 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2279 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2280 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2282 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2283 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2285 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2286 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2288 case AMDGPU_UCODE_ID_MAXIMUM:
2296 static void psp_print_fw_hdr(struct psp_context *psp,
2297 struct amdgpu_firmware_info *ucode)
2299 struct amdgpu_device *adev = psp->adev;
2300 struct common_firmware_header *hdr;
2302 switch (ucode->ucode_id) {
2303 case AMDGPU_UCODE_ID_SDMA0:
2304 case AMDGPU_UCODE_ID_SDMA1:
2305 case AMDGPU_UCODE_ID_SDMA2:
2306 case AMDGPU_UCODE_ID_SDMA3:
2307 case AMDGPU_UCODE_ID_SDMA4:
2308 case AMDGPU_UCODE_ID_SDMA5:
2309 case AMDGPU_UCODE_ID_SDMA6:
2310 case AMDGPU_UCODE_ID_SDMA7:
2311 hdr = (struct common_firmware_header *)
2312 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2313 amdgpu_ucode_print_sdma_hdr(hdr);
2315 case AMDGPU_UCODE_ID_CP_CE:
2316 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2317 amdgpu_ucode_print_gfx_hdr(hdr);
2319 case AMDGPU_UCODE_ID_CP_PFP:
2320 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2321 amdgpu_ucode_print_gfx_hdr(hdr);
2323 case AMDGPU_UCODE_ID_CP_ME:
2324 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2325 amdgpu_ucode_print_gfx_hdr(hdr);
2327 case AMDGPU_UCODE_ID_CP_MEC1:
2328 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2329 amdgpu_ucode_print_gfx_hdr(hdr);
2331 case AMDGPU_UCODE_ID_RLC_G:
2332 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2333 amdgpu_ucode_print_rlc_hdr(hdr);
2335 case AMDGPU_UCODE_ID_SMC:
2336 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2337 amdgpu_ucode_print_smc_hdr(hdr);
2344 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2345 struct psp_gfx_cmd_resp *cmd)
2348 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2350 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2351 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2352 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2353 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2355 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2357 DRM_ERROR("Unknown firmware type\n");
2362 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2363 struct amdgpu_firmware_info *ucode)
2366 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2368 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2370 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2371 psp->fence_buf_mc_addr);
2374 release_psp_cmd_buf(psp);
2379 static int psp_load_smu_fw(struct psp_context *psp)
2382 struct amdgpu_device *adev = psp->adev;
2383 struct amdgpu_firmware_info *ucode =
2384 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2385 struct amdgpu_ras *ras = psp->ras_context.ras;
2388 * Skip SMU FW reloading in case of using BACO for runpm only,
2389 * as SMU is always alive.
2391 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2394 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2397 if ((amdgpu_in_reset(adev) &&
2398 ras && adev->ras_enabled &&
2399 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2400 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2401 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2403 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2406 ret = psp_execute_non_psp_fw_load(psp, ucode);
2409 DRM_ERROR("PSP load smu failed!\n");
2414 static bool fw_load_skip_check(struct psp_context *psp,
2415 struct amdgpu_firmware_info *ucode)
2417 if (!ucode->fw || !ucode->ucode_size)
2420 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2421 (psp_smu_reload_quirk(psp) ||
2422 psp->autoload_supported ||
2423 psp->pmfw_centralized_cstate_management))
2426 if (amdgpu_sriov_vf(psp->adev) &&
2427 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2430 if (psp->autoload_supported &&
2431 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2432 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2433 /* skip mec JT when autoload is enabled */
2439 int psp_load_fw_list(struct psp_context *psp,
2440 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2443 struct amdgpu_firmware_info *ucode;
2445 for (i = 0; i < ucode_count; ++i) {
2446 ucode = ucode_list[i];
2447 psp_print_fw_hdr(psp, ucode);
2448 ret = psp_execute_non_psp_fw_load(psp, ucode);
2455 static int psp_load_non_psp_fw(struct psp_context *psp)
2458 struct amdgpu_firmware_info *ucode;
2459 struct amdgpu_device *adev = psp->adev;
2461 if (psp->autoload_supported &&
2462 !psp->pmfw_centralized_cstate_management) {
2463 ret = psp_load_smu_fw(psp);
2468 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2469 ucode = &adev->firmware.ucode[i];
2471 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2472 !fw_load_skip_check(psp, ucode)) {
2473 ret = psp_load_smu_fw(psp);
2479 if (fw_load_skip_check(psp, ucode))
2482 if (psp->autoload_supported &&
2483 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2484 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2485 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2486 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2487 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2488 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2489 /* PSP only receive one SDMA fw for sienna_cichlid,
2490 * as all four sdma fw are same */
2493 psp_print_fw_hdr(psp, ucode);
2495 ret = psp_execute_non_psp_fw_load(psp, ucode);
2499 /* Start rlc autoload after psp recieved all the gfx firmware */
2500 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2501 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2502 ret = psp_rlc_autoload_start(psp);
2504 DRM_ERROR("Failed to start rlc autoload\n");
2513 static int psp_load_fw(struct amdgpu_device *adev)
2516 struct psp_context *psp = &adev->psp;
2518 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2519 /* should not destroy ring, only stop */
2520 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2522 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2524 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2526 DRM_ERROR("PSP ring init failed!\n");
2531 ret = psp_hw_start(psp);
2535 ret = psp_load_non_psp_fw(psp);
2539 ret = psp_asd_initialize(psp);
2541 DRM_ERROR("PSP load asd failed!\n");
2545 ret = psp_rl_load(adev);
2547 DRM_ERROR("PSP load RL failed!\n");
2551 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2552 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2553 ret = psp_xgmi_initialize(psp, false, true);
2554 /* Warning the XGMI seesion initialize failure
2555 * Instead of stop driver initialization
2558 dev_err(psp->adev->dev,
2559 "XGMI: Failed to initialize XGMI session\n");
2564 ret = psp_ras_initialize(psp);
2566 dev_err(psp->adev->dev,
2567 "RAS: Failed to initialize RAS\n");
2569 ret = psp_hdcp_initialize(psp);
2571 dev_err(psp->adev->dev,
2572 "HDCP: Failed to initialize HDCP\n");
2574 ret = psp_dtm_initialize(psp);
2576 dev_err(psp->adev->dev,
2577 "DTM: Failed to initialize DTM\n");
2579 ret = psp_rap_initialize(psp);
2581 dev_err(psp->adev->dev,
2582 "RAP: Failed to initialize RAP\n");
2584 ret = psp_securedisplay_initialize(psp);
2586 dev_err(psp->adev->dev,
2587 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2593 psp_free_shared_bufs(psp);
2596 * all cleanup jobs (xgmi terminate, ras terminate,
2597 * ring destroy, cmd/fence/fw buffers destory,
2598 * psp->cmd destory) are delayed to psp_hw_fini
2600 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2604 static int psp_hw_init(void *handle)
2607 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2609 mutex_lock(&adev->firmware.mutex);
2611 * This sequence is just used on hw_init only once, no need on
2614 ret = amdgpu_ucode_init_bo(adev);
2618 ret = psp_load_fw(adev);
2620 DRM_ERROR("PSP firmware loading failed\n");
2624 mutex_unlock(&adev->firmware.mutex);
2628 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2629 mutex_unlock(&adev->firmware.mutex);
2633 static int psp_hw_fini(void *handle)
2635 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2636 struct psp_context *psp = &adev->psp;
2639 psp_ras_terminate(psp);
2640 psp_securedisplay_terminate(psp);
2641 psp_rap_terminate(psp);
2642 psp_dtm_terminate(psp);
2643 psp_hdcp_terminate(psp);
2645 if (adev->gmc.xgmi.num_physical_nodes > 1)
2646 psp_xgmi_terminate(psp);
2649 psp_asd_terminate(psp);
2650 psp_tmr_terminate(psp);
2652 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2657 static int psp_suspend(void *handle)
2660 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2661 struct psp_context *psp = &adev->psp;
2663 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2664 psp->xgmi_context.context.initialized) {
2665 ret = psp_xgmi_terminate(psp);
2667 DRM_ERROR("Failed to terminate xgmi ta\n");
2673 ret = psp_ras_terminate(psp);
2675 DRM_ERROR("Failed to terminate ras ta\n");
2678 ret = psp_hdcp_terminate(psp);
2680 DRM_ERROR("Failed to terminate hdcp ta\n");
2683 ret = psp_dtm_terminate(psp);
2685 DRM_ERROR("Failed to terminate dtm ta\n");
2688 ret = psp_rap_terminate(psp);
2690 DRM_ERROR("Failed to terminate rap ta\n");
2693 ret = psp_securedisplay_terminate(psp);
2695 DRM_ERROR("Failed to terminate securedisplay ta\n");
2700 ret = psp_asd_terminate(psp);
2702 DRM_ERROR("Failed to terminate asd\n");
2706 ret = psp_tmr_terminate(psp);
2708 DRM_ERROR("Failed to terminate tmr\n");
2712 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2714 DRM_ERROR("PSP ring stop failed\n");
2720 static int psp_resume(void *handle)
2723 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2724 struct psp_context *psp = &adev->psp;
2726 DRM_INFO("PSP is resuming...\n");
2728 if (psp->mem_train_ctx.enable_mem_training) {
2729 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2731 DRM_ERROR("Failed to process memory training!\n");
2736 mutex_lock(&adev->firmware.mutex);
2738 ret = psp_hw_start(psp);
2742 ret = psp_load_non_psp_fw(psp);
2746 ret = psp_asd_initialize(psp);
2748 DRM_ERROR("PSP load asd failed!\n");
2752 ret = psp_rl_load(adev);
2754 dev_err(adev->dev, "PSP load RL failed!\n");
2758 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2759 ret = psp_xgmi_initialize(psp, false, true);
2760 /* Warning the XGMI seesion initialize failure
2761 * Instead of stop driver initialization
2764 dev_err(psp->adev->dev,
2765 "XGMI: Failed to initialize XGMI session\n");
2769 ret = psp_ras_initialize(psp);
2771 dev_err(psp->adev->dev,
2772 "RAS: Failed to initialize RAS\n");
2774 ret = psp_hdcp_initialize(psp);
2776 dev_err(psp->adev->dev,
2777 "HDCP: Failed to initialize HDCP\n");
2779 ret = psp_dtm_initialize(psp);
2781 dev_err(psp->adev->dev,
2782 "DTM: Failed to initialize DTM\n");
2784 ret = psp_rap_initialize(psp);
2786 dev_err(psp->adev->dev,
2787 "RAP: Failed to initialize RAP\n");
2789 ret = psp_securedisplay_initialize(psp);
2791 dev_err(psp->adev->dev,
2792 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2795 mutex_unlock(&adev->firmware.mutex);
2800 DRM_ERROR("PSP resume failed\n");
2801 mutex_unlock(&adev->firmware.mutex);
2805 int psp_gpu_reset(struct amdgpu_device *adev)
2809 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2812 mutex_lock(&adev->psp.mutex);
2813 ret = psp_mode1_reset(&adev->psp);
2814 mutex_unlock(&adev->psp.mutex);
2819 int psp_rlc_autoload_start(struct psp_context *psp)
2822 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2824 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2826 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2827 psp->fence_buf_mc_addr);
2829 release_psp_cmd_buf(psp);
2834 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2835 uint64_t cmd_gpu_addr, int cmd_size)
2837 struct amdgpu_firmware_info ucode = {0};
2839 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2840 AMDGPU_UCODE_ID_VCN0_RAM;
2841 ucode.mc_addr = cmd_gpu_addr;
2842 ucode.ucode_size = cmd_size;
2844 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2847 int psp_ring_cmd_submit(struct psp_context *psp,
2848 uint64_t cmd_buf_mc_addr,
2849 uint64_t fence_mc_addr,
2852 unsigned int psp_write_ptr_reg = 0;
2853 struct psp_gfx_rb_frame *write_frame;
2854 struct psp_ring *ring = &psp->km_ring;
2855 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2856 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2857 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2858 struct amdgpu_device *adev = psp->adev;
2859 uint32_t ring_size_dw = ring->ring_size / 4;
2860 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2862 /* KM (GPCOM) prepare write pointer */
2863 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2865 /* Update KM RB frame pointer to new frame */
2866 /* write_frame ptr increments by size of rb_frame in bytes */
2867 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2868 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2869 write_frame = ring_buffer_start;
2871 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2872 /* Check invalid write_frame ptr address */
2873 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2874 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2875 ring_buffer_start, ring_buffer_end, write_frame);
2876 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2880 /* Initialize KM RB frame */
2881 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2883 /* Update KM RB frame */
2884 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2885 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2886 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2887 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2888 write_frame->fence_value = index;
2889 amdgpu_device_flush_hdp(adev, NULL);
2891 /* Update the write Pointer in DWORDs */
2892 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2893 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2897 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2899 struct amdgpu_device *adev = psp->adev;
2900 char fw_name[PSP_FW_NAME_LEN];
2901 const struct psp_firmware_header_v1_0 *asd_hdr;
2904 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2905 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2909 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2910 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2911 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2912 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2913 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2914 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2917 amdgpu_ucode_release(&adev->psp.asd_fw);
2921 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
2923 struct amdgpu_device *adev = psp->adev;
2924 char fw_name[PSP_FW_NAME_LEN];
2925 const struct psp_firmware_header_v1_0 *toc_hdr;
2928 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2929 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
2933 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2934 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2935 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2936 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2937 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2938 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2941 amdgpu_ucode_release(&adev->psp.toc_fw);
2945 static int parse_sos_bin_descriptor(struct psp_context *psp,
2946 const struct psp_fw_bin_desc *desc,
2947 const struct psp_firmware_header_v2_0 *sos_hdr)
2949 uint8_t *ucode_start_addr = NULL;
2951 if (!psp || !desc || !sos_hdr)
2954 ucode_start_addr = (uint8_t *)sos_hdr +
2955 le32_to_cpu(desc->offset_bytes) +
2956 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2958 switch (desc->fw_type) {
2959 case PSP_FW_TYPE_PSP_SOS:
2960 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
2961 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
2962 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
2963 psp->sos.start_addr = ucode_start_addr;
2965 case PSP_FW_TYPE_PSP_SYS_DRV:
2966 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
2967 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
2968 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
2969 psp->sys.start_addr = ucode_start_addr;
2971 case PSP_FW_TYPE_PSP_KDB:
2972 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
2973 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
2974 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
2975 psp->kdb.start_addr = ucode_start_addr;
2977 case PSP_FW_TYPE_PSP_TOC:
2978 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
2979 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
2980 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
2981 psp->toc.start_addr = ucode_start_addr;
2983 case PSP_FW_TYPE_PSP_SPL:
2984 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
2985 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
2986 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
2987 psp->spl.start_addr = ucode_start_addr;
2989 case PSP_FW_TYPE_PSP_RL:
2990 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
2991 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
2992 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
2993 psp->rl.start_addr = ucode_start_addr;
2995 case PSP_FW_TYPE_PSP_SOC_DRV:
2996 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
2997 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
2998 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2999 psp->soc_drv.start_addr = ucode_start_addr;
3001 case PSP_FW_TYPE_PSP_INTF_DRV:
3002 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3003 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3004 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3005 psp->intf_drv.start_addr = ucode_start_addr;
3007 case PSP_FW_TYPE_PSP_DBG_DRV:
3008 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3009 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3010 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3011 psp->dbg_drv.start_addr = ucode_start_addr;
3013 case PSP_FW_TYPE_PSP_RAS_DRV:
3014 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3015 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3016 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3017 psp->ras_drv.start_addr = ucode_start_addr;
3020 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3027 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3029 const struct psp_firmware_header_v1_0 *sos_hdr;
3030 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3031 uint8_t *ucode_array_start_addr;
3033 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3034 ucode_array_start_addr = (uint8_t *)sos_hdr +
3035 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3037 if (adev->gmc.xgmi.connected_to_cpu ||
3038 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3039 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3040 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3042 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3043 adev->psp.sys.start_addr = ucode_array_start_addr;
3045 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3046 adev->psp.sos.start_addr = ucode_array_start_addr +
3047 le32_to_cpu(sos_hdr->sos.offset_bytes);
3049 /* Load alternate PSP SOS FW */
3050 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3052 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3053 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3055 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3056 adev->psp.sys.start_addr = ucode_array_start_addr +
3057 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3059 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3060 adev->psp.sos.start_addr = ucode_array_start_addr +
3061 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3064 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3065 dev_warn(adev->dev, "PSP SOS FW not available");
3072 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3074 struct amdgpu_device *adev = psp->adev;
3075 char fw_name[PSP_FW_NAME_LEN];
3076 const struct psp_firmware_header_v1_0 *sos_hdr;
3077 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3078 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3079 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3080 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3082 uint8_t *ucode_array_start_addr;
3085 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3086 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3090 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3091 ucode_array_start_addr = (uint8_t *)sos_hdr +
3092 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3093 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3095 switch (sos_hdr->header.header_version_major) {
3097 err = psp_init_sos_base_fw(adev);
3101 if (sos_hdr->header.header_version_minor == 1) {
3102 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3103 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3104 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3105 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3106 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3107 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3108 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3110 if (sos_hdr->header.header_version_minor == 2) {
3111 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3112 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3113 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3114 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3116 if (sos_hdr->header.header_version_minor == 3) {
3117 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3118 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3119 adev->psp.toc.start_addr = ucode_array_start_addr +
3120 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3121 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3122 adev->psp.kdb.start_addr = ucode_array_start_addr +
3123 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3124 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3125 adev->psp.spl.start_addr = ucode_array_start_addr +
3126 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3127 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3128 adev->psp.rl.start_addr = ucode_array_start_addr +
3129 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3133 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3135 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3136 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3141 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3142 err = parse_sos_bin_descriptor(psp,
3143 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3151 "unsupported psp sos firmware\n");
3158 amdgpu_ucode_release(&adev->psp.sos_fw);
3163 static int parse_ta_bin_descriptor(struct psp_context *psp,
3164 const struct psp_fw_bin_desc *desc,
3165 const struct ta_firmware_header_v2_0 *ta_hdr)
3167 uint8_t *ucode_start_addr = NULL;
3169 if (!psp || !desc || !ta_hdr)
3172 ucode_start_addr = (uint8_t *)ta_hdr +
3173 le32_to_cpu(desc->offset_bytes) +
3174 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3176 switch (desc->fw_type) {
3177 case TA_FW_TYPE_PSP_ASD:
3178 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3179 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3180 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3181 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3183 case TA_FW_TYPE_PSP_XGMI:
3184 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3185 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3186 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3188 case TA_FW_TYPE_PSP_RAS:
3189 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3190 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3191 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3193 case TA_FW_TYPE_PSP_HDCP:
3194 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3195 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3196 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3198 case TA_FW_TYPE_PSP_DTM:
3199 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3200 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3201 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3203 case TA_FW_TYPE_PSP_RAP:
3204 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3205 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3206 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3208 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3209 psp->securedisplay_context.context.bin_desc.fw_version =
3210 le32_to_cpu(desc->fw_version);
3211 psp->securedisplay_context.context.bin_desc.size_bytes =
3212 le32_to_cpu(desc->size_bytes);
3213 psp->securedisplay_context.context.bin_desc.start_addr =
3217 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3224 static int parse_ta_v1_microcode(struct psp_context *psp)
3226 const struct ta_firmware_header_v1_0 *ta_hdr;
3227 struct amdgpu_device *adev = psp->adev;
3229 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3231 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3234 adev->psp.xgmi_context.context.bin_desc.fw_version =
3235 le32_to_cpu(ta_hdr->xgmi.fw_version);
3236 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3237 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3238 adev->psp.xgmi_context.context.bin_desc.start_addr =
3240 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3242 adev->psp.ras_context.context.bin_desc.fw_version =
3243 le32_to_cpu(ta_hdr->ras.fw_version);
3244 adev->psp.ras_context.context.bin_desc.size_bytes =
3245 le32_to_cpu(ta_hdr->ras.size_bytes);
3246 adev->psp.ras_context.context.bin_desc.start_addr =
3247 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3248 le32_to_cpu(ta_hdr->ras.offset_bytes);
3250 adev->psp.hdcp_context.context.bin_desc.fw_version =
3251 le32_to_cpu(ta_hdr->hdcp.fw_version);
3252 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3253 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3254 adev->psp.hdcp_context.context.bin_desc.start_addr =
3256 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3258 adev->psp.dtm_context.context.bin_desc.fw_version =
3259 le32_to_cpu(ta_hdr->dtm.fw_version);
3260 adev->psp.dtm_context.context.bin_desc.size_bytes =
3261 le32_to_cpu(ta_hdr->dtm.size_bytes);
3262 adev->psp.dtm_context.context.bin_desc.start_addr =
3263 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3264 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3266 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3267 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3268 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3269 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3270 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3271 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3272 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3274 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3279 static int parse_ta_v2_microcode(struct psp_context *psp)
3281 const struct ta_firmware_header_v2_0 *ta_hdr;
3282 struct amdgpu_device *adev = psp->adev;
3286 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3288 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3291 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3292 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3296 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3297 err = parse_ta_bin_descriptor(psp,
3298 &ta_hdr->ta_fw_bin[ta_index],
3307 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3309 const struct common_firmware_header *hdr;
3310 struct amdgpu_device *adev = psp->adev;
3311 char fw_name[PSP_FW_NAME_LEN];
3314 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3315 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3319 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3320 switch (le16_to_cpu(hdr->header_version_major)) {
3322 err = parse_ta_v1_microcode(psp);
3325 err = parse_ta_v2_microcode(psp);
3328 dev_err(adev->dev, "unsupported TA header version\n");
3333 amdgpu_ucode_release(&adev->psp.ta_fw);
3338 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3340 struct amdgpu_device *adev = psp->adev;
3341 char fw_name[PSP_FW_NAME_LEN];
3342 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3343 struct amdgpu_firmware_info *info = NULL;
3346 if (!amdgpu_sriov_vf(adev)) {
3347 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3351 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3352 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3354 if (err == -ENODEV) {
3355 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3359 dev_err(adev->dev, "fail to initialize cap microcode\n");
3362 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3363 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3364 info->fw = adev->psp.cap_fw;
3365 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3366 adev->psp.cap_fw->data;
3367 adev->firmware.fw_size += ALIGN(
3368 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3369 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3370 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3371 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3376 amdgpu_ucode_release(&adev->psp.cap_fw);
3380 static int psp_set_clockgating_state(void *handle,
3381 enum amd_clockgating_state state)
3386 static int psp_set_powergating_state(void *handle,
3387 enum amd_powergating_state state)
3392 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3393 struct device_attribute *attr,
3396 struct drm_device *ddev = dev_get_drvdata(dev);
3397 struct amdgpu_device *adev = drm_to_adev(ddev);
3401 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3402 DRM_INFO("PSP block is not ready yet.");
3406 mutex_lock(&adev->psp.mutex);
3407 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3408 mutex_unlock(&adev->psp.mutex);
3411 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3415 return sysfs_emit(buf, "%x\n", fw_ver);
3418 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3419 struct device_attribute *attr,
3423 struct drm_device *ddev = dev_get_drvdata(dev);
3424 struct amdgpu_device *adev = drm_to_adev(ddev);
3427 const struct firmware *usbc_pd_fw;
3428 struct amdgpu_bo *fw_buf_bo = NULL;
3429 uint64_t fw_pri_mc_addr;
3430 void *fw_pri_cpu_addr;
3432 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3433 DRM_INFO("PSP block is not ready yet.");
3437 if (!drm_dev_enter(ddev, &idx))
3440 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3441 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3445 /* LFB address which is aligned to 1MB boundary per PSP request */
3446 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3447 AMDGPU_GEM_DOMAIN_VRAM |
3448 AMDGPU_GEM_DOMAIN_GTT,
3449 &fw_buf_bo, &fw_pri_mc_addr,
3454 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3456 mutex_lock(&adev->psp.mutex);
3457 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3458 mutex_unlock(&adev->psp.mutex);
3460 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3463 release_firmware(usbc_pd_fw);
3466 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3474 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3478 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3481 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3482 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3487 static DEVICE_ATTR(usbc_pd_fw, 0644,
3488 psp_usbc_pd_fw_sysfs_read,
3489 psp_usbc_pd_fw_sysfs_write);
3491 int is_psp_fw_valid(struct psp_bin_desc bin)
3493 return bin.size_bytes;
3496 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3497 struct bin_attribute *bin_attr,
3498 char *buffer, loff_t pos, size_t count)
3500 struct device *dev = kobj_to_dev(kobj);
3501 struct drm_device *ddev = dev_get_drvdata(dev);
3502 struct amdgpu_device *adev = drm_to_adev(ddev);
3504 adev->psp.vbflash_done = false;
3506 /* Safeguard against memory drain */
3507 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3508 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3509 kvfree(adev->psp.vbflash_tmp_buf);
3510 adev->psp.vbflash_tmp_buf = NULL;
3511 adev->psp.vbflash_image_size = 0;
3515 /* TODO Just allocate max for now and optimize to realloc later if needed */
3516 if (!adev->psp.vbflash_tmp_buf) {
3517 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3518 if (!adev->psp.vbflash_tmp_buf)
3522 mutex_lock(&adev->psp.mutex);
3523 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3524 adev->psp.vbflash_image_size += count;
3525 mutex_unlock(&adev->psp.mutex);
3527 dev_info(adev->dev, "VBIOS flash write PSP done");
3532 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3533 struct bin_attribute *bin_attr, char *buffer,
3534 loff_t pos, size_t count)
3536 struct device *dev = kobj_to_dev(kobj);
3537 struct drm_device *ddev = dev_get_drvdata(dev);
3538 struct amdgpu_device *adev = drm_to_adev(ddev);
3539 struct amdgpu_bo *fw_buf_bo = NULL;
3540 uint64_t fw_pri_mc_addr;
3541 void *fw_pri_cpu_addr;
3544 dev_info(adev->dev, "VBIOS flash to PSP started");
3546 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3547 AMDGPU_GPU_PAGE_SIZE,
3548 AMDGPU_GEM_DOMAIN_VRAM,
3555 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3557 mutex_lock(&adev->psp.mutex);
3558 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3559 mutex_unlock(&adev->psp.mutex);
3561 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3564 kvfree(adev->psp.vbflash_tmp_buf);
3565 adev->psp.vbflash_tmp_buf = NULL;
3566 adev->psp.vbflash_image_size = 0;
3569 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3573 dev_info(adev->dev, "VBIOS flash to PSP done");
3577 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3578 struct device_attribute *attr,
3581 struct drm_device *ddev = dev_get_drvdata(dev);
3582 struct amdgpu_device *adev = drm_to_adev(ddev);
3583 uint32_t vbflash_status;
3585 vbflash_status = psp_vbflash_status(&adev->psp);
3586 if (!adev->psp.vbflash_done)
3588 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3591 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3594 static const struct bin_attribute psp_vbflash_bin_attr = {
3595 .attr = {.name = "psp_vbflash", .mode = 0664},
3597 .write = amdgpu_psp_vbflash_write,
3598 .read = amdgpu_psp_vbflash_read,
3601 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3603 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3606 struct psp_context *psp = &adev->psp;
3608 if (amdgpu_sriov_vf(adev))
3611 switch (adev->ip_versions[MP0_HWIP][0]) {
3612 case IP_VERSION(13, 0, 0):
3613 case IP_VERSION(13, 0, 7):
3614 case IP_VERSION(13, 0, 10):
3617 psp_v13_0_set_psp_funcs(psp);
3619 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3621 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3622 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3624 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3631 const struct amd_ip_funcs psp_ip_funcs = {
3633 .early_init = psp_early_init,
3635 .sw_init = psp_sw_init,
3636 .sw_fini = psp_sw_fini,
3637 .hw_init = psp_hw_init,
3638 .hw_fini = psp_hw_fini,
3639 .suspend = psp_suspend,
3640 .resume = psp_resume,
3642 .check_soft_reset = NULL,
3643 .wait_for_idle = NULL,
3645 .set_clockgating_state = psp_set_clockgating_state,
3646 .set_powergating_state = psp_set_powergating_state,
3649 static int psp_sysfs_init(struct amdgpu_device *adev)
3651 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3654 DRM_ERROR("Failed to create USBC PD FW control file!");
3659 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3661 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3662 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3665 static void psp_sysfs_fini(struct amdgpu_device *adev)
3667 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3670 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3671 .type = AMD_IP_BLOCK_TYPE_PSP,
3675 .funcs = &psp_ip_funcs,
3678 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3679 .type = AMD_IP_BLOCK_TYPE_PSP,
3683 .funcs = &psp_ip_funcs,
3686 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3687 .type = AMD_IP_BLOCK_TYPE_PSP,
3691 .funcs = &psp_ip_funcs,
3694 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3695 .type = AMD_IP_BLOCK_TYPE_PSP,
3699 .funcs = &psp_ip_funcs,
3702 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
3703 .type = AMD_IP_BLOCK_TYPE_PSP,
3707 .funcs = &psp_ip_funcs,
3710 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3711 .type = AMD_IP_BLOCK_TYPE_PSP,
3715 .funcs = &psp_ip_funcs,
3718 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3719 .type = AMD_IP_BLOCK_TYPE_PSP,
3723 .funcs = &psp_ip_funcs,