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 <linux/dma-mapping.h>
30 #include "amdgpu_psp.h"
31 #include "amdgpu_ucode.h"
32 #include "soc15_common.h"
34 #include "psp_v10_0.h"
35 #include "psp_v11_0.h"
36 #include "psp_v12_0.h"
38 #include "amdgpu_ras.h"
40 static void psp_set_funcs(struct amdgpu_device *adev);
42 static int psp_sysfs_init(struct amdgpu_device *adev);
43 static void psp_sysfs_fini(struct amdgpu_device *adev);
46 * Due to DF Cstate management centralized to PMFW, the firmware
47 * loading sequence will be updated as below:
53 * - Load other non-psp fw
55 * - Load XGMI/RAS/HDCP/DTM TA if any
57 * This new sequence is required for
59 * - Navi12 and onwards
61 static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
63 struct amdgpu_device *adev = psp->adev;
65 psp->pmfw_centralized_cstate_management = false;
67 if (amdgpu_sriov_vf(adev))
70 if (adev->flags & AMD_IS_APU)
73 if ((adev->asic_type == CHIP_ARCTURUS) ||
74 (adev->asic_type >= CHIP_NAVI12))
75 psp->pmfw_centralized_cstate_management = true;
78 static int psp_early_init(void *handle)
80 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
81 struct psp_context *psp = &adev->psp;
85 switch (adev->asic_type) {
88 psp_v3_1_set_psp_funcs(psp);
89 psp->autoload_supported = false;
92 psp_v10_0_set_psp_funcs(psp);
93 psp->autoload_supported = false;
97 psp_v11_0_set_psp_funcs(psp);
98 psp->autoload_supported = false;
103 psp_v11_0_set_psp_funcs(psp);
104 psp->autoload_supported = true;
107 psp_v12_0_set_psp_funcs(psp);
115 psp_check_pmfw_centralized_cstate_management(psp);
120 static int psp_sw_init(void *handle)
122 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
123 struct psp_context *psp = &adev->psp;
126 ret = psp_init_microcode(psp);
128 DRM_ERROR("Failed to load psp firmware!\n");
132 ret = psp_mem_training_init(psp);
134 DRM_ERROR("Failed to initialize memory training!\n");
137 ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
139 DRM_ERROR("Failed to process memory training!\n");
143 if (adev->asic_type == CHIP_NAVI10) {
144 ret= psp_sysfs_init(adev);
153 static int psp_sw_fini(void *handle)
155 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
157 psp_mem_training_fini(&adev->psp);
158 release_firmware(adev->psp.sos_fw);
159 adev->psp.sos_fw = NULL;
160 release_firmware(adev->psp.asd_fw);
161 adev->psp.asd_fw = NULL;
162 if (adev->psp.ta_fw) {
163 release_firmware(adev->psp.ta_fw);
164 adev->psp.ta_fw = NULL;
167 if (adev->asic_type == CHIP_NAVI10)
168 psp_sysfs_fini(adev);
173 int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
174 uint32_t reg_val, uint32_t mask, bool check_changed)
178 struct amdgpu_device *adev = psp->adev;
180 for (i = 0; i < adev->usec_timeout; i++) {
181 val = RREG32(reg_index);
186 if ((val & mask) == reg_val)
196 psp_cmd_submit_buf(struct psp_context *psp,
197 struct amdgpu_firmware_info *ucode,
198 struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
203 bool ras_intr = false;
205 mutex_lock(&psp->mutex);
207 memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
209 memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
211 index = atomic_inc_return(&psp->fence_value);
212 ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
214 atomic_dec(&psp->fence_value);
215 mutex_unlock(&psp->mutex);
219 amdgpu_asic_invalidate_hdp(psp->adev, NULL);
220 while (*((unsigned int *)psp->fence_buf) != index) {
224 * Shouldn't wait for timeout when err_event_athub occurs,
225 * because gpu reset thread triggered and lock resource should
226 * be released for psp resume sequence.
228 ras_intr = amdgpu_ras_intr_triggered();
232 amdgpu_asic_invalidate_hdp(psp->adev, NULL);
235 /* In some cases, psp response status is not 0 even there is no
236 * problem while the command is submitted. Some version of PSP FW
237 * doesn't write 0 to that field.
238 * So here we would like to only print a warning instead of an error
239 * during psp initialization to avoid breaking hw_init and it doesn't
242 if ((psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
244 DRM_WARN("failed to load ucode id (%d) ",
246 DRM_WARN("psp command (0x%X) failed and response status is (0x%X)\n",
247 psp->cmd_buf_mem->cmd_id,
248 psp->cmd_buf_mem->resp.status);
250 mutex_unlock(&psp->mutex);
255 /* get xGMI session id from response buffer */
256 cmd->resp.session_id = psp->cmd_buf_mem->resp.session_id;
259 ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
260 ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
262 mutex_unlock(&psp->mutex);
267 static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
268 struct psp_gfx_cmd_resp *cmd,
269 uint64_t tmr_mc, uint32_t size)
271 if (psp_support_vmr_ring(psp))
272 cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
274 cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
275 cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
276 cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
277 cmd->cmd.cmd_setup_tmr.buf_size = size;
280 static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
281 uint64_t pri_buf_mc, uint32_t size)
283 cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
284 cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
285 cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
286 cmd->cmd.cmd_load_toc.toc_size = size;
289 /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
290 static int psp_load_toc(struct psp_context *psp,
294 struct psp_gfx_cmd_resp *cmd;
296 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
299 /* Copy toc to psp firmware private buffer */
300 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
301 memcpy(psp->fw_pri_buf, psp->toc_start_addr, psp->toc_bin_size);
303 psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc_bin_size);
305 ret = psp_cmd_submit_buf(psp, NULL, cmd,
306 psp->fence_buf_mc_addr);
308 *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
313 /* Set up Trusted Memory Region */
314 static int psp_tmr_init(struct psp_context *psp)
322 * According to HW engineer, they prefer the TMR address be "naturally
323 * aligned" , e.g. the start address be an integer divide of TMR size.
325 * Note: this memory need be reserved till the driver
328 tmr_size = PSP_TMR_SIZE;
330 /* For ASICs support RLC autoload, psp will parse the toc
331 * and calculate the total size of TMR needed */
332 if (!amdgpu_sriov_vf(psp->adev) &&
333 psp->toc_start_addr &&
336 ret = psp_load_toc(psp, &tmr_size);
338 DRM_ERROR("Failed to load toc\n");
343 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
344 ret = amdgpu_bo_create_kernel(psp->adev, tmr_size, PSP_TMR_SIZE,
345 AMDGPU_GEM_DOMAIN_VRAM,
346 &psp->tmr_bo, &psp->tmr_mc_addr, pptr);
351 static int psp_tmr_load(struct psp_context *psp)
354 struct psp_gfx_cmd_resp *cmd;
356 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
360 psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr,
361 amdgpu_bo_size(psp->tmr_bo));
362 DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
363 amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
365 ret = psp_cmd_submit_buf(psp, NULL, cmd,
366 psp->fence_buf_mc_addr);
373 static void psp_prep_asd_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
374 uint64_t asd_mc, uint32_t size)
376 cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
377 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
378 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
379 cmd->cmd.cmd_load_ta.app_len = size;
381 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = 0;
382 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = 0;
383 cmd->cmd.cmd_load_ta.cmd_buf_len = 0;
386 static int psp_asd_load(struct psp_context *psp)
389 struct psp_gfx_cmd_resp *cmd;
391 /* If PSP version doesn't match ASD version, asd loading will be failed.
392 * add workaround to bypass it for sriov now.
393 * TODO: add version check to make it common
395 if (amdgpu_sriov_vf(psp->adev))
398 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
402 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
403 memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
405 psp_prep_asd_load_cmd_buf(cmd, psp->fw_pri_mc_addr,
406 psp->asd_ucode_size);
408 ret = psp_cmd_submit_buf(psp, NULL, cmd,
409 psp->fence_buf_mc_addr);
411 psp->asd_context.asd_initialized = true;
412 psp->asd_context.session_id = cmd->resp.session_id;
420 static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
423 cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
424 cmd->cmd.cmd_unload_ta.session_id = session_id;
427 static int psp_asd_unload(struct psp_context *psp)
430 struct psp_gfx_cmd_resp *cmd;
432 if (amdgpu_sriov_vf(psp->adev))
435 if (!psp->asd_context.asd_initialized)
438 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
442 psp_prep_ta_unload_cmd_buf(cmd, psp->asd_context.session_id);
444 ret = psp_cmd_submit_buf(psp, NULL, cmd,
445 psp->fence_buf_mc_addr);
447 psp->asd_context.asd_initialized = false;
454 static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
455 uint32_t id, uint32_t value)
457 cmd->cmd_id = GFX_CMD_ID_PROG_REG;
458 cmd->cmd.cmd_setup_reg_prog.reg_value = value;
459 cmd->cmd.cmd_setup_reg_prog.reg_id = id;
462 int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
465 struct psp_gfx_cmd_resp *cmd = NULL;
468 if (reg >= PSP_REG_LAST)
471 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
475 psp_prep_reg_prog_cmd_buf(cmd, reg, value);
476 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
482 static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
484 uint32_t ta_bin_size,
485 uint64_t ta_shared_mc,
486 uint32_t ta_shared_size)
488 cmd->cmd_id = GFX_CMD_ID_LOAD_TA;
489 cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
490 cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
491 cmd->cmd.cmd_load_ta.app_len = ta_bin_size;
493 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(ta_shared_mc);
494 cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(ta_shared_mc);
495 cmd->cmd.cmd_load_ta.cmd_buf_len = ta_shared_size;
498 static int psp_xgmi_init_shared_buf(struct psp_context *psp)
503 * Allocate 16k memory aligned to 4k from Frame Buffer (local
504 * physical) for xgmi ta <-> Driver
506 ret = amdgpu_bo_create_kernel(psp->adev, PSP_XGMI_SHARED_MEM_SIZE,
507 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
508 &psp->xgmi_context.xgmi_shared_bo,
509 &psp->xgmi_context.xgmi_shared_mc_addr,
510 &psp->xgmi_context.xgmi_shared_buf);
515 static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
519 cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
520 cmd->cmd.cmd_invoke_cmd.session_id = session_id;
521 cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
524 int psp_ta_invoke(struct psp_context *psp,
529 struct psp_gfx_cmd_resp *cmd;
531 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
535 psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, session_id);
537 ret = psp_cmd_submit_buf(psp, NULL, cmd,
538 psp->fence_buf_mc_addr);
545 static int psp_xgmi_load(struct psp_context *psp)
548 struct psp_gfx_cmd_resp *cmd;
551 * TODO: bypass the loading in sriov for now
554 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
558 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
559 memcpy(psp->fw_pri_buf, psp->ta_xgmi_start_addr, psp->ta_xgmi_ucode_size);
561 psp_prep_ta_load_cmd_buf(cmd,
563 psp->ta_xgmi_ucode_size,
564 psp->xgmi_context.xgmi_shared_mc_addr,
565 PSP_XGMI_SHARED_MEM_SIZE);
567 ret = psp_cmd_submit_buf(psp, NULL, cmd,
568 psp->fence_buf_mc_addr);
571 psp->xgmi_context.initialized = 1;
572 psp->xgmi_context.session_id = cmd->resp.session_id;
580 static int psp_xgmi_unload(struct psp_context *psp)
583 struct psp_gfx_cmd_resp *cmd;
584 struct amdgpu_device *adev = psp->adev;
586 /* XGMI TA unload currently is not supported on Arcturus */
587 if (adev->asic_type == CHIP_ARCTURUS)
591 * TODO: bypass the unloading in sriov for now
594 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
598 psp_prep_ta_unload_cmd_buf(cmd, psp->xgmi_context.session_id);
600 ret = psp_cmd_submit_buf(psp, NULL, cmd,
601 psp->fence_buf_mc_addr);
608 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
610 return psp_ta_invoke(psp, ta_cmd_id, psp->xgmi_context.session_id);
613 int psp_xgmi_terminate(struct psp_context *psp)
617 if (!psp->xgmi_context.initialized)
620 ret = psp_xgmi_unload(psp);
624 psp->xgmi_context.initialized = 0;
626 /* free xgmi shared memory */
627 amdgpu_bo_free_kernel(&psp->xgmi_context.xgmi_shared_bo,
628 &psp->xgmi_context.xgmi_shared_mc_addr,
629 &psp->xgmi_context.xgmi_shared_buf);
634 int psp_xgmi_initialize(struct psp_context *psp)
636 struct ta_xgmi_shared_memory *xgmi_cmd;
639 if (!psp->adev->psp.ta_fw ||
640 !psp->adev->psp.ta_xgmi_ucode_size ||
641 !psp->adev->psp.ta_xgmi_start_addr)
644 if (!psp->xgmi_context.initialized) {
645 ret = psp_xgmi_init_shared_buf(psp);
651 ret = psp_xgmi_load(psp);
655 /* Initialize XGMI session */
656 xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.xgmi_shared_buf);
657 memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
658 xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
660 ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
666 static int psp_ras_init_shared_buf(struct psp_context *psp)
671 * Allocate 16k memory aligned to 4k from Frame Buffer (local
672 * physical) for ras ta <-> Driver
674 ret = amdgpu_bo_create_kernel(psp->adev, PSP_RAS_SHARED_MEM_SIZE,
675 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
676 &psp->ras.ras_shared_bo,
677 &psp->ras.ras_shared_mc_addr,
678 &psp->ras.ras_shared_buf);
683 static int psp_ras_load(struct psp_context *psp)
686 struct psp_gfx_cmd_resp *cmd;
689 * TODO: bypass the loading in sriov for now
691 if (amdgpu_sriov_vf(psp->adev))
694 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
698 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
699 memcpy(psp->fw_pri_buf, psp->ta_ras_start_addr, psp->ta_ras_ucode_size);
701 psp_prep_ta_load_cmd_buf(cmd,
703 psp->ta_ras_ucode_size,
704 psp->ras.ras_shared_mc_addr,
705 PSP_RAS_SHARED_MEM_SIZE);
707 ret = psp_cmd_submit_buf(psp, NULL, cmd,
708 psp->fence_buf_mc_addr);
711 psp->ras.ras_initialized = true;
712 psp->ras.session_id = cmd->resp.session_id;
720 static int psp_ras_unload(struct psp_context *psp)
723 struct psp_gfx_cmd_resp *cmd;
726 * TODO: bypass the unloading in sriov for now
728 if (amdgpu_sriov_vf(psp->adev))
731 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
735 psp_prep_ta_unload_cmd_buf(cmd, psp->ras.session_id);
737 ret = psp_cmd_submit_buf(psp, NULL, cmd,
738 psp->fence_buf_mc_addr);
745 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
748 * TODO: bypass the loading in sriov for now
750 if (amdgpu_sriov_vf(psp->adev))
753 return psp_ta_invoke(psp, ta_cmd_id, psp->ras.session_id);
756 int psp_ras_enable_features(struct psp_context *psp,
757 union ta_ras_cmd_input *info, bool enable)
759 struct ta_ras_shared_memory *ras_cmd;
762 if (!psp->ras.ras_initialized)
765 ras_cmd = (struct ta_ras_shared_memory *)psp->ras.ras_shared_buf;
766 memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
769 ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
771 ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
773 ras_cmd->ras_in_message = *info;
775 ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
779 return ras_cmd->ras_status;
782 static int psp_ras_terminate(struct psp_context *psp)
787 * TODO: bypass the terminate in sriov for now
789 if (amdgpu_sriov_vf(psp->adev))
792 if (!psp->ras.ras_initialized)
795 ret = psp_ras_unload(psp);
799 psp->ras.ras_initialized = false;
801 /* free ras shared memory */
802 amdgpu_bo_free_kernel(&psp->ras.ras_shared_bo,
803 &psp->ras.ras_shared_mc_addr,
804 &psp->ras.ras_shared_buf);
809 static int psp_ras_initialize(struct psp_context *psp)
814 * TODO: bypass the initialize in sriov for now
816 if (amdgpu_sriov_vf(psp->adev))
819 if (!psp->adev->psp.ta_ras_ucode_size ||
820 !psp->adev->psp.ta_ras_start_addr) {
821 dev_info(psp->adev->dev, "RAS: optional ras ta ucode is not available\n");
825 if (!psp->ras.ras_initialized) {
826 ret = psp_ras_init_shared_buf(psp);
831 ret = psp_ras_load(psp);
840 static int psp_hdcp_init_shared_buf(struct psp_context *psp)
845 * Allocate 16k memory aligned to 4k from Frame Buffer (local
846 * physical) for hdcp ta <-> Driver
848 ret = amdgpu_bo_create_kernel(psp->adev, PSP_HDCP_SHARED_MEM_SIZE,
849 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
850 &psp->hdcp_context.hdcp_shared_bo,
851 &psp->hdcp_context.hdcp_shared_mc_addr,
852 &psp->hdcp_context.hdcp_shared_buf);
857 static int psp_hdcp_load(struct psp_context *psp)
860 struct psp_gfx_cmd_resp *cmd;
863 * TODO: bypass the loading in sriov for now
865 if (amdgpu_sriov_vf(psp->adev))
868 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
872 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
873 memcpy(psp->fw_pri_buf, psp->ta_hdcp_start_addr,
874 psp->ta_hdcp_ucode_size);
876 psp_prep_ta_load_cmd_buf(cmd,
878 psp->ta_hdcp_ucode_size,
879 psp->hdcp_context.hdcp_shared_mc_addr,
880 PSP_HDCP_SHARED_MEM_SIZE);
882 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
885 psp->hdcp_context.hdcp_initialized = true;
886 psp->hdcp_context.session_id = cmd->resp.session_id;
893 static int psp_hdcp_initialize(struct psp_context *psp)
898 * TODO: bypass the initialize in sriov for now
900 if (amdgpu_sriov_vf(psp->adev))
903 if (!psp->adev->psp.ta_hdcp_ucode_size ||
904 !psp->adev->psp.ta_hdcp_start_addr) {
905 dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
909 if (!psp->hdcp_context.hdcp_initialized) {
910 ret = psp_hdcp_init_shared_buf(psp);
915 ret = psp_hdcp_load(psp);
922 static int psp_hdcp_unload(struct psp_context *psp)
925 struct psp_gfx_cmd_resp *cmd;
928 * TODO: bypass the unloading in sriov for now
930 if (amdgpu_sriov_vf(psp->adev))
933 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
937 psp_prep_ta_unload_cmd_buf(cmd, psp->hdcp_context.session_id);
939 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
946 int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
949 * TODO: bypass the loading in sriov for now
951 if (amdgpu_sriov_vf(psp->adev))
954 return psp_ta_invoke(psp, ta_cmd_id, psp->hdcp_context.session_id);
957 static int psp_hdcp_terminate(struct psp_context *psp)
962 * TODO: bypass the terminate in sriov for now
964 if (amdgpu_sriov_vf(psp->adev))
967 if (!psp->hdcp_context.hdcp_initialized)
970 ret = psp_hdcp_unload(psp);
974 psp->hdcp_context.hdcp_initialized = false;
976 /* free hdcp shared memory */
977 amdgpu_bo_free_kernel(&psp->hdcp_context.hdcp_shared_bo,
978 &psp->hdcp_context.hdcp_shared_mc_addr,
979 &psp->hdcp_context.hdcp_shared_buf);
986 static int psp_dtm_init_shared_buf(struct psp_context *psp)
991 * Allocate 16k memory aligned to 4k from Frame Buffer (local
992 * physical) for dtm ta <-> Driver
994 ret = amdgpu_bo_create_kernel(psp->adev, PSP_DTM_SHARED_MEM_SIZE,
995 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
996 &psp->dtm_context.dtm_shared_bo,
997 &psp->dtm_context.dtm_shared_mc_addr,
998 &psp->dtm_context.dtm_shared_buf);
1003 static int psp_dtm_load(struct psp_context *psp)
1006 struct psp_gfx_cmd_resp *cmd;
1009 * TODO: bypass the loading in sriov for now
1011 if (amdgpu_sriov_vf(psp->adev))
1014 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1018 memset(psp->fw_pri_buf, 0, PSP_1_MEG);
1019 memcpy(psp->fw_pri_buf, psp->ta_dtm_start_addr, psp->ta_dtm_ucode_size);
1021 psp_prep_ta_load_cmd_buf(cmd,
1022 psp->fw_pri_mc_addr,
1023 psp->ta_dtm_ucode_size,
1024 psp->dtm_context.dtm_shared_mc_addr,
1025 PSP_DTM_SHARED_MEM_SIZE);
1027 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1030 psp->dtm_context.dtm_initialized = true;
1031 psp->dtm_context.session_id = cmd->resp.session_id;
1039 static int psp_dtm_initialize(struct psp_context *psp)
1044 * TODO: bypass the initialize in sriov for now
1046 if (amdgpu_sriov_vf(psp->adev))
1049 if (!psp->adev->psp.ta_dtm_ucode_size ||
1050 !psp->adev->psp.ta_dtm_start_addr) {
1051 dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
1055 if (!psp->dtm_context.dtm_initialized) {
1056 ret = psp_dtm_init_shared_buf(psp);
1061 ret = psp_dtm_load(psp);
1068 static int psp_dtm_unload(struct psp_context *psp)
1071 struct psp_gfx_cmd_resp *cmd;
1074 * TODO: bypass the unloading in sriov for now
1076 if (amdgpu_sriov_vf(psp->adev))
1079 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1083 psp_prep_ta_unload_cmd_buf(cmd, psp->dtm_context.session_id);
1085 ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1092 int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1095 * TODO: bypass the loading in sriov for now
1097 if (amdgpu_sriov_vf(psp->adev))
1100 return psp_ta_invoke(psp, ta_cmd_id, psp->dtm_context.session_id);
1103 static int psp_dtm_terminate(struct psp_context *psp)
1108 * TODO: bypass the terminate in sriov for now
1110 if (amdgpu_sriov_vf(psp->adev))
1113 if (!psp->dtm_context.dtm_initialized)
1116 ret = psp_dtm_unload(psp);
1120 psp->dtm_context.dtm_initialized = false;
1122 /* free hdcp shared memory */
1123 amdgpu_bo_free_kernel(&psp->dtm_context.dtm_shared_bo,
1124 &psp->dtm_context.dtm_shared_mc_addr,
1125 &psp->dtm_context.dtm_shared_buf);
1131 static int psp_hw_start(struct psp_context *psp)
1133 struct amdgpu_device *adev = psp->adev;
1136 if (!amdgpu_sriov_vf(adev)) {
1137 if (psp->kdb_bin_size &&
1138 (psp->funcs->bootloader_load_kdb != NULL)) {
1139 ret = psp_bootloader_load_kdb(psp);
1141 DRM_ERROR("PSP load kdb failed!\n");
1146 ret = psp_bootloader_load_sysdrv(psp);
1148 DRM_ERROR("PSP load sysdrv failed!\n");
1152 ret = psp_bootloader_load_sos(psp);
1154 DRM_ERROR("PSP load sos failed!\n");
1159 ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
1161 DRM_ERROR("PSP create ring failed!\n");
1165 ret = psp_tmr_init(psp);
1167 DRM_ERROR("PSP tmr init failed!\n");
1172 * For those ASICs with DF Cstate management centralized
1173 * to PMFW, TMR setup should be performed after PMFW
1174 * loaded and before other non-psp firmware loaded.
1176 if (!psp->pmfw_centralized_cstate_management) {
1177 ret = psp_tmr_load(psp);
1179 DRM_ERROR("PSP load tmr failed!\n");
1187 static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
1188 enum psp_gfx_fw_type *type)
1190 switch (ucode->ucode_id) {
1191 case AMDGPU_UCODE_ID_SDMA0:
1192 *type = GFX_FW_TYPE_SDMA0;
1194 case AMDGPU_UCODE_ID_SDMA1:
1195 *type = GFX_FW_TYPE_SDMA1;
1197 case AMDGPU_UCODE_ID_SDMA2:
1198 *type = GFX_FW_TYPE_SDMA2;
1200 case AMDGPU_UCODE_ID_SDMA3:
1201 *type = GFX_FW_TYPE_SDMA3;
1203 case AMDGPU_UCODE_ID_SDMA4:
1204 *type = GFX_FW_TYPE_SDMA4;
1206 case AMDGPU_UCODE_ID_SDMA5:
1207 *type = GFX_FW_TYPE_SDMA5;
1209 case AMDGPU_UCODE_ID_SDMA6:
1210 *type = GFX_FW_TYPE_SDMA6;
1212 case AMDGPU_UCODE_ID_SDMA7:
1213 *type = GFX_FW_TYPE_SDMA7;
1215 case AMDGPU_UCODE_ID_CP_CE:
1216 *type = GFX_FW_TYPE_CP_CE;
1218 case AMDGPU_UCODE_ID_CP_PFP:
1219 *type = GFX_FW_TYPE_CP_PFP;
1221 case AMDGPU_UCODE_ID_CP_ME:
1222 *type = GFX_FW_TYPE_CP_ME;
1224 case AMDGPU_UCODE_ID_CP_MEC1:
1225 *type = GFX_FW_TYPE_CP_MEC;
1227 case AMDGPU_UCODE_ID_CP_MEC1_JT:
1228 *type = GFX_FW_TYPE_CP_MEC_ME1;
1230 case AMDGPU_UCODE_ID_CP_MEC2:
1231 *type = GFX_FW_TYPE_CP_MEC;
1233 case AMDGPU_UCODE_ID_CP_MEC2_JT:
1234 *type = GFX_FW_TYPE_CP_MEC_ME2;
1236 case AMDGPU_UCODE_ID_RLC_G:
1237 *type = GFX_FW_TYPE_RLC_G;
1239 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
1240 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
1242 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
1243 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
1245 case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
1246 *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
1248 case AMDGPU_UCODE_ID_SMC:
1249 *type = GFX_FW_TYPE_SMU;
1251 case AMDGPU_UCODE_ID_UVD:
1252 *type = GFX_FW_TYPE_UVD;
1254 case AMDGPU_UCODE_ID_UVD1:
1255 *type = GFX_FW_TYPE_UVD1;
1257 case AMDGPU_UCODE_ID_VCE:
1258 *type = GFX_FW_TYPE_VCE;
1260 case AMDGPU_UCODE_ID_VCN:
1261 *type = GFX_FW_TYPE_VCN;
1263 case AMDGPU_UCODE_ID_VCN1:
1264 *type = GFX_FW_TYPE_VCN1;
1266 case AMDGPU_UCODE_ID_DMCU_ERAM:
1267 *type = GFX_FW_TYPE_DMCU_ERAM;
1269 case AMDGPU_UCODE_ID_DMCU_INTV:
1270 *type = GFX_FW_TYPE_DMCU_ISR;
1272 case AMDGPU_UCODE_ID_VCN0_RAM:
1273 *type = GFX_FW_TYPE_VCN0_RAM;
1275 case AMDGPU_UCODE_ID_VCN1_RAM:
1276 *type = GFX_FW_TYPE_VCN1_RAM;
1278 case AMDGPU_UCODE_ID_DMCUB:
1279 *type = GFX_FW_TYPE_DMUB;
1281 case AMDGPU_UCODE_ID_MAXIMUM:
1289 static void psp_print_fw_hdr(struct psp_context *psp,
1290 struct amdgpu_firmware_info *ucode)
1292 struct amdgpu_device *adev = psp->adev;
1293 struct common_firmware_header *hdr;
1295 switch (ucode->ucode_id) {
1296 case AMDGPU_UCODE_ID_SDMA0:
1297 case AMDGPU_UCODE_ID_SDMA1:
1298 case AMDGPU_UCODE_ID_SDMA2:
1299 case AMDGPU_UCODE_ID_SDMA3:
1300 case AMDGPU_UCODE_ID_SDMA4:
1301 case AMDGPU_UCODE_ID_SDMA5:
1302 case AMDGPU_UCODE_ID_SDMA6:
1303 case AMDGPU_UCODE_ID_SDMA7:
1304 hdr = (struct common_firmware_header *)
1305 adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
1306 amdgpu_ucode_print_sdma_hdr(hdr);
1308 case AMDGPU_UCODE_ID_CP_CE:
1309 hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
1310 amdgpu_ucode_print_gfx_hdr(hdr);
1312 case AMDGPU_UCODE_ID_CP_PFP:
1313 hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
1314 amdgpu_ucode_print_gfx_hdr(hdr);
1316 case AMDGPU_UCODE_ID_CP_ME:
1317 hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
1318 amdgpu_ucode_print_gfx_hdr(hdr);
1320 case AMDGPU_UCODE_ID_CP_MEC1:
1321 hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
1322 amdgpu_ucode_print_gfx_hdr(hdr);
1324 case AMDGPU_UCODE_ID_RLC_G:
1325 hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
1326 amdgpu_ucode_print_rlc_hdr(hdr);
1328 case AMDGPU_UCODE_ID_SMC:
1329 hdr = (struct common_firmware_header *)adev->pm.fw->data;
1330 amdgpu_ucode_print_smc_hdr(hdr);
1337 static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
1338 struct psp_gfx_cmd_resp *cmd)
1341 uint64_t fw_mem_mc_addr = ucode->mc_addr;
1343 memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
1345 cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
1346 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
1347 cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
1348 cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
1350 ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
1352 DRM_ERROR("Unknown firmware type\n");
1357 static int psp_execute_np_fw_load(struct psp_context *psp,
1358 struct amdgpu_firmware_info *ucode)
1362 ret = psp_prep_load_ip_fw_cmd_buf(ucode, psp->cmd);
1366 ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
1367 psp->fence_buf_mc_addr);
1372 static int psp_np_fw_load(struct psp_context *psp)
1375 struct amdgpu_firmware_info *ucode;
1376 struct amdgpu_device* adev = psp->adev;
1378 if (psp->autoload_supported ||
1379 psp->pmfw_centralized_cstate_management) {
1380 ucode = &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
1381 if (!ucode->fw || amdgpu_sriov_vf(adev))
1384 ret = psp_execute_np_fw_load(psp, ucode);
1389 if (psp->pmfw_centralized_cstate_management) {
1390 ret = psp_tmr_load(psp);
1392 DRM_ERROR("PSP load tmr failed!\n");
1398 for (i = 0; i < adev->firmware.max_ucodes; i++) {
1399 ucode = &adev->firmware.ucode[i];
1403 if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
1404 (psp_smu_reload_quirk(psp) ||
1405 psp->autoload_supported ||
1406 psp->pmfw_centralized_cstate_management))
1409 if (amdgpu_sriov_vf(adev) &&
1410 (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
1411 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
1412 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2
1413 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3
1414 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA4
1415 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA5
1416 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA6
1417 || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA7
1418 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G
1419 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
1420 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
1421 || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
1422 || ucode->ucode_id == AMDGPU_UCODE_ID_SMC))
1423 /*skip ucode loading in SRIOV VF */
1426 if (psp->autoload_supported &&
1427 (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
1428 ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
1429 /* skip mec JT when autoload is enabled */
1432 psp_print_fw_hdr(psp, ucode);
1434 ret = psp_execute_np_fw_load(psp, ucode);
1438 /* Start rlc autoload after psp recieved all the gfx firmware */
1439 if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
1440 AMDGPU_UCODE_ID_CP_MEC2 : AMDGPU_UCODE_ID_RLC_G)) {
1441 ret = psp_rlc_autoload(psp);
1443 DRM_ERROR("Failed to start rlc autoload\n");
1448 /* check if firmware loaded sucessfully */
1449 if (!amdgpu_psp_check_fw_loading_status(adev, i))
1457 static int psp_load_fw(struct amdgpu_device *adev)
1460 struct psp_context *psp = &adev->psp;
1462 if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset) {
1463 psp_ring_stop(psp, PSP_RING_TYPE__KM); /* should not destroy ring, only stop */
1467 psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1471 ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
1472 AMDGPU_GEM_DOMAIN_GTT,
1474 &psp->fw_pri_mc_addr,
1479 ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
1480 AMDGPU_GEM_DOMAIN_VRAM,
1482 &psp->fence_buf_mc_addr,
1487 ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
1488 AMDGPU_GEM_DOMAIN_VRAM,
1489 &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
1490 (void **)&psp->cmd_buf_mem);
1494 memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
1496 ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
1498 DRM_ERROR("PSP ring init failed!\n");
1503 ret = psp_hw_start(psp);
1507 ret = psp_np_fw_load(psp);
1511 ret = psp_asd_load(psp);
1513 DRM_ERROR("PSP load asd failed!\n");
1517 if (psp->adev->psp.ta_fw) {
1518 ret = psp_ras_initialize(psp);
1520 dev_err(psp->adev->dev,
1521 "RAS: Failed to initialize RAS\n");
1523 ret = psp_hdcp_initialize(psp);
1525 dev_err(psp->adev->dev,
1526 "HDCP: Failed to initialize HDCP\n");
1528 ret = psp_dtm_initialize(psp);
1530 dev_err(psp->adev->dev,
1531 "DTM: Failed to initialize DTM\n");
1538 * all cleanup jobs (xgmi terminate, ras terminate,
1539 * ring destroy, cmd/fence/fw buffers destory,
1540 * psp->cmd destory) are delayed to psp_hw_fini
1545 static int psp_hw_init(void *handle)
1548 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1550 mutex_lock(&adev->firmware.mutex);
1552 * This sequence is just used on hw_init only once, no need on
1555 ret = amdgpu_ucode_init_bo(adev);
1559 ret = psp_load_fw(adev);
1561 DRM_ERROR("PSP firmware loading failed\n");
1565 mutex_unlock(&adev->firmware.mutex);
1569 adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
1570 mutex_unlock(&adev->firmware.mutex);
1574 static int psp_hw_fini(void *handle)
1576 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1577 struct psp_context *psp = &adev->psp;
1581 if (psp->adev->psp.ta_fw) {
1582 psp_ras_terminate(psp);
1583 psp_dtm_terminate(psp);
1584 psp_hdcp_terminate(psp);
1587 psp_asd_unload(psp);
1589 psp_ring_destroy(psp, PSP_RING_TYPE__KM);
1591 pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
1592 amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
1593 amdgpu_bo_free_kernel(&psp->fw_pri_bo,
1594 &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
1595 amdgpu_bo_free_kernel(&psp->fence_buf_bo,
1596 &psp->fence_buf_mc_addr, &psp->fence_buf);
1597 amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
1598 (void **)&psp->cmd_buf_mem);
1606 static int psp_suspend(void *handle)
1609 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1610 struct psp_context *psp = &adev->psp;
1612 if (adev->gmc.xgmi.num_physical_nodes > 1 &&
1613 psp->xgmi_context.initialized == 1) {
1614 ret = psp_xgmi_terminate(psp);
1616 DRM_ERROR("Failed to terminate xgmi ta\n");
1621 if (psp->adev->psp.ta_fw) {
1622 ret = psp_ras_terminate(psp);
1624 DRM_ERROR("Failed to terminate ras ta\n");
1627 ret = psp_hdcp_terminate(psp);
1629 DRM_ERROR("Failed to terminate hdcp ta\n");
1632 ret = psp_dtm_terminate(psp);
1634 DRM_ERROR("Failed to terminate dtm ta\n");
1639 ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
1641 DRM_ERROR("PSP ring stop failed\n");
1648 static int psp_resume(void *handle)
1651 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1652 struct psp_context *psp = &adev->psp;
1654 DRM_INFO("PSP is resuming...\n");
1656 ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
1658 DRM_ERROR("Failed to process memory training!\n");
1662 mutex_lock(&adev->firmware.mutex);
1664 ret = psp_hw_start(psp);
1668 ret = psp_np_fw_load(psp);
1672 ret = psp_asd_load(psp);
1674 DRM_ERROR("PSP load asd failed!\n");
1678 if (adev->gmc.xgmi.num_physical_nodes > 1) {
1679 ret = psp_xgmi_initialize(psp);
1680 /* Warning the XGMI seesion initialize failure
1681 * Instead of stop driver initialization
1684 dev_err(psp->adev->dev,
1685 "XGMI: Failed to initialize XGMI session\n");
1688 if (psp->adev->psp.ta_fw) {
1689 ret = psp_ras_initialize(psp);
1691 dev_err(psp->adev->dev,
1692 "RAS: Failed to initialize RAS\n");
1694 ret = psp_hdcp_initialize(psp);
1696 dev_err(psp->adev->dev,
1697 "HDCP: Failed to initialize HDCP\n");
1699 ret = psp_dtm_initialize(psp);
1701 dev_err(psp->adev->dev,
1702 "DTM: Failed to initialize DTM\n");
1705 mutex_unlock(&adev->firmware.mutex);
1710 DRM_ERROR("PSP resume failed\n");
1711 mutex_unlock(&adev->firmware.mutex);
1715 int psp_gpu_reset(struct amdgpu_device *adev)
1719 if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
1722 mutex_lock(&adev->psp.mutex);
1723 ret = psp_mode1_reset(&adev->psp);
1724 mutex_unlock(&adev->psp.mutex);
1729 int psp_rlc_autoload_start(struct psp_context *psp)
1732 struct psp_gfx_cmd_resp *cmd;
1734 cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
1738 cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
1740 ret = psp_cmd_submit_buf(psp, NULL, cmd,
1741 psp->fence_buf_mc_addr);
1746 int psp_update_vcn_sram(struct amdgpu_device *adev, int inst_idx,
1747 uint64_t cmd_gpu_addr, int cmd_size)
1749 struct amdgpu_firmware_info ucode = {0};
1751 ucode.ucode_id = inst_idx ? AMDGPU_UCODE_ID_VCN1_RAM :
1752 AMDGPU_UCODE_ID_VCN0_RAM;
1753 ucode.mc_addr = cmd_gpu_addr;
1754 ucode.ucode_size = cmd_size;
1756 return psp_execute_np_fw_load(&adev->psp, &ucode);
1759 int psp_ring_cmd_submit(struct psp_context *psp,
1760 uint64_t cmd_buf_mc_addr,
1761 uint64_t fence_mc_addr,
1764 unsigned int psp_write_ptr_reg = 0;
1765 struct psp_gfx_rb_frame *write_frame;
1766 struct psp_ring *ring = &psp->km_ring;
1767 struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
1768 struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
1769 ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
1770 struct amdgpu_device *adev = psp->adev;
1771 uint32_t ring_size_dw = ring->ring_size / 4;
1772 uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
1774 /* KM (GPCOM) prepare write pointer */
1775 psp_write_ptr_reg = psp_ring_get_wptr(psp);
1777 /* Update KM RB frame pointer to new frame */
1778 /* write_frame ptr increments by size of rb_frame in bytes */
1779 /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
1780 if ((psp_write_ptr_reg % ring_size_dw) == 0)
1781 write_frame = ring_buffer_start;
1783 write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
1784 /* Check invalid write_frame ptr address */
1785 if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
1786 DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
1787 ring_buffer_start, ring_buffer_end, write_frame);
1788 DRM_ERROR("write_frame is pointing to address out of bounds\n");
1792 /* Initialize KM RB frame */
1793 memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
1795 /* Update KM RB frame */
1796 write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
1797 write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
1798 write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
1799 write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
1800 write_frame->fence_value = index;
1801 amdgpu_asic_flush_hdp(adev, NULL);
1803 /* Update the write Pointer in DWORDs */
1804 psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
1805 psp_ring_set_wptr(psp, psp_write_ptr_reg);
1809 static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
1810 enum AMDGPU_UCODE_ID ucode_type)
1812 struct amdgpu_firmware_info *ucode = NULL;
1814 if (!adev->firmware.fw_size)
1817 ucode = &adev->firmware.ucode[ucode_type];
1818 if (!ucode->fw || !ucode->ucode_size)
1821 return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
1824 static int psp_set_clockgating_state(void *handle,
1825 enum amd_clockgating_state state)
1830 static int psp_set_powergating_state(void *handle,
1831 enum amd_powergating_state state)
1836 static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
1837 struct device_attribute *attr,
1840 struct drm_device *ddev = dev_get_drvdata(dev);
1841 struct amdgpu_device *adev = ddev->dev_private;
1845 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
1846 DRM_INFO("PSP block is not ready yet.");
1850 mutex_lock(&adev->psp.mutex);
1851 ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
1852 mutex_unlock(&adev->psp.mutex);
1855 DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
1859 return snprintf(buf, PAGE_SIZE, "%x\n", fw_ver);
1862 static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
1863 struct device_attribute *attr,
1867 struct drm_device *ddev = dev_get_drvdata(dev);
1868 struct amdgpu_device *adev = ddev->dev_private;
1870 dma_addr_t dma_addr;
1873 const struct firmware *usbc_pd_fw;
1875 if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
1876 DRM_INFO("PSP block is not ready yet.");
1880 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
1881 ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
1885 /* We need contiguous physical mem to place the FW for psp to access */
1886 cpu_addr = dma_alloc_coherent(adev->dev, usbc_pd_fw->size, &dma_addr, GFP_KERNEL);
1888 ret = dma_mapping_error(adev->dev, dma_addr);
1892 memcpy_toio(cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
1895 * x86 specific workaround.
1896 * Without it the buffer is invisible in PSP.
1898 * TODO Remove once PSP starts snooping CPU cache
1901 clflush_cache_range(cpu_addr, (usbc_pd_fw->size & ~(L1_CACHE_BYTES - 1)));
1904 mutex_lock(&adev->psp.mutex);
1905 ret = psp_load_usbc_pd_fw(&adev->psp, dma_addr);
1906 mutex_unlock(&adev->psp.mutex);
1909 dma_free_coherent(adev->dev, usbc_pd_fw->size, cpu_addr, dma_addr);
1910 release_firmware(usbc_pd_fw);
1914 DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
1921 static DEVICE_ATTR(usbc_pd_fw, S_IRUGO | S_IWUSR,
1922 psp_usbc_pd_fw_sysfs_read,
1923 psp_usbc_pd_fw_sysfs_write);
1927 const struct amd_ip_funcs psp_ip_funcs = {
1929 .early_init = psp_early_init,
1931 .sw_init = psp_sw_init,
1932 .sw_fini = psp_sw_fini,
1933 .hw_init = psp_hw_init,
1934 .hw_fini = psp_hw_fini,
1935 .suspend = psp_suspend,
1936 .resume = psp_resume,
1938 .check_soft_reset = NULL,
1939 .wait_for_idle = NULL,
1941 .set_clockgating_state = psp_set_clockgating_state,
1942 .set_powergating_state = psp_set_powergating_state,
1945 static int psp_sysfs_init(struct amdgpu_device *adev)
1947 int ret = device_create_file(adev->dev, &dev_attr_usbc_pd_fw);
1950 DRM_ERROR("Failed to create USBC PD FW control file!");
1955 static void psp_sysfs_fini(struct amdgpu_device *adev)
1957 device_remove_file(adev->dev, &dev_attr_usbc_pd_fw);
1960 static const struct amdgpu_psp_funcs psp_funcs = {
1961 .check_fw_loading_status = psp_check_fw_loading_status,
1964 static void psp_set_funcs(struct amdgpu_device *adev)
1966 if (NULL == adev->firmware.funcs)
1967 adev->firmware.funcs = &psp_funcs;
1970 const struct amdgpu_ip_block_version psp_v3_1_ip_block =
1972 .type = AMD_IP_BLOCK_TYPE_PSP,
1976 .funcs = &psp_ip_funcs,
1979 const struct amdgpu_ip_block_version psp_v10_0_ip_block =
1981 .type = AMD_IP_BLOCK_TYPE_PSP,
1985 .funcs = &psp_ip_funcs,
1988 const struct amdgpu_ip_block_version psp_v11_0_ip_block =
1990 .type = AMD_IP_BLOCK_TYPE_PSP,
1994 .funcs = &psp_ip_funcs,
1997 const struct amdgpu_ip_block_version psp_v12_0_ip_block =
1999 .type = AMD_IP_BLOCK_TYPE_PSP,
2003 .funcs = &psp_ip_funcs,