2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/firmware.h>
27 #include <drm/drm_drv.h>
30 #include "amdgpu_psp.h"
31 #include "amdgpu_ucode.h"
32 #include "amdgpu_xgmi.h"
33 #include "soc15_common.h"
35 #include "psp_v10_0.h"
36 #include "psp_v11_0.h"
37 #include "psp_v11_0_8.h"
38 #include "psp_v12_0.h"
39 #include "psp_v13_0.h"
40 #include "psp_v13_0_4.h"
41 #include "psp_v14_0.h"
43 #include "amdgpu_ras.h"
44 #include "amdgpu_securedisplay.h"
45 #include "amdgpu_atomfirmware.h"
47 #define AMD_VBIOS_FILE_MAX_SIZE_B (1024*1024*3)
49 static int psp_load_smu_fw(struct psp_context *psp);
50 static int psp_rap_terminate(struct psp_context *psp);
51 static int psp_securedisplay_terminate(struct psp_context *psp);
53 static int psp_ring_init(struct psp_context *psp,
54 enum psp_ring_type ring_type)
57 struct psp_ring *ring;
58 struct amdgpu_device *adev = psp->adev;
62 ring->ring_type = ring_type;
64 /* allocate 4k Page of Local Frame Buffer memory for ring */
65 ring->ring_size = 0x1000;
66 ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
67 AMDGPU_GEM_DOMAIN_VRAM |
68 AMDGPU_GEM_DOMAIN_GTT,
70 &ring->ring_mem_mc_addr,
71 (void **)&ring->ring_mem);
81 * Due to DF Cstate management centralized to PMFW, the firmware
82 * loading sequence will be updated as below:
88 * - Load other non-psp fw
90 * - Load XGMI/RAS/HDCP/DTM TA if any
92 * This new sequence is required for
93 * - Arcturus and onwards
95 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
97 struct amdgpu_device *adev = psp->adev;
99 if (amdgpu_sriov_vf(adev)) {
100 psp->pmfw_centralized_cstate_management = false;
104 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
105 case IP_VERSION(11, 0, 0):
106 case IP_VERSION(11, 0, 4):
107 case IP_VERSION(11, 0, 5):
108 case IP_VERSION(11, 0, 7):
109 case IP_VERSION(11, 0, 9):
110 case IP_VERSION(11, 0, 11):
111 case IP_VERSION(11, 0, 12):
112 case IP_VERSION(11, 0, 13):
113 case IP_VERSION(13, 0, 0):
114 case IP_VERSION(13, 0, 2):
115 case IP_VERSION(13, 0, 7):
116 psp->pmfw_centralized_cstate_management = true;
119 psp->pmfw_centralized_cstate_management = false;
124 static int psp_init_sriov_microcode(struct psp_context *psp)
126 struct amdgpu_device *adev = psp->adev;
127 char ucode_prefix[30];
130 amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
132 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
133 case IP_VERSION(9, 0, 0):
134 case IP_VERSION(11, 0, 7):
135 case IP_VERSION(11, 0, 9):
136 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
137 ret = psp_init_cap_microcode(psp, ucode_prefix);
139 case IP_VERSION(13, 0, 2):
140 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
141 ret = psp_init_cap_microcode(psp, ucode_prefix);
142 ret &= psp_init_ta_microcode(psp, ucode_prefix);
144 case IP_VERSION(13, 0, 0):
145 adev->virt.autoload_ucode_id = 0;
147 case IP_VERSION(13, 0, 6):
148 case IP_VERSION(13, 0, 14):
149 ret = psp_init_cap_microcode(psp, ucode_prefix);
150 ret &= psp_init_ta_microcode(psp, ucode_prefix);
152 case IP_VERSION(13, 0, 10):
153 adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
154 ret = psp_init_cap_microcode(psp, ucode_prefix);
162 static int psp_early_init(void *handle)
164 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
165 struct psp_context *psp = &adev->psp;
167 psp->autoload_supported = true;
168 psp->boot_time_tmr = true;
170 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
171 case IP_VERSION(9, 0, 0):
172 psp_v3_1_set_psp_funcs(psp);
173 psp->autoload_supported = false;
174 psp->boot_time_tmr = false;
176 case IP_VERSION(10, 0, 0):
177 case IP_VERSION(10, 0, 1):
178 psp_v10_0_set_psp_funcs(psp);
179 psp->autoload_supported = false;
180 psp->boot_time_tmr = false;
182 case IP_VERSION(11, 0, 2):
183 case IP_VERSION(11, 0, 4):
184 psp_v11_0_set_psp_funcs(psp);
185 psp->autoload_supported = false;
186 psp->boot_time_tmr = false;
188 case IP_VERSION(11, 0, 0):
189 case IP_VERSION(11, 0, 7):
190 adev->psp.sup_pd_fw_up = !amdgpu_sriov_vf(adev);
192 case IP_VERSION(11, 0, 5):
193 case IP_VERSION(11, 0, 9):
194 case IP_VERSION(11, 0, 11):
195 case IP_VERSION(11, 5, 0):
196 case IP_VERSION(11, 0, 12):
197 case IP_VERSION(11, 0, 13):
198 psp_v11_0_set_psp_funcs(psp);
199 psp->boot_time_tmr = false;
201 case IP_VERSION(11, 0, 3):
202 case IP_VERSION(12, 0, 1):
203 psp_v12_0_set_psp_funcs(psp);
204 psp->autoload_supported = false;
205 psp->boot_time_tmr = false;
207 case IP_VERSION(13, 0, 2):
208 psp->boot_time_tmr = false;
210 case IP_VERSION(13, 0, 6):
211 case IP_VERSION(13, 0, 14):
212 psp_v13_0_set_psp_funcs(psp);
213 psp->autoload_supported = false;
215 case IP_VERSION(13, 0, 1):
216 case IP_VERSION(13, 0, 3):
217 case IP_VERSION(13, 0, 5):
218 case IP_VERSION(13, 0, 8):
219 case IP_VERSION(13, 0, 11):
220 case IP_VERSION(14, 0, 0):
221 case IP_VERSION(14, 0, 1):
222 case IP_VERSION(14, 0, 4):
223 psp_v13_0_set_psp_funcs(psp);
224 psp->boot_time_tmr = false;
226 case IP_VERSION(11, 0, 8):
227 if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
228 psp_v11_0_8_set_psp_funcs(psp);
230 psp->autoload_supported = false;
231 psp->boot_time_tmr = false;
233 case IP_VERSION(13, 0, 0):
234 case IP_VERSION(13, 0, 7):
235 case IP_VERSION(13, 0, 10):
236 psp_v13_0_set_psp_funcs(psp);
237 adev->psp.sup_ifwi_up = !amdgpu_sriov_vf(adev);
238 psp->boot_time_tmr = false;
240 case IP_VERSION(13, 0, 4):
241 psp_v13_0_4_set_psp_funcs(psp);
242 psp->boot_time_tmr = false;
244 case IP_VERSION(14, 0, 2):
245 case IP_VERSION(14, 0, 3):
246 psp_v14_0_set_psp_funcs(psp);
254 adev->psp_timeout = 20000;
256 psp_check_pmfw_centralized_cstate_management(psp);
258 if (amdgpu_sriov_vf(adev))
259 return psp_init_sriov_microcode(psp);
261 return psp_init_microcode(psp);
264 void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
266 amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
267 &mem_ctx->shared_buf);
268 mem_ctx->shared_bo = NULL;
271 static void psp_free_shared_bufs(struct psp_context *psp)
276 /* free TMR memory buffer */
277 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
278 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
281 /* free xgmi shared memory */
282 psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
284 /* free ras shared memory */
285 psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
287 /* free hdcp shared memory */
288 psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
290 /* free dtm shared memory */
291 psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
293 /* free rap shared memory */
294 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
296 /* free securedisplay shared memory */
297 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
302 static void psp_memory_training_fini(struct psp_context *psp)
304 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
306 ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
307 kfree(ctx->sys_cache);
308 ctx->sys_cache = NULL;
311 static int psp_memory_training_init(struct psp_context *psp)
314 struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
316 if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
317 dev_dbg(psp->adev->dev, "memory training is not supported!\n");
321 ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
322 if (ctx->sys_cache == NULL) {
323 dev_err(psp->adev->dev, "alloc mem_train_ctx.sys_cache failed!\n");
328 dev_dbg(psp->adev->dev,
329 "train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
330 ctx->train_data_size,
331 ctx->p2c_train_data_offset,
332 ctx->c2p_train_data_offset);
333 ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
337 psp_memory_training_fini(psp);
342 * Helper funciton to query psp runtime database entry
344 * @adev: amdgpu_device pointer
345 * @entry_type: the type of psp runtime database entry
346 * @db_entry: runtime database entry pointer
348 * Return false if runtime database doesn't exit or entry is invalid
349 * or true if the specific database entry is found, and copy to @db_entry
351 static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
352 enum psp_runtime_entry_type entry_type,
355 uint64_t db_header_pos, db_dir_pos;
356 struct psp_runtime_data_header db_header = {0};
357 struct psp_runtime_data_directory db_dir = {0};
361 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 6) ||
362 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 14))
365 db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
366 db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
368 /* read runtime db header from vram */
369 amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
370 sizeof(struct psp_runtime_data_header), false);
372 if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
373 /* runtime db doesn't exist, exit */
374 dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
378 /* read runtime database entry from vram */
379 amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
380 sizeof(struct psp_runtime_data_directory), false);
382 if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
383 /* invalid db entry count, exit */
384 dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
388 /* look up for requested entry type */
389 for (i = 0; i < db_dir.entry_count && !ret; i++) {
390 if (db_dir.entry_list[i].entry_type == entry_type) {
391 switch (entry_type) {
392 case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
393 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
394 /* invalid db entry size */
395 dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
398 /* read runtime database entry */
399 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
400 (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
403 case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
404 if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
405 /* invalid db entry size */
406 dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
409 /* read runtime database entry */
410 amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
411 (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
424 static int psp_sw_init(void *handle)
426 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
427 struct psp_context *psp = &adev->psp;
429 struct psp_runtime_boot_cfg_entry boot_cfg_entry;
430 struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
431 struct psp_runtime_scpm_entry scpm_entry;
433 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
435 dev_err(adev->dev, "Failed to allocate memory to command buffer!\n");
439 adev->psp.xgmi_context.supports_extended_data =
440 !adev->gmc.xgmi.connected_to_cpu &&
441 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 2);
443 memset(&scpm_entry, 0, sizeof(scpm_entry));
444 if ((psp_get_runtime_db_entry(adev,
445 PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
447 (scpm_entry.scpm_status != SCPM_DISABLE)) {
448 adev->scpm_enabled = true;
449 adev->scpm_status = scpm_entry.scpm_status;
451 adev->scpm_enabled = false;
452 adev->scpm_status = SCPM_DISABLE;
455 /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
457 memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
458 if (psp_get_runtime_db_entry(adev,
459 PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
461 psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
462 if ((psp->boot_cfg_bitmask) &
463 BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
464 /* If psp runtime database exists, then
465 * only enable two stage memory training
466 * when TWO_STAGE_DRAM_TRAINING bit is set
467 * in runtime database
469 mem_training_ctx->enable_mem_training = true;
473 /* If psp runtime database doesn't exist or is
474 * invalid, force enable two stage memory training
476 mem_training_ctx->enable_mem_training = true;
479 if (mem_training_ctx->enable_mem_training) {
480 ret = psp_memory_training_init(psp);
482 dev_err(adev->dev, "Failed to initialize memory training!\n");
486 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
488 dev_err(adev->dev, "Failed to process memory training!\n");
493 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
494 (amdgpu_sriov_vf(adev) || adev->debug_use_vram_fw_buf) ?
495 AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
497 &psp->fw_pri_mc_addr,
502 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
503 AMDGPU_GEM_DOMAIN_VRAM |
504 AMDGPU_GEM_DOMAIN_GTT,
506 &psp->fence_buf_mc_addr,
511 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
512 AMDGPU_GEM_DOMAIN_VRAM |
513 AMDGPU_GEM_DOMAIN_GTT,
514 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
515 (void **)&psp->cmd_buf_mem);
522 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
523 &psp->fence_buf_mc_addr, &psp->fence_buf);
525 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
526 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
530 static int psp_sw_fini(void *handle)
532 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
533 struct psp_context *psp = &adev->psp;
534 struct psp_gfx_cmd_resp *cmd = psp->cmd;
536 psp_memory_training_fini(psp);
538 amdgpu_ucode_release(&psp->sos_fw);
539 amdgpu_ucode_release(&psp->asd_fw);
540 amdgpu_ucode_release(&psp->ta_fw);
541 amdgpu_ucode_release(&psp->cap_fw);
542 amdgpu_ucode_release(&psp->toc_fw);
547 psp_free_shared_bufs(psp);
549 if (psp->km_ring.ring_mem)
550 amdgpu_bo_free_kernel(&adev->firmware.rbuf,
551 &psp->km_ring.ring_mem_mc_addr,
552 (void **)&psp->km_ring.ring_mem);
554 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
555 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
556 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
557 &psp->fence_buf_mc_addr, &psp->fence_buf);
558 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
559 (void **)&psp->cmd_buf_mem);
564 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
565 uint32_t reg_val, uint32_t mask, bool check_changed)
569 struct amdgpu_device *adev = psp->adev;
571 if (psp->adev->no_hw_access)
574 for (i = 0; i < adev->usec_timeout; i++) {
575 val = RREG32(reg_index);
580 if ((val & mask) == reg_val)
589 int psp_wait_for_spirom_update(struct psp_context *psp, uint32_t reg_index,
590 uint32_t reg_val, uint32_t mask, uint32_t msec_timeout)
594 struct amdgpu_device *adev = psp->adev;
596 if (psp->adev->no_hw_access)
599 for (i = 0; i < msec_timeout; i++) {
600 val = RREG32(reg_index);
601 if ((val & mask) == reg_val)
609 static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
612 case GFX_CMD_ID_LOAD_TA:
614 case GFX_CMD_ID_UNLOAD_TA:
616 case GFX_CMD_ID_INVOKE_CMD:
618 case GFX_CMD_ID_LOAD_ASD:
620 case GFX_CMD_ID_SETUP_TMR:
622 case GFX_CMD_ID_LOAD_IP_FW:
624 case GFX_CMD_ID_DESTROY_TMR:
625 return "DESTROY_TMR";
626 case GFX_CMD_ID_SAVE_RESTORE:
627 return "SAVE_RESTORE_IP_FW";
628 case GFX_CMD_ID_SETUP_VMR:
630 case GFX_CMD_ID_DESTROY_VMR:
631 return "DESTROY_VMR";
632 case GFX_CMD_ID_PROG_REG:
634 case GFX_CMD_ID_GET_FW_ATTESTATION:
635 return "GET_FW_ATTESTATION";
636 case GFX_CMD_ID_LOAD_TOC:
637 return "ID_LOAD_TOC";
638 case GFX_CMD_ID_AUTOLOAD_RLC:
639 return "AUTOLOAD_RLC";
640 case GFX_CMD_ID_BOOT_CFG:
643 return "UNKNOWN CMD";
647 static bool psp_err_warn(struct psp_context *psp)
649 struct psp_gfx_cmd_resp *cmd = psp->cmd_buf_mem;
651 /* This response indicates reg list is already loaded */
652 if (amdgpu_ip_version(psp->adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 2) &&
653 cmd->cmd_id == GFX_CMD_ID_LOAD_IP_FW &&
654 cmd->cmd.cmd_load_ip_fw.fw_type == GFX_FW_TYPE_REG_LIST &&
655 cmd->resp.status == TEE_ERROR_CANCEL)
662 psp_cmd_submit_buf(struct psp_context *psp,
663 struct amdgpu_firmware_info *ucode,
664 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
668 int timeout = psp->adev->psp_timeout;
669 bool ras_intr = false;
670 bool skip_unsupport = false;
672 if (psp->adev->no_hw_access)
675 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
677 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
679 index = atomic_inc_return(&psp->fence_value);
680 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
682 atomic_dec(&psp->fence_value);
686 amdgpu_device_invalidate_hdp(psp->adev, NULL);
687 while (*((unsigned int *)psp->fence_buf) != index) {
691 * Shouldn't wait for timeout when err_event_athub occurs,
692 * because gpu reset thread triggered and lock resource should
693 * be released for psp resume sequence.
695 ras_intr = amdgpu_ras_intr_triggered();
698 usleep_range(10, 100);
699 amdgpu_device_invalidate_hdp(psp->adev, NULL);
702 /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
703 skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
704 psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
706 memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
708 /* In some cases, psp response status is not 0 even there is no
709 * problem while the command is submitted. Some version of PSP FW
710 * doesn't write 0 to that field.
711 * So here we would like to only print a warning instead of an error
712 * during psp initialization to avoid breaking hw_init and it doesn't
715 if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
717 dev_warn(psp->adev->dev,
718 "failed to load ucode %s(0x%X) ",
719 amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
720 if (psp_err_warn(psp))
723 "psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
724 psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id),
725 psp->cmd_buf_mem->cmd_id,
726 psp->cmd_buf_mem->resp.status);
727 /* If any firmware (including CAP) load fails under SRIOV, it should
728 * return failure to stop the VF from initializing.
729 * Also return failure in case of timeout
731 if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
738 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
739 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
746 static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
748 struct psp_gfx_cmd_resp *cmd = psp->cmd;
750 mutex_lock(&psp->mutex);
752 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
757 static void release_psp_cmd_buf(struct psp_context *psp)
759 mutex_unlock(&psp->mutex);
762 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
763 struct psp_gfx_cmd_resp *cmd,
764 uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
766 struct amdgpu_device *adev = psp->adev;
771 size = amdgpu_bo_size(tmr_bo);
772 tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
775 if (amdgpu_sriov_vf(psp->adev))
776 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
778 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
779 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
780 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
781 cmd->cmd.cmd_setup_tmr.buf_size = size;
782 cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
783 cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
784 cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
787 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
788 uint64_t pri_buf_mc, uint32_t size)
790 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
791 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
792 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
793 cmd->cmd.cmd_load_toc.toc_size = size;
796 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
797 static int psp_load_toc(struct psp_context *psp,
801 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
803 /* Copy toc to psp firmware private buffer */
804 psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
806 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
808 ret = psp_cmd_submit_buf(psp, NULL, cmd,
809 psp->fence_buf_mc_addr);
811 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
813 release_psp_cmd_buf(psp);
818 /* Set up Trusted Memory Region */
819 static int psp_tmr_init(struct psp_context *psp)
827 * According to HW engineer, they prefer the TMR address be "naturally
828 * aligned" , e.g. the start address be an integer divide of TMR size.
830 * Note: this memory need be reserved till the driver
833 tmr_size = PSP_TMR_SIZE(psp->adev);
835 /* For ASICs support RLC autoload, psp will parse the toc
836 * and calculate the total size of TMR needed
838 if (!amdgpu_sriov_vf(psp->adev) &&
839 psp->toc.start_addr &&
840 psp->toc.size_bytes &&
842 ret = psp_load_toc(psp, &tmr_size);
844 dev_err(psp->adev->dev, "Failed to load toc\n");
849 if (!psp->tmr_bo && !psp->boot_time_tmr) {
850 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
851 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
853 AMDGPU_HAS_VRAM(psp->adev) ?
854 AMDGPU_GEM_DOMAIN_VRAM :
855 AMDGPU_GEM_DOMAIN_GTT,
856 &psp->tmr_bo, &psp->tmr_mc_addr,
863 static bool psp_skip_tmr(struct psp_context *psp)
865 switch (amdgpu_ip_version(psp->adev, MP0_HWIP, 0)) {
866 case IP_VERSION(11, 0, 9):
867 case IP_VERSION(11, 0, 7):
868 case IP_VERSION(13, 0, 2):
869 case IP_VERSION(13, 0, 6):
870 case IP_VERSION(13, 0, 10):
871 case IP_VERSION(13, 0, 14):
878 static int psp_tmr_load(struct psp_context *psp)
881 struct psp_gfx_cmd_resp *cmd;
883 /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
884 * Already set up by host driver.
886 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
889 cmd = acquire_psp_cmd_buf(psp);
891 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
893 dev_info(psp->adev->dev, "reserve 0x%lx from 0x%llx for PSP TMR\n",
894 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
896 ret = psp_cmd_submit_buf(psp, NULL, cmd,
897 psp->fence_buf_mc_addr);
899 release_psp_cmd_buf(psp);
904 static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
905 struct psp_gfx_cmd_resp *cmd)
907 if (amdgpu_sriov_vf(psp->adev))
908 cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
910 cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
913 static int psp_tmr_unload(struct psp_context *psp)
916 struct psp_gfx_cmd_resp *cmd;
918 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
919 * as TMR is not loaded at all
921 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
924 cmd = acquire_psp_cmd_buf(psp);
926 psp_prep_tmr_unload_cmd_buf(psp, cmd);
927 dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
929 ret = psp_cmd_submit_buf(psp, NULL, cmd,
930 psp->fence_buf_mc_addr);
932 release_psp_cmd_buf(psp);
937 static int psp_tmr_terminate(struct psp_context *psp)
939 return psp_tmr_unload(psp);
942 int psp_get_fw_attestation_records_addr(struct psp_context *psp,
943 uint64_t *output_ptr)
946 struct psp_gfx_cmd_resp *cmd;
951 if (amdgpu_sriov_vf(psp->adev))
954 cmd = acquire_psp_cmd_buf(psp);
956 cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
958 ret = psp_cmd_submit_buf(psp, NULL, cmd,
959 psp->fence_buf_mc_addr);
962 *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
963 ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
966 release_psp_cmd_buf(psp);
971 static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
973 struct psp_context *psp = &adev->psp;
974 struct psp_gfx_cmd_resp *cmd;
977 if (amdgpu_sriov_vf(adev))
980 cmd = acquire_psp_cmd_buf(psp);
982 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
983 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
985 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
988 (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
991 release_psp_cmd_buf(psp);
996 static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
999 struct psp_context *psp = &adev->psp;
1000 struct psp_gfx_cmd_resp *cmd;
1002 if (amdgpu_sriov_vf(adev))
1005 cmd = acquire_psp_cmd_buf(psp);
1007 cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
1008 cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
1009 cmd->cmd.boot_cfg.boot_config = boot_cfg;
1010 cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
1012 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1014 release_psp_cmd_buf(psp);
1019 static int psp_rl_load(struct amdgpu_device *adev)
1022 struct psp_context *psp = &adev->psp;
1023 struct psp_gfx_cmd_resp *cmd;
1025 if (!is_psp_fw_valid(psp->rl))
1028 cmd = acquire_psp_cmd_buf(psp);
1030 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
1031 memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
1033 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
1034 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
1035 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
1036 cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
1037 cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
1039 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1041 release_psp_cmd_buf(psp);
1046 int psp_spatial_partition(struct psp_context *psp, int mode)
1048 struct psp_gfx_cmd_resp *cmd;
1051 if (amdgpu_sriov_vf(psp->adev))
1054 cmd = acquire_psp_cmd_buf(psp);
1056 cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1057 cmd->cmd.cmd_spatial_part.mode = mode;
1059 dev_info(psp->adev->dev, "Requesting %d partitions through PSP", mode);
1060 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1062 release_psp_cmd_buf(psp);
1067 static int psp_asd_initialize(struct psp_context *psp)
1071 /* If PSP version doesn't match ASD version, asd loading will be failed.
1072 * add workaround to bypass it for sriov now.
1073 * TODO: add version check to make it common
1075 if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1078 /* bypass asd if display hardware is not available */
1079 if (!amdgpu_device_has_display_hardware(psp->adev) &&
1080 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) >= IP_VERSION(13, 0, 10))
1083 psp->asd_context.mem_context.shared_mc_addr = 0;
1084 psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
1085 psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
1087 ret = psp_ta_load(psp, &psp->asd_context);
1089 psp->asd_context.initialized = true;
1094 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1095 uint32_t session_id)
1097 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
1098 cmd->cmd.cmd_unload_ta.session_id = session_id;
1101 int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
1104 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1106 psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
1108 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1110 context->resp_status = cmd->resp.status;
1112 release_psp_cmd_buf(psp);
1117 static int psp_asd_terminate(struct psp_context *psp)
1121 if (amdgpu_sriov_vf(psp->adev))
1124 if (!psp->asd_context.initialized)
1127 ret = psp_ta_unload(psp, &psp->asd_context);
1129 psp->asd_context.initialized = false;
1134 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1135 uint32_t id, uint32_t value)
1137 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1138 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1139 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1142 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1145 struct psp_gfx_cmd_resp *cmd;
1148 if (reg >= PSP_REG_LAST)
1151 cmd = acquire_psp_cmd_buf(psp);
1153 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1154 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1156 dev_err(psp->adev->dev, "PSP failed to program reg id %d\n", reg);
1158 release_psp_cmd_buf(psp);
1163 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1165 struct ta_context *context)
1167 cmd->cmd_id = context->ta_load_type;
1168 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
1169 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1170 cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
1172 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1173 lower_32_bits(context->mem_context.shared_mc_addr);
1174 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1175 upper_32_bits(context->mem_context.shared_mc_addr);
1176 cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
1179 int psp_ta_init_shared_buf(struct psp_context *psp,
1180 struct ta_mem_context *mem_ctx)
1183 * Allocate 16k memory aligned to 4k from Frame Buffer (local
1184 * physical) for ta to host memory
1186 return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
1187 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
1188 AMDGPU_GEM_DOMAIN_GTT,
1189 &mem_ctx->shared_bo,
1190 &mem_ctx->shared_mc_addr,
1191 &mem_ctx->shared_buf);
1194 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1196 uint32_t session_id)
1198 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
1199 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
1200 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
1203 int psp_ta_invoke(struct psp_context *psp,
1205 struct ta_context *context)
1208 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
1210 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
1212 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1213 psp->fence_buf_mc_addr);
1215 context->resp_status = cmd->resp.status;
1217 release_psp_cmd_buf(psp);
1222 int psp_ta_load(struct psp_context *psp, struct ta_context *context)
1225 struct psp_gfx_cmd_resp *cmd;
1227 cmd = acquire_psp_cmd_buf(psp);
1229 psp_copy_fw(psp, context->bin_desc.start_addr,
1230 context->bin_desc.size_bytes);
1232 psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
1234 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1235 psp->fence_buf_mc_addr);
1237 context->resp_status = cmd->resp.status;
1240 context->session_id = cmd->resp.session_id;
1242 release_psp_cmd_buf(psp);
1247 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1249 return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1252 int psp_xgmi_terminate(struct psp_context *psp)
1255 struct amdgpu_device *adev = psp->adev;
1257 /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
1258 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 4) ||
1259 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 2) &&
1260 adev->gmc.xgmi.connected_to_cpu))
1263 if (!psp->xgmi_context.context.initialized)
1266 ret = psp_ta_unload(psp, &psp->xgmi_context.context);
1268 psp->xgmi_context.context.initialized = false;
1273 int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
1275 struct ta_xgmi_shared_memory *xgmi_cmd;
1279 !psp->xgmi_context.context.bin_desc.size_bytes ||
1280 !psp->xgmi_context.context.bin_desc.start_addr)
1286 psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
1287 psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1289 if (!psp->xgmi_context.context.mem_context.shared_buf) {
1290 ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
1296 ret = psp_ta_load(psp, &psp->xgmi_context.context);
1298 psp->xgmi_context.context.initialized = true;
1303 /* Initialize XGMI session */
1304 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
1305 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1306 xgmi_cmd->flag_extend_link_record = set_extended_data;
1307 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
1309 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1310 /* note down the capbility flag for XGMI TA */
1311 psp->xgmi_context.xgmi_ta_caps = xgmi_cmd->caps_flag;
1316 int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
1318 struct ta_xgmi_shared_memory *xgmi_cmd;
1321 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1322 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1324 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
1326 /* Invoke xgmi ta to get hive id */
1327 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1331 *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
1336 int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
1338 struct ta_xgmi_shared_memory *xgmi_cmd;
1341 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1342 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1344 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
1346 /* Invoke xgmi ta to get the node id */
1347 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1351 *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
1356 static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1358 return (amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1359 IP_VERSION(13, 0, 2) &&
1360 psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
1361 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) >=
1362 IP_VERSION(13, 0, 6);
1366 * Chips that support extended topology information require the driver to
1367 * reflect topology information in the opposite direction. This is
1368 * because the TA has already exceeded its link record limit and if the
1369 * TA holds bi-directional information, the driver would have to do
1370 * multiple fetches instead of just two.
1372 static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
1373 struct psp_xgmi_node_info node_info)
1375 struct amdgpu_device *mirror_adev;
1376 struct amdgpu_hive_info *hive;
1377 uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
1378 uint64_t dst_node_id = node_info.node_id;
1379 uint8_t dst_num_hops = node_info.num_hops;
1380 uint8_t dst_num_links = node_info.num_links;
1382 hive = amdgpu_get_xgmi_hive(psp->adev);
1386 list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
1387 struct psp_xgmi_topology_info *mirror_top_info;
1390 if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
1393 mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
1394 for (j = 0; j < mirror_top_info->num_nodes; j++) {
1395 if (mirror_top_info->nodes[j].node_id != src_node_id)
1398 mirror_top_info->nodes[j].num_hops = dst_num_hops;
1400 * prevent 0 num_links value re-reflection since reflection
1401 * criteria is based on num_hops (direct or indirect).
1405 mirror_top_info->nodes[j].num_links = dst_num_links;
1413 amdgpu_put_xgmi_hive(hive);
1416 int psp_xgmi_get_topology_info(struct psp_context *psp,
1418 struct psp_xgmi_topology_info *topology,
1419 bool get_extended_data)
1421 struct ta_xgmi_shared_memory *xgmi_cmd;
1422 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1423 struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
1427 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1430 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1431 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1432 xgmi_cmd->flag_extend_link_record = get_extended_data;
1434 /* Fill in the shared memory with topology information as input */
1435 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1436 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_TOPOLOGY_INFO;
1437 topology_info_input->num_nodes = number_devices;
1439 for (i = 0; i < topology_info_input->num_nodes; i++) {
1440 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1441 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1442 topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
1443 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1446 /* Invoke xgmi ta to get the topology information */
1447 ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_TOPOLOGY_INFO);
1451 /* Read the output topology information from the shared memory */
1452 topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
1453 topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
1454 for (i = 0; i < topology->num_nodes; i++) {
1455 /* extended data will either be 0 or equal to non-extended data */
1456 if (topology_info_output->nodes[i].num_hops)
1457 topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
1459 /* non-extended data gets everything here so no need to update */
1460 if (!get_extended_data) {
1461 topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
1462 topology->nodes[i].is_sharing_enabled =
1463 topology_info_output->nodes[i].is_sharing_enabled;
1464 topology->nodes[i].sdma_engine =
1465 topology_info_output->nodes[i].sdma_engine;
1470 /* Invoke xgmi ta again to get the link information */
1471 if (psp_xgmi_peer_link_info_supported(psp)) {
1472 struct ta_xgmi_cmd_get_peer_link_info *link_info_output;
1473 struct ta_xgmi_cmd_get_extend_peer_link_info *link_extend_info_output;
1474 bool requires_reflection =
1475 (psp->xgmi_context.supports_extended_data &&
1476 get_extended_data) ||
1477 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1478 IP_VERSION(13, 0, 6) ||
1479 amdgpu_ip_version(psp->adev, MP0_HWIP, 0) ==
1480 IP_VERSION(13, 0, 14);
1481 bool ta_port_num_support = amdgpu_sriov_vf(psp->adev) ? 0 :
1482 psp->xgmi_context.xgmi_ta_caps & EXTEND_PEER_LINK_INFO_CMD_FLAG;
1484 /* popluate the shared output buffer rather than the cmd input buffer
1485 * with node_ids as the input for GET_PEER_LINKS command execution.
1486 * This is required for GET_PEER_LINKS per xgmi ta implementation.
1487 * The same requirement for GET_EXTEND_PEER_LINKS command.
1489 if (ta_port_num_support) {
1490 link_extend_info_output = &xgmi_cmd->xgmi_out_message.get_extend_link_info;
1492 for (i = 0; i < topology->num_nodes; i++)
1493 link_extend_info_output->nodes[i].node_id = topology->nodes[i].node_id;
1495 link_extend_info_output->num_nodes = topology->num_nodes;
1496 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_EXTEND_PEER_LINKS;
1498 link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
1500 for (i = 0; i < topology->num_nodes; i++)
1501 link_info_output->nodes[i].node_id = topology->nodes[i].node_id;
1503 link_info_output->num_nodes = topology->num_nodes;
1504 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1507 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
1511 for (i = 0; i < topology->num_nodes; i++) {
1512 uint8_t node_num_links = ta_port_num_support ?
1513 link_extend_info_output->nodes[i].num_links : link_info_output->nodes[i].num_links;
1514 /* accumulate num_links on extended data */
1515 if (get_extended_data) {
1516 topology->nodes[i].num_links = topology->nodes[i].num_links + node_num_links;
1518 topology->nodes[i].num_links = (requires_reflection && topology->nodes[i].num_links) ?
1519 topology->nodes[i].num_links : node_num_links;
1521 /* popluate the connected port num info if supported and available */
1522 if (ta_port_num_support && topology->nodes[i].num_links) {
1523 memcpy(topology->nodes[i].port_num, link_extend_info_output->nodes[i].port_num,
1524 sizeof(struct xgmi_connected_port_num) * TA_XGMI__MAX_PORT_NUM);
1527 /* reflect the topology information for bi-directionality */
1528 if (requires_reflection && topology->nodes[i].num_hops)
1529 psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
1536 int psp_xgmi_set_topology_info(struct psp_context *psp,
1538 struct psp_xgmi_topology_info *topology)
1540 struct ta_xgmi_shared_memory *xgmi_cmd;
1541 struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
1544 if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
1547 xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
1548 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
1550 topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
1551 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
1552 topology_info_input->num_nodes = number_devices;
1554 for (i = 0; i < topology_info_input->num_nodes; i++) {
1555 topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
1556 topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
1557 topology_info_input->nodes[i].is_sharing_enabled = 1;
1558 topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
1561 /* Invoke xgmi ta to set topology information */
1562 return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
1566 static void psp_ras_ta_check_status(struct psp_context *psp)
1568 struct ta_ras_shared_memory *ras_cmd =
1569 (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1571 switch (ras_cmd->ras_status) {
1572 case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
1573 dev_warn(psp->adev->dev,
1574 "RAS WARNING: cmd failed due to unsupported ip\n");
1576 case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
1577 dev_warn(psp->adev->dev,
1578 "RAS WARNING: cmd failed due to unsupported error injection\n");
1580 case TA_RAS_STATUS__SUCCESS:
1582 case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
1583 if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
1584 dev_warn(psp->adev->dev,
1585 "RAS WARNING: Inject error to critical region is not allowed\n");
1588 dev_warn(psp->adev->dev,
1589 "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
1594 static int psp_ras_send_cmd(struct psp_context *psp,
1595 enum ras_command cmd_id, void *in, void *out)
1597 struct ta_ras_shared_memory *ras_cmd;
1598 uint32_t cmd = cmd_id;
1604 mutex_lock(&psp->ras_context.mutex);
1605 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1606 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1609 case TA_RAS_COMMAND__ENABLE_FEATURES:
1610 case TA_RAS_COMMAND__DISABLE_FEATURES:
1611 memcpy(&ras_cmd->ras_in_message,
1612 in, sizeof(ras_cmd->ras_in_message));
1614 case TA_RAS_COMMAND__TRIGGER_ERROR:
1615 memcpy(&ras_cmd->ras_in_message.trigger_error,
1616 in, sizeof(ras_cmd->ras_in_message.trigger_error));
1618 case TA_RAS_COMMAND__QUERY_ADDRESS:
1619 memcpy(&ras_cmd->ras_in_message.address,
1620 in, sizeof(ras_cmd->ras_in_message.address));
1623 dev_err(psp->adev->dev, "Invalid ras cmd id: %u\n", cmd);
1628 ras_cmd->cmd_id = cmd;
1629 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1632 case TA_RAS_COMMAND__TRIGGER_ERROR:
1634 memcpy(out, &ras_cmd->ras_status, sizeof(ras_cmd->ras_status));
1636 case TA_RAS_COMMAND__QUERY_ADDRESS:
1637 if (ret || ras_cmd->ras_status || psp->cmd_buf_mem->resp.status)
1641 &ras_cmd->ras_out_message.address,
1642 sizeof(ras_cmd->ras_out_message.address));
1649 mutex_unlock(&psp->ras_context.mutex);
1654 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1656 struct ta_ras_shared_memory *ras_cmd;
1659 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1662 * TODO: bypass the loading in sriov for now
1664 if (amdgpu_sriov_vf(psp->adev))
1667 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
1669 if (amdgpu_ras_intr_triggered())
1672 if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
1673 dev_warn(psp->adev->dev, "RAS: Unsupported Interface\n");
1678 if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
1679 dev_warn(psp->adev->dev, "ECC switch disabled\n");
1681 ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1682 } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
1683 dev_warn(psp->adev->dev,
1684 "RAS internal register access blocked\n");
1686 psp_ras_ta_check_status(psp);
1692 int psp_ras_enable_features(struct psp_context *psp,
1693 union ta_ras_cmd_input *info, bool enable)
1695 enum ras_command cmd_id;
1698 if (!psp->ras_context.context.initialized || !info)
1702 TA_RAS_COMMAND__ENABLE_FEATURES : TA_RAS_COMMAND__DISABLE_FEATURES;
1703 ret = psp_ras_send_cmd(psp, cmd_id, info, NULL);
1710 int psp_ras_terminate(struct psp_context *psp)
1715 * TODO: bypass the terminate in sriov for now
1717 if (amdgpu_sriov_vf(psp->adev))
1720 if (!psp->ras_context.context.initialized)
1723 ret = psp_ta_unload(psp, &psp->ras_context.context);
1725 psp->ras_context.context.initialized = false;
1727 mutex_destroy(&psp->ras_context.mutex);
1732 int psp_ras_initialize(struct psp_context *psp)
1735 uint32_t boot_cfg = 0xFF;
1736 struct amdgpu_device *adev = psp->adev;
1737 struct ta_ras_shared_memory *ras_cmd;
1740 * TODO: bypass the initialize in sriov for now
1742 if (amdgpu_sriov_vf(adev))
1745 if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1746 !adev->psp.ras_context.context.bin_desc.start_addr) {
1747 dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
1751 if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
1752 /* query GECC enablement status from boot config
1753 * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
1755 ret = psp_boot_config_get(adev, &boot_cfg);
1757 dev_warn(adev->dev, "PSP get boot config failed\n");
1759 if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
1761 dev_info(adev->dev, "GECC is disabled\n");
1763 /* disable GECC in next boot cycle if ras is
1764 * disabled by module parameter amdgpu_ras_enable
1765 * and/or amdgpu_ras_mask, or boot_config_get call
1768 ret = psp_boot_config_set(adev, 0);
1770 dev_warn(adev->dev, "PSP set boot config failed\n");
1772 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");
1775 if (boot_cfg == 1) {
1776 dev_info(adev->dev, "GECC is enabled\n");
1778 /* enable GECC in next boot cycle if it is disabled
1779 * in boot config, or force enable GECC if failed to
1780 * get boot configuration
1782 ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
1784 dev_warn(adev->dev, "PSP set boot config failed\n");
1786 dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
1791 psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
1792 psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1794 if (!psp->ras_context.context.mem_context.shared_buf) {
1795 ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
1800 ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1801 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1803 if (amdgpu_ras_is_poison_mode_supported(adev))
1804 ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
1805 if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu)
1806 ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
1807 ras_cmd->ras_in_message.init_flags.xcc_mask =
1809 ras_cmd->ras_in_message.init_flags.channel_dis_num = hweight32(adev->gmc.m_half_use) * 2;
1811 ret = psp_ta_load(psp, &psp->ras_context.context);
1813 if (!ret && !ras_cmd->ras_status) {
1814 psp->ras_context.context.initialized = true;
1815 mutex_init(&psp->ras_context.mutex);
1817 if (ras_cmd->ras_status)
1818 dev_warn(adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
1820 /* fail to load RAS TA */
1821 psp->ras_context.context.initialized = false;
1827 int psp_ras_trigger_error(struct psp_context *psp,
1828 struct ta_ras_trigger_error_input *info, uint32_t instance_mask)
1830 struct amdgpu_device *adev = psp->adev;
1833 uint32_t ras_status = 0;
1835 if (!psp->ras_context.context.initialized || !info)
1838 switch (info->block_id) {
1839 case TA_RAS_BLOCK__GFX:
1840 dev_mask = GET_MASK(GC, instance_mask);
1842 case TA_RAS_BLOCK__SDMA:
1843 dev_mask = GET_MASK(SDMA0, instance_mask);
1845 case TA_RAS_BLOCK__VCN:
1846 case TA_RAS_BLOCK__JPEG:
1847 dev_mask = GET_MASK(VCN, instance_mask);
1850 dev_mask = instance_mask;
1854 /* reuse sub_block_index for backward compatibility */
1855 dev_mask <<= AMDGPU_RAS_INST_SHIFT;
1856 dev_mask &= AMDGPU_RAS_INST_MASK;
1857 info->sub_block_index |= dev_mask;
1859 ret = psp_ras_send_cmd(psp,
1860 TA_RAS_COMMAND__TRIGGER_ERROR, info, &ras_status);
1864 /* If err_event_athub occurs error inject was successful, however
1865 * return status from TA is no long reliable
1867 if (amdgpu_ras_intr_triggered())
1870 if (ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
1872 else if (ras_status)
1878 int psp_ras_query_address(struct psp_context *psp,
1879 struct ta_ras_query_address_input *addr_in,
1880 struct ta_ras_query_address_output *addr_out)
1884 if (!psp->ras_context.context.initialized ||
1885 !addr_in || !addr_out)
1888 ret = psp_ras_send_cmd(psp,
1889 TA_RAS_COMMAND__QUERY_ADDRESS, addr_in, addr_out);
1896 static int psp_hdcp_initialize(struct psp_context *psp)
1901 * TODO: bypass the initialize in sriov for now
1903 if (amdgpu_sriov_vf(psp->adev))
1906 /* bypass hdcp initialization if dmu is harvested */
1907 if (!amdgpu_device_has_display_hardware(psp->adev))
1910 if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1911 !psp->hdcp_context.context.bin_desc.start_addr) {
1912 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
1916 psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
1917 psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1919 if (!psp->hdcp_context.context.mem_context.shared_buf) {
1920 ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1925 ret = psp_ta_load(psp, &psp->hdcp_context.context);
1927 psp->hdcp_context.context.initialized = true;
1928 mutex_init(&psp->hdcp_context.mutex);
1934 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1937 * TODO: bypass the loading in sriov for now
1939 if (amdgpu_sriov_vf(psp->adev))
1942 if (!psp->hdcp_context.context.initialized)
1945 return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1948 static int psp_hdcp_terminate(struct psp_context *psp)
1953 * TODO: bypass the terminate in sriov for now
1955 if (amdgpu_sriov_vf(psp->adev))
1958 if (!psp->hdcp_context.context.initialized)
1961 ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1963 psp->hdcp_context.context.initialized = false;
1970 static int psp_dtm_initialize(struct psp_context *psp)
1975 * TODO: bypass the initialize in sriov for now
1977 if (amdgpu_sriov_vf(psp->adev))
1980 /* bypass dtm initialization if dmu is harvested */
1981 if (!amdgpu_device_has_display_hardware(psp->adev))
1984 if (!psp->dtm_context.context.bin_desc.size_bytes ||
1985 !psp->dtm_context.context.bin_desc.start_addr) {
1986 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1990 psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
1991 psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1993 if (!psp->dtm_context.context.mem_context.shared_buf) {
1994 ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1999 ret = psp_ta_load(psp, &psp->dtm_context.context);
2001 psp->dtm_context.context.initialized = true;
2002 mutex_init(&psp->dtm_context.mutex);
2008 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2011 * TODO: bypass the loading in sriov for now
2013 if (amdgpu_sriov_vf(psp->adev))
2016 if (!psp->dtm_context.context.initialized)
2019 return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
2022 static int psp_dtm_terminate(struct psp_context *psp)
2027 * TODO: bypass the terminate in sriov for now
2029 if (amdgpu_sriov_vf(psp->adev))
2032 if (!psp->dtm_context.context.initialized)
2035 ret = psp_ta_unload(psp, &psp->dtm_context.context);
2037 psp->dtm_context.context.initialized = false;
2044 static int psp_rap_initialize(struct psp_context *psp)
2047 enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
2050 * TODO: bypass the initialize in sriov for now
2052 if (amdgpu_sriov_vf(psp->adev))
2055 if (!psp->rap_context.context.bin_desc.size_bytes ||
2056 !psp->rap_context.context.bin_desc.start_addr) {
2057 dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
2061 psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
2062 psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
2064 if (!psp->rap_context.context.mem_context.shared_buf) {
2065 ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
2070 ret = psp_ta_load(psp, &psp->rap_context.context);
2072 psp->rap_context.context.initialized = true;
2073 mutex_init(&psp->rap_context.mutex);
2077 ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
2078 if (ret || status != TA_RAP_STATUS__SUCCESS) {
2079 psp_rap_terminate(psp);
2080 /* free rap shared memory */
2081 psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
2083 dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
2092 static int psp_rap_terminate(struct psp_context *psp)
2096 if (!psp->rap_context.context.initialized)
2099 ret = psp_ta_unload(psp, &psp->rap_context.context);
2101 psp->rap_context.context.initialized = false;
2106 int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
2108 struct ta_rap_shared_memory *rap_cmd;
2111 if (!psp->rap_context.context.initialized)
2114 if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
2115 ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
2118 mutex_lock(&psp->rap_context.mutex);
2120 rap_cmd = (struct ta_rap_shared_memory *)
2121 psp->rap_context.context.mem_context.shared_buf;
2122 memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
2124 rap_cmd->cmd_id = ta_cmd_id;
2125 rap_cmd->validation_method_id = METHOD_A;
2127 ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
2132 *status = rap_cmd->rap_status;
2135 mutex_unlock(&psp->rap_context.mutex);
2141 /* securedisplay start */
2142 static int psp_securedisplay_initialize(struct psp_context *psp)
2145 struct ta_securedisplay_cmd *securedisplay_cmd;
2148 * TODO: bypass the initialize in sriov for now
2150 if (amdgpu_sriov_vf(psp->adev))
2153 /* bypass securedisplay initialization if dmu is harvested */
2154 if (!amdgpu_device_has_display_hardware(psp->adev))
2157 if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
2158 !psp->securedisplay_context.context.bin_desc.start_addr) {
2159 dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
2163 psp->securedisplay_context.context.mem_context.shared_mem_size =
2164 PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
2165 psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
2167 if (!psp->securedisplay_context.context.initialized) {
2168 ret = psp_ta_init_shared_buf(psp,
2169 &psp->securedisplay_context.context.mem_context);
2174 ret = psp_ta_load(psp, &psp->securedisplay_context.context);
2176 psp->securedisplay_context.context.initialized = true;
2177 mutex_init(&psp->securedisplay_context.mutex);
2181 mutex_lock(&psp->securedisplay_context.mutex);
2183 psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
2184 TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2186 ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2188 mutex_unlock(&psp->securedisplay_context.mutex);
2191 psp_securedisplay_terminate(psp);
2192 /* free securedisplay shared memory */
2193 psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
2194 dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
2198 if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
2199 psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
2200 dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
2201 securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
2202 /* don't try again */
2203 psp->securedisplay_context.context.bin_desc.size_bytes = 0;
2209 static int psp_securedisplay_terminate(struct psp_context *psp)
2214 * TODO:bypass the terminate in sriov for now
2216 if (amdgpu_sriov_vf(psp->adev))
2219 if (!psp->securedisplay_context.context.initialized)
2222 ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2224 psp->securedisplay_context.context.initialized = false;
2229 int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2233 if (!psp->securedisplay_context.context.initialized)
2236 if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2237 ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2240 ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2244 /* SECUREDISPLAY end */
2246 int amdgpu_psp_wait_for_bootloader(struct amdgpu_device *adev)
2248 struct psp_context *psp = &adev->psp;
2251 if (!amdgpu_sriov_vf(adev) && psp->funcs && psp->funcs->wait_for_bootloader != NULL)
2252 ret = psp->funcs->wait_for_bootloader(psp);
2257 bool amdgpu_psp_get_ras_capability(struct psp_context *psp)
2260 psp->funcs->get_ras_capability) {
2261 return psp->funcs->get_ras_capability(psp);
2267 static int psp_hw_start(struct psp_context *psp)
2269 struct amdgpu_device *adev = psp->adev;
2272 if (!amdgpu_sriov_vf(adev)) {
2273 if ((is_psp_fw_valid(psp->kdb)) &&
2274 (psp->funcs->bootloader_load_kdb != NULL)) {
2275 ret = psp_bootloader_load_kdb(psp);
2277 dev_err(adev->dev, "PSP load kdb failed!\n");
2282 if ((is_psp_fw_valid(psp->spl)) &&
2283 (psp->funcs->bootloader_load_spl != NULL)) {
2284 ret = psp_bootloader_load_spl(psp);
2286 dev_err(adev->dev, "PSP load spl failed!\n");
2291 if ((is_psp_fw_valid(psp->sys)) &&
2292 (psp->funcs->bootloader_load_sysdrv != NULL)) {
2293 ret = psp_bootloader_load_sysdrv(psp);
2295 dev_err(adev->dev, "PSP load sys drv failed!\n");
2300 if ((is_psp_fw_valid(psp->soc_drv)) &&
2301 (psp->funcs->bootloader_load_soc_drv != NULL)) {
2302 ret = psp_bootloader_load_soc_drv(psp);
2304 dev_err(adev->dev, "PSP load soc drv failed!\n");
2309 if ((is_psp_fw_valid(psp->intf_drv)) &&
2310 (psp->funcs->bootloader_load_intf_drv != NULL)) {
2311 ret = psp_bootloader_load_intf_drv(psp);
2313 dev_err(adev->dev, "PSP load intf drv failed!\n");
2318 if ((is_psp_fw_valid(psp->dbg_drv)) &&
2319 (psp->funcs->bootloader_load_dbg_drv != NULL)) {
2320 ret = psp_bootloader_load_dbg_drv(psp);
2322 dev_err(adev->dev, "PSP load dbg drv failed!\n");
2327 if ((is_psp_fw_valid(psp->ras_drv)) &&
2328 (psp->funcs->bootloader_load_ras_drv != NULL)) {
2329 ret = psp_bootloader_load_ras_drv(psp);
2331 dev_err(adev->dev, "PSP load ras_drv failed!\n");
2336 if ((is_psp_fw_valid(psp->ipkeymgr_drv)) &&
2337 (psp->funcs->bootloader_load_ipkeymgr_drv != NULL)) {
2338 ret = psp_bootloader_load_ipkeymgr_drv(psp);
2340 dev_err(adev->dev, "PSP load ipkeymgr_drv failed!\n");
2345 if ((is_psp_fw_valid(psp->sos)) &&
2346 (psp->funcs->bootloader_load_sos != NULL)) {
2347 ret = psp_bootloader_load_sos(psp);
2349 dev_err(adev->dev, "PSP load sos failed!\n");
2355 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
2357 dev_err(adev->dev, "PSP create ring failed!\n");
2361 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
2364 if (!psp->boot_time_tmr || psp->autoload_supported) {
2365 ret = psp_tmr_init(psp);
2367 dev_err(adev->dev, "PSP tmr init failed!\n");
2374 * For ASICs with DF Cstate management centralized
2375 * to PMFW, TMR setup should be performed after PMFW
2376 * loaded and before other non-psp firmware loaded.
2378 if (psp->pmfw_centralized_cstate_management) {
2379 ret = psp_load_smu_fw(psp);
2384 if (!psp->boot_time_tmr || !psp->autoload_supported) {
2385 ret = psp_tmr_load(psp);
2387 dev_err(adev->dev, "PSP load tmr failed!\n");
2395 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2396 enum psp_gfx_fw_type *type)
2398 switch (ucode->ucode_id) {
2399 case AMDGPU_UCODE_ID_CAP:
2400 *type = GFX_FW_TYPE_CAP;
2402 case AMDGPU_UCODE_ID_SDMA0:
2403 *type = GFX_FW_TYPE_SDMA0;
2405 case AMDGPU_UCODE_ID_SDMA1:
2406 *type = GFX_FW_TYPE_SDMA1;
2408 case AMDGPU_UCODE_ID_SDMA2:
2409 *type = GFX_FW_TYPE_SDMA2;
2411 case AMDGPU_UCODE_ID_SDMA3:
2412 *type = GFX_FW_TYPE_SDMA3;
2414 case AMDGPU_UCODE_ID_SDMA4:
2415 *type = GFX_FW_TYPE_SDMA4;
2417 case AMDGPU_UCODE_ID_SDMA5:
2418 *type = GFX_FW_TYPE_SDMA5;
2420 case AMDGPU_UCODE_ID_SDMA6:
2421 *type = GFX_FW_TYPE_SDMA6;
2423 case AMDGPU_UCODE_ID_SDMA7:
2424 *type = GFX_FW_TYPE_SDMA7;
2426 case AMDGPU_UCODE_ID_CP_MES:
2427 *type = GFX_FW_TYPE_CP_MES;
2429 case AMDGPU_UCODE_ID_CP_MES_DATA:
2430 *type = GFX_FW_TYPE_MES_STACK;
2432 case AMDGPU_UCODE_ID_CP_MES1:
2433 *type = GFX_FW_TYPE_CP_MES_KIQ;
2435 case AMDGPU_UCODE_ID_CP_MES1_DATA:
2436 *type = GFX_FW_TYPE_MES_KIQ_STACK;
2438 case AMDGPU_UCODE_ID_CP_CE:
2439 *type = GFX_FW_TYPE_CP_CE;
2441 case AMDGPU_UCODE_ID_CP_PFP:
2442 *type = GFX_FW_TYPE_CP_PFP;
2444 case AMDGPU_UCODE_ID_CP_ME:
2445 *type = GFX_FW_TYPE_CP_ME;
2447 case AMDGPU_UCODE_ID_CP_MEC1:
2448 *type = GFX_FW_TYPE_CP_MEC;
2450 case AMDGPU_UCODE_ID_CP_MEC1_JT:
2451 *type = GFX_FW_TYPE_CP_MEC_ME1;
2453 case AMDGPU_UCODE_ID_CP_MEC2:
2454 *type = GFX_FW_TYPE_CP_MEC;
2456 case AMDGPU_UCODE_ID_CP_MEC2_JT:
2457 *type = GFX_FW_TYPE_CP_MEC_ME2;
2459 case AMDGPU_UCODE_ID_RLC_P:
2460 *type = GFX_FW_TYPE_RLC_P;
2462 case AMDGPU_UCODE_ID_RLC_V:
2463 *type = GFX_FW_TYPE_RLC_V;
2465 case AMDGPU_UCODE_ID_RLC_G:
2466 *type = GFX_FW_TYPE_RLC_G;
2468 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2469 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2471 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2472 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2474 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2475 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2477 case AMDGPU_UCODE_ID_RLC_IRAM:
2478 *type = GFX_FW_TYPE_RLC_IRAM;
2480 case AMDGPU_UCODE_ID_RLC_DRAM:
2481 *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
2483 case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
2484 *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
2486 case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
2487 *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
2489 case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
2490 *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
2492 case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
2493 *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
2495 case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
2496 *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
2498 case AMDGPU_UCODE_ID_SMC:
2499 *type = GFX_FW_TYPE_SMU;
2501 case AMDGPU_UCODE_ID_PPTABLE:
2502 *type = GFX_FW_TYPE_PPTABLE;
2504 case AMDGPU_UCODE_ID_UVD:
2505 *type = GFX_FW_TYPE_UVD;
2507 case AMDGPU_UCODE_ID_UVD1:
2508 *type = GFX_FW_TYPE_UVD1;
2510 case AMDGPU_UCODE_ID_VCE:
2511 *type = GFX_FW_TYPE_VCE;
2513 case AMDGPU_UCODE_ID_VCN:
2514 *type = GFX_FW_TYPE_VCN;
2516 case AMDGPU_UCODE_ID_VCN1:
2517 *type = GFX_FW_TYPE_VCN1;
2519 case AMDGPU_UCODE_ID_DMCU_ERAM:
2520 *type = GFX_FW_TYPE_DMCU_ERAM;
2522 case AMDGPU_UCODE_ID_DMCU_INTV:
2523 *type = GFX_FW_TYPE_DMCU_ISR;
2525 case AMDGPU_UCODE_ID_VCN0_RAM:
2526 *type = GFX_FW_TYPE_VCN0_RAM;
2528 case AMDGPU_UCODE_ID_VCN1_RAM:
2529 *type = GFX_FW_TYPE_VCN1_RAM;
2531 case AMDGPU_UCODE_ID_DMCUB:
2532 *type = GFX_FW_TYPE_DMUB;
2534 case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
2535 case AMDGPU_UCODE_ID_SDMA_RS64:
2536 *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
2538 case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
2539 *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
2541 case AMDGPU_UCODE_ID_IMU_I:
2542 *type = GFX_FW_TYPE_IMU_I;
2544 case AMDGPU_UCODE_ID_IMU_D:
2545 *type = GFX_FW_TYPE_IMU_D;
2547 case AMDGPU_UCODE_ID_CP_RS64_PFP:
2548 *type = GFX_FW_TYPE_RS64_PFP;
2550 case AMDGPU_UCODE_ID_CP_RS64_ME:
2551 *type = GFX_FW_TYPE_RS64_ME;
2553 case AMDGPU_UCODE_ID_CP_RS64_MEC:
2554 *type = GFX_FW_TYPE_RS64_MEC;
2556 case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2557 *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2559 case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2560 *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2562 case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2563 *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2565 case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2566 *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2568 case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2569 *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2571 case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2572 *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2574 case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2575 *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2577 case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2578 *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2580 case AMDGPU_UCODE_ID_VPE_CTX:
2581 *type = GFX_FW_TYPE_VPEC_FW1;
2583 case AMDGPU_UCODE_ID_VPE_CTL:
2584 *type = GFX_FW_TYPE_VPEC_FW2;
2586 case AMDGPU_UCODE_ID_VPE:
2587 *type = GFX_FW_TYPE_VPE;
2589 case AMDGPU_UCODE_ID_UMSCH_MM_UCODE:
2590 *type = GFX_FW_TYPE_UMSCH_UCODE;
2592 case AMDGPU_UCODE_ID_UMSCH_MM_DATA:
2593 *type = GFX_FW_TYPE_UMSCH_DATA;
2595 case AMDGPU_UCODE_ID_UMSCH_MM_CMD_BUFFER:
2596 *type = GFX_FW_TYPE_UMSCH_CMD_BUFFER;
2598 case AMDGPU_UCODE_ID_P2S_TABLE:
2599 *type = GFX_FW_TYPE_P2S_TABLE;
2601 case AMDGPU_UCODE_ID_JPEG_RAM:
2602 *type = GFX_FW_TYPE_JPEG_RAM;
2604 case AMDGPU_UCODE_ID_ISP:
2605 *type = GFX_FW_TYPE_ISP;
2607 case AMDGPU_UCODE_ID_MAXIMUM:
2615 static void psp_print_fw_hdr(struct psp_context *psp,
2616 struct amdgpu_firmware_info *ucode)
2618 struct amdgpu_device *adev = psp->adev;
2619 struct common_firmware_header *hdr;
2621 switch (ucode->ucode_id) {
2622 case AMDGPU_UCODE_ID_SDMA0:
2623 case AMDGPU_UCODE_ID_SDMA1:
2624 case AMDGPU_UCODE_ID_SDMA2:
2625 case AMDGPU_UCODE_ID_SDMA3:
2626 case AMDGPU_UCODE_ID_SDMA4:
2627 case AMDGPU_UCODE_ID_SDMA5:
2628 case AMDGPU_UCODE_ID_SDMA6:
2629 case AMDGPU_UCODE_ID_SDMA7:
2630 hdr = (struct common_firmware_header *)
2631 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2632 amdgpu_ucode_print_sdma_hdr(hdr);
2634 case AMDGPU_UCODE_ID_CP_CE:
2635 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2636 amdgpu_ucode_print_gfx_hdr(hdr);
2638 case AMDGPU_UCODE_ID_CP_PFP:
2639 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2640 amdgpu_ucode_print_gfx_hdr(hdr);
2642 case AMDGPU_UCODE_ID_CP_ME:
2643 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2644 amdgpu_ucode_print_gfx_hdr(hdr);
2646 case AMDGPU_UCODE_ID_CP_MEC1:
2647 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2648 amdgpu_ucode_print_gfx_hdr(hdr);
2650 case AMDGPU_UCODE_ID_RLC_G:
2651 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2652 amdgpu_ucode_print_rlc_hdr(hdr);
2654 case AMDGPU_UCODE_ID_SMC:
2655 hdr = (struct common_firmware_header *)adev->pm.fw->data;
2656 amdgpu_ucode_print_smc_hdr(hdr);
2663 static int psp_prep_load_ip_fw_cmd_buf(struct psp_context *psp,
2664 struct amdgpu_firmware_info *ucode,
2665 struct psp_gfx_cmd_resp *cmd)
2668 uint64_t fw_mem_mc_addr = ucode->mc_addr;
2670 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2671 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2672 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2673 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2675 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2677 dev_err(psp->adev->dev, "Unknown firmware type\n");
2682 int psp_execute_ip_fw_load(struct psp_context *psp,
2683 struct amdgpu_firmware_info *ucode)
2686 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
2688 ret = psp_prep_load_ip_fw_cmd_buf(psp, ucode, cmd);
2690 ret = psp_cmd_submit_buf(psp, ucode, cmd,
2691 psp->fence_buf_mc_addr);
2694 release_psp_cmd_buf(psp);
2699 static int psp_load_p2s_table(struct psp_context *psp)
2702 struct amdgpu_device *adev = psp->adev;
2703 struct amdgpu_firmware_info *ucode =
2704 &adev->firmware.ucode[AMDGPU_UCODE_ID_P2S_TABLE];
2706 if (adev->in_runpm && ((adev->pm.rpm_mode == AMDGPU_RUNPM_BACO) ||
2707 (adev->pm.rpm_mode == AMDGPU_RUNPM_BAMACO)))
2710 if (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 6) ||
2711 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(13, 0, 14)) {
2712 uint32_t supp_vers = adev->flags & AMD_IS_APU ? 0x0036013D :
2714 if (psp->sos.fw_version < supp_vers)
2718 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2721 ret = psp_execute_ip_fw_load(psp, ucode);
2726 static int psp_load_smu_fw(struct psp_context *psp)
2729 struct amdgpu_device *adev = psp->adev;
2730 struct amdgpu_firmware_info *ucode =
2731 &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
2732 struct amdgpu_ras *ras = psp->ras_context.ras;
2735 * Skip SMU FW reloading in case of using BACO for runpm only,
2736 * as SMU is always alive.
2738 if (adev->in_runpm && ((adev->pm.rpm_mode == AMDGPU_RUNPM_BACO) ||
2739 (adev->pm.rpm_mode == AMDGPU_RUNPM_BAMACO)))
2742 if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
2745 if ((amdgpu_in_reset(adev) && ras && adev->ras_enabled &&
2746 (amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 4) ||
2747 amdgpu_ip_version(adev, MP0_HWIP, 0) == IP_VERSION(11, 0, 2)))) {
2748 ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2750 dev_err(adev->dev, "Failed to set MP1 state prepare for reload\n");
2753 ret = psp_execute_ip_fw_load(psp, ucode);
2756 dev_err(adev->dev, "PSP load smu failed!\n");
2761 static bool fw_load_skip_check(struct psp_context *psp,
2762 struct amdgpu_firmware_info *ucode)
2764 if (!ucode->fw || !ucode->ucode_size)
2767 if (ucode->ucode_id == AMDGPU_UCODE_ID_P2S_TABLE)
2770 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2771 (psp_smu_reload_quirk(psp) ||
2772 psp->autoload_supported ||
2773 psp->pmfw_centralized_cstate_management))
2776 if (amdgpu_sriov_vf(psp->adev) &&
2777 amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
2780 if (psp->autoload_supported &&
2781 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2782 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2783 /* skip mec JT when autoload is enabled */
2789 int psp_load_fw_list(struct psp_context *psp,
2790 struct amdgpu_firmware_info **ucode_list, int ucode_count)
2793 struct amdgpu_firmware_info *ucode;
2795 for (i = 0; i < ucode_count; ++i) {
2796 ucode = ucode_list[i];
2797 psp_print_fw_hdr(psp, ucode);
2798 ret = psp_execute_ip_fw_load(psp, ucode);
2805 static int psp_load_non_psp_fw(struct psp_context *psp)
2808 struct amdgpu_firmware_info *ucode;
2809 struct amdgpu_device *adev = psp->adev;
2811 if (psp->autoload_supported &&
2812 !psp->pmfw_centralized_cstate_management) {
2813 ret = psp_load_smu_fw(psp);
2818 /* Load P2S table first if it's available */
2819 psp_load_p2s_table(psp);
2821 for (i = 0; i < adev->firmware.max_ucodes; i++) {
2822 ucode = &adev->firmware.ucode[i];
2824 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2825 !fw_load_skip_check(psp, ucode)) {
2826 ret = psp_load_smu_fw(psp);
2832 if (fw_load_skip_check(psp, ucode))
2835 if (psp->autoload_supported &&
2836 (amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2837 IP_VERSION(11, 0, 7) ||
2838 amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2839 IP_VERSION(11, 0, 11) ||
2840 amdgpu_ip_version(adev, MP0_HWIP, 0) ==
2841 IP_VERSION(11, 0, 12)) &&
2842 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2843 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2844 ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2845 /* PSP only receive one SDMA fw for sienna_cichlid,
2846 * as all four sdma fw are same
2850 psp_print_fw_hdr(psp, ucode);
2852 ret = psp_execute_ip_fw_load(psp, ucode);
2856 /* Start rlc autoload after psp recieved all the gfx firmware */
2857 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2858 adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2859 ret = psp_rlc_autoload_start(psp);
2861 dev_err(adev->dev, "Failed to start rlc autoload\n");
2870 static int psp_load_fw(struct amdgpu_device *adev)
2873 struct psp_context *psp = &adev->psp;
2875 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2876 /* should not destroy ring, only stop */
2877 psp_ring_stop(psp, PSP_RING_TYPE__KM);
2879 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
2881 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
2883 dev_err(adev->dev, "PSP ring init failed!\n");
2888 ret = psp_hw_start(psp);
2892 ret = psp_load_non_psp_fw(psp);
2896 ret = psp_asd_initialize(psp);
2898 dev_err(adev->dev, "PSP load asd failed!\n");
2902 ret = psp_rl_load(adev);
2904 dev_err(adev->dev, "PSP load RL failed!\n");
2908 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2909 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2910 ret = psp_xgmi_initialize(psp, false, true);
2911 /* Warning the XGMI seesion initialize failure
2912 * Instead of stop driver initialization
2915 dev_err(psp->adev->dev,
2916 "XGMI: Failed to initialize XGMI session\n");
2921 ret = psp_ras_initialize(psp);
2923 dev_err(psp->adev->dev,
2924 "RAS: Failed to initialize RAS\n");
2926 ret = psp_hdcp_initialize(psp);
2928 dev_err(psp->adev->dev,
2929 "HDCP: Failed to initialize HDCP\n");
2931 ret = psp_dtm_initialize(psp);
2933 dev_err(psp->adev->dev,
2934 "DTM: Failed to initialize DTM\n");
2936 ret = psp_rap_initialize(psp);
2938 dev_err(psp->adev->dev,
2939 "RAP: Failed to initialize RAP\n");
2941 ret = psp_securedisplay_initialize(psp);
2943 dev_err(psp->adev->dev,
2944 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
2950 psp_free_shared_bufs(psp);
2953 * all cleanup jobs (xgmi terminate, ras terminate,
2954 * ring destroy, cmd/fence/fw buffers destory,
2955 * psp->cmd destory) are delayed to psp_hw_fini
2957 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2961 static int psp_hw_init(void *handle)
2964 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2966 mutex_lock(&adev->firmware.mutex);
2968 * This sequence is just used on hw_init only once, no need on
2971 ret = amdgpu_ucode_init_bo(adev);
2975 ret = psp_load_fw(adev);
2977 dev_err(adev->dev, "PSP firmware loading failed\n");
2981 mutex_unlock(&adev->firmware.mutex);
2985 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
2986 mutex_unlock(&adev->firmware.mutex);
2990 static int psp_hw_fini(void *handle)
2992 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2993 struct psp_context *psp = &adev->psp;
2996 psp_ras_terminate(psp);
2997 psp_securedisplay_terminate(psp);
2998 psp_rap_terminate(psp);
2999 psp_dtm_terminate(psp);
3000 psp_hdcp_terminate(psp);
3002 if (adev->gmc.xgmi.num_physical_nodes > 1)
3003 psp_xgmi_terminate(psp);
3006 psp_asd_terminate(psp);
3007 psp_tmr_terminate(psp);
3009 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
3014 static int psp_suspend(void *handle)
3017 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3018 struct psp_context *psp = &adev->psp;
3020 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
3021 psp->xgmi_context.context.initialized) {
3022 ret = psp_xgmi_terminate(psp);
3024 dev_err(adev->dev, "Failed to terminate xgmi ta\n");
3030 ret = psp_ras_terminate(psp);
3032 dev_err(adev->dev, "Failed to terminate ras ta\n");
3035 ret = psp_hdcp_terminate(psp);
3037 dev_err(adev->dev, "Failed to terminate hdcp ta\n");
3040 ret = psp_dtm_terminate(psp);
3042 dev_err(adev->dev, "Failed to terminate dtm ta\n");
3045 ret = psp_rap_terminate(psp);
3047 dev_err(adev->dev, "Failed to terminate rap ta\n");
3050 ret = psp_securedisplay_terminate(psp);
3052 dev_err(adev->dev, "Failed to terminate securedisplay ta\n");
3057 ret = psp_asd_terminate(psp);
3059 dev_err(adev->dev, "Failed to terminate asd\n");
3063 ret = psp_tmr_terminate(psp);
3065 dev_err(adev->dev, "Failed to terminate tmr\n");
3069 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
3071 dev_err(adev->dev, "PSP ring stop failed\n");
3077 static int psp_resume(void *handle)
3080 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
3081 struct psp_context *psp = &adev->psp;
3083 dev_info(adev->dev, "PSP is resuming...\n");
3085 if (psp->mem_train_ctx.enable_mem_training) {
3086 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
3088 dev_err(adev->dev, "Failed to process memory training!\n");
3093 mutex_lock(&adev->firmware.mutex);
3095 ret = psp_hw_start(psp);
3099 ret = psp_load_non_psp_fw(psp);
3103 ret = psp_asd_initialize(psp);
3105 dev_err(adev->dev, "PSP load asd failed!\n");
3109 ret = psp_rl_load(adev);
3111 dev_err(adev->dev, "PSP load RL failed!\n");
3115 if (adev->gmc.xgmi.num_physical_nodes > 1) {
3116 ret = psp_xgmi_initialize(psp, false, true);
3117 /* Warning the XGMI seesion initialize failure
3118 * Instead of stop driver initialization
3121 dev_err(psp->adev->dev,
3122 "XGMI: Failed to initialize XGMI session\n");
3126 ret = psp_ras_initialize(psp);
3128 dev_err(psp->adev->dev,
3129 "RAS: Failed to initialize RAS\n");
3131 ret = psp_hdcp_initialize(psp);
3133 dev_err(psp->adev->dev,
3134 "HDCP: Failed to initialize HDCP\n");
3136 ret = psp_dtm_initialize(psp);
3138 dev_err(psp->adev->dev,
3139 "DTM: Failed to initialize DTM\n");
3141 ret = psp_rap_initialize(psp);
3143 dev_err(psp->adev->dev,
3144 "RAP: Failed to initialize RAP\n");
3146 ret = psp_securedisplay_initialize(psp);
3148 dev_err(psp->adev->dev,
3149 "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
3152 mutex_unlock(&adev->firmware.mutex);
3157 dev_err(adev->dev, "PSP resume failed\n");
3158 mutex_unlock(&adev->firmware.mutex);
3162 int psp_gpu_reset(struct amdgpu_device *adev)
3166 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
3169 mutex_lock(&adev->psp.mutex);
3170 ret = psp_mode1_reset(&adev->psp);
3171 mutex_unlock(&adev->psp.mutex);
3176 int psp_rlc_autoload_start(struct psp_context *psp)
3179 struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
3181 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
3183 ret = psp_cmd_submit_buf(psp, NULL, cmd,
3184 psp->fence_buf_mc_addr);
3186 release_psp_cmd_buf(psp);
3191 int psp_ring_cmd_submit(struct psp_context *psp,
3192 uint64_t cmd_buf_mc_addr,
3193 uint64_t fence_mc_addr,
3196 unsigned int psp_write_ptr_reg = 0;
3197 struct psp_gfx_rb_frame *write_frame;
3198 struct psp_ring *ring = &psp->km_ring;
3199 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
3200 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
3201 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
3202 struct amdgpu_device *adev = psp->adev;
3203 uint32_t ring_size_dw = ring->ring_size / 4;
3204 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
3206 /* KM (GPCOM) prepare write pointer */
3207 psp_write_ptr_reg = psp_ring_get_wptr(psp);
3209 /* Update KM RB frame pointer to new frame */
3210 /* write_frame ptr increments by size of rb_frame in bytes */
3211 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
3212 if ((psp_write_ptr_reg % ring_size_dw) == 0)
3213 write_frame = ring_buffer_start;
3215 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
3216 /* Check invalid write_frame ptr address */
3217 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
3219 "ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
3220 ring_buffer_start, ring_buffer_end, write_frame);
3222 "write_frame is pointing to address out of bounds\n");
3226 /* Initialize KM RB frame */
3227 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
3229 /* Update KM RB frame */
3230 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
3231 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
3232 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
3233 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
3234 write_frame->fence_value = index;
3235 amdgpu_device_flush_hdp(adev, NULL);
3237 /* Update the write Pointer in DWORDs */
3238 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
3239 psp_ring_set_wptr(psp, psp_write_ptr_reg);
3243 int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
3245 struct amdgpu_device *adev = psp->adev;
3246 const struct psp_firmware_header_v1_0 *asd_hdr;
3249 err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, "amdgpu/%s_asd.bin", chip_name);
3253 asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
3254 adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
3255 adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
3256 adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
3257 adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
3258 le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
3261 amdgpu_ucode_release(&adev->psp.asd_fw);
3265 int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
3267 struct amdgpu_device *adev = psp->adev;
3268 const struct psp_firmware_header_v1_0 *toc_hdr;
3271 err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, "amdgpu/%s_toc.bin", chip_name);
3275 toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
3276 adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
3277 adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
3278 adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
3279 adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
3280 le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
3283 amdgpu_ucode_release(&adev->psp.toc_fw);
3287 static int parse_sos_bin_descriptor(struct psp_context *psp,
3288 const struct psp_fw_bin_desc *desc,
3289 const struct psp_firmware_header_v2_0 *sos_hdr)
3291 uint8_t *ucode_start_addr = NULL;
3293 if (!psp || !desc || !sos_hdr)
3296 ucode_start_addr = (uint8_t *)sos_hdr +
3297 le32_to_cpu(desc->offset_bytes) +
3298 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3300 switch (desc->fw_type) {
3301 case PSP_FW_TYPE_PSP_SOS:
3302 psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3303 psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3304 psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3305 psp->sos.start_addr = ucode_start_addr;
3307 case PSP_FW_TYPE_PSP_SYS_DRV:
3308 psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3309 psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3310 psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3311 psp->sys.start_addr = ucode_start_addr;
3313 case PSP_FW_TYPE_PSP_KDB:
3314 psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3315 psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3316 psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3317 psp->kdb.start_addr = ucode_start_addr;
3319 case PSP_FW_TYPE_PSP_TOC:
3320 psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3321 psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3322 psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3323 psp->toc.start_addr = ucode_start_addr;
3325 case PSP_FW_TYPE_PSP_SPL:
3326 psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3327 psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3328 psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3329 psp->spl.start_addr = ucode_start_addr;
3331 case PSP_FW_TYPE_PSP_RL:
3332 psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3333 psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3334 psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3335 psp->rl.start_addr = ucode_start_addr;
3337 case PSP_FW_TYPE_PSP_SOC_DRV:
3338 psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
3339 psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
3340 psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3341 psp->soc_drv.start_addr = ucode_start_addr;
3343 case PSP_FW_TYPE_PSP_INTF_DRV:
3344 psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
3345 psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
3346 psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3347 psp->intf_drv.start_addr = ucode_start_addr;
3349 case PSP_FW_TYPE_PSP_DBG_DRV:
3350 psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
3351 psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
3352 psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3353 psp->dbg_drv.start_addr = ucode_start_addr;
3355 case PSP_FW_TYPE_PSP_RAS_DRV:
3356 psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3357 psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3358 psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3359 psp->ras_drv.start_addr = ucode_start_addr;
3361 case PSP_FW_TYPE_PSP_IPKEYMGR_DRV:
3362 psp->ipkeymgr_drv.fw_version = le32_to_cpu(desc->fw_version);
3363 psp->ipkeymgr_drv.feature_version = le32_to_cpu(desc->fw_version);
3364 psp->ipkeymgr_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3365 psp->ipkeymgr_drv.start_addr = ucode_start_addr;
3368 dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3375 static int psp_init_sos_base_fw(struct amdgpu_device *adev)
3377 const struct psp_firmware_header_v1_0 *sos_hdr;
3378 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3379 uint8_t *ucode_array_start_addr;
3381 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3382 ucode_array_start_addr = (uint8_t *)sos_hdr +
3383 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3385 if (adev->gmc.xgmi.connected_to_cpu ||
3386 (amdgpu_ip_version(adev, MP0_HWIP, 0) != IP_VERSION(13, 0, 2))) {
3387 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3388 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
3390 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3391 adev->psp.sys.start_addr = ucode_array_start_addr;
3393 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3394 adev->psp.sos.start_addr = ucode_array_start_addr +
3395 le32_to_cpu(sos_hdr->sos.offset_bytes);
3397 /* Load alternate PSP SOS FW */
3398 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3400 adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3401 adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3403 adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3404 adev->psp.sys.start_addr = ucode_array_start_addr +
3405 le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
3407 adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3408 adev->psp.sos.start_addr = ucode_array_start_addr +
3409 le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
3412 if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
3413 dev_warn(adev->dev, "PSP SOS FW not available");
3420 int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
3422 struct amdgpu_device *adev = psp->adev;
3423 const struct psp_firmware_header_v1_0 *sos_hdr;
3424 const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
3425 const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
3426 const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3427 const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
3429 uint8_t *ucode_array_start_addr;
3432 err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, "amdgpu/%s_sos.bin", chip_name);
3436 sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3437 ucode_array_start_addr = (uint8_t *)sos_hdr +
3438 le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3439 amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
3441 switch (sos_hdr->header.header_version_major) {
3443 err = psp_init_sos_base_fw(adev);
3447 if (sos_hdr->header.header_version_minor == 1) {
3448 sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3449 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3450 adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3451 le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3452 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3453 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3454 le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
3456 if (sos_hdr->header.header_version_minor == 2) {
3457 sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3458 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3459 adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
3460 le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
3462 if (sos_hdr->header.header_version_minor == 3) {
3463 sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3464 adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3465 adev->psp.toc.start_addr = ucode_array_start_addr +
3466 le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3467 adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3468 adev->psp.kdb.start_addr = ucode_array_start_addr +
3469 le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3470 adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3471 adev->psp.spl.start_addr = ucode_array_start_addr +
3472 le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3473 adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3474 adev->psp.rl.start_addr = ucode_array_start_addr +
3475 le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
3479 sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3481 if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3482 dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3487 for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3488 err = parse_sos_bin_descriptor(psp,
3489 &sos_hdr_v2_0->psp_fw_bin[fw_index],
3497 "unsupported psp sos firmware\n");
3504 amdgpu_ucode_release(&adev->psp.sos_fw);
3509 static int parse_ta_bin_descriptor(struct psp_context *psp,
3510 const struct psp_fw_bin_desc *desc,
3511 const struct ta_firmware_header_v2_0 *ta_hdr)
3513 uint8_t *ucode_start_addr = NULL;
3515 if (!psp || !desc || !ta_hdr)
3518 ucode_start_addr = (uint8_t *)ta_hdr +
3519 le32_to_cpu(desc->offset_bytes) +
3520 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3522 switch (desc->fw_type) {
3523 case TA_FW_TYPE_PSP_ASD:
3524 psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3525 psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3526 psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3527 psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3529 case TA_FW_TYPE_PSP_XGMI:
3530 psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3531 psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3532 psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3534 case TA_FW_TYPE_PSP_RAS:
3535 psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3536 psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3537 psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3539 case TA_FW_TYPE_PSP_HDCP:
3540 psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3541 psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3542 psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3544 case TA_FW_TYPE_PSP_DTM:
3545 psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3546 psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3547 psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3549 case TA_FW_TYPE_PSP_RAP:
3550 psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3551 psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3552 psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
3554 case TA_FW_TYPE_PSP_SECUREDISPLAY:
3555 psp->securedisplay_context.context.bin_desc.fw_version =
3556 le32_to_cpu(desc->fw_version);
3557 psp->securedisplay_context.context.bin_desc.size_bytes =
3558 le32_to_cpu(desc->size_bytes);
3559 psp->securedisplay_context.context.bin_desc.start_addr =
3563 dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3570 static int parse_ta_v1_microcode(struct psp_context *psp)
3572 const struct ta_firmware_header_v1_0 *ta_hdr;
3573 struct amdgpu_device *adev = psp->adev;
3575 ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3577 if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3580 adev->psp.xgmi_context.context.bin_desc.fw_version =
3581 le32_to_cpu(ta_hdr->xgmi.fw_version);
3582 adev->psp.xgmi_context.context.bin_desc.size_bytes =
3583 le32_to_cpu(ta_hdr->xgmi.size_bytes);
3584 adev->psp.xgmi_context.context.bin_desc.start_addr =
3586 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3588 adev->psp.ras_context.context.bin_desc.fw_version =
3589 le32_to_cpu(ta_hdr->ras.fw_version);
3590 adev->psp.ras_context.context.bin_desc.size_bytes =
3591 le32_to_cpu(ta_hdr->ras.size_bytes);
3592 adev->psp.ras_context.context.bin_desc.start_addr =
3593 (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3594 le32_to_cpu(ta_hdr->ras.offset_bytes);
3596 adev->psp.hdcp_context.context.bin_desc.fw_version =
3597 le32_to_cpu(ta_hdr->hdcp.fw_version);
3598 adev->psp.hdcp_context.context.bin_desc.size_bytes =
3599 le32_to_cpu(ta_hdr->hdcp.size_bytes);
3600 adev->psp.hdcp_context.context.bin_desc.start_addr =
3602 le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3604 adev->psp.dtm_context.context.bin_desc.fw_version =
3605 le32_to_cpu(ta_hdr->dtm.fw_version);
3606 adev->psp.dtm_context.context.bin_desc.size_bytes =
3607 le32_to_cpu(ta_hdr->dtm.size_bytes);
3608 adev->psp.dtm_context.context.bin_desc.start_addr =
3609 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3610 le32_to_cpu(ta_hdr->dtm.offset_bytes);
3612 adev->psp.securedisplay_context.context.bin_desc.fw_version =
3613 le32_to_cpu(ta_hdr->securedisplay.fw_version);
3614 adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3615 le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3616 adev->psp.securedisplay_context.context.bin_desc.start_addr =
3617 (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3618 le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3620 adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3625 static int parse_ta_v2_microcode(struct psp_context *psp)
3627 const struct ta_firmware_header_v2_0 *ta_hdr;
3628 struct amdgpu_device *adev = psp->adev;
3632 ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3634 if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3637 if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3638 dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3642 for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3643 err = parse_ta_bin_descriptor(psp,
3644 &ta_hdr->ta_fw_bin[ta_index],
3653 int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3655 const struct common_firmware_header *hdr;
3656 struct amdgpu_device *adev = psp->adev;
3659 err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, "amdgpu/%s_ta.bin", chip_name);
3663 hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3664 switch (le16_to_cpu(hdr->header_version_major)) {
3666 err = parse_ta_v1_microcode(psp);
3669 err = parse_ta_v2_microcode(psp);
3672 dev_err(adev->dev, "unsupported TA header version\n");
3677 amdgpu_ucode_release(&adev->psp.ta_fw);
3682 int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3684 struct amdgpu_device *adev = psp->adev;
3685 const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3686 struct amdgpu_firmware_info *info = NULL;
3689 if (!amdgpu_sriov_vf(adev)) {
3690 dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3694 err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, "amdgpu/%s_cap.bin", chip_name);
3696 if (err == -ENODEV) {
3697 dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3701 dev_err(adev->dev, "fail to initialize cap microcode\n");
3704 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3705 info->ucode_id = AMDGPU_UCODE_ID_CAP;
3706 info->fw = adev->psp.cap_fw;
3707 cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3708 adev->psp.cap_fw->data;
3709 adev->firmware.fw_size += ALIGN(
3710 le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3711 adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3712 adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3713 adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3718 amdgpu_ucode_release(&adev->psp.cap_fw);
3722 static int psp_set_clockgating_state(void *handle,
3723 enum amd_clockgating_state state)
3728 static int psp_set_powergating_state(void *handle,
3729 enum amd_powergating_state state)
3734 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
3735 struct device_attribute *attr,
3738 struct drm_device *ddev = dev_get_drvdata(dev);
3739 struct amdgpu_device *adev = drm_to_adev(ddev);
3743 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3744 dev_info(adev->dev, "PSP block is not ready yet\n.");
3748 mutex_lock(&adev->psp.mutex);
3749 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
3750 mutex_unlock(&adev->psp.mutex);
3753 dev_err(adev->dev, "Failed to read USBC PD FW, err = %d\n", ret);
3757 return sysfs_emit(buf, "%x\n", fw_ver);
3760 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
3761 struct device_attribute *attr,
3765 struct drm_device *ddev = dev_get_drvdata(dev);
3766 struct amdgpu_device *adev = drm_to_adev(ddev);
3768 const struct firmware *usbc_pd_fw;
3769 struct amdgpu_bo *fw_buf_bo = NULL;
3770 uint64_t fw_pri_mc_addr;
3771 void *fw_pri_cpu_addr;
3773 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
3774 dev_err(adev->dev, "PSP block is not ready yet.");
3778 if (!drm_dev_enter(ddev, &idx))
3781 ret = amdgpu_ucode_request(adev, &usbc_pd_fw, "amdgpu/%s", buf);
3785 /* LFB address which is aligned to 1MB boundary per PSP request */
3786 ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
3787 AMDGPU_GEM_DOMAIN_VRAM |
3788 AMDGPU_GEM_DOMAIN_GTT,
3789 &fw_buf_bo, &fw_pri_mc_addr,
3794 memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
3796 mutex_lock(&adev->psp.mutex);
3797 ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
3798 mutex_unlock(&adev->psp.mutex);
3800 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3803 amdgpu_ucode_release(&usbc_pd_fw);
3806 dev_err(adev->dev, "Failed to load USBC PD FW, err = %d", ret);
3814 void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3818 if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3821 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3822 memcpy(psp->fw_pri_buf, start_addr, bin_size);
3829 * Reading from this file will retrieve the USB-C PD firmware version. Writing to
3830 * this file will trigger the update process.
3832 static DEVICE_ATTR(usbc_pd_fw, 0644,
3833 psp_usbc_pd_fw_sysfs_read,
3834 psp_usbc_pd_fw_sysfs_write);
3836 int is_psp_fw_valid(struct psp_bin_desc bin)
3838 return bin.size_bytes;
3841 static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
3842 struct bin_attribute *bin_attr,
3843 char *buffer, loff_t pos, size_t count)
3845 struct device *dev = kobj_to_dev(kobj);
3846 struct drm_device *ddev = dev_get_drvdata(dev);
3847 struct amdgpu_device *adev = drm_to_adev(ddev);
3849 adev->psp.vbflash_done = false;
3851 /* Safeguard against memory drain */
3852 if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
3853 dev_err(adev->dev, "File size cannot exceed %u\n", AMD_VBIOS_FILE_MAX_SIZE_B);
3854 kvfree(adev->psp.vbflash_tmp_buf);
3855 adev->psp.vbflash_tmp_buf = NULL;
3856 adev->psp.vbflash_image_size = 0;
3860 /* TODO Just allocate max for now and optimize to realloc later if needed */
3861 if (!adev->psp.vbflash_tmp_buf) {
3862 adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
3863 if (!adev->psp.vbflash_tmp_buf)
3867 mutex_lock(&adev->psp.mutex);
3868 memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
3869 adev->psp.vbflash_image_size += count;
3870 mutex_unlock(&adev->psp.mutex);
3872 dev_dbg(adev->dev, "IFWI staged for update\n");
3877 static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
3878 struct bin_attribute *bin_attr, char *buffer,
3879 loff_t pos, size_t count)
3881 struct device *dev = kobj_to_dev(kobj);
3882 struct drm_device *ddev = dev_get_drvdata(dev);
3883 struct amdgpu_device *adev = drm_to_adev(ddev);
3884 struct amdgpu_bo *fw_buf_bo = NULL;
3885 uint64_t fw_pri_mc_addr;
3886 void *fw_pri_cpu_addr;
3889 if (adev->psp.vbflash_image_size == 0)
3892 dev_dbg(adev->dev, "PSP IFWI flash process initiated\n");
3894 ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
3895 AMDGPU_GPU_PAGE_SIZE,
3896 AMDGPU_GEM_DOMAIN_VRAM,
3903 memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
3905 mutex_lock(&adev->psp.mutex);
3906 ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
3907 mutex_unlock(&adev->psp.mutex);
3909 amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
3912 kvfree(adev->psp.vbflash_tmp_buf);
3913 adev->psp.vbflash_tmp_buf = NULL;
3914 adev->psp.vbflash_image_size = 0;
3917 dev_err(adev->dev, "Failed to load IFWI, err = %d\n", ret);
3921 dev_dbg(adev->dev, "PSP IFWI flash process done\n");
3927 * Writing to this file will stage an IFWI for update. Reading from this file
3928 * will trigger the update process.
3930 static struct bin_attribute psp_vbflash_bin_attr = {
3931 .attr = {.name = "psp_vbflash", .mode = 0660},
3933 .write = amdgpu_psp_vbflash_write,
3934 .read = amdgpu_psp_vbflash_read,
3938 * DOC: psp_vbflash_status
3939 * The status of the flash process.
3940 * 0: IFWI flash not complete.
3941 * 1: IFWI flash complete.
3943 static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3944 struct device_attribute *attr,
3947 struct drm_device *ddev = dev_get_drvdata(dev);
3948 struct amdgpu_device *adev = drm_to_adev(ddev);
3949 uint32_t vbflash_status;
3951 vbflash_status = psp_vbflash_status(&adev->psp);
3952 if (!adev->psp.vbflash_done)
3954 else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3957 return sysfs_emit(buf, "0x%x\n", vbflash_status);
3959 static DEVICE_ATTR(psp_vbflash_status, 0440, amdgpu_psp_vbflash_status, NULL);
3961 static struct bin_attribute *bin_flash_attrs[] = {
3962 &psp_vbflash_bin_attr,
3966 static struct attribute *flash_attrs[] = {
3967 &dev_attr_psp_vbflash_status.attr,
3968 &dev_attr_usbc_pd_fw.attr,
3972 static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
3974 struct device *dev = kobj_to_dev(kobj);
3975 struct drm_device *ddev = dev_get_drvdata(dev);
3976 struct amdgpu_device *adev = drm_to_adev(ddev);
3978 if (attr == &dev_attr_usbc_pd_fw.attr)
3979 return adev->psp.sup_pd_fw_up ? 0660 : 0;
3981 return adev->psp.sup_ifwi_up ? 0440 : 0;
3984 static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
3985 struct bin_attribute *attr,
3988 struct device *dev = kobj_to_dev(kobj);
3989 struct drm_device *ddev = dev_get_drvdata(dev);
3990 struct amdgpu_device *adev = drm_to_adev(ddev);
3992 return adev->psp.sup_ifwi_up ? 0660 : 0;
3995 const struct attribute_group amdgpu_flash_attr_group = {
3996 .attrs = flash_attrs,
3997 .bin_attrs = bin_flash_attrs,
3998 .is_bin_visible = amdgpu_bin_flash_attr_is_visible,
3999 .is_visible = amdgpu_flash_attr_is_visible,
4002 const struct amd_ip_funcs psp_ip_funcs = {
4004 .early_init = psp_early_init,
4006 .sw_init = psp_sw_init,
4007 .sw_fini = psp_sw_fini,
4008 .hw_init = psp_hw_init,
4009 .hw_fini = psp_hw_fini,
4010 .suspend = psp_suspend,
4011 .resume = psp_resume,
4013 .check_soft_reset = NULL,
4014 .wait_for_idle = NULL,
4016 .set_clockgating_state = psp_set_clockgating_state,
4017 .set_powergating_state = psp_set_powergating_state,
4020 const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
4021 .type = AMD_IP_BLOCK_TYPE_PSP,
4025 .funcs = &psp_ip_funcs,
4028 const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
4029 .type = AMD_IP_BLOCK_TYPE_PSP,
4033 .funcs = &psp_ip_funcs,
4036 const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
4037 .type = AMD_IP_BLOCK_TYPE_PSP,
4041 .funcs = &psp_ip_funcs,
4044 const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
4045 .type = AMD_IP_BLOCK_TYPE_PSP,
4049 .funcs = &psp_ip_funcs,
4052 const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
4053 .type = AMD_IP_BLOCK_TYPE_PSP,
4057 .funcs = &psp_ip_funcs,
4060 const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
4061 .type = AMD_IP_BLOCK_TYPE_PSP,
4065 .funcs = &psp_ip_funcs,
4068 const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
4069 .type = AMD_IP_BLOCK_TYPE_PSP,
4073 .funcs = &psp_ip_funcs,
4076 const struct amdgpu_ip_block_version psp_v14_0_ip_block = {
4077 .type = AMD_IP_BLOCK_TYPE_PSP,
4081 .funcs = &psp_ip_funcs,