drm/amdgpu: Add sysfs entries for xgmi hive v2.
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_xgmi.c
1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  *
23  */
24 #include <linux/list.h>
25 #include "amdgpu.h"
26 #include "amdgpu_xgmi.h"
27
28
29 static DEFINE_MUTEX(xgmi_mutex);
30
31 #define AMDGPU_MAX_XGMI_HIVE                    8
32 #define AMDGPU_MAX_XGMI_DEVICE_PER_HIVE         4
33
34 static struct amdgpu_hive_info xgmi_hives[AMDGPU_MAX_XGMI_HIVE];
35 static unsigned hive_count = 0;
36
37 void *amdgpu_xgmi_hive_try_lock(struct amdgpu_hive_info *hive)
38 {
39         return &hive->device_list;
40 }
41
42 static ssize_t amdgpu_xgmi_show_hive_id(struct device *dev,
43                 struct device_attribute *attr, char *buf)
44 {
45         struct amdgpu_hive_info *hive =
46                         container_of(attr, struct amdgpu_hive_info, dev_attr);
47
48         return snprintf(buf, PAGE_SIZE, "%llu\n", hive->hive_id);
49 }
50
51 static int amdgpu_xgmi_sysfs_create(struct amdgpu_device *adev,
52                                     struct amdgpu_hive_info *hive)
53 {
54         int ret = 0;
55
56         if (WARN_ON(hive->kobj))
57                 return -EINVAL;
58
59         hive->kobj = kobject_create_and_add("xgmi_hive_info", &adev->dev->kobj);
60         if (!hive->kobj) {
61                 dev_err(adev->dev, "XGMI: Failed to allocate sysfs entry!\n");
62                 return -EINVAL;
63         }
64
65         hive->dev_attr = (struct device_attribute) {
66                 .attr = {
67                         .name = "xgmi_hive_id",
68                         .mode = S_IRUGO,
69
70                 },
71                 .show = amdgpu_xgmi_show_hive_id,
72         };
73
74         ret = sysfs_create_file(hive->kobj, &hive->dev_attr.attr);
75         if (ret) {
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);
79                 hive->kobj = NULL;
80         }
81
82         return ret;
83 }
84
85 static void amdgpu_xgmi_sysfs_destroy(struct amdgpu_device *adev,
86                                     struct amdgpu_hive_info *hive)
87 {
88         sysfs_remove_file(hive->kobj, &hive->dev_attr.attr);
89         kobject_del(hive->kobj);
90         kobject_put(hive->kobj);
91         hive->kobj = NULL;
92 }
93
94 static ssize_t amdgpu_xgmi_show_device_id(struct device *dev,
95                                      struct device_attribute *attr,
96                                      char *buf)
97 {
98         struct drm_device *ddev = dev_get_drvdata(dev);
99         struct amdgpu_device *adev = ddev->dev_private;
100
101         return snprintf(buf, PAGE_SIZE, "%llu\n", adev->gmc.xgmi.node_id);
102
103 }
104
105
106 static DEVICE_ATTR(xgmi_device_id, S_IRUGO, amdgpu_xgmi_show_device_id, NULL);
107
108
109 static int amdgpu_xgmi_sysfs_add_dev_info(struct amdgpu_device *adev,
110                                          struct amdgpu_hive_info *hive)
111 {
112         int ret = 0;
113         char node[10] = { 0 };
114
115         /* Create xgmi device id file */
116         ret = device_create_file(adev->dev, &dev_attr_xgmi_device_id);
117         if (ret) {
118                 dev_err(adev->dev, "XGMI: Failed to create device file xgmi_device_id\n");
119                 return ret;
120         }
121
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,
125                                         "xgmi_hive_info");
126                 if (ret) {
127                         dev_err(adev->dev, "XGMI: Failed to create link to hive info");
128                         goto remove_file;
129                 }
130         }
131
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);
135         if (ret) {
136                 dev_err(adev->dev, "XGMI: Failed to create link from hive info");
137                 goto remove_link;
138         }
139
140         goto success;
141
142
143 remove_link:
144         sysfs_remove_link(&adev->dev->kobj, adev->ddev->unique);
145
146 remove_file:
147         device_remove_file(adev->dev, &dev_attr_xgmi_device_id);
148
149 success:
150         return ret;
151 }
152
153 static void amdgpu_xgmi_sysfs_rem_dev_info(struct amdgpu_device *adev,
154                                           struct amdgpu_hive_info *hive)
155 {
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);
159 }
160
161
162
163 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock)
164 {
165         int i;
166         struct amdgpu_hive_info *tmp;
167
168         if (!adev->gmc.xgmi.hive_id)
169                 return NULL;
170
171         mutex_lock(&xgmi_mutex);
172
173         for (i = 0 ; i < hive_count; ++i) {
174                 tmp = &xgmi_hives[i];
175                 if (tmp->hive_id == adev->gmc.xgmi.hive_id) {
176                         if (lock)
177                                 mutex_lock(&tmp->hive_lock);
178                         mutex_unlock(&xgmi_mutex);
179                         return tmp;
180                 }
181         }
182         if (i >= AMDGPU_MAX_XGMI_HIVE) {
183                 mutex_unlock(&xgmi_mutex);
184                 return NULL;
185         }
186
187         /* initialize new hive if not exist */
188         tmp = &xgmi_hives[hive_count++];
189
190         if (amdgpu_xgmi_sysfs_create(adev, tmp)) {
191                 mutex_unlock(&xgmi_mutex);
192                 return NULL;
193         }
194
195         tmp->adev = adev;
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);
200
201         if (lock)
202                 mutex_lock(&tmp->hive_lock);
203
204         mutex_unlock(&xgmi_mutex);
205
206         return tmp;
207 }
208
209 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
210 {
211         int ret = -EINVAL;
212
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);
217         if (ret)
218                 dev_err(adev->dev,
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);
222
223         return ret;
224 }
225
226 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
227 {
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;
232
233         int count = 0, ret = -EINVAL;
234
235         if (!adev->gmc.xgmi.supported)
236                 return 0;
237
238         ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
239         if (ret) {
240                 dev_err(adev->dev,
241                         "XGMI: Failed to get node id\n");
242                 return ret;
243         }
244
245         ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
246         if (ret) {
247                 dev_err(adev->dev,
248                         "XGMI: Failed to get hive id\n");
249                 return ret;
250         }
251
252         hive = amdgpu_get_xgmi_hive(adev, 1);
253         if (!hive) {
254                 ret = -EINVAL;
255                 dev_err(adev->dev,
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);
258                 goto exit;
259         }
260
261         hive_topology = &hive->topology_info;
262
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;
267
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);
271                 if (ret) {
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 */
277                         break;
278                 }
279         }
280
281         list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
282                 ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
283                 if (ret)
284                         break;
285         }
286
287         if (!ret)
288                 ret = amdgpu_xgmi_sysfs_add_dev_info(adev, hive);
289
290         if (!ret)
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);
293         else
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,
296                         ret);
297
298
299         mutex_unlock(&hive->hive_lock);
300 exit:
301         return ret;
302 }
303
304 void amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
305 {
306         struct amdgpu_hive_info *hive;
307
308         if (!adev->gmc.xgmi.supported)
309                 return;
310
311         hive = amdgpu_get_xgmi_hive(adev, 1);
312         if (!hive)
313                 return;
314
315         if (!(hive->number_devices--)) {
316                 amdgpu_xgmi_sysfs_destroy(adev, hive);
317                 mutex_destroy(&hive->hive_lock);
318                 mutex_destroy(&hive->reset_lock);
319         } else {
320                 amdgpu_xgmi_sysfs_rem_dev_info(adev, hive);
321                 mutex_unlock(&hive->hive_lock);
322         }
323 }