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, 6):
150 ret = psp_init_cap_microcode(psp, ucode_prefix);
152 case IP_VERSION(13, 0, 10):
153 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
154 ret = psp_init_cap_microcode(psp, ucode_prefix);
162 static int psp_early_init(void *handle)
164 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
165 struct psp_context *psp = &adev->psp;
167 switch (adev->ip_versions[MP0_HWIP][0]) {
168 case IP_VERSION(9, 0, 0):
169 psp_v3_1_set_psp_funcs(psp);
170 psp->autoload_supported = false;
172 case IP_VERSION(10, 0, 0):
173 case IP_VERSION(10, 0, 1):
174 psp_v10_0_set_psp_funcs(psp);
175 psp->autoload_supported = false;
177 case IP_VERSION(11, 0, 2):
178 case IP_VERSION(11, 0, 4):
179 psp_v11_0_set_psp_funcs(psp);
180 psp->autoload_supported = false;
182 case IP_VERSION(11, 0, 0):
183 case IP_VERSION(11, 0, 5):
184 case IP_VERSION(11, 0, 9):
185 case IP_VERSION(11, 0, 7):
186 case IP_VERSION(11, 0, 11):
187 case IP_VERSION(11, 5, 0):
188 case IP_VERSION(11, 0, 12):
189 case IP_VERSION(11, 0, 13):
190 psp_v11_0_set_psp_funcs(psp);
191 psp->autoload_supported = true;
193 case IP_VERSION(11, 0, 3):
194 case IP_VERSION(12, 0, 1):
195 psp_v12_0_set_psp_funcs(psp);
197 case IP_VERSION(13, 0, 2):
198 case IP_VERSION(13, 0, 6):
199 psp_v13_0_set_psp_funcs(psp);
201 case IP_VERSION(13, 0, 1):
202 case IP_VERSION(13, 0, 3):
203 case IP_VERSION(13, 0, 5):
204 case IP_VERSION(13, 0, 8):
205 case IP_VERSION(13, 0, 10):
206 case IP_VERSION(13, 0, 11):
207 psp_v13_0_set_psp_funcs(psp);
208 psp->autoload_supported = true;
210 case IP_VERSION(11, 0, 8):
211 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
212 psp_v11_0_8_set_psp_funcs(psp);
213 psp->autoload_supported = false;
216 case IP_VERSION(13, 0, 0):
217 case IP_VERSION(13, 0, 7):
218 psp_v13_0_set_psp_funcs(psp);
219 psp->autoload_supported = true;
221 case IP_VERSION(13, 0, 4):
222 psp_v13_0_4_set_psp_funcs(psp);
223 psp->autoload_supported = true;
231 psp_check_pmfw_centralized_cstate_management(psp);
233 if (amdgpu_sriov_vf(adev))
234 return psp_init_sriov_microcode(psp);
236 return psp_init_microcode(psp);
239 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
241 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
242 &mem_ctx->shared_buf);
243 mem_ctx->shared_bo = NULL;
246 static void psp_free_shared_bufs(struct psp_context *psp)
251 /* free TMR memory buffer */
252 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
253 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
256 /* free xgmi shared memory */
257 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
259 /* free ras shared memory */
260 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
262 /* free hdcp shared memory */
263 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
265 /* free dtm shared memory */
266 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
268 /* free rap shared memory */
269 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
271 /* free securedisplay shared memory */
272 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
277 static void psp_memory_training_fini(struct psp_context *psp)
279 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
281 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
282 kfree(ctx->sys_cache);
283 ctx->sys_cache = NULL;
286 static int psp_memory_training_init(struct psp_context *psp)
289 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
291 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
292 DRM_DEBUG("memory training is not supported!\n");
296 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
297 if (ctx->sys_cache == NULL) {
298 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
303 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
304 ctx->train_data_size,
305 ctx->p2c_train_data_offset,
306 ctx->c2p_train_data_offset);
307 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
311 psp_memory_training_fini(psp);
316 * Helper funciton to query psp runtime database entry
318 * @adev: amdgpu_device pointer
319 * @entry_type: the type of psp runtime database entry
320 * @db_entry: runtime database entry pointer
322 * Return false if runtime database doesn't exit or entry is invalid
323 * or true if the specific database entry is found, and copy to @db_entry
325 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
326 enum psp_runtime_entry_type entry_type,
329 uint64_t db_header_pos, db_dir_pos;
330 struct psp_runtime_data_header db_header = {0};
331 struct psp_runtime_data_directory db_dir = {0};
335 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6))
338 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
339 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
341 /* read runtime db header from vram */
342 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
343 sizeof(struct psp_runtime_data_header), false);
345 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
346 /* runtime db doesn't exist, exit */
347 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
351 /* read runtime database entry from vram */
352 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
353 sizeof(struct psp_runtime_data_directory), false);
355 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
356 /* invalid db entry count, exit */
357 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
361 /* look up for requested entry type */
362 for (i = 0; i < db_dir.entry_count && !ret; i++) {
363 if (db_dir.entry_list[i].entry_type == entry_type) {
364 switch (entry_type) {
365 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
366 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
367 /* invalid db entry size */
368 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
371 /* read runtime database entry */
372 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
373 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
376 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
377 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
378 /* invalid db entry size */
379 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
382 /* read runtime database entry */
383 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
384 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
397 static int psp_sw_init(void *handle)
399 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
400 struct psp_context *psp = &adev->psp;
402 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
403 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
404 struct psp_runtime_scpm_entry scpm_entry;
406 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
408 DRM_ERROR("Failed to allocate memory to command buffer!\n");
412 adev->psp.xgmi_context.supports_extended_data =
413 !adev->gmc.xgmi.connected_to_cpu &&
414 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
416 memset(&scpm_entry, 0, sizeof(scpm_entry));
417 if ((psp_get_runtime_db_entry(adev,
418 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
420 (scpm_entry.scpm_status != SCPM_DISABLE)) {
421 adev->scpm_enabled = true;
422 adev->scpm_status = scpm_entry.scpm_status;
424 adev->scpm_enabled = false;
425 adev->scpm_status = SCPM_DISABLE;
428 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
430 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
431 if (psp_get_runtime_db_entry(adev,
432 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
434 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
435 if ((psp->boot_cfg_bitmask) &
436 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
437 /* If psp runtime database exists, then
438 * only enable two stage memory training
439 * when TWO_STAGE_DRAM_TRAINING bit is set
440 * in runtime database */
441 mem_training_ctx->enable_mem_training = true;
445 /* If psp runtime database doesn't exist or
446 * is invalid, force enable two stage memory
448 mem_training_ctx->enable_mem_training = true;
451 if (mem_training_ctx->enable_mem_training) {
452 ret = psp_memory_training_init(psp);
454 DRM_ERROR("Failed to initialize memory training!\n");
458 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
460 DRM_ERROR("Failed to process memory training!\n");
465 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
466 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
467 ret = psp_sysfs_init(adev);
472 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
473 amdgpu_sriov_vf(adev) ?
474 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
476 &psp->fw_pri_mc_addr,
481 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
482 AMDGPU_GEM_DOMAIN_VRAM |
483 AMDGPU_GEM_DOMAIN_GTT,
485 &psp->fence_buf_mc_addr,
490 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
491 AMDGPU_GEM_DOMAIN_VRAM |
492 AMDGPU_GEM_DOMAIN_GTT,
493 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
494 (void **)&psp->cmd_buf_mem);
501 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
502 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
504 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
505 &psp->fence_buf_mc_addr, &psp->fence_buf);
509 static int psp_sw_fini(void *handle)
511 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
512 struct psp_context *psp = &adev->psp;
513 struct psp_gfx_cmd_resp *cmd = psp->cmd;
515 psp_memory_training_fini(psp);
517 amdgpu_ucode_release(&psp->sos_fw);
518 amdgpu_ucode_release(&psp->asd_fw);
519 amdgpu_ucode_release(&psp->ta_fw);
520 amdgpu_ucode_release(&psp->cap_fw);
521 amdgpu_ucode_release(&psp->toc_fw);
523 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
524 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
525 psp_sysfs_fini(adev);
530 psp_free_shared_bufs(psp);
532 if (psp->km_ring.ring_mem)
533 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
534 &psp->km_ring.ring_mem_mc_addr,
535 (void **)&psp->km_ring.ring_mem);
537 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
538 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
539 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
540 &psp->fence_buf_mc_addr, &psp->fence_buf);
541 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
542 (void **)&psp->cmd_buf_mem);
547 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
548 uint32_t reg_val, uint32_t mask, bool check_changed)
552 struct amdgpu_device *adev = psp->adev;
554 if (psp->adev->no_hw_access)
557 for (i = 0; i < adev->usec_timeout; i++) {
558 val = RREG32(reg_index);
563 if ((val & mask) == reg_val)
572 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
575 case GFX_CMD_ID_LOAD_TA:
577 case GFX_CMD_ID_UNLOAD_TA:
579 case GFX_CMD_ID_INVOKE_CMD:
581 case GFX_CMD_ID_LOAD_ASD:
583 case GFX_CMD_ID_SETUP_TMR:
585 case GFX_CMD_ID_LOAD_IP_FW:
587 case GFX_CMD_ID_DESTROY_TMR:
588 return "DESTROY_TMR";
589 case GFX_CMD_ID_SAVE_RESTORE:
590 return "SAVE_RESTORE_IP_FW";
591 case GFX_CMD_ID_SETUP_VMR:
593 case GFX_CMD_ID_DESTROY_VMR:
594 return "DESTROY_VMR";
595 case GFX_CMD_ID_PROG_REG:
597 case GFX_CMD_ID_GET_FW_ATTESTATION:
598 return "GET_FW_ATTESTATION";
599 case GFX_CMD_ID_LOAD_TOC:
600 return "ID_LOAD_TOC";
601 case GFX_CMD_ID_AUTOLOAD_RLC:
602 return "AUTOLOAD_RLC";
603 case GFX_CMD_ID_BOOT_CFG:
606 return "UNKNOWN CMD";
611 psp_cmd_submit_buf(struct psp_context *psp,
612 struct amdgpu_firmware_info *ucode,
613 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
618 bool ras_intr = false;
619 bool skip_unsupport = false;
621 if (psp->adev->no_hw_access)
624 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
626 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
628 index = atomic_inc_return(&psp->fence_value);
629 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
631 atomic_dec(&psp->fence_value);
635 amdgpu_device_invalidate_hdp(psp->adev, NULL);
636 while (*((unsigned int *)psp->fence_buf) != index) {
640 * Shouldn't wait for timeout when err_event_athub occurs,
641 * because gpu reset thread triggered and lock resource should
642 * be released for psp resume sequence.
644 ras_intr = amdgpu_ras_intr_triggered();
647 usleep_range(10, 100);
648 amdgpu_device_invalidate_hdp(psp->adev, NULL);
651 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
652 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
653 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
655 memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
657 /* In some cases, psp response status is not 0 even there is no
658 * problem while the command is submitted. Some version of PSP FW
659 * doesn't write 0 to that field.
660 * So here we would like to only print a warning instead of an error
661 * during psp initialization to avoid breaking hw_init and it doesn't
664 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
666 DRM_WARN("failed to load ucode %s(0x%X) ",
667 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
668 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
669 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
670 psp->cmd_buf_mem->resp.status);
671 /* If any firmware (including CAP) load fails under SRIOV, it should
672 * return failure to stop the VF from initializing.
673 * Also return failure in case of timeout
675 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
682 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
683 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
690 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
692 struct psp_gfx_cmd_resp *cmd = psp->cmd;
694 mutex_lock(&psp->mutex);
696 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
701 static void release_psp_cmd_buf(struct psp_context *psp)
703 mutex_unlock(&psp->mutex);
706 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
707 struct psp_gfx_cmd_resp *cmd,
708 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
710 struct amdgpu_device *adev = psp->adev;
715 size = amdgpu_bo_size(tmr_bo);
716 tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
719 if (amdgpu_sriov_vf(psp->adev))
720 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
722 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
723 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
724 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
725 cmd->cmd.cmd_setup_tmr.buf_size = size;
726 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
727 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
728 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
731 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
732 uint64_t pri_buf_mc, uint32_t size)
734 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
735 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
736 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
737 cmd->cmd.cmd_load_toc.toc_size = size;
740 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
741 static int psp_load_toc(struct psp_context *psp,
745 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
747 /* Copy toc to psp firmware private buffer */
748 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
750 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
752 ret = psp_cmd_submit_buf(psp, NULL, cmd,
753 psp->fence_buf_mc_addr);
755 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
757 release_psp_cmd_buf(psp);
762 static bool psp_boottime_tmr(struct psp_context *psp)
764 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
765 case IP_VERSION(13, 0, 6):
772 /* Set up Trusted Memory Region */
773 static int psp_tmr_init(struct psp_context *psp)
781 * According to HW engineer, they prefer the TMR address be "naturally
782 * aligned" , e.g. the start address be an integer divide of TMR size.
784 * Note: this memory need be reserved till the driver
787 tmr_size = PSP_TMR_SIZE(psp->adev);
789 /* For ASICs support RLC autoload, psp will parse the toc
790 * and calculate the total size of TMR needed */
791 if (!amdgpu_sriov_vf(psp->adev) &&
792 psp->toc.start_addr &&
793 psp->toc.size_bytes &&
795 ret = psp_load_toc(psp, &tmr_size);
797 DRM_ERROR("Failed to load toc\n");
803 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
804 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
806 AMDGPU_HAS_VRAM(psp->adev) ?
807 AMDGPU_GEM_DOMAIN_VRAM :
808 AMDGPU_GEM_DOMAIN_GTT,
809 &psp->tmr_bo, &psp->tmr_mc_addr,
816 static bool psp_skip_tmr(struct psp_context *psp)
818 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
819 case IP_VERSION(11, 0, 9):
820 case IP_VERSION(11, 0, 7):
821 case IP_VERSION(13, 0, 2):
822 case IP_VERSION(13, 0, 10):
829 static int psp_tmr_load(struct psp_context *psp)
832 struct psp_gfx_cmd_resp *cmd;
834 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
835 * Already set up by host driver.
837 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
840 cmd = acquire_psp_cmd_buf(psp);
842 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
844 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
845 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
847 ret = psp_cmd_submit_buf(psp, NULL, cmd,
848 psp->fence_buf_mc_addr);
850 release_psp_cmd_buf(psp);
855 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
856 struct psp_gfx_cmd_resp *cmd)
858 if (amdgpu_sriov_vf(psp->adev))
859 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
861 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
864 static int psp_tmr_unload(struct psp_context *psp)
867 struct psp_gfx_cmd_resp *cmd;
869 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
870 * as TMR is not loaded at all
872 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
875 cmd = acquire_psp_cmd_buf(psp);
877 psp_prep_tmr_unload_cmd_buf(psp, cmd);
878 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
880 ret = psp_cmd_submit_buf(psp, NULL, cmd,
881 psp->fence_buf_mc_addr);
883 release_psp_cmd_buf(psp);
888 static int psp_tmr_terminate(struct psp_context *psp)
890 return psp_tmr_unload(psp);
893 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
894 uint64_t *output_ptr)
897 struct psp_gfx_cmd_resp *cmd;
902 if (amdgpu_sriov_vf(psp->adev))
905 cmd = acquire_psp_cmd_buf(psp);
907 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
909 ret = psp_cmd_submit_buf(psp, NULL, cmd,
910 psp->fence_buf_mc_addr);
913 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
914 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
917 release_psp_cmd_buf(psp);
922 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
924 struct psp_context *psp = &adev->psp;
925 struct psp_gfx_cmd_resp *cmd;
928 if (amdgpu_sriov_vf(adev))
931 cmd = acquire_psp_cmd_buf(psp);
933 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
934 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
936 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
939 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
942 release_psp_cmd_buf(psp);
947 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
950 struct psp_context *psp = &adev->psp;
951 struct psp_gfx_cmd_resp *cmd;
953 if (amdgpu_sriov_vf(adev))
956 cmd = acquire_psp_cmd_buf(psp);
958 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
959 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
960 cmd->cmd.boot_cfg.boot_config = boot_cfg;
961 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
963 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
965 release_psp_cmd_buf(psp);
970 static int psp_rl_load(struct amdgpu_device *adev)
973 struct psp_context *psp = &adev->psp;
974 struct psp_gfx_cmd_resp *cmd;
976 if (!is_psp_fw_valid(psp->rl))
979 cmd = acquire_psp_cmd_buf(psp);
981 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
982 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
984 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
985 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
986 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
987 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
988 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
990 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
992 release_psp_cmd_buf(psp);
997 int psp_spatial_partition(struct psp_context *psp, int mode)
999 struct psp_gfx_cmd_resp *cmd;
1002 if (amdgpu_sriov_vf(psp->adev))
1005 cmd = acquire_psp_cmd_buf(psp);
1007 cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1008 cmd->cmd.cmd_spatial_part.mode = mode;
1010 dev_info(psp->adev->dev, "Requesting %d paritions through PSP", mode);
1011 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1013 release_psp_cmd_buf(psp);
1018 static int psp_asd_initialize(struct psp_context *psp)
1022 /* If PSP version doesn't match ASD version, asd loading will be failed.
1023 * add workaround to bypass it for sriov now.
1024 * TODO: add version check to make it common
1026 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1029 psp->asd_context.mem_context.shared_mc_addr = 0;
1030 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
1031 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
1033 ret = psp_ta_load(psp, &psp->asd_context);
1035 psp->asd_context.initialized = true;
1040 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1041 uint32_t session_id)
1043 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1044 cmd->cmd.cmd_unload_ta.session_id = session_id;
1047 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1050 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1052 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1054 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1056 context->resp_status = cmd->resp.status;
1058 release_psp_cmd_buf(psp);
1063 static int psp_asd_terminate(struct psp_context *psp)
1067 if (amdgpu_sriov_vf(psp->adev))
1070 if (!psp->asd_context.initialized)
1073 ret = psp_ta_unload(psp, &psp->asd_context);
1075 psp->asd_context.initialized = false;
1080 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1081 uint32_t id, uint32_t value)
1083 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1084 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1085 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1088 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1091 struct psp_gfx_cmd_resp *cmd;
1094 if (reg >= PSP_REG_LAST)
1097 cmd = acquire_psp_cmd_buf(psp);
1099 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1100 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1102 DRM_ERROR("PSP failed to program reg id %d", reg);
1104 release_psp_cmd_buf(psp);
1109 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1111 struct ta_context *context)
1113 cmd->cmd_id = context->ta_load_type;
1114 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1115 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1116 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1118 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1119 lower_32_bits(context->mem_context.shared_mc_addr);
1120 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1121 upper_32_bits(context->mem_context.shared_mc_addr);
1122 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1125 int psp_ta_init_shared_buf(struct psp_context *psp,
1126 struct ta_mem_context *mem_ctx)
1129 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1130 * physical) for ta to host memory
1132 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1133 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1134 AMDGPU_GEM_DOMAIN_GTT,
1135 &mem_ctx->shared_bo,
1136 &mem_ctx->shared_mc_addr,
1137 &mem_ctx->shared_buf);
1140 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1142 uint32_t session_id)
1144 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1145 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1146 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1149 int psp_ta_invoke(struct psp_context *psp,
1151 struct ta_context *context)
1154 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1156 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1158 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1159 psp->fence_buf_mc_addr);
1161 context->resp_status = cmd->resp.status;
1163 release_psp_cmd_buf(psp);
1168 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1171 struct psp_gfx_cmd_resp *cmd;
1173 cmd = acquire_psp_cmd_buf(psp);
1175 psp_copy_fw(psp, context->bin_desc.start_addr,
1176 context->bin_desc.size_bytes);
1178 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1180 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1181 psp->fence_buf_mc_addr);
1183 context->resp_status = cmd->resp.status;
1186 context->session_id = cmd->resp.session_id;
1188 release_psp_cmd_buf(psp);
1193 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1195 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1198 int psp_xgmi_terminate(struct psp_context *psp)
1201 struct amdgpu_device *adev = psp->adev;
1203 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1204 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1205 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1206 adev->gmc.xgmi.connected_to_cpu))
1209 if (!psp->xgmi_context.context.initialized)
1212 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1214 psp->xgmi_context.context.initialized = false;
1219 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1221 struct ta_xgmi_shared_memory *xgmi_cmd;
1225 !psp->xgmi_context.context.bin_desc.size_bytes ||
1226 !psp->xgmi_context.context.bin_desc.start_addr)
1232 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1233 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1235 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1236 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1242 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1244 psp->xgmi_context.context.initialized = true;
1249 /* Initialize XGMI session */
1250 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1251 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1252 xgmi_cmd->flag_extend_link_record = set_extended_data;
1253 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1255 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1260 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1262 struct ta_xgmi_shared_memory *xgmi_cmd;
1265 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1266 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1268 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1270 /* Invoke xgmi ta to get hive id */
1271 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1275 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1280 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1282 struct ta_xgmi_shared_memory *xgmi_cmd;
1285 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1286 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1288 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1290 /* Invoke xgmi ta to get the node id */
1291 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1295 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1300 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1302 return (psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1303 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
1304 psp->adev->ip_versions[MP0_HWIP][0] >= IP_VERSION(13, 0, 6);
1308 * Chips that support extended topology information require the driver to
1309 * reflect topology information in the opposite direction. This is
1310 * because the TA has already exceeded its link record limit and if the
1311 * TA holds bi-directional information, the driver would have to do
1312 * multiple fetches instead of just two.
1314 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1315 struct psp_xgmi_node_info node_info)
1317 struct amdgpu_device *mirror_adev;
1318 struct amdgpu_hive_info *hive;
1319 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1320 uint64_t dst_node_id = node_info.node_id;
1321 uint8_t dst_num_hops = node_info.num_hops;
1322 uint8_t dst_num_links = node_info.num_links;
1324 hive = amdgpu_get_xgmi_hive(psp->adev);
1325 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1326 struct psp_xgmi_topology_info *mirror_top_info;
1329 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1332 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1333 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1334 if (mirror_top_info->nodes[j].node_id != src_node_id)
1337 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1339 * prevent 0 num_links value re-reflection since reflection
1340 * criteria is based on num_hops (direct or indirect).
1344 mirror_top_info->nodes[j].num_links = dst_num_links;
1352 amdgpu_put_xgmi_hive(hive);
1355 int psp_xgmi_get_topology_info(struct psp_context *psp,
1357 struct psp_xgmi_topology_info *topology,
1358 bool get_extended_data)
1360 struct ta_xgmi_shared_memory *xgmi_cmd;
1361 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1362 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1366 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1369 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1370 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1371 xgmi_cmd->flag_extend_link_record = get_extended_data;
1373 /* Fill in the shared memory with topology information as input */
1374 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1375 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1376 topology_info_input->num_nodes = number_devices;
1378 for (i = 0; i < topology_info_input->num_nodes; i++) {
1379 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1380 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1381 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1382 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1385 /* Invoke xgmi ta to get the topology information */
1386 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1390 /* Read the output topology information from the shared memory */
1391 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1392 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1393 for (i = 0; i < topology->num_nodes; i++) {
1394 /* extended data will either be 0 or equal to non-extended data */
1395 if (topology_info_output->nodes[i].num_hops)
1396 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1398 /* non-extended data gets everything here so no need to update */
1399 if (!get_extended_data) {
1400 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1401 topology->nodes[i].is_sharing_enabled =
1402 topology_info_output->nodes[i].is_sharing_enabled;
1403 topology->nodes[i].sdma_engine =
1404 topology_info_output->nodes[i].sdma_engine;
1409 /* Invoke xgmi ta again to get the link information */
1410 if (psp_xgmi_peer_link_info_supported(psp)) {
1411 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1412 bool requires_reflection =
1413 (psp->xgmi_context.supports_extended_data && get_extended_data) ||
1414 psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6);
1416 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1418 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1423 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1424 for (i = 0; i < topology->num_nodes; i++) {
1425 /* accumulate num_links on extended data */
1426 topology->nodes[i].num_links = get_extended_data ?
1427 topology->nodes[i].num_links +
1428 link_info_output->nodes[i].num_links :
1429 ((requires_reflection && topology->nodes[i].num_links) ? topology->nodes[i].num_links :
1430 link_info_output->nodes[i].num_links);
1432 /* reflect the topology information for bi-directionality */
1433 if (requires_reflection && topology->nodes[i].num_hops)
1434 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1441 int psp_xgmi_set_topology_info(struct psp_context *psp,
1443 struct psp_xgmi_topology_info *topology)
1445 struct ta_xgmi_shared_memory *xgmi_cmd;
1446 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1449 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1452 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1453 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1455 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1456 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1457 topology_info_input->num_nodes = number_devices;
1459 for (i = 0; i < topology_info_input->num_nodes; i++) {
1460 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1461 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1462 topology_info_input->nodes[i].is_sharing_enabled = 1;
1463 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1466 /* Invoke xgmi ta to set topology information */
1467 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1471 static void psp_ras_ta_check_status(struct psp_context *psp)
1473 struct ta_ras_shared_memory *ras_cmd =
1474 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1476 switch (ras_cmd->ras_status) {
1477 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1478 dev_warn(psp->adev->dev,
1479 "RAS WARNING: cmd failed due to unsupported ip\n");
1481 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1482 dev_warn(psp->adev->dev,
1483 "RAS WARNING: cmd failed due to unsupported error injection\n");
1485 case TA_RAS_STATUS__SUCCESS:
1487 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1488 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1489 dev_warn(psp->adev->dev,
1490 "RAS WARNING: Inject error to critical region is not allowed\n");
1493 dev_warn(psp->adev->dev,
1494 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1499 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1501 struct ta_ras_shared_memory *ras_cmd;
1504 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1507 * TODO: bypass the loading in sriov for now
1509 if (amdgpu_sriov_vf(psp->adev))
1512 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1514 if (amdgpu_ras_intr_triggered())
1517 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1518 DRM_WARN("RAS: Unsupported Interface");
1523 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1524 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1526 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1527 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1528 dev_warn(psp->adev->dev,
1529 "RAS internal register access blocked\n");
1531 psp_ras_ta_check_status(psp);
1537 int psp_ras_enable_features(struct psp_context *psp,
1538 union ta_ras_cmd_input *info, bool enable)
1540 struct ta_ras_shared_memory *ras_cmd;
1543 if (!psp->ras_context.context.initialized)
1546 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1547 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1550 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1552 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1554 ras_cmd->ras_in_message = *info;
1556 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1563 int psp_ras_terminate(struct psp_context *psp)
1568 * TODO: bypass the terminate in sriov for now
1570 if (amdgpu_sriov_vf(psp->adev))
1573 if (!psp->ras_context.context.initialized)
1576 ret = psp_ta_unload(psp, &psp->ras_context.context);
1578 psp->ras_context.context.initialized = false;
1583 int psp_ras_initialize(struct psp_context *psp)
1586 uint32_t boot_cfg = 0xFF;
1587 struct amdgpu_device *adev = psp->adev;
1588 struct ta_ras_shared_memory *ras_cmd;
1591 * TODO: bypass the initialize in sriov for now
1593 if (amdgpu_sriov_vf(adev))
1596 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1597 !adev->psp.ras_context.context.bin_desc.start_addr) {
1598 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1602 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1603 /* query GECC enablement status from boot config
1604 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1606 ret = psp_boot_config_get(adev, &boot_cfg);
1608 dev_warn(adev->dev, "PSP get boot config failed\n");
1610 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1612 dev_info(adev->dev, "GECC is disabled\n");
1614 /* disable GECC in next boot cycle if ras is
1615 * disabled by module parameter amdgpu_ras_enable
1616 * and/or amdgpu_ras_mask, or boot_config_get call
1619 ret = psp_boot_config_set(adev, 0);
1621 dev_warn(adev->dev, "PSP set boot config failed\n");
1623 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");
1626 if (boot_cfg == 1) {
1627 dev_info(adev->dev, "GECC is enabled\n");
1629 /* enable GECC in next boot cycle if it is disabled
1630 * in boot config, or force enable GECC if failed to
1631 * get boot configuration
1633 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1635 dev_warn(adev->dev, "PSP set boot config failed\n");
1637 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1642 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1643 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1645 if (!psp->ras_context.context.mem_context.shared_buf) {
1646 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1651 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1652 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1654 if (amdgpu_ras_is_poison_mode_supported(adev))
1655 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1656 if (!adev->gmc.xgmi.connected_to_cpu)
1657 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1659 ret = psp_ta_load(psp, &psp->ras_context.context);
1661 if (!ret && !ras_cmd->ras_status)
1662 psp->ras_context.context.initialized = true;
1664 if (ras_cmd->ras_status)
1665 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1667 /* fail to load RAS TA */
1668 psp->ras_context.context.initialized = false;
1674 int psp_ras_trigger_error(struct psp_context *psp,
1675 struct ta_ras_trigger_error_input *info)
1677 struct ta_ras_shared_memory *ras_cmd;
1680 if (!psp->ras_context.context.initialized)
1683 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1684 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1686 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1687 ras_cmd->ras_in_message.trigger_error = *info;
1689 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1693 /* If err_event_athub occurs error inject was successful, however
1694 return status from TA is no long reliable */
1695 if (amdgpu_ras_intr_triggered())
1698 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1700 else if (ras_cmd->ras_status)
1708 static int psp_hdcp_initialize(struct psp_context *psp)
1713 * TODO: bypass the initialize in sriov for now
1715 if (amdgpu_sriov_vf(psp->adev))
1718 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1719 !psp->hdcp_context.context.bin_desc.start_addr) {
1720 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1724 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1725 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1727 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1728 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1733 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1735 psp->hdcp_context.context.initialized = true;
1736 mutex_init(&psp->hdcp_context.mutex);
1742 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1745 * TODO: bypass the loading in sriov for now
1747 if (amdgpu_sriov_vf(psp->adev))
1750 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1753 static int psp_hdcp_terminate(struct psp_context *psp)
1758 * TODO: bypass the terminate in sriov for now
1760 if (amdgpu_sriov_vf(psp->adev))
1763 if (!psp->hdcp_context.context.initialized)
1766 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1768 psp->hdcp_context.context.initialized = false;
1775 static int psp_dtm_initialize(struct psp_context *psp)
1780 * TODO: bypass the initialize in sriov for now
1782 if (amdgpu_sriov_vf(psp->adev))
1785 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1786 !psp->dtm_context.context.bin_desc.start_addr) {
1787 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1791 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1792 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1794 if (!psp->dtm_context.context.mem_context.shared_buf) {
1795 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1800 ret = psp_ta_load(psp, &psp->dtm_context.context);
1802 psp->dtm_context.context.initialized = true;
1803 mutex_init(&psp->dtm_context.mutex);
1809 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1812 * TODO: bypass the loading in sriov for now
1814 if (amdgpu_sriov_vf(psp->adev))
1817 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1820 static int psp_dtm_terminate(struct psp_context *psp)
1825 * TODO: bypass the terminate in sriov for now
1827 if (amdgpu_sriov_vf(psp->adev))
1830 if (!psp->dtm_context.context.initialized)
1833 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1835 psp->dtm_context.context.initialized = false;
1842 static int psp_rap_initialize(struct psp_context *psp)
1845 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1848 * TODO: bypass the initialize in sriov for now
1850 if (amdgpu_sriov_vf(psp->adev))
1853 if (!psp->rap_context.context.bin_desc.size_bytes ||
1854 !psp->rap_context.context.bin_desc.start_addr) {
1855 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1859 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1860 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1862 if (!psp->rap_context.context.mem_context.shared_buf) {
1863 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1868 ret = psp_ta_load(psp, &psp->rap_context.context);
1870 psp->rap_context.context.initialized = true;
1871 mutex_init(&psp->rap_context.mutex);
1875 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1876 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1877 psp_rap_terminate(psp);
1878 /* free rap shared memory */
1879 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1881 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1890 static int psp_rap_terminate(struct psp_context *psp)
1894 if (!psp->rap_context.context.initialized)
1897 ret = psp_ta_unload(psp, &psp->rap_context.context);
1899 psp->rap_context.context.initialized = false;
1904 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1906 struct ta_rap_shared_memory *rap_cmd;
1909 if (!psp->rap_context.context.initialized)
1912 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1913 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1916 mutex_lock(&psp->rap_context.mutex);
1918 rap_cmd = (struct ta_rap_shared_memory *)
1919 psp->rap_context.context.mem_context.shared_buf;
1920 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1922 rap_cmd->cmd_id = ta_cmd_id;
1923 rap_cmd->validation_method_id = METHOD_A;
1925 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1930 *status = rap_cmd->rap_status;
1933 mutex_unlock(&psp->rap_context.mutex);
1939 /* securedisplay start */
1940 static int psp_securedisplay_initialize(struct psp_context *psp)
1943 struct ta_securedisplay_cmd *securedisplay_cmd;
1946 * TODO: bypass the initialize in sriov for now
1948 if (amdgpu_sriov_vf(psp->adev))
1951 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1952 !psp->securedisplay_context.context.bin_desc.start_addr) {
1953 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1957 psp->securedisplay_context.context.mem_context.shared_mem_size =
1958 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1959 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1961 if (!psp->securedisplay_context.context.initialized) {
1962 ret = psp_ta_init_shared_buf(psp,
1963 &psp->securedisplay_context.context.mem_context);
1968 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1970 psp->securedisplay_context.context.initialized = true;
1971 mutex_init(&psp->securedisplay_context.mutex);
1975 mutex_lock(&psp->securedisplay_context.mutex);
1977 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1978 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1980 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1982 mutex_unlock(&psp->securedisplay_context.mutex);
1985 psp_securedisplay_terminate(psp);
1986 /* free securedisplay shared memory */
1987 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1988 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1992 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1993 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1994 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1995 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
2001 static int psp_securedisplay_terminate(struct psp_context *psp)
2006 * TODO:bypass the terminate in sriov for now
2008 if (amdgpu_sriov_vf(psp->adev))
2011 if (!psp->securedisplay_context.context.initialized)
2014 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2016 psp->securedisplay_context.context.initialized = false;
2021 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2025 if (!psp->securedisplay_context.context.initialized)
2028 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2029 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2032 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2036 /* SECUREDISPLAY end */
2038 static int psp_hw_start(struct psp_context *psp)
2040 struct amdgpu_device *adev = psp->adev;
2043 if (!amdgpu_sriov_vf(adev)) {
2044 if ((is_psp_fw_valid(psp->kdb)) &&
2045 (psp->funcs->bootloader_load_kdb != NULL)) {
2046 ret = psp_bootloader_load_kdb(psp);
2048 DRM_ERROR("PSP load kdb failed!\n");
2053 if ((is_psp_fw_valid(psp->spl)) &&
2054 (psp->funcs->bootloader_load_spl != NULL)) {
2055 ret = psp_bootloader_load_spl(psp);
2057 DRM_ERROR("PSP load spl failed!\n");
2062 if ((is_psp_fw_valid(psp->sys)) &&
2063 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2064 ret = psp_bootloader_load_sysdrv(psp);
2066 DRM_ERROR("PSP load sys drv failed!\n");
2071 if ((is_psp_fw_valid(psp->soc_drv)) &&
2072 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2073 ret = psp_bootloader_load_soc_drv(psp);
2075 DRM_ERROR("PSP load soc drv failed!\n");
2080 if ((is_psp_fw_valid(psp->intf_drv)) &&
2081 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2082 ret = psp_bootloader_load_intf_drv(psp);
2084 DRM_ERROR("PSP load intf drv failed!\n");
2089 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2090 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2091 ret = psp_bootloader_load_dbg_drv(psp);
2093 DRM_ERROR("PSP load dbg drv failed!\n");
2098 if ((is_psp_fw_valid(psp->ras_drv)) &&
2099 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2100 ret = psp_bootloader_load_ras_drv(psp);
2102 DRM_ERROR("PSP load ras_drv failed!\n");
2107 if ((is_psp_fw_valid(psp->sos)) &&
2108 (psp->funcs->bootloader_load_sos != NULL)) {
2109 ret = psp_bootloader_load_sos(psp);
2111 DRM_ERROR("PSP load sos failed!\n");
2117 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2119 DRM_ERROR("PSP create ring failed!\n");
2123 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2126 if (!psp_boottime_tmr(psp)) {
2127 ret = psp_tmr_init(psp);
2129 DRM_ERROR("PSP tmr init failed!\n");
2136 * For ASICs with DF Cstate management centralized
2137 * to PMFW, TMR setup should be performed after PMFW
2138 * loaded and before other non-psp firmware loaded.
2140 if (psp->pmfw_centralized_cstate_management) {
2141 ret = psp_load_smu_fw(psp);
2146 ret = psp_tmr_load(psp);
2148 DRM_ERROR("PSP load tmr failed!\n");
2155 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2156 enum psp_gfx_fw_type *type)
2158 switch (ucode->ucode_id) {
2159 case AMDGPU_UCODE_ID_CAP:
2160 *type = GFX_FW_TYPE_CAP;
2162 case AMDGPU_UCODE_ID_SDMA0:
2163 *type = GFX_FW_TYPE_SDMA0;
2165 case AMDGPU_UCODE_ID_SDMA1:
2166 *type = GFX_FW_TYPE_SDMA1;
2168 case AMDGPU_UCODE_ID_SDMA2:
2169 *type = GFX_FW_TYPE_SDMA2;
2171 case AMDGPU_UCODE_ID_SDMA3:
2172 *type = GFX_FW_TYPE_SDMA3;
2174 case AMDGPU_UCODE_ID_SDMA4:
2175 *type = GFX_FW_TYPE_SDMA4;
2177 case AMDGPU_UCODE_ID_SDMA5:
2178 *type = GFX_FW_TYPE_SDMA5;
2180 case AMDGPU_UCODE_ID_SDMA6:
2181 *type = GFX_FW_TYPE_SDMA6;
2183 case AMDGPU_UCODE_ID_SDMA7:
2184 *type = GFX_FW_TYPE_SDMA7;
2186 case AMDGPU_UCODE_ID_CP_MES:
2187 *type = GFX_FW_TYPE_CP_MES;
2189 case AMDGPU_UCODE_ID_CP_MES_DATA:
2190 *type = GFX_FW_TYPE_MES_STACK;
2192 case AMDGPU_UCODE_ID_CP_MES1:
2193 *type = GFX_FW_TYPE_CP_MES_KIQ;
2195 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2196 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2198 case AMDGPU_UCODE_ID_CP_CE:
2199 *type = GFX_FW_TYPE_CP_CE;
2201 case AMDGPU_UCODE_ID_CP_PFP:
2202 *type = GFX_FW_TYPE_CP_PFP;
2204 case AMDGPU_UCODE_ID_CP_ME:
2205 *type = GFX_FW_TYPE_CP_ME;
2207 case AMDGPU_UCODE_ID_CP_MEC1:
2208 *type = GFX_FW_TYPE_CP_MEC;
2210 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2211 *type = GFX_FW_TYPE_CP_MEC_ME1;
2213 case AMDGPU_UCODE_ID_CP_MEC2:
2214 *type = GFX_FW_TYPE_CP_MEC;
2216 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2217 *type = GFX_FW_TYPE_CP_MEC_ME2;
2219 case AMDGPU_UCODE_ID_RLC_P:
2220 *type = GFX_FW_TYPE_RLC_P;
2222 case AMDGPU_UCODE_ID_RLC_V:
2223 *type = GFX_FW_TYPE_RLC_V;
2225 case AMDGPU_UCODE_ID_RLC_G:
2226 *type = GFX_FW_TYPE_RLC_G;
2228 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2229 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2231 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2232 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2234 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2235 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2237 case AMDGPU_UCODE_ID_RLC_IRAM:
2238 *type = GFX_FW_TYPE_RLC_IRAM;
2240 case AMDGPU_UCODE_ID_RLC_DRAM:
2241 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2243 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2244 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2246 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2247 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2249 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2250 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2252 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2253 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2255 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2256 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2258 case AMDGPU_UCODE_ID_SMC:
2259 *type = GFX_FW_TYPE_SMU;
2261 case AMDGPU_UCODE_ID_PPTABLE:
2262 *type = GFX_FW_TYPE_PPTABLE;
2264 case AMDGPU_UCODE_ID_UVD:
2265 *type = GFX_FW_TYPE_UVD;
2267 case AMDGPU_UCODE_ID_UVD1:
2268 *type = GFX_FW_TYPE_UVD1;
2270 case AMDGPU_UCODE_ID_VCE:
2271 *type = GFX_FW_TYPE_VCE;
2273 case AMDGPU_UCODE_ID_VCN:
2274 *type = GFX_FW_TYPE_VCN;
2276 case AMDGPU_UCODE_ID_VCN1:
2277 *type = GFX_FW_TYPE_VCN1;
2279 case AMDGPU_UCODE_ID_DMCU_ERAM:
2280 *type = GFX_FW_TYPE_DMCU_ERAM;
2282 case AMDGPU_UCODE_ID_DMCU_INTV:
2283 *type = GFX_FW_TYPE_DMCU_ISR;
2285 case AMDGPU_UCODE_ID_VCN0_RAM:
2286 *type = GFX_FW_TYPE_VCN0_RAM;
2288 case AMDGPU_UCODE_ID_VCN1_RAM:
2289 *type = GFX_FW_TYPE_VCN1_RAM;
2291 case AMDGPU_UCODE_ID_DMCUB:
2292 *type = GFX_FW_TYPE_DMUB;
2294 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2295 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2297 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2298 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2300 case AMDGPU_UCODE_ID_IMU_I:
2301 *type = GFX_FW_TYPE_IMU_I;
2303 case AMDGPU_UCODE_ID_IMU_D:
2304 *type = GFX_FW_TYPE_IMU_D;
2306 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2307 *type = GFX_FW_TYPE_RS64_PFP;
2309 case AMDGPU_UCODE_ID_CP_RS64_ME:
2310 *type = GFX_FW_TYPE_RS64_ME;
2312 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2313 *type = GFX_FW_TYPE_RS64_MEC;
2315 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2316 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2318 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2319 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2321 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2322 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2324 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2325 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2327 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2328 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2330 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2331 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2333 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2334 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2336 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2337 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2339 case AMDGPU_UCODE_ID_MAXIMUM:
2347 static void psp_print_fw_hdr(struct psp_context *psp,
2348 struct amdgpu_firmware_info *ucode)
2350 struct amdgpu_device *adev = psp->adev;
2351 struct common_firmware_header *hdr;
2353 switch (ucode->ucode_id) {
2354 case AMDGPU_UCODE_ID_SDMA0:
2355 case AMDGPU_UCODE_ID_SDMA1:
2356 case AMDGPU_UCODE_ID_SDMA2:
2357 case AMDGPU_UCODE_ID_SDMA3:
2358 case AMDGPU_UCODE_ID_SDMA4:
2359 case AMDGPU_UCODE_ID_SDMA5:
2360 case AMDGPU_UCODE_ID_SDMA6:
2361 case AMDGPU_UCODE_ID_SDMA7:
2362 hdr = (struct common_firmware_header *)
2363 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2364 amdgpu_ucode_print_sdma_hdr(hdr);
2366 case AMDGPU_UCODE_ID_CP_CE:
2367 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2368 amdgpu_ucode_print_gfx_hdr(hdr);
2370 case AMDGPU_UCODE_ID_CP_PFP:
2371 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2372 amdgpu_ucode_print_gfx_hdr(hdr);
2374 case AMDGPU_UCODE_ID_CP_ME:
2375 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2376 amdgpu_ucode_print_gfx_hdr(hdr);
2378 case AMDGPU_UCODE_ID_CP_MEC1:
2379 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2380 amdgpu_ucode_print_gfx_hdr(hdr);
2382 case AMDGPU_UCODE_ID_RLC_G:
2383 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2384 amdgpu_ucode_print_rlc_hdr(hdr);
2386 case AMDGPU_UCODE_ID_SMC:
2387 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2388 amdgpu_ucode_print_smc_hdr(hdr);
2395 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2396 struct psp_gfx_cmd_resp *cmd)
2399 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2401 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2402 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2403 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2404 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2406 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2408 DRM_ERROR("Unknown firmware type\n");
2413 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2414 struct amdgpu_firmware_info *ucode)
2417 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2419 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2421 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2422 psp->fence_buf_mc_addr);
2425 release_psp_cmd_buf(psp);
2430 static int psp_load_smu_fw(struct psp_context *psp)
2433 struct amdgpu_device *adev = psp->adev;
2434 struct amdgpu_firmware_info *ucode =
2435 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2436 struct amdgpu_ras *ras = psp->ras_context.ras;
2439 * Skip SMU FW reloading in case of using BACO for runpm only,
2440 * as SMU is always alive.
2442 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2445 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2448 if ((amdgpu_in_reset(adev) &&
2449 ras && adev->ras_enabled &&
2450 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2451 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2452 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2454 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2457 ret = psp_execute_non_psp_fw_load(psp, ucode);
2460 DRM_ERROR("PSP load smu failed!\n");
2465 static bool fw_load_skip_check(struct psp_context *psp,
2466 struct amdgpu_firmware_info *ucode)
2468 if (!ucode->fw || !ucode->ucode_size)
2471 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2472 (psp_smu_reload_quirk(psp) ||
2473 psp->autoload_supported ||
2474 psp->pmfw_centralized_cstate_management))
2477 if (amdgpu_sriov_vf(psp->adev) &&
2478 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2481 if (psp->autoload_supported &&
2482 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2483 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2484 /* skip mec JT when autoload is enabled */
2490 int psp_load_fw_list(struct psp_context *psp,
2491 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2494 struct amdgpu_firmware_info *ucode;
2496 for (i = 0; i < ucode_count; ++i) {
2497 ucode = ucode_list[i];
2498 psp_print_fw_hdr(psp, ucode);
2499 ret = psp_execute_non_psp_fw_load(psp, ucode);
2506 static int psp_load_non_psp_fw(struct psp_context *psp)
2509 struct amdgpu_firmware_info *ucode;
2510 struct amdgpu_device *adev = psp->adev;
2512 if (psp->autoload_supported &&
2513 !psp->pmfw_centralized_cstate_management) {
2514 ret = psp_load_smu_fw(psp);
2519 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2520 ucode = &adev->firmware.ucode[i];
2522 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2523 !fw_load_skip_check(psp, ucode)) {
2524 ret = psp_load_smu_fw(psp);
2530 if (fw_load_skip_check(psp, ucode))
2533 if (psp->autoload_supported &&
2534 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2535 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2536 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2537 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2538 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2539 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2540 /* PSP only receive one SDMA fw for sienna_cichlid,
2541 * as all four sdma fw are same */
2544 psp_print_fw_hdr(psp, ucode);
2546 ret = psp_execute_non_psp_fw_load(psp, ucode);
2550 /* Start rlc autoload after psp recieved all the gfx firmware */
2551 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2552 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2553 ret = psp_rlc_autoload_start(psp);
2555 DRM_ERROR("Failed to start rlc autoload\n");
2564 static int psp_load_fw(struct amdgpu_device *adev)
2567 struct psp_context *psp = &adev->psp;
2569 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2570 /* should not destroy ring, only stop */
2571 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2573 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2575 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2577 DRM_ERROR("PSP ring init failed!\n");
2582 ret = psp_hw_start(psp);
2586 ret = psp_load_non_psp_fw(psp);
2590 ret = psp_asd_initialize(psp);
2592 DRM_ERROR("PSP load asd failed!\n");
2596 ret = psp_rl_load(adev);
2598 DRM_ERROR("PSP load RL failed!\n");
2602 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2603 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2604 ret = psp_xgmi_initialize(psp, false, true);
2605 /* Warning the XGMI seesion initialize failure
2606 * Instead of stop driver initialization
2609 dev_err(psp->adev->dev,
2610 "XGMI: Failed to initialize XGMI session\n");
2615 ret = psp_ras_initialize(psp);
2617 dev_err(psp->adev->dev,
2618 "RAS: Failed to initialize RAS\n");
2620 ret = psp_hdcp_initialize(psp);
2622 dev_err(psp->adev->dev,
2623 "HDCP: Failed to initialize HDCP\n");
2625 ret = psp_dtm_initialize(psp);
2627 dev_err(psp->adev->dev,
2628 "DTM: Failed to initialize DTM\n");
2630 ret = psp_rap_initialize(psp);
2632 dev_err(psp->adev->dev,
2633 "RAP: Failed to initialize RAP\n");
2635 ret = psp_securedisplay_initialize(psp);
2637 dev_err(psp->adev->dev,
2638 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2644 psp_free_shared_bufs(psp);
2647 * all cleanup jobs (xgmi terminate, ras terminate,
2648 * ring destroy, cmd/fence/fw buffers destory,
2649 * psp->cmd destory) are delayed to psp_hw_fini
2651 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2655 static int psp_hw_init(void *handle)
2658 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2660 mutex_lock(&adev->firmware.mutex);
2662 * This sequence is just used on hw_init only once, no need on
2665 ret = amdgpu_ucode_init_bo(adev);
2669 ret = psp_load_fw(adev);
2671 DRM_ERROR("PSP firmware loading failed\n");
2675 mutex_unlock(&adev->firmware.mutex);
2679 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2680 mutex_unlock(&adev->firmware.mutex);
2684 static int psp_hw_fini(void *handle)
2686 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2687 struct psp_context *psp = &adev->psp;
2690 psp_ras_terminate(psp);
2691 psp_securedisplay_terminate(psp);
2692 psp_rap_terminate(psp);
2693 psp_dtm_terminate(psp);
2694 psp_hdcp_terminate(psp);
2696 if (adev->gmc.xgmi.num_physical_nodes > 1)
2697 psp_xgmi_terminate(psp);
2700 psp_asd_terminate(psp);
2701 psp_tmr_terminate(psp);
2703 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2708 static int psp_suspend(void *handle)
2711 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2712 struct psp_context *psp = &adev->psp;
2714 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2715 psp->xgmi_context.context.initialized) {
2716 ret = psp_xgmi_terminate(psp);
2718 DRM_ERROR("Failed to terminate xgmi ta\n");
2724 ret = psp_ras_terminate(psp);
2726 DRM_ERROR("Failed to terminate ras ta\n");
2729 ret = psp_hdcp_terminate(psp);
2731 DRM_ERROR("Failed to terminate hdcp ta\n");
2734 ret = psp_dtm_terminate(psp);
2736 DRM_ERROR("Failed to terminate dtm ta\n");
2739 ret = psp_rap_terminate(psp);
2741 DRM_ERROR("Failed to terminate rap ta\n");
2744 ret = psp_securedisplay_terminate(psp);
2746 DRM_ERROR("Failed to terminate securedisplay ta\n");
2751 ret = psp_asd_terminate(psp);
2753 DRM_ERROR("Failed to terminate asd\n");
2757 ret = psp_tmr_terminate(psp);
2759 DRM_ERROR("Failed to terminate tmr\n");
2763 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2765 DRM_ERROR("PSP ring stop failed\n");
2771 static int psp_resume(void *handle)
2774 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2775 struct psp_context *psp = &adev->psp;
2777 DRM_INFO("PSP is resuming...\n");
2779 if (psp->mem_train_ctx.enable_mem_training) {
2780 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2782 DRM_ERROR("Failed to process memory training!\n");
2787 mutex_lock(&adev->firmware.mutex);
2789 ret = psp_hw_start(psp);
2793 ret = psp_load_non_psp_fw(psp);
2797 ret = psp_asd_initialize(psp);
2799 DRM_ERROR("PSP load asd failed!\n");
2803 ret = psp_rl_load(adev);
2805 dev_err(adev->dev, "PSP load RL failed!\n");
2809 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2810 ret = psp_xgmi_initialize(psp, false, true);
2811 /* Warning the XGMI seesion initialize failure
2812 * Instead of stop driver initialization
2815 dev_err(psp->adev->dev,
2816 "XGMI: Failed to initialize XGMI session\n");
2820 ret = psp_ras_initialize(psp);
2822 dev_err(psp->adev->dev,
2823 "RAS: Failed to initialize RAS\n");
2825 ret = psp_hdcp_initialize(psp);
2827 dev_err(psp->adev->dev,
2828 "HDCP: Failed to initialize HDCP\n");
2830 ret = psp_dtm_initialize(psp);
2832 dev_err(psp->adev->dev,
2833 "DTM: Failed to initialize DTM\n");
2835 ret = psp_rap_initialize(psp);
2837 dev_err(psp->adev->dev,
2838 "RAP: Failed to initialize RAP\n");
2840 ret = psp_securedisplay_initialize(psp);
2842 dev_err(psp->adev->dev,
2843 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2846 mutex_unlock(&adev->firmware.mutex);
2851 DRM_ERROR("PSP resume failed\n");
2852 mutex_unlock(&adev->firmware.mutex);
2856 int psp_gpu_reset(struct amdgpu_device *adev)
2860 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2863 mutex_lock(&adev->psp.mutex);
2864 ret = psp_mode1_reset(&adev->psp);
2865 mutex_unlock(&adev->psp.mutex);
2870 int psp_rlc_autoload_start(struct psp_context *psp)
2873 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2875 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2877 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2878 psp->fence_buf_mc_addr);
2880 release_psp_cmd_buf(psp);
2885 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2886 uint64_t cmd_gpu_addr, int cmd_size)
2888 struct amdgpu_firmware_info ucode = {0};
2890 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2891 AMDGPU_UCODE_ID_VCN0_RAM;
2892 ucode.mc_addr = cmd_gpu_addr;
2893 ucode.ucode_size = cmd_size;
2895 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2898 int psp_ring_cmd_submit(struct psp_context *psp,
2899 uint64_t cmd_buf_mc_addr,
2900 uint64_t fence_mc_addr,
2903 unsigned int psp_write_ptr_reg = 0;
2904 struct psp_gfx_rb_frame *write_frame;
2905 struct psp_ring *ring = &psp->km_ring;
2906 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2907 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2908 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2909 struct amdgpu_device *adev = psp->adev;
2910 uint32_t ring_size_dw = ring->ring_size / 4;
2911 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2913 /* KM (GPCOM) prepare write pointer */
2914 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2916 /* Update KM RB frame pointer to new frame */
2917 /* write_frame ptr increments by size of rb_frame in bytes */
2918 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2919 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2920 write_frame = ring_buffer_start;
2922 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2923 /* Check invalid write_frame ptr address */
2924 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2925 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2926 ring_buffer_start, ring_buffer_end, write_frame);
2927 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2931 /* Initialize KM RB frame */
2932 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2934 /* Update KM RB frame */
2935 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2936 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2937 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2938 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2939 write_frame->fence_value = index;
2940 amdgpu_device_flush_hdp(adev, NULL);
2942 /* Update the write Pointer in DWORDs */
2943 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2944 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2948 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2950 struct amdgpu_device *adev = psp->adev;
2951 char fw_name[PSP_FW_NAME_LEN];
2952 const struct psp_firmware_header_v1_0 *asd_hdr;
2955 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2956 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2960 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2961 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2962 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2963 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2964 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2965 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2968 amdgpu_ucode_release(&adev->psp.asd_fw);
2972 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
2974 struct amdgpu_device *adev = psp->adev;
2975 char fw_name[PSP_FW_NAME_LEN];
2976 const struct psp_firmware_header_v1_0 *toc_hdr;
2979 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2980 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
2984 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2985 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2986 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2987 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2988 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2989 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2992 amdgpu_ucode_release(&adev->psp.toc_fw);
2996 static int parse_sos_bin_descriptor(struct psp_context *psp,
2997 const struct psp_fw_bin_desc *desc,
2998 const struct psp_firmware_header_v2_0 *sos_hdr)
3000 uint8_t *ucode_start_addr = NULL;
3002 if (!psp || !desc || !sos_hdr)
3005 ucode_start_addr = (uint8_t *)sos_hdr +
3006 le32_to_cpu(desc->offset_bytes) +
3007 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3009 switch (desc->fw_type) {
3010 case PSP_FW_TYPE_PSP_SOS:
3011 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3012 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3013 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3014 psp->sos.start_addr = ucode_start_addr;
3016 case PSP_FW_TYPE_PSP_SYS_DRV:
3017 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3018 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3019 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3020 psp->sys.start_addr = ucode_start_addr;
3022 case PSP_FW_TYPE_PSP_KDB:
3023 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3024 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3025 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3026 psp->kdb.start_addr = ucode_start_addr;
3028 case PSP_FW_TYPE_PSP_TOC:
3029 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3030 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3031 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3032 psp->toc.start_addr = ucode_start_addr;
3034 case PSP_FW_TYPE_PSP_SPL:
3035 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3036 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3037 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3038 psp->spl.start_addr = ucode_start_addr;
3040 case PSP_FW_TYPE_PSP_RL:
3041 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3042 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3043 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3044 psp->rl.start_addr = ucode_start_addr;
3046 case PSP_FW_TYPE_PSP_SOC_DRV:
3047 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3048 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3049 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3050 psp->soc_drv.start_addr = ucode_start_addr;
3052 case PSP_FW_TYPE_PSP_INTF_DRV:
3053 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3054 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3055 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3056 psp->intf_drv.start_addr = ucode_start_addr;
3058 case PSP_FW_TYPE_PSP_DBG_DRV:
3059 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3060 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3061 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3062 psp->dbg_drv.start_addr = ucode_start_addr;
3064 case PSP_FW_TYPE_PSP_RAS_DRV:
3065 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3066 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3067 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3068 psp->ras_drv.start_addr = ucode_start_addr;
3071 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3078 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3080 const struct psp_firmware_header_v1_0 *sos_hdr;
3081 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3082 uint8_t *ucode_array_start_addr;
3084 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3085 ucode_array_start_addr = (uint8_t *)sos_hdr +
3086 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3088 if (adev->gmc.xgmi.connected_to_cpu ||
3089 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3090 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3091 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3093 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3094 adev->psp.sys.start_addr = ucode_array_start_addr;
3096 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3097 adev->psp.sos.start_addr = ucode_array_start_addr +
3098 le32_to_cpu(sos_hdr->sos.offset_bytes);
3100 /* Load alternate PSP SOS FW */
3101 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3103 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3104 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3106 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3107 adev->psp.sys.start_addr = ucode_array_start_addr +
3108 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3110 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3111 adev->psp.sos.start_addr = ucode_array_start_addr +
3112 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3115 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3116 dev_warn(adev->dev, "PSP SOS FW not available");
3123 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3125 struct amdgpu_device *adev = psp->adev;
3126 char fw_name[PSP_FW_NAME_LEN];
3127 const struct psp_firmware_header_v1_0 *sos_hdr;
3128 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3129 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3130 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3131 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3133 uint8_t *ucode_array_start_addr;
3136 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3137 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3141 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3142 ucode_array_start_addr = (uint8_t *)sos_hdr +
3143 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3144 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3146 switch (sos_hdr->header.header_version_major) {
3148 err = psp_init_sos_base_fw(adev);
3152 if (sos_hdr->header.header_version_minor == 1) {
3153 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3154 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3155 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3156 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3157 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3158 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3159 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3161 if (sos_hdr->header.header_version_minor == 2) {
3162 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3163 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3164 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3165 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3167 if (sos_hdr->header.header_version_minor == 3) {
3168 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3169 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3170 adev->psp.toc.start_addr = ucode_array_start_addr +
3171 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3172 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3173 adev->psp.kdb.start_addr = ucode_array_start_addr +
3174 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3175 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3176 adev->psp.spl.start_addr = ucode_array_start_addr +
3177 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3178 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3179 adev->psp.rl.start_addr = ucode_array_start_addr +
3180 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3184 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3186 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3187 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3192 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3193 err = parse_sos_bin_descriptor(psp,
3194 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3202 "unsupported psp sos firmware\n");
3209 amdgpu_ucode_release(&adev->psp.sos_fw);
3214 static int parse_ta_bin_descriptor(struct psp_context *psp,
3215 const struct psp_fw_bin_desc *desc,
3216 const struct ta_firmware_header_v2_0 *ta_hdr)
3218 uint8_t *ucode_start_addr = NULL;
3220 if (!psp || !desc || !ta_hdr)
3223 ucode_start_addr = (uint8_t *)ta_hdr +
3224 le32_to_cpu(desc->offset_bytes) +
3225 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3227 switch (desc->fw_type) {
3228 case TA_FW_TYPE_PSP_ASD:
3229 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3230 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3231 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3232 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3234 case TA_FW_TYPE_PSP_XGMI:
3235 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3236 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3237 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3239 case TA_FW_TYPE_PSP_RAS:
3240 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3241 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3242 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3244 case TA_FW_TYPE_PSP_HDCP:
3245 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3246 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3247 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3249 case TA_FW_TYPE_PSP_DTM:
3250 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3251 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3252 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3254 case TA_FW_TYPE_PSP_RAP:
3255 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3256 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3257 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3259 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3260 psp->securedisplay_context.context.bin_desc.fw_version =
3261 le32_to_cpu(desc->fw_version);
3262 psp->securedisplay_context.context.bin_desc.size_bytes =
3263 le32_to_cpu(desc->size_bytes);
3264 psp->securedisplay_context.context.bin_desc.start_addr =
3268 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3275 static int parse_ta_v1_microcode(struct psp_context *psp)
3277 const struct ta_firmware_header_v1_0 *ta_hdr;
3278 struct amdgpu_device *adev = psp->adev;
3280 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3282 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3285 adev->psp.xgmi_context.context.bin_desc.fw_version =
3286 le32_to_cpu(ta_hdr->xgmi.fw_version);
3287 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3288 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3289 adev->psp.xgmi_context.context.bin_desc.start_addr =
3291 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3293 adev->psp.ras_context.context.bin_desc.fw_version =
3294 le32_to_cpu(ta_hdr->ras.fw_version);
3295 adev->psp.ras_context.context.bin_desc.size_bytes =
3296 le32_to_cpu(ta_hdr->ras.size_bytes);
3297 adev->psp.ras_context.context.bin_desc.start_addr =
3298 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3299 le32_to_cpu(ta_hdr->ras.offset_bytes);
3301 adev->psp.hdcp_context.context.bin_desc.fw_version =
3302 le32_to_cpu(ta_hdr->hdcp.fw_version);
3303 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3304 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3305 adev->psp.hdcp_context.context.bin_desc.start_addr =
3307 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3309 adev->psp.dtm_context.context.bin_desc.fw_version =
3310 le32_to_cpu(ta_hdr->dtm.fw_version);
3311 adev->psp.dtm_context.context.bin_desc.size_bytes =
3312 le32_to_cpu(ta_hdr->dtm.size_bytes);
3313 adev->psp.dtm_context.context.bin_desc.start_addr =
3314 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3315 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3317 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3318 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3319 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3320 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3321 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3322 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3323 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3325 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3330 static int parse_ta_v2_microcode(struct psp_context *psp)
3332 const struct ta_firmware_header_v2_0 *ta_hdr;
3333 struct amdgpu_device *adev = psp->adev;
3337 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3339 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3342 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3343 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3347 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3348 err = parse_ta_bin_descriptor(psp,
3349 &ta_hdr->ta_fw_bin[ta_index],
3358 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3360 const struct common_firmware_header *hdr;
3361 struct amdgpu_device *adev = psp->adev;
3362 char fw_name[PSP_FW_NAME_LEN];
3365 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3366 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3370 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3371 switch (le16_to_cpu(hdr->header_version_major)) {
3373 err = parse_ta_v1_microcode(psp);
3376 err = parse_ta_v2_microcode(psp);
3379 dev_err(adev->dev, "unsupported TA header version\n");
3384 amdgpu_ucode_release(&adev->psp.ta_fw);
3389 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3391 struct amdgpu_device *adev = psp->adev;
3392 char fw_name[PSP_FW_NAME_LEN];
3393 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3394 struct amdgpu_firmware_info *info = NULL;
3397 if (!amdgpu_sriov_vf(adev)) {
3398 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3402 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3403 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3405 if (err == -ENODEV) {
3406 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3410 dev_err(adev->dev, "fail to initialize cap microcode\n");
3413 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3414 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3415 info->fw = adev->psp.cap_fw;
3416 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3417 adev->psp.cap_fw->data;
3418 adev->firmware.fw_size += ALIGN(
3419 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3420 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3421 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3422 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3427 amdgpu_ucode_release(&adev->psp.cap_fw);
3431 static int psp_set_clockgating_state(void *handle,
3432 enum amd_clockgating_state state)
3437 static int psp_set_powergating_state(void *handle,
3438 enum amd_powergating_state state)
3443 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3444 struct device_attribute *attr,
3447 struct drm_device *ddev = dev_get_drvdata(dev);
3448 struct amdgpu_device *adev = drm_to_adev(ddev);
3452 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3453 DRM_INFO("PSP block is not ready yet.");
3457 mutex_lock(&adev->psp.mutex);
3458 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3459 mutex_unlock(&adev->psp.mutex);
3462 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3466 return sysfs_emit(buf, "%x\n", fw_ver);
3469 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3470 struct device_attribute *attr,
3474 struct drm_device *ddev = dev_get_drvdata(dev);
3475 struct amdgpu_device *adev = drm_to_adev(ddev);
3478 const struct firmware *usbc_pd_fw;
3479 struct amdgpu_bo *fw_buf_bo = NULL;
3480 uint64_t fw_pri_mc_addr;
3481 void *fw_pri_cpu_addr;
3483 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3484 DRM_INFO("PSP block is not ready yet.");
3488 if (!drm_dev_enter(ddev, &idx))
3491 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3492 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3496 /* LFB address which is aligned to 1MB boundary per PSP request */
3497 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3498 AMDGPU_GEM_DOMAIN_VRAM |
3499 AMDGPU_GEM_DOMAIN_GTT,
3500 &fw_buf_bo, &fw_pri_mc_addr,
3505 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3507 mutex_lock(&adev->psp.mutex);
3508 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3509 mutex_unlock(&adev->psp.mutex);
3511 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3514 release_firmware(usbc_pd_fw);
3517 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3525 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3529 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3532 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3533 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3538 static DEVICE_ATTR(usbc_pd_fw, 0644,
3539 psp_usbc_pd_fw_sysfs_read,
3540 psp_usbc_pd_fw_sysfs_write);
3542 int is_psp_fw_valid(struct psp_bin_desc bin)
3544 return bin.size_bytes;
3547 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3548 struct bin_attribute *bin_attr,
3549 char *buffer, loff_t pos, size_t count)
3551 struct device *dev = kobj_to_dev(kobj);
3552 struct drm_device *ddev = dev_get_drvdata(dev);
3553 struct amdgpu_device *adev = drm_to_adev(ddev);
3555 adev->psp.vbflash_done = false;
3557 /* Safeguard against memory drain */
3558 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3559 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3560 kvfree(adev->psp.vbflash_tmp_buf);
3561 adev->psp.vbflash_tmp_buf = NULL;
3562 adev->psp.vbflash_image_size = 0;
3566 /* TODO Just allocate max for now and optimize to realloc later if needed */
3567 if (!adev->psp.vbflash_tmp_buf) {
3568 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3569 if (!adev->psp.vbflash_tmp_buf)
3573 mutex_lock(&adev->psp.mutex);
3574 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3575 adev->psp.vbflash_image_size += count;
3576 mutex_unlock(&adev->psp.mutex);
3578 dev_info(adev->dev, "VBIOS flash write PSP done");
3583 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3584 struct bin_attribute *bin_attr, char *buffer,
3585 loff_t pos, size_t count)
3587 struct device *dev = kobj_to_dev(kobj);
3588 struct drm_device *ddev = dev_get_drvdata(dev);
3589 struct amdgpu_device *adev = drm_to_adev(ddev);
3590 struct amdgpu_bo *fw_buf_bo = NULL;
3591 uint64_t fw_pri_mc_addr;
3592 void *fw_pri_cpu_addr;
3595 dev_info(adev->dev, "VBIOS flash to PSP started");
3597 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3598 AMDGPU_GPU_PAGE_SIZE,
3599 AMDGPU_GEM_DOMAIN_VRAM,
3606 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3608 mutex_lock(&adev->psp.mutex);
3609 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3610 mutex_unlock(&adev->psp.mutex);
3612 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3615 kvfree(adev->psp.vbflash_tmp_buf);
3616 adev->psp.vbflash_tmp_buf = NULL;
3617 adev->psp.vbflash_image_size = 0;
3620 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3624 dev_info(adev->dev, "VBIOS flash to PSP done");
3628 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3629 struct device_attribute *attr,
3632 struct drm_device *ddev = dev_get_drvdata(dev);
3633 struct amdgpu_device *adev = drm_to_adev(ddev);
3634 uint32_t vbflash_status;
3636 vbflash_status = psp_vbflash_status(&adev->psp);
3637 if (!adev->psp.vbflash_done)
3639 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3642 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3645 static const struct bin_attribute psp_vbflash_bin_attr = {
3646 .attr = {.name = "psp_vbflash", .mode = 0664},
3648 .write = amdgpu_psp_vbflash_write,
3649 .read = amdgpu_psp_vbflash_read,
3652 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3654 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3657 struct psp_context *psp = &adev->psp;
3659 if (amdgpu_sriov_vf(adev))
3662 switch (adev->ip_versions[MP0_HWIP][0]) {
3663 case IP_VERSION(13, 0, 0):
3664 case IP_VERSION(13, 0, 7):
3665 case IP_VERSION(13, 0, 10):
3668 psp_v13_0_set_psp_funcs(psp);
3670 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3672 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3673 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3675 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3682 const struct amd_ip_funcs psp_ip_funcs = {
3684 .early_init = psp_early_init,
3686 .sw_init = psp_sw_init,
3687 .sw_fini = psp_sw_fini,
3688 .hw_init = psp_hw_init,
3689 .hw_fini = psp_hw_fini,
3690 .suspend = psp_suspend,
3691 .resume = psp_resume,
3693 .check_soft_reset = NULL,
3694 .wait_for_idle = NULL,
3696 .set_clockgating_state = psp_set_clockgating_state,
3697 .set_powergating_state = psp_set_powergating_state,
3700 static int psp_sysfs_init(struct amdgpu_device *adev)
3702 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3705 DRM_ERROR("Failed to create USBC PD FW control file!");
3710 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3712 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3713 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3716 static void psp_sysfs_fini(struct amdgpu_device *adev)
3718 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3721 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3722 .type = AMD_IP_BLOCK_TYPE_PSP,
3726 .funcs = &psp_ip_funcs,
3729 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3730 .type = AMD_IP_BLOCK_TYPE_PSP,
3734 .funcs = &psp_ip_funcs,
3737 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3738 .type = AMD_IP_BLOCK_TYPE_PSP,
3742 .funcs = &psp_ip_funcs,
3745 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3746 .type = AMD_IP_BLOCK_TYPE_PSP,
3750 .funcs = &psp_ip_funcs,
3753 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
3754 .type = AMD_IP_BLOCK_TYPE_PSP,
3758 .funcs = &psp_ip_funcs,
3761 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3762 .type = AMD_IP_BLOCK_TYPE_PSP,
3766 .funcs = &psp_ip_funcs,
3769 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3770 .type = AMD_IP_BLOCK_TYPE_PSP,
3774 .funcs = &psp_ip_funcs,