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 
32 #define PSP_FENCE_BUFFER_SIZE	0x1000
33 #define PSP_CMD_BUFFER_SIZE	0x1000
34 #define PSP_ASD_SHARED_MEM_SIZE 0x4000
35 #define PSP_XGMI_SHARED_MEM_SIZE 0x4000
36 #define PSP_1_MEG		0x100000
37 #define PSP_TMR_SIZE	0x400000
38 
39 struct psp_context;
40 struct psp_xgmi_node_info;
41 struct psp_xgmi_topology_info;
42 
43 enum psp_ring_type
44 {
45 	PSP_RING_TYPE__INVALID = 0,
46 	/*
47 	 * These values map to the way the PSP kernel identifies the
48 	 * rings.
49 	 */
50 	PSP_RING_TYPE__UM = 1, /* User mode ring (formerly called RBI) */
51 	PSP_RING_TYPE__KM = 2  /* Kernel mode ring (formerly called GPCOM) */
52 };
53 
54 struct psp_ring
55 {
56 	enum psp_ring_type		ring_type;
57 	struct psp_gfx_rb_frame		*ring_mem;
58 	uint64_t			ring_mem_mc_addr;
59 	void				*ring_mem_handle;
60 	uint32_t			ring_size;
61 };
62 
63 struct psp_funcs
64 {
65 	int (*init_microcode)(struct psp_context *psp);
66 	int (*bootloader_load_sysdrv)(struct psp_context *psp);
67 	int (*bootloader_load_sos)(struct psp_context *psp);
68 	int (*prep_cmd_buf)(struct amdgpu_firmware_info *ucode,
69 			    struct psp_gfx_cmd_resp *cmd);
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 };
94 
95 struct psp_xgmi_context {
96 	uint8_t				initialized;
97 	uint32_t			session_id;
98 	struct amdgpu_bo                *xgmi_shared_bo;
99 	uint64_t                        xgmi_shared_mc_addr;
100 	void                            *xgmi_shared_buf;
101 };
102 
103 struct psp_context
104 {
105 	struct amdgpu_device            *adev;
106 	struct psp_ring                 km_ring;
107 	struct psp_gfx_cmd_resp		*cmd;
108 
109 	const struct psp_funcs		*funcs;
110 
111 	/* firmware buffer */
112 	struct amdgpu_bo		*fw_pri_bo;
113 	uint64_t			fw_pri_mc_addr;
114 	void				*fw_pri_buf;
115 
116 	/* sos firmware */
117 	const struct firmware		*sos_fw;
118 	uint32_t			sos_fw_version;
119 	uint32_t			sos_feature_version;
120 	uint32_t			sys_bin_size;
121 	uint32_t			sos_bin_size;
122 	uint8_t				*sys_start_addr;
123 	uint8_t				*sos_start_addr;
124 
125 	/* tmr buffer */
126 	struct amdgpu_bo		*tmr_bo;
127 	uint64_t			tmr_mc_addr;
128 	void				*tmr_buf;
129 
130 	/* asd firmware and buffer */
131 	const struct firmware		*asd_fw;
132 	uint32_t			asd_fw_version;
133 	uint32_t			asd_feature_version;
134 	uint32_t			asd_ucode_size;
135 	uint8_t				*asd_start_addr;
136 	struct amdgpu_bo		*asd_shared_bo;
137 	uint64_t			asd_shared_mc_addr;
138 	void				*asd_shared_buf;
139 
140 	/* fence buffer */
141 	struct amdgpu_bo		*fence_buf_bo;
142 	uint64_t			fence_buf_mc_addr;
143 	void				*fence_buf;
144 
145 	/* cmd buffer */
146 	struct amdgpu_bo		*cmd_buf_bo;
147 	uint64_t			cmd_buf_mc_addr;
148 	struct psp_gfx_cmd_resp		*cmd_buf_mem;
149 
150 	/* fence value associated with cmd buffer */
151 	atomic_t			fence_value;
152 
153 	/* xgmi ta firmware and buffer */
154 	const struct firmware		*ta_fw;
155 	uint32_t			ta_xgmi_ucode_version;
156 	uint32_t			ta_xgmi_ucode_size;
157 	uint8_t				*ta_xgmi_start_addr;
158 	struct psp_xgmi_context		xgmi_context;
159 };
160 
161 struct amdgpu_psp_funcs {
162 	bool (*check_fw_loading_status)(struct amdgpu_device *adev,
163 					enum AMDGPU_UCODE_ID);
164 };
165 
166 #define AMDGPU_XGMI_MAX_CONNECTED_NODES		64
167 struct psp_xgmi_node_info {
168 	uint64_t				node_id;
169 	uint8_t					num_hops;
170 	uint8_t					is_sharing_enabled;
171 	enum ta_xgmi_assigned_sdma_engine	sdma_engine;
172 };
173 
174 struct psp_xgmi_topology_info {
175 	uint32_t			num_nodes;
176 	struct psp_xgmi_node_info	nodes[AMDGPU_XGMI_MAX_CONNECTED_NODES];
177 };
178 
179 #define psp_prep_cmd_buf(ucode, type) (psp)->funcs->prep_cmd_buf((ucode), (type))
180 #define psp_ring_init(psp, type) (psp)->funcs->ring_init((psp), (type))
181 #define psp_ring_create(psp, type) (psp)->funcs->ring_create((psp), (type))
182 #define psp_ring_stop(psp, type) (psp)->funcs->ring_stop((psp), (type))
183 #define psp_ring_destroy(psp, type) ((psp)->funcs->ring_destroy((psp), (type)))
184 #define psp_cmd_submit(psp, ucode, cmd_mc, fence_mc, index) \
185 		(psp)->funcs->cmd_submit((psp), (ucode), (cmd_mc), (fence_mc), (index))
186 #define psp_compare_sram_data(psp, ucode, type) \
187 		(psp)->funcs->compare_sram_data((psp), (ucode), (type))
188 #define psp_init_microcode(psp) \
189 		((psp)->funcs->init_microcode ? (psp)->funcs->init_microcode((psp)) : 0)
190 #define psp_bootloader_load_sysdrv(psp) \
191 		((psp)->funcs->bootloader_load_sysdrv ? (psp)->funcs->bootloader_load_sysdrv((psp)) : 0)
192 #define psp_bootloader_load_sos(psp) \
193 		((psp)->funcs->bootloader_load_sos ? (psp)->funcs->bootloader_load_sos((psp)) : 0)
194 #define psp_smu_reload_quirk(psp) \
195 		((psp)->funcs->smu_reload_quirk ? (psp)->funcs->smu_reload_quirk((psp)) : false)
196 #define psp_support_vmr_ring(psp) \
197 		((psp)->funcs->support_vmr_ring ? (psp)->funcs->support_vmr_ring((psp)) : false)
198 #define psp_mode1_reset(psp) \
199 		((psp)->funcs->mode1_reset ? (psp)->funcs->mode1_reset((psp)) : false)
200 #define psp_xgmi_get_node_id(psp, node_id) \
201 		((psp)->funcs->xgmi_get_node_id ? (psp)->funcs->xgmi_get_node_id((psp), (node_id)) : -EINVAL)
202 #define psp_xgmi_get_hive_id(psp, hive_id) \
203 		((psp)->funcs->xgmi_get_hive_id ? (psp)->funcs->xgmi_get_hive_id((psp), (hive_id)) : -EINVAL)
204 #define psp_xgmi_get_topology_info(psp, num_device, topology) \
205 		((psp)->funcs->xgmi_get_topology_info ? \
206 		(psp)->funcs->xgmi_get_topology_info((psp), (num_device), (topology)) : -EINVAL)
207 #define psp_xgmi_set_topology_info(psp, num_device, topology) \
208 		((psp)->funcs->xgmi_set_topology_info ?	 \
209 		(psp)->funcs->xgmi_set_topology_info((psp), (num_device), (topology)) : -EINVAL)
210 
211 #define amdgpu_psp_check_fw_loading_status(adev, i) (adev)->firmware.funcs->check_fw_loading_status((adev), (i))
212 
213 extern const struct amd_ip_funcs psp_ip_funcs;
214 
215 extern const struct amdgpu_ip_block_version psp_v3_1_ip_block;
216 extern int psp_wait_for(struct psp_context *psp, uint32_t reg_index,
217 			uint32_t field_val, uint32_t mask, bool check_changed);
218 
219 extern const struct amdgpu_ip_block_version psp_v10_0_ip_block;
220 
221 int psp_gpu_reset(struct amdgpu_device *adev);
222 int psp_xgmi_invoke(struct psp_context *psp, uint32_t ta_cmd_id);
223 extern const struct amdgpu_ip_block_version psp_v11_0_ip_block;
224 
225 #endif
226