Merge branch 'work.iov_iter' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-microblaze.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
38 void *amdgpu_xgmi_hive_try_lock(struct amdgpu_hive_info *hive)
39 {
40         return &hive->device_list;
41 }
42
43 struct amdgpu_hive_info *amdgpu_get_xgmi_hive(struct amdgpu_device *adev, int lock)
44 {
45         int i;
46         struct amdgpu_hive_info *tmp;
47
48         if (!adev->gmc.xgmi.hive_id)
49                 return NULL;
50
51         mutex_lock(&xgmi_mutex);
52
53         for (i = 0 ; i < hive_count; ++i) {
54                 tmp = &xgmi_hives[i];
55                 if (tmp->hive_id == adev->gmc.xgmi.hive_id) {
56                         if (lock)
57                                 mutex_lock(&tmp->hive_lock);
58                         mutex_unlock(&xgmi_mutex);
59                         return tmp;
60                 }
61         }
62         if (i >= AMDGPU_MAX_XGMI_HIVE) {
63                 mutex_unlock(&xgmi_mutex);
64                 return NULL;
65         }
66
67         /* initialize new hive if not exist */
68         tmp = &xgmi_hives[hive_count++];
69         tmp->hive_id = adev->gmc.xgmi.hive_id;
70         INIT_LIST_HEAD(&tmp->device_list);
71         mutex_init(&tmp->hive_lock);
72         mutex_init(&tmp->reset_lock);
73         if (lock)
74                 mutex_lock(&tmp->hive_lock);
75
76         mutex_unlock(&xgmi_mutex);
77
78         return tmp;
79 }
80
81 int amdgpu_xgmi_update_topology(struct amdgpu_hive_info *hive, struct amdgpu_device *adev)
82 {
83         int ret = -EINVAL;
84
85         /* Each psp need to set the latest topology */
86         ret = psp_xgmi_set_topology_info(&adev->psp,
87                                          hive->number_devices,
88                                          &hive->topology_info);
89         if (ret)
90                 dev_err(adev->dev,
91                         "XGMI: Set topology failure on device %llx, hive %llx, ret %d",
92                         adev->gmc.xgmi.node_id,
93                         adev->gmc.xgmi.hive_id, ret);
94
95         return ret;
96 }
97
98 int amdgpu_xgmi_add_device(struct amdgpu_device *adev)
99 {
100         struct psp_xgmi_topology_info *hive_topology;
101         struct amdgpu_hive_info *hive;
102         struct amdgpu_xgmi      *entry;
103         struct amdgpu_device *tmp_adev = NULL;
104
105         int count = 0, ret = -EINVAL;
106
107         if (!adev->gmc.xgmi.supported)
108                 return 0;
109
110         ret = psp_xgmi_get_node_id(&adev->psp, &adev->gmc.xgmi.node_id);
111         if (ret) {
112                 dev_err(adev->dev,
113                         "XGMI: Failed to get node id\n");
114                 return ret;
115         }
116
117         ret = psp_xgmi_get_hive_id(&adev->psp, &adev->gmc.xgmi.hive_id);
118         if (ret) {
119                 dev_err(adev->dev,
120                         "XGMI: Failed to get hive id\n");
121                 return ret;
122         }
123
124         hive = amdgpu_get_xgmi_hive(adev, 1);
125         if (!hive) {
126                 ret = -EINVAL;
127                 dev_err(adev->dev,
128                         "XGMI: node 0x%llx, can not match hive 0x%llx in the hive list.\n",
129                         adev->gmc.xgmi.node_id, adev->gmc.xgmi.hive_id);
130                 goto exit;
131         }
132
133         hive_topology = &hive->topology_info;
134
135         list_add_tail(&adev->gmc.xgmi.head, &hive->device_list);
136         list_for_each_entry(entry, &hive->device_list, head)
137                 hive_topology->nodes[count++].node_id = entry->node_id;
138         hive->number_devices = count;
139
140         /* Each psp need to get the latest topology */
141         list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
142                 ret = psp_xgmi_get_topology_info(&tmp_adev->psp, count, hive_topology);
143                 if (ret) {
144                         dev_err(tmp_adev->dev,
145                                 "XGMI: Get topology failure on device %llx, hive %llx, ret %d",
146                                 tmp_adev->gmc.xgmi.node_id,
147                                 tmp_adev->gmc.xgmi.hive_id, ret);
148                         /* To do : continue with some node failed or disable the whole hive */
149                         break;
150                 }
151         }
152
153         list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
154                 ret = amdgpu_xgmi_update_topology(hive, tmp_adev);
155                 if (ret)
156                         break;
157         }
158
159         dev_info(adev->dev, "XGMI: Add node %d, hive 0x%llx.\n",
160                  adev->gmc.xgmi.physical_node_id, adev->gmc.xgmi.hive_id);
161
162         mutex_unlock(&hive->hive_lock);
163 exit:
164         return ret;
165 }
166
167 void amdgpu_xgmi_remove_device(struct amdgpu_device *adev)
168 {
169         struct amdgpu_hive_info *hive;
170
171         if (!adev->gmc.xgmi.supported)
172                 return;
173
174         hive = amdgpu_get_xgmi_hive(adev, 1);
175         if (!hive)
176                 return;
177
178         if (!(hive->number_devices--)) {
179                 mutex_destroy(&hive->hive_lock);
180                 mutex_destroy(&hive->reset_lock);
181         } else {
182                 mutex_unlock(&hive->hive_lock);
183         }
184 }