xref: /dragonfly/sys/dev/drm/amd/amdgpu/amdgpu_psp.c (revision 78973132)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2016 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Author: Huang Rui
23b843c749SSergey Zigachev  *
24b843c749SSergey Zigachev  */
25b843c749SSergey Zigachev 
26b843c749SSergey Zigachev #include <linux/firmware.h>
27b843c749SSergey Zigachev #include <drm/drmP.h>
28b843c749SSergey Zigachev #include "amdgpu.h"
29b843c749SSergey Zigachev #include "amdgpu_psp.h"
30b843c749SSergey Zigachev #include "amdgpu_ucode.h"
31b843c749SSergey Zigachev #include "soc15_common.h"
32b843c749SSergey Zigachev #include "psp_v3_1.h"
33b843c749SSergey Zigachev #include "psp_v10_0.h"
34b843c749SSergey Zigachev 
35b843c749SSergey Zigachev static void psp_set_funcs(struct amdgpu_device *adev);
36b843c749SSergey Zigachev 
psp_early_init(void * handle)37b843c749SSergey Zigachev static int psp_early_init(void *handle)
38b843c749SSergey Zigachev {
39b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
40b843c749SSergey Zigachev 	struct psp_context *psp = &adev->psp;
41b843c749SSergey Zigachev 
42b843c749SSergey Zigachev 	psp_set_funcs(adev);
43b843c749SSergey Zigachev 
44b843c749SSergey Zigachev 	switch (adev->asic_type) {
45b843c749SSergey Zigachev 	case CHIP_VEGA10:
46b843c749SSergey Zigachev 	case CHIP_VEGA12:
47b843c749SSergey Zigachev 	case CHIP_VEGA20:
48b843c749SSergey Zigachev 		psp_v3_1_set_psp_funcs(psp);
49b843c749SSergey Zigachev 		break;
50b843c749SSergey Zigachev 	case CHIP_RAVEN:
51b843c749SSergey Zigachev 		psp_v10_0_set_psp_funcs(psp);
52b843c749SSergey Zigachev 		break;
53b843c749SSergey Zigachev 	default:
54b843c749SSergey Zigachev 		return -EINVAL;
55b843c749SSergey Zigachev 	}
56b843c749SSergey Zigachev 
57b843c749SSergey Zigachev 	psp->adev = adev;
58b843c749SSergey Zigachev 
59b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
60b843c749SSergey Zigachev 		return 0;
61b843c749SSergey Zigachev 
62b843c749SSergey Zigachev 	return 0;
63b843c749SSergey Zigachev }
64b843c749SSergey Zigachev 
psp_sw_init(void * handle)65b843c749SSergey Zigachev static int psp_sw_init(void *handle)
66b843c749SSergey Zigachev {
67b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
68b843c749SSergey Zigachev 	struct psp_context *psp = &adev->psp;
69b843c749SSergey Zigachev 	int ret;
70b843c749SSergey Zigachev 
71b843c749SSergey Zigachev 	ret = psp_init_microcode(psp);
72b843c749SSergey Zigachev 	if (ret) {
73b843c749SSergey Zigachev 		DRM_ERROR("Failed to load psp firmware!\n");
74b843c749SSergey Zigachev 		return ret;
75b843c749SSergey Zigachev 	}
76b843c749SSergey Zigachev 
77b843c749SSergey Zigachev 	return 0;
78b843c749SSergey Zigachev }
79b843c749SSergey Zigachev 
psp_sw_fini(void * handle)80b843c749SSergey Zigachev static int psp_sw_fini(void *handle)
81b843c749SSergey Zigachev {
82b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
83b843c749SSergey Zigachev 
84b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
85b843c749SSergey Zigachev 		return 0;
86b843c749SSergey Zigachev 
87b843c749SSergey Zigachev 	release_firmware(adev->psp.sos_fw);
88b843c749SSergey Zigachev 	adev->psp.sos_fw = NULL;
89b843c749SSergey Zigachev 	release_firmware(adev->psp.asd_fw);
90b843c749SSergey Zigachev 	adev->psp.asd_fw = NULL;
91b843c749SSergey Zigachev 	return 0;
92b843c749SSergey Zigachev }
93b843c749SSergey Zigachev 
psp_wait_for(struct psp_context * psp,uint32_t reg_index,uint32_t reg_val,uint32_t mask,bool check_changed)94b843c749SSergey Zigachev int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
95b843c749SSergey Zigachev 		 uint32_t reg_val, uint32_t mask, bool check_changed)
96b843c749SSergey Zigachev {
97b843c749SSergey Zigachev 	uint32_t val;
98b843c749SSergey Zigachev 	int i;
99b843c749SSergey Zigachev 	struct amdgpu_device *adev = psp->adev;
100b843c749SSergey Zigachev 
101b843c749SSergey Zigachev 	for (i = 0; i < adev->usec_timeout; i++) {
102b843c749SSergey Zigachev 		val = RREG32(reg_index);
103b843c749SSergey Zigachev 		if (check_changed) {
104b843c749SSergey Zigachev 			if (val != reg_val)
105b843c749SSergey Zigachev 				return 0;
106b843c749SSergey Zigachev 		} else {
107b843c749SSergey Zigachev 			if ((val & mask) == reg_val)
108b843c749SSergey Zigachev 				return 0;
109b843c749SSergey Zigachev 		}
110b843c749SSergey Zigachev 		udelay(1);
111b843c749SSergey Zigachev 	}
112b843c749SSergey Zigachev 
113b843c749SSergey Zigachev 	return -ETIME;
114b843c749SSergey Zigachev }
115b843c749SSergey Zigachev 
116b843c749SSergey Zigachev 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,int index)117b843c749SSergey Zigachev psp_cmd_submit_buf(struct psp_context *psp,
118b843c749SSergey Zigachev 		   struct amdgpu_firmware_info *ucode,
119b843c749SSergey Zigachev 		   struct psp_gfx_cmd_resp *cmd, uint64_t fence_mc_addr,
120b843c749SSergey Zigachev 		   int index)
121b843c749SSergey Zigachev {
122b843c749SSergey Zigachev 	int ret;
123b843c749SSergey Zigachev 
124b843c749SSergey Zigachev 	memset(psp->cmd_buf_mem, 0, PSP_CMD_BUFFER_SIZE);
125b843c749SSergey Zigachev 
126b843c749SSergey Zigachev 	memcpy(psp->cmd_buf_mem, cmd, sizeof(struct psp_gfx_cmd_resp));
127b843c749SSergey Zigachev 
128b843c749SSergey Zigachev 	ret = psp_cmd_submit(psp, ucode, psp->cmd_buf_mc_addr,
129b843c749SSergey Zigachev 			     fence_mc_addr, index);
130b843c749SSergey Zigachev 
131b843c749SSergey Zigachev 	while (*((unsigned int *)psp->fence_buf) != index) {
132b843c749SSergey Zigachev 		msleep(1);
133b843c749SSergey Zigachev 	}
134b843c749SSergey Zigachev 
135b843c749SSergey Zigachev 	if (ucode) {
136b843c749SSergey Zigachev 		ucode->tmr_mc_addr_lo = psp->cmd_buf_mem->resp.fw_addr_lo;
137b843c749SSergey Zigachev 		ucode->tmr_mc_addr_hi = psp->cmd_buf_mem->resp.fw_addr_hi;
138b843c749SSergey Zigachev 	}
139b843c749SSergey Zigachev 
140b843c749SSergey Zigachev 	return ret;
141b843c749SSergey Zigachev }
142b843c749SSergey Zigachev 
psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp * cmd,uint64_t tmr_mc,uint32_t size)143b843c749SSergey Zigachev static void psp_prep_tmr_cmd_buf(struct psp_gfx_cmd_resp *cmd,
144b843c749SSergey Zigachev 				 uint64_t tmr_mc, uint32_t size)
145b843c749SSergey Zigachev {
146b843c749SSergey Zigachev 	cmd->cmd_id = GFX_CMD_ID_SETUP_TMR;
147b843c749SSergey Zigachev 	cmd->cmd.cmd_setup_tmr.buf_phy_addr_lo = lower_32_bits(tmr_mc);
148b843c749SSergey Zigachev 	cmd->cmd.cmd_setup_tmr.buf_phy_addr_hi = upper_32_bits(tmr_mc);
149b843c749SSergey Zigachev 	cmd->cmd.cmd_setup_tmr.buf_size = size;
150b843c749SSergey Zigachev }
151b843c749SSergey Zigachev 
152b843c749SSergey Zigachev /* Set up Trusted Memory Region */
psp_tmr_init(struct psp_context * psp)153b843c749SSergey Zigachev static int psp_tmr_init(struct psp_context *psp)
154b843c749SSergey Zigachev {
155b843c749SSergey Zigachev 	int ret;
156b843c749SSergey Zigachev 
157b843c749SSergey Zigachev 	/*
158b843c749SSergey Zigachev 	 * Allocate 3M memory aligned to 1M from Frame Buffer (local
159b843c749SSergey Zigachev 	 * physical).
160b843c749SSergey Zigachev 	 *
161b843c749SSergey Zigachev 	 * Note: this memory need be reserved till the driver
162b843c749SSergey Zigachev 	 * uninitializes.
163b843c749SSergey Zigachev 	 */
164b843c749SSergey Zigachev 	ret = amdgpu_bo_create_kernel(psp->adev, 0x300000, 0x100000,
165b843c749SSergey Zigachev 				      AMDGPU_GEM_DOMAIN_VRAM,
166*78973132SSergey Zigachev 				      &psp->tmr_bo, (u64 *)&psp->tmr_mc_addr, &psp->tmr_buf);
167b843c749SSergey Zigachev 
168b843c749SSergey Zigachev 	return ret;
169b843c749SSergey Zigachev }
170b843c749SSergey Zigachev 
psp_tmr_load(struct psp_context * psp)171b843c749SSergey Zigachev static int psp_tmr_load(struct psp_context *psp)
172b843c749SSergey Zigachev {
173b843c749SSergey Zigachev 	int ret;
174b843c749SSergey Zigachev 	struct psp_gfx_cmd_resp *cmd;
175b843c749SSergey Zigachev 
176b843c749SSergey Zigachev 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
177b843c749SSergey Zigachev 	if (!cmd)
178b843c749SSergey Zigachev 		return -ENOMEM;
179b843c749SSergey Zigachev 
180b843c749SSergey Zigachev 	psp_prep_tmr_cmd_buf(cmd, psp->tmr_mc_addr, 0x300000);
181b843c749SSergey Zigachev 
182b843c749SSergey Zigachev 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
183b843c749SSergey Zigachev 				 psp->fence_buf_mc_addr, 1);
184b843c749SSergey Zigachev 	if (ret)
185b843c749SSergey Zigachev 		goto failed;
186b843c749SSergey Zigachev 
187b843c749SSergey Zigachev 	kfree(cmd);
188b843c749SSergey Zigachev 
189b843c749SSergey Zigachev 	return 0;
190b843c749SSergey Zigachev 
191b843c749SSergey Zigachev failed:
192b843c749SSergey Zigachev 	kfree(cmd);
193b843c749SSergey Zigachev 	return ret;
194b843c749SSergey Zigachev }
195b843c749SSergey Zigachev 
psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp * cmd,uint64_t asd_mc,uint64_t asd_mc_shared,uint32_t size,uint32_t shared_size)196b843c749SSergey Zigachev static void psp_prep_asd_cmd_buf(struct psp_gfx_cmd_resp *cmd,
197b843c749SSergey Zigachev 				 uint64_t asd_mc, uint64_t asd_mc_shared,
198b843c749SSergey Zigachev 				 uint32_t size, uint32_t shared_size)
199b843c749SSergey Zigachev {
200b843c749SSergey Zigachev 	cmd->cmd_id = GFX_CMD_ID_LOAD_ASD;
201b843c749SSergey Zigachev 	cmd->cmd.cmd_load_ta.app_phy_addr_lo = lower_32_bits(asd_mc);
202b843c749SSergey Zigachev 	cmd->cmd.cmd_load_ta.app_phy_addr_hi = upper_32_bits(asd_mc);
203b843c749SSergey Zigachev 	cmd->cmd.cmd_load_ta.app_len = size;
204b843c749SSergey Zigachev 
205b843c749SSergey Zigachev 	cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_lo = lower_32_bits(asd_mc_shared);
206b843c749SSergey Zigachev 	cmd->cmd.cmd_load_ta.cmd_buf_phy_addr_hi = upper_32_bits(asd_mc_shared);
207b843c749SSergey Zigachev 	cmd->cmd.cmd_load_ta.cmd_buf_len = shared_size;
208b843c749SSergey Zigachev }
209b843c749SSergey Zigachev 
psp_asd_init(struct psp_context * psp)210b843c749SSergey Zigachev static int psp_asd_init(struct psp_context *psp)
211b843c749SSergey Zigachev {
212b843c749SSergey Zigachev 	int ret;
213b843c749SSergey Zigachev 
214b843c749SSergey Zigachev 	/*
215b843c749SSergey Zigachev 	 * Allocate 16k memory aligned to 4k from Frame Buffer (local
216b843c749SSergey Zigachev 	 * physical) for shared ASD <-> Driver
217b843c749SSergey Zigachev 	 */
218b843c749SSergey Zigachev 	ret = amdgpu_bo_create_kernel(psp->adev, PSP_ASD_SHARED_MEM_SIZE,
219b843c749SSergey Zigachev 				      PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
220b843c749SSergey Zigachev 				      &psp->asd_shared_bo,
221*78973132SSergey Zigachev 				      (u64 *)&psp->asd_shared_mc_addr,
222b843c749SSergey Zigachev 				      &psp->asd_shared_buf);
223b843c749SSergey Zigachev 
224b843c749SSergey Zigachev 	return ret;
225b843c749SSergey Zigachev }
226b843c749SSergey Zigachev 
psp_asd_load(struct psp_context * psp)227b843c749SSergey Zigachev static int psp_asd_load(struct psp_context *psp)
228b843c749SSergey Zigachev {
229b843c749SSergey Zigachev 	int ret;
230b843c749SSergey Zigachev 	struct psp_gfx_cmd_resp *cmd;
231b843c749SSergey Zigachev 
232b843c749SSergey Zigachev 	/* If PSP version doesn't match ASD version, asd loading will be failed.
233b843c749SSergey Zigachev 	 * add workaround to bypass it for sriov now.
234b843c749SSergey Zigachev 	 * TODO: add version check to make it common
235b843c749SSergey Zigachev 	 */
236b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(psp->adev))
237b843c749SSergey Zigachev 		return 0;
238b843c749SSergey Zigachev 
239b843c749SSergey Zigachev 	cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
240b843c749SSergey Zigachev 	if (!cmd)
241b843c749SSergey Zigachev 		return -ENOMEM;
242b843c749SSergey Zigachev 
243b843c749SSergey Zigachev 	memset(psp->fw_pri_buf, 0, PSP_1_MEG);
244b843c749SSergey Zigachev 	memcpy(psp->fw_pri_buf, psp->asd_start_addr, psp->asd_ucode_size);
245b843c749SSergey Zigachev 
246b843c749SSergey Zigachev 	psp_prep_asd_cmd_buf(cmd, psp->fw_pri_mc_addr, psp->asd_shared_mc_addr,
247b843c749SSergey Zigachev 			     psp->asd_ucode_size, PSP_ASD_SHARED_MEM_SIZE);
248b843c749SSergey Zigachev 
249b843c749SSergey Zigachev 	ret = psp_cmd_submit_buf(psp, NULL, cmd,
250b843c749SSergey Zigachev 				 psp->fence_buf_mc_addr, 2);
251b843c749SSergey Zigachev 
252b843c749SSergey Zigachev 	kfree(cmd);
253b843c749SSergey Zigachev 
254b843c749SSergey Zigachev 	return ret;
255b843c749SSergey Zigachev }
256b843c749SSergey Zigachev 
psp_hw_start(struct psp_context * psp)257b843c749SSergey Zigachev static int psp_hw_start(struct psp_context *psp)
258b843c749SSergey Zigachev {
259b843c749SSergey Zigachev 	struct amdgpu_device *adev = psp->adev;
260b843c749SSergey Zigachev 	int ret;
261b843c749SSergey Zigachev 
262b843c749SSergey Zigachev 	if (!amdgpu_sriov_vf(adev) || !adev->in_gpu_reset) {
263b843c749SSergey Zigachev 		ret = psp_bootloader_load_sysdrv(psp);
264b843c749SSergey Zigachev 		if (ret)
265b843c749SSergey Zigachev 			return ret;
266b843c749SSergey Zigachev 
267b843c749SSergey Zigachev 		ret = psp_bootloader_load_sos(psp);
268b843c749SSergey Zigachev 		if (ret)
269b843c749SSergey Zigachev 			return ret;
270b843c749SSergey Zigachev 	}
271b843c749SSergey Zigachev 
272b843c749SSergey Zigachev 	ret = psp_ring_create(psp, PSP_RING_TYPE__KM);
273b843c749SSergey Zigachev 	if (ret)
274b843c749SSergey Zigachev 		return ret;
275b843c749SSergey Zigachev 
276b843c749SSergey Zigachev 	ret = psp_tmr_load(psp);
277b843c749SSergey Zigachev 	if (ret)
278b843c749SSergey Zigachev 		return ret;
279b843c749SSergey Zigachev 
280b843c749SSergey Zigachev 	ret = psp_asd_load(psp);
281b843c749SSergey Zigachev 	if (ret)
282b843c749SSergey Zigachev 		return ret;
283b843c749SSergey Zigachev 
284b843c749SSergey Zigachev 	return 0;
285b843c749SSergey Zigachev }
286b843c749SSergey Zigachev 
psp_np_fw_load(struct psp_context * psp)287b843c749SSergey Zigachev static int psp_np_fw_load(struct psp_context *psp)
288b843c749SSergey Zigachev {
289b843c749SSergey Zigachev 	int i, ret;
290b843c749SSergey Zigachev 	struct amdgpu_firmware_info *ucode;
291b843c749SSergey Zigachev 	struct amdgpu_device* adev = psp->adev;
292b843c749SSergey Zigachev 
293b843c749SSergey Zigachev 	for (i = 0; i < adev->firmware.max_ucodes; i++) {
294b843c749SSergey Zigachev 		ucode = &adev->firmware.ucode[i];
295b843c749SSergey Zigachev 		if (!ucode->fw)
296b843c749SSergey Zigachev 			continue;
297b843c749SSergey Zigachev 
298b843c749SSergey Zigachev 		if (ucode->ucode_id == AMDGPU_UCODE_ID_SMC &&
299b843c749SSergey Zigachev 		    psp_smu_reload_quirk(psp))
300b843c749SSergey Zigachev 			continue;
301b843c749SSergey Zigachev 		if (amdgpu_sriov_vf(adev) &&
302b843c749SSergey Zigachev 		   (ucode->ucode_id == AMDGPU_UCODE_ID_SDMA0
303b843c749SSergey Zigachev 		    || ucode->ucode_id == AMDGPU_UCODE_ID_SDMA1
304b843c749SSergey Zigachev 		    || ucode->ucode_id == AMDGPU_UCODE_ID_RLC_G))
305b843c749SSergey Zigachev 			/*skip ucode loading in SRIOV VF */
306b843c749SSergey Zigachev 			continue;
307b843c749SSergey Zigachev 
308b843c749SSergey Zigachev 		ret = psp_prep_cmd_buf(ucode, psp->cmd);
309b843c749SSergey Zigachev 		if (ret)
310b843c749SSergey Zigachev 			return ret;
311b843c749SSergey Zigachev 
312b843c749SSergey Zigachev 		ret = psp_cmd_submit_buf(psp, ucode, psp->cmd,
313b843c749SSergey Zigachev 					 psp->fence_buf_mc_addr, i + 3);
314b843c749SSergey Zigachev 		if (ret)
315b843c749SSergey Zigachev 			return ret;
316b843c749SSergey Zigachev 
317b843c749SSergey Zigachev #if 0
318b843c749SSergey Zigachev 		/* check if firmware loaded sucessfully */
319b843c749SSergey Zigachev 		if (!amdgpu_psp_check_fw_loading_status(adev, i))
320b843c749SSergey Zigachev 			return -EINVAL;
321b843c749SSergey Zigachev #endif
322b843c749SSergey Zigachev 	}
323b843c749SSergey Zigachev 
324b843c749SSergey Zigachev 	return 0;
325b843c749SSergey Zigachev }
326b843c749SSergey Zigachev 
psp_load_fw(struct amdgpu_device * adev)327b843c749SSergey Zigachev static int psp_load_fw(struct amdgpu_device *adev)
328b843c749SSergey Zigachev {
329b843c749SSergey Zigachev 	int ret;
330b843c749SSergey Zigachev 	struct psp_context *psp = &adev->psp;
331b843c749SSergey Zigachev 
332b843c749SSergey Zigachev 	if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset != 0)
333b843c749SSergey Zigachev 		goto skip_memalloc;
334b843c749SSergey Zigachev 
335b843c749SSergey Zigachev 	psp->cmd = kzalloc(sizeof(struct psp_gfx_cmd_resp), GFP_KERNEL);
336b843c749SSergey Zigachev 	if (!psp->cmd)
337b843c749SSergey Zigachev 		return -ENOMEM;
338b843c749SSergey Zigachev 
339b843c749SSergey Zigachev 	ret = amdgpu_bo_create_kernel(adev, PSP_1_MEG, PSP_1_MEG,
340b843c749SSergey Zigachev 					AMDGPU_GEM_DOMAIN_GTT,
341b843c749SSergey Zigachev 					&psp->fw_pri_bo,
342*78973132SSergey Zigachev 					(u64 *)&psp->fw_pri_mc_addr,
343b843c749SSergey Zigachev 					&psp->fw_pri_buf);
344b843c749SSergey Zigachev 	if (ret)
345b843c749SSergey Zigachev 		goto failed;
346b843c749SSergey Zigachev 
347b843c749SSergey Zigachev 	ret = amdgpu_bo_create_kernel(adev, PSP_FENCE_BUFFER_SIZE, PAGE_SIZE,
348b843c749SSergey Zigachev 					AMDGPU_GEM_DOMAIN_VRAM,
349b843c749SSergey Zigachev 					&psp->fence_buf_bo,
350*78973132SSergey Zigachev 					(u64 *)&psp->fence_buf_mc_addr,
351b843c749SSergey Zigachev 					&psp->fence_buf);
352b843c749SSergey Zigachev 	if (ret)
353b843c749SSergey Zigachev 		goto failed_mem2;
354b843c749SSergey Zigachev 
355b843c749SSergey Zigachev 	ret = amdgpu_bo_create_kernel(adev, PSP_CMD_BUFFER_SIZE, PAGE_SIZE,
356b843c749SSergey Zigachev 				      AMDGPU_GEM_DOMAIN_VRAM,
357*78973132SSergey Zigachev 				      &psp->cmd_buf_bo, (u64 *)&psp->cmd_buf_mc_addr,
358b843c749SSergey Zigachev 				      (void **)&psp->cmd_buf_mem);
359b843c749SSergey Zigachev 	if (ret)
360b843c749SSergey Zigachev 		goto failed_mem1;
361b843c749SSergey Zigachev 
362b843c749SSergey Zigachev 	memset(psp->fence_buf, 0, PSP_FENCE_BUFFER_SIZE);
363b843c749SSergey Zigachev 
364b843c749SSergey Zigachev 	ret = psp_ring_init(psp, PSP_RING_TYPE__KM);
365b843c749SSergey Zigachev 	if (ret)
366b843c749SSergey Zigachev 		goto failed_mem;
367b843c749SSergey Zigachev 
368b843c749SSergey Zigachev 	ret = psp_tmr_init(psp);
369b843c749SSergey Zigachev 	if (ret)
370b843c749SSergey Zigachev 		goto failed_mem;
371b843c749SSergey Zigachev 
372b843c749SSergey Zigachev 	ret = psp_asd_init(psp);
373b843c749SSergey Zigachev 	if (ret)
374b843c749SSergey Zigachev 		goto failed_mem;
375b843c749SSergey Zigachev 
376b843c749SSergey Zigachev skip_memalloc:
377b843c749SSergey Zigachev 	ret = psp_hw_start(psp);
378b843c749SSergey Zigachev 	if (ret)
379b843c749SSergey Zigachev 		goto failed_mem;
380b843c749SSergey Zigachev 
381b843c749SSergey Zigachev 	ret = psp_np_fw_load(psp);
382b843c749SSergey Zigachev 	if (ret)
383b843c749SSergey Zigachev 		goto failed_mem;
384b843c749SSergey Zigachev 
385b843c749SSergey Zigachev 	return 0;
386b843c749SSergey Zigachev 
387b843c749SSergey Zigachev failed_mem:
388b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&psp->cmd_buf_bo,
389*78973132SSergey Zigachev 			      (u64 *)&psp->cmd_buf_mc_addr,
390b843c749SSergey Zigachev 			      (void **)&psp->cmd_buf_mem);
391b843c749SSergey Zigachev failed_mem1:
392b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
393*78973132SSergey Zigachev 			      (u64 *)&psp->fence_buf_mc_addr, &psp->fence_buf);
394b843c749SSergey Zigachev failed_mem2:
395b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
396*78973132SSergey Zigachev 			      (u64 *)&psp->fw_pri_mc_addr, &psp->fw_pri_buf);
397b843c749SSergey Zigachev failed:
398b843c749SSergey Zigachev 	kfree(psp->cmd);
399b843c749SSergey Zigachev 	psp->cmd = NULL;
400b843c749SSergey Zigachev 	return ret;
401b843c749SSergey Zigachev }
402b843c749SSergey Zigachev 
psp_hw_init(void * handle)403b843c749SSergey Zigachev static int psp_hw_init(void *handle)
404b843c749SSergey Zigachev {
405b843c749SSergey Zigachev 	int ret;
406b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
407b843c749SSergey Zigachev 
408b843c749SSergey Zigachev 
409b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
410b843c749SSergey Zigachev 		return 0;
411b843c749SSergey Zigachev 
412b843c749SSergey Zigachev 	mutex_lock(&adev->firmware.mutex);
413b843c749SSergey Zigachev 	/*
414b843c749SSergey Zigachev 	 * This sequence is just used on hw_init only once, no need on
415b843c749SSergey Zigachev 	 * resume.
416b843c749SSergey Zigachev 	 */
417b843c749SSergey Zigachev 	ret = amdgpu_ucode_init_bo(adev);
418b843c749SSergey Zigachev 	if (ret)
419b843c749SSergey Zigachev 		goto failed;
420b843c749SSergey Zigachev 
421b843c749SSergey Zigachev 	ret = psp_load_fw(adev);
422b843c749SSergey Zigachev 	if (ret) {
423b843c749SSergey Zigachev 		DRM_ERROR("PSP firmware loading failed\n");
424b843c749SSergey Zigachev 		goto failed;
425b843c749SSergey Zigachev 	}
426b843c749SSergey Zigachev 
427b843c749SSergey Zigachev 	mutex_unlock(&adev->firmware.mutex);
428b843c749SSergey Zigachev 	return 0;
429b843c749SSergey Zigachev 
430b843c749SSergey Zigachev failed:
431b843c749SSergey Zigachev 	adev->firmware.load_type = AMDGPU_FW_LOAD_DIRECT;
432b843c749SSergey Zigachev 	mutex_unlock(&adev->firmware.mutex);
433b843c749SSergey Zigachev 	return -EINVAL;
434b843c749SSergey Zigachev }
435b843c749SSergey Zigachev 
psp_hw_fini(void * handle)436b843c749SSergey Zigachev static int psp_hw_fini(void *handle)
437b843c749SSergey Zigachev {
438b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
439b843c749SSergey Zigachev 	struct psp_context *psp = &adev->psp;
440b843c749SSergey Zigachev 
441b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
442b843c749SSergey Zigachev 		return 0;
443b843c749SSergey Zigachev 
444b843c749SSergey Zigachev 	amdgpu_ucode_fini_bo(adev);
445b843c749SSergey Zigachev 
446b843c749SSergey Zigachev 	psp_ring_destroy(psp, PSP_RING_TYPE__KM);
447b843c749SSergey Zigachev 
448*78973132SSergey Zigachev 	amdgpu_bo_free_kernel(&psp->tmr_bo, (u64 *)&psp->tmr_mc_addr, &psp->tmr_buf);
449b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&psp->fw_pri_bo,
450*78973132SSergey Zigachev 			      (u64 *)&psp->fw_pri_mc_addr, &psp->fw_pri_buf);
451b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&psp->fence_buf_bo,
452*78973132SSergey Zigachev 			      (u64 *)&psp->fence_buf_mc_addr, &psp->fence_buf);
453*78973132SSergey Zigachev 	amdgpu_bo_free_kernel(&psp->asd_shared_bo, (u64 *)&psp->asd_shared_mc_addr,
454b843c749SSergey Zigachev 			      &psp->asd_shared_buf);
455*78973132SSergey Zigachev 	amdgpu_bo_free_kernel(&psp->cmd_buf_bo, (u64 *)&psp->cmd_buf_mc_addr,
456b843c749SSergey Zigachev 			      (void **)&psp->cmd_buf_mem);
457b843c749SSergey Zigachev 
458b843c749SSergey Zigachev 	kfree(psp->cmd);
459b843c749SSergey Zigachev 	psp->cmd = NULL;
460b843c749SSergey Zigachev 
461b843c749SSergey Zigachev 	return 0;
462b843c749SSergey Zigachev }
463b843c749SSergey Zigachev 
psp_suspend(void * handle)464b843c749SSergey Zigachev static int psp_suspend(void *handle)
465b843c749SSergey Zigachev {
466b843c749SSergey Zigachev 	int ret;
467b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
468b843c749SSergey Zigachev 	struct psp_context *psp = &adev->psp;
469b843c749SSergey Zigachev 
470b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
471b843c749SSergey Zigachev 		return 0;
472b843c749SSergey Zigachev 
473b843c749SSergey Zigachev 	ret = psp_ring_stop(psp, PSP_RING_TYPE__KM);
474b843c749SSergey Zigachev 	if (ret) {
475b843c749SSergey Zigachev 		DRM_ERROR("PSP ring stop failed\n");
476b843c749SSergey Zigachev 		return ret;
477b843c749SSergey Zigachev 	}
478b843c749SSergey Zigachev 
479b843c749SSergey Zigachev 	return 0;
480b843c749SSergey Zigachev }
481b843c749SSergey Zigachev 
psp_resume(void * handle)482b843c749SSergey Zigachev static int psp_resume(void *handle)
483b843c749SSergey Zigachev {
484b843c749SSergey Zigachev 	int ret;
485b843c749SSergey Zigachev 	struct amdgpu_device *adev = (struct amdgpu_device *)handle;
486b843c749SSergey Zigachev 	struct psp_context *psp = &adev->psp;
487b843c749SSergey Zigachev 
488b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
489b843c749SSergey Zigachev 		return 0;
490b843c749SSergey Zigachev 
491b843c749SSergey Zigachev 	DRM_INFO("PSP is resuming...\n");
492b843c749SSergey Zigachev 
493b843c749SSergey Zigachev 	mutex_lock(&adev->firmware.mutex);
494b843c749SSergey Zigachev 
495b843c749SSergey Zigachev 	ret = psp_hw_start(psp);
496b843c749SSergey Zigachev 	if (ret)
497b843c749SSergey Zigachev 		goto failed;
498b843c749SSergey Zigachev 
499b843c749SSergey Zigachev 	ret = psp_np_fw_load(psp);
500b843c749SSergey Zigachev 	if (ret)
501b843c749SSergey Zigachev 		goto failed;
502b843c749SSergey Zigachev 
503b843c749SSergey Zigachev 	mutex_unlock(&adev->firmware.mutex);
504b843c749SSergey Zigachev 
505b843c749SSergey Zigachev 	return 0;
506b843c749SSergey Zigachev 
507b843c749SSergey Zigachev failed:
508b843c749SSergey Zigachev 	DRM_ERROR("PSP resume failed\n");
509b843c749SSergey Zigachev 	mutex_unlock(&adev->firmware.mutex);
510b843c749SSergey Zigachev 	return ret;
511b843c749SSergey Zigachev }
512b843c749SSergey Zigachev 
psp_gpu_reset(struct amdgpu_device * adev)513b843c749SSergey Zigachev int psp_gpu_reset(struct amdgpu_device *adev)
514b843c749SSergey Zigachev {
515b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP)
516b843c749SSergey Zigachev 		return 0;
517b843c749SSergey Zigachev 
518b843c749SSergey Zigachev 	return psp_mode1_reset(&adev->psp);
519b843c749SSergey Zigachev }
520b843c749SSergey Zigachev 
psp_check_fw_loading_status(struct amdgpu_device * adev,enum AMDGPU_UCODE_ID ucode_type)521b843c749SSergey Zigachev static bool psp_check_fw_loading_status(struct amdgpu_device *adev,
522b843c749SSergey Zigachev 					enum AMDGPU_UCODE_ID ucode_type)
523b843c749SSergey Zigachev {
524b843c749SSergey Zigachev 	struct amdgpu_firmware_info *ucode = NULL;
525b843c749SSergey Zigachev 
526b843c749SSergey Zigachev 	if (adev->firmware.load_type != AMDGPU_FW_LOAD_PSP) {
527b843c749SSergey Zigachev 		DRM_INFO("firmware is not loaded by PSP\n");
528b843c749SSergey Zigachev 		return true;
529b843c749SSergey Zigachev 	}
530b843c749SSergey Zigachev 
531b843c749SSergey Zigachev 	if (!adev->firmware.fw_size)
532b843c749SSergey Zigachev 		return false;
533b843c749SSergey Zigachev 
534b843c749SSergey Zigachev 	ucode = &adev->firmware.ucode[ucode_type];
535b843c749SSergey Zigachev 	if (!ucode->fw || !ucode->ucode_size)
536b843c749SSergey Zigachev 		return false;
537b843c749SSergey Zigachev 
538b843c749SSergey Zigachev 	return psp_compare_sram_data(&adev->psp, ucode, ucode_type);
539b843c749SSergey Zigachev }
540b843c749SSergey Zigachev 
psp_set_clockgating_state(void * handle,enum amd_clockgating_state state)541b843c749SSergey Zigachev static int psp_set_clockgating_state(void *handle,
542b843c749SSergey Zigachev 				     enum amd_clockgating_state state)
543b843c749SSergey Zigachev {
544b843c749SSergey Zigachev 	return 0;
545b843c749SSergey Zigachev }
546b843c749SSergey Zigachev 
psp_set_powergating_state(void * handle,enum amd_powergating_state state)547b843c749SSergey Zigachev static int psp_set_powergating_state(void *handle,
548b843c749SSergey Zigachev 				     enum amd_powergating_state state)
549b843c749SSergey Zigachev {
550b843c749SSergey Zigachev 	return 0;
551b843c749SSergey Zigachev }
552b843c749SSergey Zigachev 
553b843c749SSergey Zigachev const struct amd_ip_funcs psp_ip_funcs = {
554b843c749SSergey Zigachev 	.name = "psp",
555b843c749SSergey Zigachev 	.early_init = psp_early_init,
556b843c749SSergey Zigachev 	.late_init = NULL,
557b843c749SSergey Zigachev 	.sw_init = psp_sw_init,
558b843c749SSergey Zigachev 	.sw_fini = psp_sw_fini,
559b843c749SSergey Zigachev 	.hw_init = psp_hw_init,
560b843c749SSergey Zigachev 	.hw_fini = psp_hw_fini,
561b843c749SSergey Zigachev 	.suspend = psp_suspend,
562b843c749SSergey Zigachev 	.resume = psp_resume,
563b843c749SSergey Zigachev 	.is_idle = NULL,
564b843c749SSergey Zigachev 	.check_soft_reset = NULL,
565b843c749SSergey Zigachev 	.wait_for_idle = NULL,
566b843c749SSergey Zigachev 	.soft_reset = NULL,
567b843c749SSergey Zigachev 	.set_clockgating_state = psp_set_clockgating_state,
568b843c749SSergey Zigachev 	.set_powergating_state = psp_set_powergating_state,
569b843c749SSergey Zigachev };
570b843c749SSergey Zigachev 
571b843c749SSergey Zigachev static const struct amdgpu_psp_funcs psp_funcs = {
572b843c749SSergey Zigachev 	.check_fw_loading_status = psp_check_fw_loading_status,
573b843c749SSergey Zigachev };
574b843c749SSergey Zigachev 
psp_set_funcs(struct amdgpu_device * adev)575b843c749SSergey Zigachev static void psp_set_funcs(struct amdgpu_device *adev)
576b843c749SSergey Zigachev {
577b843c749SSergey Zigachev 	if (NULL == adev->firmware.funcs)
578b843c749SSergey Zigachev 		adev->firmware.funcs = &psp_funcs;
579b843c749SSergey Zigachev }
580b843c749SSergey Zigachev 
581b843c749SSergey Zigachev const struct amdgpu_ip_block_version psp_v3_1_ip_block =
582b843c749SSergey Zigachev {
583b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_PSP,
584b843c749SSergey Zigachev 	.major = 3,
585b843c749SSergey Zigachev 	.minor = 1,
586b843c749SSergey Zigachev 	.rev = 0,
587b843c749SSergey Zigachev 	.funcs = &psp_ip_funcs,
588b843c749SSergey Zigachev };
589b843c749SSergey Zigachev 
590b843c749SSergey Zigachev const struct amdgpu_ip_block_version psp_v10_0_ip_block =
591b843c749SSergey Zigachev {
592b843c749SSergey Zigachev 	.type = AMD_IP_BLOCK_TYPE_PSP,
593b843c749SSergey Zigachev 	.major = 10,
594b843c749SSergey Zigachev 	.minor = 0,
595b843c749SSergey Zigachev 	.rev = 0,
596b843c749SSergey Zigachev 	.funcs = &psp_ip_funcs,
597b843c749SSergey Zigachev };
598