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);
56 * Due to DF Cstate management centralized to PMFW, the firmware
57 * loading sequence will be updated as below:
63 * - Load other non-psp fw
65 * - Load XGMI/RAS/HDCP/DTM TA if any
67 * This new sequence is required for
68 * - Arcturus and onwards
70 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
72 struct amdgpu_device *adev = psp->adev;
74 if (amdgpu_sriov_vf(adev)) {
75 psp->pmfw_centralized_cstate_management = false;
79 switch (adev->ip_versions[MP0_HWIP][0]) {
80 case IP_VERSION(11, 0, 0):
81 case IP_VERSION(11, 0, 4):
82 case IP_VERSION(11, 0, 5):
83 case IP_VERSION(11, 0, 7):
84 case IP_VERSION(11, 0, 9):
85 case IP_VERSION(11, 0, 11):
86 case IP_VERSION(11, 0, 12):
87 case IP_VERSION(11, 0, 13):
88 case IP_VERSION(13, 0, 0):
89 case IP_VERSION(13, 0, 2):
90 case IP_VERSION(13, 0, 7):
91 psp->pmfw_centralized_cstate_management = true;
94 psp->pmfw_centralized_cstate_management = false;
99 static int psp_early_init(void *handle)
101 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
102 struct psp_context *psp = &adev->psp;
104 switch (adev->ip_versions[MP0_HWIP][0]) {
105 case IP_VERSION(9, 0, 0):
106 psp_v3_1_set_psp_funcs(psp);
107 psp->autoload_supported = false;
109 case IP_VERSION(10, 0, 0):
110 case IP_VERSION(10, 0, 1):
111 psp_v10_0_set_psp_funcs(psp);
112 psp->autoload_supported = false;
114 case IP_VERSION(11, 0, 2):
115 case IP_VERSION(11, 0, 4):
116 psp_v11_0_set_psp_funcs(psp);
117 psp->autoload_supported = false;
119 case IP_VERSION(11, 0, 0):
120 case IP_VERSION(11, 0, 5):
121 case IP_VERSION(11, 0, 9):
122 case IP_VERSION(11, 0, 7):
123 case IP_VERSION(11, 0, 11):
124 case IP_VERSION(11, 5, 0):
125 case IP_VERSION(11, 0, 12):
126 case IP_VERSION(11, 0, 13):
127 psp_v11_0_set_psp_funcs(psp);
128 psp->autoload_supported = true;
130 case IP_VERSION(11, 0, 3):
131 case IP_VERSION(12, 0, 1):
132 psp_v12_0_set_psp_funcs(psp);
134 case IP_VERSION(13, 0, 2):
135 psp_v13_0_set_psp_funcs(psp);
137 case IP_VERSION(13, 0, 1):
138 case IP_VERSION(13, 0, 3):
139 case IP_VERSION(13, 0, 5):
140 case IP_VERSION(13, 0, 8):
141 psp_v13_0_set_psp_funcs(psp);
142 psp->autoload_supported = true;
144 case IP_VERSION(11, 0, 8):
145 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
146 psp_v11_0_8_set_psp_funcs(psp);
147 psp->autoload_supported = false;
150 case IP_VERSION(13, 0, 0):
151 case IP_VERSION(13, 0, 7):
152 psp_v13_0_set_psp_funcs(psp);
153 psp->autoload_supported = true;
155 case IP_VERSION(13, 0, 4):
156 psp_v13_0_4_set_psp_funcs(psp);
157 psp->autoload_supported = true;
165 psp_check_pmfw_centralized_cstate_management(psp);
170 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
172 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
173 &mem_ctx->shared_buf);
176 static void psp_free_shared_bufs(struct psp_context *psp)
181 /* free TMR memory buffer */
182 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
183 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
185 /* free xgmi shared memory */
186 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
188 /* free ras shared memory */
189 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
191 /* free hdcp shared memory */
192 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
194 /* free dtm shared memory */
195 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
197 /* free rap shared memory */
198 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
200 /* free securedisplay shared memory */
201 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
206 static void psp_memory_training_fini(struct psp_context *psp)
208 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
210 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
211 kfree(ctx->sys_cache);
212 ctx->sys_cache = NULL;
215 static int psp_memory_training_init(struct psp_context *psp)
218 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
220 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
221 DRM_DEBUG("memory training is not supported!\n");
225 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
226 if (ctx->sys_cache == NULL) {
227 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
232 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
233 ctx->train_data_size,
234 ctx->p2c_train_data_offset,
235 ctx->c2p_train_data_offset);
236 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
240 psp_memory_training_fini(psp);
245 * Helper funciton to query psp runtime database entry
247 * @adev: amdgpu_device pointer
248 * @entry_type: the type of psp runtime database entry
249 * @db_entry: runtime database entry pointer
251 * Return false if runtime database doesn't exit or entry is invalid
252 * or true if the specific database entry is found, and copy to @db_entry
254 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
255 enum psp_runtime_entry_type entry_type,
258 uint64_t db_header_pos, db_dir_pos;
259 struct psp_runtime_data_header db_header = {0};
260 struct psp_runtime_data_directory db_dir = {0};
264 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
265 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
267 /* read runtime db header from vram */
268 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
269 sizeof(struct psp_runtime_data_header), false);
271 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
272 /* runtime db doesn't exist, exit */
273 dev_warn(adev->dev, "PSP runtime database doesn't exist\n");
277 /* read runtime database entry from vram */
278 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
279 sizeof(struct psp_runtime_data_directory), false);
281 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
282 /* invalid db entry count, exit */
283 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
287 /* look up for requested entry type */
288 for (i = 0; i < db_dir.entry_count && !ret; i++) {
289 if (db_dir.entry_list[i].entry_type == entry_type) {
290 switch (entry_type) {
291 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
292 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
293 /* invalid db entry size */
294 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
297 /* read runtime database entry */
298 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
299 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
302 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
303 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
304 /* invalid db entry size */
305 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
308 /* read runtime database entry */
309 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
310 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
323 static int psp_init_sriov_microcode(struct psp_context *psp)
325 struct amdgpu_device *adev = psp->adev;
328 switch (adev->ip_versions[MP0_HWIP][0]) {
329 case IP_VERSION(9, 0, 0):
330 ret = psp_init_cap_microcode(psp, "vega10");
332 case IP_VERSION(11, 0, 9):
333 ret = psp_init_cap_microcode(psp, "navi12");
335 case IP_VERSION(11, 0, 7):
336 ret = psp_init_cap_microcode(psp, "sienna_cichlid");
338 case IP_VERSION(13, 0, 2):
339 ret = psp_init_cap_microcode(psp, "aldebaran");
340 ret &= psp_init_ta_microcode(psp, "aldebaran");
350 static int psp_sw_init(void *handle)
352 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
353 struct psp_context *psp = &adev->psp;
355 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
356 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
357 struct psp_runtime_scpm_entry scpm_entry;
359 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
361 DRM_ERROR("Failed to allocate memory to command buffer!\n");
365 if (amdgpu_sriov_vf(adev))
366 ret = psp_init_sriov_microcode(psp);
368 ret = psp_init_microcode(psp);
370 DRM_ERROR("Failed to load psp firmware!\n");
374 adev->psp.xgmi_context.supports_extended_data =
375 !adev->gmc.xgmi.connected_to_cpu &&
376 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
378 memset(&scpm_entry, 0, sizeof(scpm_entry));
379 if ((psp_get_runtime_db_entry(adev,
380 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
382 (SCPM_DISABLE != scpm_entry.scpm_status)) {
383 adev->scpm_enabled = true;
384 adev->scpm_status = scpm_entry.scpm_status;
386 adev->scpm_enabled = false;
387 adev->scpm_status = SCPM_DISABLE;
390 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
392 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
393 if (psp_get_runtime_db_entry(adev,
394 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
396 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
397 if ((psp->boot_cfg_bitmask) &
398 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
399 /* If psp runtime database exists, then
400 * only enable two stage memory training
401 * when TWO_STAGE_DRAM_TRAINING bit is set
402 * in runtime database */
403 mem_training_ctx->enable_mem_training = true;
407 /* If psp runtime database doesn't exist or
408 * is invalid, force enable two stage memory
410 mem_training_ctx->enable_mem_training = true;
413 if (mem_training_ctx->enable_mem_training) {
414 ret = psp_memory_training_init(psp);
416 DRM_ERROR("Failed to initialize memory training!\n");
420 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
422 DRM_ERROR("Failed to process memory training!\n");
427 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
428 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
429 ret= psp_sysfs_init(adev);
435 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
436 amdgpu_sriov_vf(adev) ?
437 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
439 &psp->fw_pri_mc_addr,
444 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
445 AMDGPU_GEM_DOMAIN_VRAM,
447 &psp->fence_buf_mc_addr,
452 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
453 AMDGPU_GEM_DOMAIN_VRAM,
454 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
455 (void **)&psp->cmd_buf_mem);
462 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
463 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
465 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
466 &psp->fence_buf_mc_addr, &psp->fence_buf);
470 static int psp_sw_fini(void *handle)
472 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
473 struct psp_context *psp = &adev->psp;
474 struct psp_gfx_cmd_resp *cmd = psp->cmd;
476 psp_memory_training_fini(psp);
478 release_firmware(psp->sos_fw);
482 release_firmware(psp->asd_fw);
486 release_firmware(psp->ta_fw);
490 release_firmware(psp->cap_fw);
494 release_firmware(psp->toc_fw);
497 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
498 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
499 psp_sysfs_fini(adev);
504 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
505 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
506 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
507 &psp->fence_buf_mc_addr, &psp->fence_buf);
508 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
509 (void **)&psp->cmd_buf_mem);
514 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
515 uint32_t reg_val, uint32_t mask, bool check_changed)
519 struct amdgpu_device *adev = psp->adev;
521 if (psp->adev->no_hw_access)
524 for (i = 0; i < adev->usec_timeout; i++) {
525 val = RREG32(reg_index);
530 if ((val & mask) == reg_val)
539 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
542 case GFX_CMD_ID_LOAD_TA:
544 case GFX_CMD_ID_UNLOAD_TA:
546 case GFX_CMD_ID_INVOKE_CMD:
548 case GFX_CMD_ID_LOAD_ASD:
550 case GFX_CMD_ID_SETUP_TMR:
552 case GFX_CMD_ID_LOAD_IP_FW:
554 case GFX_CMD_ID_DESTROY_TMR:
555 return "DESTROY_TMR";
556 case GFX_CMD_ID_SAVE_RESTORE:
557 return "SAVE_RESTORE_IP_FW";
558 case GFX_CMD_ID_SETUP_VMR:
560 case GFX_CMD_ID_DESTROY_VMR:
561 return "DESTROY_VMR";
562 case GFX_CMD_ID_PROG_REG:
564 case GFX_CMD_ID_GET_FW_ATTESTATION:
565 return "GET_FW_ATTESTATION";
566 case GFX_CMD_ID_LOAD_TOC:
567 return "ID_LOAD_TOC";
568 case GFX_CMD_ID_AUTOLOAD_RLC:
569 return "AUTOLOAD_RLC";
570 case GFX_CMD_ID_BOOT_CFG:
573 return "UNKNOWN CMD";
578 psp_cmd_submit_buf(struct psp_context *psp,
579 struct amdgpu_firmware_info *ucode,
580 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
585 bool ras_intr = false;
586 bool skip_unsupport = false;
588 if (psp->adev->no_hw_access)
591 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
594 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
596 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
598 index = atomic_inc_return(&psp->fence_value);
599 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
601 atomic_dec(&psp->fence_value);
605 amdgpu_device_invalidate_hdp(psp->adev, NULL);
606 while (*((unsigned int *)psp->fence_buf) != index) {
610 * Shouldn't wait for timeout when err_event_athub occurs,
611 * because gpu reset thread triggered and lock resource should
612 * be released for psp resume sequence.
614 ras_intr = amdgpu_ras_intr_triggered();
617 usleep_range(10, 100);
618 amdgpu_device_invalidate_hdp(psp->adev, NULL);
621 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
622 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
623 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
625 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
627 /* In some cases, psp response status is not 0 even there is no
628 * problem while the command is submitted. Some version of PSP FW
629 * doesn't write 0 to that field.
630 * So here we would like to only print a warning instead of an error
631 * during psp initialization to avoid breaking hw_init and it doesn't
634 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
636 DRM_WARN("failed to load ucode %s(0x%X) ",
637 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
638 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
639 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
640 psp->cmd_buf_mem->resp.status);
641 /* If any firmware (including CAP) load fails under SRIOV, it should
642 * return failure to stop the VF from initializing.
643 * Also return failure in case of timeout
645 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
652 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
653 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
661 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
663 struct psp_gfx_cmd_resp *cmd = psp->cmd;
665 mutex_lock(&psp->mutex);
667 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
672 static void release_psp_cmd_buf(struct psp_context *psp)
674 mutex_unlock(&psp->mutex);
677 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
678 struct psp_gfx_cmd_resp *cmd,
679 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
681 struct amdgpu_device *adev = psp->adev;
682 uint32_t size = amdgpu_bo_size(tmr_bo);
683 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
685 if (amdgpu_sriov_vf(psp->adev))
686 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
688 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
689 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
690 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
691 cmd->cmd.cmd_setup_tmr.buf_size = size;
692 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
693 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
694 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
697 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
698 uint64_t pri_buf_mc, uint32_t size)
700 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
701 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
702 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
703 cmd->cmd.cmd_load_toc.toc_size = size;
706 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
707 static int psp_load_toc(struct psp_context *psp,
711 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
713 /* Copy toc to psp firmware private buffer */
714 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
716 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
718 ret = psp_cmd_submit_buf(psp, NULL, cmd,
719 psp->fence_buf_mc_addr);
721 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
723 release_psp_cmd_buf(psp);
728 /* Set up Trusted Memory Region */
729 static int psp_tmr_init(struct psp_context *psp)
737 * According to HW engineer, they prefer the TMR address be "naturally
738 * aligned" , e.g. the start address be an integer divide of TMR size.
740 * Note: this memory need be reserved till the driver
743 tmr_size = PSP_TMR_SIZE(psp->adev);
745 /* For ASICs support RLC autoload, psp will parse the toc
746 * and calculate the total size of TMR needed */
747 if (!amdgpu_sriov_vf(psp->adev) &&
748 psp->toc.start_addr &&
749 psp->toc.size_bytes &&
751 ret = psp_load_toc(psp, &tmr_size);
753 DRM_ERROR("Failed to load toc\n");
758 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
759 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_ALIGNMENT,
760 AMDGPU_GEM_DOMAIN_VRAM,
761 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
766 static bool psp_skip_tmr(struct psp_context *psp)
768 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
769 case IP_VERSION(11, 0, 9):
770 case IP_VERSION(11, 0, 7):
771 case IP_VERSION(13, 0, 2):
778 static int psp_tmr_load(struct psp_context *psp)
781 struct psp_gfx_cmd_resp *cmd;
783 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
784 * Already set up by host driver.
786 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
789 cmd = acquire_psp_cmd_buf(psp);
791 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
792 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
793 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
795 ret = psp_cmd_submit_buf(psp, NULL, cmd,
796 psp->fence_buf_mc_addr);
798 release_psp_cmd_buf(psp);
803 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
804 struct psp_gfx_cmd_resp *cmd)
806 if (amdgpu_sriov_vf(psp->adev))
807 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
809 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
812 static int psp_tmr_unload(struct psp_context *psp)
815 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
817 psp_prep_tmr_unload_cmd_buf(psp, cmd);
818 DRM_INFO("free PSP TMR buffer\n");
820 ret = psp_cmd_submit_buf(psp, NULL, cmd,
821 psp->fence_buf_mc_addr);
823 release_psp_cmd_buf(psp);
828 static int psp_tmr_terminate(struct psp_context *psp)
830 return psp_tmr_unload(psp);
833 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
834 uint64_t *output_ptr)
837 struct psp_gfx_cmd_resp *cmd;
842 if (amdgpu_sriov_vf(psp->adev))
845 cmd = acquire_psp_cmd_buf(psp);
847 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
849 ret = psp_cmd_submit_buf(psp, NULL, cmd,
850 psp->fence_buf_mc_addr);
853 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
854 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
857 release_psp_cmd_buf(psp);
862 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
864 struct psp_context *psp = &adev->psp;
865 struct psp_gfx_cmd_resp *cmd;
868 if (amdgpu_sriov_vf(adev))
871 cmd = acquire_psp_cmd_buf(psp);
873 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
874 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
876 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
879 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
882 release_psp_cmd_buf(psp);
887 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
890 struct psp_context *psp = &adev->psp;
891 struct psp_gfx_cmd_resp *cmd;
893 if (amdgpu_sriov_vf(adev))
896 cmd = acquire_psp_cmd_buf(psp);
898 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
899 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
900 cmd->cmd.boot_cfg.boot_config = boot_cfg;
901 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
903 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
905 release_psp_cmd_buf(psp);
910 static int psp_rl_load(struct amdgpu_device *adev)
913 struct psp_context *psp = &adev->psp;
914 struct psp_gfx_cmd_resp *cmd;
916 if (!is_psp_fw_valid(psp->rl))
919 cmd = acquire_psp_cmd_buf(psp);
921 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
922 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
924 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
925 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
926 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
927 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
928 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
930 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
932 release_psp_cmd_buf(psp);
937 static int psp_asd_initialize(struct psp_context *psp)
941 /* If PSP version doesn't match ASD version, asd loading will be failed.
942 * add workaround to bypass it for sriov now.
943 * TODO: add version check to make it common
945 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
948 psp->asd_context.mem_context.shared_mc_addr = 0;
949 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
950 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
952 ret = psp_ta_load(psp, &psp->asd_context);
954 psp->asd_context.initialized = true;
959 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
962 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
963 cmd->cmd.cmd_unload_ta.session_id = session_id;
966 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
969 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
971 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
973 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
975 release_psp_cmd_buf(psp);
980 static int psp_asd_terminate(struct psp_context *psp)
984 if (amdgpu_sriov_vf(psp->adev))
987 if (!psp->asd_context.initialized)
990 ret = psp_ta_unload(psp, &psp->asd_context);
992 psp->asd_context.initialized = false;
997 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
998 uint32_t id, uint32_t value)
1000 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1001 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1002 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1005 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1008 struct psp_gfx_cmd_resp *cmd;
1011 if (reg >= PSP_REG_LAST)
1014 cmd = acquire_psp_cmd_buf(psp);
1016 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1017 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1019 DRM_ERROR("PSP failed to program reg id %d", reg);
1021 release_psp_cmd_buf(psp);
1026 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1028 struct ta_context *context)
1030 cmd->cmd_id = context->ta_load_type;
1031 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1032 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1033 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1035 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1036 lower_32_bits(context->mem_context.shared_mc_addr);
1037 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1038 upper_32_bits(context->mem_context.shared_mc_addr);
1039 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1042 int psp_ta_init_shared_buf(struct psp_context *psp,
1043 struct ta_mem_context *mem_ctx)
1046 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1047 * physical) for ta to host memory
1049 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1050 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
1051 &mem_ctx->shared_bo,
1052 &mem_ctx->shared_mc_addr,
1053 &mem_ctx->shared_buf);
1056 static void psp_prep_ta_invoke_indirect_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1058 struct ta_context *context)
1060 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1061 cmd->cmd.cmd_invoke_cmd.session_id = context->session_id;
1062 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1064 cmd->cmd.cmd_invoke_cmd.buf.num_desc = 1;
1065 cmd->cmd.cmd_invoke_cmd.buf.total_size = context->mem_context.shared_mem_size;
1066 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_size = context->mem_context.shared_mem_size;
1067 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_phy_addr_lo =
1068 lower_32_bits(context->mem_context.shared_mc_addr);
1069 cmd->cmd.cmd_invoke_cmd.buf.buf_desc[0].buf_phy_addr_hi =
1070 upper_32_bits(context->mem_context.shared_mc_addr);
1073 int psp_ta_invoke_indirect(struct psp_context *psp,
1075 struct ta_context *context)
1078 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1080 psp_prep_ta_invoke_indirect_cmd_buf(cmd, ta_cmd_id, context);
1082 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1083 psp->fence_buf_mc_addr);
1085 context->resp_status = cmd->resp.status;
1087 release_psp_cmd_buf(psp);
1092 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1094 uint32_t session_id)
1096 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1097 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1098 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1101 int psp_ta_invoke(struct psp_context *psp,
1103 struct ta_context *context)
1106 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1108 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1110 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1111 psp->fence_buf_mc_addr);
1113 context->resp_status = cmd->resp.status;
1115 release_psp_cmd_buf(psp);
1120 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1123 struct psp_gfx_cmd_resp *cmd;
1125 cmd = acquire_psp_cmd_buf(psp);
1127 psp_copy_fw(psp, context->bin_desc.start_addr,
1128 context->bin_desc.size_bytes);
1130 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1132 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1133 psp->fence_buf_mc_addr);
1135 context->resp_status = cmd->resp.status;
1138 context->session_id = cmd->resp.session_id;
1141 release_psp_cmd_buf(psp);
1146 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1148 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1151 int psp_xgmi_terminate(struct psp_context *psp)
1154 struct amdgpu_device *adev = psp->adev;
1156 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1157 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1158 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1159 adev->gmc.xgmi.connected_to_cpu))
1162 if (!psp->xgmi_context.context.initialized)
1165 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1167 psp->xgmi_context.context.initialized = false;
1172 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1174 struct ta_xgmi_shared_memory *xgmi_cmd;
1178 !psp->xgmi_context.context.bin_desc.size_bytes ||
1179 !psp->xgmi_context.context.bin_desc.start_addr)
1185 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1186 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1188 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1189 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1195 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1197 psp->xgmi_context.context.initialized = true;
1202 /* Initialize XGMI session */
1203 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1204 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1205 xgmi_cmd->flag_extend_link_record = set_extended_data;
1206 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1208 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1213 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1215 struct ta_xgmi_shared_memory *xgmi_cmd;
1218 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1219 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1221 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1223 /* Invoke xgmi ta to get hive id */
1224 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1228 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1233 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1235 struct ta_xgmi_shared_memory *xgmi_cmd;
1238 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1239 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1241 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1243 /* Invoke xgmi ta to get the node id */
1244 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1248 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1253 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1255 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1256 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1260 * Chips that support extended topology information require the driver to
1261 * reflect topology information in the opposite direction. This is
1262 * because the TA has already exceeded its link record limit and if the
1263 * TA holds bi-directional information, the driver would have to do
1264 * multiple fetches instead of just two.
1266 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1267 struct psp_xgmi_node_info node_info)
1269 struct amdgpu_device *mirror_adev;
1270 struct amdgpu_hive_info *hive;
1271 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1272 uint64_t dst_node_id = node_info.node_id;
1273 uint8_t dst_num_hops = node_info.num_hops;
1274 uint8_t dst_num_links = node_info.num_links;
1276 hive = amdgpu_get_xgmi_hive(psp->adev);
1277 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1278 struct psp_xgmi_topology_info *mirror_top_info;
1281 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1284 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1285 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1286 if (mirror_top_info->nodes[j].node_id != src_node_id)
1289 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1291 * prevent 0 num_links value re-reflection since reflection
1292 * criteria is based on num_hops (direct or indirect).
1296 mirror_top_info->nodes[j].num_links = dst_num_links;
1304 amdgpu_put_xgmi_hive(hive);
1307 int psp_xgmi_get_topology_info(struct psp_context *psp,
1309 struct psp_xgmi_topology_info *topology,
1310 bool get_extended_data)
1312 struct ta_xgmi_shared_memory *xgmi_cmd;
1313 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1314 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1318 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1321 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1322 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1323 xgmi_cmd->flag_extend_link_record = get_extended_data;
1325 /* Fill in the shared memory with topology information as input */
1326 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1327 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1328 topology_info_input->num_nodes = number_devices;
1330 for (i = 0; i < topology_info_input->num_nodes; i++) {
1331 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1332 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1333 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1334 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1337 /* Invoke xgmi ta to get the topology information */
1338 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1342 /* Read the output topology information from the shared memory */
1343 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1344 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1345 for (i = 0; i < topology->num_nodes; i++) {
1346 /* extended data will either be 0 or equal to non-extended data */
1347 if (topology_info_output->nodes[i].num_hops)
1348 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1350 /* non-extended data gets everything here so no need to update */
1351 if (!get_extended_data) {
1352 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1353 topology->nodes[i].is_sharing_enabled =
1354 topology_info_output->nodes[i].is_sharing_enabled;
1355 topology->nodes[i].sdma_engine =
1356 topology_info_output->nodes[i].sdma_engine;
1361 /* Invoke xgmi ta again to get the link information */
1362 if (psp_xgmi_peer_link_info_supported(psp)) {
1363 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1365 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1367 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1372 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1373 for (i = 0; i < topology->num_nodes; i++) {
1374 /* accumulate num_links on extended data */
1375 topology->nodes[i].num_links = get_extended_data ?
1376 topology->nodes[i].num_links +
1377 link_info_output->nodes[i].num_links :
1378 link_info_output->nodes[i].num_links;
1380 /* reflect the topology information for bi-directionality */
1381 if (psp->xgmi_context.supports_extended_data &&
1382 get_extended_data && topology->nodes[i].num_hops)
1383 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1390 int psp_xgmi_set_topology_info(struct psp_context *psp,
1392 struct psp_xgmi_topology_info *topology)
1394 struct ta_xgmi_shared_memory *xgmi_cmd;
1395 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1398 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1401 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1402 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1404 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1405 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1406 topology_info_input->num_nodes = number_devices;
1408 for (i = 0; i < topology_info_input->num_nodes; i++) {
1409 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1410 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1411 topology_info_input->nodes[i].is_sharing_enabled = 1;
1412 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1415 /* Invoke xgmi ta to set topology information */
1416 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1420 static void psp_ras_ta_check_status(struct psp_context *psp)
1422 struct ta_ras_shared_memory *ras_cmd =
1423 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1425 switch (ras_cmd->ras_status) {
1426 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1427 dev_warn(psp->adev->dev,
1428 "RAS WARNING: cmd failed due to unsupported ip\n");
1430 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1431 dev_warn(psp->adev->dev,
1432 "RAS WARNING: cmd failed due to unsupported error injection\n");
1434 case TA_RAS_STATUS__SUCCESS:
1436 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1437 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1438 dev_warn(psp->adev->dev,
1439 "RAS WARNING: Inject error to critical region is not allowed\n");
1442 dev_warn(psp->adev->dev,
1443 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1448 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1450 struct ta_ras_shared_memory *ras_cmd;
1453 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1456 * TODO: bypass the loading in sriov for now
1458 if (amdgpu_sriov_vf(psp->adev))
1461 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1463 if (amdgpu_ras_intr_triggered())
1466 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1468 DRM_WARN("RAS: Unsupported Interface");
1473 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1474 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1476 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1478 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1479 dev_warn(psp->adev->dev,
1480 "RAS internal register access blocked\n");
1482 psp_ras_ta_check_status(psp);
1488 int psp_ras_enable_features(struct psp_context *psp,
1489 union ta_ras_cmd_input *info, bool enable)
1491 struct ta_ras_shared_memory *ras_cmd;
1494 if (!psp->ras_context.context.initialized)
1497 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1498 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1501 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1503 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1505 ras_cmd->ras_in_message = *info;
1507 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1514 int psp_ras_terminate(struct psp_context *psp)
1519 * TODO: bypass the terminate in sriov for now
1521 if (amdgpu_sriov_vf(psp->adev))
1524 if (!psp->ras_context.context.initialized)
1527 ret = psp_ta_unload(psp, &psp->ras_context.context);
1529 psp->ras_context.context.initialized = false;
1534 static int psp_ras_initialize(struct psp_context *psp)
1537 uint32_t boot_cfg = 0xFF;
1538 struct amdgpu_device *adev = psp->adev;
1539 struct ta_ras_shared_memory *ras_cmd;
1542 * TODO: bypass the initialize in sriov for now
1544 if (amdgpu_sriov_vf(adev))
1547 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1548 !adev->psp.ras_context.context.bin_desc.start_addr) {
1549 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1553 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1554 /* query GECC enablement status from boot config
1555 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1557 ret = psp_boot_config_get(adev, &boot_cfg);
1559 dev_warn(adev->dev, "PSP get boot config failed\n");
1561 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1563 dev_info(adev->dev, "GECC is disabled\n");
1565 /* disable GECC in next boot cycle if ras is
1566 * disabled by module parameter amdgpu_ras_enable
1567 * and/or amdgpu_ras_mask, or boot_config_get call
1570 ret = psp_boot_config_set(adev, 0);
1572 dev_warn(adev->dev, "PSP set boot config failed\n");
1574 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1575 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1578 if (1 == boot_cfg) {
1579 dev_info(adev->dev, "GECC is enabled\n");
1581 /* enable GECC in next boot cycle if it is disabled
1582 * in boot config, or force enable GECC if failed to
1583 * get boot configuration
1585 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1587 dev_warn(adev->dev, "PSP set boot config failed\n");
1589 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1594 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1595 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1597 if (!psp->ras_context.context.initialized) {
1598 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1603 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1604 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1606 if (amdgpu_ras_is_poison_mode_supported(adev))
1607 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1608 if (!adev->gmc.xgmi.connected_to_cpu)
1609 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1611 ret = psp_ta_load(psp, &psp->ras_context.context);
1613 if (!ret && !ras_cmd->ras_status)
1614 psp->ras_context.context.initialized = true;
1616 if (ras_cmd->ras_status)
1617 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1618 amdgpu_ras_fini(psp->adev);
1624 int psp_ras_trigger_error(struct psp_context *psp,
1625 struct ta_ras_trigger_error_input *info)
1627 struct ta_ras_shared_memory *ras_cmd;
1630 if (!psp->ras_context.context.initialized)
1633 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1634 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1636 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1637 ras_cmd->ras_in_message.trigger_error = *info;
1639 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1643 /* If err_event_athub occurs error inject was successful, however
1644 return status from TA is no long reliable */
1645 if (amdgpu_ras_intr_triggered())
1648 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1650 else if (ras_cmd->ras_status)
1658 static int psp_hdcp_initialize(struct psp_context *psp)
1663 * TODO: bypass the initialize in sriov for now
1665 if (amdgpu_sriov_vf(psp->adev))
1668 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1669 !psp->hdcp_context.context.bin_desc.start_addr) {
1670 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1674 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1675 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1677 if (!psp->hdcp_context.context.initialized) {
1678 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1683 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1685 psp->hdcp_context.context.initialized = true;
1686 mutex_init(&psp->hdcp_context.mutex);
1692 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1695 * TODO: bypass the loading in sriov for now
1697 if (amdgpu_sriov_vf(psp->adev))
1700 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1703 static int psp_hdcp_terminate(struct psp_context *psp)
1708 * TODO: bypass the terminate in sriov for now
1710 if (amdgpu_sriov_vf(psp->adev))
1713 if (!psp->hdcp_context.context.initialized)
1716 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1718 psp->hdcp_context.context.initialized = false;
1725 static int psp_dtm_initialize(struct psp_context *psp)
1730 * TODO: bypass the initialize in sriov for now
1732 if (amdgpu_sriov_vf(psp->adev))
1735 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1736 !psp->dtm_context.context.bin_desc.start_addr) {
1737 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1741 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1742 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1744 if (!psp->dtm_context.context.initialized) {
1745 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1750 ret = psp_ta_load(psp, &psp->dtm_context.context);
1752 psp->dtm_context.context.initialized = true;
1753 mutex_init(&psp->dtm_context.mutex);
1759 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1762 * TODO: bypass the loading in sriov for now
1764 if (amdgpu_sriov_vf(psp->adev))
1767 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1770 static int psp_dtm_terminate(struct psp_context *psp)
1775 * TODO: bypass the terminate in sriov for now
1777 if (amdgpu_sriov_vf(psp->adev))
1780 if (!psp->dtm_context.context.initialized)
1783 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1785 psp->dtm_context.context.initialized = false;
1792 static int psp_rap_initialize(struct psp_context *psp)
1795 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1798 * TODO: bypass the initialize in sriov for now
1800 if (amdgpu_sriov_vf(psp->adev))
1803 if (!psp->rap_context.context.bin_desc.size_bytes ||
1804 !psp->rap_context.context.bin_desc.start_addr) {
1805 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1809 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1810 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1812 if (!psp->rap_context.context.initialized) {
1813 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1818 ret = psp_ta_load(psp, &psp->rap_context.context);
1820 psp->rap_context.context.initialized = true;
1821 mutex_init(&psp->rap_context.mutex);
1825 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1826 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1827 psp_rap_terminate(psp);
1828 /* free rap shared memory */
1829 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1831 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1840 static int psp_rap_terminate(struct psp_context *psp)
1844 if (!psp->rap_context.context.initialized)
1847 ret = psp_ta_unload(psp, &psp->rap_context.context);
1849 psp->rap_context.context.initialized = false;
1854 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1856 struct ta_rap_shared_memory *rap_cmd;
1859 if (!psp->rap_context.context.initialized)
1862 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1863 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1866 mutex_lock(&psp->rap_context.mutex);
1868 rap_cmd = (struct ta_rap_shared_memory *)
1869 psp->rap_context.context.mem_context.shared_buf;
1870 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1872 rap_cmd->cmd_id = ta_cmd_id;
1873 rap_cmd->validation_method_id = METHOD_A;
1875 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1880 *status = rap_cmd->rap_status;
1883 mutex_unlock(&psp->rap_context.mutex);
1889 /* securedisplay start */
1890 static int psp_securedisplay_initialize(struct psp_context *psp)
1893 struct securedisplay_cmd *securedisplay_cmd;
1896 * TODO: bypass the initialize in sriov for now
1898 if (amdgpu_sriov_vf(psp->adev))
1901 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1902 !psp->securedisplay_context.context.bin_desc.start_addr) {
1903 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1907 psp->securedisplay_context.context.mem_context.shared_mem_size =
1908 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1909 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1911 if (!psp->securedisplay_context.context.initialized) {
1912 ret = psp_ta_init_shared_buf(psp,
1913 &psp->securedisplay_context.context.mem_context);
1918 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1920 psp->securedisplay_context.context.initialized = true;
1921 mutex_init(&psp->securedisplay_context.mutex);
1925 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1926 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1928 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1930 psp_securedisplay_terminate(psp);
1931 /* free securedisplay shared memory */
1932 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1933 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1937 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1938 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1939 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1940 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1946 static int psp_securedisplay_terminate(struct psp_context *psp)
1951 * TODO:bypass the terminate in sriov for now
1953 if (amdgpu_sriov_vf(psp->adev))
1956 if (!psp->securedisplay_context.context.initialized)
1959 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1961 psp->securedisplay_context.context.initialized = false;
1966 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1970 if (!psp->securedisplay_context.context.initialized)
1973 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1974 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1977 mutex_lock(&psp->securedisplay_context.mutex);
1979 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1981 mutex_unlock(&psp->securedisplay_context.mutex);
1985 /* SECUREDISPLAY end */
1987 static int psp_hw_start(struct psp_context *psp)
1989 struct amdgpu_device *adev = psp->adev;
1992 if (!amdgpu_sriov_vf(adev)) {
1993 if ((is_psp_fw_valid(psp->kdb)) &&
1994 (psp->funcs->bootloader_load_kdb != NULL)) {
1995 ret = psp_bootloader_load_kdb(psp);
1997 DRM_ERROR("PSP load kdb failed!\n");
2002 if ((is_psp_fw_valid(psp->spl)) &&
2003 (psp->funcs->bootloader_load_spl != NULL)) {
2004 ret = psp_bootloader_load_spl(psp);
2006 DRM_ERROR("PSP load spl failed!\n");
2011 if ((is_psp_fw_valid(psp->sys)) &&
2012 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2013 ret = psp_bootloader_load_sysdrv(psp);
2015 DRM_ERROR("PSP load sys drv failed!\n");
2020 if ((is_psp_fw_valid(psp->soc_drv)) &&
2021 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2022 ret = psp_bootloader_load_soc_drv(psp);
2024 DRM_ERROR("PSP load soc drv failed!\n");
2029 if ((is_psp_fw_valid(psp->intf_drv)) &&
2030 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2031 ret = psp_bootloader_load_intf_drv(psp);
2033 DRM_ERROR("PSP load intf drv failed!\n");
2038 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2039 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2040 ret = psp_bootloader_load_dbg_drv(psp);
2042 DRM_ERROR("PSP load dbg drv failed!\n");
2047 if ((is_psp_fw_valid(psp->sos)) &&
2048 (psp->funcs->bootloader_load_sos != NULL)) {
2049 ret = psp_bootloader_load_sos(psp);
2051 DRM_ERROR("PSP load sos failed!\n");
2057 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2059 DRM_ERROR("PSP create ring failed!\n");
2063 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2066 ret = psp_tmr_init(psp);
2068 DRM_ERROR("PSP tmr init failed!\n");
2074 * For ASICs with DF Cstate management centralized
2075 * to PMFW, TMR setup should be performed after PMFW
2076 * loaded and before other non-psp firmware loaded.
2078 if (psp->pmfw_centralized_cstate_management) {
2079 ret = psp_load_smu_fw(psp);
2084 ret = psp_tmr_load(psp);
2086 DRM_ERROR("PSP load tmr failed!\n");
2093 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2094 enum psp_gfx_fw_type *type)
2096 switch (ucode->ucode_id) {
2097 case AMDGPU_UCODE_ID_CAP:
2098 *type = GFX_FW_TYPE_CAP;
2100 case AMDGPU_UCODE_ID_SDMA0:
2101 *type = GFX_FW_TYPE_SDMA0;
2103 case AMDGPU_UCODE_ID_SDMA1:
2104 *type = GFX_FW_TYPE_SDMA1;
2106 case AMDGPU_UCODE_ID_SDMA2:
2107 *type = GFX_FW_TYPE_SDMA2;
2109 case AMDGPU_UCODE_ID_SDMA3:
2110 *type = GFX_FW_TYPE_SDMA3;
2112 case AMDGPU_UCODE_ID_SDMA4:
2113 *type = GFX_FW_TYPE_SDMA4;
2115 case AMDGPU_UCODE_ID_SDMA5:
2116 *type = GFX_FW_TYPE_SDMA5;
2118 case AMDGPU_UCODE_ID_SDMA6:
2119 *type = GFX_FW_TYPE_SDMA6;
2121 case AMDGPU_UCODE_ID_SDMA7:
2122 *type = GFX_FW_TYPE_SDMA7;
2124 case AMDGPU_UCODE_ID_CP_MES:
2125 *type = GFX_FW_TYPE_CP_MES;
2127 case AMDGPU_UCODE_ID_CP_MES_DATA:
2128 *type = GFX_FW_TYPE_MES_STACK;
2130 case AMDGPU_UCODE_ID_CP_MES1:
2131 *type = GFX_FW_TYPE_CP_MES_KIQ;
2133 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2134 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2136 case AMDGPU_UCODE_ID_CP_CE:
2137 *type = GFX_FW_TYPE_CP_CE;
2139 case AMDGPU_UCODE_ID_CP_PFP:
2140 *type = GFX_FW_TYPE_CP_PFP;
2142 case AMDGPU_UCODE_ID_CP_ME:
2143 *type = GFX_FW_TYPE_CP_ME;
2145 case AMDGPU_UCODE_ID_CP_MEC1:
2146 *type = GFX_FW_TYPE_CP_MEC;
2148 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2149 *type = GFX_FW_TYPE_CP_MEC_ME1;
2151 case AMDGPU_UCODE_ID_CP_MEC2:
2152 *type = GFX_FW_TYPE_CP_MEC;
2154 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2155 *type = GFX_FW_TYPE_CP_MEC_ME2;
2157 case AMDGPU_UCODE_ID_RLC_P:
2158 *type = GFX_FW_TYPE_RLC_P;
2160 case AMDGPU_UCODE_ID_RLC_V:
2161 *type = GFX_FW_TYPE_RLC_V;
2163 case AMDGPU_UCODE_ID_RLC_G:
2164 *type = GFX_FW_TYPE_RLC_G;
2166 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2167 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2169 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2170 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2172 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2173 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2175 case AMDGPU_UCODE_ID_RLC_IRAM:
2176 *type = GFX_FW_TYPE_RLC_IRAM;
2178 case AMDGPU_UCODE_ID_RLC_DRAM:
2179 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2181 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2182 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2184 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2185 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2187 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2188 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2190 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2191 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2193 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2194 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2196 case AMDGPU_UCODE_ID_SMC:
2197 *type = GFX_FW_TYPE_SMU;
2199 case AMDGPU_UCODE_ID_PPTABLE:
2200 *type = GFX_FW_TYPE_PPTABLE;
2202 case AMDGPU_UCODE_ID_UVD:
2203 *type = GFX_FW_TYPE_UVD;
2205 case AMDGPU_UCODE_ID_UVD1:
2206 *type = GFX_FW_TYPE_UVD1;
2208 case AMDGPU_UCODE_ID_VCE:
2209 *type = GFX_FW_TYPE_VCE;
2211 case AMDGPU_UCODE_ID_VCN:
2212 *type = GFX_FW_TYPE_VCN;
2214 case AMDGPU_UCODE_ID_VCN1:
2215 *type = GFX_FW_TYPE_VCN1;
2217 case AMDGPU_UCODE_ID_DMCU_ERAM:
2218 *type = GFX_FW_TYPE_DMCU_ERAM;
2220 case AMDGPU_UCODE_ID_DMCU_INTV:
2221 *type = GFX_FW_TYPE_DMCU_ISR;
2223 case AMDGPU_UCODE_ID_VCN0_RAM:
2224 *type = GFX_FW_TYPE_VCN0_RAM;
2226 case AMDGPU_UCODE_ID_VCN1_RAM:
2227 *type = GFX_FW_TYPE_VCN1_RAM;
2229 case AMDGPU_UCODE_ID_DMCUB:
2230 *type = GFX_FW_TYPE_DMUB;
2232 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2233 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2235 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2236 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2238 case AMDGPU_UCODE_ID_IMU_I:
2239 *type = GFX_FW_TYPE_IMU_I;
2241 case AMDGPU_UCODE_ID_IMU_D:
2242 *type = GFX_FW_TYPE_IMU_D;
2244 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2245 *type = GFX_FW_TYPE_RS64_PFP;
2247 case AMDGPU_UCODE_ID_CP_RS64_ME:
2248 *type = GFX_FW_TYPE_RS64_ME;
2250 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2251 *type = GFX_FW_TYPE_RS64_MEC;
2253 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2254 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2256 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2257 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2259 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2260 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2262 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2263 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2265 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2266 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2268 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2269 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2271 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2272 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2274 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2275 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2277 case AMDGPU_UCODE_ID_MAXIMUM:
2285 static void psp_print_fw_hdr(struct psp_context *psp,
2286 struct amdgpu_firmware_info *ucode)
2288 struct amdgpu_device *adev = psp->adev;
2289 struct common_firmware_header *hdr;
2291 switch (ucode->ucode_id) {
2292 case AMDGPU_UCODE_ID_SDMA0:
2293 case AMDGPU_UCODE_ID_SDMA1:
2294 case AMDGPU_UCODE_ID_SDMA2:
2295 case AMDGPU_UCODE_ID_SDMA3:
2296 case AMDGPU_UCODE_ID_SDMA4:
2297 case AMDGPU_UCODE_ID_SDMA5:
2298 case AMDGPU_UCODE_ID_SDMA6:
2299 case AMDGPU_UCODE_ID_SDMA7:
2300 hdr = (struct common_firmware_header *)
2301 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2302 amdgpu_ucode_print_sdma_hdr(hdr);
2304 case AMDGPU_UCODE_ID_CP_CE:
2305 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2306 amdgpu_ucode_print_gfx_hdr(hdr);
2308 case AMDGPU_UCODE_ID_CP_PFP:
2309 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2310 amdgpu_ucode_print_gfx_hdr(hdr);
2312 case AMDGPU_UCODE_ID_CP_ME:
2313 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2314 amdgpu_ucode_print_gfx_hdr(hdr);
2316 case AMDGPU_UCODE_ID_CP_MEC1:
2317 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2318 amdgpu_ucode_print_gfx_hdr(hdr);
2320 case AMDGPU_UCODE_ID_RLC_G:
2321 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2322 amdgpu_ucode_print_rlc_hdr(hdr);
2324 case AMDGPU_UCODE_ID_SMC:
2325 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2326 amdgpu_ucode_print_smc_hdr(hdr);
2333 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2334 struct psp_gfx_cmd_resp *cmd)
2337 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2339 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2340 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2341 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2342 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2344 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2346 DRM_ERROR("Unknown firmware type\n");
2351 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2352 struct amdgpu_firmware_info *ucode)
2355 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2357 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2359 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2360 psp->fence_buf_mc_addr);
2363 release_psp_cmd_buf(psp);
2368 static int psp_load_smu_fw(struct psp_context *psp)
2371 struct amdgpu_device *adev = psp->adev;
2372 struct amdgpu_firmware_info *ucode =
2373 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2374 struct amdgpu_ras *ras = psp->ras_context.ras;
2377 * Skip SMU FW reloading in case of using BACO for runpm only,
2378 * as SMU is always alive.
2380 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2383 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2386 if ((amdgpu_in_reset(adev) &&
2387 ras && adev->ras_enabled &&
2388 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2389 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2390 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2392 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2396 ret = psp_execute_non_psp_fw_load(psp, ucode);
2399 DRM_ERROR("PSP load smu failed!\n");
2404 static bool fw_load_skip_check(struct psp_context *psp,
2405 struct amdgpu_firmware_info *ucode)
2407 if (!ucode->fw || !ucode->ucode_size)
2410 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2411 (psp_smu_reload_quirk(psp) ||
2412 psp->autoload_supported ||
2413 psp->pmfw_centralized_cstate_management))
2416 if (amdgpu_sriov_vf(psp->adev) &&
2417 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
2418 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
2419 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2
2420 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3
2421 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA4
2422 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA5
2423 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA6
2424 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA7
2425 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G
2426 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
2427 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
2428 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
2429 || ucode->ucode_id == AMDGPU_UCODE_ID_SMC))
2430 /*skip ucode loading in SRIOV VF */
2433 if (psp->autoload_supported &&
2434 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2435 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2436 /* skip mec JT when autoload is enabled */
2442 int psp_load_fw_list(struct psp_context *psp,
2443 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2446 struct amdgpu_firmware_info *ucode;
2448 for (i = 0; i < ucode_count; ++i) {
2449 ucode = ucode_list[i];
2450 psp_print_fw_hdr(psp, ucode);
2451 ret = psp_execute_non_psp_fw_load(psp, ucode);
2458 static int psp_load_non_psp_fw(struct psp_context *psp)
2461 struct amdgpu_firmware_info *ucode;
2462 struct amdgpu_device *adev = psp->adev;
2464 if (psp->autoload_supported &&
2465 !psp->pmfw_centralized_cstate_management) {
2466 ret = psp_load_smu_fw(psp);
2471 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2472 ucode = &adev->firmware.ucode[i];
2474 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2475 !fw_load_skip_check(psp, ucode)) {
2476 ret = psp_load_smu_fw(psp);
2482 if (fw_load_skip_check(psp, ucode))
2485 if (psp->autoload_supported &&
2486 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2487 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2488 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2489 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2490 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2491 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2492 /* PSP only receive one SDMA fw for sienna_cichlid,
2493 * as all four sdma fw are same */
2496 psp_print_fw_hdr(psp, ucode);
2498 ret = psp_execute_non_psp_fw_load(psp, ucode);
2502 /* Start rlc autoload after psp recieved all the gfx firmware */
2503 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2504 AMDGPU_UCODE_ID_CP_MEC2 : AMDGPU_UCODE_ID_RLC_G)) {
2505 ret = psp_rlc_autoload_start(psp);
2507 DRM_ERROR("Failed to start rlc autoload\n");
2516 static int psp_load_fw(struct amdgpu_device *adev)
2519 struct psp_context *psp = &adev->psp;
2521 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2522 /* should not destroy ring, only stop */
2523 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2525 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2527 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2529 DRM_ERROR("PSP ring init failed!\n");
2534 ret = psp_hw_start(psp);
2538 ret = psp_load_non_psp_fw(psp);
2542 ret = psp_asd_initialize(psp);
2544 DRM_ERROR("PSP load asd failed!\n");
2548 ret = psp_rl_load(adev);
2550 DRM_ERROR("PSP load RL failed!\n");
2554 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2555 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2556 ret = psp_xgmi_initialize(psp, false, true);
2557 /* Warning the XGMI seesion initialize failure
2558 * Instead of stop driver initialization
2561 dev_err(psp->adev->dev,
2562 "XGMI: Failed to initialize XGMI session\n");
2567 ret = psp_ras_initialize(psp);
2569 dev_err(psp->adev->dev,
2570 "RAS: Failed to initialize RAS\n");
2572 ret = psp_hdcp_initialize(psp);
2574 dev_err(psp->adev->dev,
2575 "HDCP: Failed to initialize HDCP\n");
2577 ret = psp_dtm_initialize(psp);
2579 dev_err(psp->adev->dev,
2580 "DTM: Failed to initialize DTM\n");
2582 ret = psp_rap_initialize(psp);
2584 dev_err(psp->adev->dev,
2585 "RAP: Failed to initialize RAP\n");
2587 ret = psp_securedisplay_initialize(psp);
2589 dev_err(psp->adev->dev,
2590 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2596 psp_free_shared_bufs(psp);
2599 * all cleanup jobs (xgmi terminate, ras terminate,
2600 * ring destroy, cmd/fence/fw buffers destory,
2601 * psp->cmd destory) are delayed to psp_hw_fini
2603 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2607 static int psp_hw_init(void *handle)
2610 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2612 mutex_lock(&adev->firmware.mutex);
2614 * This sequence is just used on hw_init only once, no need on
2617 ret = amdgpu_ucode_init_bo(adev);
2621 ret = psp_load_fw(adev);
2623 DRM_ERROR("PSP firmware loading failed\n");
2627 mutex_unlock(&adev->firmware.mutex);
2631 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2632 mutex_unlock(&adev->firmware.mutex);
2636 static int psp_hw_fini(void *handle)
2638 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2639 struct psp_context *psp = &adev->psp;
2642 psp_ras_terminate(psp);
2643 psp_securedisplay_terminate(psp);
2644 psp_rap_terminate(psp);
2645 psp_dtm_terminate(psp);
2646 psp_hdcp_terminate(psp);
2648 if (adev->gmc.xgmi.num_physical_nodes > 1)
2649 psp_xgmi_terminate(psp);
2652 psp_asd_terminate(psp);
2653 psp_tmr_terminate(psp);
2655 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2657 psp_free_shared_bufs(psp);
2662 static int psp_suspend(void *handle)
2665 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2666 struct psp_context *psp = &adev->psp;
2668 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2669 psp->xgmi_context.context.initialized) {
2670 ret = psp_xgmi_terminate(psp);
2672 DRM_ERROR("Failed to terminate xgmi ta\n");
2678 ret = psp_ras_terminate(psp);
2680 DRM_ERROR("Failed to terminate ras ta\n");
2683 ret = psp_hdcp_terminate(psp);
2685 DRM_ERROR("Failed to terminate hdcp ta\n");
2688 ret = psp_dtm_terminate(psp);
2690 DRM_ERROR("Failed to terminate dtm ta\n");
2693 ret = psp_rap_terminate(psp);
2695 DRM_ERROR("Failed to terminate rap ta\n");
2698 ret = psp_securedisplay_terminate(psp);
2700 DRM_ERROR("Failed to terminate securedisplay ta\n");
2705 ret = psp_asd_terminate(psp);
2707 DRM_ERROR("Failed to terminate asd\n");
2711 ret = psp_tmr_terminate(psp);
2713 DRM_ERROR("Failed to terminate tmr\n");
2717 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2719 DRM_ERROR("PSP ring stop failed\n");
2723 psp_free_shared_bufs(psp);
2728 static int psp_resume(void *handle)
2731 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2732 struct psp_context *psp = &adev->psp;
2734 DRM_INFO("PSP is resuming...\n");
2736 if (psp->mem_train_ctx.enable_mem_training) {
2737 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2739 DRM_ERROR("Failed to process memory training!\n");
2744 mutex_lock(&adev->firmware.mutex);
2746 ret = psp_hw_start(psp);
2750 ret = psp_load_non_psp_fw(psp);
2754 ret = psp_asd_initialize(psp);
2756 DRM_ERROR("PSP load asd failed!\n");
2760 ret = psp_rl_load(adev);
2762 dev_err(adev->dev, "PSP load RL failed!\n");
2766 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2767 ret = psp_xgmi_initialize(psp, false, true);
2768 /* Warning the XGMI seesion initialize failure
2769 * Instead of stop driver initialization
2772 dev_err(psp->adev->dev,
2773 "XGMI: Failed to initialize XGMI session\n");
2777 ret = psp_ras_initialize(psp);
2779 dev_err(psp->adev->dev,
2780 "RAS: Failed to initialize RAS\n");
2782 ret = psp_hdcp_initialize(psp);
2784 dev_err(psp->adev->dev,
2785 "HDCP: Failed to initialize HDCP\n");
2787 ret = psp_dtm_initialize(psp);
2789 dev_err(psp->adev->dev,
2790 "DTM: Failed to initialize DTM\n");
2792 ret = psp_rap_initialize(psp);
2794 dev_err(psp->adev->dev,
2795 "RAP: Failed to initialize RAP\n");
2797 ret = psp_securedisplay_initialize(psp);
2799 dev_err(psp->adev->dev,
2800 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2803 mutex_unlock(&adev->firmware.mutex);
2808 DRM_ERROR("PSP resume failed\n");
2809 mutex_unlock(&adev->firmware.mutex);
2813 int psp_gpu_reset(struct amdgpu_device *adev)
2817 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2820 mutex_lock(&adev->psp.mutex);
2821 ret = psp_mode1_reset(&adev->psp);
2822 mutex_unlock(&adev->psp.mutex);
2827 int psp_rlc_autoload_start(struct psp_context *psp)
2830 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2832 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2834 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2835 psp->fence_buf_mc_addr);
2837 release_psp_cmd_buf(psp);
2842 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2843 uint64_t cmd_gpu_addr, int cmd_size)
2845 struct amdgpu_firmware_info ucode = {0};
2847 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2848 AMDGPU_UCODE_ID_VCN0_RAM;
2849 ucode.mc_addr = cmd_gpu_addr;
2850 ucode.ucode_size = cmd_size;
2852 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2855 int psp_ring_cmd_submit(struct psp_context *psp,
2856 uint64_t cmd_buf_mc_addr,
2857 uint64_t fence_mc_addr,
2860 unsigned int psp_write_ptr_reg = 0;
2861 struct psp_gfx_rb_frame *write_frame;
2862 struct psp_ring *ring = &psp->km_ring;
2863 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2864 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2865 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2866 struct amdgpu_device *adev = psp->adev;
2867 uint32_t ring_size_dw = ring->ring_size / 4;
2868 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2870 /* KM (GPCOM) prepare write pointer */
2871 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2873 /* Update KM RB frame pointer to new frame */
2874 /* write_frame ptr increments by size of rb_frame in bytes */
2875 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2876 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2877 write_frame = ring_buffer_start;
2879 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2880 /* Check invalid write_frame ptr address */
2881 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2882 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2883 ring_buffer_start, ring_buffer_end, write_frame);
2884 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2888 /* Initialize KM RB frame */
2889 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2891 /* Update KM RB frame */
2892 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2893 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2894 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2895 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2896 write_frame->fence_value = index;
2897 amdgpu_device_flush_hdp(adev, NULL);
2899 /* Update the write Pointer in DWORDs */
2900 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2901 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2905 int psp_init_asd_microcode(struct psp_context *psp,
2906 const char *chip_name)
2908 struct amdgpu_device *adev = psp->adev;
2909 char fw_name[PSP_FW_NAME_LEN];
2910 const struct psp_firmware_header_v1_0 *asd_hdr;
2914 dev_err(adev->dev, "invalid chip name for asd microcode\n");
2918 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2919 err = request_firmware(&adev->psp.asd_fw, fw_name, adev->dev);
2923 err = amdgpu_ucode_validate(adev->psp.asd_fw);
2927 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2928 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2929 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2930 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2931 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2932 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2935 dev_err(adev->dev, "fail to initialize asd microcode\n");
2936 release_firmware(adev->psp.asd_fw);
2937 adev->psp.asd_fw = NULL;
2941 int psp_init_toc_microcode(struct psp_context *psp,
2942 const char *chip_name)
2944 struct amdgpu_device *adev = psp->adev;
2945 char fw_name[PSP_FW_NAME_LEN];
2946 const struct psp_firmware_header_v1_0 *toc_hdr;
2950 dev_err(adev->dev, "invalid chip name for toc microcode\n");
2954 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2955 err = request_firmware(&adev->psp.toc_fw, fw_name, adev->dev);
2959 err = amdgpu_ucode_validate(adev->psp.toc_fw);
2963 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2964 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2965 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2966 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2967 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2968 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2971 dev_err(adev->dev, "fail to request/validate toc microcode\n");
2972 release_firmware(adev->psp.toc_fw);
2973 adev->psp.toc_fw = NULL;
2977 static int parse_sos_bin_descriptor(struct psp_context *psp,
2978 const struct psp_fw_bin_desc *desc,
2979 const struct psp_firmware_header_v2_0 *sos_hdr)
2981 uint8_t *ucode_start_addr = NULL;
2983 if (!psp || !desc || !sos_hdr)
2986 ucode_start_addr = (uint8_t *)sos_hdr +
2987 le32_to_cpu(desc->offset_bytes) +
2988 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2990 switch (desc->fw_type) {
2991 case PSP_FW_TYPE_PSP_SOS:
2992 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
2993 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
2994 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
2995 psp->sos.start_addr = ucode_start_addr;
2997 case PSP_FW_TYPE_PSP_SYS_DRV:
2998 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
2999 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3000 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3001 psp->sys.start_addr = ucode_start_addr;
3003 case PSP_FW_TYPE_PSP_KDB:
3004 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3005 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3006 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3007 psp->kdb.start_addr = ucode_start_addr;
3009 case PSP_FW_TYPE_PSP_TOC:
3010 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3011 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3012 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3013 psp->toc.start_addr = ucode_start_addr;
3015 case PSP_FW_TYPE_PSP_SPL:
3016 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3017 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3018 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3019 psp->spl.start_addr = ucode_start_addr;
3021 case PSP_FW_TYPE_PSP_RL:
3022 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3023 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3024 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3025 psp->rl.start_addr = ucode_start_addr;
3027 case PSP_FW_TYPE_PSP_SOC_DRV:
3028 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3029 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3030 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3031 psp->soc_drv.start_addr = ucode_start_addr;
3033 case PSP_FW_TYPE_PSP_INTF_DRV:
3034 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3035 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3036 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3037 psp->intf_drv.start_addr = ucode_start_addr;
3039 case PSP_FW_TYPE_PSP_DBG_DRV:
3040 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3041 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3042 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3043 psp->dbg_drv.start_addr = ucode_start_addr;
3046 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3053 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3055 const struct psp_firmware_header_v1_0 *sos_hdr;
3056 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3057 uint8_t *ucode_array_start_addr;
3059 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3060 ucode_array_start_addr = (uint8_t *)sos_hdr +
3061 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3063 if (adev->gmc.xgmi.connected_to_cpu ||
3064 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3065 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3066 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3068 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3069 adev->psp.sys.start_addr = ucode_array_start_addr;
3071 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3072 adev->psp.sos.start_addr = ucode_array_start_addr +
3073 le32_to_cpu(sos_hdr->sos.offset_bytes);
3075 /* Load alternate PSP SOS FW */
3076 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3078 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3079 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3081 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3082 adev->psp.sys.start_addr = ucode_array_start_addr +
3083 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3085 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3086 adev->psp.sos.start_addr = ucode_array_start_addr +
3087 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3090 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3091 dev_warn(adev->dev, "PSP SOS FW not available");
3098 int psp_init_sos_microcode(struct psp_context *psp,
3099 const char *chip_name)
3101 struct amdgpu_device *adev = psp->adev;
3102 char fw_name[PSP_FW_NAME_LEN];
3103 const struct psp_firmware_header_v1_0 *sos_hdr;
3104 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3105 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3106 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3107 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3109 uint8_t *ucode_array_start_addr;
3113 dev_err(adev->dev, "invalid chip name for sos microcode\n");
3117 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3118 err = request_firmware(&adev->psp.sos_fw, fw_name, adev->dev);
3122 err = amdgpu_ucode_validate(adev->psp.sos_fw);
3126 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3127 ucode_array_start_addr = (uint8_t *)sos_hdr +
3128 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3129 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3131 switch (sos_hdr->header.header_version_major) {
3133 err = psp_init_sos_base_fw(adev);
3137 if (sos_hdr->header.header_version_minor == 1) {
3138 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3139 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3140 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3141 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3142 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3143 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3144 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3146 if (sos_hdr->header.header_version_minor == 2) {
3147 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3148 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3149 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3150 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3152 if (sos_hdr->header.header_version_minor == 3) {
3153 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3154 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3155 adev->psp.toc.start_addr = ucode_array_start_addr +
3156 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3157 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3158 adev->psp.kdb.start_addr = ucode_array_start_addr +
3159 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3160 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3161 adev->psp.spl.start_addr = ucode_array_start_addr +
3162 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3163 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3164 adev->psp.rl.start_addr = ucode_array_start_addr +
3165 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3169 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3171 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3172 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3177 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3178 err = parse_sos_bin_descriptor(psp,
3179 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3187 "unsupported psp sos firmware\n");
3195 "failed to init sos firmware\n");
3196 release_firmware(adev->psp.sos_fw);
3197 adev->psp.sos_fw = NULL;
3202 static int parse_ta_bin_descriptor(struct psp_context *psp,
3203 const struct psp_fw_bin_desc *desc,
3204 const struct ta_firmware_header_v2_0 *ta_hdr)
3206 uint8_t *ucode_start_addr = NULL;
3208 if (!psp || !desc || !ta_hdr)
3211 ucode_start_addr = (uint8_t *)ta_hdr +
3212 le32_to_cpu(desc->offset_bytes) +
3213 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3215 switch (desc->fw_type) {
3216 case TA_FW_TYPE_PSP_ASD:
3217 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3218 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3219 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3220 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3222 case TA_FW_TYPE_PSP_XGMI:
3223 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3224 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3225 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3227 case TA_FW_TYPE_PSP_RAS:
3228 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3229 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3230 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3232 case TA_FW_TYPE_PSP_HDCP:
3233 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3234 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3235 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3237 case TA_FW_TYPE_PSP_DTM:
3238 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3239 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3240 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3242 case TA_FW_TYPE_PSP_RAP:
3243 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3244 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3245 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3247 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3248 psp->securedisplay_context.context.bin_desc.fw_version =
3249 le32_to_cpu(desc->fw_version);
3250 psp->securedisplay_context.context.bin_desc.size_bytes =
3251 le32_to_cpu(desc->size_bytes);
3252 psp->securedisplay_context.context.bin_desc.start_addr =
3256 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3263 int psp_init_ta_microcode(struct psp_context *psp,
3264 const char *chip_name)
3266 struct amdgpu_device *adev = psp->adev;
3267 char fw_name[PSP_FW_NAME_LEN];
3268 const struct ta_firmware_header_v2_0 *ta_hdr;
3273 dev_err(adev->dev, "invalid chip name for ta microcode\n");
3277 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3278 err = request_firmware(&adev->psp.ta_fw, fw_name, adev->dev);
3282 err = amdgpu_ucode_validate(adev->psp.ta_fw);
3286 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3288 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2) {
3289 dev_err(adev->dev, "unsupported TA header version\n");
3294 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3295 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3300 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3301 err = parse_ta_bin_descriptor(psp,
3302 &ta_hdr->ta_fw_bin[ta_index],
3310 dev_err(adev->dev, "fail to initialize ta microcode\n");
3311 release_firmware(adev->psp.ta_fw);
3312 adev->psp.ta_fw = NULL;
3316 int psp_init_cap_microcode(struct psp_context *psp,
3317 const char *chip_name)
3319 struct amdgpu_device *adev = psp->adev;
3320 char fw_name[PSP_FW_NAME_LEN];
3321 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3322 struct amdgpu_firmware_info *info = NULL;
3326 dev_err(adev->dev, "invalid chip name for cap microcode\n");
3330 if (!amdgpu_sriov_vf(adev)) {
3331 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3335 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3336 err = request_firmware(&adev->psp.cap_fw, fw_name, adev->dev);
3338 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3343 err = amdgpu_ucode_validate(adev->psp.cap_fw);
3345 dev_err(adev->dev, "fail to initialize cap microcode\n");
3349 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3350 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3351 info->fw = adev->psp.cap_fw;
3352 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3353 adev->psp.cap_fw->data;
3354 adev->firmware.fw_size += ALIGN(
3355 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3356 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3357 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3358 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3363 release_firmware(adev->psp.cap_fw);
3364 adev->psp.cap_fw = NULL;
3368 static int psp_set_clockgating_state(void *handle,
3369 enum amd_clockgating_state state)
3374 static int psp_set_powergating_state(void *handle,
3375 enum amd_powergating_state state)
3380 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3381 struct device_attribute *attr,
3384 struct drm_device *ddev = dev_get_drvdata(dev);
3385 struct amdgpu_device *adev = drm_to_adev(ddev);
3389 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3390 DRM_INFO("PSP block is not ready yet.");
3394 mutex_lock(&adev->psp.mutex);
3395 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3396 mutex_unlock(&adev->psp.mutex);
3399 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3403 return sysfs_emit(buf, "%x\n", fw_ver);
3406 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3407 struct device_attribute *attr,
3411 struct drm_device *ddev = dev_get_drvdata(dev);
3412 struct amdgpu_device *adev = drm_to_adev(ddev);
3415 const struct firmware *usbc_pd_fw;
3416 struct amdgpu_bo *fw_buf_bo = NULL;
3417 uint64_t fw_pri_mc_addr;
3418 void *fw_pri_cpu_addr;
3420 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3421 DRM_INFO("PSP block is not ready yet.");
3425 if (!drm_dev_enter(ddev, &idx))
3428 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3429 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3433 /* LFB address which is aligned to 1MB boundary per PSP request */
3434 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3435 AMDGPU_GEM_DOMAIN_VRAM,
3442 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3444 mutex_lock(&adev->psp.mutex);
3445 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3446 mutex_unlock(&adev->psp.mutex);
3448 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3451 release_firmware(usbc_pd_fw);
3454 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3462 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3466 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3469 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3470 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3475 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3476 psp_usbc_pd_fw_sysfs_read,
3477 psp_usbc_pd_fw_sysfs_write);
3479 int is_psp_fw_valid(struct psp_bin_desc bin)
3481 return bin.size_bytes;
3484 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3485 struct bin_attribute *bin_attr,
3486 char *buffer, loff_t pos, size_t count)
3488 struct device *dev = kobj_to_dev(kobj);
3489 struct drm_device *ddev = dev_get_drvdata(dev);
3490 struct amdgpu_device *adev = drm_to_adev(ddev);
3492 adev->psp.vbflash_done = false;
3494 /* Safeguard against memory drain */
3495 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3496 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3497 kvfree(adev->psp.vbflash_tmp_buf);
3498 adev->psp.vbflash_tmp_buf = NULL;
3499 adev->psp.vbflash_image_size = 0;
3503 /* TODO Just allocate max for now and optimize to realloc later if needed */
3504 if (!adev->psp.vbflash_tmp_buf) {
3505 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3506 if (!adev->psp.vbflash_tmp_buf)
3510 mutex_lock(&adev->psp.mutex);
3511 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3512 adev->psp.vbflash_image_size += count;
3513 mutex_unlock(&adev->psp.mutex);
3515 dev_info(adev->dev, "VBIOS flash write PSP done");
3520 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3521 struct bin_attribute *bin_attr, char *buffer,
3522 loff_t pos, size_t count)
3524 struct device *dev = kobj_to_dev(kobj);
3525 struct drm_device *ddev = dev_get_drvdata(dev);
3526 struct amdgpu_device *adev = drm_to_adev(ddev);
3527 struct amdgpu_bo *fw_buf_bo = NULL;
3528 uint64_t fw_pri_mc_addr;
3529 void *fw_pri_cpu_addr;
3532 dev_info(adev->dev, "VBIOS flash to PSP started");
3534 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3535 AMDGPU_GPU_PAGE_SIZE,
3536 AMDGPU_GEM_DOMAIN_VRAM,
3543 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3545 mutex_lock(&adev->psp.mutex);
3546 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3547 mutex_unlock(&adev->psp.mutex);
3549 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3552 kvfree(adev->psp.vbflash_tmp_buf);
3553 adev->psp.vbflash_tmp_buf = NULL;
3554 adev->psp.vbflash_image_size = 0;
3557 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3561 dev_info(adev->dev, "VBIOS flash to PSP done");
3565 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3566 struct device_attribute *attr,
3569 struct drm_device *ddev = dev_get_drvdata(dev);
3570 struct amdgpu_device *adev = drm_to_adev(ddev);
3571 uint32_t vbflash_status;
3573 vbflash_status = psp_vbflash_status(&adev->psp);
3574 if (!adev->psp.vbflash_done)
3576 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3579 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3582 static const struct bin_attribute psp_vbflash_bin_attr = {
3583 .attr = {.name = "psp_vbflash", .mode = 0664},
3585 .write = amdgpu_psp_vbflash_write,
3586 .read = amdgpu_psp_vbflash_read,
3589 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3591 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3594 struct psp_context *psp = &adev->psp;
3596 if (amdgpu_sriov_vf(adev))
3599 switch (adev->ip_versions[MP0_HWIP][0]) {
3600 case IP_VERSION(13, 0, 0):
3601 case IP_VERSION(13, 0, 7):
3604 psp_v13_0_set_psp_funcs(psp);
3606 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3608 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3609 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3611 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3618 const struct amd_ip_funcs psp_ip_funcs = {
3620 .early_init = psp_early_init,
3622 .sw_init = psp_sw_init,
3623 .sw_fini = psp_sw_fini,
3624 .hw_init = psp_hw_init,
3625 .hw_fini = psp_hw_fini,
3626 .suspend = psp_suspend,
3627 .resume = psp_resume,
3629 .check_soft_reset = NULL,
3630 .wait_for_idle = NULL,
3632 .set_clockgating_state = psp_set_clockgating_state,
3633 .set_powergating_state = psp_set_powergating_state,
3636 static int psp_sysfs_init(struct amdgpu_device *adev)
3638 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3641 DRM_ERROR("Failed to create USBC PD FW control file!");
3646 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3648 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3649 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3652 static void psp_sysfs_fini(struct amdgpu_device *adev)
3654 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3657 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3659 .type = AMD_IP_BLOCK_TYPE_PSP,
3663 .funcs = &psp_ip_funcs,
3666 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3668 .type = AMD_IP_BLOCK_TYPE_PSP,
3672 .funcs = &psp_ip_funcs,
3675 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3677 .type = AMD_IP_BLOCK_TYPE_PSP,
3681 .funcs = &psp_ip_funcs,
3684 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3685 .type = AMD_IP_BLOCK_TYPE_PSP,
3689 .funcs = &psp_ip_funcs,
3692 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3694 .type = AMD_IP_BLOCK_TYPE_PSP,
3698 .funcs = &psp_ip_funcs,
3701 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3702 .type = AMD_IP_BLOCK_TYPE_PSP,
3706 .funcs = &psp_ip_funcs,
3709 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3710 .type = AMD_IP_BLOCK_TYPE_PSP,
3714 .funcs = &psp_ip_funcs,