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;
43 static ssize_t amdgpu_xgmi_show_hive_id(struct device *dev,
44 struct device_attribute *attr, char *buf)
46 struct amdgpu_hive_info *hive =
47 container_of(attr, struct amdgpu_hive_info, dev_attr);
49 return snprintf(buf, PAGE_SIZE, "%llu\n", hive->hive_id);
52 static int amdgpu_xgmi_sysfs_create(struct amdgpu_device *adev,
53 struct amdgpu_hive_info *hive)
57 if (WARN_ON(hive->kobj))
60 hive->kobj = kobject_create_and_add("xgmi_hive_info", &adev->dev->kobj);
62 dev_err(adev->dev, "XGMI: Failed to allocate sysfs entry!\n");
66 hive->dev_attr = (struct device_attribute) {
68 .name = "xgmi_hive_id",
72 .show = amdgpu_xgmi_show_hive_id,
75 ret = sysfs_create_file(hive->kobj, &hive->dev_attr.attr);
77 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_hive_id\n");
78 kobject_del(hive->kobj);
79 kobject_put(hive->kobj);
86 static void amdgpu_xgmi_sysfs_destroy(struct amdgpu_device *adev,
87 struct amdgpu_hive_info *hive)
89 sysfs_remove_file(hive->kobj, &hive->dev_attr.attr);
90 kobject_del(hive->kobj);
91 kobject_put(hive->kobj);
95 static ssize_t amdgpu_xgmi_show_device_id(struct device *dev,
96 struct device_attribute *attr,
99 struct drm_device *ddev = dev_get_drvdata(dev);
100 struct amdgpu_device *adev = ddev->dev_private;
102 return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.xgmi.node_id);
107 static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL);
110 static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev,
111 struct amdgpu_hive_info *hive)
114 char node[10] = { 0 };
116 /* Create xgmi device id file */
117 ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id);
119 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n");
123 /* Create sysfs link to hive info folder on the first device */
124 if (adev != hive->adev) {
125 ret = sysfs_create_link(&adev->dev->kobj, hive->kobj,
128 dev_err(adev->dev, "XGMI: Failed to create link to hive info");
133 sprintf(node, "node%d", hive->number_devices);
134 /* Create sysfs link form the hive folder to yourself */
135 ret = sysfs_create_link(hive->kobj, &adev->dev->kobj, node);
137 dev_err(adev->dev, "XGMI: Failed to create link from hive info");
145 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
148 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
154 static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev,
155 struct amdgpu_hive_info *hive)
157 device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
158 sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
159 sysfs_remove_link(hive->kobj, adev->ddev->unique);
164 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock)
167 struct amdgpu_hive_info *tmp;
169 if (!adev->gmc.xgmi.hive_id)
172 mutex_lock(&xgmi_mutex);
174 for (i = 0 ; i < hive_count; ++i) {
175 tmp = &xgmi_hives[i];
176 if (tmp->hive_id == adev->gmc.xgmi.hive_id) {
178 mutex_lock(&tmp->hive_lock);
179 mutex_unlock(&xgmi_mutex);
183 if (i >= AMDGPU_MAX_XGMI_HIVE) {
184 mutex_unlock(&xgmi_mutex);
188 /* initialize new hive if not exist */
189 tmp = &xgmi_hives[hive_count++];
191 if (amdgpu_xgmi_sysfs_create(adev, tmp)) {
192 mutex_unlock(&xgmi_mutex);
197 tmp->hive_id = adev->gmc.xgmi.hive_id;
198 INIT_LIST_HEAD(&tmp->device_list);
199 mutex_init(&tmp->hive_lock);
200 mutex_init(&tmp->reset_lock);
203 mutex_lock(&tmp->hive_lock);
205 mutex_unlock(&xgmi_mutex);
210 int amdgpu_xgmi_set_pstate(struct amdgpu_device *adev, int pstate)
213 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev, 0);
218 if (hive->pstate == pstate)
221 dev_dbg(adev->dev, "Set xgmi pstate %d.\n", pstate);
223 if (is_support_sw_smu(adev))
224 ret = smu_set_xgmi_pstate(&adev->smu, pstate);
227 "XGMI: Set pstate failure on device %llx, hive %llx, ret %d",
228 adev->gmc.xgmi.node_id,
229 adev->gmc.xgmi.hive_id, ret);
234 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
238 /* Each psp need to set the latest topology */
239 ret = psp_xgmi_set_topology_info(&adev->psp,
240 hive->number_devices,
241 &hive->topology_info);
244 "XGMI: Set topology failure on device %llx, hive %llx, ret %d",
245 adev->gmc.xgmi.node_id,
246 adev->gmc.xgmi.hive_id, ret);
251 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
253 struct psp_xgmi_topology_info *hive_topology;
254 struct amdgpu_hive_info *hive;
255 struct amdgpu_xgmi *entry;
256 struct amdgpu_device *tmp_adev = NULL;
258 int count = 0, ret = -EINVAL;
260 if (!adev->gmc.xgmi.supported)
263 ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
266 "XGMI: Failed to get node id\n");
270 ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
273 "XGMI: Failed to get hive id\n");
277 hive = amdgpu_get_xgmi_hive(adev, 1);
281 "XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n",
282 adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id);
286 hive_topology = &hive->topology_info;
288 list_add_tail(&adev->gmc.xgmi.head, &hive->device_list);
289 list_for_each_entry(entry, &hive->device_list, head)
290 hive_topology->nodes[count++].node_id = entry->node_id;
291 hive->number_devices = count;
293 /* Each psp need to get the latest topology */
294 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
295 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count, hive_topology);
297 dev_err(tmp_adev->dev,
298 "XGMI: Get topology failure on device %llx, hive %llx, ret %d",
299 tmp_adev->gmc.xgmi.node_id,
300 tmp_adev->gmc.xgmi.hive_id, ret);
301 /* To do : continue with some node failed or disable the whole hive */
306 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
307 ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
313 ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive);
316 dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n",
317 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id);
319 dev_err(adev->dev, "XGMI: Failed to add node %d, hive 0x%llx ret: %d\n",
320 adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id,
324 mutex_unlock(&hive->hive_lock);
329 void amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
331 struct amdgpu_hive_info *hive;
333 if (!adev->gmc.xgmi.supported)
336 hive = amdgpu_get_xgmi_hive(adev, 1);
340 if (!(hive->number_devices--)) {
341 amdgpu_xgmi_sysfs_destroy(adev, hive);
342 mutex_destroy(&hive->hive_lock);
343 mutex_destroy(&hive->reset_lock);
345 amdgpu_xgmi_sysfs_rem_dev_info(adev, hive);
346 mutex_unlock(&hive->hive_lock);