]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_xcp.c
drm/amdgpu: Add utility functions for xcp
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_xcp.c
1 /*
2  * Copyright 2022 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 #include "amdgpu.h"
24 #include "amdgpu_xcp.h"
25
26 static int __amdgpu_xcp_run(struct amdgpu_xcp_mgr *xcp_mgr,
27                             struct amdgpu_xcp_ip *xcp_ip, int xcp_state)
28 {
29         int (*run_func)(void *handle, uint32_t inst_mask);
30         int ret = 0;
31
32         if (!xcp_ip || !xcp_ip->valid || !xcp_ip->ip_funcs)
33                 return 0;
34
35         run_func = NULL;
36
37         switch (xcp_state) {
38         case AMDGPU_XCP_PREPARE_SUSPEND:
39                 run_func = xcp_ip->ip_funcs->prepare_suspend;
40                 break;
41         case AMDGPU_XCP_SUSPEND:
42                 run_func = xcp_ip->ip_funcs->suspend;
43                 break;
44         case AMDGPU_XCP_PREPARE_RESUME:
45                 run_func = xcp_ip->ip_funcs->prepare_resume;
46                 break;
47         case AMDGPU_XCP_RESUME:
48                 run_func = xcp_ip->ip_funcs->resume;
49                 break;
50         }
51
52         if (run_func)
53                 ret = run_func(xcp_mgr->adev, xcp_ip->inst_mask);
54
55         return ret;
56 }
57
58 static int amdgpu_xcp_run_transition(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
59                                      int state)
60 {
61         struct amdgpu_xcp_ip *xcp_ip;
62         struct amdgpu_xcp *xcp;
63         int i, ret;
64
65         if (xcp_id > MAX_XCP || !xcp_mgr->xcp[xcp_id].valid)
66                 return -EINVAL;
67
68         xcp = &xcp_mgr->xcp[xcp_id];
69         for (i = 0; i < AMDGPU_XCP_MAX_BLOCKS; ++i) {
70                 xcp_ip = &xcp->ip[i];
71                 ret = __amdgpu_xcp_run(xcp_mgr, xcp_ip, state);
72                 if (ret)
73                         break;
74         }
75
76         return ret;
77 }
78
79 int amdgpu_xcp_prepare_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
80 {
81         return amdgpu_xcp_run_transition(xcp_mgr, xcp_id,
82                                          AMDGPU_XCP_PREPARE_SUSPEND);
83 }
84
85 int amdgpu_xcp_suspend(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
86 {
87         return amdgpu_xcp_run_transition(xcp_mgr, xcp_id, AMDGPU_XCP_SUSPEND);
88 }
89
90 int amdgpu_xcp_prepare_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
91 {
92         return amdgpu_xcp_run_transition(xcp_mgr, xcp_id,
93                                          AMDGPU_XCP_PREPARE_RESUME);
94 }
95
96 int amdgpu_xcp_resume(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id)
97 {
98         return amdgpu_xcp_run_transition(xcp_mgr, xcp_id, AMDGPU_XCP_RESUME);
99 }
100
101 static void __amdgpu_xcp_add_block(struct amdgpu_xcp_mgr *xcp_mgr, int xcp_id,
102                                    struct amdgpu_xcp_ip *ip)
103 {
104         struct amdgpu_xcp *xcp;
105
106         if (!ip)
107                 return;
108
109         xcp = &xcp_mgr->xcp[xcp_id];
110         xcp->ip[ip->ip_id] = *ip;
111         xcp->ip[ip->ip_id].valid = true;
112
113         xcp->valid = true;
114 }
115
116 static int __amdgpu_xcp_init(struct amdgpu_xcp_mgr *xcp_mgr, int num_xcps)
117 {
118         struct amdgpu_xcp_ip ip;
119         int i, j, ret;
120
121         for (i = 0; i < MAX_XCP; ++i)
122                 xcp_mgr->xcp[i].valid = false;
123
124         for (i = 0; i < num_xcps; ++i) {
125                 for (j = AMDGPU_XCP_GFXHUB; j < AMDGPU_XCP_MAX_BLOCKS; ++j) {
126                         ret = xcp_mgr->funcs->get_ip_details(xcp_mgr, i, j,
127                                                              &ip);
128                         if (ret)
129                                 continue;
130
131                         __amdgpu_xcp_add_block(xcp_mgr, i, &ip);
132                 }
133         }
134
135         xcp_mgr->num_xcps = num_xcps;
136
137         return 0;
138 }
139
140 int amdgpu_xcp_switch_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, int mode)
141 {
142         int ret, curr_mode, num_xcps = 0;
143
144         if (!xcp_mgr || mode == AMDGPU_XCP_MODE_NONE)
145                 return -EINVAL;
146
147         if (xcp_mgr->mode == mode)
148                 return 0;
149
150         if (!xcp_mgr->funcs || !xcp_mgr->funcs->switch_partition_mode)
151                 return 0;
152
153         mutex_lock(&xcp_mgr->xcp_lock);
154
155         curr_mode = xcp_mgr->mode;
156         /* State set to transient mode */
157         xcp_mgr->mode = AMDGPU_XCP_MODE_TRANS;
158
159         ret = xcp_mgr->funcs->switch_partition_mode(xcp_mgr, mode, &num_xcps);
160
161         if (ret) {
162                 /* Failed, get whatever mode it's at now */
163                 if (xcp_mgr->funcs->query_partition_mode)
164                         xcp_mgr->mode = amdgpu_xcp_query_partition_mode(
165                                 xcp_mgr, AMDGPU_XCP_FL_LOCKED);
166                 else
167                         xcp_mgr->mode = curr_mode;
168
169                 goto out;
170         }
171
172         if (!num_xcps || num_xcps > MAX_XCP) {
173                 ret = -EINVAL;
174                 goto out;
175         }
176
177         xcp_mgr->mode = mode;
178         __amdgpu_xcp_init(xcp_mgr, num_xcps);
179 out:
180         mutex_unlock(&xcp_mgr->xcp_lock);
181
182         return ret;
183 }
184
185 int amdgpu_xcp_query_partition_mode(struct amdgpu_xcp_mgr *xcp_mgr, u32 flags)
186 {
187         int mode;
188
189         if (xcp_mgr->mode == AMDGPU_XCP_MODE_NONE)
190                 return xcp_mgr->mode;
191
192         if (!xcp_mgr->funcs || !xcp_mgr->funcs->query_partition_mode)
193                 return xcp_mgr->mode;
194
195         if (!(flags & AMDGPU_XCP_FL_LOCKED))
196                 mutex_lock(&xcp_mgr->xcp_lock);
197         mode = xcp_mgr->funcs->query_partition_mode(xcp_mgr);
198         if (xcp_mgr->mode != AMDGPU_XCP_MODE_TRANS && mode != xcp_mgr->mode)
199                 dev_WARN(
200                         xcp_mgr->adev->dev,
201                         "Cached partition mode %d not matching with device mode %d",
202                         xcp_mgr->mode, mode);
203
204         if (!(flags & AMDGPU_XCP_FL_LOCKED))
205                 mutex_unlock(&xcp_mgr->xcp_lock);
206
207         return mode;
208 }
209
210 int amdgpu_xcp_mgr_init(struct amdgpu_device *adev, int init_mode,
211                         int init_num_xcps,
212                         struct amdgpu_xcp_mgr_funcs *xcp_funcs)
213 {
214         struct amdgpu_xcp_mgr *xcp_mgr;
215
216         if (!xcp_funcs || !xcp_funcs->switch_partition_mode ||
217             !xcp_funcs->get_ip_details)
218                 return -EINVAL;
219
220         xcp_mgr = kzalloc(sizeof(*xcp_mgr), GFP_KERNEL);
221
222         if (!xcp_mgr)
223                 return -ENOMEM;
224
225         xcp_mgr->adev = adev;
226         xcp_mgr->funcs = xcp_funcs;
227         xcp_mgr->mode = init_mode;
228         mutex_init(&xcp_mgr->xcp_lock);
229
230         if (init_mode != AMDGPU_XCP_MODE_NONE)
231                 __amdgpu_xcp_init(xcp_mgr, init_num_xcps);
232
233         adev->xcp_mgr = xcp_mgr;
234
235         return 0;
236 }
237
238 int amdgpu_xcp_get_partition(struct amdgpu_xcp_mgr *xcp_mgr,
239                              enum AMDGPU_XCP_IP_BLOCK ip, int instance)
240 {
241         struct amdgpu_xcp *xcp;
242         int i, id_mask = 0;
243
244         if (ip >= AMDGPU_XCP_MAX_BLOCKS)
245                 return -EINVAL;
246
247         for (i = 0; i < xcp_mgr->num_xcps; ++i) {
248                 xcp = &xcp_mgr->xcp[i];
249                 if ((xcp->valid) && (xcp->ip[ip].valid) &&
250                     (xcp->ip[ip].inst_mask & BIT(instance)))
251                         id_mask |= BIT(i);
252         }
253
254         if (!id_mask)
255                 id_mask = -ENXIO;
256
257         return id_mask;
258 }
259
260 int amdgpu_xcp_get_inst_details(struct amdgpu_xcp *xcp,
261                                 enum AMDGPU_XCP_IP_BLOCK ip,
262                                 uint32_t *inst_mask)
263 {
264         if (!xcp->valid || !inst_mask || !(xcp->ip[ip].valid))
265                 return -EINVAL;
266
267         *inst_mask = xcp->ip[ip].inst_mask;
268
269         return 0;
270 }
This page took 0.051145 seconds and 4 git commands to generate.