2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/firmware.h>
27 #include <drm/drm_drv.h>
30 #include "amdgpu_psp.h"
31 #include "amdgpu_ucode.h"
32 #include "amdgpu_xgmi.h"
33 #include "soc15_common.h"
35 #include "psp_v10_0.h"
36 #include "psp_v11_0.h"
37 #include "psp_v11_0_8.h"
38 #include "psp_v12_0.h"
39 #include "psp_v13_0.h"
40 #include "psp_v13_0_4.h"
42 #include "amdgpu_ras.h"
43 #include "amdgpu_securedisplay.h"
44 #include "amdgpu_atomfirmware.h"
46 #define AMD_VBIOS_FILE_MAX_SIZE_B (1024*1024*3)
48 static int psp_load_smu_fw(struct psp_context *psp);
49 static int psp_rap_terminate(struct psp_context *psp);
50 static int psp_securedisplay_terminate(struct psp_context *psp);
52 static int psp_ring_init(struct psp_context *psp,
53 enum psp_ring_type ring_type)
56 struct psp_ring *ring;
57 struct amdgpu_device *adev = psp->adev;
61 ring->ring_type = ring_type;
63 /* allocate 4k Page of Local Frame Buffer memory for ring */
64 ring->ring_size = 0x1000;
65 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
66 AMDGPU_GEM_DOMAIN_VRAM |
67 AMDGPU_GEM_DOMAIN_GTT,
69 &ring->ring_mem_mc_addr,
70 (void **)&ring->ring_mem);
80 * Due to DF Cstate management centralized to PMFW, the firmware
81 * loading sequence will be updated as below:
87 * - Load other non-psp fw
89 * - Load XGMI/RAS/HDCP/DTM TA if any
91 * This new sequence is required for
92 * - Arcturus and onwards
94 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
96 struct amdgpu_device *adev = psp->adev;
98 if (amdgpu_sriov_vf(adev)) {
99 psp->pmfw_centralized_cstate_management = false;
103 switch (adev->ip_versions[MP0_HWIP][0]) {
104 case IP_VERSION(11, 0, 0):
105 case IP_VERSION(11, 0, 4):
106 case IP_VERSION(11, 0, 5):
107 case IP_VERSION(11, 0, 7):
108 case IP_VERSION(11, 0, 9):
109 case IP_VERSION(11, 0, 11):
110 case IP_VERSION(11, 0, 12):
111 case IP_VERSION(11, 0, 13):
112 case IP_VERSION(13, 0, 0):
113 case IP_VERSION(13, 0, 2):
114 case IP_VERSION(13, 0, 7):
115 psp->pmfw_centralized_cstate_management = true;
118 psp->pmfw_centralized_cstate_management = false;
123 static int psp_init_sriov_microcode(struct psp_context *psp)
125 struct amdgpu_device *adev = psp->adev;
126 char ucode_prefix[30];
129 amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
131 switch (adev->ip_versions[MP0_HWIP][0]) {
132 case IP_VERSION(9, 0, 0):
133 case IP_VERSION(11, 0, 7):
134 case IP_VERSION(11, 0, 9):
135 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
136 ret = psp_init_cap_microcode(psp, ucode_prefix);
138 case IP_VERSION(13, 0, 2):
139 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
140 ret = psp_init_cap_microcode(psp, ucode_prefix);
141 ret &= psp_init_ta_microcode(psp, ucode_prefix);
143 case IP_VERSION(13, 0, 0):
144 adev->virt.autoload_ucode_id = 0;
146 case IP_VERSION(13, 0, 6):
147 ret = psp_init_cap_microcode(psp, ucode_prefix);
149 case IP_VERSION(13, 0, 10):
150 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
151 ret = psp_init_cap_microcode(psp, ucode_prefix);
159 static int psp_early_init(void *handle)
161 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
162 struct psp_context *psp = &adev->psp;
164 switch (adev->ip_versions[MP0_HWIP][0]) {
165 case IP_VERSION(9, 0, 0):
166 psp_v3_1_set_psp_funcs(psp);
167 psp->autoload_supported = false;
169 case IP_VERSION(10, 0, 0):
170 case IP_VERSION(10, 0, 1):
171 psp_v10_0_set_psp_funcs(psp);
172 psp->autoload_supported = false;
174 case IP_VERSION(11, 0, 2):
175 case IP_VERSION(11, 0, 4):
176 psp_v11_0_set_psp_funcs(psp);
177 psp->autoload_supported = false;
179 case IP_VERSION(11, 0, 0):
180 case IP_VERSION(11, 0, 7):
181 adev->psp.sup_pd_fw_up = !amdgpu_sriov_vf(adev);
183 case IP_VERSION(11, 0, 5):
184 case IP_VERSION(11, 0, 9):
185 case IP_VERSION(11, 0, 11):
186 case IP_VERSION(11, 5, 0):
187 case IP_VERSION(11, 0, 12):
188 case IP_VERSION(11, 0, 13):
189 psp_v11_0_set_psp_funcs(psp);
190 psp->autoload_supported = true;
192 case IP_VERSION(11, 0, 3):
193 case IP_VERSION(12, 0, 1):
194 psp_v12_0_set_psp_funcs(psp);
196 case IP_VERSION(13, 0, 2):
197 case IP_VERSION(13, 0, 6):
198 psp_v13_0_set_psp_funcs(psp);
200 case IP_VERSION(13, 0, 1):
201 case IP_VERSION(13, 0, 3):
202 case IP_VERSION(13, 0, 5):
203 case IP_VERSION(13, 0, 8):
204 case IP_VERSION(13, 0, 11):
205 case IP_VERSION(14, 0, 0):
206 psp_v13_0_set_psp_funcs(psp);
207 psp->autoload_supported = true;
209 case IP_VERSION(11, 0, 8):
210 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
211 psp_v11_0_8_set_psp_funcs(psp);
212 psp->autoload_supported = false;
215 case IP_VERSION(13, 0, 0):
216 case IP_VERSION(13, 0, 7):
217 case IP_VERSION(13, 0, 10):
218 psp_v13_0_set_psp_funcs(psp);
219 psp->autoload_supported = true;
220 adev->psp.sup_ifwi_up = !amdgpu_sriov_vf(adev);
222 case IP_VERSION(13, 0, 4):
223 psp_v13_0_4_set_psp_funcs(psp);
224 psp->autoload_supported = true;
232 psp_check_pmfw_centralized_cstate_management(psp);
234 if (amdgpu_sriov_vf(adev))
235 return psp_init_sriov_microcode(psp);
237 return psp_init_microcode(psp);
240 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
242 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
243 &mem_ctx->shared_buf);
244 mem_ctx->shared_bo = NULL;
247 static void psp_free_shared_bufs(struct psp_context *psp)
252 /* free TMR memory buffer */
253 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
254 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
257 /* free xgmi shared memory */
258 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
260 /* free ras shared memory */
261 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
263 /* free hdcp shared memory */
264 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
266 /* free dtm shared memory */
267 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
269 /* free rap shared memory */
270 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
272 /* free securedisplay shared memory */
273 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
278 static void psp_memory_training_fini(struct psp_context *psp)
280 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
282 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
283 kfree(ctx->sys_cache);
284 ctx->sys_cache = NULL;
287 static int psp_memory_training_init(struct psp_context *psp)
290 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
292 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
293 DRM_DEBUG("memory training is not supported!\n");
297 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
298 if (ctx->sys_cache == NULL) {
299 DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
304 DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
305 ctx->train_data_size,
306 ctx->p2c_train_data_offset,
307 ctx->c2p_train_data_offset);
308 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
312 psp_memory_training_fini(psp);
317 * Helper funciton to query psp runtime database entry
319 * @adev: amdgpu_device pointer
320 * @entry_type: the type of psp runtime database entry
321 * @db_entry: runtime database entry pointer
323 * Return false if runtime database doesn't exit or entry is invalid
324 * or true if the specific database entry is found, and copy to @db_entry
326 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
327 enum psp_runtime_entry_type entry_type,
330 uint64_t db_header_pos, db_dir_pos;
331 struct psp_runtime_data_header db_header = {0};
332 struct psp_runtime_data_directory db_dir = {0};
336 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6))
339 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
340 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
342 /* read runtime db header from vram */
343 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
344 sizeof(struct psp_runtime_data_header), false);
346 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
347 /* runtime db doesn't exist, exit */
348 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
352 /* read runtime database entry from vram */
353 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
354 sizeof(struct psp_runtime_data_directory), false);
356 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
357 /* invalid db entry count, exit */
358 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
362 /* look up for requested entry type */
363 for (i = 0; i < db_dir.entry_count && !ret; i++) {
364 if (db_dir.entry_list[i].entry_type == entry_type) {
365 switch (entry_type) {
366 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
367 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
368 /* invalid db entry size */
369 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
372 /* read runtime database entry */
373 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
374 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
377 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
378 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
379 /* invalid db entry size */
380 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
383 /* read runtime database entry */
384 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
385 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
398 static int psp_sw_init(void *handle)
400 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
401 struct psp_context *psp = &adev->psp;
403 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
404 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
405 struct psp_runtime_scpm_entry scpm_entry;
407 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
409 DRM_ERROR("Failed to allocate memory to command buffer!\n");
413 adev->psp.xgmi_context.supports_extended_data =
414 !adev->gmc.xgmi.connected_to_cpu &&
415 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
417 memset(&scpm_entry, 0, sizeof(scpm_entry));
418 if ((psp_get_runtime_db_entry(adev,
419 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
421 (scpm_entry.scpm_status != SCPM_DISABLE)) {
422 adev->scpm_enabled = true;
423 adev->scpm_status = scpm_entry.scpm_status;
425 adev->scpm_enabled = false;
426 adev->scpm_status = SCPM_DISABLE;
429 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
431 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
432 if (psp_get_runtime_db_entry(adev,
433 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
435 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
436 if ((psp->boot_cfg_bitmask) &
437 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
438 /* If psp runtime database exists, then
439 * only enable two stage memory training
440 * when TWO_STAGE_DRAM_TRAINING bit is set
441 * in runtime database
443 mem_training_ctx->enable_mem_training = true;
447 /* If psp runtime database doesn't exist or is
448 * invalid, force enable two stage memory training
450 mem_training_ctx->enable_mem_training = true;
453 if (mem_training_ctx->enable_mem_training) {
454 ret = psp_memory_training_init(psp);
456 DRM_ERROR("Failed to initialize memory training!\n");
460 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
462 DRM_ERROR("Failed to process memory training!\n");
467 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
468 amdgpu_sriov_vf(adev) ?
469 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
471 &psp->fw_pri_mc_addr,
476 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
477 AMDGPU_GEM_DOMAIN_VRAM |
478 AMDGPU_GEM_DOMAIN_GTT,
480 &psp->fence_buf_mc_addr,
485 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
486 AMDGPU_GEM_DOMAIN_VRAM |
487 AMDGPU_GEM_DOMAIN_GTT,
488 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
489 (void **)&psp->cmd_buf_mem);
496 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
497 &psp->fence_buf_mc_addr, &psp->fence_buf);
499 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
500 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
504 static int psp_sw_fini(void *handle)
506 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
507 struct psp_context *psp = &adev->psp;
508 struct psp_gfx_cmd_resp *cmd = psp->cmd;
510 psp_memory_training_fini(psp);
512 amdgpu_ucode_release(&psp->sos_fw);
513 amdgpu_ucode_release(&psp->asd_fw);
514 amdgpu_ucode_release(&psp->ta_fw);
515 amdgpu_ucode_release(&psp->cap_fw);
516 amdgpu_ucode_release(&psp->toc_fw);
521 psp_free_shared_bufs(psp);
523 if (psp->km_ring.ring_mem)
524 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
525 &psp->km_ring.ring_mem_mc_addr,
526 (void **)&psp->km_ring.ring_mem);
528 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
529 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
530 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
531 &psp->fence_buf_mc_addr, &psp->fence_buf);
532 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
533 (void **)&psp->cmd_buf_mem);
538 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
539 uint32_t reg_val, uint32_t mask, bool check_changed)
543 struct amdgpu_device *adev = psp->adev;
545 if (psp->adev->no_hw_access)
548 for (i = 0; i < adev->usec_timeout; i++) {
549 val = RREG32(reg_index);
554 if ((val & mask) == reg_val)
563 int psp_wait_for_spirom_update(struct psp_context *psp, uint32_t reg_index,
564 uint32_t reg_val, uint32_t mask, uint32_t msec_timeout)
568 struct amdgpu_device *adev = psp->adev;
570 if (psp->adev->no_hw_access)
573 for (i = 0; i < msec_timeout; i++) {
574 val = RREG32(reg_index);
575 if ((val & mask) == reg_val)
583 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
586 case GFX_CMD_ID_LOAD_TA:
588 case GFX_CMD_ID_UNLOAD_TA:
590 case GFX_CMD_ID_INVOKE_CMD:
592 case GFX_CMD_ID_LOAD_ASD:
594 case GFX_CMD_ID_SETUP_TMR:
596 case GFX_CMD_ID_LOAD_IP_FW:
598 case GFX_CMD_ID_DESTROY_TMR:
599 return "DESTROY_TMR";
600 case GFX_CMD_ID_SAVE_RESTORE:
601 return "SAVE_RESTORE_IP_FW";
602 case GFX_CMD_ID_SETUP_VMR:
604 case GFX_CMD_ID_DESTROY_VMR:
605 return "DESTROY_VMR";
606 case GFX_CMD_ID_PROG_REG:
608 case GFX_CMD_ID_GET_FW_ATTESTATION:
609 return "GET_FW_ATTESTATION";
610 case GFX_CMD_ID_LOAD_TOC:
611 return "ID_LOAD_TOC";
612 case GFX_CMD_ID_AUTOLOAD_RLC:
613 return "AUTOLOAD_RLC";
614 case GFX_CMD_ID_BOOT_CFG:
617 return "UNKNOWN CMD";
622 psp_cmd_submit_buf(struct psp_context *psp,
623 struct amdgpu_firmware_info *ucode,
624 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
629 bool ras_intr = false;
630 bool skip_unsupport = false;
632 if (psp->adev->no_hw_access)
635 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
637 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
639 index = atomic_inc_return(&psp->fence_value);
640 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
642 atomic_dec(&psp->fence_value);
646 amdgpu_device_invalidate_hdp(psp->adev, NULL);
647 while (*((unsigned int *)psp->fence_buf) != index) {
651 * Shouldn't wait for timeout when err_event_athub occurs,
652 * because gpu reset thread triggered and lock resource should
653 * be released for psp resume sequence.
655 ras_intr = amdgpu_ras_intr_triggered();
658 usleep_range(10, 100);
659 amdgpu_device_invalidate_hdp(psp->adev, NULL);
662 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
663 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
664 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
666 memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
668 /* In some cases, psp response status is not 0 even there is no
669 * problem while the command is submitted. Some version of PSP FW
670 * doesn't write 0 to that field.
671 * So here we would like to only print a warning instead of an error
672 * during psp initialization to avoid breaking hw_init and it doesn't
675 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
677 DRM_WARN("failed to load ucode %s(0x%X) ",
678 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
679 DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
680 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
681 psp->cmd_buf_mem->resp.status);
682 /* If any firmware (including CAP) load fails under SRIOV, it should
683 * return failure to stop the VF from initializing.
684 * Also return failure in case of timeout
686 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
693 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
694 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
701 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
703 struct psp_gfx_cmd_resp *cmd = psp->cmd;
705 mutex_lock(&psp->mutex);
707 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
712 static void release_psp_cmd_buf(struct psp_context *psp)
714 mutex_unlock(&psp->mutex);
717 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
718 struct psp_gfx_cmd_resp *cmd,
719 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
721 struct amdgpu_device *adev = psp->adev;
726 size = amdgpu_bo_size(tmr_bo);
727 tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
730 if (amdgpu_sriov_vf(psp->adev))
731 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
733 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
734 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
735 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
736 cmd->cmd.cmd_setup_tmr.buf_size = size;
737 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
738 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
739 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
742 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
743 uint64_t pri_buf_mc, uint32_t size)
745 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
746 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
747 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
748 cmd->cmd.cmd_load_toc.toc_size = size;
751 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
752 static int psp_load_toc(struct psp_context *psp,
756 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
758 /* Copy toc to psp firmware private buffer */
759 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
761 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
763 ret = psp_cmd_submit_buf(psp, NULL, cmd,
764 psp->fence_buf_mc_addr);
766 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
768 release_psp_cmd_buf(psp);
773 static bool psp_boottime_tmr(struct psp_context *psp)
775 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
776 case IP_VERSION(13, 0, 6):
783 /* Set up Trusted Memory Region */
784 static int psp_tmr_init(struct psp_context *psp)
792 * According to HW engineer, they prefer the TMR address be "naturally
793 * aligned" , e.g. the start address be an integer divide of TMR size.
795 * Note: this memory need be reserved till the driver
798 tmr_size = PSP_TMR_SIZE(psp->adev);
800 /* For ASICs support RLC autoload, psp will parse the toc
801 * and calculate the total size of TMR needed
803 if (!amdgpu_sriov_vf(psp->adev) &&
804 psp->toc.start_addr &&
805 psp->toc.size_bytes &&
807 ret = psp_load_toc(psp, &tmr_size);
809 DRM_ERROR("Failed to load toc\n");
815 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
816 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
818 AMDGPU_HAS_VRAM(psp->adev) ?
819 AMDGPU_GEM_DOMAIN_VRAM :
820 AMDGPU_GEM_DOMAIN_GTT,
821 &psp->tmr_bo, &psp->tmr_mc_addr,
828 static bool psp_skip_tmr(struct psp_context *psp)
830 switch (psp->adev->ip_versions[MP0_HWIP][0]) {
831 case IP_VERSION(11, 0, 9):
832 case IP_VERSION(11, 0, 7):
833 case IP_VERSION(13, 0, 2):
834 case IP_VERSION(13, 0, 6):
835 case IP_VERSION(13, 0, 10):
842 static int psp_tmr_load(struct psp_context *psp)
845 struct psp_gfx_cmd_resp *cmd;
847 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
848 * Already set up by host driver.
850 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
853 cmd = acquire_psp_cmd_buf(psp);
855 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
857 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
858 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
860 ret = psp_cmd_submit_buf(psp, NULL, cmd,
861 psp->fence_buf_mc_addr);
863 release_psp_cmd_buf(psp);
868 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
869 struct psp_gfx_cmd_resp *cmd)
871 if (amdgpu_sriov_vf(psp->adev))
872 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
874 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
877 static int psp_tmr_unload(struct psp_context *psp)
880 struct psp_gfx_cmd_resp *cmd;
882 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
883 * as TMR is not loaded at all
885 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
888 cmd = acquire_psp_cmd_buf(psp);
890 psp_prep_tmr_unload_cmd_buf(psp, cmd);
891 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
893 ret = psp_cmd_submit_buf(psp, NULL, cmd,
894 psp->fence_buf_mc_addr);
896 release_psp_cmd_buf(psp);
901 static int psp_tmr_terminate(struct psp_context *psp)
903 return psp_tmr_unload(psp);
906 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
907 uint64_t *output_ptr)
910 struct psp_gfx_cmd_resp *cmd;
915 if (amdgpu_sriov_vf(psp->adev))
918 cmd = acquire_psp_cmd_buf(psp);
920 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
922 ret = psp_cmd_submit_buf(psp, NULL, cmd,
923 psp->fence_buf_mc_addr);
926 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
927 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
930 release_psp_cmd_buf(psp);
935 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
937 struct psp_context *psp = &adev->psp;
938 struct psp_gfx_cmd_resp *cmd;
941 if (amdgpu_sriov_vf(adev))
944 cmd = acquire_psp_cmd_buf(psp);
946 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
947 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
949 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
952 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
955 release_psp_cmd_buf(psp);
960 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
963 struct psp_context *psp = &adev->psp;
964 struct psp_gfx_cmd_resp *cmd;
966 if (amdgpu_sriov_vf(adev))
969 cmd = acquire_psp_cmd_buf(psp);
971 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
972 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
973 cmd->cmd.boot_cfg.boot_config = boot_cfg;
974 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
976 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
978 release_psp_cmd_buf(psp);
983 static int psp_rl_load(struct amdgpu_device *adev)
986 struct psp_context *psp = &adev->psp;
987 struct psp_gfx_cmd_resp *cmd;
989 if (!is_psp_fw_valid(psp->rl))
992 cmd = acquire_psp_cmd_buf(psp);
994 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
995 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
997 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
998 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
999 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
1000 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
1001 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
1003 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1005 release_psp_cmd_buf(psp);
1010 int psp_spatial_partition(struct psp_context *psp, int mode)
1012 struct psp_gfx_cmd_resp *cmd;
1015 if (amdgpu_sriov_vf(psp->adev))
1018 cmd = acquire_psp_cmd_buf(psp);
1020 cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1021 cmd->cmd.cmd_spatial_part.mode = mode;
1023 dev_info(psp->adev->dev, "Requesting %d partitions through PSP", mode);
1024 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1026 release_psp_cmd_buf(psp);
1031 static int psp_asd_initialize(struct psp_context *psp)
1035 /* If PSP version doesn't match ASD version, asd loading will be failed.
1036 * add workaround to bypass it for sriov now.
1037 * TODO: add version check to make it common
1039 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1042 psp->asd_context.mem_context.shared_mc_addr = 0;
1043 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
1044 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
1046 ret = psp_ta_load(psp, &psp->asd_context);
1048 psp->asd_context.initialized = true;
1053 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1054 uint32_t session_id)
1056 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1057 cmd->cmd.cmd_unload_ta.session_id = session_id;
1060 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1063 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1065 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1067 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1069 context->resp_status = cmd->resp.status;
1071 release_psp_cmd_buf(psp);
1076 static int psp_asd_terminate(struct psp_context *psp)
1080 if (amdgpu_sriov_vf(psp->adev))
1083 if (!psp->asd_context.initialized)
1086 ret = psp_ta_unload(psp, &psp->asd_context);
1088 psp->asd_context.initialized = false;
1093 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1094 uint32_t id, uint32_t value)
1096 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1097 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1098 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1101 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1104 struct psp_gfx_cmd_resp *cmd;
1107 if (reg >= PSP_REG_LAST)
1110 cmd = acquire_psp_cmd_buf(psp);
1112 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1113 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1115 DRM_ERROR("PSP failed to program reg id %d", reg);
1117 release_psp_cmd_buf(psp);
1122 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1124 struct ta_context *context)
1126 cmd->cmd_id = context->ta_load_type;
1127 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1128 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1129 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1131 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1132 lower_32_bits(context->mem_context.shared_mc_addr);
1133 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1134 upper_32_bits(context->mem_context.shared_mc_addr);
1135 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1138 int psp_ta_init_shared_buf(struct psp_context *psp,
1139 struct ta_mem_context *mem_ctx)
1142 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1143 * physical) for ta to host memory
1145 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1146 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1147 AMDGPU_GEM_DOMAIN_GTT,
1148 &mem_ctx->shared_bo,
1149 &mem_ctx->shared_mc_addr,
1150 &mem_ctx->shared_buf);
1153 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1155 uint32_t session_id)
1157 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1158 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1159 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1162 int psp_ta_invoke(struct psp_context *psp,
1164 struct ta_context *context)
1167 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1169 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1171 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1172 psp->fence_buf_mc_addr);
1174 context->resp_status = cmd->resp.status;
1176 release_psp_cmd_buf(psp);
1181 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1184 struct psp_gfx_cmd_resp *cmd;
1186 cmd = acquire_psp_cmd_buf(psp);
1188 psp_copy_fw(psp, context->bin_desc.start_addr,
1189 context->bin_desc.size_bytes);
1191 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1193 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1194 psp->fence_buf_mc_addr);
1196 context->resp_status = cmd->resp.status;
1199 context->session_id = cmd->resp.session_id;
1201 release_psp_cmd_buf(psp);
1206 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1208 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1211 int psp_xgmi_terminate(struct psp_context *psp)
1214 struct amdgpu_device *adev = psp->adev;
1216 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1217 if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
1218 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1219 adev->gmc.xgmi.connected_to_cpu))
1222 if (!psp->xgmi_context.context.initialized)
1225 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1227 psp->xgmi_context.context.initialized = false;
1232 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1234 struct ta_xgmi_shared_memory *xgmi_cmd;
1238 !psp->xgmi_context.context.bin_desc.size_bytes ||
1239 !psp->xgmi_context.context.bin_desc.start_addr)
1245 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1246 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1248 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1249 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1255 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1257 psp->xgmi_context.context.initialized = true;
1262 /* Initialize XGMI session */
1263 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1264 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1265 xgmi_cmd->flag_extend_link_record = set_extended_data;
1266 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1268 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1273 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1275 struct ta_xgmi_shared_memory *xgmi_cmd;
1278 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1279 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1281 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1283 /* Invoke xgmi ta to get hive id */
1284 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1288 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1293 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1295 struct ta_xgmi_shared_memory *xgmi_cmd;
1298 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1299 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1301 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1303 /* Invoke xgmi ta to get the node id */
1304 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1308 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1313 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1315 return (psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
1316 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
1317 psp->adev->ip_versions[MP0_HWIP][0] >= IP_VERSION(13, 0, 6);
1321 * Chips that support extended topology information require the driver to
1322 * reflect topology information in the opposite direction. This is
1323 * because the TA has already exceeded its link record limit and if the
1324 * TA holds bi-directional information, the driver would have to do
1325 * multiple fetches instead of just two.
1327 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1328 struct psp_xgmi_node_info node_info)
1330 struct amdgpu_device *mirror_adev;
1331 struct amdgpu_hive_info *hive;
1332 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1333 uint64_t dst_node_id = node_info.node_id;
1334 uint8_t dst_num_hops = node_info.num_hops;
1335 uint8_t dst_num_links = node_info.num_links;
1337 hive = amdgpu_get_xgmi_hive(psp->adev);
1338 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1339 struct psp_xgmi_topology_info *mirror_top_info;
1342 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1345 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1346 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1347 if (mirror_top_info->nodes[j].node_id != src_node_id)
1350 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1352 * prevent 0 num_links value re-reflection since reflection
1353 * criteria is based on num_hops (direct or indirect).
1357 mirror_top_info->nodes[j].num_links = dst_num_links;
1365 amdgpu_put_xgmi_hive(hive);
1368 int psp_xgmi_get_topology_info(struct psp_context *psp,
1370 struct psp_xgmi_topology_info *topology,
1371 bool get_extended_data)
1373 struct ta_xgmi_shared_memory *xgmi_cmd;
1374 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1375 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1379 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1382 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1383 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1384 xgmi_cmd->flag_extend_link_record = get_extended_data;
1386 /* Fill in the shared memory with topology information as input */
1387 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1388 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
1389 topology_info_input->num_nodes = number_devices;
1391 for (i = 0; i < topology_info_input->num_nodes; i++) {
1392 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1393 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1394 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1395 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1398 /* Invoke xgmi ta to get the topology information */
1399 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
1403 /* Read the output topology information from the shared memory */
1404 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1405 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1406 for (i = 0; i < topology->num_nodes; i++) {
1407 /* extended data will either be 0 or equal to non-extended data */
1408 if (topology_info_output->nodes[i].num_hops)
1409 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1411 /* non-extended data gets everything here so no need to update */
1412 if (!get_extended_data) {
1413 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1414 topology->nodes[i].is_sharing_enabled =
1415 topology_info_output->nodes[i].is_sharing_enabled;
1416 topology->nodes[i].sdma_engine =
1417 topology_info_output->nodes[i].sdma_engine;
1422 /* Invoke xgmi ta again to get the link information */
1423 if (psp_xgmi_peer_link_info_supported(psp)) {
1424 struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
1425 bool requires_reflection =
1426 (psp->xgmi_context.supports_extended_data && get_extended_data) ||
1427 psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6);
1429 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1431 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1436 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1437 for (i = 0; i < topology->num_nodes; i++) {
1438 /* accumulate num_links on extended data */
1439 topology->nodes[i].num_links = get_extended_data ?
1440 topology->nodes[i].num_links +
1441 link_info_output->nodes[i].num_links :
1442 ((requires_reflection && topology->nodes[i].num_links) ? topology->nodes[i].num_links :
1443 link_info_output->nodes[i].num_links);
1445 /* reflect the topology information for bi-directionality */
1446 if (requires_reflection && topology->nodes[i].num_hops)
1447 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1454 int psp_xgmi_set_topology_info(struct psp_context *psp,
1456 struct psp_xgmi_topology_info *topology)
1458 struct ta_xgmi_shared_memory *xgmi_cmd;
1459 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1462 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1465 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1466 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1468 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1469 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1470 topology_info_input->num_nodes = number_devices;
1472 for (i = 0; i < topology_info_input->num_nodes; i++) {
1473 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1474 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1475 topology_info_input->nodes[i].is_sharing_enabled = 1;
1476 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1479 /* Invoke xgmi ta to set topology information */
1480 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1484 static void psp_ras_ta_check_status(struct psp_context *psp)
1486 struct ta_ras_shared_memory *ras_cmd =
1487 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1489 switch (ras_cmd->ras_status) {
1490 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1491 dev_warn(psp->adev->dev,
1492 "RAS WARNING: cmd failed due to unsupported ip\n");
1494 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1495 dev_warn(psp->adev->dev,
1496 "RAS WARNING: cmd failed due to unsupported error injection\n");
1498 case TA_RAS_STATUS__SUCCESS:
1500 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1501 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1502 dev_warn(psp->adev->dev,
1503 "RAS WARNING: Inject error to critical region is not allowed\n");
1506 dev_warn(psp->adev->dev,
1507 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1512 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1514 struct ta_ras_shared_memory *ras_cmd;
1517 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1520 * TODO: bypass the loading in sriov for now
1522 if (amdgpu_sriov_vf(psp->adev))
1525 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1527 if (amdgpu_ras_intr_triggered())
1530 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1531 DRM_WARN("RAS: Unsupported Interface");
1536 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1537 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1539 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1540 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1541 dev_warn(psp->adev->dev,
1542 "RAS internal register access blocked\n");
1544 psp_ras_ta_check_status(psp);
1550 int psp_ras_enable_features(struct psp_context *psp,
1551 union ta_ras_cmd_input *info, bool enable)
1553 struct ta_ras_shared_memory *ras_cmd;
1556 if (!psp->ras_context.context.initialized)
1559 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1560 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1563 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
1565 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
1567 ras_cmd->ras_in_message = *info;
1569 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1576 int psp_ras_terminate(struct psp_context *psp)
1581 * TODO: bypass the terminate in sriov for now
1583 if (amdgpu_sriov_vf(psp->adev))
1586 if (!psp->ras_context.context.initialized)
1589 ret = psp_ta_unload(psp, &psp->ras_context.context);
1591 psp->ras_context.context.initialized = false;
1596 int psp_ras_initialize(struct psp_context *psp)
1599 uint32_t boot_cfg = 0xFF;
1600 struct amdgpu_device *adev = psp->adev;
1601 struct ta_ras_shared_memory *ras_cmd;
1604 * TODO: bypass the initialize in sriov for now
1606 if (amdgpu_sriov_vf(adev))
1609 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1610 !adev->psp.ras_context.context.bin_desc.start_addr) {
1611 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1615 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1616 /* query GECC enablement status from boot config
1617 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1619 ret = psp_boot_config_get(adev, &boot_cfg);
1621 dev_warn(adev->dev, "PSP get boot config failed\n");
1623 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1625 dev_info(adev->dev, "GECC is disabled\n");
1627 /* disable GECC in next boot cycle if ras is
1628 * disabled by module parameter amdgpu_ras_enable
1629 * and/or amdgpu_ras_mask, or boot_config_get call
1632 ret = psp_boot_config_set(adev, 0);
1634 dev_warn(adev->dev, "PSP set boot config failed\n");
1636 dev_warn(adev->dev, "GECC will be disabled in next boot cycle if set amdgpu_ras_enable and/or amdgpu_ras_mask to 0x0\n");
1639 if (boot_cfg == 1) {
1640 dev_info(adev->dev, "GECC is enabled\n");
1642 /* enable GECC in next boot cycle if it is disabled
1643 * in boot config, or force enable GECC if failed to
1644 * get boot configuration
1646 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1648 dev_warn(adev->dev, "PSP set boot config failed\n");
1650 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1655 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1656 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1658 if (!psp->ras_context.context.mem_context.shared_buf) {
1659 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1664 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1665 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1667 if (amdgpu_ras_is_poison_mode_supported(adev))
1668 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1669 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu)
1670 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1671 ras_cmd->ras_in_message.init_flags.xcc_mask =
1673 ras_cmd->ras_in_message.init_flags.channel_dis_num = hweight32(adev->gmc.m_half_use) * 2;
1675 ret = psp_ta_load(psp, &psp->ras_context.context);
1677 if (!ret && !ras_cmd->ras_status)
1678 psp->ras_context.context.initialized = true;
1680 if (ras_cmd->ras_status)
1681 dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1683 /* fail to load RAS TA */
1684 psp->ras_context.context.initialized = false;
1690 int psp_ras_trigger_error(struct psp_context *psp,
1691 struct ta_ras_trigger_error_input *info, uint32_t instance_mask)
1693 struct ta_ras_shared_memory *ras_cmd;
1694 struct amdgpu_device *adev = psp->adev;
1698 if (!psp->ras_context.context.initialized)
1701 switch (info->block_id) {
1702 case TA_RAS_BLOCK__GFX:
1703 dev_mask = GET_MASK(GC, instance_mask);
1705 case TA_RAS_BLOCK__SDMA:
1706 dev_mask = GET_MASK(SDMA0, instance_mask);
1708 case TA_RAS_BLOCK__VCN:
1709 case TA_RAS_BLOCK__JPEG:
1710 dev_mask = GET_MASK(VCN, instance_mask);
1713 dev_mask = instance_mask;
1717 /* reuse sub_block_index for backward compatibility */
1718 dev_mask <<= AMDGPU_RAS_INST_SHIFT;
1719 dev_mask &= AMDGPU_RAS_INST_MASK;
1720 info->sub_block_index |= dev_mask;
1722 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1723 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1725 ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1726 ras_cmd->ras_in_message.trigger_error = *info;
1728 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1732 /* If err_event_athub occurs error inject was successful, however
1733 * return status from TA is no long reliable
1735 if (amdgpu_ras_intr_triggered())
1738 if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1740 else if (ras_cmd->ras_status)
1748 static int psp_hdcp_initialize(struct psp_context *psp)
1753 * TODO: bypass the initialize in sriov for now
1755 if (amdgpu_sriov_vf(psp->adev))
1758 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1759 !psp->hdcp_context.context.bin_desc.start_addr) {
1760 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1764 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1765 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1767 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1768 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1773 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1775 psp->hdcp_context.context.initialized = true;
1776 mutex_init(&psp->hdcp_context.mutex);
1782 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1785 * TODO: bypass the loading in sriov for now
1787 if (amdgpu_sriov_vf(psp->adev))
1790 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1793 static int psp_hdcp_terminate(struct psp_context *psp)
1798 * TODO: bypass the terminate in sriov for now
1800 if (amdgpu_sriov_vf(psp->adev))
1803 if (!psp->hdcp_context.context.initialized)
1806 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1808 psp->hdcp_context.context.initialized = false;
1815 static int psp_dtm_initialize(struct psp_context *psp)
1820 * TODO: bypass the initialize in sriov for now
1822 if (amdgpu_sriov_vf(psp->adev))
1825 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1826 !psp->dtm_context.context.bin_desc.start_addr) {
1827 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1831 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1832 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1834 if (!psp->dtm_context.context.mem_context.shared_buf) {
1835 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1840 ret = psp_ta_load(psp, &psp->dtm_context.context);
1842 psp->dtm_context.context.initialized = true;
1843 mutex_init(&psp->dtm_context.mutex);
1849 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1852 * TODO: bypass the loading in sriov for now
1854 if (amdgpu_sriov_vf(psp->adev))
1857 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1860 static int psp_dtm_terminate(struct psp_context *psp)
1865 * TODO: bypass the terminate in sriov for now
1867 if (amdgpu_sriov_vf(psp->adev))
1870 if (!psp->dtm_context.context.initialized)
1873 ret = psp_ta_unload(psp, &psp->dtm_context.context);
1875 psp->dtm_context.context.initialized = false;
1882 static int psp_rap_initialize(struct psp_context *psp)
1885 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
1888 * TODO: bypass the initialize in sriov for now
1890 if (amdgpu_sriov_vf(psp->adev))
1893 if (!psp->rap_context.context.bin_desc.size_bytes ||
1894 !psp->rap_context.context.bin_desc.start_addr) {
1895 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
1899 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
1900 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1902 if (!psp->rap_context.context.mem_context.shared_buf) {
1903 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
1908 ret = psp_ta_load(psp, &psp->rap_context.context);
1910 psp->rap_context.context.initialized = true;
1911 mutex_init(&psp->rap_context.mutex);
1915 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
1916 if (ret || status != TA_RAP_STATUS__SUCCESS) {
1917 psp_rap_terminate(psp);
1918 /* free rap shared memory */
1919 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
1921 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
1930 static int psp_rap_terminate(struct psp_context *psp)
1934 if (!psp->rap_context.context.initialized)
1937 ret = psp_ta_unload(psp, &psp->rap_context.context);
1939 psp->rap_context.context.initialized = false;
1944 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
1946 struct ta_rap_shared_memory *rap_cmd;
1949 if (!psp->rap_context.context.initialized)
1952 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
1953 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
1956 mutex_lock(&psp->rap_context.mutex);
1958 rap_cmd = (struct ta_rap_shared_memory *)
1959 psp->rap_context.context.mem_context.shared_buf;
1960 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
1962 rap_cmd->cmd_id = ta_cmd_id;
1963 rap_cmd->validation_method_id = METHOD_A;
1965 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
1970 *status = rap_cmd->rap_status;
1973 mutex_unlock(&psp->rap_context.mutex);
1979 /* securedisplay start */
1980 static int psp_securedisplay_initialize(struct psp_context *psp)
1983 struct ta_securedisplay_cmd *securedisplay_cmd;
1986 * TODO: bypass the initialize in sriov for now
1988 if (amdgpu_sriov_vf(psp->adev))
1991 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1992 !psp->securedisplay_context.context.bin_desc.start_addr) {
1993 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1997 psp->securedisplay_context.context.mem_context.shared_mem_size =
1998 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
1999 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
2001 if (!psp->securedisplay_context.context.initialized) {
2002 ret = psp_ta_init_shared_buf(psp,
2003 &psp->securedisplay_context.context.mem_context);
2008 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
2010 psp->securedisplay_context.context.initialized = true;
2011 mutex_init(&psp->securedisplay_context.mutex);
2015 mutex_lock(&psp->securedisplay_context.mutex);
2017 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
2018 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2020 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2022 mutex_unlock(&psp->securedisplay_context.mutex);
2025 psp_securedisplay_terminate(psp);
2026 /* free securedisplay shared memory */
2027 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
2028 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
2032 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
2033 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
2034 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
2035 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
2036 /* don't try again */
2037 psp->securedisplay_context.context.bin_desc.size_bytes = 0;
2043 static int psp_securedisplay_terminate(struct psp_context *psp)
2048 * TODO:bypass the terminate in sriov for now
2050 if (amdgpu_sriov_vf(psp->adev))
2053 if (!psp->securedisplay_context.context.initialized)
2056 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2058 psp->securedisplay_context.context.initialized = false;
2063 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2067 if (!psp->securedisplay_context.context.initialized)
2070 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2071 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2074 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2078 /* SECUREDISPLAY end */
2080 static int psp_hw_start(struct psp_context *psp)
2082 struct amdgpu_device *adev = psp->adev;
2085 if (!amdgpu_sriov_vf(adev)) {
2086 if ((is_psp_fw_valid(psp->kdb)) &&
2087 (psp->funcs->bootloader_load_kdb != NULL)) {
2088 ret = psp_bootloader_load_kdb(psp);
2090 DRM_ERROR("PSP load kdb failed!\n");
2095 if ((is_psp_fw_valid(psp->spl)) &&
2096 (psp->funcs->bootloader_load_spl != NULL)) {
2097 ret = psp_bootloader_load_spl(psp);
2099 DRM_ERROR("PSP load spl failed!\n");
2104 if ((is_psp_fw_valid(psp->sys)) &&
2105 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2106 ret = psp_bootloader_load_sysdrv(psp);
2108 DRM_ERROR("PSP load sys drv failed!\n");
2113 if ((is_psp_fw_valid(psp->soc_drv)) &&
2114 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2115 ret = psp_bootloader_load_soc_drv(psp);
2117 DRM_ERROR("PSP load soc drv failed!\n");
2122 if ((is_psp_fw_valid(psp->intf_drv)) &&
2123 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2124 ret = psp_bootloader_load_intf_drv(psp);
2126 DRM_ERROR("PSP load intf drv failed!\n");
2131 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2132 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2133 ret = psp_bootloader_load_dbg_drv(psp);
2135 DRM_ERROR("PSP load dbg drv failed!\n");
2140 if ((is_psp_fw_valid(psp->ras_drv)) &&
2141 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2142 ret = psp_bootloader_load_ras_drv(psp);
2144 DRM_ERROR("PSP load ras_drv failed!\n");
2149 if ((is_psp_fw_valid(psp->sos)) &&
2150 (psp->funcs->bootloader_load_sos != NULL)) {
2151 ret = psp_bootloader_load_sos(psp);
2153 DRM_ERROR("PSP load sos failed!\n");
2159 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2161 DRM_ERROR("PSP create ring failed!\n");
2165 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2168 if (!psp_boottime_tmr(psp)) {
2169 ret = psp_tmr_init(psp);
2171 DRM_ERROR("PSP tmr init failed!\n");
2178 * For ASICs with DF Cstate management centralized
2179 * to PMFW, TMR setup should be performed after PMFW
2180 * loaded and before other non-psp firmware loaded.
2182 if (psp->pmfw_centralized_cstate_management) {
2183 ret = psp_load_smu_fw(psp);
2188 ret = psp_tmr_load(psp);
2190 DRM_ERROR("PSP load tmr failed!\n");
2197 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2198 enum psp_gfx_fw_type *type)
2200 switch (ucode->ucode_id) {
2201 case AMDGPU_UCODE_ID_CAP:
2202 *type = GFX_FW_TYPE_CAP;
2204 case AMDGPU_UCODE_ID_SDMA0:
2205 *type = GFX_FW_TYPE_SDMA0;
2207 case AMDGPU_UCODE_ID_SDMA1:
2208 *type = GFX_FW_TYPE_SDMA1;
2210 case AMDGPU_UCODE_ID_SDMA2:
2211 *type = GFX_FW_TYPE_SDMA2;
2213 case AMDGPU_UCODE_ID_SDMA3:
2214 *type = GFX_FW_TYPE_SDMA3;
2216 case AMDGPU_UCODE_ID_SDMA4:
2217 *type = GFX_FW_TYPE_SDMA4;
2219 case AMDGPU_UCODE_ID_SDMA5:
2220 *type = GFX_FW_TYPE_SDMA5;
2222 case AMDGPU_UCODE_ID_SDMA6:
2223 *type = GFX_FW_TYPE_SDMA6;
2225 case AMDGPU_UCODE_ID_SDMA7:
2226 *type = GFX_FW_TYPE_SDMA7;
2228 case AMDGPU_UCODE_ID_CP_MES:
2229 *type = GFX_FW_TYPE_CP_MES;
2231 case AMDGPU_UCODE_ID_CP_MES_DATA:
2232 *type = GFX_FW_TYPE_MES_STACK;
2234 case AMDGPU_UCODE_ID_CP_MES1:
2235 *type = GFX_FW_TYPE_CP_MES_KIQ;
2237 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2238 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2240 case AMDGPU_UCODE_ID_CP_CE:
2241 *type = GFX_FW_TYPE_CP_CE;
2243 case AMDGPU_UCODE_ID_CP_PFP:
2244 *type = GFX_FW_TYPE_CP_PFP;
2246 case AMDGPU_UCODE_ID_CP_ME:
2247 *type = GFX_FW_TYPE_CP_ME;
2249 case AMDGPU_UCODE_ID_CP_MEC1:
2250 *type = GFX_FW_TYPE_CP_MEC;
2252 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2253 *type = GFX_FW_TYPE_CP_MEC_ME1;
2255 case AMDGPU_UCODE_ID_CP_MEC2:
2256 *type = GFX_FW_TYPE_CP_MEC;
2258 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2259 *type = GFX_FW_TYPE_CP_MEC_ME2;
2261 case AMDGPU_UCODE_ID_RLC_P:
2262 *type = GFX_FW_TYPE_RLC_P;
2264 case AMDGPU_UCODE_ID_RLC_V:
2265 *type = GFX_FW_TYPE_RLC_V;
2267 case AMDGPU_UCODE_ID_RLC_G:
2268 *type = GFX_FW_TYPE_RLC_G;
2270 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2271 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2273 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2274 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2276 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2277 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2279 case AMDGPU_UCODE_ID_RLC_IRAM:
2280 *type = GFX_FW_TYPE_RLC_IRAM;
2282 case AMDGPU_UCODE_ID_RLC_DRAM:
2283 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2285 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2286 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2288 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2289 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2291 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2292 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2294 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2295 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2297 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2298 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2300 case AMDGPU_UCODE_ID_SMC:
2301 *type = GFX_FW_TYPE_SMU;
2303 case AMDGPU_UCODE_ID_PPTABLE:
2304 *type = GFX_FW_TYPE_PPTABLE;
2306 case AMDGPU_UCODE_ID_UVD:
2307 *type = GFX_FW_TYPE_UVD;
2309 case AMDGPU_UCODE_ID_UVD1:
2310 *type = GFX_FW_TYPE_UVD1;
2312 case AMDGPU_UCODE_ID_VCE:
2313 *type = GFX_FW_TYPE_VCE;
2315 case AMDGPU_UCODE_ID_VCN:
2316 *type = GFX_FW_TYPE_VCN;
2318 case AMDGPU_UCODE_ID_VCN1:
2319 *type = GFX_FW_TYPE_VCN1;
2321 case AMDGPU_UCODE_ID_DMCU_ERAM:
2322 *type = GFX_FW_TYPE_DMCU_ERAM;
2324 case AMDGPU_UCODE_ID_DMCU_INTV:
2325 *type = GFX_FW_TYPE_DMCU_ISR;
2327 case AMDGPU_UCODE_ID_VCN0_RAM:
2328 *type = GFX_FW_TYPE_VCN0_RAM;
2330 case AMDGPU_UCODE_ID_VCN1_RAM:
2331 *type = GFX_FW_TYPE_VCN1_RAM;
2333 case AMDGPU_UCODE_ID_DMCUB:
2334 *type = GFX_FW_TYPE_DMUB;
2336 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2337 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2339 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2340 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2342 case AMDGPU_UCODE_ID_IMU_I:
2343 *type = GFX_FW_TYPE_IMU_I;
2345 case AMDGPU_UCODE_ID_IMU_D:
2346 *type = GFX_FW_TYPE_IMU_D;
2348 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2349 *type = GFX_FW_TYPE_RS64_PFP;
2351 case AMDGPU_UCODE_ID_CP_RS64_ME:
2352 *type = GFX_FW_TYPE_RS64_ME;
2354 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2355 *type = GFX_FW_TYPE_RS64_MEC;
2357 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2358 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2360 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2361 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2363 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2364 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2366 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2367 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2369 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2370 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2372 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2373 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2375 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2376 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2378 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2379 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2381 case AMDGPU_UCODE_ID_MAXIMUM:
2389 static void psp_print_fw_hdr(struct psp_context *psp,
2390 struct amdgpu_firmware_info *ucode)
2392 struct amdgpu_device *adev = psp->adev;
2393 struct common_firmware_header *hdr;
2395 switch (ucode->ucode_id) {
2396 case AMDGPU_UCODE_ID_SDMA0:
2397 case AMDGPU_UCODE_ID_SDMA1:
2398 case AMDGPU_UCODE_ID_SDMA2:
2399 case AMDGPU_UCODE_ID_SDMA3:
2400 case AMDGPU_UCODE_ID_SDMA4:
2401 case AMDGPU_UCODE_ID_SDMA5:
2402 case AMDGPU_UCODE_ID_SDMA6:
2403 case AMDGPU_UCODE_ID_SDMA7:
2404 hdr = (struct common_firmware_header *)
2405 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2406 amdgpu_ucode_print_sdma_hdr(hdr);
2408 case AMDGPU_UCODE_ID_CP_CE:
2409 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2410 amdgpu_ucode_print_gfx_hdr(hdr);
2412 case AMDGPU_UCODE_ID_CP_PFP:
2413 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2414 amdgpu_ucode_print_gfx_hdr(hdr);
2416 case AMDGPU_UCODE_ID_CP_ME:
2417 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2418 amdgpu_ucode_print_gfx_hdr(hdr);
2420 case AMDGPU_UCODE_ID_CP_MEC1:
2421 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2422 amdgpu_ucode_print_gfx_hdr(hdr);
2424 case AMDGPU_UCODE_ID_RLC_G:
2425 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2426 amdgpu_ucode_print_rlc_hdr(hdr);
2428 case AMDGPU_UCODE_ID_SMC:
2429 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2430 amdgpu_ucode_print_smc_hdr(hdr);
2437 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2438 struct psp_gfx_cmd_resp *cmd)
2441 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2443 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2444 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2445 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2446 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2448 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2450 DRM_ERROR("Unknown firmware type\n");
2455 int psp_execute_ip_fw_load(struct psp_context *psp,
2456 struct amdgpu_firmware_info *ucode)
2459 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2461 ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
2463 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2464 psp->fence_buf_mc_addr);
2467 release_psp_cmd_buf(psp);
2472 static int psp_load_smu_fw(struct psp_context *psp)
2475 struct amdgpu_device *adev = psp->adev;
2476 struct amdgpu_firmware_info *ucode =
2477 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2478 struct amdgpu_ras *ras = psp->ras_context.ras;
2481 * Skip SMU FW reloading in case of using BACO for runpm only,
2482 * as SMU is always alive.
2484 if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2487 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2490 if ((amdgpu_in_reset(adev) &&
2491 ras && adev->ras_enabled &&
2492 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
2493 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
2494 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2496 DRM_WARN("Failed to set MP1 state prepare for reload\n");
2499 ret = psp_execute_ip_fw_load(psp, ucode);
2502 DRM_ERROR("PSP load smu failed!\n");
2507 static bool fw_load_skip_check(struct psp_context *psp,
2508 struct amdgpu_firmware_info *ucode)
2510 if (!ucode->fw || !ucode->ucode_size)
2513 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2514 (psp_smu_reload_quirk(psp) ||
2515 psp->autoload_supported ||
2516 psp->pmfw_centralized_cstate_management))
2519 if (amdgpu_sriov_vf(psp->adev) &&
2520 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2523 if (psp->autoload_supported &&
2524 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2525 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2526 /* skip mec JT when autoload is enabled */
2532 int psp_load_fw_list(struct psp_context *psp,
2533 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2536 struct amdgpu_firmware_info *ucode;
2538 for (i = 0; i < ucode_count; ++i) {
2539 ucode = ucode_list[i];
2540 psp_print_fw_hdr(psp, ucode);
2541 ret = psp_execute_ip_fw_load(psp, ucode);
2548 static int psp_load_non_psp_fw(struct psp_context *psp)
2551 struct amdgpu_firmware_info *ucode;
2552 struct amdgpu_device *adev = psp->adev;
2554 if (psp->autoload_supported &&
2555 !psp->pmfw_centralized_cstate_management) {
2556 ret = psp_load_smu_fw(psp);
2561 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2562 ucode = &adev->firmware.ucode[i];
2564 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2565 !fw_load_skip_check(psp, ucode)) {
2566 ret = psp_load_smu_fw(psp);
2572 if (fw_load_skip_check(psp, ucode))
2575 if (psp->autoload_supported &&
2576 (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
2577 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
2578 adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2579 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2580 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2581 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2582 /* PSP only receive one SDMA fw for sienna_cichlid,
2583 * as all four sdma fw are same
2587 psp_print_fw_hdr(psp, ucode);
2589 ret = psp_execute_ip_fw_load(psp, ucode);
2593 /* Start rlc autoload after psp recieved all the gfx firmware */
2594 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2595 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2596 ret = psp_rlc_autoload_start(psp);
2598 DRM_ERROR("Failed to start rlc autoload\n");
2607 static int psp_load_fw(struct amdgpu_device *adev)
2610 struct psp_context *psp = &adev->psp;
2612 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2613 /* should not destroy ring, only stop */
2614 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2616 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2618 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2620 DRM_ERROR("PSP ring init failed!\n");
2625 ret = psp_hw_start(psp);
2629 ret = psp_load_non_psp_fw(psp);
2633 ret = psp_asd_initialize(psp);
2635 DRM_ERROR("PSP load asd failed!\n");
2639 ret = psp_rl_load(adev);
2641 DRM_ERROR("PSP load RL failed!\n");
2645 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2646 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2647 ret = psp_xgmi_initialize(psp, false, true);
2648 /* Warning the XGMI seesion initialize failure
2649 * Instead of stop driver initialization
2652 dev_err(psp->adev->dev,
2653 "XGMI: Failed to initialize XGMI session\n");
2658 ret = psp_ras_initialize(psp);
2660 dev_err(psp->adev->dev,
2661 "RAS: Failed to initialize RAS\n");
2663 ret = psp_hdcp_initialize(psp);
2665 dev_err(psp->adev->dev,
2666 "HDCP: Failed to initialize HDCP\n");
2668 ret = psp_dtm_initialize(psp);
2670 dev_err(psp->adev->dev,
2671 "DTM: Failed to initialize DTM\n");
2673 ret = psp_rap_initialize(psp);
2675 dev_err(psp->adev->dev,
2676 "RAP: Failed to initialize RAP\n");
2678 ret = psp_securedisplay_initialize(psp);
2680 dev_err(psp->adev->dev,
2681 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2687 psp_free_shared_bufs(psp);
2690 * all cleanup jobs (xgmi terminate, ras terminate,
2691 * ring destroy, cmd/fence/fw buffers destory,
2692 * psp->cmd destory) are delayed to psp_hw_fini
2694 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2698 static int psp_hw_init(void *handle)
2701 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2703 mutex_lock(&adev->firmware.mutex);
2705 * This sequence is just used on hw_init only once, no need on
2708 ret = amdgpu_ucode_init_bo(adev);
2712 ret = psp_load_fw(adev);
2714 DRM_ERROR("PSP firmware loading failed\n");
2718 mutex_unlock(&adev->firmware.mutex);
2722 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2723 mutex_unlock(&adev->firmware.mutex);
2727 static int psp_hw_fini(void *handle)
2729 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2730 struct psp_context *psp = &adev->psp;
2733 psp_ras_terminate(psp);
2734 psp_securedisplay_terminate(psp);
2735 psp_rap_terminate(psp);
2736 psp_dtm_terminate(psp);
2737 psp_hdcp_terminate(psp);
2739 if (adev->gmc.xgmi.num_physical_nodes > 1)
2740 psp_xgmi_terminate(psp);
2743 psp_asd_terminate(psp);
2744 psp_tmr_terminate(psp);
2746 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2751 static int psp_suspend(void *handle)
2754 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2755 struct psp_context *psp = &adev->psp;
2757 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2758 psp->xgmi_context.context.initialized) {
2759 ret = psp_xgmi_terminate(psp);
2761 DRM_ERROR("Failed to terminate xgmi ta\n");
2767 ret = psp_ras_terminate(psp);
2769 DRM_ERROR("Failed to terminate ras ta\n");
2772 ret = psp_hdcp_terminate(psp);
2774 DRM_ERROR("Failed to terminate hdcp ta\n");
2777 ret = psp_dtm_terminate(psp);
2779 DRM_ERROR("Failed to terminate dtm ta\n");
2782 ret = psp_rap_terminate(psp);
2784 DRM_ERROR("Failed to terminate rap ta\n");
2787 ret = psp_securedisplay_terminate(psp);
2789 DRM_ERROR("Failed to terminate securedisplay ta\n");
2794 ret = psp_asd_terminate(psp);
2796 DRM_ERROR("Failed to terminate asd\n");
2800 ret = psp_tmr_terminate(psp);
2802 DRM_ERROR("Failed to terminate tmr\n");
2806 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2808 DRM_ERROR("PSP ring stop failed\n");
2814 static int psp_resume(void *handle)
2817 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2818 struct psp_context *psp = &adev->psp;
2820 DRM_INFO("PSP is resuming...\n");
2822 if (psp->mem_train_ctx.enable_mem_training) {
2823 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
2825 DRM_ERROR("Failed to process memory training!\n");
2830 mutex_lock(&adev->firmware.mutex);
2832 ret = psp_hw_start(psp);
2836 ret = psp_load_non_psp_fw(psp);
2840 ret = psp_asd_initialize(psp);
2842 DRM_ERROR("PSP load asd failed!\n");
2846 ret = psp_rl_load(adev);
2848 dev_err(adev->dev, "PSP load RL failed!\n");
2852 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2853 ret = psp_xgmi_initialize(psp, false, true);
2854 /* Warning the XGMI seesion initialize failure
2855 * Instead of stop driver initialization
2858 dev_err(psp->adev->dev,
2859 "XGMI: Failed to initialize XGMI session\n");
2863 ret = psp_ras_initialize(psp);
2865 dev_err(psp->adev->dev,
2866 "RAS: Failed to initialize RAS\n");
2868 ret = psp_hdcp_initialize(psp);
2870 dev_err(psp->adev->dev,
2871 "HDCP: Failed to initialize HDCP\n");
2873 ret = psp_dtm_initialize(psp);
2875 dev_err(psp->adev->dev,
2876 "DTM: Failed to initialize DTM\n");
2878 ret = psp_rap_initialize(psp);
2880 dev_err(psp->adev->dev,
2881 "RAP: Failed to initialize RAP\n");
2883 ret = psp_securedisplay_initialize(psp);
2885 dev_err(psp->adev->dev,
2886 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2889 mutex_unlock(&adev->firmware.mutex);
2894 DRM_ERROR("PSP resume failed\n");
2895 mutex_unlock(&adev->firmware.mutex);
2899 int psp_gpu_reset(struct amdgpu_device *adev)
2903 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
2906 mutex_lock(&adev->psp.mutex);
2907 ret = psp_mode1_reset(&adev->psp);
2908 mutex_unlock(&adev->psp.mutex);
2913 int psp_rlc_autoload_start(struct psp_context *psp)
2916 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2918 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
2920 ret = psp_cmd_submit_buf(psp, NULL, cmd,
2921 psp->fence_buf_mc_addr);
2923 release_psp_cmd_buf(psp);
2928 int psp_ring_cmd_submit(struct psp_context *psp,
2929 uint64_t cmd_buf_mc_addr,
2930 uint64_t fence_mc_addr,
2933 unsigned int psp_write_ptr_reg = 0;
2934 struct psp_gfx_rb_frame *write_frame;
2935 struct psp_ring *ring = &psp->km_ring;
2936 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2937 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2938 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2939 struct amdgpu_device *adev = psp->adev;
2940 uint32_t ring_size_dw = ring->ring_size / 4;
2941 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2943 /* KM (GPCOM) prepare write pointer */
2944 psp_write_ptr_reg = psp_ring_get_wptr(psp);
2946 /* Update KM RB frame pointer to new frame */
2947 /* write_frame ptr increments by size of rb_frame in bytes */
2948 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2949 if ((psp_write_ptr_reg % ring_size_dw) == 0)
2950 write_frame = ring_buffer_start;
2952 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2953 /* Check invalid write_frame ptr address */
2954 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2955 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2956 ring_buffer_start, ring_buffer_end, write_frame);
2957 DRM_ERROR("write_frame is pointing to address out of bounds\n");
2961 /* Initialize KM RB frame */
2962 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2964 /* Update KM RB frame */
2965 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2966 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2967 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2968 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2969 write_frame->fence_value = index;
2970 amdgpu_device_flush_hdp(adev, NULL);
2972 /* Update the write Pointer in DWORDs */
2973 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2974 psp_ring_set_wptr(psp, psp_write_ptr_reg);
2978 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2980 struct amdgpu_device *adev = psp->adev;
2981 char fw_name[PSP_FW_NAME_LEN];
2982 const struct psp_firmware_header_v1_0 *asd_hdr;
2985 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
2986 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
2990 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
2991 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
2992 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
2993 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
2994 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
2995 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
2998 amdgpu_ucode_release(&adev->psp.asd_fw);
3002 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
3004 struct amdgpu_device *adev = psp->adev;
3005 char fw_name[PSP_FW_NAME_LEN];
3006 const struct psp_firmware_header_v1_0 *toc_hdr;
3009 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
3010 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
3014 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
3015 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
3016 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
3017 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
3018 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
3019 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
3022 amdgpu_ucode_release(&adev->psp.toc_fw);
3026 static int parse_sos_bin_descriptor(struct psp_context *psp,
3027 const struct psp_fw_bin_desc *desc,
3028 const struct psp_firmware_header_v2_0 *sos_hdr)
3030 uint8_t *ucode_start_addr = NULL;
3032 if (!psp || !desc || !sos_hdr)
3035 ucode_start_addr = (uint8_t *)sos_hdr +
3036 le32_to_cpu(desc->offset_bytes) +
3037 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3039 switch (desc->fw_type) {
3040 case PSP_FW_TYPE_PSP_SOS:
3041 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3042 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3043 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3044 psp->sos.start_addr = ucode_start_addr;
3046 case PSP_FW_TYPE_PSP_SYS_DRV:
3047 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3048 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3049 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3050 psp->sys.start_addr = ucode_start_addr;
3052 case PSP_FW_TYPE_PSP_KDB:
3053 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3054 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3055 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3056 psp->kdb.start_addr = ucode_start_addr;
3058 case PSP_FW_TYPE_PSP_TOC:
3059 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3060 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3061 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3062 psp->toc.start_addr = ucode_start_addr;
3064 case PSP_FW_TYPE_PSP_SPL:
3065 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3066 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3067 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3068 psp->spl.start_addr = ucode_start_addr;
3070 case PSP_FW_TYPE_PSP_RL:
3071 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3072 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3073 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3074 psp->rl.start_addr = ucode_start_addr;
3076 case PSP_FW_TYPE_PSP_SOC_DRV:
3077 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3078 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3079 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3080 psp->soc_drv.start_addr = ucode_start_addr;
3082 case PSP_FW_TYPE_PSP_INTF_DRV:
3083 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3084 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3085 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3086 psp->intf_drv.start_addr = ucode_start_addr;
3088 case PSP_FW_TYPE_PSP_DBG_DRV:
3089 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3090 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3091 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3092 psp->dbg_drv.start_addr = ucode_start_addr;
3094 case PSP_FW_TYPE_PSP_RAS_DRV:
3095 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3096 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3097 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3098 psp->ras_drv.start_addr = ucode_start_addr;
3101 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3108 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3110 const struct psp_firmware_header_v1_0 *sos_hdr;
3111 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3112 uint8_t *ucode_array_start_addr;
3114 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3115 ucode_array_start_addr = (uint8_t *)sos_hdr +
3116 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3118 if (adev->gmc.xgmi.connected_to_cpu ||
3119 (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3120 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3121 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3123 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3124 adev->psp.sys.start_addr = ucode_array_start_addr;
3126 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3127 adev->psp.sos.start_addr = ucode_array_start_addr +
3128 le32_to_cpu(sos_hdr->sos.offset_bytes);
3130 /* Load alternate PSP SOS FW */
3131 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3133 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3134 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3136 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3137 adev->psp.sys.start_addr = ucode_array_start_addr +
3138 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3140 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3141 adev->psp.sos.start_addr = ucode_array_start_addr +
3142 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3145 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3146 dev_warn(adev->dev, "PSP SOS FW not available");
3153 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3155 struct amdgpu_device *adev = psp->adev;
3156 char fw_name[PSP_FW_NAME_LEN];
3157 const struct psp_firmware_header_v1_0 *sos_hdr;
3158 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3159 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3160 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3161 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3163 uint8_t *ucode_array_start_addr;
3166 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
3167 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
3171 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3172 ucode_array_start_addr = (uint8_t *)sos_hdr +
3173 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3174 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3176 switch (sos_hdr->header.header_version_major) {
3178 err = psp_init_sos_base_fw(adev);
3182 if (sos_hdr->header.header_version_minor == 1) {
3183 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3184 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3185 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3186 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3187 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3188 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3189 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3191 if (sos_hdr->header.header_version_minor == 2) {
3192 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3193 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3194 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3195 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3197 if (sos_hdr->header.header_version_minor == 3) {
3198 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3199 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3200 adev->psp.toc.start_addr = ucode_array_start_addr +
3201 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3202 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3203 adev->psp.kdb.start_addr = ucode_array_start_addr +
3204 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3205 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3206 adev->psp.spl.start_addr = ucode_array_start_addr +
3207 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3208 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3209 adev->psp.rl.start_addr = ucode_array_start_addr +
3210 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3214 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3216 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3217 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3222 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3223 err = parse_sos_bin_descriptor(psp,
3224 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3232 "unsupported psp sos firmware\n");
3239 amdgpu_ucode_release(&adev->psp.sos_fw);
3244 static int parse_ta_bin_descriptor(struct psp_context *psp,
3245 const struct psp_fw_bin_desc *desc,
3246 const struct ta_firmware_header_v2_0 *ta_hdr)
3248 uint8_t *ucode_start_addr = NULL;
3250 if (!psp || !desc || !ta_hdr)
3253 ucode_start_addr = (uint8_t *)ta_hdr +
3254 le32_to_cpu(desc->offset_bytes) +
3255 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3257 switch (desc->fw_type) {
3258 case TA_FW_TYPE_PSP_ASD:
3259 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3260 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3261 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3262 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3264 case TA_FW_TYPE_PSP_XGMI:
3265 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3266 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3267 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3269 case TA_FW_TYPE_PSP_RAS:
3270 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3271 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3272 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3274 case TA_FW_TYPE_PSP_HDCP:
3275 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3276 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3277 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3279 case TA_FW_TYPE_PSP_DTM:
3280 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3281 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3282 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3284 case TA_FW_TYPE_PSP_RAP:
3285 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3286 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3287 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3289 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3290 psp->securedisplay_context.context.bin_desc.fw_version =
3291 le32_to_cpu(desc->fw_version);
3292 psp->securedisplay_context.context.bin_desc.size_bytes =
3293 le32_to_cpu(desc->size_bytes);
3294 psp->securedisplay_context.context.bin_desc.start_addr =
3298 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3305 static int parse_ta_v1_microcode(struct psp_context *psp)
3307 const struct ta_firmware_header_v1_0 *ta_hdr;
3308 struct amdgpu_device *adev = psp->adev;
3310 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3312 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3315 adev->psp.xgmi_context.context.bin_desc.fw_version =
3316 le32_to_cpu(ta_hdr->xgmi.fw_version);
3317 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3318 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3319 adev->psp.xgmi_context.context.bin_desc.start_addr =
3321 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3323 adev->psp.ras_context.context.bin_desc.fw_version =
3324 le32_to_cpu(ta_hdr->ras.fw_version);
3325 adev->psp.ras_context.context.bin_desc.size_bytes =
3326 le32_to_cpu(ta_hdr->ras.size_bytes);
3327 adev->psp.ras_context.context.bin_desc.start_addr =
3328 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3329 le32_to_cpu(ta_hdr->ras.offset_bytes);
3331 adev->psp.hdcp_context.context.bin_desc.fw_version =
3332 le32_to_cpu(ta_hdr->hdcp.fw_version);
3333 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3334 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3335 adev->psp.hdcp_context.context.bin_desc.start_addr =
3337 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3339 adev->psp.dtm_context.context.bin_desc.fw_version =
3340 le32_to_cpu(ta_hdr->dtm.fw_version);
3341 adev->psp.dtm_context.context.bin_desc.size_bytes =
3342 le32_to_cpu(ta_hdr->dtm.size_bytes);
3343 adev->psp.dtm_context.context.bin_desc.start_addr =
3344 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3345 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3347 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3348 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3349 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3350 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3351 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3352 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3353 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3355 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3360 static int parse_ta_v2_microcode(struct psp_context *psp)
3362 const struct ta_firmware_header_v2_0 *ta_hdr;
3363 struct amdgpu_device *adev = psp->adev;
3367 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3369 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3372 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3373 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3377 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3378 err = parse_ta_bin_descriptor(psp,
3379 &ta_hdr->ta_fw_bin[ta_index],
3388 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3390 const struct common_firmware_header *hdr;
3391 struct amdgpu_device *adev = psp->adev;
3392 char fw_name[PSP_FW_NAME_LEN];
3395 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
3396 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3400 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3401 switch (le16_to_cpu(hdr->header_version_major)) {
3403 err = parse_ta_v1_microcode(psp);
3406 err = parse_ta_v2_microcode(psp);
3409 dev_err(adev->dev, "unsupported TA header version\n");
3414 amdgpu_ucode_release(&adev->psp.ta_fw);
3419 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3421 struct amdgpu_device *adev = psp->adev;
3422 char fw_name[PSP_FW_NAME_LEN];
3423 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3424 struct amdgpu_firmware_info *info = NULL;
3427 if (!amdgpu_sriov_vf(adev)) {
3428 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3432 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
3433 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3435 if (err == -ENODEV) {
3436 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3440 dev_err(adev->dev, "fail to initialize cap microcode\n");
3443 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3444 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3445 info->fw = adev->psp.cap_fw;
3446 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3447 adev->psp.cap_fw->data;
3448 adev->firmware.fw_size += ALIGN(
3449 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3450 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3451 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3452 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3457 amdgpu_ucode_release(&adev->psp.cap_fw);
3461 static int psp_set_clockgating_state(void *handle,
3462 enum amd_clockgating_state state)
3467 static int psp_set_powergating_state(void *handle,
3468 enum amd_powergating_state state)
3473 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3474 struct device_attribute *attr,
3477 struct drm_device *ddev = dev_get_drvdata(dev);
3478 struct amdgpu_device *adev = drm_to_adev(ddev);
3482 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3483 DRM_INFO("PSP block is not ready yet.");
3487 mutex_lock(&adev->psp.mutex);
3488 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3489 mutex_unlock(&adev->psp.mutex);
3492 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
3496 return sysfs_emit(buf, "%x\n", fw_ver);
3499 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3500 struct device_attribute *attr,
3504 struct drm_device *ddev = dev_get_drvdata(dev);
3505 struct amdgpu_device *adev = drm_to_adev(ddev);
3508 const struct firmware *usbc_pd_fw;
3509 struct amdgpu_bo *fw_buf_bo = NULL;
3510 uint64_t fw_pri_mc_addr;
3511 void *fw_pri_cpu_addr;
3513 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3514 DRM_INFO("PSP block is not ready yet.");
3518 if (!drm_dev_enter(ddev, &idx))
3521 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
3522 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
3526 /* LFB address which is aligned to 1MB boundary per PSP request */
3527 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3528 AMDGPU_GEM_DOMAIN_VRAM |
3529 AMDGPU_GEM_DOMAIN_GTT,
3530 &fw_buf_bo, &fw_pri_mc_addr,
3535 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3537 mutex_lock(&adev->psp.mutex);
3538 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3539 mutex_unlock(&adev->psp.mutex);
3541 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3544 release_firmware(usbc_pd_fw);
3547 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3555 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3559 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3562 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3563 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3570 * Reading from this file will retrieve the USB-C PD firmware version. Writing to
3571 * this file will trigger the update process.
3573 static DEVICE_ATTR(usbc_pd_fw, 0644,
3574 psp_usbc_pd_fw_sysfs_read,
3575 psp_usbc_pd_fw_sysfs_write);
3577 int is_psp_fw_valid(struct psp_bin_desc bin)
3579 return bin.size_bytes;
3582 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3583 struct bin_attribute *bin_attr,
3584 char *buffer, loff_t pos, size_t count)
3586 struct device *dev = kobj_to_dev(kobj);
3587 struct drm_device *ddev = dev_get_drvdata(dev);
3588 struct amdgpu_device *adev = drm_to_adev(ddev);
3590 adev->psp.vbflash_done = false;
3592 /* Safeguard against memory drain */
3593 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3594 dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
3595 kvfree(adev->psp.vbflash_tmp_buf);
3596 adev->psp.vbflash_tmp_buf = NULL;
3597 adev->psp.vbflash_image_size = 0;
3601 /* TODO Just allocate max for now and optimize to realloc later if needed */
3602 if (!adev->psp.vbflash_tmp_buf) {
3603 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3604 if (!adev->psp.vbflash_tmp_buf)
3608 mutex_lock(&adev->psp.mutex);
3609 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3610 adev->psp.vbflash_image_size += count;
3611 mutex_unlock(&adev->psp.mutex);
3613 dev_dbg(adev->dev, "IFWI staged for update");
3618 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3619 struct bin_attribute *bin_attr, char *buffer,
3620 loff_t pos, size_t count)
3622 struct device *dev = kobj_to_dev(kobj);
3623 struct drm_device *ddev = dev_get_drvdata(dev);
3624 struct amdgpu_device *adev = drm_to_adev(ddev);
3625 struct amdgpu_bo *fw_buf_bo = NULL;
3626 uint64_t fw_pri_mc_addr;
3627 void *fw_pri_cpu_addr;
3630 if (adev->psp.vbflash_image_size == 0)
3633 dev_dbg(adev->dev, "PSP IFWI flash process initiated");
3635 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3636 AMDGPU_GPU_PAGE_SIZE,
3637 AMDGPU_GEM_DOMAIN_VRAM,
3644 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3646 mutex_lock(&adev->psp.mutex);
3647 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3648 mutex_unlock(&adev->psp.mutex);
3650 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3653 kvfree(adev->psp.vbflash_tmp_buf);
3654 adev->psp.vbflash_tmp_buf = NULL;
3655 adev->psp.vbflash_image_size = 0;
3658 dev_err(adev->dev, "Failed to load IFWI, err = %d", ret);
3662 dev_dbg(adev->dev, "PSP IFWI flash process done");
3668 * Writing to this file will stage an IFWI for update. Reading from this file
3669 * will trigger the update process.
3671 static struct bin_attribute psp_vbflash_bin_attr = {
3672 .attr = {.name = "psp_vbflash", .mode = 0660},
3674 .write = amdgpu_psp_vbflash_write,
3675 .read = amdgpu_psp_vbflash_read,
3679 * DOC: psp_vbflash_status
3680 * The status of the flash process.
3681 * 0: IFWI flash not complete.
3682 * 1: IFWI flash complete.
3684 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3685 struct device_attribute *attr,
3688 struct drm_device *ddev = dev_get_drvdata(dev);
3689 struct amdgpu_device *adev = drm_to_adev(ddev);
3690 uint32_t vbflash_status;
3692 vbflash_status = psp_vbflash_status(&adev->psp);
3693 if (!adev->psp.vbflash_done)
3695 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3698 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3700 static DEVICE_ATTR(psp_vbflash_status, 0440, amdgpu_psp_vbflash_status, NULL);
3702 static struct bin_attribute *bin_flash_attrs[] = {
3703 &psp_vbflash_bin_attr,
3707 static struct attribute *flash_attrs[] = {
3708 &dev_attr_psp_vbflash_status.attr,
3709 &dev_attr_usbc_pd_fw.attr,
3713 static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
3715 struct device *dev = kobj_to_dev(kobj);
3716 struct drm_device *ddev = dev_get_drvdata(dev);
3717 struct amdgpu_device *adev = drm_to_adev(ddev);
3719 if (attr == &dev_attr_usbc_pd_fw.attr)
3720 return adev->psp.sup_pd_fw_up ? 0660 : 0;
3722 return adev->psp.sup_ifwi_up ? 0440 : 0;
3725 static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
3726 struct bin_attribute *attr,
3729 struct device *dev = kobj_to_dev(kobj);
3730 struct drm_device *ddev = dev_get_drvdata(dev);
3731 struct amdgpu_device *adev = drm_to_adev(ddev);
3733 return adev->psp.sup_ifwi_up ? 0660 : 0;
3736 const struct attribute_group amdgpu_flash_attr_group = {
3737 .attrs = flash_attrs,
3738 .bin_attrs = bin_flash_attrs,
3739 .is_bin_visible = amdgpu_bin_flash_attr_is_visible,
3740 .is_visible = amdgpu_flash_attr_is_visible,
3743 const struct amd_ip_funcs psp_ip_funcs = {
3745 .early_init = psp_early_init,
3747 .sw_init = psp_sw_init,
3748 .sw_fini = psp_sw_fini,
3749 .hw_init = psp_hw_init,
3750 .hw_fini = psp_hw_fini,
3751 .suspend = psp_suspend,
3752 .resume = psp_resume,
3754 .check_soft_reset = NULL,
3755 .wait_for_idle = NULL,
3757 .set_clockgating_state = psp_set_clockgating_state,
3758 .set_powergating_state = psp_set_powergating_state,
3761 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
3762 .type = AMD_IP_BLOCK_TYPE_PSP,
3766 .funcs = &psp_ip_funcs,
3769 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3770 .type = AMD_IP_BLOCK_TYPE_PSP,
3774 .funcs = &psp_ip_funcs,
3777 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3778 .type = AMD_IP_BLOCK_TYPE_PSP,
3782 .funcs = &psp_ip_funcs,
3785 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
3786 .type = AMD_IP_BLOCK_TYPE_PSP,
3790 .funcs = &psp_ip_funcs,
3793 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
3794 .type = AMD_IP_BLOCK_TYPE_PSP,
3798 .funcs = &psp_ip_funcs,
3801 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
3802 .type = AMD_IP_BLOCK_TYPE_PSP,
3806 .funcs = &psp_ip_funcs,
3809 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
3810 .type = AMD_IP_BLOCK_TYPE_PSP,
3814 .funcs = &psp_ip_funcs,