xref: /linux/drivers/gpu/drm/amd/amdgpu/psp_v15_0.c (revision ca220141fa8ebae09765a242076b2b77338106b0)
1 /*
2  * Copyright 2025 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 #include <drm/drm_drv.h>
24 #include <linux/vmalloc.h>
25 #include "amdgpu.h"
26 #include "amdgpu_psp.h"
27 #include "amdgpu_ucode.h"
28 #include "soc15_common.h"
29 #include "psp_v15_0.h"
30 
31 #include "mp/mp_15_0_0_offset.h"
32 #include "mp/mp_15_0_0_sh_mask.h"
33 
34 MODULE_FIRMWARE("amdgpu/psp_15_0_0_toc.bin");
35 
36 static int psp_v15_0_0_init_microcode(struct psp_context *psp)
37 {
38 	struct amdgpu_device *adev = psp->adev;
39 	char ucode_prefix[30];
40 	int err = 0;
41 
42 	amdgpu_ucode_ip_version_decode(adev, MP0_HWIP, ucode_prefix, sizeof(ucode_prefix));
43 
44 	err = psp_init_toc_microcode(psp, ucode_prefix);
45 	if (err)
46 		return err;
47 
48 	return 0;
49 }
50 
51 static int psp_v15_0_0_ring_stop(struct psp_context *psp,
52 			       enum psp_ring_type ring_type)
53 {
54 	int ret = 0;
55 	struct amdgpu_device *adev = psp->adev;
56 
57 	if (amdgpu_sriov_vf(adev)) {
58 		/* Write the ring destroy command*/
59 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_101,
60 			     GFX_CTRL_CMD_ID_DESTROY_GPCOM_RING);
61 		/* there might be handshake issue with hardware which needs delay */
62 		mdelay(20);
63 		/* Wait for response flag (bit 31) */
64 		ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, regMPASP_SMN_C2PMSG_101),
65 				   0x80000000, 0x80000000, false);
66 	} else {
67 		/* Write the ring destroy command*/
68 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_64,
69 			     GFX_CTRL_CMD_ID_DESTROY_RINGS);
70 		/* there might be handshake issue with hardware which needs delay */
71 		mdelay(20);
72 		/* Wait for response flag (bit 31) */
73 		ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, regMPASP_SMN_C2PMSG_64),
74 				   0x80000000, 0x80000000, false);
75 	}
76 
77 	return ret;
78 }
79 
80 static int psp_v15_0_0_ring_create(struct psp_context *psp,
81 				 enum psp_ring_type ring_type)
82 {
83 	int ret = 0;
84 	unsigned int psp_ring_reg = 0;
85 	struct psp_ring *ring = &psp->km_ring;
86 	struct amdgpu_device *adev = psp->adev;
87 
88 	if (amdgpu_sriov_vf(adev)) {
89 		ret = psp_v15_0_0_ring_stop(psp, ring_type);
90 		if (ret) {
91 			DRM_ERROR("psp_v14_0_ring_stop_sriov failed!\n");
92 			return ret;
93 		}
94 
95 		/* Write low address of the ring to C2PMSG_102 */
96 		psp_ring_reg = lower_32_bits(ring->ring_mem_mc_addr);
97 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_102, psp_ring_reg);
98 		/* Write high address of the ring to C2PMSG_103 */
99 		psp_ring_reg = upper_32_bits(ring->ring_mem_mc_addr);
100 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_103, psp_ring_reg);
101 
102 		/* Write the ring initialization command to C2PMSG_101 */
103 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_101,
104 			     GFX_CTRL_CMD_ID_INIT_GPCOM_RING);
105 
106 		/* there might be handshake issue with hardware which needs delay */
107 		mdelay(20);
108 
109 		/* Wait for response flag (bit 31) in C2PMSG_101 */
110 		ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, regMPASP_SMN_C2PMSG_101),
111 				   0x80000000, 0x8000FFFF, false);
112 
113 	} else {
114 		/* Wait for sOS ready for ring creation */
115 		ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, regMPASP_SMN_C2PMSG_64),
116 				   0x80000000, 0x80000000, false);
117 		if (ret) {
118 			DRM_ERROR("Failed to wait for trust OS ready for ring creation\n");
119 			return ret;
120 		}
121 
122 		/* Write low address of the ring to C2PMSG_69 */
123 		psp_ring_reg = lower_32_bits(ring->ring_mem_mc_addr);
124 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_69, psp_ring_reg);
125 		/* Write high address of the ring to C2PMSG_70 */
126 		psp_ring_reg = upper_32_bits(ring->ring_mem_mc_addr);
127 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_70, psp_ring_reg);
128 		/* Write size of ring to C2PMSG_71 */
129 		psp_ring_reg = ring->ring_size;
130 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_71, psp_ring_reg);
131 		/* Write the ring initialization command to C2PMSG_64 */
132 		psp_ring_reg = ring_type;
133 		psp_ring_reg = psp_ring_reg << 16;
134 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_64, psp_ring_reg);
135 
136 		/* there might be handshake issue with hardware which needs delay */
137 		mdelay(20);
138 
139 		/* Wait for response flag (bit 31) in C2PMSG_64 */
140 		ret = psp_wait_for(psp, SOC15_REG_OFFSET(MP0, 0, regMPASP_SMN_C2PMSG_64),
141 				   0x80000000, 0x8000FFFF, false);
142 	}
143 
144 	return ret;
145 }
146 
147 static int psp_v15_0_0_ring_destroy(struct psp_context *psp,
148 				  enum psp_ring_type ring_type)
149 {
150 	int ret = 0;
151 	struct psp_ring *ring = &psp->km_ring;
152 	struct amdgpu_device *adev = psp->adev;
153 
154 	ret = psp_v15_0_0_ring_stop(psp, ring_type);
155 	if (ret)
156 		DRM_ERROR("Fail to stop psp ring\n");
157 
158 	amdgpu_bo_free_kernel(&adev->firmware.rbuf,
159 			      &ring->ring_mem_mc_addr,
160 			      (void **)&ring->ring_mem);
161 
162 	return ret;
163 }
164 
165 static uint32_t psp_v15_0_0_ring_get_wptr(struct psp_context *psp)
166 {
167 	uint32_t data;
168 	struct amdgpu_device *adev = psp->adev;
169 
170 	if (amdgpu_sriov_vf(adev))
171 		data = RREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_102);
172 	else
173 		data = RREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_67);
174 
175 	return data;
176 }
177 
178 static void psp_v15_0_0_ring_set_wptr(struct psp_context *psp, uint32_t value)
179 {
180 	struct amdgpu_device *adev = psp->adev;
181 
182 	if (amdgpu_sriov_vf(adev)) {
183 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_102, value);
184 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_101,
185 			     GFX_CTRL_CMD_ID_CONSUME_CMD);
186 	} else
187 		WREG32_SOC15(MP0, 0, regMPASP_SMN_C2PMSG_67, value);
188 }
189 
190 static const struct psp_funcs psp_v15_0_0_funcs = {
191 	.init_microcode = psp_v15_0_0_init_microcode,
192 	.ring_create = psp_v15_0_0_ring_create,
193 	.ring_stop = psp_v15_0_0_ring_stop,
194 	.ring_destroy = psp_v15_0_0_ring_destroy,
195 	.ring_get_wptr = psp_v15_0_0_ring_get_wptr,
196 	.ring_set_wptr = psp_v15_0_0_ring_set_wptr,
197 };
198 
199 void psp_v15_0_0_set_psp_funcs(struct psp_context *psp)
200 {
201 	psp->funcs = &psp_v15_0_0_funcs;
202 }
203