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"
41 #include "psp_v14_0.h"
43 #include "amdgpu_ras.h"
44 #include "amdgpu_securedisplay.h"
45 #include "amdgpu_atomfirmware.h"
47 #define AMD_VBIOS_FILE_MAX_SIZE_B (1024*1024*3)
49 static int psp_load_smu_fw(struct psp_context *psp);
50 static int psp_rap_terminate(struct psp_context *psp);
51 static int psp_securedisplay_terminate(struct psp_context *psp);
53 static int psp_ring_init(struct psp_context *psp,
54 enum psp_ring_type ring_type)
57 struct psp_ring *ring;
58 struct amdgpu_device *adev = psp->adev;
62 ring->ring_type = ring_type;
64 /* allocate 4k Page of Local Frame Buffer memory for ring */
65 ring->ring_size = 0x1000;
66 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
67 AMDGPU_GEM_DOMAIN_VRAM |
68 AMDGPU_GEM_DOMAIN_GTT,
70 &ring->ring_mem_mc_addr,
71 (void **)&ring->ring_mem);
81 * Due to DF Cstate management centralized to PMFW, the firmware
82 * loading sequence will be updated as below:
88 * - Load other non-psp fw
90 * - Load XGMI/RAS/HDCP/DTM TA if any
92 * This new sequence is required for
93 * - Arcturus and onwards
95 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
97 struct amdgpu_device *adev = psp->adev;
99 if (amdgpu_sriov_vf(adev)) {
100 psp->pmfw_centralized_cstate_management = false;
104 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
105 case IP_VERSION(11, 0, 0):
106 case IP_VERSION(11, 0, 4):
107 case IP_VERSION(11, 0, 5):
108 case IP_VERSION(11, 0, 7):
109 case IP_VERSION(11, 0, 9):
110 case IP_VERSION(11, 0, 11):
111 case IP_VERSION(11, 0, 12):
112 case IP_VERSION(11, 0, 13):
113 case IP_VERSION(13, 0, 0):
114 case IP_VERSION(13, 0, 2):
115 case IP_VERSION(13, 0, 7):
116 psp->pmfw_centralized_cstate_management = true;
119 psp->pmfw_centralized_cstate_management = false;
124 static int psp_init_sriov_microcode(struct psp_context *psp)
126 struct amdgpu_device *adev = psp->adev;
127 char ucode_prefix[30];
130 amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
132 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
133 case IP_VERSION(9, 0, 0):
134 case IP_VERSION(11, 0, 7):
135 case IP_VERSION(11, 0, 9):
136 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
137 ret = psp_init_cap_microcode(psp, ucode_prefix);
139 case IP_VERSION(13, 0, 2):
140 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
141 ret = psp_init_cap_microcode(psp, ucode_prefix);
142 ret &= psp_init_ta_microcode(psp, ucode_prefix);
144 case IP_VERSION(13, 0, 0):
145 adev->virt.autoload_ucode_id = 0;
147 case IP_VERSION(13, 0, 6):
148 ret = psp_init_cap_microcode(psp, ucode_prefix);
149 ret &= psp_init_ta_microcode(psp, ucode_prefix);
151 case IP_VERSION(13, 0, 10):
152 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
153 ret = psp_init_cap_microcode(psp, ucode_prefix);
161 static int psp_early_init(void *handle)
163 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
164 struct psp_context *psp = &adev->psp;
166 psp->autoload_supported = true;
167 psp->boot_time_tmr = true;
169 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
170 case IP_VERSION(9, 0, 0):
171 psp_v3_1_set_psp_funcs(psp);
172 psp->autoload_supported = false;
173 psp->boot_time_tmr = false;
175 case IP_VERSION(10, 0, 0):
176 case IP_VERSION(10, 0, 1):
177 psp_v10_0_set_psp_funcs(psp);
178 psp->autoload_supported = false;
179 psp->boot_time_tmr = false;
181 case IP_VERSION(11, 0, 2):
182 case IP_VERSION(11, 0, 4):
183 psp_v11_0_set_psp_funcs(psp);
184 psp->autoload_supported = false;
185 psp->boot_time_tmr = false;
187 case IP_VERSION(11, 0, 0):
188 case IP_VERSION(11, 0, 7):
189 adev->psp.sup_pd_fw_up = !amdgpu_sriov_vf(adev);
191 case IP_VERSION(11, 0, 5):
192 case IP_VERSION(11, 0, 9):
193 case IP_VERSION(11, 0, 11):
194 case IP_VERSION(11, 5, 0):
195 case IP_VERSION(11, 0, 12):
196 case IP_VERSION(11, 0, 13):
197 psp_v11_0_set_psp_funcs(psp);
198 psp->boot_time_tmr = false;
200 case IP_VERSION(11, 0, 3):
201 case IP_VERSION(12, 0, 1):
202 psp_v12_0_set_psp_funcs(psp);
203 psp->autoload_supported = false;
204 psp->boot_time_tmr = false;
206 case IP_VERSION(13, 0, 2):
207 psp->boot_time_tmr = false;
209 case IP_VERSION(13, 0, 6):
210 psp_v13_0_set_psp_funcs(psp);
211 psp->autoload_supported = false;
213 case IP_VERSION(13, 0, 1):
214 case IP_VERSION(13, 0, 3):
215 case IP_VERSION(13, 0, 5):
216 case IP_VERSION(13, 0, 8):
217 case IP_VERSION(13, 0, 11):
218 case IP_VERSION(14, 0, 0):
219 case IP_VERSION(14, 0, 1):
220 psp_v13_0_set_psp_funcs(psp);
221 psp->boot_time_tmr = false;
223 case IP_VERSION(11, 0, 8):
224 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
225 psp_v11_0_8_set_psp_funcs(psp);
227 psp->autoload_supported = false;
228 psp->boot_time_tmr = false;
230 case IP_VERSION(13, 0, 0):
231 case IP_VERSION(13, 0, 7):
232 case IP_VERSION(13, 0, 10):
233 psp_v13_0_set_psp_funcs(psp);
234 adev->psp.sup_ifwi_up = !amdgpu_sriov_vf(adev);
235 psp->boot_time_tmr = false;
237 case IP_VERSION(13, 0, 4):
238 psp_v13_0_4_set_psp_funcs(psp);
239 psp->boot_time_tmr = false;
241 case IP_VERSION(14, 0, 2):
242 case IP_VERSION(14, 0, 3):
243 psp_v14_0_set_psp_funcs(psp);
251 adev->psp_timeout = 20000;
253 psp_check_pmfw_centralized_cstate_management(psp);
255 if (amdgpu_sriov_vf(adev))
256 return psp_init_sriov_microcode(psp);
258 return psp_init_microcode(psp);
261 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
263 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
264 &mem_ctx->shared_buf);
265 mem_ctx->shared_bo = NULL;
268 static void psp_free_shared_bufs(struct psp_context *psp)
273 /* free TMR memory buffer */
274 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
275 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
278 /* free xgmi shared memory */
279 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
281 /* free ras shared memory */
282 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
284 /* free hdcp shared memory */
285 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
287 /* free dtm shared memory */
288 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
290 /* free rap shared memory */
291 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
293 /* free securedisplay shared memory */
294 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
299 static void psp_memory_training_fini(struct psp_context *psp)
301 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
303 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
304 kfree(ctx->sys_cache);
305 ctx->sys_cache = NULL;
308 static int psp_memory_training_init(struct psp_context *psp)
311 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
313 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
314 dev_dbg(psp->adev->dev, "memory training is not supported!\n");
318 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
319 if (ctx->sys_cache == NULL) {
320 dev_err(psp->adev->dev, "alloc mem_train_ctx.sys_cache failed!\n");
325 dev_dbg(psp->adev->dev,
326 "train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
327 ctx->train_data_size,
328 ctx->p2c_train_data_offset,
329 ctx->c2p_train_data_offset);
330 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
334 psp_memory_training_fini(psp);
339 * Helper funciton to query psp runtime database entry
341 * @adev: amdgpu_device pointer
342 * @entry_type: the type of psp runtime database entry
343 * @db_entry: runtime database entry pointer
345 * Return false if runtime database doesn't exit or entry is invalid
346 * or true if the specific database entry is found, and copy to @db_entry
348 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
349 enum psp_runtime_entry_type entry_type,
352 uint64_t db_header_pos, db_dir_pos;
353 struct psp_runtime_data_header db_header = {0};
354 struct psp_runtime_data_directory db_dir = {0};
358 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 6))
361 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
362 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
364 /* read runtime db header from vram */
365 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
366 sizeof(struct psp_runtime_data_header), false);
368 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
369 /* runtime db doesn't exist, exit */
370 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
374 /* read runtime database entry from vram */
375 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
376 sizeof(struct psp_runtime_data_directory), false);
378 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
379 /* invalid db entry count, exit */
380 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
384 /* look up for requested entry type */
385 for (i = 0; i < db_dir.entry_count && !ret; i++) {
386 if (db_dir.entry_list[i].entry_type == entry_type) {
387 switch (entry_type) {
388 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
389 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
390 /* invalid db entry size */
391 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
394 /* read runtime database entry */
395 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
396 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
399 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
400 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
401 /* invalid db entry size */
402 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
405 /* read runtime database entry */
406 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
407 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
420 static int psp_sw_init(void *handle)
422 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
423 struct psp_context *psp = &adev->psp;
425 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
426 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
427 struct psp_runtime_scpm_entry scpm_entry;
429 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
431 dev_err(adev->dev, "Failed to allocate memory to command buffer!\n");
435 adev->psp.xgmi_context.supports_extended_data =
436 !adev->gmc.xgmi.connected_to_cpu &&
437 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 2);
439 memset(&scpm_entry, 0, sizeof(scpm_entry));
440 if ((psp_get_runtime_db_entry(adev,
441 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
443 (scpm_entry.scpm_status != SCPM_DISABLE)) {
444 adev->scpm_enabled = true;
445 adev->scpm_status = scpm_entry.scpm_status;
447 adev->scpm_enabled = false;
448 adev->scpm_status = SCPM_DISABLE;
451 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
453 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
454 if (psp_get_runtime_db_entry(adev,
455 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
457 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
458 if ((psp->boot_cfg_bitmask) &
459 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
460 /* If psp runtime database exists, then
461 * only enable two stage memory training
462 * when TWO_STAGE_DRAM_TRAINING bit is set
463 * in runtime database
465 mem_training_ctx->enable_mem_training = true;
469 /* If psp runtime database doesn't exist or is
470 * invalid, force enable two stage memory training
472 mem_training_ctx->enable_mem_training = true;
475 if (mem_training_ctx->enable_mem_training) {
476 ret = psp_memory_training_init(psp);
478 dev_err(adev->dev, "Failed to initialize memory training!\n");
482 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
484 dev_err(adev->dev, "Failed to process memory training!\n");
489 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
490 (amdgpu_sriov_vf(adev) || adev->debug_use_vram_fw_buf) ?
491 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
493 &psp->fw_pri_mc_addr,
498 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
499 AMDGPU_GEM_DOMAIN_VRAM |
500 AMDGPU_GEM_DOMAIN_GTT,
502 &psp->fence_buf_mc_addr,
507 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
508 AMDGPU_GEM_DOMAIN_VRAM |
509 AMDGPU_GEM_DOMAIN_GTT,
510 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
511 (void **)&psp->cmd_buf_mem);
518 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
519 &psp->fence_buf_mc_addr, &psp->fence_buf);
521 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
522 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
526 static int psp_sw_fini(void *handle)
528 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
529 struct psp_context *psp = &adev->psp;
530 struct psp_gfx_cmd_resp *cmd = psp->cmd;
532 psp_memory_training_fini(psp);
534 amdgpu_ucode_release(&psp->sos_fw);
535 amdgpu_ucode_release(&psp->asd_fw);
536 amdgpu_ucode_release(&psp->ta_fw);
537 amdgpu_ucode_release(&psp->cap_fw);
538 amdgpu_ucode_release(&psp->toc_fw);
543 psp_free_shared_bufs(psp);
545 if (psp->km_ring.ring_mem)
546 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
547 &psp->km_ring.ring_mem_mc_addr,
548 (void **)&psp->km_ring.ring_mem);
550 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
551 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
552 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
553 &psp->fence_buf_mc_addr, &psp->fence_buf);
554 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
555 (void **)&psp->cmd_buf_mem);
560 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
561 uint32_t reg_val, uint32_t mask, bool check_changed)
565 struct amdgpu_device *adev = psp->adev;
567 if (psp->adev->no_hw_access)
570 for (i = 0; i < adev->usec_timeout; i++) {
571 val = RREG32(reg_index);
576 if ((val & mask) == reg_val)
585 int psp_wait_for_spirom_update(struct psp_context *psp, uint32_t reg_index,
586 uint32_t reg_val, uint32_t mask, uint32_t msec_timeout)
590 struct amdgpu_device *adev = psp->adev;
592 if (psp->adev->no_hw_access)
595 for (i = 0; i < msec_timeout; i++) {
596 val = RREG32(reg_index);
597 if ((val & mask) == reg_val)
605 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
608 case GFX_CMD_ID_LOAD_TA:
610 case GFX_CMD_ID_UNLOAD_TA:
612 case GFX_CMD_ID_INVOKE_CMD:
614 case GFX_CMD_ID_LOAD_ASD:
616 case GFX_CMD_ID_SETUP_TMR:
618 case GFX_CMD_ID_LOAD_IP_FW:
620 case GFX_CMD_ID_DESTROY_TMR:
621 return "DESTROY_TMR";
622 case GFX_CMD_ID_SAVE_RESTORE:
623 return "SAVE_RESTORE_IP_FW";
624 case GFX_CMD_ID_SETUP_VMR:
626 case GFX_CMD_ID_DESTROY_VMR:
627 return "DESTROY_VMR";
628 case GFX_CMD_ID_PROG_REG:
630 case GFX_CMD_ID_GET_FW_ATTESTATION:
631 return "GET_FW_ATTESTATION";
632 case GFX_CMD_ID_LOAD_TOC:
633 return "ID_LOAD_TOC";
634 case GFX_CMD_ID_AUTOLOAD_RLC:
635 return "AUTOLOAD_RLC";
636 case GFX_CMD_ID_BOOT_CFG:
639 return "UNKNOWN CMD";
643 static bool psp_err_warn(struct psp_context *psp)
645 struct psp_gfx_cmd_resp *cmd = psp->cmd_buf_mem;
647 /* This response indicates reg list is already loaded */
648 if (amdgpu_ip_version(psp->adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 2) &&
649 cmd->cmd_id == GFX_CMD_ID_LOAD_IP_FW &&
650 cmd->cmd.cmd_load_ip_fw.fw_type == GFX_FW_TYPE_REG_LIST &&
651 cmd->resp.status == TEE_ERROR_CANCEL)
658 psp_cmd_submit_buf(struct psp_context *psp,
659 struct amdgpu_firmware_info *ucode,
660 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
664 int timeout = psp->adev->psp_timeout;
665 bool ras_intr = false;
666 bool skip_unsupport = false;
668 if (psp->adev->no_hw_access)
671 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
673 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
675 index = atomic_inc_return(&psp->fence_value);
676 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
678 atomic_dec(&psp->fence_value);
682 amdgpu_device_invalidate_hdp(psp->adev, NULL);
683 while (*((unsigned int *)psp->fence_buf) != index) {
687 * Shouldn't wait for timeout when err_event_athub occurs,
688 * because gpu reset thread triggered and lock resource should
689 * be released for psp resume sequence.
691 ras_intr = amdgpu_ras_intr_triggered();
694 usleep_range(10, 100);
695 amdgpu_device_invalidate_hdp(psp->adev, NULL);
698 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
699 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
700 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
702 memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
704 /* In some cases, psp response status is not 0 even there is no
705 * problem while the command is submitted. Some version of PSP FW
706 * doesn't write 0 to that field.
707 * So here we would like to only print a warning instead of an error
708 * during psp initialization to avoid breaking hw_init and it doesn't
711 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
713 dev_warn(psp->adev->dev,
714 "failed to load ucode %s(0x%X) ",
715 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
716 if (psp_err_warn(psp))
719 "psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
720 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id),
721 psp->cmd_buf_mem->cmd_id,
722 psp->cmd_buf_mem->resp.status);
723 /* If any firmware (including CAP) load fails under SRIOV, it should
724 * return failure to stop the VF from initializing.
725 * Also return failure in case of timeout
727 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
734 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
735 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
742 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
744 struct psp_gfx_cmd_resp *cmd = psp->cmd;
746 mutex_lock(&psp->mutex);
748 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
753 static void release_psp_cmd_buf(struct psp_context *psp)
755 mutex_unlock(&psp->mutex);
758 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
759 struct psp_gfx_cmd_resp *cmd,
760 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
762 struct amdgpu_device *adev = psp->adev;
767 size = amdgpu_bo_size(tmr_bo);
768 tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
771 if (amdgpu_sriov_vf(psp->adev))
772 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
774 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
775 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
776 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
777 cmd->cmd.cmd_setup_tmr.buf_size = size;
778 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
779 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
780 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
783 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
784 uint64_t pri_buf_mc, uint32_t size)
786 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
787 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
788 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
789 cmd->cmd.cmd_load_toc.toc_size = size;
792 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
793 static int psp_load_toc(struct psp_context *psp,
797 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
799 /* Copy toc to psp firmware private buffer */
800 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
802 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
804 ret = psp_cmd_submit_buf(psp, NULL, cmd,
805 psp->fence_buf_mc_addr);
807 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
809 release_psp_cmd_buf(psp);
814 /* Set up Trusted Memory Region */
815 static int psp_tmr_init(struct psp_context *psp)
823 * According to HW engineer, they prefer the TMR address be "naturally
824 * aligned" , e.g. the start address be an integer divide of TMR size.
826 * Note: this memory need be reserved till the driver
829 tmr_size = PSP_TMR_SIZE(psp->adev);
831 /* For ASICs support RLC autoload, psp will parse the toc
832 * and calculate the total size of TMR needed
834 if (!amdgpu_sriov_vf(psp->adev) &&
835 psp->toc.start_addr &&
836 psp->toc.size_bytes &&
838 ret = psp_load_toc(psp, &tmr_size);
840 dev_err(psp->adev->dev, "Failed to load toc\n");
845 if (!psp->tmr_bo && !psp->boot_time_tmr) {
846 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
847 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
849 AMDGPU_HAS_VRAM(psp->adev) ?
850 AMDGPU_GEM_DOMAIN_VRAM :
851 AMDGPU_GEM_DOMAIN_GTT,
852 &psp->tmr_bo, &psp->tmr_mc_addr,
859 static bool psp_skip_tmr(struct psp_context *psp)
861 switch (amdgpu_ip_version(psp->adev, MP0_HWIP, 0)) {
862 case IP_VERSION(11, 0, 9):
863 case IP_VERSION(11, 0, 7):
864 case IP_VERSION(13, 0, 2):
865 case IP_VERSION(13, 0, 6):
866 case IP_VERSION(13, 0, 10):
873 static int psp_tmr_load(struct psp_context *psp)
876 struct psp_gfx_cmd_resp *cmd;
878 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
879 * Already set up by host driver.
881 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
884 cmd = acquire_psp_cmd_buf(psp);
886 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
888 dev_info(psp->adev->dev, "reserve 0x%lx from 0x%llx for PSP TMR\n",
889 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
891 ret = psp_cmd_submit_buf(psp, NULL, cmd,
892 psp->fence_buf_mc_addr);
894 release_psp_cmd_buf(psp);
899 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
900 struct psp_gfx_cmd_resp *cmd)
902 if (amdgpu_sriov_vf(psp->adev))
903 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
905 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
908 static int psp_tmr_unload(struct psp_context *psp)
911 struct psp_gfx_cmd_resp *cmd;
913 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
914 * as TMR is not loaded at all
916 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
919 cmd = acquire_psp_cmd_buf(psp);
921 psp_prep_tmr_unload_cmd_buf(psp, cmd);
922 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
924 ret = psp_cmd_submit_buf(psp, NULL, cmd,
925 psp->fence_buf_mc_addr);
927 release_psp_cmd_buf(psp);
932 static int psp_tmr_terminate(struct psp_context *psp)
934 return psp_tmr_unload(psp);
937 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
938 uint64_t *output_ptr)
941 struct psp_gfx_cmd_resp *cmd;
946 if (amdgpu_sriov_vf(psp->adev))
949 cmd = acquire_psp_cmd_buf(psp);
951 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
953 ret = psp_cmd_submit_buf(psp, NULL, cmd,
954 psp->fence_buf_mc_addr);
957 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
958 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
961 release_psp_cmd_buf(psp);
966 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
968 struct psp_context *psp = &adev->psp;
969 struct psp_gfx_cmd_resp *cmd;
972 if (amdgpu_sriov_vf(adev))
975 cmd = acquire_psp_cmd_buf(psp);
977 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
978 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
980 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
983 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
986 release_psp_cmd_buf(psp);
991 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
994 struct psp_context *psp = &adev->psp;
995 struct psp_gfx_cmd_resp *cmd;
997 if (amdgpu_sriov_vf(adev))
1000 cmd = acquire_psp_cmd_buf(psp);
1002 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
1003 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
1004 cmd->cmd.boot_cfg.boot_config = boot_cfg;
1005 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
1007 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1009 release_psp_cmd_buf(psp);
1014 static int psp_rl_load(struct amdgpu_device *adev)
1017 struct psp_context *psp = &adev->psp;
1018 struct psp_gfx_cmd_resp *cmd;
1020 if (!is_psp_fw_valid(psp->rl))
1023 cmd = acquire_psp_cmd_buf(psp);
1025 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
1026 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
1028 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
1029 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
1030 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
1031 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
1032 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
1034 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1036 release_psp_cmd_buf(psp);
1041 int psp_spatial_partition(struct psp_context *psp, int mode)
1043 struct psp_gfx_cmd_resp *cmd;
1046 if (amdgpu_sriov_vf(psp->adev))
1049 cmd = acquire_psp_cmd_buf(psp);
1051 cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1052 cmd->cmd.cmd_spatial_part.mode = mode;
1054 dev_info(psp->adev->dev, "Requesting %d partitions through PSP", mode);
1055 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1057 release_psp_cmd_buf(psp);
1062 static int psp_asd_initialize(struct psp_context *psp)
1066 /* If PSP version doesn't match ASD version, asd loading will be failed.
1067 * add workaround to bypass it for sriov now.
1068 * TODO: add version check to make it common
1070 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1073 /* bypass asd if display hardware is not available */
1074 if (!amdgpu_device_has_display_hardware(psp->adev) &&
1075 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) >= IP_VERSION(13, 0, 10))
1078 psp->asd_context.mem_context.shared_mc_addr = 0;
1079 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
1080 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
1082 ret = psp_ta_load(psp, &psp->asd_context);
1084 psp->asd_context.initialized = true;
1089 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1090 uint32_t session_id)
1092 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1093 cmd->cmd.cmd_unload_ta.session_id = session_id;
1096 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1099 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1101 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1103 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1105 context->resp_status = cmd->resp.status;
1107 release_psp_cmd_buf(psp);
1112 static int psp_asd_terminate(struct psp_context *psp)
1116 if (amdgpu_sriov_vf(psp->adev))
1119 if (!psp->asd_context.initialized)
1122 ret = psp_ta_unload(psp, &psp->asd_context);
1124 psp->asd_context.initialized = false;
1129 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1130 uint32_t id, uint32_t value)
1132 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1133 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1134 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1137 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1140 struct psp_gfx_cmd_resp *cmd;
1143 if (reg >= PSP_REG_LAST)
1146 cmd = acquire_psp_cmd_buf(psp);
1148 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1149 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1151 dev_err(psp->adev->dev, "PSP failed to program reg id %d\n", reg);
1153 release_psp_cmd_buf(psp);
1158 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1160 struct ta_context *context)
1162 cmd->cmd_id = context->ta_load_type;
1163 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1164 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1165 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1167 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1168 lower_32_bits(context->mem_context.shared_mc_addr);
1169 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1170 upper_32_bits(context->mem_context.shared_mc_addr);
1171 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1174 int psp_ta_init_shared_buf(struct psp_context *psp,
1175 struct ta_mem_context *mem_ctx)
1178 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1179 * physical) for ta to host memory
1181 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1182 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1183 AMDGPU_GEM_DOMAIN_GTT,
1184 &mem_ctx->shared_bo,
1185 &mem_ctx->shared_mc_addr,
1186 &mem_ctx->shared_buf);
1189 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1191 uint32_t session_id)
1193 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1194 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1195 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1198 int psp_ta_invoke(struct psp_context *psp,
1200 struct ta_context *context)
1203 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1205 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1207 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1208 psp->fence_buf_mc_addr);
1210 context->resp_status = cmd->resp.status;
1212 release_psp_cmd_buf(psp);
1217 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1220 struct psp_gfx_cmd_resp *cmd;
1222 cmd = acquire_psp_cmd_buf(psp);
1224 psp_copy_fw(psp, context->bin_desc.start_addr,
1225 context->bin_desc.size_bytes);
1227 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1229 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1230 psp->fence_buf_mc_addr);
1232 context->resp_status = cmd->resp.status;
1235 context->session_id = cmd->resp.session_id;
1237 release_psp_cmd_buf(psp);
1242 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1244 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1247 int psp_xgmi_terminate(struct psp_context *psp)
1250 struct amdgpu_device *adev = psp->adev;
1252 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1253 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 4) ||
1254 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 2) &&
1255 adev->gmc.xgmi.connected_to_cpu))
1258 if (!psp->xgmi_context.context.initialized)
1261 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1263 psp->xgmi_context.context.initialized = false;
1268 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1270 struct ta_xgmi_shared_memory *xgmi_cmd;
1274 !psp->xgmi_context.context.bin_desc.size_bytes ||
1275 !psp->xgmi_context.context.bin_desc.start_addr)
1281 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1282 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1284 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1285 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1291 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1293 psp->xgmi_context.context.initialized = true;
1298 /* Initialize XGMI session */
1299 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1300 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1301 xgmi_cmd->flag_extend_link_record = set_extended_data;
1302 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1304 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1305 /* note down the capbility flag for XGMI TA */
1306 psp->xgmi_context.xgmi_ta_caps = xgmi_cmd->caps_flag;
1311 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1313 struct ta_xgmi_shared_memory *xgmi_cmd;
1316 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1317 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1319 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1321 /* Invoke xgmi ta to get hive id */
1322 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1326 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1331 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1333 struct ta_xgmi_shared_memory *xgmi_cmd;
1336 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1337 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1339 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1341 /* Invoke xgmi ta to get the node id */
1342 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1346 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1351 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1353 return (amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1354 IP_VERSION(13, 0, 2) &&
1355 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
1356 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) >=
1357 IP_VERSION(13, 0, 6);
1361 * Chips that support extended topology information require the driver to
1362 * reflect topology information in the opposite direction. This is
1363 * because the TA has already exceeded its link record limit and if the
1364 * TA holds bi-directional information, the driver would have to do
1365 * multiple fetches instead of just two.
1367 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1368 struct psp_xgmi_node_info node_info)
1370 struct amdgpu_device *mirror_adev;
1371 struct amdgpu_hive_info *hive;
1372 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1373 uint64_t dst_node_id = node_info.node_id;
1374 uint8_t dst_num_hops = node_info.num_hops;
1375 uint8_t dst_num_links = node_info.num_links;
1377 hive = amdgpu_get_xgmi_hive(psp->adev);
1378 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1379 struct psp_xgmi_topology_info *mirror_top_info;
1382 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1385 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1386 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1387 if (mirror_top_info->nodes[j].node_id != src_node_id)
1390 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1392 * prevent 0 num_links value re-reflection since reflection
1393 * criteria is based on num_hops (direct or indirect).
1397 mirror_top_info->nodes[j].num_links = dst_num_links;
1405 amdgpu_put_xgmi_hive(hive);
1408 int psp_xgmi_get_topology_info(struct psp_context *psp,
1410 struct psp_xgmi_topology_info *topology,
1411 bool get_extended_data)
1413 struct ta_xgmi_shared_memory *xgmi_cmd;
1414 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1415 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1419 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1422 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1423 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1424 xgmi_cmd->flag_extend_link_record = get_extended_data;
1426 /* Fill in the shared memory with topology information as input */
1427 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1428 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_TOPOLOGY_INFO;
1429 topology_info_input->num_nodes = number_devices;
1431 for (i = 0; i < topology_info_input->num_nodes; i++) {
1432 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1433 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1434 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1435 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1438 /* Invoke xgmi ta to get the topology information */
1439 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_TOPOLOGY_INFO);
1443 /* Read the output topology information from the shared memory */
1444 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1445 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1446 for (i = 0; i < topology->num_nodes; i++) {
1447 /* extended data will either be 0 or equal to non-extended data */
1448 if (topology_info_output->nodes[i].num_hops)
1449 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1451 /* non-extended data gets everything here so no need to update */
1452 if (!get_extended_data) {
1453 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1454 topology->nodes[i].is_sharing_enabled =
1455 topology_info_output->nodes[i].is_sharing_enabled;
1456 topology->nodes[i].sdma_engine =
1457 topology_info_output->nodes[i].sdma_engine;
1462 /* Invoke xgmi ta again to get the link information */
1463 if (psp_xgmi_peer_link_info_supported(psp)) {
1464 struct ta_xgmi_cmd_get_peer_link_info *link_info_output;
1465 struct ta_xgmi_cmd_get_extend_peer_link_info *link_extend_info_output;
1466 bool requires_reflection =
1467 (psp->xgmi_context.supports_extended_data &&
1468 get_extended_data) ||
1469 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1470 IP_VERSION(13, 0, 6);
1471 bool ta_port_num_support = amdgpu_sriov_vf(psp->adev) ? 0 :
1472 psp->xgmi_context.xgmi_ta_caps & EXTEND_PEER_LINK_INFO_CMD_FLAG;
1474 /* popluate the shared output buffer rather than the cmd input buffer
1475 * with node_ids as the input for GET_PEER_LINKS command execution.
1476 * This is required for GET_PEER_LINKS per xgmi ta implementation.
1477 * The same requirement for GET_EXTEND_PEER_LINKS command.
1479 if (ta_port_num_support) {
1480 link_extend_info_output = &xgmi_cmd->xgmi_out_message.get_extend_link_info;
1482 for (i = 0; i < topology->num_nodes; i++)
1483 link_extend_info_output->nodes[i].node_id = topology->nodes[i].node_id;
1485 link_extend_info_output->num_nodes = topology->num_nodes;
1486 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_EXTEND_PEER_LINKS;
1488 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1490 for (i = 0; i < topology->num_nodes; i++)
1491 link_info_output->nodes[i].node_id = topology->nodes[i].node_id;
1493 link_info_output->num_nodes = topology->num_nodes;
1494 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1497 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1501 for (i = 0; i < topology->num_nodes; i++) {
1502 uint8_t node_num_links = ta_port_num_support ?
1503 link_extend_info_output->nodes[i].num_links : link_info_output->nodes[i].num_links;
1504 /* accumulate num_links on extended data */
1505 if (get_extended_data) {
1506 topology->nodes[i].num_links = topology->nodes[i].num_links + node_num_links;
1508 topology->nodes[i].num_links = (requires_reflection && topology->nodes[i].num_links) ?
1509 topology->nodes[i].num_links : node_num_links;
1511 /* popluate the connected port num info if supported and available */
1512 if (ta_port_num_support && topology->nodes[i].num_links) {
1513 memcpy(topology->nodes[i].port_num, link_extend_info_output->nodes[i].port_num,
1514 sizeof(struct xgmi_connected_port_num) * TA_XGMI__MAX_PORT_NUM);
1517 /* reflect the topology information for bi-directionality */
1518 if (requires_reflection && topology->nodes[i].num_hops)
1519 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1526 int psp_xgmi_set_topology_info(struct psp_context *psp,
1528 struct psp_xgmi_topology_info *topology)
1530 struct ta_xgmi_shared_memory *xgmi_cmd;
1531 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1534 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1537 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1538 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1540 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1541 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1542 topology_info_input->num_nodes = number_devices;
1544 for (i = 0; i < topology_info_input->num_nodes; i++) {
1545 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1546 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1547 topology_info_input->nodes[i].is_sharing_enabled = 1;
1548 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1551 /* Invoke xgmi ta to set topology information */
1552 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1556 static void psp_ras_ta_check_status(struct psp_context *psp)
1558 struct ta_ras_shared_memory *ras_cmd =
1559 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1561 switch (ras_cmd->ras_status) {
1562 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1563 dev_warn(psp->adev->dev,
1564 "RAS WARNING: cmd failed due to unsupported ip\n");
1566 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1567 dev_warn(psp->adev->dev,
1568 "RAS WARNING: cmd failed due to unsupported error injection\n");
1570 case TA_RAS_STATUS__SUCCESS:
1572 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1573 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1574 dev_warn(psp->adev->dev,
1575 "RAS WARNING: Inject error to critical region is not allowed\n");
1578 dev_warn(psp->adev->dev,
1579 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1584 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1586 struct ta_ras_shared_memory *ras_cmd;
1589 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1592 * TODO: bypass the loading in sriov for now
1594 if (amdgpu_sriov_vf(psp->adev))
1597 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1599 if (amdgpu_ras_intr_triggered())
1602 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1603 dev_warn(psp->adev->dev, "RAS: Unsupported Interface\n");
1608 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1609 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1611 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1612 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1613 dev_warn(psp->adev->dev,
1614 "RAS internal register access blocked\n");
1616 psp_ras_ta_check_status(psp);
1622 int psp_ras_enable_features(struct psp_context *psp,
1623 union ta_ras_cmd_input *info, bool enable)
1625 struct ta_ras_shared_memory *ras_cmd;
1628 if (!psp->ras_context.context.initialized)
1631 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1632 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1635 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1637 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1639 ras_cmd->ras_in_message = *info;
1641 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1648 int psp_ras_terminate(struct psp_context *psp)
1653 * TODO: bypass the terminate in sriov for now
1655 if (amdgpu_sriov_vf(psp->adev))
1658 if (!psp->ras_context.context.initialized)
1661 ret = psp_ta_unload(psp, &psp->ras_context.context);
1663 psp->ras_context.context.initialized = false;
1668 int psp_ras_initialize(struct psp_context *psp)
1671 uint32_t boot_cfg = 0xFF;
1672 struct amdgpu_device *adev = psp->adev;
1673 struct ta_ras_shared_memory *ras_cmd;
1676 * TODO: bypass the initialize in sriov for now
1678 if (amdgpu_sriov_vf(adev))
1681 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1682 !adev->psp.ras_context.context.bin_desc.start_addr) {
1683 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1687 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1688 /* query GECC enablement status from boot config
1689 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1691 ret = psp_boot_config_get(adev, &boot_cfg);
1693 dev_warn(adev->dev, "PSP get boot config failed\n");
1695 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1697 dev_info(adev->dev, "GECC is disabled\n");
1699 /* disable GECC in next boot cycle if ras is
1700 * disabled by module parameter amdgpu_ras_enable
1701 * and/or amdgpu_ras_mask, or boot_config_get call
1704 ret = psp_boot_config_set(adev, 0);
1706 dev_warn(adev->dev, "PSP set boot config failed\n");
1708 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");
1711 if (boot_cfg == 1) {
1712 dev_info(adev->dev, "GECC is enabled\n");
1714 /* enable GECC in next boot cycle if it is disabled
1715 * in boot config, or force enable GECC if failed to
1716 * get boot configuration
1718 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1720 dev_warn(adev->dev, "PSP set boot config failed\n");
1722 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1727 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1728 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1730 if (!psp->ras_context.context.mem_context.shared_buf) {
1731 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1736 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1737 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1739 if (amdgpu_ras_is_poison_mode_supported(adev))
1740 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1741 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu)
1742 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1743 ras_cmd->ras_in_message.init_flags.xcc_mask =
1745 ras_cmd->ras_in_message.init_flags.channel_dis_num = hweight32(adev->gmc.m_half_use) * 2;
1747 ret = psp_ta_load(psp, &psp->ras_context.context);
1749 if (!ret && !ras_cmd->ras_status)
1750 psp->ras_context.context.initialized = true;
1752 if (ras_cmd->ras_status)
1753 dev_warn(adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1755 /* fail to load RAS TA */
1756 psp->ras_context.context.initialized = false;
1762 int psp_ras_trigger_error(struct psp_context *psp,
1763 struct ta_ras_trigger_error_input *info, uint32_t instance_mask)
1765 struct ta_ras_shared_memory *ras_cmd;
1766 struct amdgpu_device *adev = psp->adev;
1770 if (!psp->ras_context.context.initialized)
1773 switch (info->block_id) {
1774 case TA_RAS_BLOCK__GFX:
1775 dev_mask = GET_MASK(GC, instance_mask);
1777 case TA_RAS_BLOCK__SDMA:
1778 dev_mask = GET_MASK(SDMA0, instance_mask);
1780 case TA_RAS_BLOCK__VCN:
1781 case TA_RAS_BLOCK__JPEG:
1782 dev_mask = GET_MASK(VCN, instance_mask);
1785 dev_mask = instance_mask;
1789 /* reuse sub_block_index for backward compatibility */
1790 dev_mask <<= AMDGPU_RAS_INST_SHIFT;
1791 dev_mask &= AMDGPU_RAS_INST_MASK;
1792 info->sub_block_index |= dev_mask;
1794 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1795 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1797 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1798 ras_cmd->ras_in_message.trigger_error = *info;
1800 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1804 /* If err_event_athub occurs error inject was successful, however
1805 * return status from TA is no long reliable
1807 if (amdgpu_ras_intr_triggered())
1810 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1812 else if (ras_cmd->ras_status)
1818 int psp_ras_query_address(struct psp_context *psp,
1819 struct ta_ras_query_address_input *addr_in,
1820 struct ta_ras_query_address_output *addr_out)
1822 struct ta_ras_shared_memory *ras_cmd;
1825 if (!psp->ras_context.context.initialized)
1828 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1829 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1831 ras_cmd->cmd_id = TA_RAS_COMMAND__QUERY_ADDRESS;
1832 ras_cmd->ras_in_message.address = *addr_in;
1834 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1835 if (ret || ras_cmd->ras_status || psp->cmd_buf_mem->resp.status)
1838 *addr_out = ras_cmd->ras_out_message.address;
1845 static int psp_hdcp_initialize(struct psp_context *psp)
1850 * TODO: bypass the initialize in sriov for now
1852 if (amdgpu_sriov_vf(psp->adev))
1855 /* bypass hdcp initialization if dmu is harvested */
1856 if (!amdgpu_device_has_display_hardware(psp->adev))
1859 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1860 !psp->hdcp_context.context.bin_desc.start_addr) {
1861 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1865 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1866 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1868 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1869 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1874 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1876 psp->hdcp_context.context.initialized = true;
1877 mutex_init(&psp->hdcp_context.mutex);
1883 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1886 * TODO: bypass the loading in sriov for now
1888 if (amdgpu_sriov_vf(psp->adev))
1891 if (!psp->hdcp_context.context.initialized)
1894 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1897 static int psp_hdcp_terminate(struct psp_context *psp)
1902 * TODO: bypass the terminate in sriov for now
1904 if (amdgpu_sriov_vf(psp->adev))
1907 if (!psp->hdcp_context.context.initialized)
1910 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1912 psp->hdcp_context.context.initialized = false;
1919 static int psp_dtm_initialize(struct psp_context *psp)
1924 * TODO: bypass the initialize in sriov for now
1926 if (amdgpu_sriov_vf(psp->adev))
1929 /* bypass dtm initialization if dmu is harvested */
1930 if (!amdgpu_device_has_display_hardware(psp->adev))
1933 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1934 !psp->dtm_context.context.bin_desc.start_addr) {
1935 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1939 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1940 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1942 if (!psp->dtm_context.context.mem_context.shared_buf) {
1943 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1948 ret = psp_ta_load(psp, &psp->dtm_context.context);
1950 psp->dtm_context.context.initialized = true;
1951 mutex_init(&psp->dtm_context.mutex);
1957 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1960 * TODO: bypass the loading in sriov for now
1962 if (amdgpu_sriov_vf(psp->adev))
1965 if (!psp->dtm_context.context.initialized)
1968 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1971 static int psp_dtm_terminate(struct psp_context *psp)
1976 * TODO: bypass the terminate in sriov for now
1978 if (amdgpu_sriov_vf(psp->adev))
1981 if (!psp->dtm_context.context.initialized)
1984 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1986 psp->dtm_context.context.initialized = false;
1993 static int psp_rap_initialize(struct psp_context *psp)
1996 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1999 * TODO: bypass the initialize in sriov for now
2001 if (amdgpu_sriov_vf(psp->adev))
2004 if (!psp->rap_context.context.bin_desc.size_bytes ||
2005 !psp->rap_context.context.bin_desc.start_addr) {
2006 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
2010 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
2011 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
2013 if (!psp->rap_context.context.mem_context.shared_buf) {
2014 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
2019 ret = psp_ta_load(psp, &psp->rap_context.context);
2021 psp->rap_context.context.initialized = true;
2022 mutex_init(&psp->rap_context.mutex);
2026 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
2027 if (ret || status != TA_RAP_STATUS__SUCCESS) {
2028 psp_rap_terminate(psp);
2029 /* free rap shared memory */
2030 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
2032 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
2041 static int psp_rap_terminate(struct psp_context *psp)
2045 if (!psp->rap_context.context.initialized)
2048 ret = psp_ta_unload(psp, &psp->rap_context.context);
2050 psp->rap_context.context.initialized = false;
2055 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
2057 struct ta_rap_shared_memory *rap_cmd;
2060 if (!psp->rap_context.context.initialized)
2063 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
2064 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
2067 mutex_lock(&psp->rap_context.mutex);
2069 rap_cmd = (struct ta_rap_shared_memory *)
2070 psp->rap_context.context.mem_context.shared_buf;
2071 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
2073 rap_cmd->cmd_id = ta_cmd_id;
2074 rap_cmd->validation_method_id = METHOD_A;
2076 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
2081 *status = rap_cmd->rap_status;
2084 mutex_unlock(&psp->rap_context.mutex);
2090 /* securedisplay start */
2091 static int psp_securedisplay_initialize(struct psp_context *psp)
2094 struct ta_securedisplay_cmd *securedisplay_cmd;
2097 * TODO: bypass the initialize in sriov for now
2099 if (amdgpu_sriov_vf(psp->adev))
2102 /* bypass securedisplay initialization if dmu is harvested */
2103 if (!amdgpu_device_has_display_hardware(psp->adev))
2106 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
2107 !psp->securedisplay_context.context.bin_desc.start_addr) {
2108 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
2112 psp->securedisplay_context.context.mem_context.shared_mem_size =
2113 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
2114 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
2116 if (!psp->securedisplay_context.context.initialized) {
2117 ret = psp_ta_init_shared_buf(psp,
2118 &psp->securedisplay_context.context.mem_context);
2123 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
2125 psp->securedisplay_context.context.initialized = true;
2126 mutex_init(&psp->securedisplay_context.mutex);
2130 mutex_lock(&psp->securedisplay_context.mutex);
2132 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
2133 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2135 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2137 mutex_unlock(&psp->securedisplay_context.mutex);
2140 psp_securedisplay_terminate(psp);
2141 /* free securedisplay shared memory */
2142 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
2143 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
2147 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
2148 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
2149 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
2150 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
2151 /* don't try again */
2152 psp->securedisplay_context.context.bin_desc.size_bytes = 0;
2158 static int psp_securedisplay_terminate(struct psp_context *psp)
2163 * TODO:bypass the terminate in sriov for now
2165 if (amdgpu_sriov_vf(psp->adev))
2168 if (!psp->securedisplay_context.context.initialized)
2171 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2173 psp->securedisplay_context.context.initialized = false;
2178 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2182 if (!psp->securedisplay_context.context.initialized)
2185 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2186 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2189 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2193 /* SECUREDISPLAY end */
2195 int amdgpu_psp_wait_for_bootloader(struct amdgpu_device *adev)
2197 struct psp_context *psp = &adev->psp;
2200 if (!amdgpu_sriov_vf(adev) && psp->funcs && psp->funcs->wait_for_bootloader != NULL)
2201 ret = psp->funcs->wait_for_bootloader(psp);
2206 bool amdgpu_psp_get_ras_capability(struct psp_context *psp)
2209 psp->funcs->get_ras_capability) {
2210 return psp->funcs->get_ras_capability(psp);
2216 static int psp_hw_start(struct psp_context *psp)
2218 struct amdgpu_device *adev = psp->adev;
2221 if (!amdgpu_sriov_vf(adev)) {
2222 if ((is_psp_fw_valid(psp->kdb)) &&
2223 (psp->funcs->bootloader_load_kdb != NULL)) {
2224 ret = psp_bootloader_load_kdb(psp);
2226 dev_err(adev->dev, "PSP load kdb failed!\n");
2231 if ((is_psp_fw_valid(psp->spl)) &&
2232 (psp->funcs->bootloader_load_spl != NULL)) {
2233 ret = psp_bootloader_load_spl(psp);
2235 dev_err(adev->dev, "PSP load spl failed!\n");
2240 if ((is_psp_fw_valid(psp->sys)) &&
2241 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2242 ret = psp_bootloader_load_sysdrv(psp);
2244 dev_err(adev->dev, "PSP load sys drv failed!\n");
2249 if ((is_psp_fw_valid(psp->soc_drv)) &&
2250 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2251 ret = psp_bootloader_load_soc_drv(psp);
2253 dev_err(adev->dev, "PSP load soc drv failed!\n");
2258 if ((is_psp_fw_valid(psp->intf_drv)) &&
2259 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2260 ret = psp_bootloader_load_intf_drv(psp);
2262 dev_err(adev->dev, "PSP load intf drv failed!\n");
2267 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2268 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2269 ret = psp_bootloader_load_dbg_drv(psp);
2271 dev_err(adev->dev, "PSP load dbg drv failed!\n");
2276 if ((is_psp_fw_valid(psp->ras_drv)) &&
2277 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2278 ret = psp_bootloader_load_ras_drv(psp);
2280 dev_err(adev->dev, "PSP load ras_drv failed!\n");
2285 if ((is_psp_fw_valid(psp->ipkeymgr_drv)) &&
2286 (psp->funcs->bootloader_load_ipkeymgr_drv != NULL)) {
2287 ret = psp_bootloader_load_ipkeymgr_drv(psp);
2289 dev_err(adev->dev, "PSP load ipkeymgr_drv failed!\n");
2294 if ((is_psp_fw_valid(psp->sos)) &&
2295 (psp->funcs->bootloader_load_sos != NULL)) {
2296 ret = psp_bootloader_load_sos(psp);
2298 dev_err(adev->dev, "PSP load sos failed!\n");
2304 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2306 dev_err(adev->dev, "PSP create ring failed!\n");
2310 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2313 if (!psp->boot_time_tmr || psp->autoload_supported) {
2314 ret = psp_tmr_init(psp);
2316 dev_err(adev->dev, "PSP tmr init failed!\n");
2323 * For ASICs with DF Cstate management centralized
2324 * to PMFW, TMR setup should be performed after PMFW
2325 * loaded and before other non-psp firmware loaded.
2327 if (psp->pmfw_centralized_cstate_management) {
2328 ret = psp_load_smu_fw(psp);
2333 if (!psp->boot_time_tmr || !psp->autoload_supported) {
2334 ret = psp_tmr_load(psp);
2336 dev_err(adev->dev, "PSP load tmr failed!\n");
2344 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2345 enum psp_gfx_fw_type *type)
2347 switch (ucode->ucode_id) {
2348 case AMDGPU_UCODE_ID_CAP:
2349 *type = GFX_FW_TYPE_CAP;
2351 case AMDGPU_UCODE_ID_SDMA0:
2352 *type = GFX_FW_TYPE_SDMA0;
2354 case AMDGPU_UCODE_ID_SDMA1:
2355 *type = GFX_FW_TYPE_SDMA1;
2357 case AMDGPU_UCODE_ID_SDMA2:
2358 *type = GFX_FW_TYPE_SDMA2;
2360 case AMDGPU_UCODE_ID_SDMA3:
2361 *type = GFX_FW_TYPE_SDMA3;
2363 case AMDGPU_UCODE_ID_SDMA4:
2364 *type = GFX_FW_TYPE_SDMA4;
2366 case AMDGPU_UCODE_ID_SDMA5:
2367 *type = GFX_FW_TYPE_SDMA5;
2369 case AMDGPU_UCODE_ID_SDMA6:
2370 *type = GFX_FW_TYPE_SDMA6;
2372 case AMDGPU_UCODE_ID_SDMA7:
2373 *type = GFX_FW_TYPE_SDMA7;
2375 case AMDGPU_UCODE_ID_CP_MES:
2376 *type = GFX_FW_TYPE_CP_MES;
2378 case AMDGPU_UCODE_ID_CP_MES_DATA:
2379 *type = GFX_FW_TYPE_MES_STACK;
2381 case AMDGPU_UCODE_ID_CP_MES1:
2382 *type = GFX_FW_TYPE_CP_MES_KIQ;
2384 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2385 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2387 case AMDGPU_UCODE_ID_CP_CE:
2388 *type = GFX_FW_TYPE_CP_CE;
2390 case AMDGPU_UCODE_ID_CP_PFP:
2391 *type = GFX_FW_TYPE_CP_PFP;
2393 case AMDGPU_UCODE_ID_CP_ME:
2394 *type = GFX_FW_TYPE_CP_ME;
2396 case AMDGPU_UCODE_ID_CP_MEC1:
2397 *type = GFX_FW_TYPE_CP_MEC;
2399 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2400 *type = GFX_FW_TYPE_CP_MEC_ME1;
2402 case AMDGPU_UCODE_ID_CP_MEC2:
2403 *type = GFX_FW_TYPE_CP_MEC;
2405 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2406 *type = GFX_FW_TYPE_CP_MEC_ME2;
2408 case AMDGPU_UCODE_ID_RLC_P:
2409 *type = GFX_FW_TYPE_RLC_P;
2411 case AMDGPU_UCODE_ID_RLC_V:
2412 *type = GFX_FW_TYPE_RLC_V;
2414 case AMDGPU_UCODE_ID_RLC_G:
2415 *type = GFX_FW_TYPE_RLC_G;
2417 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2418 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2420 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2421 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2423 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2424 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2426 case AMDGPU_UCODE_ID_RLC_IRAM:
2427 *type = GFX_FW_TYPE_RLC_IRAM;
2429 case AMDGPU_UCODE_ID_RLC_DRAM:
2430 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2432 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2433 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2435 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2436 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2438 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2439 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2441 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2442 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2444 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2445 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2447 case AMDGPU_UCODE_ID_SMC:
2448 *type = GFX_FW_TYPE_SMU;
2450 case AMDGPU_UCODE_ID_PPTABLE:
2451 *type = GFX_FW_TYPE_PPTABLE;
2453 case AMDGPU_UCODE_ID_UVD:
2454 *type = GFX_FW_TYPE_UVD;
2456 case AMDGPU_UCODE_ID_UVD1:
2457 *type = GFX_FW_TYPE_UVD1;
2459 case AMDGPU_UCODE_ID_VCE:
2460 *type = GFX_FW_TYPE_VCE;
2462 case AMDGPU_UCODE_ID_VCN:
2463 *type = GFX_FW_TYPE_VCN;
2465 case AMDGPU_UCODE_ID_VCN1:
2466 *type = GFX_FW_TYPE_VCN1;
2468 case AMDGPU_UCODE_ID_DMCU_ERAM:
2469 *type = GFX_FW_TYPE_DMCU_ERAM;
2471 case AMDGPU_UCODE_ID_DMCU_INTV:
2472 *type = GFX_FW_TYPE_DMCU_ISR;
2474 case AMDGPU_UCODE_ID_VCN0_RAM:
2475 *type = GFX_FW_TYPE_VCN0_RAM;
2477 case AMDGPU_UCODE_ID_VCN1_RAM:
2478 *type = GFX_FW_TYPE_VCN1_RAM;
2480 case AMDGPU_UCODE_ID_DMCUB:
2481 *type = GFX_FW_TYPE_DMUB;
2483 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2484 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2486 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2487 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2489 case AMDGPU_UCODE_ID_IMU_I:
2490 *type = GFX_FW_TYPE_IMU_I;
2492 case AMDGPU_UCODE_ID_IMU_D:
2493 *type = GFX_FW_TYPE_IMU_D;
2495 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2496 *type = GFX_FW_TYPE_RS64_PFP;
2498 case AMDGPU_UCODE_ID_CP_RS64_ME:
2499 *type = GFX_FW_TYPE_RS64_ME;
2501 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2502 *type = GFX_FW_TYPE_RS64_MEC;
2504 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2505 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2507 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2508 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2510 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2511 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2513 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2514 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2516 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2517 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2519 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2520 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2522 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2523 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2525 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2526 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2528 case AMDGPU_UCODE_ID_VPE_CTX:
2529 *type = GFX_FW_TYPE_VPEC_FW1;
2531 case AMDGPU_UCODE_ID_VPE_CTL:
2532 *type = GFX_FW_TYPE_VPEC_FW2;
2534 case AMDGPU_UCODE_ID_VPE:
2535 *type = GFX_FW_TYPE_VPE;
2537 case AMDGPU_UCODE_ID_UMSCH_MM_UCODE:
2538 *type = GFX_FW_TYPE_UMSCH_UCODE;
2540 case AMDGPU_UCODE_ID_UMSCH_MM_DATA:
2541 *type = GFX_FW_TYPE_UMSCH_DATA;
2543 case AMDGPU_UCODE_ID_UMSCH_MM_CMD_BUFFER:
2544 *type = GFX_FW_TYPE_UMSCH_CMD_BUFFER;
2546 case AMDGPU_UCODE_ID_P2S_TABLE:
2547 *type = GFX_FW_TYPE_P2S_TABLE;
2549 case AMDGPU_UCODE_ID_JPEG_RAM:
2550 *type = GFX_FW_TYPE_JPEG_RAM;
2552 case AMDGPU_UCODE_ID_MAXIMUM:
2560 static void psp_print_fw_hdr(struct psp_context *psp,
2561 struct amdgpu_firmware_info *ucode)
2563 struct amdgpu_device *adev = psp->adev;
2564 struct common_firmware_header *hdr;
2566 switch (ucode->ucode_id) {
2567 case AMDGPU_UCODE_ID_SDMA0:
2568 case AMDGPU_UCODE_ID_SDMA1:
2569 case AMDGPU_UCODE_ID_SDMA2:
2570 case AMDGPU_UCODE_ID_SDMA3:
2571 case AMDGPU_UCODE_ID_SDMA4:
2572 case AMDGPU_UCODE_ID_SDMA5:
2573 case AMDGPU_UCODE_ID_SDMA6:
2574 case AMDGPU_UCODE_ID_SDMA7:
2575 hdr = (struct common_firmware_header *)
2576 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2577 amdgpu_ucode_print_sdma_hdr(hdr);
2579 case AMDGPU_UCODE_ID_CP_CE:
2580 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2581 amdgpu_ucode_print_gfx_hdr(hdr);
2583 case AMDGPU_UCODE_ID_CP_PFP:
2584 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2585 amdgpu_ucode_print_gfx_hdr(hdr);
2587 case AMDGPU_UCODE_ID_CP_ME:
2588 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2589 amdgpu_ucode_print_gfx_hdr(hdr);
2591 case AMDGPU_UCODE_ID_CP_MEC1:
2592 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2593 amdgpu_ucode_print_gfx_hdr(hdr);
2595 case AMDGPU_UCODE_ID_RLC_G:
2596 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2597 amdgpu_ucode_print_rlc_hdr(hdr);
2599 case AMDGPU_UCODE_ID_SMC:
2600 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2601 amdgpu_ucode_print_smc_hdr(hdr);
2608 static int psp_prep_load_ip_fw_cmd_buf(struct psp_context *psp,
2609 struct amdgpu_firmware_info *ucode,
2610 struct psp_gfx_cmd_resp *cmd)
2613 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2615 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2616 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2617 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2618 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2620 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2622 dev_err(psp->adev->dev, "Unknown firmware type\n");
2627 int psp_execute_ip_fw_load(struct psp_context *psp,
2628 struct amdgpu_firmware_info *ucode)
2631 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2633 ret = psp_prep_load_ip_fw_cmd_buf(psp, ucode, cmd);
2635 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2636 psp->fence_buf_mc_addr);
2639 release_psp_cmd_buf(psp);
2644 static int psp_load_p2s_table(struct psp_context *psp)
2647 struct amdgpu_device *adev = psp->adev;
2648 struct amdgpu_firmware_info *ucode =
2649 &adev->firmware.ucode[AMDGPU_UCODE_ID_P2S_TABLE];
2651 if (adev->in_runpm && ((adev->pm.rpm_mode == AMDGPU_RUNPM_BACO) ||
2652 (adev->pm.rpm_mode == AMDGPU_RUNPM_BAMACO)))
2655 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 6)) {
2656 uint32_t supp_vers = adev->flags & AMD_IS_APU ? 0x0036013D :
2658 if (psp->sos.fw_version < supp_vers)
2662 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2665 ret = psp_execute_ip_fw_load(psp, ucode);
2670 static int psp_load_smu_fw(struct psp_context *psp)
2673 struct amdgpu_device *adev = psp->adev;
2674 struct amdgpu_firmware_info *ucode =
2675 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2676 struct amdgpu_ras *ras = psp->ras_context.ras;
2679 * Skip SMU FW reloading in case of using BACO for runpm only,
2680 * as SMU is always alive.
2682 if (adev->in_runpm && ((adev->pm.rpm_mode == AMDGPU_RUNPM_BACO) ||
2683 (adev->pm.rpm_mode == AMDGPU_RUNPM_BAMACO)))
2686 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2689 if ((amdgpu_in_reset(adev) && ras && adev->ras_enabled &&
2690 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 4) ||
2691 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 2)))) {
2692 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2694 dev_err(adev->dev, "Failed to set MP1 state prepare for reload\n");
2697 ret = psp_execute_ip_fw_load(psp, ucode);
2700 dev_err(adev->dev, "PSP load smu failed!\n");
2705 static bool fw_load_skip_check(struct psp_context *psp,
2706 struct amdgpu_firmware_info *ucode)
2708 if (!ucode->fw || !ucode->ucode_size)
2711 if (ucode->ucode_id == AMDGPU_UCODE_ID_P2S_TABLE)
2714 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2715 (psp_smu_reload_quirk(psp) ||
2716 psp->autoload_supported ||
2717 psp->pmfw_centralized_cstate_management))
2720 if (amdgpu_sriov_vf(psp->adev) &&
2721 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2724 if (psp->autoload_supported &&
2725 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2726 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2727 /* skip mec JT when autoload is enabled */
2733 int psp_load_fw_list(struct psp_context *psp,
2734 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2737 struct amdgpu_firmware_info *ucode;
2739 for (i = 0; i < ucode_count; ++i) {
2740 ucode = ucode_list[i];
2741 psp_print_fw_hdr(psp, ucode);
2742 ret = psp_execute_ip_fw_load(psp, ucode);
2749 static int psp_load_non_psp_fw(struct psp_context *psp)
2752 struct amdgpu_firmware_info *ucode;
2753 struct amdgpu_device *adev = psp->adev;
2755 if (psp->autoload_supported &&
2756 !psp->pmfw_centralized_cstate_management) {
2757 ret = psp_load_smu_fw(psp);
2762 /* Load P2S table first if it's available */
2763 psp_load_p2s_table(psp);
2765 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2766 ucode = &adev->firmware.ucode[i];
2768 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2769 !fw_load_skip_check(psp, ucode)) {
2770 ret = psp_load_smu_fw(psp);
2776 if (fw_load_skip_check(psp, ucode))
2779 if (psp->autoload_supported &&
2780 (amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2781 IP_VERSION(11, 0, 7) ||
2782 amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2783 IP_VERSION(11, 0, 11) ||
2784 amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2785 IP_VERSION(11, 0, 12)) &&
2786 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2787 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2788 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2789 /* PSP only receive one SDMA fw for sienna_cichlid,
2790 * as all four sdma fw are same
2794 psp_print_fw_hdr(psp, ucode);
2796 ret = psp_execute_ip_fw_load(psp, ucode);
2800 /* Start rlc autoload after psp recieved all the gfx firmware */
2801 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2802 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2803 ret = psp_rlc_autoload_start(psp);
2805 dev_err(adev->dev, "Failed to start rlc autoload\n");
2814 static int psp_load_fw(struct amdgpu_device *adev)
2817 struct psp_context *psp = &adev->psp;
2819 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2820 /* should not destroy ring, only stop */
2821 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2823 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2825 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2827 dev_err(adev->dev, "PSP ring init failed!\n");
2832 ret = psp_hw_start(psp);
2836 ret = psp_load_non_psp_fw(psp);
2840 ret = psp_asd_initialize(psp);
2842 dev_err(adev->dev, "PSP load asd failed!\n");
2846 ret = psp_rl_load(adev);
2848 dev_err(adev->dev, "PSP load RL failed!\n");
2852 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2853 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2854 ret = psp_xgmi_initialize(psp, false, true);
2855 /* Warning the XGMI seesion initialize failure
2856 * Instead of stop driver initialization
2859 dev_err(psp->adev->dev,
2860 "XGMI: Failed to initialize XGMI session\n");
2865 ret = psp_ras_initialize(psp);
2867 dev_err(psp->adev->dev,
2868 "RAS: Failed to initialize RAS\n");
2870 ret = psp_hdcp_initialize(psp);
2872 dev_err(psp->adev->dev,
2873 "HDCP: Failed to initialize HDCP\n");
2875 ret = psp_dtm_initialize(psp);
2877 dev_err(psp->adev->dev,
2878 "DTM: Failed to initialize DTM\n");
2880 ret = psp_rap_initialize(psp);
2882 dev_err(psp->adev->dev,
2883 "RAP: Failed to initialize RAP\n");
2885 ret = psp_securedisplay_initialize(psp);
2887 dev_err(psp->adev->dev,
2888 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2894 psp_free_shared_bufs(psp);
2897 * all cleanup jobs (xgmi terminate, ras terminate,
2898 * ring destroy, cmd/fence/fw buffers destory,
2899 * psp->cmd destory) are delayed to psp_hw_fini
2901 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2905 static int psp_hw_init(void *handle)
2908 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2910 mutex_lock(&adev->firmware.mutex);
2912 * This sequence is just used on hw_init only once, no need on
2915 ret = amdgpu_ucode_init_bo(adev);
2919 ret = psp_load_fw(adev);
2921 dev_err(adev->dev, "PSP firmware loading failed\n");
2925 mutex_unlock(&adev->firmware.mutex);
2929 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2930 mutex_unlock(&adev->firmware.mutex);
2934 static int psp_hw_fini(void *handle)
2936 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2937 struct psp_context *psp = &adev->psp;
2940 psp_ras_terminate(psp);
2941 psp_securedisplay_terminate(psp);
2942 psp_rap_terminate(psp);
2943 psp_dtm_terminate(psp);
2944 psp_hdcp_terminate(psp);
2946 if (adev->gmc.xgmi.num_physical_nodes > 1)
2947 psp_xgmi_terminate(psp);
2950 psp_asd_terminate(psp);
2951 psp_tmr_terminate(psp);
2953 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2958 static int psp_suspend(void *handle)
2961 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2962 struct psp_context *psp = &adev->psp;
2964 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2965 psp->xgmi_context.context.initialized) {
2966 ret = psp_xgmi_terminate(psp);
2968 dev_err(adev->dev, "Failed to terminate xgmi ta\n");
2974 ret = psp_ras_terminate(psp);
2976 dev_err(adev->dev, "Failed to terminate ras ta\n");
2979 ret = psp_hdcp_terminate(psp);
2981 dev_err(adev->dev, "Failed to terminate hdcp ta\n");
2984 ret = psp_dtm_terminate(psp);
2986 dev_err(adev->dev, "Failed to terminate dtm ta\n");
2989 ret = psp_rap_terminate(psp);
2991 dev_err(adev->dev, "Failed to terminate rap ta\n");
2994 ret = psp_securedisplay_terminate(psp);
2996 dev_err(adev->dev, "Failed to terminate securedisplay ta\n");
3001 ret = psp_asd_terminate(psp);
3003 dev_err(adev->dev, "Failed to terminate asd\n");
3007 ret = psp_tmr_terminate(psp);
3009 dev_err(adev->dev, "Failed to terminate tmr\n");
3013 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
3015 dev_err(adev->dev, "PSP ring stop failed\n");
3021 static int psp_resume(void *handle)
3024 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3025 struct psp_context *psp = &adev->psp;
3027 dev_info(adev->dev, "PSP is resuming...\n");
3029 if (psp->mem_train_ctx.enable_mem_training) {
3030 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
3032 dev_err(adev->dev, "Failed to process memory training!\n");
3037 mutex_lock(&adev->firmware.mutex);
3039 ret = psp_hw_start(psp);
3043 ret = psp_load_non_psp_fw(psp);
3047 ret = psp_asd_initialize(psp);
3049 dev_err(adev->dev, "PSP load asd failed!\n");
3053 ret = psp_rl_load(adev);
3055 dev_err(adev->dev, "PSP load RL failed!\n");
3059 if (adev->gmc.xgmi.num_physical_nodes > 1) {
3060 ret = psp_xgmi_initialize(psp, false, true);
3061 /* Warning the XGMI seesion initialize failure
3062 * Instead of stop driver initialization
3065 dev_err(psp->adev->dev,
3066 "XGMI: Failed to initialize XGMI session\n");
3070 ret = psp_ras_initialize(psp);
3072 dev_err(psp->adev->dev,
3073 "RAS: Failed to initialize RAS\n");
3075 ret = psp_hdcp_initialize(psp);
3077 dev_err(psp->adev->dev,
3078 "HDCP: Failed to initialize HDCP\n");
3080 ret = psp_dtm_initialize(psp);
3082 dev_err(psp->adev->dev,
3083 "DTM: Failed to initialize DTM\n");
3085 ret = psp_rap_initialize(psp);
3087 dev_err(psp->adev->dev,
3088 "RAP: Failed to initialize RAP\n");
3090 ret = psp_securedisplay_initialize(psp);
3092 dev_err(psp->adev->dev,
3093 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
3096 mutex_unlock(&adev->firmware.mutex);
3101 dev_err(adev->dev, "PSP resume failed\n");
3102 mutex_unlock(&adev->firmware.mutex);
3106 int psp_gpu_reset(struct amdgpu_device *adev)
3110 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
3113 mutex_lock(&adev->psp.mutex);
3114 ret = psp_mode1_reset(&adev->psp);
3115 mutex_unlock(&adev->psp.mutex);
3120 int psp_rlc_autoload_start(struct psp_context *psp)
3123 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
3125 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
3127 ret = psp_cmd_submit_buf(psp, NULL, cmd,
3128 psp->fence_buf_mc_addr);
3130 release_psp_cmd_buf(psp);
3135 int psp_ring_cmd_submit(struct psp_context *psp,
3136 uint64_t cmd_buf_mc_addr,
3137 uint64_t fence_mc_addr,
3140 unsigned int psp_write_ptr_reg = 0;
3141 struct psp_gfx_rb_frame *write_frame;
3142 struct psp_ring *ring = &psp->km_ring;
3143 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
3144 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
3145 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
3146 struct amdgpu_device *adev = psp->adev;
3147 uint32_t ring_size_dw = ring->ring_size / 4;
3148 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
3150 /* KM (GPCOM) prepare write pointer */
3151 psp_write_ptr_reg = psp_ring_get_wptr(psp);
3153 /* Update KM RB frame pointer to new frame */
3154 /* write_frame ptr increments by size of rb_frame in bytes */
3155 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
3156 if ((psp_write_ptr_reg % ring_size_dw) == 0)
3157 write_frame = ring_buffer_start;
3159 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
3160 /* Check invalid write_frame ptr address */
3161 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
3163 "ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
3164 ring_buffer_start, ring_buffer_end, write_frame);
3166 "write_frame is pointing to address out of bounds\n");
3170 /* Initialize KM RB frame */
3171 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
3173 /* Update KM RB frame */
3174 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
3175 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
3176 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
3177 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
3178 write_frame->fence_value = index;
3179 amdgpu_device_flush_hdp(adev, NULL);
3181 /* Update the write Pointer in DWORDs */
3182 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
3183 psp_ring_set_wptr(psp, psp_write_ptr_reg);
3187 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
3189 struct amdgpu_device *adev = psp->adev;
3190 char fw_name[PSP_FW_NAME_LEN];
3191 const struct psp_firmware_header_v1_0 *asd_hdr;
3194 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
3195 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
3199 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
3200 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
3201 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
3202 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
3203 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
3204 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
3207 amdgpu_ucode_release(&adev->psp.asd_fw);
3211 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
3213 struct amdgpu_device *adev = psp->adev;
3214 char fw_name[PSP_FW_NAME_LEN];
3215 const struct psp_firmware_header_v1_0 *toc_hdr;
3218 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
3219 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
3223 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
3224 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
3225 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
3226 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
3227 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
3228 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
3231 amdgpu_ucode_release(&adev->psp.toc_fw);
3235 static int parse_sos_bin_descriptor(struct psp_context *psp,
3236 const struct psp_fw_bin_desc *desc,
3237 const struct psp_firmware_header_v2_0 *sos_hdr)
3239 uint8_t *ucode_start_addr = NULL;
3241 if (!psp || !desc || !sos_hdr)
3244 ucode_start_addr = (uint8_t *)sos_hdr +
3245 le32_to_cpu(desc->offset_bytes) +
3246 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3248 switch (desc->fw_type) {
3249 case PSP_FW_TYPE_PSP_SOS:
3250 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3251 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3252 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3253 psp->sos.start_addr = ucode_start_addr;
3255 case PSP_FW_TYPE_PSP_SYS_DRV:
3256 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3257 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3258 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3259 psp->sys.start_addr = ucode_start_addr;
3261 case PSP_FW_TYPE_PSP_KDB:
3262 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3263 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3264 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3265 psp->kdb.start_addr = ucode_start_addr;
3267 case PSP_FW_TYPE_PSP_TOC:
3268 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3269 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3270 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3271 psp->toc.start_addr = ucode_start_addr;
3273 case PSP_FW_TYPE_PSP_SPL:
3274 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3275 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3276 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3277 psp->spl.start_addr = ucode_start_addr;
3279 case PSP_FW_TYPE_PSP_RL:
3280 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3281 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3282 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3283 psp->rl.start_addr = ucode_start_addr;
3285 case PSP_FW_TYPE_PSP_SOC_DRV:
3286 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3287 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3288 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3289 psp->soc_drv.start_addr = ucode_start_addr;
3291 case PSP_FW_TYPE_PSP_INTF_DRV:
3292 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3293 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3294 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3295 psp->intf_drv.start_addr = ucode_start_addr;
3297 case PSP_FW_TYPE_PSP_DBG_DRV:
3298 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3299 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3300 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3301 psp->dbg_drv.start_addr = ucode_start_addr;
3303 case PSP_FW_TYPE_PSP_RAS_DRV:
3304 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3305 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3306 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3307 psp->ras_drv.start_addr = ucode_start_addr;
3309 case PSP_FW_TYPE_PSP_IPKEYMGR_DRV:
3310 psp->ipkeymgr_drv.fw_version = le32_to_cpu(desc->fw_version);
3311 psp->ipkeymgr_drv.feature_version = le32_to_cpu(desc->fw_version);
3312 psp->ipkeymgr_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3313 psp->ipkeymgr_drv.start_addr = ucode_start_addr;
3316 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3323 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3325 const struct psp_firmware_header_v1_0 *sos_hdr;
3326 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3327 uint8_t *ucode_array_start_addr;
3329 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3330 ucode_array_start_addr = (uint8_t *)sos_hdr +
3331 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3333 if (adev->gmc.xgmi.connected_to_cpu ||
3334 (amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(13, 0, 2))) {
3335 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3336 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3338 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3339 adev->psp.sys.start_addr = ucode_array_start_addr;
3341 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3342 adev->psp.sos.start_addr = ucode_array_start_addr +
3343 le32_to_cpu(sos_hdr->sos.offset_bytes);
3345 /* Load alternate PSP SOS FW */
3346 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3348 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3349 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3351 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3352 adev->psp.sys.start_addr = ucode_array_start_addr +
3353 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3355 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3356 adev->psp.sos.start_addr = ucode_array_start_addr +
3357 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3360 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3361 dev_warn(adev->dev, "PSP SOS FW not available");
3368 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3370 struct amdgpu_device *adev = psp->adev;
3371 char fw_name[PSP_FW_NAME_LEN];
3372 const struct psp_firmware_header_v1_0 *sos_hdr;
3373 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3374 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3375 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3376 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3378 uint8_t *ucode_array_start_addr;
3381 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3382 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3386 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3387 ucode_array_start_addr = (uint8_t *)sos_hdr +
3388 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3389 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3391 switch (sos_hdr->header.header_version_major) {
3393 err = psp_init_sos_base_fw(adev);
3397 if (sos_hdr->header.header_version_minor == 1) {
3398 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3399 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3400 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3401 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3402 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3403 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3404 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3406 if (sos_hdr->header.header_version_minor == 2) {
3407 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3408 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3409 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3410 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3412 if (sos_hdr->header.header_version_minor == 3) {
3413 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3414 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3415 adev->psp.toc.start_addr = ucode_array_start_addr +
3416 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3417 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3418 adev->psp.kdb.start_addr = ucode_array_start_addr +
3419 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3420 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3421 adev->psp.spl.start_addr = ucode_array_start_addr +
3422 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3423 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3424 adev->psp.rl.start_addr = ucode_array_start_addr +
3425 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3429 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3431 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3432 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3437 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3438 err = parse_sos_bin_descriptor(psp,
3439 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3447 "unsupported psp sos firmware\n");
3454 amdgpu_ucode_release(&adev->psp.sos_fw);
3459 static int parse_ta_bin_descriptor(struct psp_context *psp,
3460 const struct psp_fw_bin_desc *desc,
3461 const struct ta_firmware_header_v2_0 *ta_hdr)
3463 uint8_t *ucode_start_addr = NULL;
3465 if (!psp || !desc || !ta_hdr)
3468 ucode_start_addr = (uint8_t *)ta_hdr +
3469 le32_to_cpu(desc->offset_bytes) +
3470 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3472 switch (desc->fw_type) {
3473 case TA_FW_TYPE_PSP_ASD:
3474 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3475 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3476 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3477 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3479 case TA_FW_TYPE_PSP_XGMI:
3480 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3481 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3482 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3484 case TA_FW_TYPE_PSP_RAS:
3485 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3486 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3487 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3489 case TA_FW_TYPE_PSP_HDCP:
3490 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3491 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3492 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3494 case TA_FW_TYPE_PSP_DTM:
3495 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3496 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3497 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3499 case TA_FW_TYPE_PSP_RAP:
3500 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3501 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3502 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3504 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3505 psp->securedisplay_context.context.bin_desc.fw_version =
3506 le32_to_cpu(desc->fw_version);
3507 psp->securedisplay_context.context.bin_desc.size_bytes =
3508 le32_to_cpu(desc->size_bytes);
3509 psp->securedisplay_context.context.bin_desc.start_addr =
3513 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3520 static int parse_ta_v1_microcode(struct psp_context *psp)
3522 const struct ta_firmware_header_v1_0 *ta_hdr;
3523 struct amdgpu_device *adev = psp->adev;
3525 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3527 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3530 adev->psp.xgmi_context.context.bin_desc.fw_version =
3531 le32_to_cpu(ta_hdr->xgmi.fw_version);
3532 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3533 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3534 adev->psp.xgmi_context.context.bin_desc.start_addr =
3536 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3538 adev->psp.ras_context.context.bin_desc.fw_version =
3539 le32_to_cpu(ta_hdr->ras.fw_version);
3540 adev->psp.ras_context.context.bin_desc.size_bytes =
3541 le32_to_cpu(ta_hdr->ras.size_bytes);
3542 adev->psp.ras_context.context.bin_desc.start_addr =
3543 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3544 le32_to_cpu(ta_hdr->ras.offset_bytes);
3546 adev->psp.hdcp_context.context.bin_desc.fw_version =
3547 le32_to_cpu(ta_hdr->hdcp.fw_version);
3548 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3549 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3550 adev->psp.hdcp_context.context.bin_desc.start_addr =
3552 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3554 adev->psp.dtm_context.context.bin_desc.fw_version =
3555 le32_to_cpu(ta_hdr->dtm.fw_version);
3556 adev->psp.dtm_context.context.bin_desc.size_bytes =
3557 le32_to_cpu(ta_hdr->dtm.size_bytes);
3558 adev->psp.dtm_context.context.bin_desc.start_addr =
3559 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3560 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3562 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3563 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3564 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3565 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3566 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3567 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3568 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3570 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3575 static int parse_ta_v2_microcode(struct psp_context *psp)
3577 const struct ta_firmware_header_v2_0 *ta_hdr;
3578 struct amdgpu_device *adev = psp->adev;
3582 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3584 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3587 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3588 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3592 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3593 err = parse_ta_bin_descriptor(psp,
3594 &ta_hdr->ta_fw_bin[ta_index],
3603 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3605 const struct common_firmware_header *hdr;
3606 struct amdgpu_device *adev = psp->adev;
3607 char fw_name[PSP_FW_NAME_LEN];
3610 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3611 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3615 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3616 switch (le16_to_cpu(hdr->header_version_major)) {
3618 err = parse_ta_v1_microcode(psp);
3621 err = parse_ta_v2_microcode(psp);
3624 dev_err(adev->dev, "unsupported TA header version\n");
3629 amdgpu_ucode_release(&adev->psp.ta_fw);
3634 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3636 struct amdgpu_device *adev = psp->adev;
3637 char fw_name[PSP_FW_NAME_LEN];
3638 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3639 struct amdgpu_firmware_info *info = NULL;
3642 if (!amdgpu_sriov_vf(adev)) {
3643 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3647 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3648 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3650 if (err == -ENODEV) {
3651 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3655 dev_err(adev->dev, "fail to initialize cap microcode\n");
3658 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3659 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3660 info->fw = adev->psp.cap_fw;
3661 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3662 adev->psp.cap_fw->data;
3663 adev->firmware.fw_size += ALIGN(
3664 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3665 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3666 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3667 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3672 amdgpu_ucode_release(&adev->psp.cap_fw);
3676 static int psp_set_clockgating_state(void *handle,
3677 enum amd_clockgating_state state)
3682 static int psp_set_powergating_state(void *handle,
3683 enum amd_powergating_state state)
3688 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3689 struct device_attribute *attr,
3692 struct drm_device *ddev = dev_get_drvdata(dev);
3693 struct amdgpu_device *adev = drm_to_adev(ddev);
3697 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3698 dev_info(adev->dev, "PSP block is not ready yet\n.");
3702 mutex_lock(&adev->psp.mutex);
3703 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3704 mutex_unlock(&adev->psp.mutex);
3707 dev_err(adev->dev, "Failed to read USBC PD FW, err = %d\n", ret);
3711 return sysfs_emit(buf, "%x\n", fw_ver);
3714 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3715 struct device_attribute *attr,
3719 struct drm_device *ddev = dev_get_drvdata(dev);
3720 struct amdgpu_device *adev = drm_to_adev(ddev);
3723 const struct firmware *usbc_pd_fw;
3724 struct amdgpu_bo *fw_buf_bo = NULL;
3725 uint64_t fw_pri_mc_addr;
3726 void *fw_pri_cpu_addr;
3728 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3729 dev_err(adev->dev, "PSP block is not ready yet.");
3733 if (!drm_dev_enter(ddev, &idx))
3736 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3737 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3741 /* LFB address which is aligned to 1MB boundary per PSP request */
3742 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3743 AMDGPU_GEM_DOMAIN_VRAM |
3744 AMDGPU_GEM_DOMAIN_GTT,
3745 &fw_buf_bo, &fw_pri_mc_addr,
3750 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3752 mutex_lock(&adev->psp.mutex);
3753 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3754 mutex_unlock(&adev->psp.mutex);
3756 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3759 release_firmware(usbc_pd_fw);
3762 dev_err(adev->dev, "Failed to load USBC PD FW, err = %d", ret);
3770 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3774 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3777 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3778 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3785 * Reading from this file will retrieve the USB-C PD firmware version. Writing to
3786 * this file will trigger the update process.
3788 static DEVICE_ATTR(usbc_pd_fw, 0644,
3789 psp_usbc_pd_fw_sysfs_read,
3790 psp_usbc_pd_fw_sysfs_write);
3792 int is_psp_fw_valid(struct psp_bin_desc bin)
3794 return bin.size_bytes;
3797 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3798 struct bin_attribute *bin_attr,
3799 char *buffer, loff_t pos, size_t count)
3801 struct device *dev = kobj_to_dev(kobj);
3802 struct drm_device *ddev = dev_get_drvdata(dev);
3803 struct amdgpu_device *adev = drm_to_adev(ddev);
3805 adev->psp.vbflash_done = false;
3807 /* Safeguard against memory drain */
3808 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3809 dev_err(adev->dev, "File size cannot exceed %u\n", AMD_VBIOS_FILE_MAX_SIZE_B);
3810 kvfree(adev->psp.vbflash_tmp_buf);
3811 adev->psp.vbflash_tmp_buf = NULL;
3812 adev->psp.vbflash_image_size = 0;
3816 /* TODO Just allocate max for now and optimize to realloc later if needed */
3817 if (!adev->psp.vbflash_tmp_buf) {
3818 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3819 if (!adev->psp.vbflash_tmp_buf)
3823 mutex_lock(&adev->psp.mutex);
3824 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3825 adev->psp.vbflash_image_size += count;
3826 mutex_unlock(&adev->psp.mutex);
3828 dev_dbg(adev->dev, "IFWI staged for update\n");
3833 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3834 struct bin_attribute *bin_attr, char *buffer,
3835 loff_t pos, size_t count)
3837 struct device *dev = kobj_to_dev(kobj);
3838 struct drm_device *ddev = dev_get_drvdata(dev);
3839 struct amdgpu_device *adev = drm_to_adev(ddev);
3840 struct amdgpu_bo *fw_buf_bo = NULL;
3841 uint64_t fw_pri_mc_addr;
3842 void *fw_pri_cpu_addr;
3845 if (adev->psp.vbflash_image_size == 0)
3848 dev_dbg(adev->dev, "PSP IFWI flash process initiated\n");
3850 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3851 AMDGPU_GPU_PAGE_SIZE,
3852 AMDGPU_GEM_DOMAIN_VRAM,
3859 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3861 mutex_lock(&adev->psp.mutex);
3862 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3863 mutex_unlock(&adev->psp.mutex);
3865 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3868 kvfree(adev->psp.vbflash_tmp_buf);
3869 adev->psp.vbflash_tmp_buf = NULL;
3870 adev->psp.vbflash_image_size = 0;
3873 dev_err(adev->dev, "Failed to load IFWI, err = %d\n", ret);
3877 dev_dbg(adev->dev, "PSP IFWI flash process done\n");
3883 * Writing to this file will stage an IFWI for update. Reading from this file
3884 * will trigger the update process.
3886 static struct bin_attribute psp_vbflash_bin_attr = {
3887 .attr = {.name = "psp_vbflash", .mode = 0660},
3889 .write = amdgpu_psp_vbflash_write,
3890 .read = amdgpu_psp_vbflash_read,
3894 * DOC: psp_vbflash_status
3895 * The status of the flash process.
3896 * 0: IFWI flash not complete.
3897 * 1: IFWI flash complete.
3899 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3900 struct device_attribute *attr,
3903 struct drm_device *ddev = dev_get_drvdata(dev);
3904 struct amdgpu_device *adev = drm_to_adev(ddev);
3905 uint32_t vbflash_status;
3907 vbflash_status = psp_vbflash_status(&adev->psp);
3908 if (!adev->psp.vbflash_done)
3910 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3913 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3915 static DEVICE_ATTR(psp_vbflash_status, 0440, amdgpu_psp_vbflash_status, NULL);
3917 static struct bin_attribute *bin_flash_attrs[] = {
3918 &psp_vbflash_bin_attr,
3922 static struct attribute *flash_attrs[] = {
3923 &dev_attr_psp_vbflash_status.attr,
3924 &dev_attr_usbc_pd_fw.attr,
3928 static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
3930 struct device *dev = kobj_to_dev(kobj);
3931 struct drm_device *ddev = dev_get_drvdata(dev);
3932 struct amdgpu_device *adev = drm_to_adev(ddev);
3934 if (attr == &dev_attr_usbc_pd_fw.attr)
3935 return adev->psp.sup_pd_fw_up ? 0660 : 0;
3937 return adev->psp.sup_ifwi_up ? 0440 : 0;
3940 static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
3941 struct bin_attribute *attr,
3944 struct device *dev = kobj_to_dev(kobj);
3945 struct drm_device *ddev = dev_get_drvdata(dev);
3946 struct amdgpu_device *adev = drm_to_adev(ddev);
3948 return adev->psp.sup_ifwi_up ? 0660 : 0;
3951 const struct attribute_group amdgpu_flash_attr_group = {
3952 .attrs = flash_attrs,
3953 .bin_attrs = bin_flash_attrs,
3954 .is_bin_visible = amdgpu_bin_flash_attr_is_visible,
3955 .is_visible = amdgpu_flash_attr_is_visible,
3958 const struct amd_ip_funcs psp_ip_funcs = {
3960 .early_init = psp_early_init,
3962 .sw_init = psp_sw_init,
3963 .sw_fini = psp_sw_fini,
3964 .hw_init = psp_hw_init,
3965 .hw_fini = psp_hw_fini,
3966 .suspend = psp_suspend,
3967 .resume = psp_resume,
3969 .check_soft_reset = NULL,
3970 .wait_for_idle = NULL,
3972 .set_clockgating_state = psp_set_clockgating_state,
3973 .set_powergating_state = psp_set_powergating_state,
3976 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3977 .type = AMD_IP_BLOCK_TYPE_PSP,
3981 .funcs = &psp_ip_funcs,
3984 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3985 .type = AMD_IP_BLOCK_TYPE_PSP,
3989 .funcs = &psp_ip_funcs,
3992 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3993 .type = AMD_IP_BLOCK_TYPE_PSP,
3997 .funcs = &psp_ip_funcs,
4000 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
4001 .type = AMD_IP_BLOCK_TYPE_PSP,
4005 .funcs = &psp_ip_funcs,
4008 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
4009 .type = AMD_IP_BLOCK_TYPE_PSP,
4013 .funcs = &psp_ip_funcs,
4016 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
4017 .type = AMD_IP_BLOCK_TYPE_PSP,
4021 .funcs = &psp_ip_funcs,
4024 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
4025 .type = AMD_IP_BLOCK_TYPE_PSP,
4029 .funcs = &psp_ip_funcs,
4032 const struct amdgpu_ip_block_version psp_v14_0_ip_block = {
4033 .type = AMD_IP_BLOCK_TYPE_PSP,
4037 .funcs = &psp_ip_funcs,