10e5ca0d1SHuang Rui /*
20e5ca0d1SHuang Rui * Copyright 2016 Advanced Micro Devices, Inc.
30e5ca0d1SHuang Rui *
40e5ca0d1SHuang Rui * Permission is hereby granted, free of charge, to any person obtaining a
50e5ca0d1SHuang Rui * copy of this software and associated documentation files (the "Software"),
60e5ca0d1SHuang Rui * to deal in the Software without restriction, including without limitation
70e5ca0d1SHuang Rui * the rights to use, copy, modify, merge, publish, distribute, sublicense,
80e5ca0d1SHuang Rui * and/or sell copies of the Software, and to permit persons to whom the
90e5ca0d1SHuang Rui * Software is furnished to do so, subject to the following conditions:
100e5ca0d1SHuang Rui *
110e5ca0d1SHuang Rui * The above copyright notice and this permission notice shall be included in
120e5ca0d1SHuang Rui * all copies or substantial portions of the Software.
130e5ca0d1SHuang Rui *
140e5ca0d1SHuang Rui * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
150e5ca0d1SHuang Rui * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
160e5ca0d1SHuang Rui * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
170e5ca0d1SHuang Rui * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
180e5ca0d1SHuang Rui * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
190e5ca0d1SHuang Rui * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
200e5ca0d1SHuang Rui * OTHER DEALINGS IN THE SOFTWARE.
210e5ca0d1SHuang Rui *
220e5ca0d1SHuang Rui * Author: Huang Rui
230e5ca0d1SHuang Rui *
240e5ca0d1SHuang Rui */
250e5ca0d1SHuang Rui
260e5ca0d1SHuang Rui #include <linux/firmware.h>
27f89f8c6bSAndrey Grodzovsky #include <drm/drm_drv.h>
28fdf2f6c5SSam Ravnborg
290e5ca0d1SHuang Rui #include "amdgpu.h"
300e5ca0d1SHuang Rui #include "amdgpu_psp.h"
310e5ca0d1SHuang Rui #include "amdgpu_ucode.h"
3244357a1bSJonathan Kim #include "amdgpu_xgmi.h"
330e5ca0d1SHuang Rui #include "soc15_common.h"
340e5ca0d1SHuang Rui #include "psp_v3_1.h"
35c1798b54SHuang Rui #include "psp_v10_0.h"
36654f761cSFeifei Xu #include "psp_v11_0.h"
373188fd07SLang Yu #include "psp_v11_0_8.h"
386a7a0bdbSAaron Liu #include "psp_v12_0.h"
39ee821083SHawking Zhang #include "psp_v13_0.h"
407e8a3ca9SXiaojian Du #include "psp_v13_0_4.h"
410e5ca0d1SHuang Rui
42bff77e86SLe Ma #include "amdgpu_ras.h"
43ecaafb7bSJinzhou Su #include "amdgpu_securedisplay.h"
44c6a11133SHawking Zhang #include "amdgpu_atomfirmware.h"
45bff77e86SLe Ma
468424f2ccSLikun Gao #define AMD_VBIOS_FILE_MAX_SIZE_B (1024*1024*3)
478424f2ccSLikun Gao
4840e611bdSJohn Clements static int psp_load_smu_fw(struct psp_context *psp);
4925c94b33SCandice Li static int psp_rap_terminate(struct psp_context *psp);
5025c94b33SCandice Li static int psp_securedisplay_terminate(struct psp_context *psp);
5140e611bdSJohn Clements
psp_ring_init(struct psp_context * psp,enum psp_ring_type ring_type)52aec3bb3aSAlex Deucher static int psp_ring_init(struct psp_context *psp,
53aec3bb3aSAlex Deucher enum psp_ring_type ring_type)
54aec3bb3aSAlex Deucher {
55aec3bb3aSAlex Deucher int ret = 0;
56aec3bb3aSAlex Deucher struct psp_ring *ring;
57aec3bb3aSAlex Deucher struct amdgpu_device *adev = psp->adev;
58aec3bb3aSAlex Deucher
59aec3bb3aSAlex Deucher ring = &psp->km_ring;
60aec3bb3aSAlex Deucher
61aec3bb3aSAlex Deucher ring->ring_type = ring_type;
62aec3bb3aSAlex Deucher
63aec3bb3aSAlex Deucher /* allocate 4k Page of Local Frame Buffer memory for ring */
64aec3bb3aSAlex Deucher ring->ring_size = 0x1000;
65aec3bb3aSAlex Deucher ret = amdgpu_bo_create_kernel(adev, ring->ring_size, PAGE_SIZE,
6658ab2c08SChristian König AMDGPU_GEM_DOMAIN_VRAM |
6758ab2c08SChristian König AMDGPU_GEM_DOMAIN_GTT,
68aec3bb3aSAlex Deucher &adev->firmware.rbuf,
69aec3bb3aSAlex Deucher &ring->ring_mem_mc_addr,
70aec3bb3aSAlex Deucher (void **)&ring->ring_mem);
71aec3bb3aSAlex Deucher if (ret) {
72aec3bb3aSAlex Deucher ring->ring_size = 0;
73aec3bb3aSAlex Deucher return ret;
74aec3bb3aSAlex Deucher }
75aec3bb3aSAlex Deucher
76aec3bb3aSAlex Deucher return 0;
77aec3bb3aSAlex Deucher }
78aec3bb3aSAlex Deucher
79995da6ccSEvan Quan /*
80995da6ccSEvan Quan * Due to DF Cstate management centralized to PMFW, the firmware
81995da6ccSEvan Quan * loading sequence will be updated as below:
82995da6ccSEvan Quan * - Load KDB
83995da6ccSEvan Quan * - Load SYS_DRV
84995da6ccSEvan Quan * - Load tOS
85995da6ccSEvan Quan * - Load PMFW
86995da6ccSEvan Quan * - Setup TMR
87995da6ccSEvan Quan * - Load other non-psp fw
88995da6ccSEvan Quan * - Load ASD
89995da6ccSEvan Quan * - Load XGMI/RAS/HDCP/DTM TA if any
90995da6ccSEvan Quan *
91995da6ccSEvan Quan * This new sequence is required for
92b335f289SHawking Zhang * - Arcturus and onwards
93995da6ccSEvan Quan */
psp_check_pmfw_centralized_cstate_management(struct psp_context * psp)94995da6ccSEvan Quan static void psp_check_pmfw_centralized_cstate_management(struct psp_context *psp)
95995da6ccSEvan Quan {
96995da6ccSEvan Quan struct amdgpu_device *adev = psp->adev;
97995da6ccSEvan Quan
9882d05736SAlex Deucher if (amdgpu_sriov_vf(adev)) {
99995da6ccSEvan Quan psp->pmfw_centralized_cstate_management = false;
100995da6ccSEvan Quan return;
10182d05736SAlex Deucher }
102995da6ccSEvan Quan
1031d789535SAlex Deucher switch (adev->ip_versions[MP0_HWIP][0]) {
10448737ac4SAlex Deucher case IP_VERSION(11, 0, 0):
10582d05736SAlex Deucher case IP_VERSION(11, 0, 4):
10648737ac4SAlex Deucher case IP_VERSION(11, 0, 5):
10782d05736SAlex Deucher case IP_VERSION(11, 0, 7):
10882d05736SAlex Deucher case IP_VERSION(11, 0, 9):
10982d05736SAlex Deucher case IP_VERSION(11, 0, 11):
11082d05736SAlex Deucher case IP_VERSION(11, 0, 12):
11182d05736SAlex Deucher case IP_VERSION(11, 0, 13):
112911a7504SLikun Gao case IP_VERSION(13, 0, 0):
11382d05736SAlex Deucher case IP_VERSION(13, 0, 2):
114438a937dSChengming Gui case IP_VERSION(13, 0, 7):
115995da6ccSEvan Quan psp->pmfw_centralized_cstate_management = true;
11682d05736SAlex Deucher break;
11782d05736SAlex Deucher default:
11882d05736SAlex Deucher psp->pmfw_centralized_cstate_management = false;
11982d05736SAlex Deucher break;
12082d05736SAlex Deucher }
121995da6ccSEvan Quan }
122995da6ccSEvan Quan
psp_init_sriov_microcode(struct psp_context * psp)1232d39c7aeSMario Limonciello static int psp_init_sriov_microcode(struct psp_context *psp)
1242d39c7aeSMario Limonciello {
1252d39c7aeSMario Limonciello struct amdgpu_device *adev = psp->adev;
1262d39c7aeSMario Limonciello char ucode_prefix[30];
1272d39c7aeSMario Limonciello int ret = 0;
1282d39c7aeSMario Limonciello
1292d39c7aeSMario Limonciello amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
1302d39c7aeSMario Limonciello
1312d39c7aeSMario Limonciello switch (adev->ip_versions[MP0_HWIP][0]) {
1322d39c7aeSMario Limonciello case IP_VERSION(9, 0, 0):
1332d39c7aeSMario Limonciello case IP_VERSION(11, 0, 7):
13462a27480SMario Limonciello case IP_VERSION(11, 0, 9):
1352d39c7aeSMario Limonciello adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
1362d39c7aeSMario Limonciello ret = psp_init_cap_microcode(psp, ucode_prefix);
1372d39c7aeSMario Limonciello break;
1382d39c7aeSMario Limonciello case IP_VERSION(13, 0, 2):
1392d39c7aeSMario Limonciello adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
1402d39c7aeSMario Limonciello ret = psp_init_cap_microcode(psp, ucode_prefix);
1412d39c7aeSMario Limonciello ret &= psp_init_ta_microcode(psp, ucode_prefix);
1422d39c7aeSMario Limonciello break;
1432d39c7aeSMario Limonciello case IP_VERSION(13, 0, 0):
1442d39c7aeSMario Limonciello adev->virt.autoload_ucode_id = 0;
1452d39c7aeSMario Limonciello break;
14663630c9eSGavin Wan case IP_VERSION(13, 0, 6):
14763630c9eSGavin Wan ret = psp_init_cap_microcode(psp, ucode_prefix);
148e24b2fdaSZhigang Luo ret &= psp_init_ta_microcode(psp, ucode_prefix);
14963630c9eSGavin Wan break;
1502d39c7aeSMario Limonciello case IP_VERSION(13, 0, 10):
1512d39c7aeSMario Limonciello adev->virt.autoload_ucode_id = AMDGPU_UCODE_ID_CP_MES1_DATA;
1523cd658deSBill Liu ret = psp_init_cap_microcode(psp, ucode_prefix);
1532d39c7aeSMario Limonciello break;
1542d39c7aeSMario Limonciello default:
1552d39c7aeSMario Limonciello return -EINVAL;
1562d39c7aeSMario Limonciello }
1572d39c7aeSMario Limonciello return ret;
1582d39c7aeSMario Limonciello }
1592d39c7aeSMario Limonciello
psp_early_init(void * handle)1600e5ca0d1SHuang Rui static int psp_early_init(void *handle)
1610e5ca0d1SHuang Rui {
1620e5ca0d1SHuang Rui struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1639d6fea57SAlex Deucher struct psp_context *psp = &adev->psp;
1640e5ca0d1SHuang Rui
1651d789535SAlex Deucher switch (adev->ip_versions[MP0_HWIP][0]) {
16682d05736SAlex Deucher case IP_VERSION(9, 0, 0):
167e7f9ccb4SAlex Deucher psp_v3_1_set_psp_funcs(psp);
1681d1f41cfSHawking Zhang psp->autoload_supported = false;
1690e5ca0d1SHuang Rui break;
17082d05736SAlex Deucher case IP_VERSION(10, 0, 0):
17182d05736SAlex Deucher case IP_VERSION(10, 0, 1):
172e7f9ccb4SAlex Deucher psp_v10_0_set_psp_funcs(psp);
1731d1f41cfSHawking Zhang psp->autoload_supported = false;
174c1798b54SHuang Rui break;
17582d05736SAlex Deucher case IP_VERSION(11, 0, 2):
17682d05736SAlex Deucher case IP_VERSION(11, 0, 4):
1771d1f41cfSHawking Zhang psp_v11_0_set_psp_funcs(psp);
1781d1f41cfSHawking Zhang psp->autoload_supported = false;
1791d1f41cfSHawking Zhang break;
18082d05736SAlex Deucher case IP_VERSION(11, 0, 0):
181e7347f1cSMario Limonciello case IP_VERSION(11, 0, 7):
182e7347f1cSMario Limonciello adev->psp.sup_pd_fw_up = !amdgpu_sriov_vf(adev);
183e7347f1cSMario Limonciello fallthrough;
18482d05736SAlex Deucher case IP_VERSION(11, 0, 5):
18582d05736SAlex Deucher case IP_VERSION(11, 0, 9):
18682d05736SAlex Deucher case IP_VERSION(11, 0, 11):
18782d05736SAlex Deucher case IP_VERSION(11, 5, 0):
18882d05736SAlex Deucher case IP_VERSION(11, 0, 12):
18982d05736SAlex Deucher case IP_VERSION(11, 0, 13):
190654f761cSFeifei Xu psp_v11_0_set_psp_funcs(psp);
1911d1f41cfSHawking Zhang psp->autoload_supported = true;
192654f761cSFeifei Xu break;
19382d05736SAlex Deucher case IP_VERSION(11, 0, 3):
19482d05736SAlex Deucher case IP_VERSION(12, 0, 1):
1956a7a0bdbSAaron Liu psp_v12_0_set_psp_funcs(psp);
1966a7a0bdbSAaron Liu break;
19782d05736SAlex Deucher case IP_VERSION(13, 0, 2):
19855f86c2bSHawking Zhang case IP_VERSION(13, 0, 6):
199ee821083SHawking Zhang psp_v13_0_set_psp_funcs(psp);
200ee821083SHawking Zhang break;
20182d05736SAlex Deucher case IP_VERSION(13, 0, 1):
20282d05736SAlex Deucher case IP_VERSION(13, 0, 3):
203d7fd297cSYifan Zhang case IP_VERSION(13, 0, 5):
204f99a7eb2SPrike Liang case IP_VERSION(13, 0, 8):
2052c83e3fdSTim Huang case IP_VERSION(13, 0, 11):
20614b2760fSLi Ma case IP_VERSION(14, 0, 0):
207903bb18bSAaron Liu psp_v13_0_set_psp_funcs(psp);
208903bb18bSAaron Liu psp->autoload_supported = true;
209903bb18bSAaron Liu break;
21082d05736SAlex Deucher case IP_VERSION(11, 0, 8):
211dfcc3e8cSAlex Deucher if (adev->apu_flags & AMD_APU_IS_CYAN_SKILLFISH2) {
2123188fd07SLang Yu psp_v11_0_8_set_psp_funcs(psp);
2133188fd07SLang Yu psp->autoload_supported = false;
2143188fd07SLang Yu }
2153188fd07SLang Yu break;
216911a7504SLikun Gao case IP_VERSION(13, 0, 0):
217438a937dSChengming Gui case IP_VERSION(13, 0, 7):
218521289d2SMario Limonciello case IP_VERSION(13, 0, 10):
219911a7504SLikun Gao psp_v13_0_set_psp_funcs(psp);
220911a7504SLikun Gao psp->autoload_supported = true;
221e7347f1cSMario Limonciello adev->psp.sup_ifwi_up = !amdgpu_sriov_vf(adev);
222911a7504SLikun Gao break;
2237e8a3ca9SXiaojian Du case IP_VERSION(13, 0, 4):
2247e8a3ca9SXiaojian Du psp_v13_0_4_set_psp_funcs(psp);
2257e8a3ca9SXiaojian Du psp->autoload_supported = true;
2267e8a3ca9SXiaojian Du break;
2270e5ca0d1SHuang Rui default:
2280e5ca0d1SHuang Rui return -EINVAL;
2290e5ca0d1SHuang Rui }
2300e5ca0d1SHuang Rui
2310e5ca0d1SHuang Rui psp->adev = adev;
2320e5ca0d1SHuang Rui
233995da6ccSEvan Quan psp_check_pmfw_centralized_cstate_management(psp);
234995da6ccSEvan Quan
2352d39c7aeSMario Limonciello if (amdgpu_sriov_vf(adev))
2362d39c7aeSMario Limonciello return psp_init_sriov_microcode(psp);
2372d39c7aeSMario Limonciello else
2382d39c7aeSMario Limonciello return psp_init_microcode(psp);
2399d6fea57SAlex Deucher }
2409d6fea57SAlex Deucher
psp_ta_free_shared_buf(struct ta_mem_context * mem_ctx)241e2c34219SAlice Wong void psp_ta_free_shared_buf(struct ta_mem_context *mem_ctx)
242e2c34219SAlice Wong {
243e2c34219SAlice Wong amdgpu_bo_free_kernel(&mem_ctx->shared_bo, &mem_ctx->shared_mc_addr,
244e2c34219SAlice Wong &mem_ctx->shared_buf);
24596e1a88fSAlex Deucher mem_ctx->shared_bo = NULL;
246e2c34219SAlice Wong }
247e2c34219SAlice Wong
psp_free_shared_bufs(struct psp_context * psp)248da40bf8fSAlex Deucher static void psp_free_shared_bufs(struct psp_context *psp)
249da40bf8fSAlex Deucher {
250da40bf8fSAlex Deucher void *tmr_buf;
251da40bf8fSAlex Deucher void **pptr;
252da40bf8fSAlex Deucher
253da40bf8fSAlex Deucher /* free TMR memory buffer */
254da40bf8fSAlex Deucher pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
255da40bf8fSAlex Deucher amdgpu_bo_free_kernel(&psp->tmr_bo, &psp->tmr_mc_addr, pptr);
25696e1a88fSAlex Deucher psp->tmr_bo = NULL;
257da40bf8fSAlex Deucher
258da40bf8fSAlex Deucher /* free xgmi shared memory */
259da40bf8fSAlex Deucher psp_ta_free_shared_buf(&psp->xgmi_context.context.mem_context);
260da40bf8fSAlex Deucher
261da40bf8fSAlex Deucher /* free ras shared memory */
262da40bf8fSAlex Deucher psp_ta_free_shared_buf(&psp->ras_context.context.mem_context);
263da40bf8fSAlex Deucher
264da40bf8fSAlex Deucher /* free hdcp shared memory */
265da40bf8fSAlex Deucher psp_ta_free_shared_buf(&psp->hdcp_context.context.mem_context);
266da40bf8fSAlex Deucher
267da40bf8fSAlex Deucher /* free dtm shared memory */
268da40bf8fSAlex Deucher psp_ta_free_shared_buf(&psp->dtm_context.context.mem_context);
269da40bf8fSAlex Deucher
270da40bf8fSAlex Deucher /* free rap shared memory */
271da40bf8fSAlex Deucher psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
272da40bf8fSAlex Deucher
273da40bf8fSAlex Deucher /* free securedisplay shared memory */
274da40bf8fSAlex Deucher psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
275da40bf8fSAlex Deucher
276da40bf8fSAlex Deucher
277da40bf8fSAlex Deucher }
278da40bf8fSAlex Deucher
psp_memory_training_fini(struct psp_context * psp)279963cee55SLikun Gao static void psp_memory_training_fini(struct psp_context *psp)
280963cee55SLikun Gao {
281963cee55SLikun Gao struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
282963cee55SLikun Gao
283963cee55SLikun Gao ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
284963cee55SLikun Gao kfree(ctx->sys_cache);
285963cee55SLikun Gao ctx->sys_cache = NULL;
286963cee55SLikun Gao }
287963cee55SLikun Gao
psp_memory_training_init(struct psp_context * psp)288963cee55SLikun Gao static int psp_memory_training_init(struct psp_context *psp)
289963cee55SLikun Gao {
290963cee55SLikun Gao int ret;
291963cee55SLikun Gao struct psp_memory_training_context *ctx = &psp->mem_train_ctx;
292963cee55SLikun Gao
293963cee55SLikun Gao if (ctx->init != PSP_MEM_TRAIN_RESERVE_SUCCESS) {
294963cee55SLikun Gao DRM_DEBUG("memory training is not supported!\n");
295963cee55SLikun Gao return 0;
296963cee55SLikun Gao }
297963cee55SLikun Gao
298963cee55SLikun Gao ctx->sys_cache = kzalloc(ctx->train_data_size, GFP_KERNEL);
299963cee55SLikun Gao if (ctx->sys_cache == NULL) {
300963cee55SLikun Gao DRM_ERROR("alloc mem_train_ctx.sys_cache failed!\n");
301963cee55SLikun Gao ret = -ENOMEM;
302963cee55SLikun Gao goto Err_out;
303963cee55SLikun Gao }
304963cee55SLikun Gao
305963cee55SLikun Gao DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
306963cee55SLikun Gao ctx->train_data_size,
307963cee55SLikun Gao ctx->p2c_train_data_offset,
308963cee55SLikun Gao ctx->c2p_train_data_offset);
309963cee55SLikun Gao ctx->init = PSP_MEM_TRAIN_INIT_SUCCESS;
310963cee55SLikun Gao return 0;
311963cee55SLikun Gao
312963cee55SLikun Gao Err_out:
313963cee55SLikun Gao psp_memory_training_fini(psp);
314963cee55SLikun Gao return ret;
315963cee55SLikun Gao }
316963cee55SLikun Gao
3173d689ae4SHawking Zhang /*
3183d689ae4SHawking Zhang * Helper funciton to query psp runtime database entry
3193d689ae4SHawking Zhang *
3203d689ae4SHawking Zhang * @adev: amdgpu_device pointer
3213d689ae4SHawking Zhang * @entry_type: the type of psp runtime database entry
3223d689ae4SHawking Zhang * @db_entry: runtime database entry pointer
3233d689ae4SHawking Zhang *
3243d689ae4SHawking Zhang * Return false if runtime database doesn't exit or entry is invalid
3253d689ae4SHawking Zhang * or true if the specific database entry is found, and copy to @db_entry
3263d689ae4SHawking Zhang */
psp_get_runtime_db_entry(struct amdgpu_device * adev,enum psp_runtime_entry_type entry_type,void * db_entry)3273d689ae4SHawking Zhang static bool psp_get_runtime_db_entry(struct amdgpu_device *adev,
3283d689ae4SHawking Zhang enum psp_runtime_entry_type entry_type,
3293d689ae4SHawking Zhang void *db_entry)
3303d689ae4SHawking Zhang {
3313d689ae4SHawking Zhang uint64_t db_header_pos, db_dir_pos;
3323d689ae4SHawking Zhang struct psp_runtime_data_header db_header = {0};
3333d689ae4SHawking Zhang struct psp_runtime_data_directory db_dir = {0};
3343d689ae4SHawking Zhang bool ret = false;
3353d689ae4SHawking Zhang int i;
3363d689ae4SHawking Zhang
337cbd442ceSLijo Lazar if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6))
338cbd442ceSLijo Lazar return false;
339cbd442ceSLijo Lazar
3403d689ae4SHawking Zhang db_header_pos = adev->gmc.mc_vram_size - PSP_RUNTIME_DB_OFFSET;
3413d689ae4SHawking Zhang db_dir_pos = db_header_pos + sizeof(struct psp_runtime_data_header);
3423d689ae4SHawking Zhang
3433d689ae4SHawking Zhang /* read runtime db header from vram */
3443d689ae4SHawking Zhang amdgpu_device_vram_access(adev, db_header_pos, (uint32_t *)&db_header,
3453d689ae4SHawking Zhang sizeof(struct psp_runtime_data_header), false);
3463d689ae4SHawking Zhang
3473d689ae4SHawking Zhang if (db_header.cookie != PSP_RUNTIME_DB_COOKIE_ID) {
3483d689ae4SHawking Zhang /* runtime db doesn't exist, exit */
34996b810d8SMario Limonciello dev_dbg(adev->dev, "PSP runtime database doesn't exist\n");
3503d689ae4SHawking Zhang return false;
3513d689ae4SHawking Zhang }
3523d689ae4SHawking Zhang
3533d689ae4SHawking Zhang /* read runtime database entry from vram */
3543d689ae4SHawking Zhang amdgpu_device_vram_access(adev, db_dir_pos, (uint32_t *)&db_dir,
3553d689ae4SHawking Zhang sizeof(struct psp_runtime_data_directory), false);
3563d689ae4SHawking Zhang
3573d689ae4SHawking Zhang if (db_dir.entry_count >= PSP_RUNTIME_DB_DIAG_ENTRY_MAX_COUNT) {
3583d689ae4SHawking Zhang /* invalid db entry count, exit */
3593d689ae4SHawking Zhang dev_warn(adev->dev, "Invalid PSP runtime database entry count\n");
3603d689ae4SHawking Zhang return false;
3613d689ae4SHawking Zhang }
3623d689ae4SHawking Zhang
3633d689ae4SHawking Zhang /* look up for requested entry type */
3643d689ae4SHawking Zhang for (i = 0; i < db_dir.entry_count && !ret; i++) {
3653d689ae4SHawking Zhang if (db_dir.entry_list[i].entry_type == entry_type) {
3663d689ae4SHawking Zhang switch (entry_type) {
3673d689ae4SHawking Zhang case PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG:
3683d689ae4SHawking Zhang if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_boot_cfg_entry)) {
3693d689ae4SHawking Zhang /* invalid db entry size */
3707f318f4eSLikun Gao dev_warn(adev->dev, "Invalid PSP runtime database boot cfg entry size\n");
3713d689ae4SHawking Zhang return false;
3723d689ae4SHawking Zhang }
3733d689ae4SHawking Zhang /* read runtime database entry */
3743d689ae4SHawking Zhang amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
3753d689ae4SHawking Zhang (uint32_t *)db_entry, sizeof(struct psp_runtime_boot_cfg_entry), false);
3763d689ae4SHawking Zhang ret = true;
3773d689ae4SHawking Zhang break;
3787f318f4eSLikun Gao case PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS:
3797f318f4eSLikun Gao if (db_dir.entry_list[i].size < sizeof(struct psp_runtime_scpm_entry)) {
3807f318f4eSLikun Gao /* invalid db entry size */
3817f318f4eSLikun Gao dev_warn(adev->dev, "Invalid PSP runtime database scpm entry size\n");
3827f318f4eSLikun Gao return false;
3837f318f4eSLikun Gao }
3847f318f4eSLikun Gao /* read runtime database entry */
3857f318f4eSLikun Gao amdgpu_device_vram_access(adev, db_header_pos + db_dir.entry_list[i].offset,
3867f318f4eSLikun Gao (uint32_t *)db_entry, sizeof(struct psp_runtime_scpm_entry), false);
3877f318f4eSLikun Gao ret = true;
3887f318f4eSLikun Gao break;
3893d689ae4SHawking Zhang default:
3903d689ae4SHawking Zhang ret = false;
3913d689ae4SHawking Zhang break;
3923d689ae4SHawking Zhang }
3933d689ae4SHawking Zhang }
3943d689ae4SHawking Zhang }
3953d689ae4SHawking Zhang
3963d689ae4SHawking Zhang return ret;
3973d689ae4SHawking Zhang }
3983d689ae4SHawking Zhang
psp_sw_init(void * handle)3999d6fea57SAlex Deucher static int psp_sw_init(void *handle)
4009d6fea57SAlex Deucher {
4019d6fea57SAlex Deucher struct amdgpu_device *adev = (struct amdgpu_device *)handle;
4029d6fea57SAlex Deucher struct psp_context *psp = &adev->psp;
4039d6fea57SAlex Deucher int ret;
4048e6e054dSHawking Zhang struct psp_runtime_boot_cfg_entry boot_cfg_entry;
4053a07101bSHawking Zhang struct psp_memory_training_context *mem_training_ctx = &psp->mem_train_ctx;
4067f318f4eSLikun Gao struct psp_runtime_scpm_entry scpm_entry;
4079d6fea57SAlex Deucher
4084fb93071SCandice Li psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
4094fb93071SCandice Li if (!psp->cmd) {
4104fb93071SCandice Li DRM_ERROR("Failed to allocate memory to command buffer!\n");
4114fb93071SCandice Li ret = -ENOMEM;
4124fb93071SCandice Li }
4134fb93071SCandice Li
4146f172ae5SJonathan Kim adev->psp.xgmi_context.supports_extended_data =
4156f172ae5SJonathan Kim !adev->gmc.xgmi.connected_to_cpu &&
4166f172ae5SJonathan Kim adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2);
4176f172ae5SJonathan Kim
4187f318f4eSLikun Gao memset(&scpm_entry, 0, sizeof(scpm_entry));
4197f318f4eSLikun Gao if ((psp_get_runtime_db_entry(adev,
4207f318f4eSLikun Gao PSP_RUNTIME_ENTRY_TYPE_PPTABLE_ERR_STATUS,
4217f318f4eSLikun Gao &scpm_entry)) &&
422f14c8c3eSSrinivasan Shanmugam (scpm_entry.scpm_status != SCPM_DISABLE)) {
4237f318f4eSLikun Gao adev->scpm_enabled = true;
4247f318f4eSLikun Gao adev->scpm_status = scpm_entry.scpm_status;
4257f318f4eSLikun Gao } else {
4267f318f4eSLikun Gao adev->scpm_enabled = false;
4277f318f4eSLikun Gao adev->scpm_status = SCPM_DISABLE;
4287f318f4eSLikun Gao }
4297f318f4eSLikun Gao
4307f318f4eSLikun Gao /* TODO: stop gpu driver services and print alarm if scpm is enabled with error status */
4317f318f4eSLikun Gao
4328e6e054dSHawking Zhang memset(&boot_cfg_entry, 0, sizeof(boot_cfg_entry));
4338e6e054dSHawking Zhang if (psp_get_runtime_db_entry(adev,
4348e6e054dSHawking Zhang PSP_RUNTIME_ENTRY_TYPE_BOOT_CONFIG,
4353a07101bSHawking Zhang &boot_cfg_entry)) {
4368e6e054dSHawking Zhang psp->boot_cfg_bitmask = boot_cfg_entry.boot_cfg_bitmask;
4373a07101bSHawking Zhang if ((psp->boot_cfg_bitmask) &
4383a07101bSHawking Zhang BOOT_CFG_FEATURE_TWO_STAGE_DRAM_TRAINING) {
4393a07101bSHawking Zhang /* If psp runtime database exists, then
4403a07101bSHawking Zhang * only enable two stage memory training
4413a07101bSHawking Zhang * when TWO_STAGE_DRAM_TRAINING bit is set
4422d5c0415SPraful Swarnakar * in runtime database
4432d5c0415SPraful Swarnakar */
4443a07101bSHawking Zhang mem_training_ctx->enable_mem_training = true;
4453a07101bSHawking Zhang }
4468e6e054dSHawking Zhang
4473a07101bSHawking Zhang } else {
4482d5c0415SPraful Swarnakar /* If psp runtime database doesn't exist or is
4492d5c0415SPraful Swarnakar * invalid, force enable two stage memory training
4502d5c0415SPraful Swarnakar */
4513a07101bSHawking Zhang mem_training_ctx->enable_mem_training = true;
4523a07101bSHawking Zhang }
4533a07101bSHawking Zhang
4543a07101bSHawking Zhang if (mem_training_ctx->enable_mem_training) {
455963cee55SLikun Gao ret = psp_memory_training_init(psp);
4560586a059STianci.Yin if (ret) {
457d5e5c1bcSColin Ian King DRM_ERROR("Failed to initialize memory training!\n");
4580586a059STianci.Yin return ret;
4590586a059STianci.Yin }
4603a07101bSHawking Zhang
4610586a059STianci.Yin ret = psp_mem_training(psp, PSP_MEM_TRAIN_COLD_BOOT);
4620586a059STianci.Yin if (ret) {
4630586a059STianci.Yin DRM_ERROR("Failed to process memory training!\n");
4640586a059STianci.Yin return ret;
4650586a059STianci.Yin }
4663a07101bSHawking Zhang }
4670586a059STianci.Yin
468b95b5391SAlex Deucher ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
469b95b5391SAlex Deucher amdgpu_sriov_vf(adev) ?
470b95b5391SAlex Deucher AMDGPU_GEM_DOMAIN_VRAM : AMDGPU_GEM_DOMAIN_GTT,
471b95b5391SAlex Deucher &psp->fw_pri_bo,
472b95b5391SAlex Deucher &psp->fw_pri_mc_addr,
473b95b5391SAlex Deucher &psp->fw_pri_buf);
474b95b5391SAlex Deucher if (ret)
475b95b5391SAlex Deucher return ret;
476b95b5391SAlex Deucher
477b95b5391SAlex Deucher ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
478228ce176SRajneesh Bhardwaj AMDGPU_GEM_DOMAIN_VRAM |
479228ce176SRajneesh Bhardwaj AMDGPU_GEM_DOMAIN_GTT,
480b95b5391SAlex Deucher &psp->fence_buf_bo,
481b95b5391SAlex Deucher &psp->fence_buf_mc_addr,
482b95b5391SAlex Deucher &psp->fence_buf);
483b95b5391SAlex Deucher if (ret)
484b95b5391SAlex Deucher goto failed1;
485b95b5391SAlex Deucher
486b95b5391SAlex Deucher ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
487228ce176SRajneesh Bhardwaj AMDGPU_GEM_DOMAIN_VRAM |
488228ce176SRajneesh Bhardwaj AMDGPU_GEM_DOMAIN_GTT,
489b95b5391SAlex Deucher &psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
490b95b5391SAlex Deucher (void **)&psp->cmd_buf_mem);
491b95b5391SAlex Deucher if (ret)
492b95b5391SAlex Deucher goto failed2;
493b95b5391SAlex Deucher
4940e5ca0d1SHuang Rui return 0;
495b95b5391SAlex Deucher
496b95b5391SAlex Deucher failed2:
497b95b5391SAlex Deucher amdgpu_bo_free_kernel(&psp->fence_buf_bo,
498b95b5391SAlex Deucher &psp->fence_buf_mc_addr, &psp->fence_buf);
4996b4cf4a3SMario Limonciello failed1:
5006b4cf4a3SMario Limonciello amdgpu_bo_free_kernel(&psp->fw_pri_bo,
5016b4cf4a3SMario Limonciello &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
502b95b5391SAlex Deucher return ret;
5030e5ca0d1SHuang Rui }
5040e5ca0d1SHuang Rui
psp_sw_fini(void * handle)5050e5ca0d1SHuang Rui static int psp_sw_fini(void *handle)
5060e5ca0d1SHuang Rui {
507c833d8aaSMonk Liu struct amdgpu_device *adev = (struct amdgpu_device *)handle;
5084fb93071SCandice Li struct psp_context *psp = &adev->psp;
5094fb93071SCandice Li struct psp_gfx_cmd_resp *cmd = psp->cmd;
510c833d8aaSMonk Liu
5114fb93071SCandice Li psp_memory_training_fini(psp);
5125a2a19b7SYushan Zhou
51307dbfc6bSMario Limonciello amdgpu_ucode_release(&psp->sos_fw);
51407dbfc6bSMario Limonciello amdgpu_ucode_release(&psp->asd_fw);
51507dbfc6bSMario Limonciello amdgpu_ucode_release(&psp->ta_fw);
51607dbfc6bSMario Limonciello amdgpu_ucode_release(&psp->cap_fw);
51707dbfc6bSMario Limonciello amdgpu_ucode_release(&psp->toc_fw);
5185a2a19b7SYushan Zhou
5194fb93071SCandice Li kfree(cmd);
5204fb93071SCandice Li cmd = NULL;
5214fb93071SCandice Li
52201382501SLonglong Yao psp_free_shared_bufs(psp);
52301382501SLonglong Yao
52483d29a5fSYiPeng Chai if (psp->km_ring.ring_mem)
52583d29a5fSYiPeng Chai amdgpu_bo_free_kernel(&adev->firmware.rbuf,
52683d29a5fSYiPeng Chai &psp->km_ring.ring_mem_mc_addr,
52783d29a5fSYiPeng Chai (void **)&psp->km_ring.ring_mem);
52883d29a5fSYiPeng Chai
529b95b5391SAlex Deucher amdgpu_bo_free_kernel(&psp->fw_pri_bo,
530b95b5391SAlex Deucher &psp->fw_pri_mc_addr, &psp->fw_pri_buf);
531b95b5391SAlex Deucher amdgpu_bo_free_kernel(&psp->fence_buf_bo,
532b95b5391SAlex Deucher &psp->fence_buf_mc_addr, &psp->fence_buf);
533b95b5391SAlex Deucher amdgpu_bo_free_kernel(&psp->cmd_buf_bo, &psp->cmd_buf_mc_addr,
534b95b5391SAlex Deucher (void **)&psp->cmd_buf_mem);
535b95b5391SAlex Deucher
5360e5ca0d1SHuang Rui return 0;
5370e5ca0d1SHuang Rui }
5380e5ca0d1SHuang Rui
psp_wait_for(struct psp_context * psp,uint32_t reg_index,uint32_t reg_val,uint32_t mask,bool check_changed)5390e5ca0d1SHuang Rui int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
5400e5ca0d1SHuang Rui uint32_t reg_val, uint32_t mask, bool check_changed)
5410e5ca0d1SHuang Rui {
5420e5ca0d1SHuang Rui uint32_t val;
5430e5ca0d1SHuang Rui int i;
5440e5ca0d1SHuang Rui struct amdgpu_device *adev = psp->adev;
5450e5ca0d1SHuang Rui
5467afefb81SAndrey Grodzovsky if (psp->adev->no_hw_access)
547bf36b52eSAndrey Grodzovsky return 0;
548bf36b52eSAndrey Grodzovsky
5490e5ca0d1SHuang Rui for (i = 0; i < adev->usec_timeout; i++) {
5502890decfSZhang, Jerry val = RREG32(reg_index);
5510e5ca0d1SHuang Rui if (check_changed) {
5520e5ca0d1SHuang Rui if (val != reg_val)
5530e5ca0d1SHuang Rui return 0;
5540e5ca0d1SHuang Rui } else {
5550e5ca0d1SHuang Rui if ((val & mask) == reg_val)
5560e5ca0d1SHuang Rui return 0;
5570e5ca0d1SHuang Rui }
5580e5ca0d1SHuang Rui udelay(1);
5590e5ca0d1SHuang Rui }
5600e5ca0d1SHuang Rui
5610e5ca0d1SHuang Rui return -ETIME;
5620e5ca0d1SHuang Rui }
5630e5ca0d1SHuang Rui
psp_wait_for_spirom_update(struct psp_context * psp,uint32_t reg_index,uint32_t reg_val,uint32_t mask,uint32_t msec_timeout)564d4a4ff1cSLikun Gao int psp_wait_for_spirom_update(struct psp_context *psp, uint32_t reg_index,
565d4a4ff1cSLikun Gao uint32_t reg_val, uint32_t mask, uint32_t msec_timeout)
566d4a4ff1cSLikun Gao {
567d4a4ff1cSLikun Gao uint32_t val;
568d4a4ff1cSLikun Gao int i;
569d4a4ff1cSLikun Gao struct amdgpu_device *adev = psp->adev;
570d4a4ff1cSLikun Gao
571d4a4ff1cSLikun Gao if (psp->adev->no_hw_access)
572d4a4ff1cSLikun Gao return 0;
573d4a4ff1cSLikun Gao
574d4a4ff1cSLikun Gao for (i = 0; i < msec_timeout; i++) {
575d4a4ff1cSLikun Gao val = RREG32(reg_index);
576d4a4ff1cSLikun Gao if ((val & mask) == reg_val)
577d4a4ff1cSLikun Gao return 0;
578d4a4ff1cSLikun Gao msleep(1);
579d4a4ff1cSLikun Gao }
580d4a4ff1cSLikun Gao
581d4a4ff1cSLikun Gao return -ETIME;
582d4a4ff1cSLikun Gao }
583d4a4ff1cSLikun Gao
psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)584dc739d18SLang Yu static const char *psp_gfx_cmd_name(enum psp_gfx_cmd_id cmd_id)
585dc739d18SLang Yu {
586dc739d18SLang Yu switch (cmd_id) {
587dc739d18SLang Yu case GFX_CMD_ID_LOAD_TA:
588dc739d18SLang Yu return "LOAD_TA";
589dc739d18SLang Yu case GFX_CMD_ID_UNLOAD_TA:
590dc739d18SLang Yu return "UNLOAD_TA";
591dc739d18SLang Yu case GFX_CMD_ID_INVOKE_CMD:
592dc739d18SLang Yu return "INVOKE_CMD";
593dc739d18SLang Yu case GFX_CMD_ID_LOAD_ASD:
594dc739d18SLang Yu return "LOAD_ASD";
595dc739d18SLang Yu case GFX_CMD_ID_SETUP_TMR:
596dc739d18SLang Yu return "SETUP_TMR";
597dc739d18SLang Yu case GFX_CMD_ID_LOAD_IP_FW:
598dc739d18SLang Yu return "LOAD_IP_FW";
599dc739d18SLang Yu case GFX_CMD_ID_DESTROY_TMR:
600dc739d18SLang Yu return "DESTROY_TMR";
601dc739d18SLang Yu case GFX_CMD_ID_SAVE_RESTORE:
602dc739d18SLang Yu return "SAVE_RESTORE_IP_FW";
603dc739d18SLang Yu case GFX_CMD_ID_SETUP_VMR:
604dc739d18SLang Yu return "SETUP_VMR";
605dc739d18SLang Yu case GFX_CMD_ID_DESTROY_VMR:
606dc739d18SLang Yu return "DESTROY_VMR";
607dc739d18SLang Yu case GFX_CMD_ID_PROG_REG:
608dc739d18SLang Yu return "PROG_REG";
609dc739d18SLang Yu case GFX_CMD_ID_GET_FW_ATTESTATION:
610dc739d18SLang Yu return "GET_FW_ATTESTATION";
611dc739d18SLang Yu case GFX_CMD_ID_LOAD_TOC:
612dc739d18SLang Yu return "ID_LOAD_TOC";
613dc739d18SLang Yu case GFX_CMD_ID_AUTOLOAD_RLC:
614dc739d18SLang Yu return "AUTOLOAD_RLC";
615dc739d18SLang Yu case GFX_CMD_ID_BOOT_CFG:
616dc739d18SLang Yu return "BOOT_CFG";
617dc739d18SLang Yu default:
618dc739d18SLang Yu return "UNKNOWN CMD";
619dc739d18SLang Yu }
620dc739d18SLang Yu }
621dc739d18SLang Yu
6220e5ca0d1SHuang Rui static int
psp_cmd_submit_buf(struct psp_context * psp,struct amdgpu_firmware_info * ucode,struct psp_gfx_cmd_resp * cmd,uint64_t fence_mc_addr)6230e5ca0d1SHuang Rui psp_cmd_submit_buf(struct psp_context *psp,
6240e5ca0d1SHuang Rui struct amdgpu_firmware_info *ucode,
6250b25cbf9SHawking Zhang struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr)
6260e5ca0d1SHuang Rui {
6270e5ca0d1SHuang Rui int ret;
628a2c5dd9eSlyndonli int index;
62957995aa8Spengzhou int timeout = 20000;
630c2c6f816SJohn Clements bool ras_intr = false;
631d73cd701SEmily Deng bool skip_unsupport = false;
6320e5ca0d1SHuang Rui
6337afefb81SAndrey Grodzovsky if (psp->adev->no_hw_access)
634bf36b52eSAndrey Grodzovsky return 0;
635bf36b52eSAndrey Grodzovsky
636a1952da7SHuang Rui memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
6370e5ca0d1SHuang Rui
638a1952da7SHuang Rui memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
6390e5ca0d1SHuang Rui
6400b25cbf9SHawking Zhang index = atomic_inc_return(&psp->fence_value);
6415bdd0b72SHawking Zhang ret = psp_ring_cmd_submit(psp, psp->cmd_buf_mc_addr, fence_mc_addr, index);
6420b25cbf9SHawking Zhang if (ret) {
6430b25cbf9SHawking Zhang atomic_dec(&psp->fence_value);
644f89f8c6bSAndrey Grodzovsky goto exit;
645ca7f65c7Skbuild test robot }
6460e5ca0d1SHuang Rui
647810085ddSEric Huang amdgpu_device_invalidate_hdp(psp->adev, NULL);
648ea114213Sxinhui pan while (*((unsigned int *)psp->fence_buf) != index) {
649ea114213Sxinhui pan if (--timeout == 0)
6507a3d7bf6SEvan Quan break;
651bff77e86SLe Ma /*
652bff77e86SLe Ma * Shouldn't wait for timeout when err_event_athub occurs,
653bff77e86SLe Ma * because gpu reset thread triggered and lock resource should
654bff77e86SLe Ma * be released for psp resume sequence.
655bff77e86SLe Ma */
656c2c6f816SJohn Clements ras_intr = amdgpu_ras_intr_triggered();
657c2c6f816SJohn Clements if (ras_intr)
658bff77e86SLe Ma break;
65957995aa8Spengzhou usleep_range(10, 100);
660810085ddSEric Huang amdgpu_device_invalidate_hdp(psp->adev, NULL);
661ea114213Sxinhui pan }
6620b25cbf9SHawking Zhang
6633bda8acdSEmily Deng /* We allow TEE_ERROR_NOT_SUPPORTED for VMR command and PSP_ERR_UNKNOWN_COMMAND in SRIOV */
6643bda8acdSEmily Deng skip_unsupport = (psp->cmd_buf_mem->resp.status == TEE_ERROR_NOT_SUPPORTED ||
6653bda8acdSEmily Deng psp->cmd_buf_mem->resp.status == PSP_ERR_UNKNOWN_COMMAND) && amdgpu_sriov_vf(psp->adev);
666d73cd701SEmily Deng
667f14c8c3eSSrinivasan Shanmugam memcpy(&cmd->resp, &psp->cmd_buf_mem->resp, sizeof(struct psp_gfx_resp));
66819ae3330SJohn Clements
669466bcb75SAaron Liu /* In some cases, psp response status is not 0 even there is no
670466bcb75SAaron Liu * problem while the command is submitted. Some version of PSP FW
671466bcb75SAaron Liu * doesn't write 0 to that field.
672466bcb75SAaron Liu * So here we would like to only print a warning instead of an error
673466bcb75SAaron Liu * during psp initialization to avoid breaking hw_init and it doesn't
674466bcb75SAaron Liu * return -EINVAL.
675466bcb75SAaron Liu */
676d73cd701SEmily Deng if (!skip_unsupport && (psp->cmd_buf_mem->resp.status || !timeout) && !ras_intr) {
67776223c54SXiangliang Yu if (ucode)
67850c6dedeSLang Yu DRM_WARN("failed to load ucode %s(0x%X) ",
67950c6dedeSLang Yu amdgpu_ucode_name(ucode->ucode_id), ucode->ucode_id);
68050c6dedeSLang Yu DRM_WARN("psp gfx command %s(0x%X) failed and response status is (0x%X)\n",
68150c6dedeSLang Yu psp_gfx_cmd_name(psp->cmd_buf_mem->cmd_id), psp->cmd_buf_mem->cmd_id,
682e6e193c0SJohn Clements psp->cmd_buf_mem->resp.status);
6834bef1abeSAlice Wong /* If any firmware (including CAP) load fails under SRIOV, it should
6844bef1abeSAlice Wong * return failure to stop the VF from initializing.
6854bef1abeSAlice Wong * Also return failure in case of timeout
686c4381d0eSBokun Zhang */
6874bef1abeSAlice Wong if ((ucode && amdgpu_sriov_vf(psp->adev)) || !timeout) {
688f89f8c6bSAndrey Grodzovsky ret = -EINVAL;
689f89f8c6bSAndrey Grodzovsky goto exit;
69028a16027SHuang Rui }
69132eaeae0SAlex Deucher }
69228a16027SHuang Rui
693435198f3SJames Zhu if (ucode) {
694435198f3SJames Zhu ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
695435198f3SJames Zhu ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
696435198f3SJames Zhu }
697435198f3SJames Zhu
698f89f8c6bSAndrey Grodzovsky exit:
6990e5ca0d1SHuang Rui return ret;
7000e5ca0d1SHuang Rui }
7010e5ca0d1SHuang Rui
acquire_psp_cmd_buf(struct psp_context * psp)7024b296527SJohn Clements static struct psp_gfx_cmd_resp *acquire_psp_cmd_buf(struct psp_context *psp)
7034b296527SJohn Clements {
7044b296527SJohn Clements struct psp_gfx_cmd_resp *cmd = psp->cmd;
7054b296527SJohn Clements
7064b296527SJohn Clements mutex_lock(&psp->mutex);
7074b296527SJohn Clements
7084b296527SJohn Clements memset(cmd, 0, sizeof(struct psp_gfx_cmd_resp));
7094b296527SJohn Clements
7104b296527SJohn Clements return cmd;
7114b296527SJohn Clements }
7124b296527SJohn Clements
release_psp_cmd_buf(struct psp_context * psp)7136c18ecefSGuchun Chen static void release_psp_cmd_buf(struct psp_context *psp)
7144b296527SJohn Clements {
7154b296527SJohn Clements mutex_unlock(&psp->mutex);
7164b296527SJohn Clements }
7174b296527SJohn Clements
psp_prep_tmr_cmd_buf(struct psp_context * psp,struct psp_gfx_cmd_resp * cmd,uint64_t tmr_mc,struct amdgpu_bo * tmr_bo)7185ec996dfSXiangliang Yu static void psp_prep_tmr_cmd_buf(struct psp_context *psp,
7195ec996dfSXiangliang Yu struct psp_gfx_cmd_resp *cmd,
72036c08237SOak Zeng uint64_t tmr_mc, struct amdgpu_bo *tmr_bo)
7210e5ca0d1SHuang Rui {
72236c08237SOak Zeng struct amdgpu_device *adev = psp->adev;
7235b03127dSLijo Lazar uint32_t size = 0;
7245b03127dSLijo Lazar uint64_t tmr_pa = 0;
7255b03127dSLijo Lazar
7265b03127dSLijo Lazar if (tmr_bo) {
7275b03127dSLijo Lazar size = amdgpu_bo_size(tmr_bo);
7285b03127dSLijo Lazar tmr_pa = amdgpu_gmc_vram_pa(adev, tmr_bo);
7295b03127dSLijo Lazar }
73036c08237SOak Zeng
731a2676149SHawking Zhang if (amdgpu_sriov_vf(psp->adev))
7325ec996dfSXiangliang Yu cmd->cmd_id = GFX_CMD_ID_SETUP_VMR;
7335ec996dfSXiangliang Yu else
7340e5ca0d1SHuang Rui cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
735f03defe0SAlex Deucher cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
736f03defe0SAlex Deucher cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
7370e5ca0d1SHuang Rui cmd->cmd.cmd_setup_tmr.buf_size = size;
73836c08237SOak Zeng cmd->cmd.cmd_setup_tmr.bitfield.virt_phy_addr = 1;
73936c08237SOak Zeng cmd->cmd.cmd_setup_tmr.system_phy_addr_lo = lower_32_bits(tmr_pa);
74036c08237SOak Zeng cmd->cmd.cmd_setup_tmr.system_phy_addr_hi = upper_32_bits(tmr_pa);
7410e5ca0d1SHuang Rui }
7420e5ca0d1SHuang Rui
psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp * cmd,uint64_t pri_buf_mc,uint32_t size)7437ea49e76SHawking Zhang static void psp_prep_load_toc_cmd_buf(struct psp_gfx_cmd_resp *cmd,
7447ea49e76SHawking Zhang uint64_t pri_buf_mc, uint32_t size)
7457ea49e76SHawking Zhang {
7467ea49e76SHawking Zhang cmd->cmd_id = GFX_CMD_ID_LOAD_TOC;
7477ea49e76SHawking Zhang cmd->cmd.cmd_load_toc.toc_phy_addr_lo = lower_32_bits(pri_buf_mc);
7487ea49e76SHawking Zhang cmd->cmd.cmd_load_toc.toc_phy_addr_hi = upper_32_bits(pri_buf_mc);
7497ea49e76SHawking Zhang cmd->cmd.cmd_load_toc.toc_size = size;
7507ea49e76SHawking Zhang }
7517ea49e76SHawking Zhang
7527ea49e76SHawking Zhang /* Issue LOAD TOC cmd to PSP to part toc and calculate tmr size needed */
psp_load_toc(struct psp_context * psp,uint32_t * tmr_size)7537ea49e76SHawking Zhang static int psp_load_toc(struct psp_context *psp,
7547ea49e76SHawking Zhang uint32_t *tmr_size)
7557ea49e76SHawking Zhang {
7567ea49e76SHawking Zhang int ret;
7574b296527SJohn Clements struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
7587ea49e76SHawking Zhang
7597ea49e76SHawking Zhang /* Copy toc to psp firmware private buffer */
760222e0a71SCandice Li psp_copy_fw(psp, psp->toc.start_addr, psp->toc.size_bytes);
7617ea49e76SHawking Zhang
762222e0a71SCandice Li psp_prep_load_toc_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->toc.size_bytes);
7637ea49e76SHawking Zhang
7647ea49e76SHawking Zhang ret = psp_cmd_submit_buf(psp, NULL, cmd,
7657ea49e76SHawking Zhang psp->fence_buf_mc_addr);
7667ea49e76SHawking Zhang if (!ret)
7677ea49e76SHawking Zhang *tmr_size = psp->cmd_buf_mem->resp.tmr_size;
7684fb93071SCandice Li
7694b296527SJohn Clements release_psp_cmd_buf(psp);
7704b296527SJohn Clements
7717ea49e76SHawking Zhang return ret;
7727ea49e76SHawking Zhang }
7737ea49e76SHawking Zhang
psp_boottime_tmr(struct psp_context * psp)7745b03127dSLijo Lazar static bool psp_boottime_tmr(struct psp_context *psp)
7755b03127dSLijo Lazar {
7765b03127dSLijo Lazar switch (psp->adev->ip_versions[MP0_HWIP][0]) {
7775b03127dSLijo Lazar case IP_VERSION(13, 0, 6):
7785b03127dSLijo Lazar return true;
7795b03127dSLijo Lazar default:
7805b03127dSLijo Lazar return false;
7815b03127dSLijo Lazar }
7825b03127dSLijo Lazar }
7835b03127dSLijo Lazar
7840e5ca0d1SHuang Rui /* Set up Trusted Memory Region */
psp_tmr_init(struct psp_context * psp)7850e5ca0d1SHuang Rui static int psp_tmr_init(struct psp_context *psp)
7860e5ca0d1SHuang Rui {
78796e1a88fSAlex Deucher int ret = 0;
7887ea49e76SHawking Zhang int tmr_size;
78912842d02STianci.Yin void *tmr_buf;
79012842d02STianci.Yin void **pptr;
7910e5ca0d1SHuang Rui
7920e5ca0d1SHuang Rui /*
793795c1b8dSshaoyunl * According to HW engineer, they prefer the TMR address be "naturally
794795c1b8dSshaoyunl * aligned" , e.g. the start address be an integer divide of TMR size.
7950e5ca0d1SHuang Rui *
7960e5ca0d1SHuang Rui * Note: this memory need be reserved till the driver
7970e5ca0d1SHuang Rui * uninitializes.
7980e5ca0d1SHuang Rui */
79947bfa5f6SOak Zeng tmr_size = PSP_TMR_SIZE(psp->adev);
8007ea49e76SHawking Zhang
8017ea49e76SHawking Zhang /* For ASICs support RLC autoload, psp will parse the toc
8022d5c0415SPraful Swarnakar * and calculate the total size of TMR needed
8032d5c0415SPraful Swarnakar */
8041b657824SJiange Zhao if (!amdgpu_sriov_vf(psp->adev) &&
805222e0a71SCandice Li psp->toc.start_addr &&
806222e0a71SCandice Li psp->toc.size_bytes &&
8077ea49e76SHawking Zhang psp->fw_pri_buf) {
8087ea49e76SHawking Zhang ret = psp_load_toc(psp, &tmr_size);
8097ea49e76SHawking Zhang if (ret) {
8107ea49e76SHawking Zhang DRM_ERROR("Failed to load toc\n");
8117ea49e76SHawking Zhang return ret;
8127ea49e76SHawking Zhang }
8137ea49e76SHawking Zhang }
8147ea49e76SHawking Zhang
81596e1a88fSAlex Deucher if (!psp->tmr_bo) {
81612842d02STianci.Yin pptr = amdgpu_sriov_vf(psp->adev) ? &tmr_buf : NULL;
81758ab2c08SChristian König ret = amdgpu_bo_create_kernel(psp->adev, tmr_size,
81858ab2c08SChristian König PSP_TMR_ALIGNMENT,
81958ab2c08SChristian König AMDGPU_HAS_VRAM(psp->adev) ?
82058ab2c08SChristian König AMDGPU_GEM_DOMAIN_VRAM :
82158ab2c08SChristian König AMDGPU_GEM_DOMAIN_GTT,
82258ab2c08SChristian König &psp->tmr_bo, &psp->tmr_mc_addr,
82358ab2c08SChristian König pptr);
82496e1a88fSAlex Deucher }
8256f2b1fccSHuang Rui
8266f2b1fccSHuang Rui return ret;
8276f2b1fccSHuang Rui }
8286f2b1fccSHuang Rui
psp_skip_tmr(struct psp_context * psp)829f61772cdSLiu ChengZhe static bool psp_skip_tmr(struct psp_context *psp)
830f61772cdSLiu ChengZhe {
8311d789535SAlex Deucher switch (psp->adev->ip_versions[MP0_HWIP][0]) {
83282d05736SAlex Deucher case IP_VERSION(11, 0, 9):
83382d05736SAlex Deucher case IP_VERSION(11, 0, 7):
83482d05736SAlex Deucher case IP_VERSION(13, 0, 2):
835acbe7610SZhigang Luo case IP_VERSION(13, 0, 6):
836f8bd7321SHorace Chen case IP_VERSION(13, 0, 10):
837f61772cdSLiu ChengZhe return true;
838f61772cdSLiu ChengZhe default:
839f61772cdSLiu ChengZhe return false;
840f61772cdSLiu ChengZhe }
841f61772cdSLiu ChengZhe }
842f61772cdSLiu ChengZhe
psp_tmr_load(struct psp_context * psp)8436f2b1fccSHuang Rui static int psp_tmr_load(struct psp_context *psp)
8446f2b1fccSHuang Rui {
8456f2b1fccSHuang Rui int ret;
8464b296527SJohn Clements struct psp_gfx_cmd_resp *cmd;
8476f2b1fccSHuang Rui
848f61772cdSLiu ChengZhe /* For Navi12 and CHIP_SIENNA_CICHLID SRIOV, do not set up TMR.
849f61772cdSLiu ChengZhe * Already set up by host driver.
850f61772cdSLiu ChengZhe */
851f61772cdSLiu ChengZhe if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
852f61772cdSLiu ChengZhe return 0;
853f61772cdSLiu ChengZhe
8544b296527SJohn Clements cmd = acquire_psp_cmd_buf(psp);
8554b296527SJohn Clements
85636c08237SOak Zeng psp_prep_tmr_cmd_buf(psp, cmd, psp->tmr_mc_addr, psp->tmr_bo);
8575b03127dSLijo Lazar if (psp->tmr_bo)
8587ea49e76SHawking Zhang DRM_INFO("reserve 0x%lx from 0x%llx for PSP TMR\n",
8597ea49e76SHawking Zhang amdgpu_bo_size(psp->tmr_bo), psp->tmr_mc_addr);
8600e5ca0d1SHuang Rui
8610e5ca0d1SHuang Rui ret = psp_cmd_submit_buf(psp, NULL, cmd,
8620b25cbf9SHawking Zhang psp->fence_buf_mc_addr);
8630e5ca0d1SHuang Rui
8644b296527SJohn Clements release_psp_cmd_buf(psp);
8654b296527SJohn Clements
8660e5ca0d1SHuang Rui return ret;
8670e5ca0d1SHuang Rui }
8680e5ca0d1SHuang Rui
psp_prep_tmr_unload_cmd_buf(struct psp_context * psp,struct psp_gfx_cmd_resp * cmd)86990937420SHuang Rui static void psp_prep_tmr_unload_cmd_buf(struct psp_context *psp,
87090937420SHuang Rui struct psp_gfx_cmd_resp *cmd)
87190937420SHuang Rui {
87290937420SHuang Rui if (amdgpu_sriov_vf(psp->adev))
87390937420SHuang Rui cmd->cmd_id = GFX_CMD_ID_DESTROY_VMR;
87490937420SHuang Rui else
87590937420SHuang Rui cmd->cmd_id = GFX_CMD_ID_DESTROY_TMR;
87690937420SHuang Rui }
87790937420SHuang Rui
psp_tmr_unload(struct psp_context * psp)87890937420SHuang Rui static int psp_tmr_unload(struct psp_context *psp)
87990937420SHuang Rui {
88090937420SHuang Rui int ret;
881f5a5b081STong Liu01 struct psp_gfx_cmd_resp *cmd;
882f5a5b081STong Liu01
883f5a5b081STong Liu01 /* skip TMR unload for Navi12 and CHIP_SIENNA_CICHLID SRIOV,
884f5a5b081STong Liu01 * as TMR is not loaded at all
885f5a5b081STong Liu01 */
886f5a5b081STong Liu01 if (amdgpu_sriov_vf(psp->adev) && psp_skip_tmr(psp))
887f5a5b081STong Liu01 return 0;
888f5a5b081STong Liu01
889f5a5b081STong Liu01 cmd = acquire_psp_cmd_buf(psp);
89090937420SHuang Rui
89190937420SHuang Rui psp_prep_tmr_unload_cmd_buf(psp, cmd);
892abcb2aceSGuchun Chen dev_dbg(psp->adev->dev, "free PSP TMR buffer\n");
89390937420SHuang Rui
89490937420SHuang Rui ret = psp_cmd_submit_buf(psp, NULL, cmd,
89590937420SHuang Rui psp->fence_buf_mc_addr);
89690937420SHuang Rui
8974b296527SJohn Clements release_psp_cmd_buf(psp);
8984b296527SJohn Clements
89990937420SHuang Rui return ret;
90090937420SHuang Rui }
90190937420SHuang Rui
psp_tmr_terminate(struct psp_context * psp)90290937420SHuang Rui static int psp_tmr_terminate(struct psp_context *psp)
90390937420SHuang Rui {
904da40bf8fSAlex Deucher return psp_tmr_unload(psp);
90590937420SHuang Rui }
90690937420SHuang Rui
psp_get_fw_attestation_records_addr(struct psp_context * psp,uint64_t * output_ptr)90719ae3330SJohn Clements int psp_get_fw_attestation_records_addr(struct psp_context *psp,
90819ae3330SJohn Clements uint64_t *output_ptr)
90919ae3330SJohn Clements {
91019ae3330SJohn Clements int ret;
9114b296527SJohn Clements struct psp_gfx_cmd_resp *cmd;
91219ae3330SJohn Clements
91319ae3330SJohn Clements if (!output_ptr)
91419ae3330SJohn Clements return -EINVAL;
91519ae3330SJohn Clements
91619ae3330SJohn Clements if (amdgpu_sriov_vf(psp->adev))
91719ae3330SJohn Clements return 0;
91819ae3330SJohn Clements
9194b296527SJohn Clements cmd = acquire_psp_cmd_buf(psp);
9204b296527SJohn Clements
92119ae3330SJohn Clements cmd->cmd_id = GFX_CMD_ID_GET_FW_ATTESTATION;
92219ae3330SJohn Clements
92319ae3330SJohn Clements ret = psp_cmd_submit_buf(psp, NULL, cmd,
92419ae3330SJohn Clements psp->fence_buf_mc_addr);
92519ae3330SJohn Clements
92619ae3330SJohn Clements if (!ret) {
92719ae3330SJohn Clements *output_ptr = ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_lo) +
92819ae3330SJohn Clements ((uint64_t)cmd->resp.uresp.fwar_db_info.fwar_db_addr_hi << 32);
92919ae3330SJohn Clements }
93019ae3330SJohn Clements
9314b296527SJohn Clements release_psp_cmd_buf(psp);
9324b296527SJohn Clements
93319ae3330SJohn Clements return ret;
93419ae3330SJohn Clements }
93519ae3330SJohn Clements
psp_boot_config_get(struct amdgpu_device * adev,uint32_t * boot_cfg)936c6642234SHawking Zhang static int psp_boot_config_get(struct amdgpu_device *adev, uint32_t *boot_cfg)
937c6642234SHawking Zhang {
938c6642234SHawking Zhang struct psp_context *psp = &adev->psp;
9394b296527SJohn Clements struct psp_gfx_cmd_resp *cmd;
940c6642234SHawking Zhang int ret;
941c6642234SHawking Zhang
942c6642234SHawking Zhang if (amdgpu_sriov_vf(adev))
943c6642234SHawking Zhang return 0;
944c6642234SHawking Zhang
9454b296527SJohn Clements cmd = acquire_psp_cmd_buf(psp);
946c6642234SHawking Zhang
947c6642234SHawking Zhang cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
948c6642234SHawking Zhang cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_GET;
949c6642234SHawking Zhang
950c6642234SHawking Zhang ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
951c6642234SHawking Zhang if (!ret) {
952c6642234SHawking Zhang *boot_cfg =
953c6642234SHawking Zhang (cmd->resp.uresp.boot_cfg.boot_cfg & BOOT_CONFIG_GECC) ? 1 : 0;
954c6642234SHawking Zhang }
955c6642234SHawking Zhang
9564b296527SJohn Clements release_psp_cmd_buf(psp);
9574b296527SJohn Clements
958c6642234SHawking Zhang return ret;
959c6642234SHawking Zhang }
960c6642234SHawking Zhang
psp_boot_config_set(struct amdgpu_device * adev,uint32_t boot_cfg)96155188d64SHawking Zhang static int psp_boot_config_set(struct amdgpu_device *adev, uint32_t boot_cfg)
962cad7b751SJohn Clements {
9634b296527SJohn Clements int ret;
964cad7b751SJohn Clements struct psp_context *psp = &adev->psp;
9654b296527SJohn Clements struct psp_gfx_cmd_resp *cmd;
966cad7b751SJohn Clements
967c6a11133SHawking Zhang if (amdgpu_sriov_vf(adev))
968cad7b751SJohn Clements return 0;
969cad7b751SJohn Clements
9704b296527SJohn Clements cmd = acquire_psp_cmd_buf(psp);
971cad7b751SJohn Clements
972cad7b751SJohn Clements cmd->cmd_id = GFX_CMD_ID_BOOT_CFG;
973cad7b751SJohn Clements cmd->cmd.boot_cfg.sub_cmd = BOOTCFG_CMD_SET;
97455188d64SHawking Zhang cmd->cmd.boot_cfg.boot_config = boot_cfg;
97555188d64SHawking Zhang cmd->cmd.boot_cfg.boot_config_valid = boot_cfg;
976cad7b751SJohn Clements
9774b296527SJohn Clements ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
9784b296527SJohn Clements
9794b296527SJohn Clements release_psp_cmd_buf(psp);
9804b296527SJohn Clements
9814b296527SJohn Clements return ret;
982cad7b751SJohn Clements }
983cad7b751SJohn Clements
psp_rl_load(struct amdgpu_device * adev)9840d2c1855SJohn Clements static int psp_rl_load(struct amdgpu_device *adev)
9850d2c1855SJohn Clements {
9864b296527SJohn Clements int ret;
9870d2c1855SJohn Clements struct psp_context *psp = &adev->psp;
9884b296527SJohn Clements struct psp_gfx_cmd_resp *cmd;
9894fb93071SCandice Li
990222e0a71SCandice Li if (!is_psp_fw_valid(psp->rl))
9910d2c1855SJohn Clements return 0;
9920d2c1855SJohn Clements
9934b296527SJohn Clements cmd = acquire_psp_cmd_buf(psp);
9944b296527SJohn Clements
9950d2c1855SJohn Clements memset(psp->fw_pri_buf, 0, PSP_1_MEG);
996222e0a71SCandice Li memcpy(psp->fw_pri_buf, psp->rl.start_addr, psp->rl.size_bytes);
9970d2c1855SJohn Clements
9980d2c1855SJohn Clements cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
9990d2c1855SJohn Clements cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(psp->fw_pri_mc_addr);
10000d2c1855SJohn Clements cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(psp->fw_pri_mc_addr);
1001222e0a71SCandice Li cmd->cmd.cmd_load_ip_fw.fw_size = psp->rl.size_bytes;
10020d2c1855SJohn Clements cmd->cmd.cmd_load_ip_fw.fw_type = GFX_FW_TYPE_REG_LIST;
10030d2c1855SJohn Clements
10044b296527SJohn Clements ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
10054b296527SJohn Clements
10064b296527SJohn Clements release_psp_cmd_buf(psp);
10074b296527SJohn Clements
10084b296527SJohn Clements return ret;
10090d2c1855SJohn Clements }
10100d2c1855SJohn Clements
psp_spatial_partition(struct psp_context * psp,int mode)1011ba08e9cbSLijo Lazar int psp_spatial_partition(struct psp_context *psp, int mode)
1012ba08e9cbSLijo Lazar {
1013ba08e9cbSLijo Lazar struct psp_gfx_cmd_resp *cmd;
1014ba08e9cbSLijo Lazar int ret;
1015ba08e9cbSLijo Lazar
1016ba08e9cbSLijo Lazar if (amdgpu_sriov_vf(psp->adev))
1017ba08e9cbSLijo Lazar return 0;
1018ba08e9cbSLijo Lazar
1019ba08e9cbSLijo Lazar cmd = acquire_psp_cmd_buf(psp);
1020ba08e9cbSLijo Lazar
1021ba08e9cbSLijo Lazar cmd->cmd_id = GFX_CMD_ID_SRIOV_SPATIAL_PART;
1022ba08e9cbSLijo Lazar cmd->cmd.cmd_spatial_part.mode = mode;
1023ba08e9cbSLijo Lazar
10249f77af01SColin Ian King dev_info(psp->adev->dev, "Requesting %d partitions through PSP", mode);
1025ba08e9cbSLijo Lazar ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
1026ba08e9cbSLijo Lazar
1027ba08e9cbSLijo Lazar release_psp_cmd_buf(psp);
1028ba08e9cbSLijo Lazar
1029ba08e9cbSLijo Lazar return ret;
1030ba08e9cbSLijo Lazar }
1031ba08e9cbSLijo Lazar
psp_asd_initialize(struct psp_context * psp)10323f83f17bSCandice Li static int psp_asd_initialize(struct psp_context *psp)
10333f83f17bSCandice Li {
10340e5ca0d1SHuang Rui int ret;
10350e5ca0d1SHuang Rui
1036943cafb8SXiangliang Yu /* If PSP version doesn't match ASD version, asd loading will be failed.
1037943cafb8SXiangliang Yu * add workaround to bypass it for sriov now.
1038943cafb8SXiangliang Yu * TODO: add version check to make it common
1039943cafb8SXiangliang Yu */
1040de3a1e33SCandice Li if (amdgpu_sriov_vf(psp->adev) || !psp->asd_context.bin_desc.size_bytes)
1041943cafb8SXiangliang Yu return 0;
1042943cafb8SXiangliang Yu
10433f83f17bSCandice Li psp->asd_context.mem_context.shared_mc_addr = 0;
10443f83f17bSCandice Li psp->asd_context.mem_context.shared_mem_size = PSP_ASD_SHARED_MEM_SIZE;
10453f83f17bSCandice Li psp->asd_context.ta_load_type = GFX_CMD_ID_LOAD_ASD;
10464b296527SJohn Clements
1047f03d97b0SAlex Deucher ret = psp_ta_load(psp, &psp->asd_context);
10483f83f17bSCandice Li if (!ret)
10493f83f17bSCandice Li psp->asd_context.initialized = true;
10504b296527SJohn Clements
10510e5ca0d1SHuang Rui return ret;
10520e5ca0d1SHuang Rui }
10530e5ca0d1SHuang Rui
psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp * cmd,uint32_t session_id)10541f455f25SJohn Clements static void psp_prep_ta_unload_cmd_buf(struct psp_gfx_cmd_resp *cmd,
10551f455f25SJohn Clements uint32_t session_id)
105671e5f0cbSHawking Zhang {
105771e5f0cbSHawking Zhang cmd->cmd_id = GFX_CMD_ID_UNLOAD_TA;
10581f455f25SJohn Clements cmd->cmd.cmd_unload_ta.session_id = session_id;
105971e5f0cbSHawking Zhang }
106071e5f0cbSHawking Zhang
psp_ta_unload(struct psp_context * psp,struct ta_context * context)1061fe96e563SCandice Li int psp_ta_unload(struct psp_context *psp, struct ta_context *context)
106271e5f0cbSHawking Zhang {
106371e5f0cbSHawking Zhang int ret;
106425c94b33SCandice Li struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
106525c94b33SCandice Li
106617c6805aSCandice Li psp_prep_ta_unload_cmd_buf(cmd, context->session_id);
106725c94b33SCandice Li
106825c94b33SCandice Li ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
106925c94b33SCandice Li
1070bf7d7772SCandice Li context->resp_status = cmd->resp.status;
1071bf7d7772SCandice Li
107225c94b33SCandice Li release_psp_cmd_buf(psp);
107325c94b33SCandice Li
107425c94b33SCandice Li return ret;
107525c94b33SCandice Li }
107625c94b33SCandice Li
psp_asd_terminate(struct psp_context * psp)107725c94b33SCandice Li static int psp_asd_terminate(struct psp_context *psp)
107825c94b33SCandice Li {
107925c94b33SCandice Li int ret;
108071e5f0cbSHawking Zhang
108171e5f0cbSHawking Zhang if (amdgpu_sriov_vf(psp->adev))
108271e5f0cbSHawking Zhang return 0;
108371e5f0cbSHawking Zhang
10843f83f17bSCandice Li if (!psp->asd_context.initialized)
108571e5f0cbSHawking Zhang return 0;
108671e5f0cbSHawking Zhang
1087f03d97b0SAlex Deucher ret = psp_ta_unload(psp, &psp->asd_context);
108871e5f0cbSHawking Zhang if (!ret)
10893f83f17bSCandice Li psp->asd_context.initialized = false;
109071e5f0cbSHawking Zhang
109171e5f0cbSHawking Zhang return ret;
109271e5f0cbSHawking Zhang }
109371e5f0cbSHawking Zhang
psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp * cmd,uint32_t id,uint32_t value)1094c5d19419STrigger Huang static void psp_prep_reg_prog_cmd_buf(struct psp_gfx_cmd_resp *cmd,
1095c5d19419STrigger Huang uint32_t id, uint32_t value)
1096c5d19419STrigger Huang {
1097c5d19419STrigger Huang cmd->cmd_id = GFX_CMD_ID_PROG_REG;
1098c5d19419STrigger Huang cmd->cmd.cmd_setup_reg_prog.reg_value = value;
1099c5d19419STrigger Huang cmd->cmd.cmd_setup_reg_prog.reg_id = id;
1100c5d19419STrigger Huang }
1101c5d19419STrigger Huang
psp_reg_program(struct psp_context * psp,enum psp_reg_prog_id reg,uint32_t value)1102c5d19419STrigger Huang int psp_reg_program(struct psp_context *psp, enum psp_reg_prog_id reg,
1103c5d19419STrigger Huang uint32_t value)
1104c5d19419STrigger Huang {
11054b296527SJohn Clements struct psp_gfx_cmd_resp *cmd;
1106c5d19419STrigger Huang int ret = 0;
1107c5d19419STrigger Huang
1108c5d19419STrigger Huang if (reg >= PSP_REG_LAST)
1109c5d19419STrigger Huang return -EINVAL;
1110c5d19419STrigger Huang
11114b296527SJohn Clements cmd = acquire_psp_cmd_buf(psp);
11124b296527SJohn Clements
1113c5d19419STrigger Huang psp_prep_reg_prog_cmd_buf(cmd, reg, value);
1114c5d19419STrigger Huang ret = psp_cmd_submit_buf(psp, NULL, cmd, psp->fence_buf_mc_addr);
11152b9ced5aSRohit Khaire if (ret)
11162b9ced5aSRohit Khaire DRM_ERROR("PSP failed to program reg id %d", reg);
1117c5d19419STrigger Huang
11184b296527SJohn Clements release_psp_cmd_buf(psp);
11194b296527SJohn Clements
1120c5d19419STrigger Huang return ret;
1121c5d19419STrigger Huang }
1122c5d19419STrigger Huang
psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp * cmd,uint64_t ta_bin_mc,struct ta_context * context)11231f455f25SJohn Clements static void psp_prep_ta_load_cmd_buf(struct psp_gfx_cmd_resp *cmd,
11241f455f25SJohn Clements uint64_t ta_bin_mc,
1125de3a1e33SCandice Li struct ta_context *context)
112697c8d171SHawking Zhang {
11273f83f17bSCandice Li cmd->cmd_id = context->ta_load_type;
11281f455f25SJohn Clements cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(ta_bin_mc);
11291f455f25SJohn Clements cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(ta_bin_mc);
1130de3a1e33SCandice Li cmd->cmd.cmd_load_ta.app_len = context->bin_desc.size_bytes;
113197c8d171SHawking Zhang
1132de3a1e33SCandice Li cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo =
1133de3a1e33SCandice Li lower_32_bits(context->mem_context.shared_mc_addr);
1134de3a1e33SCandice Li cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi =
1135de3a1e33SCandice Li upper_32_bits(context->mem_context.shared_mc_addr);
1136de3a1e33SCandice Li cmd->cmd.cmd_load_ta.cmd_buf_len = context->mem_context.shared_mem_size;
113797c8d171SHawking Zhang }
113897c8d171SHawking Zhang
psp_ta_init_shared_buf(struct psp_context * psp,struct ta_mem_context * mem_ctx)1139fe96e563SCandice Li int psp_ta_init_shared_buf(struct psp_context *psp,
1140ac1509d1SCandice Li struct ta_mem_context *mem_ctx)
114197c8d171SHawking Zhang {
114297c8d171SHawking Zhang /*
114397c8d171SHawking Zhang * Allocate 16k memory aligned to 4k from Frame Buffer (local
114430acef3cSCandice Li * physical) for ta to host memory
114597c8d171SHawking Zhang */
1146a5e7ffa1SMinghao Chi return amdgpu_bo_create_kernel(psp->adev, mem_ctx->shared_mem_size,
114758ab2c08SChristian König PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM |
114858ab2c08SChristian König AMDGPU_GEM_DOMAIN_GTT,
114930acef3cSCandice Li &mem_ctx->shared_bo,
115030acef3cSCandice Li &mem_ctx->shared_mc_addr,
115130acef3cSCandice Li &mem_ctx->shared_buf);
115297c8d171SHawking Zhang }
115397c8d171SHawking Zhang
psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp * cmd,uint32_t ta_cmd_id,uint32_t session_id)115434e48caeSJohn Clements static void psp_prep_ta_invoke_cmd_buf(struct psp_gfx_cmd_resp *cmd,
115534e48caeSJohn Clements uint32_t ta_cmd_id,
115634e48caeSJohn Clements uint32_t session_id)
115734e48caeSJohn Clements {
115834e48caeSJohn Clements cmd->cmd_id = GFX_CMD_ID_INVOKE_CMD;
115934e48caeSJohn Clements cmd->cmd.cmd_invoke_cmd.session_id = session_id;
116034e48caeSJohn Clements cmd->cmd.cmd_invoke_cmd.ta_cmd_id = ta_cmd_id;
116134e48caeSJohn Clements }
116234e48caeSJohn Clements
psp_ta_invoke(struct psp_context * psp,uint32_t ta_cmd_id,struct ta_context * context)1163fe96e563SCandice Li int psp_ta_invoke(struct psp_context *psp,
116434e48caeSJohn Clements uint32_t ta_cmd_id,
116577ec28eaSCandice Li struct ta_context *context)
116634e48caeSJohn Clements {
116734e48caeSJohn Clements int ret;
11684b296527SJohn Clements struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
116934e48caeSJohn Clements
117077ec28eaSCandice Li psp_prep_ta_invoke_cmd_buf(cmd, ta_cmd_id, context->session_id);
117134e48caeSJohn Clements
117234e48caeSJohn Clements ret = psp_cmd_submit_buf(psp, NULL, cmd,
117334e48caeSJohn Clements psp->fence_buf_mc_addr);
117434e48caeSJohn Clements
1175fe96e563SCandice Li context->resp_status = cmd->resp.status;
1176fe96e563SCandice Li
11774b296527SJohn Clements release_psp_cmd_buf(psp);
11784b296527SJohn Clements
117934e48caeSJohn Clements return ret;
118034e48caeSJohn Clements }
118134e48caeSJohn Clements
psp_ta_load(struct psp_context * psp,struct ta_context * context)1182fe96e563SCandice Li int psp_ta_load(struct psp_context *psp, struct ta_context *context)
118397c8d171SHawking Zhang {
118497c8d171SHawking Zhang int ret;
11854b296527SJohn Clements struct psp_gfx_cmd_resp *cmd;
118697c8d171SHawking Zhang
11874b296527SJohn Clements cmd = acquire_psp_cmd_buf(psp);
11884b296527SJohn Clements
1189de3a1e33SCandice Li psp_copy_fw(psp, context->bin_desc.start_addr,
1190de3a1e33SCandice Li context->bin_desc.size_bytes);
119197c8d171SHawking Zhang
11923f83f17bSCandice Li psp_prep_ta_load_cmd_buf(cmd, psp->fw_pri_mc_addr, context);
119397c8d171SHawking Zhang
119497c8d171SHawking Zhang ret = psp_cmd_submit_buf(psp, NULL, cmd,
119597c8d171SHawking Zhang psp->fence_buf_mc_addr);
119697c8d171SHawking Zhang
1197fe96e563SCandice Li context->resp_status = cmd->resp.status;
1198fe96e563SCandice Li
1199f14c8c3eSSrinivasan Shanmugam if (!ret)
1200ac1509d1SCandice Li context->session_id = cmd->resp.session_id;
120197c8d171SHawking Zhang
12024b296527SJohn Clements release_psp_cmd_buf(psp);
12034b296527SJohn Clements
120497c8d171SHawking Zhang return ret;
120597c8d171SHawking Zhang }
120697c8d171SHawking Zhang
psp_xgmi_invoke(struct psp_context * psp,uint32_t ta_cmd_id)1207ca6e1e59SHawking Zhang int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1208ca6e1e59SHawking Zhang {
120977ec28eaSCandice Li return psp_ta_invoke(psp, ta_cmd_id, &psp->xgmi_context.context);
1210ca6e1e59SHawking Zhang }
1211ca6e1e59SHawking Zhang
psp_xgmi_terminate(struct psp_context * psp)12120b9d3760SHawking Zhang int psp_xgmi_terminate(struct psp_context *psp)
12133e2e2ab5SHawking Zhang {
12143e2e2ab5SHawking Zhang int ret;
121525c94b33SCandice Li struct amdgpu_device *adev = psp->adev;
121625c94b33SCandice Li
121725c94b33SCandice Li /* XGMI TA unload currently is not supported on Arcturus/Aldebaran A+A */
12181d789535SAlex Deucher if (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
12191d789535SAlex Deucher (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
122082d05736SAlex Deucher adev->gmc.xgmi.connected_to_cpu))
122125c94b33SCandice Li return 0;
12223e2e2ab5SHawking Zhang
1223ce97f37bSCandice Li if (!psp->xgmi_context.context.initialized)
12243e2e2ab5SHawking Zhang return 0;
12253e2e2ab5SHawking Zhang
1226f03d97b0SAlex Deucher ret = psp_ta_unload(psp, &psp->xgmi_context.context);
12273e2e2ab5SHawking Zhang
1228ce97f37bSCandice Li psp->xgmi_context.context.initialized = false;
12293e2e2ab5SHawking Zhang
1230fb4f4f42SAlex Deucher return ret;
12313e2e2ab5SHawking Zhang }
12323e2e2ab5SHawking Zhang
psp_xgmi_initialize(struct psp_context * psp,bool set_extended_data,bool load_ta)123344357a1bSJonathan Kim int psp_xgmi_initialize(struct psp_context *psp, bool set_extended_data, bool load_ta)
12343e2e2ab5SHawking Zhang {
12353e2e2ab5SHawking Zhang struct ta_xgmi_shared_memory *xgmi_cmd;
12363e2e2ab5SHawking Zhang int ret;
12373e2e2ab5SHawking Zhang
12386457205cSCandice Li if (!psp->ta_fw ||
1239de3a1e33SCandice Li !psp->xgmi_context.context.bin_desc.size_bytes ||
1240de3a1e33SCandice Li !psp->xgmi_context.context.bin_desc.start_addr)
12411d69511eSAlex Deucher return -ENOENT;
12421d69511eSAlex Deucher
124344357a1bSJonathan Kim if (!load_ta)
124444357a1bSJonathan Kim goto invoke;
124544357a1bSJonathan Kim
1246ac1509d1SCandice Li psp->xgmi_context.context.mem_context.shared_mem_size = PSP_XGMI_SHARED_MEM_SIZE;
12473f83f17bSCandice Li psp->xgmi_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1248ac1509d1SCandice Li
1249a5457087SCandice Li if (!psp->xgmi_context.context.mem_context.shared_buf) {
1250f03d97b0SAlex Deucher ret = psp_ta_init_shared_buf(psp, &psp->xgmi_context.context.mem_context);
12513e2e2ab5SHawking Zhang if (ret)
12523e2e2ab5SHawking Zhang return ret;
12533e2e2ab5SHawking Zhang }
12543e2e2ab5SHawking Zhang
12553e2e2ab5SHawking Zhang /* Load XGMI TA */
1256f03d97b0SAlex Deucher ret = psp_ta_load(psp, &psp->xgmi_context.context);
1257ac1509d1SCandice Li if (!ret)
1258ac1509d1SCandice Li psp->xgmi_context.context.initialized = true;
1259ac1509d1SCandice Li else
12603e2e2ab5SHawking Zhang return ret;
12613e2e2ab5SHawking Zhang
126244357a1bSJonathan Kim invoke:
12633e2e2ab5SHawking Zhang /* Initialize XGMI session */
1264ce97f37bSCandice Li xgmi_cmd = (struct ta_xgmi_shared_memory *)(psp->xgmi_context.context.mem_context.shared_buf);
12653e2e2ab5SHawking Zhang memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
126644357a1bSJonathan Kim xgmi_cmd->flag_extend_link_record = set_extended_data;
12673e2e2ab5SHawking Zhang xgmi_cmd->cmd_id = TA_COMMAND_XGMI__INITIALIZE;
12683e2e2ab5SHawking Zhang
12693e2e2ab5SHawking Zhang ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
12703e2e2ab5SHawking Zhang
12713e2e2ab5SHawking Zhang return ret;
12723e2e2ab5SHawking Zhang }
12733e2e2ab5SHawking Zhang
psp_xgmi_get_hive_id(struct psp_context * psp,uint64_t * hive_id)127435ccba4eSHawking Zhang int psp_xgmi_get_hive_id(struct psp_context *psp, uint64_t *hive_id)
127535ccba4eSHawking Zhang {
127635ccba4eSHawking Zhang struct ta_xgmi_shared_memory *xgmi_cmd;
127735ccba4eSHawking Zhang int ret;
127835ccba4eSHawking Zhang
1279ce97f37bSCandice Li xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
128035ccba4eSHawking Zhang memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
128135ccba4eSHawking Zhang
128235ccba4eSHawking Zhang xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_HIVE_ID;
128335ccba4eSHawking Zhang
128435ccba4eSHawking Zhang /* Invoke xgmi ta to get hive id */
128535ccba4eSHawking Zhang ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
128635ccba4eSHawking Zhang if (ret)
128735ccba4eSHawking Zhang return ret;
128835ccba4eSHawking Zhang
128935ccba4eSHawking Zhang *hive_id = xgmi_cmd->xgmi_out_message.get_hive_id.hive_id;
129035ccba4eSHawking Zhang
129135ccba4eSHawking Zhang return 0;
129235ccba4eSHawking Zhang }
129335ccba4eSHawking Zhang
psp_xgmi_get_node_id(struct psp_context * psp,uint64_t * node_id)129435ccba4eSHawking Zhang int psp_xgmi_get_node_id(struct psp_context *psp, uint64_t *node_id)
129535ccba4eSHawking Zhang {
129635ccba4eSHawking Zhang struct ta_xgmi_shared_memory *xgmi_cmd;
129735ccba4eSHawking Zhang int ret;
129835ccba4eSHawking Zhang
1299ce97f37bSCandice Li xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
130035ccba4eSHawking Zhang memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
130135ccba4eSHawking Zhang
130235ccba4eSHawking Zhang xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_NODE_ID;
130335ccba4eSHawking Zhang
130435ccba4eSHawking Zhang /* Invoke xgmi ta to get the node id */
130535ccba4eSHawking Zhang ret = psp_xgmi_invoke(psp, xgmi_cmd->cmd_id);
130635ccba4eSHawking Zhang if (ret)
130735ccba4eSHawking Zhang return ret;
130835ccba4eSHawking Zhang
130935ccba4eSHawking Zhang *node_id = xgmi_cmd->xgmi_out_message.get_node_id.node_id;
131035ccba4eSHawking Zhang
131135ccba4eSHawking Zhang return 0;
131235ccba4eSHawking Zhang }
131335ccba4eSHawking Zhang
psp_xgmi_peer_link_info_supported(struct psp_context * psp)1314331e7818SJonathan Kim static bool psp_xgmi_peer_link_info_supported(struct psp_context *psp)
1315331e7818SJonathan Kim {
131607bc768aSJonathan Kim return (psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 2) &&
131707bc768aSJonathan Kim psp->xgmi_context.context.bin_desc.fw_version >= 0x2000000b) ||
131807bc768aSJonathan Kim psp->adev->ip_versions[MP0_HWIP][0] >= IP_VERSION(13, 0, 6);
1319331e7818SJonathan Kim }
1320331e7818SJonathan Kim
132144357a1bSJonathan Kim /*
132244357a1bSJonathan Kim * Chips that support extended topology information require the driver to
132344357a1bSJonathan Kim * reflect topology information in the opposite direction. This is
132444357a1bSJonathan Kim * because the TA has already exceeded its link record limit and if the
132544357a1bSJonathan Kim * TA holds bi-directional information, the driver would have to do
132644357a1bSJonathan Kim * multiple fetches instead of just two.
132744357a1bSJonathan Kim */
psp_xgmi_reflect_topology_info(struct psp_context * psp,struct psp_xgmi_node_info node_info)132844357a1bSJonathan Kim static void psp_xgmi_reflect_topology_info(struct psp_context *psp,
132944357a1bSJonathan Kim struct psp_xgmi_node_info node_info)
133044357a1bSJonathan Kim {
133144357a1bSJonathan Kim struct amdgpu_device *mirror_adev;
133244357a1bSJonathan Kim struct amdgpu_hive_info *hive;
133344357a1bSJonathan Kim uint64_t src_node_id = psp->adev->gmc.xgmi.node_id;
133444357a1bSJonathan Kim uint64_t dst_node_id = node_info.node_id;
133544357a1bSJonathan Kim uint8_t dst_num_hops = node_info.num_hops;
133644357a1bSJonathan Kim uint8_t dst_num_links = node_info.num_links;
133744357a1bSJonathan Kim
133844357a1bSJonathan Kim hive = amdgpu_get_xgmi_hive(psp->adev);
13394ab720b6SJesse Zhang if (WARN_ON(!hive))
13404ab720b6SJesse Zhang return;
13414ab720b6SJesse Zhang
134244357a1bSJonathan Kim list_for_each_entry(mirror_adev, &hive->device_list, gmc.xgmi.head) {
134344357a1bSJonathan Kim struct psp_xgmi_topology_info *mirror_top_info;
134444357a1bSJonathan Kim int j;
134544357a1bSJonathan Kim
134644357a1bSJonathan Kim if (mirror_adev->gmc.xgmi.node_id != dst_node_id)
134744357a1bSJonathan Kim continue;
134844357a1bSJonathan Kim
134944357a1bSJonathan Kim mirror_top_info = &mirror_adev->psp.xgmi_context.top_info;
135044357a1bSJonathan Kim for (j = 0; j < mirror_top_info->num_nodes; j++) {
135144357a1bSJonathan Kim if (mirror_top_info->nodes[j].node_id != src_node_id)
135244357a1bSJonathan Kim continue;
135344357a1bSJonathan Kim
135444357a1bSJonathan Kim mirror_top_info->nodes[j].num_hops = dst_num_hops;
135544357a1bSJonathan Kim /*
135644357a1bSJonathan Kim * prevent 0 num_links value re-reflection since reflection
135744357a1bSJonathan Kim * criteria is based on num_hops (direct or indirect).
135844357a1bSJonathan Kim *
135944357a1bSJonathan Kim */
136044357a1bSJonathan Kim if (dst_num_links)
136144357a1bSJonathan Kim mirror_top_info->nodes[j].num_links = dst_num_links;
136244357a1bSJonathan Kim
136344357a1bSJonathan Kim break;
136444357a1bSJonathan Kim }
136544357a1bSJonathan Kim
136644357a1bSJonathan Kim break;
136744357a1bSJonathan Kim }
13681ff186ffSJonathan Kim
13691ff186ffSJonathan Kim amdgpu_put_xgmi_hive(hive);
137044357a1bSJonathan Kim }
137144357a1bSJonathan Kim
psp_xgmi_get_topology_info(struct psp_context * psp,int number_devices,struct psp_xgmi_topology_info * topology,bool get_extended_data)137235ccba4eSHawking Zhang int psp_xgmi_get_topology_info(struct psp_context *psp,
137335ccba4eSHawking Zhang int number_devices,
137444357a1bSJonathan Kim struct psp_xgmi_topology_info *topology,
137544357a1bSJonathan Kim bool get_extended_data)
137635ccba4eSHawking Zhang {
137735ccba4eSHawking Zhang struct ta_xgmi_shared_memory *xgmi_cmd;
137835ccba4eSHawking Zhang struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
137935ccba4eSHawking Zhang struct ta_xgmi_cmd_get_topology_info_output *topology_info_output;
138035ccba4eSHawking Zhang int i;
138135ccba4eSHawking Zhang int ret;
138235ccba4eSHawking Zhang
138335ccba4eSHawking Zhang if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
138435ccba4eSHawking Zhang return -EINVAL;
138535ccba4eSHawking Zhang
1386ce97f37bSCandice Li xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
138735ccba4eSHawking Zhang memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
138844357a1bSJonathan Kim xgmi_cmd->flag_extend_link_record = get_extended_data;
138935ccba4eSHawking Zhang
139035ccba4eSHawking Zhang /* Fill in the shared memory with topology information as input */
139135ccba4eSHawking Zhang topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
139235ccba4eSHawking Zhang xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO;
139335ccba4eSHawking Zhang topology_info_input->num_nodes = number_devices;
139435ccba4eSHawking Zhang
139535ccba4eSHawking Zhang for (i = 0; i < topology_info_input->num_nodes; i++) {
139635ccba4eSHawking Zhang topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
139735ccba4eSHawking Zhang topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
139835ccba4eSHawking Zhang topology_info_input->nodes[i].is_sharing_enabled = topology->nodes[i].is_sharing_enabled;
139935ccba4eSHawking Zhang topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
140035ccba4eSHawking Zhang }
140135ccba4eSHawking Zhang
140235ccba4eSHawking Zhang /* Invoke xgmi ta to get the topology information */
140335ccba4eSHawking Zhang ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_GET_TOPOLOGY_INFO);
140435ccba4eSHawking Zhang if (ret)
140535ccba4eSHawking Zhang return ret;
140635ccba4eSHawking Zhang
140735ccba4eSHawking Zhang /* Read the output topology information from the shared memory */
140835ccba4eSHawking Zhang topology_info_output = &xgmi_cmd->xgmi_out_message.get_topology_info;
140935ccba4eSHawking Zhang topology->num_nodes = xgmi_cmd->xgmi_out_message.get_topology_info.num_nodes;
141035ccba4eSHawking Zhang for (i = 0; i < topology->num_nodes; i++) {
141144357a1bSJonathan Kim /* extended data will either be 0 or equal to non-extended data */
141244357a1bSJonathan Kim if (topology_info_output->nodes[i].num_hops)
141335ccba4eSHawking Zhang topology->nodes[i].num_hops = topology_info_output->nodes[i].num_hops;
141444357a1bSJonathan Kim
141544357a1bSJonathan Kim /* non-extended data gets everything here so no need to update */
141644357a1bSJonathan Kim if (!get_extended_data) {
141744357a1bSJonathan Kim topology->nodes[i].node_id = topology_info_output->nodes[i].node_id;
141844357a1bSJonathan Kim topology->nodes[i].is_sharing_enabled =
141944357a1bSJonathan Kim topology_info_output->nodes[i].is_sharing_enabled;
142044357a1bSJonathan Kim topology->nodes[i].sdma_engine =
142144357a1bSJonathan Kim topology_info_output->nodes[i].sdma_engine;
142244357a1bSJonathan Kim }
142344357a1bSJonathan Kim
142435ccba4eSHawking Zhang }
142535ccba4eSHawking Zhang
1426331e7818SJonathan Kim /* Invoke xgmi ta again to get the link information */
1427331e7818SJonathan Kim if (psp_xgmi_peer_link_info_supported(psp)) {
1428331e7818SJonathan Kim struct ta_xgmi_cmd_get_peer_link_info_output *link_info_output;
142907bc768aSJonathan Kim bool requires_reflection =
143007bc768aSJonathan Kim (psp->xgmi_context.supports_extended_data && get_extended_data) ||
143107bc768aSJonathan Kim psp->adev->ip_versions[MP0_HWIP][0] == IP_VERSION(13, 0, 6);
1432331e7818SJonathan Kim
1433331e7818SJonathan Kim xgmi_cmd->cmd_id = TA_COMMAND_XGMI__GET_PEER_LINKS;
1434331e7818SJonathan Kim
1435331e7818SJonathan Kim ret = psp_xgmi_invoke(psp, TA_COMMAND_XGMI__GET_PEER_LINKS);
1436331e7818SJonathan Kim
1437331e7818SJonathan Kim if (ret)
1438331e7818SJonathan Kim return ret;
1439331e7818SJonathan Kim
1440331e7818SJonathan Kim link_info_output = &xgmi_cmd->xgmi_out_message.get_link_info;
144144357a1bSJonathan Kim for (i = 0; i < topology->num_nodes; i++) {
144244357a1bSJonathan Kim /* accumulate num_links on extended data */
144344357a1bSJonathan Kim topology->nodes[i].num_links = get_extended_data ?
144444357a1bSJonathan Kim topology->nodes[i].num_links +
144544357a1bSJonathan Kim link_info_output->nodes[i].num_links :
14465ae0ec8bSShiwu Zhang ((requires_reflection && topology->nodes[i].num_links) ? topology->nodes[i].num_links :
14475ae0ec8bSShiwu Zhang link_info_output->nodes[i].num_links);
144844357a1bSJonathan Kim
144944357a1bSJonathan Kim /* reflect the topology information for bi-directionality */
145007bc768aSJonathan Kim if (requires_reflection && topology->nodes[i].num_hops)
145144357a1bSJonathan Kim psp_xgmi_reflect_topology_info(psp, topology->nodes[i]);
145244357a1bSJonathan Kim }
1453331e7818SJonathan Kim }
1454331e7818SJonathan Kim
145535ccba4eSHawking Zhang return 0;
145635ccba4eSHawking Zhang }
145735ccba4eSHawking Zhang
psp_xgmi_set_topology_info(struct psp_context * psp,int number_devices,struct psp_xgmi_topology_info * topology)145835ccba4eSHawking Zhang int psp_xgmi_set_topology_info(struct psp_context *psp,
145935ccba4eSHawking Zhang int number_devices,
146035ccba4eSHawking Zhang struct psp_xgmi_topology_info *topology)
146135ccba4eSHawking Zhang {
146235ccba4eSHawking Zhang struct ta_xgmi_shared_memory *xgmi_cmd;
146335ccba4eSHawking Zhang struct ta_xgmi_cmd_get_topology_info_input *topology_info_input;
146435ccba4eSHawking Zhang int i;
146535ccba4eSHawking Zhang
146635ccba4eSHawking Zhang if (!topology || topology->num_nodes > TA_XGMI__MAX_CONNECTED_NODES)
146735ccba4eSHawking Zhang return -EINVAL;
146835ccba4eSHawking Zhang
1469ce97f37bSCandice Li xgmi_cmd = (struct ta_xgmi_shared_memory *)psp->xgmi_context.context.mem_context.shared_buf;
147035ccba4eSHawking Zhang memset(xgmi_cmd, 0, sizeof(struct ta_xgmi_shared_memory));
147135ccba4eSHawking Zhang
147235ccba4eSHawking Zhang topology_info_input = &xgmi_cmd->xgmi_in_message.get_topology_info;
147335ccba4eSHawking Zhang xgmi_cmd->cmd_id = TA_COMMAND_XGMI__SET_TOPOLOGY_INFO;
147435ccba4eSHawking Zhang topology_info_input->num_nodes = number_devices;
147535ccba4eSHawking Zhang
147635ccba4eSHawking Zhang for (i = 0; i < topology_info_input->num_nodes; i++) {
147735ccba4eSHawking Zhang topology_info_input->nodes[i].node_id = topology->nodes[i].node_id;
147835ccba4eSHawking Zhang topology_info_input->nodes[i].num_hops = topology->nodes[i].num_hops;
147935ccba4eSHawking Zhang topology_info_input->nodes[i].is_sharing_enabled = 1;
148035ccba4eSHawking Zhang topology_info_input->nodes[i].sdma_engine = topology->nodes[i].sdma_engine;
148135ccba4eSHawking Zhang }
148235ccba4eSHawking Zhang
148335ccba4eSHawking Zhang /* Invoke xgmi ta to set topology information */
148435ccba4eSHawking Zhang return psp_xgmi_invoke(psp, TA_COMMAND_XGMI__SET_TOPOLOGY_INFO);
148535ccba4eSHawking Zhang }
148635ccba4eSHawking Zhang
14875e5d3154Sxinhui pan // ras begin
psp_ras_ta_check_status(struct psp_context * psp)14881b5254e8STao Zhou static void psp_ras_ta_check_status(struct psp_context *psp)
14891b5254e8STao Zhou {
14901b5254e8STao Zhou struct ta_ras_shared_memory *ras_cmd =
14911b5254e8STao Zhou (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
14921b5254e8STao Zhou
14931b5254e8STao Zhou switch (ras_cmd->ras_status) {
14941b5254e8STao Zhou case TA_RAS_STATUS__ERROR_UNSUPPORTED_IP:
14951b5254e8STao Zhou dev_warn(psp->adev->dev,
14961b5254e8STao Zhou "RAS WARNING: cmd failed due to unsupported ip\n");
14971b5254e8STao Zhou break;
149842f88ab7STao Zhou case TA_RAS_STATUS__ERROR_UNSUPPORTED_ERROR_INJ:
149942f88ab7STao Zhou dev_warn(psp->adev->dev,
150042f88ab7STao Zhou "RAS WARNING: cmd failed due to unsupported error injection\n");
150142f88ab7STao Zhou break;
15021b5254e8STao Zhou case TA_RAS_STATUS__SUCCESS:
15031b5254e8STao Zhou break;
150479c04621SStanley.Yang case TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED:
150579c04621SStanley.Yang if (ras_cmd->cmd_id == TA_RAS_COMMAND__TRIGGER_ERROR)
150679c04621SStanley.Yang dev_warn(psp->adev->dev,
150779c04621SStanley.Yang "RAS WARNING: Inject error to critical region is not allowed\n");
150879c04621SStanley.Yang break;
15091b5254e8STao Zhou default:
15101b5254e8STao Zhou dev_warn(psp->adev->dev,
15111b5254e8STao Zhou "RAS WARNING: ras status = 0x%X\n", ras_cmd->ras_status);
15121b5254e8STao Zhou break;
15131b5254e8STao Zhou }
15141b5254e8STao Zhou }
15151b5254e8STao Zhou
psp_ras_invoke(struct psp_context * psp,uint32_t ta_cmd_id)15165e5d3154Sxinhui pan int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
15175e5d3154Sxinhui pan {
151843965797SJohn Clements struct ta_ras_shared_memory *ras_cmd;
151943965797SJohn Clements int ret;
152043965797SJohn Clements
1521ce97f37bSCandice Li ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
152243965797SJohn Clements
15235e5d3154Sxinhui pan /*
15245e5d3154Sxinhui pan * TODO: bypass the loading in sriov for now
15255e5d3154Sxinhui pan */
15265e5d3154Sxinhui pan if (amdgpu_sriov_vf(psp->adev))
15275e5d3154Sxinhui pan return 0;
15285e5d3154Sxinhui pan
152977ec28eaSCandice Li ret = psp_ta_invoke(psp, ta_cmd_id, &psp->ras_context.context);
153043965797SJohn Clements
1531624e8c87SJohn Clements if (amdgpu_ras_intr_triggered())
1532624e8c87SJohn Clements return ret;
1533624e8c87SJohn Clements
1534f14c8c3eSSrinivasan Shanmugam if (ras_cmd->if_version > RAS_TA_HOST_IF_VER) {
153543965797SJohn Clements DRM_WARN("RAS: Unsupported Interface");
153643965797SJohn Clements return -EINVAL;
153743965797SJohn Clements }
153843965797SJohn Clements
153943965797SJohn Clements if (!ret) {
154043965797SJohn Clements if (ras_cmd->ras_out_message.flags.err_inject_switch_disable_flag) {
154143965797SJohn Clements dev_warn(psp->adev->dev, "ECC switch disabled\n");
154243965797SJohn Clements
154343965797SJohn Clements ras_cmd->ras_status = TA_RAS_STATUS__ERROR_RAS_NOT_AVAILABLE;
1544f14c8c3eSSrinivasan Shanmugam } else if (ras_cmd->ras_out_message.flags.reg_access_failure_flag)
154543965797SJohn Clements dev_warn(psp->adev->dev,
154643965797SJohn Clements "RAS internal register access blocked\n");
154773490d26SJohn Clements
15481b5254e8STao Zhou psp_ras_ta_check_status(psp);
154943965797SJohn Clements }
155043965797SJohn Clements
155143965797SJohn Clements return ret;
15525e5d3154Sxinhui pan }
15535e5d3154Sxinhui pan
psp_ras_enable_features(struct psp_context * psp,union ta_ras_cmd_input * info,bool enable)15545e5d3154Sxinhui pan int psp_ras_enable_features(struct psp_context *psp,
15555e5d3154Sxinhui pan union ta_ras_cmd_input *info, bool enable)
15565e5d3154Sxinhui pan {
15575e5d3154Sxinhui pan struct ta_ras_shared_memory *ras_cmd;
15585e5d3154Sxinhui pan int ret;
15595e5d3154Sxinhui pan
1560ce97f37bSCandice Li if (!psp->ras_context.context.initialized)
15615e5d3154Sxinhui pan return -EINVAL;
15625e5d3154Sxinhui pan
1563ce97f37bSCandice Li ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
15645e5d3154Sxinhui pan memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
15655e5d3154Sxinhui pan
15665e5d3154Sxinhui pan if (enable)
15675e5d3154Sxinhui pan ras_cmd->cmd_id = TA_RAS_COMMAND__ENABLE_FEATURES;
15685e5d3154Sxinhui pan else
15695e5d3154Sxinhui pan ras_cmd->cmd_id = TA_RAS_COMMAND__DISABLE_FEATURES;
15705e5d3154Sxinhui pan
15715e5d3154Sxinhui pan ras_cmd->ras_in_message = *info;
15725e5d3154Sxinhui pan
15735e5d3154Sxinhui pan ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
15745e5d3154Sxinhui pan if (ret)
15755e5d3154Sxinhui pan return -EINVAL;
15765e5d3154Sxinhui pan
1577334f81d1SJohn Clements return 0;
15785e5d3154Sxinhui pan }
15795e5d3154Sxinhui pan
psp_ras_terminate(struct psp_context * psp)1580fe96e563SCandice Li int psp_ras_terminate(struct psp_context *psp)
15815e5d3154Sxinhui pan {
15825e5d3154Sxinhui pan int ret;
15835e5d3154Sxinhui pan
1584edc2176dSJack Zhang /*
1585edc2176dSJack Zhang * TODO: bypass the terminate in sriov for now
1586edc2176dSJack Zhang */
1587edc2176dSJack Zhang if (amdgpu_sriov_vf(psp->adev))
1588edc2176dSJack Zhang return 0;
1589edc2176dSJack Zhang
1590ce97f37bSCandice Li if (!psp->ras_context.context.initialized)
15915e5d3154Sxinhui pan return 0;
15925e5d3154Sxinhui pan
1593f03d97b0SAlex Deucher ret = psp_ta_unload(psp, &psp->ras_context.context);
15945e5d3154Sxinhui pan
1595ce97f37bSCandice Li psp->ras_context.context.initialized = false;
15965e5d3154Sxinhui pan
1597fb4f4f42SAlex Deucher return ret;
15985e5d3154Sxinhui pan }
15995e5d3154Sxinhui pan
psp_ras_initialize(struct psp_context * psp)1600896b7addSCandice Li int psp_ras_initialize(struct psp_context *psp)
16015e5d3154Sxinhui pan {
16025e5d3154Sxinhui pan int ret;
16036246a416SHawking Zhang uint32_t boot_cfg = 0xFF;
16046246a416SHawking Zhang struct amdgpu_device *adev = psp->adev;
1605ac1509d1SCandice Li struct ta_ras_shared_memory *ras_cmd;
16065e5d3154Sxinhui pan
1607edc2176dSJack Zhang /*
1608edc2176dSJack Zhang * TODO: bypass the initialize in sriov for now
1609edc2176dSJack Zhang */
16106246a416SHawking Zhang if (amdgpu_sriov_vf(adev))
1611edc2176dSJack Zhang return 0;
1612edc2176dSJack Zhang
1613de3a1e33SCandice Li if (!adev->psp.ras_context.context.bin_desc.size_bytes ||
1614de3a1e33SCandice Li !adev->psp.ras_context.context.bin_desc.start_addr) {
16156246a416SHawking Zhang dev_info(adev->dev, "RAS: optional ras ta ucode is not available\n");
161651bd3638SHawking Zhang return 0;
161751bd3638SHawking Zhang }
161851bd3638SHawking Zhang
16196246a416SHawking Zhang if (amdgpu_atomfirmware_dynamic_boot_config_supported(adev)) {
16206246a416SHawking Zhang /* query GECC enablement status from boot config
16216246a416SHawking Zhang * boot_cfg: 1: GECC is enabled or 0: GECC is disabled
16226246a416SHawking Zhang */
16236246a416SHawking Zhang ret = psp_boot_config_get(adev, &boot_cfg);
16246246a416SHawking Zhang if (ret)
16256246a416SHawking Zhang dev_warn(adev->dev, "PSP get boot config failed\n");
16266246a416SHawking Zhang
16276246a416SHawking Zhang if (!amdgpu_ras_is_supported(psp->adev, AMDGPU_RAS_BLOCK__UMC)) {
16286246a416SHawking Zhang if (!boot_cfg) {
16296246a416SHawking Zhang dev_info(adev->dev, "GECC is disabled\n");
16306246a416SHawking Zhang } else {
16316246a416SHawking Zhang /* disable GECC in next boot cycle if ras is
16326246a416SHawking Zhang * disabled by module parameter amdgpu_ras_enable
16336246a416SHawking Zhang * and/or amdgpu_ras_mask, or boot_config_get call
16346246a416SHawking Zhang * is failed
16356246a416SHawking Zhang */
16366246a416SHawking Zhang ret = psp_boot_config_set(adev, 0);
16376246a416SHawking Zhang if (ret)
16386246a416SHawking Zhang dev_warn(adev->dev, "PSP set boot config failed\n");
16396246a416SHawking Zhang else
1640f14c8c3eSSrinivasan Shanmugam 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");
16416246a416SHawking Zhang }
16426246a416SHawking Zhang } else {
1643f14c8c3eSSrinivasan Shanmugam if (boot_cfg == 1) {
16446246a416SHawking Zhang dev_info(adev->dev, "GECC is enabled\n");
16456246a416SHawking Zhang } else {
16466246a416SHawking Zhang /* enable GECC in next boot cycle if it is disabled
16476246a416SHawking Zhang * in boot config, or force enable GECC if failed to
16486246a416SHawking Zhang * get boot configuration
16496246a416SHawking Zhang */
16506246a416SHawking Zhang ret = psp_boot_config_set(adev, BOOT_CONFIG_GECC);
16516246a416SHawking Zhang if (ret)
16526246a416SHawking Zhang dev_warn(adev->dev, "PSP set boot config failed\n");
16536246a416SHawking Zhang else
16546246a416SHawking Zhang dev_warn(adev->dev, "GECC will be enabled in next boot cycle\n");
16556246a416SHawking Zhang }
16566246a416SHawking Zhang }
16576246a416SHawking Zhang }
16586246a416SHawking Zhang
1659ac1509d1SCandice Li psp->ras_context.context.mem_context.shared_mem_size = PSP_RAS_SHARED_MEM_SIZE;
16603f83f17bSCandice Li psp->ras_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1661ac1509d1SCandice Li
1662bf7d7772SCandice Li if (!psp->ras_context.context.mem_context.shared_buf) {
1663f03d97b0SAlex Deucher ret = psp_ta_init_shared_buf(psp, &psp->ras_context.context.mem_context);
16645e5d3154Sxinhui pan if (ret)
16655e5d3154Sxinhui pan return ret;
16665e5d3154Sxinhui pan }
16675e5d3154Sxinhui pan
1668ac1509d1SCandice Li ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1669ac1509d1SCandice Li memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
16705e5d3154Sxinhui pan
1671e4348849STao Zhou if (amdgpu_ras_is_poison_mode_supported(adev))
1672ac1509d1SCandice Li ras_cmd->ras_in_message.init_flags.poison_mode_en = 1;
167338298ce6SStanley.Yang if (!adev->gmc.xgmi.connected_to_cpu && !adev->gmc.is_app_apu)
1674ac1509d1SCandice Li ras_cmd->ras_in_message.init_flags.dgpu_mode = 1;
167561a7c162SStanley.Yang ras_cmd->ras_in_message.init_flags.xcc_mask =
167661a7c162SStanley.Yang adev->gfx.xcc_mask;
16776fac3964SCandice Li ras_cmd->ras_in_message.init_flags.channel_dis_num = hweight32(adev->gmc.m_half_use) * 2;
1678ac1509d1SCandice Li
1679f03d97b0SAlex Deucher ret = psp_ta_load(psp, &psp->ras_context.context);
1680ac1509d1SCandice Li
1681ac1509d1SCandice Li if (!ret && !ras_cmd->ras_status)
1682ac1509d1SCandice Li psp->ras_context.context.initialized = true;
1683ac1509d1SCandice Li else {
1684ac1509d1SCandice Li if (ras_cmd->ras_status)
1685ac1509d1SCandice Li dev_warn(psp->adev->dev, "RAS Init Status: 0x%X\n", ras_cmd->ras_status);
16863e931368STao Zhou
16873e931368STao Zhou /* fail to load RAS TA */
16883e931368STao Zhou psp->ras_context.context.initialized = false;
1689ac1509d1SCandice Li }
1690ac1509d1SCandice Li
1691ac1509d1SCandice Li return ret;
16925e5d3154Sxinhui pan }
1693001a0a95SHawking Zhang
psp_ras_trigger_error(struct psp_context * psp,struct ta_ras_trigger_error_input * info,uint32_t instance_mask)1694001a0a95SHawking Zhang int psp_ras_trigger_error(struct psp_context *psp,
16952c22ed0bSTao Zhou struct ta_ras_trigger_error_input *info, uint32_t instance_mask)
1696001a0a95SHawking Zhang {
1697001a0a95SHawking Zhang struct ta_ras_shared_memory *ras_cmd;
16982c22ed0bSTao Zhou struct amdgpu_device *adev = psp->adev;
1699001a0a95SHawking Zhang int ret;
17002c22ed0bSTao Zhou uint32_t dev_mask;
1701001a0a95SHawking Zhang
1702ce97f37bSCandice Li if (!psp->ras_context.context.initialized)
1703001a0a95SHawking Zhang return -EINVAL;
1704001a0a95SHawking Zhang
17052c22ed0bSTao Zhou switch (info->block_id) {
17062c22ed0bSTao Zhou case TA_RAS_BLOCK__GFX:
17072c22ed0bSTao Zhou dev_mask = GET_MASK(GC, instance_mask);
17082c22ed0bSTao Zhou break;
17092c22ed0bSTao Zhou case TA_RAS_BLOCK__SDMA:
17102c22ed0bSTao Zhou dev_mask = GET_MASK(SDMA0, instance_mask);
17112c22ed0bSTao Zhou break;
17123898c8fcSStanley.Yang case TA_RAS_BLOCK__VCN:
17133898c8fcSStanley.Yang case TA_RAS_BLOCK__JPEG:
17143898c8fcSStanley.Yang dev_mask = GET_MASK(VCN, instance_mask);
17153898c8fcSStanley.Yang break;
17162c22ed0bSTao Zhou default:
17172c22ed0bSTao Zhou dev_mask = instance_mask;
17182c22ed0bSTao Zhou break;
17192c22ed0bSTao Zhou }
17202c22ed0bSTao Zhou
17212c22ed0bSTao Zhou /* reuse sub_block_index for backward compatibility */
17222c22ed0bSTao Zhou dev_mask <<= AMDGPU_RAS_INST_SHIFT;
17232c22ed0bSTao Zhou dev_mask &= AMDGPU_RAS_INST_MASK;
17242c22ed0bSTao Zhou info->sub_block_index |= dev_mask;
17252c22ed0bSTao Zhou
1726ce97f37bSCandice Li ras_cmd = (struct ta_ras_shared_memory *)psp->ras_context.context.mem_context.shared_buf;
1727001a0a95SHawking Zhang memset(ras_cmd, 0, sizeof(struct ta_ras_shared_memory));
1728001a0a95SHawking Zhang
1729001a0a95SHawking Zhang ras_cmd->cmd_id = TA_RAS_COMMAND__TRIGGER_ERROR;
1730001a0a95SHawking Zhang ras_cmd->ras_in_message.trigger_error = *info;
1731001a0a95SHawking Zhang
1732001a0a95SHawking Zhang ret = psp_ras_invoke(psp, ras_cmd->cmd_id);
1733001a0a95SHawking Zhang if (ret)
1734001a0a95SHawking Zhang return -EINVAL;
1735001a0a95SHawking Zhang
1736001a0a95SHawking Zhang /* If err_event_athub occurs error inject was successful, however
17372d5c0415SPraful Swarnakar * return status from TA is no long reliable
17382d5c0415SPraful Swarnakar */
1739001a0a95SHawking Zhang if (amdgpu_ras_intr_triggered())
1740001a0a95SHawking Zhang return 0;
1741001a0a95SHawking Zhang
174279c04621SStanley.Yang if (ras_cmd->ras_status == TA_RAS_STATUS__TEE_ERROR_ACCESS_DENIED)
174379c04621SStanley.Yang return -EACCES;
174479c04621SStanley.Yang else if (ras_cmd->ras_status)
1745334f81d1SJohn Clements return -EINVAL;
1746334f81d1SJohn Clements
1747334f81d1SJohn Clements return 0;
1748001a0a95SHawking Zhang }
17495e5d3154Sxinhui pan // ras end
17505e5d3154Sxinhui pan
1751ed19a9a2SBhawanpreet Lakha // HDCP start
psp_hdcp_initialize(struct psp_context * psp)1752ed19a9a2SBhawanpreet Lakha static int psp_hdcp_initialize(struct psp_context *psp)
1753ed19a9a2SBhawanpreet Lakha {
1754ed19a9a2SBhawanpreet Lakha int ret;
1755ed19a9a2SBhawanpreet Lakha
1756edc2176dSJack Zhang /*
1757edc2176dSJack Zhang * TODO: bypass the initialize in sriov for now
1758edc2176dSJack Zhang */
1759edc2176dSJack Zhang if (amdgpu_sriov_vf(psp->adev))
1760edc2176dSJack Zhang return 0;
1761edc2176dSJack Zhang
1762de3a1e33SCandice Li if (!psp->hdcp_context.context.bin_desc.size_bytes ||
1763de3a1e33SCandice Li !psp->hdcp_context.context.bin_desc.start_addr) {
1764a45a9e5eSAlex Deucher dev_info(psp->adev->dev, "HDCP: optional hdcp ta ucode is not available\n");
176551bd3638SHawking Zhang return 0;
176651bd3638SHawking Zhang }
176751bd3638SHawking Zhang
1768ac1509d1SCandice Li psp->hdcp_context.context.mem_context.shared_mem_size = PSP_HDCP_SHARED_MEM_SIZE;
17693f83f17bSCandice Li psp->hdcp_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1770ac1509d1SCandice Li
177161d2a9beSHoratio Zhang if (!psp->hdcp_context.context.mem_context.shared_buf) {
1772f03d97b0SAlex Deucher ret = psp_ta_init_shared_buf(psp, &psp->hdcp_context.context.mem_context);
1773ed19a9a2SBhawanpreet Lakha if (ret)
1774ed19a9a2SBhawanpreet Lakha return ret;
1775ed19a9a2SBhawanpreet Lakha }
1776ed19a9a2SBhawanpreet Lakha
1777f03d97b0SAlex Deucher ret = psp_ta_load(psp, &psp->hdcp_context.context);
1778ac1509d1SCandice Li if (!ret) {
1779ac1509d1SCandice Li psp->hdcp_context.context.initialized = true;
1780ac1509d1SCandice Li mutex_init(&psp->hdcp_context.mutex);
1781ac1509d1SCandice Li }
1782ed19a9a2SBhawanpreet Lakha
1783ac1509d1SCandice Li return ret;
1784ed19a9a2SBhawanpreet Lakha }
1785ed19a9a2SBhawanpreet Lakha
psp_hdcp_invoke(struct psp_context * psp,uint32_t ta_cmd_id)1786ed19a9a2SBhawanpreet Lakha int psp_hdcp_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1787ed19a9a2SBhawanpreet Lakha {
1788ed19a9a2SBhawanpreet Lakha /*
1789ed19a9a2SBhawanpreet Lakha * TODO: bypass the loading in sriov for now
1790ed19a9a2SBhawanpreet Lakha */
1791ed19a9a2SBhawanpreet Lakha if (amdgpu_sriov_vf(psp->adev))
1792ed19a9a2SBhawanpreet Lakha return 0;
1793ed19a9a2SBhawanpreet Lakha
179477ec28eaSCandice Li return psp_ta_invoke(psp, ta_cmd_id, &psp->hdcp_context.context);
1795ed19a9a2SBhawanpreet Lakha }
1796ed19a9a2SBhawanpreet Lakha
psp_hdcp_terminate(struct psp_context * psp)1797ed19a9a2SBhawanpreet Lakha static int psp_hdcp_terminate(struct psp_context *psp)
1798ed19a9a2SBhawanpreet Lakha {
1799ed19a9a2SBhawanpreet Lakha int ret;
1800ed19a9a2SBhawanpreet Lakha
1801edc2176dSJack Zhang /*
1802edc2176dSJack Zhang * TODO: bypass the terminate in sriov for now
1803edc2176dSJack Zhang */
1804edc2176dSJack Zhang if (amdgpu_sriov_vf(psp->adev))
1805edc2176dSJack Zhang return 0;
1806edc2176dSJack Zhang
1807da40bf8fSAlex Deucher if (!psp->hdcp_context.context.initialized)
1808ed19a9a2SBhawanpreet Lakha return 0;
1809ed19a9a2SBhawanpreet Lakha
1810f03d97b0SAlex Deucher ret = psp_ta_unload(psp, &psp->hdcp_context.context);
1811ed19a9a2SBhawanpreet Lakha
1812ce97f37bSCandice Li psp->hdcp_context.context.initialized = false;
1813ed19a9a2SBhawanpreet Lakha
1814fb4f4f42SAlex Deucher return ret;
1815ed19a9a2SBhawanpreet Lakha }
1816ed19a9a2SBhawanpreet Lakha // HDCP end
1817ed19a9a2SBhawanpreet Lakha
1818143f2305SBhawanpreet Lakha // DTM start
psp_dtm_initialize(struct psp_context * psp)1819143f2305SBhawanpreet Lakha static int psp_dtm_initialize(struct psp_context *psp)
1820143f2305SBhawanpreet Lakha {
1821143f2305SBhawanpreet Lakha int ret;
1822143f2305SBhawanpreet Lakha
1823edc2176dSJack Zhang /*
1824edc2176dSJack Zhang * TODO: bypass the initialize in sriov for now
1825edc2176dSJack Zhang */
1826edc2176dSJack Zhang if (amdgpu_sriov_vf(psp->adev))
1827edc2176dSJack Zhang return 0;
1828edc2176dSJack Zhang
1829de3a1e33SCandice Li if (!psp->dtm_context.context.bin_desc.size_bytes ||
1830de3a1e33SCandice Li !psp->dtm_context.context.bin_desc.start_addr) {
1831a45a9e5eSAlex Deucher dev_info(psp->adev->dev, "DTM: optional dtm ta ucode is not available\n");
183251bd3638SHawking Zhang return 0;
183351bd3638SHawking Zhang }
183451bd3638SHawking Zhang
1835ac1509d1SCandice Li psp->dtm_context.context.mem_context.shared_mem_size = PSP_DTM_SHARED_MEM_SIZE;
18363f83f17bSCandice Li psp->dtm_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1837ac1509d1SCandice Li
183861d2a9beSHoratio Zhang if (!psp->dtm_context.context.mem_context.shared_buf) {
1839f03d97b0SAlex Deucher ret = psp_ta_init_shared_buf(psp, &psp->dtm_context.context.mem_context);
1840143f2305SBhawanpreet Lakha if (ret)
1841143f2305SBhawanpreet Lakha return ret;
1842143f2305SBhawanpreet Lakha }
1843143f2305SBhawanpreet Lakha
1844f03d97b0SAlex Deucher ret = psp_ta_load(psp, &psp->dtm_context.context);
1845ac1509d1SCandice Li if (!ret) {
1846ac1509d1SCandice Li psp->dtm_context.context.initialized = true;
1847ac1509d1SCandice Li mutex_init(&psp->dtm_context.mutex);
1848ac1509d1SCandice Li }
1849143f2305SBhawanpreet Lakha
1850ac1509d1SCandice Li return ret;
1851143f2305SBhawanpreet Lakha }
1852143f2305SBhawanpreet Lakha
psp_dtm_invoke(struct psp_context * psp,uint32_t ta_cmd_id)1853143f2305SBhawanpreet Lakha int psp_dtm_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
1854143f2305SBhawanpreet Lakha {
1855143f2305SBhawanpreet Lakha /*
1856143f2305SBhawanpreet Lakha * TODO: bypass the loading in sriov for now
1857143f2305SBhawanpreet Lakha */
1858143f2305SBhawanpreet Lakha if (amdgpu_sriov_vf(psp->adev))
1859143f2305SBhawanpreet Lakha return 0;
1860143f2305SBhawanpreet Lakha
186177ec28eaSCandice Li return psp_ta_invoke(psp, ta_cmd_id, &psp->dtm_context.context);
1862143f2305SBhawanpreet Lakha }
1863143f2305SBhawanpreet Lakha
psp_dtm_terminate(struct psp_context * psp)1864143f2305SBhawanpreet Lakha static int psp_dtm_terminate(struct psp_context *psp)
1865143f2305SBhawanpreet Lakha {
1866143f2305SBhawanpreet Lakha int ret;
1867143f2305SBhawanpreet Lakha
1868edc2176dSJack Zhang /*
1869edc2176dSJack Zhang * TODO: bypass the terminate in sriov for now
1870edc2176dSJack Zhang */
1871edc2176dSJack Zhang if (amdgpu_sriov_vf(psp->adev))
1872edc2176dSJack Zhang return 0;
1873edc2176dSJack Zhang
1874da40bf8fSAlex Deucher if (!psp->dtm_context.context.initialized)
1875143f2305SBhawanpreet Lakha return 0;
1876143f2305SBhawanpreet Lakha
1877f03d97b0SAlex Deucher ret = psp_ta_unload(psp, &psp->dtm_context.context);
1878143f2305SBhawanpreet Lakha
1879ce97f37bSCandice Li psp->dtm_context.context.initialized = false;
1880143f2305SBhawanpreet Lakha
1881fb4f4f42SAlex Deucher return ret;
1882143f2305SBhawanpreet Lakha }
1883143f2305SBhawanpreet Lakha // DTM end
1884143f2305SBhawanpreet Lakha
18858602692bSWenhui Sheng // RAP start
psp_rap_initialize(struct psp_context * psp)18868602692bSWenhui Sheng static int psp_rap_initialize(struct psp_context *psp)
18878602692bSWenhui Sheng {
18888602692bSWenhui Sheng int ret;
18892fb3c5d0SKevin Wang enum ta_rap_status status = TA_RAP_STATUS__SUCCESS;
18908602692bSWenhui Sheng
18918602692bSWenhui Sheng /*
18928602692bSWenhui Sheng * TODO: bypass the initialize in sriov for now
18938602692bSWenhui Sheng */
18948602692bSWenhui Sheng if (amdgpu_sriov_vf(psp->adev))
18958602692bSWenhui Sheng return 0;
18968602692bSWenhui Sheng
1897de3a1e33SCandice Li if (!psp->rap_context.context.bin_desc.size_bytes ||
1898de3a1e33SCandice Li !psp->rap_context.context.bin_desc.start_addr) {
18998602692bSWenhui Sheng dev_info(psp->adev->dev, "RAP: optional rap ta ucode is not available\n");
19008602692bSWenhui Sheng return 0;
19018602692bSWenhui Sheng }
19028602692bSWenhui Sheng
1903ac1509d1SCandice Li psp->rap_context.context.mem_context.shared_mem_size = PSP_RAP_SHARED_MEM_SIZE;
19043f83f17bSCandice Li psp->rap_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
1905ac1509d1SCandice Li
190661d2a9beSHoratio Zhang if (!psp->rap_context.context.mem_context.shared_buf) {
1907f03d97b0SAlex Deucher ret = psp_ta_init_shared_buf(psp, &psp->rap_context.context.mem_context);
19088602692bSWenhui Sheng if (ret)
19098602692bSWenhui Sheng return ret;
19108602692bSWenhui Sheng }
19118602692bSWenhui Sheng
1912f03d97b0SAlex Deucher ret = psp_ta_load(psp, &psp->rap_context.context);
1913ac1509d1SCandice Li if (!ret) {
1914ac1509d1SCandice Li psp->rap_context.context.initialized = true;
1915ac1509d1SCandice Li mutex_init(&psp->rap_context.mutex);
1916ac1509d1SCandice Li } else
19178602692bSWenhui Sheng return ret;
19188602692bSWenhui Sheng
19192fb3c5d0SKevin Wang ret = psp_rap_invoke(psp, TA_CMD_RAP__INITIALIZE, &status);
19202fb3c5d0SKevin Wang if (ret || status != TA_RAP_STATUS__SUCCESS) {
192125c94b33SCandice Li psp_rap_terminate(psp);
1922da40bf8fSAlex Deucher /* free rap shared memory */
1923da40bf8fSAlex Deucher psp_ta_free_shared_buf(&psp->rap_context.context.mem_context);
19248602692bSWenhui Sheng
19252fb3c5d0SKevin Wang dev_warn(psp->adev->dev, "RAP TA initialize fail (%d) status %d.\n",
19262fb3c5d0SKevin Wang ret, status);
19272fb3c5d0SKevin Wang
19282fb3c5d0SKevin Wang return ret;
19298602692bSWenhui Sheng }
19308602692bSWenhui Sheng
19318602692bSWenhui Sheng return 0;
19328602692bSWenhui Sheng }
19338602692bSWenhui Sheng
psp_rap_terminate(struct psp_context * psp)19348602692bSWenhui Sheng static int psp_rap_terminate(struct psp_context *psp)
19358602692bSWenhui Sheng {
19368602692bSWenhui Sheng int ret;
19378602692bSWenhui Sheng
1938ce97f37bSCandice Li if (!psp->rap_context.context.initialized)
19398602692bSWenhui Sheng return 0;
19408602692bSWenhui Sheng
1941f03d97b0SAlex Deucher ret = psp_ta_unload(psp, &psp->rap_context.context);
19428602692bSWenhui Sheng
1943ce97f37bSCandice Li psp->rap_context.context.initialized = false;
19448602692bSWenhui Sheng
19458602692bSWenhui Sheng return ret;
19468602692bSWenhui Sheng }
19478602692bSWenhui Sheng
psp_rap_invoke(struct psp_context * psp,uint32_t ta_cmd_id,enum ta_rap_status * status)19482fb3c5d0SKevin Wang int psp_rap_invoke(struct psp_context *psp, uint32_t ta_cmd_id, enum ta_rap_status *status)
19498602692bSWenhui Sheng {
19508602692bSWenhui Sheng struct ta_rap_shared_memory *rap_cmd;
19512fb3c5d0SKevin Wang int ret = 0;
19528602692bSWenhui Sheng
1953ce97f37bSCandice Li if (!psp->rap_context.context.initialized)
19542fb3c5d0SKevin Wang return 0;
19558602692bSWenhui Sheng
19568602692bSWenhui Sheng if (ta_cmd_id != TA_CMD_RAP__INITIALIZE &&
19578602692bSWenhui Sheng ta_cmd_id != TA_CMD_RAP__VALIDATE_L0)
19588602692bSWenhui Sheng return -EINVAL;
19598602692bSWenhui Sheng
19608602692bSWenhui Sheng mutex_lock(&psp->rap_context.mutex);
19618602692bSWenhui Sheng
19628602692bSWenhui Sheng rap_cmd = (struct ta_rap_shared_memory *)
1963ce97f37bSCandice Li psp->rap_context.context.mem_context.shared_buf;
19648602692bSWenhui Sheng memset(rap_cmd, 0, sizeof(struct ta_rap_shared_memory));
19658602692bSWenhui Sheng
19668602692bSWenhui Sheng rap_cmd->cmd_id = ta_cmd_id;
19678602692bSWenhui Sheng rap_cmd->validation_method_id = METHOD_A;
19688602692bSWenhui Sheng
196977ec28eaSCandice Li ret = psp_ta_invoke(psp, rap_cmd->cmd_id, &psp->rap_context.context);
19702fb3c5d0SKevin Wang if (ret)
19712fb3c5d0SKevin Wang goto out_unlock;
19722fb3c5d0SKevin Wang
19732fb3c5d0SKevin Wang if (status)
19742fb3c5d0SKevin Wang *status = rap_cmd->rap_status;
19752fb3c5d0SKevin Wang
19762fb3c5d0SKevin Wang out_unlock:
19778602692bSWenhui Sheng mutex_unlock(&psp->rap_context.mutex);
19782fb3c5d0SKevin Wang
19798602692bSWenhui Sheng return ret;
19808602692bSWenhui Sheng }
19818602692bSWenhui Sheng // RAP end
19828602692bSWenhui Sheng
1983ecaafb7bSJinzhou Su /* securedisplay start */
psp_securedisplay_initialize(struct psp_context * psp)1984ecaafb7bSJinzhou Su static int psp_securedisplay_initialize(struct psp_context *psp)
1985ecaafb7bSJinzhou Su {
1986ecaafb7bSJinzhou Su int ret;
1987f6e856e7SAaron Liu struct ta_securedisplay_cmd *securedisplay_cmd;
1988ecaafb7bSJinzhou Su
1989ecaafb7bSJinzhou Su /*
1990ecaafb7bSJinzhou Su * TODO: bypass the initialize in sriov for now
1991ecaafb7bSJinzhou Su */
1992ecaafb7bSJinzhou Su if (amdgpu_sriov_vf(psp->adev))
1993ecaafb7bSJinzhou Su return 0;
1994ecaafb7bSJinzhou Su
1995de3a1e33SCandice Li if (!psp->securedisplay_context.context.bin_desc.size_bytes ||
1996de3a1e33SCandice Li !psp->securedisplay_context.context.bin_desc.start_addr) {
1997ecaafb7bSJinzhou Su dev_info(psp->adev->dev, "SECUREDISPLAY: securedisplay ta ucode is not available\n");
1998ecaafb7bSJinzhou Su return 0;
1999ecaafb7bSJinzhou Su }
2000ecaafb7bSJinzhou Su
2001ac1509d1SCandice Li psp->securedisplay_context.context.mem_context.shared_mem_size =
2002ac1509d1SCandice Li PSP_SECUREDISPLAY_SHARED_MEM_SIZE;
20033f83f17bSCandice Li psp->securedisplay_context.context.ta_load_type = GFX_CMD_ID_LOAD_TA;
2004ac1509d1SCandice Li
2005ce97f37bSCandice Li if (!psp->securedisplay_context.context.initialized) {
2006f03d97b0SAlex Deucher ret = psp_ta_init_shared_buf(psp,
2007f03d97b0SAlex Deucher &psp->securedisplay_context.context.mem_context);
2008ecaafb7bSJinzhou Su if (ret)
2009ecaafb7bSJinzhou Su return ret;
2010ecaafb7bSJinzhou Su }
2011ecaafb7bSJinzhou Su
2012f03d97b0SAlex Deucher ret = psp_ta_load(psp, &psp->securedisplay_context.context);
2013ac1509d1SCandice Li if (!ret) {
2014ac1509d1SCandice Li psp->securedisplay_context.context.initialized = true;
2015ac1509d1SCandice Li mutex_init(&psp->securedisplay_context.mutex);
2016ac1509d1SCandice Li } else
2017ecaafb7bSJinzhou Su return ret;
2018ecaafb7bSJinzhou Su
20197117007eSAlan Liu mutex_lock(&psp->securedisplay_context.mutex);
20207117007eSAlan Liu
2021ecaafb7bSJinzhou Su psp_prep_securedisplay_cmd_buf(psp, &securedisplay_cmd,
2022ecaafb7bSJinzhou Su TA_SECUREDISPLAY_COMMAND__QUERY_TA);
2023ecaafb7bSJinzhou Su
2024ecaafb7bSJinzhou Su ret = psp_securedisplay_invoke(psp, TA_SECUREDISPLAY_COMMAND__QUERY_TA);
20257117007eSAlan Liu
20267117007eSAlan Liu mutex_unlock(&psp->securedisplay_context.mutex);
20277117007eSAlan Liu
2028ecaafb7bSJinzhou Su if (ret) {
202925c94b33SCandice Li psp_securedisplay_terminate(psp);
2030da40bf8fSAlex Deucher /* free securedisplay shared memory */
2031da40bf8fSAlex Deucher psp_ta_free_shared_buf(&psp->securedisplay_context.context.mem_context);
2032ecaafb7bSJinzhou Su dev_err(psp->adev->dev, "SECUREDISPLAY TA initialize fail.\n");
2033ecaafb7bSJinzhou Su return -EINVAL;
2034ecaafb7bSJinzhou Su }
2035ecaafb7bSJinzhou Su
2036ecaafb7bSJinzhou Su if (securedisplay_cmd->status != TA_SECUREDISPLAY_STATUS__SUCCESS) {
2037ecaafb7bSJinzhou Su psp_securedisplay_parse_resp_status(psp, securedisplay_cmd->status);
2038ecaafb7bSJinzhou Su dev_err(psp->adev->dev, "SECUREDISPLAY: query securedisplay TA failed. ret 0x%x\n",
2039ecaafb7bSJinzhou Su securedisplay_cmd->securedisplay_out_message.query_ta.query_cmd_ret);
20405c6d52ffSMario Limonciello /* don't try again */
20415c6d52ffSMario Limonciello psp->securedisplay_context.context.bin_desc.size_bytes = 0;
2042ecaafb7bSJinzhou Su }
2043ecaafb7bSJinzhou Su
2044ecaafb7bSJinzhou Su return 0;
2045ecaafb7bSJinzhou Su }
2046ecaafb7bSJinzhou Su
psp_securedisplay_terminate(struct psp_context * psp)2047ecaafb7bSJinzhou Su static int psp_securedisplay_terminate(struct psp_context *psp)
2048ecaafb7bSJinzhou Su {
2049ecaafb7bSJinzhou Su int ret;
2050ecaafb7bSJinzhou Su
2051ecaafb7bSJinzhou Su /*
2052ecaafb7bSJinzhou Su * TODO:bypass the terminate in sriov for now
2053ecaafb7bSJinzhou Su */
2054ecaafb7bSJinzhou Su if (amdgpu_sriov_vf(psp->adev))
2055ecaafb7bSJinzhou Su return 0;
2056ecaafb7bSJinzhou Su
2057ce97f37bSCandice Li if (!psp->securedisplay_context.context.initialized)
2058ecaafb7bSJinzhou Su return 0;
2059ecaafb7bSJinzhou Su
2060f03d97b0SAlex Deucher ret = psp_ta_unload(psp, &psp->securedisplay_context.context);
2061ecaafb7bSJinzhou Su
2062ce97f37bSCandice Li psp->securedisplay_context.context.initialized = false;
2063ecaafb7bSJinzhou Su
2064ecaafb7bSJinzhou Su return ret;
2065ecaafb7bSJinzhou Su }
2066ecaafb7bSJinzhou Su
psp_securedisplay_invoke(struct psp_context * psp,uint32_t ta_cmd_id)2067ecaafb7bSJinzhou Su int psp_securedisplay_invoke(struct psp_context *psp, uint32_t ta_cmd_id)
2068ecaafb7bSJinzhou Su {
2069ecaafb7bSJinzhou Su int ret;
2070ecaafb7bSJinzhou Su
2071ce97f37bSCandice Li if (!psp->securedisplay_context.context.initialized)
2072ecaafb7bSJinzhou Su return -EINVAL;
2073ecaafb7bSJinzhou Su
2074ecaafb7bSJinzhou Su if (ta_cmd_id != TA_SECUREDISPLAY_COMMAND__QUERY_TA &&
2075ecaafb7bSJinzhou Su ta_cmd_id != TA_SECUREDISPLAY_COMMAND__SEND_ROI_CRC)
2076ecaafb7bSJinzhou Su return -EINVAL;
2077ecaafb7bSJinzhou Su
207877ec28eaSCandice Li ret = psp_ta_invoke(psp, ta_cmd_id, &psp->securedisplay_context.context);
2079ecaafb7bSJinzhou Su
2080ecaafb7bSJinzhou Su return ret;
2081ecaafb7bSJinzhou Su }
2082ecaafb7bSJinzhou Su /* SECUREDISPLAY end */
2083ecaafb7bSJinzhou Su
amdgpu_psp_wait_for_bootloader(struct amdgpu_device * adev)20847656168aSLijo Lazar int amdgpu_psp_wait_for_bootloader(struct amdgpu_device *adev)
20857656168aSLijo Lazar {
20867656168aSLijo Lazar struct psp_context *psp = &adev->psp;
20877656168aSLijo Lazar int ret = 0;
20887656168aSLijo Lazar
20897656168aSLijo Lazar if (!amdgpu_sriov_vf(adev) && psp->funcs && psp->funcs->wait_for_bootloader != NULL)
20907656168aSLijo Lazar ret = psp->funcs->wait_for_bootloader(psp);
20917656168aSLijo Lazar
20927656168aSLijo Lazar return ret;
20937656168aSLijo Lazar }
20947656168aSLijo Lazar
psp_hw_start(struct psp_context * psp)2095be70bbdaSHuang Rui static int psp_hw_start(struct psp_context *psp)
2096be70bbdaSHuang Rui {
209755981bd2SMonk Liu struct amdgpu_device *adev = psp->adev;
2098be70bbdaSHuang Rui int ret;
2099be70bbdaSHuang Rui
210082c4ebfaSMonk Liu if (!amdgpu_sriov_vf(adev)) {
2101222e0a71SCandice Li if ((is_psp_fw_valid(psp->kdb)) &&
210242989359SHawking Zhang (psp->funcs->bootloader_load_kdb != NULL)) {
210342989359SHawking Zhang ret = psp_bootloader_load_kdb(psp);
210442989359SHawking Zhang if (ret) {
210542989359SHawking Zhang DRM_ERROR("PSP load kdb failed!\n");
210642989359SHawking Zhang return ret;
210742989359SHawking Zhang }
210842989359SHawking Zhang }
210942989359SHawking Zhang
2110222e0a71SCandice Li if ((is_psp_fw_valid(psp->spl)) &&
2111222e0a71SCandice Li (psp->funcs->bootloader_load_spl != NULL)) {
211270509057SLikun Gao ret = psp_bootloader_load_spl(psp);
211370509057SLikun Gao if (ret) {
211470509057SLikun Gao DRM_ERROR("PSP load spl failed!\n");
211570509057SLikun Gao return ret;
211670509057SLikun Gao }
211770509057SLikun Gao }
211870509057SLikun Gao
2119222e0a71SCandice Li if ((is_psp_fw_valid(psp->sys)) &&
2120222e0a71SCandice Li (psp->funcs->bootloader_load_sysdrv != NULL)) {
21210e5ca0d1SHuang Rui ret = psp_bootloader_load_sysdrv(psp);
21227a3d7bf6SEvan Quan if (ret) {
21237a3d7bf6SEvan Quan DRM_ERROR("PSP load sys drv failed!\n");
2124be70bbdaSHuang Rui return ret;
212555981bd2SMonk Liu }
2126222e0a71SCandice Li }
21270e5ca0d1SHuang Rui
21286ff34fd6SJohn Clements if ((is_psp_fw_valid(psp->soc_drv)) &&
21296ff34fd6SJohn Clements (psp->funcs->bootloader_load_soc_drv != NULL)) {
21306ff34fd6SJohn Clements ret = psp_bootloader_load_soc_drv(psp);
21316ff34fd6SJohn Clements if (ret) {
21326ff34fd6SJohn Clements DRM_ERROR("PSP load soc drv failed!\n");
21336ff34fd6SJohn Clements return ret;
21346ff34fd6SJohn Clements }
21356ff34fd6SJohn Clements }
21366ff34fd6SJohn Clements
21376ff34fd6SJohn Clements if ((is_psp_fw_valid(psp->intf_drv)) &&
21386ff34fd6SJohn Clements (psp->funcs->bootloader_load_intf_drv != NULL)) {
21396ff34fd6SJohn Clements ret = psp_bootloader_load_intf_drv(psp);
21406ff34fd6SJohn Clements if (ret) {
21416ff34fd6SJohn Clements DRM_ERROR("PSP load intf drv failed!\n");
21426ff34fd6SJohn Clements return ret;
21436ff34fd6SJohn Clements }
21446ff34fd6SJohn Clements }
21456ff34fd6SJohn Clements
21466ff34fd6SJohn Clements if ((is_psp_fw_valid(psp->dbg_drv)) &&
21476ff34fd6SJohn Clements (psp->funcs->bootloader_load_dbg_drv != NULL)) {
21486ff34fd6SJohn Clements ret = psp_bootloader_load_dbg_drv(psp);
21496ff34fd6SJohn Clements if (ret) {
21506ff34fd6SJohn Clements DRM_ERROR("PSP load dbg drv failed!\n");
21516ff34fd6SJohn Clements return ret;
21526ff34fd6SJohn Clements }
21536ff34fd6SJohn Clements }
21546ff34fd6SJohn Clements
2155c3db1b90SJohn Clements if ((is_psp_fw_valid(psp->ras_drv)) &&
2156c3db1b90SJohn Clements (psp->funcs->bootloader_load_ras_drv != NULL)) {
2157c3db1b90SJohn Clements ret = psp_bootloader_load_ras_drv(psp);
2158c3db1b90SJohn Clements if (ret) {
2159c3db1b90SJohn Clements DRM_ERROR("PSP load ras_drv failed!\n");
2160c3db1b90SJohn Clements return ret;
2161c3db1b90SJohn Clements }
2162c3db1b90SJohn Clements }
2163c3db1b90SJohn Clements
2164222e0a71SCandice Li if ((is_psp_fw_valid(psp->sos)) &&
2165222e0a71SCandice Li (psp->funcs->bootloader_load_sos != NULL)) {
21667a3d7bf6SEvan Quan ret = psp_bootloader_load_sos(psp);
21677a3d7bf6SEvan Quan if (ret) {
21687a3d7bf6SEvan Quan DRM_ERROR("PSP load sos failed!\n");
2169be70bbdaSHuang Rui return ret;
21707a3d7bf6SEvan Quan }
21717a3d7bf6SEvan Quan }
2172222e0a71SCandice Li }
21737a3d7bf6SEvan Quan
21747a3d7bf6SEvan Quan ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
21757a3d7bf6SEvan Quan if (ret) {
21767a3d7bf6SEvan Quan DRM_ERROR("PSP create ring failed!\n");
21777a3d7bf6SEvan Quan return ret;
21787a3d7bf6SEvan Quan }
21790e5ca0d1SHuang Rui
218085dfc1d6SJingwen Chen if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
218185dfc1d6SJingwen Chen goto skip_pin_bo;
218285dfc1d6SJingwen Chen
21835b03127dSLijo Lazar if (!psp_boottime_tmr(psp)) {
2184fba08a77SHawking Zhang ret = psp_tmr_init(psp);
2185fba08a77SHawking Zhang if (ret) {
2186fba08a77SHawking Zhang DRM_ERROR("PSP tmr init failed!\n");
2187fba08a77SHawking Zhang return ret;
2188fba08a77SHawking Zhang }
21895b03127dSLijo Lazar }
2190fba08a77SHawking Zhang
219185dfc1d6SJingwen Chen skip_pin_bo:
2192995da6ccSEvan Quan /*
219340e611bdSJohn Clements * For ASICs with DF Cstate management centralized
2194995da6ccSEvan Quan * to PMFW, TMR setup should be performed after PMFW
2195995da6ccSEvan Quan * loaded and before other non-psp firmware loaded.
2196995da6ccSEvan Quan */
219740e611bdSJohn Clements if (psp->pmfw_centralized_cstate_management) {
219840e611bdSJohn Clements ret = psp_load_smu_fw(psp);
219940e611bdSJohn Clements if (ret)
220040e611bdSJohn Clements return ret;
220140e611bdSJohn Clements }
220240e611bdSJohn Clements
2203be70bbdaSHuang Rui ret = psp_tmr_load(psp);
22047a3d7bf6SEvan Quan if (ret) {
22057a3d7bf6SEvan Quan DRM_ERROR("PSP load tmr failed!\n");
2206be70bbdaSHuang Rui return ret;
22077a3d7bf6SEvan Quan }
22080e5ca0d1SHuang Rui
2209be70bbdaSHuang Rui return 0;
2210be70bbdaSHuang Rui }
2211be70bbdaSHuang Rui
psp_get_fw_type(struct amdgpu_firmware_info * ucode,enum psp_gfx_fw_type * type)2212be4630d9SHawking Zhang static int psp_get_fw_type(struct amdgpu_firmware_info *ucode,
2213be4630d9SHawking Zhang enum psp_gfx_fw_type *type)
2214be4630d9SHawking Zhang {
2215be4630d9SHawking Zhang switch (ucode->ucode_id) {
2216c4381d0eSBokun Zhang case AMDGPU_UCODE_ID_CAP:
2217c4381d0eSBokun Zhang *type = GFX_FW_TYPE_CAP;
2218c4381d0eSBokun Zhang break;
2219be4630d9SHawking Zhang case AMDGPU_UCODE_ID_SDMA0:
2220be4630d9SHawking Zhang *type = GFX_FW_TYPE_SDMA0;
2221be4630d9SHawking Zhang break;
2222be4630d9SHawking Zhang case AMDGPU_UCODE_ID_SDMA1:
2223be4630d9SHawking Zhang *type = GFX_FW_TYPE_SDMA1;
2224be4630d9SHawking Zhang break;
2225b86f8d8bSJohn Clements case AMDGPU_UCODE_ID_SDMA2:
2226b86f8d8bSJohn Clements *type = GFX_FW_TYPE_SDMA2;
2227b86f8d8bSJohn Clements break;
2228b86f8d8bSJohn Clements case AMDGPU_UCODE_ID_SDMA3:
2229b86f8d8bSJohn Clements *type = GFX_FW_TYPE_SDMA3;
2230b86f8d8bSJohn Clements break;
2231b86f8d8bSJohn Clements case AMDGPU_UCODE_ID_SDMA4:
2232b86f8d8bSJohn Clements *type = GFX_FW_TYPE_SDMA4;
2233b86f8d8bSJohn Clements break;
2234b86f8d8bSJohn Clements case AMDGPU_UCODE_ID_SDMA5:
2235b86f8d8bSJohn Clements *type = GFX_FW_TYPE_SDMA5;
2236b86f8d8bSJohn Clements break;
2237b86f8d8bSJohn Clements case AMDGPU_UCODE_ID_SDMA6:
2238b86f8d8bSJohn Clements *type = GFX_FW_TYPE_SDMA6;
2239b86f8d8bSJohn Clements break;
2240b86f8d8bSJohn Clements case AMDGPU_UCODE_ID_SDMA7:
2241b86f8d8bSJohn Clements *type = GFX_FW_TYPE_SDMA7;
2242b86f8d8bSJohn Clements break;
224393fd978bSJack Xiao case AMDGPU_UCODE_ID_CP_MES:
224493fd978bSJack Xiao *type = GFX_FW_TYPE_CP_MES;
224593fd978bSJack Xiao break;
224693fd978bSJack Xiao case AMDGPU_UCODE_ID_CP_MES_DATA:
224793fd978bSJack Xiao *type = GFX_FW_TYPE_MES_STACK;
224893fd978bSJack Xiao break;
2249c1248e11SLikun Gao case AMDGPU_UCODE_ID_CP_MES1:
2250c1248e11SLikun Gao *type = GFX_FW_TYPE_CP_MES_KIQ;
2251c1248e11SLikun Gao break;
2252c1248e11SLikun Gao case AMDGPU_UCODE_ID_CP_MES1_DATA:
2253c1248e11SLikun Gao *type = GFX_FW_TYPE_MES_KIQ_STACK;
2254c1248e11SLikun Gao break;
2255be4630d9SHawking Zhang case AMDGPU_UCODE_ID_CP_CE:
2256be4630d9SHawking Zhang *type = GFX_FW_TYPE_CP_CE;
2257be4630d9SHawking Zhang break;
2258be4630d9SHawking Zhang case AMDGPU_UCODE_ID_CP_PFP:
2259be4630d9SHawking Zhang *type = GFX_FW_TYPE_CP_PFP;
2260be4630d9SHawking Zhang break;
2261be4630d9SHawking Zhang case AMDGPU_UCODE_ID_CP_ME:
2262be4630d9SHawking Zhang *type = GFX_FW_TYPE_CP_ME;
2263be4630d9SHawking Zhang break;
2264be4630d9SHawking Zhang case AMDGPU_UCODE_ID_CP_MEC1:
2265be4630d9SHawking Zhang *type = GFX_FW_TYPE_CP_MEC;
2266be4630d9SHawking Zhang break;
2267be4630d9SHawking Zhang case AMDGPU_UCODE_ID_CP_MEC1_JT:
2268be4630d9SHawking Zhang *type = GFX_FW_TYPE_CP_MEC_ME1;
2269be4630d9SHawking Zhang break;
2270be4630d9SHawking Zhang case AMDGPU_UCODE_ID_CP_MEC2:
2271be4630d9SHawking Zhang *type = GFX_FW_TYPE_CP_MEC;
2272be4630d9SHawking Zhang break;
2273be4630d9SHawking Zhang case AMDGPU_UCODE_ID_CP_MEC2_JT:
2274be4630d9SHawking Zhang *type = GFX_FW_TYPE_CP_MEC_ME2;
2275be4630d9SHawking Zhang break;
2276a0fe38b4SLikun Gao case AMDGPU_UCODE_ID_RLC_P:
2277a0fe38b4SLikun Gao *type = GFX_FW_TYPE_RLC_P;
2278a0fe38b4SLikun Gao break;
22798e41a56aSLikun Gao case AMDGPU_UCODE_ID_RLC_V:
22808e41a56aSLikun Gao *type = GFX_FW_TYPE_RLC_V;
22818e41a56aSLikun Gao break;
2282be4630d9SHawking Zhang case AMDGPU_UCODE_ID_RLC_G:
2283be4630d9SHawking Zhang *type = GFX_FW_TYPE_RLC_G;
2284be4630d9SHawking Zhang break;
2285be4630d9SHawking Zhang case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL:
2286be4630d9SHawking Zhang *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_CNTL;
2287be4630d9SHawking Zhang break;
2288be4630d9SHawking Zhang case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM:
2289be4630d9SHawking Zhang *type = GFX_FW_TYPE_RLC_RESTORE_LIST_GPM_MEM;
2290be4630d9SHawking Zhang break;
2291be4630d9SHawking Zhang case AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM:
2292be4630d9SHawking Zhang *type = GFX_FW_TYPE_RLC_RESTORE_LIST_SRM_MEM;
2293be4630d9SHawking Zhang break;
22945bab858eSLikun Gao case AMDGPU_UCODE_ID_RLC_IRAM:
22955bab858eSLikun Gao *type = GFX_FW_TYPE_RLC_IRAM;
22965bab858eSLikun Gao break;
22975bab858eSLikun Gao case AMDGPU_UCODE_ID_RLC_DRAM:
22985bab858eSLikun Gao *type = GFX_FW_TYPE_RLC_DRAM_BOOT;
22995bab858eSLikun Gao break;
23002207efddSChengming Gui case AMDGPU_UCODE_ID_GLOBAL_TAP_DELAYS:
23012207efddSChengming Gui *type = GFX_FW_TYPE_GLOBAL_TAP_DELAYS;
23022207efddSChengming Gui break;
23032207efddSChengming Gui case AMDGPU_UCODE_ID_SE0_TAP_DELAYS:
23042207efddSChengming Gui *type = GFX_FW_TYPE_SE0_TAP_DELAYS;
23052207efddSChengming Gui break;
23062207efddSChengming Gui case AMDGPU_UCODE_ID_SE1_TAP_DELAYS:
23072207efddSChengming Gui *type = GFX_FW_TYPE_SE1_TAP_DELAYS;
23082207efddSChengming Gui break;
23092207efddSChengming Gui case AMDGPU_UCODE_ID_SE2_TAP_DELAYS:
23102207efddSChengming Gui *type = GFX_FW_TYPE_SE2_TAP_DELAYS;
23112207efddSChengming Gui break;
23122207efddSChengming Gui case AMDGPU_UCODE_ID_SE3_TAP_DELAYS:
23132207efddSChengming Gui *type = GFX_FW_TYPE_SE3_TAP_DELAYS;
23142207efddSChengming Gui break;
2315be4630d9SHawking Zhang case AMDGPU_UCODE_ID_SMC:
2316be4630d9SHawking Zhang *type = GFX_FW_TYPE_SMU;
2317be4630d9SHawking Zhang break;
2318b37c41f2SEvan Quan case AMDGPU_UCODE_ID_PPTABLE:
2319b37c41f2SEvan Quan *type = GFX_FW_TYPE_PPTABLE;
2320b37c41f2SEvan Quan break;
2321be4630d9SHawking Zhang case AMDGPU_UCODE_ID_UVD:
2322be4630d9SHawking Zhang *type = GFX_FW_TYPE_UVD;
2323be4630d9SHawking Zhang break;
2324be4630d9SHawking Zhang case AMDGPU_UCODE_ID_UVD1:
2325be4630d9SHawking Zhang *type = GFX_FW_TYPE_UVD1;
2326be4630d9SHawking Zhang break;
2327be4630d9SHawking Zhang case AMDGPU_UCODE_ID_VCE:
2328be4630d9SHawking Zhang *type = GFX_FW_TYPE_VCE;
2329be4630d9SHawking Zhang break;
2330be4630d9SHawking Zhang case AMDGPU_UCODE_ID_VCN:
2331be4630d9SHawking Zhang *type = GFX_FW_TYPE_VCN;
2332be4630d9SHawking Zhang break;
2333d83c7a07SJane Jian case AMDGPU_UCODE_ID_VCN1:
2334d83c7a07SJane Jian *type = GFX_FW_TYPE_VCN1;
2335d83c7a07SJane Jian break;
2336be4630d9SHawking Zhang case AMDGPU_UCODE_ID_DMCU_ERAM:
2337be4630d9SHawking Zhang *type = GFX_FW_TYPE_DMCU_ERAM;
2338be4630d9SHawking Zhang break;
2339be4630d9SHawking Zhang case AMDGPU_UCODE_ID_DMCU_INTV:
2340be4630d9SHawking Zhang *type = GFX_FW_TYPE_DMCU_ISR;
2341be4630d9SHawking Zhang break;
2342c76ff09bSJack Xiao case AMDGPU_UCODE_ID_VCN0_RAM:
2343c76ff09bSJack Xiao *type = GFX_FW_TYPE_VCN0_RAM;
2344c76ff09bSJack Xiao break;
2345c76ff09bSJack Xiao case AMDGPU_UCODE_ID_VCN1_RAM:
2346c76ff09bSJack Xiao *type = GFX_FW_TYPE_VCN1_RAM;
2347c76ff09bSJack Xiao break;
23482bd2a27fSNicholas Kazlauskas case AMDGPU_UCODE_ID_DMCUB:
23492bd2a27fSNicholas Kazlauskas *type = GFX_FW_TYPE_DMUB;
23502bd2a27fSNicholas Kazlauskas break;
23516777c8cfSLikun Gao case AMDGPU_UCODE_ID_SDMA_UCODE_TH0:
23526777c8cfSLikun Gao *type = GFX_FW_TYPE_SDMA_UCODE_TH0;
23536777c8cfSLikun Gao break;
23546777c8cfSLikun Gao case AMDGPU_UCODE_ID_SDMA_UCODE_TH1:
23556777c8cfSLikun Gao *type = GFX_FW_TYPE_SDMA_UCODE_TH1;
23566777c8cfSLikun Gao break;
2357a32fa029SLikun Gao case AMDGPU_UCODE_ID_IMU_I:
2358a32fa029SLikun Gao *type = GFX_FW_TYPE_IMU_I;
2359a32fa029SLikun Gao break;
2360a32fa029SLikun Gao case AMDGPU_UCODE_ID_IMU_D:
2361a32fa029SLikun Gao *type = GFX_FW_TYPE_IMU_D;
2362a32fa029SLikun Gao break;
2363be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_PFP:
2364be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_PFP;
2365be3a3409SLikun Gao break;
2366be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_ME:
2367be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_ME;
2368be3a3409SLikun Gao break;
2369be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_MEC:
2370be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_MEC;
2371be3a3409SLikun Gao break;
2372be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK:
2373be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_PFP_P0_STACK;
2374be3a3409SLikun Gao break;
2375be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK:
2376be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_PFP_P1_STACK;
2377be3a3409SLikun Gao break;
2378be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK:
2379be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_ME_P0_STACK;
2380be3a3409SLikun Gao break;
2381be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK:
2382be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_ME_P1_STACK;
2383be3a3409SLikun Gao break;
2384be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK:
2385be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_MEC_P0_STACK;
2386be3a3409SLikun Gao break;
2387be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK:
2388be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_MEC_P1_STACK;
2389be3a3409SLikun Gao break;
2390be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK:
2391be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_MEC_P2_STACK;
2392be3a3409SLikun Gao break;
2393be3a3409SLikun Gao case AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK:
2394be3a3409SLikun Gao *type = GFX_FW_TYPE_RS64_MEC_P3_STACK;
2395be3a3409SLikun Gao break;
2396be4630d9SHawking Zhang case AMDGPU_UCODE_ID_MAXIMUM:
2397be4630d9SHawking Zhang default:
2398be4630d9SHawking Zhang return -EINVAL;
2399be4630d9SHawking Zhang }
2400be4630d9SHawking Zhang
2401be4630d9SHawking Zhang return 0;
2402be4630d9SHawking Zhang }
2403be4630d9SHawking Zhang
psp_print_fw_hdr(struct psp_context * psp,struct amdgpu_firmware_info * ucode)2404c5fb9126SXiaojie Yuan static void psp_print_fw_hdr(struct psp_context *psp,
2405c5fb9126SXiaojie Yuan struct amdgpu_firmware_info *ucode)
2406c5fb9126SXiaojie Yuan {
2407c5fb9126SXiaojie Yuan struct amdgpu_device *adev = psp->adev;
2408bfa603aaSXiaojie Yuan struct common_firmware_header *hdr;
2409c5fb9126SXiaojie Yuan
2410c5fb9126SXiaojie Yuan switch (ucode->ucode_id) {
2411c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SDMA0:
2412c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SDMA1:
2413c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SDMA2:
2414c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SDMA3:
2415c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SDMA4:
2416c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SDMA5:
2417c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SDMA6:
2418c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SDMA7:
2419bfa603aaSXiaojie Yuan hdr = (struct common_firmware_header *)
2420bfa603aaSXiaojie Yuan adev->sdma.instance[ucode->ucode_id - AMDGPU_UCODE_ID_SDMA0].fw->data;
2421bfa603aaSXiaojie Yuan amdgpu_ucode_print_sdma_hdr(hdr);
2422c5fb9126SXiaojie Yuan break;
2423c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_CP_CE:
2424bfa603aaSXiaojie Yuan hdr = (struct common_firmware_header *)adev->gfx.ce_fw->data;
2425bfa603aaSXiaojie Yuan amdgpu_ucode_print_gfx_hdr(hdr);
2426c5fb9126SXiaojie Yuan break;
2427c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_CP_PFP:
2428bfa603aaSXiaojie Yuan hdr = (struct common_firmware_header *)adev->gfx.pfp_fw->data;
2429bfa603aaSXiaojie Yuan amdgpu_ucode_print_gfx_hdr(hdr);
2430c5fb9126SXiaojie Yuan break;
2431c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_CP_ME:
2432bfa603aaSXiaojie Yuan hdr = (struct common_firmware_header *)adev->gfx.me_fw->data;
2433bfa603aaSXiaojie Yuan amdgpu_ucode_print_gfx_hdr(hdr);
2434c5fb9126SXiaojie Yuan break;
2435c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_CP_MEC1:
2436bfa603aaSXiaojie Yuan hdr = (struct common_firmware_header *)adev->gfx.mec_fw->data;
2437bfa603aaSXiaojie Yuan amdgpu_ucode_print_gfx_hdr(hdr);
2438c5fb9126SXiaojie Yuan break;
2439c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_RLC_G:
2440bfa603aaSXiaojie Yuan hdr = (struct common_firmware_header *)adev->gfx.rlc_fw->data;
2441bfa603aaSXiaojie Yuan amdgpu_ucode_print_rlc_hdr(hdr);
2442c5fb9126SXiaojie Yuan break;
2443c5fb9126SXiaojie Yuan case AMDGPU_UCODE_ID_SMC:
2444bfa603aaSXiaojie Yuan hdr = (struct common_firmware_header *)adev->pm.fw->data;
2445bfa603aaSXiaojie Yuan amdgpu_ucode_print_smc_hdr(hdr);
2446c5fb9126SXiaojie Yuan break;
2447c5fb9126SXiaojie Yuan default:
2448c5fb9126SXiaojie Yuan break;
2449c5fb9126SXiaojie Yuan }
2450c5fb9126SXiaojie Yuan }
2451c5fb9126SXiaojie Yuan
psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info * ucode,struct psp_gfx_cmd_resp * cmd)2452be4630d9SHawking Zhang static int psp_prep_load_ip_fw_cmd_buf(struct amdgpu_firmware_info *ucode,
2453be4630d9SHawking Zhang struct psp_gfx_cmd_resp *cmd)
2454be4630d9SHawking Zhang {
2455be4630d9SHawking Zhang int ret;
2456be4630d9SHawking Zhang uint64_t fw_mem_mc_addr = ucode->mc_addr;
2457be4630d9SHawking Zhang
2458be4630d9SHawking Zhang cmd->cmd_id = GFX_CMD_ID_LOAD_IP_FW;
2459be4630d9SHawking Zhang cmd->cmd.cmd_load_ip_fw.fw_phy_addr_lo = lower_32_bits(fw_mem_mc_addr);
2460be4630d9SHawking Zhang cmd->cmd.cmd_load_ip_fw.fw_phy_addr_hi = upper_32_bits(fw_mem_mc_addr);
2461be4630d9SHawking Zhang cmd->cmd.cmd_load_ip_fw.fw_size = ucode->ucode_size;
2462be4630d9SHawking Zhang
2463be4630d9SHawking Zhang ret = psp_get_fw_type(ucode, &cmd->cmd.cmd_load_ip_fw.fw_type);
2464be4630d9SHawking Zhang if (ret)
2465be4630d9SHawking Zhang DRM_ERROR("Unknown firmware type\n");
2466be4630d9SHawking Zhang
2467be4630d9SHawking Zhang return ret;
2468be4630d9SHawking Zhang }
2469be4630d9SHawking Zhang
psp_execute_ip_fw_load(struct psp_context * psp,struct amdgpu_firmware_info * ucode)247045b51acbSLang Yu int psp_execute_ip_fw_load(struct psp_context *psp,
247113169562SHuang Rui struct amdgpu_firmware_info *ucode)
247213169562SHuang Rui {
247313169562SHuang Rui int ret = 0;
24744b296527SJohn Clements struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
247513169562SHuang Rui
24764b296527SJohn Clements ret = psp_prep_load_ip_fw_cmd_buf(ucode, cmd);
24774b296527SJohn Clements if (!ret) {
24784b296527SJohn Clements ret = psp_cmd_submit_buf(psp, ucode, cmd,
247913169562SHuang Rui psp->fence_buf_mc_addr);
24804b296527SJohn Clements }
24814b296527SJohn Clements
24824b296527SJohn Clements release_psp_cmd_buf(psp);
248313169562SHuang Rui
248413169562SHuang Rui return ret;
248513169562SHuang Rui }
248613169562SHuang Rui
psp_load_smu_fw(struct psp_context * psp)248740e611bdSJohn Clements static int psp_load_smu_fw(struct psp_context *psp)
2488be70bbdaSHuang Rui {
248940e611bdSJohn Clements int ret;
24907f70443fSJohn Clements struct amdgpu_device *adev = psp->adev;
249140e611bdSJohn Clements struct amdgpu_firmware_info *ucode =
24927f70443fSJohn Clements &adev->firmware.ucode[AMDGPU_UCODE_ID_SMC];
24936457205cSCandice Li struct amdgpu_ras *ras = psp->ras_context.ras;
24940e5ca0d1SHuang Rui
2495f746556aSGuchun Chen /*
2496f746556aSGuchun Chen * Skip SMU FW reloading in case of using BACO for runpm only,
2497f746556aSGuchun Chen * as SMU is always alive.
2498f746556aSGuchun Chen */
2499f746556aSGuchun Chen if (adev->in_runpm && (adev->pm.rpm_mode == AMDGPU_RUNPM_BACO))
2500f746556aSGuchun Chen return 0;
2501f746556aSGuchun Chen
250240e611bdSJohn Clements if (!ucode->fw || amdgpu_sriov_vf(psp->adev))
250340e611bdSJohn Clements return 0;
250413169562SHuang Rui
25051689fca0SEvan Quan if ((amdgpu_in_reset(adev) &&
25068ab0d6f0SLuben Tuikov ras && adev->ras_enabled &&
25071d789535SAlex Deucher (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 4) ||
25081d789535SAlex Deucher adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 2)))) {
25097f70443fSJohn Clements ret = amdgpu_dpm_set_mp1_state(adev, PP_MP1_STATE_UNLOAD);
2510f14c8c3eSSrinivasan Shanmugam if (ret)
25117f70443fSJohn Clements DRM_WARN("Failed to set MP1 state prepare for reload\n");
25127f70443fSJohn Clements }
25137f70443fSJohn Clements
251445b51acbSLang Yu ret = psp_execute_ip_fw_load(psp, ucode);
251540e611bdSJohn Clements
251613169562SHuang Rui if (ret)
251740e611bdSJohn Clements DRM_ERROR("PSP load smu failed!\n");
251840e611bdSJohn Clements
251913169562SHuang Rui return ret;
252013169562SHuang Rui }
252113169562SHuang Rui
fw_load_skip_check(struct psp_context * psp,struct amdgpu_firmware_info * ucode)252240e611bdSJohn Clements static bool fw_load_skip_check(struct psp_context *psp,
252340e611bdSJohn Clements struct amdgpu_firmware_info *ucode)
252440e611bdSJohn Clements {
252568fb37bcSChengming Gui if (!ucode->fw || !ucode->ucode_size)
252640e611bdSJohn Clements return true;
25270e5ca0d1SHuang Rui
25280e5ca0d1SHuang Rui if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
2529995da6ccSEvan Quan (psp_smu_reload_quirk(psp) ||
2530995da6ccSEvan Quan psp->autoload_supported ||
2531995da6ccSEvan Quan psp->pmfw_centralized_cstate_management))
253240e611bdSJohn Clements return true;
2533b86f8d8bSJohn Clements
253440e611bdSJohn Clements if (amdgpu_sriov_vf(psp->adev) &&
2535d9d86d08SHorace Chen amdgpu_virt_fw_load_skip_check(psp->adev, ucode->ucode_id))
253640e611bdSJohn Clements return true;
2537b86f8d8bSJohn Clements
2538119eb6dbSHawking Zhang if (psp->autoload_supported &&
2539119eb6dbSHawking Zhang (ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC1_JT ||
2540119eb6dbSHawking Zhang ucode->ucode_id == AMDGPU_UCODE_ID_CP_MEC2_JT))
2541119eb6dbSHawking Zhang /* skip mec JT when autoload is enabled */
254240e611bdSJohn Clements return true;
254340e611bdSJohn Clements
254440e611bdSJohn Clements return false;
254540e611bdSJohn Clements }
254640e611bdSJohn Clements
psp_load_fw_list(struct psp_context * psp,struct amdgpu_firmware_info ** ucode_list,int ucode_count)2547a2052839SLijo Lazar int psp_load_fw_list(struct psp_context *psp,
2548a2052839SLijo Lazar struct amdgpu_firmware_info **ucode_list, int ucode_count)
2549a2052839SLijo Lazar {
2550a2052839SLijo Lazar int ret = 0, i;
2551a2052839SLijo Lazar struct amdgpu_firmware_info *ucode;
2552a2052839SLijo Lazar
2553a2052839SLijo Lazar for (i = 0; i < ucode_count; ++i) {
2554a2052839SLijo Lazar ucode = ucode_list[i];
2555a2052839SLijo Lazar psp_print_fw_hdr(psp, ucode);
255645b51acbSLang Yu ret = psp_execute_ip_fw_load(psp, ucode);
2557a2052839SLijo Lazar if (ret)
2558a2052839SLijo Lazar return ret;
2559a2052839SLijo Lazar }
2560a2052839SLijo Lazar return ret;
2561a2052839SLijo Lazar }
2562a2052839SLijo Lazar
psp_load_non_psp_fw(struct psp_context * psp)2563cd5955f4SOak Zeng static int psp_load_non_psp_fw(struct psp_context *psp)
256440e611bdSJohn Clements {
256540e611bdSJohn Clements int i, ret;
256640e611bdSJohn Clements struct amdgpu_firmware_info *ucode;
256740e611bdSJohn Clements struct amdgpu_device *adev = psp->adev;
256840e611bdSJohn Clements
256940e611bdSJohn Clements if (psp->autoload_supported &&
257040e611bdSJohn Clements !psp->pmfw_centralized_cstate_management) {
257140e611bdSJohn Clements ret = psp_load_smu_fw(psp);
257240e611bdSJohn Clements if (ret)
257340e611bdSJohn Clements return ret;
257440e611bdSJohn Clements }
257540e611bdSJohn Clements
257640e611bdSJohn Clements for (i = 0; i < adev->firmware.max_ucodes; i++) {
257740e611bdSJohn Clements ucode = &adev->firmware.ucode[i];
257840e611bdSJohn Clements
257940e611bdSJohn Clements if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
258040e611bdSJohn Clements !fw_load_skip_check(psp, ucode)) {
258140e611bdSJohn Clements ret = psp_load_smu_fw(psp);
258240e611bdSJohn Clements if (ret)
258340e611bdSJohn Clements return ret;
258440e611bdSJohn Clements continue;
258540e611bdSJohn Clements }
258640e611bdSJohn Clements
258740e611bdSJohn Clements if (fw_load_skip_check(psp, ucode))
2588119eb6dbSHawking Zhang continue;
25890e5ca0d1SHuang Rui
2590738c822cSLikun Gao if (psp->autoload_supported &&
25911d789535SAlex Deucher (adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 7) ||
25921d789535SAlex Deucher adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 11) ||
25931d789535SAlex Deucher adev->ip_versions[MP0_HWIP][0] == IP_VERSION(11, 0, 12)) &&
2594738c822cSLikun Gao (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1 ||
2595738c822cSLikun Gao ucode->ucode_id == AMDGPU_UCODE_ID_SDMA2 ||
2596738c822cSLikun Gao ucode->ucode_id == AMDGPU_UCODE_ID_SDMA3))
2597738c822cSLikun Gao /* PSP only receive one SDMA fw for sienna_cichlid,
25982d5c0415SPraful Swarnakar * as all four sdma fw are same
25992d5c0415SPraful Swarnakar */
2600738c822cSLikun Gao continue;
2601738c822cSLikun Gao
2602c5fb9126SXiaojie Yuan psp_print_fw_hdr(psp, ucode);
2603c5fb9126SXiaojie Yuan
260445b51acbSLang Yu ret = psp_execute_ip_fw_load(psp, ucode);
26050e5ca0d1SHuang Rui if (ret)
2606be70bbdaSHuang Rui return ret;
26070e5ca0d1SHuang Rui
26081a5b4ccaSHawking Zhang /* Start rlc autoload after psp recieved all the gfx firmware */
26096de40f02SMonk Liu if (psp->autoload_supported && ucode->ucode_id == (amdgpu_sriov_vf(adev) ?
2610f8bd7321SHorace Chen adev->virt.autoload_ucode_id : AMDGPU_UCODE_ID_RLC_G)) {
2611999a69e2SHawking Zhang ret = psp_rlc_autoload_start(psp);
26121a5b4ccaSHawking Zhang if (ret) {
26131a5b4ccaSHawking Zhang DRM_ERROR("Failed to start rlc autoload\n");
26141a5b4ccaSHawking Zhang return ret;
26151a5b4ccaSHawking Zhang }
26161a5b4ccaSHawking Zhang }
26170e5ca0d1SHuang Rui }
26180e5ca0d1SHuang Rui
2619be70bbdaSHuang Rui return 0;
2620be70bbdaSHuang Rui }
2621be70bbdaSHuang Rui
psp_load_fw(struct amdgpu_device * adev)26220e5ca0d1SHuang Rui static int psp_load_fw(struct amdgpu_device *adev)
26230e5ca0d1SHuang Rui {
26240e5ca0d1SHuang Rui int ret;
26250e5ca0d1SHuang Rui struct psp_context *psp = &adev->psp;
26260e5ca0d1SHuang Rui
262753b3f8f4SDennis Li if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2628b95b5391SAlex Deucher /* should not destroy ring, only stop */
2629b95b5391SAlex Deucher psp_ring_stop(psp, PSP_RING_TYPE__KM);
2630e1944debSZhigang Luo } else {
26310e5ca0d1SHuang Rui memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
26320e5ca0d1SHuang Rui
2633be70bbdaSHuang Rui ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
26347a3d7bf6SEvan Quan if (ret) {
26357a3d7bf6SEvan Quan DRM_ERROR("PSP ring init failed!\n");
263637945a3aSEvan Quan goto failed;
26377a3d7bf6SEvan Quan }
2638b95b5391SAlex Deucher }
26390e5ca0d1SHuang Rui
2640be70bbdaSHuang Rui ret = psp_hw_start(psp);
26410e5ca0d1SHuang Rui if (ret)
264237945a3aSEvan Quan goto failed;
26430e5ca0d1SHuang Rui
2644cd5955f4SOak Zeng ret = psp_load_non_psp_fw(psp);
26450e5ca0d1SHuang Rui if (ret)
2646e2c34219SAlice Wong goto failed1;
26470e5ca0d1SHuang Rui
26483f83f17bSCandice Li ret = psp_asd_initialize(psp);
26497091b60cSHawking Zhang if (ret) {
26507091b60cSHawking Zhang DRM_ERROR("PSP load asd failed!\n");
2651e2c34219SAlice Wong goto failed1;
26527091b60cSHawking Zhang }
26537091b60cSHawking Zhang
26540d2c1855SJohn Clements ret = psp_rl_load(adev);
26550d2c1855SJohn Clements if (ret) {
26560d2c1855SJohn Clements DRM_ERROR("PSP load RL failed!\n");
2657e2c34219SAlice Wong goto failed1;
26580d2c1855SJohn Clements }
26590d2c1855SJohn Clements
2660dd26e018SZhigang Luo if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev)) {
2661dd26e018SZhigang Luo if (adev->gmc.xgmi.num_physical_nodes > 1) {
2662dd26e018SZhigang Luo ret = psp_xgmi_initialize(psp, false, true);
2663dd26e018SZhigang Luo /* Warning the XGMI seesion initialize failure
2664dd26e018SZhigang Luo * Instead of stop driver initialization
2665dd26e018SZhigang Luo */
2666dd26e018SZhigang Luo if (ret)
2667dd26e018SZhigang Luo dev_err(psp->adev->dev,
2668dd26e018SZhigang Luo "XGMI: Failed to initialize XGMI session\n");
2669dd26e018SZhigang Luo }
2670dd26e018SZhigang Luo }
2671dd26e018SZhigang Luo
26726457205cSCandice Li if (psp->ta_fw) {
26737091b60cSHawking Zhang ret = psp_ras_initialize(psp);
26747091b60cSHawking Zhang if (ret)
26757091b60cSHawking Zhang dev_err(psp->adev->dev,
26767091b60cSHawking Zhang "RAS: Failed to initialize RAS\n");
26777091b60cSHawking Zhang
26787091b60cSHawking Zhang ret = psp_hdcp_initialize(psp);
26797091b60cSHawking Zhang if (ret)
26807091b60cSHawking Zhang dev_err(psp->adev->dev,
26817091b60cSHawking Zhang "HDCP: Failed to initialize HDCP\n");
26827091b60cSHawking Zhang
26837091b60cSHawking Zhang ret = psp_dtm_initialize(psp);
26847091b60cSHawking Zhang if (ret)
26857091b60cSHawking Zhang dev_err(psp->adev->dev,
26867091b60cSHawking Zhang "DTM: Failed to initialize DTM\n");
26878602692bSWenhui Sheng
26888602692bSWenhui Sheng ret = psp_rap_initialize(psp);
26898602692bSWenhui Sheng if (ret)
26908602692bSWenhui Sheng dev_err(psp->adev->dev,
26918602692bSWenhui Sheng "RAP: Failed to initialize RAP\n");
2692ecaafb7bSJinzhou Su
2693ecaafb7bSJinzhou Su ret = psp_securedisplay_initialize(psp);
2694ecaafb7bSJinzhou Su if (ret)
2695ecaafb7bSJinzhou Su dev_err(psp->adev->dev,
2696ecaafb7bSJinzhou Su "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
26977091b60cSHawking Zhang }
26987091b60cSHawking Zhang
26990e5ca0d1SHuang Rui return 0;
27000e5ca0d1SHuang Rui
2701e2c34219SAlice Wong failed1:
2702e2c34219SAlice Wong psp_free_shared_bufs(psp);
27030e5ca0d1SHuang Rui failed:
270437945a3aSEvan Quan /*
270537945a3aSEvan Quan * all cleanup jobs (xgmi terminate, ras terminate,
270637945a3aSEvan Quan * ring destroy, cmd/fence/fw buffers destory,
270737945a3aSEvan Quan * psp->cmd destory) are delayed to psp_hw_fini
270837945a3aSEvan Quan */
2709e2c34219SAlice Wong psp_ring_destroy(psp, PSP_RING_TYPE__KM);
27100e5ca0d1SHuang Rui return ret;
27110e5ca0d1SHuang Rui }
27120e5ca0d1SHuang Rui
psp_hw_init(void * handle)27130e5ca0d1SHuang Rui static int psp_hw_init(void *handle)
27140e5ca0d1SHuang Rui {
27150e5ca0d1SHuang Rui int ret;
27160e5ca0d1SHuang Rui struct amdgpu_device *adev = (struct amdgpu_device *)handle;
27170e5ca0d1SHuang Rui
27180e5ca0d1SHuang Rui mutex_lock(&adev->firmware.mutex);
27196e13bdf6SRex Zhu /*
27206e13bdf6SRex Zhu * This sequence is just used on hw_init only once, no need on
27216e13bdf6SRex Zhu * resume.
27226e13bdf6SRex Zhu */
27236e13bdf6SRex Zhu ret = amdgpu_ucode_init_bo(adev);
27246e13bdf6SRex Zhu if (ret)
27256e13bdf6SRex Zhu goto failed;
27260e5ca0d1SHuang Rui
27270e5ca0d1SHuang Rui ret = psp_load_fw(adev);
27280e5ca0d1SHuang Rui if (ret) {
27290e5ca0d1SHuang Rui DRM_ERROR("PSP firmware loading failed\n");
27300e5ca0d1SHuang Rui goto failed;
27310e5ca0d1SHuang Rui }
27320e5ca0d1SHuang Rui
27330e5ca0d1SHuang Rui mutex_unlock(&adev->firmware.mutex);
273403597b47SKevin Wang return 0;
27350e5ca0d1SHuang Rui
27360e5ca0d1SHuang Rui failed:
27370e5ca0d1SHuang Rui adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
27380e5ca0d1SHuang Rui mutex_unlock(&adev->firmware.mutex);
27390e5ca0d1SHuang Rui return -EINVAL;
27400e5ca0d1SHuang Rui }
27410e5ca0d1SHuang Rui
psp_hw_fini(void * handle)27420e5ca0d1SHuang Rui static int psp_hw_fini(void *handle)
27430e5ca0d1SHuang Rui {
27440e5ca0d1SHuang Rui struct amdgpu_device *adev = (struct amdgpu_device *)handle;
27450e5ca0d1SHuang Rui struct psp_context *psp = &adev->psp;
27460e5ca0d1SHuang Rui
27476457205cSCandice Li if (psp->ta_fw) {
27485e5d3154Sxinhui pan psp_ras_terminate(psp);
2749ecaafb7bSJinzhou Su psp_securedisplay_terminate(psp);
27508602692bSWenhui Sheng psp_rap_terminate(psp);
2751143f2305SBhawanpreet Lakha psp_dtm_terminate(psp);
2752ed19a9a2SBhawanpreet Lakha psp_hdcp_terminate(psp);
2753d8adafc7SYiPeng Chai
2754d8adafc7SYiPeng Chai if (adev->gmc.xgmi.num_physical_nodes > 1)
2755d8adafc7SYiPeng Chai psp_xgmi_terminate(psp);
2756ed19a9a2SBhawanpreet Lakha }
27575e5d3154Sxinhui pan
275825c94b33SCandice Li psp_asd_terminate(psp);
275990937420SHuang Rui psp_tmr_terminate(psp);
2760da40bf8fSAlex Deucher
2761e3c5e982STrigger Huang psp_ring_destroy(psp, PSP_RING_TYPE__KM);
2762e3c5e982STrigger Huang
27630e5ca0d1SHuang Rui return 0;
27640e5ca0d1SHuang Rui }
27650e5ca0d1SHuang Rui
psp_suspend(void * handle)27660e5ca0d1SHuang Rui static int psp_suspend(void *handle)
27670e5ca0d1SHuang Rui {
2768da40bf8fSAlex Deucher int ret = 0;
2769bcd6eab8SEvan Quan struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2770bcd6eab8SEvan Quan struct psp_context *psp = &adev->psp;
2771bcd6eab8SEvan Quan
27723e2e2ab5SHawking Zhang if (adev->gmc.xgmi.num_physical_nodes > 1 &&
2773ce97f37bSCandice Li psp->xgmi_context.context.initialized) {
27743e2e2ab5SHawking Zhang ret = psp_xgmi_terminate(psp);
27753e2e2ab5SHawking Zhang if (ret) {
27763e2e2ab5SHawking Zhang DRM_ERROR("Failed to terminate xgmi ta\n");
2777da40bf8fSAlex Deucher goto out;
27783e2e2ab5SHawking Zhang }
27793e2e2ab5SHawking Zhang }
27803e2e2ab5SHawking Zhang
27816457205cSCandice Li if (psp->ta_fw) {
27825e5d3154Sxinhui pan ret = psp_ras_terminate(psp);
27835e5d3154Sxinhui pan if (ret) {
27845e5d3154Sxinhui pan DRM_ERROR("Failed to terminate ras ta\n");
2785da40bf8fSAlex Deucher goto out;
27865e5d3154Sxinhui pan }
2787ed19a9a2SBhawanpreet Lakha ret = psp_hdcp_terminate(psp);
2788ed19a9a2SBhawanpreet Lakha if (ret) {
2789ed19a9a2SBhawanpreet Lakha DRM_ERROR("Failed to terminate hdcp ta\n");
2790da40bf8fSAlex Deucher goto out;
2791ed19a9a2SBhawanpreet Lakha }
2792143f2305SBhawanpreet Lakha ret = psp_dtm_terminate(psp);
2793143f2305SBhawanpreet Lakha if (ret) {
2794143f2305SBhawanpreet Lakha DRM_ERROR("Failed to terminate dtm ta\n");
2795da40bf8fSAlex Deucher goto out;
2796143f2305SBhawanpreet Lakha }
27978602692bSWenhui Sheng ret = psp_rap_terminate(psp);
27988602692bSWenhui Sheng if (ret) {
27998602692bSWenhui Sheng DRM_ERROR("Failed to terminate rap ta\n");
2800da40bf8fSAlex Deucher goto out;
28018602692bSWenhui Sheng }
2802ecaafb7bSJinzhou Su ret = psp_securedisplay_terminate(psp);
2803ecaafb7bSJinzhou Su if (ret) {
2804ecaafb7bSJinzhou Su DRM_ERROR("Failed to terminate securedisplay ta\n");
2805da40bf8fSAlex Deucher goto out;
2806ecaafb7bSJinzhou Su }
280754eb4ed6Sxinhui pan }
28085e5d3154Sxinhui pan
280925c94b33SCandice Li ret = psp_asd_terminate(psp);
2810429f3d24SHuang Rui if (ret) {
281125c94b33SCandice Li DRM_ERROR("Failed to terminate asd\n");
2812da40bf8fSAlex Deucher goto out;
2813429f3d24SHuang Rui }
2814429f3d24SHuang Rui
281590937420SHuang Rui ret = psp_tmr_terminate(psp);
281690937420SHuang Rui if (ret) {
28174afaa61dSColin Ian King DRM_ERROR("Failed to terminate tmr\n");
2818da40bf8fSAlex Deucher goto out;
281990937420SHuang Rui }
282090937420SHuang Rui
2821bcd6eab8SEvan Quan ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
2822f14c8c3eSSrinivasan Shanmugam if (ret)
2823bcd6eab8SEvan Quan DRM_ERROR("PSP ring stop failed\n");
2824bcd6eab8SEvan Quan
2825da40bf8fSAlex Deucher out:
2826da40bf8fSAlex Deucher return ret;
28270e5ca0d1SHuang Rui }
28280e5ca0d1SHuang Rui
psp_resume(void * handle)28290e5ca0d1SHuang Rui static int psp_resume(void *handle)
28300e5ca0d1SHuang Rui {
28310e5ca0d1SHuang Rui int ret;
28320e5ca0d1SHuang Rui struct amdgpu_device *adev = (struct amdgpu_device *)handle;
283393ea9b9fSHuang Rui struct psp_context *psp = &adev->psp;
28340e5ca0d1SHuang Rui
283593ea9b9fSHuang Rui DRM_INFO("PSP is resuming...\n");
283693ea9b9fSHuang Rui
28373a07101bSHawking Zhang if (psp->mem_train_ctx.enable_mem_training) {
28380586a059STianci.Yin ret = psp_mem_training(psp, PSP_MEM_TRAIN_RESUME);
28390586a059STianci.Yin if (ret) {
28400586a059STianci.Yin DRM_ERROR("Failed to process memory training!\n");
28410586a059STianci.Yin return ret;
28420586a059STianci.Yin }
28433a07101bSHawking Zhang }
28440586a059STianci.Yin
28450e5ca0d1SHuang Rui mutex_lock(&adev->firmware.mutex);
28460e5ca0d1SHuang Rui
284793ea9b9fSHuang Rui ret = psp_hw_start(psp);
28480e5ca0d1SHuang Rui if (ret)
284993ea9b9fSHuang Rui goto failed;
285093ea9b9fSHuang Rui
2851cd5955f4SOak Zeng ret = psp_load_non_psp_fw(psp);
285293ea9b9fSHuang Rui if (ret)
285393ea9b9fSHuang Rui goto failed;
28540e5ca0d1SHuang Rui
28553f83f17bSCandice Li ret = psp_asd_initialize(psp);
28560d6f39bbSHawking Zhang if (ret) {
28570d6f39bbSHawking Zhang DRM_ERROR("PSP load asd failed!\n");
28580d6f39bbSHawking Zhang goto failed;
28590d6f39bbSHawking Zhang }
28600d6f39bbSHawking Zhang
2861c72942c1STao Zhou ret = psp_rl_load(adev);
2862c72942c1STao Zhou if (ret) {
2863c72942c1STao Zhou dev_err(adev->dev, "PSP load RL failed!\n");
2864c72942c1STao Zhou goto failed;
2865c72942c1STao Zhou }
2866c72942c1STao Zhou
28670d6f39bbSHawking Zhang if (adev->gmc.xgmi.num_physical_nodes > 1) {
286844357a1bSJonathan Kim ret = psp_xgmi_initialize(psp, false, true);
28690d6f39bbSHawking Zhang /* Warning the XGMI seesion initialize failure
28700d6f39bbSHawking Zhang * Instead of stop driver initialization
28710d6f39bbSHawking Zhang */
28720d6f39bbSHawking Zhang if (ret)
28730d6f39bbSHawking Zhang dev_err(psp->adev->dev,
28740d6f39bbSHawking Zhang "XGMI: Failed to initialize XGMI session\n");
28750d6f39bbSHawking Zhang }
28760d6f39bbSHawking Zhang
28776457205cSCandice Li if (psp->ta_fw) {
28780d6f39bbSHawking Zhang ret = psp_ras_initialize(psp);
28790d6f39bbSHawking Zhang if (ret)
28800d6f39bbSHawking Zhang dev_err(psp->adev->dev,
28810d6f39bbSHawking Zhang "RAS: Failed to initialize RAS\n");
28820d6f39bbSHawking Zhang
28830d6f39bbSHawking Zhang ret = psp_hdcp_initialize(psp);
28840d6f39bbSHawking Zhang if (ret)
28850d6f39bbSHawking Zhang dev_err(psp->adev->dev,
28860d6f39bbSHawking Zhang "HDCP: Failed to initialize HDCP\n");
28870d6f39bbSHawking Zhang
28880d6f39bbSHawking Zhang ret = psp_dtm_initialize(psp);
28890d6f39bbSHawking Zhang if (ret)
28900d6f39bbSHawking Zhang dev_err(psp->adev->dev,
28910d6f39bbSHawking Zhang "DTM: Failed to initialize DTM\n");
28928602692bSWenhui Sheng
28938602692bSWenhui Sheng ret = psp_rap_initialize(psp);
28948602692bSWenhui Sheng if (ret)
28958602692bSWenhui Sheng dev_err(psp->adev->dev,
28968602692bSWenhui Sheng "RAP: Failed to initialize RAP\n");
2897ecaafb7bSJinzhou Su
2898ecaafb7bSJinzhou Su ret = psp_securedisplay_initialize(psp);
2899ecaafb7bSJinzhou Su if (ret)
2900ecaafb7bSJinzhou Su dev_err(psp->adev->dev,
2901ecaafb7bSJinzhou Su "SECUREDISPLAY: Failed to initialize SECUREDISPLAY\n");
29020d6f39bbSHawking Zhang }
29030d6f39bbSHawking Zhang
29040e5ca0d1SHuang Rui mutex_unlock(&adev->firmware.mutex);
29050e5ca0d1SHuang Rui
290693ea9b9fSHuang Rui return 0;
290793ea9b9fSHuang Rui
290893ea9b9fSHuang Rui failed:
290993ea9b9fSHuang Rui DRM_ERROR("PSP resume failed\n");
291093ea9b9fSHuang Rui mutex_unlock(&adev->firmware.mutex);
29110e5ca0d1SHuang Rui return ret;
29120e5ca0d1SHuang Rui }
29130e5ca0d1SHuang Rui
psp_gpu_reset(struct amdgpu_device * adev)2914f75a9a5dSAlex Deucher int psp_gpu_reset(struct amdgpu_device *adev)
291598512bb8SKen Wang {
291632eaeae0SAlex Deucher int ret;
291732eaeae0SAlex Deucher
29182d4f9020SHuang Rui if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
29192d4f9020SHuang Rui return 0;
29202d4f9020SHuang Rui
292132eaeae0SAlex Deucher mutex_lock(&adev->psp.mutex);
292232eaeae0SAlex Deucher ret = psp_mode1_reset(&adev->psp);
292332eaeae0SAlex Deucher mutex_unlock(&adev->psp.mutex);
292432eaeae0SAlex Deucher
292532eaeae0SAlex Deucher return ret;
292698512bb8SKen Wang }
292798512bb8SKen Wang
psp_rlc_autoload_start(struct psp_context * psp)29281a5b4ccaSHawking Zhang int psp_rlc_autoload_start(struct psp_context *psp)
29291a5b4ccaSHawking Zhang {
29301a5b4ccaSHawking Zhang int ret;
29314b296527SJohn Clements struct psp_gfx_cmd_resp *cmd = acquire_psp_cmd_buf(psp);
29321a5b4ccaSHawking Zhang
29331a5b4ccaSHawking Zhang cmd->cmd_id = GFX_CMD_ID_AUTOLOAD_RLC;
29341a5b4ccaSHawking Zhang
29351a5b4ccaSHawking Zhang ret = psp_cmd_submit_buf(psp, NULL, cmd,
29361a5b4ccaSHawking Zhang psp->fence_buf_mc_addr);
29374fb93071SCandice Li
29384b296527SJohn Clements release_psp_cmd_buf(psp);
29394b296527SJohn Clements
29401a5b4ccaSHawking Zhang return ret;
29411a5b4ccaSHawking Zhang }
29421a5b4ccaSHawking Zhang
psp_ring_cmd_submit(struct psp_context * psp,uint64_t cmd_buf_mc_addr,uint64_t fence_mc_addr,int index)2943cc65176eSHawking Zhang int psp_ring_cmd_submit(struct psp_context *psp,
2944cc65176eSHawking Zhang uint64_t cmd_buf_mc_addr,
2945cc65176eSHawking Zhang uint64_t fence_mc_addr,
2946cc65176eSHawking Zhang int index)
2947cc65176eSHawking Zhang {
2948cc65176eSHawking Zhang unsigned int psp_write_ptr_reg = 0;
29492e77541bSColin Ian King struct psp_gfx_rb_frame *write_frame;
2950cc65176eSHawking Zhang struct psp_ring *ring = &psp->km_ring;
2951cc65176eSHawking Zhang struct psp_gfx_rb_frame *ring_buffer_start = ring->ring_mem;
2952cc65176eSHawking Zhang struct psp_gfx_rb_frame *ring_buffer_end = ring_buffer_start +
2953cc65176eSHawking Zhang ring->ring_size / sizeof(struct psp_gfx_rb_frame) - 1;
2954cc65176eSHawking Zhang struct amdgpu_device *adev = psp->adev;
2955cc65176eSHawking Zhang uint32_t ring_size_dw = ring->ring_size / 4;
2956cc65176eSHawking Zhang uint32_t rb_frame_size_dw = sizeof(struct psp_gfx_rb_frame) / 4;
2957cc65176eSHawking Zhang
2958cc65176eSHawking Zhang /* KM (GPCOM) prepare write pointer */
2959cc65176eSHawking Zhang psp_write_ptr_reg = psp_ring_get_wptr(psp);
2960cc65176eSHawking Zhang
2961cc65176eSHawking Zhang /* Update KM RB frame pointer to new frame */
2962cc65176eSHawking Zhang /* write_frame ptr increments by size of rb_frame in bytes */
2963cc65176eSHawking Zhang /* psp_write_ptr_reg increments by size of rb_frame in DWORDs */
2964cc65176eSHawking Zhang if ((psp_write_ptr_reg % ring_size_dw) == 0)
2965cc65176eSHawking Zhang write_frame = ring_buffer_start;
2966cc65176eSHawking Zhang else
2967cc65176eSHawking Zhang write_frame = ring_buffer_start + (psp_write_ptr_reg / rb_frame_size_dw);
2968cc65176eSHawking Zhang /* Check invalid write_frame ptr address */
2969cc65176eSHawking Zhang if ((write_frame < ring_buffer_start) || (ring_buffer_end < write_frame)) {
2970cc65176eSHawking Zhang DRM_ERROR("ring_buffer_start = %p; ring_buffer_end = %p; write_frame = %p\n",
2971cc65176eSHawking Zhang ring_buffer_start, ring_buffer_end, write_frame);
2972cc65176eSHawking Zhang DRM_ERROR("write_frame is pointing to address out of bounds\n");
2973cc65176eSHawking Zhang return -EINVAL;
2974cc65176eSHawking Zhang }
2975cc65176eSHawking Zhang
2976cc65176eSHawking Zhang /* Initialize KM RB frame */
2977cc65176eSHawking Zhang memset(write_frame, 0, sizeof(struct psp_gfx_rb_frame));
2978cc65176eSHawking Zhang
2979cc65176eSHawking Zhang /* Update KM RB frame */
2980cc65176eSHawking Zhang write_frame->cmd_buf_addr_hi = upper_32_bits(cmd_buf_mc_addr);
2981cc65176eSHawking Zhang write_frame->cmd_buf_addr_lo = lower_32_bits(cmd_buf_mc_addr);
2982cc65176eSHawking Zhang write_frame->fence_addr_hi = upper_32_bits(fence_mc_addr);
2983cc65176eSHawking Zhang write_frame->fence_addr_lo = lower_32_bits(fence_mc_addr);
2984cc65176eSHawking Zhang write_frame->fence_value = index;
2985810085ddSEric Huang amdgpu_device_flush_hdp(adev, NULL);
2986cc65176eSHawking Zhang
2987cc65176eSHawking Zhang /* Update the write Pointer in DWORDs */
2988cc65176eSHawking Zhang psp_write_ptr_reg = (psp_write_ptr_reg + rb_frame_size_dw) % ring_size_dw;
2989cc65176eSHawking Zhang psp_ring_set_wptr(psp, psp_write_ptr_reg);
2990cc65176eSHawking Zhang return 0;
2991cc65176eSHawking Zhang }
2992cc65176eSHawking Zhang
psp_init_asd_microcode(struct psp_context * psp,const char * chip_name)29932d39c7aeSMario Limonciello int psp_init_asd_microcode(struct psp_context *psp, const char *chip_name)
2994dc7195f6SHawking Zhang {
2995dc7195f6SHawking Zhang struct amdgpu_device *adev = psp->adev;
29960a305e34STao Zhou char fw_name[PSP_FW_NAME_LEN];
2997dc7195f6SHawking Zhang const struct psp_firmware_header_v1_0 *asd_hdr;
2998dc7195f6SHawking Zhang int err = 0;
2999dc7195f6SHawking Zhang
3000dc7195f6SHawking Zhang snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_asd.bin", chip_name);
300107dbfc6bSMario Limonciello err = amdgpu_ucode_request(adev, &adev->psp.asd_fw, fw_name);
3002dc7195f6SHawking Zhang if (err)
3003dc7195f6SHawking Zhang goto out;
3004dc7195f6SHawking Zhang
3005dc7195f6SHawking Zhang asd_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.asd_fw->data;
3006de3a1e33SCandice Li adev->psp.asd_context.bin_desc.fw_version = le32_to_cpu(asd_hdr->header.ucode_version);
3007de3a1e33SCandice Li adev->psp.asd_context.bin_desc.feature_version = le32_to_cpu(asd_hdr->sos.fw_version);
3008de3a1e33SCandice Li adev->psp.asd_context.bin_desc.size_bytes = le32_to_cpu(asd_hdr->header.ucode_size_bytes);
3009de3a1e33SCandice Li adev->psp.asd_context.bin_desc.start_addr = (uint8_t *)asd_hdr +
3010dc7195f6SHawking Zhang le32_to_cpu(asd_hdr->header.ucode_array_offset_bytes);
3011dc7195f6SHawking Zhang return 0;
3012dc7195f6SHawking Zhang out:
301307dbfc6bSMario Limonciello amdgpu_ucode_release(&adev->psp.asd_fw);
3014dc7195f6SHawking Zhang return err;
3015dc7195f6SHawking Zhang }
3016dc7195f6SHawking Zhang
psp_init_toc_microcode(struct psp_context * psp,const char * chip_name)30172d39c7aeSMario Limonciello int psp_init_toc_microcode(struct psp_context *psp, const char *chip_name)
30185120cb54SHuang Rui {
30195120cb54SHuang Rui struct amdgpu_device *adev = psp->adev;
302030ebc16aSLang Yu char fw_name[PSP_FW_NAME_LEN];
30215120cb54SHuang Rui const struct psp_firmware_header_v1_0 *toc_hdr;
30225120cb54SHuang Rui int err = 0;
30235120cb54SHuang Rui
30245120cb54SHuang Rui snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_toc.bin", chip_name);
302507dbfc6bSMario Limonciello err = amdgpu_ucode_request(adev, &adev->psp.toc_fw, fw_name);
30265120cb54SHuang Rui if (err)
30275120cb54SHuang Rui goto out;
30285120cb54SHuang Rui
30295120cb54SHuang Rui toc_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.toc_fw->data;
3030222e0a71SCandice Li adev->psp.toc.fw_version = le32_to_cpu(toc_hdr->header.ucode_version);
3031222e0a71SCandice Li adev->psp.toc.feature_version = le32_to_cpu(toc_hdr->sos.fw_version);
3032222e0a71SCandice Li adev->psp.toc.size_bytes = le32_to_cpu(toc_hdr->header.ucode_size_bytes);
3033222e0a71SCandice Li adev->psp.toc.start_addr = (uint8_t *)toc_hdr +
30345120cb54SHuang Rui le32_to_cpu(toc_hdr->header.ucode_array_offset_bytes);
30355120cb54SHuang Rui return 0;
30365120cb54SHuang Rui out:
303707dbfc6bSMario Limonciello amdgpu_ucode_release(&adev->psp.toc_fw);
30385120cb54SHuang Rui return err;
30395120cb54SHuang Rui }
30405120cb54SHuang Rui
parse_sos_bin_descriptor(struct psp_context * psp,const struct psp_fw_bin_desc * desc,const struct psp_firmware_header_v2_0 * sos_hdr)3041f8e487ceSJohn Clements static int parse_sos_bin_descriptor(struct psp_context *psp,
3042f8e487ceSJohn Clements const struct psp_fw_bin_desc *desc,
3043f8e487ceSJohn Clements const struct psp_firmware_header_v2_0 *sos_hdr)
3044f8e487ceSJohn Clements {
3045f8e487ceSJohn Clements uint8_t *ucode_start_addr = NULL;
3046f8e487ceSJohn Clements
3047f8e487ceSJohn Clements if (!psp || !desc || !sos_hdr)
3048f8e487ceSJohn Clements return -EINVAL;
3049f8e487ceSJohn Clements
3050f8e487ceSJohn Clements ucode_start_addr = (uint8_t *)sos_hdr +
3051f8e487ceSJohn Clements le32_to_cpu(desc->offset_bytes) +
3052f8e487ceSJohn Clements le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
3053f8e487ceSJohn Clements
3054f8e487ceSJohn Clements switch (desc->fw_type) {
3055f8e487ceSJohn Clements case PSP_FW_TYPE_PSP_SOS:
3056f8e487ceSJohn Clements psp->sos.fw_version = le32_to_cpu(desc->fw_version);
3057f8e487ceSJohn Clements psp->sos.feature_version = le32_to_cpu(desc->fw_version);
3058f8e487ceSJohn Clements psp->sos.size_bytes = le32_to_cpu(desc->size_bytes);
3059f8e487ceSJohn Clements psp->sos.start_addr = ucode_start_addr;
3060f8e487ceSJohn Clements break;
3061f8e487ceSJohn Clements case PSP_FW_TYPE_PSP_SYS_DRV:
3062f8e487ceSJohn Clements psp->sys.fw_version = le32_to_cpu(desc->fw_version);
3063f8e487ceSJohn Clements psp->sys.feature_version = le32_to_cpu(desc->fw_version);
3064f8e487ceSJohn Clements psp->sys.size_bytes = le32_to_cpu(desc->size_bytes);
3065f8e487ceSJohn Clements psp->sys.start_addr = ucode_start_addr;
3066f8e487ceSJohn Clements break;
3067f8e487ceSJohn Clements case PSP_FW_TYPE_PSP_KDB:
3068f8e487ceSJohn Clements psp->kdb.fw_version = le32_to_cpu(desc->fw_version);
3069f8e487ceSJohn Clements psp->kdb.feature_version = le32_to_cpu(desc->fw_version);
3070f8e487ceSJohn Clements psp->kdb.size_bytes = le32_to_cpu(desc->size_bytes);
3071f8e487ceSJohn Clements psp->kdb.start_addr = ucode_start_addr;
3072f8e487ceSJohn Clements break;
3073f8e487ceSJohn Clements case PSP_FW_TYPE_PSP_TOC:
3074f8e487ceSJohn Clements psp->toc.fw_version = le32_to_cpu(desc->fw_version);
3075f8e487ceSJohn Clements psp->toc.feature_version = le32_to_cpu(desc->fw_version);
3076f8e487ceSJohn Clements psp->toc.size_bytes = le32_to_cpu(desc->size_bytes);
3077f8e487ceSJohn Clements psp->toc.start_addr = ucode_start_addr;
3078f8e487ceSJohn Clements break;
3079f8e487ceSJohn Clements case PSP_FW_TYPE_PSP_SPL:
3080f8e487ceSJohn Clements psp->spl.fw_version = le32_to_cpu(desc->fw_version);
3081f8e487ceSJohn Clements psp->spl.feature_version = le32_to_cpu(desc->fw_version);
3082f8e487ceSJohn Clements psp->spl.size_bytes = le32_to_cpu(desc->size_bytes);
3083f8e487ceSJohn Clements psp->spl.start_addr = ucode_start_addr;
3084f8e487ceSJohn Clements break;
3085f8e487ceSJohn Clements case PSP_FW_TYPE_PSP_RL:
3086f8e487ceSJohn Clements psp->rl.fw_version = le32_to_cpu(desc->fw_version);
3087f8e487ceSJohn Clements psp->rl.feature_version = le32_to_cpu(desc->fw_version);
3088f8e487ceSJohn Clements psp->rl.size_bytes = le32_to_cpu(desc->size_bytes);
3089f8e487ceSJohn Clements psp->rl.start_addr = ucode_start_addr;
3090f8e487ceSJohn Clements break;
30916ff34fd6SJohn Clements case PSP_FW_TYPE_PSP_SOC_DRV:
30926ff34fd6SJohn Clements psp->soc_drv.fw_version = le32_to_cpu(desc->fw_version);
30936ff34fd6SJohn Clements psp->soc_drv.feature_version = le32_to_cpu(desc->fw_version);
30946ff34fd6SJohn Clements psp->soc_drv.size_bytes = le32_to_cpu(desc->size_bytes);
30956ff34fd6SJohn Clements psp->soc_drv.start_addr = ucode_start_addr;
30966ff34fd6SJohn Clements break;
30976ff34fd6SJohn Clements case PSP_FW_TYPE_PSP_INTF_DRV:
30986ff34fd6SJohn Clements psp->intf_drv.fw_version = le32_to_cpu(desc->fw_version);
30996ff34fd6SJohn Clements psp->intf_drv.feature_version = le32_to_cpu(desc->fw_version);
31006ff34fd6SJohn Clements psp->intf_drv.size_bytes = le32_to_cpu(desc->size_bytes);
31016ff34fd6SJohn Clements psp->intf_drv.start_addr = ucode_start_addr;
31026ff34fd6SJohn Clements break;
31036ff34fd6SJohn Clements case PSP_FW_TYPE_PSP_DBG_DRV:
31046ff34fd6SJohn Clements psp->dbg_drv.fw_version = le32_to_cpu(desc->fw_version);
31056ff34fd6SJohn Clements psp->dbg_drv.feature_version = le32_to_cpu(desc->fw_version);
31066ff34fd6SJohn Clements psp->dbg_drv.size_bytes = le32_to_cpu(desc->size_bytes);
31076ff34fd6SJohn Clements psp->dbg_drv.start_addr = ucode_start_addr;
31086ff34fd6SJohn Clements break;
3109c3db1b90SJohn Clements case PSP_FW_TYPE_PSP_RAS_DRV:
3110c3db1b90SJohn Clements psp->ras_drv.fw_version = le32_to_cpu(desc->fw_version);
3111c3db1b90SJohn Clements psp->ras_drv.feature_version = le32_to_cpu(desc->fw_version);
3112c3db1b90SJohn Clements psp->ras_drv.size_bytes = le32_to_cpu(desc->size_bytes);
3113c3db1b90SJohn Clements psp->ras_drv.start_addr = ucode_start_addr;
3114c3db1b90SJohn Clements break;
3115f8e487ceSJohn Clements default:
3116f8e487ceSJohn Clements dev_warn(psp->adev->dev, "Unsupported PSP FW type: %d\n", desc->fw_type);
3117f8e487ceSJohn Clements break;
3118f8e487ceSJohn Clements }
3119f8e487ceSJohn Clements
3120f8e487ceSJohn Clements return 0;
3121f8e487ceSJohn Clements }
3122f8e487ceSJohn Clements
psp_init_sos_base_fw(struct amdgpu_device * adev)31232a9a151fSJohn Clements static int psp_init_sos_base_fw(struct amdgpu_device *adev)
31242a9a151fSJohn Clements {
31252a9a151fSJohn Clements const struct psp_firmware_header_v1_0 *sos_hdr;
31262a9a151fSJohn Clements const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3127ed4454c3SHawking Zhang uint8_t *ucode_array_start_addr;
31282a9a151fSJohn Clements
31292a9a151fSJohn Clements sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3130ed4454c3SHawking Zhang ucode_array_start_addr = (uint8_t *)sos_hdr +
3131ed4454c3SHawking Zhang le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
31322a9a151fSJohn Clements
313382d05736SAlex Deucher if (adev->gmc.xgmi.connected_to_cpu ||
31341d789535SAlex Deucher (adev->ip_versions[MP0_HWIP][0] != IP_VERSION(13, 0, 2))) {
3135222e0a71SCandice Li adev->psp.sos.fw_version = le32_to_cpu(sos_hdr->header.ucode_version);
3136222e0a71SCandice Li adev->psp.sos.feature_version = le32_to_cpu(sos_hdr->sos.fw_version);
31372a9a151fSJohn Clements
3138222e0a71SCandice Li adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr->sos.offset_bytes);
3139222e0a71SCandice Li adev->psp.sys.start_addr = ucode_array_start_addr;
31402a9a151fSJohn Clements
3141222e0a71SCandice Li adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr->sos.size_bytes);
3142222e0a71SCandice Li adev->psp.sos.start_addr = ucode_array_start_addr +
31432a9a151fSJohn Clements le32_to_cpu(sos_hdr->sos.offset_bytes);
31442a9a151fSJohn Clements } else {
31452a9a151fSJohn Clements /* Load alternate PSP SOS FW */
31462a9a151fSJohn Clements sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
31472a9a151fSJohn Clements
3148222e0a71SCandice Li adev->psp.sos.fw_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
3149222e0a71SCandice Li adev->psp.sos.feature_version = le32_to_cpu(sos_hdr_v1_3->sos_aux.fw_version);
31502a9a151fSJohn Clements
3151222e0a71SCandice Li adev->psp.sys.size_bytes = le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.size_bytes);
3152222e0a71SCandice Li adev->psp.sys.start_addr = ucode_array_start_addr +
31532a9a151fSJohn Clements le32_to_cpu(sos_hdr_v1_3->sys_drv_aux.offset_bytes);
31542a9a151fSJohn Clements
3155222e0a71SCandice Li adev->psp.sos.size_bytes = le32_to_cpu(sos_hdr_v1_3->sos_aux.size_bytes);
3156222e0a71SCandice Li adev->psp.sos.start_addr = ucode_array_start_addr +
31572a9a151fSJohn Clements le32_to_cpu(sos_hdr_v1_3->sos_aux.offset_bytes);
31582a9a151fSJohn Clements }
31592a9a151fSJohn Clements
3160222e0a71SCandice Li if ((adev->psp.sys.size_bytes == 0) || (adev->psp.sos.size_bytes == 0)) {
31612a9a151fSJohn Clements dev_warn(adev->dev, "PSP SOS FW not available");
31622a9a151fSJohn Clements return -EINVAL;
31632a9a151fSJohn Clements }
31642a9a151fSJohn Clements
31652a9a151fSJohn Clements return 0;
31662a9a151fSJohn Clements }
31672a9a151fSJohn Clements
psp_init_sos_microcode(struct psp_context * psp,const char * chip_name)31682d39c7aeSMario Limonciello int psp_init_sos_microcode(struct psp_context *psp, const char *chip_name)
31691c301f44SHawking Zhang {
31701c301f44SHawking Zhang struct amdgpu_device *adev = psp->adev;
31710a305e34STao Zhou char fw_name[PSP_FW_NAME_LEN];
31721c301f44SHawking Zhang const struct psp_firmware_header_v1_0 *sos_hdr;
31731c301f44SHawking Zhang const struct psp_firmware_header_v1_1 *sos_hdr_v1_1;
31741c301f44SHawking Zhang const struct psp_firmware_header_v1_2 *sos_hdr_v1_2;
317543a188e0SLikun Gao const struct psp_firmware_header_v1_3 *sos_hdr_v1_3;
3176f8e487ceSJohn Clements const struct psp_firmware_header_v2_0 *sos_hdr_v2_0;
31771c301f44SHawking Zhang int err = 0;
3178ed4454c3SHawking Zhang uint8_t *ucode_array_start_addr;
3179f8e487ceSJohn Clements int fw_index = 0;
31801c301f44SHawking Zhang
31811c301f44SHawking Zhang snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_sos.bin", chip_name);
318207dbfc6bSMario Limonciello err = amdgpu_ucode_request(adev, &adev->psp.sos_fw, fw_name);
31831c301f44SHawking Zhang if (err)
31841c301f44SHawking Zhang goto out;
31851c301f44SHawking Zhang
31861c301f44SHawking Zhang sos_hdr = (const struct psp_firmware_header_v1_0 *)adev->psp.sos_fw->data;
3187ed4454c3SHawking Zhang ucode_array_start_addr = (uint8_t *)sos_hdr +
3188ed4454c3SHawking Zhang le32_to_cpu(sos_hdr->header.ucode_array_offset_bytes);
31891c301f44SHawking Zhang amdgpu_ucode_print_psp_hdr(&sos_hdr->header);
31901c301f44SHawking Zhang
31911c301f44SHawking Zhang switch (sos_hdr->header.header_version_major) {
31921c301f44SHawking Zhang case 1:
31932a9a151fSJohn Clements err = psp_init_sos_base_fw(adev);
31942a9a151fSJohn Clements if (err)
31952a9a151fSJohn Clements goto out;
31962a9a151fSJohn Clements
31971c301f44SHawking Zhang if (sos_hdr->header.header_version_minor == 1) {
31981c301f44SHawking Zhang sos_hdr_v1_1 = (const struct psp_firmware_header_v1_1 *)adev->psp.sos_fw->data;
3199222e0a71SCandice Li adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_1->toc.size_bytes);
3200222e0a71SCandice Li adev->psp.toc.start_addr = (uint8_t *)adev->psp.sys.start_addr +
320179a0f441SJohn Clements le32_to_cpu(sos_hdr_v1_1->toc.offset_bytes);
3202222e0a71SCandice Li adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_1->kdb.size_bytes);
3203222e0a71SCandice Li adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
320479a0f441SJohn Clements le32_to_cpu(sos_hdr_v1_1->kdb.offset_bytes);
32051c301f44SHawking Zhang }
32061c301f44SHawking Zhang if (sos_hdr->header.header_version_minor == 2) {
32071c301f44SHawking Zhang sos_hdr_v1_2 = (const struct psp_firmware_header_v1_2 *)adev->psp.sos_fw->data;
3208222e0a71SCandice Li adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_2->kdb.size_bytes);
3209222e0a71SCandice Li adev->psp.kdb.start_addr = (uint8_t *)adev->psp.sys.start_addr +
321079a0f441SJohn Clements le32_to_cpu(sos_hdr_v1_2->kdb.offset_bytes);
32111c301f44SHawking Zhang }
321243a188e0SLikun Gao if (sos_hdr->header.header_version_minor == 3) {
321343a188e0SLikun Gao sos_hdr_v1_3 = (const struct psp_firmware_header_v1_3 *)adev->psp.sos_fw->data;
3214222e0a71SCandice Li adev->psp.toc.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.toc.size_bytes);
3215222e0a71SCandice Li adev->psp.toc.start_addr = ucode_array_start_addr +
321679a0f441SJohn Clements le32_to_cpu(sos_hdr_v1_3->v1_1.toc.offset_bytes);
3217222e0a71SCandice Li adev->psp.kdb.size_bytes = le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.size_bytes);
3218222e0a71SCandice Li adev->psp.kdb.start_addr = ucode_array_start_addr +
321979a0f441SJohn Clements le32_to_cpu(sos_hdr_v1_3->v1_1.kdb.offset_bytes);
3220222e0a71SCandice Li adev->psp.spl.size_bytes = le32_to_cpu(sos_hdr_v1_3->spl.size_bytes);
3221222e0a71SCandice Li adev->psp.spl.start_addr = ucode_array_start_addr +
322279a0f441SJohn Clements le32_to_cpu(sos_hdr_v1_3->spl.offset_bytes);
3223222e0a71SCandice Li adev->psp.rl.size_bytes = le32_to_cpu(sos_hdr_v1_3->rl.size_bytes);
3224222e0a71SCandice Li adev->psp.rl.start_addr = ucode_array_start_addr +
322579a0f441SJohn Clements le32_to_cpu(sos_hdr_v1_3->rl.offset_bytes);
322643a188e0SLikun Gao }
32271c301f44SHawking Zhang break;
3228f8e487ceSJohn Clements case 2:
3229f8e487ceSJohn Clements sos_hdr_v2_0 = (const struct psp_firmware_header_v2_0 *)adev->psp.sos_fw->data;
3230f8e487ceSJohn Clements
3231f8e487ceSJohn Clements if (le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3232f8e487ceSJohn Clements dev_err(adev->dev, "packed SOS count exceeds maximum limit\n");
3233f8e487ceSJohn Clements err = -EINVAL;
3234f8e487ceSJohn Clements goto out;
3235f8e487ceSJohn Clements }
3236f8e487ceSJohn Clements
3237f8e487ceSJohn Clements for (fw_index = 0; fw_index < le32_to_cpu(sos_hdr_v2_0->psp_fw_bin_count); fw_index++) {
3238f8e487ceSJohn Clements err = parse_sos_bin_descriptor(psp,
3239f8e487ceSJohn Clements &sos_hdr_v2_0->psp_fw_bin[fw_index],
3240f8e487ceSJohn Clements sos_hdr_v2_0);
3241f8e487ceSJohn Clements if (err)
3242f8e487ceSJohn Clements goto out;
3243f8e487ceSJohn Clements }
3244f8e487ceSJohn Clements break;
32451c301f44SHawking Zhang default:
32461c301f44SHawking Zhang dev_err(adev->dev,
32471c301f44SHawking Zhang "unsupported psp sos firmware\n");
32481c301f44SHawking Zhang err = -EINVAL;
32491c301f44SHawking Zhang goto out;
32501c301f44SHawking Zhang }
32511c301f44SHawking Zhang
32521c301f44SHawking Zhang return 0;
32531c301f44SHawking Zhang out:
325407dbfc6bSMario Limonciello amdgpu_ucode_release(&adev->psp.sos_fw);
32551c301f44SHawking Zhang
32561c301f44SHawking Zhang return err;
32571c301f44SHawking Zhang }
32581c301f44SHawking Zhang
parse_ta_bin_descriptor(struct psp_context * psp,const struct psp_fw_bin_desc * desc,const struct ta_firmware_header_v2_0 * ta_hdr)3259c18dd61aSLee Jones static int parse_ta_bin_descriptor(struct psp_context *psp,
3260f8e487ceSJohn Clements const struct psp_fw_bin_desc *desc,
3261dcf9864dSJohn Clements const struct ta_firmware_header_v2_0 *ta_hdr)
3262dcf9864dSJohn Clements {
3263dcf9864dSJohn Clements uint8_t *ucode_start_addr = NULL;
3264dcf9864dSJohn Clements
3265dcf9864dSJohn Clements if (!psp || !desc || !ta_hdr)
3266dcf9864dSJohn Clements return -EINVAL;
3267dcf9864dSJohn Clements
3268a3302729SJohn Clements ucode_start_addr = (uint8_t *)ta_hdr +
3269a3302729SJohn Clements le32_to_cpu(desc->offset_bytes) +
3270a3302729SJohn Clements le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3271dcf9864dSJohn Clements
3272dcf9864dSJohn Clements switch (desc->fw_type) {
3273dcf9864dSJohn Clements case TA_FW_TYPE_PSP_ASD:
3274de3a1e33SCandice Li psp->asd_context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3275de3a1e33SCandice Li psp->asd_context.bin_desc.feature_version = le32_to_cpu(desc->fw_version);
3276de3a1e33SCandice Li psp->asd_context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3277de3a1e33SCandice Li psp->asd_context.bin_desc.start_addr = ucode_start_addr;
3278dcf9864dSJohn Clements break;
3279dcf9864dSJohn Clements case TA_FW_TYPE_PSP_XGMI:
32804320e6f8SCandice Li psp->xgmi_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3281de3a1e33SCandice Li psp->xgmi_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3282de3a1e33SCandice Li psp->xgmi_context.context.bin_desc.start_addr = ucode_start_addr;
3283dcf9864dSJohn Clements break;
3284dcf9864dSJohn Clements case TA_FW_TYPE_PSP_RAS:
32854320e6f8SCandice Li psp->ras_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3286de3a1e33SCandice Li psp->ras_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3287de3a1e33SCandice Li psp->ras_context.context.bin_desc.start_addr = ucode_start_addr;
3288dcf9864dSJohn Clements break;
3289dcf9864dSJohn Clements case TA_FW_TYPE_PSP_HDCP:
32904320e6f8SCandice Li psp->hdcp_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3291de3a1e33SCandice Li psp->hdcp_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3292de3a1e33SCandice Li psp->hdcp_context.context.bin_desc.start_addr = ucode_start_addr;
3293dcf9864dSJohn Clements break;
3294dcf9864dSJohn Clements case TA_FW_TYPE_PSP_DTM:
32954320e6f8SCandice Li psp->dtm_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3296de3a1e33SCandice Li psp->dtm_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3297de3a1e33SCandice Li psp->dtm_context.context.bin_desc.start_addr = ucode_start_addr;
3298dcf9864dSJohn Clements break;
32998602692bSWenhui Sheng case TA_FW_TYPE_PSP_RAP:
33004320e6f8SCandice Li psp->rap_context.context.bin_desc.fw_version = le32_to_cpu(desc->fw_version);
3301de3a1e33SCandice Li psp->rap_context.context.bin_desc.size_bytes = le32_to_cpu(desc->size_bytes);
3302de3a1e33SCandice Li psp->rap_context.context.bin_desc.start_addr = ucode_start_addr;
33038602692bSWenhui Sheng break;
3304ecaafb7bSJinzhou Su case TA_FW_TYPE_PSP_SECUREDISPLAY:
33054320e6f8SCandice Li psp->securedisplay_context.context.bin_desc.fw_version =
3306de3a1e33SCandice Li le32_to_cpu(desc->fw_version);
3307de3a1e33SCandice Li psp->securedisplay_context.context.bin_desc.size_bytes =
3308de3a1e33SCandice Li le32_to_cpu(desc->size_bytes);
3309de3a1e33SCandice Li psp->securedisplay_context.context.bin_desc.start_addr =
3310de3a1e33SCandice Li ucode_start_addr;
3311ecaafb7bSJinzhou Su break;
3312dcf9864dSJohn Clements default:
3313dcf9864dSJohn Clements dev_warn(psp->adev->dev, "Unsupported TA type: %d\n", desc->fw_type);
3314dcf9864dSJohn Clements break;
3315dcf9864dSJohn Clements }
3316dcf9864dSJohn Clements
3317dcf9864dSJohn Clements return 0;
3318dcf9864dSJohn Clements }
3319dcf9864dSJohn Clements
parse_ta_v1_microcode(struct psp_context * psp)3320994a9744SMario Limonciello static int parse_ta_v1_microcode(struct psp_context *psp)
3321dcf9864dSJohn Clements {
3322994a9744SMario Limonciello const struct ta_firmware_header_v1_0 *ta_hdr;
3323dcf9864dSJohn Clements struct amdgpu_device *adev = psp->adev;
3324994a9744SMario Limonciello
3325994a9744SMario Limonciello ta_hdr = (const struct ta_firmware_header_v1_0 *) adev->psp.ta_fw->data;
3326994a9744SMario Limonciello
3327994a9744SMario Limonciello if (le16_to_cpu(ta_hdr->header.header_version_major) != 1)
3328994a9744SMario Limonciello return -EINVAL;
3329994a9744SMario Limonciello
3330994a9744SMario Limonciello adev->psp.xgmi_context.context.bin_desc.fw_version =
3331994a9744SMario Limonciello le32_to_cpu(ta_hdr->xgmi.fw_version);
3332994a9744SMario Limonciello adev->psp.xgmi_context.context.bin_desc.size_bytes =
3333994a9744SMario Limonciello le32_to_cpu(ta_hdr->xgmi.size_bytes);
3334994a9744SMario Limonciello adev->psp.xgmi_context.context.bin_desc.start_addr =
3335994a9744SMario Limonciello (uint8_t *)ta_hdr +
3336994a9744SMario Limonciello le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3337994a9744SMario Limonciello
3338994a9744SMario Limonciello adev->psp.ras_context.context.bin_desc.fw_version =
3339994a9744SMario Limonciello le32_to_cpu(ta_hdr->ras.fw_version);
3340994a9744SMario Limonciello adev->psp.ras_context.context.bin_desc.size_bytes =
3341994a9744SMario Limonciello le32_to_cpu(ta_hdr->ras.size_bytes);
3342994a9744SMario Limonciello adev->psp.ras_context.context.bin_desc.start_addr =
3343994a9744SMario Limonciello (uint8_t *)adev->psp.xgmi_context.context.bin_desc.start_addr +
3344994a9744SMario Limonciello le32_to_cpu(ta_hdr->ras.offset_bytes);
3345994a9744SMario Limonciello
3346994a9744SMario Limonciello adev->psp.hdcp_context.context.bin_desc.fw_version =
3347994a9744SMario Limonciello le32_to_cpu(ta_hdr->hdcp.fw_version);
3348994a9744SMario Limonciello adev->psp.hdcp_context.context.bin_desc.size_bytes =
3349994a9744SMario Limonciello le32_to_cpu(ta_hdr->hdcp.size_bytes);
3350994a9744SMario Limonciello adev->psp.hdcp_context.context.bin_desc.start_addr =
3351994a9744SMario Limonciello (uint8_t *)ta_hdr +
3352994a9744SMario Limonciello le32_to_cpu(ta_hdr->header.ucode_array_offset_bytes);
3353994a9744SMario Limonciello
3354994a9744SMario Limonciello adev->psp.dtm_context.context.bin_desc.fw_version =
3355994a9744SMario Limonciello le32_to_cpu(ta_hdr->dtm.fw_version);
3356994a9744SMario Limonciello adev->psp.dtm_context.context.bin_desc.size_bytes =
3357994a9744SMario Limonciello le32_to_cpu(ta_hdr->dtm.size_bytes);
3358994a9744SMario Limonciello adev->psp.dtm_context.context.bin_desc.start_addr =
3359994a9744SMario Limonciello (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3360994a9744SMario Limonciello le32_to_cpu(ta_hdr->dtm.offset_bytes);
3361994a9744SMario Limonciello
3362994a9744SMario Limonciello adev->psp.securedisplay_context.context.bin_desc.fw_version =
3363994a9744SMario Limonciello le32_to_cpu(ta_hdr->securedisplay.fw_version);
3364994a9744SMario Limonciello adev->psp.securedisplay_context.context.bin_desc.size_bytes =
3365994a9744SMario Limonciello le32_to_cpu(ta_hdr->securedisplay.size_bytes);
3366994a9744SMario Limonciello adev->psp.securedisplay_context.context.bin_desc.start_addr =
3367994a9744SMario Limonciello (uint8_t *)adev->psp.hdcp_context.context.bin_desc.start_addr +
3368994a9744SMario Limonciello le32_to_cpu(ta_hdr->securedisplay.offset_bytes);
3369994a9744SMario Limonciello
3370994a9744SMario Limonciello adev->psp.ta_fw_version = le32_to_cpu(ta_hdr->header.ucode_version);
3371994a9744SMario Limonciello
3372994a9744SMario Limonciello return 0;
3373994a9744SMario Limonciello }
3374994a9744SMario Limonciello
parse_ta_v2_microcode(struct psp_context * psp)3375994a9744SMario Limonciello static int parse_ta_v2_microcode(struct psp_context *psp)
3376994a9744SMario Limonciello {
3377dcf9864dSJohn Clements const struct ta_firmware_header_v2_0 *ta_hdr;
3378994a9744SMario Limonciello struct amdgpu_device *adev = psp->adev;
3379dcf9864dSJohn Clements int err = 0;
3380dcf9864dSJohn Clements int ta_index = 0;
3381dcf9864dSJohn Clements
3382dcf9864dSJohn Clements ta_hdr = (const struct ta_firmware_header_v2_0 *)adev->psp.ta_fw->data;
3383dcf9864dSJohn Clements
3384994a9744SMario Limonciello if (le16_to_cpu(ta_hdr->header.header_version_major) != 2)
3385994a9744SMario Limonciello return -EINVAL;
3386dcf9864dSJohn Clements
3387f8e487ceSJohn Clements if (le32_to_cpu(ta_hdr->ta_fw_bin_count) >= UCODE_MAX_PSP_PACKAGING) {
3388dcf9864dSJohn Clements dev_err(adev->dev, "packed TA count exceeds maximum limit\n");
3389994a9744SMario Limonciello return -EINVAL;
3390dcf9864dSJohn Clements }
3391dcf9864dSJohn Clements
3392dcf9864dSJohn Clements for (ta_index = 0; ta_index < le32_to_cpu(ta_hdr->ta_fw_bin_count); ta_index++) {
3393dcf9864dSJohn Clements err = parse_ta_bin_descriptor(psp,
3394dcf9864dSJohn Clements &ta_hdr->ta_fw_bin[ta_index],
3395dcf9864dSJohn Clements ta_hdr);
3396dcf9864dSJohn Clements if (err)
3397994a9744SMario Limonciello return err;
3398dcf9864dSJohn Clements }
3399dcf9864dSJohn Clements
3400dcf9864dSJohn Clements return 0;
3401994a9744SMario Limonciello }
3402994a9744SMario Limonciello
psp_init_ta_microcode(struct psp_context * psp,const char * chip_name)3403994a9744SMario Limonciello int psp_init_ta_microcode(struct psp_context *psp, const char *chip_name)
3404994a9744SMario Limonciello {
3405994a9744SMario Limonciello const struct common_firmware_header *hdr;
3406994a9744SMario Limonciello struct amdgpu_device *adev = psp->adev;
3407994a9744SMario Limonciello char fw_name[PSP_FW_NAME_LEN];
3408994a9744SMario Limonciello int err;
3409994a9744SMario Limonciello
3410994a9744SMario Limonciello snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ta.bin", chip_name);
341107dbfc6bSMario Limonciello err = amdgpu_ucode_request(adev, &adev->psp.ta_fw, fw_name);
3412994a9744SMario Limonciello if (err)
3413994a9744SMario Limonciello return err;
3414994a9744SMario Limonciello
3415994a9744SMario Limonciello hdr = (const struct common_firmware_header *)adev->psp.ta_fw->data;
3416994a9744SMario Limonciello switch (le16_to_cpu(hdr->header_version_major)) {
3417994a9744SMario Limonciello case 1:
3418994a9744SMario Limonciello err = parse_ta_v1_microcode(psp);
3419994a9744SMario Limonciello break;
3420994a9744SMario Limonciello case 2:
3421994a9744SMario Limonciello err = parse_ta_v2_microcode(psp);
3422994a9744SMario Limonciello break;
3423994a9744SMario Limonciello default:
3424994a9744SMario Limonciello dev_err(adev->dev, "unsupported TA header version\n");
3425994a9744SMario Limonciello err = -EINVAL;
3426994a9744SMario Limonciello }
3427994a9744SMario Limonciello
342807dbfc6bSMario Limonciello if (err)
342907dbfc6bSMario Limonciello amdgpu_ucode_release(&adev->psp.ta_fw);
3430994a9744SMario Limonciello
3431dcf9864dSJohn Clements return err;
3432dcf9864dSJohn Clements }
3433dcf9864dSJohn Clements
psp_init_cap_microcode(struct psp_context * psp,const char * chip_name)34342d39c7aeSMario Limonciello int psp_init_cap_microcode(struct psp_context *psp, const char *chip_name)
3435c4381d0eSBokun Zhang {
3436c4381d0eSBokun Zhang struct amdgpu_device *adev = psp->adev;
3437c4381d0eSBokun Zhang char fw_name[PSP_FW_NAME_LEN];
3438c4381d0eSBokun Zhang const struct psp_firmware_header_v1_0 *cap_hdr_v1_0;
3439c4381d0eSBokun Zhang struct amdgpu_firmware_info *info = NULL;
3440c4381d0eSBokun Zhang int err = 0;
3441c4381d0eSBokun Zhang
3442c4381d0eSBokun Zhang if (!amdgpu_sriov_vf(adev)) {
3443c4381d0eSBokun Zhang dev_err(adev->dev, "cap microcode should only be loaded under SRIOV\n");
3444c4381d0eSBokun Zhang return -EINVAL;
3445c4381d0eSBokun Zhang }
3446c4381d0eSBokun Zhang
3447c4381d0eSBokun Zhang snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_cap.bin", chip_name);
344807dbfc6bSMario Limonciello err = amdgpu_ucode_request(adev, &adev->psp.cap_fw, fw_name);
3449c4381d0eSBokun Zhang if (err) {
345007dbfc6bSMario Limonciello if (err == -ENODEV) {
3451c4381d0eSBokun Zhang dev_warn(adev->dev, "cap microcode does not exist, skip\n");
3452c4381d0eSBokun Zhang err = 0;
3453*3f40a7ffSJiang Liu } else {
3454c4381d0eSBokun Zhang dev_err(adev->dev, "fail to initialize cap microcode\n");
3455c4381d0eSBokun Zhang }
3456*3f40a7ffSJiang Liu goto out;
3457*3f40a7ffSJiang Liu }
3458c4381d0eSBokun Zhang
3459c4381d0eSBokun Zhang info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CAP];
3460c4381d0eSBokun Zhang info->ucode_id = AMDGPU_UCODE_ID_CAP;
3461c4381d0eSBokun Zhang info->fw = adev->psp.cap_fw;
3462c4381d0eSBokun Zhang cap_hdr_v1_0 = (const struct psp_firmware_header_v1_0 *)
3463c4381d0eSBokun Zhang adev->psp.cap_fw->data;
3464c4381d0eSBokun Zhang adev->firmware.fw_size += ALIGN(
3465c4381d0eSBokun Zhang le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes), PAGE_SIZE);
3466c4381d0eSBokun Zhang adev->psp.cap_fw_version = le32_to_cpu(cap_hdr_v1_0->header.ucode_version);
3467c4381d0eSBokun Zhang adev->psp.cap_feature_version = le32_to_cpu(cap_hdr_v1_0->sos.fw_version);
3468c4381d0eSBokun Zhang adev->psp.cap_ucode_size = le32_to_cpu(cap_hdr_v1_0->header.ucode_size_bytes);
3469c4381d0eSBokun Zhang
3470c4381d0eSBokun Zhang return 0;
3471c4381d0eSBokun Zhang
3472c4381d0eSBokun Zhang out:
347307dbfc6bSMario Limonciello amdgpu_ucode_release(&adev->psp.cap_fw);
3474c4381d0eSBokun Zhang return err;
3475c4381d0eSBokun Zhang }
3476c4381d0eSBokun Zhang
psp_set_clockgating_state(void * handle,enum amd_clockgating_state state)34770e5ca0d1SHuang Rui static int psp_set_clockgating_state(void *handle,
34780e5ca0d1SHuang Rui enum amd_clockgating_state state)
34790e5ca0d1SHuang Rui {
34800e5ca0d1SHuang Rui return 0;
34810e5ca0d1SHuang Rui }
34820e5ca0d1SHuang Rui
psp_set_powergating_state(void * handle,enum amd_powergating_state state)34830e5ca0d1SHuang Rui static int psp_set_powergating_state(void *handle,
34840e5ca0d1SHuang Rui enum amd_powergating_state state)
34850e5ca0d1SHuang Rui {
34860e5ca0d1SHuang Rui return 0;
34870e5ca0d1SHuang Rui }
34880e5ca0d1SHuang Rui
psp_usbc_pd_fw_sysfs_read(struct device * dev,struct device_attribute * attr,char * buf)348957430471SAndrey Grodzovsky static ssize_t psp_usbc_pd_fw_sysfs_read(struct device *dev,
349057430471SAndrey Grodzovsky struct device_attribute *attr,
349157430471SAndrey Grodzovsky char *buf)
349257430471SAndrey Grodzovsky {
349357430471SAndrey Grodzovsky struct drm_device *ddev = dev_get_drvdata(dev);
34941348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(ddev);
349557430471SAndrey Grodzovsky uint32_t fw_ver;
349657430471SAndrey Grodzovsky int ret;
349757430471SAndrey Grodzovsky
349890f88cddSAndrey Grodzovsky if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
349990f88cddSAndrey Grodzovsky DRM_INFO("PSP block is not ready yet.");
350090f88cddSAndrey Grodzovsky return -EBUSY;
350190f88cddSAndrey Grodzovsky }
350290f88cddSAndrey Grodzovsky
350357430471SAndrey Grodzovsky mutex_lock(&adev->psp.mutex);
350457430471SAndrey Grodzovsky ret = psp_read_usbc_pd_fw(&adev->psp, &fw_ver);
350557430471SAndrey Grodzovsky mutex_unlock(&adev->psp.mutex);
350657430471SAndrey Grodzovsky
350757430471SAndrey Grodzovsky if (ret) {
350857430471SAndrey Grodzovsky DRM_ERROR("Failed to read USBC PD FW, err = %d", ret);
350957430471SAndrey Grodzovsky return ret;
351057430471SAndrey Grodzovsky }
351157430471SAndrey Grodzovsky
351236000c7aSTian Tao return sysfs_emit(buf, "%x\n", fw_ver);
351357430471SAndrey Grodzovsky }
351457430471SAndrey Grodzovsky
psp_usbc_pd_fw_sysfs_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)351557430471SAndrey Grodzovsky static ssize_t psp_usbc_pd_fw_sysfs_write(struct device *dev,
351657430471SAndrey Grodzovsky struct device_attribute *attr,
351757430471SAndrey Grodzovsky const char *buf,
351857430471SAndrey Grodzovsky size_t count)
351957430471SAndrey Grodzovsky {
352057430471SAndrey Grodzovsky struct drm_device *ddev = dev_get_drvdata(dev);
35211348969aSLuben Tuikov struct amdgpu_device *adev = drm_to_adev(ddev);
3522f89f8c6bSAndrey Grodzovsky int ret, idx;
352357430471SAndrey Grodzovsky char fw_name[100];
352457430471SAndrey Grodzovsky const struct firmware *usbc_pd_fw;
352525a3e8acSAndrey Grodzovsky struct amdgpu_bo *fw_buf_bo = NULL;
352625a3e8acSAndrey Grodzovsky uint64_t fw_pri_mc_addr;
352725a3e8acSAndrey Grodzovsky void *fw_pri_cpu_addr;
352857430471SAndrey Grodzovsky
352990f88cddSAndrey Grodzovsky if (!adev->ip_blocks[AMD_IP_BLOCK_TYPE_PSP].status.late_initialized) {
353090f88cddSAndrey Grodzovsky DRM_INFO("PSP block is not ready yet.");
353190f88cddSAndrey Grodzovsky return -EBUSY;
353290f88cddSAndrey Grodzovsky }
353357430471SAndrey Grodzovsky
3534f89f8c6bSAndrey Grodzovsky if (!drm_dev_enter(ddev, &idx))
3535f89f8c6bSAndrey Grodzovsky return -ENODEV;
3536f89f8c6bSAndrey Grodzovsky
353757430471SAndrey Grodzovsky snprintf(fw_name, sizeof(fw_name), "amdgpu/%s", buf);
353857430471SAndrey Grodzovsky ret = request_firmware(&usbc_pd_fw, fw_name, adev->dev);
353957430471SAndrey Grodzovsky if (ret)
354057430471SAndrey Grodzovsky goto fail;
354157430471SAndrey Grodzovsky
354225a3e8acSAndrey Grodzovsky /* LFB address which is aligned to 1MB boundary per PSP request */
354325a3e8acSAndrey Grodzovsky ret = amdgpu_bo_create_kernel(adev, usbc_pd_fw->size, 0x100000,
354458ab2c08SChristian König AMDGPU_GEM_DOMAIN_VRAM |
354558ab2c08SChristian König AMDGPU_GEM_DOMAIN_GTT,
354658ab2c08SChristian König &fw_buf_bo, &fw_pri_mc_addr,
354725a3e8acSAndrey Grodzovsky &fw_pri_cpu_addr);
354857430471SAndrey Grodzovsky if (ret)
354957430471SAndrey Grodzovsky goto rel_buf;
355057430471SAndrey Grodzovsky
355125a3e8acSAndrey Grodzovsky memcpy_toio(fw_pri_cpu_addr, usbc_pd_fw->data, usbc_pd_fw->size);
355257430471SAndrey Grodzovsky
355357430471SAndrey Grodzovsky mutex_lock(&adev->psp.mutex);
355425a3e8acSAndrey Grodzovsky ret = psp_load_usbc_pd_fw(&adev->psp, fw_pri_mc_addr);
355557430471SAndrey Grodzovsky mutex_unlock(&adev->psp.mutex);
355657430471SAndrey Grodzovsky
355725a3e8acSAndrey Grodzovsky amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
355825a3e8acSAndrey Grodzovsky
355957430471SAndrey Grodzovsky rel_buf:
356057430471SAndrey Grodzovsky release_firmware(usbc_pd_fw);
356157430471SAndrey Grodzovsky fail:
356257430471SAndrey Grodzovsky if (ret) {
356357430471SAndrey Grodzovsky DRM_ERROR("Failed to load USBC PD FW, err = %d", ret);
3564f89f8c6bSAndrey Grodzovsky count = ret;
356557430471SAndrey Grodzovsky }
356657430471SAndrey Grodzovsky
3567f89f8c6bSAndrey Grodzovsky drm_dev_exit(idx);
356857430471SAndrey Grodzovsky return count;
356957430471SAndrey Grodzovsky }
357057430471SAndrey Grodzovsky
psp_copy_fw(struct psp_context * psp,uint8_t * start_addr,uint32_t bin_size)3571f89f8c6bSAndrey Grodzovsky void psp_copy_fw(struct psp_context *psp, uint8_t *start_addr, uint32_t bin_size)
3572f89f8c6bSAndrey Grodzovsky {
3573f89f8c6bSAndrey Grodzovsky int idx;
3574f89f8c6bSAndrey Grodzovsky
3575c58a863bSGuchun Chen if (!drm_dev_enter(adev_to_drm(psp->adev), &idx))
3576f89f8c6bSAndrey Grodzovsky return;
3577f89f8c6bSAndrey Grodzovsky
3578f89f8c6bSAndrey Grodzovsky memset(psp->fw_pri_buf, 0, PSP_1_MEG);
3579f89f8c6bSAndrey Grodzovsky memcpy(psp->fw_pri_buf, start_addr, bin_size);
3580f89f8c6bSAndrey Grodzovsky
3581f89f8c6bSAndrey Grodzovsky drm_dev_exit(idx);
3582f89f8c6bSAndrey Grodzovsky }
3583f89f8c6bSAndrey Grodzovsky
3584649663afSMario Limonciello /**
3585649663afSMario Limonciello * DOC: usbc_pd_fw
3586649663afSMario Limonciello * Reading from this file will retrieve the USB-C PD firmware version. Writing to
3587649663afSMario Limonciello * this file will trigger the update process.
3588649663afSMario Limonciello */
3589f14c8c3eSSrinivasan Shanmugam static DEVICE_ATTR(usbc_pd_fw, 0644,
359057430471SAndrey Grodzovsky psp_usbc_pd_fw_sysfs_read,
359157430471SAndrey Grodzovsky psp_usbc_pd_fw_sysfs_write);
359257430471SAndrey Grodzovsky
is_psp_fw_valid(struct psp_bin_desc bin)3593222e0a71SCandice Li int is_psp_fw_valid(struct psp_bin_desc bin)
3594222e0a71SCandice Li {
3595222e0a71SCandice Li return bin.size_bytes;
3596222e0a71SCandice Li }
359757430471SAndrey Grodzovsky
amdgpu_psp_vbflash_write(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t pos,size_t count)35988424f2ccSLikun Gao static ssize_t amdgpu_psp_vbflash_write(struct file *filp, struct kobject *kobj,
35998424f2ccSLikun Gao struct bin_attribute *bin_attr,
36008424f2ccSLikun Gao char *buffer, loff_t pos, size_t count)
36018424f2ccSLikun Gao {
36028424f2ccSLikun Gao struct device *dev = kobj_to_dev(kobj);
36038424f2ccSLikun Gao struct drm_device *ddev = dev_get_drvdata(dev);
36048424f2ccSLikun Gao struct amdgpu_device *adev = drm_to_adev(ddev);
36058424f2ccSLikun Gao
3606dfc53681SLikun Gao adev->psp.vbflash_done = false;
3607dfc53681SLikun Gao
36088424f2ccSLikun Gao /* Safeguard against memory drain */
36098424f2ccSLikun Gao if (adev->psp.vbflash_image_size > AMD_VBIOS_FILE_MAX_SIZE_B) {
36108424f2ccSLikun Gao dev_err(adev->dev, "File size cannot exceed %u", AMD_VBIOS_FILE_MAX_SIZE_B);
36118424f2ccSLikun Gao kvfree(adev->psp.vbflash_tmp_buf);
36128424f2ccSLikun Gao adev->psp.vbflash_tmp_buf = NULL;
36138424f2ccSLikun Gao adev->psp.vbflash_image_size = 0;
36148424f2ccSLikun Gao return -ENOMEM;
36158424f2ccSLikun Gao }
36168424f2ccSLikun Gao
36178424f2ccSLikun Gao /* TODO Just allocate max for now and optimize to realloc later if needed */
36188424f2ccSLikun Gao if (!adev->psp.vbflash_tmp_buf) {
36198424f2ccSLikun Gao adev->psp.vbflash_tmp_buf = kvmalloc(AMD_VBIOS_FILE_MAX_SIZE_B, GFP_KERNEL);
36208424f2ccSLikun Gao if (!adev->psp.vbflash_tmp_buf)
36218424f2ccSLikun Gao return -ENOMEM;
36228424f2ccSLikun Gao }
36238424f2ccSLikun Gao
36248424f2ccSLikun Gao mutex_lock(&adev->psp.mutex);
36258424f2ccSLikun Gao memcpy(adev->psp.vbflash_tmp_buf + pos, buffer, count);
36268424f2ccSLikun Gao adev->psp.vbflash_image_size += count;
36278424f2ccSLikun Gao mutex_unlock(&adev->psp.mutex);
36288424f2ccSLikun Gao
36291cc506f0SMario Limonciello dev_dbg(adev->dev, "IFWI staged for update");
36308424f2ccSLikun Gao
36318424f2ccSLikun Gao return count;
36328424f2ccSLikun Gao }
36338424f2ccSLikun Gao
amdgpu_psp_vbflash_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buffer,loff_t pos,size_t count)36348424f2ccSLikun Gao static ssize_t amdgpu_psp_vbflash_read(struct file *filp, struct kobject *kobj,
36358424f2ccSLikun Gao struct bin_attribute *bin_attr, char *buffer,
36368424f2ccSLikun Gao loff_t pos, size_t count)
36378424f2ccSLikun Gao {
36388424f2ccSLikun Gao struct device *dev = kobj_to_dev(kobj);
36398424f2ccSLikun Gao struct drm_device *ddev = dev_get_drvdata(dev);
36408424f2ccSLikun Gao struct amdgpu_device *adev = drm_to_adev(ddev);
36418424f2ccSLikun Gao struct amdgpu_bo *fw_buf_bo = NULL;
36428424f2ccSLikun Gao uint64_t fw_pri_mc_addr;
36438424f2ccSLikun Gao void *fw_pri_cpu_addr;
36448424f2ccSLikun Gao int ret;
36458424f2ccSLikun Gao
36463537d6a4SMario Limonciello if (adev->psp.vbflash_image_size == 0)
36473537d6a4SMario Limonciello return -EINVAL;
36483537d6a4SMario Limonciello
36491cc506f0SMario Limonciello dev_dbg(adev->dev, "PSP IFWI flash process initiated");
36508424f2ccSLikun Gao
36518424f2ccSLikun Gao ret = amdgpu_bo_create_kernel(adev, adev->psp.vbflash_image_size,
36528424f2ccSLikun Gao AMDGPU_GPU_PAGE_SIZE,
36538424f2ccSLikun Gao AMDGPU_GEM_DOMAIN_VRAM,
36548424f2ccSLikun Gao &fw_buf_bo,
36558424f2ccSLikun Gao &fw_pri_mc_addr,
36568424f2ccSLikun Gao &fw_pri_cpu_addr);
36578424f2ccSLikun Gao if (ret)
36588424f2ccSLikun Gao goto rel_buf;
36598424f2ccSLikun Gao
36608424f2ccSLikun Gao memcpy_toio(fw_pri_cpu_addr, adev->psp.vbflash_tmp_buf, adev->psp.vbflash_image_size);
36618424f2ccSLikun Gao
36628424f2ccSLikun Gao mutex_lock(&adev->psp.mutex);
36638424f2ccSLikun Gao ret = psp_update_spirom(&adev->psp, fw_pri_mc_addr);
36648424f2ccSLikun Gao mutex_unlock(&adev->psp.mutex);
36658424f2ccSLikun Gao
36668424f2ccSLikun Gao amdgpu_bo_free_kernel(&fw_buf_bo, &fw_pri_mc_addr, &fw_pri_cpu_addr);
36678424f2ccSLikun Gao
36688424f2ccSLikun Gao rel_buf:
36698424f2ccSLikun Gao kvfree(adev->psp.vbflash_tmp_buf);
36708424f2ccSLikun Gao adev->psp.vbflash_tmp_buf = NULL;
36718424f2ccSLikun Gao adev->psp.vbflash_image_size = 0;
36728424f2ccSLikun Gao
36738424f2ccSLikun Gao if (ret) {
36741cc506f0SMario Limonciello dev_err(adev->dev, "Failed to load IFWI, err = %d", ret);
36758424f2ccSLikun Gao return ret;
36768424f2ccSLikun Gao }
36778424f2ccSLikun Gao
36781cc506f0SMario Limonciello dev_dbg(adev->dev, "PSP IFWI flash process done");
36798424f2ccSLikun Gao return 0;
36808424f2ccSLikun Gao }
36818424f2ccSLikun Gao
3682649663afSMario Limonciello /**
3683649663afSMario Limonciello * DOC: psp_vbflash
3684649663afSMario Limonciello * Writing to this file will stage an IFWI for update. Reading from this file
3685649663afSMario Limonciello * will trigger the update process.
3686649663afSMario Limonciello */
3687521289d2SMario Limonciello static struct bin_attribute psp_vbflash_bin_attr = {
3688521289d2SMario Limonciello .attr = {.name = "psp_vbflash", .mode = 0660},
3689521289d2SMario Limonciello .size = 0,
3690521289d2SMario Limonciello .write = amdgpu_psp_vbflash_write,
3691521289d2SMario Limonciello .read = amdgpu_psp_vbflash_read,
3692521289d2SMario Limonciello };
3693521289d2SMario Limonciello
3694649663afSMario Limonciello /**
3695649663afSMario Limonciello * DOC: psp_vbflash_status
3696649663afSMario Limonciello * The status of the flash process.
3697649663afSMario Limonciello * 0: IFWI flash not complete.
3698649663afSMario Limonciello * 1: IFWI flash complete.
3699649663afSMario Limonciello */
amdgpu_psp_vbflash_status(struct device * dev,struct device_attribute * attr,char * buf)3700dfc53681SLikun Gao static ssize_t amdgpu_psp_vbflash_status(struct device *dev,
3701dfc53681SLikun Gao struct device_attribute *attr,
3702dfc53681SLikun Gao char *buf)
3703dfc53681SLikun Gao {
3704dfc53681SLikun Gao struct drm_device *ddev = dev_get_drvdata(dev);
3705dfc53681SLikun Gao struct amdgpu_device *adev = drm_to_adev(ddev);
3706dfc53681SLikun Gao uint32_t vbflash_status;
3707dfc53681SLikun Gao
3708dfc53681SLikun Gao vbflash_status = psp_vbflash_status(&adev->psp);
3709dfc53681SLikun Gao if (!adev->psp.vbflash_done)
3710dfc53681SLikun Gao vbflash_status = 0;
3711dfc53681SLikun Gao else if (adev->psp.vbflash_done && !(vbflash_status & 0x80000000))
3712dfc53681SLikun Gao vbflash_status = 1;
3713dfc53681SLikun Gao
3714dfc53681SLikun Gao return sysfs_emit(buf, "0x%x\n", vbflash_status);
3715dfc53681SLikun Gao }
3716fe56c6eeSMario Limonciello static DEVICE_ATTR(psp_vbflash_status, 0440, amdgpu_psp_vbflash_status, NULL);
3717dfc53681SLikun Gao
3718e7347f1cSMario Limonciello static struct bin_attribute *bin_flash_attrs[] = {
3719e7347f1cSMario Limonciello &psp_vbflash_bin_attr,
3720e7347f1cSMario Limonciello NULL
3721e7347f1cSMario Limonciello };
3722e7347f1cSMario Limonciello
3723521289d2SMario Limonciello static struct attribute *flash_attrs[] = {
3724521289d2SMario Limonciello &dev_attr_psp_vbflash_status.attr,
372598d19a6cSMario Limonciello &dev_attr_usbc_pd_fw.attr,
3726521289d2SMario Limonciello NULL
3727521289d2SMario Limonciello };
3728521289d2SMario Limonciello
amdgpu_flash_attr_is_visible(struct kobject * kobj,struct attribute * attr,int idx)3729521289d2SMario Limonciello static umode_t amdgpu_flash_attr_is_visible(struct kobject *kobj, struct attribute *attr, int idx)
37308424f2ccSLikun Gao {
3731521289d2SMario Limonciello struct device *dev = kobj_to_dev(kobj);
3732521289d2SMario Limonciello struct drm_device *ddev = dev_get_drvdata(dev);
3733521289d2SMario Limonciello struct amdgpu_device *adev = drm_to_adev(ddev);
37348424f2ccSLikun Gao
3735e7347f1cSMario Limonciello if (attr == &dev_attr_usbc_pd_fw.attr)
3736e7347f1cSMario Limonciello return adev->psp.sup_pd_fw_up ? 0660 : 0;
37378424f2ccSLikun Gao
3738e7347f1cSMario Limonciello return adev->psp.sup_ifwi_up ? 0440 : 0;
37398424f2ccSLikun Gao }
3740e7347f1cSMario Limonciello
amdgpu_bin_flash_attr_is_visible(struct kobject * kobj,struct bin_attribute * attr,int idx)3741e7347f1cSMario Limonciello static umode_t amdgpu_bin_flash_attr_is_visible(struct kobject *kobj,
3742e7347f1cSMario Limonciello struct bin_attribute *attr,
3743e7347f1cSMario Limonciello int idx)
3744e7347f1cSMario Limonciello {
3745e7347f1cSMario Limonciello struct device *dev = kobj_to_dev(kobj);
3746e7347f1cSMario Limonciello struct drm_device *ddev = dev_get_drvdata(dev);
3747e7347f1cSMario Limonciello struct amdgpu_device *adev = drm_to_adev(ddev);
3748e7347f1cSMario Limonciello
3749e7347f1cSMario Limonciello return adev->psp.sup_ifwi_up ? 0660 : 0;
37508424f2ccSLikun Gao }
37518424f2ccSLikun Gao
3752521289d2SMario Limonciello const struct attribute_group amdgpu_flash_attr_group = {
3753521289d2SMario Limonciello .attrs = flash_attrs,
3754e7347f1cSMario Limonciello .bin_attrs = bin_flash_attrs,
3755e7347f1cSMario Limonciello .is_bin_visible = amdgpu_bin_flash_attr_is_visible,
3756521289d2SMario Limonciello .is_visible = amdgpu_flash_attr_is_visible,
3757521289d2SMario Limonciello };
3758521289d2SMario Limonciello
37590e5ca0d1SHuang Rui const struct amd_ip_funcs psp_ip_funcs = {
37600e5ca0d1SHuang Rui .name = "psp",
37610e5ca0d1SHuang Rui .early_init = psp_early_init,
376290f88cddSAndrey Grodzovsky .late_init = NULL,
37630e5ca0d1SHuang Rui .sw_init = psp_sw_init,
37640e5ca0d1SHuang Rui .sw_fini = psp_sw_fini,
37650e5ca0d1SHuang Rui .hw_init = psp_hw_init,
37660e5ca0d1SHuang Rui .hw_fini = psp_hw_fini,
37670e5ca0d1SHuang Rui .suspend = psp_suspend,
37680e5ca0d1SHuang Rui .resume = psp_resume,
37690e5ca0d1SHuang Rui .is_idle = NULL,
3770f75a9a5dSAlex Deucher .check_soft_reset = NULL,
37710e5ca0d1SHuang Rui .wait_for_idle = NULL,
3772f75a9a5dSAlex Deucher .soft_reset = NULL,
37730e5ca0d1SHuang Rui .set_clockgating_state = psp_set_clockgating_state,
37740e5ca0d1SHuang Rui .set_powergating_state = psp_set_powergating_state,
37750e5ca0d1SHuang Rui };
37760e5ca0d1SHuang Rui
3777f14c8c3eSSrinivasan Shanmugam const struct amdgpu_ip_block_version psp_v3_1_ip_block = {
37780e5ca0d1SHuang Rui .type = AMD_IP_BLOCK_TYPE_PSP,
37790e5ca0d1SHuang Rui .major = 3,
37800e5ca0d1SHuang Rui .minor = 1,
37810e5ca0d1SHuang Rui .rev = 0,
37820e5ca0d1SHuang Rui .funcs = &psp_ip_funcs,
37830e5ca0d1SHuang Rui };
3784dfbd6438SHuang Rui
3785f14c8c3eSSrinivasan Shanmugam const struct amdgpu_ip_block_version psp_v10_0_ip_block = {
3786dfbd6438SHuang Rui .type = AMD_IP_BLOCK_TYPE_PSP,
3787dfbd6438SHuang Rui .major = 10,
3788dfbd6438SHuang Rui .minor = 0,
3789dfbd6438SHuang Rui .rev = 0,
3790dfbd6438SHuang Rui .funcs = &psp_ip_funcs,
3791dfbd6438SHuang Rui };
3792654f761cSFeifei Xu
3793f14c8c3eSSrinivasan Shanmugam const struct amdgpu_ip_block_version psp_v11_0_ip_block = {
3794654f761cSFeifei Xu .type = AMD_IP_BLOCK_TYPE_PSP,
3795654f761cSFeifei Xu .major = 11,
3796654f761cSFeifei Xu .minor = 0,
3797654f761cSFeifei Xu .rev = 0,
3798654f761cSFeifei Xu .funcs = &psp_ip_funcs,
3799654f761cSFeifei Xu };
38006a7a0bdbSAaron Liu
38011c7916afSLang Yu const struct amdgpu_ip_block_version psp_v11_0_8_ip_block = {
38021c7916afSLang Yu .type = AMD_IP_BLOCK_TYPE_PSP,
38031c7916afSLang Yu .major = 11,
38041c7916afSLang Yu .minor = 0,
38051c7916afSLang Yu .rev = 8,
38061c7916afSLang Yu .funcs = &psp_ip_funcs,
38071c7916afSLang Yu };
38081c7916afSLang Yu
3809f14c8c3eSSrinivasan Shanmugam const struct amdgpu_ip_block_version psp_v12_0_ip_block = {
38106a7a0bdbSAaron Liu .type = AMD_IP_BLOCK_TYPE_PSP,
38116a7a0bdbSAaron Liu .major = 12,
38126a7a0bdbSAaron Liu .minor = 0,
38136a7a0bdbSAaron Liu .rev = 0,
38146a7a0bdbSAaron Liu .funcs = &psp_ip_funcs,
38156a7a0bdbSAaron Liu };
38169fbd96a1SHawking Zhang
38179fbd96a1SHawking Zhang const struct amdgpu_ip_block_version psp_v13_0_ip_block = {
38189fbd96a1SHawking Zhang .type = AMD_IP_BLOCK_TYPE_PSP,
38199fbd96a1SHawking Zhang .major = 13,
38209fbd96a1SHawking Zhang .minor = 0,
38219fbd96a1SHawking Zhang .rev = 0,
38229fbd96a1SHawking Zhang .funcs = &psp_ip_funcs,
38239fbd96a1SHawking Zhang };
38247e8a3ca9SXiaojian Du
38257e8a3ca9SXiaojian Du const struct amdgpu_ip_block_version psp_v13_0_4_ip_block = {
38267e8a3ca9SXiaojian Du .type = AMD_IP_BLOCK_TYPE_PSP,
38277e8a3ca9SXiaojian Du .major = 13,
38287e8a3ca9SXiaojian Du .minor = 0,
38297e8a3ca9SXiaojian Du .rev = 4,
38307e8a3ca9SXiaojian Du .funcs = &psp_ip_funcs,
38317e8a3ca9SXiaojian Du };
3832