xref: /linux/drivers/gpu/drm/amd/amdgpu/amdgpu_xgmi.c (revision 7f4f3b14e8079ecde096bd734af10e30d40c27b7)
1 /*
2  * Copyright 2018 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  */
24 #include <linux/list.h>
25 #include "amdgpu.h"
26 #include "amdgpu_xgmi.h"
27 #include "amdgpu_ras.h"
28 #include "soc15.h"
29 #include "df/df_3_6_offset.h"
30 #include "xgmi/xgmi_4_0_0_smn.h"
31 #include "xgmi/xgmi_4_0_0_sh_mask.h"
32 #include "xgmi/xgmi_6_1_0_sh_mask.h"
33 #include "wafl/wafl2_4_0_0_smn.h"
34 #include "wafl/wafl2_4_0_0_sh_mask.h"
35 
36 #include "amdgpu_reset.h"
37 
38 #define smnPCS_XGMI3X16_PCS_ERROR_STATUS 0x11a0020c
39 #define smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK   0x11a00218
40 #define smnPCS_GOPX1_PCS_ERROR_STATUS    0x12200210
41 #define smnPCS_GOPX1_PCS_ERROR_NONCORRECTABLE_MASK      0x12200218
42 
43 static DEFINE_MUTEX(xgmi_mutex);
44 
45 #define AMDGPU_MAX_XGMI_DEVICE_PER_HIVE		4
46 
47 static LIST_HEAD(xgmi_hive_list);
48 
49 static const int xgmi_pcs_err_status_reg_vg20[] = {
50 	smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS,
51 	smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x100000,
52 };
53 
54 static const int wafl_pcs_err_status_reg_vg20[] = {
55 	smnPCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS,
56 	smnPCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS + 0x100000,
57 };
58 
59 static const int xgmi_pcs_err_status_reg_arct[] = {
60 	smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS,
61 	smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x100000,
62 	smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x500000,
63 	smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x600000,
64 	smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x700000,
65 	smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x800000,
66 };
67 
68 /* same as vg20*/
69 static const int wafl_pcs_err_status_reg_arct[] = {
70 	smnPCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS,
71 	smnPCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS + 0x100000,
72 };
73 
74 static const int xgmi3x16_pcs_err_status_reg_aldebaran[] = {
75 	smnPCS_XGMI3X16_PCS_ERROR_STATUS,
76 	smnPCS_XGMI3X16_PCS_ERROR_STATUS + 0x100000,
77 	smnPCS_XGMI3X16_PCS_ERROR_STATUS + 0x200000,
78 	smnPCS_XGMI3X16_PCS_ERROR_STATUS + 0x300000,
79 	smnPCS_XGMI3X16_PCS_ERROR_STATUS + 0x400000,
80 	smnPCS_XGMI3X16_PCS_ERROR_STATUS + 0x500000,
81 	smnPCS_XGMI3X16_PCS_ERROR_STATUS + 0x600000,
82 	smnPCS_XGMI3X16_PCS_ERROR_STATUS + 0x700000
83 };
84 
85 static const int xgmi3x16_pcs_err_noncorrectable_mask_reg_aldebaran[] = {
86 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK,
87 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK + 0x100000,
88 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK + 0x200000,
89 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK + 0x300000,
90 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK + 0x400000,
91 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK + 0x500000,
92 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK + 0x600000,
93 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK + 0x700000
94 };
95 
96 static const int walf_pcs_err_status_reg_aldebaran[] = {
97 	smnPCS_GOPX1_PCS_ERROR_STATUS,
98 	smnPCS_GOPX1_PCS_ERROR_STATUS + 0x100000
99 };
100 
101 static const int walf_pcs_err_noncorrectable_mask_reg_aldebaran[] = {
102 	smnPCS_GOPX1_PCS_ERROR_NONCORRECTABLE_MASK,
103 	smnPCS_GOPX1_PCS_ERROR_NONCORRECTABLE_MASK + 0x100000
104 };
105 
106 static const int xgmi3x16_pcs_err_status_reg_v6_4[] = {
107 	smnPCS_XGMI3X16_PCS_ERROR_STATUS,
108 	smnPCS_XGMI3X16_PCS_ERROR_STATUS + 0x100000
109 };
110 
111 static const int xgmi3x16_pcs_err_noncorrectable_mask_reg_v6_4[] = {
112 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK,
113 	smnPCS_XGMI3X16_PCS_ERROR_NONCORRECTABLE_MASK + 0x100000
114 };
115 
116 static const u64 xgmi_v6_4_0_mca_base_array[] = {
117 	0x11a09200,
118 	0x11b09200,
119 };
120 
121 static const char *xgmi_v6_4_0_ras_error_code_ext[32] = {
122 	[0x00] = "XGMI PCS DataLossErr",
123 	[0x01] = "XGMI PCS TrainingErr",
124 	[0x02] = "XGMI PCS FlowCtrlAckErr",
125 	[0x03] = "XGMI PCS RxFifoUnderflowErr",
126 	[0x04] = "XGMI PCS RxFifoOverflowErr",
127 	[0x05] = "XGMI PCS CRCErr",
128 	[0x06] = "XGMI PCS BERExceededErr",
129 	[0x07] = "XGMI PCS TxMetaDataErr",
130 	[0x08] = "XGMI PCS ReplayBufParityErr",
131 	[0x09] = "XGMI PCS DataParityErr",
132 	[0x0a] = "XGMI PCS ReplayFifoOverflowErr",
133 	[0x0b] = "XGMI PCS ReplayFifoUnderflowErr",
134 	[0x0c] = "XGMI PCS ElasticFifoOverflowErr",
135 	[0x0d] = "XGMI PCS DeskewErr",
136 	[0x0e] = "XGMI PCS FlowCtrlCRCErr",
137 	[0x0f] = "XGMI PCS DataStartupLimitErr",
138 	[0x10] = "XGMI PCS FCInitTimeoutErr",
139 	[0x11] = "XGMI PCS RecoveryTimeoutErr",
140 	[0x12] = "XGMI PCS ReadySerialTimeoutErr",
141 	[0x13] = "XGMI PCS ReadySerialAttemptErr",
142 	[0x14] = "XGMI PCS RecoveryAttemptErr",
143 	[0x15] = "XGMI PCS RecoveryRelockAttemptErr",
144 	[0x16] = "XGMI PCS ReplayAttemptErr",
145 	[0x17] = "XGMI PCS SyncHdrErr",
146 	[0x18] = "XGMI PCS TxReplayTimeoutErr",
147 	[0x19] = "XGMI PCS RxReplayTimeoutErr",
148 	[0x1a] = "XGMI PCS LinkSubTxTimeoutErr",
149 	[0x1b] = "XGMI PCS LinkSubRxTimeoutErr",
150 	[0x1c] = "XGMI PCS RxCMDPktErr",
151 };
152 
153 static const struct amdgpu_pcs_ras_field xgmi_pcs_ras_fields[] = {
154 	{"XGMI PCS DataLossErr",
155 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataLossErr)},
156 	{"XGMI PCS TrainingErr",
157 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, TrainingErr)},
158 	{"XGMI PCS CRCErr",
159 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, CRCErr)},
160 	{"XGMI PCS BERExceededErr",
161 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, BERExceededErr)},
162 	{"XGMI PCS TxMetaDataErr",
163 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, TxMetaDataErr)},
164 	{"XGMI PCS ReplayBufParityErr",
165 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayBufParityErr)},
166 	{"XGMI PCS DataParityErr",
167 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataParityErr)},
168 	{"XGMI PCS ReplayFifoOverflowErr",
169 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayFifoOverflowErr)},
170 	{"XGMI PCS ReplayFifoUnderflowErr",
171 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayFifoUnderflowErr)},
172 	{"XGMI PCS ElasticFifoOverflowErr",
173 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ElasticFifoOverflowErr)},
174 	{"XGMI PCS DeskewErr",
175 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DeskewErr)},
176 	{"XGMI PCS DataStartupLimitErr",
177 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataStartupLimitErr)},
178 	{"XGMI PCS FCInitTimeoutErr",
179 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, FCInitTimeoutErr)},
180 	{"XGMI PCS RecoveryTimeoutErr",
181 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryTimeoutErr)},
182 	{"XGMI PCS ReadySerialTimeoutErr",
183 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReadySerialTimeoutErr)},
184 	{"XGMI PCS ReadySerialAttemptErr",
185 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReadySerialAttemptErr)},
186 	{"XGMI PCS RecoveryAttemptErr",
187 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryAttemptErr)},
188 	{"XGMI PCS RecoveryRelockAttemptErr",
189 	 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryRelockAttemptErr)},
190 };
191 
192 static const struct amdgpu_pcs_ras_field wafl_pcs_ras_fields[] = {
193 	{"WAFL PCS DataLossErr",
194 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataLossErr)},
195 	{"WAFL PCS TrainingErr",
196 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, TrainingErr)},
197 	{"WAFL PCS CRCErr",
198 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, CRCErr)},
199 	{"WAFL PCS BERExceededErr",
200 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, BERExceededErr)},
201 	{"WAFL PCS TxMetaDataErr",
202 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, TxMetaDataErr)},
203 	{"WAFL PCS ReplayBufParityErr",
204 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayBufParityErr)},
205 	{"WAFL PCS DataParityErr",
206 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataParityErr)},
207 	{"WAFL PCS ReplayFifoOverflowErr",
208 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayFifoOverflowErr)},
209 	{"WAFL PCS ReplayFifoUnderflowErr",
210 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayFifoUnderflowErr)},
211 	{"WAFL PCS ElasticFifoOverflowErr",
212 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ElasticFifoOverflowErr)},
213 	{"WAFL PCS DeskewErr",
214 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DeskewErr)},
215 	{"WAFL PCS DataStartupLimitErr",
216 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataStartupLimitErr)},
217 	{"WAFL PCS FCInitTimeoutErr",
218 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, FCInitTimeoutErr)},
219 	{"WAFL PCS RecoveryTimeoutErr",
220 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryTimeoutErr)},
221 	{"WAFL PCS ReadySerialTimeoutErr",
222 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReadySerialTimeoutErr)},
223 	{"WAFL PCS ReadySerialAttemptErr",
224 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReadySerialAttemptErr)},
225 	{"WAFL PCS RecoveryAttemptErr",
226 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryAttemptErr)},
227 	{"WAFL PCS RecoveryRelockAttemptErr",
228 	 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryRelockAttemptErr)},
229 };
230 
231 static const struct amdgpu_pcs_ras_field xgmi3x16_pcs_ras_fields[] = {
232 	{"XGMI3X16 PCS DataLossErr",
233 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, DataLossErr)},
234 	{"XGMI3X16 PCS TrainingErr",
235 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, TrainingErr)},
236 	{"XGMI3X16 PCS FlowCtrlAckErr",
237 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, FlowCtrlAckErr)},
238 	{"XGMI3X16 PCS RxFifoUnderflowErr",
239 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RxFifoUnderflowErr)},
240 	{"XGMI3X16 PCS RxFifoOverflowErr",
241 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RxFifoOverflowErr)},
242 	{"XGMI3X16 PCS CRCErr",
243 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, CRCErr)},
244 	{"XGMI3X16 PCS BERExceededErr",
245 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, BERExceededErr)},
246 	{"XGMI3X16 PCS TxVcidDataErr",
247 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, TxVcidDataErr)},
248 	{"XGMI3X16 PCS ReplayBufParityErr",
249 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReplayBufParityErr)},
250 	{"XGMI3X16 PCS DataParityErr",
251 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, DataParityErr)},
252 	{"XGMI3X16 PCS ReplayFifoOverflowErr",
253 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReplayFifoOverflowErr)},
254 	{"XGMI3X16 PCS ReplayFifoUnderflowErr",
255 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReplayFifoUnderflowErr)},
256 	{"XGMI3X16 PCS ElasticFifoOverflowErr",
257 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ElasticFifoOverflowErr)},
258 	{"XGMI3X16 PCS DeskewErr",
259 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, DeskewErr)},
260 	{"XGMI3X16 PCS FlowCtrlCRCErr",
261 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, FlowCtrlCRCErr)},
262 	{"XGMI3X16 PCS DataStartupLimitErr",
263 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, DataStartupLimitErr)},
264 	{"XGMI3X16 PCS FCInitTimeoutErr",
265 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, FCInitTimeoutErr)},
266 	{"XGMI3X16 PCS RecoveryTimeoutErr",
267 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RecoveryTimeoutErr)},
268 	{"XGMI3X16 PCS ReadySerialTimeoutErr",
269 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReadySerialTimeoutErr)},
270 	{"XGMI3X16 PCS ReadySerialAttemptErr",
271 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReadySerialAttemptErr)},
272 	{"XGMI3X16 PCS RecoveryAttemptErr",
273 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RecoveryAttemptErr)},
274 	{"XGMI3X16 PCS RecoveryRelockAttemptErr",
275 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RecoveryRelockAttemptErr)},
276 	{"XGMI3X16 PCS ReplayAttemptErr",
277 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReplayAttemptErr)},
278 	{"XGMI3X16 PCS SyncHdrErr",
279 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, SyncHdrErr)},
280 	{"XGMI3X16 PCS TxReplayTimeoutErr",
281 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, TxReplayTimeoutErr)},
282 	{"XGMI3X16 PCS RxReplayTimeoutErr",
283 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RxReplayTimeoutErr)},
284 	{"XGMI3X16 PCS LinkSubTxTimeoutErr",
285 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, LinkSubTxTimeoutErr)},
286 	{"XGMI3X16 PCS LinkSubRxTimeoutErr",
287 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, LinkSubRxTimeoutErr)},
288 	{"XGMI3X16 PCS RxCMDPktErr",
289 	 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RxCMDPktErr)},
290 };
291 
292 /**
293  * DOC: AMDGPU XGMI Support
294  *
295  * XGMI is a high speed interconnect that joins multiple GPU cards
296  * into a homogeneous memory space that is organized by a collective
297  * hive ID and individual node IDs, both of which are 64-bit numbers.
298  *
299  * The file xgmi_device_id contains the unique per GPU device ID and
300  * is stored in the /sys/class/drm/card${cardno}/device/ directory.
301  *
302  * Inside the device directory a sub-directory 'xgmi_hive_info' is
303  * created which contains the hive ID and the list of nodes.
304  *
305  * The hive ID is stored in:
306  *   /sys/class/drm/card${cardno}/device/xgmi_hive_info/xgmi_hive_id
307  *
308  * The node information is stored in numbered directories:
309  *   /sys/class/drm/card${cardno}/device/xgmi_hive_info/node${nodeno}/xgmi_device_id
310  *
311  * Each device has their own xgmi_hive_info direction with a mirror
312  * set of node sub-directories.
313  *
314  * The XGMI memory space is built by contiguously adding the power of
315  * two padded VRAM space from each node to each other.
316  *
317  */
318 
319 static struct attribute amdgpu_xgmi_hive_id = {
320 	.name = "xgmi_hive_id",
321 	.mode = S_IRUGO
322 };
323 
324 static struct attribute *amdgpu_xgmi_hive_attrs[] = {
325 	&amdgpu_xgmi_hive_id,
326 	NULL
327 };
328 ATTRIBUTE_GROUPS(amdgpu_xgmi_hive);
329 
330 static ssize_t amdgpu_xgmi_show_attrs(struct kobject *kobj,
331 	struct attribute *attr, char *buf)
332 {
333 	struct amdgpu_hive_info *hive = container_of(
334 		kobj, struct amdgpu_hive_info, kobj);
335 
336 	if (attr == &amdgpu_xgmi_hive_id)
337 		return snprintf(buf, PAGE_SIZE, "%llu\n", hive->hive_id);
338 
339 	return 0;
340 }
341 
342 static void amdgpu_xgmi_hive_release(struct kobject *kobj)
343 {
344 	struct amdgpu_hive_info *hive = container_of(
345 		kobj, struct amdgpu_hive_info, kobj);
346 
347 	amdgpu_reset_put_reset_domain(hive->reset_domain);
348 	hive->reset_domain = NULL;
349 
350 	mutex_destroy(&hive->hive_lock);
351 	kfree(hive);
352 }
353 
354 static const struct sysfs_ops amdgpu_xgmi_hive_ops = {
355 	.show = amdgpu_xgmi_show_attrs,
356 };
357 
358 static const struct kobj_type amdgpu_xgmi_hive_type = {
359 	.release = amdgpu_xgmi_hive_release,
360 	.sysfs_ops = &amdgpu_xgmi_hive_ops,
361 	.default_groups = amdgpu_xgmi_hive_groups,
362 };
363 
364 static ssize_t amdgpu_xgmi_show_device_id(struct device *dev,
365 				     struct device_attribute *attr,
366 				     char *buf)
367 {
368 	struct drm_device *ddev = dev_get_drvdata(dev);
369 	struct amdgpu_device *adev = drm_to_adev(ddev);
370 
371 	return sysfs_emit(buf, "%llu\n", adev->gmc.xgmi.node_id);
372 
373 }
374 
375 static ssize_t amdgpu_xgmi_show_physical_id(struct device *dev,
376 				     struct device_attribute *attr,
377 				     char *buf)
378 {
379 	struct drm_device *ddev = dev_get_drvdata(dev);
380 	struct amdgpu_device *adev = drm_to_adev(ddev);
381 
382 	return sysfs_emit(buf, "%u\n", adev->gmc.xgmi.physical_node_id);
383 
384 }
385 
386 static ssize_t amdgpu_xgmi_show_num_hops(struct device *dev,
387 					struct device_attribute *attr,
388 					char *buf)
389 {
390 	struct drm_device *ddev = dev_get_drvdata(dev);
391 	struct amdgpu_device *adev = drm_to_adev(ddev);
392 	struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
393 	int i;
394 
395 	for (i = 0; i < top->num_nodes; i++)
396 		sprintf(buf + 3 * i, "%02x ", top->nodes[i].num_hops);
397 
398 	return sysfs_emit(buf, "%s\n", buf);
399 }
400 
401 static ssize_t amdgpu_xgmi_show_num_links(struct device *dev,
402 					struct device_attribute *attr,
403 					char *buf)
404 {
405 	struct drm_device *ddev = dev_get_drvdata(dev);
406 	struct amdgpu_device *adev = drm_to_adev(ddev);
407 	struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
408 	int i;
409 
410 	for (i = 0; i < top->num_nodes; i++)
411 		sprintf(buf + 3 * i, "%02x ", top->nodes[i].num_links);
412 
413 	return sysfs_emit(buf, "%s\n", buf);
414 }
415 
416 static ssize_t amdgpu_xgmi_show_connected_port_num(struct device *dev,
417 					struct device_attribute *attr,
418 					char *buf)
419 {
420 	struct drm_device *ddev = dev_get_drvdata(dev);
421 	struct amdgpu_device *adev = drm_to_adev(ddev);
422 	struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
423 	int i, j, size = 0;
424 	int current_node;
425 	/*
426 	 * get the node id in the sysfs for the current socket and show
427 	 * it in the port num info output in the sysfs for easy reading.
428 	 * it is NOT the one retrieved from xgmi ta.
429 	 */
430 	for (i = 0; i < top->num_nodes; i++) {
431 		if (top->nodes[i].node_id == adev->gmc.xgmi.node_id) {
432 			current_node = i;
433 			break;
434 		}
435 	}
436 
437 	if (i == top->num_nodes)
438 		return -EINVAL;
439 
440 	for (i = 0; i < top->num_nodes; i++) {
441 		for (j = 0; j < top->nodes[i].num_links; j++)
442 			/* node id in sysfs starts from 1 rather than 0 so +1 here */
443 			size += sysfs_emit_at(buf, size, "%02x:%02x ->  %02x:%02x\n", current_node + 1,
444 					      top->nodes[i].port_num[j].src_xgmi_port_num, i + 1,
445 					      top->nodes[i].port_num[j].dst_xgmi_port_num);
446 	}
447 
448 	return size;
449 }
450 
451 #define AMDGPU_XGMI_SET_FICAA(o)	((o) | 0x456801)
452 static ssize_t amdgpu_xgmi_show_error(struct device *dev,
453 				      struct device_attribute *attr,
454 				      char *buf)
455 {
456 	struct drm_device *ddev = dev_get_drvdata(dev);
457 	struct amdgpu_device *adev = drm_to_adev(ddev);
458 	uint32_t ficaa_pie_ctl_in, ficaa_pie_status_in;
459 	uint64_t fica_out;
460 	unsigned int error_count = 0;
461 
462 	ficaa_pie_ctl_in = AMDGPU_XGMI_SET_FICAA(0x200);
463 	ficaa_pie_status_in = AMDGPU_XGMI_SET_FICAA(0x208);
464 
465 	if ((!adev->df.funcs) ||
466 	    (!adev->df.funcs->get_fica) ||
467 	    (!adev->df.funcs->set_fica))
468 		return -EINVAL;
469 
470 	fica_out = adev->df.funcs->get_fica(adev, ficaa_pie_ctl_in);
471 	if (fica_out != 0x1f)
472 		pr_err("xGMI error counters not enabled!\n");
473 
474 	fica_out = adev->df.funcs->get_fica(adev, ficaa_pie_status_in);
475 
476 	if ((fica_out & 0xffff) == 2)
477 		error_count = ((fica_out >> 62) & 0x1) + (fica_out >> 63);
478 
479 	adev->df.funcs->set_fica(adev, ficaa_pie_status_in, 0, 0);
480 
481 	return sysfs_emit(buf, "%u\n", error_count);
482 }
483 
484 
485 static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL);
486 static DEVICE_ATTR(xgmi_physical_id, 0444, amdgpu_xgmi_show_physical_id, NULL);
487 static DEVICE_ATTR(xgmi_error, S_IRUGO, amdgpu_xgmi_show_error, NULL);
488 static DEVICE_ATTR(xgmi_num_hops, S_IRUGO, amdgpu_xgmi_show_num_hops, NULL);
489 static DEVICE_ATTR(xgmi_num_links, S_IRUGO, amdgpu_xgmi_show_num_links, NULL);
490 static DEVICE_ATTR(xgmi_port_num, S_IRUGO, amdgpu_xgmi_show_connected_port_num, NULL);
491 
492 static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev,
493 					 struct amdgpu_hive_info *hive)
494 {
495 	int ret = 0;
496 	char node[10] = { 0 };
497 
498 	/* Create xgmi device id file */
499 	ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id);
500 	if (ret) {
501 		dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n");
502 		return ret;
503 	}
504 
505 	ret = device_create_file(adev->dev, &dev_attr_xgmi_physical_id);
506 	if (ret) {
507 		dev_err(adev->dev, "XGMI: Failed to create device file xgmi_physical_id\n");
508 		return ret;
509 	}
510 
511 	/* Create xgmi error file */
512 	ret = device_create_file(adev->dev, &dev_attr_xgmi_error);
513 	if (ret)
514 		pr_err("failed to create xgmi_error\n");
515 
516 	/* Create xgmi num hops file */
517 	ret = device_create_file(adev->dev, &dev_attr_xgmi_num_hops);
518 	if (ret)
519 		pr_err("failed to create xgmi_num_hops\n");
520 
521 	/* Create xgmi num links file */
522 	ret = device_create_file(adev->dev, &dev_attr_xgmi_num_links);
523 	if (ret)
524 		pr_err("failed to create xgmi_num_links\n");
525 
526 	/* Create xgmi port num file if supported */
527 	if (adev->psp.xgmi_context.xgmi_ta_caps & EXTEND_PEER_LINK_INFO_CMD_FLAG) {
528 		ret = device_create_file(adev->dev, &dev_attr_xgmi_port_num);
529 		if (ret)
530 			dev_err(adev->dev, "failed to create xgmi_port_num\n");
531 	}
532 
533 	/* Create sysfs link to hive info folder on the first device */
534 	if (hive->kobj.parent != (&adev->dev->kobj)) {
535 		ret = sysfs_create_link(&adev->dev->kobj, &hive->kobj,
536 					"xgmi_hive_info");
537 		if (ret) {
538 			dev_err(adev->dev, "XGMI: Failed to create link to hive info");
539 			goto remove_file;
540 		}
541 	}
542 
543 	sprintf(node, "node%d", atomic_read(&hive->number_devices));
544 	/* Create sysfs link form the hive folder to yourself */
545 	ret = sysfs_create_link(&hive->kobj, &adev->dev->kobj, node);
546 	if (ret) {
547 		dev_err(adev->dev, "XGMI: Failed to create link from hive info");
548 		goto remove_link;
549 	}
550 
551 	goto success;
552 
553 
554 remove_link:
555 	sysfs_remove_link(&adev->dev->kobj, adev_to_drm(adev)->unique);
556 
557 remove_file:
558 	device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
559 	device_remove_file(adev->dev, &dev_attr_xgmi_physical_id);
560 	device_remove_file(adev->dev, &dev_attr_xgmi_error);
561 	device_remove_file(adev->dev, &dev_attr_xgmi_num_hops);
562 	device_remove_file(adev->dev, &dev_attr_xgmi_num_links);
563 	if (adev->psp.xgmi_context.xgmi_ta_caps & EXTEND_PEER_LINK_INFO_CMD_FLAG)
564 		device_remove_file(adev->dev, &dev_attr_xgmi_port_num);
565 
566 success:
567 	return ret;
568 }
569 
570 static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev,
571 					  struct amdgpu_hive_info *hive)
572 {
573 	char node[10];
574 	memset(node, 0, sizeof(node));
575 
576 	device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
577 	device_remove_file(adev->dev, &dev_attr_xgmi_physical_id);
578 	device_remove_file(adev->dev, &dev_attr_xgmi_error);
579 	device_remove_file(adev->dev, &dev_attr_xgmi_num_hops);
580 	device_remove_file(adev->dev, &dev_attr_xgmi_num_links);
581 	if (adev->psp.xgmi_context.xgmi_ta_caps & EXTEND_PEER_LINK_INFO_CMD_FLAG)
582 		device_remove_file(adev->dev, &dev_attr_xgmi_port_num);
583 
584 	if (hive->kobj.parent != (&adev->dev->kobj))
585 		sysfs_remove_link(&adev->dev->kobj,"xgmi_hive_info");
586 
587 	sprintf(node, "node%d", atomic_read(&hive->number_devices));
588 	sysfs_remove_link(&hive->kobj, node);
589 
590 }
591 
592 
593 
594 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev)
595 {
596 	struct amdgpu_hive_info *hive = NULL;
597 	int ret;
598 
599 	if (!adev->gmc.xgmi.hive_id)
600 		return NULL;
601 
602 	if (adev->hive) {
603 		kobject_get(&adev->hive->kobj);
604 		return adev->hive;
605 	}
606 
607 	mutex_lock(&xgmi_mutex);
608 
609 	list_for_each_entry(hive, &xgmi_hive_list, node)  {
610 		if (hive->hive_id == adev->gmc.xgmi.hive_id)
611 			goto pro_end;
612 	}
613 
614 	hive = kzalloc(sizeof(*hive), GFP_KERNEL);
615 	if (!hive) {
616 		dev_err(adev->dev, "XGMI: allocation failed\n");
617 		ret = -ENOMEM;
618 		hive = NULL;
619 		goto pro_end;
620 	}
621 
622 	/* initialize new hive if not exist */
623 	ret = kobject_init_and_add(&hive->kobj,
624 			&amdgpu_xgmi_hive_type,
625 			&adev->dev->kobj,
626 			"%s", "xgmi_hive_info");
627 	if (ret) {
628 		dev_err(adev->dev, "XGMI: failed initializing kobject for xgmi hive\n");
629 		kobject_put(&hive->kobj);
630 		hive = NULL;
631 		goto pro_end;
632 	}
633 
634 	/**
635 	 * Only init hive->reset_domain for none SRIOV configuration. For SRIOV,
636 	 * Host driver decide how to reset the GPU either through FLR or chain reset.
637 	 * Guest side will get individual notifications from the host for the FLR
638 	 * if necessary.
639 	 */
640 	if (!amdgpu_sriov_vf(adev)) {
641 	/**
642 	 * Avoid recreating reset domain when hive is reconstructed for the case
643 	 * of reset the devices in the XGMI hive during probe for passthrough GPU
644 	 * See https://www.spinics.net/lists/amd-gfx/msg58836.html
645 	 */
646 		if (adev->reset_domain->type != XGMI_HIVE) {
647 			hive->reset_domain =
648 				amdgpu_reset_create_reset_domain(XGMI_HIVE, "amdgpu-reset-hive");
649 			if (!hive->reset_domain) {
650 				dev_err(adev->dev, "XGMI: failed initializing reset domain for xgmi hive\n");
651 				ret = -ENOMEM;
652 				kobject_put(&hive->kobj);
653 				hive = NULL;
654 				goto pro_end;
655 			}
656 		} else {
657 			amdgpu_reset_get_reset_domain(adev->reset_domain);
658 			hive->reset_domain = adev->reset_domain;
659 		}
660 	}
661 
662 	hive->hive_id = adev->gmc.xgmi.hive_id;
663 	INIT_LIST_HEAD(&hive->device_list);
664 	INIT_LIST_HEAD(&hive->node);
665 	mutex_init(&hive->hive_lock);
666 	atomic_set(&hive->number_devices, 0);
667 	task_barrier_init(&hive->tb);
668 	hive->pstate = AMDGPU_XGMI_PSTATE_UNKNOWN;
669 	hive->hi_req_gpu = NULL;
670 	atomic_set(&hive->requested_nps_mode, UNKNOWN_MEMORY_PARTITION_MODE);
671 
672 	/*
673 	 * hive pstate on boot is high in vega20 so we have to go to low
674 	 * pstate on after boot.
675 	 */
676 	hive->hi_req_count = AMDGPU_MAX_XGMI_DEVICE_PER_HIVE;
677 	list_add_tail(&hive->node, &xgmi_hive_list);
678 
679 pro_end:
680 	if (hive)
681 		kobject_get(&hive->kobj);
682 	mutex_unlock(&xgmi_mutex);
683 	return hive;
684 }
685 
686 void amdgpu_put_xgmi_hive(struct amdgpu_hive_info *hive)
687 {
688 	if (hive)
689 		kobject_put(&hive->kobj);
690 }
691 
692 int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate)
693 {
694 	int ret = 0;
695 	struct amdgpu_hive_info *hive;
696 	struct amdgpu_device *request_adev;
697 	bool is_hi_req = pstate == AMDGPU_XGMI_PSTATE_MAX_VEGA20;
698 	bool init_low;
699 
700 	hive = amdgpu_get_xgmi_hive(adev);
701 	if (!hive)
702 		return 0;
703 
704 	request_adev = hive->hi_req_gpu ? hive->hi_req_gpu : adev;
705 	init_low = hive->pstate == AMDGPU_XGMI_PSTATE_UNKNOWN;
706 	amdgpu_put_xgmi_hive(hive);
707 	/* fw bug so temporarily disable pstate switching */
708 	return 0;
709 
710 	if (!hive || adev->asic_type != CHIP_VEGA20)
711 		return 0;
712 
713 	mutex_lock(&hive->hive_lock);
714 
715 	if (is_hi_req)
716 		hive->hi_req_count++;
717 	else
718 		hive->hi_req_count--;
719 
720 	/*
721 	 * Vega20 only needs single peer to request pstate high for the hive to
722 	 * go high but all peers must request pstate low for the hive to go low
723 	 */
724 	if (hive->pstate == pstate ||
725 			(!is_hi_req && hive->hi_req_count && !init_low))
726 		goto out;
727 
728 	dev_dbg(request_adev->dev, "Set xgmi pstate %d.\n", pstate);
729 
730 	ret = amdgpu_dpm_set_xgmi_pstate(request_adev, pstate);
731 	if (ret) {
732 		dev_err(request_adev->dev,
733 			"XGMI: Set pstate failure on device %llx, hive %llx, ret %d",
734 			request_adev->gmc.xgmi.node_id,
735 			request_adev->gmc.xgmi.hive_id, ret);
736 		goto out;
737 	}
738 
739 	if (init_low)
740 		hive->pstate = hive->hi_req_count ?
741 					hive->pstate : AMDGPU_XGMI_PSTATE_MIN;
742 	else {
743 		hive->pstate = pstate;
744 		hive->hi_req_gpu = pstate != AMDGPU_XGMI_PSTATE_MIN ?
745 							adev : NULL;
746 	}
747 out:
748 	mutex_unlock(&hive->hive_lock);
749 	return ret;
750 }
751 
752 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
753 {
754 	int ret;
755 
756 	if (amdgpu_sriov_vf(adev))
757 		return 0;
758 
759 	/* Each psp need to set the latest topology */
760 	ret = psp_xgmi_set_topology_info(&adev->psp,
761 					 atomic_read(&hive->number_devices),
762 					 &adev->psp.xgmi_context.top_info);
763 	if (ret)
764 		dev_err(adev->dev,
765 			"XGMI: Set topology failure on device %llx, hive %llx, ret %d",
766 			adev->gmc.xgmi.node_id,
767 			adev->gmc.xgmi.hive_id, ret);
768 
769 	return ret;
770 }
771 
772 
773 /*
774  * NOTE psp_xgmi_node_info.num_hops layout is as follows:
775  * num_hops[7:6] = link type (0 = xGMI2, 1 = xGMI3, 2/3 = reserved)
776  * num_hops[5:3] = reserved
777  * num_hops[2:0] = number of hops
778  */
779 int amdgpu_xgmi_get_hops_count(struct amdgpu_device *adev,
780 		struct amdgpu_device *peer_adev)
781 {
782 	struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
783 	uint8_t num_hops_mask = 0x7;
784 	int i;
785 
786 	for (i = 0 ; i < top->num_nodes; ++i)
787 		if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
788 			return top->nodes[i].num_hops & num_hops_mask;
789 	return	-EINVAL;
790 }
791 
792 int amdgpu_xgmi_get_num_links(struct amdgpu_device *adev,
793 		struct amdgpu_device *peer_adev)
794 {
795 	struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
796 	int i;
797 
798 	for (i = 0 ; i < top->num_nodes; ++i)
799 		if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
800 			return top->nodes[i].num_links;
801 	return	-EINVAL;
802 }
803 
804 bool amdgpu_xgmi_get_is_sharing_enabled(struct amdgpu_device *adev,
805 					struct amdgpu_device *peer_adev)
806 {
807 	struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
808 	int i;
809 
810 	/* Sharing should always be enabled for non-SRIOV. */
811 	if (!amdgpu_sriov_vf(adev))
812 		return true;
813 
814 	for (i = 0 ; i < top->num_nodes; ++i)
815 		if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
816 			return !!top->nodes[i].is_sharing_enabled;
817 
818 	return false;
819 }
820 
821 /*
822  * Devices that support extended data require the entire hive to initialize with
823  * the shared memory buffer flag set.
824  *
825  * Hive locks and conditions apply - see amdgpu_xgmi_add_device
826  */
827 static int amdgpu_xgmi_initialize_hive_get_data_partition(struct amdgpu_hive_info *hive,
828 							bool set_extended_data)
829 {
830 	struct amdgpu_device *tmp_adev;
831 	int ret;
832 
833 	list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
834 		ret = psp_xgmi_initialize(&tmp_adev->psp, set_extended_data, false);
835 		if (ret) {
836 			dev_err(tmp_adev->dev,
837 				"XGMI: Failed to initialize xgmi session for data partition %i\n",
838 				set_extended_data);
839 			return ret;
840 		}
841 
842 	}
843 
844 	return 0;
845 }
846 
847 static void amdgpu_xgmi_fill_topology_info(struct amdgpu_device *adev,
848 	struct amdgpu_device *peer_adev)
849 {
850 	struct psp_xgmi_topology_info *top_info = &adev->psp.xgmi_context.top_info;
851 	struct psp_xgmi_topology_info *peer_info = &peer_adev->psp.xgmi_context.top_info;
852 
853 	for (int i = 0; i < peer_info->num_nodes; i++) {
854 		if (peer_info->nodes[i].node_id == adev->gmc.xgmi.node_id) {
855 			for (int j = 0; j < top_info->num_nodes; j++) {
856 				if (top_info->nodes[j].node_id == peer_adev->gmc.xgmi.node_id) {
857 					peer_info->nodes[i].num_hops = top_info->nodes[j].num_hops;
858 					peer_info->nodes[i].is_sharing_enabled =
859 							top_info->nodes[j].is_sharing_enabled;
860 					peer_info->nodes[i].num_links =
861 							top_info->nodes[j].num_links;
862 					return;
863 				}
864 			}
865 		}
866 	}
867 }
868 
869 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
870 {
871 	struct psp_xgmi_topology_info *top_info;
872 	struct amdgpu_hive_info *hive;
873 	struct amdgpu_xgmi	*entry;
874 	struct amdgpu_device *tmp_adev = NULL;
875 
876 	int count = 0, ret = 0;
877 
878 	if (!adev->gmc.xgmi.supported)
879 		return 0;
880 
881 	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
882 		ret = psp_xgmi_initialize(&adev->psp, false, true);
883 		if (ret) {
884 			dev_err(adev->dev,
885 				"XGMI: Failed to initialize xgmi session\n");
886 			return ret;
887 		}
888 
889 		ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
890 		if (ret) {
891 			dev_err(adev->dev,
892 				"XGMI: Failed to get hive id\n");
893 			return ret;
894 		}
895 
896 		ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
897 		if (ret) {
898 			dev_err(adev->dev,
899 				"XGMI: Failed to get node id\n");
900 			return ret;
901 		}
902 	} else {
903 		adev->gmc.xgmi.hive_id = 16;
904 		adev->gmc.xgmi.node_id = adev->gmc.xgmi.physical_node_id + 16;
905 	}
906 
907 	hive = amdgpu_get_xgmi_hive(adev);
908 	if (!hive) {
909 		ret = -EINVAL;
910 		dev_err(adev->dev,
911 			"XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n",
912 			adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id);
913 		goto exit;
914 	}
915 	mutex_lock(&hive->hive_lock);
916 
917 	top_info = &adev->psp.xgmi_context.top_info;
918 
919 	list_add_tail(&adev->gmc.xgmi.head, &hive->device_list);
920 	list_for_each_entry(entry, &hive->device_list, head)
921 		top_info->nodes[count++].node_id = entry->node_id;
922 	top_info->num_nodes = count;
923 	atomic_set(&hive->number_devices, count);
924 
925 	task_barrier_add_task(&hive->tb);
926 
927 	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
928 		list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
929 			/* update node list for other device in the hive */
930 			if (tmp_adev != adev) {
931 				top_info = &tmp_adev->psp.xgmi_context.top_info;
932 				top_info->nodes[count - 1].node_id =
933 					adev->gmc.xgmi.node_id;
934 				top_info->num_nodes = count;
935 			}
936 			ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
937 			if (ret)
938 				goto exit_unlock;
939 		}
940 
941 		if (amdgpu_sriov_vf(adev) &&
942 			adev->psp.xgmi_context.xgmi_ta_caps & EXTEND_PEER_LINK_INFO_CMD_FLAG) {
943 			/* only get topology for VF being init if it can support full duplex */
944 			ret = psp_xgmi_get_topology_info(&adev->psp, count,
945 						&adev->psp.xgmi_context.top_info, false);
946 			if (ret) {
947 				dev_err(adev->dev,
948 					"XGMI: Get topology failure on device %llx, hive %llx, ret %d",
949 					adev->gmc.xgmi.node_id,
950 					adev->gmc.xgmi.hive_id, ret);
951 				/* To do: continue with some node failed or disable the whole hive*/
952 				goto exit_unlock;
953 			}
954 
955 			/* fill the topology info for peers instead of getting from PSP */
956 			list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
957 				amdgpu_xgmi_fill_topology_info(adev, tmp_adev);
958 			}
959 		} else {
960 			/* get latest topology info for each device from psp */
961 			list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
962 				ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count,
963 					&tmp_adev->psp.xgmi_context.top_info, false);
964 				if (ret) {
965 					dev_err(tmp_adev->dev,
966 						"XGMI: Get topology failure on device %llx, hive %llx, ret %d",
967 						tmp_adev->gmc.xgmi.node_id,
968 						tmp_adev->gmc.xgmi.hive_id, ret);
969 					/* To do : continue with some node failed or disable the whole hive */
970 					goto exit_unlock;
971 				}
972 			}
973 		}
974 
975 		/* get topology again for hives that support extended data */
976 		if (adev->psp.xgmi_context.supports_extended_data) {
977 
978 			/* initialize the hive to get extended data.  */
979 			ret = amdgpu_xgmi_initialize_hive_get_data_partition(hive, true);
980 			if (ret)
981 				goto exit_unlock;
982 
983 			/* get the extended data. */
984 			list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
985 				ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count,
986 						&tmp_adev->psp.xgmi_context.top_info, true);
987 				if (ret) {
988 					dev_err(tmp_adev->dev,
989 						"XGMI: Get topology for extended data failure on device %llx, hive %llx, ret %d",
990 						tmp_adev->gmc.xgmi.node_id,
991 						tmp_adev->gmc.xgmi.hive_id, ret);
992 					goto exit_unlock;
993 				}
994 			}
995 
996 			/* initialize the hive to get non-extended data for the next round. */
997 			ret = amdgpu_xgmi_initialize_hive_get_data_partition(hive, false);
998 			if (ret)
999 				goto exit_unlock;
1000 
1001 		}
1002 	}
1003 
1004 	if (!ret)
1005 		ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive);
1006 
1007 exit_unlock:
1008 	mutex_unlock(&hive->hive_lock);
1009 exit:
1010 	if (!ret) {
1011 		adev->hive = hive;
1012 		dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n",
1013 			 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id);
1014 	} else {
1015 		amdgpu_put_xgmi_hive(hive);
1016 		dev_err(adev->dev, "XGMI: Failed to add node %d, hive 0x%llx ret: %d\n",
1017 			adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id,
1018 			ret);
1019 	}
1020 
1021 	return ret;
1022 }
1023 
1024 int amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
1025 {
1026 	struct amdgpu_hive_info *hive = adev->hive;
1027 
1028 	if (!adev->gmc.xgmi.supported)
1029 		return -EINVAL;
1030 
1031 	if (!hive)
1032 		return -EINVAL;
1033 
1034 	mutex_lock(&hive->hive_lock);
1035 	task_barrier_rem_task(&hive->tb);
1036 	amdgpu_xgmi_sysfs_rem_dev_info(adev, hive);
1037 	if (hive->hi_req_gpu == adev)
1038 		hive->hi_req_gpu = NULL;
1039 	list_del(&adev->gmc.xgmi.head);
1040 	mutex_unlock(&hive->hive_lock);
1041 
1042 	amdgpu_put_xgmi_hive(hive);
1043 	adev->hive = NULL;
1044 
1045 	if (atomic_dec_return(&hive->number_devices) == 0) {
1046 		/* Remove the hive from global hive list */
1047 		mutex_lock(&xgmi_mutex);
1048 		list_del(&hive->node);
1049 		mutex_unlock(&xgmi_mutex);
1050 
1051 		amdgpu_put_xgmi_hive(hive);
1052 	}
1053 
1054 	return 0;
1055 }
1056 
1057 static int xgmi_v6_4_0_aca_bank_parser(struct aca_handle *handle, struct aca_bank *bank,
1058 				       enum aca_smu_type type, void *data)
1059 {
1060 	struct amdgpu_device *adev = handle->adev;
1061 	struct aca_bank_info info;
1062 	const char *error_str;
1063 	u64 status, count;
1064 	int ret, ext_error_code;
1065 
1066 	ret = aca_bank_info_decode(bank, &info);
1067 	if (ret)
1068 		return ret;
1069 
1070 	status = bank->regs[ACA_REG_IDX_STATUS];
1071 	ext_error_code = ACA_REG__STATUS__ERRORCODEEXT(status);
1072 
1073 	error_str = ext_error_code < ARRAY_SIZE(xgmi_v6_4_0_ras_error_code_ext) ?
1074 		xgmi_v6_4_0_ras_error_code_ext[ext_error_code] : NULL;
1075 	if (error_str)
1076 		dev_info(adev->dev, "%s detected\n", error_str);
1077 
1078 	count = ACA_REG__MISC0__ERRCNT(bank->regs[ACA_REG_IDX_MISC0]);
1079 
1080 	switch (type) {
1081 	case ACA_SMU_TYPE_UE:
1082 		if (ext_error_code != 0 && ext_error_code != 9)
1083 			count = 0ULL;
1084 
1085 		ret = aca_error_cache_log_bank_error(handle, &info, ACA_ERROR_TYPE_UE, count);
1086 		break;
1087 	case ACA_SMU_TYPE_CE:
1088 		count = ext_error_code == 6 ? count : 0ULL;
1089 		ret = aca_error_cache_log_bank_error(handle, &info, ACA_ERROR_TYPE_CE, count);
1090 		break;
1091 	default:
1092 		return -EINVAL;
1093 	}
1094 
1095 	return ret;
1096 }
1097 
1098 static const struct aca_bank_ops xgmi_v6_4_0_aca_bank_ops = {
1099 	.aca_bank_parser = xgmi_v6_4_0_aca_bank_parser,
1100 };
1101 
1102 static const struct aca_info xgmi_v6_4_0_aca_info = {
1103 	.hwip = ACA_HWIP_TYPE_PCS_XGMI,
1104 	.mask = ACA_ERROR_UE_MASK | ACA_ERROR_CE_MASK,
1105 	.bank_ops = &xgmi_v6_4_0_aca_bank_ops,
1106 };
1107 
1108 static int amdgpu_xgmi_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
1109 {
1110 	int r;
1111 
1112 	if (!adev->gmc.xgmi.supported ||
1113 	    adev->gmc.xgmi.num_physical_nodes == 0)
1114 		return 0;
1115 
1116 	amdgpu_ras_reset_error_count(adev, AMDGPU_RAS_BLOCK__XGMI_WAFL);
1117 
1118 	r = amdgpu_ras_block_late_init(adev, ras_block);
1119 	if (r)
1120 		return r;
1121 
1122 	switch (amdgpu_ip_version(adev, XGMI_HWIP, 0)) {
1123 	case IP_VERSION(6, 4, 0):
1124 		r = amdgpu_ras_bind_aca(adev, AMDGPU_RAS_BLOCK__XGMI_WAFL,
1125 					&xgmi_v6_4_0_aca_info, NULL);
1126 		if (r)
1127 			goto late_fini;
1128 		break;
1129 	default:
1130 		break;
1131 	}
1132 
1133 	return 0;
1134 
1135 late_fini:
1136 	amdgpu_ras_block_late_fini(adev, ras_block);
1137 
1138 	return r;
1139 }
1140 
1141 uint64_t amdgpu_xgmi_get_relative_phy_addr(struct amdgpu_device *adev,
1142 					   uint64_t addr)
1143 {
1144 	struct amdgpu_xgmi *xgmi = &adev->gmc.xgmi;
1145 	return (addr + xgmi->physical_node_id * xgmi->node_segment_size);
1146 }
1147 
1148 static void pcs_clear_status(struct amdgpu_device *adev, uint32_t pcs_status_reg)
1149 {
1150 	WREG32_PCIE(pcs_status_reg, 0xFFFFFFFF);
1151 	WREG32_PCIE(pcs_status_reg, 0);
1152 }
1153 
1154 static void amdgpu_xgmi_legacy_reset_ras_error_count(struct amdgpu_device *adev)
1155 {
1156 	uint32_t i;
1157 
1158 	switch (adev->asic_type) {
1159 	case CHIP_ARCTURUS:
1160 		for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_arct); i++)
1161 			pcs_clear_status(adev,
1162 					 xgmi_pcs_err_status_reg_arct[i]);
1163 		break;
1164 	case CHIP_VEGA20:
1165 		for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_vg20); i++)
1166 			pcs_clear_status(adev,
1167 					 xgmi_pcs_err_status_reg_vg20[i]);
1168 		break;
1169 	case CHIP_ALDEBARAN:
1170 		for (i = 0; i < ARRAY_SIZE(xgmi3x16_pcs_err_status_reg_aldebaran); i++)
1171 			pcs_clear_status(adev,
1172 					 xgmi3x16_pcs_err_status_reg_aldebaran[i]);
1173 		for (i = 0; i < ARRAY_SIZE(walf_pcs_err_status_reg_aldebaran); i++)
1174 			pcs_clear_status(adev,
1175 					 walf_pcs_err_status_reg_aldebaran[i]);
1176 		break;
1177 	default:
1178 		break;
1179 	}
1180 
1181 	switch (amdgpu_ip_version(adev, XGMI_HWIP, 0)) {
1182 	case IP_VERSION(6, 4, 0):
1183 		for (i = 0; i < ARRAY_SIZE(xgmi3x16_pcs_err_status_reg_v6_4); i++)
1184 			pcs_clear_status(adev,
1185 					xgmi3x16_pcs_err_status_reg_v6_4[i]);
1186 		break;
1187 	default:
1188 		break;
1189 	}
1190 }
1191 
1192 static void __xgmi_v6_4_0_reset_error_count(struct amdgpu_device *adev, int xgmi_inst, u64 mca_base)
1193 {
1194 	WREG64_MCA(xgmi_inst, mca_base, ACA_REG_IDX_STATUS, 0ULL);
1195 }
1196 
1197 static void xgmi_v6_4_0_reset_error_count(struct amdgpu_device *adev, int xgmi_inst)
1198 {
1199 	int i;
1200 
1201 	for (i = 0; i < ARRAY_SIZE(xgmi_v6_4_0_mca_base_array); i++)
1202 		__xgmi_v6_4_0_reset_error_count(adev, xgmi_inst, xgmi_v6_4_0_mca_base_array[i]);
1203 }
1204 
1205 static void xgmi_v6_4_0_reset_ras_error_count(struct amdgpu_device *adev)
1206 {
1207 	int i;
1208 
1209 	for_each_inst(i, adev->aid_mask)
1210 		xgmi_v6_4_0_reset_error_count(adev, i);
1211 }
1212 
1213 static void amdgpu_xgmi_reset_ras_error_count(struct amdgpu_device *adev)
1214 {
1215 	switch (amdgpu_ip_version(adev, XGMI_HWIP, 0)) {
1216 	case IP_VERSION(6, 4, 0):
1217 		xgmi_v6_4_0_reset_ras_error_count(adev);
1218 		break;
1219 	default:
1220 		amdgpu_xgmi_legacy_reset_ras_error_count(adev);
1221 		break;
1222 	}
1223 }
1224 
1225 static int amdgpu_xgmi_query_pcs_error_status(struct amdgpu_device *adev,
1226 					      uint32_t value,
1227 						  uint32_t mask_value,
1228 					      uint32_t *ue_count,
1229 					      uint32_t *ce_count,
1230 					      bool is_xgmi_pcs,
1231 						  bool check_mask)
1232 {
1233 	int i;
1234 	int ue_cnt = 0;
1235 	const struct amdgpu_pcs_ras_field *pcs_ras_fields = NULL;
1236 	uint32_t field_array_size = 0;
1237 
1238 	if (is_xgmi_pcs) {
1239 		if (amdgpu_ip_version(adev, XGMI_HWIP, 0) ==
1240 		    IP_VERSION(6, 1, 0) ||
1241 		    amdgpu_ip_version(adev, XGMI_HWIP, 0) ==
1242 		    IP_VERSION(6, 4, 0)) {
1243 			pcs_ras_fields = &xgmi3x16_pcs_ras_fields[0];
1244 			field_array_size = ARRAY_SIZE(xgmi3x16_pcs_ras_fields);
1245 		} else {
1246 			pcs_ras_fields = &xgmi_pcs_ras_fields[0];
1247 			field_array_size = ARRAY_SIZE(xgmi_pcs_ras_fields);
1248 		}
1249 	} else {
1250 		pcs_ras_fields = &wafl_pcs_ras_fields[0];
1251 		field_array_size = ARRAY_SIZE(wafl_pcs_ras_fields);
1252 	}
1253 
1254 	if (check_mask)
1255 		value = value & ~mask_value;
1256 
1257 	/* query xgmi/walf pcs error status,
1258 	 * only ue is supported */
1259 	for (i = 0; value && i < field_array_size; i++) {
1260 		ue_cnt = (value &
1261 				pcs_ras_fields[i].pcs_err_mask) >>
1262 				pcs_ras_fields[i].pcs_err_shift;
1263 		if (ue_cnt) {
1264 			dev_info(adev->dev, "%s detected\n",
1265 				 pcs_ras_fields[i].err_name);
1266 			*ue_count += ue_cnt;
1267 		}
1268 
1269 		/* reset bit value if the bit is checked */
1270 		value &= ~(pcs_ras_fields[i].pcs_err_mask);
1271 	}
1272 
1273 	return 0;
1274 }
1275 
1276 static void amdgpu_xgmi_legacy_query_ras_error_count(struct amdgpu_device *adev,
1277 						     void *ras_error_status)
1278 {
1279 	struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
1280 	int i, supported = 1;
1281 	uint32_t data, mask_data = 0;
1282 	uint32_t ue_cnt = 0, ce_cnt = 0;
1283 
1284 	if (!amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__XGMI_WAFL))
1285 		return ;
1286 
1287 	err_data->ue_count = 0;
1288 	err_data->ce_count = 0;
1289 
1290 	switch (adev->asic_type) {
1291 	case CHIP_ARCTURUS:
1292 		/* check xgmi pcs error */
1293 		for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_arct); i++) {
1294 			data = RREG32_PCIE(xgmi_pcs_err_status_reg_arct[i]);
1295 			if (data)
1296 				amdgpu_xgmi_query_pcs_error_status(adev, data,
1297 						mask_data, &ue_cnt, &ce_cnt, true, false);
1298 		}
1299 		/* check wafl pcs error */
1300 		for (i = 0; i < ARRAY_SIZE(wafl_pcs_err_status_reg_arct); i++) {
1301 			data = RREG32_PCIE(wafl_pcs_err_status_reg_arct[i]);
1302 			if (data)
1303 				amdgpu_xgmi_query_pcs_error_status(adev, data,
1304 						mask_data, &ue_cnt, &ce_cnt, false, false);
1305 		}
1306 		break;
1307 	case CHIP_VEGA20:
1308 		/* check xgmi pcs error */
1309 		for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_vg20); i++) {
1310 			data = RREG32_PCIE(xgmi_pcs_err_status_reg_vg20[i]);
1311 			if (data)
1312 				amdgpu_xgmi_query_pcs_error_status(adev, data,
1313 						mask_data, &ue_cnt, &ce_cnt, true, false);
1314 		}
1315 		/* check wafl pcs error */
1316 		for (i = 0; i < ARRAY_SIZE(wafl_pcs_err_status_reg_vg20); i++) {
1317 			data = RREG32_PCIE(wafl_pcs_err_status_reg_vg20[i]);
1318 			if (data)
1319 				amdgpu_xgmi_query_pcs_error_status(adev, data,
1320 						mask_data, &ue_cnt, &ce_cnt, false, false);
1321 		}
1322 		break;
1323 	case CHIP_ALDEBARAN:
1324 		/* check xgmi3x16 pcs error */
1325 		for (i = 0; i < ARRAY_SIZE(xgmi3x16_pcs_err_status_reg_aldebaran); i++) {
1326 			data = RREG32_PCIE(xgmi3x16_pcs_err_status_reg_aldebaran[i]);
1327 			mask_data =
1328 				RREG32_PCIE(xgmi3x16_pcs_err_noncorrectable_mask_reg_aldebaran[i]);
1329 			if (data)
1330 				amdgpu_xgmi_query_pcs_error_status(adev, data,
1331 						mask_data, &ue_cnt, &ce_cnt, true, true);
1332 		}
1333 		/* check wafl pcs error */
1334 		for (i = 0; i < ARRAY_SIZE(walf_pcs_err_status_reg_aldebaran); i++) {
1335 			data = RREG32_PCIE(walf_pcs_err_status_reg_aldebaran[i]);
1336 			mask_data =
1337 				RREG32_PCIE(walf_pcs_err_noncorrectable_mask_reg_aldebaran[i]);
1338 			if (data)
1339 				amdgpu_xgmi_query_pcs_error_status(adev, data,
1340 						mask_data, &ue_cnt, &ce_cnt, false, true);
1341 		}
1342 		break;
1343 	default:
1344 		supported = 0;
1345 		break;
1346 	}
1347 
1348 	switch (amdgpu_ip_version(adev, XGMI_HWIP, 0)) {
1349 	case IP_VERSION(6, 4, 0):
1350 		/* check xgmi3x16 pcs error */
1351 		for (i = 0; i < ARRAY_SIZE(xgmi3x16_pcs_err_status_reg_v6_4); i++) {
1352 			data = RREG32_PCIE(xgmi3x16_pcs_err_status_reg_v6_4[i]);
1353 			mask_data =
1354 				RREG32_PCIE(xgmi3x16_pcs_err_noncorrectable_mask_reg_v6_4[i]);
1355 			if (data)
1356 				amdgpu_xgmi_query_pcs_error_status(adev, data,
1357 						mask_data, &ue_cnt, &ce_cnt, true, true);
1358 		}
1359 		break;
1360 	default:
1361 		if (!supported)
1362 			dev_warn(adev->dev, "XGMI RAS error query not supported");
1363 		break;
1364 	}
1365 
1366 	amdgpu_ras_reset_error_count(adev, AMDGPU_RAS_BLOCK__XGMI_WAFL);
1367 
1368 	err_data->ue_count += ue_cnt;
1369 	err_data->ce_count += ce_cnt;
1370 }
1371 
1372 static enum aca_error_type xgmi_v6_4_0_pcs_mca_get_error_type(struct amdgpu_device *adev, u64 status)
1373 {
1374 	const char *error_str;
1375 	int ext_error_code;
1376 
1377 	ext_error_code = ACA_REG__STATUS__ERRORCODEEXT(status);
1378 
1379 	error_str = ext_error_code < ARRAY_SIZE(xgmi_v6_4_0_ras_error_code_ext) ?
1380 		xgmi_v6_4_0_ras_error_code_ext[ext_error_code] : NULL;
1381 	if (error_str)
1382 		dev_info(adev->dev, "%s detected\n", error_str);
1383 
1384 	switch (ext_error_code) {
1385 	case 0:
1386 		return ACA_ERROR_TYPE_UE;
1387 	case 6:
1388 		return ACA_ERROR_TYPE_CE;
1389 	default:
1390 		return -EINVAL;
1391 	}
1392 
1393 	return -EINVAL;
1394 }
1395 
1396 static void __xgmi_v6_4_0_query_error_count(struct amdgpu_device *adev, struct amdgpu_smuio_mcm_config_info *mcm_info,
1397 					    u64 mca_base, struct ras_err_data *err_data)
1398 {
1399 	int xgmi_inst = mcm_info->die_id;
1400 	u64 status = 0;
1401 
1402 	status = RREG64_MCA(xgmi_inst, mca_base, ACA_REG_IDX_STATUS);
1403 	if (!ACA_REG__STATUS__VAL(status))
1404 		return;
1405 
1406 	switch (xgmi_v6_4_0_pcs_mca_get_error_type(adev, status)) {
1407 	case ACA_ERROR_TYPE_UE:
1408 		amdgpu_ras_error_statistic_ue_count(err_data, mcm_info, 1ULL);
1409 		break;
1410 	case ACA_ERROR_TYPE_CE:
1411 		amdgpu_ras_error_statistic_ce_count(err_data, mcm_info, 1ULL);
1412 		break;
1413 	default:
1414 		break;
1415 	}
1416 
1417 	WREG64_MCA(xgmi_inst, mca_base, ACA_REG_IDX_STATUS, 0ULL);
1418 }
1419 
1420 static void xgmi_v6_4_0_query_error_count(struct amdgpu_device *adev, int xgmi_inst, struct ras_err_data *err_data)
1421 {
1422 	struct amdgpu_smuio_mcm_config_info mcm_info = {
1423 		.socket_id = adev->smuio.funcs->get_socket_id(adev),
1424 		.die_id = xgmi_inst,
1425 	};
1426 	int i;
1427 
1428 	for (i = 0; i < ARRAY_SIZE(xgmi_v6_4_0_mca_base_array); i++)
1429 		__xgmi_v6_4_0_query_error_count(adev, &mcm_info, xgmi_v6_4_0_mca_base_array[i], err_data);
1430 }
1431 
1432 static void xgmi_v6_4_0_query_ras_error_count(struct amdgpu_device *adev, void *ras_error_status)
1433 {
1434 	struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
1435 	int i;
1436 
1437 	for_each_inst(i, adev->aid_mask)
1438 		xgmi_v6_4_0_query_error_count(adev, i, err_data);
1439 }
1440 
1441 static void amdgpu_xgmi_query_ras_error_count(struct amdgpu_device *adev,
1442 					      void *ras_error_status)
1443 {
1444 	switch (amdgpu_ip_version(adev, XGMI_HWIP, 0)) {
1445 	case IP_VERSION(6, 4, 0):
1446 		xgmi_v6_4_0_query_ras_error_count(adev, ras_error_status);
1447 		break;
1448 	default:
1449 		amdgpu_xgmi_legacy_query_ras_error_count(adev, ras_error_status);
1450 		break;
1451 	}
1452 }
1453 
1454 /* Trigger XGMI/WAFL error */
1455 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev,
1456 			void *inject_if, uint32_t instance_mask)
1457 {
1458 	int ret1, ret2;
1459 	struct ta_ras_trigger_error_input *block_info =
1460 				(struct ta_ras_trigger_error_input *)inject_if;
1461 
1462 	if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
1463 		dev_warn(adev->dev, "Failed to disallow df cstate");
1464 
1465 	ret1 = amdgpu_dpm_set_pm_policy(adev, PP_PM_POLICY_XGMI_PLPD, XGMI_PLPD_DISALLOW);
1466 	if (ret1 && ret1 != -EOPNOTSUPP)
1467 		dev_warn(adev->dev, "Failed to disallow XGMI power down");
1468 
1469 	ret2 = psp_ras_trigger_error(&adev->psp, block_info, instance_mask);
1470 
1471 	if (amdgpu_ras_intr_triggered())
1472 		return ret2;
1473 
1474 	ret1 = amdgpu_dpm_set_pm_policy(adev, PP_PM_POLICY_XGMI_PLPD, XGMI_PLPD_DEFAULT);
1475 	if (ret1 && ret1 != -EOPNOTSUPP)
1476 		dev_warn(adev->dev, "Failed to allow XGMI power down");
1477 
1478 	if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW))
1479 		dev_warn(adev->dev, "Failed to allow df cstate");
1480 
1481 	return ret2;
1482 }
1483 
1484 struct amdgpu_ras_block_hw_ops  xgmi_ras_hw_ops = {
1485 	.query_ras_error_count = amdgpu_xgmi_query_ras_error_count,
1486 	.reset_ras_error_count = amdgpu_xgmi_reset_ras_error_count,
1487 	.ras_error_inject = amdgpu_ras_error_inject_xgmi,
1488 };
1489 
1490 struct amdgpu_xgmi_ras xgmi_ras = {
1491 	.ras_block = {
1492 		.hw_ops = &xgmi_ras_hw_ops,
1493 		.ras_late_init = amdgpu_xgmi_ras_late_init,
1494 	},
1495 };
1496 
1497 int amdgpu_xgmi_ras_sw_init(struct amdgpu_device *adev)
1498 {
1499 	int err;
1500 	struct amdgpu_xgmi_ras *ras;
1501 
1502 	if (!adev->gmc.xgmi.ras)
1503 		return 0;
1504 
1505 	ras = adev->gmc.xgmi.ras;
1506 	err = amdgpu_ras_register_ras_block(adev, &ras->ras_block);
1507 	if (err) {
1508 		dev_err(adev->dev, "Failed to register xgmi_wafl_pcs ras block!\n");
1509 		return err;
1510 	}
1511 
1512 	strcpy(ras->ras_block.ras_comm.name, "xgmi_wafl");
1513 	ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__XGMI_WAFL;
1514 	ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
1515 	adev->gmc.xgmi.ras_if = &ras->ras_block.ras_comm;
1516 
1517 	return 0;
1518 }
1519 
1520 static void amdgpu_xgmi_reset_on_init_work(struct work_struct *work)
1521 {
1522 	struct amdgpu_hive_info *hive =
1523 		container_of(work, struct amdgpu_hive_info, reset_on_init_work);
1524 	struct amdgpu_reset_context reset_context;
1525 	struct amdgpu_device *tmp_adev;
1526 	struct list_head device_list;
1527 	int r;
1528 
1529 	mutex_lock(&hive->hive_lock);
1530 
1531 	INIT_LIST_HEAD(&device_list);
1532 	list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
1533 		list_add_tail(&tmp_adev->reset_list, &device_list);
1534 
1535 	tmp_adev = list_first_entry(&device_list, struct amdgpu_device,
1536 				    reset_list);
1537 	amdgpu_device_lock_reset_domain(tmp_adev->reset_domain);
1538 
1539 	reset_context.method = AMD_RESET_METHOD_ON_INIT;
1540 	reset_context.reset_req_dev = tmp_adev;
1541 	reset_context.hive = hive;
1542 	reset_context.reset_device_list = &device_list;
1543 	set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
1544 	set_bit(AMDGPU_SKIP_COREDUMP, &reset_context.flags);
1545 
1546 	amdgpu_reset_do_xgmi_reset_on_init(&reset_context);
1547 	mutex_unlock(&hive->hive_lock);
1548 	amdgpu_device_unlock_reset_domain(tmp_adev->reset_domain);
1549 
1550 	list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
1551 		r = amdgpu_ras_init_badpage_info(tmp_adev);
1552 		if (r && r != -EHWPOISON)
1553 			dev_err(tmp_adev->dev,
1554 				"error during bad page data initialization");
1555 	}
1556 }
1557 
1558 static void amdgpu_xgmi_schedule_reset_on_init(struct amdgpu_hive_info *hive)
1559 {
1560 	INIT_WORK(&hive->reset_on_init_work, amdgpu_xgmi_reset_on_init_work);
1561 	amdgpu_reset_domain_schedule(hive->reset_domain,
1562 				     &hive->reset_on_init_work);
1563 }
1564 
1565 int amdgpu_xgmi_reset_on_init(struct amdgpu_device *adev)
1566 {
1567 	struct amdgpu_hive_info *hive;
1568 	bool reset_scheduled;
1569 	int num_devs;
1570 
1571 	hive = amdgpu_get_xgmi_hive(adev);
1572 	if (!hive)
1573 		return -EINVAL;
1574 
1575 	mutex_lock(&hive->hive_lock);
1576 	num_devs = atomic_read(&hive->number_devices);
1577 	reset_scheduled = false;
1578 	if (num_devs == adev->gmc.xgmi.num_physical_nodes) {
1579 		amdgpu_xgmi_schedule_reset_on_init(hive);
1580 		reset_scheduled = true;
1581 	}
1582 
1583 	mutex_unlock(&hive->hive_lock);
1584 	amdgpu_put_xgmi_hive(hive);
1585 
1586 	if (reset_scheduled)
1587 		flush_work(&hive->reset_on_init_work);
1588 
1589 	return 0;
1590 }
1591 
1592 int amdgpu_xgmi_request_nps_change(struct amdgpu_device *adev,
1593 				   struct amdgpu_hive_info *hive,
1594 				   int req_nps_mode)
1595 {
1596 	struct amdgpu_device *tmp_adev;
1597 	int cur_nps_mode, r;
1598 
1599 	/* This is expected to be called only during unload of driver. The
1600 	 * request needs to be placed only once for all devices in the hive. If
1601 	 * one of them fail, revert the request for previous successful devices.
1602 	 * After placing the request, make hive mode as UNKNOWN so that other
1603 	 * devices don't request anymore.
1604 	 */
1605 	mutex_lock(&hive->hive_lock);
1606 	if (atomic_read(&hive->requested_nps_mode) ==
1607 	    UNKNOWN_MEMORY_PARTITION_MODE) {
1608 		dev_dbg(adev->dev, "Unexpected entry for hive NPS change");
1609 		mutex_unlock(&hive->hive_lock);
1610 		return 0;
1611 	}
1612 	list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
1613 		r = adev->gmc.gmc_funcs->request_mem_partition_mode(
1614 			tmp_adev, req_nps_mode);
1615 		if (r)
1616 			break;
1617 	}
1618 	if (r) {
1619 		/* Request back current mode if one of the requests failed */
1620 		cur_nps_mode =
1621 			adev->gmc.gmc_funcs->query_mem_partition_mode(tmp_adev);
1622 		list_for_each_entry_continue_reverse(
1623 			tmp_adev, &hive->device_list, gmc.xgmi.head)
1624 			adev->gmc.gmc_funcs->request_mem_partition_mode(
1625 				tmp_adev, cur_nps_mode);
1626 	}
1627 	/* Set to UNKNOWN so that other devices don't request anymore */
1628 	atomic_set(&hive->requested_nps_mode, UNKNOWN_MEMORY_PARTITION_MODE);
1629 	mutex_unlock(&hive->hive_lock);
1630 
1631 	return r;
1632 }
1633