2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/firmware.h>
27 #include <drm/drm_drv.h>
30 #include "amdgpu_psp.h"
31 #include "amdgpu_ucode.h"
32 #include "amdgpu_xgmi.h"
33 #include "soc15_common.h"
35 #include "psp_v10_0.h"
36 #include "psp_v11_0.h"
37 #include "psp_v11_0_8.h"
38 #include "psp_v12_0.h"
39 #include "psp_v13_0.h"
40 #include "psp_v13_0_4.h"
42 #include "amdgpu_ras.h"
43 #include "amdgpu_securedisplay.h"
44 #include "amdgpu_atomfirmware.h"
46 #define AMD_VBIOS_FILE_MAX_SIZE_B (1024*1024*3)
48 static int psp_sysfs_init(struct amdgpu_device *adev);
49 static void psp_sysfs_fini(struct amdgpu_device *adev);
51 static int psp_load_smu_fw(struct psp_context *psp);
52 static int psp_rap_terminate(struct psp_context *psp);
53 static int psp_securedisplay_terminate(struct psp_context *psp);
55 static int psp_ring_init(struct psp_context *psp,
56 enum psp_ring_type ring_type)
59 struct psp_ring *ring;
60 struct amdgpu_device *adev = psp->adev;
64 ring->ring_type = ring_type;
66 /* allocate 4k Page of Local Frame Buffer memory for ring */
67 ring->ring_size = 0x1000;
68 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
69 AMDGPU_GEM_DOMAIN_VRAM |
70 AMDGPU_GEM_DOMAIN_GTT,
72 &ring->ring_mem_mc_addr,
73 (void **)&ring->ring_mem);
83 * Due to DF Cstate management centralized to PMFW, the firmware
84 * loading sequence will be updated as below:
90 * - Load other non-psp fw
92 * - Load XGMI/RAS/HDCP/DTM TA if any
94 * This new sequence is required for
95 * - Arcturus and onwards
97 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
99 struct amdgpu_device *adev = psp->adev;
101 if (amdgpu_sriov_vf(adev)) {
102 psp->pmfw_centralized_cstate_management = false;
106 switch (adev->ip_versions[MP0_HWIP][0]) {
107 case IP_VERSION(11, 0, 0):
108 case IP_VERSION(11, 0, 4):
109 case IP_VERSION(11, 0, 5):
110 case IP_VERSION(11, 0, 7):
111 case IP_VERSION(11, 0, 9):
112 case IP_VERSION(11, 0, 11):
113 case IP_VERSION(11, 0, 12):
114 case IP_VERSION(11, 0, 13):
115 case IP_VERSION(13, 0, 0):
116 case IP_VERSION(13, 0, 2):
117 case IP_VERSION(13, 0, 7):
118 psp->pmfw_centralized_cstate_management = true;
121 psp->pmfw_centralized_cstate_management = false;
126 static int psp_init_sriov_microcode(struct psp_context *psp)
128 struct amdgpu_device *adev = psp->adev;
129 char ucode_prefix[30];
132 amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
134 switch (adev->ip_versions[MP0_HWIP][0]) {
135 case IP_VERSION(9, 0, 0):
136 case IP_VERSION(11, 0, 7):
137 case IP_VERSION(11, 0, 9):
138 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
139 ret = psp_init_cap_microcode(psp, ucode_prefix);
141 case IP_VERSION(13, 0, 2):
142 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
143 ret = psp_init_cap_microcode(psp, ucode_prefix);
144 ret &= psp_init_ta_microcode(psp, ucode_prefix);
146 case IP_VERSION(13, 0, 0):
147 adev->virt.autoload_ucode_id = 0;
149 case IP_VERSION(13, 0, 10):
150 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
158 static int psp_early_init(void *handle)
160 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
161 struct psp_context *psp = &adev->psp;
163 switch (adev->ip_versions[MP0_HWIP][0]) {
164 case IP_VERSION(9, 0, 0):
165 psp_v3_1_set_psp_funcs(psp);
166 psp->autoload_supported = false;
168 case IP_VERSION(10, 0, 0):
169 case IP_VERSION(10, 0, 1):
170 psp_v10_0_set_psp_funcs(psp);
171 psp->autoload_supported = false;
173 case IP_VERSION(11, 0, 2):
174 case IP_VERSION(11, 0, 4):
175 psp_v11_0_set_psp_funcs(psp);
176 psp->autoload_supported = false;
178 case IP_VERSION(11, 0, 0):
179 case IP_VERSION(11, 0, 5):
180 case IP_VERSION(11, 0, 9):
181 case IP_VERSION(11, 0, 7):
182 case IP_VERSION(11, 0, 11):
183 case IP_VERSION(11, 5, 0):
184 case IP_VERSION(11, 0, 12):
185 case IP_VERSION(11, 0, 13):
186 psp_v11_0_set_psp_funcs(psp);
187 psp->autoload_supported = true;
189 case IP_VERSION(11, 0, 3):
190 case IP_VERSION(12, 0, 1):
191 psp_v12_0_set_psp_funcs(psp);
193 case IP_VERSION(13, 0, 2):
194 psp_v13_0_set_psp_funcs(psp);
196 case IP_VERSION(13, 0, 1):
197 case IP_VERSION(13, 0, 3):
198 case IP_VERSION(13, 0, 5):
199 case IP_VERSION(13, 0, 8):
200 case IP_VERSION(13, 0, 10):
201 case IP_VERSION(13, 0, 11):
202 psp_v13_0_set_psp_funcs(psp);
203 psp->autoload_supported = true;
205 case IP_VERSION(11, 0, 8):
206 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
207 psp_v11_0_8_set_psp_funcs(psp);
208 psp->autoload_supported = false;
211 case IP_VERSION(13, 0, 0):
212 case IP_VERSION(13, 0, 7):
213 psp_v13_0_set_psp_funcs(psp);
214 psp->autoload_supported = true;
216 case IP_VERSION(13, 0, 4):
217 psp_v13_0_4_set_psp_funcs(psp);
218 psp->autoload_supported = true;
226 psp_check_pmfw_centralized_cstate_management(psp);
228 if (amdgpu_sriov_vf(adev))
229 return psp_init_sriov_microcode(psp);
231 return psp_init_microcode(psp);
234 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
236 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
237 &mem_ctx->shared_buf);
238 mem_ctx->shared_bo = NULL;
241 static void psp_free_shared_bufs(struct psp_context *psp)
246 /* free TMR memory buffer */
247 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
248 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
251 /* free xgmi shared memory */
252 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
254 /* free ras shared memory */
255 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
257 /* free hdcp shared memory */
258 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
260 /* free dtm shared memory */
261 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
263 /* free rap shared memory */
264 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
266 /* free securedisplay shared memory */
267 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
272 static void psp_memory_training_fini(struct psp_context *psp)
274 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
276 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
277 kfree(ctx->sys_cache);
278 ctx->sys_cache = NULL;
281 static int psp_memory_training_init(struct psp_context *psp)
284 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
286 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
287 DRM_DEBUG("memory training is not supported!\n");
291 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
292 if (ctx->sys_cache == NULL) {
293 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
298 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
299 ctx->train_data_size,
300 ctx->p2c_train_data_offset,
301 ctx->c2p_train_data_offset);
302 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
306 psp_memory_training_fini(psp);
311 * Helper funciton to query psp runtime database entry
313 * @adev: amdgpu_device pointer
314 * @entry_type: the type of psp runtime database entry
315 * @db_entry: runtime database entry pointer
317 * Return false if runtime database doesn't exit or entry is invalid
318 * or true if the specific database entry is found, and copy to @db_entry
320 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
321 enum psp_runtime_entry_type entry_type,
324 uint64_t db_header_pos, db_dir_pos;
325 struct psp_runtime_data_header db_header = {0};
326 struct psp_runtime_data_directory db_dir = {0};
330 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
331 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
333 /* read runtime db header from vram */
334 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
335 sizeof(struct psp_runtime_data_header), false);
337 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
338 /* runtime db doesn't exist, exit */
339 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
343 /* read runtime database entry from vram */
344 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
345 sizeof(struct psp_runtime_data_directory), false);
347 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
348 /* invalid db entry count, exit */
349 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
353 /* look up for requested entry type */
354 for (i = 0; i < db_dir.entry_count && !ret; i++) {
355 if (db_dir.entry_list[i].entry_type == entry_type) {
356 switch (entry_type) {
357 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
358 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
359 /* invalid db entry size */
360 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
363 /* read runtime database entry */
364 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
365 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
368 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
369 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
370 /* invalid db entry size */
371 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
374 /* read runtime database entry */
375 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
376 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
389 static int psp_sw_init(void *handle)
391 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
392 struct psp_context *psp = &adev->psp;
394 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
395 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
396 struct psp_runtime_scpm_entry scpm_entry;
398 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
400 DRM_ERROR("Failed to allocate memory to command buffer!\n");
404 adev->psp.xgmi_context.supports_extended_data =
405 !adev->gmc.xgmi.connected_to_cpu &&
406 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
408 memset(&scpm_entry, 0, sizeof(scpm_entry));
409 if ((psp_get_runtime_db_entry(adev,
410 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
412 (SCPM_DISABLE != scpm_entry.scpm_status)) {
413 adev->scpm_enabled = true;
414 adev->scpm_status = scpm_entry.scpm_status;
416 adev->scpm_enabled = false;
417 adev->scpm_status = SCPM_DISABLE;
420 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
422 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
423 if (psp_get_runtime_db_entry(adev,
424 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
426 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
427 if ((psp->boot_cfg_bitmask) &
428 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
429 /* If psp runtime database exists, then
430 * only enable two stage memory training
431 * when TWO_STAGE_DRAM_TRAINING bit is set
432 * in runtime database */
433 mem_training_ctx->enable_mem_training = true;
437 /* If psp runtime database doesn't exist or
438 * is invalid, force enable two stage memory
440 mem_training_ctx->enable_mem_training = true;
443 if (mem_training_ctx->enable_mem_training) {
444 ret = psp_memory_training_init(psp);
446 DRM_ERROR("Failed to initialize memory training!\n");
450 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
452 DRM_ERROR("Failed to process memory training!\n");
457 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
458 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
459 ret= psp_sysfs_init(adev);
465 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
466 amdgpu_sriov_vf(adev) ?
467 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
469 &psp->fw_pri_mc_addr,
474 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
475 AMDGPU_GEM_DOMAIN_VRAM,
477 &psp->fence_buf_mc_addr,
482 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
483 AMDGPU_GEM_DOMAIN_VRAM,
484 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
485 (void **)&psp->cmd_buf_mem);
492 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
493 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
495 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
496 &psp->fence_buf_mc_addr, &psp->fence_buf);
500 static int psp_sw_fini(void *handle)
502 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
503 struct psp_context *psp = &adev->psp;
504 struct psp_gfx_cmd_resp *cmd = psp->cmd;
506 psp_memory_training_fini(psp);
508 amdgpu_ucode_release(&psp->sos_fw);
509 amdgpu_ucode_release(&psp->asd_fw);
510 amdgpu_ucode_release(&psp->ta_fw);
511 amdgpu_ucode_release(&psp->cap_fw);
512 amdgpu_ucode_release(&psp->toc_fw);
514 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
515 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
516 psp_sysfs_fini(adev);
521 if (psp->km_ring.ring_mem)
522 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
523 &psp->km_ring.ring_mem_mc_addr,
524 (void **)&psp->km_ring.ring_mem);
526 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
527 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
528 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
529 &psp->fence_buf_mc_addr, &psp->fence_buf);
530 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
531 (void **)&psp->cmd_buf_mem);
536 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
537 uint32_t reg_val, uint32_t mask, bool check_changed)
541 struct amdgpu_device *adev = psp->adev;
543 if (psp->adev->no_hw_access)
546 for (i = 0; i < adev->usec_timeout; i++) {
547 val = RREG32(reg_index);
552 if ((val & mask) == reg_val)
561 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
564 case GFX_CMD_ID_LOAD_TA:
566 case GFX_CMD_ID_UNLOAD_TA:
568 case GFX_CMD_ID_INVOKE_CMD:
570 case GFX_CMD_ID_LOAD_ASD:
572 case GFX_CMD_ID_SETUP_TMR:
574 case GFX_CMD_ID_LOAD_IP_FW:
576 case GFX_CMD_ID_DESTROY_TMR:
577 return "DESTROY_TMR";
578 case GFX_CMD_ID_SAVE_RESTORE:
579 return "SAVE_RESTORE_IP_FW";
580 case GFX_CMD_ID_SETUP_VMR:
582 case GFX_CMD_ID_DESTROY_VMR:
583 return "DESTROY_VMR";
584 case GFX_CMD_ID_PROG_REG:
586 case GFX_CMD_ID_GET_FW_ATTESTATION:
587 return "GET_FW_ATTESTATION";
588 case GFX_CMD_ID_LOAD_TOC:
589 return "ID_LOAD_TOC";
590 case GFX_CMD_ID_AUTOLOAD_RLC:
591 return "AUTOLOAD_RLC";
592 case GFX_CMD_ID_BOOT_CFG:
595 return "UNKNOWN CMD";
600 psp_cmd_submit_buf(struct psp_context *psp,
601 struct amdgpu_firmware_info *ucode,
602 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
607 bool ras_intr = false;
608 bool skip_unsupport = false;
610 if (psp->adev->no_hw_access)
613 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
615 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
617 index = atomic_inc_return(&psp->fence_value);
618 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
620 atomic_dec(&psp->fence_value);
624 amdgpu_device_invalidate_hdp(psp->adev, NULL);
625 while (*((unsigned int *)psp->fence_buf) != index) {
629 * Shouldn't wait for timeout when err_event_athub occurs,
630 * because gpu reset thread triggered and lock resource should
631 * be released for psp resume sequence.
633 ras_intr = amdgpu_ras_intr_triggered();
636 usleep_range(10, 100);
637 amdgpu_device_invalidate_hdp(psp->adev, NULL);
640 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
641 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
642 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
644 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
646 /* In some cases, psp response status is not 0 even there is no
647 * problem while the command is submitted. Some version of PSP FW
648 * doesn't write 0 to that field.
649 * So here we would like to only print a warning instead of an error
650 * during psp initialization to avoid breaking hw_init and it doesn't
653 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
655 DRM_WARN("failed to load ucode %s(0x%X) ",
656 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
657 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
658 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
659 psp->cmd_buf_mem->resp.status);
660 /* If any firmware (including CAP) load fails under SRIOV, it should
661 * return failure to stop the VF from initializing.
662 * Also return failure in case of timeout
664 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
671 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
672 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
679 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
681 struct psp_gfx_cmd_resp *cmd = psp->cmd;
683 mutex_lock(&psp->mutex);
685 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
690 static void release_psp_cmd_buf(struct psp_context *psp)
692 mutex_unlock(&psp->mutex);
695 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
696 struct psp_gfx_cmd_resp *cmd,
697 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
699 struct amdgpu_device *adev = psp->adev;
700 uint32_t size = amdgpu_bo_size(tmr_bo);
701 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
703 if (amdgpu_sriov_vf(psp->adev))
704 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
706 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
707 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
708 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
709 cmd->cmd.cmd_setup_tmr.buf_size = size;
710 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
711 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
712 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
715 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
716 uint64_t pri_buf_mc, uint32_t size)
718 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
719 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
720 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
721 cmd->cmd.cmd_load_toc.toc_size = size;
724 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
725 static int psp_load_toc(struct psp_context *psp,
729 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
731 /* Copy toc to psp firmware private buffer */
732 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
734 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
736 ret = psp_cmd_submit_buf(psp, NULL, cmd,
737 psp->fence_buf_mc_addr);
739 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
741 release_psp_cmd_buf(psp);
746 /* Set up Trusted Memory Region */
747 static int psp_tmr_init(struct psp_context *psp)
755 * According to HW engineer, they prefer the TMR address be "naturally
756 * aligned" , e.g. the start address be an integer divide of TMR size.
758 * Note: this memory need be reserved till the driver
761 tmr_size = PSP_TMR_SIZE(psp->adev);
763 /* For ASICs support RLC autoload, psp will parse the toc
764 * and calculate the total size of TMR needed */
765 if (!amdgpu_sriov_vf(psp->adev) &&
766 psp->toc.start_addr &&
767 psp->toc.size_bytes &&
769 ret = psp_load_toc(psp, &tmr_size);
771 DRM_ERROR("Failed to load toc\n");
777 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
778 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
780 AMDGPU_HAS_VRAM(psp->adev) ?
781 AMDGPU_GEM_DOMAIN_VRAM :
782 AMDGPU_GEM_DOMAIN_GTT,
783 &psp->tmr_bo, &psp->tmr_mc_addr,
790 static bool psp_skip_tmr(struct psp_context *psp)
792 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
793 case IP_VERSION(11, 0, 9):
794 case IP_VERSION(11, 0, 7):
795 case IP_VERSION(13, 0, 2):
796 case IP_VERSION(13, 0, 10):
803 static int psp_tmr_load(struct psp_context *psp)
806 struct psp_gfx_cmd_resp *cmd;
808 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
809 * Already set up by host driver.
811 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
814 cmd = acquire_psp_cmd_buf(psp);
816 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
817 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
818 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
820 ret = psp_cmd_submit_buf(psp, NULL, cmd,
821 psp->fence_buf_mc_addr);
823 release_psp_cmd_buf(psp);
828 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
829 struct psp_gfx_cmd_resp *cmd)
831 if (amdgpu_sriov_vf(psp->adev))
832 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
834 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
837 static int psp_tmr_unload(struct psp_context *psp)
840 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
842 psp_prep_tmr_unload_cmd_buf(psp, cmd);
843 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
845 ret = psp_cmd_submit_buf(psp, NULL, cmd,
846 psp->fence_buf_mc_addr);
848 release_psp_cmd_buf(psp);
853 static int psp_tmr_terminate(struct psp_context *psp)
855 return psp_tmr_unload(psp);
858 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
859 uint64_t *output_ptr)
862 struct psp_gfx_cmd_resp *cmd;
867 if (amdgpu_sriov_vf(psp->adev))
870 cmd = acquire_psp_cmd_buf(psp);
872 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
874 ret = psp_cmd_submit_buf(psp, NULL, cmd,
875 psp->fence_buf_mc_addr);
878 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
879 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
882 release_psp_cmd_buf(psp);
887 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
889 struct psp_context *psp = &adev->psp;
890 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_GET;
901 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
904 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
907 release_psp_cmd_buf(psp);
912 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
915 struct psp_context *psp = &adev->psp;
916 struct psp_gfx_cmd_resp *cmd;
918 if (amdgpu_sriov_vf(adev))
921 cmd = acquire_psp_cmd_buf(psp);
923 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
924 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
925 cmd->cmd.boot_cfg.boot_config = boot_cfg;
926 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
928 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
930 release_psp_cmd_buf(psp);
935 static int psp_rl_load(struct amdgpu_device *adev)
938 struct psp_context *psp = &adev->psp;
939 struct psp_gfx_cmd_resp *cmd;
941 if (!is_psp_fw_valid(psp->rl))
944 cmd = acquire_psp_cmd_buf(psp);
946 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
947 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
949 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
950 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
951 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
952 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
953 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
955 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
957 release_psp_cmd_buf(psp);
962 static int psp_asd_initialize(struct psp_context *psp)
966 /* If PSP version doesn't match ASD version, asd loading will be failed.
967 * add workaround to bypass it for sriov now.
968 * TODO: add version check to make it common
970 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
973 psp->asd_context.mem_context.shared_mc_addr = 0;
974 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
975 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
977 ret = psp_ta_load(psp, &psp->asd_context);
979 psp->asd_context.initialized = true;
984 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
987 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
988 cmd->cmd.cmd_unload_ta.session_id = session_id;
991 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
994 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
996 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
998 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1000 context->resp_status = cmd->resp.status;
1002 release_psp_cmd_buf(psp);
1007 static int psp_asd_terminate(struct psp_context *psp)
1011 if (amdgpu_sriov_vf(psp->adev))
1014 if (!psp->asd_context.initialized)
1017 ret = psp_ta_unload(psp, &psp->asd_context);
1019 psp->asd_context.initialized = false;
1024 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1025 uint32_t id, uint32_t value)
1027 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1028 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1029 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1032 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1035 struct psp_gfx_cmd_resp *cmd;
1038 if (reg >= PSP_REG_LAST)
1041 cmd = acquire_psp_cmd_buf(psp);
1043 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1044 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1046 DRM_ERROR("PSP failed to program reg id %d", reg);
1048 release_psp_cmd_buf(psp);
1053 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1055 struct ta_context *context)
1057 cmd->cmd_id = context->ta_load_type;
1058 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1059 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1060 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1062 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1063 lower_32_bits(context->mem_context.shared_mc_addr);
1064 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1065 upper_32_bits(context->mem_context.shared_mc_addr);
1066 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1069 int psp_ta_init_shared_buf(struct psp_context *psp,
1070 struct ta_mem_context *mem_ctx)
1073 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1074 * physical) for ta to host memory
1076 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1077 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1078 AMDGPU_GEM_DOMAIN_GTT,
1079 &mem_ctx->shared_bo,
1080 &mem_ctx->shared_mc_addr,
1081 &mem_ctx->shared_buf);
1084 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1086 uint32_t session_id)
1088 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1089 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1090 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1093 int psp_ta_invoke(struct psp_context *psp,
1095 struct ta_context *context)
1098 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1100 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1102 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1103 psp->fence_buf_mc_addr);
1105 context->resp_status = cmd->resp.status;
1107 release_psp_cmd_buf(psp);
1112 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1115 struct psp_gfx_cmd_resp *cmd;
1117 cmd = acquire_psp_cmd_buf(psp);
1119 psp_copy_fw(psp, context->bin_desc.start_addr,
1120 context->bin_desc.size_bytes);
1122 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1124 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1125 psp->fence_buf_mc_addr);
1127 context->resp_status = cmd->resp.status;
1130 context->session_id = cmd->resp.session_id;
1133 release_psp_cmd_buf(psp);
1138 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1140 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1143 int psp_xgmi_terminate(struct psp_context *psp)
1146 struct amdgpu_device *adev = psp->adev;
1148 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1149 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1150 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1151 adev->gmc.xgmi.connected_to_cpu))
1154 if (!psp->xgmi_context.context.initialized)
1157 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1159 psp->xgmi_context.context.initialized = false;
1164 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1166 struct ta_xgmi_shared_memory *xgmi_cmd;
1170 !psp->xgmi_context.context.bin_desc.size_bytes ||
1171 !psp->xgmi_context.context.bin_desc.start_addr)
1177 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1178 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1180 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1181 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1187 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1189 psp->xgmi_context.context.initialized = true;
1194 /* Initialize XGMI session */
1195 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1196 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1197 xgmi_cmd->flag_extend_link_record = set_extended_data;
1198 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1200 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1205 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1207 struct ta_xgmi_shared_memory *xgmi_cmd;
1210 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1211 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1213 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1215 /* Invoke xgmi ta to get hive id */
1216 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1220 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1225 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1227 struct ta_xgmi_shared_memory *xgmi_cmd;
1230 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1231 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1233 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1235 /* Invoke xgmi ta to get the node id */
1236 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1240 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1245 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1247 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1248 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1252 * Chips that support extended topology information require the driver to
1253 * reflect topology information in the opposite direction. This is
1254 * because the TA has already exceeded its link record limit and if the
1255 * TA holds bi-directional information, the driver would have to do
1256 * multiple fetches instead of just two.
1258 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1259 struct psp_xgmi_node_info node_info)
1261 struct amdgpu_device *mirror_adev;
1262 struct amdgpu_hive_info *hive;
1263 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1264 uint64_t dst_node_id = node_info.node_id;
1265 uint8_t dst_num_hops = node_info.num_hops;
1266 uint8_t dst_num_links = node_info.num_links;
1268 hive = amdgpu_get_xgmi_hive(psp->adev);
1269 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1270 struct psp_xgmi_topology_info *mirror_top_info;
1273 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1276 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1277 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1278 if (mirror_top_info->nodes[j].node_id != src_node_id)
1281 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1283 * prevent 0 num_links value re-reflection since reflection
1284 * criteria is based on num_hops (direct or indirect).
1288 mirror_top_info->nodes[j].num_links = dst_num_links;
1296 amdgpu_put_xgmi_hive(hive);
1299 int psp_xgmi_get_topology_info(struct psp_context *psp,
1301 struct psp_xgmi_topology_info *topology,
1302 bool get_extended_data)
1304 struct ta_xgmi_shared_memory *xgmi_cmd;
1305 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1306 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1310 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1313 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1314 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1315 xgmi_cmd->flag_extend_link_record = get_extended_data;
1317 /* Fill in the shared memory with topology information as input */
1318 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1319 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1320 topology_info_input->num_nodes = number_devices;
1322 for (i = 0; i < topology_info_input->num_nodes; i++) {
1323 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1324 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1325 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1326 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1329 /* Invoke xgmi ta to get the topology information */
1330 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1334 /* Read the output topology information from the shared memory */
1335 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1336 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1337 for (i = 0; i < topology->num_nodes; i++) {
1338 /* extended data will either be 0 or equal to non-extended data */
1339 if (topology_info_output->nodes[i].num_hops)
1340 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1342 /* non-extended data gets everything here so no need to update */
1343 if (!get_extended_data) {
1344 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1345 topology->nodes[i].is_sharing_enabled =
1346 topology_info_output->nodes[i].is_sharing_enabled;
1347 topology->nodes[i].sdma_engine =
1348 topology_info_output->nodes[i].sdma_engine;
1353 /* Invoke xgmi ta again to get the link information */
1354 if (psp_xgmi_peer_link_info_supported(psp)) {
1355 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1357 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1359 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1364 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1365 for (i = 0; i < topology->num_nodes; i++) {
1366 /* accumulate num_links on extended data */
1367 topology->nodes[i].num_links = get_extended_data ?
1368 topology->nodes[i].num_links +
1369 link_info_output->nodes[i].num_links :
1370 link_info_output->nodes[i].num_links;
1372 /* reflect the topology information for bi-directionality */
1373 if (psp->xgmi_context.supports_extended_data &&
1374 get_extended_data && topology->nodes[i].num_hops)
1375 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1382 int psp_xgmi_set_topology_info(struct psp_context *psp,
1384 struct psp_xgmi_topology_info *topology)
1386 struct ta_xgmi_shared_memory *xgmi_cmd;
1387 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1390 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1393 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1394 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1396 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1397 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1398 topology_info_input->num_nodes = number_devices;
1400 for (i = 0; i < topology_info_input->num_nodes; i++) {
1401 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1402 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1403 topology_info_input->nodes[i].is_sharing_enabled = 1;
1404 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1407 /* Invoke xgmi ta to set topology information */
1408 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1412 static void psp_ras_ta_check_status(struct psp_context *psp)
1414 struct ta_ras_shared_memory *ras_cmd =
1415 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1417 switch (ras_cmd->ras_status) {
1418 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1419 dev_warn(psp->adev->dev,
1420 "RAS WARNING: cmd failed due to unsupported ip\n");
1422 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1423 dev_warn(psp->adev->dev,
1424 "RAS WARNING: cmd failed due to unsupported error injection\n");
1426 case TA_RAS_STATUS__SUCCESS:
1428 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1429 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1430 dev_warn(psp->adev->dev,
1431 "RAS WARNING: Inject error to critical region is not allowed\n");
1434 dev_warn(psp->adev->dev,
1435 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1440 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1442 struct ta_ras_shared_memory *ras_cmd;
1445 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1448 * TODO: bypass the loading in sriov for now
1450 if (amdgpu_sriov_vf(psp->adev))
1453 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1455 if (amdgpu_ras_intr_triggered())
1458 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1460 DRM_WARN("RAS: Unsupported Interface");
1465 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1466 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1468 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1470 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1471 dev_warn(psp->adev->dev,
1472 "RAS internal register access blocked\n");
1474 psp_ras_ta_check_status(psp);
1480 int psp_ras_enable_features(struct psp_context *psp,
1481 union ta_ras_cmd_input *info, bool enable)
1483 struct ta_ras_shared_memory *ras_cmd;
1486 if (!psp->ras_context.context.initialized)
1489 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1490 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1493 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1495 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1497 ras_cmd->ras_in_message = *info;
1499 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1506 int psp_ras_terminate(struct psp_context *psp)
1511 * TODO: bypass the terminate in sriov for now
1513 if (amdgpu_sriov_vf(psp->adev))
1516 if (!psp->ras_context.context.initialized)
1519 ret = psp_ta_unload(psp, &psp->ras_context.context);
1521 psp->ras_context.context.initialized = false;
1526 int psp_ras_initialize(struct psp_context *psp)
1529 uint32_t boot_cfg = 0xFF;
1530 struct amdgpu_device *adev = psp->adev;
1531 struct ta_ras_shared_memory *ras_cmd;
1534 * TODO: bypass the initialize in sriov for now
1536 if (amdgpu_sriov_vf(adev))
1539 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1540 !adev->psp.ras_context.context.bin_desc.start_addr) {
1541 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1545 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1546 /* query GECC enablement status from boot config
1547 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1549 ret = psp_boot_config_get(adev, &boot_cfg);
1551 dev_warn(adev->dev, "PSP get boot config failed\n");
1553 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1555 dev_info(adev->dev, "GECC is disabled\n");
1557 /* disable GECC in next boot cycle if ras is
1558 * disabled by module parameter amdgpu_ras_enable
1559 * and/or amdgpu_ras_mask, or boot_config_get call
1562 ret = psp_boot_config_set(adev, 0);
1564 dev_warn(adev->dev, "PSP set boot config failed\n");
1566 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1567 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1570 if (1 == boot_cfg) {
1571 dev_info(adev->dev, "GECC is enabled\n");
1573 /* enable GECC in next boot cycle if it is disabled
1574 * in boot config, or force enable GECC if failed to
1575 * get boot configuration
1577 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1579 dev_warn(adev->dev, "PSP set boot config failed\n");
1581 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1586 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1587 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1589 if (!psp->ras_context.context.mem_context.shared_buf) {
1590 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1595 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1596 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1598 if (amdgpu_ras_is_poison_mode_supported(adev))
1599 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1600 if (!adev->gmc.xgmi.connected_to_cpu)
1601 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1603 ret = psp_ta_load(psp, &psp->ras_context.context);
1605 if (!ret && !ras_cmd->ras_status)
1606 psp->ras_context.context.initialized = true;
1608 if (ras_cmd->ras_status)
1609 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1611 /* fail to load RAS TA */
1612 psp->ras_context.context.initialized = false;
1618 int psp_ras_trigger_error(struct psp_context *psp,
1619 struct ta_ras_trigger_error_input *info)
1621 struct ta_ras_shared_memory *ras_cmd;
1624 if (!psp->ras_context.context.initialized)
1627 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1628 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1630 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1631 ras_cmd->ras_in_message.trigger_error = *info;
1633 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1637 /* If err_event_athub occurs error inject was successful, however
1638 return status from TA is no long reliable */
1639 if (amdgpu_ras_intr_triggered())
1642 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1644 else if (ras_cmd->ras_status)
1652 static int psp_hdcp_initialize(struct psp_context *psp)
1657 * TODO: bypass the initialize in sriov for now
1659 if (amdgpu_sriov_vf(psp->adev))
1662 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1663 !psp->hdcp_context.context.bin_desc.start_addr) {
1664 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1668 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1669 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1671 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1672 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1677 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1679 psp->hdcp_context.context.initialized = true;
1680 mutex_init(&psp->hdcp_context.mutex);
1686 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1689 * TODO: bypass the loading in sriov for now
1691 if (amdgpu_sriov_vf(psp->adev))
1694 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1697 static int psp_hdcp_terminate(struct psp_context *psp)
1702 * TODO: bypass the terminate in sriov for now
1704 if (amdgpu_sriov_vf(psp->adev))
1707 if (!psp->hdcp_context.context.initialized)
1710 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1712 psp->hdcp_context.context.initialized = false;
1719 static int psp_dtm_initialize(struct psp_context *psp)
1724 * TODO: bypass the initialize in sriov for now
1726 if (amdgpu_sriov_vf(psp->adev))
1729 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1730 !psp->dtm_context.context.bin_desc.start_addr) {
1731 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1735 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1736 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1738 if (!psp->dtm_context.context.mem_context.shared_buf) {
1739 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1744 ret = psp_ta_load(psp, &psp->dtm_context.context);
1746 psp->dtm_context.context.initialized = true;
1747 mutex_init(&psp->dtm_context.mutex);
1753 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1756 * TODO: bypass the loading in sriov for now
1758 if (amdgpu_sriov_vf(psp->adev))
1761 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1764 static int psp_dtm_terminate(struct psp_context *psp)
1769 * TODO: bypass the terminate in sriov for now
1771 if (amdgpu_sriov_vf(psp->adev))
1774 if (!psp->dtm_context.context.initialized)
1777 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1779 psp->dtm_context.context.initialized = false;
1786 static int psp_rap_initialize(struct psp_context *psp)
1789 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1792 * TODO: bypass the initialize in sriov for now
1794 if (amdgpu_sriov_vf(psp->adev))
1797 if (!psp->rap_context.context.bin_desc.size_bytes ||
1798 !psp->rap_context.context.bin_desc.start_addr) {
1799 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1803 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1804 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1806 if (!psp->rap_context.context.mem_context.shared_buf) {
1807 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1812 ret = psp_ta_load(psp, &psp->rap_context.context);
1814 psp->rap_context.context.initialized = true;
1815 mutex_init(&psp->rap_context.mutex);
1819 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1820 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1821 psp_rap_terminate(psp);
1822 /* free rap shared memory */
1823 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1825 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1834 static int psp_rap_terminate(struct psp_context *psp)
1838 if (!psp->rap_context.context.initialized)
1841 ret = psp_ta_unload(psp, &psp->rap_context.context);
1843 psp->rap_context.context.initialized = false;
1848 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1850 struct ta_rap_shared_memory *rap_cmd;
1853 if (!psp->rap_context.context.initialized)
1856 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1857 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1860 mutex_lock(&psp->rap_context.mutex);
1862 rap_cmd = (struct ta_rap_shared_memory *)
1863 psp->rap_context.context.mem_context.shared_buf;
1864 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1866 rap_cmd->cmd_id = ta_cmd_id;
1867 rap_cmd->validation_method_id = METHOD_A;
1869 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1874 *status = rap_cmd->rap_status;
1877 mutex_unlock(&psp->rap_context.mutex);
1883 /* securedisplay start */
1884 static int psp_securedisplay_initialize(struct psp_context *psp)
1887 struct ta_securedisplay_cmd *securedisplay_cmd;
1890 * TODO: bypass the initialize in sriov for now
1892 if (amdgpu_sriov_vf(psp->adev))
1895 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1896 !psp->securedisplay_context.context.bin_desc.start_addr) {
1897 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1901 psp->securedisplay_context.context.mem_context.shared_mem_size =
1902 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1903 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1905 if (!psp->securedisplay_context.context.initialized) {
1906 ret = psp_ta_init_shared_buf(psp,
1907 &psp->securedisplay_context.context.mem_context);
1912 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1914 psp->securedisplay_context.context.initialized = true;
1915 mutex_init(&psp->securedisplay_context.mutex);
1919 mutex_lock(&psp->securedisplay_context.mutex);
1921 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1922 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1924 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1926 mutex_unlock(&psp->securedisplay_context.mutex);
1929 psp_securedisplay_terminate(psp);
1930 /* free securedisplay shared memory */
1931 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1932 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1936 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1937 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1938 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1939 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1945 static int psp_securedisplay_terminate(struct psp_context *psp)
1950 * TODO:bypass the terminate in sriov for now
1952 if (amdgpu_sriov_vf(psp->adev))
1955 if (!psp->securedisplay_context.context.initialized)
1958 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1960 psp->securedisplay_context.context.initialized = false;
1965 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1969 if (!psp->securedisplay_context.context.initialized)
1972 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1973 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1976 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1980 /* SECUREDISPLAY end */
1982 static int psp_hw_start(struct psp_context *psp)
1984 struct amdgpu_device *adev = psp->adev;
1987 if (!amdgpu_sriov_vf(adev)) {
1988 if ((is_psp_fw_valid(psp->kdb)) &&
1989 (psp->funcs->bootloader_load_kdb != NULL)) {
1990 ret = psp_bootloader_load_kdb(psp);
1992 DRM_ERROR("PSP load kdb failed!\n");
1997 if ((is_psp_fw_valid(psp->spl)) &&
1998 (psp->funcs->bootloader_load_spl != NULL)) {
1999 ret = psp_bootloader_load_spl(psp);
2001 DRM_ERROR("PSP load spl failed!\n");
2006 if ((is_psp_fw_valid(psp->sys)) &&
2007 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2008 ret = psp_bootloader_load_sysdrv(psp);
2010 DRM_ERROR("PSP load sys drv failed!\n");
2015 if ((is_psp_fw_valid(psp->soc_drv)) &&
2016 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2017 ret = psp_bootloader_load_soc_drv(psp);
2019 DRM_ERROR("PSP load soc drv failed!\n");
2024 if ((is_psp_fw_valid(psp->intf_drv)) &&
2025 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2026 ret = psp_bootloader_load_intf_drv(psp);
2028 DRM_ERROR("PSP load intf drv failed!\n");
2033 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2034 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2035 ret = psp_bootloader_load_dbg_drv(psp);
2037 DRM_ERROR("PSP load dbg drv failed!\n");
2042 if ((is_psp_fw_valid(psp->ras_drv)) &&
2043 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2044 ret = psp_bootloader_load_ras_drv(psp);
2046 DRM_ERROR("PSP load ras_drv failed!\n");
2051 if ((is_psp_fw_valid(psp->sos)) &&
2052 (psp->funcs->bootloader_load_sos != NULL)) {
2053 ret = psp_bootloader_load_sos(psp);
2055 DRM_ERROR("PSP load sos failed!\n");
2061 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2063 DRM_ERROR("PSP create ring failed!\n");
2067 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2070 ret = psp_tmr_init(psp);
2072 DRM_ERROR("PSP tmr init failed!\n");
2078 * For ASICs with DF Cstate management centralized
2079 * to PMFW, TMR setup should be performed after PMFW
2080 * loaded and before other non-psp firmware loaded.
2082 if (psp->pmfw_centralized_cstate_management) {
2083 ret = psp_load_smu_fw(psp);
2088 ret = psp_tmr_load(psp);
2090 DRM_ERROR("PSP load tmr failed!\n");
2097 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2098 enum psp_gfx_fw_type *type)
2100 switch (ucode->ucode_id) {
2101 case AMDGPU_UCODE_ID_CAP:
2102 *type = GFX_FW_TYPE_CAP;
2104 case AMDGPU_UCODE_ID_SDMA0:
2105 *type = GFX_FW_TYPE_SDMA0;
2107 case AMDGPU_UCODE_ID_SDMA1:
2108 *type = GFX_FW_TYPE_SDMA1;
2110 case AMDGPU_UCODE_ID_SDMA2:
2111 *type = GFX_FW_TYPE_SDMA2;
2113 case AMDGPU_UCODE_ID_SDMA3:
2114 *type = GFX_FW_TYPE_SDMA3;
2116 case AMDGPU_UCODE_ID_SDMA4:
2117 *type = GFX_FW_TYPE_SDMA4;
2119 case AMDGPU_UCODE_ID_SDMA5:
2120 *type = GFX_FW_TYPE_SDMA5;
2122 case AMDGPU_UCODE_ID_SDMA6:
2123 *type = GFX_FW_TYPE_SDMA6;
2125 case AMDGPU_UCODE_ID_SDMA7:
2126 *type = GFX_FW_TYPE_SDMA7;
2128 case AMDGPU_UCODE_ID_CP_MES:
2129 *type = GFX_FW_TYPE_CP_MES;
2131 case AMDGPU_UCODE_ID_CP_MES_DATA:
2132 *type = GFX_FW_TYPE_MES_STACK;
2134 case AMDGPU_UCODE_ID_CP_MES1:
2135 *type = GFX_FW_TYPE_CP_MES_KIQ;
2137 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2138 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2140 case AMDGPU_UCODE_ID_CP_CE:
2141 *type = GFX_FW_TYPE_CP_CE;
2143 case AMDGPU_UCODE_ID_CP_PFP:
2144 *type = GFX_FW_TYPE_CP_PFP;
2146 case AMDGPU_UCODE_ID_CP_ME:
2147 *type = GFX_FW_TYPE_CP_ME;
2149 case AMDGPU_UCODE_ID_CP_MEC1:
2150 *type = GFX_FW_TYPE_CP_MEC;
2152 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2153 *type = GFX_FW_TYPE_CP_MEC_ME1;
2155 case AMDGPU_UCODE_ID_CP_MEC2:
2156 *type = GFX_FW_TYPE_CP_MEC;
2158 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2159 *type = GFX_FW_TYPE_CP_MEC_ME2;
2161 case AMDGPU_UCODE_ID_RLC_P:
2162 *type = GFX_FW_TYPE_RLC_P;
2164 case AMDGPU_UCODE_ID_RLC_V:
2165 *type = GFX_FW_TYPE_RLC_V;
2167 case AMDGPU_UCODE_ID_RLC_G:
2168 *type = GFX_FW_TYPE_RLC_G;
2170 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2171 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2173 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2174 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2176 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2177 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2179 case AMDGPU_UCODE_ID_RLC_IRAM:
2180 *type = GFX_FW_TYPE_RLC_IRAM;
2182 case AMDGPU_UCODE_ID_RLC_DRAM:
2183 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2185 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2186 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2188 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2189 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2191 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2192 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2194 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2195 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2197 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2198 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2200 case AMDGPU_UCODE_ID_SMC:
2201 *type = GFX_FW_TYPE_SMU;
2203 case AMDGPU_UCODE_ID_PPTABLE:
2204 *type = GFX_FW_TYPE_PPTABLE;
2206 case AMDGPU_UCODE_ID_UVD:
2207 *type = GFX_FW_TYPE_UVD;
2209 case AMDGPU_UCODE_ID_UVD1:
2210 *type = GFX_FW_TYPE_UVD1;
2212 case AMDGPU_UCODE_ID_VCE:
2213 *type = GFX_FW_TYPE_VCE;
2215 case AMDGPU_UCODE_ID_VCN:
2216 *type = GFX_FW_TYPE_VCN;
2218 case AMDGPU_UCODE_ID_VCN1:
2219 *type = GFX_FW_TYPE_VCN1;
2221 case AMDGPU_UCODE_ID_DMCU_ERAM:
2222 *type = GFX_FW_TYPE_DMCU_ERAM;
2224 case AMDGPU_UCODE_ID_DMCU_INTV:
2225 *type = GFX_FW_TYPE_DMCU_ISR;
2227 case AMDGPU_UCODE_ID_VCN0_RAM:
2228 *type = GFX_FW_TYPE_VCN0_RAM;
2230 case AMDGPU_UCODE_ID_VCN1_RAM:
2231 *type = GFX_FW_TYPE_VCN1_RAM;
2233 case AMDGPU_UCODE_ID_DMCUB:
2234 *type = GFX_FW_TYPE_DMUB;
2236 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2237 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2239 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2240 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2242 case AMDGPU_UCODE_ID_IMU_I:
2243 *type = GFX_FW_TYPE_IMU_I;
2245 case AMDGPU_UCODE_ID_IMU_D:
2246 *type = GFX_FW_TYPE_IMU_D;
2248 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2249 *type = GFX_FW_TYPE_RS64_PFP;
2251 case AMDGPU_UCODE_ID_CP_RS64_ME:
2252 *type = GFX_FW_TYPE_RS64_ME;
2254 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2255 *type = GFX_FW_TYPE_RS64_MEC;
2257 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2258 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2260 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2261 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2263 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2264 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2266 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2267 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2269 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2270 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2272 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2273 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2275 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2276 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2278 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2279 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2281 case AMDGPU_UCODE_ID_MAXIMUM:
2289 static void psp_print_fw_hdr(struct psp_context *psp,
2290 struct amdgpu_firmware_info *ucode)
2292 struct amdgpu_device *adev = psp->adev;
2293 struct common_firmware_header *hdr;
2295 switch (ucode->ucode_id) {
2296 case AMDGPU_UCODE_ID_SDMA0:
2297 case AMDGPU_UCODE_ID_SDMA1:
2298 case AMDGPU_UCODE_ID_SDMA2:
2299 case AMDGPU_UCODE_ID_SDMA3:
2300 case AMDGPU_UCODE_ID_SDMA4:
2301 case AMDGPU_UCODE_ID_SDMA5:
2302 case AMDGPU_UCODE_ID_SDMA6:
2303 case AMDGPU_UCODE_ID_SDMA7:
2304 hdr = (struct common_firmware_header *)
2305 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2306 amdgpu_ucode_print_sdma_hdr(hdr);
2308 case AMDGPU_UCODE_ID_CP_CE:
2309 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2310 amdgpu_ucode_print_gfx_hdr(hdr);
2312 case AMDGPU_UCODE_ID_CP_PFP:
2313 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2314 amdgpu_ucode_print_gfx_hdr(hdr);
2316 case AMDGPU_UCODE_ID_CP_ME:
2317 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2318 amdgpu_ucode_print_gfx_hdr(hdr);
2320 case AMDGPU_UCODE_ID_CP_MEC1:
2321 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2322 amdgpu_ucode_print_gfx_hdr(hdr);
2324 case AMDGPU_UCODE_ID_RLC_G:
2325 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2326 amdgpu_ucode_print_rlc_hdr(hdr);
2328 case AMDGPU_UCODE_ID_SMC:
2329 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2330 amdgpu_ucode_print_smc_hdr(hdr);
2337 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2338 struct psp_gfx_cmd_resp *cmd)
2341 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2343 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2344 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2345 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2346 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2348 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2350 DRM_ERROR("Unknown firmware type\n");
2355 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2356 struct amdgpu_firmware_info *ucode)
2359 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2361 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2363 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2364 psp->fence_buf_mc_addr);
2367 release_psp_cmd_buf(psp);
2372 static int psp_load_smu_fw(struct psp_context *psp)
2375 struct amdgpu_device *adev = psp->adev;
2376 struct amdgpu_firmware_info *ucode =
2377 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2378 struct amdgpu_ras *ras = psp->ras_context.ras;
2381 * Skip SMU FW reloading in case of using BACO for runpm only,
2382 * as SMU is always alive.
2384 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2387 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2390 if ((amdgpu_in_reset(adev) &&
2391 ras && adev->ras_enabled &&
2392 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2393 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2394 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2396 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2400 ret = psp_execute_non_psp_fw_load(psp, ucode);
2403 DRM_ERROR("PSP load smu failed!\n");
2408 static bool fw_load_skip_check(struct psp_context *psp,
2409 struct amdgpu_firmware_info *ucode)
2411 if (!ucode->fw || !ucode->ucode_size)
2414 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2415 (psp_smu_reload_quirk(psp) ||
2416 psp->autoload_supported ||
2417 psp->pmfw_centralized_cstate_management))
2420 if (amdgpu_sriov_vf(psp->adev) &&
2421 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2424 if (psp->autoload_supported &&
2425 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2426 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2427 /* skip mec JT when autoload is enabled */
2433 int psp_load_fw_list(struct psp_context *psp,
2434 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2437 struct amdgpu_firmware_info *ucode;
2439 for (i = 0; i < ucode_count; ++i) {
2440 ucode = ucode_list[i];
2441 psp_print_fw_hdr(psp, ucode);
2442 ret = psp_execute_non_psp_fw_load(psp, ucode);
2449 static int psp_load_non_psp_fw(struct psp_context *psp)
2452 struct amdgpu_firmware_info *ucode;
2453 struct amdgpu_device *adev = psp->adev;
2455 if (psp->autoload_supported &&
2456 !psp->pmfw_centralized_cstate_management) {
2457 ret = psp_load_smu_fw(psp);
2462 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2463 ucode = &adev->firmware.ucode[i];
2465 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2466 !fw_load_skip_check(psp, ucode)) {
2467 ret = psp_load_smu_fw(psp);
2473 if (fw_load_skip_check(psp, ucode))
2476 if (psp->autoload_supported &&
2477 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2478 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2479 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2480 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2481 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2482 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2483 /* PSP only receive one SDMA fw for sienna_cichlid,
2484 * as all four sdma fw are same */
2487 psp_print_fw_hdr(psp, ucode);
2489 ret = psp_execute_non_psp_fw_load(psp, ucode);
2493 /* Start rlc autoload after psp recieved all the gfx firmware */
2494 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2495 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2496 ret = psp_rlc_autoload_start(psp);
2498 DRM_ERROR("Failed to start rlc autoload\n");
2507 static int psp_load_fw(struct amdgpu_device *adev)
2510 struct psp_context *psp = &adev->psp;
2512 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2513 /* should not destroy ring, only stop */
2514 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2516 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2518 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2520 DRM_ERROR("PSP ring init failed!\n");
2525 ret = psp_hw_start(psp);
2529 ret = psp_load_non_psp_fw(psp);
2533 ret = psp_asd_initialize(psp);
2535 DRM_ERROR("PSP load asd failed!\n");
2539 ret = psp_rl_load(adev);
2541 DRM_ERROR("PSP load RL failed!\n");
2545 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2546 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2547 ret = psp_xgmi_initialize(psp, false, true);
2548 /* Warning the XGMI seesion initialize failure
2549 * Instead of stop driver initialization
2552 dev_err(psp->adev->dev,
2553 "XGMI: Failed to initialize XGMI session\n");
2558 ret = psp_ras_initialize(psp);
2560 dev_err(psp->adev->dev,
2561 "RAS: Failed to initialize RAS\n");
2563 ret = psp_hdcp_initialize(psp);
2565 dev_err(psp->adev->dev,
2566 "HDCP: Failed to initialize HDCP\n");
2568 ret = psp_dtm_initialize(psp);
2570 dev_err(psp->adev->dev,
2571 "DTM: Failed to initialize DTM\n");
2573 ret = psp_rap_initialize(psp);
2575 dev_err(psp->adev->dev,
2576 "RAP: Failed to initialize RAP\n");
2578 ret = psp_securedisplay_initialize(psp);
2580 dev_err(psp->adev->dev,
2581 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2587 psp_free_shared_bufs(psp);
2590 * all cleanup jobs (xgmi terminate, ras terminate,
2591 * ring destroy, cmd/fence/fw buffers destory,
2592 * psp->cmd destory) are delayed to psp_hw_fini
2594 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2598 static int psp_hw_init(void *handle)
2601 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2603 mutex_lock(&adev->firmware.mutex);
2605 * This sequence is just used on hw_init only once, no need on
2608 ret = amdgpu_ucode_init_bo(adev);
2612 ret = psp_load_fw(adev);
2614 DRM_ERROR("PSP firmware loading failed\n");
2618 mutex_unlock(&adev->firmware.mutex);
2622 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2623 mutex_unlock(&adev->firmware.mutex);
2627 static int psp_hw_fini(void *handle)
2629 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2630 struct psp_context *psp = &adev->psp;
2633 psp_ras_terminate(psp);
2634 psp_securedisplay_terminate(psp);
2635 psp_rap_terminate(psp);
2636 psp_dtm_terminate(psp);
2637 psp_hdcp_terminate(psp);
2639 if (adev->gmc.xgmi.num_physical_nodes > 1)
2640 psp_xgmi_terminate(psp);
2643 psp_asd_terminate(psp);
2644 psp_tmr_terminate(psp);
2646 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2648 psp_free_shared_bufs(psp);
2653 static int psp_suspend(void *handle)
2656 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2657 struct psp_context *psp = &adev->psp;
2659 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2660 psp->xgmi_context.context.initialized) {
2661 ret = psp_xgmi_terminate(psp);
2663 DRM_ERROR("Failed to terminate xgmi ta\n");
2669 ret = psp_ras_terminate(psp);
2671 DRM_ERROR("Failed to terminate ras ta\n");
2674 ret = psp_hdcp_terminate(psp);
2676 DRM_ERROR("Failed to terminate hdcp ta\n");
2679 ret = psp_dtm_terminate(psp);
2681 DRM_ERROR("Failed to terminate dtm ta\n");
2684 ret = psp_rap_terminate(psp);
2686 DRM_ERROR("Failed to terminate rap ta\n");
2689 ret = psp_securedisplay_terminate(psp);
2691 DRM_ERROR("Failed to terminate securedisplay ta\n");
2696 ret = psp_asd_terminate(psp);
2698 DRM_ERROR("Failed to terminate asd\n");
2702 ret = psp_tmr_terminate(psp);
2704 DRM_ERROR("Failed to terminate tmr\n");
2708 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2710 DRM_ERROR("PSP ring stop failed\n");
2717 static int psp_resume(void *handle)
2720 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2721 struct psp_context *psp = &adev->psp;
2723 DRM_INFO("PSP is resuming...\n");
2725 if (psp->mem_train_ctx.enable_mem_training) {
2726 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2728 DRM_ERROR("Failed to process memory training!\n");
2733 mutex_lock(&adev->firmware.mutex);
2735 ret = psp_hw_start(psp);
2739 ret = psp_load_non_psp_fw(psp);
2743 ret = psp_asd_initialize(psp);
2745 DRM_ERROR("PSP load asd failed!\n");
2749 ret = psp_rl_load(adev);
2751 dev_err(adev->dev, "PSP load RL failed!\n");
2755 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2756 ret = psp_xgmi_initialize(psp, false, true);
2757 /* Warning the XGMI seesion initialize failure
2758 * Instead of stop driver initialization
2761 dev_err(psp->adev->dev,
2762 "XGMI: Failed to initialize XGMI session\n");
2766 ret = psp_ras_initialize(psp);
2768 dev_err(psp->adev->dev,
2769 "RAS: Failed to initialize RAS\n");
2771 ret = psp_hdcp_initialize(psp);
2773 dev_err(psp->adev->dev,
2774 "HDCP: Failed to initialize HDCP\n");
2776 ret = psp_dtm_initialize(psp);
2778 dev_err(psp->adev->dev,
2779 "DTM: Failed to initialize DTM\n");
2781 ret = psp_rap_initialize(psp);
2783 dev_err(psp->adev->dev,
2784 "RAP: Failed to initialize RAP\n");
2786 ret = psp_securedisplay_initialize(psp);
2788 dev_err(psp->adev->dev,
2789 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2792 mutex_unlock(&adev->firmware.mutex);
2797 DRM_ERROR("PSP resume failed\n");
2798 mutex_unlock(&adev->firmware.mutex);
2802 int psp_gpu_reset(struct amdgpu_device *adev)
2806 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2809 mutex_lock(&adev->psp.mutex);
2810 ret = psp_mode1_reset(&adev->psp);
2811 mutex_unlock(&adev->psp.mutex);
2816 int psp_rlc_autoload_start(struct psp_context *psp)
2819 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2821 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2823 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2824 psp->fence_buf_mc_addr);
2826 release_psp_cmd_buf(psp);
2831 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2832 uint64_t cmd_gpu_addr, int cmd_size)
2834 struct amdgpu_firmware_info ucode = {0};
2836 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2837 AMDGPU_UCODE_ID_VCN0_RAM;
2838 ucode.mc_addr = cmd_gpu_addr;
2839 ucode.ucode_size = cmd_size;
2841 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2844 int psp_ring_cmd_submit(struct psp_context *psp,
2845 uint64_t cmd_buf_mc_addr,
2846 uint64_t fence_mc_addr,
2849 unsigned int psp_write_ptr_reg = 0;
2850 struct psp_gfx_rb_frame *write_frame;
2851 struct psp_ring *ring = &psp->km_ring;
2852 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2853 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2854 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2855 struct amdgpu_device *adev = psp->adev;
2856 uint32_t ring_size_dw = ring->ring_size / 4;
2857 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2859 /* KM (GPCOM) prepare write pointer */
2860 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2862 /* Update KM RB frame pointer to new frame */
2863 /* write_frame ptr increments by size of rb_frame in bytes */
2864 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2865 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2866 write_frame = ring_buffer_start;
2868 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2869 /* Check invalid write_frame ptr address */
2870 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2871 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2872 ring_buffer_start, ring_buffer_end, write_frame);
2873 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2877 /* Initialize KM RB frame */
2878 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2880 /* Update KM RB frame */
2881 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2882 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2883 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2884 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2885 write_frame->fence_value = index;
2886 amdgpu_device_flush_hdp(adev, NULL);
2888 /* Update the write Pointer in DWORDs */
2889 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2890 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2894 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2896 struct amdgpu_device *adev = psp->adev;
2897 char fw_name[PSP_FW_NAME_LEN];
2898 const struct psp_firmware_header_v1_0 *asd_hdr;
2901 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2902 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2906 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2907 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2908 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2909 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2910 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2911 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2914 amdgpu_ucode_release(&adev->psp.asd_fw);
2918 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
2920 struct amdgpu_device *adev = psp->adev;
2921 char fw_name[PSP_FW_NAME_LEN];
2922 const struct psp_firmware_header_v1_0 *toc_hdr;
2925 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2926 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
2930 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2931 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2932 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2933 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2934 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2935 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2938 amdgpu_ucode_release(&adev->psp.toc_fw);
2942 static int parse_sos_bin_descriptor(struct psp_context *psp,
2943 const struct psp_fw_bin_desc *desc,
2944 const struct psp_firmware_header_v2_0 *sos_hdr)
2946 uint8_t *ucode_start_addr = NULL;
2948 if (!psp || !desc || !sos_hdr)
2951 ucode_start_addr = (uint8_t *)sos_hdr +
2952 le32_to_cpu(desc->offset_bytes) +
2953 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2955 switch (desc->fw_type) {
2956 case PSP_FW_TYPE_PSP_SOS:
2957 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
2958 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
2959 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
2960 psp->sos.start_addr = ucode_start_addr;
2962 case PSP_FW_TYPE_PSP_SYS_DRV:
2963 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
2964 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
2965 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
2966 psp->sys.start_addr = ucode_start_addr;
2968 case PSP_FW_TYPE_PSP_KDB:
2969 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
2970 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
2971 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
2972 psp->kdb.start_addr = ucode_start_addr;
2974 case PSP_FW_TYPE_PSP_TOC:
2975 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
2976 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
2977 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
2978 psp->toc.start_addr = ucode_start_addr;
2980 case PSP_FW_TYPE_PSP_SPL:
2981 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
2982 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
2983 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
2984 psp->spl.start_addr = ucode_start_addr;
2986 case PSP_FW_TYPE_PSP_RL:
2987 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
2988 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
2989 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
2990 psp->rl.start_addr = ucode_start_addr;
2992 case PSP_FW_TYPE_PSP_SOC_DRV:
2993 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
2994 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
2995 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2996 psp->soc_drv.start_addr = ucode_start_addr;
2998 case PSP_FW_TYPE_PSP_INTF_DRV:
2999 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3000 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3001 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3002 psp->intf_drv.start_addr = ucode_start_addr;
3004 case PSP_FW_TYPE_PSP_DBG_DRV:
3005 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3006 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3007 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3008 psp->dbg_drv.start_addr = ucode_start_addr;
3010 case PSP_FW_TYPE_PSP_RAS_DRV:
3011 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3012 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3013 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3014 psp->ras_drv.start_addr = ucode_start_addr;
3017 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3024 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3026 const struct psp_firmware_header_v1_0 *sos_hdr;
3027 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3028 uint8_t *ucode_array_start_addr;
3030 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3031 ucode_array_start_addr = (uint8_t *)sos_hdr +
3032 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3034 if (adev->gmc.xgmi.connected_to_cpu ||
3035 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3036 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3037 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3039 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3040 adev->psp.sys.start_addr = ucode_array_start_addr;
3042 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3043 adev->psp.sos.start_addr = ucode_array_start_addr +
3044 le32_to_cpu(sos_hdr->sos.offset_bytes);
3046 /* Load alternate PSP SOS FW */
3047 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3049 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3050 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3052 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3053 adev->psp.sys.start_addr = ucode_array_start_addr +
3054 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3056 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3057 adev->psp.sos.start_addr = ucode_array_start_addr +
3058 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3061 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3062 dev_warn(adev->dev, "PSP SOS FW not available");
3069 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3071 struct amdgpu_device *adev = psp->adev;
3072 char fw_name[PSP_FW_NAME_LEN];
3073 const struct psp_firmware_header_v1_0 *sos_hdr;
3074 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3075 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3076 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3077 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3079 uint8_t *ucode_array_start_addr;
3082 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3083 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3087 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3088 ucode_array_start_addr = (uint8_t *)sos_hdr +
3089 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3090 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3092 switch (sos_hdr->header.header_version_major) {
3094 err = psp_init_sos_base_fw(adev);
3098 if (sos_hdr->header.header_version_minor == 1) {
3099 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3100 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3101 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3102 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3103 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3104 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3105 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3107 if (sos_hdr->header.header_version_minor == 2) {
3108 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3109 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3110 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3111 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3113 if (sos_hdr->header.header_version_minor == 3) {
3114 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3115 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3116 adev->psp.toc.start_addr = ucode_array_start_addr +
3117 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3118 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3119 adev->psp.kdb.start_addr = ucode_array_start_addr +
3120 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3121 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3122 adev->psp.spl.start_addr = ucode_array_start_addr +
3123 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3124 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3125 adev->psp.rl.start_addr = ucode_array_start_addr +
3126 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3130 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3132 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3133 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3138 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3139 err = parse_sos_bin_descriptor(psp,
3140 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3148 "unsupported psp sos firmware\n");
3155 amdgpu_ucode_release(&adev->psp.sos_fw);
3160 static int parse_ta_bin_descriptor(struct psp_context *psp,
3161 const struct psp_fw_bin_desc *desc,
3162 const struct ta_firmware_header_v2_0 *ta_hdr)
3164 uint8_t *ucode_start_addr = NULL;
3166 if (!psp || !desc || !ta_hdr)
3169 ucode_start_addr = (uint8_t *)ta_hdr +
3170 le32_to_cpu(desc->offset_bytes) +
3171 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3173 switch (desc->fw_type) {
3174 case TA_FW_TYPE_PSP_ASD:
3175 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3176 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3177 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3178 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3180 case TA_FW_TYPE_PSP_XGMI:
3181 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3182 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3183 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3185 case TA_FW_TYPE_PSP_RAS:
3186 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3187 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3188 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3190 case TA_FW_TYPE_PSP_HDCP:
3191 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3192 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3193 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3195 case TA_FW_TYPE_PSP_DTM:
3196 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3197 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3198 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3200 case TA_FW_TYPE_PSP_RAP:
3201 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3202 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3203 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3205 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3206 psp->securedisplay_context.context.bin_desc.fw_version =
3207 le32_to_cpu(desc->fw_version);
3208 psp->securedisplay_context.context.bin_desc.size_bytes =
3209 le32_to_cpu(desc->size_bytes);
3210 psp->securedisplay_context.context.bin_desc.start_addr =
3214 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3221 static int parse_ta_v1_microcode(struct psp_context *psp)
3223 const struct ta_firmware_header_v1_0 *ta_hdr;
3224 struct amdgpu_device *adev = psp->adev;
3226 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3228 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3231 adev->psp.xgmi_context.context.bin_desc.fw_version =
3232 le32_to_cpu(ta_hdr->xgmi.fw_version);
3233 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3234 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3235 adev->psp.xgmi_context.context.bin_desc.start_addr =
3237 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3239 adev->psp.ras_context.context.bin_desc.fw_version =
3240 le32_to_cpu(ta_hdr->ras.fw_version);
3241 adev->psp.ras_context.context.bin_desc.size_bytes =
3242 le32_to_cpu(ta_hdr->ras.size_bytes);
3243 adev->psp.ras_context.context.bin_desc.start_addr =
3244 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3245 le32_to_cpu(ta_hdr->ras.offset_bytes);
3247 adev->psp.hdcp_context.context.bin_desc.fw_version =
3248 le32_to_cpu(ta_hdr->hdcp.fw_version);
3249 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3250 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3251 adev->psp.hdcp_context.context.bin_desc.start_addr =
3253 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3255 adev->psp.dtm_context.context.bin_desc.fw_version =
3256 le32_to_cpu(ta_hdr->dtm.fw_version);
3257 adev->psp.dtm_context.context.bin_desc.size_bytes =
3258 le32_to_cpu(ta_hdr->dtm.size_bytes);
3259 adev->psp.dtm_context.context.bin_desc.start_addr =
3260 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3261 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3263 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3264 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3265 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3266 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3267 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3268 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3269 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3271 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3276 static int parse_ta_v2_microcode(struct psp_context *psp)
3278 const struct ta_firmware_header_v2_0 *ta_hdr;
3279 struct amdgpu_device *adev = psp->adev;
3283 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3285 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3288 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3289 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3293 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3294 err = parse_ta_bin_descriptor(psp,
3295 &ta_hdr->ta_fw_bin[ta_index],
3304 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3306 const struct common_firmware_header *hdr;
3307 struct amdgpu_device *adev = psp->adev;
3308 char fw_name[PSP_FW_NAME_LEN];
3311 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3312 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3316 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3317 switch (le16_to_cpu(hdr->header_version_major)) {
3319 err = parse_ta_v1_microcode(psp);
3322 err = parse_ta_v2_microcode(psp);
3325 dev_err(adev->dev, "unsupported TA header version\n");
3330 amdgpu_ucode_release(&adev->psp.ta_fw);
3335 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3337 struct amdgpu_device *adev = psp->adev;
3338 char fw_name[PSP_FW_NAME_LEN];
3339 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3340 struct amdgpu_firmware_info *info = NULL;
3343 if (!amdgpu_sriov_vf(adev)) {
3344 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3348 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3349 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3351 if (err == -ENODEV) {
3352 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3356 dev_err(adev->dev, "fail to initialize cap microcode\n");
3359 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3360 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3361 info->fw = adev->psp.cap_fw;
3362 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3363 adev->psp.cap_fw->data;
3364 adev->firmware.fw_size += ALIGN(
3365 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3366 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3367 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3368 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3373 amdgpu_ucode_release(&adev->psp.cap_fw);
3377 static int psp_set_clockgating_state(void *handle,
3378 enum amd_clockgating_state state)
3383 static int psp_set_powergating_state(void *handle,
3384 enum amd_powergating_state state)
3389 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3390 struct device_attribute *attr,
3393 struct drm_device *ddev = dev_get_drvdata(dev);
3394 struct amdgpu_device *adev = drm_to_adev(ddev);
3398 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3399 DRM_INFO("PSP block is not ready yet.");
3403 mutex_lock(&adev->psp.mutex);
3404 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3405 mutex_unlock(&adev->psp.mutex);
3408 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3412 return sysfs_emit(buf, "%x\n", fw_ver);
3415 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3416 struct device_attribute *attr,
3420 struct drm_device *ddev = dev_get_drvdata(dev);
3421 struct amdgpu_device *adev = drm_to_adev(ddev);
3424 const struct firmware *usbc_pd_fw;
3425 struct amdgpu_bo *fw_buf_bo = NULL;
3426 uint64_t fw_pri_mc_addr;
3427 void *fw_pri_cpu_addr;
3429 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3430 DRM_INFO("PSP block is not ready yet.");
3434 if (!drm_dev_enter(ddev, &idx))
3437 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3438 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3442 /* LFB address which is aligned to 1MB boundary per PSP request */
3443 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3444 AMDGPU_GEM_DOMAIN_VRAM |
3445 AMDGPU_GEM_DOMAIN_GTT,
3446 &fw_buf_bo, &fw_pri_mc_addr,
3451 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3453 mutex_lock(&adev->psp.mutex);
3454 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3455 mutex_unlock(&adev->psp.mutex);
3457 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3460 release_firmware(usbc_pd_fw);
3463 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3471 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3475 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3478 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3479 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3484 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3485 psp_usbc_pd_fw_sysfs_read,
3486 psp_usbc_pd_fw_sysfs_write);
3488 int is_psp_fw_valid(struct psp_bin_desc bin)
3490 return bin.size_bytes;
3493 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3494 struct bin_attribute *bin_attr,
3495 char *buffer, loff_t pos, size_t count)
3497 struct device *dev = kobj_to_dev(kobj);
3498 struct drm_device *ddev = dev_get_drvdata(dev);
3499 struct amdgpu_device *adev = drm_to_adev(ddev);
3501 adev->psp.vbflash_done = false;
3503 /* Safeguard against memory drain */
3504 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3505 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3506 kvfree(adev->psp.vbflash_tmp_buf);
3507 adev->psp.vbflash_tmp_buf = NULL;
3508 adev->psp.vbflash_image_size = 0;
3512 /* TODO Just allocate max for now and optimize to realloc later if needed */
3513 if (!adev->psp.vbflash_tmp_buf) {
3514 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3515 if (!adev->psp.vbflash_tmp_buf)
3519 mutex_lock(&adev->psp.mutex);
3520 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3521 adev->psp.vbflash_image_size += count;
3522 mutex_unlock(&adev->psp.mutex);
3524 dev_info(adev->dev, "VBIOS flash write PSP done");
3529 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3530 struct bin_attribute *bin_attr, char *buffer,
3531 loff_t pos, size_t count)
3533 struct device *dev = kobj_to_dev(kobj);
3534 struct drm_device *ddev = dev_get_drvdata(dev);
3535 struct amdgpu_device *adev = drm_to_adev(ddev);
3536 struct amdgpu_bo *fw_buf_bo = NULL;
3537 uint64_t fw_pri_mc_addr;
3538 void *fw_pri_cpu_addr;
3541 dev_info(adev->dev, "VBIOS flash to PSP started");
3543 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3544 AMDGPU_GPU_PAGE_SIZE,
3545 AMDGPU_GEM_DOMAIN_VRAM,
3552 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3554 mutex_lock(&adev->psp.mutex);
3555 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3556 mutex_unlock(&adev->psp.mutex);
3558 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3561 kvfree(adev->psp.vbflash_tmp_buf);
3562 adev->psp.vbflash_tmp_buf = NULL;
3563 adev->psp.vbflash_image_size = 0;
3566 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3570 dev_info(adev->dev, "VBIOS flash to PSP done");
3574 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3575 struct device_attribute *attr,
3578 struct drm_device *ddev = dev_get_drvdata(dev);
3579 struct amdgpu_device *adev = drm_to_adev(ddev);
3580 uint32_t vbflash_status;
3582 vbflash_status = psp_vbflash_status(&adev->psp);
3583 if (!adev->psp.vbflash_done)
3585 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3588 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3591 static const struct bin_attribute psp_vbflash_bin_attr = {
3592 .attr = {.name = "psp_vbflash", .mode = 0664},
3594 .write = amdgpu_psp_vbflash_write,
3595 .read = amdgpu_psp_vbflash_read,
3598 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3600 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3603 struct psp_context *psp = &adev->psp;
3605 if (amdgpu_sriov_vf(adev))
3608 switch (adev->ip_versions[MP0_HWIP][0]) {
3609 case IP_VERSION(13, 0, 0):
3610 case IP_VERSION(13, 0, 7):
3613 psp_v13_0_set_psp_funcs(psp);
3615 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3617 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3618 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3620 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3627 const struct amd_ip_funcs psp_ip_funcs = {
3629 .early_init = psp_early_init,
3631 .sw_init = psp_sw_init,
3632 .sw_fini = psp_sw_fini,
3633 .hw_init = psp_hw_init,
3634 .hw_fini = psp_hw_fini,
3635 .suspend = psp_suspend,
3636 .resume = psp_resume,
3638 .check_soft_reset = NULL,
3639 .wait_for_idle = NULL,
3641 .set_clockgating_state = psp_set_clockgating_state,
3642 .set_powergating_state = psp_set_powergating_state,
3645 static int psp_sysfs_init(struct amdgpu_device *adev)
3647 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3650 DRM_ERROR("Failed to create USBC PD FW control file!");
3655 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3657 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3658 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3661 static void psp_sysfs_fini(struct amdgpu_device *adev)
3663 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3666 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3668 .type = AMD_IP_BLOCK_TYPE_PSP,
3672 .funcs = &psp_ip_funcs,
3675 const struct amdgpu_ip_block_version psp_v10_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_ip_block =
3686 .type = AMD_IP_BLOCK_TYPE_PSP,
3690 .funcs = &psp_ip_funcs,
3693 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3694 .type = AMD_IP_BLOCK_TYPE_PSP,
3698 .funcs = &psp_ip_funcs,
3701 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3703 .type = AMD_IP_BLOCK_TYPE_PSP,
3707 .funcs = &psp_ip_funcs,
3710 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3711 .type = AMD_IP_BLOCK_TYPE_PSP,
3715 .funcs = &psp_ip_funcs,
3718 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3719 .type = AMD_IP_BLOCK_TYPE_PSP,
3723 .funcs = &psp_ip_funcs,