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_smu.h"
28 #include "df/df_3_6_offset.h"
30 static DEFINE_MUTEX(xgmi_mutex);
32 #define AMDGPU_MAX_XGMI_HIVE 8
33 #define AMDGPU_MAX_XGMI_DEVICE_PER_HIVE 4
35 static struct amdgpu_hive_info xgmi_hives[AMDGPU_MAX_XGMI_HIVE];
36 static unsigned hive_count = 0;
38 void *amdgpu_xgmi_hive_try_lock(struct amdgpu_hive_info *hive)
40 return &hive->device_list;
44 * DOC: AMDGPU XGMI Support
46 * XGMI is a high speed interconnect that joins multiple GPU cards
47 * into a homogeneous memory space that is organized by a collective
48 * hive ID and individual node IDs, both of which are 64-bit numbers.
50 * The file xgmi_device_id contains the unique per GPU device ID and
51 * is stored in the /sys/class/drm/card${cardno}/device/ directory.
53 * Inside the device directory a sub-directory 'xgmi_hive_info' is
54 * created which contains the hive ID and the list of nodes.
56 * The hive ID is stored in:
57 * /sys/class/drm/card${cardno}/device/xgmi_hive_info/xgmi_hive_id
59 * The node information is stored in numbered directories:
60 * /sys/class/drm/card${cardno}/device/xgmi_hive_info/node${nodeno}/xgmi_device_id
62 * Each device has their own xgmi_hive_info direction with a mirror
63 * set of node sub-directories.
65 * The XGMI memory space is built by contiguously adding the power of
66 * two padded VRAM space from each node to each other.
71 static ssize_t amdgpu_xgmi_show_hive_id(struct device *dev,
72 struct device_attribute *attr, char *buf)
74 struct amdgpu_hive_info *hive =
75 container_of(attr, struct amdgpu_hive_info, dev_attr);
77 return snprintf(buf, PAGE_SIZE, "%llu\n", hive->hive_id);
80 static int amdgpu_xgmi_sysfs_create(struct amdgpu_device *adev,
81 struct amdgpu_hive_info *hive)
85 if (WARN_ON(hive->kobj))
88 hive->kobj = kobject_create_and_add("xgmi_hive_info", &adev->dev->kobj);
90 dev_err(adev->dev, "XGMI: Failed to allocate sysfs entry!\n");
94 hive->dev_attr = (struct device_attribute) {
96 .name = "xgmi_hive_id",
100 .show = amdgpu_xgmi_show_hive_id,
103 ret = sysfs_create_file(hive->kobj, &hive->dev_attr.attr);
105 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_hive_id\n");
106 kobject_del(hive->kobj);
107 kobject_put(hive->kobj);
114 static void amdgpu_xgmi_sysfs_destroy(struct amdgpu_device *adev,
115 struct amdgpu_hive_info *hive)
117 sysfs_remove_file(hive->kobj, &hive->dev_attr.attr);
118 kobject_del(hive->kobj);
119 kobject_put(hive->kobj);
123 static ssize_t amdgpu_xgmi_show_device_id(struct device *dev,
124 struct device_attribute *attr,
127 struct drm_device *ddev = dev_get_drvdata(dev);
128 struct amdgpu_device *adev = ddev->dev_private;
130 return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.xgmi.node_id);
134 #define AMDGPU_XGMI_SET_FICAA(o) ((o) | 0x456801)
135 static ssize_t amdgpu_xgmi_show_error(struct device *dev,
136 struct device_attribute *attr,
139 struct drm_device *ddev = dev_get_drvdata(dev);
140 struct amdgpu_device *adev = ddev->dev_private;
141 uint32_t ficaa_pie_ctl_in, ficaa_pie_status_in;
143 unsigned int error_count = 0;
145 ficaa_pie_ctl_in = AMDGPU_XGMI_SET_FICAA(0x200);
146 ficaa_pie_status_in = AMDGPU_XGMI_SET_FICAA(0x208);
148 fica_out = adev->df_funcs->get_fica(adev, ficaa_pie_ctl_in);
149 if (fica_out != 0x1f)
150 pr_err("xGMI error counters not enabled!\n");
152 fica_out = adev->df_funcs->get_fica(adev, ficaa_pie_status_in);
154 if ((fica_out & 0xffff) == 2)
155 error_count = ((fica_out >> 62) & 0x1) + (fica_out >> 63);
157 adev->df_funcs->set_fica(adev, ficaa_pie_status_in, 0, 0);
159 return snprintf(buf, PAGE_SIZE, "%d\n", error_count);
163 static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL);
164 static DEVICE_ATTR(xgmi_error, S_IRUGO, amdgpu_xgmi_show_error, NULL);
166 static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev,
167 struct amdgpu_hive_info *hive)
170 char node[10] = { 0 };
172 /* Create xgmi device id file */
173 ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id);
175 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n");
179 /* Create xgmi error file */
180 ret = device_create_file(adev->dev, &dev_attr_xgmi_error);
182 pr_err("failed to create xgmi_error\n");
185 /* Create sysfs link to hive info folder on the first device */
186 if (adev != hive->adev) {
187 ret = sysfs_create_link(&adev->dev->kobj, hive->kobj,
190 dev_err(adev->dev, "XGMI: Failed to create link to hive info");
195 sprintf(node, "node%d", hive->number_devices);
196 /* Create sysfs link form the hive folder to yourself */
197 ret = sysfs_create_link(hive->kobj, &adev->dev->kobj, node);
199 dev_err(adev->dev, "XGMI: Failed to create link from hive info");
207 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
210 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
216 static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev,
217 struct amdgpu_hive_info *hive)
219 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
220 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
221 sysfs_remove_link(hive->kobj, adev->ddev->unique);
226 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock)
229 struct amdgpu_hive_info *tmp;
231 if (!adev->gmc.xgmi.hive_id)
234 mutex_lock(&xgmi_mutex);
236 for (i = 0 ; i < hive_count; ++i) {
237 tmp = &xgmi_hives[i];
238 if (tmp->hive_id == adev->gmc.xgmi.hive_id) {
240 mutex_lock(&tmp->hive_lock);
241 mutex_unlock(&xgmi_mutex);
245 if (i >= AMDGPU_MAX_XGMI_HIVE) {
246 mutex_unlock(&xgmi_mutex);
250 /* initialize new hive if not exist */
251 tmp = &xgmi_hives[hive_count++];
253 if (amdgpu_xgmi_sysfs_create(adev, tmp)) {
254 mutex_unlock(&xgmi_mutex);
259 tmp->hive_id = adev->gmc.xgmi.hive_id;
260 INIT_LIST_HEAD(&tmp->device_list);
261 mutex_init(&tmp->hive_lock);
262 mutex_init(&tmp->reset_lock);
265 mutex_lock(&tmp->hive_lock);
267 mutex_unlock(&xgmi_mutex);
272 int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate)
275 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, 0);
280 if (hive->pstate == pstate)
283 dev_dbg(adev->dev, "Set xgmi pstate %d.\n", pstate);
285 if (is_support_sw_smu_xgmi(adev))
286 ret = smu_set_xgmi_pstate(&adev->smu, pstate);
289 "XGMI: Set pstate failure on device %llx, hive %llx, ret %d",
290 adev->gmc.xgmi.node_id,
291 adev->gmc.xgmi.hive_id, ret);
296 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
300 /* Each psp need to set the latest topology */
301 ret = psp_xgmi_set_topology_info(&adev->psp,
302 hive->number_devices,
303 &adev->psp.xgmi_context.top_info);
306 "XGMI: Set topology failure on device %llx, hive %llx, ret %d",
307 adev->gmc.xgmi.node_id,
308 adev->gmc.xgmi.hive_id, ret);
314 int amdgpu_xgmi_get_hops_count(struct amdgpu_device *adev,
315 struct amdgpu_device *peer_adev)
317 struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
320 for (i = 0 ; i < top->num_nodes; ++i)
321 if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
322 return top->nodes[i].num_hops;
326 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
328 struct psp_xgmi_topology_info *top_info;
329 struct amdgpu_hive_info *hive;
330 struct amdgpu_xgmi *entry;
331 struct amdgpu_device *tmp_adev = NULL;
333 int count = 0, ret = 0;
335 if (!adev->gmc.xgmi.supported)
338 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
339 ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
342 "XGMI: Failed to get hive id\n");
346 ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
349 "XGMI: Failed to get node id\n");
353 adev->gmc.xgmi.hive_id = 16;
354 adev->gmc.xgmi.node_id = adev->gmc.xgmi.physical_node_id + 16;
357 hive = amdgpu_get_xgmi_hive(adev, 1);
361 "XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n",
362 adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id);
366 top_info = &adev->psp.xgmi_context.top_info;
368 list_add_tail(&adev->gmc.xgmi.head, &hive->device_list);
369 list_for_each_entry(entry, &hive->device_list, head)
370 top_info->nodes[count++].node_id = entry->node_id;
371 top_info->num_nodes = count;
372 hive->number_devices = count;
374 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
375 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
376 /* update node list for other device in the hive */
377 if (tmp_adev != adev) {
378 top_info = &tmp_adev->psp.xgmi_context.top_info;
379 top_info->nodes[count - 1].node_id =
380 adev->gmc.xgmi.node_id;
381 top_info->num_nodes = count;
383 ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
388 /* get latest topology info for each device from psp */
389 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
390 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count,
391 &tmp_adev->psp.xgmi_context.top_info);
393 dev_err(tmp_adev->dev,
394 "XGMI: Get topology failure on device %llx, hive %llx, ret %d",
395 tmp_adev->gmc.xgmi.node_id,
396 tmp_adev->gmc.xgmi.hive_id, ret);
397 /* To do : continue with some node failed or disable the whole hive */
404 ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive);
407 mutex_unlock(&hive->hive_lock);
410 dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n",
411 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id);
413 dev_err(adev->dev, "XGMI: Failed to add node %d, hive 0x%llx ret: %d\n",
414 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id,
420 void amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
422 struct amdgpu_hive_info *hive;
424 if (!adev->gmc.xgmi.supported)
427 hive = amdgpu_get_xgmi_hive(adev, 1);
431 if (!(hive->number_devices--)) {
432 amdgpu_xgmi_sysfs_destroy(adev, hive);
433 mutex_destroy(&hive->hive_lock);
434 mutex_destroy(&hive->reset_lock);
436 amdgpu_xgmi_sysfs_rem_dev_info(adev, hive);
437 mutex_unlock(&hive->hive_lock);