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"
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);
135 static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL);
138 static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev,
139 struct amdgpu_hive_info *hive)
142 char node[10] = { 0 };
144 /* Create xgmi device id file */
145 ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id);
147 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n");
151 /* Create sysfs link to hive info folder on the first device */
152 if (adev != hive->adev) {
153 ret = sysfs_create_link(&adev->dev->kobj, hive->kobj,
156 dev_err(adev->dev, "XGMI: Failed to create link to hive info");
161 sprintf(node, "node%d", hive->number_devices);
162 /* Create sysfs link form the hive folder to yourself */
163 ret = sysfs_create_link(hive->kobj, &adev->dev->kobj, node);
165 dev_err(adev->dev, "XGMI: Failed to create link from hive info");
173 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
176 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
182 static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev,
183 struct amdgpu_hive_info *hive)
185 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
186 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
187 sysfs_remove_link(hive->kobj, adev->ddev->unique);
192 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock)
195 struct amdgpu_hive_info *tmp;
197 if (!adev->gmc.xgmi.hive_id)
200 mutex_lock(&xgmi_mutex);
202 for (i = 0 ; i < hive_count; ++i) {
203 tmp = &xgmi_hives[i];
204 if (tmp->hive_id == adev->gmc.xgmi.hive_id) {
206 mutex_lock(&tmp->hive_lock);
207 mutex_unlock(&xgmi_mutex);
211 if (i >= AMDGPU_MAX_XGMI_HIVE) {
212 mutex_unlock(&xgmi_mutex);
216 /* initialize new hive if not exist */
217 tmp = &xgmi_hives[hive_count++];
219 if (amdgpu_xgmi_sysfs_create(adev, tmp)) {
220 mutex_unlock(&xgmi_mutex);
225 tmp->hive_id = adev->gmc.xgmi.hive_id;
226 INIT_LIST_HEAD(&tmp->device_list);
227 mutex_init(&tmp->hive_lock);
228 mutex_init(&tmp->reset_lock);
231 mutex_lock(&tmp->hive_lock);
233 mutex_unlock(&xgmi_mutex);
238 int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate)
241 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, 0);
246 if (hive->pstate == pstate)
249 dev_dbg(adev->dev, "Set xgmi pstate %d.\n", pstate);
251 if (is_support_sw_smu(adev))
252 ret = smu_set_xgmi_pstate(&adev->smu, pstate);
255 "XGMI: Set pstate failure on device %llx, hive %llx, ret %d",
256 adev->gmc.xgmi.node_id,
257 adev->gmc.xgmi.hive_id, ret);
262 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
266 /* Each psp need to set the latest topology */
267 ret = psp_xgmi_set_topology_info(&adev->psp,
268 hive->number_devices,
269 &adev->psp.xgmi_context.top_info);
272 "XGMI: Set topology failure on device %llx, hive %llx, ret %d",
273 adev->gmc.xgmi.node_id,
274 adev->gmc.xgmi.hive_id, ret);
280 int amdgpu_xgmi_get_hops_count(struct amdgpu_device *adev,
281 struct amdgpu_device *peer_adev)
283 struct psp_xgmi_topology_info *top = &adev->psp.xgmi_context.top_info;
286 for (i = 0 ; i < top->num_nodes; ++i)
287 if (top->nodes[i].node_id == peer_adev->gmc.xgmi.node_id)
288 return top->nodes[i].num_hops;
292 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
294 struct psp_xgmi_topology_info *top_info;
295 struct amdgpu_hive_info *hive;
296 struct amdgpu_xgmi *entry;
297 struct amdgpu_device *tmp_adev = NULL;
299 int count = 0, ret = 0;
301 if (!adev->gmc.xgmi.supported)
304 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
305 ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
308 "XGMI: Failed to get hive id\n");
312 ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
315 "XGMI: Failed to get node id\n");
319 adev->gmc.xgmi.hive_id = 16;
320 adev->gmc.xgmi.node_id = adev->gmc.xgmi.physical_node_id + 16;
323 hive = amdgpu_get_xgmi_hive(adev, 1);
327 "XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n",
328 adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id);
332 top_info = &adev->psp.xgmi_context.top_info;
334 list_add_tail(&adev->gmc.xgmi.head, &hive->device_list);
335 list_for_each_entry(entry, &hive->device_list, head)
336 top_info->nodes[count++].node_id = entry->node_id;
337 top_info->num_nodes = count;
338 hive->number_devices = count;
340 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP)) {
341 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
342 /* update node list for other device in the hive */
343 if (tmp_adev != adev) {
344 top_info = &tmp_adev->psp.xgmi_context.top_info;
345 top_info->nodes[count - 1].node_id =
346 adev->gmc.xgmi.node_id;
347 top_info->num_nodes = count;
349 ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
354 /* get latest topology info for each device from psp */
355 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
356 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count,
357 &tmp_adev->psp.xgmi_context.top_info);
359 dev_err(tmp_adev->dev,
360 "XGMI: Get topology failure on device %llx, hive %llx, ret %d",
361 tmp_adev->gmc.xgmi.node_id,
362 tmp_adev->gmc.xgmi.hive_id, ret);
363 /* To do : continue with some node failed or disable the whole hive */
370 ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive);
373 mutex_unlock(&hive->hive_lock);
376 dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n",
377 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id);
379 dev_err(adev->dev, "XGMI: Failed to add node %d, hive 0x%llx ret: %d\n",
380 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id,
386 void amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
388 struct amdgpu_hive_info *hive;
390 if (!adev->gmc.xgmi.supported)
393 hive = amdgpu_get_xgmi_hive(adev, 1);
397 if (!(hive->number_devices--)) {
398 amdgpu_xgmi_sysfs_destroy(adev, hive);
399 mutex_destroy(&hive->hive_lock);
400 mutex_destroy(&hive->reset_lock);
402 amdgpu_xgmi_sysfs_rem_dev_info(adev, hive);
403 mutex_unlock(&hive->hive_lock);