2 * Copyright 2018 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <linux/list.h>
26 #include "amdgpu_xgmi.h"
27 #include "amdgpu_ras.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"
36 #include "amdgpu_reset.h"
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
43 static DEFINE_MUTEX(xgmi_mutex);
45 #define AMDGPU_MAX_XGMI_DEVICE_PER_HIVE 4
47 static LIST_HEAD(xgmi_hive_list);
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,
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,
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,
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,
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
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
96 static const int walf_pcs_err_status_reg_aldebaran[] = {
97 smnPCS_GOPX1_PCS_ERROR_STATUS,
98 smnPCS_GOPX1_PCS_ERROR_STATUS + 0x100000
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
106 static const struct amdgpu_pcs_ras_field xgmi_pcs_ras_fields[] = {
107 {"XGMI PCS DataLossErr",
108 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataLossErr)},
109 {"XGMI PCS TrainingErr",
110 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, TrainingErr)},
112 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, CRCErr)},
113 {"XGMI PCS BERExceededErr",
114 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, BERExceededErr)},
115 {"XGMI PCS TxMetaDataErr",
116 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, TxMetaDataErr)},
117 {"XGMI PCS ReplayBufParityErr",
118 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayBufParityErr)},
119 {"XGMI PCS DataParityErr",
120 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataParityErr)},
121 {"XGMI PCS ReplayFifoOverflowErr",
122 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayFifoOverflowErr)},
123 {"XGMI PCS ReplayFifoUnderflowErr",
124 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayFifoUnderflowErr)},
125 {"XGMI PCS ElasticFifoOverflowErr",
126 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ElasticFifoOverflowErr)},
127 {"XGMI PCS DeskewErr",
128 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DeskewErr)},
129 {"XGMI PCS DataStartupLimitErr",
130 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataStartupLimitErr)},
131 {"XGMI PCS FCInitTimeoutErr",
132 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, FCInitTimeoutErr)},
133 {"XGMI PCS RecoveryTimeoutErr",
134 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryTimeoutErr)},
135 {"XGMI PCS ReadySerialTimeoutErr",
136 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReadySerialTimeoutErr)},
137 {"XGMI PCS ReadySerialAttemptErr",
138 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReadySerialAttemptErr)},
139 {"XGMI PCS RecoveryAttemptErr",
140 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryAttemptErr)},
141 {"XGMI PCS RecoveryRelockAttemptErr",
142 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryRelockAttemptErr)},
145 static const struct amdgpu_pcs_ras_field wafl_pcs_ras_fields[] = {
146 {"WAFL PCS DataLossErr",
147 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataLossErr)},
148 {"WAFL PCS TrainingErr",
149 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, TrainingErr)},
151 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, CRCErr)},
152 {"WAFL PCS BERExceededErr",
153 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, BERExceededErr)},
154 {"WAFL PCS TxMetaDataErr",
155 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, TxMetaDataErr)},
156 {"WAFL PCS ReplayBufParityErr",
157 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayBufParityErr)},
158 {"WAFL PCS DataParityErr",
159 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataParityErr)},
160 {"WAFL PCS ReplayFifoOverflowErr",
161 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayFifoOverflowErr)},
162 {"WAFL PCS ReplayFifoUnderflowErr",
163 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayFifoUnderflowErr)},
164 {"WAFL PCS ElasticFifoOverflowErr",
165 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ElasticFifoOverflowErr)},
166 {"WAFL PCS DeskewErr",
167 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DeskewErr)},
168 {"WAFL PCS DataStartupLimitErr",
169 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataStartupLimitErr)},
170 {"WAFL PCS FCInitTimeoutErr",
171 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, FCInitTimeoutErr)},
172 {"WAFL PCS RecoveryTimeoutErr",
173 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryTimeoutErr)},
174 {"WAFL PCS ReadySerialTimeoutErr",
175 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReadySerialTimeoutErr)},
176 {"WAFL PCS ReadySerialAttemptErr",
177 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReadySerialAttemptErr)},
178 {"WAFL PCS RecoveryAttemptErr",
179 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryAttemptErr)},
180 {"WAFL PCS RecoveryRelockAttemptErr",
181 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryRelockAttemptErr)},
184 static const struct amdgpu_pcs_ras_field xgmi3x16_pcs_ras_fields[] = {
185 {"XGMI3X16 PCS DataLossErr",
186 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, DataLossErr)},
187 {"XGMI3X16 PCS TrainingErr",
188 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, TrainingErr)},
189 {"XGMI3X16 PCS FlowCtrlAckErr",
190 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, FlowCtrlAckErr)},
191 {"XGMI3X16 PCS RxFifoUnderflowErr",
192 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RxFifoUnderflowErr)},
193 {"XGMI3X16 PCS RxFifoOverflowErr",
194 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RxFifoOverflowErr)},
195 {"XGMI3X16 PCS CRCErr",
196 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, CRCErr)},
197 {"XGMI3X16 PCS BERExceededErr",
198 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, BERExceededErr)},
199 {"XGMI3X16 PCS TxVcidDataErr",
200 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, TxVcidDataErr)},
201 {"XGMI3X16 PCS ReplayBufParityErr",
202 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReplayBufParityErr)},
203 {"XGMI3X16 PCS DataParityErr",
204 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, DataParityErr)},
205 {"XGMI3X16 PCS ReplayFifoOverflowErr",
206 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReplayFifoOverflowErr)},
207 {"XGMI3X16 PCS ReplayFifoUnderflowErr",
208 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReplayFifoUnderflowErr)},
209 {"XGMI3X16 PCS ElasticFifoOverflowErr",
210 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ElasticFifoOverflowErr)},
211 {"XGMI3X16 PCS DeskewErr",
212 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, DeskewErr)},
213 {"XGMI3X16 PCS FlowCtrlCRCErr",
214 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, FlowCtrlCRCErr)},
215 {"XGMI3X16 PCS DataStartupLimitErr",
216 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, DataStartupLimitErr)},
217 {"XGMI3X16 PCS FCInitTimeoutErr",
218 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, FCInitTimeoutErr)},
219 {"XGMI3X16 PCS RecoveryTimeoutErr",
220 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RecoveryTimeoutErr)},
221 {"XGMI3X16 PCS ReadySerialTimeoutErr",
222 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReadySerialTimeoutErr)},
223 {"XGMI3X16 PCS ReadySerialAttemptErr",
224 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReadySerialAttemptErr)},
225 {"XGMI3X16 PCS RecoveryAttemptErr",
226 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RecoveryAttemptErr)},
227 {"XGMI3X16 PCS RecoveryRelockAttemptErr",
228 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RecoveryRelockAttemptErr)},
229 {"XGMI3X16 PCS ReplayAttemptErr",
230 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, ReplayAttemptErr)},
231 {"XGMI3X16 PCS SyncHdrErr",
232 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, SyncHdrErr)},
233 {"XGMI3X16 PCS TxReplayTimeoutErr",
234 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, TxReplayTimeoutErr)},
235 {"XGMI3X16 PCS RxReplayTimeoutErr",
236 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RxReplayTimeoutErr)},
237 {"XGMI3X16 PCS LinkSubTxTimeoutErr",
238 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, LinkSubTxTimeoutErr)},
239 {"XGMI3X16 PCS LinkSubRxTimeoutErr",
240 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, LinkSubRxTimeoutErr)},
241 {"XGMI3X16 PCS RxCMDPktErr",
242 SOC15_REG_FIELD(PCS_XGMI3X16_PCS_ERROR_STATUS, RxCMDPktErr)},
246 * DOC: AMDGPU XGMI Support
248 * XGMI is a high speed interconnect that joins multiple GPU cards
249 * into a homogeneous memory space that is organized by a collective
250 * hive ID and individual node IDs, both of which are 64-bit numbers.
252 * The file xgmi_device_id contains the unique per GPU device ID and
253 * is stored in the /sys/class/drm/card${cardno}/device/ directory.
255 * Inside the device directory a sub-directory 'xgmi_hive_info' is
256 * created which contains the hive ID and the list of nodes.
258 * The hive ID is stored in:
259 * /sys/class/drm/card${cardno}/device/xgmi_hive_info/xgmi_hive_id
261 * The node information is stored in numbered directories:
262 * /sys/class/drm/card${cardno}/device/xgmi_hive_info/node${nodeno}/xgmi_device_id
264 * Each device has their own xgmi_hive_info direction with a mirror
265 * set of node sub-directories.
267 * The XGMI memory space is built by contiguously adding the power of
268 * two padded VRAM space from each node to each other.
272 static struct attribute amdgpu_xgmi_hive_id = {
273 .name = "xgmi_hive_id",
277 static struct attribute *amdgpu_xgmi_hive_attrs[] = {
278 &amdgpu_xgmi_hive_id,
281 ATTRIBUTE_GROUPS(amdgpu_xgmi_hive);
283 static ssize_t amdgpu_xgmi_show_attrs(struct kobject *kobj,
284 struct attribute *attr, char *buf)
286 struct amdgpu_hive_info *hive = container_of(
287 kobj, struct amdgpu_hive_info, kobj);
289 if (attr == &amdgpu_xgmi_hive_id)
290 return snprintf(buf, PAGE_SIZE, "%llu\n", hive->hive_id);
295 static void amdgpu_xgmi_hive_release(struct kobject *kobj)
297 struct amdgpu_hive_info *hive = container_of(
298 kobj, struct amdgpu_hive_info, kobj);
300 amdgpu_reset_put_reset_domain(hive->reset_domain);
301 hive->reset_domain = NULL;
303 mutex_destroy(&hive->hive_lock);
307 static const struct sysfs_ops amdgpu_xgmi_hive_ops = {
308 .show = amdgpu_xgmi_show_attrs,
311 struct kobj_type amdgpu_xgmi_hive_type = {
312 .release = amdgpu_xgmi_hive_release,
313 .sysfs_ops = &amdgpu_xgmi_hive_ops,
314 .default_groups = amdgpu_xgmi_hive_groups,
317 static ssize_t amdgpu_xgmi_show_device_id(struct device *dev,
318 struct device_attribute *attr,
321 struct drm_device *ddev = dev_get_drvdata(dev);
322 struct amdgpu_device *adev = drm_to_adev(ddev);
324 return sysfs_emit(buf, "%llu\n", adev->gmc.xgmi.node_id);
328 #define AMDGPU_XGMI_SET_FICAA(o) ((o) | 0x456801)
329 static ssize_t amdgpu_xgmi_show_error(struct device *dev,
330 struct device_attribute *attr,
333 struct drm_device *ddev = dev_get_drvdata(dev);
334 struct amdgpu_device *adev = drm_to_adev(ddev);
335 uint32_t ficaa_pie_ctl_in, ficaa_pie_status_in;
337 unsigned int error_count = 0;
339 ficaa_pie_ctl_in = AMDGPU_XGMI_SET_FICAA(0x200);
340 ficaa_pie_status_in = AMDGPU_XGMI_SET_FICAA(0x208);
342 if ((!adev->df.funcs) ||
343 (!adev->df.funcs->get_fica) ||
344 (!adev->df.funcs->set_fica))
347 fica_out = adev->df.funcs->get_fica(adev, ficaa_pie_ctl_in);
348 if (fica_out != 0x1f)
349 pr_err("xGMI error counters not enabled!\n");
351 fica_out = adev->df.funcs->get_fica(adev, ficaa_pie_status_in);
353 if ((fica_out & 0xffff) == 2)
354 error_count = ((fica_out >> 62) & 0x1) + (fica_out >> 63);
356 adev->df.funcs->set_fica(adev, ficaa_pie_status_in, 0, 0);
358 return sysfs_emit(buf, "%u\n", error_count);
362 static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL);
363 static DEVICE_ATTR(xgmi_error, S_IRUGO, amdgpu_xgmi_show_error, NULL);
365 static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev,
366 struct amdgpu_hive_info *hive)
369 char node[10] = { 0 };
371 /* Create xgmi device id file */
372 ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id);
374 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n");
378 /* Create xgmi error file */
379 ret = device_create_file(adev->dev, &dev_attr_xgmi_error);
381 pr_err("failed to create xgmi_error\n");
384 /* Create sysfs link to hive info folder on the first device */
385 if (hive->kobj.parent != (&adev->dev->kobj)) {
386 ret = sysfs_create_link(&adev->dev->kobj, &hive->kobj,
389 dev_err(adev->dev, "XGMI: Failed to create link to hive info");
394 sprintf(node, "node%d", atomic_read(&hive->number_devices));
395 /* Create sysfs link form the hive folder to yourself */
396 ret = sysfs_create_link(&hive->kobj, &adev->dev->kobj, node);
398 dev_err(adev->dev, "XGMI: Failed to create link from hive info");
406 sysfs_remove_link(&adev->dev->kobj, adev_to_drm(adev)->unique);
409 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
415 static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev,
416 struct amdgpu_hive_info *hive)
419 memset(node, 0, sizeof(node));
421 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
422 device_remove_file(adev->dev, &dev_attr_xgmi_error);
424 if (hive->kobj.parent != (&adev->dev->kobj))
425 sysfs_remove_link(&adev->dev->kobj,"xgmi_hive_info");
427 sprintf(node, "node%d", atomic_read(&hive->number_devices));
428 sysfs_remove_link(&hive->kobj, node);
434 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev)
436 struct amdgpu_hive_info *hive = NULL;
439 if (!adev->gmc.xgmi.hive_id)
443 kobject_get(&adev->hive->kobj);
447 mutex_lock(&xgmi_mutex);
449 list_for_each_entry(hive, &xgmi_hive_list, node) {
450 if (hive->hive_id == adev->gmc.xgmi.hive_id)
454 hive = kzalloc(sizeof(*hive), GFP_KERNEL);
456 dev_err(adev->dev, "XGMI: allocation failed\n");
461 /* initialize new hive if not exist */
462 ret = kobject_init_and_add(&hive->kobj,
463 &amdgpu_xgmi_hive_type,
465 "%s", "xgmi_hive_info");
467 dev_err(adev->dev, "XGMI: failed initializing kobject for xgmi hive\n");
468 kobject_put(&hive->kobj);
474 * Only init hive->reset_domain for none SRIOV configuration. For SRIOV,
475 * Host driver decide how to reset the GPU either through FLR or chain reset.
476 * Guest side will get individual notifications from the host for the FLR
479 if (!amdgpu_sriov_vf(adev)) {
481 * Avoid recreating reset domain when hive is reconstructed for the case
482 * of reset the devices in the XGMI hive during probe for passthrough GPU
483 * See https://www.spinics.net/lists/amd-gfx/msg58836.html
485 if (adev->reset_domain->type != XGMI_HIVE) {
487 amdgpu_reset_create_reset_domain(XGMI_HIVE, "amdgpu-reset-hive");
488 if (!hive->reset_domain) {
489 dev_err(adev->dev, "XGMI: failed initializing reset domain for xgmi hive\n");
491 kobject_put(&hive->kobj);
496 amdgpu_reset_get_reset_domain(adev->reset_domain);
497 hive->reset_domain = adev->reset_domain;
501 hive->hive_id = adev->gmc.xgmi.hive_id;
502 INIT_LIST_HEAD(&hive->device_list);
503 INIT_LIST_HEAD(&hive->node);
504 mutex_init(&hive->hive_lock);
505 atomic_set(&hive->number_devices, 0);
506 task_barrier_init(&hive->tb);
507 hive->pstate = AMDGPU_XGMI_PSTATE_UNKNOWN;
508 hive->hi_req_gpu = NULL;
511 * hive pstate on boot is high in vega20 so we have to go to low
512 * pstate on after boot.
514 hive->hi_req_count = AMDGPU_MAX_XGMI_DEVICE_PER_HIVE;
515 list_add_tail(&hive->node, &xgmi_hive_list);
519 kobject_get(&hive->kobj);
520 mutex_unlock(&xgmi_mutex);
524 void amdgpu_put_xgmi_hive(struct amdgpu_hive_info *hive)
527 kobject_put(&hive->kobj);
530 int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate)
533 struct amdgpu_hive_info *hive;
534 struct amdgpu_device *request_adev;
535 bool is_hi_req = pstate == AMDGPU_XGMI_PSTATE_MAX_VEGA20;
538 hive = amdgpu_get_xgmi_hive(adev);
542 request_adev = hive->hi_req_gpu ? hive->hi_req_gpu : adev;
543 init_low = hive->pstate == AMDGPU_XGMI_PSTATE_UNKNOWN;
544 amdgpu_put_xgmi_hive(hive);
545 /* fw bug so temporarily disable pstate switching */
548 if (!hive || adev->asic_type != CHIP_VEGA20)
551 mutex_lock(&hive->hive_lock);
554 hive->hi_req_count++;
556 hive->hi_req_count--;
559 * Vega20 only needs single peer to request pstate high for the hive to
560 * go high but all peers must request pstate low for the hive to go low
562 if (hive->pstate == pstate ||
563 (!is_hi_req && hive->hi_req_count && !init_low))
566 dev_dbg(request_adev->dev, "Set xgmi pstate %d.\n", pstate);
568 ret = amdgpu_dpm_set_xgmi_pstate(request_adev, pstate);
570 dev_err(request_adev->dev,
571 "XGMI: Set pstate failure on device %llx, hive %llx, ret %d",
572 request_adev->gmc.xgmi.node_id,
573 request_adev->gmc.xgmi.hive_id, ret);
578 hive->pstate = hive->hi_req_count ?
579 hive->pstate : AMDGPU_XGMI_PSTATE_MIN;
581 hive->pstate = pstate;
582 hive->hi_req_gpu = pstate != AMDGPU_XGMI_PSTATE_MIN ?
586 mutex_unlock(&hive->hive_lock);
590 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
594 if (amdgpu_sriov_vf(adev))
597 /* Each psp need to set the latest topology */
598 ret = psp_xgmi_set_topology_info(&adev->psp,
599 atomic_read(&hive->number_devices),
600 &adev->psp.xgmi_context.top_info);
603 "XGMI: Set topology failure on device %llx, hive %llx, ret %d",
604 adev->gmc.xgmi.node_id,
605 adev->gmc.xgmi.hive_id, ret);
612 * NOTE psp_xgmi_node_info.num_hops layout is as follows:
613 * num_hops[7:6] = link type (0 = xGMI2, 1 = xGMI3, 2/3 = reserved)
614 * num_hops[5:3] = reserved
615 * num_hops[2:0] = number of hops
617 int amdgpu_xgmi_get_hops_count(struct amdgpu_device *adev,
618 struct amdgpu_device *peer_adev)
620 struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
621 uint8_t num_hops_mask = 0x7;
624 for (i = 0 ; i < top->num_nodes; ++i)
625 if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
626 return top->nodes[i].num_hops & num_hops_mask;
630 int amdgpu_xgmi_get_num_links(struct amdgpu_device *adev,
631 struct amdgpu_device *peer_adev)
633 struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
636 for (i = 0 ; i < top->num_nodes; ++i)
637 if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
638 return top->nodes[i].num_links;
643 * Devices that support extended data require the entire hive to initialize with
644 * the shared memory buffer flag set.
646 * Hive locks and conditions apply - see amdgpu_xgmi_add_device
648 static int amdgpu_xgmi_initialize_hive_get_data_partition(struct amdgpu_hive_info *hive,
649 bool set_extended_data)
651 struct amdgpu_device *tmp_adev;
654 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
655 ret = psp_xgmi_initialize(&tmp_adev->psp, set_extended_data, false);
657 dev_err(tmp_adev->dev,
658 "XGMI: Failed to initialize xgmi session for data partition %i\n",
668 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
670 struct psp_xgmi_topology_info *top_info;
671 struct amdgpu_hive_info *hive;
672 struct amdgpu_xgmi *entry;
673 struct amdgpu_device *tmp_adev = NULL;
675 int count = 0, ret = 0;
677 if (!adev->gmc.xgmi.supported)
680 if (!adev->gmc.xgmi.pending_reset &&
681 amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
682 ret = psp_xgmi_initialize(&adev->psp, false, true);
685 "XGMI: Failed to initialize xgmi session\n");
689 ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
692 "XGMI: Failed to get hive id\n");
696 ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
699 "XGMI: Failed to get node id\n");
703 adev->gmc.xgmi.hive_id = 16;
704 adev->gmc.xgmi.node_id = adev->gmc.xgmi.physical_node_id + 16;
707 hive = amdgpu_get_xgmi_hive(adev);
711 "XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n",
712 adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id);
715 mutex_lock(&hive->hive_lock);
717 top_info = &adev->psp.xgmi_context.top_info;
719 list_add_tail(&adev->gmc.xgmi.head, &hive->device_list);
720 list_for_each_entry(entry, &hive->device_list, head)
721 top_info->nodes[count++].node_id = entry->node_id;
722 top_info->num_nodes = count;
723 atomic_set(&hive->number_devices, count);
725 task_barrier_add_task(&hive->tb);
727 if (!adev->gmc.xgmi.pending_reset &&
728 amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
729 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
730 /* update node list for other device in the hive */
731 if (tmp_adev != adev) {
732 top_info = &tmp_adev->psp.xgmi_context.top_info;
733 top_info->nodes[count - 1].node_id =
734 adev->gmc.xgmi.node_id;
735 top_info->num_nodes = count;
737 ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
742 /* get latest topology info for each device from psp */
743 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
744 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count,
745 &tmp_adev->psp.xgmi_context.top_info, false);
747 dev_err(tmp_adev->dev,
748 "XGMI: Get topology failure on device %llx, hive %llx, ret %d",
749 tmp_adev->gmc.xgmi.node_id,
750 tmp_adev->gmc.xgmi.hive_id, ret);
751 /* To do : continue with some node failed or disable the whole hive */
756 /* get topology again for hives that support extended data */
757 if (adev->psp.xgmi_context.supports_extended_data) {
759 /* initialize the hive to get extended data. */
760 ret = amdgpu_xgmi_initialize_hive_get_data_partition(hive, true);
764 /* get the extended data. */
765 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
766 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count,
767 &tmp_adev->psp.xgmi_context.top_info, true);
769 dev_err(tmp_adev->dev,
770 "XGMI: Get topology for extended data failure on device %llx, hive %llx, ret %d",
771 tmp_adev->gmc.xgmi.node_id,
772 tmp_adev->gmc.xgmi.hive_id, ret);
777 /* initialize the hive to get non-extended data for the next round. */
778 ret = amdgpu_xgmi_initialize_hive_get_data_partition(hive, false);
785 if (!ret && !adev->gmc.xgmi.pending_reset)
786 ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive);
789 mutex_unlock(&hive->hive_lock);
793 dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n",
794 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id);
796 amdgpu_put_xgmi_hive(hive);
797 dev_err(adev->dev, "XGMI: Failed to add node %d, hive 0x%llx ret: %d\n",
798 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id,
805 int amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
807 struct amdgpu_hive_info *hive = adev->hive;
809 if (!adev->gmc.xgmi.supported)
815 mutex_lock(&hive->hive_lock);
816 task_barrier_rem_task(&hive->tb);
817 amdgpu_xgmi_sysfs_rem_dev_info(adev, hive);
818 if (hive->hi_req_gpu == adev)
819 hive->hi_req_gpu = NULL;
820 list_del(&adev->gmc.xgmi.head);
821 mutex_unlock(&hive->hive_lock);
823 amdgpu_put_xgmi_hive(hive);
826 if (atomic_dec_return(&hive->number_devices) == 0) {
827 /* Remove the hive from global hive list */
828 mutex_lock(&xgmi_mutex);
829 list_del(&hive->node);
830 mutex_unlock(&xgmi_mutex);
832 amdgpu_put_xgmi_hive(hive);
838 static int amdgpu_xgmi_ras_late_init(struct amdgpu_device *adev, struct ras_common_if *ras_block)
840 if (!adev->gmc.xgmi.supported ||
841 adev->gmc.xgmi.num_physical_nodes == 0)
844 adev->gmc.xgmi.ras->ras_block.hw_ops->reset_ras_error_count(adev);
846 return amdgpu_ras_block_late_init(adev, ras_block);
849 uint64_t amdgpu_xgmi_get_relative_phy_addr(struct amdgpu_device *adev,
852 struct amdgpu_xgmi *xgmi = &adev->gmc.xgmi;
853 return (addr + xgmi->physical_node_id * xgmi->node_segment_size);
856 static void pcs_clear_status(struct amdgpu_device *adev, uint32_t pcs_status_reg)
858 WREG32_PCIE(pcs_status_reg, 0xFFFFFFFF);
859 WREG32_PCIE(pcs_status_reg, 0);
862 static void amdgpu_xgmi_reset_ras_error_count(struct amdgpu_device *adev)
866 switch (adev->asic_type) {
868 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_arct); i++)
869 pcs_clear_status(adev,
870 xgmi_pcs_err_status_reg_arct[i]);
873 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_vg20); i++)
874 pcs_clear_status(adev,
875 xgmi_pcs_err_status_reg_vg20[i]);
878 for (i = 0; i < ARRAY_SIZE(xgmi3x16_pcs_err_status_reg_aldebaran); i++)
879 pcs_clear_status(adev,
880 xgmi3x16_pcs_err_status_reg_aldebaran[i]);
881 for (i = 0; i < ARRAY_SIZE(walf_pcs_err_status_reg_aldebaran); i++)
882 pcs_clear_status(adev,
883 walf_pcs_err_status_reg_aldebaran[i]);
890 static int amdgpu_xgmi_query_pcs_error_status(struct amdgpu_device *adev,
900 const struct amdgpu_pcs_ras_field *pcs_ras_fields = NULL;
901 uint32_t field_array_size = 0;
904 if (adev->ip_versions[XGMI_HWIP][0] == IP_VERSION(6, 1, 0)) {
905 pcs_ras_fields = &xgmi3x16_pcs_ras_fields[0];
906 field_array_size = ARRAY_SIZE(xgmi3x16_pcs_ras_fields);
908 pcs_ras_fields = &xgmi_pcs_ras_fields[0];
909 field_array_size = ARRAY_SIZE(xgmi_pcs_ras_fields);
912 pcs_ras_fields = &wafl_pcs_ras_fields[0];
913 field_array_size = ARRAY_SIZE(wafl_pcs_ras_fields);
917 value = value & ~mask_value;
919 /* query xgmi/walf pcs error status,
920 * only ue is supported */
921 for (i = 0; value && i < field_array_size; i++) {
923 pcs_ras_fields[i].pcs_err_mask) >>
924 pcs_ras_fields[i].pcs_err_shift;
926 dev_info(adev->dev, "%s detected\n",
927 pcs_ras_fields[i].err_name);
931 /* reset bit value if the bit is checked */
932 value &= ~(pcs_ras_fields[i].pcs_err_mask);
938 static void amdgpu_xgmi_query_ras_error_count(struct amdgpu_device *adev,
939 void *ras_error_status)
941 struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
943 uint32_t data, mask_data = 0;
944 uint32_t ue_cnt = 0, ce_cnt = 0;
946 if (!amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__XGMI_WAFL))
949 err_data->ue_count = 0;
950 err_data->ce_count = 0;
952 switch (adev->asic_type) {
954 /* check xgmi pcs error */
955 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_arct); i++) {
956 data = RREG32_PCIE(xgmi_pcs_err_status_reg_arct[i]);
958 amdgpu_xgmi_query_pcs_error_status(adev, data,
959 mask_data, &ue_cnt, &ce_cnt, true, false);
961 /* check wafl pcs error */
962 for (i = 0; i < ARRAY_SIZE(wafl_pcs_err_status_reg_arct); i++) {
963 data = RREG32_PCIE(wafl_pcs_err_status_reg_arct[i]);
965 amdgpu_xgmi_query_pcs_error_status(adev, data,
966 mask_data, &ue_cnt, &ce_cnt, false, false);
970 /* check xgmi pcs error */
971 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_vg20); i++) {
972 data = RREG32_PCIE(xgmi_pcs_err_status_reg_vg20[i]);
974 amdgpu_xgmi_query_pcs_error_status(adev, data,
975 mask_data, &ue_cnt, &ce_cnt, true, false);
977 /* check wafl pcs error */
978 for (i = 0; i < ARRAY_SIZE(wafl_pcs_err_status_reg_vg20); i++) {
979 data = RREG32_PCIE(wafl_pcs_err_status_reg_vg20[i]);
981 amdgpu_xgmi_query_pcs_error_status(adev, data,
982 mask_data, &ue_cnt, &ce_cnt, false, false);
986 /* check xgmi3x16 pcs error */
987 for (i = 0; i < ARRAY_SIZE(xgmi3x16_pcs_err_status_reg_aldebaran); i++) {
988 data = RREG32_PCIE(xgmi3x16_pcs_err_status_reg_aldebaran[i]);
990 RREG32_PCIE(xgmi3x16_pcs_err_noncorrectable_mask_reg_aldebaran[i]);
992 amdgpu_xgmi_query_pcs_error_status(adev, data,
993 mask_data, &ue_cnt, &ce_cnt, true, true);
995 /* check wafl pcs error */
996 for (i = 0; i < ARRAY_SIZE(walf_pcs_err_status_reg_aldebaran); i++) {
997 data = RREG32_PCIE(walf_pcs_err_status_reg_aldebaran[i]);
999 RREG32_PCIE(walf_pcs_err_noncorrectable_mask_reg_aldebaran[i]);
1001 amdgpu_xgmi_query_pcs_error_status(adev, data,
1002 mask_data, &ue_cnt, &ce_cnt, false, true);
1006 dev_warn(adev->dev, "XGMI RAS error query not supported");
1010 adev->gmc.xgmi.ras->ras_block.hw_ops->reset_ras_error_count(adev);
1012 err_data->ue_count += ue_cnt;
1013 err_data->ce_count += ce_cnt;
1016 /* Trigger XGMI/WAFL error */
1017 static int amdgpu_ras_error_inject_xgmi(struct amdgpu_device *adev, void *inject_if)
1020 struct ta_ras_trigger_error_input *block_info =
1021 (struct ta_ras_trigger_error_input *)inject_if;
1023 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_DISALLOW))
1024 dev_warn(adev->dev, "Failed to disallow df cstate");
1026 if (amdgpu_dpm_allow_xgmi_power_down(adev, false))
1027 dev_warn(adev->dev, "Failed to disallow XGMI power down");
1029 ret = psp_ras_trigger_error(&adev->psp, block_info);
1031 if (amdgpu_ras_intr_triggered())
1034 if (amdgpu_dpm_allow_xgmi_power_down(adev, true))
1035 dev_warn(adev->dev, "Failed to allow XGMI power down");
1037 if (amdgpu_dpm_set_df_cstate(adev, DF_CSTATE_ALLOW))
1038 dev_warn(adev->dev, "Failed to allow df cstate");
1043 struct amdgpu_ras_block_hw_ops xgmi_ras_hw_ops = {
1044 .query_ras_error_count = amdgpu_xgmi_query_ras_error_count,
1045 .reset_ras_error_count = amdgpu_xgmi_reset_ras_error_count,
1046 .ras_error_inject = amdgpu_ras_error_inject_xgmi,
1049 struct amdgpu_xgmi_ras xgmi_ras = {
1052 .name = "xgmi_wafl",
1053 .block = AMDGPU_RAS_BLOCK__XGMI_WAFL,
1054 .type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE,
1056 .hw_ops = &xgmi_ras_hw_ops,
1057 .ras_late_init = amdgpu_xgmi_ras_late_init,