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 partitions 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;
1658 ras_cmd->ras_in_message.init_flags.xcc_mask =
1661 ret = psp_ta_load(psp, &psp->ras_context.context);
1663 if (!ret && !ras_cmd->ras_status)
1664 psp->ras_context.context.initialized = true;
1666 if (ras_cmd->ras_status)
1667 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1669 /* fail to load RAS TA */
1670 psp->ras_context.context.initialized = false;
1676 int psp_ras_trigger_error(struct psp_context *psp,
1677 struct ta_ras_trigger_error_input *info, uint32_t instance_mask)
1679 struct ta_ras_shared_memory *ras_cmd;
1680 struct amdgpu_device *adev = psp->adev;
1684 if (!psp->ras_context.context.initialized)
1687 switch (info->block_id) {
1688 case TA_RAS_BLOCK__GFX:
1689 dev_mask = GET_MASK(GC, instance_mask);
1691 case TA_RAS_BLOCK__SDMA:
1692 dev_mask = GET_MASK(SDMA0, instance_mask);
1694 case TA_RAS_BLOCK__VCN:
1695 case TA_RAS_BLOCK__JPEG:
1696 dev_mask = GET_MASK(VCN, instance_mask);
1699 dev_mask = instance_mask;
1703 /* reuse sub_block_index for backward compatibility */
1704 dev_mask <<= AMDGPU_RAS_INST_SHIFT;
1705 dev_mask &= AMDGPU_RAS_INST_MASK;
1706 info->sub_block_index |= dev_mask;
1708 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1709 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1711 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1712 ras_cmd->ras_in_message.trigger_error = *info;
1714 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1718 /* If err_event_athub occurs error inject was successful, however
1719 return status from TA is no long reliable */
1720 if (amdgpu_ras_intr_triggered())
1723 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1725 else if (ras_cmd->ras_status)
1733 static int psp_hdcp_initialize(struct psp_context *psp)
1738 * TODO: bypass the initialize in sriov for now
1740 if (amdgpu_sriov_vf(psp->adev))
1743 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1744 !psp->hdcp_context.context.bin_desc.start_addr) {
1745 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1749 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1750 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1752 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1753 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1758 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1760 psp->hdcp_context.context.initialized = true;
1761 mutex_init(&psp->hdcp_context.mutex);
1767 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1770 * TODO: bypass the loading in sriov for now
1772 if (amdgpu_sriov_vf(psp->adev))
1775 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1778 static int psp_hdcp_terminate(struct psp_context *psp)
1783 * TODO: bypass the terminate in sriov for now
1785 if (amdgpu_sriov_vf(psp->adev))
1788 if (!psp->hdcp_context.context.initialized)
1791 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1793 psp->hdcp_context.context.initialized = false;
1800 static int psp_dtm_initialize(struct psp_context *psp)
1805 * TODO: bypass the initialize in sriov for now
1807 if (amdgpu_sriov_vf(psp->adev))
1810 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1811 !psp->dtm_context.context.bin_desc.start_addr) {
1812 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1816 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1817 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1819 if (!psp->dtm_context.context.mem_context.shared_buf) {
1820 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1825 ret = psp_ta_load(psp, &psp->dtm_context.context);
1827 psp->dtm_context.context.initialized = true;
1828 mutex_init(&psp->dtm_context.mutex);
1834 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1837 * TODO: bypass the loading in sriov for now
1839 if (amdgpu_sriov_vf(psp->adev))
1842 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1845 static int psp_dtm_terminate(struct psp_context *psp)
1850 * TODO: bypass the terminate in sriov for now
1852 if (amdgpu_sriov_vf(psp->adev))
1855 if (!psp->dtm_context.context.initialized)
1858 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1860 psp->dtm_context.context.initialized = false;
1867 static int psp_rap_initialize(struct psp_context *psp)
1870 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1873 * TODO: bypass the initialize in sriov for now
1875 if (amdgpu_sriov_vf(psp->adev))
1878 if (!psp->rap_context.context.bin_desc.size_bytes ||
1879 !psp->rap_context.context.bin_desc.start_addr) {
1880 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1884 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1885 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1887 if (!psp->rap_context.context.mem_context.shared_buf) {
1888 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1893 ret = psp_ta_load(psp, &psp->rap_context.context);
1895 psp->rap_context.context.initialized = true;
1896 mutex_init(&psp->rap_context.mutex);
1900 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1901 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1902 psp_rap_terminate(psp);
1903 /* free rap shared memory */
1904 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1906 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1915 static int psp_rap_terminate(struct psp_context *psp)
1919 if (!psp->rap_context.context.initialized)
1922 ret = psp_ta_unload(psp, &psp->rap_context.context);
1924 psp->rap_context.context.initialized = false;
1929 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1931 struct ta_rap_shared_memory *rap_cmd;
1934 if (!psp->rap_context.context.initialized)
1937 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1938 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1941 mutex_lock(&psp->rap_context.mutex);
1943 rap_cmd = (struct ta_rap_shared_memory *)
1944 psp->rap_context.context.mem_context.shared_buf;
1945 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1947 rap_cmd->cmd_id = ta_cmd_id;
1948 rap_cmd->validation_method_id = METHOD_A;
1950 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1955 *status = rap_cmd->rap_status;
1958 mutex_unlock(&psp->rap_context.mutex);
1964 /* securedisplay start */
1965 static int psp_securedisplay_initialize(struct psp_context *psp)
1968 struct ta_securedisplay_cmd *securedisplay_cmd;
1971 * TODO: bypass the initialize in sriov for now
1973 if (amdgpu_sriov_vf(psp->adev))
1976 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1977 !psp->securedisplay_context.context.bin_desc.start_addr) {
1978 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1982 psp->securedisplay_context.context.mem_context.shared_mem_size =
1983 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1984 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1986 if (!psp->securedisplay_context.context.initialized) {
1987 ret = psp_ta_init_shared_buf(psp,
1988 &psp->securedisplay_context.context.mem_context);
1993 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1995 psp->securedisplay_context.context.initialized = true;
1996 mutex_init(&psp->securedisplay_context.mutex);
2000 mutex_lock(&psp->securedisplay_context.mutex);
2002 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
2003 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2005 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2007 mutex_unlock(&psp->securedisplay_context.mutex);
2010 psp_securedisplay_terminate(psp);
2011 /* free securedisplay shared memory */
2012 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
2013 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
2017 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
2018 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
2019 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
2020 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
2026 static int psp_securedisplay_terminate(struct psp_context *psp)
2031 * TODO:bypass the terminate in sriov for now
2033 if (amdgpu_sriov_vf(psp->adev))
2036 if (!psp->securedisplay_context.context.initialized)
2039 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2041 psp->securedisplay_context.context.initialized = false;
2046 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2050 if (!psp->securedisplay_context.context.initialized)
2053 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2054 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2057 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2061 /* SECUREDISPLAY end */
2063 static int psp_hw_start(struct psp_context *psp)
2065 struct amdgpu_device *adev = psp->adev;
2068 if (!amdgpu_sriov_vf(adev)) {
2069 if ((is_psp_fw_valid(psp->kdb)) &&
2070 (psp->funcs->bootloader_load_kdb != NULL)) {
2071 ret = psp_bootloader_load_kdb(psp);
2073 DRM_ERROR("PSP load kdb failed!\n");
2078 if ((is_psp_fw_valid(psp->spl)) &&
2079 (psp->funcs->bootloader_load_spl != NULL)) {
2080 ret = psp_bootloader_load_spl(psp);
2082 DRM_ERROR("PSP load spl failed!\n");
2087 if ((is_psp_fw_valid(psp->sys)) &&
2088 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2089 ret = psp_bootloader_load_sysdrv(psp);
2091 DRM_ERROR("PSP load sys drv failed!\n");
2096 if ((is_psp_fw_valid(psp->soc_drv)) &&
2097 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2098 ret = psp_bootloader_load_soc_drv(psp);
2100 DRM_ERROR("PSP load soc drv failed!\n");
2105 if ((is_psp_fw_valid(psp->intf_drv)) &&
2106 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2107 ret = psp_bootloader_load_intf_drv(psp);
2109 DRM_ERROR("PSP load intf drv failed!\n");
2114 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2115 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2116 ret = psp_bootloader_load_dbg_drv(psp);
2118 DRM_ERROR("PSP load dbg drv failed!\n");
2123 if ((is_psp_fw_valid(psp->ras_drv)) &&
2124 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2125 ret = psp_bootloader_load_ras_drv(psp);
2127 DRM_ERROR("PSP load ras_drv failed!\n");
2132 if ((is_psp_fw_valid(psp->sos)) &&
2133 (psp->funcs->bootloader_load_sos != NULL)) {
2134 ret = psp_bootloader_load_sos(psp);
2136 DRM_ERROR("PSP load sos failed!\n");
2142 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2144 DRM_ERROR("PSP create ring failed!\n");
2148 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2151 if (!psp_boottime_tmr(psp)) {
2152 ret = psp_tmr_init(psp);
2154 DRM_ERROR("PSP tmr init failed!\n");
2161 * For ASICs with DF Cstate management centralized
2162 * to PMFW, TMR setup should be performed after PMFW
2163 * loaded and before other non-psp firmware loaded.
2165 if (psp->pmfw_centralized_cstate_management) {
2166 ret = psp_load_smu_fw(psp);
2171 ret = psp_tmr_load(psp);
2173 DRM_ERROR("PSP load tmr failed!\n");
2180 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2181 enum psp_gfx_fw_type *type)
2183 switch (ucode->ucode_id) {
2184 case AMDGPU_UCODE_ID_CAP:
2185 *type = GFX_FW_TYPE_CAP;
2187 case AMDGPU_UCODE_ID_SDMA0:
2188 *type = GFX_FW_TYPE_SDMA0;
2190 case AMDGPU_UCODE_ID_SDMA1:
2191 *type = GFX_FW_TYPE_SDMA1;
2193 case AMDGPU_UCODE_ID_SDMA2:
2194 *type = GFX_FW_TYPE_SDMA2;
2196 case AMDGPU_UCODE_ID_SDMA3:
2197 *type = GFX_FW_TYPE_SDMA3;
2199 case AMDGPU_UCODE_ID_SDMA4:
2200 *type = GFX_FW_TYPE_SDMA4;
2202 case AMDGPU_UCODE_ID_SDMA5:
2203 *type = GFX_FW_TYPE_SDMA5;
2205 case AMDGPU_UCODE_ID_SDMA6:
2206 *type = GFX_FW_TYPE_SDMA6;
2208 case AMDGPU_UCODE_ID_SDMA7:
2209 *type = GFX_FW_TYPE_SDMA7;
2211 case AMDGPU_UCODE_ID_CP_MES:
2212 *type = GFX_FW_TYPE_CP_MES;
2214 case AMDGPU_UCODE_ID_CP_MES_DATA:
2215 *type = GFX_FW_TYPE_MES_STACK;
2217 case AMDGPU_UCODE_ID_CP_MES1:
2218 *type = GFX_FW_TYPE_CP_MES_KIQ;
2220 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2221 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2223 case AMDGPU_UCODE_ID_CP_CE:
2224 *type = GFX_FW_TYPE_CP_CE;
2226 case AMDGPU_UCODE_ID_CP_PFP:
2227 *type = GFX_FW_TYPE_CP_PFP;
2229 case AMDGPU_UCODE_ID_CP_ME:
2230 *type = GFX_FW_TYPE_CP_ME;
2232 case AMDGPU_UCODE_ID_CP_MEC1:
2233 *type = GFX_FW_TYPE_CP_MEC;
2235 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2236 *type = GFX_FW_TYPE_CP_MEC_ME1;
2238 case AMDGPU_UCODE_ID_CP_MEC2:
2239 *type = GFX_FW_TYPE_CP_MEC;
2241 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2242 *type = GFX_FW_TYPE_CP_MEC_ME2;
2244 case AMDGPU_UCODE_ID_RLC_P:
2245 *type = GFX_FW_TYPE_RLC_P;
2247 case AMDGPU_UCODE_ID_RLC_V:
2248 *type = GFX_FW_TYPE_RLC_V;
2250 case AMDGPU_UCODE_ID_RLC_G:
2251 *type = GFX_FW_TYPE_RLC_G;
2253 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2254 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2256 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2257 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2259 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2260 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2262 case AMDGPU_UCODE_ID_RLC_IRAM:
2263 *type = GFX_FW_TYPE_RLC_IRAM;
2265 case AMDGPU_UCODE_ID_RLC_DRAM:
2266 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2268 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2269 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2271 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2272 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2274 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2275 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2277 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2278 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2280 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2281 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2283 case AMDGPU_UCODE_ID_SMC:
2284 *type = GFX_FW_TYPE_SMU;
2286 case AMDGPU_UCODE_ID_PPTABLE:
2287 *type = GFX_FW_TYPE_PPTABLE;
2289 case AMDGPU_UCODE_ID_UVD:
2290 *type = GFX_FW_TYPE_UVD;
2292 case AMDGPU_UCODE_ID_UVD1:
2293 *type = GFX_FW_TYPE_UVD1;
2295 case AMDGPU_UCODE_ID_VCE:
2296 *type = GFX_FW_TYPE_VCE;
2298 case AMDGPU_UCODE_ID_VCN:
2299 *type = GFX_FW_TYPE_VCN;
2301 case AMDGPU_UCODE_ID_VCN1:
2302 *type = GFX_FW_TYPE_VCN1;
2304 case AMDGPU_UCODE_ID_DMCU_ERAM:
2305 *type = GFX_FW_TYPE_DMCU_ERAM;
2307 case AMDGPU_UCODE_ID_DMCU_INTV:
2308 *type = GFX_FW_TYPE_DMCU_ISR;
2310 case AMDGPU_UCODE_ID_VCN0_RAM:
2311 *type = GFX_FW_TYPE_VCN0_RAM;
2313 case AMDGPU_UCODE_ID_VCN1_RAM:
2314 *type = GFX_FW_TYPE_VCN1_RAM;
2316 case AMDGPU_UCODE_ID_DMCUB:
2317 *type = GFX_FW_TYPE_DMUB;
2319 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2320 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2322 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2323 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2325 case AMDGPU_UCODE_ID_IMU_I:
2326 *type = GFX_FW_TYPE_IMU_I;
2328 case AMDGPU_UCODE_ID_IMU_D:
2329 *type = GFX_FW_TYPE_IMU_D;
2331 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2332 *type = GFX_FW_TYPE_RS64_PFP;
2334 case AMDGPU_UCODE_ID_CP_RS64_ME:
2335 *type = GFX_FW_TYPE_RS64_ME;
2337 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2338 *type = GFX_FW_TYPE_RS64_MEC;
2340 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2341 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2343 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2344 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2346 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2347 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2349 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2350 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2352 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2353 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2355 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2356 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2358 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2359 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2361 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2362 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2364 case AMDGPU_UCODE_ID_MAXIMUM:
2372 static void psp_print_fw_hdr(struct psp_context *psp,
2373 struct amdgpu_firmware_info *ucode)
2375 struct amdgpu_device *adev = psp->adev;
2376 struct common_firmware_header *hdr;
2378 switch (ucode->ucode_id) {
2379 case AMDGPU_UCODE_ID_SDMA0:
2380 case AMDGPU_UCODE_ID_SDMA1:
2381 case AMDGPU_UCODE_ID_SDMA2:
2382 case AMDGPU_UCODE_ID_SDMA3:
2383 case AMDGPU_UCODE_ID_SDMA4:
2384 case AMDGPU_UCODE_ID_SDMA5:
2385 case AMDGPU_UCODE_ID_SDMA6:
2386 case AMDGPU_UCODE_ID_SDMA7:
2387 hdr = (struct common_firmware_header *)
2388 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2389 amdgpu_ucode_print_sdma_hdr(hdr);
2391 case AMDGPU_UCODE_ID_CP_CE:
2392 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2393 amdgpu_ucode_print_gfx_hdr(hdr);
2395 case AMDGPU_UCODE_ID_CP_PFP:
2396 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2397 amdgpu_ucode_print_gfx_hdr(hdr);
2399 case AMDGPU_UCODE_ID_CP_ME:
2400 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2401 amdgpu_ucode_print_gfx_hdr(hdr);
2403 case AMDGPU_UCODE_ID_CP_MEC1:
2404 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2405 amdgpu_ucode_print_gfx_hdr(hdr);
2407 case AMDGPU_UCODE_ID_RLC_G:
2408 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2409 amdgpu_ucode_print_rlc_hdr(hdr);
2411 case AMDGPU_UCODE_ID_SMC:
2412 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2413 amdgpu_ucode_print_smc_hdr(hdr);
2420 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2421 struct psp_gfx_cmd_resp *cmd)
2424 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2426 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2427 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2428 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2429 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2431 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2433 DRM_ERROR("Unknown firmware type\n");
2438 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2439 struct amdgpu_firmware_info *ucode)
2442 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2444 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2446 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2447 psp->fence_buf_mc_addr);
2450 release_psp_cmd_buf(psp);
2455 static int psp_load_smu_fw(struct psp_context *psp)
2458 struct amdgpu_device *adev = psp->adev;
2459 struct amdgpu_firmware_info *ucode =
2460 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2461 struct amdgpu_ras *ras = psp->ras_context.ras;
2464 * Skip SMU FW reloading in case of using BACO for runpm only,
2465 * as SMU is always alive.
2467 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2470 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2473 if ((amdgpu_in_reset(adev) &&
2474 ras && adev->ras_enabled &&
2475 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2476 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2477 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2479 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2482 ret = psp_execute_non_psp_fw_load(psp, ucode);
2485 DRM_ERROR("PSP load smu failed!\n");
2490 static bool fw_load_skip_check(struct psp_context *psp,
2491 struct amdgpu_firmware_info *ucode)
2493 if (!ucode->fw || !ucode->ucode_size)
2496 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2497 (psp_smu_reload_quirk(psp) ||
2498 psp->autoload_supported ||
2499 psp->pmfw_centralized_cstate_management))
2502 if (amdgpu_sriov_vf(psp->adev) &&
2503 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2506 if (psp->autoload_supported &&
2507 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2508 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2509 /* skip mec JT when autoload is enabled */
2515 int psp_load_fw_list(struct psp_context *psp,
2516 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2519 struct amdgpu_firmware_info *ucode;
2521 for (i = 0; i < ucode_count; ++i) {
2522 ucode = ucode_list[i];
2523 psp_print_fw_hdr(psp, ucode);
2524 ret = psp_execute_non_psp_fw_load(psp, ucode);
2531 static int psp_load_non_psp_fw(struct psp_context *psp)
2534 struct amdgpu_firmware_info *ucode;
2535 struct amdgpu_device *adev = psp->adev;
2537 if (psp->autoload_supported &&
2538 !psp->pmfw_centralized_cstate_management) {
2539 ret = psp_load_smu_fw(psp);
2544 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2545 ucode = &adev->firmware.ucode[i];
2547 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2548 !fw_load_skip_check(psp, ucode)) {
2549 ret = psp_load_smu_fw(psp);
2555 if (fw_load_skip_check(psp, ucode))
2558 if (psp->autoload_supported &&
2559 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2560 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2561 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2562 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2563 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2564 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2565 /* PSP only receive one SDMA fw for sienna_cichlid,
2566 * as all four sdma fw are same */
2569 psp_print_fw_hdr(psp, ucode);
2571 ret = psp_execute_non_psp_fw_load(psp, ucode);
2575 /* Start rlc autoload after psp recieved all the gfx firmware */
2576 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2577 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2578 ret = psp_rlc_autoload_start(psp);
2580 DRM_ERROR("Failed to start rlc autoload\n");
2589 static int psp_load_fw(struct amdgpu_device *adev)
2592 struct psp_context *psp = &adev->psp;
2594 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2595 /* should not destroy ring, only stop */
2596 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2598 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2600 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2602 DRM_ERROR("PSP ring init failed!\n");
2607 ret = psp_hw_start(psp);
2611 ret = psp_load_non_psp_fw(psp);
2615 ret = psp_asd_initialize(psp);
2617 DRM_ERROR("PSP load asd failed!\n");
2621 ret = psp_rl_load(adev);
2623 DRM_ERROR("PSP load RL failed!\n");
2627 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2628 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2629 ret = psp_xgmi_initialize(psp, false, true);
2630 /* Warning the XGMI seesion initialize failure
2631 * Instead of stop driver initialization
2634 dev_err(psp->adev->dev,
2635 "XGMI: Failed to initialize XGMI session\n");
2640 ret = psp_ras_initialize(psp);
2642 dev_err(psp->adev->dev,
2643 "RAS: Failed to initialize RAS\n");
2645 ret = psp_hdcp_initialize(psp);
2647 dev_err(psp->adev->dev,
2648 "HDCP: Failed to initialize HDCP\n");
2650 ret = psp_dtm_initialize(psp);
2652 dev_err(psp->adev->dev,
2653 "DTM: Failed to initialize DTM\n");
2655 ret = psp_rap_initialize(psp);
2657 dev_err(psp->adev->dev,
2658 "RAP: Failed to initialize RAP\n");
2660 ret = psp_securedisplay_initialize(psp);
2662 dev_err(psp->adev->dev,
2663 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2669 psp_free_shared_bufs(psp);
2672 * all cleanup jobs (xgmi terminate, ras terminate,
2673 * ring destroy, cmd/fence/fw buffers destory,
2674 * psp->cmd destory) are delayed to psp_hw_fini
2676 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2680 static int psp_hw_init(void *handle)
2683 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2685 mutex_lock(&adev->firmware.mutex);
2687 * This sequence is just used on hw_init only once, no need on
2690 ret = amdgpu_ucode_init_bo(adev);
2694 ret = psp_load_fw(adev);
2696 DRM_ERROR("PSP firmware loading failed\n");
2700 mutex_unlock(&adev->firmware.mutex);
2704 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2705 mutex_unlock(&adev->firmware.mutex);
2709 static int psp_hw_fini(void *handle)
2711 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2712 struct psp_context *psp = &adev->psp;
2715 psp_ras_terminate(psp);
2716 psp_securedisplay_terminate(psp);
2717 psp_rap_terminate(psp);
2718 psp_dtm_terminate(psp);
2719 psp_hdcp_terminate(psp);
2721 if (adev->gmc.xgmi.num_physical_nodes > 1)
2722 psp_xgmi_terminate(psp);
2725 psp_asd_terminate(psp);
2726 psp_tmr_terminate(psp);
2728 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2733 static int psp_suspend(void *handle)
2736 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2737 struct psp_context *psp = &adev->psp;
2739 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2740 psp->xgmi_context.context.initialized) {
2741 ret = psp_xgmi_terminate(psp);
2743 DRM_ERROR("Failed to terminate xgmi ta\n");
2749 ret = psp_ras_terminate(psp);
2751 DRM_ERROR("Failed to terminate ras ta\n");
2754 ret = psp_hdcp_terminate(psp);
2756 DRM_ERROR("Failed to terminate hdcp ta\n");
2759 ret = psp_dtm_terminate(psp);
2761 DRM_ERROR("Failed to terminate dtm ta\n");
2764 ret = psp_rap_terminate(psp);
2766 DRM_ERROR("Failed to terminate rap ta\n");
2769 ret = psp_securedisplay_terminate(psp);
2771 DRM_ERROR("Failed to terminate securedisplay ta\n");
2776 ret = psp_asd_terminate(psp);
2778 DRM_ERROR("Failed to terminate asd\n");
2782 ret = psp_tmr_terminate(psp);
2784 DRM_ERROR("Failed to terminate tmr\n");
2788 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2790 DRM_ERROR("PSP ring stop failed\n");
2796 static int psp_resume(void *handle)
2799 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2800 struct psp_context *psp = &adev->psp;
2802 DRM_INFO("PSP is resuming...\n");
2804 if (psp->mem_train_ctx.enable_mem_training) {
2805 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2807 DRM_ERROR("Failed to process memory training!\n");
2812 mutex_lock(&adev->firmware.mutex);
2814 ret = psp_hw_start(psp);
2818 ret = psp_load_non_psp_fw(psp);
2822 ret = psp_asd_initialize(psp);
2824 DRM_ERROR("PSP load asd failed!\n");
2828 ret = psp_rl_load(adev);
2830 dev_err(adev->dev, "PSP load RL failed!\n");
2834 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2835 ret = psp_xgmi_initialize(psp, false, true);
2836 /* Warning the XGMI seesion initialize failure
2837 * Instead of stop driver initialization
2840 dev_err(psp->adev->dev,
2841 "XGMI: Failed to initialize XGMI session\n");
2845 ret = psp_ras_initialize(psp);
2847 dev_err(psp->adev->dev,
2848 "RAS: Failed to initialize RAS\n");
2850 ret = psp_hdcp_initialize(psp);
2852 dev_err(psp->adev->dev,
2853 "HDCP: Failed to initialize HDCP\n");
2855 ret = psp_dtm_initialize(psp);
2857 dev_err(psp->adev->dev,
2858 "DTM: Failed to initialize DTM\n");
2860 ret = psp_rap_initialize(psp);
2862 dev_err(psp->adev->dev,
2863 "RAP: Failed to initialize RAP\n");
2865 ret = psp_securedisplay_initialize(psp);
2867 dev_err(psp->adev->dev,
2868 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2871 mutex_unlock(&adev->firmware.mutex);
2876 DRM_ERROR("PSP resume failed\n");
2877 mutex_unlock(&adev->firmware.mutex);
2881 int psp_gpu_reset(struct amdgpu_device *adev)
2885 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2888 mutex_lock(&adev->psp.mutex);
2889 ret = psp_mode1_reset(&adev->psp);
2890 mutex_unlock(&adev->psp.mutex);
2895 int psp_rlc_autoload_start(struct psp_context *psp)
2898 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2900 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2902 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2903 psp->fence_buf_mc_addr);
2905 release_psp_cmd_buf(psp);
2910 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2911 uint64_t cmd_gpu_addr, int cmd_size)
2913 struct amdgpu_firmware_info ucode = {0};
2915 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2916 AMDGPU_UCODE_ID_VCN0_RAM;
2917 ucode.mc_addr = cmd_gpu_addr;
2918 ucode.ucode_size = cmd_size;
2920 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2923 int psp_ring_cmd_submit(struct psp_context *psp,
2924 uint64_t cmd_buf_mc_addr,
2925 uint64_t fence_mc_addr,
2928 unsigned int psp_write_ptr_reg = 0;
2929 struct psp_gfx_rb_frame *write_frame;
2930 struct psp_ring *ring = &psp->km_ring;
2931 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2932 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2933 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2934 struct amdgpu_device *adev = psp->adev;
2935 uint32_t ring_size_dw = ring->ring_size / 4;
2936 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2938 /* KM (GPCOM) prepare write pointer */
2939 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2941 /* Update KM RB frame pointer to new frame */
2942 /* write_frame ptr increments by size of rb_frame in bytes */
2943 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2944 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2945 write_frame = ring_buffer_start;
2947 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2948 /* Check invalid write_frame ptr address */
2949 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2950 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2951 ring_buffer_start, ring_buffer_end, write_frame);
2952 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2956 /* Initialize KM RB frame */
2957 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2959 /* Update KM RB frame */
2960 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2961 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2962 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2963 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2964 write_frame->fence_value = index;
2965 amdgpu_device_flush_hdp(adev, NULL);
2967 /* Update the write Pointer in DWORDs */
2968 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2969 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2973 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2975 struct amdgpu_device *adev = psp->adev;
2976 char fw_name[PSP_FW_NAME_LEN];
2977 const struct psp_firmware_header_v1_0 *asd_hdr;
2980 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2981 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2985 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2986 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2987 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2988 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2989 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2990 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2993 amdgpu_ucode_release(&adev->psp.asd_fw);
2997 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
2999 struct amdgpu_device *adev = psp->adev;
3000 char fw_name[PSP_FW_NAME_LEN];
3001 const struct psp_firmware_header_v1_0 *toc_hdr;
3004 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
3005 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
3009 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
3010 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
3011 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
3012 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
3013 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
3014 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
3017 amdgpu_ucode_release(&adev->psp.toc_fw);
3021 static int parse_sos_bin_descriptor(struct psp_context *psp,
3022 const struct psp_fw_bin_desc *desc,
3023 const struct psp_firmware_header_v2_0 *sos_hdr)
3025 uint8_t *ucode_start_addr = NULL;
3027 if (!psp || !desc || !sos_hdr)
3030 ucode_start_addr = (uint8_t *)sos_hdr +
3031 le32_to_cpu(desc->offset_bytes) +
3032 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3034 switch (desc->fw_type) {
3035 case PSP_FW_TYPE_PSP_SOS:
3036 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3037 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3038 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3039 psp->sos.start_addr = ucode_start_addr;
3041 case PSP_FW_TYPE_PSP_SYS_DRV:
3042 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3043 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3044 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3045 psp->sys.start_addr = ucode_start_addr;
3047 case PSP_FW_TYPE_PSP_KDB:
3048 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3049 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3050 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3051 psp->kdb.start_addr = ucode_start_addr;
3053 case PSP_FW_TYPE_PSP_TOC:
3054 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3055 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3056 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3057 psp->toc.start_addr = ucode_start_addr;
3059 case PSP_FW_TYPE_PSP_SPL:
3060 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3061 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3062 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3063 psp->spl.start_addr = ucode_start_addr;
3065 case PSP_FW_TYPE_PSP_RL:
3066 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3067 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3068 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3069 psp->rl.start_addr = ucode_start_addr;
3071 case PSP_FW_TYPE_PSP_SOC_DRV:
3072 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3073 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3074 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3075 psp->soc_drv.start_addr = ucode_start_addr;
3077 case PSP_FW_TYPE_PSP_INTF_DRV:
3078 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3079 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3080 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3081 psp->intf_drv.start_addr = ucode_start_addr;
3083 case PSP_FW_TYPE_PSP_DBG_DRV:
3084 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3085 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3086 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3087 psp->dbg_drv.start_addr = ucode_start_addr;
3089 case PSP_FW_TYPE_PSP_RAS_DRV:
3090 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3091 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3092 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3093 psp->ras_drv.start_addr = ucode_start_addr;
3096 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3103 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3105 const struct psp_firmware_header_v1_0 *sos_hdr;
3106 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3107 uint8_t *ucode_array_start_addr;
3109 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3110 ucode_array_start_addr = (uint8_t *)sos_hdr +
3111 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3113 if (adev->gmc.xgmi.connected_to_cpu ||
3114 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3115 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3116 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3118 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3119 adev->psp.sys.start_addr = ucode_array_start_addr;
3121 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3122 adev->psp.sos.start_addr = ucode_array_start_addr +
3123 le32_to_cpu(sos_hdr->sos.offset_bytes);
3125 /* Load alternate PSP SOS FW */
3126 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3128 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3129 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3131 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3132 adev->psp.sys.start_addr = ucode_array_start_addr +
3133 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3135 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3136 adev->psp.sos.start_addr = ucode_array_start_addr +
3137 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3140 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3141 dev_warn(adev->dev, "PSP SOS FW not available");
3148 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3150 struct amdgpu_device *adev = psp->adev;
3151 char fw_name[PSP_FW_NAME_LEN];
3152 const struct psp_firmware_header_v1_0 *sos_hdr;
3153 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3154 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3155 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3156 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3158 uint8_t *ucode_array_start_addr;
3161 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3162 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3166 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3167 ucode_array_start_addr = (uint8_t *)sos_hdr +
3168 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3169 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3171 switch (sos_hdr->header.header_version_major) {
3173 err = psp_init_sos_base_fw(adev);
3177 if (sos_hdr->header.header_version_minor == 1) {
3178 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3179 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3180 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3181 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3182 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3183 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3184 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3186 if (sos_hdr->header.header_version_minor == 2) {
3187 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3188 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3189 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3190 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3192 if (sos_hdr->header.header_version_minor == 3) {
3193 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3194 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3195 adev->psp.toc.start_addr = ucode_array_start_addr +
3196 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3197 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3198 adev->psp.kdb.start_addr = ucode_array_start_addr +
3199 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3200 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3201 adev->psp.spl.start_addr = ucode_array_start_addr +
3202 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3203 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3204 adev->psp.rl.start_addr = ucode_array_start_addr +
3205 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3209 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3211 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3212 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3217 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3218 err = parse_sos_bin_descriptor(psp,
3219 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3227 "unsupported psp sos firmware\n");
3234 amdgpu_ucode_release(&adev->psp.sos_fw);
3239 static int parse_ta_bin_descriptor(struct psp_context *psp,
3240 const struct psp_fw_bin_desc *desc,
3241 const struct ta_firmware_header_v2_0 *ta_hdr)
3243 uint8_t *ucode_start_addr = NULL;
3245 if (!psp || !desc || !ta_hdr)
3248 ucode_start_addr = (uint8_t *)ta_hdr +
3249 le32_to_cpu(desc->offset_bytes) +
3250 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3252 switch (desc->fw_type) {
3253 case TA_FW_TYPE_PSP_ASD:
3254 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3255 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3256 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3257 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3259 case TA_FW_TYPE_PSP_XGMI:
3260 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3261 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3262 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3264 case TA_FW_TYPE_PSP_RAS:
3265 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3266 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3267 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3269 case TA_FW_TYPE_PSP_HDCP:
3270 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3271 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3272 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3274 case TA_FW_TYPE_PSP_DTM:
3275 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3276 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3277 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3279 case TA_FW_TYPE_PSP_RAP:
3280 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3281 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3282 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3284 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3285 psp->securedisplay_context.context.bin_desc.fw_version =
3286 le32_to_cpu(desc->fw_version);
3287 psp->securedisplay_context.context.bin_desc.size_bytes =
3288 le32_to_cpu(desc->size_bytes);
3289 psp->securedisplay_context.context.bin_desc.start_addr =
3293 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3300 static int parse_ta_v1_microcode(struct psp_context *psp)
3302 const struct ta_firmware_header_v1_0 *ta_hdr;
3303 struct amdgpu_device *adev = psp->adev;
3305 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3307 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3310 adev->psp.xgmi_context.context.bin_desc.fw_version =
3311 le32_to_cpu(ta_hdr->xgmi.fw_version);
3312 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3313 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3314 adev->psp.xgmi_context.context.bin_desc.start_addr =
3316 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3318 adev->psp.ras_context.context.bin_desc.fw_version =
3319 le32_to_cpu(ta_hdr->ras.fw_version);
3320 adev->psp.ras_context.context.bin_desc.size_bytes =
3321 le32_to_cpu(ta_hdr->ras.size_bytes);
3322 adev->psp.ras_context.context.bin_desc.start_addr =
3323 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3324 le32_to_cpu(ta_hdr->ras.offset_bytes);
3326 adev->psp.hdcp_context.context.bin_desc.fw_version =
3327 le32_to_cpu(ta_hdr->hdcp.fw_version);
3328 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3329 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3330 adev->psp.hdcp_context.context.bin_desc.start_addr =
3332 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3334 adev->psp.dtm_context.context.bin_desc.fw_version =
3335 le32_to_cpu(ta_hdr->dtm.fw_version);
3336 adev->psp.dtm_context.context.bin_desc.size_bytes =
3337 le32_to_cpu(ta_hdr->dtm.size_bytes);
3338 adev->psp.dtm_context.context.bin_desc.start_addr =
3339 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3340 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3342 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3343 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3344 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3345 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3346 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3347 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3348 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3350 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3355 static int parse_ta_v2_microcode(struct psp_context *psp)
3357 const struct ta_firmware_header_v2_0 *ta_hdr;
3358 struct amdgpu_device *adev = psp->adev;
3362 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3364 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3367 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3368 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3372 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3373 err = parse_ta_bin_descriptor(psp,
3374 &ta_hdr->ta_fw_bin[ta_index],
3383 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3385 const struct common_firmware_header *hdr;
3386 struct amdgpu_device *adev = psp->adev;
3387 char fw_name[PSP_FW_NAME_LEN];
3390 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3391 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3395 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3396 switch (le16_to_cpu(hdr->header_version_major)) {
3398 err = parse_ta_v1_microcode(psp);
3401 err = parse_ta_v2_microcode(psp);
3404 dev_err(adev->dev, "unsupported TA header version\n");
3409 amdgpu_ucode_release(&adev->psp.ta_fw);
3414 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3416 struct amdgpu_device *adev = psp->adev;
3417 char fw_name[PSP_FW_NAME_LEN];
3418 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3419 struct amdgpu_firmware_info *info = NULL;
3422 if (!amdgpu_sriov_vf(adev)) {
3423 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3427 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3428 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3430 if (err == -ENODEV) {
3431 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3435 dev_err(adev->dev, "fail to initialize cap microcode\n");
3438 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3439 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3440 info->fw = adev->psp.cap_fw;
3441 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3442 adev->psp.cap_fw->data;
3443 adev->firmware.fw_size += ALIGN(
3444 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3445 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3446 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3447 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3452 amdgpu_ucode_release(&adev->psp.cap_fw);
3456 static int psp_set_clockgating_state(void *handle,
3457 enum amd_clockgating_state state)
3462 static int psp_set_powergating_state(void *handle,
3463 enum amd_powergating_state state)
3468 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3469 struct device_attribute *attr,
3472 struct drm_device *ddev = dev_get_drvdata(dev);
3473 struct amdgpu_device *adev = drm_to_adev(ddev);
3477 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3478 DRM_INFO("PSP block is not ready yet.");
3482 mutex_lock(&adev->psp.mutex);
3483 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3484 mutex_unlock(&adev->psp.mutex);
3487 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3491 return sysfs_emit(buf, "%x\n", fw_ver);
3494 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3495 struct device_attribute *attr,
3499 struct drm_device *ddev = dev_get_drvdata(dev);
3500 struct amdgpu_device *adev = drm_to_adev(ddev);
3503 const struct firmware *usbc_pd_fw;
3504 struct amdgpu_bo *fw_buf_bo = NULL;
3505 uint64_t fw_pri_mc_addr;
3506 void *fw_pri_cpu_addr;
3508 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3509 DRM_INFO("PSP block is not ready yet.");
3513 if (!drm_dev_enter(ddev, &idx))
3516 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3517 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3521 /* LFB address which is aligned to 1MB boundary per PSP request */
3522 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3523 AMDGPU_GEM_DOMAIN_VRAM |
3524 AMDGPU_GEM_DOMAIN_GTT,
3525 &fw_buf_bo, &fw_pri_mc_addr,
3530 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3532 mutex_lock(&adev->psp.mutex);
3533 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3534 mutex_unlock(&adev->psp.mutex);
3536 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3539 release_firmware(usbc_pd_fw);
3542 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3550 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3554 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3557 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3558 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3563 static DEVICE_ATTR(usbc_pd_fw, 0644,
3564 psp_usbc_pd_fw_sysfs_read,
3565 psp_usbc_pd_fw_sysfs_write);
3567 int is_psp_fw_valid(struct psp_bin_desc bin)
3569 return bin.size_bytes;
3572 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3573 struct bin_attribute *bin_attr,
3574 char *buffer, loff_t pos, size_t count)
3576 struct device *dev = kobj_to_dev(kobj);
3577 struct drm_device *ddev = dev_get_drvdata(dev);
3578 struct amdgpu_device *adev = drm_to_adev(ddev);
3580 adev->psp.vbflash_done = false;
3582 /* Safeguard against memory drain */
3583 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3584 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3585 kvfree(adev->psp.vbflash_tmp_buf);
3586 adev->psp.vbflash_tmp_buf = NULL;
3587 adev->psp.vbflash_image_size = 0;
3591 /* TODO Just allocate max for now and optimize to realloc later if needed */
3592 if (!adev->psp.vbflash_tmp_buf) {
3593 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3594 if (!adev->psp.vbflash_tmp_buf)
3598 mutex_lock(&adev->psp.mutex);
3599 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3600 adev->psp.vbflash_image_size += count;
3601 mutex_unlock(&adev->psp.mutex);
3603 dev_info(adev->dev, "VBIOS flash write PSP done");
3608 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3609 struct bin_attribute *bin_attr, char *buffer,
3610 loff_t pos, size_t count)
3612 struct device *dev = kobj_to_dev(kobj);
3613 struct drm_device *ddev = dev_get_drvdata(dev);
3614 struct amdgpu_device *adev = drm_to_adev(ddev);
3615 struct amdgpu_bo *fw_buf_bo = NULL;
3616 uint64_t fw_pri_mc_addr;
3617 void *fw_pri_cpu_addr;
3620 if (adev->psp.vbflash_image_size == 0)
3623 dev_info(adev->dev, "VBIOS flash to PSP started");
3625 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3626 AMDGPU_GPU_PAGE_SIZE,
3627 AMDGPU_GEM_DOMAIN_VRAM,
3634 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3636 mutex_lock(&adev->psp.mutex);
3637 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3638 mutex_unlock(&adev->psp.mutex);
3640 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3643 kvfree(adev->psp.vbflash_tmp_buf);
3644 adev->psp.vbflash_tmp_buf = NULL;
3645 adev->psp.vbflash_image_size = 0;
3648 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3652 dev_info(adev->dev, "VBIOS flash to PSP done");
3656 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3657 struct device_attribute *attr,
3660 struct drm_device *ddev = dev_get_drvdata(dev);
3661 struct amdgpu_device *adev = drm_to_adev(ddev);
3662 uint32_t vbflash_status;
3664 vbflash_status = psp_vbflash_status(&adev->psp);
3665 if (!adev->psp.vbflash_done)
3667 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3670 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3673 static const struct bin_attribute psp_vbflash_bin_attr = {
3674 .attr = {.name = "psp_vbflash", .mode = 0660},
3676 .write = amdgpu_psp_vbflash_write,
3677 .read = amdgpu_psp_vbflash_read,
3680 static DEVICE_ATTR(psp_vbflash_status, 0440, amdgpu_psp_vbflash_status, NULL);
3682 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3685 struct psp_context *psp = &adev->psp;
3687 if (amdgpu_sriov_vf(adev))
3690 switch (adev->ip_versions[MP0_HWIP][0]) {
3691 case IP_VERSION(13, 0, 0):
3692 case IP_VERSION(13, 0, 7):
3693 case IP_VERSION(13, 0, 10):
3696 psp_v13_0_set_psp_funcs(psp);
3698 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3700 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3701 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3703 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3710 const struct amd_ip_funcs psp_ip_funcs = {
3712 .early_init = psp_early_init,
3714 .sw_init = psp_sw_init,
3715 .sw_fini = psp_sw_fini,
3716 .hw_init = psp_hw_init,
3717 .hw_fini = psp_hw_fini,
3718 .suspend = psp_suspend,
3719 .resume = psp_resume,
3721 .check_soft_reset = NULL,
3722 .wait_for_idle = NULL,
3724 .set_clockgating_state = psp_set_clockgating_state,
3725 .set_powergating_state = psp_set_powergating_state,
3728 static int psp_sysfs_init(struct amdgpu_device *adev)
3730 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3733 DRM_ERROR("Failed to create USBC PD FW control file!");
3738 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3740 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3741 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3744 static void psp_sysfs_fini(struct amdgpu_device *adev)
3746 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3749 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3750 .type = AMD_IP_BLOCK_TYPE_PSP,
3754 .funcs = &psp_ip_funcs,
3757 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3758 .type = AMD_IP_BLOCK_TYPE_PSP,
3762 .funcs = &psp_ip_funcs,
3765 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3766 .type = AMD_IP_BLOCK_TYPE_PSP,
3770 .funcs = &psp_ip_funcs,
3773 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3774 .type = AMD_IP_BLOCK_TYPE_PSP,
3778 .funcs = &psp_ip_funcs,
3781 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
3782 .type = AMD_IP_BLOCK_TYPE_PSP,
3786 .funcs = &psp_ip_funcs,
3789 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3790 .type = AMD_IP_BLOCK_TYPE_PSP,
3794 .funcs = &psp_ip_funcs,
3797 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3798 .type = AMD_IP_BLOCK_TYPE_PSP,
3802 .funcs = &psp_ip_funcs,