1 /*
2  * Copyright 2016 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  * Author: Huang Rui
23  *
24  */
25 #ifndef __AMDGPU_PSP_H__
26 #define __AMDGPU_PSP_H__
27 
28 #include "amdgpu.h"
29 #include "psp_gfx_if.h"
30 #include "ta_xgmi_if.h"
31 #include "ta_ras_if.h"
32 
33 #define PSP_FENCE_BUFFER_SIZE	0x1000
34 #define PSP_CMD_BUFFER_SIZE	0x1000
35 #define PSP_ASD_SHARED_MEM_SIZE 0x4000
36 #define PSP_XGMI_SHARED_MEM_SIZE 0x4000
37 #define PSP_RAS_SHARED_MEM_SIZE 0x4000
38 #define PSP_1_MEG		0x100000
39 #define PSP_TMR_SIZE	0x400000
40 
41 struct psp_context;
42 struct psp_xgmi_node_info;
43 struct psp_xgmi_topology_info;
44 
45 enum psp_ring_type
46 {
47 	PSP_RING_TYPE__INVALID = 0,
48 	/*
49 	 * These values map to the way the PSP kernel identifies the
50 	 * rings.
51 	 */
52 	PSP_RING_TYPE__UM = 1, /* User mode ring (formerly called RBI) */
53 	PSP_RING_TYPE__KM = 2  /* Kernel mode ring (formerly called GPCOM) */
54 };
55 
56 struct psp_ring
57 {
58 	enum psp_ring_type		ring_type;
59 	struct psp_gfx_rb_frame		*ring_mem;
60 	uint64_t			ring_mem_mc_addr;
61 	void				*ring_mem_handle;
62 	uint32_t			ring_size;
63 };
64 
65 struct psp_funcs
66 {
67 	int (*init_microcode)(struct psp_context *psp);
68 	int (*bootloader_load_sysdrv)(struct psp_context *psp);
69 	int (*bootloader_load_sos)(struct psp_context *psp);
70 	int (*ring_init)(struct psp_context *psp, enum psp_ring_type ring_type);
71 	int (*ring_create)(struct psp_context *psp,
72 			   enum psp_ring_type ring_type);
73 	int (*ring_stop)(struct psp_context *psp,
74 			    enum psp_ring_type ring_type);
75 	int (*ring_destroy)(struct psp_context *psp,
76 			    enum psp_ring_type ring_type);
77 	int (*cmd_submit)(struct psp_context *psp,
78 			  struct amdgpu_firmware_info *ucode,
79 			  uint64_t cmd_buf_mc_addr, uint64_t fence_mc_addr,
80 			  int index);
81 	bool (*compare_sram_data)(struct psp_context *psp,
82 				  struct amdgpu_firmware_info *ucode,
83 				  enum AMDGPU_UCODE_ID ucode_type);
84 	bool (*smu_reload_quirk)(struct psp_context *psp);
85 	int (*mode1_reset)(struct psp_context *psp);
86 	int (*xgmi_get_node_id)(struct psp_context *psp, uint64_t *node_id);
87 	int (*xgmi_get_hive_id)(struct psp_context *psp, uint64_t *hive_id);
88 	int (*xgmi_get_topology_info)(struct psp_context *psp, int number_devices,
89 				      struct psp_xgmi_topology_info *topology);
90 	int (*xgmi_set_topology_info)(struct psp_context *psp, int number_devices,
91 				      struct psp_xgmi_topology_info *topology);
92 	bool (*support_vmr_ring)(struct psp_context *psp);
93 	int (*ras_trigger_error)(struct psp_context *psp,
94 			struct ta_ras_trigger_error_input *info);
95 	int (*ras_cure_posion)(struct psp_context *psp, uint64_t *mode_ptr);
96 };
97 
98 struct psp_xgmi_context {
99 	uint8_t				initialized;
100 	uint32_t			session_id;
101 	struct amdgpu_bo                *xgmi_shared_bo;
102 	uint64_t                        xgmi_shared_mc_addr;
103 	void                            *xgmi_shared_buf;
104 };
105 
106 struct psp_ras_context {
107 	/*ras fw*/
108 	bool			ras_initialized;
109 	uint32_t		session_id;
110 	struct amdgpu_bo	*ras_shared_bo;
111 	uint64_t		ras_shared_mc_addr;
112 	void			*ras_shared_buf;
113 	struct amdgpu_ras	*ras;
114 };
115 
116 struct psp_context
117 {
118 	struct amdgpu_device            *adev;
119 	struct psp_ring                 km_ring;
120 	struct psp_gfx_cmd_resp		*cmd;
121 
122 	const struct psp_funcs		*funcs;
123 
124 	/* firmware buffer */
125 	struct amdgpu_bo		*fw_pri_bo;
126 	uint64_t			fw_pri_mc_addr;
127 	void				*fw_pri_buf;
128 
129 	/* sos firmware */
130 	const struct firmware		*sos_fw;
131 	uint32_t			sos_fw_version;
132 	uint32_t			sos_feature_version;
133 	uint32_t			sys_bin_size;
134 	uint32_t			sos_bin_size;
135 	uint8_t				*sys_start_addr;
136 	uint8_t				*sos_start_addr;
137 
138 	/* tmr buffer */
139 	struct amdgpu_bo		*tmr_bo;
140 	uint64_t			tmr_mc_addr;
141 	void				*tmr_buf;
142 
143 	/* asd firmware and buffer */
144 	const struct firmware		*asd_fw;
145 	uint32_t			asd_fw_version;
146 	uint32_t			asd_feature_version;
147 	uint32_t			asd_ucode_size;
148 	uint8_t				*asd_start_addr;
149 	struct amdgpu_bo		*asd_shared_bo;
150 	uint64_t			asd_shared_mc_addr;
151 	void				*asd_shared_buf;
152 
153 	/* fence buffer */
154 	struct amdgpu_bo		*fence_buf_bo;
155 	uint64_t			fence_buf_mc_addr;
156 	void				*fence_buf;
157 
158 	/* cmd buffer */
159 	struct amdgpu_bo		*cmd_buf_bo;
160 	uint64_t			cmd_buf_mc_addr;
161 	struct psp_gfx_cmd_resp		*cmd_buf_mem;
162 
163 	/* fence value associated with cmd buffer */
164 	atomic_t			fence_value;
165 
166 	/* xgmi ta firmware and buffer */
167 	const struct firmware		*ta_fw;
168 	uint32_t			ta_fw_version;
169 	uint32_t			ta_xgmi_ucode_version;
170 	uint32_t			ta_xgmi_ucode_size;
171 	uint8_t				*ta_xgmi_start_addr;
172 	uint32_t			ta_ras_ucode_version;
173 	uint32_t			ta_ras_ucode_size;
174 	uint8_t				*ta_ras_start_addr;
175 	struct psp_xgmi_context		xgmi_context;
176 	struct psp_ras_context		ras;
177 };
178 
179 struct amdgpu_psp_funcs {
180 	bool (*check_fw_loading_status)(struct amdgpu_device *adev,
181 					enum AMDGPU_UCODE_ID);
182 };
183 
184 #define AMDGPU_XGMI_MAX_CONNECTED_NODES		64
185 struct psp_xgmi_node_info {
186 	uint64_t				node_id;
187 	uint8_t					num_hops;
188 	uint8_t					is_sharing_enabled;
189 	enum ta_xgmi_assigned_sdma_engine	sdma_engine;
190 };
191 
192 struct psp_xgmi_topology_info {
193 	uint32_t			num_nodes;
194 	struct psp_xgmi_node_info	nodes[AMDGPU_XGMI_MAX_CONNECTED_NODES];
195 };
196 
197 #define psp_ring_init(psp, type) (psp)->funcs->ring_init((psp), (type))
198 #define psp_ring_create(psp, type) (psp)->funcs->ring_create((psp), (type))
199 #define psp_ring_stop(psp, type) (psp)->funcs->ring_stop((psp), (type))
200 #define psp_ring_destroy(psp, type) ((psp)->funcs->ring_destroy((psp), (type)))
201 #define psp_cmd_submit(psp, ucode, cmd_mc, fence_mc, index) \
202 		(psp)->funcs->cmd_submit((psp), (ucode), (cmd_mc), (fence_mc), (index))
203 #define psp_compare_sram_data(psp, ucode, type) \
204 		(psp)->funcs->compare_sram_data((psp), (ucode), (type))
205 #define psp_init_microcode(psp) \
206 		((psp)->funcs->init_microcode ? (psp)->funcs->init_microcode((psp)) : 0)
207 #define psp_bootloader_load_sysdrv(psp) \
208 		((psp)->funcs->bootloader_load_sysdrv ? (psp)->funcs->bootloader_load_sysdrv((psp)) : 0)
209 #define psp_bootloader_load_sos(psp) \
210 		((psp)->funcs->bootloader_load_sos ? (psp)->funcs->bootloader_load_sos((psp)) : 0)
211 #define psp_smu_reload_quirk(psp) \
212 		((psp)->funcs->smu_reload_quirk ? (psp)->funcs->smu_reload_quirk((psp)) : false)
213 #define psp_support_vmr_ring(psp) \
214 		((psp)->funcs->support_vmr_ring ? (psp)->funcs->support_vmr_ring((psp)) : false)
215 #define psp_mode1_reset(psp) \
216 		((psp)->funcs->mode1_reset ? (psp)->funcs->mode1_reset((psp)) : false)
217 #define psp_xgmi_get_node_id(psp, node_id) \
218 		((psp)->funcs->xgmi_get_node_id ? (psp)->funcs->xgmi_get_node_id((psp), (node_id)) : -EINVAL)
219 #define psp_xgmi_get_hive_id(psp, hive_id) \
220 		((psp)->funcs->xgmi_get_hive_id ? (psp)->funcs->xgmi_get_hive_id((psp), (hive_id)) : -EINVAL)
221 #define psp_xgmi_get_topology_info(psp, num_device, topology) \
222 		((psp)->funcs->xgmi_get_topology_info ? \
223 		(psp)->funcs->xgmi_get_topology_info((psp), (num_device), (topology)) : -EINVAL)
224 #define psp_xgmi_set_topology_info(psp, num_device, topology) \
225 		((psp)->funcs->xgmi_set_topology_info ?	 \
226 		(psp)->funcs->xgmi_set_topology_info((psp), (num_device), (topology)) : -EINVAL)
227 
228 #define amdgpu_psp_check_fw_loading_status(adev, i) (adev)->firmware.funcs->check_fw_loading_status((adev), (i))
229 
230 #define psp_ras_trigger_error(psp, info) \
231 	((psp)->funcs->ras_trigger_error ? \
232 	(psp)->funcs->ras_trigger_error((psp), (info)) : -EINVAL)
233 #define psp_ras_cure_posion(psp, addr) \
234 	((psp)->funcs->ras_cure_posion ? \
235 	(psp)->funcs->ras_cure_posion(psp, (addr)) : -EINVAL)
236 
237 extern const struct amd_ip_funcs psp_ip_funcs;
238 
239 extern const struct amdgpu_ip_block_version psp_v3_1_ip_block;
240 extern int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
241 			uint32_t field_val, uint32_t mask, bool check_changed);
242 
243 extern const struct amdgpu_ip_block_version psp_v10_0_ip_block;
244 
245 int psp_gpu_reset(struct amdgpu_device *adev);
246 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id);
247 
248 int psp_ras_invoke(struct psp_context *psp, uint32_t ta_cmd_id);
249 int psp_ras_enable_features(struct psp_context *psp,
250 		union ta_ras_cmd_input *info, bool enable);
251 
252 extern const struct amdgpu_ip_block_version psp_v11_0_ip_block;
253 
254 #endif
255