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"
29 static DEFINE_MUTEX(xgmi_mutex);
31 #define AMDGPU_MAX_XGMI_HIVE 8
32 #define AMDGPU_MAX_XGMI_DEVICE_PER_HIVE 4
34 static struct amdgpu_hive_info xgmi_hives[AMDGPU_MAX_XGMI_HIVE];
35 static unsigned hive_count = 0;
37 void *amdgpu_xgmi_hive_try_lock(struct amdgpu_hive_info *hive)
39 return &hive->device_list;
42 static ssize_t amdgpu_xgmi_show_hive_id(struct device *dev,
43 struct device_attribute *attr, char *buf)
45 struct amdgpu_hive_info *hive =
46 container_of(attr, struct amdgpu_hive_info, dev_attr);
48 return snprintf(buf, PAGE_SIZE, "%llu\n", hive->hive_id);
51 static int amdgpu_xgmi_sysfs_create(struct amdgpu_device *adev,
52 struct amdgpu_hive_info *hive)
56 if (WARN_ON(hive->kobj))
59 hive->kobj = kobject_create_and_add("xgmi_hive_info", &adev->dev->kobj);
61 dev_err(adev->dev, "XGMI: Failed to allocate sysfs entry!\n");
65 hive->dev_attr = (struct device_attribute) {
67 .name = "xgmi_hive_id",
71 .show = amdgpu_xgmi_show_hive_id,
74 ret = sysfs_create_file(hive->kobj, &hive->dev_attr.attr);
76 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_hive_id\n");
77 kobject_del(hive->kobj);
78 kobject_put(hive->kobj);
85 static void amdgpu_xgmi_sysfs_destroy(struct amdgpu_device *adev,
86 struct amdgpu_hive_info *hive)
88 sysfs_remove_file(hive->kobj, &hive->dev_attr.attr);
89 kobject_del(hive->kobj);
90 kobject_put(hive->kobj);
94 static ssize_t amdgpu_xgmi_show_device_id(struct device *dev,
95 struct device_attribute *attr,
98 struct drm_device *ddev = dev_get_drvdata(dev);
99 struct amdgpu_device *adev = ddev->dev_private;
101 return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.xgmi.node_id);
106 static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL);
109 static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev,
110 struct amdgpu_hive_info *hive)
113 char node[10] = { 0 };
115 /* Create xgmi device id file */
116 ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id);
118 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n");
122 /* Create sysfs link to hive info folder on the first device */
123 if (adev != hive->adev) {
124 ret = sysfs_create_link(&adev->dev->kobj, hive->kobj,
127 dev_err(adev->dev, "XGMI: Failed to create link to hive info");
132 sprintf(node, "node%d", hive->number_devices);
133 /* Create sysfs link form the hive folder to yourself */
134 ret = sysfs_create_link(hive->kobj, &adev->dev->kobj, node);
136 dev_err(adev->dev, "XGMI: Failed to create link from hive info");
144 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
147 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
153 static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev,
154 struct amdgpu_hive_info *hive)
156 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
157 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
158 sysfs_remove_link(hive->kobj, adev->ddev->unique);
163 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock)
166 struct amdgpu_hive_info *tmp;
168 if (!adev->gmc.xgmi.hive_id)
171 mutex_lock(&xgmi_mutex);
173 for (i = 0 ; i < hive_count; ++i) {
174 tmp = &xgmi_hives[i];
175 if (tmp->hive_id == adev->gmc.xgmi.hive_id) {
177 mutex_lock(&tmp->hive_lock);
178 mutex_unlock(&xgmi_mutex);
182 if (i >= AMDGPU_MAX_XGMI_HIVE) {
183 mutex_unlock(&xgmi_mutex);
187 /* initialize new hive if not exist */
188 tmp = &xgmi_hives[hive_count++];
190 if (amdgpu_xgmi_sysfs_create(adev, tmp)) {
191 mutex_unlock(&xgmi_mutex);
196 tmp->hive_id = adev->gmc.xgmi.hive_id;
197 INIT_LIST_HEAD(&tmp->device_list);
198 mutex_init(&tmp->hive_lock);
199 mutex_init(&tmp->reset_lock);
202 mutex_lock(&tmp->hive_lock);
204 mutex_unlock(&xgmi_mutex);
209 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
213 /* Each psp need to set the latest topology */
214 ret = psp_xgmi_set_topology_info(&adev->psp,
215 hive->number_devices,
216 &hive->topology_info);
219 "XGMI: Set topology failure on device %llx, hive %llx, ret %d",
220 adev->gmc.xgmi.node_id,
221 adev->gmc.xgmi.hive_id, ret);
226 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
228 struct psp_xgmi_topology_info *hive_topology;
229 struct amdgpu_hive_info *hive;
230 struct amdgpu_xgmi *entry;
231 struct amdgpu_device *tmp_adev = NULL;
233 int count = 0, ret = -EINVAL;
235 if (!adev->gmc.xgmi.supported)
238 ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
241 "XGMI: Failed to get node id\n");
245 ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
248 "XGMI: Failed to get hive id\n");
252 hive = amdgpu_get_xgmi_hive(adev, 1);
256 "XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n",
257 adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id);
261 hive_topology = &hive->topology_info;
263 list_add_tail(&adev->gmc.xgmi.head, &hive->device_list);
264 list_for_each_entry(entry, &hive->device_list, head)
265 hive_topology->nodes[count++].node_id = entry->node_id;
266 hive->number_devices = count;
268 /* Each psp need to get the latest topology */
269 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
270 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count, hive_topology);
272 dev_err(tmp_adev->dev,
273 "XGMI: Get topology failure on device %llx, hive %llx, ret %d",
274 tmp_adev->gmc.xgmi.node_id,
275 tmp_adev->gmc.xgmi.hive_id, ret);
276 /* To do : continue with some node failed or disable the whole hive */
281 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
282 ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
288 ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive);
291 dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n",
292 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id);
294 dev_err(adev->dev, "XGMI: Failed to add node %d, hive 0x%llx ret: %d\n",
295 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id,
299 mutex_unlock(&hive->hive_lock);
304 void amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
306 struct amdgpu_hive_info *hive;
308 if (!adev->gmc.xgmi.supported)
311 hive = amdgpu_get_xgmi_hive(adev, 1);
315 if (!(hive->number_devices--)) {
316 amdgpu_xgmi_sysfs_destroy(adev, hive);
317 mutex_destroy(&hive->hive_lock);
318 mutex_destroy(&hive->reset_lock);
320 amdgpu_xgmi_sysfs_rem_dev_info(adev, hive);
321 mutex_unlock(&hive->hive_lock);