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";
644 psp_cmd_submit_buf(struct psp_context *psp,
645 struct amdgpu_firmware_info *ucode,
646 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
650 int timeout = psp->adev->psp_timeout;
651 bool ras_intr = false;
652 bool skip_unsupport = false;
654 if (psp->adev->no_hw_access)
657 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
659 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
661 index = atomic_inc_return(&psp->fence_value);
662 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
664 atomic_dec(&psp->fence_value);
668 amdgpu_device_invalidate_hdp(psp->adev, NULL);
669 while (*((unsigned int *)psp->fence_buf) != index) {
673 * Shouldn't wait for timeout when err_event_athub occurs,
674 * because gpu reset thread triggered and lock resource should
675 * be released for psp resume sequence.
677 ras_intr = amdgpu_ras_intr_triggered();
680 usleep_range(10, 100);
681 amdgpu_device_invalidate_hdp(psp->adev, NULL);
684 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
685 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
686 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
688 memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
690 /* In some cases, psp response status is not 0 even there is no
691 * problem while the command is submitted. Some version of PSP FW
692 * doesn't write 0 to that field.
693 * So here we would like to only print a warning instead of an error
694 * during psp initialization to avoid breaking hw_init and it doesn't
697 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
699 dev_warn(psp->adev->dev,
700 "failed to load ucode %s(0x%X) ",
701 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
702 dev_warn(psp->adev->dev,
703 "psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
704 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
705 psp->cmd_buf_mem->resp.status);
706 /* If any firmware (including CAP) load fails under SRIOV, it should
707 * return failure to stop the VF from initializing.
708 * Also return failure in case of timeout
710 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
717 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
718 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
725 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
727 struct psp_gfx_cmd_resp *cmd = psp->cmd;
729 mutex_lock(&psp->mutex);
731 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
736 static void release_psp_cmd_buf(struct psp_context *psp)
738 mutex_unlock(&psp->mutex);
741 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
742 struct psp_gfx_cmd_resp *cmd,
743 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
745 struct amdgpu_device *adev = psp->adev;
750 size = amdgpu_bo_size(tmr_bo);
751 tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
754 if (amdgpu_sriov_vf(psp->adev))
755 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
757 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
758 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
759 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
760 cmd->cmd.cmd_setup_tmr.buf_size = size;
761 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
762 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
763 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
766 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
767 uint64_t pri_buf_mc, uint32_t size)
769 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
770 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
771 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
772 cmd->cmd.cmd_load_toc.toc_size = size;
775 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
776 static int psp_load_toc(struct psp_context *psp,
780 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
782 /* Copy toc to psp firmware private buffer */
783 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
785 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
787 ret = psp_cmd_submit_buf(psp, NULL, cmd,
788 psp->fence_buf_mc_addr);
790 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
792 release_psp_cmd_buf(psp);
797 /* Set up Trusted Memory Region */
798 static int psp_tmr_init(struct psp_context *psp)
806 * According to HW engineer, they prefer the TMR address be "naturally
807 * aligned" , e.g. the start address be an integer divide of TMR size.
809 * Note: this memory need be reserved till the driver
812 tmr_size = PSP_TMR_SIZE(psp->adev);
814 /* For ASICs support RLC autoload, psp will parse the toc
815 * and calculate the total size of TMR needed
817 if (!amdgpu_sriov_vf(psp->adev) &&
818 psp->toc.start_addr &&
819 psp->toc.size_bytes &&
821 ret = psp_load_toc(psp, &tmr_size);
823 dev_err(psp->adev->dev, "Failed to load toc\n");
828 if (!psp->tmr_bo && !psp->boot_time_tmr) {
829 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
830 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
832 AMDGPU_HAS_VRAM(psp->adev) ?
833 AMDGPU_GEM_DOMAIN_VRAM :
834 AMDGPU_GEM_DOMAIN_GTT,
835 &psp->tmr_bo, &psp->tmr_mc_addr,
842 static bool psp_skip_tmr(struct psp_context *psp)
844 switch (amdgpu_ip_version(psp->adev, MP0_HWIP, 0)) {
845 case IP_VERSION(11, 0, 9):
846 case IP_VERSION(11, 0, 7):
847 case IP_VERSION(13, 0, 2):
848 case IP_VERSION(13, 0, 6):
849 case IP_VERSION(13, 0, 10):
856 static int psp_tmr_load(struct psp_context *psp)
859 struct psp_gfx_cmd_resp *cmd;
861 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
862 * Already set up by host driver.
864 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
867 cmd = acquire_psp_cmd_buf(psp);
869 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
871 dev_info(psp->adev->dev, "reserve 0x%lx from 0x%llx for PSP TMR\n",
872 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
874 ret = psp_cmd_submit_buf(psp, NULL, cmd,
875 psp->fence_buf_mc_addr);
877 release_psp_cmd_buf(psp);
882 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
883 struct psp_gfx_cmd_resp *cmd)
885 if (amdgpu_sriov_vf(psp->adev))
886 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
888 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
891 static int psp_tmr_unload(struct psp_context *psp)
894 struct psp_gfx_cmd_resp *cmd;
896 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
897 * as TMR is not loaded at all
899 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
902 cmd = acquire_psp_cmd_buf(psp);
904 psp_prep_tmr_unload_cmd_buf(psp, cmd);
905 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
907 ret = psp_cmd_submit_buf(psp, NULL, cmd,
908 psp->fence_buf_mc_addr);
910 release_psp_cmd_buf(psp);
915 static int psp_tmr_terminate(struct psp_context *psp)
917 return psp_tmr_unload(psp);
920 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
921 uint64_t *output_ptr)
924 struct psp_gfx_cmd_resp *cmd;
929 if (amdgpu_sriov_vf(psp->adev))
932 cmd = acquire_psp_cmd_buf(psp);
934 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
936 ret = psp_cmd_submit_buf(psp, NULL, cmd,
937 psp->fence_buf_mc_addr);
940 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
941 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
944 release_psp_cmd_buf(psp);
949 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
951 struct psp_context *psp = &adev->psp;
952 struct psp_gfx_cmd_resp *cmd;
955 if (amdgpu_sriov_vf(adev))
958 cmd = acquire_psp_cmd_buf(psp);
960 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
961 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
963 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
966 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
969 release_psp_cmd_buf(psp);
974 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
977 struct psp_context *psp = &adev->psp;
978 struct psp_gfx_cmd_resp *cmd;
980 if (amdgpu_sriov_vf(adev))
983 cmd = acquire_psp_cmd_buf(psp);
985 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
986 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
987 cmd->cmd.boot_cfg.boot_config = boot_cfg;
988 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
990 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
992 release_psp_cmd_buf(psp);
997 static int psp_rl_load(struct amdgpu_device *adev)
1000 struct psp_context *psp = &adev->psp;
1001 struct psp_gfx_cmd_resp *cmd;
1003 if (!is_psp_fw_valid(psp->rl))
1006 cmd = acquire_psp_cmd_buf(psp);
1008 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
1009 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
1011 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
1012 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
1013 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
1014 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
1015 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
1017 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1019 release_psp_cmd_buf(psp);
1024 int psp_spatial_partition(struct psp_context *psp, int mode)
1026 struct psp_gfx_cmd_resp *cmd;
1029 if (amdgpu_sriov_vf(psp->adev))
1032 cmd = acquire_psp_cmd_buf(psp);
1034 cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1035 cmd->cmd.cmd_spatial_part.mode = mode;
1037 dev_info(psp->adev->dev, "Requesting %d partitions through PSP", mode);
1038 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1040 release_psp_cmd_buf(psp);
1045 static int psp_asd_initialize(struct psp_context *psp)
1049 /* If PSP version doesn't match ASD version, asd loading will be failed.
1050 * add workaround to bypass it for sriov now.
1051 * TODO: add version check to make it common
1053 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1056 /* bypass asd if display hardware is not available */
1057 if (!amdgpu_device_has_display_hardware(psp->adev) &&
1058 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) >= IP_VERSION(13, 0, 10))
1061 psp->asd_context.mem_context.shared_mc_addr = 0;
1062 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
1063 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
1065 ret = psp_ta_load(psp, &psp->asd_context);
1067 psp->asd_context.initialized = true;
1072 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1073 uint32_t session_id)
1075 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1076 cmd->cmd.cmd_unload_ta.session_id = session_id;
1079 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1082 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1084 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1086 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1088 context->resp_status = cmd->resp.status;
1090 release_psp_cmd_buf(psp);
1095 static int psp_asd_terminate(struct psp_context *psp)
1099 if (amdgpu_sriov_vf(psp->adev))
1102 if (!psp->asd_context.initialized)
1105 ret = psp_ta_unload(psp, &psp->asd_context);
1107 psp->asd_context.initialized = false;
1112 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1113 uint32_t id, uint32_t value)
1115 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1116 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1117 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1120 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1123 struct psp_gfx_cmd_resp *cmd;
1126 if (reg >= PSP_REG_LAST)
1129 cmd = acquire_psp_cmd_buf(psp);
1131 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1132 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1134 dev_err(psp->adev->dev, "PSP failed to program reg id %d\n", reg);
1136 release_psp_cmd_buf(psp);
1141 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1143 struct ta_context *context)
1145 cmd->cmd_id = context->ta_load_type;
1146 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1147 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1148 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1150 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1151 lower_32_bits(context->mem_context.shared_mc_addr);
1152 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1153 upper_32_bits(context->mem_context.shared_mc_addr);
1154 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1157 int psp_ta_init_shared_buf(struct psp_context *psp,
1158 struct ta_mem_context *mem_ctx)
1161 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1162 * physical) for ta to host memory
1164 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1165 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1166 AMDGPU_GEM_DOMAIN_GTT,
1167 &mem_ctx->shared_bo,
1168 &mem_ctx->shared_mc_addr,
1169 &mem_ctx->shared_buf);
1172 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1174 uint32_t session_id)
1176 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1177 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1178 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1181 int psp_ta_invoke(struct psp_context *psp,
1183 struct ta_context *context)
1186 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1188 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1190 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1191 psp->fence_buf_mc_addr);
1193 context->resp_status = cmd->resp.status;
1195 release_psp_cmd_buf(psp);
1200 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1203 struct psp_gfx_cmd_resp *cmd;
1205 cmd = acquire_psp_cmd_buf(psp);
1207 psp_copy_fw(psp, context->bin_desc.start_addr,
1208 context->bin_desc.size_bytes);
1210 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1212 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1213 psp->fence_buf_mc_addr);
1215 context->resp_status = cmd->resp.status;
1218 context->session_id = cmd->resp.session_id;
1220 release_psp_cmd_buf(psp);
1225 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1227 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1230 int psp_xgmi_terminate(struct psp_context *psp)
1233 struct amdgpu_device *adev = psp->adev;
1235 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1236 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 4) ||
1237 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 2) &&
1238 adev->gmc.xgmi.connected_to_cpu))
1241 if (!psp->xgmi_context.context.initialized)
1244 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1246 psp->xgmi_context.context.initialized = false;
1251 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1253 struct ta_xgmi_shared_memory *xgmi_cmd;
1257 !psp->xgmi_context.context.bin_desc.size_bytes ||
1258 !psp->xgmi_context.context.bin_desc.start_addr)
1264 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1265 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1267 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1268 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1274 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1276 psp->xgmi_context.context.initialized = true;
1281 /* Initialize XGMI session */
1282 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1283 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1284 xgmi_cmd->flag_extend_link_record = set_extended_data;
1285 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1287 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1288 /* note down the capbility flag for XGMI TA */
1289 psp->xgmi_context.xgmi_ta_caps = xgmi_cmd->caps_flag;
1294 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1296 struct ta_xgmi_shared_memory *xgmi_cmd;
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));
1302 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1304 /* Invoke xgmi ta to get hive id */
1305 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1309 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1314 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1316 struct ta_xgmi_shared_memory *xgmi_cmd;
1319 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1320 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1322 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1324 /* Invoke xgmi ta to get the node id */
1325 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1329 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1334 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1336 return (amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1337 IP_VERSION(13, 0, 2) &&
1338 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
1339 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) >=
1340 IP_VERSION(13, 0, 6);
1344 * Chips that support extended topology information require the driver to
1345 * reflect topology information in the opposite direction. This is
1346 * because the TA has already exceeded its link record limit and if the
1347 * TA holds bi-directional information, the driver would have to do
1348 * multiple fetches instead of just two.
1350 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1351 struct psp_xgmi_node_info node_info)
1353 struct amdgpu_device *mirror_adev;
1354 struct amdgpu_hive_info *hive;
1355 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1356 uint64_t dst_node_id = node_info.node_id;
1357 uint8_t dst_num_hops = node_info.num_hops;
1358 uint8_t dst_num_links = node_info.num_links;
1360 hive = amdgpu_get_xgmi_hive(psp->adev);
1361 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1362 struct psp_xgmi_topology_info *mirror_top_info;
1365 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1368 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1369 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1370 if (mirror_top_info->nodes[j].node_id != src_node_id)
1373 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1375 * prevent 0 num_links value re-reflection since reflection
1376 * criteria is based on num_hops (direct or indirect).
1380 mirror_top_info->nodes[j].num_links = dst_num_links;
1388 amdgpu_put_xgmi_hive(hive);
1391 int psp_xgmi_get_topology_info(struct psp_context *psp,
1393 struct psp_xgmi_topology_info *topology,
1394 bool get_extended_data)
1396 struct ta_xgmi_shared_memory *xgmi_cmd;
1397 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1398 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1402 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1405 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1406 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1407 xgmi_cmd->flag_extend_link_record = get_extended_data;
1409 /* Fill in the shared memory with topology information as input */
1410 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1411 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_TOPOLOGY_INFO;
1412 topology_info_input->num_nodes = number_devices;
1414 for (i = 0; i < topology_info_input->num_nodes; i++) {
1415 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1416 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1417 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1418 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1421 /* Invoke xgmi ta to get the topology information */
1422 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_TOPOLOGY_INFO);
1426 /* Read the output topology information from the shared memory */
1427 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1428 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1429 for (i = 0; i < topology->num_nodes; i++) {
1430 /* extended data will either be 0 or equal to non-extended data */
1431 if (topology_info_output->nodes[i].num_hops)
1432 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1434 /* non-extended data gets everything here so no need to update */
1435 if (!get_extended_data) {
1436 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1437 topology->nodes[i].is_sharing_enabled =
1438 topology_info_output->nodes[i].is_sharing_enabled;
1439 topology->nodes[i].sdma_engine =
1440 topology_info_output->nodes[i].sdma_engine;
1445 /* Invoke xgmi ta again to get the link information */
1446 if (psp_xgmi_peer_link_info_supported(psp)) {
1447 struct ta_xgmi_cmd_get_peer_link_info *link_info_output;
1448 struct ta_xgmi_cmd_get_extend_peer_link_info *link_extend_info_output;
1449 bool requires_reflection =
1450 (psp->xgmi_context.supports_extended_data &&
1451 get_extended_data) ||
1452 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1453 IP_VERSION(13, 0, 6);
1454 bool ta_port_num_support = amdgpu_sriov_vf(psp->adev) ? 0 :
1455 psp->xgmi_context.xgmi_ta_caps & EXTEND_PEER_LINK_INFO_CMD_FLAG;
1457 /* popluate the shared output buffer rather than the cmd input buffer
1458 * with node_ids as the input for GET_PEER_LINKS command execution.
1459 * This is required for GET_PEER_LINKS per xgmi ta implementation.
1460 * The same requirement for GET_EXTEND_PEER_LINKS command.
1462 if (ta_port_num_support) {
1463 link_extend_info_output = &xgmi_cmd->xgmi_out_message.get_extend_link_info;
1465 for (i = 0; i < topology->num_nodes; i++)
1466 link_extend_info_output->nodes[i].node_id = topology->nodes[i].node_id;
1468 link_extend_info_output->num_nodes = topology->num_nodes;
1469 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_EXTEND_PEER_LINKS;
1471 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1473 for (i = 0; i < topology->num_nodes; i++)
1474 link_info_output->nodes[i].node_id = topology->nodes[i].node_id;
1476 link_info_output->num_nodes = topology->num_nodes;
1477 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1480 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1484 for (i = 0; i < topology->num_nodes; i++) {
1485 uint8_t node_num_links = ta_port_num_support ?
1486 link_extend_info_output->nodes[i].num_links : link_info_output->nodes[i].num_links;
1487 /* accumulate num_links on extended data */
1488 if (get_extended_data) {
1489 topology->nodes[i].num_links = topology->nodes[i].num_links + node_num_links;
1491 topology->nodes[i].num_links = (requires_reflection && topology->nodes[i].num_links) ?
1492 topology->nodes[i].num_links : node_num_links;
1494 /* popluate the connected port num info if supported and available */
1495 if (ta_port_num_support && topology->nodes[i].num_links) {
1496 memcpy(topology->nodes[i].port_num, link_extend_info_output->nodes[i].port_num,
1497 sizeof(struct xgmi_connected_port_num) * TA_XGMI__MAX_PORT_NUM);
1500 /* reflect the topology information for bi-directionality */
1501 if (requires_reflection && topology->nodes[i].num_hops)
1502 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1509 int psp_xgmi_set_topology_info(struct psp_context *psp,
1511 struct psp_xgmi_topology_info *topology)
1513 struct ta_xgmi_shared_memory *xgmi_cmd;
1514 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1517 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1520 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1521 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1523 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1524 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1525 topology_info_input->num_nodes = number_devices;
1527 for (i = 0; i < topology_info_input->num_nodes; i++) {
1528 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1529 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1530 topology_info_input->nodes[i].is_sharing_enabled = 1;
1531 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1534 /* Invoke xgmi ta to set topology information */
1535 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1539 static void psp_ras_ta_check_status(struct psp_context *psp)
1541 struct ta_ras_shared_memory *ras_cmd =
1542 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1544 switch (ras_cmd->ras_status) {
1545 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1546 dev_warn(psp->adev->dev,
1547 "RAS WARNING: cmd failed due to unsupported ip\n");
1549 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1550 dev_warn(psp->adev->dev,
1551 "RAS WARNING: cmd failed due to unsupported error injection\n");
1553 case TA_RAS_STATUS__SUCCESS:
1555 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1556 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1557 dev_warn(psp->adev->dev,
1558 "RAS WARNING: Inject error to critical region is not allowed\n");
1561 dev_warn(psp->adev->dev,
1562 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1567 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1569 struct ta_ras_shared_memory *ras_cmd;
1572 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1575 * TODO: bypass the loading in sriov for now
1577 if (amdgpu_sriov_vf(psp->adev))
1580 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1582 if (amdgpu_ras_intr_triggered())
1585 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1586 dev_warn(psp->adev->dev, "RAS: Unsupported Interface\n");
1591 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1592 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1594 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1595 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1596 dev_warn(psp->adev->dev,
1597 "RAS internal register access blocked\n");
1599 psp_ras_ta_check_status(psp);
1605 int psp_ras_enable_features(struct psp_context *psp,
1606 union ta_ras_cmd_input *info, bool enable)
1608 struct ta_ras_shared_memory *ras_cmd;
1611 if (!psp->ras_context.context.initialized)
1614 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1615 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1618 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1620 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1622 ras_cmd->ras_in_message = *info;
1624 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1631 int psp_ras_terminate(struct psp_context *psp)
1636 * TODO: bypass the terminate in sriov for now
1638 if (amdgpu_sriov_vf(psp->adev))
1641 if (!psp->ras_context.context.initialized)
1644 ret = psp_ta_unload(psp, &psp->ras_context.context);
1646 psp->ras_context.context.initialized = false;
1651 int psp_ras_initialize(struct psp_context *psp)
1654 uint32_t boot_cfg = 0xFF;
1655 struct amdgpu_device *adev = psp->adev;
1656 struct ta_ras_shared_memory *ras_cmd;
1659 * TODO: bypass the initialize in sriov for now
1661 if (amdgpu_sriov_vf(adev))
1664 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1665 !adev->psp.ras_context.context.bin_desc.start_addr) {
1666 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1670 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1671 /* query GECC enablement status from boot config
1672 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1674 ret = psp_boot_config_get(adev, &boot_cfg);
1676 dev_warn(adev->dev, "PSP get boot config failed\n");
1678 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1680 dev_info(adev->dev, "GECC is disabled\n");
1682 /* disable GECC in next boot cycle if ras is
1683 * disabled by module parameter amdgpu_ras_enable
1684 * and/or amdgpu_ras_mask, or boot_config_get call
1687 ret = psp_boot_config_set(adev, 0);
1689 dev_warn(adev->dev, "PSP set boot config failed\n");
1691 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");
1694 if (boot_cfg == 1) {
1695 dev_info(adev->dev, "GECC is enabled\n");
1697 /* enable GECC in next boot cycle if it is disabled
1698 * in boot config, or force enable GECC if failed to
1699 * get boot configuration
1701 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1703 dev_warn(adev->dev, "PSP set boot config failed\n");
1705 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1710 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1711 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1713 if (!psp->ras_context.context.mem_context.shared_buf) {
1714 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1719 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1720 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1722 if (amdgpu_ras_is_poison_mode_supported(adev))
1723 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1724 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu)
1725 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1726 ras_cmd->ras_in_message.init_flags.xcc_mask =
1728 ras_cmd->ras_in_message.init_flags.channel_dis_num = hweight32(adev->gmc.m_half_use) * 2;
1730 ret = psp_ta_load(psp, &psp->ras_context.context);
1732 if (!ret && !ras_cmd->ras_status)
1733 psp->ras_context.context.initialized = true;
1735 if (ras_cmd->ras_status)
1736 dev_warn(adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1738 /* fail to load RAS TA */
1739 psp->ras_context.context.initialized = false;
1745 int psp_ras_trigger_error(struct psp_context *psp,
1746 struct ta_ras_trigger_error_input *info, uint32_t instance_mask)
1748 struct ta_ras_shared_memory *ras_cmd;
1749 struct amdgpu_device *adev = psp->adev;
1753 if (!psp->ras_context.context.initialized)
1756 switch (info->block_id) {
1757 case TA_RAS_BLOCK__GFX:
1758 dev_mask = GET_MASK(GC, instance_mask);
1760 case TA_RAS_BLOCK__SDMA:
1761 dev_mask = GET_MASK(SDMA0, instance_mask);
1763 case TA_RAS_BLOCK__VCN:
1764 case TA_RAS_BLOCK__JPEG:
1765 dev_mask = GET_MASK(VCN, instance_mask);
1768 dev_mask = instance_mask;
1772 /* reuse sub_block_index for backward compatibility */
1773 dev_mask <<= AMDGPU_RAS_INST_SHIFT;
1774 dev_mask &= AMDGPU_RAS_INST_MASK;
1775 info->sub_block_index |= dev_mask;
1777 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1778 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1780 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1781 ras_cmd->ras_in_message.trigger_error = *info;
1783 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1787 /* If err_event_athub occurs error inject was successful, however
1788 * return status from TA is no long reliable
1790 if (amdgpu_ras_intr_triggered())
1793 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1795 else if (ras_cmd->ras_status)
1801 int psp_ras_query_address(struct psp_context *psp,
1802 struct ta_ras_query_address_input *addr_in,
1803 struct ta_ras_query_address_output *addr_out)
1805 struct ta_ras_shared_memory *ras_cmd;
1808 if (!psp->ras_context.context.initialized)
1811 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1812 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1814 ras_cmd->cmd_id = TA_RAS_COMMAND__QUERY_ADDRESS;
1815 ras_cmd->ras_in_message.address = *addr_in;
1817 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1818 if (ret || ras_cmd->ras_status || psp->cmd_buf_mem->resp.status)
1821 *addr_out = ras_cmd->ras_out_message.address;
1828 static int psp_hdcp_initialize(struct psp_context *psp)
1833 * TODO: bypass the initialize in sriov for now
1835 if (amdgpu_sriov_vf(psp->adev))
1838 /* bypass hdcp initialization if dmu is harvested */
1839 if (!amdgpu_device_has_display_hardware(psp->adev))
1842 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1843 !psp->hdcp_context.context.bin_desc.start_addr) {
1844 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1848 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1849 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1851 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1852 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1857 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1859 psp->hdcp_context.context.initialized = true;
1860 mutex_init(&psp->hdcp_context.mutex);
1866 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1869 * TODO: bypass the loading in sriov for now
1871 if (amdgpu_sriov_vf(psp->adev))
1874 if (!psp->hdcp_context.context.initialized)
1877 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1880 static int psp_hdcp_terminate(struct psp_context *psp)
1885 * TODO: bypass the terminate in sriov for now
1887 if (amdgpu_sriov_vf(psp->adev))
1890 if (!psp->hdcp_context.context.initialized)
1893 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1895 psp->hdcp_context.context.initialized = false;
1902 static int psp_dtm_initialize(struct psp_context *psp)
1907 * TODO: bypass the initialize in sriov for now
1909 if (amdgpu_sriov_vf(psp->adev))
1912 /* bypass dtm initialization if dmu is harvested */
1913 if (!amdgpu_device_has_display_hardware(psp->adev))
1916 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1917 !psp->dtm_context.context.bin_desc.start_addr) {
1918 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1922 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1923 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1925 if (!psp->dtm_context.context.mem_context.shared_buf) {
1926 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1931 ret = psp_ta_load(psp, &psp->dtm_context.context);
1933 psp->dtm_context.context.initialized = true;
1934 mutex_init(&psp->dtm_context.mutex);
1940 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1943 * TODO: bypass the loading in sriov for now
1945 if (amdgpu_sriov_vf(psp->adev))
1948 if (!psp->dtm_context.context.initialized)
1951 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1954 static int psp_dtm_terminate(struct psp_context *psp)
1959 * TODO: bypass the terminate in sriov for now
1961 if (amdgpu_sriov_vf(psp->adev))
1964 if (!psp->dtm_context.context.initialized)
1967 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1969 psp->dtm_context.context.initialized = false;
1976 static int psp_rap_initialize(struct psp_context *psp)
1979 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1982 * TODO: bypass the initialize in sriov for now
1984 if (amdgpu_sriov_vf(psp->adev))
1987 if (!psp->rap_context.context.bin_desc.size_bytes ||
1988 !psp->rap_context.context.bin_desc.start_addr) {
1989 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1993 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1994 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1996 if (!psp->rap_context.context.mem_context.shared_buf) {
1997 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
2002 ret = psp_ta_load(psp, &psp->rap_context.context);
2004 psp->rap_context.context.initialized = true;
2005 mutex_init(&psp->rap_context.mutex);
2009 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
2010 if (ret || status != TA_RAP_STATUS__SUCCESS) {
2011 psp_rap_terminate(psp);
2012 /* free rap shared memory */
2013 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
2015 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
2024 static int psp_rap_terminate(struct psp_context *psp)
2028 if (!psp->rap_context.context.initialized)
2031 ret = psp_ta_unload(psp, &psp->rap_context.context);
2033 psp->rap_context.context.initialized = false;
2038 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
2040 struct ta_rap_shared_memory *rap_cmd;
2043 if (!psp->rap_context.context.initialized)
2046 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
2047 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
2050 mutex_lock(&psp->rap_context.mutex);
2052 rap_cmd = (struct ta_rap_shared_memory *)
2053 psp->rap_context.context.mem_context.shared_buf;
2054 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
2056 rap_cmd->cmd_id = ta_cmd_id;
2057 rap_cmd->validation_method_id = METHOD_A;
2059 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
2064 *status = rap_cmd->rap_status;
2067 mutex_unlock(&psp->rap_context.mutex);
2073 /* securedisplay start */
2074 static int psp_securedisplay_initialize(struct psp_context *psp)
2077 struct ta_securedisplay_cmd *securedisplay_cmd;
2080 * TODO: bypass the initialize in sriov for now
2082 if (amdgpu_sriov_vf(psp->adev))
2085 /* bypass securedisplay initialization if dmu is harvested */
2086 if (!amdgpu_device_has_display_hardware(psp->adev))
2089 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
2090 !psp->securedisplay_context.context.bin_desc.start_addr) {
2091 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
2095 psp->securedisplay_context.context.mem_context.shared_mem_size =
2096 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
2097 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
2099 if (!psp->securedisplay_context.context.initialized) {
2100 ret = psp_ta_init_shared_buf(psp,
2101 &psp->securedisplay_context.context.mem_context);
2106 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
2108 psp->securedisplay_context.context.initialized = true;
2109 mutex_init(&psp->securedisplay_context.mutex);
2113 mutex_lock(&psp->securedisplay_context.mutex);
2115 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
2116 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2118 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2120 mutex_unlock(&psp->securedisplay_context.mutex);
2123 psp_securedisplay_terminate(psp);
2124 /* free securedisplay shared memory */
2125 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
2126 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
2130 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
2131 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
2132 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
2133 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
2134 /* don't try again */
2135 psp->securedisplay_context.context.bin_desc.size_bytes = 0;
2141 static int psp_securedisplay_terminate(struct psp_context *psp)
2146 * TODO:bypass the terminate in sriov for now
2148 if (amdgpu_sriov_vf(psp->adev))
2151 if (!psp->securedisplay_context.context.initialized)
2154 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2156 psp->securedisplay_context.context.initialized = false;
2161 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2165 if (!psp->securedisplay_context.context.initialized)
2168 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2169 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2172 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2176 /* SECUREDISPLAY end */
2178 int amdgpu_psp_wait_for_bootloader(struct amdgpu_device *adev)
2180 struct psp_context *psp = &adev->psp;
2183 if (!amdgpu_sriov_vf(adev) && psp->funcs && psp->funcs->wait_for_bootloader != NULL)
2184 ret = psp->funcs->wait_for_bootloader(psp);
2189 bool amdgpu_psp_get_ras_capability(struct psp_context *psp)
2192 psp->funcs->get_ras_capability) {
2193 return psp->funcs->get_ras_capability(psp);
2199 static int psp_hw_start(struct psp_context *psp)
2201 struct amdgpu_device *adev = psp->adev;
2204 if (!amdgpu_sriov_vf(adev)) {
2205 if ((is_psp_fw_valid(psp->kdb)) &&
2206 (psp->funcs->bootloader_load_kdb != NULL)) {
2207 ret = psp_bootloader_load_kdb(psp);
2209 dev_err(adev->dev, "PSP load kdb failed!\n");
2214 if ((is_psp_fw_valid(psp->spl)) &&
2215 (psp->funcs->bootloader_load_spl != NULL)) {
2216 ret = psp_bootloader_load_spl(psp);
2218 dev_err(adev->dev, "PSP load spl failed!\n");
2223 if ((is_psp_fw_valid(psp->sys)) &&
2224 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2225 ret = psp_bootloader_load_sysdrv(psp);
2227 dev_err(adev->dev, "PSP load sys drv failed!\n");
2232 if ((is_psp_fw_valid(psp->soc_drv)) &&
2233 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2234 ret = psp_bootloader_load_soc_drv(psp);
2236 dev_err(adev->dev, "PSP load soc drv failed!\n");
2241 if ((is_psp_fw_valid(psp->intf_drv)) &&
2242 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2243 ret = psp_bootloader_load_intf_drv(psp);
2245 dev_err(adev->dev, "PSP load intf drv failed!\n");
2250 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2251 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2252 ret = psp_bootloader_load_dbg_drv(psp);
2254 dev_err(adev->dev, "PSP load dbg drv failed!\n");
2259 if ((is_psp_fw_valid(psp->ras_drv)) &&
2260 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2261 ret = psp_bootloader_load_ras_drv(psp);
2263 dev_err(adev->dev, "PSP load ras_drv failed!\n");
2268 if ((is_psp_fw_valid(psp->ipkeymgr_drv)) &&
2269 (psp->funcs->bootloader_load_ipkeymgr_drv != NULL)) {
2270 ret = psp_bootloader_load_ipkeymgr_drv(psp);
2272 dev_err(adev->dev, "PSP load ipkeymgr_drv failed!\n");
2277 if ((is_psp_fw_valid(psp->sos)) &&
2278 (psp->funcs->bootloader_load_sos != NULL)) {
2279 ret = psp_bootloader_load_sos(psp);
2281 dev_err(adev->dev, "PSP load sos failed!\n");
2287 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2289 dev_err(adev->dev, "PSP create ring failed!\n");
2293 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2296 if (!psp->boot_time_tmr || psp->autoload_supported) {
2297 ret = psp_tmr_init(psp);
2299 dev_err(adev->dev, "PSP tmr init failed!\n");
2306 * For ASICs with DF Cstate management centralized
2307 * to PMFW, TMR setup should be performed after PMFW
2308 * loaded and before other non-psp firmware loaded.
2310 if (psp->pmfw_centralized_cstate_management) {
2311 ret = psp_load_smu_fw(psp);
2316 if (!psp->boot_time_tmr || !psp->autoload_supported) {
2317 ret = psp_tmr_load(psp);
2319 dev_err(adev->dev, "PSP load tmr failed!\n");
2327 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2328 enum psp_gfx_fw_type *type)
2330 switch (ucode->ucode_id) {
2331 case AMDGPU_UCODE_ID_CAP:
2332 *type = GFX_FW_TYPE_CAP;
2334 case AMDGPU_UCODE_ID_SDMA0:
2335 *type = GFX_FW_TYPE_SDMA0;
2337 case AMDGPU_UCODE_ID_SDMA1:
2338 *type = GFX_FW_TYPE_SDMA1;
2340 case AMDGPU_UCODE_ID_SDMA2:
2341 *type = GFX_FW_TYPE_SDMA2;
2343 case AMDGPU_UCODE_ID_SDMA3:
2344 *type = GFX_FW_TYPE_SDMA3;
2346 case AMDGPU_UCODE_ID_SDMA4:
2347 *type = GFX_FW_TYPE_SDMA4;
2349 case AMDGPU_UCODE_ID_SDMA5:
2350 *type = GFX_FW_TYPE_SDMA5;
2352 case AMDGPU_UCODE_ID_SDMA6:
2353 *type = GFX_FW_TYPE_SDMA6;
2355 case AMDGPU_UCODE_ID_SDMA7:
2356 *type = GFX_FW_TYPE_SDMA7;
2358 case AMDGPU_UCODE_ID_CP_MES:
2359 *type = GFX_FW_TYPE_CP_MES;
2361 case AMDGPU_UCODE_ID_CP_MES_DATA:
2362 *type = GFX_FW_TYPE_MES_STACK;
2364 case AMDGPU_UCODE_ID_CP_MES1:
2365 *type = GFX_FW_TYPE_CP_MES_KIQ;
2367 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2368 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2370 case AMDGPU_UCODE_ID_CP_CE:
2371 *type = GFX_FW_TYPE_CP_CE;
2373 case AMDGPU_UCODE_ID_CP_PFP:
2374 *type = GFX_FW_TYPE_CP_PFP;
2376 case AMDGPU_UCODE_ID_CP_ME:
2377 *type = GFX_FW_TYPE_CP_ME;
2379 case AMDGPU_UCODE_ID_CP_MEC1:
2380 *type = GFX_FW_TYPE_CP_MEC;
2382 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2383 *type = GFX_FW_TYPE_CP_MEC_ME1;
2385 case AMDGPU_UCODE_ID_CP_MEC2:
2386 *type = GFX_FW_TYPE_CP_MEC;
2388 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2389 *type = GFX_FW_TYPE_CP_MEC_ME2;
2391 case AMDGPU_UCODE_ID_RLC_P:
2392 *type = GFX_FW_TYPE_RLC_P;
2394 case AMDGPU_UCODE_ID_RLC_V:
2395 *type = GFX_FW_TYPE_RLC_V;
2397 case AMDGPU_UCODE_ID_RLC_G:
2398 *type = GFX_FW_TYPE_RLC_G;
2400 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2401 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2403 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2404 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2406 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2407 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2409 case AMDGPU_UCODE_ID_RLC_IRAM:
2410 *type = GFX_FW_TYPE_RLC_IRAM;
2412 case AMDGPU_UCODE_ID_RLC_DRAM:
2413 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2415 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2416 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2418 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2419 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2421 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2422 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2424 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2425 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2427 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2428 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2430 case AMDGPU_UCODE_ID_SMC:
2431 *type = GFX_FW_TYPE_SMU;
2433 case AMDGPU_UCODE_ID_PPTABLE:
2434 *type = GFX_FW_TYPE_PPTABLE;
2436 case AMDGPU_UCODE_ID_UVD:
2437 *type = GFX_FW_TYPE_UVD;
2439 case AMDGPU_UCODE_ID_UVD1:
2440 *type = GFX_FW_TYPE_UVD1;
2442 case AMDGPU_UCODE_ID_VCE:
2443 *type = GFX_FW_TYPE_VCE;
2445 case AMDGPU_UCODE_ID_VCN:
2446 *type = GFX_FW_TYPE_VCN;
2448 case AMDGPU_UCODE_ID_VCN1:
2449 *type = GFX_FW_TYPE_VCN1;
2451 case AMDGPU_UCODE_ID_DMCU_ERAM:
2452 *type = GFX_FW_TYPE_DMCU_ERAM;
2454 case AMDGPU_UCODE_ID_DMCU_INTV:
2455 *type = GFX_FW_TYPE_DMCU_ISR;
2457 case AMDGPU_UCODE_ID_VCN0_RAM:
2458 *type = GFX_FW_TYPE_VCN0_RAM;
2460 case AMDGPU_UCODE_ID_VCN1_RAM:
2461 *type = GFX_FW_TYPE_VCN1_RAM;
2463 case AMDGPU_UCODE_ID_DMCUB:
2464 *type = GFX_FW_TYPE_DMUB;
2466 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2467 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2469 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2470 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2472 case AMDGPU_UCODE_ID_IMU_I:
2473 *type = GFX_FW_TYPE_IMU_I;
2475 case AMDGPU_UCODE_ID_IMU_D:
2476 *type = GFX_FW_TYPE_IMU_D;
2478 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2479 *type = GFX_FW_TYPE_RS64_PFP;
2481 case AMDGPU_UCODE_ID_CP_RS64_ME:
2482 *type = GFX_FW_TYPE_RS64_ME;
2484 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2485 *type = GFX_FW_TYPE_RS64_MEC;
2487 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2488 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2490 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2491 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2493 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2494 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2496 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2497 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2499 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2500 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2502 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2503 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2505 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2506 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2508 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2509 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2511 case AMDGPU_UCODE_ID_VPE_CTX:
2512 *type = GFX_FW_TYPE_VPEC_FW1;
2514 case AMDGPU_UCODE_ID_VPE_CTL:
2515 *type = GFX_FW_TYPE_VPEC_FW2;
2517 case AMDGPU_UCODE_ID_VPE:
2518 *type = GFX_FW_TYPE_VPE;
2520 case AMDGPU_UCODE_ID_UMSCH_MM_UCODE:
2521 *type = GFX_FW_TYPE_UMSCH_UCODE;
2523 case AMDGPU_UCODE_ID_UMSCH_MM_DATA:
2524 *type = GFX_FW_TYPE_UMSCH_DATA;
2526 case AMDGPU_UCODE_ID_UMSCH_MM_CMD_BUFFER:
2527 *type = GFX_FW_TYPE_UMSCH_CMD_BUFFER;
2529 case AMDGPU_UCODE_ID_P2S_TABLE:
2530 *type = GFX_FW_TYPE_P2S_TABLE;
2532 case AMDGPU_UCODE_ID_JPEG_RAM:
2533 *type = GFX_FW_TYPE_JPEG_RAM;
2535 case AMDGPU_UCODE_ID_MAXIMUM:
2543 static void psp_print_fw_hdr(struct psp_context *psp,
2544 struct amdgpu_firmware_info *ucode)
2546 struct amdgpu_device *adev = psp->adev;
2547 struct common_firmware_header *hdr;
2549 switch (ucode->ucode_id) {
2550 case AMDGPU_UCODE_ID_SDMA0:
2551 case AMDGPU_UCODE_ID_SDMA1:
2552 case AMDGPU_UCODE_ID_SDMA2:
2553 case AMDGPU_UCODE_ID_SDMA3:
2554 case AMDGPU_UCODE_ID_SDMA4:
2555 case AMDGPU_UCODE_ID_SDMA5:
2556 case AMDGPU_UCODE_ID_SDMA6:
2557 case AMDGPU_UCODE_ID_SDMA7:
2558 hdr = (struct common_firmware_header *)
2559 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2560 amdgpu_ucode_print_sdma_hdr(hdr);
2562 case AMDGPU_UCODE_ID_CP_CE:
2563 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2564 amdgpu_ucode_print_gfx_hdr(hdr);
2566 case AMDGPU_UCODE_ID_CP_PFP:
2567 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2568 amdgpu_ucode_print_gfx_hdr(hdr);
2570 case AMDGPU_UCODE_ID_CP_ME:
2571 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2572 amdgpu_ucode_print_gfx_hdr(hdr);
2574 case AMDGPU_UCODE_ID_CP_MEC1:
2575 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2576 amdgpu_ucode_print_gfx_hdr(hdr);
2578 case AMDGPU_UCODE_ID_RLC_G:
2579 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2580 amdgpu_ucode_print_rlc_hdr(hdr);
2582 case AMDGPU_UCODE_ID_SMC:
2583 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2584 amdgpu_ucode_print_smc_hdr(hdr);
2591 static int psp_prep_load_ip_fw_cmd_buf(struct psp_context *psp,
2592 struct amdgpu_firmware_info *ucode,
2593 struct psp_gfx_cmd_resp *cmd)
2596 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2598 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2599 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2600 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2601 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2603 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2605 dev_err(psp->adev->dev, "Unknown firmware type\n");
2610 int psp_execute_ip_fw_load(struct psp_context *psp,
2611 struct amdgpu_firmware_info *ucode)
2614 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2616 ret = psp_prep_load_ip_fw_cmd_buf(psp, ucode, cmd);
2618 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2619 psp->fence_buf_mc_addr);
2622 release_psp_cmd_buf(psp);
2627 static int psp_load_p2s_table(struct psp_context *psp)
2630 struct amdgpu_device *adev = psp->adev;
2631 struct amdgpu_firmware_info *ucode =
2632 &adev->firmware.ucode[AMDGPU_UCODE_ID_P2S_TABLE];
2634 if (adev->in_runpm && ((adev->pm.rpm_mode == AMDGPU_RUNPM_BACO) ||
2635 (adev->pm.rpm_mode == AMDGPU_RUNPM_BAMACO)))
2638 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 6)) {
2639 uint32_t supp_vers = adev->flags & AMD_IS_APU ? 0x0036013D :
2641 if (psp->sos.fw_version < supp_vers)
2645 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2648 ret = psp_execute_ip_fw_load(psp, ucode);
2653 static int psp_load_smu_fw(struct psp_context *psp)
2656 struct amdgpu_device *adev = psp->adev;
2657 struct amdgpu_firmware_info *ucode =
2658 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2659 struct amdgpu_ras *ras = psp->ras_context.ras;
2662 * Skip SMU FW reloading in case of using BACO for runpm only,
2663 * as SMU is always alive.
2665 if (adev->in_runpm && ((adev->pm.rpm_mode == AMDGPU_RUNPM_BACO) ||
2666 (adev->pm.rpm_mode == AMDGPU_RUNPM_BAMACO)))
2669 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2672 if ((amdgpu_in_reset(adev) && ras && adev->ras_enabled &&
2673 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 4) ||
2674 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 2)))) {
2675 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2677 dev_err(adev->dev, "Failed to set MP1 state prepare for reload\n");
2680 ret = psp_execute_ip_fw_load(psp, ucode);
2683 dev_err(adev->dev, "PSP load smu failed!\n");
2688 static bool fw_load_skip_check(struct psp_context *psp,
2689 struct amdgpu_firmware_info *ucode)
2691 if (!ucode->fw || !ucode->ucode_size)
2694 if (ucode->ucode_id == AMDGPU_UCODE_ID_P2S_TABLE)
2697 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2698 (psp_smu_reload_quirk(psp) ||
2699 psp->autoload_supported ||
2700 psp->pmfw_centralized_cstate_management))
2703 if (amdgpu_sriov_vf(psp->adev) &&
2704 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2707 if (psp->autoload_supported &&
2708 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2709 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2710 /* skip mec JT when autoload is enabled */
2716 int psp_load_fw_list(struct psp_context *psp,
2717 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2720 struct amdgpu_firmware_info *ucode;
2722 for (i = 0; i < ucode_count; ++i) {
2723 ucode = ucode_list[i];
2724 psp_print_fw_hdr(psp, ucode);
2725 ret = psp_execute_ip_fw_load(psp, ucode);
2732 static int psp_load_non_psp_fw(struct psp_context *psp)
2735 struct amdgpu_firmware_info *ucode;
2736 struct amdgpu_device *adev = psp->adev;
2738 if (psp->autoload_supported &&
2739 !psp->pmfw_centralized_cstate_management) {
2740 ret = psp_load_smu_fw(psp);
2745 /* Load P2S table first if it's available */
2746 psp_load_p2s_table(psp);
2748 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2749 ucode = &adev->firmware.ucode[i];
2751 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2752 !fw_load_skip_check(psp, ucode)) {
2753 ret = psp_load_smu_fw(psp);
2759 if (fw_load_skip_check(psp, ucode))
2762 if (psp->autoload_supported &&
2763 (amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2764 IP_VERSION(11, 0, 7) ||
2765 amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2766 IP_VERSION(11, 0, 11) ||
2767 amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2768 IP_VERSION(11, 0, 12)) &&
2769 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2770 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2771 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2772 /* PSP only receive one SDMA fw for sienna_cichlid,
2773 * as all four sdma fw are same
2777 psp_print_fw_hdr(psp, ucode);
2779 ret = psp_execute_ip_fw_load(psp, ucode);
2783 /* Start rlc autoload after psp recieved all the gfx firmware */
2784 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2785 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2786 ret = psp_rlc_autoload_start(psp);
2788 dev_err(adev->dev, "Failed to start rlc autoload\n");
2797 static int psp_load_fw(struct amdgpu_device *adev)
2800 struct psp_context *psp = &adev->psp;
2802 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2803 /* should not destroy ring, only stop */
2804 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2806 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2808 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2810 dev_err(adev->dev, "PSP ring init failed!\n");
2815 ret = psp_hw_start(psp);
2819 ret = psp_load_non_psp_fw(psp);
2823 ret = psp_asd_initialize(psp);
2825 dev_err(adev->dev, "PSP load asd failed!\n");
2829 ret = psp_rl_load(adev);
2831 dev_err(adev->dev, "PSP load RL failed!\n");
2835 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2836 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2837 ret = psp_xgmi_initialize(psp, false, true);
2838 /* Warning the XGMI seesion initialize failure
2839 * Instead of stop driver initialization
2842 dev_err(psp->adev->dev,
2843 "XGMI: Failed to initialize XGMI session\n");
2848 ret = psp_ras_initialize(psp);
2850 dev_err(psp->adev->dev,
2851 "RAS: Failed to initialize RAS\n");
2853 ret = psp_hdcp_initialize(psp);
2855 dev_err(psp->adev->dev,
2856 "HDCP: Failed to initialize HDCP\n");
2858 ret = psp_dtm_initialize(psp);
2860 dev_err(psp->adev->dev,
2861 "DTM: Failed to initialize DTM\n");
2863 ret = psp_rap_initialize(psp);
2865 dev_err(psp->adev->dev,
2866 "RAP: Failed to initialize RAP\n");
2868 ret = psp_securedisplay_initialize(psp);
2870 dev_err(psp->adev->dev,
2871 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2877 psp_free_shared_bufs(psp);
2880 * all cleanup jobs (xgmi terminate, ras terminate,
2881 * ring destroy, cmd/fence/fw buffers destory,
2882 * psp->cmd destory) are delayed to psp_hw_fini
2884 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2888 static int psp_hw_init(void *handle)
2891 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2893 mutex_lock(&adev->firmware.mutex);
2895 * This sequence is just used on hw_init only once, no need on
2898 ret = amdgpu_ucode_init_bo(adev);
2902 ret = psp_load_fw(adev);
2904 dev_err(adev->dev, "PSP firmware loading failed\n");
2908 mutex_unlock(&adev->firmware.mutex);
2912 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2913 mutex_unlock(&adev->firmware.mutex);
2917 static int psp_hw_fini(void *handle)
2919 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2920 struct psp_context *psp = &adev->psp;
2923 psp_ras_terminate(psp);
2924 psp_securedisplay_terminate(psp);
2925 psp_rap_terminate(psp);
2926 psp_dtm_terminate(psp);
2927 psp_hdcp_terminate(psp);
2929 if (adev->gmc.xgmi.num_physical_nodes > 1)
2930 psp_xgmi_terminate(psp);
2933 psp_asd_terminate(psp);
2934 psp_tmr_terminate(psp);
2936 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2941 static int psp_suspend(void *handle)
2944 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2945 struct psp_context *psp = &adev->psp;
2947 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2948 psp->xgmi_context.context.initialized) {
2949 ret = psp_xgmi_terminate(psp);
2951 dev_err(adev->dev, "Failed to terminate xgmi ta\n");
2957 ret = psp_ras_terminate(psp);
2959 dev_err(adev->dev, "Failed to terminate ras ta\n");
2962 ret = psp_hdcp_terminate(psp);
2964 dev_err(adev->dev, "Failed to terminate hdcp ta\n");
2967 ret = psp_dtm_terminate(psp);
2969 dev_err(adev->dev, "Failed to terminate dtm ta\n");
2972 ret = psp_rap_terminate(psp);
2974 dev_err(adev->dev, "Failed to terminate rap ta\n");
2977 ret = psp_securedisplay_terminate(psp);
2979 dev_err(adev->dev, "Failed to terminate securedisplay ta\n");
2984 ret = psp_asd_terminate(psp);
2986 dev_err(adev->dev, "Failed to terminate asd\n");
2990 ret = psp_tmr_terminate(psp);
2992 dev_err(adev->dev, "Failed to terminate tmr\n");
2996 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2998 dev_err(adev->dev, "PSP ring stop failed\n");
3004 static int psp_resume(void *handle)
3007 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3008 struct psp_context *psp = &adev->psp;
3010 dev_info(adev->dev, "PSP is resuming...\n");
3012 if (psp->mem_train_ctx.enable_mem_training) {
3013 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
3015 dev_err(adev->dev, "Failed to process memory training!\n");
3020 mutex_lock(&adev->firmware.mutex);
3022 ret = psp_hw_start(psp);
3026 ret = psp_load_non_psp_fw(psp);
3030 ret = psp_asd_initialize(psp);
3032 dev_err(adev->dev, "PSP load asd failed!\n");
3036 ret = psp_rl_load(adev);
3038 dev_err(adev->dev, "PSP load RL failed!\n");
3042 if (adev->gmc.xgmi.num_physical_nodes > 1) {
3043 ret = psp_xgmi_initialize(psp, false, true);
3044 /* Warning the XGMI seesion initialize failure
3045 * Instead of stop driver initialization
3048 dev_err(psp->adev->dev,
3049 "XGMI: Failed to initialize XGMI session\n");
3053 ret = psp_ras_initialize(psp);
3055 dev_err(psp->adev->dev,
3056 "RAS: Failed to initialize RAS\n");
3058 ret = psp_hdcp_initialize(psp);
3060 dev_err(psp->adev->dev,
3061 "HDCP: Failed to initialize HDCP\n");
3063 ret = psp_dtm_initialize(psp);
3065 dev_err(psp->adev->dev,
3066 "DTM: Failed to initialize DTM\n");
3068 ret = psp_rap_initialize(psp);
3070 dev_err(psp->adev->dev,
3071 "RAP: Failed to initialize RAP\n");
3073 ret = psp_securedisplay_initialize(psp);
3075 dev_err(psp->adev->dev,
3076 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
3079 mutex_unlock(&adev->firmware.mutex);
3084 dev_err(adev->dev, "PSP resume failed\n");
3085 mutex_unlock(&adev->firmware.mutex);
3089 int psp_gpu_reset(struct amdgpu_device *adev)
3093 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
3096 mutex_lock(&adev->psp.mutex);
3097 ret = psp_mode1_reset(&adev->psp);
3098 mutex_unlock(&adev->psp.mutex);
3103 int psp_rlc_autoload_start(struct psp_context *psp)
3106 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
3108 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
3110 ret = psp_cmd_submit_buf(psp, NULL, cmd,
3111 psp->fence_buf_mc_addr);
3113 release_psp_cmd_buf(psp);
3118 int psp_ring_cmd_submit(struct psp_context *psp,
3119 uint64_t cmd_buf_mc_addr,
3120 uint64_t fence_mc_addr,
3123 unsigned int psp_write_ptr_reg = 0;
3124 struct psp_gfx_rb_frame *write_frame;
3125 struct psp_ring *ring = &psp->km_ring;
3126 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
3127 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
3128 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
3129 struct amdgpu_device *adev = psp->adev;
3130 uint32_t ring_size_dw = ring->ring_size / 4;
3131 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
3133 /* KM (GPCOM) prepare write pointer */
3134 psp_write_ptr_reg = psp_ring_get_wptr(psp);
3136 /* Update KM RB frame pointer to new frame */
3137 /* write_frame ptr increments by size of rb_frame in bytes */
3138 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
3139 if ((psp_write_ptr_reg % ring_size_dw) == 0)
3140 write_frame = ring_buffer_start;
3142 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
3143 /* Check invalid write_frame ptr address */
3144 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
3146 "ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
3147 ring_buffer_start, ring_buffer_end, write_frame);
3149 "write_frame is pointing to address out of bounds\n");
3153 /* Initialize KM RB frame */
3154 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
3156 /* Update KM RB frame */
3157 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
3158 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
3159 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
3160 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
3161 write_frame->fence_value = index;
3162 amdgpu_device_flush_hdp(adev, NULL);
3164 /* Update the write Pointer in DWORDs */
3165 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
3166 psp_ring_set_wptr(psp, psp_write_ptr_reg);
3170 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
3172 struct amdgpu_device *adev = psp->adev;
3173 char fw_name[PSP_FW_NAME_LEN];
3174 const struct psp_firmware_header_v1_0 *asd_hdr;
3177 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
3178 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
3182 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
3183 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
3184 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
3185 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
3186 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
3187 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
3190 amdgpu_ucode_release(&adev->psp.asd_fw);
3194 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
3196 struct amdgpu_device *adev = psp->adev;
3197 char fw_name[PSP_FW_NAME_LEN];
3198 const struct psp_firmware_header_v1_0 *toc_hdr;
3201 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
3202 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
3206 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
3207 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
3208 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
3209 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
3210 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
3211 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
3214 amdgpu_ucode_release(&adev->psp.toc_fw);
3218 static int parse_sos_bin_descriptor(struct psp_context *psp,
3219 const struct psp_fw_bin_desc *desc,
3220 const struct psp_firmware_header_v2_0 *sos_hdr)
3222 uint8_t *ucode_start_addr = NULL;
3224 if (!psp || !desc || !sos_hdr)
3227 ucode_start_addr = (uint8_t *)sos_hdr +
3228 le32_to_cpu(desc->offset_bytes) +
3229 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3231 switch (desc->fw_type) {
3232 case PSP_FW_TYPE_PSP_SOS:
3233 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3234 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3235 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3236 psp->sos.start_addr = ucode_start_addr;
3238 case PSP_FW_TYPE_PSP_SYS_DRV:
3239 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3240 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3241 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3242 psp->sys.start_addr = ucode_start_addr;
3244 case PSP_FW_TYPE_PSP_KDB:
3245 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3246 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3247 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3248 psp->kdb.start_addr = ucode_start_addr;
3250 case PSP_FW_TYPE_PSP_TOC:
3251 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3252 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3253 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3254 psp->toc.start_addr = ucode_start_addr;
3256 case PSP_FW_TYPE_PSP_SPL:
3257 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3258 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3259 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3260 psp->spl.start_addr = ucode_start_addr;
3262 case PSP_FW_TYPE_PSP_RL:
3263 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3264 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3265 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3266 psp->rl.start_addr = ucode_start_addr;
3268 case PSP_FW_TYPE_PSP_SOC_DRV:
3269 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3270 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3271 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3272 psp->soc_drv.start_addr = ucode_start_addr;
3274 case PSP_FW_TYPE_PSP_INTF_DRV:
3275 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3276 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3277 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3278 psp->intf_drv.start_addr = ucode_start_addr;
3280 case PSP_FW_TYPE_PSP_DBG_DRV:
3281 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3282 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3283 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3284 psp->dbg_drv.start_addr = ucode_start_addr;
3286 case PSP_FW_TYPE_PSP_RAS_DRV:
3287 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3288 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3289 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3290 psp->ras_drv.start_addr = ucode_start_addr;
3292 case PSP_FW_TYPE_PSP_IPKEYMGR_DRV:
3293 psp->ipkeymgr_drv.fw_version = le32_to_cpu(desc->fw_version);
3294 psp->ipkeymgr_drv.feature_version = le32_to_cpu(desc->fw_version);
3295 psp->ipkeymgr_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3296 psp->ipkeymgr_drv.start_addr = ucode_start_addr;
3299 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3306 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3308 const struct psp_firmware_header_v1_0 *sos_hdr;
3309 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3310 uint8_t *ucode_array_start_addr;
3312 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3313 ucode_array_start_addr = (uint8_t *)sos_hdr +
3314 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3316 if (adev->gmc.xgmi.connected_to_cpu ||
3317 (amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(13, 0, 2))) {
3318 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3319 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3321 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3322 adev->psp.sys.start_addr = ucode_array_start_addr;
3324 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3325 adev->psp.sos.start_addr = ucode_array_start_addr +
3326 le32_to_cpu(sos_hdr->sos.offset_bytes);
3328 /* Load alternate PSP SOS FW */
3329 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3331 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3332 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3334 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3335 adev->psp.sys.start_addr = ucode_array_start_addr +
3336 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3338 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3339 adev->psp.sos.start_addr = ucode_array_start_addr +
3340 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3343 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3344 dev_warn(adev->dev, "PSP SOS FW not available");
3351 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3353 struct amdgpu_device *adev = psp->adev;
3354 char fw_name[PSP_FW_NAME_LEN];
3355 const struct psp_firmware_header_v1_0 *sos_hdr;
3356 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3357 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3358 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3359 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3361 uint8_t *ucode_array_start_addr;
3364 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3365 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3369 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3370 ucode_array_start_addr = (uint8_t *)sos_hdr +
3371 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3372 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3374 switch (sos_hdr->header.header_version_major) {
3376 err = psp_init_sos_base_fw(adev);
3380 if (sos_hdr->header.header_version_minor == 1) {
3381 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3382 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3383 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3384 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3385 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3386 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3387 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3389 if (sos_hdr->header.header_version_minor == 2) {
3390 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3391 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3392 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3393 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3395 if (sos_hdr->header.header_version_minor == 3) {
3396 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3397 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3398 adev->psp.toc.start_addr = ucode_array_start_addr +
3399 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3400 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3401 adev->psp.kdb.start_addr = ucode_array_start_addr +
3402 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3403 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3404 adev->psp.spl.start_addr = ucode_array_start_addr +
3405 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3406 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3407 adev->psp.rl.start_addr = ucode_array_start_addr +
3408 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3412 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3414 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3415 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3420 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3421 err = parse_sos_bin_descriptor(psp,
3422 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3430 "unsupported psp sos firmware\n");
3437 amdgpu_ucode_release(&adev->psp.sos_fw);
3442 static int parse_ta_bin_descriptor(struct psp_context *psp,
3443 const struct psp_fw_bin_desc *desc,
3444 const struct ta_firmware_header_v2_0 *ta_hdr)
3446 uint8_t *ucode_start_addr = NULL;
3448 if (!psp || !desc || !ta_hdr)
3451 ucode_start_addr = (uint8_t *)ta_hdr +
3452 le32_to_cpu(desc->offset_bytes) +
3453 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3455 switch (desc->fw_type) {
3456 case TA_FW_TYPE_PSP_ASD:
3457 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3458 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3459 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3460 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3462 case TA_FW_TYPE_PSP_XGMI:
3463 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3464 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3465 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3467 case TA_FW_TYPE_PSP_RAS:
3468 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3469 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3470 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3472 case TA_FW_TYPE_PSP_HDCP:
3473 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3474 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3475 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3477 case TA_FW_TYPE_PSP_DTM:
3478 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3479 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3480 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3482 case TA_FW_TYPE_PSP_RAP:
3483 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3484 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3485 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3487 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3488 psp->securedisplay_context.context.bin_desc.fw_version =
3489 le32_to_cpu(desc->fw_version);
3490 psp->securedisplay_context.context.bin_desc.size_bytes =
3491 le32_to_cpu(desc->size_bytes);
3492 psp->securedisplay_context.context.bin_desc.start_addr =
3496 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3503 static int parse_ta_v1_microcode(struct psp_context *psp)
3505 const struct ta_firmware_header_v1_0 *ta_hdr;
3506 struct amdgpu_device *adev = psp->adev;
3508 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3510 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3513 adev->psp.xgmi_context.context.bin_desc.fw_version =
3514 le32_to_cpu(ta_hdr->xgmi.fw_version);
3515 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3516 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3517 adev->psp.xgmi_context.context.bin_desc.start_addr =
3519 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3521 adev->psp.ras_context.context.bin_desc.fw_version =
3522 le32_to_cpu(ta_hdr->ras.fw_version);
3523 adev->psp.ras_context.context.bin_desc.size_bytes =
3524 le32_to_cpu(ta_hdr->ras.size_bytes);
3525 adev->psp.ras_context.context.bin_desc.start_addr =
3526 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3527 le32_to_cpu(ta_hdr->ras.offset_bytes);
3529 adev->psp.hdcp_context.context.bin_desc.fw_version =
3530 le32_to_cpu(ta_hdr->hdcp.fw_version);
3531 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3532 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3533 adev->psp.hdcp_context.context.bin_desc.start_addr =
3535 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3537 adev->psp.dtm_context.context.bin_desc.fw_version =
3538 le32_to_cpu(ta_hdr->dtm.fw_version);
3539 adev->psp.dtm_context.context.bin_desc.size_bytes =
3540 le32_to_cpu(ta_hdr->dtm.size_bytes);
3541 adev->psp.dtm_context.context.bin_desc.start_addr =
3542 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3543 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3545 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3546 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3547 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3548 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3549 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3550 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3551 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3553 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3558 static int parse_ta_v2_microcode(struct psp_context *psp)
3560 const struct ta_firmware_header_v2_0 *ta_hdr;
3561 struct amdgpu_device *adev = psp->adev;
3565 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3567 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3570 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3571 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3575 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3576 err = parse_ta_bin_descriptor(psp,
3577 &ta_hdr->ta_fw_bin[ta_index],
3586 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3588 const struct common_firmware_header *hdr;
3589 struct amdgpu_device *adev = psp->adev;
3590 char fw_name[PSP_FW_NAME_LEN];
3593 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3594 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3598 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3599 switch (le16_to_cpu(hdr->header_version_major)) {
3601 err = parse_ta_v1_microcode(psp);
3604 err = parse_ta_v2_microcode(psp);
3607 dev_err(adev->dev, "unsupported TA header version\n");
3612 amdgpu_ucode_release(&adev->psp.ta_fw);
3617 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3619 struct amdgpu_device *adev = psp->adev;
3620 char fw_name[PSP_FW_NAME_LEN];
3621 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3622 struct amdgpu_firmware_info *info = NULL;
3625 if (!amdgpu_sriov_vf(adev)) {
3626 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3630 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3631 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3633 if (err == -ENODEV) {
3634 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3638 dev_err(adev->dev, "fail to initialize cap microcode\n");
3641 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3642 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3643 info->fw = adev->psp.cap_fw;
3644 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3645 adev->psp.cap_fw->data;
3646 adev->firmware.fw_size += ALIGN(
3647 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3648 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3649 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3650 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3655 amdgpu_ucode_release(&adev->psp.cap_fw);
3659 static int psp_set_clockgating_state(void *handle,
3660 enum amd_clockgating_state state)
3665 static int psp_set_powergating_state(void *handle,
3666 enum amd_powergating_state state)
3671 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3672 struct device_attribute *attr,
3675 struct drm_device *ddev = dev_get_drvdata(dev);
3676 struct amdgpu_device *adev = drm_to_adev(ddev);
3680 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3681 dev_info(adev->dev, "PSP block is not ready yet\n.");
3685 mutex_lock(&adev->psp.mutex);
3686 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3687 mutex_unlock(&adev->psp.mutex);
3690 dev_err(adev->dev, "Failed to read USBC PD FW, err = %d\n", ret);
3694 return sysfs_emit(buf, "%x\n", fw_ver);
3697 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3698 struct device_attribute *attr,
3702 struct drm_device *ddev = dev_get_drvdata(dev);
3703 struct amdgpu_device *adev = drm_to_adev(ddev);
3706 const struct firmware *usbc_pd_fw;
3707 struct amdgpu_bo *fw_buf_bo = NULL;
3708 uint64_t fw_pri_mc_addr;
3709 void *fw_pri_cpu_addr;
3711 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3712 dev_err(adev->dev, "PSP block is not ready yet.");
3716 if (!drm_dev_enter(ddev, &idx))
3719 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3720 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3724 /* LFB address which is aligned to 1MB boundary per PSP request */
3725 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3726 AMDGPU_GEM_DOMAIN_VRAM |
3727 AMDGPU_GEM_DOMAIN_GTT,
3728 &fw_buf_bo, &fw_pri_mc_addr,
3733 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3735 mutex_lock(&adev->psp.mutex);
3736 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3737 mutex_unlock(&adev->psp.mutex);
3739 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3742 release_firmware(usbc_pd_fw);
3745 dev_err(adev->dev, "Failed to load USBC PD FW, err = %d", ret);
3753 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3757 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3760 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3761 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3768 * Reading from this file will retrieve the USB-C PD firmware version. Writing to
3769 * this file will trigger the update process.
3771 static DEVICE_ATTR(usbc_pd_fw, 0644,
3772 psp_usbc_pd_fw_sysfs_read,
3773 psp_usbc_pd_fw_sysfs_write);
3775 int is_psp_fw_valid(struct psp_bin_desc bin)
3777 return bin.size_bytes;
3780 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3781 struct bin_attribute *bin_attr,
3782 char *buffer, loff_t pos, size_t count)
3784 struct device *dev = kobj_to_dev(kobj);
3785 struct drm_device *ddev = dev_get_drvdata(dev);
3786 struct amdgpu_device *adev = drm_to_adev(ddev);
3788 adev->psp.vbflash_done = false;
3790 /* Safeguard against memory drain */
3791 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3792 dev_err(adev->dev, "File size cannot exceed %u\n", AMD_VBIOS_FILE_MAX_SIZE_B);
3793 kvfree(adev->psp.vbflash_tmp_buf);
3794 adev->psp.vbflash_tmp_buf = NULL;
3795 adev->psp.vbflash_image_size = 0;
3799 /* TODO Just allocate max for now and optimize to realloc later if needed */
3800 if (!adev->psp.vbflash_tmp_buf) {
3801 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3802 if (!adev->psp.vbflash_tmp_buf)
3806 mutex_lock(&adev->psp.mutex);
3807 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3808 adev->psp.vbflash_image_size += count;
3809 mutex_unlock(&adev->psp.mutex);
3811 dev_dbg(adev->dev, "IFWI staged for update\n");
3816 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3817 struct bin_attribute *bin_attr, char *buffer,
3818 loff_t pos, size_t count)
3820 struct device *dev = kobj_to_dev(kobj);
3821 struct drm_device *ddev = dev_get_drvdata(dev);
3822 struct amdgpu_device *adev = drm_to_adev(ddev);
3823 struct amdgpu_bo *fw_buf_bo = NULL;
3824 uint64_t fw_pri_mc_addr;
3825 void *fw_pri_cpu_addr;
3828 if (adev->psp.vbflash_image_size == 0)
3831 dev_dbg(adev->dev, "PSP IFWI flash process initiated\n");
3833 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3834 AMDGPU_GPU_PAGE_SIZE,
3835 AMDGPU_GEM_DOMAIN_VRAM,
3842 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3844 mutex_lock(&adev->psp.mutex);
3845 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3846 mutex_unlock(&adev->psp.mutex);
3848 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3851 kvfree(adev->psp.vbflash_tmp_buf);
3852 adev->psp.vbflash_tmp_buf = NULL;
3853 adev->psp.vbflash_image_size = 0;
3856 dev_err(adev->dev, "Failed to load IFWI, err = %d\n", ret);
3860 dev_dbg(adev->dev, "PSP IFWI flash process done\n");
3866 * Writing to this file will stage an IFWI for update. Reading from this file
3867 * will trigger the update process.
3869 static struct bin_attribute psp_vbflash_bin_attr = {
3870 .attr = {.name = "psp_vbflash", .mode = 0660},
3872 .write = amdgpu_psp_vbflash_write,
3873 .read = amdgpu_psp_vbflash_read,
3877 * DOC: psp_vbflash_status
3878 * The status of the flash process.
3879 * 0: IFWI flash not complete.
3880 * 1: IFWI flash complete.
3882 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3883 struct device_attribute *attr,
3886 struct drm_device *ddev = dev_get_drvdata(dev);
3887 struct amdgpu_device *adev = drm_to_adev(ddev);
3888 uint32_t vbflash_status;
3890 vbflash_status = psp_vbflash_status(&adev->psp);
3891 if (!adev->psp.vbflash_done)
3893 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3896 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3898 static DEVICE_ATTR(psp_vbflash_status, 0440, amdgpu_psp_vbflash_status, NULL);
3900 static struct bin_attribute *bin_flash_attrs[] = {
3901 &psp_vbflash_bin_attr,
3905 static struct attribute *flash_attrs[] = {
3906 &dev_attr_psp_vbflash_status.attr,
3907 &dev_attr_usbc_pd_fw.attr,
3911 static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
3913 struct device *dev = kobj_to_dev(kobj);
3914 struct drm_device *ddev = dev_get_drvdata(dev);
3915 struct amdgpu_device *adev = drm_to_adev(ddev);
3917 if (attr == &dev_attr_usbc_pd_fw.attr)
3918 return adev->psp.sup_pd_fw_up ? 0660 : 0;
3920 return adev->psp.sup_ifwi_up ? 0440 : 0;
3923 static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
3924 struct bin_attribute *attr,
3927 struct device *dev = kobj_to_dev(kobj);
3928 struct drm_device *ddev = dev_get_drvdata(dev);
3929 struct amdgpu_device *adev = drm_to_adev(ddev);
3931 return adev->psp.sup_ifwi_up ? 0660 : 0;
3934 const struct attribute_group amdgpu_flash_attr_group = {
3935 .attrs = flash_attrs,
3936 .bin_attrs = bin_flash_attrs,
3937 .is_bin_visible = amdgpu_bin_flash_attr_is_visible,
3938 .is_visible = amdgpu_flash_attr_is_visible,
3941 const struct amd_ip_funcs psp_ip_funcs = {
3943 .early_init = psp_early_init,
3945 .sw_init = psp_sw_init,
3946 .sw_fini = psp_sw_fini,
3947 .hw_init = psp_hw_init,
3948 .hw_fini = psp_hw_fini,
3949 .suspend = psp_suspend,
3950 .resume = psp_resume,
3952 .check_soft_reset = NULL,
3953 .wait_for_idle = NULL,
3955 .set_clockgating_state = psp_set_clockgating_state,
3956 .set_powergating_state = psp_set_powergating_state,
3959 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3960 .type = AMD_IP_BLOCK_TYPE_PSP,
3964 .funcs = &psp_ip_funcs,
3967 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3968 .type = AMD_IP_BLOCK_TYPE_PSP,
3972 .funcs = &psp_ip_funcs,
3975 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3976 .type = AMD_IP_BLOCK_TYPE_PSP,
3980 .funcs = &psp_ip_funcs,
3983 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3984 .type = AMD_IP_BLOCK_TYPE_PSP,
3988 .funcs = &psp_ip_funcs,
3991 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
3992 .type = AMD_IP_BLOCK_TYPE_PSP,
3996 .funcs = &psp_ip_funcs,
3999 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
4000 .type = AMD_IP_BLOCK_TYPE_PSP,
4004 .funcs = &psp_ip_funcs,
4007 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
4008 .type = AMD_IP_BLOCK_TYPE_PSP,
4012 .funcs = &psp_ip_funcs,
4015 const struct amdgpu_ip_block_version psp_v14_0_ip_block = {
4016 .type = AMD_IP_BLOCK_TYPE_PSP,
4020 .funcs = &psp_ip_funcs,