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_load_smu_fw(struct psp_context *psp);
49 static int psp_rap_terminate(struct psp_context *psp);
50 static int psp_securedisplay_terminate(struct psp_context *psp);
52 static int psp_ring_init(struct psp_context *psp,
53 enum psp_ring_type ring_type)
56 struct psp_ring *ring;
57 struct amdgpu_device *adev = psp->adev;
61 ring->ring_type = ring_type;
63 /* allocate 4k Page of Local Frame Buffer memory for ring */
64 ring->ring_size = 0x1000;
65 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
66 AMDGPU_GEM_DOMAIN_VRAM |
67 AMDGPU_GEM_DOMAIN_GTT,
69 &ring->ring_mem_mc_addr,
70 (void **)&ring->ring_mem);
80 * Due to DF Cstate management centralized to PMFW, the firmware
81 * loading sequence will be updated as below:
87 * - Load other non-psp fw
89 * - Load XGMI/RAS/HDCP/DTM TA if any
91 * This new sequence is required for
92 * - Arcturus and onwards
94 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
96 struct amdgpu_device *adev = psp->adev;
98 if (amdgpu_sriov_vf(adev)) {
99 psp->pmfw_centralized_cstate_management = false;
103 switch (adev->ip_versions[MP0_HWIP][0]) {
104 case IP_VERSION(11, 0, 0):
105 case IP_VERSION(11, 0, 4):
106 case IP_VERSION(11, 0, 5):
107 case IP_VERSION(11, 0, 7):
108 case IP_VERSION(11, 0, 9):
109 case IP_VERSION(11, 0, 11):
110 case IP_VERSION(11, 0, 12):
111 case IP_VERSION(11, 0, 13):
112 case IP_VERSION(13, 0, 0):
113 case IP_VERSION(13, 0, 2):
114 case IP_VERSION(13, 0, 7):
115 psp->pmfw_centralized_cstate_management = true;
118 psp->pmfw_centralized_cstate_management = false;
123 static int psp_init_sriov_microcode(struct psp_context *psp)
125 struct amdgpu_device *adev = psp->adev;
126 char ucode_prefix[30];
129 amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
131 switch (adev->ip_versions[MP0_HWIP][0]) {
132 case IP_VERSION(9, 0, 0):
133 case IP_VERSION(11, 0, 7):
134 case IP_VERSION(11, 0, 9):
135 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
136 ret = psp_init_cap_microcode(psp, ucode_prefix);
138 case IP_VERSION(13, 0, 2):
139 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
140 ret = psp_init_cap_microcode(psp, ucode_prefix);
141 ret &= psp_init_ta_microcode(psp, ucode_prefix);
143 case IP_VERSION(13, 0, 0):
144 adev->virt.autoload_ucode_id = 0;
146 case IP_VERSION(13, 0, 6):
147 ret = psp_init_cap_microcode(psp, ucode_prefix);
149 case IP_VERSION(13, 0, 10):
150 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
151 ret = psp_init_cap_microcode(psp, ucode_prefix);
159 static int psp_early_init(void *handle)
161 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
162 struct psp_context *psp = &adev->psp;
164 switch (adev->ip_versions[MP0_HWIP][0]) {
165 case IP_VERSION(9, 0, 0):
166 psp_v3_1_set_psp_funcs(psp);
167 psp->autoload_supported = false;
169 case IP_VERSION(10, 0, 0):
170 case IP_VERSION(10, 0, 1):
171 psp_v10_0_set_psp_funcs(psp);
172 psp->autoload_supported = false;
174 case IP_VERSION(11, 0, 2):
175 case IP_VERSION(11, 0, 4):
176 psp_v11_0_set_psp_funcs(psp);
177 psp->autoload_supported = false;
179 case IP_VERSION(11, 0, 0):
180 case IP_VERSION(11, 0, 7):
181 adev->psp.sup_pd_fw_up = !amdgpu_sriov_vf(adev);
183 case IP_VERSION(11, 0, 5):
184 case IP_VERSION(11, 0, 9):
185 case IP_VERSION(11, 0, 11):
186 case IP_VERSION(11, 5, 0):
187 case IP_VERSION(11, 0, 12):
188 case IP_VERSION(11, 0, 13):
189 psp_v11_0_set_psp_funcs(psp);
190 psp->autoload_supported = true;
192 case IP_VERSION(11, 0, 3):
193 case IP_VERSION(12, 0, 1):
194 psp_v12_0_set_psp_funcs(psp);
196 case IP_VERSION(13, 0, 2):
197 case IP_VERSION(13, 0, 6):
198 psp_v13_0_set_psp_funcs(psp);
200 case IP_VERSION(13, 0, 1):
201 case IP_VERSION(13, 0, 3):
202 case IP_VERSION(13, 0, 5):
203 case IP_VERSION(13, 0, 8):
204 case IP_VERSION(13, 0, 11):
205 case IP_VERSION(14, 0, 0):
206 psp_v13_0_set_psp_funcs(psp);
207 psp->autoload_supported = true;
209 case IP_VERSION(11, 0, 8):
210 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
211 psp_v11_0_8_set_psp_funcs(psp);
212 psp->autoload_supported = false;
215 case IP_VERSION(13, 0, 0):
216 case IP_VERSION(13, 0, 7):
217 case IP_VERSION(13, 0, 10):
218 psp_v13_0_set_psp_funcs(psp);
219 psp->autoload_supported = true;
220 adev->psp.sup_ifwi_up = !amdgpu_sriov_vf(adev);
222 case IP_VERSION(13, 0, 4):
223 psp_v13_0_4_set_psp_funcs(psp);
224 psp->autoload_supported = true;
232 psp_check_pmfw_centralized_cstate_management(psp);
234 if (amdgpu_sriov_vf(adev))
235 return psp_init_sriov_microcode(psp);
237 return psp_init_microcode(psp);
240 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
242 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
243 &mem_ctx->shared_buf);
244 mem_ctx->shared_bo = NULL;
247 static void psp_free_shared_bufs(struct psp_context *psp)
252 /* free TMR memory buffer */
253 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
254 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
257 /* free xgmi shared memory */
258 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
260 /* free ras shared memory */
261 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
263 /* free hdcp shared memory */
264 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
266 /* free dtm shared memory */
267 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
269 /* free rap shared memory */
270 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
272 /* free securedisplay shared memory */
273 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
278 static void psp_memory_training_fini(struct psp_context *psp)
280 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
282 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
283 kfree(ctx->sys_cache);
284 ctx->sys_cache = NULL;
287 static int psp_memory_training_init(struct psp_context *psp)
290 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
292 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
293 DRM_DEBUG("memory training is not supported!\n");
297 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
298 if (ctx->sys_cache == NULL) {
299 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
304 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
305 ctx->train_data_size,
306 ctx->p2c_train_data_offset,
307 ctx->c2p_train_data_offset);
308 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
312 psp_memory_training_fini(psp);
317 * Helper funciton to query psp runtime database entry
319 * @adev: amdgpu_device pointer
320 * @entry_type: the type of psp runtime database entry
321 * @db_entry: runtime database entry pointer
323 * Return false if runtime database doesn't exit or entry is invalid
324 * or true if the specific database entry is found, and copy to @db_entry
326 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
327 enum psp_runtime_entry_type entry_type,
330 uint64_t db_header_pos, db_dir_pos;
331 struct psp_runtime_data_header db_header = {0};
332 struct psp_runtime_data_directory db_dir = {0};
336 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6))
339 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
340 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
342 /* read runtime db header from vram */
343 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
344 sizeof(struct psp_runtime_data_header), false);
346 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
347 /* runtime db doesn't exist, exit */
348 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
352 /* read runtime database entry from vram */
353 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
354 sizeof(struct psp_runtime_data_directory), false);
356 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
357 /* invalid db entry count, exit */
358 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
362 /* look up for requested entry type */
363 for (i = 0; i < db_dir.entry_count && !ret; i++) {
364 if (db_dir.entry_list[i].entry_type == entry_type) {
365 switch (entry_type) {
366 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
367 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
368 /* invalid db entry size */
369 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
372 /* read runtime database entry */
373 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
374 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
377 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
378 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
379 /* invalid db entry size */
380 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
383 /* read runtime database entry */
384 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
385 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
398 static int psp_sw_init(void *handle)
400 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
401 struct psp_context *psp = &adev->psp;
403 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
404 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
405 struct psp_runtime_scpm_entry scpm_entry;
407 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
409 DRM_ERROR("Failed to allocate memory to command buffer!\n");
413 adev->psp.xgmi_context.supports_extended_data =
414 !adev->gmc.xgmi.connected_to_cpu &&
415 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
417 memset(&scpm_entry, 0, sizeof(scpm_entry));
418 if ((psp_get_runtime_db_entry(adev,
419 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
421 (scpm_entry.scpm_status != SCPM_DISABLE)) {
422 adev->scpm_enabled = true;
423 adev->scpm_status = scpm_entry.scpm_status;
425 adev->scpm_enabled = false;
426 adev->scpm_status = SCPM_DISABLE;
429 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
431 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
432 if (psp_get_runtime_db_entry(adev,
433 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
435 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
436 if ((psp->boot_cfg_bitmask) &
437 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
438 /* If psp runtime database exists, then
439 * only enable two stage memory training
440 * when TWO_STAGE_DRAM_TRAINING bit is set
441 * in runtime database */
442 mem_training_ctx->enable_mem_training = true;
446 /* If psp runtime database doesn't exist or
447 * is invalid, force enable two stage memory
449 mem_training_ctx->enable_mem_training = true;
452 if (mem_training_ctx->enable_mem_training) {
453 ret = psp_memory_training_init(psp);
455 DRM_ERROR("Failed to initialize memory training!\n");
459 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
461 DRM_ERROR("Failed to process memory training!\n");
466 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
467 amdgpu_sriov_vf(adev) ?
468 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
470 &psp->fw_pri_mc_addr,
475 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
476 AMDGPU_GEM_DOMAIN_VRAM |
477 AMDGPU_GEM_DOMAIN_GTT,
479 &psp->fence_buf_mc_addr,
484 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
485 AMDGPU_GEM_DOMAIN_VRAM |
486 AMDGPU_GEM_DOMAIN_GTT,
487 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
488 (void **)&psp->cmd_buf_mem);
495 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
496 &psp->fence_buf_mc_addr, &psp->fence_buf);
498 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
499 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
503 static int psp_sw_fini(void *handle)
505 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
506 struct psp_context *psp = &adev->psp;
507 struct psp_gfx_cmd_resp *cmd = psp->cmd;
509 psp_memory_training_fini(psp);
511 amdgpu_ucode_release(&psp->sos_fw);
512 amdgpu_ucode_release(&psp->asd_fw);
513 amdgpu_ucode_release(&psp->ta_fw);
514 amdgpu_ucode_release(&psp->cap_fw);
515 amdgpu_ucode_release(&psp->toc_fw);
520 psp_free_shared_bufs(psp);
522 if (psp->km_ring.ring_mem)
523 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
524 &psp->km_ring.ring_mem_mc_addr,
525 (void **)&psp->km_ring.ring_mem);
527 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
528 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
529 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
530 &psp->fence_buf_mc_addr, &psp->fence_buf);
531 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
532 (void **)&psp->cmd_buf_mem);
537 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
538 uint32_t reg_val, uint32_t mask, bool check_changed)
542 struct amdgpu_device *adev = psp->adev;
544 if (psp->adev->no_hw_access)
547 for (i = 0; i < adev->usec_timeout; i++) {
548 val = RREG32(reg_index);
553 if ((val & mask) == reg_val)
562 int psp_wait_for_spirom_update(struct psp_context *psp, uint32_t reg_index,
563 uint32_t reg_val, uint32_t mask, uint32_t msec_timeout)
567 struct amdgpu_device *adev = psp->adev;
569 if (psp->adev->no_hw_access)
572 for (i = 0; i < msec_timeout; i++) {
573 val = RREG32(reg_index);
574 if ((val & mask) == reg_val)
582 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
585 case GFX_CMD_ID_LOAD_TA:
587 case GFX_CMD_ID_UNLOAD_TA:
589 case GFX_CMD_ID_INVOKE_CMD:
591 case GFX_CMD_ID_LOAD_ASD:
593 case GFX_CMD_ID_SETUP_TMR:
595 case GFX_CMD_ID_LOAD_IP_FW:
597 case GFX_CMD_ID_DESTROY_TMR:
598 return "DESTROY_TMR";
599 case GFX_CMD_ID_SAVE_RESTORE:
600 return "SAVE_RESTORE_IP_FW";
601 case GFX_CMD_ID_SETUP_VMR:
603 case GFX_CMD_ID_DESTROY_VMR:
604 return "DESTROY_VMR";
605 case GFX_CMD_ID_PROG_REG:
607 case GFX_CMD_ID_GET_FW_ATTESTATION:
608 return "GET_FW_ATTESTATION";
609 case GFX_CMD_ID_LOAD_TOC:
610 return "ID_LOAD_TOC";
611 case GFX_CMD_ID_AUTOLOAD_RLC:
612 return "AUTOLOAD_RLC";
613 case GFX_CMD_ID_BOOT_CFG:
616 return "UNKNOWN CMD";
621 psp_cmd_submit_buf(struct psp_context *psp,
622 struct amdgpu_firmware_info *ucode,
623 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
628 bool ras_intr = false;
629 bool skip_unsupport = false;
631 if (psp->adev->no_hw_access)
634 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
636 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
638 index = atomic_inc_return(&psp->fence_value);
639 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
641 atomic_dec(&psp->fence_value);
645 amdgpu_device_invalidate_hdp(psp->adev, NULL);
646 while (*((unsigned int *)psp->fence_buf) != index) {
650 * Shouldn't wait for timeout when err_event_athub occurs,
651 * because gpu reset thread triggered and lock resource should
652 * be released for psp resume sequence.
654 ras_intr = amdgpu_ras_intr_triggered();
657 usleep_range(10, 100);
658 amdgpu_device_invalidate_hdp(psp->adev, NULL);
661 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
662 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
663 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
665 memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
667 /* In some cases, psp response status is not 0 even there is no
668 * problem while the command is submitted. Some version of PSP FW
669 * doesn't write 0 to that field.
670 * So here we would like to only print a warning instead of an error
671 * during psp initialization to avoid breaking hw_init and it doesn't
674 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
676 DRM_WARN("failed to load ucode %s(0x%X) ",
677 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
678 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
679 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
680 psp->cmd_buf_mem->resp.status);
681 /* If any firmware (including CAP) load fails under SRIOV, it should
682 * return failure to stop the VF from initializing.
683 * Also return failure in case of timeout
685 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
692 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
693 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
700 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
702 struct psp_gfx_cmd_resp *cmd = psp->cmd;
704 mutex_lock(&psp->mutex);
706 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
711 static void release_psp_cmd_buf(struct psp_context *psp)
713 mutex_unlock(&psp->mutex);
716 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
717 struct psp_gfx_cmd_resp *cmd,
718 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
720 struct amdgpu_device *adev = psp->adev;
725 size = amdgpu_bo_size(tmr_bo);
726 tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
729 if (amdgpu_sriov_vf(psp->adev))
730 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
732 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
733 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
734 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
735 cmd->cmd.cmd_setup_tmr.buf_size = size;
736 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
737 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
738 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
741 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
742 uint64_t pri_buf_mc, uint32_t size)
744 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
745 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
746 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
747 cmd->cmd.cmd_load_toc.toc_size = size;
750 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
751 static int psp_load_toc(struct psp_context *psp,
755 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
757 /* Copy toc to psp firmware private buffer */
758 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
760 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
762 ret = psp_cmd_submit_buf(psp, NULL, cmd,
763 psp->fence_buf_mc_addr);
765 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
767 release_psp_cmd_buf(psp);
772 static bool psp_boottime_tmr(struct psp_context *psp)
774 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
775 case IP_VERSION(13, 0, 6):
782 /* Set up Trusted Memory Region */
783 static int psp_tmr_init(struct psp_context *psp)
791 * According to HW engineer, they prefer the TMR address be "naturally
792 * aligned" , e.g. the start address be an integer divide of TMR size.
794 * Note: this memory need be reserved till the driver
797 tmr_size = PSP_TMR_SIZE(psp->adev);
799 /* For ASICs support RLC autoload, psp will parse the toc
800 * and calculate the total size of TMR needed */
801 if (!amdgpu_sriov_vf(psp->adev) &&
802 psp->toc.start_addr &&
803 psp->toc.size_bytes &&
805 ret = psp_load_toc(psp, &tmr_size);
807 DRM_ERROR("Failed to load toc\n");
813 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
814 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
816 AMDGPU_HAS_VRAM(psp->adev) ?
817 AMDGPU_GEM_DOMAIN_VRAM :
818 AMDGPU_GEM_DOMAIN_GTT,
819 &psp->tmr_bo, &psp->tmr_mc_addr,
826 static bool psp_skip_tmr(struct psp_context *psp)
828 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
829 case IP_VERSION(11, 0, 9):
830 case IP_VERSION(11, 0, 7):
831 case IP_VERSION(13, 0, 2):
832 case IP_VERSION(13, 0, 6):
833 case IP_VERSION(13, 0, 10):
840 static int psp_tmr_load(struct psp_context *psp)
843 struct psp_gfx_cmd_resp *cmd;
845 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
846 * Already set up by host driver.
848 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
851 cmd = acquire_psp_cmd_buf(psp);
853 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
855 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
856 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
858 ret = psp_cmd_submit_buf(psp, NULL, cmd,
859 psp->fence_buf_mc_addr);
861 release_psp_cmd_buf(psp);
866 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
867 struct psp_gfx_cmd_resp *cmd)
869 if (amdgpu_sriov_vf(psp->adev))
870 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
872 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
875 static int psp_tmr_unload(struct psp_context *psp)
878 struct psp_gfx_cmd_resp *cmd;
880 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
881 * as TMR is not loaded at all
883 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
886 cmd = acquire_psp_cmd_buf(psp);
888 psp_prep_tmr_unload_cmd_buf(psp, cmd);
889 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
891 ret = psp_cmd_submit_buf(psp, NULL, cmd,
892 psp->fence_buf_mc_addr);
894 release_psp_cmd_buf(psp);
899 static int psp_tmr_terminate(struct psp_context *psp)
901 return psp_tmr_unload(psp);
904 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
905 uint64_t *output_ptr)
908 struct psp_gfx_cmd_resp *cmd;
913 if (amdgpu_sriov_vf(psp->adev))
916 cmd = acquire_psp_cmd_buf(psp);
918 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
920 ret = psp_cmd_submit_buf(psp, NULL, cmd,
921 psp->fence_buf_mc_addr);
924 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
925 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
928 release_psp_cmd_buf(psp);
933 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
935 struct psp_context *psp = &adev->psp;
936 struct psp_gfx_cmd_resp *cmd;
939 if (amdgpu_sriov_vf(adev))
942 cmd = acquire_psp_cmd_buf(psp);
944 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
945 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
947 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
950 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
953 release_psp_cmd_buf(psp);
958 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
961 struct psp_context *psp = &adev->psp;
962 struct psp_gfx_cmd_resp *cmd;
964 if (amdgpu_sriov_vf(adev))
967 cmd = acquire_psp_cmd_buf(psp);
969 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
970 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
971 cmd->cmd.boot_cfg.boot_config = boot_cfg;
972 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
974 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
976 release_psp_cmd_buf(psp);
981 static int psp_rl_load(struct amdgpu_device *adev)
984 struct psp_context *psp = &adev->psp;
985 struct psp_gfx_cmd_resp *cmd;
987 if (!is_psp_fw_valid(psp->rl))
990 cmd = acquire_psp_cmd_buf(psp);
992 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
993 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
995 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
996 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
997 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
998 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
999 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
1001 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1003 release_psp_cmd_buf(psp);
1008 int psp_spatial_partition(struct psp_context *psp, int mode)
1010 struct psp_gfx_cmd_resp *cmd;
1013 if (amdgpu_sriov_vf(psp->adev))
1016 cmd = acquire_psp_cmd_buf(psp);
1018 cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1019 cmd->cmd.cmd_spatial_part.mode = mode;
1021 dev_info(psp->adev->dev, "Requesting %d partitions through PSP", mode);
1022 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1024 release_psp_cmd_buf(psp);
1029 static int psp_asd_initialize(struct psp_context *psp)
1033 /* If PSP version doesn't match ASD version, asd loading will be failed.
1034 * add workaround to bypass it for sriov now.
1035 * TODO: add version check to make it common
1037 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1040 psp->asd_context.mem_context.shared_mc_addr = 0;
1041 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
1042 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
1044 ret = psp_ta_load(psp, &psp->asd_context);
1046 psp->asd_context.initialized = true;
1051 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1052 uint32_t session_id)
1054 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1055 cmd->cmd.cmd_unload_ta.session_id = session_id;
1058 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1061 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1063 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1065 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1067 context->resp_status = cmd->resp.status;
1069 release_psp_cmd_buf(psp);
1074 static int psp_asd_terminate(struct psp_context *psp)
1078 if (amdgpu_sriov_vf(psp->adev))
1081 if (!psp->asd_context.initialized)
1084 ret = psp_ta_unload(psp, &psp->asd_context);
1086 psp->asd_context.initialized = false;
1091 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1092 uint32_t id, uint32_t value)
1094 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1095 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1096 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1099 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1102 struct psp_gfx_cmd_resp *cmd;
1105 if (reg >= PSP_REG_LAST)
1108 cmd = acquire_psp_cmd_buf(psp);
1110 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1111 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1113 DRM_ERROR("PSP failed to program reg id %d", reg);
1115 release_psp_cmd_buf(psp);
1120 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1122 struct ta_context *context)
1124 cmd->cmd_id = context->ta_load_type;
1125 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1126 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1127 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1129 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1130 lower_32_bits(context->mem_context.shared_mc_addr);
1131 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1132 upper_32_bits(context->mem_context.shared_mc_addr);
1133 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1136 int psp_ta_init_shared_buf(struct psp_context *psp,
1137 struct ta_mem_context *mem_ctx)
1140 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1141 * physical) for ta to host memory
1143 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1144 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1145 AMDGPU_GEM_DOMAIN_GTT,
1146 &mem_ctx->shared_bo,
1147 &mem_ctx->shared_mc_addr,
1148 &mem_ctx->shared_buf);
1151 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1153 uint32_t session_id)
1155 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1156 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1157 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1160 int psp_ta_invoke(struct psp_context *psp,
1162 struct ta_context *context)
1165 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1167 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1169 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1170 psp->fence_buf_mc_addr);
1172 context->resp_status = cmd->resp.status;
1174 release_psp_cmd_buf(psp);
1179 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1182 struct psp_gfx_cmd_resp *cmd;
1184 cmd = acquire_psp_cmd_buf(psp);
1186 psp_copy_fw(psp, context->bin_desc.start_addr,
1187 context->bin_desc.size_bytes);
1189 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1191 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1192 psp->fence_buf_mc_addr);
1194 context->resp_status = cmd->resp.status;
1197 context->session_id = cmd->resp.session_id;
1199 release_psp_cmd_buf(psp);
1204 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1206 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1209 int psp_xgmi_terminate(struct psp_context *psp)
1212 struct amdgpu_device *adev = psp->adev;
1214 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1215 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1216 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1217 adev->gmc.xgmi.connected_to_cpu))
1220 if (!psp->xgmi_context.context.initialized)
1223 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1225 psp->xgmi_context.context.initialized = false;
1230 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1232 struct ta_xgmi_shared_memory *xgmi_cmd;
1236 !psp->xgmi_context.context.bin_desc.size_bytes ||
1237 !psp->xgmi_context.context.bin_desc.start_addr)
1243 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1244 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1246 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1247 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1253 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1255 psp->xgmi_context.context.initialized = true;
1260 /* Initialize XGMI session */
1261 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1262 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1263 xgmi_cmd->flag_extend_link_record = set_extended_data;
1264 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1266 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1271 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1273 struct ta_xgmi_shared_memory *xgmi_cmd;
1276 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1277 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1279 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1281 /* Invoke xgmi ta to get hive id */
1282 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1286 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1291 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1293 struct ta_xgmi_shared_memory *xgmi_cmd;
1296 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1297 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1299 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1301 /* Invoke xgmi ta to get the node id */
1302 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1306 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1311 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1313 return (psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1314 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
1315 psp->adev->ip_versions[MP0_HWIP][0] >= IP_VERSION(13, 0, 6);
1319 * Chips that support extended topology information require the driver to
1320 * reflect topology information in the opposite direction. This is
1321 * because the TA has already exceeded its link record limit and if the
1322 * TA holds bi-directional information, the driver would have to do
1323 * multiple fetches instead of just two.
1325 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1326 struct psp_xgmi_node_info node_info)
1328 struct amdgpu_device *mirror_adev;
1329 struct amdgpu_hive_info *hive;
1330 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1331 uint64_t dst_node_id = node_info.node_id;
1332 uint8_t dst_num_hops = node_info.num_hops;
1333 uint8_t dst_num_links = node_info.num_links;
1335 hive = amdgpu_get_xgmi_hive(psp->adev);
1336 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1337 struct psp_xgmi_topology_info *mirror_top_info;
1340 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1343 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1344 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1345 if (mirror_top_info->nodes[j].node_id != src_node_id)
1348 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1350 * prevent 0 num_links value re-reflection since reflection
1351 * criteria is based on num_hops (direct or indirect).
1355 mirror_top_info->nodes[j].num_links = dst_num_links;
1363 amdgpu_put_xgmi_hive(hive);
1366 int psp_xgmi_get_topology_info(struct psp_context *psp,
1368 struct psp_xgmi_topology_info *topology,
1369 bool get_extended_data)
1371 struct ta_xgmi_shared_memory *xgmi_cmd;
1372 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1373 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1377 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1380 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1381 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1382 xgmi_cmd->flag_extend_link_record = get_extended_data;
1384 /* Fill in the shared memory with topology information as input */
1385 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1386 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1387 topology_info_input->num_nodes = number_devices;
1389 for (i = 0; i < topology_info_input->num_nodes; i++) {
1390 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1391 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1392 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1393 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1396 /* Invoke xgmi ta to get the topology information */
1397 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1401 /* Read the output topology information from the shared memory */
1402 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1403 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1404 for (i = 0; i < topology->num_nodes; i++) {
1405 /* extended data will either be 0 or equal to non-extended data */
1406 if (topology_info_output->nodes[i].num_hops)
1407 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1409 /* non-extended data gets everything here so no need to update */
1410 if (!get_extended_data) {
1411 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1412 topology->nodes[i].is_sharing_enabled =
1413 topology_info_output->nodes[i].is_sharing_enabled;
1414 topology->nodes[i].sdma_engine =
1415 topology_info_output->nodes[i].sdma_engine;
1420 /* Invoke xgmi ta again to get the link information */
1421 if (psp_xgmi_peer_link_info_supported(psp)) {
1422 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1423 bool requires_reflection =
1424 (psp->xgmi_context.supports_extended_data && get_extended_data) ||
1425 psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6);
1427 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1429 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1434 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1435 for (i = 0; i < topology->num_nodes; i++) {
1436 /* accumulate num_links on extended data */
1437 topology->nodes[i].num_links = get_extended_data ?
1438 topology->nodes[i].num_links +
1439 link_info_output->nodes[i].num_links :
1440 ((requires_reflection && topology->nodes[i].num_links) ? topology->nodes[i].num_links :
1441 link_info_output->nodes[i].num_links);
1443 /* reflect the topology information for bi-directionality */
1444 if (requires_reflection && topology->nodes[i].num_hops)
1445 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1452 int psp_xgmi_set_topology_info(struct psp_context *psp,
1454 struct psp_xgmi_topology_info *topology)
1456 struct ta_xgmi_shared_memory *xgmi_cmd;
1457 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1460 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1463 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1464 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1466 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1467 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1468 topology_info_input->num_nodes = number_devices;
1470 for (i = 0; i < topology_info_input->num_nodes; i++) {
1471 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1472 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1473 topology_info_input->nodes[i].is_sharing_enabled = 1;
1474 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1477 /* Invoke xgmi ta to set topology information */
1478 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1482 static void psp_ras_ta_check_status(struct psp_context *psp)
1484 struct ta_ras_shared_memory *ras_cmd =
1485 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1487 switch (ras_cmd->ras_status) {
1488 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1489 dev_warn(psp->adev->dev,
1490 "RAS WARNING: cmd failed due to unsupported ip\n");
1492 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1493 dev_warn(psp->adev->dev,
1494 "RAS WARNING: cmd failed due to unsupported error injection\n");
1496 case TA_RAS_STATUS__SUCCESS:
1498 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1499 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1500 dev_warn(psp->adev->dev,
1501 "RAS WARNING: Inject error to critical region is not allowed\n");
1504 dev_warn(psp->adev->dev,
1505 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1510 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1512 struct ta_ras_shared_memory *ras_cmd;
1515 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1518 * TODO: bypass the loading in sriov for now
1520 if (amdgpu_sriov_vf(psp->adev))
1523 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1525 if (amdgpu_ras_intr_triggered())
1528 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1529 DRM_WARN("RAS: Unsupported Interface");
1534 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1535 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1537 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1538 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1539 dev_warn(psp->adev->dev,
1540 "RAS internal register access blocked\n");
1542 psp_ras_ta_check_status(psp);
1548 int psp_ras_enable_features(struct psp_context *psp,
1549 union ta_ras_cmd_input *info, bool enable)
1551 struct ta_ras_shared_memory *ras_cmd;
1554 if (!psp->ras_context.context.initialized)
1557 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1558 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1561 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1563 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1565 ras_cmd->ras_in_message = *info;
1567 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1574 int psp_ras_terminate(struct psp_context *psp)
1579 * TODO: bypass the terminate in sriov for now
1581 if (amdgpu_sriov_vf(psp->adev))
1584 if (!psp->ras_context.context.initialized)
1587 ret = psp_ta_unload(psp, &psp->ras_context.context);
1589 psp->ras_context.context.initialized = false;
1594 int psp_ras_initialize(struct psp_context *psp)
1597 uint32_t boot_cfg = 0xFF;
1598 struct amdgpu_device *adev = psp->adev;
1599 struct ta_ras_shared_memory *ras_cmd;
1602 * TODO: bypass the initialize in sriov for now
1604 if (amdgpu_sriov_vf(adev))
1607 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1608 !adev->psp.ras_context.context.bin_desc.start_addr) {
1609 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1613 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1614 /* query GECC enablement status from boot config
1615 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1617 ret = psp_boot_config_get(adev, &boot_cfg);
1619 dev_warn(adev->dev, "PSP get boot config failed\n");
1621 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1623 dev_info(adev->dev, "GECC is disabled\n");
1625 /* disable GECC in next boot cycle if ras is
1626 * disabled by module parameter amdgpu_ras_enable
1627 * and/or amdgpu_ras_mask, or boot_config_get call
1630 ret = psp_boot_config_set(adev, 0);
1632 dev_warn(adev->dev, "PSP set boot config failed\n");
1634 dev_warn(adev->dev, "GECC will be disabled in next boot cycle if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1637 if (boot_cfg == 1) {
1638 dev_info(adev->dev, "GECC is enabled\n");
1640 /* enable GECC in next boot cycle if it is disabled
1641 * in boot config, or force enable GECC if failed to
1642 * get boot configuration
1644 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1646 dev_warn(adev->dev, "PSP set boot config failed\n");
1648 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1653 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1654 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1656 if (!psp->ras_context.context.mem_context.shared_buf) {
1657 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1662 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1663 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1665 if (amdgpu_ras_is_poison_mode_supported(adev))
1666 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1667 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu)
1668 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1669 ras_cmd->ras_in_message.init_flags.xcc_mask =
1671 ras_cmd->ras_in_message.init_flags.channel_dis_num = hweight32(adev->gmc.m_half_use) * 2;
1673 ret = psp_ta_load(psp, &psp->ras_context.context);
1675 if (!ret && !ras_cmd->ras_status)
1676 psp->ras_context.context.initialized = true;
1678 if (ras_cmd->ras_status)
1679 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1681 /* fail to load RAS TA */
1682 psp->ras_context.context.initialized = false;
1688 int psp_ras_trigger_error(struct psp_context *psp,
1689 struct ta_ras_trigger_error_input *info, uint32_t instance_mask)
1691 struct ta_ras_shared_memory *ras_cmd;
1692 struct amdgpu_device *adev = psp->adev;
1696 if (!psp->ras_context.context.initialized)
1699 switch (info->block_id) {
1700 case TA_RAS_BLOCK__GFX:
1701 dev_mask = GET_MASK(GC, instance_mask);
1703 case TA_RAS_BLOCK__SDMA:
1704 dev_mask = GET_MASK(SDMA0, instance_mask);
1706 case TA_RAS_BLOCK__VCN:
1707 case TA_RAS_BLOCK__JPEG:
1708 dev_mask = GET_MASK(VCN, instance_mask);
1711 dev_mask = instance_mask;
1715 /* reuse sub_block_index for backward compatibility */
1716 dev_mask <<= AMDGPU_RAS_INST_SHIFT;
1717 dev_mask &= AMDGPU_RAS_INST_MASK;
1718 info->sub_block_index |= dev_mask;
1720 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1721 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1723 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1724 ras_cmd->ras_in_message.trigger_error = *info;
1726 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1730 /* If err_event_athub occurs error inject was successful, however
1731 return status from TA is no long reliable */
1732 if (amdgpu_ras_intr_triggered())
1735 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1737 else if (ras_cmd->ras_status)
1745 static int psp_hdcp_initialize(struct psp_context *psp)
1750 * TODO: bypass the initialize in sriov for now
1752 if (amdgpu_sriov_vf(psp->adev))
1755 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1756 !psp->hdcp_context.context.bin_desc.start_addr) {
1757 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1761 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1762 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1764 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1765 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1770 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1772 psp->hdcp_context.context.initialized = true;
1773 mutex_init(&psp->hdcp_context.mutex);
1779 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1782 * TODO: bypass the loading in sriov for now
1784 if (amdgpu_sriov_vf(psp->adev))
1787 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1790 static int psp_hdcp_terminate(struct psp_context *psp)
1795 * TODO: bypass the terminate in sriov for now
1797 if (amdgpu_sriov_vf(psp->adev))
1800 if (!psp->hdcp_context.context.initialized)
1803 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1805 psp->hdcp_context.context.initialized = false;
1812 static int psp_dtm_initialize(struct psp_context *psp)
1817 * TODO: bypass the initialize in sriov for now
1819 if (amdgpu_sriov_vf(psp->adev))
1822 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1823 !psp->dtm_context.context.bin_desc.start_addr) {
1824 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1828 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1829 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1831 if (!psp->dtm_context.context.mem_context.shared_buf) {
1832 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1837 ret = psp_ta_load(psp, &psp->dtm_context.context);
1839 psp->dtm_context.context.initialized = true;
1840 mutex_init(&psp->dtm_context.mutex);
1846 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1849 * TODO: bypass the loading in sriov for now
1851 if (amdgpu_sriov_vf(psp->adev))
1854 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1857 static int psp_dtm_terminate(struct psp_context *psp)
1862 * TODO: bypass the terminate in sriov for now
1864 if (amdgpu_sriov_vf(psp->adev))
1867 if (!psp->dtm_context.context.initialized)
1870 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1872 psp->dtm_context.context.initialized = false;
1879 static int psp_rap_initialize(struct psp_context *psp)
1882 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1885 * TODO: bypass the initialize in sriov for now
1887 if (amdgpu_sriov_vf(psp->adev))
1890 if (!psp->rap_context.context.bin_desc.size_bytes ||
1891 !psp->rap_context.context.bin_desc.start_addr) {
1892 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1896 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1897 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1899 if (!psp->rap_context.context.mem_context.shared_buf) {
1900 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1905 ret = psp_ta_load(psp, &psp->rap_context.context);
1907 psp->rap_context.context.initialized = true;
1908 mutex_init(&psp->rap_context.mutex);
1912 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1913 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1914 psp_rap_terminate(psp);
1915 /* free rap shared memory */
1916 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1918 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1927 static int psp_rap_terminate(struct psp_context *psp)
1931 if (!psp->rap_context.context.initialized)
1934 ret = psp_ta_unload(psp, &psp->rap_context.context);
1936 psp->rap_context.context.initialized = false;
1941 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1943 struct ta_rap_shared_memory *rap_cmd;
1946 if (!psp->rap_context.context.initialized)
1949 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1950 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1953 mutex_lock(&psp->rap_context.mutex);
1955 rap_cmd = (struct ta_rap_shared_memory *)
1956 psp->rap_context.context.mem_context.shared_buf;
1957 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1959 rap_cmd->cmd_id = ta_cmd_id;
1960 rap_cmd->validation_method_id = METHOD_A;
1962 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1967 *status = rap_cmd->rap_status;
1970 mutex_unlock(&psp->rap_context.mutex);
1976 /* securedisplay start */
1977 static int psp_securedisplay_initialize(struct psp_context *psp)
1980 struct ta_securedisplay_cmd *securedisplay_cmd;
1983 * TODO: bypass the initialize in sriov for now
1985 if (amdgpu_sriov_vf(psp->adev))
1988 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1989 !psp->securedisplay_context.context.bin_desc.start_addr) {
1990 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1994 psp->securedisplay_context.context.mem_context.shared_mem_size =
1995 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1996 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1998 if (!psp->securedisplay_context.context.initialized) {
1999 ret = psp_ta_init_shared_buf(psp,
2000 &psp->securedisplay_context.context.mem_context);
2005 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
2007 psp->securedisplay_context.context.initialized = true;
2008 mutex_init(&psp->securedisplay_context.mutex);
2012 mutex_lock(&psp->securedisplay_context.mutex);
2014 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
2015 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2017 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2019 mutex_unlock(&psp->securedisplay_context.mutex);
2022 psp_securedisplay_terminate(psp);
2023 /* free securedisplay shared memory */
2024 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
2025 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
2029 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
2030 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
2031 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
2032 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
2033 /* don't try again */
2034 psp->securedisplay_context.context.bin_desc.size_bytes = 0;
2040 static int psp_securedisplay_terminate(struct psp_context *psp)
2045 * TODO:bypass the terminate in sriov for now
2047 if (amdgpu_sriov_vf(psp->adev))
2050 if (!psp->securedisplay_context.context.initialized)
2053 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2055 psp->securedisplay_context.context.initialized = false;
2060 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2064 if (!psp->securedisplay_context.context.initialized)
2067 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2068 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2071 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2075 /* SECUREDISPLAY end */
2077 static int psp_hw_start(struct psp_context *psp)
2079 struct amdgpu_device *adev = psp->adev;
2082 if (!amdgpu_sriov_vf(adev)) {
2083 if ((is_psp_fw_valid(psp->kdb)) &&
2084 (psp->funcs->bootloader_load_kdb != NULL)) {
2085 ret = psp_bootloader_load_kdb(psp);
2087 DRM_ERROR("PSP load kdb failed!\n");
2092 if ((is_psp_fw_valid(psp->spl)) &&
2093 (psp->funcs->bootloader_load_spl != NULL)) {
2094 ret = psp_bootloader_load_spl(psp);
2096 DRM_ERROR("PSP load spl failed!\n");
2101 if ((is_psp_fw_valid(psp->sys)) &&
2102 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2103 ret = psp_bootloader_load_sysdrv(psp);
2105 DRM_ERROR("PSP load sys drv failed!\n");
2110 if ((is_psp_fw_valid(psp->soc_drv)) &&
2111 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2112 ret = psp_bootloader_load_soc_drv(psp);
2114 DRM_ERROR("PSP load soc drv failed!\n");
2119 if ((is_psp_fw_valid(psp->intf_drv)) &&
2120 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2121 ret = psp_bootloader_load_intf_drv(psp);
2123 DRM_ERROR("PSP load intf drv failed!\n");
2128 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2129 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2130 ret = psp_bootloader_load_dbg_drv(psp);
2132 DRM_ERROR("PSP load dbg drv failed!\n");
2137 if ((is_psp_fw_valid(psp->ras_drv)) &&
2138 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2139 ret = psp_bootloader_load_ras_drv(psp);
2141 DRM_ERROR("PSP load ras_drv failed!\n");
2146 if ((is_psp_fw_valid(psp->sos)) &&
2147 (psp->funcs->bootloader_load_sos != NULL)) {
2148 ret = psp_bootloader_load_sos(psp);
2150 DRM_ERROR("PSP load sos failed!\n");
2156 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2158 DRM_ERROR("PSP create ring failed!\n");
2162 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2165 if (!psp_boottime_tmr(psp)) {
2166 ret = psp_tmr_init(psp);
2168 DRM_ERROR("PSP tmr init failed!\n");
2175 * For ASICs with DF Cstate management centralized
2176 * to PMFW, TMR setup should be performed after PMFW
2177 * loaded and before other non-psp firmware loaded.
2179 if (psp->pmfw_centralized_cstate_management) {
2180 ret = psp_load_smu_fw(psp);
2185 ret = psp_tmr_load(psp);
2187 DRM_ERROR("PSP load tmr failed!\n");
2194 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2195 enum psp_gfx_fw_type *type)
2197 switch (ucode->ucode_id) {
2198 case AMDGPU_UCODE_ID_CAP:
2199 *type = GFX_FW_TYPE_CAP;
2201 case AMDGPU_UCODE_ID_SDMA0:
2202 *type = GFX_FW_TYPE_SDMA0;
2204 case AMDGPU_UCODE_ID_SDMA1:
2205 *type = GFX_FW_TYPE_SDMA1;
2207 case AMDGPU_UCODE_ID_SDMA2:
2208 *type = GFX_FW_TYPE_SDMA2;
2210 case AMDGPU_UCODE_ID_SDMA3:
2211 *type = GFX_FW_TYPE_SDMA3;
2213 case AMDGPU_UCODE_ID_SDMA4:
2214 *type = GFX_FW_TYPE_SDMA4;
2216 case AMDGPU_UCODE_ID_SDMA5:
2217 *type = GFX_FW_TYPE_SDMA5;
2219 case AMDGPU_UCODE_ID_SDMA6:
2220 *type = GFX_FW_TYPE_SDMA6;
2222 case AMDGPU_UCODE_ID_SDMA7:
2223 *type = GFX_FW_TYPE_SDMA7;
2225 case AMDGPU_UCODE_ID_CP_MES:
2226 *type = GFX_FW_TYPE_CP_MES;
2228 case AMDGPU_UCODE_ID_CP_MES_DATA:
2229 *type = GFX_FW_TYPE_MES_STACK;
2231 case AMDGPU_UCODE_ID_CP_MES1:
2232 *type = GFX_FW_TYPE_CP_MES_KIQ;
2234 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2235 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2237 case AMDGPU_UCODE_ID_CP_CE:
2238 *type = GFX_FW_TYPE_CP_CE;
2240 case AMDGPU_UCODE_ID_CP_PFP:
2241 *type = GFX_FW_TYPE_CP_PFP;
2243 case AMDGPU_UCODE_ID_CP_ME:
2244 *type = GFX_FW_TYPE_CP_ME;
2246 case AMDGPU_UCODE_ID_CP_MEC1:
2247 *type = GFX_FW_TYPE_CP_MEC;
2249 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2250 *type = GFX_FW_TYPE_CP_MEC_ME1;
2252 case AMDGPU_UCODE_ID_CP_MEC2:
2253 *type = GFX_FW_TYPE_CP_MEC;
2255 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2256 *type = GFX_FW_TYPE_CP_MEC_ME2;
2258 case AMDGPU_UCODE_ID_RLC_P:
2259 *type = GFX_FW_TYPE_RLC_P;
2261 case AMDGPU_UCODE_ID_RLC_V:
2262 *type = GFX_FW_TYPE_RLC_V;
2264 case AMDGPU_UCODE_ID_RLC_G:
2265 *type = GFX_FW_TYPE_RLC_G;
2267 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2268 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2270 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2271 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2273 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2274 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2276 case AMDGPU_UCODE_ID_RLC_IRAM:
2277 *type = GFX_FW_TYPE_RLC_IRAM;
2279 case AMDGPU_UCODE_ID_RLC_DRAM:
2280 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2282 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2283 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2285 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2286 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2288 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2289 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2291 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2292 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2294 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2295 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2297 case AMDGPU_UCODE_ID_SMC:
2298 *type = GFX_FW_TYPE_SMU;
2300 case AMDGPU_UCODE_ID_PPTABLE:
2301 *type = GFX_FW_TYPE_PPTABLE;
2303 case AMDGPU_UCODE_ID_UVD:
2304 *type = GFX_FW_TYPE_UVD;
2306 case AMDGPU_UCODE_ID_UVD1:
2307 *type = GFX_FW_TYPE_UVD1;
2309 case AMDGPU_UCODE_ID_VCE:
2310 *type = GFX_FW_TYPE_VCE;
2312 case AMDGPU_UCODE_ID_VCN:
2313 *type = GFX_FW_TYPE_VCN;
2315 case AMDGPU_UCODE_ID_VCN1:
2316 *type = GFX_FW_TYPE_VCN1;
2318 case AMDGPU_UCODE_ID_DMCU_ERAM:
2319 *type = GFX_FW_TYPE_DMCU_ERAM;
2321 case AMDGPU_UCODE_ID_DMCU_INTV:
2322 *type = GFX_FW_TYPE_DMCU_ISR;
2324 case AMDGPU_UCODE_ID_VCN0_RAM:
2325 *type = GFX_FW_TYPE_VCN0_RAM;
2327 case AMDGPU_UCODE_ID_VCN1_RAM:
2328 *type = GFX_FW_TYPE_VCN1_RAM;
2330 case AMDGPU_UCODE_ID_DMCUB:
2331 *type = GFX_FW_TYPE_DMUB;
2333 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2334 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2336 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2337 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2339 case AMDGPU_UCODE_ID_IMU_I:
2340 *type = GFX_FW_TYPE_IMU_I;
2342 case AMDGPU_UCODE_ID_IMU_D:
2343 *type = GFX_FW_TYPE_IMU_D;
2345 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2346 *type = GFX_FW_TYPE_RS64_PFP;
2348 case AMDGPU_UCODE_ID_CP_RS64_ME:
2349 *type = GFX_FW_TYPE_RS64_ME;
2351 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2352 *type = GFX_FW_TYPE_RS64_MEC;
2354 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2355 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2357 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2358 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2360 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2361 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2363 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2364 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2366 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2367 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2369 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2370 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2372 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2373 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2375 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2376 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2378 case AMDGPU_UCODE_ID_MAXIMUM:
2386 static void psp_print_fw_hdr(struct psp_context *psp,
2387 struct amdgpu_firmware_info *ucode)
2389 struct amdgpu_device *adev = psp->adev;
2390 struct common_firmware_header *hdr;
2392 switch (ucode->ucode_id) {
2393 case AMDGPU_UCODE_ID_SDMA0:
2394 case AMDGPU_UCODE_ID_SDMA1:
2395 case AMDGPU_UCODE_ID_SDMA2:
2396 case AMDGPU_UCODE_ID_SDMA3:
2397 case AMDGPU_UCODE_ID_SDMA4:
2398 case AMDGPU_UCODE_ID_SDMA5:
2399 case AMDGPU_UCODE_ID_SDMA6:
2400 case AMDGPU_UCODE_ID_SDMA7:
2401 hdr = (struct common_firmware_header *)
2402 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2403 amdgpu_ucode_print_sdma_hdr(hdr);
2405 case AMDGPU_UCODE_ID_CP_CE:
2406 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2407 amdgpu_ucode_print_gfx_hdr(hdr);
2409 case AMDGPU_UCODE_ID_CP_PFP:
2410 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2411 amdgpu_ucode_print_gfx_hdr(hdr);
2413 case AMDGPU_UCODE_ID_CP_ME:
2414 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2415 amdgpu_ucode_print_gfx_hdr(hdr);
2417 case AMDGPU_UCODE_ID_CP_MEC1:
2418 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2419 amdgpu_ucode_print_gfx_hdr(hdr);
2421 case AMDGPU_UCODE_ID_RLC_G:
2422 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2423 amdgpu_ucode_print_rlc_hdr(hdr);
2425 case AMDGPU_UCODE_ID_SMC:
2426 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2427 amdgpu_ucode_print_smc_hdr(hdr);
2434 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2435 struct psp_gfx_cmd_resp *cmd)
2438 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2440 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2441 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2442 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2443 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2445 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2447 DRM_ERROR("Unknown firmware type\n");
2452 int psp_execute_ip_fw_load(struct psp_context *psp,
2453 struct amdgpu_firmware_info *ucode)
2456 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2458 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2460 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2461 psp->fence_buf_mc_addr);
2464 release_psp_cmd_buf(psp);
2469 static int psp_load_smu_fw(struct psp_context *psp)
2472 struct amdgpu_device *adev = psp->adev;
2473 struct amdgpu_firmware_info *ucode =
2474 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2475 struct amdgpu_ras *ras = psp->ras_context.ras;
2478 * Skip SMU FW reloading in case of using BACO for runpm only,
2479 * as SMU is always alive.
2481 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2484 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2487 if ((amdgpu_in_reset(adev) &&
2488 ras && adev->ras_enabled &&
2489 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2490 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2491 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2493 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2496 ret = psp_execute_ip_fw_load(psp, ucode);
2499 DRM_ERROR("PSP load smu failed!\n");
2504 static bool fw_load_skip_check(struct psp_context *psp,
2505 struct amdgpu_firmware_info *ucode)
2507 if (!ucode->fw || !ucode->ucode_size)
2510 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2511 (psp_smu_reload_quirk(psp) ||
2512 psp->autoload_supported ||
2513 psp->pmfw_centralized_cstate_management))
2516 if (amdgpu_sriov_vf(psp->adev) &&
2517 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2520 if (psp->autoload_supported &&
2521 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2522 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2523 /* skip mec JT when autoload is enabled */
2529 int psp_load_fw_list(struct psp_context *psp,
2530 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2533 struct amdgpu_firmware_info *ucode;
2535 for (i = 0; i < ucode_count; ++i) {
2536 ucode = ucode_list[i];
2537 psp_print_fw_hdr(psp, ucode);
2538 ret = psp_execute_ip_fw_load(psp, ucode);
2545 static int psp_load_non_psp_fw(struct psp_context *psp)
2548 struct amdgpu_firmware_info *ucode;
2549 struct amdgpu_device *adev = psp->adev;
2551 if (psp->autoload_supported &&
2552 !psp->pmfw_centralized_cstate_management) {
2553 ret = psp_load_smu_fw(psp);
2558 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2559 ucode = &adev->firmware.ucode[i];
2561 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2562 !fw_load_skip_check(psp, ucode)) {
2563 ret = psp_load_smu_fw(psp);
2569 if (fw_load_skip_check(psp, ucode))
2572 if (psp->autoload_supported &&
2573 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2574 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2575 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2576 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2577 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2578 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2579 /* PSP only receive one SDMA fw for sienna_cichlid,
2580 * as all four sdma fw are same */
2583 psp_print_fw_hdr(psp, ucode);
2585 ret = psp_execute_ip_fw_load(psp, ucode);
2589 /* Start rlc autoload after psp recieved all the gfx firmware */
2590 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2591 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2592 ret = psp_rlc_autoload_start(psp);
2594 DRM_ERROR("Failed to start rlc autoload\n");
2603 static int psp_load_fw(struct amdgpu_device *adev)
2606 struct psp_context *psp = &adev->psp;
2608 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2609 /* should not destroy ring, only stop */
2610 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2612 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2614 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2616 DRM_ERROR("PSP ring init failed!\n");
2621 ret = psp_hw_start(psp);
2625 ret = psp_load_non_psp_fw(psp);
2629 ret = psp_asd_initialize(psp);
2631 DRM_ERROR("PSP load asd failed!\n");
2635 ret = psp_rl_load(adev);
2637 DRM_ERROR("PSP load RL failed!\n");
2641 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2642 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2643 ret = psp_xgmi_initialize(psp, false, true);
2644 /* Warning the XGMI seesion initialize failure
2645 * Instead of stop driver initialization
2648 dev_err(psp->adev->dev,
2649 "XGMI: Failed to initialize XGMI session\n");
2654 ret = psp_ras_initialize(psp);
2656 dev_err(psp->adev->dev,
2657 "RAS: Failed to initialize RAS\n");
2659 ret = psp_hdcp_initialize(psp);
2661 dev_err(psp->adev->dev,
2662 "HDCP: Failed to initialize HDCP\n");
2664 ret = psp_dtm_initialize(psp);
2666 dev_err(psp->adev->dev,
2667 "DTM: Failed to initialize DTM\n");
2669 ret = psp_rap_initialize(psp);
2671 dev_err(psp->adev->dev,
2672 "RAP: Failed to initialize RAP\n");
2674 ret = psp_securedisplay_initialize(psp);
2676 dev_err(psp->adev->dev,
2677 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2683 psp_free_shared_bufs(psp);
2686 * all cleanup jobs (xgmi terminate, ras terminate,
2687 * ring destroy, cmd/fence/fw buffers destory,
2688 * psp->cmd destory) are delayed to psp_hw_fini
2690 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2694 static int psp_hw_init(void *handle)
2697 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2699 mutex_lock(&adev->firmware.mutex);
2701 * This sequence is just used on hw_init only once, no need on
2704 ret = amdgpu_ucode_init_bo(adev);
2708 ret = psp_load_fw(adev);
2710 DRM_ERROR("PSP firmware loading failed\n");
2714 mutex_unlock(&adev->firmware.mutex);
2718 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2719 mutex_unlock(&adev->firmware.mutex);
2723 static int psp_hw_fini(void *handle)
2725 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2726 struct psp_context *psp = &adev->psp;
2729 psp_ras_terminate(psp);
2730 psp_securedisplay_terminate(psp);
2731 psp_rap_terminate(psp);
2732 psp_dtm_terminate(psp);
2733 psp_hdcp_terminate(psp);
2735 if (adev->gmc.xgmi.num_physical_nodes > 1)
2736 psp_xgmi_terminate(psp);
2739 psp_asd_terminate(psp);
2740 psp_tmr_terminate(psp);
2742 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2747 static int psp_suspend(void *handle)
2750 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2751 struct psp_context *psp = &adev->psp;
2753 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2754 psp->xgmi_context.context.initialized) {
2755 ret = psp_xgmi_terminate(psp);
2757 DRM_ERROR("Failed to terminate xgmi ta\n");
2763 ret = psp_ras_terminate(psp);
2765 DRM_ERROR("Failed to terminate ras ta\n");
2768 ret = psp_hdcp_terminate(psp);
2770 DRM_ERROR("Failed to terminate hdcp ta\n");
2773 ret = psp_dtm_terminate(psp);
2775 DRM_ERROR("Failed to terminate dtm ta\n");
2778 ret = psp_rap_terminate(psp);
2780 DRM_ERROR("Failed to terminate rap ta\n");
2783 ret = psp_securedisplay_terminate(psp);
2785 DRM_ERROR("Failed to terminate securedisplay ta\n");
2790 ret = psp_asd_terminate(psp);
2792 DRM_ERROR("Failed to terminate asd\n");
2796 ret = psp_tmr_terminate(psp);
2798 DRM_ERROR("Failed to terminate tmr\n");
2802 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2804 DRM_ERROR("PSP ring stop failed\n");
2810 static int psp_resume(void *handle)
2813 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2814 struct psp_context *psp = &adev->psp;
2816 DRM_INFO("PSP is resuming...\n");
2818 if (psp->mem_train_ctx.enable_mem_training) {
2819 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2821 DRM_ERROR("Failed to process memory training!\n");
2826 mutex_lock(&adev->firmware.mutex);
2828 ret = psp_hw_start(psp);
2832 ret = psp_load_non_psp_fw(psp);
2836 ret = psp_asd_initialize(psp);
2838 DRM_ERROR("PSP load asd failed!\n");
2842 ret = psp_rl_load(adev);
2844 dev_err(adev->dev, "PSP load RL failed!\n");
2848 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2849 ret = psp_xgmi_initialize(psp, false, true);
2850 /* Warning the XGMI seesion initialize failure
2851 * Instead of stop driver initialization
2854 dev_err(psp->adev->dev,
2855 "XGMI: Failed to initialize XGMI session\n");
2859 ret = psp_ras_initialize(psp);
2861 dev_err(psp->adev->dev,
2862 "RAS: Failed to initialize RAS\n");
2864 ret = psp_hdcp_initialize(psp);
2866 dev_err(psp->adev->dev,
2867 "HDCP: Failed to initialize HDCP\n");
2869 ret = psp_dtm_initialize(psp);
2871 dev_err(psp->adev->dev,
2872 "DTM: Failed to initialize DTM\n");
2874 ret = psp_rap_initialize(psp);
2876 dev_err(psp->adev->dev,
2877 "RAP: Failed to initialize RAP\n");
2879 ret = psp_securedisplay_initialize(psp);
2881 dev_err(psp->adev->dev,
2882 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2885 mutex_unlock(&adev->firmware.mutex);
2890 DRM_ERROR("PSP resume failed\n");
2891 mutex_unlock(&adev->firmware.mutex);
2895 int psp_gpu_reset(struct amdgpu_device *adev)
2899 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2902 mutex_lock(&adev->psp.mutex);
2903 ret = psp_mode1_reset(&adev->psp);
2904 mutex_unlock(&adev->psp.mutex);
2909 int psp_rlc_autoload_start(struct psp_context *psp)
2912 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2914 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2916 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2917 psp->fence_buf_mc_addr);
2919 release_psp_cmd_buf(psp);
2924 int psp_ring_cmd_submit(struct psp_context *psp,
2925 uint64_t cmd_buf_mc_addr,
2926 uint64_t fence_mc_addr,
2929 unsigned int psp_write_ptr_reg = 0;
2930 struct psp_gfx_rb_frame *write_frame;
2931 struct psp_ring *ring = &psp->km_ring;
2932 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2933 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2934 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2935 struct amdgpu_device *adev = psp->adev;
2936 uint32_t ring_size_dw = ring->ring_size / 4;
2937 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2939 /* KM (GPCOM) prepare write pointer */
2940 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2942 /* Update KM RB frame pointer to new frame */
2943 /* write_frame ptr increments by size of rb_frame in bytes */
2944 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2945 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2946 write_frame = ring_buffer_start;
2948 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2949 /* Check invalid write_frame ptr address */
2950 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2951 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2952 ring_buffer_start, ring_buffer_end, write_frame);
2953 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2957 /* Initialize KM RB frame */
2958 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2960 /* Update KM RB frame */
2961 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2962 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2963 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2964 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2965 write_frame->fence_value = index;
2966 amdgpu_device_flush_hdp(adev, NULL);
2968 /* Update the write Pointer in DWORDs */
2969 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2970 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2974 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2976 struct amdgpu_device *adev = psp->adev;
2977 char fw_name[PSP_FW_NAME_LEN];
2978 const struct psp_firmware_header_v1_0 *asd_hdr;
2981 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2982 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2986 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2987 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2988 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2989 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2990 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2991 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2994 amdgpu_ucode_release(&adev->psp.asd_fw);
2998 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
3000 struct amdgpu_device *adev = psp->adev;
3001 char fw_name[PSP_FW_NAME_LEN];
3002 const struct psp_firmware_header_v1_0 *toc_hdr;
3005 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
3006 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
3010 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
3011 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
3012 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
3013 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
3014 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
3015 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
3018 amdgpu_ucode_release(&adev->psp.toc_fw);
3022 static int parse_sos_bin_descriptor(struct psp_context *psp,
3023 const struct psp_fw_bin_desc *desc,
3024 const struct psp_firmware_header_v2_0 *sos_hdr)
3026 uint8_t *ucode_start_addr = NULL;
3028 if (!psp || !desc || !sos_hdr)
3031 ucode_start_addr = (uint8_t *)sos_hdr +
3032 le32_to_cpu(desc->offset_bytes) +
3033 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3035 switch (desc->fw_type) {
3036 case PSP_FW_TYPE_PSP_SOS:
3037 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3038 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3039 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3040 psp->sos.start_addr = ucode_start_addr;
3042 case PSP_FW_TYPE_PSP_SYS_DRV:
3043 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3044 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3045 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3046 psp->sys.start_addr = ucode_start_addr;
3048 case PSP_FW_TYPE_PSP_KDB:
3049 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3050 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3051 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3052 psp->kdb.start_addr = ucode_start_addr;
3054 case PSP_FW_TYPE_PSP_TOC:
3055 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3056 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3057 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3058 psp->toc.start_addr = ucode_start_addr;
3060 case PSP_FW_TYPE_PSP_SPL:
3061 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3062 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3063 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3064 psp->spl.start_addr = ucode_start_addr;
3066 case PSP_FW_TYPE_PSP_RL:
3067 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3068 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3069 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3070 psp->rl.start_addr = ucode_start_addr;
3072 case PSP_FW_TYPE_PSP_SOC_DRV:
3073 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3074 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3075 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3076 psp->soc_drv.start_addr = ucode_start_addr;
3078 case PSP_FW_TYPE_PSP_INTF_DRV:
3079 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3080 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3081 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3082 psp->intf_drv.start_addr = ucode_start_addr;
3084 case PSP_FW_TYPE_PSP_DBG_DRV:
3085 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3086 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3087 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3088 psp->dbg_drv.start_addr = ucode_start_addr;
3090 case PSP_FW_TYPE_PSP_RAS_DRV:
3091 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3092 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3093 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3094 psp->ras_drv.start_addr = ucode_start_addr;
3097 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3104 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3106 const struct psp_firmware_header_v1_0 *sos_hdr;
3107 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3108 uint8_t *ucode_array_start_addr;
3110 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3111 ucode_array_start_addr = (uint8_t *)sos_hdr +
3112 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3114 if (adev->gmc.xgmi.connected_to_cpu ||
3115 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3116 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3117 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3119 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3120 adev->psp.sys.start_addr = ucode_array_start_addr;
3122 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3123 adev->psp.sos.start_addr = ucode_array_start_addr +
3124 le32_to_cpu(sos_hdr->sos.offset_bytes);
3126 /* Load alternate PSP SOS FW */
3127 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3129 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3130 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3132 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3133 adev->psp.sys.start_addr = ucode_array_start_addr +
3134 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3136 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3137 adev->psp.sos.start_addr = ucode_array_start_addr +
3138 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3141 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3142 dev_warn(adev->dev, "PSP SOS FW not available");
3149 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3151 struct amdgpu_device *adev = psp->adev;
3152 char fw_name[PSP_FW_NAME_LEN];
3153 const struct psp_firmware_header_v1_0 *sos_hdr;
3154 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3155 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3156 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3157 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3159 uint8_t *ucode_array_start_addr;
3162 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3163 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3167 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3168 ucode_array_start_addr = (uint8_t *)sos_hdr +
3169 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3170 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3172 switch (sos_hdr->header.header_version_major) {
3174 err = psp_init_sos_base_fw(adev);
3178 if (sos_hdr->header.header_version_minor == 1) {
3179 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3180 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3181 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3182 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3183 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3184 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3185 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3187 if (sos_hdr->header.header_version_minor == 2) {
3188 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3189 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3190 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3191 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3193 if (sos_hdr->header.header_version_minor == 3) {
3194 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3195 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3196 adev->psp.toc.start_addr = ucode_array_start_addr +
3197 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3198 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3199 adev->psp.kdb.start_addr = ucode_array_start_addr +
3200 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3201 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3202 adev->psp.spl.start_addr = ucode_array_start_addr +
3203 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3204 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3205 adev->psp.rl.start_addr = ucode_array_start_addr +
3206 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3210 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3212 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3213 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3218 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3219 err = parse_sos_bin_descriptor(psp,
3220 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3228 "unsupported psp sos firmware\n");
3235 amdgpu_ucode_release(&adev->psp.sos_fw);
3240 static int parse_ta_bin_descriptor(struct psp_context *psp,
3241 const struct psp_fw_bin_desc *desc,
3242 const struct ta_firmware_header_v2_0 *ta_hdr)
3244 uint8_t *ucode_start_addr = NULL;
3246 if (!psp || !desc || !ta_hdr)
3249 ucode_start_addr = (uint8_t *)ta_hdr +
3250 le32_to_cpu(desc->offset_bytes) +
3251 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3253 switch (desc->fw_type) {
3254 case TA_FW_TYPE_PSP_ASD:
3255 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3256 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3257 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3258 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3260 case TA_FW_TYPE_PSP_XGMI:
3261 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3262 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3263 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3265 case TA_FW_TYPE_PSP_RAS:
3266 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3267 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3268 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3270 case TA_FW_TYPE_PSP_HDCP:
3271 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3272 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3273 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3275 case TA_FW_TYPE_PSP_DTM:
3276 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3277 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3278 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3280 case TA_FW_TYPE_PSP_RAP:
3281 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3282 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3283 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3285 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3286 psp->securedisplay_context.context.bin_desc.fw_version =
3287 le32_to_cpu(desc->fw_version);
3288 psp->securedisplay_context.context.bin_desc.size_bytes =
3289 le32_to_cpu(desc->size_bytes);
3290 psp->securedisplay_context.context.bin_desc.start_addr =
3294 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3301 static int parse_ta_v1_microcode(struct psp_context *psp)
3303 const struct ta_firmware_header_v1_0 *ta_hdr;
3304 struct amdgpu_device *adev = psp->adev;
3306 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3308 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3311 adev->psp.xgmi_context.context.bin_desc.fw_version =
3312 le32_to_cpu(ta_hdr->xgmi.fw_version);
3313 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3314 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3315 adev->psp.xgmi_context.context.bin_desc.start_addr =
3317 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3319 adev->psp.ras_context.context.bin_desc.fw_version =
3320 le32_to_cpu(ta_hdr->ras.fw_version);
3321 adev->psp.ras_context.context.bin_desc.size_bytes =
3322 le32_to_cpu(ta_hdr->ras.size_bytes);
3323 adev->psp.ras_context.context.bin_desc.start_addr =
3324 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3325 le32_to_cpu(ta_hdr->ras.offset_bytes);
3327 adev->psp.hdcp_context.context.bin_desc.fw_version =
3328 le32_to_cpu(ta_hdr->hdcp.fw_version);
3329 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3330 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3331 adev->psp.hdcp_context.context.bin_desc.start_addr =
3333 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3335 adev->psp.dtm_context.context.bin_desc.fw_version =
3336 le32_to_cpu(ta_hdr->dtm.fw_version);
3337 adev->psp.dtm_context.context.bin_desc.size_bytes =
3338 le32_to_cpu(ta_hdr->dtm.size_bytes);
3339 adev->psp.dtm_context.context.bin_desc.start_addr =
3340 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3341 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3343 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3344 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3345 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3346 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3347 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3348 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3349 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3351 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3356 static int parse_ta_v2_microcode(struct psp_context *psp)
3358 const struct ta_firmware_header_v2_0 *ta_hdr;
3359 struct amdgpu_device *adev = psp->adev;
3363 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3365 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3368 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3369 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3373 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3374 err = parse_ta_bin_descriptor(psp,
3375 &ta_hdr->ta_fw_bin[ta_index],
3384 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3386 const struct common_firmware_header *hdr;
3387 struct amdgpu_device *adev = psp->adev;
3388 char fw_name[PSP_FW_NAME_LEN];
3391 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3392 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3396 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3397 switch (le16_to_cpu(hdr->header_version_major)) {
3399 err = parse_ta_v1_microcode(psp);
3402 err = parse_ta_v2_microcode(psp);
3405 dev_err(adev->dev, "unsupported TA header version\n");
3410 amdgpu_ucode_release(&adev->psp.ta_fw);
3415 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3417 struct amdgpu_device *adev = psp->adev;
3418 char fw_name[PSP_FW_NAME_LEN];
3419 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3420 struct amdgpu_firmware_info *info = NULL;
3423 if (!amdgpu_sriov_vf(adev)) {
3424 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3428 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3429 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3431 if (err == -ENODEV) {
3432 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3436 dev_err(adev->dev, "fail to initialize cap microcode\n");
3439 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3440 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3441 info->fw = adev->psp.cap_fw;
3442 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3443 adev->psp.cap_fw->data;
3444 adev->firmware.fw_size += ALIGN(
3445 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3446 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3447 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3448 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3453 amdgpu_ucode_release(&adev->psp.cap_fw);
3457 static int psp_set_clockgating_state(void *handle,
3458 enum amd_clockgating_state state)
3463 static int psp_set_powergating_state(void *handle,
3464 enum amd_powergating_state state)
3469 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3470 struct device_attribute *attr,
3473 struct drm_device *ddev = dev_get_drvdata(dev);
3474 struct amdgpu_device *adev = drm_to_adev(ddev);
3478 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3479 DRM_INFO("PSP block is not ready yet.");
3483 mutex_lock(&adev->psp.mutex);
3484 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3485 mutex_unlock(&adev->psp.mutex);
3488 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3492 return sysfs_emit(buf, "%x\n", fw_ver);
3495 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3496 struct device_attribute *attr,
3500 struct drm_device *ddev = dev_get_drvdata(dev);
3501 struct amdgpu_device *adev = drm_to_adev(ddev);
3504 const struct firmware *usbc_pd_fw;
3505 struct amdgpu_bo *fw_buf_bo = NULL;
3506 uint64_t fw_pri_mc_addr;
3507 void *fw_pri_cpu_addr;
3509 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3510 DRM_INFO("PSP block is not ready yet.");
3514 if (!drm_dev_enter(ddev, &idx))
3517 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3518 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3522 /* LFB address which is aligned to 1MB boundary per PSP request */
3523 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3524 AMDGPU_GEM_DOMAIN_VRAM |
3525 AMDGPU_GEM_DOMAIN_GTT,
3526 &fw_buf_bo, &fw_pri_mc_addr,
3531 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3533 mutex_lock(&adev->psp.mutex);
3534 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3535 mutex_unlock(&adev->psp.mutex);
3537 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3540 release_firmware(usbc_pd_fw);
3543 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3551 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3555 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3558 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3559 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3566 * Reading from this file will retrieve the USB-C PD firmware version. Writing to
3567 * this file will trigger the update process.
3569 static DEVICE_ATTR(usbc_pd_fw, 0644,
3570 psp_usbc_pd_fw_sysfs_read,
3571 psp_usbc_pd_fw_sysfs_write);
3573 int is_psp_fw_valid(struct psp_bin_desc bin)
3575 return bin.size_bytes;
3578 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3579 struct bin_attribute *bin_attr,
3580 char *buffer, loff_t pos, size_t count)
3582 struct device *dev = kobj_to_dev(kobj);
3583 struct drm_device *ddev = dev_get_drvdata(dev);
3584 struct amdgpu_device *adev = drm_to_adev(ddev);
3586 adev->psp.vbflash_done = false;
3588 /* Safeguard against memory drain */
3589 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3590 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3591 kvfree(adev->psp.vbflash_tmp_buf);
3592 adev->psp.vbflash_tmp_buf = NULL;
3593 adev->psp.vbflash_image_size = 0;
3597 /* TODO Just allocate max for now and optimize to realloc later if needed */
3598 if (!adev->psp.vbflash_tmp_buf) {
3599 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3600 if (!adev->psp.vbflash_tmp_buf)
3604 mutex_lock(&adev->psp.mutex);
3605 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3606 adev->psp.vbflash_image_size += count;
3607 mutex_unlock(&adev->psp.mutex);
3609 dev_dbg(adev->dev, "IFWI staged for update");
3614 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3615 struct bin_attribute *bin_attr, char *buffer,
3616 loff_t pos, size_t count)
3618 struct device *dev = kobj_to_dev(kobj);
3619 struct drm_device *ddev = dev_get_drvdata(dev);
3620 struct amdgpu_device *adev = drm_to_adev(ddev);
3621 struct amdgpu_bo *fw_buf_bo = NULL;
3622 uint64_t fw_pri_mc_addr;
3623 void *fw_pri_cpu_addr;
3626 if (adev->psp.vbflash_image_size == 0)
3629 dev_dbg(adev->dev, "PSP IFWI flash process initiated");
3631 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3632 AMDGPU_GPU_PAGE_SIZE,
3633 AMDGPU_GEM_DOMAIN_VRAM,
3640 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3642 mutex_lock(&adev->psp.mutex);
3643 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3644 mutex_unlock(&adev->psp.mutex);
3646 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3649 kvfree(adev->psp.vbflash_tmp_buf);
3650 adev->psp.vbflash_tmp_buf = NULL;
3651 adev->psp.vbflash_image_size = 0;
3654 dev_err(adev->dev, "Failed to load IFWI, err = %d", ret);
3658 dev_dbg(adev->dev, "PSP IFWI flash process done");
3664 * Writing to this file will stage an IFWI for update. Reading from this file
3665 * will trigger the update process.
3667 static struct bin_attribute psp_vbflash_bin_attr = {
3668 .attr = {.name = "psp_vbflash", .mode = 0660},
3670 .write = amdgpu_psp_vbflash_write,
3671 .read = amdgpu_psp_vbflash_read,
3675 * DOC: psp_vbflash_status
3676 * The status of the flash process.
3677 * 0: IFWI flash not complete.
3678 * 1: IFWI flash complete.
3680 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3681 struct device_attribute *attr,
3684 struct drm_device *ddev = dev_get_drvdata(dev);
3685 struct amdgpu_device *adev = drm_to_adev(ddev);
3686 uint32_t vbflash_status;
3688 vbflash_status = psp_vbflash_status(&adev->psp);
3689 if (!adev->psp.vbflash_done)
3691 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3694 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3696 static DEVICE_ATTR(psp_vbflash_status, 0440, amdgpu_psp_vbflash_status, NULL);
3698 static struct bin_attribute *bin_flash_attrs[] = {
3699 &psp_vbflash_bin_attr,
3703 static struct attribute *flash_attrs[] = {
3704 &dev_attr_psp_vbflash_status.attr,
3705 &dev_attr_usbc_pd_fw.attr,
3709 static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
3711 struct device *dev = kobj_to_dev(kobj);
3712 struct drm_device *ddev = dev_get_drvdata(dev);
3713 struct amdgpu_device *adev = drm_to_adev(ddev);
3715 if (attr == &dev_attr_usbc_pd_fw.attr)
3716 return adev->psp.sup_pd_fw_up ? 0660 : 0;
3718 return adev->psp.sup_ifwi_up ? 0440 : 0;
3721 static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
3722 struct bin_attribute *attr,
3725 struct device *dev = kobj_to_dev(kobj);
3726 struct drm_device *ddev = dev_get_drvdata(dev);
3727 struct amdgpu_device *adev = drm_to_adev(ddev);
3729 return adev->psp.sup_ifwi_up ? 0660 : 0;
3732 const struct attribute_group amdgpu_flash_attr_group = {
3733 .attrs = flash_attrs,
3734 .bin_attrs = bin_flash_attrs,
3735 .is_bin_visible = amdgpu_bin_flash_attr_is_visible,
3736 .is_visible = amdgpu_flash_attr_is_visible,
3739 const struct amd_ip_funcs psp_ip_funcs = {
3741 .early_init = psp_early_init,
3743 .sw_init = psp_sw_init,
3744 .sw_fini = psp_sw_fini,
3745 .hw_init = psp_hw_init,
3746 .hw_fini = psp_hw_fini,
3747 .suspend = psp_suspend,
3748 .resume = psp_resume,
3750 .check_soft_reset = NULL,
3751 .wait_for_idle = NULL,
3753 .set_clockgating_state = psp_set_clockgating_state,
3754 .set_powergating_state = psp_set_powergating_state,
3757 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3758 .type = AMD_IP_BLOCK_TYPE_PSP,
3762 .funcs = &psp_ip_funcs,
3765 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3766 .type = AMD_IP_BLOCK_TYPE_PSP,
3770 .funcs = &psp_ip_funcs,
3773 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3774 .type = AMD_IP_BLOCK_TYPE_PSP,
3778 .funcs = &psp_ip_funcs,
3781 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3782 .type = AMD_IP_BLOCK_TYPE_PSP,
3786 .funcs = &psp_ip_funcs,
3789 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
3790 .type = AMD_IP_BLOCK_TYPE_PSP,
3794 .funcs = &psp_ip_funcs,
3797 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3798 .type = AMD_IP_BLOCK_TYPE_PSP,
3802 .funcs = &psp_ip_funcs,
3805 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3806 .type = AMD_IP_BLOCK_TYPE_PSP,
3810 .funcs = &psp_ip_funcs,