2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/firmware.h>
27 #include <drm/drm_drv.h>
30 #include "amdgpu_psp.h"
31 #include "amdgpu_ucode.h"
32 #include "amdgpu_xgmi.h"
33 #include "soc15_common.h"
35 #include "psp_v10_0.h"
36 #include "psp_v11_0.h"
37 #include "psp_v11_0_8.h"
38 #include "psp_v12_0.h"
39 #include "psp_v13_0.h"
40 #include "psp_v13_0_4.h"
42 #include "amdgpu_ras.h"
43 #include "amdgpu_securedisplay.h"
44 #include "amdgpu_atomfirmware.h"
46 #define AMD_VBIOS_FILE_MAX_SIZE_B (1024*1024*3)
48 static int psp_sysfs_init(struct amdgpu_device *adev);
49 static void psp_sysfs_fini(struct amdgpu_device *adev);
51 static int psp_load_smu_fw(struct psp_context *psp);
52 static int psp_rap_terminate(struct psp_context *psp);
53 static int psp_securedisplay_terminate(struct psp_context *psp);
55 static int psp_ring_init(struct psp_context *psp,
56 enum psp_ring_type ring_type)
59 struct psp_ring *ring;
60 struct amdgpu_device *adev = psp->adev;
64 ring->ring_type = ring_type;
66 /* allocate 4k Page of Local Frame Buffer memory for ring */
67 ring->ring_size = 0x1000;
68 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
69 AMDGPU_GEM_DOMAIN_VRAM |
70 AMDGPU_GEM_DOMAIN_GTT,
72 &ring->ring_mem_mc_addr,
73 (void **)&ring->ring_mem);
83 * Due to DF Cstate management centralized to PMFW, the firmware
84 * loading sequence will be updated as below:
90 * - Load other non-psp fw
92 * - Load XGMI/RAS/HDCP/DTM TA if any
94 * This new sequence is required for
95 * - Arcturus and onwards
97 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
99 struct amdgpu_device *adev = psp->adev;
101 if (amdgpu_sriov_vf(adev)) {
102 psp->pmfw_centralized_cstate_management = false;
106 switch (adev->ip_versions[MP0_HWIP][0]) {
107 case IP_VERSION(11, 0, 0):
108 case IP_VERSION(11, 0, 4):
109 case IP_VERSION(11, 0, 5):
110 case IP_VERSION(11, 0, 7):
111 case IP_VERSION(11, 0, 9):
112 case IP_VERSION(11, 0, 11):
113 case IP_VERSION(11, 0, 12):
114 case IP_VERSION(11, 0, 13):
115 case IP_VERSION(13, 0, 0):
116 case IP_VERSION(13, 0, 2):
117 case IP_VERSION(13, 0, 7):
118 psp->pmfw_centralized_cstate_management = true;
121 psp->pmfw_centralized_cstate_management = false;
126 static int psp_init_sriov_microcode(struct psp_context *psp)
128 struct amdgpu_device *adev = psp->adev;
129 char ucode_prefix[30];
132 amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
134 switch (adev->ip_versions[MP0_HWIP][0]) {
135 case IP_VERSION(9, 0, 0):
136 case IP_VERSION(11, 0, 7):
137 case IP_VERSION(11, 0, 9):
138 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
139 ret = psp_init_cap_microcode(psp, ucode_prefix);
141 case IP_VERSION(13, 0, 2):
142 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
143 ret = psp_init_cap_microcode(psp, ucode_prefix);
144 ret &= psp_init_ta_microcode(psp, ucode_prefix);
146 case IP_VERSION(13, 0, 0):
147 adev->virt.autoload_ucode_id = 0;
149 case IP_VERSION(13, 0, 10):
150 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
158 static int psp_early_init(void *handle)
160 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
161 struct psp_context *psp = &adev->psp;
163 switch (adev->ip_versions[MP0_HWIP][0]) {
164 case IP_VERSION(9, 0, 0):
165 psp_v3_1_set_psp_funcs(psp);
166 psp->autoload_supported = false;
168 case IP_VERSION(10, 0, 0):
169 case IP_VERSION(10, 0, 1):
170 psp_v10_0_set_psp_funcs(psp);
171 psp->autoload_supported = false;
173 case IP_VERSION(11, 0, 2):
174 case IP_VERSION(11, 0, 4):
175 psp_v11_0_set_psp_funcs(psp);
176 psp->autoload_supported = false;
178 case IP_VERSION(11, 0, 0):
179 case IP_VERSION(11, 0, 5):
180 case IP_VERSION(11, 0, 9):
181 case IP_VERSION(11, 0, 7):
182 case IP_VERSION(11, 0, 11):
183 case IP_VERSION(11, 5, 0):
184 case IP_VERSION(11, 0, 12):
185 case IP_VERSION(11, 0, 13):
186 psp_v11_0_set_psp_funcs(psp);
187 psp->autoload_supported = true;
189 case IP_VERSION(11, 0, 3):
190 case IP_VERSION(12, 0, 1):
191 psp_v12_0_set_psp_funcs(psp);
193 case IP_VERSION(13, 0, 2):
194 case IP_VERSION(13, 0, 6):
195 psp_v13_0_set_psp_funcs(psp);
197 case IP_VERSION(13, 0, 1):
198 case IP_VERSION(13, 0, 3):
199 case IP_VERSION(13, 0, 5):
200 case IP_VERSION(13, 0, 8):
201 case IP_VERSION(13, 0, 10):
202 case IP_VERSION(13, 0, 11):
203 psp_v13_0_set_psp_funcs(psp);
204 psp->autoload_supported = true;
206 case IP_VERSION(11, 0, 8):
207 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
208 psp_v11_0_8_set_psp_funcs(psp);
209 psp->autoload_supported = false;
212 case IP_VERSION(13, 0, 0):
213 case IP_VERSION(13, 0, 7):
214 psp_v13_0_set_psp_funcs(psp);
215 psp->autoload_supported = true;
217 case IP_VERSION(13, 0, 4):
218 psp_v13_0_4_set_psp_funcs(psp);
219 psp->autoload_supported = true;
227 psp_check_pmfw_centralized_cstate_management(psp);
229 if (amdgpu_sriov_vf(adev))
230 return psp_init_sriov_microcode(psp);
232 return psp_init_microcode(psp);
235 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
237 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
238 &mem_ctx->shared_buf);
239 mem_ctx->shared_bo = NULL;
242 static void psp_free_shared_bufs(struct psp_context *psp)
247 /* free TMR memory buffer */
248 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
249 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
252 /* free xgmi shared memory */
253 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
255 /* free ras shared memory */
256 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
258 /* free hdcp shared memory */
259 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
261 /* free dtm shared memory */
262 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
264 /* free rap shared memory */
265 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
267 /* free securedisplay shared memory */
268 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
273 static void psp_memory_training_fini(struct psp_context *psp)
275 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
277 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
278 kfree(ctx->sys_cache);
279 ctx->sys_cache = NULL;
282 static int psp_memory_training_init(struct psp_context *psp)
285 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
287 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
288 DRM_DEBUG("memory training is not supported!\n");
292 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
293 if (ctx->sys_cache == NULL) {
294 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
299 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
300 ctx->train_data_size,
301 ctx->p2c_train_data_offset,
302 ctx->c2p_train_data_offset);
303 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
307 psp_memory_training_fini(psp);
312 * Helper funciton to query psp runtime database entry
314 * @adev: amdgpu_device pointer
315 * @entry_type: the type of psp runtime database entry
316 * @db_entry: runtime database entry pointer
318 * Return false if runtime database doesn't exit or entry is invalid
319 * or true if the specific database entry is found, and copy to @db_entry
321 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
322 enum psp_runtime_entry_type entry_type,
325 uint64_t db_header_pos, db_dir_pos;
326 struct psp_runtime_data_header db_header = {0};
327 struct psp_runtime_data_directory db_dir = {0};
331 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
332 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
334 /* read runtime db header from vram */
335 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
336 sizeof(struct psp_runtime_data_header), false);
338 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
339 /* runtime db doesn't exist, exit */
340 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
344 /* read runtime database entry from vram */
345 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
346 sizeof(struct psp_runtime_data_directory), false);
348 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
349 /* invalid db entry count, exit */
350 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
354 /* look up for requested entry type */
355 for (i = 0; i < db_dir.entry_count && !ret; i++) {
356 if (db_dir.entry_list[i].entry_type == entry_type) {
357 switch (entry_type) {
358 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
359 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
360 /* invalid db entry size */
361 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
364 /* read runtime database entry */
365 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
366 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
369 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
370 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
371 /* invalid db entry size */
372 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
375 /* read runtime database entry */
376 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
377 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
390 static int psp_sw_init(void *handle)
392 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
393 struct psp_context *psp = &adev->psp;
395 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
396 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
397 struct psp_runtime_scpm_entry scpm_entry;
399 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
401 DRM_ERROR("Failed to allocate memory to command buffer!\n");
405 adev->psp.xgmi_context.supports_extended_data =
406 !adev->gmc.xgmi.connected_to_cpu &&
407 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
409 memset(&scpm_entry, 0, sizeof(scpm_entry));
410 if ((psp_get_runtime_db_entry(adev,
411 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
413 (SCPM_DISABLE != scpm_entry.scpm_status)) {
414 adev->scpm_enabled = true;
415 adev->scpm_status = scpm_entry.scpm_status;
417 adev->scpm_enabled = false;
418 adev->scpm_status = SCPM_DISABLE;
421 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
423 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
424 if (psp_get_runtime_db_entry(adev,
425 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
427 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
428 if ((psp->boot_cfg_bitmask) &
429 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
430 /* If psp runtime database exists, then
431 * only enable two stage memory training
432 * when TWO_STAGE_DRAM_TRAINING bit is set
433 * in runtime database */
434 mem_training_ctx->enable_mem_training = true;
438 /* If psp runtime database doesn't exist or
439 * is invalid, force enable two stage memory
441 mem_training_ctx->enable_mem_training = true;
444 if (mem_training_ctx->enable_mem_training) {
445 ret = psp_memory_training_init(psp);
447 DRM_ERROR("Failed to initialize memory training!\n");
451 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
453 DRM_ERROR("Failed to process memory training!\n");
458 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
459 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7)) {
460 ret= psp_sysfs_init(adev);
466 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
467 amdgpu_sriov_vf(adev) ?
468 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
470 &psp->fw_pri_mc_addr,
475 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
476 AMDGPU_GEM_DOMAIN_VRAM,
478 &psp->fence_buf_mc_addr,
483 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
484 AMDGPU_GEM_DOMAIN_VRAM,
485 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
486 (void **)&psp->cmd_buf_mem);
493 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
494 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
496 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
497 &psp->fence_buf_mc_addr, &psp->fence_buf);
501 static int psp_sw_fini(void *handle)
503 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
504 struct psp_context *psp = &adev->psp;
505 struct psp_gfx_cmd_resp *cmd = psp->cmd;
507 psp_memory_training_fini(psp);
509 amdgpu_ucode_release(&psp->sos_fw);
510 amdgpu_ucode_release(&psp->asd_fw);
511 amdgpu_ucode_release(&psp->ta_fw);
512 amdgpu_ucode_release(&psp->cap_fw);
513 amdgpu_ucode_release(&psp->toc_fw);
515 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 0) ||
516 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7))
517 psp_sysfs_fini(adev);
522 if (psp->km_ring.ring_mem)
523 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
524 &psp->km_ring.ring_mem_mc_addr,
525 (void **)&psp->km_ring.ring_mem);
527 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
528 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
529 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
530 &psp->fence_buf_mc_addr, &psp->fence_buf);
531 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
532 (void **)&psp->cmd_buf_mem);
537 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
538 uint32_t reg_val, uint32_t mask, bool check_changed)
542 struct amdgpu_device *adev = psp->adev;
544 if (psp->adev->no_hw_access)
547 for (i = 0; i < adev->usec_timeout; i++) {
548 val = RREG32(reg_index);
553 if ((val & mask) == reg_val)
562 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
565 case GFX_CMD_ID_LOAD_TA:
567 case GFX_CMD_ID_UNLOAD_TA:
569 case GFX_CMD_ID_INVOKE_CMD:
571 case GFX_CMD_ID_LOAD_ASD:
573 case GFX_CMD_ID_SETUP_TMR:
575 case GFX_CMD_ID_LOAD_IP_FW:
577 case GFX_CMD_ID_DESTROY_TMR:
578 return "DESTROY_TMR";
579 case GFX_CMD_ID_SAVE_RESTORE:
580 return "SAVE_RESTORE_IP_FW";
581 case GFX_CMD_ID_SETUP_VMR:
583 case GFX_CMD_ID_DESTROY_VMR:
584 return "DESTROY_VMR";
585 case GFX_CMD_ID_PROG_REG:
587 case GFX_CMD_ID_GET_FW_ATTESTATION:
588 return "GET_FW_ATTESTATION";
589 case GFX_CMD_ID_LOAD_TOC:
590 return "ID_LOAD_TOC";
591 case GFX_CMD_ID_AUTOLOAD_RLC:
592 return "AUTOLOAD_RLC";
593 case GFX_CMD_ID_BOOT_CFG:
596 return "UNKNOWN CMD";
601 psp_cmd_submit_buf(struct psp_context *psp,
602 struct amdgpu_firmware_info *ucode,
603 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
608 bool ras_intr = false;
609 bool skip_unsupport = false;
611 if (psp->adev->no_hw_access)
614 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
616 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
618 index = atomic_inc_return(&psp->fence_value);
619 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
621 atomic_dec(&psp->fence_value);
625 amdgpu_device_invalidate_hdp(psp->adev, NULL);
626 while (*((unsigned int *)psp->fence_buf) != index) {
630 * Shouldn't wait for timeout when err_event_athub occurs,
631 * because gpu reset thread triggered and lock resource should
632 * be released for psp resume sequence.
634 ras_intr = amdgpu_ras_intr_triggered();
637 usleep_range(10, 100);
638 amdgpu_device_invalidate_hdp(psp->adev, NULL);
641 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
642 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
643 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
645 memcpy((void*)&cmd->resp, (void*)&psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
647 /* In some cases, psp response status is not 0 even there is no
648 * problem while the command is submitted. Some version of PSP FW
649 * doesn't write 0 to that field.
650 * So here we would like to only print a warning instead of an error
651 * during psp initialization to avoid breaking hw_init and it doesn't
654 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
656 DRM_WARN("failed to load ucode %s(0x%X) ",
657 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
658 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
659 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
660 psp->cmd_buf_mem->resp.status);
661 /* If any firmware (including CAP) load fails under SRIOV, it should
662 * return failure to stop the VF from initializing.
663 * Also return failure in case of timeout
665 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
672 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
673 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
680 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
682 struct psp_gfx_cmd_resp *cmd = psp->cmd;
684 mutex_lock(&psp->mutex);
686 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
691 static void release_psp_cmd_buf(struct psp_context *psp)
693 mutex_unlock(&psp->mutex);
696 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
697 struct psp_gfx_cmd_resp *cmd,
698 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
700 struct amdgpu_device *adev = psp->adev;
701 uint32_t size = amdgpu_bo_size(tmr_bo);
702 uint64_t tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
704 if (amdgpu_sriov_vf(psp->adev))
705 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
707 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
708 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
709 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
710 cmd->cmd.cmd_setup_tmr.buf_size = size;
711 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
712 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
713 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
716 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
717 uint64_t pri_buf_mc, uint32_t size)
719 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
720 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
721 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
722 cmd->cmd.cmd_load_toc.toc_size = size;
725 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
726 static int psp_load_toc(struct psp_context *psp,
730 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
732 /* Copy toc to psp firmware private buffer */
733 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
735 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
737 ret = psp_cmd_submit_buf(psp, NULL, cmd,
738 psp->fence_buf_mc_addr);
740 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
742 release_psp_cmd_buf(psp);
747 /* Set up Trusted Memory Region */
748 static int psp_tmr_init(struct psp_context *psp)
756 * According to HW engineer, they prefer the TMR address be "naturally
757 * aligned" , e.g. the start address be an integer divide of TMR size.
759 * Note: this memory need be reserved till the driver
762 tmr_size = PSP_TMR_SIZE(psp->adev);
764 /* For ASICs support RLC autoload, psp will parse the toc
765 * and calculate the total size of TMR needed */
766 if (!amdgpu_sriov_vf(psp->adev) &&
767 psp->toc.start_addr &&
768 psp->toc.size_bytes &&
770 ret = psp_load_toc(psp, &tmr_size);
772 DRM_ERROR("Failed to load toc\n");
778 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
779 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
781 AMDGPU_HAS_VRAM(psp->adev) ?
782 AMDGPU_GEM_DOMAIN_VRAM :
783 AMDGPU_GEM_DOMAIN_GTT,
784 &psp->tmr_bo, &psp->tmr_mc_addr,
791 static bool psp_skip_tmr(struct psp_context *psp)
793 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
794 case IP_VERSION(11, 0, 9):
795 case IP_VERSION(11, 0, 7):
796 case IP_VERSION(13, 0, 2):
797 case IP_VERSION(13, 0, 10):
804 static int psp_tmr_load(struct psp_context *psp)
807 struct psp_gfx_cmd_resp *cmd;
809 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
810 * Already set up by host driver.
812 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
815 cmd = acquire_psp_cmd_buf(psp);
817 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
818 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
819 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
821 ret = psp_cmd_submit_buf(psp, NULL, cmd,
822 psp->fence_buf_mc_addr);
824 release_psp_cmd_buf(psp);
829 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
830 struct psp_gfx_cmd_resp *cmd)
832 if (amdgpu_sriov_vf(psp->adev))
833 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
835 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
838 static int psp_tmr_unload(struct psp_context *psp)
841 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
843 psp_prep_tmr_unload_cmd_buf(psp, cmd);
844 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
846 ret = psp_cmd_submit_buf(psp, NULL, cmd,
847 psp->fence_buf_mc_addr);
849 release_psp_cmd_buf(psp);
854 static int psp_tmr_terminate(struct psp_context *psp)
856 return psp_tmr_unload(psp);
859 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
860 uint64_t *output_ptr)
863 struct psp_gfx_cmd_resp *cmd;
868 if (amdgpu_sriov_vf(psp->adev))
871 cmd = acquire_psp_cmd_buf(psp);
873 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
875 ret = psp_cmd_submit_buf(psp, NULL, cmd,
876 psp->fence_buf_mc_addr);
879 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
880 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
883 release_psp_cmd_buf(psp);
888 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
890 struct psp_context *psp = &adev->psp;
891 struct psp_gfx_cmd_resp *cmd;
894 if (amdgpu_sriov_vf(adev))
897 cmd = acquire_psp_cmd_buf(psp);
899 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
900 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
902 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
905 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
908 release_psp_cmd_buf(psp);
913 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
916 struct psp_context *psp = &adev->psp;
917 struct psp_gfx_cmd_resp *cmd;
919 if (amdgpu_sriov_vf(adev))
922 cmd = acquire_psp_cmd_buf(psp);
924 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
925 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
926 cmd->cmd.boot_cfg.boot_config = boot_cfg;
927 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
929 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
931 release_psp_cmd_buf(psp);
936 static int psp_rl_load(struct amdgpu_device *adev)
939 struct psp_context *psp = &adev->psp;
940 struct psp_gfx_cmd_resp *cmd;
942 if (!is_psp_fw_valid(psp->rl))
945 cmd = acquire_psp_cmd_buf(psp);
947 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
948 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
950 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
951 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
952 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
953 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
954 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
956 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
958 release_psp_cmd_buf(psp);
963 static int psp_asd_initialize(struct psp_context *psp)
967 /* If PSP version doesn't match ASD version, asd loading will be failed.
968 * add workaround to bypass it for sriov now.
969 * TODO: add version check to make it common
971 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
974 psp->asd_context.mem_context.shared_mc_addr = 0;
975 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
976 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
978 ret = psp_ta_load(psp, &psp->asd_context);
980 psp->asd_context.initialized = true;
985 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
988 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
989 cmd->cmd.cmd_unload_ta.session_id = session_id;
992 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
995 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
997 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
999 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1001 context->resp_status = cmd->resp.status;
1003 release_psp_cmd_buf(psp);
1008 static int psp_asd_terminate(struct psp_context *psp)
1012 if (amdgpu_sriov_vf(psp->adev))
1015 if (!psp->asd_context.initialized)
1018 ret = psp_ta_unload(psp, &psp->asd_context);
1020 psp->asd_context.initialized = false;
1025 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1026 uint32_t id, uint32_t value)
1028 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1029 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1030 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1033 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1036 struct psp_gfx_cmd_resp *cmd;
1039 if (reg >= PSP_REG_LAST)
1042 cmd = acquire_psp_cmd_buf(psp);
1044 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1045 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1047 DRM_ERROR("PSP failed to program reg id %d", reg);
1049 release_psp_cmd_buf(psp);
1054 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1056 struct ta_context *context)
1058 cmd->cmd_id = context->ta_load_type;
1059 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1060 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1061 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1063 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1064 lower_32_bits(context->mem_context.shared_mc_addr);
1065 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1066 upper_32_bits(context->mem_context.shared_mc_addr);
1067 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1070 int psp_ta_init_shared_buf(struct psp_context *psp,
1071 struct ta_mem_context *mem_ctx)
1074 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1075 * physical) for ta to host memory
1077 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1078 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1079 AMDGPU_GEM_DOMAIN_GTT,
1080 &mem_ctx->shared_bo,
1081 &mem_ctx->shared_mc_addr,
1082 &mem_ctx->shared_buf);
1085 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1087 uint32_t session_id)
1089 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1090 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1091 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1094 int psp_ta_invoke(struct psp_context *psp,
1096 struct ta_context *context)
1099 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1101 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1103 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1104 psp->fence_buf_mc_addr);
1106 context->resp_status = cmd->resp.status;
1108 release_psp_cmd_buf(psp);
1113 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1116 struct psp_gfx_cmd_resp *cmd;
1118 cmd = acquire_psp_cmd_buf(psp);
1120 psp_copy_fw(psp, context->bin_desc.start_addr,
1121 context->bin_desc.size_bytes);
1123 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1125 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1126 psp->fence_buf_mc_addr);
1128 context->resp_status = cmd->resp.status;
1131 context->session_id = cmd->resp.session_id;
1134 release_psp_cmd_buf(psp);
1139 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1141 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1144 int psp_xgmi_terminate(struct psp_context *psp)
1147 struct amdgpu_device *adev = psp->adev;
1149 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1150 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1151 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1152 adev->gmc.xgmi.connected_to_cpu))
1155 if (!psp->xgmi_context.context.initialized)
1158 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1160 psp->xgmi_context.context.initialized = false;
1165 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1167 struct ta_xgmi_shared_memory *xgmi_cmd;
1171 !psp->xgmi_context.context.bin_desc.size_bytes ||
1172 !psp->xgmi_context.context.bin_desc.start_addr)
1178 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1179 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1181 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1182 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1188 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1190 psp->xgmi_context.context.initialized = true;
1195 /* Initialize XGMI session */
1196 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1197 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1198 xgmi_cmd->flag_extend_link_record = set_extended_data;
1199 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1201 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1206 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1208 struct ta_xgmi_shared_memory *xgmi_cmd;
1211 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1212 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1214 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1216 /* Invoke xgmi ta to get hive id */
1217 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1221 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1226 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1228 struct ta_xgmi_shared_memory *xgmi_cmd;
1231 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1232 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1234 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1236 /* Invoke xgmi ta to get the node id */
1237 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1241 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1246 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1248 return psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1249 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b;
1253 * Chips that support extended topology information require the driver to
1254 * reflect topology information in the opposite direction. This is
1255 * because the TA has already exceeded its link record limit and if the
1256 * TA holds bi-directional information, the driver would have to do
1257 * multiple fetches instead of just two.
1259 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1260 struct psp_xgmi_node_info node_info)
1262 struct amdgpu_device *mirror_adev;
1263 struct amdgpu_hive_info *hive;
1264 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1265 uint64_t dst_node_id = node_info.node_id;
1266 uint8_t dst_num_hops = node_info.num_hops;
1267 uint8_t dst_num_links = node_info.num_links;
1269 hive = amdgpu_get_xgmi_hive(psp->adev);
1270 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1271 struct psp_xgmi_topology_info *mirror_top_info;
1274 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1277 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1278 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1279 if (mirror_top_info->nodes[j].node_id != src_node_id)
1282 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1284 * prevent 0 num_links value re-reflection since reflection
1285 * criteria is based on num_hops (direct or indirect).
1289 mirror_top_info->nodes[j].num_links = dst_num_links;
1297 amdgpu_put_xgmi_hive(hive);
1300 int psp_xgmi_get_topology_info(struct psp_context *psp,
1302 struct psp_xgmi_topology_info *topology,
1303 bool get_extended_data)
1305 struct ta_xgmi_shared_memory *xgmi_cmd;
1306 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1307 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1311 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1314 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1315 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1316 xgmi_cmd->flag_extend_link_record = get_extended_data;
1318 /* Fill in the shared memory with topology information as input */
1319 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1320 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1321 topology_info_input->num_nodes = number_devices;
1323 for (i = 0; i < topology_info_input->num_nodes; i++) {
1324 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1325 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1326 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1327 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1330 /* Invoke xgmi ta to get the topology information */
1331 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1335 /* Read the output topology information from the shared memory */
1336 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1337 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1338 for (i = 0; i < topology->num_nodes; i++) {
1339 /* extended data will either be 0 or equal to non-extended data */
1340 if (topology_info_output->nodes[i].num_hops)
1341 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1343 /* non-extended data gets everything here so no need to update */
1344 if (!get_extended_data) {
1345 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1346 topology->nodes[i].is_sharing_enabled =
1347 topology_info_output->nodes[i].is_sharing_enabled;
1348 topology->nodes[i].sdma_engine =
1349 topology_info_output->nodes[i].sdma_engine;
1354 /* Invoke xgmi ta again to get the link information */
1355 if (psp_xgmi_peer_link_info_supported(psp)) {
1356 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1358 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1360 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1365 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1366 for (i = 0; i < topology->num_nodes; i++) {
1367 /* accumulate num_links on extended data */
1368 topology->nodes[i].num_links = get_extended_data ?
1369 topology->nodes[i].num_links +
1370 link_info_output->nodes[i].num_links :
1371 link_info_output->nodes[i].num_links;
1373 /* reflect the topology information for bi-directionality */
1374 if (psp->xgmi_context.supports_extended_data &&
1375 get_extended_data && topology->nodes[i].num_hops)
1376 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1383 int psp_xgmi_set_topology_info(struct psp_context *psp,
1385 struct psp_xgmi_topology_info *topology)
1387 struct ta_xgmi_shared_memory *xgmi_cmd;
1388 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1391 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1394 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1395 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1397 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1398 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1399 topology_info_input->num_nodes = number_devices;
1401 for (i = 0; i < topology_info_input->num_nodes; i++) {
1402 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1403 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1404 topology_info_input->nodes[i].is_sharing_enabled = 1;
1405 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1408 /* Invoke xgmi ta to set topology information */
1409 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1413 static void psp_ras_ta_check_status(struct psp_context *psp)
1415 struct ta_ras_shared_memory *ras_cmd =
1416 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1418 switch (ras_cmd->ras_status) {
1419 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1420 dev_warn(psp->adev->dev,
1421 "RAS WARNING: cmd failed due to unsupported ip\n");
1423 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1424 dev_warn(psp->adev->dev,
1425 "RAS WARNING: cmd failed due to unsupported error injection\n");
1427 case TA_RAS_STATUS__SUCCESS:
1429 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1430 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1431 dev_warn(psp->adev->dev,
1432 "RAS WARNING: Inject error to critical region is not allowed\n");
1435 dev_warn(psp->adev->dev,
1436 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1441 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1443 struct ta_ras_shared_memory *ras_cmd;
1446 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1449 * TODO: bypass the loading in sriov for now
1451 if (amdgpu_sriov_vf(psp->adev))
1454 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1456 if (amdgpu_ras_intr_triggered())
1459 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER)
1461 DRM_WARN("RAS: Unsupported Interface");
1466 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1467 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1469 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1471 else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1472 dev_warn(psp->adev->dev,
1473 "RAS internal register access blocked\n");
1475 psp_ras_ta_check_status(psp);
1481 int psp_ras_enable_features(struct psp_context *psp,
1482 union ta_ras_cmd_input *info, bool enable)
1484 struct ta_ras_shared_memory *ras_cmd;
1487 if (!psp->ras_context.context.initialized)
1490 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1491 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1494 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1496 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1498 ras_cmd->ras_in_message = *info;
1500 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1507 int psp_ras_terminate(struct psp_context *psp)
1512 * TODO: bypass the terminate in sriov for now
1514 if (amdgpu_sriov_vf(psp->adev))
1517 if (!psp->ras_context.context.initialized)
1520 ret = psp_ta_unload(psp, &psp->ras_context.context);
1522 psp->ras_context.context.initialized = false;
1527 int psp_ras_initialize(struct psp_context *psp)
1530 uint32_t boot_cfg = 0xFF;
1531 struct amdgpu_device *adev = psp->adev;
1532 struct ta_ras_shared_memory *ras_cmd;
1535 * TODO: bypass the initialize in sriov for now
1537 if (amdgpu_sriov_vf(adev))
1540 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1541 !adev->psp.ras_context.context.bin_desc.start_addr) {
1542 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1546 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1547 /* query GECC enablement status from boot config
1548 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1550 ret = psp_boot_config_get(adev, &boot_cfg);
1552 dev_warn(adev->dev, "PSP get boot config failed\n");
1554 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1556 dev_info(adev->dev, "GECC is disabled\n");
1558 /* disable GECC in next boot cycle if ras is
1559 * disabled by module parameter amdgpu_ras_enable
1560 * and/or amdgpu_ras_mask, or boot_config_get call
1563 ret = psp_boot_config_set(adev, 0);
1565 dev_warn(adev->dev, "PSP set boot config failed\n");
1567 dev_warn(adev->dev, "GECC will be disabled in next boot cycle "
1568 "if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1571 if (1 == boot_cfg) {
1572 dev_info(adev->dev, "GECC is enabled\n");
1574 /* enable GECC in next boot cycle if it is disabled
1575 * in boot config, or force enable GECC if failed to
1576 * get boot configuration
1578 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1580 dev_warn(adev->dev, "PSP set boot config failed\n");
1582 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1587 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1588 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1590 if (!psp->ras_context.context.mem_context.shared_buf) {
1591 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1596 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1597 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1599 if (amdgpu_ras_is_poison_mode_supported(adev))
1600 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1601 if (!adev->gmc.xgmi.connected_to_cpu)
1602 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1604 ret = psp_ta_load(psp, &psp->ras_context.context);
1606 if (!ret && !ras_cmd->ras_status)
1607 psp->ras_context.context.initialized = true;
1609 if (ras_cmd->ras_status)
1610 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1612 /* fail to load RAS TA */
1613 psp->ras_context.context.initialized = false;
1619 int psp_ras_trigger_error(struct psp_context *psp,
1620 struct ta_ras_trigger_error_input *info)
1622 struct ta_ras_shared_memory *ras_cmd;
1625 if (!psp->ras_context.context.initialized)
1628 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1629 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1631 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1632 ras_cmd->ras_in_message.trigger_error = *info;
1634 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1638 /* If err_event_athub occurs error inject was successful, however
1639 return status from TA is no long reliable */
1640 if (amdgpu_ras_intr_triggered())
1643 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1645 else if (ras_cmd->ras_status)
1653 static int psp_hdcp_initialize(struct psp_context *psp)
1658 * TODO: bypass the initialize in sriov for now
1660 if (amdgpu_sriov_vf(psp->adev))
1663 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1664 !psp->hdcp_context.context.bin_desc.start_addr) {
1665 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1669 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1670 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1672 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1673 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1678 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1680 psp->hdcp_context.context.initialized = true;
1681 mutex_init(&psp->hdcp_context.mutex);
1687 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1690 * TODO: bypass the loading in sriov for now
1692 if (amdgpu_sriov_vf(psp->adev))
1695 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1698 static int psp_hdcp_terminate(struct psp_context *psp)
1703 * TODO: bypass the terminate in sriov for now
1705 if (amdgpu_sriov_vf(psp->adev))
1708 if (!psp->hdcp_context.context.initialized)
1711 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1713 psp->hdcp_context.context.initialized = false;
1720 static int psp_dtm_initialize(struct psp_context *psp)
1725 * TODO: bypass the initialize in sriov for now
1727 if (amdgpu_sriov_vf(psp->adev))
1730 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1731 !psp->dtm_context.context.bin_desc.start_addr) {
1732 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1736 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1737 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1739 if (!psp->dtm_context.context.mem_context.shared_buf) {
1740 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1745 ret = psp_ta_load(psp, &psp->dtm_context.context);
1747 psp->dtm_context.context.initialized = true;
1748 mutex_init(&psp->dtm_context.mutex);
1754 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1757 * TODO: bypass the loading in sriov for now
1759 if (amdgpu_sriov_vf(psp->adev))
1762 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1765 static int psp_dtm_terminate(struct psp_context *psp)
1770 * TODO: bypass the terminate in sriov for now
1772 if (amdgpu_sriov_vf(psp->adev))
1775 if (!psp->dtm_context.context.initialized)
1778 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1780 psp->dtm_context.context.initialized = false;
1787 static int psp_rap_initialize(struct psp_context *psp)
1790 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1793 * TODO: bypass the initialize in sriov for now
1795 if (amdgpu_sriov_vf(psp->adev))
1798 if (!psp->rap_context.context.bin_desc.size_bytes ||
1799 !psp->rap_context.context.bin_desc.start_addr) {
1800 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1804 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1805 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1807 if (!psp->rap_context.context.mem_context.shared_buf) {
1808 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1813 ret = psp_ta_load(psp, &psp->rap_context.context);
1815 psp->rap_context.context.initialized = true;
1816 mutex_init(&psp->rap_context.mutex);
1820 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1821 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1822 psp_rap_terminate(psp);
1823 /* free rap shared memory */
1824 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1826 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1835 static int psp_rap_terminate(struct psp_context *psp)
1839 if (!psp->rap_context.context.initialized)
1842 ret = psp_ta_unload(psp, &psp->rap_context.context);
1844 psp->rap_context.context.initialized = false;
1849 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1851 struct ta_rap_shared_memory *rap_cmd;
1854 if (!psp->rap_context.context.initialized)
1857 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1858 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1861 mutex_lock(&psp->rap_context.mutex);
1863 rap_cmd = (struct ta_rap_shared_memory *)
1864 psp->rap_context.context.mem_context.shared_buf;
1865 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1867 rap_cmd->cmd_id = ta_cmd_id;
1868 rap_cmd->validation_method_id = METHOD_A;
1870 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1875 *status = rap_cmd->rap_status;
1878 mutex_unlock(&psp->rap_context.mutex);
1884 /* securedisplay start */
1885 static int psp_securedisplay_initialize(struct psp_context *psp)
1888 struct ta_securedisplay_cmd *securedisplay_cmd;
1891 * TODO: bypass the initialize in sriov for now
1893 if (amdgpu_sriov_vf(psp->adev))
1896 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1897 !psp->securedisplay_context.context.bin_desc.start_addr) {
1898 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1902 psp->securedisplay_context.context.mem_context.shared_mem_size =
1903 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1904 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1906 if (!psp->securedisplay_context.context.initialized) {
1907 ret = psp_ta_init_shared_buf(psp,
1908 &psp->securedisplay_context.context.mem_context);
1913 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
1915 psp->securedisplay_context.context.initialized = true;
1916 mutex_init(&psp->securedisplay_context.mutex);
1920 mutex_lock(&psp->securedisplay_context.mutex);
1922 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
1923 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1925 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
1927 mutex_unlock(&psp->securedisplay_context.mutex);
1930 psp_securedisplay_terminate(psp);
1931 /* free securedisplay shared memory */
1932 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
1933 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
1937 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
1938 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
1939 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
1940 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
1946 static int psp_securedisplay_terminate(struct psp_context *psp)
1951 * TODO:bypass the terminate in sriov for now
1953 if (amdgpu_sriov_vf(psp->adev))
1956 if (!psp->securedisplay_context.context.initialized)
1959 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
1961 psp->securedisplay_context.context.initialized = false;
1966 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1970 if (!psp->securedisplay_context.context.initialized)
1973 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
1974 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
1977 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
1981 /* SECUREDISPLAY end */
1983 static int psp_hw_start(struct psp_context *psp)
1985 struct amdgpu_device *adev = psp->adev;
1988 if (!amdgpu_sriov_vf(adev)) {
1989 if ((is_psp_fw_valid(psp->kdb)) &&
1990 (psp->funcs->bootloader_load_kdb != NULL)) {
1991 ret = psp_bootloader_load_kdb(psp);
1993 DRM_ERROR("PSP load kdb failed!\n");
1998 if ((is_psp_fw_valid(psp->spl)) &&
1999 (psp->funcs->bootloader_load_spl != NULL)) {
2000 ret = psp_bootloader_load_spl(psp);
2002 DRM_ERROR("PSP load spl failed!\n");
2007 if ((is_psp_fw_valid(psp->sys)) &&
2008 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2009 ret = psp_bootloader_load_sysdrv(psp);
2011 DRM_ERROR("PSP load sys drv failed!\n");
2016 if ((is_psp_fw_valid(psp->soc_drv)) &&
2017 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2018 ret = psp_bootloader_load_soc_drv(psp);
2020 DRM_ERROR("PSP load soc drv failed!\n");
2025 if ((is_psp_fw_valid(psp->intf_drv)) &&
2026 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2027 ret = psp_bootloader_load_intf_drv(psp);
2029 DRM_ERROR("PSP load intf drv failed!\n");
2034 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2035 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2036 ret = psp_bootloader_load_dbg_drv(psp);
2038 DRM_ERROR("PSP load dbg drv failed!\n");
2043 if ((is_psp_fw_valid(psp->ras_drv)) &&
2044 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2045 ret = psp_bootloader_load_ras_drv(psp);
2047 DRM_ERROR("PSP load ras_drv failed!\n");
2052 if ((is_psp_fw_valid(psp->sos)) &&
2053 (psp->funcs->bootloader_load_sos != NULL)) {
2054 ret = psp_bootloader_load_sos(psp);
2056 DRM_ERROR("PSP load sos failed!\n");
2062 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2064 DRM_ERROR("PSP create ring failed!\n");
2068 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2071 ret = psp_tmr_init(psp);
2073 DRM_ERROR("PSP tmr init failed!\n");
2079 * For ASICs with DF Cstate management centralized
2080 * to PMFW, TMR setup should be performed after PMFW
2081 * loaded and before other non-psp firmware loaded.
2083 if (psp->pmfw_centralized_cstate_management) {
2084 ret = psp_load_smu_fw(psp);
2089 ret = psp_tmr_load(psp);
2091 DRM_ERROR("PSP load tmr failed!\n");
2098 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2099 enum psp_gfx_fw_type *type)
2101 switch (ucode->ucode_id) {
2102 case AMDGPU_UCODE_ID_CAP:
2103 *type = GFX_FW_TYPE_CAP;
2105 case AMDGPU_UCODE_ID_SDMA0:
2106 *type = GFX_FW_TYPE_SDMA0;
2108 case AMDGPU_UCODE_ID_SDMA1:
2109 *type = GFX_FW_TYPE_SDMA1;
2111 case AMDGPU_UCODE_ID_SDMA2:
2112 *type = GFX_FW_TYPE_SDMA2;
2114 case AMDGPU_UCODE_ID_SDMA3:
2115 *type = GFX_FW_TYPE_SDMA3;
2117 case AMDGPU_UCODE_ID_SDMA4:
2118 *type = GFX_FW_TYPE_SDMA4;
2120 case AMDGPU_UCODE_ID_SDMA5:
2121 *type = GFX_FW_TYPE_SDMA5;
2123 case AMDGPU_UCODE_ID_SDMA6:
2124 *type = GFX_FW_TYPE_SDMA6;
2126 case AMDGPU_UCODE_ID_SDMA7:
2127 *type = GFX_FW_TYPE_SDMA7;
2129 case AMDGPU_UCODE_ID_CP_MES:
2130 *type = GFX_FW_TYPE_CP_MES;
2132 case AMDGPU_UCODE_ID_CP_MES_DATA:
2133 *type = GFX_FW_TYPE_MES_STACK;
2135 case AMDGPU_UCODE_ID_CP_MES1:
2136 *type = GFX_FW_TYPE_CP_MES_KIQ;
2138 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2139 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2141 case AMDGPU_UCODE_ID_CP_CE:
2142 *type = GFX_FW_TYPE_CP_CE;
2144 case AMDGPU_UCODE_ID_CP_PFP:
2145 *type = GFX_FW_TYPE_CP_PFP;
2147 case AMDGPU_UCODE_ID_CP_ME:
2148 *type = GFX_FW_TYPE_CP_ME;
2150 case AMDGPU_UCODE_ID_CP_MEC1:
2151 *type = GFX_FW_TYPE_CP_MEC;
2153 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2154 *type = GFX_FW_TYPE_CP_MEC_ME1;
2156 case AMDGPU_UCODE_ID_CP_MEC2:
2157 *type = GFX_FW_TYPE_CP_MEC;
2159 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2160 *type = GFX_FW_TYPE_CP_MEC_ME2;
2162 case AMDGPU_UCODE_ID_RLC_P:
2163 *type = GFX_FW_TYPE_RLC_P;
2165 case AMDGPU_UCODE_ID_RLC_V:
2166 *type = GFX_FW_TYPE_RLC_V;
2168 case AMDGPU_UCODE_ID_RLC_G:
2169 *type = GFX_FW_TYPE_RLC_G;
2171 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2172 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2174 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2175 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2177 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2178 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2180 case AMDGPU_UCODE_ID_RLC_IRAM:
2181 *type = GFX_FW_TYPE_RLC_IRAM;
2183 case AMDGPU_UCODE_ID_RLC_DRAM:
2184 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2186 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2187 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2189 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2190 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2192 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2193 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2195 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2196 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2198 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2199 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2201 case AMDGPU_UCODE_ID_SMC:
2202 *type = GFX_FW_TYPE_SMU;
2204 case AMDGPU_UCODE_ID_PPTABLE:
2205 *type = GFX_FW_TYPE_PPTABLE;
2207 case AMDGPU_UCODE_ID_UVD:
2208 *type = GFX_FW_TYPE_UVD;
2210 case AMDGPU_UCODE_ID_UVD1:
2211 *type = GFX_FW_TYPE_UVD1;
2213 case AMDGPU_UCODE_ID_VCE:
2214 *type = GFX_FW_TYPE_VCE;
2216 case AMDGPU_UCODE_ID_VCN:
2217 *type = GFX_FW_TYPE_VCN;
2219 case AMDGPU_UCODE_ID_VCN1:
2220 *type = GFX_FW_TYPE_VCN1;
2222 case AMDGPU_UCODE_ID_DMCU_ERAM:
2223 *type = GFX_FW_TYPE_DMCU_ERAM;
2225 case AMDGPU_UCODE_ID_DMCU_INTV:
2226 *type = GFX_FW_TYPE_DMCU_ISR;
2228 case AMDGPU_UCODE_ID_VCN0_RAM:
2229 *type = GFX_FW_TYPE_VCN0_RAM;
2231 case AMDGPU_UCODE_ID_VCN1_RAM:
2232 *type = GFX_FW_TYPE_VCN1_RAM;
2234 case AMDGPU_UCODE_ID_DMCUB:
2235 *type = GFX_FW_TYPE_DMUB;
2237 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2238 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2240 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2241 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2243 case AMDGPU_UCODE_ID_IMU_I:
2244 *type = GFX_FW_TYPE_IMU_I;
2246 case AMDGPU_UCODE_ID_IMU_D:
2247 *type = GFX_FW_TYPE_IMU_D;
2249 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2250 *type = GFX_FW_TYPE_RS64_PFP;
2252 case AMDGPU_UCODE_ID_CP_RS64_ME:
2253 *type = GFX_FW_TYPE_RS64_ME;
2255 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2256 *type = GFX_FW_TYPE_RS64_MEC;
2258 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2259 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2261 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2262 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2264 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2265 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2267 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2268 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2270 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2271 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2273 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2274 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2276 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2277 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2279 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2280 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2282 case AMDGPU_UCODE_ID_MAXIMUM:
2290 static void psp_print_fw_hdr(struct psp_context *psp,
2291 struct amdgpu_firmware_info *ucode)
2293 struct amdgpu_device *adev = psp->adev;
2294 struct common_firmware_header *hdr;
2296 switch (ucode->ucode_id) {
2297 case AMDGPU_UCODE_ID_SDMA0:
2298 case AMDGPU_UCODE_ID_SDMA1:
2299 case AMDGPU_UCODE_ID_SDMA2:
2300 case AMDGPU_UCODE_ID_SDMA3:
2301 case AMDGPU_UCODE_ID_SDMA4:
2302 case AMDGPU_UCODE_ID_SDMA5:
2303 case AMDGPU_UCODE_ID_SDMA6:
2304 case AMDGPU_UCODE_ID_SDMA7:
2305 hdr = (struct common_firmware_header *)
2306 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2307 amdgpu_ucode_print_sdma_hdr(hdr);
2309 case AMDGPU_UCODE_ID_CP_CE:
2310 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2311 amdgpu_ucode_print_gfx_hdr(hdr);
2313 case AMDGPU_UCODE_ID_CP_PFP:
2314 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2315 amdgpu_ucode_print_gfx_hdr(hdr);
2317 case AMDGPU_UCODE_ID_CP_ME:
2318 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2319 amdgpu_ucode_print_gfx_hdr(hdr);
2321 case AMDGPU_UCODE_ID_CP_MEC1:
2322 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2323 amdgpu_ucode_print_gfx_hdr(hdr);
2325 case AMDGPU_UCODE_ID_RLC_G:
2326 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2327 amdgpu_ucode_print_rlc_hdr(hdr);
2329 case AMDGPU_UCODE_ID_SMC:
2330 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2331 amdgpu_ucode_print_smc_hdr(hdr);
2338 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2339 struct psp_gfx_cmd_resp *cmd)
2342 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2344 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2345 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2346 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2347 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2349 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2351 DRM_ERROR("Unknown firmware type\n");
2356 static int psp_execute_non_psp_fw_load(struct psp_context *psp,
2357 struct amdgpu_firmware_info *ucode)
2360 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2362 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2364 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2365 psp->fence_buf_mc_addr);
2368 release_psp_cmd_buf(psp);
2373 static int psp_load_smu_fw(struct psp_context *psp)
2376 struct amdgpu_device *adev = psp->adev;
2377 struct amdgpu_firmware_info *ucode =
2378 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2379 struct amdgpu_ras *ras = psp->ras_context.ras;
2382 * Skip SMU FW reloading in case of using BACO for runpm only,
2383 * as SMU is always alive.
2385 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2388 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2391 if ((amdgpu_in_reset(adev) &&
2392 ras && adev->ras_enabled &&
2393 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2394 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2395 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2397 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2401 ret = psp_execute_non_psp_fw_load(psp, ucode);
2404 DRM_ERROR("PSP load smu failed!\n");
2409 static bool fw_load_skip_check(struct psp_context *psp,
2410 struct amdgpu_firmware_info *ucode)
2412 if (!ucode->fw || !ucode->ucode_size)
2415 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2416 (psp_smu_reload_quirk(psp) ||
2417 psp->autoload_supported ||
2418 psp->pmfw_centralized_cstate_management))
2421 if (amdgpu_sriov_vf(psp->adev) &&
2422 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2425 if (psp->autoload_supported &&
2426 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2427 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2428 /* skip mec JT when autoload is enabled */
2434 int psp_load_fw_list(struct psp_context *psp,
2435 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2438 struct amdgpu_firmware_info *ucode;
2440 for (i = 0; i < ucode_count; ++i) {
2441 ucode = ucode_list[i];
2442 psp_print_fw_hdr(psp, ucode);
2443 ret = psp_execute_non_psp_fw_load(psp, ucode);
2450 static int psp_load_non_psp_fw(struct psp_context *psp)
2453 struct amdgpu_firmware_info *ucode;
2454 struct amdgpu_device *adev = psp->adev;
2456 if (psp->autoload_supported &&
2457 !psp->pmfw_centralized_cstate_management) {
2458 ret = psp_load_smu_fw(psp);
2463 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2464 ucode = &adev->firmware.ucode[i];
2466 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2467 !fw_load_skip_check(psp, ucode)) {
2468 ret = psp_load_smu_fw(psp);
2474 if (fw_load_skip_check(psp, ucode))
2477 if (psp->autoload_supported &&
2478 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2479 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2480 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2481 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2482 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2483 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2484 /* PSP only receive one SDMA fw for sienna_cichlid,
2485 * as all four sdma fw are same */
2488 psp_print_fw_hdr(psp, ucode);
2490 ret = psp_execute_non_psp_fw_load(psp, ucode);
2494 /* Start rlc autoload after psp recieved all the gfx firmware */
2495 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2496 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2497 ret = psp_rlc_autoload_start(psp);
2499 DRM_ERROR("Failed to start rlc autoload\n");
2508 static int psp_load_fw(struct amdgpu_device *adev)
2511 struct psp_context *psp = &adev->psp;
2513 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2514 /* should not destroy ring, only stop */
2515 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2517 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2519 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2521 DRM_ERROR("PSP ring init failed!\n");
2526 ret = psp_hw_start(psp);
2530 ret = psp_load_non_psp_fw(psp);
2534 ret = psp_asd_initialize(psp);
2536 DRM_ERROR("PSP load asd failed!\n");
2540 ret = psp_rl_load(adev);
2542 DRM_ERROR("PSP load RL failed!\n");
2546 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2547 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2548 ret = psp_xgmi_initialize(psp, false, true);
2549 /* Warning the XGMI seesion initialize failure
2550 * Instead of stop driver initialization
2553 dev_err(psp->adev->dev,
2554 "XGMI: Failed to initialize XGMI session\n");
2559 ret = psp_ras_initialize(psp);
2561 dev_err(psp->adev->dev,
2562 "RAS: Failed to initialize RAS\n");
2564 ret = psp_hdcp_initialize(psp);
2566 dev_err(psp->adev->dev,
2567 "HDCP: Failed to initialize HDCP\n");
2569 ret = psp_dtm_initialize(psp);
2571 dev_err(psp->adev->dev,
2572 "DTM: Failed to initialize DTM\n");
2574 ret = psp_rap_initialize(psp);
2576 dev_err(psp->adev->dev,
2577 "RAP: Failed to initialize RAP\n");
2579 ret = psp_securedisplay_initialize(psp);
2581 dev_err(psp->adev->dev,
2582 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2588 psp_free_shared_bufs(psp);
2591 * all cleanup jobs (xgmi terminate, ras terminate,
2592 * ring destroy, cmd/fence/fw buffers destory,
2593 * psp->cmd destory) are delayed to psp_hw_fini
2595 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2599 static int psp_hw_init(void *handle)
2602 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2604 mutex_lock(&adev->firmware.mutex);
2606 * This sequence is just used on hw_init only once, no need on
2609 ret = amdgpu_ucode_init_bo(adev);
2613 ret = psp_load_fw(adev);
2615 DRM_ERROR("PSP firmware loading failed\n");
2619 mutex_unlock(&adev->firmware.mutex);
2623 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2624 mutex_unlock(&adev->firmware.mutex);
2628 static int psp_hw_fini(void *handle)
2630 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2631 struct psp_context *psp = &adev->psp;
2634 psp_ras_terminate(psp);
2635 psp_securedisplay_terminate(psp);
2636 psp_rap_terminate(psp);
2637 psp_dtm_terminate(psp);
2638 psp_hdcp_terminate(psp);
2640 if (adev->gmc.xgmi.num_physical_nodes > 1)
2641 psp_xgmi_terminate(psp);
2644 psp_asd_terminate(psp);
2645 psp_tmr_terminate(psp);
2647 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2649 psp_free_shared_bufs(psp);
2654 static int psp_suspend(void *handle)
2657 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2658 struct psp_context *psp = &adev->psp;
2660 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2661 psp->xgmi_context.context.initialized) {
2662 ret = psp_xgmi_terminate(psp);
2664 DRM_ERROR("Failed to terminate xgmi ta\n");
2670 ret = psp_ras_terminate(psp);
2672 DRM_ERROR("Failed to terminate ras ta\n");
2675 ret = psp_hdcp_terminate(psp);
2677 DRM_ERROR("Failed to terminate hdcp ta\n");
2680 ret = psp_dtm_terminate(psp);
2682 DRM_ERROR("Failed to terminate dtm ta\n");
2685 ret = psp_rap_terminate(psp);
2687 DRM_ERROR("Failed to terminate rap ta\n");
2690 ret = psp_securedisplay_terminate(psp);
2692 DRM_ERROR("Failed to terminate securedisplay ta\n");
2697 ret = psp_asd_terminate(psp);
2699 DRM_ERROR("Failed to terminate asd\n");
2703 ret = psp_tmr_terminate(psp);
2705 DRM_ERROR("Failed to terminate tmr\n");
2709 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2711 DRM_ERROR("PSP ring stop failed\n");
2718 static int psp_resume(void *handle)
2721 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2722 struct psp_context *psp = &adev->psp;
2724 DRM_INFO("PSP is resuming...\n");
2726 if (psp->mem_train_ctx.enable_mem_training) {
2727 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2729 DRM_ERROR("Failed to process memory training!\n");
2734 mutex_lock(&adev->firmware.mutex);
2736 ret = psp_hw_start(psp);
2740 ret = psp_load_non_psp_fw(psp);
2744 ret = psp_asd_initialize(psp);
2746 DRM_ERROR("PSP load asd failed!\n");
2750 ret = psp_rl_load(adev);
2752 dev_err(adev->dev, "PSP load RL failed!\n");
2756 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2757 ret = psp_xgmi_initialize(psp, false, true);
2758 /* Warning the XGMI seesion initialize failure
2759 * Instead of stop driver initialization
2762 dev_err(psp->adev->dev,
2763 "XGMI: Failed to initialize XGMI session\n");
2767 ret = psp_ras_initialize(psp);
2769 dev_err(psp->adev->dev,
2770 "RAS: Failed to initialize RAS\n");
2772 ret = psp_hdcp_initialize(psp);
2774 dev_err(psp->adev->dev,
2775 "HDCP: Failed to initialize HDCP\n");
2777 ret = psp_dtm_initialize(psp);
2779 dev_err(psp->adev->dev,
2780 "DTM: Failed to initialize DTM\n");
2782 ret = psp_rap_initialize(psp);
2784 dev_err(psp->adev->dev,
2785 "RAP: Failed to initialize RAP\n");
2787 ret = psp_securedisplay_initialize(psp);
2789 dev_err(psp->adev->dev,
2790 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2793 mutex_unlock(&adev->firmware.mutex);
2798 DRM_ERROR("PSP resume failed\n");
2799 mutex_unlock(&adev->firmware.mutex);
2803 int psp_gpu_reset(struct amdgpu_device *adev)
2807 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2810 mutex_lock(&adev->psp.mutex);
2811 ret = psp_mode1_reset(&adev->psp);
2812 mutex_unlock(&adev->psp.mutex);
2817 int psp_rlc_autoload_start(struct psp_context *psp)
2820 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2822 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2824 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2825 psp->fence_buf_mc_addr);
2827 release_psp_cmd_buf(psp);
2832 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
2833 uint64_t cmd_gpu_addr, int cmd_size)
2835 struct amdgpu_firmware_info ucode = {0};
2837 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
2838 AMDGPU_UCODE_ID_VCN0_RAM;
2839 ucode.mc_addr = cmd_gpu_addr;
2840 ucode.ucode_size = cmd_size;
2842 return psp_execute_non_psp_fw_load(&adev->psp, &ucode);
2845 int psp_ring_cmd_submit(struct psp_context *psp,
2846 uint64_t cmd_buf_mc_addr,
2847 uint64_t fence_mc_addr,
2850 unsigned int psp_write_ptr_reg = 0;
2851 struct psp_gfx_rb_frame *write_frame;
2852 struct psp_ring *ring = &psp->km_ring;
2853 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2854 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2855 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2856 struct amdgpu_device *adev = psp->adev;
2857 uint32_t ring_size_dw = ring->ring_size / 4;
2858 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2860 /* KM (GPCOM) prepare write pointer */
2861 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2863 /* Update KM RB frame pointer to new frame */
2864 /* write_frame ptr increments by size of rb_frame in bytes */
2865 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2866 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2867 write_frame = ring_buffer_start;
2869 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2870 /* Check invalid write_frame ptr address */
2871 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2872 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2873 ring_buffer_start, ring_buffer_end, write_frame);
2874 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2878 /* Initialize KM RB frame */
2879 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2881 /* Update KM RB frame */
2882 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2883 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2884 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2885 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2886 write_frame->fence_value = index;
2887 amdgpu_device_flush_hdp(adev, NULL);
2889 /* Update the write Pointer in DWORDs */
2890 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2891 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2895 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2897 struct amdgpu_device *adev = psp->adev;
2898 char fw_name[PSP_FW_NAME_LEN];
2899 const struct psp_firmware_header_v1_0 *asd_hdr;
2902 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2903 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2907 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2908 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2909 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2910 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2911 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2912 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2915 amdgpu_ucode_release(&adev->psp.asd_fw);
2919 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
2921 struct amdgpu_device *adev = psp->adev;
2922 char fw_name[PSP_FW_NAME_LEN];
2923 const struct psp_firmware_header_v1_0 *toc_hdr;
2926 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
2927 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
2931 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
2932 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
2933 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
2934 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
2935 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
2936 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
2939 amdgpu_ucode_release(&adev->psp.toc_fw);
2943 static int parse_sos_bin_descriptor(struct psp_context *psp,
2944 const struct psp_fw_bin_desc *desc,
2945 const struct psp_firmware_header_v2_0 *sos_hdr)
2947 uint8_t *ucode_start_addr = NULL;
2949 if (!psp || !desc || !sos_hdr)
2952 ucode_start_addr = (uint8_t *)sos_hdr +
2953 le32_to_cpu(desc->offset_bytes) +
2954 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
2956 switch (desc->fw_type) {
2957 case PSP_FW_TYPE_PSP_SOS:
2958 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
2959 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
2960 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
2961 psp->sos.start_addr = ucode_start_addr;
2963 case PSP_FW_TYPE_PSP_SYS_DRV:
2964 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
2965 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
2966 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
2967 psp->sys.start_addr = ucode_start_addr;
2969 case PSP_FW_TYPE_PSP_KDB:
2970 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
2971 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
2972 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
2973 psp->kdb.start_addr = ucode_start_addr;
2975 case PSP_FW_TYPE_PSP_TOC:
2976 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
2977 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
2978 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
2979 psp->toc.start_addr = ucode_start_addr;
2981 case PSP_FW_TYPE_PSP_SPL:
2982 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
2983 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
2984 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
2985 psp->spl.start_addr = ucode_start_addr;
2987 case PSP_FW_TYPE_PSP_RL:
2988 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
2989 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
2990 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
2991 psp->rl.start_addr = ucode_start_addr;
2993 case PSP_FW_TYPE_PSP_SOC_DRV:
2994 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
2995 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
2996 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
2997 psp->soc_drv.start_addr = ucode_start_addr;
2999 case PSP_FW_TYPE_PSP_INTF_DRV:
3000 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3001 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3002 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3003 psp->intf_drv.start_addr = ucode_start_addr;
3005 case PSP_FW_TYPE_PSP_DBG_DRV:
3006 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3007 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3008 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3009 psp->dbg_drv.start_addr = ucode_start_addr;
3011 case PSP_FW_TYPE_PSP_RAS_DRV:
3012 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3013 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3014 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3015 psp->ras_drv.start_addr = ucode_start_addr;
3018 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3025 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3027 const struct psp_firmware_header_v1_0 *sos_hdr;
3028 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3029 uint8_t *ucode_array_start_addr;
3031 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3032 ucode_array_start_addr = (uint8_t *)sos_hdr +
3033 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3035 if (adev->gmc.xgmi.connected_to_cpu ||
3036 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3037 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3038 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3040 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3041 adev->psp.sys.start_addr = ucode_array_start_addr;
3043 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3044 adev->psp.sos.start_addr = ucode_array_start_addr +
3045 le32_to_cpu(sos_hdr->sos.offset_bytes);
3047 /* Load alternate PSP SOS FW */
3048 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3050 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3051 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3053 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3054 adev->psp.sys.start_addr = ucode_array_start_addr +
3055 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3057 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3058 adev->psp.sos.start_addr = ucode_array_start_addr +
3059 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3062 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3063 dev_warn(adev->dev, "PSP SOS FW not available");
3070 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3072 struct amdgpu_device *adev = psp->adev;
3073 char fw_name[PSP_FW_NAME_LEN];
3074 const struct psp_firmware_header_v1_0 *sos_hdr;
3075 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3076 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3077 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3078 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3080 uint8_t *ucode_array_start_addr;
3083 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3084 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3088 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3089 ucode_array_start_addr = (uint8_t *)sos_hdr +
3090 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3091 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3093 switch (sos_hdr->header.header_version_major) {
3095 err = psp_init_sos_base_fw(adev);
3099 if (sos_hdr->header.header_version_minor == 1) {
3100 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3101 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3102 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3103 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3104 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3105 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3106 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3108 if (sos_hdr->header.header_version_minor == 2) {
3109 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3110 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3111 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3112 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3114 if (sos_hdr->header.header_version_minor == 3) {
3115 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3116 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3117 adev->psp.toc.start_addr = ucode_array_start_addr +
3118 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3119 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3120 adev->psp.kdb.start_addr = ucode_array_start_addr +
3121 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3122 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3123 adev->psp.spl.start_addr = ucode_array_start_addr +
3124 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3125 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3126 adev->psp.rl.start_addr = ucode_array_start_addr +
3127 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3131 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3133 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3134 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3139 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3140 err = parse_sos_bin_descriptor(psp,
3141 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3149 "unsupported psp sos firmware\n");
3156 amdgpu_ucode_release(&adev->psp.sos_fw);
3161 static int parse_ta_bin_descriptor(struct psp_context *psp,
3162 const struct psp_fw_bin_desc *desc,
3163 const struct ta_firmware_header_v2_0 *ta_hdr)
3165 uint8_t *ucode_start_addr = NULL;
3167 if (!psp || !desc || !ta_hdr)
3170 ucode_start_addr = (uint8_t *)ta_hdr +
3171 le32_to_cpu(desc->offset_bytes) +
3172 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3174 switch (desc->fw_type) {
3175 case TA_FW_TYPE_PSP_ASD:
3176 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3177 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3178 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3179 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3181 case TA_FW_TYPE_PSP_XGMI:
3182 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3183 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3184 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3186 case TA_FW_TYPE_PSP_RAS:
3187 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3188 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3189 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3191 case TA_FW_TYPE_PSP_HDCP:
3192 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3193 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3194 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3196 case TA_FW_TYPE_PSP_DTM:
3197 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3198 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3199 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3201 case TA_FW_TYPE_PSP_RAP:
3202 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3203 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3204 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3206 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3207 psp->securedisplay_context.context.bin_desc.fw_version =
3208 le32_to_cpu(desc->fw_version);
3209 psp->securedisplay_context.context.bin_desc.size_bytes =
3210 le32_to_cpu(desc->size_bytes);
3211 psp->securedisplay_context.context.bin_desc.start_addr =
3215 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3222 static int parse_ta_v1_microcode(struct psp_context *psp)
3224 const struct ta_firmware_header_v1_0 *ta_hdr;
3225 struct amdgpu_device *adev = psp->adev;
3227 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3229 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3232 adev->psp.xgmi_context.context.bin_desc.fw_version =
3233 le32_to_cpu(ta_hdr->xgmi.fw_version);
3234 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3235 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3236 adev->psp.xgmi_context.context.bin_desc.start_addr =
3238 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3240 adev->psp.ras_context.context.bin_desc.fw_version =
3241 le32_to_cpu(ta_hdr->ras.fw_version);
3242 adev->psp.ras_context.context.bin_desc.size_bytes =
3243 le32_to_cpu(ta_hdr->ras.size_bytes);
3244 adev->psp.ras_context.context.bin_desc.start_addr =
3245 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3246 le32_to_cpu(ta_hdr->ras.offset_bytes);
3248 adev->psp.hdcp_context.context.bin_desc.fw_version =
3249 le32_to_cpu(ta_hdr->hdcp.fw_version);
3250 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3251 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3252 adev->psp.hdcp_context.context.bin_desc.start_addr =
3254 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3256 adev->psp.dtm_context.context.bin_desc.fw_version =
3257 le32_to_cpu(ta_hdr->dtm.fw_version);
3258 adev->psp.dtm_context.context.bin_desc.size_bytes =
3259 le32_to_cpu(ta_hdr->dtm.size_bytes);
3260 adev->psp.dtm_context.context.bin_desc.start_addr =
3261 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3262 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3264 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3265 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3266 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3267 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3268 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3269 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3270 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3272 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3277 static int parse_ta_v2_microcode(struct psp_context *psp)
3279 const struct ta_firmware_header_v2_0 *ta_hdr;
3280 struct amdgpu_device *adev = psp->adev;
3284 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3286 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3289 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3290 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3294 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3295 err = parse_ta_bin_descriptor(psp,
3296 &ta_hdr->ta_fw_bin[ta_index],
3305 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3307 const struct common_firmware_header *hdr;
3308 struct amdgpu_device *adev = psp->adev;
3309 char fw_name[PSP_FW_NAME_LEN];
3312 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3313 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3317 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3318 switch (le16_to_cpu(hdr->header_version_major)) {
3320 err = parse_ta_v1_microcode(psp);
3323 err = parse_ta_v2_microcode(psp);
3326 dev_err(adev->dev, "unsupported TA header version\n");
3331 amdgpu_ucode_release(&adev->psp.ta_fw);
3336 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3338 struct amdgpu_device *adev = psp->adev;
3339 char fw_name[PSP_FW_NAME_LEN];
3340 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3341 struct amdgpu_firmware_info *info = NULL;
3344 if (!amdgpu_sriov_vf(adev)) {
3345 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3349 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3350 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3352 if (err == -ENODEV) {
3353 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3357 dev_err(adev->dev, "fail to initialize cap microcode\n");
3360 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3361 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3362 info->fw = adev->psp.cap_fw;
3363 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3364 adev->psp.cap_fw->data;
3365 adev->firmware.fw_size += ALIGN(
3366 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3367 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3368 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3369 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3374 amdgpu_ucode_release(&adev->psp.cap_fw);
3378 static int psp_set_clockgating_state(void *handle,
3379 enum amd_clockgating_state state)
3384 static int psp_set_powergating_state(void *handle,
3385 enum amd_powergating_state state)
3390 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3391 struct device_attribute *attr,
3394 struct drm_device *ddev = dev_get_drvdata(dev);
3395 struct amdgpu_device *adev = drm_to_adev(ddev);
3399 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3400 DRM_INFO("PSP block is not ready yet.");
3404 mutex_lock(&adev->psp.mutex);
3405 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3406 mutex_unlock(&adev->psp.mutex);
3409 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3413 return sysfs_emit(buf, "%x\n", fw_ver);
3416 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3417 struct device_attribute *attr,
3421 struct drm_device *ddev = dev_get_drvdata(dev);
3422 struct amdgpu_device *adev = drm_to_adev(ddev);
3425 const struct firmware *usbc_pd_fw;
3426 struct amdgpu_bo *fw_buf_bo = NULL;
3427 uint64_t fw_pri_mc_addr;
3428 void *fw_pri_cpu_addr;
3430 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3431 DRM_INFO("PSP block is not ready yet.");
3435 if (!drm_dev_enter(ddev, &idx))
3438 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3439 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3443 /* LFB address which is aligned to 1MB boundary per PSP request */
3444 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3445 AMDGPU_GEM_DOMAIN_VRAM |
3446 AMDGPU_GEM_DOMAIN_GTT,
3447 &fw_buf_bo, &fw_pri_mc_addr,
3452 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3454 mutex_lock(&adev->psp.mutex);
3455 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3456 mutex_unlock(&adev->psp.mutex);
3458 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3461 release_firmware(usbc_pd_fw);
3464 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3472 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3476 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3479 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3480 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3485 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
3486 psp_usbc_pd_fw_sysfs_read,
3487 psp_usbc_pd_fw_sysfs_write);
3489 int is_psp_fw_valid(struct psp_bin_desc bin)
3491 return bin.size_bytes;
3494 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3495 struct bin_attribute *bin_attr,
3496 char *buffer, loff_t pos, size_t count)
3498 struct device *dev = kobj_to_dev(kobj);
3499 struct drm_device *ddev = dev_get_drvdata(dev);
3500 struct amdgpu_device *adev = drm_to_adev(ddev);
3502 adev->psp.vbflash_done = false;
3504 /* Safeguard against memory drain */
3505 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3506 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3507 kvfree(adev->psp.vbflash_tmp_buf);
3508 adev->psp.vbflash_tmp_buf = NULL;
3509 adev->psp.vbflash_image_size = 0;
3513 /* TODO Just allocate max for now and optimize to realloc later if needed */
3514 if (!adev->psp.vbflash_tmp_buf) {
3515 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3516 if (!adev->psp.vbflash_tmp_buf)
3520 mutex_lock(&adev->psp.mutex);
3521 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3522 adev->psp.vbflash_image_size += count;
3523 mutex_unlock(&adev->psp.mutex);
3525 dev_info(adev->dev, "VBIOS flash write PSP done");
3530 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3531 struct bin_attribute *bin_attr, char *buffer,
3532 loff_t pos, size_t count)
3534 struct device *dev = kobj_to_dev(kobj);
3535 struct drm_device *ddev = dev_get_drvdata(dev);
3536 struct amdgpu_device *adev = drm_to_adev(ddev);
3537 struct amdgpu_bo *fw_buf_bo = NULL;
3538 uint64_t fw_pri_mc_addr;
3539 void *fw_pri_cpu_addr;
3542 dev_info(adev->dev, "VBIOS flash to PSP started");
3544 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3545 AMDGPU_GPU_PAGE_SIZE,
3546 AMDGPU_GEM_DOMAIN_VRAM,
3553 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3555 mutex_lock(&adev->psp.mutex);
3556 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3557 mutex_unlock(&adev->psp.mutex);
3559 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3562 kvfree(adev->psp.vbflash_tmp_buf);
3563 adev->psp.vbflash_tmp_buf = NULL;
3564 adev->psp.vbflash_image_size = 0;
3567 dev_err(adev->dev, "Failed to load VBIOS FW, err = %d", ret);
3571 dev_info(adev->dev, "VBIOS flash to PSP done");
3575 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3576 struct device_attribute *attr,
3579 struct drm_device *ddev = dev_get_drvdata(dev);
3580 struct amdgpu_device *adev = drm_to_adev(ddev);
3581 uint32_t vbflash_status;
3583 vbflash_status = psp_vbflash_status(&adev->psp);
3584 if (!adev->psp.vbflash_done)
3586 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3589 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3592 static const struct bin_attribute psp_vbflash_bin_attr = {
3593 .attr = {.name = "psp_vbflash", .mode = 0664},
3595 .write = amdgpu_psp_vbflash_write,
3596 .read = amdgpu_psp_vbflash_read,
3599 static DEVICE_ATTR(psp_vbflash_status, 0444, amdgpu_psp_vbflash_status, NULL);
3601 int amdgpu_psp_sysfs_init(struct amdgpu_device *adev)
3604 struct psp_context *psp = &adev->psp;
3606 if (amdgpu_sriov_vf(adev))
3609 switch (adev->ip_versions[MP0_HWIP][0]) {
3610 case IP_VERSION(13, 0, 0):
3611 case IP_VERSION(13, 0, 7):
3614 psp_v13_0_set_psp_funcs(psp);
3616 ret = sysfs_create_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3618 dev_err(adev->dev, "Failed to create device file psp_vbflash");
3619 ret = device_create_file(adev->dev, &dev_attr_psp_vbflash_status);
3621 dev_err(adev->dev, "Failed to create device file psp_vbflash_status");
3628 const struct amd_ip_funcs psp_ip_funcs = {
3630 .early_init = psp_early_init,
3632 .sw_init = psp_sw_init,
3633 .sw_fini = psp_sw_fini,
3634 .hw_init = psp_hw_init,
3635 .hw_fini = psp_hw_fini,
3636 .suspend = psp_suspend,
3637 .resume = psp_resume,
3639 .check_soft_reset = NULL,
3640 .wait_for_idle = NULL,
3642 .set_clockgating_state = psp_set_clockgating_state,
3643 .set_powergating_state = psp_set_powergating_state,
3646 static int psp_sysfs_init(struct amdgpu_device *adev)
3648 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
3651 DRM_ERROR("Failed to create USBC PD FW control file!");
3656 void amdgpu_psp_sysfs_fini(struct amdgpu_device *adev)
3658 sysfs_remove_bin_file(&adev->dev->kobj, &psp_vbflash_bin_attr);
3659 device_remove_file(adev->dev, &dev_attr_psp_vbflash_status);
3662 static void psp_sysfs_fini(struct amdgpu_device *adev)
3664 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
3667 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
3669 .type = AMD_IP_BLOCK_TYPE_PSP,
3673 .funcs = &psp_ip_funcs,
3676 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
3678 .type = AMD_IP_BLOCK_TYPE_PSP,
3682 .funcs = &psp_ip_funcs,
3685 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
3687 .type = AMD_IP_BLOCK_TYPE_PSP,
3691 .funcs = &psp_ip_funcs,
3694 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3695 .type = AMD_IP_BLOCK_TYPE_PSP,
3699 .funcs = &psp_ip_funcs,
3702 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
3704 .type = AMD_IP_BLOCK_TYPE_PSP,
3708 .funcs = &psp_ip_funcs,
3711 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3712 .type = AMD_IP_BLOCK_TYPE_PSP,
3716 .funcs = &psp_ip_funcs,
3719 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3720 .type = AMD_IP_BLOCK_TYPE_PSP,
3724 .funcs = &psp_ip_funcs,