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 "wafl/wafl2_4_0_0_smn.h"
33 #include "wafl/wafl2_4_0_0_sh_mask.h"
35 static DEFINE_MUTEX(xgmi_mutex);
37 #define AMDGPU_MAX_XGMI_DEVICE_PER_HIVE 4
39 static LIST_HEAD(xgmi_hive_list);
41 static const int xgmi_pcs_err_status_reg_vg20[] = {
42 smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS,
43 smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x100000,
46 static const int wafl_pcs_err_status_reg_vg20[] = {
47 smnPCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS,
48 smnPCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS + 0x100000,
51 static const int xgmi_pcs_err_status_reg_arct[] = {
52 smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS,
53 smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x100000,
54 smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x500000,
55 smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x600000,
56 smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x700000,
57 smnXGMI0_PCS_GOPX16_PCS_ERROR_STATUS + 0x800000,
61 static const int wafl_pcs_err_status_reg_arct[] = {
62 smnPCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS,
63 smnPCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS + 0x100000,
66 static const struct amdgpu_pcs_ras_field xgmi_pcs_ras_fields[] = {
67 {"XGMI PCS DataLossErr",
68 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataLossErr)},
69 {"XGMI PCS TrainingErr",
70 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, TrainingErr)},
72 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, CRCErr)},
73 {"XGMI PCS BERExceededErr",
74 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, BERExceededErr)},
75 {"XGMI PCS TxMetaDataErr",
76 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, TxMetaDataErr)},
77 {"XGMI PCS ReplayBufParityErr",
78 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayBufParityErr)},
79 {"XGMI PCS DataParityErr",
80 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataParityErr)},
81 {"XGMI PCS ReplayFifoOverflowErr",
82 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayFifoOverflowErr)},
83 {"XGMI PCS ReplayFifoUnderflowErr",
84 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReplayFifoUnderflowErr)},
85 {"XGMI PCS ElasticFifoOverflowErr",
86 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ElasticFifoOverflowErr)},
87 {"XGMI PCS DeskewErr",
88 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DeskewErr)},
89 {"XGMI PCS DataStartupLimitErr",
90 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, DataStartupLimitErr)},
91 {"XGMI PCS FCInitTimeoutErr",
92 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, FCInitTimeoutErr)},
93 {"XGMI PCS RecoveryTimeoutErr",
94 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryTimeoutErr)},
95 {"XGMI PCS ReadySerialTimeoutErr",
96 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReadySerialTimeoutErr)},
97 {"XGMI PCS ReadySerialAttemptErr",
98 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, ReadySerialAttemptErr)},
99 {"XGMI PCS RecoveryAttemptErr",
100 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryAttemptErr)},
101 {"XGMI PCS RecoveryRelockAttemptErr",
102 SOC15_REG_FIELD(XGMI0_PCS_GOPX16_PCS_ERROR_STATUS, RecoveryRelockAttemptErr)},
105 static const struct amdgpu_pcs_ras_field wafl_pcs_ras_fields[] = {
106 {"WAFL PCS DataLossErr",
107 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataLossErr)},
108 {"WAFL PCS TrainingErr",
109 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, TrainingErr)},
111 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, CRCErr)},
112 {"WAFL PCS BERExceededErr",
113 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, BERExceededErr)},
114 {"WAFL PCS TxMetaDataErr",
115 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, TxMetaDataErr)},
116 {"WAFL PCS ReplayBufParityErr",
117 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayBufParityErr)},
118 {"WAFL PCS DataParityErr",
119 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataParityErr)},
120 {"WAFL PCS ReplayFifoOverflowErr",
121 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayFifoOverflowErr)},
122 {"WAFL PCS ReplayFifoUnderflowErr",
123 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReplayFifoUnderflowErr)},
124 {"WAFL PCS ElasticFifoOverflowErr",
125 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ElasticFifoOverflowErr)},
126 {"WAFL PCS DeskewErr",
127 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DeskewErr)},
128 {"WAFL PCS DataStartupLimitErr",
129 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, DataStartupLimitErr)},
130 {"WAFL PCS FCInitTimeoutErr",
131 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, FCInitTimeoutErr)},
132 {"WAFL PCS RecoveryTimeoutErr",
133 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryTimeoutErr)},
134 {"WAFL PCS ReadySerialTimeoutErr",
135 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReadySerialTimeoutErr)},
136 {"WAFL PCS ReadySerialAttemptErr",
137 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, ReadySerialAttemptErr)},
138 {"WAFL PCS RecoveryAttemptErr",
139 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryAttemptErr)},
140 {"WAFL PCS RecoveryRelockAttemptErr",
141 SOC15_REG_FIELD(PCS_GOPX1_0_PCS_GOPX1_PCS_ERROR_STATUS, RecoveryRelockAttemptErr)},
145 * DOC: AMDGPU XGMI Support
147 * XGMI is a high speed interconnect that joins multiple GPU cards
148 * into a homogeneous memory space that is organized by a collective
149 * hive ID and individual node IDs, both of which are 64-bit numbers.
151 * The file xgmi_device_id contains the unique per GPU device ID and
152 * is stored in the /sys/class/drm/card${cardno}/device/ directory.
154 * Inside the device directory a sub-directory 'xgmi_hive_info' is
155 * created which contains the hive ID and the list of nodes.
157 * The hive ID is stored in:
158 * /sys/class/drm/card${cardno}/device/xgmi_hive_info/xgmi_hive_id
160 * The node information is stored in numbered directories:
161 * /sys/class/drm/card${cardno}/device/xgmi_hive_info/node${nodeno}/xgmi_device_id
163 * Each device has their own xgmi_hive_info direction with a mirror
164 * set of node sub-directories.
166 * The XGMI memory space is built by contiguously adding the power of
167 * two padded VRAM space from each node to each other.
171 static struct attribute amdgpu_xgmi_hive_id = {
172 .name = "xgmi_hive_id",
176 static struct attribute *amdgpu_xgmi_hive_attrs[] = {
177 &amdgpu_xgmi_hive_id,
181 static ssize_t amdgpu_xgmi_show_attrs(struct kobject *kobj,
182 struct attribute *attr, char *buf)
184 struct amdgpu_hive_info *hive = container_of(
185 kobj, struct amdgpu_hive_info, kobj);
187 if (attr == &amdgpu_xgmi_hive_id)
188 return snprintf(buf, PAGE_SIZE, "%llu\n", hive->hive_id);
193 static void amdgpu_xgmi_hive_release(struct kobject *kobj)
195 struct amdgpu_hive_info *hive = container_of(
196 kobj, struct amdgpu_hive_info, kobj);
198 mutex_destroy(&hive->hive_lock);
202 static const struct sysfs_ops amdgpu_xgmi_hive_ops = {
203 .show = amdgpu_xgmi_show_attrs,
206 struct kobj_type amdgpu_xgmi_hive_type = {
207 .release = amdgpu_xgmi_hive_release,
208 .sysfs_ops = &amdgpu_xgmi_hive_ops,
209 .default_attrs = amdgpu_xgmi_hive_attrs,
212 static ssize_t amdgpu_xgmi_show_device_id(struct device *dev,
213 struct device_attribute *attr,
216 struct drm_device *ddev = dev_get_drvdata(dev);
217 struct amdgpu_device *adev = drm_to_adev(ddev);
219 return sysfs_emit(buf, "%llu\n", adev->gmc.xgmi.node_id);
223 #define AMDGPU_XGMI_SET_FICAA(o) ((o) | 0x456801)
224 static ssize_t amdgpu_xgmi_show_error(struct device *dev,
225 struct device_attribute *attr,
228 struct drm_device *ddev = dev_get_drvdata(dev);
229 struct amdgpu_device *adev = drm_to_adev(ddev);
230 uint32_t ficaa_pie_ctl_in, ficaa_pie_status_in;
232 unsigned int error_count = 0;
234 ficaa_pie_ctl_in = AMDGPU_XGMI_SET_FICAA(0x200);
235 ficaa_pie_status_in = AMDGPU_XGMI_SET_FICAA(0x208);
237 fica_out = adev->df.funcs->get_fica(adev, ficaa_pie_ctl_in);
238 if (fica_out != 0x1f)
239 pr_err("xGMI error counters not enabled!\n");
241 fica_out = adev->df.funcs->get_fica(adev, ficaa_pie_status_in);
243 if ((fica_out & 0xffff) == 2)
244 error_count = ((fica_out >> 62) & 0x1) + (fica_out >> 63);
246 adev->df.funcs->set_fica(adev, ficaa_pie_status_in, 0, 0);
248 return sysfs_emit(buf, "%u\n", error_count);
252 static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL);
253 static DEVICE_ATTR(xgmi_error, S_IRUGO, amdgpu_xgmi_show_error, NULL);
255 static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev,
256 struct amdgpu_hive_info *hive)
259 char node[10] = { 0 };
261 /* Create xgmi device id file */
262 ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id);
264 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n");
268 /* Create xgmi error file */
269 ret = device_create_file(adev->dev, &dev_attr_xgmi_error);
271 pr_err("failed to create xgmi_error\n");
274 /* Create sysfs link to hive info folder on the first device */
275 if (hive->kobj.parent != (&adev->dev->kobj)) {
276 ret = sysfs_create_link(&adev->dev->kobj, &hive->kobj,
279 dev_err(adev->dev, "XGMI: Failed to create link to hive info");
284 sprintf(node, "node%d", atomic_read(&hive->number_devices));
285 /* Create sysfs link form the hive folder to yourself */
286 ret = sysfs_create_link(&hive->kobj, &adev->dev->kobj, node);
288 dev_err(adev->dev, "XGMI: Failed to create link from hive info");
296 sysfs_remove_link(&adev->dev->kobj, adev_to_drm(adev)->unique);
299 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
305 static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev,
306 struct amdgpu_hive_info *hive)
309 memset(node, 0, sizeof(node));
311 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
312 device_remove_file(adev->dev, &dev_attr_xgmi_error);
314 if (hive->kobj.parent != (&adev->dev->kobj))
315 sysfs_remove_link(&adev->dev->kobj,"xgmi_hive_info");
317 sprintf(node, "node%d", atomic_read(&hive->number_devices));
318 sysfs_remove_link(&hive->kobj, node);
324 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev)
326 struct amdgpu_hive_info *hive = NULL;
329 if (!adev->gmc.xgmi.hive_id)
333 kobject_get(&adev->hive->kobj);
337 mutex_lock(&xgmi_mutex);
339 list_for_each_entry(hive, &xgmi_hive_list, node) {
340 if (hive->hive_id == adev->gmc.xgmi.hive_id)
344 hive = kzalloc(sizeof(*hive), GFP_KERNEL);
346 dev_err(adev->dev, "XGMI: allocation failed\n");
351 /* initialize new hive if not exist */
352 ret = kobject_init_and_add(&hive->kobj,
353 &amdgpu_xgmi_hive_type,
355 "%s", "xgmi_hive_info");
357 dev_err(adev->dev, "XGMI: failed initializing kobject for xgmi hive\n");
363 hive->hive_id = adev->gmc.xgmi.hive_id;
364 INIT_LIST_HEAD(&hive->device_list);
365 INIT_LIST_HEAD(&hive->node);
366 mutex_init(&hive->hive_lock);
367 atomic_set(&hive->in_reset, 0);
368 atomic_set(&hive->number_devices, 0);
369 task_barrier_init(&hive->tb);
370 hive->pstate = AMDGPU_XGMI_PSTATE_UNKNOWN;
371 hive->hi_req_gpu = NULL;
373 * hive pstate on boot is high in vega20 so we have to go to low
374 * pstate on after boot.
376 hive->hi_req_count = AMDGPU_MAX_XGMI_DEVICE_PER_HIVE;
377 list_add_tail(&hive->node, &xgmi_hive_list);
381 kobject_get(&hive->kobj);
382 mutex_unlock(&xgmi_mutex);
386 void amdgpu_put_xgmi_hive(struct amdgpu_hive_info *hive)
389 kobject_put(&hive->kobj);
392 int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate)
395 struct amdgpu_hive_info *hive;
396 struct amdgpu_device *request_adev;
397 bool is_hi_req = pstate == AMDGPU_XGMI_PSTATE_MAX_VEGA20;
400 hive = amdgpu_get_xgmi_hive(adev);
404 request_adev = hive->hi_req_gpu ? hive->hi_req_gpu : adev;
405 init_low = hive->pstate == AMDGPU_XGMI_PSTATE_UNKNOWN;
406 amdgpu_put_xgmi_hive(hive);
407 /* fw bug so temporarily disable pstate switching */
410 if (!hive || adev->asic_type != CHIP_VEGA20)
413 mutex_lock(&hive->hive_lock);
416 hive->hi_req_count++;
418 hive->hi_req_count--;
421 * Vega20 only needs single peer to request pstate high for the hive to
422 * go high but all peers must request pstate low for the hive to go low
424 if (hive->pstate == pstate ||
425 (!is_hi_req && hive->hi_req_count && !init_low))
428 dev_dbg(request_adev->dev, "Set xgmi pstate %d.\n", pstate);
430 ret = amdgpu_dpm_set_xgmi_pstate(request_adev, pstate);
432 dev_err(request_adev->dev,
433 "XGMI: Set pstate failure on device %llx, hive %llx, ret %d",
434 request_adev->gmc.xgmi.node_id,
435 request_adev->gmc.xgmi.hive_id, ret);
440 hive->pstate = hive->hi_req_count ?
441 hive->pstate : AMDGPU_XGMI_PSTATE_MIN;
443 hive->pstate = pstate;
444 hive->hi_req_gpu = pstate != AMDGPU_XGMI_PSTATE_MIN ?
448 mutex_unlock(&hive->hive_lock);
452 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
456 /* Each psp need to set the latest topology */
457 ret = psp_xgmi_set_topology_info(&adev->psp,
458 atomic_read(&hive->number_devices),
459 &adev->psp.xgmi_context.top_info);
462 "XGMI: Set topology failure on device %llx, hive %llx, ret %d",
463 adev->gmc.xgmi.node_id,
464 adev->gmc.xgmi.hive_id, ret);
471 * NOTE psp_xgmi_node_info.num_hops layout is as follows:
472 * num_hops[7:6] = link type (0 = xGMI2, 1 = xGMI3, 2/3 = reserved)
473 * num_hops[5:3] = reserved
474 * num_hops[2:0] = number of hops
476 int amdgpu_xgmi_get_hops_count(struct amdgpu_device *adev,
477 struct amdgpu_device *peer_adev)
479 struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
480 uint8_t num_hops_mask = 0x7;
483 for (i = 0 ; i < top->num_nodes; ++i)
484 if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
485 return top->nodes[i].num_hops & num_hops_mask;
489 int amdgpu_xgmi_get_num_links(struct amdgpu_device *adev,
490 struct amdgpu_device *peer_adev)
492 struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
495 for (i = 0 ; i < top->num_nodes; ++i)
496 if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
497 return top->nodes[i].num_links;
502 * Devices that support extended data require the entire hive to initialize with
503 * the shared memory buffer flag set.
505 * Hive locks and conditions apply - see amdgpu_xgmi_add_device
507 static int amdgpu_xgmi_initialize_hive_get_data_partition(struct amdgpu_hive_info *hive,
508 bool set_extended_data)
510 struct amdgpu_device *tmp_adev;
513 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
514 ret = psp_xgmi_initialize(&tmp_adev->psp, set_extended_data, false);
516 dev_err(tmp_adev->dev,
517 "XGMI: Failed to initialize xgmi session for data partition %i\n",
527 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
529 struct psp_xgmi_topology_info *top_info;
530 struct amdgpu_hive_info *hive;
531 struct amdgpu_xgmi *entry;
532 struct amdgpu_device *tmp_adev = NULL;
534 int count = 0, ret = 0;
536 if (!adev->gmc.xgmi.supported)
539 if (!adev->gmc.xgmi.pending_reset &&
540 amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
541 ret = psp_xgmi_initialize(&adev->psp, false, true);
544 "XGMI: Failed to initialize xgmi session\n");
548 ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
551 "XGMI: Failed to get hive id\n");
555 ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
558 "XGMI: Failed to get node id\n");
562 adev->gmc.xgmi.hive_id = 16;
563 adev->gmc.xgmi.node_id = adev->gmc.xgmi.physical_node_id + 16;
566 hive = amdgpu_get_xgmi_hive(adev);
570 "XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n",
571 adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id);
574 mutex_lock(&hive->hive_lock);
576 top_info = &adev->psp.xgmi_context.top_info;
578 list_add_tail(&adev->gmc.xgmi.head, &hive->device_list);
579 list_for_each_entry(entry, &hive->device_list, head)
580 top_info->nodes[count++].node_id = entry->node_id;
581 top_info->num_nodes = count;
582 atomic_set(&hive->number_devices, count);
584 task_barrier_add_task(&hive->tb);
586 if (!adev->gmc.xgmi.pending_reset &&
587 amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
588 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
589 /* update node list for other device in the hive */
590 if (tmp_adev != adev) {
591 top_info = &tmp_adev->psp.xgmi_context.top_info;
592 top_info->nodes[count - 1].node_id =
593 adev->gmc.xgmi.node_id;
594 top_info->num_nodes = count;
596 ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
601 /* get latest topology info for each device from psp */
602 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
603 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count,
604 &tmp_adev->psp.xgmi_context.top_info, false);
606 dev_err(tmp_adev->dev,
607 "XGMI: Get topology failure on device %llx, hive %llx, ret %d",
608 tmp_adev->gmc.xgmi.node_id,
609 tmp_adev->gmc.xgmi.hive_id, ret);
610 /* To do : continue with some node failed or disable the whole hive */
615 /* get topology again for hives that support extended data */
616 if (adev->psp.xgmi_context.supports_extended_data) {
618 /* initialize the hive to get extended data. */
619 ret = amdgpu_xgmi_initialize_hive_get_data_partition(hive, true);
623 /* get the extended data. */
624 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
625 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count,
626 &tmp_adev->psp.xgmi_context.top_info, true);
628 dev_err(tmp_adev->dev,
629 "XGMI: Get topology for extended data failure on device %llx, hive %llx, ret %d",
630 tmp_adev->gmc.xgmi.node_id,
631 tmp_adev->gmc.xgmi.hive_id, ret);
636 /* initialize the hive to get non-extended data for the next round. */
637 ret = amdgpu_xgmi_initialize_hive_get_data_partition(hive, false);
644 if (!ret && !adev->gmc.xgmi.pending_reset)
645 ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive);
648 mutex_unlock(&hive->hive_lock);
652 dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n",
653 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id);
655 amdgpu_put_xgmi_hive(hive);
656 dev_err(adev->dev, "XGMI: Failed to add node %d, hive 0x%llx ret: %d\n",
657 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id,
664 int amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
666 struct amdgpu_hive_info *hive = adev->hive;
668 if (!adev->gmc.xgmi.supported)
674 mutex_lock(&hive->hive_lock);
675 task_barrier_rem_task(&hive->tb);
676 amdgpu_xgmi_sysfs_rem_dev_info(adev, hive);
677 if (hive->hi_req_gpu == adev)
678 hive->hi_req_gpu = NULL;
679 list_del(&adev->gmc.xgmi.head);
680 mutex_unlock(&hive->hive_lock);
682 amdgpu_put_xgmi_hive(hive);
685 if (atomic_dec_return(&hive->number_devices) == 0) {
686 /* Remove the hive from global hive list */
687 mutex_lock(&xgmi_mutex);
688 list_del(&hive->node);
689 mutex_unlock(&xgmi_mutex);
691 amdgpu_put_xgmi_hive(hive);
694 return psp_xgmi_terminate(&adev->psp);
697 static int amdgpu_xgmi_ras_late_init(struct amdgpu_device *adev)
700 struct ras_ih_if ih_info = {
703 struct ras_fs_if fs_info = {
704 .sysfs_name = "xgmi_wafl_err_count",
707 if (!adev->gmc.xgmi.supported ||
708 adev->gmc.xgmi.num_physical_nodes == 0)
711 adev->gmc.xgmi.ras_funcs->reset_ras_error_count(adev);
713 if (!adev->gmc.xgmi.ras_if) {
714 adev->gmc.xgmi.ras_if = kmalloc(sizeof(struct ras_common_if), GFP_KERNEL);
715 if (!adev->gmc.xgmi.ras_if)
717 adev->gmc.xgmi.ras_if->block = AMDGPU_RAS_BLOCK__XGMI_WAFL;
718 adev->gmc.xgmi.ras_if->type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
719 adev->gmc.xgmi.ras_if->sub_block_index = 0;
721 ih_info.head = fs_info.head = *adev->gmc.xgmi.ras_if;
722 r = amdgpu_ras_late_init(adev, adev->gmc.xgmi.ras_if,
724 if (r || !amdgpu_ras_is_supported(adev, adev->gmc.xgmi.ras_if->block)) {
725 kfree(adev->gmc.xgmi.ras_if);
726 adev->gmc.xgmi.ras_if = NULL;
732 static void amdgpu_xgmi_ras_fini(struct amdgpu_device *adev)
734 if (amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__XGMI_WAFL) &&
735 adev->gmc.xgmi.ras_if) {
736 struct ras_common_if *ras_if = adev->gmc.xgmi.ras_if;
737 struct ras_ih_if ih_info = {
741 amdgpu_ras_late_fini(adev, ras_if, &ih_info);
746 uint64_t amdgpu_xgmi_get_relative_phy_addr(struct amdgpu_device *adev,
749 struct amdgpu_xgmi *xgmi = &adev->gmc.xgmi;
750 return (addr + xgmi->physical_node_id * xgmi->node_segment_size);
753 static void pcs_clear_status(struct amdgpu_device *adev, uint32_t pcs_status_reg)
755 WREG32_PCIE(pcs_status_reg, 0xFFFFFFFF);
756 WREG32_PCIE(pcs_status_reg, 0);
759 static void amdgpu_xgmi_reset_ras_error_count(struct amdgpu_device *adev)
763 switch (adev->asic_type) {
765 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_arct); i++)
766 pcs_clear_status(adev,
767 xgmi_pcs_err_status_reg_arct[i]);
770 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_vg20); i++)
771 pcs_clear_status(adev,
772 xgmi_pcs_err_status_reg_vg20[i]);
779 static int amdgpu_xgmi_query_pcs_error_status(struct amdgpu_device *adev,
789 /* query xgmi pcs error status,
790 * only ue is supported */
791 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_ras_fields); i ++) {
793 xgmi_pcs_ras_fields[i].pcs_err_mask) >>
794 xgmi_pcs_ras_fields[i].pcs_err_shift;
796 dev_info(adev->dev, "%s detected\n",
797 xgmi_pcs_ras_fields[i].err_name);
802 /* query wafl pcs error status,
803 * only ue is supported */
804 for (i = 0; i < ARRAY_SIZE(wafl_pcs_ras_fields); i++) {
806 wafl_pcs_ras_fields[i].pcs_err_mask) >>
807 wafl_pcs_ras_fields[i].pcs_err_shift;
809 dev_info(adev->dev, "%s detected\n",
810 wafl_pcs_ras_fields[i].err_name);
819 static int amdgpu_xgmi_query_ras_error_count(struct amdgpu_device *adev,
820 void *ras_error_status)
822 struct ras_err_data *err_data = (struct ras_err_data *)ras_error_status;
825 uint32_t ue_cnt = 0, ce_cnt = 0;
827 if (!amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__XGMI_WAFL))
830 err_data->ue_count = 0;
831 err_data->ce_count = 0;
833 switch (adev->asic_type) {
835 /* check xgmi pcs error */
836 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_arct); i++) {
837 data = RREG32_PCIE(xgmi_pcs_err_status_reg_arct[i]);
839 amdgpu_xgmi_query_pcs_error_status(adev,
840 data, &ue_cnt, &ce_cnt, true);
842 /* check wafl pcs error */
843 for (i = 0; i < ARRAY_SIZE(wafl_pcs_err_status_reg_arct); i++) {
844 data = RREG32_PCIE(wafl_pcs_err_status_reg_arct[i]);
846 amdgpu_xgmi_query_pcs_error_status(adev,
847 data, &ue_cnt, &ce_cnt, false);
851 /* check xgmi pcs error */
852 for (i = 0; i < ARRAY_SIZE(xgmi_pcs_err_status_reg_vg20); i++) {
853 data = RREG32_PCIE(xgmi_pcs_err_status_reg_vg20[i]);
855 amdgpu_xgmi_query_pcs_error_status(adev,
856 data, &ue_cnt, &ce_cnt, true);
858 /* check wafl pcs error */
859 for (i = 0; i < ARRAY_SIZE(wafl_pcs_err_status_reg_vg20); i++) {
860 data = RREG32_PCIE(wafl_pcs_err_status_reg_vg20[i]);
862 amdgpu_xgmi_query_pcs_error_status(adev,
863 data, &ue_cnt, &ce_cnt, false);
867 dev_warn(adev->dev, "XGMI RAS error query not supported");
871 adev->gmc.xgmi.ras_funcs->reset_ras_error_count(adev);
873 err_data->ue_count += ue_cnt;
874 err_data->ce_count += ce_cnt;
879 const struct amdgpu_xgmi_ras_funcs xgmi_ras_funcs = {
880 .ras_late_init = amdgpu_xgmi_ras_late_init,
881 .ras_fini = amdgpu_xgmi_ras_fini,
882 .query_ras_error_count = amdgpu_xgmi_query_ras_error_count,
883 .reset_ras_error_count = amdgpu_xgmi_reset_ras_error_count,